1 // SPDX-License-Identifier: GPL-2.0
3 /* Authors: Cheng Xu <chengyou@linux.alibaba.com> */
4 /* Kai Shen <kaishen@linux.alibaba.com> */
5 /* Copyright (c) 2020-2022, Alibaba Group. */
7 /* Authors: Bernard Metzler <bmt@zurich.ibm.com> */
8 /* Copyright (c) 2008-2019, IBM Corporation */
10 /* Copyright (c) 2013-2015, Mellanox Technologies. All rights reserved. */
12 #include <linux/vmalloc.h>
13 #include <net/addrconf.h>
14 #include <rdma/erdma-abi.h>
15 #include <rdma/ib_umem.h>
16 #include <rdma/uverbs_ioctl.h>
20 #include "erdma_verbs.h"
22 static void assemble_qbuf_mtt_for_cmd(struct erdma_mem *mem, u32 *cfg,
23 u64 *addr0, u64 *addr1)
25 struct erdma_mtt *mtt = mem->mtt;
27 if (mem->mtt_nents > ERDMA_MAX_INLINE_MTT_ENTRIES) {
28 *addr0 = mtt->buf_dma;
29 *cfg |= FIELD_PREP(ERDMA_CMD_CREATE_QP_MTT_LEVEL_MASK,
33 memcpy(addr1, mtt->buf + 1, MTT_SIZE(mem->mtt_nents - 1));
34 *cfg |= FIELD_PREP(ERDMA_CMD_CREATE_QP_MTT_LEVEL_MASK,
39 static int create_qp_cmd(struct erdma_ucontext *uctx, struct erdma_qp *qp)
41 struct erdma_dev *dev = to_edev(qp->ibqp.device);
42 struct erdma_pd *pd = to_epd(qp->ibqp.pd);
43 struct erdma_cmdq_create_qp_req req;
44 struct erdma_uqp *user_qp;
48 erdma_cmdq_build_reqhdr(&req.hdr, CMDQ_SUBMOD_RDMA,
49 CMDQ_OPCODE_CREATE_QP);
51 req.cfg0 = FIELD_PREP(ERDMA_CMD_CREATE_QP_SQ_DEPTH_MASK,
52 ilog2(qp->attrs.sq_size)) |
53 FIELD_PREP(ERDMA_CMD_CREATE_QP_QPN_MASK, QP_ID(qp));
54 req.cfg1 = FIELD_PREP(ERDMA_CMD_CREATE_QP_RQ_DEPTH_MASK,
55 ilog2(qp->attrs.rq_size)) |
56 FIELD_PREP(ERDMA_CMD_CREATE_QP_PD_MASK, pd->pdn);
58 if (rdma_is_kernel_res(&qp->ibqp.res)) {
59 u32 pgsz_range = ilog2(SZ_1M) - ERDMA_HW_PAGE_SHIFT;
62 FIELD_PREP(ERDMA_CMD_CREATE_QP_PAGE_SIZE_MASK,
64 FIELD_PREP(ERDMA_CMD_CREATE_QP_CQN_MASK, qp->scq->cqn);
66 FIELD_PREP(ERDMA_CMD_CREATE_QP_PAGE_SIZE_MASK,
68 FIELD_PREP(ERDMA_CMD_CREATE_QP_CQN_MASK, qp->rcq->cqn);
71 FIELD_PREP(ERDMA_CMD_CREATE_QP_PAGE_OFFSET_MASK, 0) |
72 FIELD_PREP(ERDMA_CMD_CREATE_QP_MTT_CNT_MASK, 1) |
73 FIELD_PREP(ERDMA_CMD_CREATE_QP_MTT_LEVEL_MASK,
75 req.rq_mtt_cfg = req.sq_mtt_cfg;
77 req.rq_buf_addr = qp->kern_qp.rq_buf_dma_addr;
78 req.sq_buf_addr = qp->kern_qp.sq_buf_dma_addr;
79 req.sq_db_info_dma_addr = qp->kern_qp.sq_buf_dma_addr +
80 (qp->attrs.sq_size << SQEBB_SHIFT);
81 req.rq_db_info_dma_addr = qp->kern_qp.rq_buf_dma_addr +
82 (qp->attrs.rq_size << RQE_SHIFT);
84 user_qp = &qp->user_qp;
85 req.sq_cqn_mtt_cfg = FIELD_PREP(
86 ERDMA_CMD_CREATE_QP_PAGE_SIZE_MASK,
87 ilog2(user_qp->sq_mem.page_size) - ERDMA_HW_PAGE_SHIFT);
89 FIELD_PREP(ERDMA_CMD_CREATE_QP_CQN_MASK, qp->scq->cqn);
91 req.rq_cqn_mtt_cfg = FIELD_PREP(
92 ERDMA_CMD_CREATE_QP_PAGE_SIZE_MASK,
93 ilog2(user_qp->rq_mem.page_size) - ERDMA_HW_PAGE_SHIFT);
95 FIELD_PREP(ERDMA_CMD_CREATE_QP_CQN_MASK, qp->rcq->cqn);
97 req.sq_mtt_cfg = user_qp->sq_mem.page_offset;
98 req.sq_mtt_cfg |= FIELD_PREP(ERDMA_CMD_CREATE_QP_MTT_CNT_MASK,
99 user_qp->sq_mem.mtt_nents);
101 req.rq_mtt_cfg = user_qp->rq_mem.page_offset;
102 req.rq_mtt_cfg |= FIELD_PREP(ERDMA_CMD_CREATE_QP_MTT_CNT_MASK,
103 user_qp->rq_mem.mtt_nents);
105 assemble_qbuf_mtt_for_cmd(&user_qp->sq_mem, &req.sq_mtt_cfg,
106 &req.sq_buf_addr, req.sq_mtt_entry);
107 assemble_qbuf_mtt_for_cmd(&user_qp->rq_mem, &req.rq_mtt_cfg,
108 &req.rq_buf_addr, req.rq_mtt_entry);
110 req.sq_db_info_dma_addr = user_qp->sq_db_info_dma_addr;
111 req.rq_db_info_dma_addr = user_qp->rq_db_info_dma_addr;
113 if (uctx->ext_db.enable) {
114 req.sq_cqn_mtt_cfg |=
115 FIELD_PREP(ERDMA_CMD_CREATE_QP_DB_CFG_MASK, 1);
117 FIELD_PREP(ERDMA_CMD_CREATE_QP_SQDB_CFG_MASK,
118 uctx->ext_db.sdb_off) |
119 FIELD_PREP(ERDMA_CMD_CREATE_QP_RQDB_CFG_MASK,
120 uctx->ext_db.rdb_off);
124 err = erdma_post_cmd_wait(&dev->cmdq, &req, sizeof(req), &resp0,
128 FIELD_GET(ERDMA_CMDQ_CREATE_QP_RESP_COOKIE_MASK, resp0);
133 static int regmr_cmd(struct erdma_dev *dev, struct erdma_mr *mr)
135 struct erdma_pd *pd = to_epd(mr->ibmr.pd);
136 struct erdma_cmdq_reg_mr_req req;
139 erdma_cmdq_build_reqhdr(&req.hdr, CMDQ_SUBMOD_RDMA, CMDQ_OPCODE_REG_MR);
141 if (mr->type == ERDMA_MR_TYPE_FRMR ||
142 mr->mem.page_cnt > ERDMA_MAX_INLINE_MTT_ENTRIES) {
143 if (mr->mem.mtt->continuous) {
144 req.phy_addr[0] = mr->mem.mtt->buf_dma;
145 mtt_level = ERDMA_MR_MTT_1LEVEL;
147 req.phy_addr[0] = sg_dma_address(mr->mem.mtt->sglist);
148 mtt_level = mr->mem.mtt->level;
151 memcpy(req.phy_addr, mr->mem.mtt->buf,
152 MTT_SIZE(mr->mem.page_cnt));
153 mtt_level = ERDMA_MR_MTT_0LEVEL;
156 req.cfg0 = FIELD_PREP(ERDMA_CMD_MR_VALID_MASK, mr->valid) |
157 FIELD_PREP(ERDMA_CMD_MR_KEY_MASK, mr->ibmr.lkey & 0xFF) |
158 FIELD_PREP(ERDMA_CMD_MR_MPT_IDX_MASK, mr->ibmr.lkey >> 8);
159 req.cfg1 = FIELD_PREP(ERDMA_CMD_REGMR_PD_MASK, pd->pdn) |
160 FIELD_PREP(ERDMA_CMD_REGMR_TYPE_MASK, mr->type) |
161 FIELD_PREP(ERDMA_CMD_REGMR_RIGHT_MASK, mr->access);
162 req.cfg2 = FIELD_PREP(ERDMA_CMD_REGMR_PAGESIZE_MASK,
163 ilog2(mr->mem.page_size)) |
164 FIELD_PREP(ERDMA_CMD_REGMR_MTT_LEVEL_MASK, mtt_level) |
165 FIELD_PREP(ERDMA_CMD_REGMR_MTT_CNT_MASK, mr->mem.page_cnt);
167 if (mr->type == ERDMA_MR_TYPE_DMA)
170 if (mr->type == ERDMA_MR_TYPE_NORMAL) {
171 req.start_va = mr->mem.va;
172 req.size = mr->mem.len;
175 if (!mr->mem.mtt->continuous && mr->mem.mtt->level > 1) {
176 req.cfg0 |= FIELD_PREP(ERDMA_CMD_MR_VERSION_MASK, 1);
177 req.cfg2 |= FIELD_PREP(ERDMA_CMD_REGMR_MTT_PAGESIZE_MASK,
178 PAGE_SHIFT - ERDMA_HW_PAGE_SHIFT);
179 req.size_h = upper_32_bits(mr->mem.len);
180 req.mtt_cnt_h = mr->mem.page_cnt >> 20;
184 return erdma_post_cmd_wait(&dev->cmdq, &req, sizeof(req), NULL, NULL);
187 static int create_cq_cmd(struct erdma_ucontext *uctx, struct erdma_cq *cq)
189 struct erdma_dev *dev = to_edev(cq->ibcq.device);
190 struct erdma_cmdq_create_cq_req req;
191 struct erdma_mem *mem;
194 erdma_cmdq_build_reqhdr(&req.hdr, CMDQ_SUBMOD_RDMA,
195 CMDQ_OPCODE_CREATE_CQ);
197 req.cfg0 = FIELD_PREP(ERDMA_CMD_CREATE_CQ_CQN_MASK, cq->cqn) |
198 FIELD_PREP(ERDMA_CMD_CREATE_CQ_DEPTH_MASK, ilog2(cq->depth));
199 req.cfg1 = FIELD_PREP(ERDMA_CMD_CREATE_CQ_EQN_MASK, cq->assoc_eqn);
201 if (rdma_is_kernel_res(&cq->ibcq.res)) {
203 req.cfg0 |= FIELD_PREP(ERDMA_CMD_CREATE_CQ_PAGESIZE_MASK,
204 ilog2(page_size) - ERDMA_HW_PAGE_SHIFT);
205 req.qbuf_addr_l = lower_32_bits(cq->kern_cq.qbuf_dma_addr);
206 req.qbuf_addr_h = upper_32_bits(cq->kern_cq.qbuf_dma_addr);
208 req.cfg1 |= FIELD_PREP(ERDMA_CMD_CREATE_CQ_MTT_CNT_MASK, 1) |
209 FIELD_PREP(ERDMA_CMD_CREATE_CQ_MTT_LEVEL_MASK,
210 ERDMA_MR_MTT_0LEVEL);
212 req.first_page_offset = 0;
213 req.cq_db_info_addr =
214 cq->kern_cq.qbuf_dma_addr + (cq->depth << CQE_SHIFT);
216 mem = &cq->user_cq.qbuf_mem;
218 FIELD_PREP(ERDMA_CMD_CREATE_CQ_PAGESIZE_MASK,
219 ilog2(mem->page_size) - ERDMA_HW_PAGE_SHIFT);
220 if (mem->mtt_nents == 1) {
221 req.qbuf_addr_l = lower_32_bits(mem->mtt->buf[0]);
222 req.qbuf_addr_h = upper_32_bits(mem->mtt->buf[0]);
224 FIELD_PREP(ERDMA_CMD_CREATE_CQ_MTT_LEVEL_MASK,
225 ERDMA_MR_MTT_0LEVEL);
227 req.qbuf_addr_l = lower_32_bits(mem->mtt->buf_dma);
228 req.qbuf_addr_h = upper_32_bits(mem->mtt->buf_dma);
230 FIELD_PREP(ERDMA_CMD_CREATE_CQ_MTT_LEVEL_MASK,
231 ERDMA_MR_MTT_1LEVEL);
233 req.cfg1 |= FIELD_PREP(ERDMA_CMD_CREATE_CQ_MTT_CNT_MASK,
236 req.first_page_offset = mem->page_offset;
237 req.cq_db_info_addr = cq->user_cq.db_info_dma_addr;
239 if (uctx->ext_db.enable) {
240 req.cfg1 |= FIELD_PREP(
241 ERDMA_CMD_CREATE_CQ_MTT_DB_CFG_MASK, 1);
242 req.cfg2 = FIELD_PREP(ERDMA_CMD_CREATE_CQ_DB_CFG_MASK,
243 uctx->ext_db.cdb_off);
247 return erdma_post_cmd_wait(&dev->cmdq, &req, sizeof(req), NULL, NULL);
250 static int erdma_alloc_idx(struct erdma_resource_cb *res_cb)
255 spin_lock_irqsave(&res_cb->lock, flags);
256 idx = find_next_zero_bit(res_cb->bitmap, res_cb->max_cap,
257 res_cb->next_alloc_idx);
258 if (idx == res_cb->max_cap) {
259 idx = find_first_zero_bit(res_cb->bitmap, res_cb->max_cap);
260 if (idx == res_cb->max_cap) {
261 res_cb->next_alloc_idx = 1;
262 spin_unlock_irqrestore(&res_cb->lock, flags);
267 set_bit(idx, res_cb->bitmap);
268 res_cb->next_alloc_idx = idx + 1;
269 spin_unlock_irqrestore(&res_cb->lock, flags);
274 static inline void erdma_free_idx(struct erdma_resource_cb *res_cb, u32 idx)
279 spin_lock_irqsave(&res_cb->lock, flags);
280 used = __test_and_clear_bit(idx, res_cb->bitmap);
281 spin_unlock_irqrestore(&res_cb->lock, flags);
285 static struct rdma_user_mmap_entry *
286 erdma_user_mmap_entry_insert(struct erdma_ucontext *uctx, void *address,
287 u32 size, u8 mmap_flag, u64 *mmap_offset)
289 struct erdma_user_mmap_entry *entry =
290 kzalloc(sizeof(*entry), GFP_KERNEL);
296 entry->address = (u64)address;
297 entry->mmap_flag = mmap_flag;
299 size = PAGE_ALIGN(size);
301 ret = rdma_user_mmap_entry_insert(&uctx->ibucontext, &entry->rdma_entry,
308 *mmap_offset = rdma_user_mmap_get_offset(&entry->rdma_entry);
310 return &entry->rdma_entry;
313 int erdma_query_device(struct ib_device *ibdev, struct ib_device_attr *attr,
314 struct ib_udata *unused)
316 struct erdma_dev *dev = to_edev(ibdev);
318 memset(attr, 0, sizeof(*attr));
320 attr->max_mr_size = dev->attrs.max_mr_size;
321 attr->vendor_id = PCI_VENDOR_ID_ALIBABA;
322 attr->vendor_part_id = dev->pdev->device;
323 attr->hw_ver = dev->pdev->revision;
324 attr->max_qp = dev->attrs.max_qp - 1;
325 attr->max_qp_wr = min(dev->attrs.max_send_wr, dev->attrs.max_recv_wr);
326 attr->max_qp_rd_atom = dev->attrs.max_ord;
327 attr->max_qp_init_rd_atom = dev->attrs.max_ird;
328 attr->max_res_rd_atom = dev->attrs.max_qp * dev->attrs.max_ird;
329 attr->device_cap_flags = IB_DEVICE_MEM_MGT_EXTENSIONS;
330 attr->kernel_cap_flags = IBK_LOCAL_DMA_LKEY;
331 ibdev->local_dma_lkey = dev->attrs.local_dma_key;
332 attr->max_send_sge = dev->attrs.max_send_sge;
333 attr->max_recv_sge = dev->attrs.max_recv_sge;
334 attr->max_sge_rd = dev->attrs.max_sge_rd;
335 attr->max_cq = dev->attrs.max_cq - 1;
336 attr->max_cqe = dev->attrs.max_cqe;
337 attr->max_mr = dev->attrs.max_mr;
338 attr->max_pd = dev->attrs.max_pd;
339 attr->max_mw = dev->attrs.max_mw;
340 attr->max_fast_reg_page_list_len = ERDMA_MAX_FRMR_PA;
341 attr->page_size_cap = ERDMA_PAGE_SIZE_SUPPORT;
343 if (dev->attrs.cap_flags & ERDMA_DEV_CAP_FLAGS_ATOMIC)
344 attr->atomic_cap = IB_ATOMIC_GLOB;
346 attr->fw_ver = dev->attrs.fw_version;
349 addrconf_addr_eui48((u8 *)&attr->sys_image_guid,
350 dev->netdev->dev_addr);
355 int erdma_query_gid(struct ib_device *ibdev, u32 port, int idx,
358 struct erdma_dev *dev = to_edev(ibdev);
360 memset(gid, 0, sizeof(*gid));
361 ether_addr_copy(gid->raw, dev->attrs.peer_addr);
366 int erdma_query_port(struct ib_device *ibdev, u32 port,
367 struct ib_port_attr *attr)
369 struct erdma_dev *dev = to_edev(ibdev);
370 struct net_device *ndev = dev->netdev;
372 memset(attr, 0, sizeof(*attr));
374 attr->gid_tbl_len = 1;
375 attr->port_cap_flags = IB_PORT_CM_SUP | IB_PORT_DEVICE_MGMT_SUP;
376 attr->max_msg_sz = -1;
381 ib_get_eth_speed(ibdev, port, &attr->active_speed, &attr->active_width);
382 attr->max_mtu = ib_mtu_int_to_enum(ndev->mtu);
383 attr->active_mtu = ib_mtu_int_to_enum(ndev->mtu);
384 if (netif_running(ndev) && netif_carrier_ok(ndev))
385 dev->state = IB_PORT_ACTIVE;
387 dev->state = IB_PORT_DOWN;
388 attr->state = dev->state;
391 if (dev->state == IB_PORT_ACTIVE)
392 attr->phys_state = IB_PORT_PHYS_STATE_LINK_UP;
394 attr->phys_state = IB_PORT_PHYS_STATE_DISABLED;
399 int erdma_get_port_immutable(struct ib_device *ibdev, u32 port,
400 struct ib_port_immutable *port_immutable)
402 port_immutable->gid_tbl_len = 1;
403 port_immutable->core_cap_flags = RDMA_CORE_PORT_IWARP;
408 int erdma_alloc_pd(struct ib_pd *ibpd, struct ib_udata *udata)
410 struct erdma_pd *pd = to_epd(ibpd);
411 struct erdma_dev *dev = to_edev(ibpd->device);
414 pdn = erdma_alloc_idx(&dev->res_cb[ERDMA_RES_TYPE_PD]);
423 int erdma_dealloc_pd(struct ib_pd *ibpd, struct ib_udata *udata)
425 struct erdma_pd *pd = to_epd(ibpd);
426 struct erdma_dev *dev = to_edev(ibpd->device);
428 erdma_free_idx(&dev->res_cb[ERDMA_RES_TYPE_PD], pd->pdn);
433 static void erdma_flush_worker(struct work_struct *work)
435 struct delayed_work *dwork = to_delayed_work(work);
436 struct erdma_qp *qp =
437 container_of(dwork, struct erdma_qp, reflush_dwork);
438 struct erdma_cmdq_reflush_req req;
440 erdma_cmdq_build_reqhdr(&req.hdr, CMDQ_SUBMOD_RDMA,
441 CMDQ_OPCODE_REFLUSH);
443 req.sq_pi = qp->kern_qp.sq_pi;
444 req.rq_pi = qp->kern_qp.rq_pi;
445 erdma_post_cmd_wait(&qp->dev->cmdq, &req, sizeof(req), NULL, NULL);
448 static int erdma_qp_validate_cap(struct erdma_dev *dev,
449 struct ib_qp_init_attr *attrs)
451 if ((attrs->cap.max_send_wr > dev->attrs.max_send_wr) ||
452 (attrs->cap.max_recv_wr > dev->attrs.max_recv_wr) ||
453 (attrs->cap.max_send_sge > dev->attrs.max_send_sge) ||
454 (attrs->cap.max_recv_sge > dev->attrs.max_recv_sge) ||
455 (attrs->cap.max_inline_data > ERDMA_MAX_INLINE) ||
456 !attrs->cap.max_send_wr || !attrs->cap.max_recv_wr) {
463 static int erdma_qp_validate_attr(struct erdma_dev *dev,
464 struct ib_qp_init_attr *attrs)
466 if (attrs->qp_type != IB_QPT_RC)
472 if (!attrs->send_cq || !attrs->recv_cq)
478 static void free_kernel_qp(struct erdma_qp *qp)
480 struct erdma_dev *dev = qp->dev;
482 vfree(qp->kern_qp.swr_tbl);
483 vfree(qp->kern_qp.rwr_tbl);
485 if (qp->kern_qp.sq_buf)
488 WARPPED_BUFSIZE(qp->attrs.sq_size << SQEBB_SHIFT),
489 qp->kern_qp.sq_buf, qp->kern_qp.sq_buf_dma_addr);
491 if (qp->kern_qp.rq_buf)
494 WARPPED_BUFSIZE(qp->attrs.rq_size << RQE_SHIFT),
495 qp->kern_qp.rq_buf, qp->kern_qp.rq_buf_dma_addr);
498 static int init_kernel_qp(struct erdma_dev *dev, struct erdma_qp *qp,
499 struct ib_qp_init_attr *attrs)
501 struct erdma_kqp *kqp = &qp->kern_qp;
504 if (attrs->sq_sig_type == IB_SIGNAL_ALL_WR)
512 dev->func_bar + (ERDMA_SDB_SHARED_PAGE_INDEX << PAGE_SHIFT);
513 kqp->hw_rq_db = dev->func_bar + ERDMA_BAR_RQDB_SPACE_OFFSET;
515 kqp->swr_tbl = vmalloc_array(qp->attrs.sq_size, sizeof(u64));
516 kqp->rwr_tbl = vmalloc_array(qp->attrs.rq_size, sizeof(u64));
517 if (!kqp->swr_tbl || !kqp->rwr_tbl)
520 size = (qp->attrs.sq_size << SQEBB_SHIFT) + ERDMA_EXTRA_BUFFER_SIZE;
521 kqp->sq_buf = dma_alloc_coherent(&dev->pdev->dev, size,
522 &kqp->sq_buf_dma_addr, GFP_KERNEL);
526 size = (qp->attrs.rq_size << RQE_SHIFT) + ERDMA_EXTRA_BUFFER_SIZE;
527 kqp->rq_buf = dma_alloc_coherent(&dev->pdev->dev, size,
528 &kqp->rq_buf_dma_addr, GFP_KERNEL);
532 kqp->sq_db_info = kqp->sq_buf + (qp->attrs.sq_size << SQEBB_SHIFT);
533 kqp->rq_db_info = kqp->rq_buf + (qp->attrs.rq_size << RQE_SHIFT);
542 static void erdma_fill_bottom_mtt(struct erdma_dev *dev, struct erdma_mem *mem)
544 struct erdma_mtt *mtt = mem->mtt;
545 struct ib_block_iter biter;
548 while (mtt->low_level)
549 mtt = mtt->low_level;
551 rdma_umem_for_each_dma_block(mem->umem, &biter, mem->page_size)
552 mtt->buf[idx++] = rdma_block_iter_dma_address(&biter);
555 static struct erdma_mtt *erdma_create_cont_mtt(struct erdma_dev *dev,
558 struct erdma_mtt *mtt;
560 mtt = kzalloc(sizeof(*mtt), GFP_KERNEL);
562 return ERR_PTR(-ENOMEM);
565 mtt->buf = kzalloc(mtt->size, GFP_KERNEL);
569 mtt->continuous = true;
570 mtt->buf_dma = dma_map_single(&dev->pdev->dev, mtt->buf, mtt->size,
572 if (dma_mapping_error(&dev->pdev->dev, mtt->buf_dma))
573 goto err_free_mtt_buf;
583 return ERR_PTR(-ENOMEM);
586 static void erdma_destroy_mtt_buf_sg(struct erdma_dev *dev,
587 struct erdma_mtt *mtt)
589 dma_unmap_sg(&dev->pdev->dev, mtt->sglist, mtt->nsg, DMA_TO_DEVICE);
593 static void erdma_destroy_scatter_mtt(struct erdma_dev *dev,
594 struct erdma_mtt *mtt)
596 erdma_destroy_mtt_buf_sg(dev, mtt);
601 static void erdma_init_middle_mtt(struct erdma_mtt *mtt,
602 struct erdma_mtt *low_mtt)
604 struct scatterlist *sg;
607 for_each_sg(low_mtt->sglist, sg, low_mtt->nsg, i)
608 mtt->buf[idx++] = sg_dma_address(sg);
611 static int erdma_create_mtt_buf_sg(struct erdma_dev *dev, struct erdma_mtt *mtt)
613 struct scatterlist *sglist;
614 void *buf = mtt->buf;
618 /* Failed if buf is not page aligned */
619 if ((uintptr_t)buf & ~PAGE_MASK)
622 npages = DIV_ROUND_UP(mtt->size, PAGE_SIZE);
623 sglist = vzalloc(npages * sizeof(*sglist));
627 sg_init_table(sglist, npages);
628 for (i = 0; i < npages; i++) {
629 pg = vmalloc_to_page(buf);
632 sg_set_page(&sglist[i], pg, PAGE_SIZE, 0);
636 nsg = dma_map_sg(&dev->pdev->dev, sglist, npages, DMA_TO_DEVICE);
640 mtt->sglist = sglist;
650 static struct erdma_mtt *erdma_create_scatter_mtt(struct erdma_dev *dev,
653 struct erdma_mtt *mtt;
656 mtt = kzalloc(sizeof(*mtt), GFP_KERNEL);
660 mtt->size = ALIGN(size, PAGE_SIZE);
661 mtt->buf = vzalloc(mtt->size);
662 mtt->continuous = false;
666 ret = erdma_create_mtt_buf_sg(dev, mtt);
668 goto err_free_mtt_buf;
670 ibdev_dbg(&dev->ibdev, "create scatter mtt, size:%lu, nsg:%u\n",
671 mtt->size, mtt->nsg);
684 static struct erdma_mtt *erdma_create_mtt(struct erdma_dev *dev, size_t size,
685 bool force_continuous)
687 struct erdma_mtt *mtt, *tmp_mtt;
690 ibdev_dbg(&dev->ibdev, "create_mtt, size:%lu, force cont:%d\n", size,
693 if (!(dev->attrs.cap_flags & ERDMA_DEV_CAP_FLAGS_MTT_VA))
694 force_continuous = true;
696 if (force_continuous)
697 return erdma_create_cont_mtt(dev, size);
699 mtt = erdma_create_scatter_mtt(dev, size);
704 /* convergence the mtt table. */
705 while (mtt->nsg != 1 && level <= 3) {
706 tmp_mtt = erdma_create_scatter_mtt(dev, MTT_SIZE(mtt->nsg));
707 if (IS_ERR(tmp_mtt)) {
708 ret = PTR_ERR(tmp_mtt);
711 erdma_init_middle_mtt(tmp_mtt, mtt);
712 tmp_mtt->low_level = mtt;
723 ibdev_dbg(&dev->ibdev, "top mtt: level:%d, dma_addr 0x%llx\n",
724 mtt->level, mtt->sglist[0].dma_address);
729 tmp_mtt = mtt->low_level;
730 erdma_destroy_scatter_mtt(dev, mtt);
737 static void erdma_destroy_mtt(struct erdma_dev *dev, struct erdma_mtt *mtt)
739 struct erdma_mtt *tmp_mtt;
741 if (mtt->continuous) {
742 dma_unmap_single(&dev->pdev->dev, mtt->buf_dma, mtt->size,
748 tmp_mtt = mtt->low_level;
749 erdma_destroy_scatter_mtt(dev, mtt);
755 static int get_mtt_entries(struct erdma_dev *dev, struct erdma_mem *mem,
756 u64 start, u64 len, int access, u64 virt,
757 unsigned long req_page_size, bool force_continuous)
761 mem->umem = ib_umem_get(&dev->ibdev, start, len, access);
762 if (IS_ERR(mem->umem)) {
763 ret = PTR_ERR(mem->umem);
770 mem->page_size = ib_umem_find_best_pgsz(mem->umem, req_page_size, virt);
771 mem->page_offset = start & (mem->page_size - 1);
772 mem->mtt_nents = ib_umem_num_dma_blocks(mem->umem, mem->page_size);
773 mem->page_cnt = mem->mtt_nents;
774 mem->mtt = erdma_create_mtt(dev, MTT_SIZE(mem->page_cnt),
776 if (IS_ERR(mem->mtt)) {
777 ret = PTR_ERR(mem->mtt);
781 erdma_fill_bottom_mtt(dev, mem);
787 ib_umem_release(mem->umem);
794 static void put_mtt_entries(struct erdma_dev *dev, struct erdma_mem *mem)
797 erdma_destroy_mtt(dev, mem->mtt);
800 ib_umem_release(mem->umem);
805 static int erdma_map_user_dbrecords(struct erdma_ucontext *ctx,
807 struct erdma_user_dbrecords_page **dbr_page,
808 dma_addr_t *dma_addr)
810 struct erdma_user_dbrecords_page *page = NULL;
813 mutex_lock(&ctx->dbrecords_page_mutex);
815 list_for_each_entry(page, &ctx->dbrecords_page_list, list)
816 if (page->va == (dbrecords_va & PAGE_MASK))
819 page = kmalloc(sizeof(*page), GFP_KERNEL);
825 page->va = (dbrecords_va & PAGE_MASK);
828 page->umem = ib_umem_get(ctx->ibucontext.device,
829 dbrecords_va & PAGE_MASK, PAGE_SIZE, 0);
830 if (IS_ERR(page->umem)) {
831 rv = PTR_ERR(page->umem);
836 list_add(&page->list, &ctx->dbrecords_page_list);
839 *dma_addr = sg_dma_address(page->umem->sgt_append.sgt.sgl) +
840 (dbrecords_va & ~PAGE_MASK);
845 mutex_unlock(&ctx->dbrecords_page_mutex);
850 erdma_unmap_user_dbrecords(struct erdma_ucontext *ctx,
851 struct erdma_user_dbrecords_page **dbr_page)
853 if (!ctx || !(*dbr_page))
856 mutex_lock(&ctx->dbrecords_page_mutex);
857 if (--(*dbr_page)->refcnt == 0) {
858 list_del(&(*dbr_page)->list);
859 ib_umem_release((*dbr_page)->umem);
864 mutex_unlock(&ctx->dbrecords_page_mutex);
867 static int init_user_qp(struct erdma_qp *qp, struct erdma_ucontext *uctx,
868 u64 va, u32 len, u64 db_info_va)
870 dma_addr_t db_info_dma_addr;
874 if (len < (ALIGN(qp->attrs.sq_size * SQEBB_SIZE, ERDMA_HW_PAGE_SIZE) +
875 qp->attrs.rq_size * RQE_SIZE))
878 ret = get_mtt_entries(qp->dev, &qp->user_qp.sq_mem, va,
879 qp->attrs.sq_size << SQEBB_SHIFT, 0, va,
880 (SZ_1M - SZ_4K), true);
884 rq_offset = ALIGN(qp->attrs.sq_size << SQEBB_SHIFT, ERDMA_HW_PAGE_SIZE);
885 qp->user_qp.rq_offset = rq_offset;
887 ret = get_mtt_entries(qp->dev, &qp->user_qp.rq_mem, va + rq_offset,
888 qp->attrs.rq_size << RQE_SHIFT, 0, va + rq_offset,
889 (SZ_1M - SZ_4K), true);
893 ret = erdma_map_user_dbrecords(uctx, db_info_va,
894 &qp->user_qp.user_dbr_page,
899 qp->user_qp.sq_db_info_dma_addr = db_info_dma_addr;
900 qp->user_qp.rq_db_info_dma_addr = db_info_dma_addr + ERDMA_DB_SIZE;
905 put_mtt_entries(qp->dev, &qp->user_qp.rq_mem);
908 put_mtt_entries(qp->dev, &qp->user_qp.sq_mem);
913 static void free_user_qp(struct erdma_qp *qp, struct erdma_ucontext *uctx)
915 put_mtt_entries(qp->dev, &qp->user_qp.sq_mem);
916 put_mtt_entries(qp->dev, &qp->user_qp.rq_mem);
917 erdma_unmap_user_dbrecords(uctx, &qp->user_qp.user_dbr_page);
920 int erdma_create_qp(struct ib_qp *ibqp, struct ib_qp_init_attr *attrs,
921 struct ib_udata *udata)
923 struct erdma_qp *qp = to_eqp(ibqp);
924 struct erdma_dev *dev = to_edev(ibqp->device);
925 struct erdma_ucontext *uctx = rdma_udata_to_drv_context(
926 udata, struct erdma_ucontext, ibucontext);
927 struct erdma_ureq_create_qp ureq;
928 struct erdma_uresp_create_qp uresp;
931 ret = erdma_qp_validate_cap(dev, attrs);
935 ret = erdma_qp_validate_attr(dev, attrs);
939 qp->scq = to_ecq(attrs->send_cq);
940 qp->rcq = to_ecq(attrs->recv_cq);
942 qp->attrs.cc = dev->attrs.cc;
944 init_rwsem(&qp->state_lock);
946 init_completion(&qp->safe_free);
948 ret = xa_alloc_cyclic(&dev->qp_xa, &qp->ibqp.qp_num, qp,
949 XA_LIMIT(1, dev->attrs.max_qp - 1),
950 &dev->next_alloc_qpn, GFP_KERNEL);
956 qp->attrs.sq_size = roundup_pow_of_two(attrs->cap.max_send_wr *
957 ERDMA_MAX_WQEBB_PER_SQE);
958 qp->attrs.rq_size = roundup_pow_of_two(attrs->cap.max_recv_wr);
961 ret = ib_copy_from_udata(&ureq, udata,
962 min(sizeof(ureq), udata->inlen));
966 ret = init_user_qp(qp, uctx, ureq.qbuf_va, ureq.qbuf_len,
971 memset(&uresp, 0, sizeof(uresp));
973 uresp.num_sqe = qp->attrs.sq_size;
974 uresp.num_rqe = qp->attrs.rq_size;
975 uresp.qp_id = QP_ID(qp);
976 uresp.rq_offset = qp->user_qp.rq_offset;
978 ret = ib_copy_to_udata(udata, &uresp, sizeof(uresp));
982 init_kernel_qp(dev, qp, attrs);
985 qp->attrs.max_send_sge = attrs->cap.max_send_sge;
986 qp->attrs.max_recv_sge = attrs->cap.max_recv_sge;
987 qp->attrs.state = ERDMA_QP_STATE_IDLE;
988 INIT_DELAYED_WORK(&qp->reflush_dwork, erdma_flush_worker);
990 ret = create_qp_cmd(uctx, qp);
994 spin_lock_init(&qp->lock);
1000 free_user_qp(qp, uctx);
1004 xa_erase(&dev->qp_xa, QP_ID(qp));
1009 static int erdma_create_stag(struct erdma_dev *dev, u32 *stag)
1013 stag_idx = erdma_alloc_idx(&dev->res_cb[ERDMA_RES_TYPE_STAG_IDX]);
1017 /* For now, we always let key field be zero. */
1018 *stag = (stag_idx << 8);
1023 struct ib_mr *erdma_get_dma_mr(struct ib_pd *ibpd, int acc)
1025 struct erdma_dev *dev = to_edev(ibpd->device);
1026 struct erdma_mr *mr;
1030 mr = kzalloc(sizeof(*mr), GFP_KERNEL);
1032 return ERR_PTR(-ENOMEM);
1034 ret = erdma_create_stag(dev, &stag);
1038 mr->type = ERDMA_MR_TYPE_DMA;
1040 mr->ibmr.lkey = stag;
1041 mr->ibmr.rkey = stag;
1043 mr->access = ERDMA_MR_ACC_LR | to_erdma_access_flags(acc);
1044 ret = regmr_cmd(dev, mr);
1046 goto out_remove_stag;
1051 erdma_free_idx(&dev->res_cb[ERDMA_RES_TYPE_STAG_IDX],
1052 mr->ibmr.lkey >> 8);
1057 return ERR_PTR(ret);
1060 struct ib_mr *erdma_ib_alloc_mr(struct ib_pd *ibpd, enum ib_mr_type mr_type,
1063 struct erdma_mr *mr;
1064 struct erdma_dev *dev = to_edev(ibpd->device);
1068 if (mr_type != IB_MR_TYPE_MEM_REG)
1069 return ERR_PTR(-EOPNOTSUPP);
1071 if (max_num_sg > ERDMA_MR_MAX_MTT_CNT)
1072 return ERR_PTR(-EINVAL);
1074 mr = kzalloc(sizeof(*mr), GFP_KERNEL);
1076 return ERR_PTR(-ENOMEM);
1078 ret = erdma_create_stag(dev, &stag);
1082 mr->type = ERDMA_MR_TYPE_FRMR;
1084 mr->ibmr.lkey = stag;
1085 mr->ibmr.rkey = stag;
1087 /* update it in FRMR. */
1088 mr->access = ERDMA_MR_ACC_LR | ERDMA_MR_ACC_LW | ERDMA_MR_ACC_RR |
1091 mr->mem.page_size = PAGE_SIZE; /* update it later. */
1092 mr->mem.page_cnt = max_num_sg;
1093 mr->mem.mtt = erdma_create_mtt(dev, MTT_SIZE(max_num_sg), true);
1094 if (IS_ERR(mr->mem.mtt)) {
1095 ret = PTR_ERR(mr->mem.mtt);
1096 goto out_remove_stag;
1099 ret = regmr_cmd(dev, mr);
1101 goto out_destroy_mtt;
1106 erdma_destroy_mtt(dev, mr->mem.mtt);
1109 erdma_free_idx(&dev->res_cb[ERDMA_RES_TYPE_STAG_IDX],
1110 mr->ibmr.lkey >> 8);
1115 return ERR_PTR(ret);
1118 static int erdma_set_page(struct ib_mr *ibmr, u64 addr)
1120 struct erdma_mr *mr = to_emr(ibmr);
1122 if (mr->mem.mtt_nents >= mr->mem.page_cnt)
1125 mr->mem.mtt->buf[mr->mem.mtt_nents] = addr;
1126 mr->mem.mtt_nents++;
1131 int erdma_map_mr_sg(struct ib_mr *ibmr, struct scatterlist *sg, int sg_nents,
1132 unsigned int *sg_offset)
1134 struct erdma_mr *mr = to_emr(ibmr);
1137 mr->mem.mtt_nents = 0;
1139 num = ib_sg_to_pages(&mr->ibmr, sg, sg_nents, sg_offset,
1145 struct ib_mr *erdma_reg_user_mr(struct ib_pd *ibpd, u64 start, u64 len,
1146 u64 virt, int access, struct ib_udata *udata)
1148 struct erdma_mr *mr = NULL;
1149 struct erdma_dev *dev = to_edev(ibpd->device);
1153 if (!len || len > dev->attrs.max_mr_size)
1154 return ERR_PTR(-EINVAL);
1156 mr = kzalloc(sizeof(*mr), GFP_KERNEL);
1158 return ERR_PTR(-ENOMEM);
1160 ret = get_mtt_entries(dev, &mr->mem, start, len, access, virt,
1161 SZ_2G - SZ_4K, false);
1165 ret = erdma_create_stag(dev, &stag);
1167 goto err_out_put_mtt;
1169 mr->ibmr.lkey = mr->ibmr.rkey = stag;
1173 mr->access = ERDMA_MR_ACC_LR | to_erdma_access_flags(access);
1175 mr->type = ERDMA_MR_TYPE_NORMAL;
1177 ret = regmr_cmd(dev, mr);
1184 erdma_free_idx(&dev->res_cb[ERDMA_RES_TYPE_STAG_IDX],
1185 mr->ibmr.lkey >> 8);
1188 put_mtt_entries(dev, &mr->mem);
1193 return ERR_PTR(ret);
1196 int erdma_dereg_mr(struct ib_mr *ibmr, struct ib_udata *udata)
1198 struct erdma_mr *mr;
1199 struct erdma_dev *dev = to_edev(ibmr->device);
1200 struct erdma_cmdq_dereg_mr_req req;
1205 erdma_cmdq_build_reqhdr(&req.hdr, CMDQ_SUBMOD_RDMA,
1206 CMDQ_OPCODE_DEREG_MR);
1208 req.cfg = FIELD_PREP(ERDMA_CMD_MR_MPT_IDX_MASK, ibmr->lkey >> 8) |
1209 FIELD_PREP(ERDMA_CMD_MR_KEY_MASK, ibmr->lkey & 0xFF);
1211 ret = erdma_post_cmd_wait(&dev->cmdq, &req, sizeof(req), NULL, NULL);
1215 erdma_free_idx(&dev->res_cb[ERDMA_RES_TYPE_STAG_IDX], ibmr->lkey >> 8);
1217 put_mtt_entries(dev, &mr->mem);
1223 int erdma_destroy_cq(struct ib_cq *ibcq, struct ib_udata *udata)
1225 struct erdma_cq *cq = to_ecq(ibcq);
1226 struct erdma_dev *dev = to_edev(ibcq->device);
1227 struct erdma_ucontext *ctx = rdma_udata_to_drv_context(
1228 udata, struct erdma_ucontext, ibucontext);
1230 struct erdma_cmdq_destroy_cq_req req;
1232 erdma_cmdq_build_reqhdr(&req.hdr, CMDQ_SUBMOD_RDMA,
1233 CMDQ_OPCODE_DESTROY_CQ);
1236 err = erdma_post_cmd_wait(&dev->cmdq, &req, sizeof(req), NULL, NULL);
1240 if (rdma_is_kernel_res(&cq->ibcq.res)) {
1241 dma_free_coherent(&dev->pdev->dev,
1242 WARPPED_BUFSIZE(cq->depth << CQE_SHIFT),
1243 cq->kern_cq.qbuf, cq->kern_cq.qbuf_dma_addr);
1245 erdma_unmap_user_dbrecords(ctx, &cq->user_cq.user_dbr_page);
1246 put_mtt_entries(dev, &cq->user_cq.qbuf_mem);
1249 xa_erase(&dev->cq_xa, cq->cqn);
1254 int erdma_destroy_qp(struct ib_qp *ibqp, struct ib_udata *udata)
1256 struct erdma_qp *qp = to_eqp(ibqp);
1257 struct erdma_dev *dev = to_edev(ibqp->device);
1258 struct erdma_ucontext *ctx = rdma_udata_to_drv_context(
1259 udata, struct erdma_ucontext, ibucontext);
1260 struct erdma_qp_attrs qp_attrs;
1262 struct erdma_cmdq_destroy_qp_req req;
1264 down_write(&qp->state_lock);
1265 qp_attrs.state = ERDMA_QP_STATE_ERROR;
1266 erdma_modify_qp_internal(qp, &qp_attrs, ERDMA_QP_ATTR_STATE);
1267 up_write(&qp->state_lock);
1269 cancel_delayed_work_sync(&qp->reflush_dwork);
1271 erdma_cmdq_build_reqhdr(&req.hdr, CMDQ_SUBMOD_RDMA,
1272 CMDQ_OPCODE_DESTROY_QP);
1273 req.qpn = QP_ID(qp);
1275 err = erdma_post_cmd_wait(&dev->cmdq, &req, sizeof(req), NULL, NULL);
1280 wait_for_completion(&qp->safe_free);
1282 if (rdma_is_kernel_res(&qp->ibqp.res)) {
1283 vfree(qp->kern_qp.swr_tbl);
1284 vfree(qp->kern_qp.rwr_tbl);
1287 WARPPED_BUFSIZE(qp->attrs.rq_size << RQE_SHIFT),
1288 qp->kern_qp.rq_buf, qp->kern_qp.rq_buf_dma_addr);
1291 WARPPED_BUFSIZE(qp->attrs.sq_size << SQEBB_SHIFT),
1292 qp->kern_qp.sq_buf, qp->kern_qp.sq_buf_dma_addr);
1294 put_mtt_entries(dev, &qp->user_qp.sq_mem);
1295 put_mtt_entries(dev, &qp->user_qp.rq_mem);
1296 erdma_unmap_user_dbrecords(ctx, &qp->user_qp.user_dbr_page);
1300 erdma_cep_put(qp->cep);
1301 xa_erase(&dev->qp_xa, QP_ID(qp));
1306 void erdma_qp_get_ref(struct ib_qp *ibqp)
1308 erdma_qp_get(to_eqp(ibqp));
1311 void erdma_qp_put_ref(struct ib_qp *ibqp)
1313 erdma_qp_put(to_eqp(ibqp));
1316 int erdma_mmap(struct ib_ucontext *ctx, struct vm_area_struct *vma)
1318 struct rdma_user_mmap_entry *rdma_entry;
1319 struct erdma_user_mmap_entry *entry;
1323 rdma_entry = rdma_user_mmap_entry_get(ctx, vma);
1327 entry = to_emmap(rdma_entry);
1329 switch (entry->mmap_flag) {
1330 case ERDMA_MMAP_IO_NC:
1332 prot = pgprot_device(vma->vm_page_prot);
1339 err = rdma_user_mmap_io(ctx, vma, PFN_DOWN(entry->address), PAGE_SIZE,
1343 rdma_user_mmap_entry_put(rdma_entry);
1347 void erdma_mmap_free(struct rdma_user_mmap_entry *rdma_entry)
1349 struct erdma_user_mmap_entry *entry = to_emmap(rdma_entry);
1354 static int alloc_db_resources(struct erdma_dev *dev, struct erdma_ucontext *ctx,
1357 struct erdma_cmdq_ext_db_req req = {};
1362 * CAP_SYS_RAWIO is required if hardware does not support extend
1363 * doorbell mechanism.
1365 if (!ext_db_en && !capable(CAP_SYS_RAWIO))
1369 ctx->sdb = dev->func_bar_addr + ERDMA_BAR_SQDB_SPACE_OFFSET;
1370 ctx->rdb = dev->func_bar_addr + ERDMA_BAR_RQDB_SPACE_OFFSET;
1371 ctx->cdb = dev->func_bar_addr + ERDMA_BAR_CQDB_SPACE_OFFSET;
1375 erdma_cmdq_build_reqhdr(&req.hdr, CMDQ_SUBMOD_COMMON,
1376 CMDQ_OPCODE_ALLOC_DB);
1378 req.cfg = FIELD_PREP(ERDMA_CMD_EXT_DB_CQ_EN_MASK, 1) |
1379 FIELD_PREP(ERDMA_CMD_EXT_DB_RQ_EN_MASK, 1) |
1380 FIELD_PREP(ERDMA_CMD_EXT_DB_SQ_EN_MASK, 1);
1382 ret = erdma_post_cmd_wait(&dev->cmdq, &req, sizeof(req), &val0, &val1);
1386 ctx->ext_db.enable = true;
1387 ctx->ext_db.sdb_off = ERDMA_GET(val0, ALLOC_DB_RESP_SDB);
1388 ctx->ext_db.rdb_off = ERDMA_GET(val0, ALLOC_DB_RESP_RDB);
1389 ctx->ext_db.cdb_off = ERDMA_GET(val0, ALLOC_DB_RESP_CDB);
1391 ctx->sdb = dev->func_bar_addr + (ctx->ext_db.sdb_off << PAGE_SHIFT);
1392 ctx->cdb = dev->func_bar_addr + (ctx->ext_db.rdb_off << PAGE_SHIFT);
1393 ctx->rdb = dev->func_bar_addr + (ctx->ext_db.cdb_off << PAGE_SHIFT);
1398 static void free_db_resources(struct erdma_dev *dev, struct erdma_ucontext *ctx)
1400 struct erdma_cmdq_ext_db_req req = {};
1403 if (!ctx->ext_db.enable)
1406 erdma_cmdq_build_reqhdr(&req.hdr, CMDQ_SUBMOD_COMMON,
1407 CMDQ_OPCODE_FREE_DB);
1409 req.cfg = FIELD_PREP(ERDMA_CMD_EXT_DB_CQ_EN_MASK, 1) |
1410 FIELD_PREP(ERDMA_CMD_EXT_DB_RQ_EN_MASK, 1) |
1411 FIELD_PREP(ERDMA_CMD_EXT_DB_SQ_EN_MASK, 1);
1413 req.sdb_off = ctx->ext_db.sdb_off;
1414 req.rdb_off = ctx->ext_db.rdb_off;
1415 req.cdb_off = ctx->ext_db.cdb_off;
1417 ret = erdma_post_cmd_wait(&dev->cmdq, &req, sizeof(req), NULL, NULL);
1419 ibdev_err_ratelimited(&dev->ibdev,
1420 "free db resources failed %d", ret);
1423 static void erdma_uctx_user_mmap_entries_remove(struct erdma_ucontext *uctx)
1425 rdma_user_mmap_entry_remove(uctx->sq_db_mmap_entry);
1426 rdma_user_mmap_entry_remove(uctx->rq_db_mmap_entry);
1427 rdma_user_mmap_entry_remove(uctx->cq_db_mmap_entry);
1430 int erdma_alloc_ucontext(struct ib_ucontext *ibctx, struct ib_udata *udata)
1432 struct erdma_ucontext *ctx = to_ectx(ibctx);
1433 struct erdma_dev *dev = to_edev(ibctx->device);
1435 struct erdma_uresp_alloc_ctx uresp = {};
1437 if (atomic_inc_return(&dev->num_ctx) > ERDMA_MAX_CONTEXT) {
1442 if (udata->outlen < sizeof(uresp)) {
1447 INIT_LIST_HEAD(&ctx->dbrecords_page_list);
1448 mutex_init(&ctx->dbrecords_page_mutex);
1450 ret = alloc_db_resources(dev, ctx,
1451 !!(dev->attrs.cap_flags &
1452 ERDMA_DEV_CAP_FLAGS_EXTEND_DB));
1456 ctx->sq_db_mmap_entry = erdma_user_mmap_entry_insert(
1457 ctx, (void *)ctx->sdb, PAGE_SIZE, ERDMA_MMAP_IO_NC, &uresp.sdb);
1458 if (!ctx->sq_db_mmap_entry) {
1460 goto err_free_ext_db;
1463 ctx->rq_db_mmap_entry = erdma_user_mmap_entry_insert(
1464 ctx, (void *)ctx->rdb, PAGE_SIZE, ERDMA_MMAP_IO_NC, &uresp.rdb);
1465 if (!ctx->rq_db_mmap_entry) {
1467 goto err_put_mmap_entries;
1470 ctx->cq_db_mmap_entry = erdma_user_mmap_entry_insert(
1471 ctx, (void *)ctx->cdb, PAGE_SIZE, ERDMA_MMAP_IO_NC, &uresp.cdb);
1472 if (!ctx->cq_db_mmap_entry) {
1474 goto err_put_mmap_entries;
1477 uresp.dev_id = dev->pdev->device;
1479 ret = ib_copy_to_udata(udata, &uresp, sizeof(uresp));
1481 goto err_put_mmap_entries;
1485 err_put_mmap_entries:
1486 erdma_uctx_user_mmap_entries_remove(ctx);
1489 free_db_resources(dev, ctx);
1492 atomic_dec(&dev->num_ctx);
1496 void erdma_dealloc_ucontext(struct ib_ucontext *ibctx)
1498 struct erdma_dev *dev = to_edev(ibctx->device);
1499 struct erdma_ucontext *ctx = to_ectx(ibctx);
1501 erdma_uctx_user_mmap_entries_remove(ctx);
1502 free_db_resources(dev, ctx);
1503 atomic_dec(&dev->num_ctx);
1506 static int ib_qp_state_to_erdma_qp_state[IB_QPS_ERR + 1] = {
1507 [IB_QPS_RESET] = ERDMA_QP_STATE_IDLE,
1508 [IB_QPS_INIT] = ERDMA_QP_STATE_IDLE,
1509 [IB_QPS_RTR] = ERDMA_QP_STATE_RTR,
1510 [IB_QPS_RTS] = ERDMA_QP_STATE_RTS,
1511 [IB_QPS_SQD] = ERDMA_QP_STATE_CLOSING,
1512 [IB_QPS_SQE] = ERDMA_QP_STATE_TERMINATE,
1513 [IB_QPS_ERR] = ERDMA_QP_STATE_ERROR
1516 int erdma_modify_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr, int attr_mask,
1517 struct ib_udata *udata)
1519 struct erdma_qp_attrs new_attrs;
1520 enum erdma_qp_attr_mask erdma_attr_mask = 0;
1521 struct erdma_qp *qp = to_eqp(ibqp);
1524 if (attr_mask & ~IB_QP_ATTR_STANDARD_BITS)
1527 memset(&new_attrs, 0, sizeof(new_attrs));
1529 if (attr_mask & IB_QP_STATE) {
1530 new_attrs.state = ib_qp_state_to_erdma_qp_state[attr->qp_state];
1532 erdma_attr_mask |= ERDMA_QP_ATTR_STATE;
1535 down_write(&qp->state_lock);
1537 ret = erdma_modify_qp_internal(qp, &new_attrs, erdma_attr_mask);
1539 up_write(&qp->state_lock);
1544 int erdma_query_qp(struct ib_qp *ibqp, struct ib_qp_attr *qp_attr,
1545 int qp_attr_mask, struct ib_qp_init_attr *qp_init_attr)
1547 struct erdma_qp *qp;
1548 struct erdma_dev *dev;
1550 if (ibqp && qp_attr && qp_init_attr) {
1552 dev = to_edev(ibqp->device);
1557 qp_attr->cap.max_inline_data = ERDMA_MAX_INLINE;
1558 qp_init_attr->cap.max_inline_data = ERDMA_MAX_INLINE;
1560 qp_attr->cap.max_send_wr = qp->attrs.sq_size;
1561 qp_attr->cap.max_recv_wr = qp->attrs.rq_size;
1562 qp_attr->cap.max_send_sge = qp->attrs.max_send_sge;
1563 qp_attr->cap.max_recv_sge = qp->attrs.max_recv_sge;
1565 qp_attr->path_mtu = ib_mtu_int_to_enum(dev->netdev->mtu);
1566 qp_attr->max_rd_atomic = qp->attrs.irq_size;
1567 qp_attr->max_dest_rd_atomic = qp->attrs.orq_size;
1569 qp_attr->qp_access_flags = IB_ACCESS_LOCAL_WRITE |
1570 IB_ACCESS_REMOTE_WRITE |
1571 IB_ACCESS_REMOTE_READ;
1573 qp_init_attr->cap = qp_attr->cap;
1578 static int erdma_init_user_cq(struct erdma_ucontext *ctx, struct erdma_cq *cq,
1579 struct erdma_ureq_create_cq *ureq)
1582 struct erdma_dev *dev = to_edev(cq->ibcq.device);
1584 ret = get_mtt_entries(dev, &cq->user_cq.qbuf_mem, ureq->qbuf_va,
1585 ureq->qbuf_len, 0, ureq->qbuf_va, SZ_64M - SZ_4K,
1590 ret = erdma_map_user_dbrecords(ctx, ureq->db_record_va,
1591 &cq->user_cq.user_dbr_page,
1592 &cq->user_cq.db_info_dma_addr);
1594 put_mtt_entries(dev, &cq->user_cq.qbuf_mem);
1599 static int erdma_init_kernel_cq(struct erdma_cq *cq)
1601 struct erdma_dev *dev = to_edev(cq->ibcq.device);
1604 dma_alloc_coherent(&dev->pdev->dev,
1605 WARPPED_BUFSIZE(cq->depth << CQE_SHIFT),
1606 &cq->kern_cq.qbuf_dma_addr, GFP_KERNEL);
1607 if (!cq->kern_cq.qbuf)
1610 cq->kern_cq.db_record =
1611 (u64 *)(cq->kern_cq.qbuf + (cq->depth << CQE_SHIFT));
1612 spin_lock_init(&cq->kern_cq.lock);
1613 /* use default cqdb addr */
1614 cq->kern_cq.db = dev->func_bar + ERDMA_BAR_CQDB_SPACE_OFFSET;
1619 int erdma_create_cq(struct ib_cq *ibcq, const struct ib_cq_init_attr *attr,
1620 struct ib_udata *udata)
1622 struct erdma_cq *cq = to_ecq(ibcq);
1623 struct erdma_dev *dev = to_edev(ibcq->device);
1624 unsigned int depth = attr->cqe;
1626 struct erdma_ucontext *ctx = rdma_udata_to_drv_context(
1627 udata, struct erdma_ucontext, ibucontext);
1629 if (depth > dev->attrs.max_cqe)
1632 depth = roundup_pow_of_two(depth);
1633 cq->ibcq.cqe = depth;
1635 cq->assoc_eqn = attr->comp_vector + 1;
1637 ret = xa_alloc_cyclic(&dev->cq_xa, &cq->cqn, cq,
1638 XA_LIMIT(1, dev->attrs.max_cq - 1),
1639 &dev->next_alloc_cqn, GFP_KERNEL);
1643 if (!rdma_is_kernel_res(&ibcq->res)) {
1644 struct erdma_ureq_create_cq ureq;
1645 struct erdma_uresp_create_cq uresp;
1647 ret = ib_copy_from_udata(&ureq, udata,
1648 min(udata->inlen, sizeof(ureq)));
1652 ret = erdma_init_user_cq(ctx, cq, &ureq);
1656 uresp.cq_id = cq->cqn;
1657 uresp.num_cqe = depth;
1659 ret = ib_copy_to_udata(udata, &uresp,
1660 min(sizeof(uresp), udata->outlen));
1664 ret = erdma_init_kernel_cq(cq);
1669 ret = create_cq_cmd(ctx, cq);
1676 if (!rdma_is_kernel_res(&ibcq->res)) {
1677 erdma_unmap_user_dbrecords(ctx, &cq->user_cq.user_dbr_page);
1678 put_mtt_entries(dev, &cq->user_cq.qbuf_mem);
1680 dma_free_coherent(&dev->pdev->dev,
1681 WARPPED_BUFSIZE(depth << CQE_SHIFT),
1682 cq->kern_cq.qbuf, cq->kern_cq.qbuf_dma_addr);
1686 xa_erase(&dev->cq_xa, cq->cqn);
1691 void erdma_set_mtu(struct erdma_dev *dev, u32 mtu)
1693 struct erdma_cmdq_config_mtu_req req;
1695 erdma_cmdq_build_reqhdr(&req.hdr, CMDQ_SUBMOD_COMMON,
1696 CMDQ_OPCODE_CONF_MTU);
1699 erdma_post_cmd_wait(&dev->cmdq, &req, sizeof(req), NULL, NULL);
1702 void erdma_port_event(struct erdma_dev *dev, enum ib_event_type reason)
1704 struct ib_event event;
1706 event.device = &dev->ibdev;
1707 event.element.port_num = 1;
1708 event.event = reason;
1710 ib_dispatch_event(&event);