blob: b5a0bef15b7e612de702623a4cc726551a1aeb73 [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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 int err = -ENOMEM;
930
931 size = sizeof (struct mthca_next_seg) +
932 qp->rq.max_gs * sizeof (struct mthca_data_seg);
933
934 for (qp->rq.wqe_shift = 6; 1 << qp->rq.wqe_shift < size;
935 qp->rq.wqe_shift++)
936 ; /* nothing */
937
938 size = sizeof (struct mthca_next_seg) +
939 qp->sq.max_gs * sizeof (struct mthca_data_seg);
940 switch (qp->transport) {
941 case MLX:
942 size += 2 * sizeof (struct mthca_data_seg);
943 break;
944 case UD:
Roland Dreierd10ddbf2005-04-16 15:26:32 -0700945 if (mthca_is_memfree(dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 size += sizeof (struct mthca_arbel_ud_seg);
947 else
948 size += sizeof (struct mthca_tavor_ud_seg);
949 break;
950 default:
951 /* bind seg is as big as atomic + raddr segs */
952 size += sizeof (struct mthca_bind_seg);
953 }
954
955 for (qp->sq.wqe_shift = 6; 1 << qp->sq.wqe_shift < size;
956 qp->sq.wqe_shift++)
957 ; /* nothing */
958
959 qp->send_wqe_offset = ALIGN(qp->rq.max << qp->rq.wqe_shift,
960 1 << qp->sq.wqe_shift);
Roland Dreier80c8ec22005-07-07 17:57:20 -0700961
962 /*
963 * If this is a userspace QP, we don't actually have to
964 * allocate anything. All we need is to calculate the WQE
965 * sizes and the send_wqe_offset, so we're done now.
966 */
967 if (pd->ibpd.uobject)
968 return 0;
969
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 size = PAGE_ALIGN(qp->send_wqe_offset +
971 (qp->sq.max << qp->sq.wqe_shift));
972
973 qp->wrid = kmalloc((qp->rq.max + qp->sq.max) * sizeof (u64),
974 GFP_KERNEL);
975 if (!qp->wrid)
976 goto err_out;
977
Roland Dreier87b81672005-08-18 13:39:31 -0700978 err = mthca_buf_alloc(dev, size, MTHCA_MAX_DIRECT_QP_SIZE,
979 &qp->queue, &qp->is_direct, pd, 0, &qp->mr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 if (err)
Roland Dreier87b81672005-08-18 13:39:31 -0700981 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 return 0;
984
Roland Dreier87b81672005-08-18 13:39:31 -0700985err_out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 kfree(qp->wrid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 return err;
988}
989
Roland Dreier80c8ec22005-07-07 17:57:20 -0700990static void mthca_free_wqe_buf(struct mthca_dev *dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 struct mthca_qp *qp)
992{
Roland Dreier87b81672005-08-18 13:39:31 -0700993 mthca_buf_free(dev, PAGE_ALIGN(qp->send_wqe_offset +
994 (qp->sq.max << qp->sq.wqe_shift)),
995 &qp->queue, qp->is_direct, &qp->mr);
Roland Dreier80c8ec22005-07-07 17:57:20 -0700996 kfree(qp->wrid);
997}
998
999static int mthca_map_memfree(struct mthca_dev *dev,
1000 struct mthca_qp *qp)
1001{
1002 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003
Roland Dreierd10ddbf2005-04-16 15:26:32 -07001004 if (mthca_is_memfree(dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 ret = mthca_table_get(dev, dev->qp_table.qp_table, qp->qpn);
1006 if (ret)
1007 return ret;
1008
1009 ret = mthca_table_get(dev, dev->qp_table.eqp_table, qp->qpn);
1010 if (ret)
1011 goto err_qpc;
1012
Roland Dreier80c8ec22005-07-07 17:57:20 -07001013 ret = mthca_table_get(dev, dev->qp_table.rdb_table,
1014 qp->qpn << dev->qp_table.rdb_shift);
1015 if (ret)
1016 goto err_eqpc;
Roland Dreier08aeb142005-04-16 15:26:34 -07001017
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 }
1019
1020 return 0;
1021
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022err_eqpc:
1023 mthca_table_put(dev, dev->qp_table.eqp_table, qp->qpn);
1024
1025err_qpc:
1026 mthca_table_put(dev, dev->qp_table.qp_table, qp->qpn);
1027
1028 return ret;
1029}
1030
Roland Dreier80c8ec22005-07-07 17:57:20 -07001031static void mthca_unmap_memfree(struct mthca_dev *dev,
1032 struct mthca_qp *qp)
1033{
1034 mthca_table_put(dev, dev->qp_table.rdb_table,
1035 qp->qpn << dev->qp_table.rdb_shift);
1036 mthca_table_put(dev, dev->qp_table.eqp_table, qp->qpn);
1037 mthca_table_put(dev, dev->qp_table.qp_table, qp->qpn);
1038}
1039
1040static int mthca_alloc_memfree(struct mthca_dev *dev,
1041 struct mthca_qp *qp)
1042{
1043 int ret = 0;
1044
1045 if (mthca_is_memfree(dev)) {
1046 qp->rq.db_index = mthca_alloc_db(dev, MTHCA_DB_TYPE_RQ,
1047 qp->qpn, &qp->rq.db);
1048 if (qp->rq.db_index < 0)
1049 return ret;
1050
1051 qp->sq.db_index = mthca_alloc_db(dev, MTHCA_DB_TYPE_SQ,
1052 qp->qpn, &qp->sq.db);
1053 if (qp->sq.db_index < 0)
1054 mthca_free_db(dev, MTHCA_DB_TYPE_RQ, qp->rq.db_index);
1055 }
1056
1057 return ret;
1058}
1059
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060static void mthca_free_memfree(struct mthca_dev *dev,
1061 struct mthca_qp *qp)
1062{
Roland Dreierd10ddbf2005-04-16 15:26:32 -07001063 if (mthca_is_memfree(dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 mthca_free_db(dev, MTHCA_DB_TYPE_SQ, qp->sq.db_index);
1065 mthca_free_db(dev, MTHCA_DB_TYPE_RQ, qp->rq.db_index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 }
1067}
1068
1069static void mthca_wq_init(struct mthca_wq* wq)
1070{
1071 spin_lock_init(&wq->lock);
1072 wq->next_ind = 0;
1073 wq->last_comp = wq->max - 1;
1074 wq->head = 0;
1075 wq->tail = 0;
1076 wq->last = NULL;
1077}
1078
1079static int mthca_alloc_qp_common(struct mthca_dev *dev,
1080 struct mthca_pd *pd,
1081 struct mthca_cq *send_cq,
1082 struct mthca_cq *recv_cq,
1083 enum ib_sig_type send_policy,
1084 struct mthca_qp *qp)
1085{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 int ret;
1087 int i;
1088
1089 atomic_set(&qp->refcount, 1);
1090 qp->state = IB_QPS_RESET;
1091 qp->atomic_rd_en = 0;
1092 qp->resp_depth = 0;
1093 qp->sq_policy = send_policy;
1094 mthca_wq_init(&qp->sq);
1095 mthca_wq_init(&qp->rq);
1096
Roland Dreier80c8ec22005-07-07 17:57:20 -07001097 ret = mthca_map_memfree(dev, qp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 if (ret)
1099 return ret;
1100
1101 ret = mthca_alloc_wqe_buf(dev, pd, qp);
1102 if (ret) {
Roland Dreier80c8ec22005-07-07 17:57:20 -07001103 mthca_unmap_memfree(dev, qp);
1104 return ret;
1105 }
1106
1107 /*
1108 * If this is a userspace QP, we're done now. The doorbells
1109 * will be allocated and buffers will be initialized in
1110 * userspace.
1111 */
1112 if (pd->ibpd.uobject)
1113 return 0;
1114
1115 ret = mthca_alloc_memfree(dev, qp);
1116 if (ret) {
1117 mthca_free_wqe_buf(dev, qp);
1118 mthca_unmap_memfree(dev, qp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 return ret;
1120 }
1121
Roland Dreierd10ddbf2005-04-16 15:26:32 -07001122 if (mthca_is_memfree(dev)) {
Roland Dreierddf841f2005-04-16 15:26:33 -07001123 struct mthca_next_seg *next;
1124 struct mthca_data_seg *scatter;
1125 int size = (sizeof (struct mthca_next_seg) +
1126 qp->rq.max_gs * sizeof (struct mthca_data_seg)) / 16;
1127
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 for (i = 0; i < qp->rq.max; ++i) {
Roland Dreierddf841f2005-04-16 15:26:33 -07001129 next = get_recv_wqe(qp, i);
1130 next->nda_op = cpu_to_be32(((i + 1) & (qp->rq.max - 1)) <<
1131 qp->rq.wqe_shift);
1132 next->ee_nds = cpu_to_be32(size);
1133
1134 for (scatter = (void *) (next + 1);
1135 (void *) scatter < (void *) next + (1 << qp->rq.wqe_shift);
1136 ++scatter)
1137 scatter->lkey = cpu_to_be32(MTHCA_INVAL_LKEY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 }
1139
1140 for (i = 0; i < qp->sq.max; ++i) {
Roland Dreierddf841f2005-04-16 15:26:33 -07001141 next = get_send_wqe(qp, i);
1142 next->nda_op = cpu_to_be32((((i + 1) & (qp->sq.max - 1)) <<
1143 qp->sq.wqe_shift) +
1144 qp->send_wqe_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 }
1146 }
1147
1148 return 0;
1149}
1150
Roland Dreier80c8ec22005-07-07 17:57:20 -07001151static int mthca_set_qp_size(struct mthca_dev *dev, struct ib_qp_cap *cap,
1152 struct mthca_qp *qp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153{
Roland Dreier80c8ec22005-07-07 17:57:20 -07001154 /* Sanity check QP size before proceeding */
1155 if (cap->max_send_wr > 65536 || cap->max_recv_wr > 65536 ||
1156 cap->max_send_sge > 64 || cap->max_recv_sge > 64)
1157 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158
Roland Dreier80c8ec22005-07-07 17:57:20 -07001159 if (mthca_is_memfree(dev)) {
1160 qp->rq.max = cap->max_recv_wr ?
1161 roundup_pow_of_two(cap->max_recv_wr) : 0;
1162 qp->sq.max = cap->max_send_wr ?
1163 roundup_pow_of_two(cap->max_send_wr) : 0;
1164 } else {
1165 qp->rq.max = cap->max_recv_wr;
1166 qp->sq.max = cap->max_send_wr;
1167 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168
Roland Dreier80c8ec22005-07-07 17:57:20 -07001169 qp->rq.max_gs = cap->max_recv_sge;
1170 qp->sq.max_gs = max_t(int, cap->max_send_sge,
1171 ALIGN(cap->max_inline_data + MTHCA_INLINE_HEADER_SIZE,
1172 MTHCA_INLINE_CHUNK_SIZE) /
1173 sizeof (struct mthca_data_seg));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174
Roland Dreier80c8ec22005-07-07 17:57:20 -07001175 /*
1176 * For MLX transport we need 2 extra S/G entries:
1177 * one for the header and one for the checksum at the end
1178 */
1179 if ((qp->transport == MLX && qp->sq.max_gs + 2 > dev->limits.max_sg) ||
1180 qp->sq.max_gs > dev->limits.max_sg || qp->rq.max_gs > dev->limits.max_sg)
1181 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182
Roland Dreier80c8ec22005-07-07 17:57:20 -07001183 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184}
1185
1186int mthca_alloc_qp(struct mthca_dev *dev,
1187 struct mthca_pd *pd,
1188 struct mthca_cq *send_cq,
1189 struct mthca_cq *recv_cq,
1190 enum ib_qp_type type,
1191 enum ib_sig_type send_policy,
Roland Dreier80c8ec22005-07-07 17:57:20 -07001192 struct ib_qp_cap *cap,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 struct mthca_qp *qp)
1194{
1195 int err;
1196
Roland Dreier80c8ec22005-07-07 17:57:20 -07001197 err = mthca_set_qp_size(dev, cap, qp);
1198 if (err)
1199 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200
1201 switch (type) {
1202 case IB_QPT_RC: qp->transport = RC; break;
1203 case IB_QPT_UC: qp->transport = UC; break;
1204 case IB_QPT_UD: qp->transport = UD; break;
1205 default: return -EINVAL;
1206 }
1207
1208 qp->qpn = mthca_alloc(&dev->qp_table.alloc);
1209 if (qp->qpn == -1)
1210 return -ENOMEM;
1211
1212 err = mthca_alloc_qp_common(dev, pd, send_cq, recv_cq,
1213 send_policy, qp);
1214 if (err) {
1215 mthca_free(&dev->qp_table.alloc, qp->qpn);
1216 return err;
1217 }
1218
1219 spin_lock_irq(&dev->qp_table.lock);
1220 mthca_array_set(&dev->qp_table.qp,
1221 qp->qpn & (dev->limits.num_qps - 1), qp);
1222 spin_unlock_irq(&dev->qp_table.lock);
1223
1224 return 0;
1225}
1226
1227int mthca_alloc_sqp(struct mthca_dev *dev,
1228 struct mthca_pd *pd,
1229 struct mthca_cq *send_cq,
1230 struct mthca_cq *recv_cq,
1231 enum ib_sig_type send_policy,
Roland Dreier80c8ec22005-07-07 17:57:20 -07001232 struct ib_qp_cap *cap,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 int qpn,
1234 int port,
1235 struct mthca_sqp *sqp)
1236{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 u32 mqpn = qpn * 2 + dev->qp_table.sqp_start + port - 1;
Roland Dreier80c8ec22005-07-07 17:57:20 -07001238 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239
Roland Dreier80c8ec22005-07-07 17:57:20 -07001240 err = mthca_set_qp_size(dev, cap, &sqp->qp);
1241 if (err)
1242 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243
1244 sqp->header_buf_size = sqp->qp.sq.max * MTHCA_UD_HEADER_SIZE;
1245 sqp->header_buf = dma_alloc_coherent(&dev->pdev->dev, sqp->header_buf_size,
1246 &sqp->header_dma, GFP_KERNEL);
1247 if (!sqp->header_buf)
1248 return -ENOMEM;
1249
1250 spin_lock_irq(&dev->qp_table.lock);
1251 if (mthca_array_get(&dev->qp_table.qp, mqpn))
1252 err = -EBUSY;
1253 else
1254 mthca_array_set(&dev->qp_table.qp, mqpn, sqp);
1255 spin_unlock_irq(&dev->qp_table.lock);
1256
1257 if (err)
1258 goto err_out;
1259
1260 sqp->port = port;
1261 sqp->qp.qpn = mqpn;
1262 sqp->qp.transport = MLX;
1263
1264 err = mthca_alloc_qp_common(dev, pd, send_cq, recv_cq,
1265 send_policy, &sqp->qp);
1266 if (err)
1267 goto err_out_free;
1268
1269 atomic_inc(&pd->sqp_count);
1270
1271 return 0;
1272
1273 err_out_free:
1274 /*
1275 * Lock CQs here, so that CQ polling code can do QP lookup
1276 * without taking a lock.
1277 */
1278 spin_lock_irq(&send_cq->lock);
1279 if (send_cq != recv_cq)
1280 spin_lock(&recv_cq->lock);
1281
1282 spin_lock(&dev->qp_table.lock);
1283 mthca_array_clear(&dev->qp_table.qp, mqpn);
1284 spin_unlock(&dev->qp_table.lock);
1285
1286 if (send_cq != recv_cq)
1287 spin_unlock(&recv_cq->lock);
1288 spin_unlock_irq(&send_cq->lock);
1289
1290 err_out:
1291 dma_free_coherent(&dev->pdev->dev, sqp->header_buf_size,
1292 sqp->header_buf, sqp->header_dma);
1293
1294 return err;
1295}
1296
1297void mthca_free_qp(struct mthca_dev *dev,
1298 struct mthca_qp *qp)
1299{
1300 u8 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 struct mthca_cq *send_cq;
1302 struct mthca_cq *recv_cq;
1303
1304 send_cq = to_mcq(qp->ibqp.send_cq);
1305 recv_cq = to_mcq(qp->ibqp.recv_cq);
1306
1307 /*
1308 * Lock CQs here, so that CQ polling code can do QP lookup
1309 * without taking a lock.
1310 */
1311 spin_lock_irq(&send_cq->lock);
1312 if (send_cq != recv_cq)
1313 spin_lock(&recv_cq->lock);
1314
1315 spin_lock(&dev->qp_table.lock);
1316 mthca_array_clear(&dev->qp_table.qp,
1317 qp->qpn & (dev->limits.num_qps - 1));
1318 spin_unlock(&dev->qp_table.lock);
1319
1320 if (send_cq != recv_cq)
1321 spin_unlock(&recv_cq->lock);
1322 spin_unlock_irq(&send_cq->lock);
1323
1324 atomic_dec(&qp->refcount);
1325 wait_event(qp->wait, !atomic_read(&qp->refcount));
1326
1327 if (qp->state != IB_QPS_RESET)
1328 mthca_MODIFY_QP(dev, MTHCA_TRANS_ANY2RST, qp->qpn, 0, NULL, 0, &status);
1329
Roland Dreier80c8ec22005-07-07 17:57:20 -07001330 /*
1331 * If this is a userspace QP, the buffers, MR, CQs and so on
1332 * will be cleaned up in userspace, so all we have to do is
1333 * unref the mem-free tables and free the QPN in our table.
1334 */
1335 if (!qp->ibqp.uobject) {
1336 mthca_cq_clean(dev, to_mcq(qp->ibqp.send_cq)->cqn, qp->qpn);
1337 if (qp->ibqp.send_cq != qp->ibqp.recv_cq)
1338 mthca_cq_clean(dev, to_mcq(qp->ibqp.recv_cq)->cqn, qp->qpn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339
Roland Dreier80c8ec22005-07-07 17:57:20 -07001340 mthca_free_memfree(dev, qp);
1341 mthca_free_wqe_buf(dev, qp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 }
1343
Roland Dreier80c8ec22005-07-07 17:57:20 -07001344 mthca_unmap_memfree(dev, qp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345
1346 if (is_sqp(dev, qp)) {
1347 atomic_dec(&(to_mpd(qp->ibqp.pd)->sqp_count));
1348 dma_free_coherent(&dev->pdev->dev,
1349 to_msqp(qp)->header_buf_size,
1350 to_msqp(qp)->header_buf,
1351 to_msqp(qp)->header_dma);
1352 } else
1353 mthca_free(&dev->qp_table.alloc, qp->qpn);
1354}
1355
1356/* Create UD header for an MLX send and build a data segment for it */
1357static int build_mlx_header(struct mthca_dev *dev, struct mthca_sqp *sqp,
1358 int ind, struct ib_send_wr *wr,
1359 struct mthca_mlx_seg *mlx,
1360 struct mthca_data_seg *data)
1361{
1362 int header_size;
1363 int err;
Sean Hefty97f52eb2005-08-13 21:05:57 -07001364 u16 pkey;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365
1366 ib_ud_header_init(256, /* assume a MAD */
1367 sqp->ud_header.grh_present,
1368 &sqp->ud_header);
1369
1370 err = mthca_read_ah(dev, to_mah(wr->wr.ud.ah), &sqp->ud_header);
1371 if (err)
1372 return err;
1373 mlx->flags &= ~cpu_to_be32(MTHCA_NEXT_SOLICIT | 1);
1374 mlx->flags |= cpu_to_be32((!sqp->qp.ibqp.qp_num ? MTHCA_MLX_VL15 : 0) |
Sean Hefty97f52eb2005-08-13 21:05:57 -07001375 (sqp->ud_header.lrh.destination_lid ==
1376 IB_LID_PERMISSIVE ? MTHCA_MLX_SLR : 0) |
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 (sqp->ud_header.lrh.service_level << 8));
1378 mlx->rlid = sqp->ud_header.lrh.destination_lid;
1379 mlx->vcrc = 0;
1380
1381 switch (wr->opcode) {
1382 case IB_WR_SEND:
1383 sqp->ud_header.bth.opcode = IB_OPCODE_UD_SEND_ONLY;
1384 sqp->ud_header.immediate_present = 0;
1385 break;
1386 case IB_WR_SEND_WITH_IMM:
1387 sqp->ud_header.bth.opcode = IB_OPCODE_UD_SEND_ONLY_WITH_IMMEDIATE;
1388 sqp->ud_header.immediate_present = 1;
1389 sqp->ud_header.immediate_data = wr->imm_data;
1390 break;
1391 default:
1392 return -EINVAL;
1393 }
1394
1395 sqp->ud_header.lrh.virtual_lane = !sqp->qp.ibqp.qp_num ? 15 : 0;
Sean Hefty97f52eb2005-08-13 21:05:57 -07001396 if (sqp->ud_header.lrh.destination_lid == IB_LID_PERMISSIVE)
1397 sqp->ud_header.lrh.source_lid = IB_LID_PERMISSIVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 sqp->ud_header.bth.solicited_event = !!(wr->send_flags & IB_SEND_SOLICITED);
1399 if (!sqp->qp.ibqp.qp_num)
1400 ib_get_cached_pkey(&dev->ib_dev, sqp->port,
Sean Hefty97f52eb2005-08-13 21:05:57 -07001401 sqp->pkey_index, &pkey);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 else
1403 ib_get_cached_pkey(&dev->ib_dev, sqp->port,
Sean Hefty97f52eb2005-08-13 21:05:57 -07001404 wr->wr.ud.pkey_index, &pkey);
1405 sqp->ud_header.bth.pkey = cpu_to_be16(pkey);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 sqp->ud_header.bth.destination_qpn = cpu_to_be32(wr->wr.ud.remote_qpn);
1407 sqp->ud_header.bth.psn = cpu_to_be32((sqp->send_psn++) & ((1 << 24) - 1));
1408 sqp->ud_header.deth.qkey = cpu_to_be32(wr->wr.ud.remote_qkey & 0x80000000 ?
1409 sqp->qkey : wr->wr.ud.remote_qkey);
1410 sqp->ud_header.deth.source_qpn = cpu_to_be32(sqp->qp.ibqp.qp_num);
1411
1412 header_size = ib_ud_header_pack(&sqp->ud_header,
1413 sqp->header_buf +
1414 ind * MTHCA_UD_HEADER_SIZE);
1415
1416 data->byte_count = cpu_to_be32(header_size);
1417 data->lkey = cpu_to_be32(to_mpd(sqp->qp.ibqp.pd)->ntmr.ibmr.lkey);
1418 data->addr = cpu_to_be64(sqp->header_dma +
1419 ind * MTHCA_UD_HEADER_SIZE);
1420
1421 return 0;
1422}
1423
1424static inline int mthca_wq_overflow(struct mthca_wq *wq, int nreq,
1425 struct ib_cq *ib_cq)
1426{
1427 unsigned cur;
1428 struct mthca_cq *cq;
1429
1430 cur = wq->head - wq->tail;
1431 if (likely(cur + nreq < wq->max))
1432 return 0;
1433
1434 cq = to_mcq(ib_cq);
1435 spin_lock(&cq->lock);
1436 cur = wq->head - wq->tail;
1437 spin_unlock(&cq->lock);
1438
1439 return cur + nreq >= wq->max;
1440}
1441
1442int mthca_tavor_post_send(struct ib_qp *ibqp, struct ib_send_wr *wr,
1443 struct ib_send_wr **bad_wr)
1444{
1445 struct mthca_dev *dev = to_mdev(ibqp->device);
1446 struct mthca_qp *qp = to_mqp(ibqp);
1447 void *wqe;
1448 void *prev_wqe;
1449 unsigned long flags;
1450 int err = 0;
1451 int nreq;
1452 int i;
1453 int size;
1454 int size0 = 0;
1455 u32 f0 = 0;
1456 int ind;
1457 u8 op0 = 0;
1458
1459 spin_lock_irqsave(&qp->sq.lock, flags);
1460
1461 /* XXX check that state is OK to post send */
1462
1463 ind = qp->sq.next_ind;
1464
1465 for (nreq = 0; wr; ++nreq, wr = wr->next) {
1466 if (mthca_wq_overflow(&qp->sq, nreq, qp->ibqp.send_cq)) {
1467 mthca_err(dev, "SQ %06x full (%u head, %u tail,"
1468 " %d max, %d nreq)\n", qp->qpn,
1469 qp->sq.head, qp->sq.tail,
1470 qp->sq.max, nreq);
1471 err = -ENOMEM;
1472 *bad_wr = wr;
1473 goto out;
1474 }
1475
1476 wqe = get_send_wqe(qp, ind);
1477 prev_wqe = qp->sq.last;
1478 qp->sq.last = wqe;
1479
1480 ((struct mthca_next_seg *) wqe)->nda_op = 0;
1481 ((struct mthca_next_seg *) wqe)->ee_nds = 0;
1482 ((struct mthca_next_seg *) wqe)->flags =
1483 ((wr->send_flags & IB_SEND_SIGNALED) ?
1484 cpu_to_be32(MTHCA_NEXT_CQ_UPDATE) : 0) |
1485 ((wr->send_flags & IB_SEND_SOLICITED) ?
1486 cpu_to_be32(MTHCA_NEXT_SOLICIT) : 0) |
1487 cpu_to_be32(1);
1488 if (wr->opcode == IB_WR_SEND_WITH_IMM ||
1489 wr->opcode == IB_WR_RDMA_WRITE_WITH_IMM)
Roland Dreier3fba2312005-04-16 15:26:16 -07001490 ((struct mthca_next_seg *) wqe)->imm = wr->imm_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491
1492 wqe += sizeof (struct mthca_next_seg);
1493 size = sizeof (struct mthca_next_seg) / 16;
1494
1495 switch (qp->transport) {
1496 case RC:
1497 switch (wr->opcode) {
1498 case IB_WR_ATOMIC_CMP_AND_SWP:
1499 case IB_WR_ATOMIC_FETCH_AND_ADD:
1500 ((struct mthca_raddr_seg *) wqe)->raddr =
1501 cpu_to_be64(wr->wr.atomic.remote_addr);
1502 ((struct mthca_raddr_seg *) wqe)->rkey =
1503 cpu_to_be32(wr->wr.atomic.rkey);
1504 ((struct mthca_raddr_seg *) wqe)->reserved = 0;
1505
1506 wqe += sizeof (struct mthca_raddr_seg);
1507
1508 if (wr->opcode == IB_WR_ATOMIC_CMP_AND_SWP) {
1509 ((struct mthca_atomic_seg *) wqe)->swap_add =
1510 cpu_to_be64(wr->wr.atomic.swap);
1511 ((struct mthca_atomic_seg *) wqe)->compare =
1512 cpu_to_be64(wr->wr.atomic.compare_add);
1513 } else {
1514 ((struct mthca_atomic_seg *) wqe)->swap_add =
1515 cpu_to_be64(wr->wr.atomic.compare_add);
1516 ((struct mthca_atomic_seg *) wqe)->compare = 0;
1517 }
1518
1519 wqe += sizeof (struct mthca_atomic_seg);
1520 size += sizeof (struct mthca_raddr_seg) / 16 +
1521 sizeof (struct mthca_atomic_seg);
1522 break;
1523
1524 case IB_WR_RDMA_WRITE:
1525 case IB_WR_RDMA_WRITE_WITH_IMM:
1526 case IB_WR_RDMA_READ:
1527 ((struct mthca_raddr_seg *) wqe)->raddr =
1528 cpu_to_be64(wr->wr.rdma.remote_addr);
1529 ((struct mthca_raddr_seg *) wqe)->rkey =
1530 cpu_to_be32(wr->wr.rdma.rkey);
1531 ((struct mthca_raddr_seg *) wqe)->reserved = 0;
1532 wqe += sizeof (struct mthca_raddr_seg);
1533 size += sizeof (struct mthca_raddr_seg) / 16;
1534 break;
1535
1536 default:
1537 /* No extra segments required for sends */
1538 break;
1539 }
1540
1541 break;
1542
Roland Dreier9e6970b2005-06-27 14:36:42 -07001543 case UC:
1544 switch (wr->opcode) {
1545 case IB_WR_RDMA_WRITE:
1546 case IB_WR_RDMA_WRITE_WITH_IMM:
1547 ((struct mthca_raddr_seg *) wqe)->raddr =
1548 cpu_to_be64(wr->wr.rdma.remote_addr);
1549 ((struct mthca_raddr_seg *) wqe)->rkey =
1550 cpu_to_be32(wr->wr.rdma.rkey);
1551 ((struct mthca_raddr_seg *) wqe)->reserved = 0;
1552 wqe += sizeof (struct mthca_raddr_seg);
1553 size += sizeof (struct mthca_raddr_seg) / 16;
1554 break;
1555
1556 default:
1557 /* No extra segments required for sends */
1558 break;
1559 }
1560
1561 break;
1562
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 case UD:
1564 ((struct mthca_tavor_ud_seg *) wqe)->lkey =
1565 cpu_to_be32(to_mah(wr->wr.ud.ah)->key);
1566 ((struct mthca_tavor_ud_seg *) wqe)->av_addr =
1567 cpu_to_be64(to_mah(wr->wr.ud.ah)->avdma);
1568 ((struct mthca_tavor_ud_seg *) wqe)->dqpn =
1569 cpu_to_be32(wr->wr.ud.remote_qpn);
1570 ((struct mthca_tavor_ud_seg *) wqe)->qkey =
1571 cpu_to_be32(wr->wr.ud.remote_qkey);
1572
1573 wqe += sizeof (struct mthca_tavor_ud_seg);
1574 size += sizeof (struct mthca_tavor_ud_seg) / 16;
1575 break;
1576
1577 case MLX:
1578 err = build_mlx_header(dev, to_msqp(qp), ind, wr,
1579 wqe - sizeof (struct mthca_next_seg),
1580 wqe);
1581 if (err) {
1582 *bad_wr = wr;
1583 goto out;
1584 }
1585 wqe += sizeof (struct mthca_data_seg);
1586 size += sizeof (struct mthca_data_seg) / 16;
1587 break;
1588 }
1589
1590 if (wr->num_sge > qp->sq.max_gs) {
1591 mthca_err(dev, "too many gathers\n");
1592 err = -EINVAL;
1593 *bad_wr = wr;
1594 goto out;
1595 }
1596
1597 for (i = 0; i < wr->num_sge; ++i) {
1598 ((struct mthca_data_seg *) wqe)->byte_count =
1599 cpu_to_be32(wr->sg_list[i].length);
1600 ((struct mthca_data_seg *) wqe)->lkey =
1601 cpu_to_be32(wr->sg_list[i].lkey);
1602 ((struct mthca_data_seg *) wqe)->addr =
1603 cpu_to_be64(wr->sg_list[i].addr);
1604 wqe += sizeof (struct mthca_data_seg);
1605 size += sizeof (struct mthca_data_seg) / 16;
1606 }
1607
1608 /* Add one more inline data segment for ICRC */
1609 if (qp->transport == MLX) {
1610 ((struct mthca_data_seg *) wqe)->byte_count =
1611 cpu_to_be32((1 << 31) | 4);
1612 ((u32 *) wqe)[1] = 0;
1613 wqe += sizeof (struct mthca_data_seg);
1614 size += sizeof (struct mthca_data_seg) / 16;
1615 }
1616
1617 qp->wrid[ind + qp->rq.max] = wr->wr_id;
1618
1619 if (wr->opcode >= ARRAY_SIZE(mthca_opcode)) {
1620 mthca_err(dev, "opcode invalid\n");
1621 err = -EINVAL;
1622 *bad_wr = wr;
1623 goto out;
1624 }
1625
1626 if (prev_wqe) {
1627 ((struct mthca_next_seg *) prev_wqe)->nda_op =
1628 cpu_to_be32(((ind << qp->sq.wqe_shift) +
1629 qp->send_wqe_offset) |
1630 mthca_opcode[wr->opcode]);
1631 wmb();
1632 ((struct mthca_next_seg *) prev_wqe)->ee_nds =
1633 cpu_to_be32((size0 ? 0 : MTHCA_NEXT_DBD) | size);
1634 }
1635
1636 if (!size0) {
1637 size0 = size;
1638 op0 = mthca_opcode[wr->opcode];
1639 }
1640
1641 ++ind;
1642 if (unlikely(ind >= qp->sq.max))
1643 ind -= qp->sq.max;
1644 }
1645
1646out:
1647 if (likely(nreq)) {
Sean Hefty97f52eb2005-08-13 21:05:57 -07001648 __be32 doorbell[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649
1650 doorbell[0] = cpu_to_be32(((qp->sq.next_ind << qp->sq.wqe_shift) +
1651 qp->send_wqe_offset) | f0 | op0);
1652 doorbell[1] = cpu_to_be32((qp->qpn << 8) | size0);
1653
1654 wmb();
1655
1656 mthca_write64(doorbell,
1657 dev->kar + MTHCA_SEND_DOORBELL,
1658 MTHCA_GET_DOORBELL_LOCK(&dev->doorbell_lock));
1659 }
1660
1661 qp->sq.next_ind = ind;
1662 qp->sq.head += nreq;
1663
1664 spin_unlock_irqrestore(&qp->sq.lock, flags);
1665 return err;
1666}
1667
1668int mthca_tavor_post_receive(struct ib_qp *ibqp, struct ib_recv_wr *wr,
1669 struct ib_recv_wr **bad_wr)
1670{
1671 struct mthca_dev *dev = to_mdev(ibqp->device);
1672 struct mthca_qp *qp = to_mqp(ibqp);
1673 unsigned long flags;
1674 int err = 0;
1675 int nreq;
1676 int i;
1677 int size;
1678 int size0 = 0;
1679 int ind;
1680 void *wqe;
1681 void *prev_wqe;
1682
1683 spin_lock_irqsave(&qp->rq.lock, flags);
1684
1685 /* XXX check that state is OK to post receive */
1686
1687 ind = qp->rq.next_ind;
1688
1689 for (nreq = 0; wr; ++nreq, wr = wr->next) {
1690 if (mthca_wq_overflow(&qp->rq, nreq, qp->ibqp.recv_cq)) {
1691 mthca_err(dev, "RQ %06x full (%u head, %u tail,"
1692 " %d max, %d nreq)\n", qp->qpn,
1693 qp->rq.head, qp->rq.tail,
1694 qp->rq.max, nreq);
1695 err = -ENOMEM;
1696 *bad_wr = wr;
1697 goto out;
1698 }
1699
1700 wqe = get_recv_wqe(qp, ind);
1701 prev_wqe = qp->rq.last;
1702 qp->rq.last = wqe;
1703
1704 ((struct mthca_next_seg *) wqe)->nda_op = 0;
1705 ((struct mthca_next_seg *) wqe)->ee_nds =
1706 cpu_to_be32(MTHCA_NEXT_DBD);
1707 ((struct mthca_next_seg *) wqe)->flags = 0;
1708
1709 wqe += sizeof (struct mthca_next_seg);
1710 size = sizeof (struct mthca_next_seg) / 16;
1711
1712 if (unlikely(wr->num_sge > qp->rq.max_gs)) {
1713 err = -EINVAL;
1714 *bad_wr = wr;
1715 goto out;
1716 }
1717
1718 for (i = 0; i < wr->num_sge; ++i) {
1719 ((struct mthca_data_seg *) wqe)->byte_count =
1720 cpu_to_be32(wr->sg_list[i].length);
1721 ((struct mthca_data_seg *) wqe)->lkey =
1722 cpu_to_be32(wr->sg_list[i].lkey);
1723 ((struct mthca_data_seg *) wqe)->addr =
1724 cpu_to_be64(wr->sg_list[i].addr);
1725 wqe += sizeof (struct mthca_data_seg);
1726 size += sizeof (struct mthca_data_seg) / 16;
1727 }
1728
1729 qp->wrid[ind] = wr->wr_id;
1730
1731 if (likely(prev_wqe)) {
1732 ((struct mthca_next_seg *) prev_wqe)->nda_op =
1733 cpu_to_be32((ind << qp->rq.wqe_shift) | 1);
1734 wmb();
1735 ((struct mthca_next_seg *) prev_wqe)->ee_nds =
1736 cpu_to_be32(MTHCA_NEXT_DBD | size);
1737 }
1738
1739 if (!size0)
1740 size0 = size;
1741
1742 ++ind;
1743 if (unlikely(ind >= qp->rq.max))
1744 ind -= qp->rq.max;
1745 }
1746
1747out:
1748 if (likely(nreq)) {
Sean Hefty97f52eb2005-08-13 21:05:57 -07001749 __be32 doorbell[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750
1751 doorbell[0] = cpu_to_be32((qp->rq.next_ind << qp->rq.wqe_shift) | size0);
1752 doorbell[1] = cpu_to_be32((qp->qpn << 8) | nreq);
1753
1754 wmb();
1755
1756 mthca_write64(doorbell,
1757 dev->kar + MTHCA_RECEIVE_DOORBELL,
1758 MTHCA_GET_DOORBELL_LOCK(&dev->doorbell_lock));
1759 }
1760
1761 qp->rq.next_ind = ind;
1762 qp->rq.head += nreq;
1763
1764 spin_unlock_irqrestore(&qp->rq.lock, flags);
1765 return err;
1766}
1767
1768int mthca_arbel_post_send(struct ib_qp *ibqp, struct ib_send_wr *wr,
1769 struct ib_send_wr **bad_wr)
1770{
1771 struct mthca_dev *dev = to_mdev(ibqp->device);
1772 struct mthca_qp *qp = to_mqp(ibqp);
1773 void *wqe;
1774 void *prev_wqe;
1775 unsigned long flags;
1776 int err = 0;
1777 int nreq;
1778 int i;
1779 int size;
1780 int size0 = 0;
1781 u32 f0 = 0;
1782 int ind;
1783 u8 op0 = 0;
1784
1785 spin_lock_irqsave(&qp->sq.lock, flags);
1786
1787 /* XXX check that state is OK to post send */
1788
1789 ind = qp->sq.head & (qp->sq.max - 1);
1790
1791 for (nreq = 0; wr; ++nreq, wr = wr->next) {
1792 if (mthca_wq_overflow(&qp->sq, nreq, qp->ibqp.send_cq)) {
1793 mthca_err(dev, "SQ %06x full (%u head, %u tail,"
1794 " %d max, %d nreq)\n", qp->qpn,
1795 qp->sq.head, qp->sq.tail,
1796 qp->sq.max, nreq);
1797 err = -ENOMEM;
1798 *bad_wr = wr;
1799 goto out;
1800 }
1801
1802 wqe = get_send_wqe(qp, ind);
1803 prev_wqe = qp->sq.last;
1804 qp->sq.last = wqe;
1805
1806 ((struct mthca_next_seg *) wqe)->flags =
1807 ((wr->send_flags & IB_SEND_SIGNALED) ?
1808 cpu_to_be32(MTHCA_NEXT_CQ_UPDATE) : 0) |
1809 ((wr->send_flags & IB_SEND_SOLICITED) ?
1810 cpu_to_be32(MTHCA_NEXT_SOLICIT) : 0) |
1811 cpu_to_be32(1);
1812 if (wr->opcode == IB_WR_SEND_WITH_IMM ||
1813 wr->opcode == IB_WR_RDMA_WRITE_WITH_IMM)
Roland Dreier3fba2312005-04-16 15:26:16 -07001814 ((struct mthca_next_seg *) wqe)->imm = wr->imm_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815
1816 wqe += sizeof (struct mthca_next_seg);
1817 size = sizeof (struct mthca_next_seg) / 16;
1818
1819 switch (qp->transport) {
Roland Dreierddb934e2005-04-16 15:26:23 -07001820 case RC:
1821 switch (wr->opcode) {
1822 case IB_WR_ATOMIC_CMP_AND_SWP:
1823 case IB_WR_ATOMIC_FETCH_AND_ADD:
1824 ((struct mthca_raddr_seg *) wqe)->raddr =
1825 cpu_to_be64(wr->wr.atomic.remote_addr);
1826 ((struct mthca_raddr_seg *) wqe)->rkey =
1827 cpu_to_be32(wr->wr.atomic.rkey);
1828 ((struct mthca_raddr_seg *) wqe)->reserved = 0;
1829
1830 wqe += sizeof (struct mthca_raddr_seg);
1831
1832 if (wr->opcode == IB_WR_ATOMIC_CMP_AND_SWP) {
1833 ((struct mthca_atomic_seg *) wqe)->swap_add =
1834 cpu_to_be64(wr->wr.atomic.swap);
1835 ((struct mthca_atomic_seg *) wqe)->compare =
1836 cpu_to_be64(wr->wr.atomic.compare_add);
1837 } else {
1838 ((struct mthca_atomic_seg *) wqe)->swap_add =
1839 cpu_to_be64(wr->wr.atomic.compare_add);
1840 ((struct mthca_atomic_seg *) wqe)->compare = 0;
1841 }
1842
1843 wqe += sizeof (struct mthca_atomic_seg);
1844 size += sizeof (struct mthca_raddr_seg) / 16 +
1845 sizeof (struct mthca_atomic_seg);
1846 break;
1847
Roland Dreier9e6970b2005-06-27 14:36:42 -07001848 case IB_WR_RDMA_READ:
Roland Dreierddb934e2005-04-16 15:26:23 -07001849 case IB_WR_RDMA_WRITE:
1850 case IB_WR_RDMA_WRITE_WITH_IMM:
Roland Dreier9e6970b2005-06-27 14:36:42 -07001851 ((struct mthca_raddr_seg *) wqe)->raddr =
1852 cpu_to_be64(wr->wr.rdma.remote_addr);
1853 ((struct mthca_raddr_seg *) wqe)->rkey =
1854 cpu_to_be32(wr->wr.rdma.rkey);
1855 ((struct mthca_raddr_seg *) wqe)->reserved = 0;
1856 wqe += sizeof (struct mthca_raddr_seg);
1857 size += sizeof (struct mthca_raddr_seg) / 16;
1858 break;
1859
1860 default:
1861 /* No extra segments required for sends */
1862 break;
1863 }
1864
1865 break;
1866
1867 case UC:
1868 switch (wr->opcode) {
1869 case IB_WR_RDMA_WRITE:
1870 case IB_WR_RDMA_WRITE_WITH_IMM:
Roland Dreierddb934e2005-04-16 15:26:23 -07001871 ((struct mthca_raddr_seg *) wqe)->raddr =
1872 cpu_to_be64(wr->wr.rdma.remote_addr);
1873 ((struct mthca_raddr_seg *) wqe)->rkey =
1874 cpu_to_be32(wr->wr.rdma.rkey);
1875 ((struct mthca_raddr_seg *) wqe)->reserved = 0;
1876 wqe += sizeof (struct mthca_raddr_seg);
1877 size += sizeof (struct mthca_raddr_seg) / 16;
1878 break;
1879
1880 default:
1881 /* No extra segments required for sends */
1882 break;
1883 }
1884
1885 break;
1886
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887 case UD:
1888 memcpy(((struct mthca_arbel_ud_seg *) wqe)->av,
1889 to_mah(wr->wr.ud.ah)->av, MTHCA_AV_SIZE);
1890 ((struct mthca_arbel_ud_seg *) wqe)->dqpn =
1891 cpu_to_be32(wr->wr.ud.remote_qpn);
1892 ((struct mthca_arbel_ud_seg *) wqe)->qkey =
1893 cpu_to_be32(wr->wr.ud.remote_qkey);
1894
1895 wqe += sizeof (struct mthca_arbel_ud_seg);
1896 size += sizeof (struct mthca_arbel_ud_seg) / 16;
1897 break;
1898
1899 case MLX:
1900 err = build_mlx_header(dev, to_msqp(qp), ind, wr,
1901 wqe - sizeof (struct mthca_next_seg),
1902 wqe);
1903 if (err) {
1904 *bad_wr = wr;
1905 goto out;
1906 }
1907 wqe += sizeof (struct mthca_data_seg);
1908 size += sizeof (struct mthca_data_seg) / 16;
1909 break;
1910 }
1911
1912 if (wr->num_sge > qp->sq.max_gs) {
1913 mthca_err(dev, "too many gathers\n");
1914 err = -EINVAL;
1915 *bad_wr = wr;
1916 goto out;
1917 }
1918
1919 for (i = 0; i < wr->num_sge; ++i) {
1920 ((struct mthca_data_seg *) wqe)->byte_count =
1921 cpu_to_be32(wr->sg_list[i].length);
1922 ((struct mthca_data_seg *) wqe)->lkey =
1923 cpu_to_be32(wr->sg_list[i].lkey);
1924 ((struct mthca_data_seg *) wqe)->addr =
1925 cpu_to_be64(wr->sg_list[i].addr);
1926 wqe += sizeof (struct mthca_data_seg);
1927 size += sizeof (struct mthca_data_seg) / 16;
1928 }
1929
1930 /* Add one more inline data segment for ICRC */
1931 if (qp->transport == MLX) {
1932 ((struct mthca_data_seg *) wqe)->byte_count =
1933 cpu_to_be32((1 << 31) | 4);
1934 ((u32 *) wqe)[1] = 0;
1935 wqe += sizeof (struct mthca_data_seg);
1936 size += sizeof (struct mthca_data_seg) / 16;
1937 }
1938
1939 qp->wrid[ind + qp->rq.max] = wr->wr_id;
1940
1941 if (wr->opcode >= ARRAY_SIZE(mthca_opcode)) {
1942 mthca_err(dev, "opcode invalid\n");
1943 err = -EINVAL;
1944 *bad_wr = wr;
1945 goto out;
1946 }
1947
1948 if (likely(prev_wqe)) {
1949 ((struct mthca_next_seg *) prev_wqe)->nda_op =
1950 cpu_to_be32(((ind << qp->sq.wqe_shift) +
1951 qp->send_wqe_offset) |
1952 mthca_opcode[wr->opcode]);
1953 wmb();
1954 ((struct mthca_next_seg *) prev_wqe)->ee_nds =
1955 cpu_to_be32(MTHCA_NEXT_DBD | size);
1956 }
1957
1958 if (!size0) {
1959 size0 = size;
1960 op0 = mthca_opcode[wr->opcode];
1961 }
1962
1963 ++ind;
1964 if (unlikely(ind >= qp->sq.max))
1965 ind -= qp->sq.max;
1966 }
1967
1968out:
1969 if (likely(nreq)) {
Sean Hefty97f52eb2005-08-13 21:05:57 -07001970 __be32 doorbell[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971
1972 doorbell[0] = cpu_to_be32((nreq << 24) |
1973 ((qp->sq.head & 0xffff) << 8) |
1974 f0 | op0);
1975 doorbell[1] = cpu_to_be32((qp->qpn << 8) | size0);
1976
1977 qp->sq.head += nreq;
1978
1979 /*
1980 * Make sure that descriptors are written before
1981 * doorbell record.
1982 */
1983 wmb();
1984 *qp->sq.db = cpu_to_be32(qp->sq.head & 0xffff);
1985
1986 /*
1987 * Make sure doorbell record is written before we
1988 * write MMIO send doorbell.
1989 */
1990 wmb();
1991 mthca_write64(doorbell,
1992 dev->kar + MTHCA_SEND_DOORBELL,
1993 MTHCA_GET_DOORBELL_LOCK(&dev->doorbell_lock));
1994 }
1995
1996 spin_unlock_irqrestore(&qp->sq.lock, flags);
1997 return err;
1998}
1999
2000int mthca_arbel_post_receive(struct ib_qp *ibqp, struct ib_recv_wr *wr,
2001 struct ib_recv_wr **bad_wr)
2002{
2003 struct mthca_dev *dev = to_mdev(ibqp->device);
2004 struct mthca_qp *qp = to_mqp(ibqp);
2005 unsigned long flags;
2006 int err = 0;
2007 int nreq;
2008 int ind;
2009 int i;
2010 void *wqe;
2011
2012 spin_lock_irqsave(&qp->rq.lock, flags);
2013
2014 /* XXX check that state is OK to post receive */
2015
2016 ind = qp->rq.head & (qp->rq.max - 1);
2017
2018 for (nreq = 0; wr; ++nreq, wr = wr->next) {
2019 if (mthca_wq_overflow(&qp->rq, nreq, qp->ibqp.recv_cq)) {
2020 mthca_err(dev, "RQ %06x full (%u head, %u tail,"
2021 " %d max, %d nreq)\n", qp->qpn,
2022 qp->rq.head, qp->rq.tail,
2023 qp->rq.max, nreq);
2024 err = -ENOMEM;
2025 *bad_wr = wr;
2026 goto out;
2027 }
2028
2029 wqe = get_recv_wqe(qp, ind);
2030
2031 ((struct mthca_next_seg *) wqe)->flags = 0;
2032
2033 wqe += sizeof (struct mthca_next_seg);
2034
2035 if (unlikely(wr->num_sge > qp->rq.max_gs)) {
2036 err = -EINVAL;
2037 *bad_wr = wr;
2038 goto out;
2039 }
2040
2041 for (i = 0; i < wr->num_sge; ++i) {
2042 ((struct mthca_data_seg *) wqe)->byte_count =
2043 cpu_to_be32(wr->sg_list[i].length);
2044 ((struct mthca_data_seg *) wqe)->lkey =
2045 cpu_to_be32(wr->sg_list[i].lkey);
2046 ((struct mthca_data_seg *) wqe)->addr =
2047 cpu_to_be64(wr->sg_list[i].addr);
2048 wqe += sizeof (struct mthca_data_seg);
2049 }
2050
2051 if (i < qp->rq.max_gs) {
2052 ((struct mthca_data_seg *) wqe)->byte_count = 0;
Roland Dreierddf841f2005-04-16 15:26:33 -07002053 ((struct mthca_data_seg *) wqe)->lkey = cpu_to_be32(MTHCA_INVAL_LKEY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054 ((struct mthca_data_seg *) wqe)->addr = 0;
2055 }
2056
2057 qp->wrid[ind] = wr->wr_id;
2058
2059 ++ind;
2060 if (unlikely(ind >= qp->rq.max))
2061 ind -= qp->rq.max;
2062 }
2063out:
2064 if (likely(nreq)) {
2065 qp->rq.head += nreq;
2066
2067 /*
2068 * Make sure that descriptors are written before
2069 * doorbell record.
2070 */
2071 wmb();
2072 *qp->rq.db = cpu_to_be32(qp->rq.head & 0xffff);
2073 }
2074
2075 spin_unlock_irqrestore(&qp->rq.lock, flags);
2076 return err;
2077}
2078
2079int mthca_free_err_wqe(struct mthca_dev *dev, struct mthca_qp *qp, int is_send,
Sean Hefty97f52eb2005-08-13 21:05:57 -07002080 int index, int *dbd, __be32 *new_wqe)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081{
2082 struct mthca_next_seg *next;
2083
2084 if (is_send)
2085 next = get_send_wqe(qp, index);
2086 else
2087 next = get_recv_wqe(qp, index);
2088
Roland Dreierd10ddbf2005-04-16 15:26:32 -07002089 if (mthca_is_memfree(dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090 *dbd = 1;
2091 else
2092 *dbd = !!(next->ee_nds & cpu_to_be32(MTHCA_NEXT_DBD));
2093 if (next->ee_nds & cpu_to_be32(0x3f))
2094 *new_wqe = (next->nda_op & cpu_to_be32(~0x3f)) |
2095 (next->ee_nds & cpu_to_be32(0x3f));
2096 else
2097 *new_wqe = 0;
2098
2099 return 0;
2100}
2101
2102int __devinit mthca_init_qp_table(struct mthca_dev *dev)
2103{
2104 int err;
2105 u8 status;
2106 int i;
2107
2108 spin_lock_init(&dev->qp_table.lock);
2109
2110 /*
2111 * We reserve 2 extra QPs per port for the special QPs. The
2112 * special QP for port 1 has to be even, so round up.
2113 */
2114 dev->qp_table.sqp_start = (dev->limits.reserved_qps + 1) & ~1UL;
2115 err = mthca_alloc_init(&dev->qp_table.alloc,
2116 dev->limits.num_qps,
2117 (1 << 24) - 1,
2118 dev->qp_table.sqp_start +
2119 MTHCA_MAX_PORTS * 2);
2120 if (err)
2121 return err;
2122
2123 err = mthca_array_init(&dev->qp_table.qp,
2124 dev->limits.num_qps);
2125 if (err) {
2126 mthca_alloc_cleanup(&dev->qp_table.alloc);
2127 return err;
2128 }
2129
2130 for (i = 0; i < 2; ++i) {
2131 err = mthca_CONF_SPECIAL_QP(dev, i ? IB_QPT_GSI : IB_QPT_SMI,
2132 dev->qp_table.sqp_start + i * 2,
2133 &status);
2134 if (err)
2135 goto err_out;
2136 if (status) {
2137 mthca_warn(dev, "CONF_SPECIAL_QP returned "
2138 "status %02x, aborting.\n",
2139 status);
2140 err = -EINVAL;
2141 goto err_out;
2142 }
2143 }
2144 return 0;
2145
2146 err_out:
2147 for (i = 0; i < 2; ++i)
2148 mthca_CONF_SPECIAL_QP(dev, i, 0, &status);
2149
2150 mthca_array_cleanup(&dev->qp_table.qp, dev->limits.num_qps);
2151 mthca_alloc_cleanup(&dev->qp_table.alloc);
2152
2153 return err;
2154}
2155
2156void __devexit mthca_cleanup_qp_table(struct mthca_dev *dev)
2157{
2158 int i;
2159 u8 status;
2160
2161 for (i = 0; i < 2; ++i)
2162 mthca_CONF_SPECIAL_QP(dev, i, 0, &status);
2163
2164 mthca_alloc_cleanup(&dev->qp_table.alloc);
2165}