[PATCH] IB/ipath: share more common code between RC and UC protocols
[cascardo/linux.git] / 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/utsname.h>
37
38 #include "ipath_kernel.h"
39 #include "ipath_verbs.h"
40 #include "ips_common.h"
41
42 /* Not static, because we don't want the compiler removing it */
43 const char ipath_verbs_version[] = "ipath_verbs " IPATH_IDSTR;
44
45 static unsigned int ib_ipath_qp_table_size = 251;
46 module_param_named(qp_table_size, ib_ipath_qp_table_size, uint, S_IRUGO);
47 MODULE_PARM_DESC(qp_table_size, "QP table size");
48
49 unsigned int ib_ipath_lkey_table_size = 12;
50 module_param_named(lkey_table_size, ib_ipath_lkey_table_size, uint,
51                    S_IRUGO);
52 MODULE_PARM_DESC(lkey_table_size,
53                  "LKEY table size in bits (2^n, 1 <= n <= 23)");
54
55 unsigned int ib_ipath_debug;    /* debug mask */
56 module_param_named(debug, ib_ipath_debug, uint, S_IWUSR | S_IRUGO);
57 MODULE_PARM_DESC(debug, "Verbs debug mask");
58
59 MODULE_LICENSE("GPL");
60 MODULE_AUTHOR("QLogic <support@pathscale.com>");
61 MODULE_DESCRIPTION("QLogic InfiniPath driver");
62
63 const int ib_ipath_state_ops[IB_QPS_ERR + 1] = {
64         [IB_QPS_RESET] = 0,
65         [IB_QPS_INIT] = IPATH_POST_RECV_OK,
66         [IB_QPS_RTR] = IPATH_POST_RECV_OK | IPATH_PROCESS_RECV_OK,
67         [IB_QPS_RTS] = IPATH_POST_RECV_OK | IPATH_PROCESS_RECV_OK |
68             IPATH_POST_SEND_OK | IPATH_PROCESS_SEND_OK,
69         [IB_QPS_SQD] = IPATH_POST_RECV_OK | IPATH_PROCESS_RECV_OK |
70             IPATH_POST_SEND_OK,
71         [IB_QPS_SQE] = IPATH_POST_RECV_OK | IPATH_PROCESS_RECV_OK,
72         [IB_QPS_ERR] = 0,
73 };
74
75 /*
76  * Translate ib_wr_opcode into ib_wc_opcode.
77  */
78 const enum ib_wc_opcode ib_ipath_wc_opcode[] = {
79         [IB_WR_RDMA_WRITE] = IB_WC_RDMA_WRITE,
80         [IB_WR_RDMA_WRITE_WITH_IMM] = IB_WC_RDMA_WRITE,
81         [IB_WR_SEND] = IB_WC_SEND,
82         [IB_WR_SEND_WITH_IMM] = IB_WC_SEND,
83         [IB_WR_RDMA_READ] = IB_WC_RDMA_READ,
84         [IB_WR_ATOMIC_CMP_AND_SWP] = IB_WC_COMP_SWAP,
85         [IB_WR_ATOMIC_FETCH_AND_ADD] = IB_WC_FETCH_ADD
86 };
87
88 /*
89  * System image GUID.
90  */
91 static __be64 sys_image_guid;
92
93 /**
94  * ipath_copy_sge - copy data to SGE memory
95  * @ss: the SGE state
96  * @data: the data to copy
97  * @length: the length of the data
98  */
99 void ipath_copy_sge(struct ipath_sge_state *ss, void *data, u32 length)
100 {
101         struct ipath_sge *sge = &ss->sge;
102
103         while (length) {
104                 u32 len = sge->length;
105
106                 BUG_ON(len == 0);
107                 if (len > length)
108                         len = length;
109                 memcpy(sge->vaddr, data, len);
110                 sge->vaddr += len;
111                 sge->length -= len;
112                 sge->sge_length -= len;
113                 if (sge->sge_length == 0) {
114                         if (--ss->num_sge)
115                                 *sge = *ss->sg_list++;
116                 } else if (sge->length == 0 && sge->mr != NULL) {
117                         if (++sge->n >= IPATH_SEGSZ) {
118                                 if (++sge->m >= sge->mr->mapsz)
119                                         break;
120                                 sge->n = 0;
121                         }
122                         sge->vaddr =
123                                 sge->mr->map[sge->m]->segs[sge->n].vaddr;
124                         sge->length =
125                                 sge->mr->map[sge->m]->segs[sge->n].length;
126                 }
127                 data += len;
128                 length -= len;
129         }
130 }
131
132 /**
133  * ipath_skip_sge - skip over SGE memory - XXX almost dup of prev func
134  * @ss: the SGE state
135  * @length: the number of bytes to skip
136  */
137 void ipath_skip_sge(struct ipath_sge_state *ss, u32 length)
138 {
139         struct ipath_sge *sge = &ss->sge;
140
141         while (length > sge->sge_length) {
142                 length -= sge->sge_length;
143                 ss->sge = *ss->sg_list++;
144         }
145         while (length) {
146                 u32 len = sge->length;
147
148                 BUG_ON(len == 0);
149                 if (len > length)
150                         len = length;
151                 sge->vaddr += len;
152                 sge->length -= len;
153                 sge->sge_length -= len;
154                 if (sge->sge_length == 0) {
155                         if (--ss->num_sge)
156                                 *sge = *ss->sg_list++;
157                 } else if (sge->length == 0 && sge->mr != NULL) {
158                         if (++sge->n >= IPATH_SEGSZ) {
159                                 if (++sge->m >= sge->mr->mapsz)
160                                         break;
161                                 sge->n = 0;
162                         }
163                         sge->vaddr =
164                                 sge->mr->map[sge->m]->segs[sge->n].vaddr;
165                         sge->length =
166                                 sge->mr->map[sge->m]->segs[sge->n].length;
167                 }
168                 length -= len;
169         }
170 }
171
172 /**
173  * ipath_post_send - post a send on a QP
174  * @ibqp: the QP to post the send on
175  * @wr: the list of work requests to post
176  * @bad_wr: the first bad WR is put here
177  *
178  * This may be called from interrupt context.
179  */
180 static int ipath_post_send(struct ib_qp *ibqp, struct ib_send_wr *wr,
181                            struct ib_send_wr **bad_wr)
182 {
183         struct ipath_qp *qp = to_iqp(ibqp);
184         int err = 0;
185
186         /* Check that state is OK to post send. */
187         if (!(ib_ipath_state_ops[qp->state] & IPATH_POST_SEND_OK)) {
188                 *bad_wr = wr;
189                 err = -EINVAL;
190                 goto bail;
191         }
192
193         for (; wr; wr = wr->next) {
194                 switch (qp->ibqp.qp_type) {
195                 case IB_QPT_UC:
196                 case IB_QPT_RC:
197                         err = ipath_post_ruc_send(qp, wr);
198                         break;
199
200                 case IB_QPT_SMI:
201                 case IB_QPT_GSI:
202                 case IB_QPT_UD:
203                         err = ipath_post_ud_send(qp, wr);
204                         break;
205
206                 default:
207                         err = -EINVAL;
208                 }
209                 if (err) {
210                         *bad_wr = wr;
211                         break;
212                 }
213         }
214
215 bail:
216         return err;
217 }
218
219 /**
220  * ipath_post_receive - post a receive on a QP
221  * @ibqp: the QP to post the receive on
222  * @wr: the WR to post
223  * @bad_wr: the first bad WR is put here
224  *
225  * This may be called from interrupt context.
226  */
227 static int ipath_post_receive(struct ib_qp *ibqp, struct ib_recv_wr *wr,
228                               struct ib_recv_wr **bad_wr)
229 {
230         struct ipath_qp *qp = to_iqp(ibqp);
231         unsigned long flags;
232         int ret;
233
234         /* Check that state is OK to post receive. */
235         if (!(ib_ipath_state_ops[qp->state] & IPATH_POST_RECV_OK)) {
236                 *bad_wr = wr;
237                 ret = -EINVAL;
238                 goto bail;
239         }
240
241         for (; wr; wr = wr->next) {
242                 struct ipath_rwqe *wqe;
243                 u32 next;
244                 int i, j;
245
246                 if (wr->num_sge > qp->r_rq.max_sge) {
247                         *bad_wr = wr;
248                         ret = -ENOMEM;
249                         goto bail;
250                 }
251
252                 spin_lock_irqsave(&qp->r_rq.lock, flags);
253                 next = qp->r_rq.head + 1;
254                 if (next >= qp->r_rq.size)
255                         next = 0;
256                 if (next == qp->r_rq.tail) {
257                         spin_unlock_irqrestore(&qp->r_rq.lock, flags);
258                         *bad_wr = wr;
259                         ret = -ENOMEM;
260                         goto bail;
261                 }
262
263                 wqe = get_rwqe_ptr(&qp->r_rq, qp->r_rq.head);
264                 wqe->wr_id = wr->wr_id;
265                 wqe->sg_list[0].mr = NULL;
266                 wqe->sg_list[0].vaddr = NULL;
267                 wqe->sg_list[0].length = 0;
268                 wqe->sg_list[0].sge_length = 0;
269                 wqe->length = 0;
270                 for (i = 0, j = 0; i < wr->num_sge; i++) {
271                         /* Check LKEY */
272                         if (to_ipd(qp->ibqp.pd)->user &&
273                             wr->sg_list[i].lkey == 0) {
274                                 spin_unlock_irqrestore(&qp->r_rq.lock,
275                                                        flags);
276                                 *bad_wr = wr;
277                                 ret = -EINVAL;
278                                 goto bail;
279                         }
280                         if (wr->sg_list[i].length == 0)
281                                 continue;
282                         if (!ipath_lkey_ok(
283                                     &to_idev(qp->ibqp.device)->lk_table,
284                                     &wqe->sg_list[j], &wr->sg_list[i],
285                                     IB_ACCESS_LOCAL_WRITE)) {
286                                 spin_unlock_irqrestore(&qp->r_rq.lock,
287                                                        flags);
288                                 *bad_wr = wr;
289                                 ret = -EINVAL;
290                                 goto bail;
291                         }
292                         wqe->length += wr->sg_list[i].length;
293                         j++;
294                 }
295                 wqe->num_sge = j;
296                 qp->r_rq.head = next;
297                 spin_unlock_irqrestore(&qp->r_rq.lock, flags);
298         }
299         ret = 0;
300
301 bail:
302         return ret;
303 }
304
305 /**
306  * ipath_qp_rcv - processing an incoming packet on a QP
307  * @dev: the device the packet came on
308  * @hdr: the packet header
309  * @has_grh: true if the packet has a GRH
310  * @data: the packet data
311  * @tlen: the packet length
312  * @qp: the QP the packet came on
313  *
314  * This is called from ipath_ib_rcv() to process an incoming packet
315  * for the given QP.
316  * Called at interrupt level.
317  */
318 static void ipath_qp_rcv(struct ipath_ibdev *dev,
319                          struct ipath_ib_header *hdr, int has_grh,
320                          void *data, u32 tlen, struct ipath_qp *qp)
321 {
322         /* Check for valid receive state. */
323         if (!(ib_ipath_state_ops[qp->state] & IPATH_PROCESS_RECV_OK)) {
324                 dev->n_pkt_drops++;
325                 return;
326         }
327
328         switch (qp->ibqp.qp_type) {
329         case IB_QPT_SMI:
330         case IB_QPT_GSI:
331         case IB_QPT_UD:
332                 ipath_ud_rcv(dev, hdr, has_grh, data, tlen, qp);
333                 break;
334
335         case IB_QPT_RC:
336                 ipath_rc_rcv(dev, hdr, has_grh, data, tlen, qp);
337                 break;
338
339         case IB_QPT_UC:
340                 ipath_uc_rcv(dev, hdr, has_grh, data, tlen, qp);
341                 break;
342
343         default:
344                 break;
345         }
346 }
347
348 /**
349  * ipath_ib_rcv - process and incoming packet
350  * @arg: the device pointer
351  * @rhdr: the header of the packet
352  * @data: the packet data
353  * @tlen: the packet length
354  *
355  * This is called from ipath_kreceive() to process an incoming packet at
356  * interrupt level. Tlen is the length of the header + data + CRC in bytes.
357  */
358 static void ipath_ib_rcv(void *arg, void *rhdr, void *data, u32 tlen)
359 {
360         struct ipath_ibdev *dev = (struct ipath_ibdev *) arg;
361         struct ipath_ib_header *hdr = rhdr;
362         struct ipath_other_headers *ohdr;
363         struct ipath_qp *qp;
364         u32 qp_num;
365         int lnh;
366         u8 opcode;
367         u16 lid;
368
369         if (unlikely(dev == NULL))
370                 goto bail;
371
372         if (unlikely(tlen < 24)) {      /* LRH+BTH+CRC */
373                 dev->rcv_errors++;
374                 goto bail;
375         }
376
377         /* Check for a valid destination LID (see ch. 7.11.1). */
378         lid = be16_to_cpu(hdr->lrh[1]);
379         if (lid < IPS_MULTICAST_LID_BASE) {
380                 lid &= ~((1 << (dev->mkeyprot_resv_lmc & 7)) - 1);
381                 if (unlikely(lid != ipath_layer_get_lid(dev->dd))) {
382                         dev->rcv_errors++;
383                         goto bail;
384                 }
385         }
386
387         /* Check for GRH */
388         lnh = be16_to_cpu(hdr->lrh[0]) & 3;
389         if (lnh == IPS_LRH_BTH)
390                 ohdr = &hdr->u.oth;
391         else if (lnh == IPS_LRH_GRH)
392                 ohdr = &hdr->u.l.oth;
393         else {
394                 dev->rcv_errors++;
395                 goto bail;
396         }
397
398         opcode = be32_to_cpu(ohdr->bth[0]) >> 24;
399         dev->opstats[opcode].n_bytes += tlen;
400         dev->opstats[opcode].n_packets++;
401
402         /* Get the destination QP number. */
403         qp_num = be32_to_cpu(ohdr->bth[1]) & IPS_QPN_MASK;
404         if (qp_num == IPS_MULTICAST_QPN) {
405                 struct ipath_mcast *mcast;
406                 struct ipath_mcast_qp *p;
407
408                 mcast = ipath_mcast_find(&hdr->u.l.grh.dgid);
409                 if (mcast == NULL) {
410                         dev->n_pkt_drops++;
411                         goto bail;
412                 }
413                 dev->n_multicast_rcv++;
414                 list_for_each_entry_rcu(p, &mcast->qp_list, list)
415                         ipath_qp_rcv(dev, hdr, lnh == IPS_LRH_GRH, data,
416                                      tlen, p->qp);
417                 /*
418                  * Notify ipath_multicast_detach() if it is waiting for us
419                  * to finish.
420                  */
421                 if (atomic_dec_return(&mcast->refcount) <= 1)
422                         wake_up(&mcast->wait);
423         } else {
424                 qp = ipath_lookup_qpn(&dev->qp_table, qp_num);
425                 if (qp) {
426                         dev->n_unicast_rcv++;
427                         ipath_qp_rcv(dev, hdr, lnh == IPS_LRH_GRH, data,
428                                      tlen, qp);
429                         /*
430                          * Notify ipath_destroy_qp() if it is waiting
431                          * for us to finish.
432                          */
433                         if (atomic_dec_and_test(&qp->refcount))
434                                 wake_up(&qp->wait);
435                 } else
436                         dev->n_pkt_drops++;
437         }
438
439 bail:;
440 }
441
442 /**
443  * ipath_ib_timer - verbs timer
444  * @arg: the device pointer
445  *
446  * This is called from ipath_do_rcv_timer() at interrupt level to check for
447  * QPs which need retransmits and to collect performance numbers.
448  */
449 static void ipath_ib_timer(void *arg)
450 {
451         struct ipath_ibdev *dev = (struct ipath_ibdev *) arg;
452         struct ipath_qp *resend = NULL;
453         struct list_head *last;
454         struct ipath_qp *qp;
455         unsigned long flags;
456
457         if (dev == NULL)
458                 return;
459
460         spin_lock_irqsave(&dev->pending_lock, flags);
461         /* Start filling the next pending queue. */
462         if (++dev->pending_index >= ARRAY_SIZE(dev->pending))
463                 dev->pending_index = 0;
464         /* Save any requests still in the new queue, they have timed out. */
465         last = &dev->pending[dev->pending_index];
466         while (!list_empty(last)) {
467                 qp = list_entry(last->next, struct ipath_qp, timerwait);
468                 list_del_init(&qp->timerwait);
469                 qp->timer_next = resend;
470                 resend = qp;
471                 atomic_inc(&qp->refcount);
472         }
473         last = &dev->rnrwait;
474         if (!list_empty(last)) {
475                 qp = list_entry(last->next, struct ipath_qp, timerwait);
476                 if (--qp->s_rnr_timeout == 0) {
477                         do {
478                                 list_del_init(&qp->timerwait);
479                                 tasklet_hi_schedule(&qp->s_task);
480                                 if (list_empty(last))
481                                         break;
482                                 qp = list_entry(last->next, struct ipath_qp,
483                                                 timerwait);
484                         } while (qp->s_rnr_timeout == 0);
485                 }
486         }
487         /*
488          * We should only be in the started state if pma_sample_start != 0
489          */
490         if (dev->pma_sample_status == IB_PMA_SAMPLE_STATUS_STARTED &&
491             --dev->pma_sample_start == 0) {
492                 dev->pma_sample_status = IB_PMA_SAMPLE_STATUS_RUNNING;
493                 ipath_layer_snapshot_counters(dev->dd, &dev->ipath_sword,
494                                               &dev->ipath_rword,
495                                               &dev->ipath_spkts,
496                                               &dev->ipath_rpkts,
497                                               &dev->ipath_xmit_wait);
498         }
499         if (dev->pma_sample_status == IB_PMA_SAMPLE_STATUS_RUNNING) {
500                 if (dev->pma_sample_interval == 0) {
501                         u64 ta, tb, tc, td, te;
502
503                         dev->pma_sample_status = IB_PMA_SAMPLE_STATUS_DONE;
504                         ipath_layer_snapshot_counters(dev->dd, &ta, &tb,
505                                                       &tc, &td, &te);
506
507                         dev->ipath_sword = ta - dev->ipath_sword;
508                         dev->ipath_rword = tb - dev->ipath_rword;
509                         dev->ipath_spkts = tc - dev->ipath_spkts;
510                         dev->ipath_rpkts = td - dev->ipath_rpkts;
511                         dev->ipath_xmit_wait = te - dev->ipath_xmit_wait;
512                 }
513                 else
514                         dev->pma_sample_interval--;
515         }
516         spin_unlock_irqrestore(&dev->pending_lock, flags);
517
518         /* XXX What if timer fires again while this is running? */
519         for (qp = resend; qp != NULL; qp = qp->timer_next) {
520                 struct ib_wc wc;
521
522                 spin_lock_irqsave(&qp->s_lock, flags);
523                 if (qp->s_last != qp->s_tail && qp->state == IB_QPS_RTS) {
524                         dev->n_timeouts++;
525                         ipath_restart_rc(qp, qp->s_last_psn + 1, &wc);
526                 }
527                 spin_unlock_irqrestore(&qp->s_lock, flags);
528
529                 /* Notify ipath_destroy_qp() if it is waiting. */
530                 if (atomic_dec_and_test(&qp->refcount))
531                         wake_up(&qp->wait);
532         }
533 }
534
535 /**
536  * ipath_ib_piobufavail - callback when a PIO buffer is available
537  * @arg: the device pointer
538  *
539  * This is called from ipath_intr() at interrupt level when a PIO buffer is
540  * available after ipath_verbs_send() returned an error that no buffers were
541  * available.  Return 1 if we consumed all the PIO buffers and we still have
542  * QPs waiting for buffers (for now, just do a tasklet_hi_schedule and
543  * return zero).
544  */
545 static int ipath_ib_piobufavail(void *arg)
546 {
547         struct ipath_ibdev *dev = (struct ipath_ibdev *) arg;
548         struct ipath_qp *qp;
549         unsigned long flags;
550
551         if (dev == NULL)
552                 goto bail;
553
554         spin_lock_irqsave(&dev->pending_lock, flags);
555         while (!list_empty(&dev->piowait)) {
556                 qp = list_entry(dev->piowait.next, struct ipath_qp,
557                                 piowait);
558                 list_del_init(&qp->piowait);
559                 tasklet_hi_schedule(&qp->s_task);
560         }
561         spin_unlock_irqrestore(&dev->pending_lock, flags);
562
563 bail:
564         return 0;
565 }
566
567 static int ipath_query_device(struct ib_device *ibdev,
568                               struct ib_device_attr *props)
569 {
570         struct ipath_ibdev *dev = to_idev(ibdev);
571         u32 vendor, boardrev, majrev, minrev;
572
573         memset(props, 0, sizeof(*props));
574
575         props->device_cap_flags = IB_DEVICE_BAD_PKEY_CNTR |
576                 IB_DEVICE_BAD_QKEY_CNTR | IB_DEVICE_SHUTDOWN_PORT |
577                 IB_DEVICE_SYS_IMAGE_GUID;
578         ipath_layer_query_device(dev->dd, &vendor, &boardrev,
579                                  &majrev, &minrev);
580         props->vendor_id = vendor;
581         props->vendor_part_id = boardrev;
582         props->hw_ver = boardrev << 16 | majrev << 8 | minrev;
583
584         props->sys_image_guid = dev->sys_image_guid;
585
586         props->max_mr_size = ~0ull;
587         props->max_qp = 0xffff;
588         props->max_qp_wr = 0xffff;
589         props->max_sge = 255;
590         props->max_cq = 0xffff;
591         props->max_cqe = 0xffff;
592         props->max_mr = 0xffff;
593         props->max_pd = 0xffff;
594         props->max_qp_rd_atom = 1;
595         props->max_qp_init_rd_atom = 1;
596         /* props->max_res_rd_atom */
597         props->max_srq = 0xffff;
598         props->max_srq_wr = 0xffff;
599         props->max_srq_sge = 255;
600         /* props->local_ca_ack_delay */
601         props->atomic_cap = IB_ATOMIC_HCA;
602         props->max_pkeys = ipath_layer_get_npkeys(dev->dd);
603         props->max_mcast_grp = 0xffff;
604         props->max_mcast_qp_attach = 0xffff;
605         props->max_total_mcast_qp_attach = props->max_mcast_qp_attach *
606                 props->max_mcast_grp;
607
608         return 0;
609 }
610
611 const u8 ipath_cvt_physportstate[16] = {
612         [INFINIPATH_IBCS_LT_STATE_DISABLED] = 3,
613         [INFINIPATH_IBCS_LT_STATE_LINKUP] = 5,
614         [INFINIPATH_IBCS_LT_STATE_POLLACTIVE] = 2,
615         [INFINIPATH_IBCS_LT_STATE_POLLQUIET] = 2,
616         [INFINIPATH_IBCS_LT_STATE_SLEEPDELAY] = 1,
617         [INFINIPATH_IBCS_LT_STATE_SLEEPQUIET] = 1,
618         [INFINIPATH_IBCS_LT_STATE_CFGDEBOUNCE] = 4,
619         [INFINIPATH_IBCS_LT_STATE_CFGRCVFCFG] = 4,
620         [INFINIPATH_IBCS_LT_STATE_CFGWAITRMT] = 4,
621         [INFINIPATH_IBCS_LT_STATE_CFGIDLE] = 4,
622         [INFINIPATH_IBCS_LT_STATE_RECOVERRETRAIN] = 6,
623         [INFINIPATH_IBCS_LT_STATE_RECOVERWAITRMT] = 6,
624         [INFINIPATH_IBCS_LT_STATE_RECOVERIDLE] = 6,
625 };
626
627 static int ipath_query_port(struct ib_device *ibdev,
628                             u8 port, struct ib_port_attr *props)
629 {
630         struct ipath_ibdev *dev = to_idev(ibdev);
631         enum ib_mtu mtu;
632         u16 lid = ipath_layer_get_lid(dev->dd);
633         u64 ibcstat;
634
635         memset(props, 0, sizeof(*props));
636         props->lid = lid ? lid : __constant_be16_to_cpu(IB_LID_PERMISSIVE);
637         props->lmc = dev->mkeyprot_resv_lmc & 7;
638         props->sm_lid = dev->sm_lid;
639         props->sm_sl = dev->sm_sl;
640         ibcstat = ipath_layer_get_lastibcstat(dev->dd);
641         props->state = ((ibcstat >> 4) & 0x3) + 1;
642         /* See phys_state_show() */
643         props->phys_state = ipath_cvt_physportstate[
644                 ipath_layer_get_lastibcstat(dev->dd) & 0xf];
645         props->port_cap_flags = dev->port_cap_flags;
646         props->gid_tbl_len = 1;
647         props->max_msg_sz = 4096;
648         props->pkey_tbl_len = ipath_layer_get_npkeys(dev->dd);
649         props->bad_pkey_cntr = ipath_layer_get_cr_errpkey(dev->dd) -
650                 dev->z_pkey_violations;
651         props->qkey_viol_cntr = dev->qkey_violations;
652         props->active_width = IB_WIDTH_4X;
653         /* See rate_show() */
654         props->active_speed = 1;        /* Regular 10Mbs speed. */
655         props->max_vl_num = 1;          /* VLCap = VL0 */
656         props->init_type_reply = 0;
657
658         props->max_mtu = IB_MTU_4096;
659         switch (ipath_layer_get_ibmtu(dev->dd)) {
660         case 4096:
661                 mtu = IB_MTU_4096;
662                 break;
663         case 2048:
664                 mtu = IB_MTU_2048;
665                 break;
666         case 1024:
667                 mtu = IB_MTU_1024;
668                 break;
669         case 512:
670                 mtu = IB_MTU_512;
671                 break;
672         case 256:
673                 mtu = IB_MTU_256;
674                 break;
675         default:
676                 mtu = IB_MTU_2048;
677         }
678         props->active_mtu = mtu;
679         props->subnet_timeout = dev->subnet_timeout;
680
681         return 0;
682 }
683
684 static int ipath_modify_device(struct ib_device *device,
685                                int device_modify_mask,
686                                struct ib_device_modify *device_modify)
687 {
688         int ret;
689
690         if (device_modify_mask & ~(IB_DEVICE_MODIFY_SYS_IMAGE_GUID |
691                                    IB_DEVICE_MODIFY_NODE_DESC)) {
692                 ret = -EOPNOTSUPP;
693                 goto bail;
694         }
695
696         if (device_modify_mask & IB_DEVICE_MODIFY_NODE_DESC)
697                 memcpy(device->node_desc, device_modify->node_desc, 64);
698
699         if (device_modify_mask & IB_DEVICE_MODIFY_SYS_IMAGE_GUID)
700                 to_idev(device)->sys_image_guid =
701                         cpu_to_be64(device_modify->sys_image_guid);
702
703         ret = 0;
704
705 bail:
706         return ret;
707 }
708
709 static int ipath_modify_port(struct ib_device *ibdev,
710                              u8 port, int port_modify_mask,
711                              struct ib_port_modify *props)
712 {
713         struct ipath_ibdev *dev = to_idev(ibdev);
714
715         dev->port_cap_flags |= props->set_port_cap_mask;
716         dev->port_cap_flags &= ~props->clr_port_cap_mask;
717         if (port_modify_mask & IB_PORT_SHUTDOWN)
718                 ipath_layer_set_linkstate(dev->dd, IPATH_IB_LINKDOWN);
719         if (port_modify_mask & IB_PORT_RESET_QKEY_CNTR)
720                 dev->qkey_violations = 0;
721         return 0;
722 }
723
724 static int ipath_query_gid(struct ib_device *ibdev, u8 port,
725                            int index, union ib_gid *gid)
726 {
727         struct ipath_ibdev *dev = to_idev(ibdev);
728         int ret;
729
730         if (index >= 1) {
731                 ret = -EINVAL;
732                 goto bail;
733         }
734         gid->global.subnet_prefix = dev->gid_prefix;
735         gid->global.interface_id = ipath_layer_get_guid(dev->dd);
736
737         ret = 0;
738
739 bail:
740         return ret;
741 }
742
743 static struct ib_pd *ipath_alloc_pd(struct ib_device *ibdev,
744                                     struct ib_ucontext *context,
745                                     struct ib_udata *udata)
746 {
747         struct ipath_pd *pd;
748         struct ib_pd *ret;
749
750         pd = kmalloc(sizeof *pd, GFP_KERNEL);
751         if (!pd) {
752                 ret = ERR_PTR(-ENOMEM);
753                 goto bail;
754         }
755
756         /* ib_alloc_pd() will initialize pd->ibpd. */
757         pd->user = udata != NULL;
758
759         ret = &pd->ibpd;
760
761 bail:
762         return ret;
763 }
764
765 static int ipath_dealloc_pd(struct ib_pd *ibpd)
766 {
767         struct ipath_pd *pd = to_ipd(ibpd);
768
769         kfree(pd);
770
771         return 0;
772 }
773
774 /**
775  * ipath_create_ah - create an address handle
776  * @pd: the protection domain
777  * @ah_attr: the attributes of the AH
778  *
779  * This may be called from interrupt context.
780  */
781 static struct ib_ah *ipath_create_ah(struct ib_pd *pd,
782                                      struct ib_ah_attr *ah_attr)
783 {
784         struct ipath_ah *ah;
785         struct ib_ah *ret;
786
787         /* A multicast address requires a GRH (see ch. 8.4.1). */
788         if (ah_attr->dlid >= IPS_MULTICAST_LID_BASE &&
789             ah_attr->dlid != IPS_PERMISSIVE_LID &&
790             !(ah_attr->ah_flags & IB_AH_GRH)) {
791                 ret = ERR_PTR(-EINVAL);
792                 goto bail;
793         }
794
795         ah = kmalloc(sizeof *ah, GFP_ATOMIC);
796         if (!ah) {
797                 ret = ERR_PTR(-ENOMEM);
798                 goto bail;
799         }
800
801         /* ib_create_ah() will initialize ah->ibah. */
802         ah->attr = *ah_attr;
803
804         ret = &ah->ibah;
805
806 bail:
807         return ret;
808 }
809
810 /**
811  * ipath_destroy_ah - destroy an address handle
812  * @ibah: the AH to destroy
813  *
814  * This may be called from interrupt context.
815  */
816 static int ipath_destroy_ah(struct ib_ah *ibah)
817 {
818         struct ipath_ah *ah = to_iah(ibah);
819
820         kfree(ah);
821
822         return 0;
823 }
824
825 static int ipath_query_ah(struct ib_ah *ibah, struct ib_ah_attr *ah_attr)
826 {
827         struct ipath_ah *ah = to_iah(ibah);
828
829         *ah_attr = ah->attr;
830
831         return 0;
832 }
833
834 static int ipath_query_pkey(struct ib_device *ibdev, u8 port, u16 index,
835                             u16 *pkey)
836 {
837         struct ipath_ibdev *dev = to_idev(ibdev);
838         int ret;
839
840         if (index >= ipath_layer_get_npkeys(dev->dd)) {
841                 ret = -EINVAL;
842                 goto bail;
843         }
844
845         *pkey = ipath_layer_get_pkey(dev->dd, index);
846         ret = 0;
847
848 bail:
849         return ret;
850 }
851
852
853 /**
854  * ipath_alloc_ucontext - allocate a ucontest
855  * @ibdev: the infiniband device
856  * @udata: not used by the InfiniPath driver
857  */
858
859 static struct ib_ucontext *ipath_alloc_ucontext(struct ib_device *ibdev,
860                                                 struct ib_udata *udata)
861 {
862         struct ipath_ucontext *context;
863         struct ib_ucontext *ret;
864
865         context = kmalloc(sizeof *context, GFP_KERNEL);
866         if (!context) {
867                 ret = ERR_PTR(-ENOMEM);
868                 goto bail;
869         }
870
871         ret = &context->ibucontext;
872
873 bail:
874         return ret;
875 }
876
877 static int ipath_dealloc_ucontext(struct ib_ucontext *context)
878 {
879         kfree(to_iucontext(context));
880         return 0;
881 }
882
883 static int ipath_verbs_register_sysfs(struct ib_device *dev);
884
885 /**
886  * ipath_register_ib_device - register our device with the infiniband core
887  * @unit: the device number to register
888  * @dd: the device data structure
889  * Return the allocated ipath_ibdev pointer or NULL on error.
890  */
891 static void *ipath_register_ib_device(int unit, struct ipath_devdata *dd)
892 {
893         struct ipath_ibdev *idev;
894         struct ib_device *dev;
895         int ret;
896
897         idev = (struct ipath_ibdev *)ib_alloc_device(sizeof *idev);
898         if (idev == NULL)
899                 goto bail;
900
901         dev = &idev->ibdev;
902
903         /* Only need to initialize non-zero fields. */
904         spin_lock_init(&idev->qp_table.lock);
905         spin_lock_init(&idev->lk_table.lock);
906         idev->sm_lid = __constant_be16_to_cpu(IB_LID_PERMISSIVE);
907         /* Set the prefix to the default value (see ch. 4.1.1) */
908         idev->gid_prefix = __constant_cpu_to_be64(0xfe80000000000000ULL);
909
910         ret = ipath_init_qp_table(idev, ib_ipath_qp_table_size);
911         if (ret)
912                 goto err_qp;
913
914         /*
915          * The top ib_ipath_lkey_table_size bits are used to index the
916          * table.  The lower 8 bits can be owned by the user (copied from
917          * the LKEY).  The remaining bits act as a generation number or tag.
918          */
919         idev->lk_table.max = 1 << ib_ipath_lkey_table_size;
920         idev->lk_table.table = kzalloc(idev->lk_table.max *
921                                        sizeof(*idev->lk_table.table),
922                                        GFP_KERNEL);
923         if (idev->lk_table.table == NULL) {
924                 ret = -ENOMEM;
925                 goto err_lk;
926         }
927         spin_lock_init(&idev->pending_lock);
928         INIT_LIST_HEAD(&idev->pending[0]);
929         INIT_LIST_HEAD(&idev->pending[1]);
930         INIT_LIST_HEAD(&idev->pending[2]);
931         INIT_LIST_HEAD(&idev->piowait);
932         INIT_LIST_HEAD(&idev->rnrwait);
933         idev->pending_index = 0;
934         idev->port_cap_flags =
935                 IB_PORT_SYS_IMAGE_GUID_SUP | IB_PORT_CLIENT_REG_SUP;
936         idev->pma_counter_select[0] = IB_PMA_PORT_XMIT_DATA;
937         idev->pma_counter_select[1] = IB_PMA_PORT_RCV_DATA;
938         idev->pma_counter_select[2] = IB_PMA_PORT_XMIT_PKTS;
939         idev->pma_counter_select[3] = IB_PMA_PORT_RCV_PKTS;
940         idev->pma_counter_select[5] = IB_PMA_PORT_XMIT_WAIT;
941         idev->link_width_enabled = 3;   /* 1x or 4x */
942
943         /*
944          * The system image GUID is supposed to be the same for all
945          * IB HCAs in a single system but since there can be other
946          * device types in the system, we can't be sure this is unique.
947          */
948         if (!sys_image_guid)
949                 sys_image_guid = ipath_layer_get_guid(dd);
950         idev->sys_image_guid = sys_image_guid;
951         idev->ib_unit = unit;
952         idev->dd = dd;
953
954         strlcpy(dev->name, "ipath%d", IB_DEVICE_NAME_MAX);
955         dev->owner = THIS_MODULE;
956         dev->node_guid = ipath_layer_get_guid(dd);
957         dev->uverbs_abi_ver = IPATH_UVERBS_ABI_VERSION;
958         dev->uverbs_cmd_mask =
959                 (1ull << IB_USER_VERBS_CMD_GET_CONTEXT)         |
960                 (1ull << IB_USER_VERBS_CMD_QUERY_DEVICE)        |
961                 (1ull << IB_USER_VERBS_CMD_QUERY_PORT)          |
962                 (1ull << IB_USER_VERBS_CMD_ALLOC_PD)            |
963                 (1ull << IB_USER_VERBS_CMD_DEALLOC_PD)          |
964                 (1ull << IB_USER_VERBS_CMD_CREATE_AH)           |
965                 (1ull << IB_USER_VERBS_CMD_DESTROY_AH)          |
966                 (1ull << IB_USER_VERBS_CMD_QUERY_AH)            |
967                 (1ull << IB_USER_VERBS_CMD_REG_MR)              |
968                 (1ull << IB_USER_VERBS_CMD_DEREG_MR)            |
969                 (1ull << IB_USER_VERBS_CMD_CREATE_COMP_CHANNEL) |
970                 (1ull << IB_USER_VERBS_CMD_CREATE_CQ)           |
971                 (1ull << IB_USER_VERBS_CMD_RESIZE_CQ)           |
972                 (1ull << IB_USER_VERBS_CMD_DESTROY_CQ)          |
973                 (1ull << IB_USER_VERBS_CMD_POLL_CQ)             |
974                 (1ull << IB_USER_VERBS_CMD_REQ_NOTIFY_CQ)       |
975                 (1ull << IB_USER_VERBS_CMD_CREATE_QP)           |
976                 (1ull << IB_USER_VERBS_CMD_QUERY_QP)            |
977                 (1ull << IB_USER_VERBS_CMD_MODIFY_QP)           |
978                 (1ull << IB_USER_VERBS_CMD_DESTROY_QP)          |
979                 (1ull << IB_USER_VERBS_CMD_POST_SEND)           |
980                 (1ull << IB_USER_VERBS_CMD_POST_RECV)           |
981                 (1ull << IB_USER_VERBS_CMD_ATTACH_MCAST)        |
982                 (1ull << IB_USER_VERBS_CMD_DETACH_MCAST)        |
983                 (1ull << IB_USER_VERBS_CMD_CREATE_SRQ)          |
984                 (1ull << IB_USER_VERBS_CMD_MODIFY_SRQ)          |
985                 (1ull << IB_USER_VERBS_CMD_QUERY_SRQ)           |
986                 (1ull << IB_USER_VERBS_CMD_DESTROY_SRQ)         |
987                 (1ull << IB_USER_VERBS_CMD_POST_SRQ_RECV);
988         dev->node_type = IB_NODE_CA;
989         dev->phys_port_cnt = 1;
990         dev->dma_device = ipath_layer_get_device(dd);
991         dev->class_dev.dev = dev->dma_device;
992         dev->query_device = ipath_query_device;
993         dev->modify_device = ipath_modify_device;
994         dev->query_port = ipath_query_port;
995         dev->modify_port = ipath_modify_port;
996         dev->query_pkey = ipath_query_pkey;
997         dev->query_gid = ipath_query_gid;
998         dev->alloc_ucontext = ipath_alloc_ucontext;
999         dev->dealloc_ucontext = ipath_dealloc_ucontext;
1000         dev->alloc_pd = ipath_alloc_pd;
1001         dev->dealloc_pd = ipath_dealloc_pd;
1002         dev->create_ah = ipath_create_ah;
1003         dev->destroy_ah = ipath_destroy_ah;
1004         dev->query_ah = ipath_query_ah;
1005         dev->create_srq = ipath_create_srq;
1006         dev->modify_srq = ipath_modify_srq;
1007         dev->query_srq = ipath_query_srq;
1008         dev->destroy_srq = ipath_destroy_srq;
1009         dev->create_qp = ipath_create_qp;
1010         dev->modify_qp = ipath_modify_qp;
1011         dev->query_qp = ipath_query_qp;
1012         dev->destroy_qp = ipath_destroy_qp;
1013         dev->post_send = ipath_post_send;
1014         dev->post_recv = ipath_post_receive;
1015         dev->post_srq_recv = ipath_post_srq_receive;
1016         dev->create_cq = ipath_create_cq;
1017         dev->destroy_cq = ipath_destroy_cq;
1018         dev->resize_cq = ipath_resize_cq;
1019         dev->poll_cq = ipath_poll_cq;
1020         dev->req_notify_cq = ipath_req_notify_cq;
1021         dev->get_dma_mr = ipath_get_dma_mr;
1022         dev->reg_phys_mr = ipath_reg_phys_mr;
1023         dev->reg_user_mr = ipath_reg_user_mr;
1024         dev->dereg_mr = ipath_dereg_mr;
1025         dev->alloc_fmr = ipath_alloc_fmr;
1026         dev->map_phys_fmr = ipath_map_phys_fmr;
1027         dev->unmap_fmr = ipath_unmap_fmr;
1028         dev->dealloc_fmr = ipath_dealloc_fmr;
1029         dev->attach_mcast = ipath_multicast_attach;
1030         dev->detach_mcast = ipath_multicast_detach;
1031         dev->process_mad = ipath_process_mad;
1032
1033         snprintf(dev->node_desc, sizeof(dev->node_desc),
1034                  IPATH_IDSTR " %s kernel_SMA", system_utsname.nodename);
1035
1036         ret = ib_register_device(dev);
1037         if (ret)
1038                 goto err_reg;
1039
1040         if (ipath_verbs_register_sysfs(dev))
1041                 goto err_class;
1042
1043         ipath_layer_enable_timer(dd);
1044
1045         goto bail;
1046
1047 err_class:
1048         ib_unregister_device(dev);
1049 err_reg:
1050         kfree(idev->lk_table.table);
1051 err_lk:
1052         kfree(idev->qp_table.table);
1053 err_qp:
1054         ib_dealloc_device(dev);
1055         _VERBS_ERROR("ib_ipath%d cannot register verbs (%d)!\n",
1056                      unit, -ret);
1057         idev = NULL;
1058
1059 bail:
1060         return idev;
1061 }
1062
1063 static void ipath_unregister_ib_device(void *arg)
1064 {
1065         struct ipath_ibdev *dev = (struct ipath_ibdev *) arg;
1066         struct ib_device *ibdev = &dev->ibdev;
1067
1068         ipath_layer_disable_timer(dev->dd);
1069
1070         ib_unregister_device(ibdev);
1071
1072         if (!list_empty(&dev->pending[0]) ||
1073             !list_empty(&dev->pending[1]) ||
1074             !list_empty(&dev->pending[2]))
1075                 _VERBS_ERROR("ipath%d pending list not empty!\n",
1076                              dev->ib_unit);
1077         if (!list_empty(&dev->piowait))
1078                 _VERBS_ERROR("ipath%d piowait list not empty!\n",
1079                              dev->ib_unit);
1080         if (!list_empty(&dev->rnrwait))
1081                 _VERBS_ERROR("ipath%d rnrwait list not empty!\n",
1082                              dev->ib_unit);
1083         if (!ipath_mcast_tree_empty())
1084                 _VERBS_ERROR("ipath%d multicast table memory leak!\n",
1085                              dev->ib_unit);
1086         /*
1087          * Note that ipath_unregister_ib_device() can be called before all
1088          * the QPs are destroyed!
1089          */
1090         ipath_free_all_qps(&dev->qp_table);
1091         kfree(dev->qp_table.table);
1092         kfree(dev->lk_table.table);
1093         ib_dealloc_device(ibdev);
1094 }
1095
1096 static int __init ipath_verbs_init(void)
1097 {
1098         return ipath_verbs_register(ipath_register_ib_device,
1099                                     ipath_unregister_ib_device,
1100                                     ipath_ib_piobufavail, ipath_ib_rcv,
1101                                     ipath_ib_timer);
1102 }
1103
1104 static void __exit ipath_verbs_cleanup(void)
1105 {
1106         ipath_verbs_unregister();
1107 }
1108
1109 static ssize_t show_rev(struct class_device *cdev, char *buf)
1110 {
1111         struct ipath_ibdev *dev =
1112                 container_of(cdev, struct ipath_ibdev, ibdev.class_dev);
1113         int vendor, boardrev, majrev, minrev;
1114
1115         ipath_layer_query_device(dev->dd, &vendor, &boardrev,
1116                                  &majrev, &minrev);
1117         return sprintf(buf, "%d.%d\n", majrev, minrev);
1118 }
1119
1120 static ssize_t show_hca(struct class_device *cdev, char *buf)
1121 {
1122         struct ipath_ibdev *dev =
1123                 container_of(cdev, struct ipath_ibdev, ibdev.class_dev);
1124         int ret;
1125
1126         ret = ipath_layer_get_boardname(dev->dd, buf, 128);
1127         if (ret < 0)
1128                 goto bail;
1129         strcat(buf, "\n");
1130         ret = strlen(buf);
1131
1132 bail:
1133         return ret;
1134 }
1135
1136 static ssize_t show_stats(struct class_device *cdev, char *buf)
1137 {
1138         struct ipath_ibdev *dev =
1139                 container_of(cdev, struct ipath_ibdev, ibdev.class_dev);
1140         int i;
1141         int len;
1142
1143         len = sprintf(buf,
1144                       "RC resends  %d\n"
1145                       "RC no QACK  %d\n"
1146                       "RC ACKs     %d\n"
1147                       "RC SEQ NAKs %d\n"
1148                       "RC RDMA seq %d\n"
1149                       "RC RNR NAKs %d\n"
1150                       "RC OTH NAKs %d\n"
1151                       "RC timeouts %d\n"
1152                       "RC RDMA dup %d\n"
1153                       "piobuf wait %d\n"
1154                       "no piobuf   %d\n"
1155                       "PKT drops   %d\n"
1156                       "WQE errs    %d\n",
1157                       dev->n_rc_resends, dev->n_rc_qacks, dev->n_rc_acks,
1158                       dev->n_seq_naks, dev->n_rdma_seq, dev->n_rnr_naks,
1159                       dev->n_other_naks, dev->n_timeouts,
1160                       dev->n_rdma_dup_busy, dev->n_piowait,
1161                       dev->n_no_piobuf, dev->n_pkt_drops, dev->n_wqe_errs);
1162         for (i = 0; i < ARRAY_SIZE(dev->opstats); i++) {
1163                 const struct ipath_opcode_stats *si = &dev->opstats[i];
1164
1165                 if (!si->n_packets && !si->n_bytes)
1166                         continue;
1167                 len += sprintf(buf + len, "%02x %llu/%llu\n", i,
1168                                (unsigned long long) si->n_packets,
1169                                (unsigned long long) si->n_bytes);
1170         }
1171         return len;
1172 }
1173
1174 static CLASS_DEVICE_ATTR(hw_rev, S_IRUGO, show_rev, NULL);
1175 static CLASS_DEVICE_ATTR(hca_type, S_IRUGO, show_hca, NULL);
1176 static CLASS_DEVICE_ATTR(board_id, S_IRUGO, show_hca, NULL);
1177 static CLASS_DEVICE_ATTR(stats, S_IRUGO, show_stats, NULL);
1178
1179 static struct class_device_attribute *ipath_class_attributes[] = {
1180         &class_device_attr_hw_rev,
1181         &class_device_attr_hca_type,
1182         &class_device_attr_board_id,
1183         &class_device_attr_stats
1184 };
1185
1186 static int ipath_verbs_register_sysfs(struct ib_device *dev)
1187 {
1188         int i;
1189         int ret;
1190
1191         for (i = 0; i < ARRAY_SIZE(ipath_class_attributes); ++i)
1192                 if (class_device_create_file(&dev->class_dev,
1193                                              ipath_class_attributes[i])) {
1194                         ret = 1;
1195                         goto bail;
1196                 }
1197
1198         ret = 0;
1199
1200 bail:
1201         return ret;
1202 }
1203
1204 module_init(ipath_verbs_init);
1205 module_exit(ipath_verbs_cleanup);