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 struct hns_roce_qp
*hns_roce_qp_lookup(struct hns_roce_dev
*hr_dev
,
45 struct device
*dev
= hr_dev
->dev
;
46 struct hns_roce_qp
*qp
;
49 xa_lock_irqsave(&hr_dev
->qp_table_xa
, flags
);
50 qp
= __hns_roce_qp_lookup(hr_dev
, qpn
);
52 refcount_inc(&qp
->refcount
);
53 xa_unlock_irqrestore(&hr_dev
->qp_table_xa
, flags
);
56 dev_warn(dev
, "async event for bogus QP %08x\n", qpn
);
61 static void flush_work_handle(struct work_struct
*work
)
63 struct hns_roce_work
*flush_work
= container_of(work
,
64 struct hns_roce_work
, work
);
65 struct hns_roce_qp
*hr_qp
= container_of(flush_work
,
66 struct hns_roce_qp
, flush_work
);
67 struct device
*dev
= flush_work
->hr_dev
->dev
;
68 struct ib_qp_attr attr
;
72 attr_mask
= IB_QP_STATE
;
73 attr
.qp_state
= IB_QPS_ERR
;
75 if (test_and_clear_bit(HNS_ROCE_FLUSH_FLAG
, &hr_qp
->flush_flag
)) {
76 ret
= hns_roce_modify_qp(&hr_qp
->ibqp
, &attr
, attr_mask
, NULL
);
78 dev_err(dev
, "modify QP to error state failed(%d) during CQE flush\n",
83 * make sure we signal QP destroy leg that flush QP was completed
84 * so that it can safely proceed ahead now and destroy QP
86 if (refcount_dec_and_test(&hr_qp
->refcount
))
87 complete(&hr_qp
->free
);
90 void init_flush_work(struct hns_roce_dev
*hr_dev
, struct hns_roce_qp
*hr_qp
)
92 struct hns_roce_work
*flush_work
= &hr_qp
->flush_work
;
95 spin_lock_irqsave(&hr_qp
->flush_lock
, flags
);
96 /* Exit directly after destroy_qp() */
97 if (test_bit(HNS_ROCE_STOP_FLUSH_FLAG
, &hr_qp
->flush_flag
)) {
98 spin_unlock_irqrestore(&hr_qp
->flush_lock
, flags
);
102 refcount_inc(&hr_qp
->refcount
);
103 queue_work(hr_dev
->irq_workq
, &flush_work
->work
);
104 spin_unlock_irqrestore(&hr_qp
->flush_lock
, flags
);
107 void flush_cqe(struct hns_roce_dev
*dev
, struct hns_roce_qp
*qp
)
110 * Hip08 hardware cannot flush the WQEs in SQ/RQ if the QP state
111 * gets into errored mode. Hence, as a workaround to this
112 * hardware limitation, driver needs to assist in flushing. But
113 * the flushing operation uses mailbox to convey the QP state to
114 * the hardware and which can sleep due to the mutex protection
115 * around the mailbox calls. Hence, use the deferred flush for
118 if (!test_and_set_bit(HNS_ROCE_FLUSH_FLAG
, &qp
->flush_flag
))
119 init_flush_work(dev
, qp
);
122 void hns_roce_qp_event(struct hns_roce_dev
*hr_dev
, u32 qpn
, int event_type
)
124 struct hns_roce_qp
*qp
;
126 qp
= hns_roce_qp_lookup(hr_dev
, qpn
);
130 qp
->event(qp
, (enum hns_roce_event
)event_type
);
132 if (refcount_dec_and_test(&qp
->refcount
))
136 void hns_roce_flush_cqe(struct hns_roce_dev
*hr_dev
, u32 qpn
)
138 struct hns_roce_qp
*qp
;
140 qp
= hns_roce_qp_lookup(hr_dev
, qpn
);
144 qp
->state
= IB_QPS_ERR
;
145 flush_cqe(hr_dev
, qp
);
147 if (refcount_dec_and_test(&qp
->refcount
))
151 static void hns_roce_ib_qp_event(struct hns_roce_qp
*hr_qp
,
152 enum hns_roce_event type
)
154 struct ib_qp
*ibqp
= &hr_qp
->ibqp
;
155 struct ib_event event
;
157 if (ibqp
->event_handler
) {
158 event
.device
= ibqp
->device
;
159 event
.element
.qp
= ibqp
;
161 case HNS_ROCE_EVENT_TYPE_PATH_MIG
:
162 event
.event
= IB_EVENT_PATH_MIG
;
164 case HNS_ROCE_EVENT_TYPE_COMM_EST
:
165 event
.event
= IB_EVENT_COMM_EST
;
167 case HNS_ROCE_EVENT_TYPE_SQ_DRAINED
:
168 event
.event
= IB_EVENT_SQ_DRAINED
;
170 case HNS_ROCE_EVENT_TYPE_SRQ_LAST_WQE_REACH
:
171 event
.event
= IB_EVENT_QP_LAST_WQE_REACHED
;
173 case HNS_ROCE_EVENT_TYPE_WQ_CATAS_ERROR
:
174 event
.event
= IB_EVENT_QP_FATAL
;
176 case HNS_ROCE_EVENT_TYPE_PATH_MIG_FAILED
:
177 event
.event
= IB_EVENT_PATH_MIG_ERR
;
179 case HNS_ROCE_EVENT_TYPE_INV_REQ_LOCAL_WQ_ERROR
:
180 event
.event
= IB_EVENT_QP_REQ_ERR
;
182 case HNS_ROCE_EVENT_TYPE_LOCAL_WQ_ACCESS_ERROR
:
183 case HNS_ROCE_EVENT_TYPE_XRCD_VIOLATION
:
184 case HNS_ROCE_EVENT_TYPE_INVALID_XRCETH
:
185 event
.event
= IB_EVENT_QP_ACCESS_ERR
;
188 dev_dbg(ibqp
->device
->dev
.parent
, "roce_ib: Unexpected event type %d on QP %06lx\n",
192 ibqp
->event_handler(&event
, ibqp
->qp_context
);
196 static u8
get_affinity_cq_bank(u8 qp_bank
)
198 return (qp_bank
>> 1) & CQ_BANKID_MASK
;
201 static u8
get_least_load_bankid_for_qp(struct ib_qp_init_attr
*init_attr
,
202 struct hns_roce_bank
*bank
)
204 #define INVALID_LOAD_QPNUM 0xFFFFFFFF
205 struct ib_cq
*scq
= init_attr
->send_cq
;
206 u32 least_load
= INVALID_LOAD_QPNUM
;
207 unsigned long cqn
= 0;
213 cqn
= to_hr_cq(scq
)->cqn
;
215 for (i
= 0; i
< HNS_ROCE_QP_BANK_NUM
; i
++) {
216 if (scq
&& (get_affinity_cq_bank(i
) != (cqn
& CQ_BANKID_MASK
)))
219 bankcnt
= bank
[i
].inuse
;
220 if (bankcnt
< least_load
) {
221 least_load
= bankcnt
;
229 static int alloc_qpn_with_bankid(struct hns_roce_bank
*bank
, u8 bankid
,
234 id
= ida_alloc_range(&bank
->ida
, bank
->next
, bank
->max
, GFP_KERNEL
);
236 id
= ida_alloc_range(&bank
->ida
, bank
->min
, bank
->max
,
242 /* the QPN should keep increasing until the max value is reached. */
243 bank
->next
= (id
+ 1) > bank
->max
? bank
->min
: id
+ 1;
245 /* the lower 3 bits is bankid */
246 *qpn
= (id
<< 3) | bankid
;
250 static int alloc_qpn(struct hns_roce_dev
*hr_dev
, struct hns_roce_qp
*hr_qp
,
251 struct ib_qp_init_attr
*init_attr
)
253 struct hns_roce_qp_table
*qp_table
= &hr_dev
->qp_table
;
254 unsigned long num
= 0;
258 if (hr_qp
->ibqp
.qp_type
== IB_QPT_GSI
) {
261 mutex_lock(&qp_table
->bank_mutex
);
262 bankid
= get_least_load_bankid_for_qp(init_attr
, qp_table
->bank
);
264 ret
= alloc_qpn_with_bankid(&qp_table
->bank
[bankid
], bankid
,
267 ibdev_err(&hr_dev
->ib_dev
,
268 "failed to alloc QPN, ret = %d\n", ret
);
269 mutex_unlock(&qp_table
->bank_mutex
);
273 qp_table
->bank
[bankid
].inuse
++;
274 mutex_unlock(&qp_table
->bank_mutex
);
282 static void add_qp_to_list(struct hns_roce_dev
*hr_dev
,
283 struct hns_roce_qp
*hr_qp
,
284 struct ib_cq
*send_cq
, struct ib_cq
*recv_cq
)
286 struct hns_roce_cq
*hr_send_cq
, *hr_recv_cq
;
289 hr_send_cq
= send_cq
? to_hr_cq(send_cq
) : NULL
;
290 hr_recv_cq
= recv_cq
? to_hr_cq(recv_cq
) : NULL
;
292 spin_lock_irqsave(&hr_dev
->qp_list_lock
, flags
);
293 hns_roce_lock_cqs(hr_send_cq
, hr_recv_cq
);
295 list_add_tail(&hr_qp
->node
, &hr_dev
->qp_list
);
297 list_add_tail(&hr_qp
->sq_node
, &hr_send_cq
->sq_list
);
299 list_add_tail(&hr_qp
->rq_node
, &hr_recv_cq
->rq_list
);
301 hns_roce_unlock_cqs(hr_send_cq
, hr_recv_cq
);
302 spin_unlock_irqrestore(&hr_dev
->qp_list_lock
, flags
);
305 static int hns_roce_qp_store(struct hns_roce_dev
*hr_dev
,
306 struct hns_roce_qp
*hr_qp
,
307 struct ib_qp_init_attr
*init_attr
)
309 struct xarray
*xa
= &hr_dev
->qp_table_xa
;
315 ret
= xa_err(xa_store_irq(xa
, hr_qp
->qpn
, hr_qp
, GFP_KERNEL
));
317 dev_err(hr_dev
->dev
, "failed to xa store for QPC\n");
319 /* add QP to device's QP list for softwc */
320 add_qp_to_list(hr_dev
, hr_qp
, init_attr
->send_cq
,
326 static int alloc_qpc(struct hns_roce_dev
*hr_dev
, struct hns_roce_qp
*hr_qp
)
328 struct hns_roce_qp_table
*qp_table
= &hr_dev
->qp_table
;
329 struct device
*dev
= hr_dev
->dev
;
335 /* Alloc memory for QPC */
336 ret
= hns_roce_table_get(hr_dev
, &qp_table
->qp_table
, hr_qp
->qpn
);
338 dev_err(dev
, "failed to get QPC table\n");
342 /* Alloc memory for IRRL */
343 ret
= hns_roce_table_get(hr_dev
, &qp_table
->irrl_table
, hr_qp
->qpn
);
345 dev_err(dev
, "failed to get IRRL table\n");
349 if (hr_dev
->caps
.trrl_entry_sz
) {
350 /* Alloc memory for TRRL */
351 ret
= hns_roce_table_get(hr_dev
, &qp_table
->trrl_table
,
354 dev_err(dev
, "failed to get TRRL table\n");
359 if (hr_dev
->caps
.flags
& HNS_ROCE_CAP_FLAG_QP_FLOW_CTRL
) {
360 /* Alloc memory for SCC CTX */
361 ret
= hns_roce_table_get(hr_dev
, &qp_table
->sccc_table
,
364 dev_err(dev
, "failed to get SCC CTX table\n");
372 if (hr_dev
->caps
.trrl_entry_sz
)
373 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 hns_roce_table_put(hr_dev
, &qp_table
->qp_table
, hr_qp
->qpn
);
385 static void qp_user_mmap_entry_remove(struct hns_roce_qp
*hr_qp
)
387 rdma_user_mmap_entry_remove(&hr_qp
->dwqe_mmap_entry
->rdma_entry
);
390 void hns_roce_qp_remove(struct hns_roce_dev
*hr_dev
, struct hns_roce_qp
*hr_qp
)
392 struct xarray
*xa
= &hr_dev
->qp_table_xa
;
395 list_del(&hr_qp
->node
);
397 if (hr_qp
->ibqp
.qp_type
!= IB_QPT_XRC_TGT
)
398 list_del(&hr_qp
->sq_node
);
400 if (hr_qp
->ibqp
.qp_type
!= IB_QPT_XRC_INI
&&
401 hr_qp
->ibqp
.qp_type
!= IB_QPT_XRC_TGT
)
402 list_del(&hr_qp
->rq_node
);
404 xa_lock_irqsave(xa
, flags
);
405 __xa_erase(xa
, hr_qp
->qpn
);
406 xa_unlock_irqrestore(xa
, flags
);
409 static void free_qpc(struct hns_roce_dev
*hr_dev
, struct hns_roce_qp
*hr_qp
)
411 struct hns_roce_qp_table
*qp_table
= &hr_dev
->qp_table
;
413 if (hr_dev
->caps
.trrl_entry_sz
)
414 hns_roce_table_put(hr_dev
, &qp_table
->trrl_table
, hr_qp
->qpn
);
415 hns_roce_table_put(hr_dev
, &qp_table
->irrl_table
, hr_qp
->qpn
);
418 static inline u8
get_qp_bankid(unsigned long qpn
)
420 /* The lower 3 bits of QPN are used to hash to different banks */
421 return (u8
)(qpn
& GENMASK(2, 0));
424 static void free_qpn(struct hns_roce_dev
*hr_dev
, struct hns_roce_qp
*hr_qp
)
428 if (hr_qp
->ibqp
.qp_type
== IB_QPT_GSI
)
431 if (hr_qp
->qpn
< hr_dev
->caps
.reserved_qps
)
434 bankid
= get_qp_bankid(hr_qp
->qpn
);
436 ida_free(&hr_dev
->qp_table
.bank
[bankid
].ida
,
437 hr_qp
->qpn
/ HNS_ROCE_QP_BANK_NUM
);
439 mutex_lock(&hr_dev
->qp_table
.bank_mutex
);
440 hr_dev
->qp_table
.bank
[bankid
].inuse
--;
441 mutex_unlock(&hr_dev
->qp_table
.bank_mutex
);
444 static u32
proc_rq_sge(struct hns_roce_dev
*dev
, struct hns_roce_qp
*hr_qp
,
447 u32 max_sge
= dev
->caps
.max_rq_sg
;
449 if (dev
->pci_dev
->revision
>= PCI_REVISION_ID_HIP09
)
452 /* Reserve SGEs only for HIP08 in kernel; The userspace driver will
453 * calculate number of max_sge with reserved SGEs when allocating wqe
454 * buf, so there is no need to do this again in kernel. But the number
455 * may exceed the capacity of SGEs recorded in the firmware, so the
456 * kernel driver should just adapt the value accordingly.
459 max_sge
= roundup_pow_of_two(max_sge
+ 1);
461 hr_qp
->rq
.rsv_sge
= 1;
466 static int set_rq_size(struct hns_roce_dev
*hr_dev
, struct ib_qp_cap
*cap
,
467 struct hns_roce_qp
*hr_qp
, int has_rq
, bool user
)
469 u32 max_sge
= proc_rq_sge(hr_dev
, hr_qp
, user
);
472 /* If srq exist, set zero for relative number of rq */
474 hr_qp
->rq
.wqe_cnt
= 0;
475 hr_qp
->rq
.max_gs
= 0;
476 cap
->max_recv_wr
= 0;
477 cap
->max_recv_sge
= 0;
482 /* Check the validity of QP support capacity */
483 if (!cap
->max_recv_wr
|| cap
->max_recv_wr
> hr_dev
->caps
.max_wqes
||
484 cap
->max_recv_sge
> max_sge
) {
485 ibdev_err(&hr_dev
->ib_dev
,
486 "RQ config error, depth = %u, sge = %u\n",
487 cap
->max_recv_wr
, cap
->max_recv_sge
);
491 cnt
= roundup_pow_of_two(max(cap
->max_recv_wr
, hr_dev
->caps
.min_wqes
));
492 if (cnt
> hr_dev
->caps
.max_wqes
) {
493 ibdev_err(&hr_dev
->ib_dev
, "rq depth %u too large\n",
498 hr_qp
->rq
.max_gs
= roundup_pow_of_two(max(1U, cap
->max_recv_sge
) +
501 hr_qp
->rq
.wqe_shift
= ilog2(hr_dev
->caps
.max_rq_desc_sz
*
504 hr_qp
->rq
.wqe_cnt
= cnt
;
506 cap
->max_recv_wr
= cnt
;
507 cap
->max_recv_sge
= hr_qp
->rq
.max_gs
- hr_qp
->rq
.rsv_sge
;
512 static u32
get_max_inline_data(struct hns_roce_dev
*hr_dev
,
513 struct ib_qp_cap
*cap
)
515 if (cap
->max_inline_data
) {
516 cap
->max_inline_data
= roundup_pow_of_two(cap
->max_inline_data
);
517 return min(cap
->max_inline_data
,
518 hr_dev
->caps
.max_sq_inline
);
524 static void update_inline_data(struct hns_roce_qp
*hr_qp
,
525 struct ib_qp_cap
*cap
)
527 u32 sge_num
= hr_qp
->sq
.ext_sge_cnt
;
529 if (hr_qp
->config
& HNS_ROCE_EXSGE_FLAGS
) {
530 if (!(hr_qp
->ibqp
.qp_type
== IB_QPT_GSI
||
531 hr_qp
->ibqp
.qp_type
== IB_QPT_UD
))
532 sge_num
= max((u32
)HNS_ROCE_SGE_IN_WQE
, sge_num
);
534 cap
->max_inline_data
= max(cap
->max_inline_data
,
535 sge_num
* HNS_ROCE_SGE_SIZE
);
538 hr_qp
->max_inline_data
= cap
->max_inline_data
;
541 static u32
get_sge_num_from_max_send_sge(bool is_ud_or_gsi
,
544 unsigned int std_sge_num
;
545 unsigned int min_sge
;
547 std_sge_num
= is_ud_or_gsi
? 0 : HNS_ROCE_SGE_IN_WQE
;
548 min_sge
= is_ud_or_gsi
? 1 : 0;
549 return max_send_sge
> std_sge_num
? (max_send_sge
- std_sge_num
) :
553 static unsigned int get_sge_num_from_max_inl_data(bool is_ud_or_gsi
,
556 unsigned int inline_sge
;
558 if (!max_inline_data
)
562 * if max_inline_data less than
563 * HNS_ROCE_SGE_IN_WQE * HNS_ROCE_SGE_SIZE,
564 * In addition to ud's mode, no need to extend sge.
566 inline_sge
= roundup_pow_of_two(max_inline_data
) / HNS_ROCE_SGE_SIZE
;
567 if (!is_ud_or_gsi
&& inline_sge
<= HNS_ROCE_SGE_IN_WQE
)
573 static void set_ext_sge_param(struct hns_roce_dev
*hr_dev
, u32 sq_wqe_cnt
,
574 struct hns_roce_qp
*hr_qp
, struct ib_qp_cap
*cap
)
576 bool is_ud_or_gsi
= (hr_qp
->ibqp
.qp_type
== IB_QPT_GSI
||
577 hr_qp
->ibqp
.qp_type
== IB_QPT_UD
);
578 unsigned int std_sge_num
;
579 u32 inline_ext_sge
= 0;
583 cap
->max_inline_data
= get_max_inline_data(hr_dev
, cap
);
585 hr_qp
->sge
.sge_shift
= HNS_ROCE_SGE_SHIFT
;
586 std_sge_num
= is_ud_or_gsi
? 0 : HNS_ROCE_SGE_IN_WQE
;
587 ext_wqe_sge_cnt
= get_sge_num_from_max_send_sge(is_ud_or_gsi
,
590 if (hr_qp
->config
& HNS_ROCE_EXSGE_FLAGS
) {
591 inline_ext_sge
= max(ext_wqe_sge_cnt
,
592 get_sge_num_from_max_inl_data(is_ud_or_gsi
,
593 cap
->max_inline_data
));
594 hr_qp
->sq
.ext_sge_cnt
= inline_ext_sge
?
595 roundup_pow_of_two(inline_ext_sge
) : 0;
597 hr_qp
->sq
.max_gs
= max(1U, (hr_qp
->sq
.ext_sge_cnt
+ std_sge_num
));
598 hr_qp
->sq
.max_gs
= min(hr_qp
->sq
.max_gs
, hr_dev
->caps
.max_sq_sg
);
600 ext_wqe_sge_cnt
= hr_qp
->sq
.ext_sge_cnt
;
602 hr_qp
->sq
.max_gs
= max(1U, cap
->max_send_sge
);
603 hr_qp
->sq
.max_gs
= min(hr_qp
->sq
.max_gs
, hr_dev
->caps
.max_sq_sg
);
604 hr_qp
->sq
.ext_sge_cnt
= hr_qp
->sq
.max_gs
;
607 /* If the number of extended sge is not zero, they MUST use the
608 * space of HNS_HW_PAGE_SIZE at least.
610 if (ext_wqe_sge_cnt
) {
611 total_sge_cnt
= roundup_pow_of_two(sq_wqe_cnt
* ext_wqe_sge_cnt
);
612 hr_qp
->sge
.sge_cnt
= max(total_sge_cnt
,
613 (u32
)HNS_HW_PAGE_SIZE
/ HNS_ROCE_SGE_SIZE
);
616 update_inline_data(hr_qp
, cap
);
619 static int check_sq_size_with_integrity(struct hns_roce_dev
*hr_dev
,
620 struct ib_qp_cap
*cap
,
621 struct hns_roce_ib_create_qp
*ucmd
)
623 u32 roundup_sq_stride
= roundup_pow_of_two(hr_dev
->caps
.max_sq_desc_sz
);
624 u8 max_sq_stride
= ilog2(roundup_sq_stride
);
626 /* Sanity check SQ size before proceeding */
627 if (ucmd
->log_sq_stride
> max_sq_stride
||
628 ucmd
->log_sq_stride
< HNS_ROCE_IB_MIN_SQ_STRIDE
) {
629 ibdev_err(&hr_dev
->ib_dev
, "failed to check SQ stride size.\n");
633 if (cap
->max_send_sge
> hr_dev
->caps
.max_sq_sg
) {
634 ibdev_err(&hr_dev
->ib_dev
, "failed to check SQ SGE size %u.\n",
642 static int set_user_sq_size(struct hns_roce_dev
*hr_dev
,
643 struct ib_qp_cap
*cap
, struct hns_roce_qp
*hr_qp
,
644 struct hns_roce_ib_create_qp
*ucmd
)
646 struct ib_device
*ibdev
= &hr_dev
->ib_dev
;
650 if (check_shl_overflow(1, ucmd
->log_sq_bb_count
, &cnt
) ||
651 cnt
> hr_dev
->caps
.max_wqes
)
654 ret
= check_sq_size_with_integrity(hr_dev
, cap
, ucmd
);
656 ibdev_err(ibdev
, "failed to check user SQ size, ret = %d.\n",
661 set_ext_sge_param(hr_dev
, cnt
, hr_qp
, cap
);
663 hr_qp
->sq
.wqe_shift
= ucmd
->log_sq_stride
;
664 hr_qp
->sq
.wqe_cnt
= cnt
;
665 cap
->max_send_sge
= hr_qp
->sq
.max_gs
;
670 static int set_wqe_buf_attr(struct hns_roce_dev
*hr_dev
,
671 struct hns_roce_qp
*hr_qp
,
672 struct hns_roce_buf_attr
*buf_attr
)
677 hr_qp
->buff_size
= 0;
680 hr_qp
->sq
.offset
= 0;
681 buf_size
= to_hr_hem_entries_size(hr_qp
->sq
.wqe_cnt
,
682 hr_qp
->sq
.wqe_shift
);
683 if (buf_size
> 0 && idx
< ARRAY_SIZE(buf_attr
->region
)) {
684 buf_attr
->region
[idx
].size
= buf_size
;
685 buf_attr
->region
[idx
].hopnum
= hr_dev
->caps
.wqe_sq_hop_num
;
687 hr_qp
->buff_size
+= buf_size
;
690 /* extend SGE WQE in SQ */
691 hr_qp
->sge
.offset
= hr_qp
->buff_size
;
692 buf_size
= to_hr_hem_entries_size(hr_qp
->sge
.sge_cnt
,
693 hr_qp
->sge
.sge_shift
);
694 if (buf_size
> 0 && idx
< ARRAY_SIZE(buf_attr
->region
)) {
695 buf_attr
->region
[idx
].size
= buf_size
;
696 buf_attr
->region
[idx
].hopnum
= hr_dev
->caps
.wqe_sge_hop_num
;
698 hr_qp
->buff_size
+= buf_size
;
702 hr_qp
->rq
.offset
= hr_qp
->buff_size
;
703 buf_size
= to_hr_hem_entries_size(hr_qp
->rq
.wqe_cnt
,
704 hr_qp
->rq
.wqe_shift
);
705 if (buf_size
> 0 && idx
< ARRAY_SIZE(buf_attr
->region
)) {
706 buf_attr
->region
[idx
].size
= buf_size
;
707 buf_attr
->region
[idx
].hopnum
= hr_dev
->caps
.wqe_rq_hop_num
;
709 hr_qp
->buff_size
+= buf_size
;
712 if (hr_qp
->buff_size
< 1)
715 buf_attr
->page_shift
= HNS_HW_PAGE_SHIFT
+ hr_dev
->caps
.mtt_buf_pg_sz
;
716 buf_attr
->region_count
= idx
;
721 static int set_kernel_sq_size(struct hns_roce_dev
*hr_dev
,
722 struct ib_qp_cap
*cap
, struct hns_roce_qp
*hr_qp
)
724 struct ib_device
*ibdev
= &hr_dev
->ib_dev
;
727 if (!cap
->max_send_wr
|| cap
->max_send_wr
> hr_dev
->caps
.max_wqes
||
728 cap
->max_send_sge
> hr_dev
->caps
.max_sq_sg
) {
729 ibdev_err(ibdev
, "failed to check SQ WR or SGE num.\n");
733 cnt
= roundup_pow_of_two(max(cap
->max_send_wr
, hr_dev
->caps
.min_wqes
));
734 if (cnt
> hr_dev
->caps
.max_wqes
) {
735 ibdev_err(ibdev
, "failed to check WQE num, WQE num = %u.\n",
740 hr_qp
->sq
.wqe_shift
= ilog2(hr_dev
->caps
.max_sq_desc_sz
);
741 hr_qp
->sq
.wqe_cnt
= cnt
;
743 set_ext_sge_param(hr_dev
, cnt
, hr_qp
, cap
);
745 /* sync the parameters of kernel QP to user's configuration */
746 cap
->max_send_wr
= cnt
;
747 cap
->max_send_sge
= hr_qp
->sq
.max_gs
;
752 static int hns_roce_qp_has_sq(struct ib_qp_init_attr
*attr
)
754 if (attr
->qp_type
== IB_QPT_XRC_TGT
|| !attr
->cap
.max_send_wr
)
760 static int hns_roce_qp_has_rq(struct ib_qp_init_attr
*attr
)
762 if (attr
->qp_type
== IB_QPT_XRC_INI
||
763 attr
->qp_type
== IB_QPT_XRC_TGT
|| attr
->srq
||
764 !attr
->cap
.max_recv_wr
)
770 static int alloc_qp_buf(struct hns_roce_dev
*hr_dev
, struct hns_roce_qp
*hr_qp
,
771 struct ib_qp_init_attr
*init_attr
,
772 struct ib_udata
*udata
, unsigned long addr
)
774 struct ib_device
*ibdev
= &hr_dev
->ib_dev
;
775 struct hns_roce_buf_attr buf_attr
= {};
778 ret
= set_wqe_buf_attr(hr_dev
, hr_qp
, &buf_attr
);
780 ibdev_err(ibdev
, "failed to split WQE buf, ret = %d.\n", ret
);
783 ret
= hns_roce_mtr_create(hr_dev
, &hr_qp
->mtr
, &buf_attr
,
784 PAGE_SHIFT
+ hr_dev
->caps
.mtt_ba_pg_sz
,
787 ibdev_err(ibdev
, "failed to create WQE mtr, ret = %d.\n", ret
);
791 if (hr_dev
->caps
.flags
& HNS_ROCE_CAP_FLAG_DIRECT_WQE
)
792 hr_qp
->en_flags
|= HNS_ROCE_QP_CAP_DIRECT_WQE
;
801 static void free_qp_buf(struct hns_roce_dev
*hr_dev
, struct hns_roce_qp
*hr_qp
)
803 hns_roce_mtr_destroy(hr_dev
, &hr_qp
->mtr
);
806 static inline bool user_qp_has_sdb(struct hns_roce_dev
*hr_dev
,
807 struct ib_qp_init_attr
*init_attr
,
808 struct ib_udata
*udata
,
809 struct hns_roce_ib_create_qp_resp
*resp
,
810 struct hns_roce_ib_create_qp
*ucmd
)
812 return ((hr_dev
->caps
.flags
& HNS_ROCE_CAP_FLAG_QP_RECORD_DB
) &&
813 udata
->outlen
>= offsetofend(typeof(*resp
), cap_flags
) &&
814 hns_roce_qp_has_sq(init_attr
) &&
815 udata
->inlen
>= offsetofend(typeof(*ucmd
), sdb_addr
));
818 static inline bool user_qp_has_rdb(struct hns_roce_dev
*hr_dev
,
819 struct ib_qp_init_attr
*init_attr
,
820 struct ib_udata
*udata
,
821 struct hns_roce_ib_create_qp_resp
*resp
)
823 return ((hr_dev
->caps
.flags
& HNS_ROCE_CAP_FLAG_QP_RECORD_DB
) &&
824 udata
->outlen
>= offsetofend(typeof(*resp
), cap_flags
) &&
825 hns_roce_qp_has_rq(init_attr
));
828 static inline bool kernel_qp_has_rdb(struct hns_roce_dev
*hr_dev
,
829 struct ib_qp_init_attr
*init_attr
)
831 return ((hr_dev
->caps
.flags
& HNS_ROCE_CAP_FLAG_QP_RECORD_DB
) &&
832 hns_roce_qp_has_rq(init_attr
));
835 static int qp_mmap_entry(struct hns_roce_qp
*hr_qp
,
836 struct hns_roce_dev
*hr_dev
,
837 struct ib_udata
*udata
,
838 struct hns_roce_ib_create_qp_resp
*resp
)
840 struct hns_roce_ucontext
*uctx
=
841 rdma_udata_to_drv_context(udata
,
842 struct hns_roce_ucontext
, ibucontext
);
843 struct rdma_user_mmap_entry
*rdma_entry
;
846 address
= hr_dev
->dwqe_page
+ hr_qp
->qpn
* HNS_ROCE_DWQE_SIZE
;
848 hr_qp
->dwqe_mmap_entry
=
849 hns_roce_user_mmap_entry_insert(&uctx
->ibucontext
, address
,
851 HNS_ROCE_MMAP_TYPE_DWQE
);
853 if (!hr_qp
->dwqe_mmap_entry
) {
854 ibdev_err(&hr_dev
->ib_dev
, "failed to get dwqe mmap entry.\n");
858 rdma_entry
= &hr_qp
->dwqe_mmap_entry
->rdma_entry
;
859 resp
->dwqe_mmap_key
= rdma_user_mmap_get_offset(rdma_entry
);
864 static int alloc_user_qp_db(struct hns_roce_dev
*hr_dev
,
865 struct hns_roce_qp
*hr_qp
,
866 struct ib_qp_init_attr
*init_attr
,
867 struct ib_udata
*udata
,
868 struct hns_roce_ib_create_qp
*ucmd
,
869 struct hns_roce_ib_create_qp_resp
*resp
)
871 struct hns_roce_ucontext
*uctx
= rdma_udata_to_drv_context(udata
,
872 struct hns_roce_ucontext
, ibucontext
);
873 struct ib_device
*ibdev
= &hr_dev
->ib_dev
;
876 if (user_qp_has_sdb(hr_dev
, init_attr
, udata
, resp
, ucmd
)) {
877 ret
= hns_roce_db_map_user(uctx
, ucmd
->sdb_addr
, &hr_qp
->sdb
);
880 "failed to map user SQ doorbell, ret = %d.\n",
884 hr_qp
->en_flags
|= HNS_ROCE_QP_CAP_SQ_RECORD_DB
;
887 if (user_qp_has_rdb(hr_dev
, init_attr
, udata
, resp
)) {
888 ret
= hns_roce_db_map_user(uctx
, ucmd
->db_addr
, &hr_qp
->rdb
);
891 "failed to map user RQ doorbell, ret = %d.\n",
895 hr_qp
->en_flags
|= HNS_ROCE_QP_CAP_RQ_RECORD_DB
;
901 if (hr_qp
->en_flags
& HNS_ROCE_QP_CAP_SQ_RECORD_DB
)
902 hns_roce_db_unmap_user(uctx
, &hr_qp
->sdb
);
907 static int alloc_kernel_qp_db(struct hns_roce_dev
*hr_dev
,
908 struct hns_roce_qp
*hr_qp
,
909 struct ib_qp_init_attr
*init_attr
)
911 struct ib_device
*ibdev
= &hr_dev
->ib_dev
;
914 if (hr_dev
->pci_dev
->revision
>= PCI_REVISION_ID_HIP09
)
915 hr_qp
->sq
.db_reg
= hr_dev
->mem_base
+
916 HNS_ROCE_DWQE_SIZE
* hr_qp
->qpn
;
918 hr_qp
->sq
.db_reg
= hr_dev
->reg_base
+ hr_dev
->sdb_offset
+
919 DB_REG_OFFSET
* hr_dev
->priv_uar
.index
;
921 hr_qp
->rq
.db_reg
= hr_dev
->reg_base
+ hr_dev
->odb_offset
+
922 DB_REG_OFFSET
* hr_dev
->priv_uar
.index
;
924 if (kernel_qp_has_rdb(hr_dev
, init_attr
)) {
925 ret
= hns_roce_alloc_db(hr_dev
, &hr_qp
->rdb
, 0);
928 "failed to alloc kernel RQ doorbell, ret = %d.\n",
932 *hr_qp
->rdb
.db_record
= 0;
933 hr_qp
->en_flags
|= HNS_ROCE_QP_CAP_RQ_RECORD_DB
;
939 static int alloc_qp_db(struct hns_roce_dev
*hr_dev
, struct hns_roce_qp
*hr_qp
,
940 struct ib_qp_init_attr
*init_attr
,
941 struct ib_udata
*udata
,
942 struct hns_roce_ib_create_qp
*ucmd
,
943 struct hns_roce_ib_create_qp_resp
*resp
)
947 if (hr_dev
->caps
.flags
& HNS_ROCE_CAP_FLAG_SDI_MODE
)
948 hr_qp
->en_flags
|= HNS_ROCE_QP_CAP_OWNER_DB
;
951 if (hr_qp
->en_flags
& HNS_ROCE_QP_CAP_DIRECT_WQE
) {
952 ret
= qp_mmap_entry(hr_qp
, hr_dev
, udata
, resp
);
957 ret
= alloc_user_qp_db(hr_dev
, hr_qp
, init_attr
, udata
, ucmd
,
962 ret
= alloc_kernel_qp_db(hr_dev
, hr_qp
, init_attr
);
970 if (hr_qp
->en_flags
& HNS_ROCE_QP_CAP_DIRECT_WQE
)
971 qp_user_mmap_entry_remove(hr_qp
);
976 static void free_qp_db(struct hns_roce_dev
*hr_dev
, struct hns_roce_qp
*hr_qp
,
977 struct ib_udata
*udata
)
979 struct hns_roce_ucontext
*uctx
= rdma_udata_to_drv_context(
980 udata
, struct hns_roce_ucontext
, ibucontext
);
983 if (hr_qp
->en_flags
& HNS_ROCE_QP_CAP_RQ_RECORD_DB
)
984 hns_roce_db_unmap_user(uctx
, &hr_qp
->rdb
);
985 if (hr_qp
->en_flags
& HNS_ROCE_QP_CAP_SQ_RECORD_DB
)
986 hns_roce_db_unmap_user(uctx
, &hr_qp
->sdb
);
987 if (hr_qp
->en_flags
& HNS_ROCE_QP_CAP_DIRECT_WQE
)
988 qp_user_mmap_entry_remove(hr_qp
);
990 if (hr_qp
->en_flags
& HNS_ROCE_QP_CAP_RQ_RECORD_DB
)
991 hns_roce_free_db(hr_dev
, &hr_qp
->rdb
);
995 static int alloc_kernel_wrid(struct hns_roce_dev
*hr_dev
,
996 struct hns_roce_qp
*hr_qp
)
998 struct ib_device
*ibdev
= &hr_dev
->ib_dev
;
1000 u64
*rq_wrid
= NULL
;
1003 sq_wrid
= kcalloc(hr_qp
->sq
.wqe_cnt
, sizeof(u64
), GFP_KERNEL
);
1004 if (ZERO_OR_NULL_PTR(sq_wrid
)) {
1005 ibdev_err(ibdev
, "failed to alloc SQ wrid.\n");
1009 if (hr_qp
->rq
.wqe_cnt
) {
1010 rq_wrid
= kcalloc(hr_qp
->rq
.wqe_cnt
, sizeof(u64
), GFP_KERNEL
);
1011 if (ZERO_OR_NULL_PTR(rq_wrid
)) {
1012 ibdev_err(ibdev
, "failed to alloc RQ wrid.\n");
1018 hr_qp
->sq
.wrid
= sq_wrid
;
1019 hr_qp
->rq
.wrid
= rq_wrid
;
1027 static void free_kernel_wrid(struct hns_roce_qp
*hr_qp
)
1029 kfree(hr_qp
->rq
.wrid
);
1030 kfree(hr_qp
->sq
.wrid
);
1033 static void default_congest_type(struct hns_roce_dev
*hr_dev
,
1034 struct hns_roce_qp
*hr_qp
)
1036 if (hr_qp
->ibqp
.qp_type
== IB_QPT_UD
||
1037 hr_qp
->ibqp
.qp_type
== IB_QPT_GSI
)
1038 hr_qp
->cong_type
= CONG_TYPE_DCQCN
;
1040 hr_qp
->cong_type
= hr_dev
->caps
.default_cong_type
;
1043 static int set_congest_type(struct hns_roce_qp
*hr_qp
,
1044 struct hns_roce_ib_create_qp
*ucmd
)
1046 struct hns_roce_dev
*hr_dev
= to_hr_dev(hr_qp
->ibqp
.device
);
1048 switch (ucmd
->cong_type_flags
) {
1049 case HNS_ROCE_CREATE_QP_FLAGS_DCQCN
:
1050 hr_qp
->cong_type
= CONG_TYPE_DCQCN
;
1052 case HNS_ROCE_CREATE_QP_FLAGS_LDCP
:
1053 hr_qp
->cong_type
= CONG_TYPE_LDCP
;
1055 case HNS_ROCE_CREATE_QP_FLAGS_HC3
:
1056 hr_qp
->cong_type
= CONG_TYPE_HC3
;
1058 case HNS_ROCE_CREATE_QP_FLAGS_DIP
:
1059 hr_qp
->cong_type
= CONG_TYPE_DIP
;
1065 if (!test_bit(hr_qp
->cong_type
, (unsigned long *)&hr_dev
->caps
.cong_cap
))
1068 if (hr_qp
->ibqp
.qp_type
== IB_QPT_UD
&&
1069 hr_qp
->cong_type
!= CONG_TYPE_DCQCN
)
1075 static int set_congest_param(struct hns_roce_dev
*hr_dev
,
1076 struct hns_roce_qp
*hr_qp
,
1077 struct hns_roce_ib_create_qp
*ucmd
)
1079 if (ucmd
->comp_mask
& HNS_ROCE_CREATE_QP_MASK_CONGEST_TYPE
)
1080 return set_congest_type(hr_qp
, ucmd
);
1082 default_congest_type(hr_dev
, hr_qp
);
1087 static int set_qp_param(struct hns_roce_dev
*hr_dev
, struct hns_roce_qp
*hr_qp
,
1088 struct ib_qp_init_attr
*init_attr
,
1089 struct ib_udata
*udata
,
1090 struct hns_roce_ib_create_qp
*ucmd
)
1092 struct ib_device
*ibdev
= &hr_dev
->ib_dev
;
1093 struct hns_roce_ucontext
*uctx
;
1096 if (init_attr
->sq_sig_type
== IB_SIGNAL_ALL_WR
)
1097 hr_qp
->sq_signal_bits
= IB_SIGNAL_ALL_WR
;
1099 hr_qp
->sq_signal_bits
= IB_SIGNAL_REQ_WR
;
1101 ret
= set_rq_size(hr_dev
, &init_attr
->cap
, hr_qp
,
1102 hns_roce_qp_has_rq(init_attr
), !!udata
);
1104 ibdev_err(ibdev
, "failed to set user RQ size, ret = %d.\n",
1110 ret
= ib_copy_from_udata(ucmd
, udata
,
1111 min(udata
->inlen
, sizeof(*ucmd
)));
1114 "failed to copy QP ucmd, ret = %d\n", ret
);
1118 uctx
= rdma_udata_to_drv_context(udata
, struct hns_roce_ucontext
,
1120 hr_qp
->config
= uctx
->config
;
1121 ret
= set_user_sq_size(hr_dev
, &init_attr
->cap
, hr_qp
, ucmd
);
1124 "failed to set user SQ size, ret = %d.\n",
1127 ret
= set_congest_param(hr_dev
, hr_qp
, ucmd
);
1131 if (hr_dev
->pci_dev
->revision
>= PCI_REVISION_ID_HIP09
)
1132 hr_qp
->config
= HNS_ROCE_EXSGE_FLAGS
;
1133 ret
= set_kernel_sq_size(hr_dev
, &init_attr
->cap
, hr_qp
);
1136 "failed to set kernel SQ size, ret = %d.\n",
1139 default_congest_type(hr_dev
, hr_qp
);
1145 static int hns_roce_create_qp_common(struct hns_roce_dev
*hr_dev
,
1146 struct ib_qp_init_attr
*init_attr
,
1147 struct ib_udata
*udata
,
1148 struct hns_roce_qp
*hr_qp
)
1150 struct hns_roce_work
*flush_work
= &hr_qp
->flush_work
;
1151 struct hns_roce_ib_create_qp_resp resp
= {};
1152 struct ib_device
*ibdev
= &hr_dev
->ib_dev
;
1153 struct hns_roce_ib_create_qp ucmd
= {};
1156 mutex_init(&hr_qp
->mutex
);
1157 spin_lock_init(&hr_qp
->sq
.lock
);
1158 spin_lock_init(&hr_qp
->rq
.lock
);
1159 spin_lock_init(&hr_qp
->flush_lock
);
1161 hr_qp
->state
= IB_QPS_RESET
;
1162 hr_qp
->flush_flag
= 0;
1163 flush_work
->hr_dev
= hr_dev
;
1164 INIT_WORK(&flush_work
->work
, flush_work_handle
);
1166 if (init_attr
->create_flags
)
1169 ret
= set_qp_param(hr_dev
, hr_qp
, init_attr
, udata
, &ucmd
);
1171 ibdev_err(ibdev
, "failed to set QP param, ret = %d.\n", ret
);
1176 ret
= alloc_kernel_wrid(hr_dev
, hr_qp
);
1178 ibdev_err(ibdev
, "failed to alloc wrid, ret = %d.\n",
1184 ret
= alloc_qp_buf(hr_dev
, hr_qp
, init_attr
, udata
, ucmd
.buf_addr
);
1186 ibdev_err(ibdev
, "failed to alloc QP buffer, ret = %d.\n", ret
);
1190 ret
= alloc_qpn(hr_dev
, hr_qp
, init_attr
);
1192 ibdev_err(ibdev
, "failed to alloc QPN, ret = %d.\n", ret
);
1196 ret
= alloc_qp_db(hr_dev
, hr_qp
, init_attr
, udata
, &ucmd
, &resp
);
1198 ibdev_err(ibdev
, "failed to alloc QP doorbell, ret = %d.\n",
1203 ret
= alloc_qpc(hr_dev
, hr_qp
);
1205 ibdev_err(ibdev
, "failed to alloc QP context, ret = %d.\n",
1210 ret
= hns_roce_qp_store(hr_dev
, hr_qp
, init_attr
);
1212 ibdev_err(ibdev
, "failed to store QP, ret = %d.\n", ret
);
1217 resp
.cap_flags
= hr_qp
->en_flags
;
1218 ret
= ib_copy_to_udata(udata
, &resp
,
1219 min(udata
->outlen
, sizeof(resp
)));
1221 ibdev_err(ibdev
, "copy qp resp failed!\n");
1226 if (hr_dev
->caps
.flags
& HNS_ROCE_CAP_FLAG_QP_FLOW_CTRL
) {
1227 ret
= hr_dev
->hw
->qp_flow_control_init(hr_dev
, hr_qp
);
1232 hr_qp
->ibqp
.qp_num
= hr_qp
->qpn
;
1233 hr_qp
->event
= hns_roce_ib_qp_event
;
1234 refcount_set(&hr_qp
->refcount
, 1);
1235 init_completion(&hr_qp
->free
);
1240 hns_roce_qp_remove(hr_dev
, hr_qp
);
1242 free_qpc(hr_dev
, hr_qp
);
1244 free_qp_db(hr_dev
, hr_qp
, udata
);
1246 free_qpn(hr_dev
, hr_qp
);
1248 free_qp_buf(hr_dev
, hr_qp
);
1250 free_kernel_wrid(hr_qp
);
1252 mutex_destroy(&hr_qp
->mutex
);
1256 void hns_roce_qp_destroy(struct hns_roce_dev
*hr_dev
, struct hns_roce_qp
*hr_qp
,
1257 struct ib_udata
*udata
)
1259 if (refcount_dec_and_test(&hr_qp
->refcount
))
1260 complete(&hr_qp
->free
);
1261 wait_for_completion(&hr_qp
->free
);
1263 free_qpc(hr_dev
, hr_qp
);
1264 free_qpn(hr_dev
, hr_qp
);
1265 free_qp_buf(hr_dev
, hr_qp
);
1266 free_kernel_wrid(hr_qp
);
1267 free_qp_db(hr_dev
, hr_qp
, udata
);
1268 mutex_destroy(&hr_qp
->mutex
);
1271 static int check_qp_type(struct hns_roce_dev
*hr_dev
, enum ib_qp_type type
,
1275 case IB_QPT_XRC_INI
:
1276 case IB_QPT_XRC_TGT
:
1277 if (!(hr_dev
->caps
.flags
& HNS_ROCE_CAP_FLAG_XRC
))
1281 if (hr_dev
->pci_dev
->revision
== PCI_REVISION_ID_HIP08
&&
1295 ibdev_err(&hr_dev
->ib_dev
, "not support QP type %d\n", type
);
1300 int hns_roce_create_qp(struct ib_qp
*qp
, struct ib_qp_init_attr
*init_attr
,
1301 struct ib_udata
*udata
)
1303 struct ib_device
*ibdev
= qp
->device
;
1304 struct hns_roce_dev
*hr_dev
= to_hr_dev(ibdev
);
1305 struct hns_roce_qp
*hr_qp
= to_hr_qp(qp
);
1308 ret
= check_qp_type(hr_dev
, init_attr
->qp_type
, !!udata
);
1312 if (init_attr
->qp_type
== IB_QPT_XRC_TGT
)
1313 hr_qp
->xrcdn
= to_hr_xrcd(init_attr
->xrcd
)->xrcdn
;
1315 if (init_attr
->qp_type
== IB_QPT_GSI
) {
1316 hr_qp
->port
= init_attr
->port_num
- 1;
1317 hr_qp
->phy_port
= hr_dev
->iboe
.phy_port
[hr_qp
->port
];
1320 ret
= hns_roce_create_qp_common(hr_dev
, init_attr
, udata
, hr_qp
);
1322 ibdev_err(ibdev
, "create QP type 0x%x failed(%d)\n",
1323 init_attr
->qp_type
, ret
);
1327 atomic64_inc(&hr_dev
->dfx_cnt
[HNS_ROCE_DFX_QP_CREATE_ERR_CNT
]);
1332 int to_hr_qp_type(int qp_type
)
1336 return SERV_TYPE_RC
;
1339 return SERV_TYPE_UD
;
1340 case IB_QPT_XRC_INI
:
1341 case IB_QPT_XRC_TGT
:
1342 return SERV_TYPE_XRC
;
1348 static int check_mtu_validate(struct hns_roce_dev
*hr_dev
,
1349 struct hns_roce_qp
*hr_qp
,
1350 struct ib_qp_attr
*attr
, int attr_mask
)
1352 enum ib_mtu active_mtu
;
1355 p
= attr_mask
& IB_QP_PORT
? (attr
->port_num
- 1) : hr_qp
->port
;
1356 active_mtu
= iboe_get_mtu(hr_dev
->iboe
.netdevs
[p
]->mtu
);
1358 if ((hr_dev
->caps
.max_mtu
>= IB_MTU_2048
&&
1359 attr
->path_mtu
> hr_dev
->caps
.max_mtu
) ||
1360 attr
->path_mtu
< IB_MTU_256
|| attr
->path_mtu
> active_mtu
) {
1361 ibdev_err(&hr_dev
->ib_dev
,
1362 "attr path_mtu(%d)invalid while modify qp",
1370 static int hns_roce_check_qp_attr(struct ib_qp
*ibqp
, struct ib_qp_attr
*attr
,
1373 struct hns_roce_dev
*hr_dev
= to_hr_dev(ibqp
->device
);
1374 struct hns_roce_qp
*hr_qp
= to_hr_qp(ibqp
);
1377 if ((attr_mask
& IB_QP_PORT
) &&
1378 (attr
->port_num
== 0 || attr
->port_num
> hr_dev
->caps
.num_ports
)) {
1379 ibdev_err(&hr_dev
->ib_dev
, "invalid attr, port_num = %u.\n",
1384 if (attr_mask
& IB_QP_PKEY_INDEX
) {
1385 p
= attr_mask
& IB_QP_PORT
? (attr
->port_num
- 1) : hr_qp
->port
;
1386 if (attr
->pkey_index
>= hr_dev
->caps
.pkey_table_len
[p
]) {
1387 ibdev_err(&hr_dev
->ib_dev
,
1388 "invalid attr, pkey_index = %u.\n",
1394 if (attr_mask
& IB_QP_MAX_QP_RD_ATOMIC
&&
1395 attr
->max_rd_atomic
> hr_dev
->caps
.max_qp_init_rdma
) {
1396 ibdev_err(&hr_dev
->ib_dev
,
1397 "invalid attr, max_rd_atomic = %u.\n",
1398 attr
->max_rd_atomic
);
1402 if (attr_mask
& IB_QP_MAX_DEST_RD_ATOMIC
&&
1403 attr
->max_dest_rd_atomic
> hr_dev
->caps
.max_qp_dest_rdma
) {
1404 ibdev_err(&hr_dev
->ib_dev
,
1405 "invalid attr, max_dest_rd_atomic = %u.\n",
1406 attr
->max_dest_rd_atomic
);
1410 if (attr_mask
& IB_QP_PATH_MTU
)
1411 return check_mtu_validate(hr_dev
, hr_qp
, attr
, attr_mask
);
1416 int hns_roce_modify_qp(struct ib_qp
*ibqp
, struct ib_qp_attr
*attr
,
1417 int attr_mask
, struct ib_udata
*udata
)
1419 struct hns_roce_dev
*hr_dev
= to_hr_dev(ibqp
->device
);
1420 struct hns_roce_ib_modify_qp_resp resp
= {};
1421 struct hns_roce_qp
*hr_qp
= to_hr_qp(ibqp
);
1422 enum ib_qp_state cur_state
, new_state
;
1425 mutex_lock(&hr_qp
->mutex
);
1427 if (attr_mask
& IB_QP_CUR_STATE
&& attr
->cur_qp_state
!= hr_qp
->state
)
1430 cur_state
= hr_qp
->state
;
1431 new_state
= attr_mask
& IB_QP_STATE
? attr
->qp_state
: cur_state
;
1433 if (ibqp
->uobject
&&
1434 (attr_mask
& IB_QP_STATE
) && new_state
== IB_QPS_ERR
) {
1435 if (hr_qp
->en_flags
& HNS_ROCE_QP_CAP_SQ_RECORD_DB
) {
1436 hr_qp
->sq
.head
= *(int *)(hr_qp
->sdb
.virt_addr
);
1438 if (hr_qp
->en_flags
& HNS_ROCE_QP_CAP_RQ_RECORD_DB
)
1439 hr_qp
->rq
.head
= *(int *)(hr_qp
->rdb
.virt_addr
);
1441 ibdev_warn(&hr_dev
->ib_dev
,
1442 "flush cqe is not supported in userspace!\n");
1447 if (!ib_modify_qp_is_ok(cur_state
, new_state
, ibqp
->qp_type
,
1449 ibdev_err(&hr_dev
->ib_dev
, "ib_modify_qp_is_ok failed\n");
1453 ret
= hns_roce_check_qp_attr(ibqp
, attr
, attr_mask
);
1457 if (cur_state
== new_state
&& cur_state
== IB_QPS_RESET
)
1460 ret
= hr_dev
->hw
->modify_qp(ibqp
, attr
, attr_mask
, cur_state
,
1465 if (udata
&& udata
->outlen
) {
1466 resp
.tc_mode
= hr_qp
->tc_mode
;
1467 resp
.priority
= hr_qp
->sl
;
1468 ret
= ib_copy_to_udata(udata
, &resp
,
1469 min(udata
->outlen
, sizeof(resp
)));
1471 ibdev_err_ratelimited(&hr_dev
->ib_dev
,
1472 "failed to copy modify qp resp.\n");
1476 mutex_unlock(&hr_qp
->mutex
);
1478 atomic64_inc(&hr_dev
->dfx_cnt
[HNS_ROCE_DFX_QP_MODIFY_ERR_CNT
]);
1483 void hns_roce_lock_cqs(struct hns_roce_cq
*send_cq
, struct hns_roce_cq
*recv_cq
)
1484 __acquires(&send_cq
->lock
) __acquires(&recv_cq
->lock
)
1486 if (unlikely(send_cq
== NULL
&& recv_cq
== NULL
)) {
1487 __acquire(&send_cq
->lock
);
1488 __acquire(&recv_cq
->lock
);
1489 } else if (unlikely(send_cq
!= NULL
&& recv_cq
== NULL
)) {
1490 spin_lock(&send_cq
->lock
);
1491 __acquire(&recv_cq
->lock
);
1492 } else if (unlikely(send_cq
== NULL
&& recv_cq
!= NULL
)) {
1493 spin_lock(&recv_cq
->lock
);
1494 __acquire(&send_cq
->lock
);
1495 } else if (send_cq
== recv_cq
) {
1496 spin_lock(&send_cq
->lock
);
1497 __acquire(&recv_cq
->lock
);
1498 } else if (send_cq
->cqn
< recv_cq
->cqn
) {
1499 spin_lock(&send_cq
->lock
);
1500 spin_lock_nested(&recv_cq
->lock
, SINGLE_DEPTH_NESTING
);
1502 spin_lock(&recv_cq
->lock
);
1503 spin_lock_nested(&send_cq
->lock
, SINGLE_DEPTH_NESTING
);
1507 void hns_roce_unlock_cqs(struct hns_roce_cq
*send_cq
,
1508 struct hns_roce_cq
*recv_cq
) __releases(&send_cq
->lock
)
1509 __releases(&recv_cq
->lock
)
1511 if (unlikely(send_cq
== NULL
&& recv_cq
== NULL
)) {
1512 __release(&recv_cq
->lock
);
1513 __release(&send_cq
->lock
);
1514 } else if (unlikely(send_cq
!= NULL
&& recv_cq
== NULL
)) {
1515 __release(&recv_cq
->lock
);
1516 spin_unlock(&send_cq
->lock
);
1517 } else if (unlikely(send_cq
== NULL
&& recv_cq
!= NULL
)) {
1518 __release(&send_cq
->lock
);
1519 spin_unlock(&recv_cq
->lock
);
1520 } else if (send_cq
== recv_cq
) {
1521 __release(&recv_cq
->lock
);
1522 spin_unlock(&send_cq
->lock
);
1523 } else if (send_cq
->cqn
< recv_cq
->cqn
) {
1524 spin_unlock(&recv_cq
->lock
);
1525 spin_unlock(&send_cq
->lock
);
1527 spin_unlock(&send_cq
->lock
);
1528 spin_unlock(&recv_cq
->lock
);
1532 static inline void *get_wqe(struct hns_roce_qp
*hr_qp
, u32 offset
)
1534 return hns_roce_buf_offset(hr_qp
->mtr
.kmem
, offset
);
1537 void *hns_roce_get_recv_wqe(struct hns_roce_qp
*hr_qp
, unsigned int n
)
1539 return get_wqe(hr_qp
, hr_qp
->rq
.offset
+ (n
<< hr_qp
->rq
.wqe_shift
));
1542 void *hns_roce_get_send_wqe(struct hns_roce_qp
*hr_qp
, unsigned int n
)
1544 return get_wqe(hr_qp
, hr_qp
->sq
.offset
+ (n
<< hr_qp
->sq
.wqe_shift
));
1547 void *hns_roce_get_extend_sge(struct hns_roce_qp
*hr_qp
, unsigned int n
)
1549 return get_wqe(hr_qp
, hr_qp
->sge
.offset
+ (n
<< hr_qp
->sge
.sge_shift
));
1552 bool hns_roce_wq_overflow(struct hns_roce_wq
*hr_wq
, u32 nreq
,
1553 struct ib_cq
*ib_cq
)
1555 struct hns_roce_cq
*hr_cq
;
1558 cur
= hr_wq
->head
- hr_wq
->tail
;
1559 if (likely(cur
+ nreq
< hr_wq
->wqe_cnt
))
1562 hr_cq
= to_hr_cq(ib_cq
);
1563 spin_lock(&hr_cq
->lock
);
1564 cur
= hr_wq
->head
- hr_wq
->tail
;
1565 spin_unlock(&hr_cq
->lock
);
1567 return cur
+ nreq
>= hr_wq
->wqe_cnt
;
1570 int hns_roce_init_qp_table(struct hns_roce_dev
*hr_dev
)
1572 struct hns_roce_qp_table
*qp_table
= &hr_dev
->qp_table
;
1573 unsigned int reserved_from_bot
;
1576 mutex_init(&qp_table
->scc_mutex
);
1577 mutex_init(&qp_table
->bank_mutex
);
1578 xa_init(&hr_dev
->qp_table_xa
);
1579 xa_init(&qp_table
->dip_xa
);
1581 reserved_from_bot
= hr_dev
->caps
.reserved_qps
;
1583 for (i
= 0; i
< reserved_from_bot
; i
++) {
1584 hr_dev
->qp_table
.bank
[get_qp_bankid(i
)].inuse
++;
1585 hr_dev
->qp_table
.bank
[get_qp_bankid(i
)].min
++;
1588 for (i
= 0; i
< HNS_ROCE_QP_BANK_NUM
; i
++) {
1589 ida_init(&hr_dev
->qp_table
.bank
[i
].ida
);
1590 hr_dev
->qp_table
.bank
[i
].max
= hr_dev
->caps
.num_qps
/
1591 HNS_ROCE_QP_BANK_NUM
- 1;
1592 hr_dev
->qp_table
.bank
[i
].next
= hr_dev
->qp_table
.bank
[i
].min
;
1598 void hns_roce_cleanup_qp_table(struct hns_roce_dev
*hr_dev
)
1602 for (i
= 0; i
< HNS_ROCE_QP_BANK_NUM
; i
++)
1603 ida_destroy(&hr_dev
->qp_table
.bank
[i
].ida
);
1604 xa_destroy(&hr_dev
->qp_table
.dip_xa
);
1605 mutex_destroy(&hr_dev
->qp_table
.bank_mutex
);
1606 mutex_destroy(&hr_dev
->qp_table
.scc_mutex
);