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  * Author: Upinder Malhi <[email protected]>
33  * Author: Anant Deepak <[email protected]>
34  * Author: Cesare Cantu' <[email protected]>
35  * Author: Jeff Squyres <[email protected]>
36  * Author: Kiran Thirumalai <[email protected]>
37  * Author: Xuyang Wang <[email protected]>
38  * Author: Reese Faucette <[email protected]>
39  *
40  */
41 
42 #include <linux/module.h>
43 #include <linux/inetdevice.h>
44 #include <linux/init.h>
45 #include <linux/slab.h>
46 #include <linux/errno.h>
47 #include <linux/pci.h>
48 #include <linux/netdevice.h>
49 
50 #include <rdma/ib_user_verbs.h>
51 #include <rdma/ib_addr.h>
52 
53 #include "usnic_abi.h"
54 #include "usnic_common_util.h"
55 #include "usnic_ib.h"
56 #include "usnic_ib_qp_grp.h"
57 #include "usnic_log.h"
58 #include "usnic_fwd.h"
59 #include "usnic_debugfs.h"
60 #include "usnic_ib_verbs.h"
61 #include "usnic_transport.h"
62 #include "usnic_uiom.h"
63 #include "usnic_ib_sysfs.h"
64 
65 unsigned int usnic_log_lvl = USNIC_LOG_LVL_ERR;
66 unsigned int usnic_ib_share_vf = 1;
67 
68 static const char usnic_version[] =
69 	DRV_NAME ": Cisco VIC (USNIC) Verbs Driver v"
70 	DRV_VERSION " (" DRV_RELDATE ")\n";
71 
72 static DEFINE_MUTEX(usnic_ib_ibdev_list_lock);
73 static LIST_HEAD(usnic_ib_ibdev_list);
74 
75 /* Callback dump funcs */
usnic_ib_dump_vf_hdr(void * obj,char * buf,int buf_sz)76 static int usnic_ib_dump_vf_hdr(void *obj, char *buf, int buf_sz)
77 {
78 	struct usnic_ib_vf *vf = obj;
79 	return scnprintf(buf, buf_sz, "PF: %s ", dev_name(&vf->pf->ib_dev.dev));
80 }
81 /* End callback dump funcs */
82 
usnic_ib_dump_vf(struct usnic_ib_vf * vf,char * buf,int buf_sz)83 static void usnic_ib_dump_vf(struct usnic_ib_vf *vf, char *buf, int buf_sz)
84 {
85 	usnic_vnic_dump(vf->vnic, buf, buf_sz, vf,
86 			usnic_ib_dump_vf_hdr,
87 			usnic_ib_qp_grp_dump_hdr, usnic_ib_qp_grp_dump_rows);
88 }
89 
usnic_ib_log_vf(struct usnic_ib_vf * vf)90 void usnic_ib_log_vf(struct usnic_ib_vf *vf)
91 {
92 	char *buf = kzalloc(1000, GFP_KERNEL);
93 
94 	if (!buf)
95 		return;
96 
97 	usnic_ib_dump_vf(vf, buf, 1000);
98 	usnic_dbg("%s\n", buf);
99 
100 	kfree(buf);
101 }
102 
103 /* Start of netdev section */
usnic_ib_qp_grp_modify_active_to_err(struct usnic_ib_dev * us_ibdev)104 static void usnic_ib_qp_grp_modify_active_to_err(struct usnic_ib_dev *us_ibdev)
105 {
106 	struct usnic_ib_ucontext *ctx;
107 	struct usnic_ib_qp_grp *qp_grp;
108 	enum ib_qp_state cur_state;
109 	int status;
110 
111 	BUG_ON(!mutex_is_locked(&us_ibdev->usdev_lock));
112 
113 	list_for_each_entry(ctx, &us_ibdev->ctx_list, link) {
114 		list_for_each_entry(qp_grp, &ctx->qp_grp_list, link) {
115 			cur_state = qp_grp->state;
116 			if (cur_state == IB_QPS_INIT ||
117 				cur_state == IB_QPS_RTR ||
118 				cur_state == IB_QPS_RTS) {
119 				status = usnic_ib_qp_grp_modify(qp_grp,
120 								IB_QPS_ERR,
121 								NULL);
122 				if (status) {
123 					usnic_err("Failed to transition qp grp %u from %s to %s\n",
124 						qp_grp->grp_id,
125 						usnic_ib_qp_grp_state_to_string
126 						(cur_state),
127 						usnic_ib_qp_grp_state_to_string
128 						(IB_QPS_ERR));
129 				}
130 			}
131 		}
132 	}
133 }
134 
usnic_ib_handle_usdev_event(struct usnic_ib_dev * us_ibdev,unsigned long event)135 static void usnic_ib_handle_usdev_event(struct usnic_ib_dev *us_ibdev,
136 					unsigned long event)
137 {
138 	struct net_device *netdev;
139 	struct ib_event ib_event;
140 
141 	memset(&ib_event, 0, sizeof(ib_event));
142 
143 	mutex_lock(&us_ibdev->usdev_lock);
144 	netdev = us_ibdev->netdev;
145 	switch (event) {
146 	case NETDEV_REBOOT:
147 		usnic_info("PF Reset on %s\n", dev_name(&us_ibdev->ib_dev.dev));
148 		usnic_ib_qp_grp_modify_active_to_err(us_ibdev);
149 		ib_event.event = IB_EVENT_PORT_ERR;
150 		ib_event.device = &us_ibdev->ib_dev;
151 		ib_event.element.port_num = 1;
152 		ib_dispatch_event(&ib_event);
153 		break;
154 	case NETDEV_CHANGEADDR:
155 		if (!memcmp(us_ibdev->ufdev->mac, netdev->dev_addr,
156 				sizeof(us_ibdev->ufdev->mac))) {
157 			usnic_dbg("Ignoring addr change on %s\n",
158 				  dev_name(&us_ibdev->ib_dev.dev));
159 		} else {
160 			usnic_info(" %s old mac: %pM new mac: %pM\n",
161 					dev_name(&us_ibdev->ib_dev.dev),
162 					us_ibdev->ufdev->mac,
163 					netdev->dev_addr);
164 			usnic_fwd_set_mac(us_ibdev->ufdev, netdev->dev_addr);
165 			usnic_ib_qp_grp_modify_active_to_err(us_ibdev);
166 			ib_event.event = IB_EVENT_GID_CHANGE;
167 			ib_event.device = &us_ibdev->ib_dev;
168 			ib_event.element.port_num = 1;
169 			ib_dispatch_event(&ib_event);
170 		}
171 
172 		break;
173 	case NETDEV_CHANGEMTU:
174 		if (us_ibdev->ufdev->mtu != netdev->mtu) {
175 			usnic_info("MTU Change on %s old: %u new: %u\n",
176 					dev_name(&us_ibdev->ib_dev.dev),
177 					us_ibdev->ufdev->mtu, netdev->mtu);
178 			usnic_fwd_set_mtu(us_ibdev->ufdev, netdev->mtu);
179 			usnic_ib_qp_grp_modify_active_to_err(us_ibdev);
180 		} else {
181 			usnic_dbg("Ignoring MTU change on %s\n",
182 				  dev_name(&us_ibdev->ib_dev.dev));
183 		}
184 		break;
185 	default:
186 		usnic_dbg("Ignoring event %s on %s",
187 				netdev_cmd_to_name(event),
188 				dev_name(&us_ibdev->ib_dev.dev));
189 	}
190 	mutex_unlock(&us_ibdev->usdev_lock);
191 }
192 
usnic_ib_handle_port_event(struct ib_device * ibdev,struct net_device * netdev,unsigned long event)193 static void usnic_ib_handle_port_event(struct ib_device *ibdev,
194 				       struct net_device *netdev,
195 				       unsigned long event)
196 {
197 	struct usnic_ib_dev *us_ibdev =
198 			container_of(ibdev, struct usnic_ib_dev, ib_dev);
199 	struct ib_event ib_event;
200 
201 	mutex_lock(&us_ibdev->usdev_lock);
202 	switch (event) {
203 	case NETDEV_UP:
204 	case NETDEV_DOWN:
205 	case NETDEV_CHANGE:
206 		if (!us_ibdev->ufdev->link_up &&
207 		    netif_carrier_ok(netdev)) {
208 			usnic_fwd_carrier_up(us_ibdev->ufdev);
209 			usnic_info("Link UP on %s\n",
210 				   dev_name(&us_ibdev->ib_dev.dev));
211 			ib_event.event = IB_EVENT_PORT_ACTIVE;
212 			ib_event.device = &us_ibdev->ib_dev;
213 			ib_event.element.port_num = 1;
214 			ib_dispatch_event(&ib_event);
215 		} else if (us_ibdev->ufdev->link_up &&
216 			   !netif_carrier_ok(netdev)) {
217 			usnic_fwd_carrier_down(us_ibdev->ufdev);
218 			usnic_info("Link DOWN on %s\n",
219 				   dev_name(&us_ibdev->ib_dev.dev));
220 			usnic_ib_qp_grp_modify_active_to_err(us_ibdev);
221 			ib_event.event = IB_EVENT_PORT_ERR;
222 			ib_event.device = &us_ibdev->ib_dev;
223 			ib_event.element.port_num = 1;
224 			ib_dispatch_event(&ib_event);
225 		} else {
226 			usnic_dbg("Ignoring %s on %s\n",
227 				  netdev_cmd_to_name(event),
228 				  dev_name(&us_ibdev->ib_dev.dev));
229 		}
230 		break;
231 	default:
232 		break;
233 	}
234 	mutex_unlock(&us_ibdev->usdev_lock);
235 }
236 
usnic_ib_netdevice_event(struct notifier_block * notifier,unsigned long event,void * ptr)237 static int usnic_ib_netdevice_event(struct notifier_block *notifier,
238 					unsigned long event, void *ptr)
239 {
240 	struct usnic_ib_dev *us_ibdev;
241 	struct ib_device *ibdev;
242 
243 	struct net_device *netdev = netdev_notifier_info_to_dev(ptr);
244 
245 	ibdev = ib_device_get_by_netdev(netdev, RDMA_DRIVER_USNIC);
246 	if (!ibdev)
247 		return NOTIFY_DONE;
248 
249 	us_ibdev = container_of(ibdev, struct usnic_ib_dev, ib_dev);
250 	usnic_ib_handle_usdev_event(us_ibdev, event);
251 	ib_device_put(ibdev);
252 	return NOTIFY_DONE;
253 }
254 
255 static struct notifier_block usnic_ib_netdevice_notifier = {
256 	.notifier_call = usnic_ib_netdevice_event
257 };
258 /* End of netdev section */
259 
260 /* Start of inet section */
usnic_ib_handle_inet_event(struct usnic_ib_dev * us_ibdev,unsigned long event,void * ptr)261 static int usnic_ib_handle_inet_event(struct usnic_ib_dev *us_ibdev,
262 					unsigned long event, void *ptr)
263 {
264 	struct in_ifaddr *ifa = ptr;
265 	struct ib_event ib_event;
266 
267 	mutex_lock(&us_ibdev->usdev_lock);
268 
269 	switch (event) {
270 	case NETDEV_DOWN:
271 		usnic_info("%s via ip notifiers",
272 				netdev_cmd_to_name(event));
273 		usnic_fwd_del_ipaddr(us_ibdev->ufdev);
274 		usnic_ib_qp_grp_modify_active_to_err(us_ibdev);
275 		ib_event.event = IB_EVENT_GID_CHANGE;
276 		ib_event.device = &us_ibdev->ib_dev;
277 		ib_event.element.port_num = 1;
278 		ib_dispatch_event(&ib_event);
279 		break;
280 	case NETDEV_UP:
281 		usnic_fwd_add_ipaddr(us_ibdev->ufdev, ifa->ifa_address);
282 		usnic_info("%s via ip notifiers: ip %pI4",
283 				netdev_cmd_to_name(event),
284 				&us_ibdev->ufdev->inaddr);
285 		ib_event.event = IB_EVENT_GID_CHANGE;
286 		ib_event.device = &us_ibdev->ib_dev;
287 		ib_event.element.port_num = 1;
288 		ib_dispatch_event(&ib_event);
289 		break;
290 	default:
291 		usnic_info("Ignoring event %s on %s",
292 				netdev_cmd_to_name(event),
293 				dev_name(&us_ibdev->ib_dev.dev));
294 	}
295 	mutex_unlock(&us_ibdev->usdev_lock);
296 
297 	return NOTIFY_DONE;
298 }
299 
usnic_ib_inetaddr_event(struct notifier_block * notifier,unsigned long event,void * ptr)300 static int usnic_ib_inetaddr_event(struct notifier_block *notifier,
301 					unsigned long event, void *ptr)
302 {
303 	struct usnic_ib_dev *us_ibdev;
304 	struct in_ifaddr *ifa = ptr;
305 	struct net_device *netdev = ifa->ifa_dev->dev;
306 	struct ib_device *ibdev;
307 
308 	ibdev = ib_device_get_by_netdev(netdev, RDMA_DRIVER_USNIC);
309 	if (!ibdev)
310 		return NOTIFY_DONE;
311 
312 	us_ibdev = container_of(ibdev, struct usnic_ib_dev, ib_dev);
313 	usnic_ib_handle_inet_event(us_ibdev, event, ptr);
314 	ib_device_put(ibdev);
315 	return NOTIFY_DONE;
316 }
317 static struct notifier_block usnic_ib_inetaddr_notifier = {
318 	.notifier_call = usnic_ib_inetaddr_event
319 };
320 /* End of inet section*/
321 
usnic_port_immutable(struct ib_device * ibdev,u32 port_num,struct ib_port_immutable * immutable)322 static int usnic_port_immutable(struct ib_device *ibdev, u32 port_num,
323 			        struct ib_port_immutable *immutable)
324 {
325 	struct ib_port_attr attr;
326 	int err;
327 
328 	immutable->core_cap_flags = RDMA_CORE_PORT_USNIC;
329 
330 	err = ib_query_port(ibdev, port_num, &attr);
331 	if (err)
332 		return err;
333 
334 	immutable->gid_tbl_len = attr.gid_tbl_len;
335 
336 	return 0;
337 }
338 
usnic_get_dev_fw_str(struct ib_device * device,char * str)339 static void usnic_get_dev_fw_str(struct ib_device *device, char *str)
340 {
341 	struct usnic_ib_dev *us_ibdev =
342 		container_of(device, struct usnic_ib_dev, ib_dev);
343 	struct ethtool_drvinfo info;
344 
345 	mutex_lock(&us_ibdev->usdev_lock);
346 	us_ibdev->netdev->ethtool_ops->get_drvinfo(us_ibdev->netdev, &info);
347 	mutex_unlock(&us_ibdev->usdev_lock);
348 
349 	snprintf(str, IB_FW_VERSION_NAME_MAX, "%s", info.fw_version);
350 }
351 
352 static const struct ib_device_ops usnic_dev_ops = {
353 	.owner = THIS_MODULE,
354 	.driver_id = RDMA_DRIVER_USNIC,
355 	.uverbs_abi_ver = USNIC_UVERBS_ABI_VERSION,
356 
357 	.alloc_pd = usnic_ib_alloc_pd,
358 	.alloc_ucontext = usnic_ib_alloc_ucontext,
359 	.create_cq = usnic_ib_create_cq,
360 	.create_qp = usnic_ib_create_qp,
361 	.dealloc_pd = usnic_ib_dealloc_pd,
362 	.dealloc_ucontext = usnic_ib_dealloc_ucontext,
363 	.dereg_mr = usnic_ib_dereg_mr,
364 	.destroy_cq = usnic_ib_destroy_cq,
365 	.destroy_qp = usnic_ib_destroy_qp,
366 	.device_group = &usnic_attr_group,
367 	.get_dev_fw_str = usnic_get_dev_fw_str,
368 	.get_link_layer = usnic_ib_port_link_layer,
369 	.get_port_immutable = usnic_port_immutable,
370 	.mmap = usnic_ib_mmap,
371 	.modify_qp = usnic_ib_modify_qp,
372 	.query_device = usnic_ib_query_device,
373 	.query_gid = usnic_ib_query_gid,
374 	.query_port = usnic_ib_query_port,
375 	.query_qp = usnic_ib_query_qp,
376 	.reg_user_mr = usnic_ib_reg_mr,
377 	.report_port_event = usnic_ib_handle_port_event,
378 	INIT_RDMA_OBJ_SIZE(ib_pd, usnic_ib_pd, ibpd),
379 	INIT_RDMA_OBJ_SIZE(ib_cq, usnic_ib_cq, ibcq),
380 	INIT_RDMA_OBJ_SIZE(ib_qp, usnic_ib_qp_grp, ibqp),
381 	INIT_RDMA_OBJ_SIZE(ib_ucontext, usnic_ib_ucontext, ibucontext),
382 };
383 
384 /* Start of PF discovery section */
usnic_ib_device_add(struct pci_dev * dev)385 static void *usnic_ib_device_add(struct pci_dev *dev)
386 {
387 	struct usnic_ib_dev *us_ibdev;
388 	union ib_gid gid;
389 	struct in_device *ind;
390 	struct net_device *netdev;
391 	int ret;
392 
393 	usnic_dbg("\n");
394 	netdev = pci_get_drvdata(dev);
395 
396 	us_ibdev = ib_alloc_device(usnic_ib_dev, ib_dev);
397 	if (!us_ibdev) {
398 		usnic_err("Device %s context alloc failed\n",
399 				netdev_name(pci_get_drvdata(dev)));
400 		return NULL;
401 	}
402 
403 	us_ibdev->ufdev = usnic_fwd_dev_alloc(dev);
404 	if (!us_ibdev->ufdev) {
405 		usnic_err("Failed to alloc ufdev for %s\n", pci_name(dev));
406 		goto err_dealloc;
407 	}
408 
409 	mutex_init(&us_ibdev->usdev_lock);
410 	INIT_LIST_HEAD(&us_ibdev->vf_dev_list);
411 	INIT_LIST_HEAD(&us_ibdev->ctx_list);
412 
413 	us_ibdev->pdev = dev;
414 	us_ibdev->netdev = pci_get_drvdata(dev);
415 	us_ibdev->ib_dev.node_type = RDMA_NODE_USNIC_UDP;
416 	us_ibdev->ib_dev.phys_port_cnt = USNIC_IB_PORT_CNT;
417 	us_ibdev->ib_dev.num_comp_vectors = USNIC_IB_NUM_COMP_VECTORS;
418 	us_ibdev->ib_dev.dev.parent = &dev->dev;
419 
420 	ib_set_device_ops(&us_ibdev->ib_dev, &usnic_dev_ops);
421 
422 	ret = ib_device_set_netdev(&us_ibdev->ib_dev, us_ibdev->netdev, 1);
423 	if (ret)
424 		goto err_fwd_dealloc;
425 
426 	dma_set_max_seg_size(&dev->dev, SZ_2G);
427 	if (ib_register_device(&us_ibdev->ib_dev, "usnic_%d", &dev->dev))
428 		goto err_fwd_dealloc;
429 
430 	usnic_fwd_set_mtu(us_ibdev->ufdev, us_ibdev->netdev->mtu);
431 	usnic_fwd_set_mac(us_ibdev->ufdev, us_ibdev->netdev->dev_addr);
432 	if (netif_carrier_ok(us_ibdev->netdev))
433 		usnic_fwd_carrier_up(us_ibdev->ufdev);
434 
435 	rcu_read_lock();
436 	ind = __in_dev_get_rcu(netdev);
437 	if (ind) {
438 		const struct in_ifaddr *ifa;
439 
440 		ifa = rcu_dereference(ind->ifa_list);
441 		if (ifa)
442 			usnic_fwd_add_ipaddr(us_ibdev->ufdev, ifa->ifa_address);
443 	}
444 	rcu_read_unlock();
445 
446 	usnic_mac_ip_to_gid(us_ibdev->netdev->perm_addr,
447 				us_ibdev->ufdev->inaddr, &gid.raw[0]);
448 	memcpy(&us_ibdev->ib_dev.node_guid, &gid.global.interface_id,
449 		sizeof(gid.global.interface_id));
450 	kref_init(&us_ibdev->vf_cnt);
451 
452 	usnic_info("Added ibdev: %s netdev: %s with mac %pM Link: %u MTU: %u\n",
453 		   dev_name(&us_ibdev->ib_dev.dev),
454 		   netdev_name(us_ibdev->netdev), us_ibdev->ufdev->mac,
455 		   us_ibdev->ufdev->link_up, us_ibdev->ufdev->mtu);
456 	return us_ibdev;
457 
458 err_fwd_dealloc:
459 	usnic_fwd_dev_free(us_ibdev->ufdev);
460 err_dealloc:
461 	usnic_err("failed -- deallocing device\n");
462 	ib_dealloc_device(&us_ibdev->ib_dev);
463 	return NULL;
464 }
465 
usnic_ib_device_remove(struct usnic_ib_dev * us_ibdev)466 static void usnic_ib_device_remove(struct usnic_ib_dev *us_ibdev)
467 {
468 	usnic_info("Unregistering %s\n", dev_name(&us_ibdev->ib_dev.dev));
469 	usnic_ib_sysfs_unregister_usdev(us_ibdev);
470 	usnic_fwd_dev_free(us_ibdev->ufdev);
471 	ib_unregister_device(&us_ibdev->ib_dev);
472 	ib_dealloc_device(&us_ibdev->ib_dev);
473 }
474 
usnic_ib_undiscover_pf(struct kref * kref)475 static void usnic_ib_undiscover_pf(struct kref *kref)
476 {
477 	struct usnic_ib_dev *us_ibdev, *tmp;
478 	struct pci_dev *dev;
479 	bool found = false;
480 
481 	dev = container_of(kref, struct usnic_ib_dev, vf_cnt)->pdev;
482 	mutex_lock(&usnic_ib_ibdev_list_lock);
483 	list_for_each_entry_safe(us_ibdev, tmp,
484 				&usnic_ib_ibdev_list, ib_dev_link) {
485 		if (us_ibdev->pdev == dev) {
486 			list_del(&us_ibdev->ib_dev_link);
487 			found = true;
488 			break;
489 		}
490 	}
491 
492 
493 	mutex_unlock(&usnic_ib_ibdev_list_lock);
494 	if (found)
495 		usnic_ib_device_remove(us_ibdev);
496 	else
497 		WARN(1, "Failed to remove PF %s\n", pci_name(dev));
498 }
499 
usnic_ib_discover_pf(struct usnic_vnic * vnic)500 static struct usnic_ib_dev *usnic_ib_discover_pf(struct usnic_vnic *vnic)
501 {
502 	struct usnic_ib_dev *us_ibdev;
503 	struct pci_dev *parent_pci, *vf_pci;
504 	int err;
505 
506 	vf_pci = usnic_vnic_get_pdev(vnic);
507 	parent_pci = pci_physfn(vf_pci);
508 
509 	BUG_ON(!parent_pci);
510 
511 	mutex_lock(&usnic_ib_ibdev_list_lock);
512 	list_for_each_entry(us_ibdev, &usnic_ib_ibdev_list, ib_dev_link) {
513 		if (us_ibdev->pdev == parent_pci) {
514 			kref_get(&us_ibdev->vf_cnt);
515 			goto out;
516 		}
517 	}
518 
519 	us_ibdev = usnic_ib_device_add(parent_pci);
520 	if (!us_ibdev) {
521 		us_ibdev = ERR_PTR(-EFAULT);
522 		goto out;
523 	}
524 
525 	err = usnic_ib_sysfs_register_usdev(us_ibdev);
526 	if (err) {
527 		usnic_ib_device_remove(us_ibdev);
528 		us_ibdev = ERR_PTR(err);
529 		goto out;
530 	}
531 
532 	list_add(&us_ibdev->ib_dev_link, &usnic_ib_ibdev_list);
533 out:
534 	mutex_unlock(&usnic_ib_ibdev_list_lock);
535 	return us_ibdev;
536 }
537 /* End of PF discovery section */
538 
539 /* Start of PCI section */
540 
541 static const struct pci_device_id usnic_ib_pci_ids[] = {
542 	{PCI_DEVICE(PCI_VENDOR_ID_CISCO, PCI_DEVICE_ID_CISCO_VIC_USPACE_NIC)},
543 	{0,}
544 };
545 
usnic_ib_pci_probe(struct pci_dev * pdev,const struct pci_device_id * id)546 static int usnic_ib_pci_probe(struct pci_dev *pdev,
547 				const struct pci_device_id *id)
548 {
549 	int err;
550 	struct usnic_ib_dev *pf;
551 	struct usnic_ib_vf *vf;
552 	enum usnic_vnic_res_type res_type;
553 
554 	if (!device_iommu_mapped(&pdev->dev)) {
555 		usnic_err("IOMMU required but not present or enabled.  USNIC QPs will not function w/o enabling IOMMU\n");
556 		return -EPERM;
557 	}
558 
559 	vf = kzalloc(sizeof(*vf), GFP_KERNEL);
560 	if (!vf)
561 		return -ENOMEM;
562 
563 	err = pci_enable_device(pdev);
564 	if (err) {
565 		usnic_err("Failed to enable %s with err %d\n",
566 				pci_name(pdev), err);
567 		goto out_clean_vf;
568 	}
569 
570 	err = pci_request_regions(pdev, DRV_NAME);
571 	if (err) {
572 		usnic_err("Failed to request region for %s with err %d\n",
573 				pci_name(pdev), err);
574 		goto out_disable_device;
575 	}
576 
577 	pci_set_master(pdev);
578 	pci_set_drvdata(pdev, vf);
579 
580 	vf->vnic = usnic_vnic_alloc(pdev);
581 	if (IS_ERR_OR_NULL(vf->vnic)) {
582 		err = vf->vnic ? PTR_ERR(vf->vnic) : -ENOMEM;
583 		usnic_err("Failed to alloc vnic for %s with err %d\n",
584 				pci_name(pdev), err);
585 		goto out_release_regions;
586 	}
587 
588 	pf = usnic_ib_discover_pf(vf->vnic);
589 	if (IS_ERR(pf)) {
590 		err = PTR_ERR(pf);
591 		usnic_err("Failed to discover pf of vnic %s with err%d\n",
592 				pci_name(pdev), err);
593 		goto out_clean_vnic;
594 	}
595 
596 	vf->pf = pf;
597 	mutex_init(&vf->lock);
598 	mutex_lock(&pf->usdev_lock);
599 	list_add_tail(&vf->link, &pf->vf_dev_list);
600 	/*
601 	 * Save max settings (will be same for each VF, easier to re-write than
602 	 * to say "if (!set) { set_values(); set=1; }
603 	 */
604 	for (res_type = USNIC_VNIC_RES_TYPE_EOL+1;
605 			res_type < USNIC_VNIC_RES_TYPE_MAX;
606 			res_type++) {
607 		pf->vf_res_cnt[res_type] = usnic_vnic_res_cnt(vf->vnic,
608 								res_type);
609 	}
610 
611 	mutex_unlock(&pf->usdev_lock);
612 
613 	usnic_info("Registering usnic VF %s into PF %s\n", pci_name(pdev),
614 		   dev_name(&pf->ib_dev.dev));
615 	usnic_ib_log_vf(vf);
616 	return 0;
617 
618 out_clean_vnic:
619 	usnic_vnic_free(vf->vnic);
620 out_release_regions:
621 	pci_set_drvdata(pdev, NULL);
622 	pci_release_regions(pdev);
623 out_disable_device:
624 	pci_disable_device(pdev);
625 out_clean_vf:
626 	kfree(vf);
627 	return err;
628 }
629 
usnic_ib_pci_remove(struct pci_dev * pdev)630 static void usnic_ib_pci_remove(struct pci_dev *pdev)
631 {
632 	struct usnic_ib_vf *vf = pci_get_drvdata(pdev);
633 	struct usnic_ib_dev *pf = vf->pf;
634 
635 	mutex_lock(&pf->usdev_lock);
636 	list_del(&vf->link);
637 	mutex_unlock(&pf->usdev_lock);
638 
639 	kref_put(&pf->vf_cnt, usnic_ib_undiscover_pf);
640 	usnic_vnic_free(vf->vnic);
641 	pci_set_drvdata(pdev, NULL);
642 	pci_release_regions(pdev);
643 	pci_disable_device(pdev);
644 	kfree(vf);
645 
646 	usnic_info("Removed VF %s\n", pci_name(pdev));
647 }
648 
649 /* PCI driver entry points */
650 static struct pci_driver usnic_ib_pci_driver = {
651 	.name = DRV_NAME,
652 	.id_table = usnic_ib_pci_ids,
653 	.probe = usnic_ib_pci_probe,
654 	.remove = usnic_ib_pci_remove,
655 };
656 /* End of PCI section */
657 
658 /* Start of module section */
usnic_ib_init(void)659 static int __init usnic_ib_init(void)
660 {
661 	int err;
662 
663 	printk_once(KERN_INFO "%s", usnic_version);
664 
665 	err = pci_register_driver(&usnic_ib_pci_driver);
666 	if (err) {
667 		usnic_err("Unable to register with PCI\n");
668 		goto out_umem_fini;
669 	}
670 
671 	err = register_netdevice_notifier(&usnic_ib_netdevice_notifier);
672 	if (err) {
673 		usnic_err("Failed to register netdev notifier\n");
674 		goto out_pci_unreg;
675 	}
676 
677 	err = register_inetaddr_notifier(&usnic_ib_inetaddr_notifier);
678 	if (err) {
679 		usnic_err("Failed to register inet addr notifier\n");
680 		goto out_unreg_netdev_notifier;
681 	}
682 
683 	err = usnic_transport_init();
684 	if (err) {
685 		usnic_err("Failed to initialize transport\n");
686 		goto out_unreg_inetaddr_notifier;
687 	}
688 
689 	usnic_debugfs_init();
690 
691 	return 0;
692 
693 out_unreg_inetaddr_notifier:
694 	unregister_inetaddr_notifier(&usnic_ib_inetaddr_notifier);
695 out_unreg_netdev_notifier:
696 	unregister_netdevice_notifier(&usnic_ib_netdevice_notifier);
697 out_pci_unreg:
698 	pci_unregister_driver(&usnic_ib_pci_driver);
699 out_umem_fini:
700 
701 	return err;
702 }
703 
usnic_ib_destroy(void)704 static void __exit usnic_ib_destroy(void)
705 {
706 	usnic_dbg("\n");
707 	usnic_debugfs_exit();
708 	usnic_transport_fini();
709 	unregister_inetaddr_notifier(&usnic_ib_inetaddr_notifier);
710 	unregister_netdevice_notifier(&usnic_ib_netdevice_notifier);
711 	pci_unregister_driver(&usnic_ib_pci_driver);
712 }
713 
714 MODULE_DESCRIPTION("Cisco VIC (usNIC) Verbs Driver");
715 MODULE_AUTHOR("Upinder Malhi <[email protected]>");
716 MODULE_LICENSE("Dual BSD/GPL");
717 module_param(usnic_log_lvl, uint, S_IRUGO | S_IWUSR);
718 module_param(usnic_ib_share_vf, uint, S_IRUGO | S_IWUSR);
719 MODULE_PARM_DESC(usnic_log_lvl, " Off=0, Err=1, Info=2, Debug=3");
720 MODULE_PARM_DESC(usnic_ib_share_vf, "Off=0, On=1 VF sharing amongst QPs");
721 MODULE_DEVICE_TABLE(pci, usnic_ib_pci_ids);
722 
723 module_init(usnic_ib_init);
724 module_exit(usnic_ib_destroy);
725 /* End of module section */
726