sd.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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 zone_blocks;
  69. u32 zone_shift;
  70. u32 zones_optimal_open;
  71. u32 zones_optimal_nonseq;
  72. u32 zones_max_open;
  73. #endif
  74. atomic_t openers;
  75. sector_t capacity; /* size in logical blocks */
  76. u32 max_xfer_blocks;
  77. u32 opt_xfer_blocks;
  78. u32 max_ws_blocks;
  79. u32 max_unmap_blocks;
  80. u32 unmap_granularity;
  81. u32 unmap_alignment;
  82. u32 index;
  83. unsigned int physical_block_size;
  84. unsigned int max_medium_access_timeouts;
  85. unsigned int medium_access_timed_out;
  86. u8 media_present;
  87. u8 write_prot;
  88. u8 protection_type;/* Data Integrity Field */
  89. u8 provisioning_mode;
  90. u8 zeroing_mode;
  91. unsigned ATO : 1; /* state of disk ATO bit */
  92. unsigned cache_override : 1; /* temp override of WCE,RCD */
  93. unsigned WCE : 1; /* state of disk WCE bit */
  94. unsigned RCD : 1; /* state of disk RCD bit, unused */
  95. unsigned DPOFUA : 1; /* state of disk DPOFUA bit */
  96. unsigned first_scan : 1;
  97. unsigned lbpme : 1;
  98. unsigned lbprz : 1;
  99. unsigned lbpu : 1;
  100. unsigned lbpws : 1;
  101. unsigned lbpws10 : 1;
  102. unsigned lbpvpd : 1;
  103. unsigned ws10 : 1;
  104. unsigned ws16 : 1;
  105. unsigned rc_basis: 2;
  106. unsigned zoned: 2;
  107. unsigned urswrz : 1;
  108. unsigned security : 1;
  109. unsigned ignore_medium_access_errors : 1;
  110. };
  111. #define to_scsi_disk(obj) container_of(obj,struct scsi_disk,dev)
  112. static inline struct scsi_disk *scsi_disk(struct gendisk *disk)
  113. {
  114. return container_of(disk->private_data, struct scsi_disk, driver);
  115. }
  116. #define sd_printk(prefix, sdsk, fmt, a...) \
  117. (sdsk)->disk ? \
  118. sdev_prefix_printk(prefix, (sdsk)->device, \
  119. (sdsk)->disk->disk_name, fmt, ##a) : \
  120. sdev_printk(prefix, (sdsk)->device, fmt, ##a)
  121. #define sd_first_printk(prefix, sdsk, fmt, a...) \
  122. do { \
  123. if ((sdkp)->first_scan) \
  124. sd_printk(prefix, sdsk, fmt, ##a); \
  125. } while (0)
  126. static inline int scsi_medium_access_command(struct scsi_cmnd *scmd)
  127. {
  128. switch (scmd->cmnd[0]) {
  129. case READ_6:
  130. case READ_10:
  131. case READ_12:
  132. case READ_16:
  133. case SYNCHRONIZE_CACHE:
  134. case VERIFY:
  135. case VERIFY_12:
  136. case VERIFY_16:
  137. case WRITE_6:
  138. case WRITE_10:
  139. case WRITE_12:
  140. case WRITE_16:
  141. case WRITE_SAME:
  142. case WRITE_SAME_16:
  143. case UNMAP:
  144. return 1;
  145. case VARIABLE_LENGTH_CMD:
  146. switch (scmd->cmnd[9]) {
  147. case READ_32:
  148. case VERIFY_32:
  149. case WRITE_32:
  150. case WRITE_SAME_32:
  151. return 1;
  152. }
  153. }
  154. return 0;
  155. }
  156. static inline sector_t logical_to_sectors(struct scsi_device *sdev, sector_t blocks)
  157. {
  158. return blocks << (ilog2(sdev->sector_size) - 9);
  159. }
  160. static inline unsigned int logical_to_bytes(struct scsi_device *sdev, sector_t blocks)
  161. {
  162. return blocks * sdev->sector_size;
  163. }
  164. static inline sector_t bytes_to_logical(struct scsi_device *sdev, unsigned int bytes)
  165. {
  166. return bytes >> ilog2(sdev->sector_size);
  167. }
  168. static inline sector_t sectors_to_logical(struct scsi_device *sdev, sector_t sector)
  169. {
  170. return sector >> (ilog2(sdev->sector_size) - 9);
  171. }
  172. /*
  173. * Look up the DIX operation based on whether the command is read or
  174. * write and whether dix and dif are enabled.
  175. */
  176. static inline unsigned int sd_prot_op(bool write, bool dix, bool dif)
  177. {
  178. /* Lookup table: bit 2 (write), bit 1 (dix), bit 0 (dif) */
  179. const unsigned int ops[] = { /* wrt dix dif */
  180. SCSI_PROT_NORMAL, /* 0 0 0 */
  181. SCSI_PROT_READ_STRIP, /* 0 0 1 */
  182. SCSI_PROT_READ_INSERT, /* 0 1 0 */
  183. SCSI_PROT_READ_PASS, /* 0 1 1 */
  184. SCSI_PROT_NORMAL, /* 1 0 0 */
  185. SCSI_PROT_WRITE_INSERT, /* 1 0 1 */
  186. SCSI_PROT_WRITE_STRIP, /* 1 1 0 */
  187. SCSI_PROT_WRITE_PASS, /* 1 1 1 */
  188. };
  189. return ops[write << 2 | dix << 1 | dif];
  190. }
  191. /*
  192. * Returns a mask of the protection flags that are valid for a given DIX
  193. * operation.
  194. */
  195. static inline unsigned int sd_prot_flag_mask(unsigned int prot_op)
  196. {
  197. const unsigned int flag_mask[] = {
  198. [SCSI_PROT_NORMAL] = 0,
  199. [SCSI_PROT_READ_STRIP] = SCSI_PROT_TRANSFER_PI |
  200. SCSI_PROT_GUARD_CHECK |
  201. SCSI_PROT_REF_CHECK |
  202. SCSI_PROT_REF_INCREMENT,
  203. [SCSI_PROT_READ_INSERT] = SCSI_PROT_REF_INCREMENT |
  204. SCSI_PROT_IP_CHECKSUM,
  205. [SCSI_PROT_READ_PASS] = SCSI_PROT_TRANSFER_PI |
  206. SCSI_PROT_GUARD_CHECK |
  207. SCSI_PROT_REF_CHECK |
  208. SCSI_PROT_REF_INCREMENT |
  209. SCSI_PROT_IP_CHECKSUM,
  210. [SCSI_PROT_WRITE_INSERT] = SCSI_PROT_TRANSFER_PI |
  211. SCSI_PROT_REF_INCREMENT,
  212. [SCSI_PROT_WRITE_STRIP] = SCSI_PROT_GUARD_CHECK |
  213. SCSI_PROT_REF_CHECK |
  214. SCSI_PROT_REF_INCREMENT |
  215. SCSI_PROT_IP_CHECKSUM,
  216. [SCSI_PROT_WRITE_PASS] = SCSI_PROT_TRANSFER_PI |
  217. SCSI_PROT_GUARD_CHECK |
  218. SCSI_PROT_REF_CHECK |
  219. SCSI_PROT_REF_INCREMENT |
  220. SCSI_PROT_IP_CHECKSUM,
  221. };
  222. return flag_mask[prot_op];
  223. }
  224. #ifdef CONFIG_BLK_DEV_INTEGRITY
  225. extern void sd_dif_config_host(struct scsi_disk *);
  226. #else /* CONFIG_BLK_DEV_INTEGRITY */
  227. static inline void sd_dif_config_host(struct scsi_disk *disk)
  228. {
  229. }
  230. #endif /* CONFIG_BLK_DEV_INTEGRITY */
  231. static inline int sd_is_zoned(struct scsi_disk *sdkp)
  232. {
  233. return sdkp->zoned == 1 || sdkp->device->type == TYPE_ZBC;
  234. }
  235. #ifdef CONFIG_BLK_DEV_ZONED
  236. extern int sd_zbc_read_zones(struct scsi_disk *sdkp, unsigned char *buffer);
  237. extern void sd_zbc_remove(struct scsi_disk *sdkp);
  238. extern void sd_zbc_print_zones(struct scsi_disk *sdkp);
  239. extern int sd_zbc_setup_report_cmnd(struct scsi_cmnd *cmd);
  240. extern int sd_zbc_setup_reset_cmnd(struct scsi_cmnd *cmd);
  241. extern void sd_zbc_complete(struct scsi_cmnd *cmd, unsigned int good_bytes,
  242. struct scsi_sense_hdr *sshdr);
  243. #else /* CONFIG_BLK_DEV_ZONED */
  244. static inline int sd_zbc_read_zones(struct scsi_disk *sdkp,
  245. unsigned char *buf)
  246. {
  247. return 0;
  248. }
  249. static inline void sd_zbc_remove(struct scsi_disk *sdkp) {}
  250. static inline void sd_zbc_print_zones(struct scsi_disk *sdkp) {}
  251. static inline int sd_zbc_setup_report_cmnd(struct scsi_cmnd *cmd)
  252. {
  253. return BLKPREP_INVALID;
  254. }
  255. static inline int sd_zbc_setup_reset_cmnd(struct scsi_cmnd *cmd)
  256. {
  257. return BLKPREP_INVALID;
  258. }
  259. static inline void sd_zbc_complete(struct scsi_cmnd *cmd,
  260. unsigned int good_bytes,
  261. struct scsi_sense_hdr *sshdr) {}
  262. #endif /* CONFIG_BLK_DEV_ZONED */
  263. #endif /* _SCSI_DISK_H */