xref: /openbmc/linux/drivers/usb/host/ehci-q.c (revision 4f2c0a4acffbec01079c28f839422e64ddeff004)
15fd54aceSGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0+
21da177e4SLinus Torvalds /*
3d49d4317SDavid Brownell  * Copyright (C) 2001-2004 by David Brownell
41da177e4SLinus Torvalds  */
51da177e4SLinus Torvalds 
61da177e4SLinus Torvalds /* this file is part of ehci-hcd.c */
71da177e4SLinus Torvalds 
81da177e4SLinus Torvalds /*-------------------------------------------------------------------------*/
91da177e4SLinus Torvalds 
101da177e4SLinus Torvalds /*
111da177e4SLinus Torvalds  * EHCI hardware queue manipulation ... the core.  QH/QTD manipulation.
121da177e4SLinus Torvalds  *
131da177e4SLinus Torvalds  * Control, bulk, and interrupt traffic all use "qh" lists.  They list "qtd"
141da177e4SLinus Torvalds  * entries describing USB transactions, max 16-20kB/entry (with 4kB-aligned
151da177e4SLinus Torvalds  * buffers needed for the larger number).  We use one QH per endpoint, queue
161da177e4SLinus Torvalds  * multiple urbs (all three types) per endpoint.  URBs may need several qtds.
171da177e4SLinus Torvalds  *
181da177e4SLinus Torvalds  * ISO traffic uses "ISO TD" (itd, and sitd) records, and (along with
191da177e4SLinus Torvalds  * interrupts) needs careful scheduling.  Performance improvements can be
201da177e4SLinus Torvalds  * an ongoing challenge.  That's in "ehci-sched.c".
211da177e4SLinus Torvalds  *
221da177e4SLinus Torvalds  * USB 1.1 devices are handled (a) by "companion" OHCI or UHCI root hubs,
231da177e4SLinus Torvalds  * or otherwise through transaction translators (TTs) in USB 2.0 hubs using
241da177e4SLinus Torvalds  * (b) special fields in qh entries or (c) split iso entries.  TTs will
251da177e4SLinus Torvalds  * buffer low/full speed data so the host collects it at high speed.
261da177e4SLinus Torvalds  */
271da177e4SLinus Torvalds 
281da177e4SLinus Torvalds /*-------------------------------------------------------------------------*/
291da177e4SLinus Torvalds 
3064cc3f12SErkka Talvitie /* PID Codes that are used here, from EHCI specification, Table 3-16. */
3164cc3f12SErkka Talvitie #define PID_CODE_IN    1
3264cc3f12SErkka Talvitie #define PID_CODE_SETUP 2
3364cc3f12SErkka Talvitie 
341da177e4SLinus Torvalds /* fill a qtd, returning how much of the buffer we were able to queue up */
351da177e4SLinus Torvalds 
36c6c986b6SSergey Shtylyov static unsigned int
qtd_fill(struct ehci_hcd * ehci,struct ehci_qtd * qtd,dma_addr_t buf,size_t len,int token,int maxpacket)376dbd682bSStefan Roese qtd_fill(struct ehci_hcd *ehci, struct ehci_qtd *qtd, dma_addr_t buf,
386dbd682bSStefan Roese 		  size_t len, int token, int maxpacket)
391da177e4SLinus Torvalds {
40c6c986b6SSergey Shtylyov 	unsigned int count;
411da177e4SLinus Torvalds 	u64	addr = buf;
42c6c986b6SSergey Shtylyov 	int	i;
431da177e4SLinus Torvalds 
441da177e4SLinus Torvalds 	/* one buffer entry per 4K ... first might be short or unaligned */
456dbd682bSStefan Roese 	qtd->hw_buf[0] = cpu_to_hc32(ehci, (u32)addr);
466dbd682bSStefan Roese 	qtd->hw_buf_hi[0] = cpu_to_hc32(ehci, (u32)(addr >> 32));
471da177e4SLinus Torvalds 	count = 0x1000 - (buf & 0x0fff);	/* rest of that page */
481da177e4SLinus Torvalds 	if (likely (len < count))		/* ... iff needed */
491da177e4SLinus Torvalds 		count = len;
501da177e4SLinus Torvalds 	else {
511da177e4SLinus Torvalds 		buf +=  0x1000;
521da177e4SLinus Torvalds 		buf &= ~0x0fff;
531da177e4SLinus Torvalds 
541da177e4SLinus Torvalds 		/* per-qtd limit: from 16K to 20K (best alignment) */
551da177e4SLinus Torvalds 		for (i = 1; count < len && i < 5; i++) {
561da177e4SLinus Torvalds 			addr = buf;
576dbd682bSStefan Roese 			qtd->hw_buf[i] = cpu_to_hc32(ehci, (u32)addr);
586dbd682bSStefan Roese 			qtd->hw_buf_hi[i] = cpu_to_hc32(ehci,
596dbd682bSStefan Roese 					(u32)(addr >> 32));
601da177e4SLinus Torvalds 			buf += 0x1000;
611da177e4SLinus Torvalds 			if ((count + 0x1000) < len)
621da177e4SLinus Torvalds 				count += 0x1000;
631da177e4SLinus Torvalds 			else
641da177e4SLinus Torvalds 				count = len;
651da177e4SLinus Torvalds 		}
661da177e4SLinus Torvalds 
671da177e4SLinus Torvalds 		/* short packets may only terminate transfers */
681da177e4SLinus Torvalds 		if (count != len)
691da177e4SLinus Torvalds 			count -= (count % maxpacket);
701da177e4SLinus Torvalds 	}
716dbd682bSStefan Roese 	qtd->hw_token = cpu_to_hc32(ehci, (count << 16) | token);
721da177e4SLinus Torvalds 	qtd->length = count;
731da177e4SLinus Torvalds 
741da177e4SLinus Torvalds 	return count;
751da177e4SLinus Torvalds }
761da177e4SLinus Torvalds 
771da177e4SLinus Torvalds /*-------------------------------------------------------------------------*/
781da177e4SLinus Torvalds 
791da177e4SLinus Torvalds static inline void
qh_update(struct ehci_hcd * ehci,struct ehci_qh * qh,struct ehci_qtd * qtd)801da177e4SLinus Torvalds qh_update (struct ehci_hcd *ehci, struct ehci_qh *qh, struct ehci_qtd *qtd)
811da177e4SLinus Torvalds {
823807e26dSAlek Du 	struct ehci_qh_hw *hw = qh->hw;
833807e26dSAlek Du 
841da177e4SLinus Torvalds 	/* writes to an active overlay are unsafe */
85c1fdb68eSAlan Stern 	WARN_ON(qh->qh_state != QH_STATE_IDLE);
861da177e4SLinus Torvalds 
873807e26dSAlek Du 	hw->hw_qtd_next = QTD_NEXT(ehci, qtd->qtd_dma);
883807e26dSAlek Du 	hw->hw_alt_next = EHCI_LIST_END(ehci);
891da177e4SLinus Torvalds 
90a455212dSAlan Stern 	/* Except for control endpoints, we make hardware maintain data
91a455212dSAlan Stern 	 * toggle (like OHCI) ... here (re)initialize the toggle in the QH,
92a455212dSAlan Stern 	 * and set the pseudo-toggle in udev. Only usb_clear_halt() will
93a455212dSAlan Stern 	 * ever clear it.
94a455212dSAlan Stern 	 */
954c53de72SAlan Stern 	if (!(hw->hw_info1 & cpu_to_hc32(ehci, QH_TOGGLE_CTL))) {
96a455212dSAlan Stern 		unsigned	is_out, epnum;
97a455212dSAlan Stern 
98e04f5f7eSAlan Stern 		is_out = qh->is_out;
993807e26dSAlek Du 		epnum = (hc32_to_cpup(ehci, &hw->hw_info1) >> 8) & 0x0f;
100ffa0248eSAlan Stern 		if (unlikely(!usb_gettoggle(qh->ps.udev, epnum, is_out))) {
1013807e26dSAlek Du 			hw->hw_token &= ~cpu_to_hc32(ehci, QTD_TOGGLE);
102ffa0248eSAlan Stern 			usb_settoggle(qh->ps.udev, epnum, is_out, 1);
103a455212dSAlan Stern 		}
104a455212dSAlan Stern 	}
105a455212dSAlan Stern 
1063807e26dSAlek Du 	hw->hw_token &= cpu_to_hc32(ehci, QTD_TOGGLE | QTD_STS_PING);
1071da177e4SLinus Torvalds }
1081da177e4SLinus Torvalds 
1091da177e4SLinus Torvalds /* if it weren't for a common silicon quirk (writing the dummy into the qh
1101da177e4SLinus Torvalds  * overlay, so qh->hw_token wrongly becomes inactive/halted), only fault
1111da177e4SLinus Torvalds  * recovery (including urb dequeue) would need software changes to a QH...
1121da177e4SLinus Torvalds  */
1131da177e4SLinus Torvalds static void
qh_refresh(struct ehci_hcd * ehci,struct ehci_qh * qh)1141da177e4SLinus Torvalds qh_refresh (struct ehci_hcd *ehci, struct ehci_qh *qh)
1151da177e4SLinus Torvalds {
1161da177e4SLinus Torvalds 	struct ehci_qtd *qtd;
1171da177e4SLinus Torvalds 
118c1fdb68eSAlan Stern 	qtd = list_entry(qh->qtd_list.next, struct ehci_qtd, qtd_list);
119c1fdb68eSAlan Stern 
1203d037774SPavankumar Kondeti 	/*
1213d037774SPavankumar Kondeti 	 * first qtd may already be partially processed.
1223d037774SPavankumar Kondeti 	 * If we come here during unlink, the QH overlay region
1233d037774SPavankumar Kondeti 	 * might have reference to the just unlinked qtd. The
1243d037774SPavankumar Kondeti 	 * qtd is updated in qh_completions(). Update the QH
1253d037774SPavankumar Kondeti 	 * overlay here.
1263d037774SPavankumar Kondeti 	 */
127fc0855f2SAlan Stern 	if (qh->hw->hw_token & ACTIVE_BIT(ehci)) {
1283d037774SPavankumar Kondeti 		qh->hw->hw_qtd_next = qtd->hw_next;
129fc0855f2SAlan Stern 		if (qh->should_be_inactive)
130fc0855f2SAlan Stern 			ehci_warn(ehci, "qh %p should be inactive!\n", qh);
131fc0855f2SAlan Stern 	} else {
1321da177e4SLinus Torvalds 		qh_update(ehci, qh, qtd);
1331da177e4SLinus Torvalds 	}
134fc0855f2SAlan Stern 	qh->should_be_inactive = 0;
135fc0855f2SAlan Stern }
1361da177e4SLinus Torvalds 
1371da177e4SLinus Torvalds /*-------------------------------------------------------------------------*/
1381da177e4SLinus Torvalds 
139914b7012SAlan Stern static void qh_link_async(struct ehci_hcd *ehci, struct ehci_qh *qh);
140914b7012SAlan Stern 
ehci_clear_tt_buffer_complete(struct usb_hcd * hcd,struct usb_host_endpoint * ep)141914b7012SAlan Stern static void ehci_clear_tt_buffer_complete(struct usb_hcd *hcd,
142914b7012SAlan Stern 		struct usb_host_endpoint *ep)
143914b7012SAlan Stern {
144914b7012SAlan Stern 	struct ehci_hcd		*ehci = hcd_to_ehci(hcd);
145914b7012SAlan Stern 	struct ehci_qh		*qh = ep->hcpriv;
146914b7012SAlan Stern 	unsigned long		flags;
147914b7012SAlan Stern 
148914b7012SAlan Stern 	spin_lock_irqsave(&ehci->lock, flags);
149914b7012SAlan Stern 	qh->clearing_tt = 0;
150914b7012SAlan Stern 	if (qh->qh_state == QH_STATE_IDLE && !list_empty(&qh->qtd_list)
151e8799906SAlan Stern 			&& ehci->rh_state == EHCI_RH_RUNNING)
152914b7012SAlan Stern 		qh_link_async(ehci, qh);
153914b7012SAlan Stern 	spin_unlock_irqrestore(&ehci->lock, flags);
154914b7012SAlan Stern }
155914b7012SAlan Stern 
ehci_clear_tt_buffer(struct ehci_hcd * ehci,struct ehci_qh * qh,struct urb * urb,u32 token)156914b7012SAlan Stern static void ehci_clear_tt_buffer(struct ehci_hcd *ehci, struct ehci_qh *qh,
157914b7012SAlan Stern 		struct urb *urb, u32 token)
158914b7012SAlan Stern {
159914b7012SAlan Stern 
160914b7012SAlan Stern 	/* If an async split transaction gets an error or is unlinked,
161914b7012SAlan Stern 	 * the TT buffer may be left in an indeterminate state.  We
162914b7012SAlan Stern 	 * have to clear the TT buffer.
163914b7012SAlan Stern 	 *
164914b7012SAlan Stern 	 * Note: this routine is never called for Isochronous transfers.
165914b7012SAlan Stern 	 */
166914b7012SAlan Stern 	if (urb->dev->tt && !usb_pipeint(urb->pipe) && !qh->clearing_tt) {
1671c20163dSOliver Neukum #ifdef CONFIG_DYNAMIC_DEBUG
168914b7012SAlan Stern 		struct usb_device *tt = urb->dev->tt->hub;
169914b7012SAlan Stern 		dev_dbg(&tt->dev,
170914b7012SAlan Stern 			"clear tt buffer port %d, a%d ep%d t%08x\n",
171914b7012SAlan Stern 			urb->dev->ttport, urb->dev->devnum,
172914b7012SAlan Stern 			usb_pipeendpoint(urb->pipe), token);
1731c20163dSOliver Neukum #endif /* CONFIG_DYNAMIC_DEBUG */
174914b7012SAlan Stern 		if (!ehci_is_TDI(ehci)
175914b7012SAlan Stern 				|| urb->dev->tt->hub !=
176914b7012SAlan Stern 				   ehci_to_hcd(ehci)->self.root_hub) {
177914b7012SAlan Stern 			if (usb_hub_clear_tt_buffer(urb) == 0)
178914b7012SAlan Stern 				qh->clearing_tt = 1;
179914b7012SAlan Stern 		} else {
180914b7012SAlan Stern 
181914b7012SAlan Stern 			/* REVISIT ARC-derived cores don't clear the root
182914b7012SAlan Stern 			 * hub TT buffer in this way...
183914b7012SAlan Stern 			 */
184914b7012SAlan Stern 		}
185914b7012SAlan Stern 	}
186914b7012SAlan Stern }
187914b7012SAlan Stern 
qtd_copy_status(struct ehci_hcd * ehci,struct urb * urb,size_t length,u32 token)18814c04c0fSAlan Stern static int qtd_copy_status (
1891da177e4SLinus Torvalds 	struct ehci_hcd *ehci,
1901da177e4SLinus Torvalds 	struct urb *urb,
1911da177e4SLinus Torvalds 	size_t length,
1921da177e4SLinus Torvalds 	u32 token
1931da177e4SLinus Torvalds )
1941da177e4SLinus Torvalds {
19514c04c0fSAlan Stern 	int	status = -EINPROGRESS;
19614c04c0fSAlan Stern 
1971da177e4SLinus Torvalds 	/* count IN/OUT bytes, not SETUP (even short packets) */
19864cc3f12SErkka Talvitie 	if (likely(QTD_PID(token) != PID_CODE_SETUP))
1991da177e4SLinus Torvalds 		urb->actual_length += length - QTD_LENGTH (token);
2001da177e4SLinus Torvalds 
2011da177e4SLinus Torvalds 	/* don't modify error codes */
202eb231054SAlan Stern 	if (unlikely(urb->unlinked))
20314c04c0fSAlan Stern 		return status;
2041da177e4SLinus Torvalds 
2051da177e4SLinus Torvalds 	/* force cleanup after short read; not always an error */
2061da177e4SLinus Torvalds 	if (unlikely (IS_SHORT_READ (token)))
20714c04c0fSAlan Stern 		status = -EREMOTEIO;
2081da177e4SLinus Torvalds 
2091da177e4SLinus Torvalds 	/* serious "can't proceed" faults reported by the hardware */
2101da177e4SLinus Torvalds 	if (token & QTD_STS_HALT) {
2111da177e4SLinus Torvalds 		if (token & QTD_STS_BABBLE) {
2121da177e4SLinus Torvalds 			/* FIXME "must" disable babbling device's port too */
21314c04c0fSAlan Stern 			status = -EOVERFLOW;
21464cc3f12SErkka Talvitie 		/*
21564cc3f12SErkka Talvitie 		 * When MMF is active and PID Code is IN, queue is halted.
21664cc3f12SErkka Talvitie 		 * EHCI Specification, Table 4-13.
21764cc3f12SErkka Talvitie 		 */
21864cc3f12SErkka Talvitie 		} else if ((token & QTD_STS_MMF) &&
21964cc3f12SErkka Talvitie 					(QTD_PID(token) == PID_CODE_IN)) {
22064cc3f12SErkka Talvitie 			status = -EPROTO;
221ba516de3SAlan Stern 		/* CERR nonzero + halt --> stall */
222ba516de3SAlan Stern 		} else if (QTD_CERR(token)) {
223ba516de3SAlan Stern 			status = -EPIPE;
224ba516de3SAlan Stern 
225ba516de3SAlan Stern 		/* In theory, more than one of the following bits can be set
226ba516de3SAlan Stern 		 * since they are sticky and the transaction is retried.
227ba516de3SAlan Stern 		 * Which to test first is rather arbitrary.
228ba516de3SAlan Stern 		 */
2291da177e4SLinus Torvalds 		} else if (token & QTD_STS_MMF) {
2301da177e4SLinus Torvalds 			/* fs/ls interrupt xfer missed the complete-split */
23114c04c0fSAlan Stern 			status = -EPROTO;
2321da177e4SLinus Torvalds 		} else if (token & QTD_STS_DBE) {
23314c04c0fSAlan Stern 			status = (QTD_PID (token) == 1) /* IN ? */
2341da177e4SLinus Torvalds 				? -ENOSR  /* hc couldn't read data */
2351da177e4SLinus Torvalds 				: -ECOMM; /* hc couldn't write data */
2361da177e4SLinus Torvalds 		} else if (token & QTD_STS_XACT) {
237ba516de3SAlan Stern 			/* timeout, bad CRC, wrong PID, etc */
2381da177e4SLinus Torvalds 			ehci_dbg(ehci, "devpath %s ep%d%s 3strikes\n",
2391da177e4SLinus Torvalds 				urb->dev->devpath,
2401da177e4SLinus Torvalds 				usb_pipeendpoint(urb->pipe),
2411da177e4SLinus Torvalds 				usb_pipein(urb->pipe) ? "in" : "out");
24214c04c0fSAlan Stern 			status = -EPROTO;
243ba516de3SAlan Stern 		} else {	/* unknown */
24414c04c0fSAlan Stern 			status = -EPROTO;
245ba516de3SAlan Stern 		}
2461da177e4SLinus Torvalds 	}
24714c04c0fSAlan Stern 
24814c04c0fSAlan Stern 	return status;
2491da177e4SLinus Torvalds }
2501da177e4SLinus Torvalds 
2511da177e4SLinus Torvalds static void
ehci_urb_done(struct ehci_hcd * ehci,struct urb * urb,int status)25214c04c0fSAlan Stern ehci_urb_done(struct ehci_hcd *ehci, struct urb *urb, int status)
2531da177e4SLinus Torvalds {
2542656a9abSAlan Stern 	if (usb_pipetype(urb->pipe) == PIPE_INTERRUPT) {
2552656a9abSAlan Stern 		/* ... update hc-wide periodic stats */
2561da177e4SLinus Torvalds 		ehci_to_hcd(ehci)->self.bandwidth_int_reqs--;
2571da177e4SLinus Torvalds 	}
2581da177e4SLinus Torvalds 
259eb231054SAlan Stern 	if (unlikely(urb->unlinked)) {
260a0ef2bdfSCorentin Labbe 		INCR(ehci->stats.unlink);
261eb231054SAlan Stern 	} else {
2624f667627SDavid Brownell 		/* report non-error and short read status as zero */
2634f667627SDavid Brownell 		if (status == -EINPROGRESS || status == -EREMOTEIO)
26414c04c0fSAlan Stern 			status = 0;
265a0ef2bdfSCorentin Labbe 		INCR(ehci->stats.complete);
2661da177e4SLinus Torvalds 	}
2671da177e4SLinus Torvalds 
2681da177e4SLinus Torvalds #ifdef EHCI_URB_TRACE
2691da177e4SLinus Torvalds 	ehci_dbg (ehci,
2701da177e4SLinus Torvalds 		"%s %s urb %p ep%d%s status %d len %d/%d\n",
271441b62c1SHarvey Harrison 		__func__, urb->dev->devpath, urb,
2721da177e4SLinus Torvalds 		usb_pipeendpoint (urb->pipe),
2731da177e4SLinus Torvalds 		usb_pipein (urb->pipe) ? "in" : "out",
27414c04c0fSAlan Stern 		status,
2751da177e4SLinus Torvalds 		urb->actual_length, urb->transfer_buffer_length);
2761da177e4SLinus Torvalds #endif
2771da177e4SLinus Torvalds 
278e9df41c5SAlan Stern 	usb_hcd_unlink_urb_from_ep(ehci_to_hcd(ehci), urb);
2794a00027dSAlan Stern 	usb_hcd_giveback_urb(ehci_to_hcd(ehci), urb, status);
2801da177e4SLinus Torvalds }
2811da177e4SLinus Torvalds 
2821da177e4SLinus Torvalds static int qh_schedule (struct ehci_hcd *ehci, struct ehci_qh *qh);
2831da177e4SLinus Torvalds 
2841da177e4SLinus Torvalds /*
2851da177e4SLinus Torvalds  * Process and free completed qtds for a qh, returning URBs to drivers.
28679bcf7b0SAlan Stern  * Chases up to qh->hw_current.  Returns nonzero if the caller should
28779bcf7b0SAlan Stern  * unlink qh.
2881da177e4SLinus Torvalds  */
2891da177e4SLinus Torvalds static unsigned
qh_completions(struct ehci_hcd * ehci,struct ehci_qh * qh)2907d12e780SDavid Howells qh_completions (struct ehci_hcd *ehci, struct ehci_qh *qh)
2911da177e4SLinus Torvalds {
2923a44494eSAlan Stern 	struct ehci_qtd		*last, *end = qh->dummy;
2931da177e4SLinus Torvalds 	struct list_head	*entry, *tmp;
2943a44494eSAlan Stern 	int			last_status;
2951da177e4SLinus Torvalds 	int			stopped;
2961da177e4SLinus Torvalds 	u8			state;
2973807e26dSAlek Du 	struct ehci_qh_hw	*hw = qh->hw;
2981da177e4SLinus Torvalds 
2991da177e4SLinus Torvalds 	/* completions (or tasks on other cpus) must never clobber HALT
3001da177e4SLinus Torvalds 	 * till we've gone through and cleaned everything up, even when
3011da177e4SLinus Torvalds 	 * they add urbs to this qh's queue or mark them for unlinking.
3021da177e4SLinus Torvalds 	 *
3031da177e4SLinus Torvalds 	 * NOTE:  unlinking expects to be done in queue order.
3043a44494eSAlan Stern 	 *
3053a44494eSAlan Stern 	 * It's a bug for qh->qh_state to be anything other than
3063a44494eSAlan Stern 	 * QH_STATE_IDLE, unless our caller is scan_async() or
307569b394fSAlan Stern 	 * scan_intr().
3081da177e4SLinus Torvalds 	 */
3091da177e4SLinus Torvalds 	state = qh->qh_state;
3101da177e4SLinus Torvalds 	qh->qh_state = QH_STATE_COMPLETING;
3111da177e4SLinus Torvalds 	stopped = (state == QH_STATE_IDLE);
3121da177e4SLinus Torvalds 
3133a44494eSAlan Stern  rescan:
3143a44494eSAlan Stern 	last = NULL;
3153a44494eSAlan Stern 	last_status = -EINPROGRESS;
3167bc782d7SAlan Stern 	qh->dequeue_during_giveback = 0;
3173a44494eSAlan Stern 
3181da177e4SLinus Torvalds 	/* remove de-activated QTDs from front of queue.
3191da177e4SLinus Torvalds 	 * after faults (including short reads), cleanup this urb
3201da177e4SLinus Torvalds 	 * then let the queue advance.
3211da177e4SLinus Torvalds 	 * if queue is stopped, handles unlinks.
3221da177e4SLinus Torvalds 	 */
3231da177e4SLinus Torvalds 	list_for_each_safe (entry, tmp, &qh->qtd_list) {
3241da177e4SLinus Torvalds 		struct ehci_qtd	*qtd;
3251da177e4SLinus Torvalds 		struct urb	*urb;
3261da177e4SLinus Torvalds 		u32		token = 0;
3271da177e4SLinus Torvalds 
3281da177e4SLinus Torvalds 		qtd = list_entry (entry, struct ehci_qtd, qtd_list);
3291da177e4SLinus Torvalds 		urb = qtd->urb;
3301da177e4SLinus Torvalds 
3311da177e4SLinus Torvalds 		/* clean up any state from previous QTD ...*/
3321da177e4SLinus Torvalds 		if (last) {
3331da177e4SLinus Torvalds 			if (likely (last->urb != urb)) {
33414c04c0fSAlan Stern 				ehci_urb_done(ehci, last->urb, last_status);
335b5f7a0ecSMisha Zhilin 				last_status = -EINPROGRESS;
3361da177e4SLinus Torvalds 			}
3371da177e4SLinus Torvalds 			ehci_qtd_free (ehci, last);
3381da177e4SLinus Torvalds 			last = NULL;
3391da177e4SLinus Torvalds 		}
3401da177e4SLinus Torvalds 
3411da177e4SLinus Torvalds 		/* ignore urbs submitted during completions we reported */
3421da177e4SLinus Torvalds 		if (qtd == end)
3431da177e4SLinus Torvalds 			break;
3441da177e4SLinus Torvalds 
3451da177e4SLinus Torvalds 		/* hardware copies qtd out of qh overlay */
3461da177e4SLinus Torvalds 		rmb ();
3476dbd682bSStefan Roese 		token = hc32_to_cpu(ehci, qtd->hw_token);
3481da177e4SLinus Torvalds 
3491da177e4SLinus Torvalds 		/* always clean up qtds the hc de-activated */
350a2c2706eSAlan Stern  retry_xacterr:
3511da177e4SLinus Torvalds 		if ((token & QTD_STS_ACTIVE) == 0) {
3521da177e4SLinus Torvalds 
353332960bdSVikram Pandita 			/* Report Data Buffer Error: non-fatal but useful */
354332960bdSVikram Pandita 			if (token & QTD_STS_DBE)
355332960bdSVikram Pandita 				ehci_dbg(ehci,
356332960bdSVikram Pandita 					"detected DataBufferErr for urb %p ep%d%s len %d, qtd %p [qh %p]\n",
357332960bdSVikram Pandita 					urb,
358332960bdSVikram Pandita 					usb_endpoint_num(&urb->ep->desc),
359332960bdSVikram Pandita 					usb_endpoint_dir_in(&urb->ep->desc) ? "in" : "out",
360332960bdSVikram Pandita 					urb->transfer_buffer_length,
361332960bdSVikram Pandita 					qtd,
362332960bdSVikram Pandita 					qh);
363332960bdSVikram Pandita 
364a082b5c7SDavid Brownell 			/* on STALL, error, and short reads this urb must
365a082b5c7SDavid Brownell 			 * complete and all its qtds must be recycled.
366a082b5c7SDavid Brownell 			 */
3671da177e4SLinus Torvalds 			if ((token & QTD_STS_HALT) != 0) {
368a2c2706eSAlan Stern 
369a2c2706eSAlan Stern 				/* retry transaction errors until we
370a2c2706eSAlan Stern 				 * reach the software xacterr limit
371a2c2706eSAlan Stern 				 */
372a2c2706eSAlan Stern 				if ((token & QTD_STS_XACT) &&
373a2c2706eSAlan Stern 						QTD_CERR(token) == 0 &&
374ef4638f9SAlan Stern 						++qh->xacterrs < QH_XACTERR_MAX &&
375a2c2706eSAlan Stern 						!urb->unlinked) {
376a2c2706eSAlan Stern 					ehci_dbg(ehci,
377d0626808SRandy Dunlap 	"detected XactErr len %zu/%zu retry %d\n",
378ef4638f9SAlan Stern 	qtd->length - QTD_LENGTH(token), qtd->length, qh->xacterrs);
379a2c2706eSAlan Stern 
380a2c2706eSAlan Stern 					/* reset the token in the qtd and the
381a2c2706eSAlan Stern 					 * qh overlay (which still contains
382a2c2706eSAlan Stern 					 * the qtd) so that we pick up from
383a2c2706eSAlan Stern 					 * where we left off
384a2c2706eSAlan Stern 					 */
385a2c2706eSAlan Stern 					token &= ~QTD_STS_HALT;
386a2c2706eSAlan Stern 					token |= QTD_STS_ACTIVE |
387a2c2706eSAlan Stern 							(EHCI_TUNE_CERR << 10);
388a2c2706eSAlan Stern 					qtd->hw_token = cpu_to_hc32(ehci,
389a2c2706eSAlan Stern 							token);
390a2c2706eSAlan Stern 					wmb();
3913807e26dSAlek Du 					hw->hw_token = cpu_to_hc32(ehci,
3923807e26dSAlek Du 							token);
393a2c2706eSAlan Stern 					goto retry_xacterr;
394a2c2706eSAlan Stern 				}
3951da177e4SLinus Torvalds 				stopped = 1;
396fcc5184eSAlan Stern 				qh->unlink_reason |= QH_UNLINK_HALTED;
3971da177e4SLinus Torvalds 
3981da177e4SLinus Torvalds 			/* magic dummy for some short reads; qh won't advance.
3991da177e4SLinus Torvalds 			 * that silicon quirk can kick in with this dummy too.
400a082b5c7SDavid Brownell 			 *
401a082b5c7SDavid Brownell 			 * other short reads won't stop the queue, including
402a082b5c7SDavid Brownell 			 * control transfers (status stage handles that) or
403a082b5c7SDavid Brownell 			 * most other single-qtd reads ... the queue stops if
404a082b5c7SDavid Brownell 			 * URB_SHORT_NOT_OK was set so the driver submitting
405a082b5c7SDavid Brownell 			 * the urbs could clean it up.
4061da177e4SLinus Torvalds 			 */
4071da177e4SLinus Torvalds 			} else if (IS_SHORT_READ (token)
4086dbd682bSStefan Roese 					&& !(qtd->hw_alt_next
4096dbd682bSStefan Roese 						& EHCI_LIST_END(ehci))) {
4101da177e4SLinus Torvalds 				stopped = 1;
411fcc5184eSAlan Stern 				qh->unlink_reason |= QH_UNLINK_SHORT_READ;
4121da177e4SLinus Torvalds 			}
4131da177e4SLinus Torvalds 
4141da177e4SLinus Torvalds 		/* stop scanning when we reach qtds the hc is using */
4151da177e4SLinus Torvalds 		} else if (likely (!stopped
416c0c53dbcSAlan Stern 				&& ehci->rh_state >= EHCI_RH_RUNNING)) {
4171da177e4SLinus Torvalds 			break;
4181da177e4SLinus Torvalds 
419a082b5c7SDavid Brownell 		/* scan the whole queue for unlinks whenever it stops */
4201da177e4SLinus Torvalds 		} else {
4211da177e4SLinus Torvalds 			stopped = 1;
4221da177e4SLinus Torvalds 
423a082b5c7SDavid Brownell 			/* cancel everything if we halt, suspend, etc */
424fcc5184eSAlan Stern 			if (ehci->rh_state < EHCI_RH_RUNNING) {
42514c04c0fSAlan Stern 				last_status = -ESHUTDOWN;
426fcc5184eSAlan Stern 				qh->unlink_reason |= QH_UNLINK_SHUTDOWN;
427fcc5184eSAlan Stern 			}
4281da177e4SLinus Torvalds 
429a082b5c7SDavid Brownell 			/* this qtd is active; skip it unless a previous qtd
430a082b5c7SDavid Brownell 			 * for its urb faulted, or its urb was canceled.
4311da177e4SLinus Torvalds 			 */
432a082b5c7SDavid Brownell 			else if (last_status == -EINPROGRESS && !urb->unlinked)
4331da177e4SLinus Torvalds 				continue;
4341da177e4SLinus Torvalds 
435feca7746SAlan Stern 			/*
436feca7746SAlan Stern 			 * If this was the active qtd when the qh was unlinked
437feca7746SAlan Stern 			 * and the overlay's token is active, then the overlay
438feca7746SAlan Stern 			 * hasn't been written back to the qtd yet so use its
439feca7746SAlan Stern 			 * token instead of the qtd's.  After the qtd is
440feca7746SAlan Stern 			 * processed and removed, the overlay won't be valid
441feca7746SAlan Stern 			 * any more.
442feca7746SAlan Stern 			 */
443feca7746SAlan Stern 			if (state == QH_STATE_IDLE &&
444feca7746SAlan Stern 					qh->qtd_list.next == &qtd->qtd_list &&
445feca7746SAlan Stern 					(hw->hw_token & ACTIVE_BIT(ehci))) {
4463807e26dSAlek Du 				token = hc32_to_cpu(ehci, hw->hw_token);
447feca7746SAlan Stern 				hw->hw_token &= ~ACTIVE_BIT(ehci);
448fc0855f2SAlan Stern 				qh->should_be_inactive = 1;
4491da177e4SLinus Torvalds 
450914b7012SAlan Stern 				/* An unlink may leave an incomplete
451914b7012SAlan Stern 				 * async transaction in the TT buffer.
452914b7012SAlan Stern 				 * We have to clear it.
453914b7012SAlan Stern 				 */
454914b7012SAlan Stern 				ehci_clear_tt_buffer(ehci, qh, urb, token);
455914b7012SAlan Stern 			}
4561da177e4SLinus Torvalds 		}
4571da177e4SLinus Torvalds 
4584f667627SDavid Brownell 		/* unless we already know the urb's status, collect qtd status
4594f667627SDavid Brownell 		 * and update count of bytes transferred.  in common short read
4604f667627SDavid Brownell 		 * cases with only one data qtd (including control transfers),
4614f667627SDavid Brownell 		 * queue processing won't halt.  but with two or more qtds (for
4624f667627SDavid Brownell 		 * example, with a 32 KB transfer), when the first qtd gets a
4634f667627SDavid Brownell 		 * short read the second must be removed by hand.
4644f667627SDavid Brownell 		 */
4654f667627SDavid Brownell 		if (last_status == -EINPROGRESS) {
4664f667627SDavid Brownell 			last_status = qtd_copy_status(ehci, urb,
4674f667627SDavid Brownell 					qtd->length, token);
4684f667627SDavid Brownell 			if (last_status == -EREMOTEIO
4694f667627SDavid Brownell 					&& (qtd->hw_alt_next
4704f667627SDavid Brownell 						& EHCI_LIST_END(ehci)))
4714f667627SDavid Brownell 				last_status = -EINPROGRESS;
472914b7012SAlan Stern 
473914b7012SAlan Stern 			/* As part of low/full-speed endpoint-halt processing
474914b7012SAlan Stern 			 * we must clear the TT buffer (11.17.5).
475914b7012SAlan Stern 			 */
476914b7012SAlan Stern 			if (unlikely(last_status != -EINPROGRESS &&
477c2f6595fSAlan Stern 					last_status != -EREMOTEIO)) {
478c2f6595fSAlan Stern 				/* The TT's in some hubs malfunction when they
479c2f6595fSAlan Stern 				 * receive this request following a STALL (they
480c2f6595fSAlan Stern 				 * stop sending isochronous packets).  Since a
481c2f6595fSAlan Stern 				 * STALL can't leave the TT buffer in a busy
482c2f6595fSAlan Stern 				 * state (if you believe Figures 11-48 - 11-51
483c2f6595fSAlan Stern 				 * in the USB 2.0 spec), we won't clear the TT
484c2f6595fSAlan Stern 				 * buffer in this case.  Strictly speaking this
485c2f6595fSAlan Stern 				 * is a violation of the spec.
486c2f6595fSAlan Stern 				 */
487c2f6595fSAlan Stern 				if (last_status != -EPIPE)
488c2f6595fSAlan Stern 					ehci_clear_tt_buffer(ehci, qh, urb,
489c2f6595fSAlan Stern 							token);
490c2f6595fSAlan Stern 			}
491b0d9efbaSAlan Stern 		}
4921da177e4SLinus Torvalds 
493a082b5c7SDavid Brownell 		/* if we're removing something not at the queue head,
494a082b5c7SDavid Brownell 		 * patch the hardware queue pointer.
495a082b5c7SDavid Brownell 		 */
4961da177e4SLinus Torvalds 		if (stopped && qtd->qtd_list.prev != &qh->qtd_list) {
4971da177e4SLinus Torvalds 			last = list_entry (qtd->qtd_list.prev,
4981da177e4SLinus Torvalds 					struct ehci_qtd, qtd_list);
4991da177e4SLinus Torvalds 			last->hw_next = qtd->hw_next;
5001da177e4SLinus Torvalds 		}
501a082b5c7SDavid Brownell 
502a082b5c7SDavid Brownell 		/* remove qtd; it's recycled after possible urb completion */
5031da177e4SLinus Torvalds 		list_del (&qtd->qtd_list);
5041da177e4SLinus Torvalds 		last = qtd;
505a2c2706eSAlan Stern 
506a2c2706eSAlan Stern 		/* reinit the xacterr counter for the next qtd */
507ef4638f9SAlan Stern 		qh->xacterrs = 0;
5081da177e4SLinus Torvalds 	}
5091da177e4SLinus Torvalds 
5101da177e4SLinus Torvalds 	/* last urb's completion might still need calling */
5111da177e4SLinus Torvalds 	if (likely (last != NULL)) {
51214c04c0fSAlan Stern 		ehci_urb_done(ehci, last->urb, last_status);
5131da177e4SLinus Torvalds 		ehci_qtd_free (ehci, last);
5141da177e4SLinus Torvalds 	}
5151da177e4SLinus Torvalds 
5163a44494eSAlan Stern 	/* Do we need to rescan for URBs dequeued during a giveback? */
5177bc782d7SAlan Stern 	if (unlikely(qh->dequeue_during_giveback)) {
5183a44494eSAlan Stern 		/* If the QH is already unlinked, do the rescan now. */
5193a44494eSAlan Stern 		if (state == QH_STATE_IDLE)
5203a44494eSAlan Stern 			goto rescan;
5213a44494eSAlan Stern 
5227bc782d7SAlan Stern 		/* Otherwise the caller must unlink the QH. */
5233a44494eSAlan Stern 	}
5243a44494eSAlan Stern 
5251da177e4SLinus Torvalds 	/* restore original state; caller must unlink or relink */
5261da177e4SLinus Torvalds 	qh->qh_state = state;
5271da177e4SLinus Torvalds 
5281da177e4SLinus Torvalds 	/* be sure the hardware's done with the qh before refreshing
5291da177e4SLinus Torvalds 	 * it after fault cleanup, or recovering from silicon wrongly
5301da177e4SLinus Torvalds 	 * overlaying the dummy qtd (which reduces DMA chatter).
5317bc782d7SAlan Stern 	 *
532c1fdb68eSAlan Stern 	 * We won't refresh a QH that's linked (after the HC
533a082b5c7SDavid Brownell 	 * stopped the queue).  That avoids a race:
534a082b5c7SDavid Brownell 	 *  - HC reads first part of QH;
535a082b5c7SDavid Brownell 	 *  - CPU updates that first part and the token;
536a082b5c7SDavid Brownell 	 *  - HC reads rest of that QH, including token
537a082b5c7SDavid Brownell 	 * Result:  HC gets an inconsistent image, and then
538a082b5c7SDavid Brownell 	 * DMAs to/from the wrong memory (corrupting it).
539a082b5c7SDavid Brownell 	 *
540a082b5c7SDavid Brownell 	 * That should be rare for interrupt transfers,
5411da177e4SLinus Torvalds 	 * except maybe high bandwidth ...
5421da177e4SLinus Torvalds 	 */
5437bc782d7SAlan Stern 	if (stopped != 0 || hw->hw_qtd_next == EHCI_LIST_END(ehci))
544fcc5184eSAlan Stern 		qh->unlink_reason |= QH_UNLINK_DUMMY_OVERLAY;
5451da177e4SLinus Torvalds 
5467bc782d7SAlan Stern 	/* Let the caller know if the QH needs to be unlinked. */
547fcc5184eSAlan Stern 	return qh->unlink_reason;
5481da177e4SLinus Torvalds }
5491da177e4SLinus Torvalds 
5501da177e4SLinus Torvalds /*-------------------------------------------------------------------------*/
5511da177e4SLinus Torvalds 
5521da177e4SLinus Torvalds /*
5531da177e4SLinus Torvalds  * reverse of qh_urb_transaction:  free a list of TDs.
5541da177e4SLinus Torvalds  * used for cleanup after errors, before HC sees an URB's TDs.
5551da177e4SLinus Torvalds  */
qtd_list_free(struct ehci_hcd * ehci,struct urb * urb,struct list_head * qtd_list)5561da177e4SLinus Torvalds static void qtd_list_free (
5571da177e4SLinus Torvalds 	struct ehci_hcd		*ehci,
5581da177e4SLinus Torvalds 	struct urb		*urb,
5591da177e4SLinus Torvalds 	struct list_head	*qtd_list
5601da177e4SLinus Torvalds ) {
5611da177e4SLinus Torvalds 	struct list_head	*entry, *temp;
5621da177e4SLinus Torvalds 
5631da177e4SLinus Torvalds 	list_for_each_safe (entry, temp, qtd_list) {
5641da177e4SLinus Torvalds 		struct ehci_qtd	*qtd;
5651da177e4SLinus Torvalds 
5661da177e4SLinus Torvalds 		qtd = list_entry (entry, struct ehci_qtd, qtd_list);
5671da177e4SLinus Torvalds 		list_del (&qtd->qtd_list);
5681da177e4SLinus Torvalds 		ehci_qtd_free (ehci, qtd);
5691da177e4SLinus Torvalds 	}
5701da177e4SLinus Torvalds }
5711da177e4SLinus Torvalds 
5721da177e4SLinus Torvalds /*
5731da177e4SLinus Torvalds  * create a list of filled qtds for this URB; won't link into qh.
5741da177e4SLinus Torvalds  */
5751da177e4SLinus Torvalds static struct list_head *
qh_urb_transaction(struct ehci_hcd * ehci,struct urb * urb,struct list_head * head,gfp_t flags)5761da177e4SLinus Torvalds qh_urb_transaction (
5771da177e4SLinus Torvalds 	struct ehci_hcd		*ehci,
5781da177e4SLinus Torvalds 	struct urb		*urb,
5791da177e4SLinus Torvalds 	struct list_head	*head,
58055016f10SAl Viro 	gfp_t			flags
5811da177e4SLinus Torvalds ) {
5821da177e4SLinus Torvalds 	struct ehci_qtd		*qtd, *qtd_prev;
5831da177e4SLinus Torvalds 	dma_addr_t		buf;
58440f8db8fSAlan Stern 	int			len, this_sg_len, maxpacket;
5851da177e4SLinus Torvalds 	int			is_input;
5861da177e4SLinus Torvalds 	u32			token;
58740f8db8fSAlan Stern 	int			i;
58840f8db8fSAlan Stern 	struct scatterlist	*sg;
5891da177e4SLinus Torvalds 
5901da177e4SLinus Torvalds 	/*
5911da177e4SLinus Torvalds 	 * URBs map to sequences of QTDs:  one logical transaction
5921da177e4SLinus Torvalds 	 */
5931da177e4SLinus Torvalds 	qtd = ehci_qtd_alloc (ehci, flags);
5941da177e4SLinus Torvalds 	if (unlikely (!qtd))
5951da177e4SLinus Torvalds 		return NULL;
5961da177e4SLinus Torvalds 	list_add_tail (&qtd->qtd_list, head);
5971da177e4SLinus Torvalds 	qtd->urb = urb;
5981da177e4SLinus Torvalds 
5991da177e4SLinus Torvalds 	token = QTD_STS_ACTIVE;
6001da177e4SLinus Torvalds 	token |= (EHCI_TUNE_CERR << 10);
6011da177e4SLinus Torvalds 	/* for split transactions, SplitXState initialized to zero */
6021da177e4SLinus Torvalds 
6031da177e4SLinus Torvalds 	len = urb->transfer_buffer_length;
6041da177e4SLinus Torvalds 	is_input = usb_pipein (urb->pipe);
6051da177e4SLinus Torvalds 	if (usb_pipecontrol (urb->pipe)) {
6061da177e4SLinus Torvalds 		/* SETUP pid */
6076dbd682bSStefan Roese 		qtd_fill(ehci, qtd, urb->setup_dma,
6086dbd682bSStefan Roese 				sizeof (struct usb_ctrlrequest),
6091da177e4SLinus Torvalds 				token | (2 /* "setup" */ << 8), 8);
6101da177e4SLinus Torvalds 
6111da177e4SLinus Torvalds 		/* ... and always at least one more pid */
6121da177e4SLinus Torvalds 		token ^= QTD_TOGGLE;
6131da177e4SLinus Torvalds 		qtd_prev = qtd;
6141da177e4SLinus Torvalds 		qtd = ehci_qtd_alloc (ehci, flags);
6151da177e4SLinus Torvalds 		if (unlikely (!qtd))
6161da177e4SLinus Torvalds 			goto cleanup;
6171da177e4SLinus Torvalds 		qtd->urb = urb;
6186dbd682bSStefan Roese 		qtd_prev->hw_next = QTD_NEXT(ehci, qtd->qtd_dma);
6191da177e4SLinus Torvalds 		list_add_tail (&qtd->qtd_list, head);
6206912354aSAlan Stern 
6216912354aSAlan Stern 		/* for zero length DATA stages, STATUS is always IN */
6226912354aSAlan Stern 		if (len == 0)
6236912354aSAlan Stern 			token |= (1 /* "in" */ << 8);
6241da177e4SLinus Torvalds 	}
6251da177e4SLinus Torvalds 
6261da177e4SLinus Torvalds 	/*
6271da177e4SLinus Torvalds 	 * data transfer stage:  buffer setup
6281da177e4SLinus Torvalds 	 */
629bc677d5bSClemens Ladisch 	i = urb->num_mapped_sgs;
63040f8db8fSAlan Stern 	if (len > 0 && i > 0) {
631910f8d0cSMatthew Wilcox 		sg = urb->sg;
63240f8db8fSAlan Stern 		buf = sg_dma_address(sg);
63340f8db8fSAlan Stern 
63440f8db8fSAlan Stern 		/* urb->transfer_buffer_length may be smaller than the
63540f8db8fSAlan Stern 		 * size of the scatterlist (or vice versa)
63640f8db8fSAlan Stern 		 */
63740f8db8fSAlan Stern 		this_sg_len = min_t(int, sg_dma_len(sg), len);
63840f8db8fSAlan Stern 	} else {
63940f8db8fSAlan Stern 		sg = NULL;
6401da177e4SLinus Torvalds 		buf = urb->transfer_dma;
64140f8db8fSAlan Stern 		this_sg_len = len;
64240f8db8fSAlan Stern 	}
6431da177e4SLinus Torvalds 
6446912354aSAlan Stern 	if (is_input)
6451da177e4SLinus Torvalds 		token |= (1 /* "in" */ << 8);
6461da177e4SLinus Torvalds 	/* else it's already initted to "out" pid (0 << 8) */
6471da177e4SLinus Torvalds 
648*d27c66adSKhalid Masum 	maxpacket = usb_endpoint_maxp(&urb->ep->desc);
6491da177e4SLinus Torvalds 
6501da177e4SLinus Torvalds 	/*
6511da177e4SLinus Torvalds 	 * buffer gets wrapped in one or more qtds;
6521da177e4SLinus Torvalds 	 * last one may be "short" (including zero len)
6531da177e4SLinus Torvalds 	 * and may serve as a control status ack
6541da177e4SLinus Torvalds 	 */
6551da177e4SLinus Torvalds 	for (;;) {
656c6c986b6SSergey Shtylyov 		unsigned int this_qtd_len;
6571da177e4SLinus Torvalds 
65840f8db8fSAlan Stern 		this_qtd_len = qtd_fill(ehci, qtd, buf, this_sg_len, token,
65940f8db8fSAlan Stern 				maxpacket);
66040f8db8fSAlan Stern 		this_sg_len -= this_qtd_len;
6611da177e4SLinus Torvalds 		len -= this_qtd_len;
6621da177e4SLinus Torvalds 		buf += this_qtd_len;
663a082b5c7SDavid Brownell 
664a082b5c7SDavid Brownell 		/*
665a082b5c7SDavid Brownell 		 * short reads advance to a "magic" dummy instead of the next
666a082b5c7SDavid Brownell 		 * qtd ... that forces the queue to stop, for manual cleanup.
667a082b5c7SDavid Brownell 		 * (this will usually be overridden later.)
668a082b5c7SDavid Brownell 		 */
6691da177e4SLinus Torvalds 		if (is_input)
6703807e26dSAlek Du 			qtd->hw_alt_next = ehci->async->hw->hw_alt_next;
6711da177e4SLinus Torvalds 
6721da177e4SLinus Torvalds 		/* qh makes control packets use qtd toggle; maybe switch it */
6731da177e4SLinus Torvalds 		if ((maxpacket & (this_qtd_len + (maxpacket - 1))) == 0)
6741da177e4SLinus Torvalds 			token ^= QTD_TOGGLE;
6751da177e4SLinus Torvalds 
67640f8db8fSAlan Stern 		if (likely(this_sg_len <= 0)) {
67740f8db8fSAlan Stern 			if (--i <= 0 || len <= 0)
6781da177e4SLinus Torvalds 				break;
67940f8db8fSAlan Stern 			sg = sg_next(sg);
68040f8db8fSAlan Stern 			buf = sg_dma_address(sg);
68140f8db8fSAlan Stern 			this_sg_len = min_t(int, sg_dma_len(sg), len);
68240f8db8fSAlan Stern 		}
6831da177e4SLinus Torvalds 
6841da177e4SLinus Torvalds 		qtd_prev = qtd;
6851da177e4SLinus Torvalds 		qtd = ehci_qtd_alloc (ehci, flags);
6861da177e4SLinus Torvalds 		if (unlikely (!qtd))
6871da177e4SLinus Torvalds 			goto cleanup;
6881da177e4SLinus Torvalds 		qtd->urb = urb;
6896dbd682bSStefan Roese 		qtd_prev->hw_next = QTD_NEXT(ehci, qtd->qtd_dma);
6901da177e4SLinus Torvalds 		list_add_tail (&qtd->qtd_list, head);
6911da177e4SLinus Torvalds 	}
6921da177e4SLinus Torvalds 
693a082b5c7SDavid Brownell 	/*
694a082b5c7SDavid Brownell 	 * unless the caller requires manual cleanup after short reads,
695a082b5c7SDavid Brownell 	 * have the alt_next mechanism keep the queue running after the
696a082b5c7SDavid Brownell 	 * last data qtd (the only one, for control and most other cases).
6971da177e4SLinus Torvalds 	 */
6981da177e4SLinus Torvalds 	if (likely ((urb->transfer_flags & URB_SHORT_NOT_OK) == 0
6991da177e4SLinus Torvalds 				|| usb_pipecontrol (urb->pipe)))
7006dbd682bSStefan Roese 		qtd->hw_alt_next = EHCI_LIST_END(ehci);
7011da177e4SLinus Torvalds 
7021da177e4SLinus Torvalds 	/*
7031da177e4SLinus Torvalds 	 * control requests may need a terminating data "status" ack;
7049a971ddaSMing Lei 	 * other OUT ones may need a terminating short packet
7059a971ddaSMing Lei 	 * (zero length).
7061da177e4SLinus Torvalds 	 */
7076912354aSAlan Stern 	if (likely (urb->transfer_buffer_length != 0)) {
7081da177e4SLinus Torvalds 		int	one_more = 0;
7091da177e4SLinus Torvalds 
7101da177e4SLinus Torvalds 		if (usb_pipecontrol (urb->pipe)) {
7111da177e4SLinus Torvalds 			one_more = 1;
7121da177e4SLinus Torvalds 			token ^= 0x0100;	/* "in" <--> "out"  */
7131da177e4SLinus Torvalds 			token |= QTD_TOGGLE;	/* force DATA1 */
7149a971ddaSMing Lei 		} else if (usb_pipeout(urb->pipe)
7151da177e4SLinus Torvalds 				&& (urb->transfer_flags & URB_ZERO_PACKET)
7161da177e4SLinus Torvalds 				&& !(urb->transfer_buffer_length % maxpacket)) {
7171da177e4SLinus Torvalds 			one_more = 1;
7181da177e4SLinus Torvalds 		}
7191da177e4SLinus Torvalds 		if (one_more) {
7201da177e4SLinus Torvalds 			qtd_prev = qtd;
7211da177e4SLinus Torvalds 			qtd = ehci_qtd_alloc (ehci, flags);
7221da177e4SLinus Torvalds 			if (unlikely (!qtd))
7231da177e4SLinus Torvalds 				goto cleanup;
7241da177e4SLinus Torvalds 			qtd->urb = urb;
7256dbd682bSStefan Roese 			qtd_prev->hw_next = QTD_NEXT(ehci, qtd->qtd_dma);
7261da177e4SLinus Torvalds 			list_add_tail (&qtd->qtd_list, head);
7271da177e4SLinus Torvalds 
7281da177e4SLinus Torvalds 			/* never any data in such packets */
7296dbd682bSStefan Roese 			qtd_fill(ehci, qtd, 0, 0, token, 0);
7301da177e4SLinus Torvalds 		}
7311da177e4SLinus Torvalds 	}
7321da177e4SLinus Torvalds 
7331da177e4SLinus Torvalds 	/* by default, enable interrupt on urb completion */
7341da177e4SLinus Torvalds 	if (likely (!(urb->transfer_flags & URB_NO_INTERRUPT)))
7356dbd682bSStefan Roese 		qtd->hw_token |= cpu_to_hc32(ehci, QTD_IOC);
7361da177e4SLinus Torvalds 	return head;
7371da177e4SLinus Torvalds 
7381da177e4SLinus Torvalds cleanup:
7391da177e4SLinus Torvalds 	qtd_list_free (ehci, urb, head);
7401da177e4SLinus Torvalds 	return NULL;
7411da177e4SLinus Torvalds }
7421da177e4SLinus Torvalds 
7431da177e4SLinus Torvalds /*-------------------------------------------------------------------------*/
7441da177e4SLinus Torvalds 
7451da177e4SLinus Torvalds // Would be best to create all qh's from config descriptors,
7461da177e4SLinus Torvalds // when each interface/altsetting is established.  Unlink
7471da177e4SLinus Torvalds // any previous qh and cancel its urbs first; endpoints are
7481da177e4SLinus Torvalds // implicitly reset then (data toggle too).
7491da177e4SLinus Torvalds // That'd mean updating how usbcore talks to HCDs. (2.7?)
7501da177e4SLinus Torvalds 
7511da177e4SLinus Torvalds 
7521da177e4SLinus Torvalds /*
7531da177e4SLinus Torvalds  * Each QH holds a qtd list; a QH is used for everything except iso.
7541da177e4SLinus Torvalds  *
7551da177e4SLinus Torvalds  * For interrupt urbs, the scheduler must set the microframe scheduling
7561da177e4SLinus Torvalds  * mask(s) each time the QH gets scheduled.  For highspeed, that's
7571da177e4SLinus Torvalds  * just one microframe in the s-mask.  For split interrupt transactions
7581da177e4SLinus Torvalds  * there are additional complications: c-mask, maybe FSTNs.
7591da177e4SLinus Torvalds  */
7601da177e4SLinus Torvalds static struct ehci_qh *
qh_make(struct ehci_hcd * ehci,struct urb * urb,gfp_t flags)7611da177e4SLinus Torvalds qh_make (
7621da177e4SLinus Torvalds 	struct ehci_hcd		*ehci,
7631da177e4SLinus Torvalds 	struct urb		*urb,
76455016f10SAl Viro 	gfp_t			flags
7651da177e4SLinus Torvalds ) {
7661da177e4SLinus Torvalds 	struct ehci_qh		*qh = ehci_qh_alloc (ehci, flags);
767e3b89080SFelipe Balbi 	struct usb_host_endpoint *ep;
7681da177e4SLinus Torvalds 	u32			info1 = 0, info2 = 0;
7691da177e4SLinus Torvalds 	int			is_input, type;
7701da177e4SLinus Torvalds 	int			maxp = 0;
771e3b89080SFelipe Balbi 	int			mult;
772340ba5f9SDavid Brownell 	struct usb_tt		*tt = urb->dev->tt;
7733807e26dSAlek Du 	struct ehci_qh_hw	*hw;
7741da177e4SLinus Torvalds 
7751da177e4SLinus Torvalds 	if (!qh)
7761da177e4SLinus Torvalds 		return qh;
7771da177e4SLinus Torvalds 
7781da177e4SLinus Torvalds 	/*
7791da177e4SLinus Torvalds 	 * init endpoint/device data for this QH
7801da177e4SLinus Torvalds 	 */
7811da177e4SLinus Torvalds 	info1 |= usb_pipeendpoint (urb->pipe) << 8;
7821da177e4SLinus Torvalds 	info1 |= usb_pipedevice (urb->pipe) << 0;
7831da177e4SLinus Torvalds 
7841da177e4SLinus Torvalds 	is_input = usb_pipein (urb->pipe);
7851da177e4SLinus Torvalds 	type = usb_pipetype (urb->pipe);
786e3b89080SFelipe Balbi 	ep = usb_pipe_endpoint (urb->dev, urb->pipe);
7878437ab99SFelipe Balbi 	maxp = usb_endpoint_maxp (&ep->desc);
788e3b89080SFelipe Balbi 	mult = usb_endpoint_maxp_mult (&ep->desc);
7891da177e4SLinus Torvalds 
790caa9ef67SDavid Brownell 	/* 1024 byte maxpacket is a hardware ceiling.  High bandwidth
791caa9ef67SDavid Brownell 	 * acts like up to 3KB, but is built from smaller packets.
792caa9ef67SDavid Brownell 	 */
7938437ab99SFelipe Balbi 	if (maxp > 1024) {
7948437ab99SFelipe Balbi 		ehci_dbg(ehci, "bogus qh maxpacket %d\n", maxp);
795caa9ef67SDavid Brownell 		goto done;
796caa9ef67SDavid Brownell 	}
797caa9ef67SDavid Brownell 
7981da177e4SLinus Torvalds 	/* Compute interrupt scheduling parameters just once, and save.
7991da177e4SLinus Torvalds 	 * - allowing for high bandwidth, how many nsec/uframe are used?
8001da177e4SLinus Torvalds 	 * - split transactions need a second CSPLIT uframe; same question
8011da177e4SLinus Torvalds 	 * - splits also need a schedule gap (for full/low speed I/O)
8021da177e4SLinus Torvalds 	 * - qh has a polling interval
8031da177e4SLinus Torvalds 	 *
8041da177e4SLinus Torvalds 	 * For control/bulk requests, the HC or TT handles these.
8051da177e4SLinus Torvalds 	 */
8061da177e4SLinus Torvalds 	if (type == PIPE_INTERRUPT) {
807d0ce5c6bSAlan Stern 		unsigned	tmp;
808d0ce5c6bSAlan Stern 
809ffa0248eSAlan Stern 		qh->ps.usecs = NS_TO_US(usb_calc_bus_time(USB_SPEED_HIGH,
8108437ab99SFelipe Balbi 				is_input, 0, mult * maxp));
811ffa0248eSAlan Stern 		qh->ps.phase = NO_FRAME;
8121da177e4SLinus Torvalds 
8131da177e4SLinus Torvalds 		if (urb->dev->speed == USB_SPEED_HIGH) {
814ffa0248eSAlan Stern 			qh->ps.c_usecs = 0;
8151da177e4SLinus Torvalds 			qh->gap_uf = 0;
8161da177e4SLinus Torvalds 
817ffa0248eSAlan Stern 			if (urb->interval > 1 && urb->interval < 8) {
8181da177e4SLinus Torvalds 				/* NOTE interval 2 or 4 uframes could work.
8191da177e4SLinus Torvalds 				 * But interval 1 scheduling is simpler, and
8201da177e4SLinus Torvalds 				 * includes high bandwidth.
8211da177e4SLinus Torvalds 				 */
8221b9a38bfSAlan Stern 				urb->interval = 1;
823ffa0248eSAlan Stern 			} else if (urb->interval > ehci->periodic_size << 3) {
824ffa0248eSAlan Stern 				urb->interval = ehci->periodic_size << 3;
8251da177e4SLinus Torvalds 			}
826ffa0248eSAlan Stern 			qh->ps.period = urb->interval >> 3;
827d0ce5c6bSAlan Stern 
828d0ce5c6bSAlan Stern 			/* period for bandwidth allocation */
829d0ce5c6bSAlan Stern 			tmp = min_t(unsigned, EHCI_BANDWIDTH_SIZE,
830d0ce5c6bSAlan Stern 					1 << (urb->ep->desc.bInterval - 1));
831d0ce5c6bSAlan Stern 
832d0ce5c6bSAlan Stern 			/* Allow urb->interval to override */
833d0ce5c6bSAlan Stern 			qh->ps.bw_uperiod = min_t(unsigned, tmp, urb->interval);
834d0ce5c6bSAlan Stern 			qh->ps.bw_period = qh->ps.bw_uperiod >> 3;
8351da177e4SLinus Torvalds 		} else {
836d0384200Sdavid-b@pacbell.net 			int		think_time;
837d0384200Sdavid-b@pacbell.net 
8381da177e4SLinus Torvalds 			/* gap is f(FS/LS transfer times) */
8391da177e4SLinus Torvalds 			qh->gap_uf = 1 + usb_calc_bus_time (urb->dev->speed,
8401da177e4SLinus Torvalds 					is_input, 0, maxp) / (125 * 1000);
8411da177e4SLinus Torvalds 
8421da177e4SLinus Torvalds 			/* FIXME this just approximates SPLIT/CSPLIT times */
8431da177e4SLinus Torvalds 			if (is_input) {		// SPLIT, gap, CSPLIT+DATA
844ffa0248eSAlan Stern 				qh->ps.c_usecs = qh->ps.usecs + HS_USECS(0);
845ffa0248eSAlan Stern 				qh->ps.usecs = HS_USECS(1);
8461da177e4SLinus Torvalds 			} else {		// SPLIT+DATA, gap, CSPLIT
847ffa0248eSAlan Stern 				qh->ps.usecs += HS_USECS(1);
848ffa0248eSAlan Stern 				qh->ps.c_usecs = HS_USECS(0);
8491da177e4SLinus Torvalds 			}
8501da177e4SLinus Torvalds 
851d0384200Sdavid-b@pacbell.net 			think_time = tt ? tt->think_time : 0;
852ffa0248eSAlan Stern 			qh->ps.tt_usecs = NS_TO_US(think_time +
853d0384200Sdavid-b@pacbell.net 					usb_calc_bus_time (urb->dev->speed,
8548437ab99SFelipe Balbi 					is_input, 0, maxp));
855ffa0248eSAlan Stern 			if (urb->interval > ehci->periodic_size)
856ffa0248eSAlan Stern 				urb->interval = ehci->periodic_size;
857ffa0248eSAlan Stern 			qh->ps.period = urb->interval;
858d0ce5c6bSAlan Stern 
859d0ce5c6bSAlan Stern 			/* period for bandwidth allocation */
860d0ce5c6bSAlan Stern 			tmp = min_t(unsigned, EHCI_BANDWIDTH_FRAMES,
861d0ce5c6bSAlan Stern 					urb->ep->desc.bInterval);
862d0ce5c6bSAlan Stern 			tmp = rounddown_pow_of_two(tmp);
863d0ce5c6bSAlan Stern 
864d0ce5c6bSAlan Stern 			/* Allow urb->interval to override */
865d0ce5c6bSAlan Stern 			qh->ps.bw_period = min_t(unsigned, tmp, urb->interval);
866d0ce5c6bSAlan Stern 			qh->ps.bw_uperiod = qh->ps.bw_period << 3;
8671da177e4SLinus Torvalds 		}
8681da177e4SLinus Torvalds 	}
8691da177e4SLinus Torvalds 
8701da177e4SLinus Torvalds 	/* support for tt scheduling, and access to toggles */
871ffa0248eSAlan Stern 	qh->ps.udev = urb->dev;
872ffa0248eSAlan Stern 	qh->ps.ep = urb->ep;
8731da177e4SLinus Torvalds 
8741da177e4SLinus Torvalds 	/* using TT? */
8751da177e4SLinus Torvalds 	switch (urb->dev->speed) {
8761da177e4SLinus Torvalds 	case USB_SPEED_LOW:
8774c53de72SAlan Stern 		info1 |= QH_LOW_SPEED;
8788b84724eSGustavo A. R. Silva 		fallthrough;
8791da177e4SLinus Torvalds 
8801da177e4SLinus Torvalds 	case USB_SPEED_FULL:
8811da177e4SLinus Torvalds 		/* EPS 0 means "full" */
8821da177e4SLinus Torvalds 		if (type != PIPE_INTERRUPT)
8831da177e4SLinus Torvalds 			info1 |= (EHCI_TUNE_RL_TT << 28);
8841da177e4SLinus Torvalds 		if (type == PIPE_CONTROL) {
8854c53de72SAlan Stern 			info1 |= QH_CONTROL_EP;		/* for TT */
8864c53de72SAlan Stern 			info1 |= QH_TOGGLE_CTL;		/* toggle from qtd */
8871da177e4SLinus Torvalds 		}
8881da177e4SLinus Torvalds 		info1 |= maxp << 16;
8891da177e4SLinus Torvalds 
8901da177e4SLinus Torvalds 		info2 |= (EHCI_TUNE_MULT_TT << 30);
8918cd42e97SKumar Gala 
8928cd42e97SKumar Gala 		/* Some Freescale processors have an erratum in which the
8938cd42e97SKumar Gala 		 * port number in the queue head was 0..N-1 instead of 1..N.
8948cd42e97SKumar Gala 		 */
8958cd42e97SKumar Gala 		if (ehci_has_fsl_portno_bug(ehci))
8968cd42e97SKumar Gala 			info2 |= (urb->dev->ttport-1) << 23;
8978cd42e97SKumar Gala 		else
8981da177e4SLinus Torvalds 			info2 |= urb->dev->ttport << 23;
8991da177e4SLinus Torvalds 
9001da177e4SLinus Torvalds 		/* set the address of the TT; for TDI's integrated
9011da177e4SLinus Torvalds 		 * root hub tt, leave it zeroed.
9021da177e4SLinus Torvalds 		 */
903340ba5f9SDavid Brownell 		if (tt && tt->hub != ehci_to_hcd(ehci)->self.root_hub)
904340ba5f9SDavid Brownell 			info2 |= tt->hub->devnum << 16;
9051da177e4SLinus Torvalds 
9061da177e4SLinus Torvalds 		/* NOTE:  if (PIPE_INTERRUPT) { scheduler sets c-mask } */
9071da177e4SLinus Torvalds 
9081da177e4SLinus Torvalds 		break;
9091da177e4SLinus Torvalds 
9101da177e4SLinus Torvalds 	case USB_SPEED_HIGH:		/* no TT involved */
9114c53de72SAlan Stern 		info1 |= QH_HIGH_SPEED;
9121da177e4SLinus Torvalds 		if (type == PIPE_CONTROL) {
9131da177e4SLinus Torvalds 			info1 |= (EHCI_TUNE_RL_HS << 28);
9141da177e4SLinus Torvalds 			info1 |= 64 << 16;	/* usb2 fixed maxpacket */
9154c53de72SAlan Stern 			info1 |= QH_TOGGLE_CTL;	/* toggle from qtd */
9161da177e4SLinus Torvalds 			info2 |= (EHCI_TUNE_MULT_HS << 30);
9171da177e4SLinus Torvalds 		} else if (type == PIPE_BULK) {
9181da177e4SLinus Torvalds 			info1 |= (EHCI_TUNE_RL_HS << 28);
919caa9ef67SDavid Brownell 			/* The USB spec says that high speed bulk endpoints
920caa9ef67SDavid Brownell 			 * always use 512 byte maxpacket.  But some device
921caa9ef67SDavid Brownell 			 * vendors decided to ignore that, and MSFT is happy
922caa9ef67SDavid Brownell 			 * to help them do so.  So now people expect to use
923caa9ef67SDavid Brownell 			 * such nonconformant devices with Linux too; sigh.
924caa9ef67SDavid Brownell 			 */
9258437ab99SFelipe Balbi 			info1 |= maxp << 16;
9261da177e4SLinus Torvalds 			info2 |= (EHCI_TUNE_MULT_HS << 30);
9271da177e4SLinus Torvalds 		} else {		/* PIPE_INTERRUPT */
9288437ab99SFelipe Balbi 			info1 |= maxp << 16;
929e3b89080SFelipe Balbi 			info2 |= mult << 30;
9301da177e4SLinus Torvalds 		}
9311da177e4SLinus Torvalds 		break;
9321da177e4SLinus Torvalds 	default:
93382491c2aSGreg Kroah-Hartman 		ehci_dbg(ehci, "bogus dev %p speed %d\n", urb->dev,
93482491c2aSGreg Kroah-Hartman 			urb->dev->speed);
9351da177e4SLinus Torvalds done:
936c83e1a9fSAlan Stern 		qh_destroy(ehci, qh);
9371da177e4SLinus Torvalds 		return NULL;
9381da177e4SLinus Torvalds 	}
9391da177e4SLinus Torvalds 
9401da177e4SLinus Torvalds 	/* NOTE:  if (PIPE_INTERRUPT) { scheduler sets s-mask } */
9411da177e4SLinus Torvalds 
942c1fdb68eSAlan Stern 	/* init as live, toggle clear */
9431da177e4SLinus Torvalds 	qh->qh_state = QH_STATE_IDLE;
9443807e26dSAlek Du 	hw = qh->hw;
9453807e26dSAlek Du 	hw->hw_info1 = cpu_to_hc32(ehci, info1);
9463807e26dSAlek Du 	hw->hw_info2 = cpu_to_hc32(ehci, info2);
947e04f5f7eSAlan Stern 	qh->is_out = !is_input;
948a455212dSAlan Stern 	usb_settoggle (urb->dev, usb_pipeendpoint (urb->pipe), !is_input, 1);
9491da177e4SLinus Torvalds 	return qh;
9501da177e4SLinus Torvalds }
9511da177e4SLinus Torvalds 
9521da177e4SLinus Torvalds /*-------------------------------------------------------------------------*/
9531da177e4SLinus Torvalds 
enable_async(struct ehci_hcd * ehci)95431446610SAlan Stern static void enable_async(struct ehci_hcd *ehci)
95531446610SAlan Stern {
95631446610SAlan Stern 	if (ehci->async_count++)
95731446610SAlan Stern 		return;
95831446610SAlan Stern 
95931446610SAlan Stern 	/* Stop waiting to turn off the async schedule */
96031446610SAlan Stern 	ehci->enabled_hrtimer_events &= ~BIT(EHCI_HRTIMER_DISABLE_ASYNC);
96131446610SAlan Stern 
96231446610SAlan Stern 	/* Don't start the schedule until ASS is 0 */
96331446610SAlan Stern 	ehci_poll_ASS(ehci);
96418aafe64SAlan Stern 	turn_on_io_watchdog(ehci);
96531446610SAlan Stern }
96631446610SAlan Stern 
disable_async(struct ehci_hcd * ehci)96731446610SAlan Stern static void disable_async(struct ehci_hcd *ehci)
96831446610SAlan Stern {
96931446610SAlan Stern 	if (--ehci->async_count)
97031446610SAlan Stern 		return;
97131446610SAlan Stern 
9726e018751SAlan Stern 	/* The async schedule and unlink lists are supposed to be empty */
9736e018751SAlan Stern 	WARN_ON(ehci->async->qh_next.qh || !list_empty(&ehci->async_unlink) ||
974214ac7a0SAlan Stern 			!list_empty(&ehci->async_idle));
97531446610SAlan Stern 
97631446610SAlan Stern 	/* Don't turn off the schedule until ASS is 1 */
97731446610SAlan Stern 	ehci_poll_ASS(ehci);
97831446610SAlan Stern }
97931446610SAlan Stern 
9801da177e4SLinus Torvalds /* move qh (and its qtds) onto async queue; maybe enable queue.  */
9811da177e4SLinus Torvalds 
qh_link_async(struct ehci_hcd * ehci,struct ehci_qh * qh)9821da177e4SLinus Torvalds static void qh_link_async (struct ehci_hcd *ehci, struct ehci_qh *qh)
9831da177e4SLinus Torvalds {
9846dbd682bSStefan Roese 	__hc32		dma = QH_NEXT(ehci, qh->qh_dma);
9851da177e4SLinus Torvalds 	struct ehci_qh	*head;
9861da177e4SLinus Torvalds 
987914b7012SAlan Stern 	/* Don't link a QH if there's a Clear-TT-Buffer pending */
988914b7012SAlan Stern 	if (unlikely(qh->clearing_tt))
989914b7012SAlan Stern 		return;
990914b7012SAlan Stern 
9913a44494eSAlan Stern 	WARN_ON(qh->qh_state != QH_STATE_IDLE);
9923a44494eSAlan Stern 
993a455212dSAlan Stern 	/* clear halt and/or toggle; and maybe recover from silicon quirk */
9941da177e4SLinus Torvalds 	qh_refresh(ehci, qh);
9951da177e4SLinus Torvalds 
9961da177e4SLinus Torvalds 	/* splice right after start */
99731446610SAlan Stern 	head = ehci->async;
9981da177e4SLinus Torvalds 	qh->qh_next = head->qh_next;
9993807e26dSAlek Du 	qh->hw->hw_next = head->hw->hw_next;
10001da177e4SLinus Torvalds 	wmb ();
10011da177e4SLinus Torvalds 
10021da177e4SLinus Torvalds 	head->qh_next.qh = qh;
10033807e26dSAlek Du 	head->hw->hw_next = dma;
10041da177e4SLinus Torvalds 
10051da177e4SLinus Torvalds 	qh->qh_state = QH_STATE_LINKED;
10067bc782d7SAlan Stern 	qh->xacterrs = 0;
1007fcc5184eSAlan Stern 	qh->unlink_reason = 0;
10081da177e4SLinus Torvalds 	/* qtd completions reported later by interrupt */
100931446610SAlan Stern 
101031446610SAlan Stern 	enable_async(ehci);
10111da177e4SLinus Torvalds }
10121da177e4SLinus Torvalds 
10131da177e4SLinus Torvalds /*-------------------------------------------------------------------------*/
10141da177e4SLinus Torvalds 
10151da177e4SLinus Torvalds /*
10161da177e4SLinus Torvalds  * For control/bulk/interrupt, return QH with these TDs appended.
10171da177e4SLinus Torvalds  * Allocates and initializes the QH if necessary.
10181da177e4SLinus Torvalds  * Returns null if it can't allocate a QH it needs to.
10191da177e4SLinus Torvalds  * If the QH has TDs (urbs) already, that's great.
10201da177e4SLinus Torvalds  */
qh_append_tds(struct ehci_hcd * ehci,struct urb * urb,struct list_head * qtd_list,int epnum,void ** ptr)10211da177e4SLinus Torvalds static struct ehci_qh *qh_append_tds (
10221da177e4SLinus Torvalds 	struct ehci_hcd		*ehci,
10231da177e4SLinus Torvalds 	struct urb		*urb,
10241da177e4SLinus Torvalds 	struct list_head	*qtd_list,
10251da177e4SLinus Torvalds 	int			epnum,
10261da177e4SLinus Torvalds 	void			**ptr
10271da177e4SLinus Torvalds )
10281da177e4SLinus Torvalds {
10291da177e4SLinus Torvalds 	struct ehci_qh		*qh = NULL;
1030fd05e720SAl Viro 	__hc32			qh_addr_mask = cpu_to_hc32(ehci, 0x7f);
10311da177e4SLinus Torvalds 
10321da177e4SLinus Torvalds 	qh = (struct ehci_qh *) *ptr;
10331da177e4SLinus Torvalds 	if (unlikely (qh == NULL)) {
10341da177e4SLinus Torvalds 		/* can't sleep here, we have ehci->lock... */
10351da177e4SLinus Torvalds 		qh = qh_make (ehci, urb, GFP_ATOMIC);
10361da177e4SLinus Torvalds 		*ptr = qh;
10371da177e4SLinus Torvalds 	}
10381da177e4SLinus Torvalds 	if (likely (qh != NULL)) {
10391da177e4SLinus Torvalds 		struct ehci_qtd	*qtd;
10401da177e4SLinus Torvalds 
10411da177e4SLinus Torvalds 		if (unlikely (list_empty (qtd_list)))
10421da177e4SLinus Torvalds 			qtd = NULL;
10431da177e4SLinus Torvalds 		else
10441da177e4SLinus Torvalds 			qtd = list_entry (qtd_list->next, struct ehci_qtd,
10451da177e4SLinus Torvalds 					qtd_list);
10461da177e4SLinus Torvalds 
10471da177e4SLinus Torvalds 		/* control qh may need patching ... */
10481da177e4SLinus Torvalds 		if (unlikely (epnum == 0)) {
10491da177e4SLinus Torvalds 
10501da177e4SLinus Torvalds                         /* usb_reset_device() briefly reverts to address 0 */
10511da177e4SLinus Torvalds                         if (usb_pipedevice (urb->pipe) == 0)
10523807e26dSAlek Du 				qh->hw->hw_info1 &= ~qh_addr_mask;
10531da177e4SLinus Torvalds 		}
10541da177e4SLinus Torvalds 
10551da177e4SLinus Torvalds 		/* just one way to queue requests: swap with the dummy qtd.
10561da177e4SLinus Torvalds 		 * only hc or qh_refresh() ever modify the overlay.
10571da177e4SLinus Torvalds 		 */
10581da177e4SLinus Torvalds 		if (likely (qtd != NULL)) {
10591da177e4SLinus Torvalds 			struct ehci_qtd		*dummy;
10601da177e4SLinus Torvalds 			dma_addr_t		dma;
10616dbd682bSStefan Roese 			__hc32			token;
10621da177e4SLinus Torvalds 
10631da177e4SLinus Torvalds 			/* to avoid racing the HC, use the dummy td instead of
10641da177e4SLinus Torvalds 			 * the first td of our list (becomes new dummy).  both
10651da177e4SLinus Torvalds 			 * tds stay deactivated until we're done, when the
10661da177e4SLinus Torvalds 			 * HC is allowed to fetch the old dummy (4.10.2).
10671da177e4SLinus Torvalds 			 */
10681da177e4SLinus Torvalds 			token = qtd->hw_token;
10696dbd682bSStefan Roese 			qtd->hw_token = HALT_BIT(ehci);
107041f05dedSMing Lei 
10711da177e4SLinus Torvalds 			dummy = qh->dummy;
10721da177e4SLinus Torvalds 
10731da177e4SLinus Torvalds 			dma = dummy->qtd_dma;
10741da177e4SLinus Torvalds 			*dummy = *qtd;
10751da177e4SLinus Torvalds 			dummy->qtd_dma = dma;
10761da177e4SLinus Torvalds 
10771da177e4SLinus Torvalds 			list_del (&qtd->qtd_list);
10781da177e4SLinus Torvalds 			list_add (&dummy->qtd_list, qtd_list);
10797d283aeeSLuis R. Rodriguez 			list_splice_tail(qtd_list, &qh->qtd_list);
10801da177e4SLinus Torvalds 
10816dbd682bSStefan Roese 			ehci_qtd_init(ehci, qtd, qtd->qtd_dma);
10821da177e4SLinus Torvalds 			qh->dummy = qtd;
10831da177e4SLinus Torvalds 
10841da177e4SLinus Torvalds 			/* hc must see the new dummy at list end */
10851da177e4SLinus Torvalds 			dma = qtd->qtd_dma;
10861da177e4SLinus Torvalds 			qtd = list_entry (qh->qtd_list.prev,
10871da177e4SLinus Torvalds 					struct ehci_qtd, qtd_list);
10886dbd682bSStefan Roese 			qtd->hw_next = QTD_NEXT(ehci, dma);
10891da177e4SLinus Torvalds 
10901da177e4SLinus Torvalds 			/* let the hc process these next qtds */
10911da177e4SLinus Torvalds 			wmb ();
10921da177e4SLinus Torvalds 			dummy->hw_token = token;
10931da177e4SLinus Torvalds 
1094c83e1a9fSAlan Stern 			urb->hcpriv = qh;
10951da177e4SLinus Torvalds 		}
10961da177e4SLinus Torvalds 	}
10971da177e4SLinus Torvalds 	return qh;
10981da177e4SLinus Torvalds }
10991da177e4SLinus Torvalds 
11001da177e4SLinus Torvalds /*-------------------------------------------------------------------------*/
11011da177e4SLinus Torvalds 
11021da177e4SLinus Torvalds static int
submit_async(struct ehci_hcd * ehci,struct urb * urb,struct list_head * qtd_list,gfp_t mem_flags)11031da177e4SLinus Torvalds submit_async (
11041da177e4SLinus Torvalds 	struct ehci_hcd		*ehci,
11051da177e4SLinus Torvalds 	struct urb		*urb,
11061da177e4SLinus Torvalds 	struct list_head	*qtd_list,
110755016f10SAl Viro 	gfp_t			mem_flags
11081da177e4SLinus Torvalds ) {
11091da177e4SLinus Torvalds 	int			epnum;
11101da177e4SLinus Torvalds 	unsigned long		flags;
11111da177e4SLinus Torvalds 	struct ehci_qh		*qh = NULL;
1112e9df41c5SAlan Stern 	int			rc;
11131da177e4SLinus Torvalds 
1114e9df41c5SAlan Stern 	epnum = urb->ep->desc.bEndpointAddress;
11151da177e4SLinus Torvalds 
11161da177e4SLinus Torvalds #ifdef EHCI_URB_TRACE
1117eb34a908SDavid Daney 	{
1118eb34a908SDavid Daney 		struct ehci_qtd *qtd;
1119eb34a908SDavid Daney 		qtd = list_entry(qtd_list->next, struct ehci_qtd, qtd_list);
11201da177e4SLinus Torvalds 		ehci_dbg(ehci,
11211da177e4SLinus Torvalds 			 "%s %s urb %p ep%d%s len %d, qtd %p [qh %p]\n",
1122441b62c1SHarvey Harrison 			 __func__, urb->dev->devpath, urb,
11231da177e4SLinus Torvalds 			 epnum & 0x0f, (epnum & USB_DIR_IN) ? "in" : "out",
11241da177e4SLinus Torvalds 			 urb->transfer_buffer_length,
1125e9df41c5SAlan Stern 			 qtd, urb->ep->hcpriv);
1126eb34a908SDavid Daney 	}
11271da177e4SLinus Torvalds #endif
11281da177e4SLinus Torvalds 
11291da177e4SLinus Torvalds 	spin_lock_irqsave (&ehci->lock, flags);
1130541c7d43SAlan Stern 	if (unlikely(!HCD_HW_ACCESSIBLE(ehci_to_hcd(ehci)))) {
11318de98402SBenjamin Herrenschmidt 		rc = -ESHUTDOWN;
11328de98402SBenjamin Herrenschmidt 		goto done;
11338de98402SBenjamin Herrenschmidt 	}
1134e9df41c5SAlan Stern 	rc = usb_hcd_link_urb_to_ep(ehci_to_hcd(ehci), urb);
1135e9df41c5SAlan Stern 	if (unlikely(rc))
1136e9df41c5SAlan Stern 		goto done;
11378de98402SBenjamin Herrenschmidt 
1138e9df41c5SAlan Stern 	qh = qh_append_tds(ehci, urb, qtd_list, epnum, &urb->ep->hcpriv);
11398de98402SBenjamin Herrenschmidt 	if (unlikely(qh == NULL)) {
1140e9df41c5SAlan Stern 		usb_hcd_unlink_urb_from_ep(ehci_to_hcd(ehci), urb);
11418de98402SBenjamin Herrenschmidt 		rc = -ENOMEM;
11428de98402SBenjamin Herrenschmidt 		goto done;
11438de98402SBenjamin Herrenschmidt 	}
11441da177e4SLinus Torvalds 
11451da177e4SLinus Torvalds 	/* Control/bulk operations through TTs don't need scheduling,
11461da177e4SLinus Torvalds 	 * the HC and TT handle it when the TT has a buffer ready.
11471da177e4SLinus Torvalds 	 */
11481da177e4SLinus Torvalds 	if (likely (qh->qh_state == QH_STATE_IDLE))
11497a0f0d95SAlan Stern 		qh_link_async(ehci, qh);
11508de98402SBenjamin Herrenschmidt  done:
11511da177e4SLinus Torvalds 	spin_unlock_irqrestore (&ehci->lock, flags);
11528de98402SBenjamin Herrenschmidt 	if (unlikely (qh == NULL))
11531da177e4SLinus Torvalds 		qtd_list_free (ehci, urb, qtd_list);
11548de98402SBenjamin Herrenschmidt 	return rc;
11551da177e4SLinus Torvalds }
11561da177e4SLinus Torvalds 
11571da177e4SLinus Torvalds /*-------------------------------------------------------------------------*/
1158726a85caSJack Pham #ifdef CONFIG_USB_HCD_TEST_MODE
11599841f37aSManu Gautam /*
11609841f37aSManu Gautam  * This function creates the qtds and submits them for the
11619841f37aSManu Gautam  * SINGLE_STEP_SET_FEATURE Test.
11629841f37aSManu Gautam  * This is done in two parts: first SETUP req for GetDesc is sent then
11639841f37aSManu Gautam  * 15 seconds later, the IN stage for GetDesc starts to req data from dev
11649841f37aSManu Gautam  *
11655c586db8SHyunwoo Kim  * is_setup : i/p argument decides which of the two stage needs to be
11669841f37aSManu Gautam  * performed; TRUE - SETUP and FALSE - IN+STATUS
11679841f37aSManu Gautam  * Returns 0 if success
11689841f37aSManu Gautam  */
ehci_submit_single_step_set_feature(struct usb_hcd * hcd,struct urb * urb,int is_setup)1169cbbc07e1SPeter Chen static int ehci_submit_single_step_set_feature(
11709841f37aSManu Gautam 	struct usb_hcd  *hcd,
11719841f37aSManu Gautam 	struct urb      *urb,
11729841f37aSManu Gautam 	int             is_setup
11739841f37aSManu Gautam ) {
11749841f37aSManu Gautam 	struct ehci_hcd		*ehci = hcd_to_ehci(hcd);
11759841f37aSManu Gautam 	struct list_head	qtd_list;
11769841f37aSManu Gautam 	struct list_head	*head;
11779841f37aSManu Gautam 
11789841f37aSManu Gautam 	struct ehci_qtd		*qtd, *qtd_prev;
11799841f37aSManu Gautam 	dma_addr_t		buf;
11809841f37aSManu Gautam 	int			len, maxpacket;
11819841f37aSManu Gautam 	u32			token;
11829841f37aSManu Gautam 
11839841f37aSManu Gautam 	INIT_LIST_HEAD(&qtd_list);
11849841f37aSManu Gautam 	head = &qtd_list;
11859841f37aSManu Gautam 
11869841f37aSManu Gautam 	/* URBs map to sequences of QTDs:  one logical transaction */
11879841f37aSManu Gautam 	qtd = ehci_qtd_alloc(ehci, GFP_KERNEL);
11889841f37aSManu Gautam 	if (unlikely(!qtd))
11899841f37aSManu Gautam 		return -1;
11909841f37aSManu Gautam 	list_add_tail(&qtd->qtd_list, head);
11919841f37aSManu Gautam 	qtd->urb = urb;
11929841f37aSManu Gautam 
11939841f37aSManu Gautam 	token = QTD_STS_ACTIVE;
11949841f37aSManu Gautam 	token |= (EHCI_TUNE_CERR << 10);
11959841f37aSManu Gautam 
11969841f37aSManu Gautam 	len = urb->transfer_buffer_length;
11979841f37aSManu Gautam 	/*
11989841f37aSManu Gautam 	 * Check if the request is to perform just the SETUP stage (getDesc)
11999841f37aSManu Gautam 	 * as in SINGLE_STEP_SET_FEATURE test, DATA stage (IN) happens
12009841f37aSManu Gautam 	 * 15 secs after the setup
12019841f37aSManu Gautam 	 */
12029841f37aSManu Gautam 	if (is_setup) {
120391b11935SPeter Chen 		/* SETUP pid, and interrupt after SETUP completion */
12049841f37aSManu Gautam 		qtd_fill(ehci, qtd, urb->setup_dma,
12059841f37aSManu Gautam 				sizeof(struct usb_ctrlrequest),
120691b11935SPeter Chen 				QTD_IOC | token | (2 /* "setup" */ << 8), 8);
12079841f37aSManu Gautam 
12089841f37aSManu Gautam 		submit_async(ehci, urb, &qtd_list, GFP_ATOMIC);
12099841f37aSManu Gautam 		return 0; /*Return now; we shall come back after 15 seconds*/
12109841f37aSManu Gautam 	}
12119841f37aSManu Gautam 
12129841f37aSManu Gautam 	/*
12139841f37aSManu Gautam 	 * IN: data transfer stage:  buffer setup : start the IN txn phase for
12149841f37aSManu Gautam 	 * the get_Desc SETUP which was sent 15seconds back
12159841f37aSManu Gautam 	 */
12169841f37aSManu Gautam 	token ^= QTD_TOGGLE;   /*We need to start IN with DATA-1 Pid-sequence*/
12179841f37aSManu Gautam 	buf = urb->transfer_dma;
12189841f37aSManu Gautam 
12199841f37aSManu Gautam 	token |= (1 /* "in" */ << 8);  /*This is IN stage*/
12209841f37aSManu Gautam 
1221*d27c66adSKhalid Masum 	maxpacket = usb_endpoint_maxp(&urb->ep->desc);
12229841f37aSManu Gautam 
12239841f37aSManu Gautam 	qtd_fill(ehci, qtd, buf, len, token, maxpacket);
12249841f37aSManu Gautam 
12259841f37aSManu Gautam 	/*
12269841f37aSManu Gautam 	 * Our IN phase shall always be a short read; so keep the queue running
12279841f37aSManu Gautam 	 * and let it advance to the next qtd which zero length OUT status
12289841f37aSManu Gautam 	 */
12299841f37aSManu Gautam 	qtd->hw_alt_next = EHCI_LIST_END(ehci);
12309841f37aSManu Gautam 
12319841f37aSManu Gautam 	/* STATUS stage for GetDesc control request */
12329841f37aSManu Gautam 	token ^= 0x0100;        /* "in" <--> "out"  */
12339841f37aSManu Gautam 	token |= QTD_TOGGLE;    /* force DATA1 */
12349841f37aSManu Gautam 
12359841f37aSManu Gautam 	qtd_prev = qtd;
12369841f37aSManu Gautam 	qtd = ehci_qtd_alloc(ehci, GFP_ATOMIC);
12379841f37aSManu Gautam 	if (unlikely(!qtd))
12389841f37aSManu Gautam 		goto cleanup;
12399841f37aSManu Gautam 	qtd->urb = urb;
12409841f37aSManu Gautam 	qtd_prev->hw_next = QTD_NEXT(ehci, qtd->qtd_dma);
12419841f37aSManu Gautam 	list_add_tail(&qtd->qtd_list, head);
12429841f37aSManu Gautam 
124391b11935SPeter Chen 	/* Interrupt after STATUS completion */
124491b11935SPeter Chen 	qtd_fill(ehci, qtd, 0, 0, token | QTD_IOC, 0);
12459841f37aSManu Gautam 
12469841f37aSManu Gautam 	submit_async(ehci, urb, &qtd_list, GFP_KERNEL);
12479841f37aSManu Gautam 
12489841f37aSManu Gautam 	return 0;
12499841f37aSManu Gautam 
12509841f37aSManu Gautam cleanup:
12519841f37aSManu Gautam 	qtd_list_free(ehci, urb, head);
12529841f37aSManu Gautam 	return -1;
12539841f37aSManu Gautam }
1254726a85caSJack Pham #endif /* CONFIG_USB_HCD_TEST_MODE */
12559841f37aSManu Gautam 
12569841f37aSManu Gautam /*-------------------------------------------------------------------------*/
12571da177e4SLinus Torvalds 
single_unlink_async(struct ehci_hcd * ehci,struct ehci_qh * qh)12583c273a05SAlan Stern static void single_unlink_async(struct ehci_hcd *ehci, struct ehci_qh *qh)
12591da177e4SLinus Torvalds {
12601da177e4SLinus Torvalds 	struct ehci_qh		*prev;
12611da177e4SLinus Torvalds 
12623c273a05SAlan Stern 	/* Add to the end of the list of QHs waiting for the next IAAD */
12636402c796SAlan Stern 	qh->qh_state = QH_STATE_UNLINK_WAIT;
12646e018751SAlan Stern 	list_add_tail(&qh->unlink_node, &ehci->async_unlink);
12651da177e4SLinus Torvalds 
12663c273a05SAlan Stern 	/* Unlink it from the schedule */
12671da177e4SLinus Torvalds 	prev = ehci->async;
12681da177e4SLinus Torvalds 	while (prev->qh_next.qh != qh)
12691da177e4SLinus Torvalds 		prev = prev->qh_next.qh;
12701da177e4SLinus Torvalds 
12713807e26dSAlek Du 	prev->hw->hw_next = qh->hw->hw_next;
12721da177e4SLinus Torvalds 	prev->qh_next = qh->qh_next;
1273004c1968SAlan Stern 	if (ehci->qh_scan_next == qh)
1274004c1968SAlan Stern 		ehci->qh_scan_next = qh->qh_next.qh;
12753c273a05SAlan Stern }
12763c273a05SAlan Stern 
start_iaa_cycle(struct ehci_hcd * ehci)1277214ac7a0SAlan Stern static void start_iaa_cycle(struct ehci_hcd *ehci)
12783c273a05SAlan Stern {
12796e0c3339SAlan Stern 	/* If the controller isn't running, we don't have to wait for it */
12806e0c3339SAlan Stern 	if (unlikely(ehci->rh_state < EHCI_RH_RUNNING)) {
12817d12e780SDavid Howells 		end_unlink_async(ehci);
12823c273a05SAlan Stern 
1283f96fba0dSAlan Stern 	/* Otherwise start a new IAA cycle if one isn't already running */
1284f96fba0dSAlan Stern 	} else if (ehci->rh_state == EHCI_RH_RUNNING &&
1285f96fba0dSAlan Stern 			!ehci->iaa_in_progress) {
12866e0c3339SAlan Stern 
12873c273a05SAlan Stern 		/* Make sure the unlinks are all visible to the hardware */
12883c273a05SAlan Stern 		wmb();
12893c273a05SAlan Stern 
12903c273a05SAlan Stern 		ehci_writel(ehci, ehci->command | CMD_IAAD,
12913c273a05SAlan Stern 				&ehci->regs->command);
12923c273a05SAlan Stern 		ehci_readl(ehci, &ehci->regs->command);
1293f96fba0dSAlan Stern 		ehci->iaa_in_progress = true;
12943c273a05SAlan Stern 		ehci_enable_event(ehci, EHCI_HRTIMER_IAA_WATCHDOG, true);
12953c273a05SAlan Stern 	}
12963c273a05SAlan Stern }
12973c273a05SAlan Stern 
end_iaa_cycle(struct ehci_hcd * ehci)1298f96fba0dSAlan Stern static void end_iaa_cycle(struct ehci_hcd *ehci)
12993c273a05SAlan Stern {
13003c273a05SAlan Stern 	if (ehci->has_synopsys_hc_bug)
13013c273a05SAlan Stern 		ehci_writel(ehci, (u32) ehci->async->qh_dma,
13023c273a05SAlan Stern 			    &ehci->regs->async_next);
13033c273a05SAlan Stern 
1304214ac7a0SAlan Stern 	/* The current IAA cycle has ended */
1305214ac7a0SAlan Stern 	ehci->iaa_in_progress = false;
1306214ac7a0SAlan Stern 
1307f96fba0dSAlan Stern 	end_unlink_async(ehci);
1308f96fba0dSAlan Stern }
1309f96fba0dSAlan Stern 
1310f96fba0dSAlan Stern /* See if the async qh for the qtds being unlinked are now gone from the HC */
1311f96fba0dSAlan Stern 
end_unlink_async(struct ehci_hcd * ehci)1312f96fba0dSAlan Stern static void end_unlink_async(struct ehci_hcd *ehci)
1313f96fba0dSAlan Stern {
1314f96fba0dSAlan Stern 	struct ehci_qh		*qh;
1315f96fba0dSAlan Stern 	bool			early_exit;
1316f96fba0dSAlan Stern 
1317214ac7a0SAlan Stern 	if (list_empty(&ehci->async_unlink))
1318214ac7a0SAlan Stern 		return;
1319214ac7a0SAlan Stern 	qh = list_first_entry(&ehci->async_unlink, struct ehci_qh,
1320214ac7a0SAlan Stern 			unlink_node);	/* QH whose IAA cycle just ended */
1321214ac7a0SAlan Stern 
1322214ac7a0SAlan Stern 	/*
1323214ac7a0SAlan Stern 	 * If async_unlinking is set then this routine is already running,
1324214ac7a0SAlan Stern 	 * either on the stack or on another CPU.
1325214ac7a0SAlan Stern 	 */
1326214ac7a0SAlan Stern 	early_exit = ehci->async_unlinking;
1327214ac7a0SAlan Stern 
1328214ac7a0SAlan Stern 	/* If the controller isn't running, process all the waiting QHs */
1329214ac7a0SAlan Stern 	if (ehci->rh_state < EHCI_RH_RUNNING)
1330214ac7a0SAlan Stern 		list_splice_tail_init(&ehci->async_unlink, &ehci->async_idle);
1331214ac7a0SAlan Stern 
1332214ac7a0SAlan Stern 	/*
1333214ac7a0SAlan Stern 	 * Intel (?) bug: The HC can write back the overlay region even
1334214ac7a0SAlan Stern 	 * after the IAA interrupt occurs.  In self-defense, always go
1335214ac7a0SAlan Stern 	 * through two IAA cycles for each QH.
1336214ac7a0SAlan Stern 	 */
133787d61912SAlan Stern 	else if (qh->qh_state == QH_STATE_UNLINK) {
133887d61912SAlan Stern 		/*
133987d61912SAlan Stern 		 * Second IAA cycle has finished.  Process only the first
134087d61912SAlan Stern 		 * waiting QH (NVIDIA (?) bug).
134187d61912SAlan Stern 		 */
134287d61912SAlan Stern 		list_move_tail(&qh->unlink_node, &ehci->async_idle);
134387d61912SAlan Stern 	}
134487d61912SAlan Stern 
134587d61912SAlan Stern 	/*
134687d61912SAlan Stern 	 * AMD/ATI (?) bug: The HC can continue to use an active QH long
134787d61912SAlan Stern 	 * after the IAA interrupt occurs.  To prevent problems, QHs that
134887d61912SAlan Stern 	 * may still be active will wait until 2 ms have passed with no
134987d61912SAlan Stern 	 * change to the hw_current and hw_token fields (this delay occurs
135087d61912SAlan Stern 	 * between the two IAA cycles).
135187d61912SAlan Stern 	 *
135287d61912SAlan Stern 	 * The EHCI spec (4.8.2) says that active QHs must not be removed
135387d61912SAlan Stern 	 * from the async schedule and recommends waiting until the QH
135487d61912SAlan Stern 	 * goes inactive.  This is ridiculous because the QH will _never_
135587d61912SAlan Stern 	 * become inactive if the endpoint NAKs indefinitely.
135687d61912SAlan Stern 	 */
135787d61912SAlan Stern 
135887d61912SAlan Stern 	/* Some reasons for unlinking guarantee the QH can't be active */
135987d61912SAlan Stern 	else if (qh->unlink_reason & (QH_UNLINK_HALTED |
136087d61912SAlan Stern 			QH_UNLINK_SHORT_READ | QH_UNLINK_DUMMY_OVERLAY))
136187d61912SAlan Stern 		goto DelayDone;
136287d61912SAlan Stern 
136387d61912SAlan Stern 	/* The QH can't be active if the queue was and still is empty... */
136487d61912SAlan Stern 	else if	((qh->unlink_reason & QH_UNLINK_QUEUE_EMPTY) &&
136587d61912SAlan Stern 			list_empty(&qh->qtd_list))
136687d61912SAlan Stern 		goto DelayDone;
136787d61912SAlan Stern 
136887d61912SAlan Stern 	/* ... or if the QH has halted */
136987d61912SAlan Stern 	else if	(qh->hw->hw_token & cpu_to_hc32(ehci, QTD_STS_HALT))
137087d61912SAlan Stern 		goto DelayDone;
137187d61912SAlan Stern 
137287d61912SAlan Stern 	/* Otherwise we have to wait until the QH stops changing */
137387d61912SAlan Stern 	else {
137487d61912SAlan Stern 		__hc32		qh_current, qh_token;
137587d61912SAlan Stern 
137687d61912SAlan Stern 		qh_current = qh->hw->hw_current;
137787d61912SAlan Stern 		qh_token = qh->hw->hw_token;
137887d61912SAlan Stern 		if (qh_current != ehci->old_current ||
137987d61912SAlan Stern 				qh_token != ehci->old_token) {
138087d61912SAlan Stern 			ehci->old_current = qh_current;
138187d61912SAlan Stern 			ehci->old_token = qh_token;
138287d61912SAlan Stern 			ehci_enable_event(ehci,
138387d61912SAlan Stern 					EHCI_HRTIMER_ACTIVE_UNLINK, true);
138487d61912SAlan Stern 			return;
138587d61912SAlan Stern 		}
138687d61912SAlan Stern  DelayDone:
1387214ac7a0SAlan Stern 		qh->qh_state = QH_STATE_UNLINK;
1388214ac7a0SAlan Stern 		early_exit = true;
1389214ac7a0SAlan Stern 	}
139087d61912SAlan Stern 	ehci->old_current = ~0;		/* Prepare for next QH */
1391214ac7a0SAlan Stern 
1392214ac7a0SAlan Stern 	/* Start a new IAA cycle if any QHs are waiting for it */
1393214ac7a0SAlan Stern 	if (!list_empty(&ehci->async_unlink))
1394214ac7a0SAlan Stern 		start_iaa_cycle(ehci);
1395214ac7a0SAlan Stern 
1396214ac7a0SAlan Stern 	/*
1397214ac7a0SAlan Stern 	 * Don't allow nesting or concurrent calls,
1398214ac7a0SAlan Stern 	 * or wait for the second IAA cycle for the next QH.
1399214ac7a0SAlan Stern 	 */
1400214ac7a0SAlan Stern 	if (early_exit)
1401214ac7a0SAlan Stern 		return;
1402214ac7a0SAlan Stern 
14033c273a05SAlan Stern 	/* Process the idle QHs */
14043c273a05SAlan Stern 	ehci->async_unlinking = true;
1405214ac7a0SAlan Stern 	while (!list_empty(&ehci->async_idle)) {
1406214ac7a0SAlan Stern 		qh = list_first_entry(&ehci->async_idle, struct ehci_qh,
14076e018751SAlan Stern 				unlink_node);
14086e018751SAlan Stern 		list_del(&qh->unlink_node);
14093c273a05SAlan Stern 
14103c273a05SAlan Stern 		qh->qh_state = QH_STATE_IDLE;
14113c273a05SAlan Stern 		qh->qh_next.qh = NULL;
14123c273a05SAlan Stern 
141379bcf7b0SAlan Stern 		if (!list_empty(&qh->qtd_list))
14143c273a05SAlan Stern 			qh_completions(ehci, qh);
14153c273a05SAlan Stern 		if (!list_empty(&qh->qtd_list) &&
14163c273a05SAlan Stern 				ehci->rh_state == EHCI_RH_RUNNING)
14173c273a05SAlan Stern 			qh_link_async(ehci, qh);
14183c273a05SAlan Stern 		disable_async(ehci);
14193c273a05SAlan Stern 	}
14203c273a05SAlan Stern 	ehci->async_unlinking = false;
14213c273a05SAlan Stern }
14223c273a05SAlan Stern 
14236e0c3339SAlan Stern static void start_unlink_async(struct ehci_hcd *ehci, struct ehci_qh *qh);
14246e0c3339SAlan Stern 
unlink_empty_async(struct ehci_hcd * ehci)142532830f20SAlan Stern static void unlink_empty_async(struct ehci_hcd *ehci)
142632830f20SAlan Stern {
14276e0c3339SAlan Stern 	struct ehci_qh		*qh;
14286e0c3339SAlan Stern 	struct ehci_qh		*qh_to_unlink = NULL;
14296e0c3339SAlan Stern 	int			count = 0;
143032830f20SAlan Stern 
14316e0c3339SAlan Stern 	/* Find the last async QH which has been empty for a timer cycle */
14326e0c3339SAlan Stern 	for (qh = ehci->async->qh_next.qh; qh; qh = qh->qh_next.qh) {
143332830f20SAlan Stern 		if (list_empty(&qh->qtd_list) &&
143432830f20SAlan Stern 				qh->qh_state == QH_STATE_LINKED) {
14356e0c3339SAlan Stern 			++count;
1436afc2c9a2SAlan Stern 			if (qh->unlink_cycle != ehci->async_unlink_cycle)
14376e0c3339SAlan Stern 				qh_to_unlink = qh;
143832830f20SAlan Stern 		}
143932830f20SAlan Stern 	}
144032830f20SAlan Stern 
14416e0c3339SAlan Stern 	/* If nothing else is being unlinked, unlink the last empty QH */
1442214ac7a0SAlan Stern 	if (list_empty(&ehci->async_unlink) && qh_to_unlink) {
1443fcc5184eSAlan Stern 		qh_to_unlink->unlink_reason |= QH_UNLINK_QUEUE_EMPTY;
14446e0c3339SAlan Stern 		start_unlink_async(ehci, qh_to_unlink);
14456e0c3339SAlan Stern 		--count;
14466e0c3339SAlan Stern 	}
144732830f20SAlan Stern 
14486e0c3339SAlan Stern 	/* Other QHs will be handled later */
14496e0c3339SAlan Stern 	if (count > 0) {
145032830f20SAlan Stern 		ehci_enable_event(ehci, EHCI_HRTIMER_ASYNC_UNLINKS, true);
145132830f20SAlan Stern 		++ehci->async_unlink_cycle;
145232830f20SAlan Stern 	}
145332830f20SAlan Stern }
145432830f20SAlan Stern 
14558df0d77dSAlan Stern #ifdef	CONFIG_PM
14568df0d77dSAlan Stern 
14572a40f324SAlan Stern /* The root hub is suspended; unlink all the async QHs */
unlink_empty_async_suspended(struct ehci_hcd * ehci)14588df0d77dSAlan Stern static void unlink_empty_async_suspended(struct ehci_hcd *ehci)
14592a40f324SAlan Stern {
14602a40f324SAlan Stern 	struct ehci_qh		*qh;
14612a40f324SAlan Stern 
14622a40f324SAlan Stern 	while (ehci->async->qh_next.qh) {
14632a40f324SAlan Stern 		qh = ehci->async->qh_next.qh;
14642a40f324SAlan Stern 		WARN_ON(!list_empty(&qh->qtd_list));
14652a40f324SAlan Stern 		single_unlink_async(ehci, qh);
14662a40f324SAlan Stern 	}
14672a40f324SAlan Stern }
14682a40f324SAlan Stern 
14698df0d77dSAlan Stern #endif
14708df0d77dSAlan Stern 
14713c273a05SAlan Stern /* makes sure the async qh will become idle */
14723c273a05SAlan Stern /* caller must own ehci->lock */
14733c273a05SAlan Stern 
start_unlink_async(struct ehci_hcd * ehci,struct ehci_qh * qh)14743c273a05SAlan Stern static void start_unlink_async(struct ehci_hcd *ehci, struct ehci_qh *qh)
14753c273a05SAlan Stern {
14767bc782d7SAlan Stern 	/* If the QH isn't linked then there's nothing we can do. */
14777bc782d7SAlan Stern 	if (qh->qh_state != QH_STATE_LINKED)
14781da177e4SLinus Torvalds 		return;
14791da177e4SLinus Torvalds 
14803c273a05SAlan Stern 	single_unlink_async(ehci, qh);
1481214ac7a0SAlan Stern 	start_iaa_cycle(ehci);
14821da177e4SLinus Torvalds }
14831da177e4SLinus Torvalds 
14841da177e4SLinus Torvalds /*-------------------------------------------------------------------------*/
14851da177e4SLinus Torvalds 
scan_async(struct ehci_hcd * ehci)14867d12e780SDavid Howells static void scan_async (struct ehci_hcd *ehci)
14871da177e4SLinus Torvalds {
14881da177e4SLinus Torvalds 	struct ehci_qh		*qh;
148932830f20SAlan Stern 	bool			check_unlinks_later = false;
1490004c1968SAlan Stern 
1491004c1968SAlan Stern 	ehci->qh_scan_next = ehci->async->qh_next.qh;
1492004c1968SAlan Stern 	while (ehci->qh_scan_next) {
1493004c1968SAlan Stern 		qh = ehci->qh_scan_next;
1494004c1968SAlan Stern 		ehci->qh_scan_next = qh->qh_next.qh;
149579bcf7b0SAlan Stern 
14961da177e4SLinus Torvalds 		/* clean any finished work for this qh */
1497004c1968SAlan Stern 		if (!list_empty(&qh->qtd_list)) {
14981da177e4SLinus Torvalds 			int temp;
14991da177e4SLinus Torvalds 
1500004c1968SAlan Stern 			/*
1501004c1968SAlan Stern 			 * Unlinks could happen here; completion reporting
1502004c1968SAlan Stern 			 * drops the lock.  That's why ehci->qh_scan_next
1503004c1968SAlan Stern 			 * always holds the next qh to scan; if the next qh
1504004c1968SAlan Stern 			 * gets unlinked then ehci->qh_scan_next is adjusted
15053c273a05SAlan Stern 			 * in single_unlink_async().
15061da177e4SLinus Torvalds 			 */
15077d12e780SDavid Howells 			temp = qh_completions(ehci, qh);
150879bcf7b0SAlan Stern 			if (unlikely(temp)) {
15093c273a05SAlan Stern 				start_unlink_async(ehci, qh);
151032830f20SAlan Stern 			} else if (list_empty(&qh->qtd_list)
151132830f20SAlan Stern 					&& qh->qh_state == QH_STATE_LINKED) {
151232830f20SAlan Stern 				qh->unlink_cycle = ehci->async_unlink_cycle;
151332830f20SAlan Stern 				check_unlinks_later = true;
151479bcf7b0SAlan Stern 			}
15151da177e4SLinus Torvalds 		}
151632830f20SAlan Stern 	}
15171da177e4SLinus Torvalds 
151832830f20SAlan Stern 	/*
151932830f20SAlan Stern 	 * Unlink empty entries, reducing DMA usage as well
152032830f20SAlan Stern 	 * as HCD schedule-scanning costs.  Delay for any qh
15211da177e4SLinus Torvalds 	 * we just scanned, there's a not-unusual case that it
15221da177e4SLinus Torvalds 	 * doesn't stay idle for long.
15231da177e4SLinus Torvalds 	 */
152432830f20SAlan Stern 	if (check_unlinks_later && ehci->rh_state == EHCI_RH_RUNNING &&
152532830f20SAlan Stern 			!(ehci->enabled_hrtimer_events &
152632830f20SAlan Stern 				BIT(EHCI_HRTIMER_ASYNC_UNLINKS))) {
152732830f20SAlan Stern 		ehci_enable_event(ehci, EHCI_HRTIMER_ASYNC_UNLINKS, true);
152832830f20SAlan Stern 		++ehci->async_unlink_cycle;
15291da177e4SLinus Torvalds 	}
15301da177e4SLinus Torvalds }
1531