From 6d75be5af749d56a560fe15131f77c192202536c Mon Sep 17 00:00:00 2001 From: Eric Chan Date: Fri, 16 Feb 2024 12:37:00 +1100 Subject: [PATCH] allow uio driver to take in optional args --- tools/linux/blk_driver_init | 4 ++-- tools/linux/include/uio_drivers/blk.h | 2 +- tools/linux/uio_drivers/blk.c | 28 +++++++++++++++------------ tools/linux/uio_drivers/libuio.c | 14 +++++++------- 4 files changed, 26 insertions(+), 22 deletions(-) diff --git a/tools/linux/blk_driver_init b/tools/linux/blk_driver_init index 3fae0071..eb4f2c1f 100644 --- a/tools/linux/blk_driver_init +++ b/tools/linux/blk_driver_init @@ -1,4 +1,4 @@ #!/bin/sh -/root/uio_blk_driver 0 & -/root/uio_blk_driver 1 & +/root/uio_blk_driver 0 /root & +/root/uio_blk_driver 1 /root & diff --git a/tools/linux/include/uio_drivers/blk.h b/tools/linux/include/uio_drivers/blk.h index 233901be..6cf9b2d2 100644 --- a/tools/linux/include/uio_drivers/blk.h +++ b/tools/linux/include/uio_drivers/blk.h @@ -7,5 +7,5 @@ #include -int driver_init(int id, void **maps, uintptr_t *maps_phys, int num_maps); +int driver_init(int driver_id, void **maps, uintptr_t *maps_phys, int num_maps, int argc, char **argv); void driver_notified(); diff --git a/tools/linux/uio_drivers/blk.c b/tools/linux/uio_drivers/blk.c index 5b18d18b..d7167fb3 100644 --- a/tools/linux/uio_drivers/blk.c +++ b/tools/linux/uio_drivers/blk.c @@ -21,37 +21,44 @@ // #define DEBUG_UIO_BLOCK #if defined(DEBUG_UIO_BLOCK) -#define LOG_UIO_BLOCK(...) do{ printf("BLK_DRIVER_%d", driver_id); printf(": "); printf(__VA_ARGS__); }while(0) +#define LOG_UIO_BLOCK(...) do{ printf("UIO_BLK_DRIVER_%d", id); printf(": "); printf(__VA_ARGS__); }while(0) #else #define LOG_UIO_BLOCK(...) do{}while(0) #endif -#define LOG_UIO_BLOCK_ERR(...) do{ printf("BLK_DRIVER_%d", driver_id); printf("|ERROR: "); printf(__VA_ARGS__); }while(0) +#define LOG_UIO_BLOCK_ERR(...) do{ printf("UIO_BLK_DRIVER_%d", id); printf("|ERROR: "); printf(__VA_ARGS__); }while(0) #define STORAGE_MAX_PATHNAME 64 -#define STORAGE_BASENAME "/root/storage_" -int driver_id; +int id; +int storage_fd; + blk_storage_info_t *blk_config; blk_queue_handle_t h; uintptr_t blk_data; uintptr_t blk_data_phys; -int storage_fd; uintptr_t data_phys_to_virt(uintptr_t phys_addr) { return phys_addr - blk_data_phys + blk_data; } -int driver_init(int id, void **maps, uintptr_t *maps_phys, int num_maps) +int driver_init(int driver_id, void **maps, uintptr_t *maps_phys, int num_maps, int argc, char **argv) { - driver_id = id; - + id = driver_id; + if (num_maps != 4) { LOG_UIO_BLOCK_ERR("Expecting 4 maps, got %d\n", num_maps); return -1; } + if (argc != 1) { + LOG_UIO_BLOCK_ERR("Expecting 1 driver argument, got %d\n", argc); + return -1; + } + + char *storage_base_path = argv[0]; + blk_config = (blk_storage_info_t *)maps[0]; blk_req_queue_t *req_queue = (blk_req_queue_t *)maps[1]; blk_resp_queue_t *resp_queue = (blk_resp_queue_t *)maps[2]; @@ -70,11 +77,8 @@ int driver_init(int id, void **maps, uintptr_t *maps_phys, int num_maps) blk_config->blocksize = 1024; blk_config->read_only = false; - // @TODO, @ericc: finnicky and error prone, assumes storage file has name - // storage_, need to figure out a better way to set this up whilst keeping - // driver init generic to all drivers char storage_path[STORAGE_MAX_PATHNAME]; - int len = snprintf(storage_path, STORAGE_MAX_PATHNAME, "%s%d", STORAGE_BASENAME, driver_id); + int len = snprintf(storage_path, STORAGE_MAX_PATHNAME, "%s/storage_%d", storage_base_path, driver_id); if (len < 0 || len >= STORAGE_MAX_PATHNAME) { LOG_UIO_BLOCK_ERR("Failed to generate storage path\n"); return -1; diff --git a/tools/linux/uio_drivers/libuio.c b/tools/linux/uio_drivers/libuio.c index af4ab624..4a3464e9 100644 --- a/tools/linux/uio_drivers/libuio.c +++ b/tools/linux/uio_drivers/libuio.c @@ -51,7 +51,7 @@ static int num_maps; /* * Just happily abort if the user can't be bother to provide these functions */ -__attribute__((weak)) int driver_init(int id, void **maps, uintptr_t *maps_phys, int num_maps) +__attribute__((weak)) int driver_init(int driver_id, void **maps, uintptr_t *maps_phys, int num_maps, int argc, char **argv) { assert(!"should not be here!"); } @@ -239,15 +239,15 @@ static int uio_map_init(int fd) } int main(int argc, char **argv) { - if (argc != 2) { - printf("Usage: %s \n", argv[0]); + if (argc < 2) { + printf("Usage: %s [driver_args...]\n", argv[0]); return 1; } uio_num = atoi(argv[1]); if (uio_num < 0) { printf("Failed to convert uio number to integer\n"); - printf("Usage: %s \n", argv[0]); + printf("Usage: %s [driver_args...]\n", argv[0]); close(pfd.fd); return 1; } @@ -257,7 +257,7 @@ int main(int argc, char **argv) { int len = snprintf(uio_device_name, sizeof(uio_device_name), "/dev/uio%s", argv[1]); if (len < 0 || len >= sizeof(uio_device_name)) { LOG_UIO_ERR("Failed to create uio device name\n"); - printf("Usage: %s \n", argv[0]); + printf("Usage: %s [driver_args...]\n", argv[0]); return 1; } @@ -265,7 +265,7 @@ int main(int argc, char **argv) { pfd.fd = open(uio_device_name, O_RDWR); if (pfd.fd < 0) { LOG_UIO_ERR("Failed to open %s\n", uio_device_name); - printf("Usage: %s \n", argv[0]); + printf("Usage: %s [driver_args...]\n", argv[0]); return 1; } @@ -280,7 +280,7 @@ int main(int argc, char **argv) { } /* Initialise driver */ - if (driver_init(uio_num, maps, maps_phys, num_maps) != 0) { + if (driver_init(uio_num, maps, maps_phys, num_maps, argc - 2, argv + 2) != 0) { LOG_UIO_ERR("Failed to initialise driver\n"); close(pfd.fd); return 1;