162306a36Sopenharmony_ci/*
262306a36Sopenharmony_ci * Copyright (c) 2008, 2009, 2010 QLogic Corporation. All rights reserved.
362306a36Sopenharmony_ci *
462306a36Sopenharmony_ci * This software is available to you under a choice of one of two
562306a36Sopenharmony_ci * licenses.  You may choose to be licensed under the terms of the GNU
662306a36Sopenharmony_ci * General Public License (GPL) Version 2, available from the file
762306a36Sopenharmony_ci * COPYING in the main directory of this source tree, or the
862306a36Sopenharmony_ci * OpenIB.org BSD license below:
962306a36Sopenharmony_ci *
1062306a36Sopenharmony_ci *     Redistribution and use in source and binary forms, with or
1162306a36Sopenharmony_ci *     without modification, are permitted provided that the following
1262306a36Sopenharmony_ci *     conditions are met:
1362306a36Sopenharmony_ci *
1462306a36Sopenharmony_ci *      - Redistributions of source code must retain the above
1562306a36Sopenharmony_ci *        copyright notice, this list of conditions and the following
1662306a36Sopenharmony_ci *        disclaimer.
1762306a36Sopenharmony_ci *
1862306a36Sopenharmony_ci *      - Redistributions in binary form must reproduce the above
1962306a36Sopenharmony_ci *        copyright notice, this list of conditions and the following
2062306a36Sopenharmony_ci *        disclaimer in the documentation and/or other materials
2162306a36Sopenharmony_ci *        provided with the distribution.
2262306a36Sopenharmony_ci *
2362306a36Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
2462306a36Sopenharmony_ci * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
2562306a36Sopenharmony_ci * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
2662306a36Sopenharmony_ci * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
2762306a36Sopenharmony_ci * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
2862306a36Sopenharmony_ci * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
2962306a36Sopenharmony_ci * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
3062306a36Sopenharmony_ci * SOFTWARE.
3162306a36Sopenharmony_ci */
3262306a36Sopenharmony_ci
3362306a36Sopenharmony_ci#include <linux/spinlock.h>
3462306a36Sopenharmony_ci#include <linux/pci.h>
3562306a36Sopenharmony_ci#include <linux/io.h>
3662306a36Sopenharmony_ci#include <linux/delay.h>
3762306a36Sopenharmony_ci#include <linux/netdevice.h>
3862306a36Sopenharmony_ci#include <linux/vmalloc.h>
3962306a36Sopenharmony_ci#include <linux/moduleparam.h>
4062306a36Sopenharmony_ci
4162306a36Sopenharmony_ci#include "qib.h"
4262306a36Sopenharmony_ci
4362306a36Sopenharmony_cistatic unsigned qib_hol_timeout_ms = 3000;
4462306a36Sopenharmony_cimodule_param_named(hol_timeout_ms, qib_hol_timeout_ms, uint, S_IRUGO);
4562306a36Sopenharmony_ciMODULE_PARM_DESC(hol_timeout_ms,
4662306a36Sopenharmony_ci		 "duration of user app suspension after link failure");
4762306a36Sopenharmony_ci
4862306a36Sopenharmony_ciunsigned qib_sdma_fetch_arb = 1;
4962306a36Sopenharmony_cimodule_param_named(fetch_arb, qib_sdma_fetch_arb, uint, S_IRUGO);
5062306a36Sopenharmony_ciMODULE_PARM_DESC(fetch_arb, "IBA7220: change SDMA descriptor arbitration");
5162306a36Sopenharmony_ci
5262306a36Sopenharmony_ci/**
5362306a36Sopenharmony_ci * qib_disarm_piobufs - cancel a range of PIO buffers
5462306a36Sopenharmony_ci * @dd: the qlogic_ib device
5562306a36Sopenharmony_ci * @first: the first PIO buffer to cancel
5662306a36Sopenharmony_ci * @cnt: the number of PIO buffers to cancel
5762306a36Sopenharmony_ci *
5862306a36Sopenharmony_ci * Cancel a range of PIO buffers. Used at user process close,
5962306a36Sopenharmony_ci * in case it died while writing to a PIO buffer.
6062306a36Sopenharmony_ci */
6162306a36Sopenharmony_civoid qib_disarm_piobufs(struct qib_devdata *dd, unsigned first, unsigned cnt)
6262306a36Sopenharmony_ci{
6362306a36Sopenharmony_ci	unsigned long flags;
6462306a36Sopenharmony_ci	unsigned i;
6562306a36Sopenharmony_ci	unsigned last;
6662306a36Sopenharmony_ci
6762306a36Sopenharmony_ci	last = first + cnt;
6862306a36Sopenharmony_ci	spin_lock_irqsave(&dd->pioavail_lock, flags);
6962306a36Sopenharmony_ci	for (i = first; i < last; i++) {
7062306a36Sopenharmony_ci		__clear_bit(i, dd->pio_need_disarm);
7162306a36Sopenharmony_ci		dd->f_sendctrl(dd->pport, QIB_SENDCTRL_DISARM_BUF(i));
7262306a36Sopenharmony_ci	}
7362306a36Sopenharmony_ci	spin_unlock_irqrestore(&dd->pioavail_lock, flags);
7462306a36Sopenharmony_ci}
7562306a36Sopenharmony_ci
7662306a36Sopenharmony_ci/*
7762306a36Sopenharmony_ci * This is called by a user process when it sees the DISARM_BUFS event
7862306a36Sopenharmony_ci * bit is set.
7962306a36Sopenharmony_ci */
8062306a36Sopenharmony_ciint qib_disarm_piobufs_ifneeded(struct qib_ctxtdata *rcd)
8162306a36Sopenharmony_ci{
8262306a36Sopenharmony_ci	struct qib_devdata *dd = rcd->dd;
8362306a36Sopenharmony_ci	unsigned i;
8462306a36Sopenharmony_ci	unsigned last;
8562306a36Sopenharmony_ci
8662306a36Sopenharmony_ci	last = rcd->pio_base + rcd->piocnt;
8762306a36Sopenharmony_ci	/*
8862306a36Sopenharmony_ci	 * Don't need uctxt_lock here, since user has called in to us.
8962306a36Sopenharmony_ci	 * Clear at start in case more interrupts set bits while we
9062306a36Sopenharmony_ci	 * are disarming
9162306a36Sopenharmony_ci	 */
9262306a36Sopenharmony_ci	if (rcd->user_event_mask) {
9362306a36Sopenharmony_ci		/*
9462306a36Sopenharmony_ci		 * subctxt_cnt is 0 if not shared, so do base
9562306a36Sopenharmony_ci		 * separately, first, then remaining subctxt, if any
9662306a36Sopenharmony_ci		 */
9762306a36Sopenharmony_ci		clear_bit(_QIB_EVENT_DISARM_BUFS_BIT, &rcd->user_event_mask[0]);
9862306a36Sopenharmony_ci		for (i = 1; i < rcd->subctxt_cnt; i++)
9962306a36Sopenharmony_ci			clear_bit(_QIB_EVENT_DISARM_BUFS_BIT,
10062306a36Sopenharmony_ci				  &rcd->user_event_mask[i]);
10162306a36Sopenharmony_ci	}
10262306a36Sopenharmony_ci	spin_lock_irq(&dd->pioavail_lock);
10362306a36Sopenharmony_ci	for (i = rcd->pio_base; i < last; i++) {
10462306a36Sopenharmony_ci		if (__test_and_clear_bit(i, dd->pio_need_disarm))
10562306a36Sopenharmony_ci			dd->f_sendctrl(rcd->ppd, QIB_SENDCTRL_DISARM_BUF(i));
10662306a36Sopenharmony_ci	}
10762306a36Sopenharmony_ci	spin_unlock_irq(&dd->pioavail_lock);
10862306a36Sopenharmony_ci	return 0;
10962306a36Sopenharmony_ci}
11062306a36Sopenharmony_ci
11162306a36Sopenharmony_cistatic struct qib_pportdata *is_sdma_buf(struct qib_devdata *dd, unsigned i)
11262306a36Sopenharmony_ci{
11362306a36Sopenharmony_ci	struct qib_pportdata *ppd;
11462306a36Sopenharmony_ci	unsigned pidx;
11562306a36Sopenharmony_ci
11662306a36Sopenharmony_ci	for (pidx = 0; pidx < dd->num_pports; pidx++) {
11762306a36Sopenharmony_ci		ppd = dd->pport + pidx;
11862306a36Sopenharmony_ci		if (i >= ppd->sdma_state.first_sendbuf &&
11962306a36Sopenharmony_ci		    i < ppd->sdma_state.last_sendbuf)
12062306a36Sopenharmony_ci			return ppd;
12162306a36Sopenharmony_ci	}
12262306a36Sopenharmony_ci	return NULL;
12362306a36Sopenharmony_ci}
12462306a36Sopenharmony_ci
12562306a36Sopenharmony_ci/*
12662306a36Sopenharmony_ci * Return true if send buffer is being used by a user context.
12762306a36Sopenharmony_ci * Sets  _QIB_EVENT_DISARM_BUFS_BIT in user_event_mask as a side effect
12862306a36Sopenharmony_ci */
12962306a36Sopenharmony_cistatic int find_ctxt(struct qib_devdata *dd, unsigned bufn)
13062306a36Sopenharmony_ci{
13162306a36Sopenharmony_ci	struct qib_ctxtdata *rcd;
13262306a36Sopenharmony_ci	unsigned ctxt;
13362306a36Sopenharmony_ci	int ret = 0;
13462306a36Sopenharmony_ci
13562306a36Sopenharmony_ci	spin_lock(&dd->uctxt_lock);
13662306a36Sopenharmony_ci	for (ctxt = dd->first_user_ctxt; ctxt < dd->cfgctxts; ctxt++) {
13762306a36Sopenharmony_ci		rcd = dd->rcd[ctxt];
13862306a36Sopenharmony_ci		if (!rcd || bufn < rcd->pio_base ||
13962306a36Sopenharmony_ci		    bufn >= rcd->pio_base + rcd->piocnt)
14062306a36Sopenharmony_ci			continue;
14162306a36Sopenharmony_ci		if (rcd->user_event_mask) {
14262306a36Sopenharmony_ci			int i;
14362306a36Sopenharmony_ci			/*
14462306a36Sopenharmony_ci			 * subctxt_cnt is 0 if not shared, so do base
14562306a36Sopenharmony_ci			 * separately, first, then remaining subctxt, if any
14662306a36Sopenharmony_ci			 */
14762306a36Sopenharmony_ci			set_bit(_QIB_EVENT_DISARM_BUFS_BIT,
14862306a36Sopenharmony_ci				&rcd->user_event_mask[0]);
14962306a36Sopenharmony_ci			for (i = 1; i < rcd->subctxt_cnt; i++)
15062306a36Sopenharmony_ci				set_bit(_QIB_EVENT_DISARM_BUFS_BIT,
15162306a36Sopenharmony_ci					&rcd->user_event_mask[i]);
15262306a36Sopenharmony_ci		}
15362306a36Sopenharmony_ci		ret = 1;
15462306a36Sopenharmony_ci		break;
15562306a36Sopenharmony_ci	}
15662306a36Sopenharmony_ci	spin_unlock(&dd->uctxt_lock);
15762306a36Sopenharmony_ci
15862306a36Sopenharmony_ci	return ret;
15962306a36Sopenharmony_ci}
16062306a36Sopenharmony_ci
16162306a36Sopenharmony_ci/*
16262306a36Sopenharmony_ci * Disarm a set of send buffers.  If the buffer might be actively being
16362306a36Sopenharmony_ci * written to, mark the buffer to be disarmed later when it is not being
16462306a36Sopenharmony_ci * written to.
16562306a36Sopenharmony_ci *
16662306a36Sopenharmony_ci * This should only be called from the IRQ error handler.
16762306a36Sopenharmony_ci */
16862306a36Sopenharmony_civoid qib_disarm_piobufs_set(struct qib_devdata *dd, unsigned long *mask,
16962306a36Sopenharmony_ci			    unsigned cnt)
17062306a36Sopenharmony_ci{
17162306a36Sopenharmony_ci	struct qib_pportdata *ppd, *pppd[QIB_MAX_IB_PORTS];
17262306a36Sopenharmony_ci	unsigned i;
17362306a36Sopenharmony_ci	unsigned long flags;
17462306a36Sopenharmony_ci
17562306a36Sopenharmony_ci	for (i = 0; i < dd->num_pports; i++)
17662306a36Sopenharmony_ci		pppd[i] = NULL;
17762306a36Sopenharmony_ci
17862306a36Sopenharmony_ci	for (i = 0; i < cnt; i++) {
17962306a36Sopenharmony_ci		if (!test_bit(i, mask))
18062306a36Sopenharmony_ci			continue;
18162306a36Sopenharmony_ci		/*
18262306a36Sopenharmony_ci		 * If the buffer is owned by the DMA hardware,
18362306a36Sopenharmony_ci		 * reset the DMA engine.
18462306a36Sopenharmony_ci		 */
18562306a36Sopenharmony_ci		ppd = is_sdma_buf(dd, i);
18662306a36Sopenharmony_ci		if (ppd) {
18762306a36Sopenharmony_ci			pppd[ppd->port] = ppd;
18862306a36Sopenharmony_ci			continue;
18962306a36Sopenharmony_ci		}
19062306a36Sopenharmony_ci		/*
19162306a36Sopenharmony_ci		 * If the kernel is writing the buffer or the buffer is
19262306a36Sopenharmony_ci		 * owned by a user process, we can't clear it yet.
19362306a36Sopenharmony_ci		 */
19462306a36Sopenharmony_ci		spin_lock_irqsave(&dd->pioavail_lock, flags);
19562306a36Sopenharmony_ci		if (test_bit(i, dd->pio_writing) ||
19662306a36Sopenharmony_ci		    (!test_bit(i << 1, dd->pioavailkernel) &&
19762306a36Sopenharmony_ci		     find_ctxt(dd, i))) {
19862306a36Sopenharmony_ci			__set_bit(i, dd->pio_need_disarm);
19962306a36Sopenharmony_ci		} else {
20062306a36Sopenharmony_ci			dd->f_sendctrl(dd->pport, QIB_SENDCTRL_DISARM_BUF(i));
20162306a36Sopenharmony_ci		}
20262306a36Sopenharmony_ci		spin_unlock_irqrestore(&dd->pioavail_lock, flags);
20362306a36Sopenharmony_ci	}
20462306a36Sopenharmony_ci
20562306a36Sopenharmony_ci	/* do cancel_sends once per port that had sdma piobufs in error */
20662306a36Sopenharmony_ci	for (i = 0; i < dd->num_pports; i++)
20762306a36Sopenharmony_ci		if (pppd[i])
20862306a36Sopenharmony_ci			qib_cancel_sends(pppd[i]);
20962306a36Sopenharmony_ci}
21062306a36Sopenharmony_ci
21162306a36Sopenharmony_ci/**
21262306a36Sopenharmony_ci * update_send_bufs - update shadow copy of the PIO availability map
21362306a36Sopenharmony_ci * @dd: the qlogic_ib device
21462306a36Sopenharmony_ci *
21562306a36Sopenharmony_ci * called whenever our local copy indicates we have run out of send buffers
21662306a36Sopenharmony_ci */
21762306a36Sopenharmony_cistatic void update_send_bufs(struct qib_devdata *dd)
21862306a36Sopenharmony_ci{
21962306a36Sopenharmony_ci	unsigned long flags;
22062306a36Sopenharmony_ci	unsigned i;
22162306a36Sopenharmony_ci	const unsigned piobregs = dd->pioavregs;
22262306a36Sopenharmony_ci
22362306a36Sopenharmony_ci	/*
22462306a36Sopenharmony_ci	 * If the generation (check) bits have changed, then we update the
22562306a36Sopenharmony_ci	 * busy bit for the corresponding PIO buffer.  This algorithm will
22662306a36Sopenharmony_ci	 * modify positions to the value they already have in some cases
22762306a36Sopenharmony_ci	 * (i.e., no change), but it's faster than changing only the bits
22862306a36Sopenharmony_ci	 * that have changed.
22962306a36Sopenharmony_ci	 *
23062306a36Sopenharmony_ci	 * We would like to do this atomicly, to avoid spinlocks in the
23162306a36Sopenharmony_ci	 * critical send path, but that's not really possible, given the
23262306a36Sopenharmony_ci	 * type of changes, and that this routine could be called on
23362306a36Sopenharmony_ci	 * multiple cpu's simultaneously, so we lock in this routine only,
23462306a36Sopenharmony_ci	 * to avoid conflicting updates; all we change is the shadow, and
23562306a36Sopenharmony_ci	 * it's a single 64 bit memory location, so by definition the update
23662306a36Sopenharmony_ci	 * is atomic in terms of what other cpu's can see in testing the
23762306a36Sopenharmony_ci	 * bits.  The spin_lock overhead isn't too bad, since it only
23862306a36Sopenharmony_ci	 * happens when all buffers are in use, so only cpu overhead, not
23962306a36Sopenharmony_ci	 * latency or bandwidth is affected.
24062306a36Sopenharmony_ci	 */
24162306a36Sopenharmony_ci	if (!dd->pioavailregs_dma)
24262306a36Sopenharmony_ci		return;
24362306a36Sopenharmony_ci	spin_lock_irqsave(&dd->pioavail_lock, flags);
24462306a36Sopenharmony_ci	for (i = 0; i < piobregs; i++) {
24562306a36Sopenharmony_ci		u64 pchbusy, pchg, piov, pnew;
24662306a36Sopenharmony_ci
24762306a36Sopenharmony_ci		piov = le64_to_cpu(dd->pioavailregs_dma[i]);
24862306a36Sopenharmony_ci		pchg = dd->pioavailkernel[i] &
24962306a36Sopenharmony_ci			~(dd->pioavailshadow[i] ^ piov);
25062306a36Sopenharmony_ci		pchbusy = pchg << QLOGIC_IB_SENDPIOAVAIL_BUSY_SHIFT;
25162306a36Sopenharmony_ci		if (pchg && (pchbusy & dd->pioavailshadow[i])) {
25262306a36Sopenharmony_ci			pnew = dd->pioavailshadow[i] & ~pchbusy;
25362306a36Sopenharmony_ci			pnew |= piov & pchbusy;
25462306a36Sopenharmony_ci			dd->pioavailshadow[i] = pnew;
25562306a36Sopenharmony_ci		}
25662306a36Sopenharmony_ci	}
25762306a36Sopenharmony_ci	spin_unlock_irqrestore(&dd->pioavail_lock, flags);
25862306a36Sopenharmony_ci}
25962306a36Sopenharmony_ci
26062306a36Sopenharmony_ci/*
26162306a36Sopenharmony_ci * Debugging code and stats updates if no pio buffers available.
26262306a36Sopenharmony_ci */
26362306a36Sopenharmony_cistatic noinline void no_send_bufs(struct qib_devdata *dd)
26462306a36Sopenharmony_ci{
26562306a36Sopenharmony_ci	dd->upd_pio_shadow = 1;
26662306a36Sopenharmony_ci
26762306a36Sopenharmony_ci	/* not atomic, but if we lose a stat count in a while, that's OK */
26862306a36Sopenharmony_ci	qib_stats.sps_nopiobufs++;
26962306a36Sopenharmony_ci}
27062306a36Sopenharmony_ci
27162306a36Sopenharmony_ci/*
27262306a36Sopenharmony_ci * Common code for normal driver send buffer allocation, and reserved
27362306a36Sopenharmony_ci * allocation.
27462306a36Sopenharmony_ci *
27562306a36Sopenharmony_ci * Do appropriate marking as busy, etc.
27662306a36Sopenharmony_ci * Returns buffer pointer if one is found, otherwise NULL.
27762306a36Sopenharmony_ci */
27862306a36Sopenharmony_ciu32 __iomem *qib_getsendbuf_range(struct qib_devdata *dd, u32 *pbufnum,
27962306a36Sopenharmony_ci				  u32 first, u32 last)
28062306a36Sopenharmony_ci{
28162306a36Sopenharmony_ci	unsigned i, j, updated = 0;
28262306a36Sopenharmony_ci	unsigned nbufs;
28362306a36Sopenharmony_ci	unsigned long flags;
28462306a36Sopenharmony_ci	unsigned long *shadow = dd->pioavailshadow;
28562306a36Sopenharmony_ci	u32 __iomem *buf;
28662306a36Sopenharmony_ci
28762306a36Sopenharmony_ci	if (!(dd->flags & QIB_PRESENT))
28862306a36Sopenharmony_ci		return NULL;
28962306a36Sopenharmony_ci
29062306a36Sopenharmony_ci	nbufs = last - first + 1; /* number in range to check */
29162306a36Sopenharmony_ci	if (dd->upd_pio_shadow) {
29262306a36Sopenharmony_ciupdate_shadow:
29362306a36Sopenharmony_ci		/*
29462306a36Sopenharmony_ci		 * Minor optimization.  If we had no buffers on last call,
29562306a36Sopenharmony_ci		 * start out by doing the update; continue and do scan even
29662306a36Sopenharmony_ci		 * if no buffers were updated, to be paranoid.
29762306a36Sopenharmony_ci		 */
29862306a36Sopenharmony_ci		update_send_bufs(dd);
29962306a36Sopenharmony_ci		updated++;
30062306a36Sopenharmony_ci	}
30162306a36Sopenharmony_ci	i = first;
30262306a36Sopenharmony_ci	/*
30362306a36Sopenharmony_ci	 * While test_and_set_bit() is atomic, we do that and then the
30462306a36Sopenharmony_ci	 * change_bit(), and the pair is not.  See if this is the cause
30562306a36Sopenharmony_ci	 * of the remaining armlaunch errors.
30662306a36Sopenharmony_ci	 */
30762306a36Sopenharmony_ci	spin_lock_irqsave(&dd->pioavail_lock, flags);
30862306a36Sopenharmony_ci	if (dd->last_pio >= first && dd->last_pio <= last)
30962306a36Sopenharmony_ci		i = dd->last_pio + 1;
31062306a36Sopenharmony_ci	if (!first)
31162306a36Sopenharmony_ci		/* adjust to min possible  */
31262306a36Sopenharmony_ci		nbufs = last - dd->min_kernel_pio + 1;
31362306a36Sopenharmony_ci	for (j = 0; j < nbufs; j++, i++) {
31462306a36Sopenharmony_ci		if (i > last)
31562306a36Sopenharmony_ci			i = !first ? dd->min_kernel_pio : first;
31662306a36Sopenharmony_ci		if (__test_and_set_bit((2 * i) + 1, shadow))
31762306a36Sopenharmony_ci			continue;
31862306a36Sopenharmony_ci		/* flip generation bit */
31962306a36Sopenharmony_ci		__change_bit(2 * i, shadow);
32062306a36Sopenharmony_ci		/* remember that the buffer can be written to now */
32162306a36Sopenharmony_ci		__set_bit(i, dd->pio_writing);
32262306a36Sopenharmony_ci		if (!first && first != last) /* first == last on VL15, avoid */
32362306a36Sopenharmony_ci			dd->last_pio = i;
32462306a36Sopenharmony_ci		break;
32562306a36Sopenharmony_ci	}
32662306a36Sopenharmony_ci	spin_unlock_irqrestore(&dd->pioavail_lock, flags);
32762306a36Sopenharmony_ci
32862306a36Sopenharmony_ci	if (j == nbufs) {
32962306a36Sopenharmony_ci		if (!updated)
33062306a36Sopenharmony_ci			/*
33162306a36Sopenharmony_ci			 * First time through; shadow exhausted, but may be
33262306a36Sopenharmony_ci			 * buffers available, try an update and then rescan.
33362306a36Sopenharmony_ci			 */
33462306a36Sopenharmony_ci			goto update_shadow;
33562306a36Sopenharmony_ci		no_send_bufs(dd);
33662306a36Sopenharmony_ci		buf = NULL;
33762306a36Sopenharmony_ci	} else {
33862306a36Sopenharmony_ci		if (i < dd->piobcnt2k)
33962306a36Sopenharmony_ci			buf = (u32 __iomem *)(dd->pio2kbase +
34062306a36Sopenharmony_ci				i * dd->palign);
34162306a36Sopenharmony_ci		else if (i < dd->piobcnt2k + dd->piobcnt4k || !dd->piovl15base)
34262306a36Sopenharmony_ci			buf = (u32 __iomem *)(dd->pio4kbase +
34362306a36Sopenharmony_ci				(i - dd->piobcnt2k) * dd->align4k);
34462306a36Sopenharmony_ci		else
34562306a36Sopenharmony_ci			buf = (u32 __iomem *)(dd->piovl15base +
34662306a36Sopenharmony_ci				(i - (dd->piobcnt2k + dd->piobcnt4k)) *
34762306a36Sopenharmony_ci				dd->align4k);
34862306a36Sopenharmony_ci		if (pbufnum)
34962306a36Sopenharmony_ci			*pbufnum = i;
35062306a36Sopenharmony_ci		dd->upd_pio_shadow = 0;
35162306a36Sopenharmony_ci	}
35262306a36Sopenharmony_ci
35362306a36Sopenharmony_ci	return buf;
35462306a36Sopenharmony_ci}
35562306a36Sopenharmony_ci
35662306a36Sopenharmony_ci/*
35762306a36Sopenharmony_ci * Record that the caller is finished writing to the buffer so we don't
35862306a36Sopenharmony_ci * disarm it while it is being written and disarm it now if needed.
35962306a36Sopenharmony_ci */
36062306a36Sopenharmony_civoid qib_sendbuf_done(struct qib_devdata *dd, unsigned n)
36162306a36Sopenharmony_ci{
36262306a36Sopenharmony_ci	unsigned long flags;
36362306a36Sopenharmony_ci
36462306a36Sopenharmony_ci	spin_lock_irqsave(&dd->pioavail_lock, flags);
36562306a36Sopenharmony_ci	__clear_bit(n, dd->pio_writing);
36662306a36Sopenharmony_ci	if (__test_and_clear_bit(n, dd->pio_need_disarm))
36762306a36Sopenharmony_ci		dd->f_sendctrl(dd->pport, QIB_SENDCTRL_DISARM_BUF(n));
36862306a36Sopenharmony_ci	spin_unlock_irqrestore(&dd->pioavail_lock, flags);
36962306a36Sopenharmony_ci}
37062306a36Sopenharmony_ci
37162306a36Sopenharmony_ci/**
37262306a36Sopenharmony_ci * qib_chg_pioavailkernel - change which send buffers are available for kernel
37362306a36Sopenharmony_ci * @dd: the qlogic_ib device
37462306a36Sopenharmony_ci * @start: the starting send buffer number
37562306a36Sopenharmony_ci * @len: the number of send buffers
37662306a36Sopenharmony_ci * @avail: true if the buffers are available for kernel use, false otherwise
37762306a36Sopenharmony_ci * @rcd: the context pointer
37862306a36Sopenharmony_ci */
37962306a36Sopenharmony_civoid qib_chg_pioavailkernel(struct qib_devdata *dd, unsigned start,
38062306a36Sopenharmony_ci	unsigned len, u32 avail, struct qib_ctxtdata *rcd)
38162306a36Sopenharmony_ci{
38262306a36Sopenharmony_ci	unsigned long flags;
38362306a36Sopenharmony_ci	unsigned end;
38462306a36Sopenharmony_ci	unsigned ostart = start;
38562306a36Sopenharmony_ci
38662306a36Sopenharmony_ci	/* There are two bits per send buffer (busy and generation) */
38762306a36Sopenharmony_ci	start *= 2;
38862306a36Sopenharmony_ci	end = start + len * 2;
38962306a36Sopenharmony_ci
39062306a36Sopenharmony_ci	spin_lock_irqsave(&dd->pioavail_lock, flags);
39162306a36Sopenharmony_ci	/* Set or clear the busy bit in the shadow. */
39262306a36Sopenharmony_ci	while (start < end) {
39362306a36Sopenharmony_ci		if (avail) {
39462306a36Sopenharmony_ci			unsigned long dma;
39562306a36Sopenharmony_ci			int i;
39662306a36Sopenharmony_ci
39762306a36Sopenharmony_ci			/*
39862306a36Sopenharmony_ci			 * The BUSY bit will never be set, because we disarm
39962306a36Sopenharmony_ci			 * the user buffers before we hand them back to the
40062306a36Sopenharmony_ci			 * kernel.  We do have to make sure the generation
40162306a36Sopenharmony_ci			 * bit is set correctly in shadow, since it could
40262306a36Sopenharmony_ci			 * have changed many times while allocated to user.
40362306a36Sopenharmony_ci			 * We can't use the bitmap functions on the full
40462306a36Sopenharmony_ci			 * dma array because it is always little-endian, so
40562306a36Sopenharmony_ci			 * we have to flip to host-order first.
40662306a36Sopenharmony_ci			 * BITS_PER_LONG is slightly wrong, since it's
40762306a36Sopenharmony_ci			 * always 64 bits per register in chip...
40862306a36Sopenharmony_ci			 * We only work on 64 bit kernels, so that's OK.
40962306a36Sopenharmony_ci			 */
41062306a36Sopenharmony_ci			i = start / BITS_PER_LONG;
41162306a36Sopenharmony_ci			__clear_bit(QLOGIC_IB_SENDPIOAVAIL_BUSY_SHIFT + start,
41262306a36Sopenharmony_ci				    dd->pioavailshadow);
41362306a36Sopenharmony_ci			dma = (unsigned long)
41462306a36Sopenharmony_ci				le64_to_cpu(dd->pioavailregs_dma[i]);
41562306a36Sopenharmony_ci			if (test_bit((QLOGIC_IB_SENDPIOAVAIL_CHECK_SHIFT +
41662306a36Sopenharmony_ci				      start) % BITS_PER_LONG, &dma))
41762306a36Sopenharmony_ci				__set_bit(QLOGIC_IB_SENDPIOAVAIL_CHECK_SHIFT +
41862306a36Sopenharmony_ci					  start, dd->pioavailshadow);
41962306a36Sopenharmony_ci			else
42062306a36Sopenharmony_ci				__clear_bit(QLOGIC_IB_SENDPIOAVAIL_CHECK_SHIFT
42162306a36Sopenharmony_ci					    + start, dd->pioavailshadow);
42262306a36Sopenharmony_ci			__set_bit(start, dd->pioavailkernel);
42362306a36Sopenharmony_ci			if ((start >> 1) < dd->min_kernel_pio)
42462306a36Sopenharmony_ci				dd->min_kernel_pio = start >> 1;
42562306a36Sopenharmony_ci		} else {
42662306a36Sopenharmony_ci			__set_bit(start + QLOGIC_IB_SENDPIOAVAIL_BUSY_SHIFT,
42762306a36Sopenharmony_ci				  dd->pioavailshadow);
42862306a36Sopenharmony_ci			__clear_bit(start, dd->pioavailkernel);
42962306a36Sopenharmony_ci			if ((start >> 1) > dd->min_kernel_pio)
43062306a36Sopenharmony_ci				dd->min_kernel_pio = start >> 1;
43162306a36Sopenharmony_ci		}
43262306a36Sopenharmony_ci		start += 2;
43362306a36Sopenharmony_ci	}
43462306a36Sopenharmony_ci
43562306a36Sopenharmony_ci	if (dd->min_kernel_pio > 0 && dd->last_pio < dd->min_kernel_pio - 1)
43662306a36Sopenharmony_ci		dd->last_pio = dd->min_kernel_pio - 1;
43762306a36Sopenharmony_ci	spin_unlock_irqrestore(&dd->pioavail_lock, flags);
43862306a36Sopenharmony_ci
43962306a36Sopenharmony_ci	dd->f_txchk_change(dd, ostart, len, avail, rcd);
44062306a36Sopenharmony_ci}
44162306a36Sopenharmony_ci
44262306a36Sopenharmony_ci/*
44362306a36Sopenharmony_ci * Flush all sends that might be in the ready to send state, as well as any
44462306a36Sopenharmony_ci * that are in the process of being sent.  Used whenever we need to be
44562306a36Sopenharmony_ci * sure the send side is idle.  Cleans up all buffer state by canceling
44662306a36Sopenharmony_ci * all pio buffers, and issuing an abort, which cleans up anything in the
44762306a36Sopenharmony_ci * launch fifo.  The cancel is superfluous on some chip versions, but
44862306a36Sopenharmony_ci * it's safer to always do it.
44962306a36Sopenharmony_ci * PIOAvail bits are updated by the chip as if a normal send had happened.
45062306a36Sopenharmony_ci */
45162306a36Sopenharmony_civoid qib_cancel_sends(struct qib_pportdata *ppd)
45262306a36Sopenharmony_ci{
45362306a36Sopenharmony_ci	struct qib_devdata *dd = ppd->dd;
45462306a36Sopenharmony_ci	struct qib_ctxtdata *rcd;
45562306a36Sopenharmony_ci	unsigned long flags;
45662306a36Sopenharmony_ci	unsigned ctxt;
45762306a36Sopenharmony_ci	unsigned i;
45862306a36Sopenharmony_ci	unsigned last;
45962306a36Sopenharmony_ci
46062306a36Sopenharmony_ci	/*
46162306a36Sopenharmony_ci	 * Tell PSM to disarm buffers again before trying to reuse them.
46262306a36Sopenharmony_ci	 * We need to be sure the rcd doesn't change out from under us
46362306a36Sopenharmony_ci	 * while we do so.  We hold the two locks sequentially.  We might
46462306a36Sopenharmony_ci	 * needlessly set some need_disarm bits as a result, if the
46562306a36Sopenharmony_ci	 * context is closed after we release the uctxt_lock, but that's
46662306a36Sopenharmony_ci	 * fairly benign, and safer than nesting the locks.
46762306a36Sopenharmony_ci	 */
46862306a36Sopenharmony_ci	for (ctxt = dd->first_user_ctxt; ctxt < dd->cfgctxts; ctxt++) {
46962306a36Sopenharmony_ci		spin_lock_irqsave(&dd->uctxt_lock, flags);
47062306a36Sopenharmony_ci		rcd = dd->rcd[ctxt];
47162306a36Sopenharmony_ci		if (rcd && rcd->ppd == ppd) {
47262306a36Sopenharmony_ci			last = rcd->pio_base + rcd->piocnt;
47362306a36Sopenharmony_ci			if (rcd->user_event_mask) {
47462306a36Sopenharmony_ci				/*
47562306a36Sopenharmony_ci				 * subctxt_cnt is 0 if not shared, so do base
47662306a36Sopenharmony_ci				 * separately, first, then remaining subctxt,
47762306a36Sopenharmony_ci				 * if any
47862306a36Sopenharmony_ci				 */
47962306a36Sopenharmony_ci				set_bit(_QIB_EVENT_DISARM_BUFS_BIT,
48062306a36Sopenharmony_ci					&rcd->user_event_mask[0]);
48162306a36Sopenharmony_ci				for (i = 1; i < rcd->subctxt_cnt; i++)
48262306a36Sopenharmony_ci					set_bit(_QIB_EVENT_DISARM_BUFS_BIT,
48362306a36Sopenharmony_ci						&rcd->user_event_mask[i]);
48462306a36Sopenharmony_ci			}
48562306a36Sopenharmony_ci			i = rcd->pio_base;
48662306a36Sopenharmony_ci			spin_unlock_irqrestore(&dd->uctxt_lock, flags);
48762306a36Sopenharmony_ci			spin_lock_irqsave(&dd->pioavail_lock, flags);
48862306a36Sopenharmony_ci			for (; i < last; i++)
48962306a36Sopenharmony_ci				__set_bit(i, dd->pio_need_disarm);
49062306a36Sopenharmony_ci			spin_unlock_irqrestore(&dd->pioavail_lock, flags);
49162306a36Sopenharmony_ci		} else
49262306a36Sopenharmony_ci			spin_unlock_irqrestore(&dd->uctxt_lock, flags);
49362306a36Sopenharmony_ci	}
49462306a36Sopenharmony_ci
49562306a36Sopenharmony_ci	if (!(dd->flags & QIB_HAS_SEND_DMA))
49662306a36Sopenharmony_ci		dd->f_sendctrl(ppd, QIB_SENDCTRL_DISARM_ALL |
49762306a36Sopenharmony_ci				    QIB_SENDCTRL_FLUSH);
49862306a36Sopenharmony_ci}
49962306a36Sopenharmony_ci
50062306a36Sopenharmony_ci/*
50162306a36Sopenharmony_ci * Force an update of in-memory copy of the pioavail registers, when
50262306a36Sopenharmony_ci * needed for any of a variety of reasons.
50362306a36Sopenharmony_ci * If already off, this routine is a nop, on the assumption that the
50462306a36Sopenharmony_ci * caller (or set of callers) will "do the right thing".
50562306a36Sopenharmony_ci * This is a per-device operation, so just the first port.
50662306a36Sopenharmony_ci */
50762306a36Sopenharmony_civoid qib_force_pio_avail_update(struct qib_devdata *dd)
50862306a36Sopenharmony_ci{
50962306a36Sopenharmony_ci	dd->f_sendctrl(dd->pport, QIB_SENDCTRL_AVAIL_BLIP);
51062306a36Sopenharmony_ci}
51162306a36Sopenharmony_ci
51262306a36Sopenharmony_civoid qib_hol_down(struct qib_pportdata *ppd)
51362306a36Sopenharmony_ci{
51462306a36Sopenharmony_ci	/*
51562306a36Sopenharmony_ci	 * Cancel sends when the link goes DOWN so that we aren't doing it
51662306a36Sopenharmony_ci	 * at INIT when we might be trying to send SMI packets.
51762306a36Sopenharmony_ci	 */
51862306a36Sopenharmony_ci	if (!(ppd->lflags & QIBL_IB_AUTONEG_INPROG))
51962306a36Sopenharmony_ci		qib_cancel_sends(ppd);
52062306a36Sopenharmony_ci}
52162306a36Sopenharmony_ci
52262306a36Sopenharmony_ci/*
52362306a36Sopenharmony_ci * Link is at INIT.
52462306a36Sopenharmony_ci * We start the HoL timer so we can detect stuck packets blocking SMP replies.
52562306a36Sopenharmony_ci * Timer may already be running, so use mod_timer, not add_timer.
52662306a36Sopenharmony_ci */
52762306a36Sopenharmony_civoid qib_hol_init(struct qib_pportdata *ppd)
52862306a36Sopenharmony_ci{
52962306a36Sopenharmony_ci	if (ppd->hol_state != QIB_HOL_INIT) {
53062306a36Sopenharmony_ci		ppd->hol_state = QIB_HOL_INIT;
53162306a36Sopenharmony_ci		mod_timer(&ppd->hol_timer,
53262306a36Sopenharmony_ci			  jiffies + msecs_to_jiffies(qib_hol_timeout_ms));
53362306a36Sopenharmony_ci	}
53462306a36Sopenharmony_ci}
53562306a36Sopenharmony_ci
53662306a36Sopenharmony_ci/*
53762306a36Sopenharmony_ci * Link is up, continue any user processes, and ensure timer
53862306a36Sopenharmony_ci * is a nop, if running.  Let timer keep running, if set; it
53962306a36Sopenharmony_ci * will nop when it sees the link is up.
54062306a36Sopenharmony_ci */
54162306a36Sopenharmony_civoid qib_hol_up(struct qib_pportdata *ppd)
54262306a36Sopenharmony_ci{
54362306a36Sopenharmony_ci	ppd->hol_state = QIB_HOL_UP;
54462306a36Sopenharmony_ci}
54562306a36Sopenharmony_ci
54662306a36Sopenharmony_ci/*
54762306a36Sopenharmony_ci * This is only called via the timer.
54862306a36Sopenharmony_ci */
54962306a36Sopenharmony_civoid qib_hol_event(struct timer_list *t)
55062306a36Sopenharmony_ci{
55162306a36Sopenharmony_ci	struct qib_pportdata *ppd = from_timer(ppd, t, hol_timer);
55262306a36Sopenharmony_ci
55362306a36Sopenharmony_ci	/* If hardware error, etc, skip. */
55462306a36Sopenharmony_ci	if (!(ppd->dd->flags & QIB_INITTED))
55562306a36Sopenharmony_ci		return;
55662306a36Sopenharmony_ci
55762306a36Sopenharmony_ci	if (ppd->hol_state != QIB_HOL_UP) {
55862306a36Sopenharmony_ci		/*
55962306a36Sopenharmony_ci		 * Try to flush sends in case a stuck packet is blocking
56062306a36Sopenharmony_ci		 * SMP replies.
56162306a36Sopenharmony_ci		 */
56262306a36Sopenharmony_ci		qib_hol_down(ppd);
56362306a36Sopenharmony_ci		mod_timer(&ppd->hol_timer,
56462306a36Sopenharmony_ci			  jiffies + msecs_to_jiffies(qib_hol_timeout_ms));
56562306a36Sopenharmony_ci	}
56662306a36Sopenharmony_ci}
567