1 /* 2 * Copyright (c) 2016 Hisilicon Limited. 3 * Copyright (c) 2007, 2008 Mellanox Technologies. All rights reserved. 4 * 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: 10 * 11 * Redistribution and use in source and binary forms, with or 12 * without modification, are permitted provided that the following 13 * conditions are met: 14 * 15 * - Redistributions of source code must retain the above 16 * copyright notice, this list of conditions and the following 17 * disclaimer. 18 * 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. 23 * 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 31 * SOFTWARE. 32 */ 33 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> 43 44 #define SQP_NUM (2 * HNS_ROCE_MAX_PORTS) 45 46 static void flush_work_handle(struct work_struct *work) 47 { 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; 54 int attr_mask; 55 int ret; 56 57 attr_mask = IB_QP_STATE; 58 attr.qp_state = IB_QPS_ERR; 59 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); 62 if (ret) 63 dev_err(dev, "Modify QP to error state failed(%d) during CQE flush\n", 64 ret); 65 } 66 67 /* 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 70 */ 71 if (atomic_dec_and_test(&hr_qp->refcount)) 72 complete(&hr_qp->free); 73 } 74 75 void init_flush_work(struct hns_roce_dev *hr_dev, struct hns_roce_qp *hr_qp) 76 { 77 struct hns_roce_work *flush_work = &hr_qp->flush_work; 78 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); 83 } 84 85 void hns_roce_qp_event(struct hns_roce_dev *hr_dev, u32 qpn, int event_type) 86 { 87 struct device *dev = hr_dev->dev; 88 struct hns_roce_qp *qp; 89 90 xa_lock(&hr_dev->qp_table_xa); 91 qp = __hns_roce_qp_lookup(hr_dev, qpn); 92 if (qp) 93 atomic_inc(&qp->refcount); 94 xa_unlock(&hr_dev->qp_table_xa); 95 96 if (!qp) { 97 dev_warn(dev, "Async event for bogus QP %08x\n", qpn); 98 return; 99 } 100 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); 108 } 109 110 qp->event(qp, (enum hns_roce_event)event_type); 111 112 if (atomic_dec_and_test(&qp->refcount)) 113 complete(&qp->free); 114 } 115 116 static void hns_roce_ib_qp_event(struct hns_roce_qp *hr_qp, 117 enum hns_roce_event type) 118 { 119 struct ib_event event; 120 struct ib_qp *ibqp = &hr_qp->ibqp; 121 122 if (ibqp->event_handler) { 123 event.device = ibqp->device; 124 event.element.qp = ibqp; 125 switch (type) { 126 case HNS_ROCE_EVENT_TYPE_PATH_MIG: 127 event.event = IB_EVENT_PATH_MIG; 128 break; 129 case HNS_ROCE_EVENT_TYPE_COMM_EST: 130 event.event = IB_EVENT_COMM_EST; 131 break; 132 case HNS_ROCE_EVENT_TYPE_SQ_DRAINED: 133 event.event = IB_EVENT_SQ_DRAINED; 134 break; 135 case HNS_ROCE_EVENT_TYPE_SRQ_LAST_WQE_REACH: 136 event.event = IB_EVENT_QP_LAST_WQE_REACHED; 137 break; 138 case HNS_ROCE_EVENT_TYPE_WQ_CATAS_ERROR: 139 event.event = IB_EVENT_QP_FATAL; 140 break; 141 case HNS_ROCE_EVENT_TYPE_PATH_MIG_FAILED: 142 event.event = IB_EVENT_PATH_MIG_ERR; 143 break; 144 case HNS_ROCE_EVENT_TYPE_INV_REQ_LOCAL_WQ_ERROR: 145 event.event = IB_EVENT_QP_REQ_ERR; 146 break; 147 case HNS_ROCE_EVENT_TYPE_LOCAL_WQ_ACCESS_ERROR: 148 event.event = IB_EVENT_QP_ACCESS_ERR; 149 break; 150 default: 151 dev_dbg(ibqp->device->dev.parent, "roce_ib: Unexpected event type %d on QP %06lx\n", 152 type, hr_qp->qpn); 153 return; 154 } 155 ibqp->event_handler(&event, ibqp->qp_context); 156 } 157 } 158 159 static int alloc_qpn(struct hns_roce_dev *hr_dev, struct hns_roce_qp *hr_qp) 160 { 161 unsigned long num = 0; 162 int ret; 163 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]; 169 else 170 num = 1; 171 172 hr_qp->doorbell_qpn = 1; 173 } else { 174 ret = hns_roce_bitmap_alloc_range(&hr_dev->qp_table.bitmap, 175 1, 1, &num); 176 if (ret) { 177 ibdev_err(&hr_dev->ib_dev, "Failed to alloc bitmap\n"); 178 return -ENOMEM; 179 } 180 181 hr_qp->doorbell_qpn = (u32)num; 182 } 183 184 hr_qp->qpn = num; 185 186 return 0; 187 } 188 189 enum hns_roce_qp_state to_hns_roce_state(enum ib_qp_state state) 190 { 191 switch (state) { 192 case IB_QPS_RESET: 193 return HNS_ROCE_QP_STATE_RST; 194 case IB_QPS_INIT: 195 return HNS_ROCE_QP_STATE_INIT; 196 case IB_QPS_RTR: 197 return HNS_ROCE_QP_STATE_RTR; 198 case IB_QPS_RTS: 199 return HNS_ROCE_QP_STATE_RTS; 200 case IB_QPS_SQD: 201 return HNS_ROCE_QP_STATE_SQD; 202 case IB_QPS_ERR: 203 return HNS_ROCE_QP_STATE_ERR; 204 default: 205 return HNS_ROCE_QP_NUM_STATE; 206 } 207 } 208 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) 212 { 213 struct hns_roce_cq *hr_send_cq, *hr_recv_cq; 214 unsigned long flags; 215 216 hr_send_cq = send_cq ? to_hr_cq(send_cq) : NULL; 217 hr_recv_cq = recv_cq ? to_hr_cq(recv_cq) : NULL; 218 219 spin_lock_irqsave(&hr_dev->qp_list_lock, flags); 220 hns_roce_lock_cqs(hr_send_cq, hr_recv_cq); 221 222 list_add_tail(&hr_qp->node, &hr_dev->qp_list); 223 if (hr_send_cq) 224 list_add_tail(&hr_qp->sq_node, &hr_send_cq->sq_list); 225 if (hr_recv_cq) 226 list_add_tail(&hr_qp->rq_node, &hr_recv_cq->rq_list); 227 228 hns_roce_unlock_cqs(hr_send_cq, hr_recv_cq); 229 spin_unlock_irqrestore(&hr_dev->qp_list_lock, flags); 230 } 231 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) 235 { 236 struct xarray *xa = &hr_dev->qp_table_xa; 237 int ret; 238 239 if (!hr_qp->qpn) 240 return -EINVAL; 241 242 ret = xa_err(xa_store_irq(xa, hr_qp->qpn, hr_qp, GFP_KERNEL)); 243 if (ret) 244 dev_err(hr_dev->dev, "Failed to xa store for QPC\n"); 245 else 246 /* add QP to device's QP list for softwc */ 247 add_qp_to_list(hr_dev, hr_qp, init_attr->send_cq, 248 init_attr->recv_cq); 249 250 return ret; 251 } 252 253 static int alloc_qpc(struct hns_roce_dev *hr_dev, struct hns_roce_qp *hr_qp) 254 { 255 struct hns_roce_qp_table *qp_table = &hr_dev->qp_table; 256 struct device *dev = hr_dev->dev; 257 int ret; 258 259 if (!hr_qp->qpn) 260 return -EINVAL; 261 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) 265 return 0; 266 267 /* Alloc memory for QPC */ 268 ret = hns_roce_table_get(hr_dev, &qp_table->qp_table, hr_qp->qpn); 269 if (ret) { 270 dev_err(dev, "Failed to get QPC table\n"); 271 goto err_out; 272 } 273 274 /* Alloc memory for IRRL */ 275 ret = hns_roce_table_get(hr_dev, &qp_table->irrl_table, hr_qp->qpn); 276 if (ret) { 277 dev_err(dev, "Failed to get IRRL table\n"); 278 goto err_put_qp; 279 } 280 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, 284 hr_qp->qpn); 285 if (ret) { 286 dev_err(dev, "Failed to get TRRL table\n"); 287 goto err_put_irrl; 288 } 289 } 290 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, 294 hr_qp->qpn); 295 if (ret) { 296 dev_err(dev, "Failed to get SCC CTX table\n"); 297 goto err_put_trrl; 298 } 299 } 300 301 return 0; 302 303 err_put_trrl: 304 if (hr_dev->caps.trrl_entry_sz) 305 hns_roce_table_put(hr_dev, &qp_table->trrl_table, hr_qp->qpn); 306 307 err_put_irrl: 308 hns_roce_table_put(hr_dev, &qp_table->irrl_table, hr_qp->qpn); 309 310 err_put_qp: 311 hns_roce_table_put(hr_dev, &qp_table->qp_table, hr_qp->qpn); 312 313 err_out: 314 return ret; 315 } 316 317 void hns_roce_qp_remove(struct hns_roce_dev *hr_dev, struct hns_roce_qp *hr_qp) 318 { 319 struct xarray *xa = &hr_dev->qp_table_xa; 320 unsigned long flags; 321 322 list_del(&hr_qp->node); 323 list_del(&hr_qp->sq_node); 324 list_del(&hr_qp->rq_node); 325 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); 329 } 330 331 static void free_qpc(struct hns_roce_dev *hr_dev, struct hns_roce_qp *hr_qp) 332 { 333 struct hns_roce_qp_table *qp_table = &hr_dev->qp_table; 334 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) 338 return; 339 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); 343 } 344 345 static void free_qpn(struct hns_roce_dev *hr_dev, struct hns_roce_qp *hr_qp) 346 { 347 struct hns_roce_qp_table *qp_table = &hr_dev->qp_table; 348 349 if (hr_qp->ibqp.qp_type == IB_QPT_GSI) 350 return; 351 352 if (hr_qp->qpn < hr_dev->caps.reserved_qps) 353 return; 354 355 hns_roce_bitmap_free_range(&qp_table->bitmap, hr_qp->qpn, 1, BITMAP_RR); 356 } 357 358 static int set_rq_size(struct hns_roce_dev *hr_dev, struct ib_qp_cap *cap, 359 struct hns_roce_qp *hr_qp, int has_rq) 360 { 361 u32 cnt; 362 363 /* If srq exist, set zero for relative number of rq */ 364 if (!has_rq) { 365 hr_qp->rq.wqe_cnt = 0; 366 hr_qp->rq.max_gs = 0; 367 hr_qp->rq_inl_buf.wqe_cnt = 0; 368 cap->max_recv_wr = 0; 369 cap->max_recv_sge = 0; 370 371 return 0; 372 } 373 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); 379 return -EINVAL; 380 } 381 382 cnt = roundup_pow_of_two(max(cap->max_recv_wr, hr_dev->caps.min_wqes)); 383 if (cnt > hr_dev->caps.max_wqes) { 384 ibdev_err(&hr_dev->ib_dev, "rq depth %u too large\n", 385 cap->max_recv_wr); 386 return -EINVAL; 387 } 388 389 hr_qp->rq.max_gs = roundup_pow_of_two(max(1U, cap->max_recv_sge) + 390 HNS_ROCE_RESERVED_SGE); 391 392 if (hr_dev->caps.max_rq_sg <= HNS_ROCE_SGE_IN_WQE) 393 hr_qp->rq.wqe_shift = ilog2(hr_dev->caps.max_rq_desc_sz); 394 else 395 hr_qp->rq.wqe_shift = ilog2(hr_dev->caps.max_rq_desc_sz * 396 hr_qp->rq.max_gs); 397 398 hr_qp->rq.wqe_cnt = cnt; 399 if (hr_dev->caps.flags & HNS_ROCE_CAP_FLAG_RQ_INLINE) 400 hr_qp->rq_inl_buf.wqe_cnt = cnt; 401 else 402 hr_qp->rq_inl_buf.wqe_cnt = 0; 403 404 cap->max_recv_wr = cnt; 405 cap->max_recv_sge = hr_qp->rq.max_gs - HNS_ROCE_RESERVED_SGE; 406 407 return 0; 408 } 409 410 static int set_extend_sge_param(struct hns_roce_dev *hr_dev, u32 sq_wqe_cnt, 411 struct hns_roce_qp *hr_qp, 412 struct ib_qp_cap *cap) 413 { 414 struct ib_device *ibdev = &hr_dev->ib_dev; 415 u32 cnt; 416 417 cnt = max(1U, cap->max_send_sge); 418 if (hr_dev->hw_rev == HNS_ROCE_HW_VER1) { 419 hr_qp->sq.max_gs = roundup_pow_of_two(cnt); 420 hr_qp->sge.sge_cnt = 0; 421 422 return 0; 423 } 424 425 hr_qp->sq.max_gs = cnt; 426 427 /* UD sqwqe's sge use extend sge */ 428 if (hr_qp->ibqp.qp_type == IB_QPT_GSI || 429 hr_qp->ibqp.qp_type == IB_QPT_UD) { 430 cnt = roundup_pow_of_two(sq_wqe_cnt * hr_qp->sq.max_gs); 431 } else if (hr_qp->sq.max_gs > HNS_ROCE_SGE_IN_WQE) { 432 cnt = roundup_pow_of_two(sq_wqe_cnt * 433 (hr_qp->sq.max_gs - HNS_ROCE_SGE_IN_WQE)); 434 435 if (hr_dev->pci_dev->revision == PCI_REVISION_ID_HIP08_A) { 436 if (cnt > hr_dev->caps.max_extend_sg) { 437 ibdev_err(ibdev, 438 "failed to check exSGE num, exSGE num = %d.\n", 439 cnt); 440 return -EINVAL; 441 } 442 } 443 } else { 444 cnt = 0; 445 } 446 447 hr_qp->sge.sge_shift = HNS_ROCE_SGE_SHIFT; 448 hr_qp->sge.sge_cnt = cnt; 449 450 return 0; 451 } 452 453 static int check_sq_size_with_integrity(struct hns_roce_dev *hr_dev, 454 struct ib_qp_cap *cap, 455 struct hns_roce_ib_create_qp *ucmd) 456 { 457 u32 roundup_sq_stride = roundup_pow_of_two(hr_dev->caps.max_sq_desc_sz); 458 u8 max_sq_stride = ilog2(roundup_sq_stride); 459 460 /* Sanity check SQ size before proceeding */ 461 if (ucmd->log_sq_stride > max_sq_stride || 462 ucmd->log_sq_stride < HNS_ROCE_IB_MIN_SQ_STRIDE) { 463 ibdev_err(&hr_dev->ib_dev, "Failed to check SQ stride size\n"); 464 return -EINVAL; 465 } 466 467 if (cap->max_send_sge > hr_dev->caps.max_sq_sg) { 468 ibdev_err(&hr_dev->ib_dev, "Failed to check SQ SGE size %d\n", 469 cap->max_send_sge); 470 return -EINVAL; 471 } 472 473 return 0; 474 } 475 476 static int set_user_sq_size(struct hns_roce_dev *hr_dev, 477 struct ib_qp_cap *cap, struct hns_roce_qp *hr_qp, 478 struct hns_roce_ib_create_qp *ucmd) 479 { 480 struct ib_device *ibdev = &hr_dev->ib_dev; 481 u32 cnt = 0; 482 int ret; 483 484 if (check_shl_overflow(1, ucmd->log_sq_bb_count, &cnt) || 485 cnt > hr_dev->caps.max_wqes) 486 return -EINVAL; 487 488 ret = check_sq_size_with_integrity(hr_dev, cap, ucmd); 489 if (ret) { 490 ibdev_err(ibdev, "failed to check user SQ size, ret = %d.\n", 491 ret); 492 return ret; 493 } 494 495 ret = set_extend_sge_param(hr_dev, cnt, hr_qp, cap); 496 if (ret) 497 return ret; 498 499 hr_qp->sq.wqe_shift = ucmd->log_sq_stride; 500 hr_qp->sq.wqe_cnt = cnt; 501 502 return 0; 503 } 504 505 static int set_wqe_buf_attr(struct hns_roce_dev *hr_dev, 506 struct hns_roce_qp *hr_qp, 507 struct hns_roce_buf_attr *buf_attr) 508 { 509 int buf_size; 510 int idx = 0; 511 512 hr_qp->buff_size = 0; 513 514 /* SQ WQE */ 515 hr_qp->sq.offset = 0; 516 buf_size = to_hr_hem_entries_size(hr_qp->sq.wqe_cnt, 517 hr_qp->sq.wqe_shift); 518 if (buf_size > 0 && idx < ARRAY_SIZE(buf_attr->region)) { 519 buf_attr->region[idx].size = buf_size; 520 buf_attr->region[idx].hopnum = hr_dev->caps.wqe_sq_hop_num; 521 idx++; 522 hr_qp->buff_size += buf_size; 523 } 524 525 /* extend SGE WQE in SQ */ 526 hr_qp->sge.offset = hr_qp->buff_size; 527 buf_size = to_hr_hem_entries_size(hr_qp->sge.sge_cnt, 528 hr_qp->sge.sge_shift); 529 if (buf_size > 0 && idx < ARRAY_SIZE(buf_attr->region)) { 530 buf_attr->region[idx].size = buf_size; 531 buf_attr->region[idx].hopnum = hr_dev->caps.wqe_sge_hop_num; 532 idx++; 533 hr_qp->buff_size += buf_size; 534 } 535 536 /* RQ WQE */ 537 hr_qp->rq.offset = hr_qp->buff_size; 538 buf_size = to_hr_hem_entries_size(hr_qp->rq.wqe_cnt, 539 hr_qp->rq.wqe_shift); 540 if (buf_size > 0 && idx < ARRAY_SIZE(buf_attr->region)) { 541 buf_attr->region[idx].size = buf_size; 542 buf_attr->region[idx].hopnum = hr_dev->caps.wqe_rq_hop_num; 543 idx++; 544 hr_qp->buff_size += buf_size; 545 } 546 547 if (hr_qp->buff_size < 1) 548 return -EINVAL; 549 550 buf_attr->page_shift = HNS_HW_PAGE_SHIFT + hr_dev->caps.mtt_buf_pg_sz; 551 buf_attr->fixed_page = true; 552 buf_attr->region_count = idx; 553 554 return 0; 555 } 556 557 static int set_kernel_sq_size(struct hns_roce_dev *hr_dev, 558 struct ib_qp_cap *cap, struct hns_roce_qp *hr_qp) 559 { 560 struct ib_device *ibdev = &hr_dev->ib_dev; 561 u32 cnt; 562 int ret; 563 564 if (!cap->max_send_wr || cap->max_send_wr > hr_dev->caps.max_wqes || 565 cap->max_send_sge > hr_dev->caps.max_sq_sg || 566 cap->max_inline_data > hr_dev->caps.max_sq_inline) { 567 ibdev_err(ibdev, 568 "failed to check SQ WR, SGE or inline num, ret = %d.\n", 569 -EINVAL); 570 return -EINVAL; 571 } 572 573 cnt = roundup_pow_of_two(max(cap->max_send_wr, hr_dev->caps.min_wqes)); 574 if (cnt > hr_dev->caps.max_wqes) { 575 ibdev_err(ibdev, "failed to check WQE num, WQE num = %d.\n", 576 cnt); 577 return -EINVAL; 578 } 579 580 hr_qp->sq.wqe_shift = ilog2(hr_dev->caps.max_sq_desc_sz); 581 hr_qp->sq.wqe_cnt = cnt; 582 583 ret = set_extend_sge_param(hr_dev, cnt, hr_qp, cap); 584 if (ret) 585 return ret; 586 587 /* sync the parameters of kernel QP to user's configuration */ 588 cap->max_send_wr = cnt; 589 cap->max_send_sge = hr_qp->sq.max_gs; 590 591 /* We don't support inline sends for kernel QPs (yet) */ 592 cap->max_inline_data = 0; 593 594 return 0; 595 } 596 597 static int hns_roce_qp_has_sq(struct ib_qp_init_attr *attr) 598 { 599 if (attr->qp_type == IB_QPT_XRC_TGT || !attr->cap.max_send_wr) 600 return 0; 601 602 return 1; 603 } 604 605 static int hns_roce_qp_has_rq(struct ib_qp_init_attr *attr) 606 { 607 if (attr->qp_type == IB_QPT_XRC_INI || 608 attr->qp_type == IB_QPT_XRC_TGT || attr->srq || 609 !attr->cap.max_recv_wr) 610 return 0; 611 612 return 1; 613 } 614 615 static int alloc_rq_inline_buf(struct hns_roce_qp *hr_qp, 616 struct ib_qp_init_attr *init_attr) 617 { 618 u32 max_recv_sge = init_attr->cap.max_recv_sge; 619 u32 wqe_cnt = hr_qp->rq_inl_buf.wqe_cnt; 620 struct hns_roce_rinl_wqe *wqe_list; 621 int i; 622 623 /* allocate recv inline buf */ 624 wqe_list = kcalloc(wqe_cnt, sizeof(struct hns_roce_rinl_wqe), 625 GFP_KERNEL); 626 627 if (!wqe_list) 628 goto err; 629 630 /* Allocate a continuous buffer for all inline sge we need */ 631 wqe_list[0].sg_list = kcalloc(wqe_cnt, (max_recv_sge * 632 sizeof(struct hns_roce_rinl_sge)), 633 GFP_KERNEL); 634 if (!wqe_list[0].sg_list) 635 goto err_wqe_list; 636 637 /* Assign buffers of sg_list to each inline wqe */ 638 for (i = 1; i < wqe_cnt; i++) 639 wqe_list[i].sg_list = &wqe_list[0].sg_list[i * max_recv_sge]; 640 641 hr_qp->rq_inl_buf.wqe_list = wqe_list; 642 643 return 0; 644 645 err_wqe_list: 646 kfree(wqe_list); 647 648 err: 649 return -ENOMEM; 650 } 651 652 static void free_rq_inline_buf(struct hns_roce_qp *hr_qp) 653 { 654 if (hr_qp->rq_inl_buf.wqe_list) 655 kfree(hr_qp->rq_inl_buf.wqe_list[0].sg_list); 656 kfree(hr_qp->rq_inl_buf.wqe_list); 657 } 658 659 static int alloc_qp_buf(struct hns_roce_dev *hr_dev, struct hns_roce_qp *hr_qp, 660 struct ib_qp_init_attr *init_attr, 661 struct ib_udata *udata, unsigned long addr) 662 { 663 struct ib_device *ibdev = &hr_dev->ib_dev; 664 struct hns_roce_buf_attr buf_attr = {}; 665 int ret; 666 667 if (!udata && hr_qp->rq_inl_buf.wqe_cnt) { 668 ret = alloc_rq_inline_buf(hr_qp, init_attr); 669 if (ret) { 670 ibdev_err(ibdev, 671 "failed to alloc inline buf, ret = %d.\n", 672 ret); 673 return ret; 674 } 675 } else { 676 hr_qp->rq_inl_buf.wqe_list = NULL; 677 } 678 679 ret = set_wqe_buf_attr(hr_dev, hr_qp, &buf_attr); 680 if (ret) { 681 ibdev_err(ibdev, "failed to split WQE buf, ret = %d.\n", ret); 682 goto err_inline; 683 } 684 ret = hns_roce_mtr_create(hr_dev, &hr_qp->mtr, &buf_attr, 685 HNS_HW_PAGE_SHIFT + hr_dev->caps.mtt_ba_pg_sz, 686 udata, addr); 687 if (ret) { 688 ibdev_err(ibdev, "failed to create WQE mtr, ret = %d.\n", ret); 689 goto err_inline; 690 } 691 692 return 0; 693 err_inline: 694 free_rq_inline_buf(hr_qp); 695 696 return ret; 697 } 698 699 static void free_qp_buf(struct hns_roce_dev *hr_dev, struct hns_roce_qp *hr_qp) 700 { 701 hns_roce_mtr_destroy(hr_dev, &hr_qp->mtr); 702 free_rq_inline_buf(hr_qp); 703 } 704 705 static inline bool user_qp_has_sdb(struct hns_roce_dev *hr_dev, 706 struct ib_qp_init_attr *init_attr, 707 struct ib_udata *udata, 708 struct hns_roce_ib_create_qp_resp *resp, 709 struct hns_roce_ib_create_qp *ucmd) 710 { 711 return ((hr_dev->caps.flags & HNS_ROCE_CAP_FLAG_SQ_RECORD_DB) && 712 udata->outlen >= offsetofend(typeof(*resp), cap_flags) && 713 hns_roce_qp_has_sq(init_attr) && 714 udata->inlen >= offsetofend(typeof(*ucmd), sdb_addr)); 715 } 716 717 static inline bool user_qp_has_rdb(struct hns_roce_dev *hr_dev, 718 struct ib_qp_init_attr *init_attr, 719 struct ib_udata *udata, 720 struct hns_roce_ib_create_qp_resp *resp) 721 { 722 return ((hr_dev->caps.flags & HNS_ROCE_CAP_FLAG_RECORD_DB) && 723 udata->outlen >= offsetofend(typeof(*resp), cap_flags) && 724 hns_roce_qp_has_rq(init_attr)); 725 } 726 727 static inline bool kernel_qp_has_rdb(struct hns_roce_dev *hr_dev, 728 struct ib_qp_init_attr *init_attr) 729 { 730 return ((hr_dev->caps.flags & HNS_ROCE_CAP_FLAG_RECORD_DB) && 731 hns_roce_qp_has_rq(init_attr)); 732 } 733 734 static int alloc_qp_db(struct hns_roce_dev *hr_dev, struct hns_roce_qp *hr_qp, 735 struct ib_qp_init_attr *init_attr, 736 struct ib_udata *udata, 737 struct hns_roce_ib_create_qp *ucmd, 738 struct hns_roce_ib_create_qp_resp *resp) 739 { 740 struct hns_roce_ucontext *uctx = rdma_udata_to_drv_context( 741 udata, struct hns_roce_ucontext, ibucontext); 742 struct ib_device *ibdev = &hr_dev->ib_dev; 743 int ret; 744 745 if (udata) { 746 if (user_qp_has_sdb(hr_dev, init_attr, udata, resp, ucmd)) { 747 ret = hns_roce_db_map_user(uctx, udata, ucmd->sdb_addr, 748 &hr_qp->sdb); 749 if (ret) { 750 ibdev_err(ibdev, 751 "Failed to map user SQ doorbell\n"); 752 goto err_out; 753 } 754 hr_qp->en_flags |= HNS_ROCE_QP_CAP_SQ_RECORD_DB; 755 resp->cap_flags |= HNS_ROCE_QP_CAP_SQ_RECORD_DB; 756 } 757 758 if (user_qp_has_rdb(hr_dev, init_attr, udata, resp)) { 759 ret = hns_roce_db_map_user(uctx, udata, ucmd->db_addr, 760 &hr_qp->rdb); 761 if (ret) { 762 ibdev_err(ibdev, 763 "Failed to map user RQ doorbell\n"); 764 goto err_sdb; 765 } 766 hr_qp->en_flags |= HNS_ROCE_QP_CAP_RQ_RECORD_DB; 767 resp->cap_flags |= HNS_ROCE_QP_CAP_RQ_RECORD_DB; 768 } 769 } else { 770 /* QP doorbell register address */ 771 hr_qp->sq.db_reg_l = hr_dev->reg_base + hr_dev->sdb_offset + 772 DB_REG_OFFSET * hr_dev->priv_uar.index; 773 hr_qp->rq.db_reg_l = hr_dev->reg_base + hr_dev->odb_offset + 774 DB_REG_OFFSET * hr_dev->priv_uar.index; 775 776 if (kernel_qp_has_rdb(hr_dev, init_attr)) { 777 ret = hns_roce_alloc_db(hr_dev, &hr_qp->rdb, 0); 778 if (ret) { 779 ibdev_err(ibdev, 780 "Failed to alloc kernel RQ doorbell\n"); 781 goto err_out; 782 } 783 *hr_qp->rdb.db_record = 0; 784 hr_qp->en_flags |= HNS_ROCE_QP_CAP_RQ_RECORD_DB; 785 } 786 } 787 788 return 0; 789 err_sdb: 790 if (udata && hr_qp->en_flags & HNS_ROCE_QP_CAP_SQ_RECORD_DB) 791 hns_roce_db_unmap_user(uctx, &hr_qp->sdb); 792 err_out: 793 return ret; 794 } 795 796 static void free_qp_db(struct hns_roce_dev *hr_dev, struct hns_roce_qp *hr_qp, 797 struct ib_udata *udata) 798 { 799 struct hns_roce_ucontext *uctx = rdma_udata_to_drv_context( 800 udata, struct hns_roce_ucontext, ibucontext); 801 802 if (udata) { 803 if (hr_qp->en_flags & HNS_ROCE_QP_CAP_RQ_RECORD_DB) 804 hns_roce_db_unmap_user(uctx, &hr_qp->rdb); 805 if (hr_qp->en_flags & HNS_ROCE_QP_CAP_SQ_RECORD_DB) 806 hns_roce_db_unmap_user(uctx, &hr_qp->sdb); 807 } else { 808 if (hr_qp->en_flags & HNS_ROCE_QP_CAP_RQ_RECORD_DB) 809 hns_roce_free_db(hr_dev, &hr_qp->rdb); 810 } 811 } 812 813 static int alloc_kernel_wrid(struct hns_roce_dev *hr_dev, 814 struct hns_roce_qp *hr_qp) 815 { 816 struct ib_device *ibdev = &hr_dev->ib_dev; 817 u64 *sq_wrid = NULL; 818 u64 *rq_wrid = NULL; 819 int ret; 820 821 sq_wrid = kcalloc(hr_qp->sq.wqe_cnt, sizeof(u64), GFP_KERNEL); 822 if (ZERO_OR_NULL_PTR(sq_wrid)) { 823 ibdev_err(ibdev, "Failed to alloc SQ wrid\n"); 824 return -ENOMEM; 825 } 826 827 if (hr_qp->rq.wqe_cnt) { 828 rq_wrid = kcalloc(hr_qp->rq.wqe_cnt, sizeof(u64), GFP_KERNEL); 829 if (ZERO_OR_NULL_PTR(rq_wrid)) { 830 ibdev_err(ibdev, "Failed to alloc RQ wrid\n"); 831 ret = -ENOMEM; 832 goto err_sq; 833 } 834 } 835 836 hr_qp->sq.wrid = sq_wrid; 837 hr_qp->rq.wrid = rq_wrid; 838 return 0; 839 err_sq: 840 kfree(sq_wrid); 841 842 return ret; 843 } 844 845 static void free_kernel_wrid(struct hns_roce_qp *hr_qp) 846 { 847 kfree(hr_qp->rq.wrid); 848 kfree(hr_qp->sq.wrid); 849 } 850 851 static int set_qp_param(struct hns_roce_dev *hr_dev, struct hns_roce_qp *hr_qp, 852 struct ib_qp_init_attr *init_attr, 853 struct ib_udata *udata, 854 struct hns_roce_ib_create_qp *ucmd) 855 { 856 struct ib_device *ibdev = &hr_dev->ib_dev; 857 int ret; 858 859 hr_qp->ibqp.qp_type = init_attr->qp_type; 860 861 if (init_attr->sq_sig_type == IB_SIGNAL_ALL_WR) 862 hr_qp->sq_signal_bits = IB_SIGNAL_ALL_WR; 863 else 864 hr_qp->sq_signal_bits = IB_SIGNAL_REQ_WR; 865 866 ret = set_rq_size(hr_dev, &init_attr->cap, hr_qp, 867 hns_roce_qp_has_rq(init_attr)); 868 if (ret) { 869 ibdev_err(ibdev, "failed to set user RQ size, ret = %d.\n", 870 ret); 871 return ret; 872 } 873 874 if (udata) { 875 if (ib_copy_from_udata(ucmd, udata, sizeof(*ucmd))) { 876 ibdev_err(ibdev, "Failed to copy QP ucmd\n"); 877 return -EFAULT; 878 } 879 880 ret = set_user_sq_size(hr_dev, &init_attr->cap, hr_qp, ucmd); 881 if (ret) 882 ibdev_err(ibdev, "Failed to set user SQ size\n"); 883 } else { 884 if (init_attr->create_flags & 885 IB_QP_CREATE_BLOCK_MULTICAST_LOOPBACK) { 886 ibdev_err(ibdev, "Failed to check multicast loopback\n"); 887 return -EINVAL; 888 } 889 890 if (init_attr->create_flags & IB_QP_CREATE_IPOIB_UD_LSO) { 891 ibdev_err(ibdev, "Failed to check ipoib ud lso\n"); 892 return -EINVAL; 893 } 894 895 ret = set_kernel_sq_size(hr_dev, &init_attr->cap, hr_qp); 896 if (ret) 897 ibdev_err(ibdev, "Failed to set kernel SQ size\n"); 898 } 899 900 return ret; 901 } 902 903 static int hns_roce_create_qp_common(struct hns_roce_dev *hr_dev, 904 struct ib_pd *ib_pd, 905 struct ib_qp_init_attr *init_attr, 906 struct ib_udata *udata, 907 struct hns_roce_qp *hr_qp) 908 { 909 struct hns_roce_ib_create_qp_resp resp = {}; 910 struct ib_device *ibdev = &hr_dev->ib_dev; 911 struct hns_roce_ib_create_qp ucmd; 912 int ret; 913 914 mutex_init(&hr_qp->mutex); 915 spin_lock_init(&hr_qp->sq.lock); 916 spin_lock_init(&hr_qp->rq.lock); 917 918 hr_qp->state = IB_QPS_RESET; 919 hr_qp->flush_flag = 0; 920 921 ret = set_qp_param(hr_dev, hr_qp, init_attr, udata, &ucmd); 922 if (ret) { 923 ibdev_err(ibdev, "Failed to set QP param\n"); 924 return ret; 925 } 926 927 if (!udata) { 928 ret = alloc_kernel_wrid(hr_dev, hr_qp); 929 if (ret) { 930 ibdev_err(ibdev, "Failed to alloc wrid\n"); 931 return ret; 932 } 933 } 934 935 ret = alloc_qp_db(hr_dev, hr_qp, init_attr, udata, &ucmd, &resp); 936 if (ret) { 937 ibdev_err(ibdev, "Failed to alloc QP doorbell\n"); 938 goto err_wrid; 939 } 940 941 ret = alloc_qp_buf(hr_dev, hr_qp, init_attr, udata, ucmd.buf_addr); 942 if (ret) { 943 ibdev_err(ibdev, "Failed to alloc QP buffer\n"); 944 goto err_db; 945 } 946 947 ret = alloc_qpn(hr_dev, hr_qp); 948 if (ret) { 949 ibdev_err(ibdev, "Failed to alloc QPN\n"); 950 goto err_buf; 951 } 952 953 ret = alloc_qpc(hr_dev, hr_qp); 954 if (ret) { 955 ibdev_err(ibdev, "Failed to alloc QP context\n"); 956 goto err_qpn; 957 } 958 959 ret = hns_roce_qp_store(hr_dev, hr_qp, init_attr); 960 if (ret) { 961 ibdev_err(ibdev, "Failed to store QP\n"); 962 goto err_qpc; 963 } 964 965 if (udata) { 966 ret = ib_copy_to_udata(udata, &resp, 967 min(udata->outlen, sizeof(resp))); 968 if (ret) { 969 ibdev_err(ibdev, "copy qp resp failed!\n"); 970 goto err_store; 971 } 972 } 973 974 if (hr_dev->caps.flags & HNS_ROCE_CAP_FLAG_QP_FLOW_CTRL) { 975 ret = hr_dev->hw->qp_flow_control_init(hr_dev, hr_qp); 976 if (ret) 977 goto err_store; 978 } 979 980 hr_qp->ibqp.qp_num = hr_qp->qpn; 981 hr_qp->event = hns_roce_ib_qp_event; 982 atomic_set(&hr_qp->refcount, 1); 983 init_completion(&hr_qp->free); 984 985 return 0; 986 987 err_store: 988 hns_roce_qp_remove(hr_dev, hr_qp); 989 err_qpc: 990 free_qpc(hr_dev, hr_qp); 991 err_qpn: 992 free_qpn(hr_dev, hr_qp); 993 err_buf: 994 free_qp_buf(hr_dev, hr_qp); 995 err_db: 996 free_qp_db(hr_dev, hr_qp, udata); 997 err_wrid: 998 free_kernel_wrid(hr_qp); 999 return ret; 1000 } 1001 1002 void hns_roce_qp_destroy(struct hns_roce_dev *hr_dev, struct hns_roce_qp *hr_qp, 1003 struct ib_udata *udata) 1004 { 1005 if (atomic_dec_and_test(&hr_qp->refcount)) 1006 complete(&hr_qp->free); 1007 wait_for_completion(&hr_qp->free); 1008 1009 free_qpc(hr_dev, hr_qp); 1010 free_qpn(hr_dev, hr_qp); 1011 free_qp_buf(hr_dev, hr_qp); 1012 free_kernel_wrid(hr_qp); 1013 free_qp_db(hr_dev, hr_qp, udata); 1014 1015 kfree(hr_qp); 1016 } 1017 1018 struct ib_qp *hns_roce_create_qp(struct ib_pd *pd, 1019 struct ib_qp_init_attr *init_attr, 1020 struct ib_udata *udata) 1021 { 1022 struct hns_roce_dev *hr_dev = to_hr_dev(pd->device); 1023 struct ib_device *ibdev = &hr_dev->ib_dev; 1024 struct hns_roce_qp *hr_qp; 1025 int ret; 1026 1027 switch (init_attr->qp_type) { 1028 case IB_QPT_RC: { 1029 hr_qp = kzalloc(sizeof(*hr_qp), GFP_KERNEL); 1030 if (!hr_qp) 1031 return ERR_PTR(-ENOMEM); 1032 1033 ret = hns_roce_create_qp_common(hr_dev, pd, init_attr, udata, 1034 hr_qp); 1035 if (ret) { 1036 ibdev_err(ibdev, "Create QP 0x%06lx failed(%d)\n", 1037 hr_qp->qpn, ret); 1038 kfree(hr_qp); 1039 return ERR_PTR(ret); 1040 } 1041 1042 break; 1043 } 1044 case IB_QPT_GSI: { 1045 /* Userspace is not allowed to create special QPs: */ 1046 if (udata) { 1047 ibdev_err(ibdev, "not support usr space GSI\n"); 1048 return ERR_PTR(-EINVAL); 1049 } 1050 1051 hr_qp = kzalloc(sizeof(*hr_qp), GFP_KERNEL); 1052 if (!hr_qp) 1053 return ERR_PTR(-ENOMEM); 1054 1055 hr_qp->port = init_attr->port_num - 1; 1056 hr_qp->phy_port = hr_dev->iboe.phy_port[hr_qp->port]; 1057 1058 ret = hns_roce_create_qp_common(hr_dev, pd, init_attr, udata, 1059 hr_qp); 1060 if (ret) { 1061 ibdev_err(ibdev, "Create GSI QP failed!\n"); 1062 kfree(hr_qp); 1063 return ERR_PTR(ret); 1064 } 1065 1066 break; 1067 } 1068 default:{ 1069 ibdev_err(ibdev, "not support QP type %d\n", 1070 init_attr->qp_type); 1071 return ERR_PTR(-EOPNOTSUPP); 1072 } 1073 } 1074 1075 return &hr_qp->ibqp; 1076 } 1077 1078 int to_hr_qp_type(int qp_type) 1079 { 1080 int transport_type; 1081 1082 if (qp_type == IB_QPT_RC) 1083 transport_type = SERV_TYPE_RC; 1084 else if (qp_type == IB_QPT_UC) 1085 transport_type = SERV_TYPE_UC; 1086 else if (qp_type == IB_QPT_UD) 1087 transport_type = SERV_TYPE_UD; 1088 else if (qp_type == IB_QPT_GSI) 1089 transport_type = SERV_TYPE_UD; 1090 else 1091 transport_type = -1; 1092 1093 return transport_type; 1094 } 1095 1096 static int check_mtu_validate(struct hns_roce_dev *hr_dev, 1097 struct hns_roce_qp *hr_qp, 1098 struct ib_qp_attr *attr, int attr_mask) 1099 { 1100 enum ib_mtu active_mtu; 1101 int p; 1102 1103 p = attr_mask & IB_QP_PORT ? (attr->port_num - 1) : hr_qp->port; 1104 active_mtu = iboe_get_mtu(hr_dev->iboe.netdevs[p]->mtu); 1105 1106 if ((hr_dev->caps.max_mtu >= IB_MTU_2048 && 1107 attr->path_mtu > hr_dev->caps.max_mtu) || 1108 attr->path_mtu < IB_MTU_256 || attr->path_mtu > active_mtu) { 1109 ibdev_err(&hr_dev->ib_dev, 1110 "attr path_mtu(%d)invalid while modify qp", 1111 attr->path_mtu); 1112 return -EINVAL; 1113 } 1114 1115 return 0; 1116 } 1117 1118 static int hns_roce_check_qp_attr(struct ib_qp *ibqp, struct ib_qp_attr *attr, 1119 int attr_mask) 1120 { 1121 struct hns_roce_dev *hr_dev = to_hr_dev(ibqp->device); 1122 struct hns_roce_qp *hr_qp = to_hr_qp(ibqp); 1123 int p; 1124 1125 if ((attr_mask & IB_QP_PORT) && 1126 (attr->port_num == 0 || attr->port_num > hr_dev->caps.num_ports)) { 1127 ibdev_err(&hr_dev->ib_dev, 1128 "attr port_num invalid.attr->port_num=%d\n", 1129 attr->port_num); 1130 return -EINVAL; 1131 } 1132 1133 if (attr_mask & IB_QP_PKEY_INDEX) { 1134 p = attr_mask & IB_QP_PORT ? (attr->port_num - 1) : hr_qp->port; 1135 if (attr->pkey_index >= hr_dev->caps.pkey_table_len[p]) { 1136 ibdev_err(&hr_dev->ib_dev, 1137 "attr pkey_index invalid.attr->pkey_index=%d\n", 1138 attr->pkey_index); 1139 return -EINVAL; 1140 } 1141 } 1142 1143 if (attr_mask & IB_QP_MAX_QP_RD_ATOMIC && 1144 attr->max_rd_atomic > hr_dev->caps.max_qp_init_rdma) { 1145 ibdev_err(&hr_dev->ib_dev, 1146 "attr max_rd_atomic invalid.attr->max_rd_atomic=%d\n", 1147 attr->max_rd_atomic); 1148 return -EINVAL; 1149 } 1150 1151 if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC && 1152 attr->max_dest_rd_atomic > hr_dev->caps.max_qp_dest_rdma) { 1153 ibdev_err(&hr_dev->ib_dev, 1154 "attr max_dest_rd_atomic invalid.attr->max_dest_rd_atomic=%d\n", 1155 attr->max_dest_rd_atomic); 1156 return -EINVAL; 1157 } 1158 1159 if (attr_mask & IB_QP_PATH_MTU) 1160 return check_mtu_validate(hr_dev, hr_qp, attr, attr_mask); 1161 1162 return 0; 1163 } 1164 1165 int hns_roce_modify_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr, 1166 int attr_mask, struct ib_udata *udata) 1167 { 1168 struct hns_roce_dev *hr_dev = to_hr_dev(ibqp->device); 1169 struct hns_roce_qp *hr_qp = to_hr_qp(ibqp); 1170 enum ib_qp_state cur_state, new_state; 1171 int ret = -EINVAL; 1172 1173 mutex_lock(&hr_qp->mutex); 1174 1175 cur_state = attr_mask & IB_QP_CUR_STATE ? 1176 attr->cur_qp_state : (enum ib_qp_state)hr_qp->state; 1177 new_state = attr_mask & IB_QP_STATE ? attr->qp_state : cur_state; 1178 1179 if (ibqp->uobject && 1180 (attr_mask & IB_QP_STATE) && new_state == IB_QPS_ERR) { 1181 if (hr_qp->en_flags & HNS_ROCE_QP_CAP_SQ_RECORD_DB) { 1182 hr_qp->sq.head = *(int *)(hr_qp->sdb.virt_addr); 1183 1184 if (hr_qp->en_flags & HNS_ROCE_QP_CAP_RQ_RECORD_DB) 1185 hr_qp->rq.head = *(int *)(hr_qp->rdb.virt_addr); 1186 } else { 1187 ibdev_warn(&hr_dev->ib_dev, 1188 "flush cqe is not supported in userspace!\n"); 1189 goto out; 1190 } 1191 } 1192 1193 if (!ib_modify_qp_is_ok(cur_state, new_state, ibqp->qp_type, 1194 attr_mask)) { 1195 ibdev_err(&hr_dev->ib_dev, "ib_modify_qp_is_ok failed\n"); 1196 goto out; 1197 } 1198 1199 ret = hns_roce_check_qp_attr(ibqp, attr, attr_mask); 1200 if (ret) 1201 goto out; 1202 1203 if (cur_state == new_state && cur_state == IB_QPS_RESET) { 1204 if (hr_dev->hw_rev == HNS_ROCE_HW_VER1) { 1205 ret = -EPERM; 1206 ibdev_err(&hr_dev->ib_dev, 1207 "RST2RST state is not supported\n"); 1208 } else { 1209 ret = 0; 1210 } 1211 1212 goto out; 1213 } 1214 1215 ret = hr_dev->hw->modify_qp(ibqp, attr, attr_mask, cur_state, 1216 new_state); 1217 1218 out: 1219 mutex_unlock(&hr_qp->mutex); 1220 1221 return ret; 1222 } 1223 1224 void hns_roce_lock_cqs(struct hns_roce_cq *send_cq, struct hns_roce_cq *recv_cq) 1225 __acquires(&send_cq->lock) __acquires(&recv_cq->lock) 1226 { 1227 if (unlikely(send_cq == NULL && recv_cq == NULL)) { 1228 __acquire(&send_cq->lock); 1229 __acquire(&recv_cq->lock); 1230 } else if (unlikely(send_cq != NULL && recv_cq == NULL)) { 1231 spin_lock_irq(&send_cq->lock); 1232 __acquire(&recv_cq->lock); 1233 } else if (unlikely(send_cq == NULL && recv_cq != NULL)) { 1234 spin_lock_irq(&recv_cq->lock); 1235 __acquire(&send_cq->lock); 1236 } else if (send_cq == recv_cq) { 1237 spin_lock_irq(&send_cq->lock); 1238 __acquire(&recv_cq->lock); 1239 } else if (send_cq->cqn < recv_cq->cqn) { 1240 spin_lock_irq(&send_cq->lock); 1241 spin_lock_nested(&recv_cq->lock, SINGLE_DEPTH_NESTING); 1242 } else { 1243 spin_lock_irq(&recv_cq->lock); 1244 spin_lock_nested(&send_cq->lock, SINGLE_DEPTH_NESTING); 1245 } 1246 } 1247 1248 void hns_roce_unlock_cqs(struct hns_roce_cq *send_cq, 1249 struct hns_roce_cq *recv_cq) __releases(&send_cq->lock) 1250 __releases(&recv_cq->lock) 1251 { 1252 if (unlikely(send_cq == NULL && recv_cq == NULL)) { 1253 __release(&recv_cq->lock); 1254 __release(&send_cq->lock); 1255 } else if (unlikely(send_cq != NULL && recv_cq == NULL)) { 1256 __release(&recv_cq->lock); 1257 spin_unlock(&send_cq->lock); 1258 } else if (unlikely(send_cq == NULL && recv_cq != NULL)) { 1259 __release(&send_cq->lock); 1260 spin_unlock(&recv_cq->lock); 1261 } else if (send_cq == recv_cq) { 1262 __release(&recv_cq->lock); 1263 spin_unlock_irq(&send_cq->lock); 1264 } else if (send_cq->cqn < recv_cq->cqn) { 1265 spin_unlock(&recv_cq->lock); 1266 spin_unlock_irq(&send_cq->lock); 1267 } else { 1268 spin_unlock(&send_cq->lock); 1269 spin_unlock_irq(&recv_cq->lock); 1270 } 1271 } 1272 1273 static inline void *get_wqe(struct hns_roce_qp *hr_qp, int offset) 1274 { 1275 return hns_roce_buf_offset(hr_qp->mtr.kmem, offset); 1276 } 1277 1278 void *hns_roce_get_recv_wqe(struct hns_roce_qp *hr_qp, int n) 1279 { 1280 return get_wqe(hr_qp, hr_qp->rq.offset + (n << hr_qp->rq.wqe_shift)); 1281 } 1282 1283 void *hns_roce_get_send_wqe(struct hns_roce_qp *hr_qp, int n) 1284 { 1285 return get_wqe(hr_qp, hr_qp->sq.offset + (n << hr_qp->sq.wqe_shift)); 1286 } 1287 1288 void *hns_roce_get_extend_sge(struct hns_roce_qp *hr_qp, int n) 1289 { 1290 return get_wqe(hr_qp, hr_qp->sge.offset + (n << hr_qp->sge.sge_shift)); 1291 } 1292 1293 bool hns_roce_wq_overflow(struct hns_roce_wq *hr_wq, int nreq, 1294 struct ib_cq *ib_cq) 1295 { 1296 struct hns_roce_cq *hr_cq; 1297 u32 cur; 1298 1299 cur = hr_wq->head - hr_wq->tail; 1300 if (likely(cur + nreq < hr_wq->wqe_cnt)) 1301 return false; 1302 1303 hr_cq = to_hr_cq(ib_cq); 1304 spin_lock(&hr_cq->lock); 1305 cur = hr_wq->head - hr_wq->tail; 1306 spin_unlock(&hr_cq->lock); 1307 1308 return cur + nreq >= hr_wq->wqe_cnt; 1309 } 1310 1311 int hns_roce_init_qp_table(struct hns_roce_dev *hr_dev) 1312 { 1313 struct hns_roce_qp_table *qp_table = &hr_dev->qp_table; 1314 int reserved_from_top = 0; 1315 int reserved_from_bot; 1316 int ret; 1317 1318 mutex_init(&qp_table->scc_mutex); 1319 xa_init(&hr_dev->qp_table_xa); 1320 1321 reserved_from_bot = hr_dev->caps.reserved_qps; 1322 1323 ret = hns_roce_bitmap_init(&qp_table->bitmap, hr_dev->caps.num_qps, 1324 hr_dev->caps.num_qps - 1, reserved_from_bot, 1325 reserved_from_top); 1326 if (ret) { 1327 dev_err(hr_dev->dev, "qp bitmap init failed!error=%d\n", 1328 ret); 1329 return ret; 1330 } 1331 1332 return 0; 1333 } 1334 1335 void hns_roce_cleanup_qp_table(struct hns_roce_dev *hr_dev) 1336 { 1337 hns_roce_bitmap_cleanup(&hr_dev->qp_table.bitmap); 1338 } 1339