1 /*
2  * Copyright (c) 2013, Cisco Systems, Inc. All rights reserved.
3  *
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.
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>
40 
41 #include "usnic_abi.h"
42 #include "usnic_ib.h"
43 #include "usnic_common_util.h"
44 #include "usnic_ib_qp_grp.h"
45 #include "usnic_ib_verbs.h"
46 #include "usnic_fwd.h"
47 #include "usnic_log.h"
48 #include "usnic_uiom.h"
49 #include "usnic_transport.h"
50 #include "usnic_ib_verbs.h"
51 
52 #define USNIC_DEFAULT_TRANSPORT USNIC_TRANSPORT_ROCE_CUSTOM
53 
54 const 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 
78 static void usnic_ib_fw_string_to_u64(char *fw_ver_str, u64 *fw_ver)
79 {
80 	*fw_ver = *((u64 *)fw_ver_str);
81 }
82 
83 static 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;
91 	struct usnic_ib_qp_grp_flow *default_flow;
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;
114 
115 	chunk = usnic_ib_qp_grp_get_chunk(qp_grp, USNIC_VNIC_RES_TYPE_RQ);
116 	if (IS_ERR(chunk)) {
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));
121 		return PTR_ERR(chunk);
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);
130 	if (IS_ERR(chunk)) {
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));
135 		return PTR_ERR(chunk);
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);
144 	if (IS_ERR(chunk)) {
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));
149 		return PTR_ERR(chunk);
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 
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 
161 	err = ib_copy_to_udata(udata, &resp, sizeof(resp));
162 	if (err) {
163 		usnic_err("Failed to copy udata for %s", us_ibdev->ib_dev.name);
164 		return err;
165 	}
166 
167 	return 0;
168 }
169 
170 static struct usnic_ib_qp_grp*
171 find_free_vf_and_create_qp_grp(struct usnic_ib_dev *us_ibdev,
172 				struct usnic_ib_pd *pd,
173 				struct usnic_transport_spec *trans_spec,
174 				struct usnic_vnic_res_spec *res_spec)
175 {
176 	struct usnic_ib_vf *vf;
177 	struct usnic_vnic *vnic;
178 	struct usnic_ib_qp_grp *qp_grp;
179 	struct device *dev, **dev_list;
180 	int i;
181 
182 	BUG_ON(!mutex_is_locked(&us_ibdev->usdev_lock));
183 
184 	if (list_empty(&us_ibdev->vf_dev_list)) {
185 		usnic_info("No vfs to allocate\n");
186 		return NULL;
187 	}
188 
189 	if (usnic_ib_share_vf) {
190 		/* Try to find resouces on a used vf which is in pd */
191 		dev_list = usnic_uiom_get_dev_list(pd->umem_pd);
192 		if (IS_ERR(dev_list))
193 			return ERR_CAST(dev_list);
194 		for (i = 0; dev_list[i]; i++) {
195 			dev = dev_list[i];
196 			vf = pci_get_drvdata(to_pci_dev(dev));
197 			spin_lock(&vf->lock);
198 			vnic = vf->vnic;
199 			if (!usnic_vnic_check_room(vnic, res_spec)) {
200 				usnic_dbg("Found used vnic %s from %s\n",
201 						us_ibdev->ib_dev.name,
202 						pci_name(usnic_vnic_get_pdev(
203 									vnic)));
204 				qp_grp = usnic_ib_qp_grp_create(us_ibdev->ufdev,
205 								vf, pd,
206 								res_spec,
207 								trans_spec);
208 
209 				spin_unlock(&vf->lock);
210 				goto qp_grp_check;
211 			}
212 			spin_unlock(&vf->lock);
213 
214 		}
215 		usnic_uiom_free_dev_list(dev_list);
216 	}
217 
218 	/* Try to find resources on an unused vf */
219 	list_for_each_entry(vf, &us_ibdev->vf_dev_list, link) {
220 		spin_lock(&vf->lock);
221 		vnic = vf->vnic;
222 		if (vf->qp_grp_ref_cnt == 0 &&
223 		    usnic_vnic_check_room(vnic, res_spec) == 0) {
224 			qp_grp = usnic_ib_qp_grp_create(us_ibdev->ufdev, vf,
225 							pd, res_spec,
226 							trans_spec);
227 
228 			spin_unlock(&vf->lock);
229 			goto qp_grp_check;
230 		}
231 		spin_unlock(&vf->lock);
232 	}
233 
234 	usnic_info("No free qp grp found on %s\n", us_ibdev->ib_dev.name);
235 	return ERR_PTR(-ENOMEM);
236 
237 qp_grp_check:
238 	if (IS_ERR_OR_NULL(qp_grp)) {
239 		usnic_err("Failed to allocate qp_grp\n");
240 		return ERR_PTR(qp_grp ? PTR_ERR(qp_grp) : -ENOMEM);
241 	}
242 	return qp_grp;
243 }
244 
245 static void qp_grp_destroy(struct usnic_ib_qp_grp *qp_grp)
246 {
247 	struct usnic_ib_vf *vf = qp_grp->vf;
248 
249 	WARN_ON(qp_grp->state != IB_QPS_RESET);
250 
251 	spin_lock(&vf->lock);
252 	usnic_ib_qp_grp_destroy(qp_grp);
253 	spin_unlock(&vf->lock);
254 }
255 
256 static int create_qp_validate_user_data(struct usnic_ib_create_qp_cmd cmd)
257 {
258 	if (cmd.spec.trans_type <= USNIC_TRANSPORT_UNKNOWN ||
259 			cmd.spec.trans_type >= USNIC_TRANSPORT_MAX)
260 		return -EINVAL;
261 
262 	return 0;
263 }
264 
265 /* Start of ib callback functions */
266 
267 enum rdma_link_layer usnic_ib_port_link_layer(struct ib_device *device,
268 						u8 port_num)
269 {
270 	return IB_LINK_LAYER_ETHERNET;
271 }
272 
273 int usnic_ib_query_device(struct ib_device *ibdev,
274 			  struct ib_device_attr *props,
275 			  struct ib_udata *uhw)
276 {
277 	struct usnic_ib_dev *us_ibdev = to_usdev(ibdev);
278 	union ib_gid gid;
279 	struct ethtool_drvinfo info;
280 	int qp_per_vf;
281 
282 	usnic_dbg("\n");
283 	if (uhw->inlen || uhw->outlen)
284 		return -EINVAL;
285 
286 	mutex_lock(&us_ibdev->usdev_lock);
287 	us_ibdev->netdev->ethtool_ops->get_drvinfo(us_ibdev->netdev, &info);
288 	memset(props, 0, sizeof(*props));
289 	usnic_mac_ip_to_gid(us_ibdev->ufdev->mac, us_ibdev->ufdev->inaddr,
290 			&gid.raw[0]);
291 	memcpy(&props->sys_image_guid, &gid.global.interface_id,
292 		sizeof(gid.global.interface_id));
293 	usnic_ib_fw_string_to_u64(&info.fw_version[0], &props->fw_ver);
294 	props->max_mr_size = USNIC_UIOM_MAX_MR_SIZE;
295 	props->page_size_cap = USNIC_UIOM_PAGE_SIZE;
296 	props->vendor_id = PCI_VENDOR_ID_CISCO;
297 	props->vendor_part_id = PCI_DEVICE_ID_CISCO_VIC_USPACE_NIC;
298 	props->hw_ver = us_ibdev->pdev->subsystem_device;
299 	qp_per_vf = max(us_ibdev->vf_res_cnt[USNIC_VNIC_RES_TYPE_WQ],
300 			us_ibdev->vf_res_cnt[USNIC_VNIC_RES_TYPE_RQ]);
301 	props->max_qp = qp_per_vf *
302 		kref_read(&us_ibdev->vf_cnt);
303 	props->device_cap_flags = IB_DEVICE_PORT_ACTIVE_EVENT |
304 		IB_DEVICE_SYS_IMAGE_GUID | IB_DEVICE_BLOCK_MULTICAST_LOOPBACK;
305 	props->max_cq = us_ibdev->vf_res_cnt[USNIC_VNIC_RES_TYPE_CQ] *
306 		kref_read(&us_ibdev->vf_cnt);
307 	props->max_pd = USNIC_UIOM_MAX_PD_CNT;
308 	props->max_mr = USNIC_UIOM_MAX_MR_CNT;
309 	props->local_ca_ack_delay = 0;
310 	props->max_pkeys = 0;
311 	props->atomic_cap = IB_ATOMIC_NONE;
312 	props->masked_atomic_cap = props->atomic_cap;
313 	props->max_qp_rd_atom = 0;
314 	props->max_qp_init_rd_atom = 0;
315 	props->max_res_rd_atom = 0;
316 	props->max_srq = 0;
317 	props->max_srq_wr = 0;
318 	props->max_srq_sge = 0;
319 	props->max_fast_reg_page_list_len = 0;
320 	props->max_mcast_grp = 0;
321 	props->max_mcast_qp_attach = 0;
322 	props->max_total_mcast_qp_attach = 0;
323 	props->max_map_per_fmr = 0;
324 	/* Owned by Userspace
325 	 * max_qp_wr, max_sge, max_sge_rd, max_cqe */
326 	mutex_unlock(&us_ibdev->usdev_lock);
327 
328 	return 0;
329 }
330 
331 int usnic_ib_query_port(struct ib_device *ibdev, u8 port,
332 				struct ib_port_attr *props)
333 {
334 	struct usnic_ib_dev *us_ibdev = to_usdev(ibdev);
335 
336 	usnic_dbg("\n");
337 
338 	mutex_lock(&us_ibdev->usdev_lock);
339 	if (ib_get_eth_speed(ibdev, port, &props->active_speed,
340 			     &props->active_width)) {
341 		mutex_unlock(&us_ibdev->usdev_lock);
342 		return -EINVAL;
343 	}
344 
345 	/* props being zeroed by the caller, avoid zeroing it here */
346 
347 	props->lid = 0;
348 	props->lmc = 1;
349 	props->sm_lid = 0;
350 	props->sm_sl = 0;
351 
352 	if (!us_ibdev->ufdev->link_up) {
353 		props->state = IB_PORT_DOWN;
354 		props->phys_state = 3;
355 	} else if (!us_ibdev->ufdev->inaddr) {
356 		props->state = IB_PORT_INIT;
357 		props->phys_state = 4;
358 	} else {
359 		props->state = IB_PORT_ACTIVE;
360 		props->phys_state = 5;
361 	}
362 
363 	props->port_cap_flags = 0;
364 	props->gid_tbl_len = 1;
365 	props->pkey_tbl_len = 1;
366 	props->bad_pkey_cntr = 0;
367 	props->qkey_viol_cntr = 0;
368 	props->max_mtu = IB_MTU_4096;
369 	props->active_mtu = iboe_get_mtu(us_ibdev->ufdev->mtu);
370 	/* Userspace will adjust for hdrs */
371 	props->max_msg_sz = us_ibdev->ufdev->mtu;
372 	props->max_vl_num = 1;
373 	mutex_unlock(&us_ibdev->usdev_lock);
374 
375 	return 0;
376 }
377 
378 int usnic_ib_query_qp(struct ib_qp *qp, struct ib_qp_attr *qp_attr,
379 				int qp_attr_mask,
380 				struct ib_qp_init_attr *qp_init_attr)
381 {
382 	struct usnic_ib_qp_grp *qp_grp;
383 	struct usnic_ib_vf *vf;
384 	int err;
385 
386 	usnic_dbg("\n");
387 
388 	memset(qp_attr, 0, sizeof(*qp_attr));
389 	memset(qp_init_attr, 0, sizeof(*qp_init_attr));
390 
391 	qp_grp = to_uqp_grp(qp);
392 	vf = qp_grp->vf;
393 	mutex_lock(&vf->pf->usdev_lock);
394 	usnic_dbg("\n");
395 	qp_attr->qp_state = qp_grp->state;
396 	qp_attr->cur_qp_state = qp_grp->state;
397 
398 	switch (qp_grp->ibqp.qp_type) {
399 	case IB_QPT_UD:
400 		qp_attr->qkey = 0;
401 		break;
402 	default:
403 		usnic_err("Unexpected qp_type %d\n", qp_grp->ibqp.qp_type);
404 		err = -EINVAL;
405 		goto err_out;
406 	}
407 
408 	mutex_unlock(&vf->pf->usdev_lock);
409 	return 0;
410 
411 err_out:
412 	mutex_unlock(&vf->pf->usdev_lock);
413 	return err;
414 }
415 
416 int usnic_ib_query_gid(struct ib_device *ibdev, u8 port, int index,
417 				union ib_gid *gid)
418 {
419 
420 	struct usnic_ib_dev *us_ibdev = to_usdev(ibdev);
421 	usnic_dbg("\n");
422 
423 	if (index > 1)
424 		return -EINVAL;
425 
426 	mutex_lock(&us_ibdev->usdev_lock);
427 	memset(&(gid->raw[0]), 0, sizeof(gid->raw));
428 	usnic_mac_ip_to_gid(us_ibdev->ufdev->mac, us_ibdev->ufdev->inaddr,
429 			&gid->raw[0]);
430 	mutex_unlock(&us_ibdev->usdev_lock);
431 
432 	return 0;
433 }
434 
435 struct net_device *usnic_get_netdev(struct ib_device *device, u8 port_num)
436 {
437 	struct usnic_ib_dev *us_ibdev = to_usdev(device);
438 
439 	if (us_ibdev->netdev)
440 		dev_hold(us_ibdev->netdev);
441 
442 	return us_ibdev->netdev;
443 }
444 
445 int usnic_ib_query_pkey(struct ib_device *ibdev, u8 port, u16 index,
446 				u16 *pkey)
447 {
448 	if (index > 1)
449 		return -EINVAL;
450 
451 	*pkey = 0xffff;
452 	return 0;
453 }
454 
455 struct ib_pd *usnic_ib_alloc_pd(struct ib_device *ibdev,
456 					struct ib_ucontext *context,
457 					struct ib_udata *udata)
458 {
459 	struct usnic_ib_pd *pd;
460 	void *umem_pd;
461 
462 	usnic_dbg("\n");
463 
464 	pd = kzalloc(sizeof(*pd), GFP_KERNEL);
465 	if (!pd)
466 		return ERR_PTR(-ENOMEM);
467 
468 	umem_pd = pd->umem_pd = usnic_uiom_alloc_pd();
469 	if (IS_ERR_OR_NULL(umem_pd)) {
470 		kfree(pd);
471 		return ERR_PTR(umem_pd ? PTR_ERR(umem_pd) : -ENOMEM);
472 	}
473 
474 	usnic_info("domain 0x%p allocated for context 0x%p and device %s\n",
475 			pd, context, ibdev->name);
476 	return &pd->ibpd;
477 }
478 
479 int usnic_ib_dealloc_pd(struct ib_pd *pd)
480 {
481 	usnic_info("freeing domain 0x%p\n", pd);
482 
483 	usnic_uiom_dealloc_pd((to_upd(pd))->umem_pd);
484 	kfree(pd);
485 	return 0;
486 }
487 
488 struct ib_qp *usnic_ib_create_qp(struct ib_pd *pd,
489 					struct ib_qp_init_attr *init_attr,
490 					struct ib_udata *udata)
491 {
492 	int err;
493 	struct usnic_ib_dev *us_ibdev;
494 	struct usnic_ib_qp_grp *qp_grp;
495 	struct usnic_ib_ucontext *ucontext;
496 	int cq_cnt;
497 	struct usnic_vnic_res_spec res_spec;
498 	struct usnic_ib_create_qp_cmd cmd;
499 	struct usnic_transport_spec trans_spec;
500 
501 	usnic_dbg("\n");
502 
503 	ucontext = to_uucontext(pd->uobject->context);
504 	us_ibdev = to_usdev(pd->device);
505 
506 	if (init_attr->create_flags)
507 		return ERR_PTR(-EINVAL);
508 
509 	err = ib_copy_from_udata(&cmd, udata, sizeof(cmd));
510 	if (err) {
511 		usnic_err("%s: cannot copy udata for create_qp\n",
512 				us_ibdev->ib_dev.name);
513 		return ERR_PTR(-EINVAL);
514 	}
515 
516 	err = create_qp_validate_user_data(cmd);
517 	if (err) {
518 		usnic_err("%s: Failed to validate user data\n",
519 				us_ibdev->ib_dev.name);
520 		return ERR_PTR(-EINVAL);
521 	}
522 
523 	if (init_attr->qp_type != IB_QPT_UD) {
524 		usnic_err("%s asked to make a non-UD QP: %d\n",
525 				us_ibdev->ib_dev.name, init_attr->qp_type);
526 		return ERR_PTR(-EINVAL);
527 	}
528 
529 	trans_spec = cmd.spec;
530 	mutex_lock(&us_ibdev->usdev_lock);
531 	cq_cnt = (init_attr->send_cq == init_attr->recv_cq) ? 1 : 2;
532 	res_spec = min_transport_spec[trans_spec.trans_type];
533 	usnic_vnic_res_spec_update(&res_spec, USNIC_VNIC_RES_TYPE_CQ, cq_cnt);
534 	qp_grp = find_free_vf_and_create_qp_grp(us_ibdev, to_upd(pd),
535 						&trans_spec,
536 						&res_spec);
537 	if (IS_ERR_OR_NULL(qp_grp)) {
538 		err = qp_grp ? PTR_ERR(qp_grp) : -ENOMEM;
539 		goto out_release_mutex;
540 	}
541 
542 	err = usnic_ib_fill_create_qp_resp(qp_grp, udata);
543 	if (err) {
544 		err = -EBUSY;
545 		goto out_release_qp_grp;
546 	}
547 
548 	qp_grp->ctx = ucontext;
549 	list_add_tail(&qp_grp->link, &ucontext->qp_grp_list);
550 	usnic_ib_log_vf(qp_grp->vf);
551 	mutex_unlock(&us_ibdev->usdev_lock);
552 	return &qp_grp->ibqp;
553 
554 out_release_qp_grp:
555 	qp_grp_destroy(qp_grp);
556 out_release_mutex:
557 	mutex_unlock(&us_ibdev->usdev_lock);
558 	return ERR_PTR(err);
559 }
560 
561 int usnic_ib_destroy_qp(struct ib_qp *qp)
562 {
563 	struct usnic_ib_qp_grp *qp_grp;
564 	struct usnic_ib_vf *vf;
565 
566 	usnic_dbg("\n");
567 
568 	qp_grp = to_uqp_grp(qp);
569 	vf = qp_grp->vf;
570 	mutex_lock(&vf->pf->usdev_lock);
571 	if (usnic_ib_qp_grp_modify(qp_grp, IB_QPS_RESET, NULL)) {
572 		usnic_err("Failed to move qp grp %u to reset\n",
573 				qp_grp->grp_id);
574 	}
575 
576 	list_del(&qp_grp->link);
577 	qp_grp_destroy(qp_grp);
578 	mutex_unlock(&vf->pf->usdev_lock);
579 
580 	return 0;
581 }
582 
583 int usnic_ib_modify_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
584 				int attr_mask, struct ib_udata *udata)
585 {
586 	struct usnic_ib_qp_grp *qp_grp;
587 	int status;
588 	usnic_dbg("\n");
589 
590 	qp_grp = to_uqp_grp(ibqp);
591 
592 	mutex_lock(&qp_grp->vf->pf->usdev_lock);
593 	if ((attr_mask & IB_QP_PORT) && attr->port_num != 1) {
594 		/* usnic devices only have one port */
595 		status = -EINVAL;
596 		goto out_unlock;
597 	}
598 	if (attr_mask & IB_QP_STATE) {
599 		status = usnic_ib_qp_grp_modify(qp_grp, attr->qp_state, NULL);
600 	} else {
601 		usnic_err("Unhandled request, attr_mask=0x%x\n", attr_mask);
602 		status = -EINVAL;
603 	}
604 
605 out_unlock:
606 	mutex_unlock(&qp_grp->vf->pf->usdev_lock);
607 	return status;
608 }
609 
610 struct ib_cq *usnic_ib_create_cq(struct ib_device *ibdev,
611 				 const struct ib_cq_init_attr *attr,
612 				 struct ib_ucontext *context,
613 				 struct ib_udata *udata)
614 {
615 	struct ib_cq *cq;
616 
617 	usnic_dbg("\n");
618 	if (attr->flags)
619 		return ERR_PTR(-EINVAL);
620 
621 	cq = kzalloc(sizeof(*cq), GFP_KERNEL);
622 	if (!cq)
623 		return ERR_PTR(-EBUSY);
624 
625 	return cq;
626 }
627 
628 int usnic_ib_destroy_cq(struct ib_cq *cq)
629 {
630 	usnic_dbg("\n");
631 	kfree(cq);
632 	return 0;
633 }
634 
635 struct ib_mr *usnic_ib_reg_mr(struct ib_pd *pd, u64 start, u64 length,
636 					u64 virt_addr, int access_flags,
637 					struct ib_udata *udata)
638 {
639 	struct usnic_ib_mr *mr;
640 	int err;
641 
642 	usnic_dbg("start 0x%llx va 0x%llx length 0x%llx\n", start,
643 			virt_addr, length);
644 
645 	mr = kzalloc(sizeof(*mr), GFP_KERNEL);
646 	if (!mr)
647 		return ERR_PTR(-ENOMEM);
648 
649 	mr->umem = usnic_uiom_reg_get(to_upd(pd)->umem_pd, start, length,
650 					access_flags, 0);
651 	if (IS_ERR_OR_NULL(mr->umem)) {
652 		err = mr->umem ? PTR_ERR(mr->umem) : -EFAULT;
653 		goto err_free;
654 	}
655 
656 	mr->ibmr.lkey = mr->ibmr.rkey = 0;
657 	return &mr->ibmr;
658 
659 err_free:
660 	kfree(mr);
661 	return ERR_PTR(err);
662 }
663 
664 int usnic_ib_dereg_mr(struct ib_mr *ibmr)
665 {
666 	struct usnic_ib_mr *mr = to_umr(ibmr);
667 
668 	usnic_dbg("va 0x%lx length 0x%zx\n", mr->umem->va, mr->umem->length);
669 
670 	usnic_uiom_reg_release(mr->umem, ibmr->pd->uobject->context->closing);
671 	kfree(mr);
672 	return 0;
673 }
674 
675 struct ib_ucontext *usnic_ib_alloc_ucontext(struct ib_device *ibdev,
676 							struct ib_udata *udata)
677 {
678 	struct usnic_ib_ucontext *context;
679 	struct usnic_ib_dev *us_ibdev = to_usdev(ibdev);
680 	usnic_dbg("\n");
681 
682 	context = kmalloc(sizeof(*context), GFP_KERNEL);
683 	if (!context)
684 		return ERR_PTR(-ENOMEM);
685 
686 	INIT_LIST_HEAD(&context->qp_grp_list);
687 	mutex_lock(&us_ibdev->usdev_lock);
688 	list_add_tail(&context->link, &us_ibdev->ctx_list);
689 	mutex_unlock(&us_ibdev->usdev_lock);
690 
691 	return &context->ibucontext;
692 }
693 
694 int usnic_ib_dealloc_ucontext(struct ib_ucontext *ibcontext)
695 {
696 	struct usnic_ib_ucontext *context = to_uucontext(ibcontext);
697 	struct usnic_ib_dev *us_ibdev = to_usdev(ibcontext->device);
698 	usnic_dbg("\n");
699 
700 	mutex_lock(&us_ibdev->usdev_lock);
701 	BUG_ON(!list_empty(&context->qp_grp_list));
702 	list_del(&context->link);
703 	mutex_unlock(&us_ibdev->usdev_lock);
704 	kfree(context);
705 	return 0;
706 }
707 
708 int usnic_ib_mmap(struct ib_ucontext *context,
709 				struct vm_area_struct *vma)
710 {
711 	struct usnic_ib_ucontext *uctx = to_ucontext(context);
712 	struct usnic_ib_dev *us_ibdev;
713 	struct usnic_ib_qp_grp *qp_grp;
714 	struct usnic_ib_vf *vf;
715 	struct vnic_dev_bar *bar;
716 	dma_addr_t bus_addr;
717 	unsigned int len;
718 	unsigned int vfid;
719 
720 	usnic_dbg("\n");
721 
722 	us_ibdev = to_usdev(context->device);
723 	vma->vm_flags |= VM_IO;
724 	vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
725 	vfid = vma->vm_pgoff;
726 	usnic_dbg("Page Offset %lu PAGE_SHIFT %u VFID %u\n",
727 			vma->vm_pgoff, PAGE_SHIFT, vfid);
728 
729 	mutex_lock(&us_ibdev->usdev_lock);
730 	list_for_each_entry(qp_grp, &uctx->qp_grp_list, link) {
731 		vf = qp_grp->vf;
732 		if (usnic_vnic_get_index(vf->vnic) == vfid) {
733 			bar = usnic_vnic_get_bar(vf->vnic, 0);
734 			if ((vma->vm_end - vma->vm_start) != bar->len) {
735 				usnic_err("Bar0 Len %lu - Request map %lu\n",
736 						bar->len,
737 						vma->vm_end - vma->vm_start);
738 				mutex_unlock(&us_ibdev->usdev_lock);
739 				return -EINVAL;
740 			}
741 			bus_addr = bar->bus_addr;
742 			len = bar->len;
743 			usnic_dbg("bus: %pa vaddr: %p size: %ld\n",
744 					&bus_addr, bar->vaddr, bar->len);
745 			mutex_unlock(&us_ibdev->usdev_lock);
746 
747 			return remap_pfn_range(vma,
748 						vma->vm_start,
749 						bus_addr >> PAGE_SHIFT,
750 						len, vma->vm_page_prot);
751 		}
752 	}
753 
754 	mutex_unlock(&us_ibdev->usdev_lock);
755 	usnic_err("No VF %u found\n", vfid);
756 	return -EINVAL;
757 }
758 
759 /* In ib callbacks section -  Start of stub funcs */
760 struct ib_ah *usnic_ib_create_ah(struct ib_pd *pd,
761 				 struct rdma_ah_attr *ah_attr,
762 				 struct ib_udata *udata)
763 
764 {
765 	usnic_dbg("\n");
766 	return ERR_PTR(-EPERM);
767 }
768 
769 int usnic_ib_destroy_ah(struct ib_ah *ah)
770 {
771 	usnic_dbg("\n");
772 	return -EINVAL;
773 }
774 
775 int usnic_ib_post_send(struct ib_qp *ibqp, struct ib_send_wr *wr,
776 				struct ib_send_wr **bad_wr)
777 {
778 	usnic_dbg("\n");
779 	return -EINVAL;
780 }
781 
782 int usnic_ib_post_recv(struct ib_qp *ibqp, struct ib_recv_wr *wr,
783 				struct ib_recv_wr **bad_wr)
784 {
785 	usnic_dbg("\n");
786 	return -EINVAL;
787 }
788 
789 int usnic_ib_poll_cq(struct ib_cq *ibcq, int num_entries,
790 				struct ib_wc *wc)
791 {
792 	usnic_dbg("\n");
793 	return -EINVAL;
794 }
795 
796 int usnic_ib_req_notify_cq(struct ib_cq *cq,
797 					enum ib_cq_notify_flags flags)
798 {
799 	usnic_dbg("\n");
800 	return -EINVAL;
801 }
802 
803 struct ib_mr *usnic_ib_get_dma_mr(struct ib_pd *pd, int acc)
804 {
805 	usnic_dbg("\n");
806 	return ERR_PTR(-ENOMEM);
807 }
808 
809 
810 /* In ib callbacks section - End of stub funcs */
811 /* End of ib callbacks section */
812