Kristen Carlson Accardi | 11ef697 | 2006-09-28 11:29:01 -0700 | [diff] [blame] | 1 | /* |
| 2 | * libata-acpi.c |
| 3 | * Provides ACPI support for PATA/SATA. |
| 4 | * |
| 5 | * Copyright (C) 2006 Intel Corp. |
| 6 | * Copyright (C) 2006 Randy Dunlap |
| 7 | */ |
| 8 | |
Tejun Heo | 3264a8d | 2007-12-15 15:05:06 +0900 | [diff] [blame] | 9 | #include <linux/module.h> |
Kristen Carlson Accardi | 11ef697 | 2006-09-28 11:29:01 -0700 | [diff] [blame] | 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> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 18 | #include <linux/slab.h> |
Lin Ming | 3bd4660 | 2012-06-25 16:13:06 +0800 | [diff] [blame] | 19 | #include <linux/pm_runtime.h> |
Matthew Garrett | 237d844 | 2007-10-03 01:24:16 +0100 | [diff] [blame] | 20 | #include <scsi/scsi_device.h> |
Kristen Carlson Accardi | 11ef697 | 2006-09-28 11:29:01 -0700 | [diff] [blame] | 21 | #include "libata.h" |
| 22 | |
Tejun Heo | 110f66d | 2009-09-16 04:17:28 +0900 | [diff] [blame] | 23 | unsigned int ata_acpi_gtf_filter = ATA_ACPI_FILTER_DEFAULT; |
Tejun Heo | 3264a8d | 2007-12-15 15:05:06 +0900 | [diff] [blame] | 24 | module_param_named(acpi_gtf_filter, ata_acpi_gtf_filter, int, 0644); |
Tejun Heo | fa5b561 | 2009-09-16 04:17:02 +0900 | [diff] [blame] | 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)"); |
Tejun Heo | 3264a8d | 2007-12-15 15:05:06 +0900 | [diff] [blame] | 26 | |
Kristen Carlson Accardi | 11ef697 | 2006-09-28 11:29:01 -0700 | [diff] [blame] | 27 | #define NO_PORT_MULT 0xffff |
Jeff Garzik | 2dcb407 | 2007-10-19 06:42:56 -0400 | [diff] [blame] | 28 | #define SATA_ADR(root, pmp) (((root) << 16) | (pmp)) |
Kristen Carlson Accardi | 11ef697 | 2006-09-28 11:29:01 -0700 | [diff] [blame] | 29 | |
| 30 | #define REGS_PER_GTF 7 |
Tejun Heo | 4700c4b | 2007-05-15 03:28:16 +0900 | [diff] [blame] | 31 | struct ata_acpi_gtf { |
| 32 | u8 tf[REGS_PER_GTF]; /* regs. 0x1f1 - 0x1f7 */ |
| 33 | } __packed; |
Kristen Carlson Accardi | 11ef697 | 2006-09-28 11:29:01 -0700 | [diff] [blame] | 34 | |
Tejun Heo | 398e078 | 2007-12-15 15:05:03 +0900 | [diff] [blame] | 35 | static void ata_acpi_clear_gtf(struct ata_device *dev) |
| 36 | { |
| 37 | kfree(dev->gtf_cache); |
| 38 | dev->gtf_cache = NULL; |
| 39 | } |
| 40 | |
Matthew Garrett | 30dcf76 | 2012-06-25 16:13:04 +0800 | [diff] [blame] | 41 | /** |
Matthew Garrett | 30dcf76 | 2012-06-25 16:13:04 +0800 | [diff] [blame] | 42 | * ata_dev_acpi_handle - provide the acpi_handle for an ata_device |
Aaron Lu | f1bc1e4 | 2013-08-23 10:17:54 +0800 | [diff] [blame] | 43 | * @dev: the acpi_handle returned will correspond to this device |
Matthew Garrett | 30dcf76 | 2012-06-25 16:13:04 +0800 | [diff] [blame] | 44 | * |
| 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 |
Aaron Lu | f1bc1e4 | 2013-08-23 10:17:54 +0800 | [diff] [blame] | 47 | * or ACPI is disabled for this device due to consecutive errors. |
Matthew Garrett | 30dcf76 | 2012-06-25 16:13:04 +0800 | [diff] [blame] | 48 | */ |
| 49 | acpi_handle ata_dev_acpi_handle(struct ata_device *dev) |
Matthew Garrett | 6b66d95 | 2012-06-25 16:13:03 +0800 | [diff] [blame] | 50 | { |
Aaron Lu | f1bc1e4 | 2013-08-23 10:17:54 +0800 | [diff] [blame] | 51 | return dev->flags & ATA_DFLAG_ACPI_DISABLED ? |
| 52 | NULL : ACPI_HANDLE(&dev->tdev); |
Matthew Garrett | 6b66d95 | 2012-06-25 16:13:03 +0800 | [diff] [blame] | 53 | } |
Kristen Carlson Accardi | 11ef697 | 2006-09-28 11:29:01 -0700 | [diff] [blame] | 54 | |
Holger Macht | 664d080 | 2008-06-03 20:27:59 +0200 | [diff] [blame] | 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) |
| 57 | { |
| 58 | if (dev) |
| 59 | dev->flags |= ATA_DFLAG_DETACH; |
| 60 | else { |
| 61 | struct ata_link *tlink; |
| 62 | struct ata_device *tdev; |
| 63 | |
Tejun Heo | 1eca436 | 2008-11-03 20:03:17 +0900 | [diff] [blame] | 64 | ata_for_each_link(tlink, ap, EDGE) |
| 65 | ata_for_each_dev(tdev, tlink, ALL) |
Holger Macht | 664d080 | 2008-06-03 20:27:59 +0200 | [diff] [blame] | 66 | tdev->flags |= ATA_DFLAG_DETACH; |
| 67 | } |
| 68 | |
| 69 | ata_port_schedule_eh(ap); |
| 70 | } |
| 71 | |
| 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 |
Holger Macht | 664d080 | 2008-06-03 20:27:59 +0200 | [diff] [blame] | 77 | * |
| 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. |
| 81 | * |
| 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. |
| 85 | * |
| 86 | * LOCKING: |
| 87 | * ACPI notify handler context. May sleep. |
| 88 | */ |
| 89 | static void ata_acpi_handle_hotplug(struct ata_port *ap, struct ata_device *dev, |
Shaohua Li | f730ae1 | 2008-08-28 10:05:45 +0800 | [diff] [blame] | 90 | u32 event) |
Matthew Garrett | 237d844 | 2007-10-03 01:24:16 +0100 | [diff] [blame] | 91 | { |
Holger Macht | 664d080 | 2008-06-03 20:27:59 +0200 | [diff] [blame] | 92 | struct ata_eh_info *ehi = &ap->link.eh_info; |
Tejun Heo | 233f112 | 2008-03-12 14:24:43 +0900 | [diff] [blame] | 93 | int wait = 0; |
| 94 | unsigned long flags; |
Zhang Rui | 3c1e389 | 2008-07-11 09:42:03 -0400 | [diff] [blame] | 95 | |
Holger Macht | 664d080 | 2008-06-03 20:27:59 +0200 | [diff] [blame] | 96 | spin_lock_irqsave(ap->lock, flags); |
Shaohua Li | f730ae1 | 2008-08-28 10:05:45 +0800 | [diff] [blame] | 97 | /* |
| 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 |
| 101 | */ |
Tejun Heo | 233f112 | 2008-03-12 14:24:43 +0900 | [diff] [blame] | 102 | switch (event) { |
| 103 | case ACPI_NOTIFY_BUS_CHECK: |
| 104 | case ACPI_NOTIFY_DEVICE_CHECK: |
| 105 | ata_ehi_push_desc(ehi, "ACPI event"); |
Jeff Garzik | c85665f | 2008-05-19 17:56:10 -0400 | [diff] [blame] | 106 | |
Shaohua Li | f730ae1 | 2008-08-28 10:05:45 +0800 | [diff] [blame] | 107 | ata_ehi_hotplugged(ehi); |
| 108 | ata_port_freeze(ap); |
Holger Macht | 664d080 | 2008-06-03 20:27:59 +0200 | [diff] [blame] | 109 | break; |
| 110 | case ACPI_NOTIFY_EJECT_REQUEST: |
| 111 | ata_ehi_push_desc(ehi, "ACPI event"); |
| 112 | |
Holger Macht | 664d080 | 2008-06-03 20:27:59 +0200 | [diff] [blame] | 113 | ata_acpi_detach_device(ap, dev); |
| 114 | wait = 1; |
| 115 | break; |
Matthew Garrett | 237d844 | 2007-10-03 01:24:16 +0100 | [diff] [blame] | 116 | } |
| 117 | |
Matthew Garrett | ae6c23c | 2008-05-19 17:29:34 +0100 | [diff] [blame] | 118 | spin_unlock_irqrestore(ap->lock, flags); |
| 119 | |
Aaron Lu | f1bc1e4 | 2013-08-23 10:17:54 +0800 | [diff] [blame] | 120 | if (wait) |
Matthew Garrett | ae6c23c | 2008-05-19 17:29:34 +0100 | [diff] [blame] | 121 | ata_port_wait_eh(ap); |
Holger Macht | 664d080 | 2008-06-03 20:27:59 +0200 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | static void ata_acpi_dev_notify_dock(acpi_handle handle, u32 event, void *data) |
| 125 | { |
| 126 | struct ata_device *dev = data; |
| 127 | |
Shaohua Li | f730ae1 | 2008-08-28 10:05:45 +0800 | [diff] [blame] | 128 | ata_acpi_handle_hotplug(dev->link->ap, dev, event); |
Holger Macht | 664d080 | 2008-06-03 20:27:59 +0200 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | static void ata_acpi_ap_notify_dock(acpi_handle handle, u32 event, void *data) |
| 132 | { |
| 133 | struct ata_port *ap = data; |
| 134 | |
Shaohua Li | f730ae1 | 2008-08-28 10:05:45 +0800 | [diff] [blame] | 135 | ata_acpi_handle_hotplug(ap, NULL, event); |
Matthew Garrett | 237d844 | 2007-10-03 01:24:16 +0100 | [diff] [blame] | 136 | } |
| 137 | |
Shaohua Li | 1253f7a | 2008-08-28 10:06:16 +0800 | [diff] [blame] | 138 | static void ata_acpi_uevent(struct ata_port *ap, struct ata_device *dev, |
| 139 | u32 event) |
| 140 | { |
| 141 | struct kobject *kobj = NULL; |
| 142 | char event_string[20]; |
| 143 | char *envp[] = { event_string, NULL }; |
| 144 | |
| 145 | if (dev) { |
| 146 | if (dev->sdev) |
| 147 | kobj = &dev->sdev->sdev_gendev.kobj; |
| 148 | } else |
| 149 | kobj = &ap->dev->kobj; |
| 150 | |
| 151 | if (kobj) { |
| 152 | snprintf(event_string, 20, "BAY_EVENT=%d", event); |
| 153 | kobject_uevent_env(kobj, KOBJ_CHANGE, envp); |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | static void ata_acpi_ap_uevent(acpi_handle handle, u32 event, void *data) |
| 158 | { |
| 159 | ata_acpi_uevent(data, NULL, event); |
| 160 | } |
| 161 | |
| 162 | static void ata_acpi_dev_uevent(acpi_handle handle, u32 event, void *data) |
| 163 | { |
| 164 | struct ata_device *dev = data; |
| 165 | ata_acpi_uevent(dev->link->ap, dev, event); |
| 166 | } |
| 167 | |
Vasiliy Kulikov | 9c8b04b | 2011-06-25 21:07:52 +0400 | [diff] [blame] | 168 | static const struct acpi_dock_ops ata_acpi_dev_dock_ops = { |
Shaohua Li | 1253f7a | 2008-08-28 10:06:16 +0800 | [diff] [blame] | 169 | .handler = ata_acpi_dev_notify_dock, |
| 170 | .uevent = ata_acpi_dev_uevent, |
| 171 | }; |
| 172 | |
Vasiliy Kulikov | 9c8b04b | 2011-06-25 21:07:52 +0400 | [diff] [blame] | 173 | static const struct acpi_dock_ops ata_acpi_ap_dock_ops = { |
Shaohua Li | 1253f7a | 2008-08-28 10:06:16 +0800 | [diff] [blame] | 174 | .handler = ata_acpi_ap_notify_dock, |
| 175 | .uevent = ata_acpi_ap_uevent, |
| 176 | }; |
| 177 | |
Aaron Lu | f1bc1e4 | 2013-08-23 10:17:54 +0800 | [diff] [blame] | 178 | /* bind acpi handle to pata port */ |
| 179 | void ata_acpi_bind_port(struct ata_port *ap) |
Aaron Lu | 4452152 | 2013-06-20 09:38:34 +0800 | [diff] [blame] | 180 | { |
Rafael J. Wysocki | 9c5ad36 | 2013-11-28 23:58:28 +0100 | [diff] [blame] | 181 | struct acpi_device *host_companion = ACPI_COMPANION(ap->host->dev); |
Aaron Lu | 4452152 | 2013-06-20 09:38:34 +0800 | [diff] [blame] | 182 | |
Rafael J. Wysocki | 9c5ad36 | 2013-11-28 23:58:28 +0100 | [diff] [blame] | 183 | if (libata_noacpi || ap->flags & ATA_FLAG_ACPI_SATA || !host_companion) |
Aaron Lu | f1bc1e4 | 2013-08-23 10:17:54 +0800 | [diff] [blame] | 184 | return; |
Aaron Lu | 4452152 | 2013-06-20 09:38:34 +0800 | [diff] [blame] | 185 | |
Rafael J. Wysocki | 9c5ad36 | 2013-11-28 23:58:28 +0100 | [diff] [blame] | 186 | acpi_preset_companion(&ap->tdev, host_companion, ap->port_no); |
Aaron Lu | 4452152 | 2013-06-20 09:38:34 +0800 | [diff] [blame] | 187 | |
Aaron Lu | f1bc1e4 | 2013-08-23 10:17:54 +0800 | [diff] [blame] | 188 | if (ata_acpi_gtm(ap, &ap->__acpi_init_gtm) == 0) |
| 189 | ap->pflags |= ATA_PFLAG_INIT_GTM_VALID; |
Aaron Lu | 4452152 | 2013-06-20 09:38:34 +0800 | [diff] [blame] | 190 | |
Aaron Lu | f1bc1e4 | 2013-08-23 10:17:54 +0800 | [diff] [blame] | 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); |
| 194 | } |
Aaron Lu | 4452152 | 2013-06-20 09:38:34 +0800 | [diff] [blame] | 195 | |
Aaron Lu | f1bc1e4 | 2013-08-23 10:17:54 +0800 | [diff] [blame] | 196 | void ata_acpi_bind_dev(struct ata_device *dev) |
| 197 | { |
| 198 | struct ata_port *ap = dev->link->ap; |
Rafael J. Wysocki | 9c5ad36 | 2013-11-28 23:58:28 +0100 | [diff] [blame] | 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; |
Aaron Lu | f1bc1e4 | 2013-08-23 10:17:54 +0800 | [diff] [blame] | 202 | u64 adr; |
| 203 | |
| 204 | /* |
Rafael J. Wysocki | 9c5ad36 | 2013-11-28 23:58:28 +0100 | [diff] [blame] | 205 | * For both sata/pata devices, host companion device is required. |
| 206 | * For pata device, port companion device is also required. |
Aaron Lu | f1bc1e4 | 2013-08-23 10:17:54 +0800 | [diff] [blame] | 207 | */ |
Rafael J. Wysocki | 9c5ad36 | 2013-11-28 23:58:28 +0100 | [diff] [blame] | 208 | if (libata_noacpi || !host_companion || |
| 209 | (!(ap->flags & ATA_FLAG_ACPI_SATA) && !port_companion)) |
Aaron Lu | f1bc1e4 | 2013-08-23 10:17:54 +0800 | [diff] [blame] | 210 | return; |
| 211 | |
| 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); |
Rafael J. Wysocki | 9c5ad36 | 2013-11-28 23:58:28 +0100 | [diff] [blame] | 217 | parent = host_companion; |
Aaron Lu | f1bc1e4 | 2013-08-23 10:17:54 +0800 | [diff] [blame] | 218 | } else { |
| 219 | adr = dev->devno; |
Rafael J. Wysocki | 9c5ad36 | 2013-11-28 23:58:28 +0100 | [diff] [blame] | 220 | parent = port_companion; |
Aaron Lu | 4452152 | 2013-06-20 09:38:34 +0800 | [diff] [blame] | 221 | } |
Aaron Lu | f1bc1e4 | 2013-08-23 10:17:54 +0800 | [diff] [blame] | 222 | |
Rafael J. Wysocki | 9c5ad36 | 2013-11-28 23:58:28 +0100 | [diff] [blame] | 223 | acpi_preset_companion(&dev->tdev, parent, adr); |
Aaron Lu | f1bc1e4 | 2013-08-23 10:17:54 +0800 | [diff] [blame] | 224 | |
| 225 | register_hotplug_dock_device(ata_dev_acpi_handle(dev), |
| 226 | &ata_acpi_dev_dock_ops, dev, NULL, NULL); |
Aaron Lu | 4452152 | 2013-06-20 09:38:34 +0800 | [diff] [blame] | 227 | } |
| 228 | |
Kristen Carlson Accardi | 11ef697 | 2006-09-28 11:29:01 -0700 | [diff] [blame] | 229 | /** |
Tejun Heo | 562f0c2 | 2007-12-15 15:05:01 +0900 | [diff] [blame] | 230 | * ata_acpi_dissociate - dissociate ATA host from ACPI objects |
| 231 | * @host: target ATA host |
| 232 | * |
| 233 | * This function is called during driver detach after the whole host |
| 234 | * is shut down. |
| 235 | * |
| 236 | * LOCKING: |
| 237 | * EH context. |
| 238 | */ |
| 239 | void ata_acpi_dissociate(struct ata_host *host) |
| 240 | { |
Tejun Heo | c05e6ff | 2007-12-15 15:05:02 +0900 | [diff] [blame] | 241 | int i; |
| 242 | |
| 243 | /* Restore initial _GTM values so that driver which attaches |
| 244 | * afterward can use them too. |
| 245 | */ |
| 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); |
| 249 | |
Aaron Lu | f1bc1e4 | 2013-08-23 10:17:54 +0800 | [diff] [blame] | 250 | if (ACPI_HANDLE(&ap->tdev) && gtm) |
Tejun Heo | c05e6ff | 2007-12-15 15:05:02 +0900 | [diff] [blame] | 251 | ata_acpi_stm(ap, gtm); |
| 252 | } |
Tejun Heo | 562f0c2 | 2007-12-15 15:05:01 +0900 | [diff] [blame] | 253 | } |
| 254 | |
Aaron Lu | f1bc1e4 | 2013-08-23 10:17:54 +0800 | [diff] [blame] | 255 | /** |
| 256 | * ata_acpi_gtm - execute _GTM |
| 257 | * @ap: target ATA port |
| 258 | * @gtm: out parameter for _GTM result |
| 259 | * |
| 260 | * Evaluate _GTM and store the result in @gtm. |
| 261 | * |
| 262 | * LOCKING: |
| 263 | * EH context. |
| 264 | * |
| 265 | * RETURNS: |
| 266 | * 0 on success, -ENOENT if _GTM doesn't exist, -errno on failure. |
| 267 | */ |
| 268 | int ata_acpi_gtm(struct ata_port *ap, struct ata_acpi_gtm *gtm) |
Tejun Heo | 64578a3 | 2007-05-15 03:28:16 +0900 | [diff] [blame] | 269 | { |
| 270 | struct acpi_buffer output = { .length = ACPI_ALLOCATE_BUFFER }; |
| 271 | union acpi_object *out_obj; |
| 272 | acpi_status status; |
| 273 | int rc = 0; |
Aaron Lu | f1bc1e4 | 2013-08-23 10:17:54 +0800 | [diff] [blame] | 274 | acpi_handle handle = ACPI_HANDLE(&ap->tdev); |
| 275 | |
| 276 | if (!handle) |
| 277 | return -EINVAL; |
Tejun Heo | 64578a3 | 2007-05-15 03:28:16 +0900 | [diff] [blame] | 278 | |
Aaron Lu | d66af4d | 2013-04-27 09:33:07 +0800 | [diff] [blame] | 279 | status = acpi_evaluate_object(handle, "_GTM", NULL, &output); |
Tejun Heo | 64578a3 | 2007-05-15 03:28:16 +0900 | [diff] [blame] | 280 | |
| 281 | rc = -ENOENT; |
| 282 | if (status == AE_NOT_FOUND) |
| 283 | goto out_free; |
| 284 | |
| 285 | rc = -EINVAL; |
| 286 | if (ACPI_FAILURE(status)) { |
Joe Perches | a9a79df | 2011-04-15 15:51:59 -0700 | [diff] [blame] | 287 | ata_port_err(ap, "ACPI get timing mode failed (AE 0x%x)\n", |
| 288 | status); |
Tejun Heo | 64578a3 | 2007-05-15 03:28:16 +0900 | [diff] [blame] | 289 | goto out_free; |
| 290 | } |
| 291 | |
| 292 | out_obj = output.pointer; |
| 293 | if (out_obj->type != ACPI_TYPE_BUFFER) { |
Joe Perches | a9a79df | 2011-04-15 15:51:59 -0700 | [diff] [blame] | 294 | ata_port_warn(ap, "_GTM returned unexpected object type 0x%x\n", |
| 295 | out_obj->type); |
Tejun Heo | 64578a3 | 2007-05-15 03:28:16 +0900 | [diff] [blame] | 296 | |
| 297 | goto out_free; |
| 298 | } |
| 299 | |
| 300 | if (out_obj->buffer.length != sizeof(struct ata_acpi_gtm)) { |
Joe Perches | a9a79df | 2011-04-15 15:51:59 -0700 | [diff] [blame] | 301 | ata_port_err(ap, "_GTM returned invalid length %d\n", |
| 302 | out_obj->buffer.length); |
Tejun Heo | 64578a3 | 2007-05-15 03:28:16 +0900 | [diff] [blame] | 303 | goto out_free; |
| 304 | } |
| 305 | |
| 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; |
| 311 | } |
| 312 | |
Alan Cox | badff03 | 2007-10-04 21:28:18 +0100 | [diff] [blame] | 313 | EXPORT_SYMBOL_GPL(ata_acpi_gtm); |
| 314 | |
Tejun Heo | 64578a3 | 2007-05-15 03:28:16 +0900 | [diff] [blame] | 315 | /** |
| 316 | * ata_acpi_stm - execute _STM |
| 317 | * @ap: target ATA port |
| 318 | * @stm: timing parameter to _STM |
| 319 | * |
| 320 | * Evaluate _STM with timing parameter @stm. |
| 321 | * |
| 322 | * LOCKING: |
| 323 | * EH context. |
| 324 | * |
| 325 | * RETURNS: |
| 326 | * 0 on success, -ENOENT if _STM doesn't exist, -errno on failure. |
| 327 | */ |
Tejun Heo | 0d02f0b | 2007-12-15 15:04:57 +0900 | [diff] [blame] | 328 | int ata_acpi_stm(struct ata_port *ap, const struct ata_acpi_gtm *stm) |
Tejun Heo | 64578a3 | 2007-05-15 03:28:16 +0900 | [diff] [blame] | 329 | { |
| 330 | acpi_status status; |
Tejun Heo | 0d02f0b | 2007-12-15 15:04:57 +0900 | [diff] [blame] | 331 | struct ata_acpi_gtm stm_buf = *stm; |
Tejun Heo | 64578a3 | 2007-05-15 03:28:16 +0900 | [diff] [blame] | 332 | struct acpi_object_list input; |
| 333 | union acpi_object in_params[3]; |
| 334 | |
| 335 | in_params[0].type = ACPI_TYPE_BUFFER; |
| 336 | in_params[0].buffer.length = sizeof(struct ata_acpi_gtm); |
Tejun Heo | 0d02f0b | 2007-12-15 15:04:57 +0900 | [diff] [blame] | 337 | in_params[0].buffer.pointer = (u8 *)&stm_buf; |
Tejun Heo | 64578a3 | 2007-05-15 03:28:16 +0900 | [diff] [blame] | 338 | /* Buffers for id may need byteswapping ? */ |
| 339 | in_params[1].type = ACPI_TYPE_BUFFER; |
| 340 | in_params[1].buffer.length = 512; |
Tejun Heo | 9af5c9c | 2007-08-06 18:36:22 +0900 | [diff] [blame] | 341 | in_params[1].buffer.pointer = (u8 *)ap->link.device[0].id; |
Tejun Heo | 64578a3 | 2007-05-15 03:28:16 +0900 | [diff] [blame] | 342 | in_params[2].type = ACPI_TYPE_BUFFER; |
| 343 | in_params[2].buffer.length = 512; |
Tejun Heo | 9af5c9c | 2007-08-06 18:36:22 +0900 | [diff] [blame] | 344 | in_params[2].buffer.pointer = (u8 *)ap->link.device[1].id; |
Tejun Heo | 64578a3 | 2007-05-15 03:28:16 +0900 | [diff] [blame] | 345 | |
| 346 | input.count = 3; |
| 347 | input.pointer = in_params; |
| 348 | |
Aaron Lu | f1bc1e4 | 2013-08-23 10:17:54 +0800 | [diff] [blame] | 349 | status = acpi_evaluate_object(ACPI_HANDLE(&ap->tdev), "_STM", |
| 350 | &input, NULL); |
Tejun Heo | 64578a3 | 2007-05-15 03:28:16 +0900 | [diff] [blame] | 351 | |
| 352 | if (status == AE_NOT_FOUND) |
| 353 | return -ENOENT; |
| 354 | if (ACPI_FAILURE(status)) { |
Joe Perches | a9a79df | 2011-04-15 15:51:59 -0700 | [diff] [blame] | 355 | ata_port_err(ap, "ACPI set timing mode failed (status=0x%x)\n", |
| 356 | status); |
Tejun Heo | 64578a3 | 2007-05-15 03:28:16 +0900 | [diff] [blame] | 357 | return -EINVAL; |
| 358 | } |
| 359 | return 0; |
| 360 | } |
| 361 | |
Alan Cox | badff03 | 2007-10-04 21:28:18 +0100 | [diff] [blame] | 362 | EXPORT_SYMBOL_GPL(ata_acpi_stm); |
| 363 | |
Tejun Heo | 64578a3 | 2007-05-15 03:28:16 +0900 | [diff] [blame] | 364 | /** |
Tejun Heo | 4700c4b | 2007-05-15 03:28:16 +0900 | [diff] [blame] | 365 | * ata_dev_get_GTF - get the drive bootup default taskfile settings |
Tejun Heo | 3a32a8e | 2007-05-05 23:50:38 +0900 | [diff] [blame] | 366 | * @dev: target ATA device |
Tejun Heo | 4700c4b | 2007-05-15 03:28:16 +0900 | [diff] [blame] | 367 | * @gtf: output parameter for buffer containing _GTF taskfile arrays |
Kristen Carlson Accardi | 11ef697 | 2006-09-28 11:29:01 -0700 | [diff] [blame] | 368 | * |
| 369 | * This applies to both PATA and SATA drives. |
| 370 | * |
| 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. |
| 376 | * |
Tejun Heo | 4700c4b | 2007-05-15 03:28:16 +0900 | [diff] [blame] | 377 | * LOCKING: |
| 378 | * EH context. |
| 379 | * |
| 380 | * RETURNS: |
Tejun Heo | 66fa7f2 | 2007-12-15 15:05:04 +0900 | [diff] [blame] | 381 | * Number of taskfiles on success, 0 if _GTF doesn't exist. -EINVAL |
| 382 | * if _GTF is invalid. |
Kristen Carlson Accardi | 11ef697 | 2006-09-28 11:29:01 -0700 | [diff] [blame] | 383 | */ |
Tejun Heo | 398e078 | 2007-12-15 15:05:03 +0900 | [diff] [blame] | 384 | static int ata_dev_get_GTF(struct ata_device *dev, struct ata_acpi_gtf **gtf) |
Kristen Carlson Accardi | 11ef697 | 2006-09-28 11:29:01 -0700 | [diff] [blame] | 385 | { |
Tejun Heo | 9af5c9c | 2007-08-06 18:36:22 +0900 | [diff] [blame] | 386 | struct ata_port *ap = dev->link->ap; |
Tejun Heo | 3a32a8e | 2007-05-05 23:50:38 +0900 | [diff] [blame] | 387 | acpi_status status; |
Tejun Heo | 3a32a8e | 2007-05-05 23:50:38 +0900 | [diff] [blame] | 388 | struct acpi_buffer output; |
| 389 | union acpi_object *out_obj; |
Tejun Heo | 4700c4b | 2007-05-15 03:28:16 +0900 | [diff] [blame] | 390 | int rc = 0; |
Kristen Carlson Accardi | 11ef697 | 2006-09-28 11:29:01 -0700 | [diff] [blame] | 391 | |
Tejun Heo | 398e078 | 2007-12-15 15:05:03 +0900 | [diff] [blame] | 392 | /* if _GTF is cached, use the cached value */ |
| 393 | if (dev->gtf_cache) { |
| 394 | out_obj = dev->gtf_cache; |
| 395 | goto done; |
| 396 | } |
| 397 | |
Tejun Heo | 4700c4b | 2007-05-15 03:28:16 +0900 | [diff] [blame] | 398 | /* set up output buffer */ |
| 399 | output.length = ACPI_ALLOCATE_BUFFER; |
| 400 | output.pointer = NULL; /* ACPI-CA sets this; save/free it later */ |
Kristen Carlson Accardi | 11ef697 | 2006-09-28 11:29:01 -0700 | [diff] [blame] | 401 | |
Kristen Carlson Accardi | 11ef697 | 2006-09-28 11:29:01 -0700 | [diff] [blame] | 402 | if (ata_msg_probe(ap)) |
Joe Perches | a9a79df | 2011-04-15 15:51:59 -0700 | [diff] [blame] | 403 | ata_dev_dbg(dev, "%s: ENTER: port#: %d\n", |
| 404 | __func__, ap->port_no); |
Kristen Carlson Accardi | 11ef697 | 2006-09-28 11:29:01 -0700 | [diff] [blame] | 405 | |
Kristen Carlson Accardi | 11ef697 | 2006-09-28 11:29:01 -0700 | [diff] [blame] | 406 | /* _GTF has no input parameters */ |
Matthew Garrett | 30dcf76 | 2012-06-25 16:13:04 +0800 | [diff] [blame] | 407 | status = acpi_evaluate_object(ata_dev_acpi_handle(dev), "_GTF", NULL, |
| 408 | &output); |
Tejun Heo | 398e078 | 2007-12-15 15:05:03 +0900 | [diff] [blame] | 409 | out_obj = dev->gtf_cache = output.pointer; |
Tejun Heo | 4700c4b | 2007-05-15 03:28:16 +0900 | [diff] [blame] | 410 | |
Kristen Carlson Accardi | 11ef697 | 2006-09-28 11:29:01 -0700 | [diff] [blame] | 411 | if (ACPI_FAILURE(status)) { |
Tejun Heo | 4700c4b | 2007-05-15 03:28:16 +0900 | [diff] [blame] | 412 | if (status != AE_NOT_FOUND) { |
Joe Perches | a9a79df | 2011-04-15 15:51:59 -0700 | [diff] [blame] | 413 | ata_dev_warn(dev, "_GTF evaluation failed (AE 0x%x)\n", |
| 414 | status); |
Tejun Heo | 66fa7f2 | 2007-12-15 15:05:04 +0900 | [diff] [blame] | 415 | rc = -EINVAL; |
Tejun Heo | 4700c4b | 2007-05-15 03:28:16 +0900 | [diff] [blame] | 416 | } |
| 417 | goto out_free; |
Kristen Carlson Accardi | 11ef697 | 2006-09-28 11:29:01 -0700 | [diff] [blame] | 418 | } |
| 419 | |
| 420 | if (!output.length || !output.pointer) { |
| 421 | if (ata_msg_probe(ap)) |
Joe Perches | a9a79df | 2011-04-15 15:51:59 -0700 | [diff] [blame] | 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); |
Tejun Heo | 66fa7f2 | 2007-12-15 15:05:04 +0900 | [diff] [blame] | 426 | rc = -EINVAL; |
Tejun Heo | 4700c4b | 2007-05-15 03:28:16 +0900 | [diff] [blame] | 427 | goto out_free; |
Kristen Carlson Accardi | 11ef697 | 2006-09-28 11:29:01 -0700 | [diff] [blame] | 428 | } |
| 429 | |
Kristen Carlson Accardi | 11ef697 | 2006-09-28 11:29:01 -0700 | [diff] [blame] | 430 | if (out_obj->type != ACPI_TYPE_BUFFER) { |
Joe Perches | a9a79df | 2011-04-15 15:51:59 -0700 | [diff] [blame] | 431 | ata_dev_warn(dev, "_GTF unexpected object type 0x%x\n", |
| 432 | out_obj->type); |
Tejun Heo | 66fa7f2 | 2007-12-15 15:05:04 +0900 | [diff] [blame] | 433 | rc = -EINVAL; |
Tejun Heo | 4700c4b | 2007-05-15 03:28:16 +0900 | [diff] [blame] | 434 | goto out_free; |
Kristen Carlson Accardi | 11ef697 | 2006-09-28 11:29:01 -0700 | [diff] [blame] | 435 | } |
| 436 | |
Tejun Heo | 4700c4b | 2007-05-15 03:28:16 +0900 | [diff] [blame] | 437 | if (out_obj->buffer.length % REGS_PER_GTF) { |
Joe Perches | a9a79df | 2011-04-15 15:51:59 -0700 | [diff] [blame] | 438 | ata_dev_warn(dev, "unexpected _GTF length (%d)\n", |
| 439 | out_obj->buffer.length); |
Tejun Heo | 66fa7f2 | 2007-12-15 15:05:04 +0900 | [diff] [blame] | 440 | rc = -EINVAL; |
Tejun Heo | 4700c4b | 2007-05-15 03:28:16 +0900 | [diff] [blame] | 441 | goto out_free; |
Kristen Carlson Accardi | 11ef697 | 2006-09-28 11:29:01 -0700 | [diff] [blame] | 442 | } |
| 443 | |
Tejun Heo | 398e078 | 2007-12-15 15:05:03 +0900 | [diff] [blame] | 444 | done: |
Tejun Heo | 4700c4b | 2007-05-15 03:28:16 +0900 | [diff] [blame] | 445 | rc = out_obj->buffer.length / REGS_PER_GTF; |
Tejun Heo | 398e078 | 2007-12-15 15:05:03 +0900 | [diff] [blame] | 446 | if (gtf) { |
| 447 | *gtf = (void *)out_obj->buffer.pointer; |
| 448 | if (ata_msg_probe(ap)) |
Joe Perches | a9a79df | 2011-04-15 15:51:59 -0700 | [diff] [blame] | 449 | ata_dev_dbg(dev, "%s: returning gtf=%p, gtf_count=%d\n", |
| 450 | __func__, *gtf, rc); |
Tejun Heo | 398e078 | 2007-12-15 15:05:03 +0900 | [diff] [blame] | 451 | } |
Tejun Heo | 4700c4b | 2007-05-15 03:28:16 +0900 | [diff] [blame] | 452 | return rc; |
| 453 | |
| 454 | out_free: |
Tejun Heo | 398e078 | 2007-12-15 15:05:03 +0900 | [diff] [blame] | 455 | ata_acpi_clear_gtf(dev); |
Tejun Heo | 4700c4b | 2007-05-15 03:28:16 +0900 | [diff] [blame] | 456 | return rc; |
Kristen Carlson Accardi | 11ef697 | 2006-09-28 11:29:01 -0700 | [diff] [blame] | 457 | } |
| 458 | |
Tejun Heo | 7c77fa4 | 2007-12-18 16:33:03 +0900 | [diff] [blame] | 459 | /** |
| 460 | * ata_acpi_gtm_xfermode - determine xfermode from GTM parameter |
| 461 | * @dev: target device |
| 462 | * @gtm: GTM parameter to use |
| 463 | * |
| 464 | * Determine xfermask for @dev from @gtm. |
| 465 | * |
| 466 | * LOCKING: |
| 467 | * None. |
| 468 | * |
| 469 | * RETURNS: |
| 470 | * Determined xfermask. |
| 471 | */ |
| 472 | unsigned long ata_acpi_gtm_xfermask(struct ata_device *dev, |
| 473 | const struct ata_acpi_gtm *gtm) |
| 474 | { |
Tejun Heo | a0f79b9 | 2007-12-18 16:33:05 +0900 | [diff] [blame] | 475 | unsigned long xfer_mask = 0; |
| 476 | unsigned int type; |
| 477 | int unit; |
| 478 | u8 mode; |
Tejun Heo | 7c77fa4 | 2007-12-18 16:33:03 +0900 | [diff] [blame] | 479 | |
| 480 | /* we always use the 0 slot for crap hardware */ |
| 481 | unit = dev->devno; |
| 482 | if (!(gtm->flags & 0x10)) |
| 483 | unit = 0; |
| 484 | |
Tejun Heo | a0f79b9 | 2007-12-18 16:33:05 +0900 | [diff] [blame] | 485 | /* PIO */ |
| 486 | mode = ata_timing_cycle2mode(ATA_SHIFT_PIO, gtm->drive[unit].pio); |
| 487 | xfer_mask |= ata_xfer_mode2mask(mode); |
Tejun Heo | 7c77fa4 | 2007-12-18 16:33:03 +0900 | [diff] [blame] | 488 | |
| 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. |
| 492 | */ |
Tejun Heo | a0f79b9 | 2007-12-18 16:33:05 +0900 | [diff] [blame] | 493 | if (!(gtm->flags & (1 << (2 * unit)))) |
| 494 | type = ATA_SHIFT_MWDMA; |
| 495 | else |
| 496 | type = ATA_SHIFT_UDMA; |
Tejun Heo | 7c77fa4 | 2007-12-18 16:33:03 +0900 | [diff] [blame] | 497 | |
Tejun Heo | a0f79b9 | 2007-12-18 16:33:05 +0900 | [diff] [blame] | 498 | mode = ata_timing_cycle2mode(type, gtm->drive[unit].dma); |
| 499 | xfer_mask |= ata_xfer_mode2mask(mode); |
| 500 | |
| 501 | return xfer_mask; |
Tejun Heo | 7c77fa4 | 2007-12-18 16:33:03 +0900 | [diff] [blame] | 502 | } |
| 503 | EXPORT_SYMBOL_GPL(ata_acpi_gtm_xfermask); |
| 504 | |
Kristen Carlson Accardi | 11ef697 | 2006-09-28 11:29:01 -0700 | [diff] [blame] | 505 | /** |
Alan Cox | e1ddb4b | 2007-08-16 02:33:36 -0400 | [diff] [blame] | 506 | * ata_acpi_cbl_80wire - Check for 80 wire cable |
| 507 | * @ap: Port to check |
Tejun Heo | 021ee9a | 2007-12-18 16:33:06 +0900 | [diff] [blame] | 508 | * @gtm: GTM data to use |
Alan Cox | e1ddb4b | 2007-08-16 02:33:36 -0400 | [diff] [blame] | 509 | * |
Tejun Heo | 021ee9a | 2007-12-18 16:33:06 +0900 | [diff] [blame] | 510 | * Return 1 if the @gtm indicates the BIOS selected an 80wire mode. |
Alan Cox | e1ddb4b | 2007-08-16 02:33:36 -0400 | [diff] [blame] | 511 | */ |
Tejun Heo | 021ee9a | 2007-12-18 16:33:06 +0900 | [diff] [blame] | 512 | int ata_acpi_cbl_80wire(struct ata_port *ap, const struct ata_acpi_gtm *gtm) |
Alan Cox | e1ddb4b | 2007-08-16 02:33:36 -0400 | [diff] [blame] | 513 | { |
Tejun Heo | 021ee9a | 2007-12-18 16:33:06 +0900 | [diff] [blame] | 514 | struct ata_device *dev; |
Jeff Garzik | 2dcb407 | 2007-10-19 06:42:56 -0400 | [diff] [blame] | 515 | |
Tejun Heo | 1eca436 | 2008-11-03 20:03:17 +0900 | [diff] [blame] | 516 | ata_for_each_dev(dev, &ap->link, ENABLED) { |
Tejun Heo | 021ee9a | 2007-12-18 16:33:06 +0900 | [diff] [blame] | 517 | unsigned long xfer_mask, udma_mask; |
Jeff Garzik | 2dcb407 | 2007-10-19 06:42:56 -0400 | [diff] [blame] | 518 | |
Tejun Heo | 021ee9a | 2007-12-18 16:33:06 +0900 | [diff] [blame] | 519 | xfer_mask = ata_acpi_gtm_xfermask(dev, gtm); |
| 520 | ata_unpack_xfermask(xfer_mask, NULL, NULL, &udma_mask); |
| 521 | |
| 522 | if (udma_mask & ~ATA_UDMA_MASK_40C) |
| 523 | return 1; |
| 524 | } |
| 525 | |
Alan Cox | e1ddb4b | 2007-08-16 02:33:36 -0400 | [diff] [blame] | 526 | return 0; |
| 527 | } |
Alan Cox | e1ddb4b | 2007-08-16 02:33:36 -0400 | [diff] [blame] | 528 | EXPORT_SYMBOL_GPL(ata_acpi_cbl_80wire); |
| 529 | |
Tejun Heo | 3264a8d | 2007-12-15 15:05:06 +0900 | [diff] [blame] | 530 | static void ata_acpi_gtf_to_tf(struct ata_device *dev, |
| 531 | const struct ata_acpi_gtf *gtf, |
| 532 | struct ata_taskfile *tf) |
| 533 | { |
| 534 | ata_tf_init(dev, tf); |
| 535 | |
| 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 */ |
| 545 | } |
| 546 | |
Tejun Heo | 110f66d | 2009-09-16 04:17:28 +0900 | [diff] [blame] | 547 | static int ata_acpi_filter_tf(struct ata_device *dev, |
| 548 | const struct ata_taskfile *tf, |
Tejun Heo | 3264a8d | 2007-12-15 15:05:06 +0900 | [diff] [blame] | 549 | const struct ata_taskfile *ptf) |
| 550 | { |
Tejun Heo | 110f66d | 2009-09-16 04:17:28 +0900 | [diff] [blame] | 551 | if (dev->gtf_filter & ATA_ACPI_FILTER_SETXFER) { |
Tejun Heo | 3264a8d | 2007-12-15 15:05:06 +0900 | [diff] [blame] | 552 | /* libata doesn't use ACPI to configure transfer mode. |
| 553 | * It will only confuse device configuration. Skip. |
| 554 | */ |
| 555 | if (tf->command == ATA_CMD_SET_FEATURES && |
| 556 | tf->feature == SETFEATURES_XFER) |
| 557 | return 1; |
| 558 | } |
| 559 | |
Tejun Heo | 110f66d | 2009-09-16 04:17:28 +0900 | [diff] [blame] | 560 | if (dev->gtf_filter & ATA_ACPI_FILTER_LOCK) { |
Tejun Heo | 3264a8d | 2007-12-15 15:05:06 +0900 | [diff] [blame] | 561 | /* BIOS writers, sorry but we don't wanna lock |
| 562 | * features unless the user explicitly said so. |
| 563 | */ |
| 564 | |
| 565 | /* DEVICE CONFIGURATION FREEZE LOCK */ |
| 566 | if (tf->command == ATA_CMD_CONF_OVERLAY && |
| 567 | tf->feature == ATA_DCO_FREEZE_LOCK) |
| 568 | return 1; |
| 569 | |
| 570 | /* SECURITY FREEZE LOCK */ |
| 571 | if (tf->command == ATA_CMD_SEC_FREEZE_LOCK) |
| 572 | return 1; |
| 573 | |
| 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; |
| 580 | } |
| 581 | |
Tejun Heo | fa5b561 | 2009-09-16 04:17:02 +0900 | [diff] [blame] | 582 | if (tf->command == ATA_CMD_SET_FEATURES && |
| 583 | tf->feature == SETFEATURES_SATA_ENABLE) { |
Tejun Heo | b344991 | 2008-07-06 23:15:03 +0900 | [diff] [blame] | 584 | /* inhibit enabling DIPM */ |
Tejun Heo | 110f66d | 2009-09-16 04:17:28 +0900 | [diff] [blame] | 585 | if (dev->gtf_filter & ATA_ACPI_FILTER_DIPM && |
Tejun Heo | b344991 | 2008-07-06 23:15:03 +0900 | [diff] [blame] | 586 | tf->nsect == SATA_DIPM) |
| 587 | return 1; |
Tejun Heo | fa5b561 | 2009-09-16 04:17:02 +0900 | [diff] [blame] | 588 | |
| 589 | /* inhibit FPDMA non-zero offset */ |
Tejun Heo | 110f66d | 2009-09-16 04:17:28 +0900 | [diff] [blame] | 590 | if (dev->gtf_filter & ATA_ACPI_FILTER_FPDMA_OFFSET && |
Tejun Heo | fa5b561 | 2009-09-16 04:17:02 +0900 | [diff] [blame] | 591 | (tf->nsect == SATA_FPDMA_OFFSET || |
| 592 | tf->nsect == SATA_FPDMA_IN_ORDER)) |
| 593 | return 1; |
| 594 | |
| 595 | /* inhibit FPDMA auto activation */ |
Tejun Heo | 110f66d | 2009-09-16 04:17:28 +0900 | [diff] [blame] | 596 | if (dev->gtf_filter & ATA_ACPI_FILTER_FPDMA_AA && |
Tejun Heo | fa5b561 | 2009-09-16 04:17:02 +0900 | [diff] [blame] | 597 | tf->nsect == SATA_FPDMA_AA) |
| 598 | return 1; |
Tejun Heo | b344991 | 2008-07-06 23:15:03 +0900 | [diff] [blame] | 599 | } |
| 600 | |
Tejun Heo | 3264a8d | 2007-12-15 15:05:06 +0900 | [diff] [blame] | 601 | return 0; |
| 602 | } |
| 603 | |
Alan Cox | e1ddb4b | 2007-08-16 02:33:36 -0400 | [diff] [blame] | 604 | /** |
Tejun Heo | 0e8634b | 2007-12-15 15:05:05 +0900 | [diff] [blame] | 605 | * ata_acpi_run_tf - send taskfile registers to host controller |
Tejun Heo | 3a32a8e | 2007-05-05 23:50:38 +0900 | [diff] [blame] | 606 | * @dev: target ATA device |
Kristen Carlson Accardi | 11ef697 | 2006-09-28 11:29:01 -0700 | [diff] [blame] | 607 | * @gtf: raw ATA taskfile register set (0x1f1 - 0x1f7) |
| 608 | * |
Sergei Shtylyov | 3696df3 | 2011-02-04 22:04:17 +0300 | [diff] [blame] | 609 | * Outputs ATA taskfile to standard ATA host controller. |
Kristen Carlson Accardi | 11ef697 | 2006-09-28 11:29:01 -0700 | [diff] [blame] | 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. |
| 613 | * |
| 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. |
| 618 | * |
Tejun Heo | 4700c4b | 2007-05-15 03:28:16 +0900 | [diff] [blame] | 619 | * LOCKING: |
| 620 | * EH context. |
| 621 | * |
| 622 | * RETURNS: |
Tejun Heo | 3264a8d | 2007-12-15 15:05:06 +0900 | [diff] [blame] | 623 | * 1 if command is executed successfully. 0 if ignored, rejected or |
| 624 | * filtered out, -errno on other errors. |
Kristen Carlson Accardi | 11ef697 | 2006-09-28 11:29:01 -0700 | [diff] [blame] | 625 | */ |
Tejun Heo | 0e8634b | 2007-12-15 15:05:05 +0900 | [diff] [blame] | 626 | static int ata_acpi_run_tf(struct ata_device *dev, |
Tejun Heo | 3264a8d | 2007-12-15 15:05:06 +0900 | [diff] [blame] | 627 | const struct ata_acpi_gtf *gtf, |
| 628 | const struct ata_acpi_gtf *prev_gtf) |
Kristen Carlson Accardi | 11ef697 | 2006-09-28 11:29:01 -0700 | [diff] [blame] | 629 | { |
Tejun Heo | 3264a8d | 2007-12-15 15:05:06 +0900 | [diff] [blame] | 630 | struct ata_taskfile *pptf = NULL; |
| 631 | struct ata_taskfile tf, ptf, rtf; |
Tejun Heo | 4700c4b | 2007-05-15 03:28:16 +0900 | [diff] [blame] | 632 | unsigned int err_mask; |
Tejun Heo | 0e8634b | 2007-12-15 15:05:05 +0900 | [diff] [blame] | 633 | const char *level; |
Robert Hancock | 6521148 | 2009-07-14 20:43:39 -0600 | [diff] [blame] | 634 | const char *descr; |
Tejun Heo | 0e8634b | 2007-12-15 15:05:05 +0900 | [diff] [blame] | 635 | char msg[60]; |
| 636 | int rc; |
Jeff Garzik | fc16c25 | 2007-02-24 21:05:01 -0500 | [diff] [blame] | 637 | |
Tejun Heo | 4700c4b | 2007-05-15 03:28:16 +0900 | [diff] [blame] | 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; |
Kristen Carlson Accardi | 11ef697 | 2006-09-28 11:29:01 -0700 | [diff] [blame] | 642 | |
Tejun Heo | 3264a8d | 2007-12-15 15:05:06 +0900 | [diff] [blame] | 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; |
| 647 | } |
Kristen Carlson Accardi | 11ef697 | 2006-09-28 11:29:01 -0700 | [diff] [blame] | 648 | |
Tejun Heo | 110f66d | 2009-09-16 04:17:28 +0900 | [diff] [blame] | 649 | if (!ata_acpi_filter_tf(dev, &tf, pptf)) { |
Tejun Heo | 3264a8d | 2007-12-15 15:05:06 +0900 | [diff] [blame] | 650 | rtf = tf; |
| 651 | err_mask = ata_exec_internal(dev, &rtf, NULL, |
| 652 | DMA_NONE, NULL, 0, 0); |
Kristen Carlson Accardi | 11ef697 | 2006-09-28 11:29:01 -0700 | [diff] [blame] | 653 | |
Tejun Heo | 3264a8d | 2007-12-15 15:05:06 +0900 | [diff] [blame] | 654 | switch (err_mask) { |
| 655 | case 0: |
| 656 | level = KERN_DEBUG; |
| 657 | snprintf(msg, sizeof(msg), "succeeded"); |
| 658 | rc = 1; |
| 659 | break; |
Tejun Heo | 0e8634b | 2007-12-15 15:05:05 +0900 | [diff] [blame] | 660 | |
Tejun Heo | 3264a8d | 2007-12-15 15:05:06 +0900 | [diff] [blame] | 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; |
Tejun Heo | 0e8634b | 2007-12-15 15:05:05 +0900 | [diff] [blame] | 668 | |
Tejun Heo | 3264a8d | 2007-12-15 15:05:06 +0900 | [diff] [blame] | 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; |
| 676 | } |
| 677 | } else { |
Tejun Heo | 0e8634b | 2007-12-15 15:05:05 +0900 | [diff] [blame] | 678 | level = KERN_INFO; |
Tejun Heo | 3264a8d | 2007-12-15 15:05:06 +0900 | [diff] [blame] | 679 | snprintf(msg, sizeof(msg), "filtered out"); |
Tejun Heo | 0e8634b | 2007-12-15 15:05:05 +0900 | [diff] [blame] | 680 | rc = 0; |
Tejun Heo | 4700c4b | 2007-05-15 03:28:16 +0900 | [diff] [blame] | 681 | } |
Robert Hancock | 6521148 | 2009-07-14 20:43:39 -0600 | [diff] [blame] | 682 | descr = ata_get_cmd_descript(tf.command); |
Tejun Heo | 4700c4b | 2007-05-15 03:28:16 +0900 | [diff] [blame] | 683 | |
Tejun Heo | 0e8634b | 2007-12-15 15:05:05 +0900 | [diff] [blame] | 684 | ata_dev_printk(dev, level, |
Robert Hancock | 6521148 | 2009-07-14 20:43:39 -0600 | [diff] [blame] | 685 | "ACPI cmd %02x/%02x:%02x:%02x:%02x:%02x:%02x (%s) %s\n", |
Tejun Heo | 0e8634b | 2007-12-15 15:05:05 +0900 | [diff] [blame] | 686 | tf.command, tf.feature, tf.nsect, tf.lbal, |
Robert Hancock | 6521148 | 2009-07-14 20:43:39 -0600 | [diff] [blame] | 687 | tf.lbam, tf.lbah, tf.device, |
| 688 | (descr ? descr : "unknown"), msg); |
Tejun Heo | 0e8634b | 2007-12-15 15:05:05 +0900 | [diff] [blame] | 689 | |
| 690 | return rc; |
Kristen Carlson Accardi | 11ef697 | 2006-09-28 11:29:01 -0700 | [diff] [blame] | 691 | } |
| 692 | |
| 693 | /** |
Kristen Carlson Accardi | 11ef697 | 2006-09-28 11:29:01 -0700 | [diff] [blame] | 694 | * ata_acpi_exec_tfs - get then write drive taskfile settings |
Tejun Heo | 6746544 | 2007-05-15 03:28:16 +0900 | [diff] [blame] | 695 | * @dev: target ATA device |
Martin Olsson | 98a1708 | 2009-04-22 18:21:29 +0200 | [diff] [blame] | 696 | * @nr_executed: out parameter for the number of executed commands |
Kristen Carlson Accardi | 11ef697 | 2006-09-28 11:29:01 -0700 | [diff] [blame] | 697 | * |
Martin Olsson | 98a1708 | 2009-04-22 18:21:29 +0200 | [diff] [blame] | 698 | * Evaluate _GTF and execute returned taskfiles. |
Tejun Heo | 69b16a5 | 2007-05-15 03:28:16 +0900 | [diff] [blame] | 699 | * |
| 700 | * LOCKING: |
| 701 | * EH context. |
| 702 | * |
| 703 | * RETURNS: |
Tejun Heo | 66fa7f2 | 2007-12-15 15:05:04 +0900 | [diff] [blame] | 704 | * Number of executed taskfiles on success, 0 if _GTF doesn't exist. |
| 705 | * -errno on other errors. |
Kristen Carlson Accardi | 11ef697 | 2006-09-28 11:29:01 -0700 | [diff] [blame] | 706 | */ |
Tejun Heo | 66fa7f2 | 2007-12-15 15:05:04 +0900 | [diff] [blame] | 707 | static int ata_acpi_exec_tfs(struct ata_device *dev, int *nr_executed) |
Kristen Carlson Accardi | 11ef697 | 2006-09-28 11:29:01 -0700 | [diff] [blame] | 708 | { |
Tejun Heo | 3264a8d | 2007-12-15 15:05:06 +0900 | [diff] [blame] | 709 | struct ata_acpi_gtf *gtf = NULL, *pgtf = NULL; |
Tejun Heo | 6746544 | 2007-05-15 03:28:16 +0900 | [diff] [blame] | 710 | int gtf_count, i, rc; |
Kristen Carlson Accardi | 11ef697 | 2006-09-28 11:29:01 -0700 | [diff] [blame] | 711 | |
Tejun Heo | 6746544 | 2007-05-15 03:28:16 +0900 | [diff] [blame] | 712 | /* get taskfiles */ |
Tejun Heo | 66fa7f2 | 2007-12-15 15:05:04 +0900 | [diff] [blame] | 713 | rc = ata_dev_get_GTF(dev, >f); |
| 714 | if (rc < 0) |
| 715 | return rc; |
| 716 | gtf_count = rc; |
Kristen Carlson Accardi | 11ef697 | 2006-09-28 11:29:01 -0700 | [diff] [blame] | 717 | |
Tejun Heo | 6746544 | 2007-05-15 03:28:16 +0900 | [diff] [blame] | 718 | /* execute them */ |
Tejun Heo | 3264a8d | 2007-12-15 15:05:06 +0900 | [diff] [blame] | 719 | for (i = 0; i < gtf_count; i++, gtf++) { |
| 720 | rc = ata_acpi_run_tf(dev, gtf, pgtf); |
Tejun Heo | 0e8634b | 2007-12-15 15:05:05 +0900 | [diff] [blame] | 721 | if (rc < 0) |
| 722 | break; |
Tejun Heo | 3264a8d | 2007-12-15 15:05:06 +0900 | [diff] [blame] | 723 | if (rc) { |
Tejun Heo | 0e8634b | 2007-12-15 15:05:05 +0900 | [diff] [blame] | 724 | (*nr_executed)++; |
Tejun Heo | 3264a8d | 2007-12-15 15:05:06 +0900 | [diff] [blame] | 725 | pgtf = gtf; |
| 726 | } |
Kristen Carlson Accardi | 11ef697 | 2006-09-28 11:29:01 -0700 | [diff] [blame] | 727 | } |
| 728 | |
Tejun Heo | 398e078 | 2007-12-15 15:05:03 +0900 | [diff] [blame] | 729 | ata_acpi_clear_gtf(dev); |
Tejun Heo | 6746544 | 2007-05-15 03:28:16 +0900 | [diff] [blame] | 730 | |
Tejun Heo | 0e8634b | 2007-12-15 15:05:05 +0900 | [diff] [blame] | 731 | if (rc < 0) |
| 732 | return rc; |
| 733 | return 0; |
Kristen Carlson Accardi | 11ef697 | 2006-09-28 11:29:01 -0700 | [diff] [blame] | 734 | } |
| 735 | |
Kristen Carlson Accardi | 7ea1fbc | 2006-09-28 11:29:12 -0700 | [diff] [blame] | 736 | /** |
| 737 | * ata_acpi_push_id - send Identify data to drive |
Tejun Heo | 3a32a8e | 2007-05-05 23:50:38 +0900 | [diff] [blame] | 738 | * @dev: target ATA device |
Kristen Carlson Accardi | 7ea1fbc | 2006-09-28 11:29:12 -0700 | [diff] [blame] | 739 | * |
| 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. |
Tejun Heo | 69b16a5 | 2007-05-15 03:28:16 +0900 | [diff] [blame] | 745 | * |
| 746 | * LOCKING: |
| 747 | * EH context. |
| 748 | * |
| 749 | * RETURNS: |
Tejun Heo | f240677 | 2009-11-18 22:24:21 +0900 | [diff] [blame] | 750 | * 0 on success, -ENOENT if _SDD doesn't exist, -errno on failure. |
Kristen Carlson Accardi | 7ea1fbc | 2006-09-28 11:29:12 -0700 | [diff] [blame] | 751 | */ |
Tejun Heo | 6746544 | 2007-05-15 03:28:16 +0900 | [diff] [blame] | 752 | static int ata_acpi_push_id(struct ata_device *dev) |
Kristen Carlson Accardi | 7ea1fbc | 2006-09-28 11:29:12 -0700 | [diff] [blame] | 753 | { |
Tejun Heo | 9af5c9c | 2007-08-06 18:36:22 +0900 | [diff] [blame] | 754 | struct ata_port *ap = dev->link->ap; |
Tejun Heo | 3a32a8e | 2007-05-05 23:50:38 +0900 | [diff] [blame] | 755 | acpi_status status; |
| 756 | struct acpi_object_list input; |
| 757 | union acpi_object in_params[1]; |
Kristen Carlson Accardi | 7ea1fbc | 2006-09-28 11:29:12 -0700 | [diff] [blame] | 758 | |
Kristen Carlson Accardi | 7ea1fbc | 2006-09-28 11:29:12 -0700 | [diff] [blame] | 759 | if (ata_msg_probe(ap)) |
Joe Perches | a9a79df | 2011-04-15 15:51:59 -0700 | [diff] [blame] | 760 | ata_dev_dbg(dev, "%s: ix = %d, port#: %d\n", |
| 761 | __func__, dev->devno, ap->port_no); |
Kristen Carlson Accardi | 7ea1fbc | 2006-09-28 11:29:12 -0700 | [diff] [blame] | 762 | |
Kristen Carlson Accardi | 7ea1fbc | 2006-09-28 11:29:12 -0700 | [diff] [blame] | 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; |
Tejun Heo | 3a32a8e | 2007-05-05 23:50:38 +0900 | [diff] [blame] | 768 | in_params[0].buffer.length = sizeof(dev->id[0]) * ATA_ID_WORDS; |
| 769 | in_params[0].buffer.pointer = (u8 *)dev->id; |
Kristen Carlson Accardi | 7ea1fbc | 2006-09-28 11:29:12 -0700 | [diff] [blame] | 770 | /* Output buffer: _SDD has no output */ |
| 771 | |
| 772 | /* It's OK for _SDD to be missing too. */ |
Tejun Heo | 3a32a8e | 2007-05-05 23:50:38 +0900 | [diff] [blame] | 773 | swap_buf_le16(dev->id, ATA_ID_WORDS); |
Matthew Garrett | 30dcf76 | 2012-06-25 16:13:04 +0800 | [diff] [blame] | 774 | status = acpi_evaluate_object(ata_dev_acpi_handle(dev), "_SDD", &input, |
| 775 | NULL); |
Tejun Heo | 3a32a8e | 2007-05-05 23:50:38 +0900 | [diff] [blame] | 776 | swap_buf_le16(dev->id, ATA_ID_WORDS); |
Kristen Carlson Accardi | 7ea1fbc | 2006-09-28 11:29:12 -0700 | [diff] [blame] | 777 | |
Tejun Heo | f240677 | 2009-11-18 22:24:21 +0900 | [diff] [blame] | 778 | if (status == AE_NOT_FOUND) |
| 779 | return -ENOENT; |
| 780 | |
| 781 | if (ACPI_FAILURE(status)) { |
Joe Perches | a9a79df | 2011-04-15 15:51:59 -0700 | [diff] [blame] | 782 | ata_dev_warn(dev, "ACPI _SDD failed (AE 0x%x)\n", status); |
Tejun Heo | f240677 | 2009-11-18 22:24:21 +0900 | [diff] [blame] | 783 | return -EIO; |
| 784 | } |
Kristen Carlson Accardi | 7ea1fbc | 2006-09-28 11:29:12 -0700 | [diff] [blame] | 785 | |
Tejun Heo | f240677 | 2009-11-18 22:24:21 +0900 | [diff] [blame] | 786 | return 0; |
Kristen Carlson Accardi | 7ea1fbc | 2006-09-28 11:29:12 -0700 | [diff] [blame] | 787 | } |
| 788 | |
Tejun Heo | 6746544 | 2007-05-15 03:28:16 +0900 | [diff] [blame] | 789 | /** |
Tejun Heo | 64578a3 | 2007-05-15 03:28:16 +0900 | [diff] [blame] | 790 | * ata_acpi_on_suspend - ATA ACPI hook called on suspend |
| 791 | * @ap: target ATA port |
| 792 | * |
| 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. |
| 797 | * |
| 798 | * LOCKING: |
| 799 | * EH context. |
| 800 | * |
| 801 | * RETURNS: |
| 802 | * 0 on success, -errno on failure. |
| 803 | */ |
| 804 | int ata_acpi_on_suspend(struct ata_port *ap) |
| 805 | { |
Tejun Heo | c05e6ff | 2007-12-15 15:05:02 +0900 | [diff] [blame] | 806 | /* nada */ |
| 807 | return 0; |
Tejun Heo | 64578a3 | 2007-05-15 03:28:16 +0900 | [diff] [blame] | 808 | } |
| 809 | |
| 810 | /** |
Tejun Heo | 6746544 | 2007-05-15 03:28:16 +0900 | [diff] [blame] | 811 | * ata_acpi_on_resume - ATA ACPI hook called on resume |
| 812 | * @ap: target ATA port |
| 813 | * |
| 814 | * This function is called when @ap is resumed - right after port |
| 815 | * itself is resumed but before any EH action is taken. |
| 816 | * |
| 817 | * LOCKING: |
| 818 | * EH context. |
| 819 | */ |
| 820 | void ata_acpi_on_resume(struct ata_port *ap) |
| 821 | { |
Tejun Heo | c05e6ff | 2007-12-15 15:05:02 +0900 | [diff] [blame] | 822 | const struct ata_acpi_gtm *gtm = ata_acpi_init_gtm(ap); |
Tejun Heo | f58229f | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 823 | struct ata_device *dev; |
Kristen Carlson Accardi | 7ea1fbc | 2006-09-28 11:29:12 -0700 | [diff] [blame] | 824 | |
Aaron Lu | f1bc1e4 | 2013-08-23 10:17:54 +0800 | [diff] [blame] | 825 | if (ACPI_HANDLE(&ap->tdev) && gtm) { |
Tejun Heo | 398e078 | 2007-12-15 15:05:03 +0900 | [diff] [blame] | 826 | /* _GTM valid */ |
| 827 | |
| 828 | /* restore timing parameters */ |
Tejun Heo | c05e6ff | 2007-12-15 15:05:02 +0900 | [diff] [blame] | 829 | ata_acpi_stm(ap, gtm); |
Tejun Heo | 64578a3 | 2007-05-15 03:28:16 +0900 | [diff] [blame] | 830 | |
Tejun Heo | 398e078 | 2007-12-15 15:05:03 +0900 | [diff] [blame] | 831 | /* _GTF should immediately follow _STM so that it can |
| 832 | * use values set by _STM. Cache _GTF result and |
| 833 | * schedule _GTF. |
| 834 | */ |
Tejun Heo | 1eca436 | 2008-11-03 20:03:17 +0900 | [diff] [blame] | 835 | ata_for_each_dev(dev, &ap->link, ALL) { |
Tejun Heo | 398e078 | 2007-12-15 15:05:03 +0900 | [diff] [blame] | 836 | ata_acpi_clear_gtf(dev); |
Shaohua Li | 48feb3c | 2008-03-25 16:50:45 +0800 | [diff] [blame] | 837 | if (ata_dev_enabled(dev) && |
| 838 | ata_dev_get_GTF(dev, NULL) >= 0) |
Tejun Heo | 398e078 | 2007-12-15 15:05:03 +0900 | [diff] [blame] | 839 | dev->flags |= ATA_DFLAG_ACPI_PENDING; |
| 840 | } |
| 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. |
| 845 | */ |
Tejun Heo | 1eca436 | 2008-11-03 20:03:17 +0900 | [diff] [blame] | 846 | ata_for_each_dev(dev, &ap->link, ALL) { |
Tejun Heo | 398e078 | 2007-12-15 15:05:03 +0900 | [diff] [blame] | 847 | ata_acpi_clear_gtf(dev); |
Shaohua Li | 48feb3c | 2008-03-25 16:50:45 +0800 | [diff] [blame] | 848 | if (ata_dev_enabled(dev)) |
| 849 | dev->flags |= ATA_DFLAG_ACPI_PENDING; |
Tejun Heo | 398e078 | 2007-12-15 15:05:03 +0900 | [diff] [blame] | 850 | } |
| 851 | } |
Tejun Heo | 6746544 | 2007-05-15 03:28:16 +0900 | [diff] [blame] | 852 | } |
| 853 | |
Aaron Lu | a7ff60d | 2013-01-25 14:29:35 +0800 | [diff] [blame] | 854 | static int ata_acpi_choose_suspend_state(struct ata_device *dev, bool runtime) |
Shaohua Li | bd3adca | 2007-11-02 09:32:38 +0800 | [diff] [blame] | 855 | { |
Aaron Lu | 2133420 | 2013-01-15 17:21:01 +0800 | [diff] [blame] | 856 | int d_max_in = ACPI_STATE_D3_COLD; |
Aaron Lu | a7ff60d | 2013-01-25 14:29:35 +0800 | [diff] [blame] | 857 | if (!runtime) |
| 858 | goto out; |
Aaron Lu | 2133420 | 2013-01-15 17:21:01 +0800 | [diff] [blame] | 859 | |
| 860 | /* |
| 861 | * For ATAPI, runtime D3 cold is only allowed |
| 862 | * for ZPODD in zero power ready state |
| 863 | */ |
| 864 | if (dev->class == ATA_DEV_ATAPI && |
| 865 | !(zpodd_dev_enabled(dev) && zpodd_zpready(dev))) |
| 866 | d_max_in = ACPI_STATE_D3_HOT; |
| 867 | |
Aaron Lu | a7ff60d | 2013-01-25 14:29:35 +0800 | [diff] [blame] | 868 | out: |
Aaron Lu | f1bc1e4 | 2013-08-23 10:17:54 +0800 | [diff] [blame] | 869 | return acpi_pm_device_sleep_state(&dev->tdev, NULL, d_max_in); |
Aaron Lu | 2133420 | 2013-01-15 17:21:01 +0800 | [diff] [blame] | 870 | } |
| 871 | |
Aaron Lu | a7ff60d | 2013-01-25 14:29:35 +0800 | [diff] [blame] | 872 | static void sata_acpi_set_state(struct ata_port *ap, pm_message_t state) |
| 873 | { |
| 874 | bool runtime = PMSG_IS_AUTO(state); |
Shaohua Li | bd3adca | 2007-11-02 09:32:38 +0800 | [diff] [blame] | 875 | struct ata_device *dev; |
Lin Ming | febe53b | 2012-06-25 16:13:05 +0800 | [diff] [blame] | 876 | acpi_handle handle; |
Lin Ming | 3bd4660 | 2012-06-25 16:13:06 +0800 | [diff] [blame] | 877 | int acpi_state; |
Shaohua Li | bd3adca | 2007-11-02 09:32:38 +0800 | [diff] [blame] | 878 | |
Tejun Heo | 1eca436 | 2008-11-03 20:03:17 +0900 | [diff] [blame] | 879 | ata_for_each_dev(dev, &ap->link, ENABLED) { |
Lin Ming | febe53b | 2012-06-25 16:13:05 +0800 | [diff] [blame] | 880 | handle = ata_dev_acpi_handle(dev); |
Lin Ming | 3bd4660 | 2012-06-25 16:13:06 +0800 | [diff] [blame] | 881 | if (!handle) |
| 882 | continue; |
| 883 | |
Aaron Lu | a7ff60d | 2013-01-25 14:29:35 +0800 | [diff] [blame] | 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); |
Lin Ming | 3bd4660 | 2012-06-25 16:13:06 +0800 | [diff] [blame] | 892 | } else { |
Aaron Lu | a7ff60d | 2013-01-25 14:29:35 +0800 | [diff] [blame] | 893 | if (runtime && zpodd_dev_enabled(dev)) |
| 894 | zpodd_disable_run_wake(dev); |
Lin Ming | 3bd4660 | 2012-06-25 16:13:06 +0800 | [diff] [blame] | 895 | acpi_bus_set_power(handle, ACPI_STATE_D0); |
| 896 | } |
Shaohua Li | bd3adca | 2007-11-02 09:32:38 +0800 | [diff] [blame] | 897 | } |
Aaron Lu | a7ff60d | 2013-01-25 14:29:35 +0800 | [diff] [blame] | 898 | } |
Lin Ming | febe53b | 2012-06-25 16:13:05 +0800 | [diff] [blame] | 899 | |
Aaron Lu | a7ff60d | 2013-01-25 14:29:35 +0800 | [diff] [blame] | 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) |
| 902 | { |
| 903 | struct ata_device *dev; |
| 904 | acpi_handle port_handle; |
| 905 | |
Aaron Lu | f1bc1e4 | 2013-08-23 10:17:54 +0800 | [diff] [blame] | 906 | port_handle = ACPI_HANDLE(&ap->tdev); |
Aaron Lu | a7ff60d | 2013-01-25 14:29:35 +0800 | [diff] [blame] | 907 | if (!port_handle) |
| 908 | return; |
| 909 | |
| 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); |
| 914 | |
| 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; |
| 919 | |
| 920 | acpi_bus_set_power(dev_handle, state.event & PM_EVENT_RESUME ? |
Rafael J. Wysocki | 8ad928d | 2013-07-30 14:36:20 +0200 | [diff] [blame] | 921 | ACPI_STATE_D0 : ACPI_STATE_D3_COLD); |
Aaron Lu | a7ff60d | 2013-01-25 14:29:35 +0800 | [diff] [blame] | 922 | } |
| 923 | |
| 924 | if (!(state.event & PM_EVENT_RESUME)) |
Rafael J. Wysocki | 8ad928d | 2013-07-30 14:36:20 +0200 | [diff] [blame] | 925 | acpi_bus_set_power(port_handle, ACPI_STATE_D3_COLD); |
Aaron Lu | a7ff60d | 2013-01-25 14:29:35 +0800 | [diff] [blame] | 926 | } |
| 927 | |
Kristen Carlson Accardi | 11ef697 | 2006-09-28 11:29:01 -0700 | [diff] [blame] | 928 | /** |
| 929 | * ata_acpi_set_state - set the port power state |
| 930 | * @ap: target ATA port |
| 931 | * @state: state, on/off |
| 932 | * |
Aaron Lu | a7ff60d | 2013-01-25 14:29:35 +0800 | [diff] [blame] | 933 | * This function sets a proper ACPI D state for the device on |
| 934 | * system and runtime PM operations. |
Kristen Carlson Accardi | 11ef697 | 2006-09-28 11:29:01 -0700 | [diff] [blame] | 935 | */ |
| 936 | void ata_acpi_set_state(struct ata_port *ap, pm_message_t state) |
| 937 | { |
Aaron Lu | a7ff60d | 2013-01-25 14:29:35 +0800 | [diff] [blame] | 938 | if (ap->flags & ATA_FLAG_ACPI_SATA) |
| 939 | sata_acpi_set_state(ap, state); |
| 940 | else |
| 941 | pata_acpi_set_state(ap, state); |
Shaohua Li | bd3adca | 2007-11-02 09:32:38 +0800 | [diff] [blame] | 942 | } |
| 943 | |
| 944 | /** |
Tejun Heo | 6746544 | 2007-05-15 03:28:16 +0900 | [diff] [blame] | 945 | * ata_acpi_on_devcfg - ATA ACPI hook called on device donfiguration |
| 946 | * @dev: target ATA device |
| 947 | * |
| 948 | * This function is called when @dev is about to be configured. |
| 949 | * IDENTIFY data might have been modified after this hook is run. |
| 950 | * |
| 951 | * LOCKING: |
| 952 | * EH context. |
| 953 | * |
| 954 | * RETURNS: |
| 955 | * Positive number if IDENTIFY data needs to be refreshed, 0 if not, |
| 956 | * -errno on failure. |
| 957 | */ |
| 958 | int ata_acpi_on_devcfg(struct ata_device *dev) |
| 959 | { |
Tejun Heo | 9af5c9c | 2007-08-06 18:36:22 +0900 | [diff] [blame] | 960 | struct ata_port *ap = dev->link->ap; |
| 961 | struct ata_eh_context *ehc = &ap->link.eh_context; |
Tejun Heo | 6746544 | 2007-05-15 03:28:16 +0900 | [diff] [blame] | 962 | int acpi_sata = ap->flags & ATA_FLAG_ACPI_SATA; |
Tejun Heo | 66fa7f2 | 2007-12-15 15:05:04 +0900 | [diff] [blame] | 963 | int nr_executed = 0; |
Tejun Heo | 6746544 | 2007-05-15 03:28:16 +0900 | [diff] [blame] | 964 | int rc; |
| 965 | |
Matthew Garrett | 30dcf76 | 2012-06-25 16:13:04 +0800 | [diff] [blame] | 966 | if (!ata_dev_acpi_handle(dev)) |
Tejun Heo | 6746544 | 2007-05-15 03:28:16 +0900 | [diff] [blame] | 967 | return 0; |
| 968 | |
| 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; |
| 973 | |
| 974 | /* do _SDD if SATA */ |
| 975 | if (acpi_sata) { |
| 976 | rc = ata_acpi_push_id(dev); |
Tejun Heo | f240677 | 2009-11-18 22:24:21 +0900 | [diff] [blame] | 977 | if (rc && rc != -ENOENT) |
Tejun Heo | 6746544 | 2007-05-15 03:28:16 +0900 | [diff] [blame] | 978 | goto acpi_err; |
| 979 | } |
| 980 | |
| 981 | /* do _GTF */ |
Tejun Heo | 66fa7f2 | 2007-12-15 15:05:04 +0900 | [diff] [blame] | 982 | rc = ata_acpi_exec_tfs(dev, &nr_executed); |
| 983 | if (rc) |
Tejun Heo | 6746544 | 2007-05-15 03:28:16 +0900 | [diff] [blame] | 984 | goto acpi_err; |
| 985 | |
| 986 | dev->flags &= ~ATA_DFLAG_ACPI_PENDING; |
| 987 | |
| 988 | /* refresh IDENTIFY page if any _GTF command has been executed */ |
Tejun Heo | 66fa7f2 | 2007-12-15 15:05:04 +0900 | [diff] [blame] | 989 | if (nr_executed) { |
Tejun Heo | 6746544 | 2007-05-15 03:28:16 +0900 | [diff] [blame] | 990 | rc = ata_dev_reread_id(dev, 0); |
| 991 | if (rc < 0) { |
Joe Perches | a9a79df | 2011-04-15 15:51:59 -0700 | [diff] [blame] | 992 | ata_dev_err(dev, |
| 993 | "failed to IDENTIFY after ACPI commands\n"); |
Tejun Heo | 6746544 | 2007-05-15 03:28:16 +0900 | [diff] [blame] | 994 | return rc; |
| 995 | } |
| 996 | } |
| 997 | |
| 998 | return 0; |
| 999 | |
| 1000 | acpi_err: |
Tejun Heo | 66fa7f2 | 2007-12-15 15:05:04 +0900 | [diff] [blame] | 1001 | /* ignore evaluation failure if we can continue safely */ |
| 1002 | if (rc == -EINVAL && !nr_executed && !(ap->pflags & ATA_PFLAG_FROZEN)) |
| 1003 | return 0; |
Tejun Heo | 6746544 | 2007-05-15 03:28:16 +0900 | [diff] [blame] | 1004 | |
Tejun Heo | 66fa7f2 | 2007-12-15 15:05:04 +0900 | [diff] [blame] | 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; |
Tejun Heo | 6746544 | 2007-05-15 03:28:16 +0900 | [diff] [blame] | 1009 | } |
Tejun Heo | 66fa7f2 | 2007-12-15 15:05:04 +0900 | [diff] [blame] | 1010 | |
Aaron Lu | 0d0cdb0 | 2012-11-26 13:55:25 +0800 | [diff] [blame] | 1011 | dev->flags |= ATA_DFLAG_ACPI_DISABLED; |
Joe Perches | a9a79df | 2011-04-15 15:51:59 -0700 | [diff] [blame] | 1012 | ata_dev_warn(dev, "ACPI: failed the second time, disabled\n"); |
Tejun Heo | 66fa7f2 | 2007-12-15 15:05:04 +0900 | [diff] [blame] | 1013 | |
| 1014 | /* We can safely continue if no _GTF command has been executed |
| 1015 | * and port is not frozen. |
| 1016 | */ |
| 1017 | if (!nr_executed && !(ap->pflags & ATA_PFLAG_FROZEN)) |
| 1018 | return 0; |
| 1019 | |
Tejun Heo | 6746544 | 2007-05-15 03:28:16 +0900 | [diff] [blame] | 1020 | return rc; |
| 1021 | } |
Tejun Heo | 562f0c2 | 2007-12-15 15:05:01 +0900 | [diff] [blame] | 1022 | |
| 1023 | /** |
| 1024 | * ata_acpi_on_disable - ATA ACPI hook called when a device is disabled |
| 1025 | * @dev: target ATA device |
| 1026 | * |
| 1027 | * This function is called when @dev is about to be disabled. |
| 1028 | * |
| 1029 | * LOCKING: |
| 1030 | * EH context. |
| 1031 | */ |
| 1032 | void ata_acpi_on_disable(struct ata_device *dev) |
| 1033 | { |
Tejun Heo | 398e078 | 2007-12-15 15:05:03 +0900 | [diff] [blame] | 1034 | ata_acpi_clear_gtf(dev); |
Tejun Heo | 562f0c2 | 2007-12-15 15:05:01 +0900 | [diff] [blame] | 1035 | } |