2 * Copyright (c) 2016 Hisilicon Limited.
3 * Copyright (c) 2007, 2008 Mellanox Technologies. All rights reserved.
5 * This software is available to you under a choice of one of two
6 * licenses. You may choose to be licensed under the terms of the GNU
7 * General Public License (GPL) Version 2, available from the file
8 * COPYING in the main directory of this source tree, or the
9 * OpenIB.org BSD license below:
11 * Redistribution and use in source and binary forms, with or
12 * without modification, are permitted provided that the following
15 * - Redistributions of source code must retain the above
16 * copyright notice, this list of conditions and the following
19 * - Redistributions in binary form must reproduce the above
20 * copyright notice, this list of conditions and the following
21 * disclaimer in the documentation and/or other materials
22 * provided with the distribution.
24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
28 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
29 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
30 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
34 #include <linux/pci.h>
35 #include <rdma/ib_addr.h>
36 #include <rdma/ib_umem.h>
37 #include <rdma/uverbs_ioctl.h>
38 #include "hns_roce_common.h"
39 #include "hns_roce_device.h"
40 #include "hns_roce_hem.h"
42 static void flush_work_handle(struct work_struct *work)
44 struct hns_roce_work *flush_work = container_of(work,
45 struct hns_roce_work, work);
46 struct hns_roce_qp *hr_qp = container_of(flush_work,
47 struct hns_roce_qp, flush_work);
48 struct device *dev = flush_work->hr_dev->dev;
49 struct ib_qp_attr attr;
53 attr_mask = IB_QP_STATE;
54 attr.qp_state = IB_QPS_ERR;
56 if (test_and_clear_bit(HNS_ROCE_FLUSH_FLAG, &hr_qp->flush_flag)) {
57 ret = hns_roce_modify_qp(&hr_qp->ibqp, &attr, attr_mask, NULL);
59 dev_err(dev, "modify QP to error state failed(%d) during CQE flush\n",
64 * make sure we signal QP destroy leg that flush QP was completed
65 * so that it can safely proceed ahead now and destroy QP
67 if (refcount_dec_and_test(&hr_qp->refcount))
68 complete(&hr_qp->free);
71 void init_flush_work(struct hns_roce_dev *hr_dev, struct hns_roce_qp *hr_qp)
73 struct hns_roce_work *flush_work = &hr_qp->flush_work;
75 flush_work->hr_dev = hr_dev;
76 INIT_WORK(&flush_work->work, flush_work_handle);
77 refcount_inc(&hr_qp->refcount);
78 queue_work(hr_dev->irq_workq, &flush_work->work);
81 void flush_cqe(struct hns_roce_dev *dev, struct hns_roce_qp *qp)
84 * Hip08 hardware cannot flush the WQEs in SQ/RQ if the QP state
85 * gets into errored mode. Hence, as a workaround to this
86 * hardware limitation, driver needs to assist in flushing. But
87 * the flushing operation uses mailbox to convey the QP state to
88 * the hardware and which can sleep due to the mutex protection
89 * around the mailbox calls. Hence, use the deferred flush for
92 if (!test_and_set_bit(HNS_ROCE_FLUSH_FLAG, &qp->flush_flag))
93 init_flush_work(dev, qp);
96 void hns_roce_qp_event(struct hns_roce_dev *hr_dev, u32 qpn, int event_type)
98 struct device *dev = hr_dev->dev;
99 struct hns_roce_qp *qp;
101 xa_lock(&hr_dev->qp_table_xa);
102 qp = __hns_roce_qp_lookup(hr_dev, qpn);
104 refcount_inc(&qp->refcount);
105 xa_unlock(&hr_dev->qp_table_xa);
108 dev_warn(dev, "async event for bogus QP %08x\n", qpn);
112 if (event_type == HNS_ROCE_EVENT_TYPE_WQ_CATAS_ERROR ||
113 event_type == HNS_ROCE_EVENT_TYPE_INV_REQ_LOCAL_WQ_ERROR ||
114 event_type == HNS_ROCE_EVENT_TYPE_LOCAL_WQ_ACCESS_ERROR ||
115 event_type == HNS_ROCE_EVENT_TYPE_XRCD_VIOLATION ||
116 event_type == HNS_ROCE_EVENT_TYPE_INVALID_XRCETH) {
117 qp->state = IB_QPS_ERR;
119 flush_cqe(hr_dev, qp);
122 qp->event(qp, (enum hns_roce_event)event_type);
124 if (refcount_dec_and_test(&qp->refcount))
128 static void hns_roce_ib_qp_event(struct hns_roce_qp *hr_qp,
129 enum hns_roce_event type)
131 struct ib_qp *ibqp = &hr_qp->ibqp;
132 struct ib_event event;
134 if (ibqp->event_handler) {
135 event.device = ibqp->device;
136 event.element.qp = ibqp;
138 case HNS_ROCE_EVENT_TYPE_PATH_MIG:
139 event.event = IB_EVENT_PATH_MIG;
141 case HNS_ROCE_EVENT_TYPE_COMM_EST:
142 event.event = IB_EVENT_COMM_EST;
144 case HNS_ROCE_EVENT_TYPE_SQ_DRAINED:
145 event.event = IB_EVENT_SQ_DRAINED;
147 case HNS_ROCE_EVENT_TYPE_SRQ_LAST_WQE_REACH:
148 event.event = IB_EVENT_QP_LAST_WQE_REACHED;
150 case HNS_ROCE_EVENT_TYPE_WQ_CATAS_ERROR:
151 event.event = IB_EVENT_QP_FATAL;
153 case HNS_ROCE_EVENT_TYPE_PATH_MIG_FAILED:
154 event.event = IB_EVENT_PATH_MIG_ERR;
156 case HNS_ROCE_EVENT_TYPE_INV_REQ_LOCAL_WQ_ERROR:
157 event.event = IB_EVENT_QP_REQ_ERR;
159 case HNS_ROCE_EVENT_TYPE_LOCAL_WQ_ACCESS_ERROR:
160 case HNS_ROCE_EVENT_TYPE_XRCD_VIOLATION:
161 case HNS_ROCE_EVENT_TYPE_INVALID_XRCETH:
162 event.event = IB_EVENT_QP_ACCESS_ERR;
165 dev_dbg(ibqp->device->dev.parent, "roce_ib: Unexpected event type %d on QP %06lx\n",
169 ibqp->event_handler(&event, ibqp->qp_context);
173 static u8 get_least_load_bankid_for_qp(struct hns_roce_bank *bank)
175 u32 least_load = bank[0].inuse;
180 for (i = 1; i < HNS_ROCE_QP_BANK_NUM; i++) {
181 bankcnt = bank[i].inuse;
182 if (bankcnt < least_load) {
183 least_load = bankcnt;
191 static int alloc_qpn_with_bankid(struct hns_roce_bank *bank, u8 bankid,
196 id = ida_alloc_range(&bank->ida, bank->next, bank->max, GFP_KERNEL);
198 id = ida_alloc_range(&bank->ida, bank->min, bank->max,
204 /* the QPN should keep increasing until the max value is reached. */
205 bank->next = (id + 1) > bank->max ? bank->min : id + 1;
207 /* the lower 3 bits is bankid */
208 *qpn = (id << 3) | bankid;
212 static int alloc_qpn(struct hns_roce_dev *hr_dev, struct hns_roce_qp *hr_qp)
214 struct hns_roce_qp_table *qp_table = &hr_dev->qp_table;
215 unsigned long num = 0;
219 if (hr_qp->ibqp.qp_type == IB_QPT_GSI) {
222 mutex_lock(&qp_table->bank_mutex);
223 bankid = get_least_load_bankid_for_qp(qp_table->bank);
225 ret = alloc_qpn_with_bankid(&qp_table->bank[bankid], bankid,
228 ibdev_err(&hr_dev->ib_dev,
229 "failed to alloc QPN, ret = %d\n", ret);
230 mutex_unlock(&qp_table->bank_mutex);
234 qp_table->bank[bankid].inuse++;
235 mutex_unlock(&qp_table->bank_mutex);
243 static void add_qp_to_list(struct hns_roce_dev *hr_dev,
244 struct hns_roce_qp *hr_qp,
245 struct ib_cq *send_cq, struct ib_cq *recv_cq)
247 struct hns_roce_cq *hr_send_cq, *hr_recv_cq;
250 hr_send_cq = send_cq ? to_hr_cq(send_cq) : NULL;
251 hr_recv_cq = recv_cq ? to_hr_cq(recv_cq) : NULL;
253 spin_lock_irqsave(&hr_dev->qp_list_lock, flags);
254 hns_roce_lock_cqs(hr_send_cq, hr_recv_cq);
256 list_add_tail(&hr_qp->node, &hr_dev->qp_list);
258 list_add_tail(&hr_qp->sq_node, &hr_send_cq->sq_list);
260 list_add_tail(&hr_qp->rq_node, &hr_recv_cq->rq_list);
262 hns_roce_unlock_cqs(hr_send_cq, hr_recv_cq);
263 spin_unlock_irqrestore(&hr_dev->qp_list_lock, flags);
266 static int hns_roce_qp_store(struct hns_roce_dev *hr_dev,
267 struct hns_roce_qp *hr_qp,
268 struct ib_qp_init_attr *init_attr)
270 struct xarray *xa = &hr_dev->qp_table_xa;
276 ret = xa_err(xa_store_irq(xa, hr_qp->qpn, hr_qp, GFP_KERNEL));
278 dev_err(hr_dev->dev, "failed to xa store for QPC\n");
280 /* add QP to device's QP list for softwc */
281 add_qp_to_list(hr_dev, hr_qp, init_attr->send_cq,
287 static int alloc_qpc(struct hns_roce_dev *hr_dev, struct hns_roce_qp *hr_qp)
289 struct hns_roce_qp_table *qp_table = &hr_dev->qp_table;
290 struct device *dev = hr_dev->dev;
296 /* Alloc memory for QPC */
297 ret = hns_roce_table_get(hr_dev, &qp_table->qp_table, hr_qp->qpn);
299 dev_err(dev, "failed to get QPC table\n");
303 /* Alloc memory for IRRL */
304 ret = hns_roce_table_get(hr_dev, &qp_table->irrl_table, hr_qp->qpn);
306 dev_err(dev, "failed to get IRRL table\n");
310 if (hr_dev->caps.trrl_entry_sz) {
311 /* Alloc memory for TRRL */
312 ret = hns_roce_table_get(hr_dev, &qp_table->trrl_table,
315 dev_err(dev, "failed to get TRRL table\n");
320 if (hr_dev->caps.flags & HNS_ROCE_CAP_FLAG_QP_FLOW_CTRL) {
321 /* Alloc memory for SCC CTX */
322 ret = hns_roce_table_get(hr_dev, &qp_table->sccc_table,
325 dev_err(dev, "failed to get SCC CTX table\n");
333 if (hr_dev->caps.trrl_entry_sz)
334 hns_roce_table_put(hr_dev, &qp_table->trrl_table, hr_qp->qpn);
337 hns_roce_table_put(hr_dev, &qp_table->irrl_table, hr_qp->qpn);
340 hns_roce_table_put(hr_dev, &qp_table->qp_table, hr_qp->qpn);
346 static void qp_user_mmap_entry_remove(struct hns_roce_qp *hr_qp)
348 rdma_user_mmap_entry_remove(&hr_qp->dwqe_mmap_entry->rdma_entry);
351 void hns_roce_qp_remove(struct hns_roce_dev *hr_dev, struct hns_roce_qp *hr_qp)
353 struct xarray *xa = &hr_dev->qp_table_xa;
356 list_del(&hr_qp->node);
358 if (hr_qp->ibqp.qp_type != IB_QPT_XRC_TGT)
359 list_del(&hr_qp->sq_node);
361 if (hr_qp->ibqp.qp_type != IB_QPT_XRC_INI &&
362 hr_qp->ibqp.qp_type != IB_QPT_XRC_TGT)
363 list_del(&hr_qp->rq_node);
365 xa_lock_irqsave(xa, flags);
366 __xa_erase(xa, hr_qp->qpn);
367 xa_unlock_irqrestore(xa, flags);
370 static void free_qpc(struct hns_roce_dev *hr_dev, struct hns_roce_qp *hr_qp)
372 struct hns_roce_qp_table *qp_table = &hr_dev->qp_table;
374 if (hr_dev->caps.trrl_entry_sz)
375 hns_roce_table_put(hr_dev, &qp_table->trrl_table, hr_qp->qpn);
376 hns_roce_table_put(hr_dev, &qp_table->irrl_table, hr_qp->qpn);
379 static inline u8 get_qp_bankid(unsigned long qpn)
381 /* The lower 3 bits of QPN are used to hash to different banks */
382 return (u8)(qpn & GENMASK(2, 0));
385 static void free_qpn(struct hns_roce_dev *hr_dev, struct hns_roce_qp *hr_qp)
389 if (hr_qp->ibqp.qp_type == IB_QPT_GSI)
392 if (hr_qp->qpn < hr_dev->caps.reserved_qps)
395 bankid = get_qp_bankid(hr_qp->qpn);
397 ida_free(&hr_dev->qp_table.bank[bankid].ida, hr_qp->qpn >> 3);
399 mutex_lock(&hr_dev->qp_table.bank_mutex);
400 hr_dev->qp_table.bank[bankid].inuse--;
401 mutex_unlock(&hr_dev->qp_table.bank_mutex);
404 static u32 proc_rq_sge(struct hns_roce_dev *dev, struct hns_roce_qp *hr_qp,
407 u32 max_sge = dev->caps.max_rq_sg;
409 if (dev->pci_dev->revision >= PCI_REVISION_ID_HIP09)
412 /* Reserve SGEs only for HIP08 in kernel; The userspace driver will
413 * calculate number of max_sge with reserved SGEs when allocating wqe
414 * buf, so there is no need to do this again in kernel. But the number
415 * may exceed the capacity of SGEs recorded in the firmware, so the
416 * kernel driver should just adapt the value accordingly.
419 max_sge = roundup_pow_of_two(max_sge + 1);
421 hr_qp->rq.rsv_sge = 1;
426 static int set_rq_size(struct hns_roce_dev *hr_dev, struct ib_qp_cap *cap,
427 struct hns_roce_qp *hr_qp, int has_rq, bool user)
429 u32 max_sge = proc_rq_sge(hr_dev, hr_qp, user);
432 /* If srq exist, set zero for relative number of rq */
434 hr_qp->rq.wqe_cnt = 0;
435 hr_qp->rq.max_gs = 0;
436 hr_qp->rq_inl_buf.wqe_cnt = 0;
437 cap->max_recv_wr = 0;
438 cap->max_recv_sge = 0;
443 /* Check the validity of QP support capacity */
444 if (!cap->max_recv_wr || cap->max_recv_wr > hr_dev->caps.max_wqes ||
445 cap->max_recv_sge > max_sge) {
446 ibdev_err(&hr_dev->ib_dev,
447 "RQ config error, depth = %u, sge = %u\n",
448 cap->max_recv_wr, cap->max_recv_sge);
452 cnt = roundup_pow_of_two(max(cap->max_recv_wr, hr_dev->caps.min_wqes));
453 if (cnt > hr_dev->caps.max_wqes) {
454 ibdev_err(&hr_dev->ib_dev, "rq depth %u too large\n",
459 hr_qp->rq.max_gs = roundup_pow_of_two(max(1U, cap->max_recv_sge) +
462 hr_qp->rq.wqe_shift = ilog2(hr_dev->caps.max_rq_desc_sz *
465 hr_qp->rq.wqe_cnt = cnt;
466 if (hr_dev->caps.flags & HNS_ROCE_CAP_FLAG_RQ_INLINE &&
467 hr_qp->ibqp.qp_type != IB_QPT_UD &&
468 hr_qp->ibqp.qp_type != IB_QPT_GSI)
469 hr_qp->rq_inl_buf.wqe_cnt = cnt;
471 hr_qp->rq_inl_buf.wqe_cnt = 0;
473 cap->max_recv_wr = cnt;
474 cap->max_recv_sge = hr_qp->rq.max_gs - hr_qp->rq.rsv_sge;
479 static u32 get_wqe_ext_sge_cnt(struct hns_roce_qp *qp)
481 /* GSI/UD QP only has extended sge */
482 if (qp->ibqp.qp_type == IB_QPT_GSI || qp->ibqp.qp_type == IB_QPT_UD)
483 return qp->sq.max_gs;
485 if (qp->sq.max_gs > HNS_ROCE_SGE_IN_WQE)
486 return qp->sq.max_gs - HNS_ROCE_SGE_IN_WQE;
491 static void set_ext_sge_param(struct hns_roce_dev *hr_dev, u32 sq_wqe_cnt,
492 struct hns_roce_qp *hr_qp, struct ib_qp_cap *cap)
497 hr_qp->sge.sge_shift = HNS_ROCE_SGE_SHIFT;
499 hr_qp->sq.max_gs = max(1U, cap->max_send_sge);
501 wqe_sge_cnt = get_wqe_ext_sge_cnt(hr_qp);
503 /* If the number of extended sge is not zero, they MUST use the
504 * space of HNS_HW_PAGE_SIZE at least.
507 total_sge_cnt = roundup_pow_of_two(sq_wqe_cnt * wqe_sge_cnt);
508 hr_qp->sge.sge_cnt = max(total_sge_cnt,
509 (u32)HNS_HW_PAGE_SIZE / HNS_ROCE_SGE_SIZE);
513 static int check_sq_size_with_integrity(struct hns_roce_dev *hr_dev,
514 struct ib_qp_cap *cap,
515 struct hns_roce_ib_create_qp *ucmd)
517 u32 roundup_sq_stride = roundup_pow_of_two(hr_dev->caps.max_sq_desc_sz);
518 u8 max_sq_stride = ilog2(roundup_sq_stride);
520 /* Sanity check SQ size before proceeding */
521 if (ucmd->log_sq_stride > max_sq_stride ||
522 ucmd->log_sq_stride < HNS_ROCE_IB_MIN_SQ_STRIDE) {
523 ibdev_err(&hr_dev->ib_dev, "failed to check SQ stride size.\n");
527 if (cap->max_send_sge > hr_dev->caps.max_sq_sg) {
528 ibdev_err(&hr_dev->ib_dev, "failed to check SQ SGE size %u.\n",
536 static int set_user_sq_size(struct hns_roce_dev *hr_dev,
537 struct ib_qp_cap *cap, struct hns_roce_qp *hr_qp,
538 struct hns_roce_ib_create_qp *ucmd)
540 struct ib_device *ibdev = &hr_dev->ib_dev;
544 if (check_shl_overflow(1, ucmd->log_sq_bb_count, &cnt) ||
545 cnt > hr_dev->caps.max_wqes)
548 ret = check_sq_size_with_integrity(hr_dev, cap, ucmd);
550 ibdev_err(ibdev, "failed to check user SQ size, ret = %d.\n",
555 set_ext_sge_param(hr_dev, cnt, hr_qp, cap);
557 hr_qp->sq.wqe_shift = ucmd->log_sq_stride;
558 hr_qp->sq.wqe_cnt = cnt;
563 static int set_wqe_buf_attr(struct hns_roce_dev *hr_dev,
564 struct hns_roce_qp *hr_qp,
565 struct hns_roce_buf_attr *buf_attr)
570 hr_qp->buff_size = 0;
573 hr_qp->sq.offset = 0;
574 buf_size = to_hr_hem_entries_size(hr_qp->sq.wqe_cnt,
575 hr_qp->sq.wqe_shift);
576 if (buf_size > 0 && idx < ARRAY_SIZE(buf_attr->region)) {
577 buf_attr->region[idx].size = buf_size;
578 buf_attr->region[idx].hopnum = hr_dev->caps.wqe_sq_hop_num;
580 hr_qp->buff_size += buf_size;
583 /* extend SGE WQE in SQ */
584 hr_qp->sge.offset = hr_qp->buff_size;
585 buf_size = to_hr_hem_entries_size(hr_qp->sge.sge_cnt,
586 hr_qp->sge.sge_shift);
587 if (buf_size > 0 && idx < ARRAY_SIZE(buf_attr->region)) {
588 buf_attr->region[idx].size = buf_size;
589 buf_attr->region[idx].hopnum = hr_dev->caps.wqe_sge_hop_num;
591 hr_qp->buff_size += buf_size;
595 hr_qp->rq.offset = hr_qp->buff_size;
596 buf_size = to_hr_hem_entries_size(hr_qp->rq.wqe_cnt,
597 hr_qp->rq.wqe_shift);
598 if (buf_size > 0 && idx < ARRAY_SIZE(buf_attr->region)) {
599 buf_attr->region[idx].size = buf_size;
600 buf_attr->region[idx].hopnum = hr_dev->caps.wqe_rq_hop_num;
602 hr_qp->buff_size += buf_size;
605 if (hr_qp->buff_size < 1)
608 buf_attr->page_shift = HNS_HW_PAGE_SHIFT + hr_dev->caps.mtt_buf_pg_sz;
609 buf_attr->region_count = idx;
614 static int set_kernel_sq_size(struct hns_roce_dev *hr_dev,
615 struct ib_qp_cap *cap, struct hns_roce_qp *hr_qp)
617 struct ib_device *ibdev = &hr_dev->ib_dev;
620 if (!cap->max_send_wr || cap->max_send_wr > hr_dev->caps.max_wqes ||
621 cap->max_send_sge > hr_dev->caps.max_sq_sg) {
622 ibdev_err(ibdev, "failed to check SQ WR or SGE num.\n");
626 cnt = roundup_pow_of_two(max(cap->max_send_wr, hr_dev->caps.min_wqes));
627 if (cnt > hr_dev->caps.max_wqes) {
628 ibdev_err(ibdev, "failed to check WQE num, WQE num = %u.\n",
633 hr_qp->sq.wqe_shift = ilog2(hr_dev->caps.max_sq_desc_sz);
634 hr_qp->sq.wqe_cnt = cnt;
636 set_ext_sge_param(hr_dev, cnt, hr_qp, cap);
638 /* sync the parameters of kernel QP to user's configuration */
639 cap->max_send_wr = cnt;
640 cap->max_send_sge = hr_qp->sq.max_gs;
645 static int hns_roce_qp_has_sq(struct ib_qp_init_attr *attr)
647 if (attr->qp_type == IB_QPT_XRC_TGT || !attr->cap.max_send_wr)
653 static int hns_roce_qp_has_rq(struct ib_qp_init_attr *attr)
655 if (attr->qp_type == IB_QPT_XRC_INI ||
656 attr->qp_type == IB_QPT_XRC_TGT || attr->srq ||
657 !attr->cap.max_recv_wr)
663 static int alloc_rq_inline_buf(struct hns_roce_qp *hr_qp,
664 struct ib_qp_init_attr *init_attr)
666 u32 max_recv_sge = init_attr->cap.max_recv_sge;
667 u32 wqe_cnt = hr_qp->rq_inl_buf.wqe_cnt;
668 struct hns_roce_rinl_wqe *wqe_list;
671 /* allocate recv inline buf */
672 wqe_list = kcalloc(wqe_cnt, sizeof(struct hns_roce_rinl_wqe),
677 /* Allocate a continuous buffer for all inline sge we need */
678 wqe_list[0].sg_list = kcalloc(wqe_cnt, (max_recv_sge *
679 sizeof(struct hns_roce_rinl_sge)),
681 if (!wqe_list[0].sg_list)
684 /* Assign buffers of sg_list to each inline wqe */
685 for (i = 1; i < wqe_cnt; i++)
686 wqe_list[i].sg_list = &wqe_list[0].sg_list[i * max_recv_sge];
688 hr_qp->rq_inl_buf.wqe_list = wqe_list;
699 static void free_rq_inline_buf(struct hns_roce_qp *hr_qp)
701 if (hr_qp->rq_inl_buf.wqe_list)
702 kfree(hr_qp->rq_inl_buf.wqe_list[0].sg_list);
703 kfree(hr_qp->rq_inl_buf.wqe_list);
706 static int alloc_qp_buf(struct hns_roce_dev *hr_dev, struct hns_roce_qp *hr_qp,
707 struct ib_qp_init_attr *init_attr,
708 struct ib_udata *udata, unsigned long addr)
710 struct ib_device *ibdev = &hr_dev->ib_dev;
711 struct hns_roce_buf_attr buf_attr = {};
714 if (!udata && hr_qp->rq_inl_buf.wqe_cnt) {
715 ret = alloc_rq_inline_buf(hr_qp, init_attr);
718 "failed to alloc inline buf, ret = %d.\n",
723 hr_qp->rq_inl_buf.wqe_list = NULL;
726 ret = set_wqe_buf_attr(hr_dev, hr_qp, &buf_attr);
728 ibdev_err(ibdev, "failed to split WQE buf, ret = %d.\n", ret);
731 ret = hns_roce_mtr_create(hr_dev, &hr_qp->mtr, &buf_attr,
732 PAGE_SHIFT + hr_dev->caps.mtt_ba_pg_sz,
735 ibdev_err(ibdev, "failed to create WQE mtr, ret = %d.\n", ret);
739 if (hr_dev->caps.flags & HNS_ROCE_CAP_FLAG_DIRECT_WQE)
740 hr_qp->en_flags |= HNS_ROCE_QP_CAP_DIRECT_WQE;
745 free_rq_inline_buf(hr_qp);
750 static void free_qp_buf(struct hns_roce_dev *hr_dev, struct hns_roce_qp *hr_qp)
752 hns_roce_mtr_destroy(hr_dev, &hr_qp->mtr);
753 free_rq_inline_buf(hr_qp);
756 static inline bool user_qp_has_sdb(struct hns_roce_dev *hr_dev,
757 struct ib_qp_init_attr *init_attr,
758 struct ib_udata *udata,
759 struct hns_roce_ib_create_qp_resp *resp,
760 struct hns_roce_ib_create_qp *ucmd)
762 return ((hr_dev->caps.flags & HNS_ROCE_CAP_FLAG_QP_RECORD_DB) &&
763 udata->outlen >= offsetofend(typeof(*resp), cap_flags) &&
764 hns_roce_qp_has_sq(init_attr) &&
765 udata->inlen >= offsetofend(typeof(*ucmd), sdb_addr));
768 static inline bool user_qp_has_rdb(struct hns_roce_dev *hr_dev,
769 struct ib_qp_init_attr *init_attr,
770 struct ib_udata *udata,
771 struct hns_roce_ib_create_qp_resp *resp)
773 return ((hr_dev->caps.flags & HNS_ROCE_CAP_FLAG_QP_RECORD_DB) &&
774 udata->outlen >= offsetofend(typeof(*resp), cap_flags) &&
775 hns_roce_qp_has_rq(init_attr));
778 static inline bool kernel_qp_has_rdb(struct hns_roce_dev *hr_dev,
779 struct ib_qp_init_attr *init_attr)
781 return ((hr_dev->caps.flags & HNS_ROCE_CAP_FLAG_QP_RECORD_DB) &&
782 hns_roce_qp_has_rq(init_attr));
785 static int qp_mmap_entry(struct hns_roce_qp *hr_qp,
786 struct hns_roce_dev *hr_dev,
787 struct ib_udata *udata,
788 struct hns_roce_ib_create_qp_resp *resp)
790 struct hns_roce_ucontext *uctx =
791 rdma_udata_to_drv_context(udata,
792 struct hns_roce_ucontext, ibucontext);
793 struct rdma_user_mmap_entry *rdma_entry;
796 address = hr_dev->dwqe_page + hr_qp->qpn * HNS_ROCE_DWQE_SIZE;
798 hr_qp->dwqe_mmap_entry =
799 hns_roce_user_mmap_entry_insert(&uctx->ibucontext, address,
801 HNS_ROCE_MMAP_TYPE_DWQE);
803 if (!hr_qp->dwqe_mmap_entry) {
804 ibdev_err(&hr_dev->ib_dev, "failed to get dwqe mmap entry.\n");
808 rdma_entry = &hr_qp->dwqe_mmap_entry->rdma_entry;
809 resp->dwqe_mmap_key = rdma_user_mmap_get_offset(rdma_entry);
814 static int alloc_user_qp_db(struct hns_roce_dev *hr_dev,
815 struct hns_roce_qp *hr_qp,
816 struct ib_qp_init_attr *init_attr,
817 struct ib_udata *udata,
818 struct hns_roce_ib_create_qp *ucmd,
819 struct hns_roce_ib_create_qp_resp *resp)
821 struct hns_roce_ucontext *uctx = rdma_udata_to_drv_context(udata,
822 struct hns_roce_ucontext, ibucontext);
823 struct ib_device *ibdev = &hr_dev->ib_dev;
826 if (user_qp_has_sdb(hr_dev, init_attr, udata, resp, ucmd)) {
827 ret = hns_roce_db_map_user(uctx, ucmd->sdb_addr, &hr_qp->sdb);
830 "failed to map user SQ doorbell, ret = %d.\n",
834 hr_qp->en_flags |= HNS_ROCE_QP_CAP_SQ_RECORD_DB;
837 if (user_qp_has_rdb(hr_dev, init_attr, udata, resp)) {
838 ret = hns_roce_db_map_user(uctx, ucmd->db_addr, &hr_qp->rdb);
841 "failed to map user RQ doorbell, ret = %d.\n",
845 hr_qp->en_flags |= HNS_ROCE_QP_CAP_RQ_RECORD_DB;
851 if (hr_qp->en_flags & HNS_ROCE_QP_CAP_SQ_RECORD_DB)
852 hns_roce_db_unmap_user(uctx, &hr_qp->sdb);
857 static int alloc_kernel_qp_db(struct hns_roce_dev *hr_dev,
858 struct hns_roce_qp *hr_qp,
859 struct ib_qp_init_attr *init_attr)
861 struct ib_device *ibdev = &hr_dev->ib_dev;
864 if (hr_dev->pci_dev->revision >= PCI_REVISION_ID_HIP09)
865 hr_qp->sq.db_reg = hr_dev->mem_base +
866 HNS_ROCE_DWQE_SIZE * hr_qp->qpn;
868 hr_qp->sq.db_reg = hr_dev->reg_base + hr_dev->sdb_offset +
869 DB_REG_OFFSET * hr_dev->priv_uar.index;
871 hr_qp->rq.db_reg = hr_dev->reg_base + hr_dev->odb_offset +
872 DB_REG_OFFSET * hr_dev->priv_uar.index;
874 if (kernel_qp_has_rdb(hr_dev, init_attr)) {
875 ret = hns_roce_alloc_db(hr_dev, &hr_qp->rdb, 0);
878 "failed to alloc kernel RQ doorbell, ret = %d.\n",
882 *hr_qp->rdb.db_record = 0;
883 hr_qp->en_flags |= HNS_ROCE_QP_CAP_RQ_RECORD_DB;
889 static int alloc_qp_db(struct hns_roce_dev *hr_dev, struct hns_roce_qp *hr_qp,
890 struct ib_qp_init_attr *init_attr,
891 struct ib_udata *udata,
892 struct hns_roce_ib_create_qp *ucmd,
893 struct hns_roce_ib_create_qp_resp *resp)
897 if (hr_dev->caps.flags & HNS_ROCE_CAP_FLAG_SDI_MODE)
898 hr_qp->en_flags |= HNS_ROCE_QP_CAP_OWNER_DB;
901 if (hr_qp->en_flags & HNS_ROCE_QP_CAP_DIRECT_WQE) {
902 ret = qp_mmap_entry(hr_qp, hr_dev, udata, resp);
907 ret = alloc_user_qp_db(hr_dev, hr_qp, init_attr, udata, ucmd,
912 ret = alloc_kernel_qp_db(hr_dev, hr_qp, init_attr);
920 if (hr_qp->en_flags & HNS_ROCE_QP_CAP_DIRECT_WQE)
921 qp_user_mmap_entry_remove(hr_qp);
926 static void free_qp_db(struct hns_roce_dev *hr_dev, struct hns_roce_qp *hr_qp,
927 struct ib_udata *udata)
929 struct hns_roce_ucontext *uctx = rdma_udata_to_drv_context(
930 udata, struct hns_roce_ucontext, ibucontext);
933 if (hr_qp->en_flags & HNS_ROCE_QP_CAP_RQ_RECORD_DB)
934 hns_roce_db_unmap_user(uctx, &hr_qp->rdb);
935 if (hr_qp->en_flags & HNS_ROCE_QP_CAP_SQ_RECORD_DB)
936 hns_roce_db_unmap_user(uctx, &hr_qp->sdb);
937 if (hr_qp->en_flags & HNS_ROCE_QP_CAP_DIRECT_WQE)
938 qp_user_mmap_entry_remove(hr_qp);
940 if (hr_qp->en_flags & HNS_ROCE_QP_CAP_RQ_RECORD_DB)
941 hns_roce_free_db(hr_dev, &hr_qp->rdb);
945 static int alloc_kernel_wrid(struct hns_roce_dev *hr_dev,
946 struct hns_roce_qp *hr_qp)
948 struct ib_device *ibdev = &hr_dev->ib_dev;
953 sq_wrid = kcalloc(hr_qp->sq.wqe_cnt, sizeof(u64), GFP_KERNEL);
954 if (ZERO_OR_NULL_PTR(sq_wrid)) {
955 ibdev_err(ibdev, "failed to alloc SQ wrid.\n");
959 if (hr_qp->rq.wqe_cnt) {
960 rq_wrid = kcalloc(hr_qp->rq.wqe_cnt, sizeof(u64), GFP_KERNEL);
961 if (ZERO_OR_NULL_PTR(rq_wrid)) {
962 ibdev_err(ibdev, "failed to alloc RQ wrid.\n");
968 hr_qp->sq.wrid = sq_wrid;
969 hr_qp->rq.wrid = rq_wrid;
977 static void free_kernel_wrid(struct hns_roce_qp *hr_qp)
979 kfree(hr_qp->rq.wrid);
980 kfree(hr_qp->sq.wrid);
983 static int set_qp_param(struct hns_roce_dev *hr_dev, struct hns_roce_qp *hr_qp,
984 struct ib_qp_init_attr *init_attr,
985 struct ib_udata *udata,
986 struct hns_roce_ib_create_qp *ucmd)
988 struct ib_device *ibdev = &hr_dev->ib_dev;
991 if (init_attr->cap.max_inline_data > hr_dev->caps.max_sq_inline)
992 init_attr->cap.max_inline_data = hr_dev->caps.max_sq_inline;
994 hr_qp->max_inline_data = init_attr->cap.max_inline_data;
996 if (init_attr->sq_sig_type == IB_SIGNAL_ALL_WR)
997 hr_qp->sq_signal_bits = IB_SIGNAL_ALL_WR;
999 hr_qp->sq_signal_bits = IB_SIGNAL_REQ_WR;
1001 ret = set_rq_size(hr_dev, &init_attr->cap, hr_qp,
1002 hns_roce_qp_has_rq(init_attr), !!udata);
1004 ibdev_err(ibdev, "failed to set user RQ size, ret = %d.\n",
1010 ret = ib_copy_from_udata(ucmd, udata,
1011 min(udata->inlen, sizeof(*ucmd)));
1014 "failed to copy QP ucmd, ret = %d\n", ret);
1018 ret = set_user_sq_size(hr_dev, &init_attr->cap, hr_qp, ucmd);
1021 "failed to set user SQ size, ret = %d.\n",
1024 ret = set_kernel_sq_size(hr_dev, &init_attr->cap, hr_qp);
1027 "failed to set kernel SQ size, ret = %d.\n",
1034 static int hns_roce_create_qp_common(struct hns_roce_dev *hr_dev,
1035 struct ib_pd *ib_pd,
1036 struct ib_qp_init_attr *init_attr,
1037 struct ib_udata *udata,
1038 struct hns_roce_qp *hr_qp)
1040 struct hns_roce_ib_create_qp_resp resp = {};
1041 struct ib_device *ibdev = &hr_dev->ib_dev;
1042 struct hns_roce_ib_create_qp ucmd;
1045 mutex_init(&hr_qp->mutex);
1046 spin_lock_init(&hr_qp->sq.lock);
1047 spin_lock_init(&hr_qp->rq.lock);
1049 hr_qp->state = IB_QPS_RESET;
1050 hr_qp->flush_flag = 0;
1052 if (init_attr->create_flags)
1055 ret = set_qp_param(hr_dev, hr_qp, init_attr, udata, &ucmd);
1057 ibdev_err(ibdev, "failed to set QP param, ret = %d.\n", ret);
1062 ret = alloc_kernel_wrid(hr_dev, hr_qp);
1064 ibdev_err(ibdev, "failed to alloc wrid, ret = %d.\n",
1070 ret = alloc_qp_buf(hr_dev, hr_qp, init_attr, udata, ucmd.buf_addr);
1072 ibdev_err(ibdev, "failed to alloc QP buffer, ret = %d.\n", ret);
1076 ret = alloc_qpn(hr_dev, hr_qp);
1078 ibdev_err(ibdev, "failed to alloc QPN, ret = %d.\n", ret);
1082 ret = alloc_qp_db(hr_dev, hr_qp, init_attr, udata, &ucmd, &resp);
1084 ibdev_err(ibdev, "failed to alloc QP doorbell, ret = %d.\n",
1089 ret = alloc_qpc(hr_dev, hr_qp);
1091 ibdev_err(ibdev, "failed to alloc QP context, ret = %d.\n",
1096 ret = hns_roce_qp_store(hr_dev, hr_qp, init_attr);
1098 ibdev_err(ibdev, "failed to store QP, ret = %d.\n", ret);
1103 resp.cap_flags = hr_qp->en_flags;
1104 ret = ib_copy_to_udata(udata, &resp,
1105 min(udata->outlen, sizeof(resp)));
1107 ibdev_err(ibdev, "copy qp resp failed!\n");
1112 if (hr_dev->caps.flags & HNS_ROCE_CAP_FLAG_QP_FLOW_CTRL) {
1113 ret = hr_dev->hw->qp_flow_control_init(hr_dev, hr_qp);
1118 hr_qp->ibqp.qp_num = hr_qp->qpn;
1119 hr_qp->event = hns_roce_ib_qp_event;
1120 refcount_set(&hr_qp->refcount, 1);
1121 init_completion(&hr_qp->free);
1126 hns_roce_qp_remove(hr_dev, hr_qp);
1128 free_qpc(hr_dev, hr_qp);
1130 free_qp_db(hr_dev, hr_qp, udata);
1132 free_qpn(hr_dev, hr_qp);
1134 free_qp_buf(hr_dev, hr_qp);
1136 free_kernel_wrid(hr_qp);
1140 void hns_roce_qp_destroy(struct hns_roce_dev *hr_dev, struct hns_roce_qp *hr_qp,
1141 struct ib_udata *udata)
1143 if (refcount_dec_and_test(&hr_qp->refcount))
1144 complete(&hr_qp->free);
1145 wait_for_completion(&hr_qp->free);
1147 free_qpc(hr_dev, hr_qp);
1148 free_qpn(hr_dev, hr_qp);
1149 free_qp_buf(hr_dev, hr_qp);
1150 free_kernel_wrid(hr_qp);
1151 free_qp_db(hr_dev, hr_qp, udata);
1154 static int check_qp_type(struct hns_roce_dev *hr_dev, enum ib_qp_type type,
1158 case IB_QPT_XRC_INI:
1159 case IB_QPT_XRC_TGT:
1160 if (!(hr_dev->caps.flags & HNS_ROCE_CAP_FLAG_XRC))
1164 if (hr_dev->pci_dev->revision == PCI_REVISION_ID_HIP08 &&
1178 ibdev_err(&hr_dev->ib_dev, "not support QP type %d\n", type);
1183 int hns_roce_create_qp(struct ib_qp *qp, struct ib_qp_init_attr *init_attr,
1184 struct ib_udata *udata)
1186 struct ib_device *ibdev = qp->device;
1187 struct hns_roce_dev *hr_dev = to_hr_dev(ibdev);
1188 struct hns_roce_qp *hr_qp = to_hr_qp(qp);
1189 struct ib_pd *pd = qp->pd;
1192 ret = check_qp_type(hr_dev, init_attr->qp_type, !!udata);
1196 if (init_attr->qp_type == IB_QPT_XRC_TGT)
1197 hr_qp->xrcdn = to_hr_xrcd(init_attr->xrcd)->xrcdn;
1199 if (init_attr->qp_type == IB_QPT_GSI) {
1200 hr_qp->port = init_attr->port_num - 1;
1201 hr_qp->phy_port = hr_dev->iboe.phy_port[hr_qp->port];
1204 ret = hns_roce_create_qp_common(hr_dev, pd, init_attr, udata, hr_qp);
1206 ibdev_err(ibdev, "create QP type 0x%x failed(%d)\n",
1207 init_attr->qp_type, ret);
1212 int to_hr_qp_type(int qp_type)
1216 return SERV_TYPE_RC;
1219 return SERV_TYPE_UD;
1220 case IB_QPT_XRC_INI:
1221 case IB_QPT_XRC_TGT:
1222 return SERV_TYPE_XRC;
1228 static int check_mtu_validate(struct hns_roce_dev *hr_dev,
1229 struct hns_roce_qp *hr_qp,
1230 struct ib_qp_attr *attr, int attr_mask)
1232 enum ib_mtu active_mtu;
1235 p = attr_mask & IB_QP_PORT ? (attr->port_num - 1) : hr_qp->port;
1236 active_mtu = iboe_get_mtu(hr_dev->iboe.netdevs[p]->mtu);
1238 if ((hr_dev->caps.max_mtu >= IB_MTU_2048 &&
1239 attr->path_mtu > hr_dev->caps.max_mtu) ||
1240 attr->path_mtu < IB_MTU_256 || attr->path_mtu > active_mtu) {
1241 ibdev_err(&hr_dev->ib_dev,
1242 "attr path_mtu(%d)invalid while modify qp",
1250 static int hns_roce_check_qp_attr(struct ib_qp *ibqp, struct ib_qp_attr *attr,
1253 struct hns_roce_dev *hr_dev = to_hr_dev(ibqp->device);
1254 struct hns_roce_qp *hr_qp = to_hr_qp(ibqp);
1257 if ((attr_mask & IB_QP_PORT) &&
1258 (attr->port_num == 0 || attr->port_num > hr_dev->caps.num_ports)) {
1259 ibdev_err(&hr_dev->ib_dev, "invalid attr, port_num = %u.\n",
1264 if (attr_mask & IB_QP_PKEY_INDEX) {
1265 p = attr_mask & IB_QP_PORT ? (attr->port_num - 1) : hr_qp->port;
1266 if (attr->pkey_index >= hr_dev->caps.pkey_table_len[p]) {
1267 ibdev_err(&hr_dev->ib_dev,
1268 "invalid attr, pkey_index = %u.\n",
1274 if (attr_mask & IB_QP_MAX_QP_RD_ATOMIC &&
1275 attr->max_rd_atomic > hr_dev->caps.max_qp_init_rdma) {
1276 ibdev_err(&hr_dev->ib_dev,
1277 "invalid attr, max_rd_atomic = %u.\n",
1278 attr->max_rd_atomic);
1282 if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC &&
1283 attr->max_dest_rd_atomic > hr_dev->caps.max_qp_dest_rdma) {
1284 ibdev_err(&hr_dev->ib_dev,
1285 "invalid attr, max_dest_rd_atomic = %u.\n",
1286 attr->max_dest_rd_atomic);
1290 if (attr_mask & IB_QP_PATH_MTU)
1291 return check_mtu_validate(hr_dev, hr_qp, attr, attr_mask);
1296 int hns_roce_modify_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
1297 int attr_mask, struct ib_udata *udata)
1299 struct hns_roce_dev *hr_dev = to_hr_dev(ibqp->device);
1300 struct hns_roce_qp *hr_qp = to_hr_qp(ibqp);
1301 enum ib_qp_state cur_state, new_state;
1304 mutex_lock(&hr_qp->mutex);
1306 if (attr_mask & IB_QP_CUR_STATE && attr->cur_qp_state != hr_qp->state)
1309 cur_state = hr_qp->state;
1310 new_state = attr_mask & IB_QP_STATE ? attr->qp_state : cur_state;
1312 if (ibqp->uobject &&
1313 (attr_mask & IB_QP_STATE) && new_state == IB_QPS_ERR) {
1314 if (hr_qp->en_flags & HNS_ROCE_QP_CAP_SQ_RECORD_DB) {
1315 hr_qp->sq.head = *(int *)(hr_qp->sdb.virt_addr);
1317 if (hr_qp->en_flags & HNS_ROCE_QP_CAP_RQ_RECORD_DB)
1318 hr_qp->rq.head = *(int *)(hr_qp->rdb.virt_addr);
1320 ibdev_warn(&hr_dev->ib_dev,
1321 "flush cqe is not supported in userspace!\n");
1326 if (!ib_modify_qp_is_ok(cur_state, new_state, ibqp->qp_type,
1328 ibdev_err(&hr_dev->ib_dev, "ib_modify_qp_is_ok failed\n");
1332 ret = hns_roce_check_qp_attr(ibqp, attr, attr_mask);
1336 if (cur_state == new_state && cur_state == IB_QPS_RESET)
1339 ret = hr_dev->hw->modify_qp(ibqp, attr, attr_mask, cur_state,
1343 mutex_unlock(&hr_qp->mutex);
1348 void hns_roce_lock_cqs(struct hns_roce_cq *send_cq, struct hns_roce_cq *recv_cq)
1349 __acquires(&send_cq->lock) __acquires(&recv_cq->lock)
1351 if (unlikely(send_cq == NULL && recv_cq == NULL)) {
1352 __acquire(&send_cq->lock);
1353 __acquire(&recv_cq->lock);
1354 } else if (unlikely(send_cq != NULL && recv_cq == NULL)) {
1355 spin_lock_irq(&send_cq->lock);
1356 __acquire(&recv_cq->lock);
1357 } else if (unlikely(send_cq == NULL && recv_cq != NULL)) {
1358 spin_lock_irq(&recv_cq->lock);
1359 __acquire(&send_cq->lock);
1360 } else if (send_cq == recv_cq) {
1361 spin_lock_irq(&send_cq->lock);
1362 __acquire(&recv_cq->lock);
1363 } else if (send_cq->cqn < recv_cq->cqn) {
1364 spin_lock_irq(&send_cq->lock);
1365 spin_lock_nested(&recv_cq->lock, SINGLE_DEPTH_NESTING);
1367 spin_lock_irq(&recv_cq->lock);
1368 spin_lock_nested(&send_cq->lock, SINGLE_DEPTH_NESTING);
1372 void hns_roce_unlock_cqs(struct hns_roce_cq *send_cq,
1373 struct hns_roce_cq *recv_cq) __releases(&send_cq->lock)
1374 __releases(&recv_cq->lock)
1376 if (unlikely(send_cq == NULL && recv_cq == NULL)) {
1377 __release(&recv_cq->lock);
1378 __release(&send_cq->lock);
1379 } else if (unlikely(send_cq != NULL && recv_cq == NULL)) {
1380 __release(&recv_cq->lock);
1381 spin_unlock(&send_cq->lock);
1382 } else if (unlikely(send_cq == NULL && recv_cq != NULL)) {
1383 __release(&send_cq->lock);
1384 spin_unlock(&recv_cq->lock);
1385 } else if (send_cq == recv_cq) {
1386 __release(&recv_cq->lock);
1387 spin_unlock_irq(&send_cq->lock);
1388 } else if (send_cq->cqn < recv_cq->cqn) {
1389 spin_unlock(&recv_cq->lock);
1390 spin_unlock_irq(&send_cq->lock);
1392 spin_unlock(&send_cq->lock);
1393 spin_unlock_irq(&recv_cq->lock);
1397 static inline void *get_wqe(struct hns_roce_qp *hr_qp, u32 offset)
1399 return hns_roce_buf_offset(hr_qp->mtr.kmem, offset);
1402 void *hns_roce_get_recv_wqe(struct hns_roce_qp *hr_qp, unsigned int n)
1404 return get_wqe(hr_qp, hr_qp->rq.offset + (n << hr_qp->rq.wqe_shift));
1407 void *hns_roce_get_send_wqe(struct hns_roce_qp *hr_qp, unsigned int n)
1409 return get_wqe(hr_qp, hr_qp->sq.offset + (n << hr_qp->sq.wqe_shift));
1412 void *hns_roce_get_extend_sge(struct hns_roce_qp *hr_qp, unsigned int n)
1414 return get_wqe(hr_qp, hr_qp->sge.offset + (n << hr_qp->sge.sge_shift));
1417 bool hns_roce_wq_overflow(struct hns_roce_wq *hr_wq, u32 nreq,
1418 struct ib_cq *ib_cq)
1420 struct hns_roce_cq *hr_cq;
1423 cur = hr_wq->head - hr_wq->tail;
1424 if (likely(cur + nreq < hr_wq->wqe_cnt))
1427 hr_cq = to_hr_cq(ib_cq);
1428 spin_lock(&hr_cq->lock);
1429 cur = hr_wq->head - hr_wq->tail;
1430 spin_unlock(&hr_cq->lock);
1432 return cur + nreq >= hr_wq->wqe_cnt;
1435 int hns_roce_init_qp_table(struct hns_roce_dev *hr_dev)
1437 struct hns_roce_qp_table *qp_table = &hr_dev->qp_table;
1438 unsigned int reserved_from_bot;
1441 qp_table->idx_table.spare_idx = kcalloc(hr_dev->caps.num_qps,
1442 sizeof(u32), GFP_KERNEL);
1443 if (!qp_table->idx_table.spare_idx)
1446 mutex_init(&qp_table->scc_mutex);
1447 mutex_init(&qp_table->bank_mutex);
1448 xa_init(&hr_dev->qp_table_xa);
1450 reserved_from_bot = hr_dev->caps.reserved_qps;
1452 for (i = 0; i < reserved_from_bot; i++) {
1453 hr_dev->qp_table.bank[get_qp_bankid(i)].inuse++;
1454 hr_dev->qp_table.bank[get_qp_bankid(i)].min++;
1457 for (i = 0; i < HNS_ROCE_QP_BANK_NUM; i++) {
1458 ida_init(&hr_dev->qp_table.bank[i].ida);
1459 hr_dev->qp_table.bank[i].max = hr_dev->caps.num_qps /
1460 HNS_ROCE_QP_BANK_NUM - 1;
1461 hr_dev->qp_table.bank[i].next = hr_dev->qp_table.bank[i].min;
1467 void hns_roce_cleanup_qp_table(struct hns_roce_dev *hr_dev)
1471 for (i = 0; i < HNS_ROCE_QP_BANK_NUM; i++)
1472 ida_destroy(&hr_dev->qp_table.bank[i].ida);
1473 kfree(hr_dev->qp_table.idx_table.spare_idx);