Hi Alexey, On 2026-09-18T06:35:41, Alexey Charkov <[email protected]> wrote: > scsi: add a lookup by target and LUN > > SCSI device numbers are handed out in scan order, so a caller has no > general way to request a particular logical unit without relying on > assumptions on the scan order. Well known units make this worse, since > they are probed after a range whose population varies from device to > device, even though their numbers are by definition well known. > > Add a helper to unambiguously look up the block device number for a given > SCSI target number and LUN.
This is only unambiguous if there is a single SCSI controller. Device numbers are shared across all of UCLASS_SCSI, so with two UFS hosts (or UFS plus AHCI) the same target/LUN pair appears more than once and this returns whichever was scanned first. Also, the helper provides the blk descriptor rather than the device number. Please can you update the commit message to match what the function does. > > Signed-off-by: Alexey Charkov <[email protected]> > > drivers/scsi/scsi-uclass.c | 19 +++++++++++++++++++ > include/scsi.h | 16 ++++++++++++++++ > 2 files changed, 35 insertions(+) > diff --git a/drivers/scsi/scsi-uclass.c b/drivers/scsi/scsi-uclass.c > @@ -50,6 +50,25 @@ int scsi_get_blk_by_uuid(const char *uuid, > +int scsi_get_blk_by_lun(int target, int lun, struct blk_desc **blk_desc_ptr) > +{ > + struct blk_desc *blk; > + int i, ret; > + > + for (i = 0; i < blk_find_max_devnum(UCLASS_SCSI) + 1; i++) { > + ret = blk_get_desc(UCLASS_SCSI, i, &blk); > + if (ret) > + continue; I suspect this should take the SCSI controller as its first argument, since the target and LUN only mean something relative to a bus. The block devices are children of the controller (see do_scsi_scan_one()), so the lookup becomes something like: device_foreach_child(blk, dev) { struct blk_desc *desc; if (device_get_uclass_id(blk) != UCLASS_BLK) continue; desc = dev_get_uclass_plat(blk); if (desc->target == target && desc->lun == lun) { ... That also avoids calling blk_find_max_devnum() on every iteration and walking the whole BLK uclass again in blk_get_desc() for each device number. I see this is copied from scsi_get_blk_by_uuid() but it is not a great pattern to repeat. The caller in the SPL patch would then need to find the controller, which is a bit more work, but it does make it clear which UFS device is being booted. What do you think? > diff --git a/include/scsi.h b/include/scsi.h > @@ -395,6 +395,22 @@ int scsi_get_blk_by_uuid(const char *uuid, struct > blk_desc **blk_desc_ptr, > + * @target: Target id the unit sits on > + * @lun: Logical unit number to look for > + * @blk_desc_ptr: Provides the blk descriptor > + * Return: 0 if OK, -ve on error Please can you mention -ENODEV here, for when no unit matches, since that is the only error returned. Please can you also add a test for this function in test/dm/scsi.c? The sandbox controller has two targets and three LUNs, so it should be easy to check a unit which is present and one which is not. Regards, Simon
