IB/ipath: misc infiniband code, part 2
[safe/jmp/linux-2.6] / drivers / infiniband / hw / ipath / ipath_qp.c
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 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 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_modify_qp - modify the attributes of a queue pair
362  * @ibqp: the queue pair who's attributes we're modifying
363  * @attr: the new attributes
364  * @attr_mask: the mask of attributes to modify
365  *
366  * Returns 0 on success, otherwise returns an errno.
367  */
368 int ipath_modify_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
369                     int attr_mask)
370 {
371         struct ipath_qp *qp = to_iqp(ibqp);
372         enum ib_qp_state cur_state, new_state;
373         unsigned long flags;
374         int ret;
375
376         spin_lock_irqsave(&qp->r_rq.lock, flags);
377         spin_lock(&qp->s_lock);
378
379         cur_state = attr_mask & IB_QP_CUR_STATE ?
380                 attr->cur_qp_state : qp->state;
381         new_state = attr_mask & IB_QP_STATE ? attr->qp_state : cur_state;
382
383         if (!ib_modify_qp_is_ok(cur_state, new_state, ibqp->qp_type,
384                                 attr_mask))
385                 goto inval;
386
387         switch (new_state) {
388         case IB_QPS_RESET:
389                 ipath_reset_qp(qp);
390                 break;
391
392         case IB_QPS_ERR:
393                 ipath_error_qp(qp);
394                 break;
395
396         default:
397                 break;
398
399         }
400
401         if (attr_mask & IB_QP_PKEY_INDEX) {
402                 struct ipath_ibdev *dev = to_idev(ibqp->device);
403
404                 if (attr->pkey_index >= ipath_layer_get_npkeys(dev->dd))
405                         goto inval;
406                 qp->s_pkey_index = attr->pkey_index;
407         }
408
409         if (attr_mask & IB_QP_DEST_QPN)
410                 qp->remote_qpn = attr->dest_qp_num;
411
412         if (attr_mask & IB_QP_SQ_PSN) {
413                 qp->s_next_psn = attr->sq_psn;
414                 qp->s_last_psn = qp->s_next_psn - 1;
415         }
416
417         if (attr_mask & IB_QP_RQ_PSN)
418                 qp->r_psn = attr->rq_psn;
419
420         if (attr_mask & IB_QP_ACCESS_FLAGS)
421                 qp->qp_access_flags = attr->qp_access_flags;
422
423         if (attr_mask & IB_QP_AV) {
424                 if (attr->ah_attr.dlid == 0 ||
425                     attr->ah_attr.dlid >= IPS_MULTICAST_LID_BASE)
426                         goto inval;
427                 qp->remote_ah_attr = attr->ah_attr;
428         }
429
430         if (attr_mask & IB_QP_PATH_MTU)
431                 qp->path_mtu = attr->path_mtu;
432
433         if (attr_mask & IB_QP_RETRY_CNT)
434                 qp->s_retry = qp->s_retry_cnt = attr->retry_cnt;
435
436         if (attr_mask & IB_QP_RNR_RETRY) {
437                 qp->s_rnr_retry = attr->rnr_retry;
438                 if (qp->s_rnr_retry > 7)
439                         qp->s_rnr_retry = 7;
440                 qp->s_rnr_retry_cnt = qp->s_rnr_retry;
441         }
442
443         if (attr_mask & IB_QP_MIN_RNR_TIMER) {
444                 if (attr->min_rnr_timer > 31)
445                         goto inval;
446                 qp->s_min_rnr_timer = attr->min_rnr_timer;
447         }
448
449         if (attr_mask & IB_QP_QKEY)
450                 qp->qkey = attr->qkey;
451
452         if (attr_mask & IB_QP_PKEY_INDEX)
453                 qp->s_pkey_index = attr->pkey_index;
454
455         qp->state = new_state;
456         spin_unlock(&qp->s_lock);
457         spin_unlock_irqrestore(&qp->r_rq.lock, flags);
458
459         /*
460          * If QP1 changed to the RTS state, try to move to the link to INIT
461          * even if it was ACTIVE so the SM will reinitialize the SMA's
462          * state.
463          */
464         if (qp->ibqp.qp_num == 1 && new_state == IB_QPS_RTS) {
465                 struct ipath_ibdev *dev = to_idev(ibqp->device);
466
467                 ipath_layer_set_linkstate(dev->dd, IPATH_IB_LINKDOWN);
468         }
469         ret = 0;
470         goto bail;
471
472 inval:
473         spin_unlock(&qp->s_lock);
474         spin_unlock_irqrestore(&qp->r_rq.lock, flags);
475         ret = -EINVAL;
476
477 bail:
478         return ret;
479 }
480
481 int ipath_query_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
482                    int attr_mask, struct ib_qp_init_attr *init_attr)
483 {
484         struct ipath_qp *qp = to_iqp(ibqp);
485
486         attr->qp_state = qp->state;
487         attr->cur_qp_state = attr->qp_state;
488         attr->path_mtu = qp->path_mtu;
489         attr->path_mig_state = 0;
490         attr->qkey = qp->qkey;
491         attr->rq_psn = qp->r_psn;
492         attr->sq_psn = qp->s_next_psn;
493         attr->dest_qp_num = qp->remote_qpn;
494         attr->qp_access_flags = qp->qp_access_flags;
495         attr->cap.max_send_wr = qp->s_size - 1;
496         attr->cap.max_recv_wr = qp->r_rq.size - 1;
497         attr->cap.max_send_sge = qp->s_max_sge;
498         attr->cap.max_recv_sge = qp->r_rq.max_sge;
499         attr->cap.max_inline_data = 0;
500         attr->ah_attr = qp->remote_ah_attr;
501         memset(&attr->alt_ah_attr, 0, sizeof(attr->alt_ah_attr));
502         attr->pkey_index = qp->s_pkey_index;
503         attr->alt_pkey_index = 0;
504         attr->en_sqd_async_notify = 0;
505         attr->sq_draining = 0;
506         attr->max_rd_atomic = 1;
507         attr->max_dest_rd_atomic = 1;
508         attr->min_rnr_timer = qp->s_min_rnr_timer;
509         attr->port_num = 1;
510         attr->timeout = 0;
511         attr->retry_cnt = qp->s_retry_cnt;
512         attr->rnr_retry = qp->s_rnr_retry;
513         attr->alt_port_num = 0;
514         attr->alt_timeout = 0;
515
516         init_attr->event_handler = qp->ibqp.event_handler;
517         init_attr->qp_context = qp->ibqp.qp_context;
518         init_attr->send_cq = qp->ibqp.send_cq;
519         init_attr->recv_cq = qp->ibqp.recv_cq;
520         init_attr->srq = qp->ibqp.srq;
521         init_attr->cap = attr->cap;
522         init_attr->sq_sig_type =
523                 (qp->s_flags & (1 << IPATH_S_SIGNAL_REQ_WR))
524                 ? IB_SIGNAL_REQ_WR : 0;
525         init_attr->qp_type = qp->ibqp.qp_type;
526         init_attr->port_num = 1;
527         return 0;
528 }
529
530 /**
531  * ipath_compute_aeth - compute the AETH (syndrome + MSN)
532  * @qp: the queue pair to compute the AETH for
533  *
534  * Returns the AETH.
535  *
536  * The QP s_lock should be held.
537  */
538 __be32 ipath_compute_aeth(struct ipath_qp *qp)
539 {
540         u32 aeth = atomic_read(&qp->msn) & IPS_MSN_MASK;
541
542         if (qp->s_nak_state) {
543                 aeth |= qp->s_nak_state << IPS_AETH_CREDIT_SHIFT;
544         } else if (qp->ibqp.srq) {
545                 /*
546                  * Shared receive queues don't generate credits.
547                  * Set the credit field to the invalid value.
548                  */
549                 aeth |= IPS_AETH_CREDIT_INVAL << IPS_AETH_CREDIT_SHIFT;
550         } else {
551                 u32 min, max, x;
552                 u32 credits;
553
554                 /*
555                  * Compute the number of credits available (RWQEs).
556                  * XXX Not holding the r_rq.lock here so there is a small
557                  * chance that the pair of reads are not atomic.
558                  */
559                 credits = qp->r_rq.head - qp->r_rq.tail;
560                 if ((int)credits < 0)
561                         credits += qp->r_rq.size;
562                 /*
563                  * Binary search the credit table to find the code to
564                  * use.
565                  */
566                 min = 0;
567                 max = 31;
568                 for (;;) {
569                         x = (min + max) / 2;
570                         if (credit_table[x] == credits)
571                                 break;
572                         if (credit_table[x] > credits)
573                                 max = x;
574                         else if (min == x)
575                                 break;
576                         else
577                                 min = x;
578                 }
579                 aeth |= x << IPS_AETH_CREDIT_SHIFT;
580         }
581         return cpu_to_be32(aeth);
582 }
583
584 /**
585  * ipath_create_qp - create a queue pair for a device
586  * @ibpd: the protection domain who's device we create the queue pair for
587  * @init_attr: the attributes of the queue pair
588  * @udata: unused by InfiniPath
589  *
590  * Returns the queue pair on success, otherwise returns an errno.
591  *
592  * Called by the ib_create_qp() core verbs function.
593  */
594 struct ib_qp *ipath_create_qp(struct ib_pd *ibpd,
595                               struct ib_qp_init_attr *init_attr,
596                               struct ib_udata *udata)
597 {
598         struct ipath_qp *qp;
599         int err;
600         struct ipath_swqe *swq = NULL;
601         struct ipath_ibdev *dev;
602         size_t sz;
603         struct ib_qp *ret;
604
605         if (init_attr->cap.max_send_sge > 255 ||
606             init_attr->cap.max_recv_sge > 255) {
607                 ret = ERR_PTR(-ENOMEM);
608                 goto bail;
609         }
610
611         switch (init_attr->qp_type) {
612         case IB_QPT_UC:
613         case IB_QPT_RC:
614                 sz = sizeof(struct ipath_sge) *
615                         init_attr->cap.max_send_sge +
616                         sizeof(struct ipath_swqe);
617                 swq = vmalloc((init_attr->cap.max_send_wr + 1) * sz);
618                 if (swq == NULL) {
619                         ret = ERR_PTR(-ENOMEM);
620                         goto bail;
621                 }
622                 /* FALLTHROUGH */
623         case IB_QPT_UD:
624         case IB_QPT_SMI:
625         case IB_QPT_GSI:
626                 qp = kmalloc(sizeof(*qp), GFP_KERNEL);
627                 if (!qp) {
628                         ret = ERR_PTR(-ENOMEM);
629                         goto bail;
630                 }
631                 qp->r_rq.size = init_attr->cap.max_recv_wr + 1;
632                 sz = sizeof(struct ipath_sge) *
633                         init_attr->cap.max_recv_sge +
634                         sizeof(struct ipath_rwqe);
635                 qp->r_rq.wq = vmalloc(qp->r_rq.size * sz);
636                 if (!qp->r_rq.wq) {
637                         kfree(qp);
638                         ret = ERR_PTR(-ENOMEM);
639                         goto bail;
640                 }
641
642                 /*
643                  * ib_create_qp() will initialize qp->ibqp
644                  * except for qp->ibqp.qp_num.
645                  */
646                 spin_lock_init(&qp->s_lock);
647                 spin_lock_init(&qp->r_rq.lock);
648                 atomic_set(&qp->refcount, 0);
649                 init_waitqueue_head(&qp->wait);
650                 tasklet_init(&qp->s_task,
651                              init_attr->qp_type == IB_QPT_RC ?
652                              ipath_do_rc_send : ipath_do_uc_send,
653                              (unsigned long)qp);
654                 qp->piowait.next = LIST_POISON1;
655                 qp->piowait.prev = LIST_POISON2;
656                 qp->timerwait.next = LIST_POISON1;
657                 qp->timerwait.prev = LIST_POISON2;
658                 qp->state = IB_QPS_RESET;
659                 qp->s_wq = swq;
660                 qp->s_size = init_attr->cap.max_send_wr + 1;
661                 qp->s_max_sge = init_attr->cap.max_send_sge;
662                 qp->r_rq.max_sge = init_attr->cap.max_recv_sge;
663                 qp->s_flags = init_attr->sq_sig_type == IB_SIGNAL_REQ_WR ?
664                         1 << IPATH_S_SIGNAL_REQ_WR : 0;
665                 dev = to_idev(ibpd->device);
666                 err = ipath_alloc_qpn(&dev->qp_table, qp,
667                                       init_attr->qp_type);
668                 if (err) {
669                         vfree(swq);
670                         vfree(qp->r_rq.wq);
671                         kfree(qp);
672                         ret = ERR_PTR(err);
673                         goto bail;
674                 }
675                 ipath_reset_qp(qp);
676
677                 /* Tell the core driver that the kernel SMA is present. */
678                 if (qp->ibqp.qp_type == IB_QPT_SMI)
679                         ipath_layer_set_verbs_flags(dev->dd,
680                                                     IPATH_VERBS_KERNEL_SMA);
681                 break;
682
683         default:
684                 /* Don't support raw QPs */
685                 ret = ERR_PTR(-ENOSYS);
686                 goto bail;
687         }
688
689         init_attr->cap.max_inline_data = 0;
690
691         ret = &qp->ibqp;
692
693 bail:
694         return ret;
695 }
696
697 /**
698  * ipath_destroy_qp - destroy a queue pair
699  * @ibqp: the queue pair to destroy
700  *
701  * Returns 0 on success.
702  *
703  * Note that this can be called while the QP is actively sending or
704  * receiving!
705  */
706 int ipath_destroy_qp(struct ib_qp *ibqp)
707 {
708         struct ipath_qp *qp = to_iqp(ibqp);
709         struct ipath_ibdev *dev = to_idev(ibqp->device);
710         unsigned long flags;
711
712         /* Tell the core driver that the kernel SMA is gone. */
713         if (qp->ibqp.qp_type == IB_QPT_SMI)
714                 ipath_layer_set_verbs_flags(dev->dd, 0);
715
716         spin_lock_irqsave(&qp->r_rq.lock, flags);
717         spin_lock(&qp->s_lock);
718         qp->state = IB_QPS_ERR;
719         spin_unlock(&qp->s_lock);
720         spin_unlock_irqrestore(&qp->r_rq.lock, flags);
721
722         /* Stop the sending tasklet. */
723         tasklet_kill(&qp->s_task);
724
725         /* Make sure the QP isn't on the timeout list. */
726         spin_lock_irqsave(&dev->pending_lock, flags);
727         if (qp->timerwait.next != LIST_POISON1)
728                 list_del(&qp->timerwait);
729         if (qp->piowait.next != LIST_POISON1)
730                 list_del(&qp->piowait);
731         spin_unlock_irqrestore(&dev->pending_lock, flags);
732
733         /*
734          * Make sure that the QP is not in the QPN table so receive
735          * interrupts will discard packets for this QP.  XXX Also remove QP
736          * from multicast table.
737          */
738         if (atomic_read(&qp->refcount) != 0)
739                 ipath_free_qp(&dev->qp_table, qp);
740
741         vfree(qp->s_wq);
742         vfree(qp->r_rq.wq);
743         kfree(qp);
744         return 0;
745 }
746
747 /**
748  * ipath_init_qp_table - initialize the QP table for a device
749  * @idev: the device who's QP table we're initializing
750  * @size: the size of the QP table
751  *
752  * Returns 0 on success, otherwise returns an errno.
753  */
754 int ipath_init_qp_table(struct ipath_ibdev *idev, int size)
755 {
756         int i;
757         int ret;
758
759         idev->qp_table.last = 1;        /* QPN 0 and 1 are special. */
760         idev->qp_table.max = size;
761         idev->qp_table.nmaps = 1;
762         idev->qp_table.table = kzalloc(size * sizeof(*idev->qp_table.table),
763                                        GFP_KERNEL);
764         if (idev->qp_table.table == NULL) {
765                 ret = -ENOMEM;
766                 goto bail;
767         }
768
769         for (i = 0; i < ARRAY_SIZE(idev->qp_table.map); i++) {
770                 atomic_set(&idev->qp_table.map[i].n_free, BITS_PER_PAGE);
771                 idev->qp_table.map[i].page = NULL;
772         }
773
774         ret = 0;
775
776 bail:
777         return ret;
778 }
779
780 /**
781  * ipath_sqerror_qp - put a QP's send queue into an error state
782  * @qp: QP who's send queue will be put into an error state
783  * @wc: the WC responsible for putting the QP in this state
784  *
785  * Flushes the send work queue.
786  * The QP s_lock should be held.
787  */
788
789 void ipath_sqerror_qp(struct ipath_qp *qp, struct ib_wc *wc)
790 {
791         struct ipath_ibdev *dev = to_idev(qp->ibqp.device);
792         struct ipath_swqe *wqe = get_swqe_ptr(qp, qp->s_last);
793
794         _VERBS_INFO("Send queue error on QP%d/%d: err: %d\n",
795                     qp->ibqp.qp_num, qp->remote_qpn, wc->status);
796
797         spin_lock(&dev->pending_lock);
798         /* XXX What if its already removed by the timeout code? */
799         if (qp->timerwait.next != LIST_POISON1)
800                 list_del(&qp->timerwait);
801         if (qp->piowait.next != LIST_POISON1)
802                 list_del(&qp->piowait);
803         spin_unlock(&dev->pending_lock);
804
805         ipath_cq_enter(to_icq(qp->ibqp.send_cq), wc, 1);
806         if (++qp->s_last >= qp->s_size)
807                 qp->s_last = 0;
808
809         wc->status = IB_WC_WR_FLUSH_ERR;
810
811         while (qp->s_last != qp->s_head) {
812                 wc->wr_id = wqe->wr.wr_id;
813                 wc->opcode = ib_ipath_wc_opcode[wqe->wr.opcode];
814                 ipath_cq_enter(to_icq(qp->ibqp.send_cq), wc, 1);
815                 if (++qp->s_last >= qp->s_size)
816                         qp->s_last = 0;
817                 wqe = get_swqe_ptr(qp, qp->s_last);
818         }
819         qp->s_cur = qp->s_tail = qp->s_head;
820         qp->state = IB_QPS_SQE;
821 }
822
823 /**
824  * ipath_error_qp - put a QP into an error state
825  * @qp: the QP to put into an error state
826  *
827  * Flushes both send and receive work queues.
828  * QP r_rq.lock and s_lock should be held.
829  */
830
831 void ipath_error_qp(struct ipath_qp *qp)
832 {
833         struct ipath_ibdev *dev = to_idev(qp->ibqp.device);
834         struct ib_wc wc;
835
836         _VERBS_INFO("QP%d/%d in error state\n",
837                     qp->ibqp.qp_num, qp->remote_qpn);
838
839         spin_lock(&dev->pending_lock);
840         /* XXX What if its already removed by the timeout code? */
841         if (qp->timerwait.next != LIST_POISON1)
842                 list_del(&qp->timerwait);
843         if (qp->piowait.next != LIST_POISON1)
844                 list_del(&qp->piowait);
845         spin_unlock(&dev->pending_lock);
846
847         wc.status = IB_WC_WR_FLUSH_ERR;
848         wc.vendor_err = 0;
849         wc.byte_len = 0;
850         wc.imm_data = 0;
851         wc.qp_num = qp->ibqp.qp_num;
852         wc.src_qp = 0;
853         wc.wc_flags = 0;
854         wc.pkey_index = 0;
855         wc.slid = 0;
856         wc.sl = 0;
857         wc.dlid_path_bits = 0;
858         wc.port_num = 0;
859
860         while (qp->s_last != qp->s_head) {
861                 struct ipath_swqe *wqe = get_swqe_ptr(qp, qp->s_last);
862
863                 wc.wr_id = wqe->wr.wr_id;
864                 wc.opcode = ib_ipath_wc_opcode[wqe->wr.opcode];
865                 if (++qp->s_last >= qp->s_size)
866                         qp->s_last = 0;
867                 ipath_cq_enter(to_icq(qp->ibqp.send_cq), &wc, 1);
868         }
869         qp->s_cur = qp->s_tail = qp->s_head;
870         qp->s_hdrwords = 0;
871         qp->s_ack_state = IB_OPCODE_RC_ACKNOWLEDGE;
872
873         wc.opcode = IB_WC_RECV;
874         while (qp->r_rq.tail != qp->r_rq.head) {
875                 wc.wr_id = get_rwqe_ptr(&qp->r_rq, qp->r_rq.tail)->wr_id;
876                 if (++qp->r_rq.tail >= qp->r_rq.size)
877                         qp->r_rq.tail = 0;
878                 ipath_cq_enter(to_icq(qp->ibqp.recv_cq), &wc, 1);
879         }
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 }