1 /* 2 * Copyright (c) 2013, Mellanox Technologies inc. 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 int err; 84 int npages; 85 int page_shift; 86 int ncont; 87 u32 offset; 88 89 ucmdlen = 90 (udata->inlen - sizeof(struct ib_uverbs_cmd_hdr) < 91 sizeof(ucmd)) ? (sizeof(ucmd) - 92 sizeof(ucmd.reserved)) : 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 (ucmdlen == sizeof(ucmd) && 100 ucmd.reserved != 0) 101 return -EINVAL; 102 103 srq->wq_sig = !!(ucmd.flags & MLX5_SRQ_FLAG_SIGNATURE); 104 105 srq->umem = ib_umem_get(pd->uobject->context, ucmd.buf_addr, buf_size, 106 0, 0); 107 if (IS_ERR(srq->umem)) { 108 mlx5_ib_dbg(dev, "failed umem get, size %d\n", buf_size); 109 err = PTR_ERR(srq->umem); 110 return err; 111 } 112 113 mlx5_ib_cont_pages(srq->umem, ucmd.buf_addr, &npages, 114 &page_shift, &ncont, NULL); 115 err = mlx5_ib_get_buf_offset(ucmd.buf_addr, page_shift, 116 &offset); 117 if (err) { 118 mlx5_ib_warn(dev, "bad offset\n"); 119 goto err_umem; 120 } 121 122 *inlen = sizeof(**in) + sizeof(*(*in)->pas) * ncont; 123 *in = mlx5_vzalloc(*inlen); 124 if (!(*in)) { 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(to_mucontext(pd->uobject->context), 132 ucmd.db_addr, &srq->db); 133 if (err) { 134 mlx5_ib_dbg(dev, "map doorbell failed\n"); 135 goto err_in; 136 } 137 138 (*in)->ctx.log_pg_sz = page_shift - MLX5_ADAPTER_PAGE_SHIFT; 139 (*in)->ctx.pgoff_cqn = cpu_to_be32(offset << 26); 140 141 return 0; 142 143 err_in: 144 kvfree(*in); 145 146 err_umem: 147 ib_umem_release(srq->umem); 148 149 return err; 150 } 151 152 static int create_srq_kernel(struct mlx5_ib_dev *dev, struct mlx5_ib_srq *srq, 153 struct mlx5_create_srq_mbox_in **in, int buf_size, 154 int *inlen) 155 { 156 int err; 157 int i; 158 struct mlx5_wqe_srq_next_seg *next; 159 int page_shift; 160 int npages; 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 *srq->db.db = 0; 169 170 if (mlx5_buf_alloc(dev->mdev, buf_size, PAGE_SIZE * 2, &srq->buf)) { 171 mlx5_ib_dbg(dev, "buf alloc failed\n"); 172 err = -ENOMEM; 173 goto err_db; 174 } 175 page_shift = srq->buf.page_shift; 176 177 srq->head = 0; 178 srq->tail = srq->msrq.max - 1; 179 srq->wqe_ctr = 0; 180 181 for (i = 0; i < srq->msrq.max; i++) { 182 next = get_wqe(srq, i); 183 next->next_wqe_index = 184 cpu_to_be16((i + 1) & (srq->msrq.max - 1)); 185 } 186 187 npages = DIV_ROUND_UP(srq->buf.npages, 1 << (page_shift - PAGE_SHIFT)); 188 mlx5_ib_dbg(dev, "buf_size %d, page_shift %d, npages %d, calc npages %d\n", 189 buf_size, page_shift, srq->buf.npages, npages); 190 *inlen = sizeof(**in) + sizeof(*(*in)->pas) * npages; 191 *in = mlx5_vzalloc(*inlen); 192 if (!*in) { 193 err = -ENOMEM; 194 goto err_buf; 195 } 196 mlx5_fill_page_array(&srq->buf, (*in)->pas); 197 198 srq->wrid = kmalloc(srq->msrq.max * sizeof(u64), GFP_KERNEL); 199 if (!srq->wrid) { 200 mlx5_ib_dbg(dev, "kmalloc failed %lu\n", 201 (unsigned long)(srq->msrq.max * sizeof(u64))); 202 err = -ENOMEM; 203 goto err_in; 204 } 205 srq->wq_sig = !!srq_signature; 206 207 (*in)->ctx.log_pg_sz = page_shift - MLX5_ADAPTER_PAGE_SHIFT; 208 209 return 0; 210 211 err_in: 212 kvfree(*in); 213 214 err_buf: 215 mlx5_buf_free(dev->mdev, &srq->buf); 216 217 err_db: 218 mlx5_db_free(dev->mdev, &srq->db); 219 return err; 220 } 221 222 static void destroy_srq_user(struct ib_pd *pd, struct mlx5_ib_srq *srq) 223 { 224 mlx5_ib_db_unmap_user(to_mucontext(pd->uobject->context), &srq->db); 225 ib_umem_release(srq->umem); 226 } 227 228 229 static void destroy_srq_kernel(struct mlx5_ib_dev *dev, struct mlx5_ib_srq *srq) 230 { 231 kfree(srq->wrid); 232 mlx5_buf_free(dev->mdev, &srq->buf); 233 mlx5_db_free(dev->mdev, &srq->db); 234 } 235 236 struct ib_srq *mlx5_ib_create_srq(struct ib_pd *pd, 237 struct ib_srq_init_attr *init_attr, 238 struct ib_udata *udata) 239 { 240 struct mlx5_ib_dev *dev = to_mdev(pd->device); 241 struct mlx5_general_caps *gen; 242 struct mlx5_ib_srq *srq; 243 int desc_size; 244 int buf_size; 245 int err; 246 struct mlx5_create_srq_mbox_in *uninitialized_var(in); 247 int uninitialized_var(inlen); 248 int is_xrc; 249 u32 flgs, xrcdn; 250 251 gen = &dev->mdev->caps.gen; 252 /* Sanity check SRQ size before proceeding */ 253 if (init_attr->attr.max_wr >= gen->max_srq_wqes) { 254 mlx5_ib_dbg(dev, "max_wr %d, cap %d\n", 255 init_attr->attr.max_wr, 256 gen->max_srq_wqes); 257 return ERR_PTR(-EINVAL); 258 } 259 260 srq = kmalloc(sizeof(*srq), GFP_KERNEL); 261 if (!srq) 262 return ERR_PTR(-ENOMEM); 263 264 mutex_init(&srq->mutex); 265 spin_lock_init(&srq->lock); 266 srq->msrq.max = roundup_pow_of_two(init_attr->attr.max_wr + 1); 267 srq->msrq.max_gs = init_attr->attr.max_sge; 268 269 desc_size = sizeof(struct mlx5_wqe_srq_next_seg) + 270 srq->msrq.max_gs * sizeof(struct mlx5_wqe_data_seg); 271 desc_size = roundup_pow_of_two(desc_size); 272 desc_size = max_t(int, 32, desc_size); 273 srq->msrq.max_avail_gather = (desc_size - sizeof(struct mlx5_wqe_srq_next_seg)) / 274 sizeof(struct mlx5_wqe_data_seg); 275 srq->msrq.wqe_shift = ilog2(desc_size); 276 buf_size = srq->msrq.max * desc_size; 277 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", 278 desc_size, init_attr->attr.max_wr, srq->msrq.max, srq->msrq.max_gs, 279 srq->msrq.max_avail_gather); 280 281 if (pd->uobject) 282 err = create_srq_user(pd, srq, &in, udata, buf_size, &inlen); 283 else 284 err = create_srq_kernel(dev, srq, &in, buf_size, &inlen); 285 286 if (err) { 287 mlx5_ib_warn(dev, "create srq %s failed, err %d\n", 288 pd->uobject ? "user" : "kernel", err); 289 goto err_srq; 290 } 291 292 is_xrc = (init_attr->srq_type == IB_SRQT_XRC); 293 in->ctx.state_log_sz = ilog2(srq->msrq.max); 294 flgs = ((srq->msrq.wqe_shift - 4) | (is_xrc << 5) | (srq->wq_sig << 7)) << 24; 295 xrcdn = 0; 296 if (is_xrc) { 297 xrcdn = to_mxrcd(init_attr->ext.xrc.xrcd)->xrcdn; 298 in->ctx.pgoff_cqn |= cpu_to_be32(to_mcq(init_attr->ext.xrc.cq)->mcq.cqn); 299 } else if (init_attr->srq_type == IB_SRQT_BASIC) { 300 xrcdn = to_mxrcd(dev->devr.x0)->xrcdn; 301 in->ctx.pgoff_cqn |= cpu_to_be32(to_mcq(dev->devr.c0)->mcq.cqn); 302 } 303 304 in->ctx.flags_xrcd = cpu_to_be32((flgs & 0xFF000000) | (xrcdn & 0xFFFFFF)); 305 306 in->ctx.pd = cpu_to_be32(to_mpd(pd)->pdn); 307 in->ctx.db_record = cpu_to_be64(srq->db.dma); 308 err = mlx5_core_create_srq(dev->mdev, &srq->msrq, in, inlen); 309 kvfree(in); 310 if (err) { 311 mlx5_ib_dbg(dev, "create SRQ failed, err %d\n", err); 312 goto err_usr_kern_srq; 313 } 314 315 mlx5_ib_dbg(dev, "create SRQ with srqn 0x%x\n", srq->msrq.srqn); 316 317 srq->msrq.event = mlx5_ib_srq_event; 318 srq->ibsrq.ext.xrc.srq_num = srq->msrq.srqn; 319 320 if (pd->uobject) 321 if (ib_copy_to_udata(udata, &srq->msrq.srqn, sizeof(__u32))) { 322 mlx5_ib_dbg(dev, "copy to user failed\n"); 323 err = -EFAULT; 324 goto err_core; 325 } 326 327 init_attr->attr.max_wr = srq->msrq.max - 1; 328 329 return &srq->ibsrq; 330 331 err_core: 332 mlx5_core_destroy_srq(dev->mdev, &srq->msrq); 333 334 err_usr_kern_srq: 335 if (pd->uobject) 336 destroy_srq_user(pd, srq); 337 else 338 destroy_srq_kernel(dev, srq); 339 340 err_srq: 341 kfree(srq); 342 343 return ERR_PTR(err); 344 } 345 346 int mlx5_ib_modify_srq(struct ib_srq *ibsrq, struct ib_srq_attr *attr, 347 enum ib_srq_attr_mask attr_mask, struct ib_udata *udata) 348 { 349 struct mlx5_ib_dev *dev = to_mdev(ibsrq->device); 350 struct mlx5_ib_srq *srq = to_msrq(ibsrq); 351 int ret; 352 353 /* We don't support resizing SRQs yet */ 354 if (attr_mask & IB_SRQ_MAX_WR) 355 return -EINVAL; 356 357 if (attr_mask & IB_SRQ_LIMIT) { 358 if (attr->srq_limit >= srq->msrq.max) 359 return -EINVAL; 360 361 mutex_lock(&srq->mutex); 362 ret = mlx5_core_arm_srq(dev->mdev, &srq->msrq, attr->srq_limit, 1); 363 mutex_unlock(&srq->mutex); 364 365 if (ret) 366 return ret; 367 } 368 369 return 0; 370 } 371 372 int mlx5_ib_query_srq(struct ib_srq *ibsrq, struct ib_srq_attr *srq_attr) 373 { 374 struct mlx5_ib_dev *dev = to_mdev(ibsrq->device); 375 struct mlx5_ib_srq *srq = to_msrq(ibsrq); 376 int ret; 377 struct mlx5_query_srq_mbox_out *out; 378 379 out = kzalloc(sizeof(*out), GFP_KERNEL); 380 if (!out) 381 return -ENOMEM; 382 383 ret = mlx5_core_query_srq(dev->mdev, &srq->msrq, out); 384 if (ret) 385 goto out_box; 386 387 srq_attr->srq_limit = be16_to_cpu(out->ctx.lwm); 388 srq_attr->max_wr = srq->msrq.max - 1; 389 srq_attr->max_sge = srq->msrq.max_gs; 390 391 out_box: 392 kfree(out); 393 return ret; 394 } 395 396 int mlx5_ib_destroy_srq(struct ib_srq *srq) 397 { 398 struct mlx5_ib_dev *dev = to_mdev(srq->device); 399 struct mlx5_ib_srq *msrq = to_msrq(srq); 400 401 mlx5_core_destroy_srq(dev->mdev, &msrq->msrq); 402 403 if (srq->uobject) { 404 mlx5_ib_db_unmap_user(to_mucontext(srq->uobject->context), &msrq->db); 405 ib_umem_release(msrq->umem); 406 } else { 407 destroy_srq_kernel(dev, msrq); 408 } 409 410 kfree(srq); 411 return 0; 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, struct ib_recv_wr *wr, 429 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 unsigned long flags; 435 int err = 0; 436 int nreq; 437 int i; 438 439 spin_lock_irqsave(&srq->lock, flags); 440 441 for (nreq = 0; wr; nreq++, wr = wr->next) { 442 if (unlikely(wr->num_sge > srq->msrq.max_gs)) { 443 err = -EINVAL; 444 *bad_wr = wr; 445 break; 446 } 447 448 if (unlikely(srq->head == srq->tail)) { 449 err = -ENOMEM; 450 *bad_wr = wr; 451 break; 452 } 453 454 srq->wrid[srq->head] = wr->wr_id; 455 456 next = get_wqe(srq, srq->head); 457 srq->head = be16_to_cpu(next->next_wqe_index); 458 scat = (struct mlx5_wqe_data_seg *)(next + 1); 459 460 for (i = 0; i < wr->num_sge; i++) { 461 scat[i].byte_count = cpu_to_be32(wr->sg_list[i].length); 462 scat[i].lkey = cpu_to_be32(wr->sg_list[i].lkey); 463 scat[i].addr = cpu_to_be64(wr->sg_list[i].addr); 464 } 465 466 if (i < srq->msrq.max_avail_gather) { 467 scat[i].byte_count = 0; 468 scat[i].lkey = cpu_to_be32(MLX5_INVALID_LKEY); 469 scat[i].addr = 0; 470 } 471 } 472 473 if (likely(nreq)) { 474 srq->wqe_ctr += nreq; 475 476 /* Make sure that descriptors are written before 477 * doorbell record. 478 */ 479 wmb(); 480 481 *srq->db.db = cpu_to_be32(srq->wqe_ctr); 482 } 483 484 spin_unlock_irqrestore(&srq->lock, flags); 485 486 return err; 487 } 488