1 /* 2 * Copyright (c) 2013-2015, Mellanox Technologies. All rights reserved. 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/module.h> 34 #include <linux/mlx5/qp.h> 35 #include <linux/mlx5/srq.h> 36 #include <linux/slab.h> 37 #include <rdma/ib_umem.h> 38 #include <rdma/ib_user_verbs.h> 39 40 #include "mlx5_ib.h" 41 #include "user.h" 42 43 /* not supported currently */ 44 static int srq_signature; 45 46 static void *get_wqe(struct mlx5_ib_srq *srq, int n) 47 { 48 return mlx5_buf_offset(&srq->buf, n << srq->msrq.wqe_shift); 49 } 50 51 static void mlx5_ib_srq_event(struct mlx5_core_srq *srq, enum mlx5_event type) 52 { 53 struct ib_event event; 54 struct ib_srq *ibsrq = &to_mibsrq(srq)->ibsrq; 55 56 if (ibsrq->event_handler) { 57 event.device = ibsrq->device; 58 event.element.srq = ibsrq; 59 switch (type) { 60 case MLX5_EVENT_TYPE_SRQ_RQ_LIMIT: 61 event.event = IB_EVENT_SRQ_LIMIT_REACHED; 62 break; 63 case MLX5_EVENT_TYPE_SRQ_CATAS_ERROR: 64 event.event = IB_EVENT_SRQ_ERR; 65 break; 66 default: 67 pr_warn("mlx5_ib: Unexpected event type %d on SRQ %06x\n", 68 type, srq->srqn); 69 return; 70 } 71 72 ibsrq->event_handler(&event, ibsrq->srq_context); 73 } 74 } 75 76 static int create_srq_user(struct ib_pd *pd, struct mlx5_ib_srq *srq, 77 struct mlx5_create_srq_mbox_in **in, 78 struct ib_udata *udata, int buf_size, int *inlen) 79 { 80 struct mlx5_ib_dev *dev = to_mdev(pd->device); 81 struct mlx5_ib_create_srq ucmd = {}; 82 size_t ucmdlen; 83 void *xsrqc; 84 int err; 85 int npages; 86 int page_shift; 87 int ncont; 88 u32 offset; 89 u32 uidx = MLX5_IB_DEFAULT_UIDX; 90 int drv_data = udata->inlen - sizeof(struct ib_uverbs_cmd_hdr); 91 92 if (drv_data < 0) 93 return -EINVAL; 94 95 ucmdlen = (drv_data < sizeof(ucmd)) ? 96 drv_data : sizeof(ucmd); 97 98 if (ib_copy_from_udata(&ucmd, udata, ucmdlen)) { 99 mlx5_ib_dbg(dev, "failed copy udata\n"); 100 return -EFAULT; 101 } 102 103 if (ucmd.reserved0 || ucmd.reserved1) 104 return -EINVAL; 105 106 if (drv_data > sizeof(ucmd) && 107 !ib_is_udata_cleared(udata, sizeof(ucmd), 108 drv_data - sizeof(ucmd))) 109 return -EINVAL; 110 111 err = get_srq_user_index(to_mucontext(pd->uobject->context), 112 &ucmd, udata->inlen, &uidx); 113 if (err) 114 return err; 115 116 srq->wq_sig = !!(ucmd.flags & MLX5_SRQ_FLAG_SIGNATURE); 117 118 srq->umem = ib_umem_get(pd->uobject->context, ucmd.buf_addr, buf_size, 119 0, 0); 120 if (IS_ERR(srq->umem)) { 121 mlx5_ib_dbg(dev, "failed umem get, size %d\n", buf_size); 122 err = PTR_ERR(srq->umem); 123 return err; 124 } 125 126 mlx5_ib_cont_pages(srq->umem, ucmd.buf_addr, &npages, 127 &page_shift, &ncont, NULL); 128 err = mlx5_ib_get_buf_offset(ucmd.buf_addr, page_shift, 129 &offset); 130 if (err) { 131 mlx5_ib_warn(dev, "bad offset\n"); 132 goto err_umem; 133 } 134 135 *inlen = sizeof(**in) + sizeof(*(*in)->pas) * ncont; 136 *in = mlx5_vzalloc(*inlen); 137 if (!(*in)) { 138 err = -ENOMEM; 139 goto err_umem; 140 } 141 142 mlx5_ib_populate_pas(dev, srq->umem, page_shift, (*in)->pas, 0); 143 144 err = mlx5_ib_db_map_user(to_mucontext(pd->uobject->context), 145 ucmd.db_addr, &srq->db); 146 if (err) { 147 mlx5_ib_dbg(dev, "map doorbell failed\n"); 148 goto err_in; 149 } 150 151 (*in)->ctx.log_pg_sz = page_shift - MLX5_ADAPTER_PAGE_SHIFT; 152 (*in)->ctx.pgoff_cqn = cpu_to_be32(offset << 26); 153 154 if (MLX5_CAP_GEN(dev->mdev, cqe_version) == MLX5_CQE_VERSION_V1) { 155 xsrqc = MLX5_ADDR_OF(create_xrc_srq_in, *in, 156 xrc_srq_context_entry); 157 MLX5_SET(xrc_srqc, xsrqc, user_index, uidx); 158 } 159 160 return 0; 161 162 err_in: 163 kvfree(*in); 164 165 err_umem: 166 ib_umem_release(srq->umem); 167 168 return err; 169 } 170 171 static int create_srq_kernel(struct mlx5_ib_dev *dev, struct mlx5_ib_srq *srq, 172 struct mlx5_create_srq_mbox_in **in, int buf_size, 173 int *inlen) 174 { 175 int err; 176 int i; 177 struct mlx5_wqe_srq_next_seg *next; 178 int page_shift; 179 int npages; 180 void *xsrqc; 181 182 err = mlx5_db_alloc(dev->mdev, &srq->db); 183 if (err) { 184 mlx5_ib_warn(dev, "alloc dbell rec failed\n"); 185 return err; 186 } 187 188 if (mlx5_buf_alloc(dev->mdev, buf_size, &srq->buf)) { 189 mlx5_ib_dbg(dev, "buf alloc failed\n"); 190 err = -ENOMEM; 191 goto err_db; 192 } 193 page_shift = srq->buf.page_shift; 194 195 srq->head = 0; 196 srq->tail = srq->msrq.max - 1; 197 srq->wqe_ctr = 0; 198 199 for (i = 0; i < srq->msrq.max; i++) { 200 next = get_wqe(srq, i); 201 next->next_wqe_index = 202 cpu_to_be16((i + 1) & (srq->msrq.max - 1)); 203 } 204 205 npages = DIV_ROUND_UP(srq->buf.npages, 1 << (page_shift - PAGE_SHIFT)); 206 mlx5_ib_dbg(dev, "buf_size %d, page_shift %d, npages %d, calc npages %d\n", 207 buf_size, page_shift, srq->buf.npages, npages); 208 *inlen = sizeof(**in) + sizeof(*(*in)->pas) * npages; 209 *in = mlx5_vzalloc(*inlen); 210 if (!*in) { 211 err = -ENOMEM; 212 goto err_buf; 213 } 214 mlx5_fill_page_array(&srq->buf, (*in)->pas); 215 216 srq->wrid = kmalloc(srq->msrq.max * sizeof(u64), GFP_KERNEL); 217 if (!srq->wrid) { 218 mlx5_ib_dbg(dev, "kmalloc failed %lu\n", 219 (unsigned long)(srq->msrq.max * sizeof(u64))); 220 err = -ENOMEM; 221 goto err_in; 222 } 223 srq->wq_sig = !!srq_signature; 224 225 (*in)->ctx.log_pg_sz = page_shift - MLX5_ADAPTER_PAGE_SHIFT; 226 227 if (MLX5_CAP_GEN(dev->mdev, cqe_version) == MLX5_CQE_VERSION_V1) { 228 xsrqc = MLX5_ADDR_OF(create_xrc_srq_in, *in, 229 xrc_srq_context_entry); 230 /* 0xffffff means we ask to work with cqe version 0 */ 231 MLX5_SET(xrc_srqc, xsrqc, user_index, MLX5_IB_DEFAULT_UIDX); 232 } 233 234 return 0; 235 236 err_in: 237 kvfree(*in); 238 239 err_buf: 240 mlx5_buf_free(dev->mdev, &srq->buf); 241 242 err_db: 243 mlx5_db_free(dev->mdev, &srq->db); 244 return err; 245 } 246 247 static void destroy_srq_user(struct ib_pd *pd, struct mlx5_ib_srq *srq) 248 { 249 mlx5_ib_db_unmap_user(to_mucontext(pd->uobject->context), &srq->db); 250 ib_umem_release(srq->umem); 251 } 252 253 254 static void destroy_srq_kernel(struct mlx5_ib_dev *dev, struct mlx5_ib_srq *srq) 255 { 256 kfree(srq->wrid); 257 mlx5_buf_free(dev->mdev, &srq->buf); 258 mlx5_db_free(dev->mdev, &srq->db); 259 } 260 261 struct ib_srq *mlx5_ib_create_srq(struct ib_pd *pd, 262 struct ib_srq_init_attr *init_attr, 263 struct ib_udata *udata) 264 { 265 struct mlx5_ib_dev *dev = to_mdev(pd->device); 266 struct mlx5_ib_srq *srq; 267 int desc_size; 268 int buf_size; 269 int err; 270 struct mlx5_create_srq_mbox_in *uninitialized_var(in); 271 int uninitialized_var(inlen); 272 int is_xrc; 273 u32 flgs, xrcdn; 274 __u32 max_srq_wqes = 1 << MLX5_CAP_GEN(dev->mdev, log_max_srq_sz); 275 276 /* Sanity check SRQ size before proceeding */ 277 if (init_attr->attr.max_wr >= max_srq_wqes) { 278 mlx5_ib_dbg(dev, "max_wr %d, cap %d\n", 279 init_attr->attr.max_wr, 280 max_srq_wqes); 281 return ERR_PTR(-EINVAL); 282 } 283 284 srq = kmalloc(sizeof(*srq), GFP_KERNEL); 285 if (!srq) 286 return ERR_PTR(-ENOMEM); 287 288 mutex_init(&srq->mutex); 289 spin_lock_init(&srq->lock); 290 srq->msrq.max = roundup_pow_of_two(init_attr->attr.max_wr + 1); 291 srq->msrq.max_gs = init_attr->attr.max_sge; 292 293 desc_size = sizeof(struct mlx5_wqe_srq_next_seg) + 294 srq->msrq.max_gs * sizeof(struct mlx5_wqe_data_seg); 295 desc_size = roundup_pow_of_two(desc_size); 296 desc_size = max_t(int, 32, desc_size); 297 srq->msrq.max_avail_gather = (desc_size - sizeof(struct mlx5_wqe_srq_next_seg)) / 298 sizeof(struct mlx5_wqe_data_seg); 299 srq->msrq.wqe_shift = ilog2(desc_size); 300 buf_size = srq->msrq.max * desc_size; 301 mlx5_ib_dbg(dev, "desc_size 0x%x, req wr 0x%x, srq size 0x%x, max_gs 0x%x, max_avail_gather 0x%x\n", 302 desc_size, init_attr->attr.max_wr, srq->msrq.max, srq->msrq.max_gs, 303 srq->msrq.max_avail_gather); 304 305 if (pd->uobject) 306 err = create_srq_user(pd, srq, &in, udata, buf_size, &inlen); 307 else 308 err = create_srq_kernel(dev, srq, &in, buf_size, &inlen); 309 310 if (err) { 311 mlx5_ib_warn(dev, "create srq %s failed, err %d\n", 312 pd->uobject ? "user" : "kernel", err); 313 goto err_srq; 314 } 315 316 is_xrc = (init_attr->srq_type == IB_SRQT_XRC); 317 in->ctx.state_log_sz = ilog2(srq->msrq.max); 318 flgs = ((srq->msrq.wqe_shift - 4) | (is_xrc << 5) | (srq->wq_sig << 7)) << 24; 319 xrcdn = 0; 320 if (is_xrc) { 321 xrcdn = to_mxrcd(init_attr->ext.xrc.xrcd)->xrcdn; 322 in->ctx.pgoff_cqn |= cpu_to_be32(to_mcq(init_attr->ext.xrc.cq)->mcq.cqn); 323 } else if (init_attr->srq_type == IB_SRQT_BASIC) { 324 xrcdn = to_mxrcd(dev->devr.x0)->xrcdn; 325 in->ctx.pgoff_cqn |= cpu_to_be32(to_mcq(dev->devr.c0)->mcq.cqn); 326 } 327 328 in->ctx.flags_xrcd = cpu_to_be32((flgs & 0xFF000000) | (xrcdn & 0xFFFFFF)); 329 330 in->ctx.pd = cpu_to_be32(to_mpd(pd)->pdn); 331 in->ctx.db_record = cpu_to_be64(srq->db.dma); 332 err = mlx5_core_create_srq(dev->mdev, &srq->msrq, in, inlen, is_xrc); 333 kvfree(in); 334 if (err) { 335 mlx5_ib_dbg(dev, "create SRQ failed, err %d\n", err); 336 goto err_usr_kern_srq; 337 } 338 339 mlx5_ib_dbg(dev, "create SRQ with srqn 0x%x\n", srq->msrq.srqn); 340 341 srq->msrq.event = mlx5_ib_srq_event; 342 srq->ibsrq.ext.xrc.srq_num = srq->msrq.srqn; 343 344 if (pd->uobject) 345 if (ib_copy_to_udata(udata, &srq->msrq.srqn, sizeof(__u32))) { 346 mlx5_ib_dbg(dev, "copy to user failed\n"); 347 err = -EFAULT; 348 goto err_core; 349 } 350 351 init_attr->attr.max_wr = srq->msrq.max - 1; 352 353 return &srq->ibsrq; 354 355 err_core: 356 mlx5_core_destroy_srq(dev->mdev, &srq->msrq); 357 358 err_usr_kern_srq: 359 if (pd->uobject) 360 destroy_srq_user(pd, srq); 361 else 362 destroy_srq_kernel(dev, srq); 363 364 err_srq: 365 kfree(srq); 366 367 return ERR_PTR(err); 368 } 369 370 int mlx5_ib_modify_srq(struct ib_srq *ibsrq, struct ib_srq_attr *attr, 371 enum ib_srq_attr_mask attr_mask, struct ib_udata *udata) 372 { 373 struct mlx5_ib_dev *dev = to_mdev(ibsrq->device); 374 struct mlx5_ib_srq *srq = to_msrq(ibsrq); 375 int ret; 376 377 /* We don't support resizing SRQs yet */ 378 if (attr_mask & IB_SRQ_MAX_WR) 379 return -EINVAL; 380 381 if (attr_mask & IB_SRQ_LIMIT) { 382 if (attr->srq_limit >= srq->msrq.max) 383 return -EINVAL; 384 385 mutex_lock(&srq->mutex); 386 ret = mlx5_core_arm_srq(dev->mdev, &srq->msrq, attr->srq_limit, 1); 387 mutex_unlock(&srq->mutex); 388 389 if (ret) 390 return ret; 391 } 392 393 return 0; 394 } 395 396 int mlx5_ib_query_srq(struct ib_srq *ibsrq, struct ib_srq_attr *srq_attr) 397 { 398 struct mlx5_ib_dev *dev = to_mdev(ibsrq->device); 399 struct mlx5_ib_srq *srq = to_msrq(ibsrq); 400 int ret; 401 struct mlx5_query_srq_mbox_out *out; 402 403 out = kzalloc(sizeof(*out), GFP_KERNEL); 404 if (!out) 405 return -ENOMEM; 406 407 ret = mlx5_core_query_srq(dev->mdev, &srq->msrq, out); 408 if (ret) 409 goto out_box; 410 411 srq_attr->srq_limit = be16_to_cpu(out->ctx.lwm); 412 srq_attr->max_wr = srq->msrq.max - 1; 413 srq_attr->max_sge = srq->msrq.max_gs; 414 415 out_box: 416 kfree(out); 417 return ret; 418 } 419 420 int mlx5_ib_destroy_srq(struct ib_srq *srq) 421 { 422 struct mlx5_ib_dev *dev = to_mdev(srq->device); 423 struct mlx5_ib_srq *msrq = to_msrq(srq); 424 425 mlx5_core_destroy_srq(dev->mdev, &msrq->msrq); 426 427 if (srq->uobject) { 428 mlx5_ib_db_unmap_user(to_mucontext(srq->uobject->context), &msrq->db); 429 ib_umem_release(msrq->umem); 430 } else { 431 destroy_srq_kernel(dev, msrq); 432 } 433 434 kfree(srq); 435 return 0; 436 } 437 438 void mlx5_ib_free_srq_wqe(struct mlx5_ib_srq *srq, int wqe_index) 439 { 440 struct mlx5_wqe_srq_next_seg *next; 441 442 /* always called with interrupts disabled. */ 443 spin_lock(&srq->lock); 444 445 next = get_wqe(srq, srq->tail); 446 next->next_wqe_index = cpu_to_be16(wqe_index); 447 srq->tail = wqe_index; 448 449 spin_unlock(&srq->lock); 450 } 451 452 int mlx5_ib_post_srq_recv(struct ib_srq *ibsrq, struct ib_recv_wr *wr, 453 struct ib_recv_wr **bad_wr) 454 { 455 struct mlx5_ib_srq *srq = to_msrq(ibsrq); 456 struct mlx5_wqe_srq_next_seg *next; 457 struct mlx5_wqe_data_seg *scat; 458 unsigned long flags; 459 int err = 0; 460 int nreq; 461 int i; 462 463 spin_lock_irqsave(&srq->lock, flags); 464 465 for (nreq = 0; wr; nreq++, wr = wr->next) { 466 if (unlikely(wr->num_sge > srq->msrq.max_gs)) { 467 err = -EINVAL; 468 *bad_wr = wr; 469 break; 470 } 471 472 if (unlikely(srq->head == srq->tail)) { 473 err = -ENOMEM; 474 *bad_wr = wr; 475 break; 476 } 477 478 srq->wrid[srq->head] = wr->wr_id; 479 480 next = get_wqe(srq, srq->head); 481 srq->head = be16_to_cpu(next->next_wqe_index); 482 scat = (struct mlx5_wqe_data_seg *)(next + 1); 483 484 for (i = 0; i < wr->num_sge; i++) { 485 scat[i].byte_count = cpu_to_be32(wr->sg_list[i].length); 486 scat[i].lkey = cpu_to_be32(wr->sg_list[i].lkey); 487 scat[i].addr = cpu_to_be64(wr->sg_list[i].addr); 488 } 489 490 if (i < srq->msrq.max_avail_gather) { 491 scat[i].byte_count = 0; 492 scat[i].lkey = cpu_to_be32(MLX5_INVALID_LKEY); 493 scat[i].addr = 0; 494 } 495 } 496 497 if (likely(nreq)) { 498 srq->wqe_ctr += nreq; 499 500 /* Make sure that descriptors are written before 501 * doorbell record. 502 */ 503 wmb(); 504 505 *srq->db.db = cpu_to_be32(srq->wqe_ctr); 506 } 507 508 spin_unlock_irqrestore(&srq->lock, flags); 509 510 return err; 511 } 512