1 /*- 2 * Copyright (c) 2013-2020, Mellanox Technologies, Ltd. All rights reserved. 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * 1. Redistributions of source code must retain the above copyright 8 * notice, this list of conditions and the following disclaimer. 9 * 2. Redistributions in binary form must reproduce the above copyright 10 * notice, this list of conditions and the following disclaimer in the 11 * documentation and/or other materials provided with the distribution. 12 * 13 * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS `AS IS' AND 14 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 16 * ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE 17 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 18 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 19 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 20 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 21 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 22 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 23 * SUCH DAMAGE. 24 * 25 * $FreeBSD$ 26 */ 27 28 #include <linux/module.h> 29 #include <dev/mlx5/qp.h> 30 #include <dev/mlx5/srq.h> 31 #include <linux/slab.h> 32 #include <rdma/ib_umem.h> 33 #include <rdma/ib_user_verbs.h> 34 #include <rdma/uverbs_ioctl.h> 35 36 #include "mlx5_ib.h" 37 38 static void *get_wqe(struct mlx5_ib_srq *srq, int n) 39 { 40 return mlx5_buf_offset(&srq->buf, n << srq->msrq.wqe_shift); 41 } 42 43 static void mlx5_ib_srq_event(struct mlx5_core_srq *srq, int type) 44 { 45 struct ib_event event; 46 struct ib_srq *ibsrq = &to_mibsrq(srq)->ibsrq; 47 48 if (ibsrq->event_handler) { 49 event.device = ibsrq->device; 50 event.element.srq = ibsrq; 51 switch (type) { 52 case MLX5_EVENT_TYPE_SRQ_RQ_LIMIT: 53 event.event = IB_EVENT_SRQ_LIMIT_REACHED; 54 break; 55 case MLX5_EVENT_TYPE_SRQ_CATAS_ERROR: 56 event.event = IB_EVENT_SRQ_ERR; 57 break; 58 default: 59 pr_warn("mlx5_ib: Unexpected event type %d on SRQ %06x\n", 60 type, srq->srqn); 61 return; 62 } 63 64 ibsrq->event_handler(&event, ibsrq->srq_context); 65 } 66 } 67 68 static int create_srq_user(struct ib_pd *pd, struct mlx5_ib_srq *srq, 69 struct mlx5_srq_attr *in, 70 struct ib_udata *udata, int buf_size) 71 { 72 struct mlx5_ib_dev *dev = to_mdev(pd->device); 73 struct mlx5_ib_create_srq ucmd = {}; 74 struct mlx5_ib_ucontext *ucontext = rdma_udata_to_drv_context( 75 udata, struct mlx5_ib_ucontext, ibucontext); 76 size_t ucmdlen; 77 int err; 78 int npages; 79 int page_shift; 80 int ncont; 81 u32 offset; 82 u32 uidx = MLX5_IB_DEFAULT_UIDX; 83 84 ucmdlen = min(udata->inlen, sizeof(ucmd)); 85 86 if (ib_copy_from_udata(&ucmd, udata, ucmdlen)) { 87 mlx5_ib_dbg(dev, "failed copy udata\n"); 88 return -EFAULT; 89 } 90 91 if (ucmd.reserved0 || ucmd.reserved1) 92 return -EINVAL; 93 94 if (udata->inlen > sizeof(ucmd) && 95 !ib_is_udata_cleared(udata, sizeof(ucmd), 96 udata->inlen - sizeof(ucmd))) 97 return -EINVAL; 98 99 if (in->type != IB_SRQT_BASIC) { 100 err = get_srq_user_index(ucontext, &ucmd, udata->inlen, &uidx); 101 if (err) 102 return err; 103 } 104 105 srq->wq_sig = !!(ucmd.flags & MLX5_SRQ_FLAG_SIGNATURE); 106 107 srq->umem = ib_umem_get(&ucontext->ibucontext, ucmd.buf_addr, buf_size, 0, 0); 108 if (IS_ERR(srq->umem)) { 109 mlx5_ib_dbg(dev, "failed umem get, size %d\n", buf_size); 110 err = PTR_ERR(srq->umem); 111 return err; 112 } 113 114 mlx5_ib_cont_pages(srq->umem, ucmd.buf_addr, 0, &npages, 115 &page_shift, &ncont, NULL); 116 err = mlx5_ib_get_buf_offset(ucmd.buf_addr, page_shift, 117 &offset); 118 if (err) { 119 mlx5_ib_warn(dev, "bad offset\n"); 120 goto err_umem; 121 } 122 123 in->pas = mlx5_vzalloc(sizeof(*in->pas) * ncont); 124 if (!in->pas) { 125 err = -ENOMEM; 126 goto err_umem; 127 } 128 129 mlx5_ib_populate_pas(dev, srq->umem, page_shift, in->pas, 0); 130 131 err = mlx5_ib_db_map_user(ucontext, ucmd.db_addr, &srq->db); 132 if (err) { 133 mlx5_ib_dbg(dev, "map doorbell failed\n"); 134 goto err_in; 135 } 136 137 in->log_page_size = page_shift - MLX5_ADAPTER_PAGE_SHIFT; 138 in->page_offset = offset; 139 in->uid = (in->type != IB_SRQT_XRC) ? to_mpd(pd)->uid : 0; 140 if (MLX5_CAP_GEN(dev->mdev, cqe_version) == MLX5_CQE_VERSION_V1 && 141 in->type != IB_SRQT_BASIC) 142 in->user_index = uidx; 143 144 return 0; 145 146 err_in: 147 kvfree(in->pas); 148 149 err_umem: 150 ib_umem_release(srq->umem); 151 152 return err; 153 } 154 155 static int create_srq_kernel(struct mlx5_ib_dev *dev, struct mlx5_ib_srq *srq, 156 struct mlx5_srq_attr *in, int buf_size) 157 { 158 int err; 159 int i; 160 struct mlx5_wqe_srq_next_seg *next; 161 162 err = mlx5_db_alloc(dev->mdev, &srq->db); 163 if (err) { 164 mlx5_ib_warn(dev, "alloc dbell rec failed\n"); 165 return err; 166 } 167 168 if (mlx5_buf_alloc(dev->mdev, buf_size, 2 * PAGE_SIZE, &srq->buf)) { 169 mlx5_ib_dbg(dev, "buf alloc failed\n"); 170 err = -ENOMEM; 171 goto err_db; 172 } 173 174 srq->head = 0; 175 srq->tail = srq->msrq.max - 1; 176 srq->wqe_ctr = 0; 177 178 for (i = 0; i < srq->msrq.max; i++) { 179 next = get_wqe(srq, i); 180 next->next_wqe_index = 181 cpu_to_be16((i + 1) & (srq->msrq.max - 1)); 182 } 183 184 mlx5_ib_dbg(dev, "srq->buf.page_shift = %d\n", srq->buf.page_shift); 185 in->pas = mlx5_vzalloc(sizeof(*in->pas) * srq->buf.npages); 186 if (!in->pas) { 187 err = -ENOMEM; 188 goto err_buf; 189 } 190 mlx5_fill_page_array(&srq->buf, in->pas); 191 192 srq->wrid = kmalloc(srq->msrq.max * sizeof(u64), GFP_KERNEL); 193 if (!srq->wrid) { 194 err = -ENOMEM; 195 goto err_in; 196 } 197 srq->wq_sig = 0; 198 199 in->log_page_size = srq->buf.page_shift - MLX5_ADAPTER_PAGE_SHIFT; 200 if (MLX5_CAP_GEN(dev->mdev, cqe_version) == MLX5_CQE_VERSION_V1 && 201 in->type != IB_SRQT_BASIC) 202 in->user_index = MLX5_IB_DEFAULT_UIDX; 203 204 return 0; 205 206 err_in: 207 kvfree(in->pas); 208 209 err_buf: 210 mlx5_buf_free(dev->mdev, &srq->buf); 211 212 err_db: 213 mlx5_db_free(dev->mdev, &srq->db); 214 return err; 215 } 216 217 static void destroy_srq_user(struct ib_pd *pd, struct mlx5_ib_srq *srq, 218 struct ib_udata *udata) 219 { 220 mlx5_ib_db_unmap_user( 221 rdma_udata_to_drv_context( 222 udata, 223 struct mlx5_ib_ucontext, 224 ibucontext), 225 &srq->db); 226 ib_umem_release(srq->umem); 227 } 228 229 230 static void destroy_srq_kernel(struct mlx5_ib_dev *dev, struct mlx5_ib_srq *srq) 231 { 232 kfree(srq->wrid); 233 mlx5_buf_free(dev->mdev, &srq->buf); 234 mlx5_db_free(dev->mdev, &srq->db); 235 } 236 237 int mlx5_ib_create_srq(struct ib_srq *ib_srq, 238 struct ib_srq_init_attr *init_attr, 239 struct ib_udata *udata) 240 { 241 struct mlx5_ib_dev *dev = to_mdev(ib_srq->device); 242 struct mlx5_ib_srq *srq = to_msrq(ib_srq); 243 size_t desc_size; 244 size_t buf_size; 245 int err; 246 struct mlx5_srq_attr in = {}; 247 __u32 max_srq_wqes = 1 << MLX5_CAP_GEN(dev->mdev, log_max_srq_sz); 248 249 /* Sanity check SRQ size before proceeding */ 250 if (init_attr->attr.max_wr >= max_srq_wqes) { 251 mlx5_ib_dbg(dev, "max_wr %d, cap %d\n", 252 init_attr->attr.max_wr, 253 max_srq_wqes); 254 return -EINVAL; 255 } 256 257 mutex_init(&srq->mutex); 258 spin_lock_init(&srq->lock); 259 srq->msrq.max = roundup_pow_of_two(init_attr->attr.max_wr + 1); 260 srq->msrq.max_gs = init_attr->attr.max_sge; 261 262 desc_size = sizeof(struct mlx5_wqe_srq_next_seg) + 263 srq->msrq.max_gs * sizeof(struct mlx5_wqe_data_seg); 264 if (desc_size == 0 || srq->msrq.max_gs > desc_size) 265 return -EINVAL; 266 267 desc_size = roundup_pow_of_two(desc_size); 268 desc_size = max_t(size_t, 32, desc_size); 269 if (desc_size < sizeof(struct mlx5_wqe_srq_next_seg)) 270 return -EINVAL; 271 272 srq->msrq.max_avail_gather = (desc_size - sizeof(struct mlx5_wqe_srq_next_seg)) / 273 sizeof(struct mlx5_wqe_data_seg); 274 srq->msrq.wqe_shift = ilog2(desc_size); 275 buf_size = srq->msrq.max * desc_size; 276 if (buf_size < desc_size) 277 return -EINVAL; 278 279 in.type = init_attr->srq_type; 280 281 if (udata) 282 err = create_srq_user(ib_srq->pd, srq, &in, udata, buf_size); 283 else 284 err = create_srq_kernel(dev, srq, &in, buf_size); 285 286 if (err) { 287 mlx5_ib_warn(dev, "create srq %s failed, err %d\n", 288 udata ? "user" : "kernel", err); 289 return err; 290 } 291 292 in.log_size = ilog2(srq->msrq.max); 293 in.wqe_shift = srq->msrq.wqe_shift - 4; 294 if (srq->wq_sig) 295 in.flags |= MLX5_SRQ_FLAG_WQ_SIG; 296 297 if (init_attr->srq_type == IB_SRQT_XRC) 298 in.xrcd = to_mxrcd(init_attr->ext.xrc.xrcd)->xrcdn; 299 else 300 in.xrcd = to_mxrcd(dev->devr.x0)->xrcdn; 301 302 if (ib_srq_has_cq(init_attr->srq_type)) 303 in.cqn = to_mcq(init_attr->ext.cq)->mcq.cqn; 304 else 305 in.cqn = to_mcq(dev->devr.c0)->mcq.cqn; 306 307 in.pd = to_mpd(ib_srq->pd)->pdn; 308 in.db_record = srq->db.dma; 309 err = mlx5_core_create_srq(dev->mdev, &srq->msrq, &in); 310 kvfree(in.pas); 311 if (err) { 312 mlx5_ib_dbg(dev, "create SRQ failed, err %d\n", err); 313 goto err_usr_kern_srq; 314 } 315 316 mlx5_ib_dbg(dev, "create SRQ with srqn 0x%x\n", srq->msrq.srqn); 317 318 srq->msrq.event = mlx5_ib_srq_event; 319 srq->ibsrq.ext.xrc.srq_num = srq->msrq.srqn; 320 321 if (udata) 322 if (ib_copy_to_udata(udata, &srq->msrq.srqn, sizeof(__u32))) { 323 mlx5_ib_dbg(dev, "copy to user failed\n"); 324 err = -EFAULT; 325 goto err_core; 326 } 327 328 init_attr->attr.max_wr = srq->msrq.max - 1; 329 330 return 0; 331 332 err_core: 333 mlx5_core_destroy_srq(dev->mdev, &srq->msrq); 334 335 err_usr_kern_srq: 336 if (udata) 337 destroy_srq_user(ib_srq->pd, srq, udata); 338 else 339 destroy_srq_kernel(dev, srq); 340 341 return err; 342 } 343 344 int mlx5_ib_modify_srq(struct ib_srq *ibsrq, struct ib_srq_attr *attr, 345 enum ib_srq_attr_mask attr_mask, struct ib_udata *udata) 346 { 347 struct mlx5_ib_dev *dev = to_mdev(ibsrq->device); 348 struct mlx5_ib_srq *srq = to_msrq(ibsrq); 349 int ret; 350 351 /* We don't support resizing SRQs yet */ 352 if (attr_mask & IB_SRQ_MAX_WR) 353 return -EINVAL; 354 355 if (attr_mask & IB_SRQ_LIMIT) { 356 if (attr->srq_limit >= srq->msrq.max) 357 return -EINVAL; 358 359 mutex_lock(&srq->mutex); 360 ret = mlx5_core_arm_srq(dev->mdev, &srq->msrq, attr->srq_limit, 1); 361 mutex_unlock(&srq->mutex); 362 363 if (ret) 364 return ret; 365 } 366 367 return 0; 368 } 369 370 int mlx5_ib_query_srq(struct ib_srq *ibsrq, struct ib_srq_attr *srq_attr) 371 { 372 struct mlx5_ib_dev *dev = to_mdev(ibsrq->device); 373 struct mlx5_ib_srq *srq = to_msrq(ibsrq); 374 int ret; 375 struct mlx5_srq_attr *out; 376 377 out = kzalloc(sizeof(*out), GFP_KERNEL); 378 if (!out) 379 return -ENOMEM; 380 381 ret = mlx5_core_query_srq(dev->mdev, &srq->msrq, out); 382 if (ret) 383 goto out_box; 384 385 srq_attr->srq_limit = out->lwm; 386 srq_attr->max_wr = srq->msrq.max - 1; 387 srq_attr->max_sge = srq->msrq.max_gs; 388 389 out_box: 390 kfree(out); 391 return ret; 392 } 393 394 void mlx5_ib_destroy_srq(struct ib_srq *srq, struct ib_udata *udata) 395 { 396 struct mlx5_ib_dev *dev = to_mdev(srq->device); 397 struct mlx5_ib_srq *msrq = to_msrq(srq); 398 399 mlx5_core_destroy_srq(dev->mdev, &msrq->msrq); 400 401 if (srq->uobject) { 402 mlx5_ib_db_unmap_user( 403 rdma_udata_to_drv_context( 404 udata, 405 struct mlx5_ib_ucontext, 406 ibucontext), 407 &msrq->db); 408 ib_umem_release(msrq->umem); 409 } else { 410 destroy_srq_kernel(dev, msrq); 411 } 412 } 413 414 void mlx5_ib_free_srq_wqe(struct mlx5_ib_srq *srq, int wqe_index) 415 { 416 struct mlx5_wqe_srq_next_seg *next; 417 418 /* always called with interrupts disabled. */ 419 spin_lock(&srq->lock); 420 421 next = get_wqe(srq, srq->tail); 422 next->next_wqe_index = cpu_to_be16(wqe_index); 423 srq->tail = wqe_index; 424 425 spin_unlock(&srq->lock); 426 } 427 428 int mlx5_ib_post_srq_recv(struct ib_srq *ibsrq, const struct ib_recv_wr *wr, 429 const struct ib_recv_wr **bad_wr) 430 { 431 struct mlx5_ib_srq *srq = to_msrq(ibsrq); 432 struct mlx5_wqe_srq_next_seg *next; 433 struct mlx5_wqe_data_seg *scat; 434 struct mlx5_ib_dev *dev = to_mdev(ibsrq->device); 435 struct mlx5_core_dev *mdev = dev->mdev; 436 unsigned long flags; 437 int err = 0; 438 int nreq; 439 int i; 440 441 spin_lock_irqsave(&srq->lock, flags); 442 443 if (mdev->state == MLX5_DEVICE_STATE_INTERNAL_ERROR) { 444 err = -EIO; 445 *bad_wr = wr; 446 goto out; 447 } 448 449 for (nreq = 0; wr; nreq++, wr = wr->next) { 450 if (unlikely(wr->num_sge > srq->msrq.max_gs)) { 451 err = -EINVAL; 452 *bad_wr = wr; 453 break; 454 } 455 456 if (unlikely(srq->head == srq->tail)) { 457 err = -ENOMEM; 458 *bad_wr = wr; 459 break; 460 } 461 462 srq->wrid[srq->head] = wr->wr_id; 463 464 next = get_wqe(srq, srq->head); 465 srq->head = be16_to_cpu(next->next_wqe_index); 466 scat = (struct mlx5_wqe_data_seg *)(next + 1); 467 468 for (i = 0; i < wr->num_sge; i++) { 469 scat[i].byte_count = cpu_to_be32(wr->sg_list[i].length); 470 scat[i].lkey = cpu_to_be32(wr->sg_list[i].lkey); 471 scat[i].addr = cpu_to_be64(wr->sg_list[i].addr); 472 } 473 474 if (i < srq->msrq.max_avail_gather) { 475 scat[i].byte_count = 0; 476 scat[i].lkey = cpu_to_be32(MLX5_INVALID_LKEY); 477 scat[i].addr = 0; 478 } 479 } 480 481 if (likely(nreq)) { 482 srq->wqe_ctr += nreq; 483 484 /* Make sure that descriptors are written before 485 * doorbell record. 486 */ 487 wmb(); 488 489 *srq->db.db = cpu_to_be32(srq->wqe_ctr); 490 } 491 out: 492 spin_unlock_irqrestore(&srq->lock, flags); 493 494 return err; 495 } 496