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)
64 * If acpi bind operation has already happened, we can get the handle
65 * for the port by checking the corresponding scsi_host device's
66 * firmware node, otherwise we will need to find out the handle from
67 * its parent's acpi node.
70 return DEVICE_ACPI_HANDLE(&ap->scsi_host->shost_gendev);
72 return acpi_get_child(DEVICE_ACPI_HANDLE(ap->host->dev),
75 EXPORT_SYMBOL(ata_ap_acpi_handle);
78 * ata_dev_acpi_handle - provide the acpi_handle for an ata_device
79 * @dev: the acpi_device returned will correspond to this port
81 * Returns the acpi_handle for the ACPI namespace object corresponding to
82 * the ata_device passed into the function, or NULL if no such object exists
84 acpi_handle ata_dev_acpi_handle(struct ata_device *dev)
87 struct ata_port *ap = dev->link->ap;
89 if (ap->flags & ATA_FLAG_ACPI_SATA) {
90 if (!sata_pmp_attached(ap))
91 adr = SATA_ADR(ap->port_no, NO_PORT_MULT);
93 adr = SATA_ADR(ap->port_no, dev->link->pmp);
94 return acpi_get_child(DEVICE_ACPI_HANDLE(ap->host->dev), adr);
96 return acpi_get_child(ata_ap_acpi_handle(ap), dev->devno);
98 EXPORT_SYMBOL(ata_dev_acpi_handle);
100 /* @ap and @dev are the same as ata_acpi_handle_hotplug() */
101 static void ata_acpi_detach_device(struct ata_port *ap, struct ata_device *dev)
104 dev->flags |= ATA_DFLAG_DETACH;
106 struct ata_link *tlink;
107 struct ata_device *tdev;
109 ata_for_each_link(tlink, ap, EDGE)
110 ata_for_each_dev(tdev, tlink, ALL)
111 tdev->flags |= ATA_DFLAG_DETACH;
114 ata_port_schedule_eh(ap);
118 * ata_acpi_handle_hotplug - ACPI event handler backend
119 * @ap: ATA port ACPI event occurred
120 * @dev: ATA device ACPI event occurred (can be NULL)
121 * @event: ACPI event which occurred
123 * All ACPI bay / device realted events end up in this function. If
124 * the event is port-wide @dev is NULL. If the event is specific to a
125 * device, @dev points to it.
127 * Hotplug (as opposed to unplug) notification is always handled as
128 * port-wide while unplug only kills the target device on device-wide
132 * ACPI notify handler context. May sleep.
134 static void ata_acpi_handle_hotplug(struct ata_port *ap, struct ata_device *dev,
137 struct ata_eh_info *ehi = &ap->link.eh_info;
141 spin_lock_irqsave(ap->lock, flags);
143 * When dock driver calls into the routine, it will always use
144 * ACPI_NOTIFY_BUS_CHECK/ACPI_NOTIFY_DEVICE_CHECK for add and
145 * ACPI_NOTIFY_EJECT_REQUEST for remove
148 case ACPI_NOTIFY_BUS_CHECK:
149 case ACPI_NOTIFY_DEVICE_CHECK:
150 ata_ehi_push_desc(ehi, "ACPI event");
152 ata_ehi_hotplugged(ehi);
155 case ACPI_NOTIFY_EJECT_REQUEST:
156 ata_ehi_push_desc(ehi, "ACPI event");
158 ata_acpi_detach_device(ap, dev);
163 spin_unlock_irqrestore(ap->lock, flags);
166 ata_port_wait_eh(ap);
169 static void ata_acpi_dev_notify_dock(acpi_handle handle, u32 event, void *data)
171 struct ata_device *dev = data;
173 ata_acpi_handle_hotplug(dev->link->ap, dev, event);
176 static void ata_acpi_ap_notify_dock(acpi_handle handle, u32 event, void *data)
178 struct ata_port *ap = data;
180 ata_acpi_handle_hotplug(ap, NULL, event);
183 static void ata_acpi_uevent(struct ata_port *ap, struct ata_device *dev,
186 struct kobject *kobj = NULL;
187 char event_string[20];
188 char *envp[] = { event_string, NULL };
192 kobj = &dev->sdev->sdev_gendev.kobj;
194 kobj = &ap->dev->kobj;
197 snprintf(event_string, 20, "BAY_EVENT=%d", event);
198 kobject_uevent_env(kobj, KOBJ_CHANGE, envp);
202 static void ata_acpi_ap_uevent(acpi_handle handle, u32 event, void *data)
204 ata_acpi_uevent(data, NULL, event);
207 static void ata_acpi_dev_uevent(acpi_handle handle, u32 event, void *data)
209 struct ata_device *dev = data;
210 ata_acpi_uevent(dev->link->ap, dev, event);
213 static const struct acpi_dock_ops ata_acpi_dev_dock_ops = {
214 .handler = ata_acpi_dev_notify_dock,
215 .uevent = ata_acpi_dev_uevent,
218 static const struct acpi_dock_ops ata_acpi_ap_dock_ops = {
219 .handler = ata_acpi_ap_notify_dock,
220 .uevent = ata_acpi_ap_uevent,
224 * ata_acpi_dissociate - dissociate ATA host from ACPI objects
225 * @host: target ATA host
227 * This function is called during driver detach after the whole host
233 void ata_acpi_dissociate(struct ata_host *host)
237 /* Restore initial _GTM values so that driver which attaches
238 * afterward can use them too.
240 for (i = 0; i < host->n_ports; i++) {
241 struct ata_port *ap = host->ports[i];
242 const struct ata_acpi_gtm *gtm = ata_acpi_init_gtm(ap);
244 if (ata_ap_acpi_handle(ap) && gtm)
245 ata_acpi_stm(ap, gtm);
250 * ata_acpi_gtm - execute _GTM
251 * @ap: target ATA port
252 * @gtm: out parameter for _GTM result
254 * Evaluate _GTM and store the result in @gtm.
260 * 0 on success, -ENOENT if _GTM doesn't exist, -errno on failure.
262 int ata_acpi_gtm(struct ata_port *ap, struct ata_acpi_gtm *gtm)
264 struct acpi_buffer output = { .length = ACPI_ALLOCATE_BUFFER };
265 union acpi_object *out_obj;
269 status = acpi_evaluate_object(ata_ap_acpi_handle(ap), "_GTM", NULL,
273 if (status == AE_NOT_FOUND)
277 if (ACPI_FAILURE(status)) {
278 ata_port_err(ap, "ACPI get timing mode failed (AE 0x%x)\n",
283 out_obj = output.pointer;
284 if (out_obj->type != ACPI_TYPE_BUFFER) {
285 ata_port_warn(ap, "_GTM returned unexpected object type 0x%x\n",
291 if (out_obj->buffer.length != sizeof(struct ata_acpi_gtm)) {
292 ata_port_err(ap, "_GTM returned invalid length %d\n",
293 out_obj->buffer.length);
297 memcpy(gtm, out_obj->buffer.pointer, sizeof(struct ata_acpi_gtm));
300 kfree(output.pointer);
304 EXPORT_SYMBOL_GPL(ata_acpi_gtm);
307 * ata_acpi_stm - execute _STM
308 * @ap: target ATA port
309 * @stm: timing parameter to _STM
311 * Evaluate _STM with timing parameter @stm.
317 * 0 on success, -ENOENT if _STM doesn't exist, -errno on failure.
319 int ata_acpi_stm(struct ata_port *ap, const struct ata_acpi_gtm *stm)
322 struct ata_acpi_gtm stm_buf = *stm;
323 struct acpi_object_list input;
324 union acpi_object in_params[3];
326 in_params[0].type = ACPI_TYPE_BUFFER;
327 in_params[0].buffer.length = sizeof(struct ata_acpi_gtm);
328 in_params[0].buffer.pointer = (u8 *)&stm_buf;
329 /* Buffers for id may need byteswapping ? */
330 in_params[1].type = ACPI_TYPE_BUFFER;
331 in_params[1].buffer.length = 512;
332 in_params[1].buffer.pointer = (u8 *)ap->link.device[0].id;
333 in_params[2].type = ACPI_TYPE_BUFFER;
334 in_params[2].buffer.length = 512;
335 in_params[2].buffer.pointer = (u8 *)ap->link.device[1].id;
338 input.pointer = in_params;
340 status = acpi_evaluate_object(ata_ap_acpi_handle(ap), "_STM", &input,
343 if (status == AE_NOT_FOUND)
345 if (ACPI_FAILURE(status)) {
346 ata_port_err(ap, "ACPI set timing mode failed (status=0x%x)\n",
353 EXPORT_SYMBOL_GPL(ata_acpi_stm);
356 * ata_dev_get_GTF - get the drive bootup default taskfile settings
357 * @dev: target ATA device
358 * @gtf: output parameter for buffer containing _GTF taskfile arrays
360 * This applies to both PATA and SATA drives.
362 * The _GTF method has no input parameters.
363 * It returns a variable number of register set values (registers
364 * hex 1F1..1F7, taskfiles).
365 * The <variable number> is not known in advance, so have ACPI-CA
366 * allocate the buffer as needed and return it, then free it later.
372 * Number of taskfiles on success, 0 if _GTF doesn't exist. -EINVAL
373 * if _GTF is invalid.
375 static int ata_dev_get_GTF(struct ata_device *dev, struct ata_acpi_gtf **gtf)
377 struct ata_port *ap = dev->link->ap;
379 struct acpi_buffer output;
380 union acpi_object *out_obj;
383 /* if _GTF is cached, use the cached value */
384 if (dev->gtf_cache) {
385 out_obj = dev->gtf_cache;
389 /* set up output buffer */
390 output.length = ACPI_ALLOCATE_BUFFER;
391 output.pointer = NULL; /* ACPI-CA sets this; save/free it later */
393 if (ata_msg_probe(ap))
394 ata_dev_dbg(dev, "%s: ENTER: port#: %d\n",
395 __func__, ap->port_no);
397 /* _GTF has no input parameters */
398 status = acpi_evaluate_object(ata_dev_acpi_handle(dev), "_GTF", NULL,
400 out_obj = dev->gtf_cache = output.pointer;
402 if (ACPI_FAILURE(status)) {
403 if (status != AE_NOT_FOUND) {
404 ata_dev_warn(dev, "_GTF evaluation failed (AE 0x%x)\n",
411 if (!output.length || !output.pointer) {
412 if (ata_msg_probe(ap))
413 ata_dev_dbg(dev, "%s: Run _GTF: length or ptr is NULL (0x%llx, 0x%p)\n",
415 (unsigned long long)output.length,
421 if (out_obj->type != ACPI_TYPE_BUFFER) {
422 ata_dev_warn(dev, "_GTF unexpected object type 0x%x\n",
428 if (out_obj->buffer.length % REGS_PER_GTF) {
429 ata_dev_warn(dev, "unexpected _GTF length (%d)\n",
430 out_obj->buffer.length);
436 rc = out_obj->buffer.length / REGS_PER_GTF;
438 *gtf = (void *)out_obj->buffer.pointer;
439 if (ata_msg_probe(ap))
440 ata_dev_dbg(dev, "%s: returning gtf=%p, gtf_count=%d\n",
446 ata_acpi_clear_gtf(dev);
451 * ata_acpi_gtm_xfermode - determine xfermode from GTM parameter
452 * @dev: target device
453 * @gtm: GTM parameter to use
455 * Determine xfermask for @dev from @gtm.
461 * Determined xfermask.
463 unsigned long ata_acpi_gtm_xfermask(struct ata_device *dev,
464 const struct ata_acpi_gtm *gtm)
466 unsigned long xfer_mask = 0;
471 /* we always use the 0 slot for crap hardware */
473 if (!(gtm->flags & 0x10))
477 mode = ata_timing_cycle2mode(ATA_SHIFT_PIO, gtm->drive[unit].pio);
478 xfer_mask |= ata_xfer_mode2mask(mode);
480 /* See if we have MWDMA or UDMA data. We don't bother with
481 * MWDMA if UDMA is available as this means the BIOS set UDMA
482 * and our error changedown if it works is UDMA to PIO anyway.
484 if (!(gtm->flags & (1 << (2 * unit))))
485 type = ATA_SHIFT_MWDMA;
487 type = ATA_SHIFT_UDMA;
489 mode = ata_timing_cycle2mode(type, gtm->drive[unit].dma);
490 xfer_mask |= ata_xfer_mode2mask(mode);
494 EXPORT_SYMBOL_GPL(ata_acpi_gtm_xfermask);
497 * ata_acpi_cbl_80wire - Check for 80 wire cable
499 * @gtm: GTM data to use
501 * Return 1 if the @gtm indicates the BIOS selected an 80wire mode.
503 int ata_acpi_cbl_80wire(struct ata_port *ap, const struct ata_acpi_gtm *gtm)
505 struct ata_device *dev;
507 ata_for_each_dev(dev, &ap->link, ENABLED) {
508 unsigned long xfer_mask, udma_mask;
510 xfer_mask = ata_acpi_gtm_xfermask(dev, gtm);
511 ata_unpack_xfermask(xfer_mask, NULL, NULL, &udma_mask);
513 if (udma_mask & ~ATA_UDMA_MASK_40C)
519 EXPORT_SYMBOL_GPL(ata_acpi_cbl_80wire);
521 static void ata_acpi_gtf_to_tf(struct ata_device *dev,
522 const struct ata_acpi_gtf *gtf,
523 struct ata_taskfile *tf)
525 ata_tf_init(dev, tf);
527 tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
528 tf->protocol = ATA_PROT_NODATA;
529 tf->feature = gtf->tf[0]; /* 0x1f1 */
530 tf->nsect = gtf->tf[1]; /* 0x1f2 */
531 tf->lbal = gtf->tf[2]; /* 0x1f3 */
532 tf->lbam = gtf->tf[3]; /* 0x1f4 */
533 tf->lbah = gtf->tf[4]; /* 0x1f5 */
534 tf->device = gtf->tf[5]; /* 0x1f6 */
535 tf->command = gtf->tf[6]; /* 0x1f7 */
538 static int ata_acpi_filter_tf(struct ata_device *dev,
539 const struct ata_taskfile *tf,
540 const struct ata_taskfile *ptf)
542 if (dev->gtf_filter & ATA_ACPI_FILTER_SETXFER) {
543 /* libata doesn't use ACPI to configure transfer mode.
544 * It will only confuse device configuration. Skip.
546 if (tf->command == ATA_CMD_SET_FEATURES &&
547 tf->feature == SETFEATURES_XFER)
551 if (dev->gtf_filter & ATA_ACPI_FILTER_LOCK) {
552 /* BIOS writers, sorry but we don't wanna lock
553 * features unless the user explicitly said so.
556 /* DEVICE CONFIGURATION FREEZE LOCK */
557 if (tf->command == ATA_CMD_CONF_OVERLAY &&
558 tf->feature == ATA_DCO_FREEZE_LOCK)
561 /* SECURITY FREEZE LOCK */
562 if (tf->command == ATA_CMD_SEC_FREEZE_LOCK)
565 /* SET MAX LOCK and SET MAX FREEZE LOCK */
566 if ((!ptf || ptf->command != ATA_CMD_READ_NATIVE_MAX) &&
567 tf->command == ATA_CMD_SET_MAX &&
568 (tf->feature == ATA_SET_MAX_LOCK ||
569 tf->feature == ATA_SET_MAX_FREEZE_LOCK))
573 if (tf->command == ATA_CMD_SET_FEATURES &&
574 tf->feature == SETFEATURES_SATA_ENABLE) {
575 /* inhibit enabling DIPM */
576 if (dev->gtf_filter & ATA_ACPI_FILTER_DIPM &&
577 tf->nsect == SATA_DIPM)
580 /* inhibit FPDMA non-zero offset */
581 if (dev->gtf_filter & ATA_ACPI_FILTER_FPDMA_OFFSET &&
582 (tf->nsect == SATA_FPDMA_OFFSET ||
583 tf->nsect == SATA_FPDMA_IN_ORDER))
586 /* inhibit FPDMA auto activation */
587 if (dev->gtf_filter & ATA_ACPI_FILTER_FPDMA_AA &&
588 tf->nsect == SATA_FPDMA_AA)
596 * ata_acpi_run_tf - send taskfile registers to host controller
597 * @dev: target ATA device
598 * @gtf: raw ATA taskfile register set (0x1f1 - 0x1f7)
600 * Outputs ATA taskfile to standard ATA host controller.
601 * Writes the control, feature, nsect, lbal, lbam, and lbah registers.
602 * Optionally (ATA_TFLAG_LBA48) writes hob_feature, hob_nsect,
603 * hob_lbal, hob_lbam, and hob_lbah.
605 * This function waits for idle (!BUSY and !DRQ) after writing
606 * registers. If the control register has a new value, this
607 * function also waits for idle after writing control and before
608 * writing the remaining registers.
614 * 1 if command is executed successfully. 0 if ignored, rejected or
615 * filtered out, -errno on other errors.
617 static int ata_acpi_run_tf(struct ata_device *dev,
618 const struct ata_acpi_gtf *gtf,
619 const struct ata_acpi_gtf *prev_gtf)
621 struct ata_taskfile *pptf = NULL;
622 struct ata_taskfile tf, ptf, rtf;
623 unsigned int err_mask;
629 if ((gtf->tf[0] == 0) && (gtf->tf[1] == 0) && (gtf->tf[2] == 0)
630 && (gtf->tf[3] == 0) && (gtf->tf[4] == 0) && (gtf->tf[5] == 0)
631 && (gtf->tf[6] == 0))
634 ata_acpi_gtf_to_tf(dev, gtf, &tf);
636 ata_acpi_gtf_to_tf(dev, prev_gtf, &ptf);
640 if (!ata_acpi_filter_tf(dev, &tf, pptf)) {
642 err_mask = ata_exec_internal(dev, &rtf, NULL,
643 DMA_NONE, NULL, 0, 0);
648 snprintf(msg, sizeof(msg), "succeeded");
654 snprintf(msg, sizeof(msg),
655 "rejected by device (Stat=0x%02x Err=0x%02x)",
656 rtf.command, rtf.feature);
662 snprintf(msg, sizeof(msg),
663 "failed (Emask=0x%x Stat=0x%02x Err=0x%02x)",
664 err_mask, rtf.command, rtf.feature);
670 snprintf(msg, sizeof(msg), "filtered out");
673 descr = ata_get_cmd_descript(tf.command);
675 ata_dev_printk(dev, level,
676 "ACPI cmd %02x/%02x:%02x:%02x:%02x:%02x:%02x (%s) %s\n",
677 tf.command, tf.feature, tf.nsect, tf.lbal,
678 tf.lbam, tf.lbah, tf.device,
679 (descr ? descr : "unknown"), msg);
685 * ata_acpi_exec_tfs - get then write drive taskfile settings
686 * @dev: target ATA device
687 * @nr_executed: out parameter for the number of executed commands
689 * Evaluate _GTF and execute returned taskfiles.
695 * Number of executed taskfiles on success, 0 if _GTF doesn't exist.
696 * -errno on other errors.
698 static int ata_acpi_exec_tfs(struct ata_device *dev, int *nr_executed)
700 struct ata_acpi_gtf *gtf = NULL, *pgtf = NULL;
701 int gtf_count, i, rc;
704 rc = ata_dev_get_GTF(dev, >f);
710 for (i = 0; i < gtf_count; i++, gtf++) {
711 rc = ata_acpi_run_tf(dev, gtf, pgtf);
720 ata_acpi_clear_gtf(dev);
728 * ata_acpi_push_id - send Identify data to drive
729 * @dev: target ATA device
731 * _SDD ACPI object: for SATA mode only
732 * Must be after Identify (Packet) Device -- uses its data
733 * ATM this function never returns a failure. It is an optional
734 * method and if it fails for whatever reason, we should still
741 * 0 on success, -ENOENT if _SDD doesn't exist, -errno on failure.
743 static int ata_acpi_push_id(struct ata_device *dev)
745 struct ata_port *ap = dev->link->ap;
747 struct acpi_object_list input;
748 union acpi_object in_params[1];
750 if (ata_msg_probe(ap))
751 ata_dev_dbg(dev, "%s: ix = %d, port#: %d\n",
752 __func__, dev->devno, ap->port_no);
754 /* Give the drive Identify data to the drive via the _SDD method */
755 /* _SDD: set up input parameters */
757 input.pointer = in_params;
758 in_params[0].type = ACPI_TYPE_BUFFER;
759 in_params[0].buffer.length = sizeof(dev->id[0]) * ATA_ID_WORDS;
760 in_params[0].buffer.pointer = (u8 *)dev->id;
761 /* Output buffer: _SDD has no output */
763 /* It's OK for _SDD to be missing too. */
764 swap_buf_le16(dev->id, ATA_ID_WORDS);
765 status = acpi_evaluate_object(ata_dev_acpi_handle(dev), "_SDD", &input,
767 swap_buf_le16(dev->id, ATA_ID_WORDS);
769 if (status == AE_NOT_FOUND)
772 if (ACPI_FAILURE(status)) {
773 ata_dev_warn(dev, "ACPI _SDD failed (AE 0x%x)\n", status);
781 * ata_acpi_on_suspend - ATA ACPI hook called on suspend
782 * @ap: target ATA port
784 * This function is called when @ap is about to be suspended. All
785 * devices are already put to sleep but the port_suspend() callback
786 * hasn't been executed yet. Error return from this function aborts
793 * 0 on success, -errno on failure.
795 int ata_acpi_on_suspend(struct ata_port *ap)
802 * ata_acpi_on_resume - ATA ACPI hook called on resume
803 * @ap: target ATA port
805 * This function is called when @ap is resumed - right after port
806 * itself is resumed but before any EH action is taken.
811 void ata_acpi_on_resume(struct ata_port *ap)
813 const struct ata_acpi_gtm *gtm = ata_acpi_init_gtm(ap);
814 struct ata_device *dev;
816 if (ata_ap_acpi_handle(ap) && gtm) {
819 /* restore timing parameters */
820 ata_acpi_stm(ap, gtm);
822 /* _GTF should immediately follow _STM so that it can
823 * use values set by _STM. Cache _GTF result and
826 ata_for_each_dev(dev, &ap->link, ALL) {
827 ata_acpi_clear_gtf(dev);
828 if (ata_dev_enabled(dev) &&
829 ata_dev_get_GTF(dev, NULL) >= 0)
830 dev->flags |= ATA_DFLAG_ACPI_PENDING;
833 /* SATA _GTF needs to be evaulated after _SDD and
834 * there's no reason to evaluate IDE _GTF early
835 * without _STM. Clear cache and schedule _GTF.
837 ata_for_each_dev(dev, &ap->link, ALL) {
838 ata_acpi_clear_gtf(dev);
839 if (ata_dev_enabled(dev))
840 dev->flags |= ATA_DFLAG_ACPI_PENDING;
846 * ata_acpi_set_state - set the port power state
847 * @ap: target ATA port
848 * @state: state, on/off
850 * This function executes the _PS0/_PS3 ACPI method to set the power state.
851 * ACPI spec requires _PS0 when IDE power on and _PS3 when power off
853 void ata_acpi_set_state(struct ata_port *ap, pm_message_t state)
855 struct ata_device *dev;
859 /* channel first and then drives for power on and vica versa
861 handle = ata_ap_acpi_handle(ap);
862 if (handle && state.event == PM_EVENT_ON)
863 acpi_bus_set_power(handle, ACPI_STATE_D0);
865 ata_for_each_dev(dev, &ap->link, ENABLED) {
866 handle = ata_dev_acpi_handle(dev);
870 if (state.event != PM_EVENT_ON) {
871 acpi_state = acpi_pm_device_sleep_state(
872 &dev->sdev->sdev_gendev, NULL, ACPI_STATE_D3);
874 acpi_bus_set_power(handle, acpi_state);
875 /* TBD: need to check if it's runtime pm request */
876 acpi_pm_device_run_wake(
877 &dev->sdev->sdev_gendev, true);
880 acpi_pm_device_run_wake(
881 &dev->sdev->sdev_gendev, false);
882 acpi_bus_set_power(handle, ACPI_STATE_D0);
886 handle = ata_ap_acpi_handle(ap);
887 if (handle && state.event != PM_EVENT_ON)
888 acpi_bus_set_power(handle, ACPI_STATE_D3);
892 * ata_acpi_on_devcfg - ATA ACPI hook called on device donfiguration
893 * @dev: target ATA device
895 * This function is called when @dev is about to be configured.
896 * IDENTIFY data might have been modified after this hook is run.
902 * Positive number if IDENTIFY data needs to be refreshed, 0 if not,
905 int ata_acpi_on_devcfg(struct ata_device *dev)
907 struct ata_port *ap = dev->link->ap;
908 struct ata_eh_context *ehc = &ap->link.eh_context;
909 int acpi_sata = ap->flags & ATA_FLAG_ACPI_SATA;
913 if (!ata_dev_acpi_handle(dev))
916 /* do we need to do _GTF? */
917 if (!(dev->flags & ATA_DFLAG_ACPI_PENDING) &&
918 !(acpi_sata && (ehc->i.flags & ATA_EHI_DID_HARDRESET)))
921 /* do _SDD if SATA */
923 rc = ata_acpi_push_id(dev);
924 if (rc && rc != -ENOENT)
929 rc = ata_acpi_exec_tfs(dev, &nr_executed);
933 dev->flags &= ~ATA_DFLAG_ACPI_PENDING;
935 /* refresh IDENTIFY page if any _GTF command has been executed */
937 rc = ata_dev_reread_id(dev, 0);
940 "failed to IDENTIFY after ACPI commands\n");
948 /* ignore evaluation failure if we can continue safely */
949 if (rc == -EINVAL && !nr_executed && !(ap->pflags & ATA_PFLAG_FROZEN))
952 /* fail and let EH retry once more for unknown IO errors */
953 if (!(dev->flags & ATA_DFLAG_ACPI_FAILED)) {
954 dev->flags |= ATA_DFLAG_ACPI_FAILED;
958 ata_dev_warn(dev, "ACPI: failed the second time, disabled\n");
960 /* We can safely continue if no _GTF command has been executed
961 * and port is not frozen.
963 if (!nr_executed && !(ap->pflags & ATA_PFLAG_FROZEN))
970 * ata_acpi_on_disable - ATA ACPI hook called when a device is disabled
971 * @dev: target ATA device
973 * This function is called when @dev is about to be disabled.
978 void ata_acpi_on_disable(struct ata_device *dev)
980 ata_acpi_clear_gtf(dev);
983 static void ata_acpi_wake_dev(acpi_handle handle, u32 event, void *context)
985 struct ata_device *ata_dev = context;
987 if (event == ACPI_NOTIFY_DEVICE_WAKE && ata_dev &&
988 pm_runtime_suspended(&ata_dev->sdev->sdev_gendev))
989 scsi_autopm_get_device(ata_dev->sdev);
992 static void ata_acpi_add_pm_notifier(struct ata_device *dev)
994 struct acpi_device *acpi_dev;
998 handle = ata_dev_acpi_handle(dev);
1002 status = acpi_bus_get_device(handle, &acpi_dev);
1003 if (ACPI_FAILURE(status))
1006 if (dev->sdev->can_power_off) {
1007 acpi_install_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
1008 ata_acpi_wake_dev, dev);
1009 device_set_run_wake(&dev->sdev->sdev_gendev, true);
1013 static void ata_acpi_remove_pm_notifier(struct ata_device *dev)
1015 struct acpi_device *acpi_dev;
1019 handle = ata_dev_acpi_handle(dev);
1023 status = acpi_bus_get_device(handle, &acpi_dev);
1024 if (ACPI_FAILURE(status))
1027 if (dev->sdev->can_power_off) {
1028 device_set_run_wake(&dev->sdev->sdev_gendev, false);
1029 acpi_remove_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
1034 static void ata_acpi_register_power_resource(struct ata_device *dev)
1036 struct scsi_device *sdev = dev->sdev;
1038 struct device *device;
1040 handle = ata_dev_acpi_handle(dev);
1044 device = &sdev->sdev_gendev;
1046 acpi_power_resource_register_device(device, handle);
1049 static void ata_acpi_unregister_power_resource(struct ata_device *dev)
1051 struct scsi_device *sdev = dev->sdev;
1053 struct device *device;
1055 handle = ata_dev_acpi_handle(dev);
1059 device = &sdev->sdev_gendev;
1061 acpi_power_resource_unregister_device(device, handle);
1064 void ata_acpi_bind(struct ata_device *dev)
1066 ata_acpi_add_pm_notifier(dev);
1067 ata_acpi_register_power_resource(dev);
1070 void ata_acpi_unbind(struct ata_device *dev)
1072 ata_acpi_remove_pm_notifier(dev);
1073 ata_acpi_unregister_power_resource(dev);
1076 static int compat_pci_ata(struct ata_port *ap)
1078 struct device *dev = ap->tdev.parent;
1079 struct pci_dev *pdev;
1081 if (!is_pci_dev(dev))
1084 pdev = to_pci_dev(dev);
1086 if ((pdev->class >> 8) != PCI_CLASS_STORAGE_SATA &&
1087 (pdev->class >> 8) != PCI_CLASS_STORAGE_IDE)
1093 static int ata_acpi_bind_host(struct ata_port *ap, acpi_handle *handle)
1095 if (ap->flags & ATA_FLAG_ACPI_SATA)
1098 *handle = acpi_get_child(DEVICE_ACPI_HANDLE(ap->tdev.parent),
1107 static int ata_acpi_bind_device(struct ata_port *ap, struct scsi_device *sdev,
1108 acpi_handle *handle)
1110 struct ata_device *ata_dev;
1112 struct acpi_device *acpi_dev;
1113 struct acpi_device_power_state *states;
1115 if (ap->flags & ATA_FLAG_ACPI_SATA)
1116 ata_dev = &ap->link.device[sdev->channel];
1118 ata_dev = &ap->link.device[sdev->id];
1120 *handle = ata_dev_acpi_handle(ata_dev);
1125 status = acpi_bus_get_device(*handle, &acpi_dev);
1126 if (ACPI_FAILURE(status))
1130 * If firmware has _PS3 or _PR3 for this device,
1131 * and this ata ODD device support device attention,
1132 * it means this device can be powered off
1134 states = acpi_dev->power.states;
1135 if ((states[ACPI_STATE_D3_HOT].flags.valid ||
1136 states[ACPI_STATE_D3_COLD].flags.explicit_set) &&
1137 ata_dev->flags & ATA_DFLAG_DA)
1138 sdev->can_power_off = 1;
1143 static int is_ata_port(const struct device *dev)
1145 return dev->type == &ata_port_type;
1148 static struct ata_port *dev_to_ata_port(struct device *dev)
1150 while (!is_ata_port(dev)) {
1155 return to_ata_port(dev);
1158 static int ata_acpi_find_device(struct device *dev, acpi_handle *handle)
1160 struct ata_port *ap = dev_to_ata_port(dev);
1165 if (!compat_pci_ata(ap))
1168 if (scsi_is_host_device(dev))
1169 return ata_acpi_bind_host(ap, handle);
1170 else if (scsi_is_sdev_device(dev)) {
1171 struct scsi_device *sdev = to_scsi_device(dev);
1173 return ata_acpi_bind_device(ap, sdev, handle);
1178 static int ata_acpi_find_dummy(struct device *dev, acpi_handle *handle)
1183 static struct acpi_bus_type ata_acpi_bus = {
1184 .find_bridge = ata_acpi_find_dummy,
1185 .find_device = ata_acpi_find_device,
1188 int ata_acpi_register(void)
1190 return scsi_register_acpi_bus_type(&ata_acpi_bus);
1193 void ata_acpi_unregister(void)
1195 scsi_unregister_acpi_bus_type(&ata_acpi_bus);