IB/ipath: Change UD to queue work requests like RC & UC
[safe/jmp/linux-2.6] / drivers / infiniband / hw / ipath / ipath_qp.c
1 /*
2  * Copyright (c) 2006, 2007 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  */
333 static void ipath_reset_qp(struct ipath_qp *qp)
334 {
335         qp->remote_qpn = 0;
336         qp->qkey = 0;
337         qp->qp_access_flags = 0;
338         qp->s_busy = 0;
339         qp->s_flags &= IPATH_S_SIGNAL_REQ_WR;
340         qp->s_hdrwords = 0;
341         qp->s_wqe = NULL;
342         qp->s_psn = 0;
343         qp->r_psn = 0;
344         qp->r_msn = 0;
345         if (qp->ibqp.qp_type == IB_QPT_RC) {
346                 qp->s_state = IB_OPCODE_RC_SEND_LAST;
347                 qp->r_state = IB_OPCODE_RC_SEND_LAST;
348         } else {
349                 qp->s_state = IB_OPCODE_UC_SEND_LAST;
350                 qp->r_state = IB_OPCODE_UC_SEND_LAST;
351         }
352         qp->s_ack_state = IB_OPCODE_RC_ACKNOWLEDGE;
353         qp->r_nak_state = 0;
354         qp->r_wrid_valid = 0;
355         qp->s_rnr_timeout = 0;
356         qp->s_head = 0;
357         qp->s_tail = 0;
358         qp->s_cur = 0;
359         qp->s_last = 0;
360         qp->s_ssn = 1;
361         qp->s_lsn = 0;
362         qp->s_wait_credit = 0;
363         memset(qp->s_ack_queue, 0, sizeof(qp->s_ack_queue));
364         qp->r_head_ack_queue = 0;
365         qp->s_tail_ack_queue = 0;
366         qp->s_num_rd_atomic = 0;
367         if (qp->r_rq.wq) {
368                 qp->r_rq.wq->head = 0;
369                 qp->r_rq.wq->tail = 0;
370         }
371         qp->r_reuse_sge = 0;
372 }
373
374 /**
375  * ipath_error_qp - put a QP into an error state
376  * @qp: the QP to put into an error state
377  * @err: the receive completion error to signal if a RWQE is active
378  *
379  * Flushes both send and receive work queues.
380  * The QP s_lock should be held and interrupts disabled.
381  */
382
383 void ipath_error_qp(struct ipath_qp *qp, enum ib_wc_status err)
384 {
385         struct ipath_ibdev *dev = to_idev(qp->ibqp.device);
386         struct ib_wc wc;
387
388         ipath_dbg("QP%d/%d in error state\n",
389                   qp->ibqp.qp_num, qp->remote_qpn);
390
391         spin_lock(&dev->pending_lock);
392         /* XXX What if its already removed by the timeout code? */
393         if (!list_empty(&qp->timerwait))
394                 list_del_init(&qp->timerwait);
395         if (!list_empty(&qp->piowait))
396                 list_del_init(&qp->piowait);
397         spin_unlock(&dev->pending_lock);
398
399         wc.vendor_err = 0;
400         wc.byte_len = 0;
401         wc.imm_data = 0;
402         wc.qp = &qp->ibqp;
403         wc.src_qp = 0;
404         wc.wc_flags = 0;
405         wc.pkey_index = 0;
406         wc.slid = 0;
407         wc.sl = 0;
408         wc.dlid_path_bits = 0;
409         wc.port_num = 0;
410         if (qp->r_wrid_valid) {
411                 qp->r_wrid_valid = 0;
412                 wc.wr_id = qp->r_wr_id;
413                 wc.opcode = IB_WC_RECV;
414                 wc.status = err;
415                 ipath_cq_enter(to_icq(qp->ibqp.send_cq), &wc, 1);
416         }
417         wc.status = IB_WC_WR_FLUSH_ERR;
418
419         while (qp->s_last != qp->s_head) {
420                 struct ipath_swqe *wqe = get_swqe_ptr(qp, qp->s_last);
421
422                 wc.wr_id = wqe->wr.wr_id;
423                 wc.opcode = ib_ipath_wc_opcode[wqe->wr.opcode];
424                 if (++qp->s_last >= qp->s_size)
425                         qp->s_last = 0;
426                 ipath_cq_enter(to_icq(qp->ibqp.send_cq), &wc, 1);
427         }
428         qp->s_cur = qp->s_tail = qp->s_head;
429         qp->s_hdrwords = 0;
430         qp->s_ack_state = IB_OPCODE_RC_ACKNOWLEDGE;
431
432         if (qp->r_rq.wq) {
433                 struct ipath_rwq *wq;
434                 u32 head;
435                 u32 tail;
436
437                 spin_lock(&qp->r_rq.lock);
438
439                 /* sanity check pointers before trusting them */
440                 wq = qp->r_rq.wq;
441                 head = wq->head;
442                 if (head >= qp->r_rq.size)
443                         head = 0;
444                 tail = wq->tail;
445                 if (tail >= qp->r_rq.size)
446                         tail = 0;
447                 wc.opcode = IB_WC_RECV;
448                 while (tail != head) {
449                         wc.wr_id = get_rwqe_ptr(&qp->r_rq, tail)->wr_id;
450                         if (++tail >= qp->r_rq.size)
451                                 tail = 0;
452                         ipath_cq_enter(to_icq(qp->ibqp.recv_cq), &wc, 1);
453                 }
454                 wq->tail = tail;
455
456                 spin_unlock(&qp->r_rq.lock);
457         }
458 }
459
460 /**
461  * ipath_modify_qp - modify the attributes of a queue pair
462  * @ibqp: the queue pair who's attributes we're modifying
463  * @attr: the new attributes
464  * @attr_mask: the mask of attributes to modify
465  * @udata: user data for ipathverbs.so
466  *
467  * Returns 0 on success, otherwise returns an errno.
468  */
469 int ipath_modify_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
470                     int attr_mask, struct ib_udata *udata)
471 {
472         struct ipath_ibdev *dev = to_idev(ibqp->device);
473         struct ipath_qp *qp = to_iqp(ibqp);
474         enum ib_qp_state cur_state, new_state;
475         unsigned long flags;
476         int ret;
477
478         spin_lock_irqsave(&qp->s_lock, flags);
479
480         cur_state = attr_mask & IB_QP_CUR_STATE ?
481                 attr->cur_qp_state : qp->state;
482         new_state = attr_mask & IB_QP_STATE ? attr->qp_state : cur_state;
483
484         if (!ib_modify_qp_is_ok(cur_state, new_state, ibqp->qp_type,
485                                 attr_mask))
486                 goto inval;
487
488         if (attr_mask & IB_QP_AV) {
489                 if (attr->ah_attr.dlid == 0 ||
490                     attr->ah_attr.dlid >= IPATH_MULTICAST_LID_BASE)
491                         goto inval;
492
493                 if ((attr->ah_attr.ah_flags & IB_AH_GRH) &&
494                     (attr->ah_attr.grh.sgid_index > 1))
495                         goto inval;
496         }
497
498         if (attr_mask & IB_QP_PKEY_INDEX)
499                 if (attr->pkey_index >= ipath_get_npkeys(dev->dd))
500                         goto inval;
501
502         if (attr_mask & IB_QP_MIN_RNR_TIMER)
503                 if (attr->min_rnr_timer > 31)
504                         goto inval;
505
506         if (attr_mask & IB_QP_PORT)
507                 if (attr->port_num == 0 ||
508                     attr->port_num > ibqp->device->phys_port_cnt)
509                         goto inval;
510
511         /*
512          * Note: the chips support a maximum MTU of 4096, but the driver
513          * hasn't implemented this feature yet, so don't allow Path MTU
514          * values greater than 2048.
515          */
516         if (attr_mask & IB_QP_PATH_MTU)
517                 if (attr->path_mtu > IB_MTU_2048)
518                         goto inval;
519
520         if (attr_mask & IB_QP_PATH_MIG_STATE)
521                 if (attr->path_mig_state != IB_MIG_MIGRATED &&
522                     attr->path_mig_state != IB_MIG_REARM)
523                         goto inval;
524
525         if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC)
526                 if (attr->max_dest_rd_atomic > IPATH_MAX_RDMA_ATOMIC)
527                         goto inval;
528
529         switch (new_state) {
530         case IB_QPS_RESET:
531                 ipath_reset_qp(qp);
532                 break;
533
534         case IB_QPS_ERR:
535                 ipath_error_qp(qp, IB_WC_WR_FLUSH_ERR);
536                 break;
537
538         default:
539                 break;
540
541         }
542
543         if (attr_mask & IB_QP_PKEY_INDEX)
544                 qp->s_pkey_index = attr->pkey_index;
545
546         if (attr_mask & IB_QP_DEST_QPN)
547                 qp->remote_qpn = attr->dest_qp_num;
548
549         if (attr_mask & IB_QP_SQ_PSN) {
550                 qp->s_psn = qp->s_next_psn = attr->sq_psn;
551                 qp->s_last_psn = qp->s_next_psn - 1;
552         }
553
554         if (attr_mask & IB_QP_RQ_PSN)
555                 qp->r_psn = attr->rq_psn;
556
557         if (attr_mask & IB_QP_ACCESS_FLAGS)
558                 qp->qp_access_flags = attr->qp_access_flags;
559
560         if (attr_mask & IB_QP_AV)
561                 qp->remote_ah_attr = attr->ah_attr;
562
563         if (attr_mask & IB_QP_PATH_MTU)
564                 qp->path_mtu = attr->path_mtu;
565
566         if (attr_mask & IB_QP_RETRY_CNT)
567                 qp->s_retry = qp->s_retry_cnt = attr->retry_cnt;
568
569         if (attr_mask & IB_QP_RNR_RETRY) {
570                 qp->s_rnr_retry = attr->rnr_retry;
571                 if (qp->s_rnr_retry > 7)
572                         qp->s_rnr_retry = 7;
573                 qp->s_rnr_retry_cnt = qp->s_rnr_retry;
574         }
575
576         if (attr_mask & IB_QP_MIN_RNR_TIMER)
577                 qp->r_min_rnr_timer = attr->min_rnr_timer;
578
579         if (attr_mask & IB_QP_TIMEOUT)
580                 qp->timeout = attr->timeout;
581
582         if (attr_mask & IB_QP_QKEY)
583                 qp->qkey = attr->qkey;
584
585         if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC)
586                 qp->r_max_rd_atomic = attr->max_dest_rd_atomic;
587
588         if (attr_mask & IB_QP_MAX_QP_RD_ATOMIC)
589                 qp->s_max_rd_atomic = attr->max_rd_atomic;
590
591         qp->state = new_state;
592         spin_unlock_irqrestore(&qp->s_lock, flags);
593
594         ret = 0;
595         goto bail;
596
597 inval:
598         spin_unlock_irqrestore(&qp->s_lock, flags);
599         ret = -EINVAL;
600
601 bail:
602         return ret;
603 }
604
605 int ipath_query_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
606                    int attr_mask, struct ib_qp_init_attr *init_attr)
607 {
608         struct ipath_qp *qp = to_iqp(ibqp);
609
610         attr->qp_state = qp->state;
611         attr->cur_qp_state = attr->qp_state;
612         attr->path_mtu = qp->path_mtu;
613         attr->path_mig_state = 0;
614         attr->qkey = qp->qkey;
615         attr->rq_psn = qp->r_psn;
616         attr->sq_psn = qp->s_next_psn;
617         attr->dest_qp_num = qp->remote_qpn;
618         attr->qp_access_flags = qp->qp_access_flags;
619         attr->cap.max_send_wr = qp->s_size - 1;
620         attr->cap.max_recv_wr = qp->ibqp.srq ? 0 : qp->r_rq.size - 1;
621         attr->cap.max_send_sge = qp->s_max_sge;
622         attr->cap.max_recv_sge = qp->r_rq.max_sge;
623         attr->cap.max_inline_data = 0;
624         attr->ah_attr = qp->remote_ah_attr;
625         memset(&attr->alt_ah_attr, 0, sizeof(attr->alt_ah_attr));
626         attr->pkey_index = qp->s_pkey_index;
627         attr->alt_pkey_index = 0;
628         attr->en_sqd_async_notify = 0;
629         attr->sq_draining = 0;
630         attr->max_rd_atomic = qp->s_max_rd_atomic;
631         attr->max_dest_rd_atomic = qp->r_max_rd_atomic;
632         attr->min_rnr_timer = qp->r_min_rnr_timer;
633         attr->port_num = 1;
634         attr->timeout = qp->timeout;
635         attr->retry_cnt = qp->s_retry_cnt;
636         attr->rnr_retry = qp->s_rnr_retry;
637         attr->alt_port_num = 0;
638         attr->alt_timeout = 0;
639
640         init_attr->event_handler = qp->ibqp.event_handler;
641         init_attr->qp_context = qp->ibqp.qp_context;
642         init_attr->send_cq = qp->ibqp.send_cq;
643         init_attr->recv_cq = qp->ibqp.recv_cq;
644         init_attr->srq = qp->ibqp.srq;
645         init_attr->cap = attr->cap;
646         if (qp->s_flags & IPATH_S_SIGNAL_REQ_WR)
647                 init_attr->sq_sig_type = IB_SIGNAL_REQ_WR;
648         else
649                 init_attr->sq_sig_type = IB_SIGNAL_ALL_WR;
650         init_attr->qp_type = qp->ibqp.qp_type;
651         init_attr->port_num = 1;
652         return 0;
653 }
654
655 /**
656  * ipath_compute_aeth - compute the AETH (syndrome + MSN)
657  * @qp: the queue pair to compute the AETH for
658  *
659  * Returns the AETH.
660  */
661 __be32 ipath_compute_aeth(struct ipath_qp *qp)
662 {
663         u32 aeth = qp->r_msn & IPATH_MSN_MASK;
664
665         if (qp->ibqp.srq) {
666                 /*
667                  * Shared receive queues don't generate credits.
668                  * Set the credit field to the invalid value.
669                  */
670                 aeth |= IPATH_AETH_CREDIT_INVAL << IPATH_AETH_CREDIT_SHIFT;
671         } else {
672                 u32 min, max, x;
673                 u32 credits;
674                 struct ipath_rwq *wq = qp->r_rq.wq;
675                 u32 head;
676                 u32 tail;
677
678                 /* sanity check pointers before trusting them */
679                 head = wq->head;
680                 if (head >= qp->r_rq.size)
681                         head = 0;
682                 tail = wq->tail;
683                 if (tail >= qp->r_rq.size)
684                         tail = 0;
685                 /*
686                  * Compute the number of credits available (RWQEs).
687                  * XXX Not holding the r_rq.lock here so there is a small
688                  * chance that the pair of reads are not atomic.
689                  */
690                 credits = head - tail;
691                 if ((int)credits < 0)
692                         credits += qp->r_rq.size;
693                 /*
694                  * Binary search the credit table to find the code to
695                  * use.
696                  */
697                 min = 0;
698                 max = 31;
699                 for (;;) {
700                         x = (min + max) / 2;
701                         if (credit_table[x] == credits)
702                                 break;
703                         if (credit_table[x] > credits)
704                                 max = x;
705                         else if (min == x)
706                                 break;
707                         else
708                                 min = x;
709                 }
710                 aeth |= x << IPATH_AETH_CREDIT_SHIFT;
711         }
712         return cpu_to_be32(aeth);
713 }
714
715 /**
716  * ipath_create_qp - create a queue pair for a device
717  * @ibpd: the protection domain who's device we create the queue pair for
718  * @init_attr: the attributes of the queue pair
719  * @udata: unused by InfiniPath
720  *
721  * Returns the queue pair on success, otherwise returns an errno.
722  *
723  * Called by the ib_create_qp() core verbs function.
724  */
725 struct ib_qp *ipath_create_qp(struct ib_pd *ibpd,
726                               struct ib_qp_init_attr *init_attr,
727                               struct ib_udata *udata)
728 {
729         struct ipath_qp *qp;
730         int err;
731         struct ipath_swqe *swq = NULL;
732         struct ipath_ibdev *dev;
733         size_t sz;
734         struct ib_qp *ret;
735
736         if (init_attr->cap.max_send_sge > ib_ipath_max_sges ||
737             init_attr->cap.max_recv_sge > ib_ipath_max_sges ||
738             init_attr->cap.max_send_wr > ib_ipath_max_qp_wrs ||
739             init_attr->cap.max_recv_wr > ib_ipath_max_qp_wrs) {
740                 ret = ERR_PTR(-ENOMEM);
741                 goto bail;
742         }
743
744         if (init_attr->cap.max_send_sge +
745             init_attr->cap.max_recv_sge +
746             init_attr->cap.max_send_wr +
747             init_attr->cap.max_recv_wr == 0) {
748                 ret = ERR_PTR(-EINVAL);
749                 goto bail;
750         }
751
752         switch (init_attr->qp_type) {
753         case IB_QPT_UC:
754         case IB_QPT_RC:
755         case IB_QPT_UD:
756         case IB_QPT_SMI:
757         case IB_QPT_GSI:
758                 sz = sizeof(struct ipath_sge) *
759                         init_attr->cap.max_send_sge +
760                         sizeof(struct ipath_swqe);
761                 swq = vmalloc((init_attr->cap.max_send_wr + 1) * sz);
762                 if (swq == NULL) {
763                         ret = ERR_PTR(-ENOMEM);
764                         goto bail;
765                 }
766                 sz = sizeof(*qp);
767                 if (init_attr->srq) {
768                         struct ipath_srq *srq = to_isrq(init_attr->srq);
769
770                         sz += sizeof(*qp->r_sg_list) *
771                                 srq->rq.max_sge;
772                 } else
773                         sz += sizeof(*qp->r_sg_list) *
774                                 init_attr->cap.max_recv_sge;
775                 qp = kmalloc(sz, GFP_KERNEL);
776                 if (!qp) {
777                         ret = ERR_PTR(-ENOMEM);
778                         goto bail_swq;
779                 }
780                 if (init_attr->srq) {
781                         sz = 0;
782                         qp->r_rq.size = 0;
783                         qp->r_rq.max_sge = 0;
784                         qp->r_rq.wq = NULL;
785                         init_attr->cap.max_recv_wr = 0;
786                         init_attr->cap.max_recv_sge = 0;
787                 } else {
788                         qp->r_rq.size = init_attr->cap.max_recv_wr + 1;
789                         qp->r_rq.max_sge = init_attr->cap.max_recv_sge;
790                         sz = (sizeof(struct ib_sge) * qp->r_rq.max_sge) +
791                                 sizeof(struct ipath_rwqe);
792                         qp->r_rq.wq = vmalloc_user(sizeof(struct ipath_rwq) +
793                                               qp->r_rq.size * sz);
794                         if (!qp->r_rq.wq) {
795                                 ret = ERR_PTR(-ENOMEM);
796                                 goto bail_qp;
797                         }
798                 }
799
800                 /*
801                  * ib_create_qp() will initialize qp->ibqp
802                  * except for qp->ibqp.qp_num.
803                  */
804                 spin_lock_init(&qp->s_lock);
805                 spin_lock_init(&qp->r_rq.lock);
806                 atomic_set(&qp->refcount, 0);
807                 init_waitqueue_head(&qp->wait);
808                 tasklet_init(&qp->s_task, ipath_do_send, (unsigned long)qp);
809                 INIT_LIST_HEAD(&qp->piowait);
810                 INIT_LIST_HEAD(&qp->timerwait);
811                 qp->state = IB_QPS_RESET;
812                 qp->s_wq = swq;
813                 qp->s_size = init_attr->cap.max_send_wr + 1;
814                 qp->s_max_sge = init_attr->cap.max_send_sge;
815                 if (init_attr->sq_sig_type == IB_SIGNAL_REQ_WR)
816                         qp->s_flags = IPATH_S_SIGNAL_REQ_WR;
817                 else
818                         qp->s_flags = 0;
819                 dev = to_idev(ibpd->device);
820                 err = ipath_alloc_qpn(&dev->qp_table, qp,
821                                       init_attr->qp_type);
822                 if (err) {
823                         ret = ERR_PTR(err);
824                         goto bail_rwq;
825                 }
826                 qp->ip = NULL;
827                 ipath_reset_qp(qp);
828                 break;
829
830         default:
831                 /* Don't support raw QPs */
832                 ret = ERR_PTR(-ENOSYS);
833                 goto bail;
834         }
835
836         init_attr->cap.max_inline_data = 0;
837
838         /*
839          * Return the address of the RWQ as the offset to mmap.
840          * See ipath_mmap() for details.
841          */
842         if (udata && udata->outlen >= sizeof(__u64)) {
843                 int err;
844
845                 if (!qp->r_rq.wq) {
846                         __u64 offset = 0;
847
848                         err = ib_copy_to_udata(udata, &offset,
849                                                sizeof(offset));
850                         if (err) {
851                                 ret = ERR_PTR(err);
852                                 goto bail_rwq;
853                         }
854                 } else {
855                         u32 s = sizeof(struct ipath_rwq) +
856                                 qp->r_rq.size * sz;
857
858                         qp->ip =
859                             ipath_create_mmap_info(dev, s,
860                                                    ibpd->uobject->context,
861                                                    qp->r_rq.wq);
862                         if (!qp->ip) {
863                                 ret = ERR_PTR(-ENOMEM);
864                                 goto bail_rwq;
865                         }
866
867                         err = ib_copy_to_udata(udata, &(qp->ip->offset),
868                                                sizeof(qp->ip->offset));
869                         if (err) {
870                                 ret = ERR_PTR(err);
871                                 goto bail_ip;
872                         }
873                 }
874         }
875
876         spin_lock(&dev->n_qps_lock);
877         if (dev->n_qps_allocated == ib_ipath_max_qps) {
878                 spin_unlock(&dev->n_qps_lock);
879                 ret = ERR_PTR(-ENOMEM);
880                 goto bail_ip;
881         }
882
883         dev->n_qps_allocated++;
884         spin_unlock(&dev->n_qps_lock);
885
886         if (qp->ip) {
887                 spin_lock_irq(&dev->pending_lock);
888                 list_add(&qp->ip->pending_mmaps, &dev->pending_mmaps);
889                 spin_unlock_irq(&dev->pending_lock);
890         }
891
892         ret = &qp->ibqp;
893         goto bail;
894
895 bail_ip:
896         kfree(qp->ip);
897 bail_rwq:
898         vfree(qp->r_rq.wq);
899 bail_qp:
900         kfree(qp);
901 bail_swq:
902         vfree(swq);
903 bail:
904         return ret;
905 }
906
907 /**
908  * ipath_destroy_qp - destroy a queue pair
909  * @ibqp: the queue pair to destroy
910  *
911  * Returns 0 on success.
912  *
913  * Note that this can be called while the QP is actively sending or
914  * receiving!
915  */
916 int ipath_destroy_qp(struct ib_qp *ibqp)
917 {
918         struct ipath_qp *qp = to_iqp(ibqp);
919         struct ipath_ibdev *dev = to_idev(ibqp->device);
920         unsigned long flags;
921
922         spin_lock_irqsave(&qp->s_lock, flags);
923         qp->state = IB_QPS_ERR;
924         spin_unlock_irqrestore(&qp->s_lock, flags);
925         spin_lock(&dev->n_qps_lock);
926         dev->n_qps_allocated--;
927         spin_unlock(&dev->n_qps_lock);
928
929         /* Stop the sending tasklet. */
930         tasklet_kill(&qp->s_task);
931
932         /* Make sure the QP isn't on the timeout list. */
933         spin_lock_irqsave(&dev->pending_lock, flags);
934         if (!list_empty(&qp->timerwait))
935                 list_del_init(&qp->timerwait);
936         if (!list_empty(&qp->piowait))
937                 list_del_init(&qp->piowait);
938         spin_unlock_irqrestore(&dev->pending_lock, flags);
939
940         /*
941          * Make sure that the QP is not in the QPN table so receive
942          * interrupts will discard packets for this QP.  XXX Also remove QP
943          * from multicast table.
944          */
945         if (atomic_read(&qp->refcount) != 0)
946                 ipath_free_qp(&dev->qp_table, qp);
947
948         if (qp->ip)
949                 kref_put(&qp->ip->ref, ipath_release_mmap_info);
950         else
951                 vfree(qp->r_rq.wq);
952         vfree(qp->s_wq);
953         kfree(qp);
954         return 0;
955 }
956
957 /**
958  * ipath_init_qp_table - initialize the QP table for a device
959  * @idev: the device who's QP table we're initializing
960  * @size: the size of the QP table
961  *
962  * Returns 0 on success, otherwise returns an errno.
963  */
964 int ipath_init_qp_table(struct ipath_ibdev *idev, int size)
965 {
966         int i;
967         int ret;
968
969         idev->qp_table.last = 1;        /* QPN 0 and 1 are special. */
970         idev->qp_table.max = size;
971         idev->qp_table.nmaps = 1;
972         idev->qp_table.table = kzalloc(size * sizeof(*idev->qp_table.table),
973                                        GFP_KERNEL);
974         if (idev->qp_table.table == NULL) {
975                 ret = -ENOMEM;
976                 goto bail;
977         }
978
979         for (i = 0; i < ARRAY_SIZE(idev->qp_table.map); i++) {
980                 atomic_set(&idev->qp_table.map[i].n_free, BITS_PER_PAGE);
981                 idev->qp_table.map[i].page = NULL;
982         }
983
984         ret = 0;
985
986 bail:
987         return ret;
988 }
989
990 /**
991  * ipath_sqerror_qp - put a QP's send queue into an error state
992  * @qp: QP who's send queue will be put into an error state
993  * @wc: the WC responsible for putting the QP in this state
994  *
995  * Flushes the send work queue.
996  * The QP s_lock should be held and interrupts disabled.
997  */
998
999 void ipath_sqerror_qp(struct ipath_qp *qp, struct ib_wc *wc)
1000 {
1001         struct ipath_ibdev *dev = to_idev(qp->ibqp.device);
1002         struct ipath_swqe *wqe = get_swqe_ptr(qp, qp->s_last);
1003
1004         ipath_dbg("Send queue error on QP%d/%d: err: %d\n",
1005                   qp->ibqp.qp_num, qp->remote_qpn, wc->status);
1006
1007         spin_lock(&dev->pending_lock);
1008         /* XXX What if its already removed by the timeout code? */
1009         if (!list_empty(&qp->timerwait))
1010                 list_del_init(&qp->timerwait);
1011         if (!list_empty(&qp->piowait))
1012                 list_del_init(&qp->piowait);
1013         spin_unlock(&dev->pending_lock);
1014
1015         ipath_cq_enter(to_icq(qp->ibqp.send_cq), wc, 1);
1016         if (++qp->s_last >= qp->s_size)
1017                 qp->s_last = 0;
1018
1019         wc->status = IB_WC_WR_FLUSH_ERR;
1020
1021         while (qp->s_last != qp->s_head) {
1022                 wqe = get_swqe_ptr(qp, qp->s_last);
1023                 wc->wr_id = wqe->wr.wr_id;
1024                 wc->opcode = ib_ipath_wc_opcode[wqe->wr.opcode];
1025                 ipath_cq_enter(to_icq(qp->ibqp.send_cq), wc, 1);
1026                 if (++qp->s_last >= qp->s_size)
1027                         qp->s_last = 0;
1028         }
1029         qp->s_cur = qp->s_tail = qp->s_head;
1030         qp->state = IB_QPS_SQE;
1031 }
1032
1033 /**
1034  * ipath_get_credit - flush the send work queue of a QP
1035  * @qp: the qp who's send work queue to flush
1036  * @aeth: the Acknowledge Extended Transport Header
1037  *
1038  * The QP s_lock should be held.
1039  */
1040 void ipath_get_credit(struct ipath_qp *qp, u32 aeth)
1041 {
1042         u32 credit = (aeth >> IPATH_AETH_CREDIT_SHIFT) & IPATH_AETH_CREDIT_MASK;
1043
1044         /*
1045          * If the credit is invalid, we can send
1046          * as many packets as we like.  Otherwise, we have to
1047          * honor the credit field.
1048          */
1049         if (credit == IPATH_AETH_CREDIT_INVAL)
1050                 qp->s_lsn = (u32) -1;
1051         else if (qp->s_lsn != (u32) -1) {
1052                 /* Compute new LSN (i.e., MSN + credit) */
1053                 credit = (aeth + credit_table[credit]) & IPATH_MSN_MASK;
1054                 if (ipath_cmp24(credit, qp->s_lsn) > 0)
1055                         qp->s_lsn = credit;
1056         }
1057
1058         /* Restart sending if it was blocked due to lack of credits. */
1059         if (qp->s_cur != qp->s_head &&
1060             (qp->s_lsn == (u32) -1 ||
1061              ipath_cmp24(get_swqe_ptr(qp, qp->s_cur)->ssn,
1062                          qp->s_lsn + 1) <= 0))
1063                 tasklet_hi_schedule(&qp->s_task);
1064 }