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