Alan Stern | d58b4bc | 2012-07-11 11:21:54 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 by Alan Stern |
| 3 | * |
| 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. |
| 8 | * |
| 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. |
| 13 | */ |
| 14 | |
| 15 | /* This file is part of ehci-hcd.c */ |
| 16 | |
| 17 | /*-------------------------------------------------------------------------*/ |
| 18 | |
Alan Stern | 3ca9aeb | 2012-07-11 11:22:05 -0400 | [diff] [blame] | 19 | /* Set a bit in the USBCMD register */ |
| 20 | static void ehci_set_command_bit(struct ehci_hcd *ehci, u32 bit) |
| 21 | { |
| 22 | ehci->command |= bit; |
| 23 | ehci_writel(ehci, ehci->command, &ehci->regs->command); |
| 24 | |
| 25 | /* unblock posted write */ |
| 26 | ehci_readl(ehci, &ehci->regs->command); |
| 27 | } |
| 28 | |
| 29 | /* Clear a bit in the USBCMD register */ |
| 30 | static void ehci_clear_command_bit(struct ehci_hcd *ehci, u32 bit) |
| 31 | { |
| 32 | ehci->command &= ~bit; |
| 33 | ehci_writel(ehci, ehci->command, &ehci->regs->command); |
| 34 | |
| 35 | /* unblock posted write */ |
| 36 | ehci_readl(ehci, &ehci->regs->command); |
| 37 | } |
| 38 | |
| 39 | /*-------------------------------------------------------------------------*/ |
| 40 | |
Alan Stern | d58b4bc | 2012-07-11 11:21:54 -0400 | [diff] [blame] | 41 | /* |
| 42 | * EHCI timer support... Now using hrtimers. |
| 43 | * |
| 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). |
| 50 | * |
| 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. |
| 55 | * |
| 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. |
| 62 | */ |
| 63 | |
| 64 | /* |
| 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. |
| 68 | */ |
| 69 | static unsigned event_delays_ns[] = { |
Alan Stern | 3144661 | 2012-07-11 11:22:21 -0400 | [diff] [blame] | 70 | 1 * NSEC_PER_MSEC, /* EHCI_HRTIMER_POLL_ASS */ |
Alan Stern | 3ca9aeb | 2012-07-11 11:22:05 -0400 | [diff] [blame] | 71 | 1 * NSEC_PER_MSEC, /* EHCI_HRTIMER_POLL_PSS */ |
Alan Stern | bf6387b | 2012-07-11 11:22:31 -0400 | [diff] [blame] | 72 | 1 * NSEC_PER_MSEC, /* EHCI_HRTIMER_POLL_DEAD */ |
Alan Stern | df20225 | 2012-07-11 11:22:26 -0400 | [diff] [blame] | 73 | 1125 * NSEC_PER_USEC, /* EHCI_HRTIMER_UNLINK_INTR */ |
Alan Stern | 55934eb | 2012-07-11 11:22:35 -0400 | [diff] [blame] | 74 | 2 * NSEC_PER_MSEC, /* EHCI_HRTIMER_FREE_ITDS */ |
Alan Stern | 87d6191 | 2016-01-25 15:45:25 -0500 | [diff] [blame] | 75 | 2 * NSEC_PER_MSEC, /* EHCI_HRTIMER_ACTIVE_UNLINK */ |
Ming Lei | 9118f9e | 2013-07-03 22:53:10 +0800 | [diff] [blame] | 76 | 5 * NSEC_PER_MSEC, /* EHCI_HRTIMER_START_UNLINK_INTR */ |
Alan Stern | 32830f2 | 2012-07-11 11:22:53 -0400 | [diff] [blame] | 77 | 6 * NSEC_PER_MSEC, /* EHCI_HRTIMER_ASYNC_UNLINKS */ |
Alan Stern | 9d93874 | 2012-07-11 11:22:44 -0400 | [diff] [blame] | 78 | 10 * NSEC_PER_MSEC, /* EHCI_HRTIMER_IAA_WATCHDOG */ |
Alan Stern | 3ca9aeb | 2012-07-11 11:22:05 -0400 | [diff] [blame] | 79 | 10 * NSEC_PER_MSEC, /* EHCI_HRTIMER_DISABLE_PERIODIC */ |
Alan Stern | 3144661 | 2012-07-11 11:22:21 -0400 | [diff] [blame] | 80 | 15 * NSEC_PER_MSEC, /* EHCI_HRTIMER_DISABLE_ASYNC */ |
Alan Stern | 18aafe6 | 2012-07-11 11:23:04 -0400 | [diff] [blame] | 81 | 100 * NSEC_PER_MSEC, /* EHCI_HRTIMER_IO_WATCHDOG */ |
Alan Stern | d58b4bc | 2012-07-11 11:21:54 -0400 | [diff] [blame] | 82 | }; |
| 83 | |
| 84 | /* Enable a pending hrtimer event */ |
| 85 | static void ehci_enable_event(struct ehci_hcd *ehci, unsigned event, |
| 86 | bool resched) |
| 87 | { |
| 88 | ktime_t *timeout = &ehci->hr_timeouts[event]; |
| 89 | |
| 90 | if (resched) |
| 91 | *timeout = ktime_add(ktime_get(), |
| 92 | ktime_set(0, event_delays_ns[event])); |
| 93 | ehci->enabled_hrtimer_events |= (1 << event); |
| 94 | |
| 95 | /* Track only the lowest-numbered pending event */ |
| 96 | if (event < ehci->next_hrtimer_event) { |
| 97 | ehci->next_hrtimer_event = event; |
| 98 | hrtimer_start_range_ns(&ehci->hrtimer, *timeout, |
| 99 | NSEC_PER_MSEC, HRTIMER_MODE_ABS); |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | |
Alan Stern | 3144661 | 2012-07-11 11:22:21 -0400 | [diff] [blame] | 104 | /* Poll the STS_ASS status bit; see when it agrees with CMD_ASE */ |
| 105 | static void ehci_poll_ASS(struct ehci_hcd *ehci) |
| 106 | { |
| 107 | unsigned actual, want; |
| 108 | |
| 109 | /* Don't enable anything if the controller isn't running (e.g., died) */ |
| 110 | if (ehci->rh_state != EHCI_RH_RUNNING) |
| 111 | return; |
| 112 | |
| 113 | want = (ehci->command & CMD_ASE) ? STS_ASS : 0; |
| 114 | actual = ehci_readl(ehci, &ehci->regs->status) & STS_ASS; |
| 115 | |
| 116 | if (want != actual) { |
| 117 | |
Alan Stern | 6d5df89 | 2013-03-18 12:04:54 -0400 | [diff] [blame] | 118 | /* Poll again later, but give up after about 2-4 ms */ |
| 119 | if (ehci->ASS_poll_count++ < 2) { |
Alan Stern | 221f8df | 2013-02-26 13:43:41 -0500 | [diff] [blame] | 120 | ehci_enable_event(ehci, EHCI_HRTIMER_POLL_ASS, true); |
| 121 | return; |
| 122 | } |
| 123 | ehci_dbg(ehci, "Waited too long for the async schedule status (%x/%x), giving up\n", |
| 124 | want, actual); |
Alan Stern | 3144661 | 2012-07-11 11:22:21 -0400 | [diff] [blame] | 125 | } |
| 126 | ehci->ASS_poll_count = 0; |
| 127 | |
| 128 | /* The status is up-to-date; restart or stop the schedule as needed */ |
| 129 | if (want == 0) { /* Stopped */ |
| 130 | if (ehci->async_count > 0) |
| 131 | ehci_set_command_bit(ehci, CMD_ASE); |
| 132 | |
| 133 | } else { /* Running */ |
| 134 | if (ehci->async_count == 0) { |
| 135 | |
| 136 | /* Turn off the schedule after a while */ |
| 137 | ehci_enable_event(ehci, EHCI_HRTIMER_DISABLE_ASYNC, |
| 138 | true); |
| 139 | } |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | /* Turn off the async schedule after a brief delay */ |
| 144 | static void ehci_disable_ASE(struct ehci_hcd *ehci) |
| 145 | { |
| 146 | ehci_clear_command_bit(ehci, CMD_ASE); |
| 147 | } |
| 148 | |
| 149 | |
Alan Stern | 3ca9aeb | 2012-07-11 11:22:05 -0400 | [diff] [blame] | 150 | /* Poll the STS_PSS status bit; see when it agrees with CMD_PSE */ |
| 151 | static void ehci_poll_PSS(struct ehci_hcd *ehci) |
| 152 | { |
| 153 | unsigned actual, want; |
| 154 | |
| 155 | /* Don't do anything if the controller isn't running (e.g., died) */ |
| 156 | if (ehci->rh_state != EHCI_RH_RUNNING) |
| 157 | return; |
| 158 | |
| 159 | want = (ehci->command & CMD_PSE) ? STS_PSS : 0; |
| 160 | actual = ehci_readl(ehci, &ehci->regs->status) & STS_PSS; |
| 161 | |
| 162 | if (want != actual) { |
| 163 | |
Alan Stern | 6d5df89 | 2013-03-18 12:04:54 -0400 | [diff] [blame] | 164 | /* Poll again later, but give up after about 2-4 ms */ |
| 165 | if (ehci->PSS_poll_count++ < 2) { |
Alan Stern | 221f8df | 2013-02-26 13:43:41 -0500 | [diff] [blame] | 166 | ehci_enable_event(ehci, EHCI_HRTIMER_POLL_PSS, true); |
| 167 | return; |
| 168 | } |
| 169 | ehci_dbg(ehci, "Waited too long for the periodic schedule status (%x/%x), giving up\n", |
| 170 | want, actual); |
Alan Stern | 3ca9aeb | 2012-07-11 11:22:05 -0400 | [diff] [blame] | 171 | } |
| 172 | ehci->PSS_poll_count = 0; |
| 173 | |
| 174 | /* The status is up-to-date; restart or stop the schedule as needed */ |
| 175 | if (want == 0) { /* Stopped */ |
Alan Stern | 569b394 | 2012-07-11 11:23:00 -0400 | [diff] [blame] | 176 | if (ehci->periodic_count > 0) |
Alan Stern | 3ca9aeb | 2012-07-11 11:22:05 -0400 | [diff] [blame] | 177 | ehci_set_command_bit(ehci, CMD_PSE); |
Alan Stern | 3ca9aeb | 2012-07-11 11:22:05 -0400 | [diff] [blame] | 178 | |
| 179 | } else { /* Running */ |
| 180 | if (ehci->periodic_count == 0) { |
| 181 | |
| 182 | /* Turn off the schedule after a while */ |
| 183 | ehci_enable_event(ehci, EHCI_HRTIMER_DISABLE_PERIODIC, |
| 184 | true); |
| 185 | } |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | /* Turn off the periodic schedule after a brief delay */ |
| 190 | static void ehci_disable_PSE(struct ehci_hcd *ehci) |
| 191 | { |
| 192 | ehci_clear_command_bit(ehci, CMD_PSE); |
Alan Stern | 3ca9aeb | 2012-07-11 11:22:05 -0400 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | |
Alan Stern | bf6387b | 2012-07-11 11:22:31 -0400 | [diff] [blame] | 196 | /* Poll the STS_HALT status bit; see when a dead controller stops */ |
| 197 | static void ehci_handle_controller_death(struct ehci_hcd *ehci) |
| 198 | { |
| 199 | if (!(ehci_readl(ehci, &ehci->regs->status) & STS_HALT)) { |
| 200 | |
| 201 | /* Give up after a few milliseconds */ |
| 202 | if (ehci->died_poll_count++ < 5) { |
| 203 | /* Try again later */ |
| 204 | ehci_enable_event(ehci, EHCI_HRTIMER_POLL_DEAD, true); |
| 205 | return; |
| 206 | } |
| 207 | ehci_warn(ehci, "Waited too long for the controller to stop, giving up\n"); |
| 208 | } |
| 209 | |
| 210 | /* Clean up the mess */ |
| 211 | ehci->rh_state = EHCI_RH_HALTED; |
| 212 | ehci_writel(ehci, 0, &ehci->regs->configured_flag); |
| 213 | ehci_writel(ehci, 0, &ehci->regs->intr_enable); |
| 214 | ehci_work(ehci); |
Alan Stern | 3c273a0 | 2012-07-11 11:22:49 -0400 | [diff] [blame] | 215 | end_unlink_async(ehci); |
Alan Stern | bf6387b | 2012-07-11 11:22:31 -0400 | [diff] [blame] | 216 | |
| 217 | /* Not in process context, so don't try to reset the controller */ |
| 218 | } |
| 219 | |
Ming Lei | 9118f9e | 2013-07-03 22:53:10 +0800 | [diff] [blame] | 220 | /* start to unlink interrupt QHs */ |
| 221 | static void ehci_handle_start_intr_unlinks(struct ehci_hcd *ehci) |
| 222 | { |
| 223 | bool stopped = (ehci->rh_state < EHCI_RH_RUNNING); |
| 224 | |
| 225 | /* |
| 226 | * Process all the QHs on the intr_unlink list that were added |
| 227 | * before the current unlink cycle began. The list is in |
| 228 | * temporal order, so stop when we reach the first entry in the |
| 229 | * current cycle. But if the root hub isn't running then |
| 230 | * process all the QHs on the list. |
| 231 | */ |
| 232 | while (!list_empty(&ehci->intr_unlink_wait)) { |
| 233 | struct ehci_qh *qh; |
| 234 | |
| 235 | qh = list_first_entry(&ehci->intr_unlink_wait, |
| 236 | struct ehci_qh, unlink_node); |
| 237 | if (!stopped && (qh->unlink_cycle == |
| 238 | ehci->intr_unlink_wait_cycle)) |
| 239 | break; |
| 240 | list_del_init(&qh->unlink_node); |
Alan Stern | fcc5184 | 2016-01-25 15:42:04 -0500 | [diff] [blame] | 241 | qh->unlink_reason |= QH_UNLINK_QUEUE_EMPTY; |
Ming Lei | 9118f9e | 2013-07-03 22:53:10 +0800 | [diff] [blame] | 242 | start_unlink_intr(ehci, qh); |
| 243 | } |
| 244 | |
| 245 | /* Handle remaining entries later */ |
| 246 | if (!list_empty(&ehci->intr_unlink_wait)) { |
| 247 | ehci_enable_event(ehci, EHCI_HRTIMER_START_UNLINK_INTR, true); |
| 248 | ++ehci->intr_unlink_wait_cycle; |
| 249 | } |
| 250 | } |
Alan Stern | bf6387b | 2012-07-11 11:22:31 -0400 | [diff] [blame] | 251 | |
Alan Stern | df20225 | 2012-07-11 11:22:26 -0400 | [diff] [blame] | 252 | /* Handle unlinked interrupt QHs once they are gone from the hardware */ |
| 253 | static void ehci_handle_intr_unlinks(struct ehci_hcd *ehci) |
| 254 | { |
| 255 | bool stopped = (ehci->rh_state < EHCI_RH_RUNNING); |
| 256 | |
| 257 | /* |
| 258 | * Process all the QHs on the intr_unlink list that were added |
| 259 | * before the current unlink cycle began. The list is in |
| 260 | * temporal order, so stop when we reach the first entry in the |
| 261 | * current cycle. But if the root hub isn't running then |
| 262 | * process all the QHs on the list. |
| 263 | */ |
| 264 | ehci->intr_unlinking = true; |
Alan Stern | 6e01875 | 2013-03-22 13:31:45 -0400 | [diff] [blame] | 265 | while (!list_empty(&ehci->intr_unlink)) { |
| 266 | struct ehci_qh *qh; |
Alan Stern | df20225 | 2012-07-11 11:22:26 -0400 | [diff] [blame] | 267 | |
Alan Stern | 6e01875 | 2013-03-22 13:31:45 -0400 | [diff] [blame] | 268 | qh = list_first_entry(&ehci->intr_unlink, struct ehci_qh, |
| 269 | unlink_node); |
Alan Stern | df20225 | 2012-07-11 11:22:26 -0400 | [diff] [blame] | 270 | if (!stopped && qh->unlink_cycle == ehci->intr_unlink_cycle) |
| 271 | break; |
Ming Lei | 9118f9e | 2013-07-03 22:53:10 +0800 | [diff] [blame] | 272 | list_del_init(&qh->unlink_node); |
Alan Stern | df20225 | 2012-07-11 11:22:26 -0400 | [diff] [blame] | 273 | end_unlink_intr(ehci, qh); |
| 274 | } |
| 275 | |
| 276 | /* Handle remaining entries later */ |
Alan Stern | 6e01875 | 2013-03-22 13:31:45 -0400 | [diff] [blame] | 277 | if (!list_empty(&ehci->intr_unlink)) { |
Alan Stern | df20225 | 2012-07-11 11:22:26 -0400 | [diff] [blame] | 278 | ehci_enable_event(ehci, EHCI_HRTIMER_UNLINK_INTR, true); |
| 279 | ++ehci->intr_unlink_cycle; |
| 280 | } |
| 281 | ehci->intr_unlinking = false; |
| 282 | } |
| 283 | |
| 284 | |
Alan Stern | 55934eb | 2012-07-11 11:22:35 -0400 | [diff] [blame] | 285 | /* Start another free-iTDs/siTDs cycle */ |
| 286 | static void start_free_itds(struct ehci_hcd *ehci) |
| 287 | { |
| 288 | if (!(ehci->enabled_hrtimer_events & BIT(EHCI_HRTIMER_FREE_ITDS))) { |
| 289 | ehci->last_itd_to_free = list_entry( |
| 290 | ehci->cached_itd_list.prev, |
| 291 | struct ehci_itd, itd_list); |
| 292 | ehci->last_sitd_to_free = list_entry( |
| 293 | ehci->cached_sitd_list.prev, |
| 294 | struct ehci_sitd, sitd_list); |
| 295 | ehci_enable_event(ehci, EHCI_HRTIMER_FREE_ITDS, true); |
| 296 | } |
| 297 | } |
| 298 | |
| 299 | /* Wait for controller to stop using old iTDs and siTDs */ |
| 300 | static void end_free_itds(struct ehci_hcd *ehci) |
| 301 | { |
| 302 | struct ehci_itd *itd, *n; |
| 303 | struct ehci_sitd *sitd, *sn; |
| 304 | |
| 305 | if (ehci->rh_state < EHCI_RH_RUNNING) { |
| 306 | ehci->last_itd_to_free = NULL; |
| 307 | ehci->last_sitd_to_free = NULL; |
| 308 | } |
| 309 | |
| 310 | list_for_each_entry_safe(itd, n, &ehci->cached_itd_list, itd_list) { |
| 311 | list_del(&itd->itd_list); |
| 312 | dma_pool_free(ehci->itd_pool, itd, itd->itd_dma); |
| 313 | if (itd == ehci->last_itd_to_free) |
| 314 | break; |
| 315 | } |
| 316 | list_for_each_entry_safe(sitd, sn, &ehci->cached_sitd_list, sitd_list) { |
| 317 | list_del(&sitd->sitd_list); |
| 318 | dma_pool_free(ehci->sitd_pool, sitd, sitd->sitd_dma); |
| 319 | if (sitd == ehci->last_sitd_to_free) |
| 320 | break; |
| 321 | } |
| 322 | |
| 323 | if (!list_empty(&ehci->cached_itd_list) || |
| 324 | !list_empty(&ehci->cached_sitd_list)) |
| 325 | start_free_itds(ehci); |
| 326 | } |
| 327 | |
| 328 | |
Alan Stern | 9d93874 | 2012-07-11 11:22:44 -0400 | [diff] [blame] | 329 | /* Handle lost (or very late) IAA interrupts */ |
| 330 | static void ehci_iaa_watchdog(struct ehci_hcd *ehci) |
| 331 | { |
Alan Stern | 60fd4aa | 2013-03-18 12:05:19 -0400 | [diff] [blame] | 332 | u32 cmd, status; |
Alan Stern | 9d93874 | 2012-07-11 11:22:44 -0400 | [diff] [blame] | 333 | |
Alan Stern | 417c765 | 2013-03-21 12:48:42 -0400 | [diff] [blame] | 334 | /* |
| 335 | * Lost IAA irqs wedge things badly; seen first with a vt8235. |
| 336 | * So we need this watchdog, but must protect it against both |
| 337 | * (a) SMP races against real IAA firing and retriggering, and |
| 338 | * (b) clean HC shutdown, when IAA watchdog was pending. |
| 339 | */ |
Alan Stern | 214ac7a | 2013-03-22 13:31:58 -0400 | [diff] [blame] | 340 | if (!ehci->iaa_in_progress || ehci->rh_state != EHCI_RH_RUNNING) |
Alan Stern | 417c765 | 2013-03-21 12:48:42 -0400 | [diff] [blame] | 341 | return; |
| 342 | |
Alan Stern | 60fd4aa | 2013-03-18 12:05:19 -0400 | [diff] [blame] | 343 | /* If we get here, IAA is *REALLY* late. It's barely |
| 344 | * conceivable that the system is so busy that CMD_IAAD |
| 345 | * is still legitimately set, so let's be sure it's |
| 346 | * clear before we read STS_IAA. (The HC should clear |
| 347 | * CMD_IAAD when it sets STS_IAA.) |
| 348 | */ |
| 349 | cmd = ehci_readl(ehci, &ehci->regs->command); |
Alan Stern | 9d93874 | 2012-07-11 11:22:44 -0400 | [diff] [blame] | 350 | |
Alan Stern | 60fd4aa | 2013-03-18 12:05:19 -0400 | [diff] [blame] | 351 | /* |
| 352 | * If IAA is set here it either legitimately triggered |
| 353 | * after the watchdog timer expired (_way_ late, so we'll |
| 354 | * still count it as lost) ... or a silicon erratum: |
| 355 | * - VIA seems to set IAA without triggering the IRQ; |
| 356 | * - IAAD potentially cleared without setting IAA. |
| 357 | */ |
| 358 | status = ehci_readl(ehci, &ehci->regs->status); |
| 359 | if ((status & STS_IAA) || !(cmd & CMD_IAAD)) { |
| 360 | COUNT(ehci->stats.lost_iaa); |
| 361 | ehci_writel(ehci, STS_IAA, &ehci->regs->status); |
Alan Stern | 9d93874 | 2012-07-11 11:22:44 -0400 | [diff] [blame] | 362 | } |
Alan Stern | 60fd4aa | 2013-03-18 12:05:19 -0400 | [diff] [blame] | 363 | |
| 364 | ehci_dbg(ehci, "IAA watchdog: status %x cmd %x\n", status, cmd); |
Alan Stern | f96fba0 | 2016-01-25 15:44:16 -0500 | [diff] [blame] | 365 | end_iaa_cycle(ehci); |
Alan Stern | 9d93874 | 2012-07-11 11:22:44 -0400 | [diff] [blame] | 366 | } |
| 367 | |
| 368 | |
Alan Stern | 18aafe6 | 2012-07-11 11:23:04 -0400 | [diff] [blame] | 369 | /* Enable the I/O watchdog, if appropriate */ |
| 370 | static void turn_on_io_watchdog(struct ehci_hcd *ehci) |
| 371 | { |
| 372 | /* Not needed if the controller isn't running or it's already enabled */ |
| 373 | if (ehci->rh_state != EHCI_RH_RUNNING || |
| 374 | (ehci->enabled_hrtimer_events & |
| 375 | BIT(EHCI_HRTIMER_IO_WATCHDOG))) |
| 376 | return; |
| 377 | |
| 378 | /* |
| 379 | * Isochronous transfers always need the watchdog. |
| 380 | * For other sorts we use it only if the flag is set. |
| 381 | */ |
| 382 | if (ehci->isoc_count > 0 || (ehci->need_io_watchdog && |
| 383 | ehci->async_count + ehci->intr_count > 0)) |
| 384 | ehci_enable_event(ehci, EHCI_HRTIMER_IO_WATCHDOG, true); |
| 385 | } |
| 386 | |
| 387 | |
Alan Stern | d58b4bc | 2012-07-11 11:21:54 -0400 | [diff] [blame] | 388 | /* |
| 389 | * Handler functions for the hrtimer event types. |
| 390 | * Keep this array in the same order as the event types indexed by |
| 391 | * enum ehci_hrtimer_event in ehci.h. |
| 392 | */ |
| 393 | static void (*event_handlers[])(struct ehci_hcd *) = { |
Alan Stern | 3144661 | 2012-07-11 11:22:21 -0400 | [diff] [blame] | 394 | ehci_poll_ASS, /* EHCI_HRTIMER_POLL_ASS */ |
Alan Stern | 3ca9aeb | 2012-07-11 11:22:05 -0400 | [diff] [blame] | 395 | ehci_poll_PSS, /* EHCI_HRTIMER_POLL_PSS */ |
Alan Stern | bf6387b | 2012-07-11 11:22:31 -0400 | [diff] [blame] | 396 | ehci_handle_controller_death, /* EHCI_HRTIMER_POLL_DEAD */ |
Alan Stern | df20225 | 2012-07-11 11:22:26 -0400 | [diff] [blame] | 397 | ehci_handle_intr_unlinks, /* EHCI_HRTIMER_UNLINK_INTR */ |
Alan Stern | 55934eb | 2012-07-11 11:22:35 -0400 | [diff] [blame] | 398 | end_free_itds, /* EHCI_HRTIMER_FREE_ITDS */ |
Alan Stern | 87d6191 | 2016-01-25 15:45:25 -0500 | [diff] [blame] | 399 | end_unlink_async, /* EHCI_HRTIMER_ACTIVE_UNLINK */ |
Ming Lei | 9118f9e | 2013-07-03 22:53:10 +0800 | [diff] [blame] | 400 | ehci_handle_start_intr_unlinks, /* EHCI_HRTIMER_START_UNLINK_INTR */ |
Alan Stern | 32830f2 | 2012-07-11 11:22:53 -0400 | [diff] [blame] | 401 | unlink_empty_async, /* EHCI_HRTIMER_ASYNC_UNLINKS */ |
Alan Stern | 9d93874 | 2012-07-11 11:22:44 -0400 | [diff] [blame] | 402 | ehci_iaa_watchdog, /* EHCI_HRTIMER_IAA_WATCHDOG */ |
Alan Stern | 3ca9aeb | 2012-07-11 11:22:05 -0400 | [diff] [blame] | 403 | ehci_disable_PSE, /* EHCI_HRTIMER_DISABLE_PERIODIC */ |
Alan Stern | 3144661 | 2012-07-11 11:22:21 -0400 | [diff] [blame] | 404 | ehci_disable_ASE, /* EHCI_HRTIMER_DISABLE_ASYNC */ |
Alan Stern | 18aafe6 | 2012-07-11 11:23:04 -0400 | [diff] [blame] | 405 | ehci_work, /* EHCI_HRTIMER_IO_WATCHDOG */ |
Alan Stern | d58b4bc | 2012-07-11 11:21:54 -0400 | [diff] [blame] | 406 | }; |
| 407 | |
| 408 | static enum hrtimer_restart ehci_hrtimer_func(struct hrtimer *t) |
| 409 | { |
| 410 | struct ehci_hcd *ehci = container_of(t, struct ehci_hcd, hrtimer); |
| 411 | ktime_t now; |
| 412 | unsigned long events; |
| 413 | unsigned long flags; |
| 414 | unsigned e; |
| 415 | |
| 416 | spin_lock_irqsave(&ehci->lock, flags); |
| 417 | |
| 418 | events = ehci->enabled_hrtimer_events; |
| 419 | ehci->enabled_hrtimer_events = 0; |
| 420 | ehci->next_hrtimer_event = EHCI_HRTIMER_NO_EVENT; |
| 421 | |
| 422 | /* |
| 423 | * Check each pending event. If its time has expired, handle |
| 424 | * the event; otherwise re-enable it. |
| 425 | */ |
| 426 | now = ktime_get(); |
| 427 | for_each_set_bit(e, &events, EHCI_HRTIMER_NUM_EVENTS) { |
| 428 | if (now.tv64 >= ehci->hr_timeouts[e].tv64) |
| 429 | event_handlers[e](ehci); |
| 430 | else |
| 431 | ehci_enable_event(ehci, e, false); |
| 432 | } |
| 433 | |
| 434 | spin_unlock_irqrestore(&ehci->lock, flags); |
| 435 | return HRTIMER_NORESTART; |
| 436 | } |