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 void hns_roce_qp_event(struct hns_roce_dev *hr_dev, u32 qpn, int event_type)
48 struct device *dev = hr_dev->dev;
49 struct hns_roce_qp *qp;
51 xa_lock(&hr_dev->qp_table_xa);
52 qp = __hns_roce_qp_lookup(hr_dev, qpn);
54 atomic_inc(&qp->refcount);
55 xa_unlock(&hr_dev->qp_table_xa);
58 dev_warn(dev, "Async event for bogus QP %08x\n", qpn);
62 qp->event(qp, (enum hns_roce_event)event_type);
64 if (atomic_dec_and_test(&qp->refcount))
67 EXPORT_SYMBOL_GPL(hns_roce_qp_event);
69 static void hns_roce_ib_qp_event(struct hns_roce_qp *hr_qp,
70 enum hns_roce_event type)
72 struct ib_event event;
73 struct ib_qp *ibqp = &hr_qp->ibqp;
75 if (ibqp->event_handler) {
76 event.device = ibqp->device;
77 event.element.qp = ibqp;
79 case HNS_ROCE_EVENT_TYPE_PATH_MIG:
80 event.event = IB_EVENT_PATH_MIG;
82 case HNS_ROCE_EVENT_TYPE_COMM_EST:
83 event.event = IB_EVENT_COMM_EST;
85 case HNS_ROCE_EVENT_TYPE_SQ_DRAINED:
86 event.event = IB_EVENT_SQ_DRAINED;
88 case HNS_ROCE_EVENT_TYPE_SRQ_LAST_WQE_REACH:
89 event.event = IB_EVENT_QP_LAST_WQE_REACHED;
91 case HNS_ROCE_EVENT_TYPE_WQ_CATAS_ERROR:
92 event.event = IB_EVENT_QP_FATAL;
94 case HNS_ROCE_EVENT_TYPE_PATH_MIG_FAILED:
95 event.event = IB_EVENT_PATH_MIG_ERR;
97 case HNS_ROCE_EVENT_TYPE_INV_REQ_LOCAL_WQ_ERROR:
98 event.event = IB_EVENT_QP_REQ_ERR;
100 case HNS_ROCE_EVENT_TYPE_LOCAL_WQ_ACCESS_ERROR:
101 event.event = IB_EVENT_QP_ACCESS_ERR;
104 dev_dbg(ibqp->device->dev.parent, "roce_ib: Unexpected event type %d on QP %06lx\n",
108 ibqp->event_handler(&event, ibqp->qp_context);
112 static int hns_roce_reserve_range_qp(struct hns_roce_dev *hr_dev, int cnt,
113 int align, unsigned long *base)
115 struct hns_roce_qp_table *qp_table = &hr_dev->qp_table;
117 return hns_roce_bitmap_alloc_range(&qp_table->bitmap, cnt, align,
123 enum hns_roce_qp_state to_hns_roce_state(enum ib_qp_state state)
127 return HNS_ROCE_QP_STATE_RST;
129 return HNS_ROCE_QP_STATE_INIT;
131 return HNS_ROCE_QP_STATE_RTR;
133 return HNS_ROCE_QP_STATE_RTS;
135 return HNS_ROCE_QP_STATE_SQD;
137 return HNS_ROCE_QP_STATE_ERR;
139 return HNS_ROCE_QP_NUM_STATE;
142 EXPORT_SYMBOL_GPL(to_hns_roce_state);
144 static int hns_roce_gsi_qp_alloc(struct hns_roce_dev *hr_dev, unsigned long qpn,
145 struct hns_roce_qp *hr_qp)
147 struct xarray *xa = &hr_dev->qp_table_xa;
154 atomic_set(&hr_qp->refcount, 1);
155 init_completion(&hr_qp->free);
157 ret = xa_err(xa_store_irq(xa, hr_qp->qpn & (hr_dev->caps.num_qps - 1),
160 dev_err(hr_dev->dev, "QPC xa_store failed\n");
165 static int hns_roce_qp_alloc(struct hns_roce_dev *hr_dev, unsigned long qpn,
166 struct hns_roce_qp *hr_qp)
168 struct hns_roce_qp_table *qp_table = &hr_dev->qp_table;
169 struct device *dev = hr_dev->dev;
177 /* Alloc memory for QPC */
178 ret = hns_roce_table_get(hr_dev, &qp_table->qp_table, hr_qp->qpn);
180 dev_err(dev, "QPC table get failed\n");
184 /* Alloc memory for IRRL */
185 ret = hns_roce_table_get(hr_dev, &qp_table->irrl_table, hr_qp->qpn);
187 dev_err(dev, "IRRL table get failed\n");
191 if (hr_dev->caps.trrl_entry_sz) {
192 /* Alloc memory for TRRL */
193 ret = hns_roce_table_get(hr_dev, &qp_table->trrl_table,
196 dev_err(dev, "TRRL table get failed\n");
201 if (hr_dev->caps.sccc_entry_sz) {
202 /* Alloc memory for SCC CTX */
203 ret = hns_roce_table_get(hr_dev, &qp_table->sccc_table,
206 dev_err(dev, "SCC CTX table get failed\n");
211 ret = hns_roce_gsi_qp_alloc(hr_dev, qpn, hr_qp);
218 if (hr_dev->caps.sccc_entry_sz)
219 hns_roce_table_put(hr_dev, &qp_table->sccc_table,
223 if (hr_dev->caps.trrl_entry_sz)
224 hns_roce_table_put(hr_dev, &qp_table->trrl_table, hr_qp->qpn);
227 hns_roce_table_put(hr_dev, &qp_table->irrl_table, hr_qp->qpn);
230 hns_roce_table_put(hr_dev, &qp_table->qp_table, hr_qp->qpn);
236 void hns_roce_qp_remove(struct hns_roce_dev *hr_dev, struct hns_roce_qp *hr_qp)
238 struct xarray *xa = &hr_dev->qp_table_xa;
241 xa_lock_irqsave(xa, flags);
242 __xa_erase(xa, hr_qp->qpn & (hr_dev->caps.num_qps - 1));
243 xa_unlock_irqrestore(xa, flags);
245 EXPORT_SYMBOL_GPL(hns_roce_qp_remove);
247 void hns_roce_qp_free(struct hns_roce_dev *hr_dev, struct hns_roce_qp *hr_qp)
249 struct hns_roce_qp_table *qp_table = &hr_dev->qp_table;
251 if (atomic_dec_and_test(&hr_qp->refcount))
252 complete(&hr_qp->free);
253 wait_for_completion(&hr_qp->free);
255 if ((hr_qp->ibqp.qp_type) != IB_QPT_GSI) {
256 if (hr_dev->caps.trrl_entry_sz)
257 hns_roce_table_put(hr_dev, &qp_table->trrl_table,
259 hns_roce_table_put(hr_dev, &qp_table->irrl_table, hr_qp->qpn);
260 hns_roce_table_put(hr_dev, &qp_table->qp_table, hr_qp->qpn);
263 EXPORT_SYMBOL_GPL(hns_roce_qp_free);
265 void hns_roce_release_range_qp(struct hns_roce_dev *hr_dev, int base_qpn,
268 struct hns_roce_qp_table *qp_table = &hr_dev->qp_table;
270 if (base_qpn < SQP_NUM)
273 hns_roce_bitmap_free_range(&qp_table->bitmap, base_qpn, cnt, BITMAP_RR);
275 EXPORT_SYMBOL_GPL(hns_roce_release_range_qp);
277 static int hns_roce_set_rq_size(struct hns_roce_dev *hr_dev,
278 struct ib_qp_cap *cap, bool is_user, int has_rq,
279 struct hns_roce_qp *hr_qp)
281 struct device *dev = hr_dev->dev;
284 /* Check the validity of QP support capacity */
285 if (cap->max_recv_wr > hr_dev->caps.max_wqes ||
286 cap->max_recv_sge > hr_dev->caps.max_rq_sg) {
287 dev_err(dev, "RQ WR or sge error!max_recv_wr=%d max_recv_sge=%d\n",
288 cap->max_recv_wr, cap->max_recv_sge);
292 /* If srq exist, set zero for relative number of rq */
294 hr_qp->rq.wqe_cnt = 0;
295 hr_qp->rq.max_gs = 0;
296 cap->max_recv_wr = 0;
297 cap->max_recv_sge = 0;
299 if (is_user && (!cap->max_recv_wr || !cap->max_recv_sge)) {
300 dev_err(dev, "user space no need config max_recv_wr max_recv_sge\n");
304 if (hr_dev->caps.min_wqes)
305 max_cnt = max(cap->max_recv_wr, hr_dev->caps.min_wqes);
307 max_cnt = cap->max_recv_wr;
309 hr_qp->rq.wqe_cnt = roundup_pow_of_two(max_cnt);
311 if ((u32)hr_qp->rq.wqe_cnt > hr_dev->caps.max_wqes) {
312 dev_err(dev, "while setting rq size, rq.wqe_cnt too large\n");
316 max_cnt = max(1U, cap->max_recv_sge);
317 hr_qp->rq.max_gs = roundup_pow_of_two(max_cnt);
318 if (hr_dev->caps.max_rq_sg <= 2)
319 hr_qp->rq.wqe_shift =
320 ilog2(hr_dev->caps.max_rq_desc_sz);
322 hr_qp->rq.wqe_shift =
323 ilog2(hr_dev->caps.max_rq_desc_sz
327 cap->max_recv_wr = hr_qp->rq.max_post = hr_qp->rq.wqe_cnt;
328 cap->max_recv_sge = hr_qp->rq.max_gs;
333 static int hns_roce_set_user_sq_size(struct hns_roce_dev *hr_dev,
334 struct ib_qp_cap *cap,
335 struct hns_roce_qp *hr_qp,
336 struct hns_roce_ib_create_qp *ucmd)
338 u32 roundup_sq_stride = roundup_pow_of_two(hr_dev->caps.max_sq_desc_sz);
339 u8 max_sq_stride = ilog2(roundup_sq_stride);
344 /* Sanity check SQ size before proceeding */
345 if ((u32)(1 << ucmd->log_sq_bb_count) > hr_dev->caps.max_wqes ||
346 ucmd->log_sq_stride > max_sq_stride ||
347 ucmd->log_sq_stride < HNS_ROCE_IB_MIN_SQ_STRIDE) {
348 dev_err(hr_dev->dev, "check SQ size error!\n");
352 if (cap->max_send_sge > hr_dev->caps.max_sq_sg) {
353 dev_err(hr_dev->dev, "SQ sge error! max_send_sge=%d\n",
358 hr_qp->sq.wqe_cnt = 1 << ucmd->log_sq_bb_count;
359 hr_qp->sq.wqe_shift = ucmd->log_sq_stride;
361 max_cnt = max(1U, cap->max_send_sge);
362 if (hr_dev->caps.max_sq_sg <= 2)
363 hr_qp->sq.max_gs = roundup_pow_of_two(max_cnt);
365 hr_qp->sq.max_gs = max_cnt;
367 if (hr_qp->sq.max_gs > 2)
368 hr_qp->sge.sge_cnt = roundup_pow_of_two(hr_qp->sq.wqe_cnt *
369 (hr_qp->sq.max_gs - 2));
371 if ((hr_qp->sq.max_gs > 2) && (hr_dev->pci_dev->revision == 0x20)) {
372 if (hr_qp->sge.sge_cnt > hr_dev->caps.max_extend_sg) {
374 "The extended sge cnt error! sge_cnt=%d\n",
380 hr_qp->sge.sge_shift = 4;
381 ex_sge_num = hr_qp->sge.sge_cnt;
383 /* Get buf size, SQ and RQ are aligned to page_szie */
384 if (hr_dev->caps.max_sq_sg <= 2) {
385 hr_qp->buff_size = HNS_ROCE_ALOGN_UP((hr_qp->rq.wqe_cnt <<
386 hr_qp->rq.wqe_shift), PAGE_SIZE) +
387 HNS_ROCE_ALOGN_UP((hr_qp->sq.wqe_cnt <<
388 hr_qp->sq.wqe_shift), PAGE_SIZE);
390 hr_qp->sq.offset = 0;
391 hr_qp->rq.offset = HNS_ROCE_ALOGN_UP((hr_qp->sq.wqe_cnt <<
392 hr_qp->sq.wqe_shift), PAGE_SIZE);
394 page_size = 1 << (hr_dev->caps.mtt_buf_pg_sz + PAGE_SHIFT);
396 max(page_size / (1 << hr_qp->sge.sge_shift), ex_sge_num);
397 hr_qp->buff_size = HNS_ROCE_ALOGN_UP((hr_qp->rq.wqe_cnt <<
398 hr_qp->rq.wqe_shift), page_size) +
399 HNS_ROCE_ALOGN_UP((hr_qp->sge.sge_cnt <<
400 hr_qp->sge.sge_shift), page_size) +
401 HNS_ROCE_ALOGN_UP((hr_qp->sq.wqe_cnt <<
402 hr_qp->sq.wqe_shift), page_size);
404 hr_qp->sq.offset = 0;
406 hr_qp->sge.offset = HNS_ROCE_ALOGN_UP(
407 (hr_qp->sq.wqe_cnt <<
408 hr_qp->sq.wqe_shift),
410 hr_qp->rq.offset = hr_qp->sge.offset +
411 HNS_ROCE_ALOGN_UP((hr_qp->sge.sge_cnt <<
412 hr_qp->sge.sge_shift),
415 hr_qp->rq.offset = HNS_ROCE_ALOGN_UP(
416 (hr_qp->sq.wqe_cnt <<
417 hr_qp->sq.wqe_shift),
425 static int hns_roce_set_kernel_sq_size(struct hns_roce_dev *hr_dev,
426 struct ib_qp_cap *cap,
427 struct hns_roce_qp *hr_qp)
429 struct device *dev = hr_dev->dev;
434 if (cap->max_send_wr > hr_dev->caps.max_wqes ||
435 cap->max_send_sge > hr_dev->caps.max_sq_sg ||
436 cap->max_inline_data > hr_dev->caps.max_sq_inline) {
437 dev_err(dev, "SQ WR or sge or inline data error!\n");
441 hr_qp->sq.wqe_shift = ilog2(hr_dev->caps.max_sq_desc_sz);
442 hr_qp->sq_max_wqes_per_wr = 1;
443 hr_qp->sq_spare_wqes = 0;
445 if (hr_dev->caps.min_wqes)
446 max_cnt = max(cap->max_send_wr, hr_dev->caps.min_wqes);
448 max_cnt = cap->max_send_wr;
450 hr_qp->sq.wqe_cnt = roundup_pow_of_two(max_cnt);
451 if ((u32)hr_qp->sq.wqe_cnt > hr_dev->caps.max_wqes) {
452 dev_err(dev, "while setting kernel sq size, sq.wqe_cnt too large\n");
456 /* Get data_seg numbers */
457 max_cnt = max(1U, cap->max_send_sge);
458 if (hr_dev->caps.max_sq_sg <= 2)
459 hr_qp->sq.max_gs = roundup_pow_of_two(max_cnt);
461 hr_qp->sq.max_gs = max_cnt;
463 if (hr_qp->sq.max_gs > 2) {
464 hr_qp->sge.sge_cnt = roundup_pow_of_two(hr_qp->sq.wqe_cnt *
465 (hr_qp->sq.max_gs - 2));
466 hr_qp->sge.sge_shift = 4;
469 /* ud sqwqe's sge use extend sge */
470 if (hr_dev->caps.max_sq_sg > 2 && hr_qp->ibqp.qp_type == IB_QPT_GSI) {
471 hr_qp->sge.sge_cnt = roundup_pow_of_two(hr_qp->sq.wqe_cnt *
473 hr_qp->sge.sge_shift = 4;
476 if ((hr_qp->sq.max_gs > 2) && hr_dev->pci_dev->revision == 0x20) {
477 if (hr_qp->sge.sge_cnt > hr_dev->caps.max_extend_sg) {
478 dev_err(dev, "The extended sge cnt error! sge_cnt=%d\n",
484 /* Get buf size, SQ and RQ are aligned to PAGE_SIZE */
485 page_size = 1 << (hr_dev->caps.mtt_buf_pg_sz + PAGE_SHIFT);
486 hr_qp->sq.offset = 0;
487 size = HNS_ROCE_ALOGN_UP(hr_qp->sq.wqe_cnt << hr_qp->sq.wqe_shift,
490 if (hr_dev->caps.max_sq_sg > 2 && hr_qp->sge.sge_cnt) {
491 hr_qp->sge.sge_cnt = max(page_size/(1 << hr_qp->sge.sge_shift),
492 (u32)hr_qp->sge.sge_cnt);
493 hr_qp->sge.offset = size;
494 size += HNS_ROCE_ALOGN_UP(hr_qp->sge.sge_cnt <<
495 hr_qp->sge.sge_shift, page_size);
498 hr_qp->rq.offset = size;
499 size += HNS_ROCE_ALOGN_UP((hr_qp->rq.wqe_cnt << hr_qp->rq.wqe_shift),
501 hr_qp->buff_size = size;
503 /* Get wr and sge number which send */
504 cap->max_send_wr = hr_qp->sq.max_post = hr_qp->sq.wqe_cnt;
505 cap->max_send_sge = hr_qp->sq.max_gs;
507 /* We don't support inline sends for kernel QPs (yet) */
508 cap->max_inline_data = 0;
513 static int hns_roce_qp_has_sq(struct ib_qp_init_attr *attr)
515 if (attr->qp_type == IB_QPT_XRC_TGT || !attr->cap.max_send_wr)
521 static int hns_roce_qp_has_rq(struct ib_qp_init_attr *attr)
523 if (attr->qp_type == IB_QPT_XRC_INI ||
524 attr->qp_type == IB_QPT_XRC_TGT || attr->srq ||
525 !attr->cap.max_recv_wr)
531 static int hns_roce_create_qp_common(struct hns_roce_dev *hr_dev,
533 struct ib_qp_init_attr *init_attr,
534 struct ib_udata *udata, unsigned long sqpn,
535 struct hns_roce_qp *hr_qp)
537 struct device *dev = hr_dev->dev;
538 struct hns_roce_ib_create_qp ucmd;
539 struct hns_roce_ib_create_qp_resp resp = {};
540 struct hns_roce_ucontext *uctx = rdma_udata_to_drv_context(
541 udata, struct hns_roce_ucontext, ibucontext);
542 unsigned long qpn = 0;
548 mutex_init(&hr_qp->mutex);
549 spin_lock_init(&hr_qp->sq.lock);
550 spin_lock_init(&hr_qp->rq.lock);
552 hr_qp->state = IB_QPS_RESET;
554 hr_qp->ibqp.qp_type = init_attr->qp_type;
556 if (init_attr->sq_sig_type == IB_SIGNAL_ALL_WR)
557 hr_qp->sq_signal_bits = cpu_to_le32(IB_SIGNAL_ALL_WR);
559 hr_qp->sq_signal_bits = cpu_to_le32(IB_SIGNAL_REQ_WR);
561 ret = hns_roce_set_rq_size(hr_dev, &init_attr->cap, udata,
562 hns_roce_qp_has_rq(init_attr), hr_qp);
564 dev_err(dev, "hns_roce_set_rq_size failed\n");
568 if ((hr_dev->caps.flags & HNS_ROCE_CAP_FLAG_RQ_INLINE) &&
569 hns_roce_qp_has_rq(init_attr)) {
570 /* allocate recv inline buf */
571 hr_qp->rq_inl_buf.wqe_list = kcalloc(hr_qp->rq.wqe_cnt,
572 sizeof(struct hns_roce_rinl_wqe),
574 if (!hr_qp->rq_inl_buf.wqe_list) {
579 hr_qp->rq_inl_buf.wqe_cnt = hr_qp->rq.wqe_cnt;
581 /* Firstly, allocate a list of sge space buffer */
582 hr_qp->rq_inl_buf.wqe_list[0].sg_list =
583 kcalloc(hr_qp->rq_inl_buf.wqe_cnt,
584 init_attr->cap.max_recv_sge *
585 sizeof(struct hns_roce_rinl_sge),
587 if (!hr_qp->rq_inl_buf.wqe_list[0].sg_list) {
592 for (i = 1; i < hr_qp->rq_inl_buf.wqe_cnt; i++)
593 /* Secondly, reallocate the buffer */
594 hr_qp->rq_inl_buf.wqe_list[i].sg_list =
595 &hr_qp->rq_inl_buf.wqe_list[0].sg_list[i *
596 init_attr->cap.max_recv_sge];
600 if (ib_copy_from_udata(&ucmd, udata, sizeof(ucmd))) {
601 dev_err(dev, "ib_copy_from_udata error for create qp\n");
603 goto err_rq_sge_list;
606 ret = hns_roce_set_user_sq_size(hr_dev, &init_attr->cap, hr_qp,
609 dev_err(dev, "hns_roce_set_user_sq_size error for create qp\n");
610 goto err_rq_sge_list;
613 hr_qp->umem = ib_umem_get(udata, ucmd.buf_addr,
614 hr_qp->buff_size, 0, 0);
615 if (IS_ERR(hr_qp->umem)) {
616 dev_err(dev, "ib_umem_get error for create qp\n");
617 ret = PTR_ERR(hr_qp->umem);
618 goto err_rq_sge_list;
621 hr_qp->mtt.mtt_type = MTT_TYPE_WQE;
622 page_shift = PAGE_SHIFT;
623 if (hr_dev->caps.mtt_buf_pg_sz) {
624 npages = (ib_umem_page_count(hr_qp->umem) +
625 (1 << hr_dev->caps.mtt_buf_pg_sz) - 1) /
626 (1 << hr_dev->caps.mtt_buf_pg_sz);
627 page_shift += hr_dev->caps.mtt_buf_pg_sz;
628 ret = hns_roce_mtt_init(hr_dev, npages,
632 ret = hns_roce_mtt_init(hr_dev,
633 ib_umem_page_count(hr_qp->umem),
634 page_shift, &hr_qp->mtt);
637 dev_err(dev, "hns_roce_mtt_init error for create qp\n");
641 ret = hns_roce_ib_umem_write_mtt(hr_dev, &hr_qp->mtt,
644 dev_err(dev, "hns_roce_ib_umem_write_mtt error for create qp\n");
648 if ((hr_dev->caps.flags & HNS_ROCE_CAP_FLAG_SQ_RECORD_DB) &&
649 (udata->inlen >= sizeof(ucmd)) &&
650 (udata->outlen >= sizeof(resp)) &&
651 hns_roce_qp_has_sq(init_attr)) {
652 ret = hns_roce_db_map_user(uctx, udata, ucmd.sdb_addr,
655 dev_err(dev, "sq record doorbell map failed!\n");
659 /* indicate kernel supports sq record db */
660 resp.cap_flags |= HNS_ROCE_SUPPORT_SQ_RECORD_DB;
664 if ((hr_dev->caps.flags & HNS_ROCE_CAP_FLAG_RECORD_DB) &&
665 (udata->outlen >= sizeof(resp)) &&
666 hns_roce_qp_has_rq(init_attr)) {
667 ret = hns_roce_db_map_user(uctx, udata, ucmd.db_addr,
670 dev_err(dev, "rq record doorbell map failed!\n");
674 /* indicate kernel supports rq record db */
675 resp.cap_flags |= HNS_ROCE_SUPPORT_RQ_RECORD_DB;
679 if (init_attr->create_flags &
680 IB_QP_CREATE_BLOCK_MULTICAST_LOOPBACK) {
681 dev_err(dev, "init_attr->create_flags error!\n");
683 goto err_rq_sge_list;
686 if (init_attr->create_flags & IB_QP_CREATE_IPOIB_UD_LSO) {
687 dev_err(dev, "init_attr->create_flags error!\n");
689 goto err_rq_sge_list;
693 ret = hns_roce_set_kernel_sq_size(hr_dev, &init_attr->cap,
696 dev_err(dev, "hns_roce_set_kernel_sq_size error!\n");
697 goto err_rq_sge_list;
700 /* QP doorbell register address */
701 hr_qp->sq.db_reg_l = hr_dev->reg_base + hr_dev->sdb_offset +
702 DB_REG_OFFSET * hr_dev->priv_uar.index;
703 hr_qp->rq.db_reg_l = hr_dev->reg_base + hr_dev->odb_offset +
704 DB_REG_OFFSET * hr_dev->priv_uar.index;
706 if ((hr_dev->caps.flags & HNS_ROCE_CAP_FLAG_RECORD_DB) &&
707 hns_roce_qp_has_rq(init_attr)) {
708 ret = hns_roce_alloc_db(hr_dev, &hr_qp->rdb, 0);
710 dev_err(dev, "rq record doorbell alloc failed!\n");
711 goto err_rq_sge_list;
713 *hr_qp->rdb.db_record = 0;
717 /* Allocate QP buf */
718 page_shift = PAGE_SHIFT + hr_dev->caps.mtt_buf_pg_sz;
719 if (hns_roce_buf_alloc(hr_dev, hr_qp->buff_size,
720 (1 << page_shift) * 2,
721 &hr_qp->hr_buf, page_shift)) {
722 dev_err(dev, "hns_roce_buf_alloc error!\n");
727 hr_qp->mtt.mtt_type = MTT_TYPE_WQE;
729 ret = hns_roce_mtt_init(hr_dev, hr_qp->hr_buf.npages,
730 hr_qp->hr_buf.page_shift, &hr_qp->mtt);
732 dev_err(dev, "hns_roce_mtt_init error for kernel create qp\n");
736 ret = hns_roce_buf_write_mtt(hr_dev, &hr_qp->mtt,
739 dev_err(dev, "hns_roce_buf_write_mtt error for kernel create qp\n");
743 hr_qp->sq.wrid = kcalloc(hr_qp->sq.wqe_cnt, sizeof(u64),
745 hr_qp->rq.wrid = kcalloc(hr_qp->rq.wqe_cnt, sizeof(u64),
747 if (!hr_qp->sq.wrid || !hr_qp->rq.wrid) {
757 ret = hns_roce_reserve_range_qp(hr_dev, 1, 1, &qpn);
759 dev_err(dev, "hns_roce_reserve_range_qp alloc qpn error\n");
764 if (init_attr->qp_type == IB_QPT_GSI &&
765 hr_dev->hw_rev == HNS_ROCE_HW_VER1) {
766 /* In v1 engine, GSI QP context in RoCE engine's register */
767 ret = hns_roce_gsi_qp_alloc(hr_dev, qpn, hr_qp);
769 dev_err(dev, "hns_roce_qp_alloc failed!\n");
773 ret = hns_roce_qp_alloc(hr_dev, qpn, hr_qp);
775 dev_err(dev, "hns_roce_qp_alloc failed!\n");
781 hr_qp->doorbell_qpn = 1;
783 hr_qp->doorbell_qpn = cpu_to_le64(hr_qp->qpn);
786 ret = ib_copy_to_udata(udata, &resp,
787 min(udata->outlen, sizeof(resp)));
792 if (hr_dev->caps.flags & HNS_ROCE_CAP_FLAG_QP_FLOW_CTRL) {
793 ret = hr_dev->hw->qp_flow_control_init(hr_dev, hr_qp);
798 hr_qp->event = hns_roce_ib_qp_event;
803 if (init_attr->qp_type == IB_QPT_GSI &&
804 hr_dev->hw_rev == HNS_ROCE_HW_VER1)
805 hns_roce_qp_remove(hr_dev, hr_qp);
807 hns_roce_qp_free(hr_dev, hr_qp);
811 hns_roce_release_range_qp(hr_dev, qpn, 1);
815 if ((hr_dev->caps.flags & HNS_ROCE_CAP_FLAG_RECORD_DB) &&
816 (udata->outlen >= sizeof(resp)) &&
817 hns_roce_qp_has_rq(init_attr))
818 hns_roce_db_unmap_user(uctx, &hr_qp->rdb);
820 kfree(hr_qp->sq.wrid);
821 kfree(hr_qp->rq.wrid);
826 if ((hr_dev->caps.flags & HNS_ROCE_CAP_FLAG_SQ_RECORD_DB) &&
827 (udata->inlen >= sizeof(ucmd)) &&
828 (udata->outlen >= sizeof(resp)) &&
829 hns_roce_qp_has_sq(init_attr))
830 hns_roce_db_unmap_user(uctx, &hr_qp->sdb);
833 hns_roce_mtt_cleanup(hr_dev, &hr_qp->mtt);
837 ib_umem_release(hr_qp->umem);
839 hns_roce_buf_free(hr_dev, hr_qp->buff_size, &hr_qp->hr_buf);
842 if (!udata && hns_roce_qp_has_rq(init_attr) &&
843 (hr_dev->caps.flags & HNS_ROCE_CAP_FLAG_RECORD_DB))
844 hns_roce_free_db(hr_dev, &hr_qp->rdb);
847 if (hr_dev->caps.flags & HNS_ROCE_CAP_FLAG_RQ_INLINE)
848 kfree(hr_qp->rq_inl_buf.wqe_list[0].sg_list);
851 if (hr_dev->caps.flags & HNS_ROCE_CAP_FLAG_RQ_INLINE)
852 kfree(hr_qp->rq_inl_buf.wqe_list);
858 struct ib_qp *hns_roce_create_qp(struct ib_pd *pd,
859 struct ib_qp_init_attr *init_attr,
860 struct ib_udata *udata)
862 struct hns_roce_dev *hr_dev = to_hr_dev(pd->device);
863 struct device *dev = hr_dev->dev;
864 struct hns_roce_sqp *hr_sqp;
865 struct hns_roce_qp *hr_qp;
868 switch (init_attr->qp_type) {
870 hr_qp = kzalloc(sizeof(*hr_qp), GFP_KERNEL);
872 return ERR_PTR(-ENOMEM);
874 ret = hns_roce_create_qp_common(hr_dev, pd, init_attr, udata, 0,
877 dev_err(dev, "Create RC QP failed\n");
882 hr_qp->ibqp.qp_num = hr_qp->qpn;
887 /* Userspace is not allowed to create special QPs: */
889 dev_err(dev, "not support usr space GSI\n");
890 return ERR_PTR(-EINVAL);
893 hr_sqp = kzalloc(sizeof(*hr_sqp), GFP_KERNEL);
895 return ERR_PTR(-ENOMEM);
897 hr_qp = &hr_sqp->hr_qp;
898 hr_qp->port = init_attr->port_num - 1;
899 hr_qp->phy_port = hr_dev->iboe.phy_port[hr_qp->port];
901 /* when hw version is v1, the sqpn is allocated */
902 if (hr_dev->caps.max_sq_sg <= 2)
903 hr_qp->ibqp.qp_num = HNS_ROCE_MAX_PORTS +
904 hr_dev->iboe.phy_port[hr_qp->port];
906 hr_qp->ibqp.qp_num = 1;
908 ret = hns_roce_create_qp_common(hr_dev, pd, init_attr, udata,
909 hr_qp->ibqp.qp_num, hr_qp);
911 dev_err(dev, "Create GSI QP failed!\n");
919 dev_err(dev, "not support QP type %d\n", init_attr->qp_type);
920 return ERR_PTR(-EINVAL);
926 EXPORT_SYMBOL_GPL(hns_roce_create_qp);
928 int to_hr_qp_type(int qp_type)
932 if (qp_type == IB_QPT_RC)
933 transport_type = SERV_TYPE_RC;
934 else if (qp_type == IB_QPT_UC)
935 transport_type = SERV_TYPE_UC;
936 else if (qp_type == IB_QPT_UD)
937 transport_type = SERV_TYPE_UD;
938 else if (qp_type == IB_QPT_GSI)
939 transport_type = SERV_TYPE_UD;
943 return transport_type;
945 EXPORT_SYMBOL_GPL(to_hr_qp_type);
947 int hns_roce_modify_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
948 int attr_mask, struct ib_udata *udata)
950 struct hns_roce_dev *hr_dev = to_hr_dev(ibqp->device);
951 struct hns_roce_qp *hr_qp = to_hr_qp(ibqp);
952 enum ib_qp_state cur_state, new_state;
953 struct device *dev = hr_dev->dev;
956 enum ib_mtu active_mtu;
958 mutex_lock(&hr_qp->mutex);
960 cur_state = attr_mask & IB_QP_CUR_STATE ?
961 attr->cur_qp_state : (enum ib_qp_state)hr_qp->state;
962 new_state = attr_mask & IB_QP_STATE ?
963 attr->qp_state : cur_state;
966 (attr_mask & IB_QP_STATE) && new_state == IB_QPS_ERR) {
967 if (hr_qp->sdb_en == 1) {
968 hr_qp->sq.head = *(int *)(hr_qp->sdb.virt_addr);
970 if (hr_qp->rdb_en == 1)
971 hr_qp->rq.head = *(int *)(hr_qp->rdb.virt_addr);
973 dev_warn(dev, "flush cqe is not supported in userspace!\n");
978 if (!ib_modify_qp_is_ok(cur_state, new_state, ibqp->qp_type,
980 dev_err(dev, "ib_modify_qp_is_ok failed\n");
984 if ((attr_mask & IB_QP_PORT) &&
985 (attr->port_num == 0 || attr->port_num > hr_dev->caps.num_ports)) {
986 dev_err(dev, "attr port_num invalid.attr->port_num=%d\n",
991 if (attr_mask & IB_QP_PKEY_INDEX) {
992 p = attr_mask & IB_QP_PORT ? (attr->port_num - 1) : hr_qp->port;
993 if (attr->pkey_index >= hr_dev->caps.pkey_table_len[p]) {
994 dev_err(dev, "attr pkey_index invalid.attr->pkey_index=%d\n",
1000 if (attr_mask & IB_QP_PATH_MTU) {
1001 p = attr_mask & IB_QP_PORT ? (attr->port_num - 1) : hr_qp->port;
1002 active_mtu = iboe_get_mtu(hr_dev->iboe.netdevs[p]->mtu);
1004 if ((hr_dev->caps.max_mtu == IB_MTU_4096 &&
1005 attr->path_mtu > IB_MTU_4096) ||
1006 (hr_dev->caps.max_mtu == IB_MTU_2048 &&
1007 attr->path_mtu > IB_MTU_2048) ||
1008 attr->path_mtu < IB_MTU_256 ||
1009 attr->path_mtu > active_mtu) {
1010 dev_err(dev, "attr path_mtu(%d)invalid while modify qp",
1016 if (attr_mask & IB_QP_MAX_QP_RD_ATOMIC &&
1017 attr->max_rd_atomic > hr_dev->caps.max_qp_init_rdma) {
1018 dev_err(dev, "attr max_rd_atomic invalid.attr->max_rd_atomic=%d\n",
1019 attr->max_rd_atomic);
1023 if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC &&
1024 attr->max_dest_rd_atomic > hr_dev->caps.max_qp_dest_rdma) {
1025 dev_err(dev, "attr max_dest_rd_atomic invalid.attr->max_dest_rd_atomic=%d\n",
1026 attr->max_dest_rd_atomic);
1030 if (cur_state == new_state && cur_state == IB_QPS_RESET) {
1031 if (hr_dev->caps.min_wqes) {
1033 dev_err(dev, "cur_state=%d new_state=%d\n", cur_state,
1042 ret = hr_dev->hw->modify_qp(ibqp, attr, attr_mask, cur_state,
1046 mutex_unlock(&hr_qp->mutex);
1051 void hns_roce_lock_cqs(struct hns_roce_cq *send_cq, struct hns_roce_cq *recv_cq)
1052 __acquires(&send_cq->lock) __acquires(&recv_cq->lock)
1054 if (send_cq == recv_cq) {
1055 spin_lock_irq(&send_cq->lock);
1056 __acquire(&recv_cq->lock);
1057 } else if (send_cq->cqn < recv_cq->cqn) {
1058 spin_lock_irq(&send_cq->lock);
1059 spin_lock_nested(&recv_cq->lock, SINGLE_DEPTH_NESTING);
1061 spin_lock_irq(&recv_cq->lock);
1062 spin_lock_nested(&send_cq->lock, SINGLE_DEPTH_NESTING);
1065 EXPORT_SYMBOL_GPL(hns_roce_lock_cqs);
1067 void hns_roce_unlock_cqs(struct hns_roce_cq *send_cq,
1068 struct hns_roce_cq *recv_cq) __releases(&send_cq->lock)
1069 __releases(&recv_cq->lock)
1071 if (send_cq == recv_cq) {
1072 __release(&recv_cq->lock);
1073 spin_unlock_irq(&send_cq->lock);
1074 } else if (send_cq->cqn < recv_cq->cqn) {
1075 spin_unlock(&recv_cq->lock);
1076 spin_unlock_irq(&send_cq->lock);
1078 spin_unlock(&send_cq->lock);
1079 spin_unlock_irq(&recv_cq->lock);
1082 EXPORT_SYMBOL_GPL(hns_roce_unlock_cqs);
1084 static void *get_wqe(struct hns_roce_qp *hr_qp, int offset)
1087 return hns_roce_buf_offset(&hr_qp->hr_buf, offset);
1090 void *get_recv_wqe(struct hns_roce_qp *hr_qp, int n)
1092 return get_wqe(hr_qp, hr_qp->rq.offset + (n << hr_qp->rq.wqe_shift));
1094 EXPORT_SYMBOL_GPL(get_recv_wqe);
1096 void *get_send_wqe(struct hns_roce_qp *hr_qp, int n)
1098 return get_wqe(hr_qp, hr_qp->sq.offset + (n << hr_qp->sq.wqe_shift));
1100 EXPORT_SYMBOL_GPL(get_send_wqe);
1102 void *get_send_extend_sge(struct hns_roce_qp *hr_qp, int n)
1104 return hns_roce_buf_offset(&hr_qp->hr_buf, hr_qp->sge.offset +
1105 (n << hr_qp->sge.sge_shift));
1107 EXPORT_SYMBOL_GPL(get_send_extend_sge);
1109 bool hns_roce_wq_overflow(struct hns_roce_wq *hr_wq, int nreq,
1110 struct ib_cq *ib_cq)
1112 struct hns_roce_cq *hr_cq;
1115 cur = hr_wq->head - hr_wq->tail;
1116 if (likely(cur + nreq < hr_wq->max_post))
1119 hr_cq = to_hr_cq(ib_cq);
1120 spin_lock(&hr_cq->lock);
1121 cur = hr_wq->head - hr_wq->tail;
1122 spin_unlock(&hr_cq->lock);
1124 return cur + nreq >= hr_wq->max_post;
1126 EXPORT_SYMBOL_GPL(hns_roce_wq_overflow);
1128 int hns_roce_init_qp_table(struct hns_roce_dev *hr_dev)
1130 struct hns_roce_qp_table *qp_table = &hr_dev->qp_table;
1131 int reserved_from_top = 0;
1132 int reserved_from_bot;
1135 mutex_init(&qp_table->scc_mutex);
1136 xa_init(&hr_dev->qp_table_xa);
1138 /* In hw v1, a port include two SQP, six ports total 12 */
1139 if (hr_dev->caps.max_sq_sg <= 2)
1140 reserved_from_bot = SQP_NUM;
1142 reserved_from_bot = hr_dev->caps.reserved_qps;
1144 ret = hns_roce_bitmap_init(&qp_table->bitmap, hr_dev->caps.num_qps,
1145 hr_dev->caps.num_qps - 1, reserved_from_bot,
1148 dev_err(hr_dev->dev, "qp bitmap init failed!error=%d\n",
1156 void hns_roce_cleanup_qp_table(struct hns_roce_dev *hr_dev)
1158 hns_roce_bitmap_cleanup(&hr_dev->qp_table.bitmap);