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 (ap->flags & ATA_FLAG_ACPI_SATA) {
80 if (!sata_pmp_attached(ap))
81 adr = SATA_ADR(ap->port_no, NO_PORT_MULT);
83 adr = SATA_ADR(ap->port_no, dev->link->pmp);
84 return acpi_get_child(DEVICE_ACPI_HANDLE(ap->host->dev), adr);
86 return acpi_get_child(ata_ap_acpi_handle(ap), dev->devno);
88 EXPORT_SYMBOL(ata_dev_acpi_handle);
90 /* @ap and @dev are the same as ata_acpi_handle_hotplug() */
91 static void ata_acpi_detach_device(struct ata_port *ap, struct ata_device *dev)
94 dev->flags |= ATA_DFLAG_DETACH;
96 struct ata_link *tlink;
97 struct ata_device *tdev;
99 ata_for_each_link(tlink, ap, EDGE)
100 ata_for_each_dev(tdev, tlink, ALL)
101 tdev->flags |= ATA_DFLAG_DETACH;
104 ata_port_schedule_eh(ap);
108 * ata_acpi_handle_hotplug - ACPI event handler backend
109 * @ap: ATA port ACPI event occurred
110 * @dev: ATA device ACPI event occurred (can be NULL)
111 * @event: ACPI event which occurred
113 * All ACPI bay / device realted events end up in this function. If
114 * the event is port-wide @dev is NULL. If the event is specific to a
115 * device, @dev points to it.
117 * Hotplug (as opposed to unplug) notification is always handled as
118 * port-wide while unplug only kills the target device on device-wide
122 * ACPI notify handler context. May sleep.
124 static void ata_acpi_handle_hotplug(struct ata_port *ap, struct ata_device *dev,
127 struct ata_eh_info *ehi = &ap->link.eh_info;
131 spin_lock_irqsave(ap->lock, flags);
133 * When dock driver calls into the routine, it will always use
134 * ACPI_NOTIFY_BUS_CHECK/ACPI_NOTIFY_DEVICE_CHECK for add and
135 * ACPI_NOTIFY_EJECT_REQUEST for remove
138 case ACPI_NOTIFY_BUS_CHECK:
139 case ACPI_NOTIFY_DEVICE_CHECK:
140 ata_ehi_push_desc(ehi, "ACPI event");
142 ata_ehi_hotplugged(ehi);
145 case ACPI_NOTIFY_EJECT_REQUEST:
146 ata_ehi_push_desc(ehi, "ACPI event");
148 ata_acpi_detach_device(ap, dev);
153 spin_unlock_irqrestore(ap->lock, flags);
156 ata_port_wait_eh(ap);
159 static void ata_acpi_dev_notify_dock(acpi_handle handle, u32 event, void *data)
161 struct ata_device *dev = data;
163 ata_acpi_handle_hotplug(dev->link->ap, dev, event);
166 static void ata_acpi_ap_notify_dock(acpi_handle handle, u32 event, void *data)
168 struct ata_port *ap = data;
170 ata_acpi_handle_hotplug(ap, NULL, event);
173 static void ata_acpi_uevent(struct ata_port *ap, struct ata_device *dev,
176 struct kobject *kobj = NULL;
177 char event_string[20];
178 char *envp[] = { event_string, NULL };
182 kobj = &dev->sdev->sdev_gendev.kobj;
184 kobj = &ap->dev->kobj;
187 snprintf(event_string, 20, "BAY_EVENT=%d", event);
188 kobject_uevent_env(kobj, KOBJ_CHANGE, envp);
192 static void ata_acpi_ap_uevent(acpi_handle handle, u32 event, void *data)
194 ata_acpi_uevent(data, NULL, event);
197 static void ata_acpi_dev_uevent(acpi_handle handle, u32 event, void *data)
199 struct ata_device *dev = data;
200 ata_acpi_uevent(dev->link->ap, dev, event);
203 static const struct acpi_dock_ops ata_acpi_dev_dock_ops = {
204 .handler = ata_acpi_dev_notify_dock,
205 .uevent = ata_acpi_dev_uevent,
208 static const struct acpi_dock_ops ata_acpi_ap_dock_ops = {
209 .handler = ata_acpi_ap_notify_dock,
210 .uevent = ata_acpi_ap_uevent,
214 * ata_acpi_dissociate - dissociate ATA host from ACPI objects
215 * @host: target ATA host
217 * This function is called during driver detach after the whole host
223 void ata_acpi_dissociate(struct ata_host *host)
227 /* Restore initial _GTM values so that driver which attaches
228 * afterward can use them too.
230 for (i = 0; i < host->n_ports; i++) {
231 struct ata_port *ap = host->ports[i];
232 const struct ata_acpi_gtm *gtm = ata_acpi_init_gtm(ap);
234 if (ata_ap_acpi_handle(ap) && gtm)
235 ata_acpi_stm(ap, gtm);
240 * ata_acpi_gtm - execute _GTM
241 * @ap: target ATA port
242 * @gtm: out parameter for _GTM result
244 * Evaluate _GTM and store the result in @gtm.
250 * 0 on success, -ENOENT if _GTM doesn't exist, -errno on failure.
252 int ata_acpi_gtm(struct ata_port *ap, struct ata_acpi_gtm *gtm)
254 struct acpi_buffer output = { .length = ACPI_ALLOCATE_BUFFER };
255 union acpi_object *out_obj;
259 status = acpi_evaluate_object(ata_ap_acpi_handle(ap), "_GTM", NULL,
263 if (status == AE_NOT_FOUND)
267 if (ACPI_FAILURE(status)) {
268 ata_port_err(ap, "ACPI get timing mode failed (AE 0x%x)\n",
273 out_obj = output.pointer;
274 if (out_obj->type != ACPI_TYPE_BUFFER) {
275 ata_port_warn(ap, "_GTM returned unexpected object type 0x%x\n",
281 if (out_obj->buffer.length != sizeof(struct ata_acpi_gtm)) {
282 ata_port_err(ap, "_GTM returned invalid length %d\n",
283 out_obj->buffer.length);
287 memcpy(gtm, out_obj->buffer.pointer, sizeof(struct ata_acpi_gtm));
290 kfree(output.pointer);
294 EXPORT_SYMBOL_GPL(ata_acpi_gtm);
297 * ata_acpi_stm - execute _STM
298 * @ap: target ATA port
299 * @stm: timing parameter to _STM
301 * Evaluate _STM with timing parameter @stm.
307 * 0 on success, -ENOENT if _STM doesn't exist, -errno on failure.
309 int ata_acpi_stm(struct ata_port *ap, const struct ata_acpi_gtm *stm)
312 struct ata_acpi_gtm stm_buf = *stm;
313 struct acpi_object_list input;
314 union acpi_object in_params[3];
316 in_params[0].type = ACPI_TYPE_BUFFER;
317 in_params[0].buffer.length = sizeof(struct ata_acpi_gtm);
318 in_params[0].buffer.pointer = (u8 *)&stm_buf;
319 /* Buffers for id may need byteswapping ? */
320 in_params[1].type = ACPI_TYPE_BUFFER;
321 in_params[1].buffer.length = 512;
322 in_params[1].buffer.pointer = (u8 *)ap->link.device[0].id;
323 in_params[2].type = ACPI_TYPE_BUFFER;
324 in_params[2].buffer.length = 512;
325 in_params[2].buffer.pointer = (u8 *)ap->link.device[1].id;
328 input.pointer = in_params;
330 status = acpi_evaluate_object(ata_ap_acpi_handle(ap), "_STM", &input,
333 if (status == AE_NOT_FOUND)
335 if (ACPI_FAILURE(status)) {
336 ata_port_err(ap, "ACPI set timing mode failed (status=0x%x)\n",
343 EXPORT_SYMBOL_GPL(ata_acpi_stm);
346 * ata_dev_get_GTF - get the drive bootup default taskfile settings
347 * @dev: target ATA device
348 * @gtf: output parameter for buffer containing _GTF taskfile arrays
350 * This applies to both PATA and SATA drives.
352 * The _GTF method has no input parameters.
353 * It returns a variable number of register set values (registers
354 * hex 1F1..1F7, taskfiles).
355 * The <variable number> is not known in advance, so have ACPI-CA
356 * allocate the buffer as needed and return it, then free it later.
362 * Number of taskfiles on success, 0 if _GTF doesn't exist. -EINVAL
363 * if _GTF is invalid.
365 static int ata_dev_get_GTF(struct ata_device *dev, struct ata_acpi_gtf **gtf)
367 struct ata_port *ap = dev->link->ap;
369 struct acpi_buffer output;
370 union acpi_object *out_obj;
373 /* if _GTF is cached, use the cached value */
374 if (dev->gtf_cache) {
375 out_obj = dev->gtf_cache;
379 /* set up output buffer */
380 output.length = ACPI_ALLOCATE_BUFFER;
381 output.pointer = NULL; /* ACPI-CA sets this; save/free it later */
383 if (ata_msg_probe(ap))
384 ata_dev_dbg(dev, "%s: ENTER: port#: %d\n",
385 __func__, ap->port_no);
387 /* _GTF has no input parameters */
388 status = acpi_evaluate_object(ata_dev_acpi_handle(dev), "_GTF", NULL,
390 out_obj = dev->gtf_cache = output.pointer;
392 if (ACPI_FAILURE(status)) {
393 if (status != AE_NOT_FOUND) {
394 ata_dev_warn(dev, "_GTF evaluation failed (AE 0x%x)\n",
401 if (!output.length || !output.pointer) {
402 if (ata_msg_probe(ap))
403 ata_dev_dbg(dev, "%s: Run _GTF: length or ptr is NULL (0x%llx, 0x%p)\n",
405 (unsigned long long)output.length,
411 if (out_obj->type != ACPI_TYPE_BUFFER) {
412 ata_dev_warn(dev, "_GTF unexpected object type 0x%x\n",
418 if (out_obj->buffer.length % REGS_PER_GTF) {
419 ata_dev_warn(dev, "unexpected _GTF length (%d)\n",
420 out_obj->buffer.length);
426 rc = out_obj->buffer.length / REGS_PER_GTF;
428 *gtf = (void *)out_obj->buffer.pointer;
429 if (ata_msg_probe(ap))
430 ata_dev_dbg(dev, "%s: returning gtf=%p, gtf_count=%d\n",
436 ata_acpi_clear_gtf(dev);
441 * ata_acpi_gtm_xfermode - determine xfermode from GTM parameter
442 * @dev: target device
443 * @gtm: GTM parameter to use
445 * Determine xfermask for @dev from @gtm.
451 * Determined xfermask.
453 unsigned long ata_acpi_gtm_xfermask(struct ata_device *dev,
454 const struct ata_acpi_gtm *gtm)
456 unsigned long xfer_mask = 0;
461 /* we always use the 0 slot for crap hardware */
463 if (!(gtm->flags & 0x10))
467 mode = ata_timing_cycle2mode(ATA_SHIFT_PIO, gtm->drive[unit].pio);
468 xfer_mask |= ata_xfer_mode2mask(mode);
470 /* See if we have MWDMA or UDMA data. We don't bother with
471 * MWDMA if UDMA is available as this means the BIOS set UDMA
472 * and our error changedown if it works is UDMA to PIO anyway.
474 if (!(gtm->flags & (1 << (2 * unit))))
475 type = ATA_SHIFT_MWDMA;
477 type = ATA_SHIFT_UDMA;
479 mode = ata_timing_cycle2mode(type, gtm->drive[unit].dma);
480 xfer_mask |= ata_xfer_mode2mask(mode);
484 EXPORT_SYMBOL_GPL(ata_acpi_gtm_xfermask);
487 * ata_acpi_cbl_80wire - Check for 80 wire cable
489 * @gtm: GTM data to use
491 * Return 1 if the @gtm indicates the BIOS selected an 80wire mode.
493 int ata_acpi_cbl_80wire(struct ata_port *ap, const struct ata_acpi_gtm *gtm)
495 struct ata_device *dev;
497 ata_for_each_dev(dev, &ap->link, ENABLED) {
498 unsigned long xfer_mask, udma_mask;
500 xfer_mask = ata_acpi_gtm_xfermask(dev, gtm);
501 ata_unpack_xfermask(xfer_mask, NULL, NULL, &udma_mask);
503 if (udma_mask & ~ATA_UDMA_MASK_40C)
509 EXPORT_SYMBOL_GPL(ata_acpi_cbl_80wire);
511 static void ata_acpi_gtf_to_tf(struct ata_device *dev,
512 const struct ata_acpi_gtf *gtf,
513 struct ata_taskfile *tf)
515 ata_tf_init(dev, tf);
517 tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
518 tf->protocol = ATA_PROT_NODATA;
519 tf->feature = gtf->tf[0]; /* 0x1f1 */
520 tf->nsect = gtf->tf[1]; /* 0x1f2 */
521 tf->lbal = gtf->tf[2]; /* 0x1f3 */
522 tf->lbam = gtf->tf[3]; /* 0x1f4 */
523 tf->lbah = gtf->tf[4]; /* 0x1f5 */
524 tf->device = gtf->tf[5]; /* 0x1f6 */
525 tf->command = gtf->tf[6]; /* 0x1f7 */
528 static int ata_acpi_filter_tf(struct ata_device *dev,
529 const struct ata_taskfile *tf,
530 const struct ata_taskfile *ptf)
532 if (dev->gtf_filter & ATA_ACPI_FILTER_SETXFER) {
533 /* libata doesn't use ACPI to configure transfer mode.
534 * It will only confuse device configuration. Skip.
536 if (tf->command == ATA_CMD_SET_FEATURES &&
537 tf->feature == SETFEATURES_XFER)
541 if (dev->gtf_filter & ATA_ACPI_FILTER_LOCK) {
542 /* BIOS writers, sorry but we don't wanna lock
543 * features unless the user explicitly said so.
546 /* DEVICE CONFIGURATION FREEZE LOCK */
547 if (tf->command == ATA_CMD_CONF_OVERLAY &&
548 tf->feature == ATA_DCO_FREEZE_LOCK)
551 /* SECURITY FREEZE LOCK */
552 if (tf->command == ATA_CMD_SEC_FREEZE_LOCK)
555 /* SET MAX LOCK and SET MAX FREEZE LOCK */
556 if ((!ptf || ptf->command != ATA_CMD_READ_NATIVE_MAX) &&
557 tf->command == ATA_CMD_SET_MAX &&
558 (tf->feature == ATA_SET_MAX_LOCK ||
559 tf->feature == ATA_SET_MAX_FREEZE_LOCK))
563 if (tf->command == ATA_CMD_SET_FEATURES &&
564 tf->feature == SETFEATURES_SATA_ENABLE) {
565 /* inhibit enabling DIPM */
566 if (dev->gtf_filter & ATA_ACPI_FILTER_DIPM &&
567 tf->nsect == SATA_DIPM)
570 /* inhibit FPDMA non-zero offset */
571 if (dev->gtf_filter & ATA_ACPI_FILTER_FPDMA_OFFSET &&
572 (tf->nsect == SATA_FPDMA_OFFSET ||
573 tf->nsect == SATA_FPDMA_IN_ORDER))
576 /* inhibit FPDMA auto activation */
577 if (dev->gtf_filter & ATA_ACPI_FILTER_FPDMA_AA &&
578 tf->nsect == SATA_FPDMA_AA)
586 * ata_acpi_run_tf - send taskfile registers to host controller
587 * @dev: target ATA device
588 * @gtf: raw ATA taskfile register set (0x1f1 - 0x1f7)
590 * Outputs ATA taskfile to standard ATA host controller.
591 * Writes the control, feature, nsect, lbal, lbam, and lbah registers.
592 * Optionally (ATA_TFLAG_LBA48) writes hob_feature, hob_nsect,
593 * hob_lbal, hob_lbam, and hob_lbah.
595 * This function waits for idle (!BUSY and !DRQ) after writing
596 * registers. If the control register has a new value, this
597 * function also waits for idle after writing control and before
598 * writing the remaining registers.
604 * 1 if command is executed successfully. 0 if ignored, rejected or
605 * filtered out, -errno on other errors.
607 static int ata_acpi_run_tf(struct ata_device *dev,
608 const struct ata_acpi_gtf *gtf,
609 const struct ata_acpi_gtf *prev_gtf)
611 struct ata_taskfile *pptf = NULL;
612 struct ata_taskfile tf, ptf, rtf;
613 unsigned int err_mask;
619 if ((gtf->tf[0] == 0) && (gtf->tf[1] == 0) && (gtf->tf[2] == 0)
620 && (gtf->tf[3] == 0) && (gtf->tf[4] == 0) && (gtf->tf[5] == 0)
621 && (gtf->tf[6] == 0))
624 ata_acpi_gtf_to_tf(dev, gtf, &tf);
626 ata_acpi_gtf_to_tf(dev, prev_gtf, &ptf);
630 if (!ata_acpi_filter_tf(dev, &tf, pptf)) {
632 err_mask = ata_exec_internal(dev, &rtf, NULL,
633 DMA_NONE, NULL, 0, 0);
638 snprintf(msg, sizeof(msg), "succeeded");
644 snprintf(msg, sizeof(msg),
645 "rejected by device (Stat=0x%02x Err=0x%02x)",
646 rtf.command, rtf.feature);
652 snprintf(msg, sizeof(msg),
653 "failed (Emask=0x%x Stat=0x%02x Err=0x%02x)",
654 err_mask, rtf.command, rtf.feature);
660 snprintf(msg, sizeof(msg), "filtered out");
663 descr = ata_get_cmd_descript(tf.command);
665 ata_dev_printk(dev, level,
666 "ACPI cmd %02x/%02x:%02x:%02x:%02x:%02x:%02x (%s) %s\n",
667 tf.command, tf.feature, tf.nsect, tf.lbal,
668 tf.lbam, tf.lbah, tf.device,
669 (descr ? descr : "unknown"), msg);
675 * ata_acpi_exec_tfs - get then write drive taskfile settings
676 * @dev: target ATA device
677 * @nr_executed: out parameter for the number of executed commands
679 * Evaluate _GTF and execute returned taskfiles.
685 * Number of executed taskfiles on success, 0 if _GTF doesn't exist.
686 * -errno on other errors.
688 static int ata_acpi_exec_tfs(struct ata_device *dev, int *nr_executed)
690 struct ata_acpi_gtf *gtf = NULL, *pgtf = NULL;
691 int gtf_count, i, rc;
694 rc = ata_dev_get_GTF(dev, >f);
700 for (i = 0; i < gtf_count; i++, gtf++) {
701 rc = ata_acpi_run_tf(dev, gtf, pgtf);
710 ata_acpi_clear_gtf(dev);
718 * ata_acpi_push_id - send Identify data to drive
719 * @dev: target ATA device
721 * _SDD ACPI object: for SATA mode only
722 * Must be after Identify (Packet) Device -- uses its data
723 * ATM this function never returns a failure. It is an optional
724 * method and if it fails for whatever reason, we should still
731 * 0 on success, -ENOENT if _SDD doesn't exist, -errno on failure.
733 static int ata_acpi_push_id(struct ata_device *dev)
735 struct ata_port *ap = dev->link->ap;
737 struct acpi_object_list input;
738 union acpi_object in_params[1];
740 if (ata_msg_probe(ap))
741 ata_dev_dbg(dev, "%s: ix = %d, port#: %d\n",
742 __func__, dev->devno, ap->port_no);
744 /* Give the drive Identify data to the drive via the _SDD method */
745 /* _SDD: set up input parameters */
747 input.pointer = in_params;
748 in_params[0].type = ACPI_TYPE_BUFFER;
749 in_params[0].buffer.length = sizeof(dev->id[0]) * ATA_ID_WORDS;
750 in_params[0].buffer.pointer = (u8 *)dev->id;
751 /* Output buffer: _SDD has no output */
753 /* It's OK for _SDD to be missing too. */
754 swap_buf_le16(dev->id, ATA_ID_WORDS);
755 status = acpi_evaluate_object(ata_dev_acpi_handle(dev), "_SDD", &input,
757 swap_buf_le16(dev->id, ATA_ID_WORDS);
759 if (status == AE_NOT_FOUND)
762 if (ACPI_FAILURE(status)) {
763 ata_dev_warn(dev, "ACPI _SDD failed (AE 0x%x)\n", status);
771 * ata_acpi_on_suspend - ATA ACPI hook called on suspend
772 * @ap: target ATA port
774 * This function is called when @ap is about to be suspended. All
775 * devices are already put to sleep but the port_suspend() callback
776 * hasn't been executed yet. Error return from this function aborts
783 * 0 on success, -errno on failure.
785 int ata_acpi_on_suspend(struct ata_port *ap)
792 * ata_acpi_on_resume - ATA ACPI hook called on resume
793 * @ap: target ATA port
795 * This function is called when @ap is resumed - right after port
796 * itself is resumed but before any EH action is taken.
801 void ata_acpi_on_resume(struct ata_port *ap)
803 const struct ata_acpi_gtm *gtm = ata_acpi_init_gtm(ap);
804 struct ata_device *dev;
806 if (ata_ap_acpi_handle(ap) && gtm) {
809 /* restore timing parameters */
810 ata_acpi_stm(ap, gtm);
812 /* _GTF should immediately follow _STM so that it can
813 * use values set by _STM. Cache _GTF result and
816 ata_for_each_dev(dev, &ap->link, ALL) {
817 ata_acpi_clear_gtf(dev);
818 if (ata_dev_enabled(dev) &&
819 ata_dev_get_GTF(dev, NULL) >= 0)
820 dev->flags |= ATA_DFLAG_ACPI_PENDING;
823 /* SATA _GTF needs to be evaulated after _SDD and
824 * there's no reason to evaluate IDE _GTF early
825 * without _STM. Clear cache and schedule _GTF.
827 ata_for_each_dev(dev, &ap->link, ALL) {
828 ata_acpi_clear_gtf(dev);
829 if (ata_dev_enabled(dev))
830 dev->flags |= ATA_DFLAG_ACPI_PENDING;
836 * ata_acpi_set_state - set the port power state
837 * @ap: target ATA port
838 * @state: state, on/off
840 * This function executes the _PS0/_PS3 ACPI method to set the power state.
841 * ACPI spec requires _PS0 when IDE power on and _PS3 when power off
843 void ata_acpi_set_state(struct ata_port *ap, pm_message_t state)
845 struct ata_device *dev;
849 /* channel first and then drives for power on and vica versa
851 handle = ata_ap_acpi_handle(ap);
852 if (handle && state.event == PM_EVENT_ON)
853 acpi_bus_set_power(handle, ACPI_STATE_D0);
855 ata_for_each_dev(dev, &ap->link, ENABLED) {
856 handle = ata_dev_acpi_handle(dev);
860 if (state.event != PM_EVENT_ON) {
861 acpi_state = acpi_pm_device_sleep_state(
862 &dev->sdev->sdev_gendev, NULL, ACPI_STATE_D3);
864 acpi_bus_set_power(handle, acpi_state);
865 /* TBD: need to check if it's runtime pm request */
866 acpi_pm_device_run_wake(
867 &dev->sdev->sdev_gendev, true);
870 acpi_pm_device_run_wake(
871 &dev->sdev->sdev_gendev, false);
872 acpi_bus_set_power(handle, ACPI_STATE_D0);
876 handle = ata_ap_acpi_handle(ap);
877 if (handle && state.event != PM_EVENT_ON)
878 acpi_bus_set_power(handle, ACPI_STATE_D3);
882 * ata_acpi_on_devcfg - ATA ACPI hook called on device donfiguration
883 * @dev: target ATA device
885 * This function is called when @dev is about to be configured.
886 * IDENTIFY data might have been modified after this hook is run.
892 * Positive number if IDENTIFY data needs to be refreshed, 0 if not,
895 int ata_acpi_on_devcfg(struct ata_device *dev)
897 struct ata_port *ap = dev->link->ap;
898 struct ata_eh_context *ehc = &ap->link.eh_context;
899 int acpi_sata = ap->flags & ATA_FLAG_ACPI_SATA;
903 if (!ata_dev_acpi_handle(dev))
906 /* do we need to do _GTF? */
907 if (!(dev->flags & ATA_DFLAG_ACPI_PENDING) &&
908 !(acpi_sata && (ehc->i.flags & ATA_EHI_DID_HARDRESET)))
911 /* do _SDD if SATA */
913 rc = ata_acpi_push_id(dev);
914 if (rc && rc != -ENOENT)
919 rc = ata_acpi_exec_tfs(dev, &nr_executed);
923 dev->flags &= ~ATA_DFLAG_ACPI_PENDING;
925 /* refresh IDENTIFY page if any _GTF command has been executed */
927 rc = ata_dev_reread_id(dev, 0);
930 "failed to IDENTIFY after ACPI commands\n");
938 /* ignore evaluation failure if we can continue safely */
939 if (rc == -EINVAL && !nr_executed && !(ap->pflags & ATA_PFLAG_FROZEN))
942 /* fail and let EH retry once more for unknown IO errors */
943 if (!(dev->flags & ATA_DFLAG_ACPI_FAILED)) {
944 dev->flags |= ATA_DFLAG_ACPI_FAILED;
948 ata_dev_warn(dev, "ACPI: failed the second time, disabled\n");
950 /* We can safely continue if no _GTF command has been executed
951 * and port is not frozen.
953 if (!nr_executed && !(ap->pflags & ATA_PFLAG_FROZEN))
960 * ata_acpi_on_disable - ATA ACPI hook called when a device is disabled
961 * @dev: target ATA device
963 * This function is called when @dev is about to be disabled.
968 void ata_acpi_on_disable(struct ata_device *dev)
970 ata_acpi_clear_gtf(dev);
973 static void ata_acpi_wake_dev(acpi_handle handle, u32 event, void *context)
975 struct ata_device *ata_dev = context;
977 if (event == ACPI_NOTIFY_DEVICE_WAKE && ata_dev &&
978 pm_runtime_suspended(&ata_dev->sdev->sdev_gendev))
979 scsi_autopm_get_device(ata_dev->sdev);
982 static void ata_acpi_add_pm_notifier(struct ata_device *dev)
984 struct acpi_device *acpi_dev;
988 handle = ata_dev_acpi_handle(dev);
992 status = acpi_bus_get_device(handle, &acpi_dev);
993 if (ACPI_FAILURE(status))
996 if (dev->sdev->can_power_off) {
997 acpi_install_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
998 ata_acpi_wake_dev, dev);
999 device_set_run_wake(&dev->sdev->sdev_gendev, true);
1003 static void ata_acpi_remove_pm_notifier(struct ata_device *dev)
1005 struct acpi_device *acpi_dev;
1009 handle = ata_dev_acpi_handle(dev);
1013 status = acpi_bus_get_device(handle, &acpi_dev);
1014 if (ACPI_FAILURE(status))
1017 if (dev->sdev->can_power_off) {
1018 device_set_run_wake(&dev->sdev->sdev_gendev, false);
1019 acpi_remove_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
1024 static void ata_acpi_register_power_resource(struct ata_device *dev)
1026 struct scsi_device *sdev = dev->sdev;
1028 struct device *device;
1030 handle = ata_dev_acpi_handle(dev);
1034 device = &sdev->sdev_gendev;
1036 acpi_power_resource_register_device(device, handle);
1039 static void ata_acpi_unregister_power_resource(struct ata_device *dev)
1041 struct scsi_device *sdev = dev->sdev;
1043 struct device *device;
1045 handle = ata_dev_acpi_handle(dev);
1049 device = &sdev->sdev_gendev;
1051 acpi_power_resource_unregister_device(device, handle);
1054 void ata_acpi_bind(struct ata_device *dev)
1056 ata_acpi_add_pm_notifier(dev);
1057 ata_acpi_register_power_resource(dev);
1060 void ata_acpi_unbind(struct ata_device *dev)
1062 ata_acpi_remove_pm_notifier(dev);
1063 ata_acpi_unregister_power_resource(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 (ap->flags & ATA_FLAG_ACPI_SATA)
1088 *handle = acpi_get_child(DEVICE_ACPI_HANDLE(ap->tdev.parent),
1094 if (ata_acpi_gtm(ap, &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 struct acpi_device *acpi_dev;
1106 struct acpi_device_power_state *states;
1108 if (ap->flags & ATA_FLAG_ACPI_SATA)
1109 ata_dev = &ap->link.device[sdev->channel];
1111 ata_dev = &ap->link.device[sdev->id];
1113 *handle = ata_dev_acpi_handle(ata_dev);
1118 status = acpi_bus_get_device(*handle, &acpi_dev);
1119 if (ACPI_FAILURE(status))
1123 * If firmware has _PS3 or _PR3 for this device,
1124 * and this ata ODD device support device attention,
1125 * it means this device can be powered off
1127 states = acpi_dev->power.states;
1128 if ((states[ACPI_STATE_D3_HOT].flags.valid ||
1129 states[ACPI_STATE_D3_COLD].flags.explicit_set) &&
1130 ata_dev->flags & ATA_DFLAG_DA)
1131 sdev->can_power_off = 1;
1136 static int is_ata_port(const struct device *dev)
1138 return dev->type == &ata_port_type;
1141 static struct ata_port *dev_to_ata_port(struct device *dev)
1143 while (!is_ata_port(dev)) {
1148 return to_ata_port(dev);
1151 static int ata_acpi_find_device(struct device *dev, acpi_handle *handle)
1153 struct ata_port *ap = dev_to_ata_port(dev);
1158 if (!compat_pci_ata(ap))
1161 if (scsi_is_host_device(dev))
1162 return ata_acpi_bind_host(ap, handle);
1163 else if (scsi_is_sdev_device(dev)) {
1164 struct scsi_device *sdev = to_scsi_device(dev);
1166 return ata_acpi_bind_device(ap, sdev, handle);
1171 static int ata_acpi_find_dummy(struct device *dev, acpi_handle *handle)
1176 static struct acpi_bus_type ata_acpi_bus = {
1177 .find_bridge = ata_acpi_find_dummy,
1178 .find_device = ata_acpi_find_device,
1181 int ata_acpi_register(void)
1183 return scsi_register_acpi_bus_type(&ata_acpi_bus);
1186 void ata_acpi_unregister(void)
1188 scsi_unregister_acpi_bus_type(&ata_acpi_bus);