IB/ipath: Performance improvements via mmap of queues
[linux-3.10.git] / drivers / infiniband / hw / ipath / ipath_qp.c
1 /*
2  * Copyright (c) 2006 QLogic, Inc. 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 static u32 alloc_qpn(struct ipath_qp_table *qpt)
85 {
86         u32 i, offset, max_scan, qpn;
87         struct qpn_map *map;
88         u32 ret;
89
90         qpn = qpt->last + 1;
91         if (qpn >= QPN_MAX)
92                 qpn = 2;
93         offset = qpn & BITS_PER_PAGE_MASK;
94         map = &qpt->map[qpn / BITS_PER_PAGE];
95         max_scan = qpt->nmaps - !offset;
96         for (i = 0;;) {
97                 if (unlikely(!map->page)) {
98                         unsigned long page = get_zeroed_page(GFP_KERNEL);
99                         unsigned long flags;
100
101                         /*
102                          * Free the page if someone raced with us
103                          * installing it:
104                          */
105                         spin_lock_irqsave(&qpt->lock, flags);
106                         if (map->page)
107                                 free_page(page);
108                         else
109                                 map->page = (void *)page;
110                         spin_unlock_irqrestore(&qpt->lock, flags);
111                         if (unlikely(!map->page))
112                                 break;
113                 }
114                 if (likely(atomic_read(&map->n_free))) {
115                         do {
116                                 if (!test_and_set_bit(offset, map->page)) {
117                                         atomic_dec(&map->n_free);
118                                         qpt->last = qpn;
119                                         ret = qpn;
120                                         goto bail;
121                                 }
122                                 offset = find_next_offset(map, offset);
123                                 qpn = mk_qpn(qpt, map, offset);
124                                 /*
125                                  * This test differs from alloc_pidmap().
126                                  * If find_next_offset() does find a zero
127                                  * bit, we don't need to check for QPN
128                                  * wrapping around past our starting QPN.
129                                  * We just need to be sure we don't loop
130                                  * forever.
131                                  */
132                         } while (offset < BITS_PER_PAGE && qpn < QPN_MAX);
133                 }
134                 /*
135                  * In order to keep the number of pages allocated to a
136                  * minimum, we scan the all existing pages before increasing
137                  * the size of the bitmap table.
138                  */
139                 if (++i > max_scan) {
140                         if (qpt->nmaps == QPNMAP_ENTRIES)
141                                 break;
142                         map = &qpt->map[qpt->nmaps++];
143                         offset = 0;
144                 } else if (map < &qpt->map[qpt->nmaps]) {
145                         ++map;
146                         offset = 0;
147                 } else {
148                         map = &qpt->map[0];
149                         offset = 2;
150                 }
151                 qpn = mk_qpn(qpt, map, offset);
152         }
153
154         ret = 0;
155
156 bail:
157         return ret;
158 }
159
160 static void free_qpn(struct ipath_qp_table *qpt, u32 qpn)
161 {
162         struct qpn_map *map;
163
164         map = qpt->map + qpn / BITS_PER_PAGE;
165         if (map->page)
166                 clear_bit(qpn & BITS_PER_PAGE_MASK, map->page);
167         atomic_inc(&map->n_free);
168 }
169
170 /**
171  * ipath_alloc_qpn - allocate a QP number
172  * @qpt: the QP table
173  * @qp: the QP
174  * @type: the QP type (IB_QPT_SMI and IB_QPT_GSI are special)
175  *
176  * Allocate the next available QPN and put the QP into the hash table.
177  * The hash table holds a reference to the QP.
178  */
179 static int ipath_alloc_qpn(struct ipath_qp_table *qpt, struct ipath_qp *qp,
180                            enum ib_qp_type type)
181 {
182         unsigned long flags;
183         u32 qpn;
184         int ret;
185
186         if (type == IB_QPT_SMI)
187                 qpn = 0;
188         else if (type == IB_QPT_GSI)
189                 qpn = 1;
190         else {
191                 /* Allocate the next available QPN */
192                 qpn = alloc_qpn(qpt);
193                 if (qpn == 0) {
194                         ret = -ENOMEM;
195                         goto bail;
196                 }
197         }
198         qp->ibqp.qp_num = qpn;
199
200         /* Add the QP to the hash table. */
201         spin_lock_irqsave(&qpt->lock, flags);
202
203         qpn %= qpt->max;
204         qp->next = qpt->table[qpn];
205         qpt->table[qpn] = qp;
206         atomic_inc(&qp->refcount);
207
208         spin_unlock_irqrestore(&qpt->lock, flags);
209         ret = 0;
210
211 bail:
212         return ret;
213 }
214
215 /**
216  * ipath_free_qp - remove a QP from the QP table
217  * @qpt: the QP table
218  * @qp: the QP to remove
219  *
220  * Remove the QP from the table so it can't be found asynchronously by
221  * the receive interrupt routine.
222  */
223 static void ipath_free_qp(struct ipath_qp_table *qpt, struct ipath_qp *qp)
224 {
225         struct ipath_qp *q, **qpp;
226         unsigned long flags;
227         int fnd = 0;
228
229         spin_lock_irqsave(&qpt->lock, flags);
230
231         /* Remove QP from the hash table. */
232         qpp = &qpt->table[qp->ibqp.qp_num % qpt->max];
233         for (; (q = *qpp) != NULL; qpp = &q->next) {
234                 if (q == qp) {
235                         *qpp = qp->next;
236                         qp->next = NULL;
237                         atomic_dec(&qp->refcount);
238                         fnd = 1;
239                         break;
240                 }
241         }
242
243         spin_unlock_irqrestore(&qpt->lock, flags);
244
245         if (!fnd)
246                 return;
247
248         /* If QPN is not reserved, mark QPN free in the bitmap. */
249         if (qp->ibqp.qp_num > 1)
250                 free_qpn(qpt, qp->ibqp.qp_num);
251
252         wait_event(qp->wait, !atomic_read(&qp->refcount));
253 }
254
255 /**
256  * ipath_free_all_qps - remove all QPs from the table
257  * @qpt: the QP table to empty
258  */
259 void ipath_free_all_qps(struct ipath_qp_table *qpt)
260 {
261         unsigned long flags;
262         struct ipath_qp *qp, *nqp;
263         u32 n;
264
265         for (n = 0; n < qpt->max; n++) {
266                 spin_lock_irqsave(&qpt->lock, flags);
267                 qp = qpt->table[n];
268                 qpt->table[n] = NULL;
269                 spin_unlock_irqrestore(&qpt->lock, flags);
270
271                 while (qp) {
272                         nqp = qp->next;
273                         if (qp->ibqp.qp_num > 1)
274                                 free_qpn(qpt, qp->ibqp.qp_num);
275                         if (!atomic_dec_and_test(&qp->refcount) ||
276                             !ipath_destroy_qp(&qp->ibqp))
277                                 _VERBS_INFO("QP memory leak!\n");
278                         qp = nqp;
279                 }
280         }
281
282         for (n = 0; n < ARRAY_SIZE(qpt->map); n++) {
283                 if (qpt->map[n].page)
284                         free_page((unsigned long)qpt->map[n].page);
285         }
286 }
287
288 /**
289  * ipath_lookup_qpn - return the QP with the given QPN
290  * @qpt: the QP table
291  * @qpn: the QP number to look up
292  *
293  * The caller is responsible for decrementing the QP reference count
294  * when done.
295  */
296 struct ipath_qp *ipath_lookup_qpn(struct ipath_qp_table *qpt, u32 qpn)
297 {
298         unsigned long flags;
299         struct ipath_qp *qp;
300
301         spin_lock_irqsave(&qpt->lock, flags);
302
303         for (qp = qpt->table[qpn % qpt->max]; qp; qp = qp->next) {
304                 if (qp->ibqp.qp_num == qpn) {
305                         atomic_inc(&qp->refcount);
306                         break;
307                 }
308         }
309
310         spin_unlock_irqrestore(&qpt->lock, flags);
311         return qp;
312 }
313
314 /**
315  * ipath_reset_qp - initialize the QP state to the reset state
316  * @qp: the QP to reset
317  */
318 static void ipath_reset_qp(struct ipath_qp *qp)
319 {
320         qp->remote_qpn = 0;
321         qp->qkey = 0;
322         qp->qp_access_flags = 0;
323         clear_bit(IPATH_S_BUSY, &qp->s_flags);
324         qp->s_hdrwords = 0;
325         qp->s_psn = 0;
326         qp->r_psn = 0;
327         qp->r_msn = 0;
328         if (qp->ibqp.qp_type == IB_QPT_RC) {
329                 qp->s_state = IB_OPCODE_RC_SEND_LAST;
330                 qp->r_state = IB_OPCODE_RC_SEND_LAST;
331         } else {
332                 qp->s_state = IB_OPCODE_UC_SEND_LAST;
333                 qp->r_state = IB_OPCODE_UC_SEND_LAST;
334         }
335         qp->s_ack_state = IB_OPCODE_RC_ACKNOWLEDGE;
336         qp->r_ack_state = IB_OPCODE_RC_ACKNOWLEDGE;
337         qp->r_nak_state = 0;
338         qp->s_rnr_timeout = 0;
339         qp->s_head = 0;
340         qp->s_tail = 0;
341         qp->s_cur = 0;
342         qp->s_last = 0;
343         qp->s_ssn = 1;
344         qp->s_lsn = 0;
345         if (qp->r_rq.wq) {
346                 qp->r_rq.wq->head = 0;
347                 qp->r_rq.wq->tail = 0;
348         }
349         qp->r_reuse_sge = 0;
350 }
351
352 /**
353  * ipath_error_qp - put a QP into an error state
354  * @qp: the QP to put into an error state
355  *
356  * Flushes both send and receive work queues.
357  * QP s_lock should be held and interrupts disabled.
358  */
359
360 void ipath_error_qp(struct ipath_qp *qp)
361 {
362         struct ipath_ibdev *dev = to_idev(qp->ibqp.device);
363         struct ib_wc wc;
364
365         _VERBS_INFO("QP%d/%d in error state\n",
366                     qp->ibqp.qp_num, qp->remote_qpn);
367
368         spin_lock(&dev->pending_lock);
369         /* XXX What if its already removed by the timeout code? */
370         if (!list_empty(&qp->timerwait))
371                 list_del_init(&qp->timerwait);
372         if (!list_empty(&qp->piowait))
373                 list_del_init(&qp->piowait);
374         spin_unlock(&dev->pending_lock);
375
376         wc.status = IB_WC_WR_FLUSH_ERR;
377         wc.vendor_err = 0;
378         wc.byte_len = 0;
379         wc.imm_data = 0;
380         wc.qp_num = qp->ibqp.qp_num;
381         wc.src_qp = 0;
382         wc.wc_flags = 0;
383         wc.pkey_index = 0;
384         wc.slid = 0;
385         wc.sl = 0;
386         wc.dlid_path_bits = 0;
387         wc.port_num = 0;
388
389         while (qp->s_last != qp->s_head) {
390                 struct ipath_swqe *wqe = get_swqe_ptr(qp, qp->s_last);
391
392                 wc.wr_id = wqe->wr.wr_id;
393                 wc.opcode = ib_ipath_wc_opcode[wqe->wr.opcode];
394                 if (++qp->s_last >= qp->s_size)
395                         qp->s_last = 0;
396                 ipath_cq_enter(to_icq(qp->ibqp.send_cq), &wc, 1);
397         }
398         qp->s_cur = qp->s_tail = qp->s_head;
399         qp->s_hdrwords = 0;
400         qp->s_ack_state = IB_OPCODE_RC_ACKNOWLEDGE;
401
402         if (qp->r_rq.wq) {
403                 struct ipath_rwq *wq;
404                 u32 head;
405                 u32 tail;
406
407                 spin_lock(&qp->r_rq.lock);
408
409                 /* sanity check pointers before trusting them */
410                 wq = qp->r_rq.wq;
411                 head = wq->head;
412                 if (head >= qp->r_rq.size)
413                         head = 0;
414                 tail = wq->tail;
415                 if (tail >= qp->r_rq.size)
416                         tail = 0;
417                 wc.opcode = IB_WC_RECV;
418                 while (tail != head) {
419                         wc.wr_id = get_rwqe_ptr(&qp->r_rq, tail)->wr_id;
420                         if (++tail >= qp->r_rq.size)
421                                 tail = 0;
422                         ipath_cq_enter(to_icq(qp->ibqp.recv_cq), &wc, 1);
423                 }
424                 wq->tail = tail;
425
426                 spin_unlock(&qp->r_rq.lock);
427         }
428 }
429
430 /**
431  * ipath_modify_qp - modify the attributes of a queue pair
432  * @ibqp: the queue pair who's attributes we're modifying
433  * @attr: the new attributes
434  * @attr_mask: the mask of attributes to modify
435  * @udata: user data for ipathverbs.so
436  *
437  * Returns 0 on success, otherwise returns an errno.
438  */
439 int ipath_modify_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
440                     int attr_mask, struct ib_udata *udata)
441 {
442         struct ipath_ibdev *dev = to_idev(ibqp->device);
443         struct ipath_qp *qp = to_iqp(ibqp);
444         enum ib_qp_state cur_state, new_state;
445         unsigned long flags;
446         int ret;
447
448         spin_lock_irqsave(&qp->s_lock, flags);
449
450         cur_state = attr_mask & IB_QP_CUR_STATE ?
451                 attr->cur_qp_state : qp->state;
452         new_state = attr_mask & IB_QP_STATE ? attr->qp_state : cur_state;
453
454         if (!ib_modify_qp_is_ok(cur_state, new_state, ibqp->qp_type,
455                                 attr_mask))
456                 goto inval;
457
458         if (attr_mask & IB_QP_AV)
459                 if (attr->ah_attr.dlid == 0 ||
460                     attr->ah_attr.dlid >= IPATH_MULTICAST_LID_BASE)
461                         goto inval;
462
463         if (attr_mask & IB_QP_PKEY_INDEX)
464                 if (attr->pkey_index >= ipath_layer_get_npkeys(dev->dd))
465                         goto inval;
466
467         if (attr_mask & IB_QP_MIN_RNR_TIMER)
468                 if (attr->min_rnr_timer > 31)
469                         goto inval;
470
471         switch (new_state) {
472         case IB_QPS_RESET:
473                 ipath_reset_qp(qp);
474                 break;
475
476         case IB_QPS_ERR:
477                 ipath_error_qp(qp);
478                 break;
479
480         default:
481                 break;
482
483         }
484
485         if (attr_mask & IB_QP_PKEY_INDEX)
486                 qp->s_pkey_index = attr->pkey_index;
487
488         if (attr_mask & IB_QP_DEST_QPN)
489                 qp->remote_qpn = attr->dest_qp_num;
490
491         if (attr_mask & IB_QP_SQ_PSN) {
492                 qp->s_next_psn = attr->sq_psn;
493                 qp->s_last_psn = qp->s_next_psn - 1;
494         }
495
496         if (attr_mask & IB_QP_RQ_PSN)
497                 qp->r_psn = attr->rq_psn;
498
499         if (attr_mask & IB_QP_ACCESS_FLAGS)
500                 qp->qp_access_flags = attr->qp_access_flags;
501
502         if (attr_mask & IB_QP_AV)
503                 qp->remote_ah_attr = attr->ah_attr;
504
505         if (attr_mask & IB_QP_PATH_MTU)
506                 qp->path_mtu = attr->path_mtu;
507
508         if (attr_mask & IB_QP_RETRY_CNT)
509                 qp->s_retry = qp->s_retry_cnt = attr->retry_cnt;
510
511         if (attr_mask & IB_QP_RNR_RETRY) {
512                 qp->s_rnr_retry = attr->rnr_retry;
513                 if (qp->s_rnr_retry > 7)
514                         qp->s_rnr_retry = 7;
515                 qp->s_rnr_retry_cnt = qp->s_rnr_retry;
516         }
517
518         if (attr_mask & IB_QP_MIN_RNR_TIMER)
519                 qp->r_min_rnr_timer = attr->min_rnr_timer;
520
521         if (attr_mask & IB_QP_QKEY)
522                 qp->qkey = attr->qkey;
523
524         qp->state = new_state;
525         spin_unlock_irqrestore(&qp->s_lock, flags);
526
527         ret = 0;
528         goto bail;
529
530 inval:
531         spin_unlock_irqrestore(&qp->s_lock, flags);
532         ret = -EINVAL;
533
534 bail:
535         return ret;
536 }
537
538 int ipath_query_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
539                    int attr_mask, struct ib_qp_init_attr *init_attr)
540 {
541         struct ipath_qp *qp = to_iqp(ibqp);
542
543         attr->qp_state = qp->state;
544         attr->cur_qp_state = attr->qp_state;
545         attr->path_mtu = qp->path_mtu;
546         attr->path_mig_state = 0;
547         attr->qkey = qp->qkey;
548         attr->rq_psn = qp->r_psn;
549         attr->sq_psn = qp->s_next_psn;
550         attr->dest_qp_num = qp->remote_qpn;
551         attr->qp_access_flags = qp->qp_access_flags;
552         attr->cap.max_send_wr = qp->s_size - 1;
553         attr->cap.max_recv_wr = qp->ibqp.srq ? 0 : qp->r_rq.size - 1;
554         attr->cap.max_send_sge = qp->s_max_sge;
555         attr->cap.max_recv_sge = qp->r_rq.max_sge;
556         attr->cap.max_inline_data = 0;
557         attr->ah_attr = qp->remote_ah_attr;
558         memset(&attr->alt_ah_attr, 0, sizeof(attr->alt_ah_attr));
559         attr->pkey_index = qp->s_pkey_index;
560         attr->alt_pkey_index = 0;
561         attr->en_sqd_async_notify = 0;
562         attr->sq_draining = 0;
563         attr->max_rd_atomic = 1;
564         attr->max_dest_rd_atomic = 1;
565         attr->min_rnr_timer = qp->r_min_rnr_timer;
566         attr->port_num = 1;
567         attr->timeout = 0;
568         attr->retry_cnt = qp->s_retry_cnt;
569         attr->rnr_retry = qp->s_rnr_retry;
570         attr->alt_port_num = 0;
571         attr->alt_timeout = 0;
572
573         init_attr->event_handler = qp->ibqp.event_handler;
574         init_attr->qp_context = qp->ibqp.qp_context;
575         init_attr->send_cq = qp->ibqp.send_cq;
576         init_attr->recv_cq = qp->ibqp.recv_cq;
577         init_attr->srq = qp->ibqp.srq;
578         init_attr->cap = attr->cap;
579         init_attr->sq_sig_type =
580                 (qp->s_flags & (1 << IPATH_S_SIGNAL_REQ_WR))
581                 ? IB_SIGNAL_REQ_WR : 0;
582         init_attr->qp_type = qp->ibqp.qp_type;
583         init_attr->port_num = 1;
584         return 0;
585 }
586
587 /**
588  * ipath_compute_aeth - compute the AETH (syndrome + MSN)
589  * @qp: the queue pair to compute the AETH for
590  *
591  * Returns the AETH.
592  */
593 __be32 ipath_compute_aeth(struct ipath_qp *qp)
594 {
595         u32 aeth = qp->r_msn & IPATH_MSN_MASK;
596
597         if (qp->ibqp.srq) {
598                 /*
599                  * Shared receive queues don't generate credits.
600                  * Set the credit field to the invalid value.
601                  */
602                 aeth |= IPATH_AETH_CREDIT_INVAL << IPATH_AETH_CREDIT_SHIFT;
603         } else {
604                 u32 min, max, x;
605                 u32 credits;
606                 struct ipath_rwq *wq = qp->r_rq.wq;
607                 u32 head;
608                 u32 tail;
609
610                 /* sanity check pointers before trusting them */
611                 head = wq->head;
612                 if (head >= qp->r_rq.size)
613                         head = 0;
614                 tail = wq->tail;
615                 if (tail >= qp->r_rq.size)
616                         tail = 0;
617                 /*
618                  * Compute the number of credits available (RWQEs).
619                  * XXX Not holding the r_rq.lock here so there is a small
620                  * chance that the pair of reads are not atomic.
621                  */
622                 credits = head - tail;
623                 if ((int)credits < 0)
624                         credits += qp->r_rq.size;
625                 /*
626                  * Binary search the credit table to find the code to
627                  * use.
628                  */
629                 min = 0;
630                 max = 31;
631                 for (;;) {
632                         x = (min + max) / 2;
633                         if (credit_table[x] == credits)
634                                 break;
635                         if (credit_table[x] > credits)
636                                 max = x;
637                         else if (min == x)
638                                 break;
639                         else
640                                 min = x;
641                 }
642                 aeth |= x << IPATH_AETH_CREDIT_SHIFT;
643         }
644         return cpu_to_be32(aeth);
645 }
646
647 /**
648  * ipath_create_qp - create a queue pair for a device
649  * @ibpd: the protection domain who's device we create the queue pair for
650  * @init_attr: the attributes of the queue pair
651  * @udata: unused by InfiniPath
652  *
653  * Returns the queue pair on success, otherwise returns an errno.
654  *
655  * Called by the ib_create_qp() core verbs function.
656  */
657 struct ib_qp *ipath_create_qp(struct ib_pd *ibpd,
658                               struct ib_qp_init_attr *init_attr,
659                               struct ib_udata *udata)
660 {
661         struct ipath_qp *qp;
662         int err;
663         struct ipath_swqe *swq = NULL;
664         struct ipath_ibdev *dev;
665         size_t sz;
666         struct ib_qp *ret;
667
668         if (init_attr->cap.max_send_sge > ib_ipath_max_sges ||
669             init_attr->cap.max_recv_sge > ib_ipath_max_sges ||
670             init_attr->cap.max_send_wr > ib_ipath_max_qp_wrs ||
671             init_attr->cap.max_recv_wr > ib_ipath_max_qp_wrs) {
672                 ret = ERR_PTR(-ENOMEM);
673                 goto bail;
674         }
675
676         if (init_attr->cap.max_send_sge +
677             init_attr->cap.max_recv_sge +
678             init_attr->cap.max_send_wr +
679             init_attr->cap.max_recv_wr == 0) {
680                 ret = ERR_PTR(-EINVAL);
681                 goto bail;
682         }
683
684         switch (init_attr->qp_type) {
685         case IB_QPT_UC:
686         case IB_QPT_RC:
687                 sz = sizeof(struct ipath_sge) *
688                         init_attr->cap.max_send_sge +
689                         sizeof(struct ipath_swqe);
690                 swq = vmalloc((init_attr->cap.max_send_wr + 1) * sz);
691                 if (swq == NULL) {
692                         ret = ERR_PTR(-ENOMEM);
693                         goto bail;
694                 }
695                 /* FALLTHROUGH */
696         case IB_QPT_UD:
697         case IB_QPT_SMI:
698         case IB_QPT_GSI:
699                 sz = sizeof(*qp);
700                 if (init_attr->srq) {
701                         struct ipath_srq *srq = to_isrq(init_attr->srq);
702
703                         sz += sizeof(*qp->r_sg_list) *
704                                 srq->rq.max_sge;
705                 } else
706                         sz += sizeof(*qp->r_sg_list) *
707                                 init_attr->cap.max_recv_sge;
708                 qp = kmalloc(sz, GFP_KERNEL);
709                 if (!qp) {
710                         ret = ERR_PTR(-ENOMEM);
711                         goto bail_swq;
712                 }
713                 if (init_attr->srq) {
714                         sz = 0;
715                         qp->r_rq.size = 0;
716                         qp->r_rq.max_sge = 0;
717                         qp->r_rq.wq = NULL;
718                         init_attr->cap.max_recv_wr = 0;
719                         init_attr->cap.max_recv_sge = 0;
720                 } else {
721                         qp->r_rq.size = init_attr->cap.max_recv_wr + 1;
722                         qp->r_rq.max_sge = init_attr->cap.max_recv_sge;
723                         sz = (sizeof(struct ib_sge) * qp->r_rq.max_sge) +
724                                 sizeof(struct ipath_rwqe);
725                         qp->r_rq.wq = vmalloc_user(sizeof(struct ipath_rwq) +
726                                               qp->r_rq.size * sz);
727                         if (!qp->r_rq.wq) {
728                                 ret = ERR_PTR(-ENOMEM);
729                                 goto bail_qp;
730                         }
731                 }
732
733                 /*
734                  * ib_create_qp() will initialize qp->ibqp
735                  * except for qp->ibqp.qp_num.
736                  */
737                 spin_lock_init(&qp->s_lock);
738                 spin_lock_init(&qp->r_rq.lock);
739                 atomic_set(&qp->refcount, 0);
740                 init_waitqueue_head(&qp->wait);
741                 tasklet_init(&qp->s_task, ipath_do_ruc_send,
742                              (unsigned long)qp);
743                 INIT_LIST_HEAD(&qp->piowait);
744                 INIT_LIST_HEAD(&qp->timerwait);
745                 qp->state = IB_QPS_RESET;
746                 qp->s_wq = swq;
747                 qp->s_size = init_attr->cap.max_send_wr + 1;
748                 qp->s_max_sge = init_attr->cap.max_send_sge;
749                 qp->s_flags = init_attr->sq_sig_type == IB_SIGNAL_REQ_WR ?
750                         1 << IPATH_S_SIGNAL_REQ_WR : 0;
751                 dev = to_idev(ibpd->device);
752                 err = ipath_alloc_qpn(&dev->qp_table, qp,
753                                       init_attr->qp_type);
754                 if (err) {
755                         ret = ERR_PTR(err);
756                         goto bail_rwq;
757                 }
758                 qp->ip = NULL;
759                 ipath_reset_qp(qp);
760
761                 /* Tell the core driver that the kernel SMA is present. */
762                 if (init_attr->qp_type == IB_QPT_SMI)
763                         ipath_layer_set_verbs_flags(dev->dd,
764                                                     IPATH_VERBS_KERNEL_SMA);
765                 break;
766
767         default:
768                 /* Don't support raw QPs */
769                 ret = ERR_PTR(-ENOSYS);
770                 goto bail;
771         }
772
773         init_attr->cap.max_inline_data = 0;
774
775         /*
776          * Return the address of the RWQ as the offset to mmap.
777          * See ipath_mmap() for details.
778          */
779         if (udata && udata->outlen >= sizeof(__u64)) {
780                 struct ipath_mmap_info *ip;
781                 __u64 offset = (__u64) qp->r_rq.wq;
782                 int err;
783
784                 err = ib_copy_to_udata(udata, &offset, sizeof(offset));
785                 if (err) {
786                         ret = ERR_PTR(err);
787                         goto bail_rwq;
788                 }
789
790                 if (qp->r_rq.wq) {
791                         /* Allocate info for ipath_mmap(). */
792                         ip = kmalloc(sizeof(*ip), GFP_KERNEL);
793                         if (!ip) {
794                                 ret = ERR_PTR(-ENOMEM);
795                                 goto bail_rwq;
796                         }
797                         qp->ip = ip;
798                         ip->context = ibpd->uobject->context;
799                         ip->obj = qp->r_rq.wq;
800                         kref_init(&ip->ref);
801                         ip->mmap_cnt = 0;
802                         ip->size = PAGE_ALIGN(sizeof(struct ipath_rwq) +
803                                               qp->r_rq.size * sz);
804                         spin_lock_irq(&dev->pending_lock);
805                         ip->next = dev->pending_mmaps;
806                         dev->pending_mmaps = ip;
807                         spin_unlock_irq(&dev->pending_lock);
808                 }
809         }
810
811         ret = &qp->ibqp;
812         goto bail;
813
814 bail_rwq:
815         vfree(qp->r_rq.wq);
816 bail_qp:
817         kfree(qp);
818 bail_swq:
819         vfree(swq);
820 bail:
821         return ret;
822 }
823
824 /**
825  * ipath_destroy_qp - destroy a queue pair
826  * @ibqp: the queue pair to destroy
827  *
828  * Returns 0 on success.
829  *
830  * Note that this can be called while the QP is actively sending or
831  * receiving!
832  */
833 int ipath_destroy_qp(struct ib_qp *ibqp)
834 {
835         struct ipath_qp *qp = to_iqp(ibqp);
836         struct ipath_ibdev *dev = to_idev(ibqp->device);
837         unsigned long flags;
838
839         /* Tell the core driver that the kernel SMA is gone. */
840         if (qp->ibqp.qp_type == IB_QPT_SMI)
841                 ipath_layer_set_verbs_flags(dev->dd, 0);
842
843         spin_lock_irqsave(&qp->s_lock, flags);
844         qp->state = IB_QPS_ERR;
845         spin_unlock_irqrestore(&qp->s_lock, flags);
846
847         /* Stop the sending tasklet. */
848         tasklet_kill(&qp->s_task);
849
850         /* Make sure the QP isn't on the timeout list. */
851         spin_lock_irqsave(&dev->pending_lock, flags);
852         if (!list_empty(&qp->timerwait))
853                 list_del_init(&qp->timerwait);
854         if (!list_empty(&qp->piowait))
855                 list_del_init(&qp->piowait);
856         spin_unlock_irqrestore(&dev->pending_lock, flags);
857
858         /*
859          * Make sure that the QP is not in the QPN table so receive
860          * interrupts will discard packets for this QP.  XXX Also remove QP
861          * from multicast table.
862          */
863         if (atomic_read(&qp->refcount) != 0)
864                 ipath_free_qp(&dev->qp_table, qp);
865
866         if (qp->ip)
867                 kref_put(&qp->ip->ref, ipath_release_mmap_info);
868         else
869                 vfree(qp->r_rq.wq);
870         vfree(qp->s_wq);
871         kfree(qp);
872         return 0;
873 }
874
875 /**
876  * ipath_init_qp_table - initialize the QP table for a device
877  * @idev: the device who's QP table we're initializing
878  * @size: the size of the QP table
879  *
880  * Returns 0 on success, otherwise returns an errno.
881  */
882 int ipath_init_qp_table(struct ipath_ibdev *idev, int size)
883 {
884         int i;
885         int ret;
886
887         idev->qp_table.last = 1;        /* QPN 0 and 1 are special. */
888         idev->qp_table.max = size;
889         idev->qp_table.nmaps = 1;
890         idev->qp_table.table = kzalloc(size * sizeof(*idev->qp_table.table),
891                                        GFP_KERNEL);
892         if (idev->qp_table.table == NULL) {
893                 ret = -ENOMEM;
894                 goto bail;
895         }
896
897         for (i = 0; i < ARRAY_SIZE(idev->qp_table.map); i++) {
898                 atomic_set(&idev->qp_table.map[i].n_free, BITS_PER_PAGE);
899                 idev->qp_table.map[i].page = NULL;
900         }
901
902         ret = 0;
903
904 bail:
905         return ret;
906 }
907
908 /**
909  * ipath_sqerror_qp - put a QP's send queue into an error state
910  * @qp: QP who's send queue will be put into an error state
911  * @wc: the WC responsible for putting the QP in this state
912  *
913  * Flushes the send work queue.
914  * The QP s_lock should be held.
915  */
916
917 void ipath_sqerror_qp(struct ipath_qp *qp, struct ib_wc *wc)
918 {
919         struct ipath_ibdev *dev = to_idev(qp->ibqp.device);
920         struct ipath_swqe *wqe = get_swqe_ptr(qp, qp->s_last);
921
922         _VERBS_INFO("Send queue error on QP%d/%d: err: %d\n",
923                     qp->ibqp.qp_num, qp->remote_qpn, wc->status);
924
925         spin_lock(&dev->pending_lock);
926         /* XXX What if its already removed by the timeout code? */
927         if (!list_empty(&qp->timerwait))
928                 list_del_init(&qp->timerwait);
929         if (!list_empty(&qp->piowait))
930                 list_del_init(&qp->piowait);
931         spin_unlock(&dev->pending_lock);
932
933         ipath_cq_enter(to_icq(qp->ibqp.send_cq), wc, 1);
934         if (++qp->s_last >= qp->s_size)
935                 qp->s_last = 0;
936
937         wc->status = IB_WC_WR_FLUSH_ERR;
938
939         while (qp->s_last != qp->s_head) {
940                 wc->wr_id = wqe->wr.wr_id;
941                 wc->opcode = ib_ipath_wc_opcode[wqe->wr.opcode];
942                 ipath_cq_enter(to_icq(qp->ibqp.send_cq), wc, 1);
943                 if (++qp->s_last >= qp->s_size)
944                         qp->s_last = 0;
945                 wqe = get_swqe_ptr(qp, qp->s_last);
946         }
947         qp->s_cur = qp->s_tail = qp->s_head;
948         qp->state = IB_QPS_SQE;
949 }
950
951 /**
952  * ipath_get_credit - flush the send work queue of a QP
953  * @qp: the qp who's send work queue to flush
954  * @aeth: the Acknowledge Extended Transport Header
955  *
956  * The QP s_lock should be held.
957  */
958 void ipath_get_credit(struct ipath_qp *qp, u32 aeth)
959 {
960         u32 credit = (aeth >> IPATH_AETH_CREDIT_SHIFT) & IPATH_AETH_CREDIT_MASK;
961
962         /*
963          * If the credit is invalid, we can send
964          * as many packets as we like.  Otherwise, we have to
965          * honor the credit field.
966          */
967         if (credit == IPATH_AETH_CREDIT_INVAL)
968                 qp->s_lsn = (u32) -1;
969         else if (qp->s_lsn != (u32) -1) {
970                 /* Compute new LSN (i.e., MSN + credit) */
971                 credit = (aeth + credit_table[credit]) & IPATH_MSN_MASK;
972                 if (ipath_cmp24(credit, qp->s_lsn) > 0)
973                         qp->s_lsn = credit;
974         }
975
976         /* Restart sending if it was blocked due to lack of credits. */
977         if (qp->s_cur != qp->s_head &&
978             (qp->s_lsn == (u32) -1 ||
979              ipath_cmp24(get_swqe_ptr(qp, qp->s_cur)->ssn,
980                          qp->s_lsn + 1) <= 0))
981                 tasklet_hi_schedule(&qp->s_task);
982 }