1 /* 2 * Copyright (c) 2007 Cisco Systems, Inc. All rights reserved. 3 * Copyright (c) 2007, 2008 Mellanox Technologies. All rights reserved. 4 * 5 * This software is available to you under a choice of one of two 6 * licenses. You may choose to be licensed under the terms of the GNU 7 * General Public License (GPL) Version 2, available from the file 8 * COPYING in the main directory of this source tree, or the 9 * OpenIB.org BSD license below: 10 * 11 * Redistribution and use in source and binary forms, with or 12 * without modification, are permitted provided that the following 13 * conditions are met: 14 * 15 * - Redistributions of source code must retain the above 16 * copyright notice, this list of conditions and the following 17 * disclaimer. 18 * 19 * - Redistributions in binary form must reproduce the above 20 * copyright notice, this list of conditions and the following 21 * disclaimer in the documentation and/or other materials 22 * provided with the distribution. 23 * 24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 25 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 26 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 27 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 28 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 29 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 30 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 31 * SOFTWARE. 32 */ 33 34 #include <linux/mlx4/qp.h> 35 #include <linux/mlx4/srq.h> 36 #include <linux/slab.h> 37 #include <linux/vmalloc.h> 38 39 #include "mlx4_ib.h" 40 #include "user.h" 41 42 static void *get_wqe(struct mlx4_ib_srq *srq, int n) 43 { 44 return mlx4_buf_offset(&srq->buf, n << srq->msrq.wqe_shift); 45 } 46 47 static void mlx4_ib_srq_event(struct mlx4_srq *srq, enum mlx4_event type) 48 { 49 struct ib_event event; 50 struct ib_srq *ibsrq = &to_mibsrq(srq)->ibsrq; 51 52 if (ibsrq->event_handler) { 53 event.device = ibsrq->device; 54 event.element.srq = ibsrq; 55 switch (type) { 56 case MLX4_EVENT_TYPE_SRQ_LIMIT: 57 event.event = IB_EVENT_SRQ_LIMIT_REACHED; 58 break; 59 case MLX4_EVENT_TYPE_SRQ_CATAS_ERROR: 60 event.event = IB_EVENT_SRQ_ERR; 61 break; 62 default: 63 pr_warn("Unexpected event type %d " 64 "on SRQ %06x\n", type, srq->srqn); 65 return; 66 } 67 68 ibsrq->event_handler(&event, ibsrq->srq_context); 69 } 70 } 71 72 struct ib_srq *mlx4_ib_create_srq(struct ib_pd *pd, 73 struct ib_srq_init_attr *init_attr, 74 struct ib_udata *udata) 75 { 76 struct mlx4_ib_dev *dev = to_mdev(pd->device); 77 struct mlx4_ib_srq *srq; 78 struct mlx4_wqe_srq_next_seg *next; 79 struct mlx4_wqe_data_seg *scatter; 80 u32 cqn; 81 u16 xrcdn; 82 int desc_size; 83 int buf_size; 84 int err; 85 int i; 86 87 /* Sanity check SRQ size before proceeding */ 88 if (init_attr->attr.max_wr >= dev->dev->caps.max_srq_wqes || 89 init_attr->attr.max_sge > dev->dev->caps.max_srq_sge) 90 return ERR_PTR(-EINVAL); 91 92 srq = kmalloc(sizeof *srq, GFP_KERNEL); 93 if (!srq) 94 return ERR_PTR(-ENOMEM); 95 96 mutex_init(&srq->mutex); 97 spin_lock_init(&srq->lock); 98 srq->msrq.max = roundup_pow_of_two(init_attr->attr.max_wr + 1); 99 srq->msrq.max_gs = init_attr->attr.max_sge; 100 101 desc_size = max(32UL, 102 roundup_pow_of_two(sizeof (struct mlx4_wqe_srq_next_seg) + 103 srq->msrq.max_gs * 104 sizeof (struct mlx4_wqe_data_seg))); 105 srq->msrq.wqe_shift = ilog2(desc_size); 106 107 buf_size = srq->msrq.max * desc_size; 108 109 if (pd->uobject) { 110 struct mlx4_ib_create_srq ucmd; 111 112 if (ib_copy_from_udata(&ucmd, udata, sizeof ucmd)) { 113 err = -EFAULT; 114 goto err_srq; 115 } 116 117 srq->umem = ib_umem_get(pd->uobject->context, ucmd.buf_addr, 118 buf_size, 0, 0); 119 if (IS_ERR(srq->umem)) { 120 err = PTR_ERR(srq->umem); 121 goto err_srq; 122 } 123 124 err = mlx4_mtt_init(dev->dev, ib_umem_page_count(srq->umem), 125 ilog2(srq->umem->page_size), &srq->mtt); 126 if (err) 127 goto err_buf; 128 129 err = mlx4_ib_umem_write_mtt(dev, &srq->mtt, srq->umem); 130 if (err) 131 goto err_mtt; 132 133 err = mlx4_ib_db_map_user(to_mucontext(pd->uobject->context), 134 ucmd.db_addr, &srq->db); 135 if (err) 136 goto err_mtt; 137 } else { 138 err = mlx4_db_alloc(dev->dev, &srq->db, 0, GFP_KERNEL); 139 if (err) 140 goto err_srq; 141 142 *srq->db.db = 0; 143 144 if (mlx4_buf_alloc(dev->dev, buf_size, PAGE_SIZE * 2, &srq->buf, 145 GFP_KERNEL)) { 146 err = -ENOMEM; 147 goto err_db; 148 } 149 150 srq->head = 0; 151 srq->tail = srq->msrq.max - 1; 152 srq->wqe_ctr = 0; 153 154 for (i = 0; i < srq->msrq.max; ++i) { 155 next = get_wqe(srq, i); 156 next->next_wqe_index = 157 cpu_to_be16((i + 1) & (srq->msrq.max - 1)); 158 159 for (scatter = (void *) (next + 1); 160 (void *) scatter < (void *) next + desc_size; 161 ++scatter) 162 scatter->lkey = cpu_to_be32(MLX4_INVALID_LKEY); 163 } 164 165 err = mlx4_mtt_init(dev->dev, srq->buf.npages, srq->buf.page_shift, 166 &srq->mtt); 167 if (err) 168 goto err_buf; 169 170 err = mlx4_buf_write_mtt(dev->dev, &srq->mtt, &srq->buf, GFP_KERNEL); 171 if (err) 172 goto err_mtt; 173 174 srq->wrid = kmalloc_array(srq->msrq.max, sizeof(u64), 175 GFP_KERNEL | __GFP_NOWARN); 176 if (!srq->wrid) { 177 srq->wrid = __vmalloc(srq->msrq.max * sizeof(u64), 178 GFP_KERNEL, PAGE_KERNEL); 179 if (!srq->wrid) { 180 err = -ENOMEM; 181 goto err_mtt; 182 } 183 } 184 } 185 186 cqn = (init_attr->srq_type == IB_SRQT_XRC) ? 187 to_mcq(init_attr->ext.xrc.cq)->mcq.cqn : 0; 188 xrcdn = (init_attr->srq_type == IB_SRQT_XRC) ? 189 to_mxrcd(init_attr->ext.xrc.xrcd)->xrcdn : 190 (u16) dev->dev->caps.reserved_xrcds; 191 err = mlx4_srq_alloc(dev->dev, to_mpd(pd)->pdn, cqn, xrcdn, &srq->mtt, 192 srq->db.dma, &srq->msrq); 193 if (err) 194 goto err_wrid; 195 196 srq->msrq.event = mlx4_ib_srq_event; 197 srq->ibsrq.ext.xrc.srq_num = srq->msrq.srqn; 198 199 if (pd->uobject) 200 if (ib_copy_to_udata(udata, &srq->msrq.srqn, sizeof (__u32))) { 201 err = -EFAULT; 202 goto err_wrid; 203 } 204 205 init_attr->attr.max_wr = srq->msrq.max - 1; 206 207 return &srq->ibsrq; 208 209 err_wrid: 210 if (pd->uobject) 211 mlx4_ib_db_unmap_user(to_mucontext(pd->uobject->context), &srq->db); 212 else 213 kvfree(srq->wrid); 214 215 err_mtt: 216 mlx4_mtt_cleanup(dev->dev, &srq->mtt); 217 218 err_buf: 219 if (pd->uobject) 220 ib_umem_release(srq->umem); 221 else 222 mlx4_buf_free(dev->dev, buf_size, &srq->buf); 223 224 err_db: 225 if (!pd->uobject) 226 mlx4_db_free(dev->dev, &srq->db); 227 228 err_srq: 229 kfree(srq); 230 231 return ERR_PTR(err); 232 } 233 234 int mlx4_ib_modify_srq(struct ib_srq *ibsrq, struct ib_srq_attr *attr, 235 enum ib_srq_attr_mask attr_mask, struct ib_udata *udata) 236 { 237 struct mlx4_ib_dev *dev = to_mdev(ibsrq->device); 238 struct mlx4_ib_srq *srq = to_msrq(ibsrq); 239 int ret; 240 241 /* We don't support resizing SRQs (yet?) */ 242 if (attr_mask & IB_SRQ_MAX_WR) 243 return -EINVAL; 244 245 if (attr_mask & IB_SRQ_LIMIT) { 246 if (attr->srq_limit >= srq->msrq.max) 247 return -EINVAL; 248 249 mutex_lock(&srq->mutex); 250 ret = mlx4_srq_arm(dev->dev, &srq->msrq, attr->srq_limit); 251 mutex_unlock(&srq->mutex); 252 253 if (ret) 254 return ret; 255 } 256 257 return 0; 258 } 259 260 int mlx4_ib_query_srq(struct ib_srq *ibsrq, struct ib_srq_attr *srq_attr) 261 { 262 struct mlx4_ib_dev *dev = to_mdev(ibsrq->device); 263 struct mlx4_ib_srq *srq = to_msrq(ibsrq); 264 int ret; 265 int limit_watermark; 266 267 ret = mlx4_srq_query(dev->dev, &srq->msrq, &limit_watermark); 268 if (ret) 269 return ret; 270 271 srq_attr->srq_limit = limit_watermark; 272 srq_attr->max_wr = srq->msrq.max - 1; 273 srq_attr->max_sge = srq->msrq.max_gs; 274 275 return 0; 276 } 277 278 int mlx4_ib_destroy_srq(struct ib_srq *srq) 279 { 280 struct mlx4_ib_dev *dev = to_mdev(srq->device); 281 struct mlx4_ib_srq *msrq = to_msrq(srq); 282 283 mlx4_srq_free(dev->dev, &msrq->msrq); 284 mlx4_mtt_cleanup(dev->dev, &msrq->mtt); 285 286 if (srq->uobject) { 287 mlx4_ib_db_unmap_user(to_mucontext(srq->uobject->context), &msrq->db); 288 ib_umem_release(msrq->umem); 289 } else { 290 kvfree(msrq->wrid); 291 mlx4_buf_free(dev->dev, msrq->msrq.max << msrq->msrq.wqe_shift, 292 &msrq->buf); 293 mlx4_db_free(dev->dev, &msrq->db); 294 } 295 296 kfree(msrq); 297 298 return 0; 299 } 300 301 void mlx4_ib_free_srq_wqe(struct mlx4_ib_srq *srq, int wqe_index) 302 { 303 struct mlx4_wqe_srq_next_seg *next; 304 305 /* always called with interrupts disabled. */ 306 spin_lock(&srq->lock); 307 308 next = get_wqe(srq, srq->tail); 309 next->next_wqe_index = cpu_to_be16(wqe_index); 310 srq->tail = wqe_index; 311 312 spin_unlock(&srq->lock); 313 } 314 315 int mlx4_ib_post_srq_recv(struct ib_srq *ibsrq, struct ib_recv_wr *wr, 316 struct ib_recv_wr **bad_wr) 317 { 318 struct mlx4_ib_srq *srq = to_msrq(ibsrq); 319 struct mlx4_wqe_srq_next_seg *next; 320 struct mlx4_wqe_data_seg *scat; 321 unsigned long flags; 322 int err = 0; 323 int nreq; 324 int i; 325 struct mlx4_ib_dev *mdev = to_mdev(ibsrq->device); 326 327 spin_lock_irqsave(&srq->lock, flags); 328 if (mdev->dev->persist->state & MLX4_DEVICE_STATE_INTERNAL_ERROR) { 329 err = -EIO; 330 *bad_wr = wr; 331 nreq = 0; 332 goto out; 333 } 334 335 for (nreq = 0; wr; ++nreq, wr = wr->next) { 336 if (unlikely(wr->num_sge > srq->msrq.max_gs)) { 337 err = -EINVAL; 338 *bad_wr = wr; 339 break; 340 } 341 342 if (unlikely(srq->head == srq->tail)) { 343 err = -ENOMEM; 344 *bad_wr = wr; 345 break; 346 } 347 348 srq->wrid[srq->head] = wr->wr_id; 349 350 next = get_wqe(srq, srq->head); 351 srq->head = be16_to_cpu(next->next_wqe_index); 352 scat = (struct mlx4_wqe_data_seg *) (next + 1); 353 354 for (i = 0; i < wr->num_sge; ++i) { 355 scat[i].byte_count = cpu_to_be32(wr->sg_list[i].length); 356 scat[i].lkey = cpu_to_be32(wr->sg_list[i].lkey); 357 scat[i].addr = cpu_to_be64(wr->sg_list[i].addr); 358 } 359 360 if (i < srq->msrq.max_gs) { 361 scat[i].byte_count = 0; 362 scat[i].lkey = cpu_to_be32(MLX4_INVALID_LKEY); 363 scat[i].addr = 0; 364 } 365 } 366 367 if (likely(nreq)) { 368 srq->wqe_ctr += nreq; 369 370 /* 371 * Make sure that descriptors are written before 372 * doorbell record. 373 */ 374 wmb(); 375 376 *srq->db.db = cpu_to_be32(srq->wqe_ctr); 377 } 378 out: 379 380 spin_unlock_irqrestore(&srq->lock, flags); 381 382 return err; 383 } 384