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