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" []>
5 <book id="libataDevGuide">
7 <title>libATA Developer's Guide</title>
11 <firstname>Jeff</firstname>
12 <surname>Garzik</surname>
17 <year>2003-2005</year>
18 <holder>Jeff Garzik</holder>
23 The contents of this file are subject to the Open
24 Software License version 1.1 that can be found at
25 <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
30 Alternatively, the contents of this file may be used under the terms
31 of the GNU General Public License version 2 (the "GPL") as distributed
32 in the kernel source COPYING file, in which case the provisions of
33 the GPL are applicable instead of the above. If you wish to allow
34 the use of your version of this file only under the terms of the
35 GPL and not to allow others to use your version of this file under
36 the OSL, indicate your decision by deleting the provisions above and
37 replace them with the notice and other provisions required by the GPL.
38 If you do not delete the provisions above, a recipient may use your
39 version of this file under either the OSL or the GPL.
47 <chapter id="libataIntroduction">
48 <title>Introduction</title>
50 libATA is a library used inside the Linux kernel to support ATA host
51 controllers and devices. libATA provides an ATA driver API, class
52 transports for ATA and ATAPI devices, and SCSI<->ATA translation
53 for ATA devices according to the T10 SAT specification.
56 This Guide documents the libATA driver API, library functions, library
57 internals, and a couple sample ATA low-level drivers.
61 <chapter id="libataDriverApi">
62 <title>libata Driver API</title>
64 struct ata_port_operations is defined for every low-level libata
65 hardware driver, and it controls how the low-level driver
66 interfaces with the ATA and SCSI layers.
69 FIS-based drivers will hook into the system with ->qc_prep() and
70 ->qc_issue() high-level hooks. Hardware which behaves in a manner
71 similar to PCI IDE hardware may utilize several generic helpers,
72 defining at a bare minimum the bus I/O addresses of the ATA shadow
76 <title>struct ata_port_operations</title>
78 <sect2><title>Disable ATA port</title>
80 void (*port_disable) (struct ata_port *);
84 Called from ata_bus_probe() and ata_bus_reset() error paths,
85 as well as when unregistering from the SCSI module (rmmod, hot
87 This function should do whatever needs to be done to take the
88 port out of use. In most cases, ata_port_disable() can be used
92 Called from ata_bus_probe() on a failed probe.
93 Called from ata_bus_reset() on a failed bus reset.
94 Called from ata_scsi_release().
99 <sect2><title>Post-IDENTIFY device configuration</title>
101 void (*dev_config) (struct ata_port *, struct ata_device *);
105 Called after IDENTIFY [PACKET] DEVICE is issued to each device
106 found. Typically used to apply device-specific fixups prior to
107 issue of SET FEATURES - XFER MODE, and prior to operation.
110 Called by ata_device_add() after ata_dev_identify() determines
114 This entry may be specified as NULL in ata_port_operations.
119 <sect2><title>Set PIO/DMA mode</title>
121 void (*set_piomode) (struct ata_port *, struct ata_device *);
122 void (*set_dmamode) (struct ata_port *, struct ata_device *);
123 void (*post_set_mode) (struct ata_port *);
124 unsigned int (*mode_filter) (struct ata_port *, struct ata_device *, unsigned int);
128 Hooks called prior to the issue of SET FEATURES - XFER MODE
129 command. The optional ->mode_filter() hook is called when libata
130 has built a mask of the possible modes. This is passed to the
131 ->mode_filter() function which should return a mask of valid modes
132 after filtering those unsuitable due to hardware limits. It is not
133 valid to use this interface to add modes.
136 dev->pio_mode and dev->dma_mode are guaranteed to be valid when
137 ->set_piomode() and when ->set_dmamode() is called. The timings for
138 any other drive sharing the cable will also be valid at this point.
139 That is the library records the decisions for the modes of each
140 drive on a channel before it attempts to set any of them.
144 called unconditionally, after the SET FEATURES - XFER MODE
145 command completes successfully.
149 ->set_piomode() is always called (if present), but
150 ->set_dma_mode() is only called if DMA is possible.
155 <sect2><title>Taskfile read/write</title>
157 void (*tf_load) (struct ata_port *ap, struct ata_taskfile *tf);
158 void (*tf_read) (struct ata_port *ap, struct ata_taskfile *tf);
162 ->tf_load() is called to load the given taskfile into hardware
163 registers / DMA buffers. ->tf_read() is called to read the
164 hardware registers / DMA buffers, to obtain the current set of
165 taskfile register values.
166 Most drivers for taskfile-based hardware (PIO or MMIO) use
167 ata_tf_load() and ata_tf_read() for these hooks.
172 <sect2><title>ATA command execute</title>
174 void (*exec_command)(struct ata_port *ap, struct ata_taskfile *tf);
178 causes an ATA command, previously loaded with
179 ->tf_load(), to be initiated in hardware.
180 Most drivers for taskfile-based hardware use ata_exec_command()
186 <sect2><title>Per-cmd ATAPI DMA capabilities filter</title>
188 int (*check_atapi_dma) (struct ata_queued_cmd *qc);
192 Allow low-level driver to filter ATA PACKET commands, returning a status
193 indicating whether or not it is OK to use DMA for the supplied PACKET
197 This hook may be specified as NULL, in which case libata will
198 assume that atapi dma can be supported.
203 <sect2><title>Read specific ATA shadow registers</title>
205 u8 (*check_status)(struct ata_port *ap);
206 u8 (*check_altstatus)(struct ata_port *ap);
207 u8 (*check_err)(struct ata_port *ap);
211 Reads the Status/AltStatus/Error ATA shadow register from
212 hardware. On some hardware, reading the Status register has
213 the side effect of clearing the interrupt condition.
214 Most drivers for taskfile-based hardware use
215 ata_check_status() for this hook.
218 Note that because this is called from ata_device_add(), at
219 least a dummy function that clears device interrupts must be
220 provided for all drivers, even if the controller doesn't
221 actually have a taskfile status register.
226 <sect2><title>Select ATA device on bus</title>
228 void (*dev_select)(struct ata_port *ap, unsigned int device);
232 Issues the low-level hardware command(s) that causes one of N
233 hardware devices to be considered 'selected' (active and
234 available for use) on the ATA bus. This generally has no
235 meaning on FIS-based devices.
238 Most drivers for taskfile-based hardware use
239 ata_std_dev_select() for this hook. Controllers which do not
240 support second drives on a port (such as SATA contollers) will
241 use ata_noop_dev_select().
246 <sect2><title>Private tuning method</title>
248 void (*set_mode) (struct ata_port *ap);
252 By default libata performs drive and controller tuning in
253 accordance with the ATA timing rules and also applies blacklists
254 and cable limits. Some controllers need special handling and have
255 custom tuning rules, typically raid controllers that use ATA
256 commands but do not actually do drive timing.
261 This hook should not be used to replace the standard controller
262 tuning logic when a controller has quirks. Replacing the default
263 tuning logic in that case would bypass handling for drive and
264 bridge quirks that may be important to data reliability. If a
265 controller needs to filter the mode selection it should use the
266 mode_filter hook instead.
272 <sect2><title>Reset ATA bus</title>
274 void (*phy_reset) (struct ata_port *ap);
278 The very first step in the probe phase. Actions vary depending
279 on the bus type, typically. After waking up the device and probing
280 for device presence (PATA and SATA), typically a soft reset
281 (SRST) will be performed. Drivers typically use the helper
282 functions ata_bus_reset() or sata_phy_reset() for this hook.
283 Many SATA drivers use sata_phy_reset() or call it from within
284 their own phy_reset() functions.
289 <sect2><title>Control PCI IDE BMDMA engine</title>
291 void (*bmdma_setup) (struct ata_queued_cmd *qc);
292 void (*bmdma_start) (struct ata_queued_cmd *qc);
293 void (*bmdma_stop) (struct ata_port *ap);
294 u8 (*bmdma_status) (struct ata_port *ap);
298 When setting up an IDE BMDMA transaction, these hooks arm
299 (->bmdma_setup), fire (->bmdma_start), and halt (->bmdma_stop)
300 the hardware's DMA engine. ->bmdma_status is used to read the standard
301 PCI IDE DMA Status register.
305 These hooks are typically either no-ops, or simply not implemented, in
309 Most legacy IDE drivers use ata_bmdma_setup() for the bmdma_setup()
310 hook. ata_bmdma_setup() will write the pointer to the PRD table to
311 the IDE PRD Table Address register, enable DMA in the DMA Command
312 register, and call exec_command() to begin the transfer.
315 Most legacy IDE drivers use ata_bmdma_start() for the bmdma_start()
316 hook. ata_bmdma_start() will write the ATA_DMA_START flag to the DMA
320 Many legacy IDE drivers use ata_bmdma_stop() for the bmdma_stop()
321 hook. ata_bmdma_stop() clears the ATA_DMA_START flag in the DMA
325 Many legacy IDE drivers use ata_bmdma_status() as the bmdma_status() hook.
330 <sect2><title>High-level taskfile hooks</title>
332 void (*qc_prep) (struct ata_queued_cmd *qc);
333 int (*qc_issue) (struct ata_queued_cmd *qc);
337 Higher-level hooks, these two hooks can potentially supercede
338 several of the above taskfile/DMA engine hooks. ->qc_prep is
339 called after the buffers have been DMA-mapped, and is typically
340 used to populate the hardware's DMA scatter-gather table.
341 Most drivers use the standard ata_qc_prep() helper function, but
342 more advanced drivers roll their own.
345 ->qc_issue is used to make a command active, once the hardware
346 and S/G tables have been prepared. IDE BMDMA drivers use the
347 helper function ata_qc_issue_prot() for taskfile protocol-based
348 dispatch. More advanced drivers implement their own ->qc_issue.
351 ata_qc_issue_prot() calls ->tf_load(), ->bmdma_setup(), and
352 ->bmdma_start() as necessary to initiate a transfer.
357 <sect2><title>Timeout (error) handling</title>
359 void (*eng_timeout) (struct ata_port *ap);
363 This is a high level error handling function, called from the
364 error handling thread, when a command times out. Most newer
365 hardware will implement its own error handling code here. IDE BMDMA
366 drivers may use the helper function ata_eng_timeout().
371 <sect2><title>Hardware interrupt handling</title>
373 irqreturn_t (*irq_handler)(int, void *, struct pt_regs *);
374 void (*irq_clear) (struct ata_port *);
378 ->irq_handler is the interrupt handling routine registered with
379 the system, by libata. ->irq_clear is called during probe just
380 before the interrupt handler is registered, to be sure hardware
384 The second argument, dev_instance, should be cast to a pointer
385 to struct ata_host_set.
388 Most legacy IDE drivers use ata_interrupt() for the
389 irq_handler hook, which scans all ports in the host_set,
390 determines which queued command was active (if any), and calls
391 ata_host_intr(ap,qc).
394 Most legacy IDE drivers use ata_bmdma_irq_clear() for the
395 irq_clear() hook, which simply clears the interrupt and error
396 flags in the DMA status register.
401 <sect2><title>SATA phy read/write</title>
403 u32 (*scr_read) (struct ata_port *ap, unsigned int sc_reg);
404 void (*scr_write) (struct ata_port *ap, unsigned int sc_reg,
409 Read and write standard SATA phy registers. Currently only used
410 if ->phy_reset hook called the sata_phy_reset() helper function.
411 sc_reg is one of SCR_STATUS, SCR_CONTROL, SCR_ERROR, or SCR_ACTIVE.
416 <sect2><title>Init and shutdown</title>
418 int (*port_start) (struct ata_port *ap);
419 void (*port_stop) (struct ata_port *ap);
420 void (*host_stop) (struct ata_host_set *host_set);
424 ->port_start() is called just after the data structures for each
425 port are initialized. Typically this is used to alloc per-port
426 DMA buffers / tables / rings, enable DMA engines, and similar
427 tasks. Some drivers also use this entry point as a chance to
428 allocate driver-private memory for ap->private_data.
431 Many drivers use ata_port_start() as this hook or call
432 it from their own port_start() hooks. ata_port_start()
433 allocates space for a legacy IDE PRD table and returns.
436 ->port_stop() is called after ->host_stop(). It's sole function
437 is to release DMA/memory resources, now that they are no longer
438 actively being used. Many drivers also free driver-private
439 data from port at this time.
442 Many drivers use ata_port_stop() as this hook, which frees the
446 ->host_stop() is called after all ->port_stop() calls
447 have completed. The hook must finalize hardware shutdown, release DMA
448 and other resources, etc.
449 This hook may be specified as NULL, in which case it is not called.
457 <chapter id="libataEH">
458 <title>Error handling</title>
461 This chapter describes how errors are handled under libata.
462 Readers are advised to read SCSI EH
463 (Documentation/scsi/scsi_eh.txt) and ATA exceptions doc first.
466 <sect1><title>Origins of commands</title>
468 In libata, a command is represented with struct ata_queued_cmd
469 or qc. qc's are preallocated during port initialization and
470 repetitively used for command executions. Currently only one
471 qc is allocated per port but yet-to-be-merged NCQ branch
472 allocates one for each tag and maps each qc to NCQ tag 1-to-1.
475 libata commands can originate from two sources - libata itself
476 and SCSI midlayer. libata internal commands are used for
477 initialization and error handling. All normal blk requests
478 and commands for SCSI emulation are passed as SCSI commands
479 through queuecommand callback of SCSI host template.
483 <sect1><title>How commands are issued</title>
487 <varlistentry><term>Internal commands</term>
490 First, qc is allocated and initialized using
491 ata_qc_new_init(). Although ata_qc_new_init() doesn't
492 implement any wait or retry mechanism when qc is not
493 available, internal commands are currently issued only during
494 initialization and error recovery, so no other command is
495 active and allocation is guaranteed to succeed.
498 Once allocated qc's taskfile is initialized for the command to
499 be executed. qc currently has two mechanisms to notify
500 completion. One is via qc->complete_fn() callback and the
501 other is completion qc->waiting. qc->complete_fn() callback
502 is the asynchronous path used by normal SCSI translated
503 commands and qc->waiting is the synchronous (issuer sleeps in
504 process context) path used by internal commands.
507 Once initialization is complete, host_set lock is acquired
508 and the qc is issued.
513 <varlistentry><term>SCSI commands</term>
516 All libata drivers use ata_scsi_queuecmd() as
517 hostt->queuecommand callback. scmds can either be simulated
518 or translated. No qc is involved in processing a simulated
519 scmd. The result is computed right away and the scmd is
523 For a translated scmd, ata_qc_new_init() is invoked to
524 allocate a qc and the scmd is translated into the qc. SCSI
525 midlayer's completion notification function pointer is stored
529 qc->complete_fn() callback is used for completion
530 notification. ATA commands use ata_scsi_qc_complete() while
531 ATAPI commands use atapi_qc_complete(). Both functions end up
532 calling qc->scsidone to notify upper layer when the qc is
533 finished. After translation is completed, the qc is issued
537 Note that SCSI midlayer invokes hostt->queuecommand while
538 holding host_set lock, so all above occur while holding
547 <sect1><title>How commands are processed</title>
549 Depending on which protocol and which controller are used,
550 commands are processed differently. For the purpose of
551 discussion, a controller which uses taskfile interface and all
552 standard callbacks is assumed.
555 Currently 6 ATA command protocols are used. They can be
556 sorted into the following four categories according to how
561 <varlistentry><term>ATA NO DATA or DMA</term>
564 ATA_PROT_NODATA and ATA_PROT_DMA fall into this category.
565 These types of commands don't require any software
566 intervention once issued. Device will raise interrupt on
572 <varlistentry><term>ATA PIO</term>
575 ATA_PROT_PIO is in this category. libata currently
576 implements PIO with polling. ATA_NIEN bit is set to turn
577 off interrupt and pio_task on ata_wq performs polling and
583 <varlistentry><term>ATAPI NODATA or DMA</term>
586 ATA_PROT_ATAPI_NODATA and ATA_PROT_ATAPI_DMA are in this
587 category. packet_task is used to poll BSY bit after
588 issuing PACKET command. Once BSY is turned off by the
589 device, packet_task transfers CDB and hands off processing
590 to interrupt handler.
595 <varlistentry><term>ATAPI PIO</term>
598 ATA_PROT_ATAPI is in this category. ATA_NIEN bit is set
599 and, as in ATAPI NODATA or DMA, packet_task submits cdb.
600 However, after submitting cdb, further processing (data
601 transfer) is handed off to pio_task.
608 <sect1><title>How commands are completed</title>
610 Once issued, all qc's are either completed with
611 ata_qc_complete() or time out. For commands which are handled
612 by interrupts, ata_host_intr() invokes ata_qc_complete(), and,
613 for PIO tasks, pio_task invokes ata_qc_complete(). In error
614 cases, packet_task may also complete commands.
617 ata_qc_complete() does the following.
624 DMA memory is unmapped.
630 ATA_QCFLAG_ACTIVE is clared from qc->flags.
636 qc->complete_fn() callback is invoked. If the return value of
637 the callback is not zero. Completion is short circuited and
638 ata_qc_complete() returns.
644 __ata_qc_complete() is called, which does
649 qc->flags is cleared to zero.
655 ap->active_tag and qc->tag are poisoned.
661 qc->waiting is claread & completed (in that order).
667 qc is deallocated by clearing appropriate bit in ap->qactive.
678 So, it basically notifies upper layer and deallocates qc. One
679 exception is short-circuit path in #3 which is used by
683 For all non-ATAPI commands, whether it fails or not, almost
684 the same code path is taken and very little error handling
685 takes place. A qc is completed with success status if it
686 succeeded, with failed status otherwise.
689 However, failed ATAPI commands require more handling as
690 REQUEST SENSE is needed to acquire sense data. If an ATAPI
691 command fails, ata_qc_complete() is invoked with error status,
692 which in turn invokes atapi_qc_complete() via
693 qc->complete_fn() callback.
696 This makes atapi_qc_complete() set scmd->result to
697 SAM_STAT_CHECK_CONDITION, complete the scmd and return 1. As
698 the sense data is empty but scmd->result is CHECK CONDITION,
699 SCSI midlayer will invoke EH for the scmd, and returning 1
700 makes ata_qc_complete() to return without deallocating the qc.
701 This leads us to ata_scsi_error() with partially completed qc.
706 <sect1><title>ata_scsi_error()</title>
708 ata_scsi_error() is the current transportt->eh_strategy_handler()
709 for libata. As discussed above, this will be entered in two
710 cases - timeout and ATAPI error completion. This function
711 calls low level libata driver's eng_timeout() callback, the
712 standard callback for which is ata_eng_timeout(). It checks
713 if a qc is active and calls ata_qc_timeout() on the qc if so.
714 Actual error handling occurs in ata_qc_timeout().
717 If EH is invoked for timeout, ata_qc_timeout() stops BMDMA and
718 completes the qc. Note that as we're currently in EH, we
719 cannot call scsi_done. As described in SCSI EH doc, a
720 recovered scmd should be either retried with
721 scsi_queue_insert() or finished with scsi_finish_command().
722 Here, we override qc->scsidone with scsi_finish_command() and
723 calls ata_qc_complete().
726 If EH is invoked due to a failed ATAPI qc, the qc here is
727 completed but not deallocated. The purpose of this
728 half-completion is to use the qc as place holder to make EH
729 code reach this place. This is a bit hackish, but it works.
732 Once control reaches here, the qc is deallocated by invoking
733 __ata_qc_complete() explicitly. Then, internal qc for REQUEST
734 SENSE is issued. Once sense data is acquired, scmd is
735 finished by directly invoking scsi_finish_command() on the
736 scmd. Note that as we already have completed and deallocated
737 the qc which was associated with the scmd, we don't need
738 to/cannot call ata_qc_complete() again.
743 <sect1><title>Problems with the current EH</title>
749 Error representation is too crude. Currently any and all
750 error conditions are represented with ATA STATUS and ERROR
751 registers. Errors which aren't ATA device errors are treated
752 as ATA device errors by setting ATA_ERR bit. Better error
753 descriptor which can properly represent ATA and other
754 errors/exceptions is needed.
760 When handling timeouts, no action is taken to make device
761 forget about the timed out command and ready for new commands.
767 EH handling via ata_scsi_error() is not properly protected
768 from usual command processing. On EH entrance, the device is
769 not in quiescent state. Timed out commands may succeed or
770 fail any time. pio_task and atapi_task may still be running.
776 Too weak error recovery. Devices / controllers causing HSM
777 mismatch errors and other errors quite often require reset to
778 return to known state. Also, advanced error handling is
779 necessary to support features like NCQ and hotplug.
785 ATA errors are directly handled in the interrupt handler and
786 PIO errors in pio_task. This is problematic for advanced
787 error handling for the following reasons.
790 First, advanced error handling often requires context and
791 internal qc execution.
794 Second, even a simple failure (say, CRC error) needs
795 information gathering and could trigger complex error handling
796 (say, resetting & reconfiguring). Having multiple code
797 paths to gather information, enter EH and trigger actions
801 Third, scattered EH code makes implementing low level drivers
802 difficult. Low level drivers override libata callbacks. If
803 EH is scattered over several places, each affected callbacks
804 should perform its part of error handling. This can be error
813 <chapter id="libataExt">
814 <title>libata Library</title>
815 !Edrivers/scsi/libata-core.c
818 <chapter id="libataInt">
819 <title>libata Core Internals</title>
820 !Idrivers/scsi/libata-core.c
823 <chapter id="libataScsiInt">
824 <title>libata SCSI translation/emulation</title>
825 !Edrivers/scsi/libata-scsi.c
826 !Idrivers/scsi/libata-scsi.c
829 <chapter id="ataExceptions">
830 <title>ATA errors & exceptions</title>
833 This chapter tries to identify what error/exception conditions exist
834 for ATA/ATAPI devices and describe how they should be handled in
835 implementation-neutral way.
839 The term 'error' is used to describe conditions where either an
840 explicit error condition is reported from device or a command has
845 The term 'exception' is either used to describe exceptional
846 conditions which are not errors (say, power or hotplug events), or
847 to describe both errors and non-error exceptional conditions. Where
848 explicit distinction between error and exception is necessary, the
849 term 'non-error exception' is used.
853 <title>Exception categories</title>
855 Exceptions are described primarily with respect to legacy
856 taskfile + bus master IDE interface. If a controller provides
857 other better mechanism for error reporting, mapping those into
858 categories described below shouldn't be difficult.
862 In the following sections, two recovery actions - reset and
863 reconfiguring transport - are mentioned. These are described
864 further in <xref linkend="exrec"/>.
867 <sect2 id="excatHSMviolation">
868 <title>HSM violation</title>
870 This error is indicated when STATUS value doesn't match HSM
871 requirement during issuing or excution any ATA/ATAPI command.
875 <title>Examples</title>
879 ATA_STATUS doesn't contain !BSY && DRDY && !DRQ while trying
886 !BSY && !DRQ during PIO data transfer.
892 DRQ on command completion.
898 !BSY && ERR after CDB tranfer starts but before the
899 last byte of CDB is transferred. ATA/ATAPI standard states
900 that "The device shall not terminate the PACKET command
901 with an error before the last byte of the command packet has
902 been written" in the error outputs description of PACKET
903 command and the state diagram doesn't include such
911 In these cases, HSM is violated and not much information
912 regarding the error can be acquired from STATUS or ERROR
913 register. IOW, this error can be anything - driver bug,
914 faulty device, controller and/or cable.
918 As HSM is violated, reset is necessary to restore known state.
919 Reconfiguring transport for lower speed might be helpful too
920 as transmission errors sometimes cause this kind of errors.
924 <sect2 id="excatDevErr">
925 <title>ATA/ATAPI device error (non-NCQ / non-CHECK CONDITION)</title>
928 These are errors detected and reported by ATA/ATAPI devices
929 indicating device problems. For this type of errors, STATUS
930 and ERROR register values are valid and describe error
931 condition. Note that some of ATA bus errors are detected by
932 ATA/ATAPI devices and reported using the same mechanism as
933 device errors. Those cases are described later in this
938 For ATA commands, this type of errors are indicated by !BSY
939 && ERR during command execution and on completion.
942 <para>For ATAPI commands,</para>
948 !BSY && ERR && ABRT right after issuing PACKET
949 indicates that PACKET command is not supported and falls in
956 !BSY && ERR(==CHK) && !ABRT after the last
957 byte of CDB is transferred indicates CHECK CONDITION and
958 doesn't fall in this category.
964 !BSY && ERR(==CHK) && ABRT after the last byte
965 of CDB is transferred *probably* indicates CHECK CONDITION and
966 doesn't fall in this category.
973 Of errors detected as above, the followings are not ATA/ATAPI
974 device errors but ATA bus errors and should be handled
975 according to <xref linkend="excatATAbusErr"/>.
981 <term>CRC error during data transfer</term>
984 This is indicated by ICRC bit in the ERROR register and
985 means that corruption occurred during data transfer. Upto
986 ATA/ATAPI-7, the standard specifies that this bit is only
987 applicable to UDMA transfers but ATA/ATAPI-8 draft revision
988 1f says that the bit may be applicable to multiword DMA and
995 <term>ABRT error during data transfer or on completion</term>
998 Upto ATA/ATAPI-7, the standard specifies that ABRT could be
999 set on ICRC errors and on cases where a device is not able
1000 to complete a command. Combined with the fact that MWDMA
1001 and PIO transfer errors aren't allowed to use ICRC bit upto
1002 ATA/ATAPI-7, it seems to imply that ABRT bit alone could
1003 indicate tranfer errors.
1006 However, ATA/ATAPI-8 draft revision 1f removes the part
1007 that ICRC errors can turn on ABRT. So, this is kind of
1008 gray area. Some heuristics are needed here.
1016 ATA/ATAPI device errors can be further categorized as follows.
1022 <term>Media errors</term>
1025 This is indicated by UNC bit in the ERROR register. ATA
1026 devices reports UNC error only after certain number of
1027 retries cannot recover the data, so there's nothing much
1028 else to do other than notifying upper layer.
1031 READ and WRITE commands report CHS or LBA of the first
1032 failed sector but ATA/ATAPI standard specifies that the
1033 amount of transferred data on error completion is
1034 indeterminate, so we cannot assume that sectors preceding
1035 the failed sector have been transferred and thus cannot
1036 complete those sectors successfully as SCSI does.
1042 <term>Media changed / media change requested error</term>
1045 <<TODO: fill here>>
1050 <varlistentry><term>Address error</term>
1053 This is indicated by IDNF bit in the ERROR register.
1054 Report to upper layer.
1059 <varlistentry><term>Other errors</term>
1062 This can be invalid command or parameter indicated by ABRT
1063 ERROR bit or some other error condition. Note that ABRT
1064 bit can indicate a lot of things including ICRC and Address
1065 errors. Heuristics needed.
1073 Depending on commands, not all STATUS/ERROR bits are
1074 applicable. These non-applicable bits are marked with
1075 "na" in the output descriptions but upto ATA/ATAPI-7
1076 no definition of "na" can be found. However,
1077 ATA/ATAPI-8 draft revision 1f describes "N/A" as
1083 <varlistentry><term>3.2.3.3a N/A</term>
1086 A keyword the indicates a field has no defined value in
1087 this standard and should not be checked by the host or
1088 device. N/A fields should be cleared to zero.
1096 So, it seems reasonable to assume that "na" bits are
1097 cleared to zero by devices and thus need no explicit masking.
1102 <sect2 id="excatATAPIcc">
1103 <title>ATAPI device CHECK CONDITION</title>
1106 ATAPI device CHECK CONDITION error is indicated by set CHK bit
1107 (ERR bit) in the STATUS register after the last byte of CDB is
1108 transferred for a PACKET command. For this kind of errors,
1109 sense data should be acquired to gather information regarding
1110 the errors. REQUEST SENSE packet command should be used to
1115 Once sense data is acquired, this type of errors can be
1116 handled similary to other SCSI errors. Note that sense data
1117 may indicate ATA bus error (e.g. Sense Key 04h HARDWARE ERROR
1118 && ASC/ASCQ 47h/00h SCSI PARITY ERROR). In such
1119 cases, the error should be considered as an ATA bus error and
1120 handled according to <xref linkend="excatATAbusErr"/>.
1125 <sect2 id="excatNCQerr">
1126 <title>ATA device error (NCQ)</title>
1129 NCQ command error is indicated by cleared BSY and set ERR bit
1130 during NCQ command phase (one or more NCQ commands
1131 outstanding). Although STATUS and ERROR registers will
1132 contain valid values describing the error, READ LOG EXT is
1133 required to clear the error condition, determine which command
1134 has failed and acquire more information.
1138 READ LOG EXT Log Page 10h reports which tag has failed and
1139 taskfile register values describing the error. With this
1140 information the failed command can be handled as a normal ATA
1141 command error as in <xref linkend="excatDevErr"/> and all
1142 other in-flight commands must be retried. Note that this
1143 retry should not be counted - it's likely that commands
1144 retried this way would have completed normally if it were not
1145 for the failed command.
1149 Note that ATA bus errors can be reported as ATA device NCQ
1150 errors. This should be handled as described in <xref
1151 linkend="excatATAbusErr"/>.
1155 If READ LOG EXT Log Page 10h fails or reports NQ, we're
1156 thoroughly screwed. This condition should be treated
1157 according to <xref linkend="excatHSMviolation"/>.
1162 <sect2 id="excatATAbusErr">
1163 <title>ATA bus error</title>
1166 ATA bus error means that data corruption occurred during
1167 transmission over ATA bus (SATA or PATA). This type of errors
1175 ICRC or ABRT error as described in <xref linkend="excatDevErr"/>.
1181 Controller-specific error completion with error information
1182 indicating transmission error.
1188 On some controllers, command timeout. In this case, there may
1189 be a mechanism to determine that the timeout is due to
1196 Unknown/random errors, timeouts and all sorts of weirdities.
1203 As described above, transmission errors can cause wide variety
1204 of symptoms ranging from device ICRC error to random device
1205 lockup, and, for many cases, there is no way to tell if an
1206 error condition is due to transmission error or not;
1207 therefore, it's necessary to employ some kind of heuristic
1208 when dealing with errors and timeouts. For example,
1209 encountering repetitive ABRT errors for known supported
1210 command is likely to indicate ATA bus error.
1214 Once it's determined that ATA bus errors have possibly
1215 occurred, lowering ATA bus transmission speed is one of
1216 actions which may alleviate the problem. See <xref
1217 linkend="exrecReconf"/> for more information.
1222 <sect2 id="excatPCIbusErr">
1223 <title>PCI bus error</title>
1226 Data corruption or other failures during transmission over PCI
1227 (or other system bus). For standard BMDMA, this is indicated
1228 by Error bit in the BMDMA Status register. This type of
1229 errors must be logged as it indicates something is very wrong
1230 with the system. Resetting host controller is recommended.
1235 <sect2 id="excatLateCompletion">
1236 <title>Late completion</title>
1239 This occurs when timeout occurs and the timeout handler finds
1240 out that the timed out command has completed successfully or
1241 with error. This is usually caused by lost interrupts. This
1242 type of errors must be logged. Resetting host controller is
1248 <sect2 id="excatUnknown">
1249 <title>Unknown error (timeout)</title>
1252 This is when timeout occurs and the command is still
1253 processing or the host and device are in unknown state. When
1254 this occurs, HSM could be in any valid or invalid state. To
1255 bring the device to known state and make it forget about the
1256 timed out command, resetting is necessary. The timed out
1257 command may be retried.
1261 Timeouts can also be caused by transmission errors. Refer to
1262 <xref linkend="excatATAbusErr"/> for more details.
1267 <sect2 id="excatHoplugPM">
1268 <title>Hotplug and power management exceptions</title>
1271 <<TODO: fill here>>
1279 <title>EH recovery actions</title>
1282 This section discusses several important recovery actions.
1285 <sect2 id="exrecClr">
1286 <title>Clearing error condition</title>
1289 Many controllers require its error registers to be cleared by
1290 error handler. Different controllers may have different
1295 For SATA, it's strongly recommended to clear at least SError
1296 register during error handling.
1300 <sect2 id="exrecRst">
1301 <title>Reset</title>
1304 During EH, resetting is necessary in the following cases.
1311 HSM is in unknown or invalid state
1317 HBA is in unknown or invalid state
1323 EH needs to make HBA/device forget about in-flight commands
1329 HBA/device behaves weirdly
1336 Resetting during EH might be a good idea regardless of error
1337 condition to improve EH robustness. Whether to reset both or
1338 either one of HBA and device depends on situation but the
1339 following scheme is recommended.
1346 When it's known that HBA is in ready state but ATA/ATAPI
1347 device in in unknown state, reset only device.
1353 If HBA is in unknown state, reset both HBA and device.
1360 HBA resetting is implementation specific. For a controller
1361 complying to taskfile/BMDMA PCI IDE, stopping active DMA
1362 transaction may be sufficient iff BMDMA state is the only HBA
1363 context. But even mostly taskfile/BMDMA PCI IDE complying
1364 controllers may have implementation specific requirements and
1365 mechanism to reset themselves. This must be addressed by
1370 OTOH, ATA/ATAPI standard describes in detail ways to reset
1376 <varlistentry><term>PATA hardware reset</term>
1379 This is hardware initiated device reset signalled with
1380 asserted PATA RESET- signal. There is no standard way to
1381 initiate hardware reset from software although some
1382 hardware provides registers that allow driver to directly
1383 tweak the RESET- signal.
1388 <varlistentry><term>Software reset</term>
1391 This is achieved by turning CONTROL SRST bit on for at
1392 least 5us. Both PATA and SATA support it but, in case of
1393 SATA, this may require controller-specific support as the
1394 second Register FIS to clear SRST should be transmitted
1395 while BSY bit is still set. Note that on PATA, this resets
1396 both master and slave devices on a channel.
1401 <varlistentry><term>EXECUTE DEVICE DIAGNOSTIC command</term>
1404 Although ATA/ATAPI standard doesn't describe exactly, EDD
1405 implies some level of resetting, possibly similar level
1406 with software reset. Host-side EDD protocol can be handled
1407 with normal command processing and most SATA controllers
1408 should be able to handle EDD's just like other commands.
1409 As in software reset, EDD affects both devices on a PATA
1413 Although EDD does reset devices, this doesn't suit error
1414 handling as EDD cannot be issued while BSY is set and it's
1415 unclear how it will act when device is in unknown/weird
1421 <varlistentry><term>ATAPI DEVICE RESET command</term>
1424 This is very similar to software reset except that reset
1425 can be restricted to the selected device without affecting
1426 the other device sharing the cable.
1431 <varlistentry><term>SATA phy reset</term>
1434 This is the preferred way of resetting a SATA device. In
1435 effect, it's identical to PATA hardware reset. Note that
1436 this can be done with the standard SCR Control register.
1437 As such, it's usually easier to implement than software
1446 One more thing to consider when resetting devices is that
1447 resetting clears certain configuration parameters and they
1448 need to be set to their previous or newly adjusted values
1453 Parameters affected are.
1460 CHS set up with INITIALIZE DEVICE PARAMETERS (seldomly used)
1466 Parameters set with SET FEATURES including transfer mode setting
1472 Block count set with SET MULTIPLE MODE
1478 Other parameters (SET MAX, MEDIA LOCK...)
1485 ATA/ATAPI standard specifies that some parameters must be
1486 maintained across hardware or software reset, but doesn't
1487 strictly specify all of them. Always reconfiguring needed
1488 parameters after reset is required for robustness. Note that
1489 this also applies when resuming from deep sleep (power-off).
1493 Also, ATA/ATAPI standard requires that IDENTIFY DEVICE /
1494 IDENTIFY PACKET DEVICE is issued after any configuration
1495 parameter is updated or a hardware reset and the result used
1496 for further operation. OS driver is required to implement
1497 revalidation mechanism to support this.
1502 <sect2 id="exrecReconf">
1503 <title>Reconfigure transport</title>
1506 For both PATA and SATA, a lot of corners are cut for cheap
1507 connectors, cables or controllers and it's quite common to see
1508 high transmission error rate. This can be mitigated by
1509 lowering transmission speed.
1513 The following is a possible scheme Jeff Garzik suggested.
1518 If more than $N (3?) transmission errors happen in 15 minutes,
1523 if SATA, decrease SATA PHY speed. if speed cannot be decreased,
1528 decrease UDMA xfer speed. if at UDMA0, switch to PIO4,
1533 decrease PIO xfer speed. if at PIO3, complain, but continue
1545 <chapter id="PiixInt">
1546 <title>ata_piix Internals</title>
1547 !Idrivers/scsi/ata_piix.c
1550 <chapter id="SILInt">
1551 <title>sata_sil Internals</title>
1552 !Idrivers/scsi/sata_sil.c
1555 <chapter id="libataThanks">
1556 <title>Thanks</title>
1558 The bulk of the ATA knowledge comes thanks to long conversations with
1559 Andre Hedrick (www.linux-ide.org), and long hours pondering the ATA
1560 and SCSI specifications.
1563 Thanks to Alan Cox for pointing out similarities
1564 between SATA and SCSI, and in general for motivation to hack on
1568 libata's device detection
1569 method, ata_pio_devchk, and in general all the early probing was
1570 based on extensive study of Hale Landis's probe/reset code in his
1571 ATADRVR driver (www.ata-atapi.com).