Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1230 lines
35 KiB

  1. #if !defined (___atapi_h___)
  2. #define ___atapi_h___
  3. /*++
  4. Copyright (C) Microsoft Corporation, 1993 - 1999
  5. Module Name:
  6. atapi.h
  7. Abstract:
  8. This module contains the structures and definitions for the ATAPI
  9. IDE miniport driver.
  10. Author:
  11. Mike Glass
  12. Revision History:
  13. --*/
  14. #include "scsi.h"
  15. #include "stdio.h"
  16. #include "string.h"
  17. ULONG
  18. AtapiParseArgumentString(
  19. IN PCHAR String,
  20. IN PCHAR KeyWord
  21. );
  22. //
  23. // IDE register definition
  24. //
  25. typedef struct _IDE_REGISTERS_3 {
  26. ULONG Data;
  27. UCHAR Others[4];
  28. } IDE_REGISTERS_3, *PIDE_REGISTERS_3;
  29. typedef struct _ATA_COMMAND_BLOCK_READ_REGISTERS {
  30. union {
  31. struct {
  32. PUSHORT Data16;
  33. } w;
  34. struct {
  35. PUCHAR Data8;
  36. PUCHAR Error;
  37. PUCHAR SectorCount;
  38. PUCHAR SectorNumber;
  39. PUCHAR CylinderLow;
  40. PUCHAR CylinderHigh;
  41. PUCHAR DriveSelect;
  42. PUCHAR Status;
  43. } b;
  44. } size;
  45. } ATA_COMMAND_BLOCK_READ_REGISTERS, *PATA_COMMAND_BLOCK_READ_REGISTERS;
  46. typedef struct _ATA_COMMAND_BLOCK_WRITE_REGISTERS {
  47. union {
  48. struct {
  49. PUSHORT Data16;
  50. } w;
  51. struct {
  52. PUCHAR Data8;
  53. PUCHAR Feature;
  54. PUCHAR SectorCount;
  55. PUCHAR SectorNumber;
  56. PUCHAR CylinderLow;
  57. PUCHAR CylinderHigh;
  58. PUCHAR DriveSelect;
  59. PUCHAR Command;
  60. } b;
  61. } size;
  62. } ATA_COMMAND_BLOCK_WRITE_REGISTERS, *PATA_COMMAND_BLOCK_WRITE_REGISTERS;
  63. typedef struct _ATAPI_COMMAND_BLOCK_READ_REGISTERS {
  64. union {
  65. struct {
  66. PUSHORT Data16;
  67. } w;
  68. struct {
  69. PUCHAR Data8;
  70. PUCHAR Error;
  71. PUCHAR InterruptReason;
  72. PUCHAR Reserved;
  73. PUCHAR ByteCountLow;
  74. PUCHAR ByteCountHigh;
  75. PUCHAR DriveSelect;
  76. PUCHAR Status;
  77. } b;
  78. } size;
  79. } ATAPI_COMMAND_BLOCK_READ_REGISTERS, *PATAPI_COMMAND_BLOCK_READ_REGISTERS;
  80. typedef struct _ATAPI_COMMAND_BLOCK_WRITE_REGISTERS {
  81. union {
  82. struct {
  83. PUSHORT Data16;
  84. } w;
  85. struct {
  86. PUCHAR Data8;
  87. PUCHAR Feature;
  88. PUCHAR Resereved0;
  89. PUCHAR Resereved1;
  90. PUCHAR ByteCountLow;
  91. PUCHAR ByteCountHigh;
  92. PUCHAR DriveSelect;
  93. PUCHAR Command;
  94. } b;
  95. } size;
  96. } ATAPI_COMMAND_BLOCK_WRITE_REGISTERS, *PATAPI_COMMAND_BLOCK_WRITE_REGISTERS;
  97. typedef struct _IDE_COMMAND_BLOCK_WRITE_REGISTERS {
  98. PUCHAR RegistersBaseAddress;
  99. union {
  100. union {
  101. ATA_COMMAND_BLOCK_READ_REGISTERS r;
  102. ATA_COMMAND_BLOCK_WRITE_REGISTERS w;
  103. } ata;
  104. union {
  105. ATAPI_COMMAND_BLOCK_READ_REGISTERS r;
  106. ATAPI_COMMAND_BLOCK_WRITE_REGISTERS w;
  107. } atapi;
  108. } type;
  109. } IDE_COMMAND_BLOCK_WRITE_REGISTERS, *PIDE_COMMAND_BLOCK_WRITE_REGISTERS;
  110. //
  111. // handy ata macros
  112. //
  113. #define ATA_DATA16_REG(baseAddress) (baseAddress)->type.ata.r.size.w.Data16
  114. #define ATA_ERROR_REG(baseAddress) (baseAddress)->type.ata.r.size.b.Error
  115. #define ATA_SECTOR_COUNT_REG(baseAddress) (baseAddress)->type.ata.r.size.b.SectorCount
  116. #define ATA_SECTOR_NUMBER_REG(baseAddress) (baseAddress)->type.ata.r.size.b.SectorNumber
  117. #define ATA_CYLINDER_LOW_REG(baseAddress) (baseAddress)->type.ata.r.size.b.CylinderLow
  118. #define ATA_CYLINDER_HIGH_REG(baseAddress) (baseAddress)->type.ata.r.size.b.CylinderHigh
  119. #define ATA_DRIVE_SELECT_REG(baseAddress) (baseAddress)->type.ata.r.size.b.DriveSelect
  120. #define ATA_STATUS_REG(baseAddress) (baseAddress)->type.ata.r.size.b.Status
  121. #define ATA_FEATURE_REG(baseAddress) (baseAddress)->type.ata.w.size.b.Feature
  122. #define ATA_COMMAND_REG(baseAddress) (baseAddress)->type.ata.w.size.b.Command
  123. //
  124. // handy atapi macros
  125. //
  126. #define ATAPI_DATA16_REG(baseAddress) (baseAddress)->type.atapi.r.size.w.Data16
  127. #define ATAPI_ERROR_REG(baseAddress) (baseAddress)->type.atapi.r.size.b.Error
  128. #define ATAPI_INTERRUPT_REASON_REG(baseAddress) (baseAddress)->type.atapi.r.size.b.InterruptReason
  129. #define ATAPI_BYTECOUNT_LOW_REG(baseAddress) (baseAddress)->type.atapi.r.size.b.ByteCountLow
  130. #define ATAPI_BYTECOUNT_HIGH_REG(baseAddress) (baseAddress)->type.atapi.r.size.b.ByteCountHigh
  131. #define ATAPI_DRIVE_SELECT_REG(baseAddress) (baseAddress)->type.atapi.r.size.b.DriveSelect
  132. #define ATAPI_STATUS_REG(baseAddress) (baseAddress)->type.atapi.r.size.b.Status
  133. #define ATAPI_FEATURE_REG(baseAddress) (baseAddress)->type.atapi.w.size.b.Feature
  134. #define ATAPI_COMMAND_REG(baseAddress) (baseAddress)->type.atapi.w.size.b.Command
  135. //
  136. // Device Extension Device Flags
  137. //
  138. #define DFLAGS_DEVICE_PRESENT (1 << 0) // Indicates that some device is present.
  139. #define DFLAGS_ATAPI_DEVICE (1 << 1) // Indicates whether Atapi commands can be used.
  140. #define DFLAGS_TAPE_DEVICE (1 << 2) // Indicates whether this is a tape device.
  141. #define DFLAGS_INT_DRQ (1 << 3) // Indicates whether device interrupts as DRQ is set after
  142. // receiving Atapi Packet Command
  143. #define DFLAGS_REMOVABLE_DRIVE (1 << 4) // Indicates that the drive has the 'removable' bit set in
  144. // identify data (offset 128)
  145. #define DFLAGS_MEDIA_STATUS_ENABLED (1 << 5) // Media status notification enabled
  146. #define DFLAGS_USE_DMA (1 << 9) // Indicates whether device can use DMA
  147. #define DFLAGS_LBA (1 << 10) // support LBA addressing
  148. #define DFLAGS_MULTI_LUN_INITED (1 << 11) // Indicates that the init path for multi-lun has already been done.
  149. #define DFLAGS_MSN_SUPPORT (1 << 12) // Device support media status notification
  150. #define DFLAGS_AUTO_EJECT_ZIP (1 << 13) // bootup default enables auto eject
  151. #define DFLAGS_WD_MODE (1 << 14) // Indicates that unit is WD-Mode(not SFF-Mode).
  152. #define DFLAGS_LS120_FORMAT (1 << 15) // Indicates that unit uses ATAPI_LS120_FORMAT_UNIT to format
  153. #define DFLAGS_USE_UDMA (1 << 16) // Indicates whether device can use UDMA
  154. #define DFLAGS_IDENTIFY_VALID (1 << 17) // Indicates whether the Identify data is valid or not
  155. #define DFLAGS_IDENTIFY_INVALID (1 << 18) // Indicates whether the Identify data is valid or not
  156. #define DFLAGS_RDP_SET (1 << 19) // If the srb is for RDP
  157. #define DFLAGS_SONY_MEMORYSTICK (1 << 20) // If the device is a Sony Memorystick
  158. #define DFLAGS_48BIT_LBA (1 << 21) // If the device supports 48-bit LBA
  159. #define DFLAGS_DEVICE_ERASED (1 << 22) // Indicates that some device is temporarily blocked for access.
  160. //
  161. // Used to disable 'advanced' features.
  162. //
  163. #define MAX_ERRORS 4
  164. //
  165. // ATAPI command definitions
  166. //
  167. #define ATAPI_MODE_SENSE 0x5A
  168. #define ATAPI_MODE_SELECT 0x55
  169. #define ATAPI_LS120_FORMAT_UNIT 0x24
  170. //
  171. // ATAPI mode page page code
  172. //
  173. #define ATAPI_NON_CD_DRIVE_OPERATION_MODE_PAGE_PAGECODE (0x00)
  174. #define ATAPI_REMOVABLE_BLOCK_ACCESS_CAPABILITIES_PAGECODE (0x1b)
  175. //
  176. // ATAPI Command Descriptor Block
  177. //
  178. typedef struct _MODE_PARAMETER_HEADER_10 {
  179. UCHAR ModeDataLengthMsb;
  180. UCHAR ModeDataLengthLsb;
  181. UCHAR MediumType;
  182. UCHAR Reserved[5];
  183. }MODE_PARAMETER_HEADER_10, *PMODE_PARAMETER_HEADER_10;
  184. typedef struct _ATAPI_REMOVABLE_BLOCK_ACCESS_CAPABILITIES {
  185. UCHAR PageCode : 6;
  186. UCHAR Reserved0 : 1;
  187. UCHAR PSBit : 1;
  188. UCHAR PageLength;
  189. UCHAR Reserved2:6;
  190. UCHAR SRFP:1;
  191. UCHAR SFLP:1;
  192. UCHAR TotalLun:3;
  193. UCHAR Reserved3:3;
  194. UCHAR SML:1;
  195. UCHAR NCD:1;
  196. UCHAR Reserved[8];
  197. } ATAPI_REMOVABLE_BLOCK_ACCESS_CAPABILITIES, *PATAPI_REMOVABLE_BLOCK_ACCESS_CAPABILITIES;
  198. typedef struct _ATAPI_NON_CD_DRIVE_OPERATION_MODE_PAGE {
  199. UCHAR PageCode : 6;
  200. UCHAR Reserved0 : 1;
  201. UCHAR PSBit : 1;
  202. UCHAR PageLength;
  203. UCHAR Reserved2:5;
  204. UCHAR DVW:1;
  205. UCHAR SLR:1;
  206. UCHAR SLM:1;
  207. UCHAR Reserved3:4;
  208. UCHAR DDE:1;
  209. UCHAR Reserved4:3;
  210. } ATAPI_NON_CD_DRIVE_OPERATION_MODE_PAGE, *PATAPI_NON_CD_DRIVE_OPERATION_MODE_PAGE;
  211. //
  212. // IDE command definitions
  213. //
  214. #define IDE_COMMAND_NOP 0x00
  215. #define IDE_COMMAND_ATAPI_RESET 0x08
  216. #define IDE_COMMAND_RECALIBRATE 0x10
  217. #define IDE_COMMAND_READ 0x20
  218. #define IDE_COMMAND_READ_EXT 0x24
  219. #define IDE_COMMAND_READ_DMA_EXT 0x25
  220. #define IDE_COMMAND_READ_DMA_QUEUED_EXT 0x26
  221. #define IDE_COMMAND_READ_MULTIPLE_EXT 0x29
  222. #define IDE_COMMAND_WRITE 0x30
  223. #define IDE_COMMAND_WRITE_EXT 0x34
  224. #define IDE_COMMAND_WRITE_DMA_EXT 0x35
  225. #define IDE_COMMAND_WRITE_DMA_QUEUED_EXT 0x36
  226. #define IDE_COMMAND_WRITE_MULTIPLE_EXT 0x39
  227. #define IDE_COMMAND_VERIFY 0x40
  228. #define IDE_COMMAND_VERIFY_EXT 0x42
  229. #define IDE_COMMAND_SEEK 0x70
  230. #define IDE_COMMAND_EXECUTE_DEVICE_DIAGNOSTIC 0x90
  231. #define IDE_COMMAND_SET_DRIVE_PARAMETERS 0x91
  232. #define IDE_COMMAND_ATAPI_PACKET 0xA0
  233. #define IDE_COMMAND_ATAPI_IDENTIFY 0xA1
  234. #define IDE_COMMAND_READ_MULTIPLE 0xC4
  235. #define IDE_COMMAND_WRITE_MULTIPLE 0xC5
  236. #define IDE_COMMAND_SET_MULTIPLE 0xC6
  237. #define IDE_COMMAND_READ_DMA 0xC8
  238. #define IDE_COMMAND_WRITE_DMA 0xCA
  239. #define IDE_COMMAND_GET_MEDIA_STATUS 0xDA
  240. #define IDE_COMMAND_STANDBY_IMMEDIATE 0xE0
  241. #define IDE_COMMAND_IDLE_IMMEDIATE 0xE1
  242. #define IDE_COMMAND_CHECK_POWER 0xE5
  243. #define IDE_COMMAND_SLEEP 0xE6
  244. #define IDE_COMMAND_FLUSH_CACHE 0xE7
  245. #define IDE_COMMAND_FLUSH_CACHE_EXT 0xEA
  246. #define IDE_COMMAND_IDENTIFY 0xEC
  247. #define IDE_COMMAND_MEDIA_EJECT 0xED
  248. #define IDE_COMMAND_SET_FEATURE 0xEF
  249. #define IDE_COMMAND_DOOR_LOCK 0xDE
  250. #define IDE_COMMAND_DOOR_UNLOCK 0xDF
  251. #define IDE_COMMAND_NO_FLUSH 0xFF // Commmand value to indicate the target device can't handle any flush command
  252. //
  253. // IDE Set Transfer Mode
  254. //
  255. #define IDE_SET_DEFAULT_PIO_MODE(mode) ((UCHAR) 1) // disable I/O Ready
  256. #define IDE_SET_ADVANCE_PIO_MODE(mode) ((UCHAR) ((1 << 3) | (mode)))
  257. #define IDE_SET_SWDMA_MODE(mode) ((UCHAR) ((1 << 4) | (mode)))
  258. #define IDE_SET_MWDMA_MODE(mode) ((UCHAR) ((1 << 5) | (mode)))
  259. #define IDE_SET_UDMA_MODE(mode) ((UCHAR) ((1 << 6) | (mode)))
  260. #define IDE_SET_FEATURE_SET_TRANSFER_MODE 0x3
  261. #define IDE_SET_FEATURE_ENABLE_WRITE_CACHE 0x2
  262. #define IDE_SET_FEATURE_DISABLE_WRITE_CACHE 0x82
  263. //
  264. // Media Status Set Feature
  265. //
  266. #define IDE_SET_FEATURE_ENABLE_MSN 0x95
  267. #define IDE_SET_FEATURE_DISABLE_MSN 0x31
  268. #define IDE_SET_FEATURE_DISABLE_REVERT_TO_POWER_ON 0x66
  269. //
  270. // IDE drive select/head definitions
  271. //
  272. #define IDE_DRIVE_SELECT_1 0xA0
  273. #define IDE_DRIVE_SELECT_2 0x10
  274. //
  275. // IDE error definitions
  276. //
  277. #define IDE_ERROR_BAD_BLOCK 0x80
  278. #define IDE_ERROR_CRC_ERROR IDE_ERROR_BAD_BLOCK
  279. #define IDE_ERROR_DATA_ERROR 0x40
  280. #define IDE_ERROR_MEDIA_CHANGE 0x20
  281. #define IDE_ERROR_ID_NOT_FOUND 0x10
  282. #define IDE_ERROR_MEDIA_CHANGE_REQ 0x08
  283. #define IDE_ERROR_COMMAND_ABORTED 0x04
  284. #define IDE_ERROR_END_OF_MEDIA 0x02
  285. #define IDE_ERROR_ILLEGAL_LENGTH 0x01
  286. //
  287. // ATAPI register definition
  288. //
  289. typedef struct _ATAPI_REGISTERS_1 {
  290. PUCHAR RegistersBaseAddress;
  291. PUSHORT Data;
  292. PUCHAR Error;
  293. PUCHAR InterruptReason;
  294. PUCHAR Unused1;
  295. PUCHAR ByteCountLow;
  296. PUCHAR ByteCountHigh;
  297. PUCHAR DriveSelect;
  298. PUCHAR Command;
  299. } ATAPI_REGISTERS_1, *PATAPI_REGISTERS_1;
  300. typedef struct _ATAPI_REGISTERS_2 {
  301. PUCHAR RegistersBaseAddress;
  302. PUCHAR DeviceControl;
  303. PUCHAR DriveAddress;
  304. } ATAPI_REGISTERS_2, *PATAPI_REGISTERS_2;
  305. //
  306. // ATAPI interrupt reasons
  307. //
  308. #define ATAPI_IR_COD 0x01
  309. #define ATAPI_IR_IO 0x02
  310. //
  311. // IDENTIFY data
  312. //
  313. /**************** Moved to ide.h *************
  314. #pragma pack (1)
  315. typedef struct _IDENTIFY_DATA {
  316. USHORT GeneralConfiguration; // 00 00
  317. USHORT NumCylinders; // 02 1
  318. USHORT Reserved1; // 04 2
  319. USHORT NumHeads; // 06 3
  320. USHORT UnformattedBytesPerTrack; // 08 4
  321. USHORT UnformattedBytesPerSector; // 0A 5
  322. USHORT NumSectorsPerTrack; // 0C 6
  323. USHORT VendorUnique1[3]; // 0E 7-9
  324. UCHAR SerialNumber[20]; // 14 10-19
  325. USHORT BufferType; // 28 20
  326. USHORT BufferSectorSize; // 2A 21
  327. USHORT NumberOfEccBytes; // 2C 22
  328. UCHAR FirmwareRevision[8]; // 2E 23-26
  329. UCHAR ModelNumber[40]; // 36 27-46
  330. UCHAR MaximumBlockTransfer; // 5E 47
  331. UCHAR VendorUnique2; // 5F
  332. USHORT DoubleWordIo; // 60 48
  333. USHORT Capabilities; // 62 49
  334. USHORT Reserved2; // 64 50
  335. UCHAR VendorUnique3; // 66 51
  336. UCHAR PioCycleTimingMode; // 67
  337. UCHAR VendorUnique4; // 68 52
  338. UCHAR DmaCycleTimingMode; // 69
  339. USHORT TranslationFieldsValid:3; // 6A 53
  340. USHORT Reserved3:13;
  341. USHORT NumberOfCurrentCylinders; // 6C 54
  342. USHORT NumberOfCurrentHeads; // 6E 55
  343. USHORT CurrentSectorsPerTrack; // 70 56
  344. ULONG CurrentSectorCapacity; // 72 57-58
  345. USHORT CurrentMultiSectorSetting; // 59
  346. ULONG UserAddressableSectors; // 60-61
  347. USHORT SingleWordDMASupport : 8; // 62
  348. USHORT SingleWordDMAActive : 8;
  349. USHORT MultiWordDMASupport : 8; // 63
  350. USHORT MultiWordDMAActive : 8;
  351. USHORT AdvancedPIOModes : 8; // 64
  352. USHORT Reserved4 : 8;
  353. USHORT MinimumMWXferCycleTime; // 65
  354. USHORT RecommendedMWXferCycleTime; // 66
  355. USHORT MinimumPIOCycleTime; // 67
  356. USHORT MinimumPIOCycleTimeIORDY; // 68
  357. USHORT Reserved5[11]; // 69-79
  358. USHORT MajorRevision; // 80
  359. USHORT MinorRevision; // 81
  360. USHORT Reserved6[6]; // 82-87
  361. USHORT UltraDMASupport : 8; // 88
  362. USHORT UltraDMAActive : 8; //
  363. USHORT Reserved7[37]; // 89-125
  364. USHORT LastLun:3; // 126
  365. USHORT Reserved8:13;
  366. USHORT MediaStatusNotification:2; // 127
  367. USHORT Reserved9:6;
  368. USHORT DeviceWriteProtect:1;
  369. USHORT Reserved10:7;
  370. USHORT Reserved11[128]; // 128-255
  371. } IDENTIFY_DATA, *PIDENTIFY_DATA;
  372. //
  373. // Identify data without the Reserved4.
  374. //
  375. //typedef struct _IDENTIFY_DATA2 {
  376. // USHORT GeneralConfiguration; // 00 00
  377. // USHORT NumCylinders; // 02 1
  378. // USHORT Reserved1; // 04 2
  379. // USHORT NumHeads; // 06 3
  380. // USHORT UnformattedBytesPerTrack; // 08 4
  381. // USHORT UnformattedBytesPerSector; // 0A 5
  382. // USHORT NumSectorsPerTrack; // 0C 6
  383. // USHORT VendorUnique1[3]; // 0E 7-9
  384. // UCHAR SerialNumber[20]; // 14 10-19
  385. // USHORT BufferType; // 28 20
  386. // USHORT BufferSectorSize; // 2A 21
  387. // USHORT NumberOfEccBytes; // 2C 22
  388. // UCHAR FirmwareRevision[8]; // 2E 23-26
  389. // UCHAR ModelNumber[40]; // 36 27-46
  390. // UCHAR MaximumBlockTransfer; // 5E 47
  391. // UCHAR VendorUnique2; // 5F
  392. // USHORT DoubleWordIo; // 60 48
  393. // USHORT Capabilities; // 62 49
  394. // USHORT Reserved2; // 64 50
  395. // UCHAR VendorUnique3; // 66 51
  396. // UCHAR PioCycleTimingMode; // 67
  397. // UCHAR VendorUnique4; // 68 52
  398. // UCHAR DmaCycleTimingMode; // 69
  399. // USHORT TranslationFieldsValid:3; // 6A 53
  400. // USHORT Reserved3:13;
  401. // USHORT NumberOfCurrentCylinders; // 6C 54
  402. // USHORT NumberOfCurrentHeads; // 6E 55
  403. // USHORT CurrentSectorsPerTrack; // 70 56
  404. // ULONG CurrentSectorCapacity; // 72 57-58
  405. // USHORT CurrentMultiSectorSetting; // 59
  406. // ULONG UserAddressableSectors; // 60-61
  407. // USHORT SingleWordDMASupport : 8; // 62
  408. // USHORT SingleWordDMAActive : 8;
  409. // USHORT MultiWordDMASupport : 8; // 63
  410. // USHORT MultiWordDMAActive : 8;
  411. // USHORT AdvancedPIOModes : 8; // 64
  412. // USHORT Reserved4 : 8;
  413. // USHORT MinimumMWXferCycleTime; // 65
  414. // USHORT RecommendedMWXferCycleTime; // 66
  415. // USHORT MinimumPIOCycleTime; // 67
  416. // USHORT MinimumPIOCycleTimeIORDY; // 68
  417. // USHORT Reserved5[11]; // 69-79
  418. // USHORT MajorRevision; // 80
  419. // USHORT MinorRevision; // 81
  420. // USHORT Reserved6[6]; // 82-87
  421. // USHORT UltraDMASupport : 8; // 88
  422. // USHORT UltraDMAActive : 8; //
  423. // USHORT Reserved7[37]; // 89-125
  424. // USHORT LastLun:3; // 126
  425. // USHORT Reserved8:13;
  426. // USHORT MediaStatusNotification:2; // 127
  427. // USHORT Reserved9:6;
  428. // USHORT DeviceWriteProtect:1;
  429. // USHORT Reserved10:7;
  430. //} IDENTIFY_DATA2, *PIDENTIFY_DATA2;
  431. #pragma pack ()
  432. #define IDENTIFY_DATA_SIZE sizeof(IDENTIFY_DATA)
  433. ******************************************************/
  434. //
  435. // Identify Data General Configuration Bit Definition
  436. //
  437. #define IDE_IDDATA_DEVICE_TYPE_MASK ((1 << 15) | (1 << 14))
  438. #define IDE_IDDATA_ATAPI_DEVICE ((1 << 15 | (0 << 14))
  439. #define IDE_IDDATA_ATAPI_DEVICE_MASK ((1 << 12) | (1 << 11) | (1 << 10) | (1 << 9) | (1 << 8))
  440. #define IDE_IDDATA_REMOVABLE (1 << 7)
  441. #define IDE_IDDATA_DRQ_TYPE_MASK ((1 << 6) | (1 << 5))
  442. #define IDE_IDDATA_INTERRUPT_DRQ ((1 << 6) | (0 << 5))
  443. //
  444. // Identify Data 48 bit lba support
  445. //
  446. #define IDE_IDDATA_48BIT_LBA_SUPPORT (1<<10)
  447. //
  448. // IDENTIFY capability bit definitions.
  449. //
  450. #define IDENTIFY_CAPABILITIES_DMA_SUPPORTED (1 << 8)
  451. #define IDENTIFY_CAPABILITIES_LBA_SUPPORTED (1 << 9)
  452. #define IDENTIFY_CAPABILITIES_IOREADY_CAN_BE_DISABLED (1 << 10)
  453. #define IDENTIFY_CAPABILITIES_IOREADY_SUPPORTED (1 << 11)
  454. //
  455. // Identify MediaStatusNotification
  456. //
  457. #define IDENTIFY_MEDIA_STATUS_NOTIFICATION_SUPPORTED (0x1)
  458. //
  459. // Select LBA mode when progran IDE device
  460. //
  461. #define IDE_LBA_MODE (1 << 6)
  462. //
  463. // ID DATA
  464. //
  465. /********** Not needed ****************
  466. #define IDD_UDMA_MODE0_ACTIVE (1 << 0)
  467. #define IDD_UDMA_MODE1_ACTIVE (1 << 1)
  468. #define IDD_UDMA_MODE2_ACTIVE (1 << 2)
  469. #define IDD_UDMA_MODE3_ACTIVE (1 << 3)
  470. #define IDD_UDMA_MODE4_ACTIVE (1 << 4)
  471. #define IDD_UDMA_MODE5_ACTIVE (1 << 5)
  472. #define IDD_MWDMA_MODE0_ACTIVE (1 << 0)
  473. #define IDD_MWDMA_MODE1_ACTIVE (1 << 1)
  474. #define IDD_MWDMA_MODE2_ACTIVE (1 << 2)
  475. #define IDD_SWDMA_MODE0_ACTIVE (1 << 0)
  476. #define IDD_SWDMA_MODE1_ACTIVE (1 << 1)
  477. #define IDD_SWDMA_MODE2_ACTIVE (1 << 2)
  478. #define IDD_UDMA_MODE0_SUPPORTED (1 << 0)
  479. #define IDD_UDMA_MODE1_SUPPORTED (1 << 1)
  480. #define IDD_UDMA_MODE2_SUPPORTED (1 << 2)
  481. #define IDD_MWDMA_MODE0_SUPPORTED (1 << 0)
  482. #define IDD_MWDMA_MODE1_SUPPORTED (1 << 1)
  483. #define IDD_MWDMA_MODE2_SUPPORTED (1 << 2)
  484. #define IDD_SWDMA_MODE0_SUPPORTED (1 << 0)
  485. #define IDD_SWDMA_MODE1_SUPPORTED (1 << 1)
  486. #define IDD_SWDMA_MODE2_SUPPORTED (1 << 2)
  487. ************/
  488. //
  489. // Beautification macros
  490. //
  491. #define HasSlaveDevice(HwExt, Target) (HwExt->DeviceFlags[(Target+1)%MAX_IDE_DEVICE] & DFLAGS_DEVICE_PRESENT)
  492. #ifdef ENABLE_ATAPI_VERIFIER
  493. #define GetBaseStatus(BaseIoAddress, Status) \
  494. Status = ViIdeGetBaseStatus((PIDE_REGISTERS_1)BaseIoAddress);
  495. #define GetErrorByte(BaseIoAddress, ErrorByte) \
  496. ErrorByte = ViIdeGetErrorByte((PIDE_REGISTERS_1)BaseIoAddress);
  497. #else
  498. #define GetBaseStatus(BaseIoAddress, Status) \
  499. Status = IdePortInPortByte((BaseIoAddress)->Command);
  500. #define GetErrorByte(BaseIoAddress, ErrorByte) \
  501. ErrorByte = IdePortInPortByte((BaseIoAddress)->Error);
  502. #endif
  503. #define WriteCommand(BaseIoAddress, Command) \
  504. IdePortOutPortByte((BaseIoAddress)->Command, Command);
  505. #define ReadBuffer(BaseIoAddress, Buffer, Count) \
  506. IdePortInPortWordBuffer((PUSHORT)(BaseIoAddress)->Data, Buffer, Count);
  507. #define WriteBuffer(BaseIoAddress, Buffer, Count) \
  508. IdePortOutPortWordBuffer((PUSHORT)(BaseIoAddress)->Data, Buffer, Count);
  509. #define WaitOnBusy(BaseIoAddress, Status) \
  510. { \
  511. ULONG sec; \
  512. ULONG i; \
  513. for (sec=0; sec<10; sec++) { \
  514. /**/ \
  515. /* one second loop */ \
  516. /**/ \
  517. for (i=0; i<2500; i++) { \
  518. GetStatus(BaseIoAddress, Status); \
  519. if (Status & IDE_STATUS_BUSY) { \
  520. KeStallExecutionProcessor(400); \
  521. continue; \
  522. } else { \
  523. break; \
  524. } \
  525. } \
  526. if (Status & IDE_STATUS_BUSY) { \
  527. DebugPrint ((1, "ATAPI: after 1 sec wait, device is still busy with 0x%x status = 0x%x\n", (BaseIoAddress)->RegistersBaseAddress, (ULONG) (Status))); \
  528. } else { \
  529. break; \
  530. } \
  531. } \
  532. if (Status & IDE_STATUS_BUSY) { \
  533. DebugPrint ((0, "WaitOnBusy failed in %s line %u. 0x%x status = 0x%x\n", __FILE__, __LINE__, (BaseIoAddress)->RegistersBaseAddress, (ULONG) (Status))); \
  534. } \
  535. }
  536. #define WaitForDRDY(BaseIoAddress, Status) \
  537. { \
  538. ULONG i; \
  539. WaitOnBusy(BaseIoAddress, Status);\
  540. for (i=0; i<20000; i++) { \
  541. GetStatus(BaseIoAddress, Status); \
  542. if (!(Status & IDE_STATUS_IDLE)) { \
  543. KeStallExecutionProcessor(150); \
  544. continue; \
  545. } else { \
  546. break; \
  547. } \
  548. } \
  549. if (i == 20000) \
  550. DebugPrint ((0, "WaitForDRDY failed in %s line %u. 0x%x status = 0x%x\n", __FILE__, __LINE__, (BaseIoAddress)->RegistersBaseAddress, (ULONG) (Status))); \
  551. }
  552. #define WaitOnBusyUntil(BaseIoAddress, Status, Millisec) \
  553. { \
  554. ULONG i; \
  555. ULONG maxCount = Millisec * 10;\
  556. for (i=0; i<maxCount; i++) { \
  557. GetStatus(BaseIoAddress, Status); \
  558. if (Status & IDE_STATUS_BUSY) { \
  559. KeStallExecutionProcessor(100); \
  560. continue; \
  561. } else { \
  562. break; \
  563. } \
  564. } \
  565. if (i == maxCount) \
  566. DebugPrint ((0, "WaitOnBusyUntil failed in %s line %u. status = 0x%x\n", __FILE__, __LINE__, (ULONG) (Status))); \
  567. }
  568. #define WaitOnBaseBusy(BaseIoAddress, Status) \
  569. { \
  570. ULONG i; \
  571. for (i=0; i<20000; i++) { \
  572. GetBaseStatus(BaseIoAddress, Status); \
  573. if (Status & IDE_STATUS_BUSY) { \
  574. KeStallExecutionProcessor(150); \
  575. continue; \
  576. } else { \
  577. break; \
  578. } \
  579. } \
  580. }
  581. #define WaitForDrq(BaseIoAddress, Status) \
  582. { \
  583. ULONG i; \
  584. for (i=0; i<1000; i++) { \
  585. GetStatus(BaseIoAddress, Status); \
  586. if (Status & IDE_STATUS_BUSY) { \
  587. KeStallExecutionProcessor(100); \
  588. } else if (Status & IDE_STATUS_DRQ) { \
  589. break; \
  590. } else { \
  591. KeStallExecutionProcessor(200); \
  592. } \
  593. } \
  594. }
  595. #define WaitShortForDrq(BaseIoAddress, Status) \
  596. { \
  597. ULONG i; \
  598. for (i=0; i<2; i++) { \
  599. GetStatus(BaseIoAddress, Status); \
  600. if (Status & IDE_STATUS_BUSY) { \
  601. KeStallExecutionProcessor(100); \
  602. } else if (Status & IDE_STATUS_DRQ) { \
  603. break; \
  604. } else { \
  605. KeStallExecutionProcessor(100); \
  606. } \
  607. } \
  608. }
  609. #define AtapiSoftReset(BaseIoAddress1, BaseIoAddress2, DeviceNumber, interruptOff) \
  610. {\
  611. ULONG __i;\
  612. UCHAR statusByte; \
  613. SelectIdeDevice(BaseIoAddress1, DeviceNumber, 0); \
  614. KeStallExecutionProcessor(500);\
  615. IdePortOutPortByte((BaseIoAddress1)->Command, IDE_COMMAND_ATAPI_RESET); \
  616. KeStallExecutionProcessor(500);\
  617. SelectIdeDevice(BaseIoAddress1, DeviceNumber, 0); \
  618. WaitOnBusy(BaseIoAddress1, statusByte); \
  619. if ( !Is98LegacyIde(BaseIoAddress1) ) { \
  620. KeStallExecutionProcessor(500); \
  621. } else { \
  622. for (__i = 0; __i < 20; __i++) { \
  623. KeStallExecutionProcessor(500); \
  624. } \
  625. } \
  626. if (interruptOff) { \
  627. IdePortOutPortByte(BaseIoAddress2->DeviceControl, IDE_DC_DISABLE_INTERRUPTS); \
  628. } \
  629. }
  630. #define SAVE_ORIGINAL_CDB(DeviceExtension, Srb) \
  631. RtlCopyMemory(DeviceExtension->OriginalCdb, Srb->Cdb, sizeof(CDB));
  632. #define RESTORE_ORIGINAL_CDB(DeviceExtension, Srb) \
  633. RtlCopyMemory(Srb->Cdb, DeviceExtension->OriginalCdb, sizeof(CDB));
  634. //
  635. // NEC 98: Buffer size of mode sense data.
  636. //
  637. #define MODE_DATA_SIZE 192
  638. typedef enum {
  639. IdeResetBegin = 0,
  640. ideResetBusResetInProgress,
  641. ideResetAtapiReset,
  642. ideResetAtapiResetInProgress,
  643. ideResetAtapiIdentifyData,
  644. ideResetAtaIDP,
  645. ideResetAtaIDPInProgress,
  646. ideResetAtaMSN,
  647. ideResetFinal
  648. } IDE_RESET_STATE;
  649. //
  650. // Definition in ide.h
  651. //
  652. struct IDENTIFY_DATA;
  653. //
  654. // Device extension
  655. //
  656. typedef struct _HW_DEVICE_EXTENSION {
  657. //
  658. // Current request on controller.
  659. //
  660. PSCSI_REQUEST_BLOCK CurrentSrb;
  661. //
  662. // Base register locations
  663. //
  664. IDE_REGISTERS_1 BaseIoAddress1;
  665. IDE_REGISTERS_2 BaseIoAddress2;
  666. //
  667. // Register length.
  668. //
  669. ULONG BaseIoAddress1Length;
  670. ULONG BaseIoAddress2Length;
  671. //
  672. // Max ide device/target-id
  673. //
  674. ULONG MaxIdeDevice;
  675. ULONG MaxIdeTargetId;
  676. //
  677. // Variables to check empty channel
  678. //
  679. #ifdef DPC_FOR_EMPTY_CHANNEL
  680. ULONG CurrentIdeDevice;
  681. ULONG MoreWait;
  682. ULONG NoRetry;
  683. #endif
  684. //
  685. // Drive Geometry
  686. //
  687. ULONG NumberOfCylinders[MAX_IDE_DEVICE * MAX_IDE_LINE];
  688. ULONG NumberOfHeads[MAX_IDE_DEVICE * MAX_IDE_LINE];
  689. ULONG SectorsPerTrack[MAX_IDE_DEVICE * MAX_IDE_LINE];
  690. //
  691. // Interrupt Mode (Level or Edge)
  692. //
  693. ULONG InterruptMode;
  694. //
  695. // Data buffer pointer.
  696. //
  697. PUCHAR DataBuffer;
  698. //
  699. // Data words left.
  700. //
  701. ULONG BytesLeft;
  702. //
  703. // Count of errors. Used to turn off features.
  704. //
  705. ULONG ErrorCount;
  706. //
  707. // Count of timeouts. Used to turn off features.
  708. //
  709. ULONG TimeoutCount[MAX_IDE_DEVICE * MAX_IDE_LINE];
  710. //
  711. // Indicates number of platters on changer-ish devices.
  712. //
  713. ULONG LastLun[MAX_IDE_DEVICE * MAX_IDE_LINE];
  714. //
  715. // Flags word for each possible device.
  716. //
  717. ULONG DeviceFlags[MAX_IDE_DEVICE * MAX_IDE_LINE];
  718. //
  719. // Indicates the number of blocks transferred per int. according to the
  720. // identify data.
  721. //
  722. UCHAR MaximumBlockXfer[MAX_IDE_DEVICE * MAX_IDE_LINE];
  723. //
  724. // Indicates expecting an interrupt
  725. //
  726. BOOLEAN ExpectingInterrupt;
  727. //
  728. // Indicates DMA is in progress
  729. //
  730. BOOLEAN DMAInProgress;
  731. //
  732. // Keep track of whether we convert a SCSI command to ATAPI on the fly
  733. //
  734. BOOLEAN scsi2atapi;
  735. //
  736. // Indicate last tape command was DSC Restrictive.
  737. //
  738. BOOLEAN RDP;
  739. //
  740. // Driver is being used by the crash dump utility or ntldr.
  741. //
  742. BOOLEAN DriverMustPoll;
  743. //
  744. // Indicates whether '0x1f0' is the base address. Used
  745. // in SMART Ioctl calls.
  746. //
  747. BOOLEAN PrimaryAddress;
  748. BOOLEAN SecondaryAddress;
  749. //
  750. // No IDE_SET_FEATURE_SET_TRANSFER_MODE
  751. //
  752. BOOLEAN NoPioSetTransferMode;
  753. //
  754. // Placeholder for the original cdb
  755. //
  756. UCHAR OriginalCdb[16];
  757. //
  758. // Placeholder for the sub-command value of the last
  759. // SMART command.
  760. //
  761. UCHAR SmartCommand;
  762. //
  763. // Placeholder for status register after a GET_MEDIA_STATUS command
  764. //
  765. UCHAR ReturningMediaStatus;
  766. //
  767. // Identify data for device
  768. //
  769. IDENTIFY_DATA IdentifyData[MAX_IDE_DEVICE * MAX_IDE_LINE];
  770. //
  771. // PCI IDE Parent bus master interface
  772. //
  773. PCIIDE_BUSMASTER_INTERFACE BusMasterInterface;
  774. //
  775. // Device Specific Info.
  776. //
  777. struct _DEVICE_PARAMETERS {
  778. ULONG MaxBytePerPioInterrupt;
  779. UCHAR IdePioReadCommand;
  780. UCHAR IdePioWriteCommand;
  781. UCHAR IdeFlushCommand;
  782. UCHAR IdePioReadCommandExt;
  783. UCHAR IdePioWriteCommandExt;
  784. UCHAR IdeFlushCommandExt;
  785. //
  786. // Timing Stuff
  787. //
  788. BOOLEAN IoReadyEnabled;
  789. ULONG BestPioCycleTime;
  790. ULONG BestSwDmaCycleTime;
  791. ULONG BestMwDmaCycleTime;
  792. ULONG BestUDmaCycleTime;
  793. ULONG TransferModeSupported;
  794. ULONG BestPioMode;
  795. ULONG BestSwDmaMode;
  796. ULONG BestMwDmaMode;
  797. ULONG BestUDmaMode;
  798. ULONG TransferModeCurrent;
  799. ULONG TransferModeSelected;
  800. ULONG TransferModeMask;
  801. } DeviceParameters[MAX_IDE_DEVICE * MAX_IDE_LINE];
  802. #define RESET_STATE_TABLE_LEN (((2 + 3 * MAX_IDE_DEVICE) * MAX_IDE_LINE) + 1)
  803. struct RESET_STATE {
  804. ULONG WaitBusyCount;
  805. IDE_RESET_STATE State[RESET_STATE_TABLE_LEN];
  806. IDE_RESET_STATE DeviceNumber[RESET_STATE_TABLE_LEN];
  807. } ResetState;
  808. } HW_DEVICE_EXTENSION, *PHW_DEVICE_EXTENSION;
  809. //
  810. // max number of CHS addressable sectors
  811. //
  812. //#define MAX_NUM_CHS_ADDRESSABLE_SECTORS ((ULONG) (16515072 - 1))
  813. #define MAX_NUM_CHS_ADDRESSABLE_SECTORS ((ULONG) (16514064))
  814. #define MAX_28BIT_LBA ((ULONG) (1<<28))
  815. //
  816. // IDE Cycle Timing
  817. //
  818. /****************************Moved to idep.h****************
  819. #define PIO_MODE0_CYCLE_TIME 600
  820. #define PIO_MODE1_CYCLE_TIME 383
  821. #define PIO_MODE2_CYCLE_TIME 240
  822. #define PIO_MODE3_CYCLE_TIME 180
  823. #define PIO_MODE4_CYCLE_TIME 120
  824. #define SWDMA_MODE0_CYCLE_TIME 960
  825. #define SWDMA_MODE1_CYCLE_TIME 480
  826. #define SWDMA_MODE2_CYCLE_TIME 240
  827. #define MWDMA_MODE0_CYCLE_TIME 480
  828. #define MWDMA_MODE1_CYCLE_TIME 150
  829. #define MWDMA_MODE2_CYCLE_TIME 120
  830. #define UDMA_MODE0_CYCLE_TIME 120
  831. #define UDMA_MODE1_CYCLE_TIME 80
  832. #define UDMA_MODE2_CYCLE_TIME 60
  833. #define UDMA_MODE3_CYCLE_TIME 45
  834. #define UDMA_MODE4_CYCLE_TIME 30
  835. #define UDMA_MODE5_CYCLE_TIME 15
  836. #define UNINITIALIZED_CYCLE_TIME 0xffffffff
  837. #define UNINITIALIZED_TRANSFER_MODE 0xffffffff
  838. */
  839. BOOLEAN
  840. AtapiInterrupt(
  841. IN PVOID HwDeviceExtension
  842. );
  843. BOOLEAN
  844. AtapiHwInitialize(
  845. IN PVOID HwDeviceExtension,
  846. IN UCHAR FlushCommand[MAX_IDE_DEVICE * MAX_IDE_LINE]
  847. );
  848. BOOLEAN
  849. AtapiStartIo(
  850. IN PVOID HwDeviceExtension,
  851. IN PSCSI_REQUEST_BLOCK Srb
  852. );
  853. BOOLEAN
  854. AtapiResetController(
  855. IN PVOID HwDeviceExtension,
  856. IN ULONG PathId,
  857. IN PULONG CallAgain
  858. );
  859. VOID
  860. InitDeviceParameters (
  861. IN PVOID HwDeviceExtension,
  862. IN UCHAR FlushCommand[MAX_IDE_DEVICE * MAX_IDE_LINE]
  863. );
  864. VOID
  865. AtapiProgramTransferMode (
  866. PHW_DEVICE_EXTENSION DeviceExtension
  867. );
  868. VOID
  869. AtapiHwInitializeMultiLun (
  870. IN PVOID HwDeviceExtension,
  871. IN ULONG TargetId,
  872. IN ULONG numSlot
  873. );
  874. ULONG
  875. AtapiSendCommand(
  876. IN PVOID HwDeviceExtension,
  877. IN PSCSI_REQUEST_BLOCK Srb
  878. );
  879. ULONG
  880. IdeBuildSenseBuffer(
  881. IN PVOID HwDeviceExtension,
  882. IN PSCSI_REQUEST_BLOCK Srb
  883. );
  884. VOID
  885. IdeMediaStatus(
  886. IN BOOLEAN EnableMSN,
  887. IN PVOID HwDeviceExtension,
  888. IN ULONG DeviceNumber
  889. );
  890. VOID
  891. DeviceSpecificInitialize(
  892. IN PVOID HwDeviceExtension
  893. );
  894. BOOLEAN
  895. EnableBusMasterController (
  896. IN PVOID HwDeviceExtension,
  897. IN PCHAR userArgumentString
  898. );
  899. BOOLEAN
  900. AtapiDeviceDMACapable (
  901. IN PVOID HwDeviceExtension,
  902. IN ULONG deviceNumber
  903. );
  904. BOOLEAN
  905. GetAtapiIdentifyQuick (
  906. PIDE_REGISTERS_1 BaseIoAddress1,
  907. PIDE_REGISTERS_2 BaseIoAddress2,
  908. IN ULONG DeviceNumber,
  909. OUT PIDENTIFY_DATA IdentifyData
  910. );
  911. BOOLEAN
  912. IssueIdentify(
  913. PIDE_REGISTERS_1 CmdBaseAddr,
  914. PIDE_REGISTERS_2 CtrlBaseAddr,
  915. IN ULONG DeviceNumber,
  916. IN UCHAR Command,
  917. IN BOOLEAN InterruptOff,
  918. OUT PIDENTIFY_DATA IdentifyData
  919. );
  920. VOID
  921. InitDeviceGeometry(
  922. PHW_DEVICE_EXTENSION HwDeviceExtension,
  923. ULONG Device,
  924. ULONG NumberOfCylinders,
  925. ULONG NumberOfHeads,
  926. ULONG SectorsPerTrack
  927. );
  928. VOID
  929. InitHwExtWithIdentify(
  930. IN PVOID HwDeviceExtension,
  931. IN ULONG DeviceNumber,
  932. IN UCHAR Command,
  933. IN PIDENTIFY_DATA IdentifyData,
  934. IN BOOLEAN RemovableMedia
  935. );
  936. BOOLEAN
  937. SetDriveParameters(
  938. IN PVOID HwDeviceExtension,
  939. IN ULONG DeviceNumber,
  940. IN BOOLEAN Sync
  941. );
  942. BOOLEAN
  943. FindDevices(
  944. IN PVOID HwDeviceExtension,
  945. IN BOOLEAN AtapiOnly
  946. );
  947. ULONG
  948. IdeSendPassThroughCommand(
  949. IN PVOID HwDeviceExtension,
  950. IN PSCSI_REQUEST_BLOCK Srb
  951. );
  952. BOOLEAN
  953. AtapiSyncResetController(
  954. IN PVOID HwDeviceExtension,
  955. IN ULONG PathId
  956. );
  957. NTSTATUS
  958. IdeHardReset (
  959. PIDE_REGISTERS_1 BaseIoAddress1,
  960. PIDE_REGISTERS_2 BaseIoAddress2,
  961. BOOLEAN InterruptOff,
  962. BOOLEAN Sync
  963. );
  964. ULONG
  965. IdeReadWrite(
  966. IN PVOID HwDeviceExtension,
  967. IN PSCSI_REQUEST_BLOCK Srb
  968. );
  969. VOID
  970. AtapiTaskRegisterSnapshot (
  971. IN PIDE_REGISTERS_1 CmdRegBase,
  972. IN OUT PIDEREGS IdeReg
  973. );
  974. NTSTATUS
  975. AtapiSetTransferMode (
  976. PHW_DEVICE_EXTENSION DeviceExtension,
  977. ULONG DeviceNumber,
  978. UCHAR ModeValue
  979. );
  980. #define ATA_VERSION_MASK (0xfffe)
  981. #define ATA1_COMPLIANCE (1 << 1)
  982. #define ATA2_COMPLIANCE (1 << 2)
  983. #define ATA3_COMPLIANCE (1 << 3)
  984. #define ATA4_COMPLIANCE (1 << 4)
  985. #endif // ___atapi_h___