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 req.stat_ctx_flags = STAT_CTX_ALLOC_REQ_STAT_CTX_FLAGS_ROCE;
337 bnxt_re_fill_fw_msg(&fw_msg, (void *)&req, sizeof(req), (void *)&resp,
338 sizeof(resp), DFLT_HWRM_CMD_TIMEOUT);
339 rc = en_dev->en_ops->bnxt_send_fw_msg(en_dev, BNXT_ROCE_ULP, &fw_msg);
341 *fw_stats_ctx_id = le32_to_cpu(resp.stat_ctx_id);
349 static bool is_bnxt_re_dev(struct net_device *netdev)
351 struct ethtool_drvinfo drvinfo;
353 if (netdev->ethtool_ops && netdev->ethtool_ops->get_drvinfo) {
354 memset(&drvinfo, 0, sizeof(drvinfo));
355 netdev->ethtool_ops->get_drvinfo(netdev, &drvinfo);
357 if (strcmp(drvinfo.driver, "bnxt_en"))
364 static struct bnxt_re_dev *bnxt_re_from_netdev(struct net_device *netdev)
366 struct bnxt_re_dev *rdev;
369 list_for_each_entry_rcu(rdev, &bnxt_re_dev_list, list) {
370 if (rdev->netdev == netdev) {
379 static void bnxt_re_dev_unprobe(struct net_device *netdev,
380 struct bnxt_en_dev *en_dev)
383 module_put(en_dev->pdev->driver->driver.owner);
386 static struct bnxt_en_dev *bnxt_re_dev_probe(struct net_device *netdev)
388 struct bnxt *bp = netdev_priv(netdev);
389 struct bnxt_en_dev *en_dev;
390 struct pci_dev *pdev;
392 /* Call bnxt_en's RoCE probe via indirect API */
394 return ERR_PTR(-EINVAL);
396 en_dev = bp->ulp_probe(netdev);
402 return ERR_PTR(-EINVAL);
404 if (!(en_dev->flags & BNXT_EN_FLAG_ROCE_CAP)) {
406 "%s: probe error: RoCE is not supported on this device",
407 ROCE_DRV_MODULE_NAME);
408 return ERR_PTR(-ENODEV);
411 /* Bump net device reference count */
412 if (!try_module_get(pdev->driver->driver.owner))
413 return ERR_PTR(-ENODEV);
420 static void bnxt_re_unregister_ib(struct bnxt_re_dev *rdev)
422 ib_unregister_device(&rdev->ibdev);
425 static int bnxt_re_register_ib(struct bnxt_re_dev *rdev)
427 struct ib_device *ibdev = &rdev->ibdev;
430 ibdev->owner = THIS_MODULE;
431 ibdev->node_type = RDMA_NODE_IB_CA;
432 strlcpy(ibdev->name, "bnxt_re%d", IB_DEVICE_NAME_MAX);
433 strlcpy(ibdev->node_desc, BNXT_RE_DESC " HCA",
434 strlen(BNXT_RE_DESC) + 5);
435 ibdev->phys_port_cnt = 1;
437 bnxt_qplib_get_guid(rdev->netdev->dev_addr, (u8 *)&ibdev->node_guid);
439 ibdev->num_comp_vectors = 1;
440 ibdev->dev.parent = &rdev->en_dev->pdev->dev;
441 ibdev->local_dma_lkey = BNXT_QPLIB_RSVD_LKEY;
444 ibdev->uverbs_abi_ver = BNXT_RE_ABI_VERSION;
445 ibdev->uverbs_cmd_mask =
446 (1ull << IB_USER_VERBS_CMD_GET_CONTEXT) |
447 (1ull << IB_USER_VERBS_CMD_QUERY_DEVICE) |
448 (1ull << IB_USER_VERBS_CMD_QUERY_PORT) |
449 (1ull << IB_USER_VERBS_CMD_ALLOC_PD) |
450 (1ull << IB_USER_VERBS_CMD_DEALLOC_PD) |
451 (1ull << IB_USER_VERBS_CMD_REG_MR) |
452 (1ull << IB_USER_VERBS_CMD_REREG_MR) |
453 (1ull << IB_USER_VERBS_CMD_DEREG_MR) |
454 (1ull << IB_USER_VERBS_CMD_CREATE_COMP_CHANNEL) |
455 (1ull << IB_USER_VERBS_CMD_CREATE_CQ) |
456 (1ull << IB_USER_VERBS_CMD_RESIZE_CQ) |
457 (1ull << IB_USER_VERBS_CMD_DESTROY_CQ) |
458 (1ull << IB_USER_VERBS_CMD_CREATE_QP) |
459 (1ull << IB_USER_VERBS_CMD_MODIFY_QP) |
460 (1ull << IB_USER_VERBS_CMD_QUERY_QP) |
461 (1ull << IB_USER_VERBS_CMD_DESTROY_QP) |
462 (1ull << IB_USER_VERBS_CMD_CREATE_SRQ) |
463 (1ull << IB_USER_VERBS_CMD_MODIFY_SRQ) |
464 (1ull << IB_USER_VERBS_CMD_QUERY_SRQ) |
465 (1ull << IB_USER_VERBS_CMD_DESTROY_SRQ) |
466 (1ull << IB_USER_VERBS_CMD_CREATE_AH) |
467 (1ull << IB_USER_VERBS_CMD_MODIFY_AH) |
468 (1ull << IB_USER_VERBS_CMD_QUERY_AH) |
469 (1ull << IB_USER_VERBS_CMD_DESTROY_AH);
470 /* POLL_CQ and REQ_NOTIFY_CQ is directly handled in libbnxt_re */
473 ibdev->query_device = bnxt_re_query_device;
474 ibdev->modify_device = bnxt_re_modify_device;
476 ibdev->query_port = bnxt_re_query_port;
477 ibdev->modify_port = bnxt_re_modify_port;
478 ibdev->get_port_immutable = bnxt_re_get_port_immutable;
479 ibdev->query_pkey = bnxt_re_query_pkey;
480 ibdev->query_gid = bnxt_re_query_gid;
481 ibdev->get_netdev = bnxt_re_get_netdev;
482 ibdev->add_gid = bnxt_re_add_gid;
483 ibdev->del_gid = bnxt_re_del_gid;
484 ibdev->get_link_layer = bnxt_re_get_link_layer;
486 ibdev->alloc_pd = bnxt_re_alloc_pd;
487 ibdev->dealloc_pd = bnxt_re_dealloc_pd;
489 ibdev->create_ah = bnxt_re_create_ah;
490 ibdev->modify_ah = bnxt_re_modify_ah;
491 ibdev->query_ah = bnxt_re_query_ah;
492 ibdev->destroy_ah = bnxt_re_destroy_ah;
494 ibdev->create_qp = bnxt_re_create_qp;
495 ibdev->modify_qp = bnxt_re_modify_qp;
496 ibdev->query_qp = bnxt_re_query_qp;
497 ibdev->destroy_qp = bnxt_re_destroy_qp;
499 ibdev->post_send = bnxt_re_post_send;
500 ibdev->post_recv = bnxt_re_post_recv;
502 ibdev->create_cq = bnxt_re_create_cq;
503 ibdev->destroy_cq = bnxt_re_destroy_cq;
504 ibdev->poll_cq = bnxt_re_poll_cq;
505 ibdev->req_notify_cq = bnxt_re_req_notify_cq;
507 ibdev->get_dma_mr = bnxt_re_get_dma_mr;
508 ibdev->dereg_mr = bnxt_re_dereg_mr;
509 ibdev->alloc_mr = bnxt_re_alloc_mr;
510 ibdev->map_mr_sg = bnxt_re_map_mr_sg;
512 ibdev->reg_user_mr = bnxt_re_reg_user_mr;
513 ibdev->alloc_ucontext = bnxt_re_alloc_ucontext;
514 ibdev->dealloc_ucontext = bnxt_re_dealloc_ucontext;
515 ibdev->mmap = bnxt_re_mmap;
517 return ib_register_device(ibdev, NULL);
520 static ssize_t show_rev(struct device *device, struct device_attribute *attr,
523 struct bnxt_re_dev *rdev = to_bnxt_re_dev(device, ibdev.dev);
525 return scnprintf(buf, PAGE_SIZE, "0x%x\n", rdev->en_dev->pdev->vendor);
528 static ssize_t show_fw_ver(struct device *device, struct device_attribute *attr,
531 struct bnxt_re_dev *rdev = to_bnxt_re_dev(device, ibdev.dev);
533 return scnprintf(buf, PAGE_SIZE, "%s\n", rdev->dev_attr.fw_ver);
536 static ssize_t show_hca(struct device *device, struct device_attribute *attr,
539 struct bnxt_re_dev *rdev = to_bnxt_re_dev(device, ibdev.dev);
541 return scnprintf(buf, PAGE_SIZE, "%s\n", rdev->ibdev.node_desc);
544 static DEVICE_ATTR(hw_rev, 0444, show_rev, NULL);
545 static DEVICE_ATTR(fw_rev, 0444, show_fw_ver, NULL);
546 static DEVICE_ATTR(hca_type, 0444, show_hca, NULL);
548 static struct device_attribute *bnxt_re_attributes[] = {
554 static void bnxt_re_dev_remove(struct bnxt_re_dev *rdev)
556 dev_put(rdev->netdev);
559 mutex_lock(&bnxt_re_dev_lock);
560 list_del_rcu(&rdev->list);
561 mutex_unlock(&bnxt_re_dev_lock);
564 flush_workqueue(bnxt_re_wq);
566 ib_dealloc_device(&rdev->ibdev);
570 static struct bnxt_re_dev *bnxt_re_dev_add(struct net_device *netdev,
571 struct bnxt_en_dev *en_dev)
573 struct bnxt_re_dev *rdev;
575 /* Allocate bnxt_re_dev instance here */
576 rdev = (struct bnxt_re_dev *)ib_alloc_device(sizeof(*rdev));
578 dev_err(NULL, "%s: bnxt_re_dev allocation failure!",
579 ROCE_DRV_MODULE_NAME);
583 rdev->netdev = netdev;
584 dev_hold(rdev->netdev);
585 rdev->en_dev = en_dev;
586 rdev->id = rdev->en_dev->pdev->devfn;
587 INIT_LIST_HEAD(&rdev->qp_list);
588 mutex_init(&rdev->qp_lock);
589 atomic_set(&rdev->qp_count, 0);
590 atomic_set(&rdev->cq_count, 0);
591 atomic_set(&rdev->srq_count, 0);
592 atomic_set(&rdev->mr_count, 0);
593 atomic_set(&rdev->mw_count, 0);
594 rdev->cosq[0] = 0xFFFF;
595 rdev->cosq[1] = 0xFFFF;
597 mutex_lock(&bnxt_re_dev_lock);
598 list_add_tail_rcu(&rdev->list, &bnxt_re_dev_list);
599 mutex_unlock(&bnxt_re_dev_lock);
603 static int bnxt_re_aeq_handler(struct bnxt_qplib_rcfw *rcfw,
604 struct creq_func_event *aeqe)
606 switch (aeqe->event) {
607 case CREQ_FUNC_EVENT_EVENT_TX_WQE_ERROR:
609 case CREQ_FUNC_EVENT_EVENT_TX_DATA_ERROR:
611 case CREQ_FUNC_EVENT_EVENT_RX_WQE_ERROR:
613 case CREQ_FUNC_EVENT_EVENT_RX_DATA_ERROR:
615 case CREQ_FUNC_EVENT_EVENT_CQ_ERROR:
617 case CREQ_FUNC_EVENT_EVENT_TQM_ERROR:
619 case CREQ_FUNC_EVENT_EVENT_CFCQ_ERROR:
621 case CREQ_FUNC_EVENT_EVENT_CFCS_ERROR:
623 case CREQ_FUNC_EVENT_EVENT_CFCC_ERROR:
625 case CREQ_FUNC_EVENT_EVENT_CFCM_ERROR:
627 case CREQ_FUNC_EVENT_EVENT_TIM_ERROR:
635 static int bnxt_re_cqn_handler(struct bnxt_qplib_nq *nq,
636 struct bnxt_qplib_cq *handle)
638 struct bnxt_re_cq *cq = container_of(handle, struct bnxt_re_cq,
642 dev_err(NULL, "%s: CQ is NULL, CQN not handled",
643 ROCE_DRV_MODULE_NAME);
646 if (cq->ib_cq.comp_handler) {
647 /* Lock comp_handler? */
648 (*cq->ib_cq.comp_handler)(&cq->ib_cq, cq->ib_cq.cq_context);
654 static void bnxt_re_cleanup_res(struct bnxt_re_dev *rdev)
656 if (rdev->nq.hwq.max_elements)
657 bnxt_qplib_disable_nq(&rdev->nq);
659 if (rdev->qplib_res.rcfw)
660 bnxt_qplib_cleanup_res(&rdev->qplib_res);
663 static int bnxt_re_init_res(struct bnxt_re_dev *rdev)
667 bnxt_qplib_init_res(&rdev->qplib_res);
669 if (rdev->msix_entries[BNXT_RE_NQ_IDX].vector <= 0)
672 rc = bnxt_qplib_enable_nq(rdev->en_dev->pdev, &rdev->nq,
673 rdev->msix_entries[BNXT_RE_NQ_IDX].vector,
674 rdev->msix_entries[BNXT_RE_NQ_IDX].db_offset,
675 &bnxt_re_cqn_handler,
679 dev_err(rdev_to_dev(rdev), "Failed to enable NQ: %#x", rc);
684 static void bnxt_re_free_res(struct bnxt_re_dev *rdev, bool lock_wait)
686 if (rdev->nq.hwq.max_elements) {
687 bnxt_re_net_ring_free(rdev, rdev->nq.ring_id, lock_wait);
688 bnxt_qplib_free_nq(&rdev->nq);
690 if (rdev->qplib_res.dpi_tbl.max) {
691 bnxt_qplib_dealloc_dpi(&rdev->qplib_res,
692 &rdev->qplib_res.dpi_tbl,
693 &rdev->dpi_privileged);
695 if (rdev->qplib_res.rcfw) {
696 bnxt_qplib_free_res(&rdev->qplib_res);
697 rdev->qplib_res.rcfw = NULL;
701 static int bnxt_re_alloc_res(struct bnxt_re_dev *rdev)
705 /* Configure and allocate resources for qplib */
706 rdev->qplib_res.rcfw = &rdev->rcfw;
707 rc = bnxt_qplib_get_dev_attr(&rdev->rcfw, &rdev->dev_attr);
711 rc = bnxt_qplib_alloc_res(&rdev->qplib_res, rdev->en_dev->pdev,
712 rdev->netdev, &rdev->dev_attr);
716 rc = bnxt_qplib_alloc_dpi(&rdev->qplib_res.dpi_tbl,
717 &rdev->dpi_privileged,
722 rdev->nq.hwq.max_elements = BNXT_RE_MAX_CQ_COUNT +
723 BNXT_RE_MAX_SRQC_COUNT + 2;
724 rc = bnxt_qplib_alloc_nq(rdev->en_dev->pdev, &rdev->nq);
726 dev_err(rdev_to_dev(rdev),
727 "Failed to allocate NQ memory: %#x", rc);
730 rc = bnxt_re_net_ring_alloc
731 (rdev, rdev->nq.hwq.pbl[PBL_LVL_0].pg_map_arr,
732 rdev->nq.hwq.pbl[rdev->nq.hwq.level].pg_count,
733 HWRM_RING_ALLOC_CMPL, BNXT_QPLIB_NQE_MAX_CNT - 1,
734 rdev->msix_entries[BNXT_RE_NQ_IDX].ring_idx,
737 dev_err(rdev_to_dev(rdev),
738 "Failed to allocate NQ ring: %#x", rc);
743 bnxt_qplib_free_nq(&rdev->nq);
745 rdev->qplib_res.rcfw = NULL;
749 static void bnxt_re_dispatch_event(struct ib_device *ibdev, struct ib_qp *qp,
750 u8 port_num, enum ib_event_type event)
752 struct ib_event ib_event;
754 ib_event.device = ibdev;
756 ib_event.element.qp = qp;
758 ib_event.element.port_num = port_num;
759 ib_event.event = event;
760 ib_dispatch_event(&ib_event);
763 #define HWRM_QUEUE_PRI2COS_QCFG_INPUT_FLAGS_IVLAN 0x02
764 static int bnxt_re_query_hwrm_pri2cos(struct bnxt_re_dev *rdev, u8 dir,
767 struct hwrm_queue_pri2cos_qcfg_input req = {0};
768 struct bnxt *bp = netdev_priv(rdev->netdev);
769 struct hwrm_queue_pri2cos_qcfg_output resp;
770 struct bnxt_en_dev *en_dev = rdev->en_dev;
771 struct bnxt_fw_msg fw_msg;
773 u8 *qcfgmap, *tmp_map;
779 memset(&fw_msg, 0, sizeof(fw_msg));
780 bnxt_re_init_hwrm_hdr(rdev, (void *)&req,
781 HWRM_QUEUE_PRI2COS_QCFG, -1, -1);
782 flags |= (dir & 0x01);
783 flags |= HWRM_QUEUE_PRI2COS_QCFG_INPUT_FLAGS_IVLAN;
784 req.flags = cpu_to_le32(flags);
785 req.port_id = bp->pf.port_id;
787 bnxt_re_fill_fw_msg(&fw_msg, (void *)&req, sizeof(req), (void *)&resp,
788 sizeof(resp), DFLT_HWRM_CMD_TIMEOUT);
789 rc = en_dev->en_ops->bnxt_send_fw_msg(en_dev, BNXT_ROCE_ULP, &fw_msg);
793 if (resp.queue_cfg_info) {
794 dev_warn(rdev_to_dev(rdev),
795 "Asymmetric cos queue configuration detected");
796 dev_warn(rdev_to_dev(rdev),
797 " on device, QoS may not be fully functional\n");
799 qcfgmap = &resp.pri0_cos_queue_id;
800 tmp_map = (u8 *)cid_map;
801 for (i = 0; i < IEEE_8021QAZ_MAX_TCS; i++)
802 tmp_map[i] = qcfgmap[i];
807 static bool bnxt_re_is_qp1_or_shadow_qp(struct bnxt_re_dev *rdev,
808 struct bnxt_re_qp *qp)
810 return (qp->ib_qp.qp_type == IB_QPT_GSI) || (qp == rdev->qp1_sqp);
813 static void bnxt_re_dev_stop(struct bnxt_re_dev *rdev)
815 int mask = IB_QP_STATE;
816 struct ib_qp_attr qp_attr;
817 struct bnxt_re_qp *qp;
819 qp_attr.qp_state = IB_QPS_ERR;
820 mutex_lock(&rdev->qp_lock);
821 list_for_each_entry(qp, &rdev->qp_list, list) {
822 /* Modify the state of all QPs except QP1/Shadow QP */
823 if (!bnxt_re_is_qp1_or_shadow_qp(rdev, qp)) {
824 if (qp->qplib_qp.state !=
825 CMDQ_MODIFY_QP_NEW_STATE_RESET &&
826 qp->qplib_qp.state !=
827 CMDQ_MODIFY_QP_NEW_STATE_ERR) {
828 bnxt_re_dispatch_event(&rdev->ibdev, &qp->ib_qp,
829 1, IB_EVENT_QP_FATAL);
830 bnxt_re_modify_qp(&qp->ib_qp, &qp_attr, mask,
835 mutex_unlock(&rdev->qp_lock);
838 static u32 bnxt_re_get_priority_mask(struct bnxt_re_dev *rdev)
840 u32 prio_map = 0, tmp_map = 0;
841 struct net_device *netdev;
844 netdev = rdev->netdev;
846 memset(&app, 0, sizeof(app));
847 app.selector = IEEE_8021QAZ_APP_SEL_ETHERTYPE;
848 app.protocol = ETH_P_IBOE;
849 tmp_map = dcb_ieee_getapp_mask(netdev, &app);
852 app.selector = IEEE_8021QAZ_APP_SEL_DGRAM;
853 app.protocol = ROCE_V2_UDP_DPORT;
854 tmp_map = dcb_ieee_getapp_mask(netdev, &app);
862 static void bnxt_re_parse_cid_map(u8 prio_map, u8 *cid_map, u16 *cosq)
867 for (prio = 0, id = 0; prio < 8; prio++) {
868 if (prio_map & (1 << prio)) {
869 cosq[id] = cid_map[prio];
871 if (id == 2) /* Max 2 tcs supported */
877 static int bnxt_re_setup_qos(struct bnxt_re_dev *rdev)
883 /* Get priority for roce */
884 rc = bnxt_re_get_priority_mask(rdev);
889 if (prio_map == rdev->cur_prio_map)
891 rdev->cur_prio_map = prio_map;
892 /* Get cosq id for this priority */
893 rc = bnxt_re_query_hwrm_pri2cos(rdev, 0, &cid_map);
895 dev_warn(rdev_to_dev(rdev), "no cos for p_mask %x\n", prio_map);
898 /* Parse CoS IDs for app priority */
899 bnxt_re_parse_cid_map(prio_map, (u8 *)&cid_map, rdev->cosq);
902 rc = bnxt_qplib_map_tc2cos(&rdev->qplib_res, rdev->cosq);
904 dev_warn(rdev_to_dev(rdev), "no tc for cos{%x, %x}\n",
905 rdev->cosq[0], rdev->cosq[1]);
912 static void bnxt_re_ib_unreg(struct bnxt_re_dev *rdev, bool lock_wait)
916 if (test_and_clear_bit(BNXT_RE_FLAG_IBDEV_REGISTERED, &rdev->flags)) {
917 for (i = 0; i < ARRAY_SIZE(bnxt_re_attributes); i++)
918 device_remove_file(&rdev->ibdev.dev,
919 bnxt_re_attributes[i]);
921 bnxt_re_unregister_ib(rdev);
923 if (test_and_clear_bit(BNXT_RE_FLAG_QOS_WORK_REG, &rdev->flags))
924 cancel_delayed_work(&rdev->worker);
926 bnxt_re_cleanup_res(rdev);
927 bnxt_re_free_res(rdev, lock_wait);
929 if (test_and_clear_bit(BNXT_RE_FLAG_RCFW_CHANNEL_EN, &rdev->flags)) {
930 rc = bnxt_qplib_deinit_rcfw(&rdev->rcfw);
932 dev_warn(rdev_to_dev(rdev),
933 "Failed to deinitialize RCFW: %#x", rc);
934 bnxt_re_net_stats_ctx_free(rdev, rdev->qplib_ctx.stats.fw_id,
936 bnxt_qplib_free_ctx(rdev->en_dev->pdev, &rdev->qplib_ctx);
937 bnxt_qplib_disable_rcfw_channel(&rdev->rcfw);
938 bnxt_re_net_ring_free(rdev, rdev->rcfw.creq_ring_id, lock_wait);
939 bnxt_qplib_free_rcfw_channel(&rdev->rcfw);
941 if (test_and_clear_bit(BNXT_RE_FLAG_GOT_MSIX, &rdev->flags)) {
942 rc = bnxt_re_free_msix(rdev, lock_wait);
944 dev_warn(rdev_to_dev(rdev),
945 "Failed to free MSI-X vectors: %#x", rc);
947 if (test_and_clear_bit(BNXT_RE_FLAG_NETDEV_REGISTERED, &rdev->flags)) {
948 rc = bnxt_re_unregister_netdev(rdev, lock_wait);
950 dev_warn(rdev_to_dev(rdev),
951 "Failed to unregister with netdev: %#x", rc);
955 static void bnxt_re_set_resource_limits(struct bnxt_re_dev *rdev)
959 rdev->qplib_ctx.qpc_count = BNXT_RE_MAX_QPC_COUNT;
960 rdev->qplib_ctx.mrw_count = BNXT_RE_MAX_MRW_COUNT;
961 rdev->qplib_ctx.srqc_count = BNXT_RE_MAX_SRQC_COUNT;
962 rdev->qplib_ctx.cq_count = BNXT_RE_MAX_CQ_COUNT;
963 for (i = 0; i < MAX_TQM_ALLOC_REQ; i++)
964 rdev->qplib_ctx.tqm_count[i] =
965 rdev->dev_attr.tqm_alloc_reqs[i];
968 /* worker thread for polling periodic events. Now used for QoS programming*/
969 static void bnxt_re_worker(struct work_struct *work)
971 struct bnxt_re_dev *rdev = container_of(work, struct bnxt_re_dev,
974 bnxt_re_setup_qos(rdev);
975 schedule_delayed_work(&rdev->worker, msecs_to_jiffies(30000));
978 static int bnxt_re_ib_reg(struct bnxt_re_dev *rdev)
982 /* Registered a new RoCE device instance to netdev */
983 rc = bnxt_re_register_netdev(rdev);
985 pr_err("Failed to register with netedev: %#x\n", rc);
988 set_bit(BNXT_RE_FLAG_NETDEV_REGISTERED, &rdev->flags);
990 rc = bnxt_re_request_msix(rdev);
992 pr_err("Failed to get MSI-X vectors: %#x\n", rc);
996 set_bit(BNXT_RE_FLAG_GOT_MSIX, &rdev->flags);
998 /* Establish RCFW Communication Channel to initialize the context
999 * memory for the function and all child VFs
1001 rc = bnxt_qplib_alloc_rcfw_channel(rdev->en_dev->pdev, &rdev->rcfw);
1005 rc = bnxt_re_net_ring_alloc
1006 (rdev, rdev->rcfw.creq.pbl[PBL_LVL_0].pg_map_arr,
1007 rdev->rcfw.creq.pbl[rdev->rcfw.creq.level].pg_count,
1008 HWRM_RING_ALLOC_CMPL, BNXT_QPLIB_CREQE_MAX_CNT - 1,
1009 rdev->msix_entries[BNXT_RE_AEQ_IDX].ring_idx,
1010 &rdev->rcfw.creq_ring_id);
1012 pr_err("Failed to allocate CREQ: %#x\n", rc);
1015 rc = bnxt_qplib_enable_rcfw_channel
1016 (rdev->en_dev->pdev, &rdev->rcfw,
1017 rdev->msix_entries[BNXT_RE_AEQ_IDX].vector,
1018 rdev->msix_entries[BNXT_RE_AEQ_IDX].db_offset,
1019 0, &bnxt_re_aeq_handler);
1021 pr_err("Failed to enable RCFW channel: %#x\n", rc);
1025 rc = bnxt_qplib_get_dev_attr(&rdev->rcfw, &rdev->dev_attr);
1028 bnxt_re_set_resource_limits(rdev);
1030 rc = bnxt_qplib_alloc_ctx(rdev->en_dev->pdev, &rdev->qplib_ctx, 0);
1032 pr_err("Failed to allocate QPLIB context: %#x\n", rc);
1035 rc = bnxt_re_net_stats_ctx_alloc(rdev,
1036 rdev->qplib_ctx.stats.dma_map,
1037 &rdev->qplib_ctx.stats.fw_id);
1039 pr_err("Failed to allocate stats context: %#x\n", rc);
1043 rc = bnxt_qplib_init_rcfw(&rdev->rcfw, &rdev->qplib_ctx, 0);
1045 pr_err("Failed to initialize RCFW: %#x\n", rc);
1048 set_bit(BNXT_RE_FLAG_RCFW_CHANNEL_EN, &rdev->flags);
1050 /* Resources based on the 'new' device caps */
1051 rc = bnxt_re_alloc_res(rdev);
1053 pr_err("Failed to allocate resources: %#x\n", rc);
1056 rc = bnxt_re_init_res(rdev);
1058 pr_err("Failed to initialize resources: %#x\n", rc);
1062 rc = bnxt_re_setup_qos(rdev);
1064 pr_info("RoCE priority not yet configured\n");
1066 INIT_DELAYED_WORK(&rdev->worker, bnxt_re_worker);
1067 set_bit(BNXT_RE_FLAG_QOS_WORK_REG, &rdev->flags);
1068 schedule_delayed_work(&rdev->worker, msecs_to_jiffies(30000));
1070 /* Register ib dev */
1071 rc = bnxt_re_register_ib(rdev);
1073 pr_err("Failed to register with IB: %#x\n", rc);
1076 dev_info(rdev_to_dev(rdev), "Device registered successfully");
1077 for (i = 0; i < ARRAY_SIZE(bnxt_re_attributes); i++) {
1078 rc = device_create_file(&rdev->ibdev.dev,
1079 bnxt_re_attributes[i]);
1081 dev_err(rdev_to_dev(rdev),
1082 "Failed to create IB sysfs: %#x", rc);
1083 /* Must clean up all created device files */
1084 for (j = 0; j < i; j++)
1085 device_remove_file(&rdev->ibdev.dev,
1086 bnxt_re_attributes[j]);
1087 bnxt_re_unregister_ib(rdev);
1091 set_bit(BNXT_RE_FLAG_IBDEV_REGISTERED, &rdev->flags);
1092 bnxt_re_dispatch_event(&rdev->ibdev, NULL, 1, IB_EVENT_PORT_ACTIVE);
1093 bnxt_re_dispatch_event(&rdev->ibdev, NULL, 1, IB_EVENT_GID_CHANGE);
1097 bnxt_re_net_stats_ctx_free(rdev, rdev->qplib_ctx.stats.fw_id, true);
1099 bnxt_qplib_free_ctx(rdev->en_dev->pdev, &rdev->qplib_ctx);
1101 bnxt_qplib_disable_rcfw_channel(&rdev->rcfw);
1103 bnxt_re_net_ring_free(rdev, rdev->rcfw.creq_ring_id, true);
1105 bnxt_qplib_free_rcfw_channel(&rdev->rcfw);
1107 bnxt_re_ib_unreg(rdev, true);
1111 static void bnxt_re_dev_unreg(struct bnxt_re_dev *rdev)
1113 struct bnxt_en_dev *en_dev = rdev->en_dev;
1114 struct net_device *netdev = rdev->netdev;
1116 bnxt_re_dev_remove(rdev);
1119 bnxt_re_dev_unprobe(netdev, en_dev);
1122 static int bnxt_re_dev_reg(struct bnxt_re_dev **rdev, struct net_device *netdev)
1124 struct bnxt_en_dev *en_dev;
1127 if (!is_bnxt_re_dev(netdev))
1130 en_dev = bnxt_re_dev_probe(netdev);
1131 if (IS_ERR(en_dev)) {
1132 if (en_dev != ERR_PTR(-ENODEV))
1133 pr_err("%s: Failed to probe\n", ROCE_DRV_MODULE_NAME);
1134 rc = PTR_ERR(en_dev);
1137 *rdev = bnxt_re_dev_add(netdev, en_dev);
1140 bnxt_re_dev_unprobe(netdev, en_dev);
1147 static void bnxt_re_remove_one(struct bnxt_re_dev *rdev)
1149 pci_dev_put(rdev->en_dev->pdev);
1152 /* Handle all deferred netevents tasks */
1153 static void bnxt_re_task(struct work_struct *work)
1155 struct bnxt_re_work *re_work;
1156 struct bnxt_re_dev *rdev;
1159 re_work = container_of(work, struct bnxt_re_work, work);
1160 rdev = re_work->rdev;
1162 if (re_work->event != NETDEV_REGISTER &&
1163 !test_bit(BNXT_RE_FLAG_IBDEV_REGISTERED, &rdev->flags))
1166 switch (re_work->event) {
1167 case NETDEV_REGISTER:
1168 rc = bnxt_re_ib_reg(rdev);
1170 dev_err(rdev_to_dev(rdev),
1171 "Failed to register with IB: %#x", rc);
1174 bnxt_re_dispatch_event(&rdev->ibdev, NULL, 1,
1175 IB_EVENT_PORT_ACTIVE);
1178 bnxt_re_dev_stop(rdev);
1181 if (!netif_carrier_ok(rdev->netdev))
1182 bnxt_re_dev_stop(rdev);
1183 else if (netif_carrier_ok(rdev->netdev))
1184 bnxt_re_dispatch_event(&rdev->ibdev, NULL, 1,
1185 IB_EVENT_PORT_ACTIVE);
1193 static void bnxt_re_init_one(struct bnxt_re_dev *rdev)
1195 pci_dev_get(rdev->en_dev->pdev);
1199 * "Notifier chain callback can be invoked for the same chain from
1200 * different CPUs at the same time".
1202 * For cases when the netdev is already present, our call to the
1203 * register_netdevice_notifier() will actually get the rtnl_lock()
1204 * before sending NETDEV_REGISTER and (if up) NETDEV_UP
1207 * But for cases when the netdev is not already present, the notifier
1208 * chain is subjected to be invoked from different CPUs simultaneously.
1210 * This is protected by the netdev_mutex.
1212 static int bnxt_re_netdev_event(struct notifier_block *notifier,
1213 unsigned long event, void *ptr)
1215 struct net_device *real_dev, *netdev = netdev_notifier_info_to_dev(ptr);
1216 struct bnxt_re_work *re_work;
1217 struct bnxt_re_dev *rdev;
1219 bool sch_work = false;
1221 real_dev = rdma_vlan_dev_real_dev(netdev);
1225 rdev = bnxt_re_from_netdev(real_dev);
1226 if (!rdev && event != NETDEV_REGISTER)
1228 if (real_dev != netdev)
1232 case NETDEV_REGISTER:
1235 rc = bnxt_re_dev_reg(&rdev, real_dev);
1239 pr_err("Failed to register with the device %s: %#x\n",
1240 real_dev->name, rc);
1243 bnxt_re_init_one(rdev);
1247 case NETDEV_UNREGISTER:
1248 bnxt_re_ib_unreg(rdev, false);
1249 bnxt_re_remove_one(rdev);
1250 bnxt_re_dev_unreg(rdev);
1258 /* Allocate for the deferred task */
1259 re_work = kzalloc(sizeof(*re_work), GFP_ATOMIC);
1261 re_work->rdev = rdev;
1262 re_work->event = event;
1263 re_work->vlan_dev = (real_dev == netdev ?
1265 INIT_WORK(&re_work->work, bnxt_re_task);
1266 queue_work(bnxt_re_wq, &re_work->work);
1274 static struct notifier_block bnxt_re_netdev_notifier = {
1275 .notifier_call = bnxt_re_netdev_event
1278 static int __init bnxt_re_mod_init(void)
1282 pr_info("%s: %s", ROCE_DRV_MODULE_NAME, version);
1284 bnxt_re_wq = create_singlethread_workqueue("bnxt_re");
1288 INIT_LIST_HEAD(&bnxt_re_dev_list);
1290 rc = register_netdevice_notifier(&bnxt_re_netdev_notifier);
1292 pr_err("%s: Cannot register to netdevice_notifier",
1293 ROCE_DRV_MODULE_NAME);
1299 destroy_workqueue(bnxt_re_wq);
1304 static void __exit bnxt_re_mod_exit(void)
1306 unregister_netdevice_notifier(&bnxt_re_netdev_notifier);
1308 destroy_workqueue(bnxt_re_wq);
1311 module_init(bnxt_re_mod_init);
1312 module_exit(bnxt_re_mod_exit);