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