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