2 * Broadcom NetXtreme-E RoCE driver.
4 * Copyright (c) 2016 - 2017, Broadcom. All rights reserved. The term
5 * Broadcom refers to Broadcom Limited and/or its subsidiaries.
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
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
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
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.
36 * Description: Main component of the bnxt_re driver
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>
49 #include <net/addrconf.h>
50 #include <linux/if_ether.h>
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>
59 #include "qplib_res.h"
62 #include "qplib_rcfw.h"
65 #include <rdma/bnxt_re-abi.h>
67 #include "hw_counters.h"
69 static char version[] =
72 MODULE_AUTHOR("Eddie Wai <eddie.wai@broadcom.com>");
73 MODULE_DESCRIPTION(BNXT_RE_DESC " Driver");
74 MODULE_LICENSE("Dual BSD/GPL");
77 static struct list_head bnxt_re_dev_list = LIST_HEAD_INIT(bnxt_re_dev_list);
78 /* Mutex to protect the list of bnxt_re devices added */
79 static DEFINE_MUTEX(bnxt_re_dev_lock);
80 static struct workqueue_struct *bnxt_re_wq;
81 static void bnxt_re_ib_unreg(struct bnxt_re_dev *rdev);
83 static void bnxt_re_destroy_chip_ctx(struct bnxt_re_dev *rdev)
85 rdev->rcfw.res = NULL;
86 rdev->qplib_res.cctx = NULL;
89 static int bnxt_re_setup_chip_ctx(struct bnxt_re_dev *rdev)
91 struct bnxt_en_dev *en_dev;
94 en_dev = rdev->en_dev;
95 bp = netdev_priv(en_dev->net);
97 rdev->chip_ctx.chip_num = bp->chip_num;
98 /* rest members to follow eventually */
100 rdev->qplib_res.cctx = &rdev->chip_ctx;
101 rdev->rcfw.res = &rdev->qplib_res;
106 /* SR-IOV helper functions */
108 static void bnxt_re_get_sriov_func_type(struct bnxt_re_dev *rdev)
112 bp = netdev_priv(rdev->en_dev->net);
117 /* Set the maximum number of each resource that the driver actually wants
118 * to allocate. This may be up to the maximum number the firmware has
119 * reserved for the function. The driver may choose to allocate fewer
120 * resources than the firmware maximum.
122 static void bnxt_re_set_resource_limits(struct bnxt_re_dev *rdev)
124 u32 vf_qps = 0, vf_srqs = 0, vf_cqs = 0, vf_mrws = 0, vf_gids = 0;
128 struct bnxt_qplib_dev_attr *dev_attr = &rdev->dev_attr;
130 rdev->qplib_ctx.qpc_count = min_t(u32, BNXT_RE_MAX_QPC_COUNT,
133 rdev->qplib_ctx.mrw_count = BNXT_RE_MAX_MRW_COUNT_256K;
134 /* Use max_mr from fw since max_mrw does not get set */
135 rdev->qplib_ctx.mrw_count = min_t(u32, rdev->qplib_ctx.mrw_count,
137 rdev->qplib_ctx.srqc_count = min_t(u32, BNXT_RE_MAX_SRQC_COUNT,
139 rdev->qplib_ctx.cq_count = min_t(u32, BNXT_RE_MAX_CQ_COUNT,
142 for (i = 0; i < MAX_TQM_ALLOC_REQ; i++)
143 rdev->qplib_ctx.tqm_count[i] =
144 rdev->dev_attr.tqm_alloc_reqs[i];
148 * Reserve a set of resources for the PF. Divide the remaining
149 * resources among the VFs
151 vf_pct = 100 - BNXT_RE_PCT_RSVD_FOR_PF;
152 num_vfs = 100 * rdev->num_vfs;
153 vf_qps = (rdev->qplib_ctx.qpc_count * vf_pct) / num_vfs;
154 vf_srqs = (rdev->qplib_ctx.srqc_count * vf_pct) / num_vfs;
155 vf_cqs = (rdev->qplib_ctx.cq_count * vf_pct) / num_vfs;
157 * The driver allows many more MRs than other resources. If the
158 * firmware does also, then reserve a fixed amount for the PF
159 * and divide the rest among VFs. VFs may use many MRs for NFS
160 * mounts, ISER, NVME applications, etc. If the firmware
161 * severely restricts the number of MRs, then let PF have
162 * half and divide the rest among VFs, as for the other
165 if (rdev->qplib_ctx.mrw_count < BNXT_RE_MAX_MRW_COUNT_64K)
166 vf_mrws = rdev->qplib_ctx.mrw_count * vf_pct / num_vfs;
168 vf_mrws = (rdev->qplib_ctx.mrw_count -
169 BNXT_RE_RESVD_MR_FOR_PF) / rdev->num_vfs;
170 vf_gids = BNXT_RE_MAX_GID_PER_VF;
172 rdev->qplib_ctx.vf_res.max_mrw_per_vf = vf_mrws;
173 rdev->qplib_ctx.vf_res.max_gid_per_vf = vf_gids;
174 rdev->qplib_ctx.vf_res.max_qp_per_vf = vf_qps;
175 rdev->qplib_ctx.vf_res.max_srq_per_vf = vf_srqs;
176 rdev->qplib_ctx.vf_res.max_cq_per_vf = vf_cqs;
179 /* for handling bnxt_en callbacks later */
180 static void bnxt_re_stop(void *p)
184 static void bnxt_re_start(void *p)
188 static void bnxt_re_sriov_config(void *p, int num_vfs)
190 struct bnxt_re_dev *rdev = p;
195 rdev->num_vfs = num_vfs;
196 bnxt_re_set_resource_limits(rdev);
197 bnxt_qplib_set_func_resources(&rdev->qplib_res, &rdev->rcfw,
201 static void bnxt_re_shutdown(void *p)
203 struct bnxt_re_dev *rdev = p;
208 bnxt_re_ib_unreg(rdev);
211 static void bnxt_re_stop_irq(void *handle)
213 struct bnxt_re_dev *rdev = (struct bnxt_re_dev *)handle;
214 struct bnxt_qplib_rcfw *rcfw = &rdev->rcfw;
215 struct bnxt_qplib_nq *nq;
218 for (indx = BNXT_RE_NQ_IDX; indx < rdev->num_msix; indx++) {
219 nq = &rdev->nq[indx - 1];
220 bnxt_qplib_nq_stop_irq(nq, false);
223 bnxt_qplib_rcfw_stop_irq(rcfw, false);
226 static void bnxt_re_start_irq(void *handle, struct bnxt_msix_entry *ent)
228 struct bnxt_re_dev *rdev = (struct bnxt_re_dev *)handle;
229 struct bnxt_msix_entry *msix_ent = rdev->msix_entries;
230 struct bnxt_qplib_rcfw *rcfw = &rdev->rcfw;
231 struct bnxt_qplib_nq *nq;
235 /* Not setting the f/w timeout bit in rcfw.
236 * During the driver unload the first command
237 * to f/w will timeout and that will set the
240 dev_err(rdev_to_dev(rdev), "Failed to re-start IRQs\n");
244 /* Vectors may change after restart, so update with new vectors
245 * in device sctructure.
247 for (indx = 0; indx < rdev->num_msix; indx++)
248 rdev->msix_entries[indx].vector = ent[indx].vector;
250 bnxt_qplib_rcfw_start_irq(rcfw, msix_ent[BNXT_RE_AEQ_IDX].vector,
252 for (indx = BNXT_RE_NQ_IDX ; indx < rdev->num_msix; indx++) {
253 nq = &rdev->nq[indx - 1];
254 rc = bnxt_qplib_nq_start_irq(nq, indx - 1,
255 msix_ent[indx].vector, false);
257 dev_warn(rdev_to_dev(rdev),
258 "Failed to reinit NQ index %d\n", indx - 1);
262 static struct bnxt_ulp_ops bnxt_re_ulp_ops = {
263 .ulp_async_notifier = NULL,
264 .ulp_stop = bnxt_re_stop,
265 .ulp_start = bnxt_re_start,
266 .ulp_sriov_config = bnxt_re_sriov_config,
267 .ulp_shutdown = bnxt_re_shutdown,
268 .ulp_irq_stop = bnxt_re_stop_irq,
269 .ulp_irq_restart = bnxt_re_start_irq
272 /* RoCE -> Net driver */
274 /* Driver registration routines used to let the networking driver (bnxt_en)
275 * to know that the RoCE driver is now installed
277 static int bnxt_re_unregister_netdev(struct bnxt_re_dev *rdev)
279 struct bnxt_en_dev *en_dev;
285 en_dev = rdev->en_dev;
287 rc = en_dev->en_ops->bnxt_unregister_device(rdev->en_dev,
292 static int bnxt_re_register_netdev(struct bnxt_re_dev *rdev)
294 struct bnxt_en_dev *en_dev;
300 en_dev = rdev->en_dev;
302 rc = en_dev->en_ops->bnxt_register_device(en_dev, BNXT_ROCE_ULP,
303 &bnxt_re_ulp_ops, rdev);
304 rdev->qplib_res.pdev = rdev->en_dev->pdev;
308 static int bnxt_re_free_msix(struct bnxt_re_dev *rdev)
310 struct bnxt_en_dev *en_dev;
316 en_dev = rdev->en_dev;
319 rc = en_dev->en_ops->bnxt_free_msix(rdev->en_dev, BNXT_ROCE_ULP);
324 static int bnxt_re_request_msix(struct bnxt_re_dev *rdev)
326 int rc = 0, num_msix_want = BNXT_RE_MAX_MSIX, num_msix_got;
327 struct bnxt_en_dev *en_dev;
332 en_dev = rdev->en_dev;
334 num_msix_want = min_t(u32, BNXT_RE_MAX_MSIX, num_online_cpus());
336 num_msix_got = en_dev->en_ops->bnxt_request_msix(en_dev, BNXT_ROCE_ULP,
339 if (num_msix_got < BNXT_RE_MIN_MSIX) {
343 if (num_msix_got != num_msix_want) {
344 dev_warn(rdev_to_dev(rdev),
345 "Requested %d MSI-X vectors, got %d\n",
346 num_msix_want, num_msix_got);
348 rdev->num_msix = num_msix_got;
353 static void bnxt_re_init_hwrm_hdr(struct bnxt_re_dev *rdev, struct input *hdr,
354 u16 opcd, u16 crid, u16 trid)
356 hdr->req_type = cpu_to_le16(opcd);
357 hdr->cmpl_ring = cpu_to_le16(crid);
358 hdr->target_id = cpu_to_le16(trid);
361 static void bnxt_re_fill_fw_msg(struct bnxt_fw_msg *fw_msg, void *msg,
362 int msg_len, void *resp, int resp_max_len,
366 fw_msg->msg_len = msg_len;
368 fw_msg->resp_max_len = resp_max_len;
369 fw_msg->timeout = timeout;
372 static int bnxt_re_net_ring_free(struct bnxt_re_dev *rdev,
373 u16 fw_ring_id, int type)
375 struct bnxt_en_dev *en_dev = rdev->en_dev;
376 struct hwrm_ring_free_input req = {0};
377 struct hwrm_ring_free_output resp;
378 struct bnxt_fw_msg fw_msg;
384 memset(&fw_msg, 0, sizeof(fw_msg));
386 bnxt_re_init_hwrm_hdr(rdev, (void *)&req, HWRM_RING_FREE, -1, -1);
387 req.ring_type = type;
388 req.ring_id = cpu_to_le16(fw_ring_id);
389 bnxt_re_fill_fw_msg(&fw_msg, (void *)&req, sizeof(req), (void *)&resp,
390 sizeof(resp), DFLT_HWRM_CMD_TIMEOUT);
391 rc = en_dev->en_ops->bnxt_send_fw_msg(en_dev, BNXT_ROCE_ULP, &fw_msg);
393 dev_err(rdev_to_dev(rdev),
394 "Failed to free HW ring:%d :%#x", req.ring_id, rc);
398 static int bnxt_re_net_ring_alloc(struct bnxt_re_dev *rdev, dma_addr_t *dma_arr,
399 int pages, int type, u32 ring_mask,
400 u32 map_index, u16 *fw_ring_id)
402 struct bnxt_en_dev *en_dev = rdev->en_dev;
403 struct hwrm_ring_alloc_input req = {0};
404 struct hwrm_ring_alloc_output resp;
405 struct bnxt_fw_msg fw_msg;
411 memset(&fw_msg, 0, sizeof(fw_msg));
412 bnxt_re_init_hwrm_hdr(rdev, (void *)&req, HWRM_RING_ALLOC, -1, -1);
414 req.page_tbl_addr = cpu_to_le64(dma_arr[0]);
416 /* Page size is in log2 units */
417 req.page_size = BNXT_PAGE_SHIFT;
418 req.page_tbl_depth = 1;
421 /* Association of ring index with doorbell index and MSIX number */
422 req.logical_id = cpu_to_le16(map_index);
423 req.length = cpu_to_le32(ring_mask + 1);
424 req.ring_type = type;
425 req.int_mode = RING_ALLOC_REQ_INT_MODE_MSIX;
426 bnxt_re_fill_fw_msg(&fw_msg, (void *)&req, sizeof(req), (void *)&resp,
427 sizeof(resp), DFLT_HWRM_CMD_TIMEOUT);
428 rc = en_dev->en_ops->bnxt_send_fw_msg(en_dev, BNXT_ROCE_ULP, &fw_msg);
430 *fw_ring_id = le16_to_cpu(resp.ring_id);
435 static int bnxt_re_net_stats_ctx_free(struct bnxt_re_dev *rdev,
438 struct bnxt_en_dev *en_dev = rdev->en_dev;
439 struct hwrm_stat_ctx_free_input req = {0};
440 struct bnxt_fw_msg fw_msg;
446 memset(&fw_msg, 0, sizeof(fw_msg));
448 bnxt_re_init_hwrm_hdr(rdev, (void *)&req, HWRM_STAT_CTX_FREE, -1, -1);
449 req.stat_ctx_id = cpu_to_le32(fw_stats_ctx_id);
450 bnxt_re_fill_fw_msg(&fw_msg, (void *)&req, sizeof(req), (void *)&req,
451 sizeof(req), DFLT_HWRM_CMD_TIMEOUT);
452 rc = en_dev->en_ops->bnxt_send_fw_msg(en_dev, BNXT_ROCE_ULP, &fw_msg);
454 dev_err(rdev_to_dev(rdev),
455 "Failed to free HW stats context %#x", rc);
460 static int bnxt_re_net_stats_ctx_alloc(struct bnxt_re_dev *rdev,
462 u32 *fw_stats_ctx_id)
464 struct hwrm_stat_ctx_alloc_output resp = {0};
465 struct hwrm_stat_ctx_alloc_input req = {0};
466 struct bnxt_en_dev *en_dev = rdev->en_dev;
467 struct bnxt_fw_msg fw_msg;
470 *fw_stats_ctx_id = INVALID_STATS_CTX_ID;
475 memset(&fw_msg, 0, sizeof(fw_msg));
477 bnxt_re_init_hwrm_hdr(rdev, (void *)&req, HWRM_STAT_CTX_ALLOC, -1, -1);
478 req.update_period_ms = cpu_to_le32(1000);
479 req.stats_dma_addr = cpu_to_le64(dma_map);
480 req.stat_ctx_flags = STAT_CTX_ALLOC_REQ_STAT_CTX_FLAGS_ROCE;
481 bnxt_re_fill_fw_msg(&fw_msg, (void *)&req, sizeof(req), (void *)&resp,
482 sizeof(resp), DFLT_HWRM_CMD_TIMEOUT);
483 rc = en_dev->en_ops->bnxt_send_fw_msg(en_dev, BNXT_ROCE_ULP, &fw_msg);
485 *fw_stats_ctx_id = le32_to_cpu(resp.stat_ctx_id);
492 static bool is_bnxt_re_dev(struct net_device *netdev)
494 struct ethtool_drvinfo drvinfo;
496 if (netdev->ethtool_ops && netdev->ethtool_ops->get_drvinfo) {
497 memset(&drvinfo, 0, sizeof(drvinfo));
498 netdev->ethtool_ops->get_drvinfo(netdev, &drvinfo);
500 if (strcmp(drvinfo.driver, "bnxt_en"))
507 static struct bnxt_re_dev *bnxt_re_from_netdev(struct net_device *netdev)
509 struct bnxt_re_dev *rdev;
512 list_for_each_entry_rcu(rdev, &bnxt_re_dev_list, list) {
513 if (rdev->netdev == netdev) {
522 static void bnxt_re_dev_unprobe(struct net_device *netdev,
523 struct bnxt_en_dev *en_dev)
526 module_put(en_dev->pdev->driver->driver.owner);
529 static struct bnxt_en_dev *bnxt_re_dev_probe(struct net_device *netdev)
531 struct bnxt *bp = netdev_priv(netdev);
532 struct bnxt_en_dev *en_dev;
533 struct pci_dev *pdev;
535 /* Call bnxt_en's RoCE probe via indirect API */
537 return ERR_PTR(-EINVAL);
539 en_dev = bp->ulp_probe(netdev);
545 return ERR_PTR(-EINVAL);
547 if (!(en_dev->flags & BNXT_EN_FLAG_ROCE_CAP)) {
549 "%s: probe error: RoCE is not supported on this device",
550 ROCE_DRV_MODULE_NAME);
551 return ERR_PTR(-ENODEV);
554 /* Bump net device reference count */
555 if (!try_module_get(pdev->driver->driver.owner))
556 return ERR_PTR(-ENODEV);
563 static ssize_t hw_rev_show(struct device *device, struct device_attribute *attr,
566 struct bnxt_re_dev *rdev =
567 rdma_device_to_drv_device(device, struct bnxt_re_dev, ibdev);
569 return scnprintf(buf, PAGE_SIZE, "0x%x\n", rdev->en_dev->pdev->vendor);
571 static DEVICE_ATTR_RO(hw_rev);
573 static ssize_t hca_type_show(struct device *device,
574 struct device_attribute *attr, char *buf)
576 struct bnxt_re_dev *rdev =
577 rdma_device_to_drv_device(device, struct bnxt_re_dev, ibdev);
579 return scnprintf(buf, PAGE_SIZE, "%s\n", rdev->ibdev.node_desc);
581 static DEVICE_ATTR_RO(hca_type);
583 static struct attribute *bnxt_re_attributes[] = {
584 &dev_attr_hw_rev.attr,
585 &dev_attr_hca_type.attr,
589 static const struct attribute_group bnxt_re_dev_attr_group = {
590 .attrs = bnxt_re_attributes,
593 static void bnxt_re_unregister_ib(struct bnxt_re_dev *rdev)
595 ib_unregister_device(&rdev->ibdev);
598 static const struct ib_device_ops bnxt_re_dev_ops = {
599 .add_gid = bnxt_re_add_gid,
600 .alloc_hw_stats = bnxt_re_ib_alloc_hw_stats,
601 .alloc_mr = bnxt_re_alloc_mr,
602 .alloc_pd = bnxt_re_alloc_pd,
603 .alloc_ucontext = bnxt_re_alloc_ucontext,
604 .create_ah = bnxt_re_create_ah,
605 .create_cq = bnxt_re_create_cq,
606 .create_qp = bnxt_re_create_qp,
607 .create_srq = bnxt_re_create_srq,
608 .dealloc_pd = bnxt_re_dealloc_pd,
609 .dealloc_ucontext = bnxt_re_dealloc_ucontext,
610 .del_gid = bnxt_re_del_gid,
611 .dereg_mr = bnxt_re_dereg_mr,
612 .destroy_ah = bnxt_re_destroy_ah,
613 .destroy_cq = bnxt_re_destroy_cq,
614 .destroy_qp = bnxt_re_destroy_qp,
615 .destroy_srq = bnxt_re_destroy_srq,
616 .get_dev_fw_str = bnxt_re_query_fw_str,
617 .get_dma_mr = bnxt_re_get_dma_mr,
618 .get_hw_stats = bnxt_re_ib_get_hw_stats,
619 .get_link_layer = bnxt_re_get_link_layer,
620 .get_port_immutable = bnxt_re_get_port_immutable,
621 .map_mr_sg = bnxt_re_map_mr_sg,
622 .mmap = bnxt_re_mmap,
623 .modify_ah = bnxt_re_modify_ah,
624 .modify_device = bnxt_re_modify_device,
625 .modify_qp = bnxt_re_modify_qp,
626 .modify_srq = bnxt_re_modify_srq,
627 .poll_cq = bnxt_re_poll_cq,
628 .post_recv = bnxt_re_post_recv,
629 .post_send = bnxt_re_post_send,
630 .post_srq_recv = bnxt_re_post_srq_recv,
631 .query_ah = bnxt_re_query_ah,
632 .query_device = bnxt_re_query_device,
633 .query_pkey = bnxt_re_query_pkey,
634 .query_port = bnxt_re_query_port,
635 .query_qp = bnxt_re_query_qp,
636 .query_srq = bnxt_re_query_srq,
637 .reg_user_mr = bnxt_re_reg_user_mr,
638 .req_notify_cq = bnxt_re_req_notify_cq,
639 INIT_RDMA_OBJ_SIZE(ib_ah, bnxt_re_ah, ib_ah),
640 INIT_RDMA_OBJ_SIZE(ib_pd, bnxt_re_pd, ib_pd),
641 INIT_RDMA_OBJ_SIZE(ib_srq, bnxt_re_srq, ib_srq),
642 INIT_RDMA_OBJ_SIZE(ib_ucontext, bnxt_re_ucontext, ib_uctx),
645 static int bnxt_re_register_ib(struct bnxt_re_dev *rdev)
647 struct ib_device *ibdev = &rdev->ibdev;
651 ibdev->owner = THIS_MODULE;
652 ibdev->node_type = RDMA_NODE_IB_CA;
653 strlcpy(ibdev->node_desc, BNXT_RE_DESC " HCA",
654 strlen(BNXT_RE_DESC) + 5);
655 ibdev->phys_port_cnt = 1;
657 bnxt_qplib_get_guid(rdev->netdev->dev_addr, (u8 *)&ibdev->node_guid);
659 ibdev->num_comp_vectors = 1;
660 ibdev->dev.parent = &rdev->en_dev->pdev->dev;
661 ibdev->local_dma_lkey = BNXT_QPLIB_RSVD_LKEY;
664 ibdev->uverbs_abi_ver = BNXT_RE_ABI_VERSION;
665 ibdev->uverbs_cmd_mask =
666 (1ull << IB_USER_VERBS_CMD_GET_CONTEXT) |
667 (1ull << IB_USER_VERBS_CMD_QUERY_DEVICE) |
668 (1ull << IB_USER_VERBS_CMD_QUERY_PORT) |
669 (1ull << IB_USER_VERBS_CMD_ALLOC_PD) |
670 (1ull << IB_USER_VERBS_CMD_DEALLOC_PD) |
671 (1ull << IB_USER_VERBS_CMD_REG_MR) |
672 (1ull << IB_USER_VERBS_CMD_REREG_MR) |
673 (1ull << IB_USER_VERBS_CMD_DEREG_MR) |
674 (1ull << IB_USER_VERBS_CMD_CREATE_COMP_CHANNEL) |
675 (1ull << IB_USER_VERBS_CMD_CREATE_CQ) |
676 (1ull << IB_USER_VERBS_CMD_RESIZE_CQ) |
677 (1ull << IB_USER_VERBS_CMD_DESTROY_CQ) |
678 (1ull << IB_USER_VERBS_CMD_CREATE_QP) |
679 (1ull << IB_USER_VERBS_CMD_MODIFY_QP) |
680 (1ull << IB_USER_VERBS_CMD_QUERY_QP) |
681 (1ull << IB_USER_VERBS_CMD_DESTROY_QP) |
682 (1ull << IB_USER_VERBS_CMD_CREATE_SRQ) |
683 (1ull << IB_USER_VERBS_CMD_MODIFY_SRQ) |
684 (1ull << IB_USER_VERBS_CMD_QUERY_SRQ) |
685 (1ull << IB_USER_VERBS_CMD_DESTROY_SRQ) |
686 (1ull << IB_USER_VERBS_CMD_CREATE_AH) |
687 (1ull << IB_USER_VERBS_CMD_MODIFY_AH) |
688 (1ull << IB_USER_VERBS_CMD_QUERY_AH) |
689 (1ull << IB_USER_VERBS_CMD_DESTROY_AH);
690 /* POLL_CQ and REQ_NOTIFY_CQ is directly handled in libbnxt_re */
693 rdma_set_device_sysfs_group(ibdev, &bnxt_re_dev_attr_group);
694 ibdev->driver_id = RDMA_DRIVER_BNXT_RE;
695 ib_set_device_ops(ibdev, &bnxt_re_dev_ops);
696 ret = ib_device_set_netdev(&rdev->ibdev, rdev->netdev, 1);
700 return ib_register_device(ibdev, "bnxt_re%d");
703 static void bnxt_re_dev_remove(struct bnxt_re_dev *rdev)
705 dev_put(rdev->netdev);
708 mutex_lock(&bnxt_re_dev_lock);
709 list_del_rcu(&rdev->list);
710 mutex_unlock(&bnxt_re_dev_lock);
714 ib_dealloc_device(&rdev->ibdev);
718 static struct bnxt_re_dev *bnxt_re_dev_add(struct net_device *netdev,
719 struct bnxt_en_dev *en_dev)
721 struct bnxt_re_dev *rdev;
723 /* Allocate bnxt_re_dev instance here */
724 rdev = ib_alloc_device(bnxt_re_dev, ibdev);
726 dev_err(NULL, "%s: bnxt_re_dev allocation failure!",
727 ROCE_DRV_MODULE_NAME);
731 rdev->netdev = netdev;
732 dev_hold(rdev->netdev);
733 rdev->en_dev = en_dev;
734 rdev->id = rdev->en_dev->pdev->devfn;
735 INIT_LIST_HEAD(&rdev->qp_list);
736 mutex_init(&rdev->qp_lock);
737 atomic_set(&rdev->qp_count, 0);
738 atomic_set(&rdev->cq_count, 0);
739 atomic_set(&rdev->srq_count, 0);
740 atomic_set(&rdev->mr_count, 0);
741 atomic_set(&rdev->mw_count, 0);
742 rdev->cosq[0] = 0xFFFF;
743 rdev->cosq[1] = 0xFFFF;
745 mutex_lock(&bnxt_re_dev_lock);
746 list_add_tail_rcu(&rdev->list, &bnxt_re_dev_list);
747 mutex_unlock(&bnxt_re_dev_lock);
751 static int bnxt_re_handle_unaffi_async_event(struct creq_func_event
754 switch (unaffi_async->event) {
755 case CREQ_FUNC_EVENT_EVENT_TX_WQE_ERROR:
757 case CREQ_FUNC_EVENT_EVENT_TX_DATA_ERROR:
759 case CREQ_FUNC_EVENT_EVENT_RX_WQE_ERROR:
761 case CREQ_FUNC_EVENT_EVENT_RX_DATA_ERROR:
763 case CREQ_FUNC_EVENT_EVENT_CQ_ERROR:
765 case CREQ_FUNC_EVENT_EVENT_TQM_ERROR:
767 case CREQ_FUNC_EVENT_EVENT_CFCQ_ERROR:
769 case CREQ_FUNC_EVENT_EVENT_CFCS_ERROR:
771 case CREQ_FUNC_EVENT_EVENT_CFCC_ERROR:
773 case CREQ_FUNC_EVENT_EVENT_CFCM_ERROR:
775 case CREQ_FUNC_EVENT_EVENT_TIM_ERROR:
783 static int bnxt_re_handle_qp_async_event(struct creq_qp_event *qp_event,
784 struct bnxt_re_qp *qp)
786 struct ib_event event;
789 if (qp->qplib_qp.state == CMDQ_MODIFY_QP_NEW_STATE_ERR) {
790 flags = bnxt_re_lock_cqs(qp);
791 bnxt_qplib_add_flush_qp(&qp->qplib_qp);
792 bnxt_re_unlock_cqs(qp, flags);
795 memset(&event, 0, sizeof(event));
796 if (qp->qplib_qp.srq) {
797 event.device = &qp->rdev->ibdev;
798 event.element.qp = &qp->ib_qp;
799 event.event = IB_EVENT_QP_LAST_WQE_REACHED;
802 if (event.device && qp->ib_qp.event_handler)
803 qp->ib_qp.event_handler(&event, qp->ib_qp.qp_context);
808 static int bnxt_re_handle_affi_async_event(struct creq_qp_event *affi_async,
815 return rc; /* QP was already dead, still return success */
817 event = affi_async->event;
818 if (event == CREQ_QP_EVENT_EVENT_QP_ERROR_NOTIFICATION) {
819 struct bnxt_qplib_qp *lib_qp = obj;
820 struct bnxt_re_qp *qp = container_of(lib_qp, struct bnxt_re_qp,
822 rc = bnxt_re_handle_qp_async_event(affi_async, qp);
827 static int bnxt_re_aeq_handler(struct bnxt_qplib_rcfw *rcfw,
828 void *aeqe, void *obj)
830 struct creq_qp_event *affi_async;
831 struct creq_func_event *unaffi_async;
835 type = ((struct creq_base *)aeqe)->type;
836 if (type == CREQ_BASE_TYPE_FUNC_EVENT) {
838 rc = bnxt_re_handle_unaffi_async_event(unaffi_async);
841 rc = bnxt_re_handle_affi_async_event(affi_async, obj);
847 static int bnxt_re_srqn_handler(struct bnxt_qplib_nq *nq,
848 struct bnxt_qplib_srq *handle, u8 event)
850 struct bnxt_re_srq *srq = container_of(handle, struct bnxt_re_srq,
852 struct ib_event ib_event;
856 dev_err(NULL, "%s: SRQ is NULL, SRQN not handled",
857 ROCE_DRV_MODULE_NAME);
861 ib_event.device = &srq->rdev->ibdev;
862 ib_event.element.srq = &srq->ib_srq;
863 if (event == NQ_SRQ_EVENT_EVENT_SRQ_THRESHOLD_EVENT)
864 ib_event.event = IB_EVENT_SRQ_LIMIT_REACHED;
866 ib_event.event = IB_EVENT_SRQ_ERR;
868 if (srq->ib_srq.event_handler) {
869 /* Lock event_handler? */
870 (*srq->ib_srq.event_handler)(&ib_event,
871 srq->ib_srq.srq_context);
877 static int bnxt_re_cqn_handler(struct bnxt_qplib_nq *nq,
878 struct bnxt_qplib_cq *handle)
880 struct bnxt_re_cq *cq = container_of(handle, struct bnxt_re_cq,
884 dev_err(NULL, "%s: CQ is NULL, CQN not handled",
885 ROCE_DRV_MODULE_NAME);
888 if (cq->ib_cq.comp_handler) {
889 /* Lock comp_handler? */
890 (*cq->ib_cq.comp_handler)(&cq->ib_cq, cq->ib_cq.cq_context);
896 static u32 bnxt_re_get_nqdb_offset(struct bnxt_re_dev *rdev, u16 indx)
898 return bnxt_qplib_is_chip_gen_p5(&rdev->chip_ctx) ?
899 0x10000 : rdev->msix_entries[indx].db_offset;
902 static void bnxt_re_cleanup_res(struct bnxt_re_dev *rdev)
906 for (i = 1; i < rdev->num_msix; i++)
907 bnxt_qplib_disable_nq(&rdev->nq[i - 1]);
909 if (rdev->qplib_res.rcfw)
910 bnxt_qplib_cleanup_res(&rdev->qplib_res);
913 static int bnxt_re_init_res(struct bnxt_re_dev *rdev)
915 int num_vec_enabled = 0;
919 bnxt_qplib_init_res(&rdev->qplib_res);
921 for (i = 1; i < rdev->num_msix ; i++) {
922 db_offt = bnxt_re_get_nqdb_offset(rdev, i);
923 rc = bnxt_qplib_enable_nq(rdev->en_dev->pdev, &rdev->nq[i - 1],
924 i - 1, rdev->msix_entries[i].vector,
925 db_offt, &bnxt_re_cqn_handler,
926 &bnxt_re_srqn_handler);
928 dev_err(rdev_to_dev(rdev),
929 "Failed to enable NQ with rc = 0x%x", rc);
936 for (i = num_vec_enabled; i >= 0; i--)
937 bnxt_qplib_disable_nq(&rdev->nq[i]);
941 static void bnxt_re_free_nq_res(struct bnxt_re_dev *rdev)
946 for (i = 0; i < rdev->num_msix - 1; i++) {
947 type = bnxt_qplib_get_ring_type(&rdev->chip_ctx);
948 bnxt_re_net_ring_free(rdev, rdev->nq[i].ring_id, type);
949 rdev->nq[i].res = NULL;
950 bnxt_qplib_free_nq(&rdev->nq[i]);
954 static void bnxt_re_free_res(struct bnxt_re_dev *rdev)
956 bnxt_re_free_nq_res(rdev);
958 if (rdev->qplib_res.dpi_tbl.max) {
959 bnxt_qplib_dealloc_dpi(&rdev->qplib_res,
960 &rdev->qplib_res.dpi_tbl,
961 &rdev->dpi_privileged);
963 if (rdev->qplib_res.rcfw) {
964 bnxt_qplib_free_res(&rdev->qplib_res);
965 rdev->qplib_res.rcfw = NULL;
969 static int bnxt_re_alloc_res(struct bnxt_re_dev *rdev)
971 int num_vec_created = 0;
977 /* Configure and allocate resources for qplib */
978 rdev->qplib_res.rcfw = &rdev->rcfw;
979 rc = bnxt_qplib_get_dev_attr(&rdev->rcfw, &rdev->dev_attr,
984 rc = bnxt_qplib_alloc_res(&rdev->qplib_res, rdev->en_dev->pdev,
985 rdev->netdev, &rdev->dev_attr);
989 rc = bnxt_qplib_alloc_dpi(&rdev->qplib_res.dpi_tbl,
990 &rdev->dpi_privileged,
995 for (i = 0; i < rdev->num_msix - 1; i++) {
996 rdev->nq[i].res = &rdev->qplib_res;
997 rdev->nq[i].hwq.max_elements = BNXT_RE_MAX_CQ_COUNT +
998 BNXT_RE_MAX_SRQC_COUNT + 2;
999 rc = bnxt_qplib_alloc_nq(rdev->en_dev->pdev, &rdev->nq[i]);
1001 dev_err(rdev_to_dev(rdev), "Alloc Failed NQ%d rc:%#x",
1005 type = bnxt_qplib_get_ring_type(&rdev->chip_ctx);
1006 pg_map = rdev->nq[i].hwq.pbl[PBL_LVL_0].pg_map_arr;
1007 pages = rdev->nq[i].hwq.pbl[rdev->nq[i].hwq.level].pg_count;
1008 rc = bnxt_re_net_ring_alloc(rdev, pg_map, pages, type,
1009 BNXT_QPLIB_NQE_MAX_CNT - 1,
1010 rdev->msix_entries[i + 1].ring_idx,
1011 &rdev->nq[i].ring_id);
1013 dev_err(rdev_to_dev(rdev),
1014 "Failed to allocate NQ fw id with rc = 0x%x",
1016 bnxt_qplib_free_nq(&rdev->nq[i]);
1023 for (i = num_vec_created; i >= 0; i--) {
1024 type = bnxt_qplib_get_ring_type(&rdev->chip_ctx);
1025 bnxt_re_net_ring_free(rdev, rdev->nq[i].ring_id, type);
1026 bnxt_qplib_free_nq(&rdev->nq[i]);
1028 bnxt_qplib_dealloc_dpi(&rdev->qplib_res,
1029 &rdev->qplib_res.dpi_tbl,
1030 &rdev->dpi_privileged);
1032 bnxt_qplib_free_res(&rdev->qplib_res);
1035 rdev->qplib_res.rcfw = NULL;
1039 static void bnxt_re_dispatch_event(struct ib_device *ibdev, struct ib_qp *qp,
1040 u8 port_num, enum ib_event_type event)
1042 struct ib_event ib_event;
1044 ib_event.device = ibdev;
1046 ib_event.element.qp = qp;
1047 ib_event.event = event;
1048 if (qp->event_handler)
1049 qp->event_handler(&ib_event, qp->qp_context);
1052 ib_event.element.port_num = port_num;
1053 ib_event.event = event;
1054 ib_dispatch_event(&ib_event);
1058 #define HWRM_QUEUE_PRI2COS_QCFG_INPUT_FLAGS_IVLAN 0x02
1059 static int bnxt_re_query_hwrm_pri2cos(struct bnxt_re_dev *rdev, u8 dir,
1062 struct hwrm_queue_pri2cos_qcfg_input req = {0};
1063 struct bnxt *bp = netdev_priv(rdev->netdev);
1064 struct hwrm_queue_pri2cos_qcfg_output resp;
1065 struct bnxt_en_dev *en_dev = rdev->en_dev;
1066 struct bnxt_fw_msg fw_msg;
1068 u8 *qcfgmap, *tmp_map;
1074 memset(&fw_msg, 0, sizeof(fw_msg));
1075 bnxt_re_init_hwrm_hdr(rdev, (void *)&req,
1076 HWRM_QUEUE_PRI2COS_QCFG, -1, -1);
1077 flags |= (dir & 0x01);
1078 flags |= HWRM_QUEUE_PRI2COS_QCFG_INPUT_FLAGS_IVLAN;
1079 req.flags = cpu_to_le32(flags);
1080 req.port_id = bp->pf.port_id;
1082 bnxt_re_fill_fw_msg(&fw_msg, (void *)&req, sizeof(req), (void *)&resp,
1083 sizeof(resp), DFLT_HWRM_CMD_TIMEOUT);
1084 rc = en_dev->en_ops->bnxt_send_fw_msg(en_dev, BNXT_ROCE_ULP, &fw_msg);
1088 if (resp.queue_cfg_info) {
1089 dev_warn(rdev_to_dev(rdev),
1090 "Asymmetric cos queue configuration detected");
1091 dev_warn(rdev_to_dev(rdev),
1092 " on device, QoS may not be fully functional\n");
1094 qcfgmap = &resp.pri0_cos_queue_id;
1095 tmp_map = (u8 *)cid_map;
1096 for (i = 0; i < IEEE_8021QAZ_MAX_TCS; i++)
1097 tmp_map[i] = qcfgmap[i];
1102 static bool bnxt_re_is_qp1_or_shadow_qp(struct bnxt_re_dev *rdev,
1103 struct bnxt_re_qp *qp)
1105 return (qp->ib_qp.qp_type == IB_QPT_GSI) || (qp == rdev->qp1_sqp);
1108 static void bnxt_re_dev_stop(struct bnxt_re_dev *rdev)
1110 int mask = IB_QP_STATE;
1111 struct ib_qp_attr qp_attr;
1112 struct bnxt_re_qp *qp;
1114 qp_attr.qp_state = IB_QPS_ERR;
1115 mutex_lock(&rdev->qp_lock);
1116 list_for_each_entry(qp, &rdev->qp_list, list) {
1117 /* Modify the state of all QPs except QP1/Shadow QP */
1118 if (!bnxt_re_is_qp1_or_shadow_qp(rdev, qp)) {
1119 if (qp->qplib_qp.state !=
1120 CMDQ_MODIFY_QP_NEW_STATE_RESET &&
1121 qp->qplib_qp.state !=
1122 CMDQ_MODIFY_QP_NEW_STATE_ERR) {
1123 bnxt_re_dispatch_event(&rdev->ibdev, &qp->ib_qp,
1124 1, IB_EVENT_QP_FATAL);
1125 bnxt_re_modify_qp(&qp->ib_qp, &qp_attr, mask,
1130 mutex_unlock(&rdev->qp_lock);
1133 static int bnxt_re_update_gid(struct bnxt_re_dev *rdev)
1135 struct bnxt_qplib_sgid_tbl *sgid_tbl = &rdev->qplib_res.sgid_tbl;
1136 struct bnxt_qplib_gid gid;
1140 if (!test_bit(BNXT_RE_FLAG_IBDEV_REGISTERED, &rdev->flags))
1144 dev_err(rdev_to_dev(rdev), "QPLIB: SGID table not allocated");
1148 for (index = 0; index < sgid_tbl->active; index++) {
1149 gid_idx = sgid_tbl->hw_id[index];
1151 if (!memcmp(&sgid_tbl->tbl[index], &bnxt_qplib_gid_zero,
1152 sizeof(bnxt_qplib_gid_zero)))
1154 /* need to modify the VLAN enable setting of non VLAN GID only
1155 * as setting is done for VLAN GID while adding GID
1157 if (sgid_tbl->vlan[index])
1160 memcpy(&gid, &sgid_tbl->tbl[index], sizeof(gid));
1162 rc = bnxt_qplib_update_sgid(sgid_tbl, &gid, gid_idx,
1163 rdev->qplib_res.netdev->dev_addr);
1169 static u32 bnxt_re_get_priority_mask(struct bnxt_re_dev *rdev)
1171 u32 prio_map = 0, tmp_map = 0;
1172 struct net_device *netdev;
1175 netdev = rdev->netdev;
1177 memset(&app, 0, sizeof(app));
1178 app.selector = IEEE_8021QAZ_APP_SEL_ETHERTYPE;
1179 app.protocol = ETH_P_IBOE;
1180 tmp_map = dcb_ieee_getapp_mask(netdev, &app);
1183 app.selector = IEEE_8021QAZ_APP_SEL_DGRAM;
1184 app.protocol = ROCE_V2_UDP_DPORT;
1185 tmp_map = dcb_ieee_getapp_mask(netdev, &app);
1186 prio_map |= tmp_map;
1191 static void bnxt_re_parse_cid_map(u8 prio_map, u8 *cid_map, u16 *cosq)
1196 for (prio = 0, id = 0; prio < 8; prio++) {
1197 if (prio_map & (1 << prio)) {
1198 cosq[id] = cid_map[prio];
1200 if (id == 2) /* Max 2 tcs supported */
1206 static int bnxt_re_setup_qos(struct bnxt_re_dev *rdev)
1212 /* Get priority for roce */
1213 prio_map = bnxt_re_get_priority_mask(rdev);
1215 if (prio_map == rdev->cur_prio_map)
1217 rdev->cur_prio_map = prio_map;
1218 /* Get cosq id for this priority */
1219 rc = bnxt_re_query_hwrm_pri2cos(rdev, 0, &cid_map);
1221 dev_warn(rdev_to_dev(rdev), "no cos for p_mask %x\n", prio_map);
1224 /* Parse CoS IDs for app priority */
1225 bnxt_re_parse_cid_map(prio_map, (u8 *)&cid_map, rdev->cosq);
1228 rc = bnxt_qplib_map_tc2cos(&rdev->qplib_res, rdev->cosq);
1230 dev_warn(rdev_to_dev(rdev), "no tc for cos{%x, %x}\n",
1231 rdev->cosq[0], rdev->cosq[1]);
1235 /* Actual priorities are not programmed as they are already
1236 * done by L2 driver; just enable or disable priority vlan tagging
1238 if ((prio_map == 0 && rdev->qplib_res.prio) ||
1239 (prio_map != 0 && !rdev->qplib_res.prio)) {
1240 rdev->qplib_res.prio = prio_map ? true : false;
1242 bnxt_re_update_gid(rdev);
1248 static void bnxt_re_query_hwrm_intf_version(struct bnxt_re_dev *rdev)
1250 struct bnxt_en_dev *en_dev = rdev->en_dev;
1251 struct hwrm_ver_get_output resp = {0};
1252 struct hwrm_ver_get_input req = {0};
1253 struct bnxt_fw_msg fw_msg;
1256 memset(&fw_msg, 0, sizeof(fw_msg));
1257 bnxt_re_init_hwrm_hdr(rdev, (void *)&req,
1258 HWRM_VER_GET, -1, -1);
1259 req.hwrm_intf_maj = HWRM_VERSION_MAJOR;
1260 req.hwrm_intf_min = HWRM_VERSION_MINOR;
1261 req.hwrm_intf_upd = HWRM_VERSION_UPDATE;
1262 bnxt_re_fill_fw_msg(&fw_msg, (void *)&req, sizeof(req), (void *)&resp,
1263 sizeof(resp), DFLT_HWRM_CMD_TIMEOUT);
1264 rc = en_dev->en_ops->bnxt_send_fw_msg(en_dev, BNXT_ROCE_ULP, &fw_msg);
1266 dev_err(rdev_to_dev(rdev),
1267 "Failed to query HW version, rc = 0x%x", rc);
1270 rdev->qplib_ctx.hwrm_intf_ver =
1271 (u64)resp.hwrm_intf_major << 48 |
1272 (u64)resp.hwrm_intf_minor << 32 |
1273 (u64)resp.hwrm_intf_build << 16 |
1274 resp.hwrm_intf_patch;
1277 static void bnxt_re_ib_unreg(struct bnxt_re_dev *rdev)
1282 if (test_and_clear_bit(BNXT_RE_FLAG_IBDEV_REGISTERED, &rdev->flags)) {
1283 /* Cleanup ib dev */
1284 bnxt_re_unregister_ib(rdev);
1286 if (test_and_clear_bit(BNXT_RE_FLAG_QOS_WORK_REG, &rdev->flags))
1287 cancel_delayed_work_sync(&rdev->worker);
1289 if (test_and_clear_bit(BNXT_RE_FLAG_RESOURCES_INITIALIZED,
1291 bnxt_re_cleanup_res(rdev);
1292 if (test_and_clear_bit(BNXT_RE_FLAG_RESOURCES_ALLOCATED, &rdev->flags))
1293 bnxt_re_free_res(rdev);
1295 if (test_and_clear_bit(BNXT_RE_FLAG_RCFW_CHANNEL_EN, &rdev->flags)) {
1296 rc = bnxt_qplib_deinit_rcfw(&rdev->rcfw);
1298 dev_warn(rdev_to_dev(rdev),
1299 "Failed to deinitialize RCFW: %#x", rc);
1300 bnxt_re_net_stats_ctx_free(rdev, rdev->qplib_ctx.stats.fw_id);
1301 bnxt_qplib_free_ctx(rdev->en_dev->pdev, &rdev->qplib_ctx);
1302 bnxt_qplib_disable_rcfw_channel(&rdev->rcfw);
1303 type = bnxt_qplib_get_ring_type(&rdev->chip_ctx);
1304 bnxt_re_net_ring_free(rdev, rdev->rcfw.creq_ring_id, type);
1305 bnxt_qplib_free_rcfw_channel(&rdev->rcfw);
1307 if (test_and_clear_bit(BNXT_RE_FLAG_GOT_MSIX, &rdev->flags)) {
1308 rc = bnxt_re_free_msix(rdev);
1310 dev_warn(rdev_to_dev(rdev),
1311 "Failed to free MSI-X vectors: %#x", rc);
1314 bnxt_re_destroy_chip_ctx(rdev);
1315 if (test_and_clear_bit(BNXT_RE_FLAG_NETDEV_REGISTERED, &rdev->flags)) {
1316 rc = bnxt_re_unregister_netdev(rdev);
1318 dev_warn(rdev_to_dev(rdev),
1319 "Failed to unregister with netdev: %#x", rc);
1323 /* worker thread for polling periodic events. Now used for QoS programming*/
1324 static void bnxt_re_worker(struct work_struct *work)
1326 struct bnxt_re_dev *rdev = container_of(work, struct bnxt_re_dev,
1329 bnxt_re_setup_qos(rdev);
1330 schedule_delayed_work(&rdev->worker, msecs_to_jiffies(30000));
1333 static int bnxt_re_ib_reg(struct bnxt_re_dev *rdev)
1342 /* Acquire rtnl lock through out this function */
1346 /* Registered a new RoCE device instance to netdev */
1347 rc = bnxt_re_register_netdev(rdev);
1350 pr_err("Failed to register with netedev: %#x\n", rc);
1353 set_bit(BNXT_RE_FLAG_NETDEV_REGISTERED, &rdev->flags);
1355 rc = bnxt_re_setup_chip_ctx(rdev);
1357 dev_err(rdev_to_dev(rdev), "Failed to get chip context\n");
1361 /* Check whether VF or PF */
1362 bnxt_re_get_sriov_func_type(rdev);
1364 rc = bnxt_re_request_msix(rdev);
1366 pr_err("Failed to get MSI-X vectors: %#x\n", rc);
1370 set_bit(BNXT_RE_FLAG_GOT_MSIX, &rdev->flags);
1372 bnxt_re_query_hwrm_intf_version(rdev);
1374 /* Establish RCFW Communication Channel to initialize the context
1375 * memory for the function and all child VFs
1377 rc = bnxt_qplib_alloc_rcfw_channel(rdev->en_dev->pdev, &rdev->rcfw,
1379 BNXT_RE_MAX_QPC_COUNT);
1381 pr_err("Failed to allocate RCFW Channel: %#x\n", rc);
1384 type = bnxt_qplib_get_ring_type(&rdev->chip_ctx);
1385 pg_map = rdev->rcfw.creq.pbl[PBL_LVL_0].pg_map_arr;
1386 pages = rdev->rcfw.creq.pbl[rdev->rcfw.creq.level].pg_count;
1387 ridx = rdev->msix_entries[BNXT_RE_AEQ_IDX].ring_idx;
1388 rc = bnxt_re_net_ring_alloc(rdev, pg_map, pages, type,
1389 BNXT_QPLIB_CREQE_MAX_CNT - 1,
1390 ridx, &rdev->rcfw.creq_ring_id);
1392 pr_err("Failed to allocate CREQ: %#x\n", rc);
1395 db_offt = bnxt_re_get_nqdb_offset(rdev, BNXT_RE_AEQ_IDX);
1396 vid = rdev->msix_entries[BNXT_RE_AEQ_IDX].vector;
1397 rc = bnxt_qplib_enable_rcfw_channel(rdev->en_dev->pdev, &rdev->rcfw,
1398 vid, db_offt, rdev->is_virtfn,
1399 &bnxt_re_aeq_handler);
1401 pr_err("Failed to enable RCFW channel: %#x\n", rc);
1405 rc = bnxt_qplib_get_dev_attr(&rdev->rcfw, &rdev->dev_attr,
1409 if (!rdev->is_virtfn)
1410 bnxt_re_set_resource_limits(rdev);
1412 rc = bnxt_qplib_alloc_ctx(rdev->en_dev->pdev, &rdev->qplib_ctx, 0,
1413 bnxt_qplib_is_chip_gen_p5(&rdev->chip_ctx));
1415 pr_err("Failed to allocate QPLIB context: %#x\n", rc);
1418 rc = bnxt_re_net_stats_ctx_alloc(rdev,
1419 rdev->qplib_ctx.stats.dma_map,
1420 &rdev->qplib_ctx.stats.fw_id);
1422 pr_err("Failed to allocate stats context: %#x\n", rc);
1426 rc = bnxt_qplib_init_rcfw(&rdev->rcfw, &rdev->qplib_ctx,
1429 pr_err("Failed to initialize RCFW: %#x\n", rc);
1432 set_bit(BNXT_RE_FLAG_RCFW_CHANNEL_EN, &rdev->flags);
1434 /* Resources based on the 'new' device caps */
1435 rc = bnxt_re_alloc_res(rdev);
1437 pr_err("Failed to allocate resources: %#x\n", rc);
1440 set_bit(BNXT_RE_FLAG_RESOURCES_ALLOCATED, &rdev->flags);
1441 rc = bnxt_re_init_res(rdev);
1443 pr_err("Failed to initialize resources: %#x\n", rc);
1447 set_bit(BNXT_RE_FLAG_RESOURCES_INITIALIZED, &rdev->flags);
1449 if (!rdev->is_virtfn) {
1450 rc = bnxt_re_setup_qos(rdev);
1452 pr_info("RoCE priority not yet configured\n");
1454 INIT_DELAYED_WORK(&rdev->worker, bnxt_re_worker);
1455 set_bit(BNXT_RE_FLAG_QOS_WORK_REG, &rdev->flags);
1456 schedule_delayed_work(&rdev->worker, msecs_to_jiffies(30000));
1462 /* Register ib dev */
1463 rc = bnxt_re_register_ib(rdev);
1465 pr_err("Failed to register with IB: %#x\n", rc);
1468 set_bit(BNXT_RE_FLAG_IBDEV_REGISTERED, &rdev->flags);
1469 dev_info(rdev_to_dev(rdev), "Device registered successfully");
1470 ib_get_eth_speed(&rdev->ibdev, 1, &rdev->active_speed,
1471 &rdev->active_width);
1472 set_bit(BNXT_RE_FLAG_ISSUE_ROCE_STATS, &rdev->flags);
1473 bnxt_re_dispatch_event(&rdev->ibdev, NULL, 1, IB_EVENT_PORT_ACTIVE);
1474 bnxt_re_dispatch_event(&rdev->ibdev, NULL, 1, IB_EVENT_GID_CHANGE);
1478 bnxt_re_net_stats_ctx_free(rdev, rdev->qplib_ctx.stats.fw_id);
1480 bnxt_qplib_free_ctx(rdev->en_dev->pdev, &rdev->qplib_ctx);
1482 bnxt_qplib_disable_rcfw_channel(&rdev->rcfw);
1484 type = bnxt_qplib_get_ring_type(&rdev->chip_ctx);
1485 bnxt_re_net_ring_free(rdev, rdev->rcfw.creq_ring_id, type);
1487 bnxt_qplib_free_rcfw_channel(&rdev->rcfw);
1491 bnxt_re_ib_unreg(rdev);
1497 static void bnxt_re_dev_unreg(struct bnxt_re_dev *rdev)
1499 struct bnxt_en_dev *en_dev = rdev->en_dev;
1500 struct net_device *netdev = rdev->netdev;
1502 bnxt_re_dev_remove(rdev);
1505 bnxt_re_dev_unprobe(netdev, en_dev);
1508 static int bnxt_re_dev_reg(struct bnxt_re_dev **rdev, struct net_device *netdev)
1510 struct bnxt_en_dev *en_dev;
1513 if (!is_bnxt_re_dev(netdev))
1516 en_dev = bnxt_re_dev_probe(netdev);
1517 if (IS_ERR(en_dev)) {
1518 if (en_dev != ERR_PTR(-ENODEV))
1519 pr_err("%s: Failed to probe\n", ROCE_DRV_MODULE_NAME);
1520 rc = PTR_ERR(en_dev);
1523 *rdev = bnxt_re_dev_add(netdev, en_dev);
1526 bnxt_re_dev_unprobe(netdev, en_dev);
1533 static void bnxt_re_remove_one(struct bnxt_re_dev *rdev)
1535 pci_dev_put(rdev->en_dev->pdev);
1538 /* Handle all deferred netevents tasks */
1539 static void bnxt_re_task(struct work_struct *work)
1541 struct bnxt_re_work *re_work;
1542 struct bnxt_re_dev *rdev;
1545 re_work = container_of(work, struct bnxt_re_work, work);
1546 rdev = re_work->rdev;
1548 if (re_work->event != NETDEV_REGISTER &&
1549 !test_bit(BNXT_RE_FLAG_IBDEV_REGISTERED, &rdev->flags))
1552 switch (re_work->event) {
1553 case NETDEV_REGISTER:
1554 rc = bnxt_re_ib_reg(rdev);
1556 dev_err(rdev_to_dev(rdev),
1557 "Failed to register with IB: %#x", rc);
1558 bnxt_re_remove_one(rdev);
1559 bnxt_re_dev_unreg(rdev);
1564 bnxt_re_dispatch_event(&rdev->ibdev, NULL, 1,
1565 IB_EVENT_PORT_ACTIVE);
1568 bnxt_re_dev_stop(rdev);
1571 if (!netif_carrier_ok(rdev->netdev))
1572 bnxt_re_dev_stop(rdev);
1573 else if (netif_carrier_ok(rdev->netdev))
1574 bnxt_re_dispatch_event(&rdev->ibdev, NULL, 1,
1575 IB_EVENT_PORT_ACTIVE);
1576 ib_get_eth_speed(&rdev->ibdev, 1, &rdev->active_speed,
1577 &rdev->active_width);
1582 smp_mb__before_atomic();
1583 atomic_dec(&rdev->sched_count);
1588 static void bnxt_re_init_one(struct bnxt_re_dev *rdev)
1590 pci_dev_get(rdev->en_dev->pdev);
1594 * "Notifier chain callback can be invoked for the same chain from
1595 * different CPUs at the same time".
1597 * For cases when the netdev is already present, our call to the
1598 * register_netdevice_notifier() will actually get the rtnl_lock()
1599 * before sending NETDEV_REGISTER and (if up) NETDEV_UP
1602 * But for cases when the netdev is not already present, the notifier
1603 * chain is subjected to be invoked from different CPUs simultaneously.
1605 * This is protected by the netdev_mutex.
1607 static int bnxt_re_netdev_event(struct notifier_block *notifier,
1608 unsigned long event, void *ptr)
1610 struct net_device *real_dev, *netdev = netdev_notifier_info_to_dev(ptr);
1611 struct bnxt_re_work *re_work;
1612 struct bnxt_re_dev *rdev;
1614 bool sch_work = false;
1616 real_dev = rdma_vlan_dev_real_dev(netdev);
1620 rdev = bnxt_re_from_netdev(real_dev);
1621 if (!rdev && event != NETDEV_REGISTER)
1623 if (real_dev != netdev)
1627 case NETDEV_REGISTER:
1630 rc = bnxt_re_dev_reg(&rdev, real_dev);
1634 pr_err("Failed to register with the device %s: %#x\n",
1635 real_dev->name, rc);
1638 bnxt_re_init_one(rdev);
1642 case NETDEV_UNREGISTER:
1643 /* netdev notifier will call NETDEV_UNREGISTER again later since
1644 * we are still holding the reference to the netdev
1646 if (atomic_read(&rdev->sched_count) > 0)
1648 bnxt_re_ib_unreg(rdev);
1649 bnxt_re_remove_one(rdev);
1650 bnxt_re_dev_unreg(rdev);
1658 /* Allocate for the deferred task */
1659 re_work = kzalloc(sizeof(*re_work), GFP_ATOMIC);
1661 re_work->rdev = rdev;
1662 re_work->event = event;
1663 re_work->vlan_dev = (real_dev == netdev ?
1665 INIT_WORK(&re_work->work, bnxt_re_task);
1666 atomic_inc(&rdev->sched_count);
1667 queue_work(bnxt_re_wq, &re_work->work);
1675 static struct notifier_block bnxt_re_netdev_notifier = {
1676 .notifier_call = bnxt_re_netdev_event
1679 static int __init bnxt_re_mod_init(void)
1683 pr_info("%s: %s", ROCE_DRV_MODULE_NAME, version);
1685 bnxt_re_wq = create_singlethread_workqueue("bnxt_re");
1689 INIT_LIST_HEAD(&bnxt_re_dev_list);
1691 rc = register_netdevice_notifier(&bnxt_re_netdev_notifier);
1693 pr_err("%s: Cannot register to netdevice_notifier",
1694 ROCE_DRV_MODULE_NAME);
1700 destroy_workqueue(bnxt_re_wq);
1705 static void __exit bnxt_re_mod_exit(void)
1707 struct bnxt_re_dev *rdev, *next;
1708 LIST_HEAD(to_be_deleted);
1710 mutex_lock(&bnxt_re_dev_lock);
1711 /* Free all adapter allocated resources */
1712 if (!list_empty(&bnxt_re_dev_list))
1713 list_splice_init(&bnxt_re_dev_list, &to_be_deleted);
1714 mutex_unlock(&bnxt_re_dev_lock);
1716 * Cleanup the devices in reverse order so that the VF device
1717 * cleanup is done before PF cleanup
1719 list_for_each_entry_safe_reverse(rdev, next, &to_be_deleted, list) {
1720 dev_info(rdev_to_dev(rdev), "Unregistering Device");
1722 * Flush out any scheduled tasks before destroying the
1725 flush_workqueue(bnxt_re_wq);
1726 bnxt_re_dev_stop(rdev);
1727 /* Acquire the rtnl_lock as the L2 resources are freed here */
1729 bnxt_re_ib_unreg(rdev);
1731 bnxt_re_remove_one(rdev);
1732 bnxt_re_dev_unreg(rdev);
1734 unregister_netdevice_notifier(&bnxt_re_netdev_notifier);
1736 destroy_workqueue(bnxt_re_wq);
1739 module_init(bnxt_re_mod_init);
1740 module_exit(bnxt_re_mod_exit);