public inbox for u-boot@lists.denx.de
 help / color / mirror / Atom feed
From: Simon Glass <sjg@chromium.org>
To: U-Boot Mailing List <u-boot@lists.denx.de>
Cc: Tom Rini <trini@konsulko.com>, Simon Glass <sjg@chromium.org>,
	AKASHI Takahiro <takahiro.akashi@linaro.org>,
	Heinrich Schuchardt <xypron.glpk@gmx.de>,
	Marek Vasut <marex@denx.de>,
	Masahisa Kojima <masahisa.kojima@linaro.org>,
	Patrick Delaunay <patrick.delaunay@foss.st.com>,
	Pavel Herrmann <morpheus.ibis@gmail.com>
Subject: [PATCH 18/18] dm: blk: Expand iteration and add tests
Date: Mon, 28 Feb 2022 12:08:35 -0700	[thread overview]
Message-ID: <20220228190835.1480772-19-sjg@chromium.org> (raw)
In-Reply-To: <20220228190835.1480772-1-sjg@chromium.org>

Add some functions which support iteration before probing. Also add tests
for the functions.

Signed-off-by: Simon Glass <sjg@chromium.org>
---

 drivers/block/blk-uclass.c |  24 ++++++++
 include/blk.h              |  45 +++++++++++++++
 test/dm/blk.c              | 111 +++++++++++++++++++++++++++++++++++++
 3 files changed, 180 insertions(+)

diff --git a/drivers/block/blk-uclass.c b/drivers/block/blk-uclass.c
index bee1cd6f0d..84502bb47c 100644
--- a/drivers/block/blk-uclass.c
+++ b/drivers/block/blk-uclass.c
@@ -552,6 +552,30 @@ static int blk_flags_check(struct udevice *dev, enum blk_flag_t req_flags)
 	return flags & req_flags ? 0 : 1;
 }
 
+int blk_find_first(enum blk_flag_t flags, struct udevice **devp)
+{
+	int ret;
+
+	for (ret = uclass_find_first_device(UCLASS_BLK, devp);
+	     *devp && !blk_flags_check(*devp, flags);
+	     ret = uclass_find_next_device(devp))
+		return 0;
+
+	return -ENODEV;
+}
+
+int blk_find_next(enum blk_flag_t flags, struct udevice **devp)
+{
+	int ret;
+
+	for (ret = uclass_find_next_device(devp);
+	     *devp && !blk_flags_check(*devp, flags);
+	     ret = uclass_find_next_device(devp))
+		return 0;
+
+	return -ENODEV;
+}
+
 int blk_first_device_err(enum blk_flag_t flags, struct udevice **devp)
 {
 	int ret;
diff --git a/include/blk.h b/include/blk.h
index d06098bc13..dbe9ae219d 100644
--- a/include/blk.h
+++ b/include/blk.h
@@ -731,6 +731,51 @@ int blk_first_device_err(enum blk_flag_t flags, struct udevice **devp);
  */
 int blk_next_device_err(enum blk_flag_t flags, struct udevice **devp);
 
+/**
+ * blk_find_first() - Return the first matching block device
+ * @flags: Indicates type of device to return
+ * @devp:	Returns pointer to device, or NULL on error
+ *
+ * The device is not prepared for use - this is an internal function.
+ * The function uclass_get_device_tail() can be used to probe the device.
+ *
+ * Note that some devices are considered removable until they have been probed
+ *
+ * @return 0 if found, -ENODEV if not found
+ */
+int blk_find_first(enum blk_flag_t flags, struct udevice **devp);
+
+/**
+ * blk_find_next() - Return the next matching block device
+ * @flags: Indicates type of device to return
+ * @devp: On entry, pointer to device to lookup. On exit, returns pointer
+ * to the next device in the same uclass, or NULL if none
+ *
+ * The device is not prepared for use - this is an internal function.
+ * The function uclass_get_device_tail() can be used to probe the device.
+ *
+ * Note that some devices are considered removable until they have been probed
+ *
+ * @return 0 if found, -ENODEV if not found
+ */
+int blk_find_next(enum blk_flag_t flags, struct udevice **devp);
+
+/**
+ * blk_foreach() - iterate through block devices
+ *
+ * This creates a for() loop which works through the available block devices in
+ * order from start to end.
+ *
+ * If for some reason the uclass cannot be found, this does nothing.
+ *
+ * @_flags: Indicates type of device to return
+ * @_pos: struct udevice * to hold the current device. Set to NULL when there
+ * are no more devices.
+ */
+#define blk_foreach(_flags, _pos) \
+	for (int _ret = blk_find_first(_flags, &_pos); !_ret && _pos; \
+	     _ret = blk_find_next(_flags, &_pos))
+
 /**
  * blk_foreach_probe() - Helper function to iteration through block devices
  *
diff --git a/test/dm/blk.c b/test/dm/blk.c
index deccf05289..8556cc7159 100644
--- a/test/dm/blk.c
+++ b/test/dm/blk.c
@@ -217,3 +217,114 @@ static int dm_test_blk_iter(struct unit_test_state *uts)
 	return 0;
 }
 DM_TEST(dm_test_blk_iter, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
+
+/* Test finding fixed/removable block devices */
+static int dm_test_blk_flags(struct unit_test_state *uts)
+{
+	struct udevice *dev;
+
+	/* Iterate through devices without probing them */
+	ut_assertok(blk_find_first(BLKF_BOTH, &dev));
+	ut_assertnonnull(dev);
+	ut_asserteq_str("mmc2.blk", dev->name);
+
+	ut_assertok(blk_find_next(BLKF_BOTH, &dev));
+	ut_assertnonnull(dev);
+	ut_asserteq_str("mmc1.blk", dev->name);
+
+	ut_assertok(blk_find_next(BLKF_BOTH, &dev));
+	ut_assertnonnull(dev);
+	ut_asserteq_str("mmc0.blk", dev->name);
+
+	ut_asserteq(-ENODEV, blk_find_next(BLKF_BOTH, &dev));
+	ut_assertnull(dev);
+
+	/* All devices are removable until probed */
+	ut_asserteq(-ENODEV, blk_find_first(BLKF_FIXED, &dev));
+
+	ut_assertok(blk_find_first(BLKF_REMOVABLE, &dev));
+	ut_assertnonnull(dev);
+	ut_asserteq_str("mmc2.blk", dev->name);
+
+	/* Now probe them and iterate again */
+	ut_assertok(blk_first_device_err(BLKF_BOTH, &dev));
+	ut_assertnonnull(dev);
+	ut_asserteq_str("mmc2.blk", dev->name);
+
+	ut_assertok(blk_next_device_err(BLKF_BOTH, &dev));
+	ut_assertnonnull(dev);
+	ut_asserteq_str("mmc1.blk", dev->name);
+
+	ut_assertok(blk_next_device_err(BLKF_BOTH, &dev));
+	ut_assertnonnull(dev);
+	ut_asserteq_str("mmc0.blk", dev->name);
+
+	ut_asserteq(-ENODEV, blk_next_device_err(BLKF_BOTH, &dev));
+
+	/* Look only for fixed devices */
+	ut_assertok(blk_first_device_err(BLKF_FIXED, &dev));
+	ut_assertnonnull(dev);
+	ut_asserteq_str("mmc2.blk", dev->name);
+
+	ut_asserteq(-ENODEV, blk_next_device_err(BLKF_FIXED, &dev));
+
+	/* Look only for removable devices */
+	ut_assertok(blk_first_device_err(BLKF_REMOVABLE, &dev));
+	ut_assertnonnull(dev);
+	ut_asserteq_str("mmc1.blk", dev->name);
+
+	ut_assertok(blk_next_device_err(BLKF_REMOVABLE, &dev));
+	ut_assertnonnull(dev);
+	ut_asserteq_str("mmc0.blk", dev->name);
+
+	ut_asserteq(-ENODEV, blk_next_device_err(BLKF_REMOVABLE, &dev));
+
+	return 0;
+}
+DM_TEST(dm_test_blk_flags, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
+
+/* Test blk_foreach() and friend */
+static int dm_test_blk_foreach(struct unit_test_state *uts)
+{
+	struct udevice *dev;
+	int found;
+
+	/* Test blk_foreach() - use the 3rd bytes of the name (0/1/2) */
+	found = 0;
+	blk_foreach(BLKF_BOTH, dev)
+		found |= 1 << dectoul(&dev->name[3], NULL);
+	ut_asserteq(7, found);
+
+	/* All devices are removable until probed */
+	found = 0;
+	blk_foreach(BLKF_FIXED, dev)
+		found |= 1 << dectoul(&dev->name[3], NULL);
+	ut_asserteq(0, found);
+
+	found = 0;
+	blk_foreach(BLKF_REMOVABLE, dev)
+		found |= 1 << dectoul(&dev->name[3], NULL);
+	ut_asserteq(7, found);
+
+	/* Now try again with the probing functions */
+	found = 0;
+	blk_foreach_probe(BLKF_BOTH, dev)
+		found |= 1 << dectoul(&dev->name[3], NULL);
+	ut_asserteq(7, found);
+	ut_asserteq(3, blk_count_devices(BLKF_BOTH));
+
+	found = 0;
+	blk_foreach_probe(BLKF_FIXED, dev)
+		found |= 1 << dectoul(&dev->name[3], NULL);
+	ut_asserteq(4, found);
+	ut_asserteq(1, blk_count_devices(BLKF_FIXED));
+
+	found = 0;
+	blk_foreach_probe(BLKF_REMOVABLE, dev)
+		found |= 1 << dectoul(&dev->name[3], NULL);
+	ut_asserteq(3, found);
+	ut_asserteq(2, blk_count_devices(BLKF_REMOVABLE));
+
+	return 0;
+}
+DM_TEST(dm_test_blk_foreach, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
-- 
2.35.1.574.g5d30c73bfb-goog


      parent reply	other threads:[~2022-02-28 19:12 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-02-28 19:08 [PATCH 00/18] Various bugfixes related to verified boot Simon Glass
2022-02-28 19:08 ` [PATCH 01/18] buildman: Update default config to build for sandbox Simon Glass
2022-04-07 13:50   ` Tom Rini
2022-02-28 19:08 ` [PATCH 02/18] buildman: Fix up cfgutil Simon Glass
2022-02-28 19:08 ` [PATCH 03/18] binman: Correct Chromium OS entry types Simon Glass
2022-02-28 19:08 ` [PATCH 04/18] errno: Avoid including strings in SPL Simon Glass
2022-02-28 19:08 ` [PATCH 05/18] abuf: Correct a corner case with abuf_realloc() Simon Glass
2022-02-28 19:08 ` [PATCH 06/18] fdt: Correct condition for SEPARATE_BSS Simon Glass
2022-02-28 19:08 ` [PATCH 07/18] fdt: sandbox: Avoid looking for an appended device tree Simon Glass
2022-02-28 19:08 ` [PATCH 08/18] lzma: Tidy up the function prototype Simon Glass
2022-02-28 19:08 ` [PATCH 09/18] cbfs: Add some more definititions Simon Glass
2022-02-28 19:08 ` [PATCH 10/18] cros_ec: Complete the comment for cros_ec_read_batt_charge() Simon Glass
2022-02-28 19:08 ` [PATCH 11/18] spi: Avoid checking console in SPL Simon Glass
2022-02-28 19:08 ` [PATCH 12/18] disk: Correct the conditions for SPL Simon Glass
2022-02-28 19:08 ` [PATCH 13/18] Add a default for TPL_TEXT_BASE Simon Glass
2022-02-28 19:08 ` [PATCH 14/18] Make ASYMMETRIC_KEY_TYPE depend on FIT_SIGNATURE Simon Glass
2022-03-01  4:22   ` AKASHI Takahiro
2022-03-12  2:25     ` Simon Glass
2022-03-14  1:59       ` AKASHI Takahiro
2022-02-28 19:08 ` [PATCH 15/18] stdint: Add a definition of UINT8_MAX Simon Glass
2022-02-28 19:08 ` [PATCH 16/18] dm: core: Add a required struct declaration Simon Glass
2022-02-28 19:08 ` [PATCH 17/18] dm: core: Tidy up comments in uclass headers Simon Glass
2022-02-28 19:08 ` Simon Glass [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220228190835.1480772-19-sjg@chromium.org \
    --to=sjg@chromium.org \
    --cc=marex@denx.de \
    --cc=masahisa.kojima@linaro.org \
    --cc=morpheus.ibis@gmail.com \
    --cc=patrick.delaunay@foss.st.com \
    --cc=takahiro.akashi@linaro.org \
    --cc=trini@konsulko.com \
    --cc=u-boot@lists.denx.de \
    --cc=xypron.glpk@gmx.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox