libata.tmpl 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE book PUBLIC "-//OASIS//DTD DocBook XML V4.1.2//EN"
  3. "http://www.oasis-open.org/docbook/xml/4.1.2/docbookx.dtd" []>
  4. <book id="libataDevGuide">
  5. <bookinfo>
  6. <title>libATA Developer's Guide</title>
  7. <authorgroup>
  8. <author>
  9. <firstname>Jeff</firstname>
  10. <surname>Garzik</surname>
  11. </author>
  12. </authorgroup>
  13. <copyright>
  14. <year>2003-2005</year>
  15. <holder>Jeff Garzik</holder>
  16. </copyright>
  17. <legalnotice>
  18. <para>
  19. The contents of this file are subject to the Open
  20. Software License version 1.1 that can be found at
  21. <ulink url="http://www.opensource.org/licenses/osl-1.1.txt">http://www.opensource.org/licenses/osl-1.1.txt</ulink> and is included herein
  22. by reference.
  23. </para>
  24. <para>
  25. Alternatively, the contents of this file may be used under the terms
  26. of the GNU General Public License version 2 (the "GPL") as distributed
  27. in the kernel source COPYING file, in which case the provisions of
  28. the GPL are applicable instead of the above. If you wish to allow
  29. the use of your version of this file only under the terms of the
  30. GPL and not to allow others to use your version of this file under
  31. the OSL, indicate your decision by deleting the provisions above and
  32. replace them with the notice and other provisions required by the GPL.
  33. If you do not delete the provisions above, a recipient may use your
  34. version of this file under either the OSL or the GPL.
  35. </para>
  36. </legalnotice>
  37. </bookinfo>
  38. <toc></toc>
  39. <chapter id="libataIntroduction">
  40. <title>Introduction</title>
  41. <para>
  42. libATA is a library used inside the Linux kernel to support ATA host
  43. controllers and devices. libATA provides an ATA driver API, class
  44. transports for ATA and ATAPI devices, and SCSI&lt;-&gt;ATA translation
  45. for ATA devices according to the T10 SAT specification.
  46. </para>
  47. <para>
  48. This Guide documents the libATA driver API, library functions, library
  49. internals, and a couple sample ATA low-level drivers.
  50. </para>
  51. </chapter>
  52. <chapter id="libataDriverApi">
  53. <title>libata Driver API</title>
  54. <para>
  55. struct ata_port_operations is defined for every low-level libata
  56. hardware driver, and it controls how the low-level driver
  57. interfaces with the ATA and SCSI layers.
  58. </para>
  59. <para>
  60. FIS-based drivers will hook into the system with ->qc_prep() and
  61. ->qc_issue() high-level hooks. Hardware which behaves in a manner
  62. similar to PCI IDE hardware may utilize several generic helpers,
  63. defining at a bare minimum the bus I/O addresses of the ATA shadow
  64. register blocks.
  65. </para>
  66. <sect1>
  67. <title>struct ata_port_operations</title>
  68. <sect2><title>Disable ATA port</title>
  69. <programlisting>
  70. void (*port_disable) (struct ata_port *);
  71. </programlisting>
  72. <para>
  73. Called from ata_bus_probe() and ata_bus_reset() error paths,
  74. as well as when unregistering from the SCSI module (rmmod, hot
  75. unplug).
  76. This function should do whatever needs to be done to take the
  77. port out of use. In most cases, ata_port_disable() can be used
  78. as this hook.
  79. </para>
  80. <para>
  81. Called from ata_bus_probe() on a failed probe.
  82. Called from ata_bus_reset() on a failed bus reset.
  83. Called from ata_scsi_release().
  84. </para>
  85. </sect2>
  86. <sect2><title>Post-IDENTIFY device configuration</title>
  87. <programlisting>
  88. void (*dev_config) (struct ata_port *, struct ata_device *);
  89. </programlisting>
  90. <para>
  91. Called after IDENTIFY [PACKET] DEVICE is issued to each device
  92. found. Typically used to apply device-specific fixups prior to
  93. issue of SET FEATURES - XFER MODE, and prior to operation.
  94. </para>
  95. <para>
  96. Called by ata_device_add() after ata_dev_identify() determines
  97. a device is present.
  98. </para>
  99. <para>
  100. This entry may be specified as NULL in ata_port_operations.
  101. </para>
  102. </sect2>
  103. <sect2><title>Set PIO/DMA mode</title>
  104. <programlisting>
  105. void (*set_piomode) (struct ata_port *, struct ata_device *);
  106. void (*set_dmamode) (struct ata_port *, struct ata_device *);
  107. void (*post_set_mode) (struct ata_port *ap);
  108. </programlisting>
  109. <para>
  110. Hooks called prior to the issue of SET FEATURES - XFER MODE
  111. command. dev->pio_mode is guaranteed to be valid when
  112. ->set_piomode() is called, and dev->dma_mode is guaranteed to be
  113. valid when ->set_dmamode() is called. ->post_set_mode() is
  114. called unconditionally, after the SET FEATURES - XFER MODE
  115. command completes successfully.
  116. </para>
  117. <para>
  118. ->set_piomode() is always called (if present), but
  119. ->set_dma_mode() is only called if DMA is possible.
  120. </para>
  121. </sect2>
  122. <sect2><title>Taskfile read/write</title>
  123. <programlisting>
  124. void (*tf_load) (struct ata_port *ap, struct ata_taskfile *tf);
  125. void (*tf_read) (struct ata_port *ap, struct ata_taskfile *tf);
  126. </programlisting>
  127. <para>
  128. ->tf_load() is called to load the given taskfile into hardware
  129. registers / DMA buffers. ->tf_read() is called to read the
  130. hardware registers / DMA buffers, to obtain the current set of
  131. taskfile register values.
  132. Most drivers for taskfile-based hardware (PIO or MMIO) use
  133. ata_tf_load() and ata_tf_read() for these hooks.
  134. </para>
  135. </sect2>
  136. <sect2><title>ATA command execute</title>
  137. <programlisting>
  138. void (*exec_command)(struct ata_port *ap, struct ata_taskfile *tf);
  139. </programlisting>
  140. <para>
  141. causes an ATA command, previously loaded with
  142. ->tf_load(), to be initiated in hardware.
  143. Most drivers for taskfile-based hardware use ata_exec_command()
  144. for this hook.
  145. </para>
  146. </sect2>
  147. <sect2><title>Per-cmd ATAPI DMA capabilities filter</title>
  148. <programlisting>
  149. int (*check_atapi_dma) (struct ata_queued_cmd *qc);
  150. </programlisting>
  151. <para>
  152. Allow low-level driver to filter ATA PACKET commands, returning a status
  153. indicating whether or not it is OK to use DMA for the supplied PACKET
  154. command.
  155. </para>
  156. <para>
  157. This hook may be specified as NULL, in which case libata will
  158. assume that atapi dma can be supported.
  159. </para>
  160. </sect2>
  161. <sect2><title>Read specific ATA shadow registers</title>
  162. <programlisting>
  163. u8 (*check_status)(struct ata_port *ap);
  164. u8 (*check_altstatus)(struct ata_port *ap);
  165. u8 (*check_err)(struct ata_port *ap);
  166. </programlisting>
  167. <para>
  168. Reads the Status/AltStatus/Error ATA shadow register from
  169. hardware. On some hardware, reading the Status register has
  170. the side effect of clearing the interrupt condition.
  171. Most drivers for taskfile-based hardware use
  172. ata_check_status() for this hook.
  173. </para>
  174. <para>
  175. Note that because this is called from ata_device_add(), at
  176. least a dummy function that clears device interrupts must be
  177. provided for all drivers, even if the controller doesn't
  178. actually have a taskfile status register.
  179. </para>
  180. </sect2>
  181. <sect2><title>Select ATA device on bus</title>
  182. <programlisting>
  183. void (*dev_select)(struct ata_port *ap, unsigned int device);
  184. </programlisting>
  185. <para>
  186. Issues the low-level hardware command(s) that causes one of N
  187. hardware devices to be considered 'selected' (active and
  188. available for use) on the ATA bus. This generally has no
  189. meaning on FIS-based devices.
  190. </para>
  191. <para>
  192. Most drivers for taskfile-based hardware use
  193. ata_std_dev_select() for this hook. Controllers which do not
  194. support second drives on a port (such as SATA contollers) will
  195. use ata_noop_dev_select().
  196. </para>
  197. </sect2>
  198. <sect2><title>Reset ATA bus</title>
  199. <programlisting>
  200. void (*phy_reset) (struct ata_port *ap);
  201. </programlisting>
  202. <para>
  203. The very first step in the probe phase. Actions vary depending
  204. on the bus type, typically. After waking up the device and probing
  205. for device presence (PATA and SATA), typically a soft reset
  206. (SRST) will be performed. Drivers typically use the helper
  207. functions ata_bus_reset() or sata_phy_reset() for this hook.
  208. Many SATA drivers use sata_phy_reset() or call it from within
  209. their own phy_reset() functions.
  210. </para>
  211. </sect2>
  212. <sect2><title>Control PCI IDE BMDMA engine</title>
  213. <programlisting>
  214. void (*bmdma_setup) (struct ata_queued_cmd *qc);
  215. void (*bmdma_start) (struct ata_queued_cmd *qc);
  216. void (*bmdma_stop) (struct ata_port *ap);
  217. u8 (*bmdma_status) (struct ata_port *ap);
  218. </programlisting>
  219. <para>
  220. When setting up an IDE BMDMA transaction, these hooks arm
  221. (->bmdma_setup), fire (->bmdma_start), and halt (->bmdma_stop)
  222. the hardware's DMA engine. ->bmdma_status is used to read the standard
  223. PCI IDE DMA Status register.
  224. </para>
  225. <para>
  226. These hooks are typically either no-ops, or simply not implemented, in
  227. FIS-based drivers.
  228. </para>
  229. <para>
  230. Most legacy IDE drivers use ata_bmdma_setup() for the bmdma_setup()
  231. hook. ata_bmdma_setup() will write the pointer to the PRD table to
  232. the IDE PRD Table Address register, enable DMA in the DMA Command
  233. register, and call exec_command() to begin the transfer.
  234. </para>
  235. <para>
  236. Most legacy IDE drivers use ata_bmdma_start() for the bmdma_start()
  237. hook. ata_bmdma_start() will write the ATA_DMA_START flag to the DMA
  238. Command register.
  239. </para>
  240. <para>
  241. Many legacy IDE drivers use ata_bmdma_stop() for the bmdma_stop()
  242. hook. ata_bmdma_stop() clears the ATA_DMA_START flag in the DMA
  243. command register.
  244. </para>
  245. <para>
  246. Many legacy IDE drivers use ata_bmdma_status() as the bmdma_status() hook.
  247. </para>
  248. </sect2>
  249. <sect2><title>High-level taskfile hooks</title>
  250. <programlisting>
  251. void (*qc_prep) (struct ata_queued_cmd *qc);
  252. int (*qc_issue) (struct ata_queued_cmd *qc);
  253. </programlisting>
  254. <para>
  255. Higher-level hooks, these two hooks can potentially supercede
  256. several of the above taskfile/DMA engine hooks. ->qc_prep is
  257. called after the buffers have been DMA-mapped, and is typically
  258. used to populate the hardware's DMA scatter-gather table.
  259. Most drivers use the standard ata_qc_prep() helper function, but
  260. more advanced drivers roll their own.
  261. </para>
  262. <para>
  263. ->qc_issue is used to make a command active, once the hardware
  264. and S/G tables have been prepared. IDE BMDMA drivers use the
  265. helper function ata_qc_issue_prot() for taskfile protocol-based
  266. dispatch. More advanced drivers implement their own ->qc_issue.
  267. </para>
  268. <para>
  269. ata_qc_issue_prot() calls ->tf_load(), ->bmdma_setup(), and
  270. ->bmdma_start() as necessary to initiate a transfer.
  271. </para>
  272. </sect2>
  273. <sect2><title>Timeout (error) handling</title>
  274. <programlisting>
  275. void (*eng_timeout) (struct ata_port *ap);
  276. </programlisting>
  277. <para>
  278. This is a high level error handling function, called from the
  279. error handling thread, when a command times out. Most newer
  280. hardware will implement its own error handling code here. IDE BMDMA
  281. drivers may use the helper function ata_eng_timeout().
  282. </para>
  283. </sect2>
  284. <sect2><title>Hardware interrupt handling</title>
  285. <programlisting>
  286. irqreturn_t (*irq_handler)(int, void *, struct pt_regs *);
  287. void (*irq_clear) (struct ata_port *);
  288. </programlisting>
  289. <para>
  290. ->irq_handler is the interrupt handling routine registered with
  291. the system, by libata. ->irq_clear is called during probe just
  292. before the interrupt handler is registered, to be sure hardware
  293. is quiet.
  294. </para>
  295. <para>
  296. The second argument, dev_instance, should be cast to a pointer
  297. to struct ata_host_set.
  298. </para>
  299. <para>
  300. Most legacy IDE drivers use ata_interrupt() for the
  301. irq_handler hook, which scans all ports in the host_set,
  302. determines which queued command was active (if any), and calls
  303. ata_host_intr(ap,qc).
  304. </para>
  305. <para>
  306. Most legacy IDE drivers use ata_bmdma_irq_clear() for the
  307. irq_clear() hook, which simply clears the interrupt and error
  308. flags in the DMA status register.
  309. </para>
  310. </sect2>
  311. <sect2><title>SATA phy read/write</title>
  312. <programlisting>
  313. u32 (*scr_read) (struct ata_port *ap, unsigned int sc_reg);
  314. void (*scr_write) (struct ata_port *ap, unsigned int sc_reg,
  315. u32 val);
  316. </programlisting>
  317. <para>
  318. Read and write standard SATA phy registers. Currently only used
  319. if ->phy_reset hook called the sata_phy_reset() helper function.
  320. sc_reg is one of SCR_STATUS, SCR_CONTROL, SCR_ERROR, or SCR_ACTIVE.
  321. </para>
  322. </sect2>
  323. <sect2><title>Init and shutdown</title>
  324. <programlisting>
  325. int (*port_start) (struct ata_port *ap);
  326. void (*port_stop) (struct ata_port *ap);
  327. void (*host_stop) (struct ata_host_set *host_set);
  328. </programlisting>
  329. <para>
  330. ->port_start() is called just after the data structures for each
  331. port are initialized. Typically this is used to alloc per-port
  332. DMA buffers / tables / rings, enable DMA engines, and similar
  333. tasks. Some drivers also use this entry point as a chance to
  334. allocate driver-private memory for ap->private_data.
  335. </para>
  336. <para>
  337. Many drivers use ata_port_start() as this hook or call
  338. it from their own port_start() hooks. ata_port_start()
  339. allocates space for a legacy IDE PRD table and returns.
  340. </para>
  341. <para>
  342. ->port_stop() is called after ->host_stop(). It's sole function
  343. is to release DMA/memory resources, now that they are no longer
  344. actively being used. Many drivers also free driver-private
  345. data from port at this time.
  346. </para>
  347. <para>
  348. Many drivers use ata_port_stop() as this hook, which frees the
  349. PRD table.
  350. </para>
  351. <para>
  352. ->host_stop() is called after all ->port_stop() calls
  353. have completed. The hook must finalize hardware shutdown, release DMA
  354. and other resources, etc.
  355. This hook may be specified as NULL, in which case it is not called.
  356. </para>
  357. </sect2>
  358. </sect1>
  359. </chapter>
  360. <chapter id="libataExt">
  361. <title>libata Library</title>
  362. !Edrivers/scsi/libata-core.c
  363. </chapter>
  364. <chapter id="libataInt">
  365. <title>libata Core Internals</title>
  366. !Idrivers/scsi/libata-core.c
  367. </chapter>
  368. <chapter id="libataScsiInt">
  369. <title>libata SCSI translation/emulation</title>
  370. !Edrivers/scsi/libata-scsi.c
  371. !Idrivers/scsi/libata-scsi.c
  372. </chapter>
  373. <chapter id="PiixInt">
  374. <title>ata_piix Internals</title>
  375. !Idrivers/scsi/ata_piix.c
  376. </chapter>
  377. <chapter id="SILInt">
  378. <title>sata_sil Internals</title>
  379. !Idrivers/scsi/sata_sil.c
  380. </chapter>
  381. <chapter id="libataThanks">
  382. <title>Thanks</title>
  383. <para>
  384. The bulk of the ATA knowledge comes thanks to long conversations with
  385. Andre Hedrick (www.linux-ide.org), and long hours pondering the ATA
  386. and SCSI specifications.
  387. </para>
  388. <para>
  389. Thanks to Alan Cox for pointing out similarities
  390. between SATA and SCSI, and in general for motivation to hack on
  391. libata.
  392. </para>
  393. <para>
  394. libata's device detection
  395. method, ata_pio_devchk, and in general all the early probing was
  396. based on extensive study of Hale Landis's probe/reset code in his
  397. ATADRVR driver (www.ata-atapi.com).
  398. </para>
  399. </chapter>
  400. </book>