sd.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef _SCSI_DISK_H
  3. #define _SCSI_DISK_H
  4. /*
  5. * More than enough for everybody ;) The huge number of majors
  6. * is a leftover from 16bit dev_t days, we don't really need that
  7. * much numberspace.
  8. */
  9. #define SD_MAJORS 16
  10. /*
  11. * Time out in seconds for disks and Magneto-opticals (which are slower).
  12. */
  13. #define SD_TIMEOUT (30 * HZ)
  14. #define SD_MOD_TIMEOUT (75 * HZ)
  15. /*
  16. * Flush timeout is a multiplier over the standard device timeout which is
  17. * user modifiable via sysfs but initially set to SD_TIMEOUT
  18. */
  19. #define SD_FLUSH_TIMEOUT_MULTIPLIER 2
  20. #define SD_WRITE_SAME_TIMEOUT (120 * HZ)
  21. /*
  22. * Number of allowed retries
  23. */
  24. #define SD_MAX_RETRIES 5
  25. #define SD_PASSTHROUGH_RETRIES 1
  26. #define SD_MAX_MEDIUM_TIMEOUTS 2
  27. /*
  28. * Size of the initial data buffer for mode and read capacity data
  29. */
  30. #define SD_BUF_SIZE 512
  31. /*
  32. * Number of sectors at the end of the device to avoid multi-sector
  33. * accesses to in the case of last_sector_bug
  34. */
  35. #define SD_LAST_BUGGY_SECTORS 8
  36. enum {
  37. SD_EXT_CDB_SIZE = 32, /* Extended CDB size */
  38. SD_MEMPOOL_SIZE = 2, /* CDB pool size */
  39. };
  40. enum {
  41. SD_DEF_XFER_BLOCKS = 0xffff,
  42. SD_MAX_XFER_BLOCKS = 0xffffffff,
  43. SD_MAX_WS10_BLOCKS = 0xffff,
  44. SD_MAX_WS16_BLOCKS = 0x7fffff,
  45. };
  46. enum {
  47. SD_LBP_FULL = 0, /* Full logical block provisioning */
  48. SD_LBP_UNMAP, /* Use UNMAP command */
  49. SD_LBP_WS16, /* Use WRITE SAME(16) with UNMAP bit */
  50. SD_LBP_WS10, /* Use WRITE SAME(10) with UNMAP bit */
  51. SD_LBP_ZERO, /* Use WRITE SAME(10) with zero payload */
  52. SD_LBP_DISABLE, /* Discard disabled due to failed cmd */
  53. };
  54. enum {
  55. SD_ZERO_WRITE = 0, /* Use WRITE(10/16) command */
  56. SD_ZERO_WS, /* Use WRITE SAME(10/16) command */
  57. SD_ZERO_WS16_UNMAP, /* Use WRITE SAME(16) with UNMAP */
  58. SD_ZERO_WS10_UNMAP, /* Use WRITE SAME(10) with UNMAP */
  59. };
  60. /**
  61. * struct zoned_disk_info - Specific properties of a ZBC SCSI device.
  62. * @nr_zones: number of zones.
  63. * @zone_blocks: number of logical blocks per zone.
  64. *
  65. * This data structure holds the ZBC SCSI device properties that are retrieved
  66. * twice: a first time before the gendisk capacity is known and a second time
  67. * after the gendisk capacity is known.
  68. */
  69. struct zoned_disk_info {
  70. u32 nr_zones;
  71. u32 zone_blocks;
  72. };
  73. struct scsi_disk {
  74. struct scsi_device *device;
  75. /*
  76. * disk_dev is used to show attributes in /sys/class/scsi_disk/,
  77. * but otherwise not really needed. Do not use for refcounting.
  78. */
  79. struct device disk_dev;
  80. struct gendisk *disk;
  81. struct opal_dev *opal_dev;
  82. #ifdef CONFIG_BLK_DEV_ZONED
  83. /* Updated during revalidation before the gendisk capacity is known. */
  84. struct zoned_disk_info early_zone_info;
  85. /* Updated during revalidation after the gendisk capacity is known. */
  86. struct zoned_disk_info zone_info;
  87. u32 zones_optimal_open;
  88. u32 zones_optimal_nonseq;
  89. u32 zones_max_open;
  90. /*
  91. * Either zero or a power of two. If not zero it means that the offset
  92. * between zone starting LBAs is constant.
  93. */
  94. u32 zone_starting_lba_gran;
  95. u32 *zones_wp_offset;
  96. spinlock_t zones_wp_offset_lock;
  97. u32 *rev_wp_offset;
  98. struct mutex rev_mutex;
  99. struct work_struct zone_wp_offset_work;
  100. char *zone_wp_update_buf;
  101. #endif
  102. atomic_t openers;
  103. sector_t capacity; /* size in logical blocks */
  104. int max_retries;
  105. u32 min_xfer_blocks;
  106. u32 max_xfer_blocks;
  107. u32 opt_xfer_blocks;
  108. u32 max_ws_blocks;
  109. u32 max_unmap_blocks;
  110. u32 unmap_granularity;
  111. u32 unmap_alignment;
  112. u32 index;
  113. unsigned int physical_block_size;
  114. unsigned int max_medium_access_timeouts;
  115. unsigned int medium_access_timed_out;
  116. u8 media_present;
  117. u8 write_prot;
  118. u8 protection_type;/* Data Integrity Field */
  119. u8 provisioning_mode;
  120. u8 zeroing_mode;
  121. u8 nr_actuators; /* Number of actuators */
  122. unsigned ATO : 1; /* state of disk ATO bit */
  123. unsigned cache_override : 1; /* temp override of WCE,RCD */
  124. unsigned WCE : 1; /* state of disk WCE bit */
  125. unsigned RCD : 1; /* state of disk RCD bit, unused */
  126. unsigned DPOFUA : 1; /* state of disk DPOFUA bit */
  127. unsigned first_scan : 1;
  128. unsigned lbpme : 1;
  129. unsigned lbprz : 1;
  130. unsigned lbpu : 1;
  131. unsigned lbpws : 1;
  132. unsigned lbpws10 : 1;
  133. unsigned lbpvpd : 1;
  134. unsigned ws10 : 1;
  135. unsigned ws16 : 1;
  136. unsigned rc_basis: 2;
  137. unsigned zoned: 2;
  138. unsigned urswrz : 1;
  139. unsigned security : 1;
  140. unsigned ignore_medium_access_errors : 1;
  141. ANDROID_KABI_RESERVE(1);
  142. ANDROID_KABI_RESERVE(2);
  143. };
  144. #define to_scsi_disk(obj) container_of(obj, struct scsi_disk, disk_dev)
  145. static inline struct scsi_disk *scsi_disk(struct gendisk *disk)
  146. {
  147. return disk->private_data;
  148. }
  149. #define sd_printk(prefix, sdsk, fmt, a...) \
  150. (sdsk)->disk ? \
  151. sdev_prefix_printk(prefix, (sdsk)->device, \
  152. (sdsk)->disk->disk_name, fmt, ##a) : \
  153. sdev_printk(prefix, (sdsk)->device, fmt, ##a)
  154. #define sd_first_printk(prefix, sdsk, fmt, a...) \
  155. do { \
  156. if ((sdsk)->first_scan) \
  157. sd_printk(prefix, sdsk, fmt, ##a); \
  158. } while (0)
  159. static inline int scsi_medium_access_command(struct scsi_cmnd *scmd)
  160. {
  161. switch (scmd->cmnd[0]) {
  162. case READ_6:
  163. case READ_10:
  164. case READ_12:
  165. case READ_16:
  166. case SYNCHRONIZE_CACHE:
  167. case VERIFY:
  168. case VERIFY_12:
  169. case VERIFY_16:
  170. case WRITE_6:
  171. case WRITE_10:
  172. case WRITE_12:
  173. case WRITE_16:
  174. case WRITE_SAME:
  175. case WRITE_SAME_16:
  176. case UNMAP:
  177. return 1;
  178. case VARIABLE_LENGTH_CMD:
  179. switch (scmd->cmnd[9]) {
  180. case READ_32:
  181. case VERIFY_32:
  182. case WRITE_32:
  183. case WRITE_SAME_32:
  184. return 1;
  185. }
  186. }
  187. return 0;
  188. }
  189. static inline sector_t logical_to_sectors(struct scsi_device *sdev, sector_t blocks)
  190. {
  191. return blocks << (ilog2(sdev->sector_size) - 9);
  192. }
  193. static inline unsigned int logical_to_bytes(struct scsi_device *sdev, sector_t blocks)
  194. {
  195. return blocks * sdev->sector_size;
  196. }
  197. static inline sector_t bytes_to_logical(struct scsi_device *sdev, unsigned int bytes)
  198. {
  199. return bytes >> ilog2(sdev->sector_size);
  200. }
  201. static inline sector_t sectors_to_logical(struct scsi_device *sdev, sector_t sector)
  202. {
  203. return sector >> (ilog2(sdev->sector_size) - 9);
  204. }
  205. #ifdef CONFIG_BLK_DEV_INTEGRITY
  206. extern void sd_dif_config_host(struct scsi_disk *);
  207. #else /* CONFIG_BLK_DEV_INTEGRITY */
  208. static inline void sd_dif_config_host(struct scsi_disk *disk)
  209. {
  210. }
  211. #endif /* CONFIG_BLK_DEV_INTEGRITY */
  212. static inline int sd_is_zoned(struct scsi_disk *sdkp)
  213. {
  214. return sdkp->zoned == 1 || sdkp->device->type == TYPE_ZBC;
  215. }
  216. #ifdef CONFIG_BLK_DEV_ZONED
  217. void sd_zbc_free_zone_info(struct scsi_disk *sdkp);
  218. int sd_zbc_read_zones(struct scsi_disk *sdkp, u8 buf[SD_BUF_SIZE]);
  219. int sd_zbc_revalidate_zones(struct scsi_disk *sdkp);
  220. blk_status_t sd_zbc_setup_zone_mgmt_cmnd(struct scsi_cmnd *cmd,
  221. unsigned char op, bool all);
  222. unsigned int sd_zbc_complete(struct scsi_cmnd *cmd, unsigned int good_bytes,
  223. struct scsi_sense_hdr *sshdr);
  224. int sd_zbc_report_zones(struct gendisk *disk, sector_t sector,
  225. unsigned int nr_zones, report_zones_cb cb, void *data);
  226. blk_status_t sd_zbc_prepare_zone_append(struct scsi_cmnd *cmd, sector_t *lba,
  227. unsigned int nr_blocks);
  228. #else /* CONFIG_BLK_DEV_ZONED */
  229. static inline void sd_zbc_free_zone_info(struct scsi_disk *sdkp) {}
  230. static inline int sd_zbc_read_zones(struct scsi_disk *sdkp, u8 buf[SD_BUF_SIZE])
  231. {
  232. return 0;
  233. }
  234. static inline int sd_zbc_revalidate_zones(struct scsi_disk *sdkp)
  235. {
  236. return 0;
  237. }
  238. static inline blk_status_t sd_zbc_setup_zone_mgmt_cmnd(struct scsi_cmnd *cmd,
  239. unsigned char op,
  240. bool all)
  241. {
  242. return BLK_STS_TARGET;
  243. }
  244. static inline unsigned int sd_zbc_complete(struct scsi_cmnd *cmd,
  245. unsigned int good_bytes, struct scsi_sense_hdr *sshdr)
  246. {
  247. return good_bytes;
  248. }
  249. static inline blk_status_t sd_zbc_prepare_zone_append(struct scsi_cmnd *cmd,
  250. sector_t *lba,
  251. unsigned int nr_blocks)
  252. {
  253. return BLK_STS_TARGET;
  254. }
  255. #define sd_zbc_report_zones NULL
  256. #endif /* CONFIG_BLK_DEV_ZONED */
  257. void sd_print_sense_hdr(struct scsi_disk *sdkp, struct scsi_sense_hdr *sshdr);
  258. void sd_print_result(const struct scsi_disk *sdkp, const char *msg, int result);
  259. #endif /* _SCSI_DISK_H */