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);
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);
243 static int __ata_acpi_gtm(struct ata_port *ap, acpi_handle handle,
244 struct ata_acpi_gtm *gtm)
246 struct acpi_buffer output = { .length = ACPI_ALLOCATE_BUFFER };
247 union acpi_object *out_obj;
251 status = acpi_evaluate_object(handle, "_GTM", NULL, &output);
254 if (status == AE_NOT_FOUND)
258 if (ACPI_FAILURE(status)) {
259 ata_port_err(ap, "ACPI get timing mode failed (AE 0x%x)\n",
264 out_obj = output.pointer;
265 if (out_obj->type != ACPI_TYPE_BUFFER) {
266 ata_port_warn(ap, "_GTM returned unexpected object type 0x%x\n",
272 if (out_obj->buffer.length != sizeof(struct ata_acpi_gtm)) {
273 ata_port_err(ap, "_GTM returned invalid length %d\n",
274 out_obj->buffer.length);
278 memcpy(gtm, out_obj->buffer.pointer, sizeof(struct ata_acpi_gtm));
281 kfree(output.pointer);
286 * ata_acpi_gtm - execute _GTM
287 * @ap: target ATA port
288 * @gtm: out parameter for _GTM result
290 * Evaluate _GTM and store the result in @gtm.
296 * 0 on success, -ENOENT if _GTM doesn't exist, -errno on failure.
298 int ata_acpi_gtm(struct ata_port *ap, struct ata_acpi_gtm *gtm)
300 if (ata_ap_acpi_handle(ap))
301 return __ata_acpi_gtm(ap, ata_ap_acpi_handle(ap), gtm);
306 EXPORT_SYMBOL_GPL(ata_acpi_gtm);
309 * ata_acpi_stm - execute _STM
310 * @ap: target ATA port
311 * @stm: timing parameter to _STM
313 * Evaluate _STM with timing parameter @stm.
319 * 0 on success, -ENOENT if _STM doesn't exist, -errno on failure.
321 int ata_acpi_stm(struct ata_port *ap, const struct ata_acpi_gtm *stm)
324 struct ata_acpi_gtm stm_buf = *stm;
325 struct acpi_object_list input;
326 union acpi_object in_params[3];
328 in_params[0].type = ACPI_TYPE_BUFFER;
329 in_params[0].buffer.length = sizeof(struct ata_acpi_gtm);
330 in_params[0].buffer.pointer = (u8 *)&stm_buf;
331 /* Buffers for id may need byteswapping ? */
332 in_params[1].type = ACPI_TYPE_BUFFER;
333 in_params[1].buffer.length = 512;
334 in_params[1].buffer.pointer = (u8 *)ap->link.device[0].id;
335 in_params[2].type = ACPI_TYPE_BUFFER;
336 in_params[2].buffer.length = 512;
337 in_params[2].buffer.pointer = (u8 *)ap->link.device[1].id;
340 input.pointer = in_params;
342 status = acpi_evaluate_object(ata_ap_acpi_handle(ap), "_STM", &input,
345 if (status == AE_NOT_FOUND)
347 if (ACPI_FAILURE(status)) {
348 ata_port_err(ap, "ACPI set timing mode failed (status=0x%x)\n",
355 EXPORT_SYMBOL_GPL(ata_acpi_stm);
358 * ata_dev_get_GTF - get the drive bootup default taskfile settings
359 * @dev: target ATA device
360 * @gtf: output parameter for buffer containing _GTF taskfile arrays
362 * This applies to both PATA and SATA drives.
364 * The _GTF method has no input parameters.
365 * It returns a variable number of register set values (registers
366 * hex 1F1..1F7, taskfiles).
367 * The <variable number> is not known in advance, so have ACPI-CA
368 * allocate the buffer as needed and return it, then free it later.
374 * Number of taskfiles on success, 0 if _GTF doesn't exist. -EINVAL
375 * if _GTF is invalid.
377 static int ata_dev_get_GTF(struct ata_device *dev, struct ata_acpi_gtf **gtf)
379 struct ata_port *ap = dev->link->ap;
381 struct acpi_buffer output;
382 union acpi_object *out_obj;
385 /* if _GTF is cached, use the cached value */
386 if (dev->gtf_cache) {
387 out_obj = dev->gtf_cache;
391 /* set up output buffer */
392 output.length = ACPI_ALLOCATE_BUFFER;
393 output.pointer = NULL; /* ACPI-CA sets this; save/free it later */
395 if (ata_msg_probe(ap))
396 ata_dev_dbg(dev, "%s: ENTER: port#: %d\n",
397 __func__, ap->port_no);
399 /* _GTF has no input parameters */
400 status = acpi_evaluate_object(ata_dev_acpi_handle(dev), "_GTF", NULL,
402 out_obj = dev->gtf_cache = output.pointer;
404 if (ACPI_FAILURE(status)) {
405 if (status != AE_NOT_FOUND) {
406 ata_dev_warn(dev, "_GTF evaluation failed (AE 0x%x)\n",
413 if (!output.length || !output.pointer) {
414 if (ata_msg_probe(ap))
415 ata_dev_dbg(dev, "%s: Run _GTF: length or ptr is NULL (0x%llx, 0x%p)\n",
417 (unsigned long long)output.length,
423 if (out_obj->type != ACPI_TYPE_BUFFER) {
424 ata_dev_warn(dev, "_GTF unexpected object type 0x%x\n",
430 if (out_obj->buffer.length % REGS_PER_GTF) {
431 ata_dev_warn(dev, "unexpected _GTF length (%d)\n",
432 out_obj->buffer.length);
438 rc = out_obj->buffer.length / REGS_PER_GTF;
440 *gtf = (void *)out_obj->buffer.pointer;
441 if (ata_msg_probe(ap))
442 ata_dev_dbg(dev, "%s: returning gtf=%p, gtf_count=%d\n",
448 ata_acpi_clear_gtf(dev);
453 * ata_acpi_gtm_xfermode - determine xfermode from GTM parameter
454 * @dev: target device
455 * @gtm: GTM parameter to use
457 * Determine xfermask for @dev from @gtm.
463 * Determined xfermask.
465 unsigned long ata_acpi_gtm_xfermask(struct ata_device *dev,
466 const struct ata_acpi_gtm *gtm)
468 unsigned long xfer_mask = 0;
473 /* we always use the 0 slot for crap hardware */
475 if (!(gtm->flags & 0x10))
479 mode = ata_timing_cycle2mode(ATA_SHIFT_PIO, gtm->drive[unit].pio);
480 xfer_mask |= ata_xfer_mode2mask(mode);
482 /* See if we have MWDMA or UDMA data. We don't bother with
483 * MWDMA if UDMA is available as this means the BIOS set UDMA
484 * and our error changedown if it works is UDMA to PIO anyway.
486 if (!(gtm->flags & (1 << (2 * unit))))
487 type = ATA_SHIFT_MWDMA;
489 type = ATA_SHIFT_UDMA;
491 mode = ata_timing_cycle2mode(type, gtm->drive[unit].dma);
492 xfer_mask |= ata_xfer_mode2mask(mode);
496 EXPORT_SYMBOL_GPL(ata_acpi_gtm_xfermask);
499 * ata_acpi_cbl_80wire - Check for 80 wire cable
501 * @gtm: GTM data to use
503 * Return 1 if the @gtm indicates the BIOS selected an 80wire mode.
505 int ata_acpi_cbl_80wire(struct ata_port *ap, const struct ata_acpi_gtm *gtm)
507 struct ata_device *dev;
509 ata_for_each_dev(dev, &ap->link, ENABLED) {
510 unsigned long xfer_mask, udma_mask;
512 xfer_mask = ata_acpi_gtm_xfermask(dev, gtm);
513 ata_unpack_xfermask(xfer_mask, NULL, NULL, &udma_mask);
515 if (udma_mask & ~ATA_UDMA_MASK_40C)
521 EXPORT_SYMBOL_GPL(ata_acpi_cbl_80wire);
523 static void ata_acpi_gtf_to_tf(struct ata_device *dev,
524 const struct ata_acpi_gtf *gtf,
525 struct ata_taskfile *tf)
527 ata_tf_init(dev, tf);
529 tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
530 tf->protocol = ATA_PROT_NODATA;
531 tf->feature = gtf->tf[0]; /* 0x1f1 */
532 tf->nsect = gtf->tf[1]; /* 0x1f2 */
533 tf->lbal = gtf->tf[2]; /* 0x1f3 */
534 tf->lbam = gtf->tf[3]; /* 0x1f4 */
535 tf->lbah = gtf->tf[4]; /* 0x1f5 */
536 tf->device = gtf->tf[5]; /* 0x1f6 */
537 tf->command = gtf->tf[6]; /* 0x1f7 */
540 static int ata_acpi_filter_tf(struct ata_device *dev,
541 const struct ata_taskfile *tf,
542 const struct ata_taskfile *ptf)
544 if (dev->gtf_filter & ATA_ACPI_FILTER_SETXFER) {
545 /* libata doesn't use ACPI to configure transfer mode.
546 * It will only confuse device configuration. Skip.
548 if (tf->command == ATA_CMD_SET_FEATURES &&
549 tf->feature == SETFEATURES_XFER)
553 if (dev->gtf_filter & ATA_ACPI_FILTER_LOCK) {
554 /* BIOS writers, sorry but we don't wanna lock
555 * features unless the user explicitly said so.
558 /* DEVICE CONFIGURATION FREEZE LOCK */
559 if (tf->command == ATA_CMD_CONF_OVERLAY &&
560 tf->feature == ATA_DCO_FREEZE_LOCK)
563 /* SECURITY FREEZE LOCK */
564 if (tf->command == ATA_CMD_SEC_FREEZE_LOCK)
567 /* SET MAX LOCK and SET MAX FREEZE LOCK */
568 if ((!ptf || ptf->command != ATA_CMD_READ_NATIVE_MAX) &&
569 tf->command == ATA_CMD_SET_MAX &&
570 (tf->feature == ATA_SET_MAX_LOCK ||
571 tf->feature == ATA_SET_MAX_FREEZE_LOCK))
575 if (tf->command == ATA_CMD_SET_FEATURES &&
576 tf->feature == SETFEATURES_SATA_ENABLE) {
577 /* inhibit enabling DIPM */
578 if (dev->gtf_filter & ATA_ACPI_FILTER_DIPM &&
579 tf->nsect == SATA_DIPM)
582 /* inhibit FPDMA non-zero offset */
583 if (dev->gtf_filter & ATA_ACPI_FILTER_FPDMA_OFFSET &&
584 (tf->nsect == SATA_FPDMA_OFFSET ||
585 tf->nsect == SATA_FPDMA_IN_ORDER))
588 /* inhibit FPDMA auto activation */
589 if (dev->gtf_filter & ATA_ACPI_FILTER_FPDMA_AA &&
590 tf->nsect == SATA_FPDMA_AA)
598 * ata_acpi_run_tf - send taskfile registers to host controller
599 * @dev: target ATA device
600 * @gtf: raw ATA taskfile register set (0x1f1 - 0x1f7)
602 * Outputs ATA taskfile to standard ATA host controller.
603 * Writes the control, feature, nsect, lbal, lbam, and lbah registers.
604 * Optionally (ATA_TFLAG_LBA48) writes hob_feature, hob_nsect,
605 * hob_lbal, hob_lbam, and hob_lbah.
607 * This function waits for idle (!BUSY and !DRQ) after writing
608 * registers. If the control register has a new value, this
609 * function also waits for idle after writing control and before
610 * writing the remaining registers.
616 * 1 if command is executed successfully. 0 if ignored, rejected or
617 * filtered out, -errno on other errors.
619 static int ata_acpi_run_tf(struct ata_device *dev,
620 const struct ata_acpi_gtf *gtf,
621 const struct ata_acpi_gtf *prev_gtf)
623 struct ata_taskfile *pptf = NULL;
624 struct ata_taskfile tf, ptf, rtf;
625 unsigned int err_mask;
631 if ((gtf->tf[0] == 0) && (gtf->tf[1] == 0) && (gtf->tf[2] == 0)
632 && (gtf->tf[3] == 0) && (gtf->tf[4] == 0) && (gtf->tf[5] == 0)
633 && (gtf->tf[6] == 0))
636 ata_acpi_gtf_to_tf(dev, gtf, &tf);
638 ata_acpi_gtf_to_tf(dev, prev_gtf, &ptf);
642 if (!ata_acpi_filter_tf(dev, &tf, pptf)) {
644 err_mask = ata_exec_internal(dev, &rtf, NULL,
645 DMA_NONE, NULL, 0, 0);
650 snprintf(msg, sizeof(msg), "succeeded");
656 snprintf(msg, sizeof(msg),
657 "rejected by device (Stat=0x%02x Err=0x%02x)",
658 rtf.command, rtf.feature);
664 snprintf(msg, sizeof(msg),
665 "failed (Emask=0x%x Stat=0x%02x Err=0x%02x)",
666 err_mask, rtf.command, rtf.feature);
672 snprintf(msg, sizeof(msg), "filtered out");
675 descr = ata_get_cmd_descript(tf.command);
677 ata_dev_printk(dev, level,
678 "ACPI cmd %02x/%02x:%02x:%02x:%02x:%02x:%02x (%s) %s\n",
679 tf.command, tf.feature, tf.nsect, tf.lbal,
680 tf.lbam, tf.lbah, tf.device,
681 (descr ? descr : "unknown"), msg);
687 * ata_acpi_exec_tfs - get then write drive taskfile settings
688 * @dev: target ATA device
689 * @nr_executed: out parameter for the number of executed commands
691 * Evaluate _GTF and execute returned taskfiles.
697 * Number of executed taskfiles on success, 0 if _GTF doesn't exist.
698 * -errno on other errors.
700 static int ata_acpi_exec_tfs(struct ata_device *dev, int *nr_executed)
702 struct ata_acpi_gtf *gtf = NULL, *pgtf = NULL;
703 int gtf_count, i, rc;
706 rc = ata_dev_get_GTF(dev, >f);
712 for (i = 0; i < gtf_count; i++, gtf++) {
713 rc = ata_acpi_run_tf(dev, gtf, pgtf);
722 ata_acpi_clear_gtf(dev);
730 * ata_acpi_push_id - send Identify data to drive
731 * @dev: target ATA device
733 * _SDD ACPI object: for SATA mode only
734 * Must be after Identify (Packet) Device -- uses its data
735 * ATM this function never returns a failure. It is an optional
736 * method and if it fails for whatever reason, we should still
743 * 0 on success, -ENOENT if _SDD doesn't exist, -errno on failure.
745 static int ata_acpi_push_id(struct ata_device *dev)
747 struct ata_port *ap = dev->link->ap;
749 struct acpi_object_list input;
750 union acpi_object in_params[1];
752 if (ata_msg_probe(ap))
753 ata_dev_dbg(dev, "%s: ix = %d, port#: %d\n",
754 __func__, dev->devno, ap->port_no);
756 /* Give the drive Identify data to the drive via the _SDD method */
757 /* _SDD: set up input parameters */
759 input.pointer = in_params;
760 in_params[0].type = ACPI_TYPE_BUFFER;
761 in_params[0].buffer.length = sizeof(dev->id[0]) * ATA_ID_WORDS;
762 in_params[0].buffer.pointer = (u8 *)dev->id;
763 /* Output buffer: _SDD has no output */
765 /* It's OK for _SDD to be missing too. */
766 swap_buf_le16(dev->id, ATA_ID_WORDS);
767 status = acpi_evaluate_object(ata_dev_acpi_handle(dev), "_SDD", &input,
769 swap_buf_le16(dev->id, ATA_ID_WORDS);
771 if (status == AE_NOT_FOUND)
774 if (ACPI_FAILURE(status)) {
775 ata_dev_warn(dev, "ACPI _SDD failed (AE 0x%x)\n", status);
783 * ata_acpi_on_suspend - ATA ACPI hook called on suspend
784 * @ap: target ATA port
786 * This function is called when @ap is about to be suspended. All
787 * devices are already put to sleep but the port_suspend() callback
788 * hasn't been executed yet. Error return from this function aborts
795 * 0 on success, -errno on failure.
797 int ata_acpi_on_suspend(struct ata_port *ap)
804 * ata_acpi_on_resume - ATA ACPI hook called on resume
805 * @ap: target ATA port
807 * This function is called when @ap is resumed - right after port
808 * itself is resumed but before any EH action is taken.
813 void ata_acpi_on_resume(struct ata_port *ap)
815 const struct ata_acpi_gtm *gtm = ata_acpi_init_gtm(ap);
816 struct ata_device *dev;
818 if (ata_ap_acpi_handle(ap) && gtm) {
821 /* restore timing parameters */
822 ata_acpi_stm(ap, gtm);
824 /* _GTF should immediately follow _STM so that it can
825 * use values set by _STM. Cache _GTF result and
828 ata_for_each_dev(dev, &ap->link, ALL) {
829 ata_acpi_clear_gtf(dev);
830 if (ata_dev_enabled(dev) &&
831 ata_dev_get_GTF(dev, NULL) >= 0)
832 dev->flags |= ATA_DFLAG_ACPI_PENDING;
835 /* SATA _GTF needs to be evaulated after _SDD and
836 * there's no reason to evaluate IDE _GTF early
837 * without _STM. Clear cache and schedule _GTF.
839 ata_for_each_dev(dev, &ap->link, ALL) {
840 ata_acpi_clear_gtf(dev);
841 if (ata_dev_enabled(dev))
842 dev->flags |= ATA_DFLAG_ACPI_PENDING;
847 static int ata_acpi_choose_suspend_state(struct ata_device *dev, bool runtime)
849 int d_max_in = ACPI_STATE_D3_COLD;
854 * For ATAPI, runtime D3 cold is only allowed
855 * for ZPODD in zero power ready state
857 if (dev->class == ATA_DEV_ATAPI &&
858 !(zpodd_dev_enabled(dev) && zpodd_zpready(dev)))
859 d_max_in = ACPI_STATE_D3_HOT;
862 return acpi_pm_device_sleep_state(&dev->sdev->sdev_gendev,
866 static void sata_acpi_set_state(struct ata_port *ap, pm_message_t state)
868 bool runtime = PMSG_IS_AUTO(state);
869 struct ata_device *dev;
873 ata_for_each_dev(dev, &ap->link, ENABLED) {
874 handle = ata_dev_acpi_handle(dev);
878 if (!(state.event & PM_EVENT_RESUME)) {
879 acpi_state = ata_acpi_choose_suspend_state(dev, runtime);
880 if (acpi_state == ACPI_STATE_D0)
882 if (runtime && zpodd_dev_enabled(dev) &&
883 acpi_state == ACPI_STATE_D3_COLD)
884 zpodd_enable_run_wake(dev);
885 acpi_bus_set_power(handle, acpi_state);
887 if (runtime && zpodd_dev_enabled(dev))
888 zpodd_disable_run_wake(dev);
889 acpi_bus_set_power(handle, ACPI_STATE_D0);
894 /* ACPI spec requires _PS0 when IDE power on and _PS3 when power off */
895 static void pata_acpi_set_state(struct ata_port *ap, pm_message_t state)
897 struct ata_device *dev;
898 acpi_handle port_handle;
900 port_handle = ata_ap_acpi_handle(ap);
904 /* channel first and then drives for power on and vica versa
906 if (state.event & PM_EVENT_RESUME)
907 acpi_bus_set_power(port_handle, ACPI_STATE_D0);
909 ata_for_each_dev(dev, &ap->link, ENABLED) {
910 acpi_handle dev_handle = ata_dev_acpi_handle(dev);
914 acpi_bus_set_power(dev_handle, state.event & PM_EVENT_RESUME ?
915 ACPI_STATE_D0 : ACPI_STATE_D3);
918 if (!(state.event & PM_EVENT_RESUME))
919 acpi_bus_set_power(port_handle, ACPI_STATE_D3);
923 * ata_acpi_set_state - set the port power state
924 * @ap: target ATA port
925 * @state: state, on/off
927 * This function sets a proper ACPI D state for the device on
928 * system and runtime PM operations.
930 void ata_acpi_set_state(struct ata_port *ap, pm_message_t state)
932 if (ap->flags & ATA_FLAG_ACPI_SATA)
933 sata_acpi_set_state(ap, state);
935 pata_acpi_set_state(ap, state);
939 * ata_acpi_on_devcfg - ATA ACPI hook called on device donfiguration
940 * @dev: target ATA device
942 * This function is called when @dev is about to be configured.
943 * IDENTIFY data might have been modified after this hook is run.
949 * Positive number if IDENTIFY data needs to be refreshed, 0 if not,
952 int ata_acpi_on_devcfg(struct ata_device *dev)
954 struct ata_port *ap = dev->link->ap;
955 struct ata_eh_context *ehc = &ap->link.eh_context;
956 int acpi_sata = ap->flags & ATA_FLAG_ACPI_SATA;
960 if (!ata_dev_acpi_handle(dev))
963 /* do we need to do _GTF? */
964 if (!(dev->flags & ATA_DFLAG_ACPI_PENDING) &&
965 !(acpi_sata && (ehc->i.flags & ATA_EHI_DID_HARDRESET)))
968 /* do _SDD if SATA */
970 rc = ata_acpi_push_id(dev);
971 if (rc && rc != -ENOENT)
976 rc = ata_acpi_exec_tfs(dev, &nr_executed);
980 dev->flags &= ~ATA_DFLAG_ACPI_PENDING;
982 /* refresh IDENTIFY page if any _GTF command has been executed */
984 rc = ata_dev_reread_id(dev, 0);
987 "failed to IDENTIFY after ACPI commands\n");
995 /* ignore evaluation failure if we can continue safely */
996 if (rc == -EINVAL && !nr_executed && !(ap->pflags & ATA_PFLAG_FROZEN))
999 /* fail and let EH retry once more for unknown IO errors */
1000 if (!(dev->flags & ATA_DFLAG_ACPI_FAILED)) {
1001 dev->flags |= ATA_DFLAG_ACPI_FAILED;
1005 dev->flags |= ATA_DFLAG_ACPI_DISABLED;
1006 ata_dev_warn(dev, "ACPI: failed the second time, disabled\n");
1008 /* We can safely continue if no _GTF command has been executed
1009 * and port is not frozen.
1011 if (!nr_executed && !(ap->pflags & ATA_PFLAG_FROZEN))
1018 * ata_acpi_on_disable - ATA ACPI hook called when a device is disabled
1019 * @dev: target ATA device
1021 * This function is called when @dev is about to be disabled.
1026 void ata_acpi_on_disable(struct ata_device *dev)
1028 ata_acpi_clear_gtf(dev);
1031 static int compat_pci_ata(struct ata_port *ap)
1033 struct device *dev = ap->tdev.parent;
1034 struct pci_dev *pdev;
1036 if (!is_pci_dev(dev))
1039 pdev = to_pci_dev(dev);
1041 if ((pdev->class >> 8) != PCI_CLASS_STORAGE_SATA &&
1042 (pdev->class >> 8) != PCI_CLASS_STORAGE_IDE)
1048 static int ata_acpi_bind_host(struct ata_port *ap, acpi_handle *handle)
1050 if (libata_noacpi || ap->flags & ATA_FLAG_ACPI_SATA)
1053 *handle = acpi_get_child(DEVICE_ACPI_HANDLE(ap->tdev.parent),
1059 if (__ata_acpi_gtm(ap, *handle, &ap->__acpi_init_gtm) == 0)
1060 ap->pflags |= ATA_PFLAG_INIT_GTM_VALID;
1065 static int ata_acpi_bind_device(struct ata_port *ap, struct scsi_device *sdev,
1066 acpi_handle *handle)
1068 struct ata_device *ata_dev;
1070 if (ap->flags & ATA_FLAG_ACPI_SATA) {
1071 if (!sata_pmp_attached(ap))
1072 ata_dev = &ap->link.device[sdev->id];
1074 ata_dev = &ap->pmp_link[sdev->channel].device[sdev->id];
1077 ata_dev = &ap->link.device[sdev->id];
1080 *handle = ata_dev_acpi_handle(ata_dev);
1088 static int is_ata_port(const struct device *dev)
1090 return dev->type == &ata_port_type;
1093 static struct ata_port *dev_to_ata_port(struct device *dev)
1095 while (!is_ata_port(dev)) {
1100 return to_ata_port(dev);
1103 static int ata_acpi_find_device(struct device *dev, acpi_handle *handle)
1105 struct ata_port *ap = dev_to_ata_port(dev);
1110 if (!compat_pci_ata(ap))
1113 if (scsi_is_host_device(dev))
1114 return ata_acpi_bind_host(ap, handle);
1115 else if (scsi_is_sdev_device(dev)) {
1116 struct scsi_device *sdev = to_scsi_device(dev);
1118 return ata_acpi_bind_device(ap, sdev, handle);
1123 static struct acpi_bus_type ata_acpi_bus = {
1125 .find_device = ata_acpi_find_device,
1128 int ata_acpi_register(void)
1130 return scsi_register_acpi_bus_type(&ata_acpi_bus);
1133 void ata_acpi_unregister(void)
1135 scsi_unregister_acpi_bus_type(&ata_acpi_bus);