USB: EHCI: always scan each interrupt QH
[linux-2.6.git] / drivers / usb / host / ehci-timer.c
blob0e28bae78d18a3084b8c3ea6144404e71413b155
1 /*
2 * Copyright (C) 2012 by Alan Stern
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the
6 * Free Software Foundation; either version 2 of the License, or (at your
7 * option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * for more details.
15 /* This file is part of ehci-hcd.c */
17 /*-------------------------------------------------------------------------*/
19 /* Set a bit in the USBCMD register */
20 static void ehci_set_command_bit(struct ehci_hcd *ehci, u32 bit)
22 ehci->command |= bit;
23 ehci_writel(ehci, ehci->command, &ehci->regs->command);
25 /* unblock posted write */
26 ehci_readl(ehci, &ehci->regs->command);
29 /* Clear a bit in the USBCMD register */
30 static void ehci_clear_command_bit(struct ehci_hcd *ehci, u32 bit)
32 ehci->command &= ~bit;
33 ehci_writel(ehci, ehci->command, &ehci->regs->command);
35 /* unblock posted write */
36 ehci_readl(ehci, &ehci->regs->command);
39 /*-------------------------------------------------------------------------*/
42 * EHCI timer support... Now using hrtimers.
44 * Lots of different events are triggered from ehci->hrtimer. Whenever
45 * the timer routine runs, it checks each possible event; events that are
46 * currently enabled and whose expiration time has passed get handled.
47 * The set of enabled events is stored as a collection of bitflags in
48 * ehci->enabled_hrtimer_events, and they are numbered in order of
49 * increasing delay values (ranging between 1 ms and 100 ms).
51 * Rather than implementing a sorted list or tree of all pending events,
52 * we keep track only of the lowest-numbered pending event, in
53 * ehci->next_hrtimer_event. Whenever ehci->hrtimer gets restarted, its
54 * expiration time is set to the timeout value for this event.
56 * As a result, events might not get handled right away; the actual delay
57 * could be anywhere up to twice the requested delay. This doesn't
58 * matter, because none of the events are especially time-critical. The
59 * ones that matter most all have a delay of 1 ms, so they will be
60 * handled after 2 ms at most, which is okay. In addition to this, we
61 * allow for an expiration range of 1 ms.
65 * Delay lengths for the hrtimer event types.
66 * Keep this list sorted by delay length, in the same order as
67 * the event types indexed by enum ehci_hrtimer_event in ehci.h.
69 static unsigned event_delays_ns[] = {
70 1 * NSEC_PER_MSEC, /* EHCI_HRTIMER_POLL_ASS */
71 1 * NSEC_PER_MSEC, /* EHCI_HRTIMER_POLL_PSS */
72 1 * NSEC_PER_MSEC, /* EHCI_HRTIMER_POLL_DEAD */
73 1125 * NSEC_PER_USEC, /* EHCI_HRTIMER_UNLINK_INTR */
74 2 * NSEC_PER_MSEC, /* EHCI_HRTIMER_FREE_ITDS */
75 6 * NSEC_PER_MSEC, /* EHCI_HRTIMER_ASYNC_UNLINKS */
76 10 * NSEC_PER_MSEC, /* EHCI_HRTIMER_IAA_WATCHDOG */
77 10 * NSEC_PER_MSEC, /* EHCI_HRTIMER_DISABLE_PERIODIC */
78 15 * NSEC_PER_MSEC, /* EHCI_HRTIMER_DISABLE_ASYNC */
81 /* Enable a pending hrtimer event */
82 static void ehci_enable_event(struct ehci_hcd *ehci, unsigned event,
83 bool resched)
85 ktime_t *timeout = &ehci->hr_timeouts[event];
87 if (resched)
88 *timeout = ktime_add(ktime_get(),
89 ktime_set(0, event_delays_ns[event]));
90 ehci->enabled_hrtimer_events |= (1 << event);
92 /* Track only the lowest-numbered pending event */
93 if (event < ehci->next_hrtimer_event) {
94 ehci->next_hrtimer_event = event;
95 hrtimer_start_range_ns(&ehci->hrtimer, *timeout,
96 NSEC_PER_MSEC, HRTIMER_MODE_ABS);
101 /* Poll the STS_ASS status bit; see when it agrees with CMD_ASE */
102 static void ehci_poll_ASS(struct ehci_hcd *ehci)
104 unsigned actual, want;
106 /* Don't enable anything if the controller isn't running (e.g., died) */
107 if (ehci->rh_state != EHCI_RH_RUNNING)
108 return;
110 want = (ehci->command & CMD_ASE) ? STS_ASS : 0;
111 actual = ehci_readl(ehci, &ehci->regs->status) & STS_ASS;
113 if (want != actual) {
115 /* Poll again later, but give up after about 20 ms */
116 if (ehci->ASS_poll_count++ < 20) {
117 ehci_enable_event(ehci, EHCI_HRTIMER_POLL_ASS, true);
118 return;
120 ehci_warn(ehci, "Waited too long for the async schedule status, giving up\n");
122 ehci->ASS_poll_count = 0;
124 /* The status is up-to-date; restart or stop the schedule as needed */
125 if (want == 0) { /* Stopped */
126 if (ehci->async_count > 0)
127 ehci_set_command_bit(ehci, CMD_ASE);
129 } else { /* Running */
130 if (ehci->async_count == 0) {
132 /* Turn off the schedule after a while */
133 ehci_enable_event(ehci, EHCI_HRTIMER_DISABLE_ASYNC,
134 true);
139 /* Turn off the async schedule after a brief delay */
140 static void ehci_disable_ASE(struct ehci_hcd *ehci)
142 ehci_clear_command_bit(ehci, CMD_ASE);
146 /* Poll the STS_PSS status bit; see when it agrees with CMD_PSE */
147 static void ehci_poll_PSS(struct ehci_hcd *ehci)
149 unsigned actual, want;
151 /* Don't do anything if the controller isn't running (e.g., died) */
152 if (ehci->rh_state != EHCI_RH_RUNNING)
153 return;
155 want = (ehci->command & CMD_PSE) ? STS_PSS : 0;
156 actual = ehci_readl(ehci, &ehci->regs->status) & STS_PSS;
158 if (want != actual) {
160 /* Poll again later, but give up after about 20 ms */
161 if (ehci->PSS_poll_count++ < 20) {
162 ehci_enable_event(ehci, EHCI_HRTIMER_POLL_PSS, true);
163 return;
165 ehci_warn(ehci, "Waited too long for the periodic schedule status, giving up\n");
167 ehci->PSS_poll_count = 0;
169 /* The status is up-to-date; restart or stop the schedule as needed */
170 if (want == 0) { /* Stopped */
171 if (ehci->periodic_count > 0)
172 ehci_set_command_bit(ehci, CMD_PSE);
174 } else { /* Running */
175 if (ehci->periodic_count == 0) {
177 /* Turn off the schedule after a while */
178 ehci_enable_event(ehci, EHCI_HRTIMER_DISABLE_PERIODIC,
179 true);
184 /* Turn off the periodic schedule after a brief delay */
185 static void ehci_disable_PSE(struct ehci_hcd *ehci)
187 ehci_clear_command_bit(ehci, CMD_PSE);
191 /* Poll the STS_HALT status bit; see when a dead controller stops */
192 static void ehci_handle_controller_death(struct ehci_hcd *ehci)
194 if (!(ehci_readl(ehci, &ehci->regs->status) & STS_HALT)) {
196 /* Give up after a few milliseconds */
197 if (ehci->died_poll_count++ < 5) {
198 /* Try again later */
199 ehci_enable_event(ehci, EHCI_HRTIMER_POLL_DEAD, true);
200 return;
202 ehci_warn(ehci, "Waited too long for the controller to stop, giving up\n");
205 /* Clean up the mess */
206 ehci->rh_state = EHCI_RH_HALTED;
207 ehci_writel(ehci, 0, &ehci->regs->configured_flag);
208 ehci_writel(ehci, 0, &ehci->regs->intr_enable);
209 ehci_work(ehci);
210 end_unlink_async(ehci);
212 /* Not in process context, so don't try to reset the controller */
216 /* Handle unlinked interrupt QHs once they are gone from the hardware */
217 static void ehci_handle_intr_unlinks(struct ehci_hcd *ehci)
219 bool stopped = (ehci->rh_state < EHCI_RH_RUNNING);
222 * Process all the QHs on the intr_unlink list that were added
223 * before the current unlink cycle began. The list is in
224 * temporal order, so stop when we reach the first entry in the
225 * current cycle. But if the root hub isn't running then
226 * process all the QHs on the list.
228 ehci->intr_unlinking = true;
229 while (ehci->intr_unlink) {
230 struct ehci_qh *qh = ehci->intr_unlink;
232 if (!stopped && qh->unlink_cycle == ehci->intr_unlink_cycle)
233 break;
234 ehci->intr_unlink = qh->unlink_next;
235 qh->unlink_next = NULL;
236 end_unlink_intr(ehci, qh);
239 /* Handle remaining entries later */
240 if (ehci->intr_unlink) {
241 ehci_enable_event(ehci, EHCI_HRTIMER_UNLINK_INTR, true);
242 ++ehci->intr_unlink_cycle;
244 ehci->intr_unlinking = false;
248 /* Start another free-iTDs/siTDs cycle */
249 static void start_free_itds(struct ehci_hcd *ehci)
251 if (!(ehci->enabled_hrtimer_events & BIT(EHCI_HRTIMER_FREE_ITDS))) {
252 ehci->last_itd_to_free = list_entry(
253 ehci->cached_itd_list.prev,
254 struct ehci_itd, itd_list);
255 ehci->last_sitd_to_free = list_entry(
256 ehci->cached_sitd_list.prev,
257 struct ehci_sitd, sitd_list);
258 ehci_enable_event(ehci, EHCI_HRTIMER_FREE_ITDS, true);
262 /* Wait for controller to stop using old iTDs and siTDs */
263 static void end_free_itds(struct ehci_hcd *ehci)
265 struct ehci_itd *itd, *n;
266 struct ehci_sitd *sitd, *sn;
268 if (ehci->rh_state < EHCI_RH_RUNNING) {
269 ehci->last_itd_to_free = NULL;
270 ehci->last_sitd_to_free = NULL;
273 list_for_each_entry_safe(itd, n, &ehci->cached_itd_list, itd_list) {
274 list_del(&itd->itd_list);
275 dma_pool_free(ehci->itd_pool, itd, itd->itd_dma);
276 if (itd == ehci->last_itd_to_free)
277 break;
279 list_for_each_entry_safe(sitd, sn, &ehci->cached_sitd_list, sitd_list) {
280 list_del(&sitd->sitd_list);
281 dma_pool_free(ehci->sitd_pool, sitd, sitd->sitd_dma);
282 if (sitd == ehci->last_sitd_to_free)
283 break;
286 if (!list_empty(&ehci->cached_itd_list) ||
287 !list_empty(&ehci->cached_sitd_list))
288 start_free_itds(ehci);
292 /* Handle lost (or very late) IAA interrupts */
293 static void ehci_iaa_watchdog(struct ehci_hcd *ehci)
295 if (ehci->rh_state != EHCI_RH_RUNNING)
296 return;
299 * Lost IAA irqs wedge things badly; seen first with a vt8235.
300 * So we need this watchdog, but must protect it against both
301 * (a) SMP races against real IAA firing and retriggering, and
302 * (b) clean HC shutdown, when IAA watchdog was pending.
304 if (ehci->async_iaa) {
305 u32 cmd, status;
307 /* If we get here, IAA is *REALLY* late. It's barely
308 * conceivable that the system is so busy that CMD_IAAD
309 * is still legitimately set, so let's be sure it's
310 * clear before we read STS_IAA. (The HC should clear
311 * CMD_IAAD when it sets STS_IAA.)
313 cmd = ehci_readl(ehci, &ehci->regs->command);
316 * If IAA is set here it either legitimately triggered
317 * after the watchdog timer expired (_way_ late, so we'll
318 * still count it as lost) ... or a silicon erratum:
319 * - VIA seems to set IAA without triggering the IRQ;
320 * - IAAD potentially cleared without setting IAA.
322 status = ehci_readl(ehci, &ehci->regs->status);
323 if ((status & STS_IAA) || !(cmd & CMD_IAAD)) {
324 COUNT(ehci->stats.lost_iaa);
325 ehci_writel(ehci, STS_IAA, &ehci->regs->status);
328 ehci_vdbg(ehci, "IAA watchdog: status %x cmd %x\n",
329 status, cmd);
330 end_unlink_async(ehci);
336 * Handler functions for the hrtimer event types.
337 * Keep this array in the same order as the event types indexed by
338 * enum ehci_hrtimer_event in ehci.h.
340 static void (*event_handlers[])(struct ehci_hcd *) = {
341 ehci_poll_ASS, /* EHCI_HRTIMER_POLL_ASS */
342 ehci_poll_PSS, /* EHCI_HRTIMER_POLL_PSS */
343 ehci_handle_controller_death, /* EHCI_HRTIMER_POLL_DEAD */
344 ehci_handle_intr_unlinks, /* EHCI_HRTIMER_UNLINK_INTR */
345 end_free_itds, /* EHCI_HRTIMER_FREE_ITDS */
346 unlink_empty_async, /* EHCI_HRTIMER_ASYNC_UNLINKS */
347 ehci_iaa_watchdog, /* EHCI_HRTIMER_IAA_WATCHDOG */
348 ehci_disable_PSE, /* EHCI_HRTIMER_DISABLE_PERIODIC */
349 ehci_disable_ASE, /* EHCI_HRTIMER_DISABLE_ASYNC */
352 static enum hrtimer_restart ehci_hrtimer_func(struct hrtimer *t)
354 struct ehci_hcd *ehci = container_of(t, struct ehci_hcd, hrtimer);
355 ktime_t now;
356 unsigned long events;
357 unsigned long flags;
358 unsigned e;
360 spin_lock_irqsave(&ehci->lock, flags);
362 events = ehci->enabled_hrtimer_events;
363 ehci->enabled_hrtimer_events = 0;
364 ehci->next_hrtimer_event = EHCI_HRTIMER_NO_EVENT;
367 * Check each pending event. If its time has expired, handle
368 * the event; otherwise re-enable it.
370 now = ktime_get();
371 for_each_set_bit(e, &events, EHCI_HRTIMER_NUM_EVENTS) {
372 if (now.tv64 >= ehci->hr_timeouts[e].tv64)
373 event_handlers[e](ehci);
374 else
375 ehci_enable_event(ehci, e, false);
378 spin_unlock_irqrestore(&ehci->lock, flags);
379 return HRTIMER_NORESTART;