1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35#include <linux/spinlock.h>
36#include <linux/pci.h>
37#include <linux/io.h>
38#include <linux/delay.h>
39#include <linux/netdevice.h>
40#include <linux/vmalloc.h>
41#include <linux/module.h>
42#include <linux/prefetch.h>
43
44#include "qib.h"
45
46
47
48
49
50const char ib_qib_version[] = QIB_DRIVER_VERSION "\n";
51
52DEFINE_SPINLOCK(qib_devs_lock);
53LIST_HEAD(qib_dev_list);
54DEFINE_MUTEX(qib_mutex);
55
56unsigned qib_ibmtu;
57module_param_named(ibmtu, qib_ibmtu, uint, S_IRUGO);
58MODULE_PARM_DESC(ibmtu, "Set max IB MTU (0=2KB, 1=256, 2=512, ... 5=4096");
59
60unsigned qib_compat_ddr_negotiate = 1;
61module_param_named(compat_ddr_negotiate, qib_compat_ddr_negotiate, uint,
62 S_IWUSR | S_IRUGO);
63MODULE_PARM_DESC(compat_ddr_negotiate,
64 "Attempt pre-IBTA 1.2 DDR speed negotiation");
65
66MODULE_LICENSE("Dual BSD/GPL");
67MODULE_AUTHOR("Intel <ibsupport@intel.com>");
68MODULE_DESCRIPTION("Intel IB driver");
69
70
71
72
73
74
75#define QIB_PIO_MAXIBHDR 128
76
77
78
79
80#define QIB_MAX_PKT_RECV 64
81
82struct qlogic_ib_stats qib_stats;
83
84struct pci_dev *qib_get_pci_dev(struct rvt_dev_info *rdi)
85{
86 struct qib_ibdev *ibdev = container_of(rdi, struct qib_ibdev, rdi);
87 struct qib_devdata *dd = container_of(ibdev,
88 struct qib_devdata, verbs_dev);
89 return dd->pcidev;
90}
91
92
93
94
95int qib_count_active_units(void)
96{
97 struct qib_devdata *dd;
98 struct qib_pportdata *ppd;
99 unsigned long flags;
100 int pidx, nunits_active = 0;
101
102 spin_lock_irqsave(&qib_devs_lock, flags);
103 list_for_each_entry(dd, &qib_dev_list, list) {
104 if (!(dd->flags & QIB_PRESENT) || !dd->kregbase)
105 continue;
106 for (pidx = 0; pidx < dd->num_pports; ++pidx) {
107 ppd = dd->pport + pidx;
108 if (ppd->lid && (ppd->lflags & (QIBL_LINKINIT |
109 QIBL_LINKARMED | QIBL_LINKACTIVE))) {
110 nunits_active++;
111 break;
112 }
113 }
114 }
115 spin_unlock_irqrestore(&qib_devs_lock, flags);
116 return nunits_active;
117}
118
119
120
121
122
123
124int qib_count_units(int *npresentp, int *nupp)
125{
126 int nunits = 0, npresent = 0, nup = 0;
127 struct qib_devdata *dd;
128 unsigned long flags;
129 int pidx;
130 struct qib_pportdata *ppd;
131
132 spin_lock_irqsave(&qib_devs_lock, flags);
133
134 list_for_each_entry(dd, &qib_dev_list, list) {
135 nunits++;
136 if ((dd->flags & QIB_PRESENT) && dd->kregbase)
137 npresent++;
138 for (pidx = 0; pidx < dd->num_pports; ++pidx) {
139 ppd = dd->pport + pidx;
140 if (ppd->lid && (ppd->lflags & (QIBL_LINKINIT |
141 QIBL_LINKARMED | QIBL_LINKACTIVE)))
142 nup++;
143 }
144 }
145
146 spin_unlock_irqrestore(&qib_devs_lock, flags);
147
148 if (npresentp)
149 *npresentp = npresent;
150 if (nupp)
151 *nupp = nup;
152
153 return nunits;
154}
155
156
157
158
159
160
161
162
163
164
165
166
167
168int qib_wait_linkstate(struct qib_pportdata *ppd, u32 state, int msecs)
169{
170 int ret;
171 unsigned long flags;
172
173 spin_lock_irqsave(&ppd->lflags_lock, flags);
174 if (ppd->state_wanted) {
175 spin_unlock_irqrestore(&ppd->lflags_lock, flags);
176 ret = -EBUSY;
177 goto bail;
178 }
179 ppd->state_wanted = state;
180 spin_unlock_irqrestore(&ppd->lflags_lock, flags);
181 wait_event_interruptible_timeout(ppd->state_wait,
182 (ppd->lflags & state),
183 msecs_to_jiffies(msecs));
184 spin_lock_irqsave(&ppd->lflags_lock, flags);
185 ppd->state_wanted = 0;
186 spin_unlock_irqrestore(&ppd->lflags_lock, flags);
187
188 if (!(ppd->lflags & state))
189 ret = -ETIMEDOUT;
190 else
191 ret = 0;
192bail:
193 return ret;
194}
195
196int qib_set_linkstate(struct qib_pportdata *ppd, u8 newstate)
197{
198 u32 lstate;
199 int ret;
200 struct qib_devdata *dd = ppd->dd;
201 unsigned long flags;
202
203 switch (newstate) {
204 case QIB_IB_LINKDOWN_ONLY:
205 dd->f_set_ib_cfg(ppd, QIB_IB_CFG_LSTATE,
206 IB_LINKCMD_DOWN | IB_LINKINITCMD_NOP);
207
208 ret = 0;
209 goto bail;
210
211 case QIB_IB_LINKDOWN:
212 dd->f_set_ib_cfg(ppd, QIB_IB_CFG_LSTATE,
213 IB_LINKCMD_DOWN | IB_LINKINITCMD_POLL);
214
215 ret = 0;
216 goto bail;
217
218 case QIB_IB_LINKDOWN_SLEEP:
219 dd->f_set_ib_cfg(ppd, QIB_IB_CFG_LSTATE,
220 IB_LINKCMD_DOWN | IB_LINKINITCMD_SLEEP);
221
222 ret = 0;
223 goto bail;
224
225 case QIB_IB_LINKDOWN_DISABLE:
226 dd->f_set_ib_cfg(ppd, QIB_IB_CFG_LSTATE,
227 IB_LINKCMD_DOWN | IB_LINKINITCMD_DISABLE);
228
229 ret = 0;
230 goto bail;
231
232 case QIB_IB_LINKARM:
233 if (ppd->lflags & QIBL_LINKARMED) {
234 ret = 0;
235 goto bail;
236 }
237 if (!(ppd->lflags & (QIBL_LINKINIT | QIBL_LINKACTIVE))) {
238 ret = -EINVAL;
239 goto bail;
240 }
241
242
243
244
245
246
247 spin_lock_irqsave(&ppd->lflags_lock, flags);
248 ppd->lflags &= ~QIBL_LINKV;
249 spin_unlock_irqrestore(&ppd->lflags_lock, flags);
250 dd->f_set_ib_cfg(ppd, QIB_IB_CFG_LSTATE,
251 IB_LINKCMD_ARMED | IB_LINKINITCMD_NOP);
252 lstate = QIBL_LINKV;
253 break;
254
255 case QIB_IB_LINKACTIVE:
256 if (ppd->lflags & QIBL_LINKACTIVE) {
257 ret = 0;
258 goto bail;
259 }
260 if (!(ppd->lflags & QIBL_LINKARMED)) {
261 ret = -EINVAL;
262 goto bail;
263 }
264 dd->f_set_ib_cfg(ppd, QIB_IB_CFG_LSTATE,
265 IB_LINKCMD_ACTIVE | IB_LINKINITCMD_NOP);
266 lstate = QIBL_LINKACTIVE;
267 break;
268
269 default:
270 ret = -EINVAL;
271 goto bail;
272 }
273 ret = qib_wait_linkstate(ppd, lstate, 10);
274
275bail:
276 return ret;
277}
278
279
280
281
282
283static inline void *qib_get_egrbuf(const struct qib_ctxtdata *rcd, u32 etail)
284{
285 const u32 chunk = etail >> rcd->rcvegrbufs_perchunk_shift;
286 const u32 idx = etail & ((u32)rcd->rcvegrbufs_perchunk - 1);
287
288 return rcd->rcvegrbuf[chunk] + (idx << rcd->dd->rcvegrbufsize_shift);
289}
290
291
292
293
294
295static u32 qib_rcv_hdrerr(struct qib_ctxtdata *rcd, struct qib_pportdata *ppd,
296 u32 ctxt, u32 eflags, u32 l, u32 etail,
297 __le32 *rhf_addr, struct qib_message_header *rhdr)
298{
299 u32 ret = 0;
300
301 if (eflags & (QLOGIC_IB_RHF_H_ICRCERR | QLOGIC_IB_RHF_H_VCRCERR))
302 ret = 1;
303 else if (eflags == QLOGIC_IB_RHF_H_TIDERR) {
304
305 struct ib_header *hdr = (struct ib_header *)rhdr;
306 struct ib_other_headers *ohdr = NULL;
307 struct qib_ibport *ibp = &ppd->ibport_data;
308 struct qib_devdata *dd = ppd->dd;
309 struct rvt_dev_info *rdi = &dd->verbs_dev.rdi;
310 struct rvt_qp *qp = NULL;
311 u32 tlen = qib_hdrget_length_in_bytes(rhf_addr);
312 u16 lid = be16_to_cpu(hdr->lrh[1]);
313 int lnh = be16_to_cpu(hdr->lrh[0]) & 3;
314 u32 qp_num;
315 u32 opcode;
316 u32 psn;
317 int diff;
318
319
320 if (tlen < 24)
321 goto drop;
322
323 if (lid < be16_to_cpu(IB_MULTICAST_LID_BASE)) {
324 lid &= ~((1 << ppd->lmc) - 1);
325 if (unlikely(lid != ppd->lid))
326 goto drop;
327 }
328
329
330 if (lnh == QIB_LRH_BTH)
331 ohdr = &hdr->u.oth;
332 else if (lnh == QIB_LRH_GRH) {
333 u32 vtf;
334
335 ohdr = &hdr->u.l.oth;
336 if (hdr->u.l.grh.next_hdr != IB_GRH_NEXT_HDR)
337 goto drop;
338 vtf = be32_to_cpu(hdr->u.l.grh.version_tclass_flow);
339 if ((vtf >> IB_GRH_VERSION_SHIFT) != IB_GRH_VERSION)
340 goto drop;
341 } else
342 goto drop;
343
344
345 opcode = be32_to_cpu(ohdr->bth[0]);
346 opcode >>= 24;
347 psn = be32_to_cpu(ohdr->bth[2]);
348
349
350 qp_num = be32_to_cpu(ohdr->bth[1]) & RVT_QPN_MASK;
351 if (qp_num != QIB_MULTICAST_QPN) {
352 int ruc_res;
353
354 rcu_read_lock();
355 qp = rvt_lookup_qpn(rdi, &ibp->rvp, qp_num);
356 if (!qp) {
357 rcu_read_unlock();
358 goto drop;
359 }
360
361
362
363
364
365 spin_lock(&qp->r_lock);
366
367
368 if (!(ib_rvt_state_ops[qp->state] &
369 RVT_PROCESS_RECV_OK)) {
370 ibp->rvp.n_pkt_drops++;
371 goto unlock;
372 }
373
374 switch (qp->ibqp.qp_type) {
375 case IB_QPT_RC:
376 ruc_res =
377 qib_ruc_check_hdr(
378 ibp, hdr,
379 lnh == QIB_LRH_GRH,
380 qp,
381 be32_to_cpu(ohdr->bth[0]));
382 if (ruc_res)
383 goto unlock;
384
385
386 if (opcode <
387 IB_OPCODE_RC_RDMA_READ_RESPONSE_FIRST) {
388 diff = qib_cmp24(psn, qp->r_psn);
389 if (!qp->r_nak_state && diff >= 0) {
390 ibp->rvp.n_rc_seqnak++;
391 qp->r_nak_state =
392 IB_NAK_PSN_ERROR;
393
394 qp->r_ack_psn = qp->r_psn;
395
396
397
398
399
400
401
402
403 if (list_empty(&qp->rspwait)) {
404 qp->r_flags |=
405 RVT_R_RSP_NAK;
406 rvt_get_qp(qp);
407 list_add_tail(
408 &qp->rspwait,
409 &rcd->qp_wait_list);
410 }
411 }
412 }
413 break;
414 case IB_QPT_SMI:
415 case IB_QPT_GSI:
416 case IB_QPT_UD:
417 case IB_QPT_UC:
418 default:
419
420 break;
421 }
422
423unlock:
424 spin_unlock(&qp->r_lock);
425 rcu_read_unlock();
426 }
427 }
428
429drop:
430 return ret;
431}
432
433
434
435
436
437
438
439
440
441
442
443
444u32 qib_kreceive(struct qib_ctxtdata *rcd, u32 *llic, u32 *npkts)
445{
446 struct qib_devdata *dd = rcd->dd;
447 struct qib_pportdata *ppd = rcd->ppd;
448 __le32 *rhf_addr;
449 void *ebuf;
450 const u32 rsize = dd->rcvhdrentsize;
451 const u32 maxcnt = dd->rcvhdrcnt * rsize;
452 u32 etail = -1, l, hdrqtail;
453 struct qib_message_header *hdr;
454 u32 eflags, etype, tlen, i = 0, updegr = 0, crcs = 0;
455 int last;
456 u64 lval;
457 struct rvt_qp *qp, *nqp;
458
459 l = rcd->head;
460 rhf_addr = (__le32 *) rcd->rcvhdrq + l + dd->rhf_offset;
461 if (dd->flags & QIB_NODMA_RTAIL) {
462 u32 seq = qib_hdrget_seq(rhf_addr);
463
464 if (seq != rcd->seq_cnt)
465 goto bail;
466 hdrqtail = 0;
467 } else {
468 hdrqtail = qib_get_rcvhdrtail(rcd);
469 if (l == hdrqtail)
470 goto bail;
471 smp_rmb();
472 }
473
474 for (last = 0, i = 1; !last; i += !last) {
475 hdr = dd->f_get_msgheader(dd, rhf_addr);
476 eflags = qib_hdrget_err_flags(rhf_addr);
477 etype = qib_hdrget_rcv_type(rhf_addr);
478
479 tlen = qib_hdrget_length_in_bytes(rhf_addr);
480 ebuf = NULL;
481 if ((dd->flags & QIB_NODMA_RTAIL) ?
482 qib_hdrget_use_egr_buf(rhf_addr) :
483 (etype != RCVHQ_RCV_TYPE_EXPECTED)) {
484 etail = qib_hdrget_index(rhf_addr);
485 updegr = 1;
486 if (tlen > sizeof(*hdr) ||
487 etype >= RCVHQ_RCV_TYPE_NON_KD) {
488 ebuf = qib_get_egrbuf(rcd, etail);
489 prefetch_range(ebuf, tlen - sizeof(*hdr));
490 }
491 }
492 if (!eflags) {
493 u16 lrh_len = be16_to_cpu(hdr->lrh[2]) << 2;
494
495 if (lrh_len != tlen) {
496 qib_stats.sps_lenerrs++;
497 goto move_along;
498 }
499 }
500 if (etype == RCVHQ_RCV_TYPE_NON_KD && !eflags &&
501 ebuf == NULL &&
502 tlen > (dd->rcvhdrentsize - 2 + 1 -
503 qib_hdrget_offset(rhf_addr)) << 2) {
504 goto move_along;
505 }
506
507
508
509
510
511 if (unlikely(eflags))
512 crcs += qib_rcv_hdrerr(rcd, ppd, rcd->ctxt, eflags, l,
513 etail, rhf_addr, hdr);
514 else if (etype == RCVHQ_RCV_TYPE_NON_KD) {
515 qib_ib_rcv(rcd, hdr, ebuf, tlen);
516 if (crcs)
517 crcs--;
518 else if (llic && *llic)
519 --*llic;
520 }
521move_along:
522 l += rsize;
523 if (l >= maxcnt)
524 l = 0;
525 if (i == QIB_MAX_PKT_RECV)
526 last = 1;
527
528 rhf_addr = (__le32 *) rcd->rcvhdrq + l + dd->rhf_offset;
529 if (dd->flags & QIB_NODMA_RTAIL) {
530 u32 seq = qib_hdrget_seq(rhf_addr);
531
532 if (++rcd->seq_cnt > 13)
533 rcd->seq_cnt = 1;
534 if (seq != rcd->seq_cnt)
535 last = 1;
536 } else if (l == hdrqtail)
537 last = 1;
538
539
540
541
542
543
544 lval = l;
545 if (!last && !(i & 0xf)) {
546 dd->f_update_usrhead(rcd, lval, updegr, etail, i);
547 updegr = 0;
548 }
549 }
550
551 rcd->head = l;
552
553
554
555
556
557 list_for_each_entry_safe(qp, nqp, &rcd->qp_wait_list, rspwait) {
558 list_del_init(&qp->rspwait);
559 if (qp->r_flags & RVT_R_RSP_NAK) {
560 qp->r_flags &= ~RVT_R_RSP_NAK;
561 qib_send_rc_ack(qp);
562 }
563 if (qp->r_flags & RVT_R_RSP_SEND) {
564 unsigned long flags;
565
566 qp->r_flags &= ~RVT_R_RSP_SEND;
567 spin_lock_irqsave(&qp->s_lock, flags);
568 if (ib_rvt_state_ops[qp->state] &
569 RVT_PROCESS_OR_FLUSH_SEND)
570 qib_schedule_send(qp);
571 spin_unlock_irqrestore(&qp->s_lock, flags);
572 }
573 rvt_put_qp(qp);
574 }
575
576bail:
577
578 if (npkts)
579 *npkts = i;
580
581
582
583
584
585 lval = (u64)rcd->head | dd->rhdrhead_intr_off;
586 dd->f_update_usrhead(rcd, lval, updegr, etail, i);
587 return crcs;
588}
589
590
591
592
593
594
595
596
597
598
599
600
601
602int qib_set_mtu(struct qib_pportdata *ppd, u16 arg)
603{
604 u32 piosize;
605 int ret, chk;
606
607 if (arg != 256 && arg != 512 && arg != 1024 && arg != 2048 &&
608 arg != 4096) {
609 ret = -EINVAL;
610 goto bail;
611 }
612 chk = ib_mtu_enum_to_int(qib_ibmtu);
613 if (chk > 0 && arg > chk) {
614 ret = -EINVAL;
615 goto bail;
616 }
617
618 piosize = ppd->ibmaxlen;
619 ppd->ibmtu = arg;
620
621 if (arg >= (piosize - QIB_PIO_MAXIBHDR)) {
622
623 if (piosize != ppd->init_ibmaxlen) {
624 if (arg > piosize && arg <= ppd->init_ibmaxlen)
625 piosize = ppd->init_ibmaxlen - 2 * sizeof(u32);
626 ppd->ibmaxlen = piosize;
627 }
628 } else if ((arg + QIB_PIO_MAXIBHDR) != ppd->ibmaxlen) {
629 piosize = arg + QIB_PIO_MAXIBHDR - 2 * sizeof(u32);
630 ppd->ibmaxlen = piosize;
631 }
632
633 ppd->dd->f_set_ib_cfg(ppd, QIB_IB_CFG_MTU, 0);
634
635 ret = 0;
636
637bail:
638 return ret;
639}
640
641int qib_set_lid(struct qib_pportdata *ppd, u32 lid, u8 lmc)
642{
643 struct qib_devdata *dd = ppd->dd;
644
645 ppd->lid = lid;
646 ppd->lmc = lmc;
647
648 dd->f_set_ib_cfg(ppd, QIB_IB_CFG_LIDLMC,
649 lid | (~((1U << lmc) - 1)) << 16);
650
651 qib_devinfo(dd->pcidev, "IB%u:%u got a lid: 0x%x\n",
652 dd->unit, ppd->port, lid);
653
654 return 0;
655}
656
657
658
659
660
661
662
663
664#define LED_OVER_FREQ_SHIFT 8
665#define LED_OVER_FREQ_MASK (0xFF<<LED_OVER_FREQ_SHIFT)
666
667#define LED_OVER_BOTH_OFF (8)
668
669static void qib_run_led_override(struct timer_list *t)
670{
671 struct qib_pportdata *ppd = from_timer(ppd, t,
672 led_override_timer);
673 struct qib_devdata *dd = ppd->dd;
674 int timeoff;
675 int ph_idx;
676
677 if (!(dd->flags & QIB_INITTED))
678 return;
679
680 ph_idx = ppd->led_override_phase++ & 1;
681 ppd->led_override = ppd->led_override_vals[ph_idx];
682 timeoff = ppd->led_override_timeoff;
683
684 dd->f_setextled(ppd, 1);
685
686
687
688
689 if (ppd->led_override_vals[0] || ppd->led_override_vals[1])
690 mod_timer(&ppd->led_override_timer, jiffies + timeoff);
691}
692
693void qib_set_led_override(struct qib_pportdata *ppd, unsigned int val)
694{
695 struct qib_devdata *dd = ppd->dd;
696 int timeoff, freq;
697
698 if (!(dd->flags & QIB_INITTED))
699 return;
700
701
702 timeoff = HZ;
703 freq = (val & LED_OVER_FREQ_MASK) >> LED_OVER_FREQ_SHIFT;
704
705 if (freq) {
706
707 ppd->led_override_vals[0] = val & 0xF;
708 ppd->led_override_vals[1] = (val >> 4) & 0xF;
709 timeoff = (HZ << 4)/freq;
710 } else {
711
712 ppd->led_override_vals[0] = val & 0xF;
713 ppd->led_override_vals[1] = val & 0xF;
714 }
715 ppd->led_override_timeoff = timeoff;
716
717
718
719
720
721 if (atomic_inc_return(&ppd->led_override_timer_active) == 1) {
722
723 timer_setup(&ppd->led_override_timer, qib_run_led_override, 0);
724 ppd->led_override_timer.expires = jiffies + 1;
725 add_timer(&ppd->led_override_timer);
726 } else {
727 if (ppd->led_override_vals[0] || ppd->led_override_vals[1])
728 mod_timer(&ppd->led_override_timer, jiffies + 1);
729 atomic_dec(&ppd->led_override_timer_active);
730 }
731}
732
733
734
735
736
737
738
739
740
741
742int qib_reset_device(int unit)
743{
744 int ret, i;
745 struct qib_devdata *dd = qib_lookup(unit);
746 struct qib_pportdata *ppd;
747 unsigned long flags;
748 int pidx;
749
750 if (!dd) {
751 ret = -ENODEV;
752 goto bail;
753 }
754
755 qib_devinfo(dd->pcidev, "Reset on unit %u requested\n", unit);
756
757 if (!dd->kregbase || !(dd->flags & QIB_PRESENT)) {
758 qib_devinfo(dd->pcidev,
759 "Invalid unit number %u or not initialized or not present\n",
760 unit);
761 ret = -ENXIO;
762 goto bail;
763 }
764
765 spin_lock_irqsave(&dd->uctxt_lock, flags);
766 if (dd->rcd)
767 for (i = dd->first_user_ctxt; i < dd->cfgctxts; i++) {
768 if (!dd->rcd[i] || !dd->rcd[i]->cnt)
769 continue;
770 spin_unlock_irqrestore(&dd->uctxt_lock, flags);
771 ret = -EBUSY;
772 goto bail;
773 }
774 spin_unlock_irqrestore(&dd->uctxt_lock, flags);
775
776 for (pidx = 0; pidx < dd->num_pports; ++pidx) {
777 ppd = dd->pport + pidx;
778 if (atomic_read(&ppd->led_override_timer_active)) {
779
780 del_timer_sync(&ppd->led_override_timer);
781 atomic_set(&ppd->led_override_timer_active, 0);
782 }
783
784
785 ppd->led_override = LED_OVER_BOTH_OFF;
786 dd->f_setextled(ppd, 0);
787 if (dd->flags & QIB_HAS_SEND_DMA)
788 qib_teardown_sdma(ppd);
789 }
790
791 ret = dd->f_reset(dd);
792 if (ret == 1)
793 ret = qib_init(dd, 1);
794 else
795 ret = -EAGAIN;
796 if (ret)
797 qib_dev_err(dd,
798 "Reinitialize unit %u after reset failed with %d\n",
799 unit, ret);
800 else
801 qib_devinfo(dd->pcidev,
802 "Reinitialized unit %u after resetting\n",
803 unit);
804
805bail:
806 return ret;
807}
808