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