1 /* 2 * Copyright (c) 2016 Hisilicon Limited. 3 * 4 * This software is available to you under a choice of one of two 5 * licenses. You may choose to be licensed under the terms of the GNU 6 * General Public License (GPL) Version 2, available from the file 7 * COPYING in the main directory of this source tree, or the 8 * OpenIB.org BSD license below: 9 * 10 * Redistribution and use in source and binary forms, with or 11 * without modification, are permitted provided that the following 12 * conditions are met: 13 * 14 * - Redistributions of source code must retain the above 15 * copyright notice, this list of conditions and the following 16 * disclaimer. 17 * 18 * - Redistributions in binary form must reproduce the above 19 * copyright notice, this list of conditions and the following 20 * disclaimer in the documentation and/or other materials 21 * provided with the distribution. 22 * 23 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 24 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 25 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 26 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 27 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 28 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 29 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 30 * SOFTWARE. 31 */ 32 33 #include <linux/platform_device.h> 34 #include <rdma/ib_umem.h> 35 #include <rdma/uverbs_ioctl.h> 36 #include "hns_roce_device.h" 37 #include "hns_roce_cmd.h" 38 #include "hns_roce_hem.h" 39 #include <rdma/hns-abi.h> 40 #include "hns_roce_common.h" 41 42 static void hns_roce_ib_cq_comp(struct hns_roce_cq *hr_cq) 43 { 44 struct ib_cq *ibcq = &hr_cq->ib_cq; 45 46 ibcq->comp_handler(ibcq, ibcq->cq_context); 47 } 48 49 static void hns_roce_ib_cq_event(struct hns_roce_cq *hr_cq, 50 enum hns_roce_event event_type) 51 { 52 struct hns_roce_dev *hr_dev; 53 struct ib_event event; 54 struct ib_cq *ibcq; 55 56 ibcq = &hr_cq->ib_cq; 57 hr_dev = to_hr_dev(ibcq->device); 58 59 if (event_type != HNS_ROCE_EVENT_TYPE_CQ_ID_INVALID && 60 event_type != HNS_ROCE_EVENT_TYPE_CQ_ACCESS_ERROR && 61 event_type != HNS_ROCE_EVENT_TYPE_CQ_OVERFLOW) { 62 dev_err(hr_dev->dev, 63 "hns_roce_ib: Unexpected event type 0x%x on CQ %06lx\n", 64 event_type, hr_cq->cqn); 65 return; 66 } 67 68 if (ibcq->event_handler) { 69 event.device = ibcq->device; 70 event.event = IB_EVENT_CQ_ERR; 71 event.element.cq = ibcq; 72 ibcq->event_handler(&event, ibcq->cq_context); 73 } 74 } 75 76 static int hns_roce_sw2hw_cq(struct hns_roce_dev *dev, 77 struct hns_roce_cmd_mailbox *mailbox, 78 unsigned long cq_num) 79 { 80 return hns_roce_cmd_mbox(dev, mailbox->dma, 0, cq_num, 0, 81 HNS_ROCE_CMD_SW2HW_CQ, HNS_ROCE_CMD_TIMEOUT_MSECS); 82 } 83 84 static int hns_roce_cq_alloc(struct hns_roce_dev *hr_dev, int nent, 85 struct hns_roce_mtt *hr_mtt, 86 struct hns_roce_uar *hr_uar, 87 struct hns_roce_cq *hr_cq, int vector) 88 { 89 struct hns_roce_cmd_mailbox *mailbox; 90 struct hns_roce_hem_table *mtt_table; 91 struct hns_roce_cq_table *cq_table; 92 struct device *dev = hr_dev->dev; 93 dma_addr_t dma_handle; 94 u64 *mtts; 95 int ret; 96 97 cq_table = &hr_dev->cq_table; 98 99 /* Get the physical address of cq buf */ 100 if (hns_roce_check_whether_mhop(hr_dev, HEM_TYPE_CQE)) 101 mtt_table = &hr_dev->mr_table.mtt_cqe_table; 102 else 103 mtt_table = &hr_dev->mr_table.mtt_table; 104 105 mtts = hns_roce_table_find(hr_dev, mtt_table, 106 hr_mtt->first_seg, &dma_handle); 107 if (!mtts) { 108 dev_err(dev, "CQ alloc.Failed to find cq buf addr.\n"); 109 return -EINVAL; 110 } 111 112 if (vector >= hr_dev->caps.num_comp_vectors) { 113 dev_err(dev, "CQ alloc.Invalid vector.\n"); 114 return -EINVAL; 115 } 116 hr_cq->vector = vector; 117 118 ret = hns_roce_bitmap_alloc(&cq_table->bitmap, &hr_cq->cqn); 119 if (ret == -1) { 120 dev_err(dev, "CQ alloc.Failed to alloc index.\n"); 121 return -ENOMEM; 122 } 123 124 /* Get CQC memory HEM(Hardware Entry Memory) table */ 125 ret = hns_roce_table_get(hr_dev, &cq_table->table, hr_cq->cqn); 126 if (ret) { 127 dev_err(dev, "CQ alloc.Failed to get context mem.\n"); 128 goto err_out; 129 } 130 131 ret = xa_err(xa_store(&cq_table->array, hr_cq->cqn, hr_cq, GFP_KERNEL)); 132 if (ret) { 133 dev_err(dev, "CQ alloc failed xa_store.\n"); 134 goto err_put; 135 } 136 137 /* Allocate mailbox memory */ 138 mailbox = hns_roce_alloc_cmd_mailbox(hr_dev); 139 if (IS_ERR(mailbox)) { 140 ret = PTR_ERR(mailbox); 141 goto err_xa; 142 } 143 144 hr_dev->hw->write_cqc(hr_dev, hr_cq, mailbox->buf, mtts, dma_handle, 145 nent, vector); 146 147 /* Send mailbox to hw */ 148 ret = hns_roce_sw2hw_cq(hr_dev, mailbox, hr_cq->cqn); 149 hns_roce_free_cmd_mailbox(hr_dev, mailbox); 150 if (ret) { 151 dev_err(dev, "CQ alloc.Failed to cmd mailbox.\n"); 152 goto err_xa; 153 } 154 155 hr_cq->cons_index = 0; 156 hr_cq->arm_sn = 1; 157 hr_cq->uar = hr_uar; 158 159 atomic_set(&hr_cq->refcount, 1); 160 init_completion(&hr_cq->free); 161 162 return 0; 163 164 err_xa: 165 xa_erase(&cq_table->array, hr_cq->cqn); 166 167 err_put: 168 hns_roce_table_put(hr_dev, &cq_table->table, hr_cq->cqn); 169 170 err_out: 171 hns_roce_bitmap_free(&cq_table->bitmap, hr_cq->cqn, BITMAP_NO_RR); 172 return ret; 173 } 174 175 static int hns_roce_hw2sw_cq(struct hns_roce_dev *dev, 176 struct hns_roce_cmd_mailbox *mailbox, 177 unsigned long cq_num) 178 { 179 return hns_roce_cmd_mbox(dev, 0, mailbox ? mailbox->dma : 0, cq_num, 180 mailbox ? 0 : 1, HNS_ROCE_CMD_HW2SW_CQ, 181 HNS_ROCE_CMD_TIMEOUT_MSECS); 182 } 183 184 void hns_roce_free_cq(struct hns_roce_dev *hr_dev, struct hns_roce_cq *hr_cq) 185 { 186 struct hns_roce_cq_table *cq_table = &hr_dev->cq_table; 187 struct device *dev = hr_dev->dev; 188 int ret; 189 190 ret = hns_roce_hw2sw_cq(hr_dev, NULL, hr_cq->cqn); 191 if (ret) 192 dev_err(dev, "HW2SW_CQ failed (%d) for CQN %06lx\n", ret, 193 hr_cq->cqn); 194 195 xa_erase(&cq_table->array, hr_cq->cqn); 196 197 /* Waiting interrupt process procedure carried out */ 198 synchronize_irq(hr_dev->eq_table.eq[hr_cq->vector].irq); 199 200 /* wait for all interrupt processed */ 201 if (atomic_dec_and_test(&hr_cq->refcount)) 202 complete(&hr_cq->free); 203 wait_for_completion(&hr_cq->free); 204 205 hns_roce_table_put(hr_dev, &cq_table->table, hr_cq->cqn); 206 hns_roce_bitmap_free(&cq_table->bitmap, hr_cq->cqn, BITMAP_NO_RR); 207 } 208 EXPORT_SYMBOL_GPL(hns_roce_free_cq); 209 210 static int hns_roce_ib_get_cq_umem(struct hns_roce_dev *hr_dev, 211 struct ib_udata *udata, 212 struct hns_roce_cq_buf *buf, 213 struct ib_umem **umem, u64 buf_addr, int cqe) 214 { 215 int ret; 216 u32 page_shift; 217 u32 npages; 218 219 *umem = ib_umem_get(udata, buf_addr, cqe * hr_dev->caps.cq_entry_sz, 220 IB_ACCESS_LOCAL_WRITE, 1); 221 if (IS_ERR(*umem)) 222 return PTR_ERR(*umem); 223 224 if (hns_roce_check_whether_mhop(hr_dev, HEM_TYPE_CQE)) 225 buf->hr_mtt.mtt_type = MTT_TYPE_CQE; 226 else 227 buf->hr_mtt.mtt_type = MTT_TYPE_WQE; 228 229 if (hr_dev->caps.cqe_buf_pg_sz) { 230 npages = (ib_umem_page_count(*umem) + 231 (1 << hr_dev->caps.cqe_buf_pg_sz) - 1) / 232 (1 << hr_dev->caps.cqe_buf_pg_sz); 233 page_shift = PAGE_SHIFT + hr_dev->caps.cqe_buf_pg_sz; 234 ret = hns_roce_mtt_init(hr_dev, npages, page_shift, 235 &buf->hr_mtt); 236 } else { 237 ret = hns_roce_mtt_init(hr_dev, ib_umem_page_count(*umem), 238 (*umem)->page_shift, 239 &buf->hr_mtt); 240 } 241 if (ret) 242 goto err_buf; 243 244 ret = hns_roce_ib_umem_write_mtt(hr_dev, &buf->hr_mtt, *umem); 245 if (ret) 246 goto err_mtt; 247 248 return 0; 249 250 err_mtt: 251 hns_roce_mtt_cleanup(hr_dev, &buf->hr_mtt); 252 253 err_buf: 254 ib_umem_release(*umem); 255 return ret; 256 } 257 258 static int hns_roce_ib_alloc_cq_buf(struct hns_roce_dev *hr_dev, 259 struct hns_roce_cq_buf *buf, u32 nent) 260 { 261 int ret; 262 u32 page_shift = PAGE_SHIFT + hr_dev->caps.cqe_buf_pg_sz; 263 264 ret = hns_roce_buf_alloc(hr_dev, nent * hr_dev->caps.cq_entry_sz, 265 (1 << page_shift) * 2, &buf->hr_buf, 266 page_shift); 267 if (ret) 268 goto out; 269 270 if (hns_roce_check_whether_mhop(hr_dev, HEM_TYPE_CQE)) 271 buf->hr_mtt.mtt_type = MTT_TYPE_CQE; 272 else 273 buf->hr_mtt.mtt_type = MTT_TYPE_WQE; 274 275 ret = hns_roce_mtt_init(hr_dev, buf->hr_buf.npages, 276 buf->hr_buf.page_shift, &buf->hr_mtt); 277 if (ret) 278 goto err_buf; 279 280 ret = hns_roce_buf_write_mtt(hr_dev, &buf->hr_mtt, &buf->hr_buf); 281 if (ret) 282 goto err_mtt; 283 284 return 0; 285 286 err_mtt: 287 hns_roce_mtt_cleanup(hr_dev, &buf->hr_mtt); 288 289 err_buf: 290 hns_roce_buf_free(hr_dev, nent * hr_dev->caps.cq_entry_sz, 291 &buf->hr_buf); 292 out: 293 return ret; 294 } 295 296 static void hns_roce_ib_free_cq_buf(struct hns_roce_dev *hr_dev, 297 struct hns_roce_cq_buf *buf, int cqe) 298 { 299 hns_roce_buf_free(hr_dev, (cqe + 1) * hr_dev->caps.cq_entry_sz, 300 &buf->hr_buf); 301 } 302 303 struct ib_cq *hns_roce_ib_create_cq(struct ib_device *ib_dev, 304 const struct ib_cq_init_attr *attr, 305 struct ib_udata *udata) 306 { 307 struct hns_roce_dev *hr_dev = to_hr_dev(ib_dev); 308 struct device *dev = hr_dev->dev; 309 struct hns_roce_ib_create_cq ucmd; 310 struct hns_roce_ib_create_cq_resp resp = {}; 311 struct hns_roce_cq *hr_cq = NULL; 312 struct hns_roce_uar *uar = NULL; 313 int vector = attr->comp_vector; 314 int cq_entries = attr->cqe; 315 int ret; 316 struct hns_roce_ucontext *context = rdma_udata_to_drv_context( 317 udata, struct hns_roce_ucontext, ibucontext); 318 319 if (cq_entries < 1 || cq_entries > hr_dev->caps.max_cqes) { 320 dev_err(dev, "Creat CQ failed. entries=%d, max=%d\n", 321 cq_entries, hr_dev->caps.max_cqes); 322 return ERR_PTR(-EINVAL); 323 } 324 325 hr_cq = kzalloc(sizeof(*hr_cq), GFP_KERNEL); 326 if (!hr_cq) 327 return ERR_PTR(-ENOMEM); 328 329 if (hr_dev->caps.min_cqes) 330 cq_entries = max(cq_entries, hr_dev->caps.min_cqes); 331 332 cq_entries = roundup_pow_of_two((unsigned int)cq_entries); 333 hr_cq->ib_cq.cqe = cq_entries - 1; 334 spin_lock_init(&hr_cq->lock); 335 336 if (udata) { 337 if (ib_copy_from_udata(&ucmd, udata, sizeof(ucmd))) { 338 dev_err(dev, "Failed to copy_from_udata.\n"); 339 ret = -EFAULT; 340 goto err_cq; 341 } 342 343 /* Get user space address, write it into mtt table */ 344 ret = hns_roce_ib_get_cq_umem(hr_dev, udata, &hr_cq->hr_buf, 345 &hr_cq->umem, ucmd.buf_addr, 346 cq_entries); 347 if (ret) { 348 dev_err(dev, "Failed to get_cq_umem.\n"); 349 goto err_cq; 350 } 351 352 if ((hr_dev->caps.flags & HNS_ROCE_CAP_FLAG_RECORD_DB) && 353 (udata->outlen >= sizeof(resp))) { 354 ret = hns_roce_db_map_user(context, udata, ucmd.db_addr, 355 &hr_cq->db); 356 if (ret) { 357 dev_err(dev, "cq record doorbell map failed!\n"); 358 goto err_mtt; 359 } 360 hr_cq->db_en = 1; 361 resp.cap_flags |= HNS_ROCE_SUPPORT_CQ_RECORD_DB; 362 } 363 364 /* Get user space parameters */ 365 uar = &context->uar; 366 } else { 367 if (hr_dev->caps.flags & HNS_ROCE_CAP_FLAG_RECORD_DB) { 368 ret = hns_roce_alloc_db(hr_dev, &hr_cq->db, 1); 369 if (ret) 370 goto err_cq; 371 372 hr_cq->set_ci_db = hr_cq->db.db_record; 373 *hr_cq->set_ci_db = 0; 374 hr_cq->db_en = 1; 375 } 376 377 /* Init mmt table and write buff address to mtt table */ 378 ret = hns_roce_ib_alloc_cq_buf(hr_dev, &hr_cq->hr_buf, 379 cq_entries); 380 if (ret) { 381 dev_err(dev, "Failed to alloc_cq_buf.\n"); 382 goto err_db; 383 } 384 385 uar = &hr_dev->priv_uar; 386 hr_cq->cq_db_l = hr_dev->reg_base + hr_dev->odb_offset + 387 DB_REG_OFFSET * uar->index; 388 } 389 390 /* Allocate cq index, fill cq_context */ 391 ret = hns_roce_cq_alloc(hr_dev, cq_entries, &hr_cq->hr_buf.hr_mtt, uar, 392 hr_cq, vector); 393 if (ret) { 394 dev_err(dev, "Creat CQ .Failed to cq_alloc.\n"); 395 goto err_dbmap; 396 } 397 398 /* 399 * For the QP created by kernel space, tptr value should be initialized 400 * to zero; For the QP created by user space, it will cause synchronous 401 * problems if tptr is set to zero here, so we initialze it in user 402 * space. 403 */ 404 if (!udata && hr_cq->tptr_addr) 405 *hr_cq->tptr_addr = 0; 406 407 /* Get created cq handler and carry out event */ 408 hr_cq->comp = hns_roce_ib_cq_comp; 409 hr_cq->event = hns_roce_ib_cq_event; 410 hr_cq->cq_depth = cq_entries; 411 412 if (udata) { 413 resp.cqn = hr_cq->cqn; 414 ret = ib_copy_to_udata(udata, &resp, sizeof(resp)); 415 if (ret) 416 goto err_cqc; 417 } 418 419 return &hr_cq->ib_cq; 420 421 err_cqc: 422 hns_roce_free_cq(hr_dev, hr_cq); 423 424 err_dbmap: 425 if (udata && (hr_dev->caps.flags & HNS_ROCE_CAP_FLAG_RECORD_DB) && 426 (udata->outlen >= sizeof(resp))) 427 hns_roce_db_unmap_user(context, &hr_cq->db); 428 429 err_mtt: 430 hns_roce_mtt_cleanup(hr_dev, &hr_cq->hr_buf.hr_mtt); 431 if (udata) 432 ib_umem_release(hr_cq->umem); 433 else 434 hns_roce_ib_free_cq_buf(hr_dev, &hr_cq->hr_buf, 435 hr_cq->ib_cq.cqe); 436 437 err_db: 438 if (!udata && (hr_dev->caps.flags & HNS_ROCE_CAP_FLAG_RECORD_DB)) 439 hns_roce_free_db(hr_dev, &hr_cq->db); 440 441 err_cq: 442 kfree(hr_cq); 443 return ERR_PTR(ret); 444 } 445 EXPORT_SYMBOL_GPL(hns_roce_ib_create_cq); 446 447 int hns_roce_ib_destroy_cq(struct ib_cq *ib_cq, struct ib_udata *udata) 448 { 449 struct hns_roce_dev *hr_dev = to_hr_dev(ib_cq->device); 450 struct hns_roce_cq *hr_cq = to_hr_cq(ib_cq); 451 int ret = 0; 452 453 if (hr_dev->hw->destroy_cq) { 454 ret = hr_dev->hw->destroy_cq(ib_cq, udata); 455 } else { 456 hns_roce_free_cq(hr_dev, hr_cq); 457 hns_roce_mtt_cleanup(hr_dev, &hr_cq->hr_buf.hr_mtt); 458 459 if (udata) { 460 ib_umem_release(hr_cq->umem); 461 462 if (hr_cq->db_en == 1) 463 hns_roce_db_unmap_user( 464 rdma_udata_to_drv_context( 465 udata, 466 struct hns_roce_ucontext, 467 ibucontext), 468 &hr_cq->db); 469 } else { 470 /* Free the buff of stored cq */ 471 hns_roce_ib_free_cq_buf(hr_dev, &hr_cq->hr_buf, 472 ib_cq->cqe); 473 if (hr_dev->caps.flags & HNS_ROCE_CAP_FLAG_RECORD_DB) 474 hns_roce_free_db(hr_dev, &hr_cq->db); 475 } 476 477 kfree(hr_cq); 478 } 479 480 return ret; 481 } 482 EXPORT_SYMBOL_GPL(hns_roce_ib_destroy_cq); 483 484 void hns_roce_cq_completion(struct hns_roce_dev *hr_dev, u32 cqn) 485 { 486 struct device *dev = hr_dev->dev; 487 struct hns_roce_cq *cq; 488 489 cq = xa_load(&hr_dev->cq_table.array, cqn & (hr_dev->caps.num_cqs - 1)); 490 if (!cq) { 491 dev_warn(dev, "Completion event for bogus CQ 0x%08x\n", cqn); 492 return; 493 } 494 495 ++cq->arm_sn; 496 cq->comp(cq); 497 } 498 EXPORT_SYMBOL_GPL(hns_roce_cq_completion); 499 500 void hns_roce_cq_event(struct hns_roce_dev *hr_dev, u32 cqn, int event_type) 501 { 502 struct hns_roce_cq_table *cq_table = &hr_dev->cq_table; 503 struct device *dev = hr_dev->dev; 504 struct hns_roce_cq *cq; 505 506 cq = xa_load(&cq_table->array, cqn & (hr_dev->caps.num_cqs - 1)); 507 if (cq) 508 atomic_inc(&cq->refcount); 509 510 if (!cq) { 511 dev_warn(dev, "Async event for bogus CQ %08x\n", cqn); 512 return; 513 } 514 515 cq->event(cq, (enum hns_roce_event)event_type); 516 517 if (atomic_dec_and_test(&cq->refcount)) 518 complete(&cq->free); 519 } 520 EXPORT_SYMBOL_GPL(hns_roce_cq_event); 521 522 int hns_roce_init_cq_table(struct hns_roce_dev *hr_dev) 523 { 524 struct hns_roce_cq_table *cq_table = &hr_dev->cq_table; 525 526 xa_init(&cq_table->array); 527 528 return hns_roce_bitmap_init(&cq_table->bitmap, hr_dev->caps.num_cqs, 529 hr_dev->caps.num_cqs - 1, 530 hr_dev->caps.reserved_cqs, 0); 531 } 532 533 void hns_roce_cleanup_cq_table(struct hns_roce_dev *hr_dev) 534 { 535 hns_roce_bitmap_cleanup(&hr_dev->cq_table.bitmap); 536 } 537