Roland Dreier | 225c7b1 | 2007-05-08 18:00:38 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2007 Cisco Systems, 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/mlx4/qp.h> |
| 34 | #include <linux/mlx4/srq.h> |
| 35 | |
| 36 | #include "mlx4_ib.h" |
| 37 | #include "user.h" |
| 38 | |
| 39 | static void *get_wqe(struct mlx4_ib_srq *srq, int n) |
| 40 | { |
Roland Dreier | 1c69fc2 | 2008-02-06 21:07:54 -0800 | [diff] [blame^] | 41 | return mlx4_buf_offset(&srq->buf, n << srq->msrq.wqe_shift); |
Roland Dreier | 225c7b1 | 2007-05-08 18:00:38 -0700 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | static void mlx4_ib_srq_event(struct mlx4_srq *srq, enum mlx4_event type) |
| 45 | { |
| 46 | struct ib_event event; |
| 47 | struct ib_srq *ibsrq = &to_mibsrq(srq)->ibsrq; |
| 48 | |
| 49 | if (ibsrq->event_handler) { |
| 50 | event.device = ibsrq->device; |
| 51 | event.element.srq = ibsrq; |
| 52 | switch (type) { |
| 53 | case MLX4_EVENT_TYPE_SRQ_LIMIT: |
| 54 | event.event = IB_EVENT_SRQ_LIMIT_REACHED; |
| 55 | break; |
| 56 | case MLX4_EVENT_TYPE_SRQ_CATAS_ERROR: |
| 57 | event.event = IB_EVENT_SRQ_ERR; |
| 58 | break; |
| 59 | default: |
| 60 | printk(KERN_WARNING "mlx4_ib: Unexpected event type %d " |
| 61 | "on SRQ %06x\n", type, srq->srqn); |
| 62 | return; |
| 63 | } |
| 64 | |
| 65 | ibsrq->event_handler(&event, ibsrq->srq_context); |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | struct ib_srq *mlx4_ib_create_srq(struct ib_pd *pd, |
| 70 | struct ib_srq_init_attr *init_attr, |
| 71 | struct ib_udata *udata) |
| 72 | { |
| 73 | struct mlx4_ib_dev *dev = to_mdev(pd->device); |
| 74 | struct mlx4_ib_srq *srq; |
| 75 | struct mlx4_wqe_srq_next_seg *next; |
| 76 | int desc_size; |
| 77 | int buf_size; |
| 78 | int err; |
| 79 | int i; |
| 80 | |
| 81 | /* Sanity check SRQ size before proceeding */ |
| 82 | if (init_attr->attr.max_wr >= dev->dev->caps.max_srq_wqes || |
| 83 | init_attr->attr.max_sge > dev->dev->caps.max_srq_sge) |
| 84 | return ERR_PTR(-EINVAL); |
| 85 | |
| 86 | srq = kmalloc(sizeof *srq, GFP_KERNEL); |
| 87 | if (!srq) |
| 88 | return ERR_PTR(-ENOMEM); |
| 89 | |
| 90 | mutex_init(&srq->mutex); |
| 91 | spin_lock_init(&srq->lock); |
| 92 | srq->msrq.max = roundup_pow_of_two(init_attr->attr.max_wr + 1); |
| 93 | srq->msrq.max_gs = init_attr->attr.max_sge; |
| 94 | |
| 95 | desc_size = max(32UL, |
| 96 | roundup_pow_of_two(sizeof (struct mlx4_wqe_srq_next_seg) + |
| 97 | srq->msrq.max_gs * |
| 98 | sizeof (struct mlx4_wqe_data_seg))); |
| 99 | srq->msrq.wqe_shift = ilog2(desc_size); |
| 100 | |
| 101 | buf_size = srq->msrq.max * desc_size; |
| 102 | |
| 103 | if (pd->uobject) { |
| 104 | struct mlx4_ib_create_srq ucmd; |
| 105 | |
| 106 | if (ib_copy_from_udata(&ucmd, udata, sizeof ucmd)) { |
| 107 | err = -EFAULT; |
| 108 | goto err_srq; |
| 109 | } |
| 110 | |
| 111 | srq->umem = ib_umem_get(pd->uobject->context, ucmd.buf_addr, |
| 112 | buf_size, 0); |
| 113 | if (IS_ERR(srq->umem)) { |
| 114 | err = PTR_ERR(srq->umem); |
| 115 | goto err_srq; |
| 116 | } |
| 117 | |
| 118 | err = mlx4_mtt_init(dev->dev, ib_umem_page_count(srq->umem), |
| 119 | ilog2(srq->umem->page_size), &srq->mtt); |
| 120 | if (err) |
| 121 | goto err_buf; |
| 122 | |
| 123 | err = mlx4_ib_umem_write_mtt(dev, &srq->mtt, srq->umem); |
| 124 | if (err) |
| 125 | goto err_mtt; |
| 126 | |
| 127 | err = mlx4_ib_db_map_user(to_mucontext(pd->uobject->context), |
| 128 | ucmd.db_addr, &srq->db); |
| 129 | if (err) |
| 130 | goto err_mtt; |
| 131 | } else { |
| 132 | err = mlx4_ib_db_alloc(dev, &srq->db, 0); |
| 133 | if (err) |
| 134 | goto err_srq; |
| 135 | |
| 136 | *srq->db.db = 0; |
| 137 | |
| 138 | if (mlx4_buf_alloc(dev->dev, buf_size, PAGE_SIZE * 2, &srq->buf)) { |
| 139 | err = -ENOMEM; |
| 140 | goto err_db; |
| 141 | } |
| 142 | |
| 143 | srq->head = 0; |
| 144 | srq->tail = srq->msrq.max - 1; |
| 145 | srq->wqe_ctr = 0; |
| 146 | |
| 147 | for (i = 0; i < srq->msrq.max; ++i) { |
| 148 | next = get_wqe(srq, i); |
| 149 | next->next_wqe_index = |
| 150 | cpu_to_be16((i + 1) & (srq->msrq.max - 1)); |
| 151 | } |
| 152 | |
| 153 | err = mlx4_mtt_init(dev->dev, srq->buf.npages, srq->buf.page_shift, |
| 154 | &srq->mtt); |
| 155 | if (err) |
| 156 | goto err_buf; |
| 157 | |
| 158 | err = mlx4_buf_write_mtt(dev->dev, &srq->mtt, &srq->buf); |
| 159 | if (err) |
| 160 | goto err_mtt; |
| 161 | |
| 162 | srq->wrid = kmalloc(srq->msrq.max * sizeof (u64), GFP_KERNEL); |
| 163 | if (!srq->wrid) { |
| 164 | err = -ENOMEM; |
| 165 | goto err_mtt; |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | err = mlx4_srq_alloc(dev->dev, to_mpd(pd)->pdn, &srq->mtt, |
| 170 | srq->db.dma, &srq->msrq); |
| 171 | if (err) |
| 172 | goto err_wrid; |
| 173 | |
| 174 | srq->msrq.event = mlx4_ib_srq_event; |
| 175 | |
| 176 | if (pd->uobject) |
| 177 | if (ib_copy_to_udata(udata, &srq->msrq.srqn, sizeof (__u32))) { |
| 178 | err = -EFAULT; |
| 179 | goto err_wrid; |
| 180 | } |
| 181 | |
| 182 | init_attr->attr.max_wr = srq->msrq.max - 1; |
| 183 | |
| 184 | return &srq->ibsrq; |
| 185 | |
| 186 | err_wrid: |
| 187 | if (pd->uobject) |
| 188 | mlx4_ib_db_unmap_user(to_mucontext(pd->uobject->context), &srq->db); |
| 189 | else |
| 190 | kfree(srq->wrid); |
| 191 | |
| 192 | err_mtt: |
| 193 | mlx4_mtt_cleanup(dev->dev, &srq->mtt); |
| 194 | |
| 195 | err_buf: |
| 196 | if (pd->uobject) |
| 197 | ib_umem_release(srq->umem); |
| 198 | else |
| 199 | mlx4_buf_free(dev->dev, buf_size, &srq->buf); |
| 200 | |
| 201 | err_db: |
| 202 | if (!pd->uobject) |
| 203 | mlx4_ib_db_free(dev, &srq->db); |
| 204 | |
| 205 | err_srq: |
| 206 | kfree(srq); |
| 207 | |
| 208 | return ERR_PTR(err); |
| 209 | } |
| 210 | |
| 211 | int mlx4_ib_modify_srq(struct ib_srq *ibsrq, struct ib_srq_attr *attr, |
| 212 | enum ib_srq_attr_mask attr_mask, struct ib_udata *udata) |
| 213 | { |
| 214 | struct mlx4_ib_dev *dev = to_mdev(ibsrq->device); |
| 215 | struct mlx4_ib_srq *srq = to_msrq(ibsrq); |
| 216 | int ret; |
| 217 | |
| 218 | /* We don't support resizing SRQs (yet?) */ |
| 219 | if (attr_mask & IB_SRQ_MAX_WR) |
| 220 | return -EINVAL; |
| 221 | |
| 222 | if (attr_mask & IB_SRQ_LIMIT) { |
| 223 | if (attr->srq_limit >= srq->msrq.max) |
| 224 | return -EINVAL; |
| 225 | |
| 226 | mutex_lock(&srq->mutex); |
| 227 | ret = mlx4_srq_arm(dev->dev, &srq->msrq, attr->srq_limit); |
| 228 | mutex_unlock(&srq->mutex); |
| 229 | |
| 230 | if (ret) |
| 231 | return ret; |
| 232 | } |
| 233 | |
| 234 | return 0; |
| 235 | } |
| 236 | |
Jack Morgenstein | 65541cb | 2007-06-21 13:03:11 +0300 | [diff] [blame] | 237 | int mlx4_ib_query_srq(struct ib_srq *ibsrq, struct ib_srq_attr *srq_attr) |
| 238 | { |
| 239 | struct mlx4_ib_dev *dev = to_mdev(ibsrq->device); |
| 240 | struct mlx4_ib_srq *srq = to_msrq(ibsrq); |
| 241 | int ret; |
| 242 | int limit_watermark; |
| 243 | |
| 244 | ret = mlx4_srq_query(dev->dev, &srq->msrq, &limit_watermark); |
| 245 | if (ret) |
| 246 | return ret; |
| 247 | |
Roland Dreier | d7dc3cc | 2007-10-09 19:59:06 -0700 | [diff] [blame] | 248 | srq_attr->srq_limit = limit_watermark; |
Jack Morgenstein | 65541cb | 2007-06-21 13:03:11 +0300 | [diff] [blame] | 249 | srq_attr->max_wr = srq->msrq.max - 1; |
| 250 | srq_attr->max_sge = srq->msrq.max_gs; |
| 251 | |
| 252 | return 0; |
| 253 | } |
| 254 | |
Roland Dreier | 225c7b1 | 2007-05-08 18:00:38 -0700 | [diff] [blame] | 255 | int mlx4_ib_destroy_srq(struct ib_srq *srq) |
| 256 | { |
| 257 | struct mlx4_ib_dev *dev = to_mdev(srq->device); |
| 258 | struct mlx4_ib_srq *msrq = to_msrq(srq); |
| 259 | |
| 260 | mlx4_srq_free(dev->dev, &msrq->msrq); |
| 261 | mlx4_mtt_cleanup(dev->dev, &msrq->mtt); |
| 262 | |
| 263 | if (srq->uobject) { |
| 264 | mlx4_ib_db_unmap_user(to_mucontext(srq->uobject->context), &msrq->db); |
| 265 | ib_umem_release(msrq->umem); |
| 266 | } else { |
| 267 | kfree(msrq->wrid); |
| 268 | mlx4_buf_free(dev->dev, msrq->msrq.max << msrq->msrq.wqe_shift, |
| 269 | &msrq->buf); |
| 270 | mlx4_ib_db_free(dev, &msrq->db); |
| 271 | } |
| 272 | |
| 273 | kfree(msrq); |
| 274 | |
| 275 | return 0; |
| 276 | } |
| 277 | |
| 278 | void mlx4_ib_free_srq_wqe(struct mlx4_ib_srq *srq, int wqe_index) |
| 279 | { |
| 280 | struct mlx4_wqe_srq_next_seg *next; |
| 281 | |
| 282 | /* always called with interrupts disabled. */ |
| 283 | spin_lock(&srq->lock); |
| 284 | |
| 285 | next = get_wqe(srq, srq->tail); |
| 286 | next->next_wqe_index = cpu_to_be16(wqe_index); |
| 287 | srq->tail = wqe_index; |
| 288 | |
| 289 | spin_unlock(&srq->lock); |
| 290 | } |
| 291 | |
| 292 | int mlx4_ib_post_srq_recv(struct ib_srq *ibsrq, struct ib_recv_wr *wr, |
| 293 | struct ib_recv_wr **bad_wr) |
| 294 | { |
| 295 | struct mlx4_ib_srq *srq = to_msrq(ibsrq); |
| 296 | struct mlx4_wqe_srq_next_seg *next; |
| 297 | struct mlx4_wqe_data_seg *scat; |
| 298 | unsigned long flags; |
| 299 | int err = 0; |
| 300 | int nreq; |
| 301 | int i; |
| 302 | |
| 303 | spin_lock_irqsave(&srq->lock, flags); |
| 304 | |
| 305 | for (nreq = 0; wr; ++nreq, wr = wr->next) { |
| 306 | if (unlikely(wr->num_sge > srq->msrq.max_gs)) { |
| 307 | err = -EINVAL; |
| 308 | *bad_wr = wr; |
| 309 | break; |
| 310 | } |
| 311 | |
Roland Dreier | 56a8c8b | 2007-05-20 20:19:24 -0700 | [diff] [blame] | 312 | if (unlikely(srq->head == srq->tail)) { |
| 313 | err = -ENOMEM; |
| 314 | *bad_wr = wr; |
| 315 | break; |
| 316 | } |
| 317 | |
Roland Dreier | 225c7b1 | 2007-05-08 18:00:38 -0700 | [diff] [blame] | 318 | srq->wrid[srq->head] = wr->wr_id; |
| 319 | |
| 320 | next = get_wqe(srq, srq->head); |
| 321 | srq->head = be16_to_cpu(next->next_wqe_index); |
| 322 | scat = (struct mlx4_wqe_data_seg *) (next + 1); |
| 323 | |
| 324 | for (i = 0; i < wr->num_sge; ++i) { |
| 325 | scat[i].byte_count = cpu_to_be32(wr->sg_list[i].length); |
| 326 | scat[i].lkey = cpu_to_be32(wr->sg_list[i].lkey); |
| 327 | scat[i].addr = cpu_to_be64(wr->sg_list[i].addr); |
| 328 | } |
| 329 | |
| 330 | if (i < srq->msrq.max_gs) { |
| 331 | scat[i].byte_count = 0; |
| 332 | scat[i].lkey = cpu_to_be32(MLX4_INVALID_LKEY); |
| 333 | scat[i].addr = 0; |
| 334 | } |
| 335 | } |
| 336 | |
| 337 | if (likely(nreq)) { |
| 338 | srq->wqe_ctr += nreq; |
| 339 | |
| 340 | /* |
| 341 | * Make sure that descriptors are written before |
| 342 | * doorbell record. |
| 343 | */ |
| 344 | wmb(); |
| 345 | |
| 346 | *srq->db.db = cpu_to_be32(srq->wqe_ctr); |
| 347 | } |
| 348 | |
| 349 | spin_unlock_irqrestore(&srq->lock, flags); |
| 350 | |
| 351 | return err; |
| 352 | } |