tuntap: switch to use rtnl_dereference()
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / ata / libata-acpi.c
blobef01ac07502e54625cbf02b7ae7ff11322f5c245
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 acpi_get_child(DEVICE_ACPI_HANDLE(ap->host->dev), ap->port_no);
65 EXPORT_SYMBOL(ata_ap_acpi_handle);
67 /**
68 * ata_dev_acpi_handle - provide the acpi_handle for an ata_device
69 * @dev: the acpi_device returned will correspond to this port
71 * Returns the acpi_handle for the ACPI namespace object corresponding to
72 * the ata_device passed into the function, or NULL if no such object exists
74 acpi_handle ata_dev_acpi_handle(struct ata_device *dev)
76 acpi_integer adr;
77 struct ata_port *ap = dev->link->ap;
79 if (dev->flags & ATA_DFLAG_ACPI_DISABLED)
80 return NULL;
82 if (ap->flags & ATA_FLAG_ACPI_SATA) {
83 if (!sata_pmp_attached(ap))
84 adr = SATA_ADR(ap->port_no, NO_PORT_MULT);
85 else
86 adr = SATA_ADR(ap->port_no, dev->link->pmp);
87 return acpi_get_child(DEVICE_ACPI_HANDLE(ap->host->dev), adr);
88 } else
89 return acpi_get_child(ata_ap_acpi_handle(ap), dev->devno);
91 EXPORT_SYMBOL(ata_dev_acpi_handle);
93 /* @ap and @dev are the same as ata_acpi_handle_hotplug() */
94 static void ata_acpi_detach_device(struct ata_port *ap, struct ata_device *dev)
96 if (dev)
97 dev->flags |= ATA_DFLAG_DETACH;
98 else {
99 struct ata_link *tlink;
100 struct ata_device *tdev;
102 ata_for_each_link(tlink, ap, EDGE)
103 ata_for_each_dev(tdev, tlink, ALL)
104 tdev->flags |= ATA_DFLAG_DETACH;
107 ata_port_schedule_eh(ap);
111 * ata_acpi_handle_hotplug - ACPI event handler backend
112 * @ap: ATA port ACPI event occurred
113 * @dev: ATA device ACPI event occurred (can be NULL)
114 * @event: ACPI event which occurred
116 * All ACPI bay / device realted events end up in this function. If
117 * the event is port-wide @dev is NULL. If the event is specific to a
118 * device, @dev points to it.
120 * Hotplug (as opposed to unplug) notification is always handled as
121 * port-wide while unplug only kills the target device on device-wide
122 * event.
124 * LOCKING:
125 * ACPI notify handler context. May sleep.
127 static void ata_acpi_handle_hotplug(struct ata_port *ap, struct ata_device *dev,
128 u32 event)
130 struct ata_eh_info *ehi = &ap->link.eh_info;
131 int wait = 0;
132 unsigned long flags;
134 spin_lock_irqsave(ap->lock, flags);
136 * When dock driver calls into the routine, it will always use
137 * ACPI_NOTIFY_BUS_CHECK/ACPI_NOTIFY_DEVICE_CHECK for add and
138 * ACPI_NOTIFY_EJECT_REQUEST for remove
140 switch (event) {
141 case ACPI_NOTIFY_BUS_CHECK:
142 case ACPI_NOTIFY_DEVICE_CHECK:
143 ata_ehi_push_desc(ehi, "ACPI event");
145 ata_ehi_hotplugged(ehi);
146 ata_port_freeze(ap);
147 break;
148 case ACPI_NOTIFY_EJECT_REQUEST:
149 ata_ehi_push_desc(ehi, "ACPI event");
151 ata_acpi_detach_device(ap, dev);
152 wait = 1;
153 break;
156 spin_unlock_irqrestore(ap->lock, flags);
158 if (wait)
159 ata_port_wait_eh(ap);
162 static void ata_acpi_dev_notify_dock(acpi_handle handle, u32 event, void *data)
164 struct ata_device *dev = data;
166 ata_acpi_handle_hotplug(dev->link->ap, dev, event);
169 static void ata_acpi_ap_notify_dock(acpi_handle handle, u32 event, void *data)
171 struct ata_port *ap = data;
173 ata_acpi_handle_hotplug(ap, NULL, event);
176 static void ata_acpi_uevent(struct ata_port *ap, struct ata_device *dev,
177 u32 event)
179 struct kobject *kobj = NULL;
180 char event_string[20];
181 char *envp[] = { event_string, NULL };
183 if (dev) {
184 if (dev->sdev)
185 kobj = &dev->sdev->sdev_gendev.kobj;
186 } else
187 kobj = &ap->dev->kobj;
189 if (kobj) {
190 snprintf(event_string, 20, "BAY_EVENT=%d", event);
191 kobject_uevent_env(kobj, KOBJ_CHANGE, envp);
195 static void ata_acpi_ap_uevent(acpi_handle handle, u32 event, void *data)
197 ata_acpi_uevent(data, NULL, event);
200 static void ata_acpi_dev_uevent(acpi_handle handle, u32 event, void *data)
202 struct ata_device *dev = data;
203 ata_acpi_uevent(dev->link->ap, dev, event);
206 static const struct acpi_dock_ops ata_acpi_dev_dock_ops = {
207 .handler = ata_acpi_dev_notify_dock,
208 .uevent = ata_acpi_dev_uevent,
211 static const struct acpi_dock_ops ata_acpi_ap_dock_ops = {
212 .handler = ata_acpi_ap_notify_dock,
213 .uevent = ata_acpi_ap_uevent,
217 * ata_acpi_dissociate - dissociate ATA host from ACPI objects
218 * @host: target ATA host
220 * This function is called during driver detach after the whole host
221 * is shut down.
223 * LOCKING:
224 * EH context.
226 void ata_acpi_dissociate(struct ata_host *host)
228 int i;
230 /* Restore initial _GTM values so that driver which attaches
231 * afterward can use them too.
233 for (i = 0; i < host->n_ports; i++) {
234 struct ata_port *ap = host->ports[i];
235 const struct ata_acpi_gtm *gtm = ata_acpi_init_gtm(ap);
237 if (ata_ap_acpi_handle(ap) && gtm)
238 ata_acpi_stm(ap, gtm);
243 * ata_acpi_gtm - execute _GTM
244 * @ap: target ATA port
245 * @gtm: out parameter for _GTM result
247 * Evaluate _GTM and store the result in @gtm.
249 * LOCKING:
250 * EH context.
252 * RETURNS:
253 * 0 on success, -ENOENT if _GTM doesn't exist, -errno on failure.
255 int ata_acpi_gtm(struct ata_port *ap, struct ata_acpi_gtm *gtm)
257 struct acpi_buffer output = { .length = ACPI_ALLOCATE_BUFFER };
258 union acpi_object *out_obj;
259 acpi_status status;
260 int rc = 0;
262 status = acpi_evaluate_object(ata_ap_acpi_handle(ap), "_GTM", NULL,
263 &output);
265 rc = -ENOENT;
266 if (status == AE_NOT_FOUND)
267 goto out_free;
269 rc = -EINVAL;
270 if (ACPI_FAILURE(status)) {
271 ata_port_err(ap, "ACPI get timing mode failed (AE 0x%x)\n",
272 status);
273 goto out_free;
276 out_obj = output.pointer;
277 if (out_obj->type != ACPI_TYPE_BUFFER) {
278 ata_port_warn(ap, "_GTM returned unexpected object type 0x%x\n",
279 out_obj->type);
281 goto out_free;
284 if (out_obj->buffer.length != sizeof(struct ata_acpi_gtm)) {
285 ata_port_err(ap, "_GTM returned invalid length %d\n",
286 out_obj->buffer.length);
287 goto out_free;
290 memcpy(gtm, out_obj->buffer.pointer, sizeof(struct ata_acpi_gtm));
291 rc = 0;
292 out_free:
293 kfree(output.pointer);
294 return rc;
297 EXPORT_SYMBOL_GPL(ata_acpi_gtm);
300 * ata_acpi_stm - execute _STM
301 * @ap: target ATA port
302 * @stm: timing parameter to _STM
304 * Evaluate _STM with timing parameter @stm.
306 * LOCKING:
307 * EH context.
309 * RETURNS:
310 * 0 on success, -ENOENT if _STM doesn't exist, -errno on failure.
312 int ata_acpi_stm(struct ata_port *ap, const struct ata_acpi_gtm *stm)
314 acpi_status status;
315 struct ata_acpi_gtm stm_buf = *stm;
316 struct acpi_object_list input;
317 union acpi_object in_params[3];
319 in_params[0].type = ACPI_TYPE_BUFFER;
320 in_params[0].buffer.length = sizeof(struct ata_acpi_gtm);
321 in_params[0].buffer.pointer = (u8 *)&stm_buf;
322 /* Buffers for id may need byteswapping ? */
323 in_params[1].type = ACPI_TYPE_BUFFER;
324 in_params[1].buffer.length = 512;
325 in_params[1].buffer.pointer = (u8 *)ap->link.device[0].id;
326 in_params[2].type = ACPI_TYPE_BUFFER;
327 in_params[2].buffer.length = 512;
328 in_params[2].buffer.pointer = (u8 *)ap->link.device[1].id;
330 input.count = 3;
331 input.pointer = in_params;
333 status = acpi_evaluate_object(ata_ap_acpi_handle(ap), "_STM", &input,
334 NULL);
336 if (status == AE_NOT_FOUND)
337 return -ENOENT;
338 if (ACPI_FAILURE(status)) {
339 ata_port_err(ap, "ACPI set timing mode failed (status=0x%x)\n",
340 status);
341 return -EINVAL;
343 return 0;
346 EXPORT_SYMBOL_GPL(ata_acpi_stm);
349 * ata_dev_get_GTF - get the drive bootup default taskfile settings
350 * @dev: target ATA device
351 * @gtf: output parameter for buffer containing _GTF taskfile arrays
353 * This applies to both PATA and SATA drives.
355 * The _GTF method has no input parameters.
356 * It returns a variable number of register set values (registers
357 * hex 1F1..1F7, taskfiles).
358 * The <variable number> is not known in advance, so have ACPI-CA
359 * allocate the buffer as needed and return it, then free it later.
361 * LOCKING:
362 * EH context.
364 * RETURNS:
365 * Number of taskfiles on success, 0 if _GTF doesn't exist. -EINVAL
366 * if _GTF is invalid.
368 static int ata_dev_get_GTF(struct ata_device *dev, struct ata_acpi_gtf **gtf)
370 struct ata_port *ap = dev->link->ap;
371 acpi_status status;
372 struct acpi_buffer output;
373 union acpi_object *out_obj;
374 int rc = 0;
376 /* if _GTF is cached, use the cached value */
377 if (dev->gtf_cache) {
378 out_obj = dev->gtf_cache;
379 goto done;
382 /* set up output buffer */
383 output.length = ACPI_ALLOCATE_BUFFER;
384 output.pointer = NULL; /* ACPI-CA sets this; save/free it later */
386 if (ata_msg_probe(ap))
387 ata_dev_dbg(dev, "%s: ENTER: port#: %d\n",
388 __func__, ap->port_no);
390 /* _GTF has no input parameters */
391 status = acpi_evaluate_object(ata_dev_acpi_handle(dev), "_GTF", NULL,
392 &output);
393 out_obj = dev->gtf_cache = output.pointer;
395 if (ACPI_FAILURE(status)) {
396 if (status != AE_NOT_FOUND) {
397 ata_dev_warn(dev, "_GTF evaluation failed (AE 0x%x)\n",
398 status);
399 rc = -EINVAL;
401 goto out_free;
404 if (!output.length || !output.pointer) {
405 if (ata_msg_probe(ap))
406 ata_dev_dbg(dev, "%s: Run _GTF: length or ptr is NULL (0x%llx, 0x%p)\n",
407 __func__,
408 (unsigned long long)output.length,
409 output.pointer);
410 rc = -EINVAL;
411 goto out_free;
414 if (out_obj->type != ACPI_TYPE_BUFFER) {
415 ata_dev_warn(dev, "_GTF unexpected object type 0x%x\n",
416 out_obj->type);
417 rc = -EINVAL;
418 goto out_free;
421 if (out_obj->buffer.length % REGS_PER_GTF) {
422 ata_dev_warn(dev, "unexpected _GTF length (%d)\n",
423 out_obj->buffer.length);
424 rc = -EINVAL;
425 goto out_free;
428 done:
429 rc = out_obj->buffer.length / REGS_PER_GTF;
430 if (gtf) {
431 *gtf = (void *)out_obj->buffer.pointer;
432 if (ata_msg_probe(ap))
433 ata_dev_dbg(dev, "%s: returning gtf=%p, gtf_count=%d\n",
434 __func__, *gtf, rc);
436 return rc;
438 out_free:
439 ata_acpi_clear_gtf(dev);
440 return rc;
444 * ata_acpi_gtm_xfermode - determine xfermode from GTM parameter
445 * @dev: target device
446 * @gtm: GTM parameter to use
448 * Determine xfermask for @dev from @gtm.
450 * LOCKING:
451 * None.
453 * RETURNS:
454 * Determined xfermask.
456 unsigned long ata_acpi_gtm_xfermask(struct ata_device *dev,
457 const struct ata_acpi_gtm *gtm)
459 unsigned long xfer_mask = 0;
460 unsigned int type;
461 int unit;
462 u8 mode;
464 /* we always use the 0 slot for crap hardware */
465 unit = dev->devno;
466 if (!(gtm->flags & 0x10))
467 unit = 0;
469 /* PIO */
470 mode = ata_timing_cycle2mode(ATA_SHIFT_PIO, gtm->drive[unit].pio);
471 xfer_mask |= ata_xfer_mode2mask(mode);
473 /* See if we have MWDMA or UDMA data. We don't bother with
474 * MWDMA if UDMA is available as this means the BIOS set UDMA
475 * and our error changedown if it works is UDMA to PIO anyway.
477 if (!(gtm->flags & (1 << (2 * unit))))
478 type = ATA_SHIFT_MWDMA;
479 else
480 type = ATA_SHIFT_UDMA;
482 mode = ata_timing_cycle2mode(type, gtm->drive[unit].dma);
483 xfer_mask |= ata_xfer_mode2mask(mode);
485 return xfer_mask;
487 EXPORT_SYMBOL_GPL(ata_acpi_gtm_xfermask);
490 * ata_acpi_cbl_80wire - Check for 80 wire cable
491 * @ap: Port to check
492 * @gtm: GTM data to use
494 * Return 1 if the @gtm indicates the BIOS selected an 80wire mode.
496 int ata_acpi_cbl_80wire(struct ata_port *ap, const struct ata_acpi_gtm *gtm)
498 struct ata_device *dev;
500 ata_for_each_dev(dev, &ap->link, ENABLED) {
501 unsigned long xfer_mask, udma_mask;
503 xfer_mask = ata_acpi_gtm_xfermask(dev, gtm);
504 ata_unpack_xfermask(xfer_mask, NULL, NULL, &udma_mask);
506 if (udma_mask & ~ATA_UDMA_MASK_40C)
507 return 1;
510 return 0;
512 EXPORT_SYMBOL_GPL(ata_acpi_cbl_80wire);
514 static void ata_acpi_gtf_to_tf(struct ata_device *dev,
515 const struct ata_acpi_gtf *gtf,
516 struct ata_taskfile *tf)
518 ata_tf_init(dev, tf);
520 tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
521 tf->protocol = ATA_PROT_NODATA;
522 tf->feature = gtf->tf[0]; /* 0x1f1 */
523 tf->nsect = gtf->tf[1]; /* 0x1f2 */
524 tf->lbal = gtf->tf[2]; /* 0x1f3 */
525 tf->lbam = gtf->tf[3]; /* 0x1f4 */
526 tf->lbah = gtf->tf[4]; /* 0x1f5 */
527 tf->device = gtf->tf[5]; /* 0x1f6 */
528 tf->command = gtf->tf[6]; /* 0x1f7 */
531 static int ata_acpi_filter_tf(struct ata_device *dev,
532 const struct ata_taskfile *tf,
533 const struct ata_taskfile *ptf)
535 if (dev->gtf_filter & ATA_ACPI_FILTER_SETXFER) {
536 /* libata doesn't use ACPI to configure transfer mode.
537 * It will only confuse device configuration. Skip.
539 if (tf->command == ATA_CMD_SET_FEATURES &&
540 tf->feature == SETFEATURES_XFER)
541 return 1;
544 if (dev->gtf_filter & ATA_ACPI_FILTER_LOCK) {
545 /* BIOS writers, sorry but we don't wanna lock
546 * features unless the user explicitly said so.
549 /* DEVICE CONFIGURATION FREEZE LOCK */
550 if (tf->command == ATA_CMD_CONF_OVERLAY &&
551 tf->feature == ATA_DCO_FREEZE_LOCK)
552 return 1;
554 /* SECURITY FREEZE LOCK */
555 if (tf->command == ATA_CMD_SEC_FREEZE_LOCK)
556 return 1;
558 /* SET MAX LOCK and SET MAX FREEZE LOCK */
559 if ((!ptf || ptf->command != ATA_CMD_READ_NATIVE_MAX) &&
560 tf->command == ATA_CMD_SET_MAX &&
561 (tf->feature == ATA_SET_MAX_LOCK ||
562 tf->feature == ATA_SET_MAX_FREEZE_LOCK))
563 return 1;
566 if (tf->command == ATA_CMD_SET_FEATURES &&
567 tf->feature == SETFEATURES_SATA_ENABLE) {
568 /* inhibit enabling DIPM */
569 if (dev->gtf_filter & ATA_ACPI_FILTER_DIPM &&
570 tf->nsect == SATA_DIPM)
571 return 1;
573 /* inhibit FPDMA non-zero offset */
574 if (dev->gtf_filter & ATA_ACPI_FILTER_FPDMA_OFFSET &&
575 (tf->nsect == SATA_FPDMA_OFFSET ||
576 tf->nsect == SATA_FPDMA_IN_ORDER))
577 return 1;
579 /* inhibit FPDMA auto activation */
580 if (dev->gtf_filter & ATA_ACPI_FILTER_FPDMA_AA &&
581 tf->nsect == SATA_FPDMA_AA)
582 return 1;
585 return 0;
589 * ata_acpi_run_tf - send taskfile registers to host controller
590 * @dev: target ATA device
591 * @gtf: raw ATA taskfile register set (0x1f1 - 0x1f7)
593 * Outputs ATA taskfile to standard ATA host controller.
594 * Writes the control, feature, nsect, lbal, lbam, and lbah registers.
595 * Optionally (ATA_TFLAG_LBA48) writes hob_feature, hob_nsect,
596 * hob_lbal, hob_lbam, and hob_lbah.
598 * This function waits for idle (!BUSY and !DRQ) after writing
599 * registers. If the control register has a new value, this
600 * function also waits for idle after writing control and before
601 * writing the remaining registers.
603 * LOCKING:
604 * EH context.
606 * RETURNS:
607 * 1 if command is executed successfully. 0 if ignored, rejected or
608 * filtered out, -errno on other errors.
610 static int ata_acpi_run_tf(struct ata_device *dev,
611 const struct ata_acpi_gtf *gtf,
612 const struct ata_acpi_gtf *prev_gtf)
614 struct ata_taskfile *pptf = NULL;
615 struct ata_taskfile tf, ptf, rtf;
616 unsigned int err_mask;
617 const char *level;
618 const char *descr;
619 char msg[60];
620 int rc;
622 if ((gtf->tf[0] == 0) && (gtf->tf[1] == 0) && (gtf->tf[2] == 0)
623 && (gtf->tf[3] == 0) && (gtf->tf[4] == 0) && (gtf->tf[5] == 0)
624 && (gtf->tf[6] == 0))
625 return 0;
627 ata_acpi_gtf_to_tf(dev, gtf, &tf);
628 if (prev_gtf) {
629 ata_acpi_gtf_to_tf(dev, prev_gtf, &ptf);
630 pptf = &ptf;
633 if (!ata_acpi_filter_tf(dev, &tf, pptf)) {
634 rtf = tf;
635 err_mask = ata_exec_internal(dev, &rtf, NULL,
636 DMA_NONE, NULL, 0, 0);
638 switch (err_mask) {
639 case 0:
640 level = KERN_DEBUG;
641 snprintf(msg, sizeof(msg), "succeeded");
642 rc = 1;
643 break;
645 case AC_ERR_DEV:
646 level = KERN_INFO;
647 snprintf(msg, sizeof(msg),
648 "rejected by device (Stat=0x%02x Err=0x%02x)",
649 rtf.command, rtf.feature);
650 rc = 0;
651 break;
653 default:
654 level = KERN_ERR;
655 snprintf(msg, sizeof(msg),
656 "failed (Emask=0x%x Stat=0x%02x Err=0x%02x)",
657 err_mask, rtf.command, rtf.feature);
658 rc = -EIO;
659 break;
661 } else {
662 level = KERN_INFO;
663 snprintf(msg, sizeof(msg), "filtered out");
664 rc = 0;
666 descr = ata_get_cmd_descript(tf.command);
668 ata_dev_printk(dev, level,
669 "ACPI cmd %02x/%02x:%02x:%02x:%02x:%02x:%02x (%s) %s\n",
670 tf.command, tf.feature, tf.nsect, tf.lbal,
671 tf.lbam, tf.lbah, tf.device,
672 (descr ? descr : "unknown"), msg);
674 return rc;
678 * ata_acpi_exec_tfs - get then write drive taskfile settings
679 * @dev: target ATA device
680 * @nr_executed: out parameter for the number of executed commands
682 * Evaluate _GTF and execute returned taskfiles.
684 * LOCKING:
685 * EH context.
687 * RETURNS:
688 * Number of executed taskfiles on success, 0 if _GTF doesn't exist.
689 * -errno on other errors.
691 static int ata_acpi_exec_tfs(struct ata_device *dev, int *nr_executed)
693 struct ata_acpi_gtf *gtf = NULL, *pgtf = NULL;
694 int gtf_count, i, rc;
696 /* get taskfiles */
697 rc = ata_dev_get_GTF(dev, &gtf);
698 if (rc < 0)
699 return rc;
700 gtf_count = rc;
702 /* execute them */
703 for (i = 0; i < gtf_count; i++, gtf++) {
704 rc = ata_acpi_run_tf(dev, gtf, pgtf);
705 if (rc < 0)
706 break;
707 if (rc) {
708 (*nr_executed)++;
709 pgtf = gtf;
713 ata_acpi_clear_gtf(dev);
715 if (rc < 0)
716 return rc;
717 return 0;
721 * ata_acpi_push_id - send Identify data to drive
722 * @dev: target ATA device
724 * _SDD ACPI object: for SATA mode only
725 * Must be after Identify (Packet) Device -- uses its data
726 * ATM this function never returns a failure. It is an optional
727 * method and if it fails for whatever reason, we should still
728 * just keep going.
730 * LOCKING:
731 * EH context.
733 * RETURNS:
734 * 0 on success, -ENOENT if _SDD doesn't exist, -errno on failure.
736 static int ata_acpi_push_id(struct ata_device *dev)
738 struct ata_port *ap = dev->link->ap;
739 acpi_status status;
740 struct acpi_object_list input;
741 union acpi_object in_params[1];
743 if (ata_msg_probe(ap))
744 ata_dev_dbg(dev, "%s: ix = %d, port#: %d\n",
745 __func__, dev->devno, ap->port_no);
747 /* Give the drive Identify data to the drive via the _SDD method */
748 /* _SDD: set up input parameters */
749 input.count = 1;
750 input.pointer = in_params;
751 in_params[0].type = ACPI_TYPE_BUFFER;
752 in_params[0].buffer.length = sizeof(dev->id[0]) * ATA_ID_WORDS;
753 in_params[0].buffer.pointer = (u8 *)dev->id;
754 /* Output buffer: _SDD has no output */
756 /* It's OK for _SDD to be missing too. */
757 swap_buf_le16(dev->id, ATA_ID_WORDS);
758 status = acpi_evaluate_object(ata_dev_acpi_handle(dev), "_SDD", &input,
759 NULL);
760 swap_buf_le16(dev->id, ATA_ID_WORDS);
762 if (status == AE_NOT_FOUND)
763 return -ENOENT;
765 if (ACPI_FAILURE(status)) {
766 ata_dev_warn(dev, "ACPI _SDD failed (AE 0x%x)\n", status);
767 return -EIO;
770 return 0;
774 * ata_acpi_on_suspend - ATA ACPI hook called on suspend
775 * @ap: target ATA port
777 * This function is called when @ap is about to be suspended. All
778 * devices are already put to sleep but the port_suspend() callback
779 * hasn't been executed yet. Error return from this function aborts
780 * suspend.
782 * LOCKING:
783 * EH context.
785 * RETURNS:
786 * 0 on success, -errno on failure.
788 int ata_acpi_on_suspend(struct ata_port *ap)
790 /* nada */
791 return 0;
795 * ata_acpi_on_resume - ATA ACPI hook called on resume
796 * @ap: target ATA port
798 * This function is called when @ap is resumed - right after port
799 * itself is resumed but before any EH action is taken.
801 * LOCKING:
802 * EH context.
804 void ata_acpi_on_resume(struct ata_port *ap)
806 const struct ata_acpi_gtm *gtm = ata_acpi_init_gtm(ap);
807 struct ata_device *dev;
809 if (ata_ap_acpi_handle(ap) && gtm) {
810 /* _GTM valid */
812 /* restore timing parameters */
813 ata_acpi_stm(ap, gtm);
815 /* _GTF should immediately follow _STM so that it can
816 * use values set by _STM. Cache _GTF result and
817 * schedule _GTF.
819 ata_for_each_dev(dev, &ap->link, ALL) {
820 ata_acpi_clear_gtf(dev);
821 if (ata_dev_enabled(dev) &&
822 ata_dev_get_GTF(dev, NULL) >= 0)
823 dev->flags |= ATA_DFLAG_ACPI_PENDING;
825 } else {
826 /* SATA _GTF needs to be evaulated after _SDD and
827 * there's no reason to evaluate IDE _GTF early
828 * without _STM. Clear cache and schedule _GTF.
830 ata_for_each_dev(dev, &ap->link, ALL) {
831 ata_acpi_clear_gtf(dev);
832 if (ata_dev_enabled(dev))
833 dev->flags |= ATA_DFLAG_ACPI_PENDING;
839 * ata_acpi_set_state - set the port power state
840 * @ap: target ATA port
841 * @state: state, on/off
843 * This function executes the _PS0/_PS3 ACPI method to set the power state.
844 * ACPI spec requires _PS0 when IDE power on and _PS3 when power off
846 void ata_acpi_set_state(struct ata_port *ap, pm_message_t state)
848 struct ata_device *dev;
849 acpi_handle handle;
850 int acpi_state;
852 /* channel first and then drives for power on and vica versa
853 for power off */
854 handle = ata_ap_acpi_handle(ap);
855 if (handle && state.event == PM_EVENT_ON)
856 acpi_bus_set_power(handle, ACPI_STATE_D0);
858 ata_for_each_dev(dev, &ap->link, ENABLED) {
859 handle = ata_dev_acpi_handle(dev);
860 if (!handle)
861 continue;
863 if (state.event != PM_EVENT_ON) {
864 acpi_state = acpi_pm_device_sleep_state(
865 &dev->sdev->sdev_gendev, NULL, ACPI_STATE_D3);
866 if (acpi_state > 0)
867 acpi_bus_set_power(handle, acpi_state);
868 /* TBD: need to check if it's runtime pm request */
869 acpi_pm_device_run_wake(
870 &dev->sdev->sdev_gendev, true);
871 } else {
872 /* Ditto */
873 acpi_pm_device_run_wake(
874 &dev->sdev->sdev_gendev, false);
875 acpi_bus_set_power(handle, ACPI_STATE_D0);
879 handle = ata_ap_acpi_handle(ap);
880 if (handle && state.event != PM_EVENT_ON)
881 acpi_bus_set_power(handle, ACPI_STATE_D3);
885 * ata_acpi_on_devcfg - ATA ACPI hook called on device donfiguration
886 * @dev: target ATA device
888 * This function is called when @dev is about to be configured.
889 * IDENTIFY data might have been modified after this hook is run.
891 * LOCKING:
892 * EH context.
894 * RETURNS:
895 * Positive number if IDENTIFY data needs to be refreshed, 0 if not,
896 * -errno on failure.
898 int ata_acpi_on_devcfg(struct ata_device *dev)
900 struct ata_port *ap = dev->link->ap;
901 struct ata_eh_context *ehc = &ap->link.eh_context;
902 int acpi_sata = ap->flags & ATA_FLAG_ACPI_SATA;
903 int nr_executed = 0;
904 int rc;
906 if (!ata_dev_acpi_handle(dev))
907 return 0;
909 /* do we need to do _GTF? */
910 if (!(dev->flags & ATA_DFLAG_ACPI_PENDING) &&
911 !(acpi_sata && (ehc->i.flags & ATA_EHI_DID_HARDRESET)))
912 return 0;
914 /* do _SDD if SATA */
915 if (acpi_sata) {
916 rc = ata_acpi_push_id(dev);
917 if (rc && rc != -ENOENT)
918 goto acpi_err;
921 /* do _GTF */
922 rc = ata_acpi_exec_tfs(dev, &nr_executed);
923 if (rc)
924 goto acpi_err;
926 dev->flags &= ~ATA_DFLAG_ACPI_PENDING;
928 /* refresh IDENTIFY page if any _GTF command has been executed */
929 if (nr_executed) {
930 rc = ata_dev_reread_id(dev, 0);
931 if (rc < 0) {
932 ata_dev_err(dev,
933 "failed to IDENTIFY after ACPI commands\n");
934 return rc;
938 return 0;
940 acpi_err:
941 /* ignore evaluation failure if we can continue safely */
942 if (rc == -EINVAL && !nr_executed && !(ap->pflags & ATA_PFLAG_FROZEN))
943 return 0;
945 /* fail and let EH retry once more for unknown IO errors */
946 if (!(dev->flags & ATA_DFLAG_ACPI_FAILED)) {
947 dev->flags |= ATA_DFLAG_ACPI_FAILED;
948 return rc;
951 dev->flags |= ATA_DFLAG_ACPI_DISABLED;
952 ata_dev_warn(dev, "ACPI: failed the second time, disabled\n");
954 /* We can safely continue if no _GTF command has been executed
955 * and port is not frozen.
957 if (!nr_executed && !(ap->pflags & ATA_PFLAG_FROZEN))
958 return 0;
960 return rc;
964 * ata_acpi_on_disable - ATA ACPI hook called when a device is disabled
965 * @dev: target ATA device
967 * This function is called when @dev is about to be disabled.
969 * LOCKING:
970 * EH context.
972 void ata_acpi_on_disable(struct ata_device *dev)
974 ata_acpi_clear_gtf(dev);
977 static void ata_acpi_wake_dev(acpi_handle handle, u32 event, void *context)
979 struct ata_device *ata_dev = context;
981 if (event == ACPI_NOTIFY_DEVICE_WAKE && ata_dev &&
982 pm_runtime_suspended(&ata_dev->sdev->sdev_gendev))
983 scsi_autopm_get_device(ata_dev->sdev);
986 static void ata_acpi_add_pm_notifier(struct ata_device *dev)
988 struct acpi_device *acpi_dev;
989 acpi_handle handle;
990 acpi_status status;
992 handle = ata_dev_acpi_handle(dev);
993 if (!handle)
994 return;
996 status = acpi_bus_get_device(handle, &acpi_dev);
997 if (ACPI_FAILURE(status))
998 return;
1000 if (dev->sdev->can_power_off) {
1001 acpi_install_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
1002 ata_acpi_wake_dev, dev);
1003 device_set_run_wake(&dev->sdev->sdev_gendev, true);
1007 static void ata_acpi_remove_pm_notifier(struct ata_device *dev)
1009 struct acpi_device *acpi_dev;
1010 acpi_handle handle;
1011 acpi_status status;
1013 handle = ata_dev_acpi_handle(dev);
1014 if (!handle)
1015 return;
1017 status = acpi_bus_get_device(handle, &acpi_dev);
1018 if (ACPI_FAILURE(status))
1019 return;
1021 if (dev->sdev->can_power_off) {
1022 device_set_run_wake(&dev->sdev->sdev_gendev, false);
1023 acpi_remove_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
1024 ata_acpi_wake_dev);
1028 static void ata_acpi_register_power_resource(struct ata_device *dev)
1030 struct scsi_device *sdev = dev->sdev;
1031 acpi_handle handle;
1032 struct device *device;
1034 handle = ata_dev_acpi_handle(dev);
1035 if (!handle)
1036 return;
1038 device = &sdev->sdev_gendev;
1040 acpi_power_resource_register_device(device, handle);
1043 static void ata_acpi_unregister_power_resource(struct ata_device *dev)
1045 struct scsi_device *sdev = dev->sdev;
1046 acpi_handle handle;
1047 struct device *device;
1049 handle = ata_dev_acpi_handle(dev);
1050 if (!handle)
1051 return;
1053 device = &sdev->sdev_gendev;
1055 acpi_power_resource_unregister_device(device, handle);
1058 void ata_acpi_bind(struct ata_device *dev)
1060 ata_acpi_add_pm_notifier(dev);
1061 ata_acpi_register_power_resource(dev);
1064 void ata_acpi_unbind(struct ata_device *dev)
1066 ata_acpi_remove_pm_notifier(dev);
1067 ata_acpi_unregister_power_resource(dev);
1070 static int compat_pci_ata(struct ata_port *ap)
1072 struct device *dev = ap->tdev.parent;
1073 struct pci_dev *pdev;
1075 if (!is_pci_dev(dev))
1076 return 0;
1078 pdev = to_pci_dev(dev);
1080 if ((pdev->class >> 8) != PCI_CLASS_STORAGE_SATA &&
1081 (pdev->class >> 8) != PCI_CLASS_STORAGE_IDE)
1082 return 0;
1084 return 1;
1087 static int ata_acpi_bind_host(struct ata_port *ap, acpi_handle *handle)
1089 if (ap->flags & ATA_FLAG_ACPI_SATA)
1090 return -ENODEV;
1092 *handle = acpi_get_child(DEVICE_ACPI_HANDLE(ap->tdev.parent),
1093 ap->port_no);
1095 if (!*handle)
1096 return -ENODEV;
1098 if (ata_acpi_gtm(ap, &ap->__acpi_init_gtm) == 0)
1099 ap->pflags |= ATA_PFLAG_INIT_GTM_VALID;
1101 return 0;
1104 static int ata_acpi_bind_device(struct ata_port *ap, struct scsi_device *sdev,
1105 acpi_handle *handle)
1107 struct ata_device *ata_dev;
1108 acpi_status status;
1109 struct acpi_device *acpi_dev;
1110 struct acpi_device_power_state *states;
1112 if (ap->flags & ATA_FLAG_ACPI_SATA) {
1113 if (!sata_pmp_attached(ap))
1114 ata_dev = &ap->link.device[sdev->id];
1115 else
1116 ata_dev = &ap->pmp_link[sdev->channel].device[sdev->id];
1118 else {
1119 ata_dev = &ap->link.device[sdev->id];
1122 *handle = ata_dev_acpi_handle(ata_dev);
1124 if (!*handle)
1125 return -ENODEV;
1127 status = acpi_bus_get_device(*handle, &acpi_dev);
1128 if (ACPI_FAILURE(status))
1129 return 0;
1132 * If firmware has _PS3 or _PR3 for this device,
1133 * and this ata ODD device support device attention,
1134 * it means this device can be powered off
1136 states = acpi_dev->power.states;
1137 if ((states[ACPI_STATE_D3_HOT].flags.valid ||
1138 states[ACPI_STATE_D3_COLD].flags.explicit_set) &&
1139 ata_dev->flags & ATA_DFLAG_DA)
1140 sdev->can_power_off = 1;
1142 return 0;
1145 static int is_ata_port(const struct device *dev)
1147 return dev->type == &ata_port_type;
1150 static struct ata_port *dev_to_ata_port(struct device *dev)
1152 while (!is_ata_port(dev)) {
1153 if (!dev->parent)
1154 return NULL;
1155 dev = dev->parent;
1157 return to_ata_port(dev);
1160 static int ata_acpi_find_device(struct device *dev, acpi_handle *handle)
1162 struct ata_port *ap = dev_to_ata_port(dev);
1164 if (!ap)
1165 return -ENODEV;
1167 if (!compat_pci_ata(ap))
1168 return -ENODEV;
1170 if (scsi_is_host_device(dev))
1171 return ata_acpi_bind_host(ap, handle);
1172 else if (scsi_is_sdev_device(dev)) {
1173 struct scsi_device *sdev = to_scsi_device(dev);
1175 return ata_acpi_bind_device(ap, sdev, handle);
1176 } else
1177 return -ENODEV;
1180 static int ata_acpi_find_dummy(struct device *dev, acpi_handle *handle)
1182 return -ENODEV;
1185 static struct acpi_bus_type ata_acpi_bus = {
1186 .find_bridge = ata_acpi_find_dummy,
1187 .find_device = ata_acpi_find_device,
1190 int ata_acpi_register(void)
1192 return scsi_register_acpi_bus_type(&ata_acpi_bus);
1195 void ata_acpi_unregister(void)
1197 scsi_unregister_acpi_bus_type(&ata_acpi_bus);