1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0+
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * Copyright (C) 2001-2004 by David Brownell
4*4882a593Smuzhiyun */
5*4882a593Smuzhiyun
6*4882a593Smuzhiyun /* this file is part of ehci-hcd.c */
7*4882a593Smuzhiyun
8*4882a593Smuzhiyun /*-------------------------------------------------------------------------*/
9*4882a593Smuzhiyun
10*4882a593Smuzhiyun /*
11*4882a593Smuzhiyun * EHCI hardware queue manipulation ... the core. QH/QTD manipulation.
12*4882a593Smuzhiyun *
13*4882a593Smuzhiyun * Control, bulk, and interrupt traffic all use "qh" lists. They list "qtd"
14*4882a593Smuzhiyun * entries describing USB transactions, max 16-20kB/entry (with 4kB-aligned
15*4882a593Smuzhiyun * buffers needed for the larger number). We use one QH per endpoint, queue
16*4882a593Smuzhiyun * multiple urbs (all three types) per endpoint. URBs may need several qtds.
17*4882a593Smuzhiyun *
18*4882a593Smuzhiyun * ISO traffic uses "ISO TD" (itd, and sitd) records, and (along with
19*4882a593Smuzhiyun * interrupts) needs careful scheduling. Performance improvements can be
20*4882a593Smuzhiyun * an ongoing challenge. That's in "ehci-sched.c".
21*4882a593Smuzhiyun *
22*4882a593Smuzhiyun * USB 1.1 devices are handled (a) by "companion" OHCI or UHCI root hubs,
23*4882a593Smuzhiyun * or otherwise through transaction translators (TTs) in USB 2.0 hubs using
24*4882a593Smuzhiyun * (b) special fields in qh entries or (c) split iso entries. TTs will
25*4882a593Smuzhiyun * buffer low/full speed data so the host collects it at high speed.
26*4882a593Smuzhiyun */
27*4882a593Smuzhiyun
28*4882a593Smuzhiyun /*-------------------------------------------------------------------------*/
29*4882a593Smuzhiyun
30*4882a593Smuzhiyun /* PID Codes that are used here, from EHCI specification, Table 3-16. */
31*4882a593Smuzhiyun #define PID_CODE_IN 1
32*4882a593Smuzhiyun #define PID_CODE_SETUP 2
33*4882a593Smuzhiyun
34*4882a593Smuzhiyun /* fill a qtd, returning how much of the buffer we were able to queue up */
35*4882a593Smuzhiyun
36*4882a593Smuzhiyun static int
qtd_fill(struct ehci_hcd * ehci,struct ehci_qtd * qtd,dma_addr_t buf,size_t len,int token,int maxpacket)37*4882a593Smuzhiyun qtd_fill(struct ehci_hcd *ehci, struct ehci_qtd *qtd, dma_addr_t buf,
38*4882a593Smuzhiyun size_t len, int token, int maxpacket)
39*4882a593Smuzhiyun {
40*4882a593Smuzhiyun int i, count;
41*4882a593Smuzhiyun u64 addr = buf;
42*4882a593Smuzhiyun
43*4882a593Smuzhiyun /* one buffer entry per 4K ... first might be short or unaligned */
44*4882a593Smuzhiyun qtd->hw_buf[0] = cpu_to_hc32(ehci, (u32)addr);
45*4882a593Smuzhiyun qtd->hw_buf_hi[0] = cpu_to_hc32(ehci, (u32)(addr >> 32));
46*4882a593Smuzhiyun count = 0x1000 - (buf & 0x0fff); /* rest of that page */
47*4882a593Smuzhiyun if (likely (len < count)) /* ... iff needed */
48*4882a593Smuzhiyun count = len;
49*4882a593Smuzhiyun else {
50*4882a593Smuzhiyun buf += 0x1000;
51*4882a593Smuzhiyun buf &= ~0x0fff;
52*4882a593Smuzhiyun
53*4882a593Smuzhiyun /* per-qtd limit: from 16K to 20K (best alignment) */
54*4882a593Smuzhiyun for (i = 1; count < len && i < 5; i++) {
55*4882a593Smuzhiyun addr = buf;
56*4882a593Smuzhiyun qtd->hw_buf[i] = cpu_to_hc32(ehci, (u32)addr);
57*4882a593Smuzhiyun qtd->hw_buf_hi[i] = cpu_to_hc32(ehci,
58*4882a593Smuzhiyun (u32)(addr >> 32));
59*4882a593Smuzhiyun buf += 0x1000;
60*4882a593Smuzhiyun if ((count + 0x1000) < len)
61*4882a593Smuzhiyun count += 0x1000;
62*4882a593Smuzhiyun else
63*4882a593Smuzhiyun count = len;
64*4882a593Smuzhiyun }
65*4882a593Smuzhiyun
66*4882a593Smuzhiyun /* short packets may only terminate transfers */
67*4882a593Smuzhiyun if (count != len)
68*4882a593Smuzhiyun count -= (count % maxpacket);
69*4882a593Smuzhiyun }
70*4882a593Smuzhiyun qtd->hw_token = cpu_to_hc32(ehci, (count << 16) | token);
71*4882a593Smuzhiyun qtd->length = count;
72*4882a593Smuzhiyun
73*4882a593Smuzhiyun return count;
74*4882a593Smuzhiyun }
75*4882a593Smuzhiyun
76*4882a593Smuzhiyun /*-------------------------------------------------------------------------*/
77*4882a593Smuzhiyun
78*4882a593Smuzhiyun static inline void
qh_update(struct ehci_hcd * ehci,struct ehci_qh * qh,struct ehci_qtd * qtd)79*4882a593Smuzhiyun qh_update (struct ehci_hcd *ehci, struct ehci_qh *qh, struct ehci_qtd *qtd)
80*4882a593Smuzhiyun {
81*4882a593Smuzhiyun struct ehci_qh_hw *hw = qh->hw;
82*4882a593Smuzhiyun
83*4882a593Smuzhiyun /* writes to an active overlay are unsafe */
84*4882a593Smuzhiyun WARN_ON(qh->qh_state != QH_STATE_IDLE);
85*4882a593Smuzhiyun
86*4882a593Smuzhiyun hw->hw_qtd_next = QTD_NEXT(ehci, qtd->qtd_dma);
87*4882a593Smuzhiyun hw->hw_alt_next = EHCI_LIST_END(ehci);
88*4882a593Smuzhiyun
89*4882a593Smuzhiyun /* Except for control endpoints, we make hardware maintain data
90*4882a593Smuzhiyun * toggle (like OHCI) ... here (re)initialize the toggle in the QH,
91*4882a593Smuzhiyun * and set the pseudo-toggle in udev. Only usb_clear_halt() will
92*4882a593Smuzhiyun * ever clear it.
93*4882a593Smuzhiyun */
94*4882a593Smuzhiyun if (!(hw->hw_info1 & cpu_to_hc32(ehci, QH_TOGGLE_CTL))) {
95*4882a593Smuzhiyun unsigned is_out, epnum;
96*4882a593Smuzhiyun
97*4882a593Smuzhiyun is_out = qh->is_out;
98*4882a593Smuzhiyun epnum = (hc32_to_cpup(ehci, &hw->hw_info1) >> 8) & 0x0f;
99*4882a593Smuzhiyun if (unlikely(!usb_gettoggle(qh->ps.udev, epnum, is_out))) {
100*4882a593Smuzhiyun hw->hw_token &= ~cpu_to_hc32(ehci, QTD_TOGGLE);
101*4882a593Smuzhiyun usb_settoggle(qh->ps.udev, epnum, is_out, 1);
102*4882a593Smuzhiyun }
103*4882a593Smuzhiyun }
104*4882a593Smuzhiyun
105*4882a593Smuzhiyun hw->hw_token &= cpu_to_hc32(ehci, QTD_TOGGLE | QTD_STS_PING);
106*4882a593Smuzhiyun }
107*4882a593Smuzhiyun
108*4882a593Smuzhiyun /* if it weren't for a common silicon quirk (writing the dummy into the qh
109*4882a593Smuzhiyun * overlay, so qh->hw_token wrongly becomes inactive/halted), only fault
110*4882a593Smuzhiyun * recovery (including urb dequeue) would need software changes to a QH...
111*4882a593Smuzhiyun */
112*4882a593Smuzhiyun static void
qh_refresh(struct ehci_hcd * ehci,struct ehci_qh * qh)113*4882a593Smuzhiyun qh_refresh (struct ehci_hcd *ehci, struct ehci_qh *qh)
114*4882a593Smuzhiyun {
115*4882a593Smuzhiyun struct ehci_qtd *qtd;
116*4882a593Smuzhiyun
117*4882a593Smuzhiyun qtd = list_entry(qh->qtd_list.next, struct ehci_qtd, qtd_list);
118*4882a593Smuzhiyun
119*4882a593Smuzhiyun /*
120*4882a593Smuzhiyun * first qtd may already be partially processed.
121*4882a593Smuzhiyun * If we come here during unlink, the QH overlay region
122*4882a593Smuzhiyun * might have reference to the just unlinked qtd. The
123*4882a593Smuzhiyun * qtd is updated in qh_completions(). Update the QH
124*4882a593Smuzhiyun * overlay here.
125*4882a593Smuzhiyun */
126*4882a593Smuzhiyun if (qh->hw->hw_token & ACTIVE_BIT(ehci)) {
127*4882a593Smuzhiyun qh->hw->hw_qtd_next = qtd->hw_next;
128*4882a593Smuzhiyun if (qh->should_be_inactive)
129*4882a593Smuzhiyun ehci_warn(ehci, "qh %p should be inactive!\n", qh);
130*4882a593Smuzhiyun } else {
131*4882a593Smuzhiyun qh_update(ehci, qh, qtd);
132*4882a593Smuzhiyun }
133*4882a593Smuzhiyun qh->should_be_inactive = 0;
134*4882a593Smuzhiyun }
135*4882a593Smuzhiyun
136*4882a593Smuzhiyun /*-------------------------------------------------------------------------*/
137*4882a593Smuzhiyun
138*4882a593Smuzhiyun static void qh_link_async(struct ehci_hcd *ehci, struct ehci_qh *qh);
139*4882a593Smuzhiyun
ehci_clear_tt_buffer_complete(struct usb_hcd * hcd,struct usb_host_endpoint * ep)140*4882a593Smuzhiyun static void ehci_clear_tt_buffer_complete(struct usb_hcd *hcd,
141*4882a593Smuzhiyun struct usb_host_endpoint *ep)
142*4882a593Smuzhiyun {
143*4882a593Smuzhiyun struct ehci_hcd *ehci = hcd_to_ehci(hcd);
144*4882a593Smuzhiyun struct ehci_qh *qh = ep->hcpriv;
145*4882a593Smuzhiyun unsigned long flags;
146*4882a593Smuzhiyun
147*4882a593Smuzhiyun spin_lock_irqsave(&ehci->lock, flags);
148*4882a593Smuzhiyun qh->clearing_tt = 0;
149*4882a593Smuzhiyun if (qh->qh_state == QH_STATE_IDLE && !list_empty(&qh->qtd_list)
150*4882a593Smuzhiyun && ehci->rh_state == EHCI_RH_RUNNING)
151*4882a593Smuzhiyun qh_link_async(ehci, qh);
152*4882a593Smuzhiyun spin_unlock_irqrestore(&ehci->lock, flags);
153*4882a593Smuzhiyun }
154*4882a593Smuzhiyun
ehci_clear_tt_buffer(struct ehci_hcd * ehci,struct ehci_qh * qh,struct urb * urb,u32 token)155*4882a593Smuzhiyun static void ehci_clear_tt_buffer(struct ehci_hcd *ehci, struct ehci_qh *qh,
156*4882a593Smuzhiyun struct urb *urb, u32 token)
157*4882a593Smuzhiyun {
158*4882a593Smuzhiyun
159*4882a593Smuzhiyun /* If an async split transaction gets an error or is unlinked,
160*4882a593Smuzhiyun * the TT buffer may be left in an indeterminate state. We
161*4882a593Smuzhiyun * have to clear the TT buffer.
162*4882a593Smuzhiyun *
163*4882a593Smuzhiyun * Note: this routine is never called for Isochronous transfers.
164*4882a593Smuzhiyun */
165*4882a593Smuzhiyun if (urb->dev->tt && !usb_pipeint(urb->pipe) && !qh->clearing_tt) {
166*4882a593Smuzhiyun #ifdef CONFIG_DYNAMIC_DEBUG
167*4882a593Smuzhiyun struct usb_device *tt = urb->dev->tt->hub;
168*4882a593Smuzhiyun dev_dbg(&tt->dev,
169*4882a593Smuzhiyun "clear tt buffer port %d, a%d ep%d t%08x\n",
170*4882a593Smuzhiyun urb->dev->ttport, urb->dev->devnum,
171*4882a593Smuzhiyun usb_pipeendpoint(urb->pipe), token);
172*4882a593Smuzhiyun #endif /* CONFIG_DYNAMIC_DEBUG */
173*4882a593Smuzhiyun if (!ehci_is_TDI(ehci)
174*4882a593Smuzhiyun || urb->dev->tt->hub !=
175*4882a593Smuzhiyun ehci_to_hcd(ehci)->self.root_hub) {
176*4882a593Smuzhiyun if (usb_hub_clear_tt_buffer(urb) == 0)
177*4882a593Smuzhiyun qh->clearing_tt = 1;
178*4882a593Smuzhiyun } else {
179*4882a593Smuzhiyun
180*4882a593Smuzhiyun /* REVISIT ARC-derived cores don't clear the root
181*4882a593Smuzhiyun * hub TT buffer in this way...
182*4882a593Smuzhiyun */
183*4882a593Smuzhiyun }
184*4882a593Smuzhiyun }
185*4882a593Smuzhiyun }
186*4882a593Smuzhiyun
qtd_copy_status(struct ehci_hcd * ehci,struct urb * urb,size_t length,u32 token)187*4882a593Smuzhiyun static int qtd_copy_status (
188*4882a593Smuzhiyun struct ehci_hcd *ehci,
189*4882a593Smuzhiyun struct urb *urb,
190*4882a593Smuzhiyun size_t length,
191*4882a593Smuzhiyun u32 token
192*4882a593Smuzhiyun )
193*4882a593Smuzhiyun {
194*4882a593Smuzhiyun int status = -EINPROGRESS;
195*4882a593Smuzhiyun
196*4882a593Smuzhiyun /* count IN/OUT bytes, not SETUP (even short packets) */
197*4882a593Smuzhiyun if (likely(QTD_PID(token) != PID_CODE_SETUP))
198*4882a593Smuzhiyun urb->actual_length += length - QTD_LENGTH (token);
199*4882a593Smuzhiyun
200*4882a593Smuzhiyun /* don't modify error codes */
201*4882a593Smuzhiyun if (unlikely(urb->unlinked))
202*4882a593Smuzhiyun return status;
203*4882a593Smuzhiyun
204*4882a593Smuzhiyun /* force cleanup after short read; not always an error */
205*4882a593Smuzhiyun if (unlikely (IS_SHORT_READ (token)))
206*4882a593Smuzhiyun status = -EREMOTEIO;
207*4882a593Smuzhiyun
208*4882a593Smuzhiyun /* serious "can't proceed" faults reported by the hardware */
209*4882a593Smuzhiyun if (token & QTD_STS_HALT) {
210*4882a593Smuzhiyun if (token & QTD_STS_BABBLE) {
211*4882a593Smuzhiyun /* FIXME "must" disable babbling device's port too */
212*4882a593Smuzhiyun status = -EOVERFLOW;
213*4882a593Smuzhiyun /*
214*4882a593Smuzhiyun * When MMF is active and PID Code is IN, queue is halted.
215*4882a593Smuzhiyun * EHCI Specification, Table 4-13.
216*4882a593Smuzhiyun */
217*4882a593Smuzhiyun } else if ((token & QTD_STS_MMF) &&
218*4882a593Smuzhiyun (QTD_PID(token) == PID_CODE_IN)) {
219*4882a593Smuzhiyun status = -EPROTO;
220*4882a593Smuzhiyun /* CERR nonzero + halt --> stall */
221*4882a593Smuzhiyun } else if (QTD_CERR(token)) {
222*4882a593Smuzhiyun status = -EPIPE;
223*4882a593Smuzhiyun
224*4882a593Smuzhiyun /* In theory, more than one of the following bits can be set
225*4882a593Smuzhiyun * since they are sticky and the transaction is retried.
226*4882a593Smuzhiyun * Which to test first is rather arbitrary.
227*4882a593Smuzhiyun */
228*4882a593Smuzhiyun } else if (token & QTD_STS_MMF) {
229*4882a593Smuzhiyun /* fs/ls interrupt xfer missed the complete-split */
230*4882a593Smuzhiyun status = -EPROTO;
231*4882a593Smuzhiyun } else if (token & QTD_STS_DBE) {
232*4882a593Smuzhiyun status = (QTD_PID (token) == 1) /* IN ? */
233*4882a593Smuzhiyun ? -ENOSR /* hc couldn't read data */
234*4882a593Smuzhiyun : -ECOMM; /* hc couldn't write data */
235*4882a593Smuzhiyun } else if (token & QTD_STS_XACT) {
236*4882a593Smuzhiyun /* timeout, bad CRC, wrong PID, etc */
237*4882a593Smuzhiyun ehci_dbg(ehci, "devpath %s ep%d%s 3strikes\n",
238*4882a593Smuzhiyun urb->dev->devpath,
239*4882a593Smuzhiyun usb_pipeendpoint(urb->pipe),
240*4882a593Smuzhiyun usb_pipein(urb->pipe) ? "in" : "out");
241*4882a593Smuzhiyun status = -EPROTO;
242*4882a593Smuzhiyun } else { /* unknown */
243*4882a593Smuzhiyun status = -EPROTO;
244*4882a593Smuzhiyun }
245*4882a593Smuzhiyun }
246*4882a593Smuzhiyun
247*4882a593Smuzhiyun return status;
248*4882a593Smuzhiyun }
249*4882a593Smuzhiyun
250*4882a593Smuzhiyun static void
ehci_urb_done(struct ehci_hcd * ehci,struct urb * urb,int status)251*4882a593Smuzhiyun ehci_urb_done(struct ehci_hcd *ehci, struct urb *urb, int status)
252*4882a593Smuzhiyun {
253*4882a593Smuzhiyun if (usb_pipetype(urb->pipe) == PIPE_INTERRUPT) {
254*4882a593Smuzhiyun /* ... update hc-wide periodic stats */
255*4882a593Smuzhiyun ehci_to_hcd(ehci)->self.bandwidth_int_reqs--;
256*4882a593Smuzhiyun }
257*4882a593Smuzhiyun
258*4882a593Smuzhiyun if (unlikely(urb->unlinked)) {
259*4882a593Smuzhiyun INCR(ehci->stats.unlink);
260*4882a593Smuzhiyun } else {
261*4882a593Smuzhiyun /* report non-error and short read status as zero */
262*4882a593Smuzhiyun if (status == -EINPROGRESS || status == -EREMOTEIO)
263*4882a593Smuzhiyun status = 0;
264*4882a593Smuzhiyun INCR(ehci->stats.complete);
265*4882a593Smuzhiyun }
266*4882a593Smuzhiyun
267*4882a593Smuzhiyun #ifdef EHCI_URB_TRACE
268*4882a593Smuzhiyun ehci_dbg (ehci,
269*4882a593Smuzhiyun "%s %s urb %p ep%d%s status %d len %d/%d\n",
270*4882a593Smuzhiyun __func__, urb->dev->devpath, urb,
271*4882a593Smuzhiyun usb_pipeendpoint (urb->pipe),
272*4882a593Smuzhiyun usb_pipein (urb->pipe) ? "in" : "out",
273*4882a593Smuzhiyun status,
274*4882a593Smuzhiyun urb->actual_length, urb->transfer_buffer_length);
275*4882a593Smuzhiyun #endif
276*4882a593Smuzhiyun
277*4882a593Smuzhiyun usb_hcd_unlink_urb_from_ep(ehci_to_hcd(ehci), urb);
278*4882a593Smuzhiyun usb_hcd_giveback_urb(ehci_to_hcd(ehci), urb, status);
279*4882a593Smuzhiyun }
280*4882a593Smuzhiyun
281*4882a593Smuzhiyun static int qh_schedule (struct ehci_hcd *ehci, struct ehci_qh *qh);
282*4882a593Smuzhiyun
283*4882a593Smuzhiyun /*
284*4882a593Smuzhiyun * Process and free completed qtds for a qh, returning URBs to drivers.
285*4882a593Smuzhiyun * Chases up to qh->hw_current. Returns nonzero if the caller should
286*4882a593Smuzhiyun * unlink qh.
287*4882a593Smuzhiyun */
288*4882a593Smuzhiyun static unsigned
qh_completions(struct ehci_hcd * ehci,struct ehci_qh * qh)289*4882a593Smuzhiyun qh_completions (struct ehci_hcd *ehci, struct ehci_qh *qh)
290*4882a593Smuzhiyun {
291*4882a593Smuzhiyun struct ehci_qtd *last, *end = qh->dummy;
292*4882a593Smuzhiyun struct list_head *entry, *tmp;
293*4882a593Smuzhiyun int last_status;
294*4882a593Smuzhiyun int stopped;
295*4882a593Smuzhiyun u8 state;
296*4882a593Smuzhiyun struct ehci_qh_hw *hw = qh->hw;
297*4882a593Smuzhiyun
298*4882a593Smuzhiyun /* completions (or tasks on other cpus) must never clobber HALT
299*4882a593Smuzhiyun * till we've gone through and cleaned everything up, even when
300*4882a593Smuzhiyun * they add urbs to this qh's queue or mark them for unlinking.
301*4882a593Smuzhiyun *
302*4882a593Smuzhiyun * NOTE: unlinking expects to be done in queue order.
303*4882a593Smuzhiyun *
304*4882a593Smuzhiyun * It's a bug for qh->qh_state to be anything other than
305*4882a593Smuzhiyun * QH_STATE_IDLE, unless our caller is scan_async() or
306*4882a593Smuzhiyun * scan_intr().
307*4882a593Smuzhiyun */
308*4882a593Smuzhiyun state = qh->qh_state;
309*4882a593Smuzhiyun qh->qh_state = QH_STATE_COMPLETING;
310*4882a593Smuzhiyun stopped = (state == QH_STATE_IDLE);
311*4882a593Smuzhiyun
312*4882a593Smuzhiyun rescan:
313*4882a593Smuzhiyun last = NULL;
314*4882a593Smuzhiyun last_status = -EINPROGRESS;
315*4882a593Smuzhiyun qh->dequeue_during_giveback = 0;
316*4882a593Smuzhiyun
317*4882a593Smuzhiyun /* remove de-activated QTDs from front of queue.
318*4882a593Smuzhiyun * after faults (including short reads), cleanup this urb
319*4882a593Smuzhiyun * then let the queue advance.
320*4882a593Smuzhiyun * if queue is stopped, handles unlinks.
321*4882a593Smuzhiyun */
322*4882a593Smuzhiyun list_for_each_safe (entry, tmp, &qh->qtd_list) {
323*4882a593Smuzhiyun struct ehci_qtd *qtd;
324*4882a593Smuzhiyun struct urb *urb;
325*4882a593Smuzhiyun u32 token = 0;
326*4882a593Smuzhiyun
327*4882a593Smuzhiyun qtd = list_entry (entry, struct ehci_qtd, qtd_list);
328*4882a593Smuzhiyun urb = qtd->urb;
329*4882a593Smuzhiyun
330*4882a593Smuzhiyun /* clean up any state from previous QTD ...*/
331*4882a593Smuzhiyun if (last) {
332*4882a593Smuzhiyun if (likely (last->urb != urb)) {
333*4882a593Smuzhiyun ehci_urb_done(ehci, last->urb, last_status);
334*4882a593Smuzhiyun last_status = -EINPROGRESS;
335*4882a593Smuzhiyun }
336*4882a593Smuzhiyun ehci_qtd_free (ehci, last);
337*4882a593Smuzhiyun last = NULL;
338*4882a593Smuzhiyun }
339*4882a593Smuzhiyun
340*4882a593Smuzhiyun /* ignore urbs submitted during completions we reported */
341*4882a593Smuzhiyun if (qtd == end)
342*4882a593Smuzhiyun break;
343*4882a593Smuzhiyun
344*4882a593Smuzhiyun /* hardware copies qtd out of qh overlay */
345*4882a593Smuzhiyun rmb ();
346*4882a593Smuzhiyun token = hc32_to_cpu(ehci, qtd->hw_token);
347*4882a593Smuzhiyun
348*4882a593Smuzhiyun /* always clean up qtds the hc de-activated */
349*4882a593Smuzhiyun retry_xacterr:
350*4882a593Smuzhiyun if ((token & QTD_STS_ACTIVE) == 0) {
351*4882a593Smuzhiyun
352*4882a593Smuzhiyun /* Report Data Buffer Error: non-fatal but useful */
353*4882a593Smuzhiyun if (token & QTD_STS_DBE)
354*4882a593Smuzhiyun ehci_dbg(ehci,
355*4882a593Smuzhiyun "detected DataBufferErr for urb %p ep%d%s len %d, qtd %p [qh %p]\n",
356*4882a593Smuzhiyun urb,
357*4882a593Smuzhiyun usb_endpoint_num(&urb->ep->desc),
358*4882a593Smuzhiyun usb_endpoint_dir_in(&urb->ep->desc) ? "in" : "out",
359*4882a593Smuzhiyun urb->transfer_buffer_length,
360*4882a593Smuzhiyun qtd,
361*4882a593Smuzhiyun qh);
362*4882a593Smuzhiyun
363*4882a593Smuzhiyun /* on STALL, error, and short reads this urb must
364*4882a593Smuzhiyun * complete and all its qtds must be recycled.
365*4882a593Smuzhiyun */
366*4882a593Smuzhiyun if ((token & QTD_STS_HALT) != 0) {
367*4882a593Smuzhiyun
368*4882a593Smuzhiyun /* retry transaction errors until we
369*4882a593Smuzhiyun * reach the software xacterr limit
370*4882a593Smuzhiyun */
371*4882a593Smuzhiyun if ((token & QTD_STS_XACT) &&
372*4882a593Smuzhiyun QTD_CERR(token) == 0 &&
373*4882a593Smuzhiyun ++qh->xacterrs < QH_XACTERR_MAX &&
374*4882a593Smuzhiyun !urb->unlinked) {
375*4882a593Smuzhiyun ehci_dbg(ehci,
376*4882a593Smuzhiyun "detected XactErr len %zu/%zu retry %d\n",
377*4882a593Smuzhiyun qtd->length - QTD_LENGTH(token), qtd->length, qh->xacterrs);
378*4882a593Smuzhiyun
379*4882a593Smuzhiyun /* reset the token in the qtd and the
380*4882a593Smuzhiyun * qh overlay (which still contains
381*4882a593Smuzhiyun * the qtd) so that we pick up from
382*4882a593Smuzhiyun * where we left off
383*4882a593Smuzhiyun */
384*4882a593Smuzhiyun token &= ~QTD_STS_HALT;
385*4882a593Smuzhiyun token |= QTD_STS_ACTIVE |
386*4882a593Smuzhiyun (EHCI_TUNE_CERR << 10);
387*4882a593Smuzhiyun qtd->hw_token = cpu_to_hc32(ehci,
388*4882a593Smuzhiyun token);
389*4882a593Smuzhiyun wmb();
390*4882a593Smuzhiyun hw->hw_token = cpu_to_hc32(ehci,
391*4882a593Smuzhiyun token);
392*4882a593Smuzhiyun goto retry_xacterr;
393*4882a593Smuzhiyun }
394*4882a593Smuzhiyun stopped = 1;
395*4882a593Smuzhiyun qh->unlink_reason |= QH_UNLINK_HALTED;
396*4882a593Smuzhiyun
397*4882a593Smuzhiyun /* magic dummy for some short reads; qh won't advance.
398*4882a593Smuzhiyun * that silicon quirk can kick in with this dummy too.
399*4882a593Smuzhiyun *
400*4882a593Smuzhiyun * other short reads won't stop the queue, including
401*4882a593Smuzhiyun * control transfers (status stage handles that) or
402*4882a593Smuzhiyun * most other single-qtd reads ... the queue stops if
403*4882a593Smuzhiyun * URB_SHORT_NOT_OK was set so the driver submitting
404*4882a593Smuzhiyun * the urbs could clean it up.
405*4882a593Smuzhiyun */
406*4882a593Smuzhiyun } else if (IS_SHORT_READ (token)
407*4882a593Smuzhiyun && !(qtd->hw_alt_next
408*4882a593Smuzhiyun & EHCI_LIST_END(ehci))) {
409*4882a593Smuzhiyun stopped = 1;
410*4882a593Smuzhiyun qh->unlink_reason |= QH_UNLINK_SHORT_READ;
411*4882a593Smuzhiyun }
412*4882a593Smuzhiyun
413*4882a593Smuzhiyun /* stop scanning when we reach qtds the hc is using */
414*4882a593Smuzhiyun } else if (likely (!stopped
415*4882a593Smuzhiyun && ehci->rh_state >= EHCI_RH_RUNNING)) {
416*4882a593Smuzhiyun break;
417*4882a593Smuzhiyun
418*4882a593Smuzhiyun /* scan the whole queue for unlinks whenever it stops */
419*4882a593Smuzhiyun } else {
420*4882a593Smuzhiyun stopped = 1;
421*4882a593Smuzhiyun
422*4882a593Smuzhiyun /* cancel everything if we halt, suspend, etc */
423*4882a593Smuzhiyun if (ehci->rh_state < EHCI_RH_RUNNING) {
424*4882a593Smuzhiyun last_status = -ESHUTDOWN;
425*4882a593Smuzhiyun qh->unlink_reason |= QH_UNLINK_SHUTDOWN;
426*4882a593Smuzhiyun }
427*4882a593Smuzhiyun
428*4882a593Smuzhiyun /* this qtd is active; skip it unless a previous qtd
429*4882a593Smuzhiyun * for its urb faulted, or its urb was canceled.
430*4882a593Smuzhiyun */
431*4882a593Smuzhiyun else if (last_status == -EINPROGRESS && !urb->unlinked)
432*4882a593Smuzhiyun continue;
433*4882a593Smuzhiyun
434*4882a593Smuzhiyun /*
435*4882a593Smuzhiyun * If this was the active qtd when the qh was unlinked
436*4882a593Smuzhiyun * and the overlay's token is active, then the overlay
437*4882a593Smuzhiyun * hasn't been written back to the qtd yet so use its
438*4882a593Smuzhiyun * token instead of the qtd's. After the qtd is
439*4882a593Smuzhiyun * processed and removed, the overlay won't be valid
440*4882a593Smuzhiyun * any more.
441*4882a593Smuzhiyun */
442*4882a593Smuzhiyun if (state == QH_STATE_IDLE &&
443*4882a593Smuzhiyun qh->qtd_list.next == &qtd->qtd_list &&
444*4882a593Smuzhiyun (hw->hw_token & ACTIVE_BIT(ehci))) {
445*4882a593Smuzhiyun token = hc32_to_cpu(ehci, hw->hw_token);
446*4882a593Smuzhiyun hw->hw_token &= ~ACTIVE_BIT(ehci);
447*4882a593Smuzhiyun qh->should_be_inactive = 1;
448*4882a593Smuzhiyun
449*4882a593Smuzhiyun /* An unlink may leave an incomplete
450*4882a593Smuzhiyun * async transaction in the TT buffer.
451*4882a593Smuzhiyun * We have to clear it.
452*4882a593Smuzhiyun */
453*4882a593Smuzhiyun ehci_clear_tt_buffer(ehci, qh, urb, token);
454*4882a593Smuzhiyun }
455*4882a593Smuzhiyun }
456*4882a593Smuzhiyun
457*4882a593Smuzhiyun /* unless we already know the urb's status, collect qtd status
458*4882a593Smuzhiyun * and update count of bytes transferred. in common short read
459*4882a593Smuzhiyun * cases with only one data qtd (including control transfers),
460*4882a593Smuzhiyun * queue processing won't halt. but with two or more qtds (for
461*4882a593Smuzhiyun * example, with a 32 KB transfer), when the first qtd gets a
462*4882a593Smuzhiyun * short read the second must be removed by hand.
463*4882a593Smuzhiyun */
464*4882a593Smuzhiyun if (last_status == -EINPROGRESS) {
465*4882a593Smuzhiyun last_status = qtd_copy_status(ehci, urb,
466*4882a593Smuzhiyun qtd->length, token);
467*4882a593Smuzhiyun if (last_status == -EREMOTEIO
468*4882a593Smuzhiyun && (qtd->hw_alt_next
469*4882a593Smuzhiyun & EHCI_LIST_END(ehci)))
470*4882a593Smuzhiyun last_status = -EINPROGRESS;
471*4882a593Smuzhiyun
472*4882a593Smuzhiyun /* As part of low/full-speed endpoint-halt processing
473*4882a593Smuzhiyun * we must clear the TT buffer (11.17.5).
474*4882a593Smuzhiyun */
475*4882a593Smuzhiyun if (unlikely(last_status != -EINPROGRESS &&
476*4882a593Smuzhiyun last_status != -EREMOTEIO)) {
477*4882a593Smuzhiyun /* The TT's in some hubs malfunction when they
478*4882a593Smuzhiyun * receive this request following a STALL (they
479*4882a593Smuzhiyun * stop sending isochronous packets). Since a
480*4882a593Smuzhiyun * STALL can't leave the TT buffer in a busy
481*4882a593Smuzhiyun * state (if you believe Figures 11-48 - 11-51
482*4882a593Smuzhiyun * in the USB 2.0 spec), we won't clear the TT
483*4882a593Smuzhiyun * buffer in this case. Strictly speaking this
484*4882a593Smuzhiyun * is a violation of the spec.
485*4882a593Smuzhiyun */
486*4882a593Smuzhiyun if (last_status != -EPIPE)
487*4882a593Smuzhiyun ehci_clear_tt_buffer(ehci, qh, urb,
488*4882a593Smuzhiyun token);
489*4882a593Smuzhiyun }
490*4882a593Smuzhiyun }
491*4882a593Smuzhiyun
492*4882a593Smuzhiyun /* if we're removing something not at the queue head,
493*4882a593Smuzhiyun * patch the hardware queue pointer.
494*4882a593Smuzhiyun */
495*4882a593Smuzhiyun if (stopped && qtd->qtd_list.prev != &qh->qtd_list) {
496*4882a593Smuzhiyun last = list_entry (qtd->qtd_list.prev,
497*4882a593Smuzhiyun struct ehci_qtd, qtd_list);
498*4882a593Smuzhiyun last->hw_next = qtd->hw_next;
499*4882a593Smuzhiyun }
500*4882a593Smuzhiyun
501*4882a593Smuzhiyun /* remove qtd; it's recycled after possible urb completion */
502*4882a593Smuzhiyun list_del (&qtd->qtd_list);
503*4882a593Smuzhiyun last = qtd;
504*4882a593Smuzhiyun
505*4882a593Smuzhiyun /* reinit the xacterr counter for the next qtd */
506*4882a593Smuzhiyun qh->xacterrs = 0;
507*4882a593Smuzhiyun }
508*4882a593Smuzhiyun
509*4882a593Smuzhiyun /* last urb's completion might still need calling */
510*4882a593Smuzhiyun if (likely (last != NULL)) {
511*4882a593Smuzhiyun ehci_urb_done(ehci, last->urb, last_status);
512*4882a593Smuzhiyun ehci_qtd_free (ehci, last);
513*4882a593Smuzhiyun }
514*4882a593Smuzhiyun
515*4882a593Smuzhiyun /* Do we need to rescan for URBs dequeued during a giveback? */
516*4882a593Smuzhiyun if (unlikely(qh->dequeue_during_giveback)) {
517*4882a593Smuzhiyun /* If the QH is already unlinked, do the rescan now. */
518*4882a593Smuzhiyun if (state == QH_STATE_IDLE)
519*4882a593Smuzhiyun goto rescan;
520*4882a593Smuzhiyun
521*4882a593Smuzhiyun /* Otherwise the caller must unlink the QH. */
522*4882a593Smuzhiyun }
523*4882a593Smuzhiyun
524*4882a593Smuzhiyun /* restore original state; caller must unlink or relink */
525*4882a593Smuzhiyun qh->qh_state = state;
526*4882a593Smuzhiyun
527*4882a593Smuzhiyun /* be sure the hardware's done with the qh before refreshing
528*4882a593Smuzhiyun * it after fault cleanup, or recovering from silicon wrongly
529*4882a593Smuzhiyun * overlaying the dummy qtd (which reduces DMA chatter).
530*4882a593Smuzhiyun *
531*4882a593Smuzhiyun * We won't refresh a QH that's linked (after the HC
532*4882a593Smuzhiyun * stopped the queue). That avoids a race:
533*4882a593Smuzhiyun * - HC reads first part of QH;
534*4882a593Smuzhiyun * - CPU updates that first part and the token;
535*4882a593Smuzhiyun * - HC reads rest of that QH, including token
536*4882a593Smuzhiyun * Result: HC gets an inconsistent image, and then
537*4882a593Smuzhiyun * DMAs to/from the wrong memory (corrupting it).
538*4882a593Smuzhiyun *
539*4882a593Smuzhiyun * That should be rare for interrupt transfers,
540*4882a593Smuzhiyun * except maybe high bandwidth ...
541*4882a593Smuzhiyun */
542*4882a593Smuzhiyun if (stopped != 0 || hw->hw_qtd_next == EHCI_LIST_END(ehci))
543*4882a593Smuzhiyun qh->unlink_reason |= QH_UNLINK_DUMMY_OVERLAY;
544*4882a593Smuzhiyun
545*4882a593Smuzhiyun /* Let the caller know if the QH needs to be unlinked. */
546*4882a593Smuzhiyun return qh->unlink_reason;
547*4882a593Smuzhiyun }
548*4882a593Smuzhiyun
549*4882a593Smuzhiyun /*-------------------------------------------------------------------------*/
550*4882a593Smuzhiyun
551*4882a593Smuzhiyun /*
552*4882a593Smuzhiyun * reverse of qh_urb_transaction: free a list of TDs.
553*4882a593Smuzhiyun * used for cleanup after errors, before HC sees an URB's TDs.
554*4882a593Smuzhiyun */
qtd_list_free(struct ehci_hcd * ehci,struct urb * urb,struct list_head * qtd_list)555*4882a593Smuzhiyun static void qtd_list_free (
556*4882a593Smuzhiyun struct ehci_hcd *ehci,
557*4882a593Smuzhiyun struct urb *urb,
558*4882a593Smuzhiyun struct list_head *qtd_list
559*4882a593Smuzhiyun ) {
560*4882a593Smuzhiyun struct list_head *entry, *temp;
561*4882a593Smuzhiyun
562*4882a593Smuzhiyun list_for_each_safe (entry, temp, qtd_list) {
563*4882a593Smuzhiyun struct ehci_qtd *qtd;
564*4882a593Smuzhiyun
565*4882a593Smuzhiyun qtd = list_entry (entry, struct ehci_qtd, qtd_list);
566*4882a593Smuzhiyun list_del (&qtd->qtd_list);
567*4882a593Smuzhiyun ehci_qtd_free (ehci, qtd);
568*4882a593Smuzhiyun }
569*4882a593Smuzhiyun }
570*4882a593Smuzhiyun
571*4882a593Smuzhiyun /*
572*4882a593Smuzhiyun * create a list of filled qtds for this URB; won't link into qh.
573*4882a593Smuzhiyun */
574*4882a593Smuzhiyun static struct list_head *
qh_urb_transaction(struct ehci_hcd * ehci,struct urb * urb,struct list_head * head,gfp_t flags)575*4882a593Smuzhiyun qh_urb_transaction (
576*4882a593Smuzhiyun struct ehci_hcd *ehci,
577*4882a593Smuzhiyun struct urb *urb,
578*4882a593Smuzhiyun struct list_head *head,
579*4882a593Smuzhiyun gfp_t flags
580*4882a593Smuzhiyun ) {
581*4882a593Smuzhiyun struct ehci_qtd *qtd, *qtd_prev;
582*4882a593Smuzhiyun dma_addr_t buf;
583*4882a593Smuzhiyun int len, this_sg_len, maxpacket;
584*4882a593Smuzhiyun int is_input;
585*4882a593Smuzhiyun u32 token;
586*4882a593Smuzhiyun int i;
587*4882a593Smuzhiyun struct scatterlist *sg;
588*4882a593Smuzhiyun
589*4882a593Smuzhiyun /*
590*4882a593Smuzhiyun * URBs map to sequences of QTDs: one logical transaction
591*4882a593Smuzhiyun */
592*4882a593Smuzhiyun qtd = ehci_qtd_alloc (ehci, flags);
593*4882a593Smuzhiyun if (unlikely (!qtd))
594*4882a593Smuzhiyun return NULL;
595*4882a593Smuzhiyun list_add_tail (&qtd->qtd_list, head);
596*4882a593Smuzhiyun qtd->urb = urb;
597*4882a593Smuzhiyun
598*4882a593Smuzhiyun token = QTD_STS_ACTIVE;
599*4882a593Smuzhiyun token |= (EHCI_TUNE_CERR << 10);
600*4882a593Smuzhiyun /* for split transactions, SplitXState initialized to zero */
601*4882a593Smuzhiyun
602*4882a593Smuzhiyun len = urb->transfer_buffer_length;
603*4882a593Smuzhiyun is_input = usb_pipein (urb->pipe);
604*4882a593Smuzhiyun if (usb_pipecontrol (urb->pipe)) {
605*4882a593Smuzhiyun /* SETUP pid */
606*4882a593Smuzhiyun qtd_fill(ehci, qtd, urb->setup_dma,
607*4882a593Smuzhiyun sizeof (struct usb_ctrlrequest),
608*4882a593Smuzhiyun token | (2 /* "setup" */ << 8), 8);
609*4882a593Smuzhiyun
610*4882a593Smuzhiyun /* ... and always at least one more pid */
611*4882a593Smuzhiyun token ^= QTD_TOGGLE;
612*4882a593Smuzhiyun qtd_prev = qtd;
613*4882a593Smuzhiyun qtd = ehci_qtd_alloc (ehci, flags);
614*4882a593Smuzhiyun if (unlikely (!qtd))
615*4882a593Smuzhiyun goto cleanup;
616*4882a593Smuzhiyun qtd->urb = urb;
617*4882a593Smuzhiyun qtd_prev->hw_next = QTD_NEXT(ehci, qtd->qtd_dma);
618*4882a593Smuzhiyun list_add_tail (&qtd->qtd_list, head);
619*4882a593Smuzhiyun
620*4882a593Smuzhiyun /* for zero length DATA stages, STATUS is always IN */
621*4882a593Smuzhiyun if (len == 0)
622*4882a593Smuzhiyun token |= (1 /* "in" */ << 8);
623*4882a593Smuzhiyun }
624*4882a593Smuzhiyun
625*4882a593Smuzhiyun /*
626*4882a593Smuzhiyun * data transfer stage: buffer setup
627*4882a593Smuzhiyun */
628*4882a593Smuzhiyun i = urb->num_mapped_sgs;
629*4882a593Smuzhiyun if (len > 0 && i > 0) {
630*4882a593Smuzhiyun sg = urb->sg;
631*4882a593Smuzhiyun buf = sg_dma_address(sg);
632*4882a593Smuzhiyun
633*4882a593Smuzhiyun /* urb->transfer_buffer_length may be smaller than the
634*4882a593Smuzhiyun * size of the scatterlist (or vice versa)
635*4882a593Smuzhiyun */
636*4882a593Smuzhiyun this_sg_len = min_t(int, sg_dma_len(sg), len);
637*4882a593Smuzhiyun } else {
638*4882a593Smuzhiyun sg = NULL;
639*4882a593Smuzhiyun buf = urb->transfer_dma;
640*4882a593Smuzhiyun this_sg_len = len;
641*4882a593Smuzhiyun }
642*4882a593Smuzhiyun
643*4882a593Smuzhiyun if (is_input)
644*4882a593Smuzhiyun token |= (1 /* "in" */ << 8);
645*4882a593Smuzhiyun /* else it's already initted to "out" pid (0 << 8) */
646*4882a593Smuzhiyun
647*4882a593Smuzhiyun maxpacket = usb_maxpacket(urb->dev, urb->pipe, !is_input);
648*4882a593Smuzhiyun
649*4882a593Smuzhiyun /*
650*4882a593Smuzhiyun * buffer gets wrapped in one or more qtds;
651*4882a593Smuzhiyun * last one may be "short" (including zero len)
652*4882a593Smuzhiyun * and may serve as a control status ack
653*4882a593Smuzhiyun */
654*4882a593Smuzhiyun for (;;) {
655*4882a593Smuzhiyun int this_qtd_len;
656*4882a593Smuzhiyun
657*4882a593Smuzhiyun this_qtd_len = qtd_fill(ehci, qtd, buf, this_sg_len, token,
658*4882a593Smuzhiyun maxpacket);
659*4882a593Smuzhiyun this_sg_len -= this_qtd_len;
660*4882a593Smuzhiyun len -= this_qtd_len;
661*4882a593Smuzhiyun buf += this_qtd_len;
662*4882a593Smuzhiyun
663*4882a593Smuzhiyun /*
664*4882a593Smuzhiyun * short reads advance to a "magic" dummy instead of the next
665*4882a593Smuzhiyun * qtd ... that forces the queue to stop, for manual cleanup.
666*4882a593Smuzhiyun * (this will usually be overridden later.)
667*4882a593Smuzhiyun */
668*4882a593Smuzhiyun if (is_input)
669*4882a593Smuzhiyun qtd->hw_alt_next = ehci->async->hw->hw_alt_next;
670*4882a593Smuzhiyun
671*4882a593Smuzhiyun /* qh makes control packets use qtd toggle; maybe switch it */
672*4882a593Smuzhiyun if ((maxpacket & (this_qtd_len + (maxpacket - 1))) == 0)
673*4882a593Smuzhiyun token ^= QTD_TOGGLE;
674*4882a593Smuzhiyun
675*4882a593Smuzhiyun if (likely(this_sg_len <= 0)) {
676*4882a593Smuzhiyun if (--i <= 0 || len <= 0)
677*4882a593Smuzhiyun break;
678*4882a593Smuzhiyun sg = sg_next(sg);
679*4882a593Smuzhiyun buf = sg_dma_address(sg);
680*4882a593Smuzhiyun this_sg_len = min_t(int, sg_dma_len(sg), len);
681*4882a593Smuzhiyun }
682*4882a593Smuzhiyun
683*4882a593Smuzhiyun qtd_prev = qtd;
684*4882a593Smuzhiyun qtd = ehci_qtd_alloc (ehci, flags);
685*4882a593Smuzhiyun if (unlikely (!qtd))
686*4882a593Smuzhiyun goto cleanup;
687*4882a593Smuzhiyun qtd->urb = urb;
688*4882a593Smuzhiyun qtd_prev->hw_next = QTD_NEXT(ehci, qtd->qtd_dma);
689*4882a593Smuzhiyun list_add_tail (&qtd->qtd_list, head);
690*4882a593Smuzhiyun }
691*4882a593Smuzhiyun
692*4882a593Smuzhiyun /*
693*4882a593Smuzhiyun * unless the caller requires manual cleanup after short reads,
694*4882a593Smuzhiyun * have the alt_next mechanism keep the queue running after the
695*4882a593Smuzhiyun * last data qtd (the only one, for control and most other cases).
696*4882a593Smuzhiyun */
697*4882a593Smuzhiyun if (likely ((urb->transfer_flags & URB_SHORT_NOT_OK) == 0
698*4882a593Smuzhiyun || usb_pipecontrol (urb->pipe)))
699*4882a593Smuzhiyun qtd->hw_alt_next = EHCI_LIST_END(ehci);
700*4882a593Smuzhiyun
701*4882a593Smuzhiyun /*
702*4882a593Smuzhiyun * control requests may need a terminating data "status" ack;
703*4882a593Smuzhiyun * other OUT ones may need a terminating short packet
704*4882a593Smuzhiyun * (zero length).
705*4882a593Smuzhiyun */
706*4882a593Smuzhiyun if (likely (urb->transfer_buffer_length != 0)) {
707*4882a593Smuzhiyun int one_more = 0;
708*4882a593Smuzhiyun
709*4882a593Smuzhiyun if (usb_pipecontrol (urb->pipe)) {
710*4882a593Smuzhiyun one_more = 1;
711*4882a593Smuzhiyun token ^= 0x0100; /* "in" <--> "out" */
712*4882a593Smuzhiyun token |= QTD_TOGGLE; /* force DATA1 */
713*4882a593Smuzhiyun } else if (usb_pipeout(urb->pipe)
714*4882a593Smuzhiyun && (urb->transfer_flags & URB_ZERO_PACKET)
715*4882a593Smuzhiyun && !(urb->transfer_buffer_length % maxpacket)) {
716*4882a593Smuzhiyun one_more = 1;
717*4882a593Smuzhiyun }
718*4882a593Smuzhiyun if (one_more) {
719*4882a593Smuzhiyun qtd_prev = qtd;
720*4882a593Smuzhiyun qtd = ehci_qtd_alloc (ehci, flags);
721*4882a593Smuzhiyun if (unlikely (!qtd))
722*4882a593Smuzhiyun goto cleanup;
723*4882a593Smuzhiyun qtd->urb = urb;
724*4882a593Smuzhiyun qtd_prev->hw_next = QTD_NEXT(ehci, qtd->qtd_dma);
725*4882a593Smuzhiyun list_add_tail (&qtd->qtd_list, head);
726*4882a593Smuzhiyun
727*4882a593Smuzhiyun /* never any data in such packets */
728*4882a593Smuzhiyun qtd_fill(ehci, qtd, 0, 0, token, 0);
729*4882a593Smuzhiyun }
730*4882a593Smuzhiyun }
731*4882a593Smuzhiyun
732*4882a593Smuzhiyun /* by default, enable interrupt on urb completion */
733*4882a593Smuzhiyun if (likely (!(urb->transfer_flags & URB_NO_INTERRUPT)))
734*4882a593Smuzhiyun qtd->hw_token |= cpu_to_hc32(ehci, QTD_IOC);
735*4882a593Smuzhiyun return head;
736*4882a593Smuzhiyun
737*4882a593Smuzhiyun cleanup:
738*4882a593Smuzhiyun qtd_list_free (ehci, urb, head);
739*4882a593Smuzhiyun return NULL;
740*4882a593Smuzhiyun }
741*4882a593Smuzhiyun
742*4882a593Smuzhiyun /*-------------------------------------------------------------------------*/
743*4882a593Smuzhiyun
744*4882a593Smuzhiyun // Would be best to create all qh's from config descriptors,
745*4882a593Smuzhiyun // when each interface/altsetting is established. Unlink
746*4882a593Smuzhiyun // any previous qh and cancel its urbs first; endpoints are
747*4882a593Smuzhiyun // implicitly reset then (data toggle too).
748*4882a593Smuzhiyun // That'd mean updating how usbcore talks to HCDs. (2.7?)
749*4882a593Smuzhiyun
750*4882a593Smuzhiyun
751*4882a593Smuzhiyun /*
752*4882a593Smuzhiyun * Each QH holds a qtd list; a QH is used for everything except iso.
753*4882a593Smuzhiyun *
754*4882a593Smuzhiyun * For interrupt urbs, the scheduler must set the microframe scheduling
755*4882a593Smuzhiyun * mask(s) each time the QH gets scheduled. For highspeed, that's
756*4882a593Smuzhiyun * just one microframe in the s-mask. For split interrupt transactions
757*4882a593Smuzhiyun * there are additional complications: c-mask, maybe FSTNs.
758*4882a593Smuzhiyun */
759*4882a593Smuzhiyun static struct ehci_qh *
qh_make(struct ehci_hcd * ehci,struct urb * urb,gfp_t flags)760*4882a593Smuzhiyun qh_make (
761*4882a593Smuzhiyun struct ehci_hcd *ehci,
762*4882a593Smuzhiyun struct urb *urb,
763*4882a593Smuzhiyun gfp_t flags
764*4882a593Smuzhiyun ) {
765*4882a593Smuzhiyun struct ehci_qh *qh = ehci_qh_alloc (ehci, flags);
766*4882a593Smuzhiyun struct usb_host_endpoint *ep;
767*4882a593Smuzhiyun u32 info1 = 0, info2 = 0;
768*4882a593Smuzhiyun int is_input, type;
769*4882a593Smuzhiyun int maxp = 0;
770*4882a593Smuzhiyun int mult;
771*4882a593Smuzhiyun struct usb_tt *tt = urb->dev->tt;
772*4882a593Smuzhiyun struct ehci_qh_hw *hw;
773*4882a593Smuzhiyun
774*4882a593Smuzhiyun if (!qh)
775*4882a593Smuzhiyun return qh;
776*4882a593Smuzhiyun
777*4882a593Smuzhiyun /*
778*4882a593Smuzhiyun * init endpoint/device data for this QH
779*4882a593Smuzhiyun */
780*4882a593Smuzhiyun info1 |= usb_pipeendpoint (urb->pipe) << 8;
781*4882a593Smuzhiyun info1 |= usb_pipedevice (urb->pipe) << 0;
782*4882a593Smuzhiyun
783*4882a593Smuzhiyun is_input = usb_pipein (urb->pipe);
784*4882a593Smuzhiyun type = usb_pipetype (urb->pipe);
785*4882a593Smuzhiyun ep = usb_pipe_endpoint (urb->dev, urb->pipe);
786*4882a593Smuzhiyun maxp = usb_endpoint_maxp (&ep->desc);
787*4882a593Smuzhiyun mult = usb_endpoint_maxp_mult (&ep->desc);
788*4882a593Smuzhiyun
789*4882a593Smuzhiyun /* 1024 byte maxpacket is a hardware ceiling. High bandwidth
790*4882a593Smuzhiyun * acts like up to 3KB, but is built from smaller packets.
791*4882a593Smuzhiyun */
792*4882a593Smuzhiyun if (maxp > 1024) {
793*4882a593Smuzhiyun ehci_dbg(ehci, "bogus qh maxpacket %d\n", maxp);
794*4882a593Smuzhiyun goto done;
795*4882a593Smuzhiyun }
796*4882a593Smuzhiyun
797*4882a593Smuzhiyun /* Compute interrupt scheduling parameters just once, and save.
798*4882a593Smuzhiyun * - allowing for high bandwidth, how many nsec/uframe are used?
799*4882a593Smuzhiyun * - split transactions need a second CSPLIT uframe; same question
800*4882a593Smuzhiyun * - splits also need a schedule gap (for full/low speed I/O)
801*4882a593Smuzhiyun * - qh has a polling interval
802*4882a593Smuzhiyun *
803*4882a593Smuzhiyun * For control/bulk requests, the HC or TT handles these.
804*4882a593Smuzhiyun */
805*4882a593Smuzhiyun if (type == PIPE_INTERRUPT) {
806*4882a593Smuzhiyun unsigned tmp;
807*4882a593Smuzhiyun
808*4882a593Smuzhiyun qh->ps.usecs = NS_TO_US(usb_calc_bus_time(USB_SPEED_HIGH,
809*4882a593Smuzhiyun is_input, 0, mult * maxp));
810*4882a593Smuzhiyun qh->ps.phase = NO_FRAME;
811*4882a593Smuzhiyun
812*4882a593Smuzhiyun if (urb->dev->speed == USB_SPEED_HIGH) {
813*4882a593Smuzhiyun qh->ps.c_usecs = 0;
814*4882a593Smuzhiyun qh->gap_uf = 0;
815*4882a593Smuzhiyun
816*4882a593Smuzhiyun if (urb->interval > 1 && urb->interval < 8) {
817*4882a593Smuzhiyun /* NOTE interval 2 or 4 uframes could work.
818*4882a593Smuzhiyun * But interval 1 scheduling is simpler, and
819*4882a593Smuzhiyun * includes high bandwidth.
820*4882a593Smuzhiyun */
821*4882a593Smuzhiyun urb->interval = 1;
822*4882a593Smuzhiyun } else if (urb->interval > ehci->periodic_size << 3) {
823*4882a593Smuzhiyun urb->interval = ehci->periodic_size << 3;
824*4882a593Smuzhiyun }
825*4882a593Smuzhiyun qh->ps.period = urb->interval >> 3;
826*4882a593Smuzhiyun
827*4882a593Smuzhiyun /* period for bandwidth allocation */
828*4882a593Smuzhiyun tmp = min_t(unsigned, EHCI_BANDWIDTH_SIZE,
829*4882a593Smuzhiyun 1 << (urb->ep->desc.bInterval - 1));
830*4882a593Smuzhiyun
831*4882a593Smuzhiyun /* Allow urb->interval to override */
832*4882a593Smuzhiyun qh->ps.bw_uperiod = min_t(unsigned, tmp, urb->interval);
833*4882a593Smuzhiyun qh->ps.bw_period = qh->ps.bw_uperiod >> 3;
834*4882a593Smuzhiyun } else {
835*4882a593Smuzhiyun int think_time;
836*4882a593Smuzhiyun
837*4882a593Smuzhiyun /* gap is f(FS/LS transfer times) */
838*4882a593Smuzhiyun qh->gap_uf = 1 + usb_calc_bus_time (urb->dev->speed,
839*4882a593Smuzhiyun is_input, 0, maxp) / (125 * 1000);
840*4882a593Smuzhiyun
841*4882a593Smuzhiyun /* FIXME this just approximates SPLIT/CSPLIT times */
842*4882a593Smuzhiyun if (is_input) { // SPLIT, gap, CSPLIT+DATA
843*4882a593Smuzhiyun qh->ps.c_usecs = qh->ps.usecs + HS_USECS(0);
844*4882a593Smuzhiyun qh->ps.usecs = HS_USECS(1);
845*4882a593Smuzhiyun } else { // SPLIT+DATA, gap, CSPLIT
846*4882a593Smuzhiyun qh->ps.usecs += HS_USECS(1);
847*4882a593Smuzhiyun qh->ps.c_usecs = HS_USECS(0);
848*4882a593Smuzhiyun }
849*4882a593Smuzhiyun
850*4882a593Smuzhiyun think_time = tt ? tt->think_time : 0;
851*4882a593Smuzhiyun qh->ps.tt_usecs = NS_TO_US(think_time +
852*4882a593Smuzhiyun usb_calc_bus_time (urb->dev->speed,
853*4882a593Smuzhiyun is_input, 0, maxp));
854*4882a593Smuzhiyun if (urb->interval > ehci->periodic_size)
855*4882a593Smuzhiyun urb->interval = ehci->periodic_size;
856*4882a593Smuzhiyun qh->ps.period = urb->interval;
857*4882a593Smuzhiyun
858*4882a593Smuzhiyun /* period for bandwidth allocation */
859*4882a593Smuzhiyun tmp = min_t(unsigned, EHCI_BANDWIDTH_FRAMES,
860*4882a593Smuzhiyun urb->ep->desc.bInterval);
861*4882a593Smuzhiyun tmp = rounddown_pow_of_two(tmp);
862*4882a593Smuzhiyun
863*4882a593Smuzhiyun /* Allow urb->interval to override */
864*4882a593Smuzhiyun qh->ps.bw_period = min_t(unsigned, tmp, urb->interval);
865*4882a593Smuzhiyun qh->ps.bw_uperiod = qh->ps.bw_period << 3;
866*4882a593Smuzhiyun }
867*4882a593Smuzhiyun }
868*4882a593Smuzhiyun
869*4882a593Smuzhiyun /* support for tt scheduling, and access to toggles */
870*4882a593Smuzhiyun qh->ps.udev = urb->dev;
871*4882a593Smuzhiyun qh->ps.ep = urb->ep;
872*4882a593Smuzhiyun
873*4882a593Smuzhiyun /* using TT? */
874*4882a593Smuzhiyun switch (urb->dev->speed) {
875*4882a593Smuzhiyun case USB_SPEED_LOW:
876*4882a593Smuzhiyun info1 |= QH_LOW_SPEED;
877*4882a593Smuzhiyun fallthrough;
878*4882a593Smuzhiyun
879*4882a593Smuzhiyun case USB_SPEED_FULL:
880*4882a593Smuzhiyun /* EPS 0 means "full" */
881*4882a593Smuzhiyun if (type != PIPE_INTERRUPT)
882*4882a593Smuzhiyun info1 |= (EHCI_TUNE_RL_TT << 28);
883*4882a593Smuzhiyun if (type == PIPE_CONTROL) {
884*4882a593Smuzhiyun info1 |= QH_CONTROL_EP; /* for TT */
885*4882a593Smuzhiyun info1 |= QH_TOGGLE_CTL; /* toggle from qtd */
886*4882a593Smuzhiyun }
887*4882a593Smuzhiyun info1 |= maxp << 16;
888*4882a593Smuzhiyun
889*4882a593Smuzhiyun info2 |= (EHCI_TUNE_MULT_TT << 30);
890*4882a593Smuzhiyun
891*4882a593Smuzhiyun /* Some Freescale processors have an erratum in which the
892*4882a593Smuzhiyun * port number in the queue head was 0..N-1 instead of 1..N.
893*4882a593Smuzhiyun */
894*4882a593Smuzhiyun if (ehci_has_fsl_portno_bug(ehci))
895*4882a593Smuzhiyun info2 |= (urb->dev->ttport-1) << 23;
896*4882a593Smuzhiyun else
897*4882a593Smuzhiyun info2 |= urb->dev->ttport << 23;
898*4882a593Smuzhiyun
899*4882a593Smuzhiyun /* set the address of the TT; for TDI's integrated
900*4882a593Smuzhiyun * root hub tt, leave it zeroed.
901*4882a593Smuzhiyun */
902*4882a593Smuzhiyun if (tt && tt->hub != ehci_to_hcd(ehci)->self.root_hub)
903*4882a593Smuzhiyun info2 |= tt->hub->devnum << 16;
904*4882a593Smuzhiyun
905*4882a593Smuzhiyun /* NOTE: if (PIPE_INTERRUPT) { scheduler sets c-mask } */
906*4882a593Smuzhiyun
907*4882a593Smuzhiyun break;
908*4882a593Smuzhiyun
909*4882a593Smuzhiyun case USB_SPEED_HIGH: /* no TT involved */
910*4882a593Smuzhiyun info1 |= QH_HIGH_SPEED;
911*4882a593Smuzhiyun if (type == PIPE_CONTROL) {
912*4882a593Smuzhiyun info1 |= (EHCI_TUNE_RL_HS << 28);
913*4882a593Smuzhiyun info1 |= 64 << 16; /* usb2 fixed maxpacket */
914*4882a593Smuzhiyun info1 |= QH_TOGGLE_CTL; /* toggle from qtd */
915*4882a593Smuzhiyun info2 |= (EHCI_TUNE_MULT_HS << 30);
916*4882a593Smuzhiyun } else if (type == PIPE_BULK) {
917*4882a593Smuzhiyun info1 |= (EHCI_TUNE_RL_HS << 28);
918*4882a593Smuzhiyun /* The USB spec says that high speed bulk endpoints
919*4882a593Smuzhiyun * always use 512 byte maxpacket. But some device
920*4882a593Smuzhiyun * vendors decided to ignore that, and MSFT is happy
921*4882a593Smuzhiyun * to help them do so. So now people expect to use
922*4882a593Smuzhiyun * such nonconformant devices with Linux too; sigh.
923*4882a593Smuzhiyun */
924*4882a593Smuzhiyun info1 |= maxp << 16;
925*4882a593Smuzhiyun info2 |= (EHCI_TUNE_MULT_HS << 30);
926*4882a593Smuzhiyun } else { /* PIPE_INTERRUPT */
927*4882a593Smuzhiyun info1 |= maxp << 16;
928*4882a593Smuzhiyun info2 |= mult << 30;
929*4882a593Smuzhiyun }
930*4882a593Smuzhiyun break;
931*4882a593Smuzhiyun default:
932*4882a593Smuzhiyun ehci_dbg(ehci, "bogus dev %p speed %d\n", urb->dev,
933*4882a593Smuzhiyun urb->dev->speed);
934*4882a593Smuzhiyun done:
935*4882a593Smuzhiyun qh_destroy(ehci, qh);
936*4882a593Smuzhiyun return NULL;
937*4882a593Smuzhiyun }
938*4882a593Smuzhiyun
939*4882a593Smuzhiyun /* NOTE: if (PIPE_INTERRUPT) { scheduler sets s-mask } */
940*4882a593Smuzhiyun
941*4882a593Smuzhiyun /* init as live, toggle clear */
942*4882a593Smuzhiyun qh->qh_state = QH_STATE_IDLE;
943*4882a593Smuzhiyun hw = qh->hw;
944*4882a593Smuzhiyun hw->hw_info1 = cpu_to_hc32(ehci, info1);
945*4882a593Smuzhiyun hw->hw_info2 = cpu_to_hc32(ehci, info2);
946*4882a593Smuzhiyun qh->is_out = !is_input;
947*4882a593Smuzhiyun usb_settoggle (urb->dev, usb_pipeendpoint (urb->pipe), !is_input, 1);
948*4882a593Smuzhiyun return qh;
949*4882a593Smuzhiyun }
950*4882a593Smuzhiyun
951*4882a593Smuzhiyun /*-------------------------------------------------------------------------*/
952*4882a593Smuzhiyun
enable_async(struct ehci_hcd * ehci)953*4882a593Smuzhiyun static void enable_async(struct ehci_hcd *ehci)
954*4882a593Smuzhiyun {
955*4882a593Smuzhiyun if (ehci->async_count++)
956*4882a593Smuzhiyun return;
957*4882a593Smuzhiyun
958*4882a593Smuzhiyun /* Stop waiting to turn off the async schedule */
959*4882a593Smuzhiyun ehci->enabled_hrtimer_events &= ~BIT(EHCI_HRTIMER_DISABLE_ASYNC);
960*4882a593Smuzhiyun
961*4882a593Smuzhiyun /* Don't start the schedule until ASS is 0 */
962*4882a593Smuzhiyun ehci_poll_ASS(ehci);
963*4882a593Smuzhiyun turn_on_io_watchdog(ehci);
964*4882a593Smuzhiyun }
965*4882a593Smuzhiyun
disable_async(struct ehci_hcd * ehci)966*4882a593Smuzhiyun static void disable_async(struct ehci_hcd *ehci)
967*4882a593Smuzhiyun {
968*4882a593Smuzhiyun if (--ehci->async_count)
969*4882a593Smuzhiyun return;
970*4882a593Smuzhiyun
971*4882a593Smuzhiyun /* The async schedule and unlink lists are supposed to be empty */
972*4882a593Smuzhiyun WARN_ON(ehci->async->qh_next.qh || !list_empty(&ehci->async_unlink) ||
973*4882a593Smuzhiyun !list_empty(&ehci->async_idle));
974*4882a593Smuzhiyun
975*4882a593Smuzhiyun /* Don't turn off the schedule until ASS is 1 */
976*4882a593Smuzhiyun ehci_poll_ASS(ehci);
977*4882a593Smuzhiyun }
978*4882a593Smuzhiyun
979*4882a593Smuzhiyun /* move qh (and its qtds) onto async queue; maybe enable queue. */
980*4882a593Smuzhiyun
qh_link_async(struct ehci_hcd * ehci,struct ehci_qh * qh)981*4882a593Smuzhiyun static void qh_link_async (struct ehci_hcd *ehci, struct ehci_qh *qh)
982*4882a593Smuzhiyun {
983*4882a593Smuzhiyun __hc32 dma = QH_NEXT(ehci, qh->qh_dma);
984*4882a593Smuzhiyun struct ehci_qh *head;
985*4882a593Smuzhiyun
986*4882a593Smuzhiyun /* Don't link a QH if there's a Clear-TT-Buffer pending */
987*4882a593Smuzhiyun if (unlikely(qh->clearing_tt))
988*4882a593Smuzhiyun return;
989*4882a593Smuzhiyun
990*4882a593Smuzhiyun WARN_ON(qh->qh_state != QH_STATE_IDLE);
991*4882a593Smuzhiyun
992*4882a593Smuzhiyun /* clear halt and/or toggle; and maybe recover from silicon quirk */
993*4882a593Smuzhiyun qh_refresh(ehci, qh);
994*4882a593Smuzhiyun
995*4882a593Smuzhiyun /* splice right after start */
996*4882a593Smuzhiyun head = ehci->async;
997*4882a593Smuzhiyun qh->qh_next = head->qh_next;
998*4882a593Smuzhiyun qh->hw->hw_next = head->hw->hw_next;
999*4882a593Smuzhiyun wmb ();
1000*4882a593Smuzhiyun
1001*4882a593Smuzhiyun head->qh_next.qh = qh;
1002*4882a593Smuzhiyun head->hw->hw_next = dma;
1003*4882a593Smuzhiyun
1004*4882a593Smuzhiyun qh->qh_state = QH_STATE_LINKED;
1005*4882a593Smuzhiyun qh->xacterrs = 0;
1006*4882a593Smuzhiyun qh->unlink_reason = 0;
1007*4882a593Smuzhiyun /* qtd completions reported later by interrupt */
1008*4882a593Smuzhiyun
1009*4882a593Smuzhiyun enable_async(ehci);
1010*4882a593Smuzhiyun }
1011*4882a593Smuzhiyun
1012*4882a593Smuzhiyun /*-------------------------------------------------------------------------*/
1013*4882a593Smuzhiyun
1014*4882a593Smuzhiyun /*
1015*4882a593Smuzhiyun * For control/bulk/interrupt, return QH with these TDs appended.
1016*4882a593Smuzhiyun * Allocates and initializes the QH if necessary.
1017*4882a593Smuzhiyun * Returns null if it can't allocate a QH it needs to.
1018*4882a593Smuzhiyun * If the QH has TDs (urbs) already, that's great.
1019*4882a593Smuzhiyun */
qh_append_tds(struct ehci_hcd * ehci,struct urb * urb,struct list_head * qtd_list,int epnum,void ** ptr)1020*4882a593Smuzhiyun static struct ehci_qh *qh_append_tds (
1021*4882a593Smuzhiyun struct ehci_hcd *ehci,
1022*4882a593Smuzhiyun struct urb *urb,
1023*4882a593Smuzhiyun struct list_head *qtd_list,
1024*4882a593Smuzhiyun int epnum,
1025*4882a593Smuzhiyun void **ptr
1026*4882a593Smuzhiyun )
1027*4882a593Smuzhiyun {
1028*4882a593Smuzhiyun struct ehci_qh *qh = NULL;
1029*4882a593Smuzhiyun __hc32 qh_addr_mask = cpu_to_hc32(ehci, 0x7f);
1030*4882a593Smuzhiyun
1031*4882a593Smuzhiyun qh = (struct ehci_qh *) *ptr;
1032*4882a593Smuzhiyun if (unlikely (qh == NULL)) {
1033*4882a593Smuzhiyun /* can't sleep here, we have ehci->lock... */
1034*4882a593Smuzhiyun qh = qh_make (ehci, urb, GFP_ATOMIC);
1035*4882a593Smuzhiyun *ptr = qh;
1036*4882a593Smuzhiyun }
1037*4882a593Smuzhiyun if (likely (qh != NULL)) {
1038*4882a593Smuzhiyun struct ehci_qtd *qtd;
1039*4882a593Smuzhiyun
1040*4882a593Smuzhiyun if (unlikely (list_empty (qtd_list)))
1041*4882a593Smuzhiyun qtd = NULL;
1042*4882a593Smuzhiyun else
1043*4882a593Smuzhiyun qtd = list_entry (qtd_list->next, struct ehci_qtd,
1044*4882a593Smuzhiyun qtd_list);
1045*4882a593Smuzhiyun
1046*4882a593Smuzhiyun /* control qh may need patching ... */
1047*4882a593Smuzhiyun if (unlikely (epnum == 0)) {
1048*4882a593Smuzhiyun
1049*4882a593Smuzhiyun /* usb_reset_device() briefly reverts to address 0 */
1050*4882a593Smuzhiyun if (usb_pipedevice (urb->pipe) == 0)
1051*4882a593Smuzhiyun qh->hw->hw_info1 &= ~qh_addr_mask;
1052*4882a593Smuzhiyun }
1053*4882a593Smuzhiyun
1054*4882a593Smuzhiyun /* just one way to queue requests: swap with the dummy qtd.
1055*4882a593Smuzhiyun * only hc or qh_refresh() ever modify the overlay.
1056*4882a593Smuzhiyun */
1057*4882a593Smuzhiyun if (likely (qtd != NULL)) {
1058*4882a593Smuzhiyun struct ehci_qtd *dummy;
1059*4882a593Smuzhiyun dma_addr_t dma;
1060*4882a593Smuzhiyun __hc32 token;
1061*4882a593Smuzhiyun
1062*4882a593Smuzhiyun /* to avoid racing the HC, use the dummy td instead of
1063*4882a593Smuzhiyun * the first td of our list (becomes new dummy). both
1064*4882a593Smuzhiyun * tds stay deactivated until we're done, when the
1065*4882a593Smuzhiyun * HC is allowed to fetch the old dummy (4.10.2).
1066*4882a593Smuzhiyun */
1067*4882a593Smuzhiyun token = qtd->hw_token;
1068*4882a593Smuzhiyun qtd->hw_token = HALT_BIT(ehci);
1069*4882a593Smuzhiyun
1070*4882a593Smuzhiyun dummy = qh->dummy;
1071*4882a593Smuzhiyun
1072*4882a593Smuzhiyun dma = dummy->qtd_dma;
1073*4882a593Smuzhiyun *dummy = *qtd;
1074*4882a593Smuzhiyun dummy->qtd_dma = dma;
1075*4882a593Smuzhiyun
1076*4882a593Smuzhiyun list_del (&qtd->qtd_list);
1077*4882a593Smuzhiyun list_add (&dummy->qtd_list, qtd_list);
1078*4882a593Smuzhiyun list_splice_tail(qtd_list, &qh->qtd_list);
1079*4882a593Smuzhiyun
1080*4882a593Smuzhiyun ehci_qtd_init(ehci, qtd, qtd->qtd_dma);
1081*4882a593Smuzhiyun qh->dummy = qtd;
1082*4882a593Smuzhiyun
1083*4882a593Smuzhiyun /* hc must see the new dummy at list end */
1084*4882a593Smuzhiyun dma = qtd->qtd_dma;
1085*4882a593Smuzhiyun qtd = list_entry (qh->qtd_list.prev,
1086*4882a593Smuzhiyun struct ehci_qtd, qtd_list);
1087*4882a593Smuzhiyun qtd->hw_next = QTD_NEXT(ehci, dma);
1088*4882a593Smuzhiyun
1089*4882a593Smuzhiyun /* let the hc process these next qtds */
1090*4882a593Smuzhiyun wmb ();
1091*4882a593Smuzhiyun dummy->hw_token = token;
1092*4882a593Smuzhiyun
1093*4882a593Smuzhiyun urb->hcpriv = qh;
1094*4882a593Smuzhiyun }
1095*4882a593Smuzhiyun }
1096*4882a593Smuzhiyun return qh;
1097*4882a593Smuzhiyun }
1098*4882a593Smuzhiyun
1099*4882a593Smuzhiyun /*-------------------------------------------------------------------------*/
1100*4882a593Smuzhiyun
1101*4882a593Smuzhiyun static int
submit_async(struct ehci_hcd * ehci,struct urb * urb,struct list_head * qtd_list,gfp_t mem_flags)1102*4882a593Smuzhiyun submit_async (
1103*4882a593Smuzhiyun struct ehci_hcd *ehci,
1104*4882a593Smuzhiyun struct urb *urb,
1105*4882a593Smuzhiyun struct list_head *qtd_list,
1106*4882a593Smuzhiyun gfp_t mem_flags
1107*4882a593Smuzhiyun ) {
1108*4882a593Smuzhiyun int epnum;
1109*4882a593Smuzhiyun unsigned long flags;
1110*4882a593Smuzhiyun struct ehci_qh *qh = NULL;
1111*4882a593Smuzhiyun int rc;
1112*4882a593Smuzhiyun
1113*4882a593Smuzhiyun epnum = urb->ep->desc.bEndpointAddress;
1114*4882a593Smuzhiyun
1115*4882a593Smuzhiyun #ifdef EHCI_URB_TRACE
1116*4882a593Smuzhiyun {
1117*4882a593Smuzhiyun struct ehci_qtd *qtd;
1118*4882a593Smuzhiyun qtd = list_entry(qtd_list->next, struct ehci_qtd, qtd_list);
1119*4882a593Smuzhiyun ehci_dbg(ehci,
1120*4882a593Smuzhiyun "%s %s urb %p ep%d%s len %d, qtd %p [qh %p]\n",
1121*4882a593Smuzhiyun __func__, urb->dev->devpath, urb,
1122*4882a593Smuzhiyun epnum & 0x0f, (epnum & USB_DIR_IN) ? "in" : "out",
1123*4882a593Smuzhiyun urb->transfer_buffer_length,
1124*4882a593Smuzhiyun qtd, urb->ep->hcpriv);
1125*4882a593Smuzhiyun }
1126*4882a593Smuzhiyun #endif
1127*4882a593Smuzhiyun
1128*4882a593Smuzhiyun spin_lock_irqsave (&ehci->lock, flags);
1129*4882a593Smuzhiyun if (unlikely(!HCD_HW_ACCESSIBLE(ehci_to_hcd(ehci)))) {
1130*4882a593Smuzhiyun rc = -ESHUTDOWN;
1131*4882a593Smuzhiyun goto done;
1132*4882a593Smuzhiyun }
1133*4882a593Smuzhiyun rc = usb_hcd_link_urb_to_ep(ehci_to_hcd(ehci), urb);
1134*4882a593Smuzhiyun if (unlikely(rc))
1135*4882a593Smuzhiyun goto done;
1136*4882a593Smuzhiyun
1137*4882a593Smuzhiyun qh = qh_append_tds(ehci, urb, qtd_list, epnum, &urb->ep->hcpriv);
1138*4882a593Smuzhiyun if (unlikely(qh == NULL)) {
1139*4882a593Smuzhiyun usb_hcd_unlink_urb_from_ep(ehci_to_hcd(ehci), urb);
1140*4882a593Smuzhiyun rc = -ENOMEM;
1141*4882a593Smuzhiyun goto done;
1142*4882a593Smuzhiyun }
1143*4882a593Smuzhiyun
1144*4882a593Smuzhiyun /* Control/bulk operations through TTs don't need scheduling,
1145*4882a593Smuzhiyun * the HC and TT handle it when the TT has a buffer ready.
1146*4882a593Smuzhiyun */
1147*4882a593Smuzhiyun if (likely (qh->qh_state == QH_STATE_IDLE))
1148*4882a593Smuzhiyun qh_link_async(ehci, qh);
1149*4882a593Smuzhiyun done:
1150*4882a593Smuzhiyun spin_unlock_irqrestore (&ehci->lock, flags);
1151*4882a593Smuzhiyun if (unlikely (qh == NULL))
1152*4882a593Smuzhiyun qtd_list_free (ehci, urb, qtd_list);
1153*4882a593Smuzhiyun return rc;
1154*4882a593Smuzhiyun }
1155*4882a593Smuzhiyun
1156*4882a593Smuzhiyun /*-------------------------------------------------------------------------*/
1157*4882a593Smuzhiyun #ifdef CONFIG_USB_HCD_TEST_MODE
1158*4882a593Smuzhiyun /*
1159*4882a593Smuzhiyun * This function creates the qtds and submits them for the
1160*4882a593Smuzhiyun * SINGLE_STEP_SET_FEATURE Test.
1161*4882a593Smuzhiyun * This is done in two parts: first SETUP req for GetDesc is sent then
1162*4882a593Smuzhiyun * 15 seconds later, the IN stage for GetDesc starts to req data from dev
1163*4882a593Smuzhiyun *
1164*4882a593Smuzhiyun * is_setup : i/p arguement decides which of the two stage needs to be
1165*4882a593Smuzhiyun * performed; TRUE - SETUP and FALSE - IN+STATUS
1166*4882a593Smuzhiyun * Returns 0 if success
1167*4882a593Smuzhiyun */
submit_single_step_set_feature(struct usb_hcd * hcd,struct urb * urb,int is_setup)1168*4882a593Smuzhiyun static int submit_single_step_set_feature(
1169*4882a593Smuzhiyun struct usb_hcd *hcd,
1170*4882a593Smuzhiyun struct urb *urb,
1171*4882a593Smuzhiyun int is_setup
1172*4882a593Smuzhiyun ) {
1173*4882a593Smuzhiyun struct ehci_hcd *ehci = hcd_to_ehci(hcd);
1174*4882a593Smuzhiyun struct list_head qtd_list;
1175*4882a593Smuzhiyun struct list_head *head;
1176*4882a593Smuzhiyun
1177*4882a593Smuzhiyun struct ehci_qtd *qtd, *qtd_prev;
1178*4882a593Smuzhiyun dma_addr_t buf;
1179*4882a593Smuzhiyun int len, maxpacket;
1180*4882a593Smuzhiyun u32 token;
1181*4882a593Smuzhiyun
1182*4882a593Smuzhiyun INIT_LIST_HEAD(&qtd_list);
1183*4882a593Smuzhiyun head = &qtd_list;
1184*4882a593Smuzhiyun
1185*4882a593Smuzhiyun /* URBs map to sequences of QTDs: one logical transaction */
1186*4882a593Smuzhiyun qtd = ehci_qtd_alloc(ehci, GFP_KERNEL);
1187*4882a593Smuzhiyun if (unlikely(!qtd))
1188*4882a593Smuzhiyun return -1;
1189*4882a593Smuzhiyun list_add_tail(&qtd->qtd_list, head);
1190*4882a593Smuzhiyun qtd->urb = urb;
1191*4882a593Smuzhiyun
1192*4882a593Smuzhiyun token = QTD_STS_ACTIVE;
1193*4882a593Smuzhiyun token |= (EHCI_TUNE_CERR << 10);
1194*4882a593Smuzhiyun
1195*4882a593Smuzhiyun len = urb->transfer_buffer_length;
1196*4882a593Smuzhiyun /*
1197*4882a593Smuzhiyun * Check if the request is to perform just the SETUP stage (getDesc)
1198*4882a593Smuzhiyun * as in SINGLE_STEP_SET_FEATURE test, DATA stage (IN) happens
1199*4882a593Smuzhiyun * 15 secs after the setup
1200*4882a593Smuzhiyun */
1201*4882a593Smuzhiyun if (is_setup) {
1202*4882a593Smuzhiyun /* SETUP pid, and interrupt after SETUP completion */
1203*4882a593Smuzhiyun qtd_fill(ehci, qtd, urb->setup_dma,
1204*4882a593Smuzhiyun sizeof(struct usb_ctrlrequest),
1205*4882a593Smuzhiyun QTD_IOC | token | (2 /* "setup" */ << 8), 8);
1206*4882a593Smuzhiyun
1207*4882a593Smuzhiyun submit_async(ehci, urb, &qtd_list, GFP_ATOMIC);
1208*4882a593Smuzhiyun return 0; /*Return now; we shall come back after 15 seconds*/
1209*4882a593Smuzhiyun }
1210*4882a593Smuzhiyun
1211*4882a593Smuzhiyun /*
1212*4882a593Smuzhiyun * IN: data transfer stage: buffer setup : start the IN txn phase for
1213*4882a593Smuzhiyun * the get_Desc SETUP which was sent 15seconds back
1214*4882a593Smuzhiyun */
1215*4882a593Smuzhiyun token ^= QTD_TOGGLE; /*We need to start IN with DATA-1 Pid-sequence*/
1216*4882a593Smuzhiyun buf = urb->transfer_dma;
1217*4882a593Smuzhiyun
1218*4882a593Smuzhiyun token |= (1 /* "in" */ << 8); /*This is IN stage*/
1219*4882a593Smuzhiyun
1220*4882a593Smuzhiyun maxpacket = usb_maxpacket(urb->dev, urb->pipe, 0);
1221*4882a593Smuzhiyun
1222*4882a593Smuzhiyun qtd_fill(ehci, qtd, buf, len, token, maxpacket);
1223*4882a593Smuzhiyun
1224*4882a593Smuzhiyun /*
1225*4882a593Smuzhiyun * Our IN phase shall always be a short read; so keep the queue running
1226*4882a593Smuzhiyun * and let it advance to the next qtd which zero length OUT status
1227*4882a593Smuzhiyun */
1228*4882a593Smuzhiyun qtd->hw_alt_next = EHCI_LIST_END(ehci);
1229*4882a593Smuzhiyun
1230*4882a593Smuzhiyun /* STATUS stage for GetDesc control request */
1231*4882a593Smuzhiyun token ^= 0x0100; /* "in" <--> "out" */
1232*4882a593Smuzhiyun token |= QTD_TOGGLE; /* force DATA1 */
1233*4882a593Smuzhiyun
1234*4882a593Smuzhiyun qtd_prev = qtd;
1235*4882a593Smuzhiyun qtd = ehci_qtd_alloc(ehci, GFP_ATOMIC);
1236*4882a593Smuzhiyun if (unlikely(!qtd))
1237*4882a593Smuzhiyun goto cleanup;
1238*4882a593Smuzhiyun qtd->urb = urb;
1239*4882a593Smuzhiyun qtd_prev->hw_next = QTD_NEXT(ehci, qtd->qtd_dma);
1240*4882a593Smuzhiyun list_add_tail(&qtd->qtd_list, head);
1241*4882a593Smuzhiyun
1242*4882a593Smuzhiyun /* Interrupt after STATUS completion */
1243*4882a593Smuzhiyun qtd_fill(ehci, qtd, 0, 0, token | QTD_IOC, 0);
1244*4882a593Smuzhiyun
1245*4882a593Smuzhiyun submit_async(ehci, urb, &qtd_list, GFP_KERNEL);
1246*4882a593Smuzhiyun
1247*4882a593Smuzhiyun return 0;
1248*4882a593Smuzhiyun
1249*4882a593Smuzhiyun cleanup:
1250*4882a593Smuzhiyun qtd_list_free(ehci, urb, head);
1251*4882a593Smuzhiyun return -1;
1252*4882a593Smuzhiyun }
1253*4882a593Smuzhiyun #endif /* CONFIG_USB_HCD_TEST_MODE */
1254*4882a593Smuzhiyun
1255*4882a593Smuzhiyun /*-------------------------------------------------------------------------*/
1256*4882a593Smuzhiyun
single_unlink_async(struct ehci_hcd * ehci,struct ehci_qh * qh)1257*4882a593Smuzhiyun static void single_unlink_async(struct ehci_hcd *ehci, struct ehci_qh *qh)
1258*4882a593Smuzhiyun {
1259*4882a593Smuzhiyun struct ehci_qh *prev;
1260*4882a593Smuzhiyun
1261*4882a593Smuzhiyun /* Add to the end of the list of QHs waiting for the next IAAD */
1262*4882a593Smuzhiyun qh->qh_state = QH_STATE_UNLINK_WAIT;
1263*4882a593Smuzhiyun list_add_tail(&qh->unlink_node, &ehci->async_unlink);
1264*4882a593Smuzhiyun
1265*4882a593Smuzhiyun /* Unlink it from the schedule */
1266*4882a593Smuzhiyun prev = ehci->async;
1267*4882a593Smuzhiyun while (prev->qh_next.qh != qh)
1268*4882a593Smuzhiyun prev = prev->qh_next.qh;
1269*4882a593Smuzhiyun
1270*4882a593Smuzhiyun prev->hw->hw_next = qh->hw->hw_next;
1271*4882a593Smuzhiyun prev->qh_next = qh->qh_next;
1272*4882a593Smuzhiyun if (ehci->qh_scan_next == qh)
1273*4882a593Smuzhiyun ehci->qh_scan_next = qh->qh_next.qh;
1274*4882a593Smuzhiyun }
1275*4882a593Smuzhiyun
start_iaa_cycle(struct ehci_hcd * ehci)1276*4882a593Smuzhiyun static void start_iaa_cycle(struct ehci_hcd *ehci)
1277*4882a593Smuzhiyun {
1278*4882a593Smuzhiyun /* If the controller isn't running, we don't have to wait for it */
1279*4882a593Smuzhiyun if (unlikely(ehci->rh_state < EHCI_RH_RUNNING)) {
1280*4882a593Smuzhiyun end_unlink_async(ehci);
1281*4882a593Smuzhiyun
1282*4882a593Smuzhiyun /* Otherwise start a new IAA cycle if one isn't already running */
1283*4882a593Smuzhiyun } else if (ehci->rh_state == EHCI_RH_RUNNING &&
1284*4882a593Smuzhiyun !ehci->iaa_in_progress) {
1285*4882a593Smuzhiyun
1286*4882a593Smuzhiyun /* Make sure the unlinks are all visible to the hardware */
1287*4882a593Smuzhiyun wmb();
1288*4882a593Smuzhiyun
1289*4882a593Smuzhiyun ehci_writel(ehci, ehci->command | CMD_IAAD,
1290*4882a593Smuzhiyun &ehci->regs->command);
1291*4882a593Smuzhiyun ehci_readl(ehci, &ehci->regs->command);
1292*4882a593Smuzhiyun ehci->iaa_in_progress = true;
1293*4882a593Smuzhiyun ehci_enable_event(ehci, EHCI_HRTIMER_IAA_WATCHDOG, true);
1294*4882a593Smuzhiyun }
1295*4882a593Smuzhiyun }
1296*4882a593Smuzhiyun
end_iaa_cycle(struct ehci_hcd * ehci)1297*4882a593Smuzhiyun static void end_iaa_cycle(struct ehci_hcd *ehci)
1298*4882a593Smuzhiyun {
1299*4882a593Smuzhiyun if (ehci->has_synopsys_hc_bug)
1300*4882a593Smuzhiyun ehci_writel(ehci, (u32) ehci->async->qh_dma,
1301*4882a593Smuzhiyun &ehci->regs->async_next);
1302*4882a593Smuzhiyun
1303*4882a593Smuzhiyun /* The current IAA cycle has ended */
1304*4882a593Smuzhiyun ehci->iaa_in_progress = false;
1305*4882a593Smuzhiyun
1306*4882a593Smuzhiyun end_unlink_async(ehci);
1307*4882a593Smuzhiyun }
1308*4882a593Smuzhiyun
1309*4882a593Smuzhiyun /* See if the async qh for the qtds being unlinked are now gone from the HC */
1310*4882a593Smuzhiyun
end_unlink_async(struct ehci_hcd * ehci)1311*4882a593Smuzhiyun static void end_unlink_async(struct ehci_hcd *ehci)
1312*4882a593Smuzhiyun {
1313*4882a593Smuzhiyun struct ehci_qh *qh;
1314*4882a593Smuzhiyun bool early_exit;
1315*4882a593Smuzhiyun
1316*4882a593Smuzhiyun if (list_empty(&ehci->async_unlink))
1317*4882a593Smuzhiyun return;
1318*4882a593Smuzhiyun qh = list_first_entry(&ehci->async_unlink, struct ehci_qh,
1319*4882a593Smuzhiyun unlink_node); /* QH whose IAA cycle just ended */
1320*4882a593Smuzhiyun
1321*4882a593Smuzhiyun /*
1322*4882a593Smuzhiyun * If async_unlinking is set then this routine is already running,
1323*4882a593Smuzhiyun * either on the stack or on another CPU.
1324*4882a593Smuzhiyun */
1325*4882a593Smuzhiyun early_exit = ehci->async_unlinking;
1326*4882a593Smuzhiyun
1327*4882a593Smuzhiyun /* If the controller isn't running, process all the waiting QHs */
1328*4882a593Smuzhiyun if (ehci->rh_state < EHCI_RH_RUNNING)
1329*4882a593Smuzhiyun list_splice_tail_init(&ehci->async_unlink, &ehci->async_idle);
1330*4882a593Smuzhiyun
1331*4882a593Smuzhiyun /*
1332*4882a593Smuzhiyun * Intel (?) bug: The HC can write back the overlay region even
1333*4882a593Smuzhiyun * after the IAA interrupt occurs. In self-defense, always go
1334*4882a593Smuzhiyun * through two IAA cycles for each QH.
1335*4882a593Smuzhiyun */
1336*4882a593Smuzhiyun else if (qh->qh_state == QH_STATE_UNLINK) {
1337*4882a593Smuzhiyun /*
1338*4882a593Smuzhiyun * Second IAA cycle has finished. Process only the first
1339*4882a593Smuzhiyun * waiting QH (NVIDIA (?) bug).
1340*4882a593Smuzhiyun */
1341*4882a593Smuzhiyun list_move_tail(&qh->unlink_node, &ehci->async_idle);
1342*4882a593Smuzhiyun }
1343*4882a593Smuzhiyun
1344*4882a593Smuzhiyun /*
1345*4882a593Smuzhiyun * AMD/ATI (?) bug: The HC can continue to use an active QH long
1346*4882a593Smuzhiyun * after the IAA interrupt occurs. To prevent problems, QHs that
1347*4882a593Smuzhiyun * may still be active will wait until 2 ms have passed with no
1348*4882a593Smuzhiyun * change to the hw_current and hw_token fields (this delay occurs
1349*4882a593Smuzhiyun * between the two IAA cycles).
1350*4882a593Smuzhiyun *
1351*4882a593Smuzhiyun * The EHCI spec (4.8.2) says that active QHs must not be removed
1352*4882a593Smuzhiyun * from the async schedule and recommends waiting until the QH
1353*4882a593Smuzhiyun * goes inactive. This is ridiculous because the QH will _never_
1354*4882a593Smuzhiyun * become inactive if the endpoint NAKs indefinitely.
1355*4882a593Smuzhiyun */
1356*4882a593Smuzhiyun
1357*4882a593Smuzhiyun /* Some reasons for unlinking guarantee the QH can't be active */
1358*4882a593Smuzhiyun else if (qh->unlink_reason & (QH_UNLINK_HALTED |
1359*4882a593Smuzhiyun QH_UNLINK_SHORT_READ | QH_UNLINK_DUMMY_OVERLAY))
1360*4882a593Smuzhiyun goto DelayDone;
1361*4882a593Smuzhiyun
1362*4882a593Smuzhiyun /* The QH can't be active if the queue was and still is empty... */
1363*4882a593Smuzhiyun else if ((qh->unlink_reason & QH_UNLINK_QUEUE_EMPTY) &&
1364*4882a593Smuzhiyun list_empty(&qh->qtd_list))
1365*4882a593Smuzhiyun goto DelayDone;
1366*4882a593Smuzhiyun
1367*4882a593Smuzhiyun /* ... or if the QH has halted */
1368*4882a593Smuzhiyun else if (qh->hw->hw_token & cpu_to_hc32(ehci, QTD_STS_HALT))
1369*4882a593Smuzhiyun goto DelayDone;
1370*4882a593Smuzhiyun
1371*4882a593Smuzhiyun /* Otherwise we have to wait until the QH stops changing */
1372*4882a593Smuzhiyun else {
1373*4882a593Smuzhiyun __hc32 qh_current, qh_token;
1374*4882a593Smuzhiyun
1375*4882a593Smuzhiyun qh_current = qh->hw->hw_current;
1376*4882a593Smuzhiyun qh_token = qh->hw->hw_token;
1377*4882a593Smuzhiyun if (qh_current != ehci->old_current ||
1378*4882a593Smuzhiyun qh_token != ehci->old_token) {
1379*4882a593Smuzhiyun ehci->old_current = qh_current;
1380*4882a593Smuzhiyun ehci->old_token = qh_token;
1381*4882a593Smuzhiyun ehci_enable_event(ehci,
1382*4882a593Smuzhiyun EHCI_HRTIMER_ACTIVE_UNLINK, true);
1383*4882a593Smuzhiyun return;
1384*4882a593Smuzhiyun }
1385*4882a593Smuzhiyun DelayDone:
1386*4882a593Smuzhiyun qh->qh_state = QH_STATE_UNLINK;
1387*4882a593Smuzhiyun early_exit = true;
1388*4882a593Smuzhiyun }
1389*4882a593Smuzhiyun ehci->old_current = ~0; /* Prepare for next QH */
1390*4882a593Smuzhiyun
1391*4882a593Smuzhiyun /* Start a new IAA cycle if any QHs are waiting for it */
1392*4882a593Smuzhiyun if (!list_empty(&ehci->async_unlink))
1393*4882a593Smuzhiyun start_iaa_cycle(ehci);
1394*4882a593Smuzhiyun
1395*4882a593Smuzhiyun /*
1396*4882a593Smuzhiyun * Don't allow nesting or concurrent calls,
1397*4882a593Smuzhiyun * or wait for the second IAA cycle for the next QH.
1398*4882a593Smuzhiyun */
1399*4882a593Smuzhiyun if (early_exit)
1400*4882a593Smuzhiyun return;
1401*4882a593Smuzhiyun
1402*4882a593Smuzhiyun /* Process the idle QHs */
1403*4882a593Smuzhiyun ehci->async_unlinking = true;
1404*4882a593Smuzhiyun while (!list_empty(&ehci->async_idle)) {
1405*4882a593Smuzhiyun qh = list_first_entry(&ehci->async_idle, struct ehci_qh,
1406*4882a593Smuzhiyun unlink_node);
1407*4882a593Smuzhiyun list_del(&qh->unlink_node);
1408*4882a593Smuzhiyun
1409*4882a593Smuzhiyun qh->qh_state = QH_STATE_IDLE;
1410*4882a593Smuzhiyun qh->qh_next.qh = NULL;
1411*4882a593Smuzhiyun
1412*4882a593Smuzhiyun if (!list_empty(&qh->qtd_list))
1413*4882a593Smuzhiyun qh_completions(ehci, qh);
1414*4882a593Smuzhiyun if (!list_empty(&qh->qtd_list) &&
1415*4882a593Smuzhiyun ehci->rh_state == EHCI_RH_RUNNING)
1416*4882a593Smuzhiyun qh_link_async(ehci, qh);
1417*4882a593Smuzhiyun disable_async(ehci);
1418*4882a593Smuzhiyun }
1419*4882a593Smuzhiyun ehci->async_unlinking = false;
1420*4882a593Smuzhiyun }
1421*4882a593Smuzhiyun
1422*4882a593Smuzhiyun static void start_unlink_async(struct ehci_hcd *ehci, struct ehci_qh *qh);
1423*4882a593Smuzhiyun
unlink_empty_async(struct ehci_hcd * ehci)1424*4882a593Smuzhiyun static void unlink_empty_async(struct ehci_hcd *ehci)
1425*4882a593Smuzhiyun {
1426*4882a593Smuzhiyun struct ehci_qh *qh;
1427*4882a593Smuzhiyun struct ehci_qh *qh_to_unlink = NULL;
1428*4882a593Smuzhiyun int count = 0;
1429*4882a593Smuzhiyun
1430*4882a593Smuzhiyun /* Find the last async QH which has been empty for a timer cycle */
1431*4882a593Smuzhiyun for (qh = ehci->async->qh_next.qh; qh; qh = qh->qh_next.qh) {
1432*4882a593Smuzhiyun if (list_empty(&qh->qtd_list) &&
1433*4882a593Smuzhiyun qh->qh_state == QH_STATE_LINKED) {
1434*4882a593Smuzhiyun ++count;
1435*4882a593Smuzhiyun if (qh->unlink_cycle != ehci->async_unlink_cycle)
1436*4882a593Smuzhiyun qh_to_unlink = qh;
1437*4882a593Smuzhiyun }
1438*4882a593Smuzhiyun }
1439*4882a593Smuzhiyun
1440*4882a593Smuzhiyun /* If nothing else is being unlinked, unlink the last empty QH */
1441*4882a593Smuzhiyun if (list_empty(&ehci->async_unlink) && qh_to_unlink) {
1442*4882a593Smuzhiyun qh_to_unlink->unlink_reason |= QH_UNLINK_QUEUE_EMPTY;
1443*4882a593Smuzhiyun start_unlink_async(ehci, qh_to_unlink);
1444*4882a593Smuzhiyun --count;
1445*4882a593Smuzhiyun }
1446*4882a593Smuzhiyun
1447*4882a593Smuzhiyun /* Other QHs will be handled later */
1448*4882a593Smuzhiyun if (count > 0) {
1449*4882a593Smuzhiyun ehci_enable_event(ehci, EHCI_HRTIMER_ASYNC_UNLINKS, true);
1450*4882a593Smuzhiyun ++ehci->async_unlink_cycle;
1451*4882a593Smuzhiyun }
1452*4882a593Smuzhiyun }
1453*4882a593Smuzhiyun
1454*4882a593Smuzhiyun #ifdef CONFIG_PM
1455*4882a593Smuzhiyun
1456*4882a593Smuzhiyun /* The root hub is suspended; unlink all the async QHs */
unlink_empty_async_suspended(struct ehci_hcd * ehci)1457*4882a593Smuzhiyun static void unlink_empty_async_suspended(struct ehci_hcd *ehci)
1458*4882a593Smuzhiyun {
1459*4882a593Smuzhiyun struct ehci_qh *qh;
1460*4882a593Smuzhiyun
1461*4882a593Smuzhiyun while (ehci->async->qh_next.qh) {
1462*4882a593Smuzhiyun qh = ehci->async->qh_next.qh;
1463*4882a593Smuzhiyun WARN_ON(!list_empty(&qh->qtd_list));
1464*4882a593Smuzhiyun single_unlink_async(ehci, qh);
1465*4882a593Smuzhiyun }
1466*4882a593Smuzhiyun }
1467*4882a593Smuzhiyun
1468*4882a593Smuzhiyun #endif
1469*4882a593Smuzhiyun
1470*4882a593Smuzhiyun /* makes sure the async qh will become idle */
1471*4882a593Smuzhiyun /* caller must own ehci->lock */
1472*4882a593Smuzhiyun
start_unlink_async(struct ehci_hcd * ehci,struct ehci_qh * qh)1473*4882a593Smuzhiyun static void start_unlink_async(struct ehci_hcd *ehci, struct ehci_qh *qh)
1474*4882a593Smuzhiyun {
1475*4882a593Smuzhiyun /* If the QH isn't linked then there's nothing we can do. */
1476*4882a593Smuzhiyun if (qh->qh_state != QH_STATE_LINKED)
1477*4882a593Smuzhiyun return;
1478*4882a593Smuzhiyun
1479*4882a593Smuzhiyun single_unlink_async(ehci, qh);
1480*4882a593Smuzhiyun start_iaa_cycle(ehci);
1481*4882a593Smuzhiyun }
1482*4882a593Smuzhiyun
1483*4882a593Smuzhiyun /*-------------------------------------------------------------------------*/
1484*4882a593Smuzhiyun
scan_async(struct ehci_hcd * ehci)1485*4882a593Smuzhiyun static void scan_async (struct ehci_hcd *ehci)
1486*4882a593Smuzhiyun {
1487*4882a593Smuzhiyun struct ehci_qh *qh;
1488*4882a593Smuzhiyun bool check_unlinks_later = false;
1489*4882a593Smuzhiyun
1490*4882a593Smuzhiyun ehci->qh_scan_next = ehci->async->qh_next.qh;
1491*4882a593Smuzhiyun while (ehci->qh_scan_next) {
1492*4882a593Smuzhiyun qh = ehci->qh_scan_next;
1493*4882a593Smuzhiyun ehci->qh_scan_next = qh->qh_next.qh;
1494*4882a593Smuzhiyun
1495*4882a593Smuzhiyun /* clean any finished work for this qh */
1496*4882a593Smuzhiyun if (!list_empty(&qh->qtd_list)) {
1497*4882a593Smuzhiyun int temp;
1498*4882a593Smuzhiyun
1499*4882a593Smuzhiyun /*
1500*4882a593Smuzhiyun * Unlinks could happen here; completion reporting
1501*4882a593Smuzhiyun * drops the lock. That's why ehci->qh_scan_next
1502*4882a593Smuzhiyun * always holds the next qh to scan; if the next qh
1503*4882a593Smuzhiyun * gets unlinked then ehci->qh_scan_next is adjusted
1504*4882a593Smuzhiyun * in single_unlink_async().
1505*4882a593Smuzhiyun */
1506*4882a593Smuzhiyun temp = qh_completions(ehci, qh);
1507*4882a593Smuzhiyun if (unlikely(temp)) {
1508*4882a593Smuzhiyun start_unlink_async(ehci, qh);
1509*4882a593Smuzhiyun } else if (list_empty(&qh->qtd_list)
1510*4882a593Smuzhiyun && qh->qh_state == QH_STATE_LINKED) {
1511*4882a593Smuzhiyun qh->unlink_cycle = ehci->async_unlink_cycle;
1512*4882a593Smuzhiyun check_unlinks_later = true;
1513*4882a593Smuzhiyun }
1514*4882a593Smuzhiyun }
1515*4882a593Smuzhiyun }
1516*4882a593Smuzhiyun
1517*4882a593Smuzhiyun /*
1518*4882a593Smuzhiyun * Unlink empty entries, reducing DMA usage as well
1519*4882a593Smuzhiyun * as HCD schedule-scanning costs. Delay for any qh
1520*4882a593Smuzhiyun * we just scanned, there's a not-unusual case that it
1521*4882a593Smuzhiyun * doesn't stay idle for long.
1522*4882a593Smuzhiyun */
1523*4882a593Smuzhiyun if (check_unlinks_later && ehci->rh_state == EHCI_RH_RUNNING &&
1524*4882a593Smuzhiyun !(ehci->enabled_hrtimer_events &
1525*4882a593Smuzhiyun BIT(EHCI_HRTIMER_ASYNC_UNLINKS))) {
1526*4882a593Smuzhiyun ehci_enable_event(ehci, EHCI_HRTIMER_ASYNC_UNLINKS, true);
1527*4882a593Smuzhiyun ++ehci->async_unlink_cycle;
1528*4882a593Smuzhiyun }
1529*4882a593Smuzhiyun }
1530