1 // SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB 2 /* 3 * Copyright (c) 2018 Hisilicon Limited. 4 */ 5 6 #include <linux/pci.h> 7 #include <rdma/ib_umem.h> 8 #include "hns_roce_device.h" 9 #include "hns_roce_cmd.h" 10 #include "hns_roce_hem.h" 11 12 void hns_roce_srq_event(struct hns_roce_dev *hr_dev, u32 srqn, int event_type) 13 { 14 struct hns_roce_srq_table *srq_table = &hr_dev->srq_table; 15 struct hns_roce_srq *srq; 16 17 xa_lock(&srq_table->xa); 18 srq = xa_load(&srq_table->xa, srqn & (hr_dev->caps.num_srqs - 1)); 19 if (srq) 20 atomic_inc(&srq->refcount); 21 xa_unlock(&srq_table->xa); 22 23 if (!srq) { 24 dev_warn(hr_dev->dev, "Async event for bogus SRQ %08x\n", srqn); 25 return; 26 } 27 28 srq->event(srq, event_type); 29 30 if (atomic_dec_and_test(&srq->refcount)) 31 complete(&srq->free); 32 } 33 34 static void hns_roce_ib_srq_event(struct hns_roce_srq *srq, 35 enum hns_roce_event event_type) 36 { 37 struct hns_roce_dev *hr_dev = to_hr_dev(srq->ibsrq.device); 38 struct ib_srq *ibsrq = &srq->ibsrq; 39 struct ib_event event; 40 41 if (ibsrq->event_handler) { 42 event.device = ibsrq->device; 43 event.element.srq = ibsrq; 44 switch (event_type) { 45 case HNS_ROCE_EVENT_TYPE_SRQ_LIMIT_REACH: 46 event.event = IB_EVENT_SRQ_LIMIT_REACHED; 47 break; 48 case HNS_ROCE_EVENT_TYPE_SRQ_CATAS_ERROR: 49 event.event = IB_EVENT_SRQ_ERR; 50 break; 51 default: 52 dev_err(hr_dev->dev, 53 "hns_roce:Unexpected event type 0x%x on SRQ %06lx\n", 54 event_type, srq->srqn); 55 return; 56 } 57 58 ibsrq->event_handler(&event, ibsrq->srq_context); 59 } 60 } 61 62 static int hns_roce_hw_create_srq(struct hns_roce_dev *dev, 63 struct hns_roce_cmd_mailbox *mailbox, 64 unsigned long srq_num) 65 { 66 return hns_roce_cmd_mbox(dev, mailbox->dma, 0, srq_num, 0, 67 HNS_ROCE_CMD_CREATE_SRQ, 68 HNS_ROCE_CMD_TIMEOUT_MSECS); 69 } 70 71 static int hns_roce_hw_destroy_srq(struct hns_roce_dev *dev, 72 struct hns_roce_cmd_mailbox *mailbox, 73 unsigned long srq_num) 74 { 75 return hns_roce_cmd_mbox(dev, 0, mailbox ? mailbox->dma : 0, srq_num, 76 mailbox ? 0 : 1, HNS_ROCE_CMD_DESTROY_SRQ, 77 HNS_ROCE_CMD_TIMEOUT_MSECS); 78 } 79 80 static int alloc_srqc(struct hns_roce_dev *hr_dev, struct hns_roce_srq *srq) 81 { 82 struct hns_roce_srq_table *srq_table = &hr_dev->srq_table; 83 struct ib_device *ibdev = &hr_dev->ib_dev; 84 struct hns_roce_cmd_mailbox *mailbox; 85 int ret; 86 87 ret = hns_roce_bitmap_alloc(&srq_table->bitmap, &srq->srqn); 88 if (ret) { 89 ibdev_err(ibdev, "failed to alloc SRQ number.\n"); 90 return -ENOMEM; 91 } 92 93 ret = hns_roce_table_get(hr_dev, &srq_table->table, srq->srqn); 94 if (ret) { 95 ibdev_err(ibdev, "failed to get SRQC table, ret = %d.\n", ret); 96 goto err_out; 97 } 98 99 ret = xa_err(xa_store(&srq_table->xa, srq->srqn, srq, GFP_KERNEL)); 100 if (ret) { 101 ibdev_err(ibdev, "failed to store SRQC, ret = %d.\n", ret); 102 goto err_put; 103 } 104 105 mailbox = hns_roce_alloc_cmd_mailbox(hr_dev); 106 if (IS_ERR_OR_NULL(mailbox)) { 107 ibdev_err(ibdev, "failed to alloc mailbox for SRQC.\n"); 108 ret = -ENOMEM; 109 goto err_xa; 110 } 111 112 ret = hr_dev->hw->write_srqc(srq, mailbox->buf); 113 if (ret) { 114 ibdev_err(ibdev, "failed to write SRQC.\n"); 115 goto err_mbox; 116 } 117 118 ret = hns_roce_hw_create_srq(hr_dev, mailbox, srq->srqn); 119 if (ret) { 120 ibdev_err(ibdev, "failed to config SRQC, ret = %d.\n", ret); 121 goto err_mbox; 122 } 123 124 hns_roce_free_cmd_mailbox(hr_dev, mailbox); 125 126 return 0; 127 128 err_mbox: 129 hns_roce_free_cmd_mailbox(hr_dev, mailbox); 130 err_xa: 131 xa_erase(&srq_table->xa, srq->srqn); 132 err_put: 133 hns_roce_table_put(hr_dev, &srq_table->table, srq->srqn); 134 err_out: 135 hns_roce_bitmap_free(&srq_table->bitmap, srq->srqn, BITMAP_NO_RR); 136 137 return ret; 138 } 139 140 static void free_srqc(struct hns_roce_dev *hr_dev, struct hns_roce_srq *srq) 141 { 142 struct hns_roce_srq_table *srq_table = &hr_dev->srq_table; 143 int ret; 144 145 ret = hns_roce_hw_destroy_srq(hr_dev, NULL, srq->srqn); 146 if (ret) 147 dev_err(hr_dev->dev, "DESTROY_SRQ failed (%d) for SRQN %06lx\n", 148 ret, srq->srqn); 149 150 xa_erase(&srq_table->xa, srq->srqn); 151 152 if (atomic_dec_and_test(&srq->refcount)) 153 complete(&srq->free); 154 wait_for_completion(&srq->free); 155 156 hns_roce_table_put(hr_dev, &srq_table->table, srq->srqn); 157 hns_roce_bitmap_free(&srq_table->bitmap, srq->srqn, BITMAP_NO_RR); 158 } 159 160 static int alloc_srq_idx(struct hns_roce_dev *hr_dev, struct hns_roce_srq *srq, 161 struct ib_udata *udata, unsigned long addr) 162 { 163 struct hns_roce_idx_que *idx_que = &srq->idx_que; 164 struct ib_device *ibdev = &hr_dev->ib_dev; 165 struct hns_roce_buf_attr buf_attr = {}; 166 int ret; 167 168 srq->idx_que.entry_shift = ilog2(HNS_ROCE_IDX_QUE_ENTRY_SZ); 169 170 buf_attr.page_shift = hr_dev->caps.idx_buf_pg_sz + HNS_HW_PAGE_SHIFT; 171 buf_attr.region[0].size = to_hr_hem_entries_size(srq->wqe_cnt, 172 srq->idx_que.entry_shift); 173 buf_attr.region[0].hopnum = hr_dev->caps.idx_hop_num; 174 buf_attr.region_count = 1; 175 176 ret = hns_roce_mtr_create(hr_dev, &idx_que->mtr, &buf_attr, 177 hr_dev->caps.idx_ba_pg_sz + HNS_HW_PAGE_SHIFT, 178 udata, addr); 179 if (ret) { 180 ibdev_err(ibdev, 181 "failed to alloc SRQ idx mtr, ret = %d.\n", ret); 182 return ret; 183 } 184 185 if (!udata) { 186 idx_que->bitmap = bitmap_zalloc(srq->wqe_cnt, GFP_KERNEL); 187 if (!idx_que->bitmap) { 188 ibdev_err(ibdev, "failed to alloc SRQ idx bitmap.\n"); 189 ret = -ENOMEM; 190 goto err_idx_mtr; 191 } 192 } 193 194 idx_que->head = 0; 195 idx_que->tail = 0; 196 197 return 0; 198 err_idx_mtr: 199 hns_roce_mtr_destroy(hr_dev, &idx_que->mtr); 200 201 return ret; 202 } 203 204 static void free_srq_idx(struct hns_roce_dev *hr_dev, struct hns_roce_srq *srq) 205 { 206 struct hns_roce_idx_que *idx_que = &srq->idx_que; 207 208 bitmap_free(idx_que->bitmap); 209 idx_que->bitmap = NULL; 210 hns_roce_mtr_destroy(hr_dev, &idx_que->mtr); 211 } 212 213 static int alloc_srq_wqe_buf(struct hns_roce_dev *hr_dev, 214 struct hns_roce_srq *srq, 215 struct ib_udata *udata, unsigned long addr) 216 { 217 struct ib_device *ibdev = &hr_dev->ib_dev; 218 struct hns_roce_buf_attr buf_attr = {}; 219 int ret; 220 221 srq->wqe_shift = ilog2(roundup_pow_of_two(max(HNS_ROCE_SGE_SIZE, 222 HNS_ROCE_SGE_SIZE * 223 srq->max_gs))); 224 225 buf_attr.page_shift = hr_dev->caps.srqwqe_buf_pg_sz + HNS_HW_PAGE_SHIFT; 226 buf_attr.region[0].size = to_hr_hem_entries_size(srq->wqe_cnt, 227 srq->wqe_shift); 228 buf_attr.region[0].hopnum = hr_dev->caps.srqwqe_hop_num; 229 buf_attr.region_count = 1; 230 231 ret = hns_roce_mtr_create(hr_dev, &srq->buf_mtr, &buf_attr, 232 hr_dev->caps.srqwqe_ba_pg_sz + 233 HNS_HW_PAGE_SHIFT, udata, addr); 234 if (ret) 235 ibdev_err(ibdev, 236 "failed to alloc SRQ buf mtr, ret = %d.\n", ret); 237 238 return ret; 239 } 240 241 static void free_srq_wqe_buf(struct hns_roce_dev *hr_dev, 242 struct hns_roce_srq *srq) 243 { 244 hns_roce_mtr_destroy(hr_dev, &srq->buf_mtr); 245 } 246 247 static int alloc_srq_wrid(struct hns_roce_dev *hr_dev, struct hns_roce_srq *srq) 248 { 249 srq->wrid = kvmalloc_array(srq->wqe_cnt, sizeof(u64), GFP_KERNEL); 250 if (!srq->wrid) 251 return -ENOMEM; 252 253 return 0; 254 } 255 256 static void free_srq_wrid(struct hns_roce_srq *srq) 257 { 258 kfree(srq->wrid); 259 srq->wrid = NULL; 260 } 261 262 static u32 proc_srq_sge(struct hns_roce_dev *dev, struct hns_roce_srq *hr_srq, 263 bool user) 264 { 265 u32 max_sge = dev->caps.max_srq_sges; 266 267 if (dev->pci_dev->revision >= PCI_REVISION_ID_HIP09) 268 return max_sge; 269 270 /* Reserve SGEs only for HIP08 in kernel; The userspace driver will 271 * calculate number of max_sge with reserved SGEs when allocating wqe 272 * buf, so there is no need to do this again in kernel. But the number 273 * may exceed the capacity of SGEs recorded in the firmware, so the 274 * kernel driver should just adapt the value accordingly. 275 */ 276 if (user) 277 max_sge = roundup_pow_of_two(max_sge + 1); 278 else 279 hr_srq->rsv_sge = 1; 280 281 return max_sge; 282 } 283 284 static int set_srq_basic_param(struct hns_roce_srq *srq, 285 struct ib_srq_init_attr *init_attr, 286 struct ib_udata *udata) 287 { 288 struct hns_roce_dev *hr_dev = to_hr_dev(srq->ibsrq.device); 289 struct ib_srq_attr *attr = &init_attr->attr; 290 u32 max_sge; 291 292 max_sge = proc_srq_sge(hr_dev, srq, !!udata); 293 if (attr->max_wr > hr_dev->caps.max_srq_wrs || 294 attr->max_sge > max_sge) { 295 ibdev_err(&hr_dev->ib_dev, 296 "invalid SRQ attr, depth = %u, sge = %u.\n", 297 attr->max_wr, attr->max_sge); 298 return -EINVAL; 299 } 300 301 attr->max_wr = max_t(u32, attr->max_wr, HNS_ROCE_MIN_SRQ_WQE_NUM); 302 srq->wqe_cnt = roundup_pow_of_two(attr->max_wr); 303 srq->max_gs = roundup_pow_of_two(attr->max_sge + srq->rsv_sge); 304 305 attr->max_wr = srq->wqe_cnt; 306 attr->max_sge = srq->max_gs - srq->rsv_sge; 307 attr->srq_limit = 0; 308 309 return 0; 310 } 311 312 static void set_srq_ext_param(struct hns_roce_srq *srq, 313 struct ib_srq_init_attr *init_attr) 314 { 315 srq->cqn = ib_srq_has_cq(init_attr->srq_type) ? 316 to_hr_cq(init_attr->ext.cq)->cqn : 0; 317 } 318 319 static int set_srq_param(struct hns_roce_srq *srq, 320 struct ib_srq_init_attr *init_attr, 321 struct ib_udata *udata) 322 { 323 int ret; 324 325 ret = set_srq_basic_param(srq, init_attr, udata); 326 if (ret) 327 return ret; 328 329 set_srq_ext_param(srq, init_attr); 330 331 return 0; 332 } 333 334 static int alloc_srq_buf(struct hns_roce_dev *hr_dev, struct hns_roce_srq *srq, 335 struct ib_udata *udata) 336 { 337 struct hns_roce_ib_create_srq ucmd = {}; 338 int ret; 339 340 if (udata) { 341 ret = ib_copy_from_udata(&ucmd, udata, 342 min(udata->inlen, sizeof(ucmd))); 343 if (ret) { 344 ibdev_err(&hr_dev->ib_dev, 345 "failed to copy SRQ udata, ret = %d.\n", 346 ret); 347 return ret; 348 } 349 } 350 351 ret = alloc_srq_idx(hr_dev, srq, udata, ucmd.que_addr); 352 if (ret) 353 return ret; 354 355 ret = alloc_srq_wqe_buf(hr_dev, srq, udata, ucmd.buf_addr); 356 if (ret) 357 goto err_idx; 358 359 if (!udata) { 360 ret = alloc_srq_wrid(hr_dev, srq); 361 if (ret) 362 goto err_wqe_buf; 363 } 364 365 return 0; 366 367 err_wqe_buf: 368 free_srq_wqe_buf(hr_dev, srq); 369 err_idx: 370 free_srq_idx(hr_dev, srq); 371 372 return ret; 373 } 374 375 static void free_srq_buf(struct hns_roce_dev *hr_dev, struct hns_roce_srq *srq) 376 { 377 free_srq_wrid(srq); 378 free_srq_wqe_buf(hr_dev, srq); 379 free_srq_idx(hr_dev, srq); 380 } 381 382 int hns_roce_create_srq(struct ib_srq *ib_srq, 383 struct ib_srq_init_attr *init_attr, 384 struct ib_udata *udata) 385 { 386 struct hns_roce_dev *hr_dev = to_hr_dev(ib_srq->device); 387 struct hns_roce_ib_create_srq_resp resp = {}; 388 struct hns_roce_srq *srq = to_hr_srq(ib_srq); 389 int ret; 390 391 mutex_init(&srq->mutex); 392 spin_lock_init(&srq->lock); 393 394 ret = set_srq_param(srq, init_attr, udata); 395 if (ret) 396 return ret; 397 398 ret = alloc_srq_buf(hr_dev, srq, udata); 399 if (ret) 400 return ret; 401 402 ret = alloc_srqc(hr_dev, srq); 403 if (ret) 404 goto err_srq_buf; 405 406 if (udata) { 407 resp.srqn = srq->srqn; 408 if (ib_copy_to_udata(udata, &resp, 409 min(udata->outlen, sizeof(resp)))) { 410 ret = -EFAULT; 411 goto err_srqc; 412 } 413 } 414 415 srq->db_reg_l = hr_dev->reg_base + SRQ_DB_REG; 416 srq->event = hns_roce_ib_srq_event; 417 atomic_set(&srq->refcount, 1); 418 init_completion(&srq->free); 419 420 return 0; 421 422 err_srqc: 423 free_srqc(hr_dev, srq); 424 err_srq_buf: 425 free_srq_buf(hr_dev, srq); 426 427 return ret; 428 } 429 430 int hns_roce_destroy_srq(struct ib_srq *ibsrq, struct ib_udata *udata) 431 { 432 struct hns_roce_dev *hr_dev = to_hr_dev(ibsrq->device); 433 struct hns_roce_srq *srq = to_hr_srq(ibsrq); 434 435 free_srqc(hr_dev, srq); 436 free_srq_buf(hr_dev, srq); 437 return 0; 438 } 439 440 int hns_roce_init_srq_table(struct hns_roce_dev *hr_dev) 441 { 442 struct hns_roce_srq_table *srq_table = &hr_dev->srq_table; 443 444 xa_init(&srq_table->xa); 445 446 return hns_roce_bitmap_init(&srq_table->bitmap, hr_dev->caps.num_srqs, 447 hr_dev->caps.num_srqs - 1, 448 hr_dev->caps.reserved_srqs, 0); 449 } 450 451 void hns_roce_cleanup_srq_table(struct hns_roce_dev *hr_dev) 452 { 453 hns_roce_bitmap_cleanup(&hr_dev->srq_table.bitmap); 454 } 455