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;
510 ibdev->alloc_fmr = bnxt_re_alloc_fmr;
511 ibdev->map_phys_fmr = bnxt_re_map_phys_fmr;
512 ibdev->unmap_fmr = bnxt_re_unmap_fmr;
513 ibdev->dealloc_fmr = bnxt_re_dealloc_fmr;
515 ibdev->reg_user_mr = bnxt_re_reg_user_mr;
516 ibdev->alloc_ucontext = bnxt_re_alloc_ucontext;
517 ibdev->dealloc_ucontext = bnxt_re_dealloc_ucontext;
518 ibdev->mmap = bnxt_re_mmap;
520 return ib_register_device(ibdev, NULL);
523 static ssize_t show_rev(struct device *device, struct device_attribute *attr,
526 struct bnxt_re_dev *rdev = to_bnxt_re_dev(device, ibdev.dev);
528 return scnprintf(buf, PAGE_SIZE, "0x%x\n", rdev->en_dev->pdev->vendor);
531 static ssize_t show_fw_ver(struct device *device, struct device_attribute *attr,
534 struct bnxt_re_dev *rdev = to_bnxt_re_dev(device, ibdev.dev);
536 return scnprintf(buf, PAGE_SIZE, "%s\n", rdev->dev_attr.fw_ver);
539 static ssize_t show_hca(struct device *device, struct device_attribute *attr,
542 struct bnxt_re_dev *rdev = to_bnxt_re_dev(device, ibdev.dev);
544 return scnprintf(buf, PAGE_SIZE, "%s\n", rdev->ibdev.node_desc);
547 static DEVICE_ATTR(hw_rev, 0444, show_rev, NULL);
548 static DEVICE_ATTR(fw_rev, 0444, show_fw_ver, NULL);
549 static DEVICE_ATTR(hca_type, 0444, show_hca, NULL);
551 static struct device_attribute *bnxt_re_attributes[] = {
557 static void bnxt_re_dev_remove(struct bnxt_re_dev *rdev)
559 dev_put(rdev->netdev);
562 mutex_lock(&bnxt_re_dev_lock);
563 list_del_rcu(&rdev->list);
564 mutex_unlock(&bnxt_re_dev_lock);
567 flush_workqueue(bnxt_re_wq);
569 ib_dealloc_device(&rdev->ibdev);
573 static struct bnxt_re_dev *bnxt_re_dev_add(struct net_device *netdev,
574 struct bnxt_en_dev *en_dev)
576 struct bnxt_re_dev *rdev;
578 /* Allocate bnxt_re_dev instance here */
579 rdev = (struct bnxt_re_dev *)ib_alloc_device(sizeof(*rdev));
581 dev_err(NULL, "%s: bnxt_re_dev allocation failure!",
582 ROCE_DRV_MODULE_NAME);
586 rdev->netdev = netdev;
587 dev_hold(rdev->netdev);
588 rdev->en_dev = en_dev;
589 rdev->id = rdev->en_dev->pdev->devfn;
590 INIT_LIST_HEAD(&rdev->qp_list);
591 mutex_init(&rdev->qp_lock);
592 atomic_set(&rdev->qp_count, 0);
593 atomic_set(&rdev->cq_count, 0);
594 atomic_set(&rdev->srq_count, 0);
595 atomic_set(&rdev->mr_count, 0);
596 atomic_set(&rdev->mw_count, 0);
597 rdev->cosq[0] = 0xFFFF;
598 rdev->cosq[1] = 0xFFFF;
600 mutex_lock(&bnxt_re_dev_lock);
601 list_add_tail_rcu(&rdev->list, &bnxt_re_dev_list);
602 mutex_unlock(&bnxt_re_dev_lock);
606 static int bnxt_re_aeq_handler(struct bnxt_qplib_rcfw *rcfw,
607 struct creq_func_event *aeqe)
609 switch (aeqe->event) {
610 case CREQ_FUNC_EVENT_EVENT_TX_WQE_ERROR:
612 case CREQ_FUNC_EVENT_EVENT_TX_DATA_ERROR:
614 case CREQ_FUNC_EVENT_EVENT_RX_WQE_ERROR:
616 case CREQ_FUNC_EVENT_EVENT_RX_DATA_ERROR:
618 case CREQ_FUNC_EVENT_EVENT_CQ_ERROR:
620 case CREQ_FUNC_EVENT_EVENT_TQM_ERROR:
622 case CREQ_FUNC_EVENT_EVENT_CFCQ_ERROR:
624 case CREQ_FUNC_EVENT_EVENT_CFCS_ERROR:
626 case CREQ_FUNC_EVENT_EVENT_CFCC_ERROR:
628 case CREQ_FUNC_EVENT_EVENT_CFCM_ERROR:
630 case CREQ_FUNC_EVENT_EVENT_TIM_ERROR:
638 static int bnxt_re_cqn_handler(struct bnxt_qplib_nq *nq,
639 struct bnxt_qplib_cq *handle)
641 struct bnxt_re_cq *cq = container_of(handle, struct bnxt_re_cq,
645 dev_err(NULL, "%s: CQ is NULL, CQN not handled",
646 ROCE_DRV_MODULE_NAME);
649 if (cq->ib_cq.comp_handler) {
650 /* Lock comp_handler? */
651 (*cq->ib_cq.comp_handler)(&cq->ib_cq, cq->ib_cq.cq_context);
657 static void bnxt_re_cleanup_res(struct bnxt_re_dev *rdev)
659 if (rdev->nq.hwq.max_elements)
660 bnxt_qplib_disable_nq(&rdev->nq);
662 if (rdev->qplib_res.rcfw)
663 bnxt_qplib_cleanup_res(&rdev->qplib_res);
666 static int bnxt_re_init_res(struct bnxt_re_dev *rdev)
670 bnxt_qplib_init_res(&rdev->qplib_res);
672 if (rdev->msix_entries[BNXT_RE_NQ_IDX].vector <= 0)
675 rc = bnxt_qplib_enable_nq(rdev->en_dev->pdev, &rdev->nq,
676 rdev->msix_entries[BNXT_RE_NQ_IDX].vector,
677 rdev->msix_entries[BNXT_RE_NQ_IDX].db_offset,
678 &bnxt_re_cqn_handler,
682 dev_err(rdev_to_dev(rdev), "Failed to enable NQ: %#x", rc);
687 static void bnxt_re_free_res(struct bnxt_re_dev *rdev, bool lock_wait)
689 if (rdev->nq.hwq.max_elements) {
690 bnxt_re_net_ring_free(rdev, rdev->nq.ring_id, lock_wait);
691 bnxt_qplib_free_nq(&rdev->nq);
693 if (rdev->qplib_res.dpi_tbl.max) {
694 bnxt_qplib_dealloc_dpi(&rdev->qplib_res,
695 &rdev->qplib_res.dpi_tbl,
696 &rdev->dpi_privileged);
698 if (rdev->qplib_res.rcfw) {
699 bnxt_qplib_free_res(&rdev->qplib_res);
700 rdev->qplib_res.rcfw = NULL;
704 static int bnxt_re_alloc_res(struct bnxt_re_dev *rdev)
708 /* Configure and allocate resources for qplib */
709 rdev->qplib_res.rcfw = &rdev->rcfw;
710 rc = bnxt_qplib_get_dev_attr(&rdev->rcfw, &rdev->dev_attr);
714 rc = bnxt_qplib_alloc_res(&rdev->qplib_res, rdev->en_dev->pdev,
715 rdev->netdev, &rdev->dev_attr);
719 rc = bnxt_qplib_alloc_dpi(&rdev->qplib_res.dpi_tbl,
720 &rdev->dpi_privileged,
725 rdev->nq.hwq.max_elements = BNXT_RE_MAX_CQ_COUNT +
726 BNXT_RE_MAX_SRQC_COUNT + 2;
727 rc = bnxt_qplib_alloc_nq(rdev->en_dev->pdev, &rdev->nq);
729 dev_err(rdev_to_dev(rdev),
730 "Failed to allocate NQ memory: %#x", rc);
733 rc = bnxt_re_net_ring_alloc
734 (rdev, rdev->nq.hwq.pbl[PBL_LVL_0].pg_map_arr,
735 rdev->nq.hwq.pbl[rdev->nq.hwq.level].pg_count,
736 HWRM_RING_ALLOC_CMPL, BNXT_QPLIB_NQE_MAX_CNT - 1,
737 rdev->msix_entries[BNXT_RE_NQ_IDX].ring_idx,
740 dev_err(rdev_to_dev(rdev),
741 "Failed to allocate NQ ring: %#x", rc);
746 bnxt_qplib_free_nq(&rdev->nq);
748 rdev->qplib_res.rcfw = NULL;
752 static void bnxt_re_dispatch_event(struct ib_device *ibdev, struct ib_qp *qp,
753 u8 port_num, enum ib_event_type event)
755 struct ib_event ib_event;
757 ib_event.device = ibdev;
759 ib_event.element.qp = qp;
761 ib_event.element.port_num = port_num;
762 ib_event.event = event;
763 ib_dispatch_event(&ib_event);
766 #define HWRM_QUEUE_PRI2COS_QCFG_INPUT_FLAGS_IVLAN 0x02
767 static int bnxt_re_query_hwrm_pri2cos(struct bnxt_re_dev *rdev, u8 dir,
770 struct hwrm_queue_pri2cos_qcfg_input req = {0};
771 struct bnxt *bp = netdev_priv(rdev->netdev);
772 struct hwrm_queue_pri2cos_qcfg_output resp;
773 struct bnxt_en_dev *en_dev = rdev->en_dev;
774 struct bnxt_fw_msg fw_msg;
776 u8 *qcfgmap, *tmp_map;
782 memset(&fw_msg, 0, sizeof(fw_msg));
783 bnxt_re_init_hwrm_hdr(rdev, (void *)&req,
784 HWRM_QUEUE_PRI2COS_QCFG, -1, -1);
785 flags |= (dir & 0x01);
786 flags |= HWRM_QUEUE_PRI2COS_QCFG_INPUT_FLAGS_IVLAN;
787 req.flags = cpu_to_le32(flags);
788 req.port_id = bp->pf.port_id;
790 bnxt_re_fill_fw_msg(&fw_msg, (void *)&req, sizeof(req), (void *)&resp,
791 sizeof(resp), DFLT_HWRM_CMD_TIMEOUT);
792 rc = en_dev->en_ops->bnxt_send_fw_msg(en_dev, BNXT_ROCE_ULP, &fw_msg);
796 if (resp.queue_cfg_info) {
797 dev_warn(rdev_to_dev(rdev),
798 "Asymmetric cos queue configuration detected");
799 dev_warn(rdev_to_dev(rdev),
800 " on device, QoS may not be fully functional\n");
802 qcfgmap = &resp.pri0_cos_queue_id;
803 tmp_map = (u8 *)cid_map;
804 for (i = 0; i < IEEE_8021QAZ_MAX_TCS; i++)
805 tmp_map[i] = qcfgmap[i];
810 static bool bnxt_re_is_qp1_or_shadow_qp(struct bnxt_re_dev *rdev,
811 struct bnxt_re_qp *qp)
813 return (qp->ib_qp.qp_type == IB_QPT_GSI) || (qp == rdev->qp1_sqp);
816 static void bnxt_re_dev_stop(struct bnxt_re_dev *rdev)
818 int mask = IB_QP_STATE;
819 struct ib_qp_attr qp_attr;
820 struct bnxt_re_qp *qp;
822 qp_attr.qp_state = IB_QPS_ERR;
823 mutex_lock(&rdev->qp_lock);
824 list_for_each_entry(qp, &rdev->qp_list, list) {
825 /* Modify the state of all QPs except QP1/Shadow QP */
826 if (!bnxt_re_is_qp1_or_shadow_qp(rdev, qp)) {
827 if (qp->qplib_qp.state !=
828 CMDQ_MODIFY_QP_NEW_STATE_RESET &&
829 qp->qplib_qp.state !=
830 CMDQ_MODIFY_QP_NEW_STATE_ERR) {
831 bnxt_re_dispatch_event(&rdev->ibdev, &qp->ib_qp,
832 1, IB_EVENT_QP_FATAL);
833 bnxt_re_modify_qp(&qp->ib_qp, &qp_attr, mask,
838 mutex_unlock(&rdev->qp_lock);
841 static u32 bnxt_re_get_priority_mask(struct bnxt_re_dev *rdev)
843 u32 prio_map = 0, tmp_map = 0;
844 struct net_device *netdev;
847 netdev = rdev->netdev;
849 memset(&app, 0, sizeof(app));
850 app.selector = IEEE_8021QAZ_APP_SEL_ETHERTYPE;
851 app.protocol = ETH_P_IBOE;
852 tmp_map = dcb_ieee_getapp_mask(netdev, &app);
855 app.selector = IEEE_8021QAZ_APP_SEL_DGRAM;
856 app.protocol = ROCE_V2_UDP_DPORT;
857 tmp_map = dcb_ieee_getapp_mask(netdev, &app);
865 static void bnxt_re_parse_cid_map(u8 prio_map, u8 *cid_map, u16 *cosq)
870 for (prio = 0, id = 0; prio < 8; prio++) {
871 if (prio_map & (1 << prio)) {
872 cosq[id] = cid_map[prio];
874 if (id == 2) /* Max 2 tcs supported */
880 static int bnxt_re_setup_qos(struct bnxt_re_dev *rdev)
886 /* Get priority for roce */
887 rc = bnxt_re_get_priority_mask(rdev);
892 if (prio_map == rdev->cur_prio_map)
894 rdev->cur_prio_map = prio_map;
895 /* Get cosq id for this priority */
896 rc = bnxt_re_query_hwrm_pri2cos(rdev, 0, &cid_map);
898 dev_warn(rdev_to_dev(rdev), "no cos for p_mask %x\n", prio_map);
901 /* Parse CoS IDs for app priority */
902 bnxt_re_parse_cid_map(prio_map, (u8 *)&cid_map, rdev->cosq);
905 rc = bnxt_qplib_map_tc2cos(&rdev->qplib_res, rdev->cosq);
907 dev_warn(rdev_to_dev(rdev), "no tc for cos{%x, %x}\n",
908 rdev->cosq[0], rdev->cosq[1]);
915 static void bnxt_re_ib_unreg(struct bnxt_re_dev *rdev, bool lock_wait)
919 if (test_and_clear_bit(BNXT_RE_FLAG_IBDEV_REGISTERED, &rdev->flags)) {
920 for (i = 0; i < ARRAY_SIZE(bnxt_re_attributes); i++)
921 device_remove_file(&rdev->ibdev.dev,
922 bnxt_re_attributes[i]);
924 bnxt_re_unregister_ib(rdev);
926 if (test_and_clear_bit(BNXT_RE_FLAG_QOS_WORK_REG, &rdev->flags))
927 cancel_delayed_work(&rdev->worker);
929 bnxt_re_cleanup_res(rdev);
930 bnxt_re_free_res(rdev, lock_wait);
932 if (test_and_clear_bit(BNXT_RE_FLAG_RCFW_CHANNEL_EN, &rdev->flags)) {
933 rc = bnxt_qplib_deinit_rcfw(&rdev->rcfw);
935 dev_warn(rdev_to_dev(rdev),
936 "Failed to deinitialize RCFW: %#x", rc);
937 bnxt_re_net_stats_ctx_free(rdev, rdev->qplib_ctx.stats.fw_id,
939 bnxt_qplib_free_ctx(rdev->en_dev->pdev, &rdev->qplib_ctx);
940 bnxt_qplib_disable_rcfw_channel(&rdev->rcfw);
941 bnxt_re_net_ring_free(rdev, rdev->rcfw.creq_ring_id, lock_wait);
942 bnxt_qplib_free_rcfw_channel(&rdev->rcfw);
944 if (test_and_clear_bit(BNXT_RE_FLAG_GOT_MSIX, &rdev->flags)) {
945 rc = bnxt_re_free_msix(rdev, lock_wait);
947 dev_warn(rdev_to_dev(rdev),
948 "Failed to free MSI-X vectors: %#x", rc);
950 if (test_and_clear_bit(BNXT_RE_FLAG_NETDEV_REGISTERED, &rdev->flags)) {
951 rc = bnxt_re_unregister_netdev(rdev, lock_wait);
953 dev_warn(rdev_to_dev(rdev),
954 "Failed to unregister with netdev: %#x", rc);
958 static void bnxt_re_set_resource_limits(struct bnxt_re_dev *rdev)
962 rdev->qplib_ctx.qpc_count = BNXT_RE_MAX_QPC_COUNT;
963 rdev->qplib_ctx.mrw_count = BNXT_RE_MAX_MRW_COUNT;
964 rdev->qplib_ctx.srqc_count = BNXT_RE_MAX_SRQC_COUNT;
965 rdev->qplib_ctx.cq_count = BNXT_RE_MAX_CQ_COUNT;
966 for (i = 0; i < MAX_TQM_ALLOC_REQ; i++)
967 rdev->qplib_ctx.tqm_count[i] =
968 rdev->dev_attr.tqm_alloc_reqs[i];
971 /* worker thread for polling periodic events. Now used for QoS programming*/
972 static void bnxt_re_worker(struct work_struct *work)
974 struct bnxt_re_dev *rdev = container_of(work, struct bnxt_re_dev,
977 bnxt_re_setup_qos(rdev);
978 schedule_delayed_work(&rdev->worker, msecs_to_jiffies(30000));
981 static int bnxt_re_ib_reg(struct bnxt_re_dev *rdev)
985 /* Registered a new RoCE device instance to netdev */
986 rc = bnxt_re_register_netdev(rdev);
988 pr_err("Failed to register with netedev: %#x\n", rc);
991 set_bit(BNXT_RE_FLAG_NETDEV_REGISTERED, &rdev->flags);
993 rc = bnxt_re_request_msix(rdev);
995 pr_err("Failed to get MSI-X vectors: %#x\n", rc);
999 set_bit(BNXT_RE_FLAG_GOT_MSIX, &rdev->flags);
1001 /* Establish RCFW Communication Channel to initialize the context
1002 * memory for the function and all child VFs
1004 rc = bnxt_qplib_alloc_rcfw_channel(rdev->en_dev->pdev, &rdev->rcfw);
1008 rc = bnxt_re_net_ring_alloc
1009 (rdev, rdev->rcfw.creq.pbl[PBL_LVL_0].pg_map_arr,
1010 rdev->rcfw.creq.pbl[rdev->rcfw.creq.level].pg_count,
1011 HWRM_RING_ALLOC_CMPL, BNXT_QPLIB_CREQE_MAX_CNT - 1,
1012 rdev->msix_entries[BNXT_RE_AEQ_IDX].ring_idx,
1013 &rdev->rcfw.creq_ring_id);
1015 pr_err("Failed to allocate CREQ: %#x\n", rc);
1018 rc = bnxt_qplib_enable_rcfw_channel
1019 (rdev->en_dev->pdev, &rdev->rcfw,
1020 rdev->msix_entries[BNXT_RE_AEQ_IDX].vector,
1021 rdev->msix_entries[BNXT_RE_AEQ_IDX].db_offset,
1022 0, &bnxt_re_aeq_handler);
1024 pr_err("Failed to enable RCFW channel: %#x\n", rc);
1028 rc = bnxt_qplib_get_dev_attr(&rdev->rcfw, &rdev->dev_attr);
1031 bnxt_re_set_resource_limits(rdev);
1033 rc = bnxt_qplib_alloc_ctx(rdev->en_dev->pdev, &rdev->qplib_ctx, 0);
1035 pr_err("Failed to allocate QPLIB context: %#x\n", rc);
1038 rc = bnxt_re_net_stats_ctx_alloc(rdev,
1039 rdev->qplib_ctx.stats.dma_map,
1040 &rdev->qplib_ctx.stats.fw_id);
1042 pr_err("Failed to allocate stats context: %#x\n", rc);
1046 rc = bnxt_qplib_init_rcfw(&rdev->rcfw, &rdev->qplib_ctx, 0);
1048 pr_err("Failed to initialize RCFW: %#x\n", rc);
1051 set_bit(BNXT_RE_FLAG_RCFW_CHANNEL_EN, &rdev->flags);
1053 /* Resources based on the 'new' device caps */
1054 rc = bnxt_re_alloc_res(rdev);
1056 pr_err("Failed to allocate resources: %#x\n", rc);
1059 rc = bnxt_re_init_res(rdev);
1061 pr_err("Failed to initialize resources: %#x\n", rc);
1065 rc = bnxt_re_setup_qos(rdev);
1067 pr_info("RoCE priority not yet configured\n");
1069 INIT_DELAYED_WORK(&rdev->worker, bnxt_re_worker);
1070 set_bit(BNXT_RE_FLAG_QOS_WORK_REG, &rdev->flags);
1071 schedule_delayed_work(&rdev->worker, msecs_to_jiffies(30000));
1073 /* Register ib dev */
1074 rc = bnxt_re_register_ib(rdev);
1076 pr_err("Failed to register with IB: %#x\n", rc);
1079 dev_info(rdev_to_dev(rdev), "Device registered successfully");
1080 for (i = 0; i < ARRAY_SIZE(bnxt_re_attributes); i++) {
1081 rc = device_create_file(&rdev->ibdev.dev,
1082 bnxt_re_attributes[i]);
1084 dev_err(rdev_to_dev(rdev),
1085 "Failed to create IB sysfs: %#x", rc);
1086 /* Must clean up all created device files */
1087 for (j = 0; j < i; j++)
1088 device_remove_file(&rdev->ibdev.dev,
1089 bnxt_re_attributes[j]);
1090 bnxt_re_unregister_ib(rdev);
1094 set_bit(BNXT_RE_FLAG_IBDEV_REGISTERED, &rdev->flags);
1095 bnxt_re_dispatch_event(&rdev->ibdev, NULL, 1, IB_EVENT_PORT_ACTIVE);
1096 bnxt_re_dispatch_event(&rdev->ibdev, NULL, 1, IB_EVENT_GID_CHANGE);
1100 bnxt_re_net_stats_ctx_free(rdev, rdev->qplib_ctx.stats.fw_id, true);
1102 bnxt_qplib_free_ctx(rdev->en_dev->pdev, &rdev->qplib_ctx);
1104 bnxt_qplib_disable_rcfw_channel(&rdev->rcfw);
1106 bnxt_re_net_ring_free(rdev, rdev->rcfw.creq_ring_id, true);
1108 bnxt_qplib_free_rcfw_channel(&rdev->rcfw);
1110 bnxt_re_ib_unreg(rdev, true);
1114 static void bnxt_re_dev_unreg(struct bnxt_re_dev *rdev)
1116 struct bnxt_en_dev *en_dev = rdev->en_dev;
1117 struct net_device *netdev = rdev->netdev;
1119 bnxt_re_dev_remove(rdev);
1122 bnxt_re_dev_unprobe(netdev, en_dev);
1125 static int bnxt_re_dev_reg(struct bnxt_re_dev **rdev, struct net_device *netdev)
1127 struct bnxt_en_dev *en_dev;
1130 if (!is_bnxt_re_dev(netdev))
1133 en_dev = bnxt_re_dev_probe(netdev);
1134 if (IS_ERR(en_dev)) {
1135 if (en_dev != ERR_PTR(-ENODEV))
1136 pr_err("%s: Failed to probe\n", ROCE_DRV_MODULE_NAME);
1137 rc = PTR_ERR(en_dev);
1140 *rdev = bnxt_re_dev_add(netdev, en_dev);
1143 bnxt_re_dev_unprobe(netdev, en_dev);
1150 static void bnxt_re_remove_one(struct bnxt_re_dev *rdev)
1152 pci_dev_put(rdev->en_dev->pdev);
1155 /* Handle all deferred netevents tasks */
1156 static void bnxt_re_task(struct work_struct *work)
1158 struct bnxt_re_work *re_work;
1159 struct bnxt_re_dev *rdev;
1162 re_work = container_of(work, struct bnxt_re_work, work);
1163 rdev = re_work->rdev;
1165 if (re_work->event != NETDEV_REGISTER &&
1166 !test_bit(BNXT_RE_FLAG_IBDEV_REGISTERED, &rdev->flags))
1169 switch (re_work->event) {
1170 case NETDEV_REGISTER:
1171 rc = bnxt_re_ib_reg(rdev);
1173 dev_err(rdev_to_dev(rdev),
1174 "Failed to register with IB: %#x", rc);
1177 bnxt_re_dispatch_event(&rdev->ibdev, NULL, 1,
1178 IB_EVENT_PORT_ACTIVE);
1181 bnxt_re_dev_stop(rdev);
1184 if (!netif_carrier_ok(rdev->netdev))
1185 bnxt_re_dev_stop(rdev);
1186 else if (netif_carrier_ok(rdev->netdev))
1187 bnxt_re_dispatch_event(&rdev->ibdev, NULL, 1,
1188 IB_EVENT_PORT_ACTIVE);
1196 static void bnxt_re_init_one(struct bnxt_re_dev *rdev)
1198 pci_dev_get(rdev->en_dev->pdev);
1202 * "Notifier chain callback can be invoked for the same chain from
1203 * different CPUs at the same time".
1205 * For cases when the netdev is already present, our call to the
1206 * register_netdevice_notifier() will actually get the rtnl_lock()
1207 * before sending NETDEV_REGISTER and (if up) NETDEV_UP
1210 * But for cases when the netdev is not already present, the notifier
1211 * chain is subjected to be invoked from different CPUs simultaneously.
1213 * This is protected by the netdev_mutex.
1215 static int bnxt_re_netdev_event(struct notifier_block *notifier,
1216 unsigned long event, void *ptr)
1218 struct net_device *real_dev, *netdev = netdev_notifier_info_to_dev(ptr);
1219 struct bnxt_re_work *re_work;
1220 struct bnxt_re_dev *rdev;
1222 bool sch_work = false;
1224 real_dev = rdma_vlan_dev_real_dev(netdev);
1228 rdev = bnxt_re_from_netdev(real_dev);
1229 if (!rdev && event != NETDEV_REGISTER)
1231 if (real_dev != netdev)
1235 case NETDEV_REGISTER:
1238 rc = bnxt_re_dev_reg(&rdev, real_dev);
1242 pr_err("Failed to register with the device %s: %#x\n",
1243 real_dev->name, rc);
1246 bnxt_re_init_one(rdev);
1250 case NETDEV_UNREGISTER:
1251 bnxt_re_ib_unreg(rdev, false);
1252 bnxt_re_remove_one(rdev);
1253 bnxt_re_dev_unreg(rdev);
1261 /* Allocate for the deferred task */
1262 re_work = kzalloc(sizeof(*re_work), GFP_ATOMIC);
1264 re_work->rdev = rdev;
1265 re_work->event = event;
1266 re_work->vlan_dev = (real_dev == netdev ?
1268 INIT_WORK(&re_work->work, bnxt_re_task);
1269 queue_work(bnxt_re_wq, &re_work->work);
1277 static struct notifier_block bnxt_re_netdev_notifier = {
1278 .notifier_call = bnxt_re_netdev_event
1281 static int __init bnxt_re_mod_init(void)
1285 pr_info("%s: %s", ROCE_DRV_MODULE_NAME, version);
1287 bnxt_re_wq = create_singlethread_workqueue("bnxt_re");
1291 INIT_LIST_HEAD(&bnxt_re_dev_list);
1293 rc = register_netdevice_notifier(&bnxt_re_netdev_notifier);
1295 pr_err("%s: Cannot register to netdevice_notifier",
1296 ROCE_DRV_MODULE_NAME);
1302 destroy_workqueue(bnxt_re_wq);
1307 static void __exit bnxt_re_mod_exit(void)
1309 unregister_netdevice_notifier(&bnxt_re_netdev_notifier);
1311 destroy_workqueue(bnxt_re_wq);
1314 module_init(bnxt_re_mod_init);
1315 module_exit(bnxt_re_mod_exit);