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> |
Aaron Lu | a59b9aa | 2013-01-15 17:21:02 +0800 | [diff] [blame] | 20 | #include <linux/pm_qos.h> |
Matthew Garrett | 237d844 | 2007-10-03 01:24:16 +0100 | [diff] [blame] | 21 | #include <scsi/scsi_device.h> |
Kristen Carlson Accardi | 11ef697 | 2006-09-28 11:29:01 -0700 | [diff] [blame] | 22 | #include "libata.h" |
| 23 | |
| 24 | #include <acpi/acpi_bus.h> |
Kristen Carlson Accardi | 11ef697 | 2006-09-28 11:29:01 -0700 | [diff] [blame] | 25 | |
Tejun Heo | 110f66d | 2009-09-16 04:17:28 +0900 | [diff] [blame] | 26 | unsigned int ata_acpi_gtf_filter = ATA_ACPI_FILTER_DEFAULT; |
Tejun Heo | 3264a8d | 2007-12-15 15:05:06 +0900 | [diff] [blame] | 27 | module_param_named(acpi_gtf_filter, ata_acpi_gtf_filter, int, 0644); |
Tejun Heo | fa5b561 | 2009-09-16 04:17:02 +0900 | [diff] [blame] | 28 | 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] | 29 | |
Kristen Carlson Accardi | 11ef697 | 2006-09-28 11:29:01 -0700 | [diff] [blame] | 30 | #define NO_PORT_MULT 0xffff |
Jeff Garzik | 2dcb407 | 2007-10-19 06:42:56 -0400 | [diff] [blame] | 31 | #define SATA_ADR(root, pmp) (((root) << 16) | (pmp)) |
Kristen Carlson Accardi | 11ef697 | 2006-09-28 11:29:01 -0700 | [diff] [blame] | 32 | |
| 33 | #define REGS_PER_GTF 7 |
Tejun Heo | 4700c4b | 2007-05-15 03:28:16 +0900 | [diff] [blame] | 34 | struct ata_acpi_gtf { |
| 35 | u8 tf[REGS_PER_GTF]; /* regs. 0x1f1 - 0x1f7 */ |
| 36 | } __packed; |
Kristen Carlson Accardi | 11ef697 | 2006-09-28 11:29:01 -0700 | [diff] [blame] | 37 | |
Alan Cox | ca42663 | 2007-03-08 23:13:50 +0000 | [diff] [blame] | 38 | /* |
| 39 | * Helper - belongs in the PCI layer somewhere eventually |
| 40 | */ |
| 41 | static int is_pci_dev(struct device *dev) |
| 42 | { |
| 43 | return (dev->bus == &pci_bus_type); |
| 44 | } |
Kristen Carlson Accardi | 11ef697 | 2006-09-28 11:29:01 -0700 | [diff] [blame] | 45 | |
Tejun Heo | 398e078 | 2007-12-15 15:05:03 +0900 | [diff] [blame] | 46 | static void ata_acpi_clear_gtf(struct ata_device *dev) |
| 47 | { |
| 48 | kfree(dev->gtf_cache); |
| 49 | dev->gtf_cache = NULL; |
| 50 | } |
| 51 | |
Matthew Garrett | 30dcf76 | 2012-06-25 16:13:04 +0800 | [diff] [blame] | 52 | /** |
| 53 | * ata_ap_acpi_handle - provide the acpi_handle for an ata_port |
| 54 | * @ap: the acpi_handle returned will correspond to this port |
| 55 | * |
| 56 | * Returns the acpi_handle for the ACPI namespace object corresponding to |
| 57 | * the ata_port passed into the function, or NULL if no such object exists |
| 58 | */ |
| 59 | acpi_handle ata_ap_acpi_handle(struct ata_port *ap) |
Matthew Garrett | 6b66d95 | 2012-06-25 16:13:03 +0800 | [diff] [blame] | 60 | { |
| 61 | if (ap->flags & ATA_FLAG_ACPI_SATA) |
| 62 | return NULL; |
| 63 | |
Aaron Lu | 8340091 | 2012-08-15 17:08:12 +0800 | [diff] [blame] | 64 | return acpi_get_child(DEVICE_ACPI_HANDLE(ap->host->dev), ap->port_no); |
Matthew Garrett | 6b66d95 | 2012-06-25 16:13:03 +0800 | [diff] [blame] | 65 | } |
Matthew Garrett | 30dcf76 | 2012-06-25 16:13:04 +0800 | [diff] [blame] | 66 | EXPORT_SYMBOL(ata_ap_acpi_handle); |
Matthew Garrett | 6b66d95 | 2012-06-25 16:13:03 +0800 | [diff] [blame] | 67 | |
Matthew Garrett | 30dcf76 | 2012-06-25 16:13:04 +0800 | [diff] [blame] | 68 | /** |
| 69 | * ata_dev_acpi_handle - provide the acpi_handle for an ata_device |
| 70 | * @dev: the acpi_device returned will correspond to this port |
| 71 | * |
| 72 | * Returns the acpi_handle for the ACPI namespace object corresponding to |
| 73 | * the ata_device passed into the function, or NULL if no such object exists |
| 74 | */ |
| 75 | acpi_handle ata_dev_acpi_handle(struct ata_device *dev) |
Matthew Garrett | 6b66d95 | 2012-06-25 16:13:03 +0800 | [diff] [blame] | 76 | { |
| 77 | acpi_integer adr; |
| 78 | struct ata_port *ap = dev->link->ap; |
| 79 | |
Aaron Lu | 0d0cdb0 | 2012-11-26 13:55:25 +0800 | [diff] [blame] | 80 | if (dev->flags & ATA_DFLAG_ACPI_DISABLED) |
| 81 | return NULL; |
| 82 | |
Matthew Garrett | 6b66d95 | 2012-06-25 16:13:03 +0800 | [diff] [blame] | 83 | if (ap->flags & ATA_FLAG_ACPI_SATA) { |
| 84 | if (!sata_pmp_attached(ap)) |
| 85 | adr = SATA_ADR(ap->port_no, NO_PORT_MULT); |
| 86 | else |
| 87 | adr = SATA_ADR(ap->port_no, dev->link->pmp); |
| 88 | return acpi_get_child(DEVICE_ACPI_HANDLE(ap->host->dev), adr); |
| 89 | } else |
Matthew Garrett | 30dcf76 | 2012-06-25 16:13:04 +0800 | [diff] [blame] | 90 | return acpi_get_child(ata_ap_acpi_handle(ap), dev->devno); |
Matthew Garrett | 6b66d95 | 2012-06-25 16:13:03 +0800 | [diff] [blame] | 91 | } |
Matthew Garrett | 30dcf76 | 2012-06-25 16:13:04 +0800 | [diff] [blame] | 92 | EXPORT_SYMBOL(ata_dev_acpi_handle); |
Kristen Carlson Accardi | 11ef697 | 2006-09-28 11:29:01 -0700 | [diff] [blame] | 93 | |
Holger Macht | 664d080 | 2008-06-03 20:27:59 +0200 | [diff] [blame] | 94 | /* @ap and @dev are the same as ata_acpi_handle_hotplug() */ |
| 95 | static void ata_acpi_detach_device(struct ata_port *ap, struct ata_device *dev) |
| 96 | { |
| 97 | if (dev) |
| 98 | dev->flags |= ATA_DFLAG_DETACH; |
| 99 | else { |
| 100 | struct ata_link *tlink; |
| 101 | struct ata_device *tdev; |
| 102 | |
Tejun Heo | 1eca436 | 2008-11-03 20:03:17 +0900 | [diff] [blame] | 103 | ata_for_each_link(tlink, ap, EDGE) |
| 104 | ata_for_each_dev(tdev, tlink, ALL) |
Holger Macht | 664d080 | 2008-06-03 20:27:59 +0200 | [diff] [blame] | 105 | tdev->flags |= ATA_DFLAG_DETACH; |
| 106 | } |
| 107 | |
| 108 | ata_port_schedule_eh(ap); |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * ata_acpi_handle_hotplug - ACPI event handler backend |
| 113 | * @ap: ATA port ACPI event occurred |
| 114 | * @dev: ATA device ACPI event occurred (can be NULL) |
| 115 | * @event: ACPI event which occurred |
Holger Macht | 664d080 | 2008-06-03 20:27:59 +0200 | [diff] [blame] | 116 | * |
| 117 | * All ACPI bay / device realted events end up in this function. If |
| 118 | * the event is port-wide @dev is NULL. If the event is specific to a |
| 119 | * device, @dev points to it. |
| 120 | * |
| 121 | * Hotplug (as opposed to unplug) notification is always handled as |
| 122 | * port-wide while unplug only kills the target device on device-wide |
| 123 | * event. |
| 124 | * |
| 125 | * LOCKING: |
| 126 | * ACPI notify handler context. May sleep. |
| 127 | */ |
| 128 | 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] | 129 | u32 event) |
Matthew Garrett | 237d844 | 2007-10-03 01:24:16 +0100 | [diff] [blame] | 130 | { |
Holger Macht | 664d080 | 2008-06-03 20:27:59 +0200 | [diff] [blame] | 131 | struct ata_eh_info *ehi = &ap->link.eh_info; |
Tejun Heo | 233f112 | 2008-03-12 14:24:43 +0900 | [diff] [blame] | 132 | int wait = 0; |
| 133 | unsigned long flags; |
Zhang Rui | 3c1e389 | 2008-07-11 09:42:03 -0400 | [diff] [blame] | 134 | |
Holger Macht | 664d080 | 2008-06-03 20:27:59 +0200 | [diff] [blame] | 135 | spin_lock_irqsave(ap->lock, flags); |
Shaohua Li | f730ae1 | 2008-08-28 10:05:45 +0800 | [diff] [blame] | 136 | /* |
| 137 | * When dock driver calls into the routine, it will always use |
| 138 | * ACPI_NOTIFY_BUS_CHECK/ACPI_NOTIFY_DEVICE_CHECK for add and |
| 139 | * ACPI_NOTIFY_EJECT_REQUEST for remove |
| 140 | */ |
Tejun Heo | 233f112 | 2008-03-12 14:24:43 +0900 | [diff] [blame] | 141 | switch (event) { |
| 142 | case ACPI_NOTIFY_BUS_CHECK: |
| 143 | case ACPI_NOTIFY_DEVICE_CHECK: |
| 144 | ata_ehi_push_desc(ehi, "ACPI event"); |
Jeff Garzik | c85665f | 2008-05-19 17:56:10 -0400 | [diff] [blame] | 145 | |
Shaohua Li | f730ae1 | 2008-08-28 10:05:45 +0800 | [diff] [blame] | 146 | ata_ehi_hotplugged(ehi); |
| 147 | ata_port_freeze(ap); |
Holger Macht | 664d080 | 2008-06-03 20:27:59 +0200 | [diff] [blame] | 148 | break; |
| 149 | case ACPI_NOTIFY_EJECT_REQUEST: |
| 150 | ata_ehi_push_desc(ehi, "ACPI event"); |
| 151 | |
Holger Macht | 664d080 | 2008-06-03 20:27:59 +0200 | [diff] [blame] | 152 | ata_acpi_detach_device(ap, dev); |
| 153 | wait = 1; |
| 154 | break; |
Matthew Garrett | 237d844 | 2007-10-03 01:24:16 +0100 | [diff] [blame] | 155 | } |
| 156 | |
Matthew Garrett | ae6c23c | 2008-05-19 17:29:34 +0100 | [diff] [blame] | 157 | spin_unlock_irqrestore(ap->lock, flags); |
| 158 | |
Shaohua Li | f730ae1 | 2008-08-28 10:05:45 +0800 | [diff] [blame] | 159 | if (wait) |
Matthew Garrett | ae6c23c | 2008-05-19 17:29:34 +0100 | [diff] [blame] | 160 | ata_port_wait_eh(ap); |
Holger Macht | 664d080 | 2008-06-03 20:27:59 +0200 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | static void ata_acpi_dev_notify_dock(acpi_handle handle, u32 event, void *data) |
| 164 | { |
| 165 | struct ata_device *dev = data; |
| 166 | |
Shaohua Li | f730ae1 | 2008-08-28 10:05:45 +0800 | [diff] [blame] | 167 | ata_acpi_handle_hotplug(dev->link->ap, dev, event); |
Holger Macht | 664d080 | 2008-06-03 20:27:59 +0200 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | static void ata_acpi_ap_notify_dock(acpi_handle handle, u32 event, void *data) |
| 171 | { |
| 172 | struct ata_port *ap = data; |
| 173 | |
Shaohua Li | f730ae1 | 2008-08-28 10:05:45 +0800 | [diff] [blame] | 174 | ata_acpi_handle_hotplug(ap, NULL, event); |
Matthew Garrett | 237d844 | 2007-10-03 01:24:16 +0100 | [diff] [blame] | 175 | } |
| 176 | |
Shaohua Li | 1253f7a | 2008-08-28 10:06:16 +0800 | [diff] [blame] | 177 | static void ata_acpi_uevent(struct ata_port *ap, struct ata_device *dev, |
| 178 | u32 event) |
| 179 | { |
| 180 | struct kobject *kobj = NULL; |
| 181 | char event_string[20]; |
| 182 | char *envp[] = { event_string, NULL }; |
| 183 | |
| 184 | if (dev) { |
| 185 | if (dev->sdev) |
| 186 | kobj = &dev->sdev->sdev_gendev.kobj; |
| 187 | } else |
| 188 | kobj = &ap->dev->kobj; |
| 189 | |
| 190 | if (kobj) { |
| 191 | snprintf(event_string, 20, "BAY_EVENT=%d", event); |
| 192 | kobject_uevent_env(kobj, KOBJ_CHANGE, envp); |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | static void ata_acpi_ap_uevent(acpi_handle handle, u32 event, void *data) |
| 197 | { |
| 198 | ata_acpi_uevent(data, NULL, event); |
| 199 | } |
| 200 | |
| 201 | static void ata_acpi_dev_uevent(acpi_handle handle, u32 event, void *data) |
| 202 | { |
| 203 | struct ata_device *dev = data; |
| 204 | ata_acpi_uevent(dev->link->ap, dev, event); |
| 205 | } |
| 206 | |
Vasiliy Kulikov | 9c8b04b | 2011-06-25 21:07:52 +0400 | [diff] [blame] | 207 | static const struct acpi_dock_ops ata_acpi_dev_dock_ops = { |
Shaohua Li | 1253f7a | 2008-08-28 10:06:16 +0800 | [diff] [blame] | 208 | .handler = ata_acpi_dev_notify_dock, |
| 209 | .uevent = ata_acpi_dev_uevent, |
| 210 | }; |
| 211 | |
Vasiliy Kulikov | 9c8b04b | 2011-06-25 21:07:52 +0400 | [diff] [blame] | 212 | static const struct acpi_dock_ops ata_acpi_ap_dock_ops = { |
Shaohua Li | 1253f7a | 2008-08-28 10:06:16 +0800 | [diff] [blame] | 213 | .handler = ata_acpi_ap_notify_dock, |
| 214 | .uevent = ata_acpi_ap_uevent, |
| 215 | }; |
| 216 | |
Kristen Carlson Accardi | 11ef697 | 2006-09-28 11:29:01 -0700 | [diff] [blame] | 217 | /** |
Tejun Heo | 562f0c2 | 2007-12-15 15:05:01 +0900 | [diff] [blame] | 218 | * ata_acpi_dissociate - dissociate ATA host from ACPI objects |
| 219 | * @host: target ATA host |
| 220 | * |
| 221 | * This function is called during driver detach after the whole host |
| 222 | * is shut down. |
| 223 | * |
| 224 | * LOCKING: |
| 225 | * EH context. |
| 226 | */ |
| 227 | void ata_acpi_dissociate(struct ata_host *host) |
| 228 | { |
Tejun Heo | c05e6ff | 2007-12-15 15:05:02 +0900 | [diff] [blame] | 229 | int i; |
| 230 | |
| 231 | /* Restore initial _GTM values so that driver which attaches |
| 232 | * afterward can use them too. |
| 233 | */ |
| 234 | for (i = 0; i < host->n_ports; i++) { |
| 235 | struct ata_port *ap = host->ports[i]; |
| 236 | const struct ata_acpi_gtm *gtm = ata_acpi_init_gtm(ap); |
| 237 | |
Matthew Garrett | 30dcf76 | 2012-06-25 16:13:04 +0800 | [diff] [blame] | 238 | if (ata_ap_acpi_handle(ap) && gtm) |
Tejun Heo | c05e6ff | 2007-12-15 15:05:02 +0900 | [diff] [blame] | 239 | ata_acpi_stm(ap, gtm); |
| 240 | } |
Tejun Heo | 562f0c2 | 2007-12-15 15:05:01 +0900 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | /** |
Tejun Heo | 64578a3 | 2007-05-15 03:28:16 +0900 | [diff] [blame] | 244 | * ata_acpi_gtm - execute _GTM |
| 245 | * @ap: target ATA port |
| 246 | * @gtm: out parameter for _GTM result |
| 247 | * |
| 248 | * Evaluate _GTM and store the result in @gtm. |
| 249 | * |
| 250 | * LOCKING: |
| 251 | * EH context. |
| 252 | * |
| 253 | * RETURNS: |
| 254 | * 0 on success, -ENOENT if _GTM doesn't exist, -errno on failure. |
| 255 | */ |
Tejun Heo | 0d02f0b | 2007-12-15 15:04:57 +0900 | [diff] [blame] | 256 | 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] | 257 | { |
| 258 | struct acpi_buffer output = { .length = ACPI_ALLOCATE_BUFFER }; |
| 259 | union acpi_object *out_obj; |
| 260 | acpi_status status; |
| 261 | int rc = 0; |
| 262 | |
Matthew Garrett | 30dcf76 | 2012-06-25 16:13:04 +0800 | [diff] [blame] | 263 | status = acpi_evaluate_object(ata_ap_acpi_handle(ap), "_GTM", NULL, |
| 264 | &output); |
Tejun Heo | 64578a3 | 2007-05-15 03:28:16 +0900 | [diff] [blame] | 265 | |
| 266 | rc = -ENOENT; |
| 267 | if (status == AE_NOT_FOUND) |
| 268 | goto out_free; |
| 269 | |
| 270 | rc = -EINVAL; |
| 271 | if (ACPI_FAILURE(status)) { |
Joe Perches | a9a79df | 2011-04-15 15:51:59 -0700 | [diff] [blame] | 272 | ata_port_err(ap, "ACPI get timing mode failed (AE 0x%x)\n", |
| 273 | status); |
Tejun Heo | 64578a3 | 2007-05-15 03:28:16 +0900 | [diff] [blame] | 274 | goto out_free; |
| 275 | } |
| 276 | |
| 277 | out_obj = output.pointer; |
| 278 | if (out_obj->type != ACPI_TYPE_BUFFER) { |
Joe Perches | a9a79df | 2011-04-15 15:51:59 -0700 | [diff] [blame] | 279 | ata_port_warn(ap, "_GTM returned unexpected object type 0x%x\n", |
| 280 | out_obj->type); |
Tejun Heo | 64578a3 | 2007-05-15 03:28:16 +0900 | [diff] [blame] | 281 | |
| 282 | goto out_free; |
| 283 | } |
| 284 | |
| 285 | if (out_obj->buffer.length != sizeof(struct ata_acpi_gtm)) { |
Joe Perches | a9a79df | 2011-04-15 15:51:59 -0700 | [diff] [blame] | 286 | ata_port_err(ap, "_GTM returned invalid length %d\n", |
| 287 | out_obj->buffer.length); |
Tejun Heo | 64578a3 | 2007-05-15 03:28:16 +0900 | [diff] [blame] | 288 | goto out_free; |
| 289 | } |
| 290 | |
| 291 | memcpy(gtm, out_obj->buffer.pointer, sizeof(struct ata_acpi_gtm)); |
| 292 | rc = 0; |
| 293 | out_free: |
| 294 | kfree(output.pointer); |
| 295 | return rc; |
| 296 | } |
| 297 | |
Alan Cox | badff03 | 2007-10-04 21:28:18 +0100 | [diff] [blame] | 298 | EXPORT_SYMBOL_GPL(ata_acpi_gtm); |
| 299 | |
Tejun Heo | 64578a3 | 2007-05-15 03:28:16 +0900 | [diff] [blame] | 300 | /** |
| 301 | * ata_acpi_stm - execute _STM |
| 302 | * @ap: target ATA port |
| 303 | * @stm: timing parameter to _STM |
| 304 | * |
| 305 | * Evaluate _STM with timing parameter @stm. |
| 306 | * |
| 307 | * LOCKING: |
| 308 | * EH context. |
| 309 | * |
| 310 | * RETURNS: |
| 311 | * 0 on success, -ENOENT if _STM doesn't exist, -errno on failure. |
| 312 | */ |
Tejun Heo | 0d02f0b | 2007-12-15 15:04:57 +0900 | [diff] [blame] | 313 | 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] | 314 | { |
| 315 | acpi_status status; |
Tejun Heo | 0d02f0b | 2007-12-15 15:04:57 +0900 | [diff] [blame] | 316 | struct ata_acpi_gtm stm_buf = *stm; |
Tejun Heo | 64578a3 | 2007-05-15 03:28:16 +0900 | [diff] [blame] | 317 | struct acpi_object_list input; |
| 318 | union acpi_object in_params[3]; |
| 319 | |
| 320 | in_params[0].type = ACPI_TYPE_BUFFER; |
| 321 | in_params[0].buffer.length = sizeof(struct ata_acpi_gtm); |
Tejun Heo | 0d02f0b | 2007-12-15 15:04:57 +0900 | [diff] [blame] | 322 | in_params[0].buffer.pointer = (u8 *)&stm_buf; |
Tejun Heo | 64578a3 | 2007-05-15 03:28:16 +0900 | [diff] [blame] | 323 | /* Buffers for id may need byteswapping ? */ |
| 324 | in_params[1].type = ACPI_TYPE_BUFFER; |
| 325 | in_params[1].buffer.length = 512; |
Tejun Heo | 9af5c9c | 2007-08-06 18:36:22 +0900 | [diff] [blame] | 326 | in_params[1].buffer.pointer = (u8 *)ap->link.device[0].id; |
Tejun Heo | 64578a3 | 2007-05-15 03:28:16 +0900 | [diff] [blame] | 327 | in_params[2].type = ACPI_TYPE_BUFFER; |
| 328 | in_params[2].buffer.length = 512; |
Tejun Heo | 9af5c9c | 2007-08-06 18:36:22 +0900 | [diff] [blame] | 329 | in_params[2].buffer.pointer = (u8 *)ap->link.device[1].id; |
Tejun Heo | 64578a3 | 2007-05-15 03:28:16 +0900 | [diff] [blame] | 330 | |
| 331 | input.count = 3; |
| 332 | input.pointer = in_params; |
| 333 | |
Matthew Garrett | 30dcf76 | 2012-06-25 16:13:04 +0800 | [diff] [blame] | 334 | status = acpi_evaluate_object(ata_ap_acpi_handle(ap), "_STM", &input, |
| 335 | NULL); |
Tejun Heo | 64578a3 | 2007-05-15 03:28:16 +0900 | [diff] [blame] | 336 | |
| 337 | if (status == AE_NOT_FOUND) |
| 338 | return -ENOENT; |
| 339 | if (ACPI_FAILURE(status)) { |
Joe Perches | a9a79df | 2011-04-15 15:51:59 -0700 | [diff] [blame] | 340 | ata_port_err(ap, "ACPI set timing mode failed (status=0x%x)\n", |
| 341 | status); |
Tejun Heo | 64578a3 | 2007-05-15 03:28:16 +0900 | [diff] [blame] | 342 | return -EINVAL; |
| 343 | } |
| 344 | return 0; |
| 345 | } |
| 346 | |
Alan Cox | badff03 | 2007-10-04 21:28:18 +0100 | [diff] [blame] | 347 | EXPORT_SYMBOL_GPL(ata_acpi_stm); |
| 348 | |
Tejun Heo | 64578a3 | 2007-05-15 03:28:16 +0900 | [diff] [blame] | 349 | /** |
Tejun Heo | 4700c4b | 2007-05-15 03:28:16 +0900 | [diff] [blame] | 350 | * ata_dev_get_GTF - get the drive bootup default taskfile settings |
Tejun Heo | 3a32a8e | 2007-05-05 23:50:38 +0900 | [diff] [blame] | 351 | * @dev: target ATA device |
Tejun Heo | 4700c4b | 2007-05-15 03:28:16 +0900 | [diff] [blame] | 352 | * @gtf: output parameter for buffer containing _GTF taskfile arrays |
Kristen Carlson Accardi | 11ef697 | 2006-09-28 11:29:01 -0700 | [diff] [blame] | 353 | * |
| 354 | * This applies to both PATA and SATA drives. |
| 355 | * |
| 356 | * The _GTF method has no input parameters. |
| 357 | * It returns a variable number of register set values (registers |
| 358 | * hex 1F1..1F7, taskfiles). |
| 359 | * The <variable number> is not known in advance, so have ACPI-CA |
| 360 | * allocate the buffer as needed and return it, then free it later. |
| 361 | * |
Tejun Heo | 4700c4b | 2007-05-15 03:28:16 +0900 | [diff] [blame] | 362 | * LOCKING: |
| 363 | * EH context. |
| 364 | * |
| 365 | * RETURNS: |
Tejun Heo | 66fa7f2 | 2007-12-15 15:05:04 +0900 | [diff] [blame] | 366 | * Number of taskfiles on success, 0 if _GTF doesn't exist. -EINVAL |
| 367 | * if _GTF is invalid. |
Kristen Carlson Accardi | 11ef697 | 2006-09-28 11:29:01 -0700 | [diff] [blame] | 368 | */ |
Tejun Heo | 398e078 | 2007-12-15 15:05:03 +0900 | [diff] [blame] | 369 | 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] | 370 | { |
Tejun Heo | 9af5c9c | 2007-08-06 18:36:22 +0900 | [diff] [blame] | 371 | struct ata_port *ap = dev->link->ap; |
Tejun Heo | 3a32a8e | 2007-05-05 23:50:38 +0900 | [diff] [blame] | 372 | acpi_status status; |
Tejun Heo | 3a32a8e | 2007-05-05 23:50:38 +0900 | [diff] [blame] | 373 | struct acpi_buffer output; |
| 374 | union acpi_object *out_obj; |
Tejun Heo | 4700c4b | 2007-05-15 03:28:16 +0900 | [diff] [blame] | 375 | int rc = 0; |
Kristen Carlson Accardi | 11ef697 | 2006-09-28 11:29:01 -0700 | [diff] [blame] | 376 | |
Tejun Heo | 398e078 | 2007-12-15 15:05:03 +0900 | [diff] [blame] | 377 | /* if _GTF is cached, use the cached value */ |
| 378 | if (dev->gtf_cache) { |
| 379 | out_obj = dev->gtf_cache; |
| 380 | goto done; |
| 381 | } |
| 382 | |
Tejun Heo | 4700c4b | 2007-05-15 03:28:16 +0900 | [diff] [blame] | 383 | /* set up output buffer */ |
| 384 | output.length = ACPI_ALLOCATE_BUFFER; |
| 385 | output.pointer = NULL; /* ACPI-CA sets this; save/free it later */ |
Kristen Carlson Accardi | 11ef697 | 2006-09-28 11:29:01 -0700 | [diff] [blame] | 386 | |
Kristen Carlson Accardi | 11ef697 | 2006-09-28 11:29:01 -0700 | [diff] [blame] | 387 | if (ata_msg_probe(ap)) |
Joe Perches | a9a79df | 2011-04-15 15:51:59 -0700 | [diff] [blame] | 388 | ata_dev_dbg(dev, "%s: ENTER: port#: %d\n", |
| 389 | __func__, ap->port_no); |
Kristen Carlson Accardi | 11ef697 | 2006-09-28 11:29:01 -0700 | [diff] [blame] | 390 | |
Kristen Carlson Accardi | 11ef697 | 2006-09-28 11:29:01 -0700 | [diff] [blame] | 391 | /* _GTF has no input parameters */ |
Matthew Garrett | 30dcf76 | 2012-06-25 16:13:04 +0800 | [diff] [blame] | 392 | status = acpi_evaluate_object(ata_dev_acpi_handle(dev), "_GTF", NULL, |
| 393 | &output); |
Tejun Heo | 398e078 | 2007-12-15 15:05:03 +0900 | [diff] [blame] | 394 | out_obj = dev->gtf_cache = output.pointer; |
Tejun Heo | 4700c4b | 2007-05-15 03:28:16 +0900 | [diff] [blame] | 395 | |
Kristen Carlson Accardi | 11ef697 | 2006-09-28 11:29:01 -0700 | [diff] [blame] | 396 | if (ACPI_FAILURE(status)) { |
Tejun Heo | 4700c4b | 2007-05-15 03:28:16 +0900 | [diff] [blame] | 397 | if (status != AE_NOT_FOUND) { |
Joe Perches | a9a79df | 2011-04-15 15:51:59 -0700 | [diff] [blame] | 398 | ata_dev_warn(dev, "_GTF evaluation failed (AE 0x%x)\n", |
| 399 | status); |
Tejun Heo | 66fa7f2 | 2007-12-15 15:05:04 +0900 | [diff] [blame] | 400 | rc = -EINVAL; |
Tejun Heo | 4700c4b | 2007-05-15 03:28:16 +0900 | [diff] [blame] | 401 | } |
| 402 | goto out_free; |
Kristen Carlson Accardi | 11ef697 | 2006-09-28 11:29:01 -0700 | [diff] [blame] | 403 | } |
| 404 | |
| 405 | if (!output.length || !output.pointer) { |
| 406 | if (ata_msg_probe(ap)) |
Joe Perches | a9a79df | 2011-04-15 15:51:59 -0700 | [diff] [blame] | 407 | ata_dev_dbg(dev, "%s: Run _GTF: length or ptr is NULL (0x%llx, 0x%p)\n", |
| 408 | __func__, |
| 409 | (unsigned long long)output.length, |
| 410 | output.pointer); |
Tejun Heo | 66fa7f2 | 2007-12-15 15:05:04 +0900 | [diff] [blame] | 411 | rc = -EINVAL; |
Tejun Heo | 4700c4b | 2007-05-15 03:28:16 +0900 | [diff] [blame] | 412 | goto out_free; |
Kristen Carlson Accardi | 11ef697 | 2006-09-28 11:29:01 -0700 | [diff] [blame] | 413 | } |
| 414 | |
Kristen Carlson Accardi | 11ef697 | 2006-09-28 11:29:01 -0700 | [diff] [blame] | 415 | if (out_obj->type != ACPI_TYPE_BUFFER) { |
Joe Perches | a9a79df | 2011-04-15 15:51:59 -0700 | [diff] [blame] | 416 | ata_dev_warn(dev, "_GTF unexpected object type 0x%x\n", |
| 417 | out_obj->type); |
Tejun Heo | 66fa7f2 | 2007-12-15 15:05:04 +0900 | [diff] [blame] | 418 | rc = -EINVAL; |
Tejun Heo | 4700c4b | 2007-05-15 03:28:16 +0900 | [diff] [blame] | 419 | goto out_free; |
Kristen Carlson Accardi | 11ef697 | 2006-09-28 11:29:01 -0700 | [diff] [blame] | 420 | } |
| 421 | |
Tejun Heo | 4700c4b | 2007-05-15 03:28:16 +0900 | [diff] [blame] | 422 | if (out_obj->buffer.length % REGS_PER_GTF) { |
Joe Perches | a9a79df | 2011-04-15 15:51:59 -0700 | [diff] [blame] | 423 | ata_dev_warn(dev, "unexpected _GTF length (%d)\n", |
| 424 | out_obj->buffer.length); |
Tejun Heo | 66fa7f2 | 2007-12-15 15:05:04 +0900 | [diff] [blame] | 425 | rc = -EINVAL; |
Tejun Heo | 4700c4b | 2007-05-15 03:28:16 +0900 | [diff] [blame] | 426 | goto out_free; |
Kristen Carlson Accardi | 11ef697 | 2006-09-28 11:29:01 -0700 | [diff] [blame] | 427 | } |
| 428 | |
Tejun Heo | 398e078 | 2007-12-15 15:05:03 +0900 | [diff] [blame] | 429 | done: |
Tejun Heo | 4700c4b | 2007-05-15 03:28:16 +0900 | [diff] [blame] | 430 | rc = out_obj->buffer.length / REGS_PER_GTF; |
Tejun Heo | 398e078 | 2007-12-15 15:05:03 +0900 | [diff] [blame] | 431 | if (gtf) { |
| 432 | *gtf = (void *)out_obj->buffer.pointer; |
| 433 | if (ata_msg_probe(ap)) |
Joe Perches | a9a79df | 2011-04-15 15:51:59 -0700 | [diff] [blame] | 434 | ata_dev_dbg(dev, "%s: returning gtf=%p, gtf_count=%d\n", |
| 435 | __func__, *gtf, rc); |
Tejun Heo | 398e078 | 2007-12-15 15:05:03 +0900 | [diff] [blame] | 436 | } |
Tejun Heo | 4700c4b | 2007-05-15 03:28:16 +0900 | [diff] [blame] | 437 | return rc; |
| 438 | |
| 439 | out_free: |
Tejun Heo | 398e078 | 2007-12-15 15:05:03 +0900 | [diff] [blame] | 440 | ata_acpi_clear_gtf(dev); |
Tejun Heo | 4700c4b | 2007-05-15 03:28:16 +0900 | [diff] [blame] | 441 | return rc; |
Kristen Carlson Accardi | 11ef697 | 2006-09-28 11:29:01 -0700 | [diff] [blame] | 442 | } |
| 443 | |
Tejun Heo | 7c77fa4 | 2007-12-18 16:33:03 +0900 | [diff] [blame] | 444 | /** |
| 445 | * ata_acpi_gtm_xfermode - determine xfermode from GTM parameter |
| 446 | * @dev: target device |
| 447 | * @gtm: GTM parameter to use |
| 448 | * |
| 449 | * Determine xfermask for @dev from @gtm. |
| 450 | * |
| 451 | * LOCKING: |
| 452 | * None. |
| 453 | * |
| 454 | * RETURNS: |
| 455 | * Determined xfermask. |
| 456 | */ |
| 457 | unsigned long ata_acpi_gtm_xfermask(struct ata_device *dev, |
| 458 | const struct ata_acpi_gtm *gtm) |
| 459 | { |
Tejun Heo | a0f79b9 | 2007-12-18 16:33:05 +0900 | [diff] [blame] | 460 | unsigned long xfer_mask = 0; |
| 461 | unsigned int type; |
| 462 | int unit; |
| 463 | u8 mode; |
Tejun Heo | 7c77fa4 | 2007-12-18 16:33:03 +0900 | [diff] [blame] | 464 | |
| 465 | /* we always use the 0 slot for crap hardware */ |
| 466 | unit = dev->devno; |
| 467 | if (!(gtm->flags & 0x10)) |
| 468 | unit = 0; |
| 469 | |
Tejun Heo | a0f79b9 | 2007-12-18 16:33:05 +0900 | [diff] [blame] | 470 | /* PIO */ |
| 471 | mode = ata_timing_cycle2mode(ATA_SHIFT_PIO, gtm->drive[unit].pio); |
| 472 | xfer_mask |= ata_xfer_mode2mask(mode); |
Tejun Heo | 7c77fa4 | 2007-12-18 16:33:03 +0900 | [diff] [blame] | 473 | |
| 474 | /* See if we have MWDMA or UDMA data. We don't bother with |
| 475 | * MWDMA if UDMA is available as this means the BIOS set UDMA |
| 476 | * and our error changedown if it works is UDMA to PIO anyway. |
| 477 | */ |
Tejun Heo | a0f79b9 | 2007-12-18 16:33:05 +0900 | [diff] [blame] | 478 | if (!(gtm->flags & (1 << (2 * unit)))) |
| 479 | type = ATA_SHIFT_MWDMA; |
| 480 | else |
| 481 | type = ATA_SHIFT_UDMA; |
Tejun Heo | 7c77fa4 | 2007-12-18 16:33:03 +0900 | [diff] [blame] | 482 | |
Tejun Heo | a0f79b9 | 2007-12-18 16:33:05 +0900 | [diff] [blame] | 483 | mode = ata_timing_cycle2mode(type, gtm->drive[unit].dma); |
| 484 | xfer_mask |= ata_xfer_mode2mask(mode); |
| 485 | |
| 486 | return xfer_mask; |
Tejun Heo | 7c77fa4 | 2007-12-18 16:33:03 +0900 | [diff] [blame] | 487 | } |
| 488 | EXPORT_SYMBOL_GPL(ata_acpi_gtm_xfermask); |
| 489 | |
Kristen Carlson Accardi | 11ef697 | 2006-09-28 11:29:01 -0700 | [diff] [blame] | 490 | /** |
Alan Cox | e1ddb4b | 2007-08-16 02:33:36 -0400 | [diff] [blame] | 491 | * ata_acpi_cbl_80wire - Check for 80 wire cable |
| 492 | * @ap: Port to check |
Tejun Heo | 021ee9a | 2007-12-18 16:33:06 +0900 | [diff] [blame] | 493 | * @gtm: GTM data to use |
Alan Cox | e1ddb4b | 2007-08-16 02:33:36 -0400 | [diff] [blame] | 494 | * |
Tejun Heo | 021ee9a | 2007-12-18 16:33:06 +0900 | [diff] [blame] | 495 | * Return 1 if the @gtm indicates the BIOS selected an 80wire mode. |
Alan Cox | e1ddb4b | 2007-08-16 02:33:36 -0400 | [diff] [blame] | 496 | */ |
Tejun Heo | 021ee9a | 2007-12-18 16:33:06 +0900 | [diff] [blame] | 497 | 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] | 498 | { |
Tejun Heo | 021ee9a | 2007-12-18 16:33:06 +0900 | [diff] [blame] | 499 | struct ata_device *dev; |
Jeff Garzik | 2dcb407 | 2007-10-19 06:42:56 -0400 | [diff] [blame] | 500 | |
Tejun Heo | 1eca436 | 2008-11-03 20:03:17 +0900 | [diff] [blame] | 501 | ata_for_each_dev(dev, &ap->link, ENABLED) { |
Tejun Heo | 021ee9a | 2007-12-18 16:33:06 +0900 | [diff] [blame] | 502 | unsigned long xfer_mask, udma_mask; |
Jeff Garzik | 2dcb407 | 2007-10-19 06:42:56 -0400 | [diff] [blame] | 503 | |
Tejun Heo | 021ee9a | 2007-12-18 16:33:06 +0900 | [diff] [blame] | 504 | xfer_mask = ata_acpi_gtm_xfermask(dev, gtm); |
| 505 | ata_unpack_xfermask(xfer_mask, NULL, NULL, &udma_mask); |
| 506 | |
| 507 | if (udma_mask & ~ATA_UDMA_MASK_40C) |
| 508 | return 1; |
| 509 | } |
| 510 | |
Alan Cox | e1ddb4b | 2007-08-16 02:33:36 -0400 | [diff] [blame] | 511 | return 0; |
| 512 | } |
Alan Cox | e1ddb4b | 2007-08-16 02:33:36 -0400 | [diff] [blame] | 513 | EXPORT_SYMBOL_GPL(ata_acpi_cbl_80wire); |
| 514 | |
Tejun Heo | 3264a8d | 2007-12-15 15:05:06 +0900 | [diff] [blame] | 515 | static void ata_acpi_gtf_to_tf(struct ata_device *dev, |
| 516 | const struct ata_acpi_gtf *gtf, |
| 517 | struct ata_taskfile *tf) |
| 518 | { |
| 519 | ata_tf_init(dev, tf); |
| 520 | |
| 521 | tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE; |
| 522 | tf->protocol = ATA_PROT_NODATA; |
| 523 | tf->feature = gtf->tf[0]; /* 0x1f1 */ |
| 524 | tf->nsect = gtf->tf[1]; /* 0x1f2 */ |
| 525 | tf->lbal = gtf->tf[2]; /* 0x1f3 */ |
| 526 | tf->lbam = gtf->tf[3]; /* 0x1f4 */ |
| 527 | tf->lbah = gtf->tf[4]; /* 0x1f5 */ |
| 528 | tf->device = gtf->tf[5]; /* 0x1f6 */ |
| 529 | tf->command = gtf->tf[6]; /* 0x1f7 */ |
| 530 | } |
| 531 | |
Tejun Heo | 110f66d | 2009-09-16 04:17:28 +0900 | [diff] [blame] | 532 | static int ata_acpi_filter_tf(struct ata_device *dev, |
| 533 | const struct ata_taskfile *tf, |
Tejun Heo | 3264a8d | 2007-12-15 15:05:06 +0900 | [diff] [blame] | 534 | const struct ata_taskfile *ptf) |
| 535 | { |
Tejun Heo | 110f66d | 2009-09-16 04:17:28 +0900 | [diff] [blame] | 536 | if (dev->gtf_filter & ATA_ACPI_FILTER_SETXFER) { |
Tejun Heo | 3264a8d | 2007-12-15 15:05:06 +0900 | [diff] [blame] | 537 | /* libata doesn't use ACPI to configure transfer mode. |
| 538 | * It will only confuse device configuration. Skip. |
| 539 | */ |
| 540 | if (tf->command == ATA_CMD_SET_FEATURES && |
| 541 | tf->feature == SETFEATURES_XFER) |
| 542 | return 1; |
| 543 | } |
| 544 | |
Tejun Heo | 110f66d | 2009-09-16 04:17:28 +0900 | [diff] [blame] | 545 | if (dev->gtf_filter & ATA_ACPI_FILTER_LOCK) { |
Tejun Heo | 3264a8d | 2007-12-15 15:05:06 +0900 | [diff] [blame] | 546 | /* BIOS writers, sorry but we don't wanna lock |
| 547 | * features unless the user explicitly said so. |
| 548 | */ |
| 549 | |
| 550 | /* DEVICE CONFIGURATION FREEZE LOCK */ |
| 551 | if (tf->command == ATA_CMD_CONF_OVERLAY && |
| 552 | tf->feature == ATA_DCO_FREEZE_LOCK) |
| 553 | return 1; |
| 554 | |
| 555 | /* SECURITY FREEZE LOCK */ |
| 556 | if (tf->command == ATA_CMD_SEC_FREEZE_LOCK) |
| 557 | return 1; |
| 558 | |
| 559 | /* SET MAX LOCK and SET MAX FREEZE LOCK */ |
| 560 | if ((!ptf || ptf->command != ATA_CMD_READ_NATIVE_MAX) && |
| 561 | tf->command == ATA_CMD_SET_MAX && |
| 562 | (tf->feature == ATA_SET_MAX_LOCK || |
| 563 | tf->feature == ATA_SET_MAX_FREEZE_LOCK)) |
| 564 | return 1; |
| 565 | } |
| 566 | |
Tejun Heo | fa5b561 | 2009-09-16 04:17:02 +0900 | [diff] [blame] | 567 | if (tf->command == ATA_CMD_SET_FEATURES && |
| 568 | tf->feature == SETFEATURES_SATA_ENABLE) { |
Tejun Heo | b344991 | 2008-07-06 23:15:03 +0900 | [diff] [blame] | 569 | /* inhibit enabling DIPM */ |
Tejun Heo | 110f66d | 2009-09-16 04:17:28 +0900 | [diff] [blame] | 570 | if (dev->gtf_filter & ATA_ACPI_FILTER_DIPM && |
Tejun Heo | b344991 | 2008-07-06 23:15:03 +0900 | [diff] [blame] | 571 | tf->nsect == SATA_DIPM) |
| 572 | return 1; |
Tejun Heo | fa5b561 | 2009-09-16 04:17:02 +0900 | [diff] [blame] | 573 | |
| 574 | /* inhibit FPDMA non-zero offset */ |
Tejun Heo | 110f66d | 2009-09-16 04:17:28 +0900 | [diff] [blame] | 575 | if (dev->gtf_filter & ATA_ACPI_FILTER_FPDMA_OFFSET && |
Tejun Heo | fa5b561 | 2009-09-16 04:17:02 +0900 | [diff] [blame] | 576 | (tf->nsect == SATA_FPDMA_OFFSET || |
| 577 | tf->nsect == SATA_FPDMA_IN_ORDER)) |
| 578 | return 1; |
| 579 | |
| 580 | /* inhibit FPDMA auto activation */ |
Tejun Heo | 110f66d | 2009-09-16 04:17:28 +0900 | [diff] [blame] | 581 | if (dev->gtf_filter & ATA_ACPI_FILTER_FPDMA_AA && |
Tejun Heo | fa5b561 | 2009-09-16 04:17:02 +0900 | [diff] [blame] | 582 | tf->nsect == SATA_FPDMA_AA) |
| 583 | return 1; |
Tejun Heo | b344991 | 2008-07-06 23:15:03 +0900 | [diff] [blame] | 584 | } |
| 585 | |
Tejun Heo | 3264a8d | 2007-12-15 15:05:06 +0900 | [diff] [blame] | 586 | return 0; |
| 587 | } |
| 588 | |
Alan Cox | e1ddb4b | 2007-08-16 02:33:36 -0400 | [diff] [blame] | 589 | /** |
Tejun Heo | 0e8634b | 2007-12-15 15:05:05 +0900 | [diff] [blame] | 590 | * ata_acpi_run_tf - send taskfile registers to host controller |
Tejun Heo | 3a32a8e | 2007-05-05 23:50:38 +0900 | [diff] [blame] | 591 | * @dev: target ATA device |
Kristen Carlson Accardi | 11ef697 | 2006-09-28 11:29:01 -0700 | [diff] [blame] | 592 | * @gtf: raw ATA taskfile register set (0x1f1 - 0x1f7) |
| 593 | * |
Sergei Shtylyov | 3696df3 | 2011-02-04 22:04:17 +0300 | [diff] [blame] | 594 | * Outputs ATA taskfile to standard ATA host controller. |
Kristen Carlson Accardi | 11ef697 | 2006-09-28 11:29:01 -0700 | [diff] [blame] | 595 | * Writes the control, feature, nsect, lbal, lbam, and lbah registers. |
| 596 | * Optionally (ATA_TFLAG_LBA48) writes hob_feature, hob_nsect, |
| 597 | * hob_lbal, hob_lbam, and hob_lbah. |
| 598 | * |
| 599 | * This function waits for idle (!BUSY and !DRQ) after writing |
| 600 | * registers. If the control register has a new value, this |
| 601 | * function also waits for idle after writing control and before |
| 602 | * writing the remaining registers. |
| 603 | * |
Tejun Heo | 4700c4b | 2007-05-15 03:28:16 +0900 | [diff] [blame] | 604 | * LOCKING: |
| 605 | * EH context. |
| 606 | * |
| 607 | * RETURNS: |
Tejun Heo | 3264a8d | 2007-12-15 15:05:06 +0900 | [diff] [blame] | 608 | * 1 if command is executed successfully. 0 if ignored, rejected or |
| 609 | * filtered out, -errno on other errors. |
Kristen Carlson Accardi | 11ef697 | 2006-09-28 11:29:01 -0700 | [diff] [blame] | 610 | */ |
Tejun Heo | 0e8634b | 2007-12-15 15:05:05 +0900 | [diff] [blame] | 611 | static int ata_acpi_run_tf(struct ata_device *dev, |
Tejun Heo | 3264a8d | 2007-12-15 15:05:06 +0900 | [diff] [blame] | 612 | const struct ata_acpi_gtf *gtf, |
| 613 | const struct ata_acpi_gtf *prev_gtf) |
Kristen Carlson Accardi | 11ef697 | 2006-09-28 11:29:01 -0700 | [diff] [blame] | 614 | { |
Tejun Heo | 3264a8d | 2007-12-15 15:05:06 +0900 | [diff] [blame] | 615 | struct ata_taskfile *pptf = NULL; |
| 616 | struct ata_taskfile tf, ptf, rtf; |
Tejun Heo | 4700c4b | 2007-05-15 03:28:16 +0900 | [diff] [blame] | 617 | unsigned int err_mask; |
Tejun Heo | 0e8634b | 2007-12-15 15:05:05 +0900 | [diff] [blame] | 618 | const char *level; |
Robert Hancock | 6521148 | 2009-07-14 20:43:39 -0600 | [diff] [blame] | 619 | const char *descr; |
Tejun Heo | 0e8634b | 2007-12-15 15:05:05 +0900 | [diff] [blame] | 620 | char msg[60]; |
| 621 | int rc; |
Jeff Garzik | fc16c25 | 2007-02-24 21:05:01 -0500 | [diff] [blame] | 622 | |
Tejun Heo | 4700c4b | 2007-05-15 03:28:16 +0900 | [diff] [blame] | 623 | if ((gtf->tf[0] == 0) && (gtf->tf[1] == 0) && (gtf->tf[2] == 0) |
| 624 | && (gtf->tf[3] == 0) && (gtf->tf[4] == 0) && (gtf->tf[5] == 0) |
| 625 | && (gtf->tf[6] == 0)) |
| 626 | return 0; |
Kristen Carlson Accardi | 11ef697 | 2006-09-28 11:29:01 -0700 | [diff] [blame] | 627 | |
Tejun Heo | 3264a8d | 2007-12-15 15:05:06 +0900 | [diff] [blame] | 628 | ata_acpi_gtf_to_tf(dev, gtf, &tf); |
| 629 | if (prev_gtf) { |
| 630 | ata_acpi_gtf_to_tf(dev, prev_gtf, &ptf); |
| 631 | pptf = &ptf; |
| 632 | } |
Kristen Carlson Accardi | 11ef697 | 2006-09-28 11:29:01 -0700 | [diff] [blame] | 633 | |
Tejun Heo | 110f66d | 2009-09-16 04:17:28 +0900 | [diff] [blame] | 634 | if (!ata_acpi_filter_tf(dev, &tf, pptf)) { |
Tejun Heo | 3264a8d | 2007-12-15 15:05:06 +0900 | [diff] [blame] | 635 | rtf = tf; |
| 636 | err_mask = ata_exec_internal(dev, &rtf, NULL, |
| 637 | DMA_NONE, NULL, 0, 0); |
Kristen Carlson Accardi | 11ef697 | 2006-09-28 11:29:01 -0700 | [diff] [blame] | 638 | |
Tejun Heo | 3264a8d | 2007-12-15 15:05:06 +0900 | [diff] [blame] | 639 | switch (err_mask) { |
| 640 | case 0: |
| 641 | level = KERN_DEBUG; |
| 642 | snprintf(msg, sizeof(msg), "succeeded"); |
| 643 | rc = 1; |
| 644 | break; |
Tejun Heo | 0e8634b | 2007-12-15 15:05:05 +0900 | [diff] [blame] | 645 | |
Tejun Heo | 3264a8d | 2007-12-15 15:05:06 +0900 | [diff] [blame] | 646 | case AC_ERR_DEV: |
| 647 | level = KERN_INFO; |
| 648 | snprintf(msg, sizeof(msg), |
| 649 | "rejected by device (Stat=0x%02x Err=0x%02x)", |
| 650 | rtf.command, rtf.feature); |
| 651 | rc = 0; |
| 652 | break; |
Tejun Heo | 0e8634b | 2007-12-15 15:05:05 +0900 | [diff] [blame] | 653 | |
Tejun Heo | 3264a8d | 2007-12-15 15:05:06 +0900 | [diff] [blame] | 654 | default: |
| 655 | level = KERN_ERR; |
| 656 | snprintf(msg, sizeof(msg), |
| 657 | "failed (Emask=0x%x Stat=0x%02x Err=0x%02x)", |
| 658 | err_mask, rtf.command, rtf.feature); |
| 659 | rc = -EIO; |
| 660 | break; |
| 661 | } |
| 662 | } else { |
Tejun Heo | 0e8634b | 2007-12-15 15:05:05 +0900 | [diff] [blame] | 663 | level = KERN_INFO; |
Tejun Heo | 3264a8d | 2007-12-15 15:05:06 +0900 | [diff] [blame] | 664 | snprintf(msg, sizeof(msg), "filtered out"); |
Tejun Heo | 0e8634b | 2007-12-15 15:05:05 +0900 | [diff] [blame] | 665 | rc = 0; |
Tejun Heo | 4700c4b | 2007-05-15 03:28:16 +0900 | [diff] [blame] | 666 | } |
Robert Hancock | 6521148 | 2009-07-14 20:43:39 -0600 | [diff] [blame] | 667 | descr = ata_get_cmd_descript(tf.command); |
Tejun Heo | 4700c4b | 2007-05-15 03:28:16 +0900 | [diff] [blame] | 668 | |
Tejun Heo | 0e8634b | 2007-12-15 15:05:05 +0900 | [diff] [blame] | 669 | ata_dev_printk(dev, level, |
Robert Hancock | 6521148 | 2009-07-14 20:43:39 -0600 | [diff] [blame] | 670 | "ACPI cmd %02x/%02x:%02x:%02x:%02x:%02x:%02x (%s) %s\n", |
Tejun Heo | 0e8634b | 2007-12-15 15:05:05 +0900 | [diff] [blame] | 671 | tf.command, tf.feature, tf.nsect, tf.lbal, |
Robert Hancock | 6521148 | 2009-07-14 20:43:39 -0600 | [diff] [blame] | 672 | tf.lbam, tf.lbah, tf.device, |
| 673 | (descr ? descr : "unknown"), msg); |
Tejun Heo | 0e8634b | 2007-12-15 15:05:05 +0900 | [diff] [blame] | 674 | |
| 675 | return rc; |
Kristen Carlson Accardi | 11ef697 | 2006-09-28 11:29:01 -0700 | [diff] [blame] | 676 | } |
| 677 | |
| 678 | /** |
Kristen Carlson Accardi | 11ef697 | 2006-09-28 11:29:01 -0700 | [diff] [blame] | 679 | * ata_acpi_exec_tfs - get then write drive taskfile settings |
Tejun Heo | 6746544 | 2007-05-15 03:28:16 +0900 | [diff] [blame] | 680 | * @dev: target ATA device |
Martin Olsson | 98a1708 | 2009-04-22 18:21:29 +0200 | [diff] [blame] | 681 | * @nr_executed: out parameter for the number of executed commands |
Kristen Carlson Accardi | 11ef697 | 2006-09-28 11:29:01 -0700 | [diff] [blame] | 682 | * |
Martin Olsson | 98a1708 | 2009-04-22 18:21:29 +0200 | [diff] [blame] | 683 | * Evaluate _GTF and execute returned taskfiles. |
Tejun Heo | 69b16a5 | 2007-05-15 03:28:16 +0900 | [diff] [blame] | 684 | * |
| 685 | * LOCKING: |
| 686 | * EH context. |
| 687 | * |
| 688 | * RETURNS: |
Tejun Heo | 66fa7f2 | 2007-12-15 15:05:04 +0900 | [diff] [blame] | 689 | * Number of executed taskfiles on success, 0 if _GTF doesn't exist. |
| 690 | * -errno on other errors. |
Kristen Carlson Accardi | 11ef697 | 2006-09-28 11:29:01 -0700 | [diff] [blame] | 691 | */ |
Tejun Heo | 66fa7f2 | 2007-12-15 15:05:04 +0900 | [diff] [blame] | 692 | 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] | 693 | { |
Tejun Heo | 3264a8d | 2007-12-15 15:05:06 +0900 | [diff] [blame] | 694 | struct ata_acpi_gtf *gtf = NULL, *pgtf = NULL; |
Tejun Heo | 6746544 | 2007-05-15 03:28:16 +0900 | [diff] [blame] | 695 | int gtf_count, i, rc; |
Kristen Carlson Accardi | 11ef697 | 2006-09-28 11:29:01 -0700 | [diff] [blame] | 696 | |
Tejun Heo | 6746544 | 2007-05-15 03:28:16 +0900 | [diff] [blame] | 697 | /* get taskfiles */ |
Tejun Heo | 66fa7f2 | 2007-12-15 15:05:04 +0900 | [diff] [blame] | 698 | rc = ata_dev_get_GTF(dev, >f); |
| 699 | if (rc < 0) |
| 700 | return rc; |
| 701 | gtf_count = rc; |
Kristen Carlson Accardi | 11ef697 | 2006-09-28 11:29:01 -0700 | [diff] [blame] | 702 | |
Tejun Heo | 6746544 | 2007-05-15 03:28:16 +0900 | [diff] [blame] | 703 | /* execute them */ |
Tejun Heo | 3264a8d | 2007-12-15 15:05:06 +0900 | [diff] [blame] | 704 | for (i = 0; i < gtf_count; i++, gtf++) { |
| 705 | rc = ata_acpi_run_tf(dev, gtf, pgtf); |
Tejun Heo | 0e8634b | 2007-12-15 15:05:05 +0900 | [diff] [blame] | 706 | if (rc < 0) |
| 707 | break; |
Tejun Heo | 3264a8d | 2007-12-15 15:05:06 +0900 | [diff] [blame] | 708 | if (rc) { |
Tejun Heo | 0e8634b | 2007-12-15 15:05:05 +0900 | [diff] [blame] | 709 | (*nr_executed)++; |
Tejun Heo | 3264a8d | 2007-12-15 15:05:06 +0900 | [diff] [blame] | 710 | pgtf = gtf; |
| 711 | } |
Kristen Carlson Accardi | 11ef697 | 2006-09-28 11:29:01 -0700 | [diff] [blame] | 712 | } |
| 713 | |
Tejun Heo | 398e078 | 2007-12-15 15:05:03 +0900 | [diff] [blame] | 714 | ata_acpi_clear_gtf(dev); |
Tejun Heo | 6746544 | 2007-05-15 03:28:16 +0900 | [diff] [blame] | 715 | |
Tejun Heo | 0e8634b | 2007-12-15 15:05:05 +0900 | [diff] [blame] | 716 | if (rc < 0) |
| 717 | return rc; |
| 718 | return 0; |
Kristen Carlson Accardi | 11ef697 | 2006-09-28 11:29:01 -0700 | [diff] [blame] | 719 | } |
| 720 | |
Kristen Carlson Accardi | 7ea1fbc | 2006-09-28 11:29:12 -0700 | [diff] [blame] | 721 | /** |
| 722 | * ata_acpi_push_id - send Identify data to drive |
Tejun Heo | 3a32a8e | 2007-05-05 23:50:38 +0900 | [diff] [blame] | 723 | * @dev: target ATA device |
Kristen Carlson Accardi | 7ea1fbc | 2006-09-28 11:29:12 -0700 | [diff] [blame] | 724 | * |
| 725 | * _SDD ACPI object: for SATA mode only |
| 726 | * Must be after Identify (Packet) Device -- uses its data |
| 727 | * ATM this function never returns a failure. It is an optional |
| 728 | * method and if it fails for whatever reason, we should still |
| 729 | * just keep going. |
Tejun Heo | 69b16a5 | 2007-05-15 03:28:16 +0900 | [diff] [blame] | 730 | * |
| 731 | * LOCKING: |
| 732 | * EH context. |
| 733 | * |
| 734 | * RETURNS: |
Tejun Heo | f240677 | 2009-11-18 22:24:21 +0900 | [diff] [blame] | 735 | * 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] | 736 | */ |
Tejun Heo | 6746544 | 2007-05-15 03:28:16 +0900 | [diff] [blame] | 737 | static int ata_acpi_push_id(struct ata_device *dev) |
Kristen Carlson Accardi | 7ea1fbc | 2006-09-28 11:29:12 -0700 | [diff] [blame] | 738 | { |
Tejun Heo | 9af5c9c | 2007-08-06 18:36:22 +0900 | [diff] [blame] | 739 | struct ata_port *ap = dev->link->ap; |
Tejun Heo | 3a32a8e | 2007-05-05 23:50:38 +0900 | [diff] [blame] | 740 | acpi_status status; |
| 741 | struct acpi_object_list input; |
| 742 | union acpi_object in_params[1]; |
Kristen Carlson Accardi | 7ea1fbc | 2006-09-28 11:29:12 -0700 | [diff] [blame] | 743 | |
Kristen Carlson Accardi | 7ea1fbc | 2006-09-28 11:29:12 -0700 | [diff] [blame] | 744 | if (ata_msg_probe(ap)) |
Joe Perches | a9a79df | 2011-04-15 15:51:59 -0700 | [diff] [blame] | 745 | ata_dev_dbg(dev, "%s: ix = %d, port#: %d\n", |
| 746 | __func__, dev->devno, ap->port_no); |
Kristen Carlson Accardi | 7ea1fbc | 2006-09-28 11:29:12 -0700 | [diff] [blame] | 747 | |
Kristen Carlson Accardi | 7ea1fbc | 2006-09-28 11:29:12 -0700 | [diff] [blame] | 748 | /* Give the drive Identify data to the drive via the _SDD method */ |
| 749 | /* _SDD: set up input parameters */ |
| 750 | input.count = 1; |
| 751 | input.pointer = in_params; |
| 752 | in_params[0].type = ACPI_TYPE_BUFFER; |
Tejun Heo | 3a32a8e | 2007-05-05 23:50:38 +0900 | [diff] [blame] | 753 | in_params[0].buffer.length = sizeof(dev->id[0]) * ATA_ID_WORDS; |
| 754 | in_params[0].buffer.pointer = (u8 *)dev->id; |
Kristen Carlson Accardi | 7ea1fbc | 2006-09-28 11:29:12 -0700 | [diff] [blame] | 755 | /* Output buffer: _SDD has no output */ |
| 756 | |
| 757 | /* It's OK for _SDD to be missing too. */ |
Tejun Heo | 3a32a8e | 2007-05-05 23:50:38 +0900 | [diff] [blame] | 758 | swap_buf_le16(dev->id, ATA_ID_WORDS); |
Matthew Garrett | 30dcf76 | 2012-06-25 16:13:04 +0800 | [diff] [blame] | 759 | status = acpi_evaluate_object(ata_dev_acpi_handle(dev), "_SDD", &input, |
| 760 | NULL); |
Tejun Heo | 3a32a8e | 2007-05-05 23:50:38 +0900 | [diff] [blame] | 761 | swap_buf_le16(dev->id, ATA_ID_WORDS); |
Kristen Carlson Accardi | 7ea1fbc | 2006-09-28 11:29:12 -0700 | [diff] [blame] | 762 | |
Tejun Heo | f240677 | 2009-11-18 22:24:21 +0900 | [diff] [blame] | 763 | if (status == AE_NOT_FOUND) |
| 764 | return -ENOENT; |
| 765 | |
| 766 | if (ACPI_FAILURE(status)) { |
Joe Perches | a9a79df | 2011-04-15 15:51:59 -0700 | [diff] [blame] | 767 | ata_dev_warn(dev, "ACPI _SDD failed (AE 0x%x)\n", status); |
Tejun Heo | f240677 | 2009-11-18 22:24:21 +0900 | [diff] [blame] | 768 | return -EIO; |
| 769 | } |
Kristen Carlson Accardi | 7ea1fbc | 2006-09-28 11:29:12 -0700 | [diff] [blame] | 770 | |
Tejun Heo | f240677 | 2009-11-18 22:24:21 +0900 | [diff] [blame] | 771 | return 0; |
Kristen Carlson Accardi | 7ea1fbc | 2006-09-28 11:29:12 -0700 | [diff] [blame] | 772 | } |
| 773 | |
Tejun Heo | 6746544 | 2007-05-15 03:28:16 +0900 | [diff] [blame] | 774 | /** |
Tejun Heo | 64578a3 | 2007-05-15 03:28:16 +0900 | [diff] [blame] | 775 | * ata_acpi_on_suspend - ATA ACPI hook called on suspend |
| 776 | * @ap: target ATA port |
| 777 | * |
| 778 | * This function is called when @ap is about to be suspended. All |
| 779 | * devices are already put to sleep but the port_suspend() callback |
| 780 | * hasn't been executed yet. Error return from this function aborts |
| 781 | * suspend. |
| 782 | * |
| 783 | * LOCKING: |
| 784 | * EH context. |
| 785 | * |
| 786 | * RETURNS: |
| 787 | * 0 on success, -errno on failure. |
| 788 | */ |
| 789 | int ata_acpi_on_suspend(struct ata_port *ap) |
| 790 | { |
Tejun Heo | c05e6ff | 2007-12-15 15:05:02 +0900 | [diff] [blame] | 791 | /* nada */ |
| 792 | return 0; |
Tejun Heo | 64578a3 | 2007-05-15 03:28:16 +0900 | [diff] [blame] | 793 | } |
| 794 | |
| 795 | /** |
Tejun Heo | 6746544 | 2007-05-15 03:28:16 +0900 | [diff] [blame] | 796 | * ata_acpi_on_resume - ATA ACPI hook called on resume |
| 797 | * @ap: target ATA port |
| 798 | * |
| 799 | * This function is called when @ap is resumed - right after port |
| 800 | * itself is resumed but before any EH action is taken. |
| 801 | * |
| 802 | * LOCKING: |
| 803 | * EH context. |
| 804 | */ |
| 805 | void ata_acpi_on_resume(struct ata_port *ap) |
| 806 | { |
Tejun Heo | c05e6ff | 2007-12-15 15:05:02 +0900 | [diff] [blame] | 807 | const struct ata_acpi_gtm *gtm = ata_acpi_init_gtm(ap); |
Tejun Heo | f58229f | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 808 | struct ata_device *dev; |
Kristen Carlson Accardi | 7ea1fbc | 2006-09-28 11:29:12 -0700 | [diff] [blame] | 809 | |
Matthew Garrett | 30dcf76 | 2012-06-25 16:13:04 +0800 | [diff] [blame] | 810 | if (ata_ap_acpi_handle(ap) && gtm) { |
Tejun Heo | 398e078 | 2007-12-15 15:05:03 +0900 | [diff] [blame] | 811 | /* _GTM valid */ |
| 812 | |
| 813 | /* restore timing parameters */ |
Tejun Heo | c05e6ff | 2007-12-15 15:05:02 +0900 | [diff] [blame] | 814 | ata_acpi_stm(ap, gtm); |
Tejun Heo | 64578a3 | 2007-05-15 03:28:16 +0900 | [diff] [blame] | 815 | |
Tejun Heo | 398e078 | 2007-12-15 15:05:03 +0900 | [diff] [blame] | 816 | /* _GTF should immediately follow _STM so that it can |
| 817 | * use values set by _STM. Cache _GTF result and |
| 818 | * schedule _GTF. |
| 819 | */ |
Tejun Heo | 1eca436 | 2008-11-03 20:03:17 +0900 | [diff] [blame] | 820 | ata_for_each_dev(dev, &ap->link, ALL) { |
Tejun Heo | 398e078 | 2007-12-15 15:05:03 +0900 | [diff] [blame] | 821 | ata_acpi_clear_gtf(dev); |
Shaohua Li | 48feb3c | 2008-03-25 16:50:45 +0800 | [diff] [blame] | 822 | if (ata_dev_enabled(dev) && |
| 823 | ata_dev_get_GTF(dev, NULL) >= 0) |
Tejun Heo | 398e078 | 2007-12-15 15:05:03 +0900 | [diff] [blame] | 824 | dev->flags |= ATA_DFLAG_ACPI_PENDING; |
| 825 | } |
| 826 | } else { |
| 827 | /* SATA _GTF needs to be evaulated after _SDD and |
| 828 | * there's no reason to evaluate IDE _GTF early |
| 829 | * without _STM. Clear cache and schedule _GTF. |
| 830 | */ |
Tejun Heo | 1eca436 | 2008-11-03 20:03:17 +0900 | [diff] [blame] | 831 | ata_for_each_dev(dev, &ap->link, ALL) { |
Tejun Heo | 398e078 | 2007-12-15 15:05:03 +0900 | [diff] [blame] | 832 | ata_acpi_clear_gtf(dev); |
Shaohua Li | 48feb3c | 2008-03-25 16:50:45 +0800 | [diff] [blame] | 833 | if (ata_dev_enabled(dev)) |
| 834 | dev->flags |= ATA_DFLAG_ACPI_PENDING; |
Tejun Heo | 398e078 | 2007-12-15 15:05:03 +0900 | [diff] [blame] | 835 | } |
| 836 | } |
Tejun Heo | 6746544 | 2007-05-15 03:28:16 +0900 | [diff] [blame] | 837 | } |
| 838 | |
Aaron Lu | a7ff60d | 2013-01-25 14:29:35 +0800 | [diff] [blame] | 839 | 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] | 840 | { |
Aaron Lu | 2133420 | 2013-01-15 17:21:01 +0800 | [diff] [blame] | 841 | int d_max_in = ACPI_STATE_D3_COLD; |
Aaron Lu | a7ff60d | 2013-01-25 14:29:35 +0800 | [diff] [blame] | 842 | if (!runtime) |
| 843 | goto out; |
Aaron Lu | 2133420 | 2013-01-15 17:21:01 +0800 | [diff] [blame] | 844 | |
| 845 | /* |
| 846 | * For ATAPI, runtime D3 cold is only allowed |
| 847 | * for ZPODD in zero power ready state |
| 848 | */ |
| 849 | if (dev->class == ATA_DEV_ATAPI && |
| 850 | !(zpodd_dev_enabled(dev) && zpodd_zpready(dev))) |
| 851 | d_max_in = ACPI_STATE_D3_HOT; |
| 852 | |
Aaron Lu | a7ff60d | 2013-01-25 14:29:35 +0800 | [diff] [blame] | 853 | out: |
Aaron Lu | 2133420 | 2013-01-15 17:21:01 +0800 | [diff] [blame] | 854 | return acpi_pm_device_sleep_state(&dev->sdev->sdev_gendev, |
| 855 | NULL, d_max_in); |
| 856 | } |
| 857 | |
Aaron Lu | a7ff60d | 2013-01-25 14:29:35 +0800 | [diff] [blame] | 858 | static void sata_acpi_set_state(struct ata_port *ap, pm_message_t state) |
| 859 | { |
| 860 | bool runtime = PMSG_IS_AUTO(state); |
Shaohua Li | bd3adca | 2007-11-02 09:32:38 +0800 | [diff] [blame] | 861 | struct ata_device *dev; |
Lin Ming | febe53b | 2012-06-25 16:13:05 +0800 | [diff] [blame] | 862 | acpi_handle handle; |
Lin Ming | 3bd4660 | 2012-06-25 16:13:06 +0800 | [diff] [blame] | 863 | int acpi_state; |
Shaohua Li | bd3adca | 2007-11-02 09:32:38 +0800 | [diff] [blame] | 864 | |
Tejun Heo | 1eca436 | 2008-11-03 20:03:17 +0900 | [diff] [blame] | 865 | ata_for_each_dev(dev, &ap->link, ENABLED) { |
Lin Ming | febe53b | 2012-06-25 16:13:05 +0800 | [diff] [blame] | 866 | handle = ata_dev_acpi_handle(dev); |
Lin Ming | 3bd4660 | 2012-06-25 16:13:06 +0800 | [diff] [blame] | 867 | if (!handle) |
| 868 | continue; |
| 869 | |
Aaron Lu | a7ff60d | 2013-01-25 14:29:35 +0800 | [diff] [blame] | 870 | if (!(state.event & PM_EVENT_RESUME)) { |
| 871 | acpi_state = ata_acpi_choose_suspend_state(dev, runtime); |
| 872 | if (acpi_state == ACPI_STATE_D0) |
| 873 | continue; |
| 874 | if (runtime && zpodd_dev_enabled(dev) && |
| 875 | acpi_state == ACPI_STATE_D3_COLD) |
| 876 | zpodd_enable_run_wake(dev); |
| 877 | acpi_bus_set_power(handle, acpi_state); |
Lin Ming | 3bd4660 | 2012-06-25 16:13:06 +0800 | [diff] [blame] | 878 | } else { |
Aaron Lu | a7ff60d | 2013-01-25 14:29:35 +0800 | [diff] [blame] | 879 | if (runtime && zpodd_dev_enabled(dev)) |
| 880 | zpodd_disable_run_wake(dev); |
Lin Ming | 3bd4660 | 2012-06-25 16:13:06 +0800 | [diff] [blame] | 881 | acpi_bus_set_power(handle, ACPI_STATE_D0); |
| 882 | } |
Shaohua Li | bd3adca | 2007-11-02 09:32:38 +0800 | [diff] [blame] | 883 | } |
Aaron Lu | a7ff60d | 2013-01-25 14:29:35 +0800 | [diff] [blame] | 884 | } |
Lin Ming | febe53b | 2012-06-25 16:13:05 +0800 | [diff] [blame] | 885 | |
Aaron Lu | a7ff60d | 2013-01-25 14:29:35 +0800 | [diff] [blame] | 886 | /* ACPI spec requires _PS0 when IDE power on and _PS3 when power off */ |
| 887 | static void pata_acpi_set_state(struct ata_port *ap, pm_message_t state) |
| 888 | { |
| 889 | struct ata_device *dev; |
| 890 | acpi_handle port_handle; |
| 891 | |
| 892 | port_handle = ata_ap_acpi_handle(ap); |
| 893 | if (!port_handle) |
| 894 | return; |
| 895 | |
| 896 | /* channel first and then drives for power on and vica versa |
| 897 | for power off */ |
| 898 | if (state.event & PM_EVENT_RESUME) |
| 899 | acpi_bus_set_power(port_handle, ACPI_STATE_D0); |
| 900 | |
| 901 | ata_for_each_dev(dev, &ap->link, ENABLED) { |
| 902 | acpi_handle dev_handle = ata_dev_acpi_handle(dev); |
| 903 | if (!dev_handle) |
| 904 | continue; |
| 905 | |
| 906 | acpi_bus_set_power(dev_handle, state.event & PM_EVENT_RESUME ? |
| 907 | ACPI_STATE_D0 : ACPI_STATE_D3); |
| 908 | } |
| 909 | |
| 910 | if (!(state.event & PM_EVENT_RESUME)) |
| 911 | acpi_bus_set_power(port_handle, ACPI_STATE_D3); |
| 912 | } |
| 913 | |
Kristen Carlson Accardi | 11ef697 | 2006-09-28 11:29:01 -0700 | [diff] [blame] | 914 | /** |
| 915 | * ata_acpi_set_state - set the port power state |
| 916 | * @ap: target ATA port |
| 917 | * @state: state, on/off |
| 918 | * |
Aaron Lu | a7ff60d | 2013-01-25 14:29:35 +0800 | [diff] [blame] | 919 | * This function sets a proper ACPI D state for the device on |
| 920 | * system and runtime PM operations. |
Kristen Carlson Accardi | 11ef697 | 2006-09-28 11:29:01 -0700 | [diff] [blame] | 921 | */ |
| 922 | void ata_acpi_set_state(struct ata_port *ap, pm_message_t state) |
| 923 | { |
Aaron Lu | a7ff60d | 2013-01-25 14:29:35 +0800 | [diff] [blame] | 924 | if (ap->flags & ATA_FLAG_ACPI_SATA) |
| 925 | sata_acpi_set_state(ap, state); |
| 926 | else |
| 927 | pata_acpi_set_state(ap, state); |
Shaohua Li | bd3adca | 2007-11-02 09:32:38 +0800 | [diff] [blame] | 928 | } |
| 929 | |
| 930 | /** |
Tejun Heo | 6746544 | 2007-05-15 03:28:16 +0900 | [diff] [blame] | 931 | * ata_acpi_on_devcfg - ATA ACPI hook called on device donfiguration |
| 932 | * @dev: target ATA device |
| 933 | * |
| 934 | * This function is called when @dev is about to be configured. |
| 935 | * IDENTIFY data might have been modified after this hook is run. |
| 936 | * |
| 937 | * LOCKING: |
| 938 | * EH context. |
| 939 | * |
| 940 | * RETURNS: |
| 941 | * Positive number if IDENTIFY data needs to be refreshed, 0 if not, |
| 942 | * -errno on failure. |
| 943 | */ |
| 944 | int ata_acpi_on_devcfg(struct ata_device *dev) |
| 945 | { |
Tejun Heo | 9af5c9c | 2007-08-06 18:36:22 +0900 | [diff] [blame] | 946 | struct ata_port *ap = dev->link->ap; |
| 947 | struct ata_eh_context *ehc = &ap->link.eh_context; |
Tejun Heo | 6746544 | 2007-05-15 03:28:16 +0900 | [diff] [blame] | 948 | int acpi_sata = ap->flags & ATA_FLAG_ACPI_SATA; |
Tejun Heo | 66fa7f2 | 2007-12-15 15:05:04 +0900 | [diff] [blame] | 949 | int nr_executed = 0; |
Tejun Heo | 6746544 | 2007-05-15 03:28:16 +0900 | [diff] [blame] | 950 | int rc; |
| 951 | |
Matthew Garrett | 30dcf76 | 2012-06-25 16:13:04 +0800 | [diff] [blame] | 952 | if (!ata_dev_acpi_handle(dev)) |
Tejun Heo | 6746544 | 2007-05-15 03:28:16 +0900 | [diff] [blame] | 953 | return 0; |
| 954 | |
| 955 | /* do we need to do _GTF? */ |
| 956 | if (!(dev->flags & ATA_DFLAG_ACPI_PENDING) && |
| 957 | !(acpi_sata && (ehc->i.flags & ATA_EHI_DID_HARDRESET))) |
| 958 | return 0; |
| 959 | |
| 960 | /* do _SDD if SATA */ |
| 961 | if (acpi_sata) { |
| 962 | rc = ata_acpi_push_id(dev); |
Tejun Heo | f240677 | 2009-11-18 22:24:21 +0900 | [diff] [blame] | 963 | if (rc && rc != -ENOENT) |
Tejun Heo | 6746544 | 2007-05-15 03:28:16 +0900 | [diff] [blame] | 964 | goto acpi_err; |
| 965 | } |
| 966 | |
| 967 | /* do _GTF */ |
Tejun Heo | 66fa7f2 | 2007-12-15 15:05:04 +0900 | [diff] [blame] | 968 | rc = ata_acpi_exec_tfs(dev, &nr_executed); |
| 969 | if (rc) |
Tejun Heo | 6746544 | 2007-05-15 03:28:16 +0900 | [diff] [blame] | 970 | goto acpi_err; |
| 971 | |
| 972 | dev->flags &= ~ATA_DFLAG_ACPI_PENDING; |
| 973 | |
| 974 | /* refresh IDENTIFY page if any _GTF command has been executed */ |
Tejun Heo | 66fa7f2 | 2007-12-15 15:05:04 +0900 | [diff] [blame] | 975 | if (nr_executed) { |
Tejun Heo | 6746544 | 2007-05-15 03:28:16 +0900 | [diff] [blame] | 976 | rc = ata_dev_reread_id(dev, 0); |
| 977 | if (rc < 0) { |
Joe Perches | a9a79df | 2011-04-15 15:51:59 -0700 | [diff] [blame] | 978 | ata_dev_err(dev, |
| 979 | "failed to IDENTIFY after ACPI commands\n"); |
Tejun Heo | 6746544 | 2007-05-15 03:28:16 +0900 | [diff] [blame] | 980 | return rc; |
| 981 | } |
| 982 | } |
| 983 | |
| 984 | return 0; |
| 985 | |
| 986 | acpi_err: |
Tejun Heo | 66fa7f2 | 2007-12-15 15:05:04 +0900 | [diff] [blame] | 987 | /* ignore evaluation failure if we can continue safely */ |
| 988 | if (rc == -EINVAL && !nr_executed && !(ap->pflags & ATA_PFLAG_FROZEN)) |
| 989 | return 0; |
Tejun Heo | 6746544 | 2007-05-15 03:28:16 +0900 | [diff] [blame] | 990 | |
Tejun Heo | 66fa7f2 | 2007-12-15 15:05:04 +0900 | [diff] [blame] | 991 | /* fail and let EH retry once more for unknown IO errors */ |
| 992 | if (!(dev->flags & ATA_DFLAG_ACPI_FAILED)) { |
| 993 | dev->flags |= ATA_DFLAG_ACPI_FAILED; |
| 994 | return rc; |
Tejun Heo | 6746544 | 2007-05-15 03:28:16 +0900 | [diff] [blame] | 995 | } |
Tejun Heo | 66fa7f2 | 2007-12-15 15:05:04 +0900 | [diff] [blame] | 996 | |
Aaron Lu | 0d0cdb0 | 2012-11-26 13:55:25 +0800 | [diff] [blame] | 997 | dev->flags |= ATA_DFLAG_ACPI_DISABLED; |
Joe Perches | a9a79df | 2011-04-15 15:51:59 -0700 | [diff] [blame] | 998 | ata_dev_warn(dev, "ACPI: failed the second time, disabled\n"); |
Tejun Heo | 66fa7f2 | 2007-12-15 15:05:04 +0900 | [diff] [blame] | 999 | |
| 1000 | /* We can safely continue if no _GTF command has been executed |
| 1001 | * and port is not frozen. |
| 1002 | */ |
| 1003 | if (!nr_executed && !(ap->pflags & ATA_PFLAG_FROZEN)) |
| 1004 | return 0; |
| 1005 | |
Tejun Heo | 6746544 | 2007-05-15 03:28:16 +0900 | [diff] [blame] | 1006 | return rc; |
| 1007 | } |
Tejun Heo | 562f0c2 | 2007-12-15 15:05:01 +0900 | [diff] [blame] | 1008 | |
| 1009 | /** |
| 1010 | * ata_acpi_on_disable - ATA ACPI hook called when a device is disabled |
| 1011 | * @dev: target ATA device |
| 1012 | * |
| 1013 | * This function is called when @dev is about to be disabled. |
| 1014 | * |
| 1015 | * LOCKING: |
| 1016 | * EH context. |
| 1017 | */ |
| 1018 | void ata_acpi_on_disable(struct ata_device *dev) |
| 1019 | { |
Tejun Heo | 398e078 | 2007-12-15 15:05:03 +0900 | [diff] [blame] | 1020 | ata_acpi_clear_gtf(dev); |
Tejun Heo | 562f0c2 | 2007-12-15 15:05:01 +0900 | [diff] [blame] | 1021 | } |
Matthew Garrett | 6b66d95 | 2012-06-25 16:13:03 +0800 | [diff] [blame] | 1022 | |
Lin Ming | a606dac3 | 2012-06-25 16:13:07 +0800 | [diff] [blame] | 1023 | static void ata_acpi_register_power_resource(struct ata_device *dev) |
| 1024 | { |
| 1025 | struct scsi_device *sdev = dev->sdev; |
| 1026 | acpi_handle handle; |
Lin Ming | a606dac3 | 2012-06-25 16:13:07 +0800 | [diff] [blame] | 1027 | |
| 1028 | handle = ata_dev_acpi_handle(dev); |
Rafael J. Wysocki | bc9b640 | 2013-01-17 14:11:05 +0100 | [diff] [blame] | 1029 | if (handle) |
| 1030 | acpi_dev_pm_remove_dependent(handle, &sdev->sdev_gendev); |
Lin Ming | a606dac3 | 2012-06-25 16:13:07 +0800 | [diff] [blame] | 1031 | } |
| 1032 | |
| 1033 | static void ata_acpi_unregister_power_resource(struct ata_device *dev) |
| 1034 | { |
| 1035 | struct scsi_device *sdev = dev->sdev; |
| 1036 | acpi_handle handle; |
Lin Ming | a606dac3 | 2012-06-25 16:13:07 +0800 | [diff] [blame] | 1037 | |
| 1038 | handle = ata_dev_acpi_handle(dev); |
Rafael J. Wysocki | bc9b640 | 2013-01-17 14:11:05 +0100 | [diff] [blame] | 1039 | if (handle) |
| 1040 | acpi_dev_pm_remove_dependent(handle, &sdev->sdev_gendev); |
Lin Ming | a606dac3 | 2012-06-25 16:13:07 +0800 | [diff] [blame] | 1041 | } |
| 1042 | |
Lin Ming | 3bd4660 | 2012-06-25 16:13:06 +0800 | [diff] [blame] | 1043 | void ata_acpi_bind(struct ata_device *dev) |
| 1044 | { |
Lin Ming | a606dac3 | 2012-06-25 16:13:07 +0800 | [diff] [blame] | 1045 | ata_acpi_register_power_resource(dev); |
Aaron Lu | a59b9aa | 2013-01-15 17:21:02 +0800 | [diff] [blame] | 1046 | if (zpodd_dev_enabled(dev)) |
| 1047 | dev_pm_qos_expose_flags(&dev->sdev->sdev_gendev, 0); |
Lin Ming | 3bd4660 | 2012-06-25 16:13:06 +0800 | [diff] [blame] | 1048 | } |
| 1049 | |
| 1050 | void ata_acpi_unbind(struct ata_device *dev) |
| 1051 | { |
Lin Ming | a606dac3 | 2012-06-25 16:13:07 +0800 | [diff] [blame] | 1052 | ata_acpi_unregister_power_resource(dev); |
Lin Ming | 3bd4660 | 2012-06-25 16:13:06 +0800 | [diff] [blame] | 1053 | } |
| 1054 | |
Matthew Garrett | 6b66d95 | 2012-06-25 16:13:03 +0800 | [diff] [blame] | 1055 | static int compat_pci_ata(struct ata_port *ap) |
| 1056 | { |
| 1057 | struct device *dev = ap->tdev.parent; |
| 1058 | struct pci_dev *pdev; |
| 1059 | |
| 1060 | if (!is_pci_dev(dev)) |
| 1061 | return 0; |
| 1062 | |
| 1063 | pdev = to_pci_dev(dev); |
| 1064 | |
| 1065 | if ((pdev->class >> 8) != PCI_CLASS_STORAGE_SATA && |
| 1066 | (pdev->class >> 8) != PCI_CLASS_STORAGE_IDE) |
| 1067 | return 0; |
| 1068 | |
| 1069 | return 1; |
| 1070 | } |
| 1071 | |
| 1072 | static int ata_acpi_bind_host(struct ata_port *ap, acpi_handle *handle) |
| 1073 | { |
| 1074 | if (ap->flags & ATA_FLAG_ACPI_SATA) |
| 1075 | return -ENODEV; |
| 1076 | |
| 1077 | *handle = acpi_get_child(DEVICE_ACPI_HANDLE(ap->tdev.parent), |
| 1078 | ap->port_no); |
| 1079 | |
| 1080 | if (!*handle) |
| 1081 | return -ENODEV; |
| 1082 | |
Aaron Lu | 8340091 | 2012-08-15 17:08:12 +0800 | [diff] [blame] | 1083 | if (ata_acpi_gtm(ap, &ap->__acpi_init_gtm) == 0) |
| 1084 | ap->pflags |= ATA_PFLAG_INIT_GTM_VALID; |
| 1085 | |
Matthew Garrett | 6b66d95 | 2012-06-25 16:13:03 +0800 | [diff] [blame] | 1086 | return 0; |
| 1087 | } |
| 1088 | |
| 1089 | static int ata_acpi_bind_device(struct ata_port *ap, struct scsi_device *sdev, |
| 1090 | acpi_handle *handle) |
| 1091 | { |
| 1092 | struct ata_device *ata_dev; |
| 1093 | |
Aaron Lu | 60817a6 | 2012-10-09 15:37:48 +0800 | [diff] [blame] | 1094 | if (ap->flags & ATA_FLAG_ACPI_SATA) { |
| 1095 | if (!sata_pmp_attached(ap)) |
| 1096 | ata_dev = &ap->link.device[sdev->id]; |
| 1097 | else |
| 1098 | ata_dev = &ap->pmp_link[sdev->channel].device[sdev->id]; |
| 1099 | } |
| 1100 | else { |
Matthew Garrett | 6b66d95 | 2012-06-25 16:13:03 +0800 | [diff] [blame] | 1101 | ata_dev = &ap->link.device[sdev->id]; |
Aaron Lu | 60817a6 | 2012-10-09 15:37:48 +0800 | [diff] [blame] | 1102 | } |
Matthew Garrett | 6b66d95 | 2012-06-25 16:13:03 +0800 | [diff] [blame] | 1103 | |
Matthew Garrett | 30dcf76 | 2012-06-25 16:13:04 +0800 | [diff] [blame] | 1104 | *handle = ata_dev_acpi_handle(ata_dev); |
Matthew Garrett | 6b66d95 | 2012-06-25 16:13:03 +0800 | [diff] [blame] | 1105 | |
| 1106 | if (!*handle) |
| 1107 | return -ENODEV; |
| 1108 | |
| 1109 | return 0; |
| 1110 | } |
| 1111 | |
| 1112 | static int is_ata_port(const struct device *dev) |
| 1113 | { |
| 1114 | return dev->type == &ata_port_type; |
| 1115 | } |
| 1116 | |
| 1117 | static struct ata_port *dev_to_ata_port(struct device *dev) |
| 1118 | { |
| 1119 | while (!is_ata_port(dev)) { |
| 1120 | if (!dev->parent) |
| 1121 | return NULL; |
| 1122 | dev = dev->parent; |
| 1123 | } |
| 1124 | return to_ata_port(dev); |
| 1125 | } |
| 1126 | |
| 1127 | static int ata_acpi_find_device(struct device *dev, acpi_handle *handle) |
| 1128 | { |
| 1129 | struct ata_port *ap = dev_to_ata_port(dev); |
| 1130 | |
| 1131 | if (!ap) |
| 1132 | return -ENODEV; |
| 1133 | |
| 1134 | if (!compat_pci_ata(ap)) |
| 1135 | return -ENODEV; |
| 1136 | |
| 1137 | if (scsi_is_host_device(dev)) |
| 1138 | return ata_acpi_bind_host(ap, handle); |
| 1139 | else if (scsi_is_sdev_device(dev)) { |
| 1140 | struct scsi_device *sdev = to_scsi_device(dev); |
| 1141 | |
| 1142 | return ata_acpi_bind_device(ap, sdev, handle); |
| 1143 | } else |
| 1144 | return -ENODEV; |
| 1145 | } |
| 1146 | |
| 1147 | static int ata_acpi_find_dummy(struct device *dev, acpi_handle *handle) |
| 1148 | { |
| 1149 | return -ENODEV; |
| 1150 | } |
| 1151 | |
| 1152 | static struct acpi_bus_type ata_acpi_bus = { |
Rafael J. Wysocki | 5354009 | 2013-03-03 22:35:20 +0100 | [diff] [blame^] | 1153 | .name = "ATA", |
Matthew Garrett | 6b66d95 | 2012-06-25 16:13:03 +0800 | [diff] [blame] | 1154 | .find_bridge = ata_acpi_find_dummy, |
| 1155 | .find_device = ata_acpi_find_device, |
| 1156 | }; |
| 1157 | |
| 1158 | int ata_acpi_register(void) |
| 1159 | { |
| 1160 | return scsi_register_acpi_bus_type(&ata_acpi_bus); |
| 1161 | } |
| 1162 | |
| 1163 | void ata_acpi_unregister(void) |
| 1164 | { |
| 1165 | scsi_unregister_acpi_bus_type(&ata_acpi_bus); |
| 1166 | } |