blob: b7e3d2342799086174de85a1d8ab3f44f4248456 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Copyright (c) 2004 Topspin Communications. All rights reserved.
Roland Dreier80c8ec22005-07-07 17:57:20 -07003 * Copyright (c) 2005 Cisco Systems. All rights reserved.
Roland Dreier2a1d9b72005-08-10 23:03:10 -07004 * Copyright (c) 2005 Mellanox Technologies. All rights reserved.
5 * Copyright (c) 2004 Voltaire, Inc. All rights reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
7 * This software is available to you under a choice of one of two
8 * licenses. You may choose to be licensed under the terms of the GNU
9 * General Public License (GPL) Version 2, available from the file
10 * COPYING in the main directory of this source tree, or the
11 * OpenIB.org BSD license below:
12 *
13 * Redistribution and use in source and binary forms, with or
14 * without modification, are permitted provided that the following
15 * conditions are met:
16 *
17 * - Redistributions of source code must retain the above
18 * copyright notice, this list of conditions and the following
19 * disclaimer.
20 *
21 * - Redistributions in binary form must reproduce the above
22 * copyright notice, this list of conditions and the following
23 * disclaimer in the documentation and/or other materials
24 * provided with the distribution.
25 *
26 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
27 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
28 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
29 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
30 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
31 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
32 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
33 * SOFTWARE.
34 *
35 * $Id: mthca_qp.c 1355 2004-12-17 15:23:43Z roland $
36 */
37
38#include <linux/init.h>
39
40#include <ib_verbs.h>
41#include <ib_cache.h>
42#include <ib_pack.h>
43
44#include "mthca_dev.h"
45#include "mthca_cmd.h"
46#include "mthca_memfree.h"
47
48enum {
49 MTHCA_MAX_DIRECT_QP_SIZE = 4 * PAGE_SIZE,
50 MTHCA_ACK_REQ_FREQ = 10,
51 MTHCA_FLIGHT_LIMIT = 9,
Roland Dreier80c8ec22005-07-07 17:57:20 -070052 MTHCA_UD_HEADER_SIZE = 72, /* largest UD header possible */
53 MTHCA_INLINE_HEADER_SIZE = 4, /* data segment overhead for inline */
54 MTHCA_INLINE_CHUNK_SIZE = 16 /* inline data segment chunk */
Linus Torvalds1da177e2005-04-16 15:20:36 -070055};
56
57enum {
58 MTHCA_QP_STATE_RST = 0,
59 MTHCA_QP_STATE_INIT = 1,
60 MTHCA_QP_STATE_RTR = 2,
61 MTHCA_QP_STATE_RTS = 3,
62 MTHCA_QP_STATE_SQE = 4,
63 MTHCA_QP_STATE_SQD = 5,
64 MTHCA_QP_STATE_ERR = 6,
65 MTHCA_QP_STATE_DRAINING = 7
66};
67
68enum {
69 MTHCA_QP_ST_RC = 0x0,
70 MTHCA_QP_ST_UC = 0x1,
71 MTHCA_QP_ST_RD = 0x2,
72 MTHCA_QP_ST_UD = 0x3,
73 MTHCA_QP_ST_MLX = 0x7
74};
75
76enum {
77 MTHCA_QP_PM_MIGRATED = 0x3,
78 MTHCA_QP_PM_ARMED = 0x0,
79 MTHCA_QP_PM_REARM = 0x1
80};
81
82enum {
83 /* qp_context flags */
84 MTHCA_QP_BIT_DE = 1 << 8,
85 /* params1 */
86 MTHCA_QP_BIT_SRE = 1 << 15,
87 MTHCA_QP_BIT_SWE = 1 << 14,
88 MTHCA_QP_BIT_SAE = 1 << 13,
89 MTHCA_QP_BIT_SIC = 1 << 4,
90 MTHCA_QP_BIT_SSC = 1 << 3,
91 /* params2 */
92 MTHCA_QP_BIT_RRE = 1 << 15,
93 MTHCA_QP_BIT_RWE = 1 << 14,
94 MTHCA_QP_BIT_RAE = 1 << 13,
95 MTHCA_QP_BIT_RIC = 1 << 4,
96 MTHCA_QP_BIT_RSC = 1 << 3
97};
98
99struct mthca_qp_path {
Sean Hefty97f52eb2005-08-13 21:05:57 -0700100 __be32 port_pkey;
101 u8 rnr_retry;
102 u8 g_mylmc;
103 __be16 rlid;
104 u8 ackto;
105 u8 mgid_index;
106 u8 static_rate;
107 u8 hop_limit;
108 __be32 sl_tclass_flowlabel;
109 u8 rgid[16];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110} __attribute__((packed));
111
112struct mthca_qp_context {
Sean Hefty97f52eb2005-08-13 21:05:57 -0700113 __be32 flags;
114 __be32 tavor_sched_queue; /* Reserved on Arbel */
115 u8 mtu_msgmax;
116 u8 rq_size_stride; /* Reserved on Tavor */
117 u8 sq_size_stride; /* Reserved on Tavor */
118 u8 rlkey_arbel_sched_queue; /* Reserved on Tavor */
119 __be32 usr_page;
120 __be32 local_qpn;
121 __be32 remote_qpn;
122 u32 reserved1[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 struct mthca_qp_path pri_path;
124 struct mthca_qp_path alt_path;
Sean Hefty97f52eb2005-08-13 21:05:57 -0700125 __be32 rdd;
126 __be32 pd;
127 __be32 wqe_base;
128 __be32 wqe_lkey;
129 __be32 params1;
130 __be32 reserved2;
131 __be32 next_send_psn;
132 __be32 cqn_snd;
133 __be32 snd_wqe_base_l; /* Next send WQE on Tavor */
134 __be32 snd_db_index; /* (debugging only entries) */
135 __be32 last_acked_psn;
136 __be32 ssn;
137 __be32 params2;
138 __be32 rnr_nextrecvpsn;
139 __be32 ra_buff_indx;
140 __be32 cqn_rcv;
141 __be32 rcv_wqe_base_l; /* Next recv WQE on Tavor */
142 __be32 rcv_db_index; /* (debugging only entries) */
143 __be32 qkey;
144 __be32 srqn;
145 __be32 rmsn;
146 __be16 rq_wqe_counter; /* reserved on Tavor */
147 __be16 sq_wqe_counter; /* reserved on Tavor */
148 u32 reserved3[18];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149} __attribute__((packed));
150
151struct mthca_qp_param {
Sean Hefty97f52eb2005-08-13 21:05:57 -0700152 __be32 opt_param_mask;
153 u32 reserved1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 struct mthca_qp_context context;
Sean Hefty97f52eb2005-08-13 21:05:57 -0700155 u32 reserved2[62];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156} __attribute__((packed));
157
158enum {
159 MTHCA_QP_OPTPAR_ALT_ADDR_PATH = 1 << 0,
160 MTHCA_QP_OPTPAR_RRE = 1 << 1,
161 MTHCA_QP_OPTPAR_RAE = 1 << 2,
162 MTHCA_QP_OPTPAR_RWE = 1 << 3,
163 MTHCA_QP_OPTPAR_PKEY_INDEX = 1 << 4,
164 MTHCA_QP_OPTPAR_Q_KEY = 1 << 5,
165 MTHCA_QP_OPTPAR_RNR_TIMEOUT = 1 << 6,
166 MTHCA_QP_OPTPAR_PRIMARY_ADDR_PATH = 1 << 7,
167 MTHCA_QP_OPTPAR_SRA_MAX = 1 << 8,
168 MTHCA_QP_OPTPAR_RRA_MAX = 1 << 9,
169 MTHCA_QP_OPTPAR_PM_STATE = 1 << 10,
170 MTHCA_QP_OPTPAR_PORT_NUM = 1 << 11,
171 MTHCA_QP_OPTPAR_RETRY_COUNT = 1 << 12,
172 MTHCA_QP_OPTPAR_ALT_RNR_RETRY = 1 << 13,
173 MTHCA_QP_OPTPAR_ACK_TIMEOUT = 1 << 14,
174 MTHCA_QP_OPTPAR_RNR_RETRY = 1 << 15,
175 MTHCA_QP_OPTPAR_SCHED_QUEUE = 1 << 16
176};
177
178enum {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 MTHCA_NEXT_DBD = 1 << 7,
180 MTHCA_NEXT_FENCE = 1 << 6,
181 MTHCA_NEXT_CQ_UPDATE = 1 << 3,
182 MTHCA_NEXT_EVENT_GEN = 1 << 2,
183 MTHCA_NEXT_SOLICIT = 1 << 1,
184
185 MTHCA_MLX_VL15 = 1 << 17,
186 MTHCA_MLX_SLR = 1 << 16
187};
188
Roland Dreierddf841f2005-04-16 15:26:33 -0700189enum {
190 MTHCA_INVAL_LKEY = 0x100
191};
192
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193struct mthca_next_seg {
Sean Hefty97f52eb2005-08-13 21:05:57 -0700194 __be32 nda_op; /* [31:6] next WQE [4:0] next opcode */
195 __be32 ee_nds; /* [31:8] next EE [7] DBD [6] F [5:0] next WQE size */
196 __be32 flags; /* [3] CQ [2] Event [1] Solicit */
197 __be32 imm; /* immediate data */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198};
199
200struct mthca_tavor_ud_seg {
Sean Hefty97f52eb2005-08-13 21:05:57 -0700201 u32 reserved1;
202 __be32 lkey;
203 __be64 av_addr;
204 u32 reserved2[4];
205 __be32 dqpn;
206 __be32 qkey;
207 u32 reserved3[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208};
209
210struct mthca_arbel_ud_seg {
Sean Hefty97f52eb2005-08-13 21:05:57 -0700211 __be32 av[8];
212 __be32 dqpn;
213 __be32 qkey;
214 u32 reserved[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215};
216
217struct mthca_bind_seg {
Sean Hefty97f52eb2005-08-13 21:05:57 -0700218 __be32 flags; /* [31] Atomic [30] rem write [29] rem read */
219 u32 reserved;
220 __be32 new_rkey;
221 __be32 lkey;
222 __be64 addr;
223 __be64 length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224};
225
226struct mthca_raddr_seg {
Sean Hefty97f52eb2005-08-13 21:05:57 -0700227 __be64 raddr;
228 __be32 rkey;
229 u32 reserved;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230};
231
232struct mthca_atomic_seg {
Sean Hefty97f52eb2005-08-13 21:05:57 -0700233 __be64 swap_add;
234 __be64 compare;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235};
236
237struct mthca_data_seg {
Sean Hefty97f52eb2005-08-13 21:05:57 -0700238 __be32 byte_count;
239 __be32 lkey;
240 __be64 addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241};
242
243struct mthca_mlx_seg {
Sean Hefty97f52eb2005-08-13 21:05:57 -0700244 __be32 nda_op;
245 __be32 nds;
246 __be32 flags; /* [17] VL15 [16] SLR [14:12] static rate
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 [11:8] SL [3] C [2] E */
Sean Hefty97f52eb2005-08-13 21:05:57 -0700248 __be16 rlid;
249 __be16 vcrc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250};
251
252static const u8 mthca_opcode[] = {
253 [IB_WR_SEND] = MTHCA_OPCODE_SEND,
254 [IB_WR_SEND_WITH_IMM] = MTHCA_OPCODE_SEND_IMM,
255 [IB_WR_RDMA_WRITE] = MTHCA_OPCODE_RDMA_WRITE,
256 [IB_WR_RDMA_WRITE_WITH_IMM] = MTHCA_OPCODE_RDMA_WRITE_IMM,
257 [IB_WR_RDMA_READ] = MTHCA_OPCODE_RDMA_READ,
258 [IB_WR_ATOMIC_CMP_AND_SWP] = MTHCA_OPCODE_ATOMIC_CS,
259 [IB_WR_ATOMIC_FETCH_AND_ADD] = MTHCA_OPCODE_ATOMIC_FA,
260};
261
262static int is_sqp(struct mthca_dev *dev, struct mthca_qp *qp)
263{
264 return qp->qpn >= dev->qp_table.sqp_start &&
265 qp->qpn <= dev->qp_table.sqp_start + 3;
266}
267
268static int is_qp0(struct mthca_dev *dev, struct mthca_qp *qp)
269{
270 return qp->qpn >= dev->qp_table.sqp_start &&
271 qp->qpn <= dev->qp_table.sqp_start + 1;
272}
273
274static void *get_recv_wqe(struct mthca_qp *qp, int n)
275{
276 if (qp->is_direct)
277 return qp->queue.direct.buf + (n << qp->rq.wqe_shift);
278 else
279 return qp->queue.page_list[(n << qp->rq.wqe_shift) >> PAGE_SHIFT].buf +
280 ((n << qp->rq.wqe_shift) & (PAGE_SIZE - 1));
281}
282
283static void *get_send_wqe(struct mthca_qp *qp, int n)
284{
285 if (qp->is_direct)
286 return qp->queue.direct.buf + qp->send_wqe_offset +
287 (n << qp->sq.wqe_shift);
288 else
289 return qp->queue.page_list[(qp->send_wqe_offset +
290 (n << qp->sq.wqe_shift)) >>
291 PAGE_SHIFT].buf +
292 ((qp->send_wqe_offset + (n << qp->sq.wqe_shift)) &
293 (PAGE_SIZE - 1));
294}
295
296void mthca_qp_event(struct mthca_dev *dev, u32 qpn,
297 enum ib_event_type event_type)
298{
299 struct mthca_qp *qp;
300 struct ib_event event;
301
302 spin_lock(&dev->qp_table.lock);
303 qp = mthca_array_get(&dev->qp_table.qp, qpn & (dev->limits.num_qps - 1));
304 if (qp)
305 atomic_inc(&qp->refcount);
306 spin_unlock(&dev->qp_table.lock);
307
308 if (!qp) {
309 mthca_warn(dev, "Async event for bogus QP %08x\n", qpn);
310 return;
311 }
312
313 event.device = &dev->ib_dev;
314 event.event = event_type;
315 event.element.qp = &qp->ibqp;
316 if (qp->ibqp.event_handler)
317 qp->ibqp.event_handler(&event, qp->ibqp.qp_context);
318
319 if (atomic_dec_and_test(&qp->refcount))
320 wake_up(&qp->wait);
321}
322
323static int to_mthca_state(enum ib_qp_state ib_state)
324{
325 switch (ib_state) {
326 case IB_QPS_RESET: return MTHCA_QP_STATE_RST;
327 case IB_QPS_INIT: return MTHCA_QP_STATE_INIT;
328 case IB_QPS_RTR: return MTHCA_QP_STATE_RTR;
329 case IB_QPS_RTS: return MTHCA_QP_STATE_RTS;
330 case IB_QPS_SQD: return MTHCA_QP_STATE_SQD;
331 case IB_QPS_SQE: return MTHCA_QP_STATE_SQE;
332 case IB_QPS_ERR: return MTHCA_QP_STATE_ERR;
333 default: return -1;
334 }
335}
336
337enum { RC, UC, UD, RD, RDEE, MLX, NUM_TRANS };
338
339static int to_mthca_st(int transport)
340{
341 switch (transport) {
342 case RC: return MTHCA_QP_ST_RC;
343 case UC: return MTHCA_QP_ST_UC;
344 case UD: return MTHCA_QP_ST_UD;
345 case RD: return MTHCA_QP_ST_RD;
346 case MLX: return MTHCA_QP_ST_MLX;
347 default: return -1;
348 }
349}
350
351static const struct {
352 int trans;
353 u32 req_param[NUM_TRANS];
354 u32 opt_param[NUM_TRANS];
355} state_table[IB_QPS_ERR + 1][IB_QPS_ERR + 1] = {
356 [IB_QPS_RESET] = {
357 [IB_QPS_RESET] = { .trans = MTHCA_TRANS_ANY2RST },
358 [IB_QPS_ERR] = { .trans = MTHCA_TRANS_ANY2ERR },
359 [IB_QPS_INIT] = {
360 .trans = MTHCA_TRANS_RST2INIT,
361 .req_param = {
362 [UD] = (IB_QP_PKEY_INDEX |
363 IB_QP_PORT |
364 IB_QP_QKEY),
Roland Dreier9e6970b2005-06-27 14:36:42 -0700365 [UC] = (IB_QP_PKEY_INDEX |
366 IB_QP_PORT |
367 IB_QP_ACCESS_FLAGS),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 [RC] = (IB_QP_PKEY_INDEX |
369 IB_QP_PORT |
370 IB_QP_ACCESS_FLAGS),
371 [MLX] = (IB_QP_PKEY_INDEX |
372 IB_QP_QKEY),
373 },
374 /* bug-for-bug compatibility with VAPI: */
375 .opt_param = {
376 [MLX] = IB_QP_PORT
377 }
378 },
379 },
380 [IB_QPS_INIT] = {
381 [IB_QPS_RESET] = { .trans = MTHCA_TRANS_ANY2RST },
382 [IB_QPS_ERR] = { .trans = MTHCA_TRANS_ANY2ERR },
383 [IB_QPS_INIT] = {
384 .trans = MTHCA_TRANS_INIT2INIT,
385 .opt_param = {
386 [UD] = (IB_QP_PKEY_INDEX |
387 IB_QP_PORT |
388 IB_QP_QKEY),
Roland Dreier9e6970b2005-06-27 14:36:42 -0700389 [UC] = (IB_QP_PKEY_INDEX |
390 IB_QP_PORT |
391 IB_QP_ACCESS_FLAGS),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 [RC] = (IB_QP_PKEY_INDEX |
393 IB_QP_PORT |
394 IB_QP_ACCESS_FLAGS),
395 [MLX] = (IB_QP_PKEY_INDEX |
396 IB_QP_QKEY),
397 }
398 },
399 [IB_QPS_RTR] = {
400 .trans = MTHCA_TRANS_INIT2RTR,
401 .req_param = {
Roland Dreier9e6970b2005-06-27 14:36:42 -0700402 [UC] = (IB_QP_AV |
403 IB_QP_PATH_MTU |
404 IB_QP_DEST_QPN |
405 IB_QP_RQ_PSN |
406 IB_QP_MAX_DEST_RD_ATOMIC),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 [RC] = (IB_QP_AV |
408 IB_QP_PATH_MTU |
409 IB_QP_DEST_QPN |
410 IB_QP_RQ_PSN |
411 IB_QP_MAX_DEST_RD_ATOMIC |
412 IB_QP_MIN_RNR_TIMER),
413 },
414 .opt_param = {
415 [UD] = (IB_QP_PKEY_INDEX |
416 IB_QP_QKEY),
Roland Dreier9e6970b2005-06-27 14:36:42 -0700417 [UC] = (IB_QP_ALT_PATH |
418 IB_QP_ACCESS_FLAGS |
419 IB_QP_PKEY_INDEX),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 [RC] = (IB_QP_ALT_PATH |
421 IB_QP_ACCESS_FLAGS |
422 IB_QP_PKEY_INDEX),
423 [MLX] = (IB_QP_PKEY_INDEX |
424 IB_QP_QKEY),
425 }
426 }
427 },
428 [IB_QPS_RTR] = {
429 [IB_QPS_RESET] = { .trans = MTHCA_TRANS_ANY2RST },
430 [IB_QPS_ERR] = { .trans = MTHCA_TRANS_ANY2ERR },
431 [IB_QPS_RTS] = {
432 .trans = MTHCA_TRANS_RTR2RTS,
433 .req_param = {
434 [UD] = IB_QP_SQ_PSN,
Roland Dreier9e6970b2005-06-27 14:36:42 -0700435 [UC] = (IB_QP_SQ_PSN |
436 IB_QP_MAX_QP_RD_ATOMIC),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 [RC] = (IB_QP_TIMEOUT |
438 IB_QP_RETRY_CNT |
439 IB_QP_RNR_RETRY |
440 IB_QP_SQ_PSN |
441 IB_QP_MAX_QP_RD_ATOMIC),
442 [MLX] = IB_QP_SQ_PSN,
443 },
444 .opt_param = {
445 [UD] = (IB_QP_CUR_STATE |
446 IB_QP_QKEY),
Roland Dreier9e6970b2005-06-27 14:36:42 -0700447 [UC] = (IB_QP_CUR_STATE |
448 IB_QP_ALT_PATH |
449 IB_QP_ACCESS_FLAGS |
450 IB_QP_PKEY_INDEX |
451 IB_QP_PATH_MIG_STATE),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 [RC] = (IB_QP_CUR_STATE |
453 IB_QP_ALT_PATH |
454 IB_QP_ACCESS_FLAGS |
455 IB_QP_PKEY_INDEX |
456 IB_QP_MIN_RNR_TIMER |
457 IB_QP_PATH_MIG_STATE),
458 [MLX] = (IB_QP_CUR_STATE |
459 IB_QP_QKEY),
460 }
461 }
462 },
463 [IB_QPS_RTS] = {
464 [IB_QPS_RESET] = { .trans = MTHCA_TRANS_ANY2RST },
465 [IB_QPS_ERR] = { .trans = MTHCA_TRANS_ANY2ERR },
466 [IB_QPS_RTS] = {
467 .trans = MTHCA_TRANS_RTS2RTS,
468 .opt_param = {
469 [UD] = (IB_QP_CUR_STATE |
470 IB_QP_QKEY),
Roland Dreier9e6970b2005-06-27 14:36:42 -0700471 [UC] = (IB_QP_ACCESS_FLAGS |
472 IB_QP_ALT_PATH |
473 IB_QP_PATH_MIG_STATE),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 [RC] = (IB_QP_ACCESS_FLAGS |
475 IB_QP_ALT_PATH |
476 IB_QP_PATH_MIG_STATE |
477 IB_QP_MIN_RNR_TIMER),
478 [MLX] = (IB_QP_CUR_STATE |
479 IB_QP_QKEY),
480 }
481 },
482 [IB_QPS_SQD] = {
483 .trans = MTHCA_TRANS_RTS2SQD,
484 },
485 },
486 [IB_QPS_SQD] = {
487 [IB_QPS_RESET] = { .trans = MTHCA_TRANS_ANY2RST },
488 [IB_QPS_ERR] = { .trans = MTHCA_TRANS_ANY2ERR },
489 [IB_QPS_RTS] = {
490 .trans = MTHCA_TRANS_SQD2RTS,
491 .opt_param = {
492 [UD] = (IB_QP_CUR_STATE |
493 IB_QP_QKEY),
Roland Dreier9e6970b2005-06-27 14:36:42 -0700494 [UC] = (IB_QP_CUR_STATE |
495 IB_QP_ALT_PATH |
496 IB_QP_ACCESS_FLAGS |
497 IB_QP_PATH_MIG_STATE),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 [RC] = (IB_QP_CUR_STATE |
499 IB_QP_ALT_PATH |
500 IB_QP_ACCESS_FLAGS |
501 IB_QP_MIN_RNR_TIMER |
502 IB_QP_PATH_MIG_STATE),
503 [MLX] = (IB_QP_CUR_STATE |
504 IB_QP_QKEY),
505 }
506 },
507 [IB_QPS_SQD] = {
508 .trans = MTHCA_TRANS_SQD2SQD,
509 .opt_param = {
510 [UD] = (IB_QP_PKEY_INDEX |
511 IB_QP_QKEY),
Roland Dreier9e6970b2005-06-27 14:36:42 -0700512 [UC] = (IB_QP_AV |
513 IB_QP_MAX_QP_RD_ATOMIC |
514 IB_QP_MAX_DEST_RD_ATOMIC |
515 IB_QP_CUR_STATE |
516 IB_QP_ALT_PATH |
517 IB_QP_ACCESS_FLAGS |
518 IB_QP_PKEY_INDEX |
519 IB_QP_PATH_MIG_STATE),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 [RC] = (IB_QP_AV |
521 IB_QP_TIMEOUT |
522 IB_QP_RETRY_CNT |
523 IB_QP_RNR_RETRY |
524 IB_QP_MAX_QP_RD_ATOMIC |
525 IB_QP_MAX_DEST_RD_ATOMIC |
526 IB_QP_CUR_STATE |
527 IB_QP_ALT_PATH |
528 IB_QP_ACCESS_FLAGS |
529 IB_QP_PKEY_INDEX |
530 IB_QP_MIN_RNR_TIMER |
531 IB_QP_PATH_MIG_STATE),
532 [MLX] = (IB_QP_PKEY_INDEX |
533 IB_QP_QKEY),
534 }
535 }
536 },
537 [IB_QPS_SQE] = {
538 [IB_QPS_RESET] = { .trans = MTHCA_TRANS_ANY2RST },
539 [IB_QPS_ERR] = { .trans = MTHCA_TRANS_ANY2ERR },
540 [IB_QPS_RTS] = {
541 .trans = MTHCA_TRANS_SQERR2RTS,
542 .opt_param = {
543 [UD] = (IB_QP_CUR_STATE |
544 IB_QP_QKEY),
Roland Dreier9e6970b2005-06-27 14:36:42 -0700545 [UC] = (IB_QP_CUR_STATE),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 [RC] = (IB_QP_CUR_STATE |
547 IB_QP_MIN_RNR_TIMER),
548 [MLX] = (IB_QP_CUR_STATE |
549 IB_QP_QKEY),
550 }
551 }
552 },
553 [IB_QPS_ERR] = {
554 [IB_QPS_RESET] = { .trans = MTHCA_TRANS_ANY2RST },
555 [IB_QPS_ERR] = { .trans = MTHCA_TRANS_ANY2ERR }
556 }
557};
558
559static void store_attrs(struct mthca_sqp *sqp, struct ib_qp_attr *attr,
560 int attr_mask)
561{
562 if (attr_mask & IB_QP_PKEY_INDEX)
563 sqp->pkey_index = attr->pkey_index;
564 if (attr_mask & IB_QP_QKEY)
565 sqp->qkey = attr->qkey;
566 if (attr_mask & IB_QP_SQ_PSN)
567 sqp->send_psn = attr->sq_psn;
568}
569
570static void init_port(struct mthca_dev *dev, int port)
571{
572 int err;
573 u8 status;
574 struct mthca_init_ib_param param;
575
576 memset(&param, 0, sizeof param);
577
Roland Dreierda6561c2005-08-17 07:39:10 -0700578 param.port_width = dev->limits.port_width_cap;
579 param.vl_cap = dev->limits.vl_cap;
580 param.mtu_cap = dev->limits.mtu_cap;
581 param.gid_cap = dev->limits.gid_table_len;
582 param.pkey_cap = dev->limits.pkey_table_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583
584 err = mthca_INIT_IB(dev, &param, port, &status);
585 if (err)
586 mthca_warn(dev, "INIT_IB failed, return code %d.\n", err);
587 if (status)
588 mthca_warn(dev, "INIT_IB returned status %02x.\n", status);
589}
590
591int mthca_modify_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr, int attr_mask)
592{
593 struct mthca_dev *dev = to_mdev(ibqp->device);
594 struct mthca_qp *qp = to_mqp(ibqp);
595 enum ib_qp_state cur_state, new_state;
Roland Dreiered878452005-06-27 14:36:45 -0700596 struct mthca_mailbox *mailbox;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 struct mthca_qp_param *qp_param;
598 struct mthca_qp_context *qp_context;
599 u32 req_param, opt_param;
600 u8 status;
601 int err;
602
603 if (attr_mask & IB_QP_CUR_STATE) {
604 if (attr->cur_qp_state != IB_QPS_RTR &&
605 attr->cur_qp_state != IB_QPS_RTS &&
606 attr->cur_qp_state != IB_QPS_SQD &&
607 attr->cur_qp_state != IB_QPS_SQE)
608 return -EINVAL;
609 else
610 cur_state = attr->cur_qp_state;
611 } else {
612 spin_lock_irq(&qp->sq.lock);
613 spin_lock(&qp->rq.lock);
614 cur_state = qp->state;
615 spin_unlock(&qp->rq.lock);
616 spin_unlock_irq(&qp->sq.lock);
617 }
618
619 if (attr_mask & IB_QP_STATE) {
620 if (attr->qp_state < 0 || attr->qp_state > IB_QPS_ERR)
621 return -EINVAL;
622 new_state = attr->qp_state;
623 } else
624 new_state = cur_state;
625
626 if (state_table[cur_state][new_state].trans == MTHCA_TRANS_INVALID) {
627 mthca_dbg(dev, "Illegal QP transition "
628 "%d->%d\n", cur_state, new_state);
629 return -EINVAL;
630 }
631
632 req_param = state_table[cur_state][new_state].req_param[qp->transport];
633 opt_param = state_table[cur_state][new_state].opt_param[qp->transport];
634
635 if ((req_param & attr_mask) != req_param) {
636 mthca_dbg(dev, "QP transition "
637 "%d->%d missing req attr 0x%08x\n",
638 cur_state, new_state,
639 req_param & ~attr_mask);
640 return -EINVAL;
641 }
642
643 if (attr_mask & ~(req_param | opt_param | IB_QP_STATE)) {
644 mthca_dbg(dev, "QP transition (transport %d) "
645 "%d->%d has extra attr 0x%08x\n",
646 qp->transport,
647 cur_state, new_state,
648 attr_mask & ~(req_param | opt_param |
649 IB_QP_STATE));
650 return -EINVAL;
651 }
652
Roland Dreiered878452005-06-27 14:36:45 -0700653 mailbox = mthca_alloc_mailbox(dev, GFP_KERNEL);
654 if (IS_ERR(mailbox))
655 return PTR_ERR(mailbox);
656 qp_param = mailbox->buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 qp_context = &qp_param->context;
658 memset(qp_param, 0, sizeof *qp_param);
659
660 qp_context->flags = cpu_to_be32((to_mthca_state(new_state) << 28) |
661 (to_mthca_st(qp->transport) << 16));
662 qp_context->flags |= cpu_to_be32(MTHCA_QP_BIT_DE);
663 if (!(attr_mask & IB_QP_PATH_MIG_STATE))
664 qp_context->flags |= cpu_to_be32(MTHCA_QP_PM_MIGRATED << 11);
665 else {
666 qp_param->opt_param_mask |= cpu_to_be32(MTHCA_QP_OPTPAR_PM_STATE);
667 switch (attr->path_mig_state) {
668 case IB_MIG_MIGRATED:
669 qp_context->flags |= cpu_to_be32(MTHCA_QP_PM_MIGRATED << 11);
670 break;
671 case IB_MIG_REARM:
672 qp_context->flags |= cpu_to_be32(MTHCA_QP_PM_REARM << 11);
673 break;
674 case IB_MIG_ARMED:
675 qp_context->flags |= cpu_to_be32(MTHCA_QP_PM_ARMED << 11);
676 break;
677 }
678 }
679
680 /* leave tavor_sched_queue as 0 */
681
682 if (qp->transport == MLX || qp->transport == UD)
683 qp_context->mtu_msgmax = (IB_MTU_2048 << 5) | 11;
684 else if (attr_mask & IB_QP_PATH_MTU)
685 qp_context->mtu_msgmax = (attr->path_mtu << 5) | 31;
686
Roland Dreierd10ddbf2005-04-16 15:26:32 -0700687 if (mthca_is_memfree(dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 qp_context->rq_size_stride =
689 ((ffs(qp->rq.max) - 1) << 3) | (qp->rq.wqe_shift - 4);
690 qp_context->sq_size_stride =
691 ((ffs(qp->sq.max) - 1) << 3) | (qp->sq.wqe_shift - 4);
692 }
693
694 /* leave arbel_sched_queue as 0 */
695
Roland Dreier80c8ec22005-07-07 17:57:20 -0700696 if (qp->ibqp.uobject)
697 qp_context->usr_page =
698 cpu_to_be32(to_mucontext(qp->ibqp.uobject->context)->uar.index);
699 else
700 qp_context->usr_page = cpu_to_be32(dev->driver_uar.index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 qp_context->local_qpn = cpu_to_be32(qp->qpn);
702 if (attr_mask & IB_QP_DEST_QPN) {
703 qp_context->remote_qpn = cpu_to_be32(attr->dest_qp_num);
704 }
705
706 if (qp->transport == MLX)
707 qp_context->pri_path.port_pkey |=
708 cpu_to_be32(to_msqp(qp)->port << 24);
709 else {
710 if (attr_mask & IB_QP_PORT) {
711 qp_context->pri_path.port_pkey |=
712 cpu_to_be32(attr->port_num << 24);
713 qp_param->opt_param_mask |= cpu_to_be32(MTHCA_QP_OPTPAR_PORT_NUM);
714 }
715 }
716
717 if (attr_mask & IB_QP_PKEY_INDEX) {
718 qp_context->pri_path.port_pkey |=
719 cpu_to_be32(attr->pkey_index);
720 qp_param->opt_param_mask |= cpu_to_be32(MTHCA_QP_OPTPAR_PKEY_INDEX);
721 }
722
723 if (attr_mask & IB_QP_RNR_RETRY) {
724 qp_context->pri_path.rnr_retry = attr->rnr_retry << 5;
725 qp_param->opt_param_mask |= cpu_to_be32(MTHCA_QP_OPTPAR_RNR_RETRY);
726 }
727
728 if (attr_mask & IB_QP_AV) {
729 qp_context->pri_path.g_mylmc = attr->ah_attr.src_path_bits & 0x7f;
730 qp_context->pri_path.rlid = cpu_to_be16(attr->ah_attr.dlid);
Roland Dreiercd123d72005-06-27 14:36:40 -0700731 qp_context->pri_path.static_rate = !!attr->ah_attr.static_rate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 if (attr->ah_attr.ah_flags & IB_AH_GRH) {
733 qp_context->pri_path.g_mylmc |= 1 << 7;
734 qp_context->pri_path.mgid_index = attr->ah_attr.grh.sgid_index;
735 qp_context->pri_path.hop_limit = attr->ah_attr.grh.hop_limit;
736 qp_context->pri_path.sl_tclass_flowlabel =
737 cpu_to_be32((attr->ah_attr.sl << 28) |
738 (attr->ah_attr.grh.traffic_class << 20) |
739 (attr->ah_attr.grh.flow_label));
740 memcpy(qp_context->pri_path.rgid,
741 attr->ah_attr.grh.dgid.raw, 16);
742 } else {
743 qp_context->pri_path.sl_tclass_flowlabel =
744 cpu_to_be32(attr->ah_attr.sl << 28);
745 }
746 qp_param->opt_param_mask |= cpu_to_be32(MTHCA_QP_OPTPAR_PRIMARY_ADDR_PATH);
747 }
748
749 if (attr_mask & IB_QP_TIMEOUT) {
750 qp_context->pri_path.ackto = attr->timeout;
751 qp_param->opt_param_mask |= cpu_to_be32(MTHCA_QP_OPTPAR_ACK_TIMEOUT);
752 }
753
754 /* XXX alt_path */
755
756 /* leave rdd as 0 */
757 qp_context->pd = cpu_to_be32(to_mpd(ibqp->pd)->pd_num);
758 /* leave wqe_base as 0 (we always create an MR based at 0 for WQs) */
759 qp_context->wqe_lkey = cpu_to_be32(qp->mr.ibmr.lkey);
760 qp_context->params1 = cpu_to_be32((MTHCA_ACK_REQ_FREQ << 28) |
761 (MTHCA_FLIGHT_LIMIT << 24) |
762 MTHCA_QP_BIT_SRE |
763 MTHCA_QP_BIT_SWE |
764 MTHCA_QP_BIT_SAE);
765 if (qp->sq_policy == IB_SIGNAL_ALL_WR)
766 qp_context->params1 |= cpu_to_be32(MTHCA_QP_BIT_SSC);
767 if (attr_mask & IB_QP_RETRY_CNT) {
768 qp_context->params1 |= cpu_to_be32(attr->retry_cnt << 16);
769 qp_param->opt_param_mask |= cpu_to_be32(MTHCA_QP_OPTPAR_RETRY_COUNT);
770 }
771
Roland Dreier34a4a752005-06-27 14:36:41 -0700772 if (attr_mask & IB_QP_MAX_QP_RD_ATOMIC) {
773 qp_context->params1 |= cpu_to_be32(min(attr->max_rd_atomic ?
774 ffs(attr->max_rd_atomic) - 1 : 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 7) << 21);
776 qp_param->opt_param_mask |= cpu_to_be32(MTHCA_QP_OPTPAR_SRA_MAX);
777 }
778
779 if (attr_mask & IB_QP_SQ_PSN)
780 qp_context->next_send_psn = cpu_to_be32(attr->sq_psn);
781 qp_context->cqn_snd = cpu_to_be32(to_mcq(ibqp->send_cq)->cqn);
782
Roland Dreierd10ddbf2005-04-16 15:26:32 -0700783 if (mthca_is_memfree(dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 qp_context->snd_wqe_base_l = cpu_to_be32(qp->send_wqe_offset);
785 qp_context->snd_db_index = cpu_to_be32(qp->sq.db_index);
786 }
787
788 if (attr_mask & IB_QP_ACCESS_FLAGS) {
789 /*
790 * Only enable RDMA/atomics if we have responder
791 * resources set to a non-zero value.
792 */
793 if (qp->resp_depth) {
794 qp_context->params2 |=
795 cpu_to_be32(attr->qp_access_flags & IB_ACCESS_REMOTE_WRITE ?
796 MTHCA_QP_BIT_RWE : 0);
797 qp_context->params2 |=
798 cpu_to_be32(attr->qp_access_flags & IB_ACCESS_REMOTE_READ ?
799 MTHCA_QP_BIT_RRE : 0);
800 qp_context->params2 |=
801 cpu_to_be32(attr->qp_access_flags & IB_ACCESS_REMOTE_ATOMIC ?
802 MTHCA_QP_BIT_RAE : 0);
803 }
804
805 qp_param->opt_param_mask |= cpu_to_be32(MTHCA_QP_OPTPAR_RWE |
806 MTHCA_QP_OPTPAR_RRE |
807 MTHCA_QP_OPTPAR_RAE);
808
809 qp->atomic_rd_en = attr->qp_access_flags;
810 }
811
Roland Dreier34a4a752005-06-27 14:36:41 -0700812 if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 u8 rra_max;
814
Roland Dreier34a4a752005-06-27 14:36:41 -0700815 if (qp->resp_depth && !attr->max_dest_rd_atomic) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 /*
817 * Lowering our responder resources to zero.
818 * Turn off RDMA/atomics as responder.
819 * (RWE/RRE/RAE in params2 already zero)
820 */
821 qp_param->opt_param_mask |= cpu_to_be32(MTHCA_QP_OPTPAR_RWE |
822 MTHCA_QP_OPTPAR_RRE |
823 MTHCA_QP_OPTPAR_RAE);
824 }
825
Roland Dreier34a4a752005-06-27 14:36:41 -0700826 if (!qp->resp_depth && attr->max_dest_rd_atomic) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 /*
828 * Increasing our responder resources from
829 * zero. Turn on RDMA/atomics as appropriate.
830 */
831 qp_context->params2 |=
832 cpu_to_be32(qp->atomic_rd_en & IB_ACCESS_REMOTE_WRITE ?
833 MTHCA_QP_BIT_RWE : 0);
834 qp_context->params2 |=
835 cpu_to_be32(qp->atomic_rd_en & IB_ACCESS_REMOTE_READ ?
836 MTHCA_QP_BIT_RRE : 0);
837 qp_context->params2 |=
838 cpu_to_be32(qp->atomic_rd_en & IB_ACCESS_REMOTE_ATOMIC ?
839 MTHCA_QP_BIT_RAE : 0);
840
841 qp_param->opt_param_mask |= cpu_to_be32(MTHCA_QP_OPTPAR_RWE |
842 MTHCA_QP_OPTPAR_RRE |
843 MTHCA_QP_OPTPAR_RAE);
844 }
845
846 for (rra_max = 0;
Roland Dreier34a4a752005-06-27 14:36:41 -0700847 1 << rra_max < attr->max_dest_rd_atomic &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 rra_max < dev->qp_table.rdb_shift;
849 ++rra_max)
850 ; /* nothing */
851
852 qp_context->params2 |= cpu_to_be32(rra_max << 21);
853 qp_param->opt_param_mask |= cpu_to_be32(MTHCA_QP_OPTPAR_RRA_MAX);
854
Roland Dreier34a4a752005-06-27 14:36:41 -0700855 qp->resp_depth = attr->max_dest_rd_atomic;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 }
857
858 qp_context->params2 |= cpu_to_be32(MTHCA_QP_BIT_RSC);
859
860 if (attr_mask & IB_QP_MIN_RNR_TIMER) {
861 qp_context->rnr_nextrecvpsn |= cpu_to_be32(attr->min_rnr_timer << 24);
862 qp_param->opt_param_mask |= cpu_to_be32(MTHCA_QP_OPTPAR_RNR_TIMEOUT);
863 }
864 if (attr_mask & IB_QP_RQ_PSN)
865 qp_context->rnr_nextrecvpsn |= cpu_to_be32(attr->rq_psn);
866
867 qp_context->ra_buff_indx =
868 cpu_to_be32(dev->qp_table.rdb_base +
869 ((qp->qpn & (dev->limits.num_qps - 1)) * MTHCA_RDB_ENTRY_SIZE <<
870 dev->qp_table.rdb_shift));
871
872 qp_context->cqn_rcv = cpu_to_be32(to_mcq(ibqp->recv_cq)->cqn);
873
Roland Dreierd10ddbf2005-04-16 15:26:32 -0700874 if (mthca_is_memfree(dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 qp_context->rcv_db_index = cpu_to_be32(qp->rq.db_index);
876
877 if (attr_mask & IB_QP_QKEY) {
878 qp_context->qkey = cpu_to_be32(attr->qkey);
879 qp_param->opt_param_mask |= cpu_to_be32(MTHCA_QP_OPTPAR_Q_KEY);
880 }
881
882 err = mthca_MODIFY_QP(dev, state_table[cur_state][new_state].trans,
Roland Dreiered878452005-06-27 14:36:45 -0700883 qp->qpn, 0, mailbox, 0, &status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 if (status) {
885 mthca_warn(dev, "modify QP %d returned status %02x.\n",
886 state_table[cur_state][new_state].trans, status);
887 err = -EINVAL;
888 }
889
890 if (!err)
891 qp->state = new_state;
892
Roland Dreiered878452005-06-27 14:36:45 -0700893 mthca_free_mailbox(dev, mailbox);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894
895 if (is_sqp(dev, qp))
896 store_attrs(to_msqp(qp), attr, attr_mask);
897
898 /*
899 * If we are moving QP0 to RTR, bring the IB link up; if we
900 * are moving QP0 to RESET or ERROR, bring the link back down.
901 */
902 if (is_qp0(dev, qp)) {
903 if (cur_state != IB_QPS_RTR &&
904 new_state == IB_QPS_RTR)
905 init_port(dev, to_msqp(qp)->port);
906
907 if (cur_state != IB_QPS_RESET &&
908 cur_state != IB_QPS_ERR &&
909 (new_state == IB_QPS_RESET ||
910 new_state == IB_QPS_ERR))
911 mthca_CLOSE_IB(dev, to_msqp(qp)->port, &status);
912 }
913
914 return err;
915}
916
917/*
918 * Allocate and register buffer for WQEs. qp->rq.max, sq.max,
919 * rq.max_gs and sq.max_gs must all be assigned.
920 * mthca_alloc_wqe_buf will calculate rq.wqe_shift and
921 * sq.wqe_shift (as well as send_wqe_offset, is_direct, and
922 * queue)
923 */
924static int mthca_alloc_wqe_buf(struct mthca_dev *dev,
925 struct mthca_pd *pd,
926 struct mthca_qp *qp)
927{
928 int size;
929 int i;
930 int npages, shift;
931 dma_addr_t t;
932 u64 *dma_list = NULL;
933 int err = -ENOMEM;
934
935 size = sizeof (struct mthca_next_seg) +
936 qp->rq.max_gs * sizeof (struct mthca_data_seg);
937
938 for (qp->rq.wqe_shift = 6; 1 << qp->rq.wqe_shift < size;
939 qp->rq.wqe_shift++)
940 ; /* nothing */
941
942 size = sizeof (struct mthca_next_seg) +
943 qp->sq.max_gs * sizeof (struct mthca_data_seg);
944 switch (qp->transport) {
945 case MLX:
946 size += 2 * sizeof (struct mthca_data_seg);
947 break;
948 case UD:
Roland Dreierd10ddbf2005-04-16 15:26:32 -0700949 if (mthca_is_memfree(dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 size += sizeof (struct mthca_arbel_ud_seg);
951 else
952 size += sizeof (struct mthca_tavor_ud_seg);
953 break;
954 default:
955 /* bind seg is as big as atomic + raddr segs */
956 size += sizeof (struct mthca_bind_seg);
957 }
958
959 for (qp->sq.wqe_shift = 6; 1 << qp->sq.wqe_shift < size;
960 qp->sq.wqe_shift++)
961 ; /* nothing */
962
963 qp->send_wqe_offset = ALIGN(qp->rq.max << qp->rq.wqe_shift,
964 1 << qp->sq.wqe_shift);
Roland Dreier80c8ec22005-07-07 17:57:20 -0700965
966 /*
967 * If this is a userspace QP, we don't actually have to
968 * allocate anything. All we need is to calculate the WQE
969 * sizes and the send_wqe_offset, so we're done now.
970 */
971 if (pd->ibpd.uobject)
972 return 0;
973
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 size = PAGE_ALIGN(qp->send_wqe_offset +
975 (qp->sq.max << qp->sq.wqe_shift));
976
977 qp->wrid = kmalloc((qp->rq.max + qp->sq.max) * sizeof (u64),
978 GFP_KERNEL);
979 if (!qp->wrid)
980 goto err_out;
981
982 if (size <= MTHCA_MAX_DIRECT_QP_SIZE) {
983 qp->is_direct = 1;
984 npages = 1;
985 shift = get_order(size) + PAGE_SHIFT;
986
987 if (0)
988 mthca_dbg(dev, "Creating direct QP of size %d (shift %d)\n",
989 size, shift);
990
Roland Dreier64dc81f2005-06-27 14:36:40 -0700991 qp->queue.direct.buf = dma_alloc_coherent(&dev->pdev->dev, size,
992 &t, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 if (!qp->queue.direct.buf)
994 goto err_out;
995
996 pci_unmap_addr_set(&qp->queue.direct, mapping, t);
997
998 memset(qp->queue.direct.buf, 0, size);
999
1000 while (t & ((1 << shift) - 1)) {
1001 --shift;
1002 npages *= 2;
1003 }
1004
1005 dma_list = kmalloc(npages * sizeof *dma_list, GFP_KERNEL);
1006 if (!dma_list)
1007 goto err_out_free;
1008
1009 for (i = 0; i < npages; ++i)
1010 dma_list[i] = t + i * (1 << shift);
1011 } else {
1012 qp->is_direct = 0;
1013 npages = size / PAGE_SIZE;
1014 shift = PAGE_SHIFT;
1015
1016 if (0)
1017 mthca_dbg(dev, "Creating indirect QP with %d pages\n", npages);
1018
1019 dma_list = kmalloc(npages * sizeof *dma_list, GFP_KERNEL);
1020 if (!dma_list)
1021 goto err_out;
1022
1023 qp->queue.page_list = kmalloc(npages *
1024 sizeof *qp->queue.page_list,
1025 GFP_KERNEL);
1026 if (!qp->queue.page_list)
1027 goto err_out;
1028
1029 for (i = 0; i < npages; ++i) {
1030 qp->queue.page_list[i].buf =
Roland Dreier64dc81f2005-06-27 14:36:40 -07001031 dma_alloc_coherent(&dev->pdev->dev, PAGE_SIZE,
1032 &t, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 if (!qp->queue.page_list[i].buf)
1034 goto err_out_free;
1035
1036 memset(qp->queue.page_list[i].buf, 0, PAGE_SIZE);
1037
1038 pci_unmap_addr_set(&qp->queue.page_list[i], mapping, t);
1039 dma_list[i] = t;
1040 }
1041 }
1042
1043 err = mthca_mr_alloc_phys(dev, pd->pd_num, dma_list, shift,
1044 npages, 0, size,
1045 MTHCA_MPT_FLAG_LOCAL_READ,
1046 &qp->mr);
1047 if (err)
1048 goto err_out_free;
1049
1050 kfree(dma_list);
1051 return 0;
1052
1053 err_out_free:
1054 if (qp->is_direct) {
Roland Dreier64dc81f2005-06-27 14:36:40 -07001055 dma_free_coherent(&dev->pdev->dev, size, qp->queue.direct.buf,
1056 pci_unmap_addr(&qp->queue.direct, mapping));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 } else
1058 for (i = 0; i < npages; ++i) {
1059 if (qp->queue.page_list[i].buf)
Roland Dreier64dc81f2005-06-27 14:36:40 -07001060 dma_free_coherent(&dev->pdev->dev, PAGE_SIZE,
1061 qp->queue.page_list[i].buf,
1062 pci_unmap_addr(&qp->queue.page_list[i],
1063 mapping));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064
1065 }
1066
1067 err_out:
1068 kfree(qp->wrid);
1069 kfree(dma_list);
1070 return err;
1071}
1072
Roland Dreier80c8ec22005-07-07 17:57:20 -07001073static void mthca_free_wqe_buf(struct mthca_dev *dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 struct mthca_qp *qp)
1075{
Roland Dreier80c8ec22005-07-07 17:57:20 -07001076 int i;
1077 int size = PAGE_ALIGN(qp->send_wqe_offset +
1078 (qp->sq.max << qp->sq.wqe_shift));
1079
1080 if (qp->is_direct) {
1081 dma_free_coherent(&dev->pdev->dev, size, qp->queue.direct.buf,
1082 pci_unmap_addr(&qp->queue.direct, mapping));
1083 } else {
1084 for (i = 0; i < size / PAGE_SIZE; ++i) {
1085 dma_free_coherent(&dev->pdev->dev, PAGE_SIZE,
1086 qp->queue.page_list[i].buf,
1087 pci_unmap_addr(&qp->queue.page_list[i],
1088 mapping));
1089 }
1090 }
1091
1092 kfree(qp->wrid);
1093}
1094
1095static int mthca_map_memfree(struct mthca_dev *dev,
1096 struct mthca_qp *qp)
1097{
1098 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099
Roland Dreierd10ddbf2005-04-16 15:26:32 -07001100 if (mthca_is_memfree(dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 ret = mthca_table_get(dev, dev->qp_table.qp_table, qp->qpn);
1102 if (ret)
1103 return ret;
1104
1105 ret = mthca_table_get(dev, dev->qp_table.eqp_table, qp->qpn);
1106 if (ret)
1107 goto err_qpc;
1108
Roland Dreier80c8ec22005-07-07 17:57:20 -07001109 ret = mthca_table_get(dev, dev->qp_table.rdb_table,
1110 qp->qpn << dev->qp_table.rdb_shift);
1111 if (ret)
1112 goto err_eqpc;
Roland Dreier08aeb142005-04-16 15:26:34 -07001113
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 }
1115
1116 return 0;
1117
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118err_eqpc:
1119 mthca_table_put(dev, dev->qp_table.eqp_table, qp->qpn);
1120
1121err_qpc:
1122 mthca_table_put(dev, dev->qp_table.qp_table, qp->qpn);
1123
1124 return ret;
1125}
1126
Roland Dreier80c8ec22005-07-07 17:57:20 -07001127static void mthca_unmap_memfree(struct mthca_dev *dev,
1128 struct mthca_qp *qp)
1129{
1130 mthca_table_put(dev, dev->qp_table.rdb_table,
1131 qp->qpn << dev->qp_table.rdb_shift);
1132 mthca_table_put(dev, dev->qp_table.eqp_table, qp->qpn);
1133 mthca_table_put(dev, dev->qp_table.qp_table, qp->qpn);
1134}
1135
1136static int mthca_alloc_memfree(struct mthca_dev *dev,
1137 struct mthca_qp *qp)
1138{
1139 int ret = 0;
1140
1141 if (mthca_is_memfree(dev)) {
1142 qp->rq.db_index = mthca_alloc_db(dev, MTHCA_DB_TYPE_RQ,
1143 qp->qpn, &qp->rq.db);
1144 if (qp->rq.db_index < 0)
1145 return ret;
1146
1147 qp->sq.db_index = mthca_alloc_db(dev, MTHCA_DB_TYPE_SQ,
1148 qp->qpn, &qp->sq.db);
1149 if (qp->sq.db_index < 0)
1150 mthca_free_db(dev, MTHCA_DB_TYPE_RQ, qp->rq.db_index);
1151 }
1152
1153 return ret;
1154}
1155
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156static void mthca_free_memfree(struct mthca_dev *dev,
1157 struct mthca_qp *qp)
1158{
Roland Dreierd10ddbf2005-04-16 15:26:32 -07001159 if (mthca_is_memfree(dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 mthca_free_db(dev, MTHCA_DB_TYPE_SQ, qp->sq.db_index);
1161 mthca_free_db(dev, MTHCA_DB_TYPE_RQ, qp->rq.db_index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 }
1163}
1164
1165static void mthca_wq_init(struct mthca_wq* wq)
1166{
1167 spin_lock_init(&wq->lock);
1168 wq->next_ind = 0;
1169 wq->last_comp = wq->max - 1;
1170 wq->head = 0;
1171 wq->tail = 0;
1172 wq->last = NULL;
1173}
1174
1175static int mthca_alloc_qp_common(struct mthca_dev *dev,
1176 struct mthca_pd *pd,
1177 struct mthca_cq *send_cq,
1178 struct mthca_cq *recv_cq,
1179 enum ib_sig_type send_policy,
1180 struct mthca_qp *qp)
1181{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 int ret;
1183 int i;
1184
1185 atomic_set(&qp->refcount, 1);
1186 qp->state = IB_QPS_RESET;
1187 qp->atomic_rd_en = 0;
1188 qp->resp_depth = 0;
1189 qp->sq_policy = send_policy;
1190 mthca_wq_init(&qp->sq);
1191 mthca_wq_init(&qp->rq);
1192
Roland Dreier80c8ec22005-07-07 17:57:20 -07001193 ret = mthca_map_memfree(dev, qp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 if (ret)
1195 return ret;
1196
1197 ret = mthca_alloc_wqe_buf(dev, pd, qp);
1198 if (ret) {
Roland Dreier80c8ec22005-07-07 17:57:20 -07001199 mthca_unmap_memfree(dev, qp);
1200 return ret;
1201 }
1202
1203 /*
1204 * If this is a userspace QP, we're done now. The doorbells
1205 * will be allocated and buffers will be initialized in
1206 * userspace.
1207 */
1208 if (pd->ibpd.uobject)
1209 return 0;
1210
1211 ret = mthca_alloc_memfree(dev, qp);
1212 if (ret) {
1213 mthca_free_wqe_buf(dev, qp);
1214 mthca_unmap_memfree(dev, qp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 return ret;
1216 }
1217
Roland Dreierd10ddbf2005-04-16 15:26:32 -07001218 if (mthca_is_memfree(dev)) {
Roland Dreierddf841f2005-04-16 15:26:33 -07001219 struct mthca_next_seg *next;
1220 struct mthca_data_seg *scatter;
1221 int size = (sizeof (struct mthca_next_seg) +
1222 qp->rq.max_gs * sizeof (struct mthca_data_seg)) / 16;
1223
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 for (i = 0; i < qp->rq.max; ++i) {
Roland Dreierddf841f2005-04-16 15:26:33 -07001225 next = get_recv_wqe(qp, i);
1226 next->nda_op = cpu_to_be32(((i + 1) & (qp->rq.max - 1)) <<
1227 qp->rq.wqe_shift);
1228 next->ee_nds = cpu_to_be32(size);
1229
1230 for (scatter = (void *) (next + 1);
1231 (void *) scatter < (void *) next + (1 << qp->rq.wqe_shift);
1232 ++scatter)
1233 scatter->lkey = cpu_to_be32(MTHCA_INVAL_LKEY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 }
1235
1236 for (i = 0; i < qp->sq.max; ++i) {
Roland Dreierddf841f2005-04-16 15:26:33 -07001237 next = get_send_wqe(qp, i);
1238 next->nda_op = cpu_to_be32((((i + 1) & (qp->sq.max - 1)) <<
1239 qp->sq.wqe_shift) +
1240 qp->send_wqe_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 }
1242 }
1243
1244 return 0;
1245}
1246
Roland Dreier80c8ec22005-07-07 17:57:20 -07001247static int mthca_set_qp_size(struct mthca_dev *dev, struct ib_qp_cap *cap,
1248 struct mthca_qp *qp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249{
Roland Dreier80c8ec22005-07-07 17:57:20 -07001250 /* Sanity check QP size before proceeding */
1251 if (cap->max_send_wr > 65536 || cap->max_recv_wr > 65536 ||
1252 cap->max_send_sge > 64 || cap->max_recv_sge > 64)
1253 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254
Roland Dreier80c8ec22005-07-07 17:57:20 -07001255 if (mthca_is_memfree(dev)) {
1256 qp->rq.max = cap->max_recv_wr ?
1257 roundup_pow_of_two(cap->max_recv_wr) : 0;
1258 qp->sq.max = cap->max_send_wr ?
1259 roundup_pow_of_two(cap->max_send_wr) : 0;
1260 } else {
1261 qp->rq.max = cap->max_recv_wr;
1262 qp->sq.max = cap->max_send_wr;
1263 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264
Roland Dreier80c8ec22005-07-07 17:57:20 -07001265 qp->rq.max_gs = cap->max_recv_sge;
1266 qp->sq.max_gs = max_t(int, cap->max_send_sge,
1267 ALIGN(cap->max_inline_data + MTHCA_INLINE_HEADER_SIZE,
1268 MTHCA_INLINE_CHUNK_SIZE) /
1269 sizeof (struct mthca_data_seg));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270
Roland Dreier80c8ec22005-07-07 17:57:20 -07001271 /*
1272 * For MLX transport we need 2 extra S/G entries:
1273 * one for the header and one for the checksum at the end
1274 */
1275 if ((qp->transport == MLX && qp->sq.max_gs + 2 > dev->limits.max_sg) ||
1276 qp->sq.max_gs > dev->limits.max_sg || qp->rq.max_gs > dev->limits.max_sg)
1277 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278
Roland Dreier80c8ec22005-07-07 17:57:20 -07001279 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280}
1281
1282int mthca_alloc_qp(struct mthca_dev *dev,
1283 struct mthca_pd *pd,
1284 struct mthca_cq *send_cq,
1285 struct mthca_cq *recv_cq,
1286 enum ib_qp_type type,
1287 enum ib_sig_type send_policy,
Roland Dreier80c8ec22005-07-07 17:57:20 -07001288 struct ib_qp_cap *cap,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 struct mthca_qp *qp)
1290{
1291 int err;
1292
Roland Dreier80c8ec22005-07-07 17:57:20 -07001293 err = mthca_set_qp_size(dev, cap, qp);
1294 if (err)
1295 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296
1297 switch (type) {
1298 case IB_QPT_RC: qp->transport = RC; break;
1299 case IB_QPT_UC: qp->transport = UC; break;
1300 case IB_QPT_UD: qp->transport = UD; break;
1301 default: return -EINVAL;
1302 }
1303
1304 qp->qpn = mthca_alloc(&dev->qp_table.alloc);
1305 if (qp->qpn == -1)
1306 return -ENOMEM;
1307
1308 err = mthca_alloc_qp_common(dev, pd, send_cq, recv_cq,
1309 send_policy, qp);
1310 if (err) {
1311 mthca_free(&dev->qp_table.alloc, qp->qpn);
1312 return err;
1313 }
1314
1315 spin_lock_irq(&dev->qp_table.lock);
1316 mthca_array_set(&dev->qp_table.qp,
1317 qp->qpn & (dev->limits.num_qps - 1), qp);
1318 spin_unlock_irq(&dev->qp_table.lock);
1319
1320 return 0;
1321}
1322
1323int mthca_alloc_sqp(struct mthca_dev *dev,
1324 struct mthca_pd *pd,
1325 struct mthca_cq *send_cq,
1326 struct mthca_cq *recv_cq,
1327 enum ib_sig_type send_policy,
Roland Dreier80c8ec22005-07-07 17:57:20 -07001328 struct ib_qp_cap *cap,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 int qpn,
1330 int port,
1331 struct mthca_sqp *sqp)
1332{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 u32 mqpn = qpn * 2 + dev->qp_table.sqp_start + port - 1;
Roland Dreier80c8ec22005-07-07 17:57:20 -07001334 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335
Roland Dreier80c8ec22005-07-07 17:57:20 -07001336 err = mthca_set_qp_size(dev, cap, &sqp->qp);
1337 if (err)
1338 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339
1340 sqp->header_buf_size = sqp->qp.sq.max * MTHCA_UD_HEADER_SIZE;
1341 sqp->header_buf = dma_alloc_coherent(&dev->pdev->dev, sqp->header_buf_size,
1342 &sqp->header_dma, GFP_KERNEL);
1343 if (!sqp->header_buf)
1344 return -ENOMEM;
1345
1346 spin_lock_irq(&dev->qp_table.lock);
1347 if (mthca_array_get(&dev->qp_table.qp, mqpn))
1348 err = -EBUSY;
1349 else
1350 mthca_array_set(&dev->qp_table.qp, mqpn, sqp);
1351 spin_unlock_irq(&dev->qp_table.lock);
1352
1353 if (err)
1354 goto err_out;
1355
1356 sqp->port = port;
1357 sqp->qp.qpn = mqpn;
1358 sqp->qp.transport = MLX;
1359
1360 err = mthca_alloc_qp_common(dev, pd, send_cq, recv_cq,
1361 send_policy, &sqp->qp);
1362 if (err)
1363 goto err_out_free;
1364
1365 atomic_inc(&pd->sqp_count);
1366
1367 return 0;
1368
1369 err_out_free:
1370 /*
1371 * Lock CQs here, so that CQ polling code can do QP lookup
1372 * without taking a lock.
1373 */
1374 spin_lock_irq(&send_cq->lock);
1375 if (send_cq != recv_cq)
1376 spin_lock(&recv_cq->lock);
1377
1378 spin_lock(&dev->qp_table.lock);
1379 mthca_array_clear(&dev->qp_table.qp, mqpn);
1380 spin_unlock(&dev->qp_table.lock);
1381
1382 if (send_cq != recv_cq)
1383 spin_unlock(&recv_cq->lock);
1384 spin_unlock_irq(&send_cq->lock);
1385
1386 err_out:
1387 dma_free_coherent(&dev->pdev->dev, sqp->header_buf_size,
1388 sqp->header_buf, sqp->header_dma);
1389
1390 return err;
1391}
1392
1393void mthca_free_qp(struct mthca_dev *dev,
1394 struct mthca_qp *qp)
1395{
1396 u8 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 struct mthca_cq *send_cq;
1398 struct mthca_cq *recv_cq;
1399
1400 send_cq = to_mcq(qp->ibqp.send_cq);
1401 recv_cq = to_mcq(qp->ibqp.recv_cq);
1402
1403 /*
1404 * Lock CQs here, so that CQ polling code can do QP lookup
1405 * without taking a lock.
1406 */
1407 spin_lock_irq(&send_cq->lock);
1408 if (send_cq != recv_cq)
1409 spin_lock(&recv_cq->lock);
1410
1411 spin_lock(&dev->qp_table.lock);
1412 mthca_array_clear(&dev->qp_table.qp,
1413 qp->qpn & (dev->limits.num_qps - 1));
1414 spin_unlock(&dev->qp_table.lock);
1415
1416 if (send_cq != recv_cq)
1417 spin_unlock(&recv_cq->lock);
1418 spin_unlock_irq(&send_cq->lock);
1419
1420 atomic_dec(&qp->refcount);
1421 wait_event(qp->wait, !atomic_read(&qp->refcount));
1422
1423 if (qp->state != IB_QPS_RESET)
1424 mthca_MODIFY_QP(dev, MTHCA_TRANS_ANY2RST, qp->qpn, 0, NULL, 0, &status);
1425
Roland Dreier80c8ec22005-07-07 17:57:20 -07001426 /*
1427 * If this is a userspace QP, the buffers, MR, CQs and so on
1428 * will be cleaned up in userspace, so all we have to do is
1429 * unref the mem-free tables and free the QPN in our table.
1430 */
1431 if (!qp->ibqp.uobject) {
1432 mthca_cq_clean(dev, to_mcq(qp->ibqp.send_cq)->cqn, qp->qpn);
1433 if (qp->ibqp.send_cq != qp->ibqp.recv_cq)
1434 mthca_cq_clean(dev, to_mcq(qp->ibqp.recv_cq)->cqn, qp->qpn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435
Roland Dreier80c8ec22005-07-07 17:57:20 -07001436 mthca_free_mr(dev, &qp->mr);
1437 mthca_free_memfree(dev, qp);
1438 mthca_free_wqe_buf(dev, qp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 }
1440
Roland Dreier80c8ec22005-07-07 17:57:20 -07001441 mthca_unmap_memfree(dev, qp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442
1443 if (is_sqp(dev, qp)) {
1444 atomic_dec(&(to_mpd(qp->ibqp.pd)->sqp_count));
1445 dma_free_coherent(&dev->pdev->dev,
1446 to_msqp(qp)->header_buf_size,
1447 to_msqp(qp)->header_buf,
1448 to_msqp(qp)->header_dma);
1449 } else
1450 mthca_free(&dev->qp_table.alloc, qp->qpn);
1451}
1452
1453/* Create UD header for an MLX send and build a data segment for it */
1454static int build_mlx_header(struct mthca_dev *dev, struct mthca_sqp *sqp,
1455 int ind, struct ib_send_wr *wr,
1456 struct mthca_mlx_seg *mlx,
1457 struct mthca_data_seg *data)
1458{
1459 int header_size;
1460 int err;
Sean Hefty97f52eb2005-08-13 21:05:57 -07001461 u16 pkey;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462
1463 ib_ud_header_init(256, /* assume a MAD */
1464 sqp->ud_header.grh_present,
1465 &sqp->ud_header);
1466
1467 err = mthca_read_ah(dev, to_mah(wr->wr.ud.ah), &sqp->ud_header);
1468 if (err)
1469 return err;
1470 mlx->flags &= ~cpu_to_be32(MTHCA_NEXT_SOLICIT | 1);
1471 mlx->flags |= cpu_to_be32((!sqp->qp.ibqp.qp_num ? MTHCA_MLX_VL15 : 0) |
Sean Hefty97f52eb2005-08-13 21:05:57 -07001472 (sqp->ud_header.lrh.destination_lid ==
1473 IB_LID_PERMISSIVE ? MTHCA_MLX_SLR : 0) |
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 (sqp->ud_header.lrh.service_level << 8));
1475 mlx->rlid = sqp->ud_header.lrh.destination_lid;
1476 mlx->vcrc = 0;
1477
1478 switch (wr->opcode) {
1479 case IB_WR_SEND:
1480 sqp->ud_header.bth.opcode = IB_OPCODE_UD_SEND_ONLY;
1481 sqp->ud_header.immediate_present = 0;
1482 break;
1483 case IB_WR_SEND_WITH_IMM:
1484 sqp->ud_header.bth.opcode = IB_OPCODE_UD_SEND_ONLY_WITH_IMMEDIATE;
1485 sqp->ud_header.immediate_present = 1;
1486 sqp->ud_header.immediate_data = wr->imm_data;
1487 break;
1488 default:
1489 return -EINVAL;
1490 }
1491
1492 sqp->ud_header.lrh.virtual_lane = !sqp->qp.ibqp.qp_num ? 15 : 0;
Sean Hefty97f52eb2005-08-13 21:05:57 -07001493 if (sqp->ud_header.lrh.destination_lid == IB_LID_PERMISSIVE)
1494 sqp->ud_header.lrh.source_lid = IB_LID_PERMISSIVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 sqp->ud_header.bth.solicited_event = !!(wr->send_flags & IB_SEND_SOLICITED);
1496 if (!sqp->qp.ibqp.qp_num)
1497 ib_get_cached_pkey(&dev->ib_dev, sqp->port,
Sean Hefty97f52eb2005-08-13 21:05:57 -07001498 sqp->pkey_index, &pkey);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 else
1500 ib_get_cached_pkey(&dev->ib_dev, sqp->port,
Sean Hefty97f52eb2005-08-13 21:05:57 -07001501 wr->wr.ud.pkey_index, &pkey);
1502 sqp->ud_header.bth.pkey = cpu_to_be16(pkey);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 sqp->ud_header.bth.destination_qpn = cpu_to_be32(wr->wr.ud.remote_qpn);
1504 sqp->ud_header.bth.psn = cpu_to_be32((sqp->send_psn++) & ((1 << 24) - 1));
1505 sqp->ud_header.deth.qkey = cpu_to_be32(wr->wr.ud.remote_qkey & 0x80000000 ?
1506 sqp->qkey : wr->wr.ud.remote_qkey);
1507 sqp->ud_header.deth.source_qpn = cpu_to_be32(sqp->qp.ibqp.qp_num);
1508
1509 header_size = ib_ud_header_pack(&sqp->ud_header,
1510 sqp->header_buf +
1511 ind * MTHCA_UD_HEADER_SIZE);
1512
1513 data->byte_count = cpu_to_be32(header_size);
1514 data->lkey = cpu_to_be32(to_mpd(sqp->qp.ibqp.pd)->ntmr.ibmr.lkey);
1515 data->addr = cpu_to_be64(sqp->header_dma +
1516 ind * MTHCA_UD_HEADER_SIZE);
1517
1518 return 0;
1519}
1520
1521static inline int mthca_wq_overflow(struct mthca_wq *wq, int nreq,
1522 struct ib_cq *ib_cq)
1523{
1524 unsigned cur;
1525 struct mthca_cq *cq;
1526
1527 cur = wq->head - wq->tail;
1528 if (likely(cur + nreq < wq->max))
1529 return 0;
1530
1531 cq = to_mcq(ib_cq);
1532 spin_lock(&cq->lock);
1533 cur = wq->head - wq->tail;
1534 spin_unlock(&cq->lock);
1535
1536 return cur + nreq >= wq->max;
1537}
1538
1539int mthca_tavor_post_send(struct ib_qp *ibqp, struct ib_send_wr *wr,
1540 struct ib_send_wr **bad_wr)
1541{
1542 struct mthca_dev *dev = to_mdev(ibqp->device);
1543 struct mthca_qp *qp = to_mqp(ibqp);
1544 void *wqe;
1545 void *prev_wqe;
1546 unsigned long flags;
1547 int err = 0;
1548 int nreq;
1549 int i;
1550 int size;
1551 int size0 = 0;
1552 u32 f0 = 0;
1553 int ind;
1554 u8 op0 = 0;
1555
1556 spin_lock_irqsave(&qp->sq.lock, flags);
1557
1558 /* XXX check that state is OK to post send */
1559
1560 ind = qp->sq.next_ind;
1561
1562 for (nreq = 0; wr; ++nreq, wr = wr->next) {
1563 if (mthca_wq_overflow(&qp->sq, nreq, qp->ibqp.send_cq)) {
1564 mthca_err(dev, "SQ %06x full (%u head, %u tail,"
1565 " %d max, %d nreq)\n", qp->qpn,
1566 qp->sq.head, qp->sq.tail,
1567 qp->sq.max, nreq);
1568 err = -ENOMEM;
1569 *bad_wr = wr;
1570 goto out;
1571 }
1572
1573 wqe = get_send_wqe(qp, ind);
1574 prev_wqe = qp->sq.last;
1575 qp->sq.last = wqe;
1576
1577 ((struct mthca_next_seg *) wqe)->nda_op = 0;
1578 ((struct mthca_next_seg *) wqe)->ee_nds = 0;
1579 ((struct mthca_next_seg *) wqe)->flags =
1580 ((wr->send_flags & IB_SEND_SIGNALED) ?
1581 cpu_to_be32(MTHCA_NEXT_CQ_UPDATE) : 0) |
1582 ((wr->send_flags & IB_SEND_SOLICITED) ?
1583 cpu_to_be32(MTHCA_NEXT_SOLICIT) : 0) |
1584 cpu_to_be32(1);
1585 if (wr->opcode == IB_WR_SEND_WITH_IMM ||
1586 wr->opcode == IB_WR_RDMA_WRITE_WITH_IMM)
Roland Dreier3fba2312005-04-16 15:26:16 -07001587 ((struct mthca_next_seg *) wqe)->imm = wr->imm_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588
1589 wqe += sizeof (struct mthca_next_seg);
1590 size = sizeof (struct mthca_next_seg) / 16;
1591
1592 switch (qp->transport) {
1593 case RC:
1594 switch (wr->opcode) {
1595 case IB_WR_ATOMIC_CMP_AND_SWP:
1596 case IB_WR_ATOMIC_FETCH_AND_ADD:
1597 ((struct mthca_raddr_seg *) wqe)->raddr =
1598 cpu_to_be64(wr->wr.atomic.remote_addr);
1599 ((struct mthca_raddr_seg *) wqe)->rkey =
1600 cpu_to_be32(wr->wr.atomic.rkey);
1601 ((struct mthca_raddr_seg *) wqe)->reserved = 0;
1602
1603 wqe += sizeof (struct mthca_raddr_seg);
1604
1605 if (wr->opcode == IB_WR_ATOMIC_CMP_AND_SWP) {
1606 ((struct mthca_atomic_seg *) wqe)->swap_add =
1607 cpu_to_be64(wr->wr.atomic.swap);
1608 ((struct mthca_atomic_seg *) wqe)->compare =
1609 cpu_to_be64(wr->wr.atomic.compare_add);
1610 } else {
1611 ((struct mthca_atomic_seg *) wqe)->swap_add =
1612 cpu_to_be64(wr->wr.atomic.compare_add);
1613 ((struct mthca_atomic_seg *) wqe)->compare = 0;
1614 }
1615
1616 wqe += sizeof (struct mthca_atomic_seg);
1617 size += sizeof (struct mthca_raddr_seg) / 16 +
1618 sizeof (struct mthca_atomic_seg);
1619 break;
1620
1621 case IB_WR_RDMA_WRITE:
1622 case IB_WR_RDMA_WRITE_WITH_IMM:
1623 case IB_WR_RDMA_READ:
1624 ((struct mthca_raddr_seg *) wqe)->raddr =
1625 cpu_to_be64(wr->wr.rdma.remote_addr);
1626 ((struct mthca_raddr_seg *) wqe)->rkey =
1627 cpu_to_be32(wr->wr.rdma.rkey);
1628 ((struct mthca_raddr_seg *) wqe)->reserved = 0;
1629 wqe += sizeof (struct mthca_raddr_seg);
1630 size += sizeof (struct mthca_raddr_seg) / 16;
1631 break;
1632
1633 default:
1634 /* No extra segments required for sends */
1635 break;
1636 }
1637
1638 break;
1639
Roland Dreier9e6970b2005-06-27 14:36:42 -07001640 case UC:
1641 switch (wr->opcode) {
1642 case IB_WR_RDMA_WRITE:
1643 case IB_WR_RDMA_WRITE_WITH_IMM:
1644 ((struct mthca_raddr_seg *) wqe)->raddr =
1645 cpu_to_be64(wr->wr.rdma.remote_addr);
1646 ((struct mthca_raddr_seg *) wqe)->rkey =
1647 cpu_to_be32(wr->wr.rdma.rkey);
1648 ((struct mthca_raddr_seg *) wqe)->reserved = 0;
1649 wqe += sizeof (struct mthca_raddr_seg);
1650 size += sizeof (struct mthca_raddr_seg) / 16;
1651 break;
1652
1653 default:
1654 /* No extra segments required for sends */
1655 break;
1656 }
1657
1658 break;
1659
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 case UD:
1661 ((struct mthca_tavor_ud_seg *) wqe)->lkey =
1662 cpu_to_be32(to_mah(wr->wr.ud.ah)->key);
1663 ((struct mthca_tavor_ud_seg *) wqe)->av_addr =
1664 cpu_to_be64(to_mah(wr->wr.ud.ah)->avdma);
1665 ((struct mthca_tavor_ud_seg *) wqe)->dqpn =
1666 cpu_to_be32(wr->wr.ud.remote_qpn);
1667 ((struct mthca_tavor_ud_seg *) wqe)->qkey =
1668 cpu_to_be32(wr->wr.ud.remote_qkey);
1669
1670 wqe += sizeof (struct mthca_tavor_ud_seg);
1671 size += sizeof (struct mthca_tavor_ud_seg) / 16;
1672 break;
1673
1674 case MLX:
1675 err = build_mlx_header(dev, to_msqp(qp), ind, wr,
1676 wqe - sizeof (struct mthca_next_seg),
1677 wqe);
1678 if (err) {
1679 *bad_wr = wr;
1680 goto out;
1681 }
1682 wqe += sizeof (struct mthca_data_seg);
1683 size += sizeof (struct mthca_data_seg) / 16;
1684 break;
1685 }
1686
1687 if (wr->num_sge > qp->sq.max_gs) {
1688 mthca_err(dev, "too many gathers\n");
1689 err = -EINVAL;
1690 *bad_wr = wr;
1691 goto out;
1692 }
1693
1694 for (i = 0; i < wr->num_sge; ++i) {
1695 ((struct mthca_data_seg *) wqe)->byte_count =
1696 cpu_to_be32(wr->sg_list[i].length);
1697 ((struct mthca_data_seg *) wqe)->lkey =
1698 cpu_to_be32(wr->sg_list[i].lkey);
1699 ((struct mthca_data_seg *) wqe)->addr =
1700 cpu_to_be64(wr->sg_list[i].addr);
1701 wqe += sizeof (struct mthca_data_seg);
1702 size += sizeof (struct mthca_data_seg) / 16;
1703 }
1704
1705 /* Add one more inline data segment for ICRC */
1706 if (qp->transport == MLX) {
1707 ((struct mthca_data_seg *) wqe)->byte_count =
1708 cpu_to_be32((1 << 31) | 4);
1709 ((u32 *) wqe)[1] = 0;
1710 wqe += sizeof (struct mthca_data_seg);
1711 size += sizeof (struct mthca_data_seg) / 16;
1712 }
1713
1714 qp->wrid[ind + qp->rq.max] = wr->wr_id;
1715
1716 if (wr->opcode >= ARRAY_SIZE(mthca_opcode)) {
1717 mthca_err(dev, "opcode invalid\n");
1718 err = -EINVAL;
1719 *bad_wr = wr;
1720 goto out;
1721 }
1722
1723 if (prev_wqe) {
1724 ((struct mthca_next_seg *) prev_wqe)->nda_op =
1725 cpu_to_be32(((ind << qp->sq.wqe_shift) +
1726 qp->send_wqe_offset) |
1727 mthca_opcode[wr->opcode]);
1728 wmb();
1729 ((struct mthca_next_seg *) prev_wqe)->ee_nds =
1730 cpu_to_be32((size0 ? 0 : MTHCA_NEXT_DBD) | size);
1731 }
1732
1733 if (!size0) {
1734 size0 = size;
1735 op0 = mthca_opcode[wr->opcode];
1736 }
1737
1738 ++ind;
1739 if (unlikely(ind >= qp->sq.max))
1740 ind -= qp->sq.max;
1741 }
1742
1743out:
1744 if (likely(nreq)) {
Sean Hefty97f52eb2005-08-13 21:05:57 -07001745 __be32 doorbell[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746
1747 doorbell[0] = cpu_to_be32(((qp->sq.next_ind << qp->sq.wqe_shift) +
1748 qp->send_wqe_offset) | f0 | op0);
1749 doorbell[1] = cpu_to_be32((qp->qpn << 8) | size0);
1750
1751 wmb();
1752
1753 mthca_write64(doorbell,
1754 dev->kar + MTHCA_SEND_DOORBELL,
1755 MTHCA_GET_DOORBELL_LOCK(&dev->doorbell_lock));
1756 }
1757
1758 qp->sq.next_ind = ind;
1759 qp->sq.head += nreq;
1760
1761 spin_unlock_irqrestore(&qp->sq.lock, flags);
1762 return err;
1763}
1764
1765int mthca_tavor_post_receive(struct ib_qp *ibqp, struct ib_recv_wr *wr,
1766 struct ib_recv_wr **bad_wr)
1767{
1768 struct mthca_dev *dev = to_mdev(ibqp->device);
1769 struct mthca_qp *qp = to_mqp(ibqp);
1770 unsigned long flags;
1771 int err = 0;
1772 int nreq;
1773 int i;
1774 int size;
1775 int size0 = 0;
1776 int ind;
1777 void *wqe;
1778 void *prev_wqe;
1779
1780 spin_lock_irqsave(&qp->rq.lock, flags);
1781
1782 /* XXX check that state is OK to post receive */
1783
1784 ind = qp->rq.next_ind;
1785
1786 for (nreq = 0; wr; ++nreq, wr = wr->next) {
1787 if (mthca_wq_overflow(&qp->rq, nreq, qp->ibqp.recv_cq)) {
1788 mthca_err(dev, "RQ %06x full (%u head, %u tail,"
1789 " %d max, %d nreq)\n", qp->qpn,
1790 qp->rq.head, qp->rq.tail,
1791 qp->rq.max, nreq);
1792 err = -ENOMEM;
1793 *bad_wr = wr;
1794 goto out;
1795 }
1796
1797 wqe = get_recv_wqe(qp, ind);
1798 prev_wqe = qp->rq.last;
1799 qp->rq.last = wqe;
1800
1801 ((struct mthca_next_seg *) wqe)->nda_op = 0;
1802 ((struct mthca_next_seg *) wqe)->ee_nds =
1803 cpu_to_be32(MTHCA_NEXT_DBD);
1804 ((struct mthca_next_seg *) wqe)->flags = 0;
1805
1806 wqe += sizeof (struct mthca_next_seg);
1807 size = sizeof (struct mthca_next_seg) / 16;
1808
1809 if (unlikely(wr->num_sge > qp->rq.max_gs)) {
1810 err = -EINVAL;
1811 *bad_wr = wr;
1812 goto out;
1813 }
1814
1815 for (i = 0; i < wr->num_sge; ++i) {
1816 ((struct mthca_data_seg *) wqe)->byte_count =
1817 cpu_to_be32(wr->sg_list[i].length);
1818 ((struct mthca_data_seg *) wqe)->lkey =
1819 cpu_to_be32(wr->sg_list[i].lkey);
1820 ((struct mthca_data_seg *) wqe)->addr =
1821 cpu_to_be64(wr->sg_list[i].addr);
1822 wqe += sizeof (struct mthca_data_seg);
1823 size += sizeof (struct mthca_data_seg) / 16;
1824 }
1825
1826 qp->wrid[ind] = wr->wr_id;
1827
1828 if (likely(prev_wqe)) {
1829 ((struct mthca_next_seg *) prev_wqe)->nda_op =
1830 cpu_to_be32((ind << qp->rq.wqe_shift) | 1);
1831 wmb();
1832 ((struct mthca_next_seg *) prev_wqe)->ee_nds =
1833 cpu_to_be32(MTHCA_NEXT_DBD | size);
1834 }
1835
1836 if (!size0)
1837 size0 = size;
1838
1839 ++ind;
1840 if (unlikely(ind >= qp->rq.max))
1841 ind -= qp->rq.max;
1842 }
1843
1844out:
1845 if (likely(nreq)) {
Sean Hefty97f52eb2005-08-13 21:05:57 -07001846 __be32 doorbell[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847
1848 doorbell[0] = cpu_to_be32((qp->rq.next_ind << qp->rq.wqe_shift) | size0);
1849 doorbell[1] = cpu_to_be32((qp->qpn << 8) | nreq);
1850
1851 wmb();
1852
1853 mthca_write64(doorbell,
1854 dev->kar + MTHCA_RECEIVE_DOORBELL,
1855 MTHCA_GET_DOORBELL_LOCK(&dev->doorbell_lock));
1856 }
1857
1858 qp->rq.next_ind = ind;
1859 qp->rq.head += nreq;
1860
1861 spin_unlock_irqrestore(&qp->rq.lock, flags);
1862 return err;
1863}
1864
1865int mthca_arbel_post_send(struct ib_qp *ibqp, struct ib_send_wr *wr,
1866 struct ib_send_wr **bad_wr)
1867{
1868 struct mthca_dev *dev = to_mdev(ibqp->device);
1869 struct mthca_qp *qp = to_mqp(ibqp);
1870 void *wqe;
1871 void *prev_wqe;
1872 unsigned long flags;
1873 int err = 0;
1874 int nreq;
1875 int i;
1876 int size;
1877 int size0 = 0;
1878 u32 f0 = 0;
1879 int ind;
1880 u8 op0 = 0;
1881
1882 spin_lock_irqsave(&qp->sq.lock, flags);
1883
1884 /* XXX check that state is OK to post send */
1885
1886 ind = qp->sq.head & (qp->sq.max - 1);
1887
1888 for (nreq = 0; wr; ++nreq, wr = wr->next) {
1889 if (mthca_wq_overflow(&qp->sq, nreq, qp->ibqp.send_cq)) {
1890 mthca_err(dev, "SQ %06x full (%u head, %u tail,"
1891 " %d max, %d nreq)\n", qp->qpn,
1892 qp->sq.head, qp->sq.tail,
1893 qp->sq.max, nreq);
1894 err = -ENOMEM;
1895 *bad_wr = wr;
1896 goto out;
1897 }
1898
1899 wqe = get_send_wqe(qp, ind);
1900 prev_wqe = qp->sq.last;
1901 qp->sq.last = wqe;
1902
1903 ((struct mthca_next_seg *) wqe)->flags =
1904 ((wr->send_flags & IB_SEND_SIGNALED) ?
1905 cpu_to_be32(MTHCA_NEXT_CQ_UPDATE) : 0) |
1906 ((wr->send_flags & IB_SEND_SOLICITED) ?
1907 cpu_to_be32(MTHCA_NEXT_SOLICIT) : 0) |
1908 cpu_to_be32(1);
1909 if (wr->opcode == IB_WR_SEND_WITH_IMM ||
1910 wr->opcode == IB_WR_RDMA_WRITE_WITH_IMM)
Roland Dreier3fba2312005-04-16 15:26:16 -07001911 ((struct mthca_next_seg *) wqe)->imm = wr->imm_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912
1913 wqe += sizeof (struct mthca_next_seg);
1914 size = sizeof (struct mthca_next_seg) / 16;
1915
1916 switch (qp->transport) {
Roland Dreierddb934e2005-04-16 15:26:23 -07001917 case RC:
1918 switch (wr->opcode) {
1919 case IB_WR_ATOMIC_CMP_AND_SWP:
1920 case IB_WR_ATOMIC_FETCH_AND_ADD:
1921 ((struct mthca_raddr_seg *) wqe)->raddr =
1922 cpu_to_be64(wr->wr.atomic.remote_addr);
1923 ((struct mthca_raddr_seg *) wqe)->rkey =
1924 cpu_to_be32(wr->wr.atomic.rkey);
1925 ((struct mthca_raddr_seg *) wqe)->reserved = 0;
1926
1927 wqe += sizeof (struct mthca_raddr_seg);
1928
1929 if (wr->opcode == IB_WR_ATOMIC_CMP_AND_SWP) {
1930 ((struct mthca_atomic_seg *) wqe)->swap_add =
1931 cpu_to_be64(wr->wr.atomic.swap);
1932 ((struct mthca_atomic_seg *) wqe)->compare =
1933 cpu_to_be64(wr->wr.atomic.compare_add);
1934 } else {
1935 ((struct mthca_atomic_seg *) wqe)->swap_add =
1936 cpu_to_be64(wr->wr.atomic.compare_add);
1937 ((struct mthca_atomic_seg *) wqe)->compare = 0;
1938 }
1939
1940 wqe += sizeof (struct mthca_atomic_seg);
1941 size += sizeof (struct mthca_raddr_seg) / 16 +
1942 sizeof (struct mthca_atomic_seg);
1943 break;
1944
Roland Dreier9e6970b2005-06-27 14:36:42 -07001945 case IB_WR_RDMA_READ:
Roland Dreierddb934e2005-04-16 15:26:23 -07001946 case IB_WR_RDMA_WRITE:
1947 case IB_WR_RDMA_WRITE_WITH_IMM:
Roland Dreier9e6970b2005-06-27 14:36:42 -07001948 ((struct mthca_raddr_seg *) wqe)->raddr =
1949 cpu_to_be64(wr->wr.rdma.remote_addr);
1950 ((struct mthca_raddr_seg *) wqe)->rkey =
1951 cpu_to_be32(wr->wr.rdma.rkey);
1952 ((struct mthca_raddr_seg *) wqe)->reserved = 0;
1953 wqe += sizeof (struct mthca_raddr_seg);
1954 size += sizeof (struct mthca_raddr_seg) / 16;
1955 break;
1956
1957 default:
1958 /* No extra segments required for sends */
1959 break;
1960 }
1961
1962 break;
1963
1964 case UC:
1965 switch (wr->opcode) {
1966 case IB_WR_RDMA_WRITE:
1967 case IB_WR_RDMA_WRITE_WITH_IMM:
Roland Dreierddb934e2005-04-16 15:26:23 -07001968 ((struct mthca_raddr_seg *) wqe)->raddr =
1969 cpu_to_be64(wr->wr.rdma.remote_addr);
1970 ((struct mthca_raddr_seg *) wqe)->rkey =
1971 cpu_to_be32(wr->wr.rdma.rkey);
1972 ((struct mthca_raddr_seg *) wqe)->reserved = 0;
1973 wqe += sizeof (struct mthca_raddr_seg);
1974 size += sizeof (struct mthca_raddr_seg) / 16;
1975 break;
1976
1977 default:
1978 /* No extra segments required for sends */
1979 break;
1980 }
1981
1982 break;
1983
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984 case UD:
1985 memcpy(((struct mthca_arbel_ud_seg *) wqe)->av,
1986 to_mah(wr->wr.ud.ah)->av, MTHCA_AV_SIZE);
1987 ((struct mthca_arbel_ud_seg *) wqe)->dqpn =
1988 cpu_to_be32(wr->wr.ud.remote_qpn);
1989 ((struct mthca_arbel_ud_seg *) wqe)->qkey =
1990 cpu_to_be32(wr->wr.ud.remote_qkey);
1991
1992 wqe += sizeof (struct mthca_arbel_ud_seg);
1993 size += sizeof (struct mthca_arbel_ud_seg) / 16;
1994 break;
1995
1996 case MLX:
1997 err = build_mlx_header(dev, to_msqp(qp), ind, wr,
1998 wqe - sizeof (struct mthca_next_seg),
1999 wqe);
2000 if (err) {
2001 *bad_wr = wr;
2002 goto out;
2003 }
2004 wqe += sizeof (struct mthca_data_seg);
2005 size += sizeof (struct mthca_data_seg) / 16;
2006 break;
2007 }
2008
2009 if (wr->num_sge > qp->sq.max_gs) {
2010 mthca_err(dev, "too many gathers\n");
2011 err = -EINVAL;
2012 *bad_wr = wr;
2013 goto out;
2014 }
2015
2016 for (i = 0; i < wr->num_sge; ++i) {
2017 ((struct mthca_data_seg *) wqe)->byte_count =
2018 cpu_to_be32(wr->sg_list[i].length);
2019 ((struct mthca_data_seg *) wqe)->lkey =
2020 cpu_to_be32(wr->sg_list[i].lkey);
2021 ((struct mthca_data_seg *) wqe)->addr =
2022 cpu_to_be64(wr->sg_list[i].addr);
2023 wqe += sizeof (struct mthca_data_seg);
2024 size += sizeof (struct mthca_data_seg) / 16;
2025 }
2026
2027 /* Add one more inline data segment for ICRC */
2028 if (qp->transport == MLX) {
2029 ((struct mthca_data_seg *) wqe)->byte_count =
2030 cpu_to_be32((1 << 31) | 4);
2031 ((u32 *) wqe)[1] = 0;
2032 wqe += sizeof (struct mthca_data_seg);
2033 size += sizeof (struct mthca_data_seg) / 16;
2034 }
2035
2036 qp->wrid[ind + qp->rq.max] = wr->wr_id;
2037
2038 if (wr->opcode >= ARRAY_SIZE(mthca_opcode)) {
2039 mthca_err(dev, "opcode invalid\n");
2040 err = -EINVAL;
2041 *bad_wr = wr;
2042 goto out;
2043 }
2044
2045 if (likely(prev_wqe)) {
2046 ((struct mthca_next_seg *) prev_wqe)->nda_op =
2047 cpu_to_be32(((ind << qp->sq.wqe_shift) +
2048 qp->send_wqe_offset) |
2049 mthca_opcode[wr->opcode]);
2050 wmb();
2051 ((struct mthca_next_seg *) prev_wqe)->ee_nds =
2052 cpu_to_be32(MTHCA_NEXT_DBD | size);
2053 }
2054
2055 if (!size0) {
2056 size0 = size;
2057 op0 = mthca_opcode[wr->opcode];
2058 }
2059
2060 ++ind;
2061 if (unlikely(ind >= qp->sq.max))
2062 ind -= qp->sq.max;
2063 }
2064
2065out:
2066 if (likely(nreq)) {
Sean Hefty97f52eb2005-08-13 21:05:57 -07002067 __be32 doorbell[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068
2069 doorbell[0] = cpu_to_be32((nreq << 24) |
2070 ((qp->sq.head & 0xffff) << 8) |
2071 f0 | op0);
2072 doorbell[1] = cpu_to_be32((qp->qpn << 8) | size0);
2073
2074 qp->sq.head += nreq;
2075
2076 /*
2077 * Make sure that descriptors are written before
2078 * doorbell record.
2079 */
2080 wmb();
2081 *qp->sq.db = cpu_to_be32(qp->sq.head & 0xffff);
2082
2083 /*
2084 * Make sure doorbell record is written before we
2085 * write MMIO send doorbell.
2086 */
2087 wmb();
2088 mthca_write64(doorbell,
2089 dev->kar + MTHCA_SEND_DOORBELL,
2090 MTHCA_GET_DOORBELL_LOCK(&dev->doorbell_lock));
2091 }
2092
2093 spin_unlock_irqrestore(&qp->sq.lock, flags);
2094 return err;
2095}
2096
2097int mthca_arbel_post_receive(struct ib_qp *ibqp, struct ib_recv_wr *wr,
2098 struct ib_recv_wr **bad_wr)
2099{
2100 struct mthca_dev *dev = to_mdev(ibqp->device);
2101 struct mthca_qp *qp = to_mqp(ibqp);
2102 unsigned long flags;
2103 int err = 0;
2104 int nreq;
2105 int ind;
2106 int i;
2107 void *wqe;
2108
2109 spin_lock_irqsave(&qp->rq.lock, flags);
2110
2111 /* XXX check that state is OK to post receive */
2112
2113 ind = qp->rq.head & (qp->rq.max - 1);
2114
2115 for (nreq = 0; wr; ++nreq, wr = wr->next) {
2116 if (mthca_wq_overflow(&qp->rq, nreq, qp->ibqp.recv_cq)) {
2117 mthca_err(dev, "RQ %06x full (%u head, %u tail,"
2118 " %d max, %d nreq)\n", qp->qpn,
2119 qp->rq.head, qp->rq.tail,
2120 qp->rq.max, nreq);
2121 err = -ENOMEM;
2122 *bad_wr = wr;
2123 goto out;
2124 }
2125
2126 wqe = get_recv_wqe(qp, ind);
2127
2128 ((struct mthca_next_seg *) wqe)->flags = 0;
2129
2130 wqe += sizeof (struct mthca_next_seg);
2131
2132 if (unlikely(wr->num_sge > qp->rq.max_gs)) {
2133 err = -EINVAL;
2134 *bad_wr = wr;
2135 goto out;
2136 }
2137
2138 for (i = 0; i < wr->num_sge; ++i) {
2139 ((struct mthca_data_seg *) wqe)->byte_count =
2140 cpu_to_be32(wr->sg_list[i].length);
2141 ((struct mthca_data_seg *) wqe)->lkey =
2142 cpu_to_be32(wr->sg_list[i].lkey);
2143 ((struct mthca_data_seg *) wqe)->addr =
2144 cpu_to_be64(wr->sg_list[i].addr);
2145 wqe += sizeof (struct mthca_data_seg);
2146 }
2147
2148 if (i < qp->rq.max_gs) {
2149 ((struct mthca_data_seg *) wqe)->byte_count = 0;
Roland Dreierddf841f2005-04-16 15:26:33 -07002150 ((struct mthca_data_seg *) wqe)->lkey = cpu_to_be32(MTHCA_INVAL_LKEY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151 ((struct mthca_data_seg *) wqe)->addr = 0;
2152 }
2153
2154 qp->wrid[ind] = wr->wr_id;
2155
2156 ++ind;
2157 if (unlikely(ind >= qp->rq.max))
2158 ind -= qp->rq.max;
2159 }
2160out:
2161 if (likely(nreq)) {
2162 qp->rq.head += nreq;
2163
2164 /*
2165 * Make sure that descriptors are written before
2166 * doorbell record.
2167 */
2168 wmb();
2169 *qp->rq.db = cpu_to_be32(qp->rq.head & 0xffff);
2170 }
2171
2172 spin_unlock_irqrestore(&qp->rq.lock, flags);
2173 return err;
2174}
2175
2176int mthca_free_err_wqe(struct mthca_dev *dev, struct mthca_qp *qp, int is_send,
Sean Hefty97f52eb2005-08-13 21:05:57 -07002177 int index, int *dbd, __be32 *new_wqe)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178{
2179 struct mthca_next_seg *next;
2180
2181 if (is_send)
2182 next = get_send_wqe(qp, index);
2183 else
2184 next = get_recv_wqe(qp, index);
2185
Roland Dreierd10ddbf2005-04-16 15:26:32 -07002186 if (mthca_is_memfree(dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187 *dbd = 1;
2188 else
2189 *dbd = !!(next->ee_nds & cpu_to_be32(MTHCA_NEXT_DBD));
2190 if (next->ee_nds & cpu_to_be32(0x3f))
2191 *new_wqe = (next->nda_op & cpu_to_be32(~0x3f)) |
2192 (next->ee_nds & cpu_to_be32(0x3f));
2193 else
2194 *new_wqe = 0;
2195
2196 return 0;
2197}
2198
2199int __devinit mthca_init_qp_table(struct mthca_dev *dev)
2200{
2201 int err;
2202 u8 status;
2203 int i;
2204
2205 spin_lock_init(&dev->qp_table.lock);
2206
2207 /*
2208 * We reserve 2 extra QPs per port for the special QPs. The
2209 * special QP for port 1 has to be even, so round up.
2210 */
2211 dev->qp_table.sqp_start = (dev->limits.reserved_qps + 1) & ~1UL;
2212 err = mthca_alloc_init(&dev->qp_table.alloc,
2213 dev->limits.num_qps,
2214 (1 << 24) - 1,
2215 dev->qp_table.sqp_start +
2216 MTHCA_MAX_PORTS * 2);
2217 if (err)
2218 return err;
2219
2220 err = mthca_array_init(&dev->qp_table.qp,
2221 dev->limits.num_qps);
2222 if (err) {
2223 mthca_alloc_cleanup(&dev->qp_table.alloc);
2224 return err;
2225 }
2226
2227 for (i = 0; i < 2; ++i) {
2228 err = mthca_CONF_SPECIAL_QP(dev, i ? IB_QPT_GSI : IB_QPT_SMI,
2229 dev->qp_table.sqp_start + i * 2,
2230 &status);
2231 if (err)
2232 goto err_out;
2233 if (status) {
2234 mthca_warn(dev, "CONF_SPECIAL_QP returned "
2235 "status %02x, aborting.\n",
2236 status);
2237 err = -EINVAL;
2238 goto err_out;
2239 }
2240 }
2241 return 0;
2242
2243 err_out:
2244 for (i = 0; i < 2; ++i)
2245 mthca_CONF_SPECIAL_QP(dev, i, 0, &status);
2246
2247 mthca_array_cleanup(&dev->qp_table.qp, dev->limits.num_qps);
2248 mthca_alloc_cleanup(&dev->qp_table.alloc);
2249
2250 return err;
2251}
2252
2253void __devexit mthca_cleanup_qp_table(struct mthca_dev *dev)
2254{
2255 int i;
2256 u8 status;
2257
2258 for (i = 0; i < 2; ++i)
2259 mthca_CONF_SPECIAL_QP(dev, i, 0, &status);
2260
2261 mthca_alloc_cleanup(&dev->qp_table.alloc);
2262}