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 ", vf->pf->ib_dev.name);
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", us_ibdev->ib_dev.name);
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", us_ibdev->ib_dev.name);
155 			ib_event.event = IB_EVENT_PORT_ACTIVE;
156 			ib_event.device = &us_ibdev->ib_dev;
157 			ib_event.element.port_num = 1;
158 			ib_dispatch_event(&ib_event);
159 		} else if (us_ibdev->ufdev->link_up &&
160 				!netif_carrier_ok(netdev)) {
161 			usnic_fwd_carrier_down(us_ibdev->ufdev);
162 			usnic_info("Link DOWN on %s\n", us_ibdev->ib_dev.name);
163 			usnic_ib_qp_grp_modify_active_to_err(us_ibdev);
164 			ib_event.event = IB_EVENT_PORT_ERR;
165 			ib_event.device = &us_ibdev->ib_dev;
166 			ib_event.element.port_num = 1;
167 			ib_dispatch_event(&ib_event);
168 		} else {
169 			usnic_dbg("Ignoring %s on %s\n",
170 					netdev_cmd_to_name(event),
171 					us_ibdev->ib_dev.name);
172 		}
173 		break;
174 	case NETDEV_CHANGEADDR:
175 		if (!memcmp(us_ibdev->ufdev->mac, netdev->dev_addr,
176 				sizeof(us_ibdev->ufdev->mac))) {
177 			usnic_dbg("Ignoring addr change on %s\n",
178 					us_ibdev->ib_dev.name);
179 		} else {
180 			usnic_info(" %s old mac: %pM new mac: %pM\n",
181 					us_ibdev->ib_dev.name,
182 					us_ibdev->ufdev->mac,
183 					netdev->dev_addr);
184 			usnic_fwd_set_mac(us_ibdev->ufdev, netdev->dev_addr);
185 			usnic_ib_qp_grp_modify_active_to_err(us_ibdev);
186 			ib_event.event = IB_EVENT_GID_CHANGE;
187 			ib_event.device = &us_ibdev->ib_dev;
188 			ib_event.element.port_num = 1;
189 			ib_dispatch_event(&ib_event);
190 		}
191 
192 		break;
193 	case NETDEV_CHANGEMTU:
194 		if (us_ibdev->ufdev->mtu != netdev->mtu) {
195 			usnic_info("MTU Change on %s old: %u new: %u\n",
196 					us_ibdev->ib_dev.name,
197 					us_ibdev->ufdev->mtu, netdev->mtu);
198 			usnic_fwd_set_mtu(us_ibdev->ufdev, netdev->mtu);
199 			usnic_ib_qp_grp_modify_active_to_err(us_ibdev);
200 		} else {
201 			usnic_dbg("Ignoring MTU change on %s\n",
202 					us_ibdev->ib_dev.name);
203 		}
204 		break;
205 	default:
206 		usnic_dbg("Ignoring event %s on %s",
207 				netdev_cmd_to_name(event),
208 				us_ibdev->ib_dev.name);
209 	}
210 	mutex_unlock(&us_ibdev->usdev_lock);
211 }
212 
213 static int usnic_ib_netdevice_event(struct notifier_block *notifier,
214 					unsigned long event, void *ptr)
215 {
216 	struct usnic_ib_dev *us_ibdev;
217 
218 	struct net_device *netdev = netdev_notifier_info_to_dev(ptr);
219 
220 	mutex_lock(&usnic_ib_ibdev_list_lock);
221 	list_for_each_entry(us_ibdev, &usnic_ib_ibdev_list, ib_dev_link) {
222 		if (us_ibdev->netdev == netdev) {
223 			usnic_ib_handle_usdev_event(us_ibdev, event);
224 			break;
225 		}
226 	}
227 	mutex_unlock(&usnic_ib_ibdev_list_lock);
228 
229 	return NOTIFY_DONE;
230 }
231 
232 static struct notifier_block usnic_ib_netdevice_notifier = {
233 	.notifier_call = usnic_ib_netdevice_event
234 };
235 /* End of netdev section */
236 
237 /* Start of inet section */
238 static int usnic_ib_handle_inet_event(struct usnic_ib_dev *us_ibdev,
239 					unsigned long event, void *ptr)
240 {
241 	struct in_ifaddr *ifa = ptr;
242 	struct ib_event ib_event;
243 
244 	mutex_lock(&us_ibdev->usdev_lock);
245 
246 	switch (event) {
247 	case NETDEV_DOWN:
248 		usnic_info("%s via ip notifiers",
249 				netdev_cmd_to_name(event));
250 		usnic_fwd_del_ipaddr(us_ibdev->ufdev);
251 		usnic_ib_qp_grp_modify_active_to_err(us_ibdev);
252 		ib_event.event = IB_EVENT_GID_CHANGE;
253 		ib_event.device = &us_ibdev->ib_dev;
254 		ib_event.element.port_num = 1;
255 		ib_dispatch_event(&ib_event);
256 		break;
257 	case NETDEV_UP:
258 		usnic_fwd_add_ipaddr(us_ibdev->ufdev, ifa->ifa_address);
259 		usnic_info("%s via ip notifiers: ip %pI4",
260 				netdev_cmd_to_name(event),
261 				&us_ibdev->ufdev->inaddr);
262 		ib_event.event = IB_EVENT_GID_CHANGE;
263 		ib_event.device = &us_ibdev->ib_dev;
264 		ib_event.element.port_num = 1;
265 		ib_dispatch_event(&ib_event);
266 		break;
267 	default:
268 		usnic_info("Ignoring event %s on %s",
269 				netdev_cmd_to_name(event),
270 				us_ibdev->ib_dev.name);
271 	}
272 	mutex_unlock(&us_ibdev->usdev_lock);
273 
274 	return NOTIFY_DONE;
275 }
276 
277 static int usnic_ib_inetaddr_event(struct notifier_block *notifier,
278 					unsigned long event, void *ptr)
279 {
280 	struct usnic_ib_dev *us_ibdev;
281 	struct in_ifaddr *ifa = ptr;
282 	struct net_device *netdev = ifa->ifa_dev->dev;
283 
284 	mutex_lock(&usnic_ib_ibdev_list_lock);
285 	list_for_each_entry(us_ibdev, &usnic_ib_ibdev_list, ib_dev_link) {
286 		if (us_ibdev->netdev == netdev) {
287 			usnic_ib_handle_inet_event(us_ibdev, event, ptr);
288 			break;
289 		}
290 	}
291 	mutex_unlock(&usnic_ib_ibdev_list_lock);
292 
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 /* Start of PF discovery section */
332 static void *usnic_ib_device_add(struct pci_dev *dev)
333 {
334 	struct usnic_ib_dev *us_ibdev;
335 	union ib_gid gid;
336 	struct in_device *ind;
337 	struct net_device *netdev;
338 
339 	usnic_dbg("\n");
340 	netdev = pci_get_drvdata(dev);
341 
342 	us_ibdev = (struct usnic_ib_dev *)ib_alloc_device(sizeof(*us_ibdev));
343 	if (!us_ibdev) {
344 		usnic_err("Device %s context alloc failed\n",
345 				netdev_name(pci_get_drvdata(dev)));
346 		return ERR_PTR(-EFAULT);
347 	}
348 
349 	us_ibdev->ufdev = usnic_fwd_dev_alloc(dev);
350 	if (!us_ibdev->ufdev) {
351 		usnic_err("Failed to alloc ufdev for %s\n", pci_name(dev));
352 		goto err_dealloc;
353 	}
354 
355 	mutex_init(&us_ibdev->usdev_lock);
356 	INIT_LIST_HEAD(&us_ibdev->vf_dev_list);
357 	INIT_LIST_HEAD(&us_ibdev->ctx_list);
358 
359 	us_ibdev->pdev = dev;
360 	us_ibdev->netdev = pci_get_drvdata(dev);
361 	us_ibdev->ib_dev.owner = THIS_MODULE;
362 	us_ibdev->ib_dev.node_type = RDMA_NODE_USNIC_UDP;
363 	us_ibdev->ib_dev.phys_port_cnt = USNIC_IB_PORT_CNT;
364 	us_ibdev->ib_dev.num_comp_vectors = USNIC_IB_NUM_COMP_VECTORS;
365 	us_ibdev->ib_dev.dev.parent = &dev->dev;
366 	us_ibdev->ib_dev.uverbs_abi_ver = USNIC_UVERBS_ABI_VERSION;
367 	strlcpy(us_ibdev->ib_dev.name, "usnic_%d", IB_DEVICE_NAME_MAX);
368 
369 	us_ibdev->ib_dev.uverbs_cmd_mask =
370 		(1ull << IB_USER_VERBS_CMD_GET_CONTEXT) |
371 		(1ull << IB_USER_VERBS_CMD_QUERY_DEVICE) |
372 		(1ull << IB_USER_VERBS_CMD_QUERY_PORT) |
373 		(1ull << IB_USER_VERBS_CMD_ALLOC_PD) |
374 		(1ull << IB_USER_VERBS_CMD_DEALLOC_PD) |
375 		(1ull << IB_USER_VERBS_CMD_REG_MR) |
376 		(1ull << IB_USER_VERBS_CMD_DEREG_MR) |
377 		(1ull << IB_USER_VERBS_CMD_CREATE_COMP_CHANNEL) |
378 		(1ull << IB_USER_VERBS_CMD_CREATE_CQ) |
379 		(1ull << IB_USER_VERBS_CMD_DESTROY_CQ) |
380 		(1ull << IB_USER_VERBS_CMD_CREATE_QP) |
381 		(1ull << IB_USER_VERBS_CMD_MODIFY_QP) |
382 		(1ull << IB_USER_VERBS_CMD_QUERY_QP) |
383 		(1ull << IB_USER_VERBS_CMD_DESTROY_QP) |
384 		(1ull << IB_USER_VERBS_CMD_ATTACH_MCAST) |
385 		(1ull << IB_USER_VERBS_CMD_DETACH_MCAST) |
386 		(1ull << IB_USER_VERBS_CMD_OPEN_QP);
387 
388 	us_ibdev->ib_dev.query_device = usnic_ib_query_device;
389 	us_ibdev->ib_dev.query_port = usnic_ib_query_port;
390 	us_ibdev->ib_dev.query_pkey = usnic_ib_query_pkey;
391 	us_ibdev->ib_dev.query_gid = usnic_ib_query_gid;
392 	us_ibdev->ib_dev.get_netdev = usnic_get_netdev;
393 	us_ibdev->ib_dev.get_link_layer = usnic_ib_port_link_layer;
394 	us_ibdev->ib_dev.alloc_pd = usnic_ib_alloc_pd;
395 	us_ibdev->ib_dev.dealloc_pd = usnic_ib_dealloc_pd;
396 	us_ibdev->ib_dev.create_qp = usnic_ib_create_qp;
397 	us_ibdev->ib_dev.modify_qp = usnic_ib_modify_qp;
398 	us_ibdev->ib_dev.query_qp = usnic_ib_query_qp;
399 	us_ibdev->ib_dev.destroy_qp = usnic_ib_destroy_qp;
400 	us_ibdev->ib_dev.create_cq = usnic_ib_create_cq;
401 	us_ibdev->ib_dev.destroy_cq = usnic_ib_destroy_cq;
402 	us_ibdev->ib_dev.reg_user_mr = usnic_ib_reg_mr;
403 	us_ibdev->ib_dev.dereg_mr = usnic_ib_dereg_mr;
404 	us_ibdev->ib_dev.alloc_ucontext = usnic_ib_alloc_ucontext;
405 	us_ibdev->ib_dev.dealloc_ucontext = usnic_ib_dealloc_ucontext;
406 	us_ibdev->ib_dev.mmap = usnic_ib_mmap;
407 	us_ibdev->ib_dev.create_ah = usnic_ib_create_ah;
408 	us_ibdev->ib_dev.destroy_ah = usnic_ib_destroy_ah;
409 	us_ibdev->ib_dev.post_send = usnic_ib_post_send;
410 	us_ibdev->ib_dev.post_recv = usnic_ib_post_recv;
411 	us_ibdev->ib_dev.poll_cq = usnic_ib_poll_cq;
412 	us_ibdev->ib_dev.req_notify_cq = usnic_ib_req_notify_cq;
413 	us_ibdev->ib_dev.get_dma_mr = usnic_ib_get_dma_mr;
414 	us_ibdev->ib_dev.get_port_immutable = usnic_port_immutable;
415 	us_ibdev->ib_dev.get_dev_fw_str     = usnic_get_dev_fw_str;
416 
417 
418 	us_ibdev->ib_dev.driver_id = RDMA_DRIVER_USNIC;
419 	if (ib_register_device(&us_ibdev->ib_dev, NULL))
420 		goto err_fwd_dealloc;
421 
422 	usnic_fwd_set_mtu(us_ibdev->ufdev, us_ibdev->netdev->mtu);
423 	usnic_fwd_set_mac(us_ibdev->ufdev, us_ibdev->netdev->dev_addr);
424 	if (netif_carrier_ok(us_ibdev->netdev))
425 		usnic_fwd_carrier_up(us_ibdev->ufdev);
426 
427 	ind = in_dev_get(netdev);
428 	if (ind->ifa_list)
429 		usnic_fwd_add_ipaddr(us_ibdev->ufdev,
430 				     ind->ifa_list->ifa_address);
431 	in_dev_put(ind);
432 
433 	usnic_mac_ip_to_gid(us_ibdev->netdev->perm_addr,
434 				us_ibdev->ufdev->inaddr, &gid.raw[0]);
435 	memcpy(&us_ibdev->ib_dev.node_guid, &gid.global.interface_id,
436 		sizeof(gid.global.interface_id));
437 	kref_init(&us_ibdev->vf_cnt);
438 
439 	usnic_info("Added ibdev: %s netdev: %s with mac %pM Link: %u MTU: %u\n",
440 			us_ibdev->ib_dev.name, netdev_name(us_ibdev->netdev),
441 			us_ibdev->ufdev->mac, us_ibdev->ufdev->link_up,
442 			us_ibdev->ufdev->mtu);
443 	return us_ibdev;
444 
445 err_fwd_dealloc:
446 	usnic_fwd_dev_free(us_ibdev->ufdev);
447 err_dealloc:
448 	usnic_err("failed -- deallocing device\n");
449 	ib_dealloc_device(&us_ibdev->ib_dev);
450 	return NULL;
451 }
452 
453 static void usnic_ib_device_remove(struct usnic_ib_dev *us_ibdev)
454 {
455 	usnic_info("Unregistering %s\n", us_ibdev->ib_dev.name);
456 	usnic_ib_sysfs_unregister_usdev(us_ibdev);
457 	usnic_fwd_dev_free(us_ibdev->ufdev);
458 	ib_unregister_device(&us_ibdev->ib_dev);
459 	ib_dealloc_device(&us_ibdev->ib_dev);
460 }
461 
462 static void usnic_ib_undiscover_pf(struct kref *kref)
463 {
464 	struct usnic_ib_dev *us_ibdev, *tmp;
465 	struct pci_dev *dev;
466 	bool found = false;
467 
468 	dev = container_of(kref, struct usnic_ib_dev, vf_cnt)->pdev;
469 	mutex_lock(&usnic_ib_ibdev_list_lock);
470 	list_for_each_entry_safe(us_ibdev, tmp,
471 				&usnic_ib_ibdev_list, ib_dev_link) {
472 		if (us_ibdev->pdev == dev) {
473 			list_del(&us_ibdev->ib_dev_link);
474 			usnic_ib_device_remove(us_ibdev);
475 			found = true;
476 			break;
477 		}
478 	}
479 
480 	WARN(!found, "Failed to remove PF %s\n", pci_name(dev));
481 
482 	mutex_unlock(&usnic_ib_ibdev_list_lock);
483 }
484 
485 static struct usnic_ib_dev *usnic_ib_discover_pf(struct usnic_vnic *vnic)
486 {
487 	struct usnic_ib_dev *us_ibdev;
488 	struct pci_dev *parent_pci, *vf_pci;
489 	int err;
490 
491 	vf_pci = usnic_vnic_get_pdev(vnic);
492 	parent_pci = pci_physfn(vf_pci);
493 
494 	BUG_ON(!parent_pci);
495 
496 	mutex_lock(&usnic_ib_ibdev_list_lock);
497 	list_for_each_entry(us_ibdev, &usnic_ib_ibdev_list, ib_dev_link) {
498 		if (us_ibdev->pdev == parent_pci) {
499 			kref_get(&us_ibdev->vf_cnt);
500 			goto out;
501 		}
502 	}
503 
504 	us_ibdev = usnic_ib_device_add(parent_pci);
505 	if (IS_ERR_OR_NULL(us_ibdev)) {
506 		us_ibdev = us_ibdev ? us_ibdev : ERR_PTR(-EFAULT);
507 		goto out;
508 	}
509 
510 	err = usnic_ib_sysfs_register_usdev(us_ibdev);
511 	if (err) {
512 		usnic_ib_device_remove(us_ibdev);
513 		us_ibdev = ERR_PTR(err);
514 		goto out;
515 	}
516 
517 	list_add(&us_ibdev->ib_dev_link, &usnic_ib_ibdev_list);
518 out:
519 	mutex_unlock(&usnic_ib_ibdev_list_lock);
520 	return us_ibdev;
521 }
522 /* End of PF discovery section */
523 
524 /* Start of PCI section */
525 
526 static const struct pci_device_id usnic_ib_pci_ids[] = {
527 	{PCI_DEVICE(PCI_VENDOR_ID_CISCO, PCI_DEVICE_ID_CISCO_VIC_USPACE_NIC)},
528 	{0,}
529 };
530 
531 static int usnic_ib_pci_probe(struct pci_dev *pdev,
532 				const struct pci_device_id *id)
533 {
534 	int err;
535 	struct usnic_ib_dev *pf;
536 	struct usnic_ib_vf *vf;
537 	enum usnic_vnic_res_type res_type;
538 
539 	vf = kzalloc(sizeof(*vf), GFP_KERNEL);
540 	if (!vf)
541 		return -ENOMEM;
542 
543 	err = pci_enable_device(pdev);
544 	if (err) {
545 		usnic_err("Failed to enable %s with err %d\n",
546 				pci_name(pdev), err);
547 		goto out_clean_vf;
548 	}
549 
550 	err = pci_request_regions(pdev, DRV_NAME);
551 	if (err) {
552 		usnic_err("Failed to request region for %s with err %d\n",
553 				pci_name(pdev), err);
554 		goto out_disable_device;
555 	}
556 
557 	pci_set_master(pdev);
558 	pci_set_drvdata(pdev, vf);
559 
560 	vf->vnic = usnic_vnic_alloc(pdev);
561 	if (IS_ERR_OR_NULL(vf->vnic)) {
562 		err = vf->vnic ? PTR_ERR(vf->vnic) : -ENOMEM;
563 		usnic_err("Failed to alloc vnic for %s with err %d\n",
564 				pci_name(pdev), err);
565 		goto out_release_regions;
566 	}
567 
568 	pf = usnic_ib_discover_pf(vf->vnic);
569 	if (IS_ERR_OR_NULL(pf)) {
570 		usnic_err("Failed to discover pf of vnic %s with err%ld\n",
571 				pci_name(pdev), PTR_ERR(pf));
572 		err = pf ? PTR_ERR(pf) : -EFAULT;
573 		goto out_clean_vnic;
574 	}
575 
576 	vf->pf = pf;
577 	spin_lock_init(&vf->lock);
578 	mutex_lock(&pf->usdev_lock);
579 	list_add_tail(&vf->link, &pf->vf_dev_list);
580 	/*
581 	 * Save max settings (will be same for each VF, easier to re-write than
582 	 * to say "if (!set) { set_values(); set=1; }
583 	 */
584 	for (res_type = USNIC_VNIC_RES_TYPE_EOL+1;
585 			res_type < USNIC_VNIC_RES_TYPE_MAX;
586 			res_type++) {
587 		pf->vf_res_cnt[res_type] = usnic_vnic_res_cnt(vf->vnic,
588 								res_type);
589 	}
590 
591 	mutex_unlock(&pf->usdev_lock);
592 
593 	usnic_info("Registering usnic VF %s into PF %s\n", pci_name(pdev),
594 			pf->ib_dev.name);
595 	usnic_ib_log_vf(vf);
596 	return 0;
597 
598 out_clean_vnic:
599 	usnic_vnic_free(vf->vnic);
600 out_release_regions:
601 	pci_set_drvdata(pdev, NULL);
602 	pci_clear_master(pdev);
603 	pci_release_regions(pdev);
604 out_disable_device:
605 	pci_disable_device(pdev);
606 out_clean_vf:
607 	kfree(vf);
608 	return err;
609 }
610 
611 static void usnic_ib_pci_remove(struct pci_dev *pdev)
612 {
613 	struct usnic_ib_vf *vf = pci_get_drvdata(pdev);
614 	struct usnic_ib_dev *pf = vf->pf;
615 
616 	mutex_lock(&pf->usdev_lock);
617 	list_del(&vf->link);
618 	mutex_unlock(&pf->usdev_lock);
619 
620 	kref_put(&pf->vf_cnt, usnic_ib_undiscover_pf);
621 	usnic_vnic_free(vf->vnic);
622 	pci_set_drvdata(pdev, NULL);
623 	pci_clear_master(pdev);
624 	pci_release_regions(pdev);
625 	pci_disable_device(pdev);
626 	kfree(vf);
627 
628 	usnic_info("Removed VF %s\n", pci_name(pdev));
629 }
630 
631 /* PCI driver entry points */
632 static struct pci_driver usnic_ib_pci_driver = {
633 	.name = DRV_NAME,
634 	.id_table = usnic_ib_pci_ids,
635 	.probe = usnic_ib_pci_probe,
636 	.remove = usnic_ib_pci_remove,
637 };
638 /* End of PCI section */
639 
640 /* Start of module section */
641 static int __init usnic_ib_init(void)
642 {
643 	int err;
644 
645 	printk_once(KERN_INFO "%s", usnic_version);
646 
647 	err = usnic_uiom_init(DRV_NAME);
648 	if (err) {
649 		usnic_err("Unable to initalize umem with err %d\n", err);
650 		return err;
651 	}
652 
653 	err = pci_register_driver(&usnic_ib_pci_driver);
654 	if (err) {
655 		usnic_err("Unable to register with PCI\n");
656 		goto out_umem_fini;
657 	}
658 
659 	err = register_netdevice_notifier(&usnic_ib_netdevice_notifier);
660 	if (err) {
661 		usnic_err("Failed to register netdev notifier\n");
662 		goto out_pci_unreg;
663 	}
664 
665 	err = register_inetaddr_notifier(&usnic_ib_inetaddr_notifier);
666 	if (err) {
667 		usnic_err("Failed to register inet addr notifier\n");
668 		goto out_unreg_netdev_notifier;
669 	}
670 
671 	err = usnic_transport_init();
672 	if (err) {
673 		usnic_err("Failed to initialize transport\n");
674 		goto out_unreg_inetaddr_notifier;
675 	}
676 
677 	usnic_debugfs_init();
678 
679 	return 0;
680 
681 out_unreg_inetaddr_notifier:
682 	unregister_inetaddr_notifier(&usnic_ib_inetaddr_notifier);
683 out_unreg_netdev_notifier:
684 	unregister_netdevice_notifier(&usnic_ib_netdevice_notifier);
685 out_pci_unreg:
686 	pci_unregister_driver(&usnic_ib_pci_driver);
687 out_umem_fini:
688 	usnic_uiom_fini();
689 
690 	return err;
691 }
692 
693 static void __exit usnic_ib_destroy(void)
694 {
695 	usnic_dbg("\n");
696 	usnic_debugfs_exit();
697 	usnic_transport_fini();
698 	unregister_inetaddr_notifier(&usnic_ib_inetaddr_notifier);
699 	unregister_netdevice_notifier(&usnic_ib_netdevice_notifier);
700 	pci_unregister_driver(&usnic_ib_pci_driver);
701 	usnic_uiom_fini();
702 }
703 
704 MODULE_DESCRIPTION("Cisco VIC (usNIC) Verbs Driver");
705 MODULE_AUTHOR("Upinder Malhi <umalhi@cisco.com>");
706 MODULE_LICENSE("Dual BSD/GPL");
707 module_param(usnic_log_lvl, uint, S_IRUGO | S_IWUSR);
708 module_param(usnic_ib_share_vf, uint, S_IRUGO | S_IWUSR);
709 MODULE_PARM_DESC(usnic_log_lvl, " Off=0, Err=1, Info=2, Debug=3");
710 MODULE_PARM_DESC(usnic_ib_share_vf, "Off=0, On=1 VF sharing amongst QPs");
711 MODULE_DEVICE_TABLE(pci, usnic_ib_pci_ids);
712 
713 module_init(usnic_ib_init);
714 module_exit(usnic_ib_destroy);
715 /* End of module section */
716