Linux 3.11-rc3
[cris-mirror.git] / drivers / ata / libata-acpi.c
blobcf4e7020adacde5e69881a21adb0c578d31d7a3e
1 /*
2 * libata-acpi.c
3 * Provides ACPI support for PATA/SATA.
5 * Copyright (C) 2006 Intel Corp.
6 * Copyright (C) 2006 Randy Dunlap
7 */
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>
21 #include "libata.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
33 struct ata_acpi_gtf {
34 u8 tf[REGS_PER_GTF]; /* regs. 0x1f1 - 0x1f7 */
35 } __packed;
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;
51 /**
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)
61 return NULL;
63 return ap->scsi_host ?
64 DEVICE_ACPI_HANDLE(&ap->scsi_host->shost_gendev) : NULL;
66 EXPORT_SYMBOL(ata_ap_acpi_handle);
68 /**
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)
77 acpi_integer adr;
78 struct ata_port *ap = dev->link->ap;
80 if (libata_noacpi || dev->flags & ATA_DFLAG_ACPI_DISABLED)
81 return NULL;
83 if (ap->flags & ATA_FLAG_ACPI_SATA) {
84 if (!sata_pmp_attached(ap))
85 adr = SATA_ADR(ap->port_no, NO_PORT_MULT);
86 else
87 adr = SATA_ADR(ap->port_no, dev->link->pmp);
88 return acpi_get_child(DEVICE_ACPI_HANDLE(ap->host->dev), adr);
89 } else
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)
97 if (dev)
98 dev->flags |= ATA_DFLAG_DETACH;
99 else {
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
123 * event.
125 * LOCKING:
126 * ACPI notify handler context. May sleep.
128 static void ata_acpi_handle_hotplug(struct ata_port *ap, struct ata_device *dev,
129 u32 event)
131 struct ata_eh_info *ehi = &ap->link.eh_info;
132 int wait = 0;
133 unsigned long flags;
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
141 switch (event) {
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);
147 ata_port_freeze(ap);
148 break;
149 case ACPI_NOTIFY_EJECT_REQUEST:
150 ata_ehi_push_desc(ehi, "ACPI event");
152 ata_acpi_detach_device(ap, dev);
153 wait = 1;
154 break;
157 spin_unlock_irqrestore(ap->lock, flags);
159 if (wait) {
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,
180 u32 event)
182 struct kobject *kobj = NULL;
183 char event_string[20];
184 char *envp[] = { event_string, NULL };
186 if (dev) {
187 if (dev->sdev)
188 kobj = &dev->sdev->sdev_gendev.kobj;
189 } else
190 kobj = &ap->dev->kobj;
192 if (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)
221 int i;
223 for (i = 0; i < host->n_ports; i++) {
224 struct ata_port *ap = host->ports[i];
225 acpi_handle handle;
226 struct ata_device *dev;
228 if (!ap)
229 continue;
231 handle = ata_ap_acpi_handle(ap);
232 if (handle) {
233 /* we might be on a docking station */
234 register_hotplug_dock_device(handle,
235 &ata_acpi_ap_dock_ops, ap,
236 NULL, NULL);
239 ata_for_each_dev(dev, &ap->link, ALL) {
240 handle = ata_dev_acpi_handle(dev);
241 if (!handle)
242 continue;
244 /* we might be on a docking station */
245 register_hotplug_dock_device(handle,
246 &ata_acpi_dev_dock_ops,
247 dev, NULL, NULL);
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
257 * is shut down.
259 * LOCKING:
260 * EH context.
262 void ata_acpi_dissociate(struct ata_host *host)
264 int i;
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;
283 acpi_status status;
284 int rc = 0;
286 status = acpi_evaluate_object(handle, "_GTM", NULL, &output);
288 rc = -ENOENT;
289 if (status == AE_NOT_FOUND)
290 goto out_free;
292 rc = -EINVAL;
293 if (ACPI_FAILURE(status)) {
294 ata_port_err(ap, "ACPI get timing mode failed (AE 0x%x)\n",
295 status);
296 goto out_free;
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",
302 out_obj->type);
304 goto out_free;
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);
310 goto out_free;
313 memcpy(gtm, out_obj->buffer.pointer, sizeof(struct ata_acpi_gtm));
314 rc = 0;
315 out_free:
316 kfree(output.pointer);
317 return rc;
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.
327 * LOCKING:
328 * EH context.
330 * RETURNS:
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);
337 else
338 return -EINVAL;
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.
350 * LOCKING:
351 * EH context.
353 * RETURNS:
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)
358 acpi_status status;
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;
374 input.count = 3;
375 input.pointer = in_params;
377 status = acpi_evaluate_object(ata_ap_acpi_handle(ap), "_STM", &input,
378 NULL);
380 if (status == AE_NOT_FOUND)
381 return -ENOENT;
382 if (ACPI_FAILURE(status)) {
383 ata_port_err(ap, "ACPI set timing mode failed (status=0x%x)\n",
384 status);
385 return -EINVAL;
387 return 0;
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.
405 * LOCKING:
406 * EH context.
408 * RETURNS:
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;
415 acpi_status status;
416 struct acpi_buffer output;
417 union acpi_object *out_obj;
418 int rc = 0;
420 /* if _GTF is cached, use the cached value */
421 if (dev->gtf_cache) {
422 out_obj = dev->gtf_cache;
423 goto done;
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,
436 &output);
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",
442 status);
443 rc = -EINVAL;
445 goto out_free;
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",
451 __func__,
452 (unsigned long long)output.length,
453 output.pointer);
454 rc = -EINVAL;
455 goto out_free;
458 if (out_obj->type != ACPI_TYPE_BUFFER) {
459 ata_dev_warn(dev, "_GTF unexpected object type 0x%x\n",
460 out_obj->type);
461 rc = -EINVAL;
462 goto out_free;
465 if (out_obj->buffer.length % REGS_PER_GTF) {
466 ata_dev_warn(dev, "unexpected _GTF length (%d)\n",
467 out_obj->buffer.length);
468 rc = -EINVAL;
469 goto out_free;
472 done:
473 rc = out_obj->buffer.length / REGS_PER_GTF;
474 if (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",
478 __func__, *gtf, rc);
480 return rc;
482 out_free:
483 ata_acpi_clear_gtf(dev);
484 return rc;
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.
494 * LOCKING:
495 * None.
497 * RETURNS:
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;
504 unsigned int type;
505 int unit;
506 u8 mode;
508 /* we always use the 0 slot for crap hardware */
509 unit = dev->devno;
510 if (!(gtm->flags & 0x10))
511 unit = 0;
513 /* PIO */
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;
523 else
524 type = ATA_SHIFT_UDMA;
526 mode = ata_timing_cycle2mode(type, gtm->drive[unit].dma);
527 xfer_mask |= ata_xfer_mode2mask(mode);
529 return xfer_mask;
531 EXPORT_SYMBOL_GPL(ata_acpi_gtm_xfermask);
534 * ata_acpi_cbl_80wire - Check for 80 wire cable
535 * @ap: Port to check
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)
551 return 1;
554 return 0;
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)
585 return 1;
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)
596 return 1;
598 /* SECURITY FREEZE LOCK */
599 if (tf->command == ATA_CMD_SEC_FREEZE_LOCK)
600 return 1;
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))
607 return 1;
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)
615 return 1;
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))
621 return 1;
623 /* inhibit FPDMA auto activation */
624 if (dev->gtf_filter & ATA_ACPI_FILTER_FPDMA_AA &&
625 tf->nsect == SATA_FPDMA_AA)
626 return 1;
629 return 0;
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.
647 * LOCKING:
648 * EH context.
650 * RETURNS:
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;
661 const char *level;
662 const char *descr;
663 char msg[60];
664 int rc;
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))
669 return 0;
671 ata_acpi_gtf_to_tf(dev, gtf, &tf);
672 if (prev_gtf) {
673 ata_acpi_gtf_to_tf(dev, prev_gtf, &ptf);
674 pptf = &ptf;
677 if (!ata_acpi_filter_tf(dev, &tf, pptf)) {
678 rtf = tf;
679 err_mask = ata_exec_internal(dev, &rtf, NULL,
680 DMA_NONE, NULL, 0, 0);
682 switch (err_mask) {
683 case 0:
684 level = KERN_DEBUG;
685 snprintf(msg, sizeof(msg), "succeeded");
686 rc = 1;
687 break;
689 case AC_ERR_DEV:
690 level = KERN_INFO;
691 snprintf(msg, sizeof(msg),
692 "rejected by device (Stat=0x%02x Err=0x%02x)",
693 rtf.command, rtf.feature);
694 rc = 0;
695 break;
697 default:
698 level = KERN_ERR;
699 snprintf(msg, sizeof(msg),
700 "failed (Emask=0x%x Stat=0x%02x Err=0x%02x)",
701 err_mask, rtf.command, rtf.feature);
702 rc = -EIO;
703 break;
705 } else {
706 level = KERN_INFO;
707 snprintf(msg, sizeof(msg), "filtered out");
708 rc = 0;
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);
718 return rc;
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.
728 * LOCKING:
729 * EH context.
731 * RETURNS:
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;
740 /* get taskfiles */
741 rc = ata_dev_get_GTF(dev, &gtf);
742 if (rc < 0)
743 return rc;
744 gtf_count = rc;
746 /* execute them */
747 for (i = 0; i < gtf_count; i++, gtf++) {
748 rc = ata_acpi_run_tf(dev, gtf, pgtf);
749 if (rc < 0)
750 break;
751 if (rc) {
752 (*nr_executed)++;
753 pgtf = gtf;
757 ata_acpi_clear_gtf(dev);
759 if (rc < 0)
760 return rc;
761 return 0;
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
772 * just keep going.
774 * LOCKING:
775 * EH context.
777 * RETURNS:
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;
783 acpi_status status;
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 */
793 input.count = 1;
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,
803 NULL);
804 swap_buf_le16(dev->id, ATA_ID_WORDS);
806 if (status == AE_NOT_FOUND)
807 return -ENOENT;
809 if (ACPI_FAILURE(status)) {
810 ata_dev_warn(dev, "ACPI _SDD failed (AE 0x%x)\n", status);
811 return -EIO;
814 return 0;
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
824 * suspend.
826 * LOCKING:
827 * EH context.
829 * RETURNS:
830 * 0 on success, -errno on failure.
832 int ata_acpi_on_suspend(struct ata_port *ap)
834 /* nada */
835 return 0;
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.
845 * LOCKING:
846 * EH context.
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) {
854 /* _GTM valid */
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
861 * schedule _GTF.
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;
869 } else {
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;
885 if (!runtime)
886 goto out;
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;
896 out:
897 return acpi_pm_device_sleep_state(&dev->sdev->sdev_gendev,
898 NULL, d_max_in);
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;
905 acpi_handle handle;
906 int acpi_state;
908 ata_for_each_dev(dev, &ap->link, ENABLED) {
909 handle = ata_dev_acpi_handle(dev);
910 if (!handle)
911 continue;
913 if (!(state.event & PM_EVENT_RESUME)) {
914 acpi_state = ata_acpi_choose_suspend_state(dev, runtime);
915 if (acpi_state == ACPI_STATE_D0)
916 continue;
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);
921 } else {
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);
936 if (!port_handle)
937 return;
939 /* channel first and then drives for power on and vica versa
940 for power off */
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);
946 if (!dev_handle)
947 continue;
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);
969 else
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.
980 * LOCKING:
981 * EH context.
983 * RETURNS:
984 * Positive number if IDENTIFY data needs to be refreshed, 0 if not,
985 * -errno on failure.
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;
992 int nr_executed = 0;
993 int rc;
995 if (!ata_dev_acpi_handle(dev))
996 return 0;
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)))
1001 return 0;
1003 /* do _SDD if SATA */
1004 if (acpi_sata) {
1005 rc = ata_acpi_push_id(dev);
1006 if (rc && rc != -ENOENT)
1007 goto acpi_err;
1010 /* do _GTF */
1011 rc = ata_acpi_exec_tfs(dev, &nr_executed);
1012 if (rc)
1013 goto acpi_err;
1015 dev->flags &= ~ATA_DFLAG_ACPI_PENDING;
1017 /* refresh IDENTIFY page if any _GTF command has been executed */
1018 if (nr_executed) {
1019 rc = ata_dev_reread_id(dev, 0);
1020 if (rc < 0) {
1021 ata_dev_err(dev,
1022 "failed to IDENTIFY after ACPI commands\n");
1023 return rc;
1027 return 0;
1029 acpi_err:
1030 /* ignore evaluation failure if we can continue safely */
1031 if (rc == -EINVAL && !nr_executed && !(ap->pflags & ATA_PFLAG_FROZEN))
1032 return 0;
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;
1037 return rc;
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))
1047 return 0;
1049 return rc;
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.
1058 * LOCKING:
1059 * EH context.
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))
1072 return 0;
1074 pdev = to_pci_dev(dev);
1076 if ((pdev->class >> 8) != PCI_CLASS_STORAGE_SATA &&
1077 (pdev->class >> 8) != PCI_CLASS_STORAGE_IDE)
1078 return 0;
1080 return 1;
1083 static int ata_acpi_bind_host(struct ata_port *ap, acpi_handle *handle)
1085 if (libata_noacpi || ap->flags & ATA_FLAG_ACPI_SATA)
1086 return -ENODEV;
1088 *handle = acpi_get_child(DEVICE_ACPI_HANDLE(ap->tdev.parent),
1089 ap->port_no);
1091 if (!*handle)
1092 return -ENODEV;
1094 if (__ata_acpi_gtm(ap, *handle, &ap->__acpi_init_gtm) == 0)
1095 ap->pflags |= ATA_PFLAG_INIT_GTM_VALID;
1097 return 0;
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];
1108 else
1109 ata_dev = &ap->pmp_link[sdev->channel].device[sdev->id];
1111 else {
1112 ata_dev = &ap->link.device[sdev->id];
1115 *handle = ata_dev_acpi_handle(ata_dev);
1117 if (!*handle)
1118 return -ENODEV;
1120 return 0;
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)) {
1131 if (!dev->parent)
1132 return NULL;
1133 dev = dev->parent;
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);
1142 if (!ap)
1143 return -ENODEV;
1145 if (!compat_pci_ata(ap))
1146 return -ENODEV;
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);
1154 } else
1155 return -ENODEV;
1158 static struct acpi_bus_type ata_acpi_bus = {
1159 .name = "ATA",
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);