]> git.proxmox.com Git - mirror_ubuntu-kernels.git/blame - drivers/infiniband/hw/usnic/usnic_ib_verbs.c
Merge branches 'for-5.1/upstream-fixes', 'for-5.2/core', 'for-5.2/ish', 'for-5.2...
[mirror_ubuntu-kernels.git] / drivers / infiniband / hw / usnic / usnic_ib_verbs.c
CommitLineData
e3cf00d0
UM
1/*
2 * Copyright (c) 2013, Cisco Systems, Inc. All rights reserved.
3 *
3805eade
JS
4 * This software is available to you under a choice of one of two
5 * licenses. You may choose to be licensed under the terms of the GNU
6 * General Public License (GPL) Version 2, available from the file
7 * COPYING in the main directory of this source tree, or the
8 * BSD license below:
9 *
10 * Redistribution and use in source and binary forms, with or
11 * without modification, are permitted provided that the following
12 * conditions are met:
13 *
14 * - Redistributions of source code must retain the above
15 * copyright notice, this list of conditions and the following
16 * disclaimer.
17 *
18 * - Redistributions in binary form must reproduce the above
19 * copyright notice, this list of conditions and the following
20 * disclaimer in the documentation and/or other materials
21 * provided with the distribution.
e3cf00d0
UM
22 *
23 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30 * SOFTWARE.
31 *
32 */
33#include <linux/module.h>
34#include <linux/init.h>
35#include <linux/slab.h>
36#include <linux/errno.h>
37
38#include <rdma/ib_user_verbs.h>
39#include <rdma/ib_addr.h>
89944450 40#include <rdma/uverbs_ioctl.h>
e3cf00d0
UM
41
42#include "usnic_abi.h"
43#include "usnic_ib.h"
44#include "usnic_common_util.h"
45#include "usnic_ib_qp_grp.h"
19188436 46#include "usnic_ib_verbs.h"
e3cf00d0
UM
47#include "usnic_fwd.h"
48#include "usnic_log.h"
49#include "usnic_uiom.h"
50#include "usnic_transport.h"
51
52#define USNIC_DEFAULT_TRANSPORT USNIC_TRANSPORT_ROCE_CUSTOM
53
bd8c2021
BVA
54const struct usnic_vnic_res_spec min_transport_spec[USNIC_TRANSPORT_MAX] = {
55 { /*USNIC_TRANSPORT_UNKNOWN*/
56 .resources = {
57 {.type = USNIC_VNIC_RES_TYPE_EOL, .cnt = 0,},
58 },
59 },
60 { /*USNIC_TRANSPORT_ROCE_CUSTOM*/
61 .resources = {
62 {.type = USNIC_VNIC_RES_TYPE_WQ, .cnt = 1,},
63 {.type = USNIC_VNIC_RES_TYPE_RQ, .cnt = 1,},
64 {.type = USNIC_VNIC_RES_TYPE_CQ, .cnt = 1,},
65 {.type = USNIC_VNIC_RES_TYPE_EOL, .cnt = 0,},
66 },
67 },
68 { /*USNIC_TRANSPORT_IPV4_UDP*/
69 .resources = {
70 {.type = USNIC_VNIC_RES_TYPE_WQ, .cnt = 1,},
71 {.type = USNIC_VNIC_RES_TYPE_RQ, .cnt = 1,},
72 {.type = USNIC_VNIC_RES_TYPE_CQ, .cnt = 1,},
73 {.type = USNIC_VNIC_RES_TYPE_EOL, .cnt = 0,},
74 },
75 },
76};
77
e3cf00d0
UM
78static void usnic_ib_fw_string_to_u64(char *fw_ver_str, u64 *fw_ver)
79{
3ea72861 80 *fw_ver = *((u64 *)fw_ver_str);
e3cf00d0
UM
81}
82
83static int usnic_ib_fill_create_qp_resp(struct usnic_ib_qp_grp *qp_grp,
84 struct ib_udata *udata)
85{
86 struct usnic_ib_dev *us_ibdev;
87 struct usnic_ib_create_qp_resp resp;
88 struct pci_dev *pdev;
89 struct vnic_dev_bar *bar;
90 struct usnic_vnic_res_chunk *chunk;
8af94ac6 91 struct usnic_ib_qp_grp_flow *default_flow;
e3cf00d0
UM
92 int i, err;
93
94 memset(&resp, 0, sizeof(resp));
95
96 us_ibdev = qp_grp->vf->pf;
97 pdev = usnic_vnic_get_pdev(qp_grp->vf->vnic);
98 if (!pdev) {
99 usnic_err("Failed to get pdev of qp_grp %d\n",
100 qp_grp->grp_id);
101 return -EFAULT;
102 }
103
104 bar = usnic_vnic_get_bar(qp_grp->vf->vnic, 0);
105 if (!bar) {
106 usnic_err("Failed to get bar0 of qp_grp %d vf %s",
107 qp_grp->grp_id, pci_name(pdev));
108 return -EFAULT;
109 }
110
111 resp.vfid = usnic_vnic_get_index(qp_grp->vf->vnic);
112 resp.bar_bus_addr = bar->bus_addr;
113 resp.bar_len = bar->len;
e3cf00d0
UM
114
115 chunk = usnic_ib_qp_grp_get_chunk(qp_grp, USNIC_VNIC_RES_TYPE_RQ);
5f4c7e4e 116 if (IS_ERR(chunk)) {
e3cf00d0
UM
117 usnic_err("Failed to get chunk %s for qp_grp %d with err %ld\n",
118 usnic_vnic_res_type_to_str(USNIC_VNIC_RES_TYPE_RQ),
119 qp_grp->grp_id,
120 PTR_ERR(chunk));
5f4c7e4e 121 return PTR_ERR(chunk);
e3cf00d0
UM
122 }
123
124 WARN_ON(chunk->type != USNIC_VNIC_RES_TYPE_RQ);
125 resp.rq_cnt = chunk->cnt;
126 for (i = 0; i < chunk->cnt; i++)
127 resp.rq_idx[i] = chunk->res[i]->vnic_idx;
128
129 chunk = usnic_ib_qp_grp_get_chunk(qp_grp, USNIC_VNIC_RES_TYPE_WQ);
5f4c7e4e 130 if (IS_ERR(chunk)) {
e3cf00d0
UM
131 usnic_err("Failed to get chunk %s for qp_grp %d with err %ld\n",
132 usnic_vnic_res_type_to_str(USNIC_VNIC_RES_TYPE_WQ),
133 qp_grp->grp_id,
134 PTR_ERR(chunk));
5f4c7e4e 135 return PTR_ERR(chunk);
e3cf00d0
UM
136 }
137
138 WARN_ON(chunk->type != USNIC_VNIC_RES_TYPE_WQ);
139 resp.wq_cnt = chunk->cnt;
140 for (i = 0; i < chunk->cnt; i++)
141 resp.wq_idx[i] = chunk->res[i]->vnic_idx;
142
143 chunk = usnic_ib_qp_grp_get_chunk(qp_grp, USNIC_VNIC_RES_TYPE_CQ);
5f4c7e4e 144 if (IS_ERR(chunk)) {
e3cf00d0
UM
145 usnic_err("Failed to get chunk %s for qp_grp %d with err %ld\n",
146 usnic_vnic_res_type_to_str(USNIC_VNIC_RES_TYPE_CQ),
147 qp_grp->grp_id,
148 PTR_ERR(chunk));
5f4c7e4e 149 return PTR_ERR(chunk);
e3cf00d0
UM
150 }
151
152 WARN_ON(chunk->type != USNIC_VNIC_RES_TYPE_CQ);
153 resp.cq_cnt = chunk->cnt;
154 for (i = 0; i < chunk->cnt; i++)
155 resp.cq_idx[i] = chunk->res[i]->vnic_idx;
156
8af94ac6
UM
157 default_flow = list_first_entry(&qp_grp->flows_lst,
158 struct usnic_ib_qp_grp_flow, link);
159 resp.transport = default_flow->trans_type;
160
e3cf00d0
UM
161 err = ib_copy_to_udata(udata, &resp, sizeof(resp));
162 if (err) {
9de69861
JG
163 usnic_err("Failed to copy udata for %s",
164 dev_name(&us_ibdev->ib_dev.dev));
e3cf00d0
UM
165 return err;
166 }
167
168 return 0;
169}
170
171static struct usnic_ib_qp_grp*
172find_free_vf_and_create_qp_grp(struct usnic_ib_dev *us_ibdev,
173 struct usnic_ib_pd *pd,
8af94ac6 174 struct usnic_transport_spec *trans_spec,
e3cf00d0
UM
175 struct usnic_vnic_res_spec *res_spec)
176{
177 struct usnic_ib_vf *vf;
178 struct usnic_vnic *vnic;
179 struct usnic_ib_qp_grp *qp_grp;
180 struct device *dev, **dev_list;
313e16d5 181 int i;
e3cf00d0
UM
182
183 BUG_ON(!mutex_is_locked(&us_ibdev->usdev_lock));
184
185 if (list_empty(&us_ibdev->vf_dev_list)) {
186 usnic_info("No vfs to allocate\n");
187 return NULL;
188 }
189
e3cf00d0
UM
190 if (usnic_ib_share_vf) {
191 /* Try to find resouces on a used vf which is in pd */
192 dev_list = usnic_uiom_get_dev_list(pd->umem_pd);
d518a44d
DC
193 if (IS_ERR(dev_list))
194 return ERR_CAST(dev_list);
e3cf00d0
UM
195 for (i = 0; dev_list[i]; i++) {
196 dev = dev_list[i];
197 vf = pci_get_drvdata(to_pci_dev(dev));
198 spin_lock(&vf->lock);
199 vnic = vf->vnic;
200 if (!usnic_vnic_check_room(vnic, res_spec)) {
201 usnic_dbg("Found used vnic %s from %s\n",
9de69861 202 dev_name(&us_ibdev->ib_dev.dev),
e3cf00d0
UM
203 pci_name(usnic_vnic_get_pdev(
204 vnic)));
313e16d5
LR
205 qp_grp = usnic_ib_qp_grp_create(us_ibdev->ufdev,
206 vf, pd,
207 res_spec,
208 trans_spec);
209
210 spin_unlock(&vf->lock);
211 goto qp_grp_check;
e3cf00d0
UM
212 }
213 spin_unlock(&vf->lock);
214
215 }
216 usnic_uiom_free_dev_list(dev_list);
217 }
218
313e16d5
LR
219 /* Try to find resources on an unused vf */
220 list_for_each_entry(vf, &us_ibdev->vf_dev_list, link) {
221 spin_lock(&vf->lock);
222 vnic = vf->vnic;
223 if (vf->qp_grp_ref_cnt == 0 &&
224 usnic_vnic_check_room(vnic, res_spec) == 0) {
225 qp_grp = usnic_ib_qp_grp_create(us_ibdev->ufdev, vf,
226 pd, res_spec,
227 trans_spec);
228
e3cf00d0 229 spin_unlock(&vf->lock);
313e16d5 230 goto qp_grp_check;
e3cf00d0 231 }
313e16d5 232 spin_unlock(&vf->lock);
e3cf00d0
UM
233 }
234
9de69861
JG
235 usnic_info("No free qp grp found on %s\n",
236 dev_name(&us_ibdev->ib_dev.dev));
313e16d5 237 return ERR_PTR(-ENOMEM);
e3cf00d0 238
313e16d5 239qp_grp_check:
e3cf00d0
UM
240 if (IS_ERR_OR_NULL(qp_grp)) {
241 usnic_err("Failed to allocate qp_grp\n");
242 return ERR_PTR(qp_grp ? PTR_ERR(qp_grp) : -ENOMEM);
243 }
e3cf00d0
UM
244 return qp_grp;
245}
246
247static void qp_grp_destroy(struct usnic_ib_qp_grp *qp_grp)
248{
249 struct usnic_ib_vf *vf = qp_grp->vf;
250
251 WARN_ON(qp_grp->state != IB_QPS_RESET);
252
253 spin_lock(&vf->lock);
254 usnic_ib_qp_grp_destroy(qp_grp);
255 spin_unlock(&vf->lock);
256}
257
c7845bca
UM
258static int create_qp_validate_user_data(struct usnic_ib_create_qp_cmd cmd)
259{
260 if (cmd.spec.trans_type <= USNIC_TRANSPORT_UNKNOWN ||
261 cmd.spec.trans_type >= USNIC_TRANSPORT_MAX)
262 return -EINVAL;
263
264 return 0;
265}
266
e3cf00d0
UM
267/* Start of ib callback functions */
268
269enum rdma_link_layer usnic_ib_port_link_layer(struct ib_device *device,
270 u8 port_num)
271{
272 return IB_LINK_LAYER_ETHERNET;
273}
274
275int usnic_ib_query_device(struct ib_device *ibdev,
2528e33e
MB
276 struct ib_device_attr *props,
277 struct ib_udata *uhw)
e3cf00d0
UM
278{
279 struct usnic_ib_dev *us_ibdev = to_usdev(ibdev);
280 union ib_gid gid;
281 struct ethtool_drvinfo info;
e3cf00d0
UM
282 int qp_per_vf;
283
284 usnic_dbg("\n");
2528e33e
MB
285 if (uhw->inlen || uhw->outlen)
286 return -EINVAL;
287
e3cf00d0
UM
288 mutex_lock(&us_ibdev->usdev_lock);
289 us_ibdev->netdev->ethtool_ops->get_drvinfo(us_ibdev->netdev, &info);
e3cf00d0 290 memset(props, 0, sizeof(*props));
c7845bca
UM
291 usnic_mac_ip_to_gid(us_ibdev->ufdev->mac, us_ibdev->ufdev->inaddr,
292 &gid.raw[0]);
e3cf00d0
UM
293 memcpy(&props->sys_image_guid, &gid.global.interface_id,
294 sizeof(gid.global.interface_id));
295 usnic_ib_fw_string_to_u64(&info.fw_version[0], &props->fw_ver);
296 props->max_mr_size = USNIC_UIOM_MAX_MR_SIZE;
297 props->page_size_cap = USNIC_UIOM_PAGE_SIZE;
298 props->vendor_id = PCI_VENDOR_ID_CISCO;
299 props->vendor_part_id = PCI_DEVICE_ID_CISCO_VIC_USPACE_NIC;
300 props->hw_ver = us_ibdev->pdev->subsystem_device;
301 qp_per_vf = max(us_ibdev->vf_res_cnt[USNIC_VNIC_RES_TYPE_WQ],
302 us_ibdev->vf_res_cnt[USNIC_VNIC_RES_TYPE_RQ]);
303 props->max_qp = qp_per_vf *
2c935bc5 304 kref_read(&us_ibdev->vf_cnt);
e3cf00d0
UM
305 props->device_cap_flags = IB_DEVICE_PORT_ACTIVE_EVENT |
306 IB_DEVICE_SYS_IMAGE_GUID | IB_DEVICE_BLOCK_MULTICAST_LOOPBACK;
307 props->max_cq = us_ibdev->vf_res_cnt[USNIC_VNIC_RES_TYPE_CQ] *
2c935bc5 308 kref_read(&us_ibdev->vf_cnt);
e3cf00d0
UM
309 props->max_pd = USNIC_UIOM_MAX_PD_CNT;
310 props->max_mr = USNIC_UIOM_MAX_MR_CNT;
311 props->local_ca_ack_delay = 0;
312 props->max_pkeys = 0;
313 props->atomic_cap = IB_ATOMIC_NONE;
314 props->masked_atomic_cap = props->atomic_cap;
315 props->max_qp_rd_atom = 0;
316 props->max_qp_init_rd_atom = 0;
317 props->max_res_rd_atom = 0;
318 props->max_srq = 0;
319 props->max_srq_wr = 0;
320 props->max_srq_sge = 0;
321 props->max_fast_reg_page_list_len = 0;
322 props->max_mcast_grp = 0;
323 props->max_mcast_qp_attach = 0;
324 props->max_total_mcast_qp_attach = 0;
325 props->max_map_per_fmr = 0;
326 /* Owned by Userspace
327 * max_qp_wr, max_sge, max_sge_rd, max_cqe */
328 mutex_unlock(&us_ibdev->usdev_lock);
329
330 return 0;
331}
332
333int usnic_ib_query_port(struct ib_device *ibdev, u8 port,
334 struct ib_port_attr *props)
335{
336 struct usnic_ib_dev *us_ibdev = to_usdev(ibdev);
e3cf00d0
UM
337
338 usnic_dbg("\n");
339
7be05753 340 if (ib_get_eth_speed(ibdev, port, &props->active_speed,
8036e90f 341 &props->active_width))
d4186194 342 return -EINVAL;
d4186194 343
8036e90f
PK
344 /*
345 * usdev_lock is acquired after (and not before) ib_get_eth_speed call
346 * because acquiring rtnl_lock in ib_get_eth_speed, while holding
347 * usdev_lock could lead to a deadlock.
348 */
349 mutex_lock(&us_ibdev->usdev_lock);
c4550c63 350 /* props being zeroed by the caller, avoid zeroing it here */
e3cf00d0
UM
351
352 props->lid = 0;
353 props->lmc = 1;
354 props->sm_lid = 0;
355 props->sm_sl = 0;
356
c7845bca 357 if (!us_ibdev->ufdev->link_up) {
e3cf00d0
UM
358 props->state = IB_PORT_DOWN;
359 props->phys_state = 3;
c7845bca
UM
360 } else if (!us_ibdev->ufdev->inaddr) {
361 props->state = IB_PORT_INIT;
362 props->phys_state = 4;
363 } else {
364 props->state = IB_PORT_ACTIVE;
365 props->phys_state = 5;
e3cf00d0
UM
366 }
367
368 props->port_cap_flags = 0;
369 props->gid_tbl_len = 1;
370 props->pkey_tbl_len = 1;
371 props->bad_pkey_cntr = 0;
372 props->qkey_viol_cntr = 0;
e3cf00d0 373 props->max_mtu = IB_MTU_4096;
8af94ac6 374 props->active_mtu = iboe_get_mtu(us_ibdev->ufdev->mtu);
e3cf00d0 375 /* Userspace will adjust for hdrs */
8af94ac6 376 props->max_msg_sz = us_ibdev->ufdev->mtu;
e3cf00d0
UM
377 props->max_vl_num = 1;
378 mutex_unlock(&us_ibdev->usdev_lock);
379
380 return 0;
381}
382
383int usnic_ib_query_qp(struct ib_qp *qp, struct ib_qp_attr *qp_attr,
384 int qp_attr_mask,
385 struct ib_qp_init_attr *qp_init_attr)
386{
387 struct usnic_ib_qp_grp *qp_grp;
388 struct usnic_ib_vf *vf;
389 int err;
390
391 usnic_dbg("\n");
392
393 memset(qp_attr, 0, sizeof(*qp_attr));
394 memset(qp_init_attr, 0, sizeof(*qp_init_attr));
395
396 qp_grp = to_uqp_grp(qp);
397 vf = qp_grp->vf;
398 mutex_lock(&vf->pf->usdev_lock);
399 usnic_dbg("\n");
400 qp_attr->qp_state = qp_grp->state;
401 qp_attr->cur_qp_state = qp_grp->state;
402
403 switch (qp_grp->ibqp.qp_type) {
404 case IB_QPT_UD:
405 qp_attr->qkey = 0;
406 break;
407 default:
408 usnic_err("Unexpected qp_type %d\n", qp_grp->ibqp.qp_type);
409 err = -EINVAL;
410 goto err_out;
411 }
412
413 mutex_unlock(&vf->pf->usdev_lock);
414 return 0;
415
416err_out:
417 mutex_unlock(&vf->pf->usdev_lock);
418 return err;
419}
420
421int usnic_ib_query_gid(struct ib_device *ibdev, u8 port, int index,
422 union ib_gid *gid)
423{
424
425 struct usnic_ib_dev *us_ibdev = to_usdev(ibdev);
426 usnic_dbg("\n");
427
428 if (index > 1)
429 return -EINVAL;
430
431 mutex_lock(&us_ibdev->usdev_lock);
432 memset(&(gid->raw[0]), 0, sizeof(gid->raw));
c7845bca
UM
433 usnic_mac_ip_to_gid(us_ibdev->ufdev->mac, us_ibdev->ufdev->inaddr,
434 &gid->raw[0]);
e3cf00d0
UM
435 mutex_unlock(&us_ibdev->usdev_lock);
436
437 return 0;
438}
439
440int usnic_ib_query_pkey(struct ib_device *ibdev, u8 port, u16 index,
441 u16 *pkey)
442{
4959d5da 443 if (index > 0)
e3cf00d0
UM
444 return -EINVAL;
445
446 *pkey = 0xffff;
447 return 0;
448}
449
21a428a0
LR
450int usnic_ib_alloc_pd(struct ib_pd *ibpd, struct ib_ucontext *context,
451 struct ib_udata *udata)
e3cf00d0 452{
21a428a0 453 struct usnic_ib_pd *pd = to_upd(ibpd);
e3cf00d0
UM
454 void *umem_pd;
455
e3cf00d0
UM
456 umem_pd = pd->umem_pd = usnic_uiom_alloc_pd();
457 if (IS_ERR_OR_NULL(umem_pd)) {
21a428a0 458 return umem_pd ? PTR_ERR(umem_pd) : -ENOMEM;
e3cf00d0
UM
459 }
460
21a428a0 461 return 0;
e3cf00d0
UM
462}
463
21a428a0 464void usnic_ib_dealloc_pd(struct ib_pd *pd)
e3cf00d0 465{
e3cf00d0 466 usnic_uiom_dealloc_pd((to_upd(pd))->umem_pd);
e3cf00d0
UM
467}
468
469struct ib_qp *usnic_ib_create_qp(struct ib_pd *pd,
470 struct ib_qp_init_attr *init_attr,
471 struct ib_udata *udata)
472{
473 int err;
474 struct usnic_ib_dev *us_ibdev;
475 struct usnic_ib_qp_grp *qp_grp;
89944450
SR
476 struct usnic_ib_ucontext *ucontext = rdma_udata_to_drv_context(
477 udata, struct usnic_ib_ucontext, ibucontext);
e3cf00d0
UM
478 int cq_cnt;
479 struct usnic_vnic_res_spec res_spec;
c7845bca 480 struct usnic_ib_create_qp_cmd cmd;
8af94ac6 481 struct usnic_transport_spec trans_spec;
e3cf00d0
UM
482
483 usnic_dbg("\n");
484
e3cf00d0
UM
485 us_ibdev = to_usdev(pd->device);
486
60093dc0
OG
487 if (init_attr->create_flags)
488 return ERR_PTR(-EINVAL);
489
c7845bca
UM
490 err = ib_copy_from_udata(&cmd, udata, sizeof(cmd));
491 if (err) {
492 usnic_err("%s: cannot copy udata for create_qp\n",
9de69861 493 dev_name(&us_ibdev->ib_dev.dev));
c7845bca
UM
494 return ERR_PTR(-EINVAL);
495 }
496
497 err = create_qp_validate_user_data(cmd);
498 if (err) {
499 usnic_err("%s: Failed to validate user data\n",
9de69861 500 dev_name(&us_ibdev->ib_dev.dev));
c7845bca
UM
501 return ERR_PTR(-EINVAL);
502 }
503
e3cf00d0
UM
504 if (init_attr->qp_type != IB_QPT_UD) {
505 usnic_err("%s asked to make a non-UD QP: %d\n",
9de69861 506 dev_name(&us_ibdev->ib_dev.dev), init_attr->qp_type);
e3cf00d0
UM
507 return ERR_PTR(-EINVAL);
508 }
509
c7845bca 510 trans_spec = cmd.spec;
e3cf00d0 511 mutex_lock(&us_ibdev->usdev_lock);
8af94ac6
UM
512 cq_cnt = (init_attr->send_cq == init_attr->recv_cq) ? 1 : 2;
513 res_spec = min_transport_spec[trans_spec.trans_type];
e3cf00d0
UM
514 usnic_vnic_res_spec_update(&res_spec, USNIC_VNIC_RES_TYPE_CQ, cq_cnt);
515 qp_grp = find_free_vf_and_create_qp_grp(us_ibdev, to_upd(pd),
8af94ac6 516 &trans_spec,
e3cf00d0
UM
517 &res_spec);
518 if (IS_ERR_OR_NULL(qp_grp)) {
6a54d9f9 519 err = qp_grp ? PTR_ERR(qp_grp) : -ENOMEM;
e3cf00d0
UM
520 goto out_release_mutex;
521 }
522
523 err = usnic_ib_fill_create_qp_resp(qp_grp, udata);
524 if (err) {
525 err = -EBUSY;
526 goto out_release_qp_grp;
527 }
528
529 qp_grp->ctx = ucontext;
530 list_add_tail(&qp_grp->link, &ucontext->qp_grp_list);
531 usnic_ib_log_vf(qp_grp->vf);
532 mutex_unlock(&us_ibdev->usdev_lock);
533 return &qp_grp->ibqp;
534
535out_release_qp_grp:
536 qp_grp_destroy(qp_grp);
537out_release_mutex:
538 mutex_unlock(&us_ibdev->usdev_lock);
539 return ERR_PTR(err);
540}
541
542int usnic_ib_destroy_qp(struct ib_qp *qp)
543{
544 struct usnic_ib_qp_grp *qp_grp;
545 struct usnic_ib_vf *vf;
546
547 usnic_dbg("\n");
548
549 qp_grp = to_uqp_grp(qp);
550 vf = qp_grp->vf;
551 mutex_lock(&vf->pf->usdev_lock);
552 if (usnic_ib_qp_grp_modify(qp_grp, IB_QPS_RESET, NULL)) {
553 usnic_err("Failed to move qp grp %u to reset\n",
554 qp_grp->grp_id);
555 }
556
557 list_del(&qp_grp->link);
558 qp_grp_destroy(qp_grp);
559 mutex_unlock(&vf->pf->usdev_lock);
560
561 return 0;
562}
563
564int usnic_ib_modify_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
565 int attr_mask, struct ib_udata *udata)
566{
567 struct usnic_ib_qp_grp *qp_grp;
568 int status;
569 usnic_dbg("\n");
570
571 qp_grp = to_uqp_grp(ibqp);
572
e3cf00d0 573 mutex_lock(&qp_grp->vf->pf->usdev_lock);
89e5323c
NE
574 if ((attr_mask & IB_QP_PORT) && attr->port_num != 1) {
575 /* usnic devices only have one port */
576 status = -EINVAL;
577 goto out_unlock;
578 }
579 if (attr_mask & IB_QP_STATE) {
580 status = usnic_ib_qp_grp_modify(qp_grp, attr->qp_state, NULL);
e3cf00d0 581 } else {
89e5323c 582 usnic_err("Unhandled request, attr_mask=0x%x\n", attr_mask);
e3cf00d0
UM
583 status = -EINVAL;
584 }
585
89e5323c 586out_unlock:
e3cf00d0
UM
587 mutex_unlock(&qp_grp->vf->pf->usdev_lock);
588 return status;
589}
590
bcf4c1ea
MB
591struct ib_cq *usnic_ib_create_cq(struct ib_device *ibdev,
592 const struct ib_cq_init_attr *attr,
593 struct ib_ucontext *context,
594 struct ib_udata *udata)
e3cf00d0
UM
595{
596 struct ib_cq *cq;
597
598 usnic_dbg("\n");
bcf4c1ea
MB
599 if (attr->flags)
600 return ERR_PTR(-EINVAL);
601
e3cf00d0
UM
602 cq = kzalloc(sizeof(*cq), GFP_KERNEL);
603 if (!cq)
604 return ERR_PTR(-EBUSY);
605
606 return cq;
607}
608
609int usnic_ib_destroy_cq(struct ib_cq *cq)
610{
611 usnic_dbg("\n");
612 kfree(cq);
613 return 0;
614}
615
616struct ib_mr *usnic_ib_reg_mr(struct ib_pd *pd, u64 start, u64 length,
617 u64 virt_addr, int access_flags,
618 struct ib_udata *udata)
619{
620 struct usnic_ib_mr *mr;
621 int err;
622
623 usnic_dbg("start 0x%llx va 0x%llx length 0x%llx\n", start,
624 virt_addr, length);
625
626 mr = kzalloc(sizeof(*mr), GFP_KERNEL);
59f65f49
JL
627 if (!mr)
628 return ERR_PTR(-ENOMEM);
e3cf00d0
UM
629
630 mr->umem = usnic_uiom_reg_get(to_upd(pd)->umem_pd, start, length,
631 access_flags, 0);
632 if (IS_ERR_OR_NULL(mr->umem)) {
6a54d9f9 633 err = mr->umem ? PTR_ERR(mr->umem) : -EFAULT;
e3cf00d0
UM
634 goto err_free;
635 }
636
637 mr->ibmr.lkey = mr->ibmr.rkey = 0;
638 return &mr->ibmr;
639
640err_free:
641 kfree(mr);
642 return ERR_PTR(err);
643}
644
645int usnic_ib_dereg_mr(struct ib_mr *ibmr)
646{
647 struct usnic_ib_mr *mr = to_umr(ibmr);
648
649 usnic_dbg("va 0x%lx length 0x%zx\n", mr->umem->va, mr->umem->length);
650
43cbd64b 651 usnic_uiom_reg_release(mr->umem, ibmr->uobject->context);
e3cf00d0
UM
652 kfree(mr);
653 return 0;
654}
655
a2a074ef 656int usnic_ib_alloc_ucontext(struct ib_ucontext *uctx, struct ib_udata *udata)
e3cf00d0 657{
a2a074ef
LR
658 struct ib_device *ibdev = uctx->device;
659 struct usnic_ib_ucontext *context = to_ucontext(uctx);
e3cf00d0
UM
660 struct usnic_ib_dev *us_ibdev = to_usdev(ibdev);
661 usnic_dbg("\n");
662
e3cf00d0
UM
663 INIT_LIST_HEAD(&context->qp_grp_list);
664 mutex_lock(&us_ibdev->usdev_lock);
665 list_add_tail(&context->link, &us_ibdev->ctx_list);
666 mutex_unlock(&us_ibdev->usdev_lock);
667
a2a074ef 668 return 0;
e3cf00d0
UM
669}
670
a2a074ef 671void usnic_ib_dealloc_ucontext(struct ib_ucontext *ibcontext)
e3cf00d0
UM
672{
673 struct usnic_ib_ucontext *context = to_uucontext(ibcontext);
674 struct usnic_ib_dev *us_ibdev = to_usdev(ibcontext->device);
675 usnic_dbg("\n");
676
677 mutex_lock(&us_ibdev->usdev_lock);
a2a074ef 678 WARN_ON_ONCE(!list_empty(&context->qp_grp_list));
e3cf00d0
UM
679 list_del(&context->link);
680 mutex_unlock(&us_ibdev->usdev_lock);
e3cf00d0
UM
681}
682
683int usnic_ib_mmap(struct ib_ucontext *context,
684 struct vm_area_struct *vma)
685{
686 struct usnic_ib_ucontext *uctx = to_ucontext(context);
687 struct usnic_ib_dev *us_ibdev;
688 struct usnic_ib_qp_grp *qp_grp;
689 struct usnic_ib_vf *vf;
690 struct vnic_dev_bar *bar;
691 dma_addr_t bus_addr;
692 unsigned int len;
693 unsigned int vfid;
694
695 usnic_dbg("\n");
696
697 us_ibdev = to_usdev(context->device);
698 vma->vm_flags |= VM_IO;
699 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
700 vfid = vma->vm_pgoff;
701 usnic_dbg("Page Offset %lu PAGE_SHIFT %u VFID %u\n",
702 vma->vm_pgoff, PAGE_SHIFT, vfid);
703
704 mutex_lock(&us_ibdev->usdev_lock);
705 list_for_each_entry(qp_grp, &uctx->qp_grp_list, link) {
706 vf = qp_grp->vf;
707 if (usnic_vnic_get_index(vf->vnic) == vfid) {
708 bar = usnic_vnic_get_bar(vf->vnic, 0);
709 if ((vma->vm_end - vma->vm_start) != bar->len) {
710 usnic_err("Bar0 Len %lu - Request map %lu\n",
711 bar->len,
712 vma->vm_end - vma->vm_start);
713 mutex_unlock(&us_ibdev->usdev_lock);
714 return -EINVAL;
715 }
716 bus_addr = bar->bus_addr;
717 len = bar->len;
718 usnic_dbg("bus: %pa vaddr: %p size: %ld\n",
719 &bus_addr, bar->vaddr, bar->len);
720 mutex_unlock(&us_ibdev->usdev_lock);
721
722 return remap_pfn_range(vma,
723 vma->vm_start,
724 bus_addr >> PAGE_SHIFT,
725 len, vma->vm_page_prot);
726 }
727 }
728
729 mutex_unlock(&us_ibdev->usdev_lock);
730 usnic_err("No VF %u found\n", vfid);
731 return -EINVAL;
732}
733
e3cf00d0 734/* End of ib callbacks section */