0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014 #include <linux/kernel.h>
0015 #include <linux/blkdev.h>
0016 #include <linux/export.h>
0017 #include <linux/pci.h>
0018 #include <scsi/scsi.h>
0019 #include <scsi/scsi_host.h>
0020 #include <scsi/scsi_eh.h>
0021 #include <scsi/scsi_device.h>
0022 #include <scsi/scsi_cmnd.h>
0023 #include <scsi/scsi_dbg.h>
0024 #include "../scsi/scsi_transport_api.h"
0025
0026 #include <linux/libata.h>
0027
0028 #include <trace/events/libata.h>
0029 #include "libata.h"
0030
0031 enum {
0032
0033 ATA_EH_SPDN_NCQ_OFF = (1 << 0),
0034 ATA_EH_SPDN_SPEED_DOWN = (1 << 1),
0035 ATA_EH_SPDN_FALLBACK_TO_PIO = (1 << 2),
0036 ATA_EH_SPDN_KEEP_ERRORS = (1 << 3),
0037
0038
0039 ATA_EFLAG_IS_IO = (1 << 0),
0040 ATA_EFLAG_DUBIOUS_XFER = (1 << 1),
0041 ATA_EFLAG_OLD_ER = (1 << 31),
0042
0043
0044 ATA_ECAT_NONE = 0,
0045 ATA_ECAT_ATA_BUS = 1,
0046 ATA_ECAT_TOUT_HSM = 2,
0047 ATA_ECAT_UNK_DEV = 3,
0048 ATA_ECAT_DUBIOUS_NONE = 4,
0049 ATA_ECAT_DUBIOUS_ATA_BUS = 5,
0050 ATA_ECAT_DUBIOUS_TOUT_HSM = 6,
0051 ATA_ECAT_DUBIOUS_UNK_DEV = 7,
0052 ATA_ECAT_NR = 8,
0053
0054 ATA_EH_CMD_DFL_TIMEOUT = 5000,
0055
0056
0057 ATA_EH_RESET_COOL_DOWN = 5000,
0058
0059
0060
0061
0062
0063
0064 ATA_EH_PRERESET_TIMEOUT = 10000,
0065 ATA_EH_FASTDRAIN_INTERVAL = 3000,
0066
0067 ATA_EH_UA_TRIES = 5,
0068
0069
0070 ATA_EH_PROBE_TRIAL_INTERVAL = 60000,
0071 ATA_EH_PROBE_TRIALS = 2,
0072 };
0073
0074
0075
0076
0077
0078
0079
0080
0081 static const unsigned long ata_eh_reset_timeouts[] = {
0082 10000,
0083 10000,
0084 35000,
0085 5000,
0086 ULONG_MAX,
0087 };
0088
0089 static const unsigned int ata_eh_identify_timeouts[] = {
0090 5000,
0091 10000,
0092 30000,
0093 UINT_MAX,
0094 };
0095
0096 static const unsigned int ata_eh_revalidate_timeouts[] = {
0097 15000,
0098 15000,
0099 UINT_MAX,
0100 };
0101
0102 static const unsigned int ata_eh_flush_timeouts[] = {
0103 15000,
0104 15000,
0105 30000,
0106 UINT_MAX,
0107 };
0108
0109 static const unsigned int ata_eh_other_timeouts[] = {
0110 5000,
0111 10000,
0112
0113 UINT_MAX,
0114 };
0115
0116 struct ata_eh_cmd_timeout_ent {
0117 const u8 *commands;
0118 const unsigned int *timeouts;
0119 };
0120
0121
0122
0123
0124
0125
0126
0127
0128
0129
0130
0131
0132
0133 #define CMDS(cmds...) (const u8 []){ cmds, 0 }
0134 static const struct ata_eh_cmd_timeout_ent
0135 ata_eh_cmd_timeout_table[ATA_EH_CMD_TIMEOUT_TABLE_SIZE] = {
0136 { .commands = CMDS(ATA_CMD_ID_ATA, ATA_CMD_ID_ATAPI),
0137 .timeouts = ata_eh_identify_timeouts, },
0138 { .commands = CMDS(ATA_CMD_READ_LOG_EXT, ATA_CMD_READ_LOG_DMA_EXT),
0139 .timeouts = ata_eh_revalidate_timeouts, },
0140 { .commands = CMDS(ATA_CMD_READ_NATIVE_MAX, ATA_CMD_READ_NATIVE_MAX_EXT),
0141 .timeouts = ata_eh_other_timeouts, },
0142 { .commands = CMDS(ATA_CMD_SET_MAX, ATA_CMD_SET_MAX_EXT),
0143 .timeouts = ata_eh_other_timeouts, },
0144 { .commands = CMDS(ATA_CMD_SET_FEATURES),
0145 .timeouts = ata_eh_other_timeouts, },
0146 { .commands = CMDS(ATA_CMD_INIT_DEV_PARAMS),
0147 .timeouts = ata_eh_other_timeouts, },
0148 { .commands = CMDS(ATA_CMD_FLUSH, ATA_CMD_FLUSH_EXT),
0149 .timeouts = ata_eh_flush_timeouts },
0150 };
0151 #undef CMDS
0152
0153 static void __ata_port_freeze(struct ata_port *ap);
0154 #ifdef CONFIG_PM
0155 static void ata_eh_handle_port_suspend(struct ata_port *ap);
0156 static void ata_eh_handle_port_resume(struct ata_port *ap);
0157 #else
0158 static void ata_eh_handle_port_suspend(struct ata_port *ap)
0159 { }
0160
0161 static void ata_eh_handle_port_resume(struct ata_port *ap)
0162 { }
0163 #endif
0164
0165 static __printf(2, 0) void __ata_ehi_pushv_desc(struct ata_eh_info *ehi,
0166 const char *fmt, va_list args)
0167 {
0168 ehi->desc_len += vscnprintf(ehi->desc + ehi->desc_len,
0169 ATA_EH_DESC_LEN - ehi->desc_len,
0170 fmt, args);
0171 }
0172
0173
0174
0175
0176
0177
0178
0179
0180
0181
0182
0183 void __ata_ehi_push_desc(struct ata_eh_info *ehi, const char *fmt, ...)
0184 {
0185 va_list args;
0186
0187 va_start(args, fmt);
0188 __ata_ehi_pushv_desc(ehi, fmt, args);
0189 va_end(args);
0190 }
0191 EXPORT_SYMBOL_GPL(__ata_ehi_push_desc);
0192
0193
0194
0195
0196
0197
0198
0199
0200
0201
0202
0203
0204 void ata_ehi_push_desc(struct ata_eh_info *ehi, const char *fmt, ...)
0205 {
0206 va_list args;
0207
0208 if (ehi->desc_len)
0209 __ata_ehi_push_desc(ehi, ", ");
0210
0211 va_start(args, fmt);
0212 __ata_ehi_pushv_desc(ehi, fmt, args);
0213 va_end(args);
0214 }
0215 EXPORT_SYMBOL_GPL(ata_ehi_push_desc);
0216
0217
0218
0219
0220
0221
0222
0223
0224
0225
0226 void ata_ehi_clear_desc(struct ata_eh_info *ehi)
0227 {
0228 ehi->desc[0] = '\0';
0229 ehi->desc_len = 0;
0230 }
0231 EXPORT_SYMBOL_GPL(ata_ehi_clear_desc);
0232
0233
0234
0235
0236
0237
0238
0239
0240
0241
0242
0243
0244
0245
0246 void ata_port_desc(struct ata_port *ap, const char *fmt, ...)
0247 {
0248 va_list args;
0249
0250 WARN_ON(!(ap->pflags & ATA_PFLAG_INITIALIZING));
0251
0252 if (ap->link.eh_info.desc_len)
0253 __ata_ehi_push_desc(&ap->link.eh_info, " ");
0254
0255 va_start(args, fmt);
0256 __ata_ehi_pushv_desc(&ap->link.eh_info, fmt, args);
0257 va_end(args);
0258 }
0259 EXPORT_SYMBOL_GPL(ata_port_desc);
0260
0261 #ifdef CONFIG_PCI
0262
0263
0264
0265
0266
0267
0268
0269
0270
0271
0272
0273
0274
0275
0276
0277 void ata_port_pbar_desc(struct ata_port *ap, int bar, ssize_t offset,
0278 const char *name)
0279 {
0280 struct pci_dev *pdev = to_pci_dev(ap->host->dev);
0281 char *type = "";
0282 unsigned long long start, len;
0283
0284 if (pci_resource_flags(pdev, bar) & IORESOURCE_MEM)
0285 type = "m";
0286 else if (pci_resource_flags(pdev, bar) & IORESOURCE_IO)
0287 type = "i";
0288
0289 start = (unsigned long long)pci_resource_start(pdev, bar);
0290 len = (unsigned long long)pci_resource_len(pdev, bar);
0291
0292 if (offset < 0)
0293 ata_port_desc(ap, "%s %s%llu@0x%llx", name, type, len, start);
0294 else
0295 ata_port_desc(ap, "%s 0x%llx", name,
0296 start + (unsigned long long)offset);
0297 }
0298 EXPORT_SYMBOL_GPL(ata_port_pbar_desc);
0299 #endif
0300
0301 static int ata_lookup_timeout_table(u8 cmd)
0302 {
0303 int i;
0304
0305 for (i = 0; i < ATA_EH_CMD_TIMEOUT_TABLE_SIZE; i++) {
0306 const u8 *cur;
0307
0308 for (cur = ata_eh_cmd_timeout_table[i].commands; *cur; cur++)
0309 if (*cur == cmd)
0310 return i;
0311 }
0312
0313 return -1;
0314 }
0315
0316
0317
0318
0319
0320
0321
0322
0323
0324
0325
0326
0327
0328
0329 unsigned int ata_internal_cmd_timeout(struct ata_device *dev, u8 cmd)
0330 {
0331 struct ata_eh_context *ehc = &dev->link->eh_context;
0332 int ent = ata_lookup_timeout_table(cmd);
0333 int idx;
0334
0335 if (ent < 0)
0336 return ATA_EH_CMD_DFL_TIMEOUT;
0337
0338 idx = ehc->cmd_timeout_idx[dev->devno][ent];
0339 return ata_eh_cmd_timeout_table[ent].timeouts[idx];
0340 }
0341
0342
0343
0344
0345
0346
0347
0348
0349
0350
0351
0352
0353
0354 void ata_internal_cmd_timed_out(struct ata_device *dev, u8 cmd)
0355 {
0356 struct ata_eh_context *ehc = &dev->link->eh_context;
0357 int ent = ata_lookup_timeout_table(cmd);
0358 int idx;
0359
0360 if (ent < 0)
0361 return;
0362
0363 idx = ehc->cmd_timeout_idx[dev->devno][ent];
0364 if (ata_eh_cmd_timeout_table[ent].timeouts[idx + 1] != UINT_MAX)
0365 ehc->cmd_timeout_idx[dev->devno][ent]++;
0366 }
0367
0368 static void ata_ering_record(struct ata_ering *ering, unsigned int eflags,
0369 unsigned int err_mask)
0370 {
0371 struct ata_ering_entry *ent;
0372
0373 WARN_ON(!err_mask);
0374
0375 ering->cursor++;
0376 ering->cursor %= ATA_ERING_SIZE;
0377
0378 ent = &ering->ring[ering->cursor];
0379 ent->eflags = eflags;
0380 ent->err_mask = err_mask;
0381 ent->timestamp = get_jiffies_64();
0382 }
0383
0384 static struct ata_ering_entry *ata_ering_top(struct ata_ering *ering)
0385 {
0386 struct ata_ering_entry *ent = &ering->ring[ering->cursor];
0387
0388 if (ent->err_mask)
0389 return ent;
0390 return NULL;
0391 }
0392
0393 int ata_ering_map(struct ata_ering *ering,
0394 int (*map_fn)(struct ata_ering_entry *, void *),
0395 void *arg)
0396 {
0397 int idx, rc = 0;
0398 struct ata_ering_entry *ent;
0399
0400 idx = ering->cursor;
0401 do {
0402 ent = &ering->ring[idx];
0403 if (!ent->err_mask)
0404 break;
0405 rc = map_fn(ent, arg);
0406 if (rc)
0407 break;
0408 idx = (idx - 1 + ATA_ERING_SIZE) % ATA_ERING_SIZE;
0409 } while (idx != ering->cursor);
0410
0411 return rc;
0412 }
0413
0414 static int ata_ering_clear_cb(struct ata_ering_entry *ent, void *void_arg)
0415 {
0416 ent->eflags |= ATA_EFLAG_OLD_ER;
0417 return 0;
0418 }
0419
0420 static void ata_ering_clear(struct ata_ering *ering)
0421 {
0422 ata_ering_map(ering, ata_ering_clear_cb, NULL);
0423 }
0424
0425 static unsigned int ata_eh_dev_action(struct ata_device *dev)
0426 {
0427 struct ata_eh_context *ehc = &dev->link->eh_context;
0428
0429 return ehc->i.action | ehc->i.dev_action[dev->devno];
0430 }
0431
0432 static void ata_eh_clear_action(struct ata_link *link, struct ata_device *dev,
0433 struct ata_eh_info *ehi, unsigned int action)
0434 {
0435 struct ata_device *tdev;
0436
0437 if (!dev) {
0438 ehi->action &= ~action;
0439 ata_for_each_dev(tdev, link, ALL)
0440 ehi->dev_action[tdev->devno] &= ~action;
0441 } else {
0442
0443 WARN_ON(!(action & ATA_EH_PERDEV_MASK));
0444
0445
0446 if (ehi->action & action) {
0447 ata_for_each_dev(tdev, link, ALL)
0448 ehi->dev_action[tdev->devno] |=
0449 ehi->action & action;
0450 ehi->action &= ~action;
0451 }
0452
0453
0454 ehi->dev_action[dev->devno] &= ~action;
0455 }
0456 }
0457
0458
0459
0460
0461
0462
0463
0464
0465
0466
0467
0468
0469 void ata_eh_acquire(struct ata_port *ap)
0470 {
0471 mutex_lock(&ap->host->eh_mutex);
0472 WARN_ON_ONCE(ap->host->eh_owner);
0473 ap->host->eh_owner = current;
0474 }
0475
0476
0477
0478
0479
0480
0481
0482
0483
0484
0485
0486 void ata_eh_release(struct ata_port *ap)
0487 {
0488 WARN_ON_ONCE(ap->host->eh_owner != current);
0489 ap->host->eh_owner = NULL;
0490 mutex_unlock(&ap->host->eh_mutex);
0491 }
0492
0493 static void ata_eh_unload(struct ata_port *ap)
0494 {
0495 struct ata_link *link;
0496 struct ata_device *dev;
0497 unsigned long flags;
0498
0499
0500
0501
0502 ata_for_each_link(link, ap, PMP_FIRST) {
0503 sata_scr_write(link, SCR_CONTROL, link->saved_scontrol & 0xff0);
0504 ata_for_each_dev(dev, link, ALL)
0505 ata_dev_disable(dev);
0506 }
0507
0508
0509 spin_lock_irqsave(ap->lock, flags);
0510
0511 ata_port_freeze(ap);
0512 ap->pflags &= ~ATA_PFLAG_EH_PENDING;
0513 ap->pflags |= ATA_PFLAG_UNLOADED;
0514
0515 spin_unlock_irqrestore(ap->lock, flags);
0516 }
0517
0518
0519
0520
0521
0522
0523
0524
0525
0526
0527
0528
0529
0530 void ata_scsi_error(struct Scsi_Host *host)
0531 {
0532 struct ata_port *ap = ata_shost_to_port(host);
0533 unsigned long flags;
0534 LIST_HEAD(eh_work_q);
0535
0536 spin_lock_irqsave(host->host_lock, flags);
0537 list_splice_init(&host->eh_cmd_q, &eh_work_q);
0538 spin_unlock_irqrestore(host->host_lock, flags);
0539
0540 ata_scsi_cmd_error_handler(host, ap, &eh_work_q);
0541
0542
0543
0544 ata_scsi_port_error_handler(host, ap);
0545
0546
0547 WARN_ON(!list_empty(&eh_work_q));
0548
0549 }
0550
0551
0552
0553
0554
0555
0556
0557
0558
0559
0560
0561 void ata_scsi_cmd_error_handler(struct Scsi_Host *host, struct ata_port *ap,
0562 struct list_head *eh_work_q)
0563 {
0564 int i;
0565 unsigned long flags;
0566
0567
0568 ata_sff_flush_pio_task(ap);
0569
0570
0571
0572
0573
0574
0575
0576
0577
0578
0579
0580
0581
0582
0583
0584 spin_lock_irqsave(ap->lock, flags);
0585 if (ap->ops->error_handler) {
0586 struct scsi_cmnd *scmd, *tmp;
0587 int nr_timedout = 0;
0588
0589
0590
0591
0592
0593
0594
0595
0596
0597
0598 if (ap->ops->lost_interrupt)
0599 ap->ops->lost_interrupt(ap);
0600
0601 list_for_each_entry_safe(scmd, tmp, eh_work_q, eh_entry) {
0602 struct ata_queued_cmd *qc;
0603
0604 ata_qc_for_each_raw(ap, qc, i) {
0605 if (qc->flags & ATA_QCFLAG_ACTIVE &&
0606 qc->scsicmd == scmd)
0607 break;
0608 }
0609
0610 if (i < ATA_MAX_QUEUE) {
0611
0612 if (!(qc->flags & ATA_QCFLAG_FAILED)) {
0613
0614 qc->err_mask |= AC_ERR_TIMEOUT;
0615 qc->flags |= ATA_QCFLAG_FAILED;
0616 nr_timedout++;
0617 }
0618 } else {
0619
0620
0621
0622
0623 scmd->retries = scmd->allowed;
0624 scsi_eh_finish_cmd(scmd, &ap->eh_done_q);
0625 }
0626 }
0627
0628
0629
0630
0631
0632
0633
0634 if (nr_timedout)
0635 __ata_port_freeze(ap);
0636
0637
0638
0639 ap->eh_tries = ATA_EH_MAX_TRIES;
0640 }
0641 spin_unlock_irqrestore(ap->lock, flags);
0642
0643 }
0644 EXPORT_SYMBOL(ata_scsi_cmd_error_handler);
0645
0646
0647
0648
0649
0650
0651
0652
0653
0654 void ata_scsi_port_error_handler(struct Scsi_Host *host, struct ata_port *ap)
0655 {
0656 unsigned long flags;
0657
0658
0659 if (ap->ops->error_handler) {
0660 struct ata_link *link;
0661
0662
0663 ata_eh_acquire(ap);
0664 repeat:
0665
0666 del_timer_sync(&ap->fastdrain_timer);
0667
0668
0669 ata_eh_handle_port_resume(ap);
0670
0671
0672 spin_lock_irqsave(ap->lock, flags);
0673
0674 ata_for_each_link(link, ap, HOST_FIRST) {
0675 struct ata_eh_context *ehc = &link->eh_context;
0676 struct ata_device *dev;
0677
0678 memset(&link->eh_context, 0, sizeof(link->eh_context));
0679 link->eh_context.i = link->eh_info;
0680 memset(&link->eh_info, 0, sizeof(link->eh_info));
0681
0682 ata_for_each_dev(dev, link, ENABLED) {
0683 int devno = dev->devno;
0684
0685 ehc->saved_xfer_mode[devno] = dev->xfer_mode;
0686 if (ata_ncq_enabled(dev))
0687 ehc->saved_ncq_enabled |= 1 << devno;
0688 }
0689 }
0690
0691 ap->pflags |= ATA_PFLAG_EH_IN_PROGRESS;
0692 ap->pflags &= ~ATA_PFLAG_EH_PENDING;
0693 ap->excl_link = NULL;
0694
0695 spin_unlock_irqrestore(ap->lock, flags);
0696
0697
0698 if (!(ap->pflags & (ATA_PFLAG_UNLOADING | ATA_PFLAG_SUSPENDED)))
0699 ap->ops->error_handler(ap);
0700 else {
0701
0702 if ((ap->pflags & ATA_PFLAG_UNLOADING) &&
0703 !(ap->pflags & ATA_PFLAG_UNLOADED))
0704 ata_eh_unload(ap);
0705 ata_eh_finish(ap);
0706 }
0707
0708
0709 ata_eh_handle_port_suspend(ap);
0710
0711
0712
0713
0714
0715 spin_lock_irqsave(ap->lock, flags);
0716
0717 if (ap->pflags & ATA_PFLAG_EH_PENDING) {
0718 if (--ap->eh_tries) {
0719 spin_unlock_irqrestore(ap->lock, flags);
0720 goto repeat;
0721 }
0722 ata_port_err(ap,
0723 "EH pending after %d tries, giving up\n",
0724 ATA_EH_MAX_TRIES);
0725 ap->pflags &= ~ATA_PFLAG_EH_PENDING;
0726 }
0727
0728
0729 ata_for_each_link(link, ap, HOST_FIRST)
0730 memset(&link->eh_info, 0, sizeof(link->eh_info));
0731
0732
0733
0734
0735
0736
0737 ap->ops->end_eh(ap);
0738
0739 spin_unlock_irqrestore(ap->lock, flags);
0740 ata_eh_release(ap);
0741 } else {
0742 WARN_ON(ata_qc_from_tag(ap, ap->link.active_tag) == NULL);
0743 ap->ops->eng_timeout(ap);
0744 }
0745
0746 scsi_eh_flush_done_q(&ap->eh_done_q);
0747
0748
0749 spin_lock_irqsave(ap->lock, flags);
0750
0751 if (ap->pflags & ATA_PFLAG_LOADING)
0752 ap->pflags &= ~ATA_PFLAG_LOADING;
0753 else if ((ap->pflags & ATA_PFLAG_SCSI_HOTPLUG) &&
0754 !(ap->flags & ATA_FLAG_SAS_HOST))
0755 schedule_delayed_work(&ap->hotplug_task, 0);
0756
0757 if (ap->pflags & ATA_PFLAG_RECOVERED)
0758 ata_port_info(ap, "EH complete\n");
0759
0760 ap->pflags &= ~(ATA_PFLAG_SCSI_HOTPLUG | ATA_PFLAG_RECOVERED);
0761
0762
0763 ap->pflags &= ~ATA_PFLAG_EH_IN_PROGRESS;
0764 wake_up_all(&ap->eh_wait_q);
0765
0766 spin_unlock_irqrestore(ap->lock, flags);
0767 }
0768 EXPORT_SYMBOL_GPL(ata_scsi_port_error_handler);
0769
0770
0771
0772
0773
0774
0775
0776
0777
0778
0779 void ata_port_wait_eh(struct ata_port *ap)
0780 {
0781 unsigned long flags;
0782 DEFINE_WAIT(wait);
0783
0784 retry:
0785 spin_lock_irqsave(ap->lock, flags);
0786
0787 while (ap->pflags & (ATA_PFLAG_EH_PENDING | ATA_PFLAG_EH_IN_PROGRESS)) {
0788 prepare_to_wait(&ap->eh_wait_q, &wait, TASK_UNINTERRUPTIBLE);
0789 spin_unlock_irqrestore(ap->lock, flags);
0790 schedule();
0791 spin_lock_irqsave(ap->lock, flags);
0792 }
0793 finish_wait(&ap->eh_wait_q, &wait);
0794
0795 spin_unlock_irqrestore(ap->lock, flags);
0796
0797
0798 if (scsi_host_in_recovery(ap->scsi_host)) {
0799 ata_msleep(ap, 10);
0800 goto retry;
0801 }
0802 }
0803 EXPORT_SYMBOL_GPL(ata_port_wait_eh);
0804
0805 static unsigned int ata_eh_nr_in_flight(struct ata_port *ap)
0806 {
0807 struct ata_queued_cmd *qc;
0808 unsigned int tag;
0809 unsigned int nr = 0;
0810
0811
0812 ata_qc_for_each(ap, qc, tag) {
0813 if (qc)
0814 nr++;
0815 }
0816
0817 return nr;
0818 }
0819
0820 void ata_eh_fastdrain_timerfn(struct timer_list *t)
0821 {
0822 struct ata_port *ap = from_timer(ap, t, fastdrain_timer);
0823 unsigned long flags;
0824 unsigned int cnt;
0825
0826 spin_lock_irqsave(ap->lock, flags);
0827
0828 cnt = ata_eh_nr_in_flight(ap);
0829
0830
0831 if (!cnt)
0832 goto out_unlock;
0833
0834 if (cnt == ap->fastdrain_cnt) {
0835 struct ata_queued_cmd *qc;
0836 unsigned int tag;
0837
0838
0839
0840
0841 ata_qc_for_each(ap, qc, tag) {
0842 if (qc)
0843 qc->err_mask |= AC_ERR_TIMEOUT;
0844 }
0845
0846 ata_port_freeze(ap);
0847 } else {
0848
0849 ap->fastdrain_cnt = cnt;
0850 ap->fastdrain_timer.expires =
0851 ata_deadline(jiffies, ATA_EH_FASTDRAIN_INTERVAL);
0852 add_timer(&ap->fastdrain_timer);
0853 }
0854
0855 out_unlock:
0856 spin_unlock_irqrestore(ap->lock, flags);
0857 }
0858
0859
0860
0861
0862
0863
0864
0865
0866
0867
0868
0869
0870
0871 static void ata_eh_set_pending(struct ata_port *ap, int fastdrain)
0872 {
0873 unsigned int cnt;
0874
0875
0876 if (ap->pflags & ATA_PFLAG_EH_PENDING)
0877 return;
0878
0879 ap->pflags |= ATA_PFLAG_EH_PENDING;
0880
0881 if (!fastdrain)
0882 return;
0883
0884
0885 cnt = ata_eh_nr_in_flight(ap);
0886 if (!cnt)
0887 return;
0888
0889
0890 ap->fastdrain_cnt = cnt;
0891 ap->fastdrain_timer.expires =
0892 ata_deadline(jiffies, ATA_EH_FASTDRAIN_INTERVAL);
0893 add_timer(&ap->fastdrain_timer);
0894 }
0895
0896
0897
0898
0899
0900
0901
0902
0903
0904
0905
0906 void ata_qc_schedule_eh(struct ata_queued_cmd *qc)
0907 {
0908 struct ata_port *ap = qc->ap;
0909
0910 WARN_ON(!ap->ops->error_handler);
0911
0912 qc->flags |= ATA_QCFLAG_FAILED;
0913 ata_eh_set_pending(ap, 1);
0914
0915
0916
0917
0918
0919
0920 blk_abort_request(scsi_cmd_to_rq(qc->scsicmd));
0921 }
0922
0923
0924
0925
0926
0927
0928
0929
0930 void ata_std_sched_eh(struct ata_port *ap)
0931 {
0932 WARN_ON(!ap->ops->error_handler);
0933
0934 if (ap->pflags & ATA_PFLAG_INITIALIZING)
0935 return;
0936
0937 ata_eh_set_pending(ap, 1);
0938 scsi_schedule_eh(ap->scsi_host);
0939
0940 trace_ata_std_sched_eh(ap);
0941 }
0942 EXPORT_SYMBOL_GPL(ata_std_sched_eh);
0943
0944
0945
0946
0947
0948
0949
0950
0951
0952
0953
0954
0955
0956 void ata_std_end_eh(struct ata_port *ap)
0957 {
0958 struct Scsi_Host *host = ap->scsi_host;
0959
0960 host->host_eh_scheduled = 0;
0961 }
0962 EXPORT_SYMBOL(ata_std_end_eh);
0963
0964
0965
0966
0967
0968
0969
0970
0971
0972
0973
0974
0975 void ata_port_schedule_eh(struct ata_port *ap)
0976 {
0977
0978 ap->ops->sched_eh(ap);
0979 }
0980 EXPORT_SYMBOL_GPL(ata_port_schedule_eh);
0981
0982 static int ata_do_link_abort(struct ata_port *ap, struct ata_link *link)
0983 {
0984 struct ata_queued_cmd *qc;
0985 int tag, nr_aborted = 0;
0986
0987 WARN_ON(!ap->ops->error_handler);
0988
0989
0990 ata_eh_set_pending(ap, 0);
0991
0992
0993 ata_qc_for_each_with_internal(ap, qc, tag) {
0994 if (qc && (!link || qc->dev->link == link)) {
0995 qc->flags |= ATA_QCFLAG_FAILED;
0996 ata_qc_complete(qc);
0997 nr_aborted++;
0998 }
0999 }
1000
1001 if (!nr_aborted)
1002 ata_port_schedule_eh(ap);
1003
1004 return nr_aborted;
1005 }
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019 int ata_link_abort(struct ata_link *link)
1020 {
1021 return ata_do_link_abort(link->ap, link);
1022 }
1023 EXPORT_SYMBOL_GPL(ata_link_abort);
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037 int ata_port_abort(struct ata_port *ap)
1038 {
1039 return ata_do_link_abort(ap, NULL);
1040 }
1041 EXPORT_SYMBOL_GPL(ata_port_abort);
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061 static void __ata_port_freeze(struct ata_port *ap)
1062 {
1063 WARN_ON(!ap->ops->error_handler);
1064
1065 if (ap->ops->freeze)
1066 ap->ops->freeze(ap);
1067
1068 ap->pflags |= ATA_PFLAG_FROZEN;
1069
1070 trace_ata_port_freeze(ap);
1071 }
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087 int ata_port_freeze(struct ata_port *ap)
1088 {
1089 int nr_aborted;
1090
1091 WARN_ON(!ap->ops->error_handler);
1092
1093 __ata_port_freeze(ap);
1094 nr_aborted = ata_port_abort(ap);
1095
1096 return nr_aborted;
1097 }
1098 EXPORT_SYMBOL_GPL(ata_port_freeze);
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109 void ata_eh_freeze_port(struct ata_port *ap)
1110 {
1111 unsigned long flags;
1112
1113 if (!ap->ops->error_handler)
1114 return;
1115
1116 spin_lock_irqsave(ap->lock, flags);
1117 __ata_port_freeze(ap);
1118 spin_unlock_irqrestore(ap->lock, flags);
1119 }
1120 EXPORT_SYMBOL_GPL(ata_eh_freeze_port);
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131 void ata_eh_thaw_port(struct ata_port *ap)
1132 {
1133 unsigned long flags;
1134
1135 if (!ap->ops->error_handler)
1136 return;
1137
1138 spin_lock_irqsave(ap->lock, flags);
1139
1140 ap->pflags &= ~ATA_PFLAG_FROZEN;
1141
1142 if (ap->ops->thaw)
1143 ap->ops->thaw(ap);
1144
1145 spin_unlock_irqrestore(ap->lock, flags);
1146
1147 trace_ata_port_thaw(ap);
1148 }
1149
1150 static void ata_eh_scsidone(struct scsi_cmnd *scmd)
1151 {
1152
1153 }
1154
1155 static void __ata_eh_qc_complete(struct ata_queued_cmd *qc)
1156 {
1157 struct ata_port *ap = qc->ap;
1158 struct scsi_cmnd *scmd = qc->scsicmd;
1159 unsigned long flags;
1160
1161 spin_lock_irqsave(ap->lock, flags);
1162 qc->scsidone = ata_eh_scsidone;
1163 __ata_qc_complete(qc);
1164 WARN_ON(ata_tag_valid(qc->tag));
1165 spin_unlock_irqrestore(ap->lock, flags);
1166
1167 scsi_eh_finish_cmd(scmd, &ap->eh_done_q);
1168 }
1169
1170
1171
1172
1173
1174
1175
1176
1177 void ata_eh_qc_complete(struct ata_queued_cmd *qc)
1178 {
1179 struct scsi_cmnd *scmd = qc->scsicmd;
1180 scmd->retries = scmd->allowed;
1181 __ata_eh_qc_complete(qc);
1182 }
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195 void ata_eh_qc_retry(struct ata_queued_cmd *qc)
1196 {
1197 struct scsi_cmnd *scmd = qc->scsicmd;
1198 if (!qc->err_mask)
1199 scmd->allowed++;
1200 __ata_eh_qc_complete(qc);
1201 }
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212 void ata_dev_disable(struct ata_device *dev)
1213 {
1214 if (!ata_dev_enabled(dev))
1215 return;
1216
1217 ata_dev_warn(dev, "disable device\n");
1218 ata_acpi_on_disable(dev);
1219 ata_down_xfermask_limit(dev, ATA_DNXFER_FORCE_PIO0 | ATA_DNXFER_QUIET);
1220 dev->class++;
1221
1222
1223
1224
1225 ata_ering_clear(&dev->ering);
1226 }
1227 EXPORT_SYMBOL_GPL(ata_dev_disable);
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238 void ata_eh_detach_dev(struct ata_device *dev)
1239 {
1240 struct ata_link *link = dev->link;
1241 struct ata_port *ap = link->ap;
1242 struct ata_eh_context *ehc = &link->eh_context;
1243 unsigned long flags;
1244
1245 ata_dev_disable(dev);
1246
1247 spin_lock_irqsave(ap->lock, flags);
1248
1249 dev->flags &= ~ATA_DFLAG_DETACH;
1250
1251 if (ata_scsi_offline_dev(dev)) {
1252 dev->flags |= ATA_DFLAG_DETACHED;
1253 ap->pflags |= ATA_PFLAG_SCSI_HOTPLUG;
1254 }
1255
1256
1257 ata_eh_clear_action(link, dev, &link->eh_info, ATA_EH_PERDEV_MASK);
1258 ata_eh_clear_action(link, dev, &link->eh_context.i, ATA_EH_PERDEV_MASK);
1259 ehc->saved_xfer_mode[dev->devno] = 0;
1260 ehc->saved_ncq_enabled &= ~(1 << dev->devno);
1261
1262 spin_unlock_irqrestore(ap->lock, flags);
1263 }
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278 void ata_eh_about_to_do(struct ata_link *link, struct ata_device *dev,
1279 unsigned int action)
1280 {
1281 struct ata_port *ap = link->ap;
1282 struct ata_eh_info *ehi = &link->eh_info;
1283 struct ata_eh_context *ehc = &link->eh_context;
1284 unsigned long flags;
1285
1286 trace_ata_eh_about_to_do(link, dev ? dev->devno : 0, action);
1287
1288 spin_lock_irqsave(ap->lock, flags);
1289
1290 ata_eh_clear_action(link, dev, ehi, action);
1291
1292
1293
1294
1295 if (!(ehc->i.flags & ATA_EHI_QUIET) && link != ap->slave_link)
1296 ap->pflags |= ATA_PFLAG_RECOVERED;
1297
1298 spin_unlock_irqrestore(ap->lock, flags);
1299 }
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313 void ata_eh_done(struct ata_link *link, struct ata_device *dev,
1314 unsigned int action)
1315 {
1316 struct ata_eh_context *ehc = &link->eh_context;
1317
1318 trace_ata_eh_done(link, dev ? dev->devno : 0, action);
1319
1320 ata_eh_clear_action(link, dev, &ehc->i, action);
1321 }
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337 static const char *ata_err_string(unsigned int err_mask)
1338 {
1339 if (err_mask & AC_ERR_HOST_BUS)
1340 return "host bus error";
1341 if (err_mask & AC_ERR_ATA_BUS)
1342 return "ATA bus error";
1343 if (err_mask & AC_ERR_TIMEOUT)
1344 return "timeout";
1345 if (err_mask & AC_ERR_HSM)
1346 return "HSM violation";
1347 if (err_mask & AC_ERR_SYSTEM)
1348 return "internal error";
1349 if (err_mask & AC_ERR_MEDIA)
1350 return "media error";
1351 if (err_mask & AC_ERR_INVALID)
1352 return "invalid argument";
1353 if (err_mask & AC_ERR_DEV)
1354 return "device error";
1355 if (err_mask & AC_ERR_NCQ)
1356 return "NCQ error";
1357 if (err_mask & AC_ERR_NODEV_HINT)
1358 return "Polling detection error";
1359 return "unknown error";
1360 }
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375 unsigned int atapi_eh_tur(struct ata_device *dev, u8 *r_sense_key)
1376 {
1377 u8 cdb[ATAPI_CDB_LEN] = { TEST_UNIT_READY, 0, 0, 0, 0, 0 };
1378 struct ata_taskfile tf;
1379 unsigned int err_mask;
1380
1381 ata_tf_init(dev, &tf);
1382
1383 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
1384 tf.command = ATA_CMD_PACKET;
1385 tf.protocol = ATAPI_PROT_NODATA;
1386
1387 err_mask = ata_exec_internal(dev, &tf, cdb, DMA_NONE, NULL, 0, 0);
1388 if (err_mask == AC_ERR_DEV)
1389 *r_sense_key = tf.error >> 4;
1390 return err_mask;
1391 }
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404 static void ata_eh_request_sense(struct ata_queued_cmd *qc,
1405 struct scsi_cmnd *cmd)
1406 {
1407 struct ata_device *dev = qc->dev;
1408 struct ata_taskfile tf;
1409 unsigned int err_mask;
1410
1411 if (qc->ap->pflags & ATA_PFLAG_FROZEN) {
1412 ata_dev_warn(dev, "sense data available but port frozen\n");
1413 return;
1414 }
1415
1416 if (!cmd || qc->flags & ATA_QCFLAG_SENSE_VALID)
1417 return;
1418
1419 if (!ata_id_sense_reporting_enabled(dev->id)) {
1420 ata_dev_warn(qc->dev, "sense data reporting disabled\n");
1421 return;
1422 }
1423
1424 ata_tf_init(dev, &tf);
1425 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
1426 tf.flags |= ATA_TFLAG_LBA | ATA_TFLAG_LBA48;
1427 tf.command = ATA_CMD_REQ_SENSE_DATA;
1428 tf.protocol = ATA_PROT_NODATA;
1429
1430 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
1431
1432 if (tf.status & ATA_SENSE) {
1433 ata_scsi_set_sense(dev, cmd, tf.lbah, tf.lbam, tf.lbal);
1434 qc->flags |= ATA_QCFLAG_SENSE_VALID;
1435 } else {
1436 ata_dev_warn(dev, "request sense failed stat %02x emask %x\n",
1437 tf.status, err_mask);
1438 }
1439 }
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456 unsigned int atapi_eh_request_sense(struct ata_device *dev,
1457 u8 *sense_buf, u8 dfl_sense_key)
1458 {
1459 u8 cdb[ATAPI_CDB_LEN] =
1460 { REQUEST_SENSE, 0, 0, 0, SCSI_SENSE_BUFFERSIZE, 0 };
1461 struct ata_port *ap = dev->link->ap;
1462 struct ata_taskfile tf;
1463
1464 memset(sense_buf, 0, SCSI_SENSE_BUFFERSIZE);
1465
1466
1467
1468
1469 sense_buf[0] = 0x70;
1470 sense_buf[2] = dfl_sense_key;
1471
1472
1473 ata_tf_init(dev, &tf);
1474
1475 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
1476 tf.command = ATA_CMD_PACKET;
1477
1478
1479 if (ap->flags & ATA_FLAG_PIO_DMA) {
1480 tf.protocol = ATAPI_PROT_DMA;
1481 tf.feature |= ATAPI_PKT_DMA;
1482 } else {
1483 tf.protocol = ATAPI_PROT_PIO;
1484 tf.lbam = SCSI_SENSE_BUFFERSIZE;
1485 tf.lbah = 0;
1486 }
1487
1488 return ata_exec_internal(dev, &tf, cdb, DMA_FROM_DEVICE,
1489 sense_buf, SCSI_SENSE_BUFFERSIZE, 0);
1490 }
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502 static void ata_eh_analyze_serror(struct ata_link *link)
1503 {
1504 struct ata_eh_context *ehc = &link->eh_context;
1505 u32 serror = ehc->i.serror;
1506 unsigned int err_mask = 0, action = 0;
1507 u32 hotplug_mask;
1508
1509 if (serror & (SERR_PERSISTENT | SERR_DATA)) {
1510 err_mask |= AC_ERR_ATA_BUS;
1511 action |= ATA_EH_RESET;
1512 }
1513 if (serror & SERR_PROTOCOL) {
1514 err_mask |= AC_ERR_HSM;
1515 action |= ATA_EH_RESET;
1516 }
1517 if (serror & SERR_INTERNAL) {
1518 err_mask |= AC_ERR_SYSTEM;
1519 action |= ATA_EH_RESET;
1520 }
1521
1522
1523
1524
1525
1526
1527 if (link->lpm_policy > ATA_LPM_MAX_POWER)
1528 hotplug_mask = 0;
1529 else if (!(link->flags & ATA_LFLAG_DISABLED) || ata_is_host_link(link))
1530 hotplug_mask = SERR_PHYRDY_CHG | SERR_DEV_XCHG;
1531 else
1532 hotplug_mask = SERR_PHYRDY_CHG;
1533
1534 if (serror & hotplug_mask)
1535 ata_ehi_hotplugged(&ehc->i);
1536
1537 ehc->i.err_mask |= err_mask;
1538 ehc->i.action |= action;
1539 }
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556 static unsigned int ata_eh_analyze_tf(struct ata_queued_cmd *qc,
1557 const struct ata_taskfile *tf)
1558 {
1559 unsigned int tmp, action = 0;
1560 u8 stat = tf->status, err = tf->error;
1561
1562 if ((stat & (ATA_BUSY | ATA_DRQ | ATA_DRDY)) != ATA_DRDY) {
1563 qc->err_mask |= AC_ERR_HSM;
1564 return ATA_EH_RESET;
1565 }
1566
1567 if (stat & (ATA_ERR | ATA_DF)) {
1568 qc->err_mask |= AC_ERR_DEV;
1569
1570
1571
1572
1573 if (stat & ATA_DF)
1574 stat &= ~ATA_SENSE;
1575 } else {
1576 return 0;
1577 }
1578
1579 switch (qc->dev->class) {
1580 case ATA_DEV_ZAC:
1581 if (stat & ATA_SENSE)
1582 ata_eh_request_sense(qc, qc->scsicmd);
1583 fallthrough;
1584 case ATA_DEV_ATA:
1585 if (err & ATA_ICRC)
1586 qc->err_mask |= AC_ERR_ATA_BUS;
1587 if (err & (ATA_UNC | ATA_AMNF))
1588 qc->err_mask |= AC_ERR_MEDIA;
1589 if (err & ATA_IDNF)
1590 qc->err_mask |= AC_ERR_INVALID;
1591 break;
1592
1593 case ATA_DEV_ATAPI:
1594 if (!(qc->ap->pflags & ATA_PFLAG_FROZEN)) {
1595 tmp = atapi_eh_request_sense(qc->dev,
1596 qc->scsicmd->sense_buffer,
1597 qc->result_tf.error >> 4);
1598 if (!tmp)
1599 qc->flags |= ATA_QCFLAG_SENSE_VALID;
1600 else
1601 qc->err_mask |= tmp;
1602 }
1603 }
1604
1605 if (qc->flags & ATA_QCFLAG_SENSE_VALID) {
1606 enum scsi_disposition ret = scsi_check_sense(qc->scsicmd);
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616 if (ret == NEEDS_RETRY || ret == ADD_TO_MLQUEUE) {
1617 qc->flags |= ATA_QCFLAG_RETRY;
1618 qc->err_mask |= AC_ERR_OTHER;
1619 } else if (ret != SUCCESS) {
1620 qc->err_mask |= AC_ERR_HSM;
1621 }
1622 }
1623 if (qc->err_mask & (AC_ERR_HSM | AC_ERR_TIMEOUT | AC_ERR_ATA_BUS))
1624 action |= ATA_EH_RESET;
1625
1626 return action;
1627 }
1628
1629 static int ata_eh_categorize_error(unsigned int eflags, unsigned int err_mask,
1630 int *xfer_ok)
1631 {
1632 int base = 0;
1633
1634 if (!(eflags & ATA_EFLAG_DUBIOUS_XFER))
1635 *xfer_ok = 1;
1636
1637 if (!*xfer_ok)
1638 base = ATA_ECAT_DUBIOUS_NONE;
1639
1640 if (err_mask & AC_ERR_ATA_BUS)
1641 return base + ATA_ECAT_ATA_BUS;
1642
1643 if (err_mask & AC_ERR_TIMEOUT)
1644 return base + ATA_ECAT_TOUT_HSM;
1645
1646 if (eflags & ATA_EFLAG_IS_IO) {
1647 if (err_mask & AC_ERR_HSM)
1648 return base + ATA_ECAT_TOUT_HSM;
1649 if ((err_mask &
1650 (AC_ERR_DEV|AC_ERR_MEDIA|AC_ERR_INVALID)) == AC_ERR_DEV)
1651 return base + ATA_ECAT_UNK_DEV;
1652 }
1653
1654 return 0;
1655 }
1656
1657 struct speed_down_verdict_arg {
1658 u64 since;
1659 int xfer_ok;
1660 int nr_errors[ATA_ECAT_NR];
1661 };
1662
1663 static int speed_down_verdict_cb(struct ata_ering_entry *ent, void *void_arg)
1664 {
1665 struct speed_down_verdict_arg *arg = void_arg;
1666 int cat;
1667
1668 if ((ent->eflags & ATA_EFLAG_OLD_ER) || (ent->timestamp < arg->since))
1669 return -1;
1670
1671 cat = ata_eh_categorize_error(ent->eflags, ent->err_mask,
1672 &arg->xfer_ok);
1673 arg->nr_errors[cat]++;
1674
1675 return 0;
1676 }
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735 static unsigned int ata_eh_speed_down_verdict(struct ata_device *dev)
1736 {
1737 const u64 j5mins = 5LLU * 60 * HZ, j10mins = 10LLU * 60 * HZ;
1738 u64 j64 = get_jiffies_64();
1739 struct speed_down_verdict_arg arg;
1740 unsigned int verdict = 0;
1741
1742
1743 memset(&arg, 0, sizeof(arg));
1744 arg.since = j64 - min(j64, j5mins);
1745 ata_ering_map(&dev->ering, speed_down_verdict_cb, &arg);
1746
1747 if (arg.nr_errors[ATA_ECAT_DUBIOUS_ATA_BUS] +
1748 arg.nr_errors[ATA_ECAT_DUBIOUS_TOUT_HSM] > 1)
1749 verdict |= ATA_EH_SPDN_SPEED_DOWN |
1750 ATA_EH_SPDN_FALLBACK_TO_PIO | ATA_EH_SPDN_KEEP_ERRORS;
1751
1752 if (arg.nr_errors[ATA_ECAT_DUBIOUS_TOUT_HSM] +
1753 arg.nr_errors[ATA_ECAT_DUBIOUS_UNK_DEV] > 1)
1754 verdict |= ATA_EH_SPDN_NCQ_OFF | ATA_EH_SPDN_KEEP_ERRORS;
1755
1756 if (arg.nr_errors[ATA_ECAT_ATA_BUS] +
1757 arg.nr_errors[ATA_ECAT_TOUT_HSM] +
1758 arg.nr_errors[ATA_ECAT_UNK_DEV] > 6)
1759 verdict |= ATA_EH_SPDN_FALLBACK_TO_PIO;
1760
1761
1762 memset(&arg, 0, sizeof(arg));
1763 arg.since = j64 - min(j64, j10mins);
1764 ata_ering_map(&dev->ering, speed_down_verdict_cb, &arg);
1765
1766 if (arg.nr_errors[ATA_ECAT_TOUT_HSM] +
1767 arg.nr_errors[ATA_ECAT_UNK_DEV] > 3)
1768 verdict |= ATA_EH_SPDN_NCQ_OFF;
1769
1770 if (arg.nr_errors[ATA_ECAT_ATA_BUS] +
1771 arg.nr_errors[ATA_ECAT_TOUT_HSM] > 3 ||
1772 arg.nr_errors[ATA_ECAT_UNK_DEV] > 6)
1773 verdict |= ATA_EH_SPDN_SPEED_DOWN;
1774
1775 return verdict;
1776 }
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795 static unsigned int ata_eh_speed_down(struct ata_device *dev,
1796 unsigned int eflags, unsigned int err_mask)
1797 {
1798 struct ata_link *link = ata_dev_phys_link(dev);
1799 int xfer_ok = 0;
1800 unsigned int verdict;
1801 unsigned int action = 0;
1802
1803
1804 if (ata_eh_categorize_error(eflags, err_mask, &xfer_ok) == 0)
1805 return 0;
1806
1807
1808 ata_ering_record(&dev->ering, eflags, err_mask);
1809 verdict = ata_eh_speed_down_verdict(dev);
1810
1811
1812 if ((verdict & ATA_EH_SPDN_NCQ_OFF) &&
1813 (dev->flags & (ATA_DFLAG_PIO | ATA_DFLAG_NCQ |
1814 ATA_DFLAG_NCQ_OFF)) == ATA_DFLAG_NCQ) {
1815 dev->flags |= ATA_DFLAG_NCQ_OFF;
1816 ata_dev_warn(dev, "NCQ disabled due to excessive errors\n");
1817 goto done;
1818 }
1819
1820
1821 if (verdict & ATA_EH_SPDN_SPEED_DOWN) {
1822
1823 if (sata_down_spd_limit(link, 0) == 0) {
1824 action |= ATA_EH_RESET;
1825 goto done;
1826 }
1827
1828
1829 if (dev->spdn_cnt < 2) {
1830 static const int dma_dnxfer_sel[] =
1831 { ATA_DNXFER_DMA, ATA_DNXFER_40C };
1832 static const int pio_dnxfer_sel[] =
1833 { ATA_DNXFER_PIO, ATA_DNXFER_FORCE_PIO0 };
1834 int sel;
1835
1836 if (dev->xfer_shift != ATA_SHIFT_PIO)
1837 sel = dma_dnxfer_sel[dev->spdn_cnt];
1838 else
1839 sel = pio_dnxfer_sel[dev->spdn_cnt];
1840
1841 dev->spdn_cnt++;
1842
1843 if (ata_down_xfermask_limit(dev, sel) == 0) {
1844 action |= ATA_EH_RESET;
1845 goto done;
1846 }
1847 }
1848 }
1849
1850
1851
1852
1853 if ((verdict & ATA_EH_SPDN_FALLBACK_TO_PIO) && (dev->spdn_cnt >= 2) &&
1854 (link->ap->cbl != ATA_CBL_SATA || dev->class == ATA_DEV_ATAPI) &&
1855 (dev->xfer_shift != ATA_SHIFT_PIO)) {
1856 if (ata_down_xfermask_limit(dev, ATA_DNXFER_FORCE_PIO) == 0) {
1857 dev->spdn_cnt = 0;
1858 action |= ATA_EH_RESET;
1859 goto done;
1860 }
1861 }
1862
1863 return 0;
1864 done:
1865
1866 if (!(verdict & ATA_EH_SPDN_KEEP_ERRORS))
1867 ata_ering_clear(&dev->ering);
1868 return action;
1869 }
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880 static inline int ata_eh_worth_retry(struct ata_queued_cmd *qc)
1881 {
1882 if (qc->err_mask & AC_ERR_MEDIA)
1883 return 0;
1884 if (qc->flags & ATA_QCFLAG_IO)
1885 return 1;
1886 if (qc->err_mask & AC_ERR_INVALID)
1887 return 0;
1888 return qc->err_mask != AC_ERR_DEV;
1889 }
1890
1891
1892
1893
1894
1895
1896
1897
1898 static inline bool ata_eh_quiet(struct ata_queued_cmd *qc)
1899 {
1900 if (qc->scsicmd && scsi_cmd_to_rq(qc->scsicmd)->rq_flags & RQF_QUIET)
1901 qc->flags |= ATA_QCFLAG_QUIET;
1902 return qc->flags & ATA_QCFLAG_QUIET;
1903 }
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916 static void ata_eh_link_autopsy(struct ata_link *link)
1917 {
1918 struct ata_port *ap = link->ap;
1919 struct ata_eh_context *ehc = &link->eh_context;
1920 struct ata_queued_cmd *qc;
1921 struct ata_device *dev;
1922 unsigned int all_err_mask = 0, eflags = 0;
1923 int tag, nr_failed = 0, nr_quiet = 0;
1924 u32 serror;
1925 int rc;
1926
1927 if (ehc->i.flags & ATA_EHI_NO_AUTOPSY)
1928 return;
1929
1930
1931 rc = sata_scr_read(link, SCR_ERROR, &serror);
1932 if (rc == 0) {
1933 ehc->i.serror |= serror;
1934 ata_eh_analyze_serror(link);
1935 } else if (rc != -EOPNOTSUPP) {
1936
1937 ehc->i.probe_mask |= ATA_ALL_DEVICES;
1938 ehc->i.action |= ATA_EH_RESET;
1939 ehc->i.err_mask |= AC_ERR_OTHER;
1940 }
1941
1942
1943 ata_eh_analyze_ncq_error(link);
1944
1945
1946 if (ehc->i.err_mask & ~AC_ERR_OTHER)
1947 ehc->i.err_mask &= ~AC_ERR_OTHER;
1948
1949 all_err_mask |= ehc->i.err_mask;
1950
1951 ata_qc_for_each_raw(ap, qc, tag) {
1952 if (!(qc->flags & ATA_QCFLAG_FAILED) ||
1953 ata_dev_phys_link(qc->dev) != link)
1954 continue;
1955
1956
1957 qc->err_mask |= ehc->i.err_mask;
1958
1959
1960 ehc->i.action |= ata_eh_analyze_tf(qc, &qc->result_tf);
1961
1962
1963 if (qc->err_mask & AC_ERR_ATA_BUS)
1964 qc->err_mask &= ~(AC_ERR_DEV | AC_ERR_MEDIA |
1965 AC_ERR_INVALID);
1966
1967
1968 if (qc->err_mask & ~AC_ERR_OTHER)
1969 qc->err_mask &= ~AC_ERR_OTHER;
1970
1971
1972
1973
1974
1975
1976
1977
1978 if (qc->flags & ATA_QCFLAG_SENSE_VALID)
1979 qc->err_mask &= ~(AC_ERR_DEV | AC_ERR_OTHER);
1980 else if (ata_eh_worth_retry(qc))
1981 qc->flags |= ATA_QCFLAG_RETRY;
1982
1983
1984 ehc->i.dev = qc->dev;
1985 all_err_mask |= qc->err_mask;
1986 if (qc->flags & ATA_QCFLAG_IO)
1987 eflags |= ATA_EFLAG_IS_IO;
1988 trace_ata_eh_link_autopsy_qc(qc);
1989
1990
1991 if (ata_eh_quiet(qc))
1992 nr_quiet++;
1993 nr_failed++;
1994 }
1995
1996
1997 if (nr_quiet == nr_failed)
1998 ehc->i.flags |= ATA_EHI_QUIET;
1999
2000
2001 if (ap->pflags & ATA_PFLAG_FROZEN ||
2002 all_err_mask & (AC_ERR_HSM | AC_ERR_TIMEOUT))
2003 ehc->i.action |= ATA_EH_RESET;
2004 else if (((eflags & ATA_EFLAG_IS_IO) && all_err_mask) ||
2005 (!(eflags & ATA_EFLAG_IS_IO) && (all_err_mask & ~AC_ERR_DEV)))
2006 ehc->i.action |= ATA_EH_REVALIDATE;
2007
2008
2009
2010
2011 if (ehc->i.dev) {
2012 ehc->i.dev_action[ehc->i.dev->devno] |=
2013 ehc->i.action & ATA_EH_PERDEV_MASK;
2014 ehc->i.action &= ~ATA_EH_PERDEV_MASK;
2015 }
2016
2017
2018 if ((all_err_mask & AC_ERR_TIMEOUT) && !ata_is_host_link(link))
2019 ap->link.eh_context.i.err_mask |= AC_ERR_TIMEOUT;
2020
2021
2022 dev = ehc->i.dev;
2023 if (!dev && ((ata_link_max_devices(link) == 1 &&
2024 ata_dev_enabled(link->device))))
2025 dev = link->device;
2026
2027 if (dev) {
2028 if (dev->flags & ATA_DFLAG_DUBIOUS_XFER)
2029 eflags |= ATA_EFLAG_DUBIOUS_XFER;
2030 ehc->i.action |= ata_eh_speed_down(dev, eflags, all_err_mask);
2031 trace_ata_eh_link_autopsy(dev, ehc->i.action, all_err_mask);
2032 }
2033 }
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045 void ata_eh_autopsy(struct ata_port *ap)
2046 {
2047 struct ata_link *link;
2048
2049 ata_for_each_link(link, ap, EDGE)
2050 ata_eh_link_autopsy(link);
2051
2052
2053
2054
2055
2056 if (ap->slave_link) {
2057 struct ata_eh_context *mehc = &ap->link.eh_context;
2058 struct ata_eh_context *sehc = &ap->slave_link->eh_context;
2059
2060
2061 sehc->i.flags |= mehc->i.flags & ATA_EHI_TO_SLAVE_MASK;
2062
2063
2064 ata_eh_link_autopsy(ap->slave_link);
2065
2066
2067 ata_eh_about_to_do(ap->slave_link, NULL, ATA_EH_ALL_ACTIONS);
2068 mehc->i.action |= sehc->i.action;
2069 mehc->i.dev_action[1] |= sehc->i.dev_action[1];
2070 mehc->i.flags |= sehc->i.flags;
2071 ata_eh_done(ap->slave_link, NULL, ATA_EH_ALL_ACTIONS);
2072 }
2073
2074
2075
2076
2077 if (sata_pmp_attached(ap))
2078 ata_eh_link_autopsy(&ap->link);
2079 }
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090 const char *ata_get_cmd_name(u8 command)
2091 {
2092 #ifdef CONFIG_ATA_VERBOSE_ERROR
2093 static const struct
2094 {
2095 u8 command;
2096 const char *text;
2097 } cmd_descr[] = {
2098 { ATA_CMD_DEV_RESET, "DEVICE RESET" },
2099 { ATA_CMD_CHK_POWER, "CHECK POWER MODE" },
2100 { ATA_CMD_STANDBY, "STANDBY" },
2101 { ATA_CMD_IDLE, "IDLE" },
2102 { ATA_CMD_EDD, "EXECUTE DEVICE DIAGNOSTIC" },
2103 { ATA_CMD_DOWNLOAD_MICRO, "DOWNLOAD MICROCODE" },
2104 { ATA_CMD_DOWNLOAD_MICRO_DMA, "DOWNLOAD MICROCODE DMA" },
2105 { ATA_CMD_NOP, "NOP" },
2106 { ATA_CMD_FLUSH, "FLUSH CACHE" },
2107 { ATA_CMD_FLUSH_EXT, "FLUSH CACHE EXT" },
2108 { ATA_CMD_ID_ATA, "IDENTIFY DEVICE" },
2109 { ATA_CMD_ID_ATAPI, "IDENTIFY PACKET DEVICE" },
2110 { ATA_CMD_SERVICE, "SERVICE" },
2111 { ATA_CMD_READ, "READ DMA" },
2112 { ATA_CMD_READ_EXT, "READ DMA EXT" },
2113 { ATA_CMD_READ_QUEUED, "READ DMA QUEUED" },
2114 { ATA_CMD_READ_STREAM_EXT, "READ STREAM EXT" },
2115 { ATA_CMD_READ_STREAM_DMA_EXT, "READ STREAM DMA EXT" },
2116 { ATA_CMD_WRITE, "WRITE DMA" },
2117 { ATA_CMD_WRITE_EXT, "WRITE DMA EXT" },
2118 { ATA_CMD_WRITE_QUEUED, "WRITE DMA QUEUED EXT" },
2119 { ATA_CMD_WRITE_STREAM_EXT, "WRITE STREAM EXT" },
2120 { ATA_CMD_WRITE_STREAM_DMA_EXT, "WRITE STREAM DMA EXT" },
2121 { ATA_CMD_WRITE_FUA_EXT, "WRITE DMA FUA EXT" },
2122 { ATA_CMD_WRITE_QUEUED_FUA_EXT, "WRITE DMA QUEUED FUA EXT" },
2123 { ATA_CMD_FPDMA_READ, "READ FPDMA QUEUED" },
2124 { ATA_CMD_FPDMA_WRITE, "WRITE FPDMA QUEUED" },
2125 { ATA_CMD_NCQ_NON_DATA, "NCQ NON-DATA" },
2126 { ATA_CMD_FPDMA_SEND, "SEND FPDMA QUEUED" },
2127 { ATA_CMD_FPDMA_RECV, "RECEIVE FPDMA QUEUED" },
2128 { ATA_CMD_PIO_READ, "READ SECTOR(S)" },
2129 { ATA_CMD_PIO_READ_EXT, "READ SECTOR(S) EXT" },
2130 { ATA_CMD_PIO_WRITE, "WRITE SECTOR(S)" },
2131 { ATA_CMD_PIO_WRITE_EXT, "WRITE SECTOR(S) EXT" },
2132 { ATA_CMD_READ_MULTI, "READ MULTIPLE" },
2133 { ATA_CMD_READ_MULTI_EXT, "READ MULTIPLE EXT" },
2134 { ATA_CMD_WRITE_MULTI, "WRITE MULTIPLE" },
2135 { ATA_CMD_WRITE_MULTI_EXT, "WRITE MULTIPLE EXT" },
2136 { ATA_CMD_WRITE_MULTI_FUA_EXT, "WRITE MULTIPLE FUA EXT" },
2137 { ATA_CMD_SET_FEATURES, "SET FEATURES" },
2138 { ATA_CMD_SET_MULTI, "SET MULTIPLE MODE" },
2139 { ATA_CMD_VERIFY, "READ VERIFY SECTOR(S)" },
2140 { ATA_CMD_VERIFY_EXT, "READ VERIFY SECTOR(S) EXT" },
2141 { ATA_CMD_WRITE_UNCORR_EXT, "WRITE UNCORRECTABLE EXT" },
2142 { ATA_CMD_STANDBYNOW1, "STANDBY IMMEDIATE" },
2143 { ATA_CMD_IDLEIMMEDIATE, "IDLE IMMEDIATE" },
2144 { ATA_CMD_SLEEP, "SLEEP" },
2145 { ATA_CMD_INIT_DEV_PARAMS, "INITIALIZE DEVICE PARAMETERS" },
2146 { ATA_CMD_READ_NATIVE_MAX, "READ NATIVE MAX ADDRESS" },
2147 { ATA_CMD_READ_NATIVE_MAX_EXT, "READ NATIVE MAX ADDRESS EXT" },
2148 { ATA_CMD_SET_MAX, "SET MAX ADDRESS" },
2149 { ATA_CMD_SET_MAX_EXT, "SET MAX ADDRESS EXT" },
2150 { ATA_CMD_READ_LOG_EXT, "READ LOG EXT" },
2151 { ATA_CMD_WRITE_LOG_EXT, "WRITE LOG EXT" },
2152 { ATA_CMD_READ_LOG_DMA_EXT, "READ LOG DMA EXT" },
2153 { ATA_CMD_WRITE_LOG_DMA_EXT, "WRITE LOG DMA EXT" },
2154 { ATA_CMD_TRUSTED_NONDATA, "TRUSTED NON-DATA" },
2155 { ATA_CMD_TRUSTED_RCV, "TRUSTED RECEIVE" },
2156 { ATA_CMD_TRUSTED_RCV_DMA, "TRUSTED RECEIVE DMA" },
2157 { ATA_CMD_TRUSTED_SND, "TRUSTED SEND" },
2158 { ATA_CMD_TRUSTED_SND_DMA, "TRUSTED SEND DMA" },
2159 { ATA_CMD_PMP_READ, "READ BUFFER" },
2160 { ATA_CMD_PMP_READ_DMA, "READ BUFFER DMA" },
2161 { ATA_CMD_PMP_WRITE, "WRITE BUFFER" },
2162 { ATA_CMD_PMP_WRITE_DMA, "WRITE BUFFER DMA" },
2163 { ATA_CMD_CONF_OVERLAY, "DEVICE CONFIGURATION OVERLAY" },
2164 { ATA_CMD_SEC_SET_PASS, "SECURITY SET PASSWORD" },
2165 { ATA_CMD_SEC_UNLOCK, "SECURITY UNLOCK" },
2166 { ATA_CMD_SEC_ERASE_PREP, "SECURITY ERASE PREPARE" },
2167 { ATA_CMD_SEC_ERASE_UNIT, "SECURITY ERASE UNIT" },
2168 { ATA_CMD_SEC_FREEZE_LOCK, "SECURITY FREEZE LOCK" },
2169 { ATA_CMD_SEC_DISABLE_PASS, "SECURITY DISABLE PASSWORD" },
2170 { ATA_CMD_CONFIG_STREAM, "CONFIGURE STREAM" },
2171 { ATA_CMD_SMART, "SMART" },
2172 { ATA_CMD_MEDIA_LOCK, "DOOR LOCK" },
2173 { ATA_CMD_MEDIA_UNLOCK, "DOOR UNLOCK" },
2174 { ATA_CMD_DSM, "DATA SET MANAGEMENT" },
2175 { ATA_CMD_CHK_MED_CRD_TYP, "CHECK MEDIA CARD TYPE" },
2176 { ATA_CMD_CFA_REQ_EXT_ERR, "CFA REQUEST EXTENDED ERROR" },
2177 { ATA_CMD_CFA_WRITE_NE, "CFA WRITE SECTORS WITHOUT ERASE" },
2178 { ATA_CMD_CFA_TRANS_SECT, "CFA TRANSLATE SECTOR" },
2179 { ATA_CMD_CFA_ERASE, "CFA ERASE SECTORS" },
2180 { ATA_CMD_CFA_WRITE_MULT_NE, "CFA WRITE MULTIPLE WITHOUT ERASE" },
2181 { ATA_CMD_REQ_SENSE_DATA, "REQUEST SENSE DATA EXT" },
2182 { ATA_CMD_SANITIZE_DEVICE, "SANITIZE DEVICE" },
2183 { ATA_CMD_ZAC_MGMT_IN, "ZAC MANAGEMENT IN" },
2184 { ATA_CMD_ZAC_MGMT_OUT, "ZAC MANAGEMENT OUT" },
2185 { ATA_CMD_READ_LONG, "READ LONG (with retries)" },
2186 { ATA_CMD_READ_LONG_ONCE, "READ LONG (without retries)" },
2187 { ATA_CMD_WRITE_LONG, "WRITE LONG (with retries)" },
2188 { ATA_CMD_WRITE_LONG_ONCE, "WRITE LONG (without retries)" },
2189 { ATA_CMD_RESTORE, "RECALIBRATE" },
2190 { 0, NULL }
2191 };
2192
2193 unsigned int i;
2194 for (i = 0; cmd_descr[i].text; i++)
2195 if (cmd_descr[i].command == command)
2196 return cmd_descr[i].text;
2197 #endif
2198
2199 return "unknown";
2200 }
2201 EXPORT_SYMBOL_GPL(ata_get_cmd_name);
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212 static void ata_eh_link_report(struct ata_link *link)
2213 {
2214 struct ata_port *ap = link->ap;
2215 struct ata_eh_context *ehc = &link->eh_context;
2216 struct ata_queued_cmd *qc;
2217 const char *frozen, *desc;
2218 char tries_buf[6] = "";
2219 int tag, nr_failed = 0;
2220
2221 if (ehc->i.flags & ATA_EHI_QUIET)
2222 return;
2223
2224 desc = NULL;
2225 if (ehc->i.desc[0] != '\0')
2226 desc = ehc->i.desc;
2227
2228 ata_qc_for_each_raw(ap, qc, tag) {
2229 if (!(qc->flags & ATA_QCFLAG_FAILED) ||
2230 ata_dev_phys_link(qc->dev) != link ||
2231 ((qc->flags & ATA_QCFLAG_QUIET) &&
2232 qc->err_mask == AC_ERR_DEV))
2233 continue;
2234 if (qc->flags & ATA_QCFLAG_SENSE_VALID && !qc->err_mask)
2235 continue;
2236
2237 nr_failed++;
2238 }
2239
2240 if (!nr_failed && !ehc->i.err_mask)
2241 return;
2242
2243 frozen = "";
2244 if (ap->pflags & ATA_PFLAG_FROZEN)
2245 frozen = " frozen";
2246
2247 if (ap->eh_tries < ATA_EH_MAX_TRIES)
2248 snprintf(tries_buf, sizeof(tries_buf), " t%d",
2249 ap->eh_tries);
2250
2251 if (ehc->i.dev) {
2252 ata_dev_err(ehc->i.dev, "exception Emask 0x%x "
2253 "SAct 0x%x SErr 0x%x action 0x%x%s%s\n",
2254 ehc->i.err_mask, link->sactive, ehc->i.serror,
2255 ehc->i.action, frozen, tries_buf);
2256 if (desc)
2257 ata_dev_err(ehc->i.dev, "%s\n", desc);
2258 } else {
2259 ata_link_err(link, "exception Emask 0x%x "
2260 "SAct 0x%x SErr 0x%x action 0x%x%s%s\n",
2261 ehc->i.err_mask, link->sactive, ehc->i.serror,
2262 ehc->i.action, frozen, tries_buf);
2263 if (desc)
2264 ata_link_err(link, "%s\n", desc);
2265 }
2266
2267 #ifdef CONFIG_ATA_VERBOSE_ERROR
2268 if (ehc->i.serror)
2269 ata_link_err(link,
2270 "SError: { %s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s}\n",
2271 ehc->i.serror & SERR_DATA_RECOVERED ? "RecovData " : "",
2272 ehc->i.serror & SERR_COMM_RECOVERED ? "RecovComm " : "",
2273 ehc->i.serror & SERR_DATA ? "UnrecovData " : "",
2274 ehc->i.serror & SERR_PERSISTENT ? "Persist " : "",
2275 ehc->i.serror & SERR_PROTOCOL ? "Proto " : "",
2276 ehc->i.serror & SERR_INTERNAL ? "HostInt " : "",
2277 ehc->i.serror & SERR_PHYRDY_CHG ? "PHYRdyChg " : "",
2278 ehc->i.serror & SERR_PHY_INT_ERR ? "PHYInt " : "",
2279 ehc->i.serror & SERR_COMM_WAKE ? "CommWake " : "",
2280 ehc->i.serror & SERR_10B_8B_ERR ? "10B8B " : "",
2281 ehc->i.serror & SERR_DISPARITY ? "Dispar " : "",
2282 ehc->i.serror & SERR_CRC ? "BadCRC " : "",
2283 ehc->i.serror & SERR_HANDSHAKE ? "Handshk " : "",
2284 ehc->i.serror & SERR_LINK_SEQ_ERR ? "LinkSeq " : "",
2285 ehc->i.serror & SERR_TRANS_ST_ERROR ? "TrStaTrns " : "",
2286 ehc->i.serror & SERR_UNRECOG_FIS ? "UnrecFIS " : "",
2287 ehc->i.serror & SERR_DEV_XCHG ? "DevExch " : "");
2288 #endif
2289
2290 ata_qc_for_each_raw(ap, qc, tag) {
2291 struct ata_taskfile *cmd = &qc->tf, *res = &qc->result_tf;
2292 char data_buf[20] = "";
2293 char cdb_buf[70] = "";
2294
2295 if (!(qc->flags & ATA_QCFLAG_FAILED) ||
2296 ata_dev_phys_link(qc->dev) != link || !qc->err_mask)
2297 continue;
2298
2299 if (qc->dma_dir != DMA_NONE) {
2300 static const char *dma_str[] = {
2301 [DMA_BIDIRECTIONAL] = "bidi",
2302 [DMA_TO_DEVICE] = "out",
2303 [DMA_FROM_DEVICE] = "in",
2304 };
2305 const char *prot_str = NULL;
2306
2307 switch (qc->tf.protocol) {
2308 case ATA_PROT_UNKNOWN:
2309 prot_str = "unknown";
2310 break;
2311 case ATA_PROT_NODATA:
2312 prot_str = "nodata";
2313 break;
2314 case ATA_PROT_PIO:
2315 prot_str = "pio";
2316 break;
2317 case ATA_PROT_DMA:
2318 prot_str = "dma";
2319 break;
2320 case ATA_PROT_NCQ:
2321 prot_str = "ncq dma";
2322 break;
2323 case ATA_PROT_NCQ_NODATA:
2324 prot_str = "ncq nodata";
2325 break;
2326 case ATAPI_PROT_NODATA:
2327 prot_str = "nodata";
2328 break;
2329 case ATAPI_PROT_PIO:
2330 prot_str = "pio";
2331 break;
2332 case ATAPI_PROT_DMA:
2333 prot_str = "dma";
2334 break;
2335 }
2336 snprintf(data_buf, sizeof(data_buf), " %s %u %s",
2337 prot_str, qc->nbytes, dma_str[qc->dma_dir]);
2338 }
2339
2340 if (ata_is_atapi(qc->tf.protocol)) {
2341 const u8 *cdb = qc->cdb;
2342 size_t cdb_len = qc->dev->cdb_len;
2343
2344 if (qc->scsicmd) {
2345 cdb = qc->scsicmd->cmnd;
2346 cdb_len = qc->scsicmd->cmd_len;
2347 }
2348 __scsi_format_command(cdb_buf, sizeof(cdb_buf),
2349 cdb, cdb_len);
2350 } else
2351 ata_dev_err(qc->dev, "failed command: %s\n",
2352 ata_get_cmd_name(cmd->command));
2353
2354 ata_dev_err(qc->dev,
2355 "cmd %02x/%02x:%02x:%02x:%02x:%02x/%02x:%02x:%02x:%02x:%02x/%02x "
2356 "tag %d%s\n %s"
2357 "res %02x/%02x:%02x:%02x:%02x:%02x/%02x:%02x:%02x:%02x:%02x/%02x "
2358 "Emask 0x%x (%s)%s\n",
2359 cmd->command, cmd->feature, cmd->nsect,
2360 cmd->lbal, cmd->lbam, cmd->lbah,
2361 cmd->hob_feature, cmd->hob_nsect,
2362 cmd->hob_lbal, cmd->hob_lbam, cmd->hob_lbah,
2363 cmd->device, qc->tag, data_buf, cdb_buf,
2364 res->status, res->error, res->nsect,
2365 res->lbal, res->lbam, res->lbah,
2366 res->hob_feature, res->hob_nsect,
2367 res->hob_lbal, res->hob_lbam, res->hob_lbah,
2368 res->device, qc->err_mask, ata_err_string(qc->err_mask),
2369 qc->err_mask & AC_ERR_NCQ ? " <F>" : "");
2370
2371 #ifdef CONFIG_ATA_VERBOSE_ERROR
2372 if (res->status & (ATA_BUSY | ATA_DRDY | ATA_DF | ATA_DRQ |
2373 ATA_SENSE | ATA_ERR)) {
2374 if (res->status & ATA_BUSY)
2375 ata_dev_err(qc->dev, "status: { Busy }\n");
2376 else
2377 ata_dev_err(qc->dev, "status: { %s%s%s%s%s}\n",
2378 res->status & ATA_DRDY ? "DRDY " : "",
2379 res->status & ATA_DF ? "DF " : "",
2380 res->status & ATA_DRQ ? "DRQ " : "",
2381 res->status & ATA_SENSE ? "SENSE " : "",
2382 res->status & ATA_ERR ? "ERR " : "");
2383 }
2384
2385 if (cmd->command != ATA_CMD_PACKET &&
2386 (res->error & (ATA_ICRC | ATA_UNC | ATA_AMNF | ATA_IDNF |
2387 ATA_ABORTED)))
2388 ata_dev_err(qc->dev, "error: { %s%s%s%s%s}\n",
2389 res->error & ATA_ICRC ? "ICRC " : "",
2390 res->error & ATA_UNC ? "UNC " : "",
2391 res->error & ATA_AMNF ? "AMNF " : "",
2392 res->error & ATA_IDNF ? "IDNF " : "",
2393 res->error & ATA_ABORTED ? "ABRT " : "");
2394 #endif
2395 }
2396 }
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407 void ata_eh_report(struct ata_port *ap)
2408 {
2409 struct ata_link *link;
2410
2411 ata_for_each_link(link, ap, HOST_FIRST)
2412 ata_eh_link_report(link);
2413 }
2414
2415 static int ata_do_reset(struct ata_link *link, ata_reset_fn_t reset,
2416 unsigned int *classes, unsigned long deadline,
2417 bool clear_classes)
2418 {
2419 struct ata_device *dev;
2420
2421 if (clear_classes)
2422 ata_for_each_dev(dev, link, ALL)
2423 classes[dev->devno] = ATA_DEV_UNKNOWN;
2424
2425 return reset(link, classes, deadline);
2426 }
2427
2428 static int ata_eh_followup_srst_needed(struct ata_link *link, int rc)
2429 {
2430 if ((link->flags & ATA_LFLAG_NO_SRST) || ata_link_offline(link))
2431 return 0;
2432 if (rc == -EAGAIN)
2433 return 1;
2434 if (sata_pmp_supported(link->ap) && ata_is_host_link(link))
2435 return 1;
2436 return 0;
2437 }
2438
2439 int ata_eh_reset(struct ata_link *link, int classify,
2440 ata_prereset_fn_t prereset, ata_reset_fn_t softreset,
2441 ata_reset_fn_t hardreset, ata_postreset_fn_t postreset)
2442 {
2443 struct ata_port *ap = link->ap;
2444 struct ata_link *slave = ap->slave_link;
2445 struct ata_eh_context *ehc = &link->eh_context;
2446 struct ata_eh_context *sehc = slave ? &slave->eh_context : NULL;
2447 unsigned int *classes = ehc->classes;
2448 unsigned int lflags = link->flags;
2449 int verbose = !(ehc->i.flags & ATA_EHI_QUIET);
2450 int max_tries = 0, try = 0;
2451 struct ata_link *failed_link;
2452 struct ata_device *dev;
2453 unsigned long deadline, now;
2454 ata_reset_fn_t reset;
2455 unsigned long flags;
2456 u32 sstatus;
2457 int nr_unknown, rc;
2458
2459
2460
2461
2462 while (ata_eh_reset_timeouts[max_tries] != ULONG_MAX)
2463 max_tries++;
2464 if (link->flags & ATA_LFLAG_RST_ONCE)
2465 max_tries = 1;
2466 if (link->flags & ATA_LFLAG_NO_HRST)
2467 hardreset = NULL;
2468 if (link->flags & ATA_LFLAG_NO_SRST)
2469 softreset = NULL;
2470
2471
2472 if (ehc->i.flags & ATA_EHI_DID_RESET) {
2473 now = jiffies;
2474 WARN_ON(time_after(ehc->last_reset, now));
2475 deadline = ata_deadline(ehc->last_reset,
2476 ATA_EH_RESET_COOL_DOWN);
2477 if (time_before(now, deadline))
2478 schedule_timeout_uninterruptible(deadline - now);
2479 }
2480
2481 spin_lock_irqsave(ap->lock, flags);
2482 ap->pflags |= ATA_PFLAG_RESETTING;
2483 spin_unlock_irqrestore(ap->lock, flags);
2484
2485 ata_eh_about_to_do(link, NULL, ATA_EH_RESET);
2486
2487 ata_for_each_dev(dev, link, ALL) {
2488
2489
2490
2491
2492
2493
2494
2495 dev->pio_mode = XFER_PIO_0;
2496 dev->dma_mode = 0xff;
2497
2498
2499
2500
2501
2502
2503 if (ap->ops->set_piomode)
2504 ap->ops->set_piomode(ap, dev);
2505 }
2506
2507
2508 reset = NULL;
2509 ehc->i.action &= ~ATA_EH_RESET;
2510 if (hardreset) {
2511 reset = hardreset;
2512 ehc->i.action |= ATA_EH_HARDRESET;
2513 } else if (softreset) {
2514 reset = softreset;
2515 ehc->i.action |= ATA_EH_SOFTRESET;
2516 }
2517
2518 if (prereset) {
2519 unsigned long deadline = ata_deadline(jiffies,
2520 ATA_EH_PRERESET_TIMEOUT);
2521
2522 if (slave) {
2523 sehc->i.action &= ~ATA_EH_RESET;
2524 sehc->i.action |= ehc->i.action;
2525 }
2526
2527 rc = prereset(link, deadline);
2528
2529
2530
2531
2532
2533 if (slave && (rc == 0 || rc == -ENOENT)) {
2534 int tmp;
2535
2536 tmp = prereset(slave, deadline);
2537 if (tmp != -ENOENT)
2538 rc = tmp;
2539
2540 ehc->i.action |= sehc->i.action;
2541 }
2542
2543 if (rc) {
2544 if (rc == -ENOENT) {
2545 ata_link_dbg(link, "port disabled--ignoring\n");
2546 ehc->i.action &= ~ATA_EH_RESET;
2547
2548 ata_for_each_dev(dev, link, ALL)
2549 classes[dev->devno] = ATA_DEV_NONE;
2550
2551 rc = 0;
2552 } else
2553 ata_link_err(link,
2554 "prereset failed (errno=%d)\n",
2555 rc);
2556 goto out;
2557 }
2558
2559
2560
2561
2562 if (reset && !(ehc->i.action & ATA_EH_RESET)) {
2563 ata_for_each_dev(dev, link, ALL)
2564 classes[dev->devno] = ATA_DEV_NONE;
2565 if ((ap->pflags & ATA_PFLAG_FROZEN) &&
2566 ata_is_host_link(link))
2567 ata_eh_thaw_port(ap);
2568 rc = 0;
2569 goto out;
2570 }
2571 }
2572
2573 retry:
2574
2575
2576
2577 if (ata_is_host_link(link))
2578 ata_eh_freeze_port(ap);
2579
2580 deadline = ata_deadline(jiffies, ata_eh_reset_timeouts[try++]);
2581
2582 if (reset) {
2583 if (verbose)
2584 ata_link_info(link, "%s resetting link\n",
2585 reset == softreset ? "soft" : "hard");
2586
2587
2588 ehc->last_reset = jiffies;
2589 if (reset == hardreset) {
2590 ehc->i.flags |= ATA_EHI_DID_HARDRESET;
2591 trace_ata_link_hardreset_begin(link, classes, deadline);
2592 } else {
2593 ehc->i.flags |= ATA_EHI_DID_SOFTRESET;
2594 trace_ata_link_softreset_begin(link, classes, deadline);
2595 }
2596
2597 rc = ata_do_reset(link, reset, classes, deadline, true);
2598 if (reset == hardreset)
2599 trace_ata_link_hardreset_end(link, classes, rc);
2600 else
2601 trace_ata_link_softreset_end(link, classes, rc);
2602 if (rc && rc != -EAGAIN) {
2603 failed_link = link;
2604 goto fail;
2605 }
2606
2607
2608 if (slave && reset == hardreset) {
2609 int tmp;
2610
2611 if (verbose)
2612 ata_link_info(slave, "hard resetting link\n");
2613
2614 ata_eh_about_to_do(slave, NULL, ATA_EH_RESET);
2615 trace_ata_slave_hardreset_begin(slave, classes,
2616 deadline);
2617 tmp = ata_do_reset(slave, reset, classes, deadline,
2618 false);
2619 trace_ata_slave_hardreset_end(slave, classes, tmp);
2620 switch (tmp) {
2621 case -EAGAIN:
2622 rc = -EAGAIN;
2623 break;
2624 case 0:
2625 break;
2626 default:
2627 failed_link = slave;
2628 rc = tmp;
2629 goto fail;
2630 }
2631 }
2632
2633
2634 if (reset == hardreset &&
2635 ata_eh_followup_srst_needed(link, rc)) {
2636 reset = softreset;
2637
2638 if (!reset) {
2639 ata_link_err(link,
2640 "follow-up softreset required but no softreset available\n");
2641 failed_link = link;
2642 rc = -EINVAL;
2643 goto fail;
2644 }
2645
2646 ata_eh_about_to_do(link, NULL, ATA_EH_RESET);
2647 trace_ata_link_softreset_begin(link, classes, deadline);
2648 rc = ata_do_reset(link, reset, classes, deadline, true);
2649 trace_ata_link_softreset_end(link, classes, rc);
2650 if (rc) {
2651 failed_link = link;
2652 goto fail;
2653 }
2654 }
2655 } else {
2656 if (verbose)
2657 ata_link_info(link,
2658 "no reset method available, skipping reset\n");
2659 if (!(lflags & ATA_LFLAG_ASSUME_CLASS))
2660 lflags |= ATA_LFLAG_ASSUME_ATA;
2661 }
2662
2663
2664
2665
2666 ata_for_each_dev(dev, link, ALL) {
2667
2668
2669
2670
2671 dev->pio_mode = XFER_PIO_0;
2672 dev->flags &= ~ATA_DFLAG_SLEEPING;
2673
2674 if (ata_phys_link_offline(ata_dev_phys_link(dev)))
2675 continue;
2676
2677
2678 if (lflags & ATA_LFLAG_ASSUME_ATA)
2679 classes[dev->devno] = ATA_DEV_ATA;
2680 else if (lflags & ATA_LFLAG_ASSUME_SEMB)
2681 classes[dev->devno] = ATA_DEV_SEMB_UNSUP;
2682 }
2683
2684
2685 if (sata_scr_read(link, SCR_STATUS, &sstatus) == 0)
2686 link->sata_spd = (sstatus >> 4) & 0xf;
2687 if (slave && sata_scr_read(slave, SCR_STATUS, &sstatus) == 0)
2688 slave->sata_spd = (sstatus >> 4) & 0xf;
2689
2690
2691 if (ata_is_host_link(link))
2692 ata_eh_thaw_port(ap);
2693
2694
2695
2696
2697
2698
2699
2700
2701 if (postreset) {
2702 postreset(link, classes);
2703 trace_ata_link_postreset(link, classes, rc);
2704 if (slave) {
2705 postreset(slave, classes);
2706 trace_ata_slave_postreset(slave, classes, rc);
2707 }
2708 }
2709
2710
2711
2712
2713
2714
2715
2716
2717 spin_lock_irqsave(link->ap->lock, flags);
2718 memset(&link->eh_info, 0, sizeof(link->eh_info));
2719 if (slave)
2720 memset(&slave->eh_info, 0, sizeof(link->eh_info));
2721 ap->pflags &= ~ATA_PFLAG_EH_PENDING;
2722 spin_unlock_irqrestore(link->ap->lock, flags);
2723
2724 if (ap->pflags & ATA_PFLAG_FROZEN)
2725 ata_eh_thaw_port(ap);
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735 nr_unknown = 0;
2736 ata_for_each_dev(dev, link, ALL) {
2737 if (ata_phys_link_online(ata_dev_phys_link(dev))) {
2738 if (classes[dev->devno] == ATA_DEV_UNKNOWN) {
2739 ata_dev_dbg(dev, "link online but device misclassified\n");
2740 classes[dev->devno] = ATA_DEV_NONE;
2741 nr_unknown++;
2742 }
2743 } else if (ata_phys_link_offline(ata_dev_phys_link(dev))) {
2744 if (ata_class_enabled(classes[dev->devno]))
2745 ata_dev_dbg(dev,
2746 "link offline, clearing class %d to NONE\n",
2747 classes[dev->devno]);
2748 classes[dev->devno] = ATA_DEV_NONE;
2749 } else if (classes[dev->devno] == ATA_DEV_UNKNOWN) {
2750 ata_dev_dbg(dev,
2751 "link status unknown, clearing UNKNOWN to NONE\n");
2752 classes[dev->devno] = ATA_DEV_NONE;
2753 }
2754 }
2755
2756 if (classify && nr_unknown) {
2757 if (try < max_tries) {
2758 ata_link_warn(link,
2759 "link online but %d devices misclassified, retrying\n",
2760 nr_unknown);
2761 failed_link = link;
2762 rc = -EAGAIN;
2763 goto fail;
2764 }
2765 ata_link_warn(link,
2766 "link online but %d devices misclassified, "
2767 "device detection might fail\n", nr_unknown);
2768 }
2769
2770
2771 ata_eh_done(link, NULL, ATA_EH_RESET);
2772 if (slave)
2773 ata_eh_done(slave, NULL, ATA_EH_RESET);
2774 ehc->last_reset = jiffies;
2775 ehc->i.action |= ATA_EH_REVALIDATE;
2776 link->lpm_policy = ATA_LPM_UNKNOWN;
2777
2778 rc = 0;
2779 out:
2780
2781 ehc->i.flags &= ~ATA_EHI_HOTPLUGGED;
2782 if (slave)
2783 sehc->i.flags &= ~ATA_EHI_HOTPLUGGED;
2784
2785 spin_lock_irqsave(ap->lock, flags);
2786 ap->pflags &= ~ATA_PFLAG_RESETTING;
2787 spin_unlock_irqrestore(ap->lock, flags);
2788
2789 return rc;
2790
2791 fail:
2792
2793 if (!ata_is_host_link(link) &&
2794 sata_scr_read(link, SCR_STATUS, &sstatus))
2795 rc = -ERESTART;
2796
2797 if (try >= max_tries) {
2798
2799
2800
2801
2802
2803
2804 if (ata_is_host_link(link))
2805 ata_eh_thaw_port(ap);
2806 goto out;
2807 }
2808
2809 now = jiffies;
2810 if (time_before(now, deadline)) {
2811 unsigned long delta = deadline - now;
2812
2813 ata_link_warn(failed_link,
2814 "reset failed (errno=%d), retrying in %u secs\n",
2815 rc, DIV_ROUND_UP(jiffies_to_msecs(delta), 1000));
2816
2817 ata_eh_release(ap);
2818 while (delta)
2819 delta = schedule_timeout_uninterruptible(delta);
2820 ata_eh_acquire(ap);
2821 }
2822
2823
2824
2825
2826
2827 if (rc == -ERESTART) {
2828 if (ata_is_host_link(link))
2829 ata_eh_thaw_port(ap);
2830 goto out;
2831 }
2832
2833 if (try == max_tries - 1) {
2834 sata_down_spd_limit(link, 0);
2835 if (slave)
2836 sata_down_spd_limit(slave, 0);
2837 } else if (rc == -EPIPE)
2838 sata_down_spd_limit(failed_link, 0);
2839
2840 if (hardreset)
2841 reset = hardreset;
2842 goto retry;
2843 }
2844
2845 static inline void ata_eh_pull_park_action(struct ata_port *ap)
2846 {
2847 struct ata_link *link;
2848 struct ata_device *dev;
2849 unsigned long flags;
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877 spin_lock_irqsave(ap->lock, flags);
2878 reinit_completion(&ap->park_req_pending);
2879 ata_for_each_link(link, ap, EDGE) {
2880 ata_for_each_dev(dev, link, ALL) {
2881 struct ata_eh_info *ehi = &link->eh_info;
2882
2883 link->eh_context.i.dev_action[dev->devno] |=
2884 ehi->dev_action[dev->devno] & ATA_EH_PARK;
2885 ata_eh_clear_action(link, dev, ehi, ATA_EH_PARK);
2886 }
2887 }
2888 spin_unlock_irqrestore(ap->lock, flags);
2889 }
2890
2891 static void ata_eh_park_issue_cmd(struct ata_device *dev, int park)
2892 {
2893 struct ata_eh_context *ehc = &dev->link->eh_context;
2894 struct ata_taskfile tf;
2895 unsigned int err_mask;
2896
2897 ata_tf_init(dev, &tf);
2898 if (park) {
2899 ehc->unloaded_mask |= 1 << dev->devno;
2900 tf.command = ATA_CMD_IDLEIMMEDIATE;
2901 tf.feature = 0x44;
2902 tf.lbal = 0x4c;
2903 tf.lbam = 0x4e;
2904 tf.lbah = 0x55;
2905 } else {
2906 ehc->unloaded_mask &= ~(1 << dev->devno);
2907 tf.command = ATA_CMD_CHK_POWER;
2908 }
2909
2910 tf.flags |= ATA_TFLAG_DEVICE | ATA_TFLAG_ISADDR;
2911 tf.protocol = ATA_PROT_NODATA;
2912 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
2913 if (park && (err_mask || tf.lbal != 0xc4)) {
2914 ata_dev_err(dev, "head unload failed!\n");
2915 ehc->unloaded_mask &= ~(1 << dev->devno);
2916 }
2917 }
2918
2919 static int ata_eh_revalidate_and_attach(struct ata_link *link,
2920 struct ata_device **r_failed_dev)
2921 {
2922 struct ata_port *ap = link->ap;
2923 struct ata_eh_context *ehc = &link->eh_context;
2924 struct ata_device *dev;
2925 unsigned int new_mask = 0;
2926 unsigned long flags;
2927 int rc = 0;
2928
2929
2930
2931
2932
2933 ata_for_each_dev(dev, link, ALL_REVERSE) {
2934 unsigned int action = ata_eh_dev_action(dev);
2935 unsigned int readid_flags = 0;
2936
2937 if (ehc->i.flags & ATA_EHI_DID_RESET)
2938 readid_flags |= ATA_READID_POSTRESET;
2939
2940 if ((action & ATA_EH_REVALIDATE) && ata_dev_enabled(dev)) {
2941 WARN_ON(dev->class == ATA_DEV_PMP);
2942
2943 if (ata_phys_link_offline(ata_dev_phys_link(dev))) {
2944 rc = -EIO;
2945 goto err;
2946 }
2947
2948 ata_eh_about_to_do(link, dev, ATA_EH_REVALIDATE);
2949 rc = ata_dev_revalidate(dev, ehc->classes[dev->devno],
2950 readid_flags);
2951 if (rc)
2952 goto err;
2953
2954 ata_eh_done(link, dev, ATA_EH_REVALIDATE);
2955
2956
2957
2958
2959 ehc->i.flags |= ATA_EHI_SETMODE;
2960
2961
2962 schedule_work(&(ap->scsi_rescan_task));
2963 } else if (dev->class == ATA_DEV_UNKNOWN &&
2964 ehc->tries[dev->devno] &&
2965 ata_class_enabled(ehc->classes[dev->devno])) {
2966
2967
2968
2969
2970
2971
2972 dev->class = ehc->classes[dev->devno];
2973
2974 if (dev->class == ATA_DEV_PMP)
2975 rc = sata_pmp_attach(dev);
2976 else
2977 rc = ata_dev_read_id(dev, &dev->class,
2978 readid_flags, dev->id);
2979
2980
2981 ehc->classes[dev->devno] = dev->class;
2982 dev->class = ATA_DEV_UNKNOWN;
2983
2984 switch (rc) {
2985 case 0:
2986
2987 ata_ering_clear(&dev->ering);
2988 new_mask |= 1 << dev->devno;
2989 break;
2990 case -ENOENT:
2991
2992
2993
2994
2995 ata_eh_thaw_port(ap);
2996 break;
2997 default:
2998 goto err;
2999 }
3000 }
3001 }
3002
3003
3004 if ((ehc->i.flags & ATA_EHI_DID_RESET) && ata_is_host_link(link)) {
3005 if (ap->ops->cable_detect)
3006 ap->cbl = ap->ops->cable_detect(ap);
3007 ata_force_cbl(ap);
3008 }
3009
3010
3011
3012
3013 ata_for_each_dev(dev, link, ALL) {
3014 if (!(new_mask & (1 << dev->devno)))
3015 continue;
3016
3017 dev->class = ehc->classes[dev->devno];
3018
3019 if (dev->class == ATA_DEV_PMP)
3020 continue;
3021
3022 ehc->i.flags |= ATA_EHI_PRINTINFO;
3023 rc = ata_dev_configure(dev);
3024 ehc->i.flags &= ~ATA_EHI_PRINTINFO;
3025 if (rc) {
3026 dev->class = ATA_DEV_UNKNOWN;
3027 goto err;
3028 }
3029
3030 spin_lock_irqsave(ap->lock, flags);
3031 ap->pflags |= ATA_PFLAG_SCSI_HOTPLUG;
3032 spin_unlock_irqrestore(ap->lock, flags);
3033
3034
3035 ehc->i.flags |= ATA_EHI_SETMODE;
3036 }
3037
3038 return 0;
3039
3040 err:
3041 *r_failed_dev = dev;
3042 return rc;
3043 }
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060 int ata_set_mode(struct ata_link *link, struct ata_device **r_failed_dev)
3061 {
3062 struct ata_port *ap = link->ap;
3063 struct ata_device *dev;
3064 int rc;
3065
3066
3067 ata_for_each_dev(dev, link, ENABLED) {
3068 if (!(dev->flags & ATA_DFLAG_DUBIOUS_XFER)) {
3069 struct ata_ering_entry *ent;
3070
3071 ent = ata_ering_top(&dev->ering);
3072 if (ent)
3073 ent->eflags &= ~ATA_EFLAG_DUBIOUS_XFER;
3074 }
3075 }
3076
3077
3078 if (ap->ops->set_mode)
3079 rc = ap->ops->set_mode(link, r_failed_dev);
3080 else
3081 rc = ata_do_set_mode(link, r_failed_dev);
3082
3083
3084 ata_for_each_dev(dev, link, ENABLED) {
3085 struct ata_eh_context *ehc = &link->eh_context;
3086 u8 saved_xfer_mode = ehc->saved_xfer_mode[dev->devno];
3087 u8 saved_ncq = !!(ehc->saved_ncq_enabled & (1 << dev->devno));
3088
3089 if (dev->xfer_mode != saved_xfer_mode ||
3090 ata_ncq_enabled(dev) != saved_ncq)
3091 dev->flags |= ATA_DFLAG_DUBIOUS_XFER;
3092 }
3093
3094 return rc;
3095 }
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111 static int atapi_eh_clear_ua(struct ata_device *dev)
3112 {
3113 int i;
3114
3115 for (i = 0; i < ATA_EH_UA_TRIES; i++) {
3116 u8 *sense_buffer = dev->link->ap->sector_buf;
3117 u8 sense_key = 0;
3118 unsigned int err_mask;
3119
3120 err_mask = atapi_eh_tur(dev, &sense_key);
3121 if (err_mask != 0 && err_mask != AC_ERR_DEV) {
3122 ata_dev_warn(dev,
3123 "TEST_UNIT_READY failed (err_mask=0x%x)\n",
3124 err_mask);
3125 return -EIO;
3126 }
3127
3128 if (!err_mask || sense_key != UNIT_ATTENTION)
3129 return 0;
3130
3131 err_mask = atapi_eh_request_sense(dev, sense_buffer, sense_key);
3132 if (err_mask) {
3133 ata_dev_warn(dev, "failed to clear "
3134 "UNIT ATTENTION (err_mask=0x%x)\n", err_mask);
3135 return -EIO;
3136 }
3137 }
3138
3139 ata_dev_warn(dev, "UNIT ATTENTION persists after %d tries\n",
3140 ATA_EH_UA_TRIES);
3141
3142 return 0;
3143 }
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162 static int ata_eh_maybe_retry_flush(struct ata_device *dev)
3163 {
3164 struct ata_link *link = dev->link;
3165 struct ata_port *ap = link->ap;
3166 struct ata_queued_cmd *qc;
3167 struct ata_taskfile tf;
3168 unsigned int err_mask;
3169 int rc = 0;
3170
3171
3172 if (!ata_tag_valid(link->active_tag))
3173 return 0;
3174
3175 qc = __ata_qc_from_tag(ap, link->active_tag);
3176 if (qc->dev != dev || (qc->tf.command != ATA_CMD_FLUSH_EXT &&
3177 qc->tf.command != ATA_CMD_FLUSH))
3178 return 0;
3179
3180
3181 if (qc->err_mask & AC_ERR_DEV)
3182 return 0;
3183
3184
3185 ata_tf_init(dev, &tf);
3186
3187 tf.command = qc->tf.command;
3188 tf.flags |= ATA_TFLAG_DEVICE;
3189 tf.protocol = ATA_PROT_NODATA;
3190
3191 ata_dev_warn(dev, "retrying FLUSH 0x%x Emask 0x%x\n",
3192 tf.command, qc->err_mask);
3193
3194 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
3195 if (!err_mask) {
3196
3197
3198
3199
3200
3201
3202
3203
3204 qc->scsicmd->allowed = max(qc->scsicmd->allowed, 1);
3205 } else {
3206 ata_dev_warn(dev, "FLUSH failed Emask 0x%x\n",
3207 err_mask);
3208 rc = -EIO;
3209
3210
3211 if (err_mask & AC_ERR_DEV) {
3212 qc->err_mask |= AC_ERR_DEV;
3213 qc->result_tf = tf;
3214 if (!(ap->pflags & ATA_PFLAG_FROZEN))
3215 rc = 0;
3216 }
3217 }
3218 return rc;
3219 }
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238 static int ata_eh_set_lpm(struct ata_link *link, enum ata_lpm_policy policy,
3239 struct ata_device **r_failed_dev)
3240 {
3241 struct ata_port *ap = ata_is_host_link(link) ? link->ap : NULL;
3242 struct ata_eh_context *ehc = &link->eh_context;
3243 struct ata_device *dev, *link_dev = NULL, *lpm_dev = NULL;
3244 enum ata_lpm_policy old_policy = link->lpm_policy;
3245 bool no_dipm = link->ap->flags & ATA_FLAG_NO_DIPM;
3246 unsigned int hints = ATA_LPM_EMPTY | ATA_LPM_HIPM;
3247 unsigned int err_mask;
3248 int rc;
3249
3250
3251 if (!IS_ENABLED(CONFIG_SATA_HOST) ||
3252 (link->flags & ATA_LFLAG_NO_LPM) || (ap && !ap->ops->set_lpm))
3253 return 0;
3254
3255
3256
3257
3258
3259
3260
3261 ata_for_each_dev(dev, link, ENABLED) {
3262 bool hipm = ata_id_has_hipm(dev->id);
3263 bool dipm = ata_id_has_dipm(dev->id) && !no_dipm;
3264
3265
3266 if (!link_dev)
3267 link_dev = dev;
3268
3269 if (!lpm_dev && (hipm || dipm))
3270 lpm_dev = dev;
3271
3272 hints &= ~ATA_LPM_EMPTY;
3273 if (!hipm)
3274 hints &= ~ATA_LPM_HIPM;
3275
3276
3277 if (policy < ATA_LPM_MED_POWER_WITH_DIPM && dipm) {
3278 err_mask = ata_dev_set_feature(dev,
3279 SETFEATURES_SATA_DISABLE, SATA_DIPM);
3280 if (err_mask && err_mask != AC_ERR_DEV) {
3281 ata_dev_warn(dev,
3282 "failed to disable DIPM, Emask 0x%x\n",
3283 err_mask);
3284 rc = -EIO;
3285 goto fail;
3286 }
3287 }
3288 }
3289
3290 if (ap) {
3291 rc = ap->ops->set_lpm(link, policy, hints);
3292 if (!rc && ap->slave_link)
3293 rc = ap->ops->set_lpm(ap->slave_link, policy, hints);
3294 } else
3295 rc = sata_pmp_set_lpm(link, policy, hints);
3296
3297
3298
3299
3300
3301 if (rc) {
3302 if (rc == -EOPNOTSUPP) {
3303 link->flags |= ATA_LFLAG_NO_LPM;
3304 return 0;
3305 }
3306 dev = lpm_dev ? lpm_dev : link_dev;
3307 goto fail;
3308 }
3309
3310
3311
3312
3313
3314 link->lpm_policy = policy;
3315 if (ap && ap->slave_link)
3316 ap->slave_link->lpm_policy = policy;
3317
3318
3319 ata_for_each_dev(dev, link, ENABLED) {
3320 if (policy >= ATA_LPM_MED_POWER_WITH_DIPM && !no_dipm &&
3321 ata_id_has_dipm(dev->id)) {
3322 err_mask = ata_dev_set_feature(dev,
3323 SETFEATURES_SATA_ENABLE, SATA_DIPM);
3324 if (err_mask && err_mask != AC_ERR_DEV) {
3325 ata_dev_warn(dev,
3326 "failed to enable DIPM, Emask 0x%x\n",
3327 err_mask);
3328 rc = -EIO;
3329 goto fail;
3330 }
3331 }
3332 }
3333
3334 link->last_lpm_change = jiffies;
3335 link->flags |= ATA_LFLAG_CHANGED;
3336
3337 return 0;
3338
3339 fail:
3340
3341 link->lpm_policy = old_policy;
3342 if (ap && ap->slave_link)
3343 ap->slave_link->lpm_policy = old_policy;
3344
3345
3346 if (!dev || ehc->tries[dev->devno] <= 2) {
3347 ata_link_warn(link, "disabling LPM on the link\n");
3348 link->flags |= ATA_LFLAG_NO_LPM;
3349 }
3350 if (r_failed_dev)
3351 *r_failed_dev = dev;
3352 return rc;
3353 }
3354
3355 int ata_link_nr_enabled(struct ata_link *link)
3356 {
3357 struct ata_device *dev;
3358 int cnt = 0;
3359
3360 ata_for_each_dev(dev, link, ENABLED)
3361 cnt++;
3362 return cnt;
3363 }
3364
3365 static int ata_link_nr_vacant(struct ata_link *link)
3366 {
3367 struct ata_device *dev;
3368 int cnt = 0;
3369
3370 ata_for_each_dev(dev, link, ALL)
3371 if (dev->class == ATA_DEV_UNKNOWN)
3372 cnt++;
3373 return cnt;
3374 }
3375
3376 static int ata_eh_skip_recovery(struct ata_link *link)
3377 {
3378 struct ata_port *ap = link->ap;
3379 struct ata_eh_context *ehc = &link->eh_context;
3380 struct ata_device *dev;
3381
3382
3383 if (link->flags & ATA_LFLAG_DISABLED)
3384 return 1;
3385
3386
3387 if (ehc->i.flags & ATA_EHI_NO_RECOVERY)
3388 return 1;
3389
3390
3391 if ((ap->pflags & ATA_PFLAG_FROZEN) || ata_link_nr_enabled(link))
3392 return 0;
3393
3394
3395 if ((ehc->i.action & ATA_EH_RESET) &&
3396 !(ehc->i.flags & ATA_EHI_DID_RESET))
3397 return 0;
3398
3399
3400 ata_for_each_dev(dev, link, ALL) {
3401 if (dev->class == ATA_DEV_UNKNOWN &&
3402 ehc->classes[dev->devno] != ATA_DEV_NONE)
3403 return 0;
3404 }
3405
3406 return 1;
3407 }
3408
3409 static int ata_count_probe_trials_cb(struct ata_ering_entry *ent, void *void_arg)
3410 {
3411 u64 interval = msecs_to_jiffies(ATA_EH_PROBE_TRIAL_INTERVAL);
3412 u64 now = get_jiffies_64();
3413 int *trials = void_arg;
3414
3415 if ((ent->eflags & ATA_EFLAG_OLD_ER) ||
3416 (ent->timestamp < now - min(now, interval)))
3417 return -1;
3418
3419 (*trials)++;
3420 return 0;
3421 }
3422
3423 static int ata_eh_schedule_probe(struct ata_device *dev)
3424 {
3425 struct ata_eh_context *ehc = &dev->link->eh_context;
3426 struct ata_link *link = ata_dev_phys_link(dev);
3427 int trials = 0;
3428
3429 if (!(ehc->i.probe_mask & (1 << dev->devno)) ||
3430 (ehc->did_probe_mask & (1 << dev->devno)))
3431 return 0;
3432
3433 ata_eh_detach_dev(dev);
3434 ata_dev_init(dev);
3435 ehc->did_probe_mask |= (1 << dev->devno);
3436 ehc->i.action |= ATA_EH_RESET;
3437 ehc->saved_xfer_mode[dev->devno] = 0;
3438 ehc->saved_ncq_enabled &= ~(1 << dev->devno);
3439
3440
3441 if (link->lpm_policy > ATA_LPM_MAX_POWER) {
3442 if (ata_is_host_link(link))
3443 link->ap->ops->set_lpm(link, ATA_LPM_MAX_POWER,
3444 ATA_LPM_EMPTY);
3445 else
3446 sata_pmp_set_lpm(link, ATA_LPM_MAX_POWER,
3447 ATA_LPM_EMPTY);
3448 }
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463 ata_ering_record(&dev->ering, 0, AC_ERR_OTHER);
3464 ata_ering_map(&dev->ering, ata_count_probe_trials_cb, &trials);
3465
3466 if (trials > ATA_EH_PROBE_TRIALS)
3467 sata_down_spd_limit(link, 1);
3468
3469 return 1;
3470 }
3471
3472 static int ata_eh_handle_dev_fail(struct ata_device *dev, int err)
3473 {
3474 struct ata_eh_context *ehc = &dev->link->eh_context;
3475
3476
3477
3478
3479 if (err != -EAGAIN)
3480 ehc->tries[dev->devno]--;
3481
3482 switch (err) {
3483 case -ENODEV:
3484
3485 ehc->i.probe_mask |= (1 << dev->devno);
3486 fallthrough;
3487 case -EINVAL:
3488
3489 ehc->tries[dev->devno] = min(ehc->tries[dev->devno], 1);
3490 fallthrough;
3491 case -EIO:
3492 if (ehc->tries[dev->devno] == 1) {
3493
3494
3495
3496 sata_down_spd_limit(ata_dev_phys_link(dev), 0);
3497 if (dev->pio_mode > XFER_PIO_0)
3498 ata_down_xfermask_limit(dev, ATA_DNXFER_PIO);
3499 }
3500 }
3501
3502 if (ata_dev_enabled(dev) && !ehc->tries[dev->devno]) {
3503
3504 ata_dev_disable(dev);
3505
3506
3507 if (ata_phys_link_offline(ata_dev_phys_link(dev)))
3508 ata_eh_detach_dev(dev);
3509
3510
3511 if (ata_eh_schedule_probe(dev)) {
3512 ehc->tries[dev->devno] = ATA_EH_DEV_TRIES;
3513 memset(ehc->cmd_timeout_idx[dev->devno], 0,
3514 sizeof(ehc->cmd_timeout_idx[dev->devno]));
3515 }
3516
3517 return 1;
3518 } else {
3519 ehc->i.action |= ATA_EH_RESET;
3520 return 0;
3521 }
3522 }
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546 int ata_eh_recover(struct ata_port *ap, ata_prereset_fn_t prereset,
3547 ata_reset_fn_t softreset, ata_reset_fn_t hardreset,
3548 ata_postreset_fn_t postreset,
3549 struct ata_link **r_failed_link)
3550 {
3551 struct ata_link *link;
3552 struct ata_device *dev;
3553 int rc, nr_fails;
3554 unsigned long flags, deadline;
3555
3556
3557 ata_for_each_link(link, ap, EDGE) {
3558 struct ata_eh_context *ehc = &link->eh_context;
3559
3560
3561 if (ehc->i.action & ATA_EH_ENABLE_LINK) {
3562 ata_eh_about_to_do(link, NULL, ATA_EH_ENABLE_LINK);
3563 spin_lock_irqsave(ap->lock, flags);
3564 link->flags &= ~ATA_LFLAG_DISABLED;
3565 spin_unlock_irqrestore(ap->lock, flags);
3566 ata_eh_done(link, NULL, ATA_EH_ENABLE_LINK);
3567 }
3568
3569 ata_for_each_dev(dev, link, ALL) {
3570 if (link->flags & ATA_LFLAG_NO_RETRY)
3571 ehc->tries[dev->devno] = 1;
3572 else
3573 ehc->tries[dev->devno] = ATA_EH_DEV_TRIES;
3574
3575
3576 ehc->i.action |= ehc->i.dev_action[dev->devno] &
3577 ~ATA_EH_PERDEV_MASK;
3578 ehc->i.dev_action[dev->devno] &= ATA_EH_PERDEV_MASK;
3579
3580
3581 if (dev->flags & ATA_DFLAG_DETACH)
3582 ata_eh_detach_dev(dev);
3583
3584
3585 if (!ata_dev_enabled(dev))
3586 ata_eh_schedule_probe(dev);
3587 }
3588 }
3589
3590 retry:
3591 rc = 0;
3592
3593
3594 if (ap->pflags & ATA_PFLAG_UNLOADING)
3595 goto out;
3596
3597
3598 ata_for_each_link(link, ap, EDGE) {
3599 struct ata_eh_context *ehc = &link->eh_context;
3600
3601
3602 if (ata_eh_skip_recovery(link))
3603 ehc->i.action = 0;
3604
3605 ata_for_each_dev(dev, link, ALL)
3606 ehc->classes[dev->devno] = ATA_DEV_UNKNOWN;
3607 }
3608
3609
3610 ata_for_each_link(link, ap, EDGE) {
3611 struct ata_eh_context *ehc = &link->eh_context;
3612
3613 if (!(ehc->i.action & ATA_EH_RESET))
3614 continue;
3615
3616 rc = ata_eh_reset(link, ata_link_nr_vacant(link),
3617 prereset, softreset, hardreset, postreset);
3618 if (rc) {
3619 ata_link_err(link, "reset failed, giving up\n");
3620 goto out;
3621 }
3622 }
3623
3624 do {
3625 unsigned long now;
3626
3627
3628
3629
3630
3631 ata_eh_pull_park_action(ap);
3632
3633 deadline = jiffies;
3634 ata_for_each_link(link, ap, EDGE) {
3635 ata_for_each_dev(dev, link, ALL) {
3636 struct ata_eh_context *ehc = &link->eh_context;
3637 unsigned long tmp;
3638
3639 if (dev->class != ATA_DEV_ATA &&
3640 dev->class != ATA_DEV_ZAC)
3641 continue;
3642 if (!(ehc->i.dev_action[dev->devno] &
3643 ATA_EH_PARK))
3644 continue;
3645 tmp = dev->unpark_deadline;
3646 if (time_before(deadline, tmp))
3647 deadline = tmp;
3648 else if (time_before_eq(tmp, jiffies))
3649 continue;
3650 if (ehc->unloaded_mask & (1 << dev->devno))
3651 continue;
3652
3653 ata_eh_park_issue_cmd(dev, 1);
3654 }
3655 }
3656
3657 now = jiffies;
3658 if (time_before_eq(deadline, now))
3659 break;
3660
3661 ata_eh_release(ap);
3662 deadline = wait_for_completion_timeout(&ap->park_req_pending,
3663 deadline - now);
3664 ata_eh_acquire(ap);
3665 } while (deadline);
3666 ata_for_each_link(link, ap, EDGE) {
3667 ata_for_each_dev(dev, link, ALL) {
3668 if (!(link->eh_context.unloaded_mask &
3669 (1 << dev->devno)))
3670 continue;
3671
3672 ata_eh_park_issue_cmd(dev, 0);
3673 ata_eh_done(link, dev, ATA_EH_PARK);
3674 }
3675 }
3676
3677
3678 nr_fails = 0;
3679 ata_for_each_link(link, ap, PMP_FIRST) {
3680 struct ata_eh_context *ehc = &link->eh_context;
3681
3682 if (sata_pmp_attached(ap) && ata_is_host_link(link))
3683 goto config_lpm;
3684
3685
3686 rc = ata_eh_revalidate_and_attach(link, &dev);
3687 if (rc)
3688 goto rest_fail;
3689
3690
3691 if (link->device->class == ATA_DEV_PMP) {
3692 ehc->i.action = 0;
3693 return 0;
3694 }
3695
3696
3697 if (ehc->i.flags & ATA_EHI_SETMODE) {
3698 rc = ata_set_mode(link, &dev);
3699 if (rc)
3700 goto rest_fail;
3701 ehc->i.flags &= ~ATA_EHI_SETMODE;
3702 }
3703
3704
3705
3706
3707 if (ehc->i.flags & ATA_EHI_DID_RESET) {
3708 ata_for_each_dev(dev, link, ALL) {
3709 if (dev->class != ATA_DEV_ATAPI)
3710 continue;
3711 rc = atapi_eh_clear_ua(dev);
3712 if (rc)
3713 goto rest_fail;
3714 if (zpodd_dev_enabled(dev))
3715 zpodd_post_poweron(dev);
3716 }
3717 }
3718
3719
3720 ata_for_each_dev(dev, link, ALL) {
3721 if (dev->class != ATA_DEV_ATA &&
3722 dev->class != ATA_DEV_ZAC)
3723 continue;
3724 rc = ata_eh_maybe_retry_flush(dev);
3725 if (rc)
3726 goto rest_fail;
3727 }
3728
3729 config_lpm:
3730
3731 if (link->lpm_policy != ap->target_lpm_policy) {
3732 rc = ata_eh_set_lpm(link, ap->target_lpm_policy, &dev);
3733 if (rc)
3734 goto rest_fail;
3735 }
3736
3737
3738 ehc->i.flags = 0;
3739 continue;
3740
3741 rest_fail:
3742 nr_fails++;
3743 if (dev)
3744 ata_eh_handle_dev_fail(dev, rc);
3745
3746 if (ap->pflags & ATA_PFLAG_FROZEN) {
3747
3748
3749
3750 if (sata_pmp_attached(ap))
3751 goto out;
3752 break;
3753 }
3754 }
3755
3756 if (nr_fails)
3757 goto retry;
3758
3759 out:
3760 if (rc && r_failed_link)
3761 *r_failed_link = link;
3762
3763 return rc;
3764 }
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776 void ata_eh_finish(struct ata_port *ap)
3777 {
3778 struct ata_queued_cmd *qc;
3779 int tag;
3780
3781
3782 ata_qc_for_each_raw(ap, qc, tag) {
3783 if (!(qc->flags & ATA_QCFLAG_FAILED))
3784 continue;
3785
3786 if (qc->err_mask) {
3787
3788
3789
3790
3791 if (qc->flags & ATA_QCFLAG_RETRY)
3792 ata_eh_qc_retry(qc);
3793 else
3794 ata_eh_qc_complete(qc);
3795 } else {
3796 if (qc->flags & ATA_QCFLAG_SENSE_VALID) {
3797 ata_eh_qc_complete(qc);
3798 } else {
3799
3800 memset(&qc->result_tf, 0, sizeof(qc->result_tf));
3801 ata_eh_qc_retry(qc);
3802 }
3803 }
3804 }
3805
3806
3807 WARN_ON(ap->nr_active_links);
3808 ap->nr_active_links = 0;
3809 }
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825 void ata_do_eh(struct ata_port *ap, ata_prereset_fn_t prereset,
3826 ata_reset_fn_t softreset, ata_reset_fn_t hardreset,
3827 ata_postreset_fn_t postreset)
3828 {
3829 struct ata_device *dev;
3830 int rc;
3831
3832 ata_eh_autopsy(ap);
3833 ata_eh_report(ap);
3834
3835 rc = ata_eh_recover(ap, prereset, softreset, hardreset, postreset,
3836 NULL);
3837 if (rc) {
3838 ata_for_each_dev(dev, &ap->link, ALL)
3839 ata_dev_disable(dev);
3840 }
3841
3842 ata_eh_finish(ap);
3843 }
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854 void ata_std_error_handler(struct ata_port *ap)
3855 {
3856 struct ata_port_operations *ops = ap->ops;
3857 ata_reset_fn_t hardreset = ops->hardreset;
3858
3859
3860 if (hardreset == sata_std_hardreset && !sata_scr_valid(&ap->link))
3861 hardreset = NULL;
3862
3863 ata_do_eh(ap, ops->prereset, ops->softreset, hardreset, ops->postreset);
3864 }
3865 EXPORT_SYMBOL_GPL(ata_std_error_handler);
3866
3867 #ifdef CONFIG_PM
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877 static void ata_eh_handle_port_suspend(struct ata_port *ap)
3878 {
3879 unsigned long flags;
3880 int rc = 0;
3881 struct ata_device *dev;
3882
3883
3884 spin_lock_irqsave(ap->lock, flags);
3885 if (!(ap->pflags & ATA_PFLAG_PM_PENDING) ||
3886 ap->pm_mesg.event & PM_EVENT_RESUME) {
3887 spin_unlock_irqrestore(ap->lock, flags);
3888 return;
3889 }
3890 spin_unlock_irqrestore(ap->lock, flags);
3891
3892 WARN_ON(ap->pflags & ATA_PFLAG_SUSPENDED);
3893
3894
3895
3896
3897
3898
3899 if (PMSG_IS_AUTO(ap->pm_mesg)) {
3900 ata_for_each_dev(dev, &ap->link, ENABLED) {
3901 if (zpodd_dev_enabled(dev))
3902 zpodd_on_suspend(dev);
3903 }
3904 }
3905
3906
3907 ata_eh_freeze_port(ap);
3908
3909 if (ap->ops->port_suspend)
3910 rc = ap->ops->port_suspend(ap, ap->pm_mesg);
3911
3912 ata_acpi_set_state(ap, ap->pm_mesg);
3913
3914
3915 spin_lock_irqsave(ap->lock, flags);
3916
3917 ap->pflags &= ~ATA_PFLAG_PM_PENDING;
3918 if (rc == 0)
3919 ap->pflags |= ATA_PFLAG_SUSPENDED;
3920 else if (ap->pflags & ATA_PFLAG_FROZEN)
3921 ata_port_schedule_eh(ap);
3922
3923 spin_unlock_irqrestore(ap->lock, flags);
3924
3925 return;
3926 }
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937 static void ata_eh_handle_port_resume(struct ata_port *ap)
3938 {
3939 struct ata_link *link;
3940 struct ata_device *dev;
3941 unsigned long flags;
3942
3943
3944 spin_lock_irqsave(ap->lock, flags);
3945 if (!(ap->pflags & ATA_PFLAG_PM_PENDING) ||
3946 !(ap->pm_mesg.event & PM_EVENT_RESUME)) {
3947 spin_unlock_irqrestore(ap->lock, flags);
3948 return;
3949 }
3950 spin_unlock_irqrestore(ap->lock, flags);
3951
3952 WARN_ON(!(ap->pflags & ATA_PFLAG_SUSPENDED));
3953
3954
3955
3956
3957
3958
3959
3960
3961 ata_for_each_link(link, ap, HOST_FIRST)
3962 ata_for_each_dev(dev, link, ALL)
3963 ata_ering_clear(&dev->ering);
3964
3965 ata_acpi_set_state(ap, ap->pm_mesg);
3966
3967 if (ap->ops->port_resume)
3968 ap->ops->port_resume(ap);
3969
3970
3971 ata_acpi_on_resume(ap);
3972
3973
3974 spin_lock_irqsave(ap->lock, flags);
3975 ap->pflags &= ~(ATA_PFLAG_PM_PENDING | ATA_PFLAG_SUSPENDED);
3976 spin_unlock_irqrestore(ap->lock, flags);
3977 }
3978 #endif