Merge branch 'wr-cleanup' of git://git.infradead.org/users/hch/rdma into wr-cleanup
[firefly-linux-kernel-4.4.55.git] / drivers / staging / rdma / ipath / ipath_verbs.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 <rdma/ib_mad.h>
35 #include <rdma/ib_user_verbs.h>
36 #include <linux/io.h>
37 #include <linux/slab.h>
38 #include <linux/module.h>
39 #include <linux/utsname.h>
40 #include <linux/rculist.h>
41
42 #include "ipath_kernel.h"
43 #include "ipath_verbs.h"
44 #include "ipath_common.h"
45
46 static unsigned int ib_ipath_qp_table_size = 251;
47 module_param_named(qp_table_size, ib_ipath_qp_table_size, uint, S_IRUGO);
48 MODULE_PARM_DESC(qp_table_size, "QP table size");
49
50 unsigned int ib_ipath_lkey_table_size = 12;
51 module_param_named(lkey_table_size, ib_ipath_lkey_table_size, uint,
52                    S_IRUGO);
53 MODULE_PARM_DESC(lkey_table_size,
54                  "LKEY table size in bits (2^n, 1 <= n <= 23)");
55
56 static unsigned int ib_ipath_max_pds = 0xFFFF;
57 module_param_named(max_pds, ib_ipath_max_pds, uint, S_IWUSR | S_IRUGO);
58 MODULE_PARM_DESC(max_pds,
59                  "Maximum number of protection domains to support");
60
61 static unsigned int ib_ipath_max_ahs = 0xFFFF;
62 module_param_named(max_ahs, ib_ipath_max_ahs, uint, S_IWUSR | S_IRUGO);
63 MODULE_PARM_DESC(max_ahs, "Maximum number of address handles to support");
64
65 unsigned int ib_ipath_max_cqes = 0x2FFFF;
66 module_param_named(max_cqes, ib_ipath_max_cqes, uint, S_IWUSR | S_IRUGO);
67 MODULE_PARM_DESC(max_cqes,
68                  "Maximum number of completion queue entries to support");
69
70 unsigned int ib_ipath_max_cqs = 0x1FFFF;
71 module_param_named(max_cqs, ib_ipath_max_cqs, uint, S_IWUSR | S_IRUGO);
72 MODULE_PARM_DESC(max_cqs, "Maximum number of completion queues to support");
73
74 unsigned int ib_ipath_max_qp_wrs = 0x3FFF;
75 module_param_named(max_qp_wrs, ib_ipath_max_qp_wrs, uint,
76                    S_IWUSR | S_IRUGO);
77 MODULE_PARM_DESC(max_qp_wrs, "Maximum number of QP WRs to support");
78
79 unsigned int ib_ipath_max_qps = 16384;
80 module_param_named(max_qps, ib_ipath_max_qps, uint, S_IWUSR | S_IRUGO);
81 MODULE_PARM_DESC(max_qps, "Maximum number of QPs to support");
82
83 unsigned int ib_ipath_max_sges = 0x60;
84 module_param_named(max_sges, ib_ipath_max_sges, uint, S_IWUSR | S_IRUGO);
85 MODULE_PARM_DESC(max_sges, "Maximum number of SGEs to support");
86
87 unsigned int ib_ipath_max_mcast_grps = 16384;
88 module_param_named(max_mcast_grps, ib_ipath_max_mcast_grps, uint,
89                    S_IWUSR | S_IRUGO);
90 MODULE_PARM_DESC(max_mcast_grps,
91                  "Maximum number of multicast groups to support");
92
93 unsigned int ib_ipath_max_mcast_qp_attached = 16;
94 module_param_named(max_mcast_qp_attached, ib_ipath_max_mcast_qp_attached,
95                    uint, S_IWUSR | S_IRUGO);
96 MODULE_PARM_DESC(max_mcast_qp_attached,
97                  "Maximum number of attached QPs to support");
98
99 unsigned int ib_ipath_max_srqs = 1024;
100 module_param_named(max_srqs, ib_ipath_max_srqs, uint, S_IWUSR | S_IRUGO);
101 MODULE_PARM_DESC(max_srqs, "Maximum number of SRQs to support");
102
103 unsigned int ib_ipath_max_srq_sges = 128;
104 module_param_named(max_srq_sges, ib_ipath_max_srq_sges,
105                    uint, S_IWUSR | S_IRUGO);
106 MODULE_PARM_DESC(max_srq_sges, "Maximum number of SRQ SGEs to support");
107
108 unsigned int ib_ipath_max_srq_wrs = 0x1FFFF;
109 module_param_named(max_srq_wrs, ib_ipath_max_srq_wrs,
110                    uint, S_IWUSR | S_IRUGO);
111 MODULE_PARM_DESC(max_srq_wrs, "Maximum number of SRQ WRs support");
112
113 static unsigned int ib_ipath_disable_sma;
114 module_param_named(disable_sma, ib_ipath_disable_sma, uint, S_IWUSR | S_IRUGO);
115 MODULE_PARM_DESC(disable_sma, "Disable the SMA");
116
117 /*
118  * Note that it is OK to post send work requests in the SQE and ERR
119  * states; ipath_do_send() will process them and generate error
120  * completions as per IB 1.2 C10-96.
121  */
122 const int ib_ipath_state_ops[IB_QPS_ERR + 1] = {
123         [IB_QPS_RESET] = 0,
124         [IB_QPS_INIT] = IPATH_POST_RECV_OK,
125         [IB_QPS_RTR] = IPATH_POST_RECV_OK | IPATH_PROCESS_RECV_OK,
126         [IB_QPS_RTS] = IPATH_POST_RECV_OK | IPATH_PROCESS_RECV_OK |
127             IPATH_POST_SEND_OK | IPATH_PROCESS_SEND_OK |
128             IPATH_PROCESS_NEXT_SEND_OK,
129         [IB_QPS_SQD] = IPATH_POST_RECV_OK | IPATH_PROCESS_RECV_OK |
130             IPATH_POST_SEND_OK | IPATH_PROCESS_SEND_OK,
131         [IB_QPS_SQE] = IPATH_POST_RECV_OK | IPATH_PROCESS_RECV_OK |
132             IPATH_POST_SEND_OK | IPATH_FLUSH_SEND,
133         [IB_QPS_ERR] = IPATH_POST_RECV_OK | IPATH_FLUSH_RECV |
134             IPATH_POST_SEND_OK | IPATH_FLUSH_SEND,
135 };
136
137 struct ipath_ucontext {
138         struct ib_ucontext ibucontext;
139 };
140
141 static inline struct ipath_ucontext *to_iucontext(struct ib_ucontext
142                                                   *ibucontext)
143 {
144         return container_of(ibucontext, struct ipath_ucontext, ibucontext);
145 }
146
147 /*
148  * Translate ib_wr_opcode into ib_wc_opcode.
149  */
150 const enum ib_wc_opcode ib_ipath_wc_opcode[] = {
151         [IB_WR_RDMA_WRITE] = IB_WC_RDMA_WRITE,
152         [IB_WR_RDMA_WRITE_WITH_IMM] = IB_WC_RDMA_WRITE,
153         [IB_WR_SEND] = IB_WC_SEND,
154         [IB_WR_SEND_WITH_IMM] = IB_WC_SEND,
155         [IB_WR_RDMA_READ] = IB_WC_RDMA_READ,
156         [IB_WR_ATOMIC_CMP_AND_SWP] = IB_WC_COMP_SWAP,
157         [IB_WR_ATOMIC_FETCH_AND_ADD] = IB_WC_FETCH_ADD
158 };
159
160 /*
161  * System image GUID.
162  */
163 static __be64 sys_image_guid;
164
165 /**
166  * ipath_copy_sge - copy data to SGE memory
167  * @ss: the SGE state
168  * @data: the data to copy
169  * @length: the length of the data
170  */
171 void ipath_copy_sge(struct ipath_sge_state *ss, void *data, u32 length)
172 {
173         struct ipath_sge *sge = &ss->sge;
174
175         while (length) {
176                 u32 len = sge->length;
177
178                 if (len > length)
179                         len = length;
180                 if (len > sge->sge_length)
181                         len = sge->sge_length;
182                 BUG_ON(len == 0);
183                 memcpy(sge->vaddr, data, len);
184                 sge->vaddr += len;
185                 sge->length -= len;
186                 sge->sge_length -= len;
187                 if (sge->sge_length == 0) {
188                         if (--ss->num_sge)
189                                 *sge = *ss->sg_list++;
190                 } else if (sge->length == 0 && sge->mr != NULL) {
191                         if (++sge->n >= IPATH_SEGSZ) {
192                                 if (++sge->m >= sge->mr->mapsz)
193                                         break;
194                                 sge->n = 0;
195                         }
196                         sge->vaddr =
197                                 sge->mr->map[sge->m]->segs[sge->n].vaddr;
198                         sge->length =
199                                 sge->mr->map[sge->m]->segs[sge->n].length;
200                 }
201                 data += len;
202                 length -= len;
203         }
204 }
205
206 /**
207  * ipath_skip_sge - skip over SGE memory - XXX almost dup of prev func
208  * @ss: the SGE state
209  * @length: the number of bytes to skip
210  */
211 void ipath_skip_sge(struct ipath_sge_state *ss, u32 length)
212 {
213         struct ipath_sge *sge = &ss->sge;
214
215         while (length) {
216                 u32 len = sge->length;
217
218                 if (len > length)
219                         len = length;
220                 if (len > sge->sge_length)
221                         len = sge->sge_length;
222                 BUG_ON(len == 0);
223                 sge->vaddr += len;
224                 sge->length -= len;
225                 sge->sge_length -= len;
226                 if (sge->sge_length == 0) {
227                         if (--ss->num_sge)
228                                 *sge = *ss->sg_list++;
229                 } else if (sge->length == 0 && sge->mr != NULL) {
230                         if (++sge->n >= IPATH_SEGSZ) {
231                                 if (++sge->m >= sge->mr->mapsz)
232                                         break;
233                                 sge->n = 0;
234                         }
235                         sge->vaddr =
236                                 sge->mr->map[sge->m]->segs[sge->n].vaddr;
237                         sge->length =
238                                 sge->mr->map[sge->m]->segs[sge->n].length;
239                 }
240                 length -= len;
241         }
242 }
243
244 /*
245  * Count the number of DMA descriptors needed to send length bytes of data.
246  * Don't modify the ipath_sge_state to get the count.
247  * Return zero if any of the segments is not aligned.
248  */
249 static u32 ipath_count_sge(struct ipath_sge_state *ss, u32 length)
250 {
251         struct ipath_sge *sg_list = ss->sg_list;
252         struct ipath_sge sge = ss->sge;
253         u8 num_sge = ss->num_sge;
254         u32 ndesc = 1;  /* count the header */
255
256         while (length) {
257                 u32 len = sge.length;
258
259                 if (len > length)
260                         len = length;
261                 if (len > sge.sge_length)
262                         len = sge.sge_length;
263                 BUG_ON(len == 0);
264                 if (((long) sge.vaddr & (sizeof(u32) - 1)) ||
265                     (len != length && (len & (sizeof(u32) - 1)))) {
266                         ndesc = 0;
267                         break;
268                 }
269                 ndesc++;
270                 sge.vaddr += len;
271                 sge.length -= len;
272                 sge.sge_length -= len;
273                 if (sge.sge_length == 0) {
274                         if (--num_sge)
275                                 sge = *sg_list++;
276                 } else if (sge.length == 0 && sge.mr != NULL) {
277                         if (++sge.n >= IPATH_SEGSZ) {
278                                 if (++sge.m >= sge.mr->mapsz)
279                                         break;
280                                 sge.n = 0;
281                         }
282                         sge.vaddr =
283                                 sge.mr->map[sge.m]->segs[sge.n].vaddr;
284                         sge.length =
285                                 sge.mr->map[sge.m]->segs[sge.n].length;
286                 }
287                 length -= len;
288         }
289         return ndesc;
290 }
291
292 /*
293  * Copy from the SGEs to the data buffer.
294  */
295 static void ipath_copy_from_sge(void *data, struct ipath_sge_state *ss,
296                                 u32 length)
297 {
298         struct ipath_sge *sge = &ss->sge;
299
300         while (length) {
301                 u32 len = sge->length;
302
303                 if (len > length)
304                         len = length;
305                 if (len > sge->sge_length)
306                         len = sge->sge_length;
307                 BUG_ON(len == 0);
308                 memcpy(data, sge->vaddr, len);
309                 sge->vaddr += len;
310                 sge->length -= len;
311                 sge->sge_length -= len;
312                 if (sge->sge_length == 0) {
313                         if (--ss->num_sge)
314                                 *sge = *ss->sg_list++;
315                 } else if (sge->length == 0 && sge->mr != NULL) {
316                         if (++sge->n >= IPATH_SEGSZ) {
317                                 if (++sge->m >= sge->mr->mapsz)
318                                         break;
319                                 sge->n = 0;
320                         }
321                         sge->vaddr =
322                                 sge->mr->map[sge->m]->segs[sge->n].vaddr;
323                         sge->length =
324                                 sge->mr->map[sge->m]->segs[sge->n].length;
325                 }
326                 data += len;
327                 length -= len;
328         }
329 }
330
331 /**
332  * ipath_post_one_send - post one RC, UC, or UD send work request
333  * @qp: the QP to post on
334  * @wr: the work request to send
335  */
336 static int ipath_post_one_send(struct ipath_qp *qp, struct ib_send_wr *wr)
337 {
338         struct ipath_swqe *wqe;
339         u32 next;
340         int i;
341         int j;
342         int acc;
343         int ret;
344         unsigned long flags;
345         struct ipath_devdata *dd = to_idev(qp->ibqp.device)->dd;
346
347         spin_lock_irqsave(&qp->s_lock, flags);
348
349         if (qp->ibqp.qp_type != IB_QPT_SMI &&
350             !(dd->ipath_flags & IPATH_LINKACTIVE)) {
351                 ret = -ENETDOWN;
352                 goto bail;
353         }
354
355         /* Check that state is OK to post send. */
356         if (unlikely(!(ib_ipath_state_ops[qp->state] & IPATH_POST_SEND_OK)))
357                 goto bail_inval;
358
359         /* IB spec says that num_sge == 0 is OK. */
360         if (wr->num_sge > qp->s_max_sge)
361                 goto bail_inval;
362
363         /*
364          * Don't allow RDMA reads or atomic operations on UC or
365          * undefined operations.
366          * Make sure buffer is large enough to hold the result for atomics.
367          */
368         if (qp->ibqp.qp_type == IB_QPT_UC) {
369                 if ((unsigned) wr->opcode >= IB_WR_RDMA_READ)
370                         goto bail_inval;
371         } else if (qp->ibqp.qp_type == IB_QPT_UD) {
372                 /* Check UD opcode */
373                 if (wr->opcode != IB_WR_SEND &&
374                     wr->opcode != IB_WR_SEND_WITH_IMM)
375                         goto bail_inval;
376                 /* Check UD destination address PD */
377                 if (qp->ibqp.pd != ud_wr(wr)->ah->pd)
378                         goto bail_inval;
379         } else if ((unsigned) wr->opcode > IB_WR_ATOMIC_FETCH_AND_ADD)
380                 goto bail_inval;
381         else if (wr->opcode >= IB_WR_ATOMIC_CMP_AND_SWP &&
382                    (wr->num_sge == 0 ||
383                     wr->sg_list[0].length < sizeof(u64) ||
384                     wr->sg_list[0].addr & (sizeof(u64) - 1)))
385                 goto bail_inval;
386         else if (wr->opcode >= IB_WR_RDMA_READ && !qp->s_max_rd_atomic)
387                 goto bail_inval;
388
389         next = qp->s_head + 1;
390         if (next >= qp->s_size)
391                 next = 0;
392         if (next == qp->s_last) {
393                 ret = -ENOMEM;
394                 goto bail;
395         }
396
397         wqe = get_swqe_ptr(qp, qp->s_head);
398
399         if (qp->ibqp.qp_type != IB_QPT_UC &&
400             qp->ibqp.qp_type != IB_QPT_RC)
401                 memcpy(&wqe->ud_wr, ud_wr(wr), sizeof(wqe->ud_wr));
402         else if (wr->opcode == IB_WR_FAST_REG_MR)
403                 memcpy(&wqe->fast_reg_wr, fast_reg_wr(wr),
404                         sizeof(wqe->fast_reg_wr));
405         else if (wr->opcode == IB_WR_RDMA_WRITE_WITH_IMM ||
406                  wr->opcode == IB_WR_RDMA_WRITE ||
407                  wr->opcode == IB_WR_RDMA_READ)
408                 memcpy(&wqe->rdma_wr, rdma_wr(wr), sizeof(wqe->rdma_wr));
409         else if (wr->opcode == IB_WR_ATOMIC_CMP_AND_SWP ||
410                  wr->opcode == IB_WR_ATOMIC_FETCH_AND_ADD)
411                 memcpy(&wqe->atomic_wr, atomic_wr(wr), sizeof(wqe->atomic_wr));
412         else
413                 memcpy(&wqe->wr, wr, sizeof(wqe->wr));
414
415         wqe->length = 0;
416         if (wr->num_sge) {
417                 acc = wr->opcode >= IB_WR_RDMA_READ ?
418                         IB_ACCESS_LOCAL_WRITE : 0;
419                 for (i = 0, j = 0; i < wr->num_sge; i++) {
420                         u32 length = wr->sg_list[i].length;
421                         int ok;
422
423                         if (length == 0)
424                                 continue;
425                         ok = ipath_lkey_ok(qp, &wqe->sg_list[j],
426                                            &wr->sg_list[i], acc);
427                         if (!ok)
428                                 goto bail_inval;
429                         wqe->length += length;
430                         j++;
431                 }
432                 wqe->wr.num_sge = j;
433         }
434         if (qp->ibqp.qp_type == IB_QPT_UC ||
435             qp->ibqp.qp_type == IB_QPT_RC) {
436                 if (wqe->length > 0x80000000U)
437                         goto bail_inval;
438         } else if (wqe->length > to_idev(qp->ibqp.device)->dd->ipath_ibmtu)
439                 goto bail_inval;
440         wqe->ssn = qp->s_ssn++;
441         qp->s_head = next;
442
443         ret = 0;
444         goto bail;
445
446 bail_inval:
447         ret = -EINVAL;
448 bail:
449         spin_unlock_irqrestore(&qp->s_lock, flags);
450         return ret;
451 }
452
453 /**
454  * ipath_post_send - post a send on a QP
455  * @ibqp: the QP to post the send on
456  * @wr: the list of work requests to post
457  * @bad_wr: the first bad WR is put here
458  *
459  * This may be called from interrupt context.
460  */
461 static int ipath_post_send(struct ib_qp *ibqp, struct ib_send_wr *wr,
462                            struct ib_send_wr **bad_wr)
463 {
464         struct ipath_qp *qp = to_iqp(ibqp);
465         int err = 0;
466
467         for (; wr; wr = wr->next) {
468                 err = ipath_post_one_send(qp, wr);
469                 if (err) {
470                         *bad_wr = wr;
471                         goto bail;
472                 }
473         }
474
475         /* Try to do the send work in the caller's context. */
476         ipath_do_send((unsigned long) qp);
477
478 bail:
479         return err;
480 }
481
482 /**
483  * ipath_post_receive - post a receive on a QP
484  * @ibqp: the QP to post the receive on
485  * @wr: the WR to post
486  * @bad_wr: the first bad WR is put here
487  *
488  * This may be called from interrupt context.
489  */
490 static int ipath_post_receive(struct ib_qp *ibqp, struct ib_recv_wr *wr,
491                               struct ib_recv_wr **bad_wr)
492 {
493         struct ipath_qp *qp = to_iqp(ibqp);
494         struct ipath_rwq *wq = qp->r_rq.wq;
495         unsigned long flags;
496         int ret;
497
498         /* Check that state is OK to post receive. */
499         if (!(ib_ipath_state_ops[qp->state] & IPATH_POST_RECV_OK) || !wq) {
500                 *bad_wr = wr;
501                 ret = -EINVAL;
502                 goto bail;
503         }
504
505         for (; wr; wr = wr->next) {
506                 struct ipath_rwqe *wqe;
507                 u32 next;
508                 int i;
509
510                 if ((unsigned) wr->num_sge > qp->r_rq.max_sge) {
511                         *bad_wr = wr;
512                         ret = -EINVAL;
513                         goto bail;
514                 }
515
516                 spin_lock_irqsave(&qp->r_rq.lock, flags);
517                 next = wq->head + 1;
518                 if (next >= qp->r_rq.size)
519                         next = 0;
520                 if (next == wq->tail) {
521                         spin_unlock_irqrestore(&qp->r_rq.lock, flags);
522                         *bad_wr = wr;
523                         ret = -ENOMEM;
524                         goto bail;
525                 }
526
527                 wqe = get_rwqe_ptr(&qp->r_rq, wq->head);
528                 wqe->wr_id = wr->wr_id;
529                 wqe->num_sge = wr->num_sge;
530                 for (i = 0; i < wr->num_sge; i++)
531                         wqe->sg_list[i] = wr->sg_list[i];
532                 /* Make sure queue entry is written before the head index. */
533                 smp_wmb();
534                 wq->head = next;
535                 spin_unlock_irqrestore(&qp->r_rq.lock, flags);
536         }
537         ret = 0;
538
539 bail:
540         return ret;
541 }
542
543 /**
544  * ipath_qp_rcv - processing an incoming packet on a QP
545  * @dev: the device the packet came on
546  * @hdr: the packet header
547  * @has_grh: true if the packet has a GRH
548  * @data: the packet data
549  * @tlen: the packet length
550  * @qp: the QP the packet came on
551  *
552  * This is called from ipath_ib_rcv() to process an incoming packet
553  * for the given QP.
554  * Called at interrupt level.
555  */
556 static void ipath_qp_rcv(struct ipath_ibdev *dev,
557                          struct ipath_ib_header *hdr, int has_grh,
558                          void *data, u32 tlen, struct ipath_qp *qp)
559 {
560         /* Check for valid receive state. */
561         if (!(ib_ipath_state_ops[qp->state] & IPATH_PROCESS_RECV_OK)) {
562                 dev->n_pkt_drops++;
563                 return;
564         }
565
566         switch (qp->ibqp.qp_type) {
567         case IB_QPT_SMI:
568         case IB_QPT_GSI:
569                 if (ib_ipath_disable_sma)
570                         break;
571                 /* FALLTHROUGH */
572         case IB_QPT_UD:
573                 ipath_ud_rcv(dev, hdr, has_grh, data, tlen, qp);
574                 break;
575
576         case IB_QPT_RC:
577                 ipath_rc_rcv(dev, hdr, has_grh, data, tlen, qp);
578                 break;
579
580         case IB_QPT_UC:
581                 ipath_uc_rcv(dev, hdr, has_grh, data, tlen, qp);
582                 break;
583
584         default:
585                 break;
586         }
587 }
588
589 /**
590  * ipath_ib_rcv - process an incoming packet
591  * @arg: the device pointer
592  * @rhdr: the header of the packet
593  * @data: the packet data
594  * @tlen: the packet length
595  *
596  * This is called from ipath_kreceive() to process an incoming packet at
597  * interrupt level. Tlen is the length of the header + data + CRC in bytes.
598  */
599 void ipath_ib_rcv(struct ipath_ibdev *dev, void *rhdr, void *data,
600                   u32 tlen)
601 {
602         struct ipath_ib_header *hdr = rhdr;
603         struct ipath_other_headers *ohdr;
604         struct ipath_qp *qp;
605         u32 qp_num;
606         int lnh;
607         u8 opcode;
608         u16 lid;
609
610         if (unlikely(dev == NULL))
611                 goto bail;
612
613         if (unlikely(tlen < 24)) {      /* LRH+BTH+CRC */
614                 dev->rcv_errors++;
615                 goto bail;
616         }
617
618         /* Check for a valid destination LID (see ch. 7.11.1). */
619         lid = be16_to_cpu(hdr->lrh[1]);
620         if (lid < IPATH_MULTICAST_LID_BASE) {
621                 lid &= ~((1 << dev->dd->ipath_lmc) - 1);
622                 if (unlikely(lid != dev->dd->ipath_lid)) {
623                         dev->rcv_errors++;
624                         goto bail;
625                 }
626         }
627
628         /* Check for GRH */
629         lnh = be16_to_cpu(hdr->lrh[0]) & 3;
630         if (lnh == IPATH_LRH_BTH)
631                 ohdr = &hdr->u.oth;
632         else if (lnh == IPATH_LRH_GRH)
633                 ohdr = &hdr->u.l.oth;
634         else {
635                 dev->rcv_errors++;
636                 goto bail;
637         }
638
639         opcode = (be32_to_cpu(ohdr->bth[0]) >> 24) & 0x7f;
640         dev->opstats[opcode].n_bytes += tlen;
641         dev->opstats[opcode].n_packets++;
642
643         /* Get the destination QP number. */
644         qp_num = be32_to_cpu(ohdr->bth[1]) & IPATH_QPN_MASK;
645         if (qp_num == IPATH_MULTICAST_QPN) {
646                 struct ipath_mcast *mcast;
647                 struct ipath_mcast_qp *p;
648
649                 if (lnh != IPATH_LRH_GRH) {
650                         dev->n_pkt_drops++;
651                         goto bail;
652                 }
653                 mcast = ipath_mcast_find(&hdr->u.l.grh.dgid);
654                 if (mcast == NULL) {
655                         dev->n_pkt_drops++;
656                         goto bail;
657                 }
658                 dev->n_multicast_rcv++;
659                 list_for_each_entry_rcu(p, &mcast->qp_list, list)
660                         ipath_qp_rcv(dev, hdr, 1, data, tlen, p->qp);
661                 /*
662                  * Notify ipath_multicast_detach() if it is waiting for us
663                  * to finish.
664                  */
665                 if (atomic_dec_return(&mcast->refcount) <= 1)
666                         wake_up(&mcast->wait);
667         } else {
668                 qp = ipath_lookup_qpn(&dev->qp_table, qp_num);
669                 if (qp) {
670                         dev->n_unicast_rcv++;
671                         ipath_qp_rcv(dev, hdr, lnh == IPATH_LRH_GRH, data,
672                                      tlen, qp);
673                         /*
674                          * Notify ipath_destroy_qp() if it is waiting
675                          * for us to finish.
676                          */
677                         if (atomic_dec_and_test(&qp->refcount))
678                                 wake_up(&qp->wait);
679                 } else
680                         dev->n_pkt_drops++;
681         }
682
683 bail:;
684 }
685
686 /**
687  * ipath_ib_timer - verbs timer
688  * @arg: the device pointer
689  *
690  * This is called from ipath_do_rcv_timer() at interrupt level to check for
691  * QPs which need retransmits and to collect performance numbers.
692  */
693 static void ipath_ib_timer(struct ipath_ibdev *dev)
694 {
695         struct ipath_qp *resend = NULL;
696         struct ipath_qp *rnr = NULL;
697         struct list_head *last;
698         struct ipath_qp *qp;
699         unsigned long flags;
700
701         if (dev == NULL)
702                 return;
703
704         spin_lock_irqsave(&dev->pending_lock, flags);
705         /* Start filling the next pending queue. */
706         if (++dev->pending_index >= ARRAY_SIZE(dev->pending))
707                 dev->pending_index = 0;
708         /* Save any requests still in the new queue, they have timed out. */
709         last = &dev->pending[dev->pending_index];
710         while (!list_empty(last)) {
711                 qp = list_entry(last->next, struct ipath_qp, timerwait);
712                 list_del_init(&qp->timerwait);
713                 qp->timer_next = resend;
714                 resend = qp;
715                 atomic_inc(&qp->refcount);
716         }
717         last = &dev->rnrwait;
718         if (!list_empty(last)) {
719                 qp = list_entry(last->next, struct ipath_qp, timerwait);
720                 if (--qp->s_rnr_timeout == 0) {
721                         do {
722                                 list_del_init(&qp->timerwait);
723                                 qp->timer_next = rnr;
724                                 rnr = qp;
725                                 atomic_inc(&qp->refcount);
726                                 if (list_empty(last))
727                                         break;
728                                 qp = list_entry(last->next, struct ipath_qp,
729                                                 timerwait);
730                         } while (qp->s_rnr_timeout == 0);
731                 }
732         }
733         /*
734          * We should only be in the started state if pma_sample_start != 0
735          */
736         if (dev->pma_sample_status == IB_PMA_SAMPLE_STATUS_STARTED &&
737             --dev->pma_sample_start == 0) {
738                 dev->pma_sample_status = IB_PMA_SAMPLE_STATUS_RUNNING;
739                 ipath_snapshot_counters(dev->dd, &dev->ipath_sword,
740                                         &dev->ipath_rword,
741                                         &dev->ipath_spkts,
742                                         &dev->ipath_rpkts,
743                                         &dev->ipath_xmit_wait);
744         }
745         if (dev->pma_sample_status == IB_PMA_SAMPLE_STATUS_RUNNING) {
746                 if (dev->pma_sample_interval == 0) {
747                         u64 ta, tb, tc, td, te;
748
749                         dev->pma_sample_status = IB_PMA_SAMPLE_STATUS_DONE;
750                         ipath_snapshot_counters(dev->dd, &ta, &tb,
751                                                 &tc, &td, &te);
752
753                         dev->ipath_sword = ta - dev->ipath_sword;
754                         dev->ipath_rword = tb - dev->ipath_rword;
755                         dev->ipath_spkts = tc - dev->ipath_spkts;
756                         dev->ipath_rpkts = td - dev->ipath_rpkts;
757                         dev->ipath_xmit_wait = te - dev->ipath_xmit_wait;
758                 }
759                 else
760                         dev->pma_sample_interval--;
761         }
762         spin_unlock_irqrestore(&dev->pending_lock, flags);
763
764         /* XXX What if timer fires again while this is running? */
765         while (resend != NULL) {
766                 qp = resend;
767                 resend = qp->timer_next;
768
769                 spin_lock_irqsave(&qp->s_lock, flags);
770                 if (qp->s_last != qp->s_tail &&
771                     ib_ipath_state_ops[qp->state] & IPATH_PROCESS_SEND_OK) {
772                         dev->n_timeouts++;
773                         ipath_restart_rc(qp, qp->s_last_psn + 1);
774                 }
775                 spin_unlock_irqrestore(&qp->s_lock, flags);
776
777                 /* Notify ipath_destroy_qp() if it is waiting. */
778                 if (atomic_dec_and_test(&qp->refcount))
779                         wake_up(&qp->wait);
780         }
781         while (rnr != NULL) {
782                 qp = rnr;
783                 rnr = qp->timer_next;
784
785                 spin_lock_irqsave(&qp->s_lock, flags);
786                 if (ib_ipath_state_ops[qp->state] & IPATH_PROCESS_SEND_OK)
787                         ipath_schedule_send(qp);
788                 spin_unlock_irqrestore(&qp->s_lock, flags);
789
790                 /* Notify ipath_destroy_qp() if it is waiting. */
791                 if (atomic_dec_and_test(&qp->refcount))
792                         wake_up(&qp->wait);
793         }
794 }
795
796 static void update_sge(struct ipath_sge_state *ss, u32 length)
797 {
798         struct ipath_sge *sge = &ss->sge;
799
800         sge->vaddr += length;
801         sge->length -= length;
802         sge->sge_length -= length;
803         if (sge->sge_length == 0) {
804                 if (--ss->num_sge)
805                         *sge = *ss->sg_list++;
806         } else if (sge->length == 0 && sge->mr != NULL) {
807                 if (++sge->n >= IPATH_SEGSZ) {
808                         if (++sge->m >= sge->mr->mapsz)
809                                 return;
810                         sge->n = 0;
811                 }
812                 sge->vaddr = sge->mr->map[sge->m]->segs[sge->n].vaddr;
813                 sge->length = sge->mr->map[sge->m]->segs[sge->n].length;
814         }
815 }
816
817 #ifdef __LITTLE_ENDIAN
818 static inline u32 get_upper_bits(u32 data, u32 shift)
819 {
820         return data >> shift;
821 }
822
823 static inline u32 set_upper_bits(u32 data, u32 shift)
824 {
825         return data << shift;
826 }
827
828 static inline u32 clear_upper_bytes(u32 data, u32 n, u32 off)
829 {
830         data <<= ((sizeof(u32) - n) * BITS_PER_BYTE);
831         data >>= ((sizeof(u32) - n - off) * BITS_PER_BYTE);
832         return data;
833 }
834 #else
835 static inline u32 get_upper_bits(u32 data, u32 shift)
836 {
837         return data << shift;
838 }
839
840 static inline u32 set_upper_bits(u32 data, u32 shift)
841 {
842         return data >> shift;
843 }
844
845 static inline u32 clear_upper_bytes(u32 data, u32 n, u32 off)
846 {
847         data >>= ((sizeof(u32) - n) * BITS_PER_BYTE);
848         data <<= ((sizeof(u32) - n - off) * BITS_PER_BYTE);
849         return data;
850 }
851 #endif
852
853 static void copy_io(u32 __iomem *piobuf, struct ipath_sge_state *ss,
854                     u32 length, unsigned flush_wc)
855 {
856         u32 extra = 0;
857         u32 data = 0;
858         u32 last;
859
860         while (1) {
861                 u32 len = ss->sge.length;
862                 u32 off;
863
864                 if (len > length)
865                         len = length;
866                 if (len > ss->sge.sge_length)
867                         len = ss->sge.sge_length;
868                 BUG_ON(len == 0);
869                 /* If the source address is not aligned, try to align it. */
870                 off = (unsigned long)ss->sge.vaddr & (sizeof(u32) - 1);
871                 if (off) {
872                         u32 *addr = (u32 *)((unsigned long)ss->sge.vaddr &
873                                             ~(sizeof(u32) - 1));
874                         u32 v = get_upper_bits(*addr, off * BITS_PER_BYTE);
875                         u32 y;
876
877                         y = sizeof(u32) - off;
878                         if (len > y)
879                                 len = y;
880                         if (len + extra >= sizeof(u32)) {
881                                 data |= set_upper_bits(v, extra *
882                                                        BITS_PER_BYTE);
883                                 len = sizeof(u32) - extra;
884                                 if (len == length) {
885                                         last = data;
886                                         break;
887                                 }
888                                 __raw_writel(data, piobuf);
889                                 piobuf++;
890                                 extra = 0;
891                                 data = 0;
892                         } else {
893                                 /* Clear unused upper bytes */
894                                 data |= clear_upper_bytes(v, len, extra);
895                                 if (len == length) {
896                                         last = data;
897                                         break;
898                                 }
899                                 extra += len;
900                         }
901                 } else if (extra) {
902                         /* Source address is aligned. */
903                         u32 *addr = (u32 *) ss->sge.vaddr;
904                         int shift = extra * BITS_PER_BYTE;
905                         int ushift = 32 - shift;
906                         u32 l = len;
907
908                         while (l >= sizeof(u32)) {
909                                 u32 v = *addr;
910
911                                 data |= set_upper_bits(v, shift);
912                                 __raw_writel(data, piobuf);
913                                 data = get_upper_bits(v, ushift);
914                                 piobuf++;
915                                 addr++;
916                                 l -= sizeof(u32);
917                         }
918                         /*
919                          * We still have 'extra' number of bytes leftover.
920                          */
921                         if (l) {
922                                 u32 v = *addr;
923
924                                 if (l + extra >= sizeof(u32)) {
925                                         data |= set_upper_bits(v, shift);
926                                         len -= l + extra - sizeof(u32);
927                                         if (len == length) {
928                                                 last = data;
929                                                 break;
930                                         }
931                                         __raw_writel(data, piobuf);
932                                         piobuf++;
933                                         extra = 0;
934                                         data = 0;
935                                 } else {
936                                         /* Clear unused upper bytes */
937                                         data |= clear_upper_bytes(v, l,
938                                                                   extra);
939                                         if (len == length) {
940                                                 last = data;
941                                                 break;
942                                         }
943                                         extra += l;
944                                 }
945                         } else if (len == length) {
946                                 last = data;
947                                 break;
948                         }
949                 } else if (len == length) {
950                         u32 w;
951
952                         /*
953                          * Need to round up for the last dword in the
954                          * packet.
955                          */
956                         w = (len + 3) >> 2;
957                         __iowrite32_copy(piobuf, ss->sge.vaddr, w - 1);
958                         piobuf += w - 1;
959                         last = ((u32 *) ss->sge.vaddr)[w - 1];
960                         break;
961                 } else {
962                         u32 w = len >> 2;
963
964                         __iowrite32_copy(piobuf, ss->sge.vaddr, w);
965                         piobuf += w;
966
967                         extra = len & (sizeof(u32) - 1);
968                         if (extra) {
969                                 u32 v = ((u32 *) ss->sge.vaddr)[w];
970
971                                 /* Clear unused upper bytes */
972                                 data = clear_upper_bytes(v, extra, 0);
973                         }
974                 }
975                 update_sge(ss, len);
976                 length -= len;
977         }
978         /* Update address before sending packet. */
979         update_sge(ss, length);
980         if (flush_wc) {
981                 /* must flush early everything before trigger word */
982                 ipath_flush_wc();
983                 __raw_writel(last, piobuf);
984                 /* be sure trigger word is written */
985                 ipath_flush_wc();
986         } else
987                 __raw_writel(last, piobuf);
988 }
989
990 /*
991  * Convert IB rate to delay multiplier.
992  */
993 unsigned ipath_ib_rate_to_mult(enum ib_rate rate)
994 {
995         switch (rate) {
996         case IB_RATE_2_5_GBPS: return 8;
997         case IB_RATE_5_GBPS:   return 4;
998         case IB_RATE_10_GBPS:  return 2;
999         case IB_RATE_20_GBPS:  return 1;
1000         default:               return 0;
1001         }
1002 }
1003
1004 /*
1005  * Convert delay multiplier to IB rate
1006  */
1007 static enum ib_rate ipath_mult_to_ib_rate(unsigned mult)
1008 {
1009         switch (mult) {
1010         case 8:  return IB_RATE_2_5_GBPS;
1011         case 4:  return IB_RATE_5_GBPS;
1012         case 2:  return IB_RATE_10_GBPS;
1013         case 1:  return IB_RATE_20_GBPS;
1014         default: return IB_RATE_PORT_CURRENT;
1015         }
1016 }
1017
1018 static inline struct ipath_verbs_txreq *get_txreq(struct ipath_ibdev *dev)
1019 {
1020         struct ipath_verbs_txreq *tx = NULL;
1021         unsigned long flags;
1022
1023         spin_lock_irqsave(&dev->pending_lock, flags);
1024         if (!list_empty(&dev->txreq_free)) {
1025                 struct list_head *l = dev->txreq_free.next;
1026
1027                 list_del(l);
1028                 tx = list_entry(l, struct ipath_verbs_txreq, txreq.list);
1029         }
1030         spin_unlock_irqrestore(&dev->pending_lock, flags);
1031         return tx;
1032 }
1033
1034 static inline void put_txreq(struct ipath_ibdev *dev,
1035                              struct ipath_verbs_txreq *tx)
1036 {
1037         unsigned long flags;
1038
1039         spin_lock_irqsave(&dev->pending_lock, flags);
1040         list_add(&tx->txreq.list, &dev->txreq_free);
1041         spin_unlock_irqrestore(&dev->pending_lock, flags);
1042 }
1043
1044 static void sdma_complete(void *cookie, int status)
1045 {
1046         struct ipath_verbs_txreq *tx = cookie;
1047         struct ipath_qp *qp = tx->qp;
1048         struct ipath_ibdev *dev = to_idev(qp->ibqp.device);
1049         unsigned long flags;
1050         enum ib_wc_status ibs = status == IPATH_SDMA_TXREQ_S_OK ?
1051                 IB_WC_SUCCESS : IB_WC_WR_FLUSH_ERR;
1052
1053         if (atomic_dec_and_test(&qp->s_dma_busy)) {
1054                 spin_lock_irqsave(&qp->s_lock, flags);
1055                 if (tx->wqe)
1056                         ipath_send_complete(qp, tx->wqe, ibs);
1057                 if ((ib_ipath_state_ops[qp->state] & IPATH_FLUSH_SEND &&
1058                      qp->s_last != qp->s_head) ||
1059                     (qp->s_flags & IPATH_S_WAIT_DMA))
1060                         ipath_schedule_send(qp);
1061                 spin_unlock_irqrestore(&qp->s_lock, flags);
1062                 wake_up(&qp->wait_dma);
1063         } else if (tx->wqe) {
1064                 spin_lock_irqsave(&qp->s_lock, flags);
1065                 ipath_send_complete(qp, tx->wqe, ibs);
1066                 spin_unlock_irqrestore(&qp->s_lock, flags);
1067         }
1068
1069         if (tx->txreq.flags & IPATH_SDMA_TXREQ_F_FREEBUF)
1070                 kfree(tx->txreq.map_addr);
1071         put_txreq(dev, tx);
1072
1073         if (atomic_dec_and_test(&qp->refcount))
1074                 wake_up(&qp->wait);
1075 }
1076
1077 static void decrement_dma_busy(struct ipath_qp *qp)
1078 {
1079         unsigned long flags;
1080
1081         if (atomic_dec_and_test(&qp->s_dma_busy)) {
1082                 spin_lock_irqsave(&qp->s_lock, flags);
1083                 if ((ib_ipath_state_ops[qp->state] & IPATH_FLUSH_SEND &&
1084                      qp->s_last != qp->s_head) ||
1085                     (qp->s_flags & IPATH_S_WAIT_DMA))
1086                         ipath_schedule_send(qp);
1087                 spin_unlock_irqrestore(&qp->s_lock, flags);
1088                 wake_up(&qp->wait_dma);
1089         }
1090 }
1091
1092 /*
1093  * Compute the number of clock cycles of delay before sending the next packet.
1094  * The multipliers reflect the number of clocks for the fastest rate so
1095  * one tick at 4xDDR is 8 ticks at 1xSDR.
1096  * If the destination port will take longer to receive a packet than
1097  * the outgoing link can send it, we need to delay sending the next packet
1098  * by the difference in time it takes the receiver to receive and the sender
1099  * to send this packet.
1100  * Note that this delay is always correct for UC and RC but not always
1101  * optimal for UD. For UD, the destination HCA can be different for each
1102  * packet, in which case, we could send packets to a different destination
1103  * while "waiting" for the delay. The overhead for doing this without
1104  * HW support is more than just paying the cost of delaying some packets
1105  * unnecessarily.
1106  */
1107 static inline unsigned ipath_pkt_delay(u32 plen, u8 snd_mult, u8 rcv_mult)
1108 {
1109         return (rcv_mult > snd_mult) ?
1110                 (plen * (rcv_mult - snd_mult) + 1) >> 1 : 0;
1111 }
1112
1113 static int ipath_verbs_send_dma(struct ipath_qp *qp,
1114                                 struct ipath_ib_header *hdr, u32 hdrwords,
1115                                 struct ipath_sge_state *ss, u32 len,
1116                                 u32 plen, u32 dwords)
1117 {
1118         struct ipath_ibdev *dev = to_idev(qp->ibqp.device);
1119         struct ipath_devdata *dd = dev->dd;
1120         struct ipath_verbs_txreq *tx;
1121         u32 *piobuf;
1122         u32 control;
1123         u32 ndesc;
1124         int ret;
1125
1126         tx = qp->s_tx;
1127         if (tx) {
1128                 qp->s_tx = NULL;
1129                 /* resend previously constructed packet */
1130                 atomic_inc(&qp->s_dma_busy);
1131                 ret = ipath_sdma_verbs_send(dd, tx->ss, tx->len, tx);
1132                 if (ret) {
1133                         qp->s_tx = tx;
1134                         decrement_dma_busy(qp);
1135                 }
1136                 goto bail;
1137         }
1138
1139         tx = get_txreq(dev);
1140         if (!tx) {
1141                 ret = -EBUSY;
1142                 goto bail;
1143         }
1144
1145         /*
1146          * Get the saved delay count we computed for the previous packet
1147          * and save the delay count for this packet to be used next time
1148          * we get here.
1149          */
1150         control = qp->s_pkt_delay;
1151         qp->s_pkt_delay = ipath_pkt_delay(plen, dd->delay_mult, qp->s_dmult);
1152
1153         tx->qp = qp;
1154         atomic_inc(&qp->refcount);
1155         tx->wqe = qp->s_wqe;
1156         tx->txreq.callback = sdma_complete;
1157         tx->txreq.callback_cookie = tx;
1158         tx->txreq.flags = IPATH_SDMA_TXREQ_F_HEADTOHOST |
1159                 IPATH_SDMA_TXREQ_F_INTREQ | IPATH_SDMA_TXREQ_F_FREEDESC;
1160         if (plen + 1 >= IPATH_SMALLBUF_DWORDS)
1161                 tx->txreq.flags |= IPATH_SDMA_TXREQ_F_USELARGEBUF;
1162
1163         /* VL15 packets bypass credit check */
1164         if ((be16_to_cpu(hdr->lrh[0]) >> 12) == 15) {
1165                 control |= 1ULL << 31;
1166                 tx->txreq.flags |= IPATH_SDMA_TXREQ_F_VL15;
1167         }
1168
1169         if (len) {
1170                 /*
1171                  * Don't try to DMA if it takes more descriptors than
1172                  * the queue holds.
1173                  */
1174                 ndesc = ipath_count_sge(ss, len);
1175                 if (ndesc >= dd->ipath_sdma_descq_cnt)
1176                         ndesc = 0;
1177         } else
1178                 ndesc = 1;
1179         if (ndesc) {
1180                 tx->hdr.pbc[0] = cpu_to_le32(plen);
1181                 tx->hdr.pbc[1] = cpu_to_le32(control);
1182                 memcpy(&tx->hdr.hdr, hdr, hdrwords << 2);
1183                 tx->txreq.sg_count = ndesc;
1184                 tx->map_len = (hdrwords + 2) << 2;
1185                 tx->txreq.map_addr = &tx->hdr;
1186                 atomic_inc(&qp->s_dma_busy);
1187                 ret = ipath_sdma_verbs_send(dd, ss, dwords, tx);
1188                 if (ret) {
1189                         /* save ss and length in dwords */
1190                         tx->ss = ss;
1191                         tx->len = dwords;
1192                         qp->s_tx = tx;
1193                         decrement_dma_busy(qp);
1194                 }
1195                 goto bail;
1196         }
1197
1198         /* Allocate a buffer and copy the header and payload to it. */
1199         tx->map_len = (plen + 1) << 2;
1200         piobuf = kmalloc(tx->map_len, GFP_ATOMIC);
1201         if (unlikely(piobuf == NULL)) {
1202                 ret = -EBUSY;
1203                 goto err_tx;
1204         }
1205         tx->txreq.map_addr = piobuf;
1206         tx->txreq.flags |= IPATH_SDMA_TXREQ_F_FREEBUF;
1207         tx->txreq.sg_count = 1;
1208
1209         *piobuf++ = (__force u32) cpu_to_le32(plen);
1210         *piobuf++ = (__force u32) cpu_to_le32(control);
1211         memcpy(piobuf, hdr, hdrwords << 2);
1212         ipath_copy_from_sge(piobuf + hdrwords, ss, len);
1213
1214         atomic_inc(&qp->s_dma_busy);
1215         ret = ipath_sdma_verbs_send(dd, NULL, 0, tx);
1216         /*
1217          * If we couldn't queue the DMA request, save the info
1218          * and try again later rather than destroying the
1219          * buffer and undoing the side effects of the copy.
1220          */
1221         if (ret) {
1222                 tx->ss = NULL;
1223                 tx->len = 0;
1224                 qp->s_tx = tx;
1225                 decrement_dma_busy(qp);
1226         }
1227         dev->n_unaligned++;
1228         goto bail;
1229
1230 err_tx:
1231         if (atomic_dec_and_test(&qp->refcount))
1232                 wake_up(&qp->wait);
1233         put_txreq(dev, tx);
1234 bail:
1235         return ret;
1236 }
1237
1238 static int ipath_verbs_send_pio(struct ipath_qp *qp,
1239                                 struct ipath_ib_header *ibhdr, u32 hdrwords,
1240                                 struct ipath_sge_state *ss, u32 len,
1241                                 u32 plen, u32 dwords)
1242 {
1243         struct ipath_devdata *dd = to_idev(qp->ibqp.device)->dd;
1244         u32 *hdr = (u32 *) ibhdr;
1245         u32 __iomem *piobuf;
1246         unsigned flush_wc;
1247         u32 control;
1248         int ret;
1249         unsigned long flags;
1250
1251         piobuf = ipath_getpiobuf(dd, plen, NULL);
1252         if (unlikely(piobuf == NULL)) {
1253                 ret = -EBUSY;
1254                 goto bail;
1255         }
1256
1257         /*
1258          * Get the saved delay count we computed for the previous packet
1259          * and save the delay count for this packet to be used next time
1260          * we get here.
1261          */
1262         control = qp->s_pkt_delay;
1263         qp->s_pkt_delay = ipath_pkt_delay(plen, dd->delay_mult, qp->s_dmult);
1264
1265         /* VL15 packets bypass credit check */
1266         if ((be16_to_cpu(ibhdr->lrh[0]) >> 12) == 15)
1267                 control |= 1ULL << 31;
1268
1269         /*
1270          * Write the length to the control qword plus any needed flags.
1271          * We have to flush after the PBC for correctness on some cpus
1272          * or WC buffer can be written out of order.
1273          */
1274         writeq(((u64) control << 32) | plen, piobuf);
1275         piobuf += 2;
1276
1277         flush_wc = dd->ipath_flags & IPATH_PIO_FLUSH_WC;
1278         if (len == 0) {
1279                 /*
1280                  * If there is just the header portion, must flush before
1281                  * writing last word of header for correctness, and after
1282                  * the last header word (trigger word).
1283                  */
1284                 if (flush_wc) {
1285                         ipath_flush_wc();
1286                         __iowrite32_copy(piobuf, hdr, hdrwords - 1);
1287                         ipath_flush_wc();
1288                         __raw_writel(hdr[hdrwords - 1], piobuf + hdrwords - 1);
1289                         ipath_flush_wc();
1290                 } else
1291                         __iowrite32_copy(piobuf, hdr, hdrwords);
1292                 goto done;
1293         }
1294
1295         if (flush_wc)
1296                 ipath_flush_wc();
1297         __iowrite32_copy(piobuf, hdr, hdrwords);
1298         piobuf += hdrwords;
1299
1300         /* The common case is aligned and contained in one segment. */
1301         if (likely(ss->num_sge == 1 && len <= ss->sge.length &&
1302                    !((unsigned long)ss->sge.vaddr & (sizeof(u32) - 1)))) {
1303                 u32 *addr = (u32 *) ss->sge.vaddr;
1304
1305                 /* Update address before sending packet. */
1306                 update_sge(ss, len);
1307                 if (flush_wc) {
1308                         __iowrite32_copy(piobuf, addr, dwords - 1);
1309                         /* must flush early everything before trigger word */
1310                         ipath_flush_wc();
1311                         __raw_writel(addr[dwords - 1], piobuf + dwords - 1);
1312                         /* be sure trigger word is written */
1313                         ipath_flush_wc();
1314                 } else
1315                         __iowrite32_copy(piobuf, addr, dwords);
1316                 goto done;
1317         }
1318         copy_io(piobuf, ss, len, flush_wc);
1319 done:
1320         if (qp->s_wqe) {
1321                 spin_lock_irqsave(&qp->s_lock, flags);
1322                 ipath_send_complete(qp, qp->s_wqe, IB_WC_SUCCESS);
1323                 spin_unlock_irqrestore(&qp->s_lock, flags);
1324         }
1325         ret = 0;
1326 bail:
1327         return ret;
1328 }
1329
1330 /**
1331  * ipath_verbs_send - send a packet
1332  * @qp: the QP to send on
1333  * @hdr: the packet header
1334  * @hdrwords: the number of 32-bit words in the header
1335  * @ss: the SGE to send
1336  * @len: the length of the packet in bytes
1337  */
1338 int ipath_verbs_send(struct ipath_qp *qp, struct ipath_ib_header *hdr,
1339                      u32 hdrwords, struct ipath_sge_state *ss, u32 len)
1340 {
1341         struct ipath_devdata *dd = to_idev(qp->ibqp.device)->dd;
1342         u32 plen;
1343         int ret;
1344         u32 dwords = (len + 3) >> 2;
1345
1346         /*
1347          * Calculate the send buffer trigger address.
1348          * The +1 counts for the pbc control dword following the pbc length.
1349          */
1350         plen = hdrwords + dwords + 1;
1351
1352         /*
1353          * VL15 packets (IB_QPT_SMI) will always use PIO, so we
1354          * can defer SDMA restart until link goes ACTIVE without
1355          * worrying about just how we got there.
1356          */
1357         if (qp->ibqp.qp_type == IB_QPT_SMI ||
1358             !(dd->ipath_flags & IPATH_HAS_SEND_DMA))
1359                 ret = ipath_verbs_send_pio(qp, hdr, hdrwords, ss, len,
1360                                            plen, dwords);
1361         else
1362                 ret = ipath_verbs_send_dma(qp, hdr, hdrwords, ss, len,
1363                                            plen, dwords);
1364
1365         return ret;
1366 }
1367
1368 int ipath_snapshot_counters(struct ipath_devdata *dd, u64 *swords,
1369                             u64 *rwords, u64 *spkts, u64 *rpkts,
1370                             u64 *xmit_wait)
1371 {
1372         int ret;
1373
1374         if (!(dd->ipath_flags & IPATH_INITTED)) {
1375                 /* no hardware, freeze, etc. */
1376                 ret = -EINVAL;
1377                 goto bail;
1378         }
1379         *swords = ipath_snap_cntr(dd, dd->ipath_cregs->cr_wordsendcnt);
1380         *rwords = ipath_snap_cntr(dd, dd->ipath_cregs->cr_wordrcvcnt);
1381         *spkts = ipath_snap_cntr(dd, dd->ipath_cregs->cr_pktsendcnt);
1382         *rpkts = ipath_snap_cntr(dd, dd->ipath_cregs->cr_pktrcvcnt);
1383         *xmit_wait = ipath_snap_cntr(dd, dd->ipath_cregs->cr_sendstallcnt);
1384
1385         ret = 0;
1386
1387 bail:
1388         return ret;
1389 }
1390
1391 /**
1392  * ipath_get_counters - get various chip counters
1393  * @dd: the infinipath device
1394  * @cntrs: counters are placed here
1395  *
1396  * Return the counters needed by recv_pma_get_portcounters().
1397  */
1398 int ipath_get_counters(struct ipath_devdata *dd,
1399                        struct ipath_verbs_counters *cntrs)
1400 {
1401         struct ipath_cregs const *crp = dd->ipath_cregs;
1402         int ret;
1403
1404         if (!(dd->ipath_flags & IPATH_INITTED)) {
1405                 /* no hardware, freeze, etc. */
1406                 ret = -EINVAL;
1407                 goto bail;
1408         }
1409         cntrs->symbol_error_counter =
1410                 ipath_snap_cntr(dd, crp->cr_ibsymbolerrcnt);
1411         cntrs->link_error_recovery_counter =
1412                 ipath_snap_cntr(dd, crp->cr_iblinkerrrecovcnt);
1413         /*
1414          * The link downed counter counts when the other side downs the
1415          * connection.  We add in the number of times we downed the link
1416          * due to local link integrity errors to compensate.
1417          */
1418         cntrs->link_downed_counter =
1419                 ipath_snap_cntr(dd, crp->cr_iblinkdowncnt);
1420         cntrs->port_rcv_errors =
1421                 ipath_snap_cntr(dd, crp->cr_rxdroppktcnt) +
1422                 ipath_snap_cntr(dd, crp->cr_rcvovflcnt) +
1423                 ipath_snap_cntr(dd, crp->cr_portovflcnt) +
1424                 ipath_snap_cntr(dd, crp->cr_err_rlencnt) +
1425                 ipath_snap_cntr(dd, crp->cr_invalidrlencnt) +
1426                 ipath_snap_cntr(dd, crp->cr_errlinkcnt) +
1427                 ipath_snap_cntr(dd, crp->cr_erricrccnt) +
1428                 ipath_snap_cntr(dd, crp->cr_errvcrccnt) +
1429                 ipath_snap_cntr(dd, crp->cr_errlpcrccnt) +
1430                 ipath_snap_cntr(dd, crp->cr_badformatcnt) +
1431                 dd->ipath_rxfc_unsupvl_errs;
1432         if (crp->cr_rxotherlocalphyerrcnt)
1433                 cntrs->port_rcv_errors +=
1434                         ipath_snap_cntr(dd, crp->cr_rxotherlocalphyerrcnt);
1435         if (crp->cr_rxvlerrcnt)
1436                 cntrs->port_rcv_errors +=
1437                         ipath_snap_cntr(dd, crp->cr_rxvlerrcnt);
1438         cntrs->port_rcv_remphys_errors =
1439                 ipath_snap_cntr(dd, crp->cr_rcvebpcnt);
1440         cntrs->port_xmit_discards = ipath_snap_cntr(dd, crp->cr_unsupvlcnt);
1441         cntrs->port_xmit_data = ipath_snap_cntr(dd, crp->cr_wordsendcnt);
1442         cntrs->port_rcv_data = ipath_snap_cntr(dd, crp->cr_wordrcvcnt);
1443         cntrs->port_xmit_packets = ipath_snap_cntr(dd, crp->cr_pktsendcnt);
1444         cntrs->port_rcv_packets = ipath_snap_cntr(dd, crp->cr_pktrcvcnt);
1445         cntrs->local_link_integrity_errors =
1446                 crp->cr_locallinkintegrityerrcnt ?
1447                 ipath_snap_cntr(dd, crp->cr_locallinkintegrityerrcnt) :
1448                 ((dd->ipath_flags & IPATH_GPIO_ERRINTRS) ?
1449                  dd->ipath_lli_errs : dd->ipath_lli_errors);
1450         cntrs->excessive_buffer_overrun_errors =
1451                 crp->cr_excessbufferovflcnt ?
1452                 ipath_snap_cntr(dd, crp->cr_excessbufferovflcnt) :
1453                 dd->ipath_overrun_thresh_errs;
1454         cntrs->vl15_dropped = crp->cr_vl15droppedpktcnt ?
1455                 ipath_snap_cntr(dd, crp->cr_vl15droppedpktcnt) : 0;
1456
1457         ret = 0;
1458
1459 bail:
1460         return ret;
1461 }
1462
1463 /**
1464  * ipath_ib_piobufavail - callback when a PIO buffer is available
1465  * @arg: the device pointer
1466  *
1467  * This is called from ipath_intr() at interrupt level when a PIO buffer is
1468  * available after ipath_verbs_send() returned an error that no buffers were
1469  * available.  Return 1 if we consumed all the PIO buffers and we still have
1470  * QPs waiting for buffers (for now, just restart the send tasklet and
1471  * return zero).
1472  */
1473 int ipath_ib_piobufavail(struct ipath_ibdev *dev)
1474 {
1475         struct list_head *list;
1476         struct ipath_qp *qplist;
1477         struct ipath_qp *qp;
1478         unsigned long flags;
1479
1480         if (dev == NULL)
1481                 goto bail;
1482
1483         list = &dev->piowait;
1484         qplist = NULL;
1485
1486         spin_lock_irqsave(&dev->pending_lock, flags);
1487         while (!list_empty(list)) {
1488                 qp = list_entry(list->next, struct ipath_qp, piowait);
1489                 list_del_init(&qp->piowait);
1490                 qp->pio_next = qplist;
1491                 qplist = qp;
1492                 atomic_inc(&qp->refcount);
1493         }
1494         spin_unlock_irqrestore(&dev->pending_lock, flags);
1495
1496         while (qplist != NULL) {
1497                 qp = qplist;
1498                 qplist = qp->pio_next;
1499
1500                 spin_lock_irqsave(&qp->s_lock, flags);
1501                 if (ib_ipath_state_ops[qp->state] & IPATH_PROCESS_SEND_OK)
1502                         ipath_schedule_send(qp);
1503                 spin_unlock_irqrestore(&qp->s_lock, flags);
1504
1505                 /* Notify ipath_destroy_qp() if it is waiting. */
1506                 if (atomic_dec_and_test(&qp->refcount))
1507                         wake_up(&qp->wait);
1508         }
1509
1510 bail:
1511         return 0;
1512 }
1513
1514 static int ipath_query_device(struct ib_device *ibdev, struct ib_device_attr *props,
1515                               struct ib_udata *uhw)
1516 {
1517         struct ipath_ibdev *dev = to_idev(ibdev);
1518
1519         if (uhw->inlen || uhw->outlen)
1520                 return -EINVAL;
1521
1522         memset(props, 0, sizeof(*props));
1523
1524         props->device_cap_flags = IB_DEVICE_BAD_PKEY_CNTR |
1525                 IB_DEVICE_BAD_QKEY_CNTR | IB_DEVICE_SHUTDOWN_PORT |
1526                 IB_DEVICE_SYS_IMAGE_GUID | IB_DEVICE_RC_RNR_NAK_GEN |
1527                 IB_DEVICE_PORT_ACTIVE_EVENT | IB_DEVICE_SRQ_RESIZE;
1528         props->page_size_cap = PAGE_SIZE;
1529         props->vendor_id =
1530                 IPATH_SRC_OUI_1 << 16 | IPATH_SRC_OUI_2 << 8 | IPATH_SRC_OUI_3;
1531         props->vendor_part_id = dev->dd->ipath_deviceid;
1532         props->hw_ver = dev->dd->ipath_pcirev;
1533
1534         props->sys_image_guid = dev->sys_image_guid;
1535
1536         props->max_mr_size = ~0ull;
1537         props->max_qp = ib_ipath_max_qps;
1538         props->max_qp_wr = ib_ipath_max_qp_wrs;
1539         props->max_sge = ib_ipath_max_sges;
1540         props->max_sge_rd = ib_ipath_max_sges;
1541         props->max_cq = ib_ipath_max_cqs;
1542         props->max_ah = ib_ipath_max_ahs;
1543         props->max_cqe = ib_ipath_max_cqes;
1544         props->max_mr = dev->lk_table.max;
1545         props->max_fmr = dev->lk_table.max;
1546         props->max_map_per_fmr = 32767;
1547         props->max_pd = ib_ipath_max_pds;
1548         props->max_qp_rd_atom = IPATH_MAX_RDMA_ATOMIC;
1549         props->max_qp_init_rd_atom = 255;
1550         /* props->max_res_rd_atom */
1551         props->max_srq = ib_ipath_max_srqs;
1552         props->max_srq_wr = ib_ipath_max_srq_wrs;
1553         props->max_srq_sge = ib_ipath_max_srq_sges;
1554         /* props->local_ca_ack_delay */
1555         props->atomic_cap = IB_ATOMIC_GLOB;
1556         props->max_pkeys = ipath_get_npkeys(dev->dd);
1557         props->max_mcast_grp = ib_ipath_max_mcast_grps;
1558         props->max_mcast_qp_attach = ib_ipath_max_mcast_qp_attached;
1559         props->max_total_mcast_qp_attach = props->max_mcast_qp_attach *
1560                 props->max_mcast_grp;
1561
1562         return 0;
1563 }
1564
1565 const u8 ipath_cvt_physportstate[32] = {
1566         [INFINIPATH_IBCS_LT_STATE_DISABLED] = IB_PHYSPORTSTATE_DISABLED,
1567         [INFINIPATH_IBCS_LT_STATE_LINKUP] = IB_PHYSPORTSTATE_LINKUP,
1568         [INFINIPATH_IBCS_LT_STATE_POLLACTIVE] = IB_PHYSPORTSTATE_POLL,
1569         [INFINIPATH_IBCS_LT_STATE_POLLQUIET] = IB_PHYSPORTSTATE_POLL,
1570         [INFINIPATH_IBCS_LT_STATE_SLEEPDELAY] = IB_PHYSPORTSTATE_SLEEP,
1571         [INFINIPATH_IBCS_LT_STATE_SLEEPQUIET] = IB_PHYSPORTSTATE_SLEEP,
1572         [INFINIPATH_IBCS_LT_STATE_CFGDEBOUNCE] =
1573                 IB_PHYSPORTSTATE_CFG_TRAIN,
1574         [INFINIPATH_IBCS_LT_STATE_CFGRCVFCFG] =
1575                 IB_PHYSPORTSTATE_CFG_TRAIN,
1576         [INFINIPATH_IBCS_LT_STATE_CFGWAITRMT] =
1577                 IB_PHYSPORTSTATE_CFG_TRAIN,
1578         [INFINIPATH_IBCS_LT_STATE_CFGIDLE] = IB_PHYSPORTSTATE_CFG_TRAIN,
1579         [INFINIPATH_IBCS_LT_STATE_RECOVERRETRAIN] =
1580                 IB_PHYSPORTSTATE_LINK_ERR_RECOVER,
1581         [INFINIPATH_IBCS_LT_STATE_RECOVERWAITRMT] =
1582                 IB_PHYSPORTSTATE_LINK_ERR_RECOVER,
1583         [INFINIPATH_IBCS_LT_STATE_RECOVERIDLE] =
1584                 IB_PHYSPORTSTATE_LINK_ERR_RECOVER,
1585         [0x10] = IB_PHYSPORTSTATE_CFG_TRAIN,
1586         [0x11] = IB_PHYSPORTSTATE_CFG_TRAIN,
1587         [0x12] = IB_PHYSPORTSTATE_CFG_TRAIN,
1588         [0x13] = IB_PHYSPORTSTATE_CFG_TRAIN,
1589         [0x14] = IB_PHYSPORTSTATE_CFG_TRAIN,
1590         [0x15] = IB_PHYSPORTSTATE_CFG_TRAIN,
1591         [0x16] = IB_PHYSPORTSTATE_CFG_TRAIN,
1592         [0x17] = IB_PHYSPORTSTATE_CFG_TRAIN
1593 };
1594
1595 u32 ipath_get_cr_errpkey(struct ipath_devdata *dd)
1596 {
1597         return ipath_read_creg32(dd, dd->ipath_cregs->cr_errpkey);
1598 }
1599
1600 static int ipath_query_port(struct ib_device *ibdev,
1601                             u8 port, struct ib_port_attr *props)
1602 {
1603         struct ipath_ibdev *dev = to_idev(ibdev);
1604         struct ipath_devdata *dd = dev->dd;
1605         enum ib_mtu mtu;
1606         u16 lid = dd->ipath_lid;
1607         u64 ibcstat;
1608
1609         memset(props, 0, sizeof(*props));
1610         props->lid = lid ? lid : be16_to_cpu(IB_LID_PERMISSIVE);
1611         props->lmc = dd->ipath_lmc;
1612         props->sm_lid = dev->sm_lid;
1613         props->sm_sl = dev->sm_sl;
1614         ibcstat = dd->ipath_lastibcstat;
1615         /* map LinkState to IB portinfo values.  */
1616         props->state = ipath_ib_linkstate(dd, ibcstat) + 1;
1617
1618         /* See phys_state_show() */
1619         props->phys_state = /* MEA: assumes shift == 0 */
1620                 ipath_cvt_physportstate[dd->ipath_lastibcstat &
1621                 dd->ibcs_lts_mask];
1622         props->port_cap_flags = dev->port_cap_flags;
1623         props->gid_tbl_len = 1;
1624         props->max_msg_sz = 0x80000000;
1625         props->pkey_tbl_len = ipath_get_npkeys(dd);
1626         props->bad_pkey_cntr = ipath_get_cr_errpkey(dd) -
1627                 dev->z_pkey_violations;
1628         props->qkey_viol_cntr = dev->qkey_violations;
1629         props->active_width = dd->ipath_link_width_active;
1630         /* See rate_show() */
1631         props->active_speed = dd->ipath_link_speed_active;
1632         props->max_vl_num = 1;          /* VLCap = VL0 */
1633         props->init_type_reply = 0;
1634
1635         props->max_mtu = ipath_mtu4096 ? IB_MTU_4096 : IB_MTU_2048;
1636         switch (dd->ipath_ibmtu) {
1637         case 4096:
1638                 mtu = IB_MTU_4096;
1639                 break;
1640         case 2048:
1641                 mtu = IB_MTU_2048;
1642                 break;
1643         case 1024:
1644                 mtu = IB_MTU_1024;
1645                 break;
1646         case 512:
1647                 mtu = IB_MTU_512;
1648                 break;
1649         case 256:
1650                 mtu = IB_MTU_256;
1651                 break;
1652         default:
1653                 mtu = IB_MTU_2048;
1654         }
1655         props->active_mtu = mtu;
1656         props->subnet_timeout = dev->subnet_timeout;
1657
1658         return 0;
1659 }
1660
1661 static int ipath_modify_device(struct ib_device *device,
1662                                int device_modify_mask,
1663                                struct ib_device_modify *device_modify)
1664 {
1665         int ret;
1666
1667         if (device_modify_mask & ~(IB_DEVICE_MODIFY_SYS_IMAGE_GUID |
1668                                    IB_DEVICE_MODIFY_NODE_DESC)) {
1669                 ret = -EOPNOTSUPP;
1670                 goto bail;
1671         }
1672
1673         if (device_modify_mask & IB_DEVICE_MODIFY_NODE_DESC)
1674                 memcpy(device->node_desc, device_modify->node_desc, 64);
1675
1676         if (device_modify_mask & IB_DEVICE_MODIFY_SYS_IMAGE_GUID)
1677                 to_idev(device)->sys_image_guid =
1678                         cpu_to_be64(device_modify->sys_image_guid);
1679
1680         ret = 0;
1681
1682 bail:
1683         return ret;
1684 }
1685
1686 static int ipath_modify_port(struct ib_device *ibdev,
1687                              u8 port, int port_modify_mask,
1688                              struct ib_port_modify *props)
1689 {
1690         struct ipath_ibdev *dev = to_idev(ibdev);
1691
1692         dev->port_cap_flags |= props->set_port_cap_mask;
1693         dev->port_cap_flags &= ~props->clr_port_cap_mask;
1694         if (port_modify_mask & IB_PORT_SHUTDOWN)
1695                 ipath_set_linkstate(dev->dd, IPATH_IB_LINKDOWN);
1696         if (port_modify_mask & IB_PORT_RESET_QKEY_CNTR)
1697                 dev->qkey_violations = 0;
1698         return 0;
1699 }
1700
1701 static int ipath_query_gid(struct ib_device *ibdev, u8 port,
1702                            int index, union ib_gid *gid)
1703 {
1704         struct ipath_ibdev *dev = to_idev(ibdev);
1705         int ret;
1706
1707         if (index >= 1) {
1708                 ret = -EINVAL;
1709                 goto bail;
1710         }
1711         gid->global.subnet_prefix = dev->gid_prefix;
1712         gid->global.interface_id = dev->dd->ipath_guid;
1713
1714         ret = 0;
1715
1716 bail:
1717         return ret;
1718 }
1719
1720 static struct ib_pd *ipath_alloc_pd(struct ib_device *ibdev,
1721                                     struct ib_ucontext *context,
1722                                     struct ib_udata *udata)
1723 {
1724         struct ipath_ibdev *dev = to_idev(ibdev);
1725         struct ipath_pd *pd;
1726         struct ib_pd *ret;
1727
1728         /*
1729          * This is actually totally arbitrary.  Some correctness tests
1730          * assume there's a maximum number of PDs that can be allocated.
1731          * We don't actually have this limit, but we fail the test if
1732          * we allow allocations of more than we report for this value.
1733          */
1734
1735         pd = kmalloc(sizeof *pd, GFP_KERNEL);
1736         if (!pd) {
1737                 ret = ERR_PTR(-ENOMEM);
1738                 goto bail;
1739         }
1740
1741         spin_lock(&dev->n_pds_lock);
1742         if (dev->n_pds_allocated == ib_ipath_max_pds) {
1743                 spin_unlock(&dev->n_pds_lock);
1744                 kfree(pd);
1745                 ret = ERR_PTR(-ENOMEM);
1746                 goto bail;
1747         }
1748
1749         dev->n_pds_allocated++;
1750         spin_unlock(&dev->n_pds_lock);
1751
1752         /* ib_alloc_pd() will initialize pd->ibpd. */
1753         pd->user = udata != NULL;
1754
1755         ret = &pd->ibpd;
1756
1757 bail:
1758         return ret;
1759 }
1760
1761 static int ipath_dealloc_pd(struct ib_pd *ibpd)
1762 {
1763         struct ipath_pd *pd = to_ipd(ibpd);
1764         struct ipath_ibdev *dev = to_idev(ibpd->device);
1765
1766         spin_lock(&dev->n_pds_lock);
1767         dev->n_pds_allocated--;
1768         spin_unlock(&dev->n_pds_lock);
1769
1770         kfree(pd);
1771
1772         return 0;
1773 }
1774
1775 /**
1776  * ipath_create_ah - create an address handle
1777  * @pd: the protection domain
1778  * @ah_attr: the attributes of the AH
1779  *
1780  * This may be called from interrupt context.
1781  */
1782 static struct ib_ah *ipath_create_ah(struct ib_pd *pd,
1783                                      struct ib_ah_attr *ah_attr)
1784 {
1785         struct ipath_ah *ah;
1786         struct ib_ah *ret;
1787         struct ipath_ibdev *dev = to_idev(pd->device);
1788         unsigned long flags;
1789
1790         /* A multicast address requires a GRH (see ch. 8.4.1). */
1791         if (ah_attr->dlid >= IPATH_MULTICAST_LID_BASE &&
1792             ah_attr->dlid != IPATH_PERMISSIVE_LID &&
1793             !(ah_attr->ah_flags & IB_AH_GRH)) {
1794                 ret = ERR_PTR(-EINVAL);
1795                 goto bail;
1796         }
1797
1798         if (ah_attr->dlid == 0) {
1799                 ret = ERR_PTR(-EINVAL);
1800                 goto bail;
1801         }
1802
1803         if (ah_attr->port_num < 1 ||
1804             ah_attr->port_num > pd->device->phys_port_cnt) {
1805                 ret = ERR_PTR(-EINVAL);
1806                 goto bail;
1807         }
1808
1809         ah = kmalloc(sizeof *ah, GFP_ATOMIC);
1810         if (!ah) {
1811                 ret = ERR_PTR(-ENOMEM);
1812                 goto bail;
1813         }
1814
1815         spin_lock_irqsave(&dev->n_ahs_lock, flags);
1816         if (dev->n_ahs_allocated == ib_ipath_max_ahs) {
1817                 spin_unlock_irqrestore(&dev->n_ahs_lock, flags);
1818                 kfree(ah);
1819                 ret = ERR_PTR(-ENOMEM);
1820                 goto bail;
1821         }
1822
1823         dev->n_ahs_allocated++;
1824         spin_unlock_irqrestore(&dev->n_ahs_lock, flags);
1825
1826         /* ib_create_ah() will initialize ah->ibah. */
1827         ah->attr = *ah_attr;
1828         ah->attr.static_rate = ipath_ib_rate_to_mult(ah_attr->static_rate);
1829
1830         ret = &ah->ibah;
1831
1832 bail:
1833         return ret;
1834 }
1835
1836 /**
1837  * ipath_destroy_ah - destroy an address handle
1838  * @ibah: the AH to destroy
1839  *
1840  * This may be called from interrupt context.
1841  */
1842 static int ipath_destroy_ah(struct ib_ah *ibah)
1843 {
1844         struct ipath_ibdev *dev = to_idev(ibah->device);
1845         struct ipath_ah *ah = to_iah(ibah);
1846         unsigned long flags;
1847
1848         spin_lock_irqsave(&dev->n_ahs_lock, flags);
1849         dev->n_ahs_allocated--;
1850         spin_unlock_irqrestore(&dev->n_ahs_lock, flags);
1851
1852         kfree(ah);
1853
1854         return 0;
1855 }
1856
1857 static int ipath_query_ah(struct ib_ah *ibah, struct ib_ah_attr *ah_attr)
1858 {
1859         struct ipath_ah *ah = to_iah(ibah);
1860
1861         *ah_attr = ah->attr;
1862         ah_attr->static_rate = ipath_mult_to_ib_rate(ah->attr.static_rate);
1863
1864         return 0;
1865 }
1866
1867 /**
1868  * ipath_get_npkeys - return the size of the PKEY table for port 0
1869  * @dd: the infinipath device
1870  */
1871 unsigned ipath_get_npkeys(struct ipath_devdata *dd)
1872 {
1873         return ARRAY_SIZE(dd->ipath_pd[0]->port_pkeys);
1874 }
1875
1876 /**
1877  * ipath_get_pkey - return the indexed PKEY from the port PKEY table
1878  * @dd: the infinipath device
1879  * @index: the PKEY index
1880  */
1881 unsigned ipath_get_pkey(struct ipath_devdata *dd, unsigned index)
1882 {
1883         unsigned ret;
1884
1885         /* always a kernel port, no locking needed */
1886         if (index >= ARRAY_SIZE(dd->ipath_pd[0]->port_pkeys))
1887                 ret = 0;
1888         else
1889                 ret = dd->ipath_pd[0]->port_pkeys[index];
1890
1891         return ret;
1892 }
1893
1894 static int ipath_query_pkey(struct ib_device *ibdev, u8 port, u16 index,
1895                             u16 *pkey)
1896 {
1897         struct ipath_ibdev *dev = to_idev(ibdev);
1898         int ret;
1899
1900         if (index >= ipath_get_npkeys(dev->dd)) {
1901                 ret = -EINVAL;
1902                 goto bail;
1903         }
1904
1905         *pkey = ipath_get_pkey(dev->dd, index);
1906         ret = 0;
1907
1908 bail:
1909         return ret;
1910 }
1911
1912 /**
1913  * ipath_alloc_ucontext - allocate a ucontest
1914  * @ibdev: the infiniband device
1915  * @udata: not used by the InfiniPath driver
1916  */
1917
1918 static struct ib_ucontext *ipath_alloc_ucontext(struct ib_device *ibdev,
1919                                                 struct ib_udata *udata)
1920 {
1921         struct ipath_ucontext *context;
1922         struct ib_ucontext *ret;
1923
1924         context = kmalloc(sizeof *context, GFP_KERNEL);
1925         if (!context) {
1926                 ret = ERR_PTR(-ENOMEM);
1927                 goto bail;
1928         }
1929
1930         ret = &context->ibucontext;
1931
1932 bail:
1933         return ret;
1934 }
1935
1936 static int ipath_dealloc_ucontext(struct ib_ucontext *context)
1937 {
1938         kfree(to_iucontext(context));
1939         return 0;
1940 }
1941
1942 static int ipath_verbs_register_sysfs(struct ib_device *dev);
1943
1944 static void __verbs_timer(unsigned long arg)
1945 {
1946         struct ipath_devdata *dd = (struct ipath_devdata *) arg;
1947
1948         /* Handle verbs layer timeouts. */
1949         ipath_ib_timer(dd->verbs_dev);
1950
1951         mod_timer(&dd->verbs_timer, jiffies + 1);
1952 }
1953
1954 static int enable_timer(struct ipath_devdata *dd)
1955 {
1956         /*
1957          * Early chips had a design flaw where the chip and kernel idea
1958          * of the tail register don't always agree, and therefore we won't
1959          * get an interrupt on the next packet received.
1960          * If the board supports per packet receive interrupts, use it.
1961          * Otherwise, the timer function periodically checks for packets
1962          * to cover this case.
1963          * Either way, the timer is needed for verbs layer related
1964          * processing.
1965          */
1966         if (dd->ipath_flags & IPATH_GPIO_INTR) {
1967                 ipath_write_kreg(dd, dd->ipath_kregs->kr_debugportselect,
1968                                  0x2074076542310ULL);
1969                 /* Enable GPIO bit 2 interrupt */
1970                 dd->ipath_gpio_mask |= (u64) (1 << IPATH_GPIO_PORT0_BIT);
1971                 ipath_write_kreg(dd, dd->ipath_kregs->kr_gpio_mask,
1972                                  dd->ipath_gpio_mask);
1973         }
1974
1975         init_timer(&dd->verbs_timer);
1976         dd->verbs_timer.function = __verbs_timer;
1977         dd->verbs_timer.data = (unsigned long)dd;
1978         dd->verbs_timer.expires = jiffies + 1;
1979         add_timer(&dd->verbs_timer);
1980
1981         return 0;
1982 }
1983
1984 static int disable_timer(struct ipath_devdata *dd)
1985 {
1986         /* Disable GPIO bit 2 interrupt */
1987         if (dd->ipath_flags & IPATH_GPIO_INTR) {
1988                 /* Disable GPIO bit 2 interrupt */
1989                 dd->ipath_gpio_mask &= ~((u64) (1 << IPATH_GPIO_PORT0_BIT));
1990                 ipath_write_kreg(dd, dd->ipath_kregs->kr_gpio_mask,
1991                                  dd->ipath_gpio_mask);
1992                 /*
1993                  * We might want to undo changes to debugportselect,
1994                  * but how?
1995                  */
1996         }
1997
1998         del_timer_sync(&dd->verbs_timer);
1999
2000         return 0;
2001 }
2002
2003 static int ipath_port_immutable(struct ib_device *ibdev, u8 port_num,
2004                                 struct ib_port_immutable *immutable)
2005 {
2006         struct ib_port_attr attr;
2007         int err;
2008
2009         err = ipath_query_port(ibdev, port_num, &attr);
2010         if (err)
2011                 return err;
2012
2013         immutable->pkey_tbl_len = attr.pkey_tbl_len;
2014         immutable->gid_tbl_len = attr.gid_tbl_len;
2015         immutable->core_cap_flags = RDMA_CORE_PORT_IBA_IB;
2016         immutable->max_mad_size = IB_MGMT_MAD_SIZE;
2017
2018         return 0;
2019 }
2020
2021 /**
2022  * ipath_register_ib_device - register our device with the infiniband core
2023  * @dd: the device data structure
2024  * Return the allocated ipath_ibdev pointer or NULL on error.
2025  */
2026 int ipath_register_ib_device(struct ipath_devdata *dd)
2027 {
2028         struct ipath_verbs_counters cntrs;
2029         struct ipath_ibdev *idev;
2030         struct ib_device *dev;
2031         struct ipath_verbs_txreq *tx;
2032         unsigned i;
2033         int ret;
2034
2035         idev = (struct ipath_ibdev *)ib_alloc_device(sizeof *idev);
2036         if (idev == NULL) {
2037                 ret = -ENOMEM;
2038                 goto bail;
2039         }
2040
2041         dev = &idev->ibdev;
2042
2043         if (dd->ipath_sdma_descq_cnt) {
2044                 tx = kmalloc(dd->ipath_sdma_descq_cnt * sizeof *tx,
2045                              GFP_KERNEL);
2046                 if (tx == NULL) {
2047                         ret = -ENOMEM;
2048                         goto err_tx;
2049                 }
2050         } else
2051                 tx = NULL;
2052         idev->txreq_bufs = tx;
2053
2054         /* Only need to initialize non-zero fields. */
2055         spin_lock_init(&idev->n_pds_lock);
2056         spin_lock_init(&idev->n_ahs_lock);
2057         spin_lock_init(&idev->n_cqs_lock);
2058         spin_lock_init(&idev->n_qps_lock);
2059         spin_lock_init(&idev->n_srqs_lock);
2060         spin_lock_init(&idev->n_mcast_grps_lock);
2061
2062         spin_lock_init(&idev->qp_table.lock);
2063         spin_lock_init(&idev->lk_table.lock);
2064         idev->sm_lid = be16_to_cpu(IB_LID_PERMISSIVE);
2065         /* Set the prefix to the default value (see ch. 4.1.1) */
2066         idev->gid_prefix = cpu_to_be64(0xfe80000000000000ULL);
2067
2068         ret = ipath_init_qp_table(idev, ib_ipath_qp_table_size);
2069         if (ret)
2070                 goto err_qp;
2071
2072         /*
2073          * The top ib_ipath_lkey_table_size bits are used to index the
2074          * table.  The lower 8 bits can be owned by the user (copied from
2075          * the LKEY).  The remaining bits act as a generation number or tag.
2076          */
2077         idev->lk_table.max = 1 << ib_ipath_lkey_table_size;
2078         idev->lk_table.table = kzalloc(idev->lk_table.max *
2079                                        sizeof(*idev->lk_table.table),
2080                                        GFP_KERNEL);
2081         if (idev->lk_table.table == NULL) {
2082                 ret = -ENOMEM;
2083                 goto err_lk;
2084         }
2085         INIT_LIST_HEAD(&idev->pending_mmaps);
2086         spin_lock_init(&idev->pending_lock);
2087         idev->mmap_offset = PAGE_SIZE;
2088         spin_lock_init(&idev->mmap_offset_lock);
2089         INIT_LIST_HEAD(&idev->pending[0]);
2090         INIT_LIST_HEAD(&idev->pending[1]);
2091         INIT_LIST_HEAD(&idev->pending[2]);
2092         INIT_LIST_HEAD(&idev->piowait);
2093         INIT_LIST_HEAD(&idev->rnrwait);
2094         INIT_LIST_HEAD(&idev->txreq_free);
2095         idev->pending_index = 0;
2096         idev->port_cap_flags =
2097                 IB_PORT_SYS_IMAGE_GUID_SUP | IB_PORT_CLIENT_REG_SUP;
2098         if (dd->ipath_flags & IPATH_HAS_LINK_LATENCY)
2099                 idev->port_cap_flags |= IB_PORT_LINK_LATENCY_SUP;
2100         idev->pma_counter_select[0] = IB_PMA_PORT_XMIT_DATA;
2101         idev->pma_counter_select[1] = IB_PMA_PORT_RCV_DATA;
2102         idev->pma_counter_select[2] = IB_PMA_PORT_XMIT_PKTS;
2103         idev->pma_counter_select[3] = IB_PMA_PORT_RCV_PKTS;
2104         idev->pma_counter_select[4] = IB_PMA_PORT_XMIT_WAIT;
2105
2106         /* Snapshot current HW counters to "clear" them. */
2107         ipath_get_counters(dd, &cntrs);
2108         idev->z_symbol_error_counter = cntrs.symbol_error_counter;
2109         idev->z_link_error_recovery_counter =
2110                 cntrs.link_error_recovery_counter;
2111         idev->z_link_downed_counter = cntrs.link_downed_counter;
2112         idev->z_port_rcv_errors = cntrs.port_rcv_errors;
2113         idev->z_port_rcv_remphys_errors =
2114                 cntrs.port_rcv_remphys_errors;
2115         idev->z_port_xmit_discards = cntrs.port_xmit_discards;
2116         idev->z_port_xmit_data = cntrs.port_xmit_data;
2117         idev->z_port_rcv_data = cntrs.port_rcv_data;
2118         idev->z_port_xmit_packets = cntrs.port_xmit_packets;
2119         idev->z_port_rcv_packets = cntrs.port_rcv_packets;
2120         idev->z_local_link_integrity_errors =
2121                 cntrs.local_link_integrity_errors;
2122         idev->z_excessive_buffer_overrun_errors =
2123                 cntrs.excessive_buffer_overrun_errors;
2124         idev->z_vl15_dropped = cntrs.vl15_dropped;
2125
2126         for (i = 0; i < dd->ipath_sdma_descq_cnt; i++, tx++)
2127                 list_add(&tx->txreq.list, &idev->txreq_free);
2128
2129         /*
2130          * The system image GUID is supposed to be the same for all
2131          * IB HCAs in a single system but since there can be other
2132          * device types in the system, we can't be sure this is unique.
2133          */
2134         if (!sys_image_guid)
2135                 sys_image_guid = dd->ipath_guid;
2136         idev->sys_image_guid = sys_image_guid;
2137         idev->ib_unit = dd->ipath_unit;
2138         idev->dd = dd;
2139
2140         strlcpy(dev->name, "ipath%d", IB_DEVICE_NAME_MAX);
2141         dev->owner = THIS_MODULE;
2142         dev->node_guid = dd->ipath_guid;
2143         dev->uverbs_abi_ver = IPATH_UVERBS_ABI_VERSION;
2144         dev->uverbs_cmd_mask =
2145                 (1ull << IB_USER_VERBS_CMD_GET_CONTEXT)         |
2146                 (1ull << IB_USER_VERBS_CMD_QUERY_DEVICE)        |
2147                 (1ull << IB_USER_VERBS_CMD_QUERY_PORT)          |
2148                 (1ull << IB_USER_VERBS_CMD_ALLOC_PD)            |
2149                 (1ull << IB_USER_VERBS_CMD_DEALLOC_PD)          |
2150                 (1ull << IB_USER_VERBS_CMD_CREATE_AH)           |
2151                 (1ull << IB_USER_VERBS_CMD_DESTROY_AH)          |
2152                 (1ull << IB_USER_VERBS_CMD_QUERY_AH)            |
2153                 (1ull << IB_USER_VERBS_CMD_REG_MR)              |
2154                 (1ull << IB_USER_VERBS_CMD_DEREG_MR)            |
2155                 (1ull << IB_USER_VERBS_CMD_CREATE_COMP_CHANNEL) |
2156                 (1ull << IB_USER_VERBS_CMD_CREATE_CQ)           |
2157                 (1ull << IB_USER_VERBS_CMD_RESIZE_CQ)           |
2158                 (1ull << IB_USER_VERBS_CMD_DESTROY_CQ)          |
2159                 (1ull << IB_USER_VERBS_CMD_POLL_CQ)             |
2160                 (1ull << IB_USER_VERBS_CMD_REQ_NOTIFY_CQ)       |
2161                 (1ull << IB_USER_VERBS_CMD_CREATE_QP)           |
2162                 (1ull << IB_USER_VERBS_CMD_QUERY_QP)            |
2163                 (1ull << IB_USER_VERBS_CMD_MODIFY_QP)           |
2164                 (1ull << IB_USER_VERBS_CMD_DESTROY_QP)          |
2165                 (1ull << IB_USER_VERBS_CMD_POST_SEND)           |
2166                 (1ull << IB_USER_VERBS_CMD_POST_RECV)           |
2167                 (1ull << IB_USER_VERBS_CMD_ATTACH_MCAST)        |
2168                 (1ull << IB_USER_VERBS_CMD_DETACH_MCAST)        |
2169                 (1ull << IB_USER_VERBS_CMD_CREATE_SRQ)          |
2170                 (1ull << IB_USER_VERBS_CMD_MODIFY_SRQ)          |
2171                 (1ull << IB_USER_VERBS_CMD_QUERY_SRQ)           |
2172                 (1ull << IB_USER_VERBS_CMD_DESTROY_SRQ)         |
2173                 (1ull << IB_USER_VERBS_CMD_POST_SRQ_RECV);
2174         dev->node_type = RDMA_NODE_IB_CA;
2175         dev->phys_port_cnt = 1;
2176         dev->num_comp_vectors = 1;
2177         dev->dma_device = &dd->pcidev->dev;
2178         dev->query_device = ipath_query_device;
2179         dev->modify_device = ipath_modify_device;
2180         dev->query_port = ipath_query_port;
2181         dev->modify_port = ipath_modify_port;
2182         dev->query_pkey = ipath_query_pkey;
2183         dev->query_gid = ipath_query_gid;
2184         dev->alloc_ucontext = ipath_alloc_ucontext;
2185         dev->dealloc_ucontext = ipath_dealloc_ucontext;
2186         dev->alloc_pd = ipath_alloc_pd;
2187         dev->dealloc_pd = ipath_dealloc_pd;
2188         dev->create_ah = ipath_create_ah;
2189         dev->destroy_ah = ipath_destroy_ah;
2190         dev->query_ah = ipath_query_ah;
2191         dev->create_srq = ipath_create_srq;
2192         dev->modify_srq = ipath_modify_srq;
2193         dev->query_srq = ipath_query_srq;
2194         dev->destroy_srq = ipath_destroy_srq;
2195         dev->create_qp = ipath_create_qp;
2196         dev->modify_qp = ipath_modify_qp;
2197         dev->query_qp = ipath_query_qp;
2198         dev->destroy_qp = ipath_destroy_qp;
2199         dev->post_send = ipath_post_send;
2200         dev->post_recv = ipath_post_receive;
2201         dev->post_srq_recv = ipath_post_srq_receive;
2202         dev->create_cq = ipath_create_cq;
2203         dev->destroy_cq = ipath_destroy_cq;
2204         dev->resize_cq = ipath_resize_cq;
2205         dev->poll_cq = ipath_poll_cq;
2206         dev->req_notify_cq = ipath_req_notify_cq;
2207         dev->get_dma_mr = ipath_get_dma_mr;
2208         dev->reg_phys_mr = ipath_reg_phys_mr;
2209         dev->reg_user_mr = ipath_reg_user_mr;
2210         dev->dereg_mr = ipath_dereg_mr;
2211         dev->alloc_fmr = ipath_alloc_fmr;
2212         dev->map_phys_fmr = ipath_map_phys_fmr;
2213         dev->unmap_fmr = ipath_unmap_fmr;
2214         dev->dealloc_fmr = ipath_dealloc_fmr;
2215         dev->attach_mcast = ipath_multicast_attach;
2216         dev->detach_mcast = ipath_multicast_detach;
2217         dev->process_mad = ipath_process_mad;
2218         dev->mmap = ipath_mmap;
2219         dev->dma_ops = &ipath_dma_mapping_ops;
2220         dev->get_port_immutable = ipath_port_immutable;
2221
2222         snprintf(dev->node_desc, sizeof(dev->node_desc),
2223                  IPATH_IDSTR " %s", init_utsname()->nodename);
2224
2225         ret = ib_register_device(dev, NULL);
2226         if (ret)
2227                 goto err_reg;
2228
2229         ret = ipath_verbs_register_sysfs(dev);
2230         if (ret)
2231                 goto err_class;
2232
2233         enable_timer(dd);
2234
2235         goto bail;
2236
2237 err_class:
2238         ib_unregister_device(dev);
2239 err_reg:
2240         kfree(idev->lk_table.table);
2241 err_lk:
2242         kfree(idev->qp_table.table);
2243 err_qp:
2244         kfree(idev->txreq_bufs);
2245 err_tx:
2246         ib_dealloc_device(dev);
2247         ipath_dev_err(dd, "cannot register verbs: %d!\n", -ret);
2248         idev = NULL;
2249
2250 bail:
2251         dd->verbs_dev = idev;
2252         return ret;
2253 }
2254
2255 void ipath_unregister_ib_device(struct ipath_ibdev *dev)
2256 {
2257         struct ib_device *ibdev = &dev->ibdev;
2258         u32 qps_inuse;
2259
2260         ib_unregister_device(ibdev);
2261
2262         disable_timer(dev->dd);
2263
2264         if (!list_empty(&dev->pending[0]) ||
2265             !list_empty(&dev->pending[1]) ||
2266             !list_empty(&dev->pending[2]))
2267                 ipath_dev_err(dev->dd, "pending list not empty!\n");
2268         if (!list_empty(&dev->piowait))
2269                 ipath_dev_err(dev->dd, "piowait list not empty!\n");
2270         if (!list_empty(&dev->rnrwait))
2271                 ipath_dev_err(dev->dd, "rnrwait list not empty!\n");
2272         if (!ipath_mcast_tree_empty())
2273                 ipath_dev_err(dev->dd, "multicast table memory leak!\n");
2274         /*
2275          * Note that ipath_unregister_ib_device() can be called before all
2276          * the QPs are destroyed!
2277          */
2278         qps_inuse = ipath_free_all_qps(&dev->qp_table);
2279         if (qps_inuse)
2280                 ipath_dev_err(dev->dd, "QP memory leak! %u still in use\n",
2281                         qps_inuse);
2282         kfree(dev->qp_table.table);
2283         kfree(dev->lk_table.table);
2284         kfree(dev->txreq_bufs);
2285         ib_dealloc_device(ibdev);
2286 }
2287
2288 static ssize_t show_rev(struct device *device, struct device_attribute *attr,
2289                         char *buf)
2290 {
2291         struct ipath_ibdev *dev =
2292                 container_of(device, struct ipath_ibdev, ibdev.dev);
2293
2294         return sprintf(buf, "%x\n", dev->dd->ipath_pcirev);
2295 }
2296
2297 static ssize_t show_hca(struct device *device, struct device_attribute *attr,
2298                         char *buf)
2299 {
2300         struct ipath_ibdev *dev =
2301                 container_of(device, struct ipath_ibdev, ibdev.dev);
2302         int ret;
2303
2304         ret = dev->dd->ipath_f_get_boardname(dev->dd, buf, 128);
2305         if (ret < 0)
2306                 goto bail;
2307         strcat(buf, "\n");
2308         ret = strlen(buf);
2309
2310 bail:
2311         return ret;
2312 }
2313
2314 static ssize_t show_stats(struct device *device, struct device_attribute *attr,
2315                           char *buf)
2316 {
2317         struct ipath_ibdev *dev =
2318                 container_of(device, struct ipath_ibdev, ibdev.dev);
2319         int i;
2320         int len;
2321
2322         len = sprintf(buf,
2323                       "RC resends  %d\n"
2324                       "RC no QACK  %d\n"
2325                       "RC ACKs     %d\n"
2326                       "RC SEQ NAKs %d\n"
2327                       "RC RDMA seq %d\n"
2328                       "RC RNR NAKs %d\n"
2329                       "RC OTH NAKs %d\n"
2330                       "RC timeouts %d\n"
2331                       "RC RDMA dup %d\n"
2332                       "piobuf wait %d\n"
2333                       "unaligned   %d\n"
2334                       "PKT drops   %d\n"
2335                       "WQE errs    %d\n",
2336                       dev->n_rc_resends, dev->n_rc_qacks, dev->n_rc_acks,
2337                       dev->n_seq_naks, dev->n_rdma_seq, dev->n_rnr_naks,
2338                       dev->n_other_naks, dev->n_timeouts,
2339                       dev->n_rdma_dup_busy, dev->n_piowait, dev->n_unaligned,
2340                       dev->n_pkt_drops, dev->n_wqe_errs);
2341         for (i = 0; i < ARRAY_SIZE(dev->opstats); i++) {
2342                 const struct ipath_opcode_stats *si = &dev->opstats[i];
2343
2344                 if (!si->n_packets && !si->n_bytes)
2345                         continue;
2346                 len += sprintf(buf + len, "%02x %llu/%llu\n", i,
2347                                (unsigned long long) si->n_packets,
2348                                (unsigned long long) si->n_bytes);
2349         }
2350         return len;
2351 }
2352
2353 static DEVICE_ATTR(hw_rev, S_IRUGO, show_rev, NULL);
2354 static DEVICE_ATTR(hca_type, S_IRUGO, show_hca, NULL);
2355 static DEVICE_ATTR(board_id, S_IRUGO, show_hca, NULL);
2356 static DEVICE_ATTR(stats, S_IRUGO, show_stats, NULL);
2357
2358 static struct device_attribute *ipath_class_attributes[] = {
2359         &dev_attr_hw_rev,
2360         &dev_attr_hca_type,
2361         &dev_attr_board_id,
2362         &dev_attr_stats
2363 };
2364
2365 static int ipath_verbs_register_sysfs(struct ib_device *dev)
2366 {
2367         int i;
2368         int ret;
2369
2370         for (i = 0; i < ARRAY_SIZE(ipath_class_attributes); ++i) {
2371                 ret = device_create_file(&dev->dev,
2372                                        ipath_class_attributes[i]);
2373                 if (ret)
2374                         goto bail;
2375         }
2376         return 0;
2377 bail:
2378         for (i = 0; i < ARRAY_SIZE(ipath_class_attributes); ++i)
2379                 device_remove_file(&dev->dev, ipath_class_attributes[i]);
2380         return ret;
2381 }