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 static char version[] =
68 BNXT_RE_DESC " v" ROCE_DRV_MODULE_VERSION "\n";
70 MODULE_AUTHOR("Eddie Wai <eddie.wai@broadcom.com>");
71 MODULE_DESCRIPTION(BNXT_RE_DESC " Driver");
72 MODULE_LICENSE("Dual BSD/GPL");
73 MODULE_VERSION(ROCE_DRV_MODULE_VERSION);
76 static struct list_head bnxt_re_dev_list = LIST_HEAD_INIT(bnxt_re_dev_list);
77 /* Mutex to protect the list of bnxt_re devices added */
78 static DEFINE_MUTEX(bnxt_re_dev_lock);
79 static struct workqueue_struct *bnxt_re_wq;
81 /* for handling bnxt_en callbacks later */
82 static void bnxt_re_stop(void *p)
86 static void bnxt_re_start(void *p)
90 static void bnxt_re_sriov_config(void *p, int num_vfs)
94 static struct bnxt_ulp_ops bnxt_re_ulp_ops = {
95 .ulp_async_notifier = NULL,
96 .ulp_stop = bnxt_re_stop,
97 .ulp_start = bnxt_re_start,
98 .ulp_sriov_config = bnxt_re_sriov_config
101 /* RoCE -> Net driver */
103 /* Driver registration routines used to let the networking driver (bnxt_en)
104 * to know that the RoCE driver is now installed
106 static int bnxt_re_unregister_netdev(struct bnxt_re_dev *rdev, bool lock_wait)
108 struct bnxt_en_dev *en_dev;
114 en_dev = rdev->en_dev;
115 /* Acquire rtnl lock if it is not invokded from netdev event */
119 rc = en_dev->en_ops->bnxt_unregister_device(rdev->en_dev,
126 static int bnxt_re_register_netdev(struct bnxt_re_dev *rdev)
128 struct bnxt_en_dev *en_dev;
134 en_dev = rdev->en_dev;
137 rc = en_dev->en_ops->bnxt_register_device(en_dev, BNXT_ROCE_ULP,
138 &bnxt_re_ulp_ops, rdev);
143 static int bnxt_re_free_msix(struct bnxt_re_dev *rdev, bool lock_wait)
145 struct bnxt_en_dev *en_dev;
151 en_dev = rdev->en_dev;
156 rc = en_dev->en_ops->bnxt_free_msix(rdev->en_dev, BNXT_ROCE_ULP);
163 static int bnxt_re_request_msix(struct bnxt_re_dev *rdev)
165 int rc = 0, num_msix_want = BNXT_RE_MIN_MSIX, num_msix_got;
166 struct bnxt_en_dev *en_dev;
171 en_dev = rdev->en_dev;
174 num_msix_got = en_dev->en_ops->bnxt_request_msix(en_dev, BNXT_ROCE_ULP,
177 if (num_msix_got < BNXT_RE_MIN_MSIX) {
181 if (num_msix_got != num_msix_want) {
182 dev_warn(rdev_to_dev(rdev),
183 "Requested %d MSI-X vectors, got %d\n",
184 num_msix_want, num_msix_got);
186 rdev->num_msix = num_msix_got;
192 static void bnxt_re_init_hwrm_hdr(struct bnxt_re_dev *rdev, struct input *hdr,
193 u16 opcd, u16 crid, u16 trid)
195 hdr->req_type = cpu_to_le16(opcd);
196 hdr->cmpl_ring = cpu_to_le16(crid);
197 hdr->target_id = cpu_to_le16(trid);
200 static void bnxt_re_fill_fw_msg(struct bnxt_fw_msg *fw_msg, void *msg,
201 int msg_len, void *resp, int resp_max_len,
205 fw_msg->msg_len = msg_len;
207 fw_msg->resp_max_len = resp_max_len;
208 fw_msg->timeout = timeout;
211 static int bnxt_re_net_ring_free(struct bnxt_re_dev *rdev, u16 fw_ring_id,
214 struct bnxt_en_dev *en_dev = rdev->en_dev;
215 struct hwrm_ring_free_input req = {0};
216 struct hwrm_ring_free_output resp;
217 struct bnxt_fw_msg fw_msg;
218 bool do_unlock = false;
224 memset(&fw_msg, 0, sizeof(fw_msg));
230 bnxt_re_init_hwrm_hdr(rdev, (void *)&req, HWRM_RING_FREE, -1, -1);
231 req.ring_type = RING_ALLOC_REQ_RING_TYPE_L2_CMPL;
232 req.ring_id = cpu_to_le16(fw_ring_id);
233 bnxt_re_fill_fw_msg(&fw_msg, (void *)&req, sizeof(req), (void *)&resp,
234 sizeof(resp), DFLT_HWRM_CMD_TIMEOUT);
235 rc = en_dev->en_ops->bnxt_send_fw_msg(en_dev, BNXT_ROCE_ULP, &fw_msg);
237 dev_err(rdev_to_dev(rdev),
238 "Failed to free HW ring:%d :%#x", req.ring_id, rc);
244 static int bnxt_re_net_ring_alloc(struct bnxt_re_dev *rdev, dma_addr_t *dma_arr,
245 int pages, int type, u32 ring_mask,
246 u32 map_index, u16 *fw_ring_id)
248 struct bnxt_en_dev *en_dev = rdev->en_dev;
249 struct hwrm_ring_alloc_input req = {0};
250 struct hwrm_ring_alloc_output resp;
251 struct bnxt_fw_msg fw_msg;
257 memset(&fw_msg, 0, sizeof(fw_msg));
259 bnxt_re_init_hwrm_hdr(rdev, (void *)&req, HWRM_RING_ALLOC, -1, -1);
261 req.page_tbl_addr = cpu_to_le64(dma_arr[0]);
263 /* Page size is in log2 units */
264 req.page_size = BNXT_PAGE_SHIFT;
265 req.page_tbl_depth = 1;
268 /* Association of ring index with doorbell index and MSIX number */
269 req.logical_id = cpu_to_le16(map_index);
270 req.length = cpu_to_le32(ring_mask + 1);
271 req.ring_type = RING_ALLOC_REQ_RING_TYPE_L2_CMPL;
272 req.int_mode = RING_ALLOC_REQ_INT_MODE_MSIX;
273 bnxt_re_fill_fw_msg(&fw_msg, (void *)&req, sizeof(req), (void *)&resp,
274 sizeof(resp), DFLT_HWRM_CMD_TIMEOUT);
275 rc = en_dev->en_ops->bnxt_send_fw_msg(en_dev, BNXT_ROCE_ULP, &fw_msg);
277 *fw_ring_id = le16_to_cpu(resp.ring_id);
283 static int bnxt_re_net_stats_ctx_free(struct bnxt_re_dev *rdev,
284 u32 fw_stats_ctx_id, bool lock_wait)
286 struct bnxt_en_dev *en_dev = rdev->en_dev;
287 struct hwrm_stat_ctx_free_input req = {0};
288 struct bnxt_fw_msg fw_msg;
289 bool do_unlock = false;
295 memset(&fw_msg, 0, sizeof(fw_msg));
301 bnxt_re_init_hwrm_hdr(rdev, (void *)&req, HWRM_STAT_CTX_FREE, -1, -1);
302 req.stat_ctx_id = cpu_to_le32(fw_stats_ctx_id);
303 bnxt_re_fill_fw_msg(&fw_msg, (void *)&req, sizeof(req), (void *)&req,
304 sizeof(req), DFLT_HWRM_CMD_TIMEOUT);
305 rc = en_dev->en_ops->bnxt_send_fw_msg(en_dev, BNXT_ROCE_ULP, &fw_msg);
307 dev_err(rdev_to_dev(rdev),
308 "Failed to free HW stats context %#x", rc);
315 static int bnxt_re_net_stats_ctx_alloc(struct bnxt_re_dev *rdev,
317 u32 *fw_stats_ctx_id)
319 struct hwrm_stat_ctx_alloc_output resp = {0};
320 struct hwrm_stat_ctx_alloc_input req = {0};
321 struct bnxt_en_dev *en_dev = rdev->en_dev;
322 struct bnxt_fw_msg fw_msg;
325 *fw_stats_ctx_id = INVALID_STATS_CTX_ID;
330 memset(&fw_msg, 0, sizeof(fw_msg));
333 bnxt_re_init_hwrm_hdr(rdev, (void *)&req, HWRM_STAT_CTX_ALLOC, -1, -1);
334 req.update_period_ms = cpu_to_le32(1000);
335 req.stats_dma_addr = cpu_to_le64(dma_map);
336 bnxt_re_fill_fw_msg(&fw_msg, (void *)&req, sizeof(req), (void *)&resp,
337 sizeof(resp), DFLT_HWRM_CMD_TIMEOUT);
338 rc = en_dev->en_ops->bnxt_send_fw_msg(en_dev, BNXT_ROCE_ULP, &fw_msg);
340 *fw_stats_ctx_id = le32_to_cpu(resp.stat_ctx_id);
348 static bool is_bnxt_re_dev(struct net_device *netdev)
350 struct ethtool_drvinfo drvinfo;
352 if (netdev->ethtool_ops && netdev->ethtool_ops->get_drvinfo) {
353 memset(&drvinfo, 0, sizeof(drvinfo));
354 netdev->ethtool_ops->get_drvinfo(netdev, &drvinfo);
356 if (strcmp(drvinfo.driver, "bnxt_en"))
363 static struct bnxt_re_dev *bnxt_re_from_netdev(struct net_device *netdev)
365 struct bnxt_re_dev *rdev;
368 list_for_each_entry_rcu(rdev, &bnxt_re_dev_list, list) {
369 if (rdev->netdev == netdev) {
378 static void bnxt_re_dev_unprobe(struct net_device *netdev,
379 struct bnxt_en_dev *en_dev)
382 module_put(en_dev->pdev->driver->driver.owner);
385 static struct bnxt_en_dev *bnxt_re_dev_probe(struct net_device *netdev)
387 struct bnxt *bp = netdev_priv(netdev);
388 struct bnxt_en_dev *en_dev;
389 struct pci_dev *pdev;
391 /* Call bnxt_en's RoCE probe via indirect API */
393 return ERR_PTR(-EINVAL);
395 en_dev = bp->ulp_probe(netdev);
401 return ERR_PTR(-EINVAL);
403 if (!(en_dev->flags & BNXT_EN_FLAG_ROCE_CAP)) {
405 "%s: probe error: RoCE is not supported on this device",
406 ROCE_DRV_MODULE_NAME);
407 return ERR_PTR(-ENODEV);
410 /* Bump net device reference count */
411 if (!try_module_get(pdev->driver->driver.owner))
412 return ERR_PTR(-ENODEV);
419 static void bnxt_re_unregister_ib(struct bnxt_re_dev *rdev)
421 ib_unregister_device(&rdev->ibdev);
424 static int bnxt_re_register_ib(struct bnxt_re_dev *rdev)
426 struct ib_device *ibdev = &rdev->ibdev;
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;
436 bnxt_qplib_get_guid(rdev->netdev->dev_addr, (u8 *)&ibdev->node_guid);
438 ibdev->num_comp_vectors = 1;
439 ibdev->dev.parent = &rdev->en_dev->pdev->dev;
440 ibdev->local_dma_lkey = BNXT_QPLIB_RSVD_LKEY;
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 */
472 ibdev->query_device = bnxt_re_query_device;
473 ibdev->modify_device = bnxt_re_modify_device;
475 ibdev->query_port = bnxt_re_query_port;
476 ibdev->modify_port = bnxt_re_modify_port;
477 ibdev->get_port_immutable = bnxt_re_get_port_immutable;
478 ibdev->query_pkey = bnxt_re_query_pkey;
479 ibdev->query_gid = bnxt_re_query_gid;
480 ibdev->get_netdev = bnxt_re_get_netdev;
481 ibdev->add_gid = bnxt_re_add_gid;
482 ibdev->del_gid = bnxt_re_del_gid;
483 ibdev->get_link_layer = bnxt_re_get_link_layer;
485 ibdev->alloc_pd = bnxt_re_alloc_pd;
486 ibdev->dealloc_pd = bnxt_re_dealloc_pd;
488 ibdev->create_ah = bnxt_re_create_ah;
489 ibdev->modify_ah = bnxt_re_modify_ah;
490 ibdev->query_ah = bnxt_re_query_ah;
491 ibdev->destroy_ah = bnxt_re_destroy_ah;
493 ibdev->create_qp = bnxt_re_create_qp;
494 ibdev->modify_qp = bnxt_re_modify_qp;
495 ibdev->query_qp = bnxt_re_query_qp;
496 ibdev->destroy_qp = bnxt_re_destroy_qp;
498 ibdev->post_send = bnxt_re_post_send;
499 ibdev->post_recv = bnxt_re_post_recv;
501 ibdev->create_cq = bnxt_re_create_cq;
502 ibdev->destroy_cq = bnxt_re_destroy_cq;
503 ibdev->poll_cq = bnxt_re_poll_cq;
504 ibdev->req_notify_cq = bnxt_re_req_notify_cq;
506 ibdev->get_dma_mr = bnxt_re_get_dma_mr;
507 ibdev->dereg_mr = bnxt_re_dereg_mr;
508 ibdev->alloc_mr = bnxt_re_alloc_mr;
509 ibdev->map_mr_sg = bnxt_re_map_mr_sg;
511 ibdev->reg_user_mr = bnxt_re_reg_user_mr;
512 ibdev->alloc_ucontext = bnxt_re_alloc_ucontext;
513 ibdev->dealloc_ucontext = bnxt_re_dealloc_ucontext;
514 ibdev->mmap = bnxt_re_mmap;
516 return ib_register_device(ibdev, NULL);
519 static ssize_t show_rev(struct device *device, struct device_attribute *attr,
522 struct bnxt_re_dev *rdev = to_bnxt_re_dev(device, ibdev.dev);
524 return scnprintf(buf, PAGE_SIZE, "0x%x\n", rdev->en_dev->pdev->vendor);
527 static ssize_t show_fw_ver(struct device *device, struct device_attribute *attr,
530 struct bnxt_re_dev *rdev = to_bnxt_re_dev(device, ibdev.dev);
532 return scnprintf(buf, PAGE_SIZE, "%s\n", rdev->dev_attr.fw_ver);
535 static ssize_t show_hca(struct device *device, struct device_attribute *attr,
538 struct bnxt_re_dev *rdev = to_bnxt_re_dev(device, ibdev.dev);
540 return scnprintf(buf, PAGE_SIZE, "%s\n", rdev->ibdev.node_desc);
543 static DEVICE_ATTR(hw_rev, 0444, show_rev, NULL);
544 static DEVICE_ATTR(fw_rev, 0444, show_fw_ver, NULL);
545 static DEVICE_ATTR(hca_type, 0444, show_hca, NULL);
547 static struct device_attribute *bnxt_re_attributes[] = {
553 static void bnxt_re_dev_remove(struct bnxt_re_dev *rdev)
555 dev_put(rdev->netdev);
558 mutex_lock(&bnxt_re_dev_lock);
559 list_del_rcu(&rdev->list);
560 mutex_unlock(&bnxt_re_dev_lock);
563 flush_workqueue(bnxt_re_wq);
565 ib_dealloc_device(&rdev->ibdev);
569 static struct bnxt_re_dev *bnxt_re_dev_add(struct net_device *netdev,
570 struct bnxt_en_dev *en_dev)
572 struct bnxt_re_dev *rdev;
574 /* Allocate bnxt_re_dev instance here */
575 rdev = (struct bnxt_re_dev *)ib_alloc_device(sizeof(*rdev));
577 dev_err(NULL, "%s: bnxt_re_dev allocation failure!",
578 ROCE_DRV_MODULE_NAME);
582 rdev->netdev = netdev;
583 dev_hold(rdev->netdev);
584 rdev->en_dev = en_dev;
585 rdev->id = rdev->en_dev->pdev->devfn;
586 INIT_LIST_HEAD(&rdev->qp_list);
587 mutex_init(&rdev->qp_lock);
588 atomic_set(&rdev->qp_count, 0);
589 atomic_set(&rdev->cq_count, 0);
590 atomic_set(&rdev->srq_count, 0);
591 atomic_set(&rdev->mr_count, 0);
592 atomic_set(&rdev->mw_count, 0);
593 rdev->cosq[0] = 0xFFFF;
594 rdev->cosq[1] = 0xFFFF;
596 mutex_lock(&bnxt_re_dev_lock);
597 list_add_tail_rcu(&rdev->list, &bnxt_re_dev_list);
598 mutex_unlock(&bnxt_re_dev_lock);
602 static int bnxt_re_aeq_handler(struct bnxt_qplib_rcfw *rcfw,
603 struct creq_func_event *aeqe)
605 switch (aeqe->event) {
606 case CREQ_FUNC_EVENT_EVENT_TX_WQE_ERROR:
608 case CREQ_FUNC_EVENT_EVENT_TX_DATA_ERROR:
610 case CREQ_FUNC_EVENT_EVENT_RX_WQE_ERROR:
612 case CREQ_FUNC_EVENT_EVENT_RX_DATA_ERROR:
614 case CREQ_FUNC_EVENT_EVENT_CQ_ERROR:
616 case CREQ_FUNC_EVENT_EVENT_TQM_ERROR:
618 case CREQ_FUNC_EVENT_EVENT_CFCQ_ERROR:
620 case CREQ_FUNC_EVENT_EVENT_CFCS_ERROR:
622 case CREQ_FUNC_EVENT_EVENT_CFCC_ERROR:
624 case CREQ_FUNC_EVENT_EVENT_CFCM_ERROR:
626 case CREQ_FUNC_EVENT_EVENT_TIM_ERROR:
634 static int bnxt_re_cqn_handler(struct bnxt_qplib_nq *nq,
635 struct bnxt_qplib_cq *handle)
637 struct bnxt_re_cq *cq = container_of(handle, struct bnxt_re_cq,
641 dev_err(NULL, "%s: CQ is NULL, CQN not handled",
642 ROCE_DRV_MODULE_NAME);
645 if (cq->ib_cq.comp_handler) {
646 /* Lock comp_handler? */
647 (*cq->ib_cq.comp_handler)(&cq->ib_cq, cq->ib_cq.cq_context);
653 static void bnxt_re_cleanup_res(struct bnxt_re_dev *rdev)
655 if (rdev->nq.hwq.max_elements)
656 bnxt_qplib_disable_nq(&rdev->nq);
658 if (rdev->qplib_res.rcfw)
659 bnxt_qplib_cleanup_res(&rdev->qplib_res);
662 static int bnxt_re_init_res(struct bnxt_re_dev *rdev)
666 bnxt_qplib_init_res(&rdev->qplib_res);
668 if (rdev->msix_entries[BNXT_RE_NQ_IDX].vector <= 0)
671 rc = bnxt_qplib_enable_nq(rdev->en_dev->pdev, &rdev->nq,
672 rdev->msix_entries[BNXT_RE_NQ_IDX].vector,
673 rdev->msix_entries[BNXT_RE_NQ_IDX].db_offset,
674 &bnxt_re_cqn_handler,
678 dev_err(rdev_to_dev(rdev), "Failed to enable NQ: %#x", rc);
683 static void bnxt_re_free_res(struct bnxt_re_dev *rdev, bool lock_wait)
685 if (rdev->nq.hwq.max_elements) {
686 bnxt_re_net_ring_free(rdev, rdev->nq.ring_id, lock_wait);
687 bnxt_qplib_free_nq(&rdev->nq);
689 if (rdev->qplib_res.dpi_tbl.max) {
690 bnxt_qplib_dealloc_dpi(&rdev->qplib_res,
691 &rdev->qplib_res.dpi_tbl,
692 &rdev->dpi_privileged);
694 if (rdev->qplib_res.rcfw) {
695 bnxt_qplib_free_res(&rdev->qplib_res);
696 rdev->qplib_res.rcfw = NULL;
700 static int bnxt_re_alloc_res(struct bnxt_re_dev *rdev)
704 /* Configure and allocate resources for qplib */
705 rdev->qplib_res.rcfw = &rdev->rcfw;
706 rc = bnxt_qplib_get_dev_attr(&rdev->rcfw, &rdev->dev_attr);
710 rc = bnxt_qplib_alloc_res(&rdev->qplib_res, rdev->en_dev->pdev,
711 rdev->netdev, &rdev->dev_attr);
715 rc = bnxt_qplib_alloc_dpi(&rdev->qplib_res.dpi_tbl,
716 &rdev->dpi_privileged,
721 rdev->nq.hwq.max_elements = BNXT_RE_MAX_CQ_COUNT +
722 BNXT_RE_MAX_SRQC_COUNT + 2;
723 rc = bnxt_qplib_alloc_nq(rdev->en_dev->pdev, &rdev->nq);
725 dev_err(rdev_to_dev(rdev),
726 "Failed to allocate NQ memory: %#x", rc);
729 rc = bnxt_re_net_ring_alloc
730 (rdev, rdev->nq.hwq.pbl[PBL_LVL_0].pg_map_arr,
731 rdev->nq.hwq.pbl[rdev->nq.hwq.level].pg_count,
732 HWRM_RING_ALLOC_CMPL, BNXT_QPLIB_NQE_MAX_CNT - 1,
733 rdev->msix_entries[BNXT_RE_NQ_IDX].ring_idx,
736 dev_err(rdev_to_dev(rdev),
737 "Failed to allocate NQ ring: %#x", rc);
742 bnxt_qplib_free_nq(&rdev->nq);
744 rdev->qplib_res.rcfw = NULL;
748 static void bnxt_re_dispatch_event(struct ib_device *ibdev, struct ib_qp *qp,
749 u8 port_num, enum ib_event_type event)
751 struct ib_event ib_event;
753 ib_event.device = ibdev;
755 ib_event.element.qp = qp;
757 ib_event.element.port_num = port_num;
758 ib_event.event = event;
759 ib_dispatch_event(&ib_event);
762 #define HWRM_QUEUE_PRI2COS_QCFG_INPUT_FLAGS_IVLAN 0x02
763 static int bnxt_re_query_hwrm_pri2cos(struct bnxt_re_dev *rdev, u8 dir,
766 struct hwrm_queue_pri2cos_qcfg_input req = {0};
767 struct bnxt *bp = netdev_priv(rdev->netdev);
768 struct hwrm_queue_pri2cos_qcfg_output resp;
769 struct bnxt_en_dev *en_dev = rdev->en_dev;
770 struct bnxt_fw_msg fw_msg;
772 u8 *qcfgmap, *tmp_map;
778 memset(&fw_msg, 0, sizeof(fw_msg));
779 bnxt_re_init_hwrm_hdr(rdev, (void *)&req,
780 HWRM_QUEUE_PRI2COS_QCFG, -1, -1);
781 flags |= (dir & 0x01);
782 flags |= HWRM_QUEUE_PRI2COS_QCFG_INPUT_FLAGS_IVLAN;
783 req.flags = cpu_to_le32(flags);
784 req.port_id = bp->pf.port_id;
786 bnxt_re_fill_fw_msg(&fw_msg, (void *)&req, sizeof(req), (void *)&resp,
787 sizeof(resp), DFLT_HWRM_CMD_TIMEOUT);
788 rc = en_dev->en_ops->bnxt_send_fw_msg(en_dev, BNXT_ROCE_ULP, &fw_msg);
792 if (resp.queue_cfg_info) {
793 dev_warn(rdev_to_dev(rdev),
794 "Asymmetric cos queue configuration detected");
795 dev_warn(rdev_to_dev(rdev),
796 " on device, QoS may not be fully functional\n");
798 qcfgmap = &resp.pri0_cos_queue_id;
799 tmp_map = (u8 *)cid_map;
800 for (i = 0; i < IEEE_8021QAZ_MAX_TCS; i++)
801 tmp_map[i] = qcfgmap[i];
806 static bool bnxt_re_is_qp1_or_shadow_qp(struct bnxt_re_dev *rdev,
807 struct bnxt_re_qp *qp)
809 return (qp->ib_qp.qp_type == IB_QPT_GSI) || (qp == rdev->qp1_sqp);
812 static void bnxt_re_dev_stop(struct bnxt_re_dev *rdev)
814 int mask = IB_QP_STATE;
815 struct ib_qp_attr qp_attr;
816 struct bnxt_re_qp *qp;
818 qp_attr.qp_state = IB_QPS_ERR;
819 mutex_lock(&rdev->qp_lock);
820 list_for_each_entry(qp, &rdev->qp_list, list) {
821 /* Modify the state of all QPs except QP1/Shadow QP */
822 if (!bnxt_re_is_qp1_or_shadow_qp(rdev, qp)) {
823 if (qp->qplib_qp.state !=
824 CMDQ_MODIFY_QP_NEW_STATE_RESET &&
825 qp->qplib_qp.state !=
826 CMDQ_MODIFY_QP_NEW_STATE_ERR) {
827 bnxt_re_dispatch_event(&rdev->ibdev, &qp->ib_qp,
828 1, IB_EVENT_QP_FATAL);
829 bnxt_re_modify_qp(&qp->ib_qp, &qp_attr, mask,
834 mutex_unlock(&rdev->qp_lock);
837 static u32 bnxt_re_get_priority_mask(struct bnxt_re_dev *rdev)
839 u32 prio_map = 0, tmp_map = 0;
840 struct net_device *netdev;
843 netdev = rdev->netdev;
845 memset(&app, 0, sizeof(app));
846 app.selector = IEEE_8021QAZ_APP_SEL_ETHERTYPE;
847 app.protocol = ETH_P_IBOE;
848 tmp_map = dcb_ieee_getapp_mask(netdev, &app);
851 app.selector = IEEE_8021QAZ_APP_SEL_DGRAM;
852 app.protocol = ROCE_V2_UDP_DPORT;
853 tmp_map = dcb_ieee_getapp_mask(netdev, &app);
861 static void bnxt_re_parse_cid_map(u8 prio_map, u8 *cid_map, u16 *cosq)
866 for (prio = 0, id = 0; prio < 8; prio++) {
867 if (prio_map & (1 << prio)) {
868 cosq[id] = cid_map[prio];
870 if (id == 2) /* Max 2 tcs supported */
876 static int bnxt_re_setup_qos(struct bnxt_re_dev *rdev)
882 /* Get priority for roce */
883 rc = bnxt_re_get_priority_mask(rdev);
888 if (prio_map == rdev->cur_prio_map)
890 rdev->cur_prio_map = prio_map;
891 /* Get cosq id for this priority */
892 rc = bnxt_re_query_hwrm_pri2cos(rdev, 0, &cid_map);
894 dev_warn(rdev_to_dev(rdev), "no cos for p_mask %x\n", prio_map);
897 /* Parse CoS IDs for app priority */
898 bnxt_re_parse_cid_map(prio_map, (u8 *)&cid_map, rdev->cosq);
901 rc = bnxt_qplib_map_tc2cos(&rdev->qplib_res, rdev->cosq);
903 dev_warn(rdev_to_dev(rdev), "no tc for cos{%x, %x}\n",
904 rdev->cosq[0], rdev->cosq[1]);
911 static void bnxt_re_ib_unreg(struct bnxt_re_dev *rdev, bool lock_wait)
915 if (test_and_clear_bit(BNXT_RE_FLAG_IBDEV_REGISTERED, &rdev->flags)) {
916 for (i = 0; i < ARRAY_SIZE(bnxt_re_attributes); i++)
917 device_remove_file(&rdev->ibdev.dev,
918 bnxt_re_attributes[i]);
920 bnxt_re_unregister_ib(rdev);
922 if (test_and_clear_bit(BNXT_RE_FLAG_QOS_WORK_REG, &rdev->flags))
923 cancel_delayed_work(&rdev->worker);
925 bnxt_re_cleanup_res(rdev);
926 bnxt_re_free_res(rdev, lock_wait);
928 if (test_and_clear_bit(BNXT_RE_FLAG_RCFW_CHANNEL_EN, &rdev->flags)) {
929 rc = bnxt_qplib_deinit_rcfw(&rdev->rcfw);
931 dev_warn(rdev_to_dev(rdev),
932 "Failed to deinitialize RCFW: %#x", rc);
933 bnxt_re_net_stats_ctx_free(rdev, rdev->qplib_ctx.stats.fw_id,
935 bnxt_qplib_free_ctx(rdev->en_dev->pdev, &rdev->qplib_ctx);
936 bnxt_qplib_disable_rcfw_channel(&rdev->rcfw);
937 bnxt_re_net_ring_free(rdev, rdev->rcfw.creq_ring_id, lock_wait);
938 bnxt_qplib_free_rcfw_channel(&rdev->rcfw);
940 if (test_and_clear_bit(BNXT_RE_FLAG_GOT_MSIX, &rdev->flags)) {
941 rc = bnxt_re_free_msix(rdev, lock_wait);
943 dev_warn(rdev_to_dev(rdev),
944 "Failed to free MSI-X vectors: %#x", rc);
946 if (test_and_clear_bit(BNXT_RE_FLAG_NETDEV_REGISTERED, &rdev->flags)) {
947 rc = bnxt_re_unregister_netdev(rdev, lock_wait);
949 dev_warn(rdev_to_dev(rdev),
950 "Failed to unregister with netdev: %#x", rc);
954 static void bnxt_re_set_resource_limits(struct bnxt_re_dev *rdev)
958 rdev->qplib_ctx.qpc_count = BNXT_RE_MAX_QPC_COUNT;
959 rdev->qplib_ctx.mrw_count = BNXT_RE_MAX_MRW_COUNT;
960 rdev->qplib_ctx.srqc_count = BNXT_RE_MAX_SRQC_COUNT;
961 rdev->qplib_ctx.cq_count = BNXT_RE_MAX_CQ_COUNT;
962 for (i = 0; i < MAX_TQM_ALLOC_REQ; i++)
963 rdev->qplib_ctx.tqm_count[i] =
964 rdev->dev_attr.tqm_alloc_reqs[i];
967 /* worker thread for polling periodic events. Now used for QoS programming*/
968 static void bnxt_re_worker(struct work_struct *work)
970 struct bnxt_re_dev *rdev = container_of(work, struct bnxt_re_dev,
973 bnxt_re_setup_qos(rdev);
974 schedule_delayed_work(&rdev->worker, msecs_to_jiffies(30000));
977 static int bnxt_re_ib_reg(struct bnxt_re_dev *rdev)
981 /* Registered a new RoCE device instance to netdev */
982 rc = bnxt_re_register_netdev(rdev);
984 pr_err("Failed to register with netedev: %#x\n", rc);
987 set_bit(BNXT_RE_FLAG_NETDEV_REGISTERED, &rdev->flags);
989 rc = bnxt_re_request_msix(rdev);
991 pr_err("Failed to get MSI-X vectors: %#x\n", rc);
995 set_bit(BNXT_RE_FLAG_GOT_MSIX, &rdev->flags);
997 /* Establish RCFW Communication Channel to initialize the context
998 * memory for the function and all child VFs
1000 rc = bnxt_qplib_alloc_rcfw_channel(rdev->en_dev->pdev, &rdev->rcfw);
1004 rc = bnxt_re_net_ring_alloc
1005 (rdev, rdev->rcfw.creq.pbl[PBL_LVL_0].pg_map_arr,
1006 rdev->rcfw.creq.pbl[rdev->rcfw.creq.level].pg_count,
1007 HWRM_RING_ALLOC_CMPL, BNXT_QPLIB_CREQE_MAX_CNT - 1,
1008 rdev->msix_entries[BNXT_RE_AEQ_IDX].ring_idx,
1009 &rdev->rcfw.creq_ring_id);
1011 pr_err("Failed to allocate CREQ: %#x\n", rc);
1014 rc = bnxt_qplib_enable_rcfw_channel
1015 (rdev->en_dev->pdev, &rdev->rcfw,
1016 rdev->msix_entries[BNXT_RE_AEQ_IDX].vector,
1017 rdev->msix_entries[BNXT_RE_AEQ_IDX].db_offset,
1018 0, &bnxt_re_aeq_handler);
1020 pr_err("Failed to enable RCFW channel: %#x\n", rc);
1024 rc = bnxt_qplib_get_dev_attr(&rdev->rcfw, &rdev->dev_attr);
1027 bnxt_re_set_resource_limits(rdev);
1029 rc = bnxt_qplib_alloc_ctx(rdev->en_dev->pdev, &rdev->qplib_ctx, 0);
1031 pr_err("Failed to allocate QPLIB context: %#x\n", rc);
1034 rc = bnxt_re_net_stats_ctx_alloc(rdev,
1035 rdev->qplib_ctx.stats.dma_map,
1036 &rdev->qplib_ctx.stats.fw_id);
1038 pr_err("Failed to allocate stats context: %#x\n", rc);
1042 rc = bnxt_qplib_init_rcfw(&rdev->rcfw, &rdev->qplib_ctx, 0);
1044 pr_err("Failed to initialize RCFW: %#x\n", rc);
1047 set_bit(BNXT_RE_FLAG_RCFW_CHANNEL_EN, &rdev->flags);
1049 /* Resources based on the 'new' device caps */
1050 rc = bnxt_re_alloc_res(rdev);
1052 pr_err("Failed to allocate resources: %#x\n", rc);
1055 rc = bnxt_re_init_res(rdev);
1057 pr_err("Failed to initialize resources: %#x\n", rc);
1061 rc = bnxt_re_setup_qos(rdev);
1063 pr_info("RoCE priority not yet configured\n");
1065 INIT_DELAYED_WORK(&rdev->worker, bnxt_re_worker);
1066 set_bit(BNXT_RE_FLAG_QOS_WORK_REG, &rdev->flags);
1067 schedule_delayed_work(&rdev->worker, msecs_to_jiffies(30000));
1069 /* Register ib dev */
1070 rc = bnxt_re_register_ib(rdev);
1072 pr_err("Failed to register with IB: %#x\n", rc);
1075 dev_info(rdev_to_dev(rdev), "Device registered successfully");
1076 for (i = 0; i < ARRAY_SIZE(bnxt_re_attributes); i++) {
1077 rc = device_create_file(&rdev->ibdev.dev,
1078 bnxt_re_attributes[i]);
1080 dev_err(rdev_to_dev(rdev),
1081 "Failed to create IB sysfs: %#x", rc);
1082 /* Must clean up all created device files */
1083 for (j = 0; j < i; j++)
1084 device_remove_file(&rdev->ibdev.dev,
1085 bnxt_re_attributes[j]);
1086 bnxt_re_unregister_ib(rdev);
1090 set_bit(BNXT_RE_FLAG_IBDEV_REGISTERED, &rdev->flags);
1091 bnxt_re_dispatch_event(&rdev->ibdev, NULL, 1, IB_EVENT_PORT_ACTIVE);
1092 bnxt_re_dispatch_event(&rdev->ibdev, NULL, 1, IB_EVENT_GID_CHANGE);
1096 bnxt_re_net_stats_ctx_free(rdev, rdev->qplib_ctx.stats.fw_id, true);
1098 bnxt_qplib_free_ctx(rdev->en_dev->pdev, &rdev->qplib_ctx);
1100 bnxt_qplib_disable_rcfw_channel(&rdev->rcfw);
1102 bnxt_re_net_ring_free(rdev, rdev->rcfw.creq_ring_id, true);
1104 bnxt_qplib_free_rcfw_channel(&rdev->rcfw);
1106 bnxt_re_ib_unreg(rdev, true);
1110 static void bnxt_re_dev_unreg(struct bnxt_re_dev *rdev)
1112 struct bnxt_en_dev *en_dev = rdev->en_dev;
1113 struct net_device *netdev = rdev->netdev;
1115 bnxt_re_dev_remove(rdev);
1118 bnxt_re_dev_unprobe(netdev, en_dev);
1121 static int bnxt_re_dev_reg(struct bnxt_re_dev **rdev, struct net_device *netdev)
1123 struct bnxt_en_dev *en_dev;
1126 if (!is_bnxt_re_dev(netdev))
1129 en_dev = bnxt_re_dev_probe(netdev);
1130 if (IS_ERR(en_dev)) {
1131 if (en_dev != ERR_PTR(-ENODEV))
1132 pr_err("%s: Failed to probe\n", ROCE_DRV_MODULE_NAME);
1133 rc = PTR_ERR(en_dev);
1136 *rdev = bnxt_re_dev_add(netdev, en_dev);
1139 bnxt_re_dev_unprobe(netdev, en_dev);
1146 static void bnxt_re_remove_one(struct bnxt_re_dev *rdev)
1148 pci_dev_put(rdev->en_dev->pdev);
1151 /* Handle all deferred netevents tasks */
1152 static void bnxt_re_task(struct work_struct *work)
1154 struct bnxt_re_work *re_work;
1155 struct bnxt_re_dev *rdev;
1158 re_work = container_of(work, struct bnxt_re_work, work);
1159 rdev = re_work->rdev;
1161 if (re_work->event != NETDEV_REGISTER &&
1162 !test_bit(BNXT_RE_FLAG_IBDEV_REGISTERED, &rdev->flags))
1165 switch (re_work->event) {
1166 case NETDEV_REGISTER:
1167 rc = bnxt_re_ib_reg(rdev);
1169 dev_err(rdev_to_dev(rdev),
1170 "Failed to register with IB: %#x", rc);
1173 bnxt_re_dispatch_event(&rdev->ibdev, NULL, 1,
1174 IB_EVENT_PORT_ACTIVE);
1177 bnxt_re_dev_stop(rdev);
1180 if (!netif_carrier_ok(rdev->netdev))
1181 bnxt_re_dev_stop(rdev);
1182 else if (netif_carrier_ok(rdev->netdev))
1183 bnxt_re_dispatch_event(&rdev->ibdev, NULL, 1,
1184 IB_EVENT_PORT_ACTIVE);
1192 static void bnxt_re_init_one(struct bnxt_re_dev *rdev)
1194 pci_dev_get(rdev->en_dev->pdev);
1198 * "Notifier chain callback can be invoked for the same chain from
1199 * different CPUs at the same time".
1201 * For cases when the netdev is already present, our call to the
1202 * register_netdevice_notifier() will actually get the rtnl_lock()
1203 * before sending NETDEV_REGISTER and (if up) NETDEV_UP
1206 * But for cases when the netdev is not already present, the notifier
1207 * chain is subjected to be invoked from different CPUs simultaneously.
1209 * This is protected by the netdev_mutex.
1211 static int bnxt_re_netdev_event(struct notifier_block *notifier,
1212 unsigned long event, void *ptr)
1214 struct net_device *real_dev, *netdev = netdev_notifier_info_to_dev(ptr);
1215 struct bnxt_re_work *re_work;
1216 struct bnxt_re_dev *rdev;
1218 bool sch_work = false;
1220 real_dev = rdma_vlan_dev_real_dev(netdev);
1224 rdev = bnxt_re_from_netdev(real_dev);
1225 if (!rdev && event != NETDEV_REGISTER)
1227 if (real_dev != netdev)
1231 case NETDEV_REGISTER:
1234 rc = bnxt_re_dev_reg(&rdev, real_dev);
1238 pr_err("Failed to register with the device %s: %#x\n",
1239 real_dev->name, rc);
1242 bnxt_re_init_one(rdev);
1246 case NETDEV_UNREGISTER:
1247 bnxt_re_ib_unreg(rdev, false);
1248 bnxt_re_remove_one(rdev);
1249 bnxt_re_dev_unreg(rdev);
1257 /* Allocate for the deferred task */
1258 re_work = kzalloc(sizeof(*re_work), GFP_ATOMIC);
1260 re_work->rdev = rdev;
1261 re_work->event = event;
1262 re_work->vlan_dev = (real_dev == netdev ?
1264 INIT_WORK(&re_work->work, bnxt_re_task);
1265 queue_work(bnxt_re_wq, &re_work->work);
1273 static struct notifier_block bnxt_re_netdev_notifier = {
1274 .notifier_call = bnxt_re_netdev_event
1277 static int __init bnxt_re_mod_init(void)
1281 pr_info("%s: %s", ROCE_DRV_MODULE_NAME, version);
1283 bnxt_re_wq = create_singlethread_workqueue("bnxt_re");
1287 INIT_LIST_HEAD(&bnxt_re_dev_list);
1289 rc = register_netdevice_notifier(&bnxt_re_netdev_notifier);
1291 pr_err("%s: Cannot register to netdevice_notifier",
1292 ROCE_DRV_MODULE_NAME);
1298 destroy_workqueue(bnxt_re_wq);
1303 static void __exit bnxt_re_mod_exit(void)
1305 unregister_netdevice_notifier(&bnxt_re_netdev_notifier);
1307 destroy_workqueue(bnxt_re_wq);
1310 module_init(bnxt_re_mod_init);
1311 module_exit(bnxt_re_mod_exit);