blob: 17f60fe6e5b6c489fe4f2103c14fa9100d59e76b [file] [log] [blame]
Roland Dreier225c7b12007-05-08 18:00:38 -07001/*
2 * Copyright (c) 2007 Cisco Systems, Inc. All rights reserved.
Jack Morgenstein51a379d2008-07-25 10:32:52 -07003 * Copyright (c) 2007, 2008 Mellanox Technologies. All rights reserved.
Roland Dreier225c7b12007-05-08 18:00:38 -07004 *
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
Jack Morgensteinea54b102008-01-28 10:40:59 +020034#include <linux/log2.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090035#include <linux/slab.h>
Eli Cohenfa417f72010-10-24 21:08:52 -070036#include <linux/netdevice.h>
Jack Morgensteinea54b102008-01-28 10:40:59 +020037
Roland Dreier225c7b12007-05-08 18:00:38 -070038#include <rdma/ib_cache.h>
39#include <rdma/ib_pack.h>
40
41#include <linux/mlx4/qp.h>
42
43#include "mlx4_ib.h"
44#include "user.h"
45
46enum {
47 MLX4_IB_ACK_REQ_FREQ = 8,
48};
49
50enum {
51 MLX4_IB_DEFAULT_SCHED_QUEUE = 0x83,
Eli Cohenfa417f72010-10-24 21:08:52 -070052 MLX4_IB_DEFAULT_QP0_SCHED_QUEUE = 0x3f,
53 MLX4_IB_LINK_TYPE_IB = 0,
54 MLX4_IB_LINK_TYPE_ETH = 1
Roland Dreier225c7b12007-05-08 18:00:38 -070055};
56
57enum {
58 /*
Eli Cohenfa417f72010-10-24 21:08:52 -070059 * Largest possible UD header: send with GRH and immediate
60 * data plus 14 bytes for an Ethernet header. (LRH would only
61 * use 8 bytes, so Ethernet is the biggest case)
Roland Dreier225c7b12007-05-08 18:00:38 -070062 */
Eli Cohenfa417f72010-10-24 21:08:52 -070063 MLX4_IB_UD_HEADER_SIZE = 78,
Eli Cohen417608c2009-11-12 11:19:44 -080064 MLX4_IB_LSO_HEADER_SPARE = 128,
Roland Dreier225c7b12007-05-08 18:00:38 -070065};
66
Eli Cohenfa417f72010-10-24 21:08:52 -070067enum {
68 MLX4_IB_IBOE_ETHERTYPE = 0x8915
69};
70
Roland Dreier225c7b12007-05-08 18:00:38 -070071struct mlx4_ib_sqp {
72 struct mlx4_ib_qp qp;
73 int pkey_index;
74 u32 qkey;
75 u32 send_psn;
76 struct ib_ud_header ud_header;
77 u8 header_buf[MLX4_IB_UD_HEADER_SIZE];
78};
79
Jack Morgenstein83904132007-10-18 17:36:43 +020080enum {
Eli Cohen417608c2009-11-12 11:19:44 -080081 MLX4_IB_MIN_SQ_STRIDE = 6,
82 MLX4_IB_CACHE_LINE_SIZE = 64,
Jack Morgenstein83904132007-10-18 17:36:43 +020083};
84
Roland Dreier225c7b12007-05-08 18:00:38 -070085static const __be32 mlx4_ib_opcode[] = {
Vladimir Sokolovsky6fa8f712010-04-14 17:23:39 +030086 [IB_WR_SEND] = cpu_to_be32(MLX4_OPCODE_SEND),
87 [IB_WR_LSO] = cpu_to_be32(MLX4_OPCODE_LSO),
88 [IB_WR_SEND_WITH_IMM] = cpu_to_be32(MLX4_OPCODE_SEND_IMM),
89 [IB_WR_RDMA_WRITE] = cpu_to_be32(MLX4_OPCODE_RDMA_WRITE),
90 [IB_WR_RDMA_WRITE_WITH_IMM] = cpu_to_be32(MLX4_OPCODE_RDMA_WRITE_IMM),
91 [IB_WR_RDMA_READ] = cpu_to_be32(MLX4_OPCODE_RDMA_READ),
92 [IB_WR_ATOMIC_CMP_AND_SWP] = cpu_to_be32(MLX4_OPCODE_ATOMIC_CS),
93 [IB_WR_ATOMIC_FETCH_AND_ADD] = cpu_to_be32(MLX4_OPCODE_ATOMIC_FA),
94 [IB_WR_SEND_WITH_INV] = cpu_to_be32(MLX4_OPCODE_SEND_INVAL),
95 [IB_WR_LOCAL_INV] = cpu_to_be32(MLX4_OPCODE_LOCAL_INVAL),
96 [IB_WR_FAST_REG_MR] = cpu_to_be32(MLX4_OPCODE_FMR),
97 [IB_WR_MASKED_ATOMIC_CMP_AND_SWP] = cpu_to_be32(MLX4_OPCODE_MASKED_ATOMIC_CS),
98 [IB_WR_MASKED_ATOMIC_FETCH_AND_ADD] = cpu_to_be32(MLX4_OPCODE_MASKED_ATOMIC_FA),
Roland Dreier225c7b12007-05-08 18:00:38 -070099};
100
101static struct mlx4_ib_sqp *to_msqp(struct mlx4_ib_qp *mqp)
102{
103 return container_of(mqp, struct mlx4_ib_sqp, qp);
104}
105
106static int is_sqp(struct mlx4_ib_dev *dev, struct mlx4_ib_qp *qp)
107{
108 return qp->mqp.qpn >= dev->dev->caps.sqp_start &&
109 qp->mqp.qpn <= dev->dev->caps.sqp_start + 3;
110}
111
112static int is_qp0(struct mlx4_ib_dev *dev, struct mlx4_ib_qp *qp)
113{
114 return qp->mqp.qpn >= dev->dev->caps.sqp_start &&
115 qp->mqp.qpn <= dev->dev->caps.sqp_start + 1;
116}
117
118static void *get_wqe(struct mlx4_ib_qp *qp, int offset)
119{
Roland Dreier1c69fc22008-02-06 21:07:54 -0800120 return mlx4_buf_offset(&qp->buf, offset);
Roland Dreier225c7b12007-05-08 18:00:38 -0700121}
122
123static void *get_recv_wqe(struct mlx4_ib_qp *qp, int n)
124{
125 return get_wqe(qp, qp->rq.offset + (n << qp->rq.wqe_shift));
126}
127
128static void *get_send_wqe(struct mlx4_ib_qp *qp, int n)
129{
130 return get_wqe(qp, qp->sq.offset + (n << qp->sq.wqe_shift));
131}
132
Roland Dreier0e6e7412007-06-18 08:13:48 -0700133/*
134 * Stamp a SQ WQE so that it is invalid if prefetched by marking the
Jack Morgensteinea54b102008-01-28 10:40:59 +0200135 * first four bytes of every 64 byte chunk with
136 * 0x7FFFFFF | (invalid_ownership_value << 31).
137 *
138 * When the max work request size is less than or equal to the WQE
139 * basic block size, as an optimization, we can stamp all WQEs with
140 * 0xffffffff, and skip the very first chunk of each WQE.
Roland Dreier0e6e7412007-06-18 08:13:48 -0700141 */
Jack Morgensteinea54b102008-01-28 10:40:59 +0200142static void stamp_send_wqe(struct mlx4_ib_qp *qp, int n, int size)
Roland Dreier0e6e7412007-06-18 08:13:48 -0700143{
Roland Dreierd2ae16d2008-04-16 21:01:07 -0700144 __be32 *wqe;
Roland Dreier0e6e7412007-06-18 08:13:48 -0700145 int i;
Jack Morgensteinea54b102008-01-28 10:40:59 +0200146 int s;
147 int ind;
148 void *buf;
149 __be32 stamp;
Eli Cohen9670e552008-07-14 23:48:44 -0700150 struct mlx4_wqe_ctrl_seg *ctrl;
Roland Dreier0e6e7412007-06-18 08:13:48 -0700151
Jack Morgensteinea54b102008-01-28 10:40:59 +0200152 if (qp->sq_max_wqes_per_wr > 1) {
Eli Cohen9670e552008-07-14 23:48:44 -0700153 s = roundup(size, 1U << qp->sq.wqe_shift);
Jack Morgensteinea54b102008-01-28 10:40:59 +0200154 for (i = 0; i < s; i += 64) {
155 ind = (i >> qp->sq.wqe_shift) + n;
156 stamp = ind & qp->sq.wqe_cnt ? cpu_to_be32(0x7fffffff) :
157 cpu_to_be32(0xffffffff);
158 buf = get_send_wqe(qp, ind & (qp->sq.wqe_cnt - 1));
159 wqe = buf + (i & ((1 << qp->sq.wqe_shift) - 1));
160 *wqe = stamp;
161 }
162 } else {
Eli Cohen9670e552008-07-14 23:48:44 -0700163 ctrl = buf = get_send_wqe(qp, n & (qp->sq.wqe_cnt - 1));
164 s = (ctrl->fence_size & 0x3f) << 4;
Jack Morgensteinea54b102008-01-28 10:40:59 +0200165 for (i = 64; i < s; i += 64) {
166 wqe = buf + i;
Roland Dreierd2ae16d2008-04-16 21:01:07 -0700167 *wqe = cpu_to_be32(0xffffffff);
Jack Morgensteinea54b102008-01-28 10:40:59 +0200168 }
169 }
170}
171
172static void post_nop_wqe(struct mlx4_ib_qp *qp, int n, int size)
173{
174 struct mlx4_wqe_ctrl_seg *ctrl;
175 struct mlx4_wqe_inline_seg *inl;
176 void *wqe;
177 int s;
178
179 ctrl = wqe = get_send_wqe(qp, n & (qp->sq.wqe_cnt - 1));
180 s = sizeof(struct mlx4_wqe_ctrl_seg);
181
182 if (qp->ibqp.qp_type == IB_QPT_UD) {
183 struct mlx4_wqe_datagram_seg *dgram = wqe + sizeof *ctrl;
184 struct mlx4_av *av = (struct mlx4_av *)dgram->av;
185 memset(dgram, 0, sizeof *dgram);
186 av->port_pd = cpu_to_be32((qp->port << 24) | to_mpd(qp->ibqp.pd)->pdn);
187 s += sizeof(struct mlx4_wqe_datagram_seg);
188 }
189
190 /* Pad the remainder of the WQE with an inline data segment. */
191 if (size > s) {
192 inl = wqe + s;
193 inl->byte_count = cpu_to_be32(1 << 31 | (size - s - sizeof *inl));
194 }
195 ctrl->srcrb_flags = 0;
196 ctrl->fence_size = size / 16;
197 /*
198 * Make sure descriptor is fully written before setting ownership bit
199 * (because HW can start executing as soon as we do).
200 */
201 wmb();
202
203 ctrl->owner_opcode = cpu_to_be32(MLX4_OPCODE_NOP | MLX4_WQE_CTRL_NEC) |
204 (n & qp->sq.wqe_cnt ? cpu_to_be32(1 << 31) : 0);
205
206 stamp_send_wqe(qp, n + qp->sq_spare_wqes, size);
207}
208
209/* Post NOP WQE to prevent wrap-around in the middle of WR */
210static inline unsigned pad_wraparound(struct mlx4_ib_qp *qp, int ind)
211{
212 unsigned s = qp->sq.wqe_cnt - (ind & (qp->sq.wqe_cnt - 1));
213 if (unlikely(s < qp->sq_max_wqes_per_wr)) {
214 post_nop_wqe(qp, ind, s << qp->sq.wqe_shift);
215 ind += s;
216 }
217 return ind;
Roland Dreier0e6e7412007-06-18 08:13:48 -0700218}
219
Roland Dreier225c7b12007-05-08 18:00:38 -0700220static void mlx4_ib_qp_event(struct mlx4_qp *qp, enum mlx4_event type)
221{
222 struct ib_event event;
223 struct ib_qp *ibqp = &to_mibqp(qp)->ibqp;
224
225 if (type == MLX4_EVENT_TYPE_PATH_MIG)
226 to_mibqp(qp)->port = to_mibqp(qp)->alt_port;
227
228 if (ibqp->event_handler) {
229 event.device = ibqp->device;
230 event.element.qp = ibqp;
231 switch (type) {
232 case MLX4_EVENT_TYPE_PATH_MIG:
233 event.event = IB_EVENT_PATH_MIG;
234 break;
235 case MLX4_EVENT_TYPE_COMM_EST:
236 event.event = IB_EVENT_COMM_EST;
237 break;
238 case MLX4_EVENT_TYPE_SQ_DRAINED:
239 event.event = IB_EVENT_SQ_DRAINED;
240 break;
241 case MLX4_EVENT_TYPE_SRQ_QP_LAST_WQE:
242 event.event = IB_EVENT_QP_LAST_WQE_REACHED;
243 break;
244 case MLX4_EVENT_TYPE_WQ_CATAS_ERROR:
245 event.event = IB_EVENT_QP_FATAL;
246 break;
247 case MLX4_EVENT_TYPE_PATH_MIG_FAILED:
248 event.event = IB_EVENT_PATH_MIG_ERR;
249 break;
250 case MLX4_EVENT_TYPE_WQ_INVAL_REQ_ERROR:
251 event.event = IB_EVENT_QP_REQ_ERR;
252 break;
253 case MLX4_EVENT_TYPE_WQ_ACCESS_ERROR:
254 event.event = IB_EVENT_QP_ACCESS_ERR;
255 break;
256 default:
257 printk(KERN_WARNING "mlx4_ib: Unexpected event type %d "
258 "on QP %06x\n", type, qp->qpn);
259 return;
260 }
261
262 ibqp->event_handler(&event, ibqp->qp_context);
263 }
264}
265
Eli Cohenb832be12008-04-16 21:09:27 -0700266static int send_wqe_overhead(enum ib_qp_type type, u32 flags)
Roland Dreier225c7b12007-05-08 18:00:38 -0700267{
268 /*
269 * UD WQEs must have a datagram segment.
270 * RC and UC WQEs might have a remote address segment.
271 * MLX WQEs need two extra inline data segments (for the UD
272 * header and space for the ICRC).
273 */
274 switch (type) {
275 case IB_QPT_UD:
276 return sizeof (struct mlx4_wqe_ctrl_seg) +
Eli Cohenb832be12008-04-16 21:09:27 -0700277 sizeof (struct mlx4_wqe_datagram_seg) +
Eli Cohen417608c2009-11-12 11:19:44 -0800278 ((flags & MLX4_IB_QP_LSO) ? MLX4_IB_LSO_HEADER_SPARE : 0);
Roland Dreier225c7b12007-05-08 18:00:38 -0700279 case IB_QPT_UC:
280 return sizeof (struct mlx4_wqe_ctrl_seg) +
281 sizeof (struct mlx4_wqe_raddr_seg);
282 case IB_QPT_RC:
283 return sizeof (struct mlx4_wqe_ctrl_seg) +
284 sizeof (struct mlx4_wqe_atomic_seg) +
285 sizeof (struct mlx4_wqe_raddr_seg);
286 case IB_QPT_SMI:
287 case IB_QPT_GSI:
288 return sizeof (struct mlx4_wqe_ctrl_seg) +
289 ALIGN(MLX4_IB_UD_HEADER_SIZE +
Roland Dreiere61ef242007-06-18 09:23:47 -0700290 DIV_ROUND_UP(MLX4_IB_UD_HEADER_SIZE,
291 MLX4_INLINE_ALIGN) *
Roland Dreier225c7b12007-05-08 18:00:38 -0700292 sizeof (struct mlx4_wqe_inline_seg),
293 sizeof (struct mlx4_wqe_data_seg)) +
294 ALIGN(4 +
295 sizeof (struct mlx4_wqe_inline_seg),
296 sizeof (struct mlx4_wqe_data_seg));
297 default:
298 return sizeof (struct mlx4_wqe_ctrl_seg);
299 }
300}
301
Eli Cohen24463042007-05-17 10:32:41 +0300302static int set_rq_size(struct mlx4_ib_dev *dev, struct ib_qp_cap *cap,
Roland Dreiera4cd7ed2007-06-07 23:24:39 -0700303 int is_user, int has_srq, struct mlx4_ib_qp *qp)
Roland Dreier225c7b12007-05-08 18:00:38 -0700304{
Eli Cohen24463042007-05-17 10:32:41 +0300305 /* Sanity check RQ size before proceeding */
306 if (cap->max_recv_wr > dev->dev->caps.max_wqes ||
307 cap->max_recv_sge > dev->dev->caps.max_rq_sg)
308 return -EINVAL;
309
Roland Dreiera4cd7ed2007-06-07 23:24:39 -0700310 if (has_srq) {
311 /* QPs attached to an SRQ should have no RQ */
312 if (cap->max_recv_wr)
313 return -EINVAL;
Eli Cohen24463042007-05-17 10:32:41 +0300314
Roland Dreier0e6e7412007-06-18 08:13:48 -0700315 qp->rq.wqe_cnt = qp->rq.max_gs = 0;
Roland Dreiera4cd7ed2007-06-07 23:24:39 -0700316 } else {
317 /* HW requires >= 1 RQ entry with >= 1 gather entry */
318 if (is_user && (!cap->max_recv_wr || !cap->max_recv_sge))
319 return -EINVAL;
320
Roland Dreier0e6e7412007-06-18 08:13:48 -0700321 qp->rq.wqe_cnt = roundup_pow_of_two(max(1U, cap->max_recv_wr));
Roland Dreier42c059ea2007-06-12 10:52:02 -0700322 qp->rq.max_gs = roundup_pow_of_two(max(1U, cap->max_recv_sge));
Roland Dreiera4cd7ed2007-06-07 23:24:39 -0700323 qp->rq.wqe_shift = ilog2(qp->rq.max_gs * sizeof (struct mlx4_wqe_data_seg));
324 }
Eli Cohen24463042007-05-17 10:32:41 +0300325
Roland Dreier0e6e7412007-06-18 08:13:48 -0700326 cap->max_recv_wr = qp->rq.max_post = qp->rq.wqe_cnt;
Eli Cohen24463042007-05-17 10:32:41 +0300327 cap->max_recv_sge = qp->rq.max_gs;
328
329 return 0;
330}
331
332static int set_kernel_sq_size(struct mlx4_ib_dev *dev, struct ib_qp_cap *cap,
333 enum ib_qp_type type, struct mlx4_ib_qp *qp)
334{
Jack Morgensteinea54b102008-01-28 10:40:59 +0200335 int s;
336
Eli Cohen24463042007-05-17 10:32:41 +0300337 /* Sanity check SQ size before proceeding */
Roland Dreier225c7b12007-05-08 18:00:38 -0700338 if (cap->max_send_wr > dev->dev->caps.max_wqes ||
Roland Dreier225c7b12007-05-08 18:00:38 -0700339 cap->max_send_sge > dev->dev->caps.max_sq_sg ||
Eli Cohenb832be12008-04-16 21:09:27 -0700340 cap->max_inline_data + send_wqe_overhead(type, qp->flags) +
Roland Dreier225c7b12007-05-08 18:00:38 -0700341 sizeof (struct mlx4_wqe_inline_seg) > dev->dev->caps.max_sq_desc_sz)
342 return -EINVAL;
343
344 /*
345 * For MLX transport we need 2 extra S/G entries:
346 * one for the header and one for the checksum at the end
347 */
348 if ((type == IB_QPT_SMI || type == IB_QPT_GSI) &&
349 cap->max_send_sge + 2 > dev->dev->caps.max_sq_sg)
350 return -EINVAL;
351
Jack Morgensteinea54b102008-01-28 10:40:59 +0200352 s = max(cap->max_send_sge * sizeof (struct mlx4_wqe_data_seg),
353 cap->max_inline_data + sizeof (struct mlx4_wqe_inline_seg)) +
Eli Cohenb832be12008-04-16 21:09:27 -0700354 send_wqe_overhead(type, qp->flags);
Roland Dreier225c7b12007-05-08 18:00:38 -0700355
Roland Dreiercd155c12008-05-20 14:00:02 -0700356 if (s > dev->dev->caps.max_sq_desc_sz)
357 return -EINVAL;
358
Roland Dreier0e6e7412007-06-18 08:13:48 -0700359 /*
Jack Morgensteinea54b102008-01-28 10:40:59 +0200360 * Hermon supports shrinking WQEs, such that a single work
361 * request can include multiple units of 1 << wqe_shift. This
362 * way, work requests can differ in size, and do not have to
363 * be a power of 2 in size, saving memory and speeding up send
364 * WR posting. Unfortunately, if we do this then the
365 * wqe_index field in CQEs can't be used to look up the WR ID
366 * anymore, so we do this only if selective signaling is off.
367 *
368 * Further, on 32-bit platforms, we can't use vmap() to make
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200369 * the QP buffer virtually contiguous. Thus we have to use
Jack Morgensteinea54b102008-01-28 10:40:59 +0200370 * constant-sized WRs to make sure a WR is always fully within
371 * a single page-sized chunk.
372 *
373 * Finally, we use NOP work requests to pad the end of the
374 * work queue, to avoid wrap-around in the middle of WR. We
375 * set NEC bit to avoid getting completions with error for
376 * these NOP WRs, but since NEC is only supported starting
377 * with firmware 2.2.232, we use constant-sized WRs for older
378 * firmware.
379 *
380 * And, since MLX QPs only support SEND, we use constant-sized
381 * WRs in this case.
382 *
383 * We look for the smallest value of wqe_shift such that the
384 * resulting number of wqes does not exceed device
385 * capabilities.
386 *
387 * We set WQE size to at least 64 bytes, this way stamping
388 * invalidates each WQE.
Roland Dreier0e6e7412007-06-18 08:13:48 -0700389 */
Jack Morgensteinea54b102008-01-28 10:40:59 +0200390 if (dev->dev->caps.fw_ver >= MLX4_FW_VER_WQE_CTRL_NEC &&
391 qp->sq_signal_bits && BITS_PER_LONG == 64 &&
392 type != IB_QPT_SMI && type != IB_QPT_GSI)
393 qp->sq.wqe_shift = ilog2(64);
394 else
395 qp->sq.wqe_shift = ilog2(roundup_pow_of_two(s));
396
397 for (;;) {
Jack Morgensteinea54b102008-01-28 10:40:59 +0200398 qp->sq_max_wqes_per_wr = DIV_ROUND_UP(s, 1U << qp->sq.wqe_shift);
399
400 /*
401 * We need to leave 2 KB + 1 WR of headroom in the SQ to
402 * allow HW to prefetch.
403 */
404 qp->sq_spare_wqes = (2048 >> qp->sq.wqe_shift) + qp->sq_max_wqes_per_wr;
405 qp->sq.wqe_cnt = roundup_pow_of_two(cap->max_send_wr *
406 qp->sq_max_wqes_per_wr +
407 qp->sq_spare_wqes);
408
409 if (qp->sq.wqe_cnt <= dev->dev->caps.max_wqes)
410 break;
411
412 if (qp->sq_max_wqes_per_wr <= 1)
413 return -EINVAL;
414
415 ++qp->sq.wqe_shift;
416 }
417
Roland Dreiercd155c12008-05-20 14:00:02 -0700418 qp->sq.max_gs = (min(dev->dev->caps.max_sq_desc_sz,
419 (qp->sq_max_wqes_per_wr << qp->sq.wqe_shift)) -
Eli Cohenb832be12008-04-16 21:09:27 -0700420 send_wqe_overhead(type, qp->flags)) /
421 sizeof (struct mlx4_wqe_data_seg);
Roland Dreier0e6e7412007-06-18 08:13:48 -0700422
423 qp->buf_size = (qp->rq.wqe_cnt << qp->rq.wqe_shift) +
424 (qp->sq.wqe_cnt << qp->sq.wqe_shift);
Roland Dreier225c7b12007-05-08 18:00:38 -0700425 if (qp->rq.wqe_shift > qp->sq.wqe_shift) {
426 qp->rq.offset = 0;
Roland Dreier0e6e7412007-06-18 08:13:48 -0700427 qp->sq.offset = qp->rq.wqe_cnt << qp->rq.wqe_shift;
Roland Dreier225c7b12007-05-08 18:00:38 -0700428 } else {
Roland Dreier0e6e7412007-06-18 08:13:48 -0700429 qp->rq.offset = qp->sq.wqe_cnt << qp->sq.wqe_shift;
Roland Dreier225c7b12007-05-08 18:00:38 -0700430 qp->sq.offset = 0;
431 }
432
Jack Morgensteinea54b102008-01-28 10:40:59 +0200433 cap->max_send_wr = qp->sq.max_post =
434 (qp->sq.wqe_cnt - qp->sq_spare_wqes) / qp->sq_max_wqes_per_wr;
Roland Dreiercd155c12008-05-20 14:00:02 -0700435 cap->max_send_sge = min(qp->sq.max_gs,
436 min(dev->dev->caps.max_sq_sg,
437 dev->dev->caps.max_rq_sg));
Roland Dreier54e95f82007-06-18 08:13:53 -0700438 /* We don't support inline sends for kernel QPs (yet) */
439 cap->max_inline_data = 0;
Roland Dreier225c7b12007-05-08 18:00:38 -0700440
441 return 0;
442}
443
Jack Morgenstein83904132007-10-18 17:36:43 +0200444static int set_user_sq_size(struct mlx4_ib_dev *dev,
445 struct mlx4_ib_qp *qp,
Eli Cohen24463042007-05-17 10:32:41 +0300446 struct mlx4_ib_create_qp *ucmd)
447{
Jack Morgenstein83904132007-10-18 17:36:43 +0200448 /* Sanity check SQ size before proceeding */
449 if ((1 << ucmd->log_sq_bb_count) > dev->dev->caps.max_wqes ||
450 ucmd->log_sq_stride >
451 ilog2(roundup_pow_of_two(dev->dev->caps.max_sq_desc_sz)) ||
452 ucmd->log_sq_stride < MLX4_IB_MIN_SQ_STRIDE)
453 return -EINVAL;
454
Roland Dreier0e6e7412007-06-18 08:13:48 -0700455 qp->sq.wqe_cnt = 1 << ucmd->log_sq_bb_count;
Eli Cohen24463042007-05-17 10:32:41 +0300456 qp->sq.wqe_shift = ucmd->log_sq_stride;
457
Roland Dreier0e6e7412007-06-18 08:13:48 -0700458 qp->buf_size = (qp->rq.wqe_cnt << qp->rq.wqe_shift) +
459 (qp->sq.wqe_cnt << qp->sq.wqe_shift);
Eli Cohen24463042007-05-17 10:32:41 +0300460
461 return 0;
462}
463
Roland Dreier225c7b12007-05-08 18:00:38 -0700464static int create_qp_common(struct mlx4_ib_dev *dev, struct ib_pd *pd,
465 struct ib_qp_init_attr *init_attr,
466 struct ib_udata *udata, int sqpn, struct mlx4_ib_qp *qp)
467{
Yevgeny Petrilina3cdcbf2008-10-10 12:01:37 -0700468 int qpn;
Roland Dreier225c7b12007-05-08 18:00:38 -0700469 int err;
Roland Dreier225c7b12007-05-08 18:00:38 -0700470
471 mutex_init(&qp->mutex);
472 spin_lock_init(&qp->sq.lock);
473 spin_lock_init(&qp->rq.lock);
Eli Cohenfa417f72010-10-24 21:08:52 -0700474 INIT_LIST_HEAD(&qp->gid_list);
Roland Dreier225c7b12007-05-08 18:00:38 -0700475
476 qp->state = IB_QPS_RESET;
Jack Morgensteinea54b102008-01-28 10:40:59 +0200477 if (init_attr->sq_sig_type == IB_SIGNAL_ALL_WR)
478 qp->sq_signal_bits = cpu_to_be32(MLX4_WQE_CTRL_CQ_UPDATE);
Roland Dreier225c7b12007-05-08 18:00:38 -0700479
Roland Dreiera4cd7ed2007-06-07 23:24:39 -0700480 err = set_rq_size(dev, &init_attr->cap, !!pd->uobject, !!init_attr->srq, qp);
Roland Dreier225c7b12007-05-08 18:00:38 -0700481 if (err)
482 goto err;
483
484 if (pd->uobject) {
485 struct mlx4_ib_create_qp ucmd;
486
487 if (ib_copy_from_udata(&ucmd, udata, sizeof ucmd)) {
488 err = -EFAULT;
489 goto err;
490 }
491
Roland Dreier0e6e7412007-06-18 08:13:48 -0700492 qp->sq_no_prefetch = ucmd.sq_no_prefetch;
493
Jack Morgenstein83904132007-10-18 17:36:43 +0200494 err = set_user_sq_size(dev, qp, &ucmd);
Eli Cohen24463042007-05-17 10:32:41 +0300495 if (err)
496 goto err;
497
Roland Dreier225c7b12007-05-08 18:00:38 -0700498 qp->umem = ib_umem_get(pd->uobject->context, ucmd.buf_addr,
Arthur Kepnercb9fbc52008-04-29 01:00:34 -0700499 qp->buf_size, 0, 0);
Roland Dreier225c7b12007-05-08 18:00:38 -0700500 if (IS_ERR(qp->umem)) {
501 err = PTR_ERR(qp->umem);
502 goto err;
503 }
504
505 err = mlx4_mtt_init(dev->dev, ib_umem_page_count(qp->umem),
506 ilog2(qp->umem->page_size), &qp->mtt);
507 if (err)
508 goto err_buf;
509
510 err = mlx4_ib_umem_write_mtt(dev, &qp->mtt, qp->umem);
511 if (err)
512 goto err_mtt;
513
Roland Dreier02d89b82007-05-23 15:16:08 -0700514 if (!init_attr->srq) {
515 err = mlx4_ib_db_map_user(to_mucontext(pd->uobject->context),
516 ucmd.db_addr, &qp->db);
517 if (err)
518 goto err_mtt;
519 }
Roland Dreier225c7b12007-05-08 18:00:38 -0700520 } else {
Roland Dreier0e6e7412007-06-18 08:13:48 -0700521 qp->sq_no_prefetch = 0;
522
Ron Livne521e5752008-07-14 23:48:48 -0700523 if (init_attr->create_flags & IB_QP_CREATE_BLOCK_MULTICAST_LOOPBACK)
524 qp->flags |= MLX4_IB_QP_BLOCK_MULTICAST_LOOPBACK;
525
Eli Cohenb832be12008-04-16 21:09:27 -0700526 if (init_attr->create_flags & IB_QP_CREATE_IPOIB_UD_LSO)
527 qp->flags |= MLX4_IB_QP_LSO;
528
Eli Cohen24463042007-05-17 10:32:41 +0300529 err = set_kernel_sq_size(dev, &init_attr->cap, init_attr->qp_type, qp);
530 if (err)
531 goto err;
532
Roland Dreier02d89b82007-05-23 15:16:08 -0700533 if (!init_attr->srq) {
Yevgeny Petrilin62968832008-04-23 11:55:45 -0700534 err = mlx4_db_alloc(dev->dev, &qp->db, 0);
Roland Dreier02d89b82007-05-23 15:16:08 -0700535 if (err)
536 goto err;
Roland Dreier225c7b12007-05-08 18:00:38 -0700537
Roland Dreier02d89b82007-05-23 15:16:08 -0700538 *qp->db.db = 0;
539 }
Roland Dreier225c7b12007-05-08 18:00:38 -0700540
541 if (mlx4_buf_alloc(dev->dev, qp->buf_size, PAGE_SIZE * 2, &qp->buf)) {
542 err = -ENOMEM;
543 goto err_db;
544 }
545
546 err = mlx4_mtt_init(dev->dev, qp->buf.npages, qp->buf.page_shift,
547 &qp->mtt);
548 if (err)
549 goto err_buf;
550
551 err = mlx4_buf_write_mtt(dev->dev, &qp->mtt, &qp->buf);
552 if (err)
553 goto err_mtt;
554
Roland Dreier0e6e7412007-06-18 08:13:48 -0700555 qp->sq.wrid = kmalloc(qp->sq.wqe_cnt * sizeof (u64), GFP_KERNEL);
556 qp->rq.wrid = kmalloc(qp->rq.wqe_cnt * sizeof (u64), GFP_KERNEL);
Roland Dreier225c7b12007-05-08 18:00:38 -0700557
558 if (!qp->sq.wrid || !qp->rq.wrid) {
559 err = -ENOMEM;
560 goto err_wrid;
561 }
Roland Dreier225c7b12007-05-08 18:00:38 -0700562 }
563
Yevgeny Petrilina3cdcbf2008-10-10 12:01:37 -0700564 if (sqpn) {
565 qpn = sqpn;
566 } else {
567 err = mlx4_qp_reserve_range(dev->dev, 1, 1, &qpn);
568 if (err)
569 goto err_wrid;
570 }
571
572 err = mlx4_qp_alloc(dev->dev, qpn, &qp->mqp);
Roland Dreier225c7b12007-05-08 18:00:38 -0700573 if (err)
Yevgeny Petrilina3cdcbf2008-10-10 12:01:37 -0700574 goto err_qpn;
Roland Dreier225c7b12007-05-08 18:00:38 -0700575
576 /*
577 * Hardware wants QPN written in big-endian order (after
578 * shifting) for send doorbell. Precompute this value to save
579 * a little bit when posting sends.
580 */
581 qp->doorbell_qpn = swab32(qp->mqp.qpn << 8);
582
Roland Dreier225c7b12007-05-08 18:00:38 -0700583 qp->mqp.event = mlx4_ib_qp_event;
584
585 return 0;
586
Yevgeny Petrilina3cdcbf2008-10-10 12:01:37 -0700587err_qpn:
588 if (!sqpn)
589 mlx4_qp_release_range(dev->dev, qpn, 1);
590
Roland Dreier225c7b12007-05-08 18:00:38 -0700591err_wrid:
Roland Dreier23f1b382007-07-20 21:19:43 -0700592 if (pd->uobject) {
593 if (!init_attr->srq)
594 mlx4_ib_db_unmap_user(to_mucontext(pd->uobject->context),
595 &qp->db);
596 } else {
Roland Dreier225c7b12007-05-08 18:00:38 -0700597 kfree(qp->sq.wrid);
598 kfree(qp->rq.wrid);
599 }
600
601err_mtt:
602 mlx4_mtt_cleanup(dev->dev, &qp->mtt);
603
604err_buf:
605 if (pd->uobject)
606 ib_umem_release(qp->umem);
607 else
608 mlx4_buf_free(dev->dev, qp->buf_size, &qp->buf);
609
610err_db:
Roland Dreier02d89b82007-05-23 15:16:08 -0700611 if (!pd->uobject && !init_attr->srq)
Yevgeny Petrilin62968832008-04-23 11:55:45 -0700612 mlx4_db_free(dev->dev, &qp->db);
Roland Dreier225c7b12007-05-08 18:00:38 -0700613
614err:
615 return err;
616}
617
618static enum mlx4_qp_state to_mlx4_state(enum ib_qp_state state)
619{
620 switch (state) {
621 case IB_QPS_RESET: return MLX4_QP_STATE_RST;
622 case IB_QPS_INIT: return MLX4_QP_STATE_INIT;
623 case IB_QPS_RTR: return MLX4_QP_STATE_RTR;
624 case IB_QPS_RTS: return MLX4_QP_STATE_RTS;
625 case IB_QPS_SQD: return MLX4_QP_STATE_SQD;
626 case IB_QPS_SQE: return MLX4_QP_STATE_SQER;
627 case IB_QPS_ERR: return MLX4_QP_STATE_ERR;
628 default: return -1;
629 }
630}
631
632static void mlx4_ib_lock_cqs(struct mlx4_ib_cq *send_cq, struct mlx4_ib_cq *recv_cq)
Roland Dreier338a8fa2009-09-05 20:24:49 -0700633 __acquires(&send_cq->lock) __acquires(&recv_cq->lock)
Roland Dreier225c7b12007-05-08 18:00:38 -0700634{
Roland Dreier338a8fa2009-09-05 20:24:49 -0700635 if (send_cq == recv_cq) {
Roland Dreier225c7b12007-05-08 18:00:38 -0700636 spin_lock_irq(&send_cq->lock);
Roland Dreier338a8fa2009-09-05 20:24:49 -0700637 __acquire(&recv_cq->lock);
638 } else if (send_cq->mcq.cqn < recv_cq->mcq.cqn) {
Roland Dreier225c7b12007-05-08 18:00:38 -0700639 spin_lock_irq(&send_cq->lock);
640 spin_lock_nested(&recv_cq->lock, SINGLE_DEPTH_NESTING);
641 } else {
642 spin_lock_irq(&recv_cq->lock);
643 spin_lock_nested(&send_cq->lock, SINGLE_DEPTH_NESTING);
644 }
645}
646
647static void mlx4_ib_unlock_cqs(struct mlx4_ib_cq *send_cq, struct mlx4_ib_cq *recv_cq)
Roland Dreier338a8fa2009-09-05 20:24:49 -0700648 __releases(&send_cq->lock) __releases(&recv_cq->lock)
Roland Dreier225c7b12007-05-08 18:00:38 -0700649{
Roland Dreier338a8fa2009-09-05 20:24:49 -0700650 if (send_cq == recv_cq) {
651 __release(&recv_cq->lock);
Roland Dreier225c7b12007-05-08 18:00:38 -0700652 spin_unlock_irq(&send_cq->lock);
Roland Dreier338a8fa2009-09-05 20:24:49 -0700653 } else if (send_cq->mcq.cqn < recv_cq->mcq.cqn) {
Roland Dreier225c7b12007-05-08 18:00:38 -0700654 spin_unlock(&recv_cq->lock);
655 spin_unlock_irq(&send_cq->lock);
656 } else {
657 spin_unlock(&send_cq->lock);
658 spin_unlock_irq(&recv_cq->lock);
659 }
660}
661
Eli Cohenfa417f72010-10-24 21:08:52 -0700662static void del_gid_entries(struct mlx4_ib_qp *qp)
663{
664 struct mlx4_ib_gid_entry *ge, *tmp;
665
666 list_for_each_entry_safe(ge, tmp, &qp->gid_list, list) {
667 list_del(&ge->list);
668 kfree(ge);
669 }
670}
671
Roland Dreier225c7b12007-05-08 18:00:38 -0700672static void destroy_qp_common(struct mlx4_ib_dev *dev, struct mlx4_ib_qp *qp,
673 int is_user)
674{
675 struct mlx4_ib_cq *send_cq, *recv_cq;
676
677 if (qp->state != IB_QPS_RESET)
678 if (mlx4_qp_modify(dev->dev, NULL, to_mlx4_state(qp->state),
679 MLX4_QP_STATE_RST, NULL, 0, 0, &qp->mqp))
680 printk(KERN_WARNING "mlx4_ib: modify QP %06x to RESET failed.\n",
681 qp->mqp.qpn);
682
683 send_cq = to_mcq(qp->ibqp.send_cq);
684 recv_cq = to_mcq(qp->ibqp.recv_cq);
685
686 mlx4_ib_lock_cqs(send_cq, recv_cq);
687
688 if (!is_user) {
689 __mlx4_ib_cq_clean(recv_cq, qp->mqp.qpn,
690 qp->ibqp.srq ? to_msrq(qp->ibqp.srq): NULL);
691 if (send_cq != recv_cq)
692 __mlx4_ib_cq_clean(send_cq, qp->mqp.qpn, NULL);
693 }
694
695 mlx4_qp_remove(dev->dev, &qp->mqp);
696
697 mlx4_ib_unlock_cqs(send_cq, recv_cq);
698
699 mlx4_qp_free(dev->dev, &qp->mqp);
Yevgeny Petrilina3cdcbf2008-10-10 12:01:37 -0700700
701 if (!is_sqp(dev, qp))
702 mlx4_qp_release_range(dev->dev, qp->mqp.qpn, 1);
703
Roland Dreier225c7b12007-05-08 18:00:38 -0700704 mlx4_mtt_cleanup(dev->dev, &qp->mtt);
705
706 if (is_user) {
Roland Dreier02d89b82007-05-23 15:16:08 -0700707 if (!qp->ibqp.srq)
708 mlx4_ib_db_unmap_user(to_mucontext(qp->ibqp.uobject->context),
709 &qp->db);
Roland Dreier225c7b12007-05-08 18:00:38 -0700710 ib_umem_release(qp->umem);
711 } else {
712 kfree(qp->sq.wrid);
713 kfree(qp->rq.wrid);
714 mlx4_buf_free(dev->dev, qp->buf_size, &qp->buf);
Roland Dreier02d89b82007-05-23 15:16:08 -0700715 if (!qp->ibqp.srq)
Yevgeny Petrilin62968832008-04-23 11:55:45 -0700716 mlx4_db_free(dev->dev, &qp->db);
Roland Dreier225c7b12007-05-08 18:00:38 -0700717 }
Eli Cohenfa417f72010-10-24 21:08:52 -0700718
719 del_gid_entries(qp);
Roland Dreier225c7b12007-05-08 18:00:38 -0700720}
721
722struct ib_qp *mlx4_ib_create_qp(struct ib_pd *pd,
723 struct ib_qp_init_attr *init_attr,
724 struct ib_udata *udata)
725{
726 struct mlx4_ib_dev *dev = to_mdev(pd->device);
727 struct mlx4_ib_sqp *sqp;
728 struct mlx4_ib_qp *qp;
729 int err;
730
Ron Livne521e5752008-07-14 23:48:48 -0700731 /*
732 * We only support LSO and multicast loopback blocking, and
733 * only for kernel UD QPs.
734 */
735 if (init_attr->create_flags & ~(IB_QP_CREATE_IPOIB_UD_LSO |
736 IB_QP_CREATE_BLOCK_MULTICAST_LOOPBACK))
Eli Cohenb832be12008-04-16 21:09:27 -0700737 return ERR_PTR(-EINVAL);
Ron Livne521e5752008-07-14 23:48:48 -0700738
739 if (init_attr->create_flags &&
Eli Cohenb832be12008-04-16 21:09:27 -0700740 (pd->uobject || init_attr->qp_type != IB_QPT_UD))
Eli Cohenb846f252008-04-16 21:09:27 -0700741 return ERR_PTR(-EINVAL);
742
Roland Dreier225c7b12007-05-08 18:00:38 -0700743 switch (init_attr->qp_type) {
744 case IB_QPT_RC:
745 case IB_QPT_UC:
746 case IB_QPT_UD:
747 {
Eli Cohenf507d282008-07-14 23:48:53 -0700748 qp = kzalloc(sizeof *qp, GFP_KERNEL);
Roland Dreier225c7b12007-05-08 18:00:38 -0700749 if (!qp)
750 return ERR_PTR(-ENOMEM);
751
752 err = create_qp_common(dev, pd, init_attr, udata, 0, qp);
753 if (err) {
754 kfree(qp);
755 return ERR_PTR(err);
756 }
757
758 qp->ibqp.qp_num = qp->mqp.qpn;
759
760 break;
761 }
762 case IB_QPT_SMI:
763 case IB_QPT_GSI:
764 {
765 /* Userspace is not allowed to create special QPs: */
766 if (pd->uobject)
767 return ERR_PTR(-EINVAL);
768
Eli Cohenf507d282008-07-14 23:48:53 -0700769 sqp = kzalloc(sizeof *sqp, GFP_KERNEL);
Roland Dreier225c7b12007-05-08 18:00:38 -0700770 if (!sqp)
771 return ERR_PTR(-ENOMEM);
772
773 qp = &sqp->qp;
774
775 err = create_qp_common(dev, pd, init_attr, udata,
776 dev->dev->caps.sqp_start +
777 (init_attr->qp_type == IB_QPT_SMI ? 0 : 2) +
778 init_attr->port_num - 1,
779 qp);
780 if (err) {
781 kfree(sqp);
782 return ERR_PTR(err);
783 }
784
785 qp->port = init_attr->port_num;
786 qp->ibqp.qp_num = init_attr->qp_type == IB_QPT_SMI ? 0 : 1;
787
788 break;
789 }
790 default:
791 /* Don't support raw QPs */
792 return ERR_PTR(-EINVAL);
793 }
794
795 return &qp->ibqp;
796}
797
798int mlx4_ib_destroy_qp(struct ib_qp *qp)
799{
800 struct mlx4_ib_dev *dev = to_mdev(qp->device);
801 struct mlx4_ib_qp *mqp = to_mqp(qp);
802
803 if (is_qp0(dev, mqp))
804 mlx4_CLOSE_PORT(dev->dev, mqp->port);
805
806 destroy_qp_common(dev, mqp, !!qp->pd->uobject);
807
808 if (is_sqp(dev, mqp))
809 kfree(to_msqp(mqp));
810 else
811 kfree(mqp);
812
813 return 0;
814}
815
Roland Dreier225c7b12007-05-08 18:00:38 -0700816static int to_mlx4_st(enum ib_qp_type type)
817{
818 switch (type) {
819 case IB_QPT_RC: return MLX4_QP_ST_RC;
820 case IB_QPT_UC: return MLX4_QP_ST_UC;
821 case IB_QPT_UD: return MLX4_QP_ST_UD;
822 case IB_QPT_SMI:
823 case IB_QPT_GSI: return MLX4_QP_ST_MLX;
824 default: return -1;
825 }
826}
827
Michael S. Tsirkin65adfa92007-05-14 07:26:51 +0300828static __be32 to_mlx4_access_flags(struct mlx4_ib_qp *qp, const struct ib_qp_attr *attr,
Roland Dreier225c7b12007-05-08 18:00:38 -0700829 int attr_mask)
830{
831 u8 dest_rd_atomic;
832 u32 access_flags;
833 u32 hw_access_flags = 0;
834
835 if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC)
836 dest_rd_atomic = attr->max_dest_rd_atomic;
837 else
838 dest_rd_atomic = qp->resp_depth;
839
840 if (attr_mask & IB_QP_ACCESS_FLAGS)
841 access_flags = attr->qp_access_flags;
842 else
843 access_flags = qp->atomic_rd_en;
844
845 if (!dest_rd_atomic)
846 access_flags &= IB_ACCESS_REMOTE_WRITE;
847
848 if (access_flags & IB_ACCESS_REMOTE_READ)
849 hw_access_flags |= MLX4_QP_BIT_RRE;
850 if (access_flags & IB_ACCESS_REMOTE_ATOMIC)
851 hw_access_flags |= MLX4_QP_BIT_RAE;
852 if (access_flags & IB_ACCESS_REMOTE_WRITE)
853 hw_access_flags |= MLX4_QP_BIT_RWE;
854
855 return cpu_to_be32(hw_access_flags);
856}
857
Michael S. Tsirkin65adfa92007-05-14 07:26:51 +0300858static void store_sqp_attrs(struct mlx4_ib_sqp *sqp, const struct ib_qp_attr *attr,
Roland Dreier225c7b12007-05-08 18:00:38 -0700859 int attr_mask)
860{
861 if (attr_mask & IB_QP_PKEY_INDEX)
862 sqp->pkey_index = attr->pkey_index;
863 if (attr_mask & IB_QP_QKEY)
864 sqp->qkey = attr->qkey;
865 if (attr_mask & IB_QP_SQ_PSN)
866 sqp->send_psn = attr->sq_psn;
867}
868
869static void mlx4_set_sched(struct mlx4_qp_path *path, u8 port)
870{
871 path->sched_queue = (path->sched_queue & 0xbf) | ((port - 1) << 6);
872}
873
Michael S. Tsirkin65adfa92007-05-14 07:26:51 +0300874static int mlx4_set_path(struct mlx4_ib_dev *dev, const struct ib_ah_attr *ah,
Roland Dreier225c7b12007-05-08 18:00:38 -0700875 struct mlx4_qp_path *path, u8 port)
876{
Eli Cohenfa417f72010-10-24 21:08:52 -0700877 int err;
878 int is_eth = rdma_port_get_link_layer(&dev->ib_dev, port) ==
879 IB_LINK_LAYER_ETHERNET;
880 u8 mac[6];
881 int is_mcast;
882
Roland Dreier225c7b12007-05-08 18:00:38 -0700883 path->grh_mylmc = ah->src_path_bits & 0x7f;
884 path->rlid = cpu_to_be16(ah->dlid);
885 if (ah->static_rate) {
886 path->static_rate = ah->static_rate + MLX4_STAT_RATE_OFFSET;
887 while (path->static_rate > IB_RATE_2_5_GBPS + MLX4_STAT_RATE_OFFSET &&
888 !(1 << path->static_rate & dev->dev->caps.stat_rate_support))
889 --path->static_rate;
890 } else
891 path->static_rate = 0;
892 path->counter_index = 0xff;
893
894 if (ah->ah_flags & IB_AH_GRH) {
Roland Dreier5ae2a7a2007-06-18 08:15:02 -0700895 if (ah->grh.sgid_index >= dev->dev->caps.gid_table_len[port]) {
Roland Dreier225c7b12007-05-08 18:00:38 -0700896 printk(KERN_ERR "sgid_index (%u) too large. max is %d\n",
Roland Dreier5ae2a7a2007-06-18 08:15:02 -0700897 ah->grh.sgid_index, dev->dev->caps.gid_table_len[port] - 1);
Roland Dreier225c7b12007-05-08 18:00:38 -0700898 return -1;
899 }
900
901 path->grh_mylmc |= 1 << 7;
902 path->mgid_index = ah->grh.sgid_index;
903 path->hop_limit = ah->grh.hop_limit;
904 path->tclass_flowlabel =
905 cpu_to_be32((ah->grh.traffic_class << 20) |
906 (ah->grh.flow_label));
907 memcpy(path->rgid, ah->grh.dgid.raw, 16);
908 }
909
910 path->sched_queue = MLX4_IB_DEFAULT_SCHED_QUEUE |
911 ((port - 1) << 6) | ((ah->sl & 0xf) << 2);
912
Eli Cohenfa417f72010-10-24 21:08:52 -0700913 if (is_eth) {
914 if (!(ah->ah_flags & IB_AH_GRH))
915 return -1;
916
917 err = mlx4_ib_resolve_grh(dev, ah, mac, &is_mcast, port);
918 if (err)
919 return err;
920
921 memcpy(path->dmac, mac, 6);
922 path->ackto = MLX4_IB_LINK_TYPE_ETH;
923 /* use index 0 into MAC table for IBoE */
924 path->grh_mylmc &= 0x80;
925 }
926
Roland Dreier225c7b12007-05-08 18:00:38 -0700927 return 0;
928}
929
Eli Cohenfa417f72010-10-24 21:08:52 -0700930static void update_mcg_macs(struct mlx4_ib_dev *dev, struct mlx4_ib_qp *qp)
931{
932 struct mlx4_ib_gid_entry *ge, *tmp;
933
934 list_for_each_entry_safe(ge, tmp, &qp->gid_list, list) {
935 if (!ge->added && mlx4_ib_add_mc(dev, qp, &ge->gid)) {
936 ge->added = 1;
937 ge->port = qp->port;
938 }
939 }
940}
941
Michael S. Tsirkin65adfa92007-05-14 07:26:51 +0300942static int __mlx4_ib_modify_qp(struct ib_qp *ibqp,
943 const struct ib_qp_attr *attr, int attr_mask,
944 enum ib_qp_state cur_state, enum ib_qp_state new_state)
Roland Dreier225c7b12007-05-08 18:00:38 -0700945{
946 struct mlx4_ib_dev *dev = to_mdev(ibqp->device);
947 struct mlx4_ib_qp *qp = to_mqp(ibqp);
948 struct mlx4_qp_context *context;
949 enum mlx4_qp_optpar optpar = 0;
Roland Dreier225c7b12007-05-08 18:00:38 -0700950 int sqd_event;
951 int err = -EINVAL;
952
953 context = kzalloc(sizeof *context, GFP_KERNEL);
954 if (!context)
955 return -ENOMEM;
956
Roland Dreier225c7b12007-05-08 18:00:38 -0700957 context->flags = cpu_to_be32((to_mlx4_state(new_state) << 28) |
958 (to_mlx4_st(ibqp->qp_type) << 16));
Roland Dreier225c7b12007-05-08 18:00:38 -0700959
960 if (!(attr_mask & IB_QP_PATH_MIG_STATE))
961 context->flags |= cpu_to_be32(MLX4_QP_PM_MIGRATED << 11);
962 else {
963 optpar |= MLX4_QP_OPTPAR_PM_STATE;
964 switch (attr->path_mig_state) {
965 case IB_MIG_MIGRATED:
966 context->flags |= cpu_to_be32(MLX4_QP_PM_MIGRATED << 11);
967 break;
968 case IB_MIG_REARM:
969 context->flags |= cpu_to_be32(MLX4_QP_PM_REARM << 11);
970 break;
971 case IB_MIG_ARMED:
972 context->flags |= cpu_to_be32(MLX4_QP_PM_ARMED << 11);
973 break;
974 }
975 }
976
Eli Cohenb832be12008-04-16 21:09:27 -0700977 if (ibqp->qp_type == IB_QPT_GSI || ibqp->qp_type == IB_QPT_SMI)
Roland Dreier225c7b12007-05-08 18:00:38 -0700978 context->mtu_msgmax = (IB_MTU_4096 << 5) | 11;
Eli Cohenb832be12008-04-16 21:09:27 -0700979 else if (ibqp->qp_type == IB_QPT_UD) {
980 if (qp->flags & MLX4_IB_QP_LSO)
981 context->mtu_msgmax = (IB_MTU_4096 << 5) |
982 ilog2(dev->dev->caps.max_gso_sz);
983 else
Alex Naslednikov6e0d7332008-08-07 14:06:50 -0700984 context->mtu_msgmax = (IB_MTU_4096 << 5) | 12;
Eli Cohenb832be12008-04-16 21:09:27 -0700985 } else if (attr_mask & IB_QP_PATH_MTU) {
Roland Dreier225c7b12007-05-08 18:00:38 -0700986 if (attr->path_mtu < IB_MTU_256 || attr->path_mtu > IB_MTU_4096) {
987 printk(KERN_ERR "path MTU (%u) is invalid\n",
988 attr->path_mtu);
Florin Malitaf5b40432007-07-19 15:58:09 -0400989 goto out;
Roland Dreier225c7b12007-05-08 18:00:38 -0700990 }
Eli Cohend1f2cd82008-07-14 23:48:45 -0700991 context->mtu_msgmax = (attr->path_mtu << 5) |
992 ilog2(dev->dev->caps.max_msg_sz);
Roland Dreier225c7b12007-05-08 18:00:38 -0700993 }
994
Roland Dreier0e6e7412007-06-18 08:13:48 -0700995 if (qp->rq.wqe_cnt)
996 context->rq_size_stride = ilog2(qp->rq.wqe_cnt) << 3;
Roland Dreier225c7b12007-05-08 18:00:38 -0700997 context->rq_size_stride |= qp->rq.wqe_shift - 4;
998
Roland Dreier0e6e7412007-06-18 08:13:48 -0700999 if (qp->sq.wqe_cnt)
1000 context->sq_size_stride = ilog2(qp->sq.wqe_cnt) << 3;
Roland Dreier225c7b12007-05-08 18:00:38 -07001001 context->sq_size_stride |= qp->sq.wqe_shift - 4;
1002
Roland Dreier0e6e7412007-06-18 08:13:48 -07001003 if (cur_state == IB_QPS_RESET && new_state == IB_QPS_INIT)
1004 context->sq_size_stride |= !!qp->sq_no_prefetch << 7;
1005
Roland Dreier225c7b12007-05-08 18:00:38 -07001006 if (qp->ibqp.uobject)
1007 context->usr_page = cpu_to_be32(to_mucontext(ibqp->uobject->context)->uar.index);
1008 else
1009 context->usr_page = cpu_to_be32(dev->priv_uar.index);
1010
1011 if (attr_mask & IB_QP_DEST_QPN)
1012 context->remote_qpn = cpu_to_be32(attr->dest_qp_num);
1013
1014 if (attr_mask & IB_QP_PORT) {
1015 if (cur_state == IB_QPS_SQD && new_state == IB_QPS_SQD &&
1016 !(attr_mask & IB_QP_AV)) {
1017 mlx4_set_sched(&context->pri_path, attr->port_num);
1018 optpar |= MLX4_QP_OPTPAR_SCHED_QUEUE;
1019 }
1020 }
1021
1022 if (attr_mask & IB_QP_PKEY_INDEX) {
1023 context->pri_path.pkey_index = attr->pkey_index;
1024 optpar |= MLX4_QP_OPTPAR_PKEY_INDEX;
1025 }
1026
Roland Dreier225c7b12007-05-08 18:00:38 -07001027 if (attr_mask & IB_QP_AV) {
1028 if (mlx4_set_path(dev, &attr->ah_attr, &context->pri_path,
Florin Malitaf5b40432007-07-19 15:58:09 -04001029 attr_mask & IB_QP_PORT ? attr->port_num : qp->port))
Roland Dreier225c7b12007-05-08 18:00:38 -07001030 goto out;
Roland Dreier225c7b12007-05-08 18:00:38 -07001031
1032 optpar |= (MLX4_QP_OPTPAR_PRIMARY_ADDR_PATH |
1033 MLX4_QP_OPTPAR_SCHED_QUEUE);
1034 }
1035
1036 if (attr_mask & IB_QP_TIMEOUT) {
Eli Cohenfa417f72010-10-24 21:08:52 -07001037 context->pri_path.ackto |= attr->timeout << 3;
Roland Dreier225c7b12007-05-08 18:00:38 -07001038 optpar |= MLX4_QP_OPTPAR_ACK_TIMEOUT;
1039 }
1040
1041 if (attr_mask & IB_QP_ALT_PATH) {
Roland Dreier225c7b12007-05-08 18:00:38 -07001042 if (attr->alt_port_num == 0 ||
1043 attr->alt_port_num > dev->dev->caps.num_ports)
Florin Malitaf5b40432007-07-19 15:58:09 -04001044 goto out;
Roland Dreier225c7b12007-05-08 18:00:38 -07001045
Roland Dreier5ae2a7a2007-06-18 08:15:02 -07001046 if (attr->alt_pkey_index >=
1047 dev->dev->caps.pkey_table_len[attr->alt_port_num])
Florin Malitaf5b40432007-07-19 15:58:09 -04001048 goto out;
Roland Dreier5ae2a7a2007-06-18 08:15:02 -07001049
Roland Dreier225c7b12007-05-08 18:00:38 -07001050 if (mlx4_set_path(dev, &attr->alt_ah_attr, &context->alt_path,
1051 attr->alt_port_num))
Florin Malitaf5b40432007-07-19 15:58:09 -04001052 goto out;
Roland Dreier225c7b12007-05-08 18:00:38 -07001053
1054 context->alt_path.pkey_index = attr->alt_pkey_index;
1055 context->alt_path.ackto = attr->alt_timeout << 3;
1056 optpar |= MLX4_QP_OPTPAR_ALT_ADDR_PATH;
1057 }
1058
1059 context->pd = cpu_to_be32(to_mpd(ibqp->pd)->pdn);
1060 context->params1 = cpu_to_be32(MLX4_IB_ACK_REQ_FREQ << 28);
Jack Morgenstein57f01b52007-06-06 19:35:04 +03001061
Roland Dreier95d04f02008-07-23 08:12:26 -07001062 /* Set "fast registration enabled" for all kernel QPs */
1063 if (!qp->ibqp.uobject)
1064 context->params1 |= cpu_to_be32(1 << 11);
1065
Jack Morgenstein57f01b52007-06-06 19:35:04 +03001066 if (attr_mask & IB_QP_RNR_RETRY) {
1067 context->params1 |= cpu_to_be32(attr->rnr_retry << 13);
1068 optpar |= MLX4_QP_OPTPAR_RNR_RETRY;
1069 }
1070
Roland Dreier225c7b12007-05-08 18:00:38 -07001071 if (attr_mask & IB_QP_RETRY_CNT) {
1072 context->params1 |= cpu_to_be32(attr->retry_cnt << 16);
1073 optpar |= MLX4_QP_OPTPAR_RETRY_COUNT;
1074 }
1075
1076 if (attr_mask & IB_QP_MAX_QP_RD_ATOMIC) {
1077 if (attr->max_rd_atomic)
1078 context->params1 |=
1079 cpu_to_be32(fls(attr->max_rd_atomic - 1) << 21);
1080 optpar |= MLX4_QP_OPTPAR_SRA_MAX;
1081 }
1082
1083 if (attr_mask & IB_QP_SQ_PSN)
1084 context->next_send_psn = cpu_to_be32(attr->sq_psn);
1085
1086 context->cqn_send = cpu_to_be32(to_mcq(ibqp->send_cq)->mcq.cqn);
1087
1088 if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC) {
1089 if (attr->max_dest_rd_atomic)
1090 context->params2 |=
1091 cpu_to_be32(fls(attr->max_dest_rd_atomic - 1) << 21);
1092 optpar |= MLX4_QP_OPTPAR_RRA_MAX;
1093 }
1094
1095 if (attr_mask & (IB_QP_ACCESS_FLAGS | IB_QP_MAX_DEST_RD_ATOMIC)) {
1096 context->params2 |= to_mlx4_access_flags(qp, attr, attr_mask);
1097 optpar |= MLX4_QP_OPTPAR_RWE | MLX4_QP_OPTPAR_RRE | MLX4_QP_OPTPAR_RAE;
1098 }
1099
1100 if (ibqp->srq)
1101 context->params2 |= cpu_to_be32(MLX4_QP_BIT_RIC);
1102
1103 if (attr_mask & IB_QP_MIN_RNR_TIMER) {
1104 context->rnr_nextrecvpsn |= cpu_to_be32(attr->min_rnr_timer << 24);
1105 optpar |= MLX4_QP_OPTPAR_RNR_TIMEOUT;
1106 }
1107 if (attr_mask & IB_QP_RQ_PSN)
1108 context->rnr_nextrecvpsn |= cpu_to_be32(attr->rq_psn);
1109
1110 context->cqn_recv = cpu_to_be32(to_mcq(ibqp->recv_cq)->mcq.cqn);
1111
1112 if (attr_mask & IB_QP_QKEY) {
1113 context->qkey = cpu_to_be32(attr->qkey);
1114 optpar |= MLX4_QP_OPTPAR_Q_KEY;
1115 }
1116
1117 if (ibqp->srq)
1118 context->srqn = cpu_to_be32(1 << 24 | to_msrq(ibqp->srq)->msrq.srqn);
1119
Roland Dreier02d89b82007-05-23 15:16:08 -07001120 if (!ibqp->srq && cur_state == IB_QPS_RESET && new_state == IB_QPS_INIT)
Roland Dreier225c7b12007-05-08 18:00:38 -07001121 context->db_rec_addr = cpu_to_be64(qp->db.dma);
1122
1123 if (cur_state == IB_QPS_INIT &&
1124 new_state == IB_QPS_RTR &&
1125 (ibqp->qp_type == IB_QPT_GSI || ibqp->qp_type == IB_QPT_SMI ||
1126 ibqp->qp_type == IB_QPT_UD)) {
1127 context->pri_path.sched_queue = (qp->port - 1) << 6;
1128 if (is_qp0(dev, qp))
1129 context->pri_path.sched_queue |= MLX4_IB_DEFAULT_QP0_SCHED_QUEUE;
1130 else
1131 context->pri_path.sched_queue |= MLX4_IB_DEFAULT_SCHED_QUEUE;
1132 }
1133
1134 if (cur_state == IB_QPS_RTS && new_state == IB_QPS_SQD &&
1135 attr_mask & IB_QP_EN_SQD_ASYNC_NOTIFY && attr->en_sqd_async_notify)
1136 sqd_event = 1;
1137 else
1138 sqd_event = 0;
1139
Vladimir Sokolovskyd57f5f72008-10-08 20:09:01 -07001140 if (!ibqp->uobject && cur_state == IB_QPS_RESET && new_state == IB_QPS_INIT)
1141 context->rlkey |= (1 << 4);
1142
Eli Cohenc0be5fb2007-05-24 16:05:01 +03001143 /*
1144 * Before passing a kernel QP to the HW, make sure that the
Roland Dreier0e6e7412007-06-18 08:13:48 -07001145 * ownership bits of the send queue are set and the SQ
1146 * headroom is stamped so that the hardware doesn't start
1147 * processing stale work requests.
Eli Cohenc0be5fb2007-05-24 16:05:01 +03001148 */
1149 if (!ibqp->uobject && cur_state == IB_QPS_RESET && new_state == IB_QPS_INIT) {
1150 struct mlx4_wqe_ctrl_seg *ctrl;
1151 int i;
1152
Roland Dreier0e6e7412007-06-18 08:13:48 -07001153 for (i = 0; i < qp->sq.wqe_cnt; ++i) {
Eli Cohenc0be5fb2007-05-24 16:05:01 +03001154 ctrl = get_send_wqe(qp, i);
1155 ctrl->owner_opcode = cpu_to_be32(1 << 31);
Eli Cohen9670e552008-07-14 23:48:44 -07001156 if (qp->sq_max_wqes_per_wr == 1)
1157 ctrl->fence_size = 1 << (qp->sq.wqe_shift - 4);
Roland Dreier0e6e7412007-06-18 08:13:48 -07001158
Jack Morgensteinea54b102008-01-28 10:40:59 +02001159 stamp_send_wqe(qp, i, 1 << qp->sq.wqe_shift);
Eli Cohenc0be5fb2007-05-24 16:05:01 +03001160 }
1161 }
1162
Roland Dreier225c7b12007-05-08 18:00:38 -07001163 err = mlx4_qp_modify(dev->dev, &qp->mtt, to_mlx4_state(cur_state),
1164 to_mlx4_state(new_state), context, optpar,
1165 sqd_event, &qp->mqp);
1166 if (err)
1167 goto out;
1168
1169 qp->state = new_state;
1170
1171 if (attr_mask & IB_QP_ACCESS_FLAGS)
1172 qp->atomic_rd_en = attr->qp_access_flags;
1173 if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC)
1174 qp->resp_depth = attr->max_dest_rd_atomic;
Eli Cohenfa417f72010-10-24 21:08:52 -07001175 if (attr_mask & IB_QP_PORT) {
Roland Dreier225c7b12007-05-08 18:00:38 -07001176 qp->port = attr->port_num;
Eli Cohenfa417f72010-10-24 21:08:52 -07001177 update_mcg_macs(dev, qp);
1178 }
Roland Dreier225c7b12007-05-08 18:00:38 -07001179 if (attr_mask & IB_QP_ALT_PATH)
1180 qp->alt_port = attr->alt_port_num;
1181
1182 if (is_sqp(dev, qp))
1183 store_sqp_attrs(to_msqp(qp), attr, attr_mask);
1184
1185 /*
1186 * If we moved QP0 to RTR, bring the IB link up; if we moved
1187 * QP0 to RESET or ERROR, bring the link back down.
1188 */
1189 if (is_qp0(dev, qp)) {
1190 if (cur_state != IB_QPS_RTR && new_state == IB_QPS_RTR)
Roland Dreier5ae2a7a2007-06-18 08:15:02 -07001191 if (mlx4_INIT_PORT(dev->dev, qp->port))
1192 printk(KERN_WARNING "INIT_PORT failed for port %d\n",
1193 qp->port);
Roland Dreier225c7b12007-05-08 18:00:38 -07001194
1195 if (cur_state != IB_QPS_RESET && cur_state != IB_QPS_ERR &&
1196 (new_state == IB_QPS_RESET || new_state == IB_QPS_ERR))
1197 mlx4_CLOSE_PORT(dev->dev, qp->port);
1198 }
1199
1200 /*
1201 * If we moved a kernel QP to RESET, clean up all old CQ
1202 * entries and reinitialize the QP.
1203 */
1204 if (new_state == IB_QPS_RESET && !ibqp->uobject) {
1205 mlx4_ib_cq_clean(to_mcq(ibqp->recv_cq), qp->mqp.qpn,
1206 ibqp->srq ? to_msrq(ibqp->srq): NULL);
1207 if (ibqp->send_cq != ibqp->recv_cq)
1208 mlx4_ib_cq_clean(to_mcq(ibqp->send_cq), qp->mqp.qpn, NULL);
1209
1210 qp->rq.head = 0;
1211 qp->rq.tail = 0;
1212 qp->sq.head = 0;
1213 qp->sq.tail = 0;
Jack Morgensteinea54b102008-01-28 10:40:59 +02001214 qp->sq_next_wqe = 0;
Roland Dreier02d89b82007-05-23 15:16:08 -07001215 if (!ibqp->srq)
1216 *qp->db.db = 0;
Roland Dreier225c7b12007-05-08 18:00:38 -07001217 }
1218
1219out:
Roland Dreier225c7b12007-05-08 18:00:38 -07001220 kfree(context);
1221 return err;
1222}
1223
Michael S. Tsirkin65adfa92007-05-14 07:26:51 +03001224int mlx4_ib_modify_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
1225 int attr_mask, struct ib_udata *udata)
1226{
1227 struct mlx4_ib_dev *dev = to_mdev(ibqp->device);
1228 struct mlx4_ib_qp *qp = to_mqp(ibqp);
1229 enum ib_qp_state cur_state, new_state;
1230 int err = -EINVAL;
1231
1232 mutex_lock(&qp->mutex);
1233
1234 cur_state = attr_mask & IB_QP_CUR_STATE ? attr->cur_qp_state : qp->state;
1235 new_state = attr_mask & IB_QP_STATE ? attr->qp_state : cur_state;
1236
1237 if (!ib_modify_qp_is_ok(cur_state, new_state, ibqp->qp_type, attr_mask))
1238 goto out;
1239
Michael S. Tsirkin65adfa92007-05-14 07:26:51 +03001240 if ((attr_mask & IB_QP_PORT) &&
1241 (attr->port_num == 0 || attr->port_num > dev->dev->caps.num_ports)) {
1242 goto out;
1243 }
1244
Roland Dreier5ae2a7a2007-06-18 08:15:02 -07001245 if (attr_mask & IB_QP_PKEY_INDEX) {
1246 int p = attr_mask & IB_QP_PORT ? attr->port_num : qp->port;
1247 if (attr->pkey_index >= dev->dev->caps.pkey_table_len[p])
1248 goto out;
1249 }
1250
Michael S. Tsirkin65adfa92007-05-14 07:26:51 +03001251 if (attr_mask & IB_QP_MAX_QP_RD_ATOMIC &&
1252 attr->max_rd_atomic > dev->dev->caps.max_qp_init_rdma) {
1253 goto out;
1254 }
1255
1256 if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC &&
1257 attr->max_dest_rd_atomic > dev->dev->caps.max_qp_dest_rdma) {
1258 goto out;
1259 }
1260
1261 if (cur_state == new_state && cur_state == IB_QPS_RESET) {
1262 err = 0;
1263 goto out;
1264 }
1265
Michael S. Tsirkin65adfa92007-05-14 07:26:51 +03001266 err = __mlx4_ib_modify_qp(ibqp, attr, attr_mask, cur_state, new_state);
1267
1268out:
1269 mutex_unlock(&qp->mutex);
1270 return err;
1271}
1272
Roland Dreier225c7b12007-05-08 18:00:38 -07001273static int build_mlx_header(struct mlx4_ib_sqp *sqp, struct ib_send_wr *wr,
Roland Dreierf4380002008-04-16 21:09:28 -07001274 void *wqe, unsigned *mlx_seg_len)
Roland Dreier225c7b12007-05-08 18:00:38 -07001275{
Eli Cohena4788682010-01-27 13:57:03 +00001276 struct ib_device *ib_dev = sqp->qp.ibqp.device;
Roland Dreier225c7b12007-05-08 18:00:38 -07001277 struct mlx4_wqe_mlx_seg *mlx = wqe;
1278 struct mlx4_wqe_inline_seg *inl = wqe + sizeof *mlx;
1279 struct mlx4_ib_ah *ah = to_mah(wr->wr.ud.ah);
1280 u16 pkey;
1281 int send_size;
1282 int header_size;
Roland Dreiere61ef242007-06-18 09:23:47 -07001283 int spc;
Roland Dreier225c7b12007-05-08 18:00:38 -07001284 int i;
Eli Cohenfa417f72010-10-24 21:08:52 -07001285 int is_eth;
1286 int is_grh;
Roland Dreier225c7b12007-05-08 18:00:38 -07001287
1288 send_size = 0;
1289 for (i = 0; i < wr->num_sge; ++i)
1290 send_size += wr->sg_list[i].length;
1291
Eli Cohenfa417f72010-10-24 21:08:52 -07001292 is_eth = rdma_port_get_link_layer(sqp->qp.ibqp.device, sqp->qp.port) == IB_LINK_LAYER_ETHERNET;
1293 is_grh = mlx4_ib_ah_grh_present(ah);
1294 ib_ud_header_init(send_size, !is_eth, is_eth, is_grh, 0, &sqp->ud_header);
Roland Dreier225c7b12007-05-08 18:00:38 -07001295
Eli Cohenfa417f72010-10-24 21:08:52 -07001296 if (!is_eth) {
1297 sqp->ud_header.lrh.service_level =
1298 be32_to_cpu(ah->av.ib.sl_tclass_flowlabel) >> 28;
1299 sqp->ud_header.lrh.destination_lid = ah->av.ib.dlid;
1300 sqp->ud_header.lrh.source_lid = cpu_to_be16(ah->av.ib.g_slid & 0x7f);
1301 }
1302
1303 if (is_grh) {
Roland Dreier225c7b12007-05-08 18:00:38 -07001304 sqp->ud_header.grh.traffic_class =
Eli Cohenfa417f72010-10-24 21:08:52 -07001305 (be32_to_cpu(ah->av.ib.sl_tclass_flowlabel) >> 20) & 0xff;
Roland Dreier225c7b12007-05-08 18:00:38 -07001306 sqp->ud_header.grh.flow_label =
Eli Cohenfa417f72010-10-24 21:08:52 -07001307 ah->av.ib.sl_tclass_flowlabel & cpu_to_be32(0xfffff);
1308 sqp->ud_header.grh.hop_limit = ah->av.ib.hop_limit;
1309 ib_get_cached_gid(ib_dev, be32_to_cpu(ah->av.ib.port_pd) >> 24,
1310 ah->av.ib.gid_index, &sqp->ud_header.grh.source_gid);
Roland Dreier225c7b12007-05-08 18:00:38 -07001311 memcpy(sqp->ud_header.grh.destination_gid.raw,
Eli Cohenfa417f72010-10-24 21:08:52 -07001312 ah->av.ib.dgid, 16);
Roland Dreier225c7b12007-05-08 18:00:38 -07001313 }
1314
1315 mlx->flags &= cpu_to_be32(MLX4_WQE_CTRL_CQ_UPDATE);
Eli Cohenfa417f72010-10-24 21:08:52 -07001316
1317 if (!is_eth) {
1318 mlx->flags |= cpu_to_be32((!sqp->qp.ibqp.qp_num ? MLX4_WQE_MLX_VL15 : 0) |
1319 (sqp->ud_header.lrh.destination_lid ==
1320 IB_LID_PERMISSIVE ? MLX4_WQE_MLX_SLR : 0) |
1321 (sqp->ud_header.lrh.service_level << 8));
1322 mlx->rlid = sqp->ud_header.lrh.destination_lid;
1323 }
Roland Dreier225c7b12007-05-08 18:00:38 -07001324
1325 switch (wr->opcode) {
1326 case IB_WR_SEND:
1327 sqp->ud_header.bth.opcode = IB_OPCODE_UD_SEND_ONLY;
1328 sqp->ud_header.immediate_present = 0;
1329 break;
1330 case IB_WR_SEND_WITH_IMM:
1331 sqp->ud_header.bth.opcode = IB_OPCODE_UD_SEND_ONLY_WITH_IMMEDIATE;
1332 sqp->ud_header.immediate_present = 1;
Roland Dreier0f39cf32008-04-16 21:09:32 -07001333 sqp->ud_header.immediate_data = wr->ex.imm_data;
Roland Dreier225c7b12007-05-08 18:00:38 -07001334 break;
1335 default:
1336 return -EINVAL;
1337 }
1338
Eli Cohenfa417f72010-10-24 21:08:52 -07001339 if (is_eth) {
1340 u8 *smac;
1341
1342 memcpy(sqp->ud_header.eth.dmac_h, ah->av.eth.mac, 6);
1343 /* FIXME: cache smac value? */
1344 smac = to_mdev(sqp->qp.ibqp.device)->iboe.netdevs[sqp->qp.port - 1]->dev_addr;
1345 memcpy(sqp->ud_header.eth.smac_h, smac, 6);
1346 if (!memcmp(sqp->ud_header.eth.smac_h, sqp->ud_header.eth.dmac_h, 6))
1347 mlx->flags |= cpu_to_be32(MLX4_WQE_CTRL_FORCE_LOOPBACK);
1348 sqp->ud_header.eth.type = cpu_to_be16(MLX4_IB_IBOE_ETHERTYPE);
1349 } else {
1350 sqp->ud_header.lrh.virtual_lane = !sqp->qp.ibqp.qp_num ? 15 : 0;
1351 if (sqp->ud_header.lrh.destination_lid == IB_LID_PERMISSIVE)
1352 sqp->ud_header.lrh.source_lid = IB_LID_PERMISSIVE;
1353 }
Roland Dreier225c7b12007-05-08 18:00:38 -07001354 sqp->ud_header.bth.solicited_event = !!(wr->send_flags & IB_SEND_SOLICITED);
1355 if (!sqp->qp.ibqp.qp_num)
1356 ib_get_cached_pkey(ib_dev, sqp->qp.port, sqp->pkey_index, &pkey);
1357 else
1358 ib_get_cached_pkey(ib_dev, sqp->qp.port, wr->wr.ud.pkey_index, &pkey);
1359 sqp->ud_header.bth.pkey = cpu_to_be16(pkey);
1360 sqp->ud_header.bth.destination_qpn = cpu_to_be32(wr->wr.ud.remote_qpn);
1361 sqp->ud_header.bth.psn = cpu_to_be32((sqp->send_psn++) & ((1 << 24) - 1));
1362 sqp->ud_header.deth.qkey = cpu_to_be32(wr->wr.ud.remote_qkey & 0x80000000 ?
1363 sqp->qkey : wr->wr.ud.remote_qkey);
1364 sqp->ud_header.deth.source_qpn = cpu_to_be32(sqp->qp.ibqp.qp_num);
1365
1366 header_size = ib_ud_header_pack(&sqp->ud_header, sqp->header_buf);
1367
1368 if (0) {
1369 printk(KERN_ERR "built UD header of size %d:\n", header_size);
1370 for (i = 0; i < header_size / 4; ++i) {
1371 if (i % 8 == 0)
1372 printk(" [%02x] ", i * 4);
1373 printk(" %08x",
1374 be32_to_cpu(((__be32 *) sqp->header_buf)[i]));
1375 if ((i + 1) % 8 == 0)
1376 printk("\n");
1377 }
1378 printk("\n");
1379 }
1380
Roland Dreiere61ef242007-06-18 09:23:47 -07001381 /*
1382 * Inline data segments may not cross a 64 byte boundary. If
1383 * our UD header is bigger than the space available up to the
1384 * next 64 byte boundary in the WQE, use two inline data
1385 * segments to hold the UD header.
1386 */
1387 spc = MLX4_INLINE_ALIGN -
1388 ((unsigned long) (inl + 1) & (MLX4_INLINE_ALIGN - 1));
1389 if (header_size <= spc) {
1390 inl->byte_count = cpu_to_be32(1 << 31 | header_size);
1391 memcpy(inl + 1, sqp->header_buf, header_size);
1392 i = 1;
1393 } else {
1394 inl->byte_count = cpu_to_be32(1 << 31 | spc);
1395 memcpy(inl + 1, sqp->header_buf, spc);
Roland Dreier225c7b12007-05-08 18:00:38 -07001396
Roland Dreiere61ef242007-06-18 09:23:47 -07001397 inl = (void *) (inl + 1) + spc;
1398 memcpy(inl + 1, sqp->header_buf + spc, header_size - spc);
1399 /*
1400 * Need a barrier here to make sure all the data is
1401 * visible before the byte_count field is set.
1402 * Otherwise the HCA prefetcher could grab the 64-byte
1403 * chunk with this inline segment and get a valid (!=
1404 * 0xffffffff) byte count but stale data, and end up
1405 * generating a packet with bad headers.
1406 *
1407 * The first inline segment's byte_count field doesn't
1408 * need a barrier, because it comes after a
1409 * control/MLX segment and therefore is at an offset
1410 * of 16 mod 64.
1411 */
1412 wmb();
1413 inl->byte_count = cpu_to_be32(1 << 31 | (header_size - spc));
1414 i = 2;
1415 }
1416
Roland Dreierf4380002008-04-16 21:09:28 -07001417 *mlx_seg_len =
1418 ALIGN(i * sizeof (struct mlx4_wqe_inline_seg) + header_size, 16);
1419 return 0;
Roland Dreier225c7b12007-05-08 18:00:38 -07001420}
1421
1422static int mlx4_wq_overflow(struct mlx4_ib_wq *wq, int nreq, struct ib_cq *ib_cq)
1423{
1424 unsigned cur;
1425 struct mlx4_ib_cq *cq;
1426
1427 cur = wq->head - wq->tail;
Roland Dreier0e6e7412007-06-18 08:13:48 -07001428 if (likely(cur + nreq < wq->max_post))
Roland Dreier225c7b12007-05-08 18:00:38 -07001429 return 0;
1430
1431 cq = to_mcq(ib_cq);
1432 spin_lock(&cq->lock);
1433 cur = wq->head - wq->tail;
1434 spin_unlock(&cq->lock);
1435
Roland Dreier0e6e7412007-06-18 08:13:48 -07001436 return cur + nreq >= wq->max_post;
Roland Dreier225c7b12007-05-08 18:00:38 -07001437}
1438
Roland Dreier95d04f02008-07-23 08:12:26 -07001439static __be32 convert_access(int acc)
1440{
1441 return (acc & IB_ACCESS_REMOTE_ATOMIC ? cpu_to_be32(MLX4_WQE_FMR_PERM_ATOMIC) : 0) |
1442 (acc & IB_ACCESS_REMOTE_WRITE ? cpu_to_be32(MLX4_WQE_FMR_PERM_REMOTE_WRITE) : 0) |
1443 (acc & IB_ACCESS_REMOTE_READ ? cpu_to_be32(MLX4_WQE_FMR_PERM_REMOTE_READ) : 0) |
1444 (acc & IB_ACCESS_LOCAL_WRITE ? cpu_to_be32(MLX4_WQE_FMR_PERM_LOCAL_WRITE) : 0) |
1445 cpu_to_be32(MLX4_WQE_FMR_PERM_LOCAL_READ);
1446}
1447
1448static void set_fmr_seg(struct mlx4_wqe_fmr_seg *fseg, struct ib_send_wr *wr)
1449{
1450 struct mlx4_ib_fast_reg_page_list *mfrpl = to_mfrpl(wr->wr.fast_reg.page_list);
Vladimir Sokolovsky29bdc882008-09-15 14:25:23 -07001451 int i;
1452
1453 for (i = 0; i < wr->wr.fast_reg.page_list_len; ++i)
Jack Morgenstein2b6b7d42009-05-07 21:35:13 -07001454 mfrpl->mapped_page_list[i] =
Vladimir Sokolovsky29bdc882008-09-15 14:25:23 -07001455 cpu_to_be64(wr->wr.fast_reg.page_list->page_list[i] |
1456 MLX4_MTT_FLAG_PRESENT);
Roland Dreier95d04f02008-07-23 08:12:26 -07001457
1458 fseg->flags = convert_access(wr->wr.fast_reg.access_flags);
1459 fseg->mem_key = cpu_to_be32(wr->wr.fast_reg.rkey);
1460 fseg->buf_list = cpu_to_be64(mfrpl->map);
1461 fseg->start_addr = cpu_to_be64(wr->wr.fast_reg.iova_start);
1462 fseg->reg_len = cpu_to_be64(wr->wr.fast_reg.length);
1463 fseg->offset = 0; /* XXX -- is this just for ZBVA? */
1464 fseg->page_size = cpu_to_be32(wr->wr.fast_reg.page_shift);
1465 fseg->reserved[0] = 0;
1466 fseg->reserved[1] = 0;
1467}
1468
1469static void set_local_inv_seg(struct mlx4_wqe_local_inval_seg *iseg, u32 rkey)
1470{
1471 iseg->flags = 0;
1472 iseg->mem_key = cpu_to_be32(rkey);
1473 iseg->guest_id = 0;
1474 iseg->pa = 0;
1475}
1476
Roland Dreier0fbfa6a92007-07-18 11:47:55 -07001477static __always_inline void set_raddr_seg(struct mlx4_wqe_raddr_seg *rseg,
1478 u64 remote_addr, u32 rkey)
1479{
1480 rseg->raddr = cpu_to_be64(remote_addr);
1481 rseg->rkey = cpu_to_be32(rkey);
1482 rseg->reserved = 0;
1483}
1484
1485static void set_atomic_seg(struct mlx4_wqe_atomic_seg *aseg, struct ib_send_wr *wr)
1486{
1487 if (wr->opcode == IB_WR_ATOMIC_CMP_AND_SWP) {
1488 aseg->swap_add = cpu_to_be64(wr->wr.atomic.swap);
1489 aseg->compare = cpu_to_be64(wr->wr.atomic.compare_add);
Vladimir Sokolovsky6fa8f712010-04-14 17:23:39 +03001490 } else if (wr->opcode == IB_WR_MASKED_ATOMIC_FETCH_AND_ADD) {
1491 aseg->swap_add = cpu_to_be64(wr->wr.atomic.compare_add);
1492 aseg->compare = cpu_to_be64(wr->wr.atomic.compare_add_mask);
Roland Dreier0fbfa6a92007-07-18 11:47:55 -07001493 } else {
1494 aseg->swap_add = cpu_to_be64(wr->wr.atomic.compare_add);
1495 aseg->compare = 0;
1496 }
1497
1498}
1499
Vladimir Sokolovsky6fa8f712010-04-14 17:23:39 +03001500static void set_masked_atomic_seg(struct mlx4_wqe_masked_atomic_seg *aseg,
1501 struct ib_send_wr *wr)
1502{
1503 aseg->swap_add = cpu_to_be64(wr->wr.atomic.swap);
1504 aseg->swap_add_mask = cpu_to_be64(wr->wr.atomic.swap_mask);
1505 aseg->compare = cpu_to_be64(wr->wr.atomic.compare_add);
1506 aseg->compare_mask = cpu_to_be64(wr->wr.atomic.compare_add_mask);
1507}
1508
Roland Dreier0fbfa6a92007-07-18 11:47:55 -07001509static void set_datagram_seg(struct mlx4_wqe_datagram_seg *dseg,
1510 struct ib_send_wr *wr)
1511{
1512 memcpy(dseg->av, &to_mah(wr->wr.ud.ah)->av, sizeof (struct mlx4_av));
1513 dseg->dqpn = cpu_to_be32(wr->wr.ud.remote_qpn);
1514 dseg->qkey = cpu_to_be32(wr->wr.ud.remote_qkey);
Eli Cohenfa417f72010-10-24 21:08:52 -07001515 dseg->vlan = to_mah(wr->wr.ud.ah)->av.eth.vlan;
1516 memcpy(dseg->mac, to_mah(wr->wr.ud.ah)->av.eth.mac, 6);
Roland Dreier0fbfa6a92007-07-18 11:47:55 -07001517}
1518
Jack Morgenstein6e694ea2007-09-19 09:52:25 -07001519static void set_mlx_icrc_seg(void *dseg)
Roland Dreierd420d9e2007-07-18 11:46:27 -07001520{
Jack Morgenstein6e694ea2007-09-19 09:52:25 -07001521 u32 *t = dseg;
1522 struct mlx4_wqe_inline_seg *iseg = dseg;
1523
1524 t[1] = 0;
1525
1526 /*
1527 * Need a barrier here before writing the byte_count field to
1528 * make sure that all the data is visible before the
1529 * byte_count field is set. Otherwise, if the segment begins
1530 * a new cacheline, the HCA prefetcher could grab the 64-byte
1531 * chunk and get a valid (!= * 0xffffffff) byte count but
1532 * stale data, and end up sending the wrong data.
1533 */
1534 wmb();
1535
1536 iseg->byte_count = cpu_to_be32((1 << 31) | 4);
1537}
1538
1539static void set_data_seg(struct mlx4_wqe_data_seg *dseg, struct ib_sge *sg)
1540{
Roland Dreierd420d9e2007-07-18 11:46:27 -07001541 dseg->lkey = cpu_to_be32(sg->lkey);
1542 dseg->addr = cpu_to_be64(sg->addr);
Jack Morgenstein6e694ea2007-09-19 09:52:25 -07001543
1544 /*
1545 * Need a barrier here before writing the byte_count field to
1546 * make sure that all the data is visible before the
1547 * byte_count field is set. Otherwise, if the segment begins
1548 * a new cacheline, the HCA prefetcher could grab the 64-byte
1549 * chunk and get a valid (!= * 0xffffffff) byte count but
1550 * stale data, and end up sending the wrong data.
1551 */
1552 wmb();
1553
1554 dseg->byte_count = cpu_to_be32(sg->length);
Roland Dreierd420d9e2007-07-18 11:46:27 -07001555}
1556
Roland Dreier2242fa42007-10-09 19:59:05 -07001557static void __set_data_seg(struct mlx4_wqe_data_seg *dseg, struct ib_sge *sg)
1558{
1559 dseg->byte_count = cpu_to_be32(sg->length);
1560 dseg->lkey = cpu_to_be32(sg->lkey);
1561 dseg->addr = cpu_to_be64(sg->addr);
1562}
1563
Roland Dreier47b37472008-07-22 14:19:39 -07001564static int build_lso_seg(struct mlx4_wqe_lso_seg *wqe, struct ib_send_wr *wr,
Roland Dreier0fd7e1d2009-01-16 12:47:47 -08001565 struct mlx4_ib_qp *qp, unsigned *lso_seg_len,
Eli Cohen417608c2009-11-12 11:19:44 -08001566 __be32 *lso_hdr_sz, __be32 *blh)
Eli Cohenb832be12008-04-16 21:09:27 -07001567{
1568 unsigned halign = ALIGN(sizeof *wqe + wr->wr.ud.hlen, 16);
1569
Eli Cohen417608c2009-11-12 11:19:44 -08001570 if (unlikely(halign > MLX4_IB_CACHE_LINE_SIZE))
1571 *blh = cpu_to_be32(1 << 6);
Eli Cohenb832be12008-04-16 21:09:27 -07001572
1573 if (unlikely(!(qp->flags & MLX4_IB_QP_LSO) &&
1574 wr->num_sge > qp->sq.max_gs - (halign >> 4)))
1575 return -EINVAL;
1576
1577 memcpy(wqe->header, wr->wr.ud.header, wr->wr.ud.hlen);
1578
Roland Dreier0fd7e1d2009-01-16 12:47:47 -08001579 *lso_hdr_sz = cpu_to_be32((wr->wr.ud.mss - wr->wr.ud.hlen) << 16 |
1580 wr->wr.ud.hlen);
Eli Cohenb832be12008-04-16 21:09:27 -07001581 *lso_seg_len = halign;
1582 return 0;
1583}
1584
Roland Dreier95d04f02008-07-23 08:12:26 -07001585static __be32 send_ieth(struct ib_send_wr *wr)
1586{
1587 switch (wr->opcode) {
1588 case IB_WR_SEND_WITH_IMM:
1589 case IB_WR_RDMA_WRITE_WITH_IMM:
1590 return wr->ex.imm_data;
1591
1592 case IB_WR_SEND_WITH_INV:
1593 return cpu_to_be32(wr->ex.invalidate_rkey);
1594
1595 default:
1596 return 0;
1597 }
1598}
1599
Roland Dreier225c7b12007-05-08 18:00:38 -07001600int mlx4_ib_post_send(struct ib_qp *ibqp, struct ib_send_wr *wr,
1601 struct ib_send_wr **bad_wr)
1602{
1603 struct mlx4_ib_qp *qp = to_mqp(ibqp);
1604 void *wqe;
1605 struct mlx4_wqe_ctrl_seg *ctrl;
Jack Morgenstein6e694ea2007-09-19 09:52:25 -07001606 struct mlx4_wqe_data_seg *dseg;
Roland Dreier225c7b12007-05-08 18:00:38 -07001607 unsigned long flags;
1608 int nreq;
1609 int err = 0;
Jack Morgensteinea54b102008-01-28 10:40:59 +02001610 unsigned ind;
1611 int uninitialized_var(stamp);
1612 int uninitialized_var(size);
Andrew Mortona3d8e152008-05-16 14:28:30 -07001613 unsigned uninitialized_var(seglen);
Roland Dreier0fd7e1d2009-01-16 12:47:47 -08001614 __be32 dummy;
1615 __be32 *lso_wqe;
1616 __be32 uninitialized_var(lso_hdr_sz);
Eli Cohen417608c2009-11-12 11:19:44 -08001617 __be32 blh;
Roland Dreier225c7b12007-05-08 18:00:38 -07001618 int i;
1619
Roland Dreier96db0e02007-10-30 10:53:54 -07001620 spin_lock_irqsave(&qp->sq.lock, flags);
Roland Dreier225c7b12007-05-08 18:00:38 -07001621
Jack Morgensteinea54b102008-01-28 10:40:59 +02001622 ind = qp->sq_next_wqe;
Roland Dreier225c7b12007-05-08 18:00:38 -07001623
1624 for (nreq = 0; wr; ++nreq, wr = wr->next) {
Roland Dreier0fd7e1d2009-01-16 12:47:47 -08001625 lso_wqe = &dummy;
Eli Cohen417608c2009-11-12 11:19:44 -08001626 blh = 0;
Roland Dreier0fd7e1d2009-01-16 12:47:47 -08001627
Roland Dreier225c7b12007-05-08 18:00:38 -07001628 if (mlx4_wq_overflow(&qp->sq, nreq, qp->ibqp.send_cq)) {
1629 err = -ENOMEM;
1630 *bad_wr = wr;
1631 goto out;
1632 }
1633
1634 if (unlikely(wr->num_sge > qp->sq.max_gs)) {
1635 err = -EINVAL;
1636 *bad_wr = wr;
1637 goto out;
1638 }
1639
Roland Dreier0e6e7412007-06-18 08:13:48 -07001640 ctrl = wqe = get_send_wqe(qp, ind & (qp->sq.wqe_cnt - 1));
Jack Morgensteinea54b102008-01-28 10:40:59 +02001641 qp->sq.wrid[(qp->sq.head + nreq) & (qp->sq.wqe_cnt - 1)] = wr->wr_id;
Roland Dreier225c7b12007-05-08 18:00:38 -07001642
1643 ctrl->srcrb_flags =
1644 (wr->send_flags & IB_SEND_SIGNALED ?
1645 cpu_to_be32(MLX4_WQE_CTRL_CQ_UPDATE) : 0) |
1646 (wr->send_flags & IB_SEND_SOLICITED ?
1647 cpu_to_be32(MLX4_WQE_CTRL_SOLICITED) : 0) |
Eli Cohen8ff095e2008-04-16 21:01:10 -07001648 ((wr->send_flags & IB_SEND_IP_CSUM) ?
1649 cpu_to_be32(MLX4_WQE_CTRL_IP_CSUM |
1650 MLX4_WQE_CTRL_TCP_UDP_CSUM) : 0) |
Roland Dreier225c7b12007-05-08 18:00:38 -07001651 qp->sq_signal_bits;
1652
Roland Dreier95d04f02008-07-23 08:12:26 -07001653 ctrl->imm = send_ieth(wr);
Roland Dreier225c7b12007-05-08 18:00:38 -07001654
1655 wqe += sizeof *ctrl;
1656 size = sizeof *ctrl / 16;
1657
1658 switch (ibqp->qp_type) {
1659 case IB_QPT_RC:
1660 case IB_QPT_UC:
1661 switch (wr->opcode) {
1662 case IB_WR_ATOMIC_CMP_AND_SWP:
1663 case IB_WR_ATOMIC_FETCH_AND_ADD:
Vladimir Sokolovsky6fa8f712010-04-14 17:23:39 +03001664 case IB_WR_MASKED_ATOMIC_FETCH_AND_ADD:
Roland Dreier0fbfa6a92007-07-18 11:47:55 -07001665 set_raddr_seg(wqe, wr->wr.atomic.remote_addr,
1666 wr->wr.atomic.rkey);
Roland Dreier225c7b12007-05-08 18:00:38 -07001667 wqe += sizeof (struct mlx4_wqe_raddr_seg);
1668
Roland Dreier0fbfa6a92007-07-18 11:47:55 -07001669 set_atomic_seg(wqe, wr);
Roland Dreier225c7b12007-05-08 18:00:38 -07001670 wqe += sizeof (struct mlx4_wqe_atomic_seg);
Roland Dreier0fbfa6a92007-07-18 11:47:55 -07001671
Roland Dreier225c7b12007-05-08 18:00:38 -07001672 size += (sizeof (struct mlx4_wqe_raddr_seg) +
1673 sizeof (struct mlx4_wqe_atomic_seg)) / 16;
1674
1675 break;
1676
Vladimir Sokolovsky6fa8f712010-04-14 17:23:39 +03001677 case IB_WR_MASKED_ATOMIC_CMP_AND_SWP:
1678 set_raddr_seg(wqe, wr->wr.atomic.remote_addr,
1679 wr->wr.atomic.rkey);
1680 wqe += sizeof (struct mlx4_wqe_raddr_seg);
1681
1682 set_masked_atomic_seg(wqe, wr);
1683 wqe += sizeof (struct mlx4_wqe_masked_atomic_seg);
1684
1685 size += (sizeof (struct mlx4_wqe_raddr_seg) +
1686 sizeof (struct mlx4_wqe_masked_atomic_seg)) / 16;
1687
1688 break;
1689
Roland Dreier225c7b12007-05-08 18:00:38 -07001690 case IB_WR_RDMA_READ:
1691 case IB_WR_RDMA_WRITE:
1692 case IB_WR_RDMA_WRITE_WITH_IMM:
Roland Dreier0fbfa6a92007-07-18 11:47:55 -07001693 set_raddr_seg(wqe, wr->wr.rdma.remote_addr,
1694 wr->wr.rdma.rkey);
Roland Dreier225c7b12007-05-08 18:00:38 -07001695 wqe += sizeof (struct mlx4_wqe_raddr_seg);
1696 size += sizeof (struct mlx4_wqe_raddr_seg) / 16;
Roland Dreier225c7b12007-05-08 18:00:38 -07001697 break;
1698
Roland Dreier95d04f02008-07-23 08:12:26 -07001699 case IB_WR_LOCAL_INV:
Jack Morgenstein2ac6bf42009-06-05 10:36:24 -07001700 ctrl->srcrb_flags |=
1701 cpu_to_be32(MLX4_WQE_CTRL_STRONG_ORDER);
Roland Dreier95d04f02008-07-23 08:12:26 -07001702 set_local_inv_seg(wqe, wr->ex.invalidate_rkey);
1703 wqe += sizeof (struct mlx4_wqe_local_inval_seg);
1704 size += sizeof (struct mlx4_wqe_local_inval_seg) / 16;
1705 break;
1706
1707 case IB_WR_FAST_REG_MR:
Jack Morgenstein2ac6bf42009-06-05 10:36:24 -07001708 ctrl->srcrb_flags |=
1709 cpu_to_be32(MLX4_WQE_CTRL_STRONG_ORDER);
Roland Dreier95d04f02008-07-23 08:12:26 -07001710 set_fmr_seg(wqe, wr);
1711 wqe += sizeof (struct mlx4_wqe_fmr_seg);
1712 size += sizeof (struct mlx4_wqe_fmr_seg) / 16;
1713 break;
1714
Roland Dreier225c7b12007-05-08 18:00:38 -07001715 default:
1716 /* No extra segments required for sends */
1717 break;
1718 }
1719 break;
1720
1721 case IB_QPT_UD:
Roland Dreier0fbfa6a92007-07-18 11:47:55 -07001722 set_datagram_seg(wqe, wr);
Roland Dreier225c7b12007-05-08 18:00:38 -07001723 wqe += sizeof (struct mlx4_wqe_datagram_seg);
1724 size += sizeof (struct mlx4_wqe_datagram_seg) / 16;
Eli Cohenb832be12008-04-16 21:09:27 -07001725
1726 if (wr->opcode == IB_WR_LSO) {
Eli Cohen417608c2009-11-12 11:19:44 -08001727 err = build_lso_seg(wqe, wr, qp, &seglen, &lso_hdr_sz, &blh);
Eli Cohenb832be12008-04-16 21:09:27 -07001728 if (unlikely(err)) {
1729 *bad_wr = wr;
1730 goto out;
1731 }
Roland Dreier0fd7e1d2009-01-16 12:47:47 -08001732 lso_wqe = (__be32 *) wqe;
Eli Cohenb832be12008-04-16 21:09:27 -07001733 wqe += seglen;
1734 size += seglen / 16;
1735 }
Roland Dreier225c7b12007-05-08 18:00:38 -07001736 break;
1737
1738 case IB_QPT_SMI:
1739 case IB_QPT_GSI:
Roland Dreierf4380002008-04-16 21:09:28 -07001740 err = build_mlx_header(to_msqp(qp), wr, ctrl, &seglen);
1741 if (unlikely(err)) {
Roland Dreier225c7b12007-05-08 18:00:38 -07001742 *bad_wr = wr;
1743 goto out;
1744 }
Roland Dreierf4380002008-04-16 21:09:28 -07001745 wqe += seglen;
1746 size += seglen / 16;
Roland Dreier225c7b12007-05-08 18:00:38 -07001747 break;
1748
1749 default:
1750 break;
1751 }
1752
Jack Morgenstein6e694ea2007-09-19 09:52:25 -07001753 /*
1754 * Write data segments in reverse order, so as to
1755 * overwrite cacheline stamp last within each
1756 * cacheline. This avoids issues with WQE
1757 * prefetching.
1758 */
Roland Dreier225c7b12007-05-08 18:00:38 -07001759
Jack Morgenstein6e694ea2007-09-19 09:52:25 -07001760 dseg = wqe;
1761 dseg += wr->num_sge - 1;
1762 size += wr->num_sge * (sizeof (struct mlx4_wqe_data_seg) / 16);
Roland Dreier225c7b12007-05-08 18:00:38 -07001763
1764 /* Add one more inline data segment for ICRC for MLX sends */
Jack Morgenstein6e694ea2007-09-19 09:52:25 -07001765 if (unlikely(qp->ibqp.qp_type == IB_QPT_SMI ||
1766 qp->ibqp.qp_type == IB_QPT_GSI)) {
1767 set_mlx_icrc_seg(dseg + 1);
Roland Dreier225c7b12007-05-08 18:00:38 -07001768 size += sizeof (struct mlx4_wqe_data_seg) / 16;
1769 }
1770
Jack Morgenstein6e694ea2007-09-19 09:52:25 -07001771 for (i = wr->num_sge - 1; i >= 0; --i, --dseg)
1772 set_data_seg(dseg, wr->sg_list + i);
1773
Roland Dreier0fd7e1d2009-01-16 12:47:47 -08001774 /*
1775 * Possibly overwrite stamping in cacheline with LSO
1776 * segment only after making sure all data segments
1777 * are written.
1778 */
1779 wmb();
1780 *lso_wqe = lso_hdr_sz;
1781
Roland Dreier225c7b12007-05-08 18:00:38 -07001782 ctrl->fence_size = (wr->send_flags & IB_SEND_FENCE ?
1783 MLX4_WQE_CTRL_FENCE : 0) | size;
1784
1785 /*
1786 * Make sure descriptor is fully written before
1787 * setting ownership bit (because HW can start
1788 * executing as soon as we do).
1789 */
1790 wmb();
1791
Roland Dreier59b0ed122007-05-19 08:51:58 -07001792 if (wr->opcode < 0 || wr->opcode >= ARRAY_SIZE(mlx4_ib_opcode)) {
Roland Dreier225c7b12007-05-08 18:00:38 -07001793 err = -EINVAL;
1794 goto out;
1795 }
1796
1797 ctrl->owner_opcode = mlx4_ib_opcode[wr->opcode] |
Eli Cohen417608c2009-11-12 11:19:44 -08001798 (ind & qp->sq.wqe_cnt ? cpu_to_be32(1 << 31) : 0) | blh;
Roland Dreier0e6e7412007-06-18 08:13:48 -07001799
Jack Morgensteinea54b102008-01-28 10:40:59 +02001800 stamp = ind + qp->sq_spare_wqes;
1801 ind += DIV_ROUND_UP(size * 16, 1U << qp->sq.wqe_shift);
1802
Roland Dreier0e6e7412007-06-18 08:13:48 -07001803 /*
1804 * We can improve latency by not stamping the last
1805 * send queue WQE until after ringing the doorbell, so
1806 * only stamp here if there are still more WQEs to post.
Jack Morgensteinea54b102008-01-28 10:40:59 +02001807 *
1808 * Same optimization applies to padding with NOP wqe
1809 * in case of WQE shrinking (used to prevent wrap-around
1810 * in the middle of WR).
Roland Dreier0e6e7412007-06-18 08:13:48 -07001811 */
Jack Morgensteinea54b102008-01-28 10:40:59 +02001812 if (wr->next) {
1813 stamp_send_wqe(qp, stamp, size * 16);
1814 ind = pad_wraparound(qp, ind);
1815 }
Roland Dreier225c7b12007-05-08 18:00:38 -07001816 }
1817
1818out:
1819 if (likely(nreq)) {
1820 qp->sq.head += nreq;
1821
1822 /*
1823 * Make sure that descriptors are written before
1824 * doorbell record.
1825 */
1826 wmb();
1827
1828 writel(qp->doorbell_qpn,
1829 to_mdev(ibqp->device)->uar_map + MLX4_SEND_DOORBELL);
1830
1831 /*
1832 * Make sure doorbells don't leak out of SQ spinlock
1833 * and reach the HCA out of order.
1834 */
1835 mmiowb();
Roland Dreier0e6e7412007-06-18 08:13:48 -07001836
Jack Morgensteinea54b102008-01-28 10:40:59 +02001837 stamp_send_wqe(qp, stamp, size * 16);
1838
1839 ind = pad_wraparound(qp, ind);
1840 qp->sq_next_wqe = ind;
Roland Dreier225c7b12007-05-08 18:00:38 -07001841 }
1842
Roland Dreier96db0e02007-10-30 10:53:54 -07001843 spin_unlock_irqrestore(&qp->sq.lock, flags);
Roland Dreier225c7b12007-05-08 18:00:38 -07001844
1845 return err;
1846}
1847
1848int mlx4_ib_post_recv(struct ib_qp *ibqp, struct ib_recv_wr *wr,
1849 struct ib_recv_wr **bad_wr)
1850{
1851 struct mlx4_ib_qp *qp = to_mqp(ibqp);
1852 struct mlx4_wqe_data_seg *scat;
1853 unsigned long flags;
1854 int err = 0;
1855 int nreq;
1856 int ind;
1857 int i;
1858
1859 spin_lock_irqsave(&qp->rq.lock, flags);
1860
Roland Dreier0e6e7412007-06-18 08:13:48 -07001861 ind = qp->rq.head & (qp->rq.wqe_cnt - 1);
Roland Dreier225c7b12007-05-08 18:00:38 -07001862
1863 for (nreq = 0; wr; ++nreq, wr = wr->next) {
Or Gerlitz2b946072010-01-06 12:51:30 -08001864 if (mlx4_wq_overflow(&qp->rq, nreq, qp->ibqp.recv_cq)) {
Roland Dreier225c7b12007-05-08 18:00:38 -07001865 err = -ENOMEM;
1866 *bad_wr = wr;
1867 goto out;
1868 }
1869
1870 if (unlikely(wr->num_sge > qp->rq.max_gs)) {
1871 err = -EINVAL;
1872 *bad_wr = wr;
1873 goto out;
1874 }
1875
1876 scat = get_recv_wqe(qp, ind);
1877
Roland Dreier2242fa42007-10-09 19:59:05 -07001878 for (i = 0; i < wr->num_sge; ++i)
1879 __set_data_seg(scat + i, wr->sg_list + i);
Roland Dreier225c7b12007-05-08 18:00:38 -07001880
1881 if (i < qp->rq.max_gs) {
1882 scat[i].byte_count = 0;
1883 scat[i].lkey = cpu_to_be32(MLX4_INVALID_LKEY);
1884 scat[i].addr = 0;
1885 }
1886
1887 qp->rq.wrid[ind] = wr->wr_id;
1888
Roland Dreier0e6e7412007-06-18 08:13:48 -07001889 ind = (ind + 1) & (qp->rq.wqe_cnt - 1);
Roland Dreier225c7b12007-05-08 18:00:38 -07001890 }
1891
1892out:
1893 if (likely(nreq)) {
1894 qp->rq.head += nreq;
1895
1896 /*
1897 * Make sure that descriptors are written before
1898 * doorbell record.
1899 */
1900 wmb();
1901
1902 *qp->db.db = cpu_to_be32(qp->rq.head & 0xffff);
1903 }
1904
1905 spin_unlock_irqrestore(&qp->rq.lock, flags);
1906
1907 return err;
1908}
Jack Morgenstein6a775e22007-06-21 12:27:47 +03001909
1910static inline enum ib_qp_state to_ib_qp_state(enum mlx4_qp_state mlx4_state)
1911{
1912 switch (mlx4_state) {
1913 case MLX4_QP_STATE_RST: return IB_QPS_RESET;
1914 case MLX4_QP_STATE_INIT: return IB_QPS_INIT;
1915 case MLX4_QP_STATE_RTR: return IB_QPS_RTR;
1916 case MLX4_QP_STATE_RTS: return IB_QPS_RTS;
1917 case MLX4_QP_STATE_SQ_DRAINING:
1918 case MLX4_QP_STATE_SQD: return IB_QPS_SQD;
1919 case MLX4_QP_STATE_SQER: return IB_QPS_SQE;
1920 case MLX4_QP_STATE_ERR: return IB_QPS_ERR;
1921 default: return -1;
1922 }
1923}
1924
1925static inline enum ib_mig_state to_ib_mig_state(int mlx4_mig_state)
1926{
1927 switch (mlx4_mig_state) {
1928 case MLX4_QP_PM_ARMED: return IB_MIG_ARMED;
1929 case MLX4_QP_PM_REARM: return IB_MIG_REARM;
1930 case MLX4_QP_PM_MIGRATED: return IB_MIG_MIGRATED;
1931 default: return -1;
1932 }
1933}
1934
1935static int to_ib_qp_access_flags(int mlx4_flags)
1936{
1937 int ib_flags = 0;
1938
1939 if (mlx4_flags & MLX4_QP_BIT_RRE)
1940 ib_flags |= IB_ACCESS_REMOTE_READ;
1941 if (mlx4_flags & MLX4_QP_BIT_RWE)
1942 ib_flags |= IB_ACCESS_REMOTE_WRITE;
1943 if (mlx4_flags & MLX4_QP_BIT_RAE)
1944 ib_flags |= IB_ACCESS_REMOTE_ATOMIC;
1945
1946 return ib_flags;
1947}
1948
1949static void to_ib_ah_attr(struct mlx4_dev *dev, struct ib_ah_attr *ib_ah_attr,
1950 struct mlx4_qp_path *path)
1951{
Dotan Barak8fcea952007-07-15 15:00:09 +03001952 memset(ib_ah_attr, 0, sizeof *ib_ah_attr);
Jack Morgenstein6a775e22007-06-21 12:27:47 +03001953 ib_ah_attr->port_num = path->sched_queue & 0x40 ? 2 : 1;
1954
1955 if (ib_ah_attr->port_num == 0 || ib_ah_attr->port_num > dev->caps.num_ports)
1956 return;
1957
1958 ib_ah_attr->dlid = be16_to_cpu(path->rlid);
1959 ib_ah_attr->sl = (path->sched_queue >> 2) & 0xf;
1960 ib_ah_attr->src_path_bits = path->grh_mylmc & 0x7f;
1961 ib_ah_attr->static_rate = path->static_rate ? path->static_rate - 5 : 0;
1962 ib_ah_attr->ah_flags = (path->grh_mylmc & (1 << 7)) ? IB_AH_GRH : 0;
1963 if (ib_ah_attr->ah_flags) {
1964 ib_ah_attr->grh.sgid_index = path->mgid_index;
1965 ib_ah_attr->grh.hop_limit = path->hop_limit;
1966 ib_ah_attr->grh.traffic_class =
1967 (be32_to_cpu(path->tclass_flowlabel) >> 20) & 0xff;
1968 ib_ah_attr->grh.flow_label =
Jack Morgenstein586bb582007-07-17 18:37:38 -07001969 be32_to_cpu(path->tclass_flowlabel) & 0xfffff;
Jack Morgenstein6a775e22007-06-21 12:27:47 +03001970 memcpy(ib_ah_attr->grh.dgid.raw,
1971 path->rgid, sizeof ib_ah_attr->grh.dgid.raw);
1972 }
1973}
1974
1975int mlx4_ib_query_qp(struct ib_qp *ibqp, struct ib_qp_attr *qp_attr, int qp_attr_mask,
1976 struct ib_qp_init_attr *qp_init_attr)
1977{
1978 struct mlx4_ib_dev *dev = to_mdev(ibqp->device);
1979 struct mlx4_ib_qp *qp = to_mqp(ibqp);
1980 struct mlx4_qp_context context;
1981 int mlx4_state;
Dotan Barak0df670302008-04-16 21:09:34 -07001982 int err = 0;
1983
1984 mutex_lock(&qp->mutex);
Jack Morgenstein6a775e22007-06-21 12:27:47 +03001985
1986 if (qp->state == IB_QPS_RESET) {
1987 qp_attr->qp_state = IB_QPS_RESET;
1988 goto done;
1989 }
1990
1991 err = mlx4_qp_query(dev->dev, &qp->mqp, &context);
Dotan Barak0df670302008-04-16 21:09:34 -07001992 if (err) {
1993 err = -EINVAL;
1994 goto out;
1995 }
Jack Morgenstein6a775e22007-06-21 12:27:47 +03001996
1997 mlx4_state = be32_to_cpu(context.flags) >> 28;
1998
Dotan Barak0df670302008-04-16 21:09:34 -07001999 qp->state = to_ib_qp_state(mlx4_state);
2000 qp_attr->qp_state = qp->state;
Jack Morgenstein6a775e22007-06-21 12:27:47 +03002001 qp_attr->path_mtu = context.mtu_msgmax >> 5;
2002 qp_attr->path_mig_state =
2003 to_ib_mig_state((be32_to_cpu(context.flags) >> 11) & 0x3);
2004 qp_attr->qkey = be32_to_cpu(context.qkey);
2005 qp_attr->rq_psn = be32_to_cpu(context.rnr_nextrecvpsn) & 0xffffff;
2006 qp_attr->sq_psn = be32_to_cpu(context.next_send_psn) & 0xffffff;
2007 qp_attr->dest_qp_num = be32_to_cpu(context.remote_qpn) & 0xffffff;
2008 qp_attr->qp_access_flags =
2009 to_ib_qp_access_flags(be32_to_cpu(context.params2));
2010
2011 if (qp->ibqp.qp_type == IB_QPT_RC || qp->ibqp.qp_type == IB_QPT_UC) {
2012 to_ib_ah_attr(dev->dev, &qp_attr->ah_attr, &context.pri_path);
2013 to_ib_ah_attr(dev->dev, &qp_attr->alt_ah_attr, &context.alt_path);
2014 qp_attr->alt_pkey_index = context.alt_path.pkey_index & 0x7f;
2015 qp_attr->alt_port_num = qp_attr->alt_ah_attr.port_num;
2016 }
2017
2018 qp_attr->pkey_index = context.pri_path.pkey_index & 0x7f;
Jack Morgenstein1c27cb72007-07-17 18:37:38 -07002019 if (qp_attr->qp_state == IB_QPS_INIT)
2020 qp_attr->port_num = qp->port;
2021 else
2022 qp_attr->port_num = context.pri_path.sched_queue & 0x40 ? 2 : 1;
Jack Morgenstein6a775e22007-06-21 12:27:47 +03002023
2024 /* qp_attr->en_sqd_async_notify is only applicable in modify qp */
2025 qp_attr->sq_draining = mlx4_state == MLX4_QP_STATE_SQ_DRAINING;
2026
2027 qp_attr->max_rd_atomic = 1 << ((be32_to_cpu(context.params1) >> 21) & 0x7);
2028
2029 qp_attr->max_dest_rd_atomic =
2030 1 << ((be32_to_cpu(context.params2) >> 21) & 0x7);
2031 qp_attr->min_rnr_timer =
2032 (be32_to_cpu(context.rnr_nextrecvpsn) >> 24) & 0x1f;
2033 qp_attr->timeout = context.pri_path.ackto >> 3;
2034 qp_attr->retry_cnt = (be32_to_cpu(context.params1) >> 16) & 0x7;
2035 qp_attr->rnr_retry = (be32_to_cpu(context.params1) >> 13) & 0x7;
2036 qp_attr->alt_timeout = context.alt_path.ackto >> 3;
2037
2038done:
2039 qp_attr->cur_qp_state = qp_attr->qp_state;
Roland Dreier7f5eb9b2007-07-17 20:59:02 -07002040 qp_attr->cap.max_recv_wr = qp->rq.wqe_cnt;
2041 qp_attr->cap.max_recv_sge = qp->rq.max_gs;
2042
Jack Morgenstein6a775e22007-06-21 12:27:47 +03002043 if (!ibqp->uobject) {
Roland Dreier7f5eb9b2007-07-17 20:59:02 -07002044 qp_attr->cap.max_send_wr = qp->sq.wqe_cnt;
2045 qp_attr->cap.max_send_sge = qp->sq.max_gs;
2046 } else {
2047 qp_attr->cap.max_send_wr = 0;
2048 qp_attr->cap.max_send_sge = 0;
Jack Morgenstein6a775e22007-06-21 12:27:47 +03002049 }
2050
Roland Dreier7f5eb9b2007-07-17 20:59:02 -07002051 /*
2052 * We don't support inline sends for kernel QPs (yet), and we
2053 * don't know what userspace's value should be.
2054 */
2055 qp_attr->cap.max_inline_data = 0;
2056
2057 qp_init_attr->cap = qp_attr->cap;
2058
Ron Livne521e5752008-07-14 23:48:48 -07002059 qp_init_attr->create_flags = 0;
2060 if (qp->flags & MLX4_IB_QP_BLOCK_MULTICAST_LOOPBACK)
2061 qp_init_attr->create_flags |= IB_QP_CREATE_BLOCK_MULTICAST_LOOPBACK;
2062
2063 if (qp->flags & MLX4_IB_QP_LSO)
2064 qp_init_attr->create_flags |= IB_QP_CREATE_IPOIB_UD_LSO;
2065
Dotan Barak0df670302008-04-16 21:09:34 -07002066out:
2067 mutex_unlock(&qp->mutex);
2068 return err;
Jack Morgenstein6a775e22007-06-21 12:27:47 +03002069}
2070