blob: aed1ca390e305925c9c49ee6901fed0de7b6f0c4 [file] [log] [blame]
Upinder Malhie3cf00d2013-09-10 03:38:16 +00001/*
2 * Copyright (c) 2013, Cisco Systems, Inc. All rights reserved.
3 *
Jeff Squyres3805ead2015-09-30 13:34:00 -07004 * 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.
Upinder Malhie3cf00d2013-09-10 03:38:16 +000022 *
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>
Upinder Malhic7845bc2014-01-09 14:48:17 -080043#include <linux/inetdevice.h>
Upinder Malhie3cf00d2013-09-10 03:38:16 +000044#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
65unsigned int usnic_log_lvl = USNIC_LOG_LVL_ERR;
66unsigned int usnic_ib_share_vf = 1;
67
68static const char usnic_version[] =
69 DRV_NAME ": Cisco VIC (USNIC) Verbs Driver v"
70 DRV_VERSION " (" DRV_RELDATE ")\n";
71
72static DEFINE_MUTEX(usnic_ib_ibdev_list_lock);
73static LIST_HEAD(usnic_ib_ibdev_list);
74
75/* Callback dump funcs */
76static 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
83static 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
90void 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 */
98static inline const char *usnic_ib_netdev_event_to_string(unsigned long event)
99{
100 const char *event2str[] = {"NETDEV_NONE", "NETDEV_UP", "NETDEV_DOWN",
101 "NETDEV_REBOOT", "NETDEV_CHANGE",
102 "NETDEV_REGISTER", "NETDEV_UNREGISTER", "NETDEV_CHANGEMTU",
103 "NETDEV_CHANGEADDR", "NETDEV_GOING_DOWN", "NETDEV_FEAT_CHANGE",
104 "NETDEV_BONDING_FAILOVER", "NETDEV_PRE_UP",
105 "NETDEV_PRE_TYPE_CHANGE", "NETDEV_POST_TYPE_CHANGE",
106 "NETDEV_POST_INT", "NETDEV_UNREGISTER_FINAL", "NETDEV_RELEASE",
107 "NETDEV_NOTIFY_PEERS", "NETDEV_JOIN"
108 };
109
110 if (event >= ARRAY_SIZE(event2str))
111 return "UNKNOWN_NETDEV_EVENT";
112 else
113 return event2str[event];
114}
115
116static void usnic_ib_qp_grp_modify_active_to_err(struct usnic_ib_dev *us_ibdev)
117{
118 struct usnic_ib_ucontext *ctx;
119 struct usnic_ib_qp_grp *qp_grp;
120 enum ib_qp_state cur_state;
121 int status;
122
123 BUG_ON(!mutex_is_locked(&us_ibdev->usdev_lock));
124
125 list_for_each_entry(ctx, &us_ibdev->ctx_list, link) {
126 list_for_each_entry(qp_grp, &ctx->qp_grp_list, link) {
127 cur_state = qp_grp->state;
128 if (cur_state == IB_QPS_INIT ||
129 cur_state == IB_QPS_RTR ||
130 cur_state == IB_QPS_RTS) {
131 status = usnic_ib_qp_grp_modify(qp_grp,
132 IB_QPS_ERR,
133 NULL);
134 if (status) {
135 usnic_err("Failed to transistion qp grp %u from %s to %s\n",
136 qp_grp->grp_id,
137 usnic_ib_qp_grp_state_to_string
138 (cur_state),
139 usnic_ib_qp_grp_state_to_string
140 (IB_QPS_ERR));
141 }
142 }
143 }
144 }
145}
146
147static void usnic_ib_handle_usdev_event(struct usnic_ib_dev *us_ibdev,
148 unsigned long event)
149{
150 struct net_device *netdev;
151 struct ib_event ib_event;
152
153 memset(&ib_event, 0, sizeof(ib_event));
154
155 mutex_lock(&us_ibdev->usdev_lock);
156 netdev = us_ibdev->netdev;
157 switch (event) {
158 case NETDEV_REBOOT:
159 usnic_info("PF Reset on %s\n", us_ibdev->ib_dev.name);
160 usnic_ib_qp_grp_modify_active_to_err(us_ibdev);
161 ib_event.event = IB_EVENT_PORT_ERR;
162 ib_event.device = &us_ibdev->ib_dev;
163 ib_event.element.port_num = 1;
164 ib_dispatch_event(&ib_event);
165 break;
166 case NETDEV_UP:
167 case NETDEV_DOWN:
168 case NETDEV_CHANGE:
Upinder Malhi8af94ac2014-01-09 14:48:08 -0800169 if (!us_ibdev->ufdev->link_up &&
170 netif_carrier_ok(netdev)) {
171 usnic_fwd_carrier_up(us_ibdev->ufdev);
Upinder Malhie3cf00d2013-09-10 03:38:16 +0000172 usnic_info("Link UP on %s\n", us_ibdev->ib_dev.name);
173 ib_event.event = IB_EVENT_PORT_ACTIVE;
174 ib_event.device = &us_ibdev->ib_dev;
175 ib_event.element.port_num = 1;
176 ib_dispatch_event(&ib_event);
Upinder Malhi8af94ac2014-01-09 14:48:08 -0800177 } else if (us_ibdev->ufdev->link_up &&
178 !netif_carrier_ok(netdev)) {
179 usnic_fwd_carrier_down(us_ibdev->ufdev);
Upinder Malhie3cf00d2013-09-10 03:38:16 +0000180 usnic_info("Link DOWN on %s\n", us_ibdev->ib_dev.name);
181 usnic_ib_qp_grp_modify_active_to_err(us_ibdev);
182 ib_event.event = IB_EVENT_PORT_ERR;
183 ib_event.device = &us_ibdev->ib_dev;
184 ib_event.element.port_num = 1;
185 ib_dispatch_event(&ib_event);
186 } else {
Roland Dreierc30392a2014-01-13 13:12:48 -0800187 usnic_dbg("Ignoring %s on %s\n",
Upinder Malhie3cf00d2013-09-10 03:38:16 +0000188 usnic_ib_netdev_event_to_string(event),
189 us_ibdev->ib_dev.name);
190 }
191 break;
192 case NETDEV_CHANGEADDR:
Upinder Malhi8af94ac2014-01-09 14:48:08 -0800193 if (!memcmp(us_ibdev->ufdev->mac, netdev->dev_addr,
194 sizeof(us_ibdev->ufdev->mac))) {
Roland Dreierc30392a2014-01-13 13:12:48 -0800195 usnic_dbg("Ignoring addr change on %s\n",
Upinder Malhie3cf00d2013-09-10 03:38:16 +0000196 us_ibdev->ib_dev.name);
197 } else {
198 usnic_info(" %s old mac: %pM new mac: %pM\n",
199 us_ibdev->ib_dev.name,
Upinder Malhi8af94ac2014-01-09 14:48:08 -0800200 us_ibdev->ufdev->mac,
Upinder Malhie3cf00d2013-09-10 03:38:16 +0000201 netdev->dev_addr);
Upinder Malhi8af94ac2014-01-09 14:48:08 -0800202 usnic_fwd_set_mac(us_ibdev->ufdev, netdev->dev_addr);
Upinder Malhie3cf00d2013-09-10 03:38:16 +0000203 usnic_ib_qp_grp_modify_active_to_err(us_ibdev);
204 ib_event.event = IB_EVENT_GID_CHANGE;
205 ib_event.device = &us_ibdev->ib_dev;
206 ib_event.element.port_num = 1;
207 ib_dispatch_event(&ib_event);
208 }
209
210 break;
211 case NETDEV_CHANGEMTU:
Upinder Malhi8af94ac2014-01-09 14:48:08 -0800212 if (us_ibdev->ufdev->mtu != netdev->mtu) {
Upinder Malhie3cf00d2013-09-10 03:38:16 +0000213 usnic_info("MTU Change on %s old: %u new: %u\n",
214 us_ibdev->ib_dev.name,
Upinder Malhi8af94ac2014-01-09 14:48:08 -0800215 us_ibdev->ufdev->mtu, netdev->mtu);
216 usnic_fwd_set_mtu(us_ibdev->ufdev, netdev->mtu);
Upinder Malhie3cf00d2013-09-10 03:38:16 +0000217 usnic_ib_qp_grp_modify_active_to_err(us_ibdev);
218 } else {
219 usnic_dbg("Ignoring MTU change on %s\n",
220 us_ibdev->ib_dev.name);
221 }
222 break;
223 default:
Roland Dreierc30392a2014-01-13 13:12:48 -0800224 usnic_dbg("Ignoring event %s on %s",
Upinder Malhie3cf00d2013-09-10 03:38:16 +0000225 usnic_ib_netdev_event_to_string(event),
226 us_ibdev->ib_dev.name);
227 }
228 mutex_unlock(&us_ibdev->usdev_lock);
229}
230
231static int usnic_ib_netdevice_event(struct notifier_block *notifier,
232 unsigned long event, void *ptr)
233{
234 struct usnic_ib_dev *us_ibdev;
235
236 struct net_device *netdev = netdev_notifier_info_to_dev(ptr);
237
238 mutex_lock(&usnic_ib_ibdev_list_lock);
239 list_for_each_entry(us_ibdev, &usnic_ib_ibdev_list, ib_dev_link) {
240 if (us_ibdev->netdev == netdev) {
241 usnic_ib_handle_usdev_event(us_ibdev, event);
242 break;
243 }
244 }
245 mutex_unlock(&usnic_ib_ibdev_list_lock);
246
247 return NOTIFY_DONE;
248}
249
250static struct notifier_block usnic_ib_netdevice_notifier = {
251 .notifier_call = usnic_ib_netdevice_event
252};
253/* End of netdev section */
254
Upinder Malhic7845bc2014-01-09 14:48:17 -0800255/* Start of inet section */
256static int usnic_ib_handle_inet_event(struct usnic_ib_dev *us_ibdev,
257 unsigned long event, void *ptr)
258{
259 struct in_ifaddr *ifa = ptr;
260 struct ib_event ib_event;
261
262 mutex_lock(&us_ibdev->usdev_lock);
263
264 switch (event) {
265 case NETDEV_DOWN:
266 usnic_info("%s via ip notifiers",
267 usnic_ib_netdev_event_to_string(event));
268 usnic_fwd_del_ipaddr(us_ibdev->ufdev);
269 usnic_ib_qp_grp_modify_active_to_err(us_ibdev);
270 ib_event.event = IB_EVENT_GID_CHANGE;
271 ib_event.device = &us_ibdev->ib_dev;
272 ib_event.element.port_num = 1;
273 ib_dispatch_event(&ib_event);
274 break;
275 case NETDEV_UP:
276 usnic_fwd_add_ipaddr(us_ibdev->ufdev, ifa->ifa_address);
277 usnic_info("%s via ip notifiers: ip %pI4",
278 usnic_ib_netdev_event_to_string(event),
279 &us_ibdev->ufdev->inaddr);
280 ib_event.event = IB_EVENT_GID_CHANGE;
281 ib_event.device = &us_ibdev->ib_dev;
282 ib_event.element.port_num = 1;
283 ib_dispatch_event(&ib_event);
284 break;
285 default:
Roland Dreierc30392a2014-01-13 13:12:48 -0800286 usnic_info("Ignoring event %s on %s",
Upinder Malhic7845bc2014-01-09 14:48:17 -0800287 usnic_ib_netdev_event_to_string(event),
288 us_ibdev->ib_dev.name);
289 }
290 mutex_unlock(&us_ibdev->usdev_lock);
291
292 return NOTIFY_DONE;
293}
294
295static int usnic_ib_inetaddr_event(struct notifier_block *notifier,
296 unsigned long event, void *ptr)
297{
298 struct usnic_ib_dev *us_ibdev;
299 struct in_ifaddr *ifa = ptr;
300 struct net_device *netdev = ifa->ifa_dev->dev;
301
302 mutex_lock(&usnic_ib_ibdev_list_lock);
303 list_for_each_entry(us_ibdev, &usnic_ib_ibdev_list, ib_dev_link) {
304 if (us_ibdev->netdev == netdev) {
305 usnic_ib_handle_inet_event(us_ibdev, event, ptr);
306 break;
307 }
308 }
309 mutex_unlock(&usnic_ib_ibdev_list_lock);
310
311 return NOTIFY_DONE;
312}
313static struct notifier_block usnic_ib_inetaddr_notifier = {
314 .notifier_call = usnic_ib_inetaddr_event
315};
316/* End of inet section*/
317
Ira Weiny77386132015-05-13 20:02:58 -0400318static int usnic_port_immutable(struct ib_device *ibdev, u8 port_num,
319 struct ib_port_immutable *immutable)
320{
321 struct ib_port_attr attr;
322 int err;
323
Or Gerlitzc4550c62017-01-24 13:02:39 +0200324 immutable->core_cap_flags = RDMA_CORE_PORT_USNIC;
325
326 err = ib_query_port(ibdev, port_num, &attr);
Ira Weiny77386132015-05-13 20:02:58 -0400327 if (err)
328 return err;
329
330 immutable->pkey_tbl_len = attr.pkey_tbl_len;
331 immutable->gid_tbl_len = attr.gid_tbl_len;
332
333 return 0;
334}
335
Leon Romanovsky9abb0d12017-06-27 16:49:53 +0300336static void usnic_get_dev_fw_str(struct ib_device *device, char *str)
Ira Weiny15453e82016-06-15 02:22:05 -0400337{
338 struct usnic_ib_dev *us_ibdev =
339 container_of(device, struct usnic_ib_dev, ib_dev);
340 struct ethtool_drvinfo info;
341
342 mutex_lock(&us_ibdev->usdev_lock);
343 us_ibdev->netdev->ethtool_ops->get_drvinfo(us_ibdev->netdev, &info);
344 mutex_unlock(&us_ibdev->usdev_lock);
345
Leon Romanovsky9abb0d12017-06-27 16:49:53 +0300346 snprintf(str, IB_FW_VERSION_NAME_MAX, "%s", info.fw_version);
Ira Weiny15453e82016-06-15 02:22:05 -0400347}
348
Upinder Malhie3cf00d2013-09-10 03:38:16 +0000349/* Start of PF discovery section */
350static void *usnic_ib_device_add(struct pci_dev *dev)
351{
352 struct usnic_ib_dev *us_ibdev;
353 union ib_gid gid;
Leon Romanovsky5d50f402017-08-17 15:50:41 +0300354 struct in_device *ind;
Upinder Malhic7845bc2014-01-09 14:48:17 -0800355 struct net_device *netdev;
Upinder Malhie3cf00d2013-09-10 03:38:16 +0000356
357 usnic_dbg("\n");
Upinder Malhic7845bc2014-01-09 14:48:17 -0800358 netdev = pci_get_drvdata(dev);
Upinder Malhie3cf00d2013-09-10 03:38:16 +0000359
360 us_ibdev = (struct usnic_ib_dev *)ib_alloc_device(sizeof(*us_ibdev));
Insu Yun2c79dad2015-10-15 21:15:15 +0000361 if (!us_ibdev) {
Upinder Malhie3cf00d2013-09-10 03:38:16 +0000362 usnic_err("Device %s context alloc failed\n",
363 netdev_name(pci_get_drvdata(dev)));
Insu Yun2c79dad2015-10-15 21:15:15 +0000364 return ERR_PTR(-EFAULT);
Upinder Malhie3cf00d2013-09-10 03:38:16 +0000365 }
366
367 us_ibdev->ufdev = usnic_fwd_dev_alloc(dev);
Insu Yun2c79dad2015-10-15 21:15:15 +0000368 if (!us_ibdev->ufdev) {
369 usnic_err("Failed to alloc ufdev for %s\n", pci_name(dev));
Upinder Malhie3cf00d2013-09-10 03:38:16 +0000370 goto err_dealloc;
371 }
372
373 mutex_init(&us_ibdev->usdev_lock);
374 INIT_LIST_HEAD(&us_ibdev->vf_dev_list);
375 INIT_LIST_HEAD(&us_ibdev->ctx_list);
376
377 us_ibdev->pdev = dev;
378 us_ibdev->netdev = pci_get_drvdata(dev);
379 us_ibdev->ib_dev.owner = THIS_MODULE;
Upinder Malhi61f78262014-01-15 17:02:37 -0800380 us_ibdev->ib_dev.node_type = RDMA_NODE_USNIC_UDP;
Upinder Malhie3cf00d2013-09-10 03:38:16 +0000381 us_ibdev->ib_dev.phys_port_cnt = USNIC_IB_PORT_CNT;
382 us_ibdev->ib_dev.num_comp_vectors = USNIC_IB_NUM_COMP_VECTORS;
Bart Van Assche6b06d522017-01-20 13:04:27 -0800383 us_ibdev->ib_dev.dev.parent = &dev->dev;
Upinder Malhie3cf00d2013-09-10 03:38:16 +0000384 us_ibdev->ib_dev.uverbs_abi_ver = USNIC_UVERBS_ABI_VERSION;
385 strlcpy(us_ibdev->ib_dev.name, "usnic_%d", IB_DEVICE_NAME_MAX);
386
387 us_ibdev->ib_dev.uverbs_cmd_mask =
388 (1ull << IB_USER_VERBS_CMD_GET_CONTEXT) |
389 (1ull << IB_USER_VERBS_CMD_QUERY_DEVICE) |
390 (1ull << IB_USER_VERBS_CMD_QUERY_PORT) |
391 (1ull << IB_USER_VERBS_CMD_ALLOC_PD) |
392 (1ull << IB_USER_VERBS_CMD_DEALLOC_PD) |
393 (1ull << IB_USER_VERBS_CMD_REG_MR) |
394 (1ull << IB_USER_VERBS_CMD_DEREG_MR) |
395 (1ull << IB_USER_VERBS_CMD_CREATE_COMP_CHANNEL) |
396 (1ull << IB_USER_VERBS_CMD_CREATE_CQ) |
397 (1ull << IB_USER_VERBS_CMD_DESTROY_CQ) |
398 (1ull << IB_USER_VERBS_CMD_CREATE_QP) |
399 (1ull << IB_USER_VERBS_CMD_MODIFY_QP) |
400 (1ull << IB_USER_VERBS_CMD_QUERY_QP) |
401 (1ull << IB_USER_VERBS_CMD_DESTROY_QP) |
402 (1ull << IB_USER_VERBS_CMD_ATTACH_MCAST) |
403 (1ull << IB_USER_VERBS_CMD_DETACH_MCAST) |
404 (1ull << IB_USER_VERBS_CMD_OPEN_QP);
405
406 us_ibdev->ib_dev.query_device = usnic_ib_query_device;
407 us_ibdev->ib_dev.query_port = usnic_ib_query_port;
408 us_ibdev->ib_dev.query_pkey = usnic_ib_query_pkey;
409 us_ibdev->ib_dev.query_gid = usnic_ib_query_gid;
Yuval Shaia44b0b742017-06-14 23:13:33 +0300410 us_ibdev->ib_dev.get_netdev = usnic_get_netdev;
Upinder Malhie3cf00d2013-09-10 03:38:16 +0000411 us_ibdev->ib_dev.get_link_layer = usnic_ib_port_link_layer;
412 us_ibdev->ib_dev.alloc_pd = usnic_ib_alloc_pd;
413 us_ibdev->ib_dev.dealloc_pd = usnic_ib_dealloc_pd;
414 us_ibdev->ib_dev.create_qp = usnic_ib_create_qp;
415 us_ibdev->ib_dev.modify_qp = usnic_ib_modify_qp;
416 us_ibdev->ib_dev.query_qp = usnic_ib_query_qp;
417 us_ibdev->ib_dev.destroy_qp = usnic_ib_destroy_qp;
418 us_ibdev->ib_dev.create_cq = usnic_ib_create_cq;
419 us_ibdev->ib_dev.destroy_cq = usnic_ib_destroy_cq;
420 us_ibdev->ib_dev.reg_user_mr = usnic_ib_reg_mr;
421 us_ibdev->ib_dev.dereg_mr = usnic_ib_dereg_mr;
422 us_ibdev->ib_dev.alloc_ucontext = usnic_ib_alloc_ucontext;
423 us_ibdev->ib_dev.dealloc_ucontext = usnic_ib_dealloc_ucontext;
424 us_ibdev->ib_dev.mmap = usnic_ib_mmap;
425 us_ibdev->ib_dev.create_ah = usnic_ib_create_ah;
426 us_ibdev->ib_dev.destroy_ah = usnic_ib_destroy_ah;
427 us_ibdev->ib_dev.post_send = usnic_ib_post_send;
428 us_ibdev->ib_dev.post_recv = usnic_ib_post_recv;
429 us_ibdev->ib_dev.poll_cq = usnic_ib_poll_cq;
430 us_ibdev->ib_dev.req_notify_cq = usnic_ib_req_notify_cq;
431 us_ibdev->ib_dev.get_dma_mr = usnic_ib_get_dma_mr;
Ira Weiny77386132015-05-13 20:02:58 -0400432 us_ibdev->ib_dev.get_port_immutable = usnic_port_immutable;
Ira Weiny15453e82016-06-15 02:22:05 -0400433 us_ibdev->ib_dev.get_dev_fw_str = usnic_get_dev_fw_str;
Upinder Malhie3cf00d2013-09-10 03:38:16 +0000434
435
Matan Barak0ede73b2018-03-19 15:02:34 +0200436 us_ibdev->ib_dev.driver_id = RDMA_DRIVER_USNIC;
Upinder Malhie3cf00d2013-09-10 03:38:16 +0000437 if (ib_register_device(&us_ibdev->ib_dev, NULL))
438 goto err_fwd_dealloc;
439
Upinder Malhi8af94ac2014-01-09 14:48:08 -0800440 usnic_fwd_set_mtu(us_ibdev->ufdev, us_ibdev->netdev->mtu);
441 usnic_fwd_set_mac(us_ibdev->ufdev, us_ibdev->netdev->dev_addr);
442 if (netif_carrier_ok(us_ibdev->netdev))
443 usnic_fwd_carrier_up(us_ibdev->ufdev);
444
Leon Romanovsky5d50f402017-08-17 15:50:41 +0300445 ind = in_dev_get(netdev);
446 if (ind->ifa_list)
447 usnic_fwd_add_ipaddr(us_ibdev->ufdev,
448 ind->ifa_list->ifa_address);
449 in_dev_put(ind);
Upinder Malhic7845bc2014-01-09 14:48:17 -0800450
451 usnic_mac_ip_to_gid(us_ibdev->netdev->perm_addr,
452 us_ibdev->ufdev->inaddr, &gid.raw[0]);
Upinder Malhie3cf00d2013-09-10 03:38:16 +0000453 memcpy(&us_ibdev->ib_dev.node_guid, &gid.global.interface_id,
454 sizeof(gid.global.interface_id));
455 kref_init(&us_ibdev->vf_cnt);
456
457 usnic_info("Added ibdev: %s netdev: %s with mac %pM Link: %u MTU: %u\n",
458 us_ibdev->ib_dev.name, netdev_name(us_ibdev->netdev),
Upinder Malhi8af94ac2014-01-09 14:48:08 -0800459 us_ibdev->ufdev->mac, us_ibdev->ufdev->link_up,
460 us_ibdev->ufdev->mtu);
Upinder Malhie3cf00d2013-09-10 03:38:16 +0000461 return us_ibdev;
462
463err_fwd_dealloc:
464 usnic_fwd_dev_free(us_ibdev->ufdev);
465err_dealloc:
466 usnic_err("failed -- deallocing device\n");
467 ib_dealloc_device(&us_ibdev->ib_dev);
468 return NULL;
469}
470
471static void usnic_ib_device_remove(struct usnic_ib_dev *us_ibdev)
472{
473 usnic_info("Unregistering %s\n", us_ibdev->ib_dev.name);
474 usnic_ib_sysfs_unregister_usdev(us_ibdev);
475 usnic_fwd_dev_free(us_ibdev->ufdev);
476 ib_unregister_device(&us_ibdev->ib_dev);
477 ib_dealloc_device(&us_ibdev->ib_dev);
478}
479
480static void usnic_ib_undiscover_pf(struct kref *kref)
481{
482 struct usnic_ib_dev *us_ibdev, *tmp;
483 struct pci_dev *dev;
484 bool found = false;
485
486 dev = container_of(kref, struct usnic_ib_dev, vf_cnt)->pdev;
487 mutex_lock(&usnic_ib_ibdev_list_lock);
488 list_for_each_entry_safe(us_ibdev, tmp,
489 &usnic_ib_ibdev_list, ib_dev_link) {
490 if (us_ibdev->pdev == dev) {
491 list_del(&us_ibdev->ib_dev_link);
492 usnic_ib_device_remove(us_ibdev);
493 found = true;
494 break;
495 }
496 }
497
498 WARN(!found, "Failed to remove PF %s\n", pci_name(dev));
499
500 mutex_unlock(&usnic_ib_ibdev_list_lock);
501}
502
503static struct usnic_ib_dev *usnic_ib_discover_pf(struct usnic_vnic *vnic)
504{
505 struct usnic_ib_dev *us_ibdev;
506 struct pci_dev *parent_pci, *vf_pci;
507 int err;
508
509 vf_pci = usnic_vnic_get_pdev(vnic);
510 parent_pci = pci_physfn(vf_pci);
511
512 BUG_ON(!parent_pci);
513
514 mutex_lock(&usnic_ib_ibdev_list_lock);
515 list_for_each_entry(us_ibdev, &usnic_ib_ibdev_list, ib_dev_link) {
516 if (us_ibdev->pdev == parent_pci) {
517 kref_get(&us_ibdev->vf_cnt);
518 goto out;
519 }
520 }
521
522 us_ibdev = usnic_ib_device_add(parent_pci);
523 if (IS_ERR_OR_NULL(us_ibdev)) {
Upinder Malhi6a54d9f2014-01-09 14:48:36 -0800524 us_ibdev = us_ibdev ? us_ibdev : ERR_PTR(-EFAULT);
Upinder Malhie3cf00d2013-09-10 03:38:16 +0000525 goto out;
526 }
527
528 err = usnic_ib_sysfs_register_usdev(us_ibdev);
529 if (err) {
530 usnic_ib_device_remove(us_ibdev);
531 us_ibdev = ERR_PTR(err);
532 goto out;
533 }
534
535 list_add(&us_ibdev->ib_dev_link, &usnic_ib_ibdev_list);
536out:
537 mutex_unlock(&usnic_ib_ibdev_list_lock);
538 return us_ibdev;
539}
540/* End of PF discovery section */
541
542/* Start of PCI section */
543
Benoit Taine9baa3c32014-08-08 15:56:03 +0200544static const struct pci_device_id usnic_ib_pci_ids[] = {
Upinder Malhie3cf00d2013-09-10 03:38:16 +0000545 {PCI_DEVICE(PCI_VENDOR_ID_CISCO, PCI_DEVICE_ID_CISCO_VIC_USPACE_NIC)},
546 {0,}
547};
548
549static int usnic_ib_pci_probe(struct pci_dev *pdev,
550 const struct pci_device_id *id)
551{
552 int err;
553 struct usnic_ib_dev *pf;
554 struct usnic_ib_vf *vf;
555 enum usnic_vnic_res_type res_type;
556
557 vf = kzalloc(sizeof(*vf), GFP_KERNEL);
558 if (!vf)
559 return -ENOMEM;
560
561 err = pci_enable_device(pdev);
562 if (err) {
563 usnic_err("Failed to enable %s with err %d\n",
564 pci_name(pdev), err);
565 goto out_clean_vf;
566 }
567
568 err = pci_request_regions(pdev, DRV_NAME);
569 if (err) {
570 usnic_err("Failed to request region for %s with err %d\n",
571 pci_name(pdev), err);
572 goto out_disable_device;
573 }
574
575 pci_set_master(pdev);
576 pci_set_drvdata(pdev, vf);
577
578 vf->vnic = usnic_vnic_alloc(pdev);
579 if (IS_ERR_OR_NULL(vf->vnic)) {
Upinder Malhi6a54d9f2014-01-09 14:48:36 -0800580 err = vf->vnic ? PTR_ERR(vf->vnic) : -ENOMEM;
Upinder Malhie3cf00d2013-09-10 03:38:16 +0000581 usnic_err("Failed to alloc vnic for %s with err %d\n",
582 pci_name(pdev), err);
583 goto out_release_regions;
584 }
585
586 pf = usnic_ib_discover_pf(vf->vnic);
587 if (IS_ERR_OR_NULL(pf)) {
588 usnic_err("Failed to discover pf of vnic %s with err%ld\n",
589 pci_name(pdev), PTR_ERR(pf));
Upinder Malhi6a54d9f2014-01-09 14:48:36 -0800590 err = pf ? PTR_ERR(pf) : -EFAULT;
Upinder Malhie3cf00d2013-09-10 03:38:16 +0000591 goto out_clean_vnic;
592 }
593
594 vf->pf = pf;
595 spin_lock_init(&vf->lock);
596 mutex_lock(&pf->usdev_lock);
597 list_add_tail(&vf->link, &pf->vf_dev_list);
598 /*
599 * Save max settings (will be same for each VF, easier to re-write than
600 * to say "if (!set) { set_values(); set=1; }
601 */
602 for (res_type = USNIC_VNIC_RES_TYPE_EOL+1;
603 res_type < USNIC_VNIC_RES_TYPE_MAX;
604 res_type++) {
605 pf->vf_res_cnt[res_type] = usnic_vnic_res_cnt(vf->vnic,
606 res_type);
607 }
608
609 mutex_unlock(&pf->usdev_lock);
610
611 usnic_info("Registering usnic VF %s into PF %s\n", pci_name(pdev),
612 pf->ib_dev.name);
613 usnic_ib_log_vf(vf);
614 return 0;
615
616out_clean_vnic:
617 usnic_vnic_free(vf->vnic);
618out_release_regions:
619 pci_set_drvdata(pdev, NULL);
620 pci_clear_master(pdev);
621 pci_release_regions(pdev);
622out_disable_device:
623 pci_disable_device(pdev);
624out_clean_vf:
625 kfree(vf);
626 return err;
627}
628
629static void usnic_ib_pci_remove(struct pci_dev *pdev)
630{
631 struct usnic_ib_vf *vf = pci_get_drvdata(pdev);
632 struct usnic_ib_dev *pf = vf->pf;
633
634 mutex_lock(&pf->usdev_lock);
635 list_del(&vf->link);
636 mutex_unlock(&pf->usdev_lock);
637
638 kref_put(&pf->vf_cnt, usnic_ib_undiscover_pf);
639 usnic_vnic_free(vf->vnic);
640 pci_set_drvdata(pdev, NULL);
641 pci_clear_master(pdev);
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 */
650static 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 */
659static int __init usnic_ib_init(void)
660{
661 int err;
662
663 printk_once(KERN_INFO "%s", usnic_version);
664
665 err = usnic_uiom_init(DRV_NAME);
666 if (err) {
667 usnic_err("Unable to initalize umem with err %d\n", err);
668 return err;
669 }
670
Christophe Jaillet86cd7472016-08-10 17:45:01 +0200671 err = pci_register_driver(&usnic_ib_pci_driver);
672 if (err) {
Upinder Malhie3cf00d2013-09-10 03:38:16 +0000673 usnic_err("Unable to register with PCI\n");
674 goto out_umem_fini;
675 }
676
677 err = register_netdevice_notifier(&usnic_ib_netdevice_notifier);
678 if (err) {
679 usnic_err("Failed to register netdev notifier\n");
680 goto out_pci_unreg;
681 }
682
Upinder Malhic7845bc2014-01-09 14:48:17 -0800683 err = register_inetaddr_notifier(&usnic_ib_inetaddr_notifier);
684 if (err) {
685 usnic_err("Failed to register inet addr notifier\n");
686 goto out_unreg_netdev_notifier;
687 }
688
Upinder Malhie3cf00d2013-09-10 03:38:16 +0000689 err = usnic_transport_init();
690 if (err) {
691 usnic_err("Failed to initialize transport\n");
Upinder Malhic7845bc2014-01-09 14:48:17 -0800692 goto out_unreg_inetaddr_notifier;
Upinder Malhie3cf00d2013-09-10 03:38:16 +0000693 }
694
695 usnic_debugfs_init();
696
697 return 0;
698
Upinder Malhic7845bc2014-01-09 14:48:17 -0800699out_unreg_inetaddr_notifier:
700 unregister_inetaddr_notifier(&usnic_ib_inetaddr_notifier);
Upinder Malhie3cf00d2013-09-10 03:38:16 +0000701out_unreg_netdev_notifier:
702 unregister_netdevice_notifier(&usnic_ib_netdevice_notifier);
703out_pci_unreg:
704 pci_unregister_driver(&usnic_ib_pci_driver);
705out_umem_fini:
706 usnic_uiom_fini();
707
708 return err;
709}
710
711static void __exit usnic_ib_destroy(void)
712{
713 usnic_dbg("\n");
714 usnic_debugfs_exit();
715 usnic_transport_fini();
Upinder Malhic7845bc2014-01-09 14:48:17 -0800716 unregister_inetaddr_notifier(&usnic_ib_inetaddr_notifier);
Upinder Malhie3cf00d2013-09-10 03:38:16 +0000717 unregister_netdevice_notifier(&usnic_ib_netdevice_notifier);
718 pci_unregister_driver(&usnic_ib_pci_driver);
719 usnic_uiom_fini();
720}
721
722MODULE_DESCRIPTION("Cisco VIC (usNIC) Verbs Driver");
723MODULE_AUTHOR("Upinder Malhi <umalhi@cisco.com>");
724MODULE_LICENSE("Dual BSD/GPL");
Upinder Malhie3cf00d2013-09-10 03:38:16 +0000725module_param(usnic_log_lvl, uint, S_IRUGO | S_IWUSR);
726module_param(usnic_ib_share_vf, uint, S_IRUGO | S_IWUSR);
727MODULE_PARM_DESC(usnic_log_lvl, " Off=0, Err=1, Info=2, Debug=3");
728MODULE_PARM_DESC(usnic_ib_share_vf, "Off=0, On=1 VF sharing amongst QPs");
729MODULE_DEVICE_TABLE(pci, usnic_ib_pci_ids);
730
731module_init(usnic_ib_init);
732module_exit(usnic_ib_destroy);
733/* End of module section */