Skip to content

Commit

Permalink
allow uio driver to take in optional args
Browse files Browse the repository at this point in the history
  • Loading branch information
erichchan999 committed Feb 16, 2024
1 parent 2f53b40 commit 6d75be5
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 22 deletions.
4 changes: 2 additions & 2 deletions tools/linux/blk_driver_init
Original file line number Diff line number Diff line change
@@ -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 &
2 changes: 1 addition & 1 deletion tools/linux/include/uio_drivers/blk.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@

#include <stdint.h>

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();
28 changes: 16 additions & 12 deletions tools/linux/uio_drivers/blk.c
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand All @@ -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_<id>, 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;
Expand Down
14 changes: 7 additions & 7 deletions tools/linux/uio_drivers/libuio.c
Original file line number Diff line number Diff line change
Expand Up @@ -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!");
}
Expand Down Expand Up @@ -239,15 +239,15 @@ static int uio_map_init(int fd)
}

int main(int argc, char **argv) {
if (argc != 2) {
printf("Usage: %s <uio_device_number>\n", argv[0]);
if (argc < 2) {
printf("Usage: %s <uio_device_number> [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 <uio_device_number>\n", argv[0]);
printf("Usage: %s <uio_device_number> [driver_args...]\n", argv[0]);
close(pfd.fd);
return 1;
}
Expand All @@ -257,15 +257,15 @@ 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 <uio_device_number>\n", argv[0]);
printf("Usage: %s <uio_device_number> [driver_args...]\n", argv[0]);
return 1;
}

// get the file descriptor for polling
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 <uio_device_number>\n", argv[0]);
printf("Usage: %s <uio_device_number> [driver_args...]\n", argv[0]);
return 1;
}

Expand All @@ -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;
Expand Down

0 comments on commit 6d75be5

Please sign in to comment.