1*4882a593Smuzhiyun======================== 2*4882a593SmuzhiyunlibATA Developer's Guide 3*4882a593Smuzhiyun======================== 4*4882a593Smuzhiyun 5*4882a593Smuzhiyun:Author: Jeff Garzik 6*4882a593Smuzhiyun 7*4882a593SmuzhiyunIntroduction 8*4882a593Smuzhiyun============ 9*4882a593Smuzhiyun 10*4882a593SmuzhiyunlibATA is a library used inside the Linux kernel to support ATA host 11*4882a593Smuzhiyuncontrollers and devices. libATA provides an ATA driver API, class 12*4882a593Smuzhiyuntransports for ATA and ATAPI devices, and SCSI<->ATA translation for ATA 13*4882a593Smuzhiyundevices according to the T10 SAT specification. 14*4882a593Smuzhiyun 15*4882a593SmuzhiyunThis Guide documents the libATA driver API, library functions, library 16*4882a593Smuzhiyuninternals, and a couple sample ATA low-level drivers. 17*4882a593Smuzhiyun 18*4882a593Smuzhiyunlibata Driver API 19*4882a593Smuzhiyun================= 20*4882a593Smuzhiyun 21*4882a593Smuzhiyun:c:type:`struct ata_port_operations <ata_port_operations>` 22*4882a593Smuzhiyunis defined for every low-level libata 23*4882a593Smuzhiyunhardware driver, and it controls how the low-level driver interfaces 24*4882a593Smuzhiyunwith the ATA and SCSI layers. 25*4882a593Smuzhiyun 26*4882a593SmuzhiyunFIS-based drivers will hook into the system with ``->qc_prep()`` and 27*4882a593Smuzhiyun``->qc_issue()`` high-level hooks. Hardware which behaves in a manner 28*4882a593Smuzhiyunsimilar to PCI IDE hardware may utilize several generic helpers, 29*4882a593Smuzhiyundefining at a bare minimum the bus I/O addresses of the ATA shadow 30*4882a593Smuzhiyunregister blocks. 31*4882a593Smuzhiyun 32*4882a593Smuzhiyun:c:type:`struct ata_port_operations <ata_port_operations>` 33*4882a593Smuzhiyun---------------------------------------------------------- 34*4882a593Smuzhiyun 35*4882a593SmuzhiyunDisable ATA port 36*4882a593Smuzhiyun~~~~~~~~~~~~~~~~ 37*4882a593Smuzhiyun 38*4882a593Smuzhiyun:: 39*4882a593Smuzhiyun 40*4882a593Smuzhiyun void (*port_disable) (struct ata_port *); 41*4882a593Smuzhiyun 42*4882a593Smuzhiyun 43*4882a593SmuzhiyunCalled from :c:func:`ata_bus_probe` error path, as well as when unregistering 44*4882a593Smuzhiyunfrom the SCSI module (rmmod, hot unplug). This function should do 45*4882a593Smuzhiyunwhatever needs to be done to take the port out of use. In most cases, 46*4882a593Smuzhiyun:c:func:`ata_port_disable` can be used as this hook. 47*4882a593Smuzhiyun 48*4882a593SmuzhiyunCalled from :c:func:`ata_bus_probe` on a failed probe. Called from 49*4882a593Smuzhiyun:c:func:`ata_scsi_release`. 50*4882a593Smuzhiyun 51*4882a593SmuzhiyunPost-IDENTIFY device configuration 52*4882a593Smuzhiyun~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 53*4882a593Smuzhiyun 54*4882a593Smuzhiyun:: 55*4882a593Smuzhiyun 56*4882a593Smuzhiyun void (*dev_config) (struct ata_port *, struct ata_device *); 57*4882a593Smuzhiyun 58*4882a593Smuzhiyun 59*4882a593SmuzhiyunCalled after IDENTIFY [PACKET] DEVICE is issued to each device found. 60*4882a593SmuzhiyunTypically used to apply device-specific fixups prior to issue of SET 61*4882a593SmuzhiyunFEATURES - XFER MODE, and prior to operation. 62*4882a593Smuzhiyun 63*4882a593SmuzhiyunThis entry may be specified as NULL in ata_port_operations. 64*4882a593Smuzhiyun 65*4882a593SmuzhiyunSet PIO/DMA mode 66*4882a593Smuzhiyun~~~~~~~~~~~~~~~~ 67*4882a593Smuzhiyun 68*4882a593Smuzhiyun:: 69*4882a593Smuzhiyun 70*4882a593Smuzhiyun void (*set_piomode) (struct ata_port *, struct ata_device *); 71*4882a593Smuzhiyun void (*set_dmamode) (struct ata_port *, struct ata_device *); 72*4882a593Smuzhiyun void (*post_set_mode) (struct ata_port *); 73*4882a593Smuzhiyun unsigned int (*mode_filter) (struct ata_port *, struct ata_device *, unsigned int); 74*4882a593Smuzhiyun 75*4882a593Smuzhiyun 76*4882a593SmuzhiyunHooks called prior to the issue of SET FEATURES - XFER MODE command. The 77*4882a593Smuzhiyunoptional ``->mode_filter()`` hook is called when libata has built a mask of 78*4882a593Smuzhiyunthe possible modes. This is passed to the ``->mode_filter()`` function 79*4882a593Smuzhiyunwhich should return a mask of valid modes after filtering those 80*4882a593Smuzhiyununsuitable due to hardware limits. It is not valid to use this interface 81*4882a593Smuzhiyunto add modes. 82*4882a593Smuzhiyun 83*4882a593Smuzhiyun``dev->pio_mode`` and ``dev->dma_mode`` are guaranteed to be valid when 84*4882a593Smuzhiyun``->set_piomode()`` and when ``->set_dmamode()`` is called. The timings for 85*4882a593Smuzhiyunany other drive sharing the cable will also be valid at this point. That 86*4882a593Smuzhiyunis the library records the decisions for the modes of each drive on a 87*4882a593Smuzhiyunchannel before it attempts to set any of them. 88*4882a593Smuzhiyun 89*4882a593Smuzhiyun``->post_set_mode()`` is called unconditionally, after the SET FEATURES - 90*4882a593SmuzhiyunXFER MODE command completes successfully. 91*4882a593Smuzhiyun 92*4882a593Smuzhiyun``->set_piomode()`` is always called (if present), but ``->set_dma_mode()`` 93*4882a593Smuzhiyunis only called if DMA is possible. 94*4882a593Smuzhiyun 95*4882a593SmuzhiyunTaskfile read/write 96*4882a593Smuzhiyun~~~~~~~~~~~~~~~~~~~ 97*4882a593Smuzhiyun 98*4882a593Smuzhiyun:: 99*4882a593Smuzhiyun 100*4882a593Smuzhiyun void (*sff_tf_load) (struct ata_port *ap, struct ata_taskfile *tf); 101*4882a593Smuzhiyun void (*sff_tf_read) (struct ata_port *ap, struct ata_taskfile *tf); 102*4882a593Smuzhiyun 103*4882a593Smuzhiyun 104*4882a593Smuzhiyun``->tf_load()`` is called to load the given taskfile into hardware 105*4882a593Smuzhiyunregisters / DMA buffers. ``->tf_read()`` is called to read the hardware 106*4882a593Smuzhiyunregisters / DMA buffers, to obtain the current set of taskfile register 107*4882a593Smuzhiyunvalues. Most drivers for taskfile-based hardware (PIO or MMIO) use 108*4882a593Smuzhiyun:c:func:`ata_sff_tf_load` and :c:func:`ata_sff_tf_read` for these hooks. 109*4882a593Smuzhiyun 110*4882a593SmuzhiyunPIO data read/write 111*4882a593Smuzhiyun~~~~~~~~~~~~~~~~~~~ 112*4882a593Smuzhiyun 113*4882a593Smuzhiyun:: 114*4882a593Smuzhiyun 115*4882a593Smuzhiyun void (*sff_data_xfer) (struct ata_device *, unsigned char *, unsigned int, int); 116*4882a593Smuzhiyun 117*4882a593Smuzhiyun 118*4882a593SmuzhiyunAll bmdma-style drivers must implement this hook. This is the low-level 119*4882a593Smuzhiyunoperation that actually copies the data bytes during a PIO data 120*4882a593Smuzhiyuntransfer. Typically the driver will choose one of 121*4882a593Smuzhiyun:c:func:`ata_sff_data_xfer`, or :c:func:`ata_sff_data_xfer32`. 122*4882a593Smuzhiyun 123*4882a593SmuzhiyunATA command execute 124*4882a593Smuzhiyun~~~~~~~~~~~~~~~~~~~ 125*4882a593Smuzhiyun 126*4882a593Smuzhiyun:: 127*4882a593Smuzhiyun 128*4882a593Smuzhiyun void (*sff_exec_command)(struct ata_port *ap, struct ata_taskfile *tf); 129*4882a593Smuzhiyun 130*4882a593Smuzhiyun 131*4882a593Smuzhiyuncauses an ATA command, previously loaded with ``->tf_load()``, to be 132*4882a593Smuzhiyuninitiated in hardware. Most drivers for taskfile-based hardware use 133*4882a593Smuzhiyun:c:func:`ata_sff_exec_command` for this hook. 134*4882a593Smuzhiyun 135*4882a593SmuzhiyunPer-cmd ATAPI DMA capabilities filter 136*4882a593Smuzhiyun~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 137*4882a593Smuzhiyun 138*4882a593Smuzhiyun:: 139*4882a593Smuzhiyun 140*4882a593Smuzhiyun int (*check_atapi_dma) (struct ata_queued_cmd *qc); 141*4882a593Smuzhiyun 142*4882a593Smuzhiyun 143*4882a593SmuzhiyunAllow low-level driver to filter ATA PACKET commands, returning a status 144*4882a593Smuzhiyunindicating whether or not it is OK to use DMA for the supplied PACKET 145*4882a593Smuzhiyuncommand. 146*4882a593Smuzhiyun 147*4882a593SmuzhiyunThis hook may be specified as NULL, in which case libata will assume 148*4882a593Smuzhiyunthat atapi dma can be supported. 149*4882a593Smuzhiyun 150*4882a593SmuzhiyunRead specific ATA shadow registers 151*4882a593Smuzhiyun~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 152*4882a593Smuzhiyun 153*4882a593Smuzhiyun:: 154*4882a593Smuzhiyun 155*4882a593Smuzhiyun u8 (*sff_check_status)(struct ata_port *ap); 156*4882a593Smuzhiyun u8 (*sff_check_altstatus)(struct ata_port *ap); 157*4882a593Smuzhiyun 158*4882a593Smuzhiyun 159*4882a593SmuzhiyunReads the Status/AltStatus ATA shadow register from hardware. On some 160*4882a593Smuzhiyunhardware, reading the Status register has the side effect of clearing 161*4882a593Smuzhiyunthe interrupt condition. Most drivers for taskfile-based hardware use 162*4882a593Smuzhiyun:c:func:`ata_sff_check_status` for this hook. 163*4882a593Smuzhiyun 164*4882a593SmuzhiyunWrite specific ATA shadow register 165*4882a593Smuzhiyun~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 166*4882a593Smuzhiyun 167*4882a593Smuzhiyun:: 168*4882a593Smuzhiyun 169*4882a593Smuzhiyun void (*sff_set_devctl)(struct ata_port *ap, u8 ctl); 170*4882a593Smuzhiyun 171*4882a593Smuzhiyun 172*4882a593SmuzhiyunWrite the device control ATA shadow register to the hardware. Most 173*4882a593Smuzhiyundrivers don't need to define this. 174*4882a593Smuzhiyun 175*4882a593SmuzhiyunSelect ATA device on bus 176*4882a593Smuzhiyun~~~~~~~~~~~~~~~~~~~~~~~~ 177*4882a593Smuzhiyun 178*4882a593Smuzhiyun:: 179*4882a593Smuzhiyun 180*4882a593Smuzhiyun void (*sff_dev_select)(struct ata_port *ap, unsigned int device); 181*4882a593Smuzhiyun 182*4882a593Smuzhiyun 183*4882a593SmuzhiyunIssues the low-level hardware command(s) that causes one of N hardware 184*4882a593Smuzhiyundevices to be considered 'selected' (active and available for use) on 185*4882a593Smuzhiyunthe ATA bus. This generally has no meaning on FIS-based devices. 186*4882a593Smuzhiyun 187*4882a593SmuzhiyunMost drivers for taskfile-based hardware use :c:func:`ata_sff_dev_select` for 188*4882a593Smuzhiyunthis hook. 189*4882a593Smuzhiyun 190*4882a593SmuzhiyunPrivate tuning method 191*4882a593Smuzhiyun~~~~~~~~~~~~~~~~~~~~~ 192*4882a593Smuzhiyun 193*4882a593Smuzhiyun:: 194*4882a593Smuzhiyun 195*4882a593Smuzhiyun void (*set_mode) (struct ata_port *ap); 196*4882a593Smuzhiyun 197*4882a593Smuzhiyun 198*4882a593SmuzhiyunBy default libata performs drive and controller tuning in accordance 199*4882a593Smuzhiyunwith the ATA timing rules and also applies blacklists and cable limits. 200*4882a593SmuzhiyunSome controllers need special handling and have custom tuning rules, 201*4882a593Smuzhiyuntypically raid controllers that use ATA commands but do not actually do 202*4882a593Smuzhiyundrive timing. 203*4882a593Smuzhiyun 204*4882a593Smuzhiyun **Warning** 205*4882a593Smuzhiyun 206*4882a593Smuzhiyun This hook should not be used to replace the standard controller 207*4882a593Smuzhiyun tuning logic when a controller has quirks. Replacing the default 208*4882a593Smuzhiyun tuning logic in that case would bypass handling for drive and bridge 209*4882a593Smuzhiyun quirks that may be important to data reliability. If a controller 210*4882a593Smuzhiyun needs to filter the mode selection it should use the mode_filter 211*4882a593Smuzhiyun hook instead. 212*4882a593Smuzhiyun 213*4882a593SmuzhiyunControl PCI IDE BMDMA engine 214*4882a593Smuzhiyun~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 215*4882a593Smuzhiyun 216*4882a593Smuzhiyun:: 217*4882a593Smuzhiyun 218*4882a593Smuzhiyun void (*bmdma_setup) (struct ata_queued_cmd *qc); 219*4882a593Smuzhiyun void (*bmdma_start) (struct ata_queued_cmd *qc); 220*4882a593Smuzhiyun void (*bmdma_stop) (struct ata_port *ap); 221*4882a593Smuzhiyun u8 (*bmdma_status) (struct ata_port *ap); 222*4882a593Smuzhiyun 223*4882a593Smuzhiyun 224*4882a593SmuzhiyunWhen setting up an IDE BMDMA transaction, these hooks arm 225*4882a593Smuzhiyun(``->bmdma_setup``), fire (``->bmdma_start``), and halt (``->bmdma_stop``) the 226*4882a593Smuzhiyunhardware's DMA engine. ``->bmdma_status`` is used to read the standard PCI 227*4882a593SmuzhiyunIDE DMA Status register. 228*4882a593Smuzhiyun 229*4882a593SmuzhiyunThese hooks are typically either no-ops, or simply not implemented, in 230*4882a593SmuzhiyunFIS-based drivers. 231*4882a593Smuzhiyun 232*4882a593SmuzhiyunMost legacy IDE drivers use :c:func:`ata_bmdma_setup` for the 233*4882a593Smuzhiyun:c:func:`bmdma_setup` hook. :c:func:`ata_bmdma_setup` will write the pointer 234*4882a593Smuzhiyunto the PRD table to the IDE PRD Table Address register, enable DMA in the DMA 235*4882a593SmuzhiyunCommand register, and call :c:func:`exec_command` to begin the transfer. 236*4882a593Smuzhiyun 237*4882a593SmuzhiyunMost legacy IDE drivers use :c:func:`ata_bmdma_start` for the 238*4882a593Smuzhiyun:c:func:`bmdma_start` hook. :c:func:`ata_bmdma_start` will write the 239*4882a593SmuzhiyunATA_DMA_START flag to the DMA Command register. 240*4882a593Smuzhiyun 241*4882a593SmuzhiyunMany legacy IDE drivers use :c:func:`ata_bmdma_stop` for the 242*4882a593Smuzhiyun:c:func:`bmdma_stop` hook. :c:func:`ata_bmdma_stop` clears the ATA_DMA_START 243*4882a593Smuzhiyunflag in the DMA command register. 244*4882a593Smuzhiyun 245*4882a593SmuzhiyunMany legacy IDE drivers use :c:func:`ata_bmdma_status` as the 246*4882a593Smuzhiyun:c:func:`bmdma_status` hook. 247*4882a593Smuzhiyun 248*4882a593SmuzhiyunHigh-level taskfile hooks 249*4882a593Smuzhiyun~~~~~~~~~~~~~~~~~~~~~~~~~ 250*4882a593Smuzhiyun 251*4882a593Smuzhiyun:: 252*4882a593Smuzhiyun 253*4882a593Smuzhiyun enum ata_completion_errors (*qc_prep) (struct ata_queued_cmd *qc); 254*4882a593Smuzhiyun int (*qc_issue) (struct ata_queued_cmd *qc); 255*4882a593Smuzhiyun 256*4882a593Smuzhiyun 257*4882a593SmuzhiyunHigher-level hooks, these two hooks can potentially supersede several of 258*4882a593Smuzhiyunthe above taskfile/DMA engine hooks. ``->qc_prep`` is called after the 259*4882a593Smuzhiyunbuffers have been DMA-mapped, and is typically used to populate the 260*4882a593Smuzhiyunhardware's DMA scatter-gather table. Some drivers use the standard 261*4882a593Smuzhiyun:c:func:`ata_bmdma_qc_prep` and :c:func:`ata_bmdma_dumb_qc_prep` helper 262*4882a593Smuzhiyunfunctions, but more advanced drivers roll their own. 263*4882a593Smuzhiyun 264*4882a593Smuzhiyun``->qc_issue`` is used to make a command active, once the hardware and S/G 265*4882a593Smuzhiyuntables have been prepared. IDE BMDMA drivers use the helper function 266*4882a593Smuzhiyun:c:func:`ata_sff_qc_issue` for taskfile protocol-based dispatch. More 267*4882a593Smuzhiyunadvanced drivers implement their own ``->qc_issue``. 268*4882a593Smuzhiyun 269*4882a593Smuzhiyun:c:func:`ata_sff_qc_issue` calls ``->sff_tf_load()``, ``->bmdma_setup()``, and 270*4882a593Smuzhiyun``->bmdma_start()`` as necessary to initiate a transfer. 271*4882a593Smuzhiyun 272*4882a593SmuzhiyunException and probe handling (EH) 273*4882a593Smuzhiyun~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 274*4882a593Smuzhiyun 275*4882a593Smuzhiyun:: 276*4882a593Smuzhiyun 277*4882a593Smuzhiyun void (*eng_timeout) (struct ata_port *ap); 278*4882a593Smuzhiyun void (*phy_reset) (struct ata_port *ap); 279*4882a593Smuzhiyun 280*4882a593Smuzhiyun 281*4882a593SmuzhiyunDeprecated. Use ``->error_handler()`` instead. 282*4882a593Smuzhiyun 283*4882a593Smuzhiyun:: 284*4882a593Smuzhiyun 285*4882a593Smuzhiyun void (*freeze) (struct ata_port *ap); 286*4882a593Smuzhiyun void (*thaw) (struct ata_port *ap); 287*4882a593Smuzhiyun 288*4882a593Smuzhiyun 289*4882a593Smuzhiyun:c:func:`ata_port_freeze` is called when HSM violations or some other 290*4882a593Smuzhiyuncondition disrupts normal operation of the port. A frozen port is not 291*4882a593Smuzhiyunallowed to perform any operation until the port is thawed, which usually 292*4882a593Smuzhiyunfollows a successful reset. 293*4882a593Smuzhiyun 294*4882a593SmuzhiyunThe optional ``->freeze()`` callback can be used for freezing the port 295*4882a593Smuzhiyunhardware-wise (e.g. mask interrupt and stop DMA engine). If a port 296*4882a593Smuzhiyuncannot be frozen hardware-wise, the interrupt handler must ack and clear 297*4882a593Smuzhiyuninterrupts unconditionally while the port is frozen. 298*4882a593Smuzhiyun 299*4882a593SmuzhiyunThe optional ``->thaw()`` callback is called to perform the opposite of 300*4882a593Smuzhiyun``->freeze()``: prepare the port for normal operation once again. Unmask 301*4882a593Smuzhiyuninterrupts, start DMA engine, etc. 302*4882a593Smuzhiyun 303*4882a593Smuzhiyun:: 304*4882a593Smuzhiyun 305*4882a593Smuzhiyun void (*error_handler) (struct ata_port *ap); 306*4882a593Smuzhiyun 307*4882a593Smuzhiyun 308*4882a593Smuzhiyun``->error_handler()`` is a driver's hook into probe, hotplug, and recovery 309*4882a593Smuzhiyunand other exceptional conditions. The primary responsibility of an 310*4882a593Smuzhiyunimplementation is to call :c:func:`ata_do_eh` or :c:func:`ata_bmdma_drive_eh` 311*4882a593Smuzhiyunwith a set of EH hooks as arguments: 312*4882a593Smuzhiyun 313*4882a593Smuzhiyun'prereset' hook (may be NULL) is called during an EH reset, before any 314*4882a593Smuzhiyunother actions are taken. 315*4882a593Smuzhiyun 316*4882a593Smuzhiyun'postreset' hook (may be NULL) is called after the EH reset is 317*4882a593Smuzhiyunperformed. Based on existing conditions, severity of the problem, and 318*4882a593Smuzhiyunhardware capabilities, 319*4882a593Smuzhiyun 320*4882a593SmuzhiyunEither 'softreset' (may be NULL) or 'hardreset' (may be NULL) will be 321*4882a593Smuzhiyuncalled to perform the low-level EH reset. 322*4882a593Smuzhiyun 323*4882a593Smuzhiyun:: 324*4882a593Smuzhiyun 325*4882a593Smuzhiyun void (*post_internal_cmd) (struct ata_queued_cmd *qc); 326*4882a593Smuzhiyun 327*4882a593Smuzhiyun 328*4882a593SmuzhiyunPerform any hardware-specific actions necessary to finish processing 329*4882a593Smuzhiyunafter executing a probe-time or EH-time command via 330*4882a593Smuzhiyun:c:func:`ata_exec_internal`. 331*4882a593Smuzhiyun 332*4882a593SmuzhiyunHardware interrupt handling 333*4882a593Smuzhiyun~~~~~~~~~~~~~~~~~~~~~~~~~~~ 334*4882a593Smuzhiyun 335*4882a593Smuzhiyun:: 336*4882a593Smuzhiyun 337*4882a593Smuzhiyun irqreturn_t (*irq_handler)(int, void *, struct pt_regs *); 338*4882a593Smuzhiyun void (*irq_clear) (struct ata_port *); 339*4882a593Smuzhiyun 340*4882a593Smuzhiyun 341*4882a593Smuzhiyun``->irq_handler`` is the interrupt handling routine registered with the 342*4882a593Smuzhiyunsystem, by libata. ``->irq_clear`` is called during probe just before the 343*4882a593Smuzhiyuninterrupt handler is registered, to be sure hardware is quiet. 344*4882a593Smuzhiyun 345*4882a593SmuzhiyunThe second argument, dev_instance, should be cast to a pointer to 346*4882a593Smuzhiyun:c:type:`struct ata_host_set <ata_host_set>`. 347*4882a593Smuzhiyun 348*4882a593SmuzhiyunMost legacy IDE drivers use :c:func:`ata_sff_interrupt` for the irq_handler 349*4882a593Smuzhiyunhook, which scans all ports in the host_set, determines which queued 350*4882a593Smuzhiyuncommand was active (if any), and calls ata_sff_host_intr(ap,qc). 351*4882a593Smuzhiyun 352*4882a593SmuzhiyunMost legacy IDE drivers use :c:func:`ata_sff_irq_clear` for the 353*4882a593Smuzhiyun:c:func:`irq_clear` hook, which simply clears the interrupt and error flags 354*4882a593Smuzhiyunin the DMA status register. 355*4882a593Smuzhiyun 356*4882a593SmuzhiyunSATA phy read/write 357*4882a593Smuzhiyun~~~~~~~~~~~~~~~~~~~ 358*4882a593Smuzhiyun 359*4882a593Smuzhiyun:: 360*4882a593Smuzhiyun 361*4882a593Smuzhiyun int (*scr_read) (struct ata_port *ap, unsigned int sc_reg, 362*4882a593Smuzhiyun u32 *val); 363*4882a593Smuzhiyun int (*scr_write) (struct ata_port *ap, unsigned int sc_reg, 364*4882a593Smuzhiyun u32 val); 365*4882a593Smuzhiyun 366*4882a593Smuzhiyun 367*4882a593SmuzhiyunRead and write standard SATA phy registers. Currently only used if 368*4882a593Smuzhiyun``->phy_reset`` hook called the :c:func:`sata_phy_reset` helper function. 369*4882a593Smuzhiyunsc_reg is one of SCR_STATUS, SCR_CONTROL, SCR_ERROR, or SCR_ACTIVE. 370*4882a593Smuzhiyun 371*4882a593SmuzhiyunInit and shutdown 372*4882a593Smuzhiyun~~~~~~~~~~~~~~~~~ 373*4882a593Smuzhiyun 374*4882a593Smuzhiyun:: 375*4882a593Smuzhiyun 376*4882a593Smuzhiyun int (*port_start) (struct ata_port *ap); 377*4882a593Smuzhiyun void (*port_stop) (struct ata_port *ap); 378*4882a593Smuzhiyun void (*host_stop) (struct ata_host_set *host_set); 379*4882a593Smuzhiyun 380*4882a593Smuzhiyun 381*4882a593Smuzhiyun``->port_start()`` is called just after the data structures for each port 382*4882a593Smuzhiyunare initialized. Typically this is used to alloc per-port DMA buffers / 383*4882a593Smuzhiyuntables / rings, enable DMA engines, and similar tasks. Some drivers also 384*4882a593Smuzhiyunuse this entry point as a chance to allocate driver-private memory for 385*4882a593Smuzhiyun``ap->private_data``. 386*4882a593Smuzhiyun 387*4882a593SmuzhiyunMany drivers use :c:func:`ata_port_start` as this hook or call it from their 388*4882a593Smuzhiyunown :c:func:`port_start` hooks. :c:func:`ata_port_start` allocates space for 389*4882a593Smuzhiyuna legacy IDE PRD table and returns. 390*4882a593Smuzhiyun 391*4882a593Smuzhiyun``->port_stop()`` is called after ``->host_stop()``. Its sole function is to 392*4882a593Smuzhiyunrelease DMA/memory resources, now that they are no longer actively being 393*4882a593Smuzhiyunused. Many drivers also free driver-private data from port at this time. 394*4882a593Smuzhiyun 395*4882a593Smuzhiyun``->host_stop()`` is called after all ``->port_stop()`` calls have completed. 396*4882a593SmuzhiyunThe hook must finalize hardware shutdown, release DMA and other 397*4882a593Smuzhiyunresources, etc. This hook may be specified as NULL, in which case it is 398*4882a593Smuzhiyunnot called. 399*4882a593Smuzhiyun 400*4882a593SmuzhiyunError handling 401*4882a593Smuzhiyun============== 402*4882a593Smuzhiyun 403*4882a593SmuzhiyunThis chapter describes how errors are handled under libata. Readers are 404*4882a593Smuzhiyunadvised to read SCSI EH (Documentation/scsi/scsi_eh.rst) and ATA 405*4882a593Smuzhiyunexceptions doc first. 406*4882a593Smuzhiyun 407*4882a593SmuzhiyunOrigins of commands 408*4882a593Smuzhiyun------------------- 409*4882a593Smuzhiyun 410*4882a593SmuzhiyunIn libata, a command is represented with 411*4882a593Smuzhiyun:c:type:`struct ata_queued_cmd <ata_queued_cmd>` or qc. 412*4882a593Smuzhiyunqc's are preallocated during port initialization and repetitively used 413*4882a593Smuzhiyunfor command executions. Currently only one qc is allocated per port but 414*4882a593Smuzhiyunyet-to-be-merged NCQ branch allocates one for each tag and maps each qc 415*4882a593Smuzhiyunto NCQ tag 1-to-1. 416*4882a593Smuzhiyun 417*4882a593Smuzhiyunlibata commands can originate from two sources - libata itself and SCSI 418*4882a593Smuzhiyunmidlayer. libata internal commands are used for initialization and error 419*4882a593Smuzhiyunhandling. All normal blk requests and commands for SCSI emulation are 420*4882a593Smuzhiyunpassed as SCSI commands through queuecommand callback of SCSI host 421*4882a593Smuzhiyuntemplate. 422*4882a593Smuzhiyun 423*4882a593SmuzhiyunHow commands are issued 424*4882a593Smuzhiyun----------------------- 425*4882a593Smuzhiyun 426*4882a593SmuzhiyunInternal commands 427*4882a593Smuzhiyun First, qc is allocated and initialized using :c:func:`ata_qc_new_init`. 428*4882a593Smuzhiyun Although :c:func:`ata_qc_new_init` doesn't implement any wait or retry 429*4882a593Smuzhiyun mechanism when qc is not available, internal commands are currently 430*4882a593Smuzhiyun issued only during initialization and error recovery, so no other 431*4882a593Smuzhiyun command is active and allocation is guaranteed to succeed. 432*4882a593Smuzhiyun 433*4882a593Smuzhiyun Once allocated qc's taskfile is initialized for the command to be 434*4882a593Smuzhiyun executed. qc currently has two mechanisms to notify completion. One 435*4882a593Smuzhiyun is via ``qc->complete_fn()`` callback and the other is completion 436*4882a593Smuzhiyun ``qc->waiting``. ``qc->complete_fn()`` callback is the asynchronous path 437*4882a593Smuzhiyun used by normal SCSI translated commands and ``qc->waiting`` is the 438*4882a593Smuzhiyun synchronous (issuer sleeps in process context) path used by internal 439*4882a593Smuzhiyun commands. 440*4882a593Smuzhiyun 441*4882a593Smuzhiyun Once initialization is complete, host_set lock is acquired and the 442*4882a593Smuzhiyun qc is issued. 443*4882a593Smuzhiyun 444*4882a593SmuzhiyunSCSI commands 445*4882a593Smuzhiyun All libata drivers use :c:func:`ata_scsi_queuecmd` as 446*4882a593Smuzhiyun ``hostt->queuecommand`` callback. scmds can either be simulated or 447*4882a593Smuzhiyun translated. No qc is involved in processing a simulated scmd. The 448*4882a593Smuzhiyun result is computed right away and the scmd is completed. 449*4882a593Smuzhiyun 450*4882a593Smuzhiyun For a translated scmd, :c:func:`ata_qc_new_init` is invoked to allocate a 451*4882a593Smuzhiyun qc and the scmd is translated into the qc. SCSI midlayer's 452*4882a593Smuzhiyun completion notification function pointer is stored into 453*4882a593Smuzhiyun ``qc->scsidone``. 454*4882a593Smuzhiyun 455*4882a593Smuzhiyun ``qc->complete_fn()`` callback is used for completion notification. ATA 456*4882a593Smuzhiyun commands use :c:func:`ata_scsi_qc_complete` while ATAPI commands use 457*4882a593Smuzhiyun :c:func:`atapi_qc_complete`. Both functions end up calling ``qc->scsidone`` 458*4882a593Smuzhiyun to notify upper layer when the qc is finished. After translation is 459*4882a593Smuzhiyun completed, the qc is issued with :c:func:`ata_qc_issue`. 460*4882a593Smuzhiyun 461*4882a593Smuzhiyun Note that SCSI midlayer invokes hostt->queuecommand while holding 462*4882a593Smuzhiyun host_set lock, so all above occur while holding host_set lock. 463*4882a593Smuzhiyun 464*4882a593SmuzhiyunHow commands are processed 465*4882a593Smuzhiyun-------------------------- 466*4882a593Smuzhiyun 467*4882a593SmuzhiyunDepending on which protocol and which controller are used, commands are 468*4882a593Smuzhiyunprocessed differently. For the purpose of discussion, a controller which 469*4882a593Smuzhiyunuses taskfile interface and all standard callbacks is assumed. 470*4882a593Smuzhiyun 471*4882a593SmuzhiyunCurrently 6 ATA command protocols are used. They can be sorted into the 472*4882a593Smuzhiyunfollowing four categories according to how they are processed. 473*4882a593Smuzhiyun 474*4882a593SmuzhiyunATA NO DATA or DMA 475*4882a593Smuzhiyun ATA_PROT_NODATA and ATA_PROT_DMA fall into this category. These 476*4882a593Smuzhiyun types of commands don't require any software intervention once 477*4882a593Smuzhiyun issued. Device will raise interrupt on completion. 478*4882a593Smuzhiyun 479*4882a593SmuzhiyunATA PIO 480*4882a593Smuzhiyun ATA_PROT_PIO is in this category. libata currently implements PIO 481*4882a593Smuzhiyun with polling. ATA_NIEN bit is set to turn off interrupt and 482*4882a593Smuzhiyun pio_task on ata_wq performs polling and IO. 483*4882a593Smuzhiyun 484*4882a593SmuzhiyunATAPI NODATA or DMA 485*4882a593Smuzhiyun ATA_PROT_ATAPI_NODATA and ATA_PROT_ATAPI_DMA are in this 486*4882a593Smuzhiyun category. packet_task is used to poll BSY bit after issuing PACKET 487*4882a593Smuzhiyun command. Once BSY is turned off by the device, packet_task 488*4882a593Smuzhiyun transfers CDB and hands off processing to interrupt handler. 489*4882a593Smuzhiyun 490*4882a593SmuzhiyunATAPI PIO 491*4882a593Smuzhiyun ATA_PROT_ATAPI is in this category. ATA_NIEN bit is set and, as 492*4882a593Smuzhiyun in ATAPI NODATA or DMA, packet_task submits cdb. However, after 493*4882a593Smuzhiyun submitting cdb, further processing (data transfer) is handed off to 494*4882a593Smuzhiyun pio_task. 495*4882a593Smuzhiyun 496*4882a593SmuzhiyunHow commands are completed 497*4882a593Smuzhiyun-------------------------- 498*4882a593Smuzhiyun 499*4882a593SmuzhiyunOnce issued, all qc's are either completed with :c:func:`ata_qc_complete` or 500*4882a593Smuzhiyuntime out. For commands which are handled by interrupts, 501*4882a593Smuzhiyun:c:func:`ata_host_intr` invokes :c:func:`ata_qc_complete`, and, for PIO tasks, 502*4882a593Smuzhiyunpio_task invokes :c:func:`ata_qc_complete`. In error cases, packet_task may 503*4882a593Smuzhiyunalso complete commands. 504*4882a593Smuzhiyun 505*4882a593Smuzhiyun:c:func:`ata_qc_complete` does the following. 506*4882a593Smuzhiyun 507*4882a593Smuzhiyun1. DMA memory is unmapped. 508*4882a593Smuzhiyun 509*4882a593Smuzhiyun2. ATA_QCFLAG_ACTIVE is cleared from qc->flags. 510*4882a593Smuzhiyun 511*4882a593Smuzhiyun3. :c:expr:`qc->complete_fn` callback is invoked. If the return value of the 512*4882a593Smuzhiyun callback is not zero. Completion is short circuited and 513*4882a593Smuzhiyun :c:func:`ata_qc_complete` returns. 514*4882a593Smuzhiyun 515*4882a593Smuzhiyun4. :c:func:`__ata_qc_complete` is called, which does 516*4882a593Smuzhiyun 517*4882a593Smuzhiyun 1. ``qc->flags`` is cleared to zero. 518*4882a593Smuzhiyun 519*4882a593Smuzhiyun 2. ``ap->active_tag`` and ``qc->tag`` are poisoned. 520*4882a593Smuzhiyun 521*4882a593Smuzhiyun 3. ``qc->waiting`` is cleared & completed (in that order). 522*4882a593Smuzhiyun 523*4882a593Smuzhiyun 4. qc is deallocated by clearing appropriate bit in ``ap->qactive``. 524*4882a593Smuzhiyun 525*4882a593SmuzhiyunSo, it basically notifies upper layer and deallocates qc. One exception 526*4882a593Smuzhiyunis short-circuit path in #3 which is used by :c:func:`atapi_qc_complete`. 527*4882a593Smuzhiyun 528*4882a593SmuzhiyunFor all non-ATAPI commands, whether it fails or not, almost the same 529*4882a593Smuzhiyuncode path is taken and very little error handling takes place. A qc is 530*4882a593Smuzhiyuncompleted with success status if it succeeded, with failed status 531*4882a593Smuzhiyunotherwise. 532*4882a593Smuzhiyun 533*4882a593SmuzhiyunHowever, failed ATAPI commands require more handling as REQUEST SENSE is 534*4882a593Smuzhiyunneeded to acquire sense data. If an ATAPI command fails, 535*4882a593Smuzhiyun:c:func:`ata_qc_complete` is invoked with error status, which in turn invokes 536*4882a593Smuzhiyun:c:func:`atapi_qc_complete` via ``qc->complete_fn()`` callback. 537*4882a593Smuzhiyun 538*4882a593SmuzhiyunThis makes :c:func:`atapi_qc_complete` set ``scmd->result`` to 539*4882a593SmuzhiyunSAM_STAT_CHECK_CONDITION, complete the scmd and return 1. As the 540*4882a593Smuzhiyunsense data is empty but ``scmd->result`` is CHECK CONDITION, SCSI midlayer 541*4882a593Smuzhiyunwill invoke EH for the scmd, and returning 1 makes :c:func:`ata_qc_complete` 542*4882a593Smuzhiyunto return without deallocating the qc. This leads us to 543*4882a593Smuzhiyun:c:func:`ata_scsi_error` with partially completed qc. 544*4882a593Smuzhiyun 545*4882a593Smuzhiyun:c:func:`ata_scsi_error` 546*4882a593Smuzhiyun------------------------ 547*4882a593Smuzhiyun 548*4882a593Smuzhiyun:c:func:`ata_scsi_error` is the current ``transportt->eh_strategy_handler()`` 549*4882a593Smuzhiyunfor libata. As discussed above, this will be entered in two cases - 550*4882a593Smuzhiyuntimeout and ATAPI error completion. This function calls low level libata 551*4882a593Smuzhiyundriver's :c:func:`eng_timeout` callback, the standard callback for which is 552*4882a593Smuzhiyun:c:func:`ata_eng_timeout`. It checks if a qc is active and calls 553*4882a593Smuzhiyun:c:func:`ata_qc_timeout` on the qc if so. Actual error handling occurs in 554*4882a593Smuzhiyun:c:func:`ata_qc_timeout`. 555*4882a593Smuzhiyun 556*4882a593SmuzhiyunIf EH is invoked for timeout, :c:func:`ata_qc_timeout` stops BMDMA and 557*4882a593Smuzhiyuncompletes the qc. Note that as we're currently in EH, we cannot call 558*4882a593Smuzhiyunscsi_done. As described in SCSI EH doc, a recovered scmd should be 559*4882a593Smuzhiyuneither retried with :c:func:`scsi_queue_insert` or finished with 560*4882a593Smuzhiyun:c:func:`scsi_finish_command`. Here, we override ``qc->scsidone`` with 561*4882a593Smuzhiyun:c:func:`scsi_finish_command` and calls :c:func:`ata_qc_complete`. 562*4882a593Smuzhiyun 563*4882a593SmuzhiyunIf EH is invoked due to a failed ATAPI qc, the qc here is completed but 564*4882a593Smuzhiyunnot deallocated. The purpose of this half-completion is to use the qc as 565*4882a593Smuzhiyunplace holder to make EH code reach this place. This is a bit hackish, 566*4882a593Smuzhiyunbut it works. 567*4882a593Smuzhiyun 568*4882a593SmuzhiyunOnce control reaches here, the qc is deallocated by invoking 569*4882a593Smuzhiyun:c:func:`__ata_qc_complete` explicitly. Then, internal qc for REQUEST SENSE 570*4882a593Smuzhiyunis issued. Once sense data is acquired, scmd is finished by directly 571*4882a593Smuzhiyuninvoking :c:func:`scsi_finish_command` on the scmd. Note that as we already 572*4882a593Smuzhiyunhave completed and deallocated the qc which was associated with the 573*4882a593Smuzhiyunscmd, we don't need to/cannot call :c:func:`ata_qc_complete` again. 574*4882a593Smuzhiyun 575*4882a593SmuzhiyunProblems with the current EH 576*4882a593Smuzhiyun---------------------------- 577*4882a593Smuzhiyun 578*4882a593Smuzhiyun- Error representation is too crude. Currently any and all error 579*4882a593Smuzhiyun conditions are represented with ATA STATUS and ERROR registers. 580*4882a593Smuzhiyun Errors which aren't ATA device errors are treated as ATA device 581*4882a593Smuzhiyun errors by setting ATA_ERR bit. Better error descriptor which can 582*4882a593Smuzhiyun properly represent ATA and other errors/exceptions is needed. 583*4882a593Smuzhiyun 584*4882a593Smuzhiyun- When handling timeouts, no action is taken to make device forget 585*4882a593Smuzhiyun about the timed out command and ready for new commands. 586*4882a593Smuzhiyun 587*4882a593Smuzhiyun- EH handling via :c:func:`ata_scsi_error` is not properly protected from 588*4882a593Smuzhiyun usual command processing. On EH entrance, the device is not in 589*4882a593Smuzhiyun quiescent state. Timed out commands may succeed or fail any time. 590*4882a593Smuzhiyun pio_task and atapi_task may still be running. 591*4882a593Smuzhiyun 592*4882a593Smuzhiyun- Too weak error recovery. Devices / controllers causing HSM mismatch 593*4882a593Smuzhiyun errors and other errors quite often require reset to return to known 594*4882a593Smuzhiyun state. Also, advanced error handling is necessary to support features 595*4882a593Smuzhiyun like NCQ and hotplug. 596*4882a593Smuzhiyun 597*4882a593Smuzhiyun- ATA errors are directly handled in the interrupt handler and PIO 598*4882a593Smuzhiyun errors in pio_task. This is problematic for advanced error handling 599*4882a593Smuzhiyun for the following reasons. 600*4882a593Smuzhiyun 601*4882a593Smuzhiyun First, advanced error handling often requires context and internal qc 602*4882a593Smuzhiyun execution. 603*4882a593Smuzhiyun 604*4882a593Smuzhiyun Second, even a simple failure (say, CRC error) needs information 605*4882a593Smuzhiyun gathering and could trigger complex error handling (say, resetting & 606*4882a593Smuzhiyun reconfiguring). Having multiple code paths to gather information, 607*4882a593Smuzhiyun enter EH and trigger actions makes life painful. 608*4882a593Smuzhiyun 609*4882a593Smuzhiyun Third, scattered EH code makes implementing low level drivers 610*4882a593Smuzhiyun difficult. Low level drivers override libata callbacks. If EH is 611*4882a593Smuzhiyun scattered over several places, each affected callbacks should perform 612*4882a593Smuzhiyun its part of error handling. This can be error prone and painful. 613*4882a593Smuzhiyun 614*4882a593Smuzhiyunlibata Library 615*4882a593Smuzhiyun============== 616*4882a593Smuzhiyun 617*4882a593Smuzhiyun.. kernel-doc:: drivers/ata/libata-core.c 618*4882a593Smuzhiyun :export: 619*4882a593Smuzhiyun 620*4882a593Smuzhiyunlibata Core Internals 621*4882a593Smuzhiyun===================== 622*4882a593Smuzhiyun 623*4882a593Smuzhiyun.. kernel-doc:: drivers/ata/libata-core.c 624*4882a593Smuzhiyun :internal: 625*4882a593Smuzhiyun 626*4882a593Smuzhiyun.. kernel-doc:: drivers/ata/libata-eh.c 627*4882a593Smuzhiyun 628*4882a593Smuzhiyunlibata SCSI translation/emulation 629*4882a593Smuzhiyun================================= 630*4882a593Smuzhiyun 631*4882a593Smuzhiyun.. kernel-doc:: drivers/ata/libata-scsi.c 632*4882a593Smuzhiyun :export: 633*4882a593Smuzhiyun 634*4882a593Smuzhiyun.. kernel-doc:: drivers/ata/libata-scsi.c 635*4882a593Smuzhiyun :internal: 636*4882a593Smuzhiyun 637*4882a593SmuzhiyunATA errors and exceptions 638*4882a593Smuzhiyun========================= 639*4882a593Smuzhiyun 640*4882a593SmuzhiyunThis chapter tries to identify what error/exception conditions exist for 641*4882a593SmuzhiyunATA/ATAPI devices and describe how they should be handled in 642*4882a593Smuzhiyunimplementation-neutral way. 643*4882a593Smuzhiyun 644*4882a593SmuzhiyunThe term 'error' is used to describe conditions where either an explicit 645*4882a593Smuzhiyunerror condition is reported from device or a command has timed out. 646*4882a593Smuzhiyun 647*4882a593SmuzhiyunThe term 'exception' is either used to describe exceptional conditions 648*4882a593Smuzhiyunwhich are not errors (say, power or hotplug events), or to describe both 649*4882a593Smuzhiyunerrors and non-error exceptional conditions. Where explicit distinction 650*4882a593Smuzhiyunbetween error and exception is necessary, the term 'non-error exception' 651*4882a593Smuzhiyunis used. 652*4882a593Smuzhiyun 653*4882a593SmuzhiyunException categories 654*4882a593Smuzhiyun-------------------- 655*4882a593Smuzhiyun 656*4882a593SmuzhiyunExceptions are described primarily with respect to legacy taskfile + bus 657*4882a593Smuzhiyunmaster IDE interface. If a controller provides other better mechanism 658*4882a593Smuzhiyunfor error reporting, mapping those into categories described below 659*4882a593Smuzhiyunshouldn't be difficult. 660*4882a593Smuzhiyun 661*4882a593SmuzhiyunIn the following sections, two recovery actions - reset and 662*4882a593Smuzhiyunreconfiguring transport - are mentioned. These are described further in 663*4882a593Smuzhiyun`EH recovery actions <#exrec>`__. 664*4882a593Smuzhiyun 665*4882a593SmuzhiyunHSM violation 666*4882a593Smuzhiyun~~~~~~~~~~~~~ 667*4882a593Smuzhiyun 668*4882a593SmuzhiyunThis error is indicated when STATUS value doesn't match HSM requirement 669*4882a593Smuzhiyunduring issuing or execution any ATA/ATAPI command. 670*4882a593Smuzhiyun 671*4882a593Smuzhiyun- ATA_STATUS doesn't contain !BSY && DRDY && !DRQ while trying to 672*4882a593Smuzhiyun issue a command. 673*4882a593Smuzhiyun 674*4882a593Smuzhiyun- !BSY && !DRQ during PIO data transfer. 675*4882a593Smuzhiyun 676*4882a593Smuzhiyun- DRQ on command completion. 677*4882a593Smuzhiyun 678*4882a593Smuzhiyun- !BSY && ERR after CDB transfer starts but before the last byte of CDB 679*4882a593Smuzhiyun is transferred. ATA/ATAPI standard states that "The device shall not 680*4882a593Smuzhiyun terminate the PACKET command with an error before the last byte of 681*4882a593Smuzhiyun the command packet has been written" in the error outputs description 682*4882a593Smuzhiyun of PACKET command and the state diagram doesn't include such 683*4882a593Smuzhiyun transitions. 684*4882a593Smuzhiyun 685*4882a593SmuzhiyunIn these cases, HSM is violated and not much information regarding the 686*4882a593Smuzhiyunerror can be acquired from STATUS or ERROR register. IOW, this error can 687*4882a593Smuzhiyunbe anything - driver bug, faulty device, controller and/or cable. 688*4882a593Smuzhiyun 689*4882a593SmuzhiyunAs HSM is violated, reset is necessary to restore known state. 690*4882a593SmuzhiyunReconfiguring transport for lower speed might be helpful too as 691*4882a593Smuzhiyuntransmission errors sometimes cause this kind of errors. 692*4882a593Smuzhiyun 693*4882a593SmuzhiyunATA/ATAPI device error (non-NCQ / non-CHECK CONDITION) 694*4882a593Smuzhiyun~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 695*4882a593Smuzhiyun 696*4882a593SmuzhiyunThese are errors detected and reported by ATA/ATAPI devices indicating 697*4882a593Smuzhiyundevice problems. For this type of errors, STATUS and ERROR register 698*4882a593Smuzhiyunvalues are valid and describe error condition. Note that some of ATA bus 699*4882a593Smuzhiyunerrors are detected by ATA/ATAPI devices and reported using the same 700*4882a593Smuzhiyunmechanism as device errors. Those cases are described later in this 701*4882a593Smuzhiyunsection. 702*4882a593Smuzhiyun 703*4882a593SmuzhiyunFor ATA commands, this type of errors are indicated by !BSY && ERR 704*4882a593Smuzhiyunduring command execution and on completion. 705*4882a593Smuzhiyun 706*4882a593SmuzhiyunFor ATAPI commands, 707*4882a593Smuzhiyun 708*4882a593Smuzhiyun- !BSY && ERR && ABRT right after issuing PACKET indicates that PACKET 709*4882a593Smuzhiyun command is not supported and falls in this category. 710*4882a593Smuzhiyun 711*4882a593Smuzhiyun- !BSY && ERR(==CHK) && !ABRT after the last byte of CDB is transferred 712*4882a593Smuzhiyun indicates CHECK CONDITION and doesn't fall in this category. 713*4882a593Smuzhiyun 714*4882a593Smuzhiyun- !BSY && ERR(==CHK) && ABRT after the last byte of CDB is transferred 715*4882a593Smuzhiyun \*probably\* indicates CHECK CONDITION and doesn't fall in this 716*4882a593Smuzhiyun category. 717*4882a593Smuzhiyun 718*4882a593SmuzhiyunOf errors detected as above, the following are not ATA/ATAPI device 719*4882a593Smuzhiyunerrors but ATA bus errors and should be handled according to 720*4882a593Smuzhiyun`ATA bus error <#excatATAbusErr>`__. 721*4882a593Smuzhiyun 722*4882a593SmuzhiyunCRC error during data transfer 723*4882a593Smuzhiyun This is indicated by ICRC bit in the ERROR register and means that 724*4882a593Smuzhiyun corruption occurred during data transfer. Up to ATA/ATAPI-7, the 725*4882a593Smuzhiyun standard specifies that this bit is only applicable to UDMA 726*4882a593Smuzhiyun transfers but ATA/ATAPI-8 draft revision 1f says that the bit may be 727*4882a593Smuzhiyun applicable to multiword DMA and PIO. 728*4882a593Smuzhiyun 729*4882a593SmuzhiyunABRT error during data transfer or on completion 730*4882a593Smuzhiyun Up to ATA/ATAPI-7, the standard specifies that ABRT could be set on 731*4882a593Smuzhiyun ICRC errors and on cases where a device is not able to complete a 732*4882a593Smuzhiyun command. Combined with the fact that MWDMA and PIO transfer errors 733*4882a593Smuzhiyun aren't allowed to use ICRC bit up to ATA/ATAPI-7, it seems to imply 734*4882a593Smuzhiyun that ABRT bit alone could indicate transfer errors. 735*4882a593Smuzhiyun 736*4882a593Smuzhiyun However, ATA/ATAPI-8 draft revision 1f removes the part that ICRC 737*4882a593Smuzhiyun errors can turn on ABRT. So, this is kind of gray area. Some 738*4882a593Smuzhiyun heuristics are needed here. 739*4882a593Smuzhiyun 740*4882a593SmuzhiyunATA/ATAPI device errors can be further categorized as follows. 741*4882a593Smuzhiyun 742*4882a593SmuzhiyunMedia errors 743*4882a593Smuzhiyun This is indicated by UNC bit in the ERROR register. ATA devices 744*4882a593Smuzhiyun reports UNC error only after certain number of retries cannot 745*4882a593Smuzhiyun recover the data, so there's nothing much else to do other than 746*4882a593Smuzhiyun notifying upper layer. 747*4882a593Smuzhiyun 748*4882a593Smuzhiyun READ and WRITE commands report CHS or LBA of the first failed sector 749*4882a593Smuzhiyun but ATA/ATAPI standard specifies that the amount of transferred data 750*4882a593Smuzhiyun on error completion is indeterminate, so we cannot assume that 751*4882a593Smuzhiyun sectors preceding the failed sector have been transferred and thus 752*4882a593Smuzhiyun cannot complete those sectors successfully as SCSI does. 753*4882a593Smuzhiyun 754*4882a593SmuzhiyunMedia changed / media change requested error 755*4882a593Smuzhiyun <<TODO: fill here>> 756*4882a593Smuzhiyun 757*4882a593SmuzhiyunAddress error 758*4882a593Smuzhiyun This is indicated by IDNF bit in the ERROR register. Report to upper 759*4882a593Smuzhiyun layer. 760*4882a593Smuzhiyun 761*4882a593SmuzhiyunOther errors 762*4882a593Smuzhiyun This can be invalid command or parameter indicated by ABRT ERROR bit 763*4882a593Smuzhiyun or some other error condition. Note that ABRT bit can indicate a lot 764*4882a593Smuzhiyun of things including ICRC and Address errors. Heuristics needed. 765*4882a593Smuzhiyun 766*4882a593SmuzhiyunDepending on commands, not all STATUS/ERROR bits are applicable. These 767*4882a593Smuzhiyunnon-applicable bits are marked with "na" in the output descriptions but 768*4882a593Smuzhiyunup to ATA/ATAPI-7 no definition of "na" can be found. However, 769*4882a593SmuzhiyunATA/ATAPI-8 draft revision 1f describes "N/A" as follows. 770*4882a593Smuzhiyun 771*4882a593Smuzhiyun 3.2.3.3a N/A 772*4882a593Smuzhiyun A keyword the indicates a field has no defined value in this 773*4882a593Smuzhiyun standard and should not be checked by the host or device. N/A 774*4882a593Smuzhiyun fields should be cleared to zero. 775*4882a593Smuzhiyun 776*4882a593SmuzhiyunSo, it seems reasonable to assume that "na" bits are cleared to zero by 777*4882a593Smuzhiyundevices and thus need no explicit masking. 778*4882a593Smuzhiyun 779*4882a593SmuzhiyunATAPI device CHECK CONDITION 780*4882a593Smuzhiyun~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 781*4882a593Smuzhiyun 782*4882a593SmuzhiyunATAPI device CHECK CONDITION error is indicated by set CHK bit (ERR bit) 783*4882a593Smuzhiyunin the STATUS register after the last byte of CDB is transferred for a 784*4882a593SmuzhiyunPACKET command. For this kind of errors, sense data should be acquired 785*4882a593Smuzhiyunto gather information regarding the errors. REQUEST SENSE packet command 786*4882a593Smuzhiyunshould be used to acquire sense data. 787*4882a593Smuzhiyun 788*4882a593SmuzhiyunOnce sense data is acquired, this type of errors can be handled 789*4882a593Smuzhiyunsimilarly to other SCSI errors. Note that sense data may indicate ATA 790*4882a593Smuzhiyunbus error (e.g. Sense Key 04h HARDWARE ERROR && ASC/ASCQ 47h/00h SCSI 791*4882a593SmuzhiyunPARITY ERROR). In such cases, the error should be considered as an ATA 792*4882a593Smuzhiyunbus error and handled according to `ATA bus error <#excatATAbusErr>`__. 793*4882a593Smuzhiyun 794*4882a593SmuzhiyunATA device error (NCQ) 795*4882a593Smuzhiyun~~~~~~~~~~~~~~~~~~~~~~ 796*4882a593Smuzhiyun 797*4882a593SmuzhiyunNCQ command error is indicated by cleared BSY and set ERR bit during NCQ 798*4882a593Smuzhiyuncommand phase (one or more NCQ commands outstanding). Although STATUS 799*4882a593Smuzhiyunand ERROR registers will contain valid values describing the error, READ 800*4882a593SmuzhiyunLOG EXT is required to clear the error condition, determine which 801*4882a593Smuzhiyuncommand has failed and acquire more information. 802*4882a593Smuzhiyun 803*4882a593SmuzhiyunREAD LOG EXT Log Page 10h reports which tag has failed and taskfile 804*4882a593Smuzhiyunregister values describing the error. With this information the failed 805*4882a593Smuzhiyuncommand can be handled as a normal ATA command error as in 806*4882a593Smuzhiyun`ATA/ATAPI device error (non-NCQ / non-CHECK CONDITION) <#excatDevErr>`__ 807*4882a593Smuzhiyunand all other in-flight commands must be retried. Note that this retry 808*4882a593Smuzhiyunshould not be counted - it's likely that commands retried this way would 809*4882a593Smuzhiyunhave completed normally if it were not for the failed command. 810*4882a593Smuzhiyun 811*4882a593SmuzhiyunNote that ATA bus errors can be reported as ATA device NCQ errors. This 812*4882a593Smuzhiyunshould be handled as described in `ATA bus error <#excatATAbusErr>`__. 813*4882a593Smuzhiyun 814*4882a593SmuzhiyunIf READ LOG EXT Log Page 10h fails or reports NQ, we're thoroughly 815*4882a593Smuzhiyunscrewed. This condition should be treated according to 816*4882a593Smuzhiyun`HSM violation <#excatHSMviolation>`__. 817*4882a593Smuzhiyun 818*4882a593SmuzhiyunATA bus error 819*4882a593Smuzhiyun~~~~~~~~~~~~~ 820*4882a593Smuzhiyun 821*4882a593SmuzhiyunATA bus error means that data corruption occurred during transmission 822*4882a593Smuzhiyunover ATA bus (SATA or PATA). This type of errors can be indicated by 823*4882a593Smuzhiyun 824*4882a593Smuzhiyun- ICRC or ABRT error as described in 825*4882a593Smuzhiyun `ATA/ATAPI device error (non-NCQ / non-CHECK CONDITION) <#excatDevErr>`__. 826*4882a593Smuzhiyun 827*4882a593Smuzhiyun- Controller-specific error completion with error information 828*4882a593Smuzhiyun indicating transmission error. 829*4882a593Smuzhiyun 830*4882a593Smuzhiyun- On some controllers, command timeout. In this case, there may be a 831*4882a593Smuzhiyun mechanism to determine that the timeout is due to transmission error. 832*4882a593Smuzhiyun 833*4882a593Smuzhiyun- Unknown/random errors, timeouts and all sorts of weirdities. 834*4882a593Smuzhiyun 835*4882a593SmuzhiyunAs described above, transmission errors can cause wide variety of 836*4882a593Smuzhiyunsymptoms ranging from device ICRC error to random device lockup, and, 837*4882a593Smuzhiyunfor many cases, there is no way to tell if an error condition is due to 838*4882a593Smuzhiyuntransmission error or not; therefore, it's necessary to employ some kind 839*4882a593Smuzhiyunof heuristic when dealing with errors and timeouts. For example, 840*4882a593Smuzhiyunencountering repetitive ABRT errors for known supported command is 841*4882a593Smuzhiyunlikely to indicate ATA bus error. 842*4882a593Smuzhiyun 843*4882a593SmuzhiyunOnce it's determined that ATA bus errors have possibly occurred, 844*4882a593Smuzhiyunlowering ATA bus transmission speed is one of actions which may 845*4882a593Smuzhiyunalleviate the problem. See `Reconfigure transport <#exrecReconf>`__ for 846*4882a593Smuzhiyunmore information. 847*4882a593Smuzhiyun 848*4882a593SmuzhiyunPCI bus error 849*4882a593Smuzhiyun~~~~~~~~~~~~~ 850*4882a593Smuzhiyun 851*4882a593SmuzhiyunData corruption or other failures during transmission over PCI (or other 852*4882a593Smuzhiyunsystem bus). For standard BMDMA, this is indicated by Error bit in the 853*4882a593SmuzhiyunBMDMA Status register. This type of errors must be logged as it 854*4882a593Smuzhiyunindicates something is very wrong with the system. Resetting host 855*4882a593Smuzhiyuncontroller is recommended. 856*4882a593Smuzhiyun 857*4882a593SmuzhiyunLate completion 858*4882a593Smuzhiyun~~~~~~~~~~~~~~~ 859*4882a593Smuzhiyun 860*4882a593SmuzhiyunThis occurs when timeout occurs and the timeout handler finds out that 861*4882a593Smuzhiyunthe timed out command has completed successfully or with error. This is 862*4882a593Smuzhiyunusually caused by lost interrupts. This type of errors must be logged. 863*4882a593SmuzhiyunResetting host controller is recommended. 864*4882a593Smuzhiyun 865*4882a593SmuzhiyunUnknown error (timeout) 866*4882a593Smuzhiyun~~~~~~~~~~~~~~~~~~~~~~~ 867*4882a593Smuzhiyun 868*4882a593SmuzhiyunThis is when timeout occurs and the command is still processing or the 869*4882a593Smuzhiyunhost and device are in unknown state. When this occurs, HSM could be in 870*4882a593Smuzhiyunany valid or invalid state. To bring the device to known state and make 871*4882a593Smuzhiyunit forget about the timed out command, resetting is necessary. The timed 872*4882a593Smuzhiyunout command may be retried. 873*4882a593Smuzhiyun 874*4882a593SmuzhiyunTimeouts can also be caused by transmission errors. Refer to 875*4882a593Smuzhiyun`ATA bus error <#excatATAbusErr>`__ for more details. 876*4882a593Smuzhiyun 877*4882a593SmuzhiyunHotplug and power management exceptions 878*4882a593Smuzhiyun~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 879*4882a593Smuzhiyun 880*4882a593Smuzhiyun<<TODO: fill here>> 881*4882a593Smuzhiyun 882*4882a593SmuzhiyunEH recovery actions 883*4882a593Smuzhiyun------------------- 884*4882a593Smuzhiyun 885*4882a593SmuzhiyunThis section discusses several important recovery actions. 886*4882a593Smuzhiyun 887*4882a593SmuzhiyunClearing error condition 888*4882a593Smuzhiyun~~~~~~~~~~~~~~~~~~~~~~~~ 889*4882a593Smuzhiyun 890*4882a593SmuzhiyunMany controllers require its error registers to be cleared by error 891*4882a593Smuzhiyunhandler. Different controllers may have different requirements. 892*4882a593Smuzhiyun 893*4882a593SmuzhiyunFor SATA, it's strongly recommended to clear at least SError register 894*4882a593Smuzhiyunduring error handling. 895*4882a593Smuzhiyun 896*4882a593SmuzhiyunReset 897*4882a593Smuzhiyun~~~~~ 898*4882a593Smuzhiyun 899*4882a593SmuzhiyunDuring EH, resetting is necessary in the following cases. 900*4882a593Smuzhiyun 901*4882a593Smuzhiyun- HSM is in unknown or invalid state 902*4882a593Smuzhiyun 903*4882a593Smuzhiyun- HBA is in unknown or invalid state 904*4882a593Smuzhiyun 905*4882a593Smuzhiyun- EH needs to make HBA/device forget about in-flight commands 906*4882a593Smuzhiyun 907*4882a593Smuzhiyun- HBA/device behaves weirdly 908*4882a593Smuzhiyun 909*4882a593SmuzhiyunResetting during EH might be a good idea regardless of error condition 910*4882a593Smuzhiyunto improve EH robustness. Whether to reset both or either one of HBA and 911*4882a593Smuzhiyundevice depends on situation but the following scheme is recommended. 912*4882a593Smuzhiyun 913*4882a593Smuzhiyun- When it's known that HBA is in ready state but ATA/ATAPI device is in 914*4882a593Smuzhiyun unknown state, reset only device. 915*4882a593Smuzhiyun 916*4882a593Smuzhiyun- If HBA is in unknown state, reset both HBA and device. 917*4882a593Smuzhiyun 918*4882a593SmuzhiyunHBA resetting is implementation specific. For a controller complying to 919*4882a593Smuzhiyuntaskfile/BMDMA PCI IDE, stopping active DMA transaction may be 920*4882a593Smuzhiyunsufficient iff BMDMA state is the only HBA context. But even mostly 921*4882a593Smuzhiyuntaskfile/BMDMA PCI IDE complying controllers may have implementation 922*4882a593Smuzhiyunspecific requirements and mechanism to reset themselves. This must be 923*4882a593Smuzhiyunaddressed by specific drivers. 924*4882a593Smuzhiyun 925*4882a593SmuzhiyunOTOH, ATA/ATAPI standard describes in detail ways to reset ATA/ATAPI 926*4882a593Smuzhiyundevices. 927*4882a593Smuzhiyun 928*4882a593SmuzhiyunPATA hardware reset 929*4882a593Smuzhiyun This is hardware initiated device reset signalled with asserted PATA 930*4882a593Smuzhiyun RESET- signal. There is no standard way to initiate hardware reset 931*4882a593Smuzhiyun from software although some hardware provides registers that allow 932*4882a593Smuzhiyun driver to directly tweak the RESET- signal. 933*4882a593Smuzhiyun 934*4882a593SmuzhiyunSoftware reset 935*4882a593Smuzhiyun This is achieved by turning CONTROL SRST bit on for at least 5us. 936*4882a593Smuzhiyun Both PATA and SATA support it but, in case of SATA, this may require 937*4882a593Smuzhiyun controller-specific support as the second Register FIS to clear SRST 938*4882a593Smuzhiyun should be transmitted while BSY bit is still set. Note that on PATA, 939*4882a593Smuzhiyun this resets both master and slave devices on a channel. 940*4882a593Smuzhiyun 941*4882a593SmuzhiyunEXECUTE DEVICE DIAGNOSTIC command 942*4882a593Smuzhiyun Although ATA/ATAPI standard doesn't describe exactly, EDD implies 943*4882a593Smuzhiyun some level of resetting, possibly similar level with software reset. 944*4882a593Smuzhiyun Host-side EDD protocol can be handled with normal command processing 945*4882a593Smuzhiyun and most SATA controllers should be able to handle EDD's just like 946*4882a593Smuzhiyun other commands. As in software reset, EDD affects both devices on a 947*4882a593Smuzhiyun PATA bus. 948*4882a593Smuzhiyun 949*4882a593Smuzhiyun Although EDD does reset devices, this doesn't suit error handling as 950*4882a593Smuzhiyun EDD cannot be issued while BSY is set and it's unclear how it will 951*4882a593Smuzhiyun act when device is in unknown/weird state. 952*4882a593Smuzhiyun 953*4882a593SmuzhiyunATAPI DEVICE RESET command 954*4882a593Smuzhiyun This is very similar to software reset except that reset can be 955*4882a593Smuzhiyun restricted to the selected device without affecting the other device 956*4882a593Smuzhiyun sharing the cable. 957*4882a593Smuzhiyun 958*4882a593SmuzhiyunSATA phy reset 959*4882a593Smuzhiyun This is the preferred way of resetting a SATA device. In effect, 960*4882a593Smuzhiyun it's identical to PATA hardware reset. Note that this can be done 961*4882a593Smuzhiyun with the standard SCR Control register. As such, it's usually easier 962*4882a593Smuzhiyun to implement than software reset. 963*4882a593Smuzhiyun 964*4882a593SmuzhiyunOne more thing to consider when resetting devices is that resetting 965*4882a593Smuzhiyunclears certain configuration parameters and they need to be set to their 966*4882a593Smuzhiyunprevious or newly adjusted values after reset. 967*4882a593Smuzhiyun 968*4882a593SmuzhiyunParameters affected are. 969*4882a593Smuzhiyun 970*4882a593Smuzhiyun- CHS set up with INITIALIZE DEVICE PARAMETERS (seldom used) 971*4882a593Smuzhiyun 972*4882a593Smuzhiyun- Parameters set with SET FEATURES including transfer mode setting 973*4882a593Smuzhiyun 974*4882a593Smuzhiyun- Block count set with SET MULTIPLE MODE 975*4882a593Smuzhiyun 976*4882a593Smuzhiyun- Other parameters (SET MAX, MEDIA LOCK...) 977*4882a593Smuzhiyun 978*4882a593SmuzhiyunATA/ATAPI standard specifies that some parameters must be maintained 979*4882a593Smuzhiyunacross hardware or software reset, but doesn't strictly specify all of 980*4882a593Smuzhiyunthem. Always reconfiguring needed parameters after reset is required for 981*4882a593Smuzhiyunrobustness. Note that this also applies when resuming from deep sleep 982*4882a593Smuzhiyun(power-off). 983*4882a593Smuzhiyun 984*4882a593SmuzhiyunAlso, ATA/ATAPI standard requires that IDENTIFY DEVICE / IDENTIFY PACKET 985*4882a593SmuzhiyunDEVICE is issued after any configuration parameter is updated or a 986*4882a593Smuzhiyunhardware reset and the result used for further operation. OS driver is 987*4882a593Smuzhiyunrequired to implement revalidation mechanism to support this. 988*4882a593Smuzhiyun 989*4882a593SmuzhiyunReconfigure transport 990*4882a593Smuzhiyun~~~~~~~~~~~~~~~~~~~~~ 991*4882a593Smuzhiyun 992*4882a593SmuzhiyunFor both PATA and SATA, a lot of corners are cut for cheap connectors, 993*4882a593Smuzhiyuncables or controllers and it's quite common to see high transmission 994*4882a593Smuzhiyunerror rate. This can be mitigated by lowering transmission speed. 995*4882a593Smuzhiyun 996*4882a593SmuzhiyunThe following is a possible scheme Jeff Garzik suggested. 997*4882a593Smuzhiyun 998*4882a593Smuzhiyun If more than $N (3?) transmission errors happen in 15 minutes, 999*4882a593Smuzhiyun 1000*4882a593Smuzhiyun - if SATA, decrease SATA PHY speed. if speed cannot be decreased, 1001*4882a593Smuzhiyun 1002*4882a593Smuzhiyun - decrease UDMA xfer speed. if at UDMA0, switch to PIO4, 1003*4882a593Smuzhiyun 1004*4882a593Smuzhiyun - decrease PIO xfer speed. if at PIO3, complain, but continue 1005*4882a593Smuzhiyun 1006*4882a593Smuzhiyunata_piix Internals 1007*4882a593Smuzhiyun=================== 1008*4882a593Smuzhiyun 1009*4882a593Smuzhiyun.. kernel-doc:: drivers/ata/ata_piix.c 1010*4882a593Smuzhiyun :internal: 1011*4882a593Smuzhiyun 1012*4882a593Smuzhiyunsata_sil Internals 1013*4882a593Smuzhiyun=================== 1014*4882a593Smuzhiyun 1015*4882a593Smuzhiyun.. kernel-doc:: drivers/ata/sata_sil.c 1016*4882a593Smuzhiyun :internal: 1017*4882a593Smuzhiyun 1018*4882a593SmuzhiyunThanks 1019*4882a593Smuzhiyun====== 1020*4882a593Smuzhiyun 1021*4882a593SmuzhiyunThe bulk of the ATA knowledge comes thanks to long conversations with 1022*4882a593SmuzhiyunAndre Hedrick (www.linux-ide.org), and long hours pondering the ATA and 1023*4882a593SmuzhiyunSCSI specifications. 1024*4882a593Smuzhiyun 1025*4882a593SmuzhiyunThanks to Alan Cox for pointing out similarities between SATA and SCSI, 1026*4882a593Smuzhiyunand in general for motivation to hack on libata. 1027*4882a593Smuzhiyun 1028*4882a593Smuzhiyunlibata's device detection method, ata_pio_devchk, and in general all 1029*4882a593Smuzhiyunthe early probing was based on extensive study of Hale Landis's 1030*4882a593Smuzhiyunprobe/reset code in his ATADRVR driver (www.ata-atapi.com). 1031