diff options
Diffstat (limited to 'drivers/infiniband/hw/ipath/ipath_qp.c')
-rw-r--r-- | drivers/infiniband/hw/ipath/ipath_qp.c | 913 |
1 files changed, 913 insertions, 0 deletions
diff --git a/drivers/infiniband/hw/ipath/ipath_qp.c b/drivers/infiniband/hw/ipath/ipath_qp.c new file mode 100644 index 000000000000..18890716db1e --- /dev/null +++ b/drivers/infiniband/hw/ipath/ipath_qp.c | |||
@@ -0,0 +1,913 @@ | |||
1 | /* | ||
2 | * Copyright (c) 2005, 2006 PathScale, Inc. All rights reserved. | ||
3 | * | ||
4 | * This software is available to you under a choice of one of two | ||
5 | * licenses. You may choose to be licensed under the terms of the GNU | ||
6 | * General Public License (GPL) Version 2, available from the file | ||
7 | * COPYING in the main directory of this source tree, or the | ||
8 | * OpenIB.org BSD license below: | ||
9 | * | ||
10 | * Redistribution and use in source and binary forms, with or | ||
11 | * without modification, are permitted provided that the following | ||
12 | * conditions are met: | ||
13 | * | ||
14 | * - Redistributions of source code must retain the above | ||
15 | * copyright notice, this list of conditions and the following | ||
16 | * disclaimer. | ||
17 | * | ||
18 | * - Redistributions in binary form must reproduce the above | ||
19 | * copyright notice, this list of conditions and the following | ||
20 | * disclaimer in the documentation and/or other materials | ||
21 | * provided with the distribution. | ||
22 | * | ||
23 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | ||
24 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | ||
25 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | ||
26 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS | ||
27 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN | ||
28 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN | ||
29 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
30 | * SOFTWARE. | ||
31 | */ | ||
32 | |||
33 | #include <linux/err.h> | ||
34 | #include <linux/vmalloc.h> | ||
35 | |||
36 | #include "ipath_verbs.h" | ||
37 | #include "ips_common.h" | ||
38 | |||
39 | #define BITS_PER_PAGE (PAGE_SIZE*BITS_PER_BYTE) | ||
40 | #define BITS_PER_PAGE_MASK (BITS_PER_PAGE-1) | ||
41 | #define mk_qpn(qpt, map, off) (((map) - (qpt)->map) * BITS_PER_PAGE + \ | ||
42 | (off)) | ||
43 | #define find_next_offset(map, off) find_next_zero_bit((map)->page, \ | ||
44 | BITS_PER_PAGE, off) | ||
45 | |||
46 | #define TRANS_INVALID 0 | ||
47 | #define TRANS_ANY2RST 1 | ||
48 | #define TRANS_RST2INIT 2 | ||
49 | #define TRANS_INIT2INIT 3 | ||
50 | #define TRANS_INIT2RTR 4 | ||
51 | #define TRANS_RTR2RTS 5 | ||
52 | #define TRANS_RTS2RTS 6 | ||
53 | #define TRANS_SQERR2RTS 7 | ||
54 | #define TRANS_ANY2ERR 8 | ||
55 | #define TRANS_RTS2SQD 9 /* XXX Wait for expected ACKs & signal event */ | ||
56 | #define TRANS_SQD2SQD 10 /* error if not drained & parameter change */ | ||
57 | #define TRANS_SQD2RTS 11 /* error if not drained */ | ||
58 | |||
59 | /* | ||
60 | * Convert the AETH credit code into the number of credits. | ||
61 | */ | ||
62 | static u32 credit_table[31] = { | ||
63 | 0, /* 0 */ | ||
64 | 1, /* 1 */ | ||
65 | 2, /* 2 */ | ||
66 | 3, /* 3 */ | ||
67 | 4, /* 4 */ | ||
68 | 6, /* 5 */ | ||
69 | 8, /* 6 */ | ||
70 | 12, /* 7 */ | ||
71 | 16, /* 8 */ | ||
72 | 24, /* 9 */ | ||
73 | 32, /* A */ | ||
74 | 48, /* B */ | ||
75 | 64, /* C */ | ||
76 | 96, /* D */ | ||
77 | 128, /* E */ | ||
78 | 192, /* F */ | ||
79 | 256, /* 10 */ | ||
80 | 384, /* 11 */ | ||
81 | 512, /* 12 */ | ||
82 | 768, /* 13 */ | ||
83 | 1024, /* 14 */ | ||
84 | 1536, /* 15 */ | ||
85 | 2048, /* 16 */ | ||
86 | 3072, /* 17 */ | ||
87 | 4096, /* 18 */ | ||
88 | 6144, /* 19 */ | ||
89 | 8192, /* 1A */ | ||
90 | 12288, /* 1B */ | ||
91 | 16384, /* 1C */ | ||
92 | 24576, /* 1D */ | ||
93 | 32768 /* 1E */ | ||
94 | }; | ||
95 | |||
96 | static u32 alloc_qpn(struct ipath_qp_table *qpt) | ||
97 | { | ||
98 | u32 i, offset, max_scan, qpn; | ||
99 | struct qpn_map *map; | ||
100 | u32 ret; | ||
101 | |||
102 | qpn = qpt->last + 1; | ||
103 | if (qpn >= QPN_MAX) | ||
104 | qpn = 2; | ||
105 | offset = qpn & BITS_PER_PAGE_MASK; | ||
106 | map = &qpt->map[qpn / BITS_PER_PAGE]; | ||
107 | max_scan = qpt->nmaps - !offset; | ||
108 | for (i = 0;;) { | ||
109 | if (unlikely(!map->page)) { | ||
110 | unsigned long page = get_zeroed_page(GFP_KERNEL); | ||
111 | unsigned long flags; | ||
112 | |||
113 | /* | ||
114 | * Free the page if someone raced with us | ||
115 | * installing it: | ||
116 | */ | ||
117 | spin_lock_irqsave(&qpt->lock, flags); | ||
118 | if (map->page) | ||
119 | free_page(page); | ||
120 | else | ||
121 | map->page = (void *)page; | ||
122 | spin_unlock_irqrestore(&qpt->lock, flags); | ||
123 | if (unlikely(!map->page)) | ||
124 | break; | ||
125 | } | ||
126 | if (likely(atomic_read(&map->n_free))) { | ||
127 | do { | ||
128 | if (!test_and_set_bit(offset, map->page)) { | ||
129 | atomic_dec(&map->n_free); | ||
130 | qpt->last = qpn; | ||
131 | ret = qpn; | ||
132 | goto bail; | ||
133 | } | ||
134 | offset = find_next_offset(map, offset); | ||
135 | qpn = mk_qpn(qpt, map, offset); | ||
136 | /* | ||
137 | * This test differs from alloc_pidmap(). | ||
138 | * If find_next_offset() does find a zero | ||
139 | * bit, we don't need to check for QPN | ||
140 | * wrapping around past our starting QPN. | ||
141 | * We just need to be sure we don't loop | ||
142 | * forever. | ||
143 | */ | ||
144 | } while (offset < BITS_PER_PAGE && qpn < QPN_MAX); | ||
145 | } | ||
146 | /* | ||
147 | * In order to keep the number of pages allocated to a | ||
148 | * minimum, we scan the all existing pages before increasing | ||
149 | * the size of the bitmap table. | ||
150 | */ | ||
151 | if (++i > max_scan) { | ||
152 | if (qpt->nmaps == QPNMAP_ENTRIES) | ||
153 | break; | ||
154 | map = &qpt->map[qpt->nmaps++]; | ||
155 | offset = 0; | ||
156 | } else if (map < &qpt->map[qpt->nmaps]) { | ||
157 | ++map; | ||
158 | offset = 0; | ||
159 | } else { | ||
160 | map = &qpt->map[0]; | ||
161 | offset = 2; | ||
162 | } | ||
163 | qpn = mk_qpn(qpt, map, offset); | ||
164 | } | ||
165 | |||
166 | ret = 0; | ||
167 | |||
168 | bail: | ||
169 | return ret; | ||
170 | } | ||
171 | |||
172 | static void free_qpn(struct ipath_qp_table *qpt, u32 qpn) | ||
173 | { | ||
174 | struct qpn_map *map; | ||
175 | |||
176 | map = qpt->map + qpn / BITS_PER_PAGE; | ||
177 | if (map->page) | ||
178 | clear_bit(qpn & BITS_PER_PAGE_MASK, map->page); | ||
179 | atomic_inc(&map->n_free); | ||
180 | } | ||
181 | |||
182 | /** | ||
183 | * ipath_alloc_qpn - allocate a QP number | ||
184 | * @qpt: the QP table | ||
185 | * @qp: the QP | ||
186 | * @type: the QP type (IB_QPT_SMI and IB_QPT_GSI are special) | ||
187 | * | ||
188 | * Allocate the next available QPN and put the QP into the hash table. | ||
189 | * The hash table holds a reference to the QP. | ||
190 | */ | ||
191 | static int ipath_alloc_qpn(struct ipath_qp_table *qpt, struct ipath_qp *qp, | ||
192 | enum ib_qp_type type) | ||
193 | { | ||
194 | unsigned long flags; | ||
195 | u32 qpn; | ||
196 | int ret; | ||
197 | |||
198 | if (type == IB_QPT_SMI) | ||
199 | qpn = 0; | ||
200 | else if (type == IB_QPT_GSI) | ||
201 | qpn = 1; | ||
202 | else { | ||
203 | /* Allocate the next available QPN */ | ||
204 | qpn = alloc_qpn(qpt); | ||
205 | if (qpn == 0) { | ||
206 | ret = -ENOMEM; | ||
207 | goto bail; | ||
208 | } | ||
209 | } | ||
210 | qp->ibqp.qp_num = qpn; | ||
211 | |||
212 | /* Add the QP to the hash table. */ | ||
213 | spin_lock_irqsave(&qpt->lock, flags); | ||
214 | |||
215 | qpn %= qpt->max; | ||
216 | qp->next = qpt->table[qpn]; | ||
217 | qpt->table[qpn] = qp; | ||
218 | atomic_inc(&qp->refcount); | ||
219 | |||
220 | spin_unlock_irqrestore(&qpt->lock, flags); | ||
221 | ret = 0; | ||
222 | |||
223 | bail: | ||
224 | return ret; | ||
225 | } | ||
226 | |||
227 | /** | ||
228 | * ipath_free_qp - remove a QP from the QP table | ||
229 | * @qpt: the QP table | ||
230 | * @qp: the QP to remove | ||
231 | * | ||
232 | * Remove the QP from the table so it can't be found asynchronously by | ||
233 | * the receive interrupt routine. | ||
234 | */ | ||
235 | static void ipath_free_qp(struct ipath_qp_table *qpt, struct ipath_qp *qp) | ||
236 | { | ||
237 | struct ipath_qp *q, **qpp; | ||
238 | unsigned long flags; | ||
239 | int fnd = 0; | ||
240 | |||
241 | spin_lock_irqsave(&qpt->lock, flags); | ||
242 | |||
243 | /* Remove QP from the hash table. */ | ||
244 | qpp = &qpt->table[qp->ibqp.qp_num % qpt->max]; | ||
245 | for (; (q = *qpp) != NULL; qpp = &q->next) { | ||
246 | if (q == qp) { | ||
247 | *qpp = qp->next; | ||
248 | qp->next = NULL; | ||
249 | atomic_dec(&qp->refcount); | ||
250 | fnd = 1; | ||
251 | break; | ||
252 | } | ||
253 | } | ||
254 | |||
255 | spin_unlock_irqrestore(&qpt->lock, flags); | ||
256 | |||
257 | if (!fnd) | ||
258 | return; | ||
259 | |||
260 | /* If QPN is not reserved, mark QPN free in the bitmap. */ | ||
261 | if (qp->ibqp.qp_num > 1) | ||
262 | free_qpn(qpt, qp->ibqp.qp_num); | ||
263 | |||
264 | wait_event(qp->wait, !atomic_read(&qp->refcount)); | ||
265 | } | ||
266 | |||
267 | /** | ||
268 | * ipath_free_all_qps - remove all QPs from the table | ||
269 | * @qpt: the QP table to empty | ||
270 | */ | ||
271 | void ipath_free_all_qps(struct ipath_qp_table *qpt) | ||
272 | { | ||
273 | unsigned long flags; | ||
274 | struct ipath_qp *qp, *nqp; | ||
275 | u32 n; | ||
276 | |||
277 | for (n = 0; n < qpt->max; n++) { | ||
278 | spin_lock_irqsave(&qpt->lock, flags); | ||
279 | qp = qpt->table[n]; | ||
280 | qpt->table[n] = NULL; | ||
281 | spin_unlock_irqrestore(&qpt->lock, flags); | ||
282 | |||
283 | while (qp) { | ||
284 | nqp = qp->next; | ||
285 | if (qp->ibqp.qp_num > 1) | ||
286 | free_qpn(qpt, qp->ibqp.qp_num); | ||
287 | if (!atomic_dec_and_test(&qp->refcount) || | ||
288 | !ipath_destroy_qp(&qp->ibqp)) | ||
289 | _VERBS_INFO("QP memory leak!\n"); | ||
290 | qp = nqp; | ||
291 | } | ||
292 | } | ||
293 | |||
294 | for (n = 0; n < ARRAY_SIZE(qpt->map); n++) { | ||
295 | if (qpt->map[n].page) | ||
296 | free_page((unsigned long)qpt->map[n].page); | ||
297 | } | ||
298 | } | ||
299 | |||
300 | /** | ||
301 | * ipath_lookup_qpn - return the QP with the given QPN | ||
302 | * @qpt: the QP table | ||
303 | * @qpn: the QP number to look up | ||
304 | * | ||
305 | * The caller is responsible for decrementing the QP reference count | ||
306 | * when done. | ||
307 | */ | ||
308 | struct ipath_qp *ipath_lookup_qpn(struct ipath_qp_table *qpt, u32 qpn) | ||
309 | { | ||
310 | unsigned long flags; | ||
311 | struct ipath_qp *qp; | ||
312 | |||
313 | spin_lock_irqsave(&qpt->lock, flags); | ||
314 | |||
315 | for (qp = qpt->table[qpn % qpt->max]; qp; qp = qp->next) { | ||
316 | if (qp->ibqp.qp_num == qpn) { | ||
317 | atomic_inc(&qp->refcount); | ||
318 | break; | ||
319 | } | ||
320 | } | ||
321 | |||
322 | spin_unlock_irqrestore(&qpt->lock, flags); | ||
323 | return qp; | ||
324 | } | ||
325 | |||
326 | /** | ||
327 | * ipath_reset_qp - initialize the QP state to the reset state | ||
328 | * @qp: the QP to reset | ||
329 | */ | ||
330 | static void ipath_reset_qp(struct ipath_qp *qp) | ||
331 | { | ||
332 | qp->remote_qpn = 0; | ||
333 | qp->qkey = 0; | ||
334 | qp->qp_access_flags = 0; | ||
335 | qp->s_hdrwords = 0; | ||
336 | qp->s_psn = 0; | ||
337 | qp->r_psn = 0; | ||
338 | atomic_set(&qp->msn, 0); | ||
339 | if (qp->ibqp.qp_type == IB_QPT_RC) { | ||
340 | qp->s_state = IB_OPCODE_RC_SEND_LAST; | ||
341 | qp->r_state = IB_OPCODE_RC_SEND_LAST; | ||
342 | } else { | ||
343 | qp->s_state = IB_OPCODE_UC_SEND_LAST; | ||
344 | qp->r_state = IB_OPCODE_UC_SEND_LAST; | ||
345 | } | ||
346 | qp->s_ack_state = IB_OPCODE_RC_ACKNOWLEDGE; | ||
347 | qp->s_nak_state = 0; | ||
348 | qp->s_rnr_timeout = 0; | ||
349 | qp->s_head = 0; | ||
350 | qp->s_tail = 0; | ||
351 | qp->s_cur = 0; | ||
352 | qp->s_last = 0; | ||
353 | qp->s_ssn = 1; | ||
354 | qp->s_lsn = 0; | ||
355 | qp->r_rq.head = 0; | ||
356 | qp->r_rq.tail = 0; | ||
357 | qp->r_reuse_sge = 0; | ||
358 | } | ||
359 | |||
360 | /** | ||
361 | * ipath_error_qp - put a QP into an error state | ||
362 | * @qp: the QP to put into an error state | ||
363 | * | ||
364 | * Flushes both send and receive work queues. | ||
365 | * QP r_rq.lock and s_lock should be held. | ||
366 | */ | ||
367 | |||
368 | static void ipath_error_qp(struct ipath_qp *qp) | ||
369 | { | ||
370 | struct ipath_ibdev *dev = to_idev(qp->ibqp.device); | ||
371 | struct ib_wc wc; | ||
372 | |||
373 | _VERBS_INFO("QP%d/%d in error state\n", | ||
374 | qp->ibqp.qp_num, qp->remote_qpn); | ||
375 | |||
376 | spin_lock(&dev->pending_lock); | ||
377 | /* XXX What if its already removed by the timeout code? */ | ||
378 | if (qp->timerwait.next != LIST_POISON1) | ||
379 | list_del(&qp->timerwait); | ||
380 | if (qp->piowait.next != LIST_POISON1) | ||
381 | list_del(&qp->piowait); | ||
382 | spin_unlock(&dev->pending_lock); | ||
383 | |||
384 | wc.status = IB_WC_WR_FLUSH_ERR; | ||
385 | wc.vendor_err = 0; | ||
386 | wc.byte_len = 0; | ||
387 | wc.imm_data = 0; | ||
388 | wc.qp_num = qp->ibqp.qp_num; | ||
389 | wc.src_qp = 0; | ||
390 | wc.wc_flags = 0; | ||
391 | wc.pkey_index = 0; | ||
392 | wc.slid = 0; | ||
393 | wc.sl = 0; | ||
394 | wc.dlid_path_bits = 0; | ||
395 | wc.port_num = 0; | ||
396 | |||
397 | while (qp->s_last != qp->s_head) { | ||
398 | struct ipath_swqe *wqe = get_swqe_ptr(qp, qp->s_last); | ||
399 | |||
400 | wc.wr_id = wqe->wr.wr_id; | ||
401 | wc.opcode = ib_ipath_wc_opcode[wqe->wr.opcode]; | ||
402 | if (++qp->s_last >= qp->s_size) | ||
403 | qp->s_last = 0; | ||
404 | ipath_cq_enter(to_icq(qp->ibqp.send_cq), &wc, 1); | ||
405 | } | ||
406 | qp->s_cur = qp->s_tail = qp->s_head; | ||
407 | qp->s_hdrwords = 0; | ||
408 | qp->s_ack_state = IB_OPCODE_RC_ACKNOWLEDGE; | ||
409 | |||
410 | wc.opcode = IB_WC_RECV; | ||
411 | while (qp->r_rq.tail != qp->r_rq.head) { | ||
412 | wc.wr_id = get_rwqe_ptr(&qp->r_rq, qp->r_rq.tail)->wr_id; | ||
413 | if (++qp->r_rq.tail >= qp->r_rq.size) | ||
414 | qp->r_rq.tail = 0; | ||
415 | ipath_cq_enter(to_icq(qp->ibqp.recv_cq), &wc, 1); | ||
416 | } | ||
417 | } | ||
418 | |||
419 | /** | ||
420 | * ipath_modify_qp - modify the attributes of a queue pair | ||
421 | * @ibqp: the queue pair who's attributes we're modifying | ||
422 | * @attr: the new attributes | ||
423 | * @attr_mask: the mask of attributes to modify | ||
424 | * | ||
425 | * Returns 0 on success, otherwise returns an errno. | ||
426 | */ | ||
427 | int ipath_modify_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr, | ||
428 | int attr_mask) | ||
429 | { | ||
430 | struct ipath_qp *qp = to_iqp(ibqp); | ||
431 | enum ib_qp_state cur_state, new_state; | ||
432 | unsigned long flags; | ||
433 | int ret; | ||
434 | |||
435 | spin_lock_irqsave(&qp->r_rq.lock, flags); | ||
436 | spin_lock(&qp->s_lock); | ||
437 | |||
438 | cur_state = attr_mask & IB_QP_CUR_STATE ? | ||
439 | attr->cur_qp_state : qp->state; | ||
440 | new_state = attr_mask & IB_QP_STATE ? attr->qp_state : cur_state; | ||
441 | |||
442 | if (!ib_modify_qp_is_ok(cur_state, new_state, ibqp->qp_type, | ||
443 | attr_mask)) | ||
444 | goto inval; | ||
445 | |||
446 | switch (new_state) { | ||
447 | case IB_QPS_RESET: | ||
448 | ipath_reset_qp(qp); | ||
449 | break; | ||
450 | |||
451 | case IB_QPS_ERR: | ||
452 | ipath_error_qp(qp); | ||
453 | break; | ||
454 | |||
455 | default: | ||
456 | break; | ||
457 | |||
458 | } | ||
459 | |||
460 | if (attr_mask & IB_QP_PKEY_INDEX) { | ||
461 | struct ipath_ibdev *dev = to_idev(ibqp->device); | ||
462 | |||
463 | if (attr->pkey_index >= ipath_layer_get_npkeys(dev->dd)) | ||
464 | goto inval; | ||
465 | qp->s_pkey_index = attr->pkey_index; | ||
466 | } | ||
467 | |||
468 | if (attr_mask & IB_QP_DEST_QPN) | ||
469 | qp->remote_qpn = attr->dest_qp_num; | ||
470 | |||
471 | if (attr_mask & IB_QP_SQ_PSN) { | ||
472 | qp->s_next_psn = attr->sq_psn; | ||
473 | qp->s_last_psn = qp->s_next_psn - 1; | ||
474 | } | ||
475 | |||
476 | if (attr_mask & IB_QP_RQ_PSN) | ||
477 | qp->r_psn = attr->rq_psn; | ||
478 | |||
479 | if (attr_mask & IB_QP_ACCESS_FLAGS) | ||
480 | qp->qp_access_flags = attr->qp_access_flags; | ||
481 | |||
482 | if (attr_mask & IB_QP_AV) { | ||
483 | if (attr->ah_attr.dlid == 0 || | ||
484 | attr->ah_attr.dlid >= IPS_MULTICAST_LID_BASE) | ||
485 | goto inval; | ||
486 | qp->remote_ah_attr = attr->ah_attr; | ||
487 | } | ||
488 | |||
489 | if (attr_mask & IB_QP_PATH_MTU) | ||
490 | qp->path_mtu = attr->path_mtu; | ||
491 | |||
492 | if (attr_mask & IB_QP_RETRY_CNT) | ||
493 | qp->s_retry = qp->s_retry_cnt = attr->retry_cnt; | ||
494 | |||
495 | if (attr_mask & IB_QP_RNR_RETRY) { | ||
496 | qp->s_rnr_retry = attr->rnr_retry; | ||
497 | if (qp->s_rnr_retry > 7) | ||
498 | qp->s_rnr_retry = 7; | ||
499 | qp->s_rnr_retry_cnt = qp->s_rnr_retry; | ||
500 | } | ||
501 | |||
502 | if (attr_mask & IB_QP_MIN_RNR_TIMER) { | ||
503 | if (attr->min_rnr_timer > 31) | ||
504 | goto inval; | ||
505 | qp->s_min_rnr_timer = attr->min_rnr_timer; | ||
506 | } | ||
507 | |||
508 | if (attr_mask & IB_QP_QKEY) | ||
509 | qp->qkey = attr->qkey; | ||
510 | |||
511 | if (attr_mask & IB_QP_PKEY_INDEX) | ||
512 | qp->s_pkey_index = attr->pkey_index; | ||
513 | |||
514 | qp->state = new_state; | ||
515 | spin_unlock(&qp->s_lock); | ||
516 | spin_unlock_irqrestore(&qp->r_rq.lock, flags); | ||
517 | |||
518 | /* | ||
519 | * If QP1 changed to the RTS state, try to move to the link to INIT | ||
520 | * even if it was ACTIVE so the SM will reinitialize the SMA's | ||
521 | * state. | ||
522 | */ | ||
523 | if (qp->ibqp.qp_num == 1 && new_state == IB_QPS_RTS) { | ||
524 | struct ipath_ibdev *dev = to_idev(ibqp->device); | ||
525 | |||
526 | ipath_layer_set_linkstate(dev->dd, IPATH_IB_LINKDOWN); | ||
527 | } | ||
528 | ret = 0; | ||
529 | goto bail; | ||
530 | |||
531 | inval: | ||
532 | spin_unlock(&qp->s_lock); | ||
533 | spin_unlock_irqrestore(&qp->r_rq.lock, flags); | ||
534 | ret = -EINVAL; | ||
535 | |||
536 | bail: | ||
537 | return ret; | ||
538 | } | ||
539 | |||
540 | int ipath_query_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr, | ||
541 | int attr_mask, struct ib_qp_init_attr *init_attr) | ||
542 | { | ||
543 | struct ipath_qp *qp = to_iqp(ibqp); | ||
544 | |||
545 | attr->qp_state = qp->state; | ||
546 | attr->cur_qp_state = attr->qp_state; | ||
547 | attr->path_mtu = qp->path_mtu; | ||
548 | attr->path_mig_state = 0; | ||
549 | attr->qkey = qp->qkey; | ||
550 | attr->rq_psn = qp->r_psn; | ||
551 | attr->sq_psn = qp->s_next_psn; | ||
552 | attr->dest_qp_num = qp->remote_qpn; | ||
553 | attr->qp_access_flags = qp->qp_access_flags; | ||
554 | attr->cap.max_send_wr = qp->s_size - 1; | ||
555 | attr->cap.max_recv_wr = qp->r_rq.size - 1; | ||
556 | attr->cap.max_send_sge = qp->s_max_sge; | ||
557 | attr->cap.max_recv_sge = qp->r_rq.max_sge; | ||
558 | attr->cap.max_inline_data = 0; | ||
559 | attr->ah_attr = qp->remote_ah_attr; | ||
560 | memset(&attr->alt_ah_attr, 0, sizeof(attr->alt_ah_attr)); | ||
561 | attr->pkey_index = qp->s_pkey_index; | ||
562 | attr->alt_pkey_index = 0; | ||
563 | attr->en_sqd_async_notify = 0; | ||
564 | attr->sq_draining = 0; | ||
565 | attr->max_rd_atomic = 1; | ||
566 | attr->max_dest_rd_atomic = 1; | ||
567 | attr->min_rnr_timer = qp->s_min_rnr_timer; | ||
568 | attr->port_num = 1; | ||
569 | attr->timeout = 0; | ||
570 | attr->retry_cnt = qp->s_retry_cnt; | ||
571 | attr->rnr_retry = qp->s_rnr_retry; | ||
572 | attr->alt_port_num = 0; | ||
573 | attr->alt_timeout = 0; | ||
574 | |||
575 | init_attr->event_handler = qp->ibqp.event_handler; | ||
576 | init_attr->qp_context = qp->ibqp.qp_context; | ||
577 | init_attr->send_cq = qp->ibqp.send_cq; | ||
578 | init_attr->recv_cq = qp->ibqp.recv_cq; | ||
579 | init_attr->srq = qp->ibqp.srq; | ||
580 | init_attr->cap = attr->cap; | ||
581 | init_attr->sq_sig_type = | ||
582 | (qp->s_flags & (1 << IPATH_S_SIGNAL_REQ_WR)) | ||
583 | ? IB_SIGNAL_REQ_WR : 0; | ||
584 | init_attr->qp_type = qp->ibqp.qp_type; | ||
585 | init_attr->port_num = 1; | ||
586 | return 0; | ||
587 | } | ||
588 | |||
589 | /** | ||
590 | * ipath_compute_aeth - compute the AETH (syndrome + MSN) | ||
591 | * @qp: the queue pair to compute the AETH for | ||
592 | * | ||
593 | * Returns the AETH. | ||
594 | * | ||
595 | * The QP s_lock should be held. | ||
596 | */ | ||
597 | __be32 ipath_compute_aeth(struct ipath_qp *qp) | ||
598 | { | ||
599 | u32 aeth = atomic_read(&qp->msn) & IPS_MSN_MASK; | ||
600 | |||
601 | if (qp->s_nak_state) { | ||
602 | aeth |= qp->s_nak_state << IPS_AETH_CREDIT_SHIFT; | ||
603 | } else if (qp->ibqp.srq) { | ||
604 | /* | ||
605 | * Shared receive queues don't generate credits. | ||
606 | * Set the credit field to the invalid value. | ||
607 | */ | ||
608 | aeth |= IPS_AETH_CREDIT_INVAL << IPS_AETH_CREDIT_SHIFT; | ||
609 | } else { | ||
610 | u32 min, max, x; | ||
611 | u32 credits; | ||
612 | |||
613 | /* | ||
614 | * Compute the number of credits available (RWQEs). | ||
615 | * XXX Not holding the r_rq.lock here so there is a small | ||
616 | * chance that the pair of reads are not atomic. | ||
617 | */ | ||
618 | credits = qp->r_rq.head - qp->r_rq.tail; | ||
619 | if ((int)credits < 0) | ||
620 | credits += qp->r_rq.size; | ||
621 | /* | ||
622 | * Binary search the credit table to find the code to | ||
623 | * use. | ||
624 | */ | ||
625 | min = 0; | ||
626 | max = 31; | ||
627 | for (;;) { | ||
628 | x = (min + max) / 2; | ||
629 | if (credit_table[x] == credits) | ||
630 | break; | ||
631 | if (credit_table[x] > credits) | ||
632 | max = x; | ||
633 | else if (min == x) | ||
634 | break; | ||
635 | else | ||
636 | min = x; | ||
637 | } | ||
638 | aeth |= x << IPS_AETH_CREDIT_SHIFT; | ||
639 | } | ||
640 | return cpu_to_be32(aeth); | ||
641 | } | ||
642 | |||
643 | /** | ||
644 | * ipath_create_qp - create a queue pair for a device | ||
645 | * @ibpd: the protection domain who's device we create the queue pair for | ||
646 | * @init_attr: the attributes of the queue pair | ||
647 | * @udata: unused by InfiniPath | ||
648 | * | ||
649 | * Returns the queue pair on success, otherwise returns an errno. | ||
650 | * | ||
651 | * Called by the ib_create_qp() core verbs function. | ||
652 | */ | ||
653 | struct ib_qp *ipath_create_qp(struct ib_pd *ibpd, | ||
654 | struct ib_qp_init_attr *init_attr, | ||
655 | struct ib_udata *udata) | ||
656 | { | ||
657 | struct ipath_qp *qp; | ||
658 | int err; | ||
659 | struct ipath_swqe *swq = NULL; | ||
660 | struct ipath_ibdev *dev; | ||
661 | size_t sz; | ||
662 | struct ib_qp *ret; | ||
663 | |||
664 | if (init_attr->cap.max_send_sge > 255 || | ||
665 | init_attr->cap.max_recv_sge > 255) { | ||
666 | ret = ERR_PTR(-ENOMEM); | ||
667 | goto bail; | ||
668 | } | ||
669 | |||
670 | switch (init_attr->qp_type) { | ||
671 | case IB_QPT_UC: | ||
672 | case IB_QPT_RC: | ||
673 | sz = sizeof(struct ipath_sge) * | ||
674 | init_attr->cap.max_send_sge + | ||
675 | sizeof(struct ipath_swqe); | ||
676 | swq = vmalloc((init_attr->cap.max_send_wr + 1) * sz); | ||
677 | if (swq == NULL) { | ||
678 | ret = ERR_PTR(-ENOMEM); | ||
679 | goto bail; | ||
680 | } | ||
681 | /* FALLTHROUGH */ | ||
682 | case IB_QPT_UD: | ||
683 | case IB_QPT_SMI: | ||
684 | case IB_QPT_GSI: | ||
685 | qp = kmalloc(sizeof(*qp), GFP_KERNEL); | ||
686 | if (!qp) { | ||
687 | ret = ERR_PTR(-ENOMEM); | ||
688 | goto bail; | ||
689 | } | ||
690 | qp->r_rq.size = init_attr->cap.max_recv_wr + 1; | ||
691 | sz = sizeof(struct ipath_sge) * | ||
692 | init_attr->cap.max_recv_sge + | ||
693 | sizeof(struct ipath_rwqe); | ||
694 | qp->r_rq.wq = vmalloc(qp->r_rq.size * sz); | ||
695 | if (!qp->r_rq.wq) { | ||
696 | kfree(qp); | ||
697 | ret = ERR_PTR(-ENOMEM); | ||
698 | goto bail; | ||
699 | } | ||
700 | |||
701 | /* | ||
702 | * ib_create_qp() will initialize qp->ibqp | ||
703 | * except for qp->ibqp.qp_num. | ||
704 | */ | ||
705 | spin_lock_init(&qp->s_lock); | ||
706 | spin_lock_init(&qp->r_rq.lock); | ||
707 | atomic_set(&qp->refcount, 0); | ||
708 | init_waitqueue_head(&qp->wait); | ||
709 | tasklet_init(&qp->s_task, | ||
710 | init_attr->qp_type == IB_QPT_RC ? | ||
711 | ipath_do_rc_send : ipath_do_uc_send, | ||
712 | (unsigned long)qp); | ||
713 | qp->piowait.next = LIST_POISON1; | ||
714 | qp->piowait.prev = LIST_POISON2; | ||
715 | qp->timerwait.next = LIST_POISON1; | ||
716 | qp->timerwait.prev = LIST_POISON2; | ||
717 | qp->state = IB_QPS_RESET; | ||
718 | qp->s_wq = swq; | ||
719 | qp->s_size = init_attr->cap.max_send_wr + 1; | ||
720 | qp->s_max_sge = init_attr->cap.max_send_sge; | ||
721 | qp->r_rq.max_sge = init_attr->cap.max_recv_sge; | ||
722 | qp->s_flags = init_attr->sq_sig_type == IB_SIGNAL_REQ_WR ? | ||
723 | 1 << IPATH_S_SIGNAL_REQ_WR : 0; | ||
724 | dev = to_idev(ibpd->device); | ||
725 | err = ipath_alloc_qpn(&dev->qp_table, qp, | ||
726 | init_attr->qp_type); | ||
727 | if (err) { | ||
728 | vfree(swq); | ||
729 | vfree(qp->r_rq.wq); | ||
730 | kfree(qp); | ||
731 | ret = ERR_PTR(err); | ||
732 | goto bail; | ||
733 | } | ||
734 | ipath_reset_qp(qp); | ||
735 | |||
736 | /* Tell the core driver that the kernel SMA is present. */ | ||
737 | if (qp->ibqp.qp_type == IB_QPT_SMI) | ||
738 | ipath_layer_set_verbs_flags(dev->dd, | ||
739 | IPATH_VERBS_KERNEL_SMA); | ||
740 | break; | ||
741 | |||
742 | default: | ||
743 | /* Don't support raw QPs */ | ||
744 | ret = ERR_PTR(-ENOSYS); | ||
745 | goto bail; | ||
746 | } | ||
747 | |||
748 | init_attr->cap.max_inline_data = 0; | ||
749 | |||
750 | ret = &qp->ibqp; | ||
751 | |||
752 | bail: | ||
753 | return ret; | ||
754 | } | ||
755 | |||
756 | /** | ||
757 | * ipath_destroy_qp - destroy a queue pair | ||
758 | * @ibqp: the queue pair to destroy | ||
759 | * | ||
760 | * Returns 0 on success. | ||
761 | * | ||
762 | * Note that this can be called while the QP is actively sending or | ||
763 | * receiving! | ||
764 | */ | ||
765 | int ipath_destroy_qp(struct ib_qp *ibqp) | ||
766 | { | ||
767 | struct ipath_qp *qp = to_iqp(ibqp); | ||
768 | struct ipath_ibdev *dev = to_idev(ibqp->device); | ||
769 | unsigned long flags; | ||
770 | |||
771 | /* Tell the core driver that the kernel SMA is gone. */ | ||
772 | if (qp->ibqp.qp_type == IB_QPT_SMI) | ||
773 | ipath_layer_set_verbs_flags(dev->dd, 0); | ||
774 | |||
775 | spin_lock_irqsave(&qp->r_rq.lock, flags); | ||
776 | spin_lock(&qp->s_lock); | ||
777 | qp->state = IB_QPS_ERR; | ||
778 | spin_unlock(&qp->s_lock); | ||
779 | spin_unlock_irqrestore(&qp->r_rq.lock, flags); | ||
780 | |||
781 | /* Stop the sending tasklet. */ | ||
782 | tasklet_kill(&qp->s_task); | ||
783 | |||
784 | /* Make sure the QP isn't on the timeout list. */ | ||
785 | spin_lock_irqsave(&dev->pending_lock, flags); | ||
786 | if (qp->timerwait.next != LIST_POISON1) | ||
787 | list_del(&qp->timerwait); | ||
788 | if (qp->piowait.next != LIST_POISON1) | ||
789 | list_del(&qp->piowait); | ||
790 | spin_unlock_irqrestore(&dev->pending_lock, flags); | ||
791 | |||
792 | /* | ||
793 | * Make sure that the QP is not in the QPN table so receive | ||
794 | * interrupts will discard packets for this QP. XXX Also remove QP | ||
795 | * from multicast table. | ||
796 | */ | ||
797 | if (atomic_read(&qp->refcount) != 0) | ||
798 | ipath_free_qp(&dev->qp_table, qp); | ||
799 | |||
800 | vfree(qp->s_wq); | ||
801 | vfree(qp->r_rq.wq); | ||
802 | kfree(qp); | ||
803 | return 0; | ||
804 | } | ||
805 | |||
806 | /** | ||
807 | * ipath_init_qp_table - initialize the QP table for a device | ||
808 | * @idev: the device who's QP table we're initializing | ||
809 | * @size: the size of the QP table | ||
810 | * | ||
811 | * Returns 0 on success, otherwise returns an errno. | ||
812 | */ | ||
813 | int ipath_init_qp_table(struct ipath_ibdev *idev, int size) | ||
814 | { | ||
815 | int i; | ||
816 | int ret; | ||
817 | |||
818 | idev->qp_table.last = 1; /* QPN 0 and 1 are special. */ | ||
819 | idev->qp_table.max = size; | ||
820 | idev->qp_table.nmaps = 1; | ||
821 | idev->qp_table.table = kzalloc(size * sizeof(*idev->qp_table.table), | ||
822 | GFP_KERNEL); | ||
823 | if (idev->qp_table.table == NULL) { | ||
824 | ret = -ENOMEM; | ||
825 | goto bail; | ||
826 | } | ||
827 | |||
828 | for (i = 0; i < ARRAY_SIZE(idev->qp_table.map); i++) { | ||
829 | atomic_set(&idev->qp_table.map[i].n_free, BITS_PER_PAGE); | ||
830 | idev->qp_table.map[i].page = NULL; | ||
831 | } | ||
832 | |||
833 | ret = 0; | ||
834 | |||
835 | bail: | ||
836 | return ret; | ||
837 | } | ||
838 | |||
839 | /** | ||
840 | * ipath_sqerror_qp - put a QP's send queue into an error state | ||
841 | * @qp: QP who's send queue will be put into an error state | ||
842 | * @wc: the WC responsible for putting the QP in this state | ||
843 | * | ||
844 | * Flushes the send work queue. | ||
845 | * The QP s_lock should be held. | ||
846 | */ | ||
847 | |||
848 | void ipath_sqerror_qp(struct ipath_qp *qp, struct ib_wc *wc) | ||
849 | { | ||
850 | struct ipath_ibdev *dev = to_idev(qp->ibqp.device); | ||
851 | struct ipath_swqe *wqe = get_swqe_ptr(qp, qp->s_last); | ||
852 | |||
853 | _VERBS_INFO("Send queue error on QP%d/%d: err: %d\n", | ||
854 | qp->ibqp.qp_num, qp->remote_qpn, wc->status); | ||
855 | |||
856 | spin_lock(&dev->pending_lock); | ||
857 | /* XXX What if its already removed by the timeout code? */ | ||
858 | if (qp->timerwait.next != LIST_POISON1) | ||
859 | list_del(&qp->timerwait); | ||
860 | if (qp->piowait.next != LIST_POISON1) | ||
861 | list_del(&qp->piowait); | ||
862 | spin_unlock(&dev->pending_lock); | ||
863 | |||
864 | ipath_cq_enter(to_icq(qp->ibqp.send_cq), wc, 1); | ||
865 | if (++qp->s_last >= qp->s_size) | ||
866 | qp->s_last = 0; | ||
867 | |||
868 | wc->status = IB_WC_WR_FLUSH_ERR; | ||
869 | |||
870 | while (qp->s_last != qp->s_head) { | ||
871 | wc->wr_id = wqe->wr.wr_id; | ||
872 | wc->opcode = ib_ipath_wc_opcode[wqe->wr.opcode]; | ||
873 | ipath_cq_enter(to_icq(qp->ibqp.send_cq), wc, 1); | ||
874 | if (++qp->s_last >= qp->s_size) | ||
875 | qp->s_last = 0; | ||
876 | wqe = get_swqe_ptr(qp, qp->s_last); | ||
877 | } | ||
878 | qp->s_cur = qp->s_tail = qp->s_head; | ||
879 | qp->state = IB_QPS_SQE; | ||
880 | } | ||
881 | |||
882 | /** | ||
883 | * ipath_get_credit - flush the send work queue of a QP | ||
884 | * @qp: the qp who's send work queue to flush | ||
885 | * @aeth: the Acknowledge Extended Transport Header | ||
886 | * | ||
887 | * The QP s_lock should be held. | ||
888 | */ | ||
889 | void ipath_get_credit(struct ipath_qp *qp, u32 aeth) | ||
890 | { | ||
891 | u32 credit = (aeth >> IPS_AETH_CREDIT_SHIFT) & IPS_AETH_CREDIT_MASK; | ||
892 | |||
893 | /* | ||
894 | * If the credit is invalid, we can send | ||
895 | * as many packets as we like. Otherwise, we have to | ||
896 | * honor the credit field. | ||
897 | */ | ||
898 | if (credit == IPS_AETH_CREDIT_INVAL) { | ||
899 | qp->s_lsn = (u32) -1; | ||
900 | } else if (qp->s_lsn != (u32) -1) { | ||
901 | /* Compute new LSN (i.e., MSN + credit) */ | ||
902 | credit = (aeth + credit_table[credit]) & IPS_MSN_MASK; | ||
903 | if (ipath_cmp24(credit, qp->s_lsn) > 0) | ||
904 | qp->s_lsn = credit; | ||
905 | } | ||
906 | |||
907 | /* Restart sending if it was blocked due to lack of credits. */ | ||
908 | if (qp->s_cur != qp->s_head && | ||
909 | (qp->s_lsn == (u32) -1 || | ||
910 | ipath_cmp24(get_swqe_ptr(qp, qp->s_cur)->ssn, | ||
911 | qp->s_lsn + 1) <= 0)) | ||
912 | tasklet_hi_schedule(&qp->s_task); | ||
913 | } | ||