hdio.rst 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547
  1. ==============================
  2. Summary of `HDIO_` ioctl calls
  3. ==============================
  4. - Edward A. Falk <efalk@google.com>
  5. November, 2004
  6. This document attempts to describe the ioctl(2) calls supported by
  7. the HD/IDE layer. These are by-and-large implemented (as of Linux 5.11)
  8. drivers/ata/libata-scsi.c.
  9. ioctl values are listed in <linux/hdreg.h>. As of this writing, they
  10. are as follows:
  11. ioctls that pass argument pointers to user space:
  12. ======================= =======================================
  13. HDIO_GETGEO get device geometry
  14. HDIO_GET_32BIT get current io_32bit setting
  15. HDIO_GET_IDENTITY get IDE identification info
  16. HDIO_DRIVE_TASKFILE execute raw taskfile
  17. HDIO_DRIVE_TASK execute task and special drive command
  18. HDIO_DRIVE_CMD execute a special drive command
  19. ======================= =======================================
  20. ioctls that pass non-pointer values:
  21. ======================= =======================================
  22. HDIO_SET_32BIT change io_32bit flags
  23. ======================= =======================================
  24. The information that follows was determined from reading kernel source
  25. code. It is likely that some corrections will be made over time.
  26. ------------------------------------------------------------------------------
  27. General:
  28. Unless otherwise specified, all ioctl calls return 0 on success
  29. and -1 with errno set to an appropriate value on error.
  30. Unless otherwise specified, all ioctl calls return -1 and set
  31. errno to EFAULT on a failed attempt to copy data to or from user
  32. address space.
  33. Unless otherwise specified, all data structures and constants
  34. are defined in <linux/hdreg.h>
  35. ------------------------------------------------------------------------------
  36. HDIO_GETGEO
  37. get device geometry
  38. usage::
  39. struct hd_geometry geom;
  40. ioctl(fd, HDIO_GETGEO, &geom);
  41. inputs:
  42. none
  43. outputs:
  44. hd_geometry structure containing:
  45. ========= ==================================
  46. heads number of heads
  47. sectors number of sectors/track
  48. cylinders number of cylinders, mod 65536
  49. start starting sector of this partition.
  50. ========= ==================================
  51. error returns:
  52. - EINVAL
  53. if the device is not a disk drive or floppy drive,
  54. or if the user passes a null pointer
  55. notes:
  56. Not particularly useful with modern disk drives, whose geometry
  57. is a polite fiction anyway. Modern drives are addressed
  58. purely by sector number nowadays (lba addressing), and the
  59. drive geometry is an abstraction which is actually subject
  60. to change. Currently (as of Nov 2004), the geometry values
  61. are the "bios" values -- presumably the values the drive had
  62. when Linux first booted.
  63. In addition, the cylinders field of the hd_geometry is an
  64. unsigned short, meaning that on most architectures, this
  65. ioctl will not return a meaningful value on drives with more
  66. than 65535 tracks.
  67. The start field is unsigned long, meaning that it will not
  68. contain a meaningful value for disks over 219 Gb in size.
  69. HDIO_GET_IDENTITY
  70. get IDE identification info
  71. usage::
  72. unsigned char identity[512];
  73. ioctl(fd, HDIO_GET_IDENTITY, identity);
  74. inputs:
  75. none
  76. outputs:
  77. ATA drive identity information. For full description, see
  78. the IDENTIFY DEVICE and IDENTIFY PACKET DEVICE commands in
  79. the ATA specification.
  80. error returns:
  81. - EINVAL Called on a partition instead of the whole disk device
  82. - ENOMSG IDENTIFY DEVICE information not available
  83. notes:
  84. Returns information that was obtained when the drive was
  85. probed. Some of this information is subject to change, and
  86. this ioctl does not re-probe the drive to update the
  87. information.
  88. This information is also available from /proc/ide/hdX/identify
  89. HDIO_GET_32BIT
  90. get current io_32bit setting
  91. usage::
  92. long val;
  93. ioctl(fd, HDIO_GET_32BIT, &val);
  94. inputs:
  95. none
  96. outputs:
  97. The value of the current io_32bit setting
  98. notes:
  99. 0=16-bit, 1=32-bit, 2,3 = 32bit+sync
  100. HDIO_DRIVE_TASKFILE
  101. execute raw taskfile
  102. Note:
  103. If you don't have a copy of the ANSI ATA specification
  104. handy, you should probably ignore this ioctl.
  105. - Execute an ATA disk command directly by writing the "taskfile"
  106. registers of the drive. Requires ADMIN and RAWIO access
  107. privileges.
  108. usage::
  109. struct {
  110. ide_task_request_t req_task;
  111. u8 outbuf[OUTPUT_SIZE];
  112. u8 inbuf[INPUT_SIZE];
  113. } task;
  114. memset(&task.req_task, 0, sizeof(task.req_task));
  115. task.req_task.out_size = sizeof(task.outbuf);
  116. task.req_task.in_size = sizeof(task.inbuf);
  117. ...
  118. ioctl(fd, HDIO_DRIVE_TASKFILE, &task);
  119. ...
  120. inputs:
  121. (See below for details on memory area passed to ioctl.)
  122. ============ ===================================================
  123. io_ports[8] values to be written to taskfile registers
  124. hob_ports[8] high-order bytes, for extended commands.
  125. out_flags flags indicating which registers are valid
  126. in_flags flags indicating which registers should be returned
  127. data_phase see below
  128. req_cmd command type to be executed
  129. out_size size of output buffer
  130. outbuf buffer of data to be transmitted to disk
  131. inbuf buffer of data to be received from disk (see [1])
  132. ============ ===================================================
  133. outputs:
  134. =========== ====================================================
  135. io_ports[] values returned in the taskfile registers
  136. hob_ports[] high-order bytes, for extended commands.
  137. out_flags flags indicating which registers are valid (see [2])
  138. in_flags flags indicating which registers should be returned
  139. outbuf buffer of data to be transmitted to disk (see [1])
  140. inbuf buffer of data to be received from disk
  141. =========== ====================================================
  142. error returns:
  143. - EACCES CAP_SYS_ADMIN or CAP_SYS_RAWIO privilege not set.
  144. - ENOMSG Device is not a disk drive.
  145. - ENOMEM Unable to allocate memory for task
  146. - EFAULT req_cmd == TASKFILE_IN_OUT (not implemented as of 2.6.8)
  147. - EPERM
  148. req_cmd == TASKFILE_MULTI_OUT and drive
  149. multi-count not yet set.
  150. - EIO Drive failed the command.
  151. notes:
  152. [1] READ THE FOLLOWING NOTES *CAREFULLY*. THIS IOCTL IS
  153. FULL OF GOTCHAS. Extreme caution should be used with using
  154. this ioctl. A mistake can easily corrupt data or hang the
  155. system.
  156. [2] Both the input and output buffers are copied from the
  157. user and written back to the user, even when not used.
  158. [3] If one or more bits are set in out_flags and in_flags is
  159. zero, the following values are used for in_flags.all and
  160. written back into in_flags on completion.
  161. * IDE_TASKFILE_STD_IN_FLAGS | (IDE_HOB_STD_IN_FLAGS << 8)
  162. if LBA48 addressing is enabled for the drive
  163. * IDE_TASKFILE_STD_IN_FLAGS
  164. if CHS/LBA28
  165. The association between in_flags.all and each enable
  166. bitfield flips depending on endianness; fortunately, TASKFILE
  167. only uses inflags.b.data bit and ignores all other bits.
  168. The end result is that, on any endian machines, it has no
  169. effect other than modifying in_flags on completion.
  170. [4] The default value of SELECT is (0xa0|DEV_bit|LBA_bit)
  171. except for four drives per port chipsets. For four drives
  172. per port chipsets, it's (0xa0|DEV_bit|LBA_bit) for the first
  173. pair and (0x80|DEV_bit|LBA_bit) for the second pair.
  174. [5] The argument to the ioctl is a pointer to a region of
  175. memory containing a ide_task_request_t structure, followed
  176. by an optional buffer of data to be transmitted to the
  177. drive, followed by an optional buffer to receive data from
  178. the drive.
  179. Command is passed to the disk drive via the ide_task_request_t
  180. structure, which contains these fields:
  181. ============ ===============================================
  182. io_ports[8] values for the taskfile registers
  183. hob_ports[8] high-order bytes, for extended commands
  184. out_flags flags indicating which entries in the
  185. io_ports[] and hob_ports[] arrays
  186. contain valid values. Type ide_reg_valid_t.
  187. in_flags flags indicating which entries in the
  188. io_ports[] and hob_ports[] arrays
  189. are expected to contain valid values
  190. on return.
  191. data_phase See below
  192. req_cmd Command type, see below
  193. out_size output (user->drive) buffer size, bytes
  194. in_size input (drive->user) buffer size, bytes
  195. ============ ===============================================
  196. When out_flags is zero, the following registers are loaded.
  197. ============ ===============================================
  198. HOB_FEATURE If the drive supports LBA48
  199. HOB_NSECTOR If the drive supports LBA48
  200. HOB_SECTOR If the drive supports LBA48
  201. HOB_LCYL If the drive supports LBA48
  202. HOB_HCYL If the drive supports LBA48
  203. FEATURE
  204. NSECTOR
  205. SECTOR
  206. LCYL
  207. HCYL
  208. SELECT First, masked with 0xE0 if LBA48, 0xEF
  209. otherwise; then, or'ed with the default
  210. value of SELECT.
  211. ============ ===============================================
  212. If any bit in out_flags is set, the following registers are loaded.
  213. ============ ===============================================
  214. HOB_DATA If out_flags.b.data is set. HOB_DATA will
  215. travel on DD8-DD15 on little endian machines
  216. and on DD0-DD7 on big endian machines.
  217. DATA If out_flags.b.data is set. DATA will
  218. travel on DD0-DD7 on little endian machines
  219. and on DD8-DD15 on big endian machines.
  220. HOB_NSECTOR If out_flags.b.nsector_hob is set
  221. HOB_SECTOR If out_flags.b.sector_hob is set
  222. HOB_LCYL If out_flags.b.lcyl_hob is set
  223. HOB_HCYL If out_flags.b.hcyl_hob is set
  224. FEATURE If out_flags.b.feature is set
  225. NSECTOR If out_flags.b.nsector is set
  226. SECTOR If out_flags.b.sector is set
  227. LCYL If out_flags.b.lcyl is set
  228. HCYL If out_flags.b.hcyl is set
  229. SELECT Or'ed with the default value of SELECT and
  230. loaded regardless of out_flags.b.select.
  231. ============ ===============================================
  232. Taskfile registers are read back from the drive into
  233. {io|hob}_ports[] after the command completes iff one of the
  234. following conditions is met; otherwise, the original values
  235. will be written back, unchanged.
  236. 1. The drive fails the command (EIO).
  237. 2. One or more than one bits are set in out_flags.
  238. 3. The requested data_phase is TASKFILE_NO_DATA.
  239. ============ ===============================================
  240. HOB_DATA If in_flags.b.data is set. It will contain
  241. DD8-DD15 on little endian machines and
  242. DD0-DD7 on big endian machines.
  243. DATA If in_flags.b.data is set. It will contain
  244. DD0-DD7 on little endian machines and
  245. DD8-DD15 on big endian machines.
  246. HOB_FEATURE If the drive supports LBA48
  247. HOB_NSECTOR If the drive supports LBA48
  248. HOB_SECTOR If the drive supports LBA48
  249. HOB_LCYL If the drive supports LBA48
  250. HOB_HCYL If the drive supports LBA48
  251. NSECTOR
  252. SECTOR
  253. LCYL
  254. HCYL
  255. ============ ===============================================
  256. The data_phase field describes the data transfer to be
  257. performed. Value is one of:
  258. =================== ========================================
  259. TASKFILE_IN
  260. TASKFILE_MULTI_IN
  261. TASKFILE_OUT
  262. TASKFILE_MULTI_OUT
  263. TASKFILE_IN_OUT
  264. TASKFILE_IN_DMA
  265. TASKFILE_IN_DMAQ == IN_DMA (queueing not supported)
  266. TASKFILE_OUT_DMA
  267. TASKFILE_OUT_DMAQ == OUT_DMA (queueing not supported)
  268. TASKFILE_P_IN unimplemented
  269. TASKFILE_P_IN_DMA unimplemented
  270. TASKFILE_P_IN_DMAQ unimplemented
  271. TASKFILE_P_OUT unimplemented
  272. TASKFILE_P_OUT_DMA unimplemented
  273. TASKFILE_P_OUT_DMAQ unimplemented
  274. =================== ========================================
  275. The req_cmd field classifies the command type. It may be
  276. one of:
  277. ======================== =======================================
  278. IDE_DRIVE_TASK_NO_DATA
  279. IDE_DRIVE_TASK_SET_XFER unimplemented
  280. IDE_DRIVE_TASK_IN
  281. IDE_DRIVE_TASK_OUT unimplemented
  282. IDE_DRIVE_TASK_RAW_WRITE
  283. ======================== =======================================
  284. [6] Do not access {in|out}_flags->all except for resetting
  285. all the bits. Always access individual bit fields. ->all
  286. value will flip depending on endianness. For the same
  287. reason, do not use IDE_{TASKFILE|HOB}_STD_{OUT|IN}_FLAGS
  288. constants defined in hdreg.h.
  289. HDIO_DRIVE_CMD
  290. execute a special drive command
  291. Note: If you don't have a copy of the ANSI ATA specification
  292. handy, you should probably ignore this ioctl.
  293. usage::
  294. u8 args[4+XFER_SIZE];
  295. ...
  296. ioctl(fd, HDIO_DRIVE_CMD, args);
  297. inputs:
  298. Commands other than WIN_SMART:
  299. ======= =======
  300. args[0] COMMAND
  301. args[1] NSECTOR
  302. args[2] FEATURE
  303. args[3] NSECTOR
  304. ======= =======
  305. WIN_SMART:
  306. ======= =======
  307. args[0] COMMAND
  308. args[1] SECTOR
  309. args[2] FEATURE
  310. args[3] NSECTOR
  311. ======= =======
  312. outputs:
  313. args[] buffer is filled with register values followed by any
  314. data returned by the disk.
  315. ======== ====================================================
  316. args[0] status
  317. args[1] error
  318. args[2] NSECTOR
  319. args[3] undefined
  320. args[4+] NSECTOR * 512 bytes of data returned by the command.
  321. ======== ====================================================
  322. error returns:
  323. - EACCES Access denied: requires CAP_SYS_RAWIO
  324. - ENOMEM Unable to allocate memory for task
  325. - EIO Drive reports error
  326. notes:
  327. [1] For commands other than WIN_SMART, args[1] should equal
  328. args[3]. SECTOR, LCYL and HCYL are undefined. For
  329. WIN_SMART, 0x4f and 0xc2 are loaded into LCYL and HCYL
  330. respectively. In both cases SELECT will contain the default
  331. value for the drive. Please refer to HDIO_DRIVE_TASKFILE
  332. notes for the default value of SELECT.
  333. [2] If NSECTOR value is greater than zero and the drive sets
  334. DRQ when interrupting for the command, NSECTOR * 512 bytes
  335. are read from the device into the area following NSECTOR.
  336. In the above example, the area would be
  337. args[4..4+XFER_SIZE]. 16bit PIO is used regardless of
  338. HDIO_SET_32BIT setting.
  339. [3] If COMMAND == WIN_SETFEATURES && FEATURE == SETFEATURES_XFER
  340. && NSECTOR >= XFER_SW_DMA_0 && the drive supports any DMA
  341. mode, IDE driver will try to tune the transfer mode of the
  342. drive accordingly.
  343. HDIO_DRIVE_TASK
  344. execute task and special drive command
  345. Note: If you don't have a copy of the ANSI ATA specification
  346. handy, you should probably ignore this ioctl.
  347. usage::
  348. u8 args[7];
  349. ...
  350. ioctl(fd, HDIO_DRIVE_TASK, args);
  351. inputs:
  352. Taskfile register values:
  353. ======= =======
  354. args[0] COMMAND
  355. args[1] FEATURE
  356. args[2] NSECTOR
  357. args[3] SECTOR
  358. args[4] LCYL
  359. args[5] HCYL
  360. args[6] SELECT
  361. ======= =======
  362. outputs:
  363. Taskfile register values:
  364. ======= =======
  365. args[0] status
  366. args[1] error
  367. args[2] NSECTOR
  368. args[3] SECTOR
  369. args[4] LCYL
  370. args[5] HCYL
  371. args[6] SELECT
  372. ======= =======
  373. error returns:
  374. - EACCES Access denied: requires CAP_SYS_RAWIO
  375. - ENOMEM Unable to allocate memory for task
  376. - ENOMSG Device is not a disk drive.
  377. - EIO Drive failed the command.
  378. notes:
  379. [1] DEV bit (0x10) of SELECT register is ignored and the
  380. appropriate value for the drive is used. All other bits
  381. are used unaltered.
  382. HDIO_SET_32BIT
  383. change io_32bit flags
  384. usage::
  385. int val;
  386. ioctl(fd, HDIO_SET_32BIT, val);
  387. inputs:
  388. New value for io_32bit flag
  389. outputs:
  390. none
  391. error return:
  392. - EINVAL Called on a partition instead of the whole disk device
  393. - EACCES Access denied: requires CAP_SYS_ADMIN
  394. - EINVAL value out of range [0 3]
  395. - EBUSY Controller busy