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: IB Verbs interpreter
39 #include <linux/interrupt.h>
40 #include <linux/types.h>
41 #include <linux/pci.h>
42 #include <linux/netdevice.h>
43 #include <linux/if_ether.h>
45 #include <rdma/ib_verbs.h>
46 #include <rdma/ib_user_verbs.h>
47 #include <rdma/ib_umem.h>
48 #include <rdma/ib_addr.h>
49 #include <rdma/ib_mad.h>
50 #include <rdma/ib_cache.h>
51 #include <rdma/uverbs_ioctl.h>
56 #include "qplib_res.h"
59 #include "qplib_rcfw.h"
63 #include <rdma/bnxt_re-abi.h>
65 static int __from_ib_access_flags(int iflags)
69 if (iflags & IB_ACCESS_LOCAL_WRITE)
70 qflags |= BNXT_QPLIB_ACCESS_LOCAL_WRITE;
71 if (iflags & IB_ACCESS_REMOTE_READ)
72 qflags |= BNXT_QPLIB_ACCESS_REMOTE_READ;
73 if (iflags & IB_ACCESS_REMOTE_WRITE)
74 qflags |= BNXT_QPLIB_ACCESS_REMOTE_WRITE;
75 if (iflags & IB_ACCESS_REMOTE_ATOMIC)
76 qflags |= BNXT_QPLIB_ACCESS_REMOTE_ATOMIC;
77 if (iflags & IB_ACCESS_MW_BIND)
78 qflags |= BNXT_QPLIB_ACCESS_MW_BIND;
79 if (iflags & IB_ZERO_BASED)
80 qflags |= BNXT_QPLIB_ACCESS_ZERO_BASED;
81 if (iflags & IB_ACCESS_ON_DEMAND)
82 qflags |= BNXT_QPLIB_ACCESS_ON_DEMAND;
86 static enum ib_access_flags __to_ib_access_flags(int qflags)
88 enum ib_access_flags iflags = 0;
90 if (qflags & BNXT_QPLIB_ACCESS_LOCAL_WRITE)
91 iflags |= IB_ACCESS_LOCAL_WRITE;
92 if (qflags & BNXT_QPLIB_ACCESS_REMOTE_WRITE)
93 iflags |= IB_ACCESS_REMOTE_WRITE;
94 if (qflags & BNXT_QPLIB_ACCESS_REMOTE_READ)
95 iflags |= IB_ACCESS_REMOTE_READ;
96 if (qflags & BNXT_QPLIB_ACCESS_REMOTE_ATOMIC)
97 iflags |= IB_ACCESS_REMOTE_ATOMIC;
98 if (qflags & BNXT_QPLIB_ACCESS_MW_BIND)
99 iflags |= IB_ACCESS_MW_BIND;
100 if (qflags & BNXT_QPLIB_ACCESS_ZERO_BASED)
101 iflags |= IB_ZERO_BASED;
102 if (qflags & BNXT_QPLIB_ACCESS_ON_DEMAND)
103 iflags |= IB_ACCESS_ON_DEMAND;
107 static int bnxt_re_build_sgl(struct ib_sge *ib_sg_list,
108 struct bnxt_qplib_sge *sg_list, int num)
112 for (i = 0; i < num; i++) {
113 sg_list[i].addr = ib_sg_list[i].addr;
114 sg_list[i].lkey = ib_sg_list[i].lkey;
115 sg_list[i].size = ib_sg_list[i].length;
116 total += sg_list[i].size;
122 int bnxt_re_query_device(struct ib_device *ibdev,
123 struct ib_device_attr *ib_attr,
124 struct ib_udata *udata)
126 struct bnxt_re_dev *rdev = to_bnxt_re_dev(ibdev, ibdev);
127 struct bnxt_qplib_dev_attr *dev_attr = &rdev->dev_attr;
129 memset(ib_attr, 0, sizeof(*ib_attr));
130 memcpy(&ib_attr->fw_ver, dev_attr->fw_ver,
131 min(sizeof(dev_attr->fw_ver),
132 sizeof(ib_attr->fw_ver)));
133 bnxt_qplib_get_guid(rdev->netdev->dev_addr,
134 (u8 *)&ib_attr->sys_image_guid);
135 ib_attr->max_mr_size = BNXT_RE_MAX_MR_SIZE;
136 ib_attr->page_size_cap = BNXT_RE_PAGE_SIZE_4K | BNXT_RE_PAGE_SIZE_2M;
138 ib_attr->vendor_id = rdev->en_dev->pdev->vendor;
139 ib_attr->vendor_part_id = rdev->en_dev->pdev->device;
140 ib_attr->hw_ver = rdev->en_dev->pdev->subsystem_device;
141 ib_attr->max_qp = dev_attr->max_qp;
142 ib_attr->max_qp_wr = dev_attr->max_qp_wqes;
143 ib_attr->device_cap_flags =
144 IB_DEVICE_CURR_QP_STATE_MOD
145 | IB_DEVICE_RC_RNR_NAK_GEN
146 | IB_DEVICE_SHUTDOWN_PORT
147 | IB_DEVICE_SYS_IMAGE_GUID
148 | IB_DEVICE_LOCAL_DMA_LKEY
149 | IB_DEVICE_RESIZE_MAX_WR
150 | IB_DEVICE_PORT_ACTIVE_EVENT
151 | IB_DEVICE_N_NOTIFY_CQ
152 | IB_DEVICE_MEM_WINDOW
153 | IB_DEVICE_MEM_WINDOW_TYPE_2B
154 | IB_DEVICE_MEM_MGT_EXTENSIONS;
155 ib_attr->max_send_sge = dev_attr->max_qp_sges;
156 ib_attr->max_recv_sge = dev_attr->max_qp_sges;
157 ib_attr->max_sge_rd = dev_attr->max_qp_sges;
158 ib_attr->max_cq = dev_attr->max_cq;
159 ib_attr->max_cqe = dev_attr->max_cq_wqes;
160 ib_attr->max_mr = dev_attr->max_mr;
161 ib_attr->max_pd = dev_attr->max_pd;
162 ib_attr->max_qp_rd_atom = dev_attr->max_qp_rd_atom;
163 ib_attr->max_qp_init_rd_atom = dev_attr->max_qp_init_rd_atom;
164 ib_attr->atomic_cap = IB_ATOMIC_NONE;
165 ib_attr->masked_atomic_cap = IB_ATOMIC_NONE;
167 ib_attr->max_ee_rd_atom = 0;
168 ib_attr->max_res_rd_atom = 0;
169 ib_attr->max_ee_init_rd_atom = 0;
171 ib_attr->max_rdd = 0;
172 ib_attr->max_mw = dev_attr->max_mw;
173 ib_attr->max_raw_ipv6_qp = 0;
174 ib_attr->max_raw_ethy_qp = dev_attr->max_raw_ethy_qp;
175 ib_attr->max_mcast_grp = 0;
176 ib_attr->max_mcast_qp_attach = 0;
177 ib_attr->max_total_mcast_qp_attach = 0;
178 ib_attr->max_ah = dev_attr->max_ah;
180 ib_attr->max_fmr = 0;
181 ib_attr->max_map_per_fmr = 0;
183 ib_attr->max_srq = dev_attr->max_srq;
184 ib_attr->max_srq_wr = dev_attr->max_srq_wqes;
185 ib_attr->max_srq_sge = dev_attr->max_srq_sges;
187 ib_attr->max_fast_reg_page_list_len = MAX_PBL_LVL_1_PGS;
189 ib_attr->max_pkeys = 1;
190 ib_attr->local_ca_ack_delay = BNXT_RE_DEFAULT_ACK_DELAY;
194 int bnxt_re_modify_device(struct ib_device *ibdev,
195 int device_modify_mask,
196 struct ib_device_modify *device_modify)
198 switch (device_modify_mask) {
199 case IB_DEVICE_MODIFY_SYS_IMAGE_GUID:
200 /* Modify the GUID requires the modification of the GID table */
201 /* GUID should be made as READ-ONLY */
203 case IB_DEVICE_MODIFY_NODE_DESC:
204 /* Node Desc should be made as READ-ONLY */
213 int bnxt_re_query_port(struct ib_device *ibdev, u8 port_num,
214 struct ib_port_attr *port_attr)
216 struct bnxt_re_dev *rdev = to_bnxt_re_dev(ibdev, ibdev);
217 struct bnxt_qplib_dev_attr *dev_attr = &rdev->dev_attr;
219 memset(port_attr, 0, sizeof(*port_attr));
221 if (netif_running(rdev->netdev) && netif_carrier_ok(rdev->netdev)) {
222 port_attr->state = IB_PORT_ACTIVE;
223 port_attr->phys_state = 5;
225 port_attr->state = IB_PORT_DOWN;
226 port_attr->phys_state = 3;
228 port_attr->max_mtu = IB_MTU_4096;
229 port_attr->active_mtu = iboe_get_mtu(rdev->netdev->mtu);
230 port_attr->gid_tbl_len = dev_attr->max_sgid;
231 port_attr->port_cap_flags = IB_PORT_CM_SUP | IB_PORT_REINIT_SUP |
232 IB_PORT_DEVICE_MGMT_SUP |
233 IB_PORT_VENDOR_CLASS_SUP;
234 port_attr->ip_gids = true;
236 port_attr->max_msg_sz = (u32)BNXT_RE_MAX_MR_SIZE_LOW;
237 port_attr->bad_pkey_cntr = 0;
238 port_attr->qkey_viol_cntr = 0;
239 port_attr->pkey_tbl_len = dev_attr->max_pkey;
241 port_attr->sm_lid = 0;
243 port_attr->max_vl_num = 4;
244 port_attr->sm_sl = 0;
245 port_attr->subnet_timeout = 0;
246 port_attr->init_type_reply = 0;
247 port_attr->active_speed = rdev->active_speed;
248 port_attr->active_width = rdev->active_width;
253 int bnxt_re_get_port_immutable(struct ib_device *ibdev, u8 port_num,
254 struct ib_port_immutable *immutable)
256 struct ib_port_attr port_attr;
258 if (bnxt_re_query_port(ibdev, port_num, &port_attr))
261 immutable->pkey_tbl_len = port_attr.pkey_tbl_len;
262 immutable->gid_tbl_len = port_attr.gid_tbl_len;
263 immutable->core_cap_flags = RDMA_CORE_PORT_IBA_ROCE;
264 immutable->core_cap_flags |= RDMA_CORE_CAP_PROT_ROCE_UDP_ENCAP;
265 immutable->max_mad_size = IB_MGMT_MAD_SIZE;
269 void bnxt_re_query_fw_str(struct ib_device *ibdev, char *str)
271 struct bnxt_re_dev *rdev = to_bnxt_re_dev(ibdev, ibdev);
273 snprintf(str, IB_FW_VERSION_NAME_MAX, "%d.%d.%d.%d",
274 rdev->dev_attr.fw_ver[0], rdev->dev_attr.fw_ver[1],
275 rdev->dev_attr.fw_ver[2], rdev->dev_attr.fw_ver[3]);
278 int bnxt_re_query_pkey(struct ib_device *ibdev, u8 port_num,
279 u16 index, u16 *pkey)
281 struct bnxt_re_dev *rdev = to_bnxt_re_dev(ibdev, ibdev);
283 /* Ignore port_num */
285 memset(pkey, 0, sizeof(*pkey));
286 return bnxt_qplib_get_pkey(&rdev->qplib_res,
287 &rdev->qplib_res.pkey_tbl, index, pkey);
290 int bnxt_re_query_gid(struct ib_device *ibdev, u8 port_num,
291 int index, union ib_gid *gid)
293 struct bnxt_re_dev *rdev = to_bnxt_re_dev(ibdev, ibdev);
296 /* Ignore port_num */
297 memset(gid, 0, sizeof(*gid));
298 rc = bnxt_qplib_get_sgid(&rdev->qplib_res,
299 &rdev->qplib_res.sgid_tbl, index,
300 (struct bnxt_qplib_gid *)gid);
304 int bnxt_re_del_gid(const struct ib_gid_attr *attr, void **context)
307 struct bnxt_re_gid_ctx *ctx, **ctx_tbl;
308 struct bnxt_re_dev *rdev = to_bnxt_re_dev(attr->device, ibdev);
309 struct bnxt_qplib_sgid_tbl *sgid_tbl = &rdev->qplib_res.sgid_tbl;
310 struct bnxt_qplib_gid *gid_to_del;
312 /* Delete the entry from the hardware */
317 if (sgid_tbl && sgid_tbl->active) {
318 if (ctx->idx >= sgid_tbl->max)
320 gid_to_del = &sgid_tbl->tbl[ctx->idx];
321 /* DEL_GID is called in WQ context(netdevice_event_work_handler)
322 * or via the ib_unregister_device path. In the former case QP1
323 * may not be destroyed yet, in which case just return as FW
324 * needs that entry to be present and will fail it's deletion.
325 * We could get invoked again after QP1 is destroyed OR get an
326 * ADD_GID call with a different GID value for the same index
327 * where we issue MODIFY_GID cmd to update the GID entry -- TBD
330 rdma_link_local_addr((struct in6_addr *)gid_to_del) &&
331 ctx->refcnt == 1 && rdev->qp1_sqp) {
332 dev_dbg(rdev_to_dev(rdev),
333 "Trying to delete GID0 while QP1 is alive\n");
338 rc = bnxt_qplib_del_sgid(sgid_tbl, gid_to_del, true);
340 dev_err(rdev_to_dev(rdev),
341 "Failed to remove GID: %#x", rc);
343 ctx_tbl = sgid_tbl->ctx;
344 ctx_tbl[ctx->idx] = NULL;
354 int bnxt_re_add_gid(const struct ib_gid_attr *attr, void **context)
358 u16 vlan_id = 0xFFFF;
359 struct bnxt_re_gid_ctx *ctx, **ctx_tbl;
360 struct bnxt_re_dev *rdev = to_bnxt_re_dev(attr->device, ibdev);
361 struct bnxt_qplib_sgid_tbl *sgid_tbl = &rdev->qplib_res.sgid_tbl;
363 rc = rdma_read_gid_l2_fields(attr, &vlan_id, NULL);
367 rc = bnxt_qplib_add_sgid(sgid_tbl, (struct bnxt_qplib_gid *)&attr->gid,
368 rdev->qplib_res.netdev->dev_addr,
369 vlan_id, true, &tbl_idx);
370 if (rc == -EALREADY) {
371 ctx_tbl = sgid_tbl->ctx;
372 ctx_tbl[tbl_idx]->refcnt++;
373 *context = ctx_tbl[tbl_idx];
378 dev_err(rdev_to_dev(rdev), "Failed to add GID: %#x", rc);
382 ctx = kmalloc(sizeof(*ctx), GFP_KERNEL);
385 ctx_tbl = sgid_tbl->ctx;
388 ctx_tbl[tbl_idx] = ctx;
394 enum rdma_link_layer bnxt_re_get_link_layer(struct ib_device *ibdev,
397 return IB_LINK_LAYER_ETHERNET;
400 #define BNXT_RE_FENCE_PBL_SIZE DIV_ROUND_UP(BNXT_RE_FENCE_BYTES, PAGE_SIZE)
402 static void bnxt_re_create_fence_wqe(struct bnxt_re_pd *pd)
404 struct bnxt_re_fence_data *fence = &pd->fence;
405 struct ib_mr *ib_mr = &fence->mr->ib_mr;
406 struct bnxt_qplib_swqe *wqe = &fence->bind_wqe;
408 memset(wqe, 0, sizeof(*wqe));
409 wqe->type = BNXT_QPLIB_SWQE_TYPE_BIND_MW;
410 wqe->wr_id = BNXT_QPLIB_FENCE_WRID;
411 wqe->flags |= BNXT_QPLIB_SWQE_FLAGS_SIGNAL_COMP;
412 wqe->flags |= BNXT_QPLIB_SWQE_FLAGS_UC_FENCE;
413 wqe->bind.zero_based = false;
414 wqe->bind.parent_l_key = ib_mr->lkey;
415 wqe->bind.va = (u64)(unsigned long)fence->va;
416 wqe->bind.length = fence->size;
417 wqe->bind.access_cntl = __from_ib_access_flags(IB_ACCESS_REMOTE_READ);
418 wqe->bind.mw_type = SQ_BIND_MW_TYPE_TYPE1;
420 /* Save the initial rkey in fence structure for now;
421 * wqe->bind.r_key will be set at (re)bind time.
423 fence->bind_rkey = ib_inc_rkey(fence->mw->rkey);
426 static int bnxt_re_bind_fence_mw(struct bnxt_qplib_qp *qplib_qp)
428 struct bnxt_re_qp *qp = container_of(qplib_qp, struct bnxt_re_qp,
430 struct ib_pd *ib_pd = qp->ib_qp.pd;
431 struct bnxt_re_pd *pd = container_of(ib_pd, struct bnxt_re_pd, ib_pd);
432 struct bnxt_re_fence_data *fence = &pd->fence;
433 struct bnxt_qplib_swqe *fence_wqe = &fence->bind_wqe;
434 struct bnxt_qplib_swqe wqe;
437 memcpy(&wqe, fence_wqe, sizeof(wqe));
438 wqe.bind.r_key = fence->bind_rkey;
439 fence->bind_rkey = ib_inc_rkey(fence->bind_rkey);
441 dev_dbg(rdev_to_dev(qp->rdev),
442 "Posting bind fence-WQE: rkey: %#x QP: %d PD: %p\n",
443 wqe.bind.r_key, qp->qplib_qp.id, pd);
444 rc = bnxt_qplib_post_send(&qp->qplib_qp, &wqe);
446 dev_err(rdev_to_dev(qp->rdev), "Failed to bind fence-WQE\n");
449 bnxt_qplib_post_send_db(&qp->qplib_qp);
454 static void bnxt_re_destroy_fence_mr(struct bnxt_re_pd *pd)
456 struct bnxt_re_fence_data *fence = &pd->fence;
457 struct bnxt_re_dev *rdev = pd->rdev;
458 struct device *dev = &rdev->en_dev->pdev->dev;
459 struct bnxt_re_mr *mr = fence->mr;
462 bnxt_re_dealloc_mw(fence->mw);
467 bnxt_qplib_dereg_mrw(&rdev->qplib_res, &mr->qplib_mr,
470 bnxt_qplib_free_mrw(&rdev->qplib_res, &mr->qplib_mr);
474 if (fence->dma_addr) {
475 dma_unmap_single(dev, fence->dma_addr, BNXT_RE_FENCE_BYTES,
481 static int bnxt_re_create_fence_mr(struct bnxt_re_pd *pd)
483 int mr_access_flags = IB_ACCESS_LOCAL_WRITE | IB_ACCESS_MW_BIND;
484 struct bnxt_re_fence_data *fence = &pd->fence;
485 struct bnxt_re_dev *rdev = pd->rdev;
486 struct device *dev = &rdev->en_dev->pdev->dev;
487 struct bnxt_re_mr *mr = NULL;
488 dma_addr_t dma_addr = 0;
493 dma_addr = dma_map_single(dev, fence->va, BNXT_RE_FENCE_BYTES,
495 rc = dma_mapping_error(dev, dma_addr);
497 dev_err(rdev_to_dev(rdev), "Failed to dma-map fence-MR-mem\n");
502 fence->dma_addr = dma_addr;
505 mr = kzalloc(sizeof(*mr), GFP_KERNEL);
512 mr->qplib_mr.pd = &pd->qplib_pd;
513 mr->qplib_mr.type = CMDQ_ALLOCATE_MRW_MRW_FLAGS_PMR;
514 mr->qplib_mr.flags = __from_ib_access_flags(mr_access_flags);
515 rc = bnxt_qplib_alloc_mrw(&rdev->qplib_res, &mr->qplib_mr);
517 dev_err(rdev_to_dev(rdev), "Failed to alloc fence-HW-MR\n");
522 mr->ib_mr.lkey = mr->qplib_mr.lkey;
523 mr->qplib_mr.va = (u64)(unsigned long)fence->va;
524 mr->qplib_mr.total_size = BNXT_RE_FENCE_BYTES;
526 rc = bnxt_qplib_reg_mr(&rdev->qplib_res, &mr->qplib_mr, &pbl_tbl,
527 BNXT_RE_FENCE_PBL_SIZE, false, PAGE_SIZE);
529 dev_err(rdev_to_dev(rdev), "Failed to register fence-MR\n");
532 mr->ib_mr.rkey = mr->qplib_mr.rkey;
534 /* Create a fence MW only for kernel consumers */
535 mw = bnxt_re_alloc_mw(&pd->ib_pd, IB_MW_TYPE_1, NULL);
537 dev_err(rdev_to_dev(rdev),
538 "Failed to create fence-MW for PD: %p\n", pd);
544 bnxt_re_create_fence_wqe(pd);
548 bnxt_re_destroy_fence_mr(pd);
552 /* Protection Domains */
553 void bnxt_re_dealloc_pd(struct ib_pd *ib_pd, struct ib_udata *udata)
555 struct bnxt_re_pd *pd = container_of(ib_pd, struct bnxt_re_pd, ib_pd);
556 struct bnxt_re_dev *rdev = pd->rdev;
558 bnxt_re_destroy_fence_mr(pd);
561 bnxt_qplib_dealloc_pd(&rdev->qplib_res, &rdev->qplib_res.pd_tbl,
565 int bnxt_re_alloc_pd(struct ib_pd *ibpd, struct ib_udata *udata)
567 struct ib_device *ibdev = ibpd->device;
568 struct bnxt_re_dev *rdev = to_bnxt_re_dev(ibdev, ibdev);
569 struct bnxt_re_ucontext *ucntx = rdma_udata_to_drv_context(
570 udata, struct bnxt_re_ucontext, ib_uctx);
571 struct bnxt_re_pd *pd = container_of(ibpd, struct bnxt_re_pd, ib_pd);
575 if (bnxt_qplib_alloc_pd(&rdev->qplib_res.pd_tbl, &pd->qplib_pd)) {
576 dev_err(rdev_to_dev(rdev), "Failed to allocate HW PD");
582 struct bnxt_re_pd_resp resp;
584 if (!ucntx->dpi.dbr) {
585 /* Allocate DPI in alloc_pd to avoid failing of
586 * ibv_devinfo and family of application when DPIs
589 if (bnxt_qplib_alloc_dpi(&rdev->qplib_res.dpi_tbl,
590 &ucntx->dpi, ucntx)) {
596 resp.pdid = pd->qplib_pd.id;
597 /* Still allow mapping this DBR to the new user PD. */
598 resp.dpi = ucntx->dpi.dpi;
599 resp.dbr = (u64)ucntx->dpi.umdbr;
601 rc = ib_copy_to_udata(udata, &resp, sizeof(resp));
603 dev_err(rdev_to_dev(rdev),
604 "Failed to copy user response\n");
610 if (bnxt_re_create_fence_mr(pd))
611 dev_warn(rdev_to_dev(rdev),
612 "Failed to create Fence-MR\n");
615 bnxt_qplib_dealloc_pd(&rdev->qplib_res, &rdev->qplib_res.pd_tbl,
621 /* Address Handles */
622 void bnxt_re_destroy_ah(struct ib_ah *ib_ah, u32 flags)
624 struct bnxt_re_ah *ah = container_of(ib_ah, struct bnxt_re_ah, ib_ah);
625 struct bnxt_re_dev *rdev = ah->rdev;
627 bnxt_qplib_destroy_ah(&rdev->qplib_res, &ah->qplib_ah,
628 !(flags & RDMA_DESTROY_AH_SLEEPABLE));
631 static u8 bnxt_re_stack_to_dev_nw_type(enum rdma_network_type ntype)
636 case RDMA_NETWORK_IPV4:
637 nw_type = CMDQ_CREATE_AH_TYPE_V2IPV4;
639 case RDMA_NETWORK_IPV6:
640 nw_type = CMDQ_CREATE_AH_TYPE_V2IPV6;
643 nw_type = CMDQ_CREATE_AH_TYPE_V1;
649 int bnxt_re_create_ah(struct ib_ah *ib_ah, struct rdma_ah_attr *ah_attr,
650 u32 flags, struct ib_udata *udata)
652 struct ib_pd *ib_pd = ib_ah->pd;
653 struct bnxt_re_pd *pd = container_of(ib_pd, struct bnxt_re_pd, ib_pd);
654 const struct ib_global_route *grh = rdma_ah_read_grh(ah_attr);
655 struct bnxt_re_dev *rdev = pd->rdev;
656 const struct ib_gid_attr *sgid_attr;
657 struct bnxt_re_ah *ah = container_of(ib_ah, struct bnxt_re_ah, ib_ah);
661 if (!(rdma_ah_get_ah_flags(ah_attr) & IB_AH_GRH)) {
662 dev_err(rdev_to_dev(rdev), "Failed to alloc AH: GRH not set");
667 ah->qplib_ah.pd = &pd->qplib_pd;
669 /* Supply the configuration for the HW */
670 memcpy(ah->qplib_ah.dgid.data, grh->dgid.raw,
671 sizeof(union ib_gid));
673 * If RoCE V2 is enabled, stack will have two entries for
674 * each GID entry. Avoiding this duplicte entry in HW. Dividing
675 * the GID index by 2 for RoCE V2
677 ah->qplib_ah.sgid_index = grh->sgid_index / 2;
678 ah->qplib_ah.host_sgid_index = grh->sgid_index;
679 ah->qplib_ah.traffic_class = grh->traffic_class;
680 ah->qplib_ah.flow_label = grh->flow_label;
681 ah->qplib_ah.hop_limit = grh->hop_limit;
682 ah->qplib_ah.sl = rdma_ah_get_sl(ah_attr);
684 sgid_attr = grh->sgid_attr;
685 /* Get network header type for this GID */
686 nw_type = rdma_gid_attr_network_type(sgid_attr);
687 ah->qplib_ah.nw_type = bnxt_re_stack_to_dev_nw_type(nw_type);
689 memcpy(ah->qplib_ah.dmac, ah_attr->roce.dmac, ETH_ALEN);
690 rc = bnxt_qplib_create_ah(&rdev->qplib_res, &ah->qplib_ah,
691 !(flags & RDMA_CREATE_AH_SLEEPABLE));
693 dev_err(rdev_to_dev(rdev), "Failed to allocate HW AH");
697 /* Write AVID to shared page. */
699 struct bnxt_re_ucontext *uctx = rdma_udata_to_drv_context(
700 udata, struct bnxt_re_ucontext, ib_uctx);
704 spin_lock_irqsave(&uctx->sh_lock, flag);
705 wrptr = (u32 *)(uctx->shpg + BNXT_RE_AVID_OFFT);
706 *wrptr = ah->qplib_ah.id;
707 wmb(); /* make sure cache is updated. */
708 spin_unlock_irqrestore(&uctx->sh_lock, flag);
714 int bnxt_re_modify_ah(struct ib_ah *ib_ah, struct rdma_ah_attr *ah_attr)
719 int bnxt_re_query_ah(struct ib_ah *ib_ah, struct rdma_ah_attr *ah_attr)
721 struct bnxt_re_ah *ah = container_of(ib_ah, struct bnxt_re_ah, ib_ah);
723 ah_attr->type = ib_ah->type;
724 rdma_ah_set_sl(ah_attr, ah->qplib_ah.sl);
725 memcpy(ah_attr->roce.dmac, ah->qplib_ah.dmac, ETH_ALEN);
726 rdma_ah_set_grh(ah_attr, NULL, 0,
727 ah->qplib_ah.host_sgid_index,
728 0, ah->qplib_ah.traffic_class);
729 rdma_ah_set_dgid_raw(ah_attr, ah->qplib_ah.dgid.data);
730 rdma_ah_set_port_num(ah_attr, 1);
731 rdma_ah_set_static_rate(ah_attr, 0);
735 unsigned long bnxt_re_lock_cqs(struct bnxt_re_qp *qp)
736 __acquires(&qp->scq->cq_lock) __acquires(&qp->rcq->cq_lock)
740 spin_lock_irqsave(&qp->scq->cq_lock, flags);
741 if (qp->rcq != qp->scq)
742 spin_lock(&qp->rcq->cq_lock);
744 __acquire(&qp->rcq->cq_lock);
749 void bnxt_re_unlock_cqs(struct bnxt_re_qp *qp,
751 __releases(&qp->scq->cq_lock) __releases(&qp->rcq->cq_lock)
753 if (qp->rcq != qp->scq)
754 spin_unlock(&qp->rcq->cq_lock);
756 __release(&qp->rcq->cq_lock);
757 spin_unlock_irqrestore(&qp->scq->cq_lock, flags);
761 int bnxt_re_destroy_qp(struct ib_qp *ib_qp, struct ib_udata *udata)
763 struct bnxt_re_qp *qp = container_of(ib_qp, struct bnxt_re_qp, ib_qp);
764 struct bnxt_re_dev *rdev = qp->rdev;
768 bnxt_qplib_flush_cqn_wq(&qp->qplib_qp);
769 rc = bnxt_qplib_destroy_qp(&rdev->qplib_res, &qp->qplib_qp);
771 dev_err(rdev_to_dev(rdev), "Failed to destroy HW QP");
775 if (rdma_is_kernel_res(&qp->ib_qp.res)) {
776 flags = bnxt_re_lock_cqs(qp);
777 bnxt_qplib_clean_qp(&qp->qplib_qp);
778 bnxt_re_unlock_cqs(qp, flags);
781 bnxt_qplib_free_qp_res(&rdev->qplib_res, &qp->qplib_qp);
783 if (ib_qp->qp_type == IB_QPT_GSI && rdev->qp1_sqp) {
784 bnxt_qplib_destroy_ah(&rdev->qplib_res, &rdev->sqp_ah->qplib_ah,
787 bnxt_qplib_clean_qp(&qp->qplib_qp);
788 rc = bnxt_qplib_destroy_qp(&rdev->qplib_res,
789 &rdev->qp1_sqp->qplib_qp);
791 dev_err(rdev_to_dev(rdev),
792 "Failed to destroy Shadow QP");
795 bnxt_qplib_free_qp_res(&rdev->qplib_res,
796 &rdev->qp1_sqp->qplib_qp);
797 mutex_lock(&rdev->qp_lock);
798 list_del(&rdev->qp1_sqp->list);
799 atomic_dec(&rdev->qp_count);
800 mutex_unlock(&rdev->qp_lock);
803 kfree(rdev->qp1_sqp);
804 rdev->qp1_sqp = NULL;
808 if (!IS_ERR_OR_NULL(qp->rumem))
809 ib_umem_release(qp->rumem);
810 if (!IS_ERR_OR_NULL(qp->sumem))
811 ib_umem_release(qp->sumem);
813 mutex_lock(&rdev->qp_lock);
815 atomic_dec(&rdev->qp_count);
816 mutex_unlock(&rdev->qp_lock);
821 static u8 __from_ib_qp_type(enum ib_qp_type type)
825 return CMDQ_CREATE_QP1_TYPE_GSI;
827 return CMDQ_CREATE_QP_TYPE_RC;
829 return CMDQ_CREATE_QP_TYPE_UD;
835 static int bnxt_re_init_user_qp(struct bnxt_re_dev *rdev, struct bnxt_re_pd *pd,
836 struct bnxt_re_qp *qp, struct ib_udata *udata)
838 struct bnxt_re_qp_req ureq;
839 struct bnxt_qplib_qp *qplib_qp = &qp->qplib_qp;
840 struct ib_umem *umem;
841 int bytes = 0, psn_sz;
842 struct bnxt_re_ucontext *cntx = rdma_udata_to_drv_context(
843 udata, struct bnxt_re_ucontext, ib_uctx);
845 if (ib_copy_from_udata(&ureq, udata, sizeof(ureq)))
848 bytes = (qplib_qp->sq.max_wqe * BNXT_QPLIB_MAX_SQE_ENTRY_SIZE);
849 /* Consider mapping PSN search memory only for RC QPs. */
850 if (qplib_qp->type == CMDQ_CREATE_QP_TYPE_RC) {
851 psn_sz = bnxt_qplib_is_chip_gen_p5(&rdev->chip_ctx) ?
852 sizeof(struct sq_psn_search_ext) :
853 sizeof(struct sq_psn_search);
854 bytes += (qplib_qp->sq.max_wqe * psn_sz);
856 bytes = PAGE_ALIGN(bytes);
857 umem = ib_umem_get(udata, ureq.qpsva, bytes, IB_ACCESS_LOCAL_WRITE, 1);
859 return PTR_ERR(umem);
862 qplib_qp->sq.sg_info.sglist = umem->sg_head.sgl;
863 qplib_qp->sq.sg_info.npages = ib_umem_num_pages(umem);
864 qplib_qp->sq.sg_info.nmap = umem->nmap;
865 qplib_qp->qp_handle = ureq.qp_handle;
867 if (!qp->qplib_qp.srq) {
868 bytes = (qplib_qp->rq.max_wqe * BNXT_QPLIB_MAX_RQE_ENTRY_SIZE);
869 bytes = PAGE_ALIGN(bytes);
870 umem = ib_umem_get(udata, ureq.qprva, bytes,
871 IB_ACCESS_LOCAL_WRITE, 1);
875 qplib_qp->rq.sg_info.sglist = umem->sg_head.sgl;
876 qplib_qp->rq.sg_info.npages = ib_umem_num_pages(umem);
877 qplib_qp->rq.sg_info.nmap = umem->nmap;
880 qplib_qp->dpi = &cntx->dpi;
883 ib_umem_release(qp->sumem);
885 memset(&qplib_qp->sq.sg_info, 0, sizeof(qplib_qp->sq.sg_info));
887 return PTR_ERR(umem);
890 static struct bnxt_re_ah *bnxt_re_create_shadow_qp_ah
891 (struct bnxt_re_pd *pd,
892 struct bnxt_qplib_res *qp1_res,
893 struct bnxt_qplib_qp *qp1_qp)
895 struct bnxt_re_dev *rdev = pd->rdev;
896 struct bnxt_re_ah *ah;
900 ah = kzalloc(sizeof(*ah), GFP_KERNEL);
905 ah->qplib_ah.pd = &pd->qplib_pd;
907 rc = bnxt_re_query_gid(&rdev->ibdev, 1, 0, &sgid);
911 /* supply the dgid data same as sgid */
912 memcpy(ah->qplib_ah.dgid.data, &sgid.raw,
913 sizeof(union ib_gid));
914 ah->qplib_ah.sgid_index = 0;
916 ah->qplib_ah.traffic_class = 0;
917 ah->qplib_ah.flow_label = 0;
918 ah->qplib_ah.hop_limit = 1;
920 /* Have DMAC same as SMAC */
921 ether_addr_copy(ah->qplib_ah.dmac, rdev->netdev->dev_addr);
923 rc = bnxt_qplib_create_ah(&rdev->qplib_res, &ah->qplib_ah, false);
925 dev_err(rdev_to_dev(rdev),
926 "Failed to allocate HW AH for Shadow QP");
937 static struct bnxt_re_qp *bnxt_re_create_shadow_qp
938 (struct bnxt_re_pd *pd,
939 struct bnxt_qplib_res *qp1_res,
940 struct bnxt_qplib_qp *qp1_qp)
942 struct bnxt_re_dev *rdev = pd->rdev;
943 struct bnxt_re_qp *qp;
946 qp = kzalloc(sizeof(*qp), GFP_KERNEL);
952 /* Initialize the shadow QP structure from the QP1 values */
953 ether_addr_copy(qp->qplib_qp.smac, rdev->netdev->dev_addr);
955 qp->qplib_qp.pd = &pd->qplib_pd;
956 qp->qplib_qp.qp_handle = (u64)(unsigned long)(&qp->qplib_qp);
957 qp->qplib_qp.type = IB_QPT_UD;
959 qp->qplib_qp.max_inline_data = 0;
960 qp->qplib_qp.sig_type = true;
962 /* Shadow QP SQ depth should be same as QP1 RQ depth */
963 qp->qplib_qp.sq.max_wqe = qp1_qp->rq.max_wqe;
964 qp->qplib_qp.sq.max_sge = 2;
965 /* Q full delta can be 1 since it is internal QP */
966 qp->qplib_qp.sq.q_full_delta = 1;
968 qp->qplib_qp.scq = qp1_qp->scq;
969 qp->qplib_qp.rcq = qp1_qp->rcq;
971 qp->qplib_qp.rq.max_wqe = qp1_qp->rq.max_wqe;
972 qp->qplib_qp.rq.max_sge = qp1_qp->rq.max_sge;
973 /* Q full delta can be 1 since it is internal QP */
974 qp->qplib_qp.rq.q_full_delta = 1;
976 qp->qplib_qp.mtu = qp1_qp->mtu;
978 qp->qplib_qp.sq_hdr_buf_size = 0;
979 qp->qplib_qp.rq_hdr_buf_size = BNXT_QPLIB_MAX_GRH_HDR_SIZE_IPV6;
980 qp->qplib_qp.dpi = &rdev->dpi_privileged;
982 rc = bnxt_qplib_create_qp(qp1_res, &qp->qplib_qp);
986 rdev->sqp_id = qp->qplib_qp.id;
988 spin_lock_init(&qp->sq_lock);
989 INIT_LIST_HEAD(&qp->list);
990 mutex_lock(&rdev->qp_lock);
991 list_add_tail(&qp->list, &rdev->qp_list);
992 atomic_inc(&rdev->qp_count);
993 mutex_unlock(&rdev->qp_lock);
1000 struct ib_qp *bnxt_re_create_qp(struct ib_pd *ib_pd,
1001 struct ib_qp_init_attr *qp_init_attr,
1002 struct ib_udata *udata)
1004 struct bnxt_re_pd *pd = container_of(ib_pd, struct bnxt_re_pd, ib_pd);
1005 struct bnxt_re_dev *rdev = pd->rdev;
1006 struct bnxt_qplib_dev_attr *dev_attr = &rdev->dev_attr;
1007 struct bnxt_re_qp *qp;
1008 struct bnxt_re_cq *cq;
1009 struct bnxt_re_srq *srq;
1012 if ((qp_init_attr->cap.max_send_wr > dev_attr->max_qp_wqes) ||
1013 (qp_init_attr->cap.max_recv_wr > dev_attr->max_qp_wqes) ||
1014 (qp_init_attr->cap.max_send_sge > dev_attr->max_qp_sges) ||
1015 (qp_init_attr->cap.max_recv_sge > dev_attr->max_qp_sges) ||
1016 (qp_init_attr->cap.max_inline_data > dev_attr->max_inline_data))
1017 return ERR_PTR(-EINVAL);
1019 qp = kzalloc(sizeof(*qp), GFP_KERNEL);
1021 return ERR_PTR(-ENOMEM);
1024 ether_addr_copy(qp->qplib_qp.smac, rdev->netdev->dev_addr);
1025 qp->qplib_qp.pd = &pd->qplib_pd;
1026 qp->qplib_qp.qp_handle = (u64)(unsigned long)(&qp->qplib_qp);
1027 qp->qplib_qp.type = __from_ib_qp_type(qp_init_attr->qp_type);
1029 if (qp_init_attr->qp_type == IB_QPT_GSI &&
1030 bnxt_qplib_is_chip_gen_p5(&rdev->chip_ctx))
1031 qp->qplib_qp.type = CMDQ_CREATE_QP_TYPE_GSI;
1032 if (qp->qplib_qp.type == IB_QPT_MAX) {
1033 dev_err(rdev_to_dev(rdev), "QP type 0x%x not supported",
1039 qp->qplib_qp.max_inline_data = qp_init_attr->cap.max_inline_data;
1040 qp->qplib_qp.sig_type = ((qp_init_attr->sq_sig_type ==
1041 IB_SIGNAL_ALL_WR) ? true : false);
1043 qp->qplib_qp.sq.max_sge = qp_init_attr->cap.max_send_sge;
1044 if (qp->qplib_qp.sq.max_sge > dev_attr->max_qp_sges)
1045 qp->qplib_qp.sq.max_sge = dev_attr->max_qp_sges;
1047 if (qp_init_attr->send_cq) {
1048 cq = container_of(qp_init_attr->send_cq, struct bnxt_re_cq,
1051 dev_err(rdev_to_dev(rdev), "Send CQ not found");
1055 qp->qplib_qp.scq = &cq->qplib_cq;
1059 if (qp_init_attr->recv_cq) {
1060 cq = container_of(qp_init_attr->recv_cq, struct bnxt_re_cq,
1063 dev_err(rdev_to_dev(rdev), "Receive CQ not found");
1067 qp->qplib_qp.rcq = &cq->qplib_cq;
1071 if (qp_init_attr->srq) {
1072 srq = container_of(qp_init_attr->srq, struct bnxt_re_srq,
1075 dev_err(rdev_to_dev(rdev), "SRQ not found");
1079 qp->qplib_qp.srq = &srq->qplib_srq;
1080 qp->qplib_qp.rq.max_wqe = 0;
1082 /* Allocate 1 more than what's provided so posting max doesn't
1085 entries = roundup_pow_of_two(qp_init_attr->cap.max_recv_wr + 1);
1086 qp->qplib_qp.rq.max_wqe = min_t(u32, entries,
1087 dev_attr->max_qp_wqes + 1);
1089 qp->qplib_qp.rq.q_full_delta = qp->qplib_qp.rq.max_wqe -
1090 qp_init_attr->cap.max_recv_wr;
1092 qp->qplib_qp.rq.max_sge = qp_init_attr->cap.max_recv_sge;
1093 if (qp->qplib_qp.rq.max_sge > dev_attr->max_qp_sges)
1094 qp->qplib_qp.rq.max_sge = dev_attr->max_qp_sges;
1097 qp->qplib_qp.mtu = ib_mtu_enum_to_int(iboe_get_mtu(rdev->netdev->mtu));
1099 if (qp_init_attr->qp_type == IB_QPT_GSI &&
1100 !(bnxt_qplib_is_chip_gen_p5(&rdev->chip_ctx))) {
1101 /* Allocate 1 more than what's provided */
1102 entries = roundup_pow_of_two(qp_init_attr->cap.max_send_wr + 1);
1103 qp->qplib_qp.sq.max_wqe = min_t(u32, entries,
1104 dev_attr->max_qp_wqes + 1);
1105 qp->qplib_qp.sq.q_full_delta = qp->qplib_qp.sq.max_wqe -
1106 qp_init_attr->cap.max_send_wr;
1107 qp->qplib_qp.rq.max_sge = dev_attr->max_qp_sges;
1108 if (qp->qplib_qp.rq.max_sge > dev_attr->max_qp_sges)
1109 qp->qplib_qp.rq.max_sge = dev_attr->max_qp_sges;
1110 qp->qplib_qp.sq.max_sge++;
1111 if (qp->qplib_qp.sq.max_sge > dev_attr->max_qp_sges)
1112 qp->qplib_qp.sq.max_sge = dev_attr->max_qp_sges;
1114 qp->qplib_qp.rq_hdr_buf_size =
1115 BNXT_QPLIB_MAX_QP1_RQ_HDR_SIZE_V2;
1117 qp->qplib_qp.sq_hdr_buf_size =
1118 BNXT_QPLIB_MAX_QP1_SQ_HDR_SIZE_V2;
1119 qp->qplib_qp.dpi = &rdev->dpi_privileged;
1120 rc = bnxt_qplib_create_qp1(&rdev->qplib_res, &qp->qplib_qp);
1122 dev_err(rdev_to_dev(rdev), "Failed to create HW QP1");
1125 /* Create a shadow QP to handle the QP1 traffic */
1126 rdev->qp1_sqp = bnxt_re_create_shadow_qp(pd, &rdev->qplib_res,
1128 if (!rdev->qp1_sqp) {
1130 dev_err(rdev_to_dev(rdev),
1131 "Failed to create Shadow QP for QP1");
1134 rdev->sqp_ah = bnxt_re_create_shadow_qp_ah(pd, &rdev->qplib_res,
1136 if (!rdev->sqp_ah) {
1137 bnxt_qplib_destroy_qp(&rdev->qplib_res,
1138 &rdev->qp1_sqp->qplib_qp);
1140 dev_err(rdev_to_dev(rdev),
1141 "Failed to create AH entry for ShadowQP");
1146 /* Allocate 128 + 1 more than what's provided */
1147 entries = roundup_pow_of_two(qp_init_attr->cap.max_send_wr +
1148 BNXT_QPLIB_RESERVED_QP_WRS + 1);
1149 qp->qplib_qp.sq.max_wqe = min_t(u32, entries,
1150 dev_attr->max_qp_wqes +
1151 BNXT_QPLIB_RESERVED_QP_WRS + 1);
1152 qp->qplib_qp.sq.q_full_delta = BNXT_QPLIB_RESERVED_QP_WRS + 1;
1155 * Reserving one slot for Phantom WQE. Application can
1156 * post one extra entry in this case. But allowing this to avoid
1157 * unexpected Queue full condition
1160 qp->qplib_qp.sq.q_full_delta -= 1;
1162 qp->qplib_qp.max_rd_atomic = dev_attr->max_qp_rd_atom;
1163 qp->qplib_qp.max_dest_rd_atomic = dev_attr->max_qp_init_rd_atom;
1165 rc = bnxt_re_init_user_qp(rdev, pd, qp, udata);
1169 qp->qplib_qp.dpi = &rdev->dpi_privileged;
1172 rc = bnxt_qplib_create_qp(&rdev->qplib_res, &qp->qplib_qp);
1174 dev_err(rdev_to_dev(rdev), "Failed to create HW QP");
1179 qp->ib_qp.qp_num = qp->qplib_qp.id;
1180 spin_lock_init(&qp->sq_lock);
1181 spin_lock_init(&qp->rq_lock);
1184 struct bnxt_re_qp_resp resp;
1186 resp.qpid = qp->ib_qp.qp_num;
1188 rc = ib_copy_to_udata(udata, &resp, sizeof(resp));
1190 dev_err(rdev_to_dev(rdev), "Failed to copy QP udata");
1194 INIT_LIST_HEAD(&qp->list);
1195 mutex_lock(&rdev->qp_lock);
1196 list_add_tail(&qp->list, &rdev->qp_list);
1197 atomic_inc(&rdev->qp_count);
1198 mutex_unlock(&rdev->qp_lock);
1202 bnxt_qplib_destroy_qp(&rdev->qplib_res, &qp->qplib_qp);
1206 ib_umem_release(qp->rumem);
1208 ib_umem_release(qp->sumem);
1215 static u8 __from_ib_qp_state(enum ib_qp_state state)
1219 return CMDQ_MODIFY_QP_NEW_STATE_RESET;
1221 return CMDQ_MODIFY_QP_NEW_STATE_INIT;
1223 return CMDQ_MODIFY_QP_NEW_STATE_RTR;
1225 return CMDQ_MODIFY_QP_NEW_STATE_RTS;
1227 return CMDQ_MODIFY_QP_NEW_STATE_SQD;
1229 return CMDQ_MODIFY_QP_NEW_STATE_SQE;
1232 return CMDQ_MODIFY_QP_NEW_STATE_ERR;
1236 static enum ib_qp_state __to_ib_qp_state(u8 state)
1239 case CMDQ_MODIFY_QP_NEW_STATE_RESET:
1240 return IB_QPS_RESET;
1241 case CMDQ_MODIFY_QP_NEW_STATE_INIT:
1243 case CMDQ_MODIFY_QP_NEW_STATE_RTR:
1245 case CMDQ_MODIFY_QP_NEW_STATE_RTS:
1247 case CMDQ_MODIFY_QP_NEW_STATE_SQD:
1249 case CMDQ_MODIFY_QP_NEW_STATE_SQE:
1251 case CMDQ_MODIFY_QP_NEW_STATE_ERR:
1257 static u32 __from_ib_mtu(enum ib_mtu mtu)
1261 return CMDQ_MODIFY_QP_PATH_MTU_MTU_256;
1263 return CMDQ_MODIFY_QP_PATH_MTU_MTU_512;
1265 return CMDQ_MODIFY_QP_PATH_MTU_MTU_1024;
1267 return CMDQ_MODIFY_QP_PATH_MTU_MTU_2048;
1269 return CMDQ_MODIFY_QP_PATH_MTU_MTU_4096;
1271 return CMDQ_MODIFY_QP_PATH_MTU_MTU_2048;
1275 static enum ib_mtu __to_ib_mtu(u32 mtu)
1277 switch (mtu & CREQ_QUERY_QP_RESP_SB_PATH_MTU_MASK) {
1278 case CMDQ_MODIFY_QP_PATH_MTU_MTU_256:
1280 case CMDQ_MODIFY_QP_PATH_MTU_MTU_512:
1282 case CMDQ_MODIFY_QP_PATH_MTU_MTU_1024:
1284 case CMDQ_MODIFY_QP_PATH_MTU_MTU_2048:
1286 case CMDQ_MODIFY_QP_PATH_MTU_MTU_4096:
1293 /* Shared Receive Queues */
1294 void bnxt_re_destroy_srq(struct ib_srq *ib_srq, struct ib_udata *udata)
1296 struct bnxt_re_srq *srq = container_of(ib_srq, struct bnxt_re_srq,
1298 struct bnxt_re_dev *rdev = srq->rdev;
1299 struct bnxt_qplib_srq *qplib_srq = &srq->qplib_srq;
1300 struct bnxt_qplib_nq *nq = NULL;
1303 nq = qplib_srq->cq->nq;
1304 bnxt_qplib_destroy_srq(&rdev->qplib_res, qplib_srq);
1306 ib_umem_release(srq->umem);
1307 atomic_dec(&rdev->srq_count);
1312 static int bnxt_re_init_user_srq(struct bnxt_re_dev *rdev,
1313 struct bnxt_re_pd *pd,
1314 struct bnxt_re_srq *srq,
1315 struct ib_udata *udata)
1317 struct bnxt_re_srq_req ureq;
1318 struct bnxt_qplib_srq *qplib_srq = &srq->qplib_srq;
1319 struct ib_umem *umem;
1321 struct bnxt_re_ucontext *cntx = rdma_udata_to_drv_context(
1322 udata, struct bnxt_re_ucontext, ib_uctx);
1324 if (ib_copy_from_udata(&ureq, udata, sizeof(ureq)))
1327 bytes = (qplib_srq->max_wqe * BNXT_QPLIB_MAX_RQE_ENTRY_SIZE);
1328 bytes = PAGE_ALIGN(bytes);
1329 umem = ib_umem_get(udata, ureq.srqva, bytes, IB_ACCESS_LOCAL_WRITE, 1);
1331 return PTR_ERR(umem);
1334 qplib_srq->sg_info.sglist = umem->sg_head.sgl;
1335 qplib_srq->sg_info.npages = ib_umem_num_pages(umem);
1336 qplib_srq->sg_info.nmap = umem->nmap;
1337 qplib_srq->srq_handle = ureq.srq_handle;
1338 qplib_srq->dpi = &cntx->dpi;
1343 int bnxt_re_create_srq(struct ib_srq *ib_srq,
1344 struct ib_srq_init_attr *srq_init_attr,
1345 struct ib_udata *udata)
1347 struct ib_pd *ib_pd = ib_srq->pd;
1348 struct bnxt_re_pd *pd = container_of(ib_pd, struct bnxt_re_pd, ib_pd);
1349 struct bnxt_re_dev *rdev = pd->rdev;
1350 struct bnxt_qplib_dev_attr *dev_attr = &rdev->dev_attr;
1351 struct bnxt_re_srq *srq =
1352 container_of(ib_srq, struct bnxt_re_srq, ib_srq);
1353 struct bnxt_qplib_nq *nq = NULL;
1356 if (srq_init_attr->attr.max_wr >= dev_attr->max_srq_wqes) {
1357 dev_err(rdev_to_dev(rdev), "Create CQ failed - max exceeded");
1362 if (srq_init_attr->srq_type != IB_SRQT_BASIC) {
1368 srq->qplib_srq.pd = &pd->qplib_pd;
1369 srq->qplib_srq.dpi = &rdev->dpi_privileged;
1370 /* Allocate 1 more than what's provided so posting max doesn't
1373 entries = roundup_pow_of_two(srq_init_attr->attr.max_wr + 1);
1374 if (entries > dev_attr->max_srq_wqes + 1)
1375 entries = dev_attr->max_srq_wqes + 1;
1377 srq->qplib_srq.max_wqe = entries;
1378 srq->qplib_srq.max_sge = srq_init_attr->attr.max_sge;
1379 srq->qplib_srq.threshold = srq_init_attr->attr.srq_limit;
1380 srq->srq_limit = srq_init_attr->attr.srq_limit;
1381 srq->qplib_srq.eventq_hw_ring_id = rdev->nq[0].ring_id;
1385 rc = bnxt_re_init_user_srq(rdev, pd, srq, udata);
1390 rc = bnxt_qplib_create_srq(&rdev->qplib_res, &srq->qplib_srq);
1392 dev_err(rdev_to_dev(rdev), "Create HW SRQ failed!");
1397 struct bnxt_re_srq_resp resp;
1399 resp.srqid = srq->qplib_srq.id;
1400 rc = ib_copy_to_udata(udata, &resp, sizeof(resp));
1402 dev_err(rdev_to_dev(rdev), "SRQ copy to udata failed!");
1403 bnxt_qplib_destroy_srq(&rdev->qplib_res,
1410 atomic_inc(&rdev->srq_count);
1416 ib_umem_release(srq->umem);
1421 int bnxt_re_modify_srq(struct ib_srq *ib_srq, struct ib_srq_attr *srq_attr,
1422 enum ib_srq_attr_mask srq_attr_mask,
1423 struct ib_udata *udata)
1425 struct bnxt_re_srq *srq = container_of(ib_srq, struct bnxt_re_srq,
1427 struct bnxt_re_dev *rdev = srq->rdev;
1430 switch (srq_attr_mask) {
1432 /* SRQ resize is not supported */
1435 /* Change the SRQ threshold */
1436 if (srq_attr->srq_limit > srq->qplib_srq.max_wqe)
1439 srq->qplib_srq.threshold = srq_attr->srq_limit;
1440 rc = bnxt_qplib_modify_srq(&rdev->qplib_res, &srq->qplib_srq);
1442 dev_err(rdev_to_dev(rdev), "Modify HW SRQ failed!");
1445 /* On success, update the shadow */
1446 srq->srq_limit = srq_attr->srq_limit;
1447 /* No need to Build and send response back to udata */
1450 dev_err(rdev_to_dev(rdev),
1451 "Unsupported srq_attr_mask 0x%x", srq_attr_mask);
1457 int bnxt_re_query_srq(struct ib_srq *ib_srq, struct ib_srq_attr *srq_attr)
1459 struct bnxt_re_srq *srq = container_of(ib_srq, struct bnxt_re_srq,
1461 struct bnxt_re_srq tsrq;
1462 struct bnxt_re_dev *rdev = srq->rdev;
1465 /* Get live SRQ attr */
1466 tsrq.qplib_srq.id = srq->qplib_srq.id;
1467 rc = bnxt_qplib_query_srq(&rdev->qplib_res, &tsrq.qplib_srq);
1469 dev_err(rdev_to_dev(rdev), "Query HW SRQ failed!");
1472 srq_attr->max_wr = srq->qplib_srq.max_wqe;
1473 srq_attr->max_sge = srq->qplib_srq.max_sge;
1474 srq_attr->srq_limit = tsrq.qplib_srq.threshold;
1479 int bnxt_re_post_srq_recv(struct ib_srq *ib_srq, const struct ib_recv_wr *wr,
1480 const struct ib_recv_wr **bad_wr)
1482 struct bnxt_re_srq *srq = container_of(ib_srq, struct bnxt_re_srq,
1484 struct bnxt_qplib_swqe wqe;
1485 unsigned long flags;
1488 spin_lock_irqsave(&srq->lock, flags);
1490 /* Transcribe each ib_recv_wr to qplib_swqe */
1491 wqe.num_sge = wr->num_sge;
1492 bnxt_re_build_sgl(wr->sg_list, wqe.sg_list, wr->num_sge);
1493 wqe.wr_id = wr->wr_id;
1494 wqe.type = BNXT_QPLIB_SWQE_TYPE_RECV;
1496 rc = bnxt_qplib_post_srq_recv(&srq->qplib_srq, &wqe);
1503 spin_unlock_irqrestore(&srq->lock, flags);
1507 static int bnxt_re_modify_shadow_qp(struct bnxt_re_dev *rdev,
1508 struct bnxt_re_qp *qp1_qp,
1511 struct bnxt_re_qp *qp = rdev->qp1_sqp;
1514 if (qp_attr_mask & IB_QP_STATE) {
1515 qp->qplib_qp.modify_flags |= CMDQ_MODIFY_QP_MODIFY_MASK_STATE;
1516 qp->qplib_qp.state = qp1_qp->qplib_qp.state;
1518 if (qp_attr_mask & IB_QP_PKEY_INDEX) {
1519 qp->qplib_qp.modify_flags |= CMDQ_MODIFY_QP_MODIFY_MASK_PKEY;
1520 qp->qplib_qp.pkey_index = qp1_qp->qplib_qp.pkey_index;
1523 if (qp_attr_mask & IB_QP_QKEY) {
1524 qp->qplib_qp.modify_flags |= CMDQ_MODIFY_QP_MODIFY_MASK_QKEY;
1525 /* Using a Random QKEY */
1526 qp->qplib_qp.qkey = 0x81818181;
1528 if (qp_attr_mask & IB_QP_SQ_PSN) {
1529 qp->qplib_qp.modify_flags |= CMDQ_MODIFY_QP_MODIFY_MASK_SQ_PSN;
1530 qp->qplib_qp.sq.psn = qp1_qp->qplib_qp.sq.psn;
1533 rc = bnxt_qplib_modify_qp(&rdev->qplib_res, &qp->qplib_qp);
1535 dev_err(rdev_to_dev(rdev),
1536 "Failed to modify Shadow QP for QP1");
1540 int bnxt_re_modify_qp(struct ib_qp *ib_qp, struct ib_qp_attr *qp_attr,
1541 int qp_attr_mask, struct ib_udata *udata)
1543 struct bnxt_re_qp *qp = container_of(ib_qp, struct bnxt_re_qp, ib_qp);
1544 struct bnxt_re_dev *rdev = qp->rdev;
1545 struct bnxt_qplib_dev_attr *dev_attr = &rdev->dev_attr;
1546 enum ib_qp_state curr_qp_state, new_qp_state;
1551 qp->qplib_qp.modify_flags = 0;
1552 if (qp_attr_mask & IB_QP_STATE) {
1553 curr_qp_state = __to_ib_qp_state(qp->qplib_qp.cur_qp_state);
1554 new_qp_state = qp_attr->qp_state;
1555 if (!ib_modify_qp_is_ok(curr_qp_state, new_qp_state,
1556 ib_qp->qp_type, qp_attr_mask)) {
1557 dev_err(rdev_to_dev(rdev),
1558 "Invalid attribute mask: %#x specified ",
1560 dev_err(rdev_to_dev(rdev),
1561 "for qpn: %#x type: %#x",
1562 ib_qp->qp_num, ib_qp->qp_type);
1563 dev_err(rdev_to_dev(rdev),
1564 "curr_qp_state=0x%x, new_qp_state=0x%x\n",
1565 curr_qp_state, new_qp_state);
1568 qp->qplib_qp.modify_flags |= CMDQ_MODIFY_QP_MODIFY_MASK_STATE;
1569 qp->qplib_qp.state = __from_ib_qp_state(qp_attr->qp_state);
1572 qp->qplib_qp.state == CMDQ_MODIFY_QP_NEW_STATE_ERR) {
1573 dev_dbg(rdev_to_dev(rdev),
1574 "Move QP = %p to flush list\n",
1576 flags = bnxt_re_lock_cqs(qp);
1577 bnxt_qplib_add_flush_qp(&qp->qplib_qp);
1578 bnxt_re_unlock_cqs(qp, flags);
1581 qp->qplib_qp.state == CMDQ_MODIFY_QP_NEW_STATE_RESET) {
1582 dev_dbg(rdev_to_dev(rdev),
1583 "Move QP = %p out of flush list\n",
1585 flags = bnxt_re_lock_cqs(qp);
1586 bnxt_qplib_clean_qp(&qp->qplib_qp);
1587 bnxt_re_unlock_cqs(qp, flags);
1590 if (qp_attr_mask & IB_QP_EN_SQD_ASYNC_NOTIFY) {
1591 qp->qplib_qp.modify_flags |=
1592 CMDQ_MODIFY_QP_MODIFY_MASK_EN_SQD_ASYNC_NOTIFY;
1593 qp->qplib_qp.en_sqd_async_notify = true;
1595 if (qp_attr_mask & IB_QP_ACCESS_FLAGS) {
1596 qp->qplib_qp.modify_flags |= CMDQ_MODIFY_QP_MODIFY_MASK_ACCESS;
1597 qp->qplib_qp.access =
1598 __from_ib_access_flags(qp_attr->qp_access_flags);
1599 /* LOCAL_WRITE access must be set to allow RC receive */
1600 qp->qplib_qp.access |= BNXT_QPLIB_ACCESS_LOCAL_WRITE;
1601 /* Temp: Set all params on QP as of now */
1602 qp->qplib_qp.access |= CMDQ_MODIFY_QP_ACCESS_REMOTE_WRITE;
1603 qp->qplib_qp.access |= CMDQ_MODIFY_QP_ACCESS_REMOTE_READ;
1605 if (qp_attr_mask & IB_QP_PKEY_INDEX) {
1606 qp->qplib_qp.modify_flags |= CMDQ_MODIFY_QP_MODIFY_MASK_PKEY;
1607 qp->qplib_qp.pkey_index = qp_attr->pkey_index;
1609 if (qp_attr_mask & IB_QP_QKEY) {
1610 qp->qplib_qp.modify_flags |= CMDQ_MODIFY_QP_MODIFY_MASK_QKEY;
1611 qp->qplib_qp.qkey = qp_attr->qkey;
1613 if (qp_attr_mask & IB_QP_AV) {
1614 const struct ib_global_route *grh =
1615 rdma_ah_read_grh(&qp_attr->ah_attr);
1616 const struct ib_gid_attr *sgid_attr;
1618 qp->qplib_qp.modify_flags |= CMDQ_MODIFY_QP_MODIFY_MASK_DGID |
1619 CMDQ_MODIFY_QP_MODIFY_MASK_FLOW_LABEL |
1620 CMDQ_MODIFY_QP_MODIFY_MASK_SGID_INDEX |
1621 CMDQ_MODIFY_QP_MODIFY_MASK_HOP_LIMIT |
1622 CMDQ_MODIFY_QP_MODIFY_MASK_TRAFFIC_CLASS |
1623 CMDQ_MODIFY_QP_MODIFY_MASK_DEST_MAC |
1624 CMDQ_MODIFY_QP_MODIFY_MASK_VLAN_ID;
1625 memcpy(qp->qplib_qp.ah.dgid.data, grh->dgid.raw,
1626 sizeof(qp->qplib_qp.ah.dgid.data));
1627 qp->qplib_qp.ah.flow_label = grh->flow_label;
1628 /* If RoCE V2 is enabled, stack will have two entries for
1629 * each GID entry. Avoiding this duplicte entry in HW. Dividing
1630 * the GID index by 2 for RoCE V2
1632 qp->qplib_qp.ah.sgid_index = grh->sgid_index / 2;
1633 qp->qplib_qp.ah.host_sgid_index = grh->sgid_index;
1634 qp->qplib_qp.ah.hop_limit = grh->hop_limit;
1635 qp->qplib_qp.ah.traffic_class = grh->traffic_class;
1636 qp->qplib_qp.ah.sl = rdma_ah_get_sl(&qp_attr->ah_attr);
1637 ether_addr_copy(qp->qplib_qp.ah.dmac,
1638 qp_attr->ah_attr.roce.dmac);
1640 sgid_attr = qp_attr->ah_attr.grh.sgid_attr;
1641 rc = rdma_read_gid_l2_fields(sgid_attr, NULL,
1642 &qp->qplib_qp.smac[0]);
1646 nw_type = rdma_gid_attr_network_type(sgid_attr);
1648 case RDMA_NETWORK_IPV4:
1649 qp->qplib_qp.nw_type =
1650 CMDQ_MODIFY_QP_NETWORK_TYPE_ROCEV2_IPV4;
1652 case RDMA_NETWORK_IPV6:
1653 qp->qplib_qp.nw_type =
1654 CMDQ_MODIFY_QP_NETWORK_TYPE_ROCEV2_IPV6;
1657 qp->qplib_qp.nw_type =
1658 CMDQ_MODIFY_QP_NETWORK_TYPE_ROCEV1;
1663 if (qp_attr_mask & IB_QP_PATH_MTU) {
1664 qp->qplib_qp.modify_flags |=
1665 CMDQ_MODIFY_QP_MODIFY_MASK_PATH_MTU;
1666 qp->qplib_qp.path_mtu = __from_ib_mtu(qp_attr->path_mtu);
1667 qp->qplib_qp.mtu = ib_mtu_enum_to_int(qp_attr->path_mtu);
1668 } else if (qp_attr->qp_state == IB_QPS_RTR) {
1669 qp->qplib_qp.modify_flags |=
1670 CMDQ_MODIFY_QP_MODIFY_MASK_PATH_MTU;
1671 qp->qplib_qp.path_mtu =
1672 __from_ib_mtu(iboe_get_mtu(rdev->netdev->mtu));
1674 ib_mtu_enum_to_int(iboe_get_mtu(rdev->netdev->mtu));
1677 if (qp_attr_mask & IB_QP_TIMEOUT) {
1678 qp->qplib_qp.modify_flags |= CMDQ_MODIFY_QP_MODIFY_MASK_TIMEOUT;
1679 qp->qplib_qp.timeout = qp_attr->timeout;
1681 if (qp_attr_mask & IB_QP_RETRY_CNT) {
1682 qp->qplib_qp.modify_flags |=
1683 CMDQ_MODIFY_QP_MODIFY_MASK_RETRY_CNT;
1684 qp->qplib_qp.retry_cnt = qp_attr->retry_cnt;
1686 if (qp_attr_mask & IB_QP_RNR_RETRY) {
1687 qp->qplib_qp.modify_flags |=
1688 CMDQ_MODIFY_QP_MODIFY_MASK_RNR_RETRY;
1689 qp->qplib_qp.rnr_retry = qp_attr->rnr_retry;
1691 if (qp_attr_mask & IB_QP_MIN_RNR_TIMER) {
1692 qp->qplib_qp.modify_flags |=
1693 CMDQ_MODIFY_QP_MODIFY_MASK_MIN_RNR_TIMER;
1694 qp->qplib_qp.min_rnr_timer = qp_attr->min_rnr_timer;
1696 if (qp_attr_mask & IB_QP_RQ_PSN) {
1697 qp->qplib_qp.modify_flags |= CMDQ_MODIFY_QP_MODIFY_MASK_RQ_PSN;
1698 qp->qplib_qp.rq.psn = qp_attr->rq_psn;
1700 if (qp_attr_mask & IB_QP_MAX_QP_RD_ATOMIC) {
1701 qp->qplib_qp.modify_flags |=
1702 CMDQ_MODIFY_QP_MODIFY_MASK_MAX_RD_ATOMIC;
1703 /* Cap the max_rd_atomic to device max */
1704 qp->qplib_qp.max_rd_atomic = min_t(u32, qp_attr->max_rd_atomic,
1705 dev_attr->max_qp_rd_atom);
1707 if (qp_attr_mask & IB_QP_SQ_PSN) {
1708 qp->qplib_qp.modify_flags |= CMDQ_MODIFY_QP_MODIFY_MASK_SQ_PSN;
1709 qp->qplib_qp.sq.psn = qp_attr->sq_psn;
1711 if (qp_attr_mask & IB_QP_MAX_DEST_RD_ATOMIC) {
1712 if (qp_attr->max_dest_rd_atomic >
1713 dev_attr->max_qp_init_rd_atom) {
1714 dev_err(rdev_to_dev(rdev),
1715 "max_dest_rd_atomic requested%d is > dev_max%d",
1716 qp_attr->max_dest_rd_atomic,
1717 dev_attr->max_qp_init_rd_atom);
1721 qp->qplib_qp.modify_flags |=
1722 CMDQ_MODIFY_QP_MODIFY_MASK_MAX_DEST_RD_ATOMIC;
1723 qp->qplib_qp.max_dest_rd_atomic = qp_attr->max_dest_rd_atomic;
1725 if (qp_attr_mask & IB_QP_CAP) {
1726 qp->qplib_qp.modify_flags |=
1727 CMDQ_MODIFY_QP_MODIFY_MASK_SQ_SIZE |
1728 CMDQ_MODIFY_QP_MODIFY_MASK_RQ_SIZE |
1729 CMDQ_MODIFY_QP_MODIFY_MASK_SQ_SGE |
1730 CMDQ_MODIFY_QP_MODIFY_MASK_RQ_SGE |
1731 CMDQ_MODIFY_QP_MODIFY_MASK_MAX_INLINE_DATA;
1732 if ((qp_attr->cap.max_send_wr >= dev_attr->max_qp_wqes) ||
1733 (qp_attr->cap.max_recv_wr >= dev_attr->max_qp_wqes) ||
1734 (qp_attr->cap.max_send_sge >= dev_attr->max_qp_sges) ||
1735 (qp_attr->cap.max_recv_sge >= dev_attr->max_qp_sges) ||
1736 (qp_attr->cap.max_inline_data >=
1737 dev_attr->max_inline_data)) {
1738 dev_err(rdev_to_dev(rdev),
1739 "Create QP failed - max exceeded");
1742 entries = roundup_pow_of_two(qp_attr->cap.max_send_wr);
1743 qp->qplib_qp.sq.max_wqe = min_t(u32, entries,
1744 dev_attr->max_qp_wqes + 1);
1745 qp->qplib_qp.sq.q_full_delta = qp->qplib_qp.sq.max_wqe -
1746 qp_attr->cap.max_send_wr;
1748 * Reserving one slot for Phantom WQE. Some application can
1749 * post one extra entry in this case. Allowing this to avoid
1750 * unexpected Queue full condition
1752 qp->qplib_qp.sq.q_full_delta -= 1;
1753 qp->qplib_qp.sq.max_sge = qp_attr->cap.max_send_sge;
1754 if (qp->qplib_qp.rq.max_wqe) {
1755 entries = roundup_pow_of_two(qp_attr->cap.max_recv_wr);
1756 qp->qplib_qp.rq.max_wqe =
1757 min_t(u32, entries, dev_attr->max_qp_wqes + 1);
1758 qp->qplib_qp.rq.q_full_delta = qp->qplib_qp.rq.max_wqe -
1759 qp_attr->cap.max_recv_wr;
1760 qp->qplib_qp.rq.max_sge = qp_attr->cap.max_recv_sge;
1762 /* SRQ was used prior, just ignore the RQ caps */
1765 if (qp_attr_mask & IB_QP_DEST_QPN) {
1766 qp->qplib_qp.modify_flags |=
1767 CMDQ_MODIFY_QP_MODIFY_MASK_DEST_QP_ID;
1768 qp->qplib_qp.dest_qpn = qp_attr->dest_qp_num;
1770 rc = bnxt_qplib_modify_qp(&rdev->qplib_res, &qp->qplib_qp);
1772 dev_err(rdev_to_dev(rdev), "Failed to modify HW QP");
1775 if (ib_qp->qp_type == IB_QPT_GSI && rdev->qp1_sqp)
1776 rc = bnxt_re_modify_shadow_qp(rdev, qp, qp_attr_mask);
1780 int bnxt_re_query_qp(struct ib_qp *ib_qp, struct ib_qp_attr *qp_attr,
1781 int qp_attr_mask, struct ib_qp_init_attr *qp_init_attr)
1783 struct bnxt_re_qp *qp = container_of(ib_qp, struct bnxt_re_qp, ib_qp);
1784 struct bnxt_re_dev *rdev = qp->rdev;
1785 struct bnxt_qplib_qp *qplib_qp;
1788 qplib_qp = kzalloc(sizeof(*qplib_qp), GFP_KERNEL);
1792 qplib_qp->id = qp->qplib_qp.id;
1793 qplib_qp->ah.host_sgid_index = qp->qplib_qp.ah.host_sgid_index;
1795 rc = bnxt_qplib_query_qp(&rdev->qplib_res, qplib_qp);
1797 dev_err(rdev_to_dev(rdev), "Failed to query HW QP");
1800 qp_attr->qp_state = __to_ib_qp_state(qplib_qp->state);
1801 qp_attr->en_sqd_async_notify = qplib_qp->en_sqd_async_notify ? 1 : 0;
1802 qp_attr->qp_access_flags = __to_ib_access_flags(qplib_qp->access);
1803 qp_attr->pkey_index = qplib_qp->pkey_index;
1804 qp_attr->qkey = qplib_qp->qkey;
1805 qp_attr->ah_attr.type = RDMA_AH_ATTR_TYPE_ROCE;
1806 rdma_ah_set_grh(&qp_attr->ah_attr, NULL, qplib_qp->ah.flow_label,
1807 qplib_qp->ah.host_sgid_index,
1808 qplib_qp->ah.hop_limit,
1809 qplib_qp->ah.traffic_class);
1810 rdma_ah_set_dgid_raw(&qp_attr->ah_attr, qplib_qp->ah.dgid.data);
1811 rdma_ah_set_sl(&qp_attr->ah_attr, qplib_qp->ah.sl);
1812 ether_addr_copy(qp_attr->ah_attr.roce.dmac, qplib_qp->ah.dmac);
1813 qp_attr->path_mtu = __to_ib_mtu(qplib_qp->path_mtu);
1814 qp_attr->timeout = qplib_qp->timeout;
1815 qp_attr->retry_cnt = qplib_qp->retry_cnt;
1816 qp_attr->rnr_retry = qplib_qp->rnr_retry;
1817 qp_attr->min_rnr_timer = qplib_qp->min_rnr_timer;
1818 qp_attr->rq_psn = qplib_qp->rq.psn;
1819 qp_attr->max_rd_atomic = qplib_qp->max_rd_atomic;
1820 qp_attr->sq_psn = qplib_qp->sq.psn;
1821 qp_attr->max_dest_rd_atomic = qplib_qp->max_dest_rd_atomic;
1822 qp_init_attr->sq_sig_type = qplib_qp->sig_type ? IB_SIGNAL_ALL_WR :
1824 qp_attr->dest_qp_num = qplib_qp->dest_qpn;
1826 qp_attr->cap.max_send_wr = qp->qplib_qp.sq.max_wqe;
1827 qp_attr->cap.max_send_sge = qp->qplib_qp.sq.max_sge;
1828 qp_attr->cap.max_recv_wr = qp->qplib_qp.rq.max_wqe;
1829 qp_attr->cap.max_recv_sge = qp->qplib_qp.rq.max_sge;
1830 qp_attr->cap.max_inline_data = qp->qplib_qp.max_inline_data;
1831 qp_init_attr->cap = qp_attr->cap;
1838 /* Routine for sending QP1 packets for RoCE V1 an V2
1840 static int bnxt_re_build_qp1_send_v2(struct bnxt_re_qp *qp,
1841 const struct ib_send_wr *wr,
1842 struct bnxt_qplib_swqe *wqe,
1845 struct bnxt_re_ah *ah = container_of(ud_wr(wr)->ah, struct bnxt_re_ah,
1847 struct bnxt_qplib_ah *qplib_ah = &ah->qplib_ah;
1848 const struct ib_gid_attr *sgid_attr = ah->ib_ah.sgid_attr;
1849 struct bnxt_qplib_sge sge;
1853 bool is_eth = false;
1854 bool is_vlan = false;
1855 bool is_grh = false;
1856 bool is_udp = false;
1858 u16 vlan_id = 0xFFFF;
1862 memset(&qp->qp1_hdr, 0, sizeof(qp->qp1_hdr));
1864 rc = rdma_read_gid_l2_fields(sgid_attr, &vlan_id, NULL);
1868 /* Get network header type for this GID */
1869 nw_type = rdma_gid_attr_network_type(sgid_attr);
1871 case RDMA_NETWORK_IPV4:
1872 nw_type = BNXT_RE_ROCEV2_IPV4_PACKET;
1874 case RDMA_NETWORK_IPV6:
1875 nw_type = BNXT_RE_ROCEV2_IPV6_PACKET;
1878 nw_type = BNXT_RE_ROCE_V1_PACKET;
1881 memcpy(&dgid.raw, &qplib_ah->dgid, 16);
1882 is_udp = sgid_attr->gid_type == IB_GID_TYPE_ROCE_UDP_ENCAP;
1884 if (ipv6_addr_v4mapped((struct in6_addr *)&sgid_attr->gid)) {
1886 ether_type = ETH_P_IP;
1889 ether_type = ETH_P_IPV6;
1893 ether_type = ETH_P_IBOE;
1898 is_vlan = (vlan_id && (vlan_id < 0x1000)) ? true : false;
1900 ib_ud_header_init(payload_size, !is_eth, is_eth, is_vlan, is_grh,
1901 ip_version, is_udp, 0, &qp->qp1_hdr);
1904 ether_addr_copy(qp->qp1_hdr.eth.dmac_h, ah->qplib_ah.dmac);
1905 ether_addr_copy(qp->qp1_hdr.eth.smac_h, qp->qplib_qp.smac);
1907 /* For vlan, check the sgid for vlan existence */
1910 qp->qp1_hdr.eth.type = cpu_to_be16(ether_type);
1912 qp->qp1_hdr.vlan.type = cpu_to_be16(ether_type);
1913 qp->qp1_hdr.vlan.tag = cpu_to_be16(vlan_id);
1916 if (is_grh || (ip_version == 6)) {
1917 memcpy(qp->qp1_hdr.grh.source_gid.raw, sgid_attr->gid.raw,
1918 sizeof(sgid_attr->gid));
1919 memcpy(qp->qp1_hdr.grh.destination_gid.raw, qplib_ah->dgid.data,
1920 sizeof(sgid_attr->gid));
1921 qp->qp1_hdr.grh.hop_limit = qplib_ah->hop_limit;
1924 if (ip_version == 4) {
1925 qp->qp1_hdr.ip4.tos = 0;
1926 qp->qp1_hdr.ip4.id = 0;
1927 qp->qp1_hdr.ip4.frag_off = htons(IP_DF);
1928 qp->qp1_hdr.ip4.ttl = qplib_ah->hop_limit;
1930 memcpy(&qp->qp1_hdr.ip4.saddr, sgid_attr->gid.raw + 12, 4);
1931 memcpy(&qp->qp1_hdr.ip4.daddr, qplib_ah->dgid.data + 12, 4);
1932 qp->qp1_hdr.ip4.check = ib_ud_ip4_csum(&qp->qp1_hdr);
1936 qp->qp1_hdr.udp.dport = htons(ROCE_V2_UDP_DPORT);
1937 qp->qp1_hdr.udp.sport = htons(0x8CD1);
1938 qp->qp1_hdr.udp.csum = 0;
1942 if (wr->opcode == IB_WR_SEND_WITH_IMM) {
1943 qp->qp1_hdr.bth.opcode = IB_OPCODE_UD_SEND_ONLY_WITH_IMMEDIATE;
1944 qp->qp1_hdr.immediate_present = 1;
1946 qp->qp1_hdr.bth.opcode = IB_OPCODE_UD_SEND_ONLY;
1948 if (wr->send_flags & IB_SEND_SOLICITED)
1949 qp->qp1_hdr.bth.solicited_event = 1;
1951 qp->qp1_hdr.bth.pad_count = (4 - payload_size) & 3;
1953 /* P_key for QP1 is for all members */
1954 qp->qp1_hdr.bth.pkey = cpu_to_be16(0xFFFF);
1955 qp->qp1_hdr.bth.destination_qpn = IB_QP1;
1956 qp->qp1_hdr.bth.ack_req = 0;
1958 qp->send_psn &= BTH_PSN_MASK;
1959 qp->qp1_hdr.bth.psn = cpu_to_be32(qp->send_psn);
1961 /* Use the priviledged Q_Key for QP1 */
1962 qp->qp1_hdr.deth.qkey = cpu_to_be32(IB_QP1_QKEY);
1963 qp->qp1_hdr.deth.source_qpn = IB_QP1;
1965 /* Pack the QP1 to the transmit buffer */
1966 buf = bnxt_qplib_get_qp1_sq_buf(&qp->qplib_qp, &sge);
1968 ib_ud_header_pack(&qp->qp1_hdr, buf);
1969 for (i = wqe->num_sge; i; i--) {
1970 wqe->sg_list[i].addr = wqe->sg_list[i - 1].addr;
1971 wqe->sg_list[i].lkey = wqe->sg_list[i - 1].lkey;
1972 wqe->sg_list[i].size = wqe->sg_list[i - 1].size;
1976 * Max Header buf size for IPV6 RoCE V2 is 86,
1977 * which is same as the QP1 SQ header buffer.
1978 * Header buf size for IPV4 RoCE V2 can be 66.
1979 * ETH(14) + VLAN(4)+ IP(20) + UDP (8) + BTH(20).
1980 * Subtract 20 bytes from QP1 SQ header buf size
1982 if (is_udp && ip_version == 4)
1985 * Max Header buf size for RoCE V1 is 78.
1986 * ETH(14) + VLAN(4) + GRH(40) + BTH(20).
1987 * Subtract 8 bytes from QP1 SQ header buf size
1992 /* Subtract 4 bytes for non vlan packets */
1996 wqe->sg_list[0].addr = sge.addr;
1997 wqe->sg_list[0].lkey = sge.lkey;
1998 wqe->sg_list[0].size = sge.size;
2002 dev_err(rdev_to_dev(qp->rdev), "QP1 buffer is empty!");
2008 /* For the MAD layer, it only provides the recv SGE the size of
2009 * ib_grh + MAD datagram. No Ethernet headers, Ethertype, BTH, DETH,
2010 * nor RoCE iCRC. The Cu+ solution must provide buffer for the entire
2011 * receive packet (334 bytes) with no VLAN and then copy the GRH
2012 * and the MAD datagram out to the provided SGE.
2014 static int bnxt_re_build_qp1_shadow_qp_recv(struct bnxt_re_qp *qp,
2015 const struct ib_recv_wr *wr,
2016 struct bnxt_qplib_swqe *wqe,
2019 struct bnxt_qplib_sge ref, sge;
2021 struct bnxt_re_sqp_entries *sqp_entry;
2023 rq_prod_index = bnxt_qplib_get_rq_prod_index(&qp->qplib_qp);
2025 if (!bnxt_qplib_get_qp1_rq_buf(&qp->qplib_qp, &sge))
2028 /* Create 1 SGE to receive the entire
2031 /* Save the reference from ULP */
2032 ref.addr = wqe->sg_list[0].addr;
2033 ref.lkey = wqe->sg_list[0].lkey;
2034 ref.size = wqe->sg_list[0].size;
2036 sqp_entry = &qp->rdev->sqp_tbl[rq_prod_index];
2039 wqe->sg_list[0].addr = sge.addr;
2040 wqe->sg_list[0].lkey = sge.lkey;
2041 wqe->sg_list[0].size = BNXT_QPLIB_MAX_QP1_RQ_HDR_SIZE_V2;
2042 sge.size -= wqe->sg_list[0].size;
2044 sqp_entry->sge.addr = ref.addr;
2045 sqp_entry->sge.lkey = ref.lkey;
2046 sqp_entry->sge.size = ref.size;
2047 /* Store the wrid for reporting completion */
2048 sqp_entry->wrid = wqe->wr_id;
2049 /* change the wqe->wrid to table index */
2050 wqe->wr_id = rq_prod_index;
2054 static int is_ud_qp(struct bnxt_re_qp *qp)
2056 return (qp->qplib_qp.type == CMDQ_CREATE_QP_TYPE_UD ||
2057 qp->qplib_qp.type == CMDQ_CREATE_QP_TYPE_GSI);
2060 static int bnxt_re_build_send_wqe(struct bnxt_re_qp *qp,
2061 const struct ib_send_wr *wr,
2062 struct bnxt_qplib_swqe *wqe)
2064 struct bnxt_re_ah *ah = NULL;
2067 ah = container_of(ud_wr(wr)->ah, struct bnxt_re_ah, ib_ah);
2068 wqe->send.q_key = ud_wr(wr)->remote_qkey;
2069 wqe->send.dst_qp = ud_wr(wr)->remote_qpn;
2070 wqe->send.avid = ah->qplib_ah.id;
2072 switch (wr->opcode) {
2074 wqe->type = BNXT_QPLIB_SWQE_TYPE_SEND;
2076 case IB_WR_SEND_WITH_IMM:
2077 wqe->type = BNXT_QPLIB_SWQE_TYPE_SEND_WITH_IMM;
2078 wqe->send.imm_data = wr->ex.imm_data;
2080 case IB_WR_SEND_WITH_INV:
2081 wqe->type = BNXT_QPLIB_SWQE_TYPE_SEND_WITH_INV;
2082 wqe->send.inv_key = wr->ex.invalidate_rkey;
2087 if (wr->send_flags & IB_SEND_SIGNALED)
2088 wqe->flags |= BNXT_QPLIB_SWQE_FLAGS_SIGNAL_COMP;
2089 if (wr->send_flags & IB_SEND_FENCE)
2090 wqe->flags |= BNXT_QPLIB_SWQE_FLAGS_UC_FENCE;
2091 if (wr->send_flags & IB_SEND_SOLICITED)
2092 wqe->flags |= BNXT_QPLIB_SWQE_FLAGS_SOLICIT_EVENT;
2093 if (wr->send_flags & IB_SEND_INLINE)
2094 wqe->flags |= BNXT_QPLIB_SWQE_FLAGS_INLINE;
2099 static int bnxt_re_build_rdma_wqe(const struct ib_send_wr *wr,
2100 struct bnxt_qplib_swqe *wqe)
2102 switch (wr->opcode) {
2103 case IB_WR_RDMA_WRITE:
2104 wqe->type = BNXT_QPLIB_SWQE_TYPE_RDMA_WRITE;
2106 case IB_WR_RDMA_WRITE_WITH_IMM:
2107 wqe->type = BNXT_QPLIB_SWQE_TYPE_RDMA_WRITE_WITH_IMM;
2108 wqe->rdma.imm_data = wr->ex.imm_data;
2110 case IB_WR_RDMA_READ:
2111 wqe->type = BNXT_QPLIB_SWQE_TYPE_RDMA_READ;
2112 wqe->rdma.inv_key = wr->ex.invalidate_rkey;
2117 wqe->rdma.remote_va = rdma_wr(wr)->remote_addr;
2118 wqe->rdma.r_key = rdma_wr(wr)->rkey;
2119 if (wr->send_flags & IB_SEND_SIGNALED)
2120 wqe->flags |= BNXT_QPLIB_SWQE_FLAGS_SIGNAL_COMP;
2121 if (wr->send_flags & IB_SEND_FENCE)
2122 wqe->flags |= BNXT_QPLIB_SWQE_FLAGS_UC_FENCE;
2123 if (wr->send_flags & IB_SEND_SOLICITED)
2124 wqe->flags |= BNXT_QPLIB_SWQE_FLAGS_SOLICIT_EVENT;
2125 if (wr->send_flags & IB_SEND_INLINE)
2126 wqe->flags |= BNXT_QPLIB_SWQE_FLAGS_INLINE;
2131 static int bnxt_re_build_atomic_wqe(const struct ib_send_wr *wr,
2132 struct bnxt_qplib_swqe *wqe)
2134 switch (wr->opcode) {
2135 case IB_WR_ATOMIC_CMP_AND_SWP:
2136 wqe->type = BNXT_QPLIB_SWQE_TYPE_ATOMIC_CMP_AND_SWP;
2137 wqe->atomic.cmp_data = atomic_wr(wr)->compare_add;
2138 wqe->atomic.swap_data = atomic_wr(wr)->swap;
2140 case IB_WR_ATOMIC_FETCH_AND_ADD:
2141 wqe->type = BNXT_QPLIB_SWQE_TYPE_ATOMIC_FETCH_AND_ADD;
2142 wqe->atomic.cmp_data = atomic_wr(wr)->compare_add;
2147 wqe->atomic.remote_va = atomic_wr(wr)->remote_addr;
2148 wqe->atomic.r_key = atomic_wr(wr)->rkey;
2149 if (wr->send_flags & IB_SEND_SIGNALED)
2150 wqe->flags |= BNXT_QPLIB_SWQE_FLAGS_SIGNAL_COMP;
2151 if (wr->send_flags & IB_SEND_FENCE)
2152 wqe->flags |= BNXT_QPLIB_SWQE_FLAGS_UC_FENCE;
2153 if (wr->send_flags & IB_SEND_SOLICITED)
2154 wqe->flags |= BNXT_QPLIB_SWQE_FLAGS_SOLICIT_EVENT;
2158 static int bnxt_re_build_inv_wqe(const struct ib_send_wr *wr,
2159 struct bnxt_qplib_swqe *wqe)
2161 wqe->type = BNXT_QPLIB_SWQE_TYPE_LOCAL_INV;
2162 wqe->local_inv.inv_l_key = wr->ex.invalidate_rkey;
2164 /* Need unconditional fence for local invalidate
2165 * opcode to work as expected.
2167 wqe->flags |= BNXT_QPLIB_SWQE_FLAGS_UC_FENCE;
2169 if (wr->send_flags & IB_SEND_SIGNALED)
2170 wqe->flags |= BNXT_QPLIB_SWQE_FLAGS_SIGNAL_COMP;
2171 if (wr->send_flags & IB_SEND_SOLICITED)
2172 wqe->flags |= BNXT_QPLIB_SWQE_FLAGS_SOLICIT_EVENT;
2177 static int bnxt_re_build_reg_wqe(const struct ib_reg_wr *wr,
2178 struct bnxt_qplib_swqe *wqe)
2180 struct bnxt_re_mr *mr = container_of(wr->mr, struct bnxt_re_mr, ib_mr);
2181 struct bnxt_qplib_frpl *qplib_frpl = &mr->qplib_frpl;
2182 int access = wr->access;
2184 wqe->frmr.pbl_ptr = (__le64 *)qplib_frpl->hwq.pbl_ptr[0];
2185 wqe->frmr.pbl_dma_ptr = qplib_frpl->hwq.pbl_dma_ptr[0];
2186 wqe->frmr.page_list = mr->pages;
2187 wqe->frmr.page_list_len = mr->npages;
2188 wqe->frmr.levels = qplib_frpl->hwq.level + 1;
2189 wqe->type = BNXT_QPLIB_SWQE_TYPE_REG_MR;
2191 /* Need unconditional fence for reg_mr
2192 * opcode to function as expected.
2195 wqe->flags |= BNXT_QPLIB_SWQE_FLAGS_UC_FENCE;
2197 if (wr->wr.send_flags & IB_SEND_SIGNALED)
2198 wqe->flags |= BNXT_QPLIB_SWQE_FLAGS_SIGNAL_COMP;
2200 if (access & IB_ACCESS_LOCAL_WRITE)
2201 wqe->frmr.access_cntl |= SQ_FR_PMR_ACCESS_CNTL_LOCAL_WRITE;
2202 if (access & IB_ACCESS_REMOTE_READ)
2203 wqe->frmr.access_cntl |= SQ_FR_PMR_ACCESS_CNTL_REMOTE_READ;
2204 if (access & IB_ACCESS_REMOTE_WRITE)
2205 wqe->frmr.access_cntl |= SQ_FR_PMR_ACCESS_CNTL_REMOTE_WRITE;
2206 if (access & IB_ACCESS_REMOTE_ATOMIC)
2207 wqe->frmr.access_cntl |= SQ_FR_PMR_ACCESS_CNTL_REMOTE_ATOMIC;
2208 if (access & IB_ACCESS_MW_BIND)
2209 wqe->frmr.access_cntl |= SQ_FR_PMR_ACCESS_CNTL_WINDOW_BIND;
2211 wqe->frmr.l_key = wr->key;
2212 wqe->frmr.length = wr->mr->length;
2213 wqe->frmr.pbl_pg_sz_log = (wr->mr->page_size >> PAGE_SHIFT_4K) - 1;
2214 wqe->frmr.va = wr->mr->iova;
2218 static int bnxt_re_copy_inline_data(struct bnxt_re_dev *rdev,
2219 const struct ib_send_wr *wr,
2220 struct bnxt_qplib_swqe *wqe)
2222 /* Copy the inline data to the data field */
2227 in_data = wqe->inline_data;
2228 for (i = 0; i < wr->num_sge; i++) {
2229 sge_addr = (void *)(unsigned long)
2230 wr->sg_list[i].addr;
2231 sge_len = wr->sg_list[i].length;
2233 if ((sge_len + wqe->inline_len) >
2234 BNXT_QPLIB_SWQE_MAX_INLINE_LENGTH) {
2235 dev_err(rdev_to_dev(rdev),
2236 "Inline data size requested > supported value");
2239 sge_len = wr->sg_list[i].length;
2241 memcpy(in_data, sge_addr, sge_len);
2242 in_data += wr->sg_list[i].length;
2243 wqe->inline_len += wr->sg_list[i].length;
2245 return wqe->inline_len;
2248 static int bnxt_re_copy_wr_payload(struct bnxt_re_dev *rdev,
2249 const struct ib_send_wr *wr,
2250 struct bnxt_qplib_swqe *wqe)
2254 if (wr->send_flags & IB_SEND_INLINE)
2255 payload_sz = bnxt_re_copy_inline_data(rdev, wr, wqe);
2257 payload_sz = bnxt_re_build_sgl(wr->sg_list, wqe->sg_list,
2263 static void bnxt_ud_qp_hw_stall_workaround(struct bnxt_re_qp *qp)
2265 if ((qp->ib_qp.qp_type == IB_QPT_UD ||
2266 qp->ib_qp.qp_type == IB_QPT_GSI ||
2267 qp->ib_qp.qp_type == IB_QPT_RAW_ETHERTYPE) &&
2268 qp->qplib_qp.wqe_cnt == BNXT_RE_UD_QP_HW_STALL) {
2270 struct ib_qp_attr qp_attr;
2272 qp_attr_mask = IB_QP_STATE;
2273 qp_attr.qp_state = IB_QPS_RTS;
2274 bnxt_re_modify_qp(&qp->ib_qp, &qp_attr, qp_attr_mask, NULL);
2275 qp->qplib_qp.wqe_cnt = 0;
2279 static int bnxt_re_post_send_shadow_qp(struct bnxt_re_dev *rdev,
2280 struct bnxt_re_qp *qp,
2281 const struct ib_send_wr *wr)
2283 struct bnxt_qplib_swqe wqe;
2284 int rc = 0, payload_sz = 0;
2285 unsigned long flags;
2287 spin_lock_irqsave(&qp->sq_lock, flags);
2288 memset(&wqe, 0, sizeof(wqe));
2291 memset(&wqe, 0, sizeof(wqe));
2294 wqe.num_sge = wr->num_sge;
2295 if (wr->num_sge > qp->qplib_qp.sq.max_sge) {
2296 dev_err(rdev_to_dev(rdev),
2297 "Limit exceeded for Send SGEs");
2302 payload_sz = bnxt_re_copy_wr_payload(qp->rdev, wr, &wqe);
2303 if (payload_sz < 0) {
2307 wqe.wr_id = wr->wr_id;
2309 wqe.type = BNXT_QPLIB_SWQE_TYPE_SEND;
2311 rc = bnxt_re_build_send_wqe(qp, wr, &wqe);
2313 rc = bnxt_qplib_post_send(&qp->qplib_qp, &wqe);
2316 dev_err(rdev_to_dev(rdev),
2317 "Post send failed opcode = %#x rc = %d",
2323 bnxt_qplib_post_send_db(&qp->qplib_qp);
2324 bnxt_ud_qp_hw_stall_workaround(qp);
2325 spin_unlock_irqrestore(&qp->sq_lock, flags);
2329 int bnxt_re_post_send(struct ib_qp *ib_qp, const struct ib_send_wr *wr,
2330 const struct ib_send_wr **bad_wr)
2332 struct bnxt_re_qp *qp = container_of(ib_qp, struct bnxt_re_qp, ib_qp);
2333 struct bnxt_qplib_swqe wqe;
2334 int rc = 0, payload_sz = 0;
2335 unsigned long flags;
2337 spin_lock_irqsave(&qp->sq_lock, flags);
2340 memset(&wqe, 0, sizeof(wqe));
2343 wqe.num_sge = wr->num_sge;
2344 if (wr->num_sge > qp->qplib_qp.sq.max_sge) {
2345 dev_err(rdev_to_dev(qp->rdev),
2346 "Limit exceeded for Send SGEs");
2351 payload_sz = bnxt_re_copy_wr_payload(qp->rdev, wr, &wqe);
2352 if (payload_sz < 0) {
2356 wqe.wr_id = wr->wr_id;
2358 switch (wr->opcode) {
2360 case IB_WR_SEND_WITH_IMM:
2361 if (qp->qplib_qp.type == CMDQ_CREATE_QP1_TYPE_GSI) {
2362 rc = bnxt_re_build_qp1_send_v2(qp, wr, &wqe,
2366 wqe.rawqp1.lflags |=
2367 SQ_SEND_RAWETH_QP1_LFLAGS_ROCE_CRC;
2369 switch (wr->send_flags) {
2370 case IB_SEND_IP_CSUM:
2371 wqe.rawqp1.lflags |=
2372 SQ_SEND_RAWETH_QP1_LFLAGS_IP_CHKSUM;
2378 case IB_WR_SEND_WITH_INV:
2379 rc = bnxt_re_build_send_wqe(qp, wr, &wqe);
2381 case IB_WR_RDMA_WRITE:
2382 case IB_WR_RDMA_WRITE_WITH_IMM:
2383 case IB_WR_RDMA_READ:
2384 rc = bnxt_re_build_rdma_wqe(wr, &wqe);
2386 case IB_WR_ATOMIC_CMP_AND_SWP:
2387 case IB_WR_ATOMIC_FETCH_AND_ADD:
2388 rc = bnxt_re_build_atomic_wqe(wr, &wqe);
2390 case IB_WR_RDMA_READ_WITH_INV:
2391 dev_err(rdev_to_dev(qp->rdev),
2392 "RDMA Read with Invalidate is not supported");
2395 case IB_WR_LOCAL_INV:
2396 rc = bnxt_re_build_inv_wqe(wr, &wqe);
2399 rc = bnxt_re_build_reg_wqe(reg_wr(wr), &wqe);
2402 /* Unsupported WRs */
2403 dev_err(rdev_to_dev(qp->rdev),
2404 "WR (%#x) is not supported", wr->opcode);
2409 rc = bnxt_qplib_post_send(&qp->qplib_qp, &wqe);
2412 dev_err(rdev_to_dev(qp->rdev),
2413 "post_send failed op:%#x qps = %#x rc = %d\n",
2414 wr->opcode, qp->qplib_qp.state, rc);
2420 bnxt_qplib_post_send_db(&qp->qplib_qp);
2421 bnxt_ud_qp_hw_stall_workaround(qp);
2422 spin_unlock_irqrestore(&qp->sq_lock, flags);
2427 static int bnxt_re_post_recv_shadow_qp(struct bnxt_re_dev *rdev,
2428 struct bnxt_re_qp *qp,
2429 const struct ib_recv_wr *wr)
2431 struct bnxt_qplib_swqe wqe;
2434 memset(&wqe, 0, sizeof(wqe));
2437 memset(&wqe, 0, sizeof(wqe));
2440 wqe.num_sge = wr->num_sge;
2441 if (wr->num_sge > qp->qplib_qp.rq.max_sge) {
2442 dev_err(rdev_to_dev(rdev),
2443 "Limit exceeded for Receive SGEs");
2447 bnxt_re_build_sgl(wr->sg_list, wqe.sg_list, wr->num_sge);
2448 wqe.wr_id = wr->wr_id;
2449 wqe.type = BNXT_QPLIB_SWQE_TYPE_RECV;
2451 rc = bnxt_qplib_post_recv(&qp->qplib_qp, &wqe);
2458 bnxt_qplib_post_recv_db(&qp->qplib_qp);
2462 int bnxt_re_post_recv(struct ib_qp *ib_qp, const struct ib_recv_wr *wr,
2463 const struct ib_recv_wr **bad_wr)
2465 struct bnxt_re_qp *qp = container_of(ib_qp, struct bnxt_re_qp, ib_qp);
2466 struct bnxt_qplib_swqe wqe;
2467 int rc = 0, payload_sz = 0;
2468 unsigned long flags;
2471 spin_lock_irqsave(&qp->rq_lock, flags);
2474 memset(&wqe, 0, sizeof(wqe));
2477 wqe.num_sge = wr->num_sge;
2478 if (wr->num_sge > qp->qplib_qp.rq.max_sge) {
2479 dev_err(rdev_to_dev(qp->rdev),
2480 "Limit exceeded for Receive SGEs");
2486 payload_sz = bnxt_re_build_sgl(wr->sg_list, wqe.sg_list,
2488 wqe.wr_id = wr->wr_id;
2489 wqe.type = BNXT_QPLIB_SWQE_TYPE_RECV;
2491 if (ib_qp->qp_type == IB_QPT_GSI &&
2492 qp->qplib_qp.type != CMDQ_CREATE_QP_TYPE_GSI)
2493 rc = bnxt_re_build_qp1_shadow_qp_recv(qp, wr, &wqe,
2496 rc = bnxt_qplib_post_recv(&qp->qplib_qp, &wqe);
2502 /* Ring DB if the RQEs posted reaches a threshold value */
2503 if (++count >= BNXT_RE_RQ_WQE_THRESHOLD) {
2504 bnxt_qplib_post_recv_db(&qp->qplib_qp);
2512 bnxt_qplib_post_recv_db(&qp->qplib_qp);
2514 spin_unlock_irqrestore(&qp->rq_lock, flags);
2519 /* Completion Queues */
2520 int bnxt_re_destroy_cq(struct ib_cq *ib_cq, struct ib_udata *udata)
2523 struct bnxt_re_cq *cq;
2524 struct bnxt_qplib_nq *nq;
2525 struct bnxt_re_dev *rdev;
2527 cq = container_of(ib_cq, struct bnxt_re_cq, ib_cq);
2529 nq = cq->qplib_cq.nq;
2531 rc = bnxt_qplib_destroy_cq(&rdev->qplib_res, &cq->qplib_cq);
2533 dev_err(rdev_to_dev(rdev), "Failed to destroy HW CQ");
2536 if (!IS_ERR_OR_NULL(cq->umem))
2537 ib_umem_release(cq->umem);
2539 atomic_dec(&rdev->cq_count);
2547 struct ib_cq *bnxt_re_create_cq(struct ib_device *ibdev,
2548 const struct ib_cq_init_attr *attr,
2549 struct ib_udata *udata)
2551 struct bnxt_re_dev *rdev = to_bnxt_re_dev(ibdev, ibdev);
2552 struct bnxt_qplib_dev_attr *dev_attr = &rdev->dev_attr;
2553 struct bnxt_re_cq *cq = NULL;
2555 int cqe = attr->cqe;
2556 struct bnxt_qplib_nq *nq = NULL;
2557 unsigned int nq_alloc_cnt;
2559 /* Validate CQ fields */
2560 if (cqe < 1 || cqe > dev_attr->max_cq_wqes) {
2561 dev_err(rdev_to_dev(rdev), "Failed to create CQ -max exceeded");
2562 return ERR_PTR(-EINVAL);
2564 cq = kzalloc(sizeof(*cq), GFP_KERNEL);
2566 return ERR_PTR(-ENOMEM);
2569 cq->qplib_cq.cq_handle = (u64)(unsigned long)(&cq->qplib_cq);
2571 entries = roundup_pow_of_two(cqe + 1);
2572 if (entries > dev_attr->max_cq_wqes + 1)
2573 entries = dev_attr->max_cq_wqes + 1;
2576 struct bnxt_re_cq_req req;
2577 struct bnxt_re_ucontext *uctx = rdma_udata_to_drv_context(
2578 udata, struct bnxt_re_ucontext, ib_uctx);
2579 if (ib_copy_from_udata(&req, udata, sizeof(req))) {
2584 cq->umem = ib_umem_get(udata, req.cq_va,
2585 entries * sizeof(struct cq_base),
2586 IB_ACCESS_LOCAL_WRITE, 1);
2587 if (IS_ERR(cq->umem)) {
2588 rc = PTR_ERR(cq->umem);
2591 cq->qplib_cq.sg_info.sglist = cq->umem->sg_head.sgl;
2592 cq->qplib_cq.sg_info.npages = ib_umem_num_pages(cq->umem);
2593 cq->qplib_cq.sg_info.nmap = cq->umem->nmap;
2594 cq->qplib_cq.dpi = &uctx->dpi;
2596 cq->max_cql = min_t(u32, entries, MAX_CQL_PER_POLL);
2597 cq->cql = kcalloc(cq->max_cql, sizeof(struct bnxt_qplib_cqe),
2604 cq->qplib_cq.dpi = &rdev->dpi_privileged;
2607 * Allocating the NQ in a round robin fashion. nq_alloc_cnt is a
2608 * used for getting the NQ index.
2610 nq_alloc_cnt = atomic_inc_return(&rdev->nq_alloc_cnt);
2611 nq = &rdev->nq[nq_alloc_cnt % (rdev->num_msix - 1)];
2612 cq->qplib_cq.max_wqe = entries;
2613 cq->qplib_cq.cnq_hw_ring_id = nq->ring_id;
2614 cq->qplib_cq.nq = nq;
2616 rc = bnxt_qplib_create_cq(&rdev->qplib_res, &cq->qplib_cq);
2618 dev_err(rdev_to_dev(rdev), "Failed to create HW CQ");
2622 cq->ib_cq.cqe = entries;
2623 cq->cq_period = cq->qplib_cq.period;
2626 atomic_inc(&rdev->cq_count);
2627 spin_lock_init(&cq->cq_lock);
2630 struct bnxt_re_cq_resp resp;
2632 resp.cqid = cq->qplib_cq.id;
2633 resp.tail = cq->qplib_cq.hwq.cons;
2634 resp.phase = cq->qplib_cq.period;
2636 rc = ib_copy_to_udata(udata, &resp, sizeof(resp));
2638 dev_err(rdev_to_dev(rdev), "Failed to copy CQ udata");
2639 bnxt_qplib_destroy_cq(&rdev->qplib_res, &cq->qplib_cq);
2648 ib_umem_release(cq->umem);
2655 static u8 __req_to_ib_wc_status(u8 qstatus)
2658 case CQ_REQ_STATUS_OK:
2659 return IB_WC_SUCCESS;
2660 case CQ_REQ_STATUS_BAD_RESPONSE_ERR:
2661 return IB_WC_BAD_RESP_ERR;
2662 case CQ_REQ_STATUS_LOCAL_LENGTH_ERR:
2663 return IB_WC_LOC_LEN_ERR;
2664 case CQ_REQ_STATUS_LOCAL_QP_OPERATION_ERR:
2665 return IB_WC_LOC_QP_OP_ERR;
2666 case CQ_REQ_STATUS_LOCAL_PROTECTION_ERR:
2667 return IB_WC_LOC_PROT_ERR;
2668 case CQ_REQ_STATUS_MEMORY_MGT_OPERATION_ERR:
2669 return IB_WC_GENERAL_ERR;
2670 case CQ_REQ_STATUS_REMOTE_INVALID_REQUEST_ERR:
2671 return IB_WC_REM_INV_REQ_ERR;
2672 case CQ_REQ_STATUS_REMOTE_ACCESS_ERR:
2673 return IB_WC_REM_ACCESS_ERR;
2674 case CQ_REQ_STATUS_REMOTE_OPERATION_ERR:
2675 return IB_WC_REM_OP_ERR;
2676 case CQ_REQ_STATUS_RNR_NAK_RETRY_CNT_ERR:
2677 return IB_WC_RNR_RETRY_EXC_ERR;
2678 case CQ_REQ_STATUS_TRANSPORT_RETRY_CNT_ERR:
2679 return IB_WC_RETRY_EXC_ERR;
2680 case CQ_REQ_STATUS_WORK_REQUEST_FLUSHED_ERR:
2681 return IB_WC_WR_FLUSH_ERR;
2683 return IB_WC_GENERAL_ERR;
2688 static u8 __rawqp1_to_ib_wc_status(u8 qstatus)
2691 case CQ_RES_RAWETH_QP1_STATUS_OK:
2692 return IB_WC_SUCCESS;
2693 case CQ_RES_RAWETH_QP1_STATUS_LOCAL_ACCESS_ERROR:
2694 return IB_WC_LOC_ACCESS_ERR;
2695 case CQ_RES_RAWETH_QP1_STATUS_HW_LOCAL_LENGTH_ERR:
2696 return IB_WC_LOC_LEN_ERR;
2697 case CQ_RES_RAWETH_QP1_STATUS_LOCAL_PROTECTION_ERR:
2698 return IB_WC_LOC_PROT_ERR;
2699 case CQ_RES_RAWETH_QP1_STATUS_LOCAL_QP_OPERATION_ERR:
2700 return IB_WC_LOC_QP_OP_ERR;
2701 case CQ_RES_RAWETH_QP1_STATUS_MEMORY_MGT_OPERATION_ERR:
2702 return IB_WC_GENERAL_ERR;
2703 case CQ_RES_RAWETH_QP1_STATUS_WORK_REQUEST_FLUSHED_ERR:
2704 return IB_WC_WR_FLUSH_ERR;
2705 case CQ_RES_RAWETH_QP1_STATUS_HW_FLUSH_ERR:
2706 return IB_WC_WR_FLUSH_ERR;
2708 return IB_WC_GENERAL_ERR;
2712 static u8 __rc_to_ib_wc_status(u8 qstatus)
2715 case CQ_RES_RC_STATUS_OK:
2716 return IB_WC_SUCCESS;
2717 case CQ_RES_RC_STATUS_LOCAL_ACCESS_ERROR:
2718 return IB_WC_LOC_ACCESS_ERR;
2719 case CQ_RES_RC_STATUS_LOCAL_LENGTH_ERR:
2720 return IB_WC_LOC_LEN_ERR;
2721 case CQ_RES_RC_STATUS_LOCAL_PROTECTION_ERR:
2722 return IB_WC_LOC_PROT_ERR;
2723 case CQ_RES_RC_STATUS_LOCAL_QP_OPERATION_ERR:
2724 return IB_WC_LOC_QP_OP_ERR;
2725 case CQ_RES_RC_STATUS_MEMORY_MGT_OPERATION_ERR:
2726 return IB_WC_GENERAL_ERR;
2727 case CQ_RES_RC_STATUS_REMOTE_INVALID_REQUEST_ERR:
2728 return IB_WC_REM_INV_REQ_ERR;
2729 case CQ_RES_RC_STATUS_WORK_REQUEST_FLUSHED_ERR:
2730 return IB_WC_WR_FLUSH_ERR;
2731 case CQ_RES_RC_STATUS_HW_FLUSH_ERR:
2732 return IB_WC_WR_FLUSH_ERR;
2734 return IB_WC_GENERAL_ERR;
2738 static void bnxt_re_process_req_wc(struct ib_wc *wc, struct bnxt_qplib_cqe *cqe)
2740 switch (cqe->type) {
2741 case BNXT_QPLIB_SWQE_TYPE_SEND:
2742 wc->opcode = IB_WC_SEND;
2744 case BNXT_QPLIB_SWQE_TYPE_SEND_WITH_IMM:
2745 wc->opcode = IB_WC_SEND;
2746 wc->wc_flags |= IB_WC_WITH_IMM;
2748 case BNXT_QPLIB_SWQE_TYPE_SEND_WITH_INV:
2749 wc->opcode = IB_WC_SEND;
2750 wc->wc_flags |= IB_WC_WITH_INVALIDATE;
2752 case BNXT_QPLIB_SWQE_TYPE_RDMA_WRITE:
2753 wc->opcode = IB_WC_RDMA_WRITE;
2755 case BNXT_QPLIB_SWQE_TYPE_RDMA_WRITE_WITH_IMM:
2756 wc->opcode = IB_WC_RDMA_WRITE;
2757 wc->wc_flags |= IB_WC_WITH_IMM;
2759 case BNXT_QPLIB_SWQE_TYPE_RDMA_READ:
2760 wc->opcode = IB_WC_RDMA_READ;
2762 case BNXT_QPLIB_SWQE_TYPE_ATOMIC_CMP_AND_SWP:
2763 wc->opcode = IB_WC_COMP_SWAP;
2765 case BNXT_QPLIB_SWQE_TYPE_ATOMIC_FETCH_AND_ADD:
2766 wc->opcode = IB_WC_FETCH_ADD;
2768 case BNXT_QPLIB_SWQE_TYPE_LOCAL_INV:
2769 wc->opcode = IB_WC_LOCAL_INV;
2771 case BNXT_QPLIB_SWQE_TYPE_REG_MR:
2772 wc->opcode = IB_WC_REG_MR;
2775 wc->opcode = IB_WC_SEND;
2779 wc->status = __req_to_ib_wc_status(cqe->status);
2782 static int bnxt_re_check_packet_type(u16 raweth_qp1_flags,
2783 u16 raweth_qp1_flags2)
2785 bool is_ipv6 = false, is_ipv4 = false;
2787 /* raweth_qp1_flags Bit 9-6 indicates itype */
2788 if ((raweth_qp1_flags & CQ_RES_RAWETH_QP1_RAWETH_QP1_FLAGS_ITYPE_ROCE)
2789 != CQ_RES_RAWETH_QP1_RAWETH_QP1_FLAGS_ITYPE_ROCE)
2792 if (raweth_qp1_flags2 &
2793 CQ_RES_RAWETH_QP1_RAWETH_QP1_FLAGS2_IP_CS_CALC &&
2795 CQ_RES_RAWETH_QP1_RAWETH_QP1_FLAGS2_L4_CS_CALC) {
2796 /* raweth_qp1_flags2 Bit 8 indicates ip_type. 0-v4 1 - v6 */
2797 (raweth_qp1_flags2 &
2798 CQ_RES_RAWETH_QP1_RAWETH_QP1_FLAGS2_IP_TYPE) ?
2799 (is_ipv6 = true) : (is_ipv4 = true);
2801 BNXT_RE_ROCEV2_IPV6_PACKET :
2802 BNXT_RE_ROCEV2_IPV4_PACKET);
2804 return BNXT_RE_ROCE_V1_PACKET;
2808 static int bnxt_re_to_ib_nw_type(int nw_type)
2810 u8 nw_hdr_type = 0xFF;
2813 case BNXT_RE_ROCE_V1_PACKET:
2814 nw_hdr_type = RDMA_NETWORK_ROCE_V1;
2816 case BNXT_RE_ROCEV2_IPV4_PACKET:
2817 nw_hdr_type = RDMA_NETWORK_IPV4;
2819 case BNXT_RE_ROCEV2_IPV6_PACKET:
2820 nw_hdr_type = RDMA_NETWORK_IPV6;
2826 static bool bnxt_re_is_loopback_packet(struct bnxt_re_dev *rdev,
2830 struct ethhdr *eth_hdr;
2834 tmp_buf = (u8 *)rq_hdr_buf;
2836 * If dest mac is not same as I/F mac, this could be a
2837 * loopback address or multicast address, check whether
2838 * it is a loopback packet
2840 if (!ether_addr_equal(tmp_buf, rdev->netdev->dev_addr)) {
2842 /* Check the ether type */
2843 eth_hdr = (struct ethhdr *)tmp_buf;
2844 eth_type = ntohs(eth_hdr->h_proto);
2852 struct udphdr *udp_hdr;
2854 len = (eth_type == ETH_P_IP ? sizeof(struct iphdr) :
2855 sizeof(struct ipv6hdr));
2856 tmp_buf += sizeof(struct ethhdr) + len;
2857 udp_hdr = (struct udphdr *)tmp_buf;
2858 if (ntohs(udp_hdr->dest) ==
2871 static int bnxt_re_process_raw_qp_pkt_rx(struct bnxt_re_qp *qp1_qp,
2872 struct bnxt_qplib_cqe *cqe)
2874 struct bnxt_re_dev *rdev = qp1_qp->rdev;
2875 struct bnxt_re_sqp_entries *sqp_entry = NULL;
2876 struct bnxt_re_qp *qp = rdev->qp1_sqp;
2877 struct ib_send_wr *swr;
2878 struct ib_ud_wr udwr;
2879 struct ib_recv_wr rwr;
2883 dma_addr_t rq_hdr_buf_map;
2884 dma_addr_t shrq_hdr_buf_map;
2887 struct ib_sge s_sge[2];
2888 struct ib_sge r_sge[2];
2891 memset(&udwr, 0, sizeof(udwr));
2892 memset(&rwr, 0, sizeof(rwr));
2893 memset(&s_sge, 0, sizeof(s_sge));
2894 memset(&r_sge, 0, sizeof(r_sge));
2897 tbl_idx = cqe->wr_id;
2899 rq_hdr_buf = qp1_qp->qplib_qp.rq_hdr_buf +
2900 (tbl_idx * qp1_qp->qplib_qp.rq_hdr_buf_size);
2901 rq_hdr_buf_map = bnxt_qplib_get_qp_buf_from_index(&qp1_qp->qplib_qp,
2904 /* Shadow QP header buffer */
2905 shrq_hdr_buf_map = bnxt_qplib_get_qp_buf_from_index(&qp->qplib_qp,
2907 sqp_entry = &rdev->sqp_tbl[tbl_idx];
2909 /* Store this cqe */
2910 memcpy(&sqp_entry->cqe, cqe, sizeof(struct bnxt_qplib_cqe));
2911 sqp_entry->qp1_qp = qp1_qp;
2913 /* Find packet type from the cqe */
2915 pkt_type = bnxt_re_check_packet_type(cqe->raweth_qp1_flags,
2916 cqe->raweth_qp1_flags2);
2918 dev_err(rdev_to_dev(rdev), "Invalid packet\n");
2922 /* Adjust the offset for the user buffer and post in the rq */
2924 if (pkt_type == BNXT_RE_ROCEV2_IPV4_PACKET)
2928 * QP1 loopback packet has 4 bytes of internal header before
2929 * ether header. Skip these four bytes.
2931 if (bnxt_re_is_loopback_packet(rdev, rq_hdr_buf))
2934 /* First send SGE . Skip the ether header*/
2935 s_sge[0].addr = rq_hdr_buf_map + BNXT_QPLIB_MAX_QP1_RQ_ETH_HDR_SIZE
2937 s_sge[0].lkey = 0xFFFFFFFF;
2938 s_sge[0].length = offset ? BNXT_QPLIB_MAX_GRH_HDR_SIZE_IPV4 :
2939 BNXT_QPLIB_MAX_GRH_HDR_SIZE_IPV6;
2941 /* Second Send SGE */
2942 s_sge[1].addr = s_sge[0].addr + s_sge[0].length +
2943 BNXT_QPLIB_MAX_QP1_RQ_BDETH_HDR_SIZE;
2944 if (pkt_type != BNXT_RE_ROCE_V1_PACKET)
2946 s_sge[1].lkey = 0xFFFFFFFF;
2947 s_sge[1].length = 256;
2949 /* First recv SGE */
2951 r_sge[0].addr = shrq_hdr_buf_map;
2952 r_sge[0].lkey = 0xFFFFFFFF;
2953 r_sge[0].length = 40;
2955 r_sge[1].addr = sqp_entry->sge.addr + offset;
2956 r_sge[1].lkey = sqp_entry->sge.lkey;
2957 r_sge[1].length = BNXT_QPLIB_MAX_GRH_HDR_SIZE_IPV6 + 256 - offset;
2959 /* Create receive work request */
2961 rwr.sg_list = r_sge;
2962 rwr.wr_id = tbl_idx;
2965 rc = bnxt_re_post_recv_shadow_qp(rdev, qp, &rwr);
2967 dev_err(rdev_to_dev(rdev),
2968 "Failed to post Rx buffers to shadow QP");
2973 swr->sg_list = s_sge;
2974 swr->wr_id = tbl_idx;
2975 swr->opcode = IB_WR_SEND;
2978 udwr.ah = &rdev->sqp_ah->ib_ah;
2979 udwr.remote_qpn = rdev->qp1_sqp->qplib_qp.id;
2980 udwr.remote_qkey = rdev->qp1_sqp->qplib_qp.qkey;
2982 /* post data received in the send queue */
2983 rc = bnxt_re_post_send_shadow_qp(rdev, qp, swr);
2988 static void bnxt_re_process_res_rawqp1_wc(struct ib_wc *wc,
2989 struct bnxt_qplib_cqe *cqe)
2991 wc->opcode = IB_WC_RECV;
2992 wc->status = __rawqp1_to_ib_wc_status(cqe->status);
2993 wc->wc_flags |= IB_WC_GRH;
2996 static bool bnxt_re_is_vlan_pkt(struct bnxt_qplib_cqe *orig_cqe,
3003 metadata = orig_cqe->raweth_qp1_metadata;
3004 if (orig_cqe->raweth_qp1_flags2 &
3005 CQ_RES_RAWETH_QP1_RAWETH_QP1_FLAGS2_META_FORMAT_VLAN) {
3007 CQ_RES_RAWETH_QP1_RAWETH_QP1_METADATA_TPID_MASK) >>
3008 CQ_RES_RAWETH_QP1_RAWETH_QP1_METADATA_TPID_SFT);
3009 if (tpid == ETH_P_8021Q) {
3011 CQ_RES_RAWETH_QP1_RAWETH_QP1_METADATA_VID_MASK;
3013 CQ_RES_RAWETH_QP1_RAWETH_QP1_METADATA_PRI_MASK) >>
3014 CQ_RES_RAWETH_QP1_RAWETH_QP1_METADATA_PRI_SFT;
3022 static void bnxt_re_process_res_rc_wc(struct ib_wc *wc,
3023 struct bnxt_qplib_cqe *cqe)
3025 wc->opcode = IB_WC_RECV;
3026 wc->status = __rc_to_ib_wc_status(cqe->status);
3028 if (cqe->flags & CQ_RES_RC_FLAGS_IMM)
3029 wc->wc_flags |= IB_WC_WITH_IMM;
3030 if (cqe->flags & CQ_RES_RC_FLAGS_INV)
3031 wc->wc_flags |= IB_WC_WITH_INVALIDATE;
3032 if ((cqe->flags & (CQ_RES_RC_FLAGS_RDMA | CQ_RES_RC_FLAGS_IMM)) ==
3033 (CQ_RES_RC_FLAGS_RDMA | CQ_RES_RC_FLAGS_IMM))
3034 wc->opcode = IB_WC_RECV_RDMA_WITH_IMM;
3037 static void bnxt_re_process_res_shadow_qp_wc(struct bnxt_re_qp *qp,
3039 struct bnxt_qplib_cqe *cqe)
3041 struct bnxt_re_dev *rdev = qp->rdev;
3042 struct bnxt_re_qp *qp1_qp = NULL;
3043 struct bnxt_qplib_cqe *orig_cqe = NULL;
3044 struct bnxt_re_sqp_entries *sqp_entry = NULL;
3050 tbl_idx = cqe->wr_id;
3052 sqp_entry = &rdev->sqp_tbl[tbl_idx];
3053 qp1_qp = sqp_entry->qp1_qp;
3054 orig_cqe = &sqp_entry->cqe;
3056 wc->wr_id = sqp_entry->wrid;
3057 wc->byte_len = orig_cqe->length;
3058 wc->qp = &qp1_qp->ib_qp;
3060 wc->ex.imm_data = orig_cqe->immdata;
3061 wc->src_qp = orig_cqe->src_qp;
3062 memcpy(wc->smac, orig_cqe->smac, ETH_ALEN);
3063 if (bnxt_re_is_vlan_pkt(orig_cqe, &vlan_id, &sl)) {
3064 wc->vlan_id = vlan_id;
3066 wc->wc_flags |= IB_WC_WITH_VLAN;
3069 wc->vendor_err = orig_cqe->status;
3071 wc->opcode = IB_WC_RECV;
3072 wc->status = __rawqp1_to_ib_wc_status(orig_cqe->status);
3073 wc->wc_flags |= IB_WC_GRH;
3075 nw_type = bnxt_re_check_packet_type(orig_cqe->raweth_qp1_flags,
3076 orig_cqe->raweth_qp1_flags2);
3078 wc->network_hdr_type = bnxt_re_to_ib_nw_type(nw_type);
3079 wc->wc_flags |= IB_WC_WITH_NETWORK_HDR_TYPE;
3083 static void bnxt_re_process_res_ud_wc(struct bnxt_re_qp *qp,
3085 struct bnxt_qplib_cqe *cqe)
3089 wc->opcode = IB_WC_RECV;
3090 wc->status = __rc_to_ib_wc_status(cqe->status);
3092 if (cqe->flags & CQ_RES_UD_FLAGS_IMM)
3093 wc->wc_flags |= IB_WC_WITH_IMM;
3094 /* report only on GSI QP for Thor */
3095 if (qp->qplib_qp.type == CMDQ_CREATE_QP_TYPE_GSI) {
3096 wc->wc_flags |= IB_WC_GRH;
3097 memcpy(wc->smac, cqe->smac, ETH_ALEN);
3098 wc->wc_flags |= IB_WC_WITH_SMAC;
3099 if (cqe->flags & CQ_RES_UD_FLAGS_META_FORMAT_VLAN) {
3100 wc->vlan_id = (cqe->cfa_meta & 0xFFF);
3101 if (wc->vlan_id < 0x1000)
3102 wc->wc_flags |= IB_WC_WITH_VLAN;
3104 nw_type = (cqe->flags & CQ_RES_UD_FLAGS_ROCE_IP_VER_MASK) >>
3105 CQ_RES_UD_FLAGS_ROCE_IP_VER_SFT;
3106 wc->network_hdr_type = bnxt_re_to_ib_nw_type(nw_type);
3107 wc->wc_flags |= IB_WC_WITH_NETWORK_HDR_TYPE;
3112 static int send_phantom_wqe(struct bnxt_re_qp *qp)
3114 struct bnxt_qplib_qp *lib_qp = &qp->qplib_qp;
3115 unsigned long flags;
3118 spin_lock_irqsave(&qp->sq_lock, flags);
3120 rc = bnxt_re_bind_fence_mw(lib_qp);
3122 lib_qp->sq.phantom_wqe_cnt++;
3123 dev_dbg(&lib_qp->sq.hwq.pdev->dev,
3124 "qp %#x sq->prod %#x sw_prod %#x phantom_wqe_cnt %d\n",
3125 lib_qp->id, lib_qp->sq.hwq.prod,
3126 HWQ_CMP(lib_qp->sq.hwq.prod, &lib_qp->sq.hwq),
3127 lib_qp->sq.phantom_wqe_cnt);
3130 spin_unlock_irqrestore(&qp->sq_lock, flags);
3134 int bnxt_re_poll_cq(struct ib_cq *ib_cq, int num_entries, struct ib_wc *wc)
3136 struct bnxt_re_cq *cq = container_of(ib_cq, struct bnxt_re_cq, ib_cq);
3137 struct bnxt_re_qp *qp;
3138 struct bnxt_qplib_cqe *cqe;
3139 int i, ncqe, budget;
3140 struct bnxt_qplib_q *sq;
3141 struct bnxt_qplib_qp *lib_qp;
3143 struct bnxt_re_sqp_entries *sqp_entry = NULL;
3144 unsigned long flags;
3146 spin_lock_irqsave(&cq->cq_lock, flags);
3147 budget = min_t(u32, num_entries, cq->max_cql);
3148 num_entries = budget;
3150 dev_err(rdev_to_dev(cq->rdev), "POLL CQ : no CQL to use");
3156 ncqe = bnxt_qplib_poll_cq(&cq->qplib_cq, cqe, budget, &lib_qp);
3159 if (sq->send_phantom) {
3160 qp = container_of(lib_qp,
3161 struct bnxt_re_qp, qplib_qp);
3162 if (send_phantom_wqe(qp) == -ENOMEM)
3163 dev_err(rdev_to_dev(cq->rdev),
3164 "Phantom failed! Scheduled to send again\n");
3166 sq->send_phantom = false;
3170 ncqe += bnxt_qplib_process_flush_list(&cq->qplib_cq,
3177 for (i = 0; i < ncqe; i++, cqe++) {
3178 /* Transcribe each qplib_wqe back to ib_wc */
3179 memset(wc, 0, sizeof(*wc));
3181 wc->wr_id = cqe->wr_id;
3182 wc->byte_len = cqe->length;
3184 ((struct bnxt_qplib_qp *)
3185 (unsigned long)(cqe->qp_handle),
3186 struct bnxt_re_qp, qplib_qp);
3188 dev_err(rdev_to_dev(cq->rdev),
3189 "POLL CQ : bad QP handle");
3192 wc->qp = &qp->ib_qp;
3193 wc->ex.imm_data = cqe->immdata;
3194 wc->src_qp = cqe->src_qp;
3195 memcpy(wc->smac, cqe->smac, ETH_ALEN);
3197 wc->vendor_err = cqe->status;
3199 switch (cqe->opcode) {
3200 case CQ_BASE_CQE_TYPE_REQ:
3201 if (qp->rdev->qp1_sqp && qp->qplib_qp.id ==
3202 qp->rdev->qp1_sqp->qplib_qp.id) {
3203 /* Handle this completion with
3204 * the stored completion
3206 memset(wc, 0, sizeof(*wc));
3209 bnxt_re_process_req_wc(wc, cqe);
3211 case CQ_BASE_CQE_TYPE_RES_RAWETH_QP1:
3215 rc = bnxt_re_process_raw_qp_pkt_rx
3218 memset(wc, 0, sizeof(*wc));
3223 /* Errors need not be looped back.
3224 * But change the wr_id to the one
3225 * stored in the table
3227 tbl_idx = cqe->wr_id;
3228 sqp_entry = &cq->rdev->sqp_tbl[tbl_idx];
3229 wc->wr_id = sqp_entry->wrid;
3230 bnxt_re_process_res_rawqp1_wc(wc, cqe);
3232 case CQ_BASE_CQE_TYPE_RES_RC:
3233 bnxt_re_process_res_rc_wc(wc, cqe);
3235 case CQ_BASE_CQE_TYPE_RES_UD:
3236 if (qp->rdev->qp1_sqp && qp->qplib_qp.id ==
3237 qp->rdev->qp1_sqp->qplib_qp.id) {
3238 /* Handle this completion with
3239 * the stored completion
3244 bnxt_re_process_res_shadow_qp_wc
3249 bnxt_re_process_res_ud_wc(qp, wc, cqe);
3252 dev_err(rdev_to_dev(cq->rdev),
3253 "POLL CQ : type 0x%x not handled",
3262 spin_unlock_irqrestore(&cq->cq_lock, flags);
3263 return num_entries - budget;
3266 int bnxt_re_req_notify_cq(struct ib_cq *ib_cq,
3267 enum ib_cq_notify_flags ib_cqn_flags)
3269 struct bnxt_re_cq *cq = container_of(ib_cq, struct bnxt_re_cq, ib_cq);
3270 int type = 0, rc = 0;
3271 unsigned long flags;
3273 spin_lock_irqsave(&cq->cq_lock, flags);
3274 /* Trigger on the very next completion */
3275 if (ib_cqn_flags & IB_CQ_NEXT_COMP)
3276 type = DBC_DBC_TYPE_CQ_ARMALL;
3277 /* Trigger on the next solicited completion */
3278 else if (ib_cqn_flags & IB_CQ_SOLICITED)
3279 type = DBC_DBC_TYPE_CQ_ARMSE;
3281 /* Poll to see if there are missed events */
3282 if ((ib_cqn_flags & IB_CQ_REPORT_MISSED_EVENTS) &&
3283 !(bnxt_qplib_is_cq_empty(&cq->qplib_cq))) {
3287 bnxt_qplib_req_notify_cq(&cq->qplib_cq, type);
3290 spin_unlock_irqrestore(&cq->cq_lock, flags);
3294 /* Memory Regions */
3295 struct ib_mr *bnxt_re_get_dma_mr(struct ib_pd *ib_pd, int mr_access_flags)
3297 struct bnxt_re_pd *pd = container_of(ib_pd, struct bnxt_re_pd, ib_pd);
3298 struct bnxt_re_dev *rdev = pd->rdev;
3299 struct bnxt_re_mr *mr;
3303 mr = kzalloc(sizeof(*mr), GFP_KERNEL);
3305 return ERR_PTR(-ENOMEM);
3308 mr->qplib_mr.pd = &pd->qplib_pd;
3309 mr->qplib_mr.flags = __from_ib_access_flags(mr_access_flags);
3310 mr->qplib_mr.type = CMDQ_ALLOCATE_MRW_MRW_FLAGS_PMR;
3312 /* Allocate and register 0 as the address */
3313 rc = bnxt_qplib_alloc_mrw(&rdev->qplib_res, &mr->qplib_mr);
3317 mr->qplib_mr.hwq.level = PBL_LVL_MAX;
3318 mr->qplib_mr.total_size = -1; /* Infinte length */
3319 rc = bnxt_qplib_reg_mr(&rdev->qplib_res, &mr->qplib_mr, &pbl, 0, false,
3324 mr->ib_mr.lkey = mr->qplib_mr.lkey;
3325 if (mr_access_flags & (IB_ACCESS_REMOTE_WRITE | IB_ACCESS_REMOTE_READ |
3326 IB_ACCESS_REMOTE_ATOMIC))
3327 mr->ib_mr.rkey = mr->ib_mr.lkey;
3328 atomic_inc(&rdev->mr_count);
3333 bnxt_qplib_free_mrw(&rdev->qplib_res, &mr->qplib_mr);
3339 int bnxt_re_dereg_mr(struct ib_mr *ib_mr, struct ib_udata *udata)
3341 struct bnxt_re_mr *mr = container_of(ib_mr, struct bnxt_re_mr, ib_mr);
3342 struct bnxt_re_dev *rdev = mr->rdev;
3345 rc = bnxt_qplib_free_mrw(&rdev->qplib_res, &mr->qplib_mr);
3347 dev_err(rdev_to_dev(rdev), "Dereg MR failed: %#x\n", rc);
3350 rc = bnxt_qplib_free_fast_reg_page_list(&rdev->qplib_res,
3356 if (!IS_ERR_OR_NULL(mr->ib_umem))
3357 ib_umem_release(mr->ib_umem);
3360 atomic_dec(&rdev->mr_count);
3364 static int bnxt_re_set_page(struct ib_mr *ib_mr, u64 addr)
3366 struct bnxt_re_mr *mr = container_of(ib_mr, struct bnxt_re_mr, ib_mr);
3368 if (unlikely(mr->npages == mr->qplib_frpl.max_pg_ptrs))
3371 mr->pages[mr->npages++] = addr;
3375 int bnxt_re_map_mr_sg(struct ib_mr *ib_mr, struct scatterlist *sg, int sg_nents,
3376 unsigned int *sg_offset)
3378 struct bnxt_re_mr *mr = container_of(ib_mr, struct bnxt_re_mr, ib_mr);
3381 return ib_sg_to_pages(ib_mr, sg, sg_nents, sg_offset, bnxt_re_set_page);
3384 struct ib_mr *bnxt_re_alloc_mr(struct ib_pd *ib_pd, enum ib_mr_type type,
3385 u32 max_num_sg, struct ib_udata *udata)
3387 struct bnxt_re_pd *pd = container_of(ib_pd, struct bnxt_re_pd, ib_pd);
3388 struct bnxt_re_dev *rdev = pd->rdev;
3389 struct bnxt_re_mr *mr = NULL;
3392 if (type != IB_MR_TYPE_MEM_REG) {
3393 dev_dbg(rdev_to_dev(rdev), "MR type 0x%x not supported", type);
3394 return ERR_PTR(-EINVAL);
3396 if (max_num_sg > MAX_PBL_LVL_1_PGS)
3397 return ERR_PTR(-EINVAL);
3399 mr = kzalloc(sizeof(*mr), GFP_KERNEL);
3401 return ERR_PTR(-ENOMEM);
3404 mr->qplib_mr.pd = &pd->qplib_pd;
3405 mr->qplib_mr.flags = BNXT_QPLIB_FR_PMR;
3406 mr->qplib_mr.type = CMDQ_ALLOCATE_MRW_MRW_FLAGS_PMR;
3408 rc = bnxt_qplib_alloc_mrw(&rdev->qplib_res, &mr->qplib_mr);
3412 mr->ib_mr.lkey = mr->qplib_mr.lkey;
3413 mr->ib_mr.rkey = mr->ib_mr.lkey;
3415 mr->pages = kcalloc(max_num_sg, sizeof(u64), GFP_KERNEL);
3420 rc = bnxt_qplib_alloc_fast_reg_page_list(&rdev->qplib_res,
3421 &mr->qplib_frpl, max_num_sg);
3423 dev_err(rdev_to_dev(rdev),
3424 "Failed to allocate HW FR page list");
3428 atomic_inc(&rdev->mr_count);
3434 bnxt_qplib_free_mrw(&rdev->qplib_res, &mr->qplib_mr);
3440 struct ib_mw *bnxt_re_alloc_mw(struct ib_pd *ib_pd, enum ib_mw_type type,
3441 struct ib_udata *udata)
3443 struct bnxt_re_pd *pd = container_of(ib_pd, struct bnxt_re_pd, ib_pd);
3444 struct bnxt_re_dev *rdev = pd->rdev;
3445 struct bnxt_re_mw *mw;
3448 mw = kzalloc(sizeof(*mw), GFP_KERNEL);
3450 return ERR_PTR(-ENOMEM);
3452 mw->qplib_mw.pd = &pd->qplib_pd;
3454 mw->qplib_mw.type = (type == IB_MW_TYPE_1 ?
3455 CMDQ_ALLOCATE_MRW_MRW_FLAGS_MW_TYPE1 :
3456 CMDQ_ALLOCATE_MRW_MRW_FLAGS_MW_TYPE2B);
3457 rc = bnxt_qplib_alloc_mrw(&rdev->qplib_res, &mw->qplib_mw);
3459 dev_err(rdev_to_dev(rdev), "Allocate MW failed!");
3462 mw->ib_mw.rkey = mw->qplib_mw.rkey;
3464 atomic_inc(&rdev->mw_count);
3472 int bnxt_re_dealloc_mw(struct ib_mw *ib_mw)
3474 struct bnxt_re_mw *mw = container_of(ib_mw, struct bnxt_re_mw, ib_mw);
3475 struct bnxt_re_dev *rdev = mw->rdev;
3478 rc = bnxt_qplib_free_mrw(&rdev->qplib_res, &mw->qplib_mw);
3480 dev_err(rdev_to_dev(rdev), "Free MW failed: %#x\n", rc);
3485 atomic_dec(&rdev->mw_count);
3489 static int bnxt_re_page_size_ok(int page_shift)
3491 switch (page_shift) {
3492 case CMDQ_REGISTER_MR_LOG2_PBL_PG_SIZE_PG_4K:
3493 case CMDQ_REGISTER_MR_LOG2_PBL_PG_SIZE_PG_8K:
3494 case CMDQ_REGISTER_MR_LOG2_PBL_PG_SIZE_PG_64K:
3495 case CMDQ_REGISTER_MR_LOG2_PBL_PG_SIZE_PG_2M:
3496 case CMDQ_REGISTER_MR_LOG2_PBL_PG_SIZE_PG_256K:
3497 case CMDQ_REGISTER_MR_LOG2_PBL_PG_SIZE_PG_1M:
3498 case CMDQ_REGISTER_MR_LOG2_PBL_PG_SIZE_PG_4M:
3499 case CMDQ_REGISTER_MR_LOG2_PBL_PG_SIZE_PG_1G:
3506 static int fill_umem_pbl_tbl(struct ib_umem *umem, u64 *pbl_tbl_orig,
3509 u64 *pbl_tbl = pbl_tbl_orig;
3510 u64 page_size = BIT_ULL(page_shift);
3511 struct ib_block_iter biter;
3513 rdma_for_each_block(umem->sg_head.sgl, &biter, umem->nmap, page_size)
3514 *pbl_tbl++ = rdma_block_iter_dma_address(&biter);
3516 return pbl_tbl - pbl_tbl_orig;
3520 struct ib_mr *bnxt_re_reg_user_mr(struct ib_pd *ib_pd, u64 start, u64 length,
3521 u64 virt_addr, int mr_access_flags,
3522 struct ib_udata *udata)
3524 struct bnxt_re_pd *pd = container_of(ib_pd, struct bnxt_re_pd, ib_pd);
3525 struct bnxt_re_dev *rdev = pd->rdev;
3526 struct bnxt_re_mr *mr;
3527 struct ib_umem *umem;
3528 u64 *pbl_tbl = NULL;
3529 int umem_pgs, page_shift, rc;
3531 if (length > BNXT_RE_MAX_MR_SIZE) {
3532 dev_err(rdev_to_dev(rdev), "MR Size: %lld > Max supported:%lld\n",
3533 length, BNXT_RE_MAX_MR_SIZE);
3534 return ERR_PTR(-ENOMEM);
3537 mr = kzalloc(sizeof(*mr), GFP_KERNEL);
3539 return ERR_PTR(-ENOMEM);
3542 mr->qplib_mr.pd = &pd->qplib_pd;
3543 mr->qplib_mr.flags = __from_ib_access_flags(mr_access_flags);
3544 mr->qplib_mr.type = CMDQ_ALLOCATE_MRW_MRW_FLAGS_MR;
3546 rc = bnxt_qplib_alloc_mrw(&rdev->qplib_res, &mr->qplib_mr);
3548 dev_err(rdev_to_dev(rdev), "Failed to allocate MR");
3551 /* The fixed portion of the rkey is the same as the lkey */
3552 mr->ib_mr.rkey = mr->qplib_mr.rkey;
3554 umem = ib_umem_get(udata, start, length, mr_access_flags, 0);
3556 dev_err(rdev_to_dev(rdev), "Failed to get umem");
3562 mr->qplib_mr.va = virt_addr;
3563 umem_pgs = ib_umem_page_count(umem);
3565 dev_err(rdev_to_dev(rdev), "umem is invalid!");
3569 mr->qplib_mr.total_size = length;
3571 pbl_tbl = kcalloc(umem_pgs, sizeof(u64 *), GFP_KERNEL);
3577 page_shift = __ffs(ib_umem_find_best_pgsz(umem,
3578 BNXT_RE_PAGE_SIZE_4K | BNXT_RE_PAGE_SIZE_2M,
3581 if (!bnxt_re_page_size_ok(page_shift)) {
3582 dev_err(rdev_to_dev(rdev), "umem page size unsupported!");
3587 if (page_shift == BNXT_RE_PAGE_SHIFT_4K &&
3588 length > BNXT_RE_MAX_MR_SIZE_LOW) {
3589 dev_err(rdev_to_dev(rdev), "Requested MR Sz:%llu Max sup:%llu",
3590 length, (u64)BNXT_RE_MAX_MR_SIZE_LOW);
3595 /* Map umem buf ptrs to the PBL */
3596 umem_pgs = fill_umem_pbl_tbl(umem, pbl_tbl, page_shift);
3597 rc = bnxt_qplib_reg_mr(&rdev->qplib_res, &mr->qplib_mr, pbl_tbl,
3598 umem_pgs, false, 1 << page_shift);
3600 dev_err(rdev_to_dev(rdev), "Failed to register user MR");
3606 mr->ib_mr.lkey = mr->qplib_mr.lkey;
3607 mr->ib_mr.rkey = mr->qplib_mr.lkey;
3608 atomic_inc(&rdev->mr_count);
3614 ib_umem_release(umem);
3616 bnxt_qplib_free_mrw(&rdev->qplib_res, &mr->qplib_mr);
3622 int bnxt_re_alloc_ucontext(struct ib_ucontext *ctx, struct ib_udata *udata)
3624 struct ib_device *ibdev = ctx->device;
3625 struct bnxt_re_ucontext *uctx =
3626 container_of(ctx, struct bnxt_re_ucontext, ib_uctx);
3627 struct bnxt_re_dev *rdev = to_bnxt_re_dev(ibdev, ibdev);
3628 struct bnxt_qplib_dev_attr *dev_attr = &rdev->dev_attr;
3629 struct bnxt_re_uctx_resp resp;
3630 u32 chip_met_rev_num = 0;
3633 dev_dbg(rdev_to_dev(rdev), "ABI version requested %d",
3634 ibdev->uverbs_abi_ver);
3636 if (ibdev->uverbs_abi_ver != BNXT_RE_ABI_VERSION) {
3637 dev_dbg(rdev_to_dev(rdev), " is different from the device %d ",
3638 BNXT_RE_ABI_VERSION);
3644 uctx->shpg = (void *)__get_free_page(GFP_KERNEL);
3649 spin_lock_init(&uctx->sh_lock);
3651 resp.comp_mask = BNXT_RE_UCNTX_CMASK_HAVE_CCTX;
3652 chip_met_rev_num = rdev->chip_ctx.chip_num;
3653 chip_met_rev_num |= ((u32)rdev->chip_ctx.chip_rev & 0xFF) <<
3654 BNXT_RE_CHIP_ID0_CHIP_REV_SFT;
3655 chip_met_rev_num |= ((u32)rdev->chip_ctx.chip_metal & 0xFF) <<
3656 BNXT_RE_CHIP_ID0_CHIP_MET_SFT;
3657 resp.chip_id0 = chip_met_rev_num;
3658 /* Future extension of chip info */
3660 /*Temp, Use xa_alloc instead */
3661 resp.dev_id = rdev->en_dev->pdev->devfn;
3662 resp.max_qp = rdev->qplib_ctx.qpc_count;
3663 resp.pg_size = PAGE_SIZE;
3664 resp.cqe_sz = sizeof(struct cq_base);
3665 resp.max_cqd = dev_attr->max_cq_wqes;
3668 rc = ib_copy_to_udata(udata, &resp, min(udata->outlen, sizeof(resp)));
3670 dev_err(rdev_to_dev(rdev), "Failed to copy user context");
3677 free_page((unsigned long)uctx->shpg);
3683 void bnxt_re_dealloc_ucontext(struct ib_ucontext *ib_uctx)
3685 struct bnxt_re_ucontext *uctx = container_of(ib_uctx,
3686 struct bnxt_re_ucontext,
3689 struct bnxt_re_dev *rdev = uctx->rdev;
3692 free_page((unsigned long)uctx->shpg);
3694 if (uctx->dpi.dbr) {
3695 /* Free DPI only if this is the first PD allocated by the
3696 * application and mark the context dpi as NULL
3698 bnxt_qplib_dealloc_dpi(&rdev->qplib_res,
3699 &rdev->qplib_res.dpi_tbl, &uctx->dpi);
3700 uctx->dpi.dbr = NULL;
3704 /* Helper function to mmap the virtual memory from user app */
3705 int bnxt_re_mmap(struct ib_ucontext *ib_uctx, struct vm_area_struct *vma)
3707 struct bnxt_re_ucontext *uctx = container_of(ib_uctx,
3708 struct bnxt_re_ucontext,
3710 struct bnxt_re_dev *rdev = uctx->rdev;
3713 if (vma->vm_end - vma->vm_start != PAGE_SIZE)
3716 if (vma->vm_pgoff) {
3717 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
3718 if (io_remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff,
3719 PAGE_SIZE, vma->vm_page_prot)) {
3720 dev_err(rdev_to_dev(rdev), "Failed to map DPI");
3724 pfn = virt_to_phys(uctx->shpg) >> PAGE_SHIFT;
3725 if (remap_pfn_range(vma, vma->vm_start,
3726 pfn, PAGE_SIZE, vma->vm_page_prot)) {
3727 dev_err(rdev_to_dev(rdev),
3728 "Failed to map shared page");