Back to home page

LXR

 
 

    


File indexing completed on 2025-05-11 08:24:13

0001 /**
0002  * @file
0003  *
0004  * @brief Block Device Disk Management API
0005  *
0006  * @ingroup rtems_disk
0007  */
0008 
0009 /*
0010  * Copyright (C) 2001 OKTET Ltd., St.-Petersburg, Russia
0011  * Author: Victor V. Vengerov <vvv@oktet.ru>
0012  */
0013 
0014 #ifndef _RTEMS_DISKDEVS_H
0015 #define _RTEMS_DISKDEVS_H
0016 
0017 #include <rtems.h>
0018 #include <rtems/libio.h>
0019 #include <rtems/chain.h>
0020 
0021 #ifdef __cplusplus
0022 extern "C" {
0023 #endif
0024 
0025 typedef struct rtems_disk_device rtems_disk_device;
0026 
0027 /**
0028  * @defgroup rtems_disk Block Device Disk Management
0029  *
0030  * @ingroup rtems_libblock
0031  *
0032  * @brief This module provides functions to manage disk devices.
0033  *
0034  * A disk is a set of blocks which are identified by a consecutive set of
0035  * non-negative integers starting at zero.  There are also logical disks which
0036  * contain a subset of consecutive disk blocks.  The logical disks are used to
0037  * represent the partitions of a disk.  The disk devices are accessed via the
0038  * @ref rtems_bdbuf "block device buffer module".
0039  */
0040 /**@{**/
0041 
0042 /**
0043  * @brief Block device block index type.
0044  */
0045 typedef uint32_t rtems_blkdev_bnum;
0046 
0047 /**
0048  * @brief Block device IO control handler type.
0049  */
0050 typedef int (*rtems_block_device_ioctl)(
0051   rtems_disk_device *dd,
0052   uint32_t req,
0053   void *argp
0054 );
0055 
0056 /**
0057  * @brief Trigger value to disable further read-ahead requests.
0058  */
0059 #define RTEMS_DISK_READ_AHEAD_NO_TRIGGER ((rtems_blkdev_bnum) -1)
0060 
0061 /**
0062  * @brief Size value to set number of blocks based on config and disk size.
0063  */
0064 #define RTEMS_DISK_READ_AHEAD_SIZE_AUTO (0)
0065 
0066 /**
0067  * @brief Block device read-ahead control.
0068  */
0069 typedef struct {
0070   /**
0071    * @brief Chain node for the read-ahead request queue of the read-ahead task.
0072    */
0073   rtems_chain_node node;
0074 
0075   /**
0076    * @brief Block value to trigger the read-ahead request.
0077    *
0078    * A value of @ref RTEMS_DISK_READ_AHEAD_NO_TRIGGER will disable further
0079    * read-ahead requests (except the ones triggered by @a rtems_bdbuf_peek)
0080    * since no valid block can have this value.
0081    */
0082   rtems_blkdev_bnum trigger;
0083 
0084   /**
0085    * @brief Start block for the next read-ahead request.
0086    *
0087    * In case the trigger value is out of range of valid blocks, this value my
0088    * be arbitrary.
0089    */
0090   rtems_blkdev_bnum next;
0091 
0092   /**
0093    * @brief Size of the next read-ahead request in blocks.
0094    *
0095    * A value of @ref RTEMS_DISK_READ_AHEAD_SIZE_AUTO will try to read the rest
0096    * of the disk but at most the configured max_read_ahead_blocks.
0097    */
0098   uint32_t nr_blocks;
0099 } rtems_blkdev_read_ahead;
0100 
0101 /**
0102  * @brief Block device statistics.
0103  *
0104  * Integer overflows in the statistic counters may happen.
0105  */
0106 typedef struct {
0107   /**
0108    * @brief Read hit count.
0109    *
0110    * A read hit occurs in the rtems_bdbuf_read() function in case the block is
0111    * in the cached or modified state.
0112    */
0113   uint32_t read_hits;
0114 
0115   /**
0116    * @brief Read miss count.
0117    *
0118    * A read miss occurs in the rtems_bdbuf_read() function in case the block is
0119    * in the empty state and a read transfer must be initiated to read the data
0120    * from the device.
0121    */
0122   uint32_t read_misses;
0123 
0124   /**
0125    * @brief Read-ahead transfer count.
0126    *
0127    * Each read-ahead transfer may read multiple blocks. This counts all
0128    * transfers (including peeks).
0129    */
0130   uint32_t read_ahead_transfers;
0131 
0132   /**
0133    * @brief Read-ahead transfers caused by a peek.
0134    */
0135   uint32_t read_ahead_peeks;
0136 
0137   /**
0138    * @brief Count of blocks transfered from the device.
0139    */
0140   uint32_t read_blocks;
0141 
0142   /**
0143    * @brief Read error count.
0144    *
0145    * Error count of transfers issued by the read or read-ahead requests.
0146    */
0147   uint32_t read_errors;
0148 
0149   /**
0150    * @brief Write transfer count.
0151    *
0152    * Each write transfer may write multiple blocks.
0153    */
0154   uint32_t write_transfers;
0155 
0156   /**
0157    * @brief Count of blocks transfered to the device.
0158    */
0159   uint32_t write_blocks;
0160 
0161   /**
0162    * @brief Write error count.
0163    *
0164    * Error count of transfers issued by write requests.
0165    */
0166   uint32_t write_errors;
0167 } rtems_blkdev_stats;
0168 
0169 /**
0170  * @brief Description of a disk device (logical and physical disks).
0171  *
0172  * An array of pointer tables to rtems_disk_device structures is maintained.
0173  * The first table will be indexed by the major number and the second table
0174  * will be indexed by the minor number.  This allows quick lookup using a data
0175  * structure of moderated size.
0176  */
0177 struct rtems_disk_device {
0178   /**
0179    * @brief Device identifier (concatenation of major and minor number).
0180    */
0181   dev_t dev;
0182 
0183   /**
0184    * @brief Physical device identifier (equals the @c dev entry if it specifies a
0185    * physical device).
0186    */
0187   rtems_disk_device *phys_dev;
0188 
0189   /**
0190    * @brief Driver capabilities.
0191    */
0192   uint32_t capabilities;
0193 
0194   /**
0195    * @brief Disk device name.
0196    */
0197   char *name;
0198 
0199   /**
0200    * @brief Usage counter.
0201    *
0202    * Devices cannot be deleted if they are in use.
0203    */
0204   unsigned uses;
0205 
0206   /**
0207    * @brief Start media block number.
0208    *
0209    * Equals zero for physical devices.  It is a media block offset to the
0210    * related physical device for logical device.
0211    */
0212   rtems_blkdev_bnum start;
0213 
0214   /**
0215    * @brief Size of the physical or logical disk in media blocks.
0216    */
0217   rtems_blkdev_bnum size;
0218 
0219   /**
0220    * @brief Media block size in bytes.
0221    *
0222    * This is the media transfer unit the hardware defaults to.
0223    */
0224   uint32_t media_block_size;
0225 
0226   /**
0227    * @brief Block size in bytes.
0228    *
0229    * This is the minimum transfer unit.  It may be a multiple of the media
0230    * block size. It must be positive.
0231    *
0232    * @see rtems_bdbuf_set_block_size().
0233    */
0234   uint32_t block_size;
0235 
0236   /**
0237    * @brief Block count.
0238    *
0239    * @see rtems_bdbuf_set_block_size().
0240    */
0241   rtems_blkdev_bnum block_count;
0242 
0243   /**
0244    * @brief Media blocks per device blocks.
0245    *
0246    * @see rtems_bdbuf_set_block_size().
0247    */
0248   uint32_t media_blocks_per_block;
0249 
0250   /**
0251    * @brief Block to media block shift.
0252    *
0253    * In case this value is non-negative the media block of a block can be
0254    * calculated as media block = block << block_to_media_block_shift, otherwise
0255    * a 64-bit operation will be used.
0256    *
0257    * @see rtems_bdbuf_set_block_size().
0258    */
0259   int block_to_media_block_shift;
0260 
0261   /**
0262    * @brief Buffer descriptors per group count.
0263    *
0264    * @see rtems_bdbuf_set_block_size().
0265    */
0266   size_t bds_per_group;
0267 
0268   /**
0269    * @brief IO control handler for this disk.
0270    */
0271   rtems_block_device_ioctl ioctl;
0272 
0273   /**
0274    * @brief Private data for the disk driver.
0275    */
0276   void *driver_data;
0277 
0278   /**
0279    * @brief Indicates that this disk should be deleted as soon as the last user
0280    * releases this disk.
0281    */
0282   bool deleted;
0283 
0284   /**
0285    * @brief Device statistics for this disk.
0286    */
0287   rtems_blkdev_stats stats;
0288 
0289   /**
0290    * @brief Read-ahead control for this disk.
0291    */
0292   rtems_blkdev_read_ahead read_ahead;
0293 };
0294 
0295 /**
0296  * @name Disk Device Data
0297  */
0298 /**@{**/
0299 
0300 /* Use fstat() instead */
0301 RTEMS_DEPRECATED static inline dev_t rtems_disk_get_device_identifier(
0302   const rtems_disk_device *dd
0303 )
0304 {
0305   return dd->dev;
0306 }
0307 
0308 /* Use fstat() instead */
0309 RTEMS_DEPRECATED static inline rtems_device_major_number rtems_disk_get_major_number(
0310   const rtems_disk_device *dd
0311 )
0312 {
0313   return rtems_filesystem_dev_major_t(dd->dev);
0314 }
0315 
0316 /* Use fstat() instead */
0317 RTEMS_DEPRECATED static inline rtems_device_minor_number rtems_disk_get_minor_number(
0318   const rtems_disk_device *dd
0319 )
0320 {
0321   return rtems_filesystem_dev_minor_t(dd->dev);
0322 }
0323 
0324 static inline void *rtems_disk_get_driver_data(
0325   const rtems_disk_device *dd
0326 )
0327 {
0328   return dd->driver_data;
0329 }
0330 
0331 static inline uint32_t rtems_disk_get_media_block_size(
0332   const rtems_disk_device *dd
0333 )
0334 {
0335   return dd->media_block_size;
0336 }
0337 
0338 static inline uint32_t rtems_disk_get_block_size(
0339   const rtems_disk_device *dd
0340 )
0341 {
0342   return dd->block_size;
0343 }
0344 
0345 static inline rtems_blkdev_bnum rtems_disk_get_block_begin(
0346   const rtems_disk_device *dd
0347 )
0348 {
0349   return dd->start;
0350 }
0351 
0352 static inline rtems_blkdev_bnum rtems_disk_get_block_count(
0353   const rtems_disk_device *dd
0354 )
0355 {
0356   return dd->size;
0357 }
0358 
0359 /** @} */
0360 
0361 /**
0362  * @name Disk Device Maintainance
0363  */
0364 /**@{**/
0365 
0366 /* Use rtems_blkdev_create() instead */
0367 RTEMS_DEPRECATED rtems_status_code rtems_disk_create_phys(
0368   dev_t dev,
0369   uint32_t block_size,
0370   rtems_blkdev_bnum block_count,
0371   rtems_block_device_ioctl handler,
0372   void *driver_data,
0373   const char *name
0374 );
0375 
0376 /* Use rtems_blkdev_create_partition() instead */
0377 RTEMS_DEPRECATED rtems_status_code rtems_disk_create_log(
0378   dev_t dev,
0379   dev_t phys,
0380   rtems_blkdev_bnum block_begin,
0381   rtems_blkdev_bnum block_count,
0382   const char *name
0383 );
0384 
0385 /*
0386  * Use rtems_blkdev_create() or rtems_blkdev_create_partition and unlink()
0387  * instead.
0388  */
0389 RTEMS_DEPRECATED rtems_status_code rtems_disk_delete(dev_t dev);
0390 
0391 /*
0392  * Use rtems_blkdev_create() or rtems_blkdev_create_partition and open()
0393  * instead.
0394  */
0395 RTEMS_DEPRECATED rtems_disk_device *rtems_disk_obtain(dev_t dev);
0396 
0397 /*
0398  * Use rtems_blkdev_create() or rtems_blkdev_create_partition and close()
0399  * instead.
0400  */
0401 RTEMS_DEPRECATED rtems_status_code rtems_disk_release(rtems_disk_device *dd);
0402 
0403 /** @} */
0404 
0405 /**
0406  * @name Disk Management
0407  */
0408 /**@{**/
0409 
0410 /* Just remove calls to this function */
0411 RTEMS_DEPRECATED rtems_status_code rtems_disk_io_initialize(void);
0412 
0413 /* Just remove calls to this function */
0414 RTEMS_DEPRECATED rtems_status_code rtems_disk_io_done(void);
0415 
0416 /** @} */
0417 
0418 /** @} */
0419 
0420 /*
0421  * This functionality no longer available.  There is no global registration for
0422  * disk devices.
0423  */
0424 RTEMS_DEPRECATED rtems_disk_device *rtems_disk_next(dev_t dev);
0425 
0426 /* Internal function, do not use */
0427 rtems_status_code rtems_disk_init_phys(
0428   rtems_disk_device *dd,
0429   uint32_t block_size,
0430   rtems_blkdev_bnum block_count,
0431   rtems_block_device_ioctl handler,
0432   void *driver_data
0433 );
0434 
0435 /* Internal function, do not use */
0436 rtems_status_code rtems_disk_init_log(
0437   rtems_disk_device *dd,
0438   rtems_disk_device *phys_dd,
0439   rtems_blkdev_bnum block_begin,
0440   rtems_blkdev_bnum block_count
0441 );
0442 
0443 #ifdef __cplusplus
0444 }
0445 #endif
0446 
0447 #endif