Back to home page

OSCL-LXR

 
 

    


0001 /*
0002  * Copyright (c) 2008, 2009, 2010 QLogic Corporation. All rights reserved.
0003  *
0004  * This software is available to you under a choice of one of two
0005  * licenses.  You may choose to be licensed under the terms of the GNU
0006  * General Public License (GPL) Version 2, available from the file
0007  * COPYING in the main directory of this source tree, or the
0008  * OpenIB.org BSD license below:
0009  *
0010  *     Redistribution and use in source and binary forms, with or
0011  *     without modification, are permitted provided that the following
0012  *     conditions are met:
0013  *
0014  *      - Redistributions of source code must retain the above
0015  *        copyright notice, this list of conditions and the following
0016  *        disclaimer.
0017  *
0018  *      - Redistributions in binary form must reproduce the above
0019  *        copyright notice, this list of conditions and the following
0020  *        disclaimer in the documentation and/or other materials
0021  *        provided with the distribution.
0022  *
0023  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
0024  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
0025  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
0026  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
0027  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
0028  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
0029  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
0030  * SOFTWARE.
0031  */
0032 
0033 #include <linux/spinlock.h>
0034 #include <linux/pci.h>
0035 #include <linux/io.h>
0036 #include <linux/delay.h>
0037 #include <linux/netdevice.h>
0038 #include <linux/vmalloc.h>
0039 #include <linux/moduleparam.h>
0040 
0041 #include "qib.h"
0042 
0043 static unsigned qib_hol_timeout_ms = 3000;
0044 module_param_named(hol_timeout_ms, qib_hol_timeout_ms, uint, S_IRUGO);
0045 MODULE_PARM_DESC(hol_timeout_ms,
0046          "duration of user app suspension after link failure");
0047 
0048 unsigned qib_sdma_fetch_arb = 1;
0049 module_param_named(fetch_arb, qib_sdma_fetch_arb, uint, S_IRUGO);
0050 MODULE_PARM_DESC(fetch_arb, "IBA7220: change SDMA descriptor arbitration");
0051 
0052 /**
0053  * qib_disarm_piobufs - cancel a range of PIO buffers
0054  * @dd: the qlogic_ib device
0055  * @first: the first PIO buffer to cancel
0056  * @cnt: the number of PIO buffers to cancel
0057  *
0058  * Cancel a range of PIO buffers. Used at user process close,
0059  * in case it died while writing to a PIO buffer.
0060  */
0061 void qib_disarm_piobufs(struct qib_devdata *dd, unsigned first, unsigned cnt)
0062 {
0063     unsigned long flags;
0064     unsigned i;
0065     unsigned last;
0066 
0067     last = first + cnt;
0068     spin_lock_irqsave(&dd->pioavail_lock, flags);
0069     for (i = first; i < last; i++) {
0070         __clear_bit(i, dd->pio_need_disarm);
0071         dd->f_sendctrl(dd->pport, QIB_SENDCTRL_DISARM_BUF(i));
0072     }
0073     spin_unlock_irqrestore(&dd->pioavail_lock, flags);
0074 }
0075 
0076 /*
0077  * This is called by a user process when it sees the DISARM_BUFS event
0078  * bit is set.
0079  */
0080 int qib_disarm_piobufs_ifneeded(struct qib_ctxtdata *rcd)
0081 {
0082     struct qib_devdata *dd = rcd->dd;
0083     unsigned i;
0084     unsigned last;
0085     unsigned n = 0;
0086 
0087     last = rcd->pio_base + rcd->piocnt;
0088     /*
0089      * Don't need uctxt_lock here, since user has called in to us.
0090      * Clear at start in case more interrupts set bits while we
0091      * are disarming
0092      */
0093     if (rcd->user_event_mask) {
0094         /*
0095          * subctxt_cnt is 0 if not shared, so do base
0096          * separately, first, then remaining subctxt, if any
0097          */
0098         clear_bit(_QIB_EVENT_DISARM_BUFS_BIT, &rcd->user_event_mask[0]);
0099         for (i = 1; i < rcd->subctxt_cnt; i++)
0100             clear_bit(_QIB_EVENT_DISARM_BUFS_BIT,
0101                   &rcd->user_event_mask[i]);
0102     }
0103     spin_lock_irq(&dd->pioavail_lock);
0104     for (i = rcd->pio_base; i < last; i++) {
0105         if (__test_and_clear_bit(i, dd->pio_need_disarm)) {
0106             n++;
0107             dd->f_sendctrl(rcd->ppd, QIB_SENDCTRL_DISARM_BUF(i));
0108         }
0109     }
0110     spin_unlock_irq(&dd->pioavail_lock);
0111     return 0;
0112 }
0113 
0114 static struct qib_pportdata *is_sdma_buf(struct qib_devdata *dd, unsigned i)
0115 {
0116     struct qib_pportdata *ppd;
0117     unsigned pidx;
0118 
0119     for (pidx = 0; pidx < dd->num_pports; pidx++) {
0120         ppd = dd->pport + pidx;
0121         if (i >= ppd->sdma_state.first_sendbuf &&
0122             i < ppd->sdma_state.last_sendbuf)
0123             return ppd;
0124     }
0125     return NULL;
0126 }
0127 
0128 /*
0129  * Return true if send buffer is being used by a user context.
0130  * Sets  _QIB_EVENT_DISARM_BUFS_BIT in user_event_mask as a side effect
0131  */
0132 static int find_ctxt(struct qib_devdata *dd, unsigned bufn)
0133 {
0134     struct qib_ctxtdata *rcd;
0135     unsigned ctxt;
0136     int ret = 0;
0137 
0138     spin_lock(&dd->uctxt_lock);
0139     for (ctxt = dd->first_user_ctxt; ctxt < dd->cfgctxts; ctxt++) {
0140         rcd = dd->rcd[ctxt];
0141         if (!rcd || bufn < rcd->pio_base ||
0142             bufn >= rcd->pio_base + rcd->piocnt)
0143             continue;
0144         if (rcd->user_event_mask) {
0145             int i;
0146             /*
0147              * subctxt_cnt is 0 if not shared, so do base
0148              * separately, first, then remaining subctxt, if any
0149              */
0150             set_bit(_QIB_EVENT_DISARM_BUFS_BIT,
0151                 &rcd->user_event_mask[0]);
0152             for (i = 1; i < rcd->subctxt_cnt; i++)
0153                 set_bit(_QIB_EVENT_DISARM_BUFS_BIT,
0154                     &rcd->user_event_mask[i]);
0155         }
0156         ret = 1;
0157         break;
0158     }
0159     spin_unlock(&dd->uctxt_lock);
0160 
0161     return ret;
0162 }
0163 
0164 /*
0165  * Disarm a set of send buffers.  If the buffer might be actively being
0166  * written to, mark the buffer to be disarmed later when it is not being
0167  * written to.
0168  *
0169  * This should only be called from the IRQ error handler.
0170  */
0171 void qib_disarm_piobufs_set(struct qib_devdata *dd, unsigned long *mask,
0172                 unsigned cnt)
0173 {
0174     struct qib_pportdata *ppd, *pppd[QIB_MAX_IB_PORTS];
0175     unsigned i;
0176     unsigned long flags;
0177 
0178     for (i = 0; i < dd->num_pports; i++)
0179         pppd[i] = NULL;
0180 
0181     for (i = 0; i < cnt; i++) {
0182         if (!test_bit(i, mask))
0183             continue;
0184         /*
0185          * If the buffer is owned by the DMA hardware,
0186          * reset the DMA engine.
0187          */
0188         ppd = is_sdma_buf(dd, i);
0189         if (ppd) {
0190             pppd[ppd->port] = ppd;
0191             continue;
0192         }
0193         /*
0194          * If the kernel is writing the buffer or the buffer is
0195          * owned by a user process, we can't clear it yet.
0196          */
0197         spin_lock_irqsave(&dd->pioavail_lock, flags);
0198         if (test_bit(i, dd->pio_writing) ||
0199             (!test_bit(i << 1, dd->pioavailkernel) &&
0200              find_ctxt(dd, i))) {
0201             __set_bit(i, dd->pio_need_disarm);
0202         } else {
0203             dd->f_sendctrl(dd->pport, QIB_SENDCTRL_DISARM_BUF(i));
0204         }
0205         spin_unlock_irqrestore(&dd->pioavail_lock, flags);
0206     }
0207 
0208     /* do cancel_sends once per port that had sdma piobufs in error */
0209     for (i = 0; i < dd->num_pports; i++)
0210         if (pppd[i])
0211             qib_cancel_sends(pppd[i]);
0212 }
0213 
0214 /**
0215  * update_send_bufs - update shadow copy of the PIO availability map
0216  * @dd: the qlogic_ib device
0217  *
0218  * called whenever our local copy indicates we have run out of send buffers
0219  */
0220 static void update_send_bufs(struct qib_devdata *dd)
0221 {
0222     unsigned long flags;
0223     unsigned i;
0224     const unsigned piobregs = dd->pioavregs;
0225 
0226     /*
0227      * If the generation (check) bits have changed, then we update the
0228      * busy bit for the corresponding PIO buffer.  This algorithm will
0229      * modify positions to the value they already have in some cases
0230      * (i.e., no change), but it's faster than changing only the bits
0231      * that have changed.
0232      *
0233      * We would like to do this atomicly, to avoid spinlocks in the
0234      * critical send path, but that's not really possible, given the
0235      * type of changes, and that this routine could be called on
0236      * multiple cpu's simultaneously, so we lock in this routine only,
0237      * to avoid conflicting updates; all we change is the shadow, and
0238      * it's a single 64 bit memory location, so by definition the update
0239      * is atomic in terms of what other cpu's can see in testing the
0240      * bits.  The spin_lock overhead isn't too bad, since it only
0241      * happens when all buffers are in use, so only cpu overhead, not
0242      * latency or bandwidth is affected.
0243      */
0244     if (!dd->pioavailregs_dma)
0245         return;
0246     spin_lock_irqsave(&dd->pioavail_lock, flags);
0247     for (i = 0; i < piobregs; i++) {
0248         u64 pchbusy, pchg, piov, pnew;
0249 
0250         piov = le64_to_cpu(dd->pioavailregs_dma[i]);
0251         pchg = dd->pioavailkernel[i] &
0252             ~(dd->pioavailshadow[i] ^ piov);
0253         pchbusy = pchg << QLOGIC_IB_SENDPIOAVAIL_BUSY_SHIFT;
0254         if (pchg && (pchbusy & dd->pioavailshadow[i])) {
0255             pnew = dd->pioavailshadow[i] & ~pchbusy;
0256             pnew |= piov & pchbusy;
0257             dd->pioavailshadow[i] = pnew;
0258         }
0259     }
0260     spin_unlock_irqrestore(&dd->pioavail_lock, flags);
0261 }
0262 
0263 /*
0264  * Debugging code and stats updates if no pio buffers available.
0265  */
0266 static noinline void no_send_bufs(struct qib_devdata *dd)
0267 {
0268     dd->upd_pio_shadow = 1;
0269 
0270     /* not atomic, but if we lose a stat count in a while, that's OK */
0271     qib_stats.sps_nopiobufs++;
0272 }
0273 
0274 /*
0275  * Common code for normal driver send buffer allocation, and reserved
0276  * allocation.
0277  *
0278  * Do appropriate marking as busy, etc.
0279  * Returns buffer pointer if one is found, otherwise NULL.
0280  */
0281 u32 __iomem *qib_getsendbuf_range(struct qib_devdata *dd, u32 *pbufnum,
0282                   u32 first, u32 last)
0283 {
0284     unsigned i, j, updated = 0;
0285     unsigned nbufs;
0286     unsigned long flags;
0287     unsigned long *shadow = dd->pioavailshadow;
0288     u32 __iomem *buf;
0289 
0290     if (!(dd->flags & QIB_PRESENT))
0291         return NULL;
0292 
0293     nbufs = last - first + 1; /* number in range to check */
0294     if (dd->upd_pio_shadow) {
0295 update_shadow:
0296         /*
0297          * Minor optimization.  If we had no buffers on last call,
0298          * start out by doing the update; continue and do scan even
0299          * if no buffers were updated, to be paranoid.
0300          */
0301         update_send_bufs(dd);
0302         updated++;
0303     }
0304     i = first;
0305     /*
0306      * While test_and_set_bit() is atomic, we do that and then the
0307      * change_bit(), and the pair is not.  See if this is the cause
0308      * of the remaining armlaunch errors.
0309      */
0310     spin_lock_irqsave(&dd->pioavail_lock, flags);
0311     if (dd->last_pio >= first && dd->last_pio <= last)
0312         i = dd->last_pio + 1;
0313     if (!first)
0314         /* adjust to min possible  */
0315         nbufs = last - dd->min_kernel_pio + 1;
0316     for (j = 0; j < nbufs; j++, i++) {
0317         if (i > last)
0318             i = !first ? dd->min_kernel_pio : first;
0319         if (__test_and_set_bit((2 * i) + 1, shadow))
0320             continue;
0321         /* flip generation bit */
0322         __change_bit(2 * i, shadow);
0323         /* remember that the buffer can be written to now */
0324         __set_bit(i, dd->pio_writing);
0325         if (!first && first != last) /* first == last on VL15, avoid */
0326             dd->last_pio = i;
0327         break;
0328     }
0329     spin_unlock_irqrestore(&dd->pioavail_lock, flags);
0330 
0331     if (j == nbufs) {
0332         if (!updated)
0333             /*
0334              * First time through; shadow exhausted, but may be
0335              * buffers available, try an update and then rescan.
0336              */
0337             goto update_shadow;
0338         no_send_bufs(dd);
0339         buf = NULL;
0340     } else {
0341         if (i < dd->piobcnt2k)
0342             buf = (u32 __iomem *)(dd->pio2kbase +
0343                 i * dd->palign);
0344         else if (i < dd->piobcnt2k + dd->piobcnt4k || !dd->piovl15base)
0345             buf = (u32 __iomem *)(dd->pio4kbase +
0346                 (i - dd->piobcnt2k) * dd->align4k);
0347         else
0348             buf = (u32 __iomem *)(dd->piovl15base +
0349                 (i - (dd->piobcnt2k + dd->piobcnt4k)) *
0350                 dd->align4k);
0351         if (pbufnum)
0352             *pbufnum = i;
0353         dd->upd_pio_shadow = 0;
0354     }
0355 
0356     return buf;
0357 }
0358 
0359 /*
0360  * Record that the caller is finished writing to the buffer so we don't
0361  * disarm it while it is being written and disarm it now if needed.
0362  */
0363 void qib_sendbuf_done(struct qib_devdata *dd, unsigned n)
0364 {
0365     unsigned long flags;
0366 
0367     spin_lock_irqsave(&dd->pioavail_lock, flags);
0368     __clear_bit(n, dd->pio_writing);
0369     if (__test_and_clear_bit(n, dd->pio_need_disarm))
0370         dd->f_sendctrl(dd->pport, QIB_SENDCTRL_DISARM_BUF(n));
0371     spin_unlock_irqrestore(&dd->pioavail_lock, flags);
0372 }
0373 
0374 /**
0375  * qib_chg_pioavailkernel - change which send buffers are available for kernel
0376  * @dd: the qlogic_ib device
0377  * @start: the starting send buffer number
0378  * @len: the number of send buffers
0379  * @avail: true if the buffers are available for kernel use, false otherwise
0380  * @rcd: the context pointer
0381  */
0382 void qib_chg_pioavailkernel(struct qib_devdata *dd, unsigned start,
0383     unsigned len, u32 avail, struct qib_ctxtdata *rcd)
0384 {
0385     unsigned long flags;
0386     unsigned end;
0387     unsigned ostart = start;
0388 
0389     /* There are two bits per send buffer (busy and generation) */
0390     start *= 2;
0391     end = start + len * 2;
0392 
0393     spin_lock_irqsave(&dd->pioavail_lock, flags);
0394     /* Set or clear the busy bit in the shadow. */
0395     while (start < end) {
0396         if (avail) {
0397             unsigned long dma;
0398             int i;
0399 
0400             /*
0401              * The BUSY bit will never be set, because we disarm
0402              * the user buffers before we hand them back to the
0403              * kernel.  We do have to make sure the generation
0404              * bit is set correctly in shadow, since it could
0405              * have changed many times while allocated to user.
0406              * We can't use the bitmap functions on the full
0407              * dma array because it is always little-endian, so
0408              * we have to flip to host-order first.
0409              * BITS_PER_LONG is slightly wrong, since it's
0410              * always 64 bits per register in chip...
0411              * We only work on 64 bit kernels, so that's OK.
0412              */
0413             i = start / BITS_PER_LONG;
0414             __clear_bit(QLOGIC_IB_SENDPIOAVAIL_BUSY_SHIFT + start,
0415                     dd->pioavailshadow);
0416             dma = (unsigned long)
0417                 le64_to_cpu(dd->pioavailregs_dma[i]);
0418             if (test_bit((QLOGIC_IB_SENDPIOAVAIL_CHECK_SHIFT +
0419                       start) % BITS_PER_LONG, &dma))
0420                 __set_bit(QLOGIC_IB_SENDPIOAVAIL_CHECK_SHIFT +
0421                       start, dd->pioavailshadow);
0422             else
0423                 __clear_bit(QLOGIC_IB_SENDPIOAVAIL_CHECK_SHIFT
0424                         + start, dd->pioavailshadow);
0425             __set_bit(start, dd->pioavailkernel);
0426             if ((start >> 1) < dd->min_kernel_pio)
0427                 dd->min_kernel_pio = start >> 1;
0428         } else {
0429             __set_bit(start + QLOGIC_IB_SENDPIOAVAIL_BUSY_SHIFT,
0430                   dd->pioavailshadow);
0431             __clear_bit(start, dd->pioavailkernel);
0432             if ((start >> 1) > dd->min_kernel_pio)
0433                 dd->min_kernel_pio = start >> 1;
0434         }
0435         start += 2;
0436     }
0437 
0438     if (dd->min_kernel_pio > 0 && dd->last_pio < dd->min_kernel_pio - 1)
0439         dd->last_pio = dd->min_kernel_pio - 1;
0440     spin_unlock_irqrestore(&dd->pioavail_lock, flags);
0441 
0442     dd->f_txchk_change(dd, ostart, len, avail, rcd);
0443 }
0444 
0445 /*
0446  * Flush all sends that might be in the ready to send state, as well as any
0447  * that are in the process of being sent.  Used whenever we need to be
0448  * sure the send side is idle.  Cleans up all buffer state by canceling
0449  * all pio buffers, and issuing an abort, which cleans up anything in the
0450  * launch fifo.  The cancel is superfluous on some chip versions, but
0451  * it's safer to always do it.
0452  * PIOAvail bits are updated by the chip as if a normal send had happened.
0453  */
0454 void qib_cancel_sends(struct qib_pportdata *ppd)
0455 {
0456     struct qib_devdata *dd = ppd->dd;
0457     struct qib_ctxtdata *rcd;
0458     unsigned long flags;
0459     unsigned ctxt;
0460     unsigned i;
0461     unsigned last;
0462 
0463     /*
0464      * Tell PSM to disarm buffers again before trying to reuse them.
0465      * We need to be sure the rcd doesn't change out from under us
0466      * while we do so.  We hold the two locks sequentially.  We might
0467      * needlessly set some need_disarm bits as a result, if the
0468      * context is closed after we release the uctxt_lock, but that's
0469      * fairly benign, and safer than nesting the locks.
0470      */
0471     for (ctxt = dd->first_user_ctxt; ctxt < dd->cfgctxts; ctxt++) {
0472         spin_lock_irqsave(&dd->uctxt_lock, flags);
0473         rcd = dd->rcd[ctxt];
0474         if (rcd && rcd->ppd == ppd) {
0475             last = rcd->pio_base + rcd->piocnt;
0476             if (rcd->user_event_mask) {
0477                 /*
0478                  * subctxt_cnt is 0 if not shared, so do base
0479                  * separately, first, then remaining subctxt,
0480                  * if any
0481                  */
0482                 set_bit(_QIB_EVENT_DISARM_BUFS_BIT,
0483                     &rcd->user_event_mask[0]);
0484                 for (i = 1; i < rcd->subctxt_cnt; i++)
0485                     set_bit(_QIB_EVENT_DISARM_BUFS_BIT,
0486                         &rcd->user_event_mask[i]);
0487             }
0488             i = rcd->pio_base;
0489             spin_unlock_irqrestore(&dd->uctxt_lock, flags);
0490             spin_lock_irqsave(&dd->pioavail_lock, flags);
0491             for (; i < last; i++)
0492                 __set_bit(i, dd->pio_need_disarm);
0493             spin_unlock_irqrestore(&dd->pioavail_lock, flags);
0494         } else
0495             spin_unlock_irqrestore(&dd->uctxt_lock, flags);
0496     }
0497 
0498     if (!(dd->flags & QIB_HAS_SEND_DMA))
0499         dd->f_sendctrl(ppd, QIB_SENDCTRL_DISARM_ALL |
0500                     QIB_SENDCTRL_FLUSH);
0501 }
0502 
0503 /*
0504  * Force an update of in-memory copy of the pioavail registers, when
0505  * needed for any of a variety of reasons.
0506  * If already off, this routine is a nop, on the assumption that the
0507  * caller (or set of callers) will "do the right thing".
0508  * This is a per-device operation, so just the first port.
0509  */
0510 void qib_force_pio_avail_update(struct qib_devdata *dd)
0511 {
0512     dd->f_sendctrl(dd->pport, QIB_SENDCTRL_AVAIL_BLIP);
0513 }
0514 
0515 void qib_hol_down(struct qib_pportdata *ppd)
0516 {
0517     /*
0518      * Cancel sends when the link goes DOWN so that we aren't doing it
0519      * at INIT when we might be trying to send SMI packets.
0520      */
0521     if (!(ppd->lflags & QIBL_IB_AUTONEG_INPROG))
0522         qib_cancel_sends(ppd);
0523 }
0524 
0525 /*
0526  * Link is at INIT.
0527  * We start the HoL timer so we can detect stuck packets blocking SMP replies.
0528  * Timer may already be running, so use mod_timer, not add_timer.
0529  */
0530 void qib_hol_init(struct qib_pportdata *ppd)
0531 {
0532     if (ppd->hol_state != QIB_HOL_INIT) {
0533         ppd->hol_state = QIB_HOL_INIT;
0534         mod_timer(&ppd->hol_timer,
0535               jiffies + msecs_to_jiffies(qib_hol_timeout_ms));
0536     }
0537 }
0538 
0539 /*
0540  * Link is up, continue any user processes, and ensure timer
0541  * is a nop, if running.  Let timer keep running, if set; it
0542  * will nop when it sees the link is up.
0543  */
0544 void qib_hol_up(struct qib_pportdata *ppd)
0545 {
0546     ppd->hol_state = QIB_HOL_UP;
0547 }
0548 
0549 /*
0550  * This is only called via the timer.
0551  */
0552 void qib_hol_event(struct timer_list *t)
0553 {
0554     struct qib_pportdata *ppd = from_timer(ppd, t, hol_timer);
0555 
0556     /* If hardware error, etc, skip. */
0557     if (!(ppd->dd->flags & QIB_INITTED))
0558         return;
0559 
0560     if (ppd->hol_state != QIB_HOL_UP) {
0561         /*
0562          * Try to flush sends in case a stuck packet is blocking
0563          * SMP replies.
0564          */
0565         qib_hol_down(ppd);
0566         mod_timer(&ppd->hol_timer,
0567               jiffies + msecs_to_jiffies(qib_hol_timeout_ms));
0568     }
0569 }