816a6bdc0b1c76473801cd808cf92341ddf065f0
[linux.git] / drivers / infiniband / hw / qib / qib_driver.c
1 /*
2  * Copyright (c) 2006, 2007, 2008, 2009 QLogic Corporation. All rights reserved.
3  * Copyright (c) 2003, 2004, 2005, 2006 PathScale, Inc. All rights reserved.
4  *
5  * This software is available to you under a choice of one of two
6  * licenses.  You may choose to be licensed under the terms of the GNU
7  * General Public License (GPL) Version 2, available from the file
8  * COPYING in the main directory of this source tree, or the
9  * OpenIB.org BSD license below:
10  *
11  *     Redistribution and use in source and binary forms, with or
12  *     without modification, are permitted provided that the following
13  *     conditions are met:
14  *
15  *      - Redistributions of source code must retain the above
16  *        copyright notice, this list of conditions and the following
17  *        disclaimer.
18  *
19  *      - Redistributions in binary form must reproduce the above
20  *        copyright notice, this list of conditions and the following
21  *        disclaimer in the documentation and/or other materials
22  *        provided with the distribution.
23  *
24  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
28  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
29  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
30  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
31  * SOFTWARE.
32  */
33
34 #include <linux/spinlock.h>
35 #include <linux/pci.h>
36 #include <linux/io.h>
37 #include <linux/delay.h>
38 #include <linux/netdevice.h>
39 #include <linux/vmalloc.h>
40
41 #include "qib.h"
42
43 /*
44  * The size has to be longer than this string, so we can append
45  * board/chip information to it in the init code.
46  */
47 const char ib_qib_version[] = QIB_IDSTR "\n";
48
49 DEFINE_SPINLOCK(qib_devs_lock);
50 LIST_HEAD(qib_dev_list);
51 DEFINE_MUTEX(qib_mutex);        /* general driver use */
52
53 unsigned qib_ibmtu;
54 module_param_named(ibmtu, qib_ibmtu, uint, S_IRUGO);
55 MODULE_PARM_DESC(ibmtu, "Set max IB MTU (0=2KB, 1=256, 2=512, ... 5=4096");
56
57 unsigned qib_compat_ddr_negotiate = 1;
58 module_param_named(compat_ddr_negotiate, qib_compat_ddr_negotiate, uint,
59                    S_IWUSR | S_IRUGO);
60 MODULE_PARM_DESC(compat_ddr_negotiate,
61                  "Attempt pre-IBTA 1.2 DDR speed negotiation");
62
63 MODULE_LICENSE("Dual BSD/GPL");
64 MODULE_AUTHOR("QLogic <support@qlogic.com>");
65 MODULE_DESCRIPTION("QLogic IB driver");
66
67 /*
68  * QIB_PIO_MAXIBHDR is the max IB header size allowed for in our
69  * PIO send buffers.  This is well beyond anything currently
70  * defined in the InfiniBand spec.
71  */
72 #define QIB_PIO_MAXIBHDR 128
73
74 /*
75  * QIB_MAX_PKT_RCV is the max # if packets processed per receive interrupt.
76  */
77 #define QIB_MAX_PKT_RECV 64
78
79 struct qlogic_ib_stats qib_stats;
80
81 const char *qib_get_unit_name(int unit)
82 {
83         static char iname[16];
84
85         snprintf(iname, sizeof iname, "infinipath%u", unit);
86         return iname;
87 }
88
89 /*
90  * Return count of units with at least one port ACTIVE.
91  */
92 int qib_count_active_units(void)
93 {
94         struct qib_devdata *dd;
95         struct qib_pportdata *ppd;
96         unsigned long flags;
97         int pidx, nunits_active = 0;
98
99         spin_lock_irqsave(&qib_devs_lock, flags);
100         list_for_each_entry(dd, &qib_dev_list, list) {
101                 if (!(dd->flags & QIB_PRESENT) || !dd->kregbase)
102                         continue;
103                 for (pidx = 0; pidx < dd->num_pports; ++pidx) {
104                         ppd = dd->pport + pidx;
105                         if (ppd->lid && (ppd->lflags & (QIBL_LINKINIT |
106                                          QIBL_LINKARMED | QIBL_LINKACTIVE))) {
107                                 nunits_active++;
108                                 break;
109                         }
110                 }
111         }
112         spin_unlock_irqrestore(&qib_devs_lock, flags);
113         return nunits_active;
114 }
115
116 /*
117  * Return count of all units, optionally return in arguments
118  * the number of usable (present) units, and the number of
119  * ports that are up.
120  */
121 int qib_count_units(int *npresentp, int *nupp)
122 {
123         int nunits = 0, npresent = 0, nup = 0;
124         struct qib_devdata *dd;
125         unsigned long flags;
126         int pidx;
127         struct qib_pportdata *ppd;
128
129         spin_lock_irqsave(&qib_devs_lock, flags);
130
131         list_for_each_entry(dd, &qib_dev_list, list) {
132                 nunits++;
133                 if ((dd->flags & QIB_PRESENT) && dd->kregbase)
134                         npresent++;
135                 for (pidx = 0; pidx < dd->num_pports; ++pidx) {
136                         ppd = dd->pport + pidx;
137                         if (ppd->lid && (ppd->lflags & (QIBL_LINKINIT |
138                                          QIBL_LINKARMED | QIBL_LINKACTIVE)))
139                                 nup++;
140                 }
141         }
142
143         spin_unlock_irqrestore(&qib_devs_lock, flags);
144
145         if (npresentp)
146                 *npresentp = npresent;
147         if (nupp)
148                 *nupp = nup;
149
150         return nunits;
151 }
152
153 /**
154  * qib_wait_linkstate - wait for an IB link state change to occur
155  * @dd: the qlogic_ib device
156  * @state: the state to wait for
157  * @msecs: the number of milliseconds to wait
158  *
159  * wait up to msecs milliseconds for IB link state change to occur for
160  * now, take the easy polling route.  Currently used only by
161  * qib_set_linkstate.  Returns 0 if state reached, otherwise
162  * -ETIMEDOUT state can have multiple states set, for any of several
163  * transitions.
164  */
165 int qib_wait_linkstate(struct qib_pportdata *ppd, u32 state, int msecs)
166 {
167         int ret;
168         unsigned long flags;
169
170         spin_lock_irqsave(&ppd->lflags_lock, flags);
171         if (ppd->state_wanted) {
172                 spin_unlock_irqrestore(&ppd->lflags_lock, flags);
173                 ret = -EBUSY;
174                 goto bail;
175         }
176         ppd->state_wanted = state;
177         spin_unlock_irqrestore(&ppd->lflags_lock, flags);
178         wait_event_interruptible_timeout(ppd->state_wait,
179                                          (ppd->lflags & state),
180                                          msecs_to_jiffies(msecs));
181         spin_lock_irqsave(&ppd->lflags_lock, flags);
182         ppd->state_wanted = 0;
183         spin_unlock_irqrestore(&ppd->lflags_lock, flags);
184
185         if (!(ppd->lflags & state))
186                 ret = -ETIMEDOUT;
187         else
188                 ret = 0;
189 bail:
190         return ret;
191 }
192
193 int qib_set_linkstate(struct qib_pportdata *ppd, u8 newstate)
194 {
195         u32 lstate;
196         int ret;
197         struct qib_devdata *dd = ppd->dd;
198         unsigned long flags;
199
200         switch (newstate) {
201         case QIB_IB_LINKDOWN_ONLY:
202                 dd->f_set_ib_cfg(ppd, QIB_IB_CFG_LSTATE,
203                                  IB_LINKCMD_DOWN | IB_LINKINITCMD_NOP);
204                 /* don't wait */
205                 ret = 0;
206                 goto bail;
207
208         case QIB_IB_LINKDOWN:
209                 dd->f_set_ib_cfg(ppd, QIB_IB_CFG_LSTATE,
210                                  IB_LINKCMD_DOWN | IB_LINKINITCMD_POLL);
211                 /* don't wait */
212                 ret = 0;
213                 goto bail;
214
215         case QIB_IB_LINKDOWN_SLEEP:
216                 dd->f_set_ib_cfg(ppd, QIB_IB_CFG_LSTATE,
217                                  IB_LINKCMD_DOWN | IB_LINKINITCMD_SLEEP);
218                 /* don't wait */
219                 ret = 0;
220                 goto bail;
221
222         case QIB_IB_LINKDOWN_DISABLE:
223                 dd->f_set_ib_cfg(ppd, QIB_IB_CFG_LSTATE,
224                                  IB_LINKCMD_DOWN | IB_LINKINITCMD_DISABLE);
225                 /* don't wait */
226                 ret = 0;
227                 goto bail;
228
229         case QIB_IB_LINKARM:
230                 if (ppd->lflags & QIBL_LINKARMED) {
231                         ret = 0;
232                         goto bail;
233                 }
234                 if (!(ppd->lflags & (QIBL_LINKINIT | QIBL_LINKACTIVE))) {
235                         ret = -EINVAL;
236                         goto bail;
237                 }
238                 /*
239                  * Since the port can be ACTIVE when we ask for ARMED,
240                  * clear QIBL_LINKV so we can wait for a transition.
241                  * If the link isn't ARMED, then something else happened
242                  * and there is no point waiting for ARMED.
243                  */
244                 spin_lock_irqsave(&ppd->lflags_lock, flags);
245                 ppd->lflags &= ~QIBL_LINKV;
246                 spin_unlock_irqrestore(&ppd->lflags_lock, flags);
247                 dd->f_set_ib_cfg(ppd, QIB_IB_CFG_LSTATE,
248                                  IB_LINKCMD_ARMED | IB_LINKINITCMD_NOP);
249                 lstate = QIBL_LINKV;
250                 break;
251
252         case QIB_IB_LINKACTIVE:
253                 if (ppd->lflags & QIBL_LINKACTIVE) {
254                         ret = 0;
255                         goto bail;
256                 }
257                 if (!(ppd->lflags & QIBL_LINKARMED)) {
258                         ret = -EINVAL;
259                         goto bail;
260                 }
261                 dd->f_set_ib_cfg(ppd, QIB_IB_CFG_LSTATE,
262                                  IB_LINKCMD_ACTIVE | IB_LINKINITCMD_NOP);
263                 lstate = QIBL_LINKACTIVE;
264                 break;
265
266         default:
267                 ret = -EINVAL;
268                 goto bail;
269         }
270         ret = qib_wait_linkstate(ppd, lstate, 10);
271
272 bail:
273         return ret;
274 }
275
276 /*
277  * Get address of eager buffer from it's index (allocated in chunks, not
278  * contiguous).
279  */
280 static inline void *qib_get_egrbuf(const struct qib_ctxtdata *rcd, u32 etail)
281 {
282         const u32 chunk = etail / rcd->rcvegrbufs_perchunk;
283         const u32 idx =  etail % rcd->rcvegrbufs_perchunk;
284
285         return rcd->rcvegrbuf[chunk] + idx * rcd->dd->rcvegrbufsize;
286 }
287
288 /*
289  * Returns 1 if error was a CRC, else 0.
290  * Needed for some chip's synthesized error counters.
291  */
292 static u32 qib_rcv_hdrerr(struct qib_pportdata *ppd, u32 ctxt,
293                           u32 eflags, u32 l, u32 etail, __le32 *rhf_addr,
294                           struct qib_message_header *hdr)
295 {
296         u32 ret = 0;
297
298         if (eflags & (QLOGIC_IB_RHF_H_ICRCERR | QLOGIC_IB_RHF_H_VCRCERR))
299                 ret = 1;
300         return ret;
301 }
302
303 /*
304  * qib_kreceive - receive a packet
305  * @rcd: the qlogic_ib context
306  * @llic: gets count of good packets needed to clear lli,
307  *          (used with chips that need need to track crcs for lli)
308  *
309  * called from interrupt handler for errors or receive interrupt
310  * Returns number of CRC error packets, needed by some chips for
311  * local link integrity tracking.   crcs are adjusted down by following
312  * good packets, if any, and count of good packets is also tracked.
313  */
314 u32 qib_kreceive(struct qib_ctxtdata *rcd, u32 *llic, u32 *npkts)
315 {
316         struct qib_devdata *dd = rcd->dd;
317         struct qib_pportdata *ppd = rcd->ppd;
318         __le32 *rhf_addr;
319         void *ebuf;
320         const u32 rsize = dd->rcvhdrentsize;        /* words */
321         const u32 maxcnt = dd->rcvhdrcnt * rsize;   /* words */
322         u32 etail = -1, l, hdrqtail;
323         struct qib_message_header *hdr;
324         u32 eflags, etype, tlen, i = 0, updegr = 0, crcs = 0;
325         int last;
326         u64 lval;
327         struct qib_qp *qp, *nqp;
328
329         l = rcd->head;
330         rhf_addr = (__le32 *) rcd->rcvhdrq + l + dd->rhf_offset;
331         if (dd->flags & QIB_NODMA_RTAIL) {
332                 u32 seq = qib_hdrget_seq(rhf_addr);
333                 if (seq != rcd->seq_cnt)
334                         goto bail;
335                 hdrqtail = 0;
336         } else {
337                 hdrqtail = qib_get_rcvhdrtail(rcd);
338                 if (l == hdrqtail)
339                         goto bail;
340                 smp_rmb();  /* prevent speculative reads of dma'ed hdrq */
341         }
342
343         for (last = 0, i = 1; !last; i += !last) {
344                 hdr = dd->f_get_msgheader(dd, rhf_addr);
345                 eflags = qib_hdrget_err_flags(rhf_addr);
346                 etype = qib_hdrget_rcv_type(rhf_addr);
347                 /* total length */
348                 tlen = qib_hdrget_length_in_bytes(rhf_addr);
349                 ebuf = NULL;
350                 if ((dd->flags & QIB_NODMA_RTAIL) ?
351                     qib_hdrget_use_egr_buf(rhf_addr) :
352                     (etype != RCVHQ_RCV_TYPE_EXPECTED)) {
353                         etail = qib_hdrget_index(rhf_addr);
354                         updegr = 1;
355                         if (tlen > sizeof(*hdr) ||
356                             etype >= RCVHQ_RCV_TYPE_NON_KD)
357                                 ebuf = qib_get_egrbuf(rcd, etail);
358                 }
359                 if (!eflags) {
360                         u16 lrh_len = be16_to_cpu(hdr->lrh[2]) << 2;
361
362                         if (lrh_len != tlen) {
363                                 qib_stats.sps_lenerrs++;
364                                 goto move_along;
365                         }
366                 }
367                 if (etype == RCVHQ_RCV_TYPE_NON_KD && !eflags &&
368                     ebuf == NULL &&
369                     tlen > (dd->rcvhdrentsize - 2 + 1 -
370                                 qib_hdrget_offset(rhf_addr)) << 2) {
371                         goto move_along;
372                 }
373
374                 /*
375                  * Both tiderr and qibhdrerr are set for all plain IB
376                  * packets; only qibhdrerr should be set.
377                  */
378                 if (unlikely(eflags))
379                         crcs += qib_rcv_hdrerr(ppd, rcd->ctxt, eflags, l,
380                                                etail, rhf_addr, hdr);
381                 else if (etype == RCVHQ_RCV_TYPE_NON_KD) {
382                         qib_ib_rcv(rcd, hdr, ebuf, tlen);
383                         if (crcs)
384                                 crcs--;
385                         else if (llic && *llic)
386                                 --*llic;
387                 }
388 move_along:
389                 l += rsize;
390                 if (l >= maxcnt)
391                         l = 0;
392                 if (i == QIB_MAX_PKT_RECV)
393                         last = 1;
394
395                 rhf_addr = (__le32 *) rcd->rcvhdrq + l + dd->rhf_offset;
396                 if (dd->flags & QIB_NODMA_RTAIL) {
397                         u32 seq = qib_hdrget_seq(rhf_addr);
398
399                         if (++rcd->seq_cnt > 13)
400                                 rcd->seq_cnt = 1;
401                         if (seq != rcd->seq_cnt)
402                                 last = 1;
403                 } else if (l == hdrqtail)
404                         last = 1;
405                 /*
406                  * Update head regs etc., every 16 packets, if not last pkt,
407                  * to help prevent rcvhdrq overflows, when many packets
408                  * are processed and queue is nearly full.
409                  * Don't request an interrupt for intermediate updates.
410                  */
411                 lval = l;
412                 if (!last && !(i & 0xf)) {
413                         dd->f_update_usrhead(rcd, lval, updegr, etail, i);
414                         updegr = 0;
415                 }
416         }
417
418         rcd->head = l;
419         rcd->pkt_count += i;
420
421         /*
422          * Iterate over all QPs waiting to respond.
423          * The list won't change since the IRQ is only run on one CPU.
424          */
425         list_for_each_entry_safe(qp, nqp, &rcd->qp_wait_list, rspwait) {
426                 list_del_init(&qp->rspwait);
427                 if (qp->r_flags & QIB_R_RSP_NAK) {
428                         qp->r_flags &= ~QIB_R_RSP_NAK;
429                         qib_send_rc_ack(qp);
430                 }
431                 if (qp->r_flags & QIB_R_RSP_SEND) {
432                         unsigned long flags;
433
434                         qp->r_flags &= ~QIB_R_RSP_SEND;
435                         spin_lock_irqsave(&qp->s_lock, flags);
436                         if (ib_qib_state_ops[qp->state] &
437                                         QIB_PROCESS_OR_FLUSH_SEND)
438                                 qib_schedule_send(qp);
439                         spin_unlock_irqrestore(&qp->s_lock, flags);
440                 }
441                 if (atomic_dec_and_test(&qp->refcount))
442                         wake_up(&qp->wait);
443         }
444
445 bail:
446         /* Report number of packets consumed */
447         if (npkts)
448                 *npkts = i;
449
450         /*
451          * Always write head at end, and setup rcv interrupt, even
452          * if no packets were processed.
453          */
454         lval = (u64)rcd->head | dd->rhdrhead_intr_off;
455         dd->f_update_usrhead(rcd, lval, updegr, etail, i);
456         return crcs;
457 }
458
459 /**
460  * qib_set_mtu - set the MTU
461  * @ppd: the perport data
462  * @arg: the new MTU
463  *
464  * We can handle "any" incoming size, the issue here is whether we
465  * need to restrict our outgoing size.   For now, we don't do any
466  * sanity checking on this, and we don't deal with what happens to
467  * programs that are already running when the size changes.
468  * NOTE: changing the MTU will usually cause the IBC to go back to
469  * link INIT state...
470  */
471 int qib_set_mtu(struct qib_pportdata *ppd, u16 arg)
472 {
473         u32 piosize;
474         int ret, chk;
475
476         if (arg != 256 && arg != 512 && arg != 1024 && arg != 2048 &&
477             arg != 4096) {
478                 ret = -EINVAL;
479                 goto bail;
480         }
481         chk = ib_mtu_enum_to_int(qib_ibmtu);
482         if (chk > 0 && arg > chk) {
483                 ret = -EINVAL;
484                 goto bail;
485         }
486
487         piosize = ppd->ibmaxlen;
488         ppd->ibmtu = arg;
489
490         if (arg >= (piosize - QIB_PIO_MAXIBHDR)) {
491                 /* Only if it's not the initial value (or reset to it) */
492                 if (piosize != ppd->init_ibmaxlen) {
493                         if (arg > piosize && arg <= ppd->init_ibmaxlen)
494                                 piosize = ppd->init_ibmaxlen - 2 * sizeof(u32);
495                         ppd->ibmaxlen = piosize;
496                 }
497         } else if ((arg + QIB_PIO_MAXIBHDR) != ppd->ibmaxlen) {
498                 piosize = arg + QIB_PIO_MAXIBHDR - 2 * sizeof(u32);
499                 ppd->ibmaxlen = piosize;
500         }
501
502         ppd->dd->f_set_ib_cfg(ppd, QIB_IB_CFG_MTU, 0);
503
504         ret = 0;
505
506 bail:
507         return ret;
508 }
509
510 int qib_set_lid(struct qib_pportdata *ppd, u32 lid, u8 lmc)
511 {
512         struct qib_devdata *dd = ppd->dd;
513         ppd->lid = lid;
514         ppd->lmc = lmc;
515
516         dd->f_set_ib_cfg(ppd, QIB_IB_CFG_LIDLMC,
517                          lid | (~((1U << lmc) - 1)) << 16);
518
519         qib_devinfo(dd->pcidev, "IB%u:%u got a lid: 0x%x\n",
520                     dd->unit, ppd->port, lid);
521
522         return 0;
523 }
524
525 /*
526  * Following deal with the "obviously simple" task of overriding the state
527  * of the LEDS, which normally indicate link physical and logical status.
528  * The complications arise in dealing with different hardware mappings
529  * and the board-dependent routine being called from interrupts.
530  * and then there's the requirement to _flash_ them.
531  */
532 #define LED_OVER_FREQ_SHIFT 8
533 #define LED_OVER_FREQ_MASK (0xFF<<LED_OVER_FREQ_SHIFT)
534 /* Below is "non-zero" to force override, but both actual LEDs are off */
535 #define LED_OVER_BOTH_OFF (8)
536
537 static void qib_run_led_override(unsigned long opaque)
538 {
539         struct qib_pportdata *ppd = (struct qib_pportdata *)opaque;
540         struct qib_devdata *dd = ppd->dd;
541         int timeoff;
542         int ph_idx;
543
544         if (!(dd->flags & QIB_INITTED))
545                 return;
546
547         ph_idx = ppd->led_override_phase++ & 1;
548         ppd->led_override = ppd->led_override_vals[ph_idx];
549         timeoff = ppd->led_override_timeoff;
550
551         dd->f_setextled(ppd, 1);
552         /*
553          * don't re-fire the timer if user asked for it to be off; we let
554          * it fire one more time after they turn it off to simplify
555          */
556         if (ppd->led_override_vals[0] || ppd->led_override_vals[1])
557                 mod_timer(&ppd->led_override_timer, jiffies + timeoff);
558 }
559
560 void qib_set_led_override(struct qib_pportdata *ppd, unsigned int val)
561 {
562         struct qib_devdata *dd = ppd->dd;
563         int timeoff, freq;
564
565         if (!(dd->flags & QIB_INITTED))
566                 return;
567
568         /* First check if we are blinking. If not, use 1HZ polling */
569         timeoff = HZ;
570         freq = (val & LED_OVER_FREQ_MASK) >> LED_OVER_FREQ_SHIFT;
571
572         if (freq) {
573                 /* For blink, set each phase from one nybble of val */
574                 ppd->led_override_vals[0] = val & 0xF;
575                 ppd->led_override_vals[1] = (val >> 4) & 0xF;
576                 timeoff = (HZ << 4)/freq;
577         } else {
578                 /* Non-blink set both phases the same. */
579                 ppd->led_override_vals[0] = val & 0xF;
580                 ppd->led_override_vals[1] = val & 0xF;
581         }
582         ppd->led_override_timeoff = timeoff;
583
584         /*
585          * If the timer has not already been started, do so. Use a "quick"
586          * timeout so the function will be called soon, to look at our request.
587          */
588         if (atomic_inc_return(&ppd->led_override_timer_active) == 1) {
589                 /* Need to start timer */
590                 init_timer(&ppd->led_override_timer);
591                 ppd->led_override_timer.function = qib_run_led_override;
592                 ppd->led_override_timer.data = (unsigned long) ppd;
593                 ppd->led_override_timer.expires = jiffies + 1;
594                 add_timer(&ppd->led_override_timer);
595         } else {
596                 if (ppd->led_override_vals[0] || ppd->led_override_vals[1])
597                         mod_timer(&ppd->led_override_timer, jiffies + 1);
598                 atomic_dec(&ppd->led_override_timer_active);
599         }
600 }
601
602 /**
603  * qib_reset_device - reset the chip if possible
604  * @unit: the device to reset
605  *
606  * Whether or not reset is successful, we attempt to re-initialize the chip
607  * (that is, much like a driver unload/reload).  We clear the INITTED flag
608  * so that the various entry points will fail until we reinitialize.  For
609  * now, we only allow this if no user contexts are open that use chip resources
610  */
611 int qib_reset_device(int unit)
612 {
613         int ret, i;
614         struct qib_devdata *dd = qib_lookup(unit);
615         struct qib_pportdata *ppd;
616         unsigned long flags;
617         int pidx;
618
619         if (!dd) {
620                 ret = -ENODEV;
621                 goto bail;
622         }
623
624         qib_devinfo(dd->pcidev, "Reset on unit %u requested\n", unit);
625
626         if (!dd->kregbase || !(dd->flags & QIB_PRESENT)) {
627                 qib_devinfo(dd->pcidev, "Invalid unit number %u or "
628                             "not initialized or not present\n", unit);
629                 ret = -ENXIO;
630                 goto bail;
631         }
632
633         spin_lock_irqsave(&dd->uctxt_lock, flags);
634         if (dd->rcd)
635                 for (i = dd->first_user_ctxt; i < dd->cfgctxts; i++) {
636                         if (!dd->rcd[i] || !dd->rcd[i]->cnt)
637                                 continue;
638                         spin_unlock_irqrestore(&dd->uctxt_lock, flags);
639                         ret = -EBUSY;
640                         goto bail;
641                 }
642         spin_unlock_irqrestore(&dd->uctxt_lock, flags);
643
644         for (pidx = 0; pidx < dd->num_pports; ++pidx) {
645                 ppd = dd->pport + pidx;
646                 if (atomic_read(&ppd->led_override_timer_active)) {
647                         /* Need to stop LED timer, _then_ shut off LEDs */
648                         del_timer_sync(&ppd->led_override_timer);
649                         atomic_set(&ppd->led_override_timer_active, 0);
650                 }
651
652                 /* Shut off LEDs after we are sure timer is not running */
653                 ppd->led_override = LED_OVER_BOTH_OFF;
654                 dd->f_setextled(ppd, 0);
655                 if (dd->flags & QIB_HAS_SEND_DMA)
656                         qib_teardown_sdma(ppd);
657         }
658
659         ret = dd->f_reset(dd);
660         if (ret == 1)
661                 ret = qib_init(dd, 1);
662         else
663                 ret = -EAGAIN;
664         if (ret)
665                 qib_dev_err(dd, "Reinitialize unit %u after "
666                             "reset failed with %d\n", unit, ret);
667         else
668                 qib_devinfo(dd->pcidev, "Reinitialized unit %u after "
669                             "resetting\n", unit);
670
671 bail:
672         return ret;
673 }