From ff1f0e414a6eafd76c6e8ec114bebf6df6c49d68 Mon Sep 17 00:00:00 2001 From: Simon Glass Date: Sat, 29 Oct 2022 19:47:03 -0600 Subject: dm: sandbox: Drop non-BLK code from host implementation This is not used anymore. Drop it. Signed-off-by: Simon Glass --- include/sandboxblockdev.h | 3 --- 1 file changed, 3 deletions(-) (limited to 'include') diff --git a/include/sandboxblockdev.h b/include/sandboxblockdev.h index dc983f0417b..0528f891b12 100644 --- a/include/sandboxblockdev.h +++ b/include/sandboxblockdev.h @@ -10,9 +10,6 @@ #define SANDBOX_HOST_MAX_DEVICES 4 struct host_block_dev { -#ifndef CONFIG_BLK - struct blk_desc blk_dev; -#endif char *filename; int fd; }; -- cgit v1.2.3 From 6ca4d5b96b54bdd6086c084631500e918b37ceb7 Mon Sep 17 00:00:00 2001 From: Simon Glass Date: Sat, 29 Oct 2022 19:47:04 -0600 Subject: sandbox: Add missing comments for os_alarm() Add the documentation to avoid a warning with 'make htmldocs'. Fixes: 10107efedd5 ("sandbox: add SIGALRM-based watchdog device") Signed-off-by: Simon Glass --- include/os.h | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'include') diff --git a/include/os.h b/include/os.h index 54874f5e0e8..0415f0f0e7a 100644 --- a/include/os.h +++ b/include/os.h @@ -110,6 +110,10 @@ void os_exit(int exit_code) __attribute__((noreturn)); /** * os_alarm() - access to the OS alarm() system call + * + * @seconds: number of seconds before the signal is sent + * Returns: number of seconds remaining until any previously scheduled alarm was + * due to be delivered; 0 if there was no previously scheduled alarm */ unsigned int os_alarm(unsigned int seconds); -- cgit v1.2.3 From 5ea894ac4285be2bebc2e7bdfd6451c699469f37 Mon Sep 17 00:00:00 2001 From: Simon Glass Date: Sat, 29 Oct 2022 19:47:08 -0600 Subject: dm: test: Clear the block cache after running a test Some tests access data in block devices and so cause the cache to fill up. This results in memory being allocated. Some tests check the malloc usage at the beginning and then again at the end, to ensure there is no memory leak caused by the test. The block cache makes this difficult, since the any test may cause entries to be allocated or even freed, if the cache becomes full. It is simpler to clear the block cache after each test. This ensures that it will not introduce noise in tests which check malloc usage. Add the logic to clear the cache, using the existing blkcache_invalidate() function. Drop the duplicate code at the same time. Signed-off-by: Simon Glass --- include/blk.h | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) (limited to 'include') diff --git a/include/blk.h b/include/blk.h index e854166edb9..c8cbb64c7f8 100644 --- a/include/blk.h +++ b/include/blk.h @@ -147,8 +147,8 @@ void blkcache_fill(int iftype, int dev, * blkcache_invalidate() - discard the cache for a set of blocks * because of a write or device (re)initialization. * - * @param iftype - uclass_id_x for type of device - * @param dev - device index of particular type + * @iftype - UCLASS_ID_ for type of device, or -1 for any + * @dev - device index of particular type, if @iftype is not -1 */ void blkcache_invalidate(int iftype, int dev); @@ -178,6 +178,9 @@ struct block_cache_stats { */ void blkcache_stats(struct block_cache_stats *stats); +/** blkcache_free() - free all memory allocated to the block cache */ +void blkcache_free(void); + #else static inline int blkcache_read(int iftype, int dev, @@ -193,6 +196,8 @@ static inline void blkcache_fill(int iftype, int dev, static inline void blkcache_invalidate(int iftype, int dev) {} +static inline void blkcache_free(void) {} + #endif #if CONFIG_IS_ENABLED(BLK) -- cgit v1.2.3 From d1b46595700b063faaec3e33f5754642e68b3d8f Mon Sep 17 00:00:00 2001 From: Simon Glass Date: Sat, 29 Oct 2022 19:47:13 -0600 Subject: test: Add a way to detect a test that breaks another When running unit tests, some may have side effects which cause a subsequent test to break. This can sometimes be seen when using 'ut dm' or similar. Add a new argument which allows a particular (failing) test to be run immediately after a certain number of tests have run. This allows the test causing the failure to be determined. Update the documentation also. Signed-off-by: Simon Glass --- include/test/ut.h | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) (limited to 'include') diff --git a/include/test/ut.h b/include/test/ut.h index e0e618b58c2..4d00b4eeca1 100644 --- a/include/test/ut.h +++ b/include/test/ut.h @@ -410,10 +410,15 @@ void test_set_state(struct unit_test_state *uts); * then all tests are run * @runs_per_test: Number of times to run each test (typically 1) * @force_run: Run tests that are marked as manual-only (UT_TESTF_MANUAL) + * @test_insert: String describing a test to run after n other tests run, in the + * format n:name where n is the number of tests to run before this one and + * name is the name of the test to run. This is used to find which test causes + * another test to fail. If the one test fails, testing stops immediately. + * Pass NULL to disable this * Return: 0 if all tests passed, -1 if any failed */ int ut_run_list(const char *name, const char *prefix, struct unit_test *tests, int count, const char *select_name, int runs_per_test, - bool force_run); + bool force_run, const char *test_insert); #endif -- cgit v1.2.3 From 41e751091d7cb1a71ac13ab037e0fcf4fcee67e3 Mon Sep 17 00:00:00 2001 From: Simon Glass Date: Sat, 29 Oct 2022 19:47:14 -0600 Subject: dm: blk: Tidy up obtaining a block device from its parent This function now finds its block-device child by looking for a child device of the correct uclass (UCLASS_BLK). It cannot produce a device of any other type, so drop the superfluous check. Provide a version which does not probe the device, since that is often needed when setting up the device's platdata. Also fix up the function's comment. Signed-off-by: Simon Glass --- include/blk.h | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) (limited to 'include') diff --git a/include/blk.h b/include/blk.h index c8cbb64c7f8..1db203c1bab 100644 --- a/include/blk.h +++ b/include/blk.h @@ -453,10 +453,36 @@ int blk_next_free_devnum(enum uclass_id uclass_id); */ int blk_select_hwpart(struct udevice *dev, int hwpart); +/** + * blk_find_from_parent() - find a block device by looking up its parent + * + * All block devices have a parent 'media' device which provides the block + * driver for the block device, ensuring that access to the underlying medium + * is available. + * + * The block device is not activated by this function. See + * blk_get_from_parent() for that. + * + * @parent: Media device + * @devp: Returns the associated block device, if any + * Returns: 0 if OK, -ENODEV if @parent is not a media device and has no + * UCLASS_BLK child + */ +int blk_find_from_parent(struct udevice *parent, struct udevice **devp); + /** * blk_get_from_parent() - obtain a block device by looking up its parent * - * All devices with + * All block devices have a parent 'media' device which provides the block + * driver for the block device, ensuring that access to the underlying medium + * is available. + * + * The block device is probed and ready for use. + * + * @parent: Media device + * @devp: Returns the associated block device, if any + * Returns: 0 if OK, -ENODEV if @parent is not a media device and has no + * UCLASS_BLK child */ int blk_get_from_parent(struct udevice *parent, struct udevice **devp); -- cgit v1.2.3 From 9bd1aa8af2ed3efde28250987806aaeb12837c66 Mon Sep 17 00:00:00 2001 From: Simon Glass Date: Sat, 29 Oct 2022 19:47:15 -0600 Subject: dm: sandbox: Create a new HOST uclass Sandbox supports block devices which can access files on the host machine. At present there is no uclass for this. The devices are attached to the root devic. The block-device type is therefore set to UCLASS_ROOT which is confusing. Block devices should be attached to a 'media' device instead, something which handles access to the actual media and provides the block driver for the block device. Create a new uclass to handle this. It supports two operations, to attach and detach a file on the host machine. For now this is not fully plumbed in. Signed-off-by: Simon Glass --- include/dm/uclass-id.h | 1 + include/sandbox_host.h | 125 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 126 insertions(+) create mode 100644 include/sandbox_host.h (limited to 'include') diff --git a/include/dm/uclass-id.h b/include/dm/uclass-id.h index 1f3cf8c0853..376f741cc2b 100644 --- a/include/dm/uclass-id.h +++ b/include/dm/uclass-id.h @@ -63,6 +63,7 @@ enum uclass_id { UCLASS_GPIO, /* Bank of general-purpose I/O pins */ UCLASS_HASH, /* Hash device */ UCLASS_HWSPINLOCK, /* Hardware semaphores */ + UCLASS_HOST, /* Sandbox host device */ UCLASS_I2C, /* I2C bus */ UCLASS_I2C_EEPROM, /* I2C EEPROM device */ UCLASS_I2C_GENERIC, /* Generic I2C device */ diff --git a/include/sandbox_host.h b/include/sandbox_host.h new file mode 100644 index 00000000000..2e37ede2354 --- /dev/null +++ b/include/sandbox_host.h @@ -0,0 +1,125 @@ +/* SPDX-License-Identifier: GPL-2.0+ */ +/* + * sandbox host uclass + * + * Copyright 2022 Google LLC + */ + +#ifndef __SANDBOX_HOST__ +#define __SANDBOX_HOST__ + +/** + * struct host_sb_plat - platform data for a host device + * + * @label: Label for this device (allocated) + * @filename: Name of file this is attached to, or NULL (allocated) + * @fd: File descriptor of file, or 0 for none (file is not open) + */ +struct host_sb_plat { + char *label; + char *filename; + int fd; +}; + +/** + * struct host_ops - operations supported by UCLASS_HOST + * + * @attach_file: Attach a new file to a device + * @detach_file: Detach a file from a device + */ +struct host_ops { + /* + * attach_file() - Attach a new file to the device + * + * @dev: Device to update + * @filename: Name of the file, e.g. "/path/to/disk.img" + * Returns: 0 if OK, -EEXIST if a file is already attached, other -ve on + * other error + */ + int (*attach_file)(struct udevice *dev, const char *filename); + + /** + * detach_file() - Detach a file from the device + * + * @dev: Device to detach from + * Returns: 0 if OK, -ENOENT if no file is attached, other -ve on other + * error + */ + int (*detach_file)(struct udevice *dev); +}; + +#define host_get_ops(dev) ((struct host_ops *)(dev)->driver->ops) + +/** + * host_attach_file() - Attach a new file to the device + * + * @dev: Device to update + * @filename: Name of the file, e.g. "/path/to/disk.img" + * Returns: 0 if OK, -EEXIST if a file is already attached, other -ve on + * other error + */ +int host_attach_file(struct udevice *dev, const char *filename); + +/** + * host_detach_file() - Detach a file from the device + * + * @dev: Device to detach from + * Returns: 0 if OK, -ENOENT if no file is attached, other -ve on other + * error + */ +int host_detach_file(struct udevice *dev); + +/** + * host_create_device() - Create a new host device + * + * Any existing device with the same label is removed and unbound first + * + * @label: Label of the attachment, e.g. "test1" + * @removable: true if the device should be marked as removable, false + * if it is fixed. See enum blk_flag_t + * @devp: Returns the device created, on success + * Returns: 0 if OK, -ve on error + */ +int host_create_device(const char *label, bool removable, + struct udevice **devp); + +/** + * host_create_attach_file() - Create a new host device attached to a file + * + * @label: Label of the attachment, e.g. "test1" + * @filename: Name of the file, e.g. "/path/to/disk.img" + * @removable: true if the device should be marked as removable, false + * if it is fixed. See enum blk_flag_t + * @devp: Returns the device created, on success + * Returns: 0 if OK, -ve on error + */ +int host_create_attach_file(const char *label, const char *filename, + bool removable, struct udevice **devp); + +/** + * host_find_by_label() - Find a host by label + * + * Searches all host devices to find one with the given label + * + * @label: Label to find + * Returns: associated device, or NULL if not found + */ +struct udevice *host_find_by_label(const char *label); + +/** + * host_get_cur_dev() - Get the current device + * + * Returns current device, or NULL if none + */ +struct udevice *host_get_cur_dev(void); + +/** + * host_set_cur_dev() - Set the current device + * + * Sets the current device, or clears it if @dev is NULL + * + * @dev: Device to set as the current one + */ +void host_set_cur_dev(struct udevice *dev); + +#endif /* __SANDBOX_HOST__ */ -- cgit v1.2.3 From 952018117ab4daff5fb4500d5ce0143678473ca4 Mon Sep 17 00:00:00 2001 From: Simon Glass Date: Sat, 29 Oct 2022 19:47:17 -0600 Subject: dm: sandbox: Switch over to using the new host uclass Update the sandbox implementation to use UCLASS_HOST and adjust all the pieces to continue to work: - Update the 'host' command to use the new API - Replace various uses of UCLASS_ROOT with UCLASS_HOST - Disable test_eficonfig since it doesn't work (this should have a unit test to allow this to be debugged) - Update the blk test to use the new API - Drop the old header file Unfortunately it does not seem to be possible to split this change up further. Signed-off-by: Simon Glass --- include/sandboxblockdev.h | 28 ---------------------------- 1 file changed, 28 deletions(-) delete mode 100644 include/sandboxblockdev.h (limited to 'include') diff --git a/include/sandboxblockdev.h b/include/sandboxblockdev.h deleted file mode 100644 index 0528f891b12..00000000000 --- a/include/sandboxblockdev.h +++ /dev/null @@ -1,28 +0,0 @@ -/* SPDX-License-Identifier: GPL-2.0+ */ -/* - * Copyright (c) 2013, Henrik Nordstrom - */ - -#ifndef __SANDBOX_BLOCK_DEV__ -#define __SANDBOX_BLOCK_DEV__ - -/* Maximum number of host devices - see drivers/block/sandbox.c */ -#define SANDBOX_HOST_MAX_DEVICES 4 - -struct host_block_dev { - char *filename; - int fd; -}; - -/** - * host_dev_bind() - Bind or unbind a device - * - * @dev: Device number (0=first slot) - * @filename: Host filename to use, or NULL to unbind - * @removable: true if the block device should mark itself as removable - */ -int host_dev_bind(int dev, char *filename, bool removable); - -int host_get_dev_err(int dev, struct blk_desc **blk_devp); - -#endif -- cgit v1.2.3