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 27 #define NO_PORT_MULT 0xffff 28 #define SATA_ADR(root,pmp) (((root) << 16) | (pmp)) 29 30 #define REGS_PER_GTF 7 31 struct ata_acpi_gtf { 32 u8 tf[REGS_PER_GTF]; /* regs. 0x1f1 - 0x1f7 */ 33 } __packed; 34 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 } 42 43 static void ata_acpi_associate_sata_port(struct ata_port *ap) 44 { 45 acpi_integer adr = SATA_ADR(ap->port_no, NO_PORT_MULT); 46 47 ap->device->acpi_handle = acpi_get_child(ap->host->acpi_handle, adr); 48 } 49 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 } 67 } 68 69 /** 70 * ata_acpi_associate - associate ATA host with ACPI objects 71 * @host: target ATA host 72 * 73 * Look up ACPI objects associated with @host and initialize 74 * acpi_handle fields of @host, its ports and devices accordingly. 75 * 76 * LOCKING: 77 * EH context. 78 * 79 * RETURNS: 80 * 0 on success, -errno on failure. 81 */ 82 void ata_acpi_associate(struct ata_host *host) 83 { 84 int i; 85 86 if (!is_pci_dev(host->dev) || libata_noacpi) 87 return; 88 89 host->acpi_handle = DEVICE_ACPI_HANDLE(host->dev); 90 if (!host->acpi_handle) 91 return; 92 93 for (i = 0; i < host->n_ports; i++) { 94 struct ata_port *ap = host->ports[i]; 95 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); 100 } 101 } 102 103 /** 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 /** 206 * ata_dev_get_GTF - get the drive bootup default taskfile settings 207 * @dev: target ATA device 208 * @gtf: output parameter for buffer containing _GTF taskfile arrays 209 * @ptr_to_free: pointer which should be freed 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 * 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. 225 */ 226 static int ata_dev_get_GTF(struct ata_device *dev, struct ata_acpi_gtf **gtf, 227 void **ptr_to_free) 228 { 229 struct ata_port *ap = dev->ap; 230 acpi_status status; 231 struct acpi_buffer output; 232 union acpi_object *out_obj; 233 int rc = 0; 234 235 /* set up output buffer */ 236 output.length = ACPI_ALLOCATE_BUFFER; 237 output.pointer = NULL; /* ACPI-CA sets this; save/free it later */ 238 239 if (ata_msg_probe(ap)) 240 ata_dev_printk(dev, KERN_DEBUG, "%s: ENTER: port#: %d\n", 241 __FUNCTION__, ap->port_no); 242 243 /* _GTF has no input parameters */ 244 status = acpi_evaluate_object(dev->acpi_handle, "_GTF", NULL, &output); 245 246 if (ACPI_FAILURE(status)) { 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; 254 } 255 256 if (!output.length || !output.pointer) { 257 if (ata_msg_probe(ap)) 258 ata_dev_printk(dev, KERN_DEBUG, "%s: Run _GTF: " 259 "length or ptr is NULL (0x%llx, 0x%p)\n", 260 __FUNCTION__, 261 (unsigned long long)output.length, 262 output.pointer); 263 goto out_free; 264 } 265 266 out_obj = output.pointer; 267 if (out_obj->type != ACPI_TYPE_BUFFER) { 268 ata_dev_printk(dev, KERN_WARNING, 269 "_GTF unexpected object type 0x%x\n", 270 out_obj->type); 271 rc = -EINVAL; 272 goto out_free; 273 } 274 275 if (out_obj->buffer.length % REGS_PER_GTF) { 276 ata_dev_printk(dev, KERN_WARNING, 277 "unexpected _GTF length (%d)\n", 278 out_obj->buffer.length); 279 rc = -EINVAL; 280 goto out_free; 281 } 282 283 *ptr_to_free = out_obj; 284 *gtf = (void *)out_obj->buffer.pointer; 285 rc = out_obj->buffer.length / REGS_PER_GTF; 286 287 if (ata_msg_probe(ap)) 288 ata_dev_printk(dev, KERN_DEBUG, "%s: returning " 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; 296 } 297 298 /** 299 * taskfile_load_raw - send taskfile registers to host controller 300 * @dev: target ATA device 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 * 314 * LOCKING: 315 * EH context. 316 * 317 * RETURNS: 318 * 0 on success, -errno on failure. 319 */ 320 static int taskfile_load_raw(struct ata_device *dev, 321 const struct ata_acpi_gtf *gtf) 322 { 323 struct ata_port *ap = dev->ap; 324 struct ata_taskfile tf, rtf; 325 unsigned int err_mask; 326 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; 331 332 ata_tf_init(dev, &tf); 333 334 /* convert gtf to tf */ 335 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE; /* TBD */ 336 tf.protocol = ATA_PROT_NODATA; 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 */ 344 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) { 354 ata_dev_printk(dev, KERN_ERR, 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; 363 } 364 365 /** 366 * ata_acpi_exec_tfs - get then write drive taskfile settings 367 * @dev: target ATA device 368 * 369 * Evaluate _GTF and excute returned taskfiles. 370 * 371 * LOCKING: 372 * EH context. 373 * 374 * RETURNS: 375 * Number of executed taskfiles on success, 0 if _GTF doesn't exist or 376 * doesn't contain valid data. -errno on other errors. 377 */ 378 static int ata_acpi_exec_tfs(struct ata_device *dev) 379 { 380 struct ata_acpi_gtf *gtf = NULL; 381 void *ptr_to_free = NULL; 382 int gtf_count, i, rc; 383 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; 389 390 /* execute them */ 391 for (i = 0, rc = 0; i < gtf_count; i++) { 392 int tmp; 393 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; 400 } 401 402 kfree(ptr_to_free); 403 404 if (rc == 0) 405 return gtf_count; 406 return rc; 407 } 408 409 /** 410 * ata_acpi_push_id - send Identify data to drive 411 * @dev: target ATA device 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. 418 * 419 * LOCKING: 420 * EH context. 421 * 422 * RETURNS: 423 * 0 on success, -errno on failure. 424 */ 425 static int ata_acpi_push_id(struct ata_device *dev) 426 { 427 struct ata_port *ap = dev->ap; 428 int err; 429 acpi_status status; 430 struct acpi_object_list input; 431 union acpi_object in_params[1]; 432 433 if (ata_msg_probe(ap)) 434 ata_dev_printk(dev, KERN_DEBUG, "%s: ix = %d, port#: %d\n", 435 __FUNCTION__, dev->devno, ap->port_no); 436 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; 442 in_params[0].buffer.length = sizeof(dev->id[0]) * ATA_ID_WORDS; 443 in_params[0].buffer.pointer = (u8 *)dev->id; 444 /* Output buffer: _SDD has no output */ 445 446 /* It's OK for _SDD to be missing too. */ 447 swap_buf_le16(dev->id, ATA_ID_WORDS); 448 status = acpi_evaluate_object(dev->acpi_handle, "_SDD", &input, NULL); 449 swap_buf_le16(dev->id, ATA_ID_WORDS); 450 451 err = ACPI_FAILURE(status) ? -EIO : 0; 452 if (err < 0) 453 ata_dev_printk(dev, KERN_WARNING, 454 "ACPI _SDD failed (AE 0x%x)\n", status); 455 456 return err; 457 } 458 459 /** 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 /** 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; 512 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 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 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 } 595