IB/core: Define 'ib' and 'roce' rdma_ah_attr types
[linux-block.git] / drivers / infiniband / hw / vmw_pvrdma / pvrdma_verbs.c
CommitLineData
29c8d9eb
AR
1/*
2 * Copyright (c) 2012-2016 VMware, Inc. All rights reserved.
3 *
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.
12 *
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.
16 *
17 * The BSD 2-Clause License
18 *
19 * Redistribution and use in source and binary forms, with or
20 * without modification, are permitted provided that the following
21 * conditions are met:
22 *
23 * - Redistributions of source code must retain the above
24 * copyright notice, this list of conditions and the following
25 * disclaimer.
26 *
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.
31 *
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.
44 */
45
46#include <asm/page.h>
47#include <linux/inet.h>
48#include <linux/io.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>
53
54#include "pvrdma.h"
55
56/**
57 * pvrdma_query_device - query device
58 * @ibdev: the device to query
59 * @props: the device properties
60 * @uhw: user data
61 *
62 * @return: 0 on success, otherwise negative errno
63 */
64int pvrdma_query_device(struct ib_device *ibdev,
65 struct ib_device_attr *props,
66 struct ib_udata *uhw)
67{
68 struct pvrdma_dev *dev = to_vdev(ibdev);
69
70 if (uhw->inlen || uhw->outlen)
71 return -EINVAL;
72
73 memset(props, 0, sizeof(*props));
74
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;
92 props->atomic_cap =
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;
104 }
105
106 return 0;
107}
108
109/**
110 * pvrdma_query_port - query device port attributes
111 * @ibdev: the device to query
112 * @port: the port number
113 * @props: the device properties
114 *
115 * @return: 0 on success, otherwise negative errno
116 */
117int pvrdma_query_port(struct ib_device *ibdev, u8 port,
118 struct ib_port_attr *props)
119{
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;
125 int err;
126
127 memset(cmd, 0, sizeof(*cmd));
128 cmd->hdr.cmd = PVRDMA_CMD_QUERY_PORT;
129 cmd->port_num = port;
130
131 err = pvrdma_cmd_post(dev, &req, &rsp, PVRDMA_CMD_QUERY_PORT_RESP);
132 if (err < 0) {
133 dev_warn(&dev->pdev->dev,
134 "could not query port, error: %d\n", err);
135 return err;
136 }
137
c4550c63 138 /* props being zeroed by the caller, avoid zeroing it here */
29c8d9eb
AR
139
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;
160
161 return 0;
162}
163
164/**
165 * pvrdma_query_gid - query device gid
166 * @ibdev: the device to query
167 * @port: the port number
168 * @index: the index
169 * @gid: the device gid value
170 *
171 * @return: 0 on success, otherwise negative errno
172 */
173int pvrdma_query_gid(struct ib_device *ibdev, u8 port, int index,
174 union ib_gid *gid)
175{
176 struct pvrdma_dev *dev = to_vdev(ibdev);
177
178 if (index >= dev->dsr->caps.gid_tbl_len)
179 return -EINVAL;
180
181 memcpy(gid, &dev->sgid_tbl[index], sizeof(union ib_gid));
182
183 return 0;
184}
185
186/**
187 * pvrdma_query_pkey - query device port's P_Key table
188 * @ibdev: the device to query
189 * @port: the port number
190 * @index: the index
191 * @pkey: the device P_Key value
192 *
193 * @return: 0 on success, otherwise negative errno
194 */
195int pvrdma_query_pkey(struct ib_device *ibdev, u8 port, u16 index,
196 u16 *pkey)
197{
198 int err = 0;
199 union pvrdma_cmd_req req;
200 union pvrdma_cmd_resp rsp;
201 struct pvrdma_cmd_query_pkey *cmd = &req.query_pkey;
202
203 memset(cmd, 0, sizeof(*cmd));
204 cmd->hdr.cmd = PVRDMA_CMD_QUERY_PKEY;
205 cmd->port_num = port;
206 cmd->index = index;
207
208 err = pvrdma_cmd_post(to_vdev(ibdev), &req, &rsp,
209 PVRDMA_CMD_QUERY_PKEY_RESP);
210 if (err < 0) {
211 dev_warn(&to_vdev(ibdev)->pdev->dev,
212 "could not query pkey, error: %d\n", err);
213 return err;
214 }
215
216 *pkey = rsp.query_pkey_resp.pkey;
217
218 return 0;
219}
220
221enum rdma_link_layer pvrdma_port_link_layer(struct ib_device *ibdev,
222 u8 port)
223{
224 return IB_LINK_LAYER_ETHERNET;
225}
226
227int pvrdma_modify_device(struct ib_device *ibdev, int mask,
228 struct ib_device_modify *props)
229{
230 unsigned long flags;
231
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);
236 return -EOPNOTSUPP;
237 }
238
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);
243 }
244
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);
250 }
251
252 return 0;
253}
254
255/**
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
261 *
262 * @return: 0 on success, otherwise negative errno
263 */
264int pvrdma_modify_port(struct ib_device *ibdev, u8 port, int mask,
265 struct ib_port_modify *props)
266{
267 struct ib_port_attr attr;
268 struct pvrdma_dev *vdev = to_vdev(ibdev);
269 int ret;
270
271 if (mask & ~IB_PORT_SHUTDOWN) {
272 dev_warn(&vdev->pdev->dev,
273 "unsupported port modify mask %#x\n", mask);
274 return -EOPNOTSUPP;
275 }
276
277 mutex_lock(&vdev->port_mutex);
c4550c63 278 ret = ib_query_port(ibdev, port, &attr);
29c8d9eb
AR
279 if (ret)
280 goto out;
281
282 vdev->port_cap_mask |= props->set_port_cap_mask;
283 vdev->port_cap_mask &= ~props->clr_port_cap_mask;
284
285 if (mask & IB_PORT_SHUTDOWN)
286 vdev->ib_active = false;
287
288out:
289 mutex_unlock(&vdev->port_mutex);
290 return ret;
291}
292
293/**
294 * pvrdma_alloc_ucontext - allocate ucontext
295 * @ibdev: the IB device
296 * @udata: user data
297 *
298 * @return: the ib_ucontext pointer on success, otherwise errno.
299 */
300struct ib_ucontext *pvrdma_alloc_ucontext(struct ib_device *ibdev,
301 struct ib_udata *udata)
302{
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;
7d211c81 309 struct pvrdma_alloc_ucontext_resp uresp = {0};
29c8d9eb
AR
310 int ret;
311 void *ptr;
312
313 if (!vdev->ib_active)
314 return ERR_PTR(-EAGAIN);
315
316 context = kmalloc(sizeof(*context), GFP_KERNEL);
317 if (!context)
318 return ERR_PTR(-ENOMEM);
319
320 context->dev = vdev;
321 ret = pvrdma_uar_alloc(vdev, &context->uar);
322 if (ret) {
323 kfree(context);
324 return ERR_PTR(-ENOMEM);
325 }
326
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);
332 if (ret < 0) {
333 dev_warn(&vdev->pdev->dev,
334 "could not create ucontext, error: %d\n", ret);
335 ptr = ERR_PTR(ret);
336 goto err;
337 }
338
339 context->ctx_handle = resp->ctx_handle;
340
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));
344 if (ret) {
345 pvrdma_uar_free(vdev, &context->uar);
346 context->ibucontext.device = ibdev;
347 pvrdma_dealloc_ucontext(&context->ibucontext);
348 return ERR_PTR(-EFAULT);
349 }
350
351 return &context->ibucontext;
352
353err:
354 pvrdma_uar_free(vdev, &context->uar);
355 kfree(context);
356 return ptr;
357}
358
359/**
360 * pvrdma_dealloc_ucontext - deallocate ucontext
361 * @ibcontext: the ucontext
362 *
363 * @return: 0 on success, otherwise errno.
364 */
365int pvrdma_dealloc_ucontext(struct ib_ucontext *ibcontext)
366{
367 struct pvrdma_ucontext *context = to_vucontext(ibcontext);
368 union pvrdma_cmd_req req;
369 struct pvrdma_cmd_destroy_uc *cmd = &req.destroy_uc;
370 int ret;
371
372 memset(cmd, 0, sizeof(*cmd));
373 cmd->hdr.cmd = PVRDMA_CMD_DESTROY_UC;
374 cmd->ctx_handle = context->ctx_handle;
375
376 ret = pvrdma_cmd_post(context->dev, &req, NULL, 0);
377 if (ret < 0)
378 dev_warn(&context->dev->pdev->dev,
379 "destroy ucontext failed, error: %d\n", ret);
380
381 /* Free the UAR even if the device command failed */
382 pvrdma_uar_free(to_vdev(ibcontext->device), &context->uar);
383 kfree(context);
384
385 return ret;
386}
387
388/**
389 * pvrdma_mmap - create mmap region
390 * @ibcontext: the user context
391 * @vma: the VMA
392 *
393 * @return: 0 on success, otherwise errno.
394 */
395int pvrdma_mmap(struct ib_ucontext *ibcontext, struct vm_area_struct *vma)
396{
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;
401
402 dev_dbg(&context->dev->pdev->dev, "create mmap region\n");
403
404 if ((size != PAGE_SIZE) || (offset & ~PAGE_MASK)) {
405 dev_warn(&context->dev->pdev->dev,
406 "invalid params for mmap region\n");
407 return -EINVAL;
408 }
409
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,
414 vma->vm_page_prot))
415 return -EAGAIN;
416
417 return 0;
418}
419
420/**
421 * pvrdma_alloc_pd - allocate protection domain
422 * @ibdev: the IB device
423 * @context: user context
424 * @udata: user data
425 *
426 * @return: the ib_pd protection domain pointer on success, otherwise errno.
427 */
428struct ib_pd *pvrdma_alloc_pd(struct ib_device *ibdev,
429 struct ib_ucontext *context,
430 struct ib_udata *udata)
431{
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;
438 int ret;
439 void *ptr;
440
441 /* Check allowed max pds */
442 if (!atomic_add_unless(&dev->num_pds, 1, dev->dsr->caps.max_pd))
443 return ERR_PTR(-ENOMEM);
444
445 pd = kmalloc(sizeof(*pd), GFP_KERNEL);
446 if (!pd) {
447 ptr = ERR_PTR(-ENOMEM);
448 goto err;
449 }
450
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);
455 if (ret < 0) {
456 dev_warn(&dev->pdev->dev,
457 "failed to allocate protection domain, error: %d\n",
458 ret);
459 ptr = ERR_PTR(ret);
460 goto freepd;
461 }
462
463 pd->privileged = !context;
464 pd->pd_handle = resp->pd_handle;
465 pd->pdn = resp->pd_handle;
466
467 if (context) {
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);
473 }
474 }
475
476 /* u32 pd handle */
477 return &pd->ibpd;
478
479freepd:
480 kfree(pd);
481err:
482 atomic_dec(&dev->num_pds);
483 return ptr;
484}
485
486/**
487 * pvrdma_dealloc_pd - deallocate protection domain
488 * @pd: the protection domain to be released
489 *
490 * @return: 0 on success, otherwise errno.
491 */
492int pvrdma_dealloc_pd(struct ib_pd *pd)
493{
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;
497 int ret;
498
499 memset(cmd, 0, sizeof(*cmd));
500 cmd->hdr.cmd = PVRDMA_CMD_DESTROY_PD;
501 cmd->pd_handle = to_vpd(pd)->pd_handle;
502
503 ret = pvrdma_cmd_post(dev, &req, NULL, 0);
504 if (ret)
505 dev_warn(&dev->pdev->dev,
506 "could not dealloc protection domain, error: %d\n",
507 ret);
508
509 kfree(to_vpd(pd));
510 atomic_dec(&dev->num_pds);
511
512 return 0;
513}
514
515/**
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
520 *
521 * @return: the ib_ah pointer on success, otherwise errno.
522 */
90898850 523struct ib_ah *pvrdma_create_ah(struct ib_pd *pd, struct rdma_ah_attr *ah_attr,
29c8d9eb
AR
524 struct ib_udata *udata)
525{
526 struct pvrdma_dev *dev = to_vdev(pd->device);
527 struct pvrdma_ah *ah;
d8966fcd
DC
528 const struct ib_global_route *grh;
529 u8 port_num = rdma_ah_get_port_num(ah_attr);
29c8d9eb 530
d8966fcd 531 if (!(rdma_ah_get_ah_flags(ah_attr) & IB_AH_GRH))
29c8d9eb 532 return ERR_PTR(-EINVAL);
29c8d9eb 533
44c58487
DC
534 grh = rdma_ah_read_grh(ah_attr);
535 if ((ah_attr->type != RDMA_AH_ATTR_TYPE_ROCE) ||
d8966fcd 536 rdma_is_multicast_addr((struct in6_addr *)grh->dgid.raw))
29c8d9eb
AR
537 return ERR_PTR(-EINVAL);
538
539 if (!atomic_add_unless(&dev->num_ahs, 1, dev->dsr->caps.max_ah))
540 return ERR_PTR(-ENOMEM);
541
542 ah = kzalloc(sizeof(*ah), GFP_KERNEL);
543 if (!ah) {
544 atomic_dec(&dev->num_ahs);
545 return ERR_PTR(-ENOMEM);
546 }
547
d8966fcd
DC
548 ah->av.port_pd = to_vpd(pd)->pd_handle | (port_num << 24);
549 ah->av.src_path_bits = rdma_ah_get_path_bits(ah_attr);
29c8d9eb 550 ah->av.src_path_bits |= 0x80;
d8966fcd
DC
551 ah->av.gid_index = grh->sgid_index;
552 ah->av.hop_limit = grh->hop_limit;
553 ah->av.sl_tclass_flowlabel = (grh->traffic_class << 20) |
554 grh->flow_label;
555 memcpy(ah->av.dgid, grh->dgid.raw, 16);
44c58487 556 memcpy(ah->av.dmac, ah_attr->roce.dmac, ETH_ALEN);
29c8d9eb
AR
557
558 ah->ibah.device = pd->device;
559 ah->ibah.pd = pd;
560 ah->ibah.uobject = NULL;
561
562 return &ah->ibah;
563}
564
565/**
566 * pvrdma_destroy_ah - destroy an address handle
567 * @ah: the address handle to destroyed
568 *
569 * @return: 0 on success.
570 */
571int pvrdma_destroy_ah(struct ib_ah *ah)
572{
573 struct pvrdma_dev *dev = to_vdev(ah->device);
574
575 kfree(to_vah(ah));
576 atomic_dec(&dev->num_ahs);
577
578 return 0;
579}