libata.rst 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993
  1. ========================
  2. libATA Developer's Guide
  3. ========================
  4. :Author: Jeff Garzik
  5. Introduction
  6. ============
  7. libATA is a library used inside the Linux kernel to support ATA host
  8. controllers and devices. libATA provides an ATA driver API, class
  9. transports for ATA and ATAPI devices, and SCSI<->ATA translation for ATA
  10. devices according to the T10 SAT specification.
  11. This Guide documents the libATA driver API, library functions, library
  12. internals, and a couple sample ATA low-level drivers.
  13. libata Driver API
  14. =================
  15. :c:type:`struct ata_port_operations <ata_port_operations>`
  16. is defined for every low-level libata
  17. hardware driver, and it controls how the low-level driver interfaces
  18. with the ATA and SCSI layers.
  19. FIS-based drivers will hook into the system with ``->qc_prep()`` and
  20. ``->qc_issue()`` high-level hooks. Hardware which behaves in a manner
  21. similar to PCI IDE hardware may utilize several generic helpers,
  22. defining at a bare minimum the bus I/O addresses of the ATA shadow
  23. register blocks.
  24. :c:type:`struct ata_port_operations <ata_port_operations>`
  25. ----------------------------------------------------------
  26. Post-IDENTIFY device configuration
  27. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  28. ::
  29. void (*dev_config) (struct ata_port *, struct ata_device *);
  30. Called after IDENTIFY [PACKET] DEVICE is issued to each device found.
  31. Typically used to apply device-specific fixups prior to issue of SET
  32. FEATURES - XFER MODE, and prior to operation.
  33. This entry may be specified as NULL in ata_port_operations.
  34. Set PIO/DMA mode
  35. ~~~~~~~~~~~~~~~~
  36. ::
  37. void (*set_piomode) (struct ata_port *, struct ata_device *);
  38. void (*set_dmamode) (struct ata_port *, struct ata_device *);
  39. void (*post_set_mode) (struct ata_port *);
  40. unsigned int (*mode_filter) (struct ata_port *, struct ata_device *, unsigned int);
  41. Hooks called prior to the issue of SET FEATURES - XFER MODE command. The
  42. optional ``->mode_filter()`` hook is called when libata has built a mask of
  43. the possible modes. This is passed to the ``->mode_filter()`` function
  44. which should return a mask of valid modes after filtering those
  45. unsuitable due to hardware limits. It is not valid to use this interface
  46. to add modes.
  47. ``dev->pio_mode`` and ``dev->dma_mode`` are guaranteed to be valid when
  48. ``->set_piomode()`` and when ``->set_dmamode()`` is called. The timings for
  49. any other drive sharing the cable will also be valid at this point. That
  50. is the library records the decisions for the modes of each drive on a
  51. channel before it attempts to set any of them.
  52. ``->post_set_mode()`` is called unconditionally, after the SET FEATURES -
  53. XFER MODE command completes successfully.
  54. ``->set_piomode()`` is always called (if present), but ``->set_dma_mode()``
  55. is only called if DMA is possible.
  56. Taskfile read/write
  57. ~~~~~~~~~~~~~~~~~~~
  58. ::
  59. void (*sff_tf_load) (struct ata_port *ap, struct ata_taskfile *tf);
  60. void (*sff_tf_read) (struct ata_port *ap, struct ata_taskfile *tf);
  61. ``->tf_load()`` is called to load the given taskfile into hardware
  62. registers / DMA buffers. ``->tf_read()`` is called to read the hardware
  63. registers / DMA buffers, to obtain the current set of taskfile register
  64. values. Most drivers for taskfile-based hardware (PIO or MMIO) use
  65. :c:func:`ata_sff_tf_load` and :c:func:`ata_sff_tf_read` for these hooks.
  66. PIO data read/write
  67. ~~~~~~~~~~~~~~~~~~~
  68. ::
  69. void (*sff_data_xfer) (struct ata_device *, unsigned char *, unsigned int, int);
  70. All bmdma-style drivers must implement this hook. This is the low-level
  71. operation that actually copies the data bytes during a PIO data
  72. transfer. Typically the driver will choose one of
  73. :c:func:`ata_sff_data_xfer`, or :c:func:`ata_sff_data_xfer32`.
  74. ATA command execute
  75. ~~~~~~~~~~~~~~~~~~~
  76. ::
  77. void (*sff_exec_command)(struct ata_port *ap, struct ata_taskfile *tf);
  78. causes an ATA command, previously loaded with ``->tf_load()``, to be
  79. initiated in hardware. Most drivers for taskfile-based hardware use
  80. :c:func:`ata_sff_exec_command` for this hook.
  81. Per-cmd ATAPI DMA capabilities filter
  82. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  83. ::
  84. int (*check_atapi_dma) (struct ata_queued_cmd *qc);
  85. Allow low-level driver to filter ATA PACKET commands, returning a status
  86. indicating whether or not it is OK to use DMA for the supplied PACKET
  87. command.
  88. This hook may be specified as NULL, in which case libata will assume
  89. that atapi dma can be supported.
  90. Read specific ATA shadow registers
  91. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  92. ::
  93. u8 (*sff_check_status)(struct ata_port *ap);
  94. u8 (*sff_check_altstatus)(struct ata_port *ap);
  95. Reads the Status/AltStatus ATA shadow register from hardware. On some
  96. hardware, reading the Status register has the side effect of clearing
  97. the interrupt condition. Most drivers for taskfile-based hardware use
  98. :c:func:`ata_sff_check_status` for this hook.
  99. Write specific ATA shadow register
  100. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  101. ::
  102. void (*sff_set_devctl)(struct ata_port *ap, u8 ctl);
  103. Write the device control ATA shadow register to the hardware. Most
  104. drivers don't need to define this.
  105. Select ATA device on bus
  106. ~~~~~~~~~~~~~~~~~~~~~~~~
  107. ::
  108. void (*sff_dev_select)(struct ata_port *ap, unsigned int device);
  109. Issues the low-level hardware command(s) that causes one of N hardware
  110. devices to be considered 'selected' (active and available for use) on
  111. the ATA bus. This generally has no meaning on FIS-based devices.
  112. Most drivers for taskfile-based hardware use :c:func:`ata_sff_dev_select` for
  113. this hook.
  114. Private tuning method
  115. ~~~~~~~~~~~~~~~~~~~~~
  116. ::
  117. void (*set_mode) (struct ata_port *ap);
  118. By default libata performs drive and controller tuning in accordance
  119. with the ATA timing rules and also applies blacklists and cable limits.
  120. Some controllers need special handling and have custom tuning rules,
  121. typically raid controllers that use ATA commands but do not actually do
  122. drive timing.
  123. **Warning**
  124. This hook should not be used to replace the standard controller
  125. tuning logic when a controller has quirks. Replacing the default
  126. tuning logic in that case would bypass handling for drive and bridge
  127. quirks that may be important to data reliability. If a controller
  128. needs to filter the mode selection it should use the mode_filter
  129. hook instead.
  130. Control PCI IDE BMDMA engine
  131. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  132. ::
  133. void (*bmdma_setup) (struct ata_queued_cmd *qc);
  134. void (*bmdma_start) (struct ata_queued_cmd *qc);
  135. void (*bmdma_stop) (struct ata_port *ap);
  136. u8 (*bmdma_status) (struct ata_port *ap);
  137. When setting up an IDE BMDMA transaction, these hooks arm
  138. (``->bmdma_setup``), fire (``->bmdma_start``), and halt (``->bmdma_stop``) the
  139. hardware's DMA engine. ``->bmdma_status`` is used to read the standard PCI
  140. IDE DMA Status register.
  141. These hooks are typically either no-ops, or simply not implemented, in
  142. FIS-based drivers.
  143. Most legacy IDE drivers use :c:func:`ata_bmdma_setup` for the
  144. :c:func:`bmdma_setup` hook. :c:func:`ata_bmdma_setup` will write the pointer
  145. to the PRD table to the IDE PRD Table Address register, enable DMA in the DMA
  146. Command register, and call :c:func:`exec_command` to begin the transfer.
  147. Most legacy IDE drivers use :c:func:`ata_bmdma_start` for the
  148. :c:func:`bmdma_start` hook. :c:func:`ata_bmdma_start` will write the
  149. ATA_DMA_START flag to the DMA Command register.
  150. Many legacy IDE drivers use :c:func:`ata_bmdma_stop` for the
  151. :c:func:`bmdma_stop` hook. :c:func:`ata_bmdma_stop` clears the ATA_DMA_START
  152. flag in the DMA command register.
  153. Many legacy IDE drivers use :c:func:`ata_bmdma_status` as the
  154. :c:func:`bmdma_status` hook.
  155. High-level taskfile hooks
  156. ~~~~~~~~~~~~~~~~~~~~~~~~~
  157. ::
  158. enum ata_completion_errors (*qc_prep) (struct ata_queued_cmd *qc);
  159. int (*qc_issue) (struct ata_queued_cmd *qc);
  160. Higher-level hooks, these two hooks can potentially supersede several of
  161. the above taskfile/DMA engine hooks. ``->qc_prep`` is called after the
  162. buffers have been DMA-mapped, and is typically used to populate the
  163. hardware's DMA scatter-gather table. Some drivers use the standard
  164. :c:func:`ata_bmdma_qc_prep` and :c:func:`ata_bmdma_dumb_qc_prep` helper
  165. functions, but more advanced drivers roll their own.
  166. ``->qc_issue`` is used to make a command active, once the hardware and S/G
  167. tables have been prepared. IDE BMDMA drivers use the helper function
  168. :c:func:`ata_sff_qc_issue` for taskfile protocol-based dispatch. More
  169. advanced drivers implement their own ``->qc_issue``.
  170. :c:func:`ata_sff_qc_issue` calls ``->sff_tf_load()``, ``->bmdma_setup()``, and
  171. ``->bmdma_start()`` as necessary to initiate a transfer.
  172. Exception and probe handling (EH)
  173. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  174. ::
  175. void (*freeze) (struct ata_port *ap);
  176. void (*thaw) (struct ata_port *ap);
  177. :c:func:`ata_port_freeze` is called when HSM violations or some other
  178. condition disrupts normal operation of the port. A frozen port is not
  179. allowed to perform any operation until the port is thawed, which usually
  180. follows a successful reset.
  181. The optional ``->freeze()`` callback can be used for freezing the port
  182. hardware-wise (e.g. mask interrupt and stop DMA engine). If a port
  183. cannot be frozen hardware-wise, the interrupt handler must ack and clear
  184. interrupts unconditionally while the port is frozen.
  185. The optional ``->thaw()`` callback is called to perform the opposite of
  186. ``->freeze()``: prepare the port for normal operation once again. Unmask
  187. interrupts, start DMA engine, etc.
  188. ::
  189. void (*error_handler) (struct ata_port *ap);
  190. ``->error_handler()`` is a driver's hook into probe, hotplug, and recovery
  191. and other exceptional conditions. The primary responsibility of an
  192. implementation is to call :c:func:`ata_do_eh` or :c:func:`ata_bmdma_drive_eh`
  193. with a set of EH hooks as arguments:
  194. 'prereset' hook (may be NULL) is called during an EH reset, before any
  195. other actions are taken.
  196. 'postreset' hook (may be NULL) is called after the EH reset is
  197. performed. Based on existing conditions, severity of the problem, and
  198. hardware capabilities,
  199. Either 'softreset' (may be NULL) or 'hardreset' (may be NULL) will be
  200. called to perform the low-level EH reset.
  201. ::
  202. void (*post_internal_cmd) (struct ata_queued_cmd *qc);
  203. Perform any hardware-specific actions necessary to finish processing
  204. after executing a probe-time or EH-time command via
  205. :c:func:`ata_exec_internal`.
  206. Hardware interrupt handling
  207. ~~~~~~~~~~~~~~~~~~~~~~~~~~~
  208. ::
  209. irqreturn_t (*irq_handler)(int, void *, struct pt_regs *);
  210. void (*irq_clear) (struct ata_port *);
  211. ``->irq_handler`` is the interrupt handling routine registered with the
  212. system, by libata. ``->irq_clear`` is called during probe just before the
  213. interrupt handler is registered, to be sure hardware is quiet.
  214. The second argument, dev_instance, should be cast to a pointer to
  215. :c:type:`struct ata_host_set <ata_host_set>`.
  216. Most legacy IDE drivers use :c:func:`ata_sff_interrupt` for the irq_handler
  217. hook, which scans all ports in the host_set, determines which queued
  218. command was active (if any), and calls ata_sff_host_intr(ap,qc).
  219. Most legacy IDE drivers use :c:func:`ata_sff_irq_clear` for the
  220. :c:func:`irq_clear` hook, which simply clears the interrupt and error flags
  221. in the DMA status register.
  222. SATA phy read/write
  223. ~~~~~~~~~~~~~~~~~~~
  224. ::
  225. int (*scr_read) (struct ata_port *ap, unsigned int sc_reg,
  226. u32 *val);
  227. int (*scr_write) (struct ata_port *ap, unsigned int sc_reg,
  228. u32 val);
  229. Read and write standard SATA phy registers.
  230. sc_reg is one of SCR_STATUS, SCR_CONTROL, SCR_ERROR, or SCR_ACTIVE.
  231. Init and shutdown
  232. ~~~~~~~~~~~~~~~~~
  233. ::
  234. int (*port_start) (struct ata_port *ap);
  235. void (*port_stop) (struct ata_port *ap);
  236. void (*host_stop) (struct ata_host_set *host_set);
  237. ``->port_start()`` is called just after the data structures for each port
  238. are initialized. Typically this is used to alloc per-port DMA buffers /
  239. tables / rings, enable DMA engines, and similar tasks. Some drivers also
  240. use this entry point as a chance to allocate driver-private memory for
  241. ``ap->private_data``.
  242. Many drivers use :c:func:`ata_port_start` as this hook or call it from their
  243. own :c:func:`port_start` hooks. :c:func:`ata_port_start` allocates space for
  244. a legacy IDE PRD table and returns.
  245. ``->port_stop()`` is called after ``->host_stop()``. Its sole function is to
  246. release DMA/memory resources, now that they are no longer actively being
  247. used. Many drivers also free driver-private data from port at this time.
  248. ``->host_stop()`` is called after all ``->port_stop()`` calls have completed.
  249. The hook must finalize hardware shutdown, release DMA and other
  250. resources, etc. This hook may be specified as NULL, in which case it is
  251. not called.
  252. Error handling
  253. ==============
  254. This chapter describes how errors are handled under libata. Readers are
  255. advised to read SCSI EH (Documentation/scsi/scsi_eh.rst) and ATA
  256. exceptions doc first.
  257. Origins of commands
  258. -------------------
  259. In libata, a command is represented with
  260. :c:type:`struct ata_queued_cmd <ata_queued_cmd>` or qc.
  261. qc's are preallocated during port initialization and repetitively used
  262. for command executions. Currently only one qc is allocated per port but
  263. yet-to-be-merged NCQ branch allocates one for each tag and maps each qc
  264. to NCQ tag 1-to-1.
  265. libata commands can originate from two sources - libata itself and SCSI
  266. midlayer. libata internal commands are used for initialization and error
  267. handling. All normal blk requests and commands for SCSI emulation are
  268. passed as SCSI commands through queuecommand callback of SCSI host
  269. template.
  270. How commands are issued
  271. -----------------------
  272. Internal commands
  273. Once allocated qc's taskfile is initialized for the command to be
  274. executed. qc currently has two mechanisms to notify completion. One
  275. is via ``qc->complete_fn()`` callback and the other is completion
  276. ``qc->waiting``. ``qc->complete_fn()`` callback is the asynchronous path
  277. used by normal SCSI translated commands and ``qc->waiting`` is the
  278. synchronous (issuer sleeps in process context) path used by internal
  279. commands.
  280. Once initialization is complete, host_set lock is acquired and the
  281. qc is issued.
  282. SCSI commands
  283. All libata drivers use :c:func:`ata_scsi_queuecmd` as
  284. ``hostt->queuecommand`` callback. scmds can either be simulated or
  285. translated. No qc is involved in processing a simulated scmd. The
  286. result is computed right away and the scmd is completed.
  287. ``qc->complete_fn()`` callback is used for completion notification. ATA
  288. commands use :c:func:`ata_scsi_qc_complete` while ATAPI commands use
  289. :c:func:`atapi_qc_complete`. Both functions end up calling ``qc->scsidone``
  290. to notify upper layer when the qc is finished. After translation is
  291. completed, the qc is issued with :c:func:`ata_qc_issue`.
  292. Note that SCSI midlayer invokes hostt->queuecommand while holding
  293. host_set lock, so all above occur while holding host_set lock.
  294. How commands are processed
  295. --------------------------
  296. Depending on which protocol and which controller are used, commands are
  297. processed differently. For the purpose of discussion, a controller which
  298. uses taskfile interface and all standard callbacks is assumed.
  299. Currently 6 ATA command protocols are used. They can be sorted into the
  300. following four categories according to how they are processed.
  301. ATA NO DATA or DMA
  302. ATA_PROT_NODATA and ATA_PROT_DMA fall into this category. These
  303. types of commands don't require any software intervention once
  304. issued. Device will raise interrupt on completion.
  305. ATA PIO
  306. ATA_PROT_PIO is in this category. libata currently implements PIO
  307. with polling. ATA_NIEN bit is set to turn off interrupt and
  308. pio_task on ata_wq performs polling and IO.
  309. ATAPI NODATA or DMA
  310. ATA_PROT_ATAPI_NODATA and ATA_PROT_ATAPI_DMA are in this
  311. category. packet_task is used to poll BSY bit after issuing PACKET
  312. command. Once BSY is turned off by the device, packet_task
  313. transfers CDB and hands off processing to interrupt handler.
  314. ATAPI PIO
  315. ATA_PROT_ATAPI is in this category. ATA_NIEN bit is set and, as
  316. in ATAPI NODATA or DMA, packet_task submits cdb. However, after
  317. submitting cdb, further processing (data transfer) is handed off to
  318. pio_task.
  319. How commands are completed
  320. --------------------------
  321. Once issued, all qc's are either completed with :c:func:`ata_qc_complete` or
  322. time out. For commands which are handled by interrupts,
  323. :c:func:`ata_host_intr` invokes :c:func:`ata_qc_complete`, and, for PIO tasks,
  324. pio_task invokes :c:func:`ata_qc_complete`. In error cases, packet_task may
  325. also complete commands.
  326. :c:func:`ata_qc_complete` does the following.
  327. 1. DMA memory is unmapped.
  328. 2. ATA_QCFLAG_ACTIVE is cleared from qc->flags.
  329. 3. :c:expr:`qc->complete_fn` callback is invoked. If the return value of the
  330. callback is not zero. Completion is short circuited and
  331. :c:func:`ata_qc_complete` returns.
  332. 4. :c:func:`__ata_qc_complete` is called, which does
  333. 1. ``qc->flags`` is cleared to zero.
  334. 2. ``ap->active_tag`` and ``qc->tag`` are poisoned.
  335. 3. ``qc->waiting`` is cleared & completed (in that order).
  336. 4. qc is deallocated by clearing appropriate bit in ``ap->qactive``.
  337. So, it basically notifies upper layer and deallocates qc. One exception
  338. is short-circuit path in #3 which is used by :c:func:`atapi_qc_complete`.
  339. For all non-ATAPI commands, whether it fails or not, almost the same
  340. code path is taken and very little error handling takes place. A qc is
  341. completed with success status if it succeeded, with failed status
  342. otherwise.
  343. However, failed ATAPI commands require more handling as REQUEST SENSE is
  344. needed to acquire sense data. If an ATAPI command fails,
  345. :c:func:`ata_qc_complete` is invoked with error status, which in turn invokes
  346. :c:func:`atapi_qc_complete` via ``qc->complete_fn()`` callback.
  347. This makes :c:func:`atapi_qc_complete` set ``scmd->result`` to
  348. SAM_STAT_CHECK_CONDITION, complete the scmd and return 1. As the
  349. sense data is empty but ``scmd->result`` is CHECK CONDITION, SCSI midlayer
  350. will invoke EH for the scmd, and returning 1 makes :c:func:`ata_qc_complete`
  351. to return without deallocating the qc. This leads us to
  352. :c:func:`ata_scsi_error` with partially completed qc.
  353. :c:func:`ata_scsi_error`
  354. ------------------------
  355. :c:func:`ata_scsi_error` is the current ``transportt->eh_strategy_handler()``
  356. for libata. As discussed above, this will be entered in two cases -
  357. timeout and ATAPI error completion. This function will check if a qc is active
  358. and has not failed yet. Such a qc will be marked with AC_ERR_TIMEOUT such that
  359. EH will know to handle it later. Then it calls low level libata driver's
  360. :c:func:`error_handler` callback.
  361. When the :c:func:`error_handler` callback is invoked it stops BMDMA and
  362. completes the qc. Note that as we're currently in EH, we cannot call
  363. scsi_done. As described in SCSI EH doc, a recovered scmd should be
  364. either retried with :c:func:`scsi_queue_insert` or finished with
  365. :c:func:`scsi_finish_command`. Here, we override ``qc->scsidone`` with
  366. :c:func:`scsi_finish_command` and calls :c:func:`ata_qc_complete`.
  367. If EH is invoked due to a failed ATAPI qc, the qc here is completed but
  368. not deallocated. The purpose of this half-completion is to use the qc as
  369. place holder to make EH code reach this place. This is a bit hackish,
  370. but it works.
  371. Once control reaches here, the qc is deallocated by invoking
  372. :c:func:`__ata_qc_complete` explicitly. Then, internal qc for REQUEST SENSE
  373. is issued. Once sense data is acquired, scmd is finished by directly
  374. invoking :c:func:`scsi_finish_command` on the scmd. Note that as we already
  375. have completed and deallocated the qc which was associated with the
  376. scmd, we don't need to/cannot call :c:func:`ata_qc_complete` again.
  377. Problems with the current EH
  378. ----------------------------
  379. - Error representation is too crude. Currently any and all error
  380. conditions are represented with ATA STATUS and ERROR registers.
  381. Errors which aren't ATA device errors are treated as ATA device
  382. errors by setting ATA_ERR bit. Better error descriptor which can
  383. properly represent ATA and other errors/exceptions is needed.
  384. - When handling timeouts, no action is taken to make device forget
  385. about the timed out command and ready for new commands.
  386. - EH handling via :c:func:`ata_scsi_error` is not properly protected from
  387. usual command processing. On EH entrance, the device is not in
  388. quiescent state. Timed out commands may succeed or fail any time.
  389. pio_task and atapi_task may still be running.
  390. - Too weak error recovery. Devices / controllers causing HSM mismatch
  391. errors and other errors quite often require reset to return to known
  392. state. Also, advanced error handling is necessary to support features
  393. like NCQ and hotplug.
  394. - ATA errors are directly handled in the interrupt handler and PIO
  395. errors in pio_task. This is problematic for advanced error handling
  396. for the following reasons.
  397. First, advanced error handling often requires context and internal qc
  398. execution.
  399. Second, even a simple failure (say, CRC error) needs information
  400. gathering and could trigger complex error handling (say, resetting &
  401. reconfiguring). Having multiple code paths to gather information,
  402. enter EH and trigger actions makes life painful.
  403. Third, scattered EH code makes implementing low level drivers
  404. difficult. Low level drivers override libata callbacks. If EH is
  405. scattered over several places, each affected callbacks should perform
  406. its part of error handling. This can be error prone and painful.
  407. libata Library
  408. ==============
  409. .. kernel-doc:: drivers/ata/libata-core.c
  410. :export:
  411. libata Core Internals
  412. =====================
  413. .. kernel-doc:: drivers/ata/libata-core.c
  414. :internal:
  415. .. kernel-doc:: drivers/ata/libata-eh.c
  416. libata SCSI translation/emulation
  417. =================================
  418. .. kernel-doc:: drivers/ata/libata-scsi.c
  419. :export:
  420. .. kernel-doc:: drivers/ata/libata-scsi.c
  421. :internal:
  422. ATA errors and exceptions
  423. =========================
  424. This chapter tries to identify what error/exception conditions exist for
  425. ATA/ATAPI devices and describe how they should be handled in
  426. implementation-neutral way.
  427. The term 'error' is used to describe conditions where either an explicit
  428. error condition is reported from device or a command has timed out.
  429. The term 'exception' is either used to describe exceptional conditions
  430. which are not errors (say, power or hotplug events), or to describe both
  431. errors and non-error exceptional conditions. Where explicit distinction
  432. between error and exception is necessary, the term 'non-error exception'
  433. is used.
  434. Exception categories
  435. --------------------
  436. Exceptions are described primarily with respect to legacy taskfile + bus
  437. master IDE interface. If a controller provides other better mechanism
  438. for error reporting, mapping those into categories described below
  439. shouldn't be difficult.
  440. In the following sections, two recovery actions - reset and
  441. reconfiguring transport - are mentioned. These are described further in
  442. `EH recovery actions <#exrec>`__.
  443. HSM violation
  444. ~~~~~~~~~~~~~
  445. This error is indicated when STATUS value doesn't match HSM requirement
  446. during issuing or execution any ATA/ATAPI command.
  447. - ATA_STATUS doesn't contain !BSY && DRDY && !DRQ while trying to
  448. issue a command.
  449. - !BSY && !DRQ during PIO data transfer.
  450. - DRQ on command completion.
  451. - !BSY && ERR after CDB transfer starts but before the last byte of CDB
  452. is transferred. ATA/ATAPI standard states that "The device shall not
  453. terminate the PACKET command with an error before the last byte of
  454. the command packet has been written" in the error outputs description
  455. of PACKET command and the state diagram doesn't include such
  456. transitions.
  457. In these cases, HSM is violated and not much information regarding the
  458. error can be acquired from STATUS or ERROR register. IOW, this error can
  459. be anything - driver bug, faulty device, controller and/or cable.
  460. As HSM is violated, reset is necessary to restore known state.
  461. Reconfiguring transport for lower speed might be helpful too as
  462. transmission errors sometimes cause this kind of errors.
  463. ATA/ATAPI device error (non-NCQ / non-CHECK CONDITION)
  464. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  465. These are errors detected and reported by ATA/ATAPI devices indicating
  466. device problems. For this type of errors, STATUS and ERROR register
  467. values are valid and describe error condition. Note that some of ATA bus
  468. errors are detected by ATA/ATAPI devices and reported using the same
  469. mechanism as device errors. Those cases are described later in this
  470. section.
  471. For ATA commands, this type of errors are indicated by !BSY && ERR
  472. during command execution and on completion.
  473. For ATAPI commands,
  474. - !BSY && ERR && ABRT right after issuing PACKET indicates that PACKET
  475. command is not supported and falls in this category.
  476. - !BSY && ERR(==CHK) && !ABRT after the last byte of CDB is transferred
  477. indicates CHECK CONDITION and doesn't fall in this category.
  478. - !BSY && ERR(==CHK) && ABRT after the last byte of CDB is transferred
  479. \*probably\* indicates CHECK CONDITION and doesn't fall in this
  480. category.
  481. Of errors detected as above, the following are not ATA/ATAPI device
  482. errors but ATA bus errors and should be handled according to
  483. `ATA bus error <#excatATAbusErr>`__.
  484. CRC error during data transfer
  485. This is indicated by ICRC bit in the ERROR register and means that
  486. corruption occurred during data transfer. Up to ATA/ATAPI-7, the
  487. standard specifies that this bit is only applicable to UDMA
  488. transfers but ATA/ATAPI-8 draft revision 1f says that the bit may be
  489. applicable to multiword DMA and PIO.
  490. ABRT error during data transfer or on completion
  491. Up to ATA/ATAPI-7, the standard specifies that ABRT could be set on
  492. ICRC errors and on cases where a device is not able to complete a
  493. command. Combined with the fact that MWDMA and PIO transfer errors
  494. aren't allowed to use ICRC bit up to ATA/ATAPI-7, it seems to imply
  495. that ABRT bit alone could indicate transfer errors.
  496. However, ATA/ATAPI-8 draft revision 1f removes the part that ICRC
  497. errors can turn on ABRT. So, this is kind of gray area. Some
  498. heuristics are needed here.
  499. ATA/ATAPI device errors can be further categorized as follows.
  500. Media errors
  501. This is indicated by UNC bit in the ERROR register. ATA devices
  502. reports UNC error only after certain number of retries cannot
  503. recover the data, so there's nothing much else to do other than
  504. notifying upper layer.
  505. READ and WRITE commands report CHS or LBA of the first failed sector
  506. but ATA/ATAPI standard specifies that the amount of transferred data
  507. on error completion is indeterminate, so we cannot assume that
  508. sectors preceding the failed sector have been transferred and thus
  509. cannot complete those sectors successfully as SCSI does.
  510. Media changed / media change requested error
  511. <<TODO: fill here>>
  512. Address error
  513. This is indicated by IDNF bit in the ERROR register. Report to upper
  514. layer.
  515. Other errors
  516. This can be invalid command or parameter indicated by ABRT ERROR bit
  517. or some other error condition. Note that ABRT bit can indicate a lot
  518. of things including ICRC and Address errors. Heuristics needed.
  519. Depending on commands, not all STATUS/ERROR bits are applicable. These
  520. non-applicable bits are marked with "na" in the output descriptions but
  521. up to ATA/ATAPI-7 no definition of "na" can be found. However,
  522. ATA/ATAPI-8 draft revision 1f describes "N/A" as follows.
  523. 3.2.3.3a N/A
  524. A keyword the indicates a field has no defined value in this
  525. standard and should not be checked by the host or device. N/A
  526. fields should be cleared to zero.
  527. So, it seems reasonable to assume that "na" bits are cleared to zero by
  528. devices and thus need no explicit masking.
  529. ATAPI device CHECK CONDITION
  530. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  531. ATAPI device CHECK CONDITION error is indicated by set CHK bit (ERR bit)
  532. in the STATUS register after the last byte of CDB is transferred for a
  533. PACKET command. For this kind of errors, sense data should be acquired
  534. to gather information regarding the errors. REQUEST SENSE packet command
  535. should be used to acquire sense data.
  536. Once sense data is acquired, this type of errors can be handled
  537. similarly to other SCSI errors. Note that sense data may indicate ATA
  538. bus error (e.g. Sense Key 04h HARDWARE ERROR && ASC/ASCQ 47h/00h SCSI
  539. PARITY ERROR). In such cases, the error should be considered as an ATA
  540. bus error and handled according to `ATA bus error <#excatATAbusErr>`__.
  541. ATA device error (NCQ)
  542. ~~~~~~~~~~~~~~~~~~~~~~
  543. NCQ command error is indicated by cleared BSY and set ERR bit during NCQ
  544. command phase (one or more NCQ commands outstanding). Although STATUS
  545. and ERROR registers will contain valid values describing the error, READ
  546. LOG EXT is required to clear the error condition, determine which
  547. command has failed and acquire more information.
  548. READ LOG EXT Log Page 10h reports which tag has failed and taskfile
  549. register values describing the error. With this information the failed
  550. command can be handled as a normal ATA command error as in
  551. `ATA/ATAPI device error (non-NCQ / non-CHECK CONDITION) <#excatDevErr>`__
  552. and all other in-flight commands must be retried. Note that this retry
  553. should not be counted - it's likely that commands retried this way would
  554. have completed normally if it were not for the failed command.
  555. Note that ATA bus errors can be reported as ATA device NCQ errors. This
  556. should be handled as described in `ATA bus error <#excatATAbusErr>`__.
  557. If READ LOG EXT Log Page 10h fails or reports NQ, we're thoroughly
  558. screwed. This condition should be treated according to
  559. `HSM violation <#excatHSMviolation>`__.
  560. ATA bus error
  561. ~~~~~~~~~~~~~
  562. ATA bus error means that data corruption occurred during transmission
  563. over ATA bus (SATA or PATA). This type of errors can be indicated by
  564. - ICRC or ABRT error as described in
  565. `ATA/ATAPI device error (non-NCQ / non-CHECK CONDITION) <#excatDevErr>`__.
  566. - Controller-specific error completion with error information
  567. indicating transmission error.
  568. - On some controllers, command timeout. In this case, there may be a
  569. mechanism to determine that the timeout is due to transmission error.
  570. - Unknown/random errors, timeouts and all sorts of weirdities.
  571. As described above, transmission errors can cause wide variety of
  572. symptoms ranging from device ICRC error to random device lockup, and,
  573. for many cases, there is no way to tell if an error condition is due to
  574. transmission error or not; therefore, it's necessary to employ some kind
  575. of heuristic when dealing with errors and timeouts. For example,
  576. encountering repetitive ABRT errors for known supported command is
  577. likely to indicate ATA bus error.
  578. Once it's determined that ATA bus errors have possibly occurred,
  579. lowering ATA bus transmission speed is one of actions which may
  580. alleviate the problem. See `Reconfigure transport <#exrecReconf>`__ for
  581. more information.
  582. PCI bus error
  583. ~~~~~~~~~~~~~
  584. Data corruption or other failures during transmission over PCI (or other
  585. system bus). For standard BMDMA, this is indicated by Error bit in the
  586. BMDMA Status register. This type of errors must be logged as it
  587. indicates something is very wrong with the system. Resetting host
  588. controller is recommended.
  589. Late completion
  590. ~~~~~~~~~~~~~~~
  591. This occurs when timeout occurs and the timeout handler finds out that
  592. the timed out command has completed successfully or with error. This is
  593. usually caused by lost interrupts. This type of errors must be logged.
  594. Resetting host controller is recommended.
  595. Unknown error (timeout)
  596. ~~~~~~~~~~~~~~~~~~~~~~~
  597. This is when timeout occurs and the command is still processing or the
  598. host and device are in unknown state. When this occurs, HSM could be in
  599. any valid or invalid state. To bring the device to known state and make
  600. it forget about the timed out command, resetting is necessary. The timed
  601. out command may be retried.
  602. Timeouts can also be caused by transmission errors. Refer to
  603. `ATA bus error <#excatATAbusErr>`__ for more details.
  604. Hotplug and power management exceptions
  605. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  606. <<TODO: fill here>>
  607. EH recovery actions
  608. -------------------
  609. This section discusses several important recovery actions.
  610. Clearing error condition
  611. ~~~~~~~~~~~~~~~~~~~~~~~~
  612. Many controllers require its error registers to be cleared by error
  613. handler. Different controllers may have different requirements.
  614. For SATA, it's strongly recommended to clear at least SError register
  615. during error handling.
  616. Reset
  617. ~~~~~
  618. During EH, resetting is necessary in the following cases.
  619. - HSM is in unknown or invalid state
  620. - HBA is in unknown or invalid state
  621. - EH needs to make HBA/device forget about in-flight commands
  622. - HBA/device behaves weirdly
  623. Resetting during EH might be a good idea regardless of error condition
  624. to improve EH robustness. Whether to reset both or either one of HBA and
  625. device depends on situation but the following scheme is recommended.
  626. - When it's known that HBA is in ready state but ATA/ATAPI device is in
  627. unknown state, reset only device.
  628. - If HBA is in unknown state, reset both HBA and device.
  629. HBA resetting is implementation specific. For a controller complying to
  630. taskfile/BMDMA PCI IDE, stopping active DMA transaction may be
  631. sufficient iff BMDMA state is the only HBA context. But even mostly
  632. taskfile/BMDMA PCI IDE complying controllers may have implementation
  633. specific requirements and mechanism to reset themselves. This must be
  634. addressed by specific drivers.
  635. OTOH, ATA/ATAPI standard describes in detail ways to reset ATA/ATAPI
  636. devices.
  637. PATA hardware reset
  638. This is hardware initiated device reset signalled with asserted PATA
  639. RESET- signal. There is no standard way to initiate hardware reset
  640. from software although some hardware provides registers that allow
  641. driver to directly tweak the RESET- signal.
  642. Software reset
  643. This is achieved by turning CONTROL SRST bit on for at least 5us.
  644. Both PATA and SATA support it but, in case of SATA, this may require
  645. controller-specific support as the second Register FIS to clear SRST
  646. should be transmitted while BSY bit is still set. Note that on PATA,
  647. this resets both master and slave devices on a channel.
  648. EXECUTE DEVICE DIAGNOSTIC command
  649. Although ATA/ATAPI standard doesn't describe exactly, EDD implies
  650. some level of resetting, possibly similar level with software reset.
  651. Host-side EDD protocol can be handled with normal command processing
  652. and most SATA controllers should be able to handle EDD's just like
  653. other commands. As in software reset, EDD affects both devices on a
  654. PATA bus.
  655. Although EDD does reset devices, this doesn't suit error handling as
  656. EDD cannot be issued while BSY is set and it's unclear how it will
  657. act when device is in unknown/weird state.
  658. ATAPI DEVICE RESET command
  659. This is very similar to software reset except that reset can be
  660. restricted to the selected device without affecting the other device
  661. sharing the cable.
  662. SATA phy reset
  663. This is the preferred way of resetting a SATA device. In effect,
  664. it's identical to PATA hardware reset. Note that this can be done
  665. with the standard SCR Control register. As such, it's usually easier
  666. to implement than software reset.
  667. One more thing to consider when resetting devices is that resetting
  668. clears certain configuration parameters and they need to be set to their
  669. previous or newly adjusted values after reset.
  670. Parameters affected are.
  671. - CHS set up with INITIALIZE DEVICE PARAMETERS (seldom used)
  672. - Parameters set with SET FEATURES including transfer mode setting
  673. - Block count set with SET MULTIPLE MODE
  674. - Other parameters (SET MAX, MEDIA LOCK...)
  675. ATA/ATAPI standard specifies that some parameters must be maintained
  676. across hardware or software reset, but doesn't strictly specify all of
  677. them. Always reconfiguring needed parameters after reset is required for
  678. robustness. Note that this also applies when resuming from deep sleep
  679. (power-off).
  680. Also, ATA/ATAPI standard requires that IDENTIFY DEVICE / IDENTIFY PACKET
  681. DEVICE is issued after any configuration parameter is updated or a
  682. hardware reset and the result used for further operation. OS driver is
  683. required to implement revalidation mechanism to support this.
  684. Reconfigure transport
  685. ~~~~~~~~~~~~~~~~~~~~~
  686. For both PATA and SATA, a lot of corners are cut for cheap connectors,
  687. cables or controllers and it's quite common to see high transmission
  688. error rate. This can be mitigated by lowering transmission speed.
  689. The following is a possible scheme Jeff Garzik suggested.
  690. If more than $N (3?) transmission errors happen in 15 minutes,
  691. - if SATA, decrease SATA PHY speed. if speed cannot be decreased,
  692. - decrease UDMA xfer speed. if at UDMA0, switch to PIO4,
  693. - decrease PIO xfer speed. if at PIO3, complain, but continue
  694. ata_piix Internals
  695. ===================
  696. .. kernel-doc:: drivers/ata/ata_piix.c
  697. :internal:
  698. sata_sil Internals
  699. ===================
  700. .. kernel-doc:: drivers/ata/sata_sil.c
  701. :internal:
  702. Thanks
  703. ======
  704. The bulk of the ATA knowledge comes thanks to long conversations with
  705. Andre Hedrick (www.linux-ide.org), and long hours pondering the ATA and
  706. SCSI specifications.
  707. Thanks to Alan Cox for pointing out similarities between SATA and SCSI,
  708. and in general for motivation to hack on libata.
  709. libata's device detection method, ata_pio_devchk, and in general all
  710. the early probing was based on extensive study of Hale Landis's
  711. probe/reset code in his ATADRVR driver (www.ata-atapi.com).