PM / sleep: Asynchronous threads for suspend_noirq
[linux/fpc-iii.git] / drivers / ata / libata-acpi.c
blob9e69a5308693de59abd5c596244033af43f7b3a8
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 unsigned int ata_acpi_gtf_filter = ATA_ACPI_FILTER_DEFAULT;
24 module_param_named(acpi_gtf_filter, ata_acpi_gtf_filter, int, 0644);
25 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)");
27 #define NO_PORT_MULT 0xffff
28 #define SATA_ADR(root, pmp) (((root) << 16) | (pmp))
30 #define REGS_PER_GTF 7
31 struct ata_acpi_gtf {
32 u8 tf[REGS_PER_GTF]; /* regs. 0x1f1 - 0x1f7 */
33 } __packed;
35 static void ata_acpi_clear_gtf(struct ata_device *dev)
37 kfree(dev->gtf_cache);
38 dev->gtf_cache = NULL;
41 /**
42 * ata_dev_acpi_handle - provide the acpi_handle for an ata_device
43 * @dev: the acpi_handle returned will correspond to this device
45 * Returns the acpi_handle for the ACPI namespace object corresponding to
46 * the ata_device passed into the function, or NULL if no such object exists
47 * or ACPI is disabled for this device due to consecutive errors.
49 acpi_handle ata_dev_acpi_handle(struct ata_device *dev)
51 return dev->flags & ATA_DFLAG_ACPI_DISABLED ?
52 NULL : ACPI_HANDLE(&dev->tdev);
55 /* @ap and @dev are the same as ata_acpi_handle_hotplug() */
56 static void ata_acpi_detach_device(struct ata_port *ap, struct ata_device *dev)
58 if (dev)
59 dev->flags |= ATA_DFLAG_DETACH;
60 else {
61 struct ata_link *tlink;
62 struct ata_device *tdev;
64 ata_for_each_link(tlink, ap, EDGE)
65 ata_for_each_dev(tdev, tlink, ALL)
66 tdev->flags |= ATA_DFLAG_DETACH;
69 ata_port_schedule_eh(ap);
72 /**
73 * ata_acpi_handle_hotplug - ACPI event handler backend
74 * @ap: ATA port ACPI event occurred
75 * @dev: ATA device ACPI event occurred (can be NULL)
76 * @event: ACPI event which occurred
78 * All ACPI bay / device realted events end up in this function. If
79 * the event is port-wide @dev is NULL. If the event is specific to a
80 * device, @dev points to it.
82 * Hotplug (as opposed to unplug) notification is always handled as
83 * port-wide while unplug only kills the target device on device-wide
84 * event.
86 * LOCKING:
87 * ACPI notify handler context. May sleep.
89 static void ata_acpi_handle_hotplug(struct ata_port *ap, struct ata_device *dev,
90 u32 event)
92 struct ata_eh_info *ehi = &ap->link.eh_info;
93 int wait = 0;
94 unsigned long flags;
96 spin_lock_irqsave(ap->lock, flags);
98 * When dock driver calls into the routine, it will always use
99 * ACPI_NOTIFY_BUS_CHECK/ACPI_NOTIFY_DEVICE_CHECK for add and
100 * ACPI_NOTIFY_EJECT_REQUEST for remove
102 switch (event) {
103 case ACPI_NOTIFY_BUS_CHECK:
104 case ACPI_NOTIFY_DEVICE_CHECK:
105 ata_ehi_push_desc(ehi, "ACPI event");
107 ata_ehi_hotplugged(ehi);
108 ata_port_freeze(ap);
109 break;
110 case ACPI_NOTIFY_EJECT_REQUEST:
111 ata_ehi_push_desc(ehi, "ACPI event");
113 ata_acpi_detach_device(ap, dev);
114 wait = 1;
115 break;
118 spin_unlock_irqrestore(ap->lock, flags);
120 if (wait)
121 ata_port_wait_eh(ap);
124 static void ata_acpi_dev_notify_dock(acpi_handle handle, u32 event, void *data)
126 struct ata_device *dev = data;
128 ata_acpi_handle_hotplug(dev->link->ap, dev, event);
131 static void ata_acpi_ap_notify_dock(acpi_handle handle, u32 event, void *data)
133 struct ata_port *ap = data;
135 ata_acpi_handle_hotplug(ap, NULL, event);
138 static void ata_acpi_uevent(struct ata_port *ap, struct ata_device *dev,
139 u32 event)
141 struct kobject *kobj = NULL;
142 char event_string[20];
143 char *envp[] = { event_string, NULL };
145 if (dev) {
146 if (dev->sdev)
147 kobj = &dev->sdev->sdev_gendev.kobj;
148 } else
149 kobj = &ap->dev->kobj;
151 if (kobj) {
152 snprintf(event_string, 20, "BAY_EVENT=%d", event);
153 kobject_uevent_env(kobj, KOBJ_CHANGE, envp);
157 static void ata_acpi_ap_uevent(acpi_handle handle, u32 event, void *data)
159 ata_acpi_uevent(data, NULL, event);
162 static void ata_acpi_dev_uevent(acpi_handle handle, u32 event, void *data)
164 struct ata_device *dev = data;
165 ata_acpi_uevent(dev->link->ap, dev, event);
168 static const struct acpi_dock_ops ata_acpi_dev_dock_ops = {
169 .handler = ata_acpi_dev_notify_dock,
170 .uevent = ata_acpi_dev_uevent,
173 static const struct acpi_dock_ops ata_acpi_ap_dock_ops = {
174 .handler = ata_acpi_ap_notify_dock,
175 .uevent = ata_acpi_ap_uevent,
178 /* bind acpi handle to pata port */
179 void ata_acpi_bind_port(struct ata_port *ap)
181 struct acpi_device *host_companion = ACPI_COMPANION(ap->host->dev);
183 if (libata_noacpi || ap->flags & ATA_FLAG_ACPI_SATA || !host_companion)
184 return;
186 acpi_preset_companion(&ap->tdev, host_companion, ap->port_no);
188 if (ata_acpi_gtm(ap, &ap->__acpi_init_gtm) == 0)
189 ap->pflags |= ATA_PFLAG_INIT_GTM_VALID;
191 /* we might be on a docking station */
192 register_hotplug_dock_device(ACPI_HANDLE(&ap->tdev),
193 &ata_acpi_ap_dock_ops, ap, NULL, NULL);
196 void ata_acpi_bind_dev(struct ata_device *dev)
198 struct ata_port *ap = dev->link->ap;
199 struct acpi_device *port_companion = ACPI_COMPANION(&ap->tdev);
200 struct acpi_device *host_companion = ACPI_COMPANION(ap->host->dev);
201 struct acpi_device *parent;
202 u64 adr;
205 * For both sata/pata devices, host companion device is required.
206 * For pata device, port companion device is also required.
208 if (libata_noacpi || !host_companion ||
209 (!(ap->flags & ATA_FLAG_ACPI_SATA) && !port_companion))
210 return;
212 if (ap->flags & ATA_FLAG_ACPI_SATA) {
213 if (!sata_pmp_attached(ap))
214 adr = SATA_ADR(ap->port_no, NO_PORT_MULT);
215 else
216 adr = SATA_ADR(ap->port_no, dev->link->pmp);
217 parent = host_companion;
218 } else {
219 adr = dev->devno;
220 parent = port_companion;
223 acpi_preset_companion(&dev->tdev, parent, adr);
225 register_hotplug_dock_device(ata_dev_acpi_handle(dev),
226 &ata_acpi_dev_dock_ops, dev, NULL, NULL);
230 * ata_acpi_dissociate - dissociate ATA host from ACPI objects
231 * @host: target ATA host
233 * This function is called during driver detach after the whole host
234 * is shut down.
236 * LOCKING:
237 * EH context.
239 void ata_acpi_dissociate(struct ata_host *host)
241 int i;
243 /* Restore initial _GTM values so that driver which attaches
244 * afterward can use them too.
246 for (i = 0; i < host->n_ports; i++) {
247 struct ata_port *ap = host->ports[i];
248 const struct ata_acpi_gtm *gtm = ata_acpi_init_gtm(ap);
250 if (ACPI_HANDLE(&ap->tdev) && gtm)
251 ata_acpi_stm(ap, gtm);
256 * ata_acpi_gtm - execute _GTM
257 * @ap: target ATA port
258 * @gtm: out parameter for _GTM result
260 * Evaluate _GTM and store the result in @gtm.
262 * LOCKING:
263 * EH context.
265 * RETURNS:
266 * 0 on success, -ENOENT if _GTM doesn't exist, -errno on failure.
268 int ata_acpi_gtm(struct ata_port *ap, struct ata_acpi_gtm *gtm)
270 struct acpi_buffer output = { .length = ACPI_ALLOCATE_BUFFER };
271 union acpi_object *out_obj;
272 acpi_status status;
273 int rc = 0;
274 acpi_handle handle = ACPI_HANDLE(&ap->tdev);
276 if (!handle)
277 return -EINVAL;
279 status = acpi_evaluate_object(handle, "_GTM", NULL, &output);
281 rc = -ENOENT;
282 if (status == AE_NOT_FOUND)
283 goto out_free;
285 rc = -EINVAL;
286 if (ACPI_FAILURE(status)) {
287 ata_port_err(ap, "ACPI get timing mode failed (AE 0x%x)\n",
288 status);
289 goto out_free;
292 out_obj = output.pointer;
293 if (out_obj->type != ACPI_TYPE_BUFFER) {
294 ata_port_warn(ap, "_GTM returned unexpected object type 0x%x\n",
295 out_obj->type);
297 goto out_free;
300 if (out_obj->buffer.length != sizeof(struct ata_acpi_gtm)) {
301 ata_port_err(ap, "_GTM returned invalid length %d\n",
302 out_obj->buffer.length);
303 goto out_free;
306 memcpy(gtm, out_obj->buffer.pointer, sizeof(struct ata_acpi_gtm));
307 rc = 0;
308 out_free:
309 kfree(output.pointer);
310 return rc;
313 EXPORT_SYMBOL_GPL(ata_acpi_gtm);
316 * ata_acpi_stm - execute _STM
317 * @ap: target ATA port
318 * @stm: timing parameter to _STM
320 * Evaluate _STM with timing parameter @stm.
322 * LOCKING:
323 * EH context.
325 * RETURNS:
326 * 0 on success, -ENOENT if _STM doesn't exist, -errno on failure.
328 int ata_acpi_stm(struct ata_port *ap, const struct ata_acpi_gtm *stm)
330 acpi_status status;
331 struct ata_acpi_gtm stm_buf = *stm;
332 struct acpi_object_list input;
333 union acpi_object in_params[3];
335 in_params[0].type = ACPI_TYPE_BUFFER;
336 in_params[0].buffer.length = sizeof(struct ata_acpi_gtm);
337 in_params[0].buffer.pointer = (u8 *)&stm_buf;
338 /* Buffers for id may need byteswapping ? */
339 in_params[1].type = ACPI_TYPE_BUFFER;
340 in_params[1].buffer.length = 512;
341 in_params[1].buffer.pointer = (u8 *)ap->link.device[0].id;
342 in_params[2].type = ACPI_TYPE_BUFFER;
343 in_params[2].buffer.length = 512;
344 in_params[2].buffer.pointer = (u8 *)ap->link.device[1].id;
346 input.count = 3;
347 input.pointer = in_params;
349 status = acpi_evaluate_object(ACPI_HANDLE(&ap->tdev), "_STM",
350 &input, NULL);
352 if (status == AE_NOT_FOUND)
353 return -ENOENT;
354 if (ACPI_FAILURE(status)) {
355 ata_port_err(ap, "ACPI set timing mode failed (status=0x%x)\n",
356 status);
357 return -EINVAL;
359 return 0;
362 EXPORT_SYMBOL_GPL(ata_acpi_stm);
365 * ata_dev_get_GTF - get the drive bootup default taskfile settings
366 * @dev: target ATA device
367 * @gtf: output parameter for buffer containing _GTF taskfile arrays
369 * This applies to both PATA and SATA drives.
371 * The _GTF method has no input parameters.
372 * It returns a variable number of register set values (registers
373 * hex 1F1..1F7, taskfiles).
374 * The <variable number> is not known in advance, so have ACPI-CA
375 * allocate the buffer as needed and return it, then free it later.
377 * LOCKING:
378 * EH context.
380 * RETURNS:
381 * Number of taskfiles on success, 0 if _GTF doesn't exist. -EINVAL
382 * if _GTF is invalid.
384 static int ata_dev_get_GTF(struct ata_device *dev, struct ata_acpi_gtf **gtf)
386 struct ata_port *ap = dev->link->ap;
387 acpi_status status;
388 struct acpi_buffer output;
389 union acpi_object *out_obj;
390 int rc = 0;
392 /* if _GTF is cached, use the cached value */
393 if (dev->gtf_cache) {
394 out_obj = dev->gtf_cache;
395 goto done;
398 /* set up output buffer */
399 output.length = ACPI_ALLOCATE_BUFFER;
400 output.pointer = NULL; /* ACPI-CA sets this; save/free it later */
402 if (ata_msg_probe(ap))
403 ata_dev_dbg(dev, "%s: ENTER: port#: %d\n",
404 __func__, ap->port_no);
406 /* _GTF has no input parameters */
407 status = acpi_evaluate_object(ata_dev_acpi_handle(dev), "_GTF", NULL,
408 &output);
409 out_obj = dev->gtf_cache = output.pointer;
411 if (ACPI_FAILURE(status)) {
412 if (status != AE_NOT_FOUND) {
413 ata_dev_warn(dev, "_GTF evaluation failed (AE 0x%x)\n",
414 status);
415 rc = -EINVAL;
417 goto out_free;
420 if (!output.length || !output.pointer) {
421 if (ata_msg_probe(ap))
422 ata_dev_dbg(dev, "%s: Run _GTF: length or ptr is NULL (0x%llx, 0x%p)\n",
423 __func__,
424 (unsigned long long)output.length,
425 output.pointer);
426 rc = -EINVAL;
427 goto out_free;
430 if (out_obj->type != ACPI_TYPE_BUFFER) {
431 ata_dev_warn(dev, "_GTF unexpected object type 0x%x\n",
432 out_obj->type);
433 rc = -EINVAL;
434 goto out_free;
437 if (out_obj->buffer.length % REGS_PER_GTF) {
438 ata_dev_warn(dev, "unexpected _GTF length (%d)\n",
439 out_obj->buffer.length);
440 rc = -EINVAL;
441 goto out_free;
444 done:
445 rc = out_obj->buffer.length / REGS_PER_GTF;
446 if (gtf) {
447 *gtf = (void *)out_obj->buffer.pointer;
448 if (ata_msg_probe(ap))
449 ata_dev_dbg(dev, "%s: returning gtf=%p, gtf_count=%d\n",
450 __func__, *gtf, rc);
452 return rc;
454 out_free:
455 ata_acpi_clear_gtf(dev);
456 return rc;
460 * ata_acpi_gtm_xfermode - determine xfermode from GTM parameter
461 * @dev: target device
462 * @gtm: GTM parameter to use
464 * Determine xfermask for @dev from @gtm.
466 * LOCKING:
467 * None.
469 * RETURNS:
470 * Determined xfermask.
472 unsigned long ata_acpi_gtm_xfermask(struct ata_device *dev,
473 const struct ata_acpi_gtm *gtm)
475 unsigned long xfer_mask = 0;
476 unsigned int type;
477 int unit;
478 u8 mode;
480 /* we always use the 0 slot for crap hardware */
481 unit = dev->devno;
482 if (!(gtm->flags & 0x10))
483 unit = 0;
485 /* PIO */
486 mode = ata_timing_cycle2mode(ATA_SHIFT_PIO, gtm->drive[unit].pio);
487 xfer_mask |= ata_xfer_mode2mask(mode);
489 /* See if we have MWDMA or UDMA data. We don't bother with
490 * MWDMA if UDMA is available as this means the BIOS set UDMA
491 * and our error changedown if it works is UDMA to PIO anyway.
493 if (!(gtm->flags & (1 << (2 * unit))))
494 type = ATA_SHIFT_MWDMA;
495 else
496 type = ATA_SHIFT_UDMA;
498 mode = ata_timing_cycle2mode(type, gtm->drive[unit].dma);
499 xfer_mask |= ata_xfer_mode2mask(mode);
501 return xfer_mask;
503 EXPORT_SYMBOL_GPL(ata_acpi_gtm_xfermask);
506 * ata_acpi_cbl_80wire - Check for 80 wire cable
507 * @ap: Port to check
508 * @gtm: GTM data to use
510 * Return 1 if the @gtm indicates the BIOS selected an 80wire mode.
512 int ata_acpi_cbl_80wire(struct ata_port *ap, const struct ata_acpi_gtm *gtm)
514 struct ata_device *dev;
516 ata_for_each_dev(dev, &ap->link, ENABLED) {
517 unsigned long xfer_mask, udma_mask;
519 xfer_mask = ata_acpi_gtm_xfermask(dev, gtm);
520 ata_unpack_xfermask(xfer_mask, NULL, NULL, &udma_mask);
522 if (udma_mask & ~ATA_UDMA_MASK_40C)
523 return 1;
526 return 0;
528 EXPORT_SYMBOL_GPL(ata_acpi_cbl_80wire);
530 static void ata_acpi_gtf_to_tf(struct ata_device *dev,
531 const struct ata_acpi_gtf *gtf,
532 struct ata_taskfile *tf)
534 ata_tf_init(dev, tf);
536 tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
537 tf->protocol = ATA_PROT_NODATA;
538 tf->feature = gtf->tf[0]; /* 0x1f1 */
539 tf->nsect = gtf->tf[1]; /* 0x1f2 */
540 tf->lbal = gtf->tf[2]; /* 0x1f3 */
541 tf->lbam = gtf->tf[3]; /* 0x1f4 */
542 tf->lbah = gtf->tf[4]; /* 0x1f5 */
543 tf->device = gtf->tf[5]; /* 0x1f6 */
544 tf->command = gtf->tf[6]; /* 0x1f7 */
547 static int ata_acpi_filter_tf(struct ata_device *dev,
548 const struct ata_taskfile *tf,
549 const struct ata_taskfile *ptf)
551 if (dev->gtf_filter & ATA_ACPI_FILTER_SETXFER) {
552 /* libata doesn't use ACPI to configure transfer mode.
553 * It will only confuse device configuration. Skip.
555 if (tf->command == ATA_CMD_SET_FEATURES &&
556 tf->feature == SETFEATURES_XFER)
557 return 1;
560 if (dev->gtf_filter & ATA_ACPI_FILTER_LOCK) {
561 /* BIOS writers, sorry but we don't wanna lock
562 * features unless the user explicitly said so.
565 /* DEVICE CONFIGURATION FREEZE LOCK */
566 if (tf->command == ATA_CMD_CONF_OVERLAY &&
567 tf->feature == ATA_DCO_FREEZE_LOCK)
568 return 1;
570 /* SECURITY FREEZE LOCK */
571 if (tf->command == ATA_CMD_SEC_FREEZE_LOCK)
572 return 1;
574 /* SET MAX LOCK and SET MAX FREEZE LOCK */
575 if ((!ptf || ptf->command != ATA_CMD_READ_NATIVE_MAX) &&
576 tf->command == ATA_CMD_SET_MAX &&
577 (tf->feature == ATA_SET_MAX_LOCK ||
578 tf->feature == ATA_SET_MAX_FREEZE_LOCK))
579 return 1;
582 if (tf->command == ATA_CMD_SET_FEATURES &&
583 tf->feature == SETFEATURES_SATA_ENABLE) {
584 /* inhibit enabling DIPM */
585 if (dev->gtf_filter & ATA_ACPI_FILTER_DIPM &&
586 tf->nsect == SATA_DIPM)
587 return 1;
589 /* inhibit FPDMA non-zero offset */
590 if (dev->gtf_filter & ATA_ACPI_FILTER_FPDMA_OFFSET &&
591 (tf->nsect == SATA_FPDMA_OFFSET ||
592 tf->nsect == SATA_FPDMA_IN_ORDER))
593 return 1;
595 /* inhibit FPDMA auto activation */
596 if (dev->gtf_filter & ATA_ACPI_FILTER_FPDMA_AA &&
597 tf->nsect == SATA_FPDMA_AA)
598 return 1;
601 return 0;
605 * ata_acpi_run_tf - send taskfile registers to host controller
606 * @dev: target ATA device
607 * @gtf: raw ATA taskfile register set (0x1f1 - 0x1f7)
609 * Outputs ATA taskfile to standard ATA host controller.
610 * Writes the control, feature, nsect, lbal, lbam, and lbah registers.
611 * Optionally (ATA_TFLAG_LBA48) writes hob_feature, hob_nsect,
612 * hob_lbal, hob_lbam, and hob_lbah.
614 * This function waits for idle (!BUSY and !DRQ) after writing
615 * registers. If the control register has a new value, this
616 * function also waits for idle after writing control and before
617 * writing the remaining registers.
619 * LOCKING:
620 * EH context.
622 * RETURNS:
623 * 1 if command is executed successfully. 0 if ignored, rejected or
624 * filtered out, -errno on other errors.
626 static int ata_acpi_run_tf(struct ata_device *dev,
627 const struct ata_acpi_gtf *gtf,
628 const struct ata_acpi_gtf *prev_gtf)
630 struct ata_taskfile *pptf = NULL;
631 struct ata_taskfile tf, ptf, rtf;
632 unsigned int err_mask;
633 const char *level;
634 const char *descr;
635 char msg[60];
636 int rc;
638 if ((gtf->tf[0] == 0) && (gtf->tf[1] == 0) && (gtf->tf[2] == 0)
639 && (gtf->tf[3] == 0) && (gtf->tf[4] == 0) && (gtf->tf[5] == 0)
640 && (gtf->tf[6] == 0))
641 return 0;
643 ata_acpi_gtf_to_tf(dev, gtf, &tf);
644 if (prev_gtf) {
645 ata_acpi_gtf_to_tf(dev, prev_gtf, &ptf);
646 pptf = &ptf;
649 if (!ata_acpi_filter_tf(dev, &tf, pptf)) {
650 rtf = tf;
651 err_mask = ata_exec_internal(dev, &rtf, NULL,
652 DMA_NONE, NULL, 0, 0);
654 switch (err_mask) {
655 case 0:
656 level = KERN_DEBUG;
657 snprintf(msg, sizeof(msg), "succeeded");
658 rc = 1;
659 break;
661 case AC_ERR_DEV:
662 level = KERN_INFO;
663 snprintf(msg, sizeof(msg),
664 "rejected by device (Stat=0x%02x Err=0x%02x)",
665 rtf.command, rtf.feature);
666 rc = 0;
667 break;
669 default:
670 level = KERN_ERR;
671 snprintf(msg, sizeof(msg),
672 "failed (Emask=0x%x Stat=0x%02x Err=0x%02x)",
673 err_mask, rtf.command, rtf.feature);
674 rc = -EIO;
675 break;
677 } else {
678 level = KERN_INFO;
679 snprintf(msg, sizeof(msg), "filtered out");
680 rc = 0;
682 descr = ata_get_cmd_descript(tf.command);
684 ata_dev_printk(dev, level,
685 "ACPI cmd %02x/%02x:%02x:%02x:%02x:%02x:%02x (%s) %s\n",
686 tf.command, tf.feature, tf.nsect, tf.lbal,
687 tf.lbam, tf.lbah, tf.device,
688 (descr ? descr : "unknown"), msg);
690 return rc;
694 * ata_acpi_exec_tfs - get then write drive taskfile settings
695 * @dev: target ATA device
696 * @nr_executed: out parameter for the number of executed commands
698 * Evaluate _GTF and execute returned taskfiles.
700 * LOCKING:
701 * EH context.
703 * RETURNS:
704 * Number of executed taskfiles on success, 0 if _GTF doesn't exist.
705 * -errno on other errors.
707 static int ata_acpi_exec_tfs(struct ata_device *dev, int *nr_executed)
709 struct ata_acpi_gtf *gtf = NULL, *pgtf = NULL;
710 int gtf_count, i, rc;
712 /* get taskfiles */
713 rc = ata_dev_get_GTF(dev, &gtf);
714 if (rc < 0)
715 return rc;
716 gtf_count = rc;
718 /* execute them */
719 for (i = 0; i < gtf_count; i++, gtf++) {
720 rc = ata_acpi_run_tf(dev, gtf, pgtf);
721 if (rc < 0)
722 break;
723 if (rc) {
724 (*nr_executed)++;
725 pgtf = gtf;
729 ata_acpi_clear_gtf(dev);
731 if (rc < 0)
732 return rc;
733 return 0;
737 * ata_acpi_push_id - send Identify data to drive
738 * @dev: target ATA device
740 * _SDD ACPI object: for SATA mode only
741 * Must be after Identify (Packet) Device -- uses its data
742 * ATM this function never returns a failure. It is an optional
743 * method and if it fails for whatever reason, we should still
744 * just keep going.
746 * LOCKING:
747 * EH context.
749 * RETURNS:
750 * 0 on success, -ENOENT if _SDD doesn't exist, -errno on failure.
752 static int ata_acpi_push_id(struct ata_device *dev)
754 struct ata_port *ap = dev->link->ap;
755 acpi_status status;
756 struct acpi_object_list input;
757 union acpi_object in_params[1];
759 if (ata_msg_probe(ap))
760 ata_dev_dbg(dev, "%s: ix = %d, port#: %d\n",
761 __func__, dev->devno, ap->port_no);
763 /* Give the drive Identify data to the drive via the _SDD method */
764 /* _SDD: set up input parameters */
765 input.count = 1;
766 input.pointer = in_params;
767 in_params[0].type = ACPI_TYPE_BUFFER;
768 in_params[0].buffer.length = sizeof(dev->id[0]) * ATA_ID_WORDS;
769 in_params[0].buffer.pointer = (u8 *)dev->id;
770 /* Output buffer: _SDD has no output */
772 /* It's OK for _SDD to be missing too. */
773 swap_buf_le16(dev->id, ATA_ID_WORDS);
774 status = acpi_evaluate_object(ata_dev_acpi_handle(dev), "_SDD", &input,
775 NULL);
776 swap_buf_le16(dev->id, ATA_ID_WORDS);
778 if (status == AE_NOT_FOUND)
779 return -ENOENT;
781 if (ACPI_FAILURE(status)) {
782 ata_dev_warn(dev, "ACPI _SDD failed (AE 0x%x)\n", status);
783 return -EIO;
786 return 0;
790 * ata_acpi_on_suspend - ATA ACPI hook called on suspend
791 * @ap: target ATA port
793 * This function is called when @ap is about to be suspended. All
794 * devices are already put to sleep but the port_suspend() callback
795 * hasn't been executed yet. Error return from this function aborts
796 * suspend.
798 * LOCKING:
799 * EH context.
801 * RETURNS:
802 * 0 on success, -errno on failure.
804 int ata_acpi_on_suspend(struct ata_port *ap)
806 /* nada */
807 return 0;
811 * ata_acpi_on_resume - ATA ACPI hook called on resume
812 * @ap: target ATA port
814 * This function is called when @ap is resumed - right after port
815 * itself is resumed but before any EH action is taken.
817 * LOCKING:
818 * EH context.
820 void ata_acpi_on_resume(struct ata_port *ap)
822 const struct ata_acpi_gtm *gtm = ata_acpi_init_gtm(ap);
823 struct ata_device *dev;
825 if (ACPI_HANDLE(&ap->tdev) && gtm) {
826 /* _GTM valid */
828 /* restore timing parameters */
829 ata_acpi_stm(ap, gtm);
831 /* _GTF should immediately follow _STM so that it can
832 * use values set by _STM. Cache _GTF result and
833 * schedule _GTF.
835 ata_for_each_dev(dev, &ap->link, ALL) {
836 ata_acpi_clear_gtf(dev);
837 if (ata_dev_enabled(dev) &&
838 ata_dev_get_GTF(dev, NULL) >= 0)
839 dev->flags |= ATA_DFLAG_ACPI_PENDING;
841 } else {
842 /* SATA _GTF needs to be evaulated after _SDD and
843 * there's no reason to evaluate IDE _GTF early
844 * without _STM. Clear cache and schedule _GTF.
846 ata_for_each_dev(dev, &ap->link, ALL) {
847 ata_acpi_clear_gtf(dev);
848 if (ata_dev_enabled(dev))
849 dev->flags |= ATA_DFLAG_ACPI_PENDING;
854 static int ata_acpi_choose_suspend_state(struct ata_device *dev, bool runtime)
856 int d_max_in = ACPI_STATE_D3_COLD;
857 if (!runtime)
858 goto out;
861 * For ATAPI, runtime D3 cold is only allowed
862 * for ZPODD in zero power ready state
864 if (dev->class == ATA_DEV_ATAPI &&
865 !(zpodd_dev_enabled(dev) && zpodd_zpready(dev)))
866 d_max_in = ACPI_STATE_D3_HOT;
868 out:
869 return acpi_pm_device_sleep_state(&dev->tdev, NULL, d_max_in);
872 static void sata_acpi_set_state(struct ata_port *ap, pm_message_t state)
874 bool runtime = PMSG_IS_AUTO(state);
875 struct ata_device *dev;
876 acpi_handle handle;
877 int acpi_state;
879 ata_for_each_dev(dev, &ap->link, ENABLED) {
880 handle = ata_dev_acpi_handle(dev);
881 if (!handle)
882 continue;
884 if (!(state.event & PM_EVENT_RESUME)) {
885 acpi_state = ata_acpi_choose_suspend_state(dev, runtime);
886 if (acpi_state == ACPI_STATE_D0)
887 continue;
888 if (runtime && zpodd_dev_enabled(dev) &&
889 acpi_state == ACPI_STATE_D3_COLD)
890 zpodd_enable_run_wake(dev);
891 acpi_bus_set_power(handle, acpi_state);
892 } else {
893 if (runtime && zpodd_dev_enabled(dev))
894 zpodd_disable_run_wake(dev);
895 acpi_bus_set_power(handle, ACPI_STATE_D0);
900 /* ACPI spec requires _PS0 when IDE power on and _PS3 when power off */
901 static void pata_acpi_set_state(struct ata_port *ap, pm_message_t state)
903 struct ata_device *dev;
904 acpi_handle port_handle;
906 port_handle = ACPI_HANDLE(&ap->tdev);
907 if (!port_handle)
908 return;
910 /* channel first and then drives for power on and vica versa
911 for power off */
912 if (state.event & PM_EVENT_RESUME)
913 acpi_bus_set_power(port_handle, ACPI_STATE_D0);
915 ata_for_each_dev(dev, &ap->link, ENABLED) {
916 acpi_handle dev_handle = ata_dev_acpi_handle(dev);
917 if (!dev_handle)
918 continue;
920 acpi_bus_set_power(dev_handle, state.event & PM_EVENT_RESUME ?
921 ACPI_STATE_D0 : ACPI_STATE_D3_COLD);
924 if (!(state.event & PM_EVENT_RESUME))
925 acpi_bus_set_power(port_handle, ACPI_STATE_D3_COLD);
929 * ata_acpi_set_state - set the port power state
930 * @ap: target ATA port
931 * @state: state, on/off
933 * This function sets a proper ACPI D state for the device on
934 * system and runtime PM operations.
936 void ata_acpi_set_state(struct ata_port *ap, pm_message_t state)
938 if (ap->flags & ATA_FLAG_ACPI_SATA)
939 sata_acpi_set_state(ap, state);
940 else
941 pata_acpi_set_state(ap, state);
945 * ata_acpi_on_devcfg - ATA ACPI hook called on device donfiguration
946 * @dev: target ATA device
948 * This function is called when @dev is about to be configured.
949 * IDENTIFY data might have been modified after this hook is run.
951 * LOCKING:
952 * EH context.
954 * RETURNS:
955 * Positive number if IDENTIFY data needs to be refreshed, 0 if not,
956 * -errno on failure.
958 int ata_acpi_on_devcfg(struct ata_device *dev)
960 struct ata_port *ap = dev->link->ap;
961 struct ata_eh_context *ehc = &ap->link.eh_context;
962 int acpi_sata = ap->flags & ATA_FLAG_ACPI_SATA;
963 int nr_executed = 0;
964 int rc;
966 if (!ata_dev_acpi_handle(dev))
967 return 0;
969 /* do we need to do _GTF? */
970 if (!(dev->flags & ATA_DFLAG_ACPI_PENDING) &&
971 !(acpi_sata && (ehc->i.flags & ATA_EHI_DID_HARDRESET)))
972 return 0;
974 /* do _SDD if SATA */
975 if (acpi_sata) {
976 rc = ata_acpi_push_id(dev);
977 if (rc && rc != -ENOENT)
978 goto acpi_err;
981 /* do _GTF */
982 rc = ata_acpi_exec_tfs(dev, &nr_executed);
983 if (rc)
984 goto acpi_err;
986 dev->flags &= ~ATA_DFLAG_ACPI_PENDING;
988 /* refresh IDENTIFY page if any _GTF command has been executed */
989 if (nr_executed) {
990 rc = ata_dev_reread_id(dev, 0);
991 if (rc < 0) {
992 ata_dev_err(dev,
993 "failed to IDENTIFY after ACPI commands\n");
994 return rc;
998 return 0;
1000 acpi_err:
1001 /* ignore evaluation failure if we can continue safely */
1002 if (rc == -EINVAL && !nr_executed && !(ap->pflags & ATA_PFLAG_FROZEN))
1003 return 0;
1005 /* fail and let EH retry once more for unknown IO errors */
1006 if (!(dev->flags & ATA_DFLAG_ACPI_FAILED)) {
1007 dev->flags |= ATA_DFLAG_ACPI_FAILED;
1008 return rc;
1011 dev->flags |= ATA_DFLAG_ACPI_DISABLED;
1012 ata_dev_warn(dev, "ACPI: failed the second time, disabled\n");
1014 /* We can safely continue if no _GTF command has been executed
1015 * and port is not frozen.
1017 if (!nr_executed && !(ap->pflags & ATA_PFLAG_FROZEN))
1018 return 0;
1020 return rc;
1024 * ata_acpi_on_disable - ATA ACPI hook called when a device is disabled
1025 * @dev: target ATA device
1027 * This function is called when @dev is about to be disabled.
1029 * LOCKING:
1030 * EH context.
1032 void ata_acpi_on_disable(struct ata_device *dev)
1034 ata_acpi_clear_gtf(dev);