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 ap
->scsi_host
?
64 DEVICE_ACPI_HANDLE(&ap
->scsi_host
->shost_gendev
) : NULL
;
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 (libata_noacpi
|| 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
);
161 flush_work(&ap
->hotplug_task
.work
);
165 static void ata_acpi_dev_notify_dock(acpi_handle handle
, u32 event
, void *data
)
167 struct ata_device
*dev
= data
;
169 ata_acpi_handle_hotplug(dev
->link
->ap
, dev
, event
);
172 static void ata_acpi_ap_notify_dock(acpi_handle handle
, u32 event
, void *data
)
174 struct ata_port
*ap
= data
;
176 ata_acpi_handle_hotplug(ap
, NULL
, event
);
179 static void ata_acpi_uevent(struct ata_port
*ap
, struct ata_device
*dev
,
182 struct kobject
*kobj
= NULL
;
183 char event_string
[20];
184 char *envp
[] = { event_string
, NULL
};
188 kobj
= &dev
->sdev
->sdev_gendev
.kobj
;
190 kobj
= &ap
->dev
->kobj
;
193 snprintf(event_string
, 20, "BAY_EVENT=%d", event
);
194 kobject_uevent_env(kobj
, KOBJ_CHANGE
, envp
);
198 static void ata_acpi_ap_uevent(acpi_handle handle
, u32 event
, void *data
)
200 ata_acpi_uevent(data
, NULL
, event
);
203 static void ata_acpi_dev_uevent(acpi_handle handle
, u32 event
, void *data
)
205 struct ata_device
*dev
= data
;
206 ata_acpi_uevent(dev
->link
->ap
, dev
, event
);
209 static const struct acpi_dock_ops ata_acpi_dev_dock_ops
= {
210 .handler
= ata_acpi_dev_notify_dock
,
211 .uevent
= ata_acpi_dev_uevent
,
214 static const struct acpi_dock_ops ata_acpi_ap_dock_ops
= {
215 .handler
= ata_acpi_ap_notify_dock
,
216 .uevent
= ata_acpi_ap_uevent
,
219 void ata_acpi_hotplug_init(struct ata_host
*host
)
223 for (i
= 0; i
< host
->n_ports
; i
++) {
224 struct ata_port
*ap
= host
->ports
[i
];
226 struct ata_device
*dev
;
231 handle
= ata_ap_acpi_handle(ap
);
233 /* we might be on a docking station */
234 register_hotplug_dock_device(handle
,
235 &ata_acpi_ap_dock_ops
, ap
,
239 ata_for_each_dev(dev
, &ap
->link
, ALL
) {
240 handle
= ata_dev_acpi_handle(dev
);
244 /* we might be on a docking station */
245 register_hotplug_dock_device(handle
,
246 &ata_acpi_dev_dock_ops
,
253 * ata_acpi_dissociate - dissociate ATA host from ACPI objects
254 * @host: target ATA host
256 * This function is called during driver detach after the whole host
262 void ata_acpi_dissociate(struct ata_host
*host
)
266 /* Restore initial _GTM values so that driver which attaches
267 * afterward can use them too.
269 for (i
= 0; i
< host
->n_ports
; i
++) {
270 struct ata_port
*ap
= host
->ports
[i
];
271 const struct ata_acpi_gtm
*gtm
= ata_acpi_init_gtm(ap
);
273 if (ata_ap_acpi_handle(ap
) && gtm
)
274 ata_acpi_stm(ap
, gtm
);
278 static int __ata_acpi_gtm(struct ata_port
*ap
, acpi_handle handle
,
279 struct ata_acpi_gtm
*gtm
)
281 struct acpi_buffer output
= { .length
= ACPI_ALLOCATE_BUFFER
};
282 union acpi_object
*out_obj
;
286 status
= acpi_evaluate_object(handle
, "_GTM", NULL
, &output
);
289 if (status
== AE_NOT_FOUND
)
293 if (ACPI_FAILURE(status
)) {
294 ata_port_err(ap
, "ACPI get timing mode failed (AE 0x%x)\n",
299 out_obj
= output
.pointer
;
300 if (out_obj
->type
!= ACPI_TYPE_BUFFER
) {
301 ata_port_warn(ap
, "_GTM returned unexpected object type 0x%x\n",
307 if (out_obj
->buffer
.length
!= sizeof(struct ata_acpi_gtm
)) {
308 ata_port_err(ap
, "_GTM returned invalid length %d\n",
309 out_obj
->buffer
.length
);
313 memcpy(gtm
, out_obj
->buffer
.pointer
, sizeof(struct ata_acpi_gtm
));
316 kfree(output
.pointer
);
321 * ata_acpi_gtm - execute _GTM
322 * @ap: target ATA port
323 * @gtm: out parameter for _GTM result
325 * Evaluate _GTM and store the result in @gtm.
331 * 0 on success, -ENOENT if _GTM doesn't exist, -errno on failure.
333 int ata_acpi_gtm(struct ata_port
*ap
, struct ata_acpi_gtm
*gtm
)
335 if (ata_ap_acpi_handle(ap
))
336 return __ata_acpi_gtm(ap
, ata_ap_acpi_handle(ap
), gtm
);
341 EXPORT_SYMBOL_GPL(ata_acpi_gtm
);
344 * ata_acpi_stm - execute _STM
345 * @ap: target ATA port
346 * @stm: timing parameter to _STM
348 * Evaluate _STM with timing parameter @stm.
354 * 0 on success, -ENOENT if _STM doesn't exist, -errno on failure.
356 int ata_acpi_stm(struct ata_port
*ap
, const struct ata_acpi_gtm
*stm
)
359 struct ata_acpi_gtm stm_buf
= *stm
;
360 struct acpi_object_list input
;
361 union acpi_object in_params
[3];
363 in_params
[0].type
= ACPI_TYPE_BUFFER
;
364 in_params
[0].buffer
.length
= sizeof(struct ata_acpi_gtm
);
365 in_params
[0].buffer
.pointer
= (u8
*)&stm_buf
;
366 /* Buffers for id may need byteswapping ? */
367 in_params
[1].type
= ACPI_TYPE_BUFFER
;
368 in_params
[1].buffer
.length
= 512;
369 in_params
[1].buffer
.pointer
= (u8
*)ap
->link
.device
[0].id
;
370 in_params
[2].type
= ACPI_TYPE_BUFFER
;
371 in_params
[2].buffer
.length
= 512;
372 in_params
[2].buffer
.pointer
= (u8
*)ap
->link
.device
[1].id
;
375 input
.pointer
= in_params
;
377 status
= acpi_evaluate_object(ata_ap_acpi_handle(ap
), "_STM", &input
,
380 if (status
== AE_NOT_FOUND
)
382 if (ACPI_FAILURE(status
)) {
383 ata_port_err(ap
, "ACPI set timing mode failed (status=0x%x)\n",
390 EXPORT_SYMBOL_GPL(ata_acpi_stm
);
393 * ata_dev_get_GTF - get the drive bootup default taskfile settings
394 * @dev: target ATA device
395 * @gtf: output parameter for buffer containing _GTF taskfile arrays
397 * This applies to both PATA and SATA drives.
399 * The _GTF method has no input parameters.
400 * It returns a variable number of register set values (registers
401 * hex 1F1..1F7, taskfiles).
402 * The <variable number> is not known in advance, so have ACPI-CA
403 * allocate the buffer as needed and return it, then free it later.
409 * Number of taskfiles on success, 0 if _GTF doesn't exist. -EINVAL
410 * if _GTF is invalid.
412 static int ata_dev_get_GTF(struct ata_device
*dev
, struct ata_acpi_gtf
**gtf
)
414 struct ata_port
*ap
= dev
->link
->ap
;
416 struct acpi_buffer output
;
417 union acpi_object
*out_obj
;
420 /* if _GTF is cached, use the cached value */
421 if (dev
->gtf_cache
) {
422 out_obj
= dev
->gtf_cache
;
426 /* set up output buffer */
427 output
.length
= ACPI_ALLOCATE_BUFFER
;
428 output
.pointer
= NULL
; /* ACPI-CA sets this; save/free it later */
430 if (ata_msg_probe(ap
))
431 ata_dev_dbg(dev
, "%s: ENTER: port#: %d\n",
432 __func__
, ap
->port_no
);
434 /* _GTF has no input parameters */
435 status
= acpi_evaluate_object(ata_dev_acpi_handle(dev
), "_GTF", NULL
,
437 out_obj
= dev
->gtf_cache
= output
.pointer
;
439 if (ACPI_FAILURE(status
)) {
440 if (status
!= AE_NOT_FOUND
) {
441 ata_dev_warn(dev
, "_GTF evaluation failed (AE 0x%x)\n",
448 if (!output
.length
|| !output
.pointer
) {
449 if (ata_msg_probe(ap
))
450 ata_dev_dbg(dev
, "%s: Run _GTF: length or ptr is NULL (0x%llx, 0x%p)\n",
452 (unsigned long long)output
.length
,
458 if (out_obj
->type
!= ACPI_TYPE_BUFFER
) {
459 ata_dev_warn(dev
, "_GTF unexpected object type 0x%x\n",
465 if (out_obj
->buffer
.length
% REGS_PER_GTF
) {
466 ata_dev_warn(dev
, "unexpected _GTF length (%d)\n",
467 out_obj
->buffer
.length
);
473 rc
= out_obj
->buffer
.length
/ REGS_PER_GTF
;
475 *gtf
= (void *)out_obj
->buffer
.pointer
;
476 if (ata_msg_probe(ap
))
477 ata_dev_dbg(dev
, "%s: returning gtf=%p, gtf_count=%d\n",
483 ata_acpi_clear_gtf(dev
);
488 * ata_acpi_gtm_xfermode - determine xfermode from GTM parameter
489 * @dev: target device
490 * @gtm: GTM parameter to use
492 * Determine xfermask for @dev from @gtm.
498 * Determined xfermask.
500 unsigned long ata_acpi_gtm_xfermask(struct ata_device
*dev
,
501 const struct ata_acpi_gtm
*gtm
)
503 unsigned long xfer_mask
= 0;
508 /* we always use the 0 slot for crap hardware */
510 if (!(gtm
->flags
& 0x10))
514 mode
= ata_timing_cycle2mode(ATA_SHIFT_PIO
, gtm
->drive
[unit
].pio
);
515 xfer_mask
|= ata_xfer_mode2mask(mode
);
517 /* See if we have MWDMA or UDMA data. We don't bother with
518 * MWDMA if UDMA is available as this means the BIOS set UDMA
519 * and our error changedown if it works is UDMA to PIO anyway.
521 if (!(gtm
->flags
& (1 << (2 * unit
))))
522 type
= ATA_SHIFT_MWDMA
;
524 type
= ATA_SHIFT_UDMA
;
526 mode
= ata_timing_cycle2mode(type
, gtm
->drive
[unit
].dma
);
527 xfer_mask
|= ata_xfer_mode2mask(mode
);
531 EXPORT_SYMBOL_GPL(ata_acpi_gtm_xfermask
);
534 * ata_acpi_cbl_80wire - Check for 80 wire cable
536 * @gtm: GTM data to use
538 * Return 1 if the @gtm indicates the BIOS selected an 80wire mode.
540 int ata_acpi_cbl_80wire(struct ata_port
*ap
, const struct ata_acpi_gtm
*gtm
)
542 struct ata_device
*dev
;
544 ata_for_each_dev(dev
, &ap
->link
, ENABLED
) {
545 unsigned long xfer_mask
, udma_mask
;
547 xfer_mask
= ata_acpi_gtm_xfermask(dev
, gtm
);
548 ata_unpack_xfermask(xfer_mask
, NULL
, NULL
, &udma_mask
);
550 if (udma_mask
& ~ATA_UDMA_MASK_40C
)
556 EXPORT_SYMBOL_GPL(ata_acpi_cbl_80wire
);
558 static void ata_acpi_gtf_to_tf(struct ata_device
*dev
,
559 const struct ata_acpi_gtf
*gtf
,
560 struct ata_taskfile
*tf
)
562 ata_tf_init(dev
, tf
);
564 tf
->flags
|= ATA_TFLAG_ISADDR
| ATA_TFLAG_DEVICE
;
565 tf
->protocol
= ATA_PROT_NODATA
;
566 tf
->feature
= gtf
->tf
[0]; /* 0x1f1 */
567 tf
->nsect
= gtf
->tf
[1]; /* 0x1f2 */
568 tf
->lbal
= gtf
->tf
[2]; /* 0x1f3 */
569 tf
->lbam
= gtf
->tf
[3]; /* 0x1f4 */
570 tf
->lbah
= gtf
->tf
[4]; /* 0x1f5 */
571 tf
->device
= gtf
->tf
[5]; /* 0x1f6 */
572 tf
->command
= gtf
->tf
[6]; /* 0x1f7 */
575 static int ata_acpi_filter_tf(struct ata_device
*dev
,
576 const struct ata_taskfile
*tf
,
577 const struct ata_taskfile
*ptf
)
579 if (dev
->gtf_filter
& ATA_ACPI_FILTER_SETXFER
) {
580 /* libata doesn't use ACPI to configure transfer mode.
581 * It will only confuse device configuration. Skip.
583 if (tf
->command
== ATA_CMD_SET_FEATURES
&&
584 tf
->feature
== SETFEATURES_XFER
)
588 if (dev
->gtf_filter
& ATA_ACPI_FILTER_LOCK
) {
589 /* BIOS writers, sorry but we don't wanna lock
590 * features unless the user explicitly said so.
593 /* DEVICE CONFIGURATION FREEZE LOCK */
594 if (tf
->command
== ATA_CMD_CONF_OVERLAY
&&
595 tf
->feature
== ATA_DCO_FREEZE_LOCK
)
598 /* SECURITY FREEZE LOCK */
599 if (tf
->command
== ATA_CMD_SEC_FREEZE_LOCK
)
602 /* SET MAX LOCK and SET MAX FREEZE LOCK */
603 if ((!ptf
|| ptf
->command
!= ATA_CMD_READ_NATIVE_MAX
) &&
604 tf
->command
== ATA_CMD_SET_MAX
&&
605 (tf
->feature
== ATA_SET_MAX_LOCK
||
606 tf
->feature
== ATA_SET_MAX_FREEZE_LOCK
))
610 if (tf
->command
== ATA_CMD_SET_FEATURES
&&
611 tf
->feature
== SETFEATURES_SATA_ENABLE
) {
612 /* inhibit enabling DIPM */
613 if (dev
->gtf_filter
& ATA_ACPI_FILTER_DIPM
&&
614 tf
->nsect
== SATA_DIPM
)
617 /* inhibit FPDMA non-zero offset */
618 if (dev
->gtf_filter
& ATA_ACPI_FILTER_FPDMA_OFFSET
&&
619 (tf
->nsect
== SATA_FPDMA_OFFSET
||
620 tf
->nsect
== SATA_FPDMA_IN_ORDER
))
623 /* inhibit FPDMA auto activation */
624 if (dev
->gtf_filter
& ATA_ACPI_FILTER_FPDMA_AA
&&
625 tf
->nsect
== SATA_FPDMA_AA
)
633 * ata_acpi_run_tf - send taskfile registers to host controller
634 * @dev: target ATA device
635 * @gtf: raw ATA taskfile register set (0x1f1 - 0x1f7)
637 * Outputs ATA taskfile to standard ATA host controller.
638 * Writes the control, feature, nsect, lbal, lbam, and lbah registers.
639 * Optionally (ATA_TFLAG_LBA48) writes hob_feature, hob_nsect,
640 * hob_lbal, hob_lbam, and hob_lbah.
642 * This function waits for idle (!BUSY and !DRQ) after writing
643 * registers. If the control register has a new value, this
644 * function also waits for idle after writing control and before
645 * writing the remaining registers.
651 * 1 if command is executed successfully. 0 if ignored, rejected or
652 * filtered out, -errno on other errors.
654 static int ata_acpi_run_tf(struct ata_device
*dev
,
655 const struct ata_acpi_gtf
*gtf
,
656 const struct ata_acpi_gtf
*prev_gtf
)
658 struct ata_taskfile
*pptf
= NULL
;
659 struct ata_taskfile tf
, ptf
, rtf
;
660 unsigned int err_mask
;
666 if ((gtf
->tf
[0] == 0) && (gtf
->tf
[1] == 0) && (gtf
->tf
[2] == 0)
667 && (gtf
->tf
[3] == 0) && (gtf
->tf
[4] == 0) && (gtf
->tf
[5] == 0)
668 && (gtf
->tf
[6] == 0))
671 ata_acpi_gtf_to_tf(dev
, gtf
, &tf
);
673 ata_acpi_gtf_to_tf(dev
, prev_gtf
, &ptf
);
677 if (!ata_acpi_filter_tf(dev
, &tf
, pptf
)) {
679 err_mask
= ata_exec_internal(dev
, &rtf
, NULL
,
680 DMA_NONE
, NULL
, 0, 0);
685 snprintf(msg
, sizeof(msg
), "succeeded");
691 snprintf(msg
, sizeof(msg
),
692 "rejected by device (Stat=0x%02x Err=0x%02x)",
693 rtf
.command
, rtf
.feature
);
699 snprintf(msg
, sizeof(msg
),
700 "failed (Emask=0x%x Stat=0x%02x Err=0x%02x)",
701 err_mask
, rtf
.command
, rtf
.feature
);
707 snprintf(msg
, sizeof(msg
), "filtered out");
710 descr
= ata_get_cmd_descript(tf
.command
);
712 ata_dev_printk(dev
, level
,
713 "ACPI cmd %02x/%02x:%02x:%02x:%02x:%02x:%02x (%s) %s\n",
714 tf
.command
, tf
.feature
, tf
.nsect
, tf
.lbal
,
715 tf
.lbam
, tf
.lbah
, tf
.device
,
716 (descr
? descr
: "unknown"), msg
);
722 * ata_acpi_exec_tfs - get then write drive taskfile settings
723 * @dev: target ATA device
724 * @nr_executed: out parameter for the number of executed commands
726 * Evaluate _GTF and execute returned taskfiles.
732 * Number of executed taskfiles on success, 0 if _GTF doesn't exist.
733 * -errno on other errors.
735 static int ata_acpi_exec_tfs(struct ata_device
*dev
, int *nr_executed
)
737 struct ata_acpi_gtf
*gtf
= NULL
, *pgtf
= NULL
;
738 int gtf_count
, i
, rc
;
741 rc
= ata_dev_get_GTF(dev
, >f
);
747 for (i
= 0; i
< gtf_count
; i
++, gtf
++) {
748 rc
= ata_acpi_run_tf(dev
, gtf
, pgtf
);
757 ata_acpi_clear_gtf(dev
);
765 * ata_acpi_push_id - send Identify data to drive
766 * @dev: target ATA device
768 * _SDD ACPI object: for SATA mode only
769 * Must be after Identify (Packet) Device -- uses its data
770 * ATM this function never returns a failure. It is an optional
771 * method and if it fails for whatever reason, we should still
778 * 0 on success, -ENOENT if _SDD doesn't exist, -errno on failure.
780 static int ata_acpi_push_id(struct ata_device
*dev
)
782 struct ata_port
*ap
= dev
->link
->ap
;
784 struct acpi_object_list input
;
785 union acpi_object in_params
[1];
787 if (ata_msg_probe(ap
))
788 ata_dev_dbg(dev
, "%s: ix = %d, port#: %d\n",
789 __func__
, dev
->devno
, ap
->port_no
);
791 /* Give the drive Identify data to the drive via the _SDD method */
792 /* _SDD: set up input parameters */
794 input
.pointer
= in_params
;
795 in_params
[0].type
= ACPI_TYPE_BUFFER
;
796 in_params
[0].buffer
.length
= sizeof(dev
->id
[0]) * ATA_ID_WORDS
;
797 in_params
[0].buffer
.pointer
= (u8
*)dev
->id
;
798 /* Output buffer: _SDD has no output */
800 /* It's OK for _SDD to be missing too. */
801 swap_buf_le16(dev
->id
, ATA_ID_WORDS
);
802 status
= acpi_evaluate_object(ata_dev_acpi_handle(dev
), "_SDD", &input
,
804 swap_buf_le16(dev
->id
, ATA_ID_WORDS
);
806 if (status
== AE_NOT_FOUND
)
809 if (ACPI_FAILURE(status
)) {
810 ata_dev_warn(dev
, "ACPI _SDD failed (AE 0x%x)\n", status
);
818 * ata_acpi_on_suspend - ATA ACPI hook called on suspend
819 * @ap: target ATA port
821 * This function is called when @ap is about to be suspended. All
822 * devices are already put to sleep but the port_suspend() callback
823 * hasn't been executed yet. Error return from this function aborts
830 * 0 on success, -errno on failure.
832 int ata_acpi_on_suspend(struct ata_port
*ap
)
839 * ata_acpi_on_resume - ATA ACPI hook called on resume
840 * @ap: target ATA port
842 * This function is called when @ap is resumed - right after port
843 * itself is resumed but before any EH action is taken.
848 void ata_acpi_on_resume(struct ata_port
*ap
)
850 const struct ata_acpi_gtm
*gtm
= ata_acpi_init_gtm(ap
);
851 struct ata_device
*dev
;
853 if (ata_ap_acpi_handle(ap
) && gtm
) {
856 /* restore timing parameters */
857 ata_acpi_stm(ap
, gtm
);
859 /* _GTF should immediately follow _STM so that it can
860 * use values set by _STM. Cache _GTF result and
863 ata_for_each_dev(dev
, &ap
->link
, ALL
) {
864 ata_acpi_clear_gtf(dev
);
865 if (ata_dev_enabled(dev
) &&
866 ata_dev_get_GTF(dev
, NULL
) >= 0)
867 dev
->flags
|= ATA_DFLAG_ACPI_PENDING
;
870 /* SATA _GTF needs to be evaulated after _SDD and
871 * there's no reason to evaluate IDE _GTF early
872 * without _STM. Clear cache and schedule _GTF.
874 ata_for_each_dev(dev
, &ap
->link
, ALL
) {
875 ata_acpi_clear_gtf(dev
);
876 if (ata_dev_enabled(dev
))
877 dev
->flags
|= ATA_DFLAG_ACPI_PENDING
;
882 static int ata_acpi_choose_suspend_state(struct ata_device
*dev
, bool runtime
)
884 int d_max_in
= ACPI_STATE_D3_COLD
;
889 * For ATAPI, runtime D3 cold is only allowed
890 * for ZPODD in zero power ready state
892 if (dev
->class == ATA_DEV_ATAPI
&&
893 !(zpodd_dev_enabled(dev
) && zpodd_zpready(dev
)))
894 d_max_in
= ACPI_STATE_D3_HOT
;
897 return acpi_pm_device_sleep_state(&dev
->sdev
->sdev_gendev
,
901 static void sata_acpi_set_state(struct ata_port
*ap
, pm_message_t state
)
903 bool runtime
= PMSG_IS_AUTO(state
);
904 struct ata_device
*dev
;
908 ata_for_each_dev(dev
, &ap
->link
, ENABLED
) {
909 handle
= ata_dev_acpi_handle(dev
);
913 if (!(state
.event
& PM_EVENT_RESUME
)) {
914 acpi_state
= ata_acpi_choose_suspend_state(dev
, runtime
);
915 if (acpi_state
== ACPI_STATE_D0
)
917 if (runtime
&& zpodd_dev_enabled(dev
) &&
918 acpi_state
== ACPI_STATE_D3_COLD
)
919 zpodd_enable_run_wake(dev
);
920 acpi_bus_set_power(handle
, acpi_state
);
922 if (runtime
&& zpodd_dev_enabled(dev
))
923 zpodd_disable_run_wake(dev
);
924 acpi_bus_set_power(handle
, ACPI_STATE_D0
);
929 /* ACPI spec requires _PS0 when IDE power on and _PS3 when power off */
930 static void pata_acpi_set_state(struct ata_port
*ap
, pm_message_t state
)
932 struct ata_device
*dev
;
933 acpi_handle port_handle
;
935 port_handle
= ata_ap_acpi_handle(ap
);
939 /* channel first and then drives for power on and vica versa
941 if (state
.event
& PM_EVENT_RESUME
)
942 acpi_bus_set_power(port_handle
, ACPI_STATE_D0
);
944 ata_for_each_dev(dev
, &ap
->link
, ENABLED
) {
945 acpi_handle dev_handle
= ata_dev_acpi_handle(dev
);
949 acpi_bus_set_power(dev_handle
, state
.event
& PM_EVENT_RESUME
?
950 ACPI_STATE_D0
: ACPI_STATE_D3
);
953 if (!(state
.event
& PM_EVENT_RESUME
))
954 acpi_bus_set_power(port_handle
, ACPI_STATE_D3
);
958 * ata_acpi_set_state - set the port power state
959 * @ap: target ATA port
960 * @state: state, on/off
962 * This function sets a proper ACPI D state for the device on
963 * system and runtime PM operations.
965 void ata_acpi_set_state(struct ata_port
*ap
, pm_message_t state
)
967 if (ap
->flags
& ATA_FLAG_ACPI_SATA
)
968 sata_acpi_set_state(ap
, state
);
970 pata_acpi_set_state(ap
, state
);
974 * ata_acpi_on_devcfg - ATA ACPI hook called on device donfiguration
975 * @dev: target ATA device
977 * This function is called when @dev is about to be configured.
978 * IDENTIFY data might have been modified after this hook is run.
984 * Positive number if IDENTIFY data needs to be refreshed, 0 if not,
987 int ata_acpi_on_devcfg(struct ata_device
*dev
)
989 struct ata_port
*ap
= dev
->link
->ap
;
990 struct ata_eh_context
*ehc
= &ap
->link
.eh_context
;
991 int acpi_sata
= ap
->flags
& ATA_FLAG_ACPI_SATA
;
995 if (!ata_dev_acpi_handle(dev
))
998 /* do we need to do _GTF? */
999 if (!(dev
->flags
& ATA_DFLAG_ACPI_PENDING
) &&
1000 !(acpi_sata
&& (ehc
->i
.flags
& ATA_EHI_DID_HARDRESET
)))
1003 /* do _SDD if SATA */
1005 rc
= ata_acpi_push_id(dev
);
1006 if (rc
&& rc
!= -ENOENT
)
1011 rc
= ata_acpi_exec_tfs(dev
, &nr_executed
);
1015 dev
->flags
&= ~ATA_DFLAG_ACPI_PENDING
;
1017 /* refresh IDENTIFY page if any _GTF command has been executed */
1019 rc
= ata_dev_reread_id(dev
, 0);
1022 "failed to IDENTIFY after ACPI commands\n");
1030 /* ignore evaluation failure if we can continue safely */
1031 if (rc
== -EINVAL
&& !nr_executed
&& !(ap
->pflags
& ATA_PFLAG_FROZEN
))
1034 /* fail and let EH retry once more for unknown IO errors */
1035 if (!(dev
->flags
& ATA_DFLAG_ACPI_FAILED
)) {
1036 dev
->flags
|= ATA_DFLAG_ACPI_FAILED
;
1040 dev
->flags
|= ATA_DFLAG_ACPI_DISABLED
;
1041 ata_dev_warn(dev
, "ACPI: failed the second time, disabled\n");
1043 /* We can safely continue if no _GTF command has been executed
1044 * and port is not frozen.
1046 if (!nr_executed
&& !(ap
->pflags
& ATA_PFLAG_FROZEN
))
1053 * ata_acpi_on_disable - ATA ACPI hook called when a device is disabled
1054 * @dev: target ATA device
1056 * This function is called when @dev is about to be disabled.
1061 void ata_acpi_on_disable(struct ata_device
*dev
)
1063 ata_acpi_clear_gtf(dev
);
1066 static int compat_pci_ata(struct ata_port
*ap
)
1068 struct device
*dev
= ap
->tdev
.parent
;
1069 struct pci_dev
*pdev
;
1071 if (!is_pci_dev(dev
))
1074 pdev
= to_pci_dev(dev
);
1076 if ((pdev
->class >> 8) != PCI_CLASS_STORAGE_SATA
&&
1077 (pdev
->class >> 8) != PCI_CLASS_STORAGE_IDE
)
1083 static int ata_acpi_bind_host(struct ata_port
*ap
, acpi_handle
*handle
)
1085 if (libata_noacpi
|| ap
->flags
& ATA_FLAG_ACPI_SATA
)
1088 *handle
= acpi_get_child(DEVICE_ACPI_HANDLE(ap
->tdev
.parent
),
1094 if (__ata_acpi_gtm(ap
, *handle
, &ap
->__acpi_init_gtm
) == 0)
1095 ap
->pflags
|= ATA_PFLAG_INIT_GTM_VALID
;
1100 static int ata_acpi_bind_device(struct ata_port
*ap
, struct scsi_device
*sdev
,
1101 acpi_handle
*handle
)
1103 struct ata_device
*ata_dev
;
1105 if (ap
->flags
& ATA_FLAG_ACPI_SATA
) {
1106 if (!sata_pmp_attached(ap
))
1107 ata_dev
= &ap
->link
.device
[sdev
->id
];
1109 ata_dev
= &ap
->pmp_link
[sdev
->channel
].device
[sdev
->id
];
1112 ata_dev
= &ap
->link
.device
[sdev
->id
];
1115 *handle
= ata_dev_acpi_handle(ata_dev
);
1123 static int is_ata_port(const struct device
*dev
)
1125 return dev
->type
== &ata_port_type
;
1128 static struct ata_port
*dev_to_ata_port(struct device
*dev
)
1130 while (!is_ata_port(dev
)) {
1135 return to_ata_port(dev
);
1138 static int ata_acpi_find_device(struct device
*dev
, acpi_handle
*handle
)
1140 struct ata_port
*ap
= dev_to_ata_port(dev
);
1145 if (!compat_pci_ata(ap
))
1148 if (scsi_is_host_device(dev
))
1149 return ata_acpi_bind_host(ap
, handle
);
1150 else if (scsi_is_sdev_device(dev
)) {
1151 struct scsi_device
*sdev
= to_scsi_device(dev
);
1153 return ata_acpi_bind_device(ap
, sdev
, handle
);
1158 static struct acpi_bus_type ata_acpi_bus
= {
1160 .find_device
= ata_acpi_find_device
,
1163 int ata_acpi_register(void)
1165 return scsi_register_acpi_bus_type(&ata_acpi_bus
);
1168 void ata_acpi_unregister(void)
1170 scsi_unregister_acpi_bus_type(&ata_acpi_bus
);