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