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 int is_xrc) 80 { 81 struct mlx5_ib_dev *dev = to_mdev(pd->device); 82 struct mlx5_ib_create_srq ucmd = {}; 83 size_t ucmdlen; 84 void *xsrqc; 85 int err; 86 int npages; 87 int page_shift; 88 int ncont; 89 u32 offset; 90 u32 uidx = MLX5_IB_DEFAULT_UIDX; 91 92 ucmdlen = min(udata->inlen, sizeof(ucmd)); 93 94 if (ib_copy_from_udata(&ucmd, udata, ucmdlen)) { 95 mlx5_ib_dbg(dev, "failed copy udata\n"); 96 return -EFAULT; 97 } 98 99 if (ucmd.reserved0 || ucmd.reserved1) 100 return -EINVAL; 101 102 if (udata->inlen > sizeof(ucmd) && 103 !ib_is_udata_cleared(udata, sizeof(ucmd), 104 udata->inlen - sizeof(ucmd))) 105 return -EINVAL; 106 107 if (is_xrc) { 108 err = get_srq_user_index(to_mucontext(pd->uobject->context), 109 &ucmd, udata->inlen, &uidx); 110 if (err) 111 return err; 112 } 113 114 srq->wq_sig = !!(ucmd.flags & MLX5_SRQ_FLAG_SIGNATURE); 115 116 srq->umem = ib_umem_get(pd->uobject->context, ucmd.buf_addr, buf_size, 117 0, 0); 118 if (IS_ERR(srq->umem)) { 119 mlx5_ib_dbg(dev, "failed umem get, size %d\n", buf_size); 120 err = PTR_ERR(srq->umem); 121 return err; 122 } 123 124 mlx5_ib_cont_pages(srq->umem, ucmd.buf_addr, &npages, 125 &page_shift, &ncont, NULL); 126 err = mlx5_ib_get_buf_offset(ucmd.buf_addr, page_shift, 127 &offset); 128 if (err) { 129 mlx5_ib_warn(dev, "bad offset\n"); 130 goto err_umem; 131 } 132 133 *inlen = sizeof(**in) + sizeof(*(*in)->pas) * ncont; 134 *in = mlx5_vzalloc(*inlen); 135 if (!(*in)) { 136 err = -ENOMEM; 137 goto err_umem; 138 } 139 140 mlx5_ib_populate_pas(dev, srq->umem, page_shift, (*in)->pas, 0); 141 142 err = mlx5_ib_db_map_user(to_mucontext(pd->uobject->context), 143 ucmd.db_addr, &srq->db); 144 if (err) { 145 mlx5_ib_dbg(dev, "map doorbell failed\n"); 146 goto err_in; 147 } 148 149 (*in)->ctx.log_pg_sz = page_shift - MLX5_ADAPTER_PAGE_SHIFT; 150 (*in)->ctx.pgoff_cqn = cpu_to_be32(offset << 26); 151 152 if ((MLX5_CAP_GEN(dev->mdev, cqe_version) == MLX5_CQE_VERSION_V1) && 153 is_xrc){ 154 xsrqc = MLX5_ADDR_OF(create_xrc_srq_in, *in, 155 xrc_srq_context_entry); 156 MLX5_SET(xrc_srqc, xsrqc, user_index, uidx); 157 } 158 159 return 0; 160 161 err_in: 162 kvfree(*in); 163 164 err_umem: 165 ib_umem_release(srq->umem); 166 167 return err; 168 } 169 170 static int create_srq_kernel(struct mlx5_ib_dev *dev, struct mlx5_ib_srq *srq, 171 struct mlx5_create_srq_mbox_in **in, int buf_size, 172 int *inlen, int is_xrc) 173 { 174 int err; 175 int i; 176 struct mlx5_wqe_srq_next_seg *next; 177 int page_shift; 178 int npages; 179 void *xsrqc; 180 181 err = mlx5_db_alloc(dev->mdev, &srq->db); 182 if (err) { 183 mlx5_ib_warn(dev, "alloc dbell rec failed\n"); 184 return err; 185 } 186 187 if (mlx5_buf_alloc(dev->mdev, buf_size, &srq->buf)) { 188 mlx5_ib_dbg(dev, "buf alloc failed\n"); 189 err = -ENOMEM; 190 goto err_db; 191 } 192 page_shift = srq->buf.page_shift; 193 194 srq->head = 0; 195 srq->tail = srq->msrq.max - 1; 196 srq->wqe_ctr = 0; 197 198 for (i = 0; i < srq->msrq.max; i++) { 199 next = get_wqe(srq, i); 200 next->next_wqe_index = 201 cpu_to_be16((i + 1) & (srq->msrq.max - 1)); 202 } 203 204 npages = DIV_ROUND_UP(srq->buf.npages, 1 << (page_shift - PAGE_SHIFT)); 205 mlx5_ib_dbg(dev, "buf_size %d, page_shift %d, npages %d, calc npages %d\n", 206 buf_size, page_shift, srq->buf.npages, npages); 207 *inlen = sizeof(**in) + sizeof(*(*in)->pas) * npages; 208 *in = mlx5_vzalloc(*inlen); 209 if (!*in) { 210 err = -ENOMEM; 211 goto err_buf; 212 } 213 mlx5_fill_page_array(&srq->buf, (*in)->pas); 214 215 srq->wrid = kmalloc(srq->msrq.max * sizeof(u64), GFP_KERNEL); 216 if (!srq->wrid) { 217 mlx5_ib_dbg(dev, "kmalloc failed %lu\n", 218 (unsigned long)(srq->msrq.max * sizeof(u64))); 219 err = -ENOMEM; 220 goto err_in; 221 } 222 srq->wq_sig = !!srq_signature; 223 224 (*in)->ctx.log_pg_sz = page_shift - MLX5_ADAPTER_PAGE_SHIFT; 225 226 if ((MLX5_CAP_GEN(dev->mdev, cqe_version) == MLX5_CQE_VERSION_V1) && 227 is_xrc){ 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 is_xrc = (init_attr->srq_type == IB_SRQT_XRC); 306 307 if (pd->uobject) 308 err = create_srq_user(pd, srq, &in, udata, buf_size, &inlen, 309 is_xrc); 310 else 311 err = create_srq_kernel(dev, srq, &in, buf_size, &inlen, 312 is_xrc); 313 314 if (err) { 315 mlx5_ib_warn(dev, "create srq %s failed, err %d\n", 316 pd->uobject ? "user" : "kernel", err); 317 goto err_srq; 318 } 319 320 in->ctx.state_log_sz = ilog2(srq->msrq.max); 321 flgs = ((srq->msrq.wqe_shift - 4) | (is_xrc << 5) | (srq->wq_sig << 7)) << 24; 322 xrcdn = 0; 323 if (is_xrc) { 324 xrcdn = to_mxrcd(init_attr->ext.xrc.xrcd)->xrcdn; 325 in->ctx.pgoff_cqn |= cpu_to_be32(to_mcq(init_attr->ext.xrc.cq)->mcq.cqn); 326 } else if (init_attr->srq_type == IB_SRQT_BASIC) { 327 xrcdn = to_mxrcd(dev->devr.x0)->xrcdn; 328 in->ctx.pgoff_cqn |= cpu_to_be32(to_mcq(dev->devr.c0)->mcq.cqn); 329 } 330 331 in->ctx.flags_xrcd = cpu_to_be32((flgs & 0xFF000000) | (xrcdn & 0xFFFFFF)); 332 333 in->ctx.pd = cpu_to_be32(to_mpd(pd)->pdn); 334 in->ctx.db_record = cpu_to_be64(srq->db.dma); 335 err = mlx5_core_create_srq(dev->mdev, &srq->msrq, in, inlen, is_xrc); 336 kvfree(in); 337 if (err) { 338 mlx5_ib_dbg(dev, "create SRQ failed, err %d\n", err); 339 goto err_usr_kern_srq; 340 } 341 342 mlx5_ib_dbg(dev, "create SRQ with srqn 0x%x\n", srq->msrq.srqn); 343 344 srq->msrq.event = mlx5_ib_srq_event; 345 srq->ibsrq.ext.xrc.srq_num = srq->msrq.srqn; 346 347 if (pd->uobject) 348 if (ib_copy_to_udata(udata, &srq->msrq.srqn, sizeof(__u32))) { 349 mlx5_ib_dbg(dev, "copy to user failed\n"); 350 err = -EFAULT; 351 goto err_core; 352 } 353 354 init_attr->attr.max_wr = srq->msrq.max - 1; 355 356 return &srq->ibsrq; 357 358 err_core: 359 mlx5_core_destroy_srq(dev->mdev, &srq->msrq); 360 361 err_usr_kern_srq: 362 if (pd->uobject) 363 destroy_srq_user(pd, srq); 364 else 365 destroy_srq_kernel(dev, srq); 366 367 err_srq: 368 kfree(srq); 369 370 return ERR_PTR(err); 371 } 372 373 int mlx5_ib_modify_srq(struct ib_srq *ibsrq, struct ib_srq_attr *attr, 374 enum ib_srq_attr_mask attr_mask, struct ib_udata *udata) 375 { 376 struct mlx5_ib_dev *dev = to_mdev(ibsrq->device); 377 struct mlx5_ib_srq *srq = to_msrq(ibsrq); 378 int ret; 379 380 /* We don't support resizing SRQs yet */ 381 if (attr_mask & IB_SRQ_MAX_WR) 382 return -EINVAL; 383 384 if (attr_mask & IB_SRQ_LIMIT) { 385 if (attr->srq_limit >= srq->msrq.max) 386 return -EINVAL; 387 388 mutex_lock(&srq->mutex); 389 ret = mlx5_core_arm_srq(dev->mdev, &srq->msrq, attr->srq_limit, 1); 390 mutex_unlock(&srq->mutex); 391 392 if (ret) 393 return ret; 394 } 395 396 return 0; 397 } 398 399 int mlx5_ib_query_srq(struct ib_srq *ibsrq, struct ib_srq_attr *srq_attr) 400 { 401 struct mlx5_ib_dev *dev = to_mdev(ibsrq->device); 402 struct mlx5_ib_srq *srq = to_msrq(ibsrq); 403 int ret; 404 struct mlx5_query_srq_mbox_out *out; 405 406 out = kzalloc(sizeof(*out), GFP_KERNEL); 407 if (!out) 408 return -ENOMEM; 409 410 ret = mlx5_core_query_srq(dev->mdev, &srq->msrq, out); 411 if (ret) 412 goto out_box; 413 414 srq_attr->srq_limit = be16_to_cpu(out->ctx.lwm); 415 srq_attr->max_wr = srq->msrq.max - 1; 416 srq_attr->max_sge = srq->msrq.max_gs; 417 418 out_box: 419 kfree(out); 420 return ret; 421 } 422 423 int mlx5_ib_destroy_srq(struct ib_srq *srq) 424 { 425 struct mlx5_ib_dev *dev = to_mdev(srq->device); 426 struct mlx5_ib_srq *msrq = to_msrq(srq); 427 428 mlx5_core_destroy_srq(dev->mdev, &msrq->msrq); 429 430 if (srq->uobject) { 431 mlx5_ib_db_unmap_user(to_mucontext(srq->uobject->context), &msrq->db); 432 ib_umem_release(msrq->umem); 433 } else { 434 destroy_srq_kernel(dev, msrq); 435 } 436 437 kfree(srq); 438 return 0; 439 } 440 441 void mlx5_ib_free_srq_wqe(struct mlx5_ib_srq *srq, int wqe_index) 442 { 443 struct mlx5_wqe_srq_next_seg *next; 444 445 /* always called with interrupts disabled. */ 446 spin_lock(&srq->lock); 447 448 next = get_wqe(srq, srq->tail); 449 next->next_wqe_index = cpu_to_be16(wqe_index); 450 srq->tail = wqe_index; 451 452 spin_unlock(&srq->lock); 453 } 454 455 int mlx5_ib_post_srq_recv(struct ib_srq *ibsrq, struct ib_recv_wr *wr, 456 struct ib_recv_wr **bad_wr) 457 { 458 struct mlx5_ib_srq *srq = to_msrq(ibsrq); 459 struct mlx5_wqe_srq_next_seg *next; 460 struct mlx5_wqe_data_seg *scat; 461 unsigned long flags; 462 int err = 0; 463 int nreq; 464 int i; 465 466 spin_lock_irqsave(&srq->lock, flags); 467 468 for (nreq = 0; wr; nreq++, wr = wr->next) { 469 if (unlikely(wr->num_sge > srq->msrq.max_gs)) { 470 err = -EINVAL; 471 *bad_wr = wr; 472 break; 473 } 474 475 if (unlikely(srq->head == srq->tail)) { 476 err = -ENOMEM; 477 *bad_wr = wr; 478 break; 479 } 480 481 srq->wrid[srq->head] = wr->wr_id; 482 483 next = get_wqe(srq, srq->head); 484 srq->head = be16_to_cpu(next->next_wqe_index); 485 scat = (struct mlx5_wqe_data_seg *)(next + 1); 486 487 for (i = 0; i < wr->num_sge; i++) { 488 scat[i].byte_count = cpu_to_be32(wr->sg_list[i].length); 489 scat[i].lkey = cpu_to_be32(wr->sg_list[i].lkey); 490 scat[i].addr = cpu_to_be64(wr->sg_list[i].addr); 491 } 492 493 if (i < srq->msrq.max_avail_gather) { 494 scat[i].byte_count = 0; 495 scat[i].lkey = cpu_to_be32(MLX5_INVALID_LKEY); 496 scat[i].addr = 0; 497 } 498 } 499 500 if (likely(nreq)) { 501 srq->wqe_ctr += nreq; 502 503 /* Make sure that descriptors are written before 504 * doorbell record. 505 */ 506 wmb(); 507 508 *srq->db.db = cpu_to_be32(srq->wqe_ctr); 509 } 510 511 spin_unlock_irqrestore(&srq->lock, flags); 512 513 return err; 514 } 515