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