mirror of
https://github.com/u-boot/u-boot.git
synced 2024-11-29 15:43:44 +08:00
dm: blk: Add a legacy block interface
There is quite a bit of duplicated common code related to block devices in the IDE and SCSI implementations. Create some helper functions that can be used to reduce the duplication. These rely on a linker list of interface-type drivers Signed-off-by: Simon Glass <sjg@chromium.org>
This commit is contained in:
parent
c649e3c91c
commit
6eef6eac1f
@ -7,6 +7,10 @@
|
||||
|
||||
obj-$(CONFIG_BLK) += blk-uclass.o
|
||||
|
||||
ifndef CONFIG_BLK
|
||||
obj-y += blk_legacy.o
|
||||
endif
|
||||
|
||||
obj-$(CONFIG_AHCI) += ahci-uclass.o
|
||||
obj-$(CONFIG_SCSI_AHCI) += ahci.o
|
||||
obj-$(CONFIG_DWC_AHSATA) += dwc_ahsata.o
|
||||
|
261
drivers/block/blk_legacy.c
Normal file
261
drivers/block/blk_legacy.c
Normal file
@ -0,0 +1,261 @@
|
||||
/*
|
||||
* Copyright (C) 2016 Google, Inc
|
||||
* Written by Simon Glass <sjg@chromium.org>
|
||||
*
|
||||
* SPDX-License-Identifier: GPL-2.0+
|
||||
*/
|
||||
|
||||
#include <common.h>
|
||||
#include <linux/err.h>
|
||||
|
||||
struct blk_driver *blk_driver_lookup_type(int if_type)
|
||||
{
|
||||
struct blk_driver *drv = ll_entry_start(struct blk_driver, blk_driver);
|
||||
const int n_ents = ll_entry_count(struct blk_driver, blk_driver);
|
||||
struct blk_driver *entry;
|
||||
|
||||
for (entry = drv; entry != drv + n_ents; entry++) {
|
||||
if (if_type == entry->if_type)
|
||||
return entry;
|
||||
}
|
||||
|
||||
/* Not found */
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static struct blk_driver *blk_driver_lookup_typename(const char *if_typename)
|
||||
{
|
||||
struct blk_driver *drv = ll_entry_start(struct blk_driver, blk_driver);
|
||||
const int n_ents = ll_entry_count(struct blk_driver, blk_driver);
|
||||
struct blk_driver *entry;
|
||||
|
||||
for (entry = drv; entry != drv + n_ents; entry++) {
|
||||
if (!strcmp(if_typename, entry->if_typename))
|
||||
return entry;
|
||||
}
|
||||
|
||||
/* Not found */
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/**
|
||||
* get_desc() - Get the block device descriptor for the given device number
|
||||
*
|
||||
* @drv: Legacy block driver
|
||||
* @devnum: Device number (0 = first)
|
||||
* @descp: Returns block device descriptor on success
|
||||
* @return 0 on success, -ENODEV if there is no such device, -ENOSYS if the
|
||||
* driver does not provide a way to find a device, or other -ve on other
|
||||
* error.
|
||||
*/
|
||||
static int get_desc(struct blk_driver *drv, int devnum, struct blk_desc **descp)
|
||||
{
|
||||
if (drv->desc) {
|
||||
if (devnum < 0 || devnum >= drv->max_devs)
|
||||
return -ENODEV;
|
||||
*descp = &drv->desc[devnum];
|
||||
return 0;
|
||||
}
|
||||
if (!drv->get_dev)
|
||||
return -ENOSYS;
|
||||
|
||||
return drv->get_dev(devnum, descp);
|
||||
}
|
||||
|
||||
#ifdef HAVE_BLOCK_DEVICE
|
||||
int blk_list_part(enum if_type if_type)
|
||||
{
|
||||
struct blk_driver *drv;
|
||||
struct blk_desc *desc;
|
||||
int devnum, ok;
|
||||
bool first = true;
|
||||
|
||||
drv = blk_driver_lookup_type(if_type);
|
||||
if (!drv)
|
||||
return -ENOSYS;
|
||||
for (ok = 0, devnum = 0; devnum < drv->max_devs; ++devnum) {
|
||||
if (get_desc(drv, devnum, &desc))
|
||||
continue;
|
||||
if (desc->part_type != PART_TYPE_UNKNOWN) {
|
||||
++ok;
|
||||
if (!first)
|
||||
putc('\n');
|
||||
part_print(desc);
|
||||
first = false;
|
||||
}
|
||||
}
|
||||
if (!ok)
|
||||
return -ENODEV;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int blk_print_part_devnum(enum if_type if_type, int devnum)
|
||||
{
|
||||
struct blk_driver *drv = blk_driver_lookup_type(if_type);
|
||||
struct blk_desc *desc;
|
||||
int ret;
|
||||
|
||||
if (!drv)
|
||||
return -ENOSYS;
|
||||
ret = get_desc(drv, devnum, &desc);
|
||||
if (ret)
|
||||
return ret;
|
||||
if (desc->type == DEV_TYPE_UNKNOWN)
|
||||
return -ENOENT;
|
||||
part_print(desc);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void blk_list_devices(enum if_type if_type)
|
||||
{
|
||||
struct blk_driver *drv = blk_driver_lookup_type(if_type);
|
||||
struct blk_desc *desc;
|
||||
int i;
|
||||
|
||||
if (!drv)
|
||||
return;
|
||||
for (i = 0; i < drv->max_devs; ++i) {
|
||||
if (get_desc(drv, i, &desc))
|
||||
continue;
|
||||
if (desc->type == DEV_TYPE_UNKNOWN)
|
||||
continue; /* list only known devices */
|
||||
printf("Device %d: ", i);
|
||||
dev_print(desc);
|
||||
}
|
||||
}
|
||||
|
||||
int blk_print_device_num(enum if_type if_type, int devnum)
|
||||
{
|
||||
struct blk_driver *drv = blk_driver_lookup_type(if_type);
|
||||
struct blk_desc *desc;
|
||||
int ret;
|
||||
|
||||
if (!drv)
|
||||
return -ENOSYS;
|
||||
ret = get_desc(drv, devnum, &desc);
|
||||
if (ret)
|
||||
return ret;
|
||||
printf("\n%s device %d: ", drv->if_typename, devnum);
|
||||
dev_print(desc);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int blk_show_device(enum if_type if_type, int devnum)
|
||||
{
|
||||
struct blk_driver *drv = blk_driver_lookup_type(if_type);
|
||||
struct blk_desc *desc;
|
||||
int ret;
|
||||
|
||||
if (!drv)
|
||||
return -ENOSYS;
|
||||
printf("\nDevice %d: ", devnum);
|
||||
if (devnum >= drv->max_devs) {
|
||||
puts("unknown device\n");
|
||||
return -ENODEV;
|
||||
}
|
||||
ret = get_desc(drv, devnum, &desc);
|
||||
if (ret)
|
||||
return ret;
|
||||
dev_print(desc);
|
||||
|
||||
if (desc->type == DEV_TYPE_UNKNOWN)
|
||||
return -ENOENT;
|
||||
|
||||
return 0;
|
||||
}
|
||||
#endif /* HAVE_BLOCK_DEVICE */
|
||||
|
||||
struct blk_desc *blk_get_devnum_by_type(enum if_type if_type, int devnum)
|
||||
{
|
||||
struct blk_driver *drv = blk_driver_lookup_type(if_type);
|
||||
struct blk_desc *desc;
|
||||
|
||||
if (!drv)
|
||||
return NULL;
|
||||
|
||||
if (get_desc(drv, devnum, &desc))
|
||||
return NULL;
|
||||
|
||||
return desc;
|
||||
}
|
||||
|
||||
int blk_dselect_hwpart(struct blk_desc *desc, int hwpart)
|
||||
{
|
||||
struct blk_driver *drv = blk_driver_lookup_type(desc->if_type);
|
||||
|
||||
if (!drv)
|
||||
return -ENOSYS;
|
||||
if (drv->select_hwpart)
|
||||
return drv->select_hwpart(desc, hwpart);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
struct blk_desc *blk_get_devnum_by_typename(const char *if_typename, int devnum)
|
||||
{
|
||||
struct blk_driver *drv = blk_driver_lookup_typename(if_typename);
|
||||
struct blk_desc *desc;
|
||||
|
||||
if (!drv)
|
||||
return NULL;
|
||||
|
||||
if (get_desc(drv, devnum, &desc))
|
||||
return NULL;
|
||||
|
||||
return desc;
|
||||
}
|
||||
|
||||
ulong blk_read_devnum(enum if_type if_type, int devnum, lbaint_t start,
|
||||
lbaint_t blkcnt, void *buffer)
|
||||
{
|
||||
struct blk_driver *drv = blk_driver_lookup_type(if_type);
|
||||
struct blk_desc *desc;
|
||||
ulong n;
|
||||
int ret;
|
||||
|
||||
if (!drv)
|
||||
return -ENOSYS;
|
||||
ret = get_desc(drv, devnum, &desc);
|
||||
if (ret)
|
||||
return ret;
|
||||
n = desc->block_read(desc, start, blkcnt, buffer);
|
||||
if (IS_ERR_VALUE(n))
|
||||
return n;
|
||||
|
||||
/* flush cache after read */
|
||||
flush_cache((ulong)buffer, blkcnt * desc->blksz);
|
||||
|
||||
return n;
|
||||
}
|
||||
|
||||
ulong blk_write_devnum(enum if_type if_type, int devnum, lbaint_t start,
|
||||
lbaint_t blkcnt, const void *buffer)
|
||||
{
|
||||
struct blk_driver *drv = blk_driver_lookup_type(if_type);
|
||||
struct blk_desc *desc;
|
||||
int ret;
|
||||
|
||||
if (!drv)
|
||||
return -ENOSYS;
|
||||
ret = get_desc(drv, devnum, &desc);
|
||||
if (ret)
|
||||
return ret;
|
||||
return desc->block_write(desc, start, blkcnt, buffer);
|
||||
}
|
||||
|
||||
int blk_select_hwpart_devnum(enum if_type if_type, int devnum, int hwpart)
|
||||
{
|
||||
struct blk_driver *drv = blk_driver_lookup_type(if_type);
|
||||
struct blk_desc *desc;
|
||||
int ret;
|
||||
|
||||
if (!drv)
|
||||
return -ENOSYS;
|
||||
ret = get_desc(drv, devnum, &desc);
|
||||
if (ret)
|
||||
return ret;
|
||||
return drv->select_hwpart(desc, hwpart);
|
||||
}
|
195
include/blk.h
195
include/blk.h
@ -340,6 +340,201 @@ static inline ulong blk_derase(struct blk_desc *block_dev, lbaint_t start,
|
||||
blkcache_invalidate(block_dev->if_type, block_dev->devnum);
|
||||
return block_dev->block_erase(block_dev, start, blkcnt);
|
||||
}
|
||||
|
||||
/**
|
||||
* struct blk_driver - Driver for block interface types
|
||||
*
|
||||
* This provides access to the block devices for each interface type. One
|
||||
* driver should be provided using U_BOOT_LEGACY_BLK() for each interface
|
||||
* type that is to be supported.
|
||||
*
|
||||
* @if_typename: Interface type name
|
||||
* @if_type: Interface type
|
||||
* @max_devs: Maximum number of devices supported
|
||||
* @desc: Pointer to list of devices for this interface type,
|
||||
* or NULL to use @get_dev() instead
|
||||
*/
|
||||
struct blk_driver {
|
||||
const char *if_typename;
|
||||
enum if_type if_type;
|
||||
int max_devs;
|
||||
struct blk_desc *desc;
|
||||
/**
|
||||
* get_dev() - get a pointer to a block device given its number
|
||||
*
|
||||
* Each interface allocates its own devices and typically
|
||||
* struct blk_desc is contained with the interface's data structure.
|
||||
* There is no global numbering for block devices. This method allows
|
||||
* the device for an interface type to be obtained when @desc is NULL.
|
||||
*
|
||||
* @devnum: Device number (0 for first device on that interface,
|
||||
* 1 for second, etc.
|
||||
* @descp: Returns pointer to the block device on success
|
||||
* @return 0 if OK, -ve on error
|
||||
*/
|
||||
int (*get_dev)(int devnum, struct blk_desc **descp);
|
||||
|
||||
/**
|
||||
* select_hwpart() - Select a hardware partition
|
||||
*
|
||||
* Some devices (e.g. MMC) can support partitioning at the hardware
|
||||
* level. This is quite separate from the normal idea of
|
||||
* software-based partitions. MMC hardware partitions must be
|
||||
* explicitly selected. Once selected only the region of the device
|
||||
* covered by that partition is accessible.
|
||||
*
|
||||
* The MMC standard provides for two boot partitions (numbered 1 and 2),
|
||||
* rpmb (3), and up to 4 addition general-purpose partitions (4-7).
|
||||
* Partition 0 is the main user-data partition.
|
||||
*
|
||||
* @desc: Block device descriptor
|
||||
* @hwpart: Hardware partition number to select. 0 means the main
|
||||
* user-data partition, 1 is the first partition, 2 is
|
||||
* the second, etc.
|
||||
* @return 0 if OK, other value for an error
|
||||
*/
|
||||
int (*select_hwpart)(struct blk_desc *desc, int hwpart);
|
||||
};
|
||||
|
||||
/*
|
||||
* Declare a new U-Boot legacy block driver. New drivers should use driver
|
||||
* model (UCLASS_BLK).
|
||||
*/
|
||||
#define U_BOOT_LEGACY_BLK(__name) \
|
||||
ll_entry_declare(struct blk_driver, __name, blk_driver)
|
||||
|
||||
struct blk_driver *blk_driver_lookup_type(int if_type);
|
||||
|
||||
#endif /* !CONFIG_BLK */
|
||||
|
||||
/**
|
||||
* blk_get_devnum_by_typename() - Get a block device by type and number
|
||||
*
|
||||
* This looks through the available block devices of the given type, returning
|
||||
* the one with the given @devnum.
|
||||
*
|
||||
* @if_type: Block device type
|
||||
* @devnum: Device number
|
||||
* @return point to block device descriptor, or NULL if not found
|
||||
*/
|
||||
struct blk_desc *blk_get_devnum_by_type(enum if_type if_type, int devnum);
|
||||
|
||||
/**
|
||||
* blk_get_devnum_by_type() - Get a block device by type name, and number
|
||||
*
|
||||
* This looks up the block device type based on @if_typename, then calls
|
||||
* blk_get_devnum_by_type().
|
||||
*
|
||||
* @if_typename: Block device type name
|
||||
* @devnum: Device number
|
||||
* @return point to block device descriptor, or NULL if not found
|
||||
*/
|
||||
struct blk_desc *blk_get_devnum_by_typename(const char *if_typename,
|
||||
int devnum);
|
||||
|
||||
/**
|
||||
* blk_dselect_hwpart() - select a hardware partition
|
||||
*
|
||||
* This selects a hardware partition (such as is supported by MMC). The block
|
||||
* device size may change as this effectively points the block device to a
|
||||
* partition at the hardware level. See the select_hwpart() method above.
|
||||
*
|
||||
* @desc: Block device descriptor for the device to select
|
||||
* @hwpart: Partition number to select
|
||||
* @return 0 if OK, -ve on error
|
||||
*/
|
||||
int blk_dselect_hwpart(struct blk_desc *desc, int hwpart);
|
||||
|
||||
/**
|
||||
* blk_list_part() - list the partitions for block devices of a given type
|
||||
*
|
||||
* This looks up the partition type for each block device of type @if_type,
|
||||
* then displays a list of partitions.
|
||||
*
|
||||
* @if_type: Block device type
|
||||
* @return 0 if OK, -ENODEV if there is none of that type
|
||||
*/
|
||||
int blk_list_part(enum if_type if_type);
|
||||
|
||||
/**
|
||||
* blk_list_devices() - list the block devices of a given type
|
||||
*
|
||||
* This lists each block device of the type @if_type, showing the capacity
|
||||
* as well as type-specific information.
|
||||
*
|
||||
* @if_type: Block device type
|
||||
*/
|
||||
void blk_list_devices(enum if_type if_type);
|
||||
|
||||
/**
|
||||
* blk_show_device() - show information about a given block device
|
||||
*
|
||||
* This shows the block device capacity as well as type-specific information.
|
||||
*
|
||||
* @if_type: Block device type
|
||||
* @devnum: Device number
|
||||
* @return 0 if OK, -ENODEV for invalid device number
|
||||
*/
|
||||
int blk_show_device(enum if_type if_type, int devnum);
|
||||
|
||||
/**
|
||||
* blk_print_device_num() - show information about a given block device
|
||||
*
|
||||
* This is similar to blk_show_device() but returns an error if the block
|
||||
* device type is unknown.
|
||||
*
|
||||
* @if_type: Block device type
|
||||
* @devnum: Device number
|
||||
* @return 0 if OK, -ENODEV for invalid device number, -ENOENT if the block
|
||||
* device is not connected
|
||||
*/
|
||||
int blk_print_device_num(enum if_type if_type, int devnum);
|
||||
|
||||
/**
|
||||
* blk_print_part_devnum() - print the partition information for a device
|
||||
*
|
||||
* @if_type: Block device type
|
||||
* @devnum: Device number
|
||||
* @return 0 if OK, -ENOENT if the block device is not connected, -ENOSYS if
|
||||
* the interface type is not supported, other -ve on other error
|
||||
*/
|
||||
int blk_print_part_devnum(enum if_type if_type, int devnum);
|
||||
|
||||
/**
|
||||
* blk_read_devnum() - read blocks from a device
|
||||
*
|
||||
* @if_type: Block device type
|
||||
* @devnum: Device number
|
||||
* @blkcnt: Number of blocks to read
|
||||
* @buffer: Address to write data to
|
||||
* @return number of blocks read, or -ve error number on error
|
||||
*/
|
||||
ulong blk_read_devnum(enum if_type if_type, int devnum, lbaint_t start,
|
||||
lbaint_t blkcnt, void *buffer);
|
||||
|
||||
/**
|
||||
* blk_write_devnum() - write blocks to a device
|
||||
*
|
||||
* @if_type: Block device type
|
||||
* @devnum: Device number
|
||||
* @blkcnt: Number of blocks to write
|
||||
* @buffer: Address to read data from
|
||||
* @return number of blocks written, or -ve error number on error
|
||||
*/
|
||||
ulong blk_write_devnum(enum if_type if_type, int devnum, lbaint_t start,
|
||||
lbaint_t blkcnt, const void *buffer);
|
||||
|
||||
/**
|
||||
* blk_select_hwpart_devnum() - select a hardware partition
|
||||
*
|
||||
* This is similar to blk_dselect_hwpart() but it looks up the interface and
|
||||
* device number.
|
||||
*
|
||||
* @if_type: Block device type
|
||||
* @devnum: Device number
|
||||
* @hwpart: Partition number to select
|
||||
* @return 0 if OK, -ve on error
|
||||
*/
|
||||
int blk_select_hwpart_devnum(enum if_type if_type, int devnum, int hwpart);
|
||||
|
||||
#endif
|
||||
|
Loading…
Reference in New Issue
Block a user