libata.tmpl 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575
  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 *);
  108. unsigned int (*mode_filter) (struct ata_port *, struct ata_device *, unsigned int);
  109. </programlisting>
  110. <para>
  111. Hooks called prior to the issue of SET FEATURES - XFER MODE
  112. command. The optional ->mode_filter() hook is called when libata
  113. has built a mask of the possible modes. This is passed to the
  114. ->mode_filter() function which should return a mask of valid modes
  115. after filtering those unsuitable due to hardware limits. It is not
  116. valid to use this interface to add modes.
  117. </para>
  118. <para>
  119. dev->pio_mode and dev->dma_mode are guaranteed to be valid when
  120. ->set_piomode() and when ->set_dmamode() is called. The timings for
  121. any other drive sharing the cable will also be valid at this point.
  122. That is the library records the decisions for the modes of each
  123. drive on a channel before it attempts to set any of them.
  124. </para>
  125. <para>
  126. ->post_set_mode() is
  127. called unconditionally, after the SET FEATURES - XFER MODE
  128. command completes successfully.
  129. </para>
  130. <para>
  131. ->set_piomode() is always called (if present), but
  132. ->set_dma_mode() is only called if DMA is possible.
  133. </para>
  134. </sect2>
  135. <sect2><title>Taskfile read/write</title>
  136. <programlisting>
  137. void (*tf_load) (struct ata_port *ap, struct ata_taskfile *tf);
  138. void (*tf_read) (struct ata_port *ap, struct ata_taskfile *tf);
  139. </programlisting>
  140. <para>
  141. ->tf_load() is called to load the given taskfile into hardware
  142. registers / DMA buffers. ->tf_read() is called to read the
  143. hardware registers / DMA buffers, to obtain the current set of
  144. taskfile register values.
  145. Most drivers for taskfile-based hardware (PIO or MMIO) use
  146. ata_tf_load() and ata_tf_read() for these hooks.
  147. </para>
  148. </sect2>
  149. <sect2><title>ATA command execute</title>
  150. <programlisting>
  151. void (*exec_command)(struct ata_port *ap, struct ata_taskfile *tf);
  152. </programlisting>
  153. <para>
  154. causes an ATA command, previously loaded with
  155. ->tf_load(), to be initiated in hardware.
  156. Most drivers for taskfile-based hardware use ata_exec_command()
  157. for this hook.
  158. </para>
  159. </sect2>
  160. <sect2><title>Per-cmd ATAPI DMA capabilities filter</title>
  161. <programlisting>
  162. int (*check_atapi_dma) (struct ata_queued_cmd *qc);
  163. </programlisting>
  164. <para>
  165. Allow low-level driver to filter ATA PACKET commands, returning a status
  166. indicating whether or not it is OK to use DMA for the supplied PACKET
  167. command.
  168. </para>
  169. <para>
  170. This hook may be specified as NULL, in which case libata will
  171. assume that atapi dma can be supported.
  172. </para>
  173. </sect2>
  174. <sect2><title>Read specific ATA shadow registers</title>
  175. <programlisting>
  176. u8 (*check_status)(struct ata_port *ap);
  177. u8 (*check_altstatus)(struct ata_port *ap);
  178. u8 (*check_err)(struct ata_port *ap);
  179. </programlisting>
  180. <para>
  181. Reads the Status/AltStatus/Error ATA shadow register from
  182. hardware. On some hardware, reading the Status register has
  183. the side effect of clearing the interrupt condition.
  184. Most drivers for taskfile-based hardware use
  185. ata_check_status() for this hook.
  186. </para>
  187. <para>
  188. Note that because this is called from ata_device_add(), at
  189. least a dummy function that clears device interrupts must be
  190. provided for all drivers, even if the controller doesn't
  191. actually have a taskfile status register.
  192. </para>
  193. </sect2>
  194. <sect2><title>Select ATA device on bus</title>
  195. <programlisting>
  196. void (*dev_select)(struct ata_port *ap, unsigned int device);
  197. </programlisting>
  198. <para>
  199. Issues the low-level hardware command(s) that causes one of N
  200. hardware devices to be considered 'selected' (active and
  201. available for use) on the ATA bus. This generally has no
  202. meaning on FIS-based devices.
  203. </para>
  204. <para>
  205. Most drivers for taskfile-based hardware use
  206. ata_std_dev_select() for this hook. Controllers which do not
  207. support second drives on a port (such as SATA contollers) will
  208. use ata_noop_dev_select().
  209. </para>
  210. </sect2>
  211. <sect2><title>Private tuning method</title>
  212. <programlisting>
  213. void (*set_mode) (struct ata_port *ap);
  214. </programlisting>
  215. <para>
  216. By default libata performs drive and controller tuning in
  217. accordance with the ATA timing rules and also applies blacklists
  218. and cable limits. Some controllers need special handling and have
  219. custom tuning rules, typically raid controllers that use ATA
  220. commands but do not actually do drive timing.
  221. </para>
  222. <warning>
  223. <para>
  224. This hook should not be used to replace the standard controller
  225. tuning logic when a controller has quirks. Replacing the default
  226. tuning logic in that case would bypass handling for drive and
  227. bridge quirks that may be important to data reliability. If a
  228. controller needs to filter the mode selection it should use the
  229. mode_filter hook instead.
  230. </para>
  231. </warning>
  232. </sect2>
  233. <sect2><title>Reset ATA bus</title>
  234. <programlisting>
  235. void (*phy_reset) (struct ata_port *ap);
  236. </programlisting>
  237. <para>
  238. The very first step in the probe phase. Actions vary depending
  239. on the bus type, typically. After waking up the device and probing
  240. for device presence (PATA and SATA), typically a soft reset
  241. (SRST) will be performed. Drivers typically use the helper
  242. functions ata_bus_reset() or sata_phy_reset() for this hook.
  243. Many SATA drivers use sata_phy_reset() or call it from within
  244. their own phy_reset() functions.
  245. </para>
  246. </sect2>
  247. <sect2><title>Control PCI IDE BMDMA engine</title>
  248. <programlisting>
  249. void (*bmdma_setup) (struct ata_queued_cmd *qc);
  250. void (*bmdma_start) (struct ata_queued_cmd *qc);
  251. void (*bmdma_stop) (struct ata_port *ap);
  252. u8 (*bmdma_status) (struct ata_port *ap);
  253. </programlisting>
  254. <para>
  255. When setting up an IDE BMDMA transaction, these hooks arm
  256. (->bmdma_setup), fire (->bmdma_start), and halt (->bmdma_stop)
  257. the hardware's DMA engine. ->bmdma_status is used to read the standard
  258. PCI IDE DMA Status register.
  259. </para>
  260. <para>
  261. These hooks are typically either no-ops, or simply not implemented, in
  262. FIS-based drivers.
  263. </para>
  264. <para>
  265. Most legacy IDE drivers use ata_bmdma_setup() for the bmdma_setup()
  266. hook. ata_bmdma_setup() will write the pointer to the PRD table to
  267. the IDE PRD Table Address register, enable DMA in the DMA Command
  268. register, and call exec_command() to begin the transfer.
  269. </para>
  270. <para>
  271. Most legacy IDE drivers use ata_bmdma_start() for the bmdma_start()
  272. hook. ata_bmdma_start() will write the ATA_DMA_START flag to the DMA
  273. Command register.
  274. </para>
  275. <para>
  276. Many legacy IDE drivers use ata_bmdma_stop() for the bmdma_stop()
  277. hook. ata_bmdma_stop() clears the ATA_DMA_START flag in the DMA
  278. command register.
  279. </para>
  280. <para>
  281. Many legacy IDE drivers use ata_bmdma_status() as the bmdma_status() hook.
  282. </para>
  283. </sect2>
  284. <sect2><title>High-level taskfile hooks</title>
  285. <programlisting>
  286. void (*qc_prep) (struct ata_queued_cmd *qc);
  287. int (*qc_issue) (struct ata_queued_cmd *qc);
  288. </programlisting>
  289. <para>
  290. Higher-level hooks, these two hooks can potentially supercede
  291. several of the above taskfile/DMA engine hooks. ->qc_prep is
  292. called after the buffers have been DMA-mapped, and is typically
  293. used to populate the hardware's DMA scatter-gather table.
  294. Most drivers use the standard ata_qc_prep() helper function, but
  295. more advanced drivers roll their own.
  296. </para>
  297. <para>
  298. ->qc_issue is used to make a command active, once the hardware
  299. and S/G tables have been prepared. IDE BMDMA drivers use the
  300. helper function ata_qc_issue_prot() for taskfile protocol-based
  301. dispatch. More advanced drivers implement their own ->qc_issue.
  302. </para>
  303. <para>
  304. ata_qc_issue_prot() calls ->tf_load(), ->bmdma_setup(), and
  305. ->bmdma_start() as necessary to initiate a transfer.
  306. </para>
  307. </sect2>
  308. <sect2><title>Timeout (error) handling</title>
  309. <programlisting>
  310. void (*eng_timeout) (struct ata_port *ap);
  311. </programlisting>
  312. <para>
  313. This is a high level error handling function, called from the
  314. error handling thread, when a command times out. Most newer
  315. hardware will implement its own error handling code here. IDE BMDMA
  316. drivers may use the helper function ata_eng_timeout().
  317. </para>
  318. </sect2>
  319. <sect2><title>Hardware interrupt handling</title>
  320. <programlisting>
  321. irqreturn_t (*irq_handler)(int, void *, struct pt_regs *);
  322. void (*irq_clear) (struct ata_port *);
  323. </programlisting>
  324. <para>
  325. ->irq_handler is the interrupt handling routine registered with
  326. the system, by libata. ->irq_clear is called during probe just
  327. before the interrupt handler is registered, to be sure hardware
  328. is quiet.
  329. </para>
  330. <para>
  331. The second argument, dev_instance, should be cast to a pointer
  332. to struct ata_host_set.
  333. </para>
  334. <para>
  335. Most legacy IDE drivers use ata_interrupt() for the
  336. irq_handler hook, which scans all ports in the host_set,
  337. determines which queued command was active (if any), and calls
  338. ata_host_intr(ap,qc).
  339. </para>
  340. <para>
  341. Most legacy IDE drivers use ata_bmdma_irq_clear() for the
  342. irq_clear() hook, which simply clears the interrupt and error
  343. flags in the DMA status register.
  344. </para>
  345. </sect2>
  346. <sect2><title>SATA phy read/write</title>
  347. <programlisting>
  348. u32 (*scr_read) (struct ata_port *ap, unsigned int sc_reg);
  349. void (*scr_write) (struct ata_port *ap, unsigned int sc_reg,
  350. u32 val);
  351. </programlisting>
  352. <para>
  353. Read and write standard SATA phy registers. Currently only used
  354. if ->phy_reset hook called the sata_phy_reset() helper function.
  355. sc_reg is one of SCR_STATUS, SCR_CONTROL, SCR_ERROR, or SCR_ACTIVE.
  356. </para>
  357. </sect2>
  358. <sect2><title>Init and shutdown</title>
  359. <programlisting>
  360. int (*port_start) (struct ata_port *ap);
  361. void (*port_stop) (struct ata_port *ap);
  362. void (*host_stop) (struct ata_host_set *host_set);
  363. </programlisting>
  364. <para>
  365. ->port_start() is called just after the data structures for each
  366. port are initialized. Typically this is used to alloc per-port
  367. DMA buffers / tables / rings, enable DMA engines, and similar
  368. tasks. Some drivers also use this entry point as a chance to
  369. allocate driver-private memory for ap->private_data.
  370. </para>
  371. <para>
  372. Many drivers use ata_port_start() as this hook or call
  373. it from their own port_start() hooks. ata_port_start()
  374. allocates space for a legacy IDE PRD table and returns.
  375. </para>
  376. <para>
  377. ->port_stop() is called after ->host_stop(). It's sole function
  378. is to release DMA/memory resources, now that they are no longer
  379. actively being used. Many drivers also free driver-private
  380. data from port at this time.
  381. </para>
  382. <para>
  383. Many drivers use ata_port_stop() as this hook, which frees the
  384. PRD table.
  385. </para>
  386. <para>
  387. ->host_stop() is called after all ->port_stop() calls
  388. have completed. The hook must finalize hardware shutdown, release DMA
  389. and other resources, etc.
  390. This hook may be specified as NULL, in which case it is not called.
  391. </para>
  392. </sect2>
  393. </sect1>
  394. </chapter>
  395. <chapter id="libataEH">
  396. <title>Error handling</title>
  397. <para>
  398. This chapter describes how errors are handled under libata.
  399. Readers are advised to read SCSI EH
  400. (Documentation/scsi/scsi_eh.txt) and ATA exceptions doc first.
  401. </para>
  402. <sect1><title>Origins of commands</title>
  403. <para>
  404. In libata, a command is represented with struct ata_queued_cmd
  405. or qc. qc's are preallocated during port initialization and
  406. repetitively used for command executions. Currently only one
  407. qc is allocated per port but yet-to-be-merged NCQ branch
  408. allocates one for each tag and maps each qc to NCQ tag 1-to-1.
  409. </para>
  410. <para>
  411. libata commands can originate from two sources - libata itself
  412. and SCSI midlayer. libata internal commands are used for
  413. initialization and error handling. All normal blk requests
  414. and commands for SCSI emulation are passed as SCSI commands
  415. through queuecommand callback of SCSI host template.
  416. </para>
  417. </sect1>
  418. <sect1><title>How commands are issued</title>
  419. <variablelist>
  420. <varlistentry><term>Internal commands</term>
  421. <listitem>
  422. <para>
  423. First, qc is allocated and initialized using
  424. ata_qc_new_init(). Although ata_qc_new_init() doesn't
  425. implement any wait or retry mechanism when qc is not
  426. available, internal commands are currently issued only during
  427. initialization and error recovery, so no other command is
  428. active and allocation is guaranteed to succeed.
  429. </para>
  430. <para>
  431. Once allocated qc's taskfile is initialized for the command to
  432. be executed. qc currently has two mechanisms to notify
  433. completion. One is via qc->complete_fn() callback and the
  434. other is completion qc->waiting. qc->complete_fn() callback
  435. is the asynchronous path used by normal SCSI translated
  436. commands and qc->waiting is the synchronous (issuer sleeps in
  437. process context) path used by internal commands.
  438. </para>
  439. <para>
  440. Once initialization is complete, host_set lock is acquired
  441. and the qc is issued.
  442. </para>
  443. </listitem>
  444. </varlistentry>
  445. <varlistentry><term>SCSI commands</term>
  446. <listitem>
  447. <para>
  448. All libata drivers use ata_scsi_queuecmd() as
  449. hostt->queuecommand callback. scmds can either be simulated
  450. or translated. No qc is involved in processing a simulated
  451. scmd. The result is computed right away and the scmd is
  452. completed.
  453. </para>
  454. <para>
  455. For a translated scmd, ata_qc_new_init() is invoked to
  456. allocate a qc and the scmd is translated into the qc. SCSI
  457. midlayer's completion notification function pointer is stored
  458. into qc->scsidone.
  459. </para>
  460. <para>
  461. qc->complete_fn() callback is used for completion
  462. notification. ATA commands use ata_scsi_qc_complete() while
  463. ATAPI commands use atapi_qc_complete(). Both functions end up
  464. calling qc->scsidone to notify upper layer when the qc is
  465. finished. After translation is completed, the qc is issued
  466. with ata_qc_issue().
  467. </para>
  468. <para>
  469. Note that SCSI midlayer invokes hostt->queuecommand while
  470. holding host_set lock, so all above occur while holding
  471. host_set lock.
  472. </para>
  473. </listitem>
  474. </varlistentry>
  475. </variablelist>
  476. </sect1>
  477. <sect1><title>How commands are processed</title>
  478. <para>
  479. Depending on which protocol and which controller are used,
  480. commands are processed differently. For the purpose of
  481. discussion, a controller which uses taskfile interface and all
  482. standard callbacks is assumed.
  483. </para>
  484. <para>
  485. Currently 6 ATA command protocols are used. They can be
  486. sorted into the following four categories according to how
  487. they are processed.
  488. </para>
  489. <variablelist>
  490. <varlistentry><term>ATA NO DATA or DMA</term>
  491. <listitem>
  492. <para>
  493. ATA_PROT_NODATA and ATA_PROT_DMA fall into this category.
  494. These types of commands don't require any software
  495. intervention once issued. Device will raise interrupt on
  496. completion.
  497. </para>
  498. </listitem>
  499. </varlistentry>
  500. <varlistentry><term>ATA PIO</term>
  501. <listitem>
  502. <para>
  503. ATA_PROT_PIO is in this category. libata currently
  504. implements PIO with polling. ATA_NIEN bit is set to turn
  505. off interrupt and pio_task on ata_wq performs polling and
  506. IO.
  507. </para>
  508. </listitem>
  509. </varlistentry>
  510. <varlistentry><term>ATAPI NODATA or DMA</term>
  511. <listitem>
  512. <para>
  513. ATA_PROT_ATAPI_NODATA and ATA_PROT_ATAPI_DMA are in this
  514. category. packet_task is used to poll BSY bit after
  515. issuing PACKET command. Once BSY is turned off by the
  516. device, packet_task transfers CDB and hands off processing
  517. to interrupt handler.
  518. </para>
  519. </listitem>
  520. </varlistentry>
  521. <varlistentry><term>ATAPI PIO</term>
  522. <listitem>
  523. <para>
  524. ATA_PROT_ATAPI is in this category. ATA_NIEN bit is set
  525. and, as in ATAPI NODATA or DMA, packet_task submits cdb.
  526. However, after submitting cdb, further processing (data
  527. transfer) is handed off to pio_task.
  528. </para>
  529. </listitem>
  530. </varlistentry>
  531. </variablelist>
  532. </sect1>
  533. <sect1><title>How commands are completed</title>
  534. <para>
  535. Once issued, all qc's are either completed with
  536. ata_qc_complete() or time out. For commands which are handled
  537. by interrupts, ata_host_intr() invokes ata_qc_complete(), and,
  538. for PIO tasks, pio_task invokes ata_qc_complete(). In error
  539. cases, packet_task may also complete commands.
  540. </para>
  541. <para>
  542. ata_qc_complete() does the following.
  543. </para>
  544. <orderedlist>
  545. <listitem>
  546. <para>
  547. DMA memory is unmapped.
  548. </para>
  549. </listitem>
  550. <listitem>
  551. <para>
  552. ATA_QCFLAG_ACTIVE is clared from qc->flags.
  553. </para>
  554. </listitem>
  555. <listitem>
  556. <para>
  557. qc->complete_fn() callback is invoked. If the return value of
  558. the callback is not zero. Completion is short circuited and
  559. ata_qc_complete() returns.
  560. </para>
  561. </listitem>
  562. <listitem>
  563. <para>
  564. __ata_qc_complete() is called, which does
  565. <orderedlist>
  566. <listitem>
  567. <para>
  568. qc->flags is cleared to zero.
  569. </para>
  570. </listitem>
  571. <listitem>
  572. <para>
  573. ap->active_tag and qc->tag are poisoned.
  574. </para>
  575. </listitem>
  576. <listitem>
  577. <para>
  578. qc->waiting is claread &amp; completed (in that order).
  579. </para>
  580. </listitem>
  581. <listitem>
  582. <para>
  583. qc is deallocated by clearing appropriate bit in ap->qactive.
  584. </para>
  585. </listitem>
  586. </orderedlist>
  587. </para>
  588. </listitem>
  589. </orderedlist>
  590. <para>
  591. So, it basically notifies upper layer and deallocates qc. One
  592. exception is short-circuit path in #3 which is used by
  593. atapi_qc_complete().
  594. </para>
  595. <para>
  596. For all non-ATAPI commands, whether it fails or not, almost
  597. the same code path is taken and very little error handling
  598. takes place. A qc is completed with success status if it
  599. succeeded, with failed status otherwise.
  600. </para>
  601. <para>
  602. However, failed ATAPI commands require more handling as
  603. REQUEST SENSE is needed to acquire sense data. If an ATAPI
  604. command fails, ata_qc_complete() is invoked with error status,
  605. which in turn invokes atapi_qc_complete() via
  606. qc->complete_fn() callback.
  607. </para>
  608. <para>
  609. This makes atapi_qc_complete() set scmd->result to
  610. SAM_STAT_CHECK_CONDITION, complete the scmd and return 1. As
  611. the sense data is empty but scmd->result is CHECK CONDITION,
  612. SCSI midlayer will invoke EH for the scmd, and returning 1
  613. makes ata_qc_complete() to return without deallocating the qc.
  614. This leads us to ata_scsi_error() with partially completed qc.
  615. </para>
  616. </sect1>
  617. <sect1><title>ata_scsi_error()</title>
  618. <para>
  619. ata_scsi_error() is the current transportt->eh_strategy_handler()
  620. for libata. As discussed above, this will be entered in two
  621. cases - timeout and ATAPI error completion. This function
  622. calls low level libata driver's eng_timeout() callback, the
  623. standard callback for which is ata_eng_timeout(). It checks
  624. if a qc is active and calls ata_qc_timeout() on the qc if so.
  625. Actual error handling occurs in ata_qc_timeout().
  626. </para>
  627. <para>
  628. If EH is invoked for timeout, ata_qc_timeout() stops BMDMA and
  629. completes the qc. Note that as we're currently in EH, we
  630. cannot call scsi_done. As described in SCSI EH doc, a
  631. recovered scmd should be either retried with
  632. scsi_queue_insert() or finished with scsi_finish_command().
  633. Here, we override qc->scsidone with scsi_finish_command() and
  634. calls ata_qc_complete().
  635. </para>
  636. <para>
  637. If EH is invoked due to a failed ATAPI qc, the qc here is
  638. completed but not deallocated. The purpose of this
  639. half-completion is to use the qc as place holder to make EH
  640. code reach this place. This is a bit hackish, but it works.
  641. </para>
  642. <para>
  643. Once control reaches here, the qc is deallocated by invoking
  644. __ata_qc_complete() explicitly. Then, internal qc for REQUEST
  645. SENSE is issued. Once sense data is acquired, scmd is
  646. finished by directly invoking scsi_finish_command() on the
  647. scmd. Note that as we already have completed and deallocated
  648. the qc which was associated with the scmd, we don't need
  649. to/cannot call ata_qc_complete() again.
  650. </para>
  651. </sect1>
  652. <sect1><title>Problems with the current EH</title>
  653. <itemizedlist>
  654. <listitem>
  655. <para>
  656. Error representation is too crude. Currently any and all
  657. error conditions are represented with ATA STATUS and ERROR
  658. registers. Errors which aren't ATA device errors are treated
  659. as ATA device errors by setting ATA_ERR bit. Better error
  660. descriptor which can properly represent ATA and other
  661. errors/exceptions is needed.
  662. </para>
  663. </listitem>
  664. <listitem>
  665. <para>
  666. When handling timeouts, no action is taken to make device
  667. forget about the timed out command and ready for new commands.
  668. </para>
  669. </listitem>
  670. <listitem>
  671. <para>
  672. EH handling via ata_scsi_error() is not properly protected
  673. from usual command processing. On EH entrance, the device is
  674. not in quiescent state. Timed out commands may succeed or
  675. fail any time. pio_task and atapi_task may still be running.
  676. </para>
  677. </listitem>
  678. <listitem>
  679. <para>
  680. Too weak error recovery. Devices / controllers causing HSM
  681. mismatch errors and other errors quite often require reset to
  682. return to known state. Also, advanced error handling is
  683. necessary to support features like NCQ and hotplug.
  684. </para>
  685. </listitem>
  686. <listitem>
  687. <para>
  688. ATA errors are directly handled in the interrupt handler and
  689. PIO errors in pio_task. This is problematic for advanced
  690. error handling for the following reasons.
  691. </para>
  692. <para>
  693. First, advanced error handling often requires context and
  694. internal qc execution.
  695. </para>
  696. <para>
  697. Second, even a simple failure (say, CRC error) needs
  698. information gathering and could trigger complex error handling
  699. (say, resetting &amp; reconfiguring). Having multiple code
  700. paths to gather information, enter EH and trigger actions
  701. makes life painful.
  702. </para>
  703. <para>
  704. Third, scattered EH code makes implementing low level drivers
  705. difficult. Low level drivers override libata callbacks. If
  706. EH is scattered over several places, each affected callbacks
  707. should perform its part of error handling. This can be error
  708. prone and painful.
  709. </para>
  710. </listitem>
  711. </itemizedlist>
  712. </sect1>
  713. </chapter>
  714. <chapter id="libataExt">
  715. <title>libata Library</title>
  716. !Edrivers/scsi/libata-core.c
  717. </chapter>
  718. <chapter id="libataInt">
  719. <title>libata Core Internals</title>
  720. !Idrivers/scsi/libata-core.c
  721. </chapter>
  722. <chapter id="libataScsiInt">
  723. <title>libata SCSI translation/emulation</title>
  724. !Edrivers/scsi/libata-scsi.c
  725. !Idrivers/scsi/libata-scsi.c
  726. </chapter>
  727. <chapter id="ataExceptions">
  728. <title>ATA errors &amp; exceptions</title>
  729. <para>
  730. This chapter tries to identify what error/exception conditions exist
  731. for ATA/ATAPI devices and describe how they should be handled in
  732. implementation-neutral way.
  733. </para>
  734. <para>
  735. The term 'error' is used to describe conditions where either an
  736. explicit error condition is reported from device or a command has
  737. timed out.
  738. </para>
  739. <para>
  740. The term 'exception' is either used to describe exceptional
  741. conditions which are not errors (say, power or hotplug events), or
  742. to describe both errors and non-error exceptional conditions. Where
  743. explicit distinction between error and exception is necessary, the
  744. term 'non-error exception' is used.
  745. </para>
  746. <sect1 id="excat">
  747. <title>Exception categories</title>
  748. <para>
  749. Exceptions are described primarily with respect to legacy
  750. taskfile + bus master IDE interface. If a controller provides
  751. other better mechanism for error reporting, mapping those into
  752. categories described below shouldn't be difficult.
  753. </para>
  754. <para>
  755. In the following sections, two recovery actions - reset and
  756. reconfiguring transport - are mentioned. These are described
  757. further in <xref linkend="exrec"/>.
  758. </para>
  759. <sect2 id="excatHSMviolation">
  760. <title>HSM violation</title>
  761. <para>
  762. This error is indicated when STATUS value doesn't match HSM
  763. requirement during issuing or excution any ATA/ATAPI command.
  764. </para>
  765. <itemizedlist>
  766. <title>Examples</title>
  767. <listitem>
  768. <para>
  769. ATA_STATUS doesn't contain !BSY &amp;&amp; DRDY &amp;&amp; !DRQ while trying
  770. to issue a command.
  771. </para>
  772. </listitem>
  773. <listitem>
  774. <para>
  775. !BSY &amp;&amp; !DRQ during PIO data transfer.
  776. </para>
  777. </listitem>
  778. <listitem>
  779. <para>
  780. DRQ on command completion.
  781. </para>
  782. </listitem>
  783. <listitem>
  784. <para>
  785. !BSY &amp;&amp; ERR after CDB tranfer starts but before the
  786. last byte of CDB is transferred. ATA/ATAPI standard states
  787. that &quot;The device shall not terminate the PACKET command
  788. with an error before the last byte of the command packet has
  789. been written&quot; in the error outputs description of PACKET
  790. command and the state diagram doesn't include such
  791. transitions.
  792. </para>
  793. </listitem>
  794. </itemizedlist>
  795. <para>
  796. In these cases, HSM is violated and not much information
  797. regarding the error can be acquired from STATUS or ERROR
  798. register. IOW, this error can be anything - driver bug,
  799. faulty device, controller and/or cable.
  800. </para>
  801. <para>
  802. As HSM is violated, reset is necessary to restore known state.
  803. Reconfiguring transport for lower speed might be helpful too
  804. as transmission errors sometimes cause this kind of errors.
  805. </para>
  806. </sect2>
  807. <sect2 id="excatDevErr">
  808. <title>ATA/ATAPI device error (non-NCQ / non-CHECK CONDITION)</title>
  809. <para>
  810. These are errors detected and reported by ATA/ATAPI devices
  811. indicating device problems. For this type of errors, STATUS
  812. and ERROR register values are valid and describe error
  813. condition. Note that some of ATA bus errors are detected by
  814. ATA/ATAPI devices and reported using the same mechanism as
  815. device errors. Those cases are described later in this
  816. section.
  817. </para>
  818. <para>
  819. For ATA commands, this type of errors are indicated by !BSY
  820. &amp;&amp; ERR during command execution and on completion.
  821. </para>
  822. <para>For ATAPI commands,</para>
  823. <itemizedlist>
  824. <listitem>
  825. <para>
  826. !BSY &amp;&amp; ERR &amp;&amp; ABRT right after issuing PACKET
  827. indicates that PACKET command is not supported and falls in
  828. this category.
  829. </para>
  830. </listitem>
  831. <listitem>
  832. <para>
  833. !BSY &amp;&amp; ERR(==CHK) &amp;&amp; !ABRT after the last
  834. byte of CDB is transferred indicates CHECK CONDITION and
  835. doesn't fall in this category.
  836. </para>
  837. </listitem>
  838. <listitem>
  839. <para>
  840. !BSY &amp;&amp; ERR(==CHK) &amp;&amp; ABRT after the last byte
  841. of CDB is transferred *probably* indicates CHECK CONDITION and
  842. doesn't fall in this category.
  843. </para>
  844. </listitem>
  845. </itemizedlist>
  846. <para>
  847. Of errors detected as above, the followings are not ATA/ATAPI
  848. device errors but ATA bus errors and should be handled
  849. according to <xref linkend="excatATAbusErr"/>.
  850. </para>
  851. <variablelist>
  852. <varlistentry>
  853. <term>CRC error during data transfer</term>
  854. <listitem>
  855. <para>
  856. This is indicated by ICRC bit in the ERROR register and
  857. means that corruption occurred during data transfer. Upto
  858. ATA/ATAPI-7, the standard specifies that this bit is only
  859. applicable to UDMA transfers but ATA/ATAPI-8 draft revision
  860. 1f says that the bit may be applicable to multiword DMA and
  861. PIO.
  862. </para>
  863. </listitem>
  864. </varlistentry>
  865. <varlistentry>
  866. <term>ABRT error during data transfer or on completion</term>
  867. <listitem>
  868. <para>
  869. Upto ATA/ATAPI-7, the standard specifies that ABRT could be
  870. set on ICRC errors and on cases where a device is not able
  871. to complete a command. Combined with the fact that MWDMA
  872. and PIO transfer errors aren't allowed to use ICRC bit upto
  873. ATA/ATAPI-7, it seems to imply that ABRT bit alone could
  874. indicate tranfer errors.
  875. </para>
  876. <para>
  877. However, ATA/ATAPI-8 draft revision 1f removes the part
  878. that ICRC errors can turn on ABRT. So, this is kind of
  879. gray area. Some heuristics are needed here.
  880. </para>
  881. </listitem>
  882. </varlistentry>
  883. </variablelist>
  884. <para>
  885. ATA/ATAPI device errors can be further categorized as follows.
  886. </para>
  887. <variablelist>
  888. <varlistentry>
  889. <term>Media errors</term>
  890. <listitem>
  891. <para>
  892. This is indicated by UNC bit in the ERROR register. ATA
  893. devices reports UNC error only after certain number of
  894. retries cannot recover the data, so there's nothing much
  895. else to do other than notifying upper layer.
  896. </para>
  897. <para>
  898. READ and WRITE commands report CHS or LBA of the first
  899. failed sector but ATA/ATAPI standard specifies that the
  900. amount of transferred data on error completion is
  901. indeterminate, so we cannot assume that sectors preceding
  902. the failed sector have been transferred and thus cannot
  903. complete those sectors successfully as SCSI does.
  904. </para>
  905. </listitem>
  906. </varlistentry>
  907. <varlistentry>
  908. <term>Media changed / media change requested error</term>
  909. <listitem>
  910. <para>
  911. &lt;&lt;TODO: fill here&gt;&gt;
  912. </para>
  913. </listitem>
  914. </varlistentry>
  915. <varlistentry><term>Address error</term>
  916. <listitem>
  917. <para>
  918. This is indicated by IDNF bit in the ERROR register.
  919. Report to upper layer.
  920. </para>
  921. </listitem>
  922. </varlistentry>
  923. <varlistentry><term>Other errors</term>
  924. <listitem>
  925. <para>
  926. This can be invalid command or parameter indicated by ABRT
  927. ERROR bit or some other error condition. Note that ABRT
  928. bit can indicate a lot of things including ICRC and Address
  929. errors. Heuristics needed.
  930. </para>
  931. </listitem>
  932. </varlistentry>
  933. </variablelist>
  934. <para>
  935. Depending on commands, not all STATUS/ERROR bits are
  936. applicable. These non-applicable bits are marked with
  937. &quot;na&quot; in the output descriptions but upto ATA/ATAPI-7
  938. no definition of &quot;na&quot; can be found. However,
  939. ATA/ATAPI-8 draft revision 1f describes &quot;N/A&quot; as
  940. follows.
  941. </para>
  942. <blockquote>
  943. <variablelist>
  944. <varlistentry><term>3.2.3.3a N/A</term>
  945. <listitem>
  946. <para>
  947. A keyword the indicates a field has no defined value in
  948. this standard and should not be checked by the host or
  949. device. N/A fields should be cleared to zero.
  950. </para>
  951. </listitem>
  952. </varlistentry>
  953. </variablelist>
  954. </blockquote>
  955. <para>
  956. So, it seems reasonable to assume that &quot;na&quot; bits are
  957. cleared to zero by devices and thus need no explicit masking.
  958. </para>
  959. </sect2>
  960. <sect2 id="excatATAPIcc">
  961. <title>ATAPI device CHECK CONDITION</title>
  962. <para>
  963. ATAPI device CHECK CONDITION error is indicated by set CHK bit
  964. (ERR bit) in the STATUS register after the last byte of CDB is
  965. transferred for a PACKET command. For this kind of errors,
  966. sense data should be acquired to gather information regarding
  967. the errors. REQUEST SENSE packet command should be used to
  968. acquire sense data.
  969. </para>
  970. <para>
  971. Once sense data is acquired, this type of errors can be
  972. handled similary to other SCSI errors. Note that sense data
  973. may indicate ATA bus error (e.g. Sense Key 04h HARDWARE ERROR
  974. &amp;&amp; ASC/ASCQ 47h/00h SCSI PARITY ERROR). In such
  975. cases, the error should be considered as an ATA bus error and
  976. handled according to <xref linkend="excatATAbusErr"/>.
  977. </para>
  978. </sect2>
  979. <sect2 id="excatNCQerr">
  980. <title>ATA device error (NCQ)</title>
  981. <para>
  982. NCQ command error is indicated by cleared BSY and set ERR bit
  983. during NCQ command phase (one or more NCQ commands
  984. outstanding). Although STATUS and ERROR registers will
  985. contain valid values describing the error, READ LOG EXT is
  986. required to clear the error condition, determine which command
  987. has failed and acquire more information.
  988. </para>
  989. <para>
  990. READ LOG EXT Log Page 10h reports which tag has failed and
  991. taskfile register values describing the error. With this
  992. information the failed command can be handled as a normal ATA
  993. command error as in <xref linkend="excatDevErr"/> and all
  994. other in-flight commands must be retried. Note that this
  995. retry should not be counted - it's likely that commands
  996. retried this way would have completed normally if it were not
  997. for the failed command.
  998. </para>
  999. <para>
  1000. Note that ATA bus errors can be reported as ATA device NCQ
  1001. errors. This should be handled as described in <xref
  1002. linkend="excatATAbusErr"/>.
  1003. </para>
  1004. <para>
  1005. If READ LOG EXT Log Page 10h fails or reports NQ, we're
  1006. thoroughly screwed. This condition should be treated
  1007. according to <xref linkend="excatHSMviolation"/>.
  1008. </para>
  1009. </sect2>
  1010. <sect2 id="excatATAbusErr">
  1011. <title>ATA bus error</title>
  1012. <para>
  1013. ATA bus error means that data corruption occurred during
  1014. transmission over ATA bus (SATA or PATA). This type of errors
  1015. can be indicated by
  1016. </para>
  1017. <itemizedlist>
  1018. <listitem>
  1019. <para>
  1020. ICRC or ABRT error as described in <xref linkend="excatDevErr"/>.
  1021. </para>
  1022. </listitem>
  1023. <listitem>
  1024. <para>
  1025. Controller-specific error completion with error information
  1026. indicating transmission error.
  1027. </para>
  1028. </listitem>
  1029. <listitem>
  1030. <para>
  1031. On some controllers, command timeout. In this case, there may
  1032. be a mechanism to determine that the timeout is due to
  1033. transmission error.
  1034. </para>
  1035. </listitem>
  1036. <listitem>
  1037. <para>
  1038. Unknown/random errors, timeouts and all sorts of weirdities.
  1039. </para>
  1040. </listitem>
  1041. </itemizedlist>
  1042. <para>
  1043. As described above, transmission errors can cause wide variety
  1044. of symptoms ranging from device ICRC error to random device
  1045. lockup, and, for many cases, there is no way to tell if an
  1046. error condition is due to transmission error or not;
  1047. therefore, it's necessary to employ some kind of heuristic
  1048. when dealing with errors and timeouts. For example,
  1049. encountering repetitive ABRT errors for known supported
  1050. command is likely to indicate ATA bus error.
  1051. </para>
  1052. <para>
  1053. Once it's determined that ATA bus errors have possibly
  1054. occurred, lowering ATA bus transmission speed is one of
  1055. actions which may alleviate the problem. See <xref
  1056. linkend="exrecReconf"/> for more information.
  1057. </para>
  1058. </sect2>
  1059. <sect2 id="excatPCIbusErr">
  1060. <title>PCI bus error</title>
  1061. <para>
  1062. Data corruption or other failures during transmission over PCI
  1063. (or other system bus). For standard BMDMA, this is indicated
  1064. by Error bit in the BMDMA Status register. This type of
  1065. errors must be logged as it indicates something is very wrong
  1066. with the system. Resetting host controller is recommended.
  1067. </para>
  1068. </sect2>
  1069. <sect2 id="excatLateCompletion">
  1070. <title>Late completion</title>
  1071. <para>
  1072. This occurs when timeout occurs and the timeout handler finds
  1073. out that the timed out command has completed successfully or
  1074. with error. This is usually caused by lost interrupts. This
  1075. type of errors must be logged. Resetting host controller is
  1076. recommended.
  1077. </para>
  1078. </sect2>
  1079. <sect2 id="excatUnknown">
  1080. <title>Unknown error (timeout)</title>
  1081. <para>
  1082. This is when timeout occurs and the command is still
  1083. processing or the host and device are in unknown state. When
  1084. this occurs, HSM could be in any valid or invalid state. To
  1085. bring the device to known state and make it forget about the
  1086. timed out command, resetting is necessary. The timed out
  1087. command may be retried.
  1088. </para>
  1089. <para>
  1090. Timeouts can also be caused by transmission errors. Refer to
  1091. <xref linkend="excatATAbusErr"/> for more details.
  1092. </para>
  1093. </sect2>
  1094. <sect2 id="excatHoplugPM">
  1095. <title>Hotplug and power management exceptions</title>
  1096. <para>
  1097. &lt;&lt;TODO: fill here&gt;&gt;
  1098. </para>
  1099. </sect2>
  1100. </sect1>
  1101. <sect1 id="exrec">
  1102. <title>EH recovery actions</title>
  1103. <para>
  1104. This section discusses several important recovery actions.
  1105. </para>
  1106. <sect2 id="exrecClr">
  1107. <title>Clearing error condition</title>
  1108. <para>
  1109. Many controllers require its error registers to be cleared by
  1110. error handler. Different controllers may have different
  1111. requirements.
  1112. </para>
  1113. <para>
  1114. For SATA, it's strongly recommended to clear at least SError
  1115. register during error handling.
  1116. </para>
  1117. </sect2>
  1118. <sect2 id="exrecRst">
  1119. <title>Reset</title>
  1120. <para>
  1121. During EH, resetting is necessary in the following cases.
  1122. </para>
  1123. <itemizedlist>
  1124. <listitem>
  1125. <para>
  1126. HSM is in unknown or invalid state
  1127. </para>
  1128. </listitem>
  1129. <listitem>
  1130. <para>
  1131. HBA is in unknown or invalid state
  1132. </para>
  1133. </listitem>
  1134. <listitem>
  1135. <para>
  1136. EH needs to make HBA/device forget about in-flight commands
  1137. </para>
  1138. </listitem>
  1139. <listitem>
  1140. <para>
  1141. HBA/device behaves weirdly
  1142. </para>
  1143. </listitem>
  1144. </itemizedlist>
  1145. <para>
  1146. Resetting during EH might be a good idea regardless of error
  1147. condition to improve EH robustness. Whether to reset both or
  1148. either one of HBA and device depends on situation but the
  1149. following scheme is recommended.
  1150. </para>
  1151. <itemizedlist>
  1152. <listitem>
  1153. <para>
  1154. When it's known that HBA is in ready state but ATA/ATAPI
  1155. device in in unknown state, reset only device.
  1156. </para>
  1157. </listitem>
  1158. <listitem>
  1159. <para>
  1160. If HBA is in unknown state, reset both HBA and device.
  1161. </para>
  1162. </listitem>
  1163. </itemizedlist>
  1164. <para>
  1165. HBA resetting is implementation specific. For a controller
  1166. complying to taskfile/BMDMA PCI IDE, stopping active DMA
  1167. transaction may be sufficient iff BMDMA state is the only HBA
  1168. context. But even mostly taskfile/BMDMA PCI IDE complying
  1169. controllers may have implementation specific requirements and
  1170. mechanism to reset themselves. This must be addressed by
  1171. specific drivers.
  1172. </para>
  1173. <para>
  1174. OTOH, ATA/ATAPI standard describes in detail ways to reset
  1175. ATA/ATAPI devices.
  1176. </para>
  1177. <variablelist>
  1178. <varlistentry><term>PATA hardware reset</term>
  1179. <listitem>
  1180. <para>
  1181. This is hardware initiated device reset signalled with
  1182. asserted PATA RESET- signal. There is no standard way to
  1183. initiate hardware reset from software although some
  1184. hardware provides registers that allow driver to directly
  1185. tweak the RESET- signal.
  1186. </para>
  1187. </listitem>
  1188. </varlistentry>
  1189. <varlistentry><term>Software reset</term>
  1190. <listitem>
  1191. <para>
  1192. This is achieved by turning CONTROL SRST bit on for at
  1193. least 5us. Both PATA and SATA support it but, in case of
  1194. SATA, this may require controller-specific support as the
  1195. second Register FIS to clear SRST should be transmitted
  1196. while BSY bit is still set. Note that on PATA, this resets
  1197. both master and slave devices on a channel.
  1198. </para>
  1199. </listitem>
  1200. </varlistentry>
  1201. <varlistentry><term>EXECUTE DEVICE DIAGNOSTIC command</term>
  1202. <listitem>
  1203. <para>
  1204. Although ATA/ATAPI standard doesn't describe exactly, EDD
  1205. implies some level of resetting, possibly similar level
  1206. with software reset. Host-side EDD protocol can be handled
  1207. with normal command processing and most SATA controllers
  1208. should be able to handle EDD's just like other commands.
  1209. As in software reset, EDD affects both devices on a PATA
  1210. bus.
  1211. </para>
  1212. <para>
  1213. Although EDD does reset devices, this doesn't suit error
  1214. handling as EDD cannot be issued while BSY is set and it's
  1215. unclear how it will act when device is in unknown/weird
  1216. state.
  1217. </para>
  1218. </listitem>
  1219. </varlistentry>
  1220. <varlistentry><term>ATAPI DEVICE RESET command</term>
  1221. <listitem>
  1222. <para>
  1223. This is very similar to software reset except that reset
  1224. can be restricted to the selected device without affecting
  1225. the other device sharing the cable.
  1226. </para>
  1227. </listitem>
  1228. </varlistentry>
  1229. <varlistentry><term>SATA phy reset</term>
  1230. <listitem>
  1231. <para>
  1232. This is the preferred way of resetting a SATA device. In
  1233. effect, it's identical to PATA hardware reset. Note that
  1234. this can be done with the standard SCR Control register.
  1235. As such, it's usually easier to implement than software
  1236. reset.
  1237. </para>
  1238. </listitem>
  1239. </varlistentry>
  1240. </variablelist>
  1241. <para>
  1242. One more thing to consider when resetting devices is that
  1243. resetting clears certain configuration parameters and they
  1244. need to be set to their previous or newly adjusted values
  1245. after reset.
  1246. </para>
  1247. <para>
  1248. Parameters affected are.
  1249. </para>
  1250. <itemizedlist>
  1251. <listitem>
  1252. <para>
  1253. CHS set up with INITIALIZE DEVICE PARAMETERS (seldomly used)
  1254. </para>
  1255. </listitem>
  1256. <listitem>
  1257. <para>
  1258. Parameters set with SET FEATURES including transfer mode setting
  1259. </para>
  1260. </listitem>
  1261. <listitem>
  1262. <para>
  1263. Block count set with SET MULTIPLE MODE
  1264. </para>
  1265. </listitem>
  1266. <listitem>
  1267. <para>
  1268. Other parameters (SET MAX, MEDIA LOCK...)
  1269. </para>
  1270. </listitem>
  1271. </itemizedlist>
  1272. <para>
  1273. ATA/ATAPI standard specifies that some parameters must be
  1274. maintained across hardware or software reset, but doesn't
  1275. strictly specify all of them. Always reconfiguring needed
  1276. parameters after reset is required for robustness. Note that
  1277. this also applies when resuming from deep sleep (power-off).
  1278. </para>
  1279. <para>
  1280. Also, ATA/ATAPI standard requires that IDENTIFY DEVICE /
  1281. IDENTIFY PACKET DEVICE is issued after any configuration
  1282. parameter is updated or a hardware reset and the result used
  1283. for further operation. OS driver is required to implement
  1284. revalidation mechanism to support this.
  1285. </para>
  1286. </sect2>
  1287. <sect2 id="exrecReconf">
  1288. <title>Reconfigure transport</title>
  1289. <para>
  1290. For both PATA and SATA, a lot of corners are cut for cheap
  1291. connectors, cables or controllers and it's quite common to see
  1292. high transmission error rate. This can be mitigated by
  1293. lowering transmission speed.
  1294. </para>
  1295. <para>
  1296. The following is a possible scheme Jeff Garzik suggested.
  1297. </para>
  1298. <blockquote>
  1299. <para>
  1300. If more than $N (3?) transmission errors happen in 15 minutes,
  1301. </para>
  1302. <itemizedlist>
  1303. <listitem>
  1304. <para>
  1305. if SATA, decrease SATA PHY speed. if speed cannot be decreased,
  1306. </para>
  1307. </listitem>
  1308. <listitem>
  1309. <para>
  1310. decrease UDMA xfer speed. if at UDMA0, switch to PIO4,
  1311. </para>
  1312. </listitem>
  1313. <listitem>
  1314. <para>
  1315. decrease PIO xfer speed. if at PIO3, complain, but continue
  1316. </para>
  1317. </listitem>
  1318. </itemizedlist>
  1319. </blockquote>
  1320. </sect2>
  1321. </sect1>
  1322. </chapter>
  1323. <chapter id="PiixInt">
  1324. <title>ata_piix Internals</title>
  1325. !Idrivers/scsi/ata_piix.c
  1326. </chapter>
  1327. <chapter id="SILInt">
  1328. <title>sata_sil Internals</title>
  1329. !Idrivers/scsi/sata_sil.c
  1330. </chapter>
  1331. <chapter id="libataThanks">
  1332. <title>Thanks</title>
  1333. <para>
  1334. The bulk of the ATA knowledge comes thanks to long conversations with
  1335. Andre Hedrick (www.linux-ide.org), and long hours pondering the ATA
  1336. and SCSI specifications.
  1337. </para>
  1338. <para>
  1339. Thanks to Alan Cox for pointing out similarities
  1340. between SATA and SCSI, and in general for motivation to hack on
  1341. libata.
  1342. </para>
  1343. <para>
  1344. libata's device detection
  1345. method, ata_pio_devchk, and in general all the early probing was
  1346. based on extensive study of Hale Landis's probe/reset code in his
  1347. ATADRVR driver (www.ata-atapi.com).
  1348. </para>
  1349. </chapter>
  1350. </book>