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