diff options
| author | Tom Rini <[email protected]> | 2022-11-08 09:45:10 -0500 |
|---|---|---|
| committer | Tom Rini <[email protected]> | 2022-11-08 09:45:10 -0500 |
| commit | 77b5cc2948f5d93fe3d275302f596ffd8701a875 (patch) | |
| tree | 703dfe2a0ebb2eacb241c19b99c96665a2d66811 /drivers | |
| parent | 88bd8ee106591eb900561715c44ad04441afc0e3 (diff) | |
| parent | 168a0e45fcf49194fca55795f84a844f16b480f6 (diff) | |
Merge tag 'dm-pull-7nov22' of https://source.denx.de/u-boot/custodians/u-boot-dm
sandbox UCLASS_HOST
Diffstat (limited to 'drivers')
| -rw-r--r-- | drivers/block/Makefile | 2 | ||||
| -rw-r--r-- | drivers/block/blk-uclass.c | 74 | ||||
| -rw-r--r-- | drivers/block/blkcache.c | 23 | ||||
| -rw-r--r-- | drivers/block/host-uclass.c | 176 | ||||
| -rw-r--r-- | drivers/block/host_dev.c | 142 | ||||
| -rw-r--r-- | drivers/block/sandbox.c | 236 |
6 files changed, 384 insertions, 269 deletions
diff --git a/drivers/block/Makefile b/drivers/block/Makefile index 19d9317c825..f12447d78d8 100644 --- a/drivers/block/Makefile +++ b/drivers/block/Makefile @@ -12,7 +12,7 @@ endif ifndef CONFIG_SPL_BUILD obj-$(CONFIG_IDE) += ide.o endif -obj-$(CONFIG_SANDBOX) += sandbox.o +obj-$(CONFIG_SANDBOX) += sandbox.o host-uclass.o host_dev.o obj-$(CONFIG_$(SPL_TPL_)BLOCK_CACHE) += blkcache.o obj-$(CONFIG_EFI_MEDIA) += efi-media-uclass.o diff --git a/drivers/block/blk-uclass.c b/drivers/block/blk-uclass.c index e82789f4a38..c69fc4d5182 100644 --- a/drivers/block/blk-uclass.c +++ b/drivers/block/blk-uclass.c @@ -26,7 +26,7 @@ static struct { { UCLASS_USB, "usb" }, { UCLASS_MMC, "mmc" }, { UCLASS_AHCI, "sata" }, - { UCLASS_ROOT, "host" }, + { UCLASS_HOST, "host" }, { UCLASS_NVME, "nvme" }, { UCLASS_EFI_MEDIA, "efi" }, { UCLASS_EFI_LOADER, "efiloader" }, @@ -369,45 +369,43 @@ int blk_dselect_hwpart(struct blk_desc *desc, int hwpart) return blk_select_hwpart(desc->bdev, hwpart); } -int blk_first_device(int uclass_id, struct udevice **devp) +static int _blk_next_device(int uclass_id, struct udevice **devp) { struct blk_desc *desc; - int ret; + int ret = 0; + + for (; *devp; uclass_find_next_device(devp)) { + desc = dev_get_uclass_plat(*devp); + if (desc->uclass_id == uclass_id) { + ret = device_probe(*devp); + if (!ret) + return 0; + } + } - ret = uclass_find_first_device(UCLASS_BLK, devp); if (ret) return ret; - if (!*devp) - return -ENODEV; - do { - desc = dev_get_uclass_plat(*devp); - if (desc->uclass_id == uclass_id) - return 0; - ret = uclass_find_next_device(devp); - if (ret) - return ret; - } while (*devp); return -ENODEV; } +int blk_first_device(int uclass_id, struct udevice **devp) +{ + uclass_find_first_device(UCLASS_BLK, devp); + + return _blk_next_device(uclass_id, devp); +} + int blk_next_device(struct udevice **devp) { struct blk_desc *desc; - int ret, uclass_id; + int uclass_id; desc = dev_get_uclass_plat(*devp); uclass_id = desc->uclass_id; - do { - ret = uclass_find_next_device(devp); - if (ret) - return ret; - if (!*devp) - return -ENODEV; - desc = dev_get_uclass_plat(*devp); - if (desc->uclass_id == uclass_id) - return 0; - } while (1); + uclass_find_next_device(devp); + + return _blk_next_device(uclass_id, devp); } int blk_find_device(int uclass_id, int devnum, struct udevice **devp) @@ -508,24 +506,28 @@ ulong blk_derase(struct blk_desc *desc, lbaint_t start, lbaint_t blkcnt) return blk_erase(desc->bdev, start, blkcnt); } -int blk_get_from_parent(struct udevice *parent, struct udevice **devp) +int blk_find_from_parent(struct udevice *parent, struct udevice **devp) { struct udevice *dev; - enum uclass_id id; - int ret; - device_find_first_child(parent, &dev); - if (!dev) { + if (device_find_first_child_by_uclass(parent, UCLASS_BLK, &dev)) { debug("%s: No block device found for parent '%s'\n", __func__, parent->name); return -ENODEV; } - id = device_get_uclass_id(dev); - if (id != UCLASS_BLK) { - debug("%s: Incorrect uclass %s for block device '%s'\n", - __func__, uclass_get_name(id), dev->name); - return -ENOTBLK; - } + *devp = dev; + + return 0; +} + +int blk_get_from_parent(struct udevice *parent, struct udevice **devp) +{ + struct udevice *dev; + int ret; + + ret = blk_find_from_parent(parent, &dev); + if (ret) + return ret; ret = device_probe(dev); if (ret) return ret; diff --git a/drivers/block/blkcache.c b/drivers/block/blkcache.c index b53420a3a88..f99465aa479 100644 --- a/drivers/block/blkcache.c +++ b/drivers/block/blkcache.c @@ -150,8 +150,8 @@ void blkcache_invalidate(int iftype, int devnum) list_for_each_safe(entry, n, &block_cache) { node = (struct block_cache_node *)entry; - if ((node->iftype == iftype) && - (node->devnum == devnum)) { + if (iftype == -1 || + (node->iftype == iftype && node->devnum == devnum)) { list_del(entry); free(node->cache); free(node); @@ -162,18 +162,10 @@ void blkcache_invalidate(int iftype, int devnum) void blkcache_configure(unsigned blocks, unsigned entries) { - struct block_cache_node *node; + /* invalidate cache if there is a change */ if ((blocks != _stats.max_blocks_per_entry) || - (entries != _stats.max_entries)) { - /* invalidate cache */ - while (!list_empty(&block_cache)) { - node = (struct block_cache_node *)block_cache.next; - list_del(&node->lh); - free(node->cache); - free(node); - } - _stats.entries = 0; - } + (entries != _stats.max_entries)) + blkcache_invalidate(-1, 0); _stats.max_blocks_per_entry = blocks; _stats.max_entries = entries; @@ -188,3 +180,8 @@ void blkcache_stats(struct block_cache_stats *stats) _stats.hits = 0; _stats.misses = 0; } + +void blkcache_free(void) +{ + blkcache_invalidate(-1, 0); +} diff --git a/drivers/block/host-uclass.c b/drivers/block/host-uclass.c new file mode 100644 index 00000000000..6460d968c23 --- /dev/null +++ b/drivers/block/host-uclass.c @@ -0,0 +1,176 @@ +// SPDX-License-Identifier: GPL-2.0+ +/* + * Uclass for sandbox host interface, used to access files on the host which + * contain partitions and filesystem + * + * Copyright 2022 Google LLC + * Written by Simon Glass <[email protected]> + */ + +#define LOG_CATEGORY UCLASS_HOST + +#include <common.h> +#include <blk.h> +#include <dm.h> +#include <malloc.h> +#include <sandbox_host.h> +#include <dm/device-internal.h> +#include <dm/lists.h> +#include <dm/uclass-internal.h> + +DECLARE_GLOBAL_DATA_PTR; + +/** + * struct host_priv - information kept by the host uclass + * + * @cur_dev: Currently selected host device, or NULL if none + */ +struct host_priv { + struct udevice *cur_dev; +}; + +int host_create_device(const char *label, bool removable, struct udevice **devp) +{ + char dev_name[30], *str, *label_new; + struct host_sb_plat *plat; + struct udevice *dev, *blk; + int ret; + + /* unbind any existing device with this label */ + dev = host_find_by_label(label); + if (dev) { + ret = host_detach_file(dev); + if (ret) + return log_msg_ret("det", ret); + + ret = device_unbind(dev); + if (ret) + return log_msg_ret("unb", ret); + } + + snprintf(dev_name, sizeof(dev_name), "host-%s", label); + str = strdup(dev_name); + if (!str) + return -ENOMEM; + + label_new = strdup(label); + if (!label_new) { + ret = -ENOMEM; + goto no_label; + } + + ret = device_bind_driver(gd->dm_root, "host_sb_drv", str, &dev); + if (ret) + goto no_dev; + device_set_name_alloced(dev); + + if (!blk_find_from_parent(dev, &blk)) { + struct blk_desc *desc = dev_get_uclass_plat(blk); + + desc->removable = removable; + } + + plat = dev_get_plat(dev); + plat->label = label_new; + *devp = dev; + + return 0; + +no_dev: + free(label_new); +no_label: + free(str); + + return ret; +} + +int host_attach_file(struct udevice *dev, const char *filename) +{ + struct host_ops *ops = host_get_ops(dev); + + if (!ops->attach_file) + return -ENOSYS; + + return ops->attach_file(dev, filename); +} + +int host_create_attach_file(const char *label, const char *filename, + bool removable, struct udevice **devp) +{ + struct udevice *dev; + int ret; + + ret = host_create_device(label, removable, &dev); + if (ret) + return log_msg_ret("cre", ret); + + ret = host_attach_file(dev, filename); + if (ret) { + device_unbind(dev); + return log_msg_ret("att", ret); + } + *devp = dev; + + return 0; +} + +int host_detach_file(struct udevice *dev) +{ + struct host_ops *ops = host_get_ops(dev); + + if (!ops->detach_file) + return -ENOSYS; + + if (dev == host_get_cur_dev()) + host_set_cur_dev(NULL); + + return ops->detach_file(dev); +} + +struct udevice *host_find_by_label(const char *label) +{ + struct udevice *dev; + struct uclass *uc; + + uclass_id_foreach_dev(UCLASS_HOST, dev, uc) { + struct host_sb_plat *plat = dev_get_plat(dev); + + if (plat->label && !strcmp(label, plat->label)) + return dev; + } + + return NULL; +} + +struct udevice *host_get_cur_dev(void) +{ + struct uclass *uc = uclass_find(UCLASS_HOST); + + if (uc) { + struct host_priv *priv = uclass_get_priv(uc); + + return priv->cur_dev; + } + + return NULL; +} + +void host_set_cur_dev(struct udevice *dev) +{ + struct uclass *uc = uclass_find(UCLASS_HOST); + + if (uc) { + struct host_priv *priv = uclass_get_priv(uc); + + priv->cur_dev = dev; + } +} + +UCLASS_DRIVER(host) = { + .id = UCLASS_HOST, + .name = "host", +#if CONFIG_IS_ENABLED(OF_REAL) + .post_bind = dm_scan_fdt_dev, +#endif + .priv_auto = sizeof(struct host_priv), +}; diff --git a/drivers/block/host_dev.c b/drivers/block/host_dev.c new file mode 100644 index 00000000000..5885fc358a5 --- /dev/null +++ b/drivers/block/host_dev.c @@ -0,0 +1,142 @@ +// SPDX-License-Identifier: GPL-2.0+ +/* + * Driver for sandbox host interface, used to access files on the host which + * contain partitions and filesystem + * + * Copyright 2022 Google LLC + * Written by Simon Glass <[email protected]> + */ + +#define LOG_CATEGORY UCLASS_HOST + +#include <common.h> +#include <blk.h> +#include <bootdev.h> +#include <dm.h> +#include <log.h> +#include <malloc.h> +#include <os.h> +#include <sandbox_host.h> +#include <dm/device-internal.h> + +static int host_sb_attach_file(struct udevice *dev, const char *filename) +{ + struct host_sb_plat *plat = dev_get_plat(dev); + struct blk_desc *desc; + struct udevice *blk; + int ret, fd, size; + char *fname; + + if (!filename) + return -EINVAL; + + if (plat->fd) + return log_msg_ret("fd", -EEXIST); + + /* Sanity check that host_sb_bind() has been used */ + ret = blk_find_from_parent(dev, &blk); + if (ret) + return ret; + + fd = os_open(filename, OS_O_RDWR); + if (fd == -1) { + printf("Failed to access host backing file '%s', trying read-only\n", + filename); + fd = os_open(filename, OS_O_RDONLY); + if (fd == -1) { + printf("- still failed\n"); + return log_msg_ret("open", -ENOENT); + } + } + + fname = strdup(filename); + if (!fname) { + ret = -ENOMEM; + goto err_fname; + } + + size = os_filesize(fd); + desc = dev_get_uclass_plat(blk); + desc->lba = size / desc->blksz; + + /* write this in last, when nothing can go wrong */ + plat = dev_get_plat(dev); + plat->fd = fd; + plat->filename = fname; + + return 0; + +err_fname: + os_close(fd); + + return ret; +} + +int host_sb_detach_file(struct udevice *dev) +{ + struct host_sb_plat *plat = dev_get_plat(dev); + int ret; + + if (!plat->fd) + return log_msg_ret("fd", -ENOENT); + + ret = device_remove(dev, DM_REMOVE_NORMAL); + if (ret) + return log_msg_ret("rem", ret); + + /* Unbind all children */ + ret = device_chld_unbind(dev, NULL); + if (ret) + return log_msg_ret("unb", ret); + + os_close(plat->fd); + plat->fd = 0; + free(plat->filename); + free(plat->label); + + return 0; +} + +static int host_sb_bind(struct udevice *dev) +{ + struct udevice *blk, *bdev; + struct blk_desc *desc; + int ret; + + ret = blk_create_devicef(dev, "sandbox_host_blk", "blk", UCLASS_HOST, + dev_seq(dev), 512, 0, &blk); + if (ret) + return log_msg_ret("blk", ret); + + desc = dev_get_uclass_plat(blk); + snprintf(desc->vendor, BLK_VEN_SIZE, "U-Boot"); + snprintf(desc->product, BLK_PRD_SIZE, "hostfile"); + snprintf(desc->revision, BLK_REV_SIZE, "1.0"); + + if (CONFIG_IS_ENABLED(BOOTSTD)) { + ret = bootdev_bind(dev, "host_bootdev", "bootdev", &bdev); + if (ret) + return log_msg_ret("bd", ret); + } + + return 0; +} + +struct host_ops host_sb_ops = { + .attach_file = host_sb_attach_file, + .detach_file = host_sb_detach_file, +}; + +static const struct udevice_id host_ids[] = { + { .compatible = "sandbox,host" }, + { } +}; + +U_BOOT_DRIVER(host_sb_drv) = { + .name = "host_sb_drv", + .id = UCLASS_HOST, + .of_match = host_ids, + .ops = &host_sb_ops, + .bind = host_sb_bind, + .plat_auto = sizeof(struct host_sb_plat), +}; diff --git a/drivers/block/sandbox.c b/drivers/block/sandbox.c index f2aae89716b..be4e02cb601 100644 --- a/drivers/block/sandbox.c +++ b/drivers/block/sandbox.c @@ -10,242 +10,50 @@ #include <part.h> #include <os.h> #include <malloc.h> -#include <sandboxblockdev.h> +#include <sandbox_host.h> #include <asm/global_data.h> #include <dm/device_compat.h> -#include <linux/errno.h> #include <dm/device-internal.h> +#include <linux/errno.h> DECLARE_GLOBAL_DATA_PTR; -#ifndef CONFIG_BLK -static struct host_block_dev host_devices[SANDBOX_HOST_MAX_DEVICES]; - -static struct host_block_dev *find_host_device(int dev) -{ - if (dev >= 0 && dev < SANDBOX_HOST_MAX_DEVICES) - return &host_devices[dev]; - - return NULL; -} -#endif - -#ifdef CONFIG_BLK static unsigned long host_block_read(struct udevice *dev, unsigned long start, lbaint_t blkcnt, void *buffer) { - struct host_block_dev *host_dev = dev_get_plat(dev); - struct blk_desc *block_dev = dev_get_uclass_plat(dev); + struct blk_desc *desc = dev_get_uclass_plat(dev); + struct udevice *host_dev = dev_get_parent(dev); + struct host_sb_plat *plat = dev_get_plat(host_dev); -#else -static unsigned long host_block_read(struct blk_desc *block_dev, - unsigned long start, lbaint_t blkcnt, - void *buffer) -{ - int dev = block_dev->devnum; - struct host_block_dev *host_dev = find_host_device(dev); - - if (!host_dev) - return -1; -#endif - - if (os_lseek(host_dev->fd, start * block_dev->blksz, OS_SEEK_SET) == - -1) { + if (os_lseek(plat->fd, start * desc->blksz, OS_SEEK_SET) == -1) { printf("ERROR: Invalid block %lx\n", start); return -1; } - ssize_t len = os_read(host_dev->fd, buffer, blkcnt * block_dev->blksz); + ssize_t len = os_read(plat->fd, buffer, blkcnt * desc->blksz); if (len >= 0) - return len / block_dev->blksz; - return -1; + return len / desc->blksz; + + return -EIO; } -#ifdef CONFIG_BLK static unsigned long host_block_write(struct udevice *dev, unsigned long start, lbaint_t blkcnt, const void *buffer) { - struct host_block_dev *host_dev = dev_get_plat(dev); - struct blk_desc *block_dev = dev_get_uclass_plat(dev); -#else -static unsigned long host_block_write(struct blk_desc *block_dev, - unsigned long start, lbaint_t blkcnt, - const void *buffer) -{ - int dev = block_dev->devnum; - struct host_block_dev *host_dev = find_host_device(dev); -#endif + struct blk_desc *desc = dev_get_uclass_plat(dev); + struct udevice *host_dev = dev_get_parent(dev); + struct host_sb_plat *plat = dev_get_plat(host_dev); - if (os_lseek(host_dev->fd, start * block_dev->blksz, OS_SEEK_SET) == - -1) { + if (os_lseek(plat->fd, start * desc->blksz, OS_SEEK_SET) == -1) { printf("ERROR: Invalid block %lx\n", start); return -1; } - ssize_t len = os_write(host_dev->fd, buffer, blkcnt * block_dev->blksz); + ssize_t len = os_write(plat->fd, buffer, blkcnt * desc->blksz); if (len >= 0) - return len / block_dev->blksz; - return -1; -} - -#ifdef CONFIG_BLK -int host_dev_bind(int devnum, char *filename, bool removable) -{ - struct host_block_dev *host_dev; - struct udevice *dev; - struct blk_desc *desc; - char dev_name[20], *str, *fname; - int ret, fd; - - /* Remove and unbind the old device, if any */ - ret = blk_get_device(UCLASS_ROOT, devnum, &dev); - if (ret == 0) { - ret = device_remove(dev, DM_REMOVE_NORMAL); - if (ret) - return ret; - ret = device_unbind(dev); - if (ret) - return ret; - } else if (ret != -ENODEV) { - return ret; - } - - if (!filename) - return 0; - - snprintf(dev_name, sizeof(dev_name), "host%d", devnum); - str = strdup(dev_name); - if (!str) - return -ENOMEM; - fname = strdup(filename); - if (!fname) { - free(str); - return -ENOMEM; - } - - fd = os_open(filename, OS_O_RDWR); - if (fd == -1) { - printf("Failed to access host backing file '%s', trying read-only\n", - filename); - fd = os_open(filename, OS_O_RDONLY); - if (fd == -1) { - printf("- still failed\n"); - ret = -ENOENT; - goto err; - } - } - ret = blk_create_device(gd->dm_root, "sandbox_host_blk", str, - UCLASS_ROOT, devnum, 512, - os_lseek(fd, 0, OS_SEEK_END) / 512, &dev); - if (ret) - goto err_file; - - host_dev = dev_get_plat(dev); - host_dev->fd = fd; - host_dev->filename = fname; + return len / desc->blksz; - ret = device_probe(dev); - if (ret) { - device_unbind(dev); - goto err_file; - } - - desc = blk_get_devnum_by_uclass_id(UCLASS_ROOT, devnum); - desc->removable = removable; - snprintf(desc->vendor, BLK_VEN_SIZE, "U-Boot"); - snprintf(desc->product, BLK_PRD_SIZE, "hostfile"); - snprintf(desc->revision, BLK_REV_SIZE, "1.0"); - - return 0; -err_file: - os_close(fd); -err: - free(fname); - free(str); - return ret; -} -#else -int host_dev_bind(int dev, char *filename, bool removable) -{ - struct host_block_dev *host_dev = find_host_device(dev); - - if (!host_dev) - return -1; - if (host_dev->blk_dev.priv) { - os_close(host_dev->fd); - host_dev->blk_dev.priv = NULL; - } - if (host_dev->filename) - free(host_dev->filename); - if (filename && *filename) { - host_dev->filename = strdup(filename); - } else { - host_dev->filename = NULL; - return 0; - } - - host_dev->fd = os_open(host_dev->filename, OS_O_RDWR); - if (host_dev->fd == -1) { - printf("Failed to access host backing file '%s'\n", - host_dev->filename); - return 1; - } - - struct blk_desc *blk_dev = &host_dev->blk_dev; - blk_dev->uclass_id = UCLASS_ROOT; - blk_dev->priv = host_dev; - blk_dev->blksz = 512; - blk_dev->lba = os_lseek(host_dev->fd, 0, OS_SEEK_END) / blk_dev->blksz; - blk_dev->block_read = host_block_read; - blk_dev->block_write = host_block_write; - blk_dev->devnum = dev; - blk_dev->part_type = PART_TYPE_UNKNOWN; - blk_dev->removable = removable; - snprintf(blk_dev->vendor, BLK_VEN_SIZE, "U-Boot"); - snprintf(blk_dev->product, BLK_PRD_SIZE, "hostfile"); - snprintf(blk_dev->revision, BLK_REV_SIZE, "1.0"); - part_init(blk_dev); - - return 0; -} -#endif - -int host_get_dev_err(int devnum, struct blk_desc **blk_devp) -{ -#ifdef CONFIG_BLK - struct udevice *dev; - int ret; - - ret = blk_get_device(UCLASS_ROOT, devnum, &dev); - if (ret) - return ret; - *blk_devp = dev_get_uclass_plat(dev); -#else - struct host_block_dev *host_dev = find_host_device(devnum); - - if (!host_dev) - return -ENODEV; - - if (!host_dev->blk_dev.priv) - return -ENOENT; - - *blk_devp = &host_dev->blk_dev; -#endif - - return 0; -} - -#ifdef CONFIG_BLK - -int sandbox_host_unbind(struct udevice *dev) -{ - struct host_block_dev *host_dev; - - /* Data validity is checked in host_dev_bind() */ - host_dev = dev_get_plat(dev); - os_close(host_dev->fd); - - return 0; + return -EIO; } static const struct blk_ops sandbox_host_blk_ops = { @@ -257,14 +65,4 @@ U_BOOT_DRIVER(sandbox_host_blk) = { .name = "sandbox_host_blk", .id = UCLASS_BLK, .ops = &sandbox_host_blk_ops, - .unbind = sandbox_host_unbind, - .plat_auto = sizeof(struct host_block_dev), -}; -#else -U_BOOT_LEGACY_BLK(sandbox_host) = { - .uclass_idname = "host", - .uclass_id = UCLASS_ROOT, - .max_devs = SANDBOX_HOST_MAX_DEVICES, - .get_dev = host_get_dev_err, }; -#endif |
