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"
42 #include <rdma/hns-abi.h>
44 #define SQP_NUM (2 * HNS_ROCE_MAX_PORTS)
46 static void flush_work_handle(struct work_struct
*work
)
48 struct hns_roce_work
*flush_work
= container_of(work
,
49 struct hns_roce_work
, work
);
50 struct hns_roce_qp
*hr_qp
= container_of(flush_work
,
51 struct hns_roce_qp
, flush_work
);
52 struct device
*dev
= flush_work
->hr_dev
->dev
;
53 struct ib_qp_attr attr
;
57 attr_mask
= IB_QP_STATE
;
58 attr
.qp_state
= IB_QPS_ERR
;
60 if (test_and_clear_bit(HNS_ROCE_FLUSH_FLAG
, &hr_qp
->flush_flag
)) {
61 ret
= hns_roce_modify_qp(&hr_qp
->ibqp
, &attr
, attr_mask
, NULL
);
63 dev_err(dev
, "Modify QP to error state failed(%d) during CQE flush\n",
68 * make sure we signal QP destroy leg that flush QP was completed
69 * so that it can safely proceed ahead now and destroy QP
71 if (atomic_dec_and_test(&hr_qp
->refcount
))
72 complete(&hr_qp
->free
);
75 void init_flush_work(struct hns_roce_dev
*hr_dev
, struct hns_roce_qp
*hr_qp
)
77 struct hns_roce_work
*flush_work
= &hr_qp
->flush_work
;
79 flush_work
->hr_dev
= hr_dev
;
80 INIT_WORK(&flush_work
->work
, flush_work_handle
);
81 atomic_inc(&hr_qp
->refcount
);
82 queue_work(hr_dev
->irq_workq
, &flush_work
->work
);
85 void hns_roce_qp_event(struct hns_roce_dev
*hr_dev
, u32 qpn
, int event_type
)
87 struct device
*dev
= hr_dev
->dev
;
88 struct hns_roce_qp
*qp
;
90 xa_lock(&hr_dev
->qp_table_xa
);
91 qp
= __hns_roce_qp_lookup(hr_dev
, qpn
);
93 atomic_inc(&qp
->refcount
);
94 xa_unlock(&hr_dev
->qp_table_xa
);
97 dev_warn(dev
, "Async event for bogus QP %08x\n", qpn
);
101 if (hr_dev
->hw_rev
!= HNS_ROCE_HW_VER1
&&
102 (event_type
== HNS_ROCE_EVENT_TYPE_WQ_CATAS_ERROR
||
103 event_type
== HNS_ROCE_EVENT_TYPE_INV_REQ_LOCAL_WQ_ERROR
||
104 event_type
== HNS_ROCE_EVENT_TYPE_LOCAL_WQ_ACCESS_ERROR
)) {
105 qp
->state
= IB_QPS_ERR
;
106 if (!test_and_set_bit(HNS_ROCE_FLUSH_FLAG
, &qp
->flush_flag
))
107 init_flush_work(hr_dev
, qp
);
110 qp
->event(qp
, (enum hns_roce_event
)event_type
);
112 if (atomic_dec_and_test(&qp
->refcount
))
116 static void hns_roce_ib_qp_event(struct hns_roce_qp
*hr_qp
,
117 enum hns_roce_event type
)
119 struct ib_event event
;
120 struct ib_qp
*ibqp
= &hr_qp
->ibqp
;
122 if (ibqp
->event_handler
) {
123 event
.device
= ibqp
->device
;
124 event
.element
.qp
= ibqp
;
126 case HNS_ROCE_EVENT_TYPE_PATH_MIG
:
127 event
.event
= IB_EVENT_PATH_MIG
;
129 case HNS_ROCE_EVENT_TYPE_COMM_EST
:
130 event
.event
= IB_EVENT_COMM_EST
;
132 case HNS_ROCE_EVENT_TYPE_SQ_DRAINED
:
133 event
.event
= IB_EVENT_SQ_DRAINED
;
135 case HNS_ROCE_EVENT_TYPE_SRQ_LAST_WQE_REACH
:
136 event
.event
= IB_EVENT_QP_LAST_WQE_REACHED
;
138 case HNS_ROCE_EVENT_TYPE_WQ_CATAS_ERROR
:
139 event
.event
= IB_EVENT_QP_FATAL
;
141 case HNS_ROCE_EVENT_TYPE_PATH_MIG_FAILED
:
142 event
.event
= IB_EVENT_PATH_MIG_ERR
;
144 case HNS_ROCE_EVENT_TYPE_INV_REQ_LOCAL_WQ_ERROR
:
145 event
.event
= IB_EVENT_QP_REQ_ERR
;
147 case HNS_ROCE_EVENT_TYPE_LOCAL_WQ_ACCESS_ERROR
:
148 event
.event
= IB_EVENT_QP_ACCESS_ERR
;
151 dev_dbg(ibqp
->device
->dev
.parent
, "roce_ib: Unexpected event type %d on QP %06lx\n",
155 ibqp
->event_handler(&event
, ibqp
->qp_context
);
159 static int alloc_qpn(struct hns_roce_dev
*hr_dev
, struct hns_roce_qp
*hr_qp
)
161 unsigned long num
= 0;
164 if (hr_qp
->ibqp
.qp_type
== IB_QPT_GSI
) {
165 /* when hw version is v1, the sqpn is allocated */
166 if (hr_dev
->hw_rev
== HNS_ROCE_HW_VER1
)
167 num
= HNS_ROCE_MAX_PORTS
+
168 hr_dev
->iboe
.phy_port
[hr_qp
->port
];
172 hr_qp
->doorbell_qpn
= 1;
174 ret
= hns_roce_bitmap_alloc_range(&hr_dev
->qp_table
.bitmap
,
177 ibdev_err(&hr_dev
->ib_dev
, "Failed to alloc bitmap\n");
181 hr_qp
->doorbell_qpn
= (u32
)num
;
189 enum hns_roce_qp_state
to_hns_roce_state(enum ib_qp_state state
)
193 return HNS_ROCE_QP_STATE_RST
;
195 return HNS_ROCE_QP_STATE_INIT
;
197 return HNS_ROCE_QP_STATE_RTR
;
199 return HNS_ROCE_QP_STATE_RTS
;
201 return HNS_ROCE_QP_STATE_SQD
;
203 return HNS_ROCE_QP_STATE_ERR
;
205 return HNS_ROCE_QP_NUM_STATE
;
209 static void add_qp_to_list(struct hns_roce_dev
*hr_dev
,
210 struct hns_roce_qp
*hr_qp
,
211 struct ib_cq
*send_cq
, struct ib_cq
*recv_cq
)
213 struct hns_roce_cq
*hr_send_cq
, *hr_recv_cq
;
216 hr_send_cq
= send_cq
? to_hr_cq(send_cq
) : NULL
;
217 hr_recv_cq
= recv_cq
? to_hr_cq(recv_cq
) : NULL
;
219 spin_lock_irqsave(&hr_dev
->qp_list_lock
, flags
);
220 hns_roce_lock_cqs(hr_send_cq
, hr_recv_cq
);
222 list_add_tail(&hr_qp
->node
, &hr_dev
->qp_list
);
224 list_add_tail(&hr_qp
->sq_node
, &hr_send_cq
->sq_list
);
226 list_add_tail(&hr_qp
->rq_node
, &hr_recv_cq
->rq_list
);
228 hns_roce_unlock_cqs(hr_send_cq
, hr_recv_cq
);
229 spin_unlock_irqrestore(&hr_dev
->qp_list_lock
, flags
);
232 static int hns_roce_qp_store(struct hns_roce_dev
*hr_dev
,
233 struct hns_roce_qp
*hr_qp
,
234 struct ib_qp_init_attr
*init_attr
)
236 struct xarray
*xa
= &hr_dev
->qp_table_xa
;
242 ret
= xa_err(xa_store_irq(xa
, hr_qp
->qpn
, hr_qp
, GFP_KERNEL
));
244 dev_err(hr_dev
->dev
, "Failed to xa store for QPC\n");
246 /* add QP to device's QP list for softwc */
247 add_qp_to_list(hr_dev
, hr_qp
, init_attr
->send_cq
,
253 static int alloc_qpc(struct hns_roce_dev
*hr_dev
, struct hns_roce_qp
*hr_qp
)
255 struct hns_roce_qp_table
*qp_table
= &hr_dev
->qp_table
;
256 struct device
*dev
= hr_dev
->dev
;
262 /* In v1 engine, GSI QP context is saved in the RoCE hw's register */
263 if (hr_qp
->ibqp
.qp_type
== IB_QPT_GSI
&&
264 hr_dev
->hw_rev
== HNS_ROCE_HW_VER1
)
267 /* Alloc memory for QPC */
268 ret
= hns_roce_table_get(hr_dev
, &qp_table
->qp_table
, hr_qp
->qpn
);
270 dev_err(dev
, "Failed to get QPC table\n");
274 /* Alloc memory for IRRL */
275 ret
= hns_roce_table_get(hr_dev
, &qp_table
->irrl_table
, hr_qp
->qpn
);
277 dev_err(dev
, "Failed to get IRRL table\n");
281 if (hr_dev
->caps
.trrl_entry_sz
) {
282 /* Alloc memory for TRRL */
283 ret
= hns_roce_table_get(hr_dev
, &qp_table
->trrl_table
,
286 dev_err(dev
, "Failed to get TRRL table\n");
291 if (hr_dev
->caps
.sccc_entry_sz
) {
292 /* Alloc memory for SCC CTX */
293 ret
= hns_roce_table_get(hr_dev
, &qp_table
->sccc_table
,
296 dev_err(dev
, "Failed to get SCC CTX table\n");
304 if (hr_dev
->caps
.trrl_entry_sz
)
305 hns_roce_table_put(hr_dev
, &qp_table
->trrl_table
, hr_qp
->qpn
);
308 hns_roce_table_put(hr_dev
, &qp_table
->irrl_table
, hr_qp
->qpn
);
311 hns_roce_table_put(hr_dev
, &qp_table
->qp_table
, hr_qp
->qpn
);
317 void hns_roce_qp_remove(struct hns_roce_dev
*hr_dev
, struct hns_roce_qp
*hr_qp
)
319 struct xarray
*xa
= &hr_dev
->qp_table_xa
;
322 list_del(&hr_qp
->node
);
323 list_del(&hr_qp
->sq_node
);
324 list_del(&hr_qp
->rq_node
);
326 xa_lock_irqsave(xa
, flags
);
327 __xa_erase(xa
, hr_qp
->qpn
& (hr_dev
->caps
.num_qps
- 1));
328 xa_unlock_irqrestore(xa
, flags
);
331 static void free_qpc(struct hns_roce_dev
*hr_dev
, struct hns_roce_qp
*hr_qp
)
333 struct hns_roce_qp_table
*qp_table
= &hr_dev
->qp_table
;
335 /* In v1 engine, GSI QP context is saved in the RoCE hw's register */
336 if (hr_qp
->ibqp
.qp_type
== IB_QPT_GSI
&&
337 hr_dev
->hw_rev
== HNS_ROCE_HW_VER1
)
340 if (hr_dev
->caps
.trrl_entry_sz
)
341 hns_roce_table_put(hr_dev
, &qp_table
->trrl_table
, hr_qp
->qpn
);
342 hns_roce_table_put(hr_dev
, &qp_table
->irrl_table
, hr_qp
->qpn
);
345 static void free_qpn(struct hns_roce_dev
*hr_dev
, struct hns_roce_qp
*hr_qp
)
347 struct hns_roce_qp_table
*qp_table
= &hr_dev
->qp_table
;
349 if (hr_qp
->ibqp
.qp_type
== IB_QPT_GSI
)
352 if (hr_qp
->qpn
< hr_dev
->caps
.reserved_qps
)
355 hns_roce_bitmap_free_range(&qp_table
->bitmap
, hr_qp
->qpn
, 1, BITMAP_RR
);
358 static int set_rq_size(struct hns_roce_dev
*hr_dev
,
359 struct ib_qp_cap
*cap
, bool is_user
, int has_rq
,
360 struct hns_roce_qp
*hr_qp
)
364 /* If srq exist, set zero for relative number of rq */
366 hr_qp
->rq
.wqe_cnt
= 0;
367 hr_qp
->rq
.max_gs
= 0;
368 cap
->max_recv_wr
= 0;
369 cap
->max_recv_sge
= 0;
374 /* Check the validity of QP support capacity */
375 if (!cap
->max_recv_wr
|| cap
->max_recv_wr
> hr_dev
->caps
.max_wqes
||
376 cap
->max_recv_sge
> hr_dev
->caps
.max_rq_sg
) {
377 ibdev_err(&hr_dev
->ib_dev
, "RQ config error, depth=%u, sge=%d\n",
378 cap
->max_recv_wr
, cap
->max_recv_sge
);
382 max_cnt
= max(cap
->max_recv_wr
, hr_dev
->caps
.min_wqes
);
384 hr_qp
->rq
.wqe_cnt
= roundup_pow_of_two(max_cnt
);
385 if ((u32
)hr_qp
->rq
.wqe_cnt
> hr_dev
->caps
.max_wqes
) {
386 ibdev_err(&hr_dev
->ib_dev
, "rq depth %u too large\n",
391 max_cnt
= max(1U, cap
->max_recv_sge
);
392 hr_qp
->rq
.max_gs
= roundup_pow_of_two(max_cnt
);
394 if (hr_dev
->caps
.max_rq_sg
<= HNS_ROCE_SGE_IN_WQE
)
395 hr_qp
->rq
.wqe_shift
= ilog2(hr_dev
->caps
.max_rq_desc_sz
);
397 hr_qp
->rq
.wqe_shift
= ilog2(hr_dev
->caps
.max_rq_desc_sz
*
400 cap
->max_recv_wr
= hr_qp
->rq
.wqe_cnt
;
401 cap
->max_recv_sge
= hr_qp
->rq
.max_gs
;
406 static int check_sq_size_with_integrity(struct hns_roce_dev
*hr_dev
,
407 struct ib_qp_cap
*cap
,
408 struct hns_roce_ib_create_qp
*ucmd
)
410 u32 roundup_sq_stride
= roundup_pow_of_two(hr_dev
->caps
.max_sq_desc_sz
);
411 u8 max_sq_stride
= ilog2(roundup_sq_stride
);
413 /* Sanity check SQ size before proceeding */
414 if (ucmd
->log_sq_stride
> max_sq_stride
||
415 ucmd
->log_sq_stride
< HNS_ROCE_IB_MIN_SQ_STRIDE
) {
416 ibdev_err(&hr_dev
->ib_dev
, "Failed to check SQ stride size\n");
420 if (cap
->max_send_sge
> hr_dev
->caps
.max_sq_sg
) {
421 ibdev_err(&hr_dev
->ib_dev
, "Failed to check SQ SGE size %d\n",
429 static int set_user_sq_size(struct hns_roce_dev
*hr_dev
,
430 struct ib_qp_cap
*cap
, struct hns_roce_qp
*hr_qp
,
431 struct hns_roce_ib_create_qp
*ucmd
)
438 if (check_shl_overflow(1, ucmd
->log_sq_bb_count
, &hr_qp
->sq
.wqe_cnt
) ||
439 hr_qp
->sq
.wqe_cnt
> hr_dev
->caps
.max_wqes
)
442 ret
= check_sq_size_with_integrity(hr_dev
, cap
, ucmd
);
444 ibdev_err(&hr_dev
->ib_dev
, "Failed to check user SQ size limit\n");
448 hr_qp
->sq
.wqe_shift
= ucmd
->log_sq_stride
;
450 max_cnt
= max(1U, cap
->max_send_sge
);
451 if (hr_dev
->hw_rev
== HNS_ROCE_HW_VER1
)
452 hr_qp
->sq
.max_gs
= roundup_pow_of_two(max_cnt
);
454 hr_qp
->sq
.max_gs
= max_cnt
;
456 if (hr_qp
->sq
.max_gs
> HNS_ROCE_SGE_IN_WQE
)
457 hr_qp
->sge
.sge_cnt
= roundup_pow_of_two(hr_qp
->sq
.wqe_cnt
*
458 (hr_qp
->sq
.max_gs
- 2));
460 if (hr_qp
->sq
.max_gs
> HNS_ROCE_SGE_IN_WQE
&&
461 hr_dev
->pci_dev
->revision
== PCI_REVISION_ID_HIP08_A
) {
462 if (hr_qp
->sge
.sge_cnt
> hr_dev
->caps
.max_extend_sg
) {
463 ibdev_err(&hr_dev
->ib_dev
,
464 "Failed to check extended SGE size limit %d\n",
470 hr_qp
->sge
.sge_shift
= 4;
471 ex_sge_num
= hr_qp
->sge
.sge_cnt
;
473 /* Get buf size, SQ and RQ are aligned to page_szie */
474 if (hr_dev
->hw_rev
== HNS_ROCE_HW_VER1
) {
475 hr_qp
->buff_size
= round_up((hr_qp
->rq
.wqe_cnt
<<
476 hr_qp
->rq
.wqe_shift
), PAGE_SIZE
) +
477 round_up((hr_qp
->sq
.wqe_cnt
<<
478 hr_qp
->sq
.wqe_shift
), PAGE_SIZE
);
480 hr_qp
->sq
.offset
= 0;
481 hr_qp
->rq
.offset
= round_up((hr_qp
->sq
.wqe_cnt
<<
482 hr_qp
->sq
.wqe_shift
), PAGE_SIZE
);
484 page_size
= 1 << (hr_dev
->caps
.mtt_buf_pg_sz
+ PAGE_SHIFT
);
485 hr_qp
->sge
.sge_cnt
= ex_sge_num
?
486 max(page_size
/ (1 << hr_qp
->sge
.sge_shift
), ex_sge_num
) : 0;
487 hr_qp
->buff_size
= round_up((hr_qp
->rq
.wqe_cnt
<<
488 hr_qp
->rq
.wqe_shift
), page_size
) +
489 round_up((hr_qp
->sge
.sge_cnt
<<
490 hr_qp
->sge
.sge_shift
), page_size
) +
491 round_up((hr_qp
->sq
.wqe_cnt
<<
492 hr_qp
->sq
.wqe_shift
), page_size
);
494 hr_qp
->sq
.offset
= 0;
496 hr_qp
->sge
.offset
= round_up((hr_qp
->sq
.wqe_cnt
<<
497 hr_qp
->sq
.wqe_shift
),
499 hr_qp
->rq
.offset
= hr_qp
->sge
.offset
+
500 round_up((hr_qp
->sge
.sge_cnt
<<
501 hr_qp
->sge
.sge_shift
),
504 hr_qp
->rq
.offset
= round_up((hr_qp
->sq
.wqe_cnt
<<
505 hr_qp
->sq
.wqe_shift
),
513 static int split_wqe_buf_region(struct hns_roce_dev
*hr_dev
,
514 struct hns_roce_qp
*hr_qp
,
515 struct hns_roce_buf_region
*regions
,
516 int region_max
, int page_shift
)
518 int page_size
= 1 << page_shift
;
524 if (hr_qp
->buff_size
< 1 || region_max
< 1)
527 if (hr_qp
->sge
.sge_cnt
> 0)
528 is_extend_sge
= true;
530 is_extend_sge
= false;
534 buf_size
= hr_qp
->sge
.offset
- hr_qp
->sq
.offset
;
536 buf_size
= hr_qp
->rq
.offset
- hr_qp
->sq
.offset
;
538 if (buf_size
> 0 && region_cnt
< region_max
) {
539 buf_cnt
= DIV_ROUND_UP(buf_size
, page_size
);
540 hns_roce_init_buf_region(®ions
[region_cnt
],
541 hr_dev
->caps
.wqe_sq_hop_num
,
542 hr_qp
->sq
.offset
/ page_size
,
549 buf_size
= hr_qp
->rq
.offset
- hr_qp
->sge
.offset
;
550 if (buf_size
> 0 && region_cnt
< region_max
) {
551 buf_cnt
= DIV_ROUND_UP(buf_size
, page_size
);
552 hns_roce_init_buf_region(®ions
[region_cnt
],
553 hr_dev
->caps
.wqe_sge_hop_num
,
554 hr_qp
->sge
.offset
/ page_size
,
561 buf_size
= hr_qp
->buff_size
- hr_qp
->rq
.offset
;
563 buf_cnt
= DIV_ROUND_UP(buf_size
, page_size
);
564 hns_roce_init_buf_region(®ions
[region_cnt
],
565 hr_dev
->caps
.wqe_rq_hop_num
,
566 hr_qp
->rq
.offset
/ page_size
,
574 static int set_extend_sge_param(struct hns_roce_dev
*hr_dev
,
575 struct hns_roce_qp
*hr_qp
)
577 struct device
*dev
= hr_dev
->dev
;
579 if (hr_qp
->sq
.max_gs
> 2) {
580 hr_qp
->sge
.sge_cnt
= roundup_pow_of_two(hr_qp
->sq
.wqe_cnt
*
581 (hr_qp
->sq
.max_gs
- 2));
582 hr_qp
->sge
.sge_shift
= 4;
585 /* ud sqwqe's sge use extend sge */
586 if (hr_dev
->hw_rev
!= HNS_ROCE_HW_VER1
&&
587 hr_qp
->ibqp
.qp_type
== IB_QPT_GSI
) {
588 hr_qp
->sge
.sge_cnt
= roundup_pow_of_two(hr_qp
->sq
.wqe_cnt
*
590 hr_qp
->sge
.sge_shift
= 4;
593 if (hr_qp
->sq
.max_gs
> 2 &&
594 hr_dev
->pci_dev
->revision
== PCI_REVISION_ID_HIP08_A
) {
595 if (hr_qp
->sge
.sge_cnt
> hr_dev
->caps
.max_extend_sg
) {
596 dev_err(dev
, "The extended sge cnt error! sge_cnt=%d\n",
605 static int set_kernel_sq_size(struct hns_roce_dev
*hr_dev
,
606 struct ib_qp_cap
*cap
, struct hns_roce_qp
*hr_qp
)
613 if (!cap
->max_send_wr
|| cap
->max_send_wr
> hr_dev
->caps
.max_wqes
||
614 cap
->max_send_sge
> hr_dev
->caps
.max_sq_sg
||
615 cap
->max_inline_data
> hr_dev
->caps
.max_sq_inline
) {
616 ibdev_err(&hr_dev
->ib_dev
,
617 "SQ WR or sge or inline data error!\n");
621 hr_qp
->sq
.wqe_shift
= ilog2(hr_dev
->caps
.max_sq_desc_sz
);
623 max_cnt
= max(cap
->max_send_wr
, hr_dev
->caps
.min_wqes
);
625 hr_qp
->sq
.wqe_cnt
= roundup_pow_of_two(max_cnt
);
626 if ((u32
)hr_qp
->sq
.wqe_cnt
> hr_dev
->caps
.max_wqes
) {
627 ibdev_err(&hr_dev
->ib_dev
,
628 "while setting kernel sq size, sq.wqe_cnt too large\n");
632 /* Get data_seg numbers */
633 max_cnt
= max(1U, cap
->max_send_sge
);
634 if (hr_dev
->hw_rev
== HNS_ROCE_HW_VER1
)
635 hr_qp
->sq
.max_gs
= roundup_pow_of_two(max_cnt
);
637 hr_qp
->sq
.max_gs
= max_cnt
;
639 ret
= set_extend_sge_param(hr_dev
, hr_qp
);
641 ibdev_err(&hr_dev
->ib_dev
, "set extend sge parameters fail\n");
645 /* Get buf size, SQ and RQ are aligned to PAGE_SIZE */
646 page_size
= 1 << (hr_dev
->caps
.mtt_buf_pg_sz
+ PAGE_SHIFT
);
647 hr_qp
->sq
.offset
= 0;
648 size
= round_up(hr_qp
->sq
.wqe_cnt
<< hr_qp
->sq
.wqe_shift
, page_size
);
650 if (hr_dev
->hw_rev
!= HNS_ROCE_HW_VER1
&& hr_qp
->sge
.sge_cnt
) {
651 hr_qp
->sge
.sge_cnt
= max(page_size
/(1 << hr_qp
->sge
.sge_shift
),
652 (u32
)hr_qp
->sge
.sge_cnt
);
653 hr_qp
->sge
.offset
= size
;
654 size
+= round_up(hr_qp
->sge
.sge_cnt
<< hr_qp
->sge
.sge_shift
,
658 hr_qp
->rq
.offset
= size
;
659 size
+= round_up((hr_qp
->rq
.wqe_cnt
<< hr_qp
->rq
.wqe_shift
), page_size
);
660 hr_qp
->buff_size
= size
;
662 /* Get wr and sge number which send */
663 cap
->max_send_wr
= hr_qp
->sq
.wqe_cnt
;
664 cap
->max_send_sge
= hr_qp
->sq
.max_gs
;
666 /* We don't support inline sends for kernel QPs (yet) */
667 cap
->max_inline_data
= 0;
672 static int hns_roce_qp_has_sq(struct ib_qp_init_attr
*attr
)
674 if (attr
->qp_type
== IB_QPT_XRC_TGT
|| !attr
->cap
.max_send_wr
)
680 static int hns_roce_qp_has_rq(struct ib_qp_init_attr
*attr
)
682 if (attr
->qp_type
== IB_QPT_XRC_INI
||
683 attr
->qp_type
== IB_QPT_XRC_TGT
|| attr
->srq
||
684 !attr
->cap
.max_recv_wr
)
690 static int alloc_rq_inline_buf(struct hns_roce_qp
*hr_qp
,
691 struct ib_qp_init_attr
*init_attr
)
693 u32 max_recv_sge
= init_attr
->cap
.max_recv_sge
;
694 struct hns_roce_rinl_wqe
*wqe_list
;
695 u32 wqe_cnt
= hr_qp
->rq
.wqe_cnt
;
698 /* allocate recv inline buf */
699 wqe_list
= kcalloc(wqe_cnt
, sizeof(struct hns_roce_rinl_wqe
),
705 /* Allocate a continuous buffer for all inline sge we need */
706 wqe_list
[0].sg_list
= kcalloc(wqe_cnt
, (max_recv_sge
*
707 sizeof(struct hns_roce_rinl_sge
)),
709 if (!wqe_list
[0].sg_list
)
712 /* Assign buffers of sg_list to each inline wqe */
713 for (i
= 1; i
< wqe_cnt
; i
++)
714 wqe_list
[i
].sg_list
= &wqe_list
[0].sg_list
[i
* max_recv_sge
];
716 hr_qp
->rq_inl_buf
.wqe_list
= wqe_list
;
717 hr_qp
->rq_inl_buf
.wqe_cnt
= wqe_cnt
;
728 static void free_rq_inline_buf(struct hns_roce_qp
*hr_qp
)
730 kfree(hr_qp
->rq_inl_buf
.wqe_list
[0].sg_list
);
731 kfree(hr_qp
->rq_inl_buf
.wqe_list
);
734 static int map_wqe_buf(struct hns_roce_dev
*hr_dev
, struct hns_roce_qp
*hr_qp
,
735 u32 page_shift
, bool is_user
)
737 /* WQE buffer include 3 parts: SQ, extend SGE and RQ. */
738 #define HNS_ROCE_WQE_REGION_MAX 3
739 struct hns_roce_buf_region regions
[HNS_ROCE_WQE_REGION_MAX
] = {};
740 dma_addr_t
*buf_list
[HNS_ROCE_WQE_REGION_MAX
] = {};
741 struct ib_device
*ibdev
= &hr_dev
->ib_dev
;
742 struct hns_roce_buf_region
*r
;
748 region_count
= split_wqe_buf_region(hr_dev
, hr_qp
, regions
,
749 ARRAY_SIZE(regions
), page_shift
);
751 /* alloc a tmp list to store WQE buffers address */
752 ret
= hns_roce_alloc_buf_list(regions
, buf_list
, region_count
);
754 ibdev_err(ibdev
, "Failed to alloc WQE buffer list\n");
758 for (i
= 0; i
< region_count
; i
++) {
761 buf_count
= hns_roce_get_umem_bufs(hr_dev
, buf_list
[i
],
762 r
->count
, r
->offset
, hr_qp
->umem
,
765 buf_count
= hns_roce_get_kmem_bufs(hr_dev
, buf_list
[i
],
766 r
->count
, r
->offset
, &hr_qp
->hr_buf
);
768 if (buf_count
!= r
->count
) {
769 ibdev_err(ibdev
, "Failed to get %s WQE buf, expect %d = %d.\n",
770 is_user
? "user" : "kernel",
771 r
->count
, buf_count
);
777 hr_qp
->wqe_bt_pg_shift
= hr_dev
->caps
.mtt_ba_pg_sz
;
778 hns_roce_mtr_init(&hr_qp
->mtr
, PAGE_SHIFT
+ hr_qp
->wqe_bt_pg_shift
,
780 ret
= hns_roce_mtr_attach(hr_dev
, &hr_qp
->mtr
, buf_list
, regions
,
783 ibdev_err(ibdev
, "Failed to attach WQE's mtr\n");
787 hns_roce_mtr_cleanup(hr_dev
, &hr_qp
->mtr
);
789 hns_roce_free_buf_list(buf_list
, region_count
);
794 static int alloc_qp_buf(struct hns_roce_dev
*hr_dev
, struct hns_roce_qp
*hr_qp
,
795 struct ib_qp_init_attr
*init_attr
,
796 struct ib_udata
*udata
, unsigned long addr
)
798 u32 page_shift
= PAGE_SHIFT
+ hr_dev
->caps
.mtt_buf_pg_sz
;
799 struct ib_device
*ibdev
= &hr_dev
->ib_dev
;
800 bool is_rq_buf_inline
;
803 is_rq_buf_inline
= (hr_dev
->caps
.flags
& HNS_ROCE_CAP_FLAG_RQ_INLINE
) &&
804 hns_roce_qp_has_rq(init_attr
);
805 if (is_rq_buf_inline
) {
806 ret
= alloc_rq_inline_buf(hr_qp
, init_attr
);
808 ibdev_err(ibdev
, "Failed to alloc inline RQ buffer\n");
814 hr_qp
->umem
= ib_umem_get(ibdev
, addr
, hr_qp
->buff_size
, 0);
815 if (IS_ERR(hr_qp
->umem
)) {
816 ret
= PTR_ERR(hr_qp
->umem
);
820 ret
= hns_roce_buf_alloc(hr_dev
, hr_qp
->buff_size
,
821 (1 << page_shift
) * 2,
822 &hr_qp
->hr_buf
, page_shift
);
827 ret
= map_wqe_buf(hr_dev
, hr_qp
, page_shift
, udata
);
834 if (is_rq_buf_inline
)
835 free_rq_inline_buf(hr_qp
);
839 ib_umem_release(hr_qp
->umem
);
842 hns_roce_buf_free(hr_dev
, hr_qp
->buff_size
, &hr_qp
->hr_buf
);
845 ibdev_err(ibdev
, "Failed to alloc WQE buffer, ret %d.\n", ret
);
850 static void free_qp_buf(struct hns_roce_dev
*hr_dev
, struct hns_roce_qp
*hr_qp
)
852 hns_roce_mtr_cleanup(hr_dev
, &hr_qp
->mtr
);
854 ib_umem_release(hr_qp
->umem
);
858 if (hr_qp
->hr_buf
.nbufs
> 0)
859 hns_roce_buf_free(hr_dev
, hr_qp
->buff_size
, &hr_qp
->hr_buf
);
861 if ((hr_dev
->caps
.flags
& HNS_ROCE_CAP_FLAG_RQ_INLINE
) &&
863 free_rq_inline_buf(hr_qp
);
866 static inline bool user_qp_has_sdb(struct hns_roce_dev
*hr_dev
,
867 struct ib_qp_init_attr
*init_attr
,
868 struct ib_udata
*udata
,
869 struct hns_roce_ib_create_qp_resp
*resp
,
870 struct hns_roce_ib_create_qp
*ucmd
)
872 return ((hr_dev
->caps
.flags
& HNS_ROCE_CAP_FLAG_SQ_RECORD_DB
) &&
873 udata
->outlen
>= offsetofend(typeof(*resp
), cap_flags
) &&
874 hns_roce_qp_has_sq(init_attr
) &&
875 udata
->inlen
>= offsetofend(typeof(*ucmd
), sdb_addr
));
878 static inline bool user_qp_has_rdb(struct hns_roce_dev
*hr_dev
,
879 struct ib_qp_init_attr
*init_attr
,
880 struct ib_udata
*udata
,
881 struct hns_roce_ib_create_qp_resp
*resp
)
883 return ((hr_dev
->caps
.flags
& HNS_ROCE_CAP_FLAG_RECORD_DB
) &&
884 udata
->outlen
>= offsetofend(typeof(*resp
), cap_flags
) &&
885 hns_roce_qp_has_rq(init_attr
));
888 static inline bool kernel_qp_has_rdb(struct hns_roce_dev
*hr_dev
,
889 struct ib_qp_init_attr
*init_attr
)
891 return ((hr_dev
->caps
.flags
& HNS_ROCE_CAP_FLAG_RECORD_DB
) &&
892 hns_roce_qp_has_rq(init_attr
));
895 static int alloc_qp_db(struct hns_roce_dev
*hr_dev
, struct hns_roce_qp
*hr_qp
,
896 struct ib_qp_init_attr
*init_attr
,
897 struct ib_udata
*udata
,
898 struct hns_roce_ib_create_qp
*ucmd
,
899 struct hns_roce_ib_create_qp_resp
*resp
)
901 struct hns_roce_ucontext
*uctx
= rdma_udata_to_drv_context(
902 udata
, struct hns_roce_ucontext
, ibucontext
);
903 struct ib_device
*ibdev
= &hr_dev
->ib_dev
;
907 if (user_qp_has_sdb(hr_dev
, init_attr
, udata
, resp
, ucmd
)) {
908 ret
= hns_roce_db_map_user(uctx
, udata
, ucmd
->sdb_addr
,
912 "Failed to map user SQ doorbell\n");
916 resp
->cap_flags
|= HNS_ROCE_SUPPORT_SQ_RECORD_DB
;
919 if (user_qp_has_rdb(hr_dev
, init_attr
, udata
, resp
)) {
920 ret
= hns_roce_db_map_user(uctx
, udata
, ucmd
->db_addr
,
924 "Failed to map user RQ doorbell\n");
928 resp
->cap_flags
|= HNS_ROCE_SUPPORT_RQ_RECORD_DB
;
931 /* QP doorbell register address */
932 hr_qp
->sq
.db_reg_l
= hr_dev
->reg_base
+ hr_dev
->sdb_offset
+
933 DB_REG_OFFSET
* hr_dev
->priv_uar
.index
;
934 hr_qp
->rq
.db_reg_l
= hr_dev
->reg_base
+ hr_dev
->odb_offset
+
935 DB_REG_OFFSET
* hr_dev
->priv_uar
.index
;
937 if (kernel_qp_has_rdb(hr_dev
, init_attr
)) {
938 ret
= hns_roce_alloc_db(hr_dev
, &hr_qp
->rdb
, 0);
941 "Failed to alloc kernel RQ doorbell\n");
944 *hr_qp
->rdb
.db_record
= 0;
951 if (udata
&& hr_qp
->sdb_en
)
952 hns_roce_db_unmap_user(uctx
, &hr_qp
->sdb
);
957 static void free_qp_db(struct hns_roce_dev
*hr_dev
, struct hns_roce_qp
*hr_qp
,
958 struct ib_udata
*udata
)
960 struct hns_roce_ucontext
*uctx
= rdma_udata_to_drv_context(
961 udata
, struct hns_roce_ucontext
, ibucontext
);
965 hns_roce_db_unmap_user(uctx
, &hr_qp
->rdb
);
967 hns_roce_db_unmap_user(uctx
, &hr_qp
->sdb
);
970 hns_roce_free_db(hr_dev
, &hr_qp
->rdb
);
974 static int alloc_kernel_wrid(struct hns_roce_dev
*hr_dev
,
975 struct hns_roce_qp
*hr_qp
)
977 struct ib_device
*ibdev
= &hr_dev
->ib_dev
;
982 sq_wrid
= kcalloc(hr_qp
->sq
.wqe_cnt
, sizeof(u64
), GFP_KERNEL
);
983 if (ZERO_OR_NULL_PTR(sq_wrid
)) {
984 ibdev_err(ibdev
, "Failed to alloc SQ wrid\n");
988 if (hr_qp
->rq
.wqe_cnt
) {
989 rq_wrid
= kcalloc(hr_qp
->rq
.wqe_cnt
, sizeof(u64
), GFP_KERNEL
);
990 if (ZERO_OR_NULL_PTR(rq_wrid
)) {
991 ibdev_err(ibdev
, "Failed to alloc RQ wrid\n");
997 hr_qp
->sq
.wrid
= sq_wrid
;
998 hr_qp
->rq
.wrid
= rq_wrid
;
1006 static void free_kernel_wrid(struct hns_roce_dev
*hr_dev
,
1007 struct hns_roce_qp
*hr_qp
)
1009 kfree(hr_qp
->rq
.wrid
);
1010 kfree(hr_qp
->sq
.wrid
);
1013 static int set_qp_param(struct hns_roce_dev
*hr_dev
, struct hns_roce_qp
*hr_qp
,
1014 struct ib_qp_init_attr
*init_attr
,
1015 struct ib_udata
*udata
,
1016 struct hns_roce_ib_create_qp
*ucmd
)
1018 struct ib_device
*ibdev
= &hr_dev
->ib_dev
;
1021 hr_qp
->ibqp
.qp_type
= init_attr
->qp_type
;
1023 if (init_attr
->sq_sig_type
== IB_SIGNAL_ALL_WR
)
1024 hr_qp
->sq_signal_bits
= IB_SIGNAL_ALL_WR
;
1026 hr_qp
->sq_signal_bits
= IB_SIGNAL_REQ_WR
;
1028 ret
= set_rq_size(hr_dev
, &init_attr
->cap
, udata
,
1029 hns_roce_qp_has_rq(init_attr
), hr_qp
);
1031 ibdev_err(ibdev
, "Failed to set user RQ size\n");
1036 if (ib_copy_from_udata(ucmd
, udata
, sizeof(*ucmd
))) {
1037 ibdev_err(ibdev
, "Failed to copy QP ucmd\n");
1041 ret
= set_user_sq_size(hr_dev
, &init_attr
->cap
, hr_qp
, ucmd
);
1043 ibdev_err(ibdev
, "Failed to set user SQ size\n");
1045 if (init_attr
->create_flags
&
1046 IB_QP_CREATE_BLOCK_MULTICAST_LOOPBACK
) {
1047 ibdev_err(ibdev
, "Failed to check multicast loopback\n");
1051 if (init_attr
->create_flags
& IB_QP_CREATE_IPOIB_UD_LSO
) {
1052 ibdev_err(ibdev
, "Failed to check ipoib ud lso\n");
1056 ret
= set_kernel_sq_size(hr_dev
, &init_attr
->cap
, hr_qp
);
1058 ibdev_err(ibdev
, "Failed to set kernel SQ size\n");
1064 static int hns_roce_create_qp_common(struct hns_roce_dev
*hr_dev
,
1065 struct ib_pd
*ib_pd
,
1066 struct ib_qp_init_attr
*init_attr
,
1067 struct ib_udata
*udata
,
1068 struct hns_roce_qp
*hr_qp
)
1070 struct hns_roce_ib_create_qp_resp resp
= {};
1071 struct ib_device
*ibdev
= &hr_dev
->ib_dev
;
1072 struct hns_roce_ib_create_qp ucmd
;
1075 mutex_init(&hr_qp
->mutex
);
1076 spin_lock_init(&hr_qp
->sq
.lock
);
1077 spin_lock_init(&hr_qp
->rq
.lock
);
1079 hr_qp
->state
= IB_QPS_RESET
;
1080 hr_qp
->flush_flag
= 0;
1082 ret
= set_qp_param(hr_dev
, hr_qp
, init_attr
, udata
, &ucmd
);
1084 ibdev_err(ibdev
, "Failed to set QP param\n");
1089 ret
= alloc_kernel_wrid(hr_dev
, hr_qp
);
1091 ibdev_err(ibdev
, "Failed to alloc wrid\n");
1096 ret
= alloc_qp_db(hr_dev
, hr_qp
, init_attr
, udata
, &ucmd
, &resp
);
1098 ibdev_err(ibdev
, "Failed to alloc QP doorbell\n");
1102 ret
= alloc_qp_buf(hr_dev
, hr_qp
, init_attr
, udata
, ucmd
.buf_addr
);
1104 ibdev_err(ibdev
, "Failed to alloc QP buffer\n");
1108 ret
= alloc_qpn(hr_dev
, hr_qp
);
1110 ibdev_err(ibdev
, "Failed to alloc QPN\n");
1114 ret
= alloc_qpc(hr_dev
, hr_qp
);
1116 ibdev_err(ibdev
, "Failed to alloc QP context\n");
1120 ret
= hns_roce_qp_store(hr_dev
, hr_qp
, init_attr
);
1122 ibdev_err(ibdev
, "Failed to store QP\n");
1127 ret
= ib_copy_to_udata(udata
, &resp
,
1128 min(udata
->outlen
, sizeof(resp
)));
1130 ibdev_err(ibdev
, "copy qp resp failed!\n");
1135 if (hr_dev
->caps
.flags
& HNS_ROCE_CAP_FLAG_QP_FLOW_CTRL
) {
1136 ret
= hr_dev
->hw
->qp_flow_control_init(hr_dev
, hr_qp
);
1141 hr_qp
->ibqp
.qp_num
= hr_qp
->qpn
;
1142 hr_qp
->event
= hns_roce_ib_qp_event
;
1143 atomic_set(&hr_qp
->refcount
, 1);
1144 init_completion(&hr_qp
->free
);
1149 hns_roce_qp_remove(hr_dev
, hr_qp
);
1151 free_qpc(hr_dev
, hr_qp
);
1153 free_qpn(hr_dev
, hr_qp
);
1155 free_qp_buf(hr_dev
, hr_qp
);
1157 free_qp_db(hr_dev
, hr_qp
, udata
);
1159 free_kernel_wrid(hr_dev
, hr_qp
);
1163 void hns_roce_qp_destroy(struct hns_roce_dev
*hr_dev
, struct hns_roce_qp
*hr_qp
,
1164 struct ib_udata
*udata
)
1166 if (atomic_dec_and_test(&hr_qp
->refcount
))
1167 complete(&hr_qp
->free
);
1168 wait_for_completion(&hr_qp
->free
);
1170 free_qpc(hr_dev
, hr_qp
);
1171 free_qpn(hr_dev
, hr_qp
);
1172 free_qp_buf(hr_dev
, hr_qp
);
1173 free_kernel_wrid(hr_dev
, hr_qp
);
1174 free_qp_db(hr_dev
, hr_qp
, udata
);
1179 struct ib_qp
*hns_roce_create_qp(struct ib_pd
*pd
,
1180 struct ib_qp_init_attr
*init_attr
,
1181 struct ib_udata
*udata
)
1183 struct hns_roce_dev
*hr_dev
= to_hr_dev(pd
->device
);
1184 struct ib_device
*ibdev
= &hr_dev
->ib_dev
;
1185 struct hns_roce_qp
*hr_qp
;
1188 switch (init_attr
->qp_type
) {
1190 hr_qp
= kzalloc(sizeof(*hr_qp
), GFP_KERNEL
);
1192 return ERR_PTR(-ENOMEM
);
1194 ret
= hns_roce_create_qp_common(hr_dev
, pd
, init_attr
, udata
,
1197 ibdev_err(ibdev
, "Create QP 0x%06lx failed(%d)\n",
1200 return ERR_PTR(ret
);
1206 /* Userspace is not allowed to create special QPs: */
1208 ibdev_err(ibdev
, "not support usr space GSI\n");
1209 return ERR_PTR(-EINVAL
);
1212 hr_qp
= kzalloc(sizeof(*hr_qp
), GFP_KERNEL
);
1214 return ERR_PTR(-ENOMEM
);
1216 hr_qp
->port
= init_attr
->port_num
- 1;
1217 hr_qp
->phy_port
= hr_dev
->iboe
.phy_port
[hr_qp
->port
];
1219 ret
= hns_roce_create_qp_common(hr_dev
, pd
, init_attr
, udata
,
1222 ibdev_err(ibdev
, "Create GSI QP failed!\n");
1224 return ERR_PTR(ret
);
1230 ibdev_err(ibdev
, "not support QP type %d\n",
1231 init_attr
->qp_type
);
1232 return ERR_PTR(-EOPNOTSUPP
);
1236 return &hr_qp
->ibqp
;
1239 int to_hr_qp_type(int qp_type
)
1243 if (qp_type
== IB_QPT_RC
)
1244 transport_type
= SERV_TYPE_RC
;
1245 else if (qp_type
== IB_QPT_UC
)
1246 transport_type
= SERV_TYPE_UC
;
1247 else if (qp_type
== IB_QPT_UD
)
1248 transport_type
= SERV_TYPE_UD
;
1249 else if (qp_type
== IB_QPT_GSI
)
1250 transport_type
= SERV_TYPE_UD
;
1252 transport_type
= -1;
1254 return transport_type
;
1257 static int check_mtu_validate(struct hns_roce_dev
*hr_dev
,
1258 struct hns_roce_qp
*hr_qp
,
1259 struct ib_qp_attr
*attr
, int attr_mask
)
1261 enum ib_mtu active_mtu
;
1264 p
= attr_mask
& IB_QP_PORT
? (attr
->port_num
- 1) : hr_qp
->port
;
1265 active_mtu
= iboe_get_mtu(hr_dev
->iboe
.netdevs
[p
]->mtu
);
1267 if ((hr_dev
->caps
.max_mtu
>= IB_MTU_2048
&&
1268 attr
->path_mtu
> hr_dev
->caps
.max_mtu
) ||
1269 attr
->path_mtu
< IB_MTU_256
|| attr
->path_mtu
> active_mtu
) {
1270 ibdev_err(&hr_dev
->ib_dev
,
1271 "attr path_mtu(%d)invalid while modify qp",
1279 static int hns_roce_check_qp_attr(struct ib_qp
*ibqp
, struct ib_qp_attr
*attr
,
1282 struct hns_roce_dev
*hr_dev
= to_hr_dev(ibqp
->device
);
1283 struct hns_roce_qp
*hr_qp
= to_hr_qp(ibqp
);
1286 if ((attr_mask
& IB_QP_PORT
) &&
1287 (attr
->port_num
== 0 || attr
->port_num
> hr_dev
->caps
.num_ports
)) {
1288 ibdev_err(&hr_dev
->ib_dev
,
1289 "attr port_num invalid.attr->port_num=%d\n",
1294 if (attr_mask
& IB_QP_PKEY_INDEX
) {
1295 p
= attr_mask
& IB_QP_PORT
? (attr
->port_num
- 1) : hr_qp
->port
;
1296 if (attr
->pkey_index
>= hr_dev
->caps
.pkey_table_len
[p
]) {
1297 ibdev_err(&hr_dev
->ib_dev
,
1298 "attr pkey_index invalid.attr->pkey_index=%d\n",
1304 if (attr_mask
& IB_QP_MAX_QP_RD_ATOMIC
&&
1305 attr
->max_rd_atomic
> hr_dev
->caps
.max_qp_init_rdma
) {
1306 ibdev_err(&hr_dev
->ib_dev
,
1307 "attr max_rd_atomic invalid.attr->max_rd_atomic=%d\n",
1308 attr
->max_rd_atomic
);
1312 if (attr_mask
& IB_QP_MAX_DEST_RD_ATOMIC
&&
1313 attr
->max_dest_rd_atomic
> hr_dev
->caps
.max_qp_dest_rdma
) {
1314 ibdev_err(&hr_dev
->ib_dev
,
1315 "attr max_dest_rd_atomic invalid.attr->max_dest_rd_atomic=%d\n",
1316 attr
->max_dest_rd_atomic
);
1320 if (attr_mask
& IB_QP_PATH_MTU
)
1321 return check_mtu_validate(hr_dev
, hr_qp
, attr
, attr_mask
);
1326 int hns_roce_modify_qp(struct ib_qp
*ibqp
, struct ib_qp_attr
*attr
,
1327 int attr_mask
, struct ib_udata
*udata
)
1329 struct hns_roce_dev
*hr_dev
= to_hr_dev(ibqp
->device
);
1330 struct hns_roce_qp
*hr_qp
= to_hr_qp(ibqp
);
1331 enum ib_qp_state cur_state
, new_state
;
1334 mutex_lock(&hr_qp
->mutex
);
1336 cur_state
= attr_mask
& IB_QP_CUR_STATE
?
1337 attr
->cur_qp_state
: (enum ib_qp_state
)hr_qp
->state
;
1338 new_state
= attr_mask
& IB_QP_STATE
? attr
->qp_state
: cur_state
;
1340 if (ibqp
->uobject
&&
1341 (attr_mask
& IB_QP_STATE
) && new_state
== IB_QPS_ERR
) {
1342 if (hr_qp
->sdb_en
== 1) {
1343 hr_qp
->sq
.head
= *(int *)(hr_qp
->sdb
.virt_addr
);
1345 if (hr_qp
->rdb_en
== 1)
1346 hr_qp
->rq
.head
= *(int *)(hr_qp
->rdb
.virt_addr
);
1348 ibdev_warn(&hr_dev
->ib_dev
,
1349 "flush cqe is not supported in userspace!\n");
1354 if (!ib_modify_qp_is_ok(cur_state
, new_state
, ibqp
->qp_type
,
1356 ibdev_err(&hr_dev
->ib_dev
, "ib_modify_qp_is_ok failed\n");
1360 ret
= hns_roce_check_qp_attr(ibqp
, attr
, attr_mask
);
1364 if (cur_state
== new_state
&& cur_state
== IB_QPS_RESET
) {
1365 if (hr_dev
->hw_rev
== HNS_ROCE_HW_VER1
) {
1367 ibdev_err(&hr_dev
->ib_dev
,
1368 "RST2RST state is not supported\n");
1376 ret
= hr_dev
->hw
->modify_qp(ibqp
, attr
, attr_mask
, cur_state
,
1380 mutex_unlock(&hr_qp
->mutex
);
1385 void hns_roce_lock_cqs(struct hns_roce_cq
*send_cq
, struct hns_roce_cq
*recv_cq
)
1386 __acquires(&send_cq
->lock
) __acquires(&recv_cq
->lock
)
1388 if (unlikely(send_cq
== NULL
&& recv_cq
== NULL
)) {
1389 __acquire(&send_cq
->lock
);
1390 __acquire(&recv_cq
->lock
);
1391 } else if (unlikely(send_cq
!= NULL
&& recv_cq
== NULL
)) {
1392 spin_lock_irq(&send_cq
->lock
);
1393 __acquire(&recv_cq
->lock
);
1394 } else if (unlikely(send_cq
== NULL
&& recv_cq
!= NULL
)) {
1395 spin_lock_irq(&recv_cq
->lock
);
1396 __acquire(&send_cq
->lock
);
1397 } else if (send_cq
== recv_cq
) {
1398 spin_lock_irq(&send_cq
->lock
);
1399 __acquire(&recv_cq
->lock
);
1400 } else if (send_cq
->cqn
< recv_cq
->cqn
) {
1401 spin_lock_irq(&send_cq
->lock
);
1402 spin_lock_nested(&recv_cq
->lock
, SINGLE_DEPTH_NESTING
);
1404 spin_lock_irq(&recv_cq
->lock
);
1405 spin_lock_nested(&send_cq
->lock
, SINGLE_DEPTH_NESTING
);
1409 void hns_roce_unlock_cqs(struct hns_roce_cq
*send_cq
,
1410 struct hns_roce_cq
*recv_cq
) __releases(&send_cq
->lock
)
1411 __releases(&recv_cq
->lock
)
1413 if (unlikely(send_cq
== NULL
&& recv_cq
== NULL
)) {
1414 __release(&recv_cq
->lock
);
1415 __release(&send_cq
->lock
);
1416 } else if (unlikely(send_cq
!= NULL
&& recv_cq
== NULL
)) {
1417 __release(&recv_cq
->lock
);
1418 spin_unlock(&send_cq
->lock
);
1419 } else if (unlikely(send_cq
== NULL
&& recv_cq
!= NULL
)) {
1420 __release(&send_cq
->lock
);
1421 spin_unlock(&recv_cq
->lock
);
1422 } else if (send_cq
== recv_cq
) {
1423 __release(&recv_cq
->lock
);
1424 spin_unlock_irq(&send_cq
->lock
);
1425 } else if (send_cq
->cqn
< recv_cq
->cqn
) {
1426 spin_unlock(&recv_cq
->lock
);
1427 spin_unlock_irq(&send_cq
->lock
);
1429 spin_unlock(&send_cq
->lock
);
1430 spin_unlock_irq(&recv_cq
->lock
);
1434 static void *get_wqe(struct hns_roce_qp
*hr_qp
, int offset
)
1437 return hns_roce_buf_offset(&hr_qp
->hr_buf
, offset
);
1440 void *hns_roce_get_recv_wqe(struct hns_roce_qp
*hr_qp
, int n
)
1442 return get_wqe(hr_qp
, hr_qp
->rq
.offset
+ (n
<< hr_qp
->rq
.wqe_shift
));
1445 void *hns_roce_get_send_wqe(struct hns_roce_qp
*hr_qp
, int n
)
1447 return get_wqe(hr_qp
, hr_qp
->sq
.offset
+ (n
<< hr_qp
->sq
.wqe_shift
));
1450 void *hns_roce_get_extend_sge(struct hns_roce_qp
*hr_qp
, int n
)
1452 return hns_roce_buf_offset(&hr_qp
->hr_buf
, hr_qp
->sge
.offset
+
1453 (n
<< hr_qp
->sge
.sge_shift
));
1456 bool hns_roce_wq_overflow(struct hns_roce_wq
*hr_wq
, int nreq
,
1457 struct ib_cq
*ib_cq
)
1459 struct hns_roce_cq
*hr_cq
;
1462 cur
= hr_wq
->head
- hr_wq
->tail
;
1463 if (likely(cur
+ nreq
< hr_wq
->wqe_cnt
))
1466 hr_cq
= to_hr_cq(ib_cq
);
1467 spin_lock(&hr_cq
->lock
);
1468 cur
= hr_wq
->head
- hr_wq
->tail
;
1469 spin_unlock(&hr_cq
->lock
);
1471 return cur
+ nreq
>= hr_wq
->wqe_cnt
;
1474 int hns_roce_init_qp_table(struct hns_roce_dev
*hr_dev
)
1476 struct hns_roce_qp_table
*qp_table
= &hr_dev
->qp_table
;
1477 int reserved_from_top
= 0;
1478 int reserved_from_bot
;
1481 mutex_init(&qp_table
->scc_mutex
);
1482 xa_init(&hr_dev
->qp_table_xa
);
1484 reserved_from_bot
= hr_dev
->caps
.reserved_qps
;
1486 ret
= hns_roce_bitmap_init(&qp_table
->bitmap
, hr_dev
->caps
.num_qps
,
1487 hr_dev
->caps
.num_qps
- 1, reserved_from_bot
,
1490 dev_err(hr_dev
->dev
, "qp bitmap init failed!error=%d\n",
1498 void hns_roce_cleanup_qp_table(struct hns_roce_dev
*hr_dev
)
1500 hns_roce_bitmap_cleanup(&hr_dev
->qp_table
.bitmap
);