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 <linux/pm_qos.h>
21 #include <scsi/scsi_device.h>
24 #include <acpi/acpi_bus.h>
26 unsigned int ata_acpi_gtf_filter
= ATA_ACPI_FILTER_DEFAULT
;
27 module_param_named(acpi_gtf_filter
, ata_acpi_gtf_filter
, int, 0644);
28 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)");
30 #define NO_PORT_MULT 0xffff
31 #define SATA_ADR(root, pmp) (((root) << 16) | (pmp))
33 #define REGS_PER_GTF 7
35 u8 tf
[REGS_PER_GTF
]; /* regs. 0x1f1 - 0x1f7 */
39 * Helper - belongs in the PCI layer somewhere eventually
41 static int is_pci_dev(struct device
*dev
)
43 return (dev
->bus
== &pci_bus_type
);
46 static void ata_acpi_clear_gtf(struct ata_device
*dev
)
48 kfree(dev
->gtf_cache
);
49 dev
->gtf_cache
= NULL
;
53 * ata_ap_acpi_handle - provide the acpi_handle for an ata_port
54 * @ap: the acpi_handle returned will correspond to this port
56 * Returns the acpi_handle for the ACPI namespace object corresponding to
57 * the ata_port passed into the function, or NULL if no such object exists
59 acpi_handle
ata_ap_acpi_handle(struct ata_port
*ap
)
61 if (ap
->flags
& ATA_FLAG_ACPI_SATA
)
64 return acpi_get_child(DEVICE_ACPI_HANDLE(ap
->host
->dev
), ap
->port_no
);
66 EXPORT_SYMBOL(ata_ap_acpi_handle
);
69 * ata_dev_acpi_handle - provide the acpi_handle for an ata_device
70 * @dev: the acpi_device returned will correspond to this port
72 * Returns the acpi_handle for the ACPI namespace object corresponding to
73 * the ata_device passed into the function, or NULL if no such object exists
75 acpi_handle
ata_dev_acpi_handle(struct ata_device
*dev
)
78 struct ata_port
*ap
= dev
->link
->ap
;
80 if (dev
->flags
& ATA_DFLAG_ACPI_DISABLED
)
83 if (ap
->flags
& ATA_FLAG_ACPI_SATA
) {
84 if (!sata_pmp_attached(ap
))
85 adr
= SATA_ADR(ap
->port_no
, NO_PORT_MULT
);
87 adr
= SATA_ADR(ap
->port_no
, dev
->link
->pmp
);
88 return acpi_get_child(DEVICE_ACPI_HANDLE(ap
->host
->dev
), adr
);
90 return acpi_get_child(ata_ap_acpi_handle(ap
), dev
->devno
);
92 EXPORT_SYMBOL(ata_dev_acpi_handle
);
94 /* @ap and @dev are the same as ata_acpi_handle_hotplug() */
95 static void ata_acpi_detach_device(struct ata_port
*ap
, struct ata_device
*dev
)
98 dev
->flags
|= ATA_DFLAG_DETACH
;
100 struct ata_link
*tlink
;
101 struct ata_device
*tdev
;
103 ata_for_each_link(tlink
, ap
, EDGE
)
104 ata_for_each_dev(tdev
, tlink
, ALL
)
105 tdev
->flags
|= ATA_DFLAG_DETACH
;
108 ata_port_schedule_eh(ap
);
112 * ata_acpi_handle_hotplug - ACPI event handler backend
113 * @ap: ATA port ACPI event occurred
114 * @dev: ATA device ACPI event occurred (can be NULL)
115 * @event: ACPI event which occurred
117 * All ACPI bay / device realted events end up in this function. If
118 * the event is port-wide @dev is NULL. If the event is specific to a
119 * device, @dev points to it.
121 * Hotplug (as opposed to unplug) notification is always handled as
122 * port-wide while unplug only kills the target device on device-wide
126 * ACPI notify handler context. May sleep.
128 static void ata_acpi_handle_hotplug(struct ata_port
*ap
, struct ata_device
*dev
,
131 struct ata_eh_info
*ehi
= &ap
->link
.eh_info
;
135 spin_lock_irqsave(ap
->lock
, flags
);
137 * When dock driver calls into the routine, it will always use
138 * ACPI_NOTIFY_BUS_CHECK/ACPI_NOTIFY_DEVICE_CHECK for add and
139 * ACPI_NOTIFY_EJECT_REQUEST for remove
142 case ACPI_NOTIFY_BUS_CHECK
:
143 case ACPI_NOTIFY_DEVICE_CHECK
:
144 ata_ehi_push_desc(ehi
, "ACPI event");
146 ata_ehi_hotplugged(ehi
);
149 case ACPI_NOTIFY_EJECT_REQUEST
:
150 ata_ehi_push_desc(ehi
, "ACPI event");
152 ata_acpi_detach_device(ap
, dev
);
157 spin_unlock_irqrestore(ap
->lock
, flags
);
160 ata_port_wait_eh(ap
);
163 static void ata_acpi_dev_notify_dock(acpi_handle handle
, u32 event
, void *data
)
165 struct ata_device
*dev
= data
;
167 ata_acpi_handle_hotplug(dev
->link
->ap
, dev
, event
);
170 static void ata_acpi_ap_notify_dock(acpi_handle handle
, u32 event
, void *data
)
172 struct ata_port
*ap
= data
;
174 ata_acpi_handle_hotplug(ap
, NULL
, event
);
177 static void ata_acpi_uevent(struct ata_port
*ap
, struct ata_device
*dev
,
180 struct kobject
*kobj
= NULL
;
181 char event_string
[20];
182 char *envp
[] = { event_string
, NULL
};
186 kobj
= &dev
->sdev
->sdev_gendev
.kobj
;
188 kobj
= &ap
->dev
->kobj
;
191 snprintf(event_string
, 20, "BAY_EVENT=%d", event
);
192 kobject_uevent_env(kobj
, KOBJ_CHANGE
, envp
);
196 static void ata_acpi_ap_uevent(acpi_handle handle
, u32 event
, void *data
)
198 ata_acpi_uevent(data
, NULL
, event
);
201 static void ata_acpi_dev_uevent(acpi_handle handle
, u32 event
, void *data
)
203 struct ata_device
*dev
= data
;
204 ata_acpi_uevent(dev
->link
->ap
, dev
, event
);
207 static const struct acpi_dock_ops ata_acpi_dev_dock_ops
= {
208 .handler
= ata_acpi_dev_notify_dock
,
209 .uevent
= ata_acpi_dev_uevent
,
212 static const struct acpi_dock_ops ata_acpi_ap_dock_ops
= {
213 .handler
= ata_acpi_ap_notify_dock
,
214 .uevent
= ata_acpi_ap_uevent
,
218 * ata_acpi_dissociate - dissociate ATA host from ACPI objects
219 * @host: target ATA host
221 * This function is called during driver detach after the whole host
227 void ata_acpi_dissociate(struct ata_host
*host
)
231 /* Restore initial _GTM values so that driver which attaches
232 * afterward can use them too.
234 for (i
= 0; i
< host
->n_ports
; i
++) {
235 struct ata_port
*ap
= host
->ports
[i
];
236 const struct ata_acpi_gtm
*gtm
= ata_acpi_init_gtm(ap
);
238 if (ata_ap_acpi_handle(ap
) && gtm
)
239 ata_acpi_stm(ap
, gtm
);
244 * ata_acpi_gtm - execute _GTM
245 * @ap: target ATA port
246 * @gtm: out parameter for _GTM result
248 * Evaluate _GTM and store the result in @gtm.
254 * 0 on success, -ENOENT if _GTM doesn't exist, -errno on failure.
256 int ata_acpi_gtm(struct ata_port
*ap
, struct ata_acpi_gtm
*gtm
)
258 struct acpi_buffer output
= { .length
= ACPI_ALLOCATE_BUFFER
};
259 union acpi_object
*out_obj
;
263 status
= acpi_evaluate_object(ata_ap_acpi_handle(ap
), "_GTM", NULL
,
267 if (status
== AE_NOT_FOUND
)
271 if (ACPI_FAILURE(status
)) {
272 ata_port_err(ap
, "ACPI get timing mode failed (AE 0x%x)\n",
277 out_obj
= output
.pointer
;
278 if (out_obj
->type
!= ACPI_TYPE_BUFFER
) {
279 ata_port_warn(ap
, "_GTM returned unexpected object type 0x%x\n",
285 if (out_obj
->buffer
.length
!= sizeof(struct ata_acpi_gtm
)) {
286 ata_port_err(ap
, "_GTM returned invalid length %d\n",
287 out_obj
->buffer
.length
);
291 memcpy(gtm
, out_obj
->buffer
.pointer
, sizeof(struct ata_acpi_gtm
));
294 kfree(output
.pointer
);
298 EXPORT_SYMBOL_GPL(ata_acpi_gtm
);
301 * ata_acpi_stm - execute _STM
302 * @ap: target ATA port
303 * @stm: timing parameter to _STM
305 * Evaluate _STM with timing parameter @stm.
311 * 0 on success, -ENOENT if _STM doesn't exist, -errno on failure.
313 int ata_acpi_stm(struct ata_port
*ap
, const struct ata_acpi_gtm
*stm
)
316 struct ata_acpi_gtm stm_buf
= *stm
;
317 struct acpi_object_list input
;
318 union acpi_object in_params
[3];
320 in_params
[0].type
= ACPI_TYPE_BUFFER
;
321 in_params
[0].buffer
.length
= sizeof(struct ata_acpi_gtm
);
322 in_params
[0].buffer
.pointer
= (u8
*)&stm_buf
;
323 /* Buffers for id may need byteswapping ? */
324 in_params
[1].type
= ACPI_TYPE_BUFFER
;
325 in_params
[1].buffer
.length
= 512;
326 in_params
[1].buffer
.pointer
= (u8
*)ap
->link
.device
[0].id
;
327 in_params
[2].type
= ACPI_TYPE_BUFFER
;
328 in_params
[2].buffer
.length
= 512;
329 in_params
[2].buffer
.pointer
= (u8
*)ap
->link
.device
[1].id
;
332 input
.pointer
= in_params
;
334 status
= acpi_evaluate_object(ata_ap_acpi_handle(ap
), "_STM", &input
,
337 if (status
== AE_NOT_FOUND
)
339 if (ACPI_FAILURE(status
)) {
340 ata_port_err(ap
, "ACPI set timing mode failed (status=0x%x)\n",
347 EXPORT_SYMBOL_GPL(ata_acpi_stm
);
350 * ata_dev_get_GTF - get the drive bootup default taskfile settings
351 * @dev: target ATA device
352 * @gtf: output parameter for buffer containing _GTF taskfile arrays
354 * This applies to both PATA and SATA drives.
356 * The _GTF method has no input parameters.
357 * It returns a variable number of register set values (registers
358 * hex 1F1..1F7, taskfiles).
359 * The <variable number> is not known in advance, so have ACPI-CA
360 * allocate the buffer as needed and return it, then free it later.
366 * Number of taskfiles on success, 0 if _GTF doesn't exist. -EINVAL
367 * if _GTF is invalid.
369 static int ata_dev_get_GTF(struct ata_device
*dev
, struct ata_acpi_gtf
**gtf
)
371 struct ata_port
*ap
= dev
->link
->ap
;
373 struct acpi_buffer output
;
374 union acpi_object
*out_obj
;
377 /* if _GTF is cached, use the cached value */
378 if (dev
->gtf_cache
) {
379 out_obj
= dev
->gtf_cache
;
383 /* set up output buffer */
384 output
.length
= ACPI_ALLOCATE_BUFFER
;
385 output
.pointer
= NULL
; /* ACPI-CA sets this; save/free it later */
387 if (ata_msg_probe(ap
))
388 ata_dev_dbg(dev
, "%s: ENTER: port#: %d\n",
389 __func__
, ap
->port_no
);
391 /* _GTF has no input parameters */
392 status
= acpi_evaluate_object(ata_dev_acpi_handle(dev
), "_GTF", NULL
,
394 out_obj
= dev
->gtf_cache
= output
.pointer
;
396 if (ACPI_FAILURE(status
)) {
397 if (status
!= AE_NOT_FOUND
) {
398 ata_dev_warn(dev
, "_GTF evaluation failed (AE 0x%x)\n",
405 if (!output
.length
|| !output
.pointer
) {
406 if (ata_msg_probe(ap
))
407 ata_dev_dbg(dev
, "%s: Run _GTF: length or ptr is NULL (0x%llx, 0x%p)\n",
409 (unsigned long long)output
.length
,
415 if (out_obj
->type
!= ACPI_TYPE_BUFFER
) {
416 ata_dev_warn(dev
, "_GTF unexpected object type 0x%x\n",
422 if (out_obj
->buffer
.length
% REGS_PER_GTF
) {
423 ata_dev_warn(dev
, "unexpected _GTF length (%d)\n",
424 out_obj
->buffer
.length
);
430 rc
= out_obj
->buffer
.length
/ REGS_PER_GTF
;
432 *gtf
= (void *)out_obj
->buffer
.pointer
;
433 if (ata_msg_probe(ap
))
434 ata_dev_dbg(dev
, "%s: returning gtf=%p, gtf_count=%d\n",
440 ata_acpi_clear_gtf(dev
);
445 * ata_acpi_gtm_xfermode - determine xfermode from GTM parameter
446 * @dev: target device
447 * @gtm: GTM parameter to use
449 * Determine xfermask for @dev from @gtm.
455 * Determined xfermask.
457 unsigned long ata_acpi_gtm_xfermask(struct ata_device
*dev
,
458 const struct ata_acpi_gtm
*gtm
)
460 unsigned long xfer_mask
= 0;
465 /* we always use the 0 slot for crap hardware */
467 if (!(gtm
->flags
& 0x10))
471 mode
= ata_timing_cycle2mode(ATA_SHIFT_PIO
, gtm
->drive
[unit
].pio
);
472 xfer_mask
|= ata_xfer_mode2mask(mode
);
474 /* See if we have MWDMA or UDMA data. We don't bother with
475 * MWDMA if UDMA is available as this means the BIOS set UDMA
476 * and our error changedown if it works is UDMA to PIO anyway.
478 if (!(gtm
->flags
& (1 << (2 * unit
))))
479 type
= ATA_SHIFT_MWDMA
;
481 type
= ATA_SHIFT_UDMA
;
483 mode
= ata_timing_cycle2mode(type
, gtm
->drive
[unit
].dma
);
484 xfer_mask
|= ata_xfer_mode2mask(mode
);
488 EXPORT_SYMBOL_GPL(ata_acpi_gtm_xfermask
);
491 * ata_acpi_cbl_80wire - Check for 80 wire cable
493 * @gtm: GTM data to use
495 * Return 1 if the @gtm indicates the BIOS selected an 80wire mode.
497 int ata_acpi_cbl_80wire(struct ata_port
*ap
, const struct ata_acpi_gtm
*gtm
)
499 struct ata_device
*dev
;
501 ata_for_each_dev(dev
, &ap
->link
, ENABLED
) {
502 unsigned long xfer_mask
, udma_mask
;
504 xfer_mask
= ata_acpi_gtm_xfermask(dev
, gtm
);
505 ata_unpack_xfermask(xfer_mask
, NULL
, NULL
, &udma_mask
);
507 if (udma_mask
& ~ATA_UDMA_MASK_40C
)
513 EXPORT_SYMBOL_GPL(ata_acpi_cbl_80wire
);
515 static void ata_acpi_gtf_to_tf(struct ata_device
*dev
,
516 const struct ata_acpi_gtf
*gtf
,
517 struct ata_taskfile
*tf
)
519 ata_tf_init(dev
, tf
);
521 tf
->flags
|= ATA_TFLAG_ISADDR
| ATA_TFLAG_DEVICE
;
522 tf
->protocol
= ATA_PROT_NODATA
;
523 tf
->feature
= gtf
->tf
[0]; /* 0x1f1 */
524 tf
->nsect
= gtf
->tf
[1]; /* 0x1f2 */
525 tf
->lbal
= gtf
->tf
[2]; /* 0x1f3 */
526 tf
->lbam
= gtf
->tf
[3]; /* 0x1f4 */
527 tf
->lbah
= gtf
->tf
[4]; /* 0x1f5 */
528 tf
->device
= gtf
->tf
[5]; /* 0x1f6 */
529 tf
->command
= gtf
->tf
[6]; /* 0x1f7 */
532 static int ata_acpi_filter_tf(struct ata_device
*dev
,
533 const struct ata_taskfile
*tf
,
534 const struct ata_taskfile
*ptf
)
536 if (dev
->gtf_filter
& ATA_ACPI_FILTER_SETXFER
) {
537 /* libata doesn't use ACPI to configure transfer mode.
538 * It will only confuse device configuration. Skip.
540 if (tf
->command
== ATA_CMD_SET_FEATURES
&&
541 tf
->feature
== SETFEATURES_XFER
)
545 if (dev
->gtf_filter
& ATA_ACPI_FILTER_LOCK
) {
546 /* BIOS writers, sorry but we don't wanna lock
547 * features unless the user explicitly said so.
550 /* DEVICE CONFIGURATION FREEZE LOCK */
551 if (tf
->command
== ATA_CMD_CONF_OVERLAY
&&
552 tf
->feature
== ATA_DCO_FREEZE_LOCK
)
555 /* SECURITY FREEZE LOCK */
556 if (tf
->command
== ATA_CMD_SEC_FREEZE_LOCK
)
559 /* SET MAX LOCK and SET MAX FREEZE LOCK */
560 if ((!ptf
|| ptf
->command
!= ATA_CMD_READ_NATIVE_MAX
) &&
561 tf
->command
== ATA_CMD_SET_MAX
&&
562 (tf
->feature
== ATA_SET_MAX_LOCK
||
563 tf
->feature
== ATA_SET_MAX_FREEZE_LOCK
))
567 if (tf
->command
== ATA_CMD_SET_FEATURES
&&
568 tf
->feature
== SETFEATURES_SATA_ENABLE
) {
569 /* inhibit enabling DIPM */
570 if (dev
->gtf_filter
& ATA_ACPI_FILTER_DIPM
&&
571 tf
->nsect
== SATA_DIPM
)
574 /* inhibit FPDMA non-zero offset */
575 if (dev
->gtf_filter
& ATA_ACPI_FILTER_FPDMA_OFFSET
&&
576 (tf
->nsect
== SATA_FPDMA_OFFSET
||
577 tf
->nsect
== SATA_FPDMA_IN_ORDER
))
580 /* inhibit FPDMA auto activation */
581 if (dev
->gtf_filter
& ATA_ACPI_FILTER_FPDMA_AA
&&
582 tf
->nsect
== SATA_FPDMA_AA
)
590 * ata_acpi_run_tf - send taskfile registers to host controller
591 * @dev: target ATA device
592 * @gtf: raw ATA taskfile register set (0x1f1 - 0x1f7)
594 * Outputs ATA taskfile to standard ATA host controller.
595 * Writes the control, feature, nsect, lbal, lbam, and lbah registers.
596 * Optionally (ATA_TFLAG_LBA48) writes hob_feature, hob_nsect,
597 * hob_lbal, hob_lbam, and hob_lbah.
599 * This function waits for idle (!BUSY and !DRQ) after writing
600 * registers. If the control register has a new value, this
601 * function also waits for idle after writing control and before
602 * writing the remaining registers.
608 * 1 if command is executed successfully. 0 if ignored, rejected or
609 * filtered out, -errno on other errors.
611 static int ata_acpi_run_tf(struct ata_device
*dev
,
612 const struct ata_acpi_gtf
*gtf
,
613 const struct ata_acpi_gtf
*prev_gtf
)
615 struct ata_taskfile
*pptf
= NULL
;
616 struct ata_taskfile tf
, ptf
, rtf
;
617 unsigned int err_mask
;
623 if ((gtf
->tf
[0] == 0) && (gtf
->tf
[1] == 0) && (gtf
->tf
[2] == 0)
624 && (gtf
->tf
[3] == 0) && (gtf
->tf
[4] == 0) && (gtf
->tf
[5] == 0)
625 && (gtf
->tf
[6] == 0))
628 ata_acpi_gtf_to_tf(dev
, gtf
, &tf
);
630 ata_acpi_gtf_to_tf(dev
, prev_gtf
, &ptf
);
634 if (!ata_acpi_filter_tf(dev
, &tf
, pptf
)) {
636 err_mask
= ata_exec_internal(dev
, &rtf
, NULL
,
637 DMA_NONE
, NULL
, 0, 0);
642 snprintf(msg
, sizeof(msg
), "succeeded");
648 snprintf(msg
, sizeof(msg
),
649 "rejected by device (Stat=0x%02x Err=0x%02x)",
650 rtf
.command
, rtf
.feature
);
656 snprintf(msg
, sizeof(msg
),
657 "failed (Emask=0x%x Stat=0x%02x Err=0x%02x)",
658 err_mask
, rtf
.command
, rtf
.feature
);
664 snprintf(msg
, sizeof(msg
), "filtered out");
667 descr
= ata_get_cmd_descript(tf
.command
);
669 ata_dev_printk(dev
, level
,
670 "ACPI cmd %02x/%02x:%02x:%02x:%02x:%02x:%02x (%s) %s\n",
671 tf
.command
, tf
.feature
, tf
.nsect
, tf
.lbal
,
672 tf
.lbam
, tf
.lbah
, tf
.device
,
673 (descr
? descr
: "unknown"), msg
);
679 * ata_acpi_exec_tfs - get then write drive taskfile settings
680 * @dev: target ATA device
681 * @nr_executed: out parameter for the number of executed commands
683 * Evaluate _GTF and execute returned taskfiles.
689 * Number of executed taskfiles on success, 0 if _GTF doesn't exist.
690 * -errno on other errors.
692 static int ata_acpi_exec_tfs(struct ata_device
*dev
, int *nr_executed
)
694 struct ata_acpi_gtf
*gtf
= NULL
, *pgtf
= NULL
;
695 int gtf_count
, i
, rc
;
698 rc
= ata_dev_get_GTF(dev
, >f
);
704 for (i
= 0; i
< gtf_count
; i
++, gtf
++) {
705 rc
= ata_acpi_run_tf(dev
, gtf
, pgtf
);
714 ata_acpi_clear_gtf(dev
);
722 * ata_acpi_push_id - send Identify data to drive
723 * @dev: target ATA device
725 * _SDD ACPI object: for SATA mode only
726 * Must be after Identify (Packet) Device -- uses its data
727 * ATM this function never returns a failure. It is an optional
728 * method and if it fails for whatever reason, we should still
735 * 0 on success, -ENOENT if _SDD doesn't exist, -errno on failure.
737 static int ata_acpi_push_id(struct ata_device
*dev
)
739 struct ata_port
*ap
= dev
->link
->ap
;
741 struct acpi_object_list input
;
742 union acpi_object in_params
[1];
744 if (ata_msg_probe(ap
))
745 ata_dev_dbg(dev
, "%s: ix = %d, port#: %d\n",
746 __func__
, dev
->devno
, ap
->port_no
);
748 /* Give the drive Identify data to the drive via the _SDD method */
749 /* _SDD: set up input parameters */
751 input
.pointer
= in_params
;
752 in_params
[0].type
= ACPI_TYPE_BUFFER
;
753 in_params
[0].buffer
.length
= sizeof(dev
->id
[0]) * ATA_ID_WORDS
;
754 in_params
[0].buffer
.pointer
= (u8
*)dev
->id
;
755 /* Output buffer: _SDD has no output */
757 /* It's OK for _SDD to be missing too. */
758 swap_buf_le16(dev
->id
, ATA_ID_WORDS
);
759 status
= acpi_evaluate_object(ata_dev_acpi_handle(dev
), "_SDD", &input
,
761 swap_buf_le16(dev
->id
, ATA_ID_WORDS
);
763 if (status
== AE_NOT_FOUND
)
766 if (ACPI_FAILURE(status
)) {
767 ata_dev_warn(dev
, "ACPI _SDD failed (AE 0x%x)\n", status
);
775 * ata_acpi_on_suspend - ATA ACPI hook called on suspend
776 * @ap: target ATA port
778 * This function is called when @ap is about to be suspended. All
779 * devices are already put to sleep but the port_suspend() callback
780 * hasn't been executed yet. Error return from this function aborts
787 * 0 on success, -errno on failure.
789 int ata_acpi_on_suspend(struct ata_port
*ap
)
796 * ata_acpi_on_resume - ATA ACPI hook called on resume
797 * @ap: target ATA port
799 * This function is called when @ap is resumed - right after port
800 * itself is resumed but before any EH action is taken.
805 void ata_acpi_on_resume(struct ata_port
*ap
)
807 const struct ata_acpi_gtm
*gtm
= ata_acpi_init_gtm(ap
);
808 struct ata_device
*dev
;
810 if (ata_ap_acpi_handle(ap
) && gtm
) {
813 /* restore timing parameters */
814 ata_acpi_stm(ap
, gtm
);
816 /* _GTF should immediately follow _STM so that it can
817 * use values set by _STM. Cache _GTF result and
820 ata_for_each_dev(dev
, &ap
->link
, ALL
) {
821 ata_acpi_clear_gtf(dev
);
822 if (ata_dev_enabled(dev
) &&
823 ata_dev_get_GTF(dev
, NULL
) >= 0)
824 dev
->flags
|= ATA_DFLAG_ACPI_PENDING
;
827 /* SATA _GTF needs to be evaulated after _SDD and
828 * there's no reason to evaluate IDE _GTF early
829 * without _STM. Clear cache and schedule _GTF.
831 ata_for_each_dev(dev
, &ap
->link
, ALL
) {
832 ata_acpi_clear_gtf(dev
);
833 if (ata_dev_enabled(dev
))
834 dev
->flags
|= ATA_DFLAG_ACPI_PENDING
;
839 static int ata_acpi_choose_suspend_state(struct ata_device
*dev
, bool runtime
)
841 int d_max_in
= ACPI_STATE_D3_COLD
;
846 * For ATAPI, runtime D3 cold is only allowed
847 * for ZPODD in zero power ready state
849 if (dev
->class == ATA_DEV_ATAPI
&&
850 !(zpodd_dev_enabled(dev
) && zpodd_zpready(dev
)))
851 d_max_in
= ACPI_STATE_D3_HOT
;
854 return acpi_pm_device_sleep_state(&dev
->sdev
->sdev_gendev
,
858 static void sata_acpi_set_state(struct ata_port
*ap
, pm_message_t state
)
860 bool runtime
= PMSG_IS_AUTO(state
);
861 struct ata_device
*dev
;
865 ata_for_each_dev(dev
, &ap
->link
, ENABLED
) {
866 handle
= ata_dev_acpi_handle(dev
);
870 if (!(state
.event
& PM_EVENT_RESUME
)) {
871 acpi_state
= ata_acpi_choose_suspend_state(dev
, runtime
);
872 if (acpi_state
== ACPI_STATE_D0
)
874 if (runtime
&& zpodd_dev_enabled(dev
) &&
875 acpi_state
== ACPI_STATE_D3_COLD
)
876 zpodd_enable_run_wake(dev
);
877 acpi_bus_set_power(handle
, acpi_state
);
879 if (runtime
&& zpodd_dev_enabled(dev
))
880 zpodd_disable_run_wake(dev
);
881 acpi_bus_set_power(handle
, ACPI_STATE_D0
);
886 /* ACPI spec requires _PS0 when IDE power on and _PS3 when power off */
887 static void pata_acpi_set_state(struct ata_port
*ap
, pm_message_t state
)
889 struct ata_device
*dev
;
890 acpi_handle port_handle
;
892 port_handle
= ata_ap_acpi_handle(ap
);
896 /* channel first and then drives for power on and vica versa
898 if (state
.event
& PM_EVENT_RESUME
)
899 acpi_bus_set_power(port_handle
, ACPI_STATE_D0
);
901 ata_for_each_dev(dev
, &ap
->link
, ENABLED
) {
902 acpi_handle dev_handle
= ata_dev_acpi_handle(dev
);
906 acpi_bus_set_power(dev_handle
, state
.event
& PM_EVENT_RESUME
?
907 ACPI_STATE_D0
: ACPI_STATE_D3
);
910 if (!(state
.event
& PM_EVENT_RESUME
))
911 acpi_bus_set_power(port_handle
, ACPI_STATE_D3
);
915 * ata_acpi_set_state - set the port power state
916 * @ap: target ATA port
917 * @state: state, on/off
919 * This function sets a proper ACPI D state for the device on
920 * system and runtime PM operations.
922 void ata_acpi_set_state(struct ata_port
*ap
, pm_message_t state
)
924 if (ap
->flags
& ATA_FLAG_ACPI_SATA
)
925 sata_acpi_set_state(ap
, state
);
927 pata_acpi_set_state(ap
, state
);
931 * ata_acpi_on_devcfg - ATA ACPI hook called on device donfiguration
932 * @dev: target ATA device
934 * This function is called when @dev is about to be configured.
935 * IDENTIFY data might have been modified after this hook is run.
941 * Positive number if IDENTIFY data needs to be refreshed, 0 if not,
944 int ata_acpi_on_devcfg(struct ata_device
*dev
)
946 struct ata_port
*ap
= dev
->link
->ap
;
947 struct ata_eh_context
*ehc
= &ap
->link
.eh_context
;
948 int acpi_sata
= ap
->flags
& ATA_FLAG_ACPI_SATA
;
952 if (!ata_dev_acpi_handle(dev
))
955 /* do we need to do _GTF? */
956 if (!(dev
->flags
& ATA_DFLAG_ACPI_PENDING
) &&
957 !(acpi_sata
&& (ehc
->i
.flags
& ATA_EHI_DID_HARDRESET
)))
960 /* do _SDD if SATA */
962 rc
= ata_acpi_push_id(dev
);
963 if (rc
&& rc
!= -ENOENT
)
968 rc
= ata_acpi_exec_tfs(dev
, &nr_executed
);
972 dev
->flags
&= ~ATA_DFLAG_ACPI_PENDING
;
974 /* refresh IDENTIFY page if any _GTF command has been executed */
976 rc
= ata_dev_reread_id(dev
, 0);
979 "failed to IDENTIFY after ACPI commands\n");
987 /* ignore evaluation failure if we can continue safely */
988 if (rc
== -EINVAL
&& !nr_executed
&& !(ap
->pflags
& ATA_PFLAG_FROZEN
))
991 /* fail and let EH retry once more for unknown IO errors */
992 if (!(dev
->flags
& ATA_DFLAG_ACPI_FAILED
)) {
993 dev
->flags
|= ATA_DFLAG_ACPI_FAILED
;
997 dev
->flags
|= ATA_DFLAG_ACPI_DISABLED
;
998 ata_dev_warn(dev
, "ACPI: failed the second time, disabled\n");
1000 /* We can safely continue if no _GTF command has been executed
1001 * and port is not frozen.
1003 if (!nr_executed
&& !(ap
->pflags
& ATA_PFLAG_FROZEN
))
1010 * ata_acpi_on_disable - ATA ACPI hook called when a device is disabled
1011 * @dev: target ATA device
1013 * This function is called when @dev is about to be disabled.
1018 void ata_acpi_on_disable(struct ata_device
*dev
)
1020 ata_acpi_clear_gtf(dev
);
1023 static void ata_acpi_register_power_resource(struct ata_device
*dev
)
1025 struct scsi_device
*sdev
= dev
->sdev
;
1028 handle
= ata_dev_acpi_handle(dev
);
1030 acpi_dev_pm_remove_dependent(handle
, &sdev
->sdev_gendev
);
1033 static void ata_acpi_unregister_power_resource(struct ata_device
*dev
)
1035 struct scsi_device
*sdev
= dev
->sdev
;
1038 handle
= ata_dev_acpi_handle(dev
);
1040 acpi_dev_pm_remove_dependent(handle
, &sdev
->sdev_gendev
);
1043 void ata_acpi_bind(struct ata_device
*dev
)
1045 ata_acpi_register_power_resource(dev
);
1046 if (zpodd_dev_enabled(dev
))
1047 dev_pm_qos_expose_flags(&dev
->sdev
->sdev_gendev
, 0);
1050 void ata_acpi_unbind(struct ata_device
*dev
)
1052 ata_acpi_unregister_power_resource(dev
);
1055 static int compat_pci_ata(struct ata_port
*ap
)
1057 struct device
*dev
= ap
->tdev
.parent
;
1058 struct pci_dev
*pdev
;
1060 if (!is_pci_dev(dev
))
1063 pdev
= to_pci_dev(dev
);
1065 if ((pdev
->class >> 8) != PCI_CLASS_STORAGE_SATA
&&
1066 (pdev
->class >> 8) != PCI_CLASS_STORAGE_IDE
)
1072 static int ata_acpi_bind_host(struct ata_port
*ap
, acpi_handle
*handle
)
1074 if (ap
->flags
& ATA_FLAG_ACPI_SATA
)
1077 *handle
= acpi_get_child(DEVICE_ACPI_HANDLE(ap
->tdev
.parent
),
1083 if (ata_acpi_gtm(ap
, &ap
->__acpi_init_gtm
) == 0)
1084 ap
->pflags
|= ATA_PFLAG_INIT_GTM_VALID
;
1089 static int ata_acpi_bind_device(struct ata_port
*ap
, struct scsi_device
*sdev
,
1090 acpi_handle
*handle
)
1092 struct ata_device
*ata_dev
;
1094 if (ap
->flags
& ATA_FLAG_ACPI_SATA
) {
1095 if (!sata_pmp_attached(ap
))
1096 ata_dev
= &ap
->link
.device
[sdev
->id
];
1098 ata_dev
= &ap
->pmp_link
[sdev
->channel
].device
[sdev
->id
];
1101 ata_dev
= &ap
->link
.device
[sdev
->id
];
1104 *handle
= ata_dev_acpi_handle(ata_dev
);
1112 static int is_ata_port(const struct device
*dev
)
1114 return dev
->type
== &ata_port_type
;
1117 static struct ata_port
*dev_to_ata_port(struct device
*dev
)
1119 while (!is_ata_port(dev
)) {
1124 return to_ata_port(dev
);
1127 static int ata_acpi_find_device(struct device
*dev
, acpi_handle
*handle
)
1129 struct ata_port
*ap
= dev_to_ata_port(dev
);
1134 if (!compat_pci_ata(ap
))
1137 if (scsi_is_host_device(dev
))
1138 return ata_acpi_bind_host(ap
, handle
);
1139 else if (scsi_is_sdev_device(dev
)) {
1140 struct scsi_device
*sdev
= to_scsi_device(dev
);
1142 return ata_acpi_bind_device(ap
, sdev
, handle
);
1147 static struct acpi_bus_type ata_acpi_bus
= {
1149 .find_device
= ata_acpi_find_device
,
1152 int ata_acpi_register(void)
1154 return scsi_register_acpi_bus_type(&ata_acpi_bus
);
1157 void ata_acpi_unregister(void)
1159 scsi_unregister_acpi_bus_type(&ata_acpi_bus
);