0001
0002
0003
0004
0005
0006
0007
0008
0009
0010 #include <linux/module.h>
0011 #include <linux/ata.h>
0012 #include <linux/delay.h>
0013 #include <linux/device.h>
0014 #include <linux/errno.h>
0015 #include <linux/kernel.h>
0016 #include <linux/acpi.h>
0017 #include <linux/libata.h>
0018 #include <linux/pci.h>
0019 #include <linux/slab.h>
0020 #include <linux/pm_runtime.h>
0021 #include <scsi/scsi_device.h>
0022 #include "libata.h"
0023
0024 unsigned int ata_acpi_gtf_filter = ATA_ACPI_FILTER_DEFAULT;
0025 module_param_named(acpi_gtf_filter, ata_acpi_gtf_filter, int, 0644);
0026 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)");
0027
0028 #define NO_PORT_MULT 0xffff
0029 #define SATA_ADR(root, pmp) (((root) << 16) | (pmp))
0030
0031 #define REGS_PER_GTF 7
0032 struct ata_acpi_gtf {
0033 u8 tf[REGS_PER_GTF];
0034 } __packed;
0035
0036 static void ata_acpi_clear_gtf(struct ata_device *dev)
0037 {
0038 kfree(dev->gtf_cache);
0039 dev->gtf_cache = NULL;
0040 }
0041
0042 struct ata_acpi_hotplug_context {
0043 struct acpi_hotplug_context hp;
0044 union {
0045 struct ata_port *ap;
0046 struct ata_device *dev;
0047 } data;
0048 };
0049
0050 #define ata_hotplug_data(context) (container_of((context), struct ata_acpi_hotplug_context, hp)->data)
0051
0052
0053
0054
0055
0056
0057
0058
0059
0060 acpi_handle ata_dev_acpi_handle(struct ata_device *dev)
0061 {
0062 return dev->flags & ATA_DFLAG_ACPI_DISABLED ?
0063 NULL : ACPI_HANDLE(&dev->tdev);
0064 }
0065
0066
0067 static void ata_acpi_detach_device(struct ata_port *ap, struct ata_device *dev)
0068 {
0069 if (dev)
0070 dev->flags |= ATA_DFLAG_DETACH;
0071 else {
0072 struct ata_link *tlink;
0073 struct ata_device *tdev;
0074
0075 ata_for_each_link(tlink, ap, EDGE)
0076 ata_for_each_dev(tdev, tlink, ALL)
0077 tdev->flags |= ATA_DFLAG_DETACH;
0078 }
0079
0080 ata_port_schedule_eh(ap);
0081 }
0082
0083
0084
0085
0086
0087
0088
0089
0090
0091
0092
0093
0094
0095
0096
0097
0098
0099
0100 static void ata_acpi_handle_hotplug(struct ata_port *ap, struct ata_device *dev,
0101 u32 event)
0102 {
0103 struct ata_eh_info *ehi = &ap->link.eh_info;
0104 int wait = 0;
0105 unsigned long flags;
0106
0107 spin_lock_irqsave(ap->lock, flags);
0108
0109
0110
0111
0112
0113 switch (event) {
0114 case ACPI_NOTIFY_BUS_CHECK:
0115 case ACPI_NOTIFY_DEVICE_CHECK:
0116 ata_ehi_push_desc(ehi, "ACPI event");
0117
0118 ata_ehi_hotplugged(ehi);
0119 ata_port_freeze(ap);
0120 break;
0121 case ACPI_NOTIFY_EJECT_REQUEST:
0122 ata_ehi_push_desc(ehi, "ACPI event");
0123
0124 ata_acpi_detach_device(ap, dev);
0125 wait = 1;
0126 break;
0127 }
0128
0129 spin_unlock_irqrestore(ap->lock, flags);
0130
0131 if (wait)
0132 ata_port_wait_eh(ap);
0133 }
0134
0135 static int ata_acpi_dev_notify_dock(struct acpi_device *adev, u32 event)
0136 {
0137 struct ata_device *dev = ata_hotplug_data(adev->hp).dev;
0138 ata_acpi_handle_hotplug(dev->link->ap, dev, event);
0139 return 0;
0140 }
0141
0142 static int ata_acpi_ap_notify_dock(struct acpi_device *adev, u32 event)
0143 {
0144 ata_acpi_handle_hotplug(ata_hotplug_data(adev->hp).ap, NULL, event);
0145 return 0;
0146 }
0147
0148 static void ata_acpi_uevent(struct ata_port *ap, struct ata_device *dev,
0149 u32 event)
0150 {
0151 struct kobject *kobj = NULL;
0152 char event_string[20];
0153 char *envp[] = { event_string, NULL };
0154
0155 if (dev) {
0156 if (dev->sdev)
0157 kobj = &dev->sdev->sdev_gendev.kobj;
0158 } else
0159 kobj = &ap->dev->kobj;
0160
0161 if (kobj) {
0162 snprintf(event_string, 20, "BAY_EVENT=%d", event);
0163 kobject_uevent_env(kobj, KOBJ_CHANGE, envp);
0164 }
0165 }
0166
0167 static void ata_acpi_ap_uevent(struct acpi_device *adev, u32 event)
0168 {
0169 ata_acpi_uevent(ata_hotplug_data(adev->hp).ap, NULL, event);
0170 }
0171
0172 static void ata_acpi_dev_uevent(struct acpi_device *adev, u32 event)
0173 {
0174 struct ata_device *dev = ata_hotplug_data(adev->hp).dev;
0175 ata_acpi_uevent(dev->link->ap, dev, event);
0176 }
0177
0178
0179 void ata_acpi_bind_port(struct ata_port *ap)
0180 {
0181 struct acpi_device *host_companion = ACPI_COMPANION(ap->host->dev);
0182 struct acpi_device *adev;
0183 struct ata_acpi_hotplug_context *context;
0184
0185 if (libata_noacpi || ap->flags & ATA_FLAG_ACPI_SATA || !host_companion)
0186 return;
0187
0188 acpi_preset_companion(&ap->tdev, host_companion, ap->port_no);
0189
0190 if (ata_acpi_gtm(ap, &ap->__acpi_init_gtm) == 0)
0191 ap->pflags |= ATA_PFLAG_INIT_GTM_VALID;
0192
0193 adev = ACPI_COMPANION(&ap->tdev);
0194 if (!adev || adev->hp)
0195 return;
0196
0197 context = kzalloc(sizeof(*context), GFP_KERNEL);
0198 if (!context)
0199 return;
0200
0201 context->data.ap = ap;
0202 acpi_initialize_hp_context(adev, &context->hp, ata_acpi_ap_notify_dock,
0203 ata_acpi_ap_uevent);
0204 }
0205
0206 void ata_acpi_bind_dev(struct ata_device *dev)
0207 {
0208 struct ata_port *ap = dev->link->ap;
0209 struct acpi_device *port_companion = ACPI_COMPANION(&ap->tdev);
0210 struct acpi_device *host_companion = ACPI_COMPANION(ap->host->dev);
0211 struct acpi_device *parent, *adev;
0212 struct ata_acpi_hotplug_context *context;
0213 u64 adr;
0214
0215
0216
0217
0218
0219 if (libata_noacpi || !host_companion ||
0220 (!(ap->flags & ATA_FLAG_ACPI_SATA) && !port_companion))
0221 return;
0222
0223 if (ap->flags & ATA_FLAG_ACPI_SATA) {
0224 if (!sata_pmp_attached(ap))
0225 adr = SATA_ADR(ap->port_no, NO_PORT_MULT);
0226 else
0227 adr = SATA_ADR(ap->port_no, dev->link->pmp);
0228 parent = host_companion;
0229 } else {
0230 adr = dev->devno;
0231 parent = port_companion;
0232 }
0233
0234 acpi_preset_companion(&dev->tdev, parent, adr);
0235 adev = ACPI_COMPANION(&dev->tdev);
0236 if (!adev || adev->hp)
0237 return;
0238
0239 context = kzalloc(sizeof(*context), GFP_KERNEL);
0240 if (!context)
0241 return;
0242
0243 context->data.dev = dev;
0244 acpi_initialize_hp_context(adev, &context->hp, ata_acpi_dev_notify_dock,
0245 ata_acpi_dev_uevent);
0246 }
0247
0248
0249
0250
0251
0252
0253
0254
0255
0256
0257
0258 void ata_acpi_dissociate(struct ata_host *host)
0259 {
0260 int i;
0261
0262
0263
0264
0265 for (i = 0; i < host->n_ports; i++) {
0266 struct ata_port *ap = host->ports[i];
0267 const struct ata_acpi_gtm *gtm = ata_acpi_init_gtm(ap);
0268
0269 if (ACPI_HANDLE(&ap->tdev) && gtm)
0270 ata_acpi_stm(ap, gtm);
0271 }
0272 }
0273
0274
0275
0276
0277
0278
0279
0280
0281
0282
0283
0284
0285
0286
0287 int ata_acpi_gtm(struct ata_port *ap, struct ata_acpi_gtm *gtm)
0288 {
0289 struct acpi_buffer output = { .length = ACPI_ALLOCATE_BUFFER };
0290 union acpi_object *out_obj;
0291 acpi_status status;
0292 int rc = 0;
0293 acpi_handle handle = ACPI_HANDLE(&ap->tdev);
0294
0295 if (!handle)
0296 return -EINVAL;
0297
0298 status = acpi_evaluate_object(handle, "_GTM", NULL, &output);
0299
0300 rc = -ENOENT;
0301 if (status == AE_NOT_FOUND)
0302 goto out_free;
0303
0304 rc = -EINVAL;
0305 if (ACPI_FAILURE(status)) {
0306 ata_port_err(ap, "ACPI get timing mode failed (AE 0x%x)\n",
0307 status);
0308 goto out_free;
0309 }
0310
0311 out_obj = output.pointer;
0312 if (out_obj->type != ACPI_TYPE_BUFFER) {
0313 ata_port_warn(ap, "_GTM returned unexpected object type 0x%x\n",
0314 out_obj->type);
0315
0316 goto out_free;
0317 }
0318
0319 if (out_obj->buffer.length != sizeof(struct ata_acpi_gtm)) {
0320 ata_port_err(ap, "_GTM returned invalid length %d\n",
0321 out_obj->buffer.length);
0322 goto out_free;
0323 }
0324
0325 memcpy(gtm, out_obj->buffer.pointer, sizeof(struct ata_acpi_gtm));
0326 rc = 0;
0327 out_free:
0328 kfree(output.pointer);
0329 return rc;
0330 }
0331
0332 EXPORT_SYMBOL_GPL(ata_acpi_gtm);
0333
0334
0335
0336
0337
0338
0339
0340
0341
0342
0343
0344
0345
0346
0347 int ata_acpi_stm(struct ata_port *ap, const struct ata_acpi_gtm *stm)
0348 {
0349 acpi_status status;
0350 struct ata_acpi_gtm stm_buf = *stm;
0351 struct acpi_object_list input;
0352 union acpi_object in_params[3];
0353
0354 in_params[0].type = ACPI_TYPE_BUFFER;
0355 in_params[0].buffer.length = sizeof(struct ata_acpi_gtm);
0356 in_params[0].buffer.pointer = (u8 *)&stm_buf;
0357
0358 in_params[1].type = ACPI_TYPE_BUFFER;
0359 in_params[1].buffer.length = 512;
0360 in_params[1].buffer.pointer = (u8 *)ap->link.device[0].id;
0361 in_params[2].type = ACPI_TYPE_BUFFER;
0362 in_params[2].buffer.length = 512;
0363 in_params[2].buffer.pointer = (u8 *)ap->link.device[1].id;
0364
0365 input.count = 3;
0366 input.pointer = in_params;
0367
0368 status = acpi_evaluate_object(ACPI_HANDLE(&ap->tdev), "_STM",
0369 &input, NULL);
0370
0371 if (status == AE_NOT_FOUND)
0372 return -ENOENT;
0373 if (ACPI_FAILURE(status)) {
0374 ata_port_err(ap, "ACPI set timing mode failed (status=0x%x)\n",
0375 status);
0376 return -EINVAL;
0377 }
0378 return 0;
0379 }
0380
0381 EXPORT_SYMBOL_GPL(ata_acpi_stm);
0382
0383
0384
0385
0386
0387
0388
0389
0390
0391
0392
0393
0394
0395
0396
0397
0398
0399
0400
0401
0402
0403 static int ata_dev_get_GTF(struct ata_device *dev, struct ata_acpi_gtf **gtf)
0404 {
0405 acpi_status status;
0406 struct acpi_buffer output;
0407 union acpi_object *out_obj;
0408 int rc = 0;
0409
0410
0411 if (dev->gtf_cache) {
0412 out_obj = dev->gtf_cache;
0413 goto done;
0414 }
0415
0416
0417 output.length = ACPI_ALLOCATE_BUFFER;
0418 output.pointer = NULL;
0419
0420
0421 status = acpi_evaluate_object(ata_dev_acpi_handle(dev), "_GTF", NULL,
0422 &output);
0423 out_obj = dev->gtf_cache = output.pointer;
0424
0425 if (ACPI_FAILURE(status)) {
0426 if (status != AE_NOT_FOUND) {
0427 ata_dev_warn(dev, "_GTF evaluation failed (AE 0x%x)\n",
0428 status);
0429 rc = -EINVAL;
0430 }
0431 goto out_free;
0432 }
0433
0434 if (!output.length || !output.pointer) {
0435 ata_dev_dbg(dev, "Run _GTF: length or ptr is NULL (0x%llx, 0x%p)\n",
0436 (unsigned long long)output.length,
0437 output.pointer);
0438 rc = -EINVAL;
0439 goto out_free;
0440 }
0441
0442 if (out_obj->type != ACPI_TYPE_BUFFER) {
0443 ata_dev_warn(dev, "_GTF unexpected object type 0x%x\n",
0444 out_obj->type);
0445 rc = -EINVAL;
0446 goto out_free;
0447 }
0448
0449 if (out_obj->buffer.length % REGS_PER_GTF) {
0450 ata_dev_warn(dev, "unexpected _GTF length (%d)\n",
0451 out_obj->buffer.length);
0452 rc = -EINVAL;
0453 goto out_free;
0454 }
0455
0456 done:
0457 rc = out_obj->buffer.length / REGS_PER_GTF;
0458 if (gtf) {
0459 *gtf = (void *)out_obj->buffer.pointer;
0460 ata_dev_dbg(dev, "returning gtf=%p, gtf_count=%d\n",
0461 *gtf, rc);
0462 }
0463 return rc;
0464
0465 out_free:
0466 ata_acpi_clear_gtf(dev);
0467 return rc;
0468 }
0469
0470
0471
0472
0473
0474
0475
0476
0477
0478
0479
0480
0481
0482
0483 unsigned int ata_acpi_gtm_xfermask(struct ata_device *dev,
0484 const struct ata_acpi_gtm *gtm)
0485 {
0486 unsigned int xfer_mask = 0;
0487 unsigned int type;
0488 int unit;
0489 u8 mode;
0490
0491
0492 unit = dev->devno;
0493 if (!(gtm->flags & 0x10))
0494 unit = 0;
0495
0496
0497 mode = ata_timing_cycle2mode(ATA_SHIFT_PIO, gtm->drive[unit].pio);
0498 xfer_mask |= ata_xfer_mode2mask(mode);
0499
0500
0501
0502
0503
0504 if (!(gtm->flags & (1 << (2 * unit))))
0505 type = ATA_SHIFT_MWDMA;
0506 else
0507 type = ATA_SHIFT_UDMA;
0508
0509 mode = ata_timing_cycle2mode(type, gtm->drive[unit].dma);
0510 xfer_mask |= ata_xfer_mode2mask(mode);
0511
0512 return xfer_mask;
0513 }
0514 EXPORT_SYMBOL_GPL(ata_acpi_gtm_xfermask);
0515
0516
0517
0518
0519
0520
0521
0522
0523 int ata_acpi_cbl_80wire(struct ata_port *ap, const struct ata_acpi_gtm *gtm)
0524 {
0525 struct ata_device *dev;
0526
0527 ata_for_each_dev(dev, &ap->link, ENABLED) {
0528 unsigned int xfer_mask, udma_mask;
0529
0530 xfer_mask = ata_acpi_gtm_xfermask(dev, gtm);
0531 ata_unpack_xfermask(xfer_mask, NULL, NULL, &udma_mask);
0532
0533 if (udma_mask & ~ATA_UDMA_MASK_40C)
0534 return 1;
0535 }
0536
0537 return 0;
0538 }
0539 EXPORT_SYMBOL_GPL(ata_acpi_cbl_80wire);
0540
0541 static void ata_acpi_gtf_to_tf(struct ata_device *dev,
0542 const struct ata_acpi_gtf *gtf,
0543 struct ata_taskfile *tf)
0544 {
0545 ata_tf_init(dev, tf);
0546
0547 tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
0548 tf->protocol = ATA_PROT_NODATA;
0549 tf->error = gtf->tf[0];
0550 tf->nsect = gtf->tf[1];
0551 tf->lbal = gtf->tf[2];
0552 tf->lbam = gtf->tf[3];
0553 tf->lbah = gtf->tf[4];
0554 tf->device = gtf->tf[5];
0555 tf->status = gtf->tf[6];
0556 }
0557
0558 static int ata_acpi_filter_tf(struct ata_device *dev,
0559 const struct ata_taskfile *tf,
0560 const struct ata_taskfile *ptf)
0561 {
0562 if (dev->gtf_filter & ATA_ACPI_FILTER_SETXFER) {
0563
0564
0565
0566 if (tf->command == ATA_CMD_SET_FEATURES &&
0567 tf->feature == SETFEATURES_XFER)
0568 return 1;
0569 }
0570
0571 if (dev->gtf_filter & ATA_ACPI_FILTER_LOCK) {
0572
0573
0574
0575
0576
0577 if (tf->command == ATA_CMD_CONF_OVERLAY &&
0578 tf->feature == ATA_DCO_FREEZE_LOCK)
0579 return 1;
0580
0581
0582 if (tf->command == ATA_CMD_SEC_FREEZE_LOCK)
0583 return 1;
0584
0585
0586 if ((!ptf || ptf->command != ATA_CMD_READ_NATIVE_MAX) &&
0587 tf->command == ATA_CMD_SET_MAX &&
0588 (tf->feature == ATA_SET_MAX_LOCK ||
0589 tf->feature == ATA_SET_MAX_FREEZE_LOCK))
0590 return 1;
0591 }
0592
0593 if (tf->command == ATA_CMD_SET_FEATURES &&
0594 tf->feature == SETFEATURES_SATA_ENABLE) {
0595
0596 if (dev->gtf_filter & ATA_ACPI_FILTER_DIPM &&
0597 tf->nsect == SATA_DIPM)
0598 return 1;
0599
0600
0601 if (dev->gtf_filter & ATA_ACPI_FILTER_FPDMA_OFFSET &&
0602 (tf->nsect == SATA_FPDMA_OFFSET ||
0603 tf->nsect == SATA_FPDMA_IN_ORDER))
0604 return 1;
0605
0606
0607 if (dev->gtf_filter & ATA_ACPI_FILTER_FPDMA_AA &&
0608 tf->nsect == SATA_FPDMA_AA)
0609 return 1;
0610 }
0611
0612 return 0;
0613 }
0614
0615
0616
0617
0618
0619
0620
0621
0622
0623
0624
0625
0626
0627
0628
0629
0630
0631
0632
0633
0634
0635
0636
0637
0638 static int ata_acpi_run_tf(struct ata_device *dev,
0639 const struct ata_acpi_gtf *gtf,
0640 const struct ata_acpi_gtf *prev_gtf)
0641 {
0642 struct ata_taskfile *pptf = NULL;
0643 struct ata_taskfile tf, ptf, rtf;
0644 unsigned int err_mask;
0645 const char *descr;
0646 int rc;
0647
0648 if ((gtf->tf[0] == 0) && (gtf->tf[1] == 0) && (gtf->tf[2] == 0)
0649 && (gtf->tf[3] == 0) && (gtf->tf[4] == 0) && (gtf->tf[5] == 0)
0650 && (gtf->tf[6] == 0))
0651 return 0;
0652
0653 ata_acpi_gtf_to_tf(dev, gtf, &tf);
0654 if (prev_gtf) {
0655 ata_acpi_gtf_to_tf(dev, prev_gtf, &ptf);
0656 pptf = &ptf;
0657 }
0658
0659 descr = ata_get_cmd_name(tf.command);
0660
0661 if (!ata_acpi_filter_tf(dev, &tf, pptf)) {
0662 rtf = tf;
0663 err_mask = ata_exec_internal(dev, &rtf, NULL,
0664 DMA_NONE, NULL, 0, 0);
0665
0666 switch (err_mask) {
0667 case 0:
0668 ata_dev_dbg(dev,
0669 "ACPI cmd %02x/%02x:%02x:%02x:%02x:%02x:%02x"
0670 "(%s) succeeded\n",
0671 tf.command, tf.feature, tf.nsect, tf.lbal,
0672 tf.lbam, tf.lbah, tf.device, descr);
0673 rc = 1;
0674 break;
0675
0676 case AC_ERR_DEV:
0677 ata_dev_info(dev,
0678 "ACPI cmd %02x/%02x:%02x:%02x:%02x:%02x:%02x"
0679 "(%s) rejected by device (Stat=0x%02x Err=0x%02x)",
0680 tf.command, tf.feature, tf.nsect, tf.lbal,
0681 tf.lbam, tf.lbah, tf.device, descr,
0682 rtf.status, rtf.error);
0683 rc = 0;
0684 break;
0685
0686 default:
0687 ata_dev_err(dev,
0688 "ACPI cmd %02x/%02x:%02x:%02x:%02x:%02x:%02x"
0689 "(%s) failed (Emask=0x%x Stat=0x%02x Err=0x%02x)",
0690 tf.command, tf.feature, tf.nsect, tf.lbal,
0691 tf.lbam, tf.lbah, tf.device, descr,
0692 err_mask, rtf.status, rtf.error);
0693 rc = -EIO;
0694 break;
0695 }
0696 } else {
0697 ata_dev_info(dev,
0698 "ACPI cmd %02x/%02x:%02x:%02x:%02x:%02x:%02x"
0699 "(%s) filtered out\n",
0700 tf.command, tf.feature, tf.nsect, tf.lbal,
0701 tf.lbam, tf.lbah, tf.device, descr);
0702 rc = 0;
0703 }
0704 return rc;
0705 }
0706
0707
0708
0709
0710
0711
0712
0713
0714
0715
0716
0717
0718
0719
0720
0721 static int ata_acpi_exec_tfs(struct ata_device *dev, int *nr_executed)
0722 {
0723 struct ata_acpi_gtf *gtf = NULL, *pgtf = NULL;
0724 int gtf_count, i, rc;
0725
0726
0727 rc = ata_dev_get_GTF(dev, >f);
0728 if (rc < 0)
0729 return rc;
0730 gtf_count = rc;
0731
0732
0733 for (i = 0; i < gtf_count; i++, gtf++) {
0734 rc = ata_acpi_run_tf(dev, gtf, pgtf);
0735 if (rc < 0)
0736 break;
0737 if (rc) {
0738 (*nr_executed)++;
0739 pgtf = gtf;
0740 }
0741 }
0742
0743 ata_acpi_clear_gtf(dev);
0744
0745 if (rc < 0)
0746 return rc;
0747 return 0;
0748 }
0749
0750
0751
0752
0753
0754
0755
0756
0757
0758
0759
0760
0761
0762
0763
0764
0765
0766 static int ata_acpi_push_id(struct ata_device *dev)
0767 {
0768 struct ata_port *ap = dev->link->ap;
0769 acpi_status status;
0770 struct acpi_object_list input;
0771 union acpi_object in_params[1];
0772
0773 ata_dev_dbg(dev, "%s: ix = %d, port#: %d\n",
0774 __func__, dev->devno, ap->port_no);
0775
0776
0777
0778 input.count = 1;
0779 input.pointer = in_params;
0780 in_params[0].type = ACPI_TYPE_BUFFER;
0781 in_params[0].buffer.length = sizeof(dev->id[0]) * ATA_ID_WORDS;
0782 in_params[0].buffer.pointer = (u8 *)dev->id;
0783
0784
0785
0786 swap_buf_le16(dev->id, ATA_ID_WORDS);
0787 status = acpi_evaluate_object(ata_dev_acpi_handle(dev), "_SDD", &input,
0788 NULL);
0789 swap_buf_le16(dev->id, ATA_ID_WORDS);
0790
0791 if (status == AE_NOT_FOUND)
0792 return -ENOENT;
0793
0794 if (ACPI_FAILURE(status)) {
0795 ata_dev_warn(dev, "ACPI _SDD failed (AE 0x%x)\n", status);
0796 return -EIO;
0797 }
0798
0799 return 0;
0800 }
0801
0802
0803
0804
0805
0806
0807
0808
0809
0810
0811
0812 void ata_acpi_on_resume(struct ata_port *ap)
0813 {
0814 const struct ata_acpi_gtm *gtm = ata_acpi_init_gtm(ap);
0815 struct ata_device *dev;
0816
0817 if (ACPI_HANDLE(&ap->tdev) && gtm) {
0818
0819
0820
0821 ata_acpi_stm(ap, gtm);
0822
0823
0824
0825
0826
0827 ata_for_each_dev(dev, &ap->link, ALL) {
0828 ata_acpi_clear_gtf(dev);
0829 if (ata_dev_enabled(dev) &&
0830 ata_dev_acpi_handle(dev) &&
0831 ata_dev_get_GTF(dev, NULL) >= 0)
0832 dev->flags |= ATA_DFLAG_ACPI_PENDING;
0833 }
0834 } else {
0835
0836
0837
0838
0839 ata_for_each_dev(dev, &ap->link, ALL) {
0840 ata_acpi_clear_gtf(dev);
0841 if (ata_dev_enabled(dev))
0842 dev->flags |= ATA_DFLAG_ACPI_PENDING;
0843 }
0844 }
0845 }
0846
0847 static int ata_acpi_choose_suspend_state(struct ata_device *dev, bool runtime)
0848 {
0849 int d_max_in = ACPI_STATE_D3_COLD;
0850 if (!runtime)
0851 goto out;
0852
0853
0854
0855
0856
0857 if (dev->class == ATA_DEV_ATAPI &&
0858 !(zpodd_dev_enabled(dev) && zpodd_zpready(dev)))
0859 d_max_in = ACPI_STATE_D3_HOT;
0860
0861 out:
0862 return acpi_pm_device_sleep_state(&dev->tdev, NULL, d_max_in);
0863 }
0864
0865 static void sata_acpi_set_state(struct ata_port *ap, pm_message_t state)
0866 {
0867 bool runtime = PMSG_IS_AUTO(state);
0868 struct ata_device *dev;
0869 acpi_handle handle;
0870 int acpi_state;
0871
0872 ata_for_each_dev(dev, &ap->link, ENABLED) {
0873 handle = ata_dev_acpi_handle(dev);
0874 if (!handle)
0875 continue;
0876
0877 if (!(state.event & PM_EVENT_RESUME)) {
0878 acpi_state = ata_acpi_choose_suspend_state(dev, runtime);
0879 if (acpi_state == ACPI_STATE_D0)
0880 continue;
0881 if (runtime && zpodd_dev_enabled(dev) &&
0882 acpi_state == ACPI_STATE_D3_COLD)
0883 zpodd_enable_run_wake(dev);
0884 acpi_bus_set_power(handle, acpi_state);
0885 } else {
0886 if (runtime && zpodd_dev_enabled(dev))
0887 zpodd_disable_run_wake(dev);
0888 acpi_bus_set_power(handle, ACPI_STATE_D0);
0889 }
0890 }
0891 }
0892
0893
0894 static void pata_acpi_set_state(struct ata_port *ap, pm_message_t state)
0895 {
0896 struct ata_device *dev;
0897 acpi_handle port_handle;
0898
0899 port_handle = ACPI_HANDLE(&ap->tdev);
0900 if (!port_handle)
0901 return;
0902
0903
0904
0905 if (state.event & PM_EVENT_RESUME)
0906 acpi_bus_set_power(port_handle, ACPI_STATE_D0);
0907
0908 ata_for_each_dev(dev, &ap->link, ENABLED) {
0909 acpi_handle dev_handle = ata_dev_acpi_handle(dev);
0910 if (!dev_handle)
0911 continue;
0912
0913 acpi_bus_set_power(dev_handle, state.event & PM_EVENT_RESUME ?
0914 ACPI_STATE_D0 : ACPI_STATE_D3_COLD);
0915 }
0916
0917 if (!(state.event & PM_EVENT_RESUME))
0918 acpi_bus_set_power(port_handle, ACPI_STATE_D3_COLD);
0919 }
0920
0921
0922
0923
0924
0925
0926
0927
0928
0929 void ata_acpi_set_state(struct ata_port *ap, pm_message_t state)
0930 {
0931 if (ap->flags & ATA_FLAG_ACPI_SATA)
0932 sata_acpi_set_state(ap, state);
0933 else
0934 pata_acpi_set_state(ap, state);
0935 }
0936
0937
0938
0939
0940
0941
0942
0943
0944
0945
0946
0947
0948
0949
0950
0951 int ata_acpi_on_devcfg(struct ata_device *dev)
0952 {
0953 struct ata_port *ap = dev->link->ap;
0954 struct ata_eh_context *ehc = &ap->link.eh_context;
0955 int acpi_sata = ap->flags & ATA_FLAG_ACPI_SATA;
0956 int nr_executed = 0;
0957 int rc;
0958
0959 if (!ata_dev_acpi_handle(dev))
0960 return 0;
0961
0962
0963 if (!(dev->flags & ATA_DFLAG_ACPI_PENDING) &&
0964 !(acpi_sata && (ehc->i.flags & ATA_EHI_DID_HARDRESET)))
0965 return 0;
0966
0967
0968 if (acpi_sata) {
0969 rc = ata_acpi_push_id(dev);
0970 if (rc && rc != -ENOENT)
0971 goto acpi_err;
0972 }
0973
0974
0975 rc = ata_acpi_exec_tfs(dev, &nr_executed);
0976 if (rc)
0977 goto acpi_err;
0978
0979 dev->flags &= ~ATA_DFLAG_ACPI_PENDING;
0980
0981
0982 if (nr_executed) {
0983 rc = ata_dev_reread_id(dev, 0);
0984 if (rc < 0) {
0985 ata_dev_err(dev,
0986 "failed to IDENTIFY after ACPI commands\n");
0987 return rc;
0988 }
0989 }
0990
0991 return 0;
0992
0993 acpi_err:
0994
0995 if (rc == -EINVAL && !nr_executed && !(ap->pflags & ATA_PFLAG_FROZEN))
0996 return 0;
0997
0998
0999 if (!(dev->flags & ATA_DFLAG_ACPI_FAILED)) {
1000 dev->flags |= ATA_DFLAG_ACPI_FAILED;
1001 return rc;
1002 }
1003
1004 dev->flags |= ATA_DFLAG_ACPI_DISABLED;
1005 ata_dev_warn(dev, "ACPI: failed the second time, disabled\n");
1006
1007
1008
1009
1010 if (!nr_executed && !(ap->pflags & ATA_PFLAG_FROZEN))
1011 return 0;
1012
1013 return rc;
1014 }
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025 void ata_acpi_on_disable(struct ata_device *dev)
1026 {
1027 ata_acpi_clear_gtf(dev);
1028 }