Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/jikos/hid
[GitHub/LineageOS/android_kernel_motorola_exynos9610.git] / drivers / infiniband / hw / bnxt_re / main.c
1 /*
2 * Broadcom NetXtreme-E RoCE driver.
3 *
4 * Copyright (c) 2016 - 2017, Broadcom. All rights reserved. The term
5 * Broadcom refers to Broadcom Limited and/or its subsidiaries.
6 *
7 * This software is available to you under a choice of one of two
8 * licenses. You may choose to be licensed under the terms of the GNU
9 * General Public License (GPL) Version 2, available from the file
10 * COPYING in the main directory of this source tree, or the
11 * BSD license below:
12 *
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
15 * are met:
16 *
17 * 1. Redistributions of source code must retain the above copyright
18 * notice, this list of conditions and the following disclaimer.
19 * 2. Redistributions in binary form must reproduce the above copyright
20 * notice, this list of conditions and the following disclaimer in
21 * the documentation and/or other materials provided with the
22 * distribution.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS''
25 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
26 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
27 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS
28 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
31 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
32 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
33 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
34 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 *
36 * Description: Main component of the bnxt_re driver
37 */
38
39 #include <linux/module.h>
40 #include <linux/netdevice.h>
41 #include <linux/ethtool.h>
42 #include <linux/mutex.h>
43 #include <linux/list.h>
44 #include <linux/rculist.h>
45 #include <linux/spinlock.h>
46 #include <linux/pci.h>
47 #include <net/dcbnl.h>
48 #include <net/ipv6.h>
49 #include <net/addrconf.h>
50 #include <linux/if_ether.h>
51
52 #include <rdma/ib_verbs.h>
53 #include <rdma/ib_user_verbs.h>
54 #include <rdma/ib_umem.h>
55 #include <rdma/ib_addr.h>
56
57 #include "bnxt_ulp.h"
58 #include "roce_hsi.h"
59 #include "qplib_res.h"
60 #include "qplib_sp.h"
61 #include "qplib_fp.h"
62 #include "qplib_rcfw.h"
63 #include "bnxt_re.h"
64 #include "ib_verbs.h"
65 #include <rdma/bnxt_re-abi.h>
66 #include "bnxt.h"
67 static char version[] =
68 BNXT_RE_DESC " v" ROCE_DRV_MODULE_VERSION "\n";
69
70 MODULE_AUTHOR("Eddie Wai <eddie.wai@broadcom.com>");
71 MODULE_DESCRIPTION(BNXT_RE_DESC " Driver");
72 MODULE_LICENSE("Dual BSD/GPL");
73 MODULE_VERSION(ROCE_DRV_MODULE_VERSION);
74
75 /* globals */
76 static struct list_head bnxt_re_dev_list = LIST_HEAD_INIT(bnxt_re_dev_list);
77 /* Mutex to protect the list of bnxt_re devices added */
78 static DEFINE_MUTEX(bnxt_re_dev_lock);
79 static struct workqueue_struct *bnxt_re_wq;
80
81 /* for handling bnxt_en callbacks later */
82 static void bnxt_re_stop(void *p)
83 {
84 }
85
86 static void bnxt_re_start(void *p)
87 {
88 }
89
90 static void bnxt_re_sriov_config(void *p, int num_vfs)
91 {
92 }
93
94 static struct bnxt_ulp_ops bnxt_re_ulp_ops = {
95 .ulp_async_notifier = NULL,
96 .ulp_stop = bnxt_re_stop,
97 .ulp_start = bnxt_re_start,
98 .ulp_sriov_config = bnxt_re_sriov_config
99 };
100
101 /* RoCE -> Net driver */
102
103 /* Driver registration routines used to let the networking driver (bnxt_en)
104 * to know that the RoCE driver is now installed
105 */
106 static int bnxt_re_unregister_netdev(struct bnxt_re_dev *rdev, bool lock_wait)
107 {
108 struct bnxt_en_dev *en_dev;
109 int rc;
110
111 if (!rdev)
112 return -EINVAL;
113
114 en_dev = rdev->en_dev;
115 /* Acquire rtnl lock if it is not invokded from netdev event */
116 if (lock_wait)
117 rtnl_lock();
118
119 rc = en_dev->en_ops->bnxt_unregister_device(rdev->en_dev,
120 BNXT_ROCE_ULP);
121 if (lock_wait)
122 rtnl_unlock();
123 return rc;
124 }
125
126 static int bnxt_re_register_netdev(struct bnxt_re_dev *rdev)
127 {
128 struct bnxt_en_dev *en_dev;
129 int rc = 0;
130
131 if (!rdev)
132 return -EINVAL;
133
134 en_dev = rdev->en_dev;
135
136 rtnl_lock();
137 rc = en_dev->en_ops->bnxt_register_device(en_dev, BNXT_ROCE_ULP,
138 &bnxt_re_ulp_ops, rdev);
139 rtnl_unlock();
140 return rc;
141 }
142
143 static int bnxt_re_free_msix(struct bnxt_re_dev *rdev, bool lock_wait)
144 {
145 struct bnxt_en_dev *en_dev;
146 int rc;
147
148 if (!rdev)
149 return -EINVAL;
150
151 en_dev = rdev->en_dev;
152
153 if (lock_wait)
154 rtnl_lock();
155
156 rc = en_dev->en_ops->bnxt_free_msix(rdev->en_dev, BNXT_ROCE_ULP);
157
158 if (lock_wait)
159 rtnl_unlock();
160 return rc;
161 }
162
163 static int bnxt_re_request_msix(struct bnxt_re_dev *rdev)
164 {
165 int rc = 0, num_msix_want = BNXT_RE_MIN_MSIX, num_msix_got;
166 struct bnxt_en_dev *en_dev;
167
168 if (!rdev)
169 return -EINVAL;
170
171 en_dev = rdev->en_dev;
172
173 rtnl_lock();
174 num_msix_got = en_dev->en_ops->bnxt_request_msix(en_dev, BNXT_ROCE_ULP,
175 rdev->msix_entries,
176 num_msix_want);
177 if (num_msix_got < BNXT_RE_MIN_MSIX) {
178 rc = -EINVAL;
179 goto done;
180 }
181 if (num_msix_got != num_msix_want) {
182 dev_warn(rdev_to_dev(rdev),
183 "Requested %d MSI-X vectors, got %d\n",
184 num_msix_want, num_msix_got);
185 }
186 rdev->num_msix = num_msix_got;
187 done:
188 rtnl_unlock();
189 return rc;
190 }
191
192 static void bnxt_re_init_hwrm_hdr(struct bnxt_re_dev *rdev, struct input *hdr,
193 u16 opcd, u16 crid, u16 trid)
194 {
195 hdr->req_type = cpu_to_le16(opcd);
196 hdr->cmpl_ring = cpu_to_le16(crid);
197 hdr->target_id = cpu_to_le16(trid);
198 }
199
200 static void bnxt_re_fill_fw_msg(struct bnxt_fw_msg *fw_msg, void *msg,
201 int msg_len, void *resp, int resp_max_len,
202 int timeout)
203 {
204 fw_msg->msg = msg;
205 fw_msg->msg_len = msg_len;
206 fw_msg->resp = resp;
207 fw_msg->resp_max_len = resp_max_len;
208 fw_msg->timeout = timeout;
209 }
210
211 static int bnxt_re_net_ring_free(struct bnxt_re_dev *rdev, u16 fw_ring_id,
212 bool lock_wait)
213 {
214 struct bnxt_en_dev *en_dev = rdev->en_dev;
215 struct hwrm_ring_free_input req = {0};
216 struct hwrm_ring_free_output resp;
217 struct bnxt_fw_msg fw_msg;
218 bool do_unlock = false;
219 int rc = -EINVAL;
220
221 if (!en_dev)
222 return rc;
223
224 memset(&fw_msg, 0, sizeof(fw_msg));
225 if (lock_wait) {
226 rtnl_lock();
227 do_unlock = true;
228 }
229
230 bnxt_re_init_hwrm_hdr(rdev, (void *)&req, HWRM_RING_FREE, -1, -1);
231 req.ring_type = RING_ALLOC_REQ_RING_TYPE_L2_CMPL;
232 req.ring_id = cpu_to_le16(fw_ring_id);
233 bnxt_re_fill_fw_msg(&fw_msg, (void *)&req, sizeof(req), (void *)&resp,
234 sizeof(resp), DFLT_HWRM_CMD_TIMEOUT);
235 rc = en_dev->en_ops->bnxt_send_fw_msg(en_dev, BNXT_ROCE_ULP, &fw_msg);
236 if (rc)
237 dev_err(rdev_to_dev(rdev),
238 "Failed to free HW ring:%d :%#x", req.ring_id, rc);
239 if (do_unlock)
240 rtnl_unlock();
241 return rc;
242 }
243
244 static int bnxt_re_net_ring_alloc(struct bnxt_re_dev *rdev, dma_addr_t *dma_arr,
245 int pages, int type, u32 ring_mask,
246 u32 map_index, u16 *fw_ring_id)
247 {
248 struct bnxt_en_dev *en_dev = rdev->en_dev;
249 struct hwrm_ring_alloc_input req = {0};
250 struct hwrm_ring_alloc_output resp;
251 struct bnxt_fw_msg fw_msg;
252 int rc = -EINVAL;
253
254 if (!en_dev)
255 return rc;
256
257 memset(&fw_msg, 0, sizeof(fw_msg));
258 rtnl_lock();
259 bnxt_re_init_hwrm_hdr(rdev, (void *)&req, HWRM_RING_ALLOC, -1, -1);
260 req.enables = 0;
261 req.page_tbl_addr = cpu_to_le64(dma_arr[0]);
262 if (pages > 1) {
263 /* Page size is in log2 units */
264 req.page_size = BNXT_PAGE_SHIFT;
265 req.page_tbl_depth = 1;
266 }
267 req.fbo = 0;
268 /* Association of ring index with doorbell index and MSIX number */
269 req.logical_id = cpu_to_le16(map_index);
270 req.length = cpu_to_le32(ring_mask + 1);
271 req.ring_type = RING_ALLOC_REQ_RING_TYPE_L2_CMPL;
272 req.int_mode = RING_ALLOC_REQ_INT_MODE_MSIX;
273 bnxt_re_fill_fw_msg(&fw_msg, (void *)&req, sizeof(req), (void *)&resp,
274 sizeof(resp), DFLT_HWRM_CMD_TIMEOUT);
275 rc = en_dev->en_ops->bnxt_send_fw_msg(en_dev, BNXT_ROCE_ULP, &fw_msg);
276 if (!rc)
277 *fw_ring_id = le16_to_cpu(resp.ring_id);
278
279 rtnl_unlock();
280 return rc;
281 }
282
283 static int bnxt_re_net_stats_ctx_free(struct bnxt_re_dev *rdev,
284 u32 fw_stats_ctx_id, bool lock_wait)
285 {
286 struct bnxt_en_dev *en_dev = rdev->en_dev;
287 struct hwrm_stat_ctx_free_input req = {0};
288 struct bnxt_fw_msg fw_msg;
289 bool do_unlock = false;
290 int rc = -EINVAL;
291
292 if (!en_dev)
293 return rc;
294
295 memset(&fw_msg, 0, sizeof(fw_msg));
296 if (lock_wait) {
297 rtnl_lock();
298 do_unlock = true;
299 }
300
301 bnxt_re_init_hwrm_hdr(rdev, (void *)&req, HWRM_STAT_CTX_FREE, -1, -1);
302 req.stat_ctx_id = cpu_to_le32(fw_stats_ctx_id);
303 bnxt_re_fill_fw_msg(&fw_msg, (void *)&req, sizeof(req), (void *)&req,
304 sizeof(req), DFLT_HWRM_CMD_TIMEOUT);
305 rc = en_dev->en_ops->bnxt_send_fw_msg(en_dev, BNXT_ROCE_ULP, &fw_msg);
306 if (rc)
307 dev_err(rdev_to_dev(rdev),
308 "Failed to free HW stats context %#x", rc);
309
310 if (do_unlock)
311 rtnl_unlock();
312 return rc;
313 }
314
315 static int bnxt_re_net_stats_ctx_alloc(struct bnxt_re_dev *rdev,
316 dma_addr_t dma_map,
317 u32 *fw_stats_ctx_id)
318 {
319 struct hwrm_stat_ctx_alloc_output resp = {0};
320 struct hwrm_stat_ctx_alloc_input req = {0};
321 struct bnxt_en_dev *en_dev = rdev->en_dev;
322 struct bnxt_fw_msg fw_msg;
323 int rc = -EINVAL;
324
325 *fw_stats_ctx_id = INVALID_STATS_CTX_ID;
326
327 if (!en_dev)
328 return rc;
329
330 memset(&fw_msg, 0, sizeof(fw_msg));
331 rtnl_lock();
332
333 bnxt_re_init_hwrm_hdr(rdev, (void *)&req, HWRM_STAT_CTX_ALLOC, -1, -1);
334 req.update_period_ms = cpu_to_le32(1000);
335 req.stats_dma_addr = cpu_to_le64(dma_map);
336 bnxt_re_fill_fw_msg(&fw_msg, (void *)&req, sizeof(req), (void *)&resp,
337 sizeof(resp), DFLT_HWRM_CMD_TIMEOUT);
338 rc = en_dev->en_ops->bnxt_send_fw_msg(en_dev, BNXT_ROCE_ULP, &fw_msg);
339 if (!rc)
340 *fw_stats_ctx_id = le32_to_cpu(resp.stat_ctx_id);
341
342 rtnl_unlock();
343 return rc;
344 }
345
346 /* Device */
347
348 static bool is_bnxt_re_dev(struct net_device *netdev)
349 {
350 struct ethtool_drvinfo drvinfo;
351
352 if (netdev->ethtool_ops && netdev->ethtool_ops->get_drvinfo) {
353 memset(&drvinfo, 0, sizeof(drvinfo));
354 netdev->ethtool_ops->get_drvinfo(netdev, &drvinfo);
355
356 if (strcmp(drvinfo.driver, "bnxt_en"))
357 return false;
358 return true;
359 }
360 return false;
361 }
362
363 static struct bnxt_re_dev *bnxt_re_from_netdev(struct net_device *netdev)
364 {
365 struct bnxt_re_dev *rdev;
366
367 rcu_read_lock();
368 list_for_each_entry_rcu(rdev, &bnxt_re_dev_list, list) {
369 if (rdev->netdev == netdev) {
370 rcu_read_unlock();
371 return rdev;
372 }
373 }
374 rcu_read_unlock();
375 return NULL;
376 }
377
378 static void bnxt_re_dev_unprobe(struct net_device *netdev,
379 struct bnxt_en_dev *en_dev)
380 {
381 dev_put(netdev);
382 module_put(en_dev->pdev->driver->driver.owner);
383 }
384
385 static struct bnxt_en_dev *bnxt_re_dev_probe(struct net_device *netdev)
386 {
387 struct bnxt *bp = netdev_priv(netdev);
388 struct bnxt_en_dev *en_dev;
389 struct pci_dev *pdev;
390
391 /* Call bnxt_en's RoCE probe via indirect API */
392 if (!bp->ulp_probe)
393 return ERR_PTR(-EINVAL);
394
395 en_dev = bp->ulp_probe(netdev);
396 if (IS_ERR(en_dev))
397 return en_dev;
398
399 pdev = en_dev->pdev;
400 if (!pdev)
401 return ERR_PTR(-EINVAL);
402
403 if (!(en_dev->flags & BNXT_EN_FLAG_ROCE_CAP)) {
404 dev_dbg(&pdev->dev,
405 "%s: probe error: RoCE is not supported on this device",
406 ROCE_DRV_MODULE_NAME);
407 return ERR_PTR(-ENODEV);
408 }
409
410 /* Bump net device reference count */
411 if (!try_module_get(pdev->driver->driver.owner))
412 return ERR_PTR(-ENODEV);
413
414 dev_hold(netdev);
415
416 return en_dev;
417 }
418
419 static void bnxt_re_unregister_ib(struct bnxt_re_dev *rdev)
420 {
421 ib_unregister_device(&rdev->ibdev);
422 }
423
424 static int bnxt_re_register_ib(struct bnxt_re_dev *rdev)
425 {
426 struct ib_device *ibdev = &rdev->ibdev;
427
428 /* ib device init */
429 ibdev->owner = THIS_MODULE;
430 ibdev->node_type = RDMA_NODE_IB_CA;
431 strlcpy(ibdev->name, "bnxt_re%d", IB_DEVICE_NAME_MAX);
432 strlcpy(ibdev->node_desc, BNXT_RE_DESC " HCA",
433 strlen(BNXT_RE_DESC) + 5);
434 ibdev->phys_port_cnt = 1;
435
436 bnxt_qplib_get_guid(rdev->netdev->dev_addr, (u8 *)&ibdev->node_guid);
437
438 ibdev->num_comp_vectors = 1;
439 ibdev->dev.parent = &rdev->en_dev->pdev->dev;
440 ibdev->local_dma_lkey = BNXT_QPLIB_RSVD_LKEY;
441
442 /* User space */
443 ibdev->uverbs_abi_ver = BNXT_RE_ABI_VERSION;
444 ibdev->uverbs_cmd_mask =
445 (1ull << IB_USER_VERBS_CMD_GET_CONTEXT) |
446 (1ull << IB_USER_VERBS_CMD_QUERY_DEVICE) |
447 (1ull << IB_USER_VERBS_CMD_QUERY_PORT) |
448 (1ull << IB_USER_VERBS_CMD_ALLOC_PD) |
449 (1ull << IB_USER_VERBS_CMD_DEALLOC_PD) |
450 (1ull << IB_USER_VERBS_CMD_REG_MR) |
451 (1ull << IB_USER_VERBS_CMD_REREG_MR) |
452 (1ull << IB_USER_VERBS_CMD_DEREG_MR) |
453 (1ull << IB_USER_VERBS_CMD_CREATE_COMP_CHANNEL) |
454 (1ull << IB_USER_VERBS_CMD_CREATE_CQ) |
455 (1ull << IB_USER_VERBS_CMD_RESIZE_CQ) |
456 (1ull << IB_USER_VERBS_CMD_DESTROY_CQ) |
457 (1ull << IB_USER_VERBS_CMD_CREATE_QP) |
458 (1ull << IB_USER_VERBS_CMD_MODIFY_QP) |
459 (1ull << IB_USER_VERBS_CMD_QUERY_QP) |
460 (1ull << IB_USER_VERBS_CMD_DESTROY_QP) |
461 (1ull << IB_USER_VERBS_CMD_CREATE_SRQ) |
462 (1ull << IB_USER_VERBS_CMD_MODIFY_SRQ) |
463 (1ull << IB_USER_VERBS_CMD_QUERY_SRQ) |
464 (1ull << IB_USER_VERBS_CMD_DESTROY_SRQ) |
465 (1ull << IB_USER_VERBS_CMD_CREATE_AH) |
466 (1ull << IB_USER_VERBS_CMD_MODIFY_AH) |
467 (1ull << IB_USER_VERBS_CMD_QUERY_AH) |
468 (1ull << IB_USER_VERBS_CMD_DESTROY_AH);
469 /* POLL_CQ and REQ_NOTIFY_CQ is directly handled in libbnxt_re */
470
471 /* Kernel verbs */
472 ibdev->query_device = bnxt_re_query_device;
473 ibdev->modify_device = bnxt_re_modify_device;
474
475 ibdev->query_port = bnxt_re_query_port;
476 ibdev->modify_port = bnxt_re_modify_port;
477 ibdev->get_port_immutable = bnxt_re_get_port_immutable;
478 ibdev->query_pkey = bnxt_re_query_pkey;
479 ibdev->query_gid = bnxt_re_query_gid;
480 ibdev->get_netdev = bnxt_re_get_netdev;
481 ibdev->add_gid = bnxt_re_add_gid;
482 ibdev->del_gid = bnxt_re_del_gid;
483 ibdev->get_link_layer = bnxt_re_get_link_layer;
484
485 ibdev->alloc_pd = bnxt_re_alloc_pd;
486 ibdev->dealloc_pd = bnxt_re_dealloc_pd;
487
488 ibdev->create_ah = bnxt_re_create_ah;
489 ibdev->modify_ah = bnxt_re_modify_ah;
490 ibdev->query_ah = bnxt_re_query_ah;
491 ibdev->destroy_ah = bnxt_re_destroy_ah;
492
493 ibdev->create_qp = bnxt_re_create_qp;
494 ibdev->modify_qp = bnxt_re_modify_qp;
495 ibdev->query_qp = bnxt_re_query_qp;
496 ibdev->destroy_qp = bnxt_re_destroy_qp;
497
498 ibdev->post_send = bnxt_re_post_send;
499 ibdev->post_recv = bnxt_re_post_recv;
500
501 ibdev->create_cq = bnxt_re_create_cq;
502 ibdev->destroy_cq = bnxt_re_destroy_cq;
503 ibdev->poll_cq = bnxt_re_poll_cq;
504 ibdev->req_notify_cq = bnxt_re_req_notify_cq;
505
506 ibdev->get_dma_mr = bnxt_re_get_dma_mr;
507 ibdev->dereg_mr = bnxt_re_dereg_mr;
508 ibdev->alloc_mr = bnxt_re_alloc_mr;
509 ibdev->map_mr_sg = bnxt_re_map_mr_sg;
510 ibdev->alloc_fmr = bnxt_re_alloc_fmr;
511 ibdev->map_phys_fmr = bnxt_re_map_phys_fmr;
512 ibdev->unmap_fmr = bnxt_re_unmap_fmr;
513 ibdev->dealloc_fmr = bnxt_re_dealloc_fmr;
514
515 ibdev->reg_user_mr = bnxt_re_reg_user_mr;
516 ibdev->alloc_ucontext = bnxt_re_alloc_ucontext;
517 ibdev->dealloc_ucontext = bnxt_re_dealloc_ucontext;
518 ibdev->mmap = bnxt_re_mmap;
519
520 return ib_register_device(ibdev, NULL);
521 }
522
523 static ssize_t show_rev(struct device *device, struct device_attribute *attr,
524 char *buf)
525 {
526 struct bnxt_re_dev *rdev = to_bnxt_re_dev(device, ibdev.dev);
527
528 return scnprintf(buf, PAGE_SIZE, "0x%x\n", rdev->en_dev->pdev->vendor);
529 }
530
531 static ssize_t show_fw_ver(struct device *device, struct device_attribute *attr,
532 char *buf)
533 {
534 struct bnxt_re_dev *rdev = to_bnxt_re_dev(device, ibdev.dev);
535
536 return scnprintf(buf, PAGE_SIZE, "%s\n", rdev->dev_attr.fw_ver);
537 }
538
539 static ssize_t show_hca(struct device *device, struct device_attribute *attr,
540 char *buf)
541 {
542 struct bnxt_re_dev *rdev = to_bnxt_re_dev(device, ibdev.dev);
543
544 return scnprintf(buf, PAGE_SIZE, "%s\n", rdev->ibdev.node_desc);
545 }
546
547 static DEVICE_ATTR(hw_rev, 0444, show_rev, NULL);
548 static DEVICE_ATTR(fw_rev, 0444, show_fw_ver, NULL);
549 static DEVICE_ATTR(hca_type, 0444, show_hca, NULL);
550
551 static struct device_attribute *bnxt_re_attributes[] = {
552 &dev_attr_hw_rev,
553 &dev_attr_fw_rev,
554 &dev_attr_hca_type
555 };
556
557 static void bnxt_re_dev_remove(struct bnxt_re_dev *rdev)
558 {
559 dev_put(rdev->netdev);
560 rdev->netdev = NULL;
561
562 mutex_lock(&bnxt_re_dev_lock);
563 list_del_rcu(&rdev->list);
564 mutex_unlock(&bnxt_re_dev_lock);
565
566 synchronize_rcu();
567 flush_workqueue(bnxt_re_wq);
568
569 ib_dealloc_device(&rdev->ibdev);
570 /* rdev is gone */
571 }
572
573 static struct bnxt_re_dev *bnxt_re_dev_add(struct net_device *netdev,
574 struct bnxt_en_dev *en_dev)
575 {
576 struct bnxt_re_dev *rdev;
577
578 /* Allocate bnxt_re_dev instance here */
579 rdev = (struct bnxt_re_dev *)ib_alloc_device(sizeof(*rdev));
580 if (!rdev) {
581 dev_err(NULL, "%s: bnxt_re_dev allocation failure!",
582 ROCE_DRV_MODULE_NAME);
583 return NULL;
584 }
585 /* Default values */
586 rdev->netdev = netdev;
587 dev_hold(rdev->netdev);
588 rdev->en_dev = en_dev;
589 rdev->id = rdev->en_dev->pdev->devfn;
590 INIT_LIST_HEAD(&rdev->qp_list);
591 mutex_init(&rdev->qp_lock);
592 atomic_set(&rdev->qp_count, 0);
593 atomic_set(&rdev->cq_count, 0);
594 atomic_set(&rdev->srq_count, 0);
595 atomic_set(&rdev->mr_count, 0);
596 atomic_set(&rdev->mw_count, 0);
597 rdev->cosq[0] = 0xFFFF;
598 rdev->cosq[1] = 0xFFFF;
599
600 mutex_lock(&bnxt_re_dev_lock);
601 list_add_tail_rcu(&rdev->list, &bnxt_re_dev_list);
602 mutex_unlock(&bnxt_re_dev_lock);
603 return rdev;
604 }
605
606 static int bnxt_re_aeq_handler(struct bnxt_qplib_rcfw *rcfw,
607 struct creq_func_event *aeqe)
608 {
609 switch (aeqe->event) {
610 case CREQ_FUNC_EVENT_EVENT_TX_WQE_ERROR:
611 break;
612 case CREQ_FUNC_EVENT_EVENT_TX_DATA_ERROR:
613 break;
614 case CREQ_FUNC_EVENT_EVENT_RX_WQE_ERROR:
615 break;
616 case CREQ_FUNC_EVENT_EVENT_RX_DATA_ERROR:
617 break;
618 case CREQ_FUNC_EVENT_EVENT_CQ_ERROR:
619 break;
620 case CREQ_FUNC_EVENT_EVENT_TQM_ERROR:
621 break;
622 case CREQ_FUNC_EVENT_EVENT_CFCQ_ERROR:
623 break;
624 case CREQ_FUNC_EVENT_EVENT_CFCS_ERROR:
625 break;
626 case CREQ_FUNC_EVENT_EVENT_CFCC_ERROR:
627 break;
628 case CREQ_FUNC_EVENT_EVENT_CFCM_ERROR:
629 break;
630 case CREQ_FUNC_EVENT_EVENT_TIM_ERROR:
631 break;
632 default:
633 return -EINVAL;
634 }
635 return 0;
636 }
637
638 static int bnxt_re_cqn_handler(struct bnxt_qplib_nq *nq,
639 struct bnxt_qplib_cq *handle)
640 {
641 struct bnxt_re_cq *cq = container_of(handle, struct bnxt_re_cq,
642 qplib_cq);
643
644 if (!cq) {
645 dev_err(NULL, "%s: CQ is NULL, CQN not handled",
646 ROCE_DRV_MODULE_NAME);
647 return -EINVAL;
648 }
649 if (cq->ib_cq.comp_handler) {
650 /* Lock comp_handler? */
651 (*cq->ib_cq.comp_handler)(&cq->ib_cq, cq->ib_cq.cq_context);
652 }
653
654 return 0;
655 }
656
657 static void bnxt_re_cleanup_res(struct bnxt_re_dev *rdev)
658 {
659 if (rdev->nq.hwq.max_elements)
660 bnxt_qplib_disable_nq(&rdev->nq);
661
662 if (rdev->qplib_res.rcfw)
663 bnxt_qplib_cleanup_res(&rdev->qplib_res);
664 }
665
666 static int bnxt_re_init_res(struct bnxt_re_dev *rdev)
667 {
668 int rc = 0;
669
670 bnxt_qplib_init_res(&rdev->qplib_res);
671
672 if (rdev->msix_entries[BNXT_RE_NQ_IDX].vector <= 0)
673 return -EINVAL;
674
675 rc = bnxt_qplib_enable_nq(rdev->en_dev->pdev, &rdev->nq,
676 rdev->msix_entries[BNXT_RE_NQ_IDX].vector,
677 rdev->msix_entries[BNXT_RE_NQ_IDX].db_offset,
678 &bnxt_re_cqn_handler,
679 NULL);
680
681 if (rc)
682 dev_err(rdev_to_dev(rdev), "Failed to enable NQ: %#x", rc);
683
684 return rc;
685 }
686
687 static void bnxt_re_free_res(struct bnxt_re_dev *rdev, bool lock_wait)
688 {
689 if (rdev->nq.hwq.max_elements) {
690 bnxt_re_net_ring_free(rdev, rdev->nq.ring_id, lock_wait);
691 bnxt_qplib_free_nq(&rdev->nq);
692 }
693 if (rdev->qplib_res.dpi_tbl.max) {
694 bnxt_qplib_dealloc_dpi(&rdev->qplib_res,
695 &rdev->qplib_res.dpi_tbl,
696 &rdev->dpi_privileged);
697 }
698 if (rdev->qplib_res.rcfw) {
699 bnxt_qplib_free_res(&rdev->qplib_res);
700 rdev->qplib_res.rcfw = NULL;
701 }
702 }
703
704 static int bnxt_re_alloc_res(struct bnxt_re_dev *rdev)
705 {
706 int rc = 0;
707
708 /* Configure and allocate resources for qplib */
709 rdev->qplib_res.rcfw = &rdev->rcfw;
710 rc = bnxt_qplib_get_dev_attr(&rdev->rcfw, &rdev->dev_attr);
711 if (rc)
712 goto fail;
713
714 rc = bnxt_qplib_alloc_res(&rdev->qplib_res, rdev->en_dev->pdev,
715 rdev->netdev, &rdev->dev_attr);
716 if (rc)
717 goto fail;
718
719 rc = bnxt_qplib_alloc_dpi(&rdev->qplib_res.dpi_tbl,
720 &rdev->dpi_privileged,
721 rdev);
722 if (rc)
723 goto fail;
724
725 rdev->nq.hwq.max_elements = BNXT_RE_MAX_CQ_COUNT +
726 BNXT_RE_MAX_SRQC_COUNT + 2;
727 rc = bnxt_qplib_alloc_nq(rdev->en_dev->pdev, &rdev->nq);
728 if (rc) {
729 dev_err(rdev_to_dev(rdev),
730 "Failed to allocate NQ memory: %#x", rc);
731 goto fail;
732 }
733 rc = bnxt_re_net_ring_alloc
734 (rdev, rdev->nq.hwq.pbl[PBL_LVL_0].pg_map_arr,
735 rdev->nq.hwq.pbl[rdev->nq.hwq.level].pg_count,
736 HWRM_RING_ALLOC_CMPL, BNXT_QPLIB_NQE_MAX_CNT - 1,
737 rdev->msix_entries[BNXT_RE_NQ_IDX].ring_idx,
738 &rdev->nq.ring_id);
739 if (rc) {
740 dev_err(rdev_to_dev(rdev),
741 "Failed to allocate NQ ring: %#x", rc);
742 goto free_nq;
743 }
744 return 0;
745 free_nq:
746 bnxt_qplib_free_nq(&rdev->nq);
747 fail:
748 rdev->qplib_res.rcfw = NULL;
749 return rc;
750 }
751
752 static void bnxt_re_dispatch_event(struct ib_device *ibdev, struct ib_qp *qp,
753 u8 port_num, enum ib_event_type event)
754 {
755 struct ib_event ib_event;
756
757 ib_event.device = ibdev;
758 if (qp)
759 ib_event.element.qp = qp;
760 else
761 ib_event.element.port_num = port_num;
762 ib_event.event = event;
763 ib_dispatch_event(&ib_event);
764 }
765
766 #define HWRM_QUEUE_PRI2COS_QCFG_INPUT_FLAGS_IVLAN 0x02
767 static int bnxt_re_query_hwrm_pri2cos(struct bnxt_re_dev *rdev, u8 dir,
768 u64 *cid_map)
769 {
770 struct hwrm_queue_pri2cos_qcfg_input req = {0};
771 struct bnxt *bp = netdev_priv(rdev->netdev);
772 struct hwrm_queue_pri2cos_qcfg_output resp;
773 struct bnxt_en_dev *en_dev = rdev->en_dev;
774 struct bnxt_fw_msg fw_msg;
775 u32 flags = 0;
776 u8 *qcfgmap, *tmp_map;
777 int rc = 0, i;
778
779 if (!cid_map)
780 return -EINVAL;
781
782 memset(&fw_msg, 0, sizeof(fw_msg));
783 bnxt_re_init_hwrm_hdr(rdev, (void *)&req,
784 HWRM_QUEUE_PRI2COS_QCFG, -1, -1);
785 flags |= (dir & 0x01);
786 flags |= HWRM_QUEUE_PRI2COS_QCFG_INPUT_FLAGS_IVLAN;
787 req.flags = cpu_to_le32(flags);
788 req.port_id = bp->pf.port_id;
789
790 bnxt_re_fill_fw_msg(&fw_msg, (void *)&req, sizeof(req), (void *)&resp,
791 sizeof(resp), DFLT_HWRM_CMD_TIMEOUT);
792 rc = en_dev->en_ops->bnxt_send_fw_msg(en_dev, BNXT_ROCE_ULP, &fw_msg);
793 if (rc)
794 return rc;
795
796 if (resp.queue_cfg_info) {
797 dev_warn(rdev_to_dev(rdev),
798 "Asymmetric cos queue configuration detected");
799 dev_warn(rdev_to_dev(rdev),
800 " on device, QoS may not be fully functional\n");
801 }
802 qcfgmap = &resp.pri0_cos_queue_id;
803 tmp_map = (u8 *)cid_map;
804 for (i = 0; i < IEEE_8021QAZ_MAX_TCS; i++)
805 tmp_map[i] = qcfgmap[i];
806
807 return rc;
808 }
809
810 static bool bnxt_re_is_qp1_or_shadow_qp(struct bnxt_re_dev *rdev,
811 struct bnxt_re_qp *qp)
812 {
813 return (qp->ib_qp.qp_type == IB_QPT_GSI) || (qp == rdev->qp1_sqp);
814 }
815
816 static void bnxt_re_dev_stop(struct bnxt_re_dev *rdev)
817 {
818 int mask = IB_QP_STATE;
819 struct ib_qp_attr qp_attr;
820 struct bnxt_re_qp *qp;
821
822 qp_attr.qp_state = IB_QPS_ERR;
823 mutex_lock(&rdev->qp_lock);
824 list_for_each_entry(qp, &rdev->qp_list, list) {
825 /* Modify the state of all QPs except QP1/Shadow QP */
826 if (!bnxt_re_is_qp1_or_shadow_qp(rdev, qp)) {
827 if (qp->qplib_qp.state !=
828 CMDQ_MODIFY_QP_NEW_STATE_RESET &&
829 qp->qplib_qp.state !=
830 CMDQ_MODIFY_QP_NEW_STATE_ERR) {
831 bnxt_re_dispatch_event(&rdev->ibdev, &qp->ib_qp,
832 1, IB_EVENT_QP_FATAL);
833 bnxt_re_modify_qp(&qp->ib_qp, &qp_attr, mask,
834 NULL);
835 }
836 }
837 }
838 mutex_unlock(&rdev->qp_lock);
839 }
840
841 static u32 bnxt_re_get_priority_mask(struct bnxt_re_dev *rdev)
842 {
843 u32 prio_map = 0, tmp_map = 0;
844 struct net_device *netdev;
845 struct dcb_app app;
846
847 netdev = rdev->netdev;
848
849 memset(&app, 0, sizeof(app));
850 app.selector = IEEE_8021QAZ_APP_SEL_ETHERTYPE;
851 app.protocol = ETH_P_IBOE;
852 tmp_map = dcb_ieee_getapp_mask(netdev, &app);
853 prio_map = tmp_map;
854
855 app.selector = IEEE_8021QAZ_APP_SEL_DGRAM;
856 app.protocol = ROCE_V2_UDP_DPORT;
857 tmp_map = dcb_ieee_getapp_mask(netdev, &app);
858 prio_map |= tmp_map;
859
860 if (!prio_map)
861 prio_map = -EFAULT;
862 return prio_map;
863 }
864
865 static void bnxt_re_parse_cid_map(u8 prio_map, u8 *cid_map, u16 *cosq)
866 {
867 u16 prio;
868 u8 id;
869
870 for (prio = 0, id = 0; prio < 8; prio++) {
871 if (prio_map & (1 << prio)) {
872 cosq[id] = cid_map[prio];
873 id++;
874 if (id == 2) /* Max 2 tcs supported */
875 break;
876 }
877 }
878 }
879
880 static int bnxt_re_setup_qos(struct bnxt_re_dev *rdev)
881 {
882 u8 prio_map = 0;
883 u64 cid_map;
884 int rc;
885
886 /* Get priority for roce */
887 rc = bnxt_re_get_priority_mask(rdev);
888 if (rc < 0)
889 return rc;
890 prio_map = (u8)rc;
891
892 if (prio_map == rdev->cur_prio_map)
893 return 0;
894 rdev->cur_prio_map = prio_map;
895 /* Get cosq id for this priority */
896 rc = bnxt_re_query_hwrm_pri2cos(rdev, 0, &cid_map);
897 if (rc) {
898 dev_warn(rdev_to_dev(rdev), "no cos for p_mask %x\n", prio_map);
899 return rc;
900 }
901 /* Parse CoS IDs for app priority */
902 bnxt_re_parse_cid_map(prio_map, (u8 *)&cid_map, rdev->cosq);
903
904 /* Config BONO. */
905 rc = bnxt_qplib_map_tc2cos(&rdev->qplib_res, rdev->cosq);
906 if (rc) {
907 dev_warn(rdev_to_dev(rdev), "no tc for cos{%x, %x}\n",
908 rdev->cosq[0], rdev->cosq[1]);
909 return rc;
910 }
911
912 return 0;
913 }
914
915 static void bnxt_re_ib_unreg(struct bnxt_re_dev *rdev, bool lock_wait)
916 {
917 int i, rc;
918
919 if (test_and_clear_bit(BNXT_RE_FLAG_IBDEV_REGISTERED, &rdev->flags)) {
920 for (i = 0; i < ARRAY_SIZE(bnxt_re_attributes); i++)
921 device_remove_file(&rdev->ibdev.dev,
922 bnxt_re_attributes[i]);
923 /* Cleanup ib dev */
924 bnxt_re_unregister_ib(rdev);
925 }
926 if (test_and_clear_bit(BNXT_RE_FLAG_QOS_WORK_REG, &rdev->flags))
927 cancel_delayed_work(&rdev->worker);
928
929 bnxt_re_cleanup_res(rdev);
930 bnxt_re_free_res(rdev, lock_wait);
931
932 if (test_and_clear_bit(BNXT_RE_FLAG_RCFW_CHANNEL_EN, &rdev->flags)) {
933 rc = bnxt_qplib_deinit_rcfw(&rdev->rcfw);
934 if (rc)
935 dev_warn(rdev_to_dev(rdev),
936 "Failed to deinitialize RCFW: %#x", rc);
937 bnxt_re_net_stats_ctx_free(rdev, rdev->qplib_ctx.stats.fw_id,
938 lock_wait);
939 bnxt_qplib_free_ctx(rdev->en_dev->pdev, &rdev->qplib_ctx);
940 bnxt_qplib_disable_rcfw_channel(&rdev->rcfw);
941 bnxt_re_net_ring_free(rdev, rdev->rcfw.creq_ring_id, lock_wait);
942 bnxt_qplib_free_rcfw_channel(&rdev->rcfw);
943 }
944 if (test_and_clear_bit(BNXT_RE_FLAG_GOT_MSIX, &rdev->flags)) {
945 rc = bnxt_re_free_msix(rdev, lock_wait);
946 if (rc)
947 dev_warn(rdev_to_dev(rdev),
948 "Failed to free MSI-X vectors: %#x", rc);
949 }
950 if (test_and_clear_bit(BNXT_RE_FLAG_NETDEV_REGISTERED, &rdev->flags)) {
951 rc = bnxt_re_unregister_netdev(rdev, lock_wait);
952 if (rc)
953 dev_warn(rdev_to_dev(rdev),
954 "Failed to unregister with netdev: %#x", rc);
955 }
956 }
957
958 static void bnxt_re_set_resource_limits(struct bnxt_re_dev *rdev)
959 {
960 u32 i;
961
962 rdev->qplib_ctx.qpc_count = BNXT_RE_MAX_QPC_COUNT;
963 rdev->qplib_ctx.mrw_count = BNXT_RE_MAX_MRW_COUNT;
964 rdev->qplib_ctx.srqc_count = BNXT_RE_MAX_SRQC_COUNT;
965 rdev->qplib_ctx.cq_count = BNXT_RE_MAX_CQ_COUNT;
966 for (i = 0; i < MAX_TQM_ALLOC_REQ; i++)
967 rdev->qplib_ctx.tqm_count[i] =
968 rdev->dev_attr.tqm_alloc_reqs[i];
969 }
970
971 /* worker thread for polling periodic events. Now used for QoS programming*/
972 static void bnxt_re_worker(struct work_struct *work)
973 {
974 struct bnxt_re_dev *rdev = container_of(work, struct bnxt_re_dev,
975 worker.work);
976
977 bnxt_re_setup_qos(rdev);
978 schedule_delayed_work(&rdev->worker, msecs_to_jiffies(30000));
979 }
980
981 static int bnxt_re_ib_reg(struct bnxt_re_dev *rdev)
982 {
983 int i, j, rc;
984
985 /* Registered a new RoCE device instance to netdev */
986 rc = bnxt_re_register_netdev(rdev);
987 if (rc) {
988 pr_err("Failed to register with netedev: %#x\n", rc);
989 return -EINVAL;
990 }
991 set_bit(BNXT_RE_FLAG_NETDEV_REGISTERED, &rdev->flags);
992
993 rc = bnxt_re_request_msix(rdev);
994 if (rc) {
995 pr_err("Failed to get MSI-X vectors: %#x\n", rc);
996 rc = -EINVAL;
997 goto fail;
998 }
999 set_bit(BNXT_RE_FLAG_GOT_MSIX, &rdev->flags);
1000
1001 /* Establish RCFW Communication Channel to initialize the context
1002 * memory for the function and all child VFs
1003 */
1004 rc = bnxt_qplib_alloc_rcfw_channel(rdev->en_dev->pdev, &rdev->rcfw);
1005 if (rc)
1006 goto fail;
1007
1008 rc = bnxt_re_net_ring_alloc
1009 (rdev, rdev->rcfw.creq.pbl[PBL_LVL_0].pg_map_arr,
1010 rdev->rcfw.creq.pbl[rdev->rcfw.creq.level].pg_count,
1011 HWRM_RING_ALLOC_CMPL, BNXT_QPLIB_CREQE_MAX_CNT - 1,
1012 rdev->msix_entries[BNXT_RE_AEQ_IDX].ring_idx,
1013 &rdev->rcfw.creq_ring_id);
1014 if (rc) {
1015 pr_err("Failed to allocate CREQ: %#x\n", rc);
1016 goto free_rcfw;
1017 }
1018 rc = bnxt_qplib_enable_rcfw_channel
1019 (rdev->en_dev->pdev, &rdev->rcfw,
1020 rdev->msix_entries[BNXT_RE_AEQ_IDX].vector,
1021 rdev->msix_entries[BNXT_RE_AEQ_IDX].db_offset,
1022 0, &bnxt_re_aeq_handler);
1023 if (rc) {
1024 pr_err("Failed to enable RCFW channel: %#x\n", rc);
1025 goto free_ring;
1026 }
1027
1028 rc = bnxt_qplib_get_dev_attr(&rdev->rcfw, &rdev->dev_attr);
1029 if (rc)
1030 goto disable_rcfw;
1031 bnxt_re_set_resource_limits(rdev);
1032
1033 rc = bnxt_qplib_alloc_ctx(rdev->en_dev->pdev, &rdev->qplib_ctx, 0);
1034 if (rc) {
1035 pr_err("Failed to allocate QPLIB context: %#x\n", rc);
1036 goto disable_rcfw;
1037 }
1038 rc = bnxt_re_net_stats_ctx_alloc(rdev,
1039 rdev->qplib_ctx.stats.dma_map,
1040 &rdev->qplib_ctx.stats.fw_id);
1041 if (rc) {
1042 pr_err("Failed to allocate stats context: %#x\n", rc);
1043 goto free_ctx;
1044 }
1045
1046 rc = bnxt_qplib_init_rcfw(&rdev->rcfw, &rdev->qplib_ctx, 0);
1047 if (rc) {
1048 pr_err("Failed to initialize RCFW: %#x\n", rc);
1049 goto free_sctx;
1050 }
1051 set_bit(BNXT_RE_FLAG_RCFW_CHANNEL_EN, &rdev->flags);
1052
1053 /* Resources based on the 'new' device caps */
1054 rc = bnxt_re_alloc_res(rdev);
1055 if (rc) {
1056 pr_err("Failed to allocate resources: %#x\n", rc);
1057 goto fail;
1058 }
1059 rc = bnxt_re_init_res(rdev);
1060 if (rc) {
1061 pr_err("Failed to initialize resources: %#x\n", rc);
1062 goto fail;
1063 }
1064
1065 rc = bnxt_re_setup_qos(rdev);
1066 if (rc)
1067 pr_info("RoCE priority not yet configured\n");
1068
1069 INIT_DELAYED_WORK(&rdev->worker, bnxt_re_worker);
1070 set_bit(BNXT_RE_FLAG_QOS_WORK_REG, &rdev->flags);
1071 schedule_delayed_work(&rdev->worker, msecs_to_jiffies(30000));
1072
1073 /* Register ib dev */
1074 rc = bnxt_re_register_ib(rdev);
1075 if (rc) {
1076 pr_err("Failed to register with IB: %#x\n", rc);
1077 goto fail;
1078 }
1079 dev_info(rdev_to_dev(rdev), "Device registered successfully");
1080 for (i = 0; i < ARRAY_SIZE(bnxt_re_attributes); i++) {
1081 rc = device_create_file(&rdev->ibdev.dev,
1082 bnxt_re_attributes[i]);
1083 if (rc) {
1084 dev_err(rdev_to_dev(rdev),
1085 "Failed to create IB sysfs: %#x", rc);
1086 /* Must clean up all created device files */
1087 for (j = 0; j < i; j++)
1088 device_remove_file(&rdev->ibdev.dev,
1089 bnxt_re_attributes[j]);
1090 bnxt_re_unregister_ib(rdev);
1091 goto fail;
1092 }
1093 }
1094 set_bit(BNXT_RE_FLAG_IBDEV_REGISTERED, &rdev->flags);
1095 bnxt_re_dispatch_event(&rdev->ibdev, NULL, 1, IB_EVENT_PORT_ACTIVE);
1096 bnxt_re_dispatch_event(&rdev->ibdev, NULL, 1, IB_EVENT_GID_CHANGE);
1097
1098 return 0;
1099 free_sctx:
1100 bnxt_re_net_stats_ctx_free(rdev, rdev->qplib_ctx.stats.fw_id, true);
1101 free_ctx:
1102 bnxt_qplib_free_ctx(rdev->en_dev->pdev, &rdev->qplib_ctx);
1103 disable_rcfw:
1104 bnxt_qplib_disable_rcfw_channel(&rdev->rcfw);
1105 free_ring:
1106 bnxt_re_net_ring_free(rdev, rdev->rcfw.creq_ring_id, true);
1107 free_rcfw:
1108 bnxt_qplib_free_rcfw_channel(&rdev->rcfw);
1109 fail:
1110 bnxt_re_ib_unreg(rdev, true);
1111 return rc;
1112 }
1113
1114 static void bnxt_re_dev_unreg(struct bnxt_re_dev *rdev)
1115 {
1116 struct bnxt_en_dev *en_dev = rdev->en_dev;
1117 struct net_device *netdev = rdev->netdev;
1118
1119 bnxt_re_dev_remove(rdev);
1120
1121 if (netdev)
1122 bnxt_re_dev_unprobe(netdev, en_dev);
1123 }
1124
1125 static int bnxt_re_dev_reg(struct bnxt_re_dev **rdev, struct net_device *netdev)
1126 {
1127 struct bnxt_en_dev *en_dev;
1128 int rc = 0;
1129
1130 if (!is_bnxt_re_dev(netdev))
1131 return -ENODEV;
1132
1133 en_dev = bnxt_re_dev_probe(netdev);
1134 if (IS_ERR(en_dev)) {
1135 if (en_dev != ERR_PTR(-ENODEV))
1136 pr_err("%s: Failed to probe\n", ROCE_DRV_MODULE_NAME);
1137 rc = PTR_ERR(en_dev);
1138 goto exit;
1139 }
1140 *rdev = bnxt_re_dev_add(netdev, en_dev);
1141 if (!*rdev) {
1142 rc = -ENOMEM;
1143 bnxt_re_dev_unprobe(netdev, en_dev);
1144 goto exit;
1145 }
1146 exit:
1147 return rc;
1148 }
1149
1150 static void bnxt_re_remove_one(struct bnxt_re_dev *rdev)
1151 {
1152 pci_dev_put(rdev->en_dev->pdev);
1153 }
1154
1155 /* Handle all deferred netevents tasks */
1156 static void bnxt_re_task(struct work_struct *work)
1157 {
1158 struct bnxt_re_work *re_work;
1159 struct bnxt_re_dev *rdev;
1160 int rc = 0;
1161
1162 re_work = container_of(work, struct bnxt_re_work, work);
1163 rdev = re_work->rdev;
1164
1165 if (re_work->event != NETDEV_REGISTER &&
1166 !test_bit(BNXT_RE_FLAG_IBDEV_REGISTERED, &rdev->flags))
1167 return;
1168
1169 switch (re_work->event) {
1170 case NETDEV_REGISTER:
1171 rc = bnxt_re_ib_reg(rdev);
1172 if (rc)
1173 dev_err(rdev_to_dev(rdev),
1174 "Failed to register with IB: %#x", rc);
1175 break;
1176 case NETDEV_UP:
1177 bnxt_re_dispatch_event(&rdev->ibdev, NULL, 1,
1178 IB_EVENT_PORT_ACTIVE);
1179 break;
1180 case NETDEV_DOWN:
1181 bnxt_re_dev_stop(rdev);
1182 break;
1183 case NETDEV_CHANGE:
1184 if (!netif_carrier_ok(rdev->netdev))
1185 bnxt_re_dev_stop(rdev);
1186 else if (netif_carrier_ok(rdev->netdev))
1187 bnxt_re_dispatch_event(&rdev->ibdev, NULL, 1,
1188 IB_EVENT_PORT_ACTIVE);
1189 break;
1190 default:
1191 break;
1192 }
1193 kfree(re_work);
1194 }
1195
1196 static void bnxt_re_init_one(struct bnxt_re_dev *rdev)
1197 {
1198 pci_dev_get(rdev->en_dev->pdev);
1199 }
1200
1201 /*
1202 * "Notifier chain callback can be invoked for the same chain from
1203 * different CPUs at the same time".
1204 *
1205 * For cases when the netdev is already present, our call to the
1206 * register_netdevice_notifier() will actually get the rtnl_lock()
1207 * before sending NETDEV_REGISTER and (if up) NETDEV_UP
1208 * events.
1209 *
1210 * But for cases when the netdev is not already present, the notifier
1211 * chain is subjected to be invoked from different CPUs simultaneously.
1212 *
1213 * This is protected by the netdev_mutex.
1214 */
1215 static int bnxt_re_netdev_event(struct notifier_block *notifier,
1216 unsigned long event, void *ptr)
1217 {
1218 struct net_device *real_dev, *netdev = netdev_notifier_info_to_dev(ptr);
1219 struct bnxt_re_work *re_work;
1220 struct bnxt_re_dev *rdev;
1221 int rc = 0;
1222 bool sch_work = false;
1223
1224 real_dev = rdma_vlan_dev_real_dev(netdev);
1225 if (!real_dev)
1226 real_dev = netdev;
1227
1228 rdev = bnxt_re_from_netdev(real_dev);
1229 if (!rdev && event != NETDEV_REGISTER)
1230 goto exit;
1231 if (real_dev != netdev)
1232 goto exit;
1233
1234 switch (event) {
1235 case NETDEV_REGISTER:
1236 if (rdev)
1237 break;
1238 rc = bnxt_re_dev_reg(&rdev, real_dev);
1239 if (rc == -ENODEV)
1240 break;
1241 if (rc) {
1242 pr_err("Failed to register with the device %s: %#x\n",
1243 real_dev->name, rc);
1244 break;
1245 }
1246 bnxt_re_init_one(rdev);
1247 sch_work = true;
1248 break;
1249
1250 case NETDEV_UNREGISTER:
1251 bnxt_re_ib_unreg(rdev, false);
1252 bnxt_re_remove_one(rdev);
1253 bnxt_re_dev_unreg(rdev);
1254 break;
1255
1256 default:
1257 sch_work = true;
1258 break;
1259 }
1260 if (sch_work) {
1261 /* Allocate for the deferred task */
1262 re_work = kzalloc(sizeof(*re_work), GFP_ATOMIC);
1263 if (re_work) {
1264 re_work->rdev = rdev;
1265 re_work->event = event;
1266 re_work->vlan_dev = (real_dev == netdev ?
1267 NULL : netdev);
1268 INIT_WORK(&re_work->work, bnxt_re_task);
1269 queue_work(bnxt_re_wq, &re_work->work);
1270 }
1271 }
1272
1273 exit:
1274 return NOTIFY_DONE;
1275 }
1276
1277 static struct notifier_block bnxt_re_netdev_notifier = {
1278 .notifier_call = bnxt_re_netdev_event
1279 };
1280
1281 static int __init bnxt_re_mod_init(void)
1282 {
1283 int rc = 0;
1284
1285 pr_info("%s: %s", ROCE_DRV_MODULE_NAME, version);
1286
1287 bnxt_re_wq = create_singlethread_workqueue("bnxt_re");
1288 if (!bnxt_re_wq)
1289 return -ENOMEM;
1290
1291 INIT_LIST_HEAD(&bnxt_re_dev_list);
1292
1293 rc = register_netdevice_notifier(&bnxt_re_netdev_notifier);
1294 if (rc) {
1295 pr_err("%s: Cannot register to netdevice_notifier",
1296 ROCE_DRV_MODULE_NAME);
1297 goto err_netdev;
1298 }
1299 return 0;
1300
1301 err_netdev:
1302 destroy_workqueue(bnxt_re_wq);
1303
1304 return rc;
1305 }
1306
1307 static void __exit bnxt_re_mod_exit(void)
1308 {
1309 unregister_netdevice_notifier(&bnxt_re_netdev_notifier);
1310 if (bnxt_re_wq)
1311 destroy_workqueue(bnxt_re_wq);
1312 }
1313
1314 module_init(bnxt_re_mod_init);
1315 module_exit(bnxt_re_mod_exit);