blob: 2f71123e0a9febedf25fc07eae7950b85d1ebc44 [file] [log] [blame]
Selvin Xavier1ac5a402017-02-10 03:19:33 -08001/*
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"
67static char version[] =
68 BNXT_RE_DESC " v" ROCE_DRV_MODULE_VERSION "\n";
69
70MODULE_AUTHOR("Eddie Wai <eddie.wai@broadcom.com>");
71MODULE_DESCRIPTION(BNXT_RE_DESC " Driver");
72MODULE_LICENSE("Dual BSD/GPL");
Selvin Xavier1ac5a402017-02-10 03:19:33 -080073
74/* globals */
75static struct list_head bnxt_re_dev_list = LIST_HEAD_INIT(bnxt_re_dev_list);
76/* Mutex to protect the list of bnxt_re devices added */
77static DEFINE_MUTEX(bnxt_re_dev_lock);
78static struct workqueue_struct *bnxt_re_wq;
79
80/* for handling bnxt_en callbacks later */
81static void bnxt_re_stop(void *p)
82{
83}
84
85static void bnxt_re_start(void *p)
86{
87}
88
89static void bnxt_re_sriov_config(void *p, int num_vfs)
90{
91}
92
93static struct bnxt_ulp_ops bnxt_re_ulp_ops = {
94 .ulp_async_notifier = NULL,
95 .ulp_stop = bnxt_re_stop,
96 .ulp_start = bnxt_re_start,
97 .ulp_sriov_config = bnxt_re_sriov_config
98};
99
100/* RoCE -> Net driver */
101
102/* Driver registration routines used to let the networking driver (bnxt_en)
103 * to know that the RoCE driver is now installed
104 */
105static int bnxt_re_unregister_netdev(struct bnxt_re_dev *rdev, bool lock_wait)
106{
107 struct bnxt_en_dev *en_dev;
108 int rc;
109
110 if (!rdev)
111 return -EINVAL;
112
113 en_dev = rdev->en_dev;
114 /* Acquire rtnl lock if it is not invokded from netdev event */
115 if (lock_wait)
116 rtnl_lock();
117
118 rc = en_dev->en_ops->bnxt_unregister_device(rdev->en_dev,
119 BNXT_ROCE_ULP);
120 if (lock_wait)
121 rtnl_unlock();
122 return rc;
123}
124
125static int bnxt_re_register_netdev(struct bnxt_re_dev *rdev)
126{
127 struct bnxt_en_dev *en_dev;
128 int rc = 0;
129
130 if (!rdev)
131 return -EINVAL;
132
133 en_dev = rdev->en_dev;
134
135 rtnl_lock();
136 rc = en_dev->en_ops->bnxt_register_device(en_dev, BNXT_ROCE_ULP,
137 &bnxt_re_ulp_ops, rdev);
138 rtnl_unlock();
139 return rc;
140}
141
142static int bnxt_re_free_msix(struct bnxt_re_dev *rdev, bool lock_wait)
143{
144 struct bnxt_en_dev *en_dev;
145 int rc;
146
147 if (!rdev)
148 return -EINVAL;
149
150 en_dev = rdev->en_dev;
151
152 if (lock_wait)
153 rtnl_lock();
154
155 rc = en_dev->en_ops->bnxt_free_msix(rdev->en_dev, BNXT_ROCE_ULP);
156
157 if (lock_wait)
158 rtnl_unlock();
159 return rc;
160}
161
162static int bnxt_re_request_msix(struct bnxt_re_dev *rdev)
163{
164 int rc = 0, num_msix_want = BNXT_RE_MIN_MSIX, num_msix_got;
165 struct bnxt_en_dev *en_dev;
166
167 if (!rdev)
168 return -EINVAL;
169
170 en_dev = rdev->en_dev;
171
172 rtnl_lock();
173 num_msix_got = en_dev->en_ops->bnxt_request_msix(en_dev, BNXT_ROCE_ULP,
174 rdev->msix_entries,
175 num_msix_want);
176 if (num_msix_got < BNXT_RE_MIN_MSIX) {
177 rc = -EINVAL;
178 goto done;
179 }
180 if (num_msix_got != num_msix_want) {
181 dev_warn(rdev_to_dev(rdev),
182 "Requested %d MSI-X vectors, got %d\n",
183 num_msix_want, num_msix_got);
184 }
185 rdev->num_msix = num_msix_got;
186done:
187 rtnl_unlock();
188 return rc;
189}
190
191static void bnxt_re_init_hwrm_hdr(struct bnxt_re_dev *rdev, struct input *hdr,
192 u16 opcd, u16 crid, u16 trid)
193{
194 hdr->req_type = cpu_to_le16(opcd);
195 hdr->cmpl_ring = cpu_to_le16(crid);
196 hdr->target_id = cpu_to_le16(trid);
197}
198
199static void bnxt_re_fill_fw_msg(struct bnxt_fw_msg *fw_msg, void *msg,
200 int msg_len, void *resp, int resp_max_len,
201 int timeout)
202{
203 fw_msg->msg = msg;
204 fw_msg->msg_len = msg_len;
205 fw_msg->resp = resp;
206 fw_msg->resp_max_len = resp_max_len;
207 fw_msg->timeout = timeout;
208}
209
210static int bnxt_re_net_ring_free(struct bnxt_re_dev *rdev, u16 fw_ring_id,
211 bool lock_wait)
212{
213 struct bnxt_en_dev *en_dev = rdev->en_dev;
214 struct hwrm_ring_free_input req = {0};
215 struct hwrm_ring_free_output resp;
216 struct bnxt_fw_msg fw_msg;
217 bool do_unlock = false;
218 int rc = -EINVAL;
219
220 if (!en_dev)
221 return rc;
222
223 memset(&fw_msg, 0, sizeof(fw_msg));
224 if (lock_wait) {
225 rtnl_lock();
226 do_unlock = true;
227 }
228
229 bnxt_re_init_hwrm_hdr(rdev, (void *)&req, HWRM_RING_FREE, -1, -1);
Stephen Rothwelldb690322017-02-15 11:30:03 +1100230 req.ring_type = RING_ALLOC_REQ_RING_TYPE_L2_CMPL;
Selvin Xavier1ac5a402017-02-10 03:19:33 -0800231 req.ring_id = cpu_to_le16(fw_ring_id);
232 bnxt_re_fill_fw_msg(&fw_msg, (void *)&req, sizeof(req), (void *)&resp,
233 sizeof(resp), DFLT_HWRM_CMD_TIMEOUT);
234 rc = en_dev->en_ops->bnxt_send_fw_msg(en_dev, BNXT_ROCE_ULP, &fw_msg);
235 if (rc)
236 dev_err(rdev_to_dev(rdev),
237 "Failed to free HW ring:%d :%#x", req.ring_id, rc);
238 if (do_unlock)
239 rtnl_unlock();
240 return rc;
241}
242
243static int bnxt_re_net_ring_alloc(struct bnxt_re_dev *rdev, dma_addr_t *dma_arr,
244 int pages, int type, u32 ring_mask,
245 u32 map_index, u16 *fw_ring_id)
246{
247 struct bnxt_en_dev *en_dev = rdev->en_dev;
248 struct hwrm_ring_alloc_input req = {0};
249 struct hwrm_ring_alloc_output resp;
250 struct bnxt_fw_msg fw_msg;
251 int rc = -EINVAL;
252
253 if (!en_dev)
254 return rc;
255
256 memset(&fw_msg, 0, sizeof(fw_msg));
257 rtnl_lock();
258 bnxt_re_init_hwrm_hdr(rdev, (void *)&req, HWRM_RING_ALLOC, -1, -1);
259 req.enables = 0;
260 req.page_tbl_addr = cpu_to_le64(dma_arr[0]);
261 if (pages > 1) {
262 /* Page size is in log2 units */
263 req.page_size = BNXT_PAGE_SHIFT;
264 req.page_tbl_depth = 1;
265 }
266 req.fbo = 0;
267 /* Association of ring index with doorbell index and MSIX number */
268 req.logical_id = cpu_to_le16(map_index);
269 req.length = cpu_to_le32(ring_mask + 1);
Stephen Rothwelldb690322017-02-15 11:30:03 +1100270 req.ring_type = RING_ALLOC_REQ_RING_TYPE_L2_CMPL;
Selvin Xavier1ac5a402017-02-10 03:19:33 -0800271 req.int_mode = RING_ALLOC_REQ_INT_MODE_MSIX;
272 bnxt_re_fill_fw_msg(&fw_msg, (void *)&req, sizeof(req), (void *)&resp,
273 sizeof(resp), DFLT_HWRM_CMD_TIMEOUT);
274 rc = en_dev->en_ops->bnxt_send_fw_msg(en_dev, BNXT_ROCE_ULP, &fw_msg);
275 if (!rc)
276 *fw_ring_id = le16_to_cpu(resp.ring_id);
277
278 rtnl_unlock();
279 return rc;
280}
281
282static int bnxt_re_net_stats_ctx_free(struct bnxt_re_dev *rdev,
283 u32 fw_stats_ctx_id, bool lock_wait)
284{
285 struct bnxt_en_dev *en_dev = rdev->en_dev;
286 struct hwrm_stat_ctx_free_input req = {0};
287 struct bnxt_fw_msg fw_msg;
288 bool do_unlock = false;
289 int rc = -EINVAL;
290
291 if (!en_dev)
292 return rc;
293
294 memset(&fw_msg, 0, sizeof(fw_msg));
295 if (lock_wait) {
296 rtnl_lock();
297 do_unlock = true;
298 }
299
300 bnxt_re_init_hwrm_hdr(rdev, (void *)&req, HWRM_STAT_CTX_FREE, -1, -1);
301 req.stat_ctx_id = cpu_to_le32(fw_stats_ctx_id);
302 bnxt_re_fill_fw_msg(&fw_msg, (void *)&req, sizeof(req), (void *)&req,
303 sizeof(req), DFLT_HWRM_CMD_TIMEOUT);
304 rc = en_dev->en_ops->bnxt_send_fw_msg(en_dev, BNXT_ROCE_ULP, &fw_msg);
305 if (rc)
306 dev_err(rdev_to_dev(rdev),
307 "Failed to free HW stats context %#x", rc);
308
309 if (do_unlock)
310 rtnl_unlock();
311 return rc;
312}
313
314static int bnxt_re_net_stats_ctx_alloc(struct bnxt_re_dev *rdev,
315 dma_addr_t dma_map,
316 u32 *fw_stats_ctx_id)
317{
318 struct hwrm_stat_ctx_alloc_output resp = {0};
319 struct hwrm_stat_ctx_alloc_input req = {0};
320 struct bnxt_en_dev *en_dev = rdev->en_dev;
321 struct bnxt_fw_msg fw_msg;
322 int rc = -EINVAL;
323
324 *fw_stats_ctx_id = INVALID_STATS_CTX_ID;
325
326 if (!en_dev)
327 return rc;
328
329 memset(&fw_msg, 0, sizeof(fw_msg));
330 rtnl_lock();
331
332 bnxt_re_init_hwrm_hdr(rdev, (void *)&req, HWRM_STAT_CTX_ALLOC, -1, -1);
333 req.update_period_ms = cpu_to_le32(1000);
334 req.stats_dma_addr = cpu_to_le64(dma_map);
Somnath Kotur536f0922017-06-29 12:28:14 -0700335 req.stat_ctx_flags = STAT_CTX_ALLOC_REQ_STAT_CTX_FLAGS_ROCE;
Selvin Xavier1ac5a402017-02-10 03:19:33 -0800336 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
348static 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
363static 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
378static 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
385static 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
419static void bnxt_re_unregister_ib(struct bnxt_re_dev *rdev)
420{
421 ib_unregister_device(&rdev->ibdev);
422}
423
424static 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;
Linus Torvaldsac1820f2017-02-25 13:45:43 -0800439 ibdev->dev.parent = &rdev->en_dev->pdev->dev;
Selvin Xavier1ac5a402017-02-10 03:19:33 -0800440 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;
Selvin Xavier1ac5a402017-02-10 03:19:33 -0800476 ibdev->get_port_immutable = bnxt_re_get_port_immutable;
477 ibdev->query_pkey = bnxt_re_query_pkey;
478 ibdev->query_gid = bnxt_re_query_gid;
479 ibdev->get_netdev = bnxt_re_get_netdev;
480 ibdev->add_gid = bnxt_re_add_gid;
481 ibdev->del_gid = bnxt_re_del_gid;
482 ibdev->get_link_layer = bnxt_re_get_link_layer;
483
484 ibdev->alloc_pd = bnxt_re_alloc_pd;
485 ibdev->dealloc_pd = bnxt_re_dealloc_pd;
486
487 ibdev->create_ah = bnxt_re_create_ah;
488 ibdev->modify_ah = bnxt_re_modify_ah;
489 ibdev->query_ah = bnxt_re_query_ah;
490 ibdev->destroy_ah = bnxt_re_destroy_ah;
491
492 ibdev->create_qp = bnxt_re_create_qp;
493 ibdev->modify_qp = bnxt_re_modify_qp;
494 ibdev->query_qp = bnxt_re_query_qp;
495 ibdev->destroy_qp = bnxt_re_destroy_qp;
496
497 ibdev->post_send = bnxt_re_post_send;
498 ibdev->post_recv = bnxt_re_post_recv;
499
500 ibdev->create_cq = bnxt_re_create_cq;
501 ibdev->destroy_cq = bnxt_re_destroy_cq;
502 ibdev->poll_cq = bnxt_re_poll_cq;
503 ibdev->req_notify_cq = bnxt_re_req_notify_cq;
504
505 ibdev->get_dma_mr = bnxt_re_get_dma_mr;
506 ibdev->dereg_mr = bnxt_re_dereg_mr;
507 ibdev->alloc_mr = bnxt_re_alloc_mr;
508 ibdev->map_mr_sg = bnxt_re_map_mr_sg;
Selvin Xavier1ac5a402017-02-10 03:19:33 -0800509
510 ibdev->reg_user_mr = bnxt_re_reg_user_mr;
511 ibdev->alloc_ucontext = bnxt_re_alloc_ucontext;
512 ibdev->dealloc_ucontext = bnxt_re_dealloc_ucontext;
513 ibdev->mmap = bnxt_re_mmap;
514
515 return ib_register_device(ibdev, NULL);
516}
517
518static ssize_t show_rev(struct device *device, struct device_attribute *attr,
519 char *buf)
520{
521 struct bnxt_re_dev *rdev = to_bnxt_re_dev(device, ibdev.dev);
522
523 return scnprintf(buf, PAGE_SIZE, "0x%x\n", rdev->en_dev->pdev->vendor);
524}
525
526static ssize_t show_fw_ver(struct device *device, struct device_attribute *attr,
527 char *buf)
528{
529 struct bnxt_re_dev *rdev = to_bnxt_re_dev(device, ibdev.dev);
530
531 return scnprintf(buf, PAGE_SIZE, "%s\n", rdev->dev_attr.fw_ver);
532}
533
534static ssize_t show_hca(struct device *device, struct device_attribute *attr,
535 char *buf)
536{
537 struct bnxt_re_dev *rdev = to_bnxt_re_dev(device, ibdev.dev);
538
539 return scnprintf(buf, PAGE_SIZE, "%s\n", rdev->ibdev.node_desc);
540}
541
542static DEVICE_ATTR(hw_rev, 0444, show_rev, NULL);
543static DEVICE_ATTR(fw_rev, 0444, show_fw_ver, NULL);
544static DEVICE_ATTR(hca_type, 0444, show_hca, NULL);
545
546static struct device_attribute *bnxt_re_attributes[] = {
547 &dev_attr_hw_rev,
548 &dev_attr_fw_rev,
549 &dev_attr_hca_type
550};
551
552static void bnxt_re_dev_remove(struct bnxt_re_dev *rdev)
553{
554 dev_put(rdev->netdev);
555 rdev->netdev = NULL;
556
557 mutex_lock(&bnxt_re_dev_lock);
558 list_del_rcu(&rdev->list);
559 mutex_unlock(&bnxt_re_dev_lock);
560
561 synchronize_rcu();
562 flush_workqueue(bnxt_re_wq);
563
564 ib_dealloc_device(&rdev->ibdev);
565 /* rdev is gone */
566}
567
568static struct bnxt_re_dev *bnxt_re_dev_add(struct net_device *netdev,
569 struct bnxt_en_dev *en_dev)
570{
571 struct bnxt_re_dev *rdev;
572
573 /* Allocate bnxt_re_dev instance here */
574 rdev = (struct bnxt_re_dev *)ib_alloc_device(sizeof(*rdev));
575 if (!rdev) {
576 dev_err(NULL, "%s: bnxt_re_dev allocation failure!",
577 ROCE_DRV_MODULE_NAME);
578 return NULL;
579 }
580 /* Default values */
581 rdev->netdev = netdev;
582 dev_hold(rdev->netdev);
583 rdev->en_dev = en_dev;
584 rdev->id = rdev->en_dev->pdev->devfn;
585 INIT_LIST_HEAD(&rdev->qp_list);
586 mutex_init(&rdev->qp_lock);
587 atomic_set(&rdev->qp_count, 0);
588 atomic_set(&rdev->cq_count, 0);
589 atomic_set(&rdev->srq_count, 0);
590 atomic_set(&rdev->mr_count, 0);
591 atomic_set(&rdev->mw_count, 0);
592 rdev->cosq[0] = 0xFFFF;
593 rdev->cosq[1] = 0xFFFF;
594
595 mutex_lock(&bnxt_re_dev_lock);
596 list_add_tail_rcu(&rdev->list, &bnxt_re_dev_list);
597 mutex_unlock(&bnxt_re_dev_lock);
598 return rdev;
599}
600
601static int bnxt_re_aeq_handler(struct bnxt_qplib_rcfw *rcfw,
602 struct creq_func_event *aeqe)
603{
604 switch (aeqe->event) {
605 case CREQ_FUNC_EVENT_EVENT_TX_WQE_ERROR:
606 break;
607 case CREQ_FUNC_EVENT_EVENT_TX_DATA_ERROR:
608 break;
609 case CREQ_FUNC_EVENT_EVENT_RX_WQE_ERROR:
610 break;
611 case CREQ_FUNC_EVENT_EVENT_RX_DATA_ERROR:
612 break;
613 case CREQ_FUNC_EVENT_EVENT_CQ_ERROR:
614 break;
615 case CREQ_FUNC_EVENT_EVENT_TQM_ERROR:
616 break;
617 case CREQ_FUNC_EVENT_EVENT_CFCQ_ERROR:
618 break;
619 case CREQ_FUNC_EVENT_EVENT_CFCS_ERROR:
620 break;
621 case CREQ_FUNC_EVENT_EVENT_CFCC_ERROR:
622 break;
623 case CREQ_FUNC_EVENT_EVENT_CFCM_ERROR:
624 break;
625 case CREQ_FUNC_EVENT_EVENT_TIM_ERROR:
626 break;
627 default:
628 return -EINVAL;
629 }
630 return 0;
631}
632
633static int bnxt_re_cqn_handler(struct bnxt_qplib_nq *nq,
634 struct bnxt_qplib_cq *handle)
635{
636 struct bnxt_re_cq *cq = container_of(handle, struct bnxt_re_cq,
637 qplib_cq);
638
639 if (!cq) {
640 dev_err(NULL, "%s: CQ is NULL, CQN not handled",
641 ROCE_DRV_MODULE_NAME);
642 return -EINVAL;
643 }
644 if (cq->ib_cq.comp_handler) {
645 /* Lock comp_handler? */
646 (*cq->ib_cq.comp_handler)(&cq->ib_cq, cq->ib_cq.cq_context);
647 }
648
649 return 0;
650}
651
652static void bnxt_re_cleanup_res(struct bnxt_re_dev *rdev)
653{
654 if (rdev->nq.hwq.max_elements)
655 bnxt_qplib_disable_nq(&rdev->nq);
656
657 if (rdev->qplib_res.rcfw)
658 bnxt_qplib_cleanup_res(&rdev->qplib_res);
659}
660
661static int bnxt_re_init_res(struct bnxt_re_dev *rdev)
662{
663 int rc = 0;
664
665 bnxt_qplib_init_res(&rdev->qplib_res);
666
667 if (rdev->msix_entries[BNXT_RE_NQ_IDX].vector <= 0)
668 return -EINVAL;
669
670 rc = bnxt_qplib_enable_nq(rdev->en_dev->pdev, &rdev->nq,
671 rdev->msix_entries[BNXT_RE_NQ_IDX].vector,
672 rdev->msix_entries[BNXT_RE_NQ_IDX].db_offset,
673 &bnxt_re_cqn_handler,
674 NULL);
675
676 if (rc)
677 dev_err(rdev_to_dev(rdev), "Failed to enable NQ: %#x", rc);
678
679 return rc;
680}
681
682static void bnxt_re_free_res(struct bnxt_re_dev *rdev, bool lock_wait)
683{
684 if (rdev->nq.hwq.max_elements) {
685 bnxt_re_net_ring_free(rdev, rdev->nq.ring_id, lock_wait);
686 bnxt_qplib_free_nq(&rdev->nq);
687 }
688 if (rdev->qplib_res.dpi_tbl.max) {
689 bnxt_qplib_dealloc_dpi(&rdev->qplib_res,
690 &rdev->qplib_res.dpi_tbl,
691 &rdev->dpi_privileged);
692 }
693 if (rdev->qplib_res.rcfw) {
694 bnxt_qplib_free_res(&rdev->qplib_res);
695 rdev->qplib_res.rcfw = NULL;
696 }
697}
698
699static int bnxt_re_alloc_res(struct bnxt_re_dev *rdev)
700{
701 int rc = 0;
702
703 /* Configure and allocate resources for qplib */
704 rdev->qplib_res.rcfw = &rdev->rcfw;
705 rc = bnxt_qplib_get_dev_attr(&rdev->rcfw, &rdev->dev_attr);
706 if (rc)
707 goto fail;
708
709 rc = bnxt_qplib_alloc_res(&rdev->qplib_res, rdev->en_dev->pdev,
710 rdev->netdev, &rdev->dev_attr);
711 if (rc)
712 goto fail;
713
714 rc = bnxt_qplib_alloc_dpi(&rdev->qplib_res.dpi_tbl,
715 &rdev->dpi_privileged,
716 rdev);
717 if (rc)
718 goto fail;
719
720 rdev->nq.hwq.max_elements = BNXT_RE_MAX_CQ_COUNT +
721 BNXT_RE_MAX_SRQC_COUNT + 2;
722 rc = bnxt_qplib_alloc_nq(rdev->en_dev->pdev, &rdev->nq);
723 if (rc) {
724 dev_err(rdev_to_dev(rdev),
725 "Failed to allocate NQ memory: %#x", rc);
726 goto fail;
727 }
728 rc = bnxt_re_net_ring_alloc
729 (rdev, rdev->nq.hwq.pbl[PBL_LVL_0].pg_map_arr,
730 rdev->nq.hwq.pbl[rdev->nq.hwq.level].pg_count,
731 HWRM_RING_ALLOC_CMPL, BNXT_QPLIB_NQE_MAX_CNT - 1,
732 rdev->msix_entries[BNXT_RE_NQ_IDX].ring_idx,
733 &rdev->nq.ring_id);
734 if (rc) {
735 dev_err(rdev_to_dev(rdev),
736 "Failed to allocate NQ ring: %#x", rc);
737 goto free_nq;
738 }
739 return 0;
740free_nq:
741 bnxt_qplib_free_nq(&rdev->nq);
742fail:
743 rdev->qplib_res.rcfw = NULL;
744 return rc;
745}
746
747static void bnxt_re_dispatch_event(struct ib_device *ibdev, struct ib_qp *qp,
748 u8 port_num, enum ib_event_type event)
749{
750 struct ib_event ib_event;
751
752 ib_event.device = ibdev;
753 if (qp)
754 ib_event.element.qp = qp;
755 else
756 ib_event.element.port_num = port_num;
757 ib_event.event = event;
758 ib_dispatch_event(&ib_event);
759}
760
761#define HWRM_QUEUE_PRI2COS_QCFG_INPUT_FLAGS_IVLAN 0x02
762static int bnxt_re_query_hwrm_pri2cos(struct bnxt_re_dev *rdev, u8 dir,
763 u64 *cid_map)
764{
765 struct hwrm_queue_pri2cos_qcfg_input req = {0};
766 struct bnxt *bp = netdev_priv(rdev->netdev);
767 struct hwrm_queue_pri2cos_qcfg_output resp;
768 struct bnxt_en_dev *en_dev = rdev->en_dev;
769 struct bnxt_fw_msg fw_msg;
770 u32 flags = 0;
771 u8 *qcfgmap, *tmp_map;
772 int rc = 0, i;
773
774 if (!cid_map)
775 return -EINVAL;
776
777 memset(&fw_msg, 0, sizeof(fw_msg));
778 bnxt_re_init_hwrm_hdr(rdev, (void *)&req,
779 HWRM_QUEUE_PRI2COS_QCFG, -1, -1);
780 flags |= (dir & 0x01);
781 flags |= HWRM_QUEUE_PRI2COS_QCFG_INPUT_FLAGS_IVLAN;
782 req.flags = cpu_to_le32(flags);
783 req.port_id = bp->pf.port_id;
784
785 bnxt_re_fill_fw_msg(&fw_msg, (void *)&req, sizeof(req), (void *)&resp,
786 sizeof(resp), DFLT_HWRM_CMD_TIMEOUT);
787 rc = en_dev->en_ops->bnxt_send_fw_msg(en_dev, BNXT_ROCE_ULP, &fw_msg);
788 if (rc)
789 return rc;
790
791 if (resp.queue_cfg_info) {
792 dev_warn(rdev_to_dev(rdev),
793 "Asymmetric cos queue configuration detected");
794 dev_warn(rdev_to_dev(rdev),
795 " on device, QoS may not be fully functional\n");
796 }
797 qcfgmap = &resp.pri0_cos_queue_id;
798 tmp_map = (u8 *)cid_map;
799 for (i = 0; i < IEEE_8021QAZ_MAX_TCS; i++)
800 tmp_map[i] = qcfgmap[i];
801
802 return rc;
803}
804
805static bool bnxt_re_is_qp1_or_shadow_qp(struct bnxt_re_dev *rdev,
806 struct bnxt_re_qp *qp)
807{
808 return (qp->ib_qp.qp_type == IB_QPT_GSI) || (qp == rdev->qp1_sqp);
809}
810
811static void bnxt_re_dev_stop(struct bnxt_re_dev *rdev)
812{
813 int mask = IB_QP_STATE;
814 struct ib_qp_attr qp_attr;
815 struct bnxt_re_qp *qp;
816
817 qp_attr.qp_state = IB_QPS_ERR;
818 mutex_lock(&rdev->qp_lock);
819 list_for_each_entry(qp, &rdev->qp_list, list) {
820 /* Modify the state of all QPs except QP1/Shadow QP */
821 if (!bnxt_re_is_qp1_or_shadow_qp(rdev, qp)) {
822 if (qp->qplib_qp.state !=
823 CMDQ_MODIFY_QP_NEW_STATE_RESET &&
824 qp->qplib_qp.state !=
825 CMDQ_MODIFY_QP_NEW_STATE_ERR) {
826 bnxt_re_dispatch_event(&rdev->ibdev, &qp->ib_qp,
827 1, IB_EVENT_QP_FATAL);
828 bnxt_re_modify_qp(&qp->ib_qp, &qp_attr, mask,
829 NULL);
830 }
831 }
832 }
833 mutex_unlock(&rdev->qp_lock);
834}
835
Kalesh AP5fac5b12017-06-29 12:28:10 -0700836static int bnxt_re_update_gid(struct bnxt_re_dev *rdev)
837{
838 struct bnxt_qplib_sgid_tbl *sgid_tbl = &rdev->qplib_res.sgid_tbl;
839 struct bnxt_qplib_gid gid;
840 u16 gid_idx, index;
841 int rc = 0;
842
843 if (!test_bit(BNXT_RE_FLAG_IBDEV_REGISTERED, &rdev->flags))
844 return 0;
845
846 if (!sgid_tbl) {
847 dev_err(rdev_to_dev(rdev), "QPLIB: SGID table not allocated");
848 return -EINVAL;
849 }
850
851 for (index = 0; index < sgid_tbl->active; index++) {
852 gid_idx = sgid_tbl->hw_id[index];
853
854 if (!memcmp(&sgid_tbl->tbl[index], &bnxt_qplib_gid_zero,
855 sizeof(bnxt_qplib_gid_zero)))
856 continue;
857 /* need to modify the VLAN enable setting of non VLAN GID only
858 * as setting is done for VLAN GID while adding GID
859 */
860 if (sgid_tbl->vlan[index])
861 continue;
862
863 memcpy(&gid, &sgid_tbl->tbl[index], sizeof(gid));
864
865 rc = bnxt_qplib_update_sgid(sgid_tbl, &gid, gid_idx,
866 rdev->qplib_res.netdev->dev_addr);
867 }
868
869 return rc;
870}
871
Selvin Xavier1ac5a402017-02-10 03:19:33 -0800872static u32 bnxt_re_get_priority_mask(struct bnxt_re_dev *rdev)
873{
874 u32 prio_map = 0, tmp_map = 0;
875 struct net_device *netdev;
876 struct dcb_app app;
877
878 netdev = rdev->netdev;
879
880 memset(&app, 0, sizeof(app));
881 app.selector = IEEE_8021QAZ_APP_SEL_ETHERTYPE;
882 app.protocol = ETH_P_IBOE;
883 tmp_map = dcb_ieee_getapp_mask(netdev, &app);
884 prio_map = tmp_map;
885
886 app.selector = IEEE_8021QAZ_APP_SEL_DGRAM;
887 app.protocol = ROCE_V2_UDP_DPORT;
888 tmp_map = dcb_ieee_getapp_mask(netdev, &app);
889 prio_map |= tmp_map;
890
Selvin Xavier1ac5a402017-02-10 03:19:33 -0800891 return prio_map;
892}
893
894static void bnxt_re_parse_cid_map(u8 prio_map, u8 *cid_map, u16 *cosq)
895{
896 u16 prio;
897 u8 id;
898
899 for (prio = 0, id = 0; prio < 8; prio++) {
900 if (prio_map & (1 << prio)) {
901 cosq[id] = cid_map[prio];
902 id++;
903 if (id == 2) /* Max 2 tcs supported */
904 break;
905 }
906 }
907}
908
909static int bnxt_re_setup_qos(struct bnxt_re_dev *rdev)
910{
911 u8 prio_map = 0;
912 u64 cid_map;
913 int rc;
914
915 /* Get priority for roce */
Kalesh AP5fac5b12017-06-29 12:28:10 -0700916 prio_map = bnxt_re_get_priority_mask(rdev);
Selvin Xavier1ac5a402017-02-10 03:19:33 -0800917
918 if (prio_map == rdev->cur_prio_map)
919 return 0;
920 rdev->cur_prio_map = prio_map;
921 /* Get cosq id for this priority */
922 rc = bnxt_re_query_hwrm_pri2cos(rdev, 0, &cid_map);
923 if (rc) {
924 dev_warn(rdev_to_dev(rdev), "no cos for p_mask %x\n", prio_map);
925 return rc;
926 }
927 /* Parse CoS IDs for app priority */
928 bnxt_re_parse_cid_map(prio_map, (u8 *)&cid_map, rdev->cosq);
929
930 /* Config BONO. */
931 rc = bnxt_qplib_map_tc2cos(&rdev->qplib_res, rdev->cosq);
932 if (rc) {
933 dev_warn(rdev_to_dev(rdev), "no tc for cos{%x, %x}\n",
934 rdev->cosq[0], rdev->cosq[1]);
935 return rc;
936 }
937
Kalesh AP5fac5b12017-06-29 12:28:10 -0700938 /* Actual priorities are not programmed as they are already
939 * done by L2 driver; just enable or disable priority vlan tagging
940 */
941 if ((prio_map == 0 && rdev->qplib_res.prio) ||
942 (prio_map != 0 && !rdev->qplib_res.prio)) {
943 rdev->qplib_res.prio = prio_map ? true : false;
944
945 bnxt_re_update_gid(rdev);
946 }
947
Selvin Xavier1ac5a402017-02-10 03:19:33 -0800948 return 0;
949}
950
951static void bnxt_re_ib_unreg(struct bnxt_re_dev *rdev, bool lock_wait)
952{
953 int i, rc;
954
955 if (test_and_clear_bit(BNXT_RE_FLAG_IBDEV_REGISTERED, &rdev->flags)) {
956 for (i = 0; i < ARRAY_SIZE(bnxt_re_attributes); i++)
957 device_remove_file(&rdev->ibdev.dev,
958 bnxt_re_attributes[i]);
959 /* Cleanup ib dev */
960 bnxt_re_unregister_ib(rdev);
961 }
962 if (test_and_clear_bit(BNXT_RE_FLAG_QOS_WORK_REG, &rdev->flags))
963 cancel_delayed_work(&rdev->worker);
964
965 bnxt_re_cleanup_res(rdev);
966 bnxt_re_free_res(rdev, lock_wait);
967
968 if (test_and_clear_bit(BNXT_RE_FLAG_RCFW_CHANNEL_EN, &rdev->flags)) {
969 rc = bnxt_qplib_deinit_rcfw(&rdev->rcfw);
970 if (rc)
971 dev_warn(rdev_to_dev(rdev),
972 "Failed to deinitialize RCFW: %#x", rc);
973 bnxt_re_net_stats_ctx_free(rdev, rdev->qplib_ctx.stats.fw_id,
974 lock_wait);
975 bnxt_qplib_free_ctx(rdev->en_dev->pdev, &rdev->qplib_ctx);
976 bnxt_qplib_disable_rcfw_channel(&rdev->rcfw);
977 bnxt_re_net_ring_free(rdev, rdev->rcfw.creq_ring_id, lock_wait);
978 bnxt_qplib_free_rcfw_channel(&rdev->rcfw);
979 }
980 if (test_and_clear_bit(BNXT_RE_FLAG_GOT_MSIX, &rdev->flags)) {
981 rc = bnxt_re_free_msix(rdev, lock_wait);
982 if (rc)
983 dev_warn(rdev_to_dev(rdev),
984 "Failed to free MSI-X vectors: %#x", rc);
985 }
986 if (test_and_clear_bit(BNXT_RE_FLAG_NETDEV_REGISTERED, &rdev->flags)) {
987 rc = bnxt_re_unregister_netdev(rdev, lock_wait);
988 if (rc)
989 dev_warn(rdev_to_dev(rdev),
990 "Failed to unregister with netdev: %#x", rc);
991 }
992}
993
994static void bnxt_re_set_resource_limits(struct bnxt_re_dev *rdev)
995{
996 u32 i;
997
998 rdev->qplib_ctx.qpc_count = BNXT_RE_MAX_QPC_COUNT;
999 rdev->qplib_ctx.mrw_count = BNXT_RE_MAX_MRW_COUNT;
1000 rdev->qplib_ctx.srqc_count = BNXT_RE_MAX_SRQC_COUNT;
1001 rdev->qplib_ctx.cq_count = BNXT_RE_MAX_CQ_COUNT;
1002 for (i = 0; i < MAX_TQM_ALLOC_REQ; i++)
1003 rdev->qplib_ctx.tqm_count[i] =
1004 rdev->dev_attr.tqm_alloc_reqs[i];
1005}
1006
1007/* worker thread for polling periodic events. Now used for QoS programming*/
1008static void bnxt_re_worker(struct work_struct *work)
1009{
1010 struct bnxt_re_dev *rdev = container_of(work, struct bnxt_re_dev,
1011 worker.work);
1012
1013 bnxt_re_setup_qos(rdev);
1014 schedule_delayed_work(&rdev->worker, msecs_to_jiffies(30000));
1015}
1016
1017static int bnxt_re_ib_reg(struct bnxt_re_dev *rdev)
1018{
1019 int i, j, rc;
1020
1021 /* Registered a new RoCE device instance to netdev */
1022 rc = bnxt_re_register_netdev(rdev);
1023 if (rc) {
1024 pr_err("Failed to register with netedev: %#x\n", rc);
1025 return -EINVAL;
1026 }
1027 set_bit(BNXT_RE_FLAG_NETDEV_REGISTERED, &rdev->flags);
1028
1029 rc = bnxt_re_request_msix(rdev);
1030 if (rc) {
1031 pr_err("Failed to get MSI-X vectors: %#x\n", rc);
1032 rc = -EINVAL;
1033 goto fail;
1034 }
1035 set_bit(BNXT_RE_FLAG_GOT_MSIX, &rdev->flags);
1036
1037 /* Establish RCFW Communication Channel to initialize the context
1038 * memory for the function and all child VFs
1039 */
Selvin Xavierf218d672017-06-29 12:28:15 -07001040 rc = bnxt_qplib_alloc_rcfw_channel(rdev->en_dev->pdev, &rdev->rcfw,
1041 BNXT_RE_MAX_QPC_COUNT);
Selvin Xavier1ac5a402017-02-10 03:19:33 -08001042 if (rc)
1043 goto fail;
1044
1045 rc = bnxt_re_net_ring_alloc
1046 (rdev, rdev->rcfw.creq.pbl[PBL_LVL_0].pg_map_arr,
1047 rdev->rcfw.creq.pbl[rdev->rcfw.creq.level].pg_count,
1048 HWRM_RING_ALLOC_CMPL, BNXT_QPLIB_CREQE_MAX_CNT - 1,
1049 rdev->msix_entries[BNXT_RE_AEQ_IDX].ring_idx,
1050 &rdev->rcfw.creq_ring_id);
1051 if (rc) {
1052 pr_err("Failed to allocate CREQ: %#x\n", rc);
1053 goto free_rcfw;
1054 }
1055 rc = bnxt_qplib_enable_rcfw_channel
1056 (rdev->en_dev->pdev, &rdev->rcfw,
1057 rdev->msix_entries[BNXT_RE_AEQ_IDX].vector,
1058 rdev->msix_entries[BNXT_RE_AEQ_IDX].db_offset,
1059 0, &bnxt_re_aeq_handler);
1060 if (rc) {
1061 pr_err("Failed to enable RCFW channel: %#x\n", rc);
1062 goto free_ring;
1063 }
1064
1065 rc = bnxt_qplib_get_dev_attr(&rdev->rcfw, &rdev->dev_attr);
1066 if (rc)
1067 goto disable_rcfw;
1068 bnxt_re_set_resource_limits(rdev);
1069
1070 rc = bnxt_qplib_alloc_ctx(rdev->en_dev->pdev, &rdev->qplib_ctx, 0);
1071 if (rc) {
1072 pr_err("Failed to allocate QPLIB context: %#x\n", rc);
1073 goto disable_rcfw;
1074 }
1075 rc = bnxt_re_net_stats_ctx_alloc(rdev,
1076 rdev->qplib_ctx.stats.dma_map,
1077 &rdev->qplib_ctx.stats.fw_id);
1078 if (rc) {
1079 pr_err("Failed to allocate stats context: %#x\n", rc);
1080 goto free_ctx;
1081 }
1082
1083 rc = bnxt_qplib_init_rcfw(&rdev->rcfw, &rdev->qplib_ctx, 0);
1084 if (rc) {
1085 pr_err("Failed to initialize RCFW: %#x\n", rc);
1086 goto free_sctx;
1087 }
1088 set_bit(BNXT_RE_FLAG_RCFW_CHANNEL_EN, &rdev->flags);
1089
1090 /* Resources based on the 'new' device caps */
1091 rc = bnxt_re_alloc_res(rdev);
1092 if (rc) {
1093 pr_err("Failed to allocate resources: %#x\n", rc);
1094 goto fail;
1095 }
1096 rc = bnxt_re_init_res(rdev);
1097 if (rc) {
1098 pr_err("Failed to initialize resources: %#x\n", rc);
1099 goto fail;
1100 }
1101
1102 rc = bnxt_re_setup_qos(rdev);
1103 if (rc)
1104 pr_info("RoCE priority not yet configured\n");
1105
1106 INIT_DELAYED_WORK(&rdev->worker, bnxt_re_worker);
1107 set_bit(BNXT_RE_FLAG_QOS_WORK_REG, &rdev->flags);
1108 schedule_delayed_work(&rdev->worker, msecs_to_jiffies(30000));
1109
1110 /* Register ib dev */
1111 rc = bnxt_re_register_ib(rdev);
1112 if (rc) {
1113 pr_err("Failed to register with IB: %#x\n", rc);
1114 goto fail;
1115 }
1116 dev_info(rdev_to_dev(rdev), "Device registered successfully");
1117 for (i = 0; i < ARRAY_SIZE(bnxt_re_attributes); i++) {
1118 rc = device_create_file(&rdev->ibdev.dev,
1119 bnxt_re_attributes[i]);
1120 if (rc) {
1121 dev_err(rdev_to_dev(rdev),
1122 "Failed to create IB sysfs: %#x", rc);
1123 /* Must clean up all created device files */
1124 for (j = 0; j < i; j++)
1125 device_remove_file(&rdev->ibdev.dev,
1126 bnxt_re_attributes[j]);
1127 bnxt_re_unregister_ib(rdev);
1128 goto fail;
1129 }
1130 }
1131 set_bit(BNXT_RE_FLAG_IBDEV_REGISTERED, &rdev->flags);
1132 bnxt_re_dispatch_event(&rdev->ibdev, NULL, 1, IB_EVENT_PORT_ACTIVE);
1133 bnxt_re_dispatch_event(&rdev->ibdev, NULL, 1, IB_EVENT_GID_CHANGE);
1134
1135 return 0;
1136free_sctx:
1137 bnxt_re_net_stats_ctx_free(rdev, rdev->qplib_ctx.stats.fw_id, true);
1138free_ctx:
1139 bnxt_qplib_free_ctx(rdev->en_dev->pdev, &rdev->qplib_ctx);
1140disable_rcfw:
1141 bnxt_qplib_disable_rcfw_channel(&rdev->rcfw);
1142free_ring:
1143 bnxt_re_net_ring_free(rdev, rdev->rcfw.creq_ring_id, true);
1144free_rcfw:
1145 bnxt_qplib_free_rcfw_channel(&rdev->rcfw);
1146fail:
1147 bnxt_re_ib_unreg(rdev, true);
1148 return rc;
1149}
1150
1151static void bnxt_re_dev_unreg(struct bnxt_re_dev *rdev)
1152{
1153 struct bnxt_en_dev *en_dev = rdev->en_dev;
1154 struct net_device *netdev = rdev->netdev;
1155
1156 bnxt_re_dev_remove(rdev);
1157
1158 if (netdev)
1159 bnxt_re_dev_unprobe(netdev, en_dev);
1160}
1161
1162static int bnxt_re_dev_reg(struct bnxt_re_dev **rdev, struct net_device *netdev)
1163{
1164 struct bnxt_en_dev *en_dev;
1165 int rc = 0;
1166
1167 if (!is_bnxt_re_dev(netdev))
1168 return -ENODEV;
1169
1170 en_dev = bnxt_re_dev_probe(netdev);
1171 if (IS_ERR(en_dev)) {
1172 if (en_dev != ERR_PTR(-ENODEV))
1173 pr_err("%s: Failed to probe\n", ROCE_DRV_MODULE_NAME);
1174 rc = PTR_ERR(en_dev);
1175 goto exit;
1176 }
1177 *rdev = bnxt_re_dev_add(netdev, en_dev);
1178 if (!*rdev) {
1179 rc = -ENOMEM;
1180 bnxt_re_dev_unprobe(netdev, en_dev);
1181 goto exit;
1182 }
1183exit:
1184 return rc;
1185}
1186
1187static void bnxt_re_remove_one(struct bnxt_re_dev *rdev)
1188{
1189 pci_dev_put(rdev->en_dev->pdev);
1190}
1191
1192/* Handle all deferred netevents tasks */
1193static void bnxt_re_task(struct work_struct *work)
1194{
1195 struct bnxt_re_work *re_work;
1196 struct bnxt_re_dev *rdev;
1197 int rc = 0;
1198
1199 re_work = container_of(work, struct bnxt_re_work, work);
1200 rdev = re_work->rdev;
1201
1202 if (re_work->event != NETDEV_REGISTER &&
1203 !test_bit(BNXT_RE_FLAG_IBDEV_REGISTERED, &rdev->flags))
1204 return;
1205
1206 switch (re_work->event) {
1207 case NETDEV_REGISTER:
1208 rc = bnxt_re_ib_reg(rdev);
1209 if (rc)
1210 dev_err(rdev_to_dev(rdev),
1211 "Failed to register with IB: %#x", rc);
1212 break;
1213 case NETDEV_UP:
1214 bnxt_re_dispatch_event(&rdev->ibdev, NULL, 1,
1215 IB_EVENT_PORT_ACTIVE);
1216 break;
1217 case NETDEV_DOWN:
1218 bnxt_re_dev_stop(rdev);
1219 break;
1220 case NETDEV_CHANGE:
1221 if (!netif_carrier_ok(rdev->netdev))
1222 bnxt_re_dev_stop(rdev);
1223 else if (netif_carrier_ok(rdev->netdev))
1224 bnxt_re_dispatch_event(&rdev->ibdev, NULL, 1,
1225 IB_EVENT_PORT_ACTIVE);
1226 break;
1227 default:
1228 break;
1229 }
1230 kfree(re_work);
1231}
1232
1233static void bnxt_re_init_one(struct bnxt_re_dev *rdev)
1234{
1235 pci_dev_get(rdev->en_dev->pdev);
1236}
1237
1238/*
1239 * "Notifier chain callback can be invoked for the same chain from
1240 * different CPUs at the same time".
1241 *
1242 * For cases when the netdev is already present, our call to the
1243 * register_netdevice_notifier() will actually get the rtnl_lock()
1244 * before sending NETDEV_REGISTER and (if up) NETDEV_UP
1245 * events.
1246 *
1247 * But for cases when the netdev is not already present, the notifier
1248 * chain is subjected to be invoked from different CPUs simultaneously.
1249 *
1250 * This is protected by the netdev_mutex.
1251 */
1252static int bnxt_re_netdev_event(struct notifier_block *notifier,
1253 unsigned long event, void *ptr)
1254{
1255 struct net_device *real_dev, *netdev = netdev_notifier_info_to_dev(ptr);
1256 struct bnxt_re_work *re_work;
1257 struct bnxt_re_dev *rdev;
1258 int rc = 0;
1259 bool sch_work = false;
1260
1261 real_dev = rdma_vlan_dev_real_dev(netdev);
1262 if (!real_dev)
1263 real_dev = netdev;
1264
1265 rdev = bnxt_re_from_netdev(real_dev);
1266 if (!rdev && event != NETDEV_REGISTER)
1267 goto exit;
1268 if (real_dev != netdev)
1269 goto exit;
1270
1271 switch (event) {
1272 case NETDEV_REGISTER:
1273 if (rdev)
1274 break;
1275 rc = bnxt_re_dev_reg(&rdev, real_dev);
1276 if (rc == -ENODEV)
1277 break;
1278 if (rc) {
1279 pr_err("Failed to register with the device %s: %#x\n",
1280 real_dev->name, rc);
1281 break;
1282 }
1283 bnxt_re_init_one(rdev);
1284 sch_work = true;
1285 break;
1286
1287 case NETDEV_UNREGISTER:
1288 bnxt_re_ib_unreg(rdev, false);
1289 bnxt_re_remove_one(rdev);
1290 bnxt_re_dev_unreg(rdev);
1291 break;
1292
1293 default:
1294 sch_work = true;
1295 break;
1296 }
1297 if (sch_work) {
1298 /* Allocate for the deferred task */
1299 re_work = kzalloc(sizeof(*re_work), GFP_ATOMIC);
1300 if (re_work) {
1301 re_work->rdev = rdev;
1302 re_work->event = event;
1303 re_work->vlan_dev = (real_dev == netdev ?
1304 NULL : netdev);
1305 INIT_WORK(&re_work->work, bnxt_re_task);
1306 queue_work(bnxt_re_wq, &re_work->work);
1307 }
1308 }
1309
1310exit:
1311 return NOTIFY_DONE;
1312}
1313
1314static struct notifier_block bnxt_re_netdev_notifier = {
1315 .notifier_call = bnxt_re_netdev_event
1316};
1317
1318static int __init bnxt_re_mod_init(void)
1319{
1320 int rc = 0;
1321
1322 pr_info("%s: %s", ROCE_DRV_MODULE_NAME, version);
1323
1324 bnxt_re_wq = create_singlethread_workqueue("bnxt_re");
1325 if (!bnxt_re_wq)
1326 return -ENOMEM;
1327
1328 INIT_LIST_HEAD(&bnxt_re_dev_list);
1329
1330 rc = register_netdevice_notifier(&bnxt_re_netdev_notifier);
1331 if (rc) {
1332 pr_err("%s: Cannot register to netdevice_notifier",
1333 ROCE_DRV_MODULE_NAME);
1334 goto err_netdev;
1335 }
1336 return 0;
1337
1338err_netdev:
1339 destroy_workqueue(bnxt_re_wq);
1340
1341 return rc;
1342}
1343
1344static void __exit bnxt_re_mod_exit(void)
1345{
1346 unregister_netdevice_notifier(&bnxt_re_netdev_notifier);
1347 if (bnxt_re_wq)
1348 destroy_workqueue(bnxt_re_wq);
1349}
1350
1351module_init(bnxt_re_mod_init);
1352module_exit(bnxt_re_mod_exit);