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 <umalhi@cisco.com>
33  * Author: Anant Deepak <anadeepa@cisco.com>
34  * Author: Cesare Cantu' <cantuc@cisco.com>
35  * Author: Jeff Squyres <jsquyres@cisco.com>
36  * Author: Kiran Thirumalai <kithirum@cisco.com>
37  * Author: Xuyang Wang <xuywang@cisco.com>
38  * Author: Reese Faucette <rfaucett@cisco.com>
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 */
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 
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 
90 void usnic_ib_log_vf(struct usnic_ib_vf *vf)
91 {
92 	char buf[1000];
93 	usnic_ib_dump_vf(vf, buf, sizeof(buf));
94 	usnic_dbg("%s\n", buf);
95 }
96 
97 /* Start of netdev section */
98 static void usnic_ib_qp_grp_modify_active_to_err(struct usnic_ib_dev *us_ibdev)
99 {
100 	struct usnic_ib_ucontext *ctx;
101 	struct usnic_ib_qp_grp *qp_grp;
102 	enum ib_qp_state cur_state;
103 	int status;
104 
105 	BUG_ON(!mutex_is_locked(&us_ibdev->usdev_lock));
106 
107 	list_for_each_entry(ctx, &us_ibdev->ctx_list, link) {
108 		list_for_each_entry(qp_grp, &ctx->qp_grp_list, link) {
109 			cur_state = qp_grp->state;
110 			if (cur_state == IB_QPS_INIT ||
111 				cur_state == IB_QPS_RTR ||
112 				cur_state == IB_QPS_RTS) {
113 				status = usnic_ib_qp_grp_modify(qp_grp,
114 								IB_QPS_ERR,
115 								NULL);
116 				if (status) {
117 					usnic_err("Failed to transistion qp grp %u from %s to %s\n",
118 						qp_grp->grp_id,
119 						usnic_ib_qp_grp_state_to_string
120 						(cur_state),
121 						usnic_ib_qp_grp_state_to_string
122 						(IB_QPS_ERR));
123 				}
124 			}
125 		}
126 	}
127 }
128 
129 static void usnic_ib_handle_usdev_event(struct usnic_ib_dev *us_ibdev,
130 					unsigned long event)
131 {
132 	struct net_device *netdev;
133 	struct ib_event ib_event;
134 
135 	memset(&ib_event, 0, sizeof(ib_event));
136 
137 	mutex_lock(&us_ibdev->usdev_lock);
138 	netdev = us_ibdev->netdev;
139 	switch (event) {
140 	case NETDEV_REBOOT:
141 		usnic_info("PF Reset on %s\n", dev_name(&us_ibdev->ib_dev.dev));
142 		usnic_ib_qp_grp_modify_active_to_err(us_ibdev);
143 		ib_event.event = IB_EVENT_PORT_ERR;
144 		ib_event.device = &us_ibdev->ib_dev;
145 		ib_event.element.port_num = 1;
146 		ib_dispatch_event(&ib_event);
147 		break;
148 	case NETDEV_UP:
149 	case NETDEV_DOWN:
150 	case NETDEV_CHANGE:
151 		if (!us_ibdev->ufdev->link_up &&
152 				netif_carrier_ok(netdev)) {
153 			usnic_fwd_carrier_up(us_ibdev->ufdev);
154 			usnic_info("Link UP on %s\n",
155 				   dev_name(&us_ibdev->ib_dev.dev));
156 			ib_event.event = IB_EVENT_PORT_ACTIVE;
157 			ib_event.device = &us_ibdev->ib_dev;
158 			ib_event.element.port_num = 1;
159 			ib_dispatch_event(&ib_event);
160 		} else if (us_ibdev->ufdev->link_up &&
161 				!netif_carrier_ok(netdev)) {
162 			usnic_fwd_carrier_down(us_ibdev->ufdev);
163 			usnic_info("Link DOWN on %s\n",
164 				   dev_name(&us_ibdev->ib_dev.dev));
165 			usnic_ib_qp_grp_modify_active_to_err(us_ibdev);
166 			ib_event.event = IB_EVENT_PORT_ERR;
167 			ib_event.device = &us_ibdev->ib_dev;
168 			ib_event.element.port_num = 1;
169 			ib_dispatch_event(&ib_event);
170 		} else {
171 			usnic_dbg("Ignoring %s on %s\n",
172 					netdev_cmd_to_name(event),
173 					dev_name(&us_ibdev->ib_dev.dev));
174 		}
175 		break;
176 	case NETDEV_CHANGEADDR:
177 		if (!memcmp(us_ibdev->ufdev->mac, netdev->dev_addr,
178 				sizeof(us_ibdev->ufdev->mac))) {
179 			usnic_dbg("Ignoring addr change on %s\n",
180 				  dev_name(&us_ibdev->ib_dev.dev));
181 		} else {
182 			usnic_info(" %s old mac: %pM new mac: %pM\n",
183 					dev_name(&us_ibdev->ib_dev.dev),
184 					us_ibdev->ufdev->mac,
185 					netdev->dev_addr);
186 			usnic_fwd_set_mac(us_ibdev->ufdev, netdev->dev_addr);
187 			usnic_ib_qp_grp_modify_active_to_err(us_ibdev);
188 			ib_event.event = IB_EVENT_GID_CHANGE;
189 			ib_event.device = &us_ibdev->ib_dev;
190 			ib_event.element.port_num = 1;
191 			ib_dispatch_event(&ib_event);
192 		}
193 
194 		break;
195 	case NETDEV_CHANGEMTU:
196 		if (us_ibdev->ufdev->mtu != netdev->mtu) {
197 			usnic_info("MTU Change on %s old: %u new: %u\n",
198 					dev_name(&us_ibdev->ib_dev.dev),
199 					us_ibdev->ufdev->mtu, netdev->mtu);
200 			usnic_fwd_set_mtu(us_ibdev->ufdev, netdev->mtu);
201 			usnic_ib_qp_grp_modify_active_to_err(us_ibdev);
202 		} else {
203 			usnic_dbg("Ignoring MTU change on %s\n",
204 				  dev_name(&us_ibdev->ib_dev.dev));
205 		}
206 		break;
207 	default:
208 		usnic_dbg("Ignoring event %s on %s",
209 				netdev_cmd_to_name(event),
210 				dev_name(&us_ibdev->ib_dev.dev));
211 	}
212 	mutex_unlock(&us_ibdev->usdev_lock);
213 }
214 
215 static int usnic_ib_netdevice_event(struct notifier_block *notifier,
216 					unsigned long event, void *ptr)
217 {
218 	struct usnic_ib_dev *us_ibdev;
219 	struct ib_device *ibdev;
220 
221 	struct net_device *netdev = netdev_notifier_info_to_dev(ptr);
222 
223 	ibdev = ib_device_get_by_netdev(netdev, RDMA_DRIVER_USNIC);
224 	if (!ibdev)
225 		return NOTIFY_DONE;
226 
227 	us_ibdev = container_of(ibdev, struct usnic_ib_dev, ib_dev);
228 	usnic_ib_handle_usdev_event(us_ibdev, event);
229 	ib_device_put(ibdev);
230 	return NOTIFY_DONE;
231 }
232 
233 static struct notifier_block usnic_ib_netdevice_notifier = {
234 	.notifier_call = usnic_ib_netdevice_event
235 };
236 /* End of netdev section */
237 
238 /* Start of inet section */
239 static int usnic_ib_handle_inet_event(struct usnic_ib_dev *us_ibdev,
240 					unsigned long event, void *ptr)
241 {
242 	struct in_ifaddr *ifa = ptr;
243 	struct ib_event ib_event;
244 
245 	mutex_lock(&us_ibdev->usdev_lock);
246 
247 	switch (event) {
248 	case NETDEV_DOWN:
249 		usnic_info("%s via ip notifiers",
250 				netdev_cmd_to_name(event));
251 		usnic_fwd_del_ipaddr(us_ibdev->ufdev);
252 		usnic_ib_qp_grp_modify_active_to_err(us_ibdev);
253 		ib_event.event = IB_EVENT_GID_CHANGE;
254 		ib_event.device = &us_ibdev->ib_dev;
255 		ib_event.element.port_num = 1;
256 		ib_dispatch_event(&ib_event);
257 		break;
258 	case NETDEV_UP:
259 		usnic_fwd_add_ipaddr(us_ibdev->ufdev, ifa->ifa_address);
260 		usnic_info("%s via ip notifiers: ip %pI4",
261 				netdev_cmd_to_name(event),
262 				&us_ibdev->ufdev->inaddr);
263 		ib_event.event = IB_EVENT_GID_CHANGE;
264 		ib_event.device = &us_ibdev->ib_dev;
265 		ib_event.element.port_num = 1;
266 		ib_dispatch_event(&ib_event);
267 		break;
268 	default:
269 		usnic_info("Ignoring event %s on %s",
270 				netdev_cmd_to_name(event),
271 				dev_name(&us_ibdev->ib_dev.dev));
272 	}
273 	mutex_unlock(&us_ibdev->usdev_lock);
274 
275 	return NOTIFY_DONE;
276 }
277 
278 static int usnic_ib_inetaddr_event(struct notifier_block *notifier,
279 					unsigned long event, void *ptr)
280 {
281 	struct usnic_ib_dev *us_ibdev;
282 	struct in_ifaddr *ifa = ptr;
283 	struct net_device *netdev = ifa->ifa_dev->dev;
284 	struct ib_device *ibdev;
285 
286 	ibdev = ib_device_get_by_netdev(netdev, RDMA_DRIVER_USNIC);
287 	if (!ibdev)
288 		return NOTIFY_DONE;
289 
290 	us_ibdev = container_of(ibdev, struct usnic_ib_dev, ib_dev);
291 	usnic_ib_handle_inet_event(us_ibdev, event, ptr);
292 	ib_device_put(ibdev);
293 	return NOTIFY_DONE;
294 }
295 static struct notifier_block usnic_ib_inetaddr_notifier = {
296 	.notifier_call = usnic_ib_inetaddr_event
297 };
298 /* End of inet section*/
299 
300 static int usnic_port_immutable(struct ib_device *ibdev, u8 port_num,
301 			        struct ib_port_immutable *immutable)
302 {
303 	struct ib_port_attr attr;
304 	int err;
305 
306 	immutable->core_cap_flags = RDMA_CORE_PORT_USNIC;
307 
308 	err = ib_query_port(ibdev, port_num, &attr);
309 	if (err)
310 		return err;
311 
312 	immutable->pkey_tbl_len = attr.pkey_tbl_len;
313 	immutable->gid_tbl_len = attr.gid_tbl_len;
314 
315 	return 0;
316 }
317 
318 static void usnic_get_dev_fw_str(struct ib_device *device, char *str)
319 {
320 	struct usnic_ib_dev *us_ibdev =
321 		container_of(device, struct usnic_ib_dev, ib_dev);
322 	struct ethtool_drvinfo info;
323 
324 	mutex_lock(&us_ibdev->usdev_lock);
325 	us_ibdev->netdev->ethtool_ops->get_drvinfo(us_ibdev->netdev, &info);
326 	mutex_unlock(&us_ibdev->usdev_lock);
327 
328 	snprintf(str, IB_FW_VERSION_NAME_MAX, "%s", info.fw_version);
329 }
330 
331 static const struct ib_device_ops usnic_dev_ops = {
332 	.owner = THIS_MODULE,
333 	.driver_id = RDMA_DRIVER_USNIC,
334 	.uverbs_abi_ver = USNIC_UVERBS_ABI_VERSION,
335 
336 	.alloc_pd = usnic_ib_alloc_pd,
337 	.alloc_ucontext = usnic_ib_alloc_ucontext,
338 	.create_cq = usnic_ib_create_cq,
339 	.create_qp = usnic_ib_create_qp,
340 	.dealloc_pd = usnic_ib_dealloc_pd,
341 	.dealloc_ucontext = usnic_ib_dealloc_ucontext,
342 	.dereg_mr = usnic_ib_dereg_mr,
343 	.destroy_cq = usnic_ib_destroy_cq,
344 	.destroy_qp = usnic_ib_destroy_qp,
345 	.get_dev_fw_str = usnic_get_dev_fw_str,
346 	.get_link_layer = usnic_ib_port_link_layer,
347 	.get_port_immutable = usnic_port_immutable,
348 	.mmap = usnic_ib_mmap,
349 	.modify_qp = usnic_ib_modify_qp,
350 	.query_device = usnic_ib_query_device,
351 	.query_gid = usnic_ib_query_gid,
352 	.query_pkey = usnic_ib_query_pkey,
353 	.query_port = usnic_ib_query_port,
354 	.query_qp = usnic_ib_query_qp,
355 	.reg_user_mr = usnic_ib_reg_mr,
356 	INIT_RDMA_OBJ_SIZE(ib_pd, usnic_ib_pd, ibpd),
357 	INIT_RDMA_OBJ_SIZE(ib_cq, usnic_ib_cq, ibcq),
358 	INIT_RDMA_OBJ_SIZE(ib_ucontext, usnic_ib_ucontext, ibucontext),
359 };
360 
361 /* Start of PF discovery section */
362 static void *usnic_ib_device_add(struct pci_dev *dev)
363 {
364 	struct usnic_ib_dev *us_ibdev;
365 	union ib_gid gid;
366 	struct in_device *ind;
367 	struct net_device *netdev;
368 	int ret;
369 
370 	usnic_dbg("\n");
371 	netdev = pci_get_drvdata(dev);
372 
373 	us_ibdev = ib_alloc_device(usnic_ib_dev, ib_dev);
374 	if (!us_ibdev) {
375 		usnic_err("Device %s context alloc failed\n",
376 				netdev_name(pci_get_drvdata(dev)));
377 		return ERR_PTR(-EFAULT);
378 	}
379 
380 	us_ibdev->ufdev = usnic_fwd_dev_alloc(dev);
381 	if (!us_ibdev->ufdev) {
382 		usnic_err("Failed to alloc ufdev for %s\n", pci_name(dev));
383 		goto err_dealloc;
384 	}
385 
386 	mutex_init(&us_ibdev->usdev_lock);
387 	INIT_LIST_HEAD(&us_ibdev->vf_dev_list);
388 	INIT_LIST_HEAD(&us_ibdev->ctx_list);
389 
390 	us_ibdev->pdev = dev;
391 	us_ibdev->netdev = pci_get_drvdata(dev);
392 	us_ibdev->ib_dev.node_type = RDMA_NODE_USNIC_UDP;
393 	us_ibdev->ib_dev.phys_port_cnt = USNIC_IB_PORT_CNT;
394 	us_ibdev->ib_dev.num_comp_vectors = USNIC_IB_NUM_COMP_VECTORS;
395 	us_ibdev->ib_dev.dev.parent = &dev->dev;
396 
397 	us_ibdev->ib_dev.uverbs_cmd_mask =
398 		(1ull << IB_USER_VERBS_CMD_GET_CONTEXT) |
399 		(1ull << IB_USER_VERBS_CMD_QUERY_DEVICE) |
400 		(1ull << IB_USER_VERBS_CMD_QUERY_PORT) |
401 		(1ull << IB_USER_VERBS_CMD_ALLOC_PD) |
402 		(1ull << IB_USER_VERBS_CMD_DEALLOC_PD) |
403 		(1ull << IB_USER_VERBS_CMD_REG_MR) |
404 		(1ull << IB_USER_VERBS_CMD_DEREG_MR) |
405 		(1ull << IB_USER_VERBS_CMD_CREATE_COMP_CHANNEL) |
406 		(1ull << IB_USER_VERBS_CMD_CREATE_CQ) |
407 		(1ull << IB_USER_VERBS_CMD_DESTROY_CQ) |
408 		(1ull << IB_USER_VERBS_CMD_CREATE_QP) |
409 		(1ull << IB_USER_VERBS_CMD_MODIFY_QP) |
410 		(1ull << IB_USER_VERBS_CMD_QUERY_QP) |
411 		(1ull << IB_USER_VERBS_CMD_DESTROY_QP) |
412 		(1ull << IB_USER_VERBS_CMD_ATTACH_MCAST) |
413 		(1ull << IB_USER_VERBS_CMD_DETACH_MCAST) |
414 		(1ull << IB_USER_VERBS_CMD_OPEN_QP);
415 
416 	ib_set_device_ops(&us_ibdev->ib_dev, &usnic_dev_ops);
417 
418 	rdma_set_device_sysfs_group(&us_ibdev->ib_dev, &usnic_attr_group);
419 
420 	ret = ib_device_set_netdev(&us_ibdev->ib_dev, us_ibdev->netdev, 1);
421 	if (ret)
422 		goto err_fwd_dealloc;
423 
424 	if (ib_register_device(&us_ibdev->ib_dev, "usnic_%d"))
425 		goto err_fwd_dealloc;
426 
427 	usnic_fwd_set_mtu(us_ibdev->ufdev, us_ibdev->netdev->mtu);
428 	usnic_fwd_set_mac(us_ibdev->ufdev, us_ibdev->netdev->dev_addr);
429 	if (netif_carrier_ok(us_ibdev->netdev))
430 		usnic_fwd_carrier_up(us_ibdev->ufdev);
431 
432 	rcu_read_lock();
433 	ind = __in_dev_get_rcu(netdev);
434 	if (ind) {
435 		const struct in_ifaddr *ifa;
436 
437 		ifa = rcu_dereference(ind->ifa_list);
438 		if (ifa)
439 			usnic_fwd_add_ipaddr(us_ibdev->ufdev, ifa->ifa_address);
440 	}
441 	rcu_read_unlock();
442 
443 	usnic_mac_ip_to_gid(us_ibdev->netdev->perm_addr,
444 				us_ibdev->ufdev->inaddr, &gid.raw[0]);
445 	memcpy(&us_ibdev->ib_dev.node_guid, &gid.global.interface_id,
446 		sizeof(gid.global.interface_id));
447 	kref_init(&us_ibdev->vf_cnt);
448 
449 	usnic_info("Added ibdev: %s netdev: %s with mac %pM Link: %u MTU: %u\n",
450 		   dev_name(&us_ibdev->ib_dev.dev),
451 		   netdev_name(us_ibdev->netdev), us_ibdev->ufdev->mac,
452 		   us_ibdev->ufdev->link_up, us_ibdev->ufdev->mtu);
453 	return us_ibdev;
454 
455 err_fwd_dealloc:
456 	usnic_fwd_dev_free(us_ibdev->ufdev);
457 err_dealloc:
458 	usnic_err("failed -- deallocing device\n");
459 	ib_dealloc_device(&us_ibdev->ib_dev);
460 	return NULL;
461 }
462 
463 static void usnic_ib_device_remove(struct usnic_ib_dev *us_ibdev)
464 {
465 	usnic_info("Unregistering %s\n", dev_name(&us_ibdev->ib_dev.dev));
466 	usnic_ib_sysfs_unregister_usdev(us_ibdev);
467 	usnic_fwd_dev_free(us_ibdev->ufdev);
468 	ib_unregister_device(&us_ibdev->ib_dev);
469 	ib_dealloc_device(&us_ibdev->ib_dev);
470 }
471 
472 static void usnic_ib_undiscover_pf(struct kref *kref)
473 {
474 	struct usnic_ib_dev *us_ibdev, *tmp;
475 	struct pci_dev *dev;
476 	bool found = false;
477 
478 	dev = container_of(kref, struct usnic_ib_dev, vf_cnt)->pdev;
479 	mutex_lock(&usnic_ib_ibdev_list_lock);
480 	list_for_each_entry_safe(us_ibdev, tmp,
481 				&usnic_ib_ibdev_list, ib_dev_link) {
482 		if (us_ibdev->pdev == dev) {
483 			list_del(&us_ibdev->ib_dev_link);
484 			found = true;
485 			break;
486 		}
487 	}
488 
489 
490 	mutex_unlock(&usnic_ib_ibdev_list_lock);
491 	if (found)
492 		usnic_ib_device_remove(us_ibdev);
493 	else
494 		WARN(1, "Failed to remove PF %s\n", pci_name(dev));
495 }
496 
497 static struct usnic_ib_dev *usnic_ib_discover_pf(struct usnic_vnic *vnic)
498 {
499 	struct usnic_ib_dev *us_ibdev;
500 	struct pci_dev *parent_pci, *vf_pci;
501 	int err;
502 
503 	vf_pci = usnic_vnic_get_pdev(vnic);
504 	parent_pci = pci_physfn(vf_pci);
505 
506 	BUG_ON(!parent_pci);
507 
508 	mutex_lock(&usnic_ib_ibdev_list_lock);
509 	list_for_each_entry(us_ibdev, &usnic_ib_ibdev_list, ib_dev_link) {
510 		if (us_ibdev->pdev == parent_pci) {
511 			kref_get(&us_ibdev->vf_cnt);
512 			goto out;
513 		}
514 	}
515 
516 	us_ibdev = usnic_ib_device_add(parent_pci);
517 	if (IS_ERR_OR_NULL(us_ibdev)) {
518 		us_ibdev = us_ibdev ? us_ibdev : ERR_PTR(-EFAULT);
519 		goto out;
520 	}
521 
522 	err = usnic_ib_sysfs_register_usdev(us_ibdev);
523 	if (err) {
524 		usnic_ib_device_remove(us_ibdev);
525 		us_ibdev = ERR_PTR(err);
526 		goto out;
527 	}
528 
529 	list_add(&us_ibdev->ib_dev_link, &usnic_ib_ibdev_list);
530 out:
531 	mutex_unlock(&usnic_ib_ibdev_list_lock);
532 	return us_ibdev;
533 }
534 /* End of PF discovery section */
535 
536 /* Start of PCI section */
537 
538 static const struct pci_device_id usnic_ib_pci_ids[] = {
539 	{PCI_DEVICE(PCI_VENDOR_ID_CISCO, PCI_DEVICE_ID_CISCO_VIC_USPACE_NIC)},
540 	{0,}
541 };
542 
543 static int usnic_ib_pci_probe(struct pci_dev *pdev,
544 				const struct pci_device_id *id)
545 {
546 	int err;
547 	struct usnic_ib_dev *pf;
548 	struct usnic_ib_vf *vf;
549 	enum usnic_vnic_res_type res_type;
550 
551 	vf = kzalloc(sizeof(*vf), GFP_KERNEL);
552 	if (!vf)
553 		return -ENOMEM;
554 
555 	err = pci_enable_device(pdev);
556 	if (err) {
557 		usnic_err("Failed to enable %s with err %d\n",
558 				pci_name(pdev), err);
559 		goto out_clean_vf;
560 	}
561 
562 	err = pci_request_regions(pdev, DRV_NAME);
563 	if (err) {
564 		usnic_err("Failed to request region for %s with err %d\n",
565 				pci_name(pdev), err);
566 		goto out_disable_device;
567 	}
568 
569 	pci_set_master(pdev);
570 	pci_set_drvdata(pdev, vf);
571 
572 	vf->vnic = usnic_vnic_alloc(pdev);
573 	if (IS_ERR_OR_NULL(vf->vnic)) {
574 		err = vf->vnic ? PTR_ERR(vf->vnic) : -ENOMEM;
575 		usnic_err("Failed to alloc vnic for %s with err %d\n",
576 				pci_name(pdev), err);
577 		goto out_release_regions;
578 	}
579 
580 	pf = usnic_ib_discover_pf(vf->vnic);
581 	if (IS_ERR_OR_NULL(pf)) {
582 		usnic_err("Failed to discover pf of vnic %s with err%ld\n",
583 				pci_name(pdev), PTR_ERR(pf));
584 		err = pf ? PTR_ERR(pf) : -EFAULT;
585 		goto out_clean_vnic;
586 	}
587 
588 	vf->pf = pf;
589 	spin_lock_init(&vf->lock);
590 	mutex_lock(&pf->usdev_lock);
591 	list_add_tail(&vf->link, &pf->vf_dev_list);
592 	/*
593 	 * Save max settings (will be same for each VF, easier to re-write than
594 	 * to say "if (!set) { set_values(); set=1; }
595 	 */
596 	for (res_type = USNIC_VNIC_RES_TYPE_EOL+1;
597 			res_type < USNIC_VNIC_RES_TYPE_MAX;
598 			res_type++) {
599 		pf->vf_res_cnt[res_type] = usnic_vnic_res_cnt(vf->vnic,
600 								res_type);
601 	}
602 
603 	mutex_unlock(&pf->usdev_lock);
604 
605 	usnic_info("Registering usnic VF %s into PF %s\n", pci_name(pdev),
606 		   dev_name(&pf->ib_dev.dev));
607 	usnic_ib_log_vf(vf);
608 	return 0;
609 
610 out_clean_vnic:
611 	usnic_vnic_free(vf->vnic);
612 out_release_regions:
613 	pci_set_drvdata(pdev, NULL);
614 	pci_clear_master(pdev);
615 	pci_release_regions(pdev);
616 out_disable_device:
617 	pci_disable_device(pdev);
618 out_clean_vf:
619 	kfree(vf);
620 	return err;
621 }
622 
623 static void usnic_ib_pci_remove(struct pci_dev *pdev)
624 {
625 	struct usnic_ib_vf *vf = pci_get_drvdata(pdev);
626 	struct usnic_ib_dev *pf = vf->pf;
627 
628 	mutex_lock(&pf->usdev_lock);
629 	list_del(&vf->link);
630 	mutex_unlock(&pf->usdev_lock);
631 
632 	kref_put(&pf->vf_cnt, usnic_ib_undiscover_pf);
633 	usnic_vnic_free(vf->vnic);
634 	pci_set_drvdata(pdev, NULL);
635 	pci_clear_master(pdev);
636 	pci_release_regions(pdev);
637 	pci_disable_device(pdev);
638 	kfree(vf);
639 
640 	usnic_info("Removed VF %s\n", pci_name(pdev));
641 }
642 
643 /* PCI driver entry points */
644 static struct pci_driver usnic_ib_pci_driver = {
645 	.name = DRV_NAME,
646 	.id_table = usnic_ib_pci_ids,
647 	.probe = usnic_ib_pci_probe,
648 	.remove = usnic_ib_pci_remove,
649 };
650 /* End of PCI section */
651 
652 /* Start of module section */
653 static int __init usnic_ib_init(void)
654 {
655 	int err;
656 
657 	printk_once(KERN_INFO "%s", usnic_version);
658 
659 	err = usnic_uiom_init(DRV_NAME);
660 	if (err) {
661 		usnic_err("Unable to initialize umem with err %d\n", err);
662 		return err;
663 	}
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 
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 <umalhi@cisco.com>");
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