IB/ipath: put a limit on the number of QPs that can be created
[safe/jmp/linux-2.6] / drivers / infiniband / hw / ipath / ipath_verbs.c
1 /*
2  * Copyright (c) 2006 QLogic, Inc. All rights reserved.
3  * Copyright (c) 2005, 2006 PathScale, Inc. All rights reserved.
4  *
5  * This software is available to you under a choice of one of two
6  * licenses.  You may choose to be licensed under the terms of the GNU
7  * General Public License (GPL) Version 2, available from the file
8  * COPYING in the main directory of this source tree, or the
9  * OpenIB.org BSD license below:
10  *
11  *     Redistribution and use in source and binary forms, with or
12  *     without modification, are permitted provided that the following
13  *     conditions are met:
14  *
15  *      - Redistributions of source code must retain the above
16  *        copyright notice, this list of conditions and the following
17  *        disclaimer.
18  *
19  *      - Redistributions in binary form must reproduce the above
20  *        copyright notice, this list of conditions and the following
21  *        disclaimer in the documentation and/or other materials
22  *        provided with the distribution.
23  *
24  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
28  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
29  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
30  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
31  * SOFTWARE.
32  */
33
34 #include <rdma/ib_mad.h>
35 #include <rdma/ib_user_verbs.h>
36 #include <linux/io.h>
37 #include <linux/utsname.h>
38
39 #include "ipath_kernel.h"
40 #include "ipath_verbs.h"
41 #include "ipath_common.h"
42
43 static unsigned int ib_ipath_qp_table_size = 251;
44 module_param_named(qp_table_size, ib_ipath_qp_table_size, uint, S_IRUGO);
45 MODULE_PARM_DESC(qp_table_size, "QP table size");
46
47 unsigned int ib_ipath_lkey_table_size = 12;
48 module_param_named(lkey_table_size, ib_ipath_lkey_table_size, uint,
49                    S_IRUGO);
50 MODULE_PARM_DESC(lkey_table_size,
51                  "LKEY table size in bits (2^n, 1 <= n <= 23)");
52
53 static unsigned int ib_ipath_max_pds = 0xFFFF;
54 module_param_named(max_pds, ib_ipath_max_pds, uint, S_IWUSR | S_IRUGO);
55 MODULE_PARM_DESC(max_pds,
56                  "Maximum number of protection domains to support");
57
58 static unsigned int ib_ipath_max_ahs = 0xFFFF;
59 module_param_named(max_ahs, ib_ipath_max_ahs, uint, S_IWUSR | S_IRUGO);
60 MODULE_PARM_DESC(max_ahs, "Maximum number of address handles to support");
61
62 unsigned int ib_ipath_max_cqes = 0x2FFFF;
63 module_param_named(max_cqes, ib_ipath_max_cqes, uint, S_IWUSR | S_IRUGO);
64 MODULE_PARM_DESC(max_cqes,
65                  "Maximum number of completion queue entries to support");
66
67 unsigned int ib_ipath_max_cqs = 0x1FFFF;
68 module_param_named(max_cqs, ib_ipath_max_cqs, uint, S_IWUSR | S_IRUGO);
69 MODULE_PARM_DESC(max_cqs, "Maximum number of completion queues to support");
70
71 unsigned int ib_ipath_max_qp_wrs = 0x3FFF;
72 module_param_named(max_qp_wrs, ib_ipath_max_qp_wrs, uint,
73                    S_IWUSR | S_IRUGO);
74 MODULE_PARM_DESC(max_qp_wrs, "Maximum number of QP WRs to support");
75
76 unsigned int ib_ipath_max_qps = 16384;
77 module_param_named(max_qps, ib_ipath_max_qps, uint, S_IWUSR | S_IRUGO);
78 MODULE_PARM_DESC(max_qps, "Maximum number of QPs to support");
79
80 unsigned int ib_ipath_max_sges = 0x60;
81 module_param_named(max_sges, ib_ipath_max_sges, uint, S_IWUSR | S_IRUGO);
82 MODULE_PARM_DESC(max_sges, "Maximum number of SGEs to support");
83
84 unsigned int ib_ipath_max_mcast_grps = 16384;
85 module_param_named(max_mcast_grps, ib_ipath_max_mcast_grps, uint,
86                    S_IWUSR | S_IRUGO);
87 MODULE_PARM_DESC(max_mcast_grps,
88                  "Maximum number of multicast groups to support");
89
90 unsigned int ib_ipath_max_mcast_qp_attached = 16;
91 module_param_named(max_mcast_qp_attached, ib_ipath_max_mcast_qp_attached,
92                    uint, S_IWUSR | S_IRUGO);
93 MODULE_PARM_DESC(max_mcast_qp_attached,
94                  "Maximum number of attached QPs to support");
95
96 unsigned int ib_ipath_max_srqs = 1024;
97 module_param_named(max_srqs, ib_ipath_max_srqs, uint, S_IWUSR | S_IRUGO);
98 MODULE_PARM_DESC(max_srqs, "Maximum number of SRQs to support");
99
100 unsigned int ib_ipath_max_srq_sges = 128;
101 module_param_named(max_srq_sges, ib_ipath_max_srq_sges,
102                    uint, S_IWUSR | S_IRUGO);
103 MODULE_PARM_DESC(max_srq_sges, "Maximum number of SRQ SGEs to support");
104
105 unsigned int ib_ipath_max_srq_wrs = 0x1FFFF;
106 module_param_named(max_srq_wrs, ib_ipath_max_srq_wrs,
107                    uint, S_IWUSR | S_IRUGO);
108 MODULE_PARM_DESC(max_srq_wrs, "Maximum number of SRQ WRs support");
109
110 const int ib_ipath_state_ops[IB_QPS_ERR + 1] = {
111         [IB_QPS_RESET] = 0,
112         [IB_QPS_INIT] = IPATH_POST_RECV_OK,
113         [IB_QPS_RTR] = IPATH_POST_RECV_OK | IPATH_PROCESS_RECV_OK,
114         [IB_QPS_RTS] = IPATH_POST_RECV_OK | IPATH_PROCESS_RECV_OK |
115             IPATH_POST_SEND_OK | IPATH_PROCESS_SEND_OK,
116         [IB_QPS_SQD] = IPATH_POST_RECV_OK | IPATH_PROCESS_RECV_OK |
117             IPATH_POST_SEND_OK,
118         [IB_QPS_SQE] = IPATH_POST_RECV_OK | IPATH_PROCESS_RECV_OK,
119         [IB_QPS_ERR] = 0,
120 };
121
122 struct ipath_ucontext {
123         struct ib_ucontext ibucontext;
124 };
125
126 static inline struct ipath_ucontext *to_iucontext(struct ib_ucontext
127                                                   *ibucontext)
128 {
129         return container_of(ibucontext, struct ipath_ucontext, ibucontext);
130 }
131
132 /*
133  * Translate ib_wr_opcode into ib_wc_opcode.
134  */
135 const enum ib_wc_opcode ib_ipath_wc_opcode[] = {
136         [IB_WR_RDMA_WRITE] = IB_WC_RDMA_WRITE,
137         [IB_WR_RDMA_WRITE_WITH_IMM] = IB_WC_RDMA_WRITE,
138         [IB_WR_SEND] = IB_WC_SEND,
139         [IB_WR_SEND_WITH_IMM] = IB_WC_SEND,
140         [IB_WR_RDMA_READ] = IB_WC_RDMA_READ,
141         [IB_WR_ATOMIC_CMP_AND_SWP] = IB_WC_COMP_SWAP,
142         [IB_WR_ATOMIC_FETCH_AND_ADD] = IB_WC_FETCH_ADD
143 };
144
145 /*
146  * System image GUID.
147  */
148 static __be64 sys_image_guid;
149
150 /**
151  * ipath_copy_sge - copy data to SGE memory
152  * @ss: the SGE state
153  * @data: the data to copy
154  * @length: the length of the data
155  */
156 void ipath_copy_sge(struct ipath_sge_state *ss, void *data, u32 length)
157 {
158         struct ipath_sge *sge = &ss->sge;
159
160         while (length) {
161                 u32 len = sge->length;
162
163                 BUG_ON(len == 0);
164                 if (len > length)
165                         len = length;
166                 memcpy(sge->vaddr, data, len);
167                 sge->vaddr += len;
168                 sge->length -= len;
169                 sge->sge_length -= len;
170                 if (sge->sge_length == 0) {
171                         if (--ss->num_sge)
172                                 *sge = *ss->sg_list++;
173                 } else if (sge->length == 0 && sge->mr != NULL) {
174                         if (++sge->n >= IPATH_SEGSZ) {
175                                 if (++sge->m >= sge->mr->mapsz)
176                                         break;
177                                 sge->n = 0;
178                         }
179                         sge->vaddr =
180                                 sge->mr->map[sge->m]->segs[sge->n].vaddr;
181                         sge->length =
182                                 sge->mr->map[sge->m]->segs[sge->n].length;
183                 }
184                 data += len;
185                 length -= len;
186         }
187 }
188
189 /**
190  * ipath_skip_sge - skip over SGE memory - XXX almost dup of prev func
191  * @ss: the SGE state
192  * @length: the number of bytes to skip
193  */
194 void ipath_skip_sge(struct ipath_sge_state *ss, u32 length)
195 {
196         struct ipath_sge *sge = &ss->sge;
197
198         while (length) {
199                 u32 len = sge->length;
200
201                 BUG_ON(len == 0);
202                 if (len > length)
203                         len = length;
204                 sge->vaddr += len;
205                 sge->length -= len;
206                 sge->sge_length -= len;
207                 if (sge->sge_length == 0) {
208                         if (--ss->num_sge)
209                                 *sge = *ss->sg_list++;
210                 } else if (sge->length == 0 && sge->mr != NULL) {
211                         if (++sge->n >= IPATH_SEGSZ) {
212                                 if (++sge->m >= sge->mr->mapsz)
213                                         break;
214                                 sge->n = 0;
215                         }
216                         sge->vaddr =
217                                 sge->mr->map[sge->m]->segs[sge->n].vaddr;
218                         sge->length =
219                                 sge->mr->map[sge->m]->segs[sge->n].length;
220                 }
221                 length -= len;
222         }
223 }
224
225 /**
226  * ipath_post_send - post a send on a QP
227  * @ibqp: the QP to post the send on
228  * @wr: the list of work requests to post
229  * @bad_wr: the first bad WR is put here
230  *
231  * This may be called from interrupt context.
232  */
233 static int ipath_post_send(struct ib_qp *ibqp, struct ib_send_wr *wr,
234                            struct ib_send_wr **bad_wr)
235 {
236         struct ipath_qp *qp = to_iqp(ibqp);
237         int err = 0;
238
239         /* Check that state is OK to post send. */
240         if (!(ib_ipath_state_ops[qp->state] & IPATH_POST_SEND_OK)) {
241                 *bad_wr = wr;
242                 err = -EINVAL;
243                 goto bail;
244         }
245
246         for (; wr; wr = wr->next) {
247                 switch (qp->ibqp.qp_type) {
248                 case IB_QPT_UC:
249                 case IB_QPT_RC:
250                         err = ipath_post_ruc_send(qp, wr);
251                         break;
252
253                 case IB_QPT_SMI:
254                 case IB_QPT_GSI:
255                 case IB_QPT_UD:
256                         err = ipath_post_ud_send(qp, wr);
257                         break;
258
259                 default:
260                         err = -EINVAL;
261                 }
262                 if (err) {
263                         *bad_wr = wr;
264                         break;
265                 }
266         }
267
268 bail:
269         return err;
270 }
271
272 /**
273  * ipath_post_receive - post a receive on a QP
274  * @ibqp: the QP to post the receive on
275  * @wr: the WR to post
276  * @bad_wr: the first bad WR is put here
277  *
278  * This may be called from interrupt context.
279  */
280 static int ipath_post_receive(struct ib_qp *ibqp, struct ib_recv_wr *wr,
281                               struct ib_recv_wr **bad_wr)
282 {
283         struct ipath_qp *qp = to_iqp(ibqp);
284         struct ipath_rwq *wq = qp->r_rq.wq;
285         unsigned long flags;
286         int ret;
287
288         /* Check that state is OK to post receive. */
289         if (!(ib_ipath_state_ops[qp->state] & IPATH_POST_RECV_OK) || !wq) {
290                 *bad_wr = wr;
291                 ret = -EINVAL;
292                 goto bail;
293         }
294
295         for (; wr; wr = wr->next) {
296                 struct ipath_rwqe *wqe;
297                 u32 next;
298                 int i;
299
300                 if ((unsigned) wr->num_sge > qp->r_rq.max_sge) {
301                         *bad_wr = wr;
302                         ret = -ENOMEM;
303                         goto bail;
304                 }
305
306                 spin_lock_irqsave(&qp->r_rq.lock, flags);
307                 next = wq->head + 1;
308                 if (next >= qp->r_rq.size)
309                         next = 0;
310                 if (next == wq->tail) {
311                         spin_unlock_irqrestore(&qp->r_rq.lock, flags);
312                         *bad_wr = wr;
313                         ret = -ENOMEM;
314                         goto bail;
315                 }
316
317                 wqe = get_rwqe_ptr(&qp->r_rq, wq->head);
318                 wqe->wr_id = wr->wr_id;
319                 wqe->num_sge = wr->num_sge;
320                 for (i = 0; i < wr->num_sge; i++)
321                         wqe->sg_list[i] = wr->sg_list[i];
322                 wq->head = next;
323                 spin_unlock_irqrestore(&qp->r_rq.lock, flags);
324         }
325         ret = 0;
326
327 bail:
328         return ret;
329 }
330
331 /**
332  * ipath_qp_rcv - processing an incoming packet on a QP
333  * @dev: the device the packet came on
334  * @hdr: the packet header
335  * @has_grh: true if the packet has a GRH
336  * @data: the packet data
337  * @tlen: the packet length
338  * @qp: the QP the packet came on
339  *
340  * This is called from ipath_ib_rcv() to process an incoming packet
341  * for the given QP.
342  * Called at interrupt level.
343  */
344 static void ipath_qp_rcv(struct ipath_ibdev *dev,
345                          struct ipath_ib_header *hdr, int has_grh,
346                          void *data, u32 tlen, struct ipath_qp *qp)
347 {
348         /* Check for valid receive state. */
349         if (!(ib_ipath_state_ops[qp->state] & IPATH_PROCESS_RECV_OK)) {
350                 dev->n_pkt_drops++;
351                 return;
352         }
353
354         switch (qp->ibqp.qp_type) {
355         case IB_QPT_SMI:
356         case IB_QPT_GSI:
357         case IB_QPT_UD:
358                 ipath_ud_rcv(dev, hdr, has_grh, data, tlen, qp);
359                 break;
360
361         case IB_QPT_RC:
362                 ipath_rc_rcv(dev, hdr, has_grh, data, tlen, qp);
363                 break;
364
365         case IB_QPT_UC:
366                 ipath_uc_rcv(dev, hdr, has_grh, data, tlen, qp);
367                 break;
368
369         default:
370                 break;
371         }
372 }
373
374 /**
375  * ipath_ib_rcv - process an incoming packet
376  * @arg: the device pointer
377  * @rhdr: the header of the packet
378  * @data: the packet data
379  * @tlen: the packet length
380  *
381  * This is called from ipath_kreceive() to process an incoming packet at
382  * interrupt level. Tlen is the length of the header + data + CRC in bytes.
383  */
384 void ipath_ib_rcv(struct ipath_ibdev *dev, void *rhdr, void *data,
385                   u32 tlen)
386 {
387         struct ipath_ib_header *hdr = rhdr;
388         struct ipath_other_headers *ohdr;
389         struct ipath_qp *qp;
390         u32 qp_num;
391         int lnh;
392         u8 opcode;
393         u16 lid;
394
395         if (unlikely(dev == NULL))
396                 goto bail;
397
398         if (unlikely(tlen < 24)) {      /* LRH+BTH+CRC */
399                 dev->rcv_errors++;
400                 goto bail;
401         }
402
403         /* Check for a valid destination LID (see ch. 7.11.1). */
404         lid = be16_to_cpu(hdr->lrh[1]);
405         if (lid < IPATH_MULTICAST_LID_BASE) {
406                 lid &= ~((1 << (dev->mkeyprot_resv_lmc & 7)) - 1);
407                 if (unlikely(lid != dev->dd->ipath_lid)) {
408                         dev->rcv_errors++;
409                         goto bail;
410                 }
411         }
412
413         /* Check for GRH */
414         lnh = be16_to_cpu(hdr->lrh[0]) & 3;
415         if (lnh == IPATH_LRH_BTH)
416                 ohdr = &hdr->u.oth;
417         else if (lnh == IPATH_LRH_GRH)
418                 ohdr = &hdr->u.l.oth;
419         else {
420                 dev->rcv_errors++;
421                 goto bail;
422         }
423
424         opcode = be32_to_cpu(ohdr->bth[0]) >> 24;
425         dev->opstats[opcode].n_bytes += tlen;
426         dev->opstats[opcode].n_packets++;
427
428         /* Get the destination QP number. */
429         qp_num = be32_to_cpu(ohdr->bth[1]) & IPATH_QPN_MASK;
430         if (qp_num == IPATH_MULTICAST_QPN) {
431                 struct ipath_mcast *mcast;
432                 struct ipath_mcast_qp *p;
433
434                 mcast = ipath_mcast_find(&hdr->u.l.grh.dgid);
435                 if (mcast == NULL) {
436                         dev->n_pkt_drops++;
437                         goto bail;
438                 }
439                 dev->n_multicast_rcv++;
440                 list_for_each_entry_rcu(p, &mcast->qp_list, list)
441                         ipath_qp_rcv(dev, hdr, lnh == IPATH_LRH_GRH, data,
442                                      tlen, p->qp);
443                 /*
444                  * Notify ipath_multicast_detach() if it is waiting for us
445                  * to finish.
446                  */
447                 if (atomic_dec_return(&mcast->refcount) <= 1)
448                         wake_up(&mcast->wait);
449         } else {
450                 qp = ipath_lookup_qpn(&dev->qp_table, qp_num);
451                 if (qp) {
452                         dev->n_unicast_rcv++;
453                         ipath_qp_rcv(dev, hdr, lnh == IPATH_LRH_GRH, data,
454                                      tlen, qp);
455                         /*
456                          * Notify ipath_destroy_qp() if it is waiting
457                          * for us to finish.
458                          */
459                         if (atomic_dec_and_test(&qp->refcount))
460                                 wake_up(&qp->wait);
461                 } else
462                         dev->n_pkt_drops++;
463         }
464
465 bail:;
466 }
467
468 /**
469  * ipath_ib_timer - verbs timer
470  * @arg: the device pointer
471  *
472  * This is called from ipath_do_rcv_timer() at interrupt level to check for
473  * QPs which need retransmits and to collect performance numbers.
474  */
475 void ipath_ib_timer(struct ipath_ibdev *dev)
476 {
477         struct ipath_qp *resend = NULL;
478         struct list_head *last;
479         struct ipath_qp *qp;
480         unsigned long flags;
481
482         if (dev == NULL)
483                 return;
484
485         spin_lock_irqsave(&dev->pending_lock, flags);
486         /* Start filling the next pending queue. */
487         if (++dev->pending_index >= ARRAY_SIZE(dev->pending))
488                 dev->pending_index = 0;
489         /* Save any requests still in the new queue, they have timed out. */
490         last = &dev->pending[dev->pending_index];
491         while (!list_empty(last)) {
492                 qp = list_entry(last->next, struct ipath_qp, timerwait);
493                 list_del_init(&qp->timerwait);
494                 qp->timer_next = resend;
495                 resend = qp;
496                 atomic_inc(&qp->refcount);
497         }
498         last = &dev->rnrwait;
499         if (!list_empty(last)) {
500                 qp = list_entry(last->next, struct ipath_qp, timerwait);
501                 if (--qp->s_rnr_timeout == 0) {
502                         do {
503                                 list_del_init(&qp->timerwait);
504                                 tasklet_hi_schedule(&qp->s_task);
505                                 if (list_empty(last))
506                                         break;
507                                 qp = list_entry(last->next, struct ipath_qp,
508                                                 timerwait);
509                         } while (qp->s_rnr_timeout == 0);
510                 }
511         }
512         /*
513          * We should only be in the started state if pma_sample_start != 0
514          */
515         if (dev->pma_sample_status == IB_PMA_SAMPLE_STATUS_STARTED &&
516             --dev->pma_sample_start == 0) {
517                 dev->pma_sample_status = IB_PMA_SAMPLE_STATUS_RUNNING;
518                 ipath_snapshot_counters(dev->dd, &dev->ipath_sword,
519                                         &dev->ipath_rword,
520                                         &dev->ipath_spkts,
521                                         &dev->ipath_rpkts,
522                                         &dev->ipath_xmit_wait);
523         }
524         if (dev->pma_sample_status == IB_PMA_SAMPLE_STATUS_RUNNING) {
525                 if (dev->pma_sample_interval == 0) {
526                         u64 ta, tb, tc, td, te;
527
528                         dev->pma_sample_status = IB_PMA_SAMPLE_STATUS_DONE;
529                         ipath_snapshot_counters(dev->dd, &ta, &tb,
530                                                 &tc, &td, &te);
531
532                         dev->ipath_sword = ta - dev->ipath_sword;
533                         dev->ipath_rword = tb - dev->ipath_rword;
534                         dev->ipath_spkts = tc - dev->ipath_spkts;
535                         dev->ipath_rpkts = td - dev->ipath_rpkts;
536                         dev->ipath_xmit_wait = te - dev->ipath_xmit_wait;
537                 }
538                 else
539                         dev->pma_sample_interval--;
540         }
541         spin_unlock_irqrestore(&dev->pending_lock, flags);
542
543         /* XXX What if timer fires again while this is running? */
544         for (qp = resend; qp != NULL; qp = qp->timer_next) {
545                 struct ib_wc wc;
546
547                 spin_lock_irqsave(&qp->s_lock, flags);
548                 if (qp->s_last != qp->s_tail && qp->state == IB_QPS_RTS) {
549                         dev->n_timeouts++;
550                         ipath_restart_rc(qp, qp->s_last_psn + 1, &wc);
551                 }
552                 spin_unlock_irqrestore(&qp->s_lock, flags);
553
554                 /* Notify ipath_destroy_qp() if it is waiting. */
555                 if (atomic_dec_and_test(&qp->refcount))
556                         wake_up(&qp->wait);
557         }
558 }
559
560 static void update_sge(struct ipath_sge_state *ss, u32 length)
561 {
562         struct ipath_sge *sge = &ss->sge;
563
564         sge->vaddr += length;
565         sge->length -= length;
566         sge->sge_length -= length;
567         if (sge->sge_length == 0) {
568                 if (--ss->num_sge)
569                         *sge = *ss->sg_list++;
570         } else if (sge->length == 0 && sge->mr != NULL) {
571                 if (++sge->n >= IPATH_SEGSZ) {
572                         if (++sge->m >= sge->mr->mapsz)
573                                 return;
574                         sge->n = 0;
575                 }
576                 sge->vaddr = sge->mr->map[sge->m]->segs[sge->n].vaddr;
577                 sge->length = sge->mr->map[sge->m]->segs[sge->n].length;
578         }
579 }
580
581 #ifdef __LITTLE_ENDIAN
582 static inline u32 get_upper_bits(u32 data, u32 shift)
583 {
584         return data >> shift;
585 }
586
587 static inline u32 set_upper_bits(u32 data, u32 shift)
588 {
589         return data << shift;
590 }
591
592 static inline u32 clear_upper_bytes(u32 data, u32 n, u32 off)
593 {
594         data <<= ((sizeof(u32) - n) * BITS_PER_BYTE);
595         data >>= ((sizeof(u32) - n - off) * BITS_PER_BYTE);
596         return data;
597 }
598 #else
599 static inline u32 get_upper_bits(u32 data, u32 shift)
600 {
601         return data << shift;
602 }
603
604 static inline u32 set_upper_bits(u32 data, u32 shift)
605 {
606         return data >> shift;
607 }
608
609 static inline u32 clear_upper_bytes(u32 data, u32 n, u32 off)
610 {
611         data >>= ((sizeof(u32) - n) * BITS_PER_BYTE);
612         data <<= ((sizeof(u32) - n - off) * BITS_PER_BYTE);
613         return data;
614 }
615 #endif
616
617 static void copy_io(u32 __iomem *piobuf, struct ipath_sge_state *ss,
618                     u32 length)
619 {
620         u32 extra = 0;
621         u32 data = 0;
622         u32 last;
623
624         while (1) {
625                 u32 len = ss->sge.length;
626                 u32 off;
627
628                 BUG_ON(len == 0);
629                 if (len > length)
630                         len = length;
631                 if (len > ss->sge.sge_length)
632                         len = ss->sge.sge_length;
633                 /* If the source address is not aligned, try to align it. */
634                 off = (unsigned long)ss->sge.vaddr & (sizeof(u32) - 1);
635                 if (off) {
636                         u32 *addr = (u32 *)((unsigned long)ss->sge.vaddr &
637                                             ~(sizeof(u32) - 1));
638                         u32 v = get_upper_bits(*addr, off * BITS_PER_BYTE);
639                         u32 y;
640
641                         y = sizeof(u32) - off;
642                         if (len > y)
643                                 len = y;
644                         if (len + extra >= sizeof(u32)) {
645                                 data |= set_upper_bits(v, extra *
646                                                        BITS_PER_BYTE);
647                                 len = sizeof(u32) - extra;
648                                 if (len == length) {
649                                         last = data;
650                                         break;
651                                 }
652                                 __raw_writel(data, piobuf);
653                                 piobuf++;
654                                 extra = 0;
655                                 data = 0;
656                         } else {
657                                 /* Clear unused upper bytes */
658                                 data |= clear_upper_bytes(v, len, extra);
659                                 if (len == length) {
660                                         last = data;
661                                         break;
662                                 }
663                                 extra += len;
664                         }
665                 } else if (extra) {
666                         /* Source address is aligned. */
667                         u32 *addr = (u32 *) ss->sge.vaddr;
668                         int shift = extra * BITS_PER_BYTE;
669                         int ushift = 32 - shift;
670                         u32 l = len;
671
672                         while (l >= sizeof(u32)) {
673                                 u32 v = *addr;
674
675                                 data |= set_upper_bits(v, shift);
676                                 __raw_writel(data, piobuf);
677                                 data = get_upper_bits(v, ushift);
678                                 piobuf++;
679                                 addr++;
680                                 l -= sizeof(u32);
681                         }
682                         /*
683                          * We still have 'extra' number of bytes leftover.
684                          */
685                         if (l) {
686                                 u32 v = *addr;
687
688                                 if (l + extra >= sizeof(u32)) {
689                                         data |= set_upper_bits(v, shift);
690                                         len -= l + extra - sizeof(u32);
691                                         if (len == length) {
692                                                 last = data;
693                                                 break;
694                                         }
695                                         __raw_writel(data, piobuf);
696                                         piobuf++;
697                                         extra = 0;
698                                         data = 0;
699                                 } else {
700                                         /* Clear unused upper bytes */
701                                         data |= clear_upper_bytes(v, l,
702                                                                   extra);
703                                         if (len == length) {
704                                                 last = data;
705                                                 break;
706                                         }
707                                         extra += l;
708                                 }
709                         } else if (len == length) {
710                                 last = data;
711                                 break;
712                         }
713                 } else if (len == length) {
714                         u32 w;
715
716                         /*
717                          * Need to round up for the last dword in the
718                          * packet.
719                          */
720                         w = (len + 3) >> 2;
721                         __iowrite32_copy(piobuf, ss->sge.vaddr, w - 1);
722                         piobuf += w - 1;
723                         last = ((u32 *) ss->sge.vaddr)[w - 1];
724                         break;
725                 } else {
726                         u32 w = len >> 2;
727
728                         __iowrite32_copy(piobuf, ss->sge.vaddr, w);
729                         piobuf += w;
730
731                         extra = len & (sizeof(u32) - 1);
732                         if (extra) {
733                                 u32 v = ((u32 *) ss->sge.vaddr)[w];
734
735                                 /* Clear unused upper bytes */
736                                 data = clear_upper_bytes(v, extra, 0);
737                         }
738                 }
739                 update_sge(ss, len);
740                 length -= len;
741         }
742         /* Update address before sending packet. */
743         update_sge(ss, length);
744         /* must flush early everything before trigger word */
745         ipath_flush_wc();
746         __raw_writel(last, piobuf);
747         /* be sure trigger word is written */
748         ipath_flush_wc();
749 }
750
751 /**
752  * ipath_verbs_send - send a packet
753  * @dd: the infinipath device
754  * @hdrwords: the number of words in the header
755  * @hdr: the packet header
756  * @len: the length of the packet in bytes
757  * @ss: the SGE to send
758  */
759 int ipath_verbs_send(struct ipath_devdata *dd, u32 hdrwords,
760                      u32 *hdr, u32 len, struct ipath_sge_state *ss)
761 {
762         u32 __iomem *piobuf;
763         u32 plen;
764         int ret;
765
766         /* +1 is for the qword padding of pbc */
767         plen = hdrwords + ((len + 3) >> 2) + 1;
768         if (unlikely((plen << 2) > dd->ipath_ibmaxlen)) {
769                 ipath_dbg("packet len 0x%x too long, failing\n", plen);
770                 ret = -EINVAL;
771                 goto bail;
772         }
773
774         /* Get a PIO buffer to use. */
775         piobuf = ipath_getpiobuf(dd, NULL);
776         if (unlikely(piobuf == NULL)) {
777                 ret = -EBUSY;
778                 goto bail;
779         }
780
781         /*
782          * Write len to control qword, no flags.
783          * We have to flush after the PBC for correctness on some cpus
784          * or WC buffer can be written out of order.
785          */
786         writeq(plen, piobuf);
787         ipath_flush_wc();
788         piobuf += 2;
789         if (len == 0) {
790                 /*
791                  * If there is just the header portion, must flush before
792                  * writing last word of header for correctness, and after
793                  * the last header word (trigger word).
794                  */
795                 __iowrite32_copy(piobuf, hdr, hdrwords - 1);
796                 ipath_flush_wc();
797                 __raw_writel(hdr[hdrwords - 1], piobuf + hdrwords - 1);
798                 ipath_flush_wc();
799                 ret = 0;
800                 goto bail;
801         }
802
803         __iowrite32_copy(piobuf, hdr, hdrwords);
804         piobuf += hdrwords;
805
806         /* The common case is aligned and contained in one segment. */
807         if (likely(ss->num_sge == 1 && len <= ss->sge.length &&
808                    !((unsigned long)ss->sge.vaddr & (sizeof(u32) - 1)))) {
809                 u32 w;
810                 u32 *addr = (u32 *) ss->sge.vaddr;
811
812                 /* Update address before sending packet. */
813                 update_sge(ss, len);
814                 /* Need to round up for the last dword in the packet. */
815                 w = (len + 3) >> 2;
816                 __iowrite32_copy(piobuf, addr, w - 1);
817                 /* must flush early everything before trigger word */
818                 ipath_flush_wc();
819                 __raw_writel(addr[w - 1], piobuf + w - 1);
820                 /* be sure trigger word is written */
821                 ipath_flush_wc();
822                 ret = 0;
823                 goto bail;
824         }
825         copy_io(piobuf, ss, len);
826         ret = 0;
827
828 bail:
829         return ret;
830 }
831
832 int ipath_snapshot_counters(struct ipath_devdata *dd, u64 *swords,
833                             u64 *rwords, u64 *spkts, u64 *rpkts,
834                             u64 *xmit_wait)
835 {
836         int ret;
837
838         if (!(dd->ipath_flags & IPATH_INITTED)) {
839                 /* no hardware, freeze, etc. */
840                 ipath_dbg("unit %u not usable\n", dd->ipath_unit);
841                 ret = -EINVAL;
842                 goto bail;
843         }
844         *swords = ipath_snap_cntr(dd, dd->ipath_cregs->cr_wordsendcnt);
845         *rwords = ipath_snap_cntr(dd, dd->ipath_cregs->cr_wordrcvcnt);
846         *spkts = ipath_snap_cntr(dd, dd->ipath_cregs->cr_pktsendcnt);
847         *rpkts = ipath_snap_cntr(dd, dd->ipath_cregs->cr_pktrcvcnt);
848         *xmit_wait = ipath_snap_cntr(dd, dd->ipath_cregs->cr_sendstallcnt);
849
850         ret = 0;
851
852 bail:
853         return ret;
854 }
855
856 /**
857  * ipath_get_counters - get various chip counters
858  * @dd: the infinipath device
859  * @cntrs: counters are placed here
860  *
861  * Return the counters needed by recv_pma_get_portcounters().
862  */
863 int ipath_get_counters(struct ipath_devdata *dd,
864                        struct ipath_verbs_counters *cntrs)
865 {
866         int ret;
867
868         if (!(dd->ipath_flags & IPATH_INITTED)) {
869                 /* no hardware, freeze, etc. */
870                 ipath_dbg("unit %u not usable\n", dd->ipath_unit);
871                 ret = -EINVAL;
872                 goto bail;
873         }
874         cntrs->symbol_error_counter =
875                 ipath_snap_cntr(dd, dd->ipath_cregs->cr_ibsymbolerrcnt);
876         cntrs->link_error_recovery_counter =
877                 ipath_snap_cntr(dd, dd->ipath_cregs->cr_iblinkerrrecovcnt);
878         /*
879          * The link downed counter counts when the other side downs the
880          * connection.  We add in the number of times we downed the link
881          * due to local link integrity errors to compensate.
882          */
883         cntrs->link_downed_counter =
884                 ipath_snap_cntr(dd, dd->ipath_cregs->cr_iblinkdowncnt);
885         cntrs->port_rcv_errors =
886                 ipath_snap_cntr(dd, dd->ipath_cregs->cr_rxdroppktcnt) +
887                 ipath_snap_cntr(dd, dd->ipath_cregs->cr_rcvovflcnt) +
888                 ipath_snap_cntr(dd, dd->ipath_cregs->cr_portovflcnt) +
889                 ipath_snap_cntr(dd, dd->ipath_cregs->cr_err_rlencnt) +
890                 ipath_snap_cntr(dd, dd->ipath_cregs->cr_invalidrlencnt) +
891                 ipath_snap_cntr(dd, dd->ipath_cregs->cr_erricrccnt) +
892                 ipath_snap_cntr(dd, dd->ipath_cregs->cr_errvcrccnt) +
893                 ipath_snap_cntr(dd, dd->ipath_cregs->cr_errlpcrccnt) +
894                 ipath_snap_cntr(dd, dd->ipath_cregs->cr_badformatcnt);
895         cntrs->port_rcv_remphys_errors =
896                 ipath_snap_cntr(dd, dd->ipath_cregs->cr_rcvebpcnt);
897         cntrs->port_xmit_discards =
898                 ipath_snap_cntr(dd, dd->ipath_cregs->cr_unsupvlcnt);
899         cntrs->port_xmit_data =
900                 ipath_snap_cntr(dd, dd->ipath_cregs->cr_wordsendcnt);
901         cntrs->port_rcv_data =
902                 ipath_snap_cntr(dd, dd->ipath_cregs->cr_wordrcvcnt);
903         cntrs->port_xmit_packets =
904                 ipath_snap_cntr(dd, dd->ipath_cregs->cr_pktsendcnt);
905         cntrs->port_rcv_packets =
906                 ipath_snap_cntr(dd, dd->ipath_cregs->cr_pktrcvcnt);
907         cntrs->local_link_integrity_errors = dd->ipath_lli_errors;
908         cntrs->excessive_buffer_overrun_errors = 0; /* XXX */
909
910         ret = 0;
911
912 bail:
913         return ret;
914 }
915
916 /**
917  * ipath_ib_piobufavail - callback when a PIO buffer is available
918  * @arg: the device pointer
919  *
920  * This is called from ipath_intr() at interrupt level when a PIO buffer is
921  * available after ipath_verbs_send() returned an error that no buffers were
922  * available.  Return 1 if we consumed all the PIO buffers and we still have
923  * QPs waiting for buffers (for now, just do a tasklet_hi_schedule and
924  * return zero).
925  */
926 int ipath_ib_piobufavail(struct ipath_ibdev *dev)
927 {
928         struct ipath_qp *qp;
929         unsigned long flags;
930
931         if (dev == NULL)
932                 goto bail;
933
934         spin_lock_irqsave(&dev->pending_lock, flags);
935         while (!list_empty(&dev->piowait)) {
936                 qp = list_entry(dev->piowait.next, struct ipath_qp,
937                                 piowait);
938                 list_del_init(&qp->piowait);
939                 tasklet_hi_schedule(&qp->s_task);
940         }
941         spin_unlock_irqrestore(&dev->pending_lock, flags);
942
943 bail:
944         return 0;
945 }
946
947 static int ipath_query_device(struct ib_device *ibdev,
948                               struct ib_device_attr *props)
949 {
950         struct ipath_ibdev *dev = to_idev(ibdev);
951
952         memset(props, 0, sizeof(*props));
953
954         props->device_cap_flags = IB_DEVICE_BAD_PKEY_CNTR |
955                 IB_DEVICE_BAD_QKEY_CNTR | IB_DEVICE_SHUTDOWN_PORT |
956                 IB_DEVICE_SYS_IMAGE_GUID;
957         props->page_size_cap = PAGE_SIZE;
958         props->vendor_id = dev->dd->ipath_vendorid;
959         props->vendor_part_id = dev->dd->ipath_deviceid;
960         props->hw_ver = dev->dd->ipath_pcirev;
961
962         props->sys_image_guid = dev->sys_image_guid;
963
964         props->max_mr_size = ~0ull;
965         props->max_qp = ib_ipath_max_qps;
966         props->max_qp_wr = ib_ipath_max_qp_wrs;
967         props->max_sge = ib_ipath_max_sges;
968         props->max_cq = ib_ipath_max_cqs;
969         props->max_ah = ib_ipath_max_ahs;
970         props->max_cqe = ib_ipath_max_cqes;
971         props->max_mr = dev->lk_table.max;
972         props->max_pd = ib_ipath_max_pds;
973         props->max_qp_rd_atom = 1;
974         props->max_qp_init_rd_atom = 1;
975         /* props->max_res_rd_atom */
976         props->max_srq = ib_ipath_max_srqs;
977         props->max_srq_wr = ib_ipath_max_srq_wrs;
978         props->max_srq_sge = ib_ipath_max_srq_sges;
979         /* props->local_ca_ack_delay */
980         props->atomic_cap = IB_ATOMIC_HCA;
981         props->max_pkeys = ipath_get_npkeys(dev->dd);
982         props->max_mcast_grp = ib_ipath_max_mcast_grps;
983         props->max_mcast_qp_attach = ib_ipath_max_mcast_qp_attached;
984         props->max_total_mcast_qp_attach = props->max_mcast_qp_attach *
985                 props->max_mcast_grp;
986
987         return 0;
988 }
989
990 const u8 ipath_cvt_physportstate[16] = {
991         [INFINIPATH_IBCS_LT_STATE_DISABLED] = 3,
992         [INFINIPATH_IBCS_LT_STATE_LINKUP] = 5,
993         [INFINIPATH_IBCS_LT_STATE_POLLACTIVE] = 2,
994         [INFINIPATH_IBCS_LT_STATE_POLLQUIET] = 2,
995         [INFINIPATH_IBCS_LT_STATE_SLEEPDELAY] = 1,
996         [INFINIPATH_IBCS_LT_STATE_SLEEPQUIET] = 1,
997         [INFINIPATH_IBCS_LT_STATE_CFGDEBOUNCE] = 4,
998         [INFINIPATH_IBCS_LT_STATE_CFGRCVFCFG] = 4,
999         [INFINIPATH_IBCS_LT_STATE_CFGWAITRMT] = 4,
1000         [INFINIPATH_IBCS_LT_STATE_CFGIDLE] = 4,
1001         [INFINIPATH_IBCS_LT_STATE_RECOVERRETRAIN] = 6,
1002         [INFINIPATH_IBCS_LT_STATE_RECOVERWAITRMT] = 6,
1003         [INFINIPATH_IBCS_LT_STATE_RECOVERIDLE] = 6,
1004 };
1005
1006 u32 ipath_get_cr_errpkey(struct ipath_devdata *dd)
1007 {
1008         return ipath_read_creg32(dd, dd->ipath_cregs->cr_errpkey);
1009 }
1010
1011 static int ipath_query_port(struct ib_device *ibdev,
1012                             u8 port, struct ib_port_attr *props)
1013 {
1014         struct ipath_ibdev *dev = to_idev(ibdev);
1015         enum ib_mtu mtu;
1016         u16 lid = dev->dd->ipath_lid;
1017         u64 ibcstat;
1018
1019         memset(props, 0, sizeof(*props));
1020         props->lid = lid ? lid : __constant_be16_to_cpu(IB_LID_PERMISSIVE);
1021         props->lmc = dev->mkeyprot_resv_lmc & 7;
1022         props->sm_lid = dev->sm_lid;
1023         props->sm_sl = dev->sm_sl;
1024         ibcstat = dev->dd->ipath_lastibcstat;
1025         props->state = ((ibcstat >> 4) & 0x3) + 1;
1026         /* See phys_state_show() */
1027         props->phys_state = ipath_cvt_physportstate[
1028                 dev->dd->ipath_lastibcstat & 0xf];
1029         props->port_cap_flags = dev->port_cap_flags;
1030         props->gid_tbl_len = 1;
1031         props->max_msg_sz = 0x80000000;
1032         props->pkey_tbl_len = ipath_get_npkeys(dev->dd);
1033         props->bad_pkey_cntr = ipath_get_cr_errpkey(dev->dd) -
1034                 dev->z_pkey_violations;
1035         props->qkey_viol_cntr = dev->qkey_violations;
1036         props->active_width = IB_WIDTH_4X;
1037         /* See rate_show() */
1038         props->active_speed = 1;        /* Regular 10Mbs speed. */
1039         props->max_vl_num = 1;          /* VLCap = VL0 */
1040         props->init_type_reply = 0;
1041
1042         props->max_mtu = IB_MTU_4096;
1043         switch (dev->dd->ipath_ibmtu) {
1044         case 4096:
1045                 mtu = IB_MTU_4096;
1046                 break;
1047         case 2048:
1048                 mtu = IB_MTU_2048;
1049                 break;
1050         case 1024:
1051                 mtu = IB_MTU_1024;
1052                 break;
1053         case 512:
1054                 mtu = IB_MTU_512;
1055                 break;
1056         case 256:
1057                 mtu = IB_MTU_256;
1058                 break;
1059         default:
1060                 mtu = IB_MTU_2048;
1061         }
1062         props->active_mtu = mtu;
1063         props->subnet_timeout = dev->subnet_timeout;
1064
1065         return 0;
1066 }
1067
1068 static int ipath_modify_device(struct ib_device *device,
1069                                int device_modify_mask,
1070                                struct ib_device_modify *device_modify)
1071 {
1072         int ret;
1073
1074         if (device_modify_mask & ~(IB_DEVICE_MODIFY_SYS_IMAGE_GUID |
1075                                    IB_DEVICE_MODIFY_NODE_DESC)) {
1076                 ret = -EOPNOTSUPP;
1077                 goto bail;
1078         }
1079
1080         if (device_modify_mask & IB_DEVICE_MODIFY_NODE_DESC)
1081                 memcpy(device->node_desc, device_modify->node_desc, 64);
1082
1083         if (device_modify_mask & IB_DEVICE_MODIFY_SYS_IMAGE_GUID)
1084                 to_idev(device)->sys_image_guid =
1085                         cpu_to_be64(device_modify->sys_image_guid);
1086
1087         ret = 0;
1088
1089 bail:
1090         return ret;
1091 }
1092
1093 static int ipath_modify_port(struct ib_device *ibdev,
1094                              u8 port, int port_modify_mask,
1095                              struct ib_port_modify *props)
1096 {
1097         struct ipath_ibdev *dev = to_idev(ibdev);
1098
1099         dev->port_cap_flags |= props->set_port_cap_mask;
1100         dev->port_cap_flags &= ~props->clr_port_cap_mask;
1101         if (port_modify_mask & IB_PORT_SHUTDOWN)
1102                 ipath_set_linkstate(dev->dd, IPATH_IB_LINKDOWN);
1103         if (port_modify_mask & IB_PORT_RESET_QKEY_CNTR)
1104                 dev->qkey_violations = 0;
1105         return 0;
1106 }
1107
1108 static int ipath_query_gid(struct ib_device *ibdev, u8 port,
1109                            int index, union ib_gid *gid)
1110 {
1111         struct ipath_ibdev *dev = to_idev(ibdev);
1112         int ret;
1113
1114         if (index >= 1) {
1115                 ret = -EINVAL;
1116                 goto bail;
1117         }
1118         gid->global.subnet_prefix = dev->gid_prefix;
1119         gid->global.interface_id = dev->dd->ipath_guid;
1120
1121         ret = 0;
1122
1123 bail:
1124         return ret;
1125 }
1126
1127 static struct ib_pd *ipath_alloc_pd(struct ib_device *ibdev,
1128                                     struct ib_ucontext *context,
1129                                     struct ib_udata *udata)
1130 {
1131         struct ipath_ibdev *dev = to_idev(ibdev);
1132         struct ipath_pd *pd;
1133         struct ib_pd *ret;
1134
1135         /*
1136          * This is actually totally arbitrary.  Some correctness tests
1137          * assume there's a maximum number of PDs that can be allocated.
1138          * We don't actually have this limit, but we fail the test if
1139          * we allow allocations of more than we report for this value.
1140          */
1141
1142         pd = kmalloc(sizeof *pd, GFP_KERNEL);
1143         if (!pd) {
1144                 ret = ERR_PTR(-ENOMEM);
1145                 goto bail;
1146         }
1147
1148         spin_lock(&dev->n_pds_lock);
1149         if (dev->n_pds_allocated == ib_ipath_max_pds) {
1150                 spin_unlock(&dev->n_pds_lock);
1151                 kfree(pd);
1152                 ret = ERR_PTR(-ENOMEM);
1153                 goto bail;
1154         }
1155
1156         dev->n_pds_allocated++;
1157         spin_unlock(&dev->n_pds_lock);
1158
1159         /* ib_alloc_pd() will initialize pd->ibpd. */
1160         pd->user = udata != NULL;
1161
1162         ret = &pd->ibpd;
1163
1164 bail:
1165         return ret;
1166 }
1167
1168 static int ipath_dealloc_pd(struct ib_pd *ibpd)
1169 {
1170         struct ipath_pd *pd = to_ipd(ibpd);
1171         struct ipath_ibdev *dev = to_idev(ibpd->device);
1172
1173         spin_lock(&dev->n_pds_lock);
1174         dev->n_pds_allocated--;
1175         spin_unlock(&dev->n_pds_lock);
1176
1177         kfree(pd);
1178
1179         return 0;
1180 }
1181
1182 /**
1183  * ipath_create_ah - create an address handle
1184  * @pd: the protection domain
1185  * @ah_attr: the attributes of the AH
1186  *
1187  * This may be called from interrupt context.
1188  */
1189 static struct ib_ah *ipath_create_ah(struct ib_pd *pd,
1190                                      struct ib_ah_attr *ah_attr)
1191 {
1192         struct ipath_ah *ah;
1193         struct ib_ah *ret;
1194         struct ipath_ibdev *dev = to_idev(pd->device);
1195
1196         /* A multicast address requires a GRH (see ch. 8.4.1). */
1197         if (ah_attr->dlid >= IPATH_MULTICAST_LID_BASE &&
1198             ah_attr->dlid != IPATH_PERMISSIVE_LID &&
1199             !(ah_attr->ah_flags & IB_AH_GRH)) {
1200                 ret = ERR_PTR(-EINVAL);
1201                 goto bail;
1202         }
1203
1204         if (ah_attr->dlid == 0) {
1205                 ret = ERR_PTR(-EINVAL);
1206                 goto bail;
1207         }
1208
1209         if (ah_attr->port_num < 1 ||
1210             ah_attr->port_num > pd->device->phys_port_cnt) {
1211                 ret = ERR_PTR(-EINVAL);
1212                 goto bail;
1213         }
1214
1215         ah = kmalloc(sizeof *ah, GFP_ATOMIC);
1216         if (!ah) {
1217                 ret = ERR_PTR(-ENOMEM);
1218                 goto bail;
1219         }
1220
1221         spin_lock(&dev->n_ahs_lock);
1222         if (dev->n_ahs_allocated == ib_ipath_max_ahs) {
1223                 spin_unlock(&dev->n_ahs_lock);
1224                 kfree(ah);
1225                 ret = ERR_PTR(-ENOMEM);
1226                 goto bail;
1227         }
1228
1229         dev->n_ahs_allocated++;
1230         spin_unlock(&dev->n_ahs_lock);
1231
1232         /* ib_create_ah() will initialize ah->ibah. */
1233         ah->attr = *ah_attr;
1234
1235         ret = &ah->ibah;
1236
1237 bail:
1238         return ret;
1239 }
1240
1241 /**
1242  * ipath_destroy_ah - destroy an address handle
1243  * @ibah: the AH to destroy
1244  *
1245  * This may be called from interrupt context.
1246  */
1247 static int ipath_destroy_ah(struct ib_ah *ibah)
1248 {
1249         struct ipath_ibdev *dev = to_idev(ibah->device);
1250         struct ipath_ah *ah = to_iah(ibah);
1251
1252         spin_lock(&dev->n_ahs_lock);
1253         dev->n_ahs_allocated--;
1254         spin_unlock(&dev->n_ahs_lock);
1255
1256         kfree(ah);
1257
1258         return 0;
1259 }
1260
1261 static int ipath_query_ah(struct ib_ah *ibah, struct ib_ah_attr *ah_attr)
1262 {
1263         struct ipath_ah *ah = to_iah(ibah);
1264
1265         *ah_attr = ah->attr;
1266
1267         return 0;
1268 }
1269
1270 /**
1271  * ipath_get_npkeys - return the size of the PKEY table for port 0
1272  * @dd: the infinipath device
1273  */
1274 unsigned ipath_get_npkeys(struct ipath_devdata *dd)
1275 {
1276         return ARRAY_SIZE(dd->ipath_pd[0]->port_pkeys);
1277 }
1278
1279 /**
1280  * ipath_get_pkey - return the indexed PKEY from the port 0 PKEY table
1281  * @dd: the infinipath device
1282  * @index: the PKEY index
1283  */
1284 unsigned ipath_get_pkey(struct ipath_devdata *dd, unsigned index)
1285 {
1286         unsigned ret;
1287
1288         if (index >= ARRAY_SIZE(dd->ipath_pd[0]->port_pkeys))
1289                 ret = 0;
1290         else
1291                 ret = dd->ipath_pd[0]->port_pkeys[index];
1292
1293         return ret;
1294 }
1295
1296 static int ipath_query_pkey(struct ib_device *ibdev, u8 port, u16 index,
1297                             u16 *pkey)
1298 {
1299         struct ipath_ibdev *dev = to_idev(ibdev);
1300         int ret;
1301
1302         if (index >= ipath_get_npkeys(dev->dd)) {
1303                 ret = -EINVAL;
1304                 goto bail;
1305         }
1306
1307         *pkey = ipath_get_pkey(dev->dd, index);
1308         ret = 0;
1309
1310 bail:
1311         return ret;
1312 }
1313
1314 /**
1315  * ipath_alloc_ucontext - allocate a ucontest
1316  * @ibdev: the infiniband device
1317  * @udata: not used by the InfiniPath driver
1318  */
1319
1320 static struct ib_ucontext *ipath_alloc_ucontext(struct ib_device *ibdev,
1321                                                 struct ib_udata *udata)
1322 {
1323         struct ipath_ucontext *context;
1324         struct ib_ucontext *ret;
1325
1326         context = kmalloc(sizeof *context, GFP_KERNEL);
1327         if (!context) {
1328                 ret = ERR_PTR(-ENOMEM);
1329                 goto bail;
1330         }
1331
1332         ret = &context->ibucontext;
1333
1334 bail:
1335         return ret;
1336 }
1337
1338 static int ipath_dealloc_ucontext(struct ib_ucontext *context)
1339 {
1340         kfree(to_iucontext(context));
1341         return 0;
1342 }
1343
1344 static int ipath_verbs_register_sysfs(struct ib_device *dev);
1345
1346 static void __verbs_timer(unsigned long arg)
1347 {
1348         struct ipath_devdata *dd = (struct ipath_devdata *) arg;
1349
1350         /*
1351          * If port 0 receive packet interrupts are not available, or
1352          * can be missed, poll the receive queue
1353          */
1354         if (dd->ipath_flags & IPATH_POLL_RX_INTR)
1355                 ipath_kreceive(dd);
1356
1357         /* Handle verbs layer timeouts. */
1358         ipath_ib_timer(dd->verbs_dev);
1359
1360         mod_timer(&dd->verbs_timer, jiffies + 1);
1361 }
1362
1363 static int enable_timer(struct ipath_devdata *dd)
1364 {
1365         /*
1366          * Early chips had a design flaw where the chip and kernel idea
1367          * of the tail register don't always agree, and therefore we won't
1368          * get an interrupt on the next packet received.
1369          * If the board supports per packet receive interrupts, use it.
1370          * Otherwise, the timer function periodically checks for packets
1371          * to cover this case.
1372          * Either way, the timer is needed for verbs layer related
1373          * processing.
1374          */
1375         if (dd->ipath_flags & IPATH_GPIO_INTR) {
1376                 ipath_write_kreg(dd, dd->ipath_kregs->kr_debugportselect,
1377                                  0x2074076542310ULL);
1378                 /* Enable GPIO bit 2 interrupt */
1379                 ipath_write_kreg(dd, dd->ipath_kregs->kr_gpio_mask,
1380                                  (u64) (1 << 2));
1381         }
1382
1383         init_timer(&dd->verbs_timer);
1384         dd->verbs_timer.function = __verbs_timer;
1385         dd->verbs_timer.data = (unsigned long)dd;
1386         dd->verbs_timer.expires = jiffies + 1;
1387         add_timer(&dd->verbs_timer);
1388
1389         return 0;
1390 }
1391
1392 static int disable_timer(struct ipath_devdata *dd)
1393 {
1394         /* Disable GPIO bit 2 interrupt */
1395         if (dd->ipath_flags & IPATH_GPIO_INTR)
1396                 ipath_write_kreg(dd, dd->ipath_kregs->kr_gpio_mask, 0);
1397
1398         del_timer_sync(&dd->verbs_timer);
1399
1400         return 0;
1401 }
1402
1403 /**
1404  * ipath_register_ib_device - register our device with the infiniband core
1405  * @dd: the device data structure
1406  * Return the allocated ipath_ibdev pointer or NULL on error.
1407  */
1408 int ipath_register_ib_device(struct ipath_devdata *dd)
1409 {
1410         struct ipath_verbs_counters cntrs;
1411         struct ipath_ibdev *idev;
1412         struct ib_device *dev;
1413         int ret;
1414
1415         idev = (struct ipath_ibdev *)ib_alloc_device(sizeof *idev);
1416         if (idev == NULL) {
1417                 ret = -ENOMEM;
1418                 goto bail;
1419         }
1420
1421         dev = &idev->ibdev;
1422
1423         /* Only need to initialize non-zero fields. */
1424         spin_lock_init(&idev->n_pds_lock);
1425         spin_lock_init(&idev->n_ahs_lock);
1426         spin_lock_init(&idev->n_cqs_lock);
1427         spin_lock_init(&idev->n_qps_lock);
1428         spin_lock_init(&idev->n_srqs_lock);
1429         spin_lock_init(&idev->n_mcast_grps_lock);
1430
1431         spin_lock_init(&idev->qp_table.lock);
1432         spin_lock_init(&idev->lk_table.lock);
1433         idev->sm_lid = __constant_be16_to_cpu(IB_LID_PERMISSIVE);
1434         /* Set the prefix to the default value (see ch. 4.1.1) */
1435         idev->gid_prefix = __constant_cpu_to_be64(0xfe80000000000000ULL);
1436
1437         ret = ipath_init_qp_table(idev, ib_ipath_qp_table_size);
1438         if (ret)
1439                 goto err_qp;
1440
1441         /*
1442          * The top ib_ipath_lkey_table_size bits are used to index the
1443          * table.  The lower 8 bits can be owned by the user (copied from
1444          * the LKEY).  The remaining bits act as a generation number or tag.
1445          */
1446         idev->lk_table.max = 1 << ib_ipath_lkey_table_size;
1447         idev->lk_table.table = kzalloc(idev->lk_table.max *
1448                                        sizeof(*idev->lk_table.table),
1449                                        GFP_KERNEL);
1450         if (idev->lk_table.table == NULL) {
1451                 ret = -ENOMEM;
1452                 goto err_lk;
1453         }
1454         spin_lock_init(&idev->pending_lock);
1455         INIT_LIST_HEAD(&idev->pending[0]);
1456         INIT_LIST_HEAD(&idev->pending[1]);
1457         INIT_LIST_HEAD(&idev->pending[2]);
1458         INIT_LIST_HEAD(&idev->piowait);
1459         INIT_LIST_HEAD(&idev->rnrwait);
1460         idev->pending_index = 0;
1461         idev->port_cap_flags =
1462                 IB_PORT_SYS_IMAGE_GUID_SUP | IB_PORT_CLIENT_REG_SUP;
1463         idev->pma_counter_select[0] = IB_PMA_PORT_XMIT_DATA;
1464         idev->pma_counter_select[1] = IB_PMA_PORT_RCV_DATA;
1465         idev->pma_counter_select[2] = IB_PMA_PORT_XMIT_PKTS;
1466         idev->pma_counter_select[3] = IB_PMA_PORT_RCV_PKTS;
1467         idev->pma_counter_select[5] = IB_PMA_PORT_XMIT_WAIT;
1468         idev->link_width_enabled = 3;   /* 1x or 4x */
1469
1470         /* Snapshot current HW counters to "clear" them. */
1471         ipath_get_counters(dd, &cntrs);
1472         idev->z_symbol_error_counter = cntrs.symbol_error_counter;
1473         idev->z_link_error_recovery_counter =
1474                 cntrs.link_error_recovery_counter;
1475         idev->z_link_downed_counter = cntrs.link_downed_counter;
1476         idev->z_port_rcv_errors = cntrs.port_rcv_errors;
1477         idev->z_port_rcv_remphys_errors =
1478                 cntrs.port_rcv_remphys_errors;
1479         idev->z_port_xmit_discards = cntrs.port_xmit_discards;
1480         idev->z_port_xmit_data = cntrs.port_xmit_data;
1481         idev->z_port_rcv_data = cntrs.port_rcv_data;
1482         idev->z_port_xmit_packets = cntrs.port_xmit_packets;
1483         idev->z_port_rcv_packets = cntrs.port_rcv_packets;
1484         idev->z_local_link_integrity_errors =
1485                 cntrs.local_link_integrity_errors;
1486         idev->z_excessive_buffer_overrun_errors =
1487                 cntrs.excessive_buffer_overrun_errors;
1488
1489         /*
1490          * The system image GUID is supposed to be the same for all
1491          * IB HCAs in a single system but since there can be other
1492          * device types in the system, we can't be sure this is unique.
1493          */
1494         if (!sys_image_guid)
1495                 sys_image_guid = dd->ipath_guid;
1496         idev->sys_image_guid = sys_image_guid;
1497         idev->ib_unit = dd->ipath_unit;
1498         idev->dd = dd;
1499
1500         strlcpy(dev->name, "ipath%d", IB_DEVICE_NAME_MAX);
1501         dev->owner = THIS_MODULE;
1502         dev->node_guid = dd->ipath_guid;
1503         dev->uverbs_abi_ver = IPATH_UVERBS_ABI_VERSION;
1504         dev->uverbs_cmd_mask =
1505                 (1ull << IB_USER_VERBS_CMD_GET_CONTEXT)         |
1506                 (1ull << IB_USER_VERBS_CMD_QUERY_DEVICE)        |
1507                 (1ull << IB_USER_VERBS_CMD_QUERY_PORT)          |
1508                 (1ull << IB_USER_VERBS_CMD_ALLOC_PD)            |
1509                 (1ull << IB_USER_VERBS_CMD_DEALLOC_PD)          |
1510                 (1ull << IB_USER_VERBS_CMD_CREATE_AH)           |
1511                 (1ull << IB_USER_VERBS_CMD_DESTROY_AH)          |
1512                 (1ull << IB_USER_VERBS_CMD_QUERY_AH)            |
1513                 (1ull << IB_USER_VERBS_CMD_REG_MR)              |
1514                 (1ull << IB_USER_VERBS_CMD_DEREG_MR)            |
1515                 (1ull << IB_USER_VERBS_CMD_CREATE_COMP_CHANNEL) |
1516                 (1ull << IB_USER_VERBS_CMD_CREATE_CQ)           |
1517                 (1ull << IB_USER_VERBS_CMD_RESIZE_CQ)           |
1518                 (1ull << IB_USER_VERBS_CMD_DESTROY_CQ)          |
1519                 (1ull << IB_USER_VERBS_CMD_POLL_CQ)             |
1520                 (1ull << IB_USER_VERBS_CMD_REQ_NOTIFY_CQ)       |
1521                 (1ull << IB_USER_VERBS_CMD_CREATE_QP)           |
1522                 (1ull << IB_USER_VERBS_CMD_QUERY_QP)            |
1523                 (1ull << IB_USER_VERBS_CMD_MODIFY_QP)           |
1524                 (1ull << IB_USER_VERBS_CMD_DESTROY_QP)          |
1525                 (1ull << IB_USER_VERBS_CMD_POST_SEND)           |
1526                 (1ull << IB_USER_VERBS_CMD_POST_RECV)           |
1527                 (1ull << IB_USER_VERBS_CMD_ATTACH_MCAST)        |
1528                 (1ull << IB_USER_VERBS_CMD_DETACH_MCAST)        |
1529                 (1ull << IB_USER_VERBS_CMD_CREATE_SRQ)          |
1530                 (1ull << IB_USER_VERBS_CMD_MODIFY_SRQ)          |
1531                 (1ull << IB_USER_VERBS_CMD_QUERY_SRQ)           |
1532                 (1ull << IB_USER_VERBS_CMD_DESTROY_SRQ)         |
1533                 (1ull << IB_USER_VERBS_CMD_POST_SRQ_RECV);
1534         dev->node_type = IB_NODE_CA;
1535         dev->phys_port_cnt = 1;
1536         dev->dma_device = &dd->pcidev->dev;
1537         dev->class_dev.dev = dev->dma_device;
1538         dev->query_device = ipath_query_device;
1539         dev->modify_device = ipath_modify_device;
1540         dev->query_port = ipath_query_port;
1541         dev->modify_port = ipath_modify_port;
1542         dev->query_pkey = ipath_query_pkey;
1543         dev->query_gid = ipath_query_gid;
1544         dev->alloc_ucontext = ipath_alloc_ucontext;
1545         dev->dealloc_ucontext = ipath_dealloc_ucontext;
1546         dev->alloc_pd = ipath_alloc_pd;
1547         dev->dealloc_pd = ipath_dealloc_pd;
1548         dev->create_ah = ipath_create_ah;
1549         dev->destroy_ah = ipath_destroy_ah;
1550         dev->query_ah = ipath_query_ah;
1551         dev->create_srq = ipath_create_srq;
1552         dev->modify_srq = ipath_modify_srq;
1553         dev->query_srq = ipath_query_srq;
1554         dev->destroy_srq = ipath_destroy_srq;
1555         dev->create_qp = ipath_create_qp;
1556         dev->modify_qp = ipath_modify_qp;
1557         dev->query_qp = ipath_query_qp;
1558         dev->destroy_qp = ipath_destroy_qp;
1559         dev->post_send = ipath_post_send;
1560         dev->post_recv = ipath_post_receive;
1561         dev->post_srq_recv = ipath_post_srq_receive;
1562         dev->create_cq = ipath_create_cq;
1563         dev->destroy_cq = ipath_destroy_cq;
1564         dev->resize_cq = ipath_resize_cq;
1565         dev->poll_cq = ipath_poll_cq;
1566         dev->req_notify_cq = ipath_req_notify_cq;
1567         dev->get_dma_mr = ipath_get_dma_mr;
1568         dev->reg_phys_mr = ipath_reg_phys_mr;
1569         dev->reg_user_mr = ipath_reg_user_mr;
1570         dev->dereg_mr = ipath_dereg_mr;
1571         dev->alloc_fmr = ipath_alloc_fmr;
1572         dev->map_phys_fmr = ipath_map_phys_fmr;
1573         dev->unmap_fmr = ipath_unmap_fmr;
1574         dev->dealloc_fmr = ipath_dealloc_fmr;
1575         dev->attach_mcast = ipath_multicast_attach;
1576         dev->detach_mcast = ipath_multicast_detach;
1577         dev->process_mad = ipath_process_mad;
1578         dev->mmap = ipath_mmap;
1579
1580         snprintf(dev->node_desc, sizeof(dev->node_desc),
1581                  IPATH_IDSTR " %s", system_utsname.nodename);
1582
1583         ret = ib_register_device(dev);
1584         if (ret)
1585                 goto err_reg;
1586
1587         if (ipath_verbs_register_sysfs(dev))
1588                 goto err_class;
1589
1590         enable_timer(dd);
1591
1592         goto bail;
1593
1594 err_class:
1595         ib_unregister_device(dev);
1596 err_reg:
1597         kfree(idev->lk_table.table);
1598 err_lk:
1599         kfree(idev->qp_table.table);
1600 err_qp:
1601         ib_dealloc_device(dev);
1602         ipath_dev_err(dd, "cannot register verbs: %d!\n", -ret);
1603         idev = NULL;
1604
1605 bail:
1606         dd->verbs_dev = idev;
1607         return ret;
1608 }
1609
1610 void ipath_unregister_ib_device(struct ipath_ibdev *dev)
1611 {
1612         struct ib_device *ibdev = &dev->ibdev;
1613
1614         disable_timer(dev->dd);
1615
1616         ib_unregister_device(ibdev);
1617
1618         if (!list_empty(&dev->pending[0]) ||
1619             !list_empty(&dev->pending[1]) ||
1620             !list_empty(&dev->pending[2]))
1621                 ipath_dev_err(dev->dd, "pending list not empty!\n");
1622         if (!list_empty(&dev->piowait))
1623                 ipath_dev_err(dev->dd, "piowait list not empty!\n");
1624         if (!list_empty(&dev->rnrwait))
1625                 ipath_dev_err(dev->dd, "rnrwait list not empty!\n");
1626         if (!ipath_mcast_tree_empty())
1627                 ipath_dev_err(dev->dd, "multicast table memory leak!\n");
1628         /*
1629          * Note that ipath_unregister_ib_device() can be called before all
1630          * the QPs are destroyed!
1631          */
1632         ipath_free_all_qps(&dev->qp_table);
1633         kfree(dev->qp_table.table);
1634         kfree(dev->lk_table.table);
1635         ib_dealloc_device(ibdev);
1636 }
1637
1638 static ssize_t show_rev(struct class_device *cdev, char *buf)
1639 {
1640         struct ipath_ibdev *dev =
1641                 container_of(cdev, struct ipath_ibdev, ibdev.class_dev);
1642
1643         return sprintf(buf, "%x\n", dev->dd->ipath_pcirev);
1644 }
1645
1646 static ssize_t show_hca(struct class_device *cdev, char *buf)
1647 {
1648         struct ipath_ibdev *dev =
1649                 container_of(cdev, struct ipath_ibdev, ibdev.class_dev);
1650         int ret;
1651
1652         ret = dev->dd->ipath_f_get_boardname(dev->dd, buf, 128);
1653         if (ret < 0)
1654                 goto bail;
1655         strcat(buf, "\n");
1656         ret = strlen(buf);
1657
1658 bail:
1659         return ret;
1660 }
1661
1662 static ssize_t show_stats(struct class_device *cdev, char *buf)
1663 {
1664         struct ipath_ibdev *dev =
1665                 container_of(cdev, struct ipath_ibdev, ibdev.class_dev);
1666         int i;
1667         int len;
1668
1669         len = sprintf(buf,
1670                       "RC resends  %d\n"
1671                       "RC no QACK  %d\n"
1672                       "RC ACKs     %d\n"
1673                       "RC SEQ NAKs %d\n"
1674                       "RC RDMA seq %d\n"
1675                       "RC RNR NAKs %d\n"
1676                       "RC OTH NAKs %d\n"
1677                       "RC timeouts %d\n"
1678                       "RC RDMA dup %d\n"
1679                       "piobuf wait %d\n"
1680                       "no piobuf   %d\n"
1681                       "PKT drops   %d\n"
1682                       "WQE errs    %d\n",
1683                       dev->n_rc_resends, dev->n_rc_qacks, dev->n_rc_acks,
1684                       dev->n_seq_naks, dev->n_rdma_seq, dev->n_rnr_naks,
1685                       dev->n_other_naks, dev->n_timeouts,
1686                       dev->n_rdma_dup_busy, dev->n_piowait,
1687                       dev->n_no_piobuf, dev->n_pkt_drops, dev->n_wqe_errs);
1688         for (i = 0; i < ARRAY_SIZE(dev->opstats); i++) {
1689                 const struct ipath_opcode_stats *si = &dev->opstats[i];
1690
1691                 if (!si->n_packets && !si->n_bytes)
1692                         continue;
1693                 len += sprintf(buf + len, "%02x %llu/%llu\n", i,
1694                                (unsigned long long) si->n_packets,
1695                                (unsigned long long) si->n_bytes);
1696         }
1697         return len;
1698 }
1699
1700 static CLASS_DEVICE_ATTR(hw_rev, S_IRUGO, show_rev, NULL);
1701 static CLASS_DEVICE_ATTR(hca_type, S_IRUGO, show_hca, NULL);
1702 static CLASS_DEVICE_ATTR(board_id, S_IRUGO, show_hca, NULL);
1703 static CLASS_DEVICE_ATTR(stats, S_IRUGO, show_stats, NULL);
1704
1705 static struct class_device_attribute *ipath_class_attributes[] = {
1706         &class_device_attr_hw_rev,
1707         &class_device_attr_hca_type,
1708         &class_device_attr_board_id,
1709         &class_device_attr_stats
1710 };
1711
1712 static int ipath_verbs_register_sysfs(struct ib_device *dev)
1713 {
1714         int i;
1715         int ret;
1716
1717         for (i = 0; i < ARRAY_SIZE(ipath_class_attributes); ++i)
1718                 if (class_device_create_file(&dev->class_dev,
1719                                              ipath_class_attributes[i])) {
1720                         ret = 1;
1721                         goto bail;
1722                 }
1723
1724         ret = 0;
1725
1726 bail:
1727         return ret;
1728 }