xref: /linux/drivers/infiniband/hw/qib/qib_tx.c (revision 7ae9fb1b7ecbb5d85d07857943f677fd1a559b18)
1f931551bSRalph Campbell /*
2f931551bSRalph Campbell  * Copyright (c) 2008, 2009, 2010 QLogic Corporation. All rights reserved.
3f931551bSRalph Campbell  *
4f931551bSRalph Campbell  * This software is available to you under a choice of one of two
5f931551bSRalph Campbell  * licenses.  You may choose to be licensed under the terms of the GNU
6f931551bSRalph Campbell  * General Public License (GPL) Version 2, available from the file
7f931551bSRalph Campbell  * COPYING in the main directory of this source tree, or the
8f931551bSRalph Campbell  * OpenIB.org BSD license below:
9f931551bSRalph Campbell  *
10f931551bSRalph Campbell  *     Redistribution and use in source and binary forms, with or
11f931551bSRalph Campbell  *     without modification, are permitted provided that the following
12f931551bSRalph Campbell  *     conditions are met:
13f931551bSRalph Campbell  *
14f931551bSRalph Campbell  *      - Redistributions of source code must retain the above
15f931551bSRalph Campbell  *        copyright notice, this list of conditions and the following
16f931551bSRalph Campbell  *        disclaimer.
17f931551bSRalph Campbell  *
18f931551bSRalph Campbell  *      - Redistributions in binary form must reproduce the above
19f931551bSRalph Campbell  *        copyright notice, this list of conditions and the following
20f931551bSRalph Campbell  *        disclaimer in the documentation and/or other materials
21f931551bSRalph Campbell  *        provided with the distribution.
22f931551bSRalph Campbell  *
23f931551bSRalph Campbell  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24f931551bSRalph Campbell  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25f931551bSRalph Campbell  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26f931551bSRalph Campbell  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27f931551bSRalph Campbell  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28f931551bSRalph Campbell  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29f931551bSRalph Campbell  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30f931551bSRalph Campbell  * SOFTWARE.
31f931551bSRalph Campbell  */
32f931551bSRalph Campbell 
33f931551bSRalph Campbell #include <linux/spinlock.h>
34f931551bSRalph Campbell #include <linux/pci.h>
35f931551bSRalph Campbell #include <linux/io.h>
36f931551bSRalph Campbell #include <linux/delay.h>
37f931551bSRalph Campbell #include <linux/netdevice.h>
38f931551bSRalph Campbell #include <linux/vmalloc.h>
39fec14d2fSPaul Gortmaker #include <linux/moduleparam.h>
40f931551bSRalph Campbell 
41f931551bSRalph Campbell #include "qib.h"
42f931551bSRalph Campbell 
43f931551bSRalph Campbell static unsigned qib_hol_timeout_ms = 3000;
44f931551bSRalph Campbell module_param_named(hol_timeout_ms, qib_hol_timeout_ms, uint, S_IRUGO);
45f931551bSRalph Campbell MODULE_PARM_DESC(hol_timeout_ms,
46f931551bSRalph Campbell 		 "duration of user app suspension after link failure");
47f931551bSRalph Campbell 
48f931551bSRalph Campbell unsigned qib_sdma_fetch_arb = 1;
49f931551bSRalph Campbell module_param_named(fetch_arb, qib_sdma_fetch_arb, uint, S_IRUGO);
50f931551bSRalph Campbell MODULE_PARM_DESC(fetch_arb, "IBA7220: change SDMA descriptor arbitration");
51f931551bSRalph Campbell 
52f931551bSRalph Campbell /**
53f931551bSRalph Campbell  * qib_disarm_piobufs - cancel a range of PIO buffers
54f931551bSRalph Campbell  * @dd: the qlogic_ib device
55f931551bSRalph Campbell  * @first: the first PIO buffer to cancel
56f931551bSRalph Campbell  * @cnt: the number of PIO buffers to cancel
57f931551bSRalph Campbell  *
58f931551bSRalph Campbell  * Cancel a range of PIO buffers. Used at user process close,
59f931551bSRalph Campbell  * in case it died while writing to a PIO buffer.
60f931551bSRalph Campbell  */
qib_disarm_piobufs(struct qib_devdata * dd,unsigned first,unsigned cnt)61f931551bSRalph Campbell void qib_disarm_piobufs(struct qib_devdata *dd, unsigned first, unsigned cnt)
62f931551bSRalph Campbell {
63f931551bSRalph Campbell 	unsigned long flags;
64f931551bSRalph Campbell 	unsigned i;
65f931551bSRalph Campbell 	unsigned last;
66f931551bSRalph Campbell 
67f931551bSRalph Campbell 	last = first + cnt;
68f931551bSRalph Campbell 	spin_lock_irqsave(&dd->pioavail_lock, flags);
69f931551bSRalph Campbell 	for (i = first; i < last; i++) {
70f931551bSRalph Campbell 		__clear_bit(i, dd->pio_need_disarm);
71f931551bSRalph Campbell 		dd->f_sendctrl(dd->pport, QIB_SENDCTRL_DISARM_BUF(i));
72f931551bSRalph Campbell 	}
73f931551bSRalph Campbell 	spin_unlock_irqrestore(&dd->pioavail_lock, flags);
74f931551bSRalph Campbell }
75f931551bSRalph Campbell 
76f931551bSRalph Campbell /*
77f931551bSRalph Campbell  * This is called by a user process when it sees the DISARM_BUFS event
78f931551bSRalph Campbell  * bit is set.
79f931551bSRalph Campbell  */
qib_disarm_piobufs_ifneeded(struct qib_ctxtdata * rcd)80f931551bSRalph Campbell int qib_disarm_piobufs_ifneeded(struct qib_ctxtdata *rcd)
81f931551bSRalph Campbell {
82f931551bSRalph Campbell 	struct qib_devdata *dd = rcd->dd;
83f931551bSRalph Campbell 	unsigned i;
84f931551bSRalph Campbell 	unsigned last;
85f931551bSRalph Campbell 
86f931551bSRalph Campbell 	last = rcd->pio_base + rcd->piocnt;
87f931551bSRalph Campbell 	/*
88f931551bSRalph Campbell 	 * Don't need uctxt_lock here, since user has called in to us.
89f931551bSRalph Campbell 	 * Clear at start in case more interrupts set bits while we
90f931551bSRalph Campbell 	 * are disarming
91f931551bSRalph Campbell 	 */
92f931551bSRalph Campbell 	if (rcd->user_event_mask) {
93f931551bSRalph Campbell 		/*
94f931551bSRalph Campbell 		 * subctxt_cnt is 0 if not shared, so do base
95f931551bSRalph Campbell 		 * separately, first, then remaining subctxt, if any
96f931551bSRalph Campbell 		 */
97f931551bSRalph Campbell 		clear_bit(_QIB_EVENT_DISARM_BUFS_BIT, &rcd->user_event_mask[0]);
98f931551bSRalph Campbell 		for (i = 1; i < rcd->subctxt_cnt; i++)
99f931551bSRalph Campbell 			clear_bit(_QIB_EVENT_DISARM_BUFS_BIT,
100f931551bSRalph Campbell 				  &rcd->user_event_mask[i]);
101f931551bSRalph Campbell 	}
102f931551bSRalph Campbell 	spin_lock_irq(&dd->pioavail_lock);
103f931551bSRalph Campbell 	for (i = rcd->pio_base; i < last; i++) {
104*d0b9f28fSColin Ian King 		if (__test_and_clear_bit(i, dd->pio_need_disarm))
105f931551bSRalph Campbell 			dd->f_sendctrl(rcd->ppd, QIB_SENDCTRL_DISARM_BUF(i));
106f931551bSRalph Campbell 	}
107f931551bSRalph Campbell 	spin_unlock_irq(&dd->pioavail_lock);
108f931551bSRalph Campbell 	return 0;
109f931551bSRalph Campbell }
110f931551bSRalph Campbell 
is_sdma_buf(struct qib_devdata * dd,unsigned i)111f931551bSRalph Campbell static struct qib_pportdata *is_sdma_buf(struct qib_devdata *dd, unsigned i)
112f931551bSRalph Campbell {
113f931551bSRalph Campbell 	struct qib_pportdata *ppd;
114f931551bSRalph Campbell 	unsigned pidx;
115f931551bSRalph Campbell 
116f931551bSRalph Campbell 	for (pidx = 0; pidx < dd->num_pports; pidx++) {
117f931551bSRalph Campbell 		ppd = dd->pport + pidx;
118f931551bSRalph Campbell 		if (i >= ppd->sdma_state.first_sendbuf &&
119f931551bSRalph Campbell 		    i < ppd->sdma_state.last_sendbuf)
120f931551bSRalph Campbell 			return ppd;
121f931551bSRalph Campbell 	}
122f931551bSRalph Campbell 	return NULL;
123f931551bSRalph Campbell }
124f931551bSRalph Campbell 
125f931551bSRalph Campbell /*
126f931551bSRalph Campbell  * Return true if send buffer is being used by a user context.
127f931551bSRalph Campbell  * Sets  _QIB_EVENT_DISARM_BUFS_BIT in user_event_mask as a side effect
128f931551bSRalph Campbell  */
find_ctxt(struct qib_devdata * dd,unsigned bufn)129f931551bSRalph Campbell static int find_ctxt(struct qib_devdata *dd, unsigned bufn)
130f931551bSRalph Campbell {
131f931551bSRalph Campbell 	struct qib_ctxtdata *rcd;
132f931551bSRalph Campbell 	unsigned ctxt;
133f931551bSRalph Campbell 	int ret = 0;
134f931551bSRalph Campbell 
135f931551bSRalph Campbell 	spin_lock(&dd->uctxt_lock);
136f931551bSRalph Campbell 	for (ctxt = dd->first_user_ctxt; ctxt < dd->cfgctxts; ctxt++) {
137f931551bSRalph Campbell 		rcd = dd->rcd[ctxt];
138f931551bSRalph Campbell 		if (!rcd || bufn < rcd->pio_base ||
139f931551bSRalph Campbell 		    bufn >= rcd->pio_base + rcd->piocnt)
140f931551bSRalph Campbell 			continue;
141f931551bSRalph Campbell 		if (rcd->user_event_mask) {
142f931551bSRalph Campbell 			int i;
143f931551bSRalph Campbell 			/*
144f931551bSRalph Campbell 			 * subctxt_cnt is 0 if not shared, so do base
145f931551bSRalph Campbell 			 * separately, first, then remaining subctxt, if any
146f931551bSRalph Campbell 			 */
147f931551bSRalph Campbell 			set_bit(_QIB_EVENT_DISARM_BUFS_BIT,
148f931551bSRalph Campbell 				&rcd->user_event_mask[0]);
149f931551bSRalph Campbell 			for (i = 1; i < rcd->subctxt_cnt; i++)
150f931551bSRalph Campbell 				set_bit(_QIB_EVENT_DISARM_BUFS_BIT,
151f931551bSRalph Campbell 					&rcd->user_event_mask[i]);
152f931551bSRalph Campbell 		}
153f931551bSRalph Campbell 		ret = 1;
154f931551bSRalph Campbell 		break;
155f931551bSRalph Campbell 	}
156f931551bSRalph Campbell 	spin_unlock(&dd->uctxt_lock);
157f931551bSRalph Campbell 
158f931551bSRalph Campbell 	return ret;
159f931551bSRalph Campbell }
160f931551bSRalph Campbell 
161f931551bSRalph Campbell /*
162f931551bSRalph Campbell  * Disarm a set of send buffers.  If the buffer might be actively being
163f931551bSRalph Campbell  * written to, mark the buffer to be disarmed later when it is not being
164f931551bSRalph Campbell  * written to.
165f931551bSRalph Campbell  *
166f931551bSRalph Campbell  * This should only be called from the IRQ error handler.
167f931551bSRalph Campbell  */
qib_disarm_piobufs_set(struct qib_devdata * dd,unsigned long * mask,unsigned cnt)168f931551bSRalph Campbell void qib_disarm_piobufs_set(struct qib_devdata *dd, unsigned long *mask,
169f931551bSRalph Campbell 			    unsigned cnt)
170f931551bSRalph Campbell {
171cc323b2aSRalph Campbell 	struct qib_pportdata *ppd, *pppd[QIB_MAX_IB_PORTS];
172f931551bSRalph Campbell 	unsigned i;
173f931551bSRalph Campbell 	unsigned long flags;
174f931551bSRalph Campbell 
175f931551bSRalph Campbell 	for (i = 0; i < dd->num_pports; i++)
176f931551bSRalph Campbell 		pppd[i] = NULL;
177f931551bSRalph Campbell 
178f931551bSRalph Campbell 	for (i = 0; i < cnt; i++) {
179f931551bSRalph Campbell 		if (!test_bit(i, mask))
180f931551bSRalph Campbell 			continue;
181f931551bSRalph Campbell 		/*
182f931551bSRalph Campbell 		 * If the buffer is owned by the DMA hardware,
183f931551bSRalph Campbell 		 * reset the DMA engine.
184f931551bSRalph Campbell 		 */
185f931551bSRalph Campbell 		ppd = is_sdma_buf(dd, i);
186f931551bSRalph Campbell 		if (ppd) {
187f931551bSRalph Campbell 			pppd[ppd->port] = ppd;
188f931551bSRalph Campbell 			continue;
189f931551bSRalph Campbell 		}
190f931551bSRalph Campbell 		/*
191f931551bSRalph Campbell 		 * If the kernel is writing the buffer or the buffer is
192f931551bSRalph Campbell 		 * owned by a user process, we can't clear it yet.
193f931551bSRalph Campbell 		 */
194f931551bSRalph Campbell 		spin_lock_irqsave(&dd->pioavail_lock, flags);
195f931551bSRalph Campbell 		if (test_bit(i, dd->pio_writing) ||
196f931551bSRalph Campbell 		    (!test_bit(i << 1, dd->pioavailkernel) &&
197f931551bSRalph Campbell 		     find_ctxt(dd, i))) {
198f931551bSRalph Campbell 			__set_bit(i, dd->pio_need_disarm);
199f931551bSRalph Campbell 		} else {
200f931551bSRalph Campbell 			dd->f_sendctrl(dd->pport, QIB_SENDCTRL_DISARM_BUF(i));
201f931551bSRalph Campbell 		}
202f931551bSRalph Campbell 		spin_unlock_irqrestore(&dd->pioavail_lock, flags);
203f931551bSRalph Campbell 	}
204f931551bSRalph Campbell 
205f931551bSRalph Campbell 	/* do cancel_sends once per port that had sdma piobufs in error */
206f931551bSRalph Campbell 	for (i = 0; i < dd->num_pports; i++)
207f931551bSRalph Campbell 		if (pppd[i])
208f931551bSRalph Campbell 			qib_cancel_sends(pppd[i]);
209f931551bSRalph Campbell }
210f931551bSRalph Campbell 
211f931551bSRalph Campbell /**
212f931551bSRalph Campbell  * update_send_bufs - update shadow copy of the PIO availability map
213f931551bSRalph Campbell  * @dd: the qlogic_ib device
214f931551bSRalph Campbell  *
215f931551bSRalph Campbell  * called whenever our local copy indicates we have run out of send buffers
216f931551bSRalph Campbell  */
update_send_bufs(struct qib_devdata * dd)217f931551bSRalph Campbell static void update_send_bufs(struct qib_devdata *dd)
218f931551bSRalph Campbell {
219f931551bSRalph Campbell 	unsigned long flags;
220f931551bSRalph Campbell 	unsigned i;
221f931551bSRalph Campbell 	const unsigned piobregs = dd->pioavregs;
222f931551bSRalph Campbell 
223f931551bSRalph Campbell 	/*
224f931551bSRalph Campbell 	 * If the generation (check) bits have changed, then we update the
225f931551bSRalph Campbell 	 * busy bit for the corresponding PIO buffer.  This algorithm will
226f931551bSRalph Campbell 	 * modify positions to the value they already have in some cases
227f931551bSRalph Campbell 	 * (i.e., no change), but it's faster than changing only the bits
228f931551bSRalph Campbell 	 * that have changed.
229f931551bSRalph Campbell 	 *
230f931551bSRalph Campbell 	 * We would like to do this atomicly, to avoid spinlocks in the
231f931551bSRalph Campbell 	 * critical send path, but that's not really possible, given the
232f931551bSRalph Campbell 	 * type of changes, and that this routine could be called on
233f931551bSRalph Campbell 	 * multiple cpu's simultaneously, so we lock in this routine only,
234f931551bSRalph Campbell 	 * to avoid conflicting updates; all we change is the shadow, and
235f931551bSRalph Campbell 	 * it's a single 64 bit memory location, so by definition the update
236f931551bSRalph Campbell 	 * is atomic in terms of what other cpu's can see in testing the
237f931551bSRalph Campbell 	 * bits.  The spin_lock overhead isn't too bad, since it only
238f931551bSRalph Campbell 	 * happens when all buffers are in use, so only cpu overhead, not
239f931551bSRalph Campbell 	 * latency or bandwidth is affected.
240f931551bSRalph Campbell 	 */
241f931551bSRalph Campbell 	if (!dd->pioavailregs_dma)
242f931551bSRalph Campbell 		return;
243f931551bSRalph Campbell 	spin_lock_irqsave(&dd->pioavail_lock, flags);
244f931551bSRalph Campbell 	for (i = 0; i < piobregs; i++) {
245f931551bSRalph Campbell 		u64 pchbusy, pchg, piov, pnew;
246f931551bSRalph Campbell 
247f931551bSRalph Campbell 		piov = le64_to_cpu(dd->pioavailregs_dma[i]);
248f931551bSRalph Campbell 		pchg = dd->pioavailkernel[i] &
249f931551bSRalph Campbell 			~(dd->pioavailshadow[i] ^ piov);
250f931551bSRalph Campbell 		pchbusy = pchg << QLOGIC_IB_SENDPIOAVAIL_BUSY_SHIFT;
251f931551bSRalph Campbell 		if (pchg && (pchbusy & dd->pioavailshadow[i])) {
252f931551bSRalph Campbell 			pnew = dd->pioavailshadow[i] & ~pchbusy;
253f931551bSRalph Campbell 			pnew |= piov & pchbusy;
254f931551bSRalph Campbell 			dd->pioavailshadow[i] = pnew;
255f931551bSRalph Campbell 		}
256f931551bSRalph Campbell 	}
257f931551bSRalph Campbell 	spin_unlock_irqrestore(&dd->pioavail_lock, flags);
258f931551bSRalph Campbell }
259f931551bSRalph Campbell 
260f931551bSRalph Campbell /*
261f931551bSRalph Campbell  * Debugging code and stats updates if no pio buffers available.
262f931551bSRalph Campbell  */
no_send_bufs(struct qib_devdata * dd)263f931551bSRalph Campbell static noinline void no_send_bufs(struct qib_devdata *dd)
264f931551bSRalph Campbell {
265f931551bSRalph Campbell 	dd->upd_pio_shadow = 1;
266f931551bSRalph Campbell 
267f931551bSRalph Campbell 	/* not atomic, but if we lose a stat count in a while, that's OK */
268f931551bSRalph Campbell 	qib_stats.sps_nopiobufs++;
269f931551bSRalph Campbell }
270f931551bSRalph Campbell 
271f931551bSRalph Campbell /*
272f931551bSRalph Campbell  * Common code for normal driver send buffer allocation, and reserved
273f931551bSRalph Campbell  * allocation.
274f931551bSRalph Campbell  *
275f931551bSRalph Campbell  * Do appropriate marking as busy, etc.
276f931551bSRalph Campbell  * Returns buffer pointer if one is found, otherwise NULL.
277f931551bSRalph Campbell  */
qib_getsendbuf_range(struct qib_devdata * dd,u32 * pbufnum,u32 first,u32 last)278f931551bSRalph Campbell u32 __iomem *qib_getsendbuf_range(struct qib_devdata *dd, u32 *pbufnum,
279f931551bSRalph Campbell 				  u32 first, u32 last)
280f931551bSRalph Campbell {
281f931551bSRalph Campbell 	unsigned i, j, updated = 0;
282f931551bSRalph Campbell 	unsigned nbufs;
283f931551bSRalph Campbell 	unsigned long flags;
284f931551bSRalph Campbell 	unsigned long *shadow = dd->pioavailshadow;
285f931551bSRalph Campbell 	u32 __iomem *buf;
286f931551bSRalph Campbell 
287f931551bSRalph Campbell 	if (!(dd->flags & QIB_PRESENT))
288f931551bSRalph Campbell 		return NULL;
289f931551bSRalph Campbell 
290f931551bSRalph Campbell 	nbufs = last - first + 1; /* number in range to check */
291f931551bSRalph Campbell 	if (dd->upd_pio_shadow) {
292bb77a077SMike Marciniszyn update_shadow:
293f931551bSRalph Campbell 		/*
294f931551bSRalph Campbell 		 * Minor optimization.  If we had no buffers on last call,
295f931551bSRalph Campbell 		 * start out by doing the update; continue and do scan even
296f931551bSRalph Campbell 		 * if no buffers were updated, to be paranoid.
297f931551bSRalph Campbell 		 */
298f931551bSRalph Campbell 		update_send_bufs(dd);
299f931551bSRalph Campbell 		updated++;
300f931551bSRalph Campbell 	}
301f931551bSRalph Campbell 	i = first;
302f931551bSRalph Campbell 	/*
303f931551bSRalph Campbell 	 * While test_and_set_bit() is atomic, we do that and then the
304f931551bSRalph Campbell 	 * change_bit(), and the pair is not.  See if this is the cause
305f931551bSRalph Campbell 	 * of the remaining armlaunch errors.
306f931551bSRalph Campbell 	 */
307f931551bSRalph Campbell 	spin_lock_irqsave(&dd->pioavail_lock, flags);
308bb77a077SMike Marciniszyn 	if (dd->last_pio >= first && dd->last_pio <= last)
309bb77a077SMike Marciniszyn 		i = dd->last_pio + 1;
310bb77a077SMike Marciniszyn 	if (!first)
311bb77a077SMike Marciniszyn 		/* adjust to min possible  */
312bb77a077SMike Marciniszyn 		nbufs = last - dd->min_kernel_pio + 1;
313f931551bSRalph Campbell 	for (j = 0; j < nbufs; j++, i++) {
314f931551bSRalph Campbell 		if (i > last)
315bb77a077SMike Marciniszyn 			i = !first ? dd->min_kernel_pio : first;
316f931551bSRalph Campbell 		if (__test_and_set_bit((2 * i) + 1, shadow))
317f931551bSRalph Campbell 			continue;
318f931551bSRalph Campbell 		/* flip generation bit */
319f931551bSRalph Campbell 		__change_bit(2 * i, shadow);
320f931551bSRalph Campbell 		/* remember that the buffer can be written to now */
321f931551bSRalph Campbell 		__set_bit(i, dd->pio_writing);
322bb77a077SMike Marciniszyn 		if (!first && first != last) /* first == last on VL15, avoid */
323bb77a077SMike Marciniszyn 			dd->last_pio = i;
324f931551bSRalph Campbell 		break;
325f931551bSRalph Campbell 	}
326f931551bSRalph Campbell 	spin_unlock_irqrestore(&dd->pioavail_lock, flags);
327f931551bSRalph Campbell 
328f931551bSRalph Campbell 	if (j == nbufs) {
329bb77a077SMike Marciniszyn 		if (!updated)
330f931551bSRalph Campbell 			/*
331f931551bSRalph Campbell 			 * First time through; shadow exhausted, but may be
332f931551bSRalph Campbell 			 * buffers available, try an update and then rescan.
333f931551bSRalph Campbell 			 */
334bb77a077SMike Marciniszyn 			goto update_shadow;
335f931551bSRalph Campbell 		no_send_bufs(dd);
336f931551bSRalph Campbell 		buf = NULL;
337f931551bSRalph Campbell 	} else {
338f931551bSRalph Campbell 		if (i < dd->piobcnt2k)
339f931551bSRalph Campbell 			buf = (u32 __iomem *)(dd->pio2kbase +
340f931551bSRalph Campbell 				i * dd->palign);
341fce24a9dSDave Olson 		else if (i < dd->piobcnt2k + dd->piobcnt4k || !dd->piovl15base)
342f931551bSRalph Campbell 			buf = (u32 __iomem *)(dd->pio4kbase +
343f931551bSRalph Campbell 				(i - dd->piobcnt2k) * dd->align4k);
344fce24a9dSDave Olson 		else
345fce24a9dSDave Olson 			buf = (u32 __iomem *)(dd->piovl15base +
346fce24a9dSDave Olson 				(i - (dd->piobcnt2k + dd->piobcnt4k)) *
347fce24a9dSDave Olson 				dd->align4k);
348f931551bSRalph Campbell 		if (pbufnum)
349f931551bSRalph Campbell 			*pbufnum = i;
350f931551bSRalph Campbell 		dd->upd_pio_shadow = 0;
351f931551bSRalph Campbell 	}
352f931551bSRalph Campbell 
353f931551bSRalph Campbell 	return buf;
354f931551bSRalph Campbell }
355f931551bSRalph Campbell 
356f931551bSRalph Campbell /*
357f931551bSRalph Campbell  * Record that the caller is finished writing to the buffer so we don't
358f931551bSRalph Campbell  * disarm it while it is being written and disarm it now if needed.
359f931551bSRalph Campbell  */
qib_sendbuf_done(struct qib_devdata * dd,unsigned n)360f931551bSRalph Campbell void qib_sendbuf_done(struct qib_devdata *dd, unsigned n)
361f931551bSRalph Campbell {
362f931551bSRalph Campbell 	unsigned long flags;
363f931551bSRalph Campbell 
364f931551bSRalph Campbell 	spin_lock_irqsave(&dd->pioavail_lock, flags);
365f931551bSRalph Campbell 	__clear_bit(n, dd->pio_writing);
366f931551bSRalph Campbell 	if (__test_and_clear_bit(n, dd->pio_need_disarm))
367f931551bSRalph Campbell 		dd->f_sendctrl(dd->pport, QIB_SENDCTRL_DISARM_BUF(n));
368f931551bSRalph Campbell 	spin_unlock_irqrestore(&dd->pioavail_lock, flags);
369f931551bSRalph Campbell }
370f931551bSRalph Campbell 
371f931551bSRalph Campbell /**
372f931551bSRalph Campbell  * qib_chg_pioavailkernel - change which send buffers are available for kernel
373f931551bSRalph Campbell  * @dd: the qlogic_ib device
374f931551bSRalph Campbell  * @start: the starting send buffer number
375f931551bSRalph Campbell  * @len: the number of send buffers
376f931551bSRalph Campbell  * @avail: true if the buffers are available for kernel use, false otherwise
3776b3137feSLee Jones  * @rcd: the context pointer
378f931551bSRalph Campbell  */
qib_chg_pioavailkernel(struct qib_devdata * dd,unsigned start,unsigned len,u32 avail,struct qib_ctxtdata * rcd)379f931551bSRalph Campbell void qib_chg_pioavailkernel(struct qib_devdata *dd, unsigned start,
380f931551bSRalph Campbell 	unsigned len, u32 avail, struct qib_ctxtdata *rcd)
381f931551bSRalph Campbell {
382f931551bSRalph Campbell 	unsigned long flags;
383f931551bSRalph Campbell 	unsigned end;
384f931551bSRalph Campbell 	unsigned ostart = start;
385f931551bSRalph Campbell 
386f931551bSRalph Campbell 	/* There are two bits per send buffer (busy and generation) */
387f931551bSRalph Campbell 	start *= 2;
388f931551bSRalph Campbell 	end = start + len * 2;
389f931551bSRalph Campbell 
390f931551bSRalph Campbell 	spin_lock_irqsave(&dd->pioavail_lock, flags);
391f931551bSRalph Campbell 	/* Set or clear the busy bit in the shadow. */
392f931551bSRalph Campbell 	while (start < end) {
393f931551bSRalph Campbell 		if (avail) {
394f931551bSRalph Campbell 			unsigned long dma;
395f931551bSRalph Campbell 			int i;
396f931551bSRalph Campbell 
397f931551bSRalph Campbell 			/*
398f931551bSRalph Campbell 			 * The BUSY bit will never be set, because we disarm
399f931551bSRalph Campbell 			 * the user buffers before we hand them back to the
400f931551bSRalph Campbell 			 * kernel.  We do have to make sure the generation
401f931551bSRalph Campbell 			 * bit is set correctly in shadow, since it could
402f931551bSRalph Campbell 			 * have changed many times while allocated to user.
403f931551bSRalph Campbell 			 * We can't use the bitmap functions on the full
404f931551bSRalph Campbell 			 * dma array because it is always little-endian, so
405f931551bSRalph Campbell 			 * we have to flip to host-order first.
406f931551bSRalph Campbell 			 * BITS_PER_LONG is slightly wrong, since it's
407f931551bSRalph Campbell 			 * always 64 bits per register in chip...
408f931551bSRalph Campbell 			 * We only work on 64 bit kernels, so that's OK.
409f931551bSRalph Campbell 			 */
410f931551bSRalph Campbell 			i = start / BITS_PER_LONG;
411f931551bSRalph Campbell 			__clear_bit(QLOGIC_IB_SENDPIOAVAIL_BUSY_SHIFT + start,
412f931551bSRalph Campbell 				    dd->pioavailshadow);
413f931551bSRalph Campbell 			dma = (unsigned long)
414f931551bSRalph Campbell 				le64_to_cpu(dd->pioavailregs_dma[i]);
415f931551bSRalph Campbell 			if (test_bit((QLOGIC_IB_SENDPIOAVAIL_CHECK_SHIFT +
416f931551bSRalph Campbell 				      start) % BITS_PER_LONG, &dma))
417f931551bSRalph Campbell 				__set_bit(QLOGIC_IB_SENDPIOAVAIL_CHECK_SHIFT +
418f931551bSRalph Campbell 					  start, dd->pioavailshadow);
419f931551bSRalph Campbell 			else
420f931551bSRalph Campbell 				__clear_bit(QLOGIC_IB_SENDPIOAVAIL_CHECK_SHIFT
421f931551bSRalph Campbell 					    + start, dd->pioavailshadow);
422f931551bSRalph Campbell 			__set_bit(start, dd->pioavailkernel);
423bb77a077SMike Marciniszyn 			if ((start >> 1) < dd->min_kernel_pio)
424bb77a077SMike Marciniszyn 				dd->min_kernel_pio = start >> 1;
425f931551bSRalph Campbell 		} else {
426f931551bSRalph Campbell 			__set_bit(start + QLOGIC_IB_SENDPIOAVAIL_BUSY_SHIFT,
427f931551bSRalph Campbell 				  dd->pioavailshadow);
428f931551bSRalph Campbell 			__clear_bit(start, dd->pioavailkernel);
429bb77a077SMike Marciniszyn 			if ((start >> 1) > dd->min_kernel_pio)
430bb77a077SMike Marciniszyn 				dd->min_kernel_pio = start >> 1;
431f931551bSRalph Campbell 		}
432f931551bSRalph Campbell 		start += 2;
433f931551bSRalph Campbell 	}
434f931551bSRalph Campbell 
435bb77a077SMike Marciniszyn 	if (dd->min_kernel_pio > 0 && dd->last_pio < dd->min_kernel_pio - 1)
436bb77a077SMike Marciniszyn 		dd->last_pio = dd->min_kernel_pio - 1;
437f931551bSRalph Campbell 	spin_unlock_irqrestore(&dd->pioavail_lock, flags);
438f931551bSRalph Campbell 
439f931551bSRalph Campbell 	dd->f_txchk_change(dd, ostart, len, avail, rcd);
440f931551bSRalph Campbell }
441f931551bSRalph Campbell 
442f931551bSRalph Campbell /*
443f931551bSRalph Campbell  * Flush all sends that might be in the ready to send state, as well as any
444f931551bSRalph Campbell  * that are in the process of being sent.  Used whenever we need to be
445f931551bSRalph Campbell  * sure the send side is idle.  Cleans up all buffer state by canceling
446f931551bSRalph Campbell  * all pio buffers, and issuing an abort, which cleans up anything in the
447f931551bSRalph Campbell  * launch fifo.  The cancel is superfluous on some chip versions, but
448f931551bSRalph Campbell  * it's safer to always do it.
449f931551bSRalph Campbell  * PIOAvail bits are updated by the chip as if a normal send had happened.
450f931551bSRalph Campbell  */
qib_cancel_sends(struct qib_pportdata * ppd)451f931551bSRalph Campbell void qib_cancel_sends(struct qib_pportdata *ppd)
452f931551bSRalph Campbell {
453f931551bSRalph Campbell 	struct qib_devdata *dd = ppd->dd;
454f931551bSRalph Campbell 	struct qib_ctxtdata *rcd;
455f931551bSRalph Campbell 	unsigned long flags;
456f931551bSRalph Campbell 	unsigned ctxt;
457f931551bSRalph Campbell 	unsigned i;
458f931551bSRalph Campbell 	unsigned last;
459f931551bSRalph Campbell 
460f931551bSRalph Campbell 	/*
461f931551bSRalph Campbell 	 * Tell PSM to disarm buffers again before trying to reuse them.
462f931551bSRalph Campbell 	 * We need to be sure the rcd doesn't change out from under us
463f931551bSRalph Campbell 	 * while we do so.  We hold the two locks sequentially.  We might
464f931551bSRalph Campbell 	 * needlessly set some need_disarm bits as a result, if the
465f931551bSRalph Campbell 	 * context is closed after we release the uctxt_lock, but that's
466f931551bSRalph Campbell 	 * fairly benign, and safer than nesting the locks.
467f931551bSRalph Campbell 	 */
468f931551bSRalph Campbell 	for (ctxt = dd->first_user_ctxt; ctxt < dd->cfgctxts; ctxt++) {
469f931551bSRalph Campbell 		spin_lock_irqsave(&dd->uctxt_lock, flags);
470f931551bSRalph Campbell 		rcd = dd->rcd[ctxt];
471f931551bSRalph Campbell 		if (rcd && rcd->ppd == ppd) {
472f931551bSRalph Campbell 			last = rcd->pio_base + rcd->piocnt;
473f931551bSRalph Campbell 			if (rcd->user_event_mask) {
474f931551bSRalph Campbell 				/*
475f931551bSRalph Campbell 				 * subctxt_cnt is 0 if not shared, so do base
476f931551bSRalph Campbell 				 * separately, first, then remaining subctxt,
477f931551bSRalph Campbell 				 * if any
478f931551bSRalph Campbell 				 */
479f931551bSRalph Campbell 				set_bit(_QIB_EVENT_DISARM_BUFS_BIT,
480f931551bSRalph Campbell 					&rcd->user_event_mask[0]);
481f931551bSRalph Campbell 				for (i = 1; i < rcd->subctxt_cnt; i++)
482f931551bSRalph Campbell 					set_bit(_QIB_EVENT_DISARM_BUFS_BIT,
483f931551bSRalph Campbell 						&rcd->user_event_mask[i]);
484f931551bSRalph Campbell 			}
485f931551bSRalph Campbell 			i = rcd->pio_base;
486f931551bSRalph Campbell 			spin_unlock_irqrestore(&dd->uctxt_lock, flags);
487f931551bSRalph Campbell 			spin_lock_irqsave(&dd->pioavail_lock, flags);
488f931551bSRalph Campbell 			for (; i < last; i++)
489f931551bSRalph Campbell 				__set_bit(i, dd->pio_need_disarm);
490f931551bSRalph Campbell 			spin_unlock_irqrestore(&dd->pioavail_lock, flags);
491f931551bSRalph Campbell 		} else
492f931551bSRalph Campbell 			spin_unlock_irqrestore(&dd->uctxt_lock, flags);
493f931551bSRalph Campbell 	}
494f931551bSRalph Campbell 
495f931551bSRalph Campbell 	if (!(dd->flags & QIB_HAS_SEND_DMA))
496f931551bSRalph Campbell 		dd->f_sendctrl(ppd, QIB_SENDCTRL_DISARM_ALL |
497f931551bSRalph Campbell 				    QIB_SENDCTRL_FLUSH);
498f931551bSRalph Campbell }
499f931551bSRalph Campbell 
500f931551bSRalph Campbell /*
501f931551bSRalph Campbell  * Force an update of in-memory copy of the pioavail registers, when
502f931551bSRalph Campbell  * needed for any of a variety of reasons.
503f931551bSRalph Campbell  * If already off, this routine is a nop, on the assumption that the
504f931551bSRalph Campbell  * caller (or set of callers) will "do the right thing".
505f931551bSRalph Campbell  * This is a per-device operation, so just the first port.
506f931551bSRalph Campbell  */
qib_force_pio_avail_update(struct qib_devdata * dd)507f931551bSRalph Campbell void qib_force_pio_avail_update(struct qib_devdata *dd)
508f931551bSRalph Campbell {
509f931551bSRalph Campbell 	dd->f_sendctrl(dd->pport, QIB_SENDCTRL_AVAIL_BLIP);
510f931551bSRalph Campbell }
511f931551bSRalph Campbell 
qib_hol_down(struct qib_pportdata * ppd)512f931551bSRalph Campbell void qib_hol_down(struct qib_pportdata *ppd)
513f931551bSRalph Campbell {
514f931551bSRalph Campbell 	/*
515f931551bSRalph Campbell 	 * Cancel sends when the link goes DOWN so that we aren't doing it
516f931551bSRalph Campbell 	 * at INIT when we might be trying to send SMI packets.
517f931551bSRalph Campbell 	 */
518f931551bSRalph Campbell 	if (!(ppd->lflags & QIBL_IB_AUTONEG_INPROG))
519f931551bSRalph Campbell 		qib_cancel_sends(ppd);
520f931551bSRalph Campbell }
521f931551bSRalph Campbell 
522f931551bSRalph Campbell /*
523f931551bSRalph Campbell  * Link is at INIT.
524f931551bSRalph Campbell  * We start the HoL timer so we can detect stuck packets blocking SMP replies.
525f931551bSRalph Campbell  * Timer may already be running, so use mod_timer, not add_timer.
526f931551bSRalph Campbell  */
qib_hol_init(struct qib_pportdata * ppd)527f931551bSRalph Campbell void qib_hol_init(struct qib_pportdata *ppd)
528f931551bSRalph Campbell {
529f931551bSRalph Campbell 	if (ppd->hol_state != QIB_HOL_INIT) {
530f931551bSRalph Campbell 		ppd->hol_state = QIB_HOL_INIT;
531f931551bSRalph Campbell 		mod_timer(&ppd->hol_timer,
532f931551bSRalph Campbell 			  jiffies + msecs_to_jiffies(qib_hol_timeout_ms));
533f931551bSRalph Campbell 	}
534f931551bSRalph Campbell }
535f931551bSRalph Campbell 
536f931551bSRalph Campbell /*
537f931551bSRalph Campbell  * Link is up, continue any user processes, and ensure timer
538f931551bSRalph Campbell  * is a nop, if running.  Let timer keep running, if set; it
539f931551bSRalph Campbell  * will nop when it sees the link is up.
540f931551bSRalph Campbell  */
qib_hol_up(struct qib_pportdata * ppd)541f931551bSRalph Campbell void qib_hol_up(struct qib_pportdata *ppd)
542f931551bSRalph Campbell {
543f931551bSRalph Campbell 	ppd->hol_state = QIB_HOL_UP;
544f931551bSRalph Campbell }
545f931551bSRalph Campbell 
546f931551bSRalph Campbell /*
547f931551bSRalph Campbell  * This is only called via the timer.
548f931551bSRalph Campbell  */
qib_hol_event(struct timer_list * t)5494037c92fSKees Cook void qib_hol_event(struct timer_list *t)
550f931551bSRalph Campbell {
5514037c92fSKees Cook 	struct qib_pportdata *ppd = from_timer(ppd, t, hol_timer);
552f931551bSRalph Campbell 
553f931551bSRalph Campbell 	/* If hardware error, etc, skip. */
554f931551bSRalph Campbell 	if (!(ppd->dd->flags & QIB_INITTED))
555f931551bSRalph Campbell 		return;
556f931551bSRalph Campbell 
557f931551bSRalph Campbell 	if (ppd->hol_state != QIB_HOL_UP) {
558f931551bSRalph Campbell 		/*
559f931551bSRalph Campbell 		 * Try to flush sends in case a stuck packet is blocking
560f931551bSRalph Campbell 		 * SMP replies.
561f931551bSRalph Campbell 		 */
562f931551bSRalph Campbell 		qib_hol_down(ppd);
563f931551bSRalph Campbell 		mod_timer(&ppd->hol_timer,
564f931551bSRalph Campbell 			  jiffies + msecs_to_jiffies(qib_hol_timeout_ms));
565f931551bSRalph Campbell 	}
566f931551bSRalph Campbell }
567