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