From 1654efc31339972d0e6bd41a499fcffc0a45822e Mon Sep 17 00:00:00 2001 From: Michael Sevakis Date: Wed, 15 Mar 2017 01:51:54 -0400 Subject: Unify storage threads into one * Editing a bunch of drivers' thread routines in order to implement a new feature is tedious. * No matter the number of storage drivers, they share one thread. No extra threads needed for CONFIG_STORAGE_MULTI. * Each has an event callback called by the storage thread. * A default callback is provided to fake sleeping in order to trigger idle callbacks. It could also do other default processing. Changes to it will be part of driver code without editing each one. * Drivers may sleep and wake as they please as long as they give a low pulse on their storage bit to ask to go into sleep mode. Idle callback is called on its behalf and driver immediately put into sleep mode. * Drivers may indicate they are to continue receiving events in USB mode, otherwise they receve nothing until disconnect (they do receive SYS_USB_DISCONNECTED no matter what). * Rework a few things to keep the callback implementation sane and maintainable. ata.c was dreadful with all those bools; make it a state machine and easier to follow. Remove last_user_activity; it has no purpose that isn't served by keeping the disk active through last_disk_activity instead. * Even-out stack sizes partly because of a lack of a decent place to define them by driver or SoC or whatever; it doesn't seem too critical to do that anyway. Many are simply too large while at least one isn't really adequate. They may be individually overridden if necessary (figure out where). The thread uses the greatest size demanded. Newer file code is much more frugal with stack space. I barely see use crack 50% after idle callbacks (usually mid-40s). Card insert/eject doesn't demand much. * No forcing of idle callbacks. If it isn't necessary for one or more non-disk storage types, it really isn't any more necessary for disk storage. Besides, it makes the whole thing easier to implement. Change-Id: Id30c284d82a8af66e47f2cfe104c52cbd8aa7215 --- .../target/arm/s5l8702/ipod6g/storage_ata-6g.c | 69 +++++++++++----------- 1 file changed, 35 insertions(+), 34 deletions(-) (limited to 'firmware/target/arm/s5l8702/ipod6g/storage_ata-6g.c') diff --git a/firmware/target/arm/s5l8702/ipod6g/storage_ata-6g.c b/firmware/target/arm/s5l8702/ipod6g/storage_ata-6g.c index ef39a5cabb..36d119aff3 100644 --- a/firmware/target/arm/s5l8702/ipod6g/storage_ata-6g.c +++ b/firmware/target/arm/s5l8702/ipod6g/storage_ata-6g.c @@ -19,8 +19,6 @@ * ****************************************************************************/ #include "config.h" -#include "thread.h" -#include "disk.h" #include "storage.h" #include "timer.h" #include "kernel.h" @@ -31,8 +29,6 @@ #include "mmcdefs-target.h" #include "s5l8702.h" #include "led.h" -#include "ata_idle_notify.h" -#include "disk_cache.h" #ifndef ATA_RETRIES @@ -58,7 +54,6 @@ static struct semaphore ata_wakeup; static uint32_t ata_dma_flags; static long ata_last_activity_value = -1; static long ata_sleep_timeout = 20 * HZ; -static uint32_t ata_stack[(DEFAULT_STACK_SIZE + 0x400) / 4]; static bool ata_powered; static const int ata_retries = ATA_RETRIES; static const bool ata_error_srst = true; @@ -889,21 +884,6 @@ static int ata_rw_sectors(uint64_t sector, uint32_t count, void* buffer, bool wr return 0; } -static void ata_thread(void) -{ - while (true) - { - mutex_lock(&ata_mutex); - if (TIME_AFTER(current_tick, ata_last_activity_value + ata_sleep_timeout) && ata_powered) - { - call_storage_idle_notifys(false); - ata_power_down(); - } - mutex_unlock(&ata_mutex); - sleep(HZ / 2); - } -} - /* API Functions */ int ata_soft_reset(void) { @@ -982,11 +962,6 @@ void ata_spindown(int seconds) ata_sleep_timeout = seconds * HZ; } -void ata_sleep(void) -{ - ata_last_activity_value = current_tick - ata_sleep_timeout + HZ / 5; -} - void ata_sleepnow(void) { mutex_lock(&ata_mutex); @@ -994,11 +969,6 @@ void ata_sleepnow(void) mutex_unlock(&ata_mutex); } -void ata_close(void) -{ - ata_sleepnow(); -} - void ata_spin(void) { ata_set_active(); @@ -1034,10 +1004,6 @@ int ata_init(void) mutex_unlock(&ata_mutex); if (IS_ERR(rc)) return rc; - create_thread(ata_thread, ata_stack, - sizeof(ata_stack), 0, "ATA idle monitor" - IF_PRIO(, PRIORITY_USER_INTERFACE) - IF_COP(, CPU)); return 0; } @@ -1129,3 +1095,38 @@ void INT_MMC(void) SDCI_IRQ = irq; } +int ata_event(long id, intptr_t data) +{ + int rc = 0; + + /* GCC does a lousy job culling unreachable cases in the default handler + if statements are in a switch statement, so we'll do it this way. Only + the first case is frequently hit anyway. */ + if (LIKELY(id == Q_STORAGE_TICK)) + { + if (!ata_powered || + TIME_BEFORE(current_tick, ata_last_activity_value + ata_sleep_timeout)) + { + STG_EVENT_ASSERT_ACTIVE(STORAGE_ATA); + } + } + else if (id == Q_STORAGE_SLEEPNOW) + { + ata_sleepnow(); + } + else if (id == Q_STORAGE_SLEEP) + { + ata_last_activity_value = current_tick - ata_sleep_timeout + HZ / 5; + } + else if (id == SYS_USB_CONNECTED) + { + STG_EVENT_ASSERT_ACTIVE(STORAGE_ATA); + } + else + { + rc = storage_event_default_handler(id, data, ata_last_activity_value, + STORAGE_ATA); + } + + return rc; +} -- cgit v1.2.3