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_remove_device(struct bnxt_re_dev *rdev);
82 static void bnxt_re_dealloc_driver(struct ib_device *ib_dev);
83 static void bnxt_re_stop_irq(void *handle);
84 static void bnxt_re_dev_stop(struct bnxt_re_dev *rdev);
86 static void bnxt_re_set_drv_mode(struct bnxt_re_dev *rdev, u8 mode)
88 struct bnxt_qplib_chip_ctx *cctx;
90 cctx = rdev->chip_ctx;
91 cctx->modes.wqe_mode = bnxt_qplib_is_chip_gen_p5(rdev->chip_ctx) ?
92 mode : BNXT_QPLIB_WQE_MODE_STATIC;
95 static void bnxt_re_destroy_chip_ctx(struct bnxt_re_dev *rdev)
97 struct bnxt_qplib_chip_ctx *chip_ctx;
101 chip_ctx = rdev->chip_ctx;
102 rdev->chip_ctx = NULL;
103 rdev->rcfw.res = NULL;
104 rdev->qplib_res.cctx = NULL;
105 rdev->qplib_res.pdev = NULL;
106 rdev->qplib_res.netdev = NULL;
110 static int bnxt_re_setup_chip_ctx(struct bnxt_re_dev *rdev, u8 wqe_mode)
112 struct bnxt_qplib_chip_ctx *chip_ctx;
113 struct bnxt_en_dev *en_dev;
116 en_dev = rdev->en_dev;
117 bp = netdev_priv(en_dev->net);
119 chip_ctx = kzalloc(sizeof(*chip_ctx), GFP_KERNEL);
122 chip_ctx->chip_num = bp->chip_num;
124 rdev->chip_ctx = chip_ctx;
125 /* rest members to follow eventually */
127 rdev->qplib_res.cctx = rdev->chip_ctx;
128 rdev->rcfw.res = &rdev->qplib_res;
130 bnxt_re_set_drv_mode(rdev, wqe_mode);
134 /* SR-IOV helper functions */
136 static void bnxt_re_get_sriov_func_type(struct bnxt_re_dev *rdev)
140 bp = netdev_priv(rdev->en_dev->net);
145 /* Set the maximum number of each resource that the driver actually wants
146 * to allocate. This may be up to the maximum number the firmware has
147 * reserved for the function. The driver may choose to allocate fewer
148 * resources than the firmware maximum.
150 static void bnxt_re_limit_pf_res(struct bnxt_re_dev *rdev)
152 struct bnxt_qplib_dev_attr *attr;
153 struct bnxt_qplib_ctx *ctx;
156 attr = &rdev->dev_attr;
157 ctx = &rdev->qplib_ctx;
159 ctx->qpc_count = min_t(u32, BNXT_RE_MAX_QPC_COUNT,
161 ctx->mrw_count = BNXT_RE_MAX_MRW_COUNT_256K;
162 /* Use max_mr from fw since max_mrw does not get set */
163 ctx->mrw_count = min_t(u32, ctx->mrw_count, attr->max_mr);
164 ctx->srqc_count = min_t(u32, BNXT_RE_MAX_SRQC_COUNT,
166 ctx->cq_count = min_t(u32, BNXT_RE_MAX_CQ_COUNT, attr->max_cq);
167 if (!bnxt_qplib_is_chip_gen_p5(rdev->chip_ctx))
168 for (i = 0; i < MAX_TQM_ALLOC_REQ; i++)
169 rdev->qplib_ctx.tqm_ctx.qcount[i] =
170 rdev->dev_attr.tqm_alloc_reqs[i];
173 static void bnxt_re_limit_vf_res(struct bnxt_qplib_ctx *qplib_ctx, u32 num_vf)
175 struct bnxt_qplib_vf_res *vf_res;
180 vf_res = &qplib_ctx->vf_res;
182 * Reserve a set of resources for the PF. Divide the remaining
183 * resources among the VFs
185 vf_pct = 100 - BNXT_RE_PCT_RSVD_FOR_PF;
187 num_vf = 100 * num_vf;
188 vf_res->max_qp_per_vf = (qplib_ctx->qpc_count * vf_pct) / num_vf;
189 vf_res->max_srq_per_vf = (qplib_ctx->srqc_count * vf_pct) / num_vf;
190 vf_res->max_cq_per_vf = (qplib_ctx->cq_count * vf_pct) / num_vf;
192 * The driver allows many more MRs than other resources. If the
193 * firmware does also, then reserve a fixed amount for the PF and
194 * divide the rest among VFs. VFs may use many MRs for NFS
195 * mounts, ISER, NVME applications, etc. If the firmware severely
196 * restricts the number of MRs, then let PF have half and divide
197 * the rest among VFs, as for the other resource types.
199 if (qplib_ctx->mrw_count < BNXT_RE_MAX_MRW_COUNT_64K) {
200 mrws = qplib_ctx->mrw_count * vf_pct;
203 mrws = qplib_ctx->mrw_count - BNXT_RE_RESVD_MR_FOR_PF;
205 vf_res->max_mrw_per_vf = (mrws / nvfs);
206 vf_res->max_gid_per_vf = BNXT_RE_MAX_GID_PER_VF;
209 static void bnxt_re_set_resource_limits(struct bnxt_re_dev *rdev)
213 memset(&rdev->qplib_ctx.vf_res, 0, sizeof(struct bnxt_qplib_vf_res));
214 bnxt_re_limit_pf_res(rdev);
216 num_vfs = bnxt_qplib_is_chip_gen_p5(rdev->chip_ctx) ?
217 BNXT_RE_GEN_P5_MAX_VF : rdev->num_vfs;
219 bnxt_re_limit_vf_res(&rdev->qplib_ctx, num_vfs);
222 /* for handling bnxt_en callbacks later */
223 static void bnxt_re_stop(void *p)
225 struct bnxt_re_dev *rdev = p;
232 /* L2 driver invokes this callback during device error/crash or device
233 * reset. Current RoCE driver doesn't recover the device in case of
234 * error. Handle the error by dispatching fatal events to all qps
235 * ie. by calling bnxt_re_dev_stop and release the MSIx vectors as
236 * L2 driver want to modify the MSIx table.
238 bp = netdev_priv(rdev->netdev);
240 ibdev_info(&rdev->ibdev, "Handle device stop call from L2 driver");
241 /* Check the current device state from L2 structure and move the
242 * device to detached state if FW_FATAL_COND is set.
243 * This prevents more commands to HW during clean-up,
244 * in case the device is already in error.
246 if (test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state))
247 set_bit(ERR_DEVICE_DETACHED, &rdev->rcfw.cmdq.flags);
249 bnxt_re_dev_stop(rdev);
250 bnxt_re_stop_irq(rdev);
251 /* Move the device states to detached and avoid sending any more
254 set_bit(BNXT_RE_FLAG_ERR_DEVICE_DETACHED, &rdev->flags);
255 set_bit(ERR_DEVICE_DETACHED, &rdev->rcfw.cmdq.flags);
258 static void bnxt_re_start(void *p)
262 static void bnxt_re_sriov_config(void *p, int num_vfs)
264 struct bnxt_re_dev *rdev = p;
269 if (test_bit(BNXT_RE_FLAG_ERR_DEVICE_DETACHED, &rdev->flags))
271 rdev->num_vfs = num_vfs;
272 if (!bnxt_qplib_is_chip_gen_p5(rdev->chip_ctx)) {
273 bnxt_re_set_resource_limits(rdev);
274 bnxt_qplib_set_func_resources(&rdev->qplib_res, &rdev->rcfw,
279 static void bnxt_re_shutdown(void *p)
281 struct bnxt_re_dev *rdev = p;
286 /* Release the MSIx vectors before queuing unregister */
287 bnxt_re_stop_irq(rdev);
288 ib_unregister_device_queued(&rdev->ibdev);
291 static void bnxt_re_stop_irq(void *handle)
293 struct bnxt_re_dev *rdev = (struct bnxt_re_dev *)handle;
294 struct bnxt_qplib_rcfw *rcfw = &rdev->rcfw;
295 struct bnxt_qplib_nq *nq;
298 for (indx = BNXT_RE_NQ_IDX; indx < rdev->num_msix; indx++) {
299 nq = &rdev->nq[indx - 1];
300 bnxt_qplib_nq_stop_irq(nq, false);
303 bnxt_qplib_rcfw_stop_irq(rcfw, false);
306 static void bnxt_re_start_irq(void *handle, struct bnxt_msix_entry *ent)
308 struct bnxt_re_dev *rdev = (struct bnxt_re_dev *)handle;
309 struct bnxt_msix_entry *msix_ent = rdev->msix_entries;
310 struct bnxt_qplib_rcfw *rcfw = &rdev->rcfw;
311 struct bnxt_qplib_nq *nq;
315 /* Not setting the f/w timeout bit in rcfw.
316 * During the driver unload the first command
317 * to f/w will timeout and that will set the
320 ibdev_err(&rdev->ibdev, "Failed to re-start IRQs\n");
324 /* Vectors may change after restart, so update with new vectors
325 * in device sctructure.
327 for (indx = 0; indx < rdev->num_msix; indx++)
328 rdev->msix_entries[indx].vector = ent[indx].vector;
330 bnxt_qplib_rcfw_start_irq(rcfw, msix_ent[BNXT_RE_AEQ_IDX].vector,
332 for (indx = BNXT_RE_NQ_IDX ; indx < rdev->num_msix; indx++) {
333 nq = &rdev->nq[indx - 1];
334 rc = bnxt_qplib_nq_start_irq(nq, indx - 1,
335 msix_ent[indx].vector, false);
337 ibdev_warn(&rdev->ibdev, "Failed to reinit NQ index %d\n",
342 static struct bnxt_ulp_ops bnxt_re_ulp_ops = {
343 .ulp_async_notifier = NULL,
344 .ulp_stop = bnxt_re_stop,
345 .ulp_start = bnxt_re_start,
346 .ulp_sriov_config = bnxt_re_sriov_config,
347 .ulp_shutdown = bnxt_re_shutdown,
348 .ulp_irq_stop = bnxt_re_stop_irq,
349 .ulp_irq_restart = bnxt_re_start_irq
352 /* RoCE -> Net driver */
354 /* Driver registration routines used to let the networking driver (bnxt_en)
355 * to know that the RoCE driver is now installed
357 static int bnxt_re_unregister_netdev(struct bnxt_re_dev *rdev)
359 struct bnxt_en_dev *en_dev;
365 en_dev = rdev->en_dev;
367 rc = en_dev->en_ops->bnxt_unregister_device(rdev->en_dev,
372 static int bnxt_re_register_netdev(struct bnxt_re_dev *rdev)
374 struct bnxt_en_dev *en_dev;
380 en_dev = rdev->en_dev;
382 rc = en_dev->en_ops->bnxt_register_device(en_dev, BNXT_ROCE_ULP,
383 &bnxt_re_ulp_ops, rdev);
384 rdev->qplib_res.pdev = rdev->en_dev->pdev;
388 static int bnxt_re_free_msix(struct bnxt_re_dev *rdev)
390 struct bnxt_en_dev *en_dev;
396 en_dev = rdev->en_dev;
399 rc = en_dev->en_ops->bnxt_free_msix(rdev->en_dev, BNXT_ROCE_ULP);
404 static int bnxt_re_request_msix(struct bnxt_re_dev *rdev)
406 int rc = 0, num_msix_want = BNXT_RE_MAX_MSIX, num_msix_got;
407 struct bnxt_en_dev *en_dev;
412 en_dev = rdev->en_dev;
414 num_msix_want = min_t(u32, BNXT_RE_MAX_MSIX, num_online_cpus());
416 num_msix_got = en_dev->en_ops->bnxt_request_msix(en_dev, BNXT_ROCE_ULP,
419 if (num_msix_got < BNXT_RE_MIN_MSIX) {
423 if (num_msix_got != num_msix_want) {
424 ibdev_warn(&rdev->ibdev,
425 "Requested %d MSI-X vectors, got %d\n",
426 num_msix_want, num_msix_got);
428 rdev->num_msix = num_msix_got;
433 static void bnxt_re_init_hwrm_hdr(struct bnxt_re_dev *rdev, struct input *hdr,
434 u16 opcd, u16 crid, u16 trid)
436 hdr->req_type = cpu_to_le16(opcd);
437 hdr->cmpl_ring = cpu_to_le16(crid);
438 hdr->target_id = cpu_to_le16(trid);
441 static void bnxt_re_fill_fw_msg(struct bnxt_fw_msg *fw_msg, void *msg,
442 int msg_len, void *resp, int resp_max_len,
446 fw_msg->msg_len = msg_len;
448 fw_msg->resp_max_len = resp_max_len;
449 fw_msg->timeout = timeout;
452 static int bnxt_re_net_ring_free(struct bnxt_re_dev *rdev,
453 u16 fw_ring_id, int type)
455 struct bnxt_en_dev *en_dev = rdev->en_dev;
456 struct hwrm_ring_free_input req = {0};
457 struct hwrm_ring_free_output resp;
458 struct bnxt_fw_msg fw_msg;
464 if (test_bit(BNXT_RE_FLAG_ERR_DEVICE_DETACHED, &rdev->flags))
467 memset(&fw_msg, 0, sizeof(fw_msg));
469 bnxt_re_init_hwrm_hdr(rdev, (void *)&req, HWRM_RING_FREE, -1, -1);
470 req.ring_type = type;
471 req.ring_id = cpu_to_le16(fw_ring_id);
472 bnxt_re_fill_fw_msg(&fw_msg, (void *)&req, sizeof(req), (void *)&resp,
473 sizeof(resp), DFLT_HWRM_CMD_TIMEOUT);
474 rc = en_dev->en_ops->bnxt_send_fw_msg(en_dev, BNXT_ROCE_ULP, &fw_msg);
476 ibdev_err(&rdev->ibdev, "Failed to free HW ring:%d :%#x",
481 static int bnxt_re_net_ring_alloc(struct bnxt_re_dev *rdev,
482 struct bnxt_re_ring_attr *ring_attr,
485 struct bnxt_en_dev *en_dev = rdev->en_dev;
486 struct hwrm_ring_alloc_input req = {0};
487 struct hwrm_ring_alloc_output resp;
488 struct bnxt_fw_msg fw_msg;
494 memset(&fw_msg, 0, sizeof(fw_msg));
495 bnxt_re_init_hwrm_hdr(rdev, (void *)&req, HWRM_RING_ALLOC, -1, -1);
497 req.page_tbl_addr = cpu_to_le64(ring_attr->dma_arr[0]);
498 if (ring_attr->pages > 1) {
499 /* Page size is in log2 units */
500 req.page_size = BNXT_PAGE_SHIFT;
501 req.page_tbl_depth = 1;
504 /* Association of ring index with doorbell index and MSIX number */
505 req.logical_id = cpu_to_le16(ring_attr->lrid);
506 req.length = cpu_to_le32(ring_attr->depth + 1);
507 req.ring_type = ring_attr->type;
508 req.int_mode = ring_attr->mode;
509 bnxt_re_fill_fw_msg(&fw_msg, (void *)&req, sizeof(req), (void *)&resp,
510 sizeof(resp), DFLT_HWRM_CMD_TIMEOUT);
511 rc = en_dev->en_ops->bnxt_send_fw_msg(en_dev, BNXT_ROCE_ULP, &fw_msg);
513 *fw_ring_id = le16_to_cpu(resp.ring_id);
518 static int bnxt_re_net_stats_ctx_free(struct bnxt_re_dev *rdev,
521 struct bnxt_en_dev *en_dev = rdev->en_dev;
522 struct hwrm_stat_ctx_free_input req = {0};
523 struct bnxt_fw_msg fw_msg;
529 if (test_bit(BNXT_RE_FLAG_ERR_DEVICE_DETACHED, &rdev->flags))
532 memset(&fw_msg, 0, sizeof(fw_msg));
534 bnxt_re_init_hwrm_hdr(rdev, (void *)&req, HWRM_STAT_CTX_FREE, -1, -1);
535 req.stat_ctx_id = cpu_to_le32(fw_stats_ctx_id);
536 bnxt_re_fill_fw_msg(&fw_msg, (void *)&req, sizeof(req), (void *)&req,
537 sizeof(req), DFLT_HWRM_CMD_TIMEOUT);
538 rc = en_dev->en_ops->bnxt_send_fw_msg(en_dev, BNXT_ROCE_ULP, &fw_msg);
540 ibdev_err(&rdev->ibdev, "Failed to free HW stats context %#x",
546 static int bnxt_re_net_stats_ctx_alloc(struct bnxt_re_dev *rdev,
548 u32 *fw_stats_ctx_id)
550 struct hwrm_stat_ctx_alloc_output resp = {0};
551 struct hwrm_stat_ctx_alloc_input req = {0};
552 struct bnxt_en_dev *en_dev = rdev->en_dev;
553 struct bnxt_fw_msg fw_msg;
556 *fw_stats_ctx_id = INVALID_STATS_CTX_ID;
561 memset(&fw_msg, 0, sizeof(fw_msg));
563 bnxt_re_init_hwrm_hdr(rdev, (void *)&req, HWRM_STAT_CTX_ALLOC, -1, -1);
564 req.update_period_ms = cpu_to_le32(1000);
565 req.stats_dma_addr = cpu_to_le64(dma_map);
566 req.stats_dma_length = cpu_to_le16(sizeof(struct ctx_hw_stats_ext));
567 req.stat_ctx_flags = STAT_CTX_ALLOC_REQ_STAT_CTX_FLAGS_ROCE;
568 bnxt_re_fill_fw_msg(&fw_msg, (void *)&req, sizeof(req), (void *)&resp,
569 sizeof(resp), DFLT_HWRM_CMD_TIMEOUT);
570 rc = en_dev->en_ops->bnxt_send_fw_msg(en_dev, BNXT_ROCE_ULP, &fw_msg);
572 *fw_stats_ctx_id = le32_to_cpu(resp.stat_ctx_id);
579 static bool is_bnxt_re_dev(struct net_device *netdev)
581 struct ethtool_drvinfo drvinfo;
583 if (netdev->ethtool_ops && netdev->ethtool_ops->get_drvinfo) {
584 memset(&drvinfo, 0, sizeof(drvinfo));
585 netdev->ethtool_ops->get_drvinfo(netdev, &drvinfo);
587 if (strcmp(drvinfo.driver, "bnxt_en"))
594 static struct bnxt_re_dev *bnxt_re_from_netdev(struct net_device *netdev)
596 struct ib_device *ibdev =
597 ib_device_get_by_netdev(netdev, RDMA_DRIVER_BNXT_RE);
601 return container_of(ibdev, struct bnxt_re_dev, ibdev);
604 static void bnxt_re_dev_unprobe(struct net_device *netdev,
605 struct bnxt_en_dev *en_dev)
608 module_put(en_dev->pdev->driver->driver.owner);
611 static struct bnxt_en_dev *bnxt_re_dev_probe(struct net_device *netdev)
613 struct bnxt *bp = netdev_priv(netdev);
614 struct bnxt_en_dev *en_dev;
615 struct pci_dev *pdev;
617 /* Call bnxt_en's RoCE probe via indirect API */
619 return ERR_PTR(-EINVAL);
621 en_dev = bp->ulp_probe(netdev);
627 return ERR_PTR(-EINVAL);
629 if (!(en_dev->flags & BNXT_EN_FLAG_ROCE_CAP)) {
631 "%s: probe error: RoCE is not supported on this device",
632 ROCE_DRV_MODULE_NAME);
633 return ERR_PTR(-ENODEV);
636 /* Bump net device reference count */
637 if (!try_module_get(pdev->driver->driver.owner))
638 return ERR_PTR(-ENODEV);
645 static ssize_t hw_rev_show(struct device *device, struct device_attribute *attr,
648 struct bnxt_re_dev *rdev =
649 rdma_device_to_drv_device(device, struct bnxt_re_dev, ibdev);
651 return sysfs_emit(buf, "0x%x\n", rdev->en_dev->pdev->vendor);
653 static DEVICE_ATTR_RO(hw_rev);
655 static ssize_t hca_type_show(struct device *device,
656 struct device_attribute *attr, char *buf)
658 struct bnxt_re_dev *rdev =
659 rdma_device_to_drv_device(device, struct bnxt_re_dev, ibdev);
661 return sysfs_emit(buf, "%s\n", rdev->ibdev.node_desc);
663 static DEVICE_ATTR_RO(hca_type);
665 static struct attribute *bnxt_re_attributes[] = {
666 &dev_attr_hw_rev.attr,
667 &dev_attr_hca_type.attr,
671 static const struct attribute_group bnxt_re_dev_attr_group = {
672 .attrs = bnxt_re_attributes,
675 static const struct ib_device_ops bnxt_re_dev_ops = {
676 .owner = THIS_MODULE,
677 .driver_id = RDMA_DRIVER_BNXT_RE,
678 .uverbs_abi_ver = BNXT_RE_ABI_VERSION,
680 .add_gid = bnxt_re_add_gid,
681 .alloc_hw_stats = bnxt_re_ib_alloc_hw_stats,
682 .alloc_mr = bnxt_re_alloc_mr,
683 .alloc_pd = bnxt_re_alloc_pd,
684 .alloc_ucontext = bnxt_re_alloc_ucontext,
685 .create_ah = bnxt_re_create_ah,
686 .create_cq = bnxt_re_create_cq,
687 .create_qp = bnxt_re_create_qp,
688 .create_srq = bnxt_re_create_srq,
689 .create_user_ah = bnxt_re_create_ah,
690 .dealloc_driver = bnxt_re_dealloc_driver,
691 .dealloc_pd = bnxt_re_dealloc_pd,
692 .dealloc_ucontext = bnxt_re_dealloc_ucontext,
693 .del_gid = bnxt_re_del_gid,
694 .dereg_mr = bnxt_re_dereg_mr,
695 .destroy_ah = bnxt_re_destroy_ah,
696 .destroy_cq = bnxt_re_destroy_cq,
697 .destroy_qp = bnxt_re_destroy_qp,
698 .destroy_srq = bnxt_re_destroy_srq,
699 .get_dev_fw_str = bnxt_re_query_fw_str,
700 .get_dma_mr = bnxt_re_get_dma_mr,
701 .get_hw_stats = bnxt_re_ib_get_hw_stats,
702 .get_link_layer = bnxt_re_get_link_layer,
703 .get_port_immutable = bnxt_re_get_port_immutable,
704 .map_mr_sg = bnxt_re_map_mr_sg,
705 .mmap = bnxt_re_mmap,
706 .modify_ah = bnxt_re_modify_ah,
707 .modify_qp = bnxt_re_modify_qp,
708 .modify_srq = bnxt_re_modify_srq,
709 .poll_cq = bnxt_re_poll_cq,
710 .post_recv = bnxt_re_post_recv,
711 .post_send = bnxt_re_post_send,
712 .post_srq_recv = bnxt_re_post_srq_recv,
713 .query_ah = bnxt_re_query_ah,
714 .query_device = bnxt_re_query_device,
715 .query_pkey = bnxt_re_query_pkey,
716 .query_port = bnxt_re_query_port,
717 .query_qp = bnxt_re_query_qp,
718 .query_srq = bnxt_re_query_srq,
719 .reg_user_mr = bnxt_re_reg_user_mr,
720 .req_notify_cq = bnxt_re_req_notify_cq,
721 INIT_RDMA_OBJ_SIZE(ib_ah, bnxt_re_ah, ib_ah),
722 INIT_RDMA_OBJ_SIZE(ib_cq, bnxt_re_cq, ib_cq),
723 INIT_RDMA_OBJ_SIZE(ib_pd, bnxt_re_pd, ib_pd),
724 INIT_RDMA_OBJ_SIZE(ib_srq, bnxt_re_srq, ib_srq),
725 INIT_RDMA_OBJ_SIZE(ib_ucontext, bnxt_re_ucontext, ib_uctx),
728 static int bnxt_re_register_ib(struct bnxt_re_dev *rdev)
730 struct ib_device *ibdev = &rdev->ibdev;
734 ibdev->node_type = RDMA_NODE_IB_CA;
735 strlcpy(ibdev->node_desc, BNXT_RE_DESC " HCA",
736 strlen(BNXT_RE_DESC) + 5);
737 ibdev->phys_port_cnt = 1;
739 bnxt_qplib_get_guid(rdev->netdev->dev_addr, (u8 *)&ibdev->node_guid);
741 ibdev->num_comp_vectors = rdev->num_msix - 1;
742 ibdev->dev.parent = &rdev->en_dev->pdev->dev;
743 ibdev->local_dma_lkey = BNXT_QPLIB_RSVD_LKEY;
745 rdma_set_device_sysfs_group(ibdev, &bnxt_re_dev_attr_group);
746 ib_set_device_ops(ibdev, &bnxt_re_dev_ops);
747 ret = ib_device_set_netdev(&rdev->ibdev, rdev->netdev, 1);
751 dma_set_max_seg_size(&rdev->en_dev->pdev->dev, UINT_MAX);
752 return ib_register_device(ibdev, "bnxt_re%d", &rdev->en_dev->pdev->dev);
755 static void bnxt_re_dev_remove(struct bnxt_re_dev *rdev)
757 dev_put(rdev->netdev);
759 mutex_lock(&bnxt_re_dev_lock);
760 list_del_rcu(&rdev->list);
761 mutex_unlock(&bnxt_re_dev_lock);
766 static struct bnxt_re_dev *bnxt_re_dev_add(struct net_device *netdev,
767 struct bnxt_en_dev *en_dev)
769 struct bnxt_re_dev *rdev;
771 /* Allocate bnxt_re_dev instance here */
772 rdev = ib_alloc_device(bnxt_re_dev, ibdev);
774 ibdev_err(NULL, "%s: bnxt_re_dev allocation failure!",
775 ROCE_DRV_MODULE_NAME);
779 rdev->netdev = netdev;
780 dev_hold(rdev->netdev);
781 rdev->en_dev = en_dev;
782 rdev->id = rdev->en_dev->pdev->devfn;
783 INIT_LIST_HEAD(&rdev->qp_list);
784 mutex_init(&rdev->qp_lock);
785 atomic_set(&rdev->qp_count, 0);
786 atomic_set(&rdev->cq_count, 0);
787 atomic_set(&rdev->srq_count, 0);
788 atomic_set(&rdev->mr_count, 0);
789 atomic_set(&rdev->mw_count, 0);
790 rdev->cosq[0] = 0xFFFF;
791 rdev->cosq[1] = 0xFFFF;
793 mutex_lock(&bnxt_re_dev_lock);
794 list_add_tail_rcu(&rdev->list, &bnxt_re_dev_list);
795 mutex_unlock(&bnxt_re_dev_lock);
799 static int bnxt_re_handle_unaffi_async_event(struct creq_func_event
802 switch (unaffi_async->event) {
803 case CREQ_FUNC_EVENT_EVENT_TX_WQE_ERROR:
805 case CREQ_FUNC_EVENT_EVENT_TX_DATA_ERROR:
807 case CREQ_FUNC_EVENT_EVENT_RX_WQE_ERROR:
809 case CREQ_FUNC_EVENT_EVENT_RX_DATA_ERROR:
811 case CREQ_FUNC_EVENT_EVENT_CQ_ERROR:
813 case CREQ_FUNC_EVENT_EVENT_TQM_ERROR:
815 case CREQ_FUNC_EVENT_EVENT_CFCQ_ERROR:
817 case CREQ_FUNC_EVENT_EVENT_CFCS_ERROR:
819 case CREQ_FUNC_EVENT_EVENT_CFCC_ERROR:
821 case CREQ_FUNC_EVENT_EVENT_CFCM_ERROR:
823 case CREQ_FUNC_EVENT_EVENT_TIM_ERROR:
831 static int bnxt_re_handle_qp_async_event(struct creq_qp_event *qp_event,
832 struct bnxt_re_qp *qp)
834 struct ib_event event;
837 if (qp->qplib_qp.state == CMDQ_MODIFY_QP_NEW_STATE_ERR &&
838 rdma_is_kernel_res(&qp->ib_qp.res)) {
839 flags = bnxt_re_lock_cqs(qp);
840 bnxt_qplib_add_flush_qp(&qp->qplib_qp);
841 bnxt_re_unlock_cqs(qp, flags);
844 memset(&event, 0, sizeof(event));
845 if (qp->qplib_qp.srq) {
846 event.device = &qp->rdev->ibdev;
847 event.element.qp = &qp->ib_qp;
848 event.event = IB_EVENT_QP_LAST_WQE_REACHED;
851 if (event.device && qp->ib_qp.event_handler)
852 qp->ib_qp.event_handler(&event, qp->ib_qp.qp_context);
857 static int bnxt_re_handle_affi_async_event(struct creq_qp_event *affi_async,
864 return rc; /* QP was already dead, still return success */
866 event = affi_async->event;
867 if (event == CREQ_QP_EVENT_EVENT_QP_ERROR_NOTIFICATION) {
868 struct bnxt_qplib_qp *lib_qp = obj;
869 struct bnxt_re_qp *qp = container_of(lib_qp, struct bnxt_re_qp,
871 rc = bnxt_re_handle_qp_async_event(affi_async, qp);
876 static int bnxt_re_aeq_handler(struct bnxt_qplib_rcfw *rcfw,
877 void *aeqe, void *obj)
879 struct creq_qp_event *affi_async;
880 struct creq_func_event *unaffi_async;
884 type = ((struct creq_base *)aeqe)->type;
885 if (type == CREQ_BASE_TYPE_FUNC_EVENT) {
887 rc = bnxt_re_handle_unaffi_async_event(unaffi_async);
890 rc = bnxt_re_handle_affi_async_event(affi_async, obj);
896 static int bnxt_re_srqn_handler(struct bnxt_qplib_nq *nq,
897 struct bnxt_qplib_srq *handle, u8 event)
899 struct bnxt_re_srq *srq = container_of(handle, struct bnxt_re_srq,
901 struct ib_event ib_event;
905 ibdev_err(NULL, "%s: SRQ is NULL, SRQN not handled",
906 ROCE_DRV_MODULE_NAME);
910 ib_event.device = &srq->rdev->ibdev;
911 ib_event.element.srq = &srq->ib_srq;
912 if (event == NQ_SRQ_EVENT_EVENT_SRQ_THRESHOLD_EVENT)
913 ib_event.event = IB_EVENT_SRQ_LIMIT_REACHED;
915 ib_event.event = IB_EVENT_SRQ_ERR;
917 if (srq->ib_srq.event_handler) {
918 /* Lock event_handler? */
919 (*srq->ib_srq.event_handler)(&ib_event,
920 srq->ib_srq.srq_context);
926 static int bnxt_re_cqn_handler(struct bnxt_qplib_nq *nq,
927 struct bnxt_qplib_cq *handle)
929 struct bnxt_re_cq *cq = container_of(handle, struct bnxt_re_cq,
933 ibdev_err(NULL, "%s: CQ is NULL, CQN not handled",
934 ROCE_DRV_MODULE_NAME);
937 if (cq->ib_cq.comp_handler) {
938 /* Lock comp_handler? */
939 (*cq->ib_cq.comp_handler)(&cq->ib_cq, cq->ib_cq.cq_context);
945 #define BNXT_RE_GEN_P5_PF_NQ_DB 0x10000
946 #define BNXT_RE_GEN_P5_VF_NQ_DB 0x4000
947 static u32 bnxt_re_get_nqdb_offset(struct bnxt_re_dev *rdev, u16 indx)
949 return bnxt_qplib_is_chip_gen_p5(rdev->chip_ctx) ?
950 (rdev->is_virtfn ? BNXT_RE_GEN_P5_VF_NQ_DB :
951 BNXT_RE_GEN_P5_PF_NQ_DB) :
952 rdev->msix_entries[indx].db_offset;
955 static void bnxt_re_cleanup_res(struct bnxt_re_dev *rdev)
959 for (i = 1; i < rdev->num_msix; i++)
960 bnxt_qplib_disable_nq(&rdev->nq[i - 1]);
962 if (rdev->qplib_res.rcfw)
963 bnxt_qplib_cleanup_res(&rdev->qplib_res);
966 static int bnxt_re_init_res(struct bnxt_re_dev *rdev)
968 int num_vec_enabled = 0;
972 bnxt_qplib_init_res(&rdev->qplib_res);
974 for (i = 1; i < rdev->num_msix ; i++) {
975 db_offt = bnxt_re_get_nqdb_offset(rdev, i);
976 rc = bnxt_qplib_enable_nq(rdev->en_dev->pdev, &rdev->nq[i - 1],
977 i - 1, rdev->msix_entries[i].vector,
978 db_offt, &bnxt_re_cqn_handler,
979 &bnxt_re_srqn_handler);
981 ibdev_err(&rdev->ibdev,
982 "Failed to enable NQ with rc = 0x%x", rc);
989 for (i = num_vec_enabled; i >= 0; i--)
990 bnxt_qplib_disable_nq(&rdev->nq[i]);
994 static void bnxt_re_free_nq_res(struct bnxt_re_dev *rdev)
999 for (i = 0; i < rdev->num_msix - 1; i++) {
1000 type = bnxt_qplib_get_ring_type(rdev->chip_ctx);
1001 bnxt_re_net_ring_free(rdev, rdev->nq[i].ring_id, type);
1002 bnxt_qplib_free_nq(&rdev->nq[i]);
1003 rdev->nq[i].res = NULL;
1007 static void bnxt_re_free_res(struct bnxt_re_dev *rdev)
1009 bnxt_re_free_nq_res(rdev);
1011 if (rdev->qplib_res.dpi_tbl.max) {
1012 bnxt_qplib_dealloc_dpi(&rdev->qplib_res,
1013 &rdev->qplib_res.dpi_tbl,
1014 &rdev->dpi_privileged);
1016 if (rdev->qplib_res.rcfw) {
1017 bnxt_qplib_free_res(&rdev->qplib_res);
1018 rdev->qplib_res.rcfw = NULL;
1022 static int bnxt_re_alloc_res(struct bnxt_re_dev *rdev)
1024 struct bnxt_re_ring_attr rattr = {};
1025 int num_vec_created = 0;
1029 /* Configure and allocate resources for qplib */
1030 rdev->qplib_res.rcfw = &rdev->rcfw;
1031 rc = bnxt_qplib_get_dev_attr(&rdev->rcfw, &rdev->dev_attr,
1036 rc = bnxt_qplib_alloc_res(&rdev->qplib_res, rdev->en_dev->pdev,
1037 rdev->netdev, &rdev->dev_attr);
1041 rc = bnxt_qplib_alloc_dpi(&rdev->qplib_res.dpi_tbl,
1042 &rdev->dpi_privileged,
1047 for (i = 0; i < rdev->num_msix - 1; i++) {
1048 struct bnxt_qplib_nq *nq;
1051 nq->hwq.max_elements = BNXT_QPLIB_NQE_MAX_CNT;
1052 rc = bnxt_qplib_alloc_nq(&rdev->qplib_res, &rdev->nq[i]);
1054 ibdev_err(&rdev->ibdev, "Alloc Failed NQ%d rc:%#x",
1058 type = bnxt_qplib_get_ring_type(rdev->chip_ctx);
1059 rattr.dma_arr = nq->hwq.pbl[PBL_LVL_0].pg_map_arr;
1060 rattr.pages = nq->hwq.pbl[rdev->nq[i].hwq.level].pg_count;
1062 rattr.mode = RING_ALLOC_REQ_INT_MODE_MSIX;
1063 rattr.depth = BNXT_QPLIB_NQE_MAX_CNT - 1;
1064 rattr.lrid = rdev->msix_entries[i + 1].ring_idx;
1065 rc = bnxt_re_net_ring_alloc(rdev, &rattr, &nq->ring_id);
1067 ibdev_err(&rdev->ibdev,
1068 "Failed to allocate NQ fw id with rc = 0x%x",
1070 bnxt_qplib_free_nq(&rdev->nq[i]);
1077 for (i = num_vec_created - 1; i >= 0; i--) {
1078 type = bnxt_qplib_get_ring_type(rdev->chip_ctx);
1079 bnxt_re_net_ring_free(rdev, rdev->nq[i].ring_id, type);
1080 bnxt_qplib_free_nq(&rdev->nq[i]);
1082 bnxt_qplib_dealloc_dpi(&rdev->qplib_res,
1083 &rdev->qplib_res.dpi_tbl,
1084 &rdev->dpi_privileged);
1086 bnxt_qplib_free_res(&rdev->qplib_res);
1089 rdev->qplib_res.rcfw = NULL;
1093 static void bnxt_re_dispatch_event(struct ib_device *ibdev, struct ib_qp *qp,
1094 u8 port_num, enum ib_event_type event)
1096 struct ib_event ib_event;
1098 ib_event.device = ibdev;
1100 ib_event.element.qp = qp;
1101 ib_event.event = event;
1102 if (qp->event_handler)
1103 qp->event_handler(&ib_event, qp->qp_context);
1106 ib_event.element.port_num = port_num;
1107 ib_event.event = event;
1108 ib_dispatch_event(&ib_event);
1112 #define HWRM_QUEUE_PRI2COS_QCFG_INPUT_FLAGS_IVLAN 0x02
1113 static int bnxt_re_query_hwrm_pri2cos(struct bnxt_re_dev *rdev, u8 dir,
1116 struct hwrm_queue_pri2cos_qcfg_input req = {0};
1117 struct bnxt *bp = netdev_priv(rdev->netdev);
1118 struct hwrm_queue_pri2cos_qcfg_output resp;
1119 struct bnxt_en_dev *en_dev = rdev->en_dev;
1120 struct bnxt_fw_msg fw_msg;
1122 u8 *qcfgmap, *tmp_map;
1128 memset(&fw_msg, 0, sizeof(fw_msg));
1129 bnxt_re_init_hwrm_hdr(rdev, (void *)&req,
1130 HWRM_QUEUE_PRI2COS_QCFG, -1, -1);
1131 flags |= (dir & 0x01);
1132 flags |= HWRM_QUEUE_PRI2COS_QCFG_INPUT_FLAGS_IVLAN;
1133 req.flags = cpu_to_le32(flags);
1134 req.port_id = bp->pf.port_id;
1136 bnxt_re_fill_fw_msg(&fw_msg, (void *)&req, sizeof(req), (void *)&resp,
1137 sizeof(resp), DFLT_HWRM_CMD_TIMEOUT);
1138 rc = en_dev->en_ops->bnxt_send_fw_msg(en_dev, BNXT_ROCE_ULP, &fw_msg);
1142 if (resp.queue_cfg_info) {
1143 ibdev_warn(&rdev->ibdev,
1144 "Asymmetric cos queue configuration detected");
1145 ibdev_warn(&rdev->ibdev,
1146 " on device, QoS may not be fully functional\n");
1148 qcfgmap = &resp.pri0_cos_queue_id;
1149 tmp_map = (u8 *)cid_map;
1150 for (i = 0; i < IEEE_8021QAZ_MAX_TCS; i++)
1151 tmp_map[i] = qcfgmap[i];
1156 static bool bnxt_re_is_qp1_or_shadow_qp(struct bnxt_re_dev *rdev,
1157 struct bnxt_re_qp *qp)
1159 return (qp->ib_qp.qp_type == IB_QPT_GSI) ||
1160 (qp == rdev->gsi_ctx.gsi_sqp);
1163 static void bnxt_re_dev_stop(struct bnxt_re_dev *rdev)
1165 int mask = IB_QP_STATE;
1166 struct ib_qp_attr qp_attr;
1167 struct bnxt_re_qp *qp;
1169 qp_attr.qp_state = IB_QPS_ERR;
1170 mutex_lock(&rdev->qp_lock);
1171 list_for_each_entry(qp, &rdev->qp_list, list) {
1172 /* Modify the state of all QPs except QP1/Shadow QP */
1173 if (!bnxt_re_is_qp1_or_shadow_qp(rdev, qp)) {
1174 if (qp->qplib_qp.state !=
1175 CMDQ_MODIFY_QP_NEW_STATE_RESET &&
1176 qp->qplib_qp.state !=
1177 CMDQ_MODIFY_QP_NEW_STATE_ERR) {
1178 bnxt_re_dispatch_event(&rdev->ibdev, &qp->ib_qp,
1179 1, IB_EVENT_QP_FATAL);
1180 bnxt_re_modify_qp(&qp->ib_qp, &qp_attr, mask,
1185 mutex_unlock(&rdev->qp_lock);
1188 static int bnxt_re_update_gid(struct bnxt_re_dev *rdev)
1190 struct bnxt_qplib_sgid_tbl *sgid_tbl = &rdev->qplib_res.sgid_tbl;
1191 struct bnxt_qplib_gid gid;
1195 if (!ib_device_try_get(&rdev->ibdev))
1199 ibdev_err(&rdev->ibdev, "QPLIB: SGID table not allocated");
1204 for (index = 0; index < sgid_tbl->active; index++) {
1205 gid_idx = sgid_tbl->hw_id[index];
1207 if (!memcmp(&sgid_tbl->tbl[index], &bnxt_qplib_gid_zero,
1208 sizeof(bnxt_qplib_gid_zero)))
1210 /* need to modify the VLAN enable setting of non VLAN GID only
1211 * as setting is done for VLAN GID while adding GID
1213 if (sgid_tbl->vlan[index])
1216 memcpy(&gid, &sgid_tbl->tbl[index], sizeof(gid));
1218 rc = bnxt_qplib_update_sgid(sgid_tbl, &gid, gid_idx,
1219 rdev->qplib_res.netdev->dev_addr);
1222 ib_device_put(&rdev->ibdev);
1226 static u32 bnxt_re_get_priority_mask(struct bnxt_re_dev *rdev)
1228 u32 prio_map = 0, tmp_map = 0;
1229 struct net_device *netdev;
1232 netdev = rdev->netdev;
1234 memset(&app, 0, sizeof(app));
1235 app.selector = IEEE_8021QAZ_APP_SEL_ETHERTYPE;
1236 app.protocol = ETH_P_IBOE;
1237 tmp_map = dcb_ieee_getapp_mask(netdev, &app);
1240 app.selector = IEEE_8021QAZ_APP_SEL_DGRAM;
1241 app.protocol = ROCE_V2_UDP_DPORT;
1242 tmp_map = dcb_ieee_getapp_mask(netdev, &app);
1243 prio_map |= tmp_map;
1248 static void bnxt_re_parse_cid_map(u8 prio_map, u8 *cid_map, u16 *cosq)
1253 for (prio = 0, id = 0; prio < 8; prio++) {
1254 if (prio_map & (1 << prio)) {
1255 cosq[id] = cid_map[prio];
1257 if (id == 2) /* Max 2 tcs supported */
1263 static int bnxt_re_setup_qos(struct bnxt_re_dev *rdev)
1269 /* Get priority for roce */
1270 prio_map = bnxt_re_get_priority_mask(rdev);
1272 if (prio_map == rdev->cur_prio_map)
1274 rdev->cur_prio_map = prio_map;
1275 /* Get cosq id for this priority */
1276 rc = bnxt_re_query_hwrm_pri2cos(rdev, 0, &cid_map);
1278 ibdev_warn(&rdev->ibdev, "no cos for p_mask %x\n", prio_map);
1281 /* Parse CoS IDs for app priority */
1282 bnxt_re_parse_cid_map(prio_map, (u8 *)&cid_map, rdev->cosq);
1285 rc = bnxt_qplib_map_tc2cos(&rdev->qplib_res, rdev->cosq);
1287 ibdev_warn(&rdev->ibdev, "no tc for cos{%x, %x}\n",
1288 rdev->cosq[0], rdev->cosq[1]);
1292 /* Actual priorities are not programmed as they are already
1293 * done by L2 driver; just enable or disable priority vlan tagging
1295 if ((prio_map == 0 && rdev->qplib_res.prio) ||
1296 (prio_map != 0 && !rdev->qplib_res.prio)) {
1297 rdev->qplib_res.prio = prio_map ? true : false;
1299 bnxt_re_update_gid(rdev);
1305 static void bnxt_re_query_hwrm_intf_version(struct bnxt_re_dev *rdev)
1307 struct bnxt_en_dev *en_dev = rdev->en_dev;
1308 struct hwrm_ver_get_output resp = {0};
1309 struct hwrm_ver_get_input req = {0};
1310 struct bnxt_fw_msg fw_msg;
1313 memset(&fw_msg, 0, sizeof(fw_msg));
1314 bnxt_re_init_hwrm_hdr(rdev, (void *)&req,
1315 HWRM_VER_GET, -1, -1);
1316 req.hwrm_intf_maj = HWRM_VERSION_MAJOR;
1317 req.hwrm_intf_min = HWRM_VERSION_MINOR;
1318 req.hwrm_intf_upd = HWRM_VERSION_UPDATE;
1319 bnxt_re_fill_fw_msg(&fw_msg, (void *)&req, sizeof(req), (void *)&resp,
1320 sizeof(resp), DFLT_HWRM_CMD_TIMEOUT);
1321 rc = en_dev->en_ops->bnxt_send_fw_msg(en_dev, BNXT_ROCE_ULP, &fw_msg);
1323 ibdev_err(&rdev->ibdev, "Failed to query HW version, rc = 0x%x",
1327 rdev->qplib_ctx.hwrm_intf_ver =
1328 (u64)le16_to_cpu(resp.hwrm_intf_major) << 48 |
1329 (u64)le16_to_cpu(resp.hwrm_intf_minor) << 32 |
1330 (u64)le16_to_cpu(resp.hwrm_intf_build) << 16 |
1331 le16_to_cpu(resp.hwrm_intf_patch);
1334 static int bnxt_re_ib_init(struct bnxt_re_dev *rdev)
1339 /* Register ib dev */
1340 rc = bnxt_re_register_ib(rdev);
1342 pr_err("Failed to register with IB: %#x\n", rc);
1345 dev_info(rdev_to_dev(rdev), "Device registered successfully");
1346 ib_get_eth_speed(&rdev->ibdev, 1, &rdev->active_speed,
1347 &rdev->active_width);
1348 set_bit(BNXT_RE_FLAG_ISSUE_ROCE_STATS, &rdev->flags);
1350 event = netif_running(rdev->netdev) && netif_carrier_ok(rdev->netdev) ?
1351 IB_EVENT_PORT_ACTIVE : IB_EVENT_PORT_ERR;
1353 bnxt_re_dispatch_event(&rdev->ibdev, NULL, 1, event);
1358 static void bnxt_re_dev_uninit(struct bnxt_re_dev *rdev)
1363 if (test_and_clear_bit(BNXT_RE_FLAG_QOS_WORK_REG, &rdev->flags))
1364 cancel_delayed_work_sync(&rdev->worker);
1366 if (test_and_clear_bit(BNXT_RE_FLAG_RESOURCES_INITIALIZED,
1368 bnxt_re_cleanup_res(rdev);
1369 if (test_and_clear_bit(BNXT_RE_FLAG_RESOURCES_ALLOCATED, &rdev->flags))
1370 bnxt_re_free_res(rdev);
1372 if (test_and_clear_bit(BNXT_RE_FLAG_RCFW_CHANNEL_EN, &rdev->flags)) {
1373 rc = bnxt_qplib_deinit_rcfw(&rdev->rcfw);
1375 ibdev_warn(&rdev->ibdev,
1376 "Failed to deinitialize RCFW: %#x", rc);
1377 bnxt_re_net_stats_ctx_free(rdev, rdev->qplib_ctx.stats.fw_id);
1378 bnxt_qplib_free_ctx(&rdev->qplib_res, &rdev->qplib_ctx);
1379 bnxt_qplib_disable_rcfw_channel(&rdev->rcfw);
1380 type = bnxt_qplib_get_ring_type(rdev->chip_ctx);
1381 bnxt_re_net_ring_free(rdev, rdev->rcfw.creq.ring_id, type);
1382 bnxt_qplib_free_rcfw_channel(&rdev->rcfw);
1384 if (test_and_clear_bit(BNXT_RE_FLAG_GOT_MSIX, &rdev->flags)) {
1385 rc = bnxt_re_free_msix(rdev);
1387 ibdev_warn(&rdev->ibdev,
1388 "Failed to free MSI-X vectors: %#x", rc);
1391 bnxt_re_destroy_chip_ctx(rdev);
1392 if (test_and_clear_bit(BNXT_RE_FLAG_NETDEV_REGISTERED, &rdev->flags)) {
1393 rc = bnxt_re_unregister_netdev(rdev);
1395 ibdev_warn(&rdev->ibdev,
1396 "Failed to unregister with netdev: %#x", rc);
1400 /* worker thread for polling periodic events. Now used for QoS programming*/
1401 static void bnxt_re_worker(struct work_struct *work)
1403 struct bnxt_re_dev *rdev = container_of(work, struct bnxt_re_dev,
1406 bnxt_re_setup_qos(rdev);
1407 schedule_delayed_work(&rdev->worker, msecs_to_jiffies(30000));
1410 static int bnxt_re_dev_init(struct bnxt_re_dev *rdev, u8 wqe_mode)
1412 struct bnxt_qplib_creq_ctx *creq;
1413 struct bnxt_re_ring_attr rattr;
1419 /* Registered a new RoCE device instance to netdev */
1420 memset(&rattr, 0, sizeof(rattr));
1421 rc = bnxt_re_register_netdev(rdev);
1424 ibdev_err(&rdev->ibdev,
1425 "Failed to register with netedev: %#x\n", rc);
1428 set_bit(BNXT_RE_FLAG_NETDEV_REGISTERED, &rdev->flags);
1430 rc = bnxt_re_setup_chip_ctx(rdev, wqe_mode);
1432 ibdev_err(&rdev->ibdev, "Failed to get chip context\n");
1436 /* Check whether VF or PF */
1437 bnxt_re_get_sriov_func_type(rdev);
1439 rc = bnxt_re_request_msix(rdev);
1441 ibdev_err(&rdev->ibdev,
1442 "Failed to get MSI-X vectors: %#x\n", rc);
1446 set_bit(BNXT_RE_FLAG_GOT_MSIX, &rdev->flags);
1448 bnxt_re_query_hwrm_intf_version(rdev);
1450 /* Establish RCFW Communication Channel to initialize the context
1451 * memory for the function and all child VFs
1453 rc = bnxt_qplib_alloc_rcfw_channel(&rdev->qplib_res, &rdev->rcfw,
1455 BNXT_RE_MAX_QPC_COUNT);
1457 ibdev_err(&rdev->ibdev,
1458 "Failed to allocate RCFW Channel: %#x\n", rc);
1462 type = bnxt_qplib_get_ring_type(rdev->chip_ctx);
1463 creq = &rdev->rcfw.creq;
1464 rattr.dma_arr = creq->hwq.pbl[PBL_LVL_0].pg_map_arr;
1465 rattr.pages = creq->hwq.pbl[creq->hwq.level].pg_count;
1467 rattr.mode = RING_ALLOC_REQ_INT_MODE_MSIX;
1468 rattr.depth = BNXT_QPLIB_CREQE_MAX_CNT - 1;
1469 rattr.lrid = rdev->msix_entries[BNXT_RE_AEQ_IDX].ring_idx;
1470 rc = bnxt_re_net_ring_alloc(rdev, &rattr, &creq->ring_id);
1472 ibdev_err(&rdev->ibdev, "Failed to allocate CREQ: %#x\n", rc);
1475 db_offt = bnxt_re_get_nqdb_offset(rdev, BNXT_RE_AEQ_IDX);
1476 vid = rdev->msix_entries[BNXT_RE_AEQ_IDX].vector;
1477 rc = bnxt_qplib_enable_rcfw_channel(&rdev->rcfw,
1478 vid, db_offt, rdev->is_virtfn,
1479 &bnxt_re_aeq_handler);
1481 ibdev_err(&rdev->ibdev, "Failed to enable RCFW channel: %#x\n",
1486 rc = bnxt_qplib_get_dev_attr(&rdev->rcfw, &rdev->dev_attr,
1491 bnxt_re_set_resource_limits(rdev);
1493 rc = bnxt_qplib_alloc_ctx(&rdev->qplib_res, &rdev->qplib_ctx, 0,
1494 bnxt_qplib_is_chip_gen_p5(rdev->chip_ctx));
1496 ibdev_err(&rdev->ibdev,
1497 "Failed to allocate QPLIB context: %#x\n", rc);
1500 rc = bnxt_re_net_stats_ctx_alloc(rdev,
1501 rdev->qplib_ctx.stats.dma_map,
1502 &rdev->qplib_ctx.stats.fw_id);
1504 ibdev_err(&rdev->ibdev,
1505 "Failed to allocate stats context: %#x\n", rc);
1509 rc = bnxt_qplib_init_rcfw(&rdev->rcfw, &rdev->qplib_ctx,
1512 ibdev_err(&rdev->ibdev,
1513 "Failed to initialize RCFW: %#x\n", rc);
1516 set_bit(BNXT_RE_FLAG_RCFW_CHANNEL_EN, &rdev->flags);
1518 /* Resources based on the 'new' device caps */
1519 rc = bnxt_re_alloc_res(rdev);
1521 ibdev_err(&rdev->ibdev,
1522 "Failed to allocate resources: %#x\n", rc);
1525 set_bit(BNXT_RE_FLAG_RESOURCES_ALLOCATED, &rdev->flags);
1526 rc = bnxt_re_init_res(rdev);
1528 ibdev_err(&rdev->ibdev,
1529 "Failed to initialize resources: %#x\n", rc);
1533 set_bit(BNXT_RE_FLAG_RESOURCES_INITIALIZED, &rdev->flags);
1535 if (!rdev->is_virtfn) {
1536 rc = bnxt_re_setup_qos(rdev);
1538 ibdev_info(&rdev->ibdev,
1539 "RoCE priority not yet configured\n");
1541 INIT_DELAYED_WORK(&rdev->worker, bnxt_re_worker);
1542 set_bit(BNXT_RE_FLAG_QOS_WORK_REG, &rdev->flags);
1543 schedule_delayed_work(&rdev->worker, msecs_to_jiffies(30000));
1548 bnxt_re_net_stats_ctx_free(rdev, rdev->qplib_ctx.stats.fw_id);
1550 bnxt_qplib_free_ctx(&rdev->qplib_res, &rdev->qplib_ctx);
1552 bnxt_qplib_disable_rcfw_channel(&rdev->rcfw);
1554 type = bnxt_qplib_get_ring_type(rdev->chip_ctx);
1555 bnxt_re_net_ring_free(rdev, rdev->rcfw.creq.ring_id, type);
1557 bnxt_qplib_free_rcfw_channel(&rdev->rcfw);
1559 bnxt_re_dev_uninit(rdev);
1564 static void bnxt_re_dev_unreg(struct bnxt_re_dev *rdev)
1566 struct bnxt_en_dev *en_dev = rdev->en_dev;
1567 struct net_device *netdev = rdev->netdev;
1569 bnxt_re_dev_remove(rdev);
1572 bnxt_re_dev_unprobe(netdev, en_dev);
1575 static int bnxt_re_dev_reg(struct bnxt_re_dev **rdev, struct net_device *netdev)
1577 struct bnxt_en_dev *en_dev;
1580 if (!is_bnxt_re_dev(netdev))
1583 en_dev = bnxt_re_dev_probe(netdev);
1584 if (IS_ERR(en_dev)) {
1585 if (en_dev != ERR_PTR(-ENODEV))
1586 ibdev_err(&(*rdev)->ibdev, "%s: Failed to probe\n",
1587 ROCE_DRV_MODULE_NAME);
1588 rc = PTR_ERR(en_dev);
1591 *rdev = bnxt_re_dev_add(netdev, en_dev);
1594 bnxt_re_dev_unprobe(netdev, en_dev);
1601 static void bnxt_re_remove_device(struct bnxt_re_dev *rdev)
1603 bnxt_re_dev_uninit(rdev);
1604 pci_dev_put(rdev->en_dev->pdev);
1605 bnxt_re_dev_unreg(rdev);
1608 static int bnxt_re_add_device(struct bnxt_re_dev **rdev,
1609 struct net_device *netdev, u8 wqe_mode)
1613 rc = bnxt_re_dev_reg(rdev, netdev);
1617 pr_err("Failed to register with the device %s: %#x\n",
1622 pci_dev_get((*rdev)->en_dev->pdev);
1623 rc = bnxt_re_dev_init(*rdev, wqe_mode);
1625 pci_dev_put((*rdev)->en_dev->pdev);
1626 bnxt_re_dev_unreg(*rdev);
1632 static void bnxt_re_dealloc_driver(struct ib_device *ib_dev)
1634 struct bnxt_re_dev *rdev =
1635 container_of(ib_dev, struct bnxt_re_dev, ibdev);
1637 dev_info(rdev_to_dev(rdev), "Unregistering Device");
1640 bnxt_re_remove_device(rdev);
1644 /* Handle all deferred netevents tasks */
1645 static void bnxt_re_task(struct work_struct *work)
1647 struct bnxt_re_work *re_work;
1648 struct bnxt_re_dev *rdev;
1651 re_work = container_of(work, struct bnxt_re_work, work);
1652 rdev = re_work->rdev;
1654 if (re_work->event == NETDEV_REGISTER) {
1655 rc = bnxt_re_ib_init(rdev);
1657 ibdev_err(&rdev->ibdev,
1658 "Failed to register with IB: %#x", rc);
1660 bnxt_re_remove_device(rdev);
1667 if (!ib_device_try_get(&rdev->ibdev))
1670 switch (re_work->event) {
1672 bnxt_re_dispatch_event(&rdev->ibdev, NULL, 1,
1673 IB_EVENT_PORT_ACTIVE);
1676 bnxt_re_dev_stop(rdev);
1679 if (!netif_carrier_ok(rdev->netdev))
1680 bnxt_re_dev_stop(rdev);
1681 else if (netif_carrier_ok(rdev->netdev))
1682 bnxt_re_dispatch_event(&rdev->ibdev, NULL, 1,
1683 IB_EVENT_PORT_ACTIVE);
1684 ib_get_eth_speed(&rdev->ibdev, 1, &rdev->active_speed,
1685 &rdev->active_width);
1690 ib_device_put(&rdev->ibdev);
1692 put_device(&rdev->ibdev.dev);
1697 * "Notifier chain callback can be invoked for the same chain from
1698 * different CPUs at the same time".
1700 * For cases when the netdev is already present, our call to the
1701 * register_netdevice_notifier() will actually get the rtnl_lock()
1702 * before sending NETDEV_REGISTER and (if up) NETDEV_UP
1705 * But for cases when the netdev is not already present, the notifier
1706 * chain is subjected to be invoked from different CPUs simultaneously.
1708 * This is protected by the netdev_mutex.
1710 static int bnxt_re_netdev_event(struct notifier_block *notifier,
1711 unsigned long event, void *ptr)
1713 struct net_device *real_dev, *netdev = netdev_notifier_info_to_dev(ptr);
1714 struct bnxt_re_work *re_work;
1715 struct bnxt_re_dev *rdev;
1717 bool sch_work = false;
1718 bool release = true;
1720 real_dev = rdma_vlan_dev_real_dev(netdev);
1724 rdev = bnxt_re_from_netdev(real_dev);
1725 if (!rdev && event != NETDEV_REGISTER)
1728 if (real_dev != netdev)
1732 case NETDEV_REGISTER:
1735 rc = bnxt_re_add_device(&rdev, real_dev,
1736 BNXT_QPLIB_WQE_MODE_STATIC);
1742 case NETDEV_UNREGISTER:
1743 ib_unregister_device_queued(&rdev->ibdev);
1751 /* Allocate for the deferred task */
1752 re_work = kzalloc(sizeof(*re_work), GFP_ATOMIC);
1754 get_device(&rdev->ibdev.dev);
1755 re_work->rdev = rdev;
1756 re_work->event = event;
1757 re_work->vlan_dev = (real_dev == netdev ?
1759 INIT_WORK(&re_work->work, bnxt_re_task);
1760 queue_work(bnxt_re_wq, &re_work->work);
1765 if (rdev && release)
1766 ib_device_put(&rdev->ibdev);
1770 static struct notifier_block bnxt_re_netdev_notifier = {
1771 .notifier_call = bnxt_re_netdev_event
1774 static int __init bnxt_re_mod_init(void)
1778 pr_info("%s: %s", ROCE_DRV_MODULE_NAME, version);
1780 bnxt_re_wq = create_singlethread_workqueue("bnxt_re");
1784 INIT_LIST_HEAD(&bnxt_re_dev_list);
1786 rc = register_netdevice_notifier(&bnxt_re_netdev_notifier);
1788 pr_err("%s: Cannot register to netdevice_notifier",
1789 ROCE_DRV_MODULE_NAME);
1795 destroy_workqueue(bnxt_re_wq);
1800 static void __exit bnxt_re_mod_exit(void)
1802 struct bnxt_re_dev *rdev;
1804 unregister_netdevice_notifier(&bnxt_re_netdev_notifier);
1806 destroy_workqueue(bnxt_re_wq);
1807 list_for_each_entry(rdev, &bnxt_re_dev_list, list) {
1808 /* VF device removal should be called before the removal
1809 * of PF device. Queue VFs unregister first, so that VFs
1810 * shall be removed before the PF during the call of
1811 * ib_unregister_driver.
1813 if (rdev->is_virtfn)
1814 ib_unregister_device(&rdev->ibdev);
1816 ib_unregister_driver(RDMA_DRIVER_BNXT_RE);
1819 module_init(bnxt_re_mod_init);
1820 module_exit(bnxt_re_mod_exit);