Path: blob/master/drivers/infiniband/hw/qib/qib_tx.c
15112 views
/*1* Copyright (c) 2008, 2009, 2010 QLogic Corporation. All rights reserved.2*3* This software is available to you under a choice of one of two4* licenses. You may choose to be licensed under the terms of the GNU5* General Public License (GPL) Version 2, available from the file6* COPYING in the main directory of this source tree, or the7* OpenIB.org BSD license below:8*9* Redistribution and use in source and binary forms, with or10* without modification, are permitted provided that the following11* conditions are met:12*13* - Redistributions of source code must retain the above14* copyright notice, this list of conditions and the following15* disclaimer.16*17* - Redistributions in binary form must reproduce the above18* copyright notice, this list of conditions and the following19* disclaimer in the documentation and/or other materials20* provided with the distribution.21*22* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,23* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF24* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND25* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS26* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN27* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN28* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE29* SOFTWARE.30*/3132#include <linux/spinlock.h>33#include <linux/pci.h>34#include <linux/io.h>35#include <linux/delay.h>36#include <linux/netdevice.h>37#include <linux/vmalloc.h>3839#include "qib.h"4041static unsigned qib_hol_timeout_ms = 3000;42module_param_named(hol_timeout_ms, qib_hol_timeout_ms, uint, S_IRUGO);43MODULE_PARM_DESC(hol_timeout_ms,44"duration of user app suspension after link failure");4546unsigned qib_sdma_fetch_arb = 1;47module_param_named(fetch_arb, qib_sdma_fetch_arb, uint, S_IRUGO);48MODULE_PARM_DESC(fetch_arb, "IBA7220: change SDMA descriptor arbitration");4950/**51* qib_disarm_piobufs - cancel a range of PIO buffers52* @dd: the qlogic_ib device53* @first: the first PIO buffer to cancel54* @cnt: the number of PIO buffers to cancel55*56* Cancel a range of PIO buffers. Used at user process close,57* in case it died while writing to a PIO buffer.58*/59void qib_disarm_piobufs(struct qib_devdata *dd, unsigned first, unsigned cnt)60{61unsigned long flags;62unsigned i;63unsigned last;6465last = first + cnt;66spin_lock_irqsave(&dd->pioavail_lock, flags);67for (i = first; i < last; i++) {68__clear_bit(i, dd->pio_need_disarm);69dd->f_sendctrl(dd->pport, QIB_SENDCTRL_DISARM_BUF(i));70}71spin_unlock_irqrestore(&dd->pioavail_lock, flags);72}7374/*75* This is called by a user process when it sees the DISARM_BUFS event76* bit is set.77*/78int qib_disarm_piobufs_ifneeded(struct qib_ctxtdata *rcd)79{80struct qib_devdata *dd = rcd->dd;81unsigned i;82unsigned last;83unsigned n = 0;8485last = rcd->pio_base + rcd->piocnt;86/*87* Don't need uctxt_lock here, since user has called in to us.88* Clear at start in case more interrupts set bits while we89* are disarming90*/91if (rcd->user_event_mask) {92/*93* subctxt_cnt is 0 if not shared, so do base94* separately, first, then remaining subctxt, if any95*/96clear_bit(_QIB_EVENT_DISARM_BUFS_BIT, &rcd->user_event_mask[0]);97for (i = 1; i < rcd->subctxt_cnt; i++)98clear_bit(_QIB_EVENT_DISARM_BUFS_BIT,99&rcd->user_event_mask[i]);100}101spin_lock_irq(&dd->pioavail_lock);102for (i = rcd->pio_base; i < last; i++) {103if (__test_and_clear_bit(i, dd->pio_need_disarm)) {104n++;105dd->f_sendctrl(rcd->ppd, QIB_SENDCTRL_DISARM_BUF(i));106}107}108spin_unlock_irq(&dd->pioavail_lock);109return 0;110}111112static struct qib_pportdata *is_sdma_buf(struct qib_devdata *dd, unsigned i)113{114struct qib_pportdata *ppd;115unsigned pidx;116117for (pidx = 0; pidx < dd->num_pports; pidx++) {118ppd = dd->pport + pidx;119if (i >= ppd->sdma_state.first_sendbuf &&120i < ppd->sdma_state.last_sendbuf)121return ppd;122}123return NULL;124}125126/*127* Return true if send buffer is being used by a user context.128* Sets _QIB_EVENT_DISARM_BUFS_BIT in user_event_mask as a side effect129*/130static int find_ctxt(struct qib_devdata *dd, unsigned bufn)131{132struct qib_ctxtdata *rcd;133unsigned ctxt;134int ret = 0;135136spin_lock(&dd->uctxt_lock);137for (ctxt = dd->first_user_ctxt; ctxt < dd->cfgctxts; ctxt++) {138rcd = dd->rcd[ctxt];139if (!rcd || bufn < rcd->pio_base ||140bufn >= rcd->pio_base + rcd->piocnt)141continue;142if (rcd->user_event_mask) {143int i;144/*145* subctxt_cnt is 0 if not shared, so do base146* separately, first, then remaining subctxt, if any147*/148set_bit(_QIB_EVENT_DISARM_BUFS_BIT,149&rcd->user_event_mask[0]);150for (i = 1; i < rcd->subctxt_cnt; i++)151set_bit(_QIB_EVENT_DISARM_BUFS_BIT,152&rcd->user_event_mask[i]);153}154ret = 1;155break;156}157spin_unlock(&dd->uctxt_lock);158159return ret;160}161162/*163* Disarm a set of send buffers. If the buffer might be actively being164* written to, mark the buffer to be disarmed later when it is not being165* written to.166*167* This should only be called from the IRQ error handler.168*/169void qib_disarm_piobufs_set(struct qib_devdata *dd, unsigned long *mask,170unsigned cnt)171{172struct qib_pportdata *ppd, *pppd[QIB_MAX_IB_PORTS];173unsigned i;174unsigned long flags;175176for (i = 0; i < dd->num_pports; i++)177pppd[i] = NULL;178179for (i = 0; i < cnt; i++) {180int which;181if (!test_bit(i, mask))182continue;183/*184* If the buffer is owned by the DMA hardware,185* reset the DMA engine.186*/187ppd = is_sdma_buf(dd, i);188if (ppd) {189pppd[ppd->port] = ppd;190continue;191}192/*193* If the kernel is writing the buffer or the buffer is194* owned by a user process, we can't clear it yet.195*/196spin_lock_irqsave(&dd->pioavail_lock, flags);197if (test_bit(i, dd->pio_writing) ||198(!test_bit(i << 1, dd->pioavailkernel) &&199find_ctxt(dd, i))) {200__set_bit(i, dd->pio_need_disarm);201which = 0;202} else {203which = 1;204dd->f_sendctrl(dd->pport, QIB_SENDCTRL_DISARM_BUF(i));205}206spin_unlock_irqrestore(&dd->pioavail_lock, flags);207}208209/* do cancel_sends once per port that had sdma piobufs in error */210for (i = 0; i < dd->num_pports; i++)211if (pppd[i])212qib_cancel_sends(pppd[i]);213}214215/**216* update_send_bufs - update shadow copy of the PIO availability map217* @dd: the qlogic_ib device218*219* called whenever our local copy indicates we have run out of send buffers220*/221static void update_send_bufs(struct qib_devdata *dd)222{223unsigned long flags;224unsigned i;225const unsigned piobregs = dd->pioavregs;226227/*228* If the generation (check) bits have changed, then we update the229* busy bit for the corresponding PIO buffer. This algorithm will230* modify positions to the value they already have in some cases231* (i.e., no change), but it's faster than changing only the bits232* that have changed.233*234* We would like to do this atomicly, to avoid spinlocks in the235* critical send path, but that's not really possible, given the236* type of changes, and that this routine could be called on237* multiple cpu's simultaneously, so we lock in this routine only,238* to avoid conflicting updates; all we change is the shadow, and239* it's a single 64 bit memory location, so by definition the update240* is atomic in terms of what other cpu's can see in testing the241* bits. The spin_lock overhead isn't too bad, since it only242* happens when all buffers are in use, so only cpu overhead, not243* latency or bandwidth is affected.244*/245if (!dd->pioavailregs_dma)246return;247spin_lock_irqsave(&dd->pioavail_lock, flags);248for (i = 0; i < piobregs; i++) {249u64 pchbusy, pchg, piov, pnew;250251piov = le64_to_cpu(dd->pioavailregs_dma[i]);252pchg = dd->pioavailkernel[i] &253~(dd->pioavailshadow[i] ^ piov);254pchbusy = pchg << QLOGIC_IB_SENDPIOAVAIL_BUSY_SHIFT;255if (pchg && (pchbusy & dd->pioavailshadow[i])) {256pnew = dd->pioavailshadow[i] & ~pchbusy;257pnew |= piov & pchbusy;258dd->pioavailshadow[i] = pnew;259}260}261spin_unlock_irqrestore(&dd->pioavail_lock, flags);262}263264/*265* Debugging code and stats updates if no pio buffers available.266*/267static noinline void no_send_bufs(struct qib_devdata *dd)268{269dd->upd_pio_shadow = 1;270271/* not atomic, but if we lose a stat count in a while, that's OK */272qib_stats.sps_nopiobufs++;273}274275/*276* Common code for normal driver send buffer allocation, and reserved277* allocation.278*279* Do appropriate marking as busy, etc.280* Returns buffer pointer if one is found, otherwise NULL.281*/282u32 __iomem *qib_getsendbuf_range(struct qib_devdata *dd, u32 *pbufnum,283u32 first, u32 last)284{285unsigned i, j, updated = 0;286unsigned nbufs;287unsigned long flags;288unsigned long *shadow = dd->pioavailshadow;289u32 __iomem *buf;290291if (!(dd->flags & QIB_PRESENT))292return NULL;293294nbufs = last - first + 1; /* number in range to check */295if (dd->upd_pio_shadow) {296/*297* Minor optimization. If we had no buffers on last call,298* start out by doing the update; continue and do scan even299* if no buffers were updated, to be paranoid.300*/301update_send_bufs(dd);302updated++;303}304i = first;305rescan:306/*307* While test_and_set_bit() is atomic, we do that and then the308* change_bit(), and the pair is not. See if this is the cause309* of the remaining armlaunch errors.310*/311spin_lock_irqsave(&dd->pioavail_lock, flags);312for (j = 0; j < nbufs; j++, i++) {313if (i > last)314i = first;315if (__test_and_set_bit((2 * i) + 1, shadow))316continue;317/* flip generation bit */318__change_bit(2 * i, shadow);319/* remember that the buffer can be written to now */320__set_bit(i, dd->pio_writing);321break;322}323spin_unlock_irqrestore(&dd->pioavail_lock, flags);324325if (j == nbufs) {326if (!updated) {327/*328* First time through; shadow exhausted, but may be329* buffers available, try an update and then rescan.330*/331update_send_bufs(dd);332updated++;333i = first;334goto rescan;335}336no_send_bufs(dd);337buf = NULL;338} else {339if (i < dd->piobcnt2k)340buf = (u32 __iomem *)(dd->pio2kbase +341i * dd->palign);342else if (i < dd->piobcnt2k + dd->piobcnt4k || !dd->piovl15base)343buf = (u32 __iomem *)(dd->pio4kbase +344(i - dd->piobcnt2k) * dd->align4k);345else346buf = (u32 __iomem *)(dd->piovl15base +347(i - (dd->piobcnt2k + dd->piobcnt4k)) *348dd->align4k);349if (pbufnum)350*pbufnum = i;351dd->upd_pio_shadow = 0;352}353354return buf;355}356357/*358* Record that the caller is finished writing to the buffer so we don't359* disarm it while it is being written and disarm it now if needed.360*/361void qib_sendbuf_done(struct qib_devdata *dd, unsigned n)362{363unsigned long flags;364365spin_lock_irqsave(&dd->pioavail_lock, flags);366__clear_bit(n, dd->pio_writing);367if (__test_and_clear_bit(n, dd->pio_need_disarm))368dd->f_sendctrl(dd->pport, QIB_SENDCTRL_DISARM_BUF(n));369spin_unlock_irqrestore(&dd->pioavail_lock, flags);370}371372/**373* qib_chg_pioavailkernel - change which send buffers are available for kernel374* @dd: the qlogic_ib device375* @start: the starting send buffer number376* @len: the number of send buffers377* @avail: true if the buffers are available for kernel use, false otherwise378*/379void qib_chg_pioavailkernel(struct qib_devdata *dd, unsigned start,380unsigned len, u32 avail, struct qib_ctxtdata *rcd)381{382unsigned long flags;383unsigned end;384unsigned ostart = start;385386/* There are two bits per send buffer (busy and generation) */387start *= 2;388end = start + len * 2;389390spin_lock_irqsave(&dd->pioavail_lock, flags);391/* Set or clear the busy bit in the shadow. */392while (start < end) {393if (avail) {394unsigned long dma;395int i;396397/*398* The BUSY bit will never be set, because we disarm399* the user buffers before we hand them back to the400* kernel. We do have to make sure the generation401* bit is set correctly in shadow, since it could402* have changed many times while allocated to user.403* We can't use the bitmap functions on the full404* dma array because it is always little-endian, so405* we have to flip to host-order first.406* BITS_PER_LONG is slightly wrong, since it's407* always 64 bits per register in chip...408* We only work on 64 bit kernels, so that's OK.409*/410i = start / BITS_PER_LONG;411__clear_bit(QLOGIC_IB_SENDPIOAVAIL_BUSY_SHIFT + start,412dd->pioavailshadow);413dma = (unsigned long)414le64_to_cpu(dd->pioavailregs_dma[i]);415if (test_bit((QLOGIC_IB_SENDPIOAVAIL_CHECK_SHIFT +416start) % BITS_PER_LONG, &dma))417__set_bit(QLOGIC_IB_SENDPIOAVAIL_CHECK_SHIFT +418start, dd->pioavailshadow);419else420__clear_bit(QLOGIC_IB_SENDPIOAVAIL_CHECK_SHIFT421+ start, dd->pioavailshadow);422__set_bit(start, dd->pioavailkernel);423} else {424__set_bit(start + QLOGIC_IB_SENDPIOAVAIL_BUSY_SHIFT,425dd->pioavailshadow);426__clear_bit(start, dd->pioavailkernel);427}428start += 2;429}430431spin_unlock_irqrestore(&dd->pioavail_lock, flags);432433dd->f_txchk_change(dd, ostart, len, avail, rcd);434}435436/*437* Flush all sends that might be in the ready to send state, as well as any438* that are in the process of being sent. Used whenever we need to be439* sure the send side is idle. Cleans up all buffer state by canceling440* all pio buffers, and issuing an abort, which cleans up anything in the441* launch fifo. The cancel is superfluous on some chip versions, but442* it's safer to always do it.443* PIOAvail bits are updated by the chip as if a normal send had happened.444*/445void qib_cancel_sends(struct qib_pportdata *ppd)446{447struct qib_devdata *dd = ppd->dd;448struct qib_ctxtdata *rcd;449unsigned long flags;450unsigned ctxt;451unsigned i;452unsigned last;453454/*455* Tell PSM to disarm buffers again before trying to reuse them.456* We need to be sure the rcd doesn't change out from under us457* while we do so. We hold the two locks sequentially. We might458* needlessly set some need_disarm bits as a result, if the459* context is closed after we release the uctxt_lock, but that's460* fairly benign, and safer than nesting the locks.461*/462for (ctxt = dd->first_user_ctxt; ctxt < dd->cfgctxts; ctxt++) {463spin_lock_irqsave(&dd->uctxt_lock, flags);464rcd = dd->rcd[ctxt];465if (rcd && rcd->ppd == ppd) {466last = rcd->pio_base + rcd->piocnt;467if (rcd->user_event_mask) {468/*469* subctxt_cnt is 0 if not shared, so do base470* separately, first, then remaining subctxt,471* if any472*/473set_bit(_QIB_EVENT_DISARM_BUFS_BIT,474&rcd->user_event_mask[0]);475for (i = 1; i < rcd->subctxt_cnt; i++)476set_bit(_QIB_EVENT_DISARM_BUFS_BIT,477&rcd->user_event_mask[i]);478}479i = rcd->pio_base;480spin_unlock_irqrestore(&dd->uctxt_lock, flags);481spin_lock_irqsave(&dd->pioavail_lock, flags);482for (; i < last; i++)483__set_bit(i, dd->pio_need_disarm);484spin_unlock_irqrestore(&dd->pioavail_lock, flags);485} else486spin_unlock_irqrestore(&dd->uctxt_lock, flags);487}488489if (!(dd->flags & QIB_HAS_SEND_DMA))490dd->f_sendctrl(ppd, QIB_SENDCTRL_DISARM_ALL |491QIB_SENDCTRL_FLUSH);492}493494/*495* Force an update of in-memory copy of the pioavail registers, when496* needed for any of a variety of reasons.497* If already off, this routine is a nop, on the assumption that the498* caller (or set of callers) will "do the right thing".499* This is a per-device operation, so just the first port.500*/501void qib_force_pio_avail_update(struct qib_devdata *dd)502{503dd->f_sendctrl(dd->pport, QIB_SENDCTRL_AVAIL_BLIP);504}505506void qib_hol_down(struct qib_pportdata *ppd)507{508/*509* Cancel sends when the link goes DOWN so that we aren't doing it510* at INIT when we might be trying to send SMI packets.511*/512if (!(ppd->lflags & QIBL_IB_AUTONEG_INPROG))513qib_cancel_sends(ppd);514}515516/*517* Link is at INIT.518* We start the HoL timer so we can detect stuck packets blocking SMP replies.519* Timer may already be running, so use mod_timer, not add_timer.520*/521void qib_hol_init(struct qib_pportdata *ppd)522{523if (ppd->hol_state != QIB_HOL_INIT) {524ppd->hol_state = QIB_HOL_INIT;525mod_timer(&ppd->hol_timer,526jiffies + msecs_to_jiffies(qib_hol_timeout_ms));527}528}529530/*531* Link is up, continue any user processes, and ensure timer532* is a nop, if running. Let timer keep running, if set; it533* will nop when it sees the link is up.534*/535void qib_hol_up(struct qib_pportdata *ppd)536{537ppd->hol_state = QIB_HOL_UP;538}539540/*541* This is only called via the timer.542*/543void qib_hol_event(unsigned long opaque)544{545struct qib_pportdata *ppd = (struct qib_pportdata *)opaque;546547/* If hardware error, etc, skip. */548if (!(ppd->dd->flags & QIB_INITTED))549return;550551if (ppd->hol_state != QIB_HOL_UP) {552/*553* Try to flush sends in case a stuck packet is blocking554* SMP replies.555*/556qib_hol_down(ppd);557mod_timer(&ppd->hol_timer,558jiffies + msecs_to_jiffies(qib_hol_timeout_ms));559}560}561562563