18c2ecf20Sopenharmony_ci/*
28c2ecf20Sopenharmony_ci * Copyright (c) 2008, 2009, 2010 QLogic Corporation. All rights reserved.
38c2ecf20Sopenharmony_ci *
48c2ecf20Sopenharmony_ci * This software is available to you under a choice of one of two
58c2ecf20Sopenharmony_ci * licenses.  You may choose to be licensed under the terms of the GNU
68c2ecf20Sopenharmony_ci * General Public License (GPL) Version 2, available from the file
78c2ecf20Sopenharmony_ci * COPYING in the main directory of this source tree, or the
88c2ecf20Sopenharmony_ci * OpenIB.org BSD license below:
98c2ecf20Sopenharmony_ci *
108c2ecf20Sopenharmony_ci *     Redistribution and use in source and binary forms, with or
118c2ecf20Sopenharmony_ci *     without modification, are permitted provided that the following
128c2ecf20Sopenharmony_ci *     conditions are met:
138c2ecf20Sopenharmony_ci *
148c2ecf20Sopenharmony_ci *      - Redistributions of source code must retain the above
158c2ecf20Sopenharmony_ci *        copyright notice, this list of conditions and the following
168c2ecf20Sopenharmony_ci *        disclaimer.
178c2ecf20Sopenharmony_ci *
188c2ecf20Sopenharmony_ci *      - Redistributions in binary form must reproduce the above
198c2ecf20Sopenharmony_ci *        copyright notice, this list of conditions and the following
208c2ecf20Sopenharmony_ci *        disclaimer in the documentation and/or other materials
218c2ecf20Sopenharmony_ci *        provided with the distribution.
228c2ecf20Sopenharmony_ci *
238c2ecf20Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
248c2ecf20Sopenharmony_ci * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
258c2ecf20Sopenharmony_ci * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
268c2ecf20Sopenharmony_ci * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
278c2ecf20Sopenharmony_ci * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
288c2ecf20Sopenharmony_ci * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
298c2ecf20Sopenharmony_ci * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
308c2ecf20Sopenharmony_ci * SOFTWARE.
318c2ecf20Sopenharmony_ci */
328c2ecf20Sopenharmony_ci
338c2ecf20Sopenharmony_ci#include <linux/spinlock.h>
348c2ecf20Sopenharmony_ci#include <linux/pci.h>
358c2ecf20Sopenharmony_ci#include <linux/io.h>
368c2ecf20Sopenharmony_ci#include <linux/delay.h>
378c2ecf20Sopenharmony_ci#include <linux/netdevice.h>
388c2ecf20Sopenharmony_ci#include <linux/vmalloc.h>
398c2ecf20Sopenharmony_ci#include <linux/moduleparam.h>
408c2ecf20Sopenharmony_ci
418c2ecf20Sopenharmony_ci#include "qib.h"
428c2ecf20Sopenharmony_ci
438c2ecf20Sopenharmony_cistatic unsigned qib_hol_timeout_ms = 3000;
448c2ecf20Sopenharmony_cimodule_param_named(hol_timeout_ms, qib_hol_timeout_ms, uint, S_IRUGO);
458c2ecf20Sopenharmony_ciMODULE_PARM_DESC(hol_timeout_ms,
468c2ecf20Sopenharmony_ci		 "duration of user app suspension after link failure");
478c2ecf20Sopenharmony_ci
488c2ecf20Sopenharmony_ciunsigned qib_sdma_fetch_arb = 1;
498c2ecf20Sopenharmony_cimodule_param_named(fetch_arb, qib_sdma_fetch_arb, uint, S_IRUGO);
508c2ecf20Sopenharmony_ciMODULE_PARM_DESC(fetch_arb, "IBA7220: change SDMA descriptor arbitration");
518c2ecf20Sopenharmony_ci
528c2ecf20Sopenharmony_ci/**
538c2ecf20Sopenharmony_ci * qib_disarm_piobufs - cancel a range of PIO buffers
548c2ecf20Sopenharmony_ci * @dd: the qlogic_ib device
558c2ecf20Sopenharmony_ci * @first: the first PIO buffer to cancel
568c2ecf20Sopenharmony_ci * @cnt: the number of PIO buffers to cancel
578c2ecf20Sopenharmony_ci *
588c2ecf20Sopenharmony_ci * Cancel a range of PIO buffers. Used at user process close,
598c2ecf20Sopenharmony_ci * in case it died while writing to a PIO buffer.
608c2ecf20Sopenharmony_ci */
618c2ecf20Sopenharmony_civoid qib_disarm_piobufs(struct qib_devdata *dd, unsigned first, unsigned cnt)
628c2ecf20Sopenharmony_ci{
638c2ecf20Sopenharmony_ci	unsigned long flags;
648c2ecf20Sopenharmony_ci	unsigned i;
658c2ecf20Sopenharmony_ci	unsigned last;
668c2ecf20Sopenharmony_ci
678c2ecf20Sopenharmony_ci	last = first + cnt;
688c2ecf20Sopenharmony_ci	spin_lock_irqsave(&dd->pioavail_lock, flags);
698c2ecf20Sopenharmony_ci	for (i = first; i < last; i++) {
708c2ecf20Sopenharmony_ci		__clear_bit(i, dd->pio_need_disarm);
718c2ecf20Sopenharmony_ci		dd->f_sendctrl(dd->pport, QIB_SENDCTRL_DISARM_BUF(i));
728c2ecf20Sopenharmony_ci	}
738c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&dd->pioavail_lock, flags);
748c2ecf20Sopenharmony_ci}
758c2ecf20Sopenharmony_ci
768c2ecf20Sopenharmony_ci/*
778c2ecf20Sopenharmony_ci * This is called by a user process when it sees the DISARM_BUFS event
788c2ecf20Sopenharmony_ci * bit is set.
798c2ecf20Sopenharmony_ci */
808c2ecf20Sopenharmony_ciint qib_disarm_piobufs_ifneeded(struct qib_ctxtdata *rcd)
818c2ecf20Sopenharmony_ci{
828c2ecf20Sopenharmony_ci	struct qib_devdata *dd = rcd->dd;
838c2ecf20Sopenharmony_ci	unsigned i;
848c2ecf20Sopenharmony_ci	unsigned last;
858c2ecf20Sopenharmony_ci	unsigned n = 0;
868c2ecf20Sopenharmony_ci
878c2ecf20Sopenharmony_ci	last = rcd->pio_base + rcd->piocnt;
888c2ecf20Sopenharmony_ci	/*
898c2ecf20Sopenharmony_ci	 * Don't need uctxt_lock here, since user has called in to us.
908c2ecf20Sopenharmony_ci	 * Clear at start in case more interrupts set bits while we
918c2ecf20Sopenharmony_ci	 * are disarming
928c2ecf20Sopenharmony_ci	 */
938c2ecf20Sopenharmony_ci	if (rcd->user_event_mask) {
948c2ecf20Sopenharmony_ci		/*
958c2ecf20Sopenharmony_ci		 * subctxt_cnt is 0 if not shared, so do base
968c2ecf20Sopenharmony_ci		 * separately, first, then remaining subctxt, if any
978c2ecf20Sopenharmony_ci		 */
988c2ecf20Sopenharmony_ci		clear_bit(_QIB_EVENT_DISARM_BUFS_BIT, &rcd->user_event_mask[0]);
998c2ecf20Sopenharmony_ci		for (i = 1; i < rcd->subctxt_cnt; i++)
1008c2ecf20Sopenharmony_ci			clear_bit(_QIB_EVENT_DISARM_BUFS_BIT,
1018c2ecf20Sopenharmony_ci				  &rcd->user_event_mask[i]);
1028c2ecf20Sopenharmony_ci	}
1038c2ecf20Sopenharmony_ci	spin_lock_irq(&dd->pioavail_lock);
1048c2ecf20Sopenharmony_ci	for (i = rcd->pio_base; i < last; i++) {
1058c2ecf20Sopenharmony_ci		if (__test_and_clear_bit(i, dd->pio_need_disarm)) {
1068c2ecf20Sopenharmony_ci			n++;
1078c2ecf20Sopenharmony_ci			dd->f_sendctrl(rcd->ppd, QIB_SENDCTRL_DISARM_BUF(i));
1088c2ecf20Sopenharmony_ci		}
1098c2ecf20Sopenharmony_ci	}
1108c2ecf20Sopenharmony_ci	spin_unlock_irq(&dd->pioavail_lock);
1118c2ecf20Sopenharmony_ci	return 0;
1128c2ecf20Sopenharmony_ci}
1138c2ecf20Sopenharmony_ci
1148c2ecf20Sopenharmony_cistatic struct qib_pportdata *is_sdma_buf(struct qib_devdata *dd, unsigned i)
1158c2ecf20Sopenharmony_ci{
1168c2ecf20Sopenharmony_ci	struct qib_pportdata *ppd;
1178c2ecf20Sopenharmony_ci	unsigned pidx;
1188c2ecf20Sopenharmony_ci
1198c2ecf20Sopenharmony_ci	for (pidx = 0; pidx < dd->num_pports; pidx++) {
1208c2ecf20Sopenharmony_ci		ppd = dd->pport + pidx;
1218c2ecf20Sopenharmony_ci		if (i >= ppd->sdma_state.first_sendbuf &&
1228c2ecf20Sopenharmony_ci		    i < ppd->sdma_state.last_sendbuf)
1238c2ecf20Sopenharmony_ci			return ppd;
1248c2ecf20Sopenharmony_ci	}
1258c2ecf20Sopenharmony_ci	return NULL;
1268c2ecf20Sopenharmony_ci}
1278c2ecf20Sopenharmony_ci
1288c2ecf20Sopenharmony_ci/*
1298c2ecf20Sopenharmony_ci * Return true if send buffer is being used by a user context.
1308c2ecf20Sopenharmony_ci * Sets  _QIB_EVENT_DISARM_BUFS_BIT in user_event_mask as a side effect
1318c2ecf20Sopenharmony_ci */
1328c2ecf20Sopenharmony_cistatic int find_ctxt(struct qib_devdata *dd, unsigned bufn)
1338c2ecf20Sopenharmony_ci{
1348c2ecf20Sopenharmony_ci	struct qib_ctxtdata *rcd;
1358c2ecf20Sopenharmony_ci	unsigned ctxt;
1368c2ecf20Sopenharmony_ci	int ret = 0;
1378c2ecf20Sopenharmony_ci
1388c2ecf20Sopenharmony_ci	spin_lock(&dd->uctxt_lock);
1398c2ecf20Sopenharmony_ci	for (ctxt = dd->first_user_ctxt; ctxt < dd->cfgctxts; ctxt++) {
1408c2ecf20Sopenharmony_ci		rcd = dd->rcd[ctxt];
1418c2ecf20Sopenharmony_ci		if (!rcd || bufn < rcd->pio_base ||
1428c2ecf20Sopenharmony_ci		    bufn >= rcd->pio_base + rcd->piocnt)
1438c2ecf20Sopenharmony_ci			continue;
1448c2ecf20Sopenharmony_ci		if (rcd->user_event_mask) {
1458c2ecf20Sopenharmony_ci			int i;
1468c2ecf20Sopenharmony_ci			/*
1478c2ecf20Sopenharmony_ci			 * subctxt_cnt is 0 if not shared, so do base
1488c2ecf20Sopenharmony_ci			 * separately, first, then remaining subctxt, if any
1498c2ecf20Sopenharmony_ci			 */
1508c2ecf20Sopenharmony_ci			set_bit(_QIB_EVENT_DISARM_BUFS_BIT,
1518c2ecf20Sopenharmony_ci				&rcd->user_event_mask[0]);
1528c2ecf20Sopenharmony_ci			for (i = 1; i < rcd->subctxt_cnt; i++)
1538c2ecf20Sopenharmony_ci				set_bit(_QIB_EVENT_DISARM_BUFS_BIT,
1548c2ecf20Sopenharmony_ci					&rcd->user_event_mask[i]);
1558c2ecf20Sopenharmony_ci		}
1568c2ecf20Sopenharmony_ci		ret = 1;
1578c2ecf20Sopenharmony_ci		break;
1588c2ecf20Sopenharmony_ci	}
1598c2ecf20Sopenharmony_ci	spin_unlock(&dd->uctxt_lock);
1608c2ecf20Sopenharmony_ci
1618c2ecf20Sopenharmony_ci	return ret;
1628c2ecf20Sopenharmony_ci}
1638c2ecf20Sopenharmony_ci
1648c2ecf20Sopenharmony_ci/*
1658c2ecf20Sopenharmony_ci * Disarm a set of send buffers.  If the buffer might be actively being
1668c2ecf20Sopenharmony_ci * written to, mark the buffer to be disarmed later when it is not being
1678c2ecf20Sopenharmony_ci * written to.
1688c2ecf20Sopenharmony_ci *
1698c2ecf20Sopenharmony_ci * This should only be called from the IRQ error handler.
1708c2ecf20Sopenharmony_ci */
1718c2ecf20Sopenharmony_civoid qib_disarm_piobufs_set(struct qib_devdata *dd, unsigned long *mask,
1728c2ecf20Sopenharmony_ci			    unsigned cnt)
1738c2ecf20Sopenharmony_ci{
1748c2ecf20Sopenharmony_ci	struct qib_pportdata *ppd, *pppd[QIB_MAX_IB_PORTS];
1758c2ecf20Sopenharmony_ci	unsigned i;
1768c2ecf20Sopenharmony_ci	unsigned long flags;
1778c2ecf20Sopenharmony_ci
1788c2ecf20Sopenharmony_ci	for (i = 0; i < dd->num_pports; i++)
1798c2ecf20Sopenharmony_ci		pppd[i] = NULL;
1808c2ecf20Sopenharmony_ci
1818c2ecf20Sopenharmony_ci	for (i = 0; i < cnt; i++) {
1828c2ecf20Sopenharmony_ci		if (!test_bit(i, mask))
1838c2ecf20Sopenharmony_ci			continue;
1848c2ecf20Sopenharmony_ci		/*
1858c2ecf20Sopenharmony_ci		 * If the buffer is owned by the DMA hardware,
1868c2ecf20Sopenharmony_ci		 * reset the DMA engine.
1878c2ecf20Sopenharmony_ci		 */
1888c2ecf20Sopenharmony_ci		ppd = is_sdma_buf(dd, i);
1898c2ecf20Sopenharmony_ci		if (ppd) {
1908c2ecf20Sopenharmony_ci			pppd[ppd->port] = ppd;
1918c2ecf20Sopenharmony_ci			continue;
1928c2ecf20Sopenharmony_ci		}
1938c2ecf20Sopenharmony_ci		/*
1948c2ecf20Sopenharmony_ci		 * If the kernel is writing the buffer or the buffer is
1958c2ecf20Sopenharmony_ci		 * owned by a user process, we can't clear it yet.
1968c2ecf20Sopenharmony_ci		 */
1978c2ecf20Sopenharmony_ci		spin_lock_irqsave(&dd->pioavail_lock, flags);
1988c2ecf20Sopenharmony_ci		if (test_bit(i, dd->pio_writing) ||
1998c2ecf20Sopenharmony_ci		    (!test_bit(i << 1, dd->pioavailkernel) &&
2008c2ecf20Sopenharmony_ci		     find_ctxt(dd, i))) {
2018c2ecf20Sopenharmony_ci			__set_bit(i, dd->pio_need_disarm);
2028c2ecf20Sopenharmony_ci		} else {
2038c2ecf20Sopenharmony_ci			dd->f_sendctrl(dd->pport, QIB_SENDCTRL_DISARM_BUF(i));
2048c2ecf20Sopenharmony_ci		}
2058c2ecf20Sopenharmony_ci		spin_unlock_irqrestore(&dd->pioavail_lock, flags);
2068c2ecf20Sopenharmony_ci	}
2078c2ecf20Sopenharmony_ci
2088c2ecf20Sopenharmony_ci	/* do cancel_sends once per port that had sdma piobufs in error */
2098c2ecf20Sopenharmony_ci	for (i = 0; i < dd->num_pports; i++)
2108c2ecf20Sopenharmony_ci		if (pppd[i])
2118c2ecf20Sopenharmony_ci			qib_cancel_sends(pppd[i]);
2128c2ecf20Sopenharmony_ci}
2138c2ecf20Sopenharmony_ci
2148c2ecf20Sopenharmony_ci/**
2158c2ecf20Sopenharmony_ci * update_send_bufs - update shadow copy of the PIO availability map
2168c2ecf20Sopenharmony_ci * @dd: the qlogic_ib device
2178c2ecf20Sopenharmony_ci *
2188c2ecf20Sopenharmony_ci * called whenever our local copy indicates we have run out of send buffers
2198c2ecf20Sopenharmony_ci */
2208c2ecf20Sopenharmony_cistatic void update_send_bufs(struct qib_devdata *dd)
2218c2ecf20Sopenharmony_ci{
2228c2ecf20Sopenharmony_ci	unsigned long flags;
2238c2ecf20Sopenharmony_ci	unsigned i;
2248c2ecf20Sopenharmony_ci	const unsigned piobregs = dd->pioavregs;
2258c2ecf20Sopenharmony_ci
2268c2ecf20Sopenharmony_ci	/*
2278c2ecf20Sopenharmony_ci	 * If the generation (check) bits have changed, then we update the
2288c2ecf20Sopenharmony_ci	 * busy bit for the corresponding PIO buffer.  This algorithm will
2298c2ecf20Sopenharmony_ci	 * modify positions to the value they already have in some cases
2308c2ecf20Sopenharmony_ci	 * (i.e., no change), but it's faster than changing only the bits
2318c2ecf20Sopenharmony_ci	 * that have changed.
2328c2ecf20Sopenharmony_ci	 *
2338c2ecf20Sopenharmony_ci	 * We would like to do this atomicly, to avoid spinlocks in the
2348c2ecf20Sopenharmony_ci	 * critical send path, but that's not really possible, given the
2358c2ecf20Sopenharmony_ci	 * type of changes, and that this routine could be called on
2368c2ecf20Sopenharmony_ci	 * multiple cpu's simultaneously, so we lock in this routine only,
2378c2ecf20Sopenharmony_ci	 * to avoid conflicting updates; all we change is the shadow, and
2388c2ecf20Sopenharmony_ci	 * it's a single 64 bit memory location, so by definition the update
2398c2ecf20Sopenharmony_ci	 * is atomic in terms of what other cpu's can see in testing the
2408c2ecf20Sopenharmony_ci	 * bits.  The spin_lock overhead isn't too bad, since it only
2418c2ecf20Sopenharmony_ci	 * happens when all buffers are in use, so only cpu overhead, not
2428c2ecf20Sopenharmony_ci	 * latency or bandwidth is affected.
2438c2ecf20Sopenharmony_ci	 */
2448c2ecf20Sopenharmony_ci	if (!dd->pioavailregs_dma)
2458c2ecf20Sopenharmony_ci		return;
2468c2ecf20Sopenharmony_ci	spin_lock_irqsave(&dd->pioavail_lock, flags);
2478c2ecf20Sopenharmony_ci	for (i = 0; i < piobregs; i++) {
2488c2ecf20Sopenharmony_ci		u64 pchbusy, pchg, piov, pnew;
2498c2ecf20Sopenharmony_ci
2508c2ecf20Sopenharmony_ci		piov = le64_to_cpu(dd->pioavailregs_dma[i]);
2518c2ecf20Sopenharmony_ci		pchg = dd->pioavailkernel[i] &
2528c2ecf20Sopenharmony_ci			~(dd->pioavailshadow[i] ^ piov);
2538c2ecf20Sopenharmony_ci		pchbusy = pchg << QLOGIC_IB_SENDPIOAVAIL_BUSY_SHIFT;
2548c2ecf20Sopenharmony_ci		if (pchg && (pchbusy & dd->pioavailshadow[i])) {
2558c2ecf20Sopenharmony_ci			pnew = dd->pioavailshadow[i] & ~pchbusy;
2568c2ecf20Sopenharmony_ci			pnew |= piov & pchbusy;
2578c2ecf20Sopenharmony_ci			dd->pioavailshadow[i] = pnew;
2588c2ecf20Sopenharmony_ci		}
2598c2ecf20Sopenharmony_ci	}
2608c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&dd->pioavail_lock, flags);
2618c2ecf20Sopenharmony_ci}
2628c2ecf20Sopenharmony_ci
2638c2ecf20Sopenharmony_ci/*
2648c2ecf20Sopenharmony_ci * Debugging code and stats updates if no pio buffers available.
2658c2ecf20Sopenharmony_ci */
2668c2ecf20Sopenharmony_cistatic noinline void no_send_bufs(struct qib_devdata *dd)
2678c2ecf20Sopenharmony_ci{
2688c2ecf20Sopenharmony_ci	dd->upd_pio_shadow = 1;
2698c2ecf20Sopenharmony_ci
2708c2ecf20Sopenharmony_ci	/* not atomic, but if we lose a stat count in a while, that's OK */
2718c2ecf20Sopenharmony_ci	qib_stats.sps_nopiobufs++;
2728c2ecf20Sopenharmony_ci}
2738c2ecf20Sopenharmony_ci
2748c2ecf20Sopenharmony_ci/*
2758c2ecf20Sopenharmony_ci * Common code for normal driver send buffer allocation, and reserved
2768c2ecf20Sopenharmony_ci * allocation.
2778c2ecf20Sopenharmony_ci *
2788c2ecf20Sopenharmony_ci * Do appropriate marking as busy, etc.
2798c2ecf20Sopenharmony_ci * Returns buffer pointer if one is found, otherwise NULL.
2808c2ecf20Sopenharmony_ci */
2818c2ecf20Sopenharmony_ciu32 __iomem *qib_getsendbuf_range(struct qib_devdata *dd, u32 *pbufnum,
2828c2ecf20Sopenharmony_ci				  u32 first, u32 last)
2838c2ecf20Sopenharmony_ci{
2848c2ecf20Sopenharmony_ci	unsigned i, j, updated = 0;
2858c2ecf20Sopenharmony_ci	unsigned nbufs;
2868c2ecf20Sopenharmony_ci	unsigned long flags;
2878c2ecf20Sopenharmony_ci	unsigned long *shadow = dd->pioavailshadow;
2888c2ecf20Sopenharmony_ci	u32 __iomem *buf;
2898c2ecf20Sopenharmony_ci
2908c2ecf20Sopenharmony_ci	if (!(dd->flags & QIB_PRESENT))
2918c2ecf20Sopenharmony_ci		return NULL;
2928c2ecf20Sopenharmony_ci
2938c2ecf20Sopenharmony_ci	nbufs = last - first + 1; /* number in range to check */
2948c2ecf20Sopenharmony_ci	if (dd->upd_pio_shadow) {
2958c2ecf20Sopenharmony_ciupdate_shadow:
2968c2ecf20Sopenharmony_ci		/*
2978c2ecf20Sopenharmony_ci		 * Minor optimization.  If we had no buffers on last call,
2988c2ecf20Sopenharmony_ci		 * start out by doing the update; continue and do scan even
2998c2ecf20Sopenharmony_ci		 * if no buffers were updated, to be paranoid.
3008c2ecf20Sopenharmony_ci		 */
3018c2ecf20Sopenharmony_ci		update_send_bufs(dd);
3028c2ecf20Sopenharmony_ci		updated++;
3038c2ecf20Sopenharmony_ci	}
3048c2ecf20Sopenharmony_ci	i = first;
3058c2ecf20Sopenharmony_ci	/*
3068c2ecf20Sopenharmony_ci	 * While test_and_set_bit() is atomic, we do that and then the
3078c2ecf20Sopenharmony_ci	 * change_bit(), and the pair is not.  See if this is the cause
3088c2ecf20Sopenharmony_ci	 * of the remaining armlaunch errors.
3098c2ecf20Sopenharmony_ci	 */
3108c2ecf20Sopenharmony_ci	spin_lock_irqsave(&dd->pioavail_lock, flags);
3118c2ecf20Sopenharmony_ci	if (dd->last_pio >= first && dd->last_pio <= last)
3128c2ecf20Sopenharmony_ci		i = dd->last_pio + 1;
3138c2ecf20Sopenharmony_ci	if (!first)
3148c2ecf20Sopenharmony_ci		/* adjust to min possible  */
3158c2ecf20Sopenharmony_ci		nbufs = last - dd->min_kernel_pio + 1;
3168c2ecf20Sopenharmony_ci	for (j = 0; j < nbufs; j++, i++) {
3178c2ecf20Sopenharmony_ci		if (i > last)
3188c2ecf20Sopenharmony_ci			i = !first ? dd->min_kernel_pio : first;
3198c2ecf20Sopenharmony_ci		if (__test_and_set_bit((2 * i) + 1, shadow))
3208c2ecf20Sopenharmony_ci			continue;
3218c2ecf20Sopenharmony_ci		/* flip generation bit */
3228c2ecf20Sopenharmony_ci		__change_bit(2 * i, shadow);
3238c2ecf20Sopenharmony_ci		/* remember that the buffer can be written to now */
3248c2ecf20Sopenharmony_ci		__set_bit(i, dd->pio_writing);
3258c2ecf20Sopenharmony_ci		if (!first && first != last) /* first == last on VL15, avoid */
3268c2ecf20Sopenharmony_ci			dd->last_pio = i;
3278c2ecf20Sopenharmony_ci		break;
3288c2ecf20Sopenharmony_ci	}
3298c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&dd->pioavail_lock, flags);
3308c2ecf20Sopenharmony_ci
3318c2ecf20Sopenharmony_ci	if (j == nbufs) {
3328c2ecf20Sopenharmony_ci		if (!updated)
3338c2ecf20Sopenharmony_ci			/*
3348c2ecf20Sopenharmony_ci			 * First time through; shadow exhausted, but may be
3358c2ecf20Sopenharmony_ci			 * buffers available, try an update and then rescan.
3368c2ecf20Sopenharmony_ci			 */
3378c2ecf20Sopenharmony_ci			goto update_shadow;
3388c2ecf20Sopenharmony_ci		no_send_bufs(dd);
3398c2ecf20Sopenharmony_ci		buf = NULL;
3408c2ecf20Sopenharmony_ci	} else {
3418c2ecf20Sopenharmony_ci		if (i < dd->piobcnt2k)
3428c2ecf20Sopenharmony_ci			buf = (u32 __iomem *)(dd->pio2kbase +
3438c2ecf20Sopenharmony_ci				i * dd->palign);
3448c2ecf20Sopenharmony_ci		else if (i < dd->piobcnt2k + dd->piobcnt4k || !dd->piovl15base)
3458c2ecf20Sopenharmony_ci			buf = (u32 __iomem *)(dd->pio4kbase +
3468c2ecf20Sopenharmony_ci				(i - dd->piobcnt2k) * dd->align4k);
3478c2ecf20Sopenharmony_ci		else
3488c2ecf20Sopenharmony_ci			buf = (u32 __iomem *)(dd->piovl15base +
3498c2ecf20Sopenharmony_ci				(i - (dd->piobcnt2k + dd->piobcnt4k)) *
3508c2ecf20Sopenharmony_ci				dd->align4k);
3518c2ecf20Sopenharmony_ci		if (pbufnum)
3528c2ecf20Sopenharmony_ci			*pbufnum = i;
3538c2ecf20Sopenharmony_ci		dd->upd_pio_shadow = 0;
3548c2ecf20Sopenharmony_ci	}
3558c2ecf20Sopenharmony_ci
3568c2ecf20Sopenharmony_ci	return buf;
3578c2ecf20Sopenharmony_ci}
3588c2ecf20Sopenharmony_ci
3598c2ecf20Sopenharmony_ci/*
3608c2ecf20Sopenharmony_ci * Record that the caller is finished writing to the buffer so we don't
3618c2ecf20Sopenharmony_ci * disarm it while it is being written and disarm it now if needed.
3628c2ecf20Sopenharmony_ci */
3638c2ecf20Sopenharmony_civoid qib_sendbuf_done(struct qib_devdata *dd, unsigned n)
3648c2ecf20Sopenharmony_ci{
3658c2ecf20Sopenharmony_ci	unsigned long flags;
3668c2ecf20Sopenharmony_ci
3678c2ecf20Sopenharmony_ci	spin_lock_irqsave(&dd->pioavail_lock, flags);
3688c2ecf20Sopenharmony_ci	__clear_bit(n, dd->pio_writing);
3698c2ecf20Sopenharmony_ci	if (__test_and_clear_bit(n, dd->pio_need_disarm))
3708c2ecf20Sopenharmony_ci		dd->f_sendctrl(dd->pport, QIB_SENDCTRL_DISARM_BUF(n));
3718c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&dd->pioavail_lock, flags);
3728c2ecf20Sopenharmony_ci}
3738c2ecf20Sopenharmony_ci
3748c2ecf20Sopenharmony_ci/**
3758c2ecf20Sopenharmony_ci * qib_chg_pioavailkernel - change which send buffers are available for kernel
3768c2ecf20Sopenharmony_ci * @dd: the qlogic_ib device
3778c2ecf20Sopenharmony_ci * @start: the starting send buffer number
3788c2ecf20Sopenharmony_ci * @len: the number of send buffers
3798c2ecf20Sopenharmony_ci * @avail: true if the buffers are available for kernel use, false otherwise
3808c2ecf20Sopenharmony_ci */
3818c2ecf20Sopenharmony_civoid qib_chg_pioavailkernel(struct qib_devdata *dd, unsigned start,
3828c2ecf20Sopenharmony_ci	unsigned len, u32 avail, struct qib_ctxtdata *rcd)
3838c2ecf20Sopenharmony_ci{
3848c2ecf20Sopenharmony_ci	unsigned long flags;
3858c2ecf20Sopenharmony_ci	unsigned end;
3868c2ecf20Sopenharmony_ci	unsigned ostart = start;
3878c2ecf20Sopenharmony_ci
3888c2ecf20Sopenharmony_ci	/* There are two bits per send buffer (busy and generation) */
3898c2ecf20Sopenharmony_ci	start *= 2;
3908c2ecf20Sopenharmony_ci	end = start + len * 2;
3918c2ecf20Sopenharmony_ci
3928c2ecf20Sopenharmony_ci	spin_lock_irqsave(&dd->pioavail_lock, flags);
3938c2ecf20Sopenharmony_ci	/* Set or clear the busy bit in the shadow. */
3948c2ecf20Sopenharmony_ci	while (start < end) {
3958c2ecf20Sopenharmony_ci		if (avail) {
3968c2ecf20Sopenharmony_ci			unsigned long dma;
3978c2ecf20Sopenharmony_ci			int i;
3988c2ecf20Sopenharmony_ci
3998c2ecf20Sopenharmony_ci			/*
4008c2ecf20Sopenharmony_ci			 * The BUSY bit will never be set, because we disarm
4018c2ecf20Sopenharmony_ci			 * the user buffers before we hand them back to the
4028c2ecf20Sopenharmony_ci			 * kernel.  We do have to make sure the generation
4038c2ecf20Sopenharmony_ci			 * bit is set correctly in shadow, since it could
4048c2ecf20Sopenharmony_ci			 * have changed many times while allocated to user.
4058c2ecf20Sopenharmony_ci			 * We can't use the bitmap functions on the full
4068c2ecf20Sopenharmony_ci			 * dma array because it is always little-endian, so
4078c2ecf20Sopenharmony_ci			 * we have to flip to host-order first.
4088c2ecf20Sopenharmony_ci			 * BITS_PER_LONG is slightly wrong, since it's
4098c2ecf20Sopenharmony_ci			 * always 64 bits per register in chip...
4108c2ecf20Sopenharmony_ci			 * We only work on 64 bit kernels, so that's OK.
4118c2ecf20Sopenharmony_ci			 */
4128c2ecf20Sopenharmony_ci			i = start / BITS_PER_LONG;
4138c2ecf20Sopenharmony_ci			__clear_bit(QLOGIC_IB_SENDPIOAVAIL_BUSY_SHIFT + start,
4148c2ecf20Sopenharmony_ci				    dd->pioavailshadow);
4158c2ecf20Sopenharmony_ci			dma = (unsigned long)
4168c2ecf20Sopenharmony_ci				le64_to_cpu(dd->pioavailregs_dma[i]);
4178c2ecf20Sopenharmony_ci			if (test_bit((QLOGIC_IB_SENDPIOAVAIL_CHECK_SHIFT +
4188c2ecf20Sopenharmony_ci				      start) % BITS_PER_LONG, &dma))
4198c2ecf20Sopenharmony_ci				__set_bit(QLOGIC_IB_SENDPIOAVAIL_CHECK_SHIFT +
4208c2ecf20Sopenharmony_ci					  start, dd->pioavailshadow);
4218c2ecf20Sopenharmony_ci			else
4228c2ecf20Sopenharmony_ci				__clear_bit(QLOGIC_IB_SENDPIOAVAIL_CHECK_SHIFT
4238c2ecf20Sopenharmony_ci					    + start, dd->pioavailshadow);
4248c2ecf20Sopenharmony_ci			__set_bit(start, dd->pioavailkernel);
4258c2ecf20Sopenharmony_ci			if ((start >> 1) < dd->min_kernel_pio)
4268c2ecf20Sopenharmony_ci				dd->min_kernel_pio = start >> 1;
4278c2ecf20Sopenharmony_ci		} else {
4288c2ecf20Sopenharmony_ci			__set_bit(start + QLOGIC_IB_SENDPIOAVAIL_BUSY_SHIFT,
4298c2ecf20Sopenharmony_ci				  dd->pioavailshadow);
4308c2ecf20Sopenharmony_ci			__clear_bit(start, dd->pioavailkernel);
4318c2ecf20Sopenharmony_ci			if ((start >> 1) > dd->min_kernel_pio)
4328c2ecf20Sopenharmony_ci				dd->min_kernel_pio = start >> 1;
4338c2ecf20Sopenharmony_ci		}
4348c2ecf20Sopenharmony_ci		start += 2;
4358c2ecf20Sopenharmony_ci	}
4368c2ecf20Sopenharmony_ci
4378c2ecf20Sopenharmony_ci	if (dd->min_kernel_pio > 0 && dd->last_pio < dd->min_kernel_pio - 1)
4388c2ecf20Sopenharmony_ci		dd->last_pio = dd->min_kernel_pio - 1;
4398c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&dd->pioavail_lock, flags);
4408c2ecf20Sopenharmony_ci
4418c2ecf20Sopenharmony_ci	dd->f_txchk_change(dd, ostart, len, avail, rcd);
4428c2ecf20Sopenharmony_ci}
4438c2ecf20Sopenharmony_ci
4448c2ecf20Sopenharmony_ci/*
4458c2ecf20Sopenharmony_ci * Flush all sends that might be in the ready to send state, as well as any
4468c2ecf20Sopenharmony_ci * that are in the process of being sent.  Used whenever we need to be
4478c2ecf20Sopenharmony_ci * sure the send side is idle.  Cleans up all buffer state by canceling
4488c2ecf20Sopenharmony_ci * all pio buffers, and issuing an abort, which cleans up anything in the
4498c2ecf20Sopenharmony_ci * launch fifo.  The cancel is superfluous on some chip versions, but
4508c2ecf20Sopenharmony_ci * it's safer to always do it.
4518c2ecf20Sopenharmony_ci * PIOAvail bits are updated by the chip as if a normal send had happened.
4528c2ecf20Sopenharmony_ci */
4538c2ecf20Sopenharmony_civoid qib_cancel_sends(struct qib_pportdata *ppd)
4548c2ecf20Sopenharmony_ci{
4558c2ecf20Sopenharmony_ci	struct qib_devdata *dd = ppd->dd;
4568c2ecf20Sopenharmony_ci	struct qib_ctxtdata *rcd;
4578c2ecf20Sopenharmony_ci	unsigned long flags;
4588c2ecf20Sopenharmony_ci	unsigned ctxt;
4598c2ecf20Sopenharmony_ci	unsigned i;
4608c2ecf20Sopenharmony_ci	unsigned last;
4618c2ecf20Sopenharmony_ci
4628c2ecf20Sopenharmony_ci	/*
4638c2ecf20Sopenharmony_ci	 * Tell PSM to disarm buffers again before trying to reuse them.
4648c2ecf20Sopenharmony_ci	 * We need to be sure the rcd doesn't change out from under us
4658c2ecf20Sopenharmony_ci	 * while we do so.  We hold the two locks sequentially.  We might
4668c2ecf20Sopenharmony_ci	 * needlessly set some need_disarm bits as a result, if the
4678c2ecf20Sopenharmony_ci	 * context is closed after we release the uctxt_lock, but that's
4688c2ecf20Sopenharmony_ci	 * fairly benign, and safer than nesting the locks.
4698c2ecf20Sopenharmony_ci	 */
4708c2ecf20Sopenharmony_ci	for (ctxt = dd->first_user_ctxt; ctxt < dd->cfgctxts; ctxt++) {
4718c2ecf20Sopenharmony_ci		spin_lock_irqsave(&dd->uctxt_lock, flags);
4728c2ecf20Sopenharmony_ci		rcd = dd->rcd[ctxt];
4738c2ecf20Sopenharmony_ci		if (rcd && rcd->ppd == ppd) {
4748c2ecf20Sopenharmony_ci			last = rcd->pio_base + rcd->piocnt;
4758c2ecf20Sopenharmony_ci			if (rcd->user_event_mask) {
4768c2ecf20Sopenharmony_ci				/*
4778c2ecf20Sopenharmony_ci				 * subctxt_cnt is 0 if not shared, so do base
4788c2ecf20Sopenharmony_ci				 * separately, first, then remaining subctxt,
4798c2ecf20Sopenharmony_ci				 * if any
4808c2ecf20Sopenharmony_ci				 */
4818c2ecf20Sopenharmony_ci				set_bit(_QIB_EVENT_DISARM_BUFS_BIT,
4828c2ecf20Sopenharmony_ci					&rcd->user_event_mask[0]);
4838c2ecf20Sopenharmony_ci				for (i = 1; i < rcd->subctxt_cnt; i++)
4848c2ecf20Sopenharmony_ci					set_bit(_QIB_EVENT_DISARM_BUFS_BIT,
4858c2ecf20Sopenharmony_ci						&rcd->user_event_mask[i]);
4868c2ecf20Sopenharmony_ci			}
4878c2ecf20Sopenharmony_ci			i = rcd->pio_base;
4888c2ecf20Sopenharmony_ci			spin_unlock_irqrestore(&dd->uctxt_lock, flags);
4898c2ecf20Sopenharmony_ci			spin_lock_irqsave(&dd->pioavail_lock, flags);
4908c2ecf20Sopenharmony_ci			for (; i < last; i++)
4918c2ecf20Sopenharmony_ci				__set_bit(i, dd->pio_need_disarm);
4928c2ecf20Sopenharmony_ci			spin_unlock_irqrestore(&dd->pioavail_lock, flags);
4938c2ecf20Sopenharmony_ci		} else
4948c2ecf20Sopenharmony_ci			spin_unlock_irqrestore(&dd->uctxt_lock, flags);
4958c2ecf20Sopenharmony_ci	}
4968c2ecf20Sopenharmony_ci
4978c2ecf20Sopenharmony_ci	if (!(dd->flags & QIB_HAS_SEND_DMA))
4988c2ecf20Sopenharmony_ci		dd->f_sendctrl(ppd, QIB_SENDCTRL_DISARM_ALL |
4998c2ecf20Sopenharmony_ci				    QIB_SENDCTRL_FLUSH);
5008c2ecf20Sopenharmony_ci}
5018c2ecf20Sopenharmony_ci
5028c2ecf20Sopenharmony_ci/*
5038c2ecf20Sopenharmony_ci * Force an update of in-memory copy of the pioavail registers, when
5048c2ecf20Sopenharmony_ci * needed for any of a variety of reasons.
5058c2ecf20Sopenharmony_ci * If already off, this routine is a nop, on the assumption that the
5068c2ecf20Sopenharmony_ci * caller (or set of callers) will "do the right thing".
5078c2ecf20Sopenharmony_ci * This is a per-device operation, so just the first port.
5088c2ecf20Sopenharmony_ci */
5098c2ecf20Sopenharmony_civoid qib_force_pio_avail_update(struct qib_devdata *dd)
5108c2ecf20Sopenharmony_ci{
5118c2ecf20Sopenharmony_ci	dd->f_sendctrl(dd->pport, QIB_SENDCTRL_AVAIL_BLIP);
5128c2ecf20Sopenharmony_ci}
5138c2ecf20Sopenharmony_ci
5148c2ecf20Sopenharmony_civoid qib_hol_down(struct qib_pportdata *ppd)
5158c2ecf20Sopenharmony_ci{
5168c2ecf20Sopenharmony_ci	/*
5178c2ecf20Sopenharmony_ci	 * Cancel sends when the link goes DOWN so that we aren't doing it
5188c2ecf20Sopenharmony_ci	 * at INIT when we might be trying to send SMI packets.
5198c2ecf20Sopenharmony_ci	 */
5208c2ecf20Sopenharmony_ci	if (!(ppd->lflags & QIBL_IB_AUTONEG_INPROG))
5218c2ecf20Sopenharmony_ci		qib_cancel_sends(ppd);
5228c2ecf20Sopenharmony_ci}
5238c2ecf20Sopenharmony_ci
5248c2ecf20Sopenharmony_ci/*
5258c2ecf20Sopenharmony_ci * Link is at INIT.
5268c2ecf20Sopenharmony_ci * We start the HoL timer so we can detect stuck packets blocking SMP replies.
5278c2ecf20Sopenharmony_ci * Timer may already be running, so use mod_timer, not add_timer.
5288c2ecf20Sopenharmony_ci */
5298c2ecf20Sopenharmony_civoid qib_hol_init(struct qib_pportdata *ppd)
5308c2ecf20Sopenharmony_ci{
5318c2ecf20Sopenharmony_ci	if (ppd->hol_state != QIB_HOL_INIT) {
5328c2ecf20Sopenharmony_ci		ppd->hol_state = QIB_HOL_INIT;
5338c2ecf20Sopenharmony_ci		mod_timer(&ppd->hol_timer,
5348c2ecf20Sopenharmony_ci			  jiffies + msecs_to_jiffies(qib_hol_timeout_ms));
5358c2ecf20Sopenharmony_ci	}
5368c2ecf20Sopenharmony_ci}
5378c2ecf20Sopenharmony_ci
5388c2ecf20Sopenharmony_ci/*
5398c2ecf20Sopenharmony_ci * Link is up, continue any user processes, and ensure timer
5408c2ecf20Sopenharmony_ci * is a nop, if running.  Let timer keep running, if set; it
5418c2ecf20Sopenharmony_ci * will nop when it sees the link is up.
5428c2ecf20Sopenharmony_ci */
5438c2ecf20Sopenharmony_civoid qib_hol_up(struct qib_pportdata *ppd)
5448c2ecf20Sopenharmony_ci{
5458c2ecf20Sopenharmony_ci	ppd->hol_state = QIB_HOL_UP;
5468c2ecf20Sopenharmony_ci}
5478c2ecf20Sopenharmony_ci
5488c2ecf20Sopenharmony_ci/*
5498c2ecf20Sopenharmony_ci * This is only called via the timer.
5508c2ecf20Sopenharmony_ci */
5518c2ecf20Sopenharmony_civoid qib_hol_event(struct timer_list *t)
5528c2ecf20Sopenharmony_ci{
5538c2ecf20Sopenharmony_ci	struct qib_pportdata *ppd = from_timer(ppd, t, hol_timer);
5548c2ecf20Sopenharmony_ci
5558c2ecf20Sopenharmony_ci	/* If hardware error, etc, skip. */
5568c2ecf20Sopenharmony_ci	if (!(ppd->dd->flags & QIB_INITTED))
5578c2ecf20Sopenharmony_ci		return;
5588c2ecf20Sopenharmony_ci
5598c2ecf20Sopenharmony_ci	if (ppd->hol_state != QIB_HOL_UP) {
5608c2ecf20Sopenharmony_ci		/*
5618c2ecf20Sopenharmony_ci		 * Try to flush sends in case a stuck packet is blocking
5628c2ecf20Sopenharmony_ci		 * SMP replies.
5638c2ecf20Sopenharmony_ci		 */
5648c2ecf20Sopenharmony_ci		qib_hol_down(ppd);
5658c2ecf20Sopenharmony_ci		mod_timer(&ppd->hol_timer,
5668c2ecf20Sopenharmony_ci			  jiffies + msecs_to_jiffies(qib_hol_timeout_ms));
5678c2ecf20Sopenharmony_ci	}
5688c2ecf20Sopenharmony_ci}
569