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