RDMA/core: Add SRQ type field
[linux-3.10.git] / drivers / infiniband / hw / mlx4 / srq.c
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
38 #include "mlx4_ib.h"
39 #include "user.h"
40
41 static void *get_wqe(struct mlx4_ib_srq *srq, int n)
42 {
43         return mlx4_buf_offset(&srq->buf, n << srq->msrq.wqe_shift);
44 }
45
46 static void mlx4_ib_srq_event(struct mlx4_srq *srq, enum mlx4_event type)
47 {
48         struct ib_event event;
49         struct ib_srq *ibsrq = &to_mibsrq(srq)->ibsrq;
50
51         if (ibsrq->event_handler) {
52                 event.device      = ibsrq->device;
53                 event.element.srq = ibsrq;
54                 switch (type) {
55                 case MLX4_EVENT_TYPE_SRQ_LIMIT:
56                         event.event = IB_EVENT_SRQ_LIMIT_REACHED;
57                         break;
58                 case MLX4_EVENT_TYPE_SRQ_CATAS_ERROR:
59                         event.event = IB_EVENT_SRQ_ERR;
60                         break;
61                 default:
62                         printk(KERN_WARNING "mlx4_ib: Unexpected event type %d "
63                                "on SRQ %06x\n", type, srq->srqn);
64                         return;
65                 }
66
67                 ibsrq->event_handler(&event, ibsrq->srq_context);
68         }
69 }
70
71 struct ib_srq *mlx4_ib_create_srq(struct ib_pd *pd,
72                                   struct ib_srq_init_attr *init_attr,
73                                   struct ib_udata *udata)
74 {
75         struct mlx4_ib_dev *dev = to_mdev(pd->device);
76         struct mlx4_ib_srq *srq;
77         struct mlx4_wqe_srq_next_seg *next;
78         struct mlx4_wqe_data_seg *scatter;
79         int desc_size;
80         int buf_size;
81         int err;
82         int i;
83
84         if (init_attr->srq_type != IB_SRQT_BASIC)
85                 return ERR_PTR(-ENOSYS);
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);
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                         err = -ENOMEM;
146                         goto err_db;
147                 }
148
149                 srq->head    = 0;
150                 srq->tail    = srq->msrq.max - 1;
151                 srq->wqe_ctr = 0;
152
153                 for (i = 0; i < srq->msrq.max; ++i) {
154                         next = get_wqe(srq, i);
155                         next->next_wqe_index =
156                                 cpu_to_be16((i + 1) & (srq->msrq.max - 1));
157
158                         for (scatter = (void *) (next + 1);
159                              (void *) scatter < (void *) next + desc_size;
160                              ++scatter)
161                                 scatter->lkey = cpu_to_be32(MLX4_INVALID_LKEY);
162                 }
163
164                 err = mlx4_mtt_init(dev->dev, srq->buf.npages, srq->buf.page_shift,
165                                     &srq->mtt);
166                 if (err)
167                         goto err_buf;
168
169                 err = mlx4_buf_write_mtt(dev->dev, &srq->mtt, &srq->buf);
170                 if (err)
171                         goto err_mtt;
172
173                 srq->wrid = kmalloc(srq->msrq.max * sizeof (u64), GFP_KERNEL);
174                 if (!srq->wrid) {
175                         err = -ENOMEM;
176                         goto err_mtt;
177                 }
178         }
179
180         err = mlx4_srq_alloc(dev->dev, to_mpd(pd)->pdn, &srq->mtt,
181                              srq->db.dma, &srq->msrq);
182         if (err)
183                 goto err_wrid;
184
185         srq->msrq.event = mlx4_ib_srq_event;
186
187         if (pd->uobject)
188                 if (ib_copy_to_udata(udata, &srq->msrq.srqn, sizeof (__u32))) {
189                         err = -EFAULT;
190                         goto err_wrid;
191                 }
192
193         init_attr->attr.max_wr = srq->msrq.max - 1;
194
195         return &srq->ibsrq;
196
197 err_wrid:
198         if (pd->uobject)
199                 mlx4_ib_db_unmap_user(to_mucontext(pd->uobject->context), &srq->db);
200         else
201                 kfree(srq->wrid);
202
203 err_mtt:
204         mlx4_mtt_cleanup(dev->dev, &srq->mtt);
205
206 err_buf:
207         if (pd->uobject)
208                 ib_umem_release(srq->umem);
209         else
210                 mlx4_buf_free(dev->dev, buf_size, &srq->buf);
211
212 err_db:
213         if (!pd->uobject)
214                 mlx4_db_free(dev->dev, &srq->db);
215
216 err_srq:
217         kfree(srq);
218
219         return ERR_PTR(err);
220 }
221
222 int mlx4_ib_modify_srq(struct ib_srq *ibsrq, struct ib_srq_attr *attr,
223                        enum ib_srq_attr_mask attr_mask, struct ib_udata *udata)
224 {
225         struct mlx4_ib_dev *dev = to_mdev(ibsrq->device);
226         struct mlx4_ib_srq *srq = to_msrq(ibsrq);
227         int ret;
228
229         /* We don't support resizing SRQs (yet?) */
230         if (attr_mask & IB_SRQ_MAX_WR)
231                 return -EINVAL;
232
233         if (attr_mask & IB_SRQ_LIMIT) {
234                 if (attr->srq_limit >= srq->msrq.max)
235                         return -EINVAL;
236
237                 mutex_lock(&srq->mutex);
238                 ret = mlx4_srq_arm(dev->dev, &srq->msrq, attr->srq_limit);
239                 mutex_unlock(&srq->mutex);
240
241                 if (ret)
242                         return ret;
243         }
244
245         return 0;
246 }
247
248 int mlx4_ib_query_srq(struct ib_srq *ibsrq, struct ib_srq_attr *srq_attr)
249 {
250         struct mlx4_ib_dev *dev = to_mdev(ibsrq->device);
251         struct mlx4_ib_srq *srq = to_msrq(ibsrq);
252         int ret;
253         int limit_watermark;
254
255         ret = mlx4_srq_query(dev->dev, &srq->msrq, &limit_watermark);
256         if (ret)
257                 return ret;
258
259         srq_attr->srq_limit = limit_watermark;
260         srq_attr->max_wr    = srq->msrq.max - 1;
261         srq_attr->max_sge   = srq->msrq.max_gs;
262
263         return 0;
264 }
265
266 int mlx4_ib_destroy_srq(struct ib_srq *srq)
267 {
268         struct mlx4_ib_dev *dev = to_mdev(srq->device);
269         struct mlx4_ib_srq *msrq = to_msrq(srq);
270
271         mlx4_srq_free(dev->dev, &msrq->msrq);
272         mlx4_mtt_cleanup(dev->dev, &msrq->mtt);
273
274         if (srq->uobject) {
275                 mlx4_ib_db_unmap_user(to_mucontext(srq->uobject->context), &msrq->db);
276                 ib_umem_release(msrq->umem);
277         } else {
278                 kfree(msrq->wrid);
279                 mlx4_buf_free(dev->dev, msrq->msrq.max << msrq->msrq.wqe_shift,
280                               &msrq->buf);
281                 mlx4_db_free(dev->dev, &msrq->db);
282         }
283
284         kfree(msrq);
285
286         return 0;
287 }
288
289 void mlx4_ib_free_srq_wqe(struct mlx4_ib_srq *srq, int wqe_index)
290 {
291         struct mlx4_wqe_srq_next_seg *next;
292
293         /* always called with interrupts disabled. */
294         spin_lock(&srq->lock);
295
296         next = get_wqe(srq, srq->tail);
297         next->next_wqe_index = cpu_to_be16(wqe_index);
298         srq->tail = wqe_index;
299
300         spin_unlock(&srq->lock);
301 }
302
303 int mlx4_ib_post_srq_recv(struct ib_srq *ibsrq, struct ib_recv_wr *wr,
304                           struct ib_recv_wr **bad_wr)
305 {
306         struct mlx4_ib_srq *srq = to_msrq(ibsrq);
307         struct mlx4_wqe_srq_next_seg *next;
308         struct mlx4_wqe_data_seg *scat;
309         unsigned long flags;
310         int err = 0;
311         int nreq;
312         int i;
313
314         spin_lock_irqsave(&srq->lock, flags);
315
316         for (nreq = 0; wr; ++nreq, wr = wr->next) {
317                 if (unlikely(wr->num_sge > srq->msrq.max_gs)) {
318                         err = -EINVAL;
319                         *bad_wr = wr;
320                         break;
321                 }
322
323                 if (unlikely(srq->head == srq->tail)) {
324                         err = -ENOMEM;
325                         *bad_wr = wr;
326                         break;
327                 }
328
329                 srq->wrid[srq->head] = wr->wr_id;
330
331                 next      = get_wqe(srq, srq->head);
332                 srq->head = be16_to_cpu(next->next_wqe_index);
333                 scat      = (struct mlx4_wqe_data_seg *) (next + 1);
334
335                 for (i = 0; i < wr->num_sge; ++i) {
336                         scat[i].byte_count = cpu_to_be32(wr->sg_list[i].length);
337                         scat[i].lkey       = cpu_to_be32(wr->sg_list[i].lkey);
338                         scat[i].addr       = cpu_to_be64(wr->sg_list[i].addr);
339                 }
340
341                 if (i < srq->msrq.max_gs) {
342                         scat[i].byte_count = 0;
343                         scat[i].lkey       = cpu_to_be32(MLX4_INVALID_LKEY);
344                         scat[i].addr       = 0;
345                 }
346         }
347
348         if (likely(nreq)) {
349                 srq->wqe_ctr += nreq;
350
351                 /*
352                  * Make sure that descriptors are written before
353                  * doorbell record.
354                  */
355                 wmb();
356
357                 *srq->db.db = cpu_to_be32(srq->wqe_ctr);
358         }
359
360         spin_unlock_irqrestore(&srq->lock, flags);
361
362         return err;
363 }