2 * Copyright (c) 2012-2016 VMware, Inc. All rights reserved.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of EITHER the GNU General Public License
6 * version 2 as published by the Free Software Foundation or the BSD
7 * 2-Clause License. This program is distributed in the hope that it
8 * will be useful, but WITHOUT ANY WARRANTY; WITHOUT EVEN THE IMPLIED
9 * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
10 * See the GNU General Public License version 2 for more details at
11 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.en.html.
13 * You should have received a copy of the GNU General Public License
14 * along with this program available in the file COPYING in the main
15 * directory of this source tree.
17 * The BSD 2-Clause License
19 * Redistribution and use in source and binary forms, with or
20 * without modification, are permitted provided that the following
23 * - Redistributions of source code must retain the above
24 * copyright notice, this list of conditions and the following
27 * - Redistributions in binary form must reproduce the above
28 * copyright notice, this list of conditions and the following
29 * disclaimer in the documentation and/or other materials
30 * provided with the distribution.
32 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
33 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
34 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
35 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
36 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
37 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
38 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
39 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
40 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
41 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
42 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
43 * OF THE POSSIBILITY OF SUCH DAMAGE.
47 #include <linux/inet.h>
49 #include <rdma/ib_addr.h>
50 #include <rdma/ib_smi.h>
51 #include <rdma/ib_user_verbs.h>
52 #include <rdma/vmw_pvrdma-abi.h>
57 * pvrdma_query_device - query device
58 * @ibdev: the device to query
59 * @props: the device properties
62 * @return: 0 on success, otherwise negative errno
64 int pvrdma_query_device(struct ib_device *ibdev,
65 struct ib_device_attr *props,
68 struct pvrdma_dev *dev = to_vdev(ibdev);
70 if (uhw->inlen || uhw->outlen)
73 memset(props, 0, sizeof(*props));
75 props->fw_ver = dev->dsr->caps.fw_ver;
76 props->sys_image_guid = dev->dsr->caps.sys_image_guid;
77 props->max_mr_size = dev->dsr->caps.max_mr_size;
78 props->page_size_cap = dev->dsr->caps.page_size_cap;
79 props->vendor_id = dev->dsr->caps.vendor_id;
80 props->vendor_part_id = dev->pdev->device;
81 props->hw_ver = dev->dsr->caps.hw_ver;
82 props->max_qp = dev->dsr->caps.max_qp;
83 props->max_qp_wr = dev->dsr->caps.max_qp_wr;
84 props->device_cap_flags = dev->dsr->caps.device_cap_flags;
85 props->max_sge = dev->dsr->caps.max_sge;
86 props->max_cq = dev->dsr->caps.max_cq;
87 props->max_cqe = dev->dsr->caps.max_cqe;
88 props->max_mr = dev->dsr->caps.max_mr;
89 props->max_pd = dev->dsr->caps.max_pd;
90 props->max_qp_rd_atom = dev->dsr->caps.max_qp_rd_atom;
91 props->max_qp_init_rd_atom = dev->dsr->caps.max_qp_init_rd_atom;
93 dev->dsr->caps.atomic_ops &
94 (PVRDMA_ATOMIC_OP_COMP_SWAP | PVRDMA_ATOMIC_OP_FETCH_ADD) ?
95 IB_ATOMIC_HCA : IB_ATOMIC_NONE;
96 props->masked_atomic_cap = props->atomic_cap;
97 props->max_ah = dev->dsr->caps.max_ah;
98 props->max_pkeys = dev->dsr->caps.max_pkeys;
99 props->local_ca_ack_delay = dev->dsr->caps.local_ca_ack_delay;
100 if ((dev->dsr->caps.bmme_flags & PVRDMA_BMME_FLAG_LOCAL_INV) &&
101 (dev->dsr->caps.bmme_flags & PVRDMA_BMME_FLAG_REMOTE_INV) &&
102 (dev->dsr->caps.bmme_flags & PVRDMA_BMME_FLAG_FAST_REG_WR)) {
103 props->device_cap_flags |= IB_DEVICE_MEM_MGT_EXTENSIONS;
110 * pvrdma_query_port - query device port attributes
111 * @ibdev: the device to query
112 * @port: the port number
113 * @props: the device properties
115 * @return: 0 on success, otherwise negative errno
117 int pvrdma_query_port(struct ib_device *ibdev, u8 port,
118 struct ib_port_attr *props)
120 struct pvrdma_dev *dev = to_vdev(ibdev);
121 union pvrdma_cmd_req req;
122 union pvrdma_cmd_resp rsp;
123 struct pvrdma_cmd_query_port *cmd = &req.query_port;
124 struct pvrdma_cmd_query_port_resp *resp = &rsp.query_port_resp;
127 memset(cmd, 0, sizeof(*cmd));
128 cmd->hdr.cmd = PVRDMA_CMD_QUERY_PORT;
129 cmd->port_num = port;
131 err = pvrdma_cmd_post(dev, &req, &rsp, PVRDMA_CMD_QUERY_PORT_RESP);
133 dev_warn(&dev->pdev->dev,
134 "could not query port, error: %d\n", err);
138 memset(props, 0, sizeof(*props));
140 props->state = pvrdma_port_state_to_ib(resp->attrs.state);
141 props->max_mtu = pvrdma_mtu_to_ib(resp->attrs.max_mtu);
142 props->active_mtu = pvrdma_mtu_to_ib(resp->attrs.active_mtu);
143 props->gid_tbl_len = resp->attrs.gid_tbl_len;
144 props->port_cap_flags =
145 pvrdma_port_cap_flags_to_ib(resp->attrs.port_cap_flags);
146 props->max_msg_sz = resp->attrs.max_msg_sz;
147 props->bad_pkey_cntr = resp->attrs.bad_pkey_cntr;
148 props->qkey_viol_cntr = resp->attrs.qkey_viol_cntr;
149 props->pkey_tbl_len = resp->attrs.pkey_tbl_len;
150 props->lid = resp->attrs.lid;
151 props->sm_lid = resp->attrs.sm_lid;
152 props->lmc = resp->attrs.lmc;
153 props->max_vl_num = resp->attrs.max_vl_num;
154 props->sm_sl = resp->attrs.sm_sl;
155 props->subnet_timeout = resp->attrs.subnet_timeout;
156 props->init_type_reply = resp->attrs.init_type_reply;
157 props->active_width = pvrdma_port_width_to_ib(resp->attrs.active_width);
158 props->active_speed = pvrdma_port_speed_to_ib(resp->attrs.active_speed);
159 props->phys_state = resp->attrs.phys_state;
165 * pvrdma_query_gid - query device gid
166 * @ibdev: the device to query
167 * @port: the port number
169 * @gid: the device gid value
171 * @return: 0 on success, otherwise negative errno
173 int pvrdma_query_gid(struct ib_device *ibdev, u8 port, int index,
176 struct pvrdma_dev *dev = to_vdev(ibdev);
178 if (index >= dev->dsr->caps.gid_tbl_len)
181 memcpy(gid, &dev->sgid_tbl[index], sizeof(union ib_gid));
187 * pvrdma_query_pkey - query device port's P_Key table
188 * @ibdev: the device to query
189 * @port: the port number
191 * @pkey: the device P_Key value
193 * @return: 0 on success, otherwise negative errno
195 int pvrdma_query_pkey(struct ib_device *ibdev, u8 port, u16 index,
199 union pvrdma_cmd_req req;
200 union pvrdma_cmd_resp rsp;
201 struct pvrdma_cmd_query_pkey *cmd = &req.query_pkey;
203 memset(cmd, 0, sizeof(*cmd));
204 cmd->hdr.cmd = PVRDMA_CMD_QUERY_PKEY;
205 cmd->port_num = port;
208 err = pvrdma_cmd_post(to_vdev(ibdev), &req, &rsp,
209 PVRDMA_CMD_QUERY_PKEY_RESP);
211 dev_warn(&to_vdev(ibdev)->pdev->dev,
212 "could not query pkey, error: %d\n", err);
216 *pkey = rsp.query_pkey_resp.pkey;
221 enum rdma_link_layer pvrdma_port_link_layer(struct ib_device *ibdev,
224 return IB_LINK_LAYER_ETHERNET;
227 int pvrdma_modify_device(struct ib_device *ibdev, int mask,
228 struct ib_device_modify *props)
232 if (mask & ~(IB_DEVICE_MODIFY_SYS_IMAGE_GUID |
233 IB_DEVICE_MODIFY_NODE_DESC)) {
234 dev_warn(&to_vdev(ibdev)->pdev->dev,
235 "unsupported device modify mask %#x\n", mask);
239 if (mask & IB_DEVICE_MODIFY_NODE_DESC) {
240 spin_lock_irqsave(&to_vdev(ibdev)->desc_lock, flags);
241 memcpy(ibdev->node_desc, props->node_desc, 64);
242 spin_unlock_irqrestore(&to_vdev(ibdev)->desc_lock, flags);
245 if (mask & IB_DEVICE_MODIFY_SYS_IMAGE_GUID) {
246 mutex_lock(&to_vdev(ibdev)->port_mutex);
247 to_vdev(ibdev)->sys_image_guid =
248 cpu_to_be64(props->sys_image_guid);
249 mutex_unlock(&to_vdev(ibdev)->port_mutex);
256 * pvrdma_modify_port - modify device port attributes
257 * @ibdev: the device to modify
258 * @port: the port number
259 * @mask: attributes to modify
260 * @props: the device properties
262 * @return: 0 on success, otherwise negative errno
264 int pvrdma_modify_port(struct ib_device *ibdev, u8 port, int mask,
265 struct ib_port_modify *props)
267 struct ib_port_attr attr;
268 struct pvrdma_dev *vdev = to_vdev(ibdev);
271 if (mask & ~IB_PORT_SHUTDOWN) {
272 dev_warn(&vdev->pdev->dev,
273 "unsupported port modify mask %#x\n", mask);
277 mutex_lock(&vdev->port_mutex);
278 ret = pvrdma_query_port(ibdev, port, &attr);
282 vdev->port_cap_mask |= props->set_port_cap_mask;
283 vdev->port_cap_mask &= ~props->clr_port_cap_mask;
285 if (mask & IB_PORT_SHUTDOWN)
286 vdev->ib_active = false;
289 mutex_unlock(&vdev->port_mutex);
294 * pvrdma_alloc_ucontext - allocate ucontext
295 * @ibdev: the IB device
298 * @return: the ib_ucontext pointer on success, otherwise errno.
300 struct ib_ucontext *pvrdma_alloc_ucontext(struct ib_device *ibdev,
301 struct ib_udata *udata)
303 struct pvrdma_dev *vdev = to_vdev(ibdev);
304 struct pvrdma_ucontext *context;
305 union pvrdma_cmd_req req;
306 union pvrdma_cmd_resp rsp;
307 struct pvrdma_cmd_create_uc *cmd = &req.create_uc;
308 struct pvrdma_cmd_create_uc_resp *resp = &rsp.create_uc_resp;
309 struct pvrdma_alloc_ucontext_resp uresp = {0};
313 if (!vdev->ib_active)
314 return ERR_PTR(-EAGAIN);
316 context = kmalloc(sizeof(*context), GFP_KERNEL);
318 return ERR_PTR(-ENOMEM);
321 ret = pvrdma_uar_alloc(vdev, &context->uar);
324 return ERR_PTR(-ENOMEM);
327 /* get ctx_handle from host */
328 memset(cmd, 0, sizeof(*cmd));
329 cmd->pfn = context->uar.pfn;
330 cmd->hdr.cmd = PVRDMA_CMD_CREATE_UC;
331 ret = pvrdma_cmd_post(vdev, &req, &rsp, PVRDMA_CMD_CREATE_UC_RESP);
333 dev_warn(&vdev->pdev->dev,
334 "could not create ucontext, error: %d\n", ret);
339 context->ctx_handle = resp->ctx_handle;
341 /* copy back to user */
342 uresp.qp_tab_size = vdev->dsr->caps.max_qp;
343 ret = ib_copy_to_udata(udata, &uresp, sizeof(uresp));
345 pvrdma_uar_free(vdev, &context->uar);
346 context->ibucontext.device = ibdev;
347 pvrdma_dealloc_ucontext(&context->ibucontext);
348 return ERR_PTR(-EFAULT);
351 return &context->ibucontext;
354 pvrdma_uar_free(vdev, &context->uar);
360 * pvrdma_dealloc_ucontext - deallocate ucontext
361 * @ibcontext: the ucontext
363 * @return: 0 on success, otherwise errno.
365 int pvrdma_dealloc_ucontext(struct ib_ucontext *ibcontext)
367 struct pvrdma_ucontext *context = to_vucontext(ibcontext);
368 union pvrdma_cmd_req req;
369 struct pvrdma_cmd_destroy_uc *cmd = &req.destroy_uc;
372 memset(cmd, 0, sizeof(*cmd));
373 cmd->hdr.cmd = PVRDMA_CMD_DESTROY_UC;
374 cmd->ctx_handle = context->ctx_handle;
376 ret = pvrdma_cmd_post(context->dev, &req, NULL, 0);
378 dev_warn(&context->dev->pdev->dev,
379 "destroy ucontext failed, error: %d\n", ret);
381 /* Free the UAR even if the device command failed */
382 pvrdma_uar_free(to_vdev(ibcontext->device), &context->uar);
389 * pvrdma_mmap - create mmap region
390 * @ibcontext: the user context
393 * @return: 0 on success, otherwise errno.
395 int pvrdma_mmap(struct ib_ucontext *ibcontext, struct vm_area_struct *vma)
397 struct pvrdma_ucontext *context = to_vucontext(ibcontext);
398 unsigned long start = vma->vm_start;
399 unsigned long size = vma->vm_end - vma->vm_start;
400 unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
402 dev_dbg(&context->dev->pdev->dev, "create mmap region\n");
404 if ((size != PAGE_SIZE) || (offset & ~PAGE_MASK)) {
405 dev_warn(&context->dev->pdev->dev,
406 "invalid params for mmap region\n");
410 /* Map UAR to kernel space, VM_LOCKED? */
411 vma->vm_flags |= VM_DONTCOPY | VM_DONTEXPAND;
412 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
413 if (io_remap_pfn_range(vma, start, context->uar.pfn, size,
421 * pvrdma_alloc_pd - allocate protection domain
422 * @ibdev: the IB device
423 * @context: user context
426 * @return: the ib_pd protection domain pointer on success, otherwise errno.
428 struct ib_pd *pvrdma_alloc_pd(struct ib_device *ibdev,
429 struct ib_ucontext *context,
430 struct ib_udata *udata)
432 struct pvrdma_pd *pd;
433 struct pvrdma_dev *dev = to_vdev(ibdev);
434 union pvrdma_cmd_req req;
435 union pvrdma_cmd_resp rsp;
436 struct pvrdma_cmd_create_pd *cmd = &req.create_pd;
437 struct pvrdma_cmd_create_pd_resp *resp = &rsp.create_pd_resp;
441 /* Check allowed max pds */
442 if (!atomic_add_unless(&dev->num_pds, 1, dev->dsr->caps.max_pd))
443 return ERR_PTR(-ENOMEM);
445 pd = kmalloc(sizeof(*pd), GFP_KERNEL);
447 ptr = ERR_PTR(-ENOMEM);
451 memset(cmd, 0, sizeof(*cmd));
452 cmd->hdr.cmd = PVRDMA_CMD_CREATE_PD;
453 cmd->ctx_handle = (context) ? to_vucontext(context)->ctx_handle : 0;
454 ret = pvrdma_cmd_post(dev, &req, &rsp, PVRDMA_CMD_CREATE_PD_RESP);
456 dev_warn(&dev->pdev->dev,
457 "failed to allocate protection domain, error: %d\n",
463 pd->privileged = !context;
464 pd->pd_handle = resp->pd_handle;
465 pd->pdn = resp->pd_handle;
468 if (ib_copy_to_udata(udata, &pd->pdn, sizeof(__u32))) {
469 dev_warn(&dev->pdev->dev,
470 "failed to copy back protection domain\n");
471 pvrdma_dealloc_pd(&pd->ibpd);
472 return ERR_PTR(-EFAULT);
482 atomic_dec(&dev->num_pds);
487 * pvrdma_dealloc_pd - deallocate protection domain
488 * @pd: the protection domain to be released
490 * @return: 0 on success, otherwise errno.
492 int pvrdma_dealloc_pd(struct ib_pd *pd)
494 struct pvrdma_dev *dev = to_vdev(pd->device);
495 union pvrdma_cmd_req req;
496 struct pvrdma_cmd_destroy_pd *cmd = &req.destroy_pd;
499 memset(cmd, 0, sizeof(*cmd));
500 cmd->hdr.cmd = PVRDMA_CMD_DESTROY_PD;
501 cmd->pd_handle = to_vpd(pd)->pd_handle;
503 ret = pvrdma_cmd_post(dev, &req, NULL, 0);
505 dev_warn(&dev->pdev->dev,
506 "could not dealloc protection domain, error: %d\n",
510 atomic_dec(&dev->num_pds);
516 * pvrdma_create_ah - create an address handle
517 * @pd: the protection domain
518 * @ah_attr: the attributes of the AH
519 * @udata: user data blob
521 * @return: the ib_ah pointer on success, otherwise errno.
523 struct ib_ah *pvrdma_create_ah(struct ib_pd *pd, struct ib_ah_attr *ah_attr,
524 struct ib_udata *udata)
526 struct pvrdma_dev *dev = to_vdev(pd->device);
527 struct pvrdma_ah *ah;
528 enum rdma_link_layer ll;
530 if (!(ah_attr->ah_flags & IB_AH_GRH))
531 return ERR_PTR(-EINVAL);
533 ll = rdma_port_get_link_layer(pd->device, ah_attr->port_num);
535 if (ll != IB_LINK_LAYER_ETHERNET ||
536 rdma_is_multicast_addr((struct in6_addr *)ah_attr->grh.dgid.raw))
537 return ERR_PTR(-EINVAL);
539 if (!atomic_add_unless(&dev->num_ahs, 1, dev->dsr->caps.max_ah))
540 return ERR_PTR(-ENOMEM);
542 ah = kzalloc(sizeof(*ah), GFP_KERNEL);
544 atomic_dec(&dev->num_ahs);
545 return ERR_PTR(-ENOMEM);
548 ah->av.port_pd = to_vpd(pd)->pd_handle | (ah_attr->port_num << 24);
549 ah->av.src_path_bits = ah_attr->src_path_bits;
550 ah->av.src_path_bits |= 0x80;
551 ah->av.gid_index = ah_attr->grh.sgid_index;
552 ah->av.hop_limit = ah_attr->grh.hop_limit;
553 ah->av.sl_tclass_flowlabel = (ah_attr->grh.traffic_class << 20) |
554 ah_attr->grh.flow_label;
555 memcpy(ah->av.dgid, ah_attr->grh.dgid.raw, 16);
556 memcpy(ah->av.dmac, ah_attr->dmac, 6);
558 ah->ibah.device = pd->device;
560 ah->ibah.uobject = NULL;
566 * pvrdma_destroy_ah - destroy an address handle
567 * @ah: the address handle to destroyed
569 * @return: 0 on success.
571 int pvrdma_destroy_ah(struct ib_ah *ah)
573 struct pvrdma_dev *dev = to_vdev(ah->device);
576 atomic_dec(&dev->num_ahs);