sd.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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. struct scsi_disk {
  61. struct scsi_driver *driver; /* always &sd_template */
  62. struct scsi_device *device;
  63. struct device dev;
  64. struct gendisk *disk;
  65. struct opal_dev *opal_dev;
  66. #ifdef CONFIG_BLK_DEV_ZONED
  67. u32 nr_zones;
  68. u32 rev_nr_zones;
  69. u32 zone_blocks;
  70. u32 rev_zone_blocks;
  71. u32 zones_optimal_open;
  72. u32 zones_optimal_nonseq;
  73. u32 zones_max_open;
  74. u32 *zones_wp_offset;
  75. spinlock_t zones_wp_offset_lock;
  76. u32 *rev_wp_offset;
  77. struct mutex rev_mutex;
  78. struct work_struct zone_wp_offset_work;
  79. char *zone_wp_update_buf;
  80. #endif
  81. atomic_t openers;
  82. sector_t capacity; /* size in logical blocks */
  83. int max_retries;
  84. u32 max_xfer_blocks;
  85. u32 opt_xfer_blocks;
  86. u32 max_ws_blocks;
  87. u32 max_unmap_blocks;
  88. u32 unmap_granularity;
  89. u32 unmap_alignment;
  90. u32 index;
  91. unsigned int physical_block_size;
  92. unsigned int max_medium_access_timeouts;
  93. unsigned int medium_access_timed_out;
  94. u8 media_present;
  95. u8 write_prot;
  96. u8 protection_type;/* Data Integrity Field */
  97. u8 provisioning_mode;
  98. u8 zeroing_mode;
  99. unsigned ATO : 1; /* state of disk ATO bit */
  100. unsigned cache_override : 1; /* temp override of WCE,RCD */
  101. unsigned WCE : 1; /* state of disk WCE bit */
  102. unsigned RCD : 1; /* state of disk RCD bit, unused */
  103. unsigned DPOFUA : 1; /* state of disk DPOFUA bit */
  104. unsigned first_scan : 1;
  105. unsigned lbpme : 1;
  106. unsigned lbprz : 1;
  107. unsigned lbpu : 1;
  108. unsigned lbpws : 1;
  109. unsigned lbpws10 : 1;
  110. unsigned lbpvpd : 1;
  111. unsigned ws10 : 1;
  112. unsigned ws16 : 1;
  113. unsigned rc_basis: 2;
  114. unsigned zoned: 2;
  115. unsigned urswrz : 1;
  116. unsigned security : 1;
  117. unsigned ignore_medium_access_errors : 1;
  118. ANDROID_KABI_RESERVE(1);
  119. ANDROID_KABI_RESERVE(2);
  120. };
  121. #define to_scsi_disk(obj) container_of(obj,struct scsi_disk,dev)
  122. static inline struct scsi_disk *scsi_disk(struct gendisk *disk)
  123. {
  124. return container_of(disk->private_data, struct scsi_disk, driver);
  125. }
  126. #define sd_printk(prefix, sdsk, fmt, a...) \
  127. (sdsk)->disk ? \
  128. sdev_prefix_printk(prefix, (sdsk)->device, \
  129. (sdsk)->disk->disk_name, fmt, ##a) : \
  130. sdev_printk(prefix, (sdsk)->device, fmt, ##a)
  131. #define sd_first_printk(prefix, sdsk, fmt, a...) \
  132. do { \
  133. if ((sdsk)->first_scan) \
  134. sd_printk(prefix, sdsk, fmt, ##a); \
  135. } while (0)
  136. static inline int scsi_medium_access_command(struct scsi_cmnd *scmd)
  137. {
  138. switch (scmd->cmnd[0]) {
  139. case READ_6:
  140. case READ_10:
  141. case READ_12:
  142. case READ_16:
  143. case SYNCHRONIZE_CACHE:
  144. case VERIFY:
  145. case VERIFY_12:
  146. case VERIFY_16:
  147. case WRITE_6:
  148. case WRITE_10:
  149. case WRITE_12:
  150. case WRITE_16:
  151. case WRITE_SAME:
  152. case WRITE_SAME_16:
  153. case UNMAP:
  154. return 1;
  155. case VARIABLE_LENGTH_CMD:
  156. switch (scmd->cmnd[9]) {
  157. case READ_32:
  158. case VERIFY_32:
  159. case WRITE_32:
  160. case WRITE_SAME_32:
  161. return 1;
  162. }
  163. }
  164. return 0;
  165. }
  166. static inline sector_t logical_to_sectors(struct scsi_device *sdev, sector_t blocks)
  167. {
  168. return blocks << (ilog2(sdev->sector_size) - 9);
  169. }
  170. static inline unsigned int logical_to_bytes(struct scsi_device *sdev, sector_t blocks)
  171. {
  172. return blocks * sdev->sector_size;
  173. }
  174. static inline sector_t bytes_to_logical(struct scsi_device *sdev, unsigned int bytes)
  175. {
  176. return bytes >> ilog2(sdev->sector_size);
  177. }
  178. static inline sector_t sectors_to_logical(struct scsi_device *sdev, sector_t sector)
  179. {
  180. return sector >> (ilog2(sdev->sector_size) - 9);
  181. }
  182. #ifdef CONFIG_BLK_DEV_INTEGRITY
  183. extern void sd_dif_config_host(struct scsi_disk *);
  184. #else /* CONFIG_BLK_DEV_INTEGRITY */
  185. static inline void sd_dif_config_host(struct scsi_disk *disk)
  186. {
  187. }
  188. #endif /* CONFIG_BLK_DEV_INTEGRITY */
  189. static inline int sd_is_zoned(struct scsi_disk *sdkp)
  190. {
  191. return sdkp->zoned == 1 || sdkp->device->type == TYPE_ZBC;
  192. }
  193. #ifdef CONFIG_BLK_DEV_ZONED
  194. void sd_zbc_release_disk(struct scsi_disk *sdkp);
  195. int sd_zbc_read_zones(struct scsi_disk *sdkp, unsigned char *buffer);
  196. int sd_zbc_revalidate_zones(struct scsi_disk *sdkp);
  197. blk_status_t sd_zbc_setup_zone_mgmt_cmnd(struct scsi_cmnd *cmd,
  198. unsigned char op, bool all);
  199. unsigned int sd_zbc_complete(struct scsi_cmnd *cmd, unsigned int good_bytes,
  200. struct scsi_sense_hdr *sshdr);
  201. int sd_zbc_report_zones(struct gendisk *disk, sector_t sector,
  202. unsigned int nr_zones, report_zones_cb cb, void *data);
  203. blk_status_t sd_zbc_prepare_zone_append(struct scsi_cmnd *cmd, sector_t *lba,
  204. unsigned int nr_blocks);
  205. #else /* CONFIG_BLK_DEV_ZONED */
  206. static inline void sd_zbc_release_disk(struct scsi_disk *sdkp) {}
  207. static inline int sd_zbc_read_zones(struct scsi_disk *sdkp,
  208. unsigned char *buf)
  209. {
  210. return 0;
  211. }
  212. static inline int sd_zbc_revalidate_zones(struct scsi_disk *sdkp)
  213. {
  214. return 0;
  215. }
  216. static inline blk_status_t sd_zbc_setup_zone_mgmt_cmnd(struct scsi_cmnd *cmd,
  217. unsigned char op,
  218. bool all)
  219. {
  220. return BLK_STS_TARGET;
  221. }
  222. static inline unsigned int sd_zbc_complete(struct scsi_cmnd *cmd,
  223. unsigned int good_bytes, struct scsi_sense_hdr *sshdr)
  224. {
  225. return good_bytes;
  226. }
  227. static inline blk_status_t sd_zbc_prepare_zone_append(struct scsi_cmnd *cmd,
  228. sector_t *lba,
  229. unsigned int nr_blocks)
  230. {
  231. return BLK_STS_TARGET;
  232. }
  233. #define sd_zbc_report_zones NULL
  234. #endif /* CONFIG_BLK_DEV_ZONED */
  235. void sd_print_sense_hdr(struct scsi_disk *sdkp, struct scsi_sense_hdr *sshdr);
  236. void sd_print_result(const struct scsi_disk *sdkp, const char *msg, int result);
  237. #endif /* _SCSI_DISK_H */