3 * Provides ACPI support for PATA/SATA.
5 * Copyright (C) 2006 Intel Corp.
6 * Copyright (C) 2006 Randy Dunlap
9 #include <linux/module.h>
10 #include <linux/ata.h>
11 #include <linux/delay.h>
12 #include <linux/device.h>
13 #include <linux/errno.h>
14 #include <linux/kernel.h>
15 #include <linux/acpi.h>
16 #include <linux/libata.h>
17 #include <linux/pci.h>
18 #include <linux/slab.h>
19 #include <linux/pm_runtime.h>
20 #include <scsi/scsi_device.h>
23 #include <acpi/acpi_bus.h>
25 unsigned int ata_acpi_gtf_filter
= ATA_ACPI_FILTER_DEFAULT
;
26 module_param_named(acpi_gtf_filter
, ata_acpi_gtf_filter
, int, 0644);
27 MODULE_PARM_DESC(acpi_gtf_filter
, "filter mask for ACPI _GTF commands, set to filter out (0x1=set xfermode, 0x2=lock/freeze lock, 0x4=DIPM, 0x8=FPDMA non-zero offset, 0x10=FPDMA DMA Setup FIS auto-activate)");
29 #define NO_PORT_MULT 0xffff
30 #define SATA_ADR(root, pmp) (((root) << 16) | (pmp))
32 #define REGS_PER_GTF 7
34 u8 tf
[REGS_PER_GTF
]; /* regs. 0x1f1 - 0x1f7 */
38 * Helper - belongs in the PCI layer somewhere eventually
40 static int is_pci_dev(struct device
*dev
)
42 return (dev
->bus
== &pci_bus_type
);
45 static void ata_acpi_clear_gtf(struct ata_device
*dev
)
47 kfree(dev
->gtf_cache
);
48 dev
->gtf_cache
= NULL
;
52 * ata_ap_acpi_handle - provide the acpi_handle for an ata_port
53 * @ap: the acpi_handle returned will correspond to this port
55 * Returns the acpi_handle for the ACPI namespace object corresponding to
56 * the ata_port passed into the function, or NULL if no such object exists
58 acpi_handle
ata_ap_acpi_handle(struct ata_port
*ap
)
60 if (ap
->flags
& ATA_FLAG_ACPI_SATA
)
63 return acpi_get_child(DEVICE_ACPI_HANDLE(ap
->host
->dev
), ap
->port_no
);
65 EXPORT_SYMBOL(ata_ap_acpi_handle
);
68 * ata_dev_acpi_handle - provide the acpi_handle for an ata_device
69 * @dev: the acpi_device returned will correspond to this port
71 * Returns the acpi_handle for the ACPI namespace object corresponding to
72 * the ata_device passed into the function, or NULL if no such object exists
74 acpi_handle
ata_dev_acpi_handle(struct ata_device
*dev
)
77 struct ata_port
*ap
= dev
->link
->ap
;
79 if (dev
->flags
& ATA_DFLAG_ACPI_DISABLED
)
82 if (ap
->flags
& ATA_FLAG_ACPI_SATA
) {
83 if (!sata_pmp_attached(ap
))
84 adr
= SATA_ADR(ap
->port_no
, NO_PORT_MULT
);
86 adr
= SATA_ADR(ap
->port_no
, dev
->link
->pmp
);
87 return acpi_get_child(DEVICE_ACPI_HANDLE(ap
->host
->dev
), adr
);
89 return acpi_get_child(ata_ap_acpi_handle(ap
), dev
->devno
);
91 EXPORT_SYMBOL(ata_dev_acpi_handle
);
93 /* @ap and @dev are the same as ata_acpi_handle_hotplug() */
94 static void ata_acpi_detach_device(struct ata_port
*ap
, struct ata_device
*dev
)
97 dev
->flags
|= ATA_DFLAG_DETACH
;
99 struct ata_link
*tlink
;
100 struct ata_device
*tdev
;
102 ata_for_each_link(tlink
, ap
, EDGE
)
103 ata_for_each_dev(tdev
, tlink
, ALL
)
104 tdev
->flags
|= ATA_DFLAG_DETACH
;
107 ata_port_schedule_eh(ap
);
111 * ata_acpi_handle_hotplug - ACPI event handler backend
112 * @ap: ATA port ACPI event occurred
113 * @dev: ATA device ACPI event occurred (can be NULL)
114 * @event: ACPI event which occurred
116 * All ACPI bay / device realted events end up in this function. If
117 * the event is port-wide @dev is NULL. If the event is specific to a
118 * device, @dev points to it.
120 * Hotplug (as opposed to unplug) notification is always handled as
121 * port-wide while unplug only kills the target device on device-wide
125 * ACPI notify handler context. May sleep.
127 static void ata_acpi_handle_hotplug(struct ata_port
*ap
, struct ata_device
*dev
,
130 struct ata_eh_info
*ehi
= &ap
->link
.eh_info
;
134 spin_lock_irqsave(ap
->lock
, flags
);
136 * When dock driver calls into the routine, it will always use
137 * ACPI_NOTIFY_BUS_CHECK/ACPI_NOTIFY_DEVICE_CHECK for add and
138 * ACPI_NOTIFY_EJECT_REQUEST for remove
141 case ACPI_NOTIFY_BUS_CHECK
:
142 case ACPI_NOTIFY_DEVICE_CHECK
:
143 ata_ehi_push_desc(ehi
, "ACPI event");
145 ata_ehi_hotplugged(ehi
);
148 case ACPI_NOTIFY_EJECT_REQUEST
:
149 ata_ehi_push_desc(ehi
, "ACPI event");
151 ata_acpi_detach_device(ap
, dev
);
156 spin_unlock_irqrestore(ap
->lock
, flags
);
159 ata_port_wait_eh(ap
);
162 static void ata_acpi_dev_notify_dock(acpi_handle handle
, u32 event
, void *data
)
164 struct ata_device
*dev
= data
;
166 ata_acpi_handle_hotplug(dev
->link
->ap
, dev
, event
);
169 static void ata_acpi_ap_notify_dock(acpi_handle handle
, u32 event
, void *data
)
171 struct ata_port
*ap
= data
;
173 ata_acpi_handle_hotplug(ap
, NULL
, event
);
176 static void ata_acpi_uevent(struct ata_port
*ap
, struct ata_device
*dev
,
179 struct kobject
*kobj
= NULL
;
180 char event_string
[20];
181 char *envp
[] = { event_string
, NULL
};
185 kobj
= &dev
->sdev
->sdev_gendev
.kobj
;
187 kobj
= &ap
->dev
->kobj
;
190 snprintf(event_string
, 20, "BAY_EVENT=%d", event
);
191 kobject_uevent_env(kobj
, KOBJ_CHANGE
, envp
);
195 static void ata_acpi_ap_uevent(acpi_handle handle
, u32 event
, void *data
)
197 ata_acpi_uevent(data
, NULL
, event
);
200 static void ata_acpi_dev_uevent(acpi_handle handle
, u32 event
, void *data
)
202 struct ata_device
*dev
= data
;
203 ata_acpi_uevent(dev
->link
->ap
, dev
, event
);
206 static const struct acpi_dock_ops ata_acpi_dev_dock_ops
= {
207 .handler
= ata_acpi_dev_notify_dock
,
208 .uevent
= ata_acpi_dev_uevent
,
211 static const struct acpi_dock_ops ata_acpi_ap_dock_ops
= {
212 .handler
= ata_acpi_ap_notify_dock
,
213 .uevent
= ata_acpi_ap_uevent
,
217 * ata_acpi_dissociate - dissociate ATA host from ACPI objects
218 * @host: target ATA host
220 * This function is called during driver detach after the whole host
226 void ata_acpi_dissociate(struct ata_host
*host
)
230 /* Restore initial _GTM values so that driver which attaches
231 * afterward can use them too.
233 for (i
= 0; i
< host
->n_ports
; i
++) {
234 struct ata_port
*ap
= host
->ports
[i
];
235 const struct ata_acpi_gtm
*gtm
= ata_acpi_init_gtm(ap
);
237 if (ata_ap_acpi_handle(ap
) && gtm
)
238 ata_acpi_stm(ap
, gtm
);
243 * ata_acpi_gtm - execute _GTM
244 * @ap: target ATA port
245 * @gtm: out parameter for _GTM result
247 * Evaluate _GTM and store the result in @gtm.
253 * 0 on success, -ENOENT if _GTM doesn't exist, -errno on failure.
255 int ata_acpi_gtm(struct ata_port
*ap
, struct ata_acpi_gtm
*gtm
)
257 struct acpi_buffer output
= { .length
= ACPI_ALLOCATE_BUFFER
};
258 union acpi_object
*out_obj
;
262 status
= acpi_evaluate_object(ata_ap_acpi_handle(ap
), "_GTM", NULL
,
266 if (status
== AE_NOT_FOUND
)
270 if (ACPI_FAILURE(status
)) {
271 ata_port_err(ap
, "ACPI get timing mode failed (AE 0x%x)\n",
276 out_obj
= output
.pointer
;
277 if (out_obj
->type
!= ACPI_TYPE_BUFFER
) {
278 ata_port_warn(ap
, "_GTM returned unexpected object type 0x%x\n",
284 if (out_obj
->buffer
.length
!= sizeof(struct ata_acpi_gtm
)) {
285 ata_port_err(ap
, "_GTM returned invalid length %d\n",
286 out_obj
->buffer
.length
);
290 memcpy(gtm
, out_obj
->buffer
.pointer
, sizeof(struct ata_acpi_gtm
));
293 kfree(output
.pointer
);
297 EXPORT_SYMBOL_GPL(ata_acpi_gtm
);
300 * ata_acpi_stm - execute _STM
301 * @ap: target ATA port
302 * @stm: timing parameter to _STM
304 * Evaluate _STM with timing parameter @stm.
310 * 0 on success, -ENOENT if _STM doesn't exist, -errno on failure.
312 int ata_acpi_stm(struct ata_port
*ap
, const struct ata_acpi_gtm
*stm
)
315 struct ata_acpi_gtm stm_buf
= *stm
;
316 struct acpi_object_list input
;
317 union acpi_object in_params
[3];
319 in_params
[0].type
= ACPI_TYPE_BUFFER
;
320 in_params
[0].buffer
.length
= sizeof(struct ata_acpi_gtm
);
321 in_params
[0].buffer
.pointer
= (u8
*)&stm_buf
;
322 /* Buffers for id may need byteswapping ? */
323 in_params
[1].type
= ACPI_TYPE_BUFFER
;
324 in_params
[1].buffer
.length
= 512;
325 in_params
[1].buffer
.pointer
= (u8
*)ap
->link
.device
[0].id
;
326 in_params
[2].type
= ACPI_TYPE_BUFFER
;
327 in_params
[2].buffer
.length
= 512;
328 in_params
[2].buffer
.pointer
= (u8
*)ap
->link
.device
[1].id
;
331 input
.pointer
= in_params
;
333 status
= acpi_evaluate_object(ata_ap_acpi_handle(ap
), "_STM", &input
,
336 if (status
== AE_NOT_FOUND
)
338 if (ACPI_FAILURE(status
)) {
339 ata_port_err(ap
, "ACPI set timing mode failed (status=0x%x)\n",
346 EXPORT_SYMBOL_GPL(ata_acpi_stm
);
349 * ata_dev_get_GTF - get the drive bootup default taskfile settings
350 * @dev: target ATA device
351 * @gtf: output parameter for buffer containing _GTF taskfile arrays
353 * This applies to both PATA and SATA drives.
355 * The _GTF method has no input parameters.
356 * It returns a variable number of register set values (registers
357 * hex 1F1..1F7, taskfiles).
358 * The <variable number> is not known in advance, so have ACPI-CA
359 * allocate the buffer as needed and return it, then free it later.
365 * Number of taskfiles on success, 0 if _GTF doesn't exist. -EINVAL
366 * if _GTF is invalid.
368 static int ata_dev_get_GTF(struct ata_device
*dev
, struct ata_acpi_gtf
**gtf
)
370 struct ata_port
*ap
= dev
->link
->ap
;
372 struct acpi_buffer output
;
373 union acpi_object
*out_obj
;
376 /* if _GTF is cached, use the cached value */
377 if (dev
->gtf_cache
) {
378 out_obj
= dev
->gtf_cache
;
382 /* set up output buffer */
383 output
.length
= ACPI_ALLOCATE_BUFFER
;
384 output
.pointer
= NULL
; /* ACPI-CA sets this; save/free it later */
386 if (ata_msg_probe(ap
))
387 ata_dev_dbg(dev
, "%s: ENTER: port#: %d\n",
388 __func__
, ap
->port_no
);
390 /* _GTF has no input parameters */
391 status
= acpi_evaluate_object(ata_dev_acpi_handle(dev
), "_GTF", NULL
,
393 out_obj
= dev
->gtf_cache
= output
.pointer
;
395 if (ACPI_FAILURE(status
)) {
396 if (status
!= AE_NOT_FOUND
) {
397 ata_dev_warn(dev
, "_GTF evaluation failed (AE 0x%x)\n",
404 if (!output
.length
|| !output
.pointer
) {
405 if (ata_msg_probe(ap
))
406 ata_dev_dbg(dev
, "%s: Run _GTF: length or ptr is NULL (0x%llx, 0x%p)\n",
408 (unsigned long long)output
.length
,
414 if (out_obj
->type
!= ACPI_TYPE_BUFFER
) {
415 ata_dev_warn(dev
, "_GTF unexpected object type 0x%x\n",
421 if (out_obj
->buffer
.length
% REGS_PER_GTF
) {
422 ata_dev_warn(dev
, "unexpected _GTF length (%d)\n",
423 out_obj
->buffer
.length
);
429 rc
= out_obj
->buffer
.length
/ REGS_PER_GTF
;
431 *gtf
= (void *)out_obj
->buffer
.pointer
;
432 if (ata_msg_probe(ap
))
433 ata_dev_dbg(dev
, "%s: returning gtf=%p, gtf_count=%d\n",
439 ata_acpi_clear_gtf(dev
);
444 * ata_acpi_gtm_xfermode - determine xfermode from GTM parameter
445 * @dev: target device
446 * @gtm: GTM parameter to use
448 * Determine xfermask for @dev from @gtm.
454 * Determined xfermask.
456 unsigned long ata_acpi_gtm_xfermask(struct ata_device
*dev
,
457 const struct ata_acpi_gtm
*gtm
)
459 unsigned long xfer_mask
= 0;
464 /* we always use the 0 slot for crap hardware */
466 if (!(gtm
->flags
& 0x10))
470 mode
= ata_timing_cycle2mode(ATA_SHIFT_PIO
, gtm
->drive
[unit
].pio
);
471 xfer_mask
|= ata_xfer_mode2mask(mode
);
473 /* See if we have MWDMA or UDMA data. We don't bother with
474 * MWDMA if UDMA is available as this means the BIOS set UDMA
475 * and our error changedown if it works is UDMA to PIO anyway.
477 if (!(gtm
->flags
& (1 << (2 * unit
))))
478 type
= ATA_SHIFT_MWDMA
;
480 type
= ATA_SHIFT_UDMA
;
482 mode
= ata_timing_cycle2mode(type
, gtm
->drive
[unit
].dma
);
483 xfer_mask
|= ata_xfer_mode2mask(mode
);
487 EXPORT_SYMBOL_GPL(ata_acpi_gtm_xfermask
);
490 * ata_acpi_cbl_80wire - Check for 80 wire cable
492 * @gtm: GTM data to use
494 * Return 1 if the @gtm indicates the BIOS selected an 80wire mode.
496 int ata_acpi_cbl_80wire(struct ata_port
*ap
, const struct ata_acpi_gtm
*gtm
)
498 struct ata_device
*dev
;
500 ata_for_each_dev(dev
, &ap
->link
, ENABLED
) {
501 unsigned long xfer_mask
, udma_mask
;
503 xfer_mask
= ata_acpi_gtm_xfermask(dev
, gtm
);
504 ata_unpack_xfermask(xfer_mask
, NULL
, NULL
, &udma_mask
);
506 if (udma_mask
& ~ATA_UDMA_MASK_40C
)
512 EXPORT_SYMBOL_GPL(ata_acpi_cbl_80wire
);
514 static void ata_acpi_gtf_to_tf(struct ata_device
*dev
,
515 const struct ata_acpi_gtf
*gtf
,
516 struct ata_taskfile
*tf
)
518 ata_tf_init(dev
, tf
);
520 tf
->flags
|= ATA_TFLAG_ISADDR
| ATA_TFLAG_DEVICE
;
521 tf
->protocol
= ATA_PROT_NODATA
;
522 tf
->feature
= gtf
->tf
[0]; /* 0x1f1 */
523 tf
->nsect
= gtf
->tf
[1]; /* 0x1f2 */
524 tf
->lbal
= gtf
->tf
[2]; /* 0x1f3 */
525 tf
->lbam
= gtf
->tf
[3]; /* 0x1f4 */
526 tf
->lbah
= gtf
->tf
[4]; /* 0x1f5 */
527 tf
->device
= gtf
->tf
[5]; /* 0x1f6 */
528 tf
->command
= gtf
->tf
[6]; /* 0x1f7 */
531 static int ata_acpi_filter_tf(struct ata_device
*dev
,
532 const struct ata_taskfile
*tf
,
533 const struct ata_taskfile
*ptf
)
535 if (dev
->gtf_filter
& ATA_ACPI_FILTER_SETXFER
) {
536 /* libata doesn't use ACPI to configure transfer mode.
537 * It will only confuse device configuration. Skip.
539 if (tf
->command
== ATA_CMD_SET_FEATURES
&&
540 tf
->feature
== SETFEATURES_XFER
)
544 if (dev
->gtf_filter
& ATA_ACPI_FILTER_LOCK
) {
545 /* BIOS writers, sorry but we don't wanna lock
546 * features unless the user explicitly said so.
549 /* DEVICE CONFIGURATION FREEZE LOCK */
550 if (tf
->command
== ATA_CMD_CONF_OVERLAY
&&
551 tf
->feature
== ATA_DCO_FREEZE_LOCK
)
554 /* SECURITY FREEZE LOCK */
555 if (tf
->command
== ATA_CMD_SEC_FREEZE_LOCK
)
558 /* SET MAX LOCK and SET MAX FREEZE LOCK */
559 if ((!ptf
|| ptf
->command
!= ATA_CMD_READ_NATIVE_MAX
) &&
560 tf
->command
== ATA_CMD_SET_MAX
&&
561 (tf
->feature
== ATA_SET_MAX_LOCK
||
562 tf
->feature
== ATA_SET_MAX_FREEZE_LOCK
))
566 if (tf
->command
== ATA_CMD_SET_FEATURES
&&
567 tf
->feature
== SETFEATURES_SATA_ENABLE
) {
568 /* inhibit enabling DIPM */
569 if (dev
->gtf_filter
& ATA_ACPI_FILTER_DIPM
&&
570 tf
->nsect
== SATA_DIPM
)
573 /* inhibit FPDMA non-zero offset */
574 if (dev
->gtf_filter
& ATA_ACPI_FILTER_FPDMA_OFFSET
&&
575 (tf
->nsect
== SATA_FPDMA_OFFSET
||
576 tf
->nsect
== SATA_FPDMA_IN_ORDER
))
579 /* inhibit FPDMA auto activation */
580 if (dev
->gtf_filter
& ATA_ACPI_FILTER_FPDMA_AA
&&
581 tf
->nsect
== SATA_FPDMA_AA
)
589 * ata_acpi_run_tf - send taskfile registers to host controller
590 * @dev: target ATA device
591 * @gtf: raw ATA taskfile register set (0x1f1 - 0x1f7)
593 * Outputs ATA taskfile to standard ATA host controller.
594 * Writes the control, feature, nsect, lbal, lbam, and lbah registers.
595 * Optionally (ATA_TFLAG_LBA48) writes hob_feature, hob_nsect,
596 * hob_lbal, hob_lbam, and hob_lbah.
598 * This function waits for idle (!BUSY and !DRQ) after writing
599 * registers. If the control register has a new value, this
600 * function also waits for idle after writing control and before
601 * writing the remaining registers.
607 * 1 if command is executed successfully. 0 if ignored, rejected or
608 * filtered out, -errno on other errors.
610 static int ata_acpi_run_tf(struct ata_device
*dev
,
611 const struct ata_acpi_gtf
*gtf
,
612 const struct ata_acpi_gtf
*prev_gtf
)
614 struct ata_taskfile
*pptf
= NULL
;
615 struct ata_taskfile tf
, ptf
, rtf
;
616 unsigned int err_mask
;
622 if ((gtf
->tf
[0] == 0) && (gtf
->tf
[1] == 0) && (gtf
->tf
[2] == 0)
623 && (gtf
->tf
[3] == 0) && (gtf
->tf
[4] == 0) && (gtf
->tf
[5] == 0)
624 && (gtf
->tf
[6] == 0))
627 ata_acpi_gtf_to_tf(dev
, gtf
, &tf
);
629 ata_acpi_gtf_to_tf(dev
, prev_gtf
, &ptf
);
633 if (!ata_acpi_filter_tf(dev
, &tf
, pptf
)) {
635 err_mask
= ata_exec_internal(dev
, &rtf
, NULL
,
636 DMA_NONE
, NULL
, 0, 0);
641 snprintf(msg
, sizeof(msg
), "succeeded");
647 snprintf(msg
, sizeof(msg
),
648 "rejected by device (Stat=0x%02x Err=0x%02x)",
649 rtf
.command
, rtf
.feature
);
655 snprintf(msg
, sizeof(msg
),
656 "failed (Emask=0x%x Stat=0x%02x Err=0x%02x)",
657 err_mask
, rtf
.command
, rtf
.feature
);
663 snprintf(msg
, sizeof(msg
), "filtered out");
666 descr
= ata_get_cmd_descript(tf
.command
);
668 ata_dev_printk(dev
, level
,
669 "ACPI cmd %02x/%02x:%02x:%02x:%02x:%02x:%02x (%s) %s\n",
670 tf
.command
, tf
.feature
, tf
.nsect
, tf
.lbal
,
671 tf
.lbam
, tf
.lbah
, tf
.device
,
672 (descr
? descr
: "unknown"), msg
);
678 * ata_acpi_exec_tfs - get then write drive taskfile settings
679 * @dev: target ATA device
680 * @nr_executed: out parameter for the number of executed commands
682 * Evaluate _GTF and execute returned taskfiles.
688 * Number of executed taskfiles on success, 0 if _GTF doesn't exist.
689 * -errno on other errors.
691 static int ata_acpi_exec_tfs(struct ata_device
*dev
, int *nr_executed
)
693 struct ata_acpi_gtf
*gtf
= NULL
, *pgtf
= NULL
;
694 int gtf_count
, i
, rc
;
697 rc
= ata_dev_get_GTF(dev
, >f
);
703 for (i
= 0; i
< gtf_count
; i
++, gtf
++) {
704 rc
= ata_acpi_run_tf(dev
, gtf
, pgtf
);
713 ata_acpi_clear_gtf(dev
);
721 * ata_acpi_push_id - send Identify data to drive
722 * @dev: target ATA device
724 * _SDD ACPI object: for SATA mode only
725 * Must be after Identify (Packet) Device -- uses its data
726 * ATM this function never returns a failure. It is an optional
727 * method and if it fails for whatever reason, we should still
734 * 0 on success, -ENOENT if _SDD doesn't exist, -errno on failure.
736 static int ata_acpi_push_id(struct ata_device
*dev
)
738 struct ata_port
*ap
= dev
->link
->ap
;
740 struct acpi_object_list input
;
741 union acpi_object in_params
[1];
743 if (ata_msg_probe(ap
))
744 ata_dev_dbg(dev
, "%s: ix = %d, port#: %d\n",
745 __func__
, dev
->devno
, ap
->port_no
);
747 /* Give the drive Identify data to the drive via the _SDD method */
748 /* _SDD: set up input parameters */
750 input
.pointer
= in_params
;
751 in_params
[0].type
= ACPI_TYPE_BUFFER
;
752 in_params
[0].buffer
.length
= sizeof(dev
->id
[0]) * ATA_ID_WORDS
;
753 in_params
[0].buffer
.pointer
= (u8
*)dev
->id
;
754 /* Output buffer: _SDD has no output */
756 /* It's OK for _SDD to be missing too. */
757 swap_buf_le16(dev
->id
, ATA_ID_WORDS
);
758 status
= acpi_evaluate_object(ata_dev_acpi_handle(dev
), "_SDD", &input
,
760 swap_buf_le16(dev
->id
, ATA_ID_WORDS
);
762 if (status
== AE_NOT_FOUND
)
765 if (ACPI_FAILURE(status
)) {
766 ata_dev_warn(dev
, "ACPI _SDD failed (AE 0x%x)\n", status
);
774 * ata_acpi_on_suspend - ATA ACPI hook called on suspend
775 * @ap: target ATA port
777 * This function is called when @ap is about to be suspended. All
778 * devices are already put to sleep but the port_suspend() callback
779 * hasn't been executed yet. Error return from this function aborts
786 * 0 on success, -errno on failure.
788 int ata_acpi_on_suspend(struct ata_port
*ap
)
795 * ata_acpi_on_resume - ATA ACPI hook called on resume
796 * @ap: target ATA port
798 * This function is called when @ap is resumed - right after port
799 * itself is resumed but before any EH action is taken.
804 void ata_acpi_on_resume(struct ata_port
*ap
)
806 const struct ata_acpi_gtm
*gtm
= ata_acpi_init_gtm(ap
);
807 struct ata_device
*dev
;
809 if (ata_ap_acpi_handle(ap
) && gtm
) {
812 /* restore timing parameters */
813 ata_acpi_stm(ap
, gtm
);
815 /* _GTF should immediately follow _STM so that it can
816 * use values set by _STM. Cache _GTF result and
819 ata_for_each_dev(dev
, &ap
->link
, ALL
) {
820 ata_acpi_clear_gtf(dev
);
821 if (ata_dev_enabled(dev
) &&
822 ata_dev_get_GTF(dev
, NULL
) >= 0)
823 dev
->flags
|= ATA_DFLAG_ACPI_PENDING
;
826 /* SATA _GTF needs to be evaulated after _SDD and
827 * there's no reason to evaluate IDE _GTF early
828 * without _STM. Clear cache and schedule _GTF.
830 ata_for_each_dev(dev
, &ap
->link
, ALL
) {
831 ata_acpi_clear_gtf(dev
);
832 if (ata_dev_enabled(dev
))
833 dev
->flags
|= ATA_DFLAG_ACPI_PENDING
;
839 * ata_acpi_set_state - set the port power state
840 * @ap: target ATA port
841 * @state: state, on/off
843 * This function executes the _PS0/_PS3 ACPI method to set the power state.
844 * ACPI spec requires _PS0 when IDE power on and _PS3 when power off
846 void ata_acpi_set_state(struct ata_port
*ap
, pm_message_t state
)
848 struct ata_device
*dev
;
852 /* channel first and then drives for power on and vica versa
854 handle
= ata_ap_acpi_handle(ap
);
855 if (handle
&& state
.event
== PM_EVENT_ON
)
856 acpi_bus_set_power(handle
, ACPI_STATE_D0
);
858 ata_for_each_dev(dev
, &ap
->link
, ENABLED
) {
859 handle
= ata_dev_acpi_handle(dev
);
863 if (state
.event
!= PM_EVENT_ON
) {
864 acpi_state
= acpi_pm_device_sleep_state(
865 &dev
->sdev
->sdev_gendev
, NULL
, ACPI_STATE_D3
);
867 acpi_bus_set_power(handle
, acpi_state
);
868 /* TBD: need to check if it's runtime pm request */
869 acpi_pm_device_run_wake(
870 &dev
->sdev
->sdev_gendev
, true);
873 acpi_pm_device_run_wake(
874 &dev
->sdev
->sdev_gendev
, false);
875 acpi_bus_set_power(handle
, ACPI_STATE_D0
);
879 handle
= ata_ap_acpi_handle(ap
);
880 if (handle
&& state
.event
!= PM_EVENT_ON
)
881 acpi_bus_set_power(handle
, ACPI_STATE_D3
);
885 * ata_acpi_on_devcfg - ATA ACPI hook called on device donfiguration
886 * @dev: target ATA device
888 * This function is called when @dev is about to be configured.
889 * IDENTIFY data might have been modified after this hook is run.
895 * Positive number if IDENTIFY data needs to be refreshed, 0 if not,
898 int ata_acpi_on_devcfg(struct ata_device
*dev
)
900 struct ata_port
*ap
= dev
->link
->ap
;
901 struct ata_eh_context
*ehc
= &ap
->link
.eh_context
;
902 int acpi_sata
= ap
->flags
& ATA_FLAG_ACPI_SATA
;
906 if (!ata_dev_acpi_handle(dev
))
909 /* do we need to do _GTF? */
910 if (!(dev
->flags
& ATA_DFLAG_ACPI_PENDING
) &&
911 !(acpi_sata
&& (ehc
->i
.flags
& ATA_EHI_DID_HARDRESET
)))
914 /* do _SDD if SATA */
916 rc
= ata_acpi_push_id(dev
);
917 if (rc
&& rc
!= -ENOENT
)
922 rc
= ata_acpi_exec_tfs(dev
, &nr_executed
);
926 dev
->flags
&= ~ATA_DFLAG_ACPI_PENDING
;
928 /* refresh IDENTIFY page if any _GTF command has been executed */
930 rc
= ata_dev_reread_id(dev
, 0);
933 "failed to IDENTIFY after ACPI commands\n");
941 /* ignore evaluation failure if we can continue safely */
942 if (rc
== -EINVAL
&& !nr_executed
&& !(ap
->pflags
& ATA_PFLAG_FROZEN
))
945 /* fail and let EH retry once more for unknown IO errors */
946 if (!(dev
->flags
& ATA_DFLAG_ACPI_FAILED
)) {
947 dev
->flags
|= ATA_DFLAG_ACPI_FAILED
;
951 dev
->flags
|= ATA_DFLAG_ACPI_DISABLED
;
952 ata_dev_warn(dev
, "ACPI: failed the second time, disabled\n");
954 /* We can safely continue if no _GTF command has been executed
955 * and port is not frozen.
957 if (!nr_executed
&& !(ap
->pflags
& ATA_PFLAG_FROZEN
))
964 * ata_acpi_on_disable - ATA ACPI hook called when a device is disabled
965 * @dev: target ATA device
967 * This function is called when @dev is about to be disabled.
972 void ata_acpi_on_disable(struct ata_device
*dev
)
974 ata_acpi_clear_gtf(dev
);
977 static void ata_acpi_wake_dev(acpi_handle handle
, u32 event
, void *context
)
979 struct ata_device
*ata_dev
= context
;
981 if (event
== ACPI_NOTIFY_DEVICE_WAKE
&& ata_dev
&&
982 pm_runtime_suspended(&ata_dev
->sdev
->sdev_gendev
))
983 scsi_autopm_get_device(ata_dev
->sdev
);
986 static void ata_acpi_add_pm_notifier(struct ata_device
*dev
)
988 struct acpi_device
*acpi_dev
;
992 handle
= ata_dev_acpi_handle(dev
);
996 status
= acpi_bus_get_device(handle
, &acpi_dev
);
997 if (ACPI_FAILURE(status
))
1000 if (dev
->sdev
->can_power_off
) {
1001 acpi_install_notify_handler(handle
, ACPI_SYSTEM_NOTIFY
,
1002 ata_acpi_wake_dev
, dev
);
1003 device_set_run_wake(&dev
->sdev
->sdev_gendev
, true);
1007 static void ata_acpi_remove_pm_notifier(struct ata_device
*dev
)
1009 struct acpi_device
*acpi_dev
;
1013 handle
= ata_dev_acpi_handle(dev
);
1017 status
= acpi_bus_get_device(handle
, &acpi_dev
);
1018 if (ACPI_FAILURE(status
))
1021 if (dev
->sdev
->can_power_off
) {
1022 device_set_run_wake(&dev
->sdev
->sdev_gendev
, false);
1023 acpi_remove_notify_handler(handle
, ACPI_SYSTEM_NOTIFY
,
1028 static void ata_acpi_register_power_resource(struct ata_device
*dev
)
1030 struct scsi_device
*sdev
= dev
->sdev
;
1032 struct device
*device
;
1034 handle
= ata_dev_acpi_handle(dev
);
1038 device
= &sdev
->sdev_gendev
;
1040 acpi_power_resource_register_device(device
, handle
);
1043 static void ata_acpi_unregister_power_resource(struct ata_device
*dev
)
1045 struct scsi_device
*sdev
= dev
->sdev
;
1047 struct device
*device
;
1049 handle
= ata_dev_acpi_handle(dev
);
1053 device
= &sdev
->sdev_gendev
;
1055 acpi_power_resource_unregister_device(device
, handle
);
1058 void ata_acpi_bind(struct ata_device
*dev
)
1060 ata_acpi_add_pm_notifier(dev
);
1061 ata_acpi_register_power_resource(dev
);
1064 void ata_acpi_unbind(struct ata_device
*dev
)
1066 ata_acpi_remove_pm_notifier(dev
);
1067 ata_acpi_unregister_power_resource(dev
);
1070 static int compat_pci_ata(struct ata_port
*ap
)
1072 struct device
*dev
= ap
->tdev
.parent
;
1073 struct pci_dev
*pdev
;
1075 if (!is_pci_dev(dev
))
1078 pdev
= to_pci_dev(dev
);
1080 if ((pdev
->class >> 8) != PCI_CLASS_STORAGE_SATA
&&
1081 (pdev
->class >> 8) != PCI_CLASS_STORAGE_IDE
)
1087 static int ata_acpi_bind_host(struct ata_port
*ap
, acpi_handle
*handle
)
1089 if (ap
->flags
& ATA_FLAG_ACPI_SATA
)
1092 *handle
= acpi_get_child(DEVICE_ACPI_HANDLE(ap
->tdev
.parent
),
1098 if (ata_acpi_gtm(ap
, &ap
->__acpi_init_gtm
) == 0)
1099 ap
->pflags
|= ATA_PFLAG_INIT_GTM_VALID
;
1104 static int ata_acpi_bind_device(struct ata_port
*ap
, struct scsi_device
*sdev
,
1105 acpi_handle
*handle
)
1107 struct ata_device
*ata_dev
;
1109 struct acpi_device
*acpi_dev
;
1110 struct acpi_device_power_state
*states
;
1112 if (ap
->flags
& ATA_FLAG_ACPI_SATA
) {
1113 if (!sata_pmp_attached(ap
))
1114 ata_dev
= &ap
->link
.device
[sdev
->id
];
1116 ata_dev
= &ap
->pmp_link
[sdev
->channel
].device
[sdev
->id
];
1119 ata_dev
= &ap
->link
.device
[sdev
->id
];
1122 *handle
= ata_dev_acpi_handle(ata_dev
);
1127 status
= acpi_bus_get_device(*handle
, &acpi_dev
);
1128 if (ACPI_FAILURE(status
))
1132 * If firmware has _PS3 or _PR3 for this device,
1133 * and this ata ODD device support device attention,
1134 * it means this device can be powered off
1136 states
= acpi_dev
->power
.states
;
1137 if ((states
[ACPI_STATE_D3_HOT
].flags
.valid
||
1138 states
[ACPI_STATE_D3_COLD
].flags
.explicit_set
) &&
1139 ata_dev
->flags
& ATA_DFLAG_DA
)
1140 sdev
->can_power_off
= 1;
1145 static int is_ata_port(const struct device
*dev
)
1147 return dev
->type
== &ata_port_type
;
1150 static struct ata_port
*dev_to_ata_port(struct device
*dev
)
1152 while (!is_ata_port(dev
)) {
1157 return to_ata_port(dev
);
1160 static int ata_acpi_find_device(struct device
*dev
, acpi_handle
*handle
)
1162 struct ata_port
*ap
= dev_to_ata_port(dev
);
1167 if (!compat_pci_ata(ap
))
1170 if (scsi_is_host_device(dev
))
1171 return ata_acpi_bind_host(ap
, handle
);
1172 else if (scsi_is_sdev_device(dev
)) {
1173 struct scsi_device
*sdev
= to_scsi_device(dev
);
1175 return ata_acpi_bind_device(ap
, sdev
, handle
);
1180 static int ata_acpi_find_dummy(struct device
*dev
, acpi_handle
*handle
)
1185 static struct acpi_bus_type ata_acpi_bus
= {
1186 .find_bridge
= ata_acpi_find_dummy
,
1187 .find_device
= ata_acpi_find_device
,
1190 int ata_acpi_register(void)
1192 return scsi_register_acpi_bus_type(&ata_acpi_bus
);
1195 void ata_acpi_unregister(void)
1197 scsi_unregister_acpi_bus_type(&ata_acpi_bus
);