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 <linux/platform_device.h>
36 #include <rdma/ib_addr.h>
37 #include <rdma/ib_umem.h>
38 #include <rdma/uverbs_ioctl.h>
39 #include "hns_roce_common.h"
40 #include "hns_roce_device.h"
41 #include "hns_roce_hem.h"
43 static void flush_work_handle(struct work_struct *work)
45 struct hns_roce_work *flush_work = container_of(work,
46 struct hns_roce_work, work);
47 struct hns_roce_qp *hr_qp = container_of(flush_work,
48 struct hns_roce_qp, flush_work);
49 struct device *dev = flush_work->hr_dev->dev;
50 struct ib_qp_attr attr;
54 attr_mask = IB_QP_STATE;
55 attr.qp_state = IB_QPS_ERR;
57 if (test_and_clear_bit(HNS_ROCE_FLUSH_FLAG, &hr_qp->flush_flag)) {
58 ret = hns_roce_modify_qp(&hr_qp->ibqp, &attr, attr_mask, NULL);
60 dev_err(dev, "Modify QP to error state failed(%d) during CQE flush\n",
65 * make sure we signal QP destroy leg that flush QP was completed
66 * so that it can safely proceed ahead now and destroy QP
68 if (refcount_dec_and_test(&hr_qp->refcount))
69 complete(&hr_qp->free);
72 void init_flush_work(struct hns_roce_dev *hr_dev, struct hns_roce_qp *hr_qp)
74 struct hns_roce_work *flush_work = &hr_qp->flush_work;
76 flush_work->hr_dev = hr_dev;
77 INIT_WORK(&flush_work->work, flush_work_handle);
78 refcount_inc(&hr_qp->refcount);
79 queue_work(hr_dev->irq_workq, &flush_work->work);
82 void flush_cqe(struct hns_roce_dev *dev, struct hns_roce_qp *qp)
85 * Hip08 hardware cannot flush the WQEs in SQ/RQ if the QP state
86 * gets into errored mode. Hence, as a workaround to this
87 * hardware limitation, driver needs to assist in flushing. But
88 * the flushing operation uses mailbox to convey the QP state to
89 * the hardware and which can sleep due to the mutex protection
90 * around the mailbox calls. Hence, use the deferred flush for
93 if (!test_and_set_bit(HNS_ROCE_FLUSH_FLAG, &qp->flush_flag))
94 init_flush_work(dev, qp);
97 void hns_roce_qp_event(struct hns_roce_dev *hr_dev, u32 qpn, int event_type)
99 struct device *dev = hr_dev->dev;
100 struct hns_roce_qp *qp;
102 xa_lock(&hr_dev->qp_table_xa);
103 qp = __hns_roce_qp_lookup(hr_dev, qpn);
105 refcount_inc(&qp->refcount);
106 xa_unlock(&hr_dev->qp_table_xa);
109 dev_warn(dev, "Async event for bogus QP %08x\n", qpn);
113 if (hr_dev->hw_rev != HNS_ROCE_HW_VER1 &&
114 (event_type == HNS_ROCE_EVENT_TYPE_WQ_CATAS_ERROR ||
115 event_type == HNS_ROCE_EVENT_TYPE_INV_REQ_LOCAL_WQ_ERROR ||
116 event_type == HNS_ROCE_EVENT_TYPE_LOCAL_WQ_ACCESS_ERROR ||
117 event_type == HNS_ROCE_EVENT_TYPE_XRCD_VIOLATION ||
118 event_type == HNS_ROCE_EVENT_TYPE_INVALID_XRCETH)) {
119 qp->state = IB_QPS_ERR;
121 flush_cqe(hr_dev, qp);
124 qp->event(qp, (enum hns_roce_event)event_type);
126 if (refcount_dec_and_test(&qp->refcount))
130 static void hns_roce_ib_qp_event(struct hns_roce_qp *hr_qp,
131 enum hns_roce_event type)
133 struct ib_qp *ibqp = &hr_qp->ibqp;
134 struct ib_event event;
136 if (ibqp->event_handler) {
137 event.device = ibqp->device;
138 event.element.qp = ibqp;
140 case HNS_ROCE_EVENT_TYPE_PATH_MIG:
141 event.event = IB_EVENT_PATH_MIG;
143 case HNS_ROCE_EVENT_TYPE_COMM_EST:
144 event.event = IB_EVENT_COMM_EST;
146 case HNS_ROCE_EVENT_TYPE_SQ_DRAINED:
147 event.event = IB_EVENT_SQ_DRAINED;
149 case HNS_ROCE_EVENT_TYPE_SRQ_LAST_WQE_REACH:
150 event.event = IB_EVENT_QP_LAST_WQE_REACHED;
152 case HNS_ROCE_EVENT_TYPE_WQ_CATAS_ERROR:
153 event.event = IB_EVENT_QP_FATAL;
155 case HNS_ROCE_EVENT_TYPE_PATH_MIG_FAILED:
156 event.event = IB_EVENT_PATH_MIG_ERR;
158 case HNS_ROCE_EVENT_TYPE_INV_REQ_LOCAL_WQ_ERROR:
159 event.event = IB_EVENT_QP_REQ_ERR;
161 case HNS_ROCE_EVENT_TYPE_LOCAL_WQ_ACCESS_ERROR:
162 case HNS_ROCE_EVENT_TYPE_XRCD_VIOLATION:
163 case HNS_ROCE_EVENT_TYPE_INVALID_XRCETH:
164 event.event = IB_EVENT_QP_ACCESS_ERR;
167 dev_dbg(ibqp->device->dev.parent, "roce_ib: Unexpected event type %d on QP %06lx\n",
171 ibqp->event_handler(&event, ibqp->qp_context);
175 static u8 get_least_load_bankid_for_qp(struct hns_roce_bank *bank)
177 u32 least_load = bank[0].inuse;
182 for (i = 1; i < HNS_ROCE_QP_BANK_NUM; i++) {
183 bankcnt = bank[i].inuse;
184 if (bankcnt < least_load) {
185 least_load = bankcnt;
193 static int alloc_qpn_with_bankid(struct hns_roce_bank *bank, u8 bankid,
198 id = ida_alloc_range(&bank->ida, bank->next, bank->max, GFP_KERNEL);
200 id = ida_alloc_range(&bank->ida, bank->min, bank->max,
206 /* the QPN should keep increasing until the max value is reached. */
207 bank->next = (id + 1) > bank->max ? bank->min : id + 1;
209 /* the lower 3 bits is bankid */
210 *qpn = (id << 3) | bankid;
214 static int alloc_qpn(struct hns_roce_dev *hr_dev, struct hns_roce_qp *hr_qp)
216 struct hns_roce_qp_table *qp_table = &hr_dev->qp_table;
217 unsigned long num = 0;
221 if (hr_qp->ibqp.qp_type == IB_QPT_GSI) {
222 /* when hw version is v1, the sqpn is allocated */
223 if (hr_dev->hw_rev == HNS_ROCE_HW_VER1)
224 num = HNS_ROCE_MAX_PORTS +
225 hr_dev->iboe.phy_port[hr_qp->port];
229 hr_qp->doorbell_qpn = 1;
231 mutex_lock(&qp_table->bank_mutex);
232 bankid = get_least_load_bankid_for_qp(qp_table->bank);
234 ret = alloc_qpn_with_bankid(&qp_table->bank[bankid], bankid,
237 ibdev_err(&hr_dev->ib_dev,
238 "failed to alloc QPN, ret = %d\n", ret);
239 mutex_unlock(&qp_table->bank_mutex);
243 qp_table->bank[bankid].inuse++;
244 mutex_unlock(&qp_table->bank_mutex);
246 hr_qp->doorbell_qpn = (u32)num;
254 enum hns_roce_qp_state to_hns_roce_state(enum ib_qp_state state)
258 return HNS_ROCE_QP_STATE_RST;
260 return HNS_ROCE_QP_STATE_INIT;
262 return HNS_ROCE_QP_STATE_RTR;
264 return HNS_ROCE_QP_STATE_RTS;
266 return HNS_ROCE_QP_STATE_SQD;
268 return HNS_ROCE_QP_STATE_ERR;
270 return HNS_ROCE_QP_NUM_STATE;
274 static void add_qp_to_list(struct hns_roce_dev *hr_dev,
275 struct hns_roce_qp *hr_qp,
276 struct ib_cq *send_cq, struct ib_cq *recv_cq)
278 struct hns_roce_cq *hr_send_cq, *hr_recv_cq;
281 hr_send_cq = send_cq ? to_hr_cq(send_cq) : NULL;
282 hr_recv_cq = recv_cq ? to_hr_cq(recv_cq) : NULL;
284 spin_lock_irqsave(&hr_dev->qp_list_lock, flags);
285 hns_roce_lock_cqs(hr_send_cq, hr_recv_cq);
287 list_add_tail(&hr_qp->node, &hr_dev->qp_list);
289 list_add_tail(&hr_qp->sq_node, &hr_send_cq->sq_list);
291 list_add_tail(&hr_qp->rq_node, &hr_recv_cq->rq_list);
293 hns_roce_unlock_cqs(hr_send_cq, hr_recv_cq);
294 spin_unlock_irqrestore(&hr_dev->qp_list_lock, flags);
297 static int hns_roce_qp_store(struct hns_roce_dev *hr_dev,
298 struct hns_roce_qp *hr_qp,
299 struct ib_qp_init_attr *init_attr)
301 struct xarray *xa = &hr_dev->qp_table_xa;
307 ret = xa_err(xa_store_irq(xa, hr_qp->qpn, hr_qp, GFP_KERNEL));
309 dev_err(hr_dev->dev, "Failed to xa store for QPC\n");
311 /* add QP to device's QP list for softwc */
312 add_qp_to_list(hr_dev, hr_qp, init_attr->send_cq,
318 static int alloc_qpc(struct hns_roce_dev *hr_dev, struct hns_roce_qp *hr_qp)
320 struct hns_roce_qp_table *qp_table = &hr_dev->qp_table;
321 struct device *dev = hr_dev->dev;
327 /* In v1 engine, GSI QP context is saved in the RoCE hw's register */
328 if (hr_qp->ibqp.qp_type == IB_QPT_GSI &&
329 hr_dev->hw_rev == HNS_ROCE_HW_VER1)
332 /* Alloc memory for QPC */
333 ret = hns_roce_table_get(hr_dev, &qp_table->qp_table, hr_qp->qpn);
335 dev_err(dev, "Failed to get QPC table\n");
339 /* Alloc memory for IRRL */
340 ret = hns_roce_table_get(hr_dev, &qp_table->irrl_table, hr_qp->qpn);
342 dev_err(dev, "Failed to get IRRL table\n");
346 if (hr_dev->caps.trrl_entry_sz) {
347 /* Alloc memory for TRRL */
348 ret = hns_roce_table_get(hr_dev, &qp_table->trrl_table,
351 dev_err(dev, "Failed to get TRRL table\n");
356 if (hr_dev->caps.flags & HNS_ROCE_CAP_FLAG_QP_FLOW_CTRL) {
357 /* Alloc memory for SCC CTX */
358 ret = hns_roce_table_get(hr_dev, &qp_table->sccc_table,
361 dev_err(dev, "Failed to get SCC CTX table\n");
369 if (hr_dev->caps.trrl_entry_sz)
370 hns_roce_table_put(hr_dev, &qp_table->trrl_table, hr_qp->qpn);
373 hns_roce_table_put(hr_dev, &qp_table->irrl_table, hr_qp->qpn);
376 hns_roce_table_put(hr_dev, &qp_table->qp_table, hr_qp->qpn);
382 void hns_roce_qp_remove(struct hns_roce_dev *hr_dev, struct hns_roce_qp *hr_qp)
384 struct xarray *xa = &hr_dev->qp_table_xa;
387 list_del(&hr_qp->node);
389 if (hr_qp->ibqp.qp_type != IB_QPT_XRC_TGT)
390 list_del(&hr_qp->sq_node);
392 if (hr_qp->ibqp.qp_type != IB_QPT_XRC_INI &&
393 hr_qp->ibqp.qp_type != IB_QPT_XRC_TGT)
394 list_del(&hr_qp->rq_node);
396 xa_lock_irqsave(xa, flags);
397 __xa_erase(xa, hr_qp->qpn);
398 xa_unlock_irqrestore(xa, flags);
401 static void free_qpc(struct hns_roce_dev *hr_dev, struct hns_roce_qp *hr_qp)
403 struct hns_roce_qp_table *qp_table = &hr_dev->qp_table;
405 /* In v1 engine, GSI QP context is saved in the RoCE hw's register */
406 if (hr_qp->ibqp.qp_type == IB_QPT_GSI &&
407 hr_dev->hw_rev == HNS_ROCE_HW_VER1)
410 if (hr_dev->caps.trrl_entry_sz)
411 hns_roce_table_put(hr_dev, &qp_table->trrl_table, hr_qp->qpn);
412 hns_roce_table_put(hr_dev, &qp_table->irrl_table, hr_qp->qpn);
415 static inline u8 get_qp_bankid(unsigned long qpn)
417 /* The lower 3 bits of QPN are used to hash to different banks */
418 return (u8)(qpn & GENMASK(2, 0));
421 static void free_qpn(struct hns_roce_dev *hr_dev, struct hns_roce_qp *hr_qp)
425 if (hr_qp->ibqp.qp_type == IB_QPT_GSI)
428 if (hr_qp->qpn < hr_dev->caps.reserved_qps)
431 bankid = get_qp_bankid(hr_qp->qpn);
433 ida_free(&hr_dev->qp_table.bank[bankid].ida, hr_qp->qpn >> 3);
435 mutex_lock(&hr_dev->qp_table.bank_mutex);
436 hr_dev->qp_table.bank[bankid].inuse--;
437 mutex_unlock(&hr_dev->qp_table.bank_mutex);
440 static u32 proc_rq_sge(struct hns_roce_dev *dev, struct hns_roce_qp *hr_qp,
443 u32 max_sge = dev->caps.max_rq_sg;
445 if (dev->pci_dev->revision >= PCI_REVISION_ID_HIP09)
448 /* Reserve SGEs only for HIP08 in kernel; The userspace driver will
449 * calculate number of max_sge with reserved SGEs when allocating wqe
450 * buf, so there is no need to do this again in kernel. But the number
451 * may exceed the capacity of SGEs recorded in the firmware, so the
452 * kernel driver should just adapt the value accordingly.
455 max_sge = roundup_pow_of_two(max_sge + 1);
457 hr_qp->rq.rsv_sge = 1;
462 static int set_rq_size(struct hns_roce_dev *hr_dev, struct ib_qp_cap *cap,
463 struct hns_roce_qp *hr_qp, int has_rq, bool user)
465 u32 max_sge = proc_rq_sge(hr_dev, hr_qp, user);
468 /* If srq exist, set zero for relative number of rq */
470 hr_qp->rq.wqe_cnt = 0;
471 hr_qp->rq.max_gs = 0;
472 hr_qp->rq_inl_buf.wqe_cnt = 0;
473 cap->max_recv_wr = 0;
474 cap->max_recv_sge = 0;
479 /* Check the validity of QP support capacity */
480 if (!cap->max_recv_wr || cap->max_recv_wr > hr_dev->caps.max_wqes ||
481 cap->max_recv_sge > max_sge) {
482 ibdev_err(&hr_dev->ib_dev,
483 "RQ config error, depth = %u, sge = %u\n",
484 cap->max_recv_wr, cap->max_recv_sge);
488 cnt = roundup_pow_of_two(max(cap->max_recv_wr, hr_dev->caps.min_wqes));
489 if (cnt > hr_dev->caps.max_wqes) {
490 ibdev_err(&hr_dev->ib_dev, "rq depth %u too large\n",
495 hr_qp->rq.max_gs = roundup_pow_of_two(max(1U, cap->max_recv_sge) +
498 if (hr_dev->caps.max_rq_sg <= HNS_ROCE_SGE_IN_WQE)
499 hr_qp->rq.wqe_shift = ilog2(hr_dev->caps.max_rq_desc_sz);
501 hr_qp->rq.wqe_shift = ilog2(hr_dev->caps.max_rq_desc_sz *
504 hr_qp->rq.wqe_cnt = cnt;
505 if (hr_dev->caps.flags & HNS_ROCE_CAP_FLAG_RQ_INLINE &&
506 hr_qp->ibqp.qp_type != IB_QPT_UD &&
507 hr_qp->ibqp.qp_type != IB_QPT_GSI)
508 hr_qp->rq_inl_buf.wqe_cnt = cnt;
510 hr_qp->rq_inl_buf.wqe_cnt = 0;
512 cap->max_recv_wr = cnt;
513 cap->max_recv_sge = hr_qp->rq.max_gs - hr_qp->rq.rsv_sge;
518 static u32 get_wqe_ext_sge_cnt(struct hns_roce_qp *qp)
520 /* GSI/UD QP only has extended sge */
521 if (qp->ibqp.qp_type == IB_QPT_GSI || qp->ibqp.qp_type == IB_QPT_UD)
522 return qp->sq.max_gs;
524 if (qp->sq.max_gs > HNS_ROCE_SGE_IN_WQE)
525 return qp->sq.max_gs - HNS_ROCE_SGE_IN_WQE;
530 static void set_ext_sge_param(struct hns_roce_dev *hr_dev, u32 sq_wqe_cnt,
531 struct hns_roce_qp *hr_qp, struct ib_qp_cap *cap)
536 hr_qp->sge.sge_shift = HNS_ROCE_SGE_SHIFT;
538 if (hr_dev->hw_rev == HNS_ROCE_HW_VER1) {
539 hr_qp->sq.max_gs = HNS_ROCE_SGE_IN_WQE;
543 hr_qp->sq.max_gs = max(1U, cap->max_send_sge);
545 wqe_sge_cnt = get_wqe_ext_sge_cnt(hr_qp);
547 /* If the number of extended sge is not zero, they MUST use the
548 * space of HNS_HW_PAGE_SIZE at least.
551 total_sge_cnt = roundup_pow_of_two(sq_wqe_cnt * wqe_sge_cnt);
552 hr_qp->sge.sge_cnt = max(total_sge_cnt,
553 (u32)HNS_HW_PAGE_SIZE / HNS_ROCE_SGE_SIZE);
557 static int check_sq_size_with_integrity(struct hns_roce_dev *hr_dev,
558 struct ib_qp_cap *cap,
559 struct hns_roce_ib_create_qp *ucmd)
561 u32 roundup_sq_stride = roundup_pow_of_two(hr_dev->caps.max_sq_desc_sz);
562 u8 max_sq_stride = ilog2(roundup_sq_stride);
564 /* Sanity check SQ size before proceeding */
565 if (ucmd->log_sq_stride > max_sq_stride ||
566 ucmd->log_sq_stride < HNS_ROCE_IB_MIN_SQ_STRIDE) {
567 ibdev_err(&hr_dev->ib_dev, "failed to check SQ stride size.\n");
571 if (cap->max_send_sge > hr_dev->caps.max_sq_sg) {
572 ibdev_err(&hr_dev->ib_dev, "failed to check SQ SGE size %u.\n",
580 static int set_user_sq_size(struct hns_roce_dev *hr_dev,
581 struct ib_qp_cap *cap, struct hns_roce_qp *hr_qp,
582 struct hns_roce_ib_create_qp *ucmd)
584 struct ib_device *ibdev = &hr_dev->ib_dev;
588 if (check_shl_overflow(1, ucmd->log_sq_bb_count, &cnt) ||
589 cnt > hr_dev->caps.max_wqes)
592 ret = check_sq_size_with_integrity(hr_dev, cap, ucmd);
594 ibdev_err(ibdev, "failed to check user SQ size, ret = %d.\n",
599 set_ext_sge_param(hr_dev, cnt, hr_qp, cap);
601 hr_qp->sq.wqe_shift = ucmd->log_sq_stride;
602 hr_qp->sq.wqe_cnt = cnt;
607 static int set_wqe_buf_attr(struct hns_roce_dev *hr_dev,
608 struct hns_roce_qp *hr_qp,
609 struct hns_roce_buf_attr *buf_attr)
614 hr_qp->buff_size = 0;
617 hr_qp->sq.offset = 0;
618 buf_size = to_hr_hem_entries_size(hr_qp->sq.wqe_cnt,
619 hr_qp->sq.wqe_shift);
620 if (buf_size > 0 && idx < ARRAY_SIZE(buf_attr->region)) {
621 buf_attr->region[idx].size = buf_size;
622 buf_attr->region[idx].hopnum = hr_dev->caps.wqe_sq_hop_num;
624 hr_qp->buff_size += buf_size;
627 /* extend SGE WQE in SQ */
628 hr_qp->sge.offset = hr_qp->buff_size;
629 buf_size = to_hr_hem_entries_size(hr_qp->sge.sge_cnt,
630 hr_qp->sge.sge_shift);
631 if (buf_size > 0 && idx < ARRAY_SIZE(buf_attr->region)) {
632 buf_attr->region[idx].size = buf_size;
633 buf_attr->region[idx].hopnum = hr_dev->caps.wqe_sge_hop_num;
635 hr_qp->buff_size += buf_size;
639 hr_qp->rq.offset = hr_qp->buff_size;
640 buf_size = to_hr_hem_entries_size(hr_qp->rq.wqe_cnt,
641 hr_qp->rq.wqe_shift);
642 if (buf_size > 0 && idx < ARRAY_SIZE(buf_attr->region)) {
643 buf_attr->region[idx].size = buf_size;
644 buf_attr->region[idx].hopnum = hr_dev->caps.wqe_rq_hop_num;
646 hr_qp->buff_size += buf_size;
649 if (hr_qp->buff_size < 1)
652 buf_attr->page_shift = HNS_HW_PAGE_SHIFT + hr_dev->caps.mtt_buf_pg_sz;
653 buf_attr->region_count = idx;
658 static int set_kernel_sq_size(struct hns_roce_dev *hr_dev,
659 struct ib_qp_cap *cap, struct hns_roce_qp *hr_qp)
661 struct ib_device *ibdev = &hr_dev->ib_dev;
664 if (!cap->max_send_wr || cap->max_send_wr > hr_dev->caps.max_wqes ||
665 cap->max_send_sge > hr_dev->caps.max_sq_sg) {
666 ibdev_err(ibdev, "failed to check SQ WR or SGE num.\n");
670 cnt = roundup_pow_of_two(max(cap->max_send_wr, hr_dev->caps.min_wqes));
671 if (cnt > hr_dev->caps.max_wqes) {
672 ibdev_err(ibdev, "failed to check WQE num, WQE num = %u.\n",
677 hr_qp->sq.wqe_shift = ilog2(hr_dev->caps.max_sq_desc_sz);
678 hr_qp->sq.wqe_cnt = cnt;
680 set_ext_sge_param(hr_dev, cnt, hr_qp, cap);
682 /* sync the parameters of kernel QP to user's configuration */
683 cap->max_send_wr = cnt;
684 cap->max_send_sge = hr_qp->sq.max_gs;
689 static int hns_roce_qp_has_sq(struct ib_qp_init_attr *attr)
691 if (attr->qp_type == IB_QPT_XRC_TGT || !attr->cap.max_send_wr)
697 static int hns_roce_qp_has_rq(struct ib_qp_init_attr *attr)
699 if (attr->qp_type == IB_QPT_XRC_INI ||
700 attr->qp_type == IB_QPT_XRC_TGT || attr->srq ||
701 !attr->cap.max_recv_wr)
707 static int alloc_rq_inline_buf(struct hns_roce_qp *hr_qp,
708 struct ib_qp_init_attr *init_attr)
710 u32 max_recv_sge = init_attr->cap.max_recv_sge;
711 u32 wqe_cnt = hr_qp->rq_inl_buf.wqe_cnt;
712 struct hns_roce_rinl_wqe *wqe_list;
715 /* allocate recv inline buf */
716 wqe_list = kcalloc(wqe_cnt, sizeof(struct hns_roce_rinl_wqe),
721 /* Allocate a continuous buffer for all inline sge we need */
722 wqe_list[0].sg_list = kcalloc(wqe_cnt, (max_recv_sge *
723 sizeof(struct hns_roce_rinl_sge)),
725 if (!wqe_list[0].sg_list)
728 /* Assign buffers of sg_list to each inline wqe */
729 for (i = 1; i < wqe_cnt; i++)
730 wqe_list[i].sg_list = &wqe_list[0].sg_list[i * max_recv_sge];
732 hr_qp->rq_inl_buf.wqe_list = wqe_list;
743 static void free_rq_inline_buf(struct hns_roce_qp *hr_qp)
745 if (hr_qp->rq_inl_buf.wqe_list)
746 kfree(hr_qp->rq_inl_buf.wqe_list[0].sg_list);
747 kfree(hr_qp->rq_inl_buf.wqe_list);
750 static int alloc_qp_buf(struct hns_roce_dev *hr_dev, struct hns_roce_qp *hr_qp,
751 struct ib_qp_init_attr *init_attr,
752 struct ib_udata *udata, unsigned long addr)
754 struct ib_device *ibdev = &hr_dev->ib_dev;
755 struct hns_roce_buf_attr buf_attr = {};
758 if (!udata && hr_qp->rq_inl_buf.wqe_cnt) {
759 ret = alloc_rq_inline_buf(hr_qp, init_attr);
762 "failed to alloc inline buf, ret = %d.\n",
767 hr_qp->rq_inl_buf.wqe_list = NULL;
770 ret = set_wqe_buf_attr(hr_dev, hr_qp, &buf_attr);
772 ibdev_err(ibdev, "failed to split WQE buf, ret = %d.\n", ret);
775 ret = hns_roce_mtr_create(hr_dev, &hr_qp->mtr, &buf_attr,
776 PAGE_SHIFT + hr_dev->caps.mtt_ba_pg_sz,
779 ibdev_err(ibdev, "failed to create WQE mtr, ret = %d.\n", ret);
785 free_rq_inline_buf(hr_qp);
790 static void free_qp_buf(struct hns_roce_dev *hr_dev, struct hns_roce_qp *hr_qp)
792 hns_roce_mtr_destroy(hr_dev, &hr_qp->mtr);
793 free_rq_inline_buf(hr_qp);
796 static inline bool user_qp_has_sdb(struct hns_roce_dev *hr_dev,
797 struct ib_qp_init_attr *init_attr,
798 struct ib_udata *udata,
799 struct hns_roce_ib_create_qp_resp *resp,
800 struct hns_roce_ib_create_qp *ucmd)
802 return ((hr_dev->caps.flags & HNS_ROCE_CAP_FLAG_QP_RECORD_DB) &&
803 udata->outlen >= offsetofend(typeof(*resp), cap_flags) &&
804 hns_roce_qp_has_sq(init_attr) &&
805 udata->inlen >= offsetofend(typeof(*ucmd), sdb_addr));
808 static inline bool user_qp_has_rdb(struct hns_roce_dev *hr_dev,
809 struct ib_qp_init_attr *init_attr,
810 struct ib_udata *udata,
811 struct hns_roce_ib_create_qp_resp *resp)
813 return ((hr_dev->caps.flags & HNS_ROCE_CAP_FLAG_QP_RECORD_DB) &&
814 udata->outlen >= offsetofend(typeof(*resp), cap_flags) &&
815 hns_roce_qp_has_rq(init_attr));
818 static inline bool kernel_qp_has_rdb(struct hns_roce_dev *hr_dev,
819 struct ib_qp_init_attr *init_attr)
821 return ((hr_dev->caps.flags & HNS_ROCE_CAP_FLAG_QP_RECORD_DB) &&
822 hns_roce_qp_has_rq(init_attr));
825 static int alloc_user_qp_db(struct hns_roce_dev *hr_dev,
826 struct hns_roce_qp *hr_qp,
827 struct ib_qp_init_attr *init_attr,
828 struct ib_udata *udata,
829 struct hns_roce_ib_create_qp *ucmd,
830 struct hns_roce_ib_create_qp_resp *resp)
832 struct hns_roce_ucontext *uctx = rdma_udata_to_drv_context(udata,
833 struct hns_roce_ucontext, ibucontext);
834 struct ib_device *ibdev = &hr_dev->ib_dev;
837 if (user_qp_has_sdb(hr_dev, init_attr, udata, resp, ucmd)) {
838 ret = hns_roce_db_map_user(uctx, ucmd->sdb_addr, &hr_qp->sdb);
841 "failed to map user SQ doorbell, ret = %d.\n",
845 hr_qp->en_flags |= HNS_ROCE_QP_CAP_SQ_RECORD_DB;
848 if (user_qp_has_rdb(hr_dev, init_attr, udata, resp)) {
849 ret = hns_roce_db_map_user(uctx, ucmd->db_addr, &hr_qp->rdb);
852 "failed to map user RQ doorbell, ret = %d.\n",
856 hr_qp->en_flags |= HNS_ROCE_QP_CAP_RQ_RECORD_DB;
862 if (hr_qp->en_flags & HNS_ROCE_QP_CAP_SQ_RECORD_DB)
863 hns_roce_db_unmap_user(uctx, &hr_qp->sdb);
868 static int alloc_kernel_qp_db(struct hns_roce_dev *hr_dev,
869 struct hns_roce_qp *hr_qp,
870 struct ib_qp_init_attr *init_attr)
872 struct ib_device *ibdev = &hr_dev->ib_dev;
875 if (hr_dev->pci_dev->revision >= PCI_REVISION_ID_HIP09)
876 hr_qp->sq.db_reg = hr_dev->mem_base +
877 HNS_ROCE_DWQE_SIZE * hr_qp->qpn;
879 hr_qp->sq.db_reg = hr_dev->reg_base + hr_dev->sdb_offset +
880 DB_REG_OFFSET * hr_dev->priv_uar.index;
882 hr_qp->rq.db_reg = hr_dev->reg_base + hr_dev->odb_offset +
883 DB_REG_OFFSET * hr_dev->priv_uar.index;
885 if (kernel_qp_has_rdb(hr_dev, init_attr)) {
886 ret = hns_roce_alloc_db(hr_dev, &hr_qp->rdb, 0);
889 "failed to alloc kernel RQ doorbell, ret = %d.\n",
893 *hr_qp->rdb.db_record = 0;
894 hr_qp->en_flags |= HNS_ROCE_QP_CAP_RQ_RECORD_DB;
900 static int alloc_qp_db(struct hns_roce_dev *hr_dev, struct hns_roce_qp *hr_qp,
901 struct ib_qp_init_attr *init_attr,
902 struct ib_udata *udata,
903 struct hns_roce_ib_create_qp *ucmd,
904 struct hns_roce_ib_create_qp_resp *resp)
908 if (hr_dev->caps.flags & HNS_ROCE_CAP_FLAG_SDI_MODE)
909 hr_qp->en_flags |= HNS_ROCE_QP_CAP_OWNER_DB;
912 ret = alloc_user_qp_db(hr_dev, hr_qp, init_attr, udata, ucmd,
917 ret = alloc_kernel_qp_db(hr_dev, hr_qp, init_attr);
925 static void free_qp_db(struct hns_roce_dev *hr_dev, struct hns_roce_qp *hr_qp,
926 struct ib_udata *udata)
928 struct hns_roce_ucontext *uctx = rdma_udata_to_drv_context(
929 udata, struct hns_roce_ucontext, ibucontext);
932 if (hr_qp->en_flags & HNS_ROCE_QP_CAP_RQ_RECORD_DB)
933 hns_roce_db_unmap_user(uctx, &hr_qp->rdb);
934 if (hr_qp->en_flags & HNS_ROCE_QP_CAP_SQ_RECORD_DB)
935 hns_roce_db_unmap_user(uctx, &hr_qp->sdb);
937 if (hr_qp->en_flags & HNS_ROCE_QP_CAP_RQ_RECORD_DB)
938 hns_roce_free_db(hr_dev, &hr_qp->rdb);
942 static int alloc_kernel_wrid(struct hns_roce_dev *hr_dev,
943 struct hns_roce_qp *hr_qp)
945 struct ib_device *ibdev = &hr_dev->ib_dev;
950 sq_wrid = kcalloc(hr_qp->sq.wqe_cnt, sizeof(u64), GFP_KERNEL);
951 if (ZERO_OR_NULL_PTR(sq_wrid)) {
952 ibdev_err(ibdev, "failed to alloc SQ wrid.\n");
956 if (hr_qp->rq.wqe_cnt) {
957 rq_wrid = kcalloc(hr_qp->rq.wqe_cnt, sizeof(u64), GFP_KERNEL);
958 if (ZERO_OR_NULL_PTR(rq_wrid)) {
959 ibdev_err(ibdev, "failed to alloc RQ wrid.\n");
965 hr_qp->sq.wrid = sq_wrid;
966 hr_qp->rq.wrid = rq_wrid;
974 static void free_kernel_wrid(struct hns_roce_qp *hr_qp)
976 kfree(hr_qp->rq.wrid);
977 kfree(hr_qp->sq.wrid);
980 static int set_qp_param(struct hns_roce_dev *hr_dev, struct hns_roce_qp *hr_qp,
981 struct ib_qp_init_attr *init_attr,
982 struct ib_udata *udata,
983 struct hns_roce_ib_create_qp *ucmd)
985 struct ib_device *ibdev = &hr_dev->ib_dev;
988 if (init_attr->cap.max_inline_data > hr_dev->caps.max_sq_inline)
989 init_attr->cap.max_inline_data = hr_dev->caps.max_sq_inline;
991 hr_qp->max_inline_data = init_attr->cap.max_inline_data;
993 if (init_attr->sq_sig_type == IB_SIGNAL_ALL_WR)
994 hr_qp->sq_signal_bits = IB_SIGNAL_ALL_WR;
996 hr_qp->sq_signal_bits = IB_SIGNAL_REQ_WR;
998 ret = set_rq_size(hr_dev, &init_attr->cap, hr_qp,
999 hns_roce_qp_has_rq(init_attr), !!udata);
1001 ibdev_err(ibdev, "failed to set user RQ size, ret = %d.\n",
1007 ret = ib_copy_from_udata(ucmd, udata,
1008 min(udata->inlen, sizeof(*ucmd)));
1011 "failed to copy QP ucmd, ret = %d\n", ret);
1015 ret = set_user_sq_size(hr_dev, &init_attr->cap, hr_qp, ucmd);
1018 "failed to set user SQ size, ret = %d.\n",
1021 ret = set_kernel_sq_size(hr_dev, &init_attr->cap, hr_qp);
1024 "failed to set kernel SQ size, ret = %d.\n",
1031 static int hns_roce_create_qp_common(struct hns_roce_dev *hr_dev,
1032 struct ib_pd *ib_pd,
1033 struct ib_qp_init_attr *init_attr,
1034 struct ib_udata *udata,
1035 struct hns_roce_qp *hr_qp)
1037 struct hns_roce_ib_create_qp_resp resp = {};
1038 struct ib_device *ibdev = &hr_dev->ib_dev;
1039 struct hns_roce_ib_create_qp ucmd;
1042 mutex_init(&hr_qp->mutex);
1043 spin_lock_init(&hr_qp->sq.lock);
1044 spin_lock_init(&hr_qp->rq.lock);
1046 hr_qp->state = IB_QPS_RESET;
1047 hr_qp->flush_flag = 0;
1049 if (init_attr->create_flags)
1052 ret = set_qp_param(hr_dev, hr_qp, init_attr, udata, &ucmd);
1054 ibdev_err(ibdev, "failed to set QP param, ret = %d.\n", ret);
1059 ret = alloc_kernel_wrid(hr_dev, hr_qp);
1061 ibdev_err(ibdev, "failed to alloc wrid, ret = %d.\n",
1067 ret = alloc_qp_buf(hr_dev, hr_qp, init_attr, udata, ucmd.buf_addr);
1069 ibdev_err(ibdev, "failed to alloc QP buffer, ret = %d.\n", ret);
1073 ret = alloc_qpn(hr_dev, hr_qp);
1075 ibdev_err(ibdev, "failed to alloc QPN, ret = %d.\n", ret);
1079 ret = alloc_qp_db(hr_dev, hr_qp, init_attr, udata, &ucmd, &resp);
1081 ibdev_err(ibdev, "failed to alloc QP doorbell, ret = %d.\n",
1086 ret = alloc_qpc(hr_dev, hr_qp);
1088 ibdev_err(ibdev, "failed to alloc QP context, ret = %d.\n",
1093 ret = hns_roce_qp_store(hr_dev, hr_qp, init_attr);
1095 ibdev_err(ibdev, "failed to store QP, ret = %d.\n", ret);
1100 resp.cap_flags = hr_qp->en_flags;
1101 ret = ib_copy_to_udata(udata, &resp,
1102 min(udata->outlen, sizeof(resp)));
1104 ibdev_err(ibdev, "copy qp resp failed!\n");
1109 if (hr_dev->caps.flags & HNS_ROCE_CAP_FLAG_QP_FLOW_CTRL) {
1110 ret = hr_dev->hw->qp_flow_control_init(hr_dev, hr_qp);
1115 hr_qp->ibqp.qp_num = hr_qp->qpn;
1116 hr_qp->event = hns_roce_ib_qp_event;
1117 refcount_set(&hr_qp->refcount, 1);
1118 init_completion(&hr_qp->free);
1123 hns_roce_qp_remove(hr_dev, hr_qp);
1125 free_qpc(hr_dev, hr_qp);
1127 free_qp_db(hr_dev, hr_qp, udata);
1129 free_qpn(hr_dev, hr_qp);
1131 free_qp_buf(hr_dev, hr_qp);
1133 free_kernel_wrid(hr_qp);
1137 void hns_roce_qp_destroy(struct hns_roce_dev *hr_dev, struct hns_roce_qp *hr_qp,
1138 struct ib_udata *udata)
1140 if (refcount_dec_and_test(&hr_qp->refcount))
1141 complete(&hr_qp->free);
1142 wait_for_completion(&hr_qp->free);
1144 free_qpc(hr_dev, hr_qp);
1145 free_qpn(hr_dev, hr_qp);
1146 free_qp_buf(hr_dev, hr_qp);
1147 free_kernel_wrid(hr_qp);
1148 free_qp_db(hr_dev, hr_qp, udata);
1151 static int check_qp_type(struct hns_roce_dev *hr_dev, enum ib_qp_type type,
1155 case IB_QPT_XRC_INI:
1156 case IB_QPT_XRC_TGT:
1157 if (!(hr_dev->caps.flags & HNS_ROCE_CAP_FLAG_XRC))
1161 if (hr_dev->pci_dev->revision <= PCI_REVISION_ID_HIP08 &&
1175 ibdev_err(&hr_dev->ib_dev, "not support QP type %d\n", type);
1180 int hns_roce_create_qp(struct ib_qp *qp, struct ib_qp_init_attr *init_attr,
1181 struct ib_udata *udata)
1183 struct ib_device *ibdev = qp->device;
1184 struct hns_roce_dev *hr_dev = to_hr_dev(ibdev);
1185 struct hns_roce_qp *hr_qp = to_hr_qp(qp);
1186 struct ib_pd *pd = qp->pd;
1189 ret = check_qp_type(hr_dev, init_attr->qp_type, !!udata);
1193 if (init_attr->qp_type == IB_QPT_XRC_TGT)
1194 hr_qp->xrcdn = to_hr_xrcd(init_attr->xrcd)->xrcdn;
1196 if (init_attr->qp_type == IB_QPT_GSI) {
1197 hr_qp->port = init_attr->port_num - 1;
1198 hr_qp->phy_port = hr_dev->iboe.phy_port[hr_qp->port];
1201 ret = hns_roce_create_qp_common(hr_dev, pd, init_attr, udata, hr_qp);
1203 ibdev_err(ibdev, "Create QP type 0x%x failed(%d)\n",
1204 init_attr->qp_type, ret);
1209 int to_hr_qp_type(int qp_type)
1213 return SERV_TYPE_RC;
1216 return SERV_TYPE_UD;
1217 case IB_QPT_XRC_INI:
1218 case IB_QPT_XRC_TGT:
1219 return SERV_TYPE_XRC;
1225 static int check_mtu_validate(struct hns_roce_dev *hr_dev,
1226 struct hns_roce_qp *hr_qp,
1227 struct ib_qp_attr *attr, int attr_mask)
1229 enum ib_mtu active_mtu;
1232 p = attr_mask & IB_QP_PORT ? (attr->port_num - 1) : hr_qp->port;
1233 active_mtu = iboe_get_mtu(hr_dev->iboe.netdevs[p]->mtu);
1235 if ((hr_dev->caps.max_mtu >= IB_MTU_2048 &&
1236 attr->path_mtu > hr_dev->caps.max_mtu) ||
1237 attr->path_mtu < IB_MTU_256 || attr->path_mtu > active_mtu) {
1238 ibdev_err(&hr_dev->ib_dev,
1239 "attr path_mtu(%d)invalid while modify qp",
1247 static int hns_roce_check_qp_attr(struct ib_qp *ibqp, struct ib_qp_attr *attr,
1250 struct hns_roce_dev *hr_dev = to_hr_dev(ibqp->device);
1251 struct hns_roce_qp *hr_qp = to_hr_qp(ibqp);
1254 if ((attr_mask & IB_QP_PORT) &&
1255 (attr->port_num == 0 || attr->port_num > hr_dev->caps.num_ports)) {
1256 ibdev_err(&hr_dev->ib_dev, "invalid attr, port_num = %u.\n",
1261 if (attr_mask & IB_QP_PKEY_INDEX) {
1262 p = attr_mask & IB_QP_PORT ? (attr->port_num - 1) : hr_qp->port;
1263 if (attr->pkey_index >= hr_dev->caps.pkey_table_len[p]) {
1264 ibdev_err(&hr_dev->ib_dev,
1265 "invalid attr, pkey_index = %u.\n",
1271 if (attr_mask & IB_QP_MAX_QP_RD_ATOMIC &&
1272 attr->max_rd_atomic > hr_dev->caps.max_qp_init_rdma) {
1273 ibdev_err(&hr_dev->ib_dev,
1274 "invalid attr, max_rd_atomic = %u.\n",
1275 attr->max_rd_atomic);
1279 if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC &&
1280 attr->max_dest_rd_atomic > hr_dev->caps.max_qp_dest_rdma) {
1281 ibdev_err(&hr_dev->ib_dev,
1282 "invalid attr, max_dest_rd_atomic = %u.\n",
1283 attr->max_dest_rd_atomic);
1287 if (attr_mask & IB_QP_PATH_MTU)
1288 return check_mtu_validate(hr_dev, hr_qp, attr, attr_mask);
1293 int hns_roce_modify_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
1294 int attr_mask, struct ib_udata *udata)
1296 struct hns_roce_dev *hr_dev = to_hr_dev(ibqp->device);
1297 struct hns_roce_qp *hr_qp = to_hr_qp(ibqp);
1298 enum ib_qp_state cur_state, new_state;
1301 mutex_lock(&hr_qp->mutex);
1303 if (attr_mask & IB_QP_CUR_STATE && attr->cur_qp_state != hr_qp->state)
1306 cur_state = hr_qp->state;
1307 new_state = attr_mask & IB_QP_STATE ? attr->qp_state : cur_state;
1309 if (ibqp->uobject &&
1310 (attr_mask & IB_QP_STATE) && new_state == IB_QPS_ERR) {
1311 if (hr_qp->en_flags & HNS_ROCE_QP_CAP_SQ_RECORD_DB) {
1312 hr_qp->sq.head = *(int *)(hr_qp->sdb.virt_addr);
1314 if (hr_qp->en_flags & HNS_ROCE_QP_CAP_RQ_RECORD_DB)
1315 hr_qp->rq.head = *(int *)(hr_qp->rdb.virt_addr);
1317 ibdev_warn(&hr_dev->ib_dev,
1318 "flush cqe is not supported in userspace!\n");
1323 if (!ib_modify_qp_is_ok(cur_state, new_state, ibqp->qp_type,
1325 ibdev_err(&hr_dev->ib_dev, "ib_modify_qp_is_ok failed\n");
1329 ret = hns_roce_check_qp_attr(ibqp, attr, attr_mask);
1333 if (cur_state == new_state && cur_state == IB_QPS_RESET)
1336 ret = hr_dev->hw->modify_qp(ibqp, attr, attr_mask, cur_state,
1340 mutex_unlock(&hr_qp->mutex);
1345 void hns_roce_lock_cqs(struct hns_roce_cq *send_cq, struct hns_roce_cq *recv_cq)
1346 __acquires(&send_cq->lock) __acquires(&recv_cq->lock)
1348 if (unlikely(send_cq == NULL && recv_cq == NULL)) {
1349 __acquire(&send_cq->lock);
1350 __acquire(&recv_cq->lock);
1351 } else if (unlikely(send_cq != NULL && recv_cq == NULL)) {
1352 spin_lock_irq(&send_cq->lock);
1353 __acquire(&recv_cq->lock);
1354 } else if (unlikely(send_cq == NULL && recv_cq != NULL)) {
1355 spin_lock_irq(&recv_cq->lock);
1356 __acquire(&send_cq->lock);
1357 } else if (send_cq == recv_cq) {
1358 spin_lock_irq(&send_cq->lock);
1359 __acquire(&recv_cq->lock);
1360 } else if (send_cq->cqn < recv_cq->cqn) {
1361 spin_lock_irq(&send_cq->lock);
1362 spin_lock_nested(&recv_cq->lock, SINGLE_DEPTH_NESTING);
1364 spin_lock_irq(&recv_cq->lock);
1365 spin_lock_nested(&send_cq->lock, SINGLE_DEPTH_NESTING);
1369 void hns_roce_unlock_cqs(struct hns_roce_cq *send_cq,
1370 struct hns_roce_cq *recv_cq) __releases(&send_cq->lock)
1371 __releases(&recv_cq->lock)
1373 if (unlikely(send_cq == NULL && recv_cq == NULL)) {
1374 __release(&recv_cq->lock);
1375 __release(&send_cq->lock);
1376 } else if (unlikely(send_cq != NULL && recv_cq == NULL)) {
1377 __release(&recv_cq->lock);
1378 spin_unlock(&send_cq->lock);
1379 } else if (unlikely(send_cq == NULL && recv_cq != NULL)) {
1380 __release(&send_cq->lock);
1381 spin_unlock(&recv_cq->lock);
1382 } else if (send_cq == recv_cq) {
1383 __release(&recv_cq->lock);
1384 spin_unlock_irq(&send_cq->lock);
1385 } else if (send_cq->cqn < recv_cq->cqn) {
1386 spin_unlock(&recv_cq->lock);
1387 spin_unlock_irq(&send_cq->lock);
1389 spin_unlock(&send_cq->lock);
1390 spin_unlock_irq(&recv_cq->lock);
1394 static inline void *get_wqe(struct hns_roce_qp *hr_qp, int offset)
1396 return hns_roce_buf_offset(hr_qp->mtr.kmem, offset);
1399 void *hns_roce_get_recv_wqe(struct hns_roce_qp *hr_qp, unsigned int n)
1401 return get_wqe(hr_qp, hr_qp->rq.offset + (n << hr_qp->rq.wqe_shift));
1404 void *hns_roce_get_send_wqe(struct hns_roce_qp *hr_qp, unsigned int n)
1406 return get_wqe(hr_qp, hr_qp->sq.offset + (n << hr_qp->sq.wqe_shift));
1409 void *hns_roce_get_extend_sge(struct hns_roce_qp *hr_qp, unsigned int n)
1411 return get_wqe(hr_qp, hr_qp->sge.offset + (n << hr_qp->sge.sge_shift));
1414 bool hns_roce_wq_overflow(struct hns_roce_wq *hr_wq, u32 nreq,
1415 struct ib_cq *ib_cq)
1417 struct hns_roce_cq *hr_cq;
1420 cur = hr_wq->head - hr_wq->tail;
1421 if (likely(cur + nreq < hr_wq->wqe_cnt))
1424 hr_cq = to_hr_cq(ib_cq);
1425 spin_lock(&hr_cq->lock);
1426 cur = hr_wq->head - hr_wq->tail;
1427 spin_unlock(&hr_cq->lock);
1429 return cur + nreq >= hr_wq->wqe_cnt;
1432 int hns_roce_init_qp_table(struct hns_roce_dev *hr_dev)
1434 struct hns_roce_qp_table *qp_table = &hr_dev->qp_table;
1435 unsigned int reserved_from_bot;
1438 qp_table->idx_table.spare_idx = kcalloc(hr_dev->caps.num_qps,
1439 sizeof(u32), GFP_KERNEL);
1440 if (!qp_table->idx_table.spare_idx)
1443 mutex_init(&qp_table->scc_mutex);
1444 mutex_init(&qp_table->bank_mutex);
1445 xa_init(&hr_dev->qp_table_xa);
1447 reserved_from_bot = hr_dev->caps.reserved_qps;
1449 for (i = 0; i < reserved_from_bot; i++) {
1450 hr_dev->qp_table.bank[get_qp_bankid(i)].inuse++;
1451 hr_dev->qp_table.bank[get_qp_bankid(i)].min++;
1454 for (i = 0; i < HNS_ROCE_QP_BANK_NUM; i++) {
1455 ida_init(&hr_dev->qp_table.bank[i].ida);
1456 hr_dev->qp_table.bank[i].max = hr_dev->caps.num_qps /
1457 HNS_ROCE_QP_BANK_NUM - 1;
1458 hr_dev->qp_table.bank[i].next = hr_dev->qp_table.bank[i].min;
1464 void hns_roce_cleanup_qp_table(struct hns_roce_dev *hr_dev)
1468 for (i = 0; i < HNS_ROCE_QP_BANK_NUM; i++)
1469 ida_destroy(&hr_dev->qp_table.bank[i].ida);
1470 kfree(hr_dev->qp_table.idx_table.spare_idx);