2 * Copyright (c) 2007 Cisco Systems, Inc. All rights reserved.
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:
10 * Redistribution and use in source and binary forms, with or
11 * without modification, are permitted provided that the following
14 * - Redistributions of source code must retain the above
15 * copyright notice, this list of conditions and the following
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.
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
33 #include <linux/mlx4/qp.h>
34 #include <linux/mlx4/srq.h>
39 static void *get_wqe(struct mlx4_ib_srq
*srq
, int n
)
41 int offset
= n
<< srq
->msrq
.wqe_shift
;
43 if (srq
->buf
.nbufs
== 1)
44 return srq
->buf
.u
.direct
.buf
+ offset
;
46 return srq
->buf
.u
.page_list
[offset
>> PAGE_SHIFT
].buf
+
47 (offset
& (PAGE_SIZE
- 1));
50 static void mlx4_ib_srq_event(struct mlx4_srq
*srq
, enum mlx4_event type
)
52 struct ib_event event
;
53 struct ib_srq
*ibsrq
= &to_mibsrq(srq
)->ibsrq
;
55 if (ibsrq
->event_handler
) {
56 event
.device
= ibsrq
->device
;
57 event
.element
.srq
= ibsrq
;
59 case MLX4_EVENT_TYPE_SRQ_LIMIT
:
60 event
.event
= IB_EVENT_SRQ_LIMIT_REACHED
;
62 case MLX4_EVENT_TYPE_SRQ_CATAS_ERROR
:
63 event
.event
= IB_EVENT_SRQ_ERR
;
66 printk(KERN_WARNING
"mlx4_ib: Unexpected event type %d "
67 "on SRQ %06x\n", type
, srq
->srqn
);
71 ibsrq
->event_handler(&event
, ibsrq
->srq_context
);
75 struct ib_srq
*mlx4_ib_create_srq(struct ib_pd
*pd
,
76 struct ib_srq_init_attr
*init_attr
,
77 struct ib_udata
*udata
)
79 struct mlx4_ib_dev
*dev
= to_mdev(pd
->device
);
80 struct mlx4_ib_srq
*srq
;
81 struct mlx4_wqe_srq_next_seg
*next
;
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
);
92 srq
= kmalloc(sizeof *srq
, GFP_KERNEL
);
94 return ERR_PTR(-ENOMEM
);
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
;
101 desc_size
= max(32UL,
102 roundup_pow_of_two(sizeof (struct mlx4_wqe_srq_next_seg
) +
104 sizeof (struct mlx4_wqe_data_seg
)));
105 srq
->msrq
.wqe_shift
= ilog2(desc_size
);
107 buf_size
= srq
->msrq
.max
* desc_size
;
110 struct mlx4_ib_create_srq ucmd
;
112 if (ib_copy_from_udata(&ucmd
, udata
, sizeof ucmd
)) {
117 srq
->umem
= ib_umem_get(pd
->uobject
->context
, ucmd
.buf_addr
,
119 if (IS_ERR(srq
->umem
)) {
120 err
= PTR_ERR(srq
->umem
);
124 err
= mlx4_mtt_init(dev
->dev
, ib_umem_page_count(srq
->umem
),
125 ilog2(srq
->umem
->page_size
), &srq
->mtt
);
129 err
= mlx4_ib_umem_write_mtt(dev
, &srq
->mtt
, srq
->umem
);
133 err
= mlx4_ib_db_map_user(to_mucontext(pd
->uobject
->context
),
134 ucmd
.db_addr
, &srq
->db
);
138 err
= mlx4_ib_db_alloc(dev
, &srq
->db
, 0);
144 if (mlx4_buf_alloc(dev
->dev
, buf_size
, PAGE_SIZE
* 2, &srq
->buf
)) {
150 srq
->tail
= srq
->msrq
.max
- 1;
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));
159 err
= mlx4_mtt_init(dev
->dev
, srq
->buf
.npages
, srq
->buf
.page_shift
,
164 err
= mlx4_buf_write_mtt(dev
->dev
, &srq
->mtt
, &srq
->buf
);
168 srq
->wrid
= kmalloc(srq
->msrq
.max
* sizeof (u64
), GFP_KERNEL
);
175 err
= mlx4_srq_alloc(dev
->dev
, to_mpd(pd
)->pdn
, &srq
->mtt
,
176 srq
->db
.dma
, &srq
->msrq
);
180 srq
->msrq
.event
= mlx4_ib_srq_event
;
183 if (ib_copy_to_udata(udata
, &srq
->msrq
.srqn
, sizeof (__u32
))) {
188 init_attr
->attr
.max_wr
= srq
->msrq
.max
- 1;
194 mlx4_ib_db_unmap_user(to_mucontext(pd
->uobject
->context
), &srq
->db
);
199 mlx4_mtt_cleanup(dev
->dev
, &srq
->mtt
);
203 ib_umem_release(srq
->umem
);
205 mlx4_buf_free(dev
->dev
, buf_size
, &srq
->buf
);
209 mlx4_ib_db_free(dev
, &srq
->db
);
217 int mlx4_ib_modify_srq(struct ib_srq
*ibsrq
, struct ib_srq_attr
*attr
,
218 enum ib_srq_attr_mask attr_mask
, struct ib_udata
*udata
)
220 struct mlx4_ib_dev
*dev
= to_mdev(ibsrq
->device
);
221 struct mlx4_ib_srq
*srq
= to_msrq(ibsrq
);
224 /* We don't support resizing SRQs (yet?) */
225 if (attr_mask
& IB_SRQ_MAX_WR
)
228 if (attr_mask
& IB_SRQ_LIMIT
) {
229 if (attr
->srq_limit
>= srq
->msrq
.max
)
232 mutex_lock(&srq
->mutex
);
233 ret
= mlx4_srq_arm(dev
->dev
, &srq
->msrq
, attr
->srq_limit
);
234 mutex_unlock(&srq
->mutex
);
243 int mlx4_ib_destroy_srq(struct ib_srq
*srq
)
245 struct mlx4_ib_dev
*dev
= to_mdev(srq
->device
);
246 struct mlx4_ib_srq
*msrq
= to_msrq(srq
);
248 mlx4_srq_free(dev
->dev
, &msrq
->msrq
);
249 mlx4_mtt_cleanup(dev
->dev
, &msrq
->mtt
);
252 mlx4_ib_db_unmap_user(to_mucontext(srq
->uobject
->context
), &msrq
->db
);
253 ib_umem_release(msrq
->umem
);
256 mlx4_buf_free(dev
->dev
, msrq
->msrq
.max
<< msrq
->msrq
.wqe_shift
,
258 mlx4_ib_db_free(dev
, &msrq
->db
);
266 void mlx4_ib_free_srq_wqe(struct mlx4_ib_srq
*srq
, int wqe_index
)
268 struct mlx4_wqe_srq_next_seg
*next
;
270 /* always called with interrupts disabled. */
271 spin_lock(&srq
->lock
);
273 next
= get_wqe(srq
, srq
->tail
);
274 next
->next_wqe_index
= cpu_to_be16(wqe_index
);
275 srq
->tail
= wqe_index
;
277 spin_unlock(&srq
->lock
);
280 int mlx4_ib_post_srq_recv(struct ib_srq
*ibsrq
, struct ib_recv_wr
*wr
,
281 struct ib_recv_wr
**bad_wr
)
283 struct mlx4_ib_srq
*srq
= to_msrq(ibsrq
);
284 struct mlx4_wqe_srq_next_seg
*next
;
285 struct mlx4_wqe_data_seg
*scat
;
291 spin_lock_irqsave(&srq
->lock
, flags
);
293 for (nreq
= 0; wr
; ++nreq
, wr
= wr
->next
) {
294 if (unlikely(wr
->num_sge
> srq
->msrq
.max_gs
)) {
300 srq
->wrid
[srq
->head
] = wr
->wr_id
;
302 next
= get_wqe(srq
, srq
->head
);
303 srq
->head
= be16_to_cpu(next
->next_wqe_index
);
304 scat
= (struct mlx4_wqe_data_seg
*) (next
+ 1);
306 for (i
= 0; i
< wr
->num_sge
; ++i
) {
307 scat
[i
].byte_count
= cpu_to_be32(wr
->sg_list
[i
].length
);
308 scat
[i
].lkey
= cpu_to_be32(wr
->sg_list
[i
].lkey
);
309 scat
[i
].addr
= cpu_to_be64(wr
->sg_list
[i
].addr
);
312 if (i
< srq
->msrq
.max_gs
) {
313 scat
[i
].byte_count
= 0;
314 scat
[i
].lkey
= cpu_to_be32(MLX4_INVALID_LKEY
);
320 srq
->wqe_ctr
+= nreq
;
323 * Make sure that descriptors are written before
328 *srq
->db
.db
= cpu_to_be32(srq
->wqe_ctr
);
331 spin_unlock_irqrestore(&srq
->lock
, flags
);