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 */
37 static void ata_acpi_clear_gtf(struct ata_device
*dev
)
39 kfree(dev
->gtf_cache
);
40 dev
->gtf_cache
= NULL
;
44 * ata_dev_acpi_handle - provide the acpi_handle for an ata_device
45 * @dev: the acpi_handle returned will correspond to this device
47 * Returns the acpi_handle for the ACPI namespace object corresponding to
48 * the ata_device passed into the function, or NULL if no such object exists
49 * or ACPI is disabled for this device due to consecutive errors.
51 acpi_handle
ata_dev_acpi_handle(struct ata_device
*dev
)
53 return dev
->flags
& ATA_DFLAG_ACPI_DISABLED
?
54 NULL
: ACPI_HANDLE(&dev
->tdev
);
57 /* @ap and @dev are the same as ata_acpi_handle_hotplug() */
58 static void ata_acpi_detach_device(struct ata_port
*ap
, struct ata_device
*dev
)
61 dev
->flags
|= ATA_DFLAG_DETACH
;
63 struct ata_link
*tlink
;
64 struct ata_device
*tdev
;
66 ata_for_each_link(tlink
, ap
, EDGE
)
67 ata_for_each_dev(tdev
, tlink
, ALL
)
68 tdev
->flags
|= ATA_DFLAG_DETACH
;
71 ata_port_schedule_eh(ap
);
75 * ata_acpi_handle_hotplug - ACPI event handler backend
76 * @ap: ATA port ACPI event occurred
77 * @dev: ATA device ACPI event occurred (can be NULL)
78 * @event: ACPI event which occurred
80 * All ACPI bay / device realted events end up in this function. If
81 * the event is port-wide @dev is NULL. If the event is specific to a
82 * device, @dev points to it.
84 * Hotplug (as opposed to unplug) notification is always handled as
85 * port-wide while unplug only kills the target device on device-wide
89 * ACPI notify handler context. May sleep.
91 static void ata_acpi_handle_hotplug(struct ata_port
*ap
, struct ata_device
*dev
,
94 struct ata_eh_info
*ehi
= &ap
->link
.eh_info
;
98 spin_lock_irqsave(ap
->lock
, flags
);
100 * When dock driver calls into the routine, it will always use
101 * ACPI_NOTIFY_BUS_CHECK/ACPI_NOTIFY_DEVICE_CHECK for add and
102 * ACPI_NOTIFY_EJECT_REQUEST for remove
105 case ACPI_NOTIFY_BUS_CHECK
:
106 case ACPI_NOTIFY_DEVICE_CHECK
:
107 ata_ehi_push_desc(ehi
, "ACPI event");
109 ata_ehi_hotplugged(ehi
);
112 case ACPI_NOTIFY_EJECT_REQUEST
:
113 ata_ehi_push_desc(ehi
, "ACPI event");
115 ata_acpi_detach_device(ap
, dev
);
120 spin_unlock_irqrestore(ap
->lock
, flags
);
123 ata_port_wait_eh(ap
);
126 static void ata_acpi_dev_notify_dock(acpi_handle handle
, u32 event
, void *data
)
128 struct ata_device
*dev
= data
;
130 ata_acpi_handle_hotplug(dev
->link
->ap
, dev
, event
);
133 static void ata_acpi_ap_notify_dock(acpi_handle handle
, u32 event
, void *data
)
135 struct ata_port
*ap
= data
;
137 ata_acpi_handle_hotplug(ap
, NULL
, event
);
140 static void ata_acpi_uevent(struct ata_port
*ap
, struct ata_device
*dev
,
143 struct kobject
*kobj
= NULL
;
144 char event_string
[20];
145 char *envp
[] = { event_string
, NULL
};
149 kobj
= &dev
->sdev
->sdev_gendev
.kobj
;
151 kobj
= &ap
->dev
->kobj
;
154 snprintf(event_string
, 20, "BAY_EVENT=%d", event
);
155 kobject_uevent_env(kobj
, KOBJ_CHANGE
, envp
);
159 static void ata_acpi_ap_uevent(acpi_handle handle
, u32 event
, void *data
)
161 ata_acpi_uevent(data
, NULL
, event
);
164 static void ata_acpi_dev_uevent(acpi_handle handle
, u32 event
, void *data
)
166 struct ata_device
*dev
= data
;
167 ata_acpi_uevent(dev
->link
->ap
, dev
, event
);
170 static const struct acpi_dock_ops ata_acpi_dev_dock_ops
= {
171 .handler
= ata_acpi_dev_notify_dock
,
172 .uevent
= ata_acpi_dev_uevent
,
175 static const struct acpi_dock_ops ata_acpi_ap_dock_ops
= {
176 .handler
= ata_acpi_ap_notify_dock
,
177 .uevent
= ata_acpi_ap_uevent
,
180 /* bind acpi handle to pata port */
181 void ata_acpi_bind_port(struct ata_port
*ap
)
183 acpi_handle host_handle
= ACPI_HANDLE(ap
->host
->dev
);
185 if (libata_noacpi
|| ap
->flags
& ATA_FLAG_ACPI_SATA
|| !host_handle
)
188 ACPI_HANDLE_SET(&ap
->tdev
, acpi_get_child(host_handle
, ap
->port_no
));
190 if (ata_acpi_gtm(ap
, &ap
->__acpi_init_gtm
) == 0)
191 ap
->pflags
|= ATA_PFLAG_INIT_GTM_VALID
;
193 /* we might be on a docking station */
194 register_hotplug_dock_device(ACPI_HANDLE(&ap
->tdev
),
195 &ata_acpi_ap_dock_ops
, ap
, NULL
, NULL
);
198 void ata_acpi_bind_dev(struct ata_device
*dev
)
200 struct ata_port
*ap
= dev
->link
->ap
;
201 acpi_handle port_handle
= ACPI_HANDLE(&ap
->tdev
);
202 acpi_handle host_handle
= ACPI_HANDLE(ap
->host
->dev
);
203 acpi_handle parent_handle
;
207 * For both sata/pata devices, host handle is required.
208 * For pata device, port handle is also required.
210 if (libata_noacpi
|| !host_handle
||
211 (!(ap
->flags
& ATA_FLAG_ACPI_SATA
) && !port_handle
))
214 if (ap
->flags
& ATA_FLAG_ACPI_SATA
) {
215 if (!sata_pmp_attached(ap
))
216 adr
= SATA_ADR(ap
->port_no
, NO_PORT_MULT
);
218 adr
= SATA_ADR(ap
->port_no
, dev
->link
->pmp
);
219 parent_handle
= host_handle
;
222 parent_handle
= port_handle
;
225 ACPI_HANDLE_SET(&dev
->tdev
, acpi_get_child(parent_handle
, adr
));
227 register_hotplug_dock_device(ata_dev_acpi_handle(dev
),
228 &ata_acpi_dev_dock_ops
, dev
, NULL
, NULL
);
232 * ata_acpi_dissociate - dissociate ATA host from ACPI objects
233 * @host: target ATA host
235 * This function is called during driver detach after the whole host
241 void ata_acpi_dissociate(struct ata_host
*host
)
245 /* Restore initial _GTM values so that driver which attaches
246 * afterward can use them too.
248 for (i
= 0; i
< host
->n_ports
; i
++) {
249 struct ata_port
*ap
= host
->ports
[i
];
250 const struct ata_acpi_gtm
*gtm
= ata_acpi_init_gtm(ap
);
252 if (ACPI_HANDLE(&ap
->tdev
) && gtm
)
253 ata_acpi_stm(ap
, gtm
);
258 * ata_acpi_gtm - execute _GTM
259 * @ap: target ATA port
260 * @gtm: out parameter for _GTM result
262 * Evaluate _GTM and store the result in @gtm.
268 * 0 on success, -ENOENT if _GTM doesn't exist, -errno on failure.
270 int ata_acpi_gtm(struct ata_port
*ap
, struct ata_acpi_gtm
*gtm
)
272 struct acpi_buffer output
= { .length
= ACPI_ALLOCATE_BUFFER
};
273 union acpi_object
*out_obj
;
276 acpi_handle handle
= ACPI_HANDLE(&ap
->tdev
);
281 status
= acpi_evaluate_object(handle
, "_GTM", NULL
, &output
);
284 if (status
== AE_NOT_FOUND
)
288 if (ACPI_FAILURE(status
)) {
289 ata_port_err(ap
, "ACPI get timing mode failed (AE 0x%x)\n",
294 out_obj
= output
.pointer
;
295 if (out_obj
->type
!= ACPI_TYPE_BUFFER
) {
296 ata_port_warn(ap
, "_GTM returned unexpected object type 0x%x\n",
302 if (out_obj
->buffer
.length
!= sizeof(struct ata_acpi_gtm
)) {
303 ata_port_err(ap
, "_GTM returned invalid length %d\n",
304 out_obj
->buffer
.length
);
308 memcpy(gtm
, out_obj
->buffer
.pointer
, sizeof(struct ata_acpi_gtm
));
311 kfree(output
.pointer
);
315 EXPORT_SYMBOL_GPL(ata_acpi_gtm
);
318 * ata_acpi_stm - execute _STM
319 * @ap: target ATA port
320 * @stm: timing parameter to _STM
322 * Evaluate _STM with timing parameter @stm.
328 * 0 on success, -ENOENT if _STM doesn't exist, -errno on failure.
330 int ata_acpi_stm(struct ata_port
*ap
, const struct ata_acpi_gtm
*stm
)
333 struct ata_acpi_gtm stm_buf
= *stm
;
334 struct acpi_object_list input
;
335 union acpi_object in_params
[3];
337 in_params
[0].type
= ACPI_TYPE_BUFFER
;
338 in_params
[0].buffer
.length
= sizeof(struct ata_acpi_gtm
);
339 in_params
[0].buffer
.pointer
= (u8
*)&stm_buf
;
340 /* Buffers for id may need byteswapping ? */
341 in_params
[1].type
= ACPI_TYPE_BUFFER
;
342 in_params
[1].buffer
.length
= 512;
343 in_params
[1].buffer
.pointer
= (u8
*)ap
->link
.device
[0].id
;
344 in_params
[2].type
= ACPI_TYPE_BUFFER
;
345 in_params
[2].buffer
.length
= 512;
346 in_params
[2].buffer
.pointer
= (u8
*)ap
->link
.device
[1].id
;
349 input
.pointer
= in_params
;
351 status
= acpi_evaluate_object(ACPI_HANDLE(&ap
->tdev
), "_STM",
354 if (status
== AE_NOT_FOUND
)
356 if (ACPI_FAILURE(status
)) {
357 ata_port_err(ap
, "ACPI set timing mode failed (status=0x%x)\n",
364 EXPORT_SYMBOL_GPL(ata_acpi_stm
);
367 * ata_dev_get_GTF - get the drive bootup default taskfile settings
368 * @dev: target ATA device
369 * @gtf: output parameter for buffer containing _GTF taskfile arrays
371 * This applies to both PATA and SATA drives.
373 * The _GTF method has no input parameters.
374 * It returns a variable number of register set values (registers
375 * hex 1F1..1F7, taskfiles).
376 * The <variable number> is not known in advance, so have ACPI-CA
377 * allocate the buffer as needed and return it, then free it later.
383 * Number of taskfiles on success, 0 if _GTF doesn't exist. -EINVAL
384 * if _GTF is invalid.
386 static int ata_dev_get_GTF(struct ata_device
*dev
, struct ata_acpi_gtf
**gtf
)
388 struct ata_port
*ap
= dev
->link
->ap
;
390 struct acpi_buffer output
;
391 union acpi_object
*out_obj
;
394 /* if _GTF is cached, use the cached value */
395 if (dev
->gtf_cache
) {
396 out_obj
= dev
->gtf_cache
;
400 /* set up output buffer */
401 output
.length
= ACPI_ALLOCATE_BUFFER
;
402 output
.pointer
= NULL
; /* ACPI-CA sets this; save/free it later */
404 if (ata_msg_probe(ap
))
405 ata_dev_dbg(dev
, "%s: ENTER: port#: %d\n",
406 __func__
, ap
->port_no
);
408 /* _GTF has no input parameters */
409 status
= acpi_evaluate_object(ata_dev_acpi_handle(dev
), "_GTF", NULL
,
411 out_obj
= dev
->gtf_cache
= output
.pointer
;
413 if (ACPI_FAILURE(status
)) {
414 if (status
!= AE_NOT_FOUND
) {
415 ata_dev_warn(dev
, "_GTF evaluation failed (AE 0x%x)\n",
422 if (!output
.length
|| !output
.pointer
) {
423 if (ata_msg_probe(ap
))
424 ata_dev_dbg(dev
, "%s: Run _GTF: length or ptr is NULL (0x%llx, 0x%p)\n",
426 (unsigned long long)output
.length
,
432 if (out_obj
->type
!= ACPI_TYPE_BUFFER
) {
433 ata_dev_warn(dev
, "_GTF unexpected object type 0x%x\n",
439 if (out_obj
->buffer
.length
% REGS_PER_GTF
) {
440 ata_dev_warn(dev
, "unexpected _GTF length (%d)\n",
441 out_obj
->buffer
.length
);
447 rc
= out_obj
->buffer
.length
/ REGS_PER_GTF
;
449 *gtf
= (void *)out_obj
->buffer
.pointer
;
450 if (ata_msg_probe(ap
))
451 ata_dev_dbg(dev
, "%s: returning gtf=%p, gtf_count=%d\n",
457 ata_acpi_clear_gtf(dev
);
462 * ata_acpi_gtm_xfermode - determine xfermode from GTM parameter
463 * @dev: target device
464 * @gtm: GTM parameter to use
466 * Determine xfermask for @dev from @gtm.
472 * Determined xfermask.
474 unsigned long ata_acpi_gtm_xfermask(struct ata_device
*dev
,
475 const struct ata_acpi_gtm
*gtm
)
477 unsigned long xfer_mask
= 0;
482 /* we always use the 0 slot for crap hardware */
484 if (!(gtm
->flags
& 0x10))
488 mode
= ata_timing_cycle2mode(ATA_SHIFT_PIO
, gtm
->drive
[unit
].pio
);
489 xfer_mask
|= ata_xfer_mode2mask(mode
);
491 /* See if we have MWDMA or UDMA data. We don't bother with
492 * MWDMA if UDMA is available as this means the BIOS set UDMA
493 * and our error changedown if it works is UDMA to PIO anyway.
495 if (!(gtm
->flags
& (1 << (2 * unit
))))
496 type
= ATA_SHIFT_MWDMA
;
498 type
= ATA_SHIFT_UDMA
;
500 mode
= ata_timing_cycle2mode(type
, gtm
->drive
[unit
].dma
);
501 xfer_mask
|= ata_xfer_mode2mask(mode
);
505 EXPORT_SYMBOL_GPL(ata_acpi_gtm_xfermask
);
508 * ata_acpi_cbl_80wire - Check for 80 wire cable
510 * @gtm: GTM data to use
512 * Return 1 if the @gtm indicates the BIOS selected an 80wire mode.
514 int ata_acpi_cbl_80wire(struct ata_port
*ap
, const struct ata_acpi_gtm
*gtm
)
516 struct ata_device
*dev
;
518 ata_for_each_dev(dev
, &ap
->link
, ENABLED
) {
519 unsigned long xfer_mask
, udma_mask
;
521 xfer_mask
= ata_acpi_gtm_xfermask(dev
, gtm
);
522 ata_unpack_xfermask(xfer_mask
, NULL
, NULL
, &udma_mask
);
524 if (udma_mask
& ~ATA_UDMA_MASK_40C
)
530 EXPORT_SYMBOL_GPL(ata_acpi_cbl_80wire
);
532 static void ata_acpi_gtf_to_tf(struct ata_device
*dev
,
533 const struct ata_acpi_gtf
*gtf
,
534 struct ata_taskfile
*tf
)
536 ata_tf_init(dev
, tf
);
538 tf
->flags
|= ATA_TFLAG_ISADDR
| ATA_TFLAG_DEVICE
;
539 tf
->protocol
= ATA_PROT_NODATA
;
540 tf
->feature
= gtf
->tf
[0]; /* 0x1f1 */
541 tf
->nsect
= gtf
->tf
[1]; /* 0x1f2 */
542 tf
->lbal
= gtf
->tf
[2]; /* 0x1f3 */
543 tf
->lbam
= gtf
->tf
[3]; /* 0x1f4 */
544 tf
->lbah
= gtf
->tf
[4]; /* 0x1f5 */
545 tf
->device
= gtf
->tf
[5]; /* 0x1f6 */
546 tf
->command
= gtf
->tf
[6]; /* 0x1f7 */
549 static int ata_acpi_filter_tf(struct ata_device
*dev
,
550 const struct ata_taskfile
*tf
,
551 const struct ata_taskfile
*ptf
)
553 if (dev
->gtf_filter
& ATA_ACPI_FILTER_SETXFER
) {
554 /* libata doesn't use ACPI to configure transfer mode.
555 * It will only confuse device configuration. Skip.
557 if (tf
->command
== ATA_CMD_SET_FEATURES
&&
558 tf
->feature
== SETFEATURES_XFER
)
562 if (dev
->gtf_filter
& ATA_ACPI_FILTER_LOCK
) {
563 /* BIOS writers, sorry but we don't wanna lock
564 * features unless the user explicitly said so.
567 /* DEVICE CONFIGURATION FREEZE LOCK */
568 if (tf
->command
== ATA_CMD_CONF_OVERLAY
&&
569 tf
->feature
== ATA_DCO_FREEZE_LOCK
)
572 /* SECURITY FREEZE LOCK */
573 if (tf
->command
== ATA_CMD_SEC_FREEZE_LOCK
)
576 /* SET MAX LOCK and SET MAX FREEZE LOCK */
577 if ((!ptf
|| ptf
->command
!= ATA_CMD_READ_NATIVE_MAX
) &&
578 tf
->command
== ATA_CMD_SET_MAX
&&
579 (tf
->feature
== ATA_SET_MAX_LOCK
||
580 tf
->feature
== ATA_SET_MAX_FREEZE_LOCK
))
584 if (tf
->command
== ATA_CMD_SET_FEATURES
&&
585 tf
->feature
== SETFEATURES_SATA_ENABLE
) {
586 /* inhibit enabling DIPM */
587 if (dev
->gtf_filter
& ATA_ACPI_FILTER_DIPM
&&
588 tf
->nsect
== SATA_DIPM
)
591 /* inhibit FPDMA non-zero offset */
592 if (dev
->gtf_filter
& ATA_ACPI_FILTER_FPDMA_OFFSET
&&
593 (tf
->nsect
== SATA_FPDMA_OFFSET
||
594 tf
->nsect
== SATA_FPDMA_IN_ORDER
))
597 /* inhibit FPDMA auto activation */
598 if (dev
->gtf_filter
& ATA_ACPI_FILTER_FPDMA_AA
&&
599 tf
->nsect
== SATA_FPDMA_AA
)
607 * ata_acpi_run_tf - send taskfile registers to host controller
608 * @dev: target ATA device
609 * @gtf: raw ATA taskfile register set (0x1f1 - 0x1f7)
611 * Outputs ATA taskfile to standard ATA host controller.
612 * Writes the control, feature, nsect, lbal, lbam, and lbah registers.
613 * Optionally (ATA_TFLAG_LBA48) writes hob_feature, hob_nsect,
614 * hob_lbal, hob_lbam, and hob_lbah.
616 * This function waits for idle (!BUSY and !DRQ) after writing
617 * registers. If the control register has a new value, this
618 * function also waits for idle after writing control and before
619 * writing the remaining registers.
625 * 1 if command is executed successfully. 0 if ignored, rejected or
626 * filtered out, -errno on other errors.
628 static int ata_acpi_run_tf(struct ata_device
*dev
,
629 const struct ata_acpi_gtf
*gtf
,
630 const struct ata_acpi_gtf
*prev_gtf
)
632 struct ata_taskfile
*pptf
= NULL
;
633 struct ata_taskfile tf
, ptf
, rtf
;
634 unsigned int err_mask
;
640 if ((gtf
->tf
[0] == 0) && (gtf
->tf
[1] == 0) && (gtf
->tf
[2] == 0)
641 && (gtf
->tf
[3] == 0) && (gtf
->tf
[4] == 0) && (gtf
->tf
[5] == 0)
642 && (gtf
->tf
[6] == 0))
645 ata_acpi_gtf_to_tf(dev
, gtf
, &tf
);
647 ata_acpi_gtf_to_tf(dev
, prev_gtf
, &ptf
);
651 if (!ata_acpi_filter_tf(dev
, &tf
, pptf
)) {
653 err_mask
= ata_exec_internal(dev
, &rtf
, NULL
,
654 DMA_NONE
, NULL
, 0, 0);
659 snprintf(msg
, sizeof(msg
), "succeeded");
665 snprintf(msg
, sizeof(msg
),
666 "rejected by device (Stat=0x%02x Err=0x%02x)",
667 rtf
.command
, rtf
.feature
);
673 snprintf(msg
, sizeof(msg
),
674 "failed (Emask=0x%x Stat=0x%02x Err=0x%02x)",
675 err_mask
, rtf
.command
, rtf
.feature
);
681 snprintf(msg
, sizeof(msg
), "filtered out");
684 descr
= ata_get_cmd_descript(tf
.command
);
686 ata_dev_printk(dev
, level
,
687 "ACPI cmd %02x/%02x:%02x:%02x:%02x:%02x:%02x (%s) %s\n",
688 tf
.command
, tf
.feature
, tf
.nsect
, tf
.lbal
,
689 tf
.lbam
, tf
.lbah
, tf
.device
,
690 (descr
? descr
: "unknown"), msg
);
696 * ata_acpi_exec_tfs - get then write drive taskfile settings
697 * @dev: target ATA device
698 * @nr_executed: out parameter for the number of executed commands
700 * Evaluate _GTF and execute returned taskfiles.
706 * Number of executed taskfiles on success, 0 if _GTF doesn't exist.
707 * -errno on other errors.
709 static int ata_acpi_exec_tfs(struct ata_device
*dev
, int *nr_executed
)
711 struct ata_acpi_gtf
*gtf
= NULL
, *pgtf
= NULL
;
712 int gtf_count
, i
, rc
;
715 rc
= ata_dev_get_GTF(dev
, >f
);
721 for (i
= 0; i
< gtf_count
; i
++, gtf
++) {
722 rc
= ata_acpi_run_tf(dev
, gtf
, pgtf
);
731 ata_acpi_clear_gtf(dev
);
739 * ata_acpi_push_id - send Identify data to drive
740 * @dev: target ATA device
742 * _SDD ACPI object: for SATA mode only
743 * Must be after Identify (Packet) Device -- uses its data
744 * ATM this function never returns a failure. It is an optional
745 * method and if it fails for whatever reason, we should still
752 * 0 on success, -ENOENT if _SDD doesn't exist, -errno on failure.
754 static int ata_acpi_push_id(struct ata_device
*dev
)
756 struct ata_port
*ap
= dev
->link
->ap
;
758 struct acpi_object_list input
;
759 union acpi_object in_params
[1];
761 if (ata_msg_probe(ap
))
762 ata_dev_dbg(dev
, "%s: ix = %d, port#: %d\n",
763 __func__
, dev
->devno
, ap
->port_no
);
765 /* Give the drive Identify data to the drive via the _SDD method */
766 /* _SDD: set up input parameters */
768 input
.pointer
= in_params
;
769 in_params
[0].type
= ACPI_TYPE_BUFFER
;
770 in_params
[0].buffer
.length
= sizeof(dev
->id
[0]) * ATA_ID_WORDS
;
771 in_params
[0].buffer
.pointer
= (u8
*)dev
->id
;
772 /* Output buffer: _SDD has no output */
774 /* It's OK for _SDD to be missing too. */
775 swap_buf_le16(dev
->id
, ATA_ID_WORDS
);
776 status
= acpi_evaluate_object(ata_dev_acpi_handle(dev
), "_SDD", &input
,
778 swap_buf_le16(dev
->id
, ATA_ID_WORDS
);
780 if (status
== AE_NOT_FOUND
)
783 if (ACPI_FAILURE(status
)) {
784 ata_dev_warn(dev
, "ACPI _SDD failed (AE 0x%x)\n", status
);
792 * ata_acpi_on_suspend - ATA ACPI hook called on suspend
793 * @ap: target ATA port
795 * This function is called when @ap is about to be suspended. All
796 * devices are already put to sleep but the port_suspend() callback
797 * hasn't been executed yet. Error return from this function aborts
804 * 0 on success, -errno on failure.
806 int ata_acpi_on_suspend(struct ata_port
*ap
)
813 * ata_acpi_on_resume - ATA ACPI hook called on resume
814 * @ap: target ATA port
816 * This function is called when @ap is resumed - right after port
817 * itself is resumed but before any EH action is taken.
822 void ata_acpi_on_resume(struct ata_port
*ap
)
824 const struct ata_acpi_gtm
*gtm
= ata_acpi_init_gtm(ap
);
825 struct ata_device
*dev
;
827 if (ACPI_HANDLE(&ap
->tdev
) && gtm
) {
830 /* restore timing parameters */
831 ata_acpi_stm(ap
, gtm
);
833 /* _GTF should immediately follow _STM so that it can
834 * use values set by _STM. Cache _GTF result and
837 ata_for_each_dev(dev
, &ap
->link
, ALL
) {
838 ata_acpi_clear_gtf(dev
);
839 if (ata_dev_enabled(dev
) &&
840 ata_dev_get_GTF(dev
, NULL
) >= 0)
841 dev
->flags
|= ATA_DFLAG_ACPI_PENDING
;
844 /* SATA _GTF needs to be evaulated after _SDD and
845 * there's no reason to evaluate IDE _GTF early
846 * without _STM. Clear cache and schedule _GTF.
848 ata_for_each_dev(dev
, &ap
->link
, ALL
) {
849 ata_acpi_clear_gtf(dev
);
850 if (ata_dev_enabled(dev
))
851 dev
->flags
|= ATA_DFLAG_ACPI_PENDING
;
856 static int ata_acpi_choose_suspend_state(struct ata_device
*dev
, bool runtime
)
858 int d_max_in
= ACPI_STATE_D3_COLD
;
863 * For ATAPI, runtime D3 cold is only allowed
864 * for ZPODD in zero power ready state
866 if (dev
->class == ATA_DEV_ATAPI
&&
867 !(zpodd_dev_enabled(dev
) && zpodd_zpready(dev
)))
868 d_max_in
= ACPI_STATE_D3_HOT
;
871 return acpi_pm_device_sleep_state(&dev
->tdev
, NULL
, d_max_in
);
874 static void sata_acpi_set_state(struct ata_port
*ap
, pm_message_t state
)
876 bool runtime
= PMSG_IS_AUTO(state
);
877 struct ata_device
*dev
;
881 ata_for_each_dev(dev
, &ap
->link
, ENABLED
) {
882 handle
= ata_dev_acpi_handle(dev
);
886 if (!(state
.event
& PM_EVENT_RESUME
)) {
887 acpi_state
= ata_acpi_choose_suspend_state(dev
, runtime
);
888 if (acpi_state
== ACPI_STATE_D0
)
890 if (runtime
&& zpodd_dev_enabled(dev
) &&
891 acpi_state
== ACPI_STATE_D3_COLD
)
892 zpodd_enable_run_wake(dev
);
893 acpi_bus_set_power(handle
, acpi_state
);
895 if (runtime
&& zpodd_dev_enabled(dev
))
896 zpodd_disable_run_wake(dev
);
897 acpi_bus_set_power(handle
, ACPI_STATE_D0
);
902 /* ACPI spec requires _PS0 when IDE power on and _PS3 when power off */
903 static void pata_acpi_set_state(struct ata_port
*ap
, pm_message_t state
)
905 struct ata_device
*dev
;
906 acpi_handle port_handle
;
908 port_handle
= ACPI_HANDLE(&ap
->tdev
);
912 /* channel first and then drives for power on and vica versa
914 if (state
.event
& PM_EVENT_RESUME
)
915 acpi_bus_set_power(port_handle
, ACPI_STATE_D0
);
917 ata_for_each_dev(dev
, &ap
->link
, ENABLED
) {
918 acpi_handle dev_handle
= ata_dev_acpi_handle(dev
);
922 acpi_bus_set_power(dev_handle
, state
.event
& PM_EVENT_RESUME
?
923 ACPI_STATE_D0
: ACPI_STATE_D3_COLD
);
926 if (!(state
.event
& PM_EVENT_RESUME
))
927 acpi_bus_set_power(port_handle
, ACPI_STATE_D3_COLD
);
931 * ata_acpi_set_state - set the port power state
932 * @ap: target ATA port
933 * @state: state, on/off
935 * This function sets a proper ACPI D state for the device on
936 * system and runtime PM operations.
938 void ata_acpi_set_state(struct ata_port
*ap
, pm_message_t state
)
940 if (ap
->flags
& ATA_FLAG_ACPI_SATA
)
941 sata_acpi_set_state(ap
, state
);
943 pata_acpi_set_state(ap
, state
);
947 * ata_acpi_on_devcfg - ATA ACPI hook called on device donfiguration
948 * @dev: target ATA device
950 * This function is called when @dev is about to be configured.
951 * IDENTIFY data might have been modified after this hook is run.
957 * Positive number if IDENTIFY data needs to be refreshed, 0 if not,
960 int ata_acpi_on_devcfg(struct ata_device
*dev
)
962 struct ata_port
*ap
= dev
->link
->ap
;
963 struct ata_eh_context
*ehc
= &ap
->link
.eh_context
;
964 int acpi_sata
= ap
->flags
& ATA_FLAG_ACPI_SATA
;
968 if (!ata_dev_acpi_handle(dev
))
971 /* do we need to do _GTF? */
972 if (!(dev
->flags
& ATA_DFLAG_ACPI_PENDING
) &&
973 !(acpi_sata
&& (ehc
->i
.flags
& ATA_EHI_DID_HARDRESET
)))
976 /* do _SDD if SATA */
978 rc
= ata_acpi_push_id(dev
);
979 if (rc
&& rc
!= -ENOENT
)
984 rc
= ata_acpi_exec_tfs(dev
, &nr_executed
);
988 dev
->flags
&= ~ATA_DFLAG_ACPI_PENDING
;
990 /* refresh IDENTIFY page if any _GTF command has been executed */
992 rc
= ata_dev_reread_id(dev
, 0);
995 "failed to IDENTIFY after ACPI commands\n");
1003 /* ignore evaluation failure if we can continue safely */
1004 if (rc
== -EINVAL
&& !nr_executed
&& !(ap
->pflags
& ATA_PFLAG_FROZEN
))
1007 /* fail and let EH retry once more for unknown IO errors */
1008 if (!(dev
->flags
& ATA_DFLAG_ACPI_FAILED
)) {
1009 dev
->flags
|= ATA_DFLAG_ACPI_FAILED
;
1013 dev
->flags
|= ATA_DFLAG_ACPI_DISABLED
;
1014 ata_dev_warn(dev
, "ACPI: failed the second time, disabled\n");
1016 /* We can safely continue if no _GTF command has been executed
1017 * and port is not frozen.
1019 if (!nr_executed
&& !(ap
->pflags
& ATA_PFLAG_FROZEN
))
1026 * ata_acpi_on_disable - ATA ACPI hook called when a device is disabled
1027 * @dev: target ATA device
1029 * This function is called when @dev is about to be disabled.
1034 void ata_acpi_on_disable(struct ata_device
*dev
)
1036 ata_acpi_clear_gtf(dev
);