Documentation/driver-api/libata.rst GitHub 원문 ↗

Linux 6.18.37 · Driver API

libATA Developer's Guide

libATA low-level operation hooks, queued-command lifecycle, SCSI translation, EH error taxonomy와 reset·transport recovery를 설명합니다.

Source pathDocumentation/driver-api/libata.rst
Source versionLinux v6.18.37
TranslationDUJINLABS 전문 번역 + 해설

요약·해설과 원문, 전문 번역을 서로 분리했습니다. API 이름, symbol, source path는 원문 표기를 사용합니다.

1. 요약·해설

원문의 핵심 논리와 kernel programming 관점의 보충 설명입니다. 아래의 전문 번역과는 별도로 작성했습니다.

요약과 해설

libata.rst:1-1000

libATA는 SCSI midlayer request를 ATA·ATAPI protocol로 변환하고 `ata_port_operations`로 controller-specific 작업을 분리합니다. 안정적인 driver는 qc ownership과 completion short-circuit, port freeze·thaw, HSM·NCQ·bus error 분류, reset 뒤 IDENTIFY revalidation과 단계적 link-speed 강등을 일관된 EH path에서 처리해야 합니다.

2. 영어 원문 전체

번역 기준이 된 Linux v6.18.37 원문입니다. 줄 번호는 이 버전의 파일 좌표입니다.

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

3. 한국어 전문 번역

영어 원문의 문단 순서와 의미를 유지한 전체 번역입니다. 코드, 함수명, symbol과 URL은 원문 표기를 유지합니다.

libATA Developer's Guide 소개

1-17

문서 제목은 `libATA Developer's Guide`이며 저자는 Jeff Garzik입니다. libATA는 Linux kernel 안에서 ATA host controller와 device를 지원하는 library입니다.

libATA는 ATA driver API, ATA·ATAPI device용 class transport, T10 SAT specification에 따른 SCSI와 ATA 사이 translation을 제공합니다. 이 guide는 driver API, library function과 내부 구조, 두 low-level ATA driver example을 설명합니다.

libATA 역할
SCSI midlayerSCSI↔ATA translationlibATA core·class transportLow-level ATA driverATA host controllerATA·ATAPI device

SCSI request와 ATA hardware 사이의 주요 계층입니다.

ata_port_operations 개요

18-34

모든 low-level libATA hardware driver는 `struct ata_port_operations`를 정의합니다. 이 structure가 low-level driver와 ATA·SCSI layer의 연결 방식을 제어합니다.

FIS 기반 driver는 high-level `->qc_prep()`과 `->qc_issue()` hook으로 system에 연결합니다. PCI IDE와 비슷한 hardware는 여러 generic helper를 사용할 수 있으며 최소한 ATA shadow register block의 bus I/O address를 정의합니다.

Low-level driver 연결 방식
Hardware model주요 연결
FIS 기반qc_prep + qc_issue
PCI IDE 유사Generic SFF/BMDMA helper + shadow-register I/O address
공통struct ata_port_operations

Controller model에 따른 최소 hook 집합입니다.

Post-IDENTIFY device configuration

35-48
void (*dev_config) (struct ata_port *, struct ata_device *);

`->dev_config()`는 발견한 각 device에 `IDENTIFY DEVICE` 또는 `IDENTIFY PACKET DEVICE`를 보낸 뒤 호출합니다. 일반적으로 `SET FEATURES - XFER MODE`를 보내기 전, 실제 operation을 시작하기 전에 device-specific fixup을 적용합니다. `ata_port_operations`에서 `NULL`로 둘 수 있습니다.

Device configuration 시점
Device 발견IDENTIFY [PACKET] DEVICEdev_config fixupSET FEATURES - XFER MODENormal operation

IDENTIFY 이후 transfer mode 설정 전의 fixup 단계입니다.

PIO·DMA mode 설정

49-78
void (*set_piomode) (struct ata_port *, struct ata_device *);
void (*set_dmamode) (struct ata_port *, struct ata_device *);
void (*post_set_mode) (struct ata_port *);
unsigned int (*mode_filter) (struct ata_port *, struct ata_device *, unsigned int);

이 hook들은 `SET FEATURES - XFER MODE` command를 보내기 전에 호출됩니다. Optional `->mode_filter()`는 libATA가 만든 가능한 mode mask를 받아 hardware limit에 맞지 않는 mode를 제거한 valid mask를 반환합니다. 이 interface로 mode를 추가하는 것은 허용되지 않습니다.

`->set_piomode()`와 `->set_dmamode()` 호출 시 `dev->pio_mode`와 `dev->dma_mode`는 valid합니다. 같은 cable을 공유하는 다른 drive의 timing도 valid합니다. Library가 channel의 모든 drive에 대한 mode 결정을 기록한 뒤 실제 설정을 시작하기 때문입니다.

`->post_set_mode()`는 `SET FEATURES - XFER MODE`가 성공한 뒤 조건 없이 호출됩니다. `->set_piomode()`는 hook이 있으면 항상 호출하지만 `->set_dmamode()`는 DMA가 가능할 때만 호출합니다.

Transfer mode 결정
libATA가 possible-mode mask 생성mode_filter가 hardware 부적합 mode 제거Channel의 모든 drive mode 기록set_piomode 항상 호출DMA 가능 시 set_dmamodeSET FEATURES 성공post_set_mode

가능 mode를 줄인 뒤 channel 전체 timing을 확정합니다.

Mode hook 규칙
Hook호출 시점·조건
mode_filter가능 mask filtering, mode 추가 금지
set_piomode존재하면 항상
set_dmamodeDMA 가능할 때만
post_set_modeXFER MODE 성공 뒤 무조건

각 callback의 호출 조건입니다.

Taskfile·PIO data·command 실행

79-118
void (*sff_tf_load) (struct ata_port *ap, struct ata_taskfile *tf);
void (*sff_tf_read) (struct ata_port *ap, struct ata_taskfile *tf);

`->sff_tf_load()`는 taskfile을 hardware register 또는 DMA buffer에 넣고 `->sff_tf_read()`는 현재 taskfile register 값을 읽습니다. PIO·MMIO taskfile hardware driver 대부분은 `ata_sff_tf_load()`와 `ata_sff_tf_read()`를 사용합니다.

void (*sff_data_xfer) (struct ata_device *, unsigned char *, unsigned int, int);

모든 BMDMA-style driver는 `->sff_data_xfer()`를 구현해야 합니다. PIO data transfer 중 실제 data byte를 copy하는 low-level operation이며 보통 `ata_sff_data_xfer()` 또는 `ata_sff_data_xfer32()`를 선택합니다.

void (*sff_exec_command)(struct ata_port *ap, struct ata_taskfile *tf);

`->sff_exec_command()`는 앞서 `->tf_load()`한 ATA command를 hardware에서 시작합니다. Taskfile 기반 driver 대부분은 `ata_sff_exec_command()` helper를 사용합니다.

SFF command path
sff_tf_loadsff_exec_commandHardware command 시작필요 시 sff_data_xfersff_tf_read로 result register 획득

Taskfile 준비에서 PIO data transfer까지의 low-level 순서입니다.

ATAPI DMA·shadow register·device select

119-173
int (*check_atapi_dma) (struct ata_queued_cmd *qc);

`->check_atapi_dma()`는 개별 ATA PACKET command가 DMA를 사용해도 되는지 low-level driver가 filter하게 합니다. `NULL`이면 libATA는 ATAPI DMA를 지원할 수 있다고 가정합니다.

u8   (*sff_check_status)(struct ata_port *ap);
u8   (*sff_check_altstatus)(struct ata_port *ap);

`->sff_check_status()`와 `->sff_check_altstatus()`는 hardware의 Status·AltStatus shadow register를 읽습니다. 일부 hardware에서는 Status read가 interrupt condition을 clear하는 side effect가 있습니다. Taskfile driver 대부분은 `ata_sff_check_status()`를 사용합니다.

void (*sff_set_devctl)(struct ata_port *ap, u8 ctl);

`->sff_set_devctl()`은 Device Control ATA shadow register를 씁니다. Driver 대부분은 별도 구현이 필요 없습니다.

void (*sff_dev_select)(struct ata_port *ap, unsigned int device);

`->sff_dev_select()`는 ATA bus에서 N개 hardware device 중 하나를 active device로 선택하는 low-level command를 냅니다. FIS 기반 device에는 일반적으로 의미가 없고 taskfile driver는 보통 `ata_sff_dev_select()`를 사용합니다.

ATAPI·shadow-register hook
Hook역할주의
check_atapi_dmaPACKET별 DMA 허용 판단NULL이면 DMA 가능 가정
sff_check_statusStatus readInterrupt clear side effect 가능
sff_check_altstatusAltStatus readStatus 대안
sff_set_devctlDevice Control write대부분 불필요
sff_dev_selectBus device 선택FIS device에는 대개 무의미

Optional filtering과 register side effect를 정리했습니다.

PACKET DMA 선택
ATA PACKET commandcheck_atapi_dma hook 존재?없음: DMA supported 가정있음: command 검사허용이면 DMA거부면 non-DMA path

Command별 DMA capability를 결정합니다.

Private tuning method

174-196
void (*set_mode) (struct ata_port *ap);

기본적으로 libATA는 ATA timing rule에 따라 drive와 controller를 tuning하고 blacklist와 cable limit도 적용합니다. 실제 drive timing을 다루지 않는 일부 RAID controller처럼 custom rule이 필요한 controller는 `->set_mode()`를 사용할 수 있습니다.

Controller quirk 때문에 standard tuning logic 전체를 이 hook으로 대체하면 안 됩니다. 그렇게 하면 data reliability에 중요한 drive·bridge quirk 처리를 우회합니다. Mode selection을 제한해야 한다면 `mode_filter` hook을 사용해야 합니다.

Tuning hook 선택
상황사용할 hook
일반 controllerlibATA standard timing
가능 mode 제한mode_filter
실제 drive timing이 없는 특수 controllercustom set_mode 가능
단순 quirkset_mode로 전체 대체 금지

Standard logic 보존이 필요한 이유입니다.

PCI IDE BMDMA engine 제어

197-231
void (*bmdma_setup) (struct ata_queued_cmd *qc);
void (*bmdma_start) (struct ata_queued_cmd *qc);
void (*bmdma_stop) (struct ata_port *ap);
u8   (*bmdma_status) (struct ata_port *ap);

IDE BMDMA transaction에서 `->bmdma_setup()`은 DMA engine을 arm하고 `->bmdma_start()`는 시작하며 `->bmdma_stop()`은 멈춥니다. `->bmdma_status()`는 standard PCI IDE DMA Status register를 읽습니다. FIS driver에서는 대개 no-op이거나 구현하지 않습니다.

Legacy IDE driver의 `ata_bmdma_setup()`은 PRD table pointer를 IDE PRD Table Address register에 쓰고 DMA Command register에서 DMA를 enable한 뒤 `exec_command()`를 호출합니다. `ata_bmdma_start()`는 `ATA_DMA_START`를 set하고 `ata_bmdma_stop()`은 clear합니다. Status hook에는 `ata_bmdma_status()`를 흔히 사용합니다.

BMDMA transaction
PRD table address writebmdma_setup가 DMA enable·command issuebmdma_start가 ATA_DMA_START setDMA transferbmdma_status readbmdma_stop이 flag clear

PRD 준비에서 engine stop까지의 generic helper 동작입니다.

High-level queued-command hook

232-255
enum ata_completion_errors (*qc_prep) (struct ata_queued_cmd *qc);
int (*qc_issue) (struct ata_queued_cmd *qc);

`->qc_prep()`과 `->qc_issue()`는 여러 taskfile·DMA engine hook을 대체할 수 있는 high-level hook입니다. DMA mapping 뒤 호출되는 `->qc_prep()`은 hardware scatter-gather table을 채우는 데 주로 쓰입니다. 일부 driver는 `ata_bmdma_qc_prep()` 또는 `ata_bmdma_dumb_qc_prep()`을 쓰고 advanced driver는 자체 구현합니다.

Hardware와 S/G table 준비가 끝나면 `->qc_issue()`가 command를 active로 만듭니다. IDE BMDMA driver는 taskfile protocol dispatch에 `ata_sff_qc_issue()`를 사용하며 이 helper가 필요에 따라 `->sff_tf_load()`, `->bmdma_setup()`, `->bmdma_start()`를 호출합니다.

Queued command 준비·발행
Buffer DMA mapqc_prep로 S/G table 작성Hardware 준비qc_issueata_sff_qc_issue 사용 시 tf_loadbmdma_setupbmdma_start

High-level hook과 generic SFF helper의 관계입니다.

Exception·probe handling

256-314
void (*freeze) (struct ata_port *ap);
void (*thaw) (struct ata_port *ap);

HSM violation 등으로 port normal operation이 깨지면 `ata_port_freeze()`가 port를 freeze합니다. Frozen port는 보통 successful reset 뒤 thaw될 때까지 operation을 수행할 수 없습니다. Optional `->freeze()`는 interrupt masking·DMA stop 같은 hardware freeze를 수행합니다. Hardware freeze가 불가능하면 frozen 동안 interrupt handler가 interrupt를 무조건 ack·clear해야 합니다. `->thaw()`는 interrupt unmask와 DMA restart로 normal operation을 복구합니다.

void (*error_handler) (struct ata_port *ap);

`->error_handler()`는 probe, hotplug, recovery와 다른 exceptional condition에 대한 driver hook이며 구현의 핵심 책임은 `ata_std_error_handler()` 호출입니다. Standard handler는 failed device를 되살리고 lost device를 detach하며 new device를 추가하고 필요에 따라 reset operation을 호출합니다.

Reset sequence에서 optional `prereset`은 다른 action 전에, optional `postreset`은 reset 뒤 호출됩니다. Severity와 hardware capability에 따라 `softreset` 또는 `hardreset`을 호출하며 둘 다 있으면 `hardreset`을 선호합니다. 둘 다 없으면 low-level reset 없이 ATA class device가 link에 연결된 것으로 가정합니다.

void (*post_internal_cmd) (struct ata_queued_cmd *qc);

`->post_internal_cmd()`는 probe-time 또는 EH-time에 `ata_exec_internal()`로 command를 실행한 뒤 필요한 hardware-specific 마무리를 수행합니다.

Port freeze·recovery
HSM violation·exceptionata_port_freezefreeze: IRQ mask·DMA stoperror_handlerata_std_error_handlerReset·device revalidationthaw: IRQ unmask·DMA restart

Disruption에서 standard EH를 거쳐 thaw하기까지입니다.

EH reset callback 순서
단계Callback·규칙
1prereset optional
2hardreset 우선, 없으면 softreset
3둘 다 없으면 low-level reset 생략
4postreset optional
Internal command 후post_internal_cmd

Optional callback과 reset 우선순위입니다.

Interrupt·SATA PHY·lifecycle

315-381
irqreturn_t (*irq_handler)(int, void *, struct pt_regs *);
void (*irq_clear) (struct ata_port *);

`->irq_handler`는 libATA가 system에 등록하는 interrupt routine이고 `->irq_clear`는 handler 등록 직전 probe 중 hardware를 quiet하게 만들기 위해 호출합니다. 두 번째 argument `dev_instance`는 `struct ata_host_set *`로 cast합니다.

Legacy IDE driver는 보통 `ata_sff_interrupt()`로 host_set의 모든 port를 scan해 active qc를 찾고 `ata_sff_host_intr(ap, qc)`를 호출합니다. `ata_sff_irq_clear()`는 DMA status register의 interrupt와 error flag를 clear합니다.

int (*scr_read) (struct ata_port *ap, unsigned int sc_reg,
         u32 *val);
int (*scr_write) (struct ata_port *ap, unsigned int sc_reg,
                   u32 val);

`->scr_read()`와 `->scr_write()`는 `SCR_STATUS`, `SCR_CONTROL`, `SCR_ERROR`, `SCR_ACTIVE` 같은 standard SATA PHY register를 읽고 씁니다.

int (*port_start) (struct ata_port *ap);
void (*port_stop) (struct ata_port *ap);
void (*host_stop) (struct ata_host_set *host_set);

`->port_start()`는 port data structure 초기화 직후 호출되어 per-port DMA buffer·table·ring을 allocate하고 DMA engine을 enable합니다. `ap->private_data`용 memory도 이때 allocate할 수 있습니다. `ata_port_start()`는 legacy IDE PRD table 공간을 allocate합니다.

Shutdown 때 모든 `->port_stop()` 뒤 `->host_stop()`이 호출된다고 적힌 원문과 callback 설명의 ordering을 함께 따라야 합니다. `->port_stop()`은 active use가 끝난 DMA·memory와 private data를 release하고, `->host_stop()`은 hardware shutdown과 host-level DMA resource release를 마무리하며 `NULL`일 수 있습니다.

IRQ·SCR hook
Hook역할
irq_handlerRegistered interrupt routine
irq_clearProbe 중 pending IRQ·error clear
scr_readSATA PHY register read
scr_writeSATA PHY register write

Interrupt와 SATA PHY register 역할입니다.

Port resource lifecycle
Port structure initport_start: DMA·PRD·private data allocateNormal operationport_stop: per-port resource release모든 port stophost_stop: controller·host resource finalize

Initialization과 shutdown의 resource ownership입니다.

Error handling과 command origin

382-404

이 장은 libATA error handling을 설명합니다. 먼저 `Documentation/scsi/scsi_eh.rst`의 SCSI EH와 ATA exception 문서를 읽는 것이 권장됩니다.

libATA command는 `struct ata_queued_cmd`, 줄여서 qc로 표현합니다. Qc는 port initialization 때 미리 allocate되어 반복 사용됩니다. 원문 작성 당시 port당 하나였고 향후 NCQ branch는 tag마다 하나를 allocate해 qc와 NCQ tag를 1:1로 mapping하는 model을 설명합니다.

Command source는 libATA 자체와 SCSI midlayer 두 가지입니다. Internal command는 initialization과 error handling에 쓰고, normal block request와 SCSI emulation command는 SCSI host template의 `queuecommand` callback을 통해 SCSI command로 전달됩니다.

libATA command origin
libATA init·EH internal command또는 SCSI midlayer queuecommandstruct ata_queued_cmdLow-level driver issueCompletion·EH

Internal command와 SCSI request가 qc execution으로 모입니다.

Command 발행

405-434

Internal command는 allocate한 qc의 taskfile을 초기화합니다. Completion notification은 asynchronous `qc->complete_fn()` callback과 issuer가 process context에서 sleep하는 synchronous completion `qc->waiting` 두 방식입니다. 초기화 뒤 `host_set` lock을 잡고 qc를 발행합니다.

모든 libATA driver는 `ata_scsi_queuecmd()`를 `hostt->queuecommand` callback으로 사용합니다. Simulated SCSI command는 qc 없이 즉시 result를 계산해 완료하고, translated command는 qc를 사용합니다.

ATA command completion에는 `ata_scsi_qc_complete()`, ATAPI에는 `atapi_qc_complete()`를 `qc->complete_fn()`으로 사용합니다. 둘 다 끝에서 `qc->scsidone`을 호출해 upper layer에 알립니다. Translation이 끝나면 `ata_qc_issue()`로 qc를 발행합니다. SCSI midlayer가 `hostt->queuecommand` 호출 때 `host_set` lock을 잡고 있으므로 이 과정 전체가 lock 안에서 일어납니다.

Command completion path
CommandCompletion mechanism
Internal synchronousqc->waiting
Normal translated asyncqc->complete_fn
ATA SCSI translationata_scsi_qc_complete
ATAPIatapi_qc_complete
Simulated SCSIQc 없이 즉시 완료

Source와 protocol에 따른 callback입니다.

Command protocol 처리

435-466

Protocol과 controller에 따라 처리 방식이 다릅니다. 문서는 taskfile interface와 standard callback을 사용하는 controller를 가정하고 6개 ATA protocol을 네 category로 묶습니다.

`ATA_PROT_NODATA`와 `ATA_PROT_DMA`는 issue 뒤 software intervention이 필요 없고 완료 시 device가 interrupt를 올립니다. `ATA_PROT_PIO`는 `ATA_NIEN`으로 interrupt를 끄고 `ata_wq`의 `pio_task`가 polling과 I/O를 수행합니다.

`ATA_PROT_ATAPI_NODATA`와 `ATA_PROT_ATAPI_DMA`는 PACKET command 뒤 `packet_task`가 BSY를 poll합니다. BSY가 clear되면 CDB를 전송하고 interrupt handler에 넘깁니다. `ATA_PROT_ATAPI` PIO도 `packet_task`가 CDB를 보내지만 이후 data transfer는 `pio_task`가 맡습니다.

ATA protocol processing
CategoryProtocol처리
ATA no-data/DMAATA_PROT_NODATA·ATA_PROT_DMA완료 interrupt
ATA PIOATA_PROT_PIOATA_NIEN + pio_task polling
ATAPI no-data/DMAATA_PROT_ATAPI_NODATA·DMApacket_task CDB 후 interrupt
ATAPI PIOATA_PROT_ATAPIpacket_task CDB 후 pio_task data

여섯 protocol을 software 개입 방식으로 분류했습니다.

Command 완료

467-515

발행된 qc는 `ata_qc_complete()`로 완료되거나 timeout됩니다. Interrupt command는 `ata_host_intr()`, PIO는 `pio_task`, error case 일부는 `packet_task`가 completion을 호출합니다.

`ata_qc_complete()`는 DMA mapping을 해제하고 `qc->flags`의 `ATA_QCFLAG_ACTIVE`를 clear한 뒤 `qc->complete_fn`을 호출합니다. Callback이 0이 아닌 값을 반환하면 short-circuit하고 qc를 deallocate하지 않습니다.

Normal path의 `__ata_qc_complete()`는 flags를 0으로 clear하고 `ap->active_tag`와 `qc->tag`를 poison하며 `qc->waiting`을 clear한 뒤 complete하고, `ap->qactive` bit를 clear해 qc를 deallocate합니다. 즉 upper layer에 알리고 qc를 반환합니다.

Non-ATAPI command는 성공·실패 모두 거의 같은 path로 완료합니다. Failed ATAPI command는 REQUEST SENSE가 필요합니다. Error status로 `ata_qc_complete()`를 부르면 `atapi_qc_complete()`가 `scmd->result`를 `SAM_STAT_CHECK_CONDITION`으로 설정하고 scmd를 완료한 뒤 1을 반환합니다.

Sense data는 비어 있지만 result가 CHECK CONDITION이므로 SCSI midlayer가 EH를 호출합니다. Return 1은 qc deallocation을 막아 partially completed qc를 `ata_scsi_error()`까지 운반합니다.

Normal qc completion
ata_qc_completeDMA unmapATA_QCFLAG_ACTIVE clearcomplete_fn0이면 __ata_qc_completeflags·tags poison/clearwaiting completeqactive bit clear

DMA unmap에서 qc deallocation까지의 순서입니다.

Failed ATAPI short-circuit
ATAPI command failureatapi_qc_completeSAM_STAT_CHECK_CONDITIONcomplete_fn returns 1Qc deallocation 보류SCSI EHata_scsi_error

REQUEST SENSE를 위해 qc를 반완료 상태로 유지합니다.

ata_scsi_error

516-544

`ata_scsi_error()`는 당시 libATA의 `transportt->eh_strategy_handler()`이며 timeout과 ATAPI error completion 두 경우에 진입합니다. 아직 active이고 failed로 표시되지 않은 qc에는 `AC_ERR_TIMEOUT`을 set한 뒤 low-level driver의 `error_handler`를 호출합니다.

Error handler는 BMDMA를 stop하고 qc를 완료합니다. EH context에서는 `scsi_done`을 부를 수 없으므로 `qc->scsidone`을 `scsi_finish_command()`로 바꾸고 `ata_qc_complete()`를 호출합니다.

Failed ATAPI qc는 완료됐지만 deallocate되지 않은 placeholder로 EH code를 이 지점까지 오게 합니다. 여기서 `__ata_qc_complete()`로 명시적으로 deallocate하고 REQUEST SENSE용 internal qc를 발행합니다. Sense data를 얻으면 scmd에 `scsi_finish_command()`를 직접 호출합니다. 기존 qc는 이미 완료·해제됐으므로 `ata_qc_complete()`를 다시 호출할 수 없습니다.

ata_scsi_error recovery
Timeout 또는 CHECK CONDITIONActive qc에 AC_ERR_TIMEOUTLow-level error_handlerBMDMA stopscsidone=scsi_finish_commandQc complete·deallocateREQUEST SENSE internal qcscsi_finish_command

Timeout 또는 failed ATAPI qc에서 sense completion까지입니다.

기존 EH 문제와 library 문서

545-606

기존 EH는 error 표현이 지나치게 단순해 ATA device error가 아닌 condition도 ATA STATUS·ERROR register와 `ATA_ERR`로 표현합니다. Timeout command를 device가 잊고 새 command를 받을 상태로 만드는 action도 없습니다.

`ata_scsi_error()` 진입 시 device가 quiescent하지 않아 timed-out command가 언제든 성공·실패할 수 있고 `pio_task`·`atapi_task`가 계속 실행될 수 있습니다. HSM mismatch, NCQ, hotplug에는 reset을 포함한 더 강한 recovery가 필요합니다.

ATA error는 interrupt handler, PIO error는 `pio_task`에서 직접 처리되어 advanced EH가 여러 path로 흩어집니다. Context와 internal qc가 필요한 recovery, CRC 뒤 정보 수집·reset·reconfiguration 같은 복잡한 action, low-level callback별 분산 구현을 어렵고 error-prone하게 만듭니다.

Library API와 내부 문서는 kernel-doc directive로 `drivers/ata/libata-core.c`, `drivers/ata/libata-eh.c`, `drivers/ata/libata-scsi.c`에서 가져옵니다.

.. kernel-doc:: drivers/ata/libata-core.c
   :export:
.. kernel-doc:: drivers/ata/libata-core.c
   :internal:

.. kernel-doc:: drivers/ata/libata-eh.c
.. kernel-doc:: drivers/ata/libata-scsi.c
   :export:

.. kernel-doc:: drivers/ata/libata-scsi.c
   :internal:
Current EH 한계
문제영향
Crude error representationATA 외 error 구분 불가
Timeout abort 없음Device가 old command를 계속 처리
Quiesce 보호 부족Task·completion race
Recovery 약함HSM·NCQ·hotplug 대응 부족
EH path 분산정보 수집·reset·driver 구현 복잡

문서가 지적한 구조적 문제입니다.

libATA kernel-doc source
영역Source
Core exportsdrivers/ata/libata-core.c :export:
Core internals·EHdrivers/ata/libata-core.c :internal: + libata-eh.c
SCSI translationdrivers/ata/libata-scsi.c :export: + :internal:

Generated API·internal documentation source path입니다.

ATA error·exception 정의

607-634

이 장은 ATA·ATAPI device의 error와 exception condition을 구현과 무관한 방식으로 분류하고 처리 원칙을 설명합니다.

`error`는 device가 명시적 error를 보고하거나 command가 timeout된 condition입니다. `exception`은 power·hotplug처럼 error가 아닌 exceptional condition 또는 error와 non-error exception 전체를 뜻할 수 있습니다. 구분이 필요하면 `non-error exception`이라고 합니다.

Category는 주로 legacy taskfile + bus-master IDE interface를 기준으로 설명하지만 더 나은 controller error-reporting mechanism도 이 category로 mapping할 수 있습니다. Recovery action인 reset과 transport reconfiguration은 뒤의 EH recovery section에서 설명합니다.

Error 용어
용어정의
ErrorExplicit device error 또는 timeout
Non-error exceptionPower·hotplug 등 오류가 아닌 예외
Exception문맥에 따라 non-error 또는 모든 exceptional condition

libATA 문서의 error·exception 구분입니다.

HSM violation

635-662

ATA·ATAPI command 발행 또는 실행 중 STATUS가 HSM requirement와 맞지 않으면 HSM violation입니다. Command issue 때 `!BSY && DRDY && !DRQ`가 아니거나, PIO data transfer 중 `!BSY && !DRQ`, completion 때 DRQ set, CDB 마지막 byte 전송 전 `!BSY && ERR`인 경우가 포함됩니다.

PACKET command는 command packet 마지막 byte를 쓰기 전에 error로 끝나면 안 된다고 standard가 규정하며 state diagram에도 그런 transition이 없습니다. HSM이 깨지면 STATUS·ERROR에서 유용한 정보를 거의 얻지 못해 driver bug, device, controller, cable 어느 쪽도 원인일 수 있습니다.

Known state 복구에는 reset이 필요합니다. Transmission error가 이런 증상을 만들 수 있으므로 transport speed를 낮추는 것도 도움이 될 수 있습니다.

HSM violation condition
단계Violation
Command issueExpected !BSY && DRDY && !DRQ 불충족
PIO transfer!BSY && !DRQ
CompletionDRQ set
CDB transfer마지막 byte 전 !BSY && ERR

Protocol state와 어긋나는 대표 STATUS 조합입니다.

Non-NCQ ATA·ATAPI device error

663-748

이 category는 ATA·ATAPI device가 감지·보고한 문제로 STATUS와 ERROR register가 valid합니다. 일부 ATA bus error도 같은 mechanism으로 보고됩니다. ATA command는 실행·완료 중 `!BSY && ERR`로 나타납니다.

ATAPI에서 PACKET 직후 `!BSY && ERR && ABRT`는 PACKET 미지원으로 이 category입니다. CDB 마지막 byte 뒤 `ERR(CHK) && !ABRT` 또는 `ERR(CHK) && ABRT`는 CHECK CONDITION으로 이 category가 아닙니다.

Data transfer의 ICRC는 corruption을 뜻합니다. ATA/ATAPI-7까지는 UDMA에만 적용됐지만 ATA/ATAPI-8 draft 1f는 MWDMA·PIO에도 적용 가능하다고 합니다. ABRT만으로도 transfer error일 수 있으나 revision에 따라 의미가 달라 heuristic이 필요합니다.

Media error는 ERROR의 `UNC` bit이며 device가 자체 retry 뒤에도 복구하지 못한 상태라 upper layer에 알리는 것 외에 할 일이 적습니다. READ·WRITE가 첫 failed sector CHS/LBA를 주어도 error completion 전 transfer amount가 indeterminate이므로 앞 sector가 성공했다고 가정할 수 없습니다.

Media changed·change requested 부분은 원문에 TODO로 남아 있습니다. Address error는 `IDNF` bit로 upper layer에 보고합니다. Invalid command·parameter 등의 other error는 ABRT로 표시될 수 있지만 ABRT가 ICRC·address error도 뜻해 heuristic이 필요합니다.

Command별로 적용되지 않는 STATUS·ERROR bit는 output description에서 `na`로 표시됩니다. ATA/ATAPI-8 draft 1f의 `N/A` 정의는 standard상 defined value가 없고 host·device가 검사하지 않으며 0으로 clear해야 한다는 뜻입니다. 따라서 device가 `na` bit를 0으로 clear한다고 보고 별도 masking이 필요 없다고 판단할 수 있습니다.

Device error classification
표시의미처리
ICRCData transfer corruptionATA bus error로 처리
ABRTUnsupported·invalid 또는 transfer error 가능Heuristic 필요
UNCMedia retry 실패Upper layer 보고
IDNFAddress errorUpper layer 보고
CHKATAPI CHECK CONDITIONREQUEST SENSE

ERROR bit와 처리 방향입니다.

Failed-sector 처리 제한
READ/WRITE errorFirst failed CHS/LBA 보고실제 transferred amount는 indeterminate앞 sector 성공 여부 불명SCSI처럼 partial success 처리 금지

Reported LBA 앞 sector를 성공 처리할 수 없는 이유입니다.

ATAPI CHECK CONDITION과 NCQ error

749-787

PACKET command의 CDB 마지막 byte를 전송한 뒤 STATUS의 CHK, 즉 ERR bit가 set되면 ATAPI CHECK CONDITION입니다. Error 정보를 얻기 위해 `REQUEST SENSE` packet command로 sense data를 수집합니다.

Sense data를 얻으면 다른 SCSI error처럼 처리합니다. Sense Key `04h HARDWARE ERROR`와 ASC/ASCQ `47h/00h SCSI PARITY ERROR`처럼 ATA bus error를 나타낼 수도 있으며 이 경우 ATA bus error recovery를 적용합니다.

NCQ command phase에 one or more command가 outstanding인 상태에서 BSY clear와 ERR set이면 NCQ error입니다. STATUS·ERROR도 valid하지만 error condition clear, failed command 식별과 추가 정보 수집에 `READ LOG EXT`가 필요합니다.

Log Page `10h`는 failed tag와 taskfile register 값을 보고합니다. Failed command는 normal ATA error처럼 처리하고 다른 in-flight command는 모두 retry합니다. 이 retry는 failed command가 없었다면 정상 완료됐을 가능성이 크므로 retry count에 포함하지 않습니다.

ATA bus error도 NCQ device error처럼 보고될 수 있습니다. Log Page 10h read가 실패하거나 NQ를 보고하면 상태를 복구하기 어려우므로 HSM violation으로 처리합니다.

ATAPI CHECK CONDITION
CDB 마지막 byte 전송STATUS CHK/ERRREQUEST SENSESense data 분석SCSI error 처리Parity sense면 ATA bus error recovery

PACKET failure에서 SCSI sense handling까지입니다.

NCQ error recovery
NCQ phase: !BSY && ERRREAD LOG EXT Page 10hFailed tag·taskfile 획득Failed command normal ATA error 처리다른 in-flight command retryLog read 실패·NQ면 HSM violation

Failed tag를 찾고 다른 command를 재시도합니다.

Bus error·late completion·timeout

788-851

ATA bus error는 SATA 또는 PATA 전송 중 data corruption입니다. ICRC·ABRT, controller-specific transmission error, 일부 controller의 timeout 진단, random error·timeout 등 다양한 증상으로 나타납니다.

Transmission error인지 판별할 수 없는 경우가 많아 heuristic이 필요합니다. 지원된 command에서 ABRT가 반복되는 경우가 예입니다. ATA bus error 가능성이 있으면 transmission speed를 낮춰 완화할 수 있습니다.

PCI 또는 다른 system bus 전송 중 corruption은 standard BMDMA에서 BMDMA Status의 Error bit로 나타납니다. System에 심각한 문제가 있음을 뜻하므로 반드시 log하고 host controller reset을 권장합니다.

Late completion은 timeout handler가 timed-out command가 이미 성공 또는 error로 완료됐음을 발견하는 경우이며 보통 lost interrupt가 원인입니다. Log와 host reset이 권장됩니다.

Unknown timeout은 command가 여전히 processing 중이거나 host·device가 unknown state인 경우입니다. HSM은 valid·invalid 어느 state일 수도 있으므로 reset해 known state로 만들고 timed-out command를 잊게 해야 합니다. Command는 retry할 수 있으며 transmission error 가능성도 고려합니다. Hotplug·power-management exception 절은 원문에 TODO로 남아 있습니다.

Bus·timeout category
Category주요 증상Action
ATA bus errorICRC·ABRT·timeout·random lockupHeuristic + speed down
PCI bus errorBMDMA Status ErrorLog + host reset
Late completionTimeout 뒤 이미 완료, lost IRQLog + host reset
Unknown timeoutCommand processing·state unknownReset + optional retry

증상과 권장 recovery를 비교했습니다.

ATA transmission heuristic
Error 또는 timeoutICRC·controller detail 확인명시적 transmission evidence?없으면 repeated ABRT·known command pattern 확인ATA bus error 가능Transport speed lower

명확하지 않은 error를 반복 pattern으로 판단합니다.

EH recovery와 reset 정책

852-897

EH recovery에서 controller error register를 clear해야 하며 요구사항은 controller마다 다릅니다. SATA에서는 최소한 SError register를 clear하는 것이 강하게 권장됩니다.

HSM 또는 HBA state가 unknown·invalid이거나, HBA·device가 in-flight command를 잊어야 하거나, 이상 동작할 때 reset이 필요합니다. Robustness를 위해 error 종류와 무관하게 reset하는 것도 유용할 수 있습니다.

HBA가 ready이고 device만 unknown이면 device만 reset합니다. HBA도 unknown이면 HBA와 device 모두 reset합니다. HBA reset은 implementation-specific입니다. Taskfile/BMDMA PCI IDE는 active DMA stop만으로 충분할 수 있지만 controller별 context·reset requirement는 specific driver가 처리해야 합니다.

Reset scope 결정
EH 진입Controller error register·SATA SError clearHBA state known-ready?예: device state만 unknown이면 device reset아니오: HBA + device resetConfiguration revalidation

Known state에 따라 device-only 또는 full reset을 선택합니다.

ATA·ATAPI reset 방식

898-958

PATA hardware reset은 PATA `RESET-` signal을 assert하는 hardware-initiated reset입니다. Software에서 시작하는 standard 방법은 없지만 일부 hardware는 signal을 직접 제어하는 register를 제공합니다.

Software reset은 CONTROL의 `SRST` bit를 최소 5us 동안 set합니다. PATA와 SATA 모두 지원하지만 SATA에서는 BSY가 set된 동안 SRST를 clear하는 두 번째 Register FIS를 보내야 해 controller-specific support가 필요할 수 있습니다. PATA에서는 channel의 master와 slave를 모두 reset합니다.

`EXECUTE DEVICE DIAGNOSTIC`(EDD)은 정확한 reset level이 명시되지 않았지만 software reset과 비슷한 reset을 암시합니다. Normal command path로 처리할 수 있고 SATA controller도 보통 지원하지만 BSY 중 발행할 수 없고 unknown state 동작이 불명확해 EH에는 적합하지 않습니다. PATA에서는 두 device 모두에 영향이 있습니다.

`ATAPI DEVICE RESET`은 software reset과 비슷하지만 cable의 다른 device에 영향 없이 selected device만 reset할 수 있습니다. SATA PHY reset은 SATA device의 권장 방식으로 PATA hardware reset과 사실상 같고 standard SCR Control register로 구현해 software reset보다 쉬운 경우가 많습니다.

Reset은 `INITIALIZE DEVICE PARAMETERS`의 CHS, `SET FEATURES`의 transfer mode, `SET MULTIPLE MODE`의 block count, `SET MAX`·`MEDIA LOCK` 같은 parameter를 clear할 수 있습니다. Standard가 일부 parameter 유지 여부만 규정하므로 robust driver는 reset 뒤 필요한 값을 항상 재설정해야 하며 deep sleep power-off resume에도 적용됩니다.

Configuration parameter update 또는 hardware reset 뒤에는 `IDENTIFY DEVICE` 또는 `IDENTIFY PACKET DEVICE`를 다시 실행하고 그 결과로 이후 operation을 해야 합니다. OS driver가 revalidation mechanism을 구현해야 합니다.

Device reset method
Method대상·특성
PATA hardware resetRESET- signal, software standard 없음
Software resetSRST ≥5us, PATA channel 양쪽 reset
EDDNormal command 가능하지만 unknown state EH에 부적합
ATAPI DEVICE RESETSelected device만
SATA PHY reset권장, SCR Control로 구현

범위와 EH 적합성을 비교했습니다.

Reset 후 revalidation
Device resetIDENTIFY DEVICE/PACKET 다시 실행Capability·identity revalidateTransfer mode·block count·CHS 등 복원Updated IDENTIFY result로 operation 재개

Reset이 지운 configuration을 복원하는 순서입니다.

Transport 재설정·driver internals·감사

959-1000

저가 connector·cable·controller 때문에 PATA와 SATA 모두 transmission error가 흔하며 speed를 낮춰 완화할 수 있습니다. Jeff Garzik이 제안한 scheme은 15분 안에 3회 정도보다 많은 error가 발생하면 SATA PHY speed를 먼저 낮추고, 더 낮출 수 없으면 UDMA speed를 낮추며 UDMA0에서는 PIO4로 전환합니다. 이후 PIO speed를 낮추고 PIO3에서도 계속 오류가 나면 경고하되 동작을 계속합니다.

`ata_piix`와 `sata_sil` 내부 문서는 각 driver source의 kernel-doc internal directive에서 생성됩니다.

.. kernel-doc:: drivers/ata/ata_piix.c
   :internal:
.. kernel-doc:: drivers/ata/sata_sil.c
   :internal:

ATA 지식은 Andre Hedrick과의 논의, ATA·SCSI specification 연구에서 왔습니다. Alan Cox는 SATA와 SCSI의 유사성을 지적하고 libATA 개발 동기를 제공했습니다. `ata_pio_devchk`와 초기 probing은 Hale Landis의 ATADRVR probe/reset code 연구를 기반으로 했습니다.

Transmission speed fallback
15분 내 error가 N회 초과SATA면 PHY speed 감소더 감소 불가UDMA speed 감소UDMA0이면 PIO4PIO speed 감소PIO3에서도 지속되면 경고·계속

반복 error에서 단계적으로 link mode를 낮춥니다.

Low-level driver kernel-doc
DriverSource
ata_piixdrivers/ata/ata_piix.c :internal:
sata_sildrivers/ata/sata_sil.c :internal:

Example driver internal documentation source입니다.