Commit | Line | Data |
---|---|---|
225c7b1f RD |
1 | /* |
2 | * Copyright (c) 2007 Cisco Systems, Inc. All rights reserved. | |
51a379d0 | 3 | * Copyright (c) 2007, 2008 Mellanox Technologies. All rights reserved. |
225c7b1f RD |
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> | |
5a0e3ad6 | 36 | #include <linux/slab.h> |
0ef2f05c | 37 | #include <linux/vmalloc.h> |
225c7b1f RD |
38 | |
39 | #include "mlx4_ib.h" | |
40 | #include "user.h" | |
41 | ||
42 | static void *get_wqe(struct mlx4_ib_srq *srq, int n) | |
43 | { | |
1c69fc2a | 44 | return mlx4_buf_offset(&srq->buf, n << srq->msrq.wqe_shift); |
225c7b1f RD |
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: | |
987c8f8f | 63 | pr_warn("Unexpected event type %d " |
225c7b1f RD |
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; | |
4c425588 | 79 | struct mlx4_wqe_data_seg *scatter; |
18abd5ea SH |
80 | u32 cqn; |
81 | u16 xrcdn; | |
225c7b1f RD |
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, | |
cb9fbc5c | 118 | buf_size, 0, 0); |
225c7b1f RD |
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 { | |
40f2287b | 138 | err = mlx4_db_alloc(dev->dev, &srq->db, 0, GFP_KERNEL); |
225c7b1f RD |
139 | if (err) |
140 | goto err_srq; | |
141 | ||
142 | *srq->db.db = 0; | |
143 | ||
40f2287b JK |
144 | if (mlx4_buf_alloc(dev->dev, buf_size, PAGE_SIZE * 2, &srq->buf, |
145 | GFP_KERNEL)) { | |
225c7b1f RD |
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)); | |
4c425588 JM |
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); | |
225c7b1f RD |
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 | ||
40f2287b | 170 | err = mlx4_buf_write_mtt(dev->dev, &srq->mtt, &srq->buf, GFP_KERNEL); |
225c7b1f RD |
171 | if (err) |
172 | goto err_mtt; | |
173 | ||
174 | srq->wrid = kmalloc(srq->msrq.max * sizeof (u64), GFP_KERNEL); | |
175 | if (!srq->wrid) { | |
0ef2f05c WW |
176 | srq->wrid = __vmalloc(srq->msrq.max * sizeof(u64), |
177 | GFP_KERNEL, PAGE_KERNEL); | |
178 | if (!srq->wrid) { | |
179 | err = -ENOMEM; | |
180 | goto err_mtt; | |
181 | } | |
225c7b1f RD |
182 | } |
183 | } | |
184 | ||
18abd5ea SH |
185 | cqn = (init_attr->srq_type == IB_SRQT_XRC) ? |
186 | to_mcq(init_attr->ext.xrc.cq)->mcq.cqn : 0; | |
187 | xrcdn = (init_attr->srq_type == IB_SRQT_XRC) ? | |
188 | to_mxrcd(init_attr->ext.xrc.xrcd)->xrcdn : | |
189 | (u16) dev->dev->caps.reserved_xrcds; | |
190 | err = mlx4_srq_alloc(dev->dev, to_mpd(pd)->pdn, cqn, xrcdn, &srq->mtt, | |
225c7b1f RD |
191 | srq->db.dma, &srq->msrq); |
192 | if (err) | |
193 | goto err_wrid; | |
194 | ||
195 | srq->msrq.event = mlx4_ib_srq_event; | |
18abd5ea | 196 | srq->ibsrq.ext.xrc.srq_num = srq->msrq.srqn; |
225c7b1f RD |
197 | |
198 | if (pd->uobject) | |
199 | if (ib_copy_to_udata(udata, &srq->msrq.srqn, sizeof (__u32))) { | |
200 | err = -EFAULT; | |
201 | goto err_wrid; | |
202 | } | |
203 | ||
204 | init_attr->attr.max_wr = srq->msrq.max - 1; | |
205 | ||
206 | return &srq->ibsrq; | |
207 | ||
208 | err_wrid: | |
209 | if (pd->uobject) | |
210 | mlx4_ib_db_unmap_user(to_mucontext(pd->uobject->context), &srq->db); | |
211 | else | |
0ef2f05c | 212 | kvfree(srq->wrid); |
225c7b1f RD |
213 | |
214 | err_mtt: | |
215 | mlx4_mtt_cleanup(dev->dev, &srq->mtt); | |
216 | ||
217 | err_buf: | |
218 | if (pd->uobject) | |
219 | ib_umem_release(srq->umem); | |
220 | else | |
221 | mlx4_buf_free(dev->dev, buf_size, &srq->buf); | |
222 | ||
223 | err_db: | |
224 | if (!pd->uobject) | |
6296883c | 225 | mlx4_db_free(dev->dev, &srq->db); |
225c7b1f RD |
226 | |
227 | err_srq: | |
228 | kfree(srq); | |
229 | ||
230 | return ERR_PTR(err); | |
231 | } | |
232 | ||
233 | int mlx4_ib_modify_srq(struct ib_srq *ibsrq, struct ib_srq_attr *attr, | |
234 | enum ib_srq_attr_mask attr_mask, struct ib_udata *udata) | |
235 | { | |
236 | struct mlx4_ib_dev *dev = to_mdev(ibsrq->device); | |
237 | struct mlx4_ib_srq *srq = to_msrq(ibsrq); | |
238 | int ret; | |
239 | ||
240 | /* We don't support resizing SRQs (yet?) */ | |
241 | if (attr_mask & IB_SRQ_MAX_WR) | |
242 | return -EINVAL; | |
243 | ||
244 | if (attr_mask & IB_SRQ_LIMIT) { | |
245 | if (attr->srq_limit >= srq->msrq.max) | |
246 | return -EINVAL; | |
247 | ||
248 | mutex_lock(&srq->mutex); | |
249 | ret = mlx4_srq_arm(dev->dev, &srq->msrq, attr->srq_limit); | |
250 | mutex_unlock(&srq->mutex); | |
251 | ||
252 | if (ret) | |
253 | return ret; | |
254 | } | |
255 | ||
256 | return 0; | |
257 | } | |
258 | ||
65541cb7 JM |
259 | int mlx4_ib_query_srq(struct ib_srq *ibsrq, struct ib_srq_attr *srq_attr) |
260 | { | |
261 | struct mlx4_ib_dev *dev = to_mdev(ibsrq->device); | |
262 | struct mlx4_ib_srq *srq = to_msrq(ibsrq); | |
263 | int ret; | |
264 | int limit_watermark; | |
265 | ||
266 | ret = mlx4_srq_query(dev->dev, &srq->msrq, &limit_watermark); | |
267 | if (ret) | |
268 | return ret; | |
269 | ||
d7dc3ccb | 270 | srq_attr->srq_limit = limit_watermark; |
65541cb7 JM |
271 | srq_attr->max_wr = srq->msrq.max - 1; |
272 | srq_attr->max_sge = srq->msrq.max_gs; | |
273 | ||
274 | return 0; | |
275 | } | |
276 | ||
225c7b1f RD |
277 | int mlx4_ib_destroy_srq(struct ib_srq *srq) |
278 | { | |
279 | struct mlx4_ib_dev *dev = to_mdev(srq->device); | |
280 | struct mlx4_ib_srq *msrq = to_msrq(srq); | |
281 | ||
282 | mlx4_srq_free(dev->dev, &msrq->msrq); | |
283 | mlx4_mtt_cleanup(dev->dev, &msrq->mtt); | |
284 | ||
285 | if (srq->uobject) { | |
286 | mlx4_ib_db_unmap_user(to_mucontext(srq->uobject->context), &msrq->db); | |
287 | ib_umem_release(msrq->umem); | |
288 | } else { | |
289 | kfree(msrq->wrid); | |
290 | mlx4_buf_free(dev->dev, msrq->msrq.max << msrq->msrq.wqe_shift, | |
291 | &msrq->buf); | |
6296883c | 292 | mlx4_db_free(dev->dev, &msrq->db); |
225c7b1f RD |
293 | } |
294 | ||
295 | kfree(msrq); | |
296 | ||
297 | return 0; | |
298 | } | |
299 | ||
300 | void mlx4_ib_free_srq_wqe(struct mlx4_ib_srq *srq, int wqe_index) | |
301 | { | |
302 | struct mlx4_wqe_srq_next_seg *next; | |
303 | ||
304 | /* always called with interrupts disabled. */ | |
305 | spin_lock(&srq->lock); | |
306 | ||
307 | next = get_wqe(srq, srq->tail); | |
308 | next->next_wqe_index = cpu_to_be16(wqe_index); | |
309 | srq->tail = wqe_index; | |
310 | ||
311 | spin_unlock(&srq->lock); | |
312 | } | |
313 | ||
314 | int mlx4_ib_post_srq_recv(struct ib_srq *ibsrq, struct ib_recv_wr *wr, | |
315 | struct ib_recv_wr **bad_wr) | |
316 | { | |
317 | struct mlx4_ib_srq *srq = to_msrq(ibsrq); | |
318 | struct mlx4_wqe_srq_next_seg *next; | |
319 | struct mlx4_wqe_data_seg *scat; | |
320 | unsigned long flags; | |
321 | int err = 0; | |
322 | int nreq; | |
323 | int i; | |
35f05dab | 324 | struct mlx4_ib_dev *mdev = to_mdev(ibsrq->device); |
225c7b1f RD |
325 | |
326 | spin_lock_irqsave(&srq->lock, flags); | |
35f05dab YH |
327 | if (mdev->dev->persist->state & MLX4_DEVICE_STATE_INTERNAL_ERROR) { |
328 | err = -EIO; | |
329 | *bad_wr = wr; | |
330 | nreq = 0; | |
331 | goto out; | |
332 | } | |
225c7b1f RD |
333 | |
334 | for (nreq = 0; wr; ++nreq, wr = wr->next) { | |
335 | if (unlikely(wr->num_sge > srq->msrq.max_gs)) { | |
336 | err = -EINVAL; | |
337 | *bad_wr = wr; | |
338 | break; | |
339 | } | |
340 | ||
56a8c8b6 RD |
341 | if (unlikely(srq->head == srq->tail)) { |
342 | err = -ENOMEM; | |
343 | *bad_wr = wr; | |
344 | break; | |
345 | } | |
346 | ||
225c7b1f RD |
347 | srq->wrid[srq->head] = wr->wr_id; |
348 | ||
349 | next = get_wqe(srq, srq->head); | |
350 | srq->head = be16_to_cpu(next->next_wqe_index); | |
351 | scat = (struct mlx4_wqe_data_seg *) (next + 1); | |
352 | ||
353 | for (i = 0; i < wr->num_sge; ++i) { | |
354 | scat[i].byte_count = cpu_to_be32(wr->sg_list[i].length); | |
355 | scat[i].lkey = cpu_to_be32(wr->sg_list[i].lkey); | |
356 | scat[i].addr = cpu_to_be64(wr->sg_list[i].addr); | |
357 | } | |
358 | ||
359 | if (i < srq->msrq.max_gs) { | |
360 | scat[i].byte_count = 0; | |
361 | scat[i].lkey = cpu_to_be32(MLX4_INVALID_LKEY); | |
362 | scat[i].addr = 0; | |
363 | } | |
364 | } | |
365 | ||
366 | if (likely(nreq)) { | |
367 | srq->wqe_ctr += nreq; | |
368 | ||
369 | /* | |
370 | * Make sure that descriptors are written before | |
371 | * doorbell record. | |
372 | */ | |
373 | wmb(); | |
374 | ||
375 | *srq->db.db = cpu_to_be32(srq->wqe_ctr); | |
376 | } | |
35f05dab | 377 | out: |
225c7b1f RD |
378 | |
379 | spin_unlock_irqrestore(&srq->lock, flags); | |
380 | ||
381 | return err; | |
382 | } |