#ifndef BLK_H
#define BLK_H
+#include <bouncebuf.h>
#include <dm/uclass-id.h>
#include <efi.h>
#define LBAF "%" LBAFlength "x"
#define LBAFU "%" LBAFlength "u"
+#define DEFAULT_BLKSZ 512
+
struct udevice;
static inline bool blk_enabled(void)
unsigned char hwpart; /* HW partition, e.g. for eMMC */
unsigned char type; /* device type */
unsigned char removable; /* removable device */
-#ifdef CONFIG_LBA48
/* device can use 48bit addr (ATA/ATAPI v7) */
- unsigned char lba48;
-#endif
+ bool lba48;
+ unsigned char atapi; /* Use ATAPI protocol */
+ unsigned char bb; /* Use bounce buffer */
lbaint_t lba; /* number of blocks */
unsigned long blksz; /* block size */
int log2blksz; /* for convenience: log2(blksz) */
(PAD_SIZE(size, blk_desc->blksz))
#if CONFIG_IS_ENABLED(BLOCK_CACHE)
-
-/**
- * blkcache_init() - initialize the block cache list pointers
- */
-int blkcache_init(void);
-
/**
* blkcache_read() - attempt to read a set of blocks from cache
*
* 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);
*/
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,
static inline void blkcache_invalidate(int iftype, int dev) {}
+static inline void blkcache_free(void) {}
+
#endif
-#if CONFIG_IS_ENABLED(BLK)
struct udevice;
/* Operations on block devices */
* @return 0 if OK, -ve on error
*/
int (*select_hwpart)(struct udevice *dev, int hwpart);
+
+#if IS_ENABLED(CONFIG_BOUNCE_BUFFER)
+ /**
+ * buffer_aligned() - test memory alignment of block operation buffer
+ *
+ * Some devices have limited DMA capabilities and require that the
+ * buffers passed to them fit specific properties. This optional
+ * callback can be used to indicate whether a buffer alignment is
+ * suitable for the device DMA or not, and trigger use of generic
+ * bounce buffer implementation to help use of unsuitable buffers
+ * at the expense of performance degradation.
+ *
+ * @dev: Block device associated with the request
+ * @state: Bounce buffer state
+ * @return 1 if OK, 0 if unaligned
+ */
+ int (*buffer_aligned)(struct udevice *dev, struct bounce_buffer *state);
+#endif /* CONFIG_BOUNCE_BUFFER */
};
-#define blk_get_ops(dev) ((struct blk_ops *)(dev)->driver->ops)
+#if CONFIG_IS_ENABLED(BLK)
/*
* These functions should take struct udevice instead of struct blk_desc,
unsigned long blk_derase(struct blk_desc *block_dev, lbaint_t start,
lbaint_t blkcnt);
+#endif /* BLK */
+
/**
* blk_read() - Read from a block device
*
*/
int blk_next_device(struct udevice **devp);
-/**
- * blk_create_device() - Create a new block device
- *
- * @parent: Parent of the new device
- * @drv_name: Driver name to use for the block device
- * @name: Name for the device
- * @uclass_id: Interface type (enum uclass_id_t)
- * @devnum: Device number, specific to the interface type, or -1 to
- * allocate the next available number
- * @blksz: Block size of the device in bytes (typically 512)
- * @lba: Total number of blocks of the device
- * @devp: the new device (which has not been probed)
- */
-int blk_create_device(struct udevice *parent, const char *drv_name,
- const char *name, int uclass_id, int devnum, int blksz,
- lbaint_t lba, struct udevice **devp);
-
/**
* blk_create_devicef() - Create a new named block device
*
*/
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);
*/
struct blk_desc *blk_get_by_device(struct udevice *dev);
-#else
+/**
+ * blk_get_desc() - Get the block device descriptor for the given device number
+ *
+ * @uclass_id: Interface type
+ * @devnum: Device number (0 = first)
+ * @descp: Returns block device descriptor on success
+ * Return: 0 on success, -ENODEV if there is no such device and no device
+ * with a higher device number, -ENOENT if there is no such device but there
+ * is one with a higher number, or other -ve on other error.
+ */
+int blk_get_desc(enum uclass_id uclass_id, int devnum, struct blk_desc **descp);
+
+#if !CONFIG_IS_ENABLED(BLK)
+
#include <errno.h>
+
/*
* These functions should take struct udevice instead of struct blk_desc,
* but this is convenient for migration to driver model. Add a 'd' prefix
struct blk_desc *blk_get_devnum_by_uclass_id(enum uclass_id uclass_id, int devnum);
/**
- * blk_get_devnum_by_uclass_id() - Get a block device by type name, and number
+ * blk_get_devnum_by_uclass_idname() - Get block device by type name and number
*
* This looks up the block device type based on @uclass_idname, then calls
* blk_get_devnum_by_uclass_id().
* Return: point to block device descriptor, or NULL if not found
*/
struct blk_desc *blk_get_devnum_by_uclass_idname(const char *uclass_idname,
- int devnum);
+ int devnum);
/**
* blk_dselect_hwpart() - select a hardware partition
*/
int blk_print_part_devnum(enum uclass_id uclass_id, int devnum);
-/**
- * blk_read_devnum() - read blocks from a device
- *
- * @uclass_id: Block device type
- * @devnum: Device number
- * @start: Start block number to read (0=first)
- * @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 uclass_id uclass_id, int devnum, lbaint_t start,
- lbaint_t blkcnt, void *buffer);
-
-/**
- * blk_write_devnum() - write blocks to a device
- *
- * @uclass_id: Block device type
- * @devnum: Device number
- * @start: Start block number to write (0=first)
- * @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 uclass_id uclass_id, int devnum, lbaint_t start,
- lbaint_t blkcnt, const void *buffer);
-
/**
* blk_select_hwpart_devnum() - select a hardware partition
*