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