Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

Merge tag 'mmc-v4.14-rc4-3' of git://git.kernel.org/pub/scm/linux/kernel/git/ulfh/mmc

Pull MMC fixes from Ulf Hansson:
"Fix dw_mmc request timeout issues"

* tag 'mmc-v4.14-rc4-3' of git://git.kernel.org/pub/scm/linux/kernel/git/ulfh/mmc:
mmc: dw_mmc: Fix the DTO timeout calculation
mmc: dw_mmc: Add locking to the CTO timer
mmc: dw_mmc: Fix the CTO timeout calculation
mmc: dw_mmc: cancel the CTO timer after a voltage switch

+94 -13
+94 -13
drivers/mmc/host/dw_mmc.c
··· 401 401 static inline void dw_mci_set_cto(struct dw_mci *host) 402 402 { 403 403 unsigned int cto_clks; 404 + unsigned int cto_div; 404 405 unsigned int cto_ms; 406 + unsigned long irqflags; 405 407 406 408 cto_clks = mci_readl(host, TMOUT) & 0xff; 407 - cto_ms = DIV_ROUND_UP(cto_clks, host->bus_hz / 1000); 409 + cto_div = (mci_readl(host, CLKDIV) & 0xff) * 2; 410 + if (cto_div == 0) 411 + cto_div = 1; 412 + cto_ms = DIV_ROUND_UP(MSEC_PER_SEC * cto_clks * cto_div, host->bus_hz); 408 413 409 414 /* add a bit spare time */ 410 415 cto_ms += 10; 411 416 412 - mod_timer(&host->cto_timer, 413 - jiffies + msecs_to_jiffies(cto_ms) + 1); 417 + /* 418 + * The durations we're working with are fairly short so we have to be 419 + * extra careful about synchronization here. Specifically in hardware a 420 + * command timeout is _at most_ 5.1 ms, so that means we expect an 421 + * interrupt (either command done or timeout) to come rather quickly 422 + * after the mci_writel. ...but just in case we have a long interrupt 423 + * latency let's add a bit of paranoia. 424 + * 425 + * In general we'll assume that at least an interrupt will be asserted 426 + * in hardware by the time the cto_timer runs. ...and if it hasn't 427 + * been asserted in hardware by that time then we'll assume it'll never 428 + * come. 429 + */ 430 + spin_lock_irqsave(&host->irq_lock, irqflags); 431 + if (!test_bit(EVENT_CMD_COMPLETE, &host->pending_events)) 432 + mod_timer(&host->cto_timer, 433 + jiffies + msecs_to_jiffies(cto_ms) + 1); 434 + spin_unlock_irqrestore(&host->irq_lock, irqflags); 414 435 } 415 436 416 437 static void dw_mci_start_command(struct dw_mci *host, ··· 446 425 wmb(); /* drain writebuffer */ 447 426 dw_mci_wait_while_busy(host, cmd_flags); 448 427 428 + mci_writel(host, CMD, cmd_flags | SDMMC_CMD_START); 429 + 449 430 /* response expected command only */ 450 431 if (cmd_flags & SDMMC_CMD_RESP_EXP) 451 432 dw_mci_set_cto(host); 452 - 453 - mci_writel(host, CMD, cmd_flags | SDMMC_CMD_START); 454 433 } 455 434 456 435 static inline void send_stop_abort(struct dw_mci *host, struct mmc_data *data) ··· 1936 1915 static void dw_mci_set_drto(struct dw_mci *host) 1937 1916 { 1938 1917 unsigned int drto_clks; 1918 + unsigned int drto_div; 1939 1919 unsigned int drto_ms; 1940 1920 1941 1921 drto_clks = mci_readl(host, TMOUT) >> 8; 1942 - drto_ms = DIV_ROUND_UP(drto_clks, host->bus_hz / 1000); 1922 + drto_div = (mci_readl(host, CLKDIV) & 0xff) * 2; 1923 + if (drto_div == 0) 1924 + drto_div = 1; 1925 + drto_ms = DIV_ROUND_UP(MSEC_PER_SEC * drto_clks * drto_div, 1926 + host->bus_hz); 1943 1927 1944 1928 /* add a bit spare time */ 1945 1929 drto_ms += 10; 1946 1930 1947 1931 mod_timer(&host->dto_timer, jiffies + msecs_to_jiffies(drto_ms)); 1932 + } 1933 + 1934 + static bool dw_mci_clear_pending_cmd_complete(struct dw_mci *host) 1935 + { 1936 + if (!test_bit(EVENT_CMD_COMPLETE, &host->pending_events)) 1937 + return false; 1938 + 1939 + /* 1940 + * Really be certain that the timer has stopped. This is a bit of 1941 + * paranoia and could only really happen if we had really bad 1942 + * interrupt latency and the interrupt routine and timeout were 1943 + * running concurrently so that the del_timer() in the interrupt 1944 + * handler couldn't run. 1945 + */ 1946 + WARN_ON(del_timer_sync(&host->cto_timer)); 1947 + clear_bit(EVENT_CMD_COMPLETE, &host->pending_events); 1948 + 1949 + return true; 1948 1950 } 1949 1951 1950 1952 static void dw_mci_tasklet_func(unsigned long priv) ··· 1996 1952 1997 1953 case STATE_SENDING_CMD11: 1998 1954 case STATE_SENDING_CMD: 1999 - if (!test_and_clear_bit(EVENT_CMD_COMPLETE, 2000 - &host->pending_events)) 1955 + if (!dw_mci_clear_pending_cmd_complete(host)) 2001 1956 break; 2002 1957 2003 1958 cmd = host->cmd; ··· 2165 2122 /* fall through */ 2166 2123 2167 2124 case STATE_SENDING_STOP: 2168 - if (!test_and_clear_bit(EVENT_CMD_COMPLETE, 2169 - &host->pending_events)) 2125 + if (!dw_mci_clear_pending_cmd_complete(host)) 2170 2126 break; 2171 2127 2172 2128 /* CMD error in data command */ ··· 2612 2570 2613 2571 static void dw_mci_cmd_interrupt(struct dw_mci *host, u32 status) 2614 2572 { 2573 + del_timer(&host->cto_timer); 2574 + 2615 2575 if (!host->cmd_status) 2616 2576 host->cmd_status = status; 2617 2577 ··· 2638 2594 struct dw_mci *host = dev_id; 2639 2595 u32 pending; 2640 2596 struct dw_mci_slot *slot = host->slot; 2597 + unsigned long irqflags; 2641 2598 2642 2599 pending = mci_readl(host, MINTSTS); /* read-only mask reg */ 2643 2600 ··· 2646 2601 /* Check volt switch first, since it can look like an error */ 2647 2602 if ((host->state == STATE_SENDING_CMD11) && 2648 2603 (pending & SDMMC_INT_VOLT_SWITCH)) { 2649 - unsigned long irqflags; 2650 - 2651 2604 mci_writel(host, RINTSTS, SDMMC_INT_VOLT_SWITCH); 2652 2605 pending &= ~SDMMC_INT_VOLT_SWITCH; 2653 2606 ··· 2661 2618 } 2662 2619 2663 2620 if (pending & DW_MCI_CMD_ERROR_FLAGS) { 2621 + spin_lock_irqsave(&host->irq_lock, irqflags); 2622 + 2664 2623 del_timer(&host->cto_timer); 2665 2624 mci_writel(host, RINTSTS, DW_MCI_CMD_ERROR_FLAGS); 2666 2625 host->cmd_status = pending; 2667 2626 smp_wmb(); /* drain writebuffer */ 2668 2627 set_bit(EVENT_CMD_COMPLETE, &host->pending_events); 2628 + 2629 + spin_unlock_irqrestore(&host->irq_lock, irqflags); 2669 2630 } 2670 2631 2671 2632 if (pending & DW_MCI_DATA_ERROR_FLAGS) { ··· 2709 2662 } 2710 2663 2711 2664 if (pending & SDMMC_INT_CMD_DONE) { 2712 - del_timer(&host->cto_timer); 2665 + spin_lock_irqsave(&host->irq_lock, irqflags); 2666 + 2713 2667 mci_writel(host, RINTSTS, SDMMC_INT_CMD_DONE); 2714 2668 dw_mci_cmd_interrupt(host, pending); 2669 + 2670 + spin_unlock_irqrestore(&host->irq_lock, irqflags); 2715 2671 } 2716 2672 2717 2673 if (pending & SDMMC_INT_CD) { ··· 2988 2938 static void dw_mci_cto_timer(unsigned long arg) 2989 2939 { 2990 2940 struct dw_mci *host = (struct dw_mci *)arg; 2941 + unsigned long irqflags; 2942 + u32 pending; 2991 2943 2944 + spin_lock_irqsave(&host->irq_lock, irqflags); 2945 + 2946 + /* 2947 + * If somehow we have very bad interrupt latency it's remotely possible 2948 + * that the timer could fire while the interrupt is still pending or 2949 + * while the interrupt is midway through running. Let's be paranoid 2950 + * and detect those two cases. Note that this is paranoia is somewhat 2951 + * justified because in this function we don't actually cancel the 2952 + * pending command in the controller--we just assume it will never come. 2953 + */ 2954 + pending = mci_readl(host, MINTSTS); /* read-only mask reg */ 2955 + if (pending & (DW_MCI_CMD_ERROR_FLAGS | SDMMC_INT_CMD_DONE)) { 2956 + /* The interrupt should fire; no need to act but we can warn */ 2957 + dev_warn(host->dev, "Unexpected interrupt latency\n"); 2958 + goto exit; 2959 + } 2960 + if (test_bit(EVENT_CMD_COMPLETE, &host->pending_events)) { 2961 + /* Presumably interrupt handler couldn't delete the timer */ 2962 + dev_warn(host->dev, "CTO timeout when already completed\n"); 2963 + goto exit; 2964 + } 2965 + 2966 + /* 2967 + * Continued paranoia to make sure we're in the state we expect. 2968 + * This paranoia isn't really justified but it seems good to be safe. 2969 + */ 2992 2970 switch (host->state) { 2993 2971 case STATE_SENDING_CMD11: 2994 2972 case STATE_SENDING_CMD: ··· 3035 2957 host->state); 3036 2958 break; 3037 2959 } 2960 + 2961 + exit: 2962 + spin_unlock_irqrestore(&host->irq_lock, irqflags); 3038 2963 } 3039 2964 3040 2965 static void dw_mci_dto_timer(unsigned long arg)