libata.tmpl 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820
  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="libataEH">
  361. <title>Error handling</title>
  362. <para>
  363. This chapter describes how errors are handled under libata.
  364. Readers are advised to read SCSI EH
  365. (Documentation/scsi/scsi_eh.txt) and ATA exceptions doc first.
  366. </para>
  367. <sect1><title>Origins of commands</title>
  368. <para>
  369. In libata, a command is represented with struct ata_queued_cmd
  370. or qc. qc's are preallocated during port initialization and
  371. repetitively used for command executions. Currently only one
  372. qc is allocated per port but yet-to-be-merged NCQ branch
  373. allocates one for each tag and maps each qc to NCQ tag 1-to-1.
  374. </para>
  375. <para>
  376. libata commands can originate from two sources - libata itself
  377. and SCSI midlayer. libata internal commands are used for
  378. initialization and error handling. All normal blk requests
  379. and commands for SCSI emulation are passed as SCSI commands
  380. through queuecommand callback of SCSI host template.
  381. </para>
  382. </sect1>
  383. <sect1><title>How commands are issued</title>
  384. <variablelist>
  385. <varlistentry><term>Internal commands</term>
  386. <listitem>
  387. <para>
  388. First, qc is allocated and initialized using
  389. ata_qc_new_init(). Although ata_qc_new_init() doesn't
  390. implement any wait or retry mechanism when qc is not
  391. available, internal commands are currently issued only during
  392. initialization and error recovery, so no other command is
  393. active and allocation is guaranteed to succeed.
  394. </para>
  395. <para>
  396. Once allocated qc's taskfile is initialized for the command to
  397. be executed. qc currently has two mechanisms to notify
  398. completion. One is via qc->complete_fn() callback and the
  399. other is completion qc->waiting. qc->complete_fn() callback
  400. is the asynchronous path used by normal SCSI translated
  401. commands and qc->waiting is the synchronous (issuer sleeps in
  402. process context) path used by internal commands.
  403. </para>
  404. <para>
  405. Once initialization is complete, host_set lock is acquired
  406. and the qc is issued.
  407. </para>
  408. </listitem>
  409. </varlistentry>
  410. <varlistentry><term>SCSI commands</term>
  411. <listitem>
  412. <para>
  413. All libata drivers use ata_scsi_queuecmd() as
  414. hostt->queuecommand callback. scmds can either be simulated
  415. or translated. No qc is involved in processing a simulated
  416. scmd. The result is computed right away and the scmd is
  417. completed.
  418. </para>
  419. <para>
  420. For a translated scmd, ata_qc_new_init() is invoked to
  421. allocate a qc and the scmd is translated into the qc. SCSI
  422. midlayer's completion notification function pointer is stored
  423. into qc->scsidone.
  424. </para>
  425. <para>
  426. qc->complete_fn() callback is used for completion
  427. notification. ATA commands use ata_scsi_qc_complete() while
  428. ATAPI commands use atapi_qc_complete(). Both functions end up
  429. calling qc->scsidone to notify upper layer when the qc is
  430. finished. After translation is completed, the qc is issued
  431. with ata_qc_issue().
  432. </para>
  433. <para>
  434. Note that SCSI midlayer invokes hostt->queuecommand while
  435. holding host_set lock, so all above occur while holding
  436. host_set lock.
  437. </para>
  438. </listitem>
  439. </varlistentry>
  440. </variablelist>
  441. </sect1>
  442. <sect1><title>How commands are processed</title>
  443. <para>
  444. Depending on which protocol and which controller are used,
  445. commands are processed differently. For the purpose of
  446. discussion, a controller which uses taskfile interface and all
  447. standard callbacks is assumed.
  448. </para>
  449. <para>
  450. Currently 6 ATA command protocols are used. They can be
  451. sorted into the following four categories according to how
  452. they are processed.
  453. </para>
  454. <variablelist>
  455. <varlistentry><term>ATA NO DATA or DMA</term>
  456. <listitem>
  457. <para>
  458. ATA_PROT_NODATA and ATA_PROT_DMA fall into this category.
  459. These types of commands don't require any software
  460. intervention once issued. Device will raise interrupt on
  461. completion.
  462. </para>
  463. </listitem>
  464. </varlistentry>
  465. <varlistentry><term>ATA PIO</term>
  466. <listitem>
  467. <para>
  468. ATA_PROT_PIO is in this category. libata currently
  469. implements PIO with polling. ATA_NIEN bit is set to turn
  470. off interrupt and pio_task on ata_wq performs polling and
  471. IO.
  472. </para>
  473. </listitem>
  474. </varlistentry>
  475. <varlistentry><term>ATAPI NODATA or DMA</term>
  476. <listitem>
  477. <para>
  478. ATA_PROT_ATAPI_NODATA and ATA_PROT_ATAPI_DMA are in this
  479. category. packet_task is used to poll BSY bit after
  480. issuing PACKET command. Once BSY is turned off by the
  481. device, packet_task transfers CDB and hands off processing
  482. to interrupt handler.
  483. </para>
  484. </listitem>
  485. </varlistentry>
  486. <varlistentry><term>ATAPI PIO</term>
  487. <listitem>
  488. <para>
  489. ATA_PROT_ATAPI is in this category. ATA_NIEN bit is set
  490. and, as in ATAPI NODATA or DMA, packet_task submits cdb.
  491. However, after submitting cdb, further processing (data
  492. transfer) is handed off to pio_task.
  493. </para>
  494. </listitem>
  495. </varlistentry>
  496. </variablelist>
  497. </sect1>
  498. <sect1><title>How commands are completed</title>
  499. <para>
  500. Once issued, all qc's are either completed with
  501. ata_qc_complete() or time out. For commands which are handled
  502. by interrupts, ata_host_intr() invokes ata_qc_complete(), and,
  503. for PIO tasks, pio_task invokes ata_qc_complete(). In error
  504. cases, packet_task may also complete commands.
  505. </para>
  506. <para>
  507. ata_qc_complete() does the following.
  508. </para>
  509. <orderedlist>
  510. <listitem>
  511. <para>
  512. DMA memory is unmapped.
  513. </para>
  514. </listitem>
  515. <listitem>
  516. <para>
  517. ATA_QCFLAG_ACTIVE is clared from qc->flags.
  518. </para>
  519. </listitem>
  520. <listitem>
  521. <para>
  522. qc->complete_fn() callback is invoked. If the return value of
  523. the callback is not zero. Completion is short circuited and
  524. ata_qc_complete() returns.
  525. </para>
  526. </listitem>
  527. <listitem>
  528. <para>
  529. __ata_qc_complete() is called, which does
  530. <orderedlist>
  531. <listitem>
  532. <para>
  533. qc->flags is cleared to zero.
  534. </para>
  535. </listitem>
  536. <listitem>
  537. <para>
  538. ap->active_tag and qc->tag are poisoned.
  539. </para>
  540. </listitem>
  541. <listitem>
  542. <para>
  543. qc->waiting is claread &amp; completed (in that order).
  544. </para>
  545. </listitem>
  546. <listitem>
  547. <para>
  548. qc is deallocated by clearing appropriate bit in ap->qactive.
  549. </para>
  550. </listitem>
  551. </orderedlist>
  552. </para>
  553. </listitem>
  554. </orderedlist>
  555. <para>
  556. So, it basically notifies upper layer and deallocates qc. One
  557. exception is short-circuit path in #3 which is used by
  558. atapi_qc_complete().
  559. </para>
  560. <para>
  561. For all non-ATAPI commands, whether it fails or not, almost
  562. the same code path is taken and very little error handling
  563. takes place. A qc is completed with success status if it
  564. succeeded, with failed status otherwise.
  565. </para>
  566. <para>
  567. However, failed ATAPI commands require more handling as
  568. REQUEST SENSE is needed to acquire sense data. If an ATAPI
  569. command fails, ata_qc_complete() is invoked with error status,
  570. which in turn invokes atapi_qc_complete() via
  571. qc->complete_fn() callback.
  572. </para>
  573. <para>
  574. This makes atapi_qc_complete() set scmd->result to
  575. SAM_STAT_CHECK_CONDITION, complete the scmd and return 1. As
  576. the sense data is empty but scmd->result is CHECK CONDITION,
  577. SCSI midlayer will invoke EH for the scmd, and returning 1
  578. makes ata_qc_complete() to return without deallocating the qc.
  579. This leads us to ata_scsi_error() with partially completed qc.
  580. </para>
  581. </sect1>
  582. <sect1><title>ata_scsi_error()</title>
  583. <para>
  584. ata_scsi_error() is the current hostt->eh_strategy_handler()
  585. for libata. As discussed above, this will be entered in two
  586. cases - timeout and ATAPI error completion. This function
  587. calls low level libata driver's eng_timeout() callback, the
  588. standard callback for which is ata_eng_timeout(). It checks
  589. if a qc is active and calls ata_qc_timeout() on the qc if so.
  590. Actual error handling occurs in ata_qc_timeout().
  591. </para>
  592. <para>
  593. If EH is invoked for timeout, ata_qc_timeout() stops BMDMA and
  594. completes the qc. Note that as we're currently in EH, we
  595. cannot call scsi_done. As described in SCSI EH doc, a
  596. recovered scmd should be either retried with
  597. scsi_queue_insert() or finished with scsi_finish_command().
  598. Here, we override qc->scsidone with scsi_finish_command() and
  599. calls ata_qc_complete().
  600. </para>
  601. <para>
  602. If EH is invoked due to a failed ATAPI qc, the qc here is
  603. completed but not deallocated. The purpose of this
  604. half-completion is to use the qc as place holder to make EH
  605. code reach this place. This is a bit hackish, but it works.
  606. </para>
  607. <para>
  608. Once control reaches here, the qc is deallocated by invoking
  609. __ata_qc_complete() explicitly. Then, internal qc for REQUEST
  610. SENSE is issued. Once sense data is acquired, scmd is
  611. finished by directly invoking scsi_finish_command() on the
  612. scmd. Note that as we already have completed and deallocated
  613. the qc which was associated with the scmd, we don't need
  614. to/cannot call ata_qc_complete() again.
  615. </para>
  616. </sect1>
  617. <sect1><title>Problems with the current EH</title>
  618. <itemizedlist>
  619. <listitem>
  620. <para>
  621. Error representation is too crude. Currently any and all
  622. error conditions are represented with ATA STATUS and ERROR
  623. registers. Errors which aren't ATA device errors are treated
  624. as ATA device errors by setting ATA_ERR bit. Better error
  625. descriptor which can properly represent ATA and other
  626. errors/exceptions is needed.
  627. </para>
  628. </listitem>
  629. <listitem>
  630. <para>
  631. When handling timeouts, no action is taken to make device
  632. forget about the timed out command and ready for new commands.
  633. </para>
  634. </listitem>
  635. <listitem>
  636. <para>
  637. EH handling via ata_scsi_error() is not properly protected
  638. from usual command processing. On EH entrance, the device is
  639. not in quiescent state. Timed out commands may succeed or
  640. fail any time. pio_task and atapi_task may still be running.
  641. </para>
  642. </listitem>
  643. <listitem>
  644. <para>
  645. Too weak error recovery. Devices / controllers causing HSM
  646. mismatch errors and other errors quite often require reset to
  647. return to known state. Also, advanced error handling is
  648. necessary to support features like NCQ and hotplug.
  649. </para>
  650. </listitem>
  651. <listitem>
  652. <para>
  653. ATA errors are directly handled in the interrupt handler and
  654. PIO errors in pio_task. This is problematic for advanced
  655. error handling for the following reasons.
  656. </para>
  657. <para>
  658. First, advanced error handling often requires context and
  659. internal qc execution.
  660. </para>
  661. <para>
  662. Second, even a simple failure (say, CRC error) needs
  663. information gathering and could trigger complex error handling
  664. (say, resetting &amp; reconfiguring). Having multiple code
  665. paths to gather information, enter EH and trigger actions
  666. makes life painful.
  667. </para>
  668. <para>
  669. Third, scattered EH code makes implementing low level drivers
  670. difficult. Low level drivers override libata callbacks. If
  671. EH is scattered over several places, each affected callbacks
  672. should perform its part of error handling. This can be error
  673. prone and painful.
  674. </para>
  675. </listitem>
  676. </itemizedlist>
  677. </sect1>
  678. </chapter>
  679. <chapter id="libataExt">
  680. <title>libata Library</title>
  681. !Edrivers/scsi/libata-core.c
  682. </chapter>
  683. <chapter id="libataInt">
  684. <title>libata Core Internals</title>
  685. !Idrivers/scsi/libata-core.c
  686. </chapter>
  687. <chapter id="libataScsiInt">
  688. <title>libata SCSI translation/emulation</title>
  689. !Edrivers/scsi/libata-scsi.c
  690. !Idrivers/scsi/libata-scsi.c
  691. </chapter>
  692. <chapter id="PiixInt">
  693. <title>ata_piix Internals</title>
  694. !Idrivers/scsi/ata_piix.c
  695. </chapter>
  696. <chapter id="SILInt">
  697. <title>sata_sil Internals</title>
  698. !Idrivers/scsi/sata_sil.c
  699. </chapter>
  700. <chapter id="libataThanks">
  701. <title>Thanks</title>
  702. <para>
  703. The bulk of the ATA knowledge comes thanks to long conversations with
  704. Andre Hedrick (www.linux-ide.org), and long hours pondering the ATA
  705. and SCSI specifications.
  706. </para>
  707. <para>
  708. Thanks to Alan Cox for pointing out similarities
  709. between SATA and SCSI, and in general for motivation to hack on
  710. libata.
  711. </para>
  712. <para>
  713. libata's device detection
  714. method, ata_pio_devchk, and in general all the early probing was
  715. based on extensive study of Hale Landis's probe/reset code in his
  716. ATADRVR driver (www.ata-atapi.com).
  717. </para>
  718. </chapter>
  719. </book>