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 | |
| 9 | #include <linux/ata.h> |
| 10 | #include <linux/delay.h> |
| 11 | #include <linux/device.h> |
| 12 | #include <linux/errno.h> |
| 13 | #include <linux/kernel.h> |
| 14 | #include <linux/acpi.h> |
| 15 | #include <linux/libata.h> |
| 16 | #include <linux/pci.h> |
| 17 | #include "libata.h" |
| 18 | |
| 19 | #include <acpi/acpi_bus.h> |
| 20 | #include <acpi/acnames.h> |
| 21 | #include <acpi/acnamesp.h> |
| 22 | #include <acpi/acparser.h> |
| 23 | #include <acpi/acexcep.h> |
| 24 | #include <acpi/acmacros.h> |
| 25 | #include <acpi/actypes.h> |
| 26 | |
Kristen Carlson Accardi | 11ef697 | 2006-09-28 11:29:01 -0700 | [diff] [blame] | 27 | #define NO_PORT_MULT 0xffff |
Tejun Heo | fafbae8 | 2007-05-15 03:28:16 +0900 | [diff] [blame] | 28 | #define SATA_ADR(root,pmp) (((root) << 16) | (pmp)) |
Kristen Carlson Accardi | 11ef697 | 2006-09-28 11:29:01 -0700 | [diff] [blame] | 29 | |
| 30 | #define REGS_PER_GTF 7 |
Tejun Heo | 4700c4b | 2007-05-15 03:28:16 +0900 | [diff] [blame] | 31 | struct ata_acpi_gtf { |
| 32 | u8 tf[REGS_PER_GTF]; /* regs. 0x1f1 - 0x1f7 */ |
| 33 | } __packed; |
Kristen Carlson Accardi | 11ef697 | 2006-09-28 11:29:01 -0700 | [diff] [blame] | 34 | |
Alan Cox | ca42663 | 2007-03-08 23:13:50 +0000 | [diff] [blame] | 35 | /* |
| 36 | * Helper - belongs in the PCI layer somewhere eventually |
| 37 | */ |
| 38 | static int is_pci_dev(struct device *dev) |
| 39 | { |
| 40 | return (dev->bus == &pci_bus_type); |
| 41 | } |
Kristen Carlson Accardi | 11ef697 | 2006-09-28 11:29:01 -0700 | [diff] [blame] | 42 | |
Tejun Heo | fafbae8 | 2007-05-15 03:28:16 +0900 | [diff] [blame] | 43 | static void ata_acpi_associate_sata_port(struct ata_port *ap) |
Kristen Carlson Accardi | 11ef697 | 2006-09-28 11:29:01 -0700 | [diff] [blame] | 44 | { |
Tejun Heo | fafbae8 | 2007-05-15 03:28:16 +0900 | [diff] [blame] | 45 | acpi_integer adr = SATA_ADR(ap->port_no, NO_PORT_MULT); |
Kristen Carlson Accardi | 11ef697 | 2006-09-28 11:29:01 -0700 | [diff] [blame] | 46 | |
Tejun Heo | 9af5c9c | 2007-08-06 18:36:22 +0900 | [diff] [blame] | 47 | ap->link.device->acpi_handle = |
| 48 | acpi_get_child(ap->host->acpi_handle, adr); |
Tejun Heo | fafbae8 | 2007-05-15 03:28:16 +0900 | [diff] [blame] | 49 | } |
Alan Cox | ca42663 | 2007-03-08 23:13:50 +0000 | [diff] [blame] | 50 | |
Tejun Heo | fafbae8 | 2007-05-15 03:28:16 +0900 | [diff] [blame] | 51 | static void ata_acpi_associate_ide_port(struct ata_port *ap) |
| 52 | { |
| 53 | int max_devices, i; |
| 54 | |
| 55 | ap->acpi_handle = acpi_get_child(ap->host->acpi_handle, ap->port_no); |
| 56 | if (!ap->acpi_handle) |
| 57 | return; |
| 58 | |
| 59 | max_devices = 1; |
| 60 | if (ap->flags & ATA_FLAG_SLAVE_POSS) |
| 61 | max_devices++; |
| 62 | |
| 63 | for (i = 0; i < max_devices; i++) { |
Tejun Heo | 9af5c9c | 2007-08-06 18:36:22 +0900 | [diff] [blame] | 64 | struct ata_device *dev = &ap->link.device[i]; |
Tejun Heo | fafbae8 | 2007-05-15 03:28:16 +0900 | [diff] [blame] | 65 | |
| 66 | dev->acpi_handle = acpi_get_child(ap->acpi_handle, i); |
| 67 | } |
Kristen Carlson Accardi | 11ef697 | 2006-09-28 11:29:01 -0700 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | /** |
Tejun Heo | fafbae8 | 2007-05-15 03:28:16 +0900 | [diff] [blame] | 71 | * ata_acpi_associate - associate ATA host with ACPI objects |
| 72 | * @host: target ATA host |
Kristen Carlson Accardi | 11ef697 | 2006-09-28 11:29:01 -0700 | [diff] [blame] | 73 | * |
Tejun Heo | fafbae8 | 2007-05-15 03:28:16 +0900 | [diff] [blame] | 74 | * Look up ACPI objects associated with @host and initialize |
| 75 | * acpi_handle fields of @host, its ports and devices accordingly. |
Kristen Carlson Accardi | 11ef697 | 2006-09-28 11:29:01 -0700 | [diff] [blame] | 76 | * |
Tejun Heo | fafbae8 | 2007-05-15 03:28:16 +0900 | [diff] [blame] | 77 | * LOCKING: |
| 78 | * EH context. |
| 79 | * |
| 80 | * RETURNS: |
| 81 | * 0 on success, -errno on failure. |
Kristen Carlson Accardi | 11ef697 | 2006-09-28 11:29:01 -0700 | [diff] [blame] | 82 | */ |
Tejun Heo | fafbae8 | 2007-05-15 03:28:16 +0900 | [diff] [blame] | 83 | void ata_acpi_associate(struct ata_host *host) |
Kristen Carlson Accardi | 11ef697 | 2006-09-28 11:29:01 -0700 | [diff] [blame] | 84 | { |
Tejun Heo | fafbae8 | 2007-05-15 03:28:16 +0900 | [diff] [blame] | 85 | int i; |
Alan Cox | ca42663 | 2007-03-08 23:13:50 +0000 | [diff] [blame] | 86 | |
Tejun Heo | fafbae8 | 2007-05-15 03:28:16 +0900 | [diff] [blame] | 87 | if (!is_pci_dev(host->dev) || libata_noacpi) |
| 88 | return; |
Alan Cox | ca42663 | 2007-03-08 23:13:50 +0000 | [diff] [blame] | 89 | |
Tejun Heo | fafbae8 | 2007-05-15 03:28:16 +0900 | [diff] [blame] | 90 | host->acpi_handle = DEVICE_ACPI_HANDLE(host->dev); |
| 91 | if (!host->acpi_handle) |
| 92 | return; |
Kristen Carlson Accardi | 11ef697 | 2006-09-28 11:29:01 -0700 | [diff] [blame] | 93 | |
Tejun Heo | fafbae8 | 2007-05-15 03:28:16 +0900 | [diff] [blame] | 94 | for (i = 0; i < host->n_ports; i++) { |
| 95 | struct ata_port *ap = host->ports[i]; |
Kristen Carlson Accardi | 11ef697 | 2006-09-28 11:29:01 -0700 | [diff] [blame] | 96 | |
Tejun Heo | fafbae8 | 2007-05-15 03:28:16 +0900 | [diff] [blame] | 97 | if (host->ports[0]->flags & ATA_FLAG_ACPI_SATA) |
| 98 | ata_acpi_associate_sata_port(ap); |
| 99 | else |
| 100 | ata_acpi_associate_ide_port(ap); |
Kristen Carlson Accardi | 11ef697 | 2006-09-28 11:29:01 -0700 | [diff] [blame] | 101 | } |
Kristen Carlson Accardi | 11ef697 | 2006-09-28 11:29:01 -0700 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | /** |
Tejun Heo | 64578a3 | 2007-05-15 03:28:16 +0900 | [diff] [blame] | 105 | * ata_acpi_gtm - execute _GTM |
| 106 | * @ap: target ATA port |
| 107 | * @gtm: out parameter for _GTM result |
| 108 | * |
| 109 | * Evaluate _GTM and store the result in @gtm. |
| 110 | * |
| 111 | * LOCKING: |
| 112 | * EH context. |
| 113 | * |
| 114 | * RETURNS: |
| 115 | * 0 on success, -ENOENT if _GTM doesn't exist, -errno on failure. |
| 116 | */ |
| 117 | static int ata_acpi_gtm(const struct ata_port *ap, struct ata_acpi_gtm *gtm) |
| 118 | { |
| 119 | struct acpi_buffer output = { .length = ACPI_ALLOCATE_BUFFER }; |
| 120 | union acpi_object *out_obj; |
| 121 | acpi_status status; |
| 122 | int rc = 0; |
| 123 | |
| 124 | status = acpi_evaluate_object(ap->acpi_handle, "_GTM", NULL, &output); |
| 125 | |
| 126 | rc = -ENOENT; |
| 127 | if (status == AE_NOT_FOUND) |
| 128 | goto out_free; |
| 129 | |
| 130 | rc = -EINVAL; |
| 131 | if (ACPI_FAILURE(status)) { |
| 132 | ata_port_printk(ap, KERN_ERR, |
| 133 | "ACPI get timing mode failed (AE 0x%x)\n", |
| 134 | status); |
| 135 | goto out_free; |
| 136 | } |
| 137 | |
| 138 | out_obj = output.pointer; |
| 139 | if (out_obj->type != ACPI_TYPE_BUFFER) { |
| 140 | ata_port_printk(ap, KERN_WARNING, |
| 141 | "_GTM returned unexpected object type 0x%x\n", |
| 142 | out_obj->type); |
| 143 | |
| 144 | goto out_free; |
| 145 | } |
| 146 | |
| 147 | if (out_obj->buffer.length != sizeof(struct ata_acpi_gtm)) { |
| 148 | ata_port_printk(ap, KERN_ERR, |
| 149 | "_GTM returned invalid length %d\n", |
| 150 | out_obj->buffer.length); |
| 151 | goto out_free; |
| 152 | } |
| 153 | |
| 154 | memcpy(gtm, out_obj->buffer.pointer, sizeof(struct ata_acpi_gtm)); |
| 155 | rc = 0; |
| 156 | out_free: |
| 157 | kfree(output.pointer); |
| 158 | return rc; |
| 159 | } |
| 160 | |
| 161 | /** |
| 162 | * ata_acpi_stm - execute _STM |
| 163 | * @ap: target ATA port |
| 164 | * @stm: timing parameter to _STM |
| 165 | * |
| 166 | * Evaluate _STM with timing parameter @stm. |
| 167 | * |
| 168 | * LOCKING: |
| 169 | * EH context. |
| 170 | * |
| 171 | * RETURNS: |
| 172 | * 0 on success, -ENOENT if _STM doesn't exist, -errno on failure. |
| 173 | */ |
| 174 | static int ata_acpi_stm(const struct ata_port *ap, struct ata_acpi_gtm *stm) |
| 175 | { |
| 176 | acpi_status status; |
| 177 | struct acpi_object_list input; |
| 178 | union acpi_object in_params[3]; |
| 179 | |
| 180 | in_params[0].type = ACPI_TYPE_BUFFER; |
| 181 | in_params[0].buffer.length = sizeof(struct ata_acpi_gtm); |
| 182 | in_params[0].buffer.pointer = (u8 *)stm; |
| 183 | /* Buffers for id may need byteswapping ? */ |
| 184 | in_params[1].type = ACPI_TYPE_BUFFER; |
| 185 | in_params[1].buffer.length = 512; |
Tejun Heo | 9af5c9c | 2007-08-06 18:36:22 +0900 | [diff] [blame] | 186 | in_params[1].buffer.pointer = (u8 *)ap->link.device[0].id; |
Tejun Heo | 64578a3 | 2007-05-15 03:28:16 +0900 | [diff] [blame] | 187 | in_params[2].type = ACPI_TYPE_BUFFER; |
| 188 | in_params[2].buffer.length = 512; |
Tejun Heo | 9af5c9c | 2007-08-06 18:36:22 +0900 | [diff] [blame] | 189 | in_params[2].buffer.pointer = (u8 *)ap->link.device[1].id; |
Tejun Heo | 64578a3 | 2007-05-15 03:28:16 +0900 | [diff] [blame] | 190 | |
| 191 | input.count = 3; |
| 192 | input.pointer = in_params; |
| 193 | |
| 194 | status = acpi_evaluate_object(ap->acpi_handle, "_STM", &input, NULL); |
| 195 | |
| 196 | if (status == AE_NOT_FOUND) |
| 197 | return -ENOENT; |
| 198 | if (ACPI_FAILURE(status)) { |
| 199 | ata_port_printk(ap, KERN_ERR, |
| 200 | "ACPI set timing mode failed (status=0x%x)\n", status); |
| 201 | return -EINVAL; |
| 202 | } |
| 203 | return 0; |
| 204 | } |
| 205 | |
| 206 | /** |
Tejun Heo | 4700c4b | 2007-05-15 03:28:16 +0900 | [diff] [blame] | 207 | * ata_dev_get_GTF - get the drive bootup default taskfile settings |
Tejun Heo | 3a32a8e | 2007-05-05 23:50:38 +0900 | [diff] [blame] | 208 | * @dev: target ATA device |
Tejun Heo | 4700c4b | 2007-05-15 03:28:16 +0900 | [diff] [blame] | 209 | * @gtf: output parameter for buffer containing _GTF taskfile arrays |
| 210 | * @ptr_to_free: pointer which should be freed |
Kristen Carlson Accardi | 11ef697 | 2006-09-28 11:29:01 -0700 | [diff] [blame] | 211 | * |
| 212 | * This applies to both PATA and SATA drives. |
| 213 | * |
| 214 | * The _GTF method has no input parameters. |
| 215 | * It returns a variable number of register set values (registers |
| 216 | * hex 1F1..1F7, taskfiles). |
| 217 | * The <variable number> is not known in advance, so have ACPI-CA |
| 218 | * allocate the buffer as needed and return it, then free it later. |
| 219 | * |
Tejun Heo | 4700c4b | 2007-05-15 03:28:16 +0900 | [diff] [blame] | 220 | * LOCKING: |
| 221 | * EH context. |
| 222 | * |
| 223 | * RETURNS: |
| 224 | * Number of taskfiles on success, 0 if _GTF doesn't exist or doesn't |
| 225 | * contain valid data. -errno on other errors. |
Kristen Carlson Accardi | 11ef697 | 2006-09-28 11:29:01 -0700 | [diff] [blame] | 226 | */ |
Tejun Heo | 4700c4b | 2007-05-15 03:28:16 +0900 | [diff] [blame] | 227 | static int ata_dev_get_GTF(struct ata_device *dev, struct ata_acpi_gtf **gtf, |
| 228 | void **ptr_to_free) |
Kristen Carlson Accardi | 11ef697 | 2006-09-28 11:29:01 -0700 | [diff] [blame] | 229 | { |
Tejun Heo | 9af5c9c | 2007-08-06 18:36:22 +0900 | [diff] [blame] | 230 | struct ata_port *ap = dev->link->ap; |
Tejun Heo | 3a32a8e | 2007-05-05 23:50:38 +0900 | [diff] [blame] | 231 | acpi_status status; |
Tejun Heo | 3a32a8e | 2007-05-05 23:50:38 +0900 | [diff] [blame] | 232 | struct acpi_buffer output; |
| 233 | union acpi_object *out_obj; |
Tejun Heo | 4700c4b | 2007-05-15 03:28:16 +0900 | [diff] [blame] | 234 | int rc = 0; |
Kristen Carlson Accardi | 11ef697 | 2006-09-28 11:29:01 -0700 | [diff] [blame] | 235 | |
Tejun Heo | 4700c4b | 2007-05-15 03:28:16 +0900 | [diff] [blame] | 236 | /* set up output buffer */ |
| 237 | output.length = ACPI_ALLOCATE_BUFFER; |
| 238 | output.pointer = NULL; /* ACPI-CA sets this; save/free it later */ |
Kristen Carlson Accardi | 11ef697 | 2006-09-28 11:29:01 -0700 | [diff] [blame] | 239 | |
Kristen Carlson Accardi | 11ef697 | 2006-09-28 11:29:01 -0700 | [diff] [blame] | 240 | if (ata_msg_probe(ap)) |
Tejun Heo | 3a32a8e | 2007-05-05 23:50:38 +0900 | [diff] [blame] | 241 | ata_dev_printk(dev, KERN_DEBUG, "%s: ENTER: port#: %d\n", |
Tejun Heo | 878d4fe | 2007-02-21 16:36:33 +0900 | [diff] [blame] | 242 | __FUNCTION__, ap->port_no); |
Kristen Carlson Accardi | 11ef697 | 2006-09-28 11:29:01 -0700 | [diff] [blame] | 243 | |
Kristen Carlson Accardi | 11ef697 | 2006-09-28 11:29:01 -0700 | [diff] [blame] | 244 | /* _GTF has no input parameters */ |
Tejun Heo | 4700c4b | 2007-05-15 03:28:16 +0900 | [diff] [blame] | 245 | status = acpi_evaluate_object(dev->acpi_handle, "_GTF", NULL, &output); |
| 246 | |
Kristen Carlson Accardi | 11ef697 | 2006-09-28 11:29:01 -0700 | [diff] [blame] | 247 | if (ACPI_FAILURE(status)) { |
Tejun Heo | 4700c4b | 2007-05-15 03:28:16 +0900 | [diff] [blame] | 248 | if (status != AE_NOT_FOUND) { |
| 249 | ata_dev_printk(dev, KERN_WARNING, |
| 250 | "_GTF evaluation failed (AE 0x%x)\n", |
| 251 | status); |
| 252 | rc = -EIO; |
| 253 | } |
| 254 | goto out_free; |
Kristen Carlson Accardi | 11ef697 | 2006-09-28 11:29:01 -0700 | [diff] [blame] | 255 | } |
| 256 | |
| 257 | if (!output.length || !output.pointer) { |
| 258 | if (ata_msg_probe(ap)) |
Tejun Heo | 3a32a8e | 2007-05-05 23:50:38 +0900 | [diff] [blame] | 259 | ata_dev_printk(dev, KERN_DEBUG, "%s: Run _GTF: " |
Kristen Carlson Accardi | 11ef697 | 2006-09-28 11:29:01 -0700 | [diff] [blame] | 260 | "length or ptr is NULL (0x%llx, 0x%p)\n", |
| 261 | __FUNCTION__, |
| 262 | (unsigned long long)output.length, |
| 263 | output.pointer); |
Tejun Heo | 4700c4b | 2007-05-15 03:28:16 +0900 | [diff] [blame] | 264 | goto out_free; |
Kristen Carlson Accardi | 11ef697 | 2006-09-28 11:29:01 -0700 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | out_obj = output.pointer; |
| 268 | if (out_obj->type != ACPI_TYPE_BUFFER) { |
Tejun Heo | 69b16a5 | 2007-05-15 03:28:16 +0900 | [diff] [blame] | 269 | ata_dev_printk(dev, KERN_WARNING, |
| 270 | "_GTF unexpected object type 0x%x\n", |
| 271 | out_obj->type); |
Tejun Heo | 4700c4b | 2007-05-15 03:28:16 +0900 | [diff] [blame] | 272 | rc = -EINVAL; |
| 273 | goto out_free; |
Kristen Carlson Accardi | 11ef697 | 2006-09-28 11:29:01 -0700 | [diff] [blame] | 274 | } |
| 275 | |
Tejun Heo | 4700c4b | 2007-05-15 03:28:16 +0900 | [diff] [blame] | 276 | if (out_obj->buffer.length % REGS_PER_GTF) { |
Tejun Heo | 69b16a5 | 2007-05-15 03:28:16 +0900 | [diff] [blame] | 277 | ata_dev_printk(dev, KERN_WARNING, |
| 278 | "unexpected _GTF length (%d)\n", |
| 279 | out_obj->buffer.length); |
Tejun Heo | 4700c4b | 2007-05-15 03:28:16 +0900 | [diff] [blame] | 280 | rc = -EINVAL; |
| 281 | goto out_free; |
Kristen Carlson Accardi | 11ef697 | 2006-09-28 11:29:01 -0700 | [diff] [blame] | 282 | } |
| 283 | |
Tejun Heo | 4700c4b | 2007-05-15 03:28:16 +0900 | [diff] [blame] | 284 | *ptr_to_free = out_obj; |
| 285 | *gtf = (void *)out_obj->buffer.pointer; |
| 286 | rc = out_obj->buffer.length / REGS_PER_GTF; |
| 287 | |
Kristen Carlson Accardi | 11ef697 | 2006-09-28 11:29:01 -0700 | [diff] [blame] | 288 | if (ata_msg_probe(ap)) |
Tejun Heo | 3a32a8e | 2007-05-05 23:50:38 +0900 | [diff] [blame] | 289 | ata_dev_printk(dev, KERN_DEBUG, "%s: returning " |
Tejun Heo | 4700c4b | 2007-05-15 03:28:16 +0900 | [diff] [blame] | 290 | "gtf=%p, gtf_count=%d, ptr_to_free=%p\n", |
| 291 | __FUNCTION__, *gtf, rc, *ptr_to_free); |
| 292 | return rc; |
| 293 | |
| 294 | out_free: |
| 295 | kfree(output.pointer); |
| 296 | return rc; |
Kristen Carlson Accardi | 11ef697 | 2006-09-28 11:29:01 -0700 | [diff] [blame] | 297 | } |
| 298 | |
| 299 | /** |
Alan Cox | e1ddb4b | 2007-08-16 02:33:36 -0400 | [diff] [blame] | 300 | * ata_acpi_cbl_80wire - Check for 80 wire cable |
| 301 | * @ap: Port to check |
| 302 | * |
| 303 | * Return 1 if the ACPI mode data for this port indicates the BIOS selected |
| 304 | * an 80wire mode. |
| 305 | */ |
| 306 | |
| 307 | int ata_acpi_cbl_80wire(struct ata_port *ap) |
| 308 | { |
| 309 | struct ata_acpi_gtm gtm; |
| 310 | int valid = 0; |
| 311 | |
| 312 | /* No _GTM data, no information */ |
| 313 | if (ata_acpi_gtm(ap, >m) < 0) |
| 314 | return 0; |
| 315 | |
| 316 | /* Split timing, DMA enabled */ |
| 317 | if ((gtm.flags & 0x11) == 0x11 && gtm.drive[0].dma < 55) |
| 318 | valid |= 1; |
| 319 | if ((gtm.flags & 0x14) == 0x14 && gtm.drive[1].dma < 55) |
| 320 | valid |= 2; |
| 321 | /* Shared timing, DMA enabled */ |
| 322 | if ((gtm.flags & 0x11) == 0x01 && gtm.drive[0].dma < 55) |
| 323 | valid |= 1; |
| 324 | if ((gtm.flags & 0x14) == 0x04 && gtm.drive[0].dma < 55) |
| 325 | valid |= 2; |
| 326 | |
| 327 | /* Drive check */ |
| 328 | if ((valid & 1) && ata_dev_enabled(&ap->link.device[0])) |
| 329 | return 1; |
| 330 | if ((valid & 2) && ata_dev_enabled(&ap->link.device[1])) |
| 331 | return 1; |
| 332 | return 0; |
| 333 | } |
| 334 | |
| 335 | EXPORT_SYMBOL_GPL(ata_acpi_cbl_80wire); |
| 336 | |
| 337 | /** |
Kristen Carlson Accardi | 11ef697 | 2006-09-28 11:29:01 -0700 | [diff] [blame] | 338 | * taskfile_load_raw - send taskfile registers to host controller |
Tejun Heo | 3a32a8e | 2007-05-05 23:50:38 +0900 | [diff] [blame] | 339 | * @dev: target ATA device |
Kristen Carlson Accardi | 11ef697 | 2006-09-28 11:29:01 -0700 | [diff] [blame] | 340 | * @gtf: raw ATA taskfile register set (0x1f1 - 0x1f7) |
| 341 | * |
| 342 | * Outputs ATA taskfile to standard ATA host controller using MMIO |
| 343 | * or PIO as indicated by the ATA_FLAG_MMIO flag. |
| 344 | * Writes the control, feature, nsect, lbal, lbam, and lbah registers. |
| 345 | * Optionally (ATA_TFLAG_LBA48) writes hob_feature, hob_nsect, |
| 346 | * hob_lbal, hob_lbam, and hob_lbah. |
| 347 | * |
| 348 | * This function waits for idle (!BUSY and !DRQ) after writing |
| 349 | * registers. If the control register has a new value, this |
| 350 | * function also waits for idle after writing control and before |
| 351 | * writing the remaining registers. |
| 352 | * |
Tejun Heo | 4700c4b | 2007-05-15 03:28:16 +0900 | [diff] [blame] | 353 | * LOCKING: |
| 354 | * EH context. |
| 355 | * |
| 356 | * RETURNS: |
| 357 | * 0 on success, -errno on failure. |
Kristen Carlson Accardi | 11ef697 | 2006-09-28 11:29:01 -0700 | [diff] [blame] | 358 | */ |
Tejun Heo | 4700c4b | 2007-05-15 03:28:16 +0900 | [diff] [blame] | 359 | static int taskfile_load_raw(struct ata_device *dev, |
| 360 | const struct ata_acpi_gtf *gtf) |
Kristen Carlson Accardi | 11ef697 | 2006-09-28 11:29:01 -0700 | [diff] [blame] | 361 | { |
Tejun Heo | 9af5c9c | 2007-08-06 18:36:22 +0900 | [diff] [blame] | 362 | struct ata_port *ap = dev->link->ap; |
Tejun Heo | 4700c4b | 2007-05-15 03:28:16 +0900 | [diff] [blame] | 363 | struct ata_taskfile tf, rtf; |
| 364 | unsigned int err_mask; |
Jeff Garzik | fc16c25 | 2007-02-24 21:05:01 -0500 | [diff] [blame] | 365 | |
Tejun Heo | 4700c4b | 2007-05-15 03:28:16 +0900 | [diff] [blame] | 366 | if ((gtf->tf[0] == 0) && (gtf->tf[1] == 0) && (gtf->tf[2] == 0) |
| 367 | && (gtf->tf[3] == 0) && (gtf->tf[4] == 0) && (gtf->tf[5] == 0) |
| 368 | && (gtf->tf[6] == 0)) |
| 369 | return 0; |
Kristen Carlson Accardi | 11ef697 | 2006-09-28 11:29:01 -0700 | [diff] [blame] | 370 | |
Tejun Heo | 3a32a8e | 2007-05-05 23:50:38 +0900 | [diff] [blame] | 371 | ata_tf_init(dev, &tf); |
Kristen Carlson Accardi | 11ef697 | 2006-09-28 11:29:01 -0700 | [diff] [blame] | 372 | |
Jeff Garzik | fc16c25 | 2007-02-24 21:05:01 -0500 | [diff] [blame] | 373 | /* convert gtf to tf */ |
| 374 | tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE; /* TBD */ |
Tejun Heo | 48be6b1 | 2007-04-23 02:06:46 +0900 | [diff] [blame] | 375 | tf.protocol = ATA_PROT_NODATA; |
Tejun Heo | 4700c4b | 2007-05-15 03:28:16 +0900 | [diff] [blame] | 376 | tf.feature = gtf->tf[0]; /* 0x1f1 */ |
| 377 | tf.nsect = gtf->tf[1]; /* 0x1f2 */ |
| 378 | tf.lbal = gtf->tf[2]; /* 0x1f3 */ |
| 379 | tf.lbam = gtf->tf[3]; /* 0x1f4 */ |
| 380 | tf.lbah = gtf->tf[4]; /* 0x1f5 */ |
| 381 | tf.device = gtf->tf[5]; /* 0x1f6 */ |
| 382 | tf.command = gtf->tf[6]; /* 0x1f7 */ |
Kristen Carlson Accardi | 11ef697 | 2006-09-28 11:29:01 -0700 | [diff] [blame] | 383 | |
Tejun Heo | 4700c4b | 2007-05-15 03:28:16 +0900 | [diff] [blame] | 384 | if (ata_msg_probe(ap)) |
| 385 | ata_dev_printk(dev, KERN_DEBUG, "executing ACPI cmd " |
| 386 | "%02x/%02x:%02x:%02x:%02x:%02x:%02x\n", |
| 387 | tf.command, tf.feature, tf.nsect, |
| 388 | tf.lbal, tf.lbam, tf.lbah, tf.device); |
| 389 | |
| 390 | rtf = tf; |
| 391 | err_mask = ata_exec_internal(dev, &rtf, NULL, DMA_NONE, NULL, 0); |
| 392 | if (err_mask) { |
Tejun Heo | 3a32a8e | 2007-05-05 23:50:38 +0900 | [diff] [blame] | 393 | ata_dev_printk(dev, KERN_ERR, |
Tejun Heo | 4700c4b | 2007-05-15 03:28:16 +0900 | [diff] [blame] | 394 | "ACPI cmd %02x/%02x:%02x:%02x:%02x:%02x:%02x failed " |
| 395 | "(Emask=0x%x Stat=0x%02x Err=0x%02x)\n", |
| 396 | tf.command, tf.feature, tf.nsect, tf.lbal, tf.lbam, |
| 397 | tf.lbah, tf.device, err_mask, rtf.command, rtf.feature); |
| 398 | return -EIO; |
| 399 | } |
| 400 | |
| 401 | return 0; |
Kristen Carlson Accardi | 11ef697 | 2006-09-28 11:29:01 -0700 | [diff] [blame] | 402 | } |
| 403 | |
| 404 | /** |
Kristen Carlson Accardi | 11ef697 | 2006-09-28 11:29:01 -0700 | [diff] [blame] | 405 | * ata_acpi_exec_tfs - get then write drive taskfile settings |
Tejun Heo | 6746544 | 2007-05-15 03:28:16 +0900 | [diff] [blame] | 406 | * @dev: target ATA device |
Kristen Carlson Accardi | 11ef697 | 2006-09-28 11:29:01 -0700 | [diff] [blame] | 407 | * |
Tejun Heo | 6746544 | 2007-05-15 03:28:16 +0900 | [diff] [blame] | 408 | * Evaluate _GTF and excute returned taskfiles. |
Tejun Heo | 69b16a5 | 2007-05-15 03:28:16 +0900 | [diff] [blame] | 409 | * |
| 410 | * LOCKING: |
| 411 | * EH context. |
| 412 | * |
| 413 | * RETURNS: |
Tejun Heo | 6746544 | 2007-05-15 03:28:16 +0900 | [diff] [blame] | 414 | * Number of executed taskfiles on success, 0 if _GTF doesn't exist or |
| 415 | * doesn't contain valid data. -errno on other errors. |
Kristen Carlson Accardi | 11ef697 | 2006-09-28 11:29:01 -0700 | [diff] [blame] | 416 | */ |
Tejun Heo | 6746544 | 2007-05-15 03:28:16 +0900 | [diff] [blame] | 417 | static int ata_acpi_exec_tfs(struct ata_device *dev) |
Kristen Carlson Accardi | 11ef697 | 2006-09-28 11:29:01 -0700 | [diff] [blame] | 418 | { |
Tejun Heo | 6746544 | 2007-05-15 03:28:16 +0900 | [diff] [blame] | 419 | struct ata_acpi_gtf *gtf = NULL; |
| 420 | void *ptr_to_free = NULL; |
| 421 | int gtf_count, i, rc; |
Kristen Carlson Accardi | 11ef697 | 2006-09-28 11:29:01 -0700 | [diff] [blame] | 422 | |
Tejun Heo | 6746544 | 2007-05-15 03:28:16 +0900 | [diff] [blame] | 423 | /* get taskfiles */ |
| 424 | rc = ata_dev_get_GTF(dev, >f, &ptr_to_free); |
| 425 | if (rc < 0) |
| 426 | return rc; |
| 427 | gtf_count = rc; |
Kristen Carlson Accardi | 11ef697 | 2006-09-28 11:29:01 -0700 | [diff] [blame] | 428 | |
Tejun Heo | 6746544 | 2007-05-15 03:28:16 +0900 | [diff] [blame] | 429 | /* execute them */ |
| 430 | for (i = 0, rc = 0; i < gtf_count; i++) { |
| 431 | int tmp; |
Tejun Heo | 3a32a8e | 2007-05-05 23:50:38 +0900 | [diff] [blame] | 432 | |
Tejun Heo | 6746544 | 2007-05-15 03:28:16 +0900 | [diff] [blame] | 433 | /* ACPI errors are eventually ignored. Run till the |
| 434 | * end even after errors. |
| 435 | */ |
| 436 | tmp = taskfile_load_raw(dev, gtf++); |
| 437 | if (!rc) |
| 438 | rc = tmp; |
Kristen Carlson Accardi | 11ef697 | 2006-09-28 11:29:01 -0700 | [diff] [blame] | 439 | } |
| 440 | |
Tejun Heo | 6746544 | 2007-05-15 03:28:16 +0900 | [diff] [blame] | 441 | kfree(ptr_to_free); |
| 442 | |
| 443 | if (rc == 0) |
| 444 | return gtf_count; |
| 445 | return rc; |
Kristen Carlson Accardi | 11ef697 | 2006-09-28 11:29:01 -0700 | [diff] [blame] | 446 | } |
| 447 | |
Kristen Carlson Accardi | 7ea1fbc | 2006-09-28 11:29:12 -0700 | [diff] [blame] | 448 | /** |
| 449 | * ata_acpi_push_id - send Identify data to drive |
Tejun Heo | 3a32a8e | 2007-05-05 23:50:38 +0900 | [diff] [blame] | 450 | * @dev: target ATA device |
Kristen Carlson Accardi | 7ea1fbc | 2006-09-28 11:29:12 -0700 | [diff] [blame] | 451 | * |
| 452 | * _SDD ACPI object: for SATA mode only |
| 453 | * Must be after Identify (Packet) Device -- uses its data |
| 454 | * ATM this function never returns a failure. It is an optional |
| 455 | * method and if it fails for whatever reason, we should still |
| 456 | * just keep going. |
Tejun Heo | 69b16a5 | 2007-05-15 03:28:16 +0900 | [diff] [blame] | 457 | * |
| 458 | * LOCKING: |
| 459 | * EH context. |
| 460 | * |
| 461 | * RETURNS: |
| 462 | * 0 on success, -errno on failure. |
Kristen Carlson Accardi | 7ea1fbc | 2006-09-28 11:29:12 -0700 | [diff] [blame] | 463 | */ |
Tejun Heo | 6746544 | 2007-05-15 03:28:16 +0900 | [diff] [blame] | 464 | static int ata_acpi_push_id(struct ata_device *dev) |
Kristen Carlson Accardi | 7ea1fbc | 2006-09-28 11:29:12 -0700 | [diff] [blame] | 465 | { |
Tejun Heo | 9af5c9c | 2007-08-06 18:36:22 +0900 | [diff] [blame] | 466 | struct ata_port *ap = dev->link->ap; |
Tejun Heo | 3a32a8e | 2007-05-05 23:50:38 +0900 | [diff] [blame] | 467 | int err; |
Tejun Heo | 3a32a8e | 2007-05-05 23:50:38 +0900 | [diff] [blame] | 468 | acpi_status status; |
| 469 | struct acpi_object_list input; |
| 470 | union acpi_object in_params[1]; |
Kristen Carlson Accardi | 7ea1fbc | 2006-09-28 11:29:12 -0700 | [diff] [blame] | 471 | |
Kristen Carlson Accardi | 7ea1fbc | 2006-09-28 11:29:12 -0700 | [diff] [blame] | 472 | if (ata_msg_probe(ap)) |
Tejun Heo | 3a32a8e | 2007-05-05 23:50:38 +0900 | [diff] [blame] | 473 | ata_dev_printk(dev, KERN_DEBUG, "%s: ix = %d, port#: %d\n", |
| 474 | __FUNCTION__, dev->devno, ap->port_no); |
Kristen Carlson Accardi | 7ea1fbc | 2006-09-28 11:29:12 -0700 | [diff] [blame] | 475 | |
Kristen Carlson Accardi | 7ea1fbc | 2006-09-28 11:29:12 -0700 | [diff] [blame] | 476 | /* Give the drive Identify data to the drive via the _SDD method */ |
| 477 | /* _SDD: set up input parameters */ |
| 478 | input.count = 1; |
| 479 | input.pointer = in_params; |
| 480 | in_params[0].type = ACPI_TYPE_BUFFER; |
Tejun Heo | 3a32a8e | 2007-05-05 23:50:38 +0900 | [diff] [blame] | 481 | in_params[0].buffer.length = sizeof(dev->id[0]) * ATA_ID_WORDS; |
| 482 | in_params[0].buffer.pointer = (u8 *)dev->id; |
Kristen Carlson Accardi | 7ea1fbc | 2006-09-28 11:29:12 -0700 | [diff] [blame] | 483 | /* Output buffer: _SDD has no output */ |
| 484 | |
| 485 | /* It's OK for _SDD to be missing too. */ |
Tejun Heo | 3a32a8e | 2007-05-05 23:50:38 +0900 | [diff] [blame] | 486 | swap_buf_le16(dev->id, ATA_ID_WORDS); |
Tejun Heo | fafbae8 | 2007-05-15 03:28:16 +0900 | [diff] [blame] | 487 | status = acpi_evaluate_object(dev->acpi_handle, "_SDD", &input, NULL); |
Tejun Heo | 3a32a8e | 2007-05-05 23:50:38 +0900 | [diff] [blame] | 488 | swap_buf_le16(dev->id, ATA_ID_WORDS); |
Kristen Carlson Accardi | 7ea1fbc | 2006-09-28 11:29:12 -0700 | [diff] [blame] | 489 | |
| 490 | err = ACPI_FAILURE(status) ? -EIO : 0; |
Tejun Heo | 69b16a5 | 2007-05-15 03:28:16 +0900 | [diff] [blame] | 491 | if (err < 0) |
| 492 | ata_dev_printk(dev, KERN_WARNING, |
| 493 | "ACPI _SDD failed (AE 0x%x)\n", status); |
Kristen Carlson Accardi | 7ea1fbc | 2006-09-28 11:29:12 -0700 | [diff] [blame] | 494 | |
Tejun Heo | 6746544 | 2007-05-15 03:28:16 +0900 | [diff] [blame] | 495 | return err; |
Kristen Carlson Accardi | 7ea1fbc | 2006-09-28 11:29:12 -0700 | [diff] [blame] | 496 | } |
| 497 | |
Tejun Heo | 6746544 | 2007-05-15 03:28:16 +0900 | [diff] [blame] | 498 | /** |
Tejun Heo | 64578a3 | 2007-05-15 03:28:16 +0900 | [diff] [blame] | 499 | * ata_acpi_on_suspend - ATA ACPI hook called on suspend |
| 500 | * @ap: target ATA port |
| 501 | * |
| 502 | * This function is called when @ap is about to be suspended. All |
| 503 | * devices are already put to sleep but the port_suspend() callback |
| 504 | * hasn't been executed yet. Error return from this function aborts |
| 505 | * suspend. |
| 506 | * |
| 507 | * LOCKING: |
| 508 | * EH context. |
| 509 | * |
| 510 | * RETURNS: |
| 511 | * 0 on success, -errno on failure. |
| 512 | */ |
| 513 | int ata_acpi_on_suspend(struct ata_port *ap) |
| 514 | { |
| 515 | unsigned long flags; |
| 516 | int rc; |
| 517 | |
| 518 | /* proceed iff per-port acpi_handle is valid */ |
| 519 | if (!ap->acpi_handle) |
| 520 | return 0; |
| 521 | BUG_ON(ap->flags & ATA_FLAG_ACPI_SATA); |
| 522 | |
| 523 | /* store timing parameters */ |
| 524 | rc = ata_acpi_gtm(ap, &ap->acpi_gtm); |
| 525 | |
| 526 | spin_lock_irqsave(ap->lock, flags); |
| 527 | if (rc == 0) |
| 528 | ap->pflags |= ATA_PFLAG_GTM_VALID; |
| 529 | else |
| 530 | ap->pflags &= ~ATA_PFLAG_GTM_VALID; |
| 531 | spin_unlock_irqrestore(ap->lock, flags); |
| 532 | |
| 533 | if (rc == -ENOENT) |
| 534 | rc = 0; |
| 535 | return rc; |
| 536 | } |
| 537 | |
| 538 | /** |
Tejun Heo | 6746544 | 2007-05-15 03:28:16 +0900 | [diff] [blame] | 539 | * ata_acpi_on_resume - ATA ACPI hook called on resume |
| 540 | * @ap: target ATA port |
| 541 | * |
| 542 | * This function is called when @ap is resumed - right after port |
| 543 | * itself is resumed but before any EH action is taken. |
| 544 | * |
| 545 | * LOCKING: |
| 546 | * EH context. |
| 547 | */ |
| 548 | void ata_acpi_on_resume(struct ata_port *ap) |
| 549 | { |
Tejun Heo | f58229f | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 550 | struct ata_device *dev; |
Kristen Carlson Accardi | 7ea1fbc | 2006-09-28 11:29:12 -0700 | [diff] [blame] | 551 | |
Tejun Heo | 64578a3 | 2007-05-15 03:28:16 +0900 | [diff] [blame] | 552 | if (ap->acpi_handle && (ap->pflags & ATA_PFLAG_GTM_VALID)) { |
| 553 | BUG_ON(ap->flags & ATA_FLAG_ACPI_SATA); |
| 554 | |
| 555 | /* restore timing parameters */ |
| 556 | ata_acpi_stm(ap, &ap->acpi_gtm); |
| 557 | } |
| 558 | |
Tejun Heo | 6746544 | 2007-05-15 03:28:16 +0900 | [diff] [blame] | 559 | /* schedule _GTF */ |
Tejun Heo | f58229f | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 560 | ata_link_for_each_dev(dev, &ap->link) |
| 561 | dev->flags |= ATA_DFLAG_ACPI_PENDING; |
Tejun Heo | 6746544 | 2007-05-15 03:28:16 +0900 | [diff] [blame] | 562 | } |
| 563 | |
| 564 | /** |
| 565 | * ata_acpi_on_devcfg - ATA ACPI hook called on device donfiguration |
| 566 | * @dev: target ATA device |
| 567 | * |
| 568 | * This function is called when @dev is about to be configured. |
| 569 | * IDENTIFY data might have been modified after this hook is run. |
| 570 | * |
| 571 | * LOCKING: |
| 572 | * EH context. |
| 573 | * |
| 574 | * RETURNS: |
| 575 | * Positive number if IDENTIFY data needs to be refreshed, 0 if not, |
| 576 | * -errno on failure. |
| 577 | */ |
| 578 | int ata_acpi_on_devcfg(struct ata_device *dev) |
| 579 | { |
Tejun Heo | 9af5c9c | 2007-08-06 18:36:22 +0900 | [diff] [blame] | 580 | struct ata_port *ap = dev->link->ap; |
| 581 | struct ata_eh_context *ehc = &ap->link.eh_context; |
Tejun Heo | 6746544 | 2007-05-15 03:28:16 +0900 | [diff] [blame] | 582 | int acpi_sata = ap->flags & ATA_FLAG_ACPI_SATA; |
| 583 | int rc; |
| 584 | |
Tejun Heo | 6746544 | 2007-05-15 03:28:16 +0900 | [diff] [blame] | 585 | if (!dev->acpi_handle) |
| 586 | return 0; |
| 587 | |
| 588 | /* do we need to do _GTF? */ |
| 589 | if (!(dev->flags & ATA_DFLAG_ACPI_PENDING) && |
| 590 | !(acpi_sata && (ehc->i.flags & ATA_EHI_DID_HARDRESET))) |
| 591 | return 0; |
| 592 | |
| 593 | /* do _SDD if SATA */ |
| 594 | if (acpi_sata) { |
| 595 | rc = ata_acpi_push_id(dev); |
| 596 | if (rc) |
| 597 | goto acpi_err; |
| 598 | } |
| 599 | |
| 600 | /* do _GTF */ |
| 601 | rc = ata_acpi_exec_tfs(dev); |
| 602 | if (rc < 0) |
| 603 | goto acpi_err; |
| 604 | |
| 605 | dev->flags &= ~ATA_DFLAG_ACPI_PENDING; |
| 606 | |
| 607 | /* refresh IDENTIFY page if any _GTF command has been executed */ |
| 608 | if (rc > 0) { |
| 609 | rc = ata_dev_reread_id(dev, 0); |
| 610 | if (rc < 0) { |
| 611 | ata_dev_printk(dev, KERN_ERR, "failed to IDENTIFY " |
| 612 | "after ACPI commands\n"); |
| 613 | return rc; |
| 614 | } |
| 615 | } |
| 616 | |
| 617 | return 0; |
| 618 | |
| 619 | acpi_err: |
| 620 | /* let EH retry on the first failure, disable ACPI on the second */ |
| 621 | if (dev->flags & ATA_DFLAG_ACPI_FAILED) { |
| 622 | ata_dev_printk(dev, KERN_WARNING, "ACPI on devcfg failed the " |
| 623 | "second time, disabling (errno=%d)\n", rc); |
| 624 | |
| 625 | dev->acpi_handle = NULL; |
| 626 | |
| 627 | /* if port is working, request IDENTIFY reload and continue */ |
| 628 | if (!(ap->pflags & ATA_PFLAG_FROZEN)) |
| 629 | rc = 1; |
| 630 | } |
| 631 | dev->flags |= ATA_DFLAG_ACPI_FAILED; |
| 632 | return rc; |
| 633 | } |