Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0 */
0002 #ifndef _SCSI_DISK_H
0003 #define _SCSI_DISK_H
0004 
0005 /*
0006  * More than enough for everybody ;)  The huge number of majors
0007  * is a leftover from 16bit dev_t days, we don't really need that
0008  * much numberspace.
0009  */
0010 #define SD_MAJORS   16
0011 
0012 /*
0013  * Time out in seconds for disks and Magneto-opticals (which are slower).
0014  */
0015 #define SD_TIMEOUT      (30 * HZ)
0016 #define SD_MOD_TIMEOUT      (75 * HZ)
0017 /*
0018  * Flush timeout is a multiplier over the standard device timeout which is
0019  * user modifiable via sysfs but initially set to SD_TIMEOUT
0020  */
0021 #define SD_FLUSH_TIMEOUT_MULTIPLIER 2
0022 #define SD_WRITE_SAME_TIMEOUT   (120 * HZ)
0023 
0024 /*
0025  * Number of allowed retries
0026  */
0027 #define SD_MAX_RETRIES      5
0028 #define SD_PASSTHROUGH_RETRIES  1
0029 #define SD_MAX_MEDIUM_TIMEOUTS  2
0030 
0031 /*
0032  * Size of the initial data buffer for mode and read capacity data
0033  */
0034 #define SD_BUF_SIZE     512
0035 
0036 /*
0037  * Number of sectors at the end of the device to avoid multi-sector
0038  * accesses to in the case of last_sector_bug
0039  */
0040 #define SD_LAST_BUGGY_SECTORS   8
0041 
0042 enum {
0043     SD_EXT_CDB_SIZE = 32,   /* Extended CDB size */
0044     SD_MEMPOOL_SIZE = 2,    /* CDB pool size */
0045 };
0046 
0047 enum {
0048     SD_DEF_XFER_BLOCKS = 0xffff,
0049     SD_MAX_XFER_BLOCKS = 0xffffffff,
0050     SD_MAX_WS10_BLOCKS = 0xffff,
0051     SD_MAX_WS16_BLOCKS = 0x7fffff,
0052 };
0053 
0054 enum {
0055     SD_LBP_FULL = 0,    /* Full logical block provisioning */
0056     SD_LBP_UNMAP,       /* Use UNMAP command */
0057     SD_LBP_WS16,        /* Use WRITE SAME(16) with UNMAP bit */
0058     SD_LBP_WS10,        /* Use WRITE SAME(10) with UNMAP bit */
0059     SD_LBP_ZERO,        /* Use WRITE SAME(10) with zero payload */
0060     SD_LBP_DISABLE,     /* Discard disabled due to failed cmd */
0061 };
0062 
0063 enum {
0064     SD_ZERO_WRITE = 0,  /* Use WRITE(10/16) command */
0065     SD_ZERO_WS,     /* Use WRITE SAME(10/16) command */
0066     SD_ZERO_WS16_UNMAP, /* Use WRITE SAME(16) with UNMAP */
0067     SD_ZERO_WS10_UNMAP, /* Use WRITE SAME(10) with UNMAP */
0068 };
0069 
0070 /**
0071  * struct zoned_disk_info - Specific properties of a ZBC SCSI device.
0072  * @nr_zones: number of zones.
0073  * @zone_blocks: number of logical blocks per zone.
0074  *
0075  * This data structure holds the ZBC SCSI device properties that are retrieved
0076  * twice: a first time before the gendisk capacity is known and a second time
0077  * after the gendisk capacity is known.
0078  */
0079 struct zoned_disk_info {
0080     u32     nr_zones;
0081     u32     zone_blocks;
0082 };
0083 
0084 struct scsi_disk {
0085     struct scsi_device *device;
0086 
0087     /*
0088      * disk_dev is used to show attributes in /sys/class/scsi_disk/,
0089      * but otherwise not really needed.  Do not use for refcounting.
0090      */
0091     struct device   disk_dev;
0092     struct gendisk  *disk;
0093     struct opal_dev *opal_dev;
0094 #ifdef CONFIG_BLK_DEV_ZONED
0095     /* Updated during revalidation before the gendisk capacity is known. */
0096     struct zoned_disk_info  early_zone_info;
0097     /* Updated during revalidation after the gendisk capacity is known. */
0098     struct zoned_disk_info  zone_info;
0099     u32     zones_optimal_open;
0100     u32     zones_optimal_nonseq;
0101     u32     zones_max_open;
0102     /*
0103      * Either zero or a power of two. If not zero it means that the offset
0104      * between zone starting LBAs is constant.
0105      */
0106     u32     zone_starting_lba_gran;
0107     u32     *zones_wp_offset;
0108     spinlock_t  zones_wp_offset_lock;
0109     u32     *rev_wp_offset;
0110     struct mutex    rev_mutex;
0111     struct work_struct zone_wp_offset_work;
0112     char        *zone_wp_update_buf;
0113 #endif
0114     atomic_t    openers;
0115     sector_t    capacity;   /* size in logical blocks */
0116     int     max_retries;
0117     u32     min_xfer_blocks;
0118     u32     max_xfer_blocks;
0119     u32     opt_xfer_blocks;
0120     u32     max_ws_blocks;
0121     u32     max_unmap_blocks;
0122     u32     unmap_granularity;
0123     u32     unmap_alignment;
0124     u32     index;
0125     unsigned int    physical_block_size;
0126     unsigned int    max_medium_access_timeouts;
0127     unsigned int    medium_access_timed_out;
0128     u8      media_present;
0129     u8      write_prot;
0130     u8      protection_type;/* Data Integrity Field */
0131     u8      provisioning_mode;
0132     u8      zeroing_mode;
0133     u8      nr_actuators;       /* Number of actuators */
0134     unsigned    ATO : 1;    /* state of disk ATO bit */
0135     unsigned    cache_override : 1; /* temp override of WCE,RCD */
0136     unsigned    WCE : 1;    /* state of disk WCE bit */
0137     unsigned    RCD : 1;    /* state of disk RCD bit, unused */
0138     unsigned    DPOFUA : 1; /* state of disk DPOFUA bit */
0139     unsigned    first_scan : 1;
0140     unsigned    lbpme : 1;
0141     unsigned    lbprz : 1;
0142     unsigned    lbpu : 1;
0143     unsigned    lbpws : 1;
0144     unsigned    lbpws10 : 1;
0145     unsigned    lbpvpd : 1;
0146     unsigned    ws10 : 1;
0147     unsigned    ws16 : 1;
0148     unsigned    rc_basis: 2;
0149     unsigned    zoned: 2;
0150     unsigned    urswrz : 1;
0151     unsigned    security : 1;
0152     unsigned    ignore_medium_access_errors : 1;
0153 };
0154 #define to_scsi_disk(obj) container_of(obj, struct scsi_disk, disk_dev)
0155 
0156 static inline struct scsi_disk *scsi_disk(struct gendisk *disk)
0157 {
0158     return disk->private_data;
0159 }
0160 
0161 #define sd_printk(prefix, sdsk, fmt, a...)              \
0162         (sdsk)->disk ?                          \
0163           sdev_prefix_printk(prefix, (sdsk)->device,        \
0164                  (sdsk)->disk->disk_name, fmt, ##a) :   \
0165           sdev_printk(prefix, (sdsk)->device, fmt, ##a)
0166 
0167 #define sd_first_printk(prefix, sdsk, fmt, a...)            \
0168     do {                                \
0169         if ((sdsk)->first_scan)                 \
0170             sd_printk(prefix, sdsk, fmt, ##a);      \
0171     } while (0)
0172 
0173 static inline int scsi_medium_access_command(struct scsi_cmnd *scmd)
0174 {
0175     switch (scmd->cmnd[0]) {
0176     case READ_6:
0177     case READ_10:
0178     case READ_12:
0179     case READ_16:
0180     case SYNCHRONIZE_CACHE:
0181     case VERIFY:
0182     case VERIFY_12:
0183     case VERIFY_16:
0184     case WRITE_6:
0185     case WRITE_10:
0186     case WRITE_12:
0187     case WRITE_16:
0188     case WRITE_SAME:
0189     case WRITE_SAME_16:
0190     case UNMAP:
0191         return 1;
0192     case VARIABLE_LENGTH_CMD:
0193         switch (scmd->cmnd[9]) {
0194         case READ_32:
0195         case VERIFY_32:
0196         case WRITE_32:
0197         case WRITE_SAME_32:
0198             return 1;
0199         }
0200     }
0201 
0202     return 0;
0203 }
0204 
0205 static inline sector_t logical_to_sectors(struct scsi_device *sdev, sector_t blocks)
0206 {
0207     return blocks << (ilog2(sdev->sector_size) - 9);
0208 }
0209 
0210 static inline unsigned int logical_to_bytes(struct scsi_device *sdev, sector_t blocks)
0211 {
0212     return blocks * sdev->sector_size;
0213 }
0214 
0215 static inline sector_t bytes_to_logical(struct scsi_device *sdev, unsigned int bytes)
0216 {
0217     return bytes >> ilog2(sdev->sector_size);
0218 }
0219 
0220 static inline sector_t sectors_to_logical(struct scsi_device *sdev, sector_t sector)
0221 {
0222     return sector >> (ilog2(sdev->sector_size) - 9);
0223 }
0224 
0225 #ifdef CONFIG_BLK_DEV_INTEGRITY
0226 
0227 extern void sd_dif_config_host(struct scsi_disk *);
0228 
0229 #else /* CONFIG_BLK_DEV_INTEGRITY */
0230 
0231 static inline void sd_dif_config_host(struct scsi_disk *disk)
0232 {
0233 }
0234 
0235 #endif /* CONFIG_BLK_DEV_INTEGRITY */
0236 
0237 static inline int sd_is_zoned(struct scsi_disk *sdkp)
0238 {
0239     return sdkp->zoned == 1 || sdkp->device->type == TYPE_ZBC;
0240 }
0241 
0242 #ifdef CONFIG_BLK_DEV_ZONED
0243 
0244 void sd_zbc_free_zone_info(struct scsi_disk *sdkp);
0245 int sd_zbc_read_zones(struct scsi_disk *sdkp, u8 buf[SD_BUF_SIZE]);
0246 int sd_zbc_revalidate_zones(struct scsi_disk *sdkp);
0247 blk_status_t sd_zbc_setup_zone_mgmt_cmnd(struct scsi_cmnd *cmd,
0248                      unsigned char op, bool all);
0249 unsigned int sd_zbc_complete(struct scsi_cmnd *cmd, unsigned int good_bytes,
0250                  struct scsi_sense_hdr *sshdr);
0251 int sd_zbc_report_zones(struct gendisk *disk, sector_t sector,
0252         unsigned int nr_zones, report_zones_cb cb, void *data);
0253 
0254 blk_status_t sd_zbc_prepare_zone_append(struct scsi_cmnd *cmd, sector_t *lba,
0255                         unsigned int nr_blocks);
0256 
0257 #else /* CONFIG_BLK_DEV_ZONED */
0258 
0259 static inline void sd_zbc_free_zone_info(struct scsi_disk *sdkp) {}
0260 
0261 static inline int sd_zbc_read_zones(struct scsi_disk *sdkp, u8 buf[SD_BUF_SIZE])
0262 {
0263     return 0;
0264 }
0265 
0266 static inline int sd_zbc_revalidate_zones(struct scsi_disk *sdkp)
0267 {
0268     return 0;
0269 }
0270 
0271 static inline blk_status_t sd_zbc_setup_zone_mgmt_cmnd(struct scsi_cmnd *cmd,
0272                                unsigned char op,
0273                                bool all)
0274 {
0275     return BLK_STS_TARGET;
0276 }
0277 
0278 static inline unsigned int sd_zbc_complete(struct scsi_cmnd *cmd,
0279             unsigned int good_bytes, struct scsi_sense_hdr *sshdr)
0280 {
0281     return good_bytes;
0282 }
0283 
0284 static inline blk_status_t sd_zbc_prepare_zone_append(struct scsi_cmnd *cmd,
0285                               sector_t *lba,
0286                               unsigned int nr_blocks)
0287 {
0288     return BLK_STS_TARGET;
0289 }
0290 
0291 #define sd_zbc_report_zones NULL
0292 
0293 #endif /* CONFIG_BLK_DEV_ZONED */
0294 
0295 void sd_print_sense_hdr(struct scsi_disk *sdkp, struct scsi_sense_hdr *sshdr);
0296 void sd_print_result(const struct scsi_disk *sdkp, const char *msg, int result);
0297 
0298 #endif /* _SCSI_DISK_H */