1c82ee6d3SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later 2c6fd2807SJeff Garzik /* 3c6fd2807SJeff Garzik * libata-eh.c - libata error handling 4c6fd2807SJeff Garzik * 5c6fd2807SJeff Garzik * Copyright 2006 Tejun Heo <htejun@gmail.com> 6c6fd2807SJeff Garzik * 7c6fd2807SJeff Garzik * libata documentation is available via 'make {ps|pdf}docs', 89bb9a39cSMauro Carvalho Chehab * as Documentation/driver-api/libata.rst 9c6fd2807SJeff Garzik * 10c6fd2807SJeff Garzik * Hardware documentation available from http://www.t13.org/ and 11c6fd2807SJeff Garzik * http://www.sata-io.org/ 12c6fd2807SJeff Garzik */ 13c6fd2807SJeff Garzik 14c6fd2807SJeff Garzik #include <linux/kernel.h> 15242f9dcbSJens Axboe #include <linux/blkdev.h> 1638789fdaSPaul Gortmaker #include <linux/export.h> 172855568bSJeff Garzik #include <linux/pci.h> 18c6fd2807SJeff Garzik #include <scsi/scsi.h> 19c6fd2807SJeff Garzik #include <scsi/scsi_host.h> 20c6fd2807SJeff Garzik #include <scsi/scsi_eh.h> 21c6fd2807SJeff Garzik #include <scsi/scsi_device.h> 22c6fd2807SJeff Garzik #include <scsi/scsi_cmnd.h> 236521148cSRobert Hancock #include <scsi/scsi_dbg.h> 24c6fd2807SJeff Garzik #include "../scsi/scsi_transport_api.h" 25c6fd2807SJeff Garzik 26c6fd2807SJeff Garzik #include <linux/libata.h> 27c6fd2807SJeff Garzik 28255c03d1SHannes Reinecke #include <trace/events/libata.h> 29c6fd2807SJeff Garzik #include "libata.h" 30c6fd2807SJeff Garzik 317d47e8d4STejun Heo enum { 323884f7b0STejun Heo /* speed down verdicts */ 337d47e8d4STejun Heo ATA_EH_SPDN_NCQ_OFF = (1 << 0), 347d47e8d4STejun Heo ATA_EH_SPDN_SPEED_DOWN = (1 << 1), 357d47e8d4STejun Heo ATA_EH_SPDN_FALLBACK_TO_PIO = (1 << 2), 3676326ac1STejun Heo ATA_EH_SPDN_KEEP_ERRORS = (1 << 3), 373884f7b0STejun Heo 383884f7b0STejun Heo /* error flags */ 393884f7b0STejun Heo ATA_EFLAG_IS_IO = (1 << 0), 4076326ac1STejun Heo ATA_EFLAG_DUBIOUS_XFER = (1 << 1), 41d9027470SGwendal Grignou ATA_EFLAG_OLD_ER = (1 << 31), 423884f7b0STejun Heo 433884f7b0STejun Heo /* error categories */ 443884f7b0STejun Heo ATA_ECAT_NONE = 0, 453884f7b0STejun Heo ATA_ECAT_ATA_BUS = 1, 463884f7b0STejun Heo ATA_ECAT_TOUT_HSM = 2, 473884f7b0STejun Heo ATA_ECAT_UNK_DEV = 3, 4875f9cafcSTejun Heo ATA_ECAT_DUBIOUS_NONE = 4, 4975f9cafcSTejun Heo ATA_ECAT_DUBIOUS_ATA_BUS = 5, 5075f9cafcSTejun Heo ATA_ECAT_DUBIOUS_TOUT_HSM = 6, 5175f9cafcSTejun Heo ATA_ECAT_DUBIOUS_UNK_DEV = 7, 5275f9cafcSTejun Heo ATA_ECAT_NR = 8, 537d47e8d4STejun Heo 5487fbc5a0STejun Heo ATA_EH_CMD_DFL_TIMEOUT = 5000, 5587fbc5a0STejun Heo 560a2c0f56STejun Heo /* always put at least this amount of time between resets */ 570a2c0f56STejun Heo ATA_EH_RESET_COOL_DOWN = 5000, 580a2c0f56STejun Heo 59341c2c95STejun Heo /* Waiting in ->prereset can never be reliable. It's 60341c2c95STejun Heo * sometimes nice to wait there but it can't be depended upon; 61341c2c95STejun Heo * otherwise, we wouldn't be resetting. Just give it enough 62341c2c95STejun Heo * time for most drives to spin up. 6331daabdaSTejun Heo */ 64341c2c95STejun Heo ATA_EH_PRERESET_TIMEOUT = 10000, 65341c2c95STejun Heo ATA_EH_FASTDRAIN_INTERVAL = 3000, 6611fc33daSTejun Heo 6711fc33daSTejun Heo ATA_EH_UA_TRIES = 5, 68c2c7a89cSTejun Heo 69c2c7a89cSTejun Heo /* probe speed down parameters, see ata_eh_schedule_probe() */ 70c2c7a89cSTejun Heo ATA_EH_PROBE_TRIAL_INTERVAL = 60000, /* 1 min */ 71c2c7a89cSTejun Heo ATA_EH_PROBE_TRIALS = 2, 7231daabdaSTejun Heo }; 7331daabdaSTejun Heo 7431daabdaSTejun Heo /* The following table determines how we sequence resets. Each entry 7531daabdaSTejun Heo * represents timeout for that try. The first try can be soft or 7631daabdaSTejun Heo * hardreset. All others are hardreset if available. In most cases 7731daabdaSTejun Heo * the first reset w/ 10sec timeout should succeed. Following entries 7835bf8821SDan Williams * are mostly for error handling, hotplug and those outlier devices that 7935bf8821SDan Williams * take an exceptionally long time to recover from reset. 8031daabdaSTejun Heo */ 8131daabdaSTejun Heo static const unsigned long ata_eh_reset_timeouts[] = { 82341c2c95STejun Heo 10000, /* most drives spin up by 10sec */ 83341c2c95STejun Heo 10000, /* > 99% working drives spin up before 20sec */ 8435bf8821SDan Williams 35000, /* give > 30 secs of idleness for outlier devices */ 85341c2c95STejun Heo 5000, /* and sweet one last chance */ 86d8af0eb6STejun Heo ULONG_MAX, /* > 1 min has elapsed, give up */ 8731daabdaSTejun Heo }; 8831daabdaSTejun Heo 89e06233f9SSergey Shtylyov static const unsigned int ata_eh_identify_timeouts[] = { 9087fbc5a0STejun Heo 5000, /* covers > 99% of successes and not too boring on failures */ 9187fbc5a0STejun Heo 10000, /* combined time till here is enough even for media access */ 9287fbc5a0STejun Heo 30000, /* for true idiots */ 93e06233f9SSergey Shtylyov UINT_MAX, 9487fbc5a0STejun Heo }; 9587fbc5a0STejun Heo 96e06233f9SSergey Shtylyov static const unsigned int ata_eh_revalidate_timeouts[] = { 9768dbbe7dSDamien Le Moal 15000, /* Some drives are slow to read log pages when waking-up */ 9868dbbe7dSDamien Le Moal 15000, /* combined time till here is enough even for media access */ 99e06233f9SSergey Shtylyov UINT_MAX, 10068dbbe7dSDamien Le Moal }; 10168dbbe7dSDamien Le Moal 102e06233f9SSergey Shtylyov static const unsigned int ata_eh_flush_timeouts[] = { 1036013efd8STejun Heo 15000, /* be generous with flush */ 1046013efd8STejun Heo 15000, /* ditto */ 1056013efd8STejun Heo 30000, /* and even more generous */ 106e06233f9SSergey Shtylyov UINT_MAX, 1076013efd8STejun Heo }; 1086013efd8STejun Heo 109e06233f9SSergey Shtylyov static const unsigned int ata_eh_other_timeouts[] = { 11087fbc5a0STejun Heo 5000, /* same rationale as identify timeout */ 11187fbc5a0STejun Heo 10000, /* ditto */ 11287fbc5a0STejun Heo /* but no merciful 30sec for other commands, it just isn't worth it */ 113e06233f9SSergey Shtylyov UINT_MAX, 11487fbc5a0STejun Heo }; 11587fbc5a0STejun Heo 11687fbc5a0STejun Heo struct ata_eh_cmd_timeout_ent { 11787fbc5a0STejun Heo const u8 *commands; 118e06233f9SSergey Shtylyov const unsigned int *timeouts; 11987fbc5a0STejun Heo }; 12087fbc5a0STejun Heo 12187fbc5a0STejun Heo /* The following table determines timeouts to use for EH internal 12287fbc5a0STejun Heo * commands. Each table entry is a command class and matches the 12387fbc5a0STejun Heo * commands the entry applies to and the timeout table to use. 12487fbc5a0STejun Heo * 12587fbc5a0STejun Heo * On the retry after a command timed out, the next timeout value from 12687fbc5a0STejun Heo * the table is used. If the table doesn't contain further entries, 12787fbc5a0STejun Heo * the last value is used. 12887fbc5a0STejun Heo * 12987fbc5a0STejun Heo * ehc->cmd_timeout_idx keeps track of which timeout to use per 13087fbc5a0STejun Heo * command class, so if SET_FEATURES times out on the first try, the 13187fbc5a0STejun Heo * next try will use the second timeout value only for that class. 13287fbc5a0STejun Heo */ 13387fbc5a0STejun Heo #define CMDS(cmds...) (const u8 []){ cmds, 0 } 13487fbc5a0STejun Heo static const struct ata_eh_cmd_timeout_ent 13587fbc5a0STejun Heo ata_eh_cmd_timeout_table[ATA_EH_CMD_TIMEOUT_TABLE_SIZE] = { 13687fbc5a0STejun Heo { .commands = CMDS(ATA_CMD_ID_ATA, ATA_CMD_ID_ATAPI), 13787fbc5a0STejun Heo .timeouts = ata_eh_identify_timeouts, }, 13868dbbe7dSDamien Le Moal { .commands = CMDS(ATA_CMD_READ_LOG_EXT, ATA_CMD_READ_LOG_DMA_EXT), 13968dbbe7dSDamien Le Moal .timeouts = ata_eh_revalidate_timeouts, }, 14087fbc5a0STejun Heo { .commands = CMDS(ATA_CMD_READ_NATIVE_MAX, ATA_CMD_READ_NATIVE_MAX_EXT), 14187fbc5a0STejun Heo .timeouts = ata_eh_other_timeouts, }, 14287fbc5a0STejun Heo { .commands = CMDS(ATA_CMD_SET_MAX, ATA_CMD_SET_MAX_EXT), 14387fbc5a0STejun Heo .timeouts = ata_eh_other_timeouts, }, 14487fbc5a0STejun Heo { .commands = CMDS(ATA_CMD_SET_FEATURES), 14587fbc5a0STejun Heo .timeouts = ata_eh_other_timeouts, }, 14687fbc5a0STejun Heo { .commands = CMDS(ATA_CMD_INIT_DEV_PARAMS), 14787fbc5a0STejun Heo .timeouts = ata_eh_other_timeouts, }, 1486013efd8STejun Heo { .commands = CMDS(ATA_CMD_FLUSH, ATA_CMD_FLUSH_EXT), 1496013efd8STejun Heo .timeouts = ata_eh_flush_timeouts }, 15087fbc5a0STejun Heo }; 15187fbc5a0STejun Heo #undef CMDS 15287fbc5a0STejun Heo 153c6fd2807SJeff Garzik static void __ata_port_freeze(struct ata_port *ap); 15471d7b6e5SNiklas Cassel static int ata_eh_set_lpm(struct ata_link *link, enum ata_lpm_policy policy, 15571d7b6e5SNiklas Cassel struct ata_device **r_failed_dev); 1566ffa01d8STejun Heo #ifdef CONFIG_PM 157c6fd2807SJeff Garzik static void ata_eh_handle_port_suspend(struct ata_port *ap); 158c6fd2807SJeff Garzik static void ata_eh_handle_port_resume(struct ata_port *ap); 1596ffa01d8STejun Heo #else /* CONFIG_PM */ 1606ffa01d8STejun Heo static void ata_eh_handle_port_suspend(struct ata_port *ap) 1616ffa01d8STejun Heo { } 1626ffa01d8STejun Heo 1636ffa01d8STejun Heo static void ata_eh_handle_port_resume(struct ata_port *ap) 1646ffa01d8STejun Heo { } 1656ffa01d8STejun Heo #endif /* CONFIG_PM */ 166c6fd2807SJeff Garzik 1670d74d872SMathieu Malaterre static __printf(2, 0) void __ata_ehi_pushv_desc(struct ata_eh_info *ehi, 1680d74d872SMathieu Malaterre const char *fmt, va_list args) 169b64bbc39STejun Heo { 170b64bbc39STejun Heo ehi->desc_len += vscnprintf(ehi->desc + ehi->desc_len, 171b64bbc39STejun Heo ATA_EH_DESC_LEN - ehi->desc_len, 172b64bbc39STejun Heo fmt, args); 173b64bbc39STejun Heo } 174b64bbc39STejun Heo 175b64bbc39STejun Heo /** 176b64bbc39STejun Heo * __ata_ehi_push_desc - push error description without adding separator 177b64bbc39STejun Heo * @ehi: target EHI 178b64bbc39STejun Heo * @fmt: printf format string 179b64bbc39STejun Heo * 180b64bbc39STejun Heo * Format string according to @fmt and append it to @ehi->desc. 181b64bbc39STejun Heo * 182b64bbc39STejun Heo * LOCKING: 183b64bbc39STejun Heo * spin_lock_irqsave(host lock) 184b64bbc39STejun Heo */ 185b64bbc39STejun Heo void __ata_ehi_push_desc(struct ata_eh_info *ehi, const char *fmt, ...) 186b64bbc39STejun Heo { 187b64bbc39STejun Heo va_list args; 188b64bbc39STejun Heo 189b64bbc39STejun Heo va_start(args, fmt); 190b64bbc39STejun Heo __ata_ehi_pushv_desc(ehi, fmt, args); 191b64bbc39STejun Heo va_end(args); 192b64bbc39STejun Heo } 193a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(__ata_ehi_push_desc); 194b64bbc39STejun Heo 195b64bbc39STejun Heo /** 196b64bbc39STejun Heo * ata_ehi_push_desc - push error description with separator 197b64bbc39STejun Heo * @ehi: target EHI 198b64bbc39STejun Heo * @fmt: printf format string 199b64bbc39STejun Heo * 200b64bbc39STejun Heo * Format string according to @fmt and append it to @ehi->desc. 201b64bbc39STejun Heo * If @ehi->desc is not empty, ", " is added in-between. 202b64bbc39STejun Heo * 203b64bbc39STejun Heo * LOCKING: 204b64bbc39STejun Heo * spin_lock_irqsave(host lock) 205b64bbc39STejun Heo */ 206b64bbc39STejun Heo void ata_ehi_push_desc(struct ata_eh_info *ehi, const char *fmt, ...) 207b64bbc39STejun Heo { 208b64bbc39STejun Heo va_list args; 209b64bbc39STejun Heo 210b64bbc39STejun Heo if (ehi->desc_len) 211b64bbc39STejun Heo __ata_ehi_push_desc(ehi, ", "); 212b64bbc39STejun Heo 213b64bbc39STejun Heo va_start(args, fmt); 214b64bbc39STejun Heo __ata_ehi_pushv_desc(ehi, fmt, args); 215b64bbc39STejun Heo va_end(args); 216b64bbc39STejun Heo } 217a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_ehi_push_desc); 218b64bbc39STejun Heo 219b64bbc39STejun Heo /** 220b64bbc39STejun Heo * ata_ehi_clear_desc - clean error description 221b64bbc39STejun Heo * @ehi: target EHI 222b64bbc39STejun Heo * 223b64bbc39STejun Heo * Clear @ehi->desc. 224b64bbc39STejun Heo * 225b64bbc39STejun Heo * LOCKING: 226b64bbc39STejun Heo * spin_lock_irqsave(host lock) 227b64bbc39STejun Heo */ 228b64bbc39STejun Heo void ata_ehi_clear_desc(struct ata_eh_info *ehi) 229b64bbc39STejun Heo { 230b64bbc39STejun Heo ehi->desc[0] = '\0'; 231b64bbc39STejun Heo ehi->desc_len = 0; 232b64bbc39STejun Heo } 233a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_ehi_clear_desc); 234b64bbc39STejun Heo 235cbcdd875STejun Heo /** 236cbcdd875STejun Heo * ata_port_desc - append port description 237cbcdd875STejun Heo * @ap: target ATA port 238cbcdd875STejun Heo * @fmt: printf format string 239cbcdd875STejun Heo * 240cbcdd875STejun Heo * Format string according to @fmt and append it to port 241cbcdd875STejun Heo * description. If port description is not empty, " " is added 242cbcdd875STejun Heo * in-between. This function is to be used while initializing 243cbcdd875STejun Heo * ata_host. The description is printed on host registration. 244cbcdd875STejun Heo * 245cbcdd875STejun Heo * LOCKING: 246cbcdd875STejun Heo * None. 247cbcdd875STejun Heo */ 248cbcdd875STejun Heo void ata_port_desc(struct ata_port *ap, const char *fmt, ...) 249cbcdd875STejun Heo { 250cbcdd875STejun Heo va_list args; 251cbcdd875STejun Heo 252cbcdd875STejun Heo WARN_ON(!(ap->pflags & ATA_PFLAG_INITIALIZING)); 253cbcdd875STejun Heo 254cbcdd875STejun Heo if (ap->link.eh_info.desc_len) 255cbcdd875STejun Heo __ata_ehi_push_desc(&ap->link.eh_info, " "); 256cbcdd875STejun Heo 257cbcdd875STejun Heo va_start(args, fmt); 258cbcdd875STejun Heo __ata_ehi_pushv_desc(&ap->link.eh_info, fmt, args); 259cbcdd875STejun Heo va_end(args); 260cbcdd875STejun Heo } 261a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_port_desc); 262cbcdd875STejun Heo 263cbcdd875STejun Heo #ifdef CONFIG_PCI 264cbcdd875STejun Heo /** 265cbcdd875STejun Heo * ata_port_pbar_desc - append PCI BAR description 266cbcdd875STejun Heo * @ap: target ATA port 267cbcdd875STejun Heo * @bar: target PCI BAR 268cbcdd875STejun Heo * @offset: offset into PCI BAR 269cbcdd875STejun Heo * @name: name of the area 270cbcdd875STejun Heo * 271cbcdd875STejun Heo * If @offset is negative, this function formats a string which 272cbcdd875STejun Heo * contains the name, address, size and type of the BAR and 273cbcdd875STejun Heo * appends it to the port description. If @offset is zero or 274cbcdd875STejun Heo * positive, only name and offsetted address is appended. 275cbcdd875STejun Heo * 276cbcdd875STejun Heo * LOCKING: 277cbcdd875STejun Heo * None. 278cbcdd875STejun Heo */ 279cbcdd875STejun Heo void ata_port_pbar_desc(struct ata_port *ap, int bar, ssize_t offset, 280cbcdd875STejun Heo const char *name) 281cbcdd875STejun Heo { 282cbcdd875STejun Heo struct pci_dev *pdev = to_pci_dev(ap->host->dev); 283cbcdd875STejun Heo char *type = ""; 284cbcdd875STejun Heo unsigned long long start, len; 285cbcdd875STejun Heo 286cbcdd875STejun Heo if (pci_resource_flags(pdev, bar) & IORESOURCE_MEM) 287cbcdd875STejun Heo type = "m"; 288cbcdd875STejun Heo else if (pci_resource_flags(pdev, bar) & IORESOURCE_IO) 289cbcdd875STejun Heo type = "i"; 290cbcdd875STejun Heo 291cbcdd875STejun Heo start = (unsigned long long)pci_resource_start(pdev, bar); 292cbcdd875STejun Heo len = (unsigned long long)pci_resource_len(pdev, bar); 293cbcdd875STejun Heo 294cbcdd875STejun Heo if (offset < 0) 295cbcdd875STejun Heo ata_port_desc(ap, "%s %s%llu@0x%llx", name, type, len, start); 296cbcdd875STejun Heo else 297e6a73ab1SAndrew Morton ata_port_desc(ap, "%s 0x%llx", name, 298e6a73ab1SAndrew Morton start + (unsigned long long)offset); 299cbcdd875STejun Heo } 300a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_port_pbar_desc); 301cbcdd875STejun Heo #endif /* CONFIG_PCI */ 302cbcdd875STejun Heo 30387fbc5a0STejun Heo static int ata_lookup_timeout_table(u8 cmd) 30487fbc5a0STejun Heo { 30587fbc5a0STejun Heo int i; 30687fbc5a0STejun Heo 30787fbc5a0STejun Heo for (i = 0; i < ATA_EH_CMD_TIMEOUT_TABLE_SIZE; i++) { 30887fbc5a0STejun Heo const u8 *cur; 30987fbc5a0STejun Heo 31087fbc5a0STejun Heo for (cur = ata_eh_cmd_timeout_table[i].commands; *cur; cur++) 31187fbc5a0STejun Heo if (*cur == cmd) 31287fbc5a0STejun Heo return i; 31387fbc5a0STejun Heo } 31487fbc5a0STejun Heo 31587fbc5a0STejun Heo return -1; 31687fbc5a0STejun Heo } 31787fbc5a0STejun Heo 31887fbc5a0STejun Heo /** 31987fbc5a0STejun Heo * ata_internal_cmd_timeout - determine timeout for an internal command 32087fbc5a0STejun Heo * @dev: target device 32187fbc5a0STejun Heo * @cmd: internal command to be issued 32287fbc5a0STejun Heo * 32387fbc5a0STejun Heo * Determine timeout for internal command @cmd for @dev. 32487fbc5a0STejun Heo * 32587fbc5a0STejun Heo * LOCKING: 32687fbc5a0STejun Heo * EH context. 32787fbc5a0STejun Heo * 32887fbc5a0STejun Heo * RETURNS: 32987fbc5a0STejun Heo * Determined timeout. 33087fbc5a0STejun Heo */ 331e06233f9SSergey Shtylyov unsigned int ata_internal_cmd_timeout(struct ata_device *dev, u8 cmd) 33287fbc5a0STejun Heo { 33387fbc5a0STejun Heo struct ata_eh_context *ehc = &dev->link->eh_context; 33487fbc5a0STejun Heo int ent = ata_lookup_timeout_table(cmd); 33587fbc5a0STejun Heo int idx; 33687fbc5a0STejun Heo 33787fbc5a0STejun Heo if (ent < 0) 33887fbc5a0STejun Heo return ATA_EH_CMD_DFL_TIMEOUT; 33987fbc5a0STejun Heo 34087fbc5a0STejun Heo idx = ehc->cmd_timeout_idx[dev->devno][ent]; 34187fbc5a0STejun Heo return ata_eh_cmd_timeout_table[ent].timeouts[idx]; 34287fbc5a0STejun Heo } 34387fbc5a0STejun Heo 34487fbc5a0STejun Heo /** 34587fbc5a0STejun Heo * ata_internal_cmd_timed_out - notification for internal command timeout 34687fbc5a0STejun Heo * @dev: target device 34787fbc5a0STejun Heo * @cmd: internal command which timed out 34887fbc5a0STejun Heo * 34987fbc5a0STejun Heo * Notify EH that internal command @cmd for @dev timed out. This 35087fbc5a0STejun Heo * function should be called only for commands whose timeouts are 35187fbc5a0STejun Heo * determined using ata_internal_cmd_timeout(). 35287fbc5a0STejun Heo * 35387fbc5a0STejun Heo * LOCKING: 35487fbc5a0STejun Heo * EH context. 35587fbc5a0STejun Heo */ 35687fbc5a0STejun Heo void ata_internal_cmd_timed_out(struct ata_device *dev, u8 cmd) 35787fbc5a0STejun Heo { 35887fbc5a0STejun Heo struct ata_eh_context *ehc = &dev->link->eh_context; 35987fbc5a0STejun Heo int ent = ata_lookup_timeout_table(cmd); 36087fbc5a0STejun Heo int idx; 36187fbc5a0STejun Heo 36287fbc5a0STejun Heo if (ent < 0) 36387fbc5a0STejun Heo return; 36487fbc5a0STejun Heo 36587fbc5a0STejun Heo idx = ehc->cmd_timeout_idx[dev->devno][ent]; 366e06233f9SSergey Shtylyov if (ata_eh_cmd_timeout_table[ent].timeouts[idx + 1] != UINT_MAX) 36787fbc5a0STejun Heo ehc->cmd_timeout_idx[dev->devno][ent]++; 36887fbc5a0STejun Heo } 36987fbc5a0STejun Heo 3703884f7b0STejun Heo static void ata_ering_record(struct ata_ering *ering, unsigned int eflags, 371c6fd2807SJeff Garzik unsigned int err_mask) 372c6fd2807SJeff Garzik { 373c6fd2807SJeff Garzik struct ata_ering_entry *ent; 374c6fd2807SJeff Garzik 375c6fd2807SJeff Garzik WARN_ON(!err_mask); 376c6fd2807SJeff Garzik 377c6fd2807SJeff Garzik ering->cursor++; 378c6fd2807SJeff Garzik ering->cursor %= ATA_ERING_SIZE; 379c6fd2807SJeff Garzik 380c6fd2807SJeff Garzik ent = &ering->ring[ering->cursor]; 3813884f7b0STejun Heo ent->eflags = eflags; 382c6fd2807SJeff Garzik ent->err_mask = err_mask; 383c6fd2807SJeff Garzik ent->timestamp = get_jiffies_64(); 384c6fd2807SJeff Garzik } 385c6fd2807SJeff Garzik 38676326ac1STejun Heo static struct ata_ering_entry *ata_ering_top(struct ata_ering *ering) 38776326ac1STejun Heo { 38876326ac1STejun Heo struct ata_ering_entry *ent = &ering->ring[ering->cursor]; 38976326ac1STejun Heo 39076326ac1STejun Heo if (ent->err_mask) 39176326ac1STejun Heo return ent; 39276326ac1STejun Heo return NULL; 39376326ac1STejun Heo } 39476326ac1STejun Heo 395d9027470SGwendal Grignou int ata_ering_map(struct ata_ering *ering, 396c6fd2807SJeff Garzik int (*map_fn)(struct ata_ering_entry *, void *), 397c6fd2807SJeff Garzik void *arg) 398c6fd2807SJeff Garzik { 399c6fd2807SJeff Garzik int idx, rc = 0; 400c6fd2807SJeff Garzik struct ata_ering_entry *ent; 401c6fd2807SJeff Garzik 402c6fd2807SJeff Garzik idx = ering->cursor; 403c6fd2807SJeff Garzik do { 404c6fd2807SJeff Garzik ent = &ering->ring[idx]; 405c6fd2807SJeff Garzik if (!ent->err_mask) 406c6fd2807SJeff Garzik break; 407c6fd2807SJeff Garzik rc = map_fn(ent, arg); 408c6fd2807SJeff Garzik if (rc) 409c6fd2807SJeff Garzik break; 410c6fd2807SJeff Garzik idx = (idx - 1 + ATA_ERING_SIZE) % ATA_ERING_SIZE; 411c6fd2807SJeff Garzik } while (idx != ering->cursor); 412c6fd2807SJeff Garzik 413c6fd2807SJeff Garzik return rc; 414c6fd2807SJeff Garzik } 415c6fd2807SJeff Garzik 41660428407SH Hartley Sweeten static int ata_ering_clear_cb(struct ata_ering_entry *ent, void *void_arg) 417d9027470SGwendal Grignou { 418d9027470SGwendal Grignou ent->eflags |= ATA_EFLAG_OLD_ER; 419d9027470SGwendal Grignou return 0; 420d9027470SGwendal Grignou } 421d9027470SGwendal Grignou 422d9027470SGwendal Grignou static void ata_ering_clear(struct ata_ering *ering) 423d9027470SGwendal Grignou { 424d9027470SGwendal Grignou ata_ering_map(ering, ata_ering_clear_cb, NULL); 425d9027470SGwendal Grignou } 426d9027470SGwendal Grignou 427c6fd2807SJeff Garzik static unsigned int ata_eh_dev_action(struct ata_device *dev) 428c6fd2807SJeff Garzik { 4299af5c9c9STejun Heo struct ata_eh_context *ehc = &dev->link->eh_context; 430c6fd2807SJeff Garzik 431c6fd2807SJeff Garzik return ehc->i.action | ehc->i.dev_action[dev->devno]; 432c6fd2807SJeff Garzik } 433c6fd2807SJeff Garzik 434f58229f8STejun Heo static void ata_eh_clear_action(struct ata_link *link, struct ata_device *dev, 435c6fd2807SJeff Garzik struct ata_eh_info *ehi, unsigned int action) 436c6fd2807SJeff Garzik { 437f58229f8STejun Heo struct ata_device *tdev; 438c6fd2807SJeff Garzik 439c6fd2807SJeff Garzik if (!dev) { 440c6fd2807SJeff Garzik ehi->action &= ~action; 4411eca4365STejun Heo ata_for_each_dev(tdev, link, ALL) 442f58229f8STejun Heo ehi->dev_action[tdev->devno] &= ~action; 443c6fd2807SJeff Garzik } else { 444c6fd2807SJeff Garzik /* doesn't make sense for port-wide EH actions */ 445c6fd2807SJeff Garzik WARN_ON(!(action & ATA_EH_PERDEV_MASK)); 446c6fd2807SJeff Garzik 447c6fd2807SJeff Garzik /* break ehi->action into ehi->dev_action */ 448c6fd2807SJeff Garzik if (ehi->action & action) { 4491eca4365STejun Heo ata_for_each_dev(tdev, link, ALL) 450f58229f8STejun Heo ehi->dev_action[tdev->devno] |= 451f58229f8STejun Heo ehi->action & action; 452c6fd2807SJeff Garzik ehi->action &= ~action; 453c6fd2807SJeff Garzik } 454c6fd2807SJeff Garzik 455c6fd2807SJeff Garzik /* turn off the specified per-dev action */ 456c6fd2807SJeff Garzik ehi->dev_action[dev->devno] &= ~action; 457c6fd2807SJeff Garzik } 458c6fd2807SJeff Garzik } 459c6fd2807SJeff Garzik 460c6fd2807SJeff Garzik /** 461c0c362b6STejun Heo * ata_eh_acquire - acquire EH ownership 462c0c362b6STejun Heo * @ap: ATA port to acquire EH ownership for 463c0c362b6STejun Heo * 464c0c362b6STejun Heo * Acquire EH ownership for @ap. This is the basic exclusion 465c0c362b6STejun Heo * mechanism for ports sharing a host. Only one port hanging off 466c0c362b6STejun Heo * the same host can claim the ownership of EH. 467c0c362b6STejun Heo * 468c0c362b6STejun Heo * LOCKING: 469c0c362b6STejun Heo * EH context. 470c0c362b6STejun Heo */ 471c0c362b6STejun Heo void ata_eh_acquire(struct ata_port *ap) 472c0c362b6STejun Heo { 473c0c362b6STejun Heo mutex_lock(&ap->host->eh_mutex); 474c0c362b6STejun Heo WARN_ON_ONCE(ap->host->eh_owner); 475c0c362b6STejun Heo ap->host->eh_owner = current; 476c0c362b6STejun Heo } 477c0c362b6STejun Heo 478c0c362b6STejun Heo /** 479c0c362b6STejun Heo * ata_eh_release - release EH ownership 480c0c362b6STejun Heo * @ap: ATA port to release EH ownership for 481c0c362b6STejun Heo * 482c0c362b6STejun Heo * Release EH ownership for @ap if the caller. The caller must 483c0c362b6STejun Heo * have acquired EH ownership using ata_eh_acquire() previously. 484c0c362b6STejun Heo * 485c0c362b6STejun Heo * LOCKING: 486c0c362b6STejun Heo * EH context. 487c0c362b6STejun Heo */ 488c0c362b6STejun Heo void ata_eh_release(struct ata_port *ap) 489c0c362b6STejun Heo { 490c0c362b6STejun Heo WARN_ON_ONCE(ap->host->eh_owner != current); 491c0c362b6STejun Heo ap->host->eh_owner = NULL; 492c0c362b6STejun Heo mutex_unlock(&ap->host->eh_mutex); 493c0c362b6STejun Heo } 494c0c362b6STejun Heo 495ece180d1STejun Heo static void ata_eh_unload(struct ata_port *ap) 496ece180d1STejun Heo { 497ece180d1STejun Heo struct ata_link *link; 498ece180d1STejun Heo struct ata_device *dev; 499ece180d1STejun Heo unsigned long flags; 500ece180d1STejun Heo 501ece180d1STejun Heo /* Restore SControl IPM and SPD for the next driver and 502ece180d1STejun Heo * disable attached devices. 503ece180d1STejun Heo */ 504ece180d1STejun Heo ata_for_each_link(link, ap, PMP_FIRST) { 505ece180d1STejun Heo sata_scr_write(link, SCR_CONTROL, link->saved_scontrol & 0xff0); 506ece180d1STejun Heo ata_for_each_dev(dev, link, ALL) 507ece180d1STejun Heo ata_dev_disable(dev); 508ece180d1STejun Heo } 509ece180d1STejun Heo 510ece180d1STejun Heo /* freeze and set UNLOADED */ 511ece180d1STejun Heo spin_lock_irqsave(ap->lock, flags); 512ece180d1STejun Heo 513ece180d1STejun Heo ata_port_freeze(ap); /* won't be thawed */ 514ece180d1STejun Heo ap->pflags &= ~ATA_PFLAG_EH_PENDING; /* clear pending from freeze */ 515ece180d1STejun Heo ap->pflags |= ATA_PFLAG_UNLOADED; 516ece180d1STejun Heo 517ece180d1STejun Heo spin_unlock_irqrestore(ap->lock, flags); 518ece180d1STejun Heo } 519ece180d1STejun Heo 520c6fd2807SJeff Garzik /** 521c6fd2807SJeff Garzik * ata_scsi_error - SCSI layer error handler callback 522c6fd2807SJeff Garzik * @host: SCSI host on which error occurred 523c6fd2807SJeff Garzik * 524c6fd2807SJeff Garzik * Handles SCSI-layer-thrown error events. 525c6fd2807SJeff Garzik * 526c6fd2807SJeff Garzik * LOCKING: 527c6fd2807SJeff Garzik * Inherited from SCSI layer (none, can sleep) 528c6fd2807SJeff Garzik * 529c6fd2807SJeff Garzik * RETURNS: 530c6fd2807SJeff Garzik * Zero. 531c6fd2807SJeff Garzik */ 532c6fd2807SJeff Garzik void ata_scsi_error(struct Scsi_Host *host) 533c6fd2807SJeff Garzik { 534c6fd2807SJeff Garzik struct ata_port *ap = ata_shost_to_port(host); 535c6fd2807SJeff Garzik unsigned long flags; 536c34aeebcSJames Bottomley LIST_HEAD(eh_work_q); 537c6fd2807SJeff Garzik 538c34aeebcSJames Bottomley spin_lock_irqsave(host->host_lock, flags); 539c34aeebcSJames Bottomley list_splice_init(&host->eh_cmd_q, &eh_work_q); 540c34aeebcSJames Bottomley spin_unlock_irqrestore(host->host_lock, flags); 541c34aeebcSJames Bottomley 5420e0b494cSJames Bottomley ata_scsi_cmd_error_handler(host, ap, &eh_work_q); 5430e0b494cSJames Bottomley 5440e0b494cSJames Bottomley /* If we timed raced normal completion and there is nothing to 5450e0b494cSJames Bottomley recover nr_timedout == 0 why exactly are we doing error recovery ? */ 5460e0b494cSJames Bottomley ata_scsi_port_error_handler(host, ap); 5470e0b494cSJames Bottomley 5480e0b494cSJames Bottomley /* finish or retry handled scmd's and clean up */ 54972d8c36eSWei Fang WARN_ON(!list_empty(&eh_work_q)); 5500e0b494cSJames Bottomley 5510e0b494cSJames Bottomley } 5520e0b494cSJames Bottomley 5530e0b494cSJames Bottomley /** 5540e0b494cSJames Bottomley * ata_scsi_cmd_error_handler - error callback for a list of commands 5550e0b494cSJames Bottomley * @host: scsi host containing the port 5560e0b494cSJames Bottomley * @ap: ATA port within the host 5570e0b494cSJames Bottomley * @eh_work_q: list of commands to process 5580e0b494cSJames Bottomley * 5590e0b494cSJames Bottomley * process the given list of commands and return those finished to the 5600e0b494cSJames Bottomley * ap->eh_done_q. This function is the first part of the libata error 5610e0b494cSJames Bottomley * handler which processes a given list of failed commands. 5620e0b494cSJames Bottomley */ 5630e0b494cSJames Bottomley void ata_scsi_cmd_error_handler(struct Scsi_Host *host, struct ata_port *ap, 5640e0b494cSJames Bottomley struct list_head *eh_work_q) 5650e0b494cSJames Bottomley { 5660e0b494cSJames Bottomley int i; 5670e0b494cSJames Bottomley unsigned long flags; 568b83ad9eeSWenchao Hao struct scsi_cmnd *scmd, *tmp; 569b83ad9eeSWenchao Hao int nr_timedout = 0; 5700e0b494cSJames Bottomley 571c429137aSTejun Heo /* make sure sff pio task is not running */ 572c429137aSTejun Heo ata_sff_flush_pio_task(ap); 573c6fd2807SJeff Garzik 574b83ad9eeSWenchao Hao if (!ap->ops->error_handler) 575b83ad9eeSWenchao Hao return; 576b83ad9eeSWenchao Hao 577cca3974eSJeff Garzik /* synchronize with host lock and sort out timeouts */ 578c6fd2807SJeff Garzik 579b83ad9eeSWenchao Hao /* 580b83ad9eeSWenchao Hao * For new EH, all qcs are finished in one of three ways - 581c6fd2807SJeff Garzik * normal completion, error completion, and SCSI timeout. 582c96f1732SAlan Cox * Both completions can race against SCSI timeout. When normal 583c6fd2807SJeff Garzik * completion wins, the qc never reaches EH. When error 58487629312SNiklas Cassel * completion wins, the qc has ATA_QCFLAG_EH set. 585c6fd2807SJeff Garzik * 586c6fd2807SJeff Garzik * When SCSI timeout wins, things are a bit more complex. 587c6fd2807SJeff Garzik * Normal or error completion can occur after the timeout but 588c6fd2807SJeff Garzik * before this point. In such cases, both types of 589c6fd2807SJeff Garzik * completions are honored. A scmd is determined to have 590c6fd2807SJeff Garzik * timed out iff its associated qc is active and not failed. 591c6fd2807SJeff Garzik */ 592a4f08141SPaul E. McKenney spin_lock_irqsave(ap->lock, flags); 593c6fd2807SJeff Garzik 594b83ad9eeSWenchao Hao /* 595b83ad9eeSWenchao Hao * This must occur under the ap->lock as we don't want 596b83ad9eeSWenchao Hao * a polled recovery to race the real interrupt handler 597b83ad9eeSWenchao Hao * 598b83ad9eeSWenchao Hao * The lost_interrupt handler checks for any completed but 599b83ad9eeSWenchao Hao * non-notified command and completes much like an IRQ handler. 600b83ad9eeSWenchao Hao * 601b83ad9eeSWenchao Hao * We then fall into the error recovery code which will treat 602b83ad9eeSWenchao Hao * this as if normal completion won the race 603b83ad9eeSWenchao Hao */ 604c96f1732SAlan Cox if (ap->ops->lost_interrupt) 605c96f1732SAlan Cox ap->ops->lost_interrupt(ap); 606c96f1732SAlan Cox 6070e0b494cSJames Bottomley list_for_each_entry_safe(scmd, tmp, eh_work_q, eh_entry) { 608c6fd2807SJeff Garzik struct ata_queued_cmd *qc; 609c6fd2807SJeff Garzik 610258c4e5cSJens Axboe ata_qc_for_each_raw(ap, qc, i) { 611c6fd2807SJeff Garzik if (qc->flags & ATA_QCFLAG_ACTIVE && 612c6fd2807SJeff Garzik qc->scsicmd == scmd) 613c6fd2807SJeff Garzik break; 614c6fd2807SJeff Garzik } 615c6fd2807SJeff Garzik 616c6fd2807SJeff Garzik if (i < ATA_MAX_QUEUE) { 617c6fd2807SJeff Garzik /* the scmd has an associated qc */ 61887629312SNiklas Cassel if (!(qc->flags & ATA_QCFLAG_EH)) { 619c6fd2807SJeff Garzik /* which hasn't failed yet, timeout */ 620c6fd2807SJeff Garzik qc->err_mask |= AC_ERR_TIMEOUT; 62187629312SNiklas Cassel qc->flags |= ATA_QCFLAG_EH; 622c6fd2807SJeff Garzik nr_timedout++; 623c6fd2807SJeff Garzik } 624c6fd2807SJeff Garzik } else { 625c6fd2807SJeff Garzik /* Normal completion occurred after 626c6fd2807SJeff Garzik * SCSI timeout but before this point. 627c6fd2807SJeff Garzik * Successfully complete it. 628c6fd2807SJeff Garzik */ 629c6fd2807SJeff Garzik scmd->retries = scmd->allowed; 630c6fd2807SJeff Garzik scsi_eh_finish_cmd(scmd, &ap->eh_done_q); 631c6fd2807SJeff Garzik } 632c6fd2807SJeff Garzik } 633c6fd2807SJeff Garzik 634b83ad9eeSWenchao Hao /* 635b83ad9eeSWenchao Hao * If we have timed out qcs. They belong to EH from 636c6fd2807SJeff Garzik * this point but the state of the controller is 637c6fd2807SJeff Garzik * unknown. Freeze the port to make sure the IRQ 638c6fd2807SJeff Garzik * handler doesn't diddle with those qcs. This must 63987629312SNiklas Cassel * be done atomically w.r.t. setting ATA_QCFLAG_EH. 640c6fd2807SJeff Garzik */ 641c6fd2807SJeff Garzik if (nr_timedout) 642c6fd2807SJeff Garzik __ata_port_freeze(ap); 643c6fd2807SJeff Garzik 644a1e10f7eSTejun Heo /* initialize eh_tries */ 645a1e10f7eSTejun Heo ap->eh_tries = ATA_EH_MAX_TRIES; 646c6fd2807SJeff Garzik 647b83ad9eeSWenchao Hao spin_unlock_irqrestore(ap->lock, flags); 6480e0b494cSJames Bottomley } 6490e0b494cSJames Bottomley EXPORT_SYMBOL(ata_scsi_cmd_error_handler); 6500e0b494cSJames Bottomley 6510e0b494cSJames Bottomley /** 6520e0b494cSJames Bottomley * ata_scsi_port_error_handler - recover the port after the commands 6530e0b494cSJames Bottomley * @host: SCSI host containing the port 6540e0b494cSJames Bottomley * @ap: the ATA port 6550e0b494cSJames Bottomley * 6560e0b494cSJames Bottomley * Handle the recovery of the port @ap after all the commands 6570e0b494cSJames Bottomley * have been recovered. 6580e0b494cSJames Bottomley */ 6590e0b494cSJames Bottomley void ata_scsi_port_error_handler(struct Scsi_Host *host, struct ata_port *ap) 6600e0b494cSJames Bottomley { 6610e0b494cSJames Bottomley unsigned long flags; 662c96f1732SAlan Cox 663c6fd2807SJeff Garzik /* invoke error handler */ 664c6fd2807SJeff Garzik if (ap->ops->error_handler) { 665cf1b86c8STejun Heo struct ata_link *link; 666cf1b86c8STejun Heo 667c0c362b6STejun Heo /* acquire EH ownership */ 668c0c362b6STejun Heo ata_eh_acquire(ap); 669c0c362b6STejun Heo repeat: 6705ddf24c5STejun Heo /* kill fast drain timer */ 6715ddf24c5STejun Heo del_timer_sync(&ap->fastdrain_timer); 6725ddf24c5STejun Heo 673c6fd2807SJeff Garzik /* process port resume request */ 674c6fd2807SJeff Garzik ata_eh_handle_port_resume(ap); 675c6fd2807SJeff Garzik 676c6fd2807SJeff Garzik /* fetch & clear EH info */ 677c6fd2807SJeff Garzik spin_lock_irqsave(ap->lock, flags); 678c6fd2807SJeff Garzik 6791eca4365STejun Heo ata_for_each_link(link, ap, HOST_FIRST) { 68000115e0fSTejun Heo struct ata_eh_context *ehc = &link->eh_context; 68100115e0fSTejun Heo struct ata_device *dev; 68200115e0fSTejun Heo 683cf1b86c8STejun Heo memset(&link->eh_context, 0, sizeof(link->eh_context)); 684cf1b86c8STejun Heo link->eh_context.i = link->eh_info; 685cf1b86c8STejun Heo memset(&link->eh_info, 0, sizeof(link->eh_info)); 68600115e0fSTejun Heo 6871eca4365STejun Heo ata_for_each_dev(dev, link, ENABLED) { 68800115e0fSTejun Heo int devno = dev->devno; 68900115e0fSTejun Heo 69000115e0fSTejun Heo ehc->saved_xfer_mode[devno] = dev->xfer_mode; 69100115e0fSTejun Heo if (ata_ncq_enabled(dev)) 69200115e0fSTejun Heo ehc->saved_ncq_enabled |= 1 << devno; 69300115e0fSTejun Heo } 694cf1b86c8STejun Heo } 695c6fd2807SJeff Garzik 696c6fd2807SJeff Garzik ap->pflags |= ATA_PFLAG_EH_IN_PROGRESS; 697c6fd2807SJeff Garzik ap->pflags &= ~ATA_PFLAG_EH_PENDING; 698da917d69STejun Heo ap->excl_link = NULL; /* don't maintain exclusion over EH */ 699c6fd2807SJeff Garzik 700c6fd2807SJeff Garzik spin_unlock_irqrestore(ap->lock, flags); 701c6fd2807SJeff Garzik 702c6fd2807SJeff Garzik /* invoke EH, skip if unloading or suspended */ 703c6fd2807SJeff Garzik if (!(ap->pflags & (ATA_PFLAG_UNLOADING | ATA_PFLAG_SUSPENDED))) 704c6fd2807SJeff Garzik ap->ops->error_handler(ap); 705ece180d1STejun Heo else { 706ece180d1STejun Heo /* if unloading, commence suicide */ 707ece180d1STejun Heo if ((ap->pflags & ATA_PFLAG_UNLOADING) && 708ece180d1STejun Heo !(ap->pflags & ATA_PFLAG_UNLOADED)) 709ece180d1STejun Heo ata_eh_unload(ap); 710c6fd2807SJeff Garzik ata_eh_finish(ap); 711ece180d1STejun Heo } 712c6fd2807SJeff Garzik 713c6fd2807SJeff Garzik /* process port suspend request */ 714c6fd2807SJeff Garzik ata_eh_handle_port_suspend(ap); 715c6fd2807SJeff Garzik 71625985edcSLucas De Marchi /* Exception might have happened after ->error_handler 717c6fd2807SJeff Garzik * recovered the port but before this point. Repeat 718c6fd2807SJeff Garzik * EH in such case. 719c6fd2807SJeff Garzik */ 720c6fd2807SJeff Garzik spin_lock_irqsave(ap->lock, flags); 721c6fd2807SJeff Garzik 722c6fd2807SJeff Garzik if (ap->pflags & ATA_PFLAG_EH_PENDING) { 723a1e10f7eSTejun Heo if (--ap->eh_tries) { 724c6fd2807SJeff Garzik spin_unlock_irqrestore(ap->lock, flags); 725c6fd2807SJeff Garzik goto repeat; 726c6fd2807SJeff Garzik } 727a9a79dfeSJoe Perches ata_port_err(ap, 728a9a79dfeSJoe Perches "EH pending after %d tries, giving up\n", 729a9a79dfeSJoe Perches ATA_EH_MAX_TRIES); 730914616a3STejun Heo ap->pflags &= ~ATA_PFLAG_EH_PENDING; 731c6fd2807SJeff Garzik } 732c6fd2807SJeff Garzik 733c6fd2807SJeff Garzik /* this run is complete, make sure EH info is clear */ 7341eca4365STejun Heo ata_for_each_link(link, ap, HOST_FIRST) 735cf1b86c8STejun Heo memset(&link->eh_info, 0, sizeof(link->eh_info)); 736c6fd2807SJeff Garzik 737e4a9c373SDan Williams /* end eh (clear host_eh_scheduled) while holding 738e4a9c373SDan Williams * ap->lock such that if exception occurs after this 739e4a9c373SDan Williams * point but before EH completion, SCSI midlayer will 740c6fd2807SJeff Garzik * re-initiate EH. 741c6fd2807SJeff Garzik */ 742e4a9c373SDan Williams ap->ops->end_eh(ap); 743c6fd2807SJeff Garzik 744c6fd2807SJeff Garzik spin_unlock_irqrestore(ap->lock, flags); 745c0c362b6STejun Heo ata_eh_release(ap); 746c6fd2807SJeff Garzik } else { 7479af5c9c9STejun Heo WARN_ON(ata_qc_from_tag(ap, ap->link.active_tag) == NULL); 748c6fd2807SJeff Garzik ap->ops->eng_timeout(ap); 749c6fd2807SJeff Garzik } 750c6fd2807SJeff Garzik 751c6fd2807SJeff Garzik scsi_eh_flush_done_q(&ap->eh_done_q); 752c6fd2807SJeff Garzik 753c6fd2807SJeff Garzik /* clean up */ 754c6fd2807SJeff Garzik spin_lock_irqsave(ap->lock, flags); 755c6fd2807SJeff Garzik 756c6fd2807SJeff Garzik if (ap->pflags & ATA_PFLAG_LOADING) 757c6fd2807SJeff Garzik ap->pflags &= ~ATA_PFLAG_LOADING; 7586f54120eSJason Yan else if ((ap->pflags & ATA_PFLAG_SCSI_HOTPLUG) && 7596f54120eSJason Yan !(ap->flags & ATA_FLAG_SAS_HOST)) 760ad72cf98STejun Heo schedule_delayed_work(&ap->hotplug_task, 0); 761c6fd2807SJeff Garzik 762c6fd2807SJeff Garzik if (ap->pflags & ATA_PFLAG_RECOVERED) 763a9a79dfeSJoe Perches ata_port_info(ap, "EH complete\n"); 764c6fd2807SJeff Garzik 765c6fd2807SJeff Garzik ap->pflags &= ~(ATA_PFLAG_SCSI_HOTPLUG | ATA_PFLAG_RECOVERED); 766c6fd2807SJeff Garzik 767c6fd2807SJeff Garzik /* tell wait_eh that we're done */ 768c6fd2807SJeff Garzik ap->pflags &= ~ATA_PFLAG_EH_IN_PROGRESS; 769c6fd2807SJeff Garzik wake_up_all(&ap->eh_wait_q); 770c6fd2807SJeff Garzik 771c6fd2807SJeff Garzik spin_unlock_irqrestore(ap->lock, flags); 772c6fd2807SJeff Garzik } 7730e0b494cSJames Bottomley EXPORT_SYMBOL_GPL(ata_scsi_port_error_handler); 774c6fd2807SJeff Garzik 775c6fd2807SJeff Garzik /** 776c6fd2807SJeff Garzik * ata_port_wait_eh - Wait for the currently pending EH to complete 777c6fd2807SJeff Garzik * @ap: Port to wait EH for 778c6fd2807SJeff Garzik * 779c6fd2807SJeff Garzik * Wait until the currently pending EH is complete. 780c6fd2807SJeff Garzik * 781c6fd2807SJeff Garzik * LOCKING: 782c6fd2807SJeff Garzik * Kernel thread context (may sleep). 783c6fd2807SJeff Garzik */ 784c6fd2807SJeff Garzik void ata_port_wait_eh(struct ata_port *ap) 785c6fd2807SJeff Garzik { 786c6fd2807SJeff Garzik unsigned long flags; 787c6fd2807SJeff Garzik DEFINE_WAIT(wait); 788c6fd2807SJeff Garzik 789c6fd2807SJeff Garzik retry: 790c6fd2807SJeff Garzik spin_lock_irqsave(ap->lock, flags); 791c6fd2807SJeff Garzik 792c6fd2807SJeff Garzik while (ap->pflags & (ATA_PFLAG_EH_PENDING | ATA_PFLAG_EH_IN_PROGRESS)) { 793c6fd2807SJeff Garzik prepare_to_wait(&ap->eh_wait_q, &wait, TASK_UNINTERRUPTIBLE); 794c6fd2807SJeff Garzik spin_unlock_irqrestore(ap->lock, flags); 795c6fd2807SJeff Garzik schedule(); 796c6fd2807SJeff Garzik spin_lock_irqsave(ap->lock, flags); 797c6fd2807SJeff Garzik } 798c6fd2807SJeff Garzik finish_wait(&ap->eh_wait_q, &wait); 799c6fd2807SJeff Garzik 800c6fd2807SJeff Garzik spin_unlock_irqrestore(ap->lock, flags); 801c6fd2807SJeff Garzik 802c6fd2807SJeff Garzik /* make sure SCSI EH is complete */ 803cca3974eSJeff Garzik if (scsi_host_in_recovery(ap->scsi_host)) { 80497750cebSTejun Heo ata_msleep(ap, 10); 805c6fd2807SJeff Garzik goto retry; 806c6fd2807SJeff Garzik } 807c6fd2807SJeff Garzik } 80881c757bcSDan Williams EXPORT_SYMBOL_GPL(ata_port_wait_eh); 809c6fd2807SJeff Garzik 810afae461aSSergey Shtylyov static unsigned int ata_eh_nr_in_flight(struct ata_port *ap) 8115ddf24c5STejun Heo { 812258c4e5cSJens Axboe struct ata_queued_cmd *qc; 8135ddf24c5STejun Heo unsigned int tag; 814afae461aSSergey Shtylyov unsigned int nr = 0; 8155ddf24c5STejun Heo 8165ddf24c5STejun Heo /* count only non-internal commands */ 817258c4e5cSJens Axboe ata_qc_for_each(ap, qc, tag) { 818258c4e5cSJens Axboe if (qc) 8195ddf24c5STejun Heo nr++; 8209d207accSJens Axboe } 8215ddf24c5STejun Heo 8225ddf24c5STejun Heo return nr; 8235ddf24c5STejun Heo } 8245ddf24c5STejun Heo 825b93ab338SKees Cook void ata_eh_fastdrain_timerfn(struct timer_list *t) 8265ddf24c5STejun Heo { 827b93ab338SKees Cook struct ata_port *ap = from_timer(ap, t, fastdrain_timer); 8285ddf24c5STejun Heo unsigned long flags; 829afae461aSSergey Shtylyov unsigned int cnt; 8305ddf24c5STejun Heo 8315ddf24c5STejun Heo spin_lock_irqsave(ap->lock, flags); 8325ddf24c5STejun Heo 8335ddf24c5STejun Heo cnt = ata_eh_nr_in_flight(ap); 8345ddf24c5STejun Heo 8355ddf24c5STejun Heo /* are we done? */ 8365ddf24c5STejun Heo if (!cnt) 8375ddf24c5STejun Heo goto out_unlock; 8385ddf24c5STejun Heo 8395ddf24c5STejun Heo if (cnt == ap->fastdrain_cnt) { 840258c4e5cSJens Axboe struct ata_queued_cmd *qc; 8415ddf24c5STejun Heo unsigned int tag; 8425ddf24c5STejun Heo 8435ddf24c5STejun Heo /* No progress during the last interval, tag all 8445ddf24c5STejun Heo * in-flight qcs as timed out and freeze the port. 8455ddf24c5STejun Heo */ 846258c4e5cSJens Axboe ata_qc_for_each(ap, qc, tag) { 8475ddf24c5STejun Heo if (qc) 8485ddf24c5STejun Heo qc->err_mask |= AC_ERR_TIMEOUT; 8495ddf24c5STejun Heo } 8505ddf24c5STejun Heo 8515ddf24c5STejun Heo ata_port_freeze(ap); 8525ddf24c5STejun Heo } else { 8535ddf24c5STejun Heo /* some qcs have finished, give it another chance */ 8545ddf24c5STejun Heo ap->fastdrain_cnt = cnt; 8555ddf24c5STejun Heo ap->fastdrain_timer.expires = 856341c2c95STejun Heo ata_deadline(jiffies, ATA_EH_FASTDRAIN_INTERVAL); 8575ddf24c5STejun Heo add_timer(&ap->fastdrain_timer); 8585ddf24c5STejun Heo } 8595ddf24c5STejun Heo 8605ddf24c5STejun Heo out_unlock: 8615ddf24c5STejun Heo spin_unlock_irqrestore(ap->lock, flags); 8625ddf24c5STejun Heo } 8635ddf24c5STejun Heo 8645ddf24c5STejun Heo /** 8655ddf24c5STejun Heo * ata_eh_set_pending - set ATA_PFLAG_EH_PENDING and activate fast drain 8665ddf24c5STejun Heo * @ap: target ATA port 8675ddf24c5STejun Heo * @fastdrain: activate fast drain 8685ddf24c5STejun Heo * 8695ddf24c5STejun Heo * Set ATA_PFLAG_EH_PENDING and activate fast drain if @fastdrain 8705ddf24c5STejun Heo * is non-zero and EH wasn't pending before. Fast drain ensures 8715ddf24c5STejun Heo * that EH kicks in in timely manner. 8725ddf24c5STejun Heo * 8735ddf24c5STejun Heo * LOCKING: 8745ddf24c5STejun Heo * spin_lock_irqsave(host lock) 8755ddf24c5STejun Heo */ 8765ddf24c5STejun Heo static void ata_eh_set_pending(struct ata_port *ap, int fastdrain) 8775ddf24c5STejun Heo { 878afae461aSSergey Shtylyov unsigned int cnt; 8795ddf24c5STejun Heo 8805ddf24c5STejun Heo /* already scheduled? */ 8815ddf24c5STejun Heo if (ap->pflags & ATA_PFLAG_EH_PENDING) 8825ddf24c5STejun Heo return; 8835ddf24c5STejun Heo 8845ddf24c5STejun Heo ap->pflags |= ATA_PFLAG_EH_PENDING; 8855ddf24c5STejun Heo 8865ddf24c5STejun Heo if (!fastdrain) 8875ddf24c5STejun Heo return; 8885ddf24c5STejun Heo 8895ddf24c5STejun Heo /* do we have in-flight qcs? */ 8905ddf24c5STejun Heo cnt = ata_eh_nr_in_flight(ap); 8915ddf24c5STejun Heo if (!cnt) 8925ddf24c5STejun Heo return; 8935ddf24c5STejun Heo 8945ddf24c5STejun Heo /* activate fast drain */ 8955ddf24c5STejun Heo ap->fastdrain_cnt = cnt; 896341c2c95STejun Heo ap->fastdrain_timer.expires = 897341c2c95STejun Heo ata_deadline(jiffies, ATA_EH_FASTDRAIN_INTERVAL); 8985ddf24c5STejun Heo add_timer(&ap->fastdrain_timer); 8995ddf24c5STejun Heo } 9005ddf24c5STejun Heo 901c6fd2807SJeff Garzik /** 902c6fd2807SJeff Garzik * ata_qc_schedule_eh - schedule qc for error handling 903c6fd2807SJeff Garzik * @qc: command to schedule error handling for 904c6fd2807SJeff Garzik * 905c6fd2807SJeff Garzik * Schedule error handling for @qc. EH will kick in as soon as 906c6fd2807SJeff Garzik * other commands are drained. 907c6fd2807SJeff Garzik * 908c6fd2807SJeff Garzik * LOCKING: 909cca3974eSJeff Garzik * spin_lock_irqsave(host lock) 910c6fd2807SJeff Garzik */ 911c6fd2807SJeff Garzik void ata_qc_schedule_eh(struct ata_queued_cmd *qc) 912c6fd2807SJeff Garzik { 913c6fd2807SJeff Garzik struct ata_port *ap = qc->ap; 914c6fd2807SJeff Garzik 915c6fd2807SJeff Garzik WARN_ON(!ap->ops->error_handler); 916c6fd2807SJeff Garzik 91787629312SNiklas Cassel qc->flags |= ATA_QCFLAG_EH; 9185ddf24c5STejun Heo ata_eh_set_pending(ap, 1); 919c6fd2807SJeff Garzik 920c6fd2807SJeff Garzik /* The following will fail if timeout has already expired. 921c6fd2807SJeff Garzik * ata_scsi_error() takes care of such scmds on EH entry. 92287629312SNiklas Cassel * Note that ATA_QCFLAG_EH is unconditionally set after 923c6fd2807SJeff Garzik * this function completes. 924c6fd2807SJeff Garzik */ 925c8329cd5SBart Van Assche blk_abort_request(scsi_cmd_to_rq(qc->scsicmd)); 926c6fd2807SJeff Garzik } 927c6fd2807SJeff Garzik 928c6fd2807SJeff Garzik /** 929e4a9c373SDan Williams * ata_std_sched_eh - non-libsas ata_ports issue eh with this common routine 930e4a9c373SDan Williams * @ap: ATA port to schedule EH for 931e4a9c373SDan Williams * 932e4a9c373SDan Williams * LOCKING: inherited from ata_port_schedule_eh 933e4a9c373SDan Williams * spin_lock_irqsave(host lock) 934e4a9c373SDan Williams */ 935e4a9c373SDan Williams void ata_std_sched_eh(struct ata_port *ap) 936e4a9c373SDan Williams { 937e4a9c373SDan Williams WARN_ON(!ap->ops->error_handler); 938e4a9c373SDan Williams 939e4a9c373SDan Williams if (ap->pflags & ATA_PFLAG_INITIALIZING) 940e4a9c373SDan Williams return; 941e4a9c373SDan Williams 942e4a9c373SDan Williams ata_eh_set_pending(ap, 1); 943e4a9c373SDan Williams scsi_schedule_eh(ap->scsi_host); 944e4a9c373SDan Williams 945c318458cSHannes Reinecke trace_ata_std_sched_eh(ap); 946e4a9c373SDan Williams } 947e4a9c373SDan Williams EXPORT_SYMBOL_GPL(ata_std_sched_eh); 948e4a9c373SDan Williams 949e4a9c373SDan Williams /** 950e4a9c373SDan Williams * ata_std_end_eh - non-libsas ata_ports complete eh with this common routine 951e4a9c373SDan Williams * @ap: ATA port to end EH for 952e4a9c373SDan Williams * 953e4a9c373SDan Williams * In the libata object model there is a 1:1 mapping of ata_port to 954e4a9c373SDan Williams * shost, so host fields can be directly manipulated under ap->lock, in 955e4a9c373SDan Williams * the libsas case we need to hold a lock at the ha->level to coordinate 956e4a9c373SDan Williams * these events. 957e4a9c373SDan Williams * 958e4a9c373SDan Williams * LOCKING: 959e4a9c373SDan Williams * spin_lock_irqsave(host lock) 960e4a9c373SDan Williams */ 961e4a9c373SDan Williams void ata_std_end_eh(struct ata_port *ap) 962e4a9c373SDan Williams { 963e4a9c373SDan Williams struct Scsi_Host *host = ap->scsi_host; 964e4a9c373SDan Williams 965e4a9c373SDan Williams host->host_eh_scheduled = 0; 966e4a9c373SDan Williams } 967e4a9c373SDan Williams EXPORT_SYMBOL(ata_std_end_eh); 968e4a9c373SDan Williams 969e4a9c373SDan Williams 970e4a9c373SDan Williams /** 971c6fd2807SJeff Garzik * ata_port_schedule_eh - schedule error handling without a qc 972c6fd2807SJeff Garzik * @ap: ATA port to schedule EH for 973c6fd2807SJeff Garzik * 974c6fd2807SJeff Garzik * Schedule error handling for @ap. EH will kick in as soon as 975c6fd2807SJeff Garzik * all commands are drained. 976c6fd2807SJeff Garzik * 977c6fd2807SJeff Garzik * LOCKING: 978cca3974eSJeff Garzik * spin_lock_irqsave(host lock) 979c6fd2807SJeff Garzik */ 980c6fd2807SJeff Garzik void ata_port_schedule_eh(struct ata_port *ap) 981c6fd2807SJeff Garzik { 982e4a9c373SDan Williams /* see: ata_std_sched_eh, unless you know better */ 983e4a9c373SDan Williams ap->ops->sched_eh(ap); 984c6fd2807SJeff Garzik } 985a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_port_schedule_eh); 986c6fd2807SJeff Garzik 987dbd82616STejun Heo static int ata_do_link_abort(struct ata_port *ap, struct ata_link *link) 988c6fd2807SJeff Garzik { 989258c4e5cSJens Axboe struct ata_queued_cmd *qc; 990c6fd2807SJeff Garzik int tag, nr_aborted = 0; 991c6fd2807SJeff Garzik 992c6fd2807SJeff Garzik WARN_ON(!ap->ops->error_handler); 993c6fd2807SJeff Garzik 9945ddf24c5STejun Heo /* we're gonna abort all commands, no need for fast drain */ 9955ddf24c5STejun Heo ata_eh_set_pending(ap, 0); 9965ddf24c5STejun Heo 99728361c40SJens Axboe /* include internal tag in iteration */ 998258c4e5cSJens Axboe ata_qc_for_each_with_internal(ap, qc, tag) { 999dbd82616STejun Heo if (qc && (!link || qc->dev->link == link)) { 100087629312SNiklas Cassel qc->flags |= ATA_QCFLAG_EH; 1001c6fd2807SJeff Garzik ata_qc_complete(qc); 1002c6fd2807SJeff Garzik nr_aborted++; 1003c6fd2807SJeff Garzik } 1004c6fd2807SJeff Garzik } 1005c6fd2807SJeff Garzik 1006c6fd2807SJeff Garzik if (!nr_aborted) 1007c6fd2807SJeff Garzik ata_port_schedule_eh(ap); 1008c6fd2807SJeff Garzik 1009c6fd2807SJeff Garzik return nr_aborted; 1010c6fd2807SJeff Garzik } 1011c6fd2807SJeff Garzik 1012c6fd2807SJeff Garzik /** 1013dbd82616STejun Heo * ata_link_abort - abort all qc's on the link 1014dbd82616STejun Heo * @link: ATA link to abort qc's for 1015dbd82616STejun Heo * 1016dbd82616STejun Heo * Abort all active qc's active on @link and schedule EH. 1017dbd82616STejun Heo * 1018dbd82616STejun Heo * LOCKING: 1019dbd82616STejun Heo * spin_lock_irqsave(host lock) 1020dbd82616STejun Heo * 1021dbd82616STejun Heo * RETURNS: 1022dbd82616STejun Heo * Number of aborted qc's. 1023dbd82616STejun Heo */ 1024dbd82616STejun Heo int ata_link_abort(struct ata_link *link) 1025dbd82616STejun Heo { 1026dbd82616STejun Heo return ata_do_link_abort(link->ap, link); 1027dbd82616STejun Heo } 1028a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_link_abort); 1029dbd82616STejun Heo 1030dbd82616STejun Heo /** 1031dbd82616STejun Heo * ata_port_abort - abort all qc's on the port 1032dbd82616STejun Heo * @ap: ATA port to abort qc's for 1033dbd82616STejun Heo * 1034dbd82616STejun Heo * Abort all active qc's of @ap and schedule EH. 1035dbd82616STejun Heo * 1036dbd82616STejun Heo * LOCKING: 1037dbd82616STejun Heo * spin_lock_irqsave(host_set lock) 1038dbd82616STejun Heo * 1039dbd82616STejun Heo * RETURNS: 1040dbd82616STejun Heo * Number of aborted qc's. 1041dbd82616STejun Heo */ 1042dbd82616STejun Heo int ata_port_abort(struct ata_port *ap) 1043dbd82616STejun Heo { 1044dbd82616STejun Heo return ata_do_link_abort(ap, NULL); 1045dbd82616STejun Heo } 1046a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_port_abort); 1047dbd82616STejun Heo 1048dbd82616STejun Heo /** 1049c6fd2807SJeff Garzik * __ata_port_freeze - freeze port 1050c6fd2807SJeff Garzik * @ap: ATA port to freeze 1051c6fd2807SJeff Garzik * 1052c6fd2807SJeff Garzik * This function is called when HSM violation or some other 1053c6fd2807SJeff Garzik * condition disrupts normal operation of the port. Frozen port 1054c6fd2807SJeff Garzik * is not allowed to perform any operation until the port is 1055c6fd2807SJeff Garzik * thawed, which usually follows a successful reset. 1056c6fd2807SJeff Garzik * 1057c6fd2807SJeff Garzik * ap->ops->freeze() callback can be used for freezing the port 1058c6fd2807SJeff Garzik * hardware-wise (e.g. mask interrupt and stop DMA engine). If a 1059c6fd2807SJeff Garzik * port cannot be frozen hardware-wise, the interrupt handler 1060c6fd2807SJeff Garzik * must ack and clear interrupts unconditionally while the port 1061c6fd2807SJeff Garzik * is frozen. 1062c6fd2807SJeff Garzik * 1063c6fd2807SJeff Garzik * LOCKING: 1064cca3974eSJeff Garzik * spin_lock_irqsave(host lock) 1065c6fd2807SJeff Garzik */ 1066c6fd2807SJeff Garzik static void __ata_port_freeze(struct ata_port *ap) 1067c6fd2807SJeff Garzik { 1068c6fd2807SJeff Garzik WARN_ON(!ap->ops->error_handler); 1069c6fd2807SJeff Garzik 1070c6fd2807SJeff Garzik if (ap->ops->freeze) 1071c6fd2807SJeff Garzik ap->ops->freeze(ap); 1072c6fd2807SJeff Garzik 1073c6fd2807SJeff Garzik ap->pflags |= ATA_PFLAG_FROZEN; 1074c6fd2807SJeff Garzik 1075c318458cSHannes Reinecke trace_ata_port_freeze(ap); 1076c6fd2807SJeff Garzik } 1077c6fd2807SJeff Garzik 1078c6fd2807SJeff Garzik /** 1079c6fd2807SJeff Garzik * ata_port_freeze - abort & freeze port 1080c6fd2807SJeff Garzik * @ap: ATA port to freeze 1081c6fd2807SJeff Garzik * 108254c38444SJeff Garzik * Abort and freeze @ap. The freeze operation must be called 108354c38444SJeff Garzik * first, because some hardware requires special operations 108454c38444SJeff Garzik * before the taskfile registers are accessible. 1085c6fd2807SJeff Garzik * 1086c6fd2807SJeff Garzik * LOCKING: 1087cca3974eSJeff Garzik * spin_lock_irqsave(host lock) 1088c6fd2807SJeff Garzik * 1089c6fd2807SJeff Garzik * RETURNS: 1090c6fd2807SJeff Garzik * Number of aborted commands. 1091c6fd2807SJeff Garzik */ 1092c6fd2807SJeff Garzik int ata_port_freeze(struct ata_port *ap) 1093c6fd2807SJeff Garzik { 1094c6fd2807SJeff Garzik WARN_ON(!ap->ops->error_handler); 1095c6fd2807SJeff Garzik 1096c6fd2807SJeff Garzik __ata_port_freeze(ap); 1097c6fd2807SJeff Garzik 1098cb6e73aaSye xingchen return ata_port_abort(ap); 1099c6fd2807SJeff Garzik } 1100a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_port_freeze); 1101c6fd2807SJeff Garzik 1102c6fd2807SJeff Garzik /** 1103c6fd2807SJeff Garzik * ata_eh_freeze_port - EH helper to freeze port 1104c6fd2807SJeff Garzik * @ap: ATA port to freeze 1105c6fd2807SJeff Garzik * 1106c6fd2807SJeff Garzik * Freeze @ap. 1107c6fd2807SJeff Garzik * 1108c6fd2807SJeff Garzik * LOCKING: 1109c6fd2807SJeff Garzik * None. 1110c6fd2807SJeff Garzik */ 1111c6fd2807SJeff Garzik void ata_eh_freeze_port(struct ata_port *ap) 1112c6fd2807SJeff Garzik { 1113c6fd2807SJeff Garzik unsigned long flags; 1114c6fd2807SJeff Garzik 1115c6fd2807SJeff Garzik if (!ap->ops->error_handler) 1116c6fd2807SJeff Garzik return; 1117c6fd2807SJeff Garzik 1118c6fd2807SJeff Garzik spin_lock_irqsave(ap->lock, flags); 1119c6fd2807SJeff Garzik __ata_port_freeze(ap); 1120c6fd2807SJeff Garzik spin_unlock_irqrestore(ap->lock, flags); 1121c6fd2807SJeff Garzik } 1122a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_eh_freeze_port); 1123c6fd2807SJeff Garzik 1124c6fd2807SJeff Garzik /** 112594bd5719SMauro Carvalho Chehab * ata_eh_thaw_port - EH helper to thaw port 1126c6fd2807SJeff Garzik * @ap: ATA port to thaw 1127c6fd2807SJeff Garzik * 1128c6fd2807SJeff Garzik * Thaw frozen port @ap. 1129c6fd2807SJeff Garzik * 1130c6fd2807SJeff Garzik * LOCKING: 1131c6fd2807SJeff Garzik * None. 1132c6fd2807SJeff Garzik */ 1133c6fd2807SJeff Garzik void ata_eh_thaw_port(struct ata_port *ap) 1134c6fd2807SJeff Garzik { 1135c6fd2807SJeff Garzik unsigned long flags; 1136c6fd2807SJeff Garzik 1137c6fd2807SJeff Garzik if (!ap->ops->error_handler) 1138c6fd2807SJeff Garzik return; 1139c6fd2807SJeff Garzik 1140c6fd2807SJeff Garzik spin_lock_irqsave(ap->lock, flags); 1141c6fd2807SJeff Garzik 1142c6fd2807SJeff Garzik ap->pflags &= ~ATA_PFLAG_FROZEN; 1143c6fd2807SJeff Garzik 1144c6fd2807SJeff Garzik if (ap->ops->thaw) 1145c6fd2807SJeff Garzik ap->ops->thaw(ap); 1146c6fd2807SJeff Garzik 1147c6fd2807SJeff Garzik spin_unlock_irqrestore(ap->lock, flags); 1148c6fd2807SJeff Garzik 1149c318458cSHannes Reinecke trace_ata_port_thaw(ap); 1150c6fd2807SJeff Garzik } 1151c6fd2807SJeff Garzik 1152c6fd2807SJeff Garzik static void ata_eh_scsidone(struct scsi_cmnd *scmd) 1153c6fd2807SJeff Garzik { 1154c6fd2807SJeff Garzik /* nada */ 1155c6fd2807SJeff Garzik } 1156c6fd2807SJeff Garzik 1157c6fd2807SJeff Garzik static void __ata_eh_qc_complete(struct ata_queued_cmd *qc) 1158c6fd2807SJeff Garzik { 1159c6fd2807SJeff Garzik struct ata_port *ap = qc->ap; 1160c6fd2807SJeff Garzik struct scsi_cmnd *scmd = qc->scsicmd; 1161c6fd2807SJeff Garzik unsigned long flags; 1162c6fd2807SJeff Garzik 1163c6fd2807SJeff Garzik spin_lock_irqsave(ap->lock, flags); 1164c6fd2807SJeff Garzik qc->scsidone = ata_eh_scsidone; 1165c6fd2807SJeff Garzik __ata_qc_complete(qc); 1166c6fd2807SJeff Garzik WARN_ON(ata_tag_valid(qc->tag)); 1167c6fd2807SJeff Garzik spin_unlock_irqrestore(ap->lock, flags); 1168c6fd2807SJeff Garzik 1169c6fd2807SJeff Garzik scsi_eh_finish_cmd(scmd, &ap->eh_done_q); 1170c6fd2807SJeff Garzik } 1171c6fd2807SJeff Garzik 1172c6fd2807SJeff Garzik /** 1173c6fd2807SJeff Garzik * ata_eh_qc_complete - Complete an active ATA command from EH 1174c6fd2807SJeff Garzik * @qc: Command to complete 1175c6fd2807SJeff Garzik * 1176c6fd2807SJeff Garzik * Indicate to the mid and upper layers that an ATA command has 1177c6fd2807SJeff Garzik * completed. To be used from EH. 1178c6fd2807SJeff Garzik */ 1179c6fd2807SJeff Garzik void ata_eh_qc_complete(struct ata_queued_cmd *qc) 1180c6fd2807SJeff Garzik { 1181c6fd2807SJeff Garzik struct scsi_cmnd *scmd = qc->scsicmd; 1182c6fd2807SJeff Garzik scmd->retries = scmd->allowed; 1183c6fd2807SJeff Garzik __ata_eh_qc_complete(qc); 1184c6fd2807SJeff Garzik } 1185c6fd2807SJeff Garzik 1186c6fd2807SJeff Garzik /** 1187c6fd2807SJeff Garzik * ata_eh_qc_retry - Tell midlayer to retry an ATA command after EH 1188c6fd2807SJeff Garzik * @qc: Command to retry 1189c6fd2807SJeff Garzik * 1190c6fd2807SJeff Garzik * Indicate to the mid and upper layers that an ATA command 1191c6fd2807SJeff Garzik * should be retried. To be used from EH. 1192c6fd2807SJeff Garzik * 1193c6fd2807SJeff Garzik * SCSI midlayer limits the number of retries to scmd->allowed. 1194f13e2201SGwendal Grignou * scmd->allowed is incremented for commands which get retried 1195c6fd2807SJeff Garzik * due to unrelated failures (qc->err_mask is zero). 1196c6fd2807SJeff Garzik */ 1197c6fd2807SJeff Garzik void ata_eh_qc_retry(struct ata_queued_cmd *qc) 1198c6fd2807SJeff Garzik { 1199c6fd2807SJeff Garzik struct scsi_cmnd *scmd = qc->scsicmd; 1200f13e2201SGwendal Grignou if (!qc->err_mask) 1201f13e2201SGwendal Grignou scmd->allowed++; 1202c6fd2807SJeff Garzik __ata_eh_qc_complete(qc); 1203c6fd2807SJeff Garzik } 1204c6fd2807SJeff Garzik 1205c6fd2807SJeff Garzik /** 1206678afac6STejun Heo * ata_dev_disable - disable ATA device 1207678afac6STejun Heo * @dev: ATA device to disable 1208678afac6STejun Heo * 1209678afac6STejun Heo * Disable @dev. 1210678afac6STejun Heo * 1211678afac6STejun Heo * Locking: 1212678afac6STejun Heo * EH context. 1213678afac6STejun Heo */ 1214678afac6STejun Heo void ata_dev_disable(struct ata_device *dev) 1215678afac6STejun Heo { 1216678afac6STejun Heo if (!ata_dev_enabled(dev)) 1217678afac6STejun Heo return; 1218678afac6STejun Heo 12191c95a27cSHannes Reinecke ata_dev_warn(dev, "disable device\n"); 1220678afac6STejun Heo ata_acpi_on_disable(dev); 1221678afac6STejun Heo ata_down_xfermask_limit(dev, ATA_DNXFER_FORCE_PIO0 | ATA_DNXFER_QUIET); 1222678afac6STejun Heo dev->class++; 122399cf610aSTejun Heo 122499cf610aSTejun Heo /* From now till the next successful probe, ering is used to 122599cf610aSTejun Heo * track probe failures. Clear accumulated device error info. 122699cf610aSTejun Heo */ 122799cf610aSTejun Heo ata_ering_clear(&dev->ering); 1228678afac6STejun Heo } 1229a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_dev_disable); 1230678afac6STejun Heo 1231678afac6STejun Heo /** 1232c6fd2807SJeff Garzik * ata_eh_detach_dev - detach ATA device 1233c6fd2807SJeff Garzik * @dev: ATA device to detach 1234c6fd2807SJeff Garzik * 1235c6fd2807SJeff Garzik * Detach @dev. 1236c6fd2807SJeff Garzik * 1237c6fd2807SJeff Garzik * LOCKING: 1238c6fd2807SJeff Garzik * None. 1239c6fd2807SJeff Garzik */ 1240fb7fd614STejun Heo void ata_eh_detach_dev(struct ata_device *dev) 1241c6fd2807SJeff Garzik { 1242f58229f8STejun Heo struct ata_link *link = dev->link; 1243f58229f8STejun Heo struct ata_port *ap = link->ap; 124490484ebfSTejun Heo struct ata_eh_context *ehc = &link->eh_context; 1245c6fd2807SJeff Garzik unsigned long flags; 1246c6fd2807SJeff Garzik 1247c6fd2807SJeff Garzik ata_dev_disable(dev); 1248c6fd2807SJeff Garzik 1249c6fd2807SJeff Garzik spin_lock_irqsave(ap->lock, flags); 1250c6fd2807SJeff Garzik 1251c6fd2807SJeff Garzik dev->flags &= ~ATA_DFLAG_DETACH; 1252c6fd2807SJeff Garzik 1253c6fd2807SJeff Garzik if (ata_scsi_offline_dev(dev)) { 1254c6fd2807SJeff Garzik dev->flags |= ATA_DFLAG_DETACHED; 1255c6fd2807SJeff Garzik ap->pflags |= ATA_PFLAG_SCSI_HOTPLUG; 1256c6fd2807SJeff Garzik } 1257c6fd2807SJeff Garzik 125890484ebfSTejun Heo /* clear per-dev EH info */ 1259f58229f8STejun Heo ata_eh_clear_action(link, dev, &link->eh_info, ATA_EH_PERDEV_MASK); 1260f58229f8STejun Heo ata_eh_clear_action(link, dev, &link->eh_context.i, ATA_EH_PERDEV_MASK); 126190484ebfSTejun Heo ehc->saved_xfer_mode[dev->devno] = 0; 126290484ebfSTejun Heo ehc->saved_ncq_enabled &= ~(1 << dev->devno); 1263c6fd2807SJeff Garzik 1264c6fd2807SJeff Garzik spin_unlock_irqrestore(ap->lock, flags); 1265c6fd2807SJeff Garzik } 1266c6fd2807SJeff Garzik 1267c6fd2807SJeff Garzik /** 1268c6fd2807SJeff Garzik * ata_eh_about_to_do - about to perform eh_action 1269955e57dfSTejun Heo * @link: target ATA link 1270c6fd2807SJeff Garzik * @dev: target ATA dev for per-dev action (can be NULL) 1271c6fd2807SJeff Garzik * @action: action about to be performed 1272c6fd2807SJeff Garzik * 1273c6fd2807SJeff Garzik * Called just before performing EH actions to clear related bits 1274955e57dfSTejun Heo * in @link->eh_info such that eh actions are not unnecessarily 1275955e57dfSTejun Heo * repeated. 1276c6fd2807SJeff Garzik * 1277c6fd2807SJeff Garzik * LOCKING: 1278c6fd2807SJeff Garzik * None. 1279c6fd2807SJeff Garzik */ 1280fb7fd614STejun Heo void ata_eh_about_to_do(struct ata_link *link, struct ata_device *dev, 1281c6fd2807SJeff Garzik unsigned int action) 1282c6fd2807SJeff Garzik { 1283955e57dfSTejun Heo struct ata_port *ap = link->ap; 1284955e57dfSTejun Heo struct ata_eh_info *ehi = &link->eh_info; 1285955e57dfSTejun Heo struct ata_eh_context *ehc = &link->eh_context; 1286c6fd2807SJeff Garzik unsigned long flags; 1287c6fd2807SJeff Garzik 1288c318458cSHannes Reinecke trace_ata_eh_about_to_do(link, dev ? dev->devno : 0, action); 1289c318458cSHannes Reinecke 1290c6fd2807SJeff Garzik spin_lock_irqsave(ap->lock, flags); 1291c6fd2807SJeff Garzik 1292955e57dfSTejun Heo ata_eh_clear_action(link, dev, ehi, action); 1293c6fd2807SJeff Garzik 1294a568d1d2STejun Heo /* About to take EH action, set RECOVERED. Ignore actions on 1295a568d1d2STejun Heo * slave links as master will do them again. 1296a568d1d2STejun Heo */ 1297a568d1d2STejun Heo if (!(ehc->i.flags & ATA_EHI_QUIET) && link != ap->slave_link) 1298c6fd2807SJeff Garzik ap->pflags |= ATA_PFLAG_RECOVERED; 1299c6fd2807SJeff Garzik 1300c6fd2807SJeff Garzik spin_unlock_irqrestore(ap->lock, flags); 1301c6fd2807SJeff Garzik } 1302c6fd2807SJeff Garzik 1303c6fd2807SJeff Garzik /** 1304c6fd2807SJeff Garzik * ata_eh_done - EH action complete 13052f60e1abSJonathan Corbet * @link: ATA link for which EH actions are complete 1306c6fd2807SJeff Garzik * @dev: target ATA dev for per-dev action (can be NULL) 1307c6fd2807SJeff Garzik * @action: action just completed 1308c6fd2807SJeff Garzik * 1309c6fd2807SJeff Garzik * Called right after performing EH actions to clear related bits 1310955e57dfSTejun Heo * in @link->eh_context. 1311c6fd2807SJeff Garzik * 1312c6fd2807SJeff Garzik * LOCKING: 1313c6fd2807SJeff Garzik * None. 1314c6fd2807SJeff Garzik */ 1315fb7fd614STejun Heo void ata_eh_done(struct ata_link *link, struct ata_device *dev, 1316c6fd2807SJeff Garzik unsigned int action) 1317c6fd2807SJeff Garzik { 1318955e57dfSTejun Heo struct ata_eh_context *ehc = &link->eh_context; 13199af5c9c9STejun Heo 1320c318458cSHannes Reinecke trace_ata_eh_done(link, dev ? dev->devno : 0, action); 1321c318458cSHannes Reinecke 1322955e57dfSTejun Heo ata_eh_clear_action(link, dev, &ehc->i, action); 1323c6fd2807SJeff Garzik } 1324c6fd2807SJeff Garzik 1325c6fd2807SJeff Garzik /** 1326c6fd2807SJeff Garzik * ata_err_string - convert err_mask to descriptive string 1327c6fd2807SJeff Garzik * @err_mask: error mask to convert to string 1328c6fd2807SJeff Garzik * 1329c6fd2807SJeff Garzik * Convert @err_mask to descriptive string. Errors are 1330c6fd2807SJeff Garzik * prioritized according to severity and only the most severe 1331c6fd2807SJeff Garzik * error is reported. 1332c6fd2807SJeff Garzik * 1333c6fd2807SJeff Garzik * LOCKING: 1334c6fd2807SJeff Garzik * None. 1335c6fd2807SJeff Garzik * 1336c6fd2807SJeff Garzik * RETURNS: 1337c6fd2807SJeff Garzik * Descriptive string for @err_mask 1338c6fd2807SJeff Garzik */ 1339c6fd2807SJeff Garzik static const char *ata_err_string(unsigned int err_mask) 1340c6fd2807SJeff Garzik { 1341c6fd2807SJeff Garzik if (err_mask & AC_ERR_HOST_BUS) 1342c6fd2807SJeff Garzik return "host bus error"; 1343c6fd2807SJeff Garzik if (err_mask & AC_ERR_ATA_BUS) 1344c6fd2807SJeff Garzik return "ATA bus error"; 1345c6fd2807SJeff Garzik if (err_mask & AC_ERR_TIMEOUT) 1346c6fd2807SJeff Garzik return "timeout"; 1347c6fd2807SJeff Garzik if (err_mask & AC_ERR_HSM) 1348c6fd2807SJeff Garzik return "HSM violation"; 1349c6fd2807SJeff Garzik if (err_mask & AC_ERR_SYSTEM) 1350c6fd2807SJeff Garzik return "internal error"; 1351c6fd2807SJeff Garzik if (err_mask & AC_ERR_MEDIA) 1352c6fd2807SJeff Garzik return "media error"; 1353c6fd2807SJeff Garzik if (err_mask & AC_ERR_INVALID) 1354c6fd2807SJeff Garzik return "invalid argument"; 1355c6fd2807SJeff Garzik if (err_mask & AC_ERR_DEV) 1356c6fd2807SJeff Garzik return "device error"; 135754fb131bSDamien Le Moal if (err_mask & AC_ERR_NCQ) 135854fb131bSDamien Le Moal return "NCQ error"; 135954fb131bSDamien Le Moal if (err_mask & AC_ERR_NODEV_HINT) 136054fb131bSDamien Le Moal return "Polling detection error"; 1361c6fd2807SJeff Garzik return "unknown error"; 1362c6fd2807SJeff Garzik } 1363c6fd2807SJeff Garzik 1364c6fd2807SJeff Garzik /** 136511fc33daSTejun Heo * atapi_eh_tur - perform ATAPI TEST_UNIT_READY 136611fc33daSTejun Heo * @dev: target ATAPI device 136711fc33daSTejun Heo * @r_sense_key: out parameter for sense_key 136811fc33daSTejun Heo * 136911fc33daSTejun Heo * Perform ATAPI TEST_UNIT_READY. 137011fc33daSTejun Heo * 137111fc33daSTejun Heo * LOCKING: 137211fc33daSTejun Heo * EH context (may sleep). 137311fc33daSTejun Heo * 137411fc33daSTejun Heo * RETURNS: 137511fc33daSTejun Heo * 0 on success, AC_ERR_* mask on failure. 137611fc33daSTejun Heo */ 13773dc67440SAaron Lu unsigned int atapi_eh_tur(struct ata_device *dev, u8 *r_sense_key) 137811fc33daSTejun Heo { 137911fc33daSTejun Heo u8 cdb[ATAPI_CDB_LEN] = { TEST_UNIT_READY, 0, 0, 0, 0, 0 }; 138011fc33daSTejun Heo struct ata_taskfile tf; 138111fc33daSTejun Heo unsigned int err_mask; 138211fc33daSTejun Heo 138311fc33daSTejun Heo ata_tf_init(dev, &tf); 138411fc33daSTejun Heo 138511fc33daSTejun Heo tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE; 138611fc33daSTejun Heo tf.command = ATA_CMD_PACKET; 138711fc33daSTejun Heo tf.protocol = ATAPI_PROT_NODATA; 138811fc33daSTejun Heo 138911fc33daSTejun Heo err_mask = ata_exec_internal(dev, &tf, cdb, DMA_NONE, NULL, 0, 0); 139011fc33daSTejun Heo if (err_mask == AC_ERR_DEV) 1391efcef265SSergey Shtylyov *r_sense_key = tf.error >> 4; 139211fc33daSTejun Heo return err_mask; 139311fc33daSTejun Heo } 139411fc33daSTejun Heo 139511fc33daSTejun Heo /** 1396e87fd28cSHannes Reinecke * ata_eh_request_sense - perform REQUEST_SENSE_DATA_EXT 13972f60e1abSJonathan Corbet * @qc: qc to perform REQUEST_SENSE_SENSE_DATA_EXT to 1398e87fd28cSHannes Reinecke * 1399e87fd28cSHannes Reinecke * Perform REQUEST_SENSE_DATA_EXT after the device reported CHECK 1400e87fd28cSHannes Reinecke * SENSE. This function is an EH helper. 1401e87fd28cSHannes Reinecke * 1402e87fd28cSHannes Reinecke * LOCKING: 1403e87fd28cSHannes Reinecke * Kernel thread context (may sleep). 1404*24aeebbfSNiklas Cassel * 1405*24aeebbfSNiklas Cassel * RETURNS: 1406*24aeebbfSNiklas Cassel * true if sense data could be fetched, false otherwise. 1407e87fd28cSHannes Reinecke */ 1408*24aeebbfSNiklas Cassel static bool ata_eh_request_sense(struct ata_queued_cmd *qc) 1409e87fd28cSHannes Reinecke { 1410b46c760eSNiklas Cassel struct scsi_cmnd *cmd = qc->scsicmd; 1411e87fd28cSHannes Reinecke struct ata_device *dev = qc->dev; 1412e87fd28cSHannes Reinecke struct ata_taskfile tf; 1413e87fd28cSHannes Reinecke unsigned int err_mask; 1414e87fd28cSHannes Reinecke 14154cb7c6f1SNiklas Cassel if (ata_port_is_frozen(qc->ap)) { 1416e87fd28cSHannes Reinecke ata_dev_warn(dev, "sense data available but port frozen\n"); 1417*24aeebbfSNiklas Cassel return false; 1418e87fd28cSHannes Reinecke } 1419e87fd28cSHannes Reinecke 1420e87fd28cSHannes Reinecke if (!ata_id_sense_reporting_enabled(dev->id)) { 1421e87fd28cSHannes Reinecke ata_dev_warn(qc->dev, "sense data reporting disabled\n"); 1422*24aeebbfSNiklas Cassel return false; 1423e87fd28cSHannes Reinecke } 1424e87fd28cSHannes Reinecke 1425e87fd28cSHannes Reinecke ata_tf_init(dev, &tf); 1426e87fd28cSHannes Reinecke tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE; 1427e87fd28cSHannes Reinecke tf.flags |= ATA_TFLAG_LBA | ATA_TFLAG_LBA48; 1428e87fd28cSHannes Reinecke tf.command = ATA_CMD_REQ_SENSE_DATA; 1429e87fd28cSHannes Reinecke tf.protocol = ATA_PROT_NODATA; 1430e87fd28cSHannes Reinecke 1431e87fd28cSHannes Reinecke err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0); 1432e87fd28cSHannes Reinecke /* Ignore err_mask; ATA_ERR might be set */ 1433efcef265SSergey Shtylyov if (tf.status & ATA_SENSE) { 14344b89ad8eSNiklas Cassel if (ata_scsi_sense_is_valid(tf.lbah, tf.lbam, tf.lbal)) { 1435*24aeebbfSNiklas Cassel /* Set sense without also setting scsicmd->result */ 1436*24aeebbfSNiklas Cassel scsi_build_sense_buffer(dev->flags & ATA_DFLAG_D_SENSE, 1437*24aeebbfSNiklas Cassel cmd->sense_buffer, tf.lbah, 1438*24aeebbfSNiklas Cassel tf.lbam, tf.lbal); 1439e87fd28cSHannes Reinecke qc->flags |= ATA_QCFLAG_SENSE_VALID; 1440*24aeebbfSNiklas Cassel return true; 14414b89ad8eSNiklas Cassel } 1442e87fd28cSHannes Reinecke } else { 1443e87fd28cSHannes Reinecke ata_dev_warn(dev, "request sense failed stat %02x emask %x\n", 1444efcef265SSergey Shtylyov tf.status, err_mask); 1445e87fd28cSHannes Reinecke } 1446*24aeebbfSNiklas Cassel 1447*24aeebbfSNiklas Cassel return false; 1448e87fd28cSHannes Reinecke } 1449e87fd28cSHannes Reinecke 1450e87fd28cSHannes Reinecke /** 1451c6fd2807SJeff Garzik * atapi_eh_request_sense - perform ATAPI REQUEST_SENSE 1452c6fd2807SJeff Garzik * @dev: device to perform REQUEST_SENSE to 1453c6fd2807SJeff Garzik * @sense_buf: result sense data buffer (SCSI_SENSE_BUFFERSIZE bytes long) 14543eabddb8STejun Heo * @dfl_sense_key: default sense key to use 1455c6fd2807SJeff Garzik * 1456c6fd2807SJeff Garzik * Perform ATAPI REQUEST_SENSE after the device reported CHECK 1457c6fd2807SJeff Garzik * SENSE. This function is EH helper. 1458c6fd2807SJeff Garzik * 1459c6fd2807SJeff Garzik * LOCKING: 1460c6fd2807SJeff Garzik * Kernel thread context (may sleep). 1461c6fd2807SJeff Garzik * 1462c6fd2807SJeff Garzik * RETURNS: 1463c6fd2807SJeff Garzik * 0 on success, AC_ERR_* mask on failure 1464c6fd2807SJeff Garzik */ 14653dc67440SAaron Lu unsigned int atapi_eh_request_sense(struct ata_device *dev, 14663eabddb8STejun Heo u8 *sense_buf, u8 dfl_sense_key) 1467c6fd2807SJeff Garzik { 14683eabddb8STejun Heo u8 cdb[ATAPI_CDB_LEN] = 14693eabddb8STejun Heo { REQUEST_SENSE, 0, 0, 0, SCSI_SENSE_BUFFERSIZE, 0 }; 14709af5c9c9STejun Heo struct ata_port *ap = dev->link->ap; 1471c6fd2807SJeff Garzik struct ata_taskfile tf; 1472c6fd2807SJeff Garzik 1473c6fd2807SJeff Garzik memset(sense_buf, 0, SCSI_SENSE_BUFFERSIZE); 1474c6fd2807SJeff Garzik 147556287768SAlbert Lee /* initialize sense_buf with the error register, 147656287768SAlbert Lee * for the case where they are -not- overwritten 147756287768SAlbert Lee */ 1478c6fd2807SJeff Garzik sense_buf[0] = 0x70; 14793eabddb8STejun Heo sense_buf[2] = dfl_sense_key; 148056287768SAlbert Lee 148156287768SAlbert Lee /* some devices time out if garbage left in tf */ 148256287768SAlbert Lee ata_tf_init(dev, &tf); 1483c6fd2807SJeff Garzik 1484c6fd2807SJeff Garzik tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE; 1485c6fd2807SJeff Garzik tf.command = ATA_CMD_PACKET; 1486c6fd2807SJeff Garzik 1487c6fd2807SJeff Garzik /* is it pointless to prefer PIO for "safety reasons"? */ 1488c6fd2807SJeff Garzik if (ap->flags & ATA_FLAG_PIO_DMA) { 14890dc36888STejun Heo tf.protocol = ATAPI_PROT_DMA; 1490c6fd2807SJeff Garzik tf.feature |= ATAPI_PKT_DMA; 1491c6fd2807SJeff Garzik } else { 14920dc36888STejun Heo tf.protocol = ATAPI_PROT_PIO; 1493f2dfc1a1STejun Heo tf.lbam = SCSI_SENSE_BUFFERSIZE; 1494f2dfc1a1STejun Heo tf.lbah = 0; 1495c6fd2807SJeff Garzik } 1496c6fd2807SJeff Garzik 1497c6fd2807SJeff Garzik return ata_exec_internal(dev, &tf, cdb, DMA_FROM_DEVICE, 14982b789108STejun Heo sense_buf, SCSI_SENSE_BUFFERSIZE, 0); 1499c6fd2807SJeff Garzik } 1500c6fd2807SJeff Garzik 1501c6fd2807SJeff Garzik /** 1502c6fd2807SJeff Garzik * ata_eh_analyze_serror - analyze SError for a failed port 15030260731fSTejun Heo * @link: ATA link to analyze SError for 1504c6fd2807SJeff Garzik * 1505c6fd2807SJeff Garzik * Analyze SError if available and further determine cause of 1506c6fd2807SJeff Garzik * failure. 1507c6fd2807SJeff Garzik * 1508c6fd2807SJeff Garzik * LOCKING: 1509c6fd2807SJeff Garzik * None. 1510c6fd2807SJeff Garzik */ 15110260731fSTejun Heo static void ata_eh_analyze_serror(struct ata_link *link) 1512c6fd2807SJeff Garzik { 15130260731fSTejun Heo struct ata_eh_context *ehc = &link->eh_context; 1514c6fd2807SJeff Garzik u32 serror = ehc->i.serror; 1515c6fd2807SJeff Garzik unsigned int err_mask = 0, action = 0; 1516f9df58cbSTejun Heo u32 hotplug_mask; 1517c6fd2807SJeff Garzik 1518e0614db2STejun Heo if (serror & (SERR_PERSISTENT | SERR_DATA)) { 1519c6fd2807SJeff Garzik err_mask |= AC_ERR_ATA_BUS; 1520cf480626STejun Heo action |= ATA_EH_RESET; 1521c6fd2807SJeff Garzik } 1522c6fd2807SJeff Garzik if (serror & SERR_PROTOCOL) { 1523c6fd2807SJeff Garzik err_mask |= AC_ERR_HSM; 1524cf480626STejun Heo action |= ATA_EH_RESET; 1525c6fd2807SJeff Garzik } 1526c6fd2807SJeff Garzik if (serror & SERR_INTERNAL) { 1527c6fd2807SJeff Garzik err_mask |= AC_ERR_SYSTEM; 1528cf480626STejun Heo action |= ATA_EH_RESET; 1529c6fd2807SJeff Garzik } 1530f9df58cbSTejun Heo 1531f9df58cbSTejun Heo /* Determine whether a hotplug event has occurred. Both 1532f9df58cbSTejun Heo * SError.N/X are considered hotplug events for enabled or 1533f9df58cbSTejun Heo * host links. For disabled PMP links, only N bit is 1534f9df58cbSTejun Heo * considered as X bit is left at 1 for link plugging. 1535f9df58cbSTejun Heo */ 1536eb0e85e3STejun Heo if (link->lpm_policy > ATA_LPM_MAX_POWER) 15376b7ae954STejun Heo hotplug_mask = 0; /* hotplug doesn't work w/ LPM */ 15386b7ae954STejun Heo else if (!(link->flags & ATA_LFLAG_DISABLED) || ata_is_host_link(link)) 1539f9df58cbSTejun Heo hotplug_mask = SERR_PHYRDY_CHG | SERR_DEV_XCHG; 1540f9df58cbSTejun Heo else 1541f9df58cbSTejun Heo hotplug_mask = SERR_PHYRDY_CHG; 1542f9df58cbSTejun Heo 1543f9df58cbSTejun Heo if (serror & hotplug_mask) 1544c6fd2807SJeff Garzik ata_ehi_hotplugged(&ehc->i); 1545c6fd2807SJeff Garzik 1546c6fd2807SJeff Garzik ehc->i.err_mask |= err_mask; 1547c6fd2807SJeff Garzik ehc->i.action |= action; 1548c6fd2807SJeff Garzik } 1549c6fd2807SJeff Garzik 1550c6fd2807SJeff Garzik /** 1551c6fd2807SJeff Garzik * ata_eh_analyze_tf - analyze taskfile of a failed qc 1552c6fd2807SJeff Garzik * @qc: qc to analyze 1553c6fd2807SJeff Garzik * 1554c6fd2807SJeff Garzik * Analyze taskfile of @qc and further determine cause of 1555c6fd2807SJeff Garzik * failure. This function also requests ATAPI sense data if 155625985edcSLucas De Marchi * available. 1557c6fd2807SJeff Garzik * 1558c6fd2807SJeff Garzik * LOCKING: 1559c6fd2807SJeff Garzik * Kernel thread context (may sleep). 1560c6fd2807SJeff Garzik * 1561c6fd2807SJeff Garzik * RETURNS: 1562c6fd2807SJeff Garzik * Determined recovery action 1563c6fd2807SJeff Garzik */ 1564e3b1fff6SNiklas Cassel static unsigned int ata_eh_analyze_tf(struct ata_queued_cmd *qc) 1565c6fd2807SJeff Garzik { 1566e3b1fff6SNiklas Cassel const struct ata_taskfile *tf = &qc->result_tf; 1567c6fd2807SJeff Garzik unsigned int tmp, action = 0; 1568efcef265SSergey Shtylyov u8 stat = tf->status, err = tf->error; 1569c6fd2807SJeff Garzik 1570c6fd2807SJeff Garzik if ((stat & (ATA_BUSY | ATA_DRQ | ATA_DRDY)) != ATA_DRDY) { 1571c6fd2807SJeff Garzik qc->err_mask |= AC_ERR_HSM; 1572cf480626STejun Heo return ATA_EH_RESET; 1573c6fd2807SJeff Garzik } 1574c6fd2807SJeff Garzik 1575e87fd28cSHannes Reinecke if (stat & (ATA_ERR | ATA_DF)) { 1576a51d644aSTejun Heo qc->err_mask |= AC_ERR_DEV; 1577e87fd28cSHannes Reinecke /* 1578e87fd28cSHannes Reinecke * Sense data reporting does not work if the 1579e87fd28cSHannes Reinecke * device fault bit is set. 1580e87fd28cSHannes Reinecke */ 1581e87fd28cSHannes Reinecke if (stat & ATA_DF) 1582e87fd28cSHannes Reinecke stat &= ~ATA_SENSE; 1583e87fd28cSHannes Reinecke } else { 1584c6fd2807SJeff Garzik return 0; 1585e87fd28cSHannes Reinecke } 1586c6fd2807SJeff Garzik 1587c6fd2807SJeff Garzik switch (qc->dev->class) { 1588013115d9SNiklas Cassel case ATA_DEV_ATA: 15899162c657SHannes Reinecke case ATA_DEV_ZAC: 1590461ec040SNiklas Cassel /* 1591461ec040SNiklas Cassel * Fetch the sense data explicitly if: 1592461ec040SNiklas Cassel * -It was a non-NCQ command that failed, or 1593461ec040SNiklas Cassel * -It was a NCQ command that failed, but the sense data 1594461ec040SNiklas Cassel * was not included in the NCQ command error log 1595461ec040SNiklas Cassel * (i.e. NCQ autosense is not supported by the device). 1596461ec040SNiklas Cassel */ 1597*24aeebbfSNiklas Cassel if (!(qc->flags & ATA_QCFLAG_SENSE_VALID) && 1598*24aeebbfSNiklas Cassel (stat & ATA_SENSE) && ata_eh_request_sense(qc)) 1599*24aeebbfSNiklas Cassel set_status_byte(qc->scsicmd, SAM_STAT_CHECK_CONDITION); 1600c6fd2807SJeff Garzik if (err & ATA_ICRC) 1601c6fd2807SJeff Garzik qc->err_mask |= AC_ERR_ATA_BUS; 1602eec7e1c1SAlexey Asemov if (err & (ATA_UNC | ATA_AMNF)) 1603c6fd2807SJeff Garzik qc->err_mask |= AC_ERR_MEDIA; 1604c6fd2807SJeff Garzik if (err & ATA_IDNF) 1605c6fd2807SJeff Garzik qc->err_mask |= AC_ERR_INVALID; 1606c6fd2807SJeff Garzik break; 1607c6fd2807SJeff Garzik 1608c6fd2807SJeff Garzik case ATA_DEV_ATAPI: 16094cb7c6f1SNiklas Cassel if (!ata_port_is_frozen(qc->ap)) { 16103eabddb8STejun Heo tmp = atapi_eh_request_sense(qc->dev, 16113eabddb8STejun Heo qc->scsicmd->sense_buffer, 1612efcef265SSergey Shtylyov qc->result_tf.error >> 4); 16133852e373SHannes Reinecke if (!tmp) 1614c6fd2807SJeff Garzik qc->flags |= ATA_QCFLAG_SENSE_VALID; 16153852e373SHannes Reinecke else 1616c6fd2807SJeff Garzik qc->err_mask |= tmp; 1617c6fd2807SJeff Garzik } 1618a569a30dSTejun Heo } 1619c6fd2807SJeff Garzik 16203852e373SHannes Reinecke if (qc->flags & ATA_QCFLAG_SENSE_VALID) { 1621b8e162f9SBart Van Assche enum scsi_disposition ret = scsi_check_sense(qc->scsicmd); 16223852e373SHannes Reinecke /* 162379487259SDamien Le Moal * SUCCESS here means that the sense code could be 16243852e373SHannes Reinecke * evaluated and should be passed to the upper layers 16253852e373SHannes Reinecke * for correct evaluation. 162679487259SDamien Le Moal * FAILED means the sense code could not be interpreted 16273852e373SHannes Reinecke * and the device would need to be reset. 16283852e373SHannes Reinecke * NEEDS_RETRY and ADD_TO_MLQUEUE means that the 16293852e373SHannes Reinecke * command would need to be retried. 16303852e373SHannes Reinecke */ 16313852e373SHannes Reinecke if (ret == NEEDS_RETRY || ret == ADD_TO_MLQUEUE) { 16323852e373SHannes Reinecke qc->flags |= ATA_QCFLAG_RETRY; 16333852e373SHannes Reinecke qc->err_mask |= AC_ERR_OTHER; 16343852e373SHannes Reinecke } else if (ret != SUCCESS) { 16353852e373SHannes Reinecke qc->err_mask |= AC_ERR_HSM; 16363852e373SHannes Reinecke } 16373852e373SHannes Reinecke } 1638c6fd2807SJeff Garzik if (qc->err_mask & (AC_ERR_HSM | AC_ERR_TIMEOUT | AC_ERR_ATA_BUS)) 1639cf480626STejun Heo action |= ATA_EH_RESET; 1640c6fd2807SJeff Garzik 1641c6fd2807SJeff Garzik return action; 1642c6fd2807SJeff Garzik } 1643c6fd2807SJeff Garzik 164476326ac1STejun Heo static int ata_eh_categorize_error(unsigned int eflags, unsigned int err_mask, 164576326ac1STejun Heo int *xfer_ok) 1646c6fd2807SJeff Garzik { 164776326ac1STejun Heo int base = 0; 164876326ac1STejun Heo 164976326ac1STejun Heo if (!(eflags & ATA_EFLAG_DUBIOUS_XFER)) 165076326ac1STejun Heo *xfer_ok = 1; 165176326ac1STejun Heo 165276326ac1STejun Heo if (!*xfer_ok) 165375f9cafcSTejun Heo base = ATA_ECAT_DUBIOUS_NONE; 165476326ac1STejun Heo 16557d47e8d4STejun Heo if (err_mask & AC_ERR_ATA_BUS) 165676326ac1STejun Heo return base + ATA_ECAT_ATA_BUS; 1657c6fd2807SJeff Garzik 16587d47e8d4STejun Heo if (err_mask & AC_ERR_TIMEOUT) 165976326ac1STejun Heo return base + ATA_ECAT_TOUT_HSM; 16607d47e8d4STejun Heo 16613884f7b0STejun Heo if (eflags & ATA_EFLAG_IS_IO) { 16627d47e8d4STejun Heo if (err_mask & AC_ERR_HSM) 166376326ac1STejun Heo return base + ATA_ECAT_TOUT_HSM; 16647d47e8d4STejun Heo if ((err_mask & 16657d47e8d4STejun Heo (AC_ERR_DEV|AC_ERR_MEDIA|AC_ERR_INVALID)) == AC_ERR_DEV) 166676326ac1STejun Heo return base + ATA_ECAT_UNK_DEV; 1667c6fd2807SJeff Garzik } 1668c6fd2807SJeff Garzik 1669c6fd2807SJeff Garzik return 0; 1670c6fd2807SJeff Garzik } 1671c6fd2807SJeff Garzik 16727d47e8d4STejun Heo struct speed_down_verdict_arg { 1673c6fd2807SJeff Garzik u64 since; 167476326ac1STejun Heo int xfer_ok; 16753884f7b0STejun Heo int nr_errors[ATA_ECAT_NR]; 1676c6fd2807SJeff Garzik }; 1677c6fd2807SJeff Garzik 16787d47e8d4STejun Heo static int speed_down_verdict_cb(struct ata_ering_entry *ent, void *void_arg) 1679c6fd2807SJeff Garzik { 16807d47e8d4STejun Heo struct speed_down_verdict_arg *arg = void_arg; 168176326ac1STejun Heo int cat; 1682c6fd2807SJeff Garzik 1683d9027470SGwendal Grignou if ((ent->eflags & ATA_EFLAG_OLD_ER) || (ent->timestamp < arg->since)) 1684c6fd2807SJeff Garzik return -1; 1685c6fd2807SJeff Garzik 168676326ac1STejun Heo cat = ata_eh_categorize_error(ent->eflags, ent->err_mask, 168776326ac1STejun Heo &arg->xfer_ok); 16887d47e8d4STejun Heo arg->nr_errors[cat]++; 168976326ac1STejun Heo 1690c6fd2807SJeff Garzik return 0; 1691c6fd2807SJeff Garzik } 1692c6fd2807SJeff Garzik 1693c6fd2807SJeff Garzik /** 16947d47e8d4STejun Heo * ata_eh_speed_down_verdict - Determine speed down verdict 1695c6fd2807SJeff Garzik * @dev: Device of interest 1696c6fd2807SJeff Garzik * 1697c6fd2807SJeff Garzik * This function examines error ring of @dev and determines 16987d47e8d4STejun Heo * whether NCQ needs to be turned off, transfer speed should be 16997d47e8d4STejun Heo * stepped down, or falling back to PIO is necessary. 1700c6fd2807SJeff Garzik * 17013884f7b0STejun Heo * ECAT_ATA_BUS : ATA_BUS error for any command 1702c6fd2807SJeff Garzik * 17033884f7b0STejun Heo * ECAT_TOUT_HSM : TIMEOUT for any command or HSM violation for 17043884f7b0STejun Heo * IO commands 17057d47e8d4STejun Heo * 17063884f7b0STejun Heo * ECAT_UNK_DEV : Unknown DEV error for IO commands 1707c6fd2807SJeff Garzik * 170876326ac1STejun Heo * ECAT_DUBIOUS_* : Identical to above three but occurred while 170976326ac1STejun Heo * data transfer hasn't been verified. 171076326ac1STejun Heo * 17113884f7b0STejun Heo * Verdicts are 17127d47e8d4STejun Heo * 17133884f7b0STejun Heo * NCQ_OFF : Turn off NCQ. 17147d47e8d4STejun Heo * 17153884f7b0STejun Heo * SPEED_DOWN : Speed down transfer speed but don't fall back 17163884f7b0STejun Heo * to PIO. 17173884f7b0STejun Heo * 17183884f7b0STejun Heo * FALLBACK_TO_PIO : Fall back to PIO. 17193884f7b0STejun Heo * 17203884f7b0STejun Heo * Even if multiple verdicts are returned, only one action is 172176326ac1STejun Heo * taken per error. An action triggered by non-DUBIOUS errors 172276326ac1STejun Heo * clears ering, while one triggered by DUBIOUS_* errors doesn't. 172376326ac1STejun Heo * This is to expedite speed down decisions right after device is 172476326ac1STejun Heo * initially configured. 17253884f7b0STejun Heo * 17264091fb95SMasahiro Yamada * The following are speed down rules. #1 and #2 deal with 172776326ac1STejun Heo * DUBIOUS errors. 172876326ac1STejun Heo * 172976326ac1STejun Heo * 1. If more than one DUBIOUS_ATA_BUS or DUBIOUS_TOUT_HSM errors 173076326ac1STejun Heo * occurred during last 5 mins, SPEED_DOWN and FALLBACK_TO_PIO. 173176326ac1STejun Heo * 173276326ac1STejun Heo * 2. If more than one DUBIOUS_TOUT_HSM or DUBIOUS_UNK_DEV errors 173376326ac1STejun Heo * occurred during last 5 mins, NCQ_OFF. 173476326ac1STejun Heo * 173576326ac1STejun Heo * 3. If more than 8 ATA_BUS, TOUT_HSM or UNK_DEV errors 173625985edcSLucas De Marchi * occurred during last 5 mins, FALLBACK_TO_PIO 17373884f7b0STejun Heo * 173876326ac1STejun Heo * 4. If more than 3 TOUT_HSM or UNK_DEV errors occurred 17393884f7b0STejun Heo * during last 10 mins, NCQ_OFF. 17403884f7b0STejun Heo * 174176326ac1STejun Heo * 5. If more than 3 ATA_BUS or TOUT_HSM errors, or more than 6 17423884f7b0STejun Heo * UNK_DEV errors occurred during last 10 mins, SPEED_DOWN. 17437d47e8d4STejun Heo * 1744c6fd2807SJeff Garzik * LOCKING: 1745c6fd2807SJeff Garzik * Inherited from caller. 1746c6fd2807SJeff Garzik * 1747c6fd2807SJeff Garzik * RETURNS: 17487d47e8d4STejun Heo * OR of ATA_EH_SPDN_* flags. 1749c6fd2807SJeff Garzik */ 17507d47e8d4STejun Heo static unsigned int ata_eh_speed_down_verdict(struct ata_device *dev) 1751c6fd2807SJeff Garzik { 17527d47e8d4STejun Heo const u64 j5mins = 5LLU * 60 * HZ, j10mins = 10LLU * 60 * HZ; 17537d47e8d4STejun Heo u64 j64 = get_jiffies_64(); 17547d47e8d4STejun Heo struct speed_down_verdict_arg arg; 17557d47e8d4STejun Heo unsigned int verdict = 0; 1756c6fd2807SJeff Garzik 17573884f7b0STejun Heo /* scan past 5 mins of error history */ 17583884f7b0STejun Heo memset(&arg, 0, sizeof(arg)); 17593884f7b0STejun Heo arg.since = j64 - min(j64, j5mins); 17603884f7b0STejun Heo ata_ering_map(&dev->ering, speed_down_verdict_cb, &arg); 17613884f7b0STejun Heo 176276326ac1STejun Heo if (arg.nr_errors[ATA_ECAT_DUBIOUS_ATA_BUS] + 176376326ac1STejun Heo arg.nr_errors[ATA_ECAT_DUBIOUS_TOUT_HSM] > 1) 176476326ac1STejun Heo verdict |= ATA_EH_SPDN_SPEED_DOWN | 176576326ac1STejun Heo ATA_EH_SPDN_FALLBACK_TO_PIO | ATA_EH_SPDN_KEEP_ERRORS; 176676326ac1STejun Heo 176776326ac1STejun Heo if (arg.nr_errors[ATA_ECAT_DUBIOUS_TOUT_HSM] + 176876326ac1STejun Heo arg.nr_errors[ATA_ECAT_DUBIOUS_UNK_DEV] > 1) 176976326ac1STejun Heo verdict |= ATA_EH_SPDN_NCQ_OFF | ATA_EH_SPDN_KEEP_ERRORS; 177076326ac1STejun Heo 17713884f7b0STejun Heo if (arg.nr_errors[ATA_ECAT_ATA_BUS] + 17723884f7b0STejun Heo arg.nr_errors[ATA_ECAT_TOUT_HSM] + 1773663f99b8STejun Heo arg.nr_errors[ATA_ECAT_UNK_DEV] > 6) 17743884f7b0STejun Heo verdict |= ATA_EH_SPDN_FALLBACK_TO_PIO; 17753884f7b0STejun Heo 17767d47e8d4STejun Heo /* scan past 10 mins of error history */ 1777c6fd2807SJeff Garzik memset(&arg, 0, sizeof(arg)); 17787d47e8d4STejun Heo arg.since = j64 - min(j64, j10mins); 17797d47e8d4STejun Heo ata_ering_map(&dev->ering, speed_down_verdict_cb, &arg); 1780c6fd2807SJeff Garzik 17813884f7b0STejun Heo if (arg.nr_errors[ATA_ECAT_TOUT_HSM] + 17823884f7b0STejun Heo arg.nr_errors[ATA_ECAT_UNK_DEV] > 3) 17837d47e8d4STejun Heo verdict |= ATA_EH_SPDN_NCQ_OFF; 17843884f7b0STejun Heo 17853884f7b0STejun Heo if (arg.nr_errors[ATA_ECAT_ATA_BUS] + 17863884f7b0STejun Heo arg.nr_errors[ATA_ECAT_TOUT_HSM] > 3 || 1787663f99b8STejun Heo arg.nr_errors[ATA_ECAT_UNK_DEV] > 6) 17887d47e8d4STejun Heo verdict |= ATA_EH_SPDN_SPEED_DOWN; 1789c6fd2807SJeff Garzik 17907d47e8d4STejun Heo return verdict; 1791c6fd2807SJeff Garzik } 1792c6fd2807SJeff Garzik 1793c6fd2807SJeff Garzik /** 1794c6fd2807SJeff Garzik * ata_eh_speed_down - record error and speed down if necessary 1795c6fd2807SJeff Garzik * @dev: Failed device 17963884f7b0STejun Heo * @eflags: mask of ATA_EFLAG_* flags 1797c6fd2807SJeff Garzik * @err_mask: err_mask of the error 1798c6fd2807SJeff Garzik * 1799c6fd2807SJeff Garzik * Record error and examine error history to determine whether 1800c6fd2807SJeff Garzik * adjusting transmission speed is necessary. It also sets 1801c6fd2807SJeff Garzik * transmission limits appropriately if such adjustment is 1802c6fd2807SJeff Garzik * necessary. 1803c6fd2807SJeff Garzik * 1804c6fd2807SJeff Garzik * LOCKING: 1805c6fd2807SJeff Garzik * Kernel thread context (may sleep). 1806c6fd2807SJeff Garzik * 1807c6fd2807SJeff Garzik * RETURNS: 18087d47e8d4STejun Heo * Determined recovery action. 1809c6fd2807SJeff Garzik */ 18103884f7b0STejun Heo static unsigned int ata_eh_speed_down(struct ata_device *dev, 18113884f7b0STejun Heo unsigned int eflags, unsigned int err_mask) 1812c6fd2807SJeff Garzik { 1813b1c72916STejun Heo struct ata_link *link = ata_dev_phys_link(dev); 181476326ac1STejun Heo int xfer_ok = 0; 18157d47e8d4STejun Heo unsigned int verdict; 18167d47e8d4STejun Heo unsigned int action = 0; 18177d47e8d4STejun Heo 18187d47e8d4STejun Heo /* don't bother if Cat-0 error */ 181976326ac1STejun Heo if (ata_eh_categorize_error(eflags, err_mask, &xfer_ok) == 0) 1820c6fd2807SJeff Garzik return 0; 1821c6fd2807SJeff Garzik 1822c6fd2807SJeff Garzik /* record error and determine whether speed down is necessary */ 18233884f7b0STejun Heo ata_ering_record(&dev->ering, eflags, err_mask); 18247d47e8d4STejun Heo verdict = ata_eh_speed_down_verdict(dev); 1825c6fd2807SJeff Garzik 18267d47e8d4STejun Heo /* turn off NCQ? */ 18277d47e8d4STejun Heo if ((verdict & ATA_EH_SPDN_NCQ_OFF) && 18287d47e8d4STejun Heo (dev->flags & (ATA_DFLAG_PIO | ATA_DFLAG_NCQ | 18297d47e8d4STejun Heo ATA_DFLAG_NCQ_OFF)) == ATA_DFLAG_NCQ) { 18307d47e8d4STejun Heo dev->flags |= ATA_DFLAG_NCQ_OFF; 1831a9a79dfeSJoe Perches ata_dev_warn(dev, "NCQ disabled due to excessive errors\n"); 18327d47e8d4STejun Heo goto done; 18337d47e8d4STejun Heo } 1834c6fd2807SJeff Garzik 18357d47e8d4STejun Heo /* speed down? */ 18367d47e8d4STejun Heo if (verdict & ATA_EH_SPDN_SPEED_DOWN) { 1837c6fd2807SJeff Garzik /* speed down SATA link speed if possible */ 1838a07d499bSTejun Heo if (sata_down_spd_limit(link, 0) == 0) { 1839cf480626STejun Heo action |= ATA_EH_RESET; 18407d47e8d4STejun Heo goto done; 18417d47e8d4STejun Heo } 1842c6fd2807SJeff Garzik 1843c6fd2807SJeff Garzik /* lower transfer mode */ 18447d47e8d4STejun Heo if (dev->spdn_cnt < 2) { 18457d47e8d4STejun Heo static const int dma_dnxfer_sel[] = 18467d47e8d4STejun Heo { ATA_DNXFER_DMA, ATA_DNXFER_40C }; 18477d47e8d4STejun Heo static const int pio_dnxfer_sel[] = 18487d47e8d4STejun Heo { ATA_DNXFER_PIO, ATA_DNXFER_FORCE_PIO0 }; 18497d47e8d4STejun Heo int sel; 1850c6fd2807SJeff Garzik 18517d47e8d4STejun Heo if (dev->xfer_shift != ATA_SHIFT_PIO) 18527d47e8d4STejun Heo sel = dma_dnxfer_sel[dev->spdn_cnt]; 18537d47e8d4STejun Heo else 18547d47e8d4STejun Heo sel = pio_dnxfer_sel[dev->spdn_cnt]; 18557d47e8d4STejun Heo 18567d47e8d4STejun Heo dev->spdn_cnt++; 18577d47e8d4STejun Heo 18587d47e8d4STejun Heo if (ata_down_xfermask_limit(dev, sel) == 0) { 1859cf480626STejun Heo action |= ATA_EH_RESET; 18607d47e8d4STejun Heo goto done; 18617d47e8d4STejun Heo } 18627d47e8d4STejun Heo } 18637d47e8d4STejun Heo } 18647d47e8d4STejun Heo 18657d47e8d4STejun Heo /* Fall back to PIO? Slowing down to PIO is meaningless for 1866663f99b8STejun Heo * SATA ATA devices. Consider it only for PATA and SATAPI. 18677d47e8d4STejun Heo */ 18687d47e8d4STejun Heo if ((verdict & ATA_EH_SPDN_FALLBACK_TO_PIO) && (dev->spdn_cnt >= 2) && 1869663f99b8STejun Heo (link->ap->cbl != ATA_CBL_SATA || dev->class == ATA_DEV_ATAPI) && 18707d47e8d4STejun Heo (dev->xfer_shift != ATA_SHIFT_PIO)) { 18717d47e8d4STejun Heo if (ata_down_xfermask_limit(dev, ATA_DNXFER_FORCE_PIO) == 0) { 18727d47e8d4STejun Heo dev->spdn_cnt = 0; 1873cf480626STejun Heo action |= ATA_EH_RESET; 18747d47e8d4STejun Heo goto done; 18757d47e8d4STejun Heo } 18767d47e8d4STejun Heo } 18777d47e8d4STejun Heo 1878c6fd2807SJeff Garzik return 0; 18797d47e8d4STejun Heo done: 18807d47e8d4STejun Heo /* device has been slowed down, blow error history */ 188176326ac1STejun Heo if (!(verdict & ATA_EH_SPDN_KEEP_ERRORS)) 18827d47e8d4STejun Heo ata_ering_clear(&dev->ering); 18837d47e8d4STejun Heo return action; 1884c6fd2807SJeff Garzik } 1885c6fd2807SJeff Garzik 1886c6fd2807SJeff Garzik /** 18878d899e70SMark Lord * ata_eh_worth_retry - analyze error and decide whether to retry 18888d899e70SMark Lord * @qc: qc to possibly retry 18898d899e70SMark Lord * 18908d899e70SMark Lord * Look at the cause of the error and decide if a retry 18918d899e70SMark Lord * might be useful or not. We don't want to retry media errors 18928d899e70SMark Lord * because the drive itself has probably already taken 10-30 seconds 18938d899e70SMark Lord * doing its own internal retries before reporting the failure. 18948d899e70SMark Lord */ 18958d899e70SMark Lord static inline int ata_eh_worth_retry(struct ata_queued_cmd *qc) 18968d899e70SMark Lord { 18971eaca39aSBian Yu if (qc->err_mask & AC_ERR_MEDIA) 18988d899e70SMark Lord return 0; /* don't retry media errors */ 18998d899e70SMark Lord if (qc->flags & ATA_QCFLAG_IO) 19008d899e70SMark Lord return 1; /* otherwise retry anything from fs stack */ 19018d899e70SMark Lord if (qc->err_mask & AC_ERR_INVALID) 19028d899e70SMark Lord return 0; /* don't retry these */ 19038d899e70SMark Lord return qc->err_mask != AC_ERR_DEV; /* retry if not dev error */ 19048d899e70SMark Lord } 19058d899e70SMark Lord 19068d899e70SMark Lord /** 19077eb49509SDamien Le Moal * ata_eh_quiet - check if we need to be quiet about a command error 19087eb49509SDamien Le Moal * @qc: qc to check 19097eb49509SDamien Le Moal * 19107eb49509SDamien Le Moal * Look at the qc flags anbd its scsi command request flags to determine 19117eb49509SDamien Le Moal * if we need to be quiet about the command failure. 19127eb49509SDamien Le Moal */ 19137eb49509SDamien Le Moal static inline bool ata_eh_quiet(struct ata_queued_cmd *qc) 19147eb49509SDamien Le Moal { 1915c8329cd5SBart Van Assche if (qc->scsicmd && scsi_cmd_to_rq(qc->scsicmd)->rq_flags & RQF_QUIET) 19167eb49509SDamien Le Moal qc->flags |= ATA_QCFLAG_QUIET; 19177eb49509SDamien Le Moal return qc->flags & ATA_QCFLAG_QUIET; 19187eb49509SDamien Le Moal } 19197eb49509SDamien Le Moal 19207eb49509SDamien Le Moal /** 19219b1e2658STejun Heo * ata_eh_link_autopsy - analyze error and determine recovery action 19229b1e2658STejun Heo * @link: host link to perform autopsy on 1923c6fd2807SJeff Garzik * 19240260731fSTejun Heo * Analyze why @link failed and determine which recovery actions 19250260731fSTejun Heo * are needed. This function also sets more detailed AC_ERR_* 19260260731fSTejun Heo * values and fills sense data for ATAPI CHECK SENSE. 1927c6fd2807SJeff Garzik * 1928c6fd2807SJeff Garzik * LOCKING: 1929c6fd2807SJeff Garzik * Kernel thread context (may sleep). 1930c6fd2807SJeff Garzik */ 19319b1e2658STejun Heo static void ata_eh_link_autopsy(struct ata_link *link) 1932c6fd2807SJeff Garzik { 19330260731fSTejun Heo struct ata_port *ap = link->ap; 1934936fd732STejun Heo struct ata_eh_context *ehc = &link->eh_context; 1935258c4e5cSJens Axboe struct ata_queued_cmd *qc; 1936dfcc173dSTejun Heo struct ata_device *dev; 19373884f7b0STejun Heo unsigned int all_err_mask = 0, eflags = 0; 19387eb49509SDamien Le Moal int tag, nr_failed = 0, nr_quiet = 0; 1939c6fd2807SJeff Garzik u32 serror; 1940c6fd2807SJeff Garzik int rc; 1941c6fd2807SJeff Garzik 1942c6fd2807SJeff Garzik if (ehc->i.flags & ATA_EHI_NO_AUTOPSY) 1943c6fd2807SJeff Garzik return; 1944c6fd2807SJeff Garzik 1945c6fd2807SJeff Garzik /* obtain and analyze SError */ 1946936fd732STejun Heo rc = sata_scr_read(link, SCR_ERROR, &serror); 1947c6fd2807SJeff Garzik if (rc == 0) { 1948c6fd2807SJeff Garzik ehc->i.serror |= serror; 19490260731fSTejun Heo ata_eh_analyze_serror(link); 19504e57c517STejun Heo } else if (rc != -EOPNOTSUPP) { 1951cf480626STejun Heo /* SError read failed, force reset and probing */ 1952b558edddSTejun Heo ehc->i.probe_mask |= ATA_ALL_DEVICES; 1953cf480626STejun Heo ehc->i.action |= ATA_EH_RESET; 19544e57c517STejun Heo ehc->i.err_mask |= AC_ERR_OTHER; 19554e57c517STejun Heo } 1956c6fd2807SJeff Garzik 1957c6fd2807SJeff Garzik /* analyze NCQ failure */ 19580260731fSTejun Heo ata_eh_analyze_ncq_error(link); 1959c6fd2807SJeff Garzik 1960c6fd2807SJeff Garzik /* any real error trumps AC_ERR_OTHER */ 1961c6fd2807SJeff Garzik if (ehc->i.err_mask & ~AC_ERR_OTHER) 1962c6fd2807SJeff Garzik ehc->i.err_mask &= ~AC_ERR_OTHER; 1963c6fd2807SJeff Garzik 1964c6fd2807SJeff Garzik all_err_mask |= ehc->i.err_mask; 1965c6fd2807SJeff Garzik 1966258c4e5cSJens Axboe ata_qc_for_each_raw(ap, qc, tag) { 196787629312SNiklas Cassel if (!(qc->flags & ATA_QCFLAG_EH) || 19683d8a3ae3SNiklas Cassel qc->flags & ATA_QCFLAG_RETRY || 1969b1c72916STejun Heo ata_dev_phys_link(qc->dev) != link) 1970c6fd2807SJeff Garzik continue; 1971c6fd2807SJeff Garzik 1972c6fd2807SJeff Garzik /* inherit upper level err_mask */ 1973c6fd2807SJeff Garzik qc->err_mask |= ehc->i.err_mask; 1974c6fd2807SJeff Garzik 1975c6fd2807SJeff Garzik /* analyze TF */ 1976e3b1fff6SNiklas Cassel ehc->i.action |= ata_eh_analyze_tf(qc); 1977c6fd2807SJeff Garzik 1978c6fd2807SJeff Garzik /* DEV errors are probably spurious in case of ATA_BUS error */ 1979c6fd2807SJeff Garzik if (qc->err_mask & AC_ERR_ATA_BUS) 1980c6fd2807SJeff Garzik qc->err_mask &= ~(AC_ERR_DEV | AC_ERR_MEDIA | 1981c6fd2807SJeff Garzik AC_ERR_INVALID); 1982c6fd2807SJeff Garzik 1983c6fd2807SJeff Garzik /* any real error trumps unknown error */ 1984c6fd2807SJeff Garzik if (qc->err_mask & ~AC_ERR_OTHER) 1985c6fd2807SJeff Garzik qc->err_mask &= ~AC_ERR_OTHER; 1986c6fd2807SJeff Garzik 1987804689adSDamien Le Moal /* 1988804689adSDamien Le Moal * SENSE_VALID trumps dev/unknown error and revalidation. Upper 1989804689adSDamien Le Moal * layers will determine whether the command is worth retrying 1990804689adSDamien Le Moal * based on the sense data and device class/type. Otherwise, 1991804689adSDamien Le Moal * determine directly if the command is worth retrying using its 1992804689adSDamien Le Moal * error mask and flags. 1993804689adSDamien Le Moal */ 1994f90f0828STejun Heo if (qc->flags & ATA_QCFLAG_SENSE_VALID) 1995c6fd2807SJeff Garzik qc->err_mask &= ~(AC_ERR_DEV | AC_ERR_OTHER); 1996804689adSDamien Le Moal else if (ata_eh_worth_retry(qc)) 199703faab78STejun Heo qc->flags |= ATA_QCFLAG_RETRY; 199803faab78STejun Heo 1999c6fd2807SJeff Garzik /* accumulate error info */ 2000c6fd2807SJeff Garzik ehc->i.dev = qc->dev; 2001c6fd2807SJeff Garzik all_err_mask |= qc->err_mask; 2002c6fd2807SJeff Garzik if (qc->flags & ATA_QCFLAG_IO) 20033884f7b0STejun Heo eflags |= ATA_EFLAG_IS_IO; 2004255c03d1SHannes Reinecke trace_ata_eh_link_autopsy_qc(qc); 20057eb49509SDamien Le Moal 20067eb49509SDamien Le Moal /* Count quiet errors */ 20077eb49509SDamien Le Moal if (ata_eh_quiet(qc)) 20087eb49509SDamien Le Moal nr_quiet++; 20097eb49509SDamien Le Moal nr_failed++; 2010c6fd2807SJeff Garzik } 2011c6fd2807SJeff Garzik 20127eb49509SDamien Le Moal /* If all failed commands requested silence, then be quiet */ 20137eb49509SDamien Le Moal if (nr_quiet == nr_failed) 20147eb49509SDamien Le Moal ehc->i.flags |= ATA_EHI_QUIET; 20157eb49509SDamien Le Moal 2016c6fd2807SJeff Garzik /* enforce default EH actions */ 20174cb7c6f1SNiklas Cassel if (ata_port_is_frozen(ap) || 2018c6fd2807SJeff Garzik all_err_mask & (AC_ERR_HSM | AC_ERR_TIMEOUT)) 2019cf480626STejun Heo ehc->i.action |= ATA_EH_RESET; 20203884f7b0STejun Heo else if (((eflags & ATA_EFLAG_IS_IO) && all_err_mask) || 20213884f7b0STejun Heo (!(eflags & ATA_EFLAG_IS_IO) && (all_err_mask & ~AC_ERR_DEV))) 2022c6fd2807SJeff Garzik ehc->i.action |= ATA_EH_REVALIDATE; 2023c6fd2807SJeff Garzik 2024dfcc173dSTejun Heo /* If we have offending qcs and the associated failed device, 2025dfcc173dSTejun Heo * perform per-dev EH action only on the offending device. 2026dfcc173dSTejun Heo */ 2027c6fd2807SJeff Garzik if (ehc->i.dev) { 2028c6fd2807SJeff Garzik ehc->i.dev_action[ehc->i.dev->devno] |= 2029c6fd2807SJeff Garzik ehc->i.action & ATA_EH_PERDEV_MASK; 2030c6fd2807SJeff Garzik ehc->i.action &= ~ATA_EH_PERDEV_MASK; 2031c6fd2807SJeff Garzik } 2032c6fd2807SJeff Garzik 20332695e366STejun Heo /* propagate timeout to host link */ 20342695e366STejun Heo if ((all_err_mask & AC_ERR_TIMEOUT) && !ata_is_host_link(link)) 20352695e366STejun Heo ap->link.eh_context.i.err_mask |= AC_ERR_TIMEOUT; 20362695e366STejun Heo 20372695e366STejun Heo /* record error and consider speeding down */ 2038dfcc173dSTejun Heo dev = ehc->i.dev; 20392695e366STejun Heo if (!dev && ((ata_link_max_devices(link) == 1 && 20402695e366STejun Heo ata_dev_enabled(link->device)))) 2041dfcc173dSTejun Heo dev = link->device; 2042dfcc173dSTejun Heo 204376326ac1STejun Heo if (dev) { 204476326ac1STejun Heo if (dev->flags & ATA_DFLAG_DUBIOUS_XFER) 204576326ac1STejun Heo eflags |= ATA_EFLAG_DUBIOUS_XFER; 20463884f7b0STejun Heo ehc->i.action |= ata_eh_speed_down(dev, eflags, all_err_mask); 2047255c03d1SHannes Reinecke trace_ata_eh_link_autopsy(dev, ehc->i.action, all_err_mask); 2048f1601113SRameshwar Prasad Sahu } 2049c6fd2807SJeff Garzik } 2050c6fd2807SJeff Garzik 2051c6fd2807SJeff Garzik /** 20529b1e2658STejun Heo * ata_eh_autopsy - analyze error and determine recovery action 20539b1e2658STejun Heo * @ap: host port to perform autopsy on 20549b1e2658STejun Heo * 20559b1e2658STejun Heo * Analyze all links of @ap and determine why they failed and 20569b1e2658STejun Heo * which recovery actions are needed. 20579b1e2658STejun Heo * 20589b1e2658STejun Heo * LOCKING: 20599b1e2658STejun Heo * Kernel thread context (may sleep). 20609b1e2658STejun Heo */ 2061fb7fd614STejun Heo void ata_eh_autopsy(struct ata_port *ap) 20629b1e2658STejun Heo { 20639b1e2658STejun Heo struct ata_link *link; 20649b1e2658STejun Heo 20651eca4365STejun Heo ata_for_each_link(link, ap, EDGE) 20669b1e2658STejun Heo ata_eh_link_autopsy(link); 20672695e366STejun Heo 2068b1c72916STejun Heo /* Handle the frigging slave link. Autopsy is done similarly 2069b1c72916STejun Heo * but actions and flags are transferred over to the master 2070b1c72916STejun Heo * link and handled from there. 2071b1c72916STejun Heo */ 2072b1c72916STejun Heo if (ap->slave_link) { 2073b1c72916STejun Heo struct ata_eh_context *mehc = &ap->link.eh_context; 2074b1c72916STejun Heo struct ata_eh_context *sehc = &ap->slave_link->eh_context; 2075b1c72916STejun Heo 2076848e4c68STejun Heo /* transfer control flags from master to slave */ 2077848e4c68STejun Heo sehc->i.flags |= mehc->i.flags & ATA_EHI_TO_SLAVE_MASK; 2078848e4c68STejun Heo 2079848e4c68STejun Heo /* perform autopsy on the slave link */ 2080b1c72916STejun Heo ata_eh_link_autopsy(ap->slave_link); 2081b1c72916STejun Heo 2082848e4c68STejun Heo /* transfer actions from slave to master and clear slave */ 2083b1c72916STejun Heo ata_eh_about_to_do(ap->slave_link, NULL, ATA_EH_ALL_ACTIONS); 2084b1c72916STejun Heo mehc->i.action |= sehc->i.action; 2085b1c72916STejun Heo mehc->i.dev_action[1] |= sehc->i.dev_action[1]; 2086b1c72916STejun Heo mehc->i.flags |= sehc->i.flags; 2087b1c72916STejun Heo ata_eh_done(ap->slave_link, NULL, ATA_EH_ALL_ACTIONS); 2088b1c72916STejun Heo } 2089b1c72916STejun Heo 20902695e366STejun Heo /* Autopsy of fanout ports can affect host link autopsy. 20912695e366STejun Heo * Perform host link autopsy last. 20922695e366STejun Heo */ 2093071f44b1STejun Heo if (sata_pmp_attached(ap)) 20942695e366STejun Heo ata_eh_link_autopsy(&ap->link); 20959b1e2658STejun Heo } 20969b1e2658STejun Heo 20979b1e2658STejun Heo /** 2098d4520903SHannes Reinecke * ata_get_cmd_name - get name for ATA command 2099d4520903SHannes Reinecke * @command: ATA command code to get name for 21006521148cSRobert Hancock * 2101d4520903SHannes Reinecke * Return a textual name of the given command or "unknown" 21026521148cSRobert Hancock * 21036521148cSRobert Hancock * LOCKING: 21046521148cSRobert Hancock * None 21056521148cSRobert Hancock */ 2106d4520903SHannes Reinecke const char *ata_get_cmd_name(u8 command) 21076521148cSRobert Hancock { 21086521148cSRobert Hancock #ifdef CONFIG_ATA_VERBOSE_ERROR 21096521148cSRobert Hancock static const struct 21106521148cSRobert Hancock { 21116521148cSRobert Hancock u8 command; 21126521148cSRobert Hancock const char *text; 21136521148cSRobert Hancock } cmd_descr[] = { 21146521148cSRobert Hancock { ATA_CMD_DEV_RESET, "DEVICE RESET" }, 21156521148cSRobert Hancock { ATA_CMD_CHK_POWER, "CHECK POWER MODE" }, 21166521148cSRobert Hancock { ATA_CMD_STANDBY, "STANDBY" }, 21176521148cSRobert Hancock { ATA_CMD_IDLE, "IDLE" }, 21186521148cSRobert Hancock { ATA_CMD_EDD, "EXECUTE DEVICE DIAGNOSTIC" }, 21196521148cSRobert Hancock { ATA_CMD_DOWNLOAD_MICRO, "DOWNLOAD MICROCODE" }, 21203915c3b5SRobert Hancock { ATA_CMD_DOWNLOAD_MICRO_DMA, "DOWNLOAD MICROCODE DMA" }, 21216521148cSRobert Hancock { ATA_CMD_NOP, "NOP" }, 21226521148cSRobert Hancock { ATA_CMD_FLUSH, "FLUSH CACHE" }, 21236521148cSRobert Hancock { ATA_CMD_FLUSH_EXT, "FLUSH CACHE EXT" }, 21246521148cSRobert Hancock { ATA_CMD_ID_ATA, "IDENTIFY DEVICE" }, 21256521148cSRobert Hancock { ATA_CMD_ID_ATAPI, "IDENTIFY PACKET DEVICE" }, 21266521148cSRobert Hancock { ATA_CMD_SERVICE, "SERVICE" }, 21276521148cSRobert Hancock { ATA_CMD_READ, "READ DMA" }, 21286521148cSRobert Hancock { ATA_CMD_READ_EXT, "READ DMA EXT" }, 21296521148cSRobert Hancock { ATA_CMD_READ_QUEUED, "READ DMA QUEUED" }, 21306521148cSRobert Hancock { ATA_CMD_READ_STREAM_EXT, "READ STREAM EXT" }, 21316521148cSRobert Hancock { ATA_CMD_READ_STREAM_DMA_EXT, "READ STREAM DMA EXT" }, 21326521148cSRobert Hancock { ATA_CMD_WRITE, "WRITE DMA" }, 21336521148cSRobert Hancock { ATA_CMD_WRITE_EXT, "WRITE DMA EXT" }, 21346521148cSRobert Hancock { ATA_CMD_WRITE_QUEUED, "WRITE DMA QUEUED EXT" }, 21356521148cSRobert Hancock { ATA_CMD_WRITE_STREAM_EXT, "WRITE STREAM EXT" }, 21366521148cSRobert Hancock { ATA_CMD_WRITE_STREAM_DMA_EXT, "WRITE STREAM DMA EXT" }, 21376521148cSRobert Hancock { ATA_CMD_WRITE_FUA_EXT, "WRITE DMA FUA EXT" }, 21386521148cSRobert Hancock { ATA_CMD_WRITE_QUEUED_FUA_EXT, "WRITE DMA QUEUED FUA EXT" }, 21396521148cSRobert Hancock { ATA_CMD_FPDMA_READ, "READ FPDMA QUEUED" }, 21406521148cSRobert Hancock { ATA_CMD_FPDMA_WRITE, "WRITE FPDMA QUEUED" }, 2141d3122bf9SDamien Le Moal { ATA_CMD_NCQ_NON_DATA, "NCQ NON-DATA" }, 21423915c3b5SRobert Hancock { ATA_CMD_FPDMA_SEND, "SEND FPDMA QUEUED" }, 21433915c3b5SRobert Hancock { ATA_CMD_FPDMA_RECV, "RECEIVE FPDMA QUEUED" }, 21446521148cSRobert Hancock { ATA_CMD_PIO_READ, "READ SECTOR(S)" }, 21456521148cSRobert Hancock { ATA_CMD_PIO_READ_EXT, "READ SECTOR(S) EXT" }, 21466521148cSRobert Hancock { ATA_CMD_PIO_WRITE, "WRITE SECTOR(S)" }, 21476521148cSRobert Hancock { ATA_CMD_PIO_WRITE_EXT, "WRITE SECTOR(S) EXT" }, 21486521148cSRobert Hancock { ATA_CMD_READ_MULTI, "READ MULTIPLE" }, 21496521148cSRobert Hancock { ATA_CMD_READ_MULTI_EXT, "READ MULTIPLE EXT" }, 21506521148cSRobert Hancock { ATA_CMD_WRITE_MULTI, "WRITE MULTIPLE" }, 21516521148cSRobert Hancock { ATA_CMD_WRITE_MULTI_EXT, "WRITE MULTIPLE EXT" }, 21526521148cSRobert Hancock { ATA_CMD_WRITE_MULTI_FUA_EXT, "WRITE MULTIPLE FUA EXT" }, 21536521148cSRobert Hancock { ATA_CMD_SET_FEATURES, "SET FEATURES" }, 21546521148cSRobert Hancock { ATA_CMD_SET_MULTI, "SET MULTIPLE MODE" }, 21556521148cSRobert Hancock { ATA_CMD_VERIFY, "READ VERIFY SECTOR(S)" }, 21566521148cSRobert Hancock { ATA_CMD_VERIFY_EXT, "READ VERIFY SECTOR(S) EXT" }, 21576521148cSRobert Hancock { ATA_CMD_WRITE_UNCORR_EXT, "WRITE UNCORRECTABLE EXT" }, 21586521148cSRobert Hancock { ATA_CMD_STANDBYNOW1, "STANDBY IMMEDIATE" }, 21596521148cSRobert Hancock { ATA_CMD_IDLEIMMEDIATE, "IDLE IMMEDIATE" }, 21606521148cSRobert Hancock { ATA_CMD_SLEEP, "SLEEP" }, 21616521148cSRobert Hancock { ATA_CMD_INIT_DEV_PARAMS, "INITIALIZE DEVICE PARAMETERS" }, 21626521148cSRobert Hancock { ATA_CMD_READ_NATIVE_MAX, "READ NATIVE MAX ADDRESS" }, 21636521148cSRobert Hancock { ATA_CMD_READ_NATIVE_MAX_EXT, "READ NATIVE MAX ADDRESS EXT" }, 21646521148cSRobert Hancock { ATA_CMD_SET_MAX, "SET MAX ADDRESS" }, 21656521148cSRobert Hancock { ATA_CMD_SET_MAX_EXT, "SET MAX ADDRESS EXT" }, 21666521148cSRobert Hancock { ATA_CMD_READ_LOG_EXT, "READ LOG EXT" }, 21676521148cSRobert Hancock { ATA_CMD_WRITE_LOG_EXT, "WRITE LOG EXT" }, 21686521148cSRobert Hancock { ATA_CMD_READ_LOG_DMA_EXT, "READ LOG DMA EXT" }, 21696521148cSRobert Hancock { ATA_CMD_WRITE_LOG_DMA_EXT, "WRITE LOG DMA EXT" }, 21703915c3b5SRobert Hancock { ATA_CMD_TRUSTED_NONDATA, "TRUSTED NON-DATA" }, 21716521148cSRobert Hancock { ATA_CMD_TRUSTED_RCV, "TRUSTED RECEIVE" }, 21726521148cSRobert Hancock { ATA_CMD_TRUSTED_RCV_DMA, "TRUSTED RECEIVE DMA" }, 21736521148cSRobert Hancock { ATA_CMD_TRUSTED_SND, "TRUSTED SEND" }, 21746521148cSRobert Hancock { ATA_CMD_TRUSTED_SND_DMA, "TRUSTED SEND DMA" }, 21756521148cSRobert Hancock { ATA_CMD_PMP_READ, "READ BUFFER" }, 21763915c3b5SRobert Hancock { ATA_CMD_PMP_READ_DMA, "READ BUFFER DMA" }, 21776521148cSRobert Hancock { ATA_CMD_PMP_WRITE, "WRITE BUFFER" }, 21783915c3b5SRobert Hancock { ATA_CMD_PMP_WRITE_DMA, "WRITE BUFFER DMA" }, 21796521148cSRobert Hancock { ATA_CMD_CONF_OVERLAY, "DEVICE CONFIGURATION OVERLAY" }, 21806521148cSRobert Hancock { ATA_CMD_SEC_SET_PASS, "SECURITY SET PASSWORD" }, 21816521148cSRobert Hancock { ATA_CMD_SEC_UNLOCK, "SECURITY UNLOCK" }, 21826521148cSRobert Hancock { ATA_CMD_SEC_ERASE_PREP, "SECURITY ERASE PREPARE" }, 21836521148cSRobert Hancock { ATA_CMD_SEC_ERASE_UNIT, "SECURITY ERASE UNIT" }, 21846521148cSRobert Hancock { ATA_CMD_SEC_FREEZE_LOCK, "SECURITY FREEZE LOCK" }, 21856521148cSRobert Hancock { ATA_CMD_SEC_DISABLE_PASS, "SECURITY DISABLE PASSWORD" }, 21866521148cSRobert Hancock { ATA_CMD_CONFIG_STREAM, "CONFIGURE STREAM" }, 21876521148cSRobert Hancock { ATA_CMD_SMART, "SMART" }, 21886521148cSRobert Hancock { ATA_CMD_MEDIA_LOCK, "DOOR LOCK" }, 21896521148cSRobert Hancock { ATA_CMD_MEDIA_UNLOCK, "DOOR UNLOCK" }, 2190acad7627SFUJITA Tomonori { ATA_CMD_DSM, "DATA SET MANAGEMENT" }, 21916521148cSRobert Hancock { ATA_CMD_CHK_MED_CRD_TYP, "CHECK MEDIA CARD TYPE" }, 21926521148cSRobert Hancock { ATA_CMD_CFA_REQ_EXT_ERR, "CFA REQUEST EXTENDED ERROR" }, 21936521148cSRobert Hancock { ATA_CMD_CFA_WRITE_NE, "CFA WRITE SECTORS WITHOUT ERASE" }, 21946521148cSRobert Hancock { ATA_CMD_CFA_TRANS_SECT, "CFA TRANSLATE SECTOR" }, 21956521148cSRobert Hancock { ATA_CMD_CFA_ERASE, "CFA ERASE SECTORS" }, 21966521148cSRobert Hancock { ATA_CMD_CFA_WRITE_MULT_NE, "CFA WRITE MULTIPLE WITHOUT ERASE" }, 21973915c3b5SRobert Hancock { ATA_CMD_REQ_SENSE_DATA, "REQUEST SENSE DATA EXT" }, 21983915c3b5SRobert Hancock { ATA_CMD_SANITIZE_DEVICE, "SANITIZE DEVICE" }, 219928a3fc22SHannes Reinecke { ATA_CMD_ZAC_MGMT_IN, "ZAC MANAGEMENT IN" }, 220027708a95SHannes Reinecke { ATA_CMD_ZAC_MGMT_OUT, "ZAC MANAGEMENT OUT" }, 22016521148cSRobert Hancock { ATA_CMD_READ_LONG, "READ LONG (with retries)" }, 22026521148cSRobert Hancock { ATA_CMD_READ_LONG_ONCE, "READ LONG (without retries)" }, 22036521148cSRobert Hancock { ATA_CMD_WRITE_LONG, "WRITE LONG (with retries)" }, 22046521148cSRobert Hancock { ATA_CMD_WRITE_LONG_ONCE, "WRITE LONG (without retries)" }, 22056521148cSRobert Hancock { ATA_CMD_RESTORE, "RECALIBRATE" }, 22066521148cSRobert Hancock { 0, NULL } /* terminate list */ 22076521148cSRobert Hancock }; 22086521148cSRobert Hancock 22096521148cSRobert Hancock unsigned int i; 22106521148cSRobert Hancock for (i = 0; cmd_descr[i].text; i++) 22116521148cSRobert Hancock if (cmd_descr[i].command == command) 22126521148cSRobert Hancock return cmd_descr[i].text; 22136521148cSRobert Hancock #endif 22146521148cSRobert Hancock 2215d4520903SHannes Reinecke return "unknown"; 22166521148cSRobert Hancock } 2217d4520903SHannes Reinecke EXPORT_SYMBOL_GPL(ata_get_cmd_name); 22186521148cSRobert Hancock 22196521148cSRobert Hancock /** 22209b1e2658STejun Heo * ata_eh_link_report - report error handling to user 22210260731fSTejun Heo * @link: ATA link EH is going on 2222c6fd2807SJeff Garzik * 2223c6fd2807SJeff Garzik * Report EH to user. 2224c6fd2807SJeff Garzik * 2225c6fd2807SJeff Garzik * LOCKING: 2226c6fd2807SJeff Garzik * None. 2227c6fd2807SJeff Garzik */ 22289b1e2658STejun Heo static void ata_eh_link_report(struct ata_link *link) 2229c6fd2807SJeff Garzik { 22300260731fSTejun Heo struct ata_port *ap = link->ap; 22310260731fSTejun Heo struct ata_eh_context *ehc = &link->eh_context; 2232258c4e5cSJens Axboe struct ata_queued_cmd *qc; 2233c6fd2807SJeff Garzik const char *frozen, *desc; 2234462098b0SLevente Kurusa char tries_buf[6] = ""; 2235c6fd2807SJeff Garzik int tag, nr_failed = 0; 2236c6fd2807SJeff Garzik 223794ff3d54STejun Heo if (ehc->i.flags & ATA_EHI_QUIET) 223894ff3d54STejun Heo return; 223994ff3d54STejun Heo 2240c6fd2807SJeff Garzik desc = NULL; 2241c6fd2807SJeff Garzik if (ehc->i.desc[0] != '\0') 2242c6fd2807SJeff Garzik desc = ehc->i.desc; 2243c6fd2807SJeff Garzik 2244258c4e5cSJens Axboe ata_qc_for_each_raw(ap, qc, tag) { 224587629312SNiklas Cassel if (!(qc->flags & ATA_QCFLAG_EH) || 2246b1c72916STejun Heo ata_dev_phys_link(qc->dev) != link || 2247e027bd36STejun Heo ((qc->flags & ATA_QCFLAG_QUIET) && 2248e027bd36STejun Heo qc->err_mask == AC_ERR_DEV)) 2249c6fd2807SJeff Garzik continue; 2250c6fd2807SJeff Garzik if (qc->flags & ATA_QCFLAG_SENSE_VALID && !qc->err_mask) 2251c6fd2807SJeff Garzik continue; 2252c6fd2807SJeff Garzik 2253c6fd2807SJeff Garzik nr_failed++; 2254c6fd2807SJeff Garzik } 2255c6fd2807SJeff Garzik 2256c6fd2807SJeff Garzik if (!nr_failed && !ehc->i.err_mask) 2257c6fd2807SJeff Garzik return; 2258c6fd2807SJeff Garzik 2259c6fd2807SJeff Garzik frozen = ""; 22604cb7c6f1SNiklas Cassel if (ata_port_is_frozen(ap)) 2261c6fd2807SJeff Garzik frozen = " frozen"; 2262c6fd2807SJeff Garzik 2263a1e10f7eSTejun Heo if (ap->eh_tries < ATA_EH_MAX_TRIES) 2264462098b0SLevente Kurusa snprintf(tries_buf, sizeof(tries_buf), " t%d", 2265a1e10f7eSTejun Heo ap->eh_tries); 2266a1e10f7eSTejun Heo 2267c6fd2807SJeff Garzik if (ehc->i.dev) { 2268a9a79dfeSJoe Perches ata_dev_err(ehc->i.dev, "exception Emask 0x%x " 2269a1e10f7eSTejun Heo "SAct 0x%x SErr 0x%x action 0x%x%s%s\n", 2270a1e10f7eSTejun Heo ehc->i.err_mask, link->sactive, ehc->i.serror, 2271a1e10f7eSTejun Heo ehc->i.action, frozen, tries_buf); 2272c6fd2807SJeff Garzik if (desc) 2273a9a79dfeSJoe Perches ata_dev_err(ehc->i.dev, "%s\n", desc); 2274c6fd2807SJeff Garzik } else { 2275a9a79dfeSJoe Perches ata_link_err(link, "exception Emask 0x%x " 2276a1e10f7eSTejun Heo "SAct 0x%x SErr 0x%x action 0x%x%s%s\n", 2277a1e10f7eSTejun Heo ehc->i.err_mask, link->sactive, ehc->i.serror, 2278a1e10f7eSTejun Heo ehc->i.action, frozen, tries_buf); 2279c6fd2807SJeff Garzik if (desc) 2280a9a79dfeSJoe Perches ata_link_err(link, "%s\n", desc); 2281c6fd2807SJeff Garzik } 2282c6fd2807SJeff Garzik 22836521148cSRobert Hancock #ifdef CONFIG_ATA_VERBOSE_ERROR 22841333e194SRobert Hancock if (ehc->i.serror) 2285a9a79dfeSJoe Perches ata_link_err(link, 22861333e194SRobert Hancock "SError: { %s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s}\n", 22871333e194SRobert Hancock ehc->i.serror & SERR_DATA_RECOVERED ? "RecovData " : "", 22881333e194SRobert Hancock ehc->i.serror & SERR_COMM_RECOVERED ? "RecovComm " : "", 22891333e194SRobert Hancock ehc->i.serror & SERR_DATA ? "UnrecovData " : "", 22901333e194SRobert Hancock ehc->i.serror & SERR_PERSISTENT ? "Persist " : "", 22911333e194SRobert Hancock ehc->i.serror & SERR_PROTOCOL ? "Proto " : "", 22921333e194SRobert Hancock ehc->i.serror & SERR_INTERNAL ? "HostInt " : "", 22931333e194SRobert Hancock ehc->i.serror & SERR_PHYRDY_CHG ? "PHYRdyChg " : "", 22941333e194SRobert Hancock ehc->i.serror & SERR_PHY_INT_ERR ? "PHYInt " : "", 22951333e194SRobert Hancock ehc->i.serror & SERR_COMM_WAKE ? "CommWake " : "", 22961333e194SRobert Hancock ehc->i.serror & SERR_10B_8B_ERR ? "10B8B " : "", 22971333e194SRobert Hancock ehc->i.serror & SERR_DISPARITY ? "Dispar " : "", 22981333e194SRobert Hancock ehc->i.serror & SERR_CRC ? "BadCRC " : "", 22991333e194SRobert Hancock ehc->i.serror & SERR_HANDSHAKE ? "Handshk " : "", 23001333e194SRobert Hancock ehc->i.serror & SERR_LINK_SEQ_ERR ? "LinkSeq " : "", 23011333e194SRobert Hancock ehc->i.serror & SERR_TRANS_ST_ERROR ? "TrStaTrns " : "", 23021333e194SRobert Hancock ehc->i.serror & SERR_UNRECOG_FIS ? "UnrecFIS " : "", 23031333e194SRobert Hancock ehc->i.serror & SERR_DEV_XCHG ? "DevExch " : ""); 23046521148cSRobert Hancock #endif 23051333e194SRobert Hancock 2306258c4e5cSJens Axboe ata_qc_for_each_raw(ap, qc, tag) { 23078a937581STejun Heo struct ata_taskfile *cmd = &qc->tf, *res = &qc->result_tf; 2308abb6a889STejun Heo char data_buf[20] = ""; 2309abb6a889STejun Heo char cdb_buf[70] = ""; 2310c6fd2807SJeff Garzik 231187629312SNiklas Cassel if (!(qc->flags & ATA_QCFLAG_EH) || 2312b1c72916STejun Heo ata_dev_phys_link(qc->dev) != link || !qc->err_mask) 2313c6fd2807SJeff Garzik continue; 2314c6fd2807SJeff Garzik 2315abb6a889STejun Heo if (qc->dma_dir != DMA_NONE) { 2316abb6a889STejun Heo static const char *dma_str[] = { 2317abb6a889STejun Heo [DMA_BIDIRECTIONAL] = "bidi", 2318abb6a889STejun Heo [DMA_TO_DEVICE] = "out", 2319abb6a889STejun Heo [DMA_FROM_DEVICE] = "in", 2320abb6a889STejun Heo }; 2321fb1b8b11SGeert Uytterhoeven const char *prot_str = NULL; 2322abb6a889STejun Heo 2323fb1b8b11SGeert Uytterhoeven switch (qc->tf.protocol) { 2324fb1b8b11SGeert Uytterhoeven case ATA_PROT_UNKNOWN: 2325fb1b8b11SGeert Uytterhoeven prot_str = "unknown"; 2326fb1b8b11SGeert Uytterhoeven break; 2327fb1b8b11SGeert Uytterhoeven case ATA_PROT_NODATA: 2328fb1b8b11SGeert Uytterhoeven prot_str = "nodata"; 2329fb1b8b11SGeert Uytterhoeven break; 2330fb1b8b11SGeert Uytterhoeven case ATA_PROT_PIO: 2331fb1b8b11SGeert Uytterhoeven prot_str = "pio"; 2332fb1b8b11SGeert Uytterhoeven break; 2333fb1b8b11SGeert Uytterhoeven case ATA_PROT_DMA: 2334fb1b8b11SGeert Uytterhoeven prot_str = "dma"; 2335fb1b8b11SGeert Uytterhoeven break; 2336fb1b8b11SGeert Uytterhoeven case ATA_PROT_NCQ: 2337fb1b8b11SGeert Uytterhoeven prot_str = "ncq dma"; 2338fb1b8b11SGeert Uytterhoeven break; 2339fb1b8b11SGeert Uytterhoeven case ATA_PROT_NCQ_NODATA: 2340fb1b8b11SGeert Uytterhoeven prot_str = "ncq nodata"; 2341fb1b8b11SGeert Uytterhoeven break; 2342fb1b8b11SGeert Uytterhoeven case ATAPI_PROT_NODATA: 2343fb1b8b11SGeert Uytterhoeven prot_str = "nodata"; 2344fb1b8b11SGeert Uytterhoeven break; 2345fb1b8b11SGeert Uytterhoeven case ATAPI_PROT_PIO: 2346fb1b8b11SGeert Uytterhoeven prot_str = "pio"; 2347fb1b8b11SGeert Uytterhoeven break; 2348fb1b8b11SGeert Uytterhoeven case ATAPI_PROT_DMA: 2349fb1b8b11SGeert Uytterhoeven prot_str = "dma"; 2350fb1b8b11SGeert Uytterhoeven break; 2351fb1b8b11SGeert Uytterhoeven } 2352abb6a889STejun Heo snprintf(data_buf, sizeof(data_buf), " %s %u %s", 2353fb1b8b11SGeert Uytterhoeven prot_str, qc->nbytes, dma_str[qc->dma_dir]); 2354abb6a889STejun Heo } 2355abb6a889STejun Heo 23566521148cSRobert Hancock if (ata_is_atapi(qc->tf.protocol)) { 2357a13b0c9dSHannes Reinecke const u8 *cdb = qc->cdb; 2358a13b0c9dSHannes Reinecke size_t cdb_len = qc->dev->cdb_len; 2359a13b0c9dSHannes Reinecke 2360cbba5b0eSHannes Reinecke if (qc->scsicmd) { 2361cbba5b0eSHannes Reinecke cdb = qc->scsicmd->cmnd; 2362cbba5b0eSHannes Reinecke cdb_len = qc->scsicmd->cmd_len; 2363cbba5b0eSHannes Reinecke } 2364cbba5b0eSHannes Reinecke __scsi_format_command(cdb_buf, sizeof(cdb_buf), 2365cbba5b0eSHannes Reinecke cdb, cdb_len); 2366d4520903SHannes Reinecke } else 2367a9a79dfeSJoe Perches ata_dev_err(qc->dev, "failed command: %s\n", 2368d4520903SHannes Reinecke ata_get_cmd_name(cmd->command)); 2369abb6a889STejun Heo 2370a9a79dfeSJoe Perches ata_dev_err(qc->dev, 23718a937581STejun Heo "cmd %02x/%02x:%02x:%02x:%02x:%02x/%02x:%02x:%02x:%02x:%02x/%02x " 2372abb6a889STejun Heo "tag %d%s\n %s" 23738a937581STejun Heo "res %02x/%02x:%02x:%02x:%02x:%02x/%02x:%02x:%02x:%02x:%02x/%02x " 23745335b729STejun Heo "Emask 0x%x (%s)%s\n", 23758a937581STejun Heo cmd->command, cmd->feature, cmd->nsect, 23768a937581STejun Heo cmd->lbal, cmd->lbam, cmd->lbah, 23778a937581STejun Heo cmd->hob_feature, cmd->hob_nsect, 23788a937581STejun Heo cmd->hob_lbal, cmd->hob_lbam, cmd->hob_lbah, 2379abb6a889STejun Heo cmd->device, qc->tag, data_buf, cdb_buf, 2380efcef265SSergey Shtylyov res->status, res->error, res->nsect, 23818a937581STejun Heo res->lbal, res->lbam, res->lbah, 23828a937581STejun Heo res->hob_feature, res->hob_nsect, 23838a937581STejun Heo res->hob_lbal, res->hob_lbam, res->hob_lbah, 23845335b729STejun Heo res->device, qc->err_mask, ata_err_string(qc->err_mask), 23855335b729STejun Heo qc->err_mask & AC_ERR_NCQ ? " <F>" : ""); 23861333e194SRobert Hancock 23876521148cSRobert Hancock #ifdef CONFIG_ATA_VERBOSE_ERROR 2388efcef265SSergey Shtylyov if (res->status & (ATA_BUSY | ATA_DRDY | ATA_DF | ATA_DRQ | 2389e87fd28cSHannes Reinecke ATA_SENSE | ATA_ERR)) { 2390efcef265SSergey Shtylyov if (res->status & ATA_BUSY) 2391a9a79dfeSJoe Perches ata_dev_err(qc->dev, "status: { Busy }\n"); 23921333e194SRobert Hancock else 2393e87fd28cSHannes Reinecke ata_dev_err(qc->dev, "status: { %s%s%s%s%s}\n", 2394efcef265SSergey Shtylyov res->status & ATA_DRDY ? "DRDY " : "", 2395efcef265SSergey Shtylyov res->status & ATA_DF ? "DF " : "", 2396efcef265SSergey Shtylyov res->status & ATA_DRQ ? "DRQ " : "", 2397efcef265SSergey Shtylyov res->status & ATA_SENSE ? "SENSE " : "", 2398efcef265SSergey Shtylyov res->status & ATA_ERR ? "ERR " : ""); 23991333e194SRobert Hancock } 24001333e194SRobert Hancock 24011333e194SRobert Hancock if (cmd->command != ATA_CMD_PACKET && 2402efcef265SSergey Shtylyov (res->error & (ATA_ICRC | ATA_UNC | ATA_AMNF | ATA_IDNF | 2403efcef265SSergey Shtylyov ATA_ABORTED))) 2404eec7e1c1SAlexey Asemov ata_dev_err(qc->dev, "error: { %s%s%s%s%s}\n", 2405efcef265SSergey Shtylyov res->error & ATA_ICRC ? "ICRC " : "", 2406efcef265SSergey Shtylyov res->error & ATA_UNC ? "UNC " : "", 2407efcef265SSergey Shtylyov res->error & ATA_AMNF ? "AMNF " : "", 2408efcef265SSergey Shtylyov res->error & ATA_IDNF ? "IDNF " : "", 2409efcef265SSergey Shtylyov res->error & ATA_ABORTED ? "ABRT " : ""); 24106521148cSRobert Hancock #endif 2411c6fd2807SJeff Garzik } 2412c6fd2807SJeff Garzik } 2413c6fd2807SJeff Garzik 24149b1e2658STejun Heo /** 24159b1e2658STejun Heo * ata_eh_report - report error handling to user 24169b1e2658STejun Heo * @ap: ATA port to report EH about 24179b1e2658STejun Heo * 24189b1e2658STejun Heo * Report EH to user. 24199b1e2658STejun Heo * 24209b1e2658STejun Heo * LOCKING: 24219b1e2658STejun Heo * None. 24229b1e2658STejun Heo */ 2423fb7fd614STejun Heo void ata_eh_report(struct ata_port *ap) 24249b1e2658STejun Heo { 24259b1e2658STejun Heo struct ata_link *link; 24269b1e2658STejun Heo 24271eca4365STejun Heo ata_for_each_link(link, ap, HOST_FIRST) 24289b1e2658STejun Heo ata_eh_link_report(link); 24299b1e2658STejun Heo } 24309b1e2658STejun Heo 2431cc0680a5STejun Heo static int ata_do_reset(struct ata_link *link, ata_reset_fn_t reset, 2432b1c72916STejun Heo unsigned int *classes, unsigned long deadline, 2433b1c72916STejun Heo bool clear_classes) 2434c6fd2807SJeff Garzik { 2435f58229f8STejun Heo struct ata_device *dev; 2436c6fd2807SJeff Garzik 2437b1c72916STejun Heo if (clear_classes) 24381eca4365STejun Heo ata_for_each_dev(dev, link, ALL) 2439f58229f8STejun Heo classes[dev->devno] = ATA_DEV_UNKNOWN; 2440c6fd2807SJeff Garzik 2441f046519fSTejun Heo return reset(link, classes, deadline); 2442c6fd2807SJeff Garzik } 2443c6fd2807SJeff Garzik 2444e8411fbaSSergei Shtylyov static int ata_eh_followup_srst_needed(struct ata_link *link, int rc) 2445c6fd2807SJeff Garzik { 244645db2f6cSTejun Heo if ((link->flags & ATA_LFLAG_NO_SRST) || ata_link_offline(link)) 2447ae791c05STejun Heo return 0; 24485dbfc9cbSTejun Heo if (rc == -EAGAIN) 2449c6fd2807SJeff Garzik return 1; 2450071f44b1STejun Heo if (sata_pmp_supported(link->ap) && ata_is_host_link(link)) 24513495de73STejun Heo return 1; 2452c6fd2807SJeff Garzik return 0; 2453c6fd2807SJeff Garzik } 2454c6fd2807SJeff Garzik 2455fb7fd614STejun Heo int ata_eh_reset(struct ata_link *link, int classify, 2456c6fd2807SJeff Garzik ata_prereset_fn_t prereset, ata_reset_fn_t softreset, 2457c6fd2807SJeff Garzik ata_reset_fn_t hardreset, ata_postreset_fn_t postreset) 2458c6fd2807SJeff Garzik { 2459afaa5c37STejun Heo struct ata_port *ap = link->ap; 2460b1c72916STejun Heo struct ata_link *slave = ap->slave_link; 2461936fd732STejun Heo struct ata_eh_context *ehc = &link->eh_context; 2462705d2014SBartlomiej Zolnierkiewicz struct ata_eh_context *sehc = slave ? &slave->eh_context : NULL; 2463c6fd2807SJeff Garzik unsigned int *classes = ehc->classes; 2464416dc9edSTejun Heo unsigned int lflags = link->flags; 2465c6fd2807SJeff Garzik int verbose = !(ehc->i.flags & ATA_EHI_QUIET); 2466d8af0eb6STejun Heo int max_tries = 0, try = 0; 2467b1c72916STejun Heo struct ata_link *failed_link; 2468f58229f8STejun Heo struct ata_device *dev; 2469416dc9edSTejun Heo unsigned long deadline, now; 2470c6fd2807SJeff Garzik ata_reset_fn_t reset; 2471afaa5c37STejun Heo unsigned long flags; 2472416dc9edSTejun Heo u32 sstatus; 2473b1c72916STejun Heo int nr_unknown, rc; 2474c6fd2807SJeff Garzik 2475932648b0STejun Heo /* 2476932648b0STejun Heo * Prepare to reset 2477932648b0STejun Heo */ 2478d8af0eb6STejun Heo while (ata_eh_reset_timeouts[max_tries] != ULONG_MAX) 2479d8af0eb6STejun Heo max_tries++; 2480ca6d43b0SDan Williams if (link->flags & ATA_LFLAG_RST_ONCE) 2481ca6d43b0SDan Williams max_tries = 1; 248205944bdfSTejun Heo if (link->flags & ATA_LFLAG_NO_HRST) 248305944bdfSTejun Heo hardreset = NULL; 248405944bdfSTejun Heo if (link->flags & ATA_LFLAG_NO_SRST) 248505944bdfSTejun Heo softreset = NULL; 2486d8af0eb6STejun Heo 248725985edcSLucas De Marchi /* make sure each reset attempt is at least COOL_DOWN apart */ 248819b72321STejun Heo if (ehc->i.flags & ATA_EHI_DID_RESET) { 24890a2c0f56STejun Heo now = jiffies; 249019b72321STejun Heo WARN_ON(time_after(ehc->last_reset, now)); 249119b72321STejun Heo deadline = ata_deadline(ehc->last_reset, 249219b72321STejun Heo ATA_EH_RESET_COOL_DOWN); 24930a2c0f56STejun Heo if (time_before(now, deadline)) 24940a2c0f56STejun Heo schedule_timeout_uninterruptible(deadline - now); 249519b72321STejun Heo } 24960a2c0f56STejun Heo 2497afaa5c37STejun Heo spin_lock_irqsave(ap->lock, flags); 2498afaa5c37STejun Heo ap->pflags |= ATA_PFLAG_RESETTING; 2499afaa5c37STejun Heo spin_unlock_irqrestore(ap->lock, flags); 2500afaa5c37STejun Heo 2501cf480626STejun Heo ata_eh_about_to_do(link, NULL, ATA_EH_RESET); 2502c6fd2807SJeff Garzik 25031eca4365STejun Heo ata_for_each_dev(dev, link, ALL) { 2504cdeab114STejun Heo /* If we issue an SRST then an ATA drive (not ATAPI) 2505cdeab114STejun Heo * may change configuration and be in PIO0 timing. If 2506cdeab114STejun Heo * we do a hard reset (or are coming from power on) 2507cdeab114STejun Heo * this is true for ATA or ATAPI. Until we've set a 2508cdeab114STejun Heo * suitable controller mode we should not touch the 2509cdeab114STejun Heo * bus as we may be talking too fast. 2510cdeab114STejun Heo */ 2511cdeab114STejun Heo dev->pio_mode = XFER_PIO_0; 25125416912aSAaron Lu dev->dma_mode = 0xff; 2513cdeab114STejun Heo 2514cdeab114STejun Heo /* If the controller has a pio mode setup function 2515cdeab114STejun Heo * then use it to set the chipset to rights. Don't 2516cdeab114STejun Heo * touch the DMA setup as that will be dealt with when 2517cdeab114STejun Heo * configuring devices. 2518cdeab114STejun Heo */ 2519cdeab114STejun Heo if (ap->ops->set_piomode) 2520cdeab114STejun Heo ap->ops->set_piomode(ap, dev); 2521cdeab114STejun Heo } 2522cdeab114STejun Heo 2523cf480626STejun Heo /* prefer hardreset */ 2524932648b0STejun Heo reset = NULL; 2525cf480626STejun Heo ehc->i.action &= ~ATA_EH_RESET; 2526cf480626STejun Heo if (hardreset) { 2527cf480626STejun Heo reset = hardreset; 2528a674050eSTejun Heo ehc->i.action |= ATA_EH_HARDRESET; 25294f7faa3fSTejun Heo } else if (softreset) { 2530cf480626STejun Heo reset = softreset; 2531a674050eSTejun Heo ehc->i.action |= ATA_EH_SOFTRESET; 2532cf480626STejun Heo } 2533c6fd2807SJeff Garzik 2534c6fd2807SJeff Garzik if (prereset) { 2535b1c72916STejun Heo unsigned long deadline = ata_deadline(jiffies, 2536b1c72916STejun Heo ATA_EH_PRERESET_TIMEOUT); 2537b1c72916STejun Heo 2538b1c72916STejun Heo if (slave) { 2539b1c72916STejun Heo sehc->i.action &= ~ATA_EH_RESET; 2540b1c72916STejun Heo sehc->i.action |= ehc->i.action; 2541b1c72916STejun Heo } 2542b1c72916STejun Heo 2543b1c72916STejun Heo rc = prereset(link, deadline); 2544b1c72916STejun Heo 2545b1c72916STejun Heo /* If present, do prereset on slave link too. Reset 2546b1c72916STejun Heo * is skipped iff both master and slave links report 2547b1c72916STejun Heo * -ENOENT or clear ATA_EH_RESET. 2548b1c72916STejun Heo */ 2549b1c72916STejun Heo if (slave && (rc == 0 || rc == -ENOENT)) { 2550b1c72916STejun Heo int tmp; 2551b1c72916STejun Heo 2552b1c72916STejun Heo tmp = prereset(slave, deadline); 2553b1c72916STejun Heo if (tmp != -ENOENT) 2554b1c72916STejun Heo rc = tmp; 2555b1c72916STejun Heo 2556b1c72916STejun Heo ehc->i.action |= sehc->i.action; 2557b1c72916STejun Heo } 2558b1c72916STejun Heo 2559c6fd2807SJeff Garzik if (rc) { 2560c961922bSAlan Cox if (rc == -ENOENT) { 2561a9a79dfeSJoe Perches ata_link_dbg(link, "port disabled--ignoring\n"); 2562cf480626STejun Heo ehc->i.action &= ~ATA_EH_RESET; 25634aa9ab67STejun Heo 25641eca4365STejun Heo ata_for_each_dev(dev, link, ALL) 2565f58229f8STejun Heo classes[dev->devno] = ATA_DEV_NONE; 25664aa9ab67STejun Heo 25674aa9ab67STejun Heo rc = 0; 2568c961922bSAlan Cox } else 2569a9a79dfeSJoe Perches ata_link_err(link, 2570a9a79dfeSJoe Perches "prereset failed (errno=%d)\n", 2571a9a79dfeSJoe Perches rc); 2572fccb6ea5STejun Heo goto out; 2573c6fd2807SJeff Garzik } 2574c6fd2807SJeff Garzik 2575932648b0STejun Heo /* prereset() might have cleared ATA_EH_RESET. If so, 2576d6515e6fSTejun Heo * bang classes, thaw and return. 2577932648b0STejun Heo */ 2578932648b0STejun Heo if (reset && !(ehc->i.action & ATA_EH_RESET)) { 25791eca4365STejun Heo ata_for_each_dev(dev, link, ALL) 2580f58229f8STejun Heo classes[dev->devno] = ATA_DEV_NONE; 25814cb7c6f1SNiklas Cassel if (ata_port_is_frozen(ap) && ata_is_host_link(link)) 2582d6515e6fSTejun Heo ata_eh_thaw_port(ap); 2583fccb6ea5STejun Heo rc = 0; 2584fccb6ea5STejun Heo goto out; 2585c6fd2807SJeff Garzik } 2586932648b0STejun Heo } 2587c6fd2807SJeff Garzik 2588c6fd2807SJeff Garzik retry: 2589932648b0STejun Heo /* 2590932648b0STejun Heo * Perform reset 2591932648b0STejun Heo */ 2592dc98c32cSTejun Heo if (ata_is_host_link(link)) 2593dc98c32cSTejun Heo ata_eh_freeze_port(ap); 2594dc98c32cSTejun Heo 2595341c2c95STejun Heo deadline = ata_deadline(jiffies, ata_eh_reset_timeouts[try++]); 259631daabdaSTejun Heo 2597932648b0STejun Heo if (reset) { 2598c6fd2807SJeff Garzik if (verbose) 2599a9a79dfeSJoe Perches ata_link_info(link, "%s resetting link\n", 2600c6fd2807SJeff Garzik reset == softreset ? "soft" : "hard"); 2601c6fd2807SJeff Garzik 2602c6fd2807SJeff Garzik /* mark that this EH session started with reset */ 260319b72321STejun Heo ehc->last_reset = jiffies; 2604f8ec26d0SHannes Reinecke if (reset == hardreset) { 26050d64a233STejun Heo ehc->i.flags |= ATA_EHI_DID_HARDRESET; 2606f8ec26d0SHannes Reinecke trace_ata_link_hardreset_begin(link, classes, deadline); 2607f8ec26d0SHannes Reinecke } else { 26080d64a233STejun Heo ehc->i.flags |= ATA_EHI_DID_SOFTRESET; 2609f8ec26d0SHannes Reinecke trace_ata_link_softreset_begin(link, classes, deadline); 2610f8ec26d0SHannes Reinecke } 2611c6fd2807SJeff Garzik 2612b1c72916STejun Heo rc = ata_do_reset(link, reset, classes, deadline, true); 2613f8ec26d0SHannes Reinecke if (reset == hardreset) 2614f8ec26d0SHannes Reinecke trace_ata_link_hardreset_end(link, classes, rc); 2615f8ec26d0SHannes Reinecke else 2616f8ec26d0SHannes Reinecke trace_ata_link_softreset_end(link, classes, rc); 2617b1c72916STejun Heo if (rc && rc != -EAGAIN) { 2618b1c72916STejun Heo failed_link = link; 26195dbfc9cbSTejun Heo goto fail; 2620b1c72916STejun Heo } 2621c6fd2807SJeff Garzik 2622b1c72916STejun Heo /* hardreset slave link if existent */ 2623b1c72916STejun Heo if (slave && reset == hardreset) { 2624b1c72916STejun Heo int tmp; 2625b1c72916STejun Heo 2626b1c72916STejun Heo if (verbose) 2627a9a79dfeSJoe Perches ata_link_info(slave, "hard resetting link\n"); 2628b1c72916STejun Heo 2629b1c72916STejun Heo ata_eh_about_to_do(slave, NULL, ATA_EH_RESET); 2630f8ec26d0SHannes Reinecke trace_ata_slave_hardreset_begin(slave, classes, 2631f8ec26d0SHannes Reinecke deadline); 2632b1c72916STejun Heo tmp = ata_do_reset(slave, reset, classes, deadline, 2633b1c72916STejun Heo false); 2634f8ec26d0SHannes Reinecke trace_ata_slave_hardreset_end(slave, classes, tmp); 2635b1c72916STejun Heo switch (tmp) { 2636b1c72916STejun Heo case -EAGAIN: 2637b1c72916STejun Heo rc = -EAGAIN; 2638e06abcc6SGustavo A. R. Silva break; 2639b1c72916STejun Heo case 0: 2640b1c72916STejun Heo break; 2641b1c72916STejun Heo default: 2642b1c72916STejun Heo failed_link = slave; 2643b1c72916STejun Heo rc = tmp; 2644b1c72916STejun Heo goto fail; 2645b1c72916STejun Heo } 2646b1c72916STejun Heo } 2647b1c72916STejun Heo 2648b1c72916STejun Heo /* perform follow-up SRST if necessary */ 2649c6fd2807SJeff Garzik if (reset == hardreset && 2650e8411fbaSSergei Shtylyov ata_eh_followup_srst_needed(link, rc)) { 2651c6fd2807SJeff Garzik reset = softreset; 2652c6fd2807SJeff Garzik 2653c6fd2807SJeff Garzik if (!reset) { 2654a9a79dfeSJoe Perches ata_link_err(link, 2655a9a79dfeSJoe Perches "follow-up softreset required but no softreset available\n"); 2656b1c72916STejun Heo failed_link = link; 2657fccb6ea5STejun Heo rc = -EINVAL; 265808cf69d0STejun Heo goto fail; 2659c6fd2807SJeff Garzik } 2660c6fd2807SJeff Garzik 2661cf480626STejun Heo ata_eh_about_to_do(link, NULL, ATA_EH_RESET); 2662f8ec26d0SHannes Reinecke trace_ata_link_softreset_begin(link, classes, deadline); 2663b1c72916STejun Heo rc = ata_do_reset(link, reset, classes, deadline, true); 2664f8ec26d0SHannes Reinecke trace_ata_link_softreset_end(link, classes, rc); 2665fe2c4d01STejun Heo if (rc) { 2666fe2c4d01STejun Heo failed_link = link; 2667fe2c4d01STejun Heo goto fail; 2668fe2c4d01STejun Heo } 2669c6fd2807SJeff Garzik } 2670932648b0STejun Heo } else { 2671932648b0STejun Heo if (verbose) 2672a9a79dfeSJoe Perches ata_link_info(link, 2673a9a79dfeSJoe Perches "no reset method available, skipping reset\n"); 2674932648b0STejun Heo if (!(lflags & ATA_LFLAG_ASSUME_CLASS)) 2675932648b0STejun Heo lflags |= ATA_LFLAG_ASSUME_ATA; 2676932648b0STejun Heo } 2677008a7896STejun Heo 2678932648b0STejun Heo /* 2679932648b0STejun Heo * Post-reset processing 2680932648b0STejun Heo */ 26811eca4365STejun Heo ata_for_each_dev(dev, link, ALL) { 2682416dc9edSTejun Heo /* After the reset, the device state is PIO 0 and the 2683416dc9edSTejun Heo * controller state is undefined. Reset also wakes up 2684416dc9edSTejun Heo * drives from sleeping mode. 2685c6fd2807SJeff Garzik */ 2686f58229f8STejun Heo dev->pio_mode = XFER_PIO_0; 2687054a5fbaSTejun Heo dev->flags &= ~ATA_DFLAG_SLEEPING; 2688c6fd2807SJeff Garzik 26893b761d3dSTejun Heo if (ata_phys_link_offline(ata_dev_phys_link(dev))) 26903b761d3dSTejun Heo continue; 26913b761d3dSTejun Heo 26924ccd3329STejun Heo /* apply class override */ 2693416dc9edSTejun Heo if (lflags & ATA_LFLAG_ASSUME_ATA) 2694ae791c05STejun Heo classes[dev->devno] = ATA_DEV_ATA; 2695416dc9edSTejun Heo else if (lflags & ATA_LFLAG_ASSUME_SEMB) 2696816ab897STejun Heo classes[dev->devno] = ATA_DEV_SEMB_UNSUP; 2697ae791c05STejun Heo } 2698ae791c05STejun Heo 2699008a7896STejun Heo /* record current link speed */ 2700936fd732STejun Heo if (sata_scr_read(link, SCR_STATUS, &sstatus) == 0) 2701936fd732STejun Heo link->sata_spd = (sstatus >> 4) & 0xf; 2702b1c72916STejun Heo if (slave && sata_scr_read(slave, SCR_STATUS, &sstatus) == 0) 2703b1c72916STejun Heo slave->sata_spd = (sstatus >> 4) & 0xf; 2704008a7896STejun Heo 2705dc98c32cSTejun Heo /* thaw the port */ 2706dc98c32cSTejun Heo if (ata_is_host_link(link)) 2707dc98c32cSTejun Heo ata_eh_thaw_port(ap); 2708dc98c32cSTejun Heo 2709f046519fSTejun Heo /* postreset() should clear hardware SError. Although SError 2710f046519fSTejun Heo * is cleared during link resume, clearing SError here is 2711f046519fSTejun Heo * necessary as some PHYs raise hotplug events after SRST. 2712f046519fSTejun Heo * This introduces race condition where hotplug occurs between 2713f046519fSTejun Heo * reset and here. This race is mediated by cross checking 2714f046519fSTejun Heo * link onlineness and classification result later. 2715f046519fSTejun Heo */ 2716b1c72916STejun Heo if (postreset) { 2717cc0680a5STejun Heo postreset(link, classes); 2718f8ec26d0SHannes Reinecke trace_ata_link_postreset(link, classes, rc); 2719f8ec26d0SHannes Reinecke if (slave) { 2720b1c72916STejun Heo postreset(slave, classes); 2721f8ec26d0SHannes Reinecke trace_ata_slave_postreset(slave, classes, rc); 2722f8ec26d0SHannes Reinecke } 2723b1c72916STejun Heo } 2724c6fd2807SJeff Garzik 27251e641060STejun Heo /* 27268c56caccSTejun Heo * Some controllers can't be frozen very well and may set spurious 27278c56caccSTejun Heo * error conditions during reset. Clear accumulated error 27288c56caccSTejun Heo * information and re-thaw the port if frozen. As reset is the 27298c56caccSTejun Heo * final recovery action and we cross check link onlineness against 27308c56caccSTejun Heo * device classification later, no hotplug event is lost by this. 27311e641060STejun Heo */ 2732f046519fSTejun Heo spin_lock_irqsave(link->ap->lock, flags); 27331e641060STejun Heo memset(&link->eh_info, 0, sizeof(link->eh_info)); 2734b1c72916STejun Heo if (slave) 27351e641060STejun Heo memset(&slave->eh_info, 0, sizeof(link->eh_info)); 27361e641060STejun Heo ap->pflags &= ~ATA_PFLAG_EH_PENDING; 2737f046519fSTejun Heo spin_unlock_irqrestore(link->ap->lock, flags); 2738f046519fSTejun Heo 27394cb7c6f1SNiklas Cassel if (ata_port_is_frozen(ap)) 27408c56caccSTejun Heo ata_eh_thaw_port(ap); 27418c56caccSTejun Heo 27423b761d3dSTejun Heo /* 27433b761d3dSTejun Heo * Make sure onlineness and classification result correspond. 2744f046519fSTejun Heo * Hotplug could have happened during reset and some 2745f046519fSTejun Heo * controllers fail to wait while a drive is spinning up after 2746f046519fSTejun Heo * being hotplugged causing misdetection. By cross checking 27473b761d3dSTejun Heo * link on/offlineness and classification result, those 27483b761d3dSTejun Heo * conditions can be reliably detected and retried. 2749f046519fSTejun Heo */ 2750b1c72916STejun Heo nr_unknown = 0; 27511eca4365STejun Heo ata_for_each_dev(dev, link, ALL) { 27523b761d3dSTejun Heo if (ata_phys_link_online(ata_dev_phys_link(dev))) { 2753b1c72916STejun Heo if (classes[dev->devno] == ATA_DEV_UNKNOWN) { 2754a9a79dfeSJoe Perches ata_dev_dbg(dev, "link online but device misclassified\n"); 2755f046519fSTejun Heo classes[dev->devno] = ATA_DEV_NONE; 2756b1c72916STejun Heo nr_unknown++; 2757b1c72916STejun Heo } 27583b761d3dSTejun Heo } else if (ata_phys_link_offline(ata_dev_phys_link(dev))) { 27593b761d3dSTejun Heo if (ata_class_enabled(classes[dev->devno])) 2760a9a79dfeSJoe Perches ata_dev_dbg(dev, 2761a9a79dfeSJoe Perches "link offline, clearing class %d to NONE\n", 27623b761d3dSTejun Heo classes[dev->devno]); 27633b761d3dSTejun Heo classes[dev->devno] = ATA_DEV_NONE; 27643b761d3dSTejun Heo } else if (classes[dev->devno] == ATA_DEV_UNKNOWN) { 2765a9a79dfeSJoe Perches ata_dev_dbg(dev, 2766a9a79dfeSJoe Perches "link status unknown, clearing UNKNOWN to NONE\n"); 27673b761d3dSTejun Heo classes[dev->devno] = ATA_DEV_NONE; 27683b761d3dSTejun Heo } 2769f046519fSTejun Heo } 2770f046519fSTejun Heo 2771b1c72916STejun Heo if (classify && nr_unknown) { 2772f046519fSTejun Heo if (try < max_tries) { 2773a9a79dfeSJoe Perches ata_link_warn(link, 2774a9a79dfeSJoe Perches "link online but %d devices misclassified, retrying\n", 27753b761d3dSTejun Heo nr_unknown); 2776b1c72916STejun Heo failed_link = link; 2777f046519fSTejun Heo rc = -EAGAIN; 2778f046519fSTejun Heo goto fail; 2779f046519fSTejun Heo } 2780a9a79dfeSJoe Perches ata_link_warn(link, 27813b761d3dSTejun Heo "link online but %d devices misclassified, " 27823b761d3dSTejun Heo "device detection might fail\n", nr_unknown); 2783f046519fSTejun Heo } 2784f046519fSTejun Heo 2785c6fd2807SJeff Garzik /* reset successful, schedule revalidation */ 2786cf480626STejun Heo ata_eh_done(link, NULL, ATA_EH_RESET); 2787b1c72916STejun Heo if (slave) 2788b1c72916STejun Heo ata_eh_done(slave, NULL, ATA_EH_RESET); 278919b72321STejun Heo ehc->last_reset = jiffies; /* update to completion time */ 2790c6fd2807SJeff Garzik ehc->i.action |= ATA_EH_REVALIDATE; 27916b7ae954STejun Heo link->lpm_policy = ATA_LPM_UNKNOWN; /* reset LPM state */ 2792416dc9edSTejun Heo 2793416dc9edSTejun Heo rc = 0; 2794fccb6ea5STejun Heo out: 2795fccb6ea5STejun Heo /* clear hotplug flag */ 2796fccb6ea5STejun Heo ehc->i.flags &= ~ATA_EHI_HOTPLUGGED; 2797b1c72916STejun Heo if (slave) 2798b1c72916STejun Heo sehc->i.flags &= ~ATA_EHI_HOTPLUGGED; 2799afaa5c37STejun Heo 2800afaa5c37STejun Heo spin_lock_irqsave(ap->lock, flags); 2801afaa5c37STejun Heo ap->pflags &= ~ATA_PFLAG_RESETTING; 2802afaa5c37STejun Heo spin_unlock_irqrestore(ap->lock, flags); 2803afaa5c37STejun Heo 2804c6fd2807SJeff Garzik return rc; 2805416dc9edSTejun Heo 2806416dc9edSTejun Heo fail: 28075958e302STejun Heo /* if SCR isn't accessible on a fan-out port, PMP needs to be reset */ 28085958e302STejun Heo if (!ata_is_host_link(link) && 28095958e302STejun Heo sata_scr_read(link, SCR_STATUS, &sstatus)) 28105958e302STejun Heo rc = -ERESTART; 28115958e302STejun Heo 28127a46c078SGwendal Grignou if (try >= max_tries) { 28138ea7645cSTejun Heo /* 28148ea7645cSTejun Heo * Thaw host port even if reset failed, so that the port 28158ea7645cSTejun Heo * can be retried on the next phy event. This risks 28168ea7645cSTejun Heo * repeated EH runs but seems to be a better tradeoff than 28178ea7645cSTejun Heo * shutting down a port after a botched hotplug attempt. 28188ea7645cSTejun Heo */ 28198ea7645cSTejun Heo if (ata_is_host_link(link)) 28208ea7645cSTejun Heo ata_eh_thaw_port(ap); 2821416dc9edSTejun Heo goto out; 28228ea7645cSTejun Heo } 2823416dc9edSTejun Heo 2824416dc9edSTejun Heo now = jiffies; 2825416dc9edSTejun Heo if (time_before(now, deadline)) { 2826416dc9edSTejun Heo unsigned long delta = deadline - now; 2827416dc9edSTejun Heo 2828a9a79dfeSJoe Perches ata_link_warn(failed_link, 28290a2c0f56STejun Heo "reset failed (errno=%d), retrying in %u secs\n", 28300a2c0f56STejun Heo rc, DIV_ROUND_UP(jiffies_to_msecs(delta), 1000)); 2831416dc9edSTejun Heo 2832c0c362b6STejun Heo ata_eh_release(ap); 2833416dc9edSTejun Heo while (delta) 2834416dc9edSTejun Heo delta = schedule_timeout_uninterruptible(delta); 2835c0c362b6STejun Heo ata_eh_acquire(ap); 2836416dc9edSTejun Heo } 2837416dc9edSTejun Heo 28387a46c078SGwendal Grignou /* 28397a46c078SGwendal Grignou * While disks spinup behind PMP, some controllers fail sending SRST. 28407a46c078SGwendal Grignou * They need to be reset - as well as the PMP - before retrying. 28417a46c078SGwendal Grignou */ 28427a46c078SGwendal Grignou if (rc == -ERESTART) { 28437a46c078SGwendal Grignou if (ata_is_host_link(link)) 28447a46c078SGwendal Grignou ata_eh_thaw_port(ap); 28457a46c078SGwendal Grignou goto out; 28467a46c078SGwendal Grignou } 28477a46c078SGwendal Grignou 2848b1c72916STejun Heo if (try == max_tries - 1) { 2849a07d499bSTejun Heo sata_down_spd_limit(link, 0); 2850b1c72916STejun Heo if (slave) 2851a07d499bSTejun Heo sata_down_spd_limit(slave, 0); 2852b1c72916STejun Heo } else if (rc == -EPIPE) 2853a07d499bSTejun Heo sata_down_spd_limit(failed_link, 0); 2854b1c72916STejun Heo 2855416dc9edSTejun Heo if (hardreset) 2856416dc9edSTejun Heo reset = hardreset; 2857416dc9edSTejun Heo goto retry; 2858c6fd2807SJeff Garzik } 2859c6fd2807SJeff Garzik 286045fabbb7SElias Oltmanns static inline void ata_eh_pull_park_action(struct ata_port *ap) 286145fabbb7SElias Oltmanns { 286245fabbb7SElias Oltmanns struct ata_link *link; 286345fabbb7SElias Oltmanns struct ata_device *dev; 286445fabbb7SElias Oltmanns unsigned long flags; 286545fabbb7SElias Oltmanns 286645fabbb7SElias Oltmanns /* 286745fabbb7SElias Oltmanns * This function can be thought of as an extended version of 286845fabbb7SElias Oltmanns * ata_eh_about_to_do() specially crafted to accommodate the 286945fabbb7SElias Oltmanns * requirements of ATA_EH_PARK handling. Since the EH thread 287045fabbb7SElias Oltmanns * does not leave the do {} while () loop in ata_eh_recover as 287145fabbb7SElias Oltmanns * long as the timeout for a park request to *one* device on 287245fabbb7SElias Oltmanns * the port has not expired, and since we still want to pick 287345fabbb7SElias Oltmanns * up park requests to other devices on the same port or 287445fabbb7SElias Oltmanns * timeout updates for the same device, we have to pull 287545fabbb7SElias Oltmanns * ATA_EH_PARK actions from eh_info into eh_context.i 287645fabbb7SElias Oltmanns * ourselves at the beginning of each pass over the loop. 287745fabbb7SElias Oltmanns * 287845fabbb7SElias Oltmanns * Additionally, all write accesses to &ap->park_req_pending 287916735d02SWolfram Sang * through reinit_completion() (see below) or complete_all() 288045fabbb7SElias Oltmanns * (see ata_scsi_park_store()) are protected by the host lock. 288145fabbb7SElias Oltmanns * As a result we have that park_req_pending.done is zero on 288245fabbb7SElias Oltmanns * exit from this function, i.e. when ATA_EH_PARK actions for 288345fabbb7SElias Oltmanns * *all* devices on port ap have been pulled into the 288445fabbb7SElias Oltmanns * respective eh_context structs. If, and only if, 288545fabbb7SElias Oltmanns * park_req_pending.done is non-zero by the time we reach 288645fabbb7SElias Oltmanns * wait_for_completion_timeout(), another ATA_EH_PARK action 288745fabbb7SElias Oltmanns * has been scheduled for at least one of the devices on port 288845fabbb7SElias Oltmanns * ap and we have to cycle over the do {} while () loop in 288945fabbb7SElias Oltmanns * ata_eh_recover() again. 289045fabbb7SElias Oltmanns */ 289145fabbb7SElias Oltmanns 289245fabbb7SElias Oltmanns spin_lock_irqsave(ap->lock, flags); 289316735d02SWolfram Sang reinit_completion(&ap->park_req_pending); 28941eca4365STejun Heo ata_for_each_link(link, ap, EDGE) { 28951eca4365STejun Heo ata_for_each_dev(dev, link, ALL) { 289645fabbb7SElias Oltmanns struct ata_eh_info *ehi = &link->eh_info; 289745fabbb7SElias Oltmanns 289845fabbb7SElias Oltmanns link->eh_context.i.dev_action[dev->devno] |= 289945fabbb7SElias Oltmanns ehi->dev_action[dev->devno] & ATA_EH_PARK; 290045fabbb7SElias Oltmanns ata_eh_clear_action(link, dev, ehi, ATA_EH_PARK); 290145fabbb7SElias Oltmanns } 290245fabbb7SElias Oltmanns } 290345fabbb7SElias Oltmanns spin_unlock_irqrestore(ap->lock, flags); 290445fabbb7SElias Oltmanns } 290545fabbb7SElias Oltmanns 290645fabbb7SElias Oltmanns static void ata_eh_park_issue_cmd(struct ata_device *dev, int park) 290745fabbb7SElias Oltmanns { 290845fabbb7SElias Oltmanns struct ata_eh_context *ehc = &dev->link->eh_context; 290945fabbb7SElias Oltmanns struct ata_taskfile tf; 291045fabbb7SElias Oltmanns unsigned int err_mask; 291145fabbb7SElias Oltmanns 291245fabbb7SElias Oltmanns ata_tf_init(dev, &tf); 291345fabbb7SElias Oltmanns if (park) { 291445fabbb7SElias Oltmanns ehc->unloaded_mask |= 1 << dev->devno; 291545fabbb7SElias Oltmanns tf.command = ATA_CMD_IDLEIMMEDIATE; 291645fabbb7SElias Oltmanns tf.feature = 0x44; 291745fabbb7SElias Oltmanns tf.lbal = 0x4c; 291845fabbb7SElias Oltmanns tf.lbam = 0x4e; 291945fabbb7SElias Oltmanns tf.lbah = 0x55; 292045fabbb7SElias Oltmanns } else { 292145fabbb7SElias Oltmanns ehc->unloaded_mask &= ~(1 << dev->devno); 292245fabbb7SElias Oltmanns tf.command = ATA_CMD_CHK_POWER; 292345fabbb7SElias Oltmanns } 292445fabbb7SElias Oltmanns 292545fabbb7SElias Oltmanns tf.flags |= ATA_TFLAG_DEVICE | ATA_TFLAG_ISADDR; 2926bd18bc04SHannes Reinecke tf.protocol = ATA_PROT_NODATA; 292745fabbb7SElias Oltmanns err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0); 292845fabbb7SElias Oltmanns if (park && (err_mask || tf.lbal != 0xc4)) { 2929a9a79dfeSJoe Perches ata_dev_err(dev, "head unload failed!\n"); 293045fabbb7SElias Oltmanns ehc->unloaded_mask &= ~(1 << dev->devno); 293145fabbb7SElias Oltmanns } 293245fabbb7SElias Oltmanns } 293345fabbb7SElias Oltmanns 29340260731fSTejun Heo static int ata_eh_revalidate_and_attach(struct ata_link *link, 2935c6fd2807SJeff Garzik struct ata_device **r_failed_dev) 2936c6fd2807SJeff Garzik { 29370260731fSTejun Heo struct ata_port *ap = link->ap; 29380260731fSTejun Heo struct ata_eh_context *ehc = &link->eh_context; 2939c6fd2807SJeff Garzik struct ata_device *dev; 29408c3c52a8STejun Heo unsigned int new_mask = 0; 2941c6fd2807SJeff Garzik unsigned long flags; 2942f58229f8STejun Heo int rc = 0; 2943c6fd2807SJeff Garzik 29448c3c52a8STejun Heo /* For PATA drive side cable detection to work, IDENTIFY must 29458c3c52a8STejun Heo * be done backwards such that PDIAG- is released by the slave 29468c3c52a8STejun Heo * device before the master device is identified. 29478c3c52a8STejun Heo */ 29481eca4365STejun Heo ata_for_each_dev(dev, link, ALL_REVERSE) { 2949f58229f8STejun Heo unsigned int action = ata_eh_dev_action(dev); 2950f58229f8STejun Heo unsigned int readid_flags = 0; 2951c6fd2807SJeff Garzik 2952bff04647STejun Heo if (ehc->i.flags & ATA_EHI_DID_RESET) 2953bff04647STejun Heo readid_flags |= ATA_READID_POSTRESET; 2954bff04647STejun Heo 29559666f400STejun Heo if ((action & ATA_EH_REVALIDATE) && ata_dev_enabled(dev)) { 2956633273a3STejun Heo WARN_ON(dev->class == ATA_DEV_PMP); 2957633273a3STejun Heo 295871d7b6e5SNiklas Cassel /* 295971d7b6e5SNiklas Cassel * The link may be in a deep sleep, wake it up. 296071d7b6e5SNiklas Cassel * 296171d7b6e5SNiklas Cassel * If the link is in deep sleep, ata_phys_link_offline() 296271d7b6e5SNiklas Cassel * will return true, causing the revalidation to fail, 296371d7b6e5SNiklas Cassel * which leads to a (potentially) needless hard reset. 296471d7b6e5SNiklas Cassel * 296571d7b6e5SNiklas Cassel * ata_eh_recover() will later restore the link policy 296671d7b6e5SNiklas Cassel * to ap->target_lpm_policy after revalidation is done. 296771d7b6e5SNiklas Cassel */ 296871d7b6e5SNiklas Cassel if (link->lpm_policy > ATA_LPM_MAX_POWER) { 296971d7b6e5SNiklas Cassel rc = ata_eh_set_lpm(link, ATA_LPM_MAX_POWER, 297071d7b6e5SNiklas Cassel r_failed_dev); 297171d7b6e5SNiklas Cassel if (rc) 297271d7b6e5SNiklas Cassel goto err; 297371d7b6e5SNiklas Cassel } 297471d7b6e5SNiklas Cassel 2975b1c72916STejun Heo if (ata_phys_link_offline(ata_dev_phys_link(dev))) { 2976c6fd2807SJeff Garzik rc = -EIO; 29778c3c52a8STejun Heo goto err; 2978c6fd2807SJeff Garzik } 2979c6fd2807SJeff Garzik 29800260731fSTejun Heo ata_eh_about_to_do(link, dev, ATA_EH_REVALIDATE); 2981422c9daaSTejun Heo rc = ata_dev_revalidate(dev, ehc->classes[dev->devno], 2982422c9daaSTejun Heo readid_flags); 2983c6fd2807SJeff Garzik if (rc) 29848c3c52a8STejun Heo goto err; 2985c6fd2807SJeff Garzik 29860260731fSTejun Heo ata_eh_done(link, dev, ATA_EH_REVALIDATE); 2987c6fd2807SJeff Garzik 2988baa1e78aSTejun Heo /* Configuration may have changed, reconfigure 2989baa1e78aSTejun Heo * transfer mode. 2990baa1e78aSTejun Heo */ 2991baa1e78aSTejun Heo ehc->i.flags |= ATA_EHI_SETMODE; 2992baa1e78aSTejun Heo 2993c6fd2807SJeff Garzik /* schedule the scsi_rescan_device() here */ 2994ad72cf98STejun Heo schedule_work(&(ap->scsi_rescan_task)); 2995c6fd2807SJeff Garzik } else if (dev->class == ATA_DEV_UNKNOWN && 2996c6fd2807SJeff Garzik ehc->tries[dev->devno] && 2997c6fd2807SJeff Garzik ata_class_enabled(ehc->classes[dev->devno])) { 2998842faa6cSTejun Heo /* Temporarily set dev->class, it will be 2999842faa6cSTejun Heo * permanently set once all configurations are 3000842faa6cSTejun Heo * complete. This is necessary because new 3001842faa6cSTejun Heo * device configuration is done in two 3002842faa6cSTejun Heo * separate loops. 3003842faa6cSTejun Heo */ 3004c6fd2807SJeff Garzik dev->class = ehc->classes[dev->devno]; 3005c6fd2807SJeff Garzik 3006633273a3STejun Heo if (dev->class == ATA_DEV_PMP) 3007633273a3STejun Heo rc = sata_pmp_attach(dev); 3008633273a3STejun Heo else 3009633273a3STejun Heo rc = ata_dev_read_id(dev, &dev->class, 3010633273a3STejun Heo readid_flags, dev->id); 3011842faa6cSTejun Heo 3012842faa6cSTejun Heo /* read_id might have changed class, store and reset */ 3013842faa6cSTejun Heo ehc->classes[dev->devno] = dev->class; 3014842faa6cSTejun Heo dev->class = ATA_DEV_UNKNOWN; 3015842faa6cSTejun Heo 30168c3c52a8STejun Heo switch (rc) { 30178c3c52a8STejun Heo case 0: 301899cf610aSTejun Heo /* clear error info accumulated during probe */ 301999cf610aSTejun Heo ata_ering_clear(&dev->ering); 3020f58229f8STejun Heo new_mask |= 1 << dev->devno; 30218c3c52a8STejun Heo break; 30228c3c52a8STejun Heo case -ENOENT: 302355a8e2c8STejun Heo /* IDENTIFY was issued to non-existent 302455a8e2c8STejun Heo * device. No need to reset. Just 3025842faa6cSTejun Heo * thaw and ignore the device. 302655a8e2c8STejun Heo */ 302755a8e2c8STejun Heo ata_eh_thaw_port(ap); 3028c6fd2807SJeff Garzik break; 30298c3c52a8STejun Heo default: 30308c3c52a8STejun Heo goto err; 30318c3c52a8STejun Heo } 30328c3c52a8STejun Heo } 3033c6fd2807SJeff Garzik } 3034c6fd2807SJeff Garzik 3035c1c4e8d5STejun Heo /* PDIAG- should have been released, ask cable type if post-reset */ 303633267325STejun Heo if ((ehc->i.flags & ATA_EHI_DID_RESET) && ata_is_host_link(link)) { 303733267325STejun Heo if (ap->ops->cable_detect) 3038c1c4e8d5STejun Heo ap->cbl = ap->ops->cable_detect(ap); 303933267325STejun Heo ata_force_cbl(ap); 304033267325STejun Heo } 3041c1c4e8d5STejun Heo 30428c3c52a8STejun Heo /* Configure new devices forward such that user doesn't see 30438c3c52a8STejun Heo * device detection messages backwards. 30448c3c52a8STejun Heo */ 30451eca4365STejun Heo ata_for_each_dev(dev, link, ALL) { 30464f7c2874STejun Heo if (!(new_mask & (1 << dev->devno))) 30478c3c52a8STejun Heo continue; 30488c3c52a8STejun Heo 3049842faa6cSTejun Heo dev->class = ehc->classes[dev->devno]; 3050842faa6cSTejun Heo 30514f7c2874STejun Heo if (dev->class == ATA_DEV_PMP) 30524f7c2874STejun Heo continue; 30534f7c2874STejun Heo 30548c3c52a8STejun Heo ehc->i.flags |= ATA_EHI_PRINTINFO; 30558c3c52a8STejun Heo rc = ata_dev_configure(dev); 30568c3c52a8STejun Heo ehc->i.flags &= ~ATA_EHI_PRINTINFO; 3057842faa6cSTejun Heo if (rc) { 3058842faa6cSTejun Heo dev->class = ATA_DEV_UNKNOWN; 30598c3c52a8STejun Heo goto err; 3060842faa6cSTejun Heo } 30618c3c52a8STejun Heo 3062c6fd2807SJeff Garzik spin_lock_irqsave(ap->lock, flags); 3063c6fd2807SJeff Garzik ap->pflags |= ATA_PFLAG_SCSI_HOTPLUG; 3064c6fd2807SJeff Garzik spin_unlock_irqrestore(ap->lock, flags); 3065baa1e78aSTejun Heo 306655a8e2c8STejun Heo /* new device discovered, configure xfermode */ 3067baa1e78aSTejun Heo ehc->i.flags |= ATA_EHI_SETMODE; 3068c6fd2807SJeff Garzik } 3069c6fd2807SJeff Garzik 30708c3c52a8STejun Heo return 0; 30718c3c52a8STejun Heo 30728c3c52a8STejun Heo err: 3073c6fd2807SJeff Garzik *r_failed_dev = dev; 3074c6fd2807SJeff Garzik return rc; 3075c6fd2807SJeff Garzik } 3076c6fd2807SJeff Garzik 30776f1d1e3aSTejun Heo /** 30786f1d1e3aSTejun Heo * ata_set_mode - Program timings and issue SET FEATURES - XFER 30796f1d1e3aSTejun Heo * @link: link on which timings will be programmed 308098a1708dSMartin Olsson * @r_failed_dev: out parameter for failed device 30816f1d1e3aSTejun Heo * 30826f1d1e3aSTejun Heo * Set ATA device disk transfer mode (PIO3, UDMA6, etc.). If 30836f1d1e3aSTejun Heo * ata_set_mode() fails, pointer to the failing device is 30846f1d1e3aSTejun Heo * returned in @r_failed_dev. 30856f1d1e3aSTejun Heo * 30866f1d1e3aSTejun Heo * LOCKING: 30876f1d1e3aSTejun Heo * PCI/etc. bus probe sem. 30886f1d1e3aSTejun Heo * 30896f1d1e3aSTejun Heo * RETURNS: 30906f1d1e3aSTejun Heo * 0 on success, negative errno otherwise 30916f1d1e3aSTejun Heo */ 30926f1d1e3aSTejun Heo int ata_set_mode(struct ata_link *link, struct ata_device **r_failed_dev) 30936f1d1e3aSTejun Heo { 30946f1d1e3aSTejun Heo struct ata_port *ap = link->ap; 309500115e0fSTejun Heo struct ata_device *dev; 309600115e0fSTejun Heo int rc; 30976f1d1e3aSTejun Heo 309876326ac1STejun Heo /* if data transfer is verified, clear DUBIOUS_XFER on ering top */ 30991eca4365STejun Heo ata_for_each_dev(dev, link, ENABLED) { 310076326ac1STejun Heo if (!(dev->flags & ATA_DFLAG_DUBIOUS_XFER)) { 310176326ac1STejun Heo struct ata_ering_entry *ent; 310276326ac1STejun Heo 310376326ac1STejun Heo ent = ata_ering_top(&dev->ering); 310476326ac1STejun Heo if (ent) 310576326ac1STejun Heo ent->eflags &= ~ATA_EFLAG_DUBIOUS_XFER; 310676326ac1STejun Heo } 310776326ac1STejun Heo } 310876326ac1STejun Heo 31096f1d1e3aSTejun Heo /* has private set_mode? */ 31106f1d1e3aSTejun Heo if (ap->ops->set_mode) 311100115e0fSTejun Heo rc = ap->ops->set_mode(link, r_failed_dev); 311200115e0fSTejun Heo else 311300115e0fSTejun Heo rc = ata_do_set_mode(link, r_failed_dev); 311400115e0fSTejun Heo 311500115e0fSTejun Heo /* if transfer mode has changed, set DUBIOUS_XFER on device */ 31161eca4365STejun Heo ata_for_each_dev(dev, link, ENABLED) { 311700115e0fSTejun Heo struct ata_eh_context *ehc = &link->eh_context; 311800115e0fSTejun Heo u8 saved_xfer_mode = ehc->saved_xfer_mode[dev->devno]; 311900115e0fSTejun Heo u8 saved_ncq = !!(ehc->saved_ncq_enabled & (1 << dev->devno)); 312000115e0fSTejun Heo 312100115e0fSTejun Heo if (dev->xfer_mode != saved_xfer_mode || 312200115e0fSTejun Heo ata_ncq_enabled(dev) != saved_ncq) 312300115e0fSTejun Heo dev->flags |= ATA_DFLAG_DUBIOUS_XFER; 312400115e0fSTejun Heo } 312500115e0fSTejun Heo 312600115e0fSTejun Heo return rc; 31276f1d1e3aSTejun Heo } 31286f1d1e3aSTejun Heo 312911fc33daSTejun Heo /** 313011fc33daSTejun Heo * atapi_eh_clear_ua - Clear ATAPI UNIT ATTENTION after reset 313111fc33daSTejun Heo * @dev: ATAPI device to clear UA for 313211fc33daSTejun Heo * 313311fc33daSTejun Heo * Resets and other operations can make an ATAPI device raise 313411fc33daSTejun Heo * UNIT ATTENTION which causes the next operation to fail. This 313511fc33daSTejun Heo * function clears UA. 313611fc33daSTejun Heo * 313711fc33daSTejun Heo * LOCKING: 313811fc33daSTejun Heo * EH context (may sleep). 313911fc33daSTejun Heo * 314011fc33daSTejun Heo * RETURNS: 314111fc33daSTejun Heo * 0 on success, -errno on failure. 314211fc33daSTejun Heo */ 314311fc33daSTejun Heo static int atapi_eh_clear_ua(struct ata_device *dev) 314411fc33daSTejun Heo { 314511fc33daSTejun Heo int i; 314611fc33daSTejun Heo 314711fc33daSTejun Heo for (i = 0; i < ATA_EH_UA_TRIES; i++) { 3148b5357081STejun Heo u8 *sense_buffer = dev->link->ap->sector_buf; 314911fc33daSTejun Heo u8 sense_key = 0; 315011fc33daSTejun Heo unsigned int err_mask; 315111fc33daSTejun Heo 315211fc33daSTejun Heo err_mask = atapi_eh_tur(dev, &sense_key); 315311fc33daSTejun Heo if (err_mask != 0 && err_mask != AC_ERR_DEV) { 3154a9a79dfeSJoe Perches ata_dev_warn(dev, 3155a9a79dfeSJoe Perches "TEST_UNIT_READY failed (err_mask=0x%x)\n", 3156a9a79dfeSJoe Perches err_mask); 315711fc33daSTejun Heo return -EIO; 315811fc33daSTejun Heo } 315911fc33daSTejun Heo 316011fc33daSTejun Heo if (!err_mask || sense_key != UNIT_ATTENTION) 316111fc33daSTejun Heo return 0; 316211fc33daSTejun Heo 316311fc33daSTejun Heo err_mask = atapi_eh_request_sense(dev, sense_buffer, sense_key); 316411fc33daSTejun Heo if (err_mask) { 3165a9a79dfeSJoe Perches ata_dev_warn(dev, "failed to clear " 316611fc33daSTejun Heo "UNIT ATTENTION (err_mask=0x%x)\n", err_mask); 316711fc33daSTejun Heo return -EIO; 316811fc33daSTejun Heo } 316911fc33daSTejun Heo } 317011fc33daSTejun Heo 3171a9a79dfeSJoe Perches ata_dev_warn(dev, "UNIT ATTENTION persists after %d tries\n", 3172a9a79dfeSJoe Perches ATA_EH_UA_TRIES); 317311fc33daSTejun Heo 317411fc33daSTejun Heo return 0; 317511fc33daSTejun Heo } 317611fc33daSTejun Heo 31776013efd8STejun Heo /** 31786013efd8STejun Heo * ata_eh_maybe_retry_flush - Retry FLUSH if necessary 31796013efd8STejun Heo * @dev: ATA device which may need FLUSH retry 31806013efd8STejun Heo * 31816013efd8STejun Heo * If @dev failed FLUSH, it needs to be reported upper layer 31826013efd8STejun Heo * immediately as it means that @dev failed to remap and already 31836013efd8STejun Heo * lost at least a sector and further FLUSH retrials won't make 31846013efd8STejun Heo * any difference to the lost sector. However, if FLUSH failed 31856013efd8STejun Heo * for other reasons, for example transmission error, FLUSH needs 31866013efd8STejun Heo * to be retried. 31876013efd8STejun Heo * 31886013efd8STejun Heo * This function determines whether FLUSH failure retry is 31896013efd8STejun Heo * necessary and performs it if so. 31906013efd8STejun Heo * 31916013efd8STejun Heo * RETURNS: 31926013efd8STejun Heo * 0 if EH can continue, -errno if EH needs to be repeated. 31936013efd8STejun Heo */ 31946013efd8STejun Heo static int ata_eh_maybe_retry_flush(struct ata_device *dev) 31956013efd8STejun Heo { 31966013efd8STejun Heo struct ata_link *link = dev->link; 31976013efd8STejun Heo struct ata_port *ap = link->ap; 31986013efd8STejun Heo struct ata_queued_cmd *qc; 31996013efd8STejun Heo struct ata_taskfile tf; 32006013efd8STejun Heo unsigned int err_mask; 32016013efd8STejun Heo int rc = 0; 32026013efd8STejun Heo 32036013efd8STejun Heo /* did flush fail for this device? */ 32046013efd8STejun Heo if (!ata_tag_valid(link->active_tag)) 32056013efd8STejun Heo return 0; 32066013efd8STejun Heo 32076013efd8STejun Heo qc = __ata_qc_from_tag(ap, link->active_tag); 32086013efd8STejun Heo if (qc->dev != dev || (qc->tf.command != ATA_CMD_FLUSH_EXT && 32096013efd8STejun Heo qc->tf.command != ATA_CMD_FLUSH)) 32106013efd8STejun Heo return 0; 32116013efd8STejun Heo 32126013efd8STejun Heo /* if the device failed it, it should be reported to upper layers */ 32136013efd8STejun Heo if (qc->err_mask & AC_ERR_DEV) 32146013efd8STejun Heo return 0; 32156013efd8STejun Heo 32166013efd8STejun Heo /* flush failed for some other reason, give it another shot */ 32176013efd8STejun Heo ata_tf_init(dev, &tf); 32186013efd8STejun Heo 32196013efd8STejun Heo tf.command = qc->tf.command; 32206013efd8STejun Heo tf.flags |= ATA_TFLAG_DEVICE; 32216013efd8STejun Heo tf.protocol = ATA_PROT_NODATA; 32226013efd8STejun Heo 3223a9a79dfeSJoe Perches ata_dev_warn(dev, "retrying FLUSH 0x%x Emask 0x%x\n", 32246013efd8STejun Heo tf.command, qc->err_mask); 32256013efd8STejun Heo 32266013efd8STejun Heo err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0); 32276013efd8STejun Heo if (!err_mask) { 32286013efd8STejun Heo /* 32296013efd8STejun Heo * FLUSH is complete but there's no way to 32306013efd8STejun Heo * successfully complete a failed command from EH. 32316013efd8STejun Heo * Making sure retry is allowed at least once and 32326013efd8STejun Heo * retrying it should do the trick - whatever was in 32336013efd8STejun Heo * the cache is already on the platter and this won't 32346013efd8STejun Heo * cause infinite loop. 32356013efd8STejun Heo */ 32366013efd8STejun Heo qc->scsicmd->allowed = max(qc->scsicmd->allowed, 1); 32376013efd8STejun Heo } else { 3238a9a79dfeSJoe Perches ata_dev_warn(dev, "FLUSH failed Emask 0x%x\n", 32396013efd8STejun Heo err_mask); 32406013efd8STejun Heo rc = -EIO; 32416013efd8STejun Heo 32426013efd8STejun Heo /* if device failed it, report it to upper layers */ 32436013efd8STejun Heo if (err_mask & AC_ERR_DEV) { 32446013efd8STejun Heo qc->err_mask |= AC_ERR_DEV; 32456013efd8STejun Heo qc->result_tf = tf; 32464cb7c6f1SNiklas Cassel if (!ata_port_is_frozen(ap)) 32476013efd8STejun Heo rc = 0; 32486013efd8STejun Heo } 32496013efd8STejun Heo } 32506013efd8STejun Heo return rc; 32516013efd8STejun Heo } 32526013efd8STejun Heo 32536b7ae954STejun Heo /** 32546b7ae954STejun Heo * ata_eh_set_lpm - configure SATA interface power management 32556b7ae954STejun Heo * @link: link to configure power management 32566b7ae954STejun Heo * @policy: the link power management policy 32576b7ae954STejun Heo * @r_failed_dev: out parameter for failed device 32586b7ae954STejun Heo * 32596b7ae954STejun Heo * Enable SATA Interface power management. This will enable 3260f4ac6476SHans de Goede * Device Interface Power Management (DIPM) for min_power and 3261f4ac6476SHans de Goede * medium_power_with_dipm policies, and then call driver specific 3262f4ac6476SHans de Goede * callbacks for enabling Host Initiated Power management. 32636b7ae954STejun Heo * 32646b7ae954STejun Heo * LOCKING: 32656b7ae954STejun Heo * EH context. 32666b7ae954STejun Heo * 32676b7ae954STejun Heo * RETURNS: 32686b7ae954STejun Heo * 0 on success, -errno on failure. 32696b7ae954STejun Heo */ 32706b7ae954STejun Heo static int ata_eh_set_lpm(struct ata_link *link, enum ata_lpm_policy policy, 32716b7ae954STejun Heo struct ata_device **r_failed_dev) 32726b7ae954STejun Heo { 32736c8ea89cSTejun Heo struct ata_port *ap = ata_is_host_link(link) ? link->ap : NULL; 32746b7ae954STejun Heo struct ata_eh_context *ehc = &link->eh_context; 32756b7ae954STejun Heo struct ata_device *dev, *link_dev = NULL, *lpm_dev = NULL; 3276e5005b15STejun Heo enum ata_lpm_policy old_policy = link->lpm_policy; 32775f6f12ccSTejun Heo bool no_dipm = link->ap->flags & ATA_FLAG_NO_DIPM; 32786b7ae954STejun Heo unsigned int hints = ATA_LPM_EMPTY | ATA_LPM_HIPM; 32796b7ae954STejun Heo unsigned int err_mask; 32806b7ae954STejun Heo int rc; 32816b7ae954STejun Heo 32826b7ae954STejun Heo /* if the link or host doesn't do LPM, noop */ 32834c9029e7SBartlomiej Zolnierkiewicz if (!IS_ENABLED(CONFIG_SATA_HOST) || 32844c9029e7SBartlomiej Zolnierkiewicz (link->flags & ATA_LFLAG_NO_LPM) || (ap && !ap->ops->set_lpm)) 32856b7ae954STejun Heo return 0; 32866b7ae954STejun Heo 32876b7ae954STejun Heo /* 32886b7ae954STejun Heo * DIPM is enabled only for MIN_POWER as some devices 32896b7ae954STejun Heo * misbehave when the host NACKs transition to SLUMBER. Order 32906b7ae954STejun Heo * device and link configurations such that the host always 32916b7ae954STejun Heo * allows DIPM requests. 32926b7ae954STejun Heo */ 32936b7ae954STejun Heo ata_for_each_dev(dev, link, ENABLED) { 32946b7ae954STejun Heo bool hipm = ata_id_has_hipm(dev->id); 3295ae01b249STejun Heo bool dipm = ata_id_has_dipm(dev->id) && !no_dipm; 32966b7ae954STejun Heo 32976b7ae954STejun Heo /* find the first enabled and LPM enabled devices */ 32986b7ae954STejun Heo if (!link_dev) 32996b7ae954STejun Heo link_dev = dev; 33006b7ae954STejun Heo 33016b7ae954STejun Heo if (!lpm_dev && (hipm || dipm)) 33026b7ae954STejun Heo lpm_dev = dev; 33036b7ae954STejun Heo 33046b7ae954STejun Heo hints &= ~ATA_LPM_EMPTY; 33056b7ae954STejun Heo if (!hipm) 33066b7ae954STejun Heo hints &= ~ATA_LPM_HIPM; 33076b7ae954STejun Heo 33086b7ae954STejun Heo /* disable DIPM before changing link config */ 3309f4ac6476SHans de Goede if (policy < ATA_LPM_MED_POWER_WITH_DIPM && dipm) { 33106b7ae954STejun Heo err_mask = ata_dev_set_feature(dev, 33116b7ae954STejun Heo SETFEATURES_SATA_DISABLE, SATA_DIPM); 33126b7ae954STejun Heo if (err_mask && err_mask != AC_ERR_DEV) { 3313a9a79dfeSJoe Perches ata_dev_warn(dev, 33146b7ae954STejun Heo "failed to disable DIPM, Emask 0x%x\n", 33156b7ae954STejun Heo err_mask); 33166b7ae954STejun Heo rc = -EIO; 33176b7ae954STejun Heo goto fail; 33186b7ae954STejun Heo } 33196b7ae954STejun Heo } 33206b7ae954STejun Heo } 33216b7ae954STejun Heo 33226c8ea89cSTejun Heo if (ap) { 33236b7ae954STejun Heo rc = ap->ops->set_lpm(link, policy, hints); 33246b7ae954STejun Heo if (!rc && ap->slave_link) 33256b7ae954STejun Heo rc = ap->ops->set_lpm(ap->slave_link, policy, hints); 33266c8ea89cSTejun Heo } else 33276c8ea89cSTejun Heo rc = sata_pmp_set_lpm(link, policy, hints); 33286b7ae954STejun Heo 33296b7ae954STejun Heo /* 33306b7ae954STejun Heo * Attribute link config failure to the first (LPM) enabled 33316b7ae954STejun Heo * device on the link. 33326b7ae954STejun Heo */ 33336b7ae954STejun Heo if (rc) { 33346b7ae954STejun Heo if (rc == -EOPNOTSUPP) { 33356b7ae954STejun Heo link->flags |= ATA_LFLAG_NO_LPM; 33366b7ae954STejun Heo return 0; 33376b7ae954STejun Heo } 33386b7ae954STejun Heo dev = lpm_dev ? lpm_dev : link_dev; 33396b7ae954STejun Heo goto fail; 33406b7ae954STejun Heo } 33416b7ae954STejun Heo 3342e5005b15STejun Heo /* 3343e5005b15STejun Heo * Low level driver acked the transition. Issue DIPM command 3344e5005b15STejun Heo * with the new policy set. 3345e5005b15STejun Heo */ 3346e5005b15STejun Heo link->lpm_policy = policy; 3347e5005b15STejun Heo if (ap && ap->slave_link) 3348e5005b15STejun Heo ap->slave_link->lpm_policy = policy; 3349e5005b15STejun Heo 33506b7ae954STejun Heo /* host config updated, enable DIPM if transitioning to MIN_POWER */ 33516b7ae954STejun Heo ata_for_each_dev(dev, link, ENABLED) { 3352f4ac6476SHans de Goede if (policy >= ATA_LPM_MED_POWER_WITH_DIPM && !no_dipm && 3353ae01b249STejun Heo ata_id_has_dipm(dev->id)) { 33546b7ae954STejun Heo err_mask = ata_dev_set_feature(dev, 33556b7ae954STejun Heo SETFEATURES_SATA_ENABLE, SATA_DIPM); 33566b7ae954STejun Heo if (err_mask && err_mask != AC_ERR_DEV) { 3357a9a79dfeSJoe Perches ata_dev_warn(dev, 33586b7ae954STejun Heo "failed to enable DIPM, Emask 0x%x\n", 33596b7ae954STejun Heo err_mask); 33606b7ae954STejun Heo rc = -EIO; 33616b7ae954STejun Heo goto fail; 33626b7ae954STejun Heo } 33636b7ae954STejun Heo } 33646b7ae954STejun Heo } 33656b7ae954STejun Heo 336609c5b480SGabriele Mazzotta link->last_lpm_change = jiffies; 336709c5b480SGabriele Mazzotta link->flags |= ATA_LFLAG_CHANGED; 336809c5b480SGabriele Mazzotta 33696b7ae954STejun Heo return 0; 33706b7ae954STejun Heo 33716b7ae954STejun Heo fail: 3372e5005b15STejun Heo /* restore the old policy */ 3373e5005b15STejun Heo link->lpm_policy = old_policy; 3374e5005b15STejun Heo if (ap && ap->slave_link) 3375e5005b15STejun Heo ap->slave_link->lpm_policy = old_policy; 3376e5005b15STejun Heo 33776b7ae954STejun Heo /* if no device or only one more chance is left, disable LPM */ 33786b7ae954STejun Heo if (!dev || ehc->tries[dev->devno] <= 2) { 3379a9a79dfeSJoe Perches ata_link_warn(link, "disabling LPM on the link\n"); 33806b7ae954STejun Heo link->flags |= ATA_LFLAG_NO_LPM; 33816b7ae954STejun Heo } 33826b7ae954STejun Heo if (r_failed_dev) 33836b7ae954STejun Heo *r_failed_dev = dev; 33846b7ae954STejun Heo return rc; 33856b7ae954STejun Heo } 33866b7ae954STejun Heo 33878a745f1fSKristen Carlson Accardi int ata_link_nr_enabled(struct ata_link *link) 3388c6fd2807SJeff Garzik { 3389f58229f8STejun Heo struct ata_device *dev; 3390f58229f8STejun Heo int cnt = 0; 3391c6fd2807SJeff Garzik 33921eca4365STejun Heo ata_for_each_dev(dev, link, ENABLED) 3393c6fd2807SJeff Garzik cnt++; 3394c6fd2807SJeff Garzik return cnt; 3395c6fd2807SJeff Garzik } 3396c6fd2807SJeff Garzik 33970260731fSTejun Heo static int ata_link_nr_vacant(struct ata_link *link) 3398c6fd2807SJeff Garzik { 3399f58229f8STejun Heo struct ata_device *dev; 3400f58229f8STejun Heo int cnt = 0; 3401c6fd2807SJeff Garzik 34021eca4365STejun Heo ata_for_each_dev(dev, link, ALL) 3403f58229f8STejun Heo if (dev->class == ATA_DEV_UNKNOWN) 3404c6fd2807SJeff Garzik cnt++; 3405c6fd2807SJeff Garzik return cnt; 3406c6fd2807SJeff Garzik } 3407c6fd2807SJeff Garzik 34080260731fSTejun Heo static int ata_eh_skip_recovery(struct ata_link *link) 3409c6fd2807SJeff Garzik { 3410672b2d65STejun Heo struct ata_port *ap = link->ap; 34110260731fSTejun Heo struct ata_eh_context *ehc = &link->eh_context; 3412f58229f8STejun Heo struct ata_device *dev; 3413c6fd2807SJeff Garzik 3414f9df58cbSTejun Heo /* skip disabled links */ 3415f9df58cbSTejun Heo if (link->flags & ATA_LFLAG_DISABLED) 3416f9df58cbSTejun Heo return 1; 3417f9df58cbSTejun Heo 3418e2f3d75fSTejun Heo /* skip if explicitly requested */ 3419e2f3d75fSTejun Heo if (ehc->i.flags & ATA_EHI_NO_RECOVERY) 3420e2f3d75fSTejun Heo return 1; 3421e2f3d75fSTejun Heo 3422672b2d65STejun Heo /* thaw frozen port and recover failed devices */ 34234cb7c6f1SNiklas Cassel if (ata_port_is_frozen(ap) || ata_link_nr_enabled(link)) 3424672b2d65STejun Heo return 0; 3425672b2d65STejun Heo 3426672b2d65STejun Heo /* reset at least once if reset is requested */ 3427672b2d65STejun Heo if ((ehc->i.action & ATA_EH_RESET) && 3428672b2d65STejun Heo !(ehc->i.flags & ATA_EHI_DID_RESET)) 3429c6fd2807SJeff Garzik return 0; 3430c6fd2807SJeff Garzik 3431c6fd2807SJeff Garzik /* skip if class codes for all vacant slots are ATA_DEV_NONE */ 34321eca4365STejun Heo ata_for_each_dev(dev, link, ALL) { 3433c6fd2807SJeff Garzik if (dev->class == ATA_DEV_UNKNOWN && 3434c6fd2807SJeff Garzik ehc->classes[dev->devno] != ATA_DEV_NONE) 3435c6fd2807SJeff Garzik return 0; 3436c6fd2807SJeff Garzik } 3437c6fd2807SJeff Garzik 3438c6fd2807SJeff Garzik return 1; 3439c6fd2807SJeff Garzik } 3440c6fd2807SJeff Garzik 3441c2c7a89cSTejun Heo static int ata_count_probe_trials_cb(struct ata_ering_entry *ent, void *void_arg) 3442c2c7a89cSTejun Heo { 3443c2c7a89cSTejun Heo u64 interval = msecs_to_jiffies(ATA_EH_PROBE_TRIAL_INTERVAL); 3444c2c7a89cSTejun Heo u64 now = get_jiffies_64(); 3445c2c7a89cSTejun Heo int *trials = void_arg; 3446c2c7a89cSTejun Heo 34476868225eSLin Ming if ((ent->eflags & ATA_EFLAG_OLD_ER) || 34486868225eSLin Ming (ent->timestamp < now - min(now, interval))) 3449c2c7a89cSTejun Heo return -1; 3450c2c7a89cSTejun Heo 3451c2c7a89cSTejun Heo (*trials)++; 3452c2c7a89cSTejun Heo return 0; 3453c2c7a89cSTejun Heo } 3454c2c7a89cSTejun Heo 345502c05a27STejun Heo static int ata_eh_schedule_probe(struct ata_device *dev) 345602c05a27STejun Heo { 345702c05a27STejun Heo struct ata_eh_context *ehc = &dev->link->eh_context; 3458c2c7a89cSTejun Heo struct ata_link *link = ata_dev_phys_link(dev); 3459c2c7a89cSTejun Heo int trials = 0; 346002c05a27STejun Heo 346102c05a27STejun Heo if (!(ehc->i.probe_mask & (1 << dev->devno)) || 346202c05a27STejun Heo (ehc->did_probe_mask & (1 << dev->devno))) 346302c05a27STejun Heo return 0; 346402c05a27STejun Heo 346502c05a27STejun Heo ata_eh_detach_dev(dev); 346602c05a27STejun Heo ata_dev_init(dev); 346702c05a27STejun Heo ehc->did_probe_mask |= (1 << dev->devno); 3468cf480626STejun Heo ehc->i.action |= ATA_EH_RESET; 346900115e0fSTejun Heo ehc->saved_xfer_mode[dev->devno] = 0; 347000115e0fSTejun Heo ehc->saved_ncq_enabled &= ~(1 << dev->devno); 347102c05a27STejun Heo 34726b7ae954STejun Heo /* the link maybe in a deep sleep, wake it up */ 34736c8ea89cSTejun Heo if (link->lpm_policy > ATA_LPM_MAX_POWER) { 34746c8ea89cSTejun Heo if (ata_is_host_link(link)) 34756c8ea89cSTejun Heo link->ap->ops->set_lpm(link, ATA_LPM_MAX_POWER, 34766c8ea89cSTejun Heo ATA_LPM_EMPTY); 34776c8ea89cSTejun Heo else 34786c8ea89cSTejun Heo sata_pmp_set_lpm(link, ATA_LPM_MAX_POWER, 34796c8ea89cSTejun Heo ATA_LPM_EMPTY); 34806c8ea89cSTejun Heo } 34816b7ae954STejun Heo 3482c2c7a89cSTejun Heo /* Record and count probe trials on the ering. The specific 3483c2c7a89cSTejun Heo * error mask used is irrelevant. Because a successful device 3484c2c7a89cSTejun Heo * detection clears the ering, this count accumulates only if 3485c2c7a89cSTejun Heo * there are consecutive failed probes. 3486c2c7a89cSTejun Heo * 3487c2c7a89cSTejun Heo * If the count is equal to or higher than ATA_EH_PROBE_TRIALS 3488c2c7a89cSTejun Heo * in the last ATA_EH_PROBE_TRIAL_INTERVAL, link speed is 3489c2c7a89cSTejun Heo * forced to 1.5Gbps. 3490c2c7a89cSTejun Heo * 3491c2c7a89cSTejun Heo * This is to work around cases where failed link speed 3492c2c7a89cSTejun Heo * negotiation results in device misdetection leading to 3493c2c7a89cSTejun Heo * infinite DEVXCHG or PHRDY CHG events. 3494c2c7a89cSTejun Heo */ 3495c2c7a89cSTejun Heo ata_ering_record(&dev->ering, 0, AC_ERR_OTHER); 3496c2c7a89cSTejun Heo ata_ering_map(&dev->ering, ata_count_probe_trials_cb, &trials); 3497c2c7a89cSTejun Heo 3498c2c7a89cSTejun Heo if (trials > ATA_EH_PROBE_TRIALS) 3499c2c7a89cSTejun Heo sata_down_spd_limit(link, 1); 3500c2c7a89cSTejun Heo 350102c05a27STejun Heo return 1; 350202c05a27STejun Heo } 350302c05a27STejun Heo 35049b1e2658STejun Heo static int ata_eh_handle_dev_fail(struct ata_device *dev, int err) 3505fee7ca72STejun Heo { 35069af5c9c9STejun Heo struct ata_eh_context *ehc = &dev->link->eh_context; 3507fee7ca72STejun Heo 3508cf9a590aSTejun Heo /* -EAGAIN from EH routine indicates retry without prejudice. 3509cf9a590aSTejun Heo * The requester is responsible for ensuring forward progress. 3510cf9a590aSTejun Heo */ 3511cf9a590aSTejun Heo if (err != -EAGAIN) 3512fee7ca72STejun Heo ehc->tries[dev->devno]--; 3513fee7ca72STejun Heo 3514fee7ca72STejun Heo switch (err) { 3515fee7ca72STejun Heo case -ENODEV: 3516fee7ca72STejun Heo /* device missing or wrong IDENTIFY data, schedule probing */ 3517fee7ca72STejun Heo ehc->i.probe_mask |= (1 << dev->devno); 3518df561f66SGustavo A. R. Silva fallthrough; 3519fee7ca72STejun Heo case -EINVAL: 3520fee7ca72STejun Heo /* give it just one more chance */ 3521fee7ca72STejun Heo ehc->tries[dev->devno] = min(ehc->tries[dev->devno], 1); 3522df561f66SGustavo A. R. Silva fallthrough; 3523fee7ca72STejun Heo case -EIO: 3524d89293abSTejun Heo if (ehc->tries[dev->devno] == 1) { 3525fee7ca72STejun Heo /* This is the last chance, better to slow 3526fee7ca72STejun Heo * down than lose it. 3527fee7ca72STejun Heo */ 3528a07d499bSTejun Heo sata_down_spd_limit(ata_dev_phys_link(dev), 0); 3529d89293abSTejun Heo if (dev->pio_mode > XFER_PIO_0) 3530fee7ca72STejun Heo ata_down_xfermask_limit(dev, ATA_DNXFER_PIO); 3531fee7ca72STejun Heo } 3532fee7ca72STejun Heo } 3533fee7ca72STejun Heo 3534fee7ca72STejun Heo if (ata_dev_enabled(dev) && !ehc->tries[dev->devno]) { 3535fee7ca72STejun Heo /* disable device if it has used up all its chances */ 3536fee7ca72STejun Heo ata_dev_disable(dev); 3537fee7ca72STejun Heo 3538fee7ca72STejun Heo /* detach if offline */ 3539b1c72916STejun Heo if (ata_phys_link_offline(ata_dev_phys_link(dev))) 3540fee7ca72STejun Heo ata_eh_detach_dev(dev); 3541fee7ca72STejun Heo 354202c05a27STejun Heo /* schedule probe if necessary */ 354387fbc5a0STejun Heo if (ata_eh_schedule_probe(dev)) { 3544fee7ca72STejun Heo ehc->tries[dev->devno] = ATA_EH_DEV_TRIES; 354587fbc5a0STejun Heo memset(ehc->cmd_timeout_idx[dev->devno], 0, 354687fbc5a0STejun Heo sizeof(ehc->cmd_timeout_idx[dev->devno])); 354787fbc5a0STejun Heo } 35489b1e2658STejun Heo 35499b1e2658STejun Heo return 1; 3550fee7ca72STejun Heo } else { 3551cf480626STejun Heo ehc->i.action |= ATA_EH_RESET; 35529b1e2658STejun Heo return 0; 3553fee7ca72STejun Heo } 3554fee7ca72STejun Heo } 3555fee7ca72STejun Heo 3556c6fd2807SJeff Garzik /** 3557c6fd2807SJeff Garzik * ata_eh_recover - recover host port after error 3558c6fd2807SJeff Garzik * @ap: host port to recover 3559c6fd2807SJeff Garzik * @prereset: prereset method (can be NULL) 3560c6fd2807SJeff Garzik * @softreset: softreset method (can be NULL) 3561c6fd2807SJeff Garzik * @hardreset: hardreset method (can be NULL) 3562c6fd2807SJeff Garzik * @postreset: postreset method (can be NULL) 35639b1e2658STejun Heo * @r_failed_link: out parameter for failed link 3564c6fd2807SJeff Garzik * 3565c6fd2807SJeff Garzik * This is the alpha and omega, eum and yang, heart and soul of 3566c6fd2807SJeff Garzik * libata exception handling. On entry, actions required to 35679b1e2658STejun Heo * recover each link and hotplug requests are recorded in the 35689b1e2658STejun Heo * link's eh_context. This function executes all the operations 35699b1e2658STejun Heo * with appropriate retrials and fallbacks to resurrect failed 3570c6fd2807SJeff Garzik * devices, detach goners and greet newcomers. 3571c6fd2807SJeff Garzik * 3572c6fd2807SJeff Garzik * LOCKING: 3573c6fd2807SJeff Garzik * Kernel thread context (may sleep). 3574c6fd2807SJeff Garzik * 3575c6fd2807SJeff Garzik * RETURNS: 3576c6fd2807SJeff Garzik * 0 on success, -errno on failure. 3577c6fd2807SJeff Garzik */ 3578fb7fd614STejun Heo int ata_eh_recover(struct ata_port *ap, ata_prereset_fn_t prereset, 3579c6fd2807SJeff Garzik ata_reset_fn_t softreset, ata_reset_fn_t hardreset, 35809b1e2658STejun Heo ata_postreset_fn_t postreset, 35819b1e2658STejun Heo struct ata_link **r_failed_link) 3582c6fd2807SJeff Garzik { 35839b1e2658STejun Heo struct ata_link *link; 3584c6fd2807SJeff Garzik struct ata_device *dev; 35856b7ae954STejun Heo int rc, nr_fails; 358645fabbb7SElias Oltmanns unsigned long flags, deadline; 3587c6fd2807SJeff Garzik 3588c6fd2807SJeff Garzik /* prep for recovery */ 35891eca4365STejun Heo ata_for_each_link(link, ap, EDGE) { 35909b1e2658STejun Heo struct ata_eh_context *ehc = &link->eh_context; 35919b1e2658STejun Heo 3592f9df58cbSTejun Heo /* re-enable link? */ 3593f9df58cbSTejun Heo if (ehc->i.action & ATA_EH_ENABLE_LINK) { 3594f9df58cbSTejun Heo ata_eh_about_to_do(link, NULL, ATA_EH_ENABLE_LINK); 3595f9df58cbSTejun Heo spin_lock_irqsave(ap->lock, flags); 3596f9df58cbSTejun Heo link->flags &= ~ATA_LFLAG_DISABLED; 3597f9df58cbSTejun Heo spin_unlock_irqrestore(ap->lock, flags); 3598f9df58cbSTejun Heo ata_eh_done(link, NULL, ATA_EH_ENABLE_LINK); 3599f9df58cbSTejun Heo } 3600f9df58cbSTejun Heo 36011eca4365STejun Heo ata_for_each_dev(dev, link, ALL) { 3602fd995f70STejun Heo if (link->flags & ATA_LFLAG_NO_RETRY) 3603fd995f70STejun Heo ehc->tries[dev->devno] = 1; 3604fd995f70STejun Heo else 3605c6fd2807SJeff Garzik ehc->tries[dev->devno] = ATA_EH_DEV_TRIES; 3606c6fd2807SJeff Garzik 360779a55b72STejun Heo /* collect port action mask recorded in dev actions */ 36089b1e2658STejun Heo ehc->i.action |= ehc->i.dev_action[dev->devno] & 36099b1e2658STejun Heo ~ATA_EH_PERDEV_MASK; 3610f58229f8STejun Heo ehc->i.dev_action[dev->devno] &= ATA_EH_PERDEV_MASK; 361179a55b72STejun Heo 3612c6fd2807SJeff Garzik /* process hotplug request */ 3613c6fd2807SJeff Garzik if (dev->flags & ATA_DFLAG_DETACH) 3614c6fd2807SJeff Garzik ata_eh_detach_dev(dev); 3615c6fd2807SJeff Garzik 361602c05a27STejun Heo /* schedule probe if necessary */ 361702c05a27STejun Heo if (!ata_dev_enabled(dev)) 361802c05a27STejun Heo ata_eh_schedule_probe(dev); 3619c6fd2807SJeff Garzik } 36209b1e2658STejun Heo } 3621c6fd2807SJeff Garzik 3622c6fd2807SJeff Garzik retry: 3623c6fd2807SJeff Garzik rc = 0; 3624c6fd2807SJeff Garzik 3625c6fd2807SJeff Garzik /* if UNLOADING, finish immediately */ 3626c6fd2807SJeff Garzik if (ap->pflags & ATA_PFLAG_UNLOADING) 3627c6fd2807SJeff Garzik goto out; 3628c6fd2807SJeff Garzik 36299b1e2658STejun Heo /* prep for EH */ 36301eca4365STejun Heo ata_for_each_link(link, ap, EDGE) { 36319b1e2658STejun Heo struct ata_eh_context *ehc = &link->eh_context; 36329b1e2658STejun Heo 3633c6fd2807SJeff Garzik /* skip EH if possible. */ 36340260731fSTejun Heo if (ata_eh_skip_recovery(link)) 3635c6fd2807SJeff Garzik ehc->i.action = 0; 3636c6fd2807SJeff Garzik 36371eca4365STejun Heo ata_for_each_dev(dev, link, ALL) 3638f58229f8STejun Heo ehc->classes[dev->devno] = ATA_DEV_UNKNOWN; 36399b1e2658STejun Heo } 3640c6fd2807SJeff Garzik 3641c6fd2807SJeff Garzik /* reset */ 36421eca4365STejun Heo ata_for_each_link(link, ap, EDGE) { 36439b1e2658STejun Heo struct ata_eh_context *ehc = &link->eh_context; 36449b1e2658STejun Heo 3645cf480626STejun Heo if (!(ehc->i.action & ATA_EH_RESET)) 36469b1e2658STejun Heo continue; 36479b1e2658STejun Heo 36489b1e2658STejun Heo rc = ata_eh_reset(link, ata_link_nr_vacant(link), 3649dc98c32cSTejun Heo prereset, softreset, hardreset, postreset); 3650c6fd2807SJeff Garzik if (rc) { 3651a9a79dfeSJoe Perches ata_link_err(link, "reset failed, giving up\n"); 3652c6fd2807SJeff Garzik goto out; 3653c6fd2807SJeff Garzik } 36549b1e2658STejun Heo } 3655c6fd2807SJeff Garzik 365645fabbb7SElias Oltmanns do { 365745fabbb7SElias Oltmanns unsigned long now; 365845fabbb7SElias Oltmanns 365945fabbb7SElias Oltmanns /* 366045fabbb7SElias Oltmanns * clears ATA_EH_PARK in eh_info and resets 366145fabbb7SElias Oltmanns * ap->park_req_pending 366245fabbb7SElias Oltmanns */ 366345fabbb7SElias Oltmanns ata_eh_pull_park_action(ap); 366445fabbb7SElias Oltmanns 366545fabbb7SElias Oltmanns deadline = jiffies; 36661eca4365STejun Heo ata_for_each_link(link, ap, EDGE) { 36671eca4365STejun Heo ata_for_each_dev(dev, link, ALL) { 366845fabbb7SElias Oltmanns struct ata_eh_context *ehc = &link->eh_context; 366945fabbb7SElias Oltmanns unsigned long tmp; 367045fabbb7SElias Oltmanns 36719162c657SHannes Reinecke if (dev->class != ATA_DEV_ATA && 36729162c657SHannes Reinecke dev->class != ATA_DEV_ZAC) 367345fabbb7SElias Oltmanns continue; 367445fabbb7SElias Oltmanns if (!(ehc->i.dev_action[dev->devno] & 367545fabbb7SElias Oltmanns ATA_EH_PARK)) 367645fabbb7SElias Oltmanns continue; 367745fabbb7SElias Oltmanns tmp = dev->unpark_deadline; 367845fabbb7SElias Oltmanns if (time_before(deadline, tmp)) 367945fabbb7SElias Oltmanns deadline = tmp; 368045fabbb7SElias Oltmanns else if (time_before_eq(tmp, jiffies)) 368145fabbb7SElias Oltmanns continue; 368245fabbb7SElias Oltmanns if (ehc->unloaded_mask & (1 << dev->devno)) 368345fabbb7SElias Oltmanns continue; 368445fabbb7SElias Oltmanns 368545fabbb7SElias Oltmanns ata_eh_park_issue_cmd(dev, 1); 368645fabbb7SElias Oltmanns } 368745fabbb7SElias Oltmanns } 368845fabbb7SElias Oltmanns 368945fabbb7SElias Oltmanns now = jiffies; 369045fabbb7SElias Oltmanns if (time_before_eq(deadline, now)) 369145fabbb7SElias Oltmanns break; 369245fabbb7SElias Oltmanns 3693c0c362b6STejun Heo ata_eh_release(ap); 369445fabbb7SElias Oltmanns deadline = wait_for_completion_timeout(&ap->park_req_pending, 369545fabbb7SElias Oltmanns deadline - now); 3696c0c362b6STejun Heo ata_eh_acquire(ap); 369745fabbb7SElias Oltmanns } while (deadline); 36981eca4365STejun Heo ata_for_each_link(link, ap, EDGE) { 36991eca4365STejun Heo ata_for_each_dev(dev, link, ALL) { 370045fabbb7SElias Oltmanns if (!(link->eh_context.unloaded_mask & 370145fabbb7SElias Oltmanns (1 << dev->devno))) 370245fabbb7SElias Oltmanns continue; 370345fabbb7SElias Oltmanns 370445fabbb7SElias Oltmanns ata_eh_park_issue_cmd(dev, 0); 370545fabbb7SElias Oltmanns ata_eh_done(link, dev, ATA_EH_PARK); 370645fabbb7SElias Oltmanns } 370745fabbb7SElias Oltmanns } 370845fabbb7SElias Oltmanns 37099b1e2658STejun Heo /* the rest */ 37106b7ae954STejun Heo nr_fails = 0; 37116b7ae954STejun Heo ata_for_each_link(link, ap, PMP_FIRST) { 37129b1e2658STejun Heo struct ata_eh_context *ehc = &link->eh_context; 37139b1e2658STejun Heo 37146b7ae954STejun Heo if (sata_pmp_attached(ap) && ata_is_host_link(link)) 37156b7ae954STejun Heo goto config_lpm; 37166b7ae954STejun Heo 3717c6fd2807SJeff Garzik /* revalidate existing devices and attach new ones */ 37180260731fSTejun Heo rc = ata_eh_revalidate_and_attach(link, &dev); 3719c6fd2807SJeff Garzik if (rc) 37206b7ae954STejun Heo goto rest_fail; 3721c6fd2807SJeff Garzik 3722633273a3STejun Heo /* if PMP got attached, return, pmp EH will take care of it */ 3723633273a3STejun Heo if (link->device->class == ATA_DEV_PMP) { 3724633273a3STejun Heo ehc->i.action = 0; 3725633273a3STejun Heo return 0; 3726633273a3STejun Heo } 3727633273a3STejun Heo 3728baa1e78aSTejun Heo /* configure transfer mode if necessary */ 3729baa1e78aSTejun Heo if (ehc->i.flags & ATA_EHI_SETMODE) { 37300260731fSTejun Heo rc = ata_set_mode(link, &dev); 37314ae72a1eSTejun Heo if (rc) 37326b7ae954STejun Heo goto rest_fail; 3733baa1e78aSTejun Heo ehc->i.flags &= ~ATA_EHI_SETMODE; 3734c6fd2807SJeff Garzik } 3735c6fd2807SJeff Garzik 373611fc33daSTejun Heo /* If reset has been issued, clear UA to avoid 373711fc33daSTejun Heo * disrupting the current users of the device. 373811fc33daSTejun Heo */ 373911fc33daSTejun Heo if (ehc->i.flags & ATA_EHI_DID_RESET) { 37401eca4365STejun Heo ata_for_each_dev(dev, link, ALL) { 374111fc33daSTejun Heo if (dev->class != ATA_DEV_ATAPI) 374211fc33daSTejun Heo continue; 374311fc33daSTejun Heo rc = atapi_eh_clear_ua(dev); 374411fc33daSTejun Heo if (rc) 37456b7ae954STejun Heo goto rest_fail; 374621334205SAaron Lu if (zpodd_dev_enabled(dev)) 374721334205SAaron Lu zpodd_post_poweron(dev); 374811fc33daSTejun Heo } 374911fc33daSTejun Heo } 375011fc33daSTejun Heo 37516013efd8STejun Heo /* retry flush if necessary */ 37526013efd8STejun Heo ata_for_each_dev(dev, link, ALL) { 37539162c657SHannes Reinecke if (dev->class != ATA_DEV_ATA && 37549162c657SHannes Reinecke dev->class != ATA_DEV_ZAC) 37556013efd8STejun Heo continue; 37566013efd8STejun Heo rc = ata_eh_maybe_retry_flush(dev); 37576013efd8STejun Heo if (rc) 37586b7ae954STejun Heo goto rest_fail; 37596013efd8STejun Heo } 37606013efd8STejun Heo 37616b7ae954STejun Heo config_lpm: 376211fc33daSTejun Heo /* configure link power saving */ 37636b7ae954STejun Heo if (link->lpm_policy != ap->target_lpm_policy) { 37646b7ae954STejun Heo rc = ata_eh_set_lpm(link, ap->target_lpm_policy, &dev); 37656b7ae954STejun Heo if (rc) 37666b7ae954STejun Heo goto rest_fail; 37676b7ae954STejun Heo } 3768ca77329fSKristen Carlson Accardi 37699b1e2658STejun Heo /* this link is okay now */ 37709b1e2658STejun Heo ehc->i.flags = 0; 37719b1e2658STejun Heo continue; 3772c6fd2807SJeff Garzik 37736b7ae954STejun Heo rest_fail: 37746b7ae954STejun Heo nr_fails++; 37756b7ae954STejun Heo if (dev) 37760a2c0f56STejun Heo ata_eh_handle_dev_fail(dev, rc); 3777c6fd2807SJeff Garzik 37784cb7c6f1SNiklas Cassel if (ata_port_is_frozen(ap)) { 3779b06ce3e5STejun Heo /* PMP reset requires working host port. 3780b06ce3e5STejun Heo * Can't retry if it's frozen. 3781b06ce3e5STejun Heo */ 3782071f44b1STejun Heo if (sata_pmp_attached(ap)) 3783b06ce3e5STejun Heo goto out; 37849b1e2658STejun Heo break; 37859b1e2658STejun Heo } 3786b06ce3e5STejun Heo } 37879b1e2658STejun Heo 37886b7ae954STejun Heo if (nr_fails) 3789c6fd2807SJeff Garzik goto retry; 3790c6fd2807SJeff Garzik 3791c6fd2807SJeff Garzik out: 37929b1e2658STejun Heo if (rc && r_failed_link) 37939b1e2658STejun Heo *r_failed_link = link; 3794c6fd2807SJeff Garzik 3795c6fd2807SJeff Garzik return rc; 3796c6fd2807SJeff Garzik } 3797c6fd2807SJeff Garzik 3798c6fd2807SJeff Garzik /** 3799c6fd2807SJeff Garzik * ata_eh_finish - finish up EH 3800c6fd2807SJeff Garzik * @ap: host port to finish EH for 3801c6fd2807SJeff Garzik * 3802c6fd2807SJeff Garzik * Recovery is complete. Clean up EH states and retry or finish 3803c6fd2807SJeff Garzik * failed qcs. 3804c6fd2807SJeff Garzik * 3805c6fd2807SJeff Garzik * LOCKING: 3806c6fd2807SJeff Garzik * None. 3807c6fd2807SJeff Garzik */ 3808fb7fd614STejun Heo void ata_eh_finish(struct ata_port *ap) 3809c6fd2807SJeff Garzik { 3810258c4e5cSJens Axboe struct ata_queued_cmd *qc; 3811c6fd2807SJeff Garzik int tag; 3812c6fd2807SJeff Garzik 3813c6fd2807SJeff Garzik /* retry or finish qcs */ 3814258c4e5cSJens Axboe ata_qc_for_each_raw(ap, qc, tag) { 381587629312SNiklas Cassel if (!(qc->flags & ATA_QCFLAG_EH)) 3816c6fd2807SJeff Garzik continue; 3817c6fd2807SJeff Garzik 3818c6fd2807SJeff Garzik if (qc->err_mask) { 3819c6fd2807SJeff Garzik /* FIXME: Once EH migration is complete, 3820c6fd2807SJeff Garzik * generate sense data in this function, 3821c6fd2807SJeff Garzik * considering both err_mask and tf. 3822c6fd2807SJeff Garzik */ 382303faab78STejun Heo if (qc->flags & ATA_QCFLAG_RETRY) 3824c6fd2807SJeff Garzik ata_eh_qc_retry(qc); 382503faab78STejun Heo else 382603faab78STejun Heo ata_eh_qc_complete(qc); 3827c6fd2807SJeff Garzik } else { 3828c6fd2807SJeff Garzik if (qc->flags & ATA_QCFLAG_SENSE_VALID) { 3829c6fd2807SJeff Garzik ata_eh_qc_complete(qc); 3830c6fd2807SJeff Garzik } else { 3831c6fd2807SJeff Garzik /* feed zero TF to sense generation */ 3832c6fd2807SJeff Garzik memset(&qc->result_tf, 0, sizeof(qc->result_tf)); 3833c6fd2807SJeff Garzik ata_eh_qc_retry(qc); 3834c6fd2807SJeff Garzik } 3835c6fd2807SJeff Garzik } 3836c6fd2807SJeff Garzik } 3837da917d69STejun Heo 3838da917d69STejun Heo /* make sure nr_active_links is zero after EH */ 3839da917d69STejun Heo WARN_ON(ap->nr_active_links); 3840da917d69STejun Heo ap->nr_active_links = 0; 3841c6fd2807SJeff Garzik } 3842c6fd2807SJeff Garzik 3843c6fd2807SJeff Garzik /** 3844c6fd2807SJeff Garzik * ata_do_eh - do standard error handling 3845c6fd2807SJeff Garzik * @ap: host port to handle error for 3846a1efdabaSTejun Heo * 3847c6fd2807SJeff Garzik * @prereset: prereset method (can be NULL) 3848c6fd2807SJeff Garzik * @softreset: softreset method (can be NULL) 3849c6fd2807SJeff Garzik * @hardreset: hardreset method (can be NULL) 3850c6fd2807SJeff Garzik * @postreset: postreset method (can be NULL) 3851c6fd2807SJeff Garzik * 3852c6fd2807SJeff Garzik * Perform standard error handling sequence. 3853c6fd2807SJeff Garzik * 3854c6fd2807SJeff Garzik * LOCKING: 3855c6fd2807SJeff Garzik * Kernel thread context (may sleep). 3856c6fd2807SJeff Garzik */ 3857c6fd2807SJeff Garzik void ata_do_eh(struct ata_port *ap, ata_prereset_fn_t prereset, 3858c6fd2807SJeff Garzik ata_reset_fn_t softreset, ata_reset_fn_t hardreset, 3859c6fd2807SJeff Garzik ata_postreset_fn_t postreset) 3860c6fd2807SJeff Garzik { 38619b1e2658STejun Heo struct ata_device *dev; 38629b1e2658STejun Heo int rc; 38639b1e2658STejun Heo 38649b1e2658STejun Heo ata_eh_autopsy(ap); 38659b1e2658STejun Heo ata_eh_report(ap); 38669b1e2658STejun Heo 38679b1e2658STejun Heo rc = ata_eh_recover(ap, prereset, softreset, hardreset, postreset, 38689b1e2658STejun Heo NULL); 38699b1e2658STejun Heo if (rc) { 38701eca4365STejun Heo ata_for_each_dev(dev, &ap->link, ALL) 38719b1e2658STejun Heo ata_dev_disable(dev); 38729b1e2658STejun Heo } 38739b1e2658STejun Heo 3874c6fd2807SJeff Garzik ata_eh_finish(ap); 3875c6fd2807SJeff Garzik } 3876c6fd2807SJeff Garzik 3877a1efdabaSTejun Heo /** 3878a1efdabaSTejun Heo * ata_std_error_handler - standard error handler 3879a1efdabaSTejun Heo * @ap: host port to handle error for 3880a1efdabaSTejun Heo * 3881a1efdabaSTejun Heo * Standard error handler 3882a1efdabaSTejun Heo * 3883a1efdabaSTejun Heo * LOCKING: 3884a1efdabaSTejun Heo * Kernel thread context (may sleep). 3885a1efdabaSTejun Heo */ 3886a1efdabaSTejun Heo void ata_std_error_handler(struct ata_port *ap) 3887a1efdabaSTejun Heo { 3888a1efdabaSTejun Heo struct ata_port_operations *ops = ap->ops; 3889a1efdabaSTejun Heo ata_reset_fn_t hardreset = ops->hardreset; 3890a1efdabaSTejun Heo 389157c9efdfSTejun Heo /* ignore built-in hardreset if SCR access is not available */ 3892fe06e5f9STejun Heo if (hardreset == sata_std_hardreset && !sata_scr_valid(&ap->link)) 3893a1efdabaSTejun Heo hardreset = NULL; 3894a1efdabaSTejun Heo 3895a1efdabaSTejun Heo ata_do_eh(ap, ops->prereset, ops->softreset, hardreset, ops->postreset); 3896a1efdabaSTejun Heo } 3897a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_std_error_handler); 3898a1efdabaSTejun Heo 38996ffa01d8STejun Heo #ifdef CONFIG_PM 3900c6fd2807SJeff Garzik /** 3901c6fd2807SJeff Garzik * ata_eh_handle_port_suspend - perform port suspend operation 3902c6fd2807SJeff Garzik * @ap: port to suspend 3903c6fd2807SJeff Garzik * 3904c6fd2807SJeff Garzik * Suspend @ap. 3905c6fd2807SJeff Garzik * 3906c6fd2807SJeff Garzik * LOCKING: 3907c6fd2807SJeff Garzik * Kernel thread context (may sleep). 3908c6fd2807SJeff Garzik */ 3909c6fd2807SJeff Garzik static void ata_eh_handle_port_suspend(struct ata_port *ap) 3910c6fd2807SJeff Garzik { 3911c6fd2807SJeff Garzik unsigned long flags; 3912c6fd2807SJeff Garzik int rc = 0; 39133dc67440SAaron Lu struct ata_device *dev; 3914c6fd2807SJeff Garzik 3915c6fd2807SJeff Garzik /* are we suspending? */ 3916c6fd2807SJeff Garzik spin_lock_irqsave(ap->lock, flags); 3917c6fd2807SJeff Garzik if (!(ap->pflags & ATA_PFLAG_PM_PENDING) || 3918a7ff60dbSAaron Lu ap->pm_mesg.event & PM_EVENT_RESUME) { 3919c6fd2807SJeff Garzik spin_unlock_irqrestore(ap->lock, flags); 3920c6fd2807SJeff Garzik return; 3921c6fd2807SJeff Garzik } 3922c6fd2807SJeff Garzik spin_unlock_irqrestore(ap->lock, flags); 3923c6fd2807SJeff Garzik 3924c6fd2807SJeff Garzik WARN_ON(ap->pflags & ATA_PFLAG_SUSPENDED); 3925c6fd2807SJeff Garzik 39263dc67440SAaron Lu /* 39273dc67440SAaron Lu * If we have a ZPODD attached, check its zero 39283dc67440SAaron Lu * power ready status before the port is frozen. 3929a7ff60dbSAaron Lu * Only needed for runtime suspend. 39303dc67440SAaron Lu */ 3931a7ff60dbSAaron Lu if (PMSG_IS_AUTO(ap->pm_mesg)) { 39323dc67440SAaron Lu ata_for_each_dev(dev, &ap->link, ENABLED) { 39333dc67440SAaron Lu if (zpodd_dev_enabled(dev)) 39343dc67440SAaron Lu zpodd_on_suspend(dev); 39353dc67440SAaron Lu } 3936a7ff60dbSAaron Lu } 39373dc67440SAaron Lu 3938c6fd2807SJeff Garzik /* suspend */ 3939c6fd2807SJeff Garzik ata_eh_freeze_port(ap); 3940c6fd2807SJeff Garzik 3941c6fd2807SJeff Garzik if (ap->ops->port_suspend) 3942c6fd2807SJeff Garzik rc = ap->ops->port_suspend(ap, ap->pm_mesg); 3943c6fd2807SJeff Garzik 3944a7ff60dbSAaron Lu ata_acpi_set_state(ap, ap->pm_mesg); 39452a7b02eaSSergey Shtylyov 3946bc6e7c4bSDan Williams /* update the flags */ 3947c6fd2807SJeff Garzik spin_lock_irqsave(ap->lock, flags); 3948c6fd2807SJeff Garzik 3949c6fd2807SJeff Garzik ap->pflags &= ~ATA_PFLAG_PM_PENDING; 3950c6fd2807SJeff Garzik if (rc == 0) 3951c6fd2807SJeff Garzik ap->pflags |= ATA_PFLAG_SUSPENDED; 39524cb7c6f1SNiklas Cassel else if (ata_port_is_frozen(ap)) 3953c6fd2807SJeff Garzik ata_port_schedule_eh(ap); 3954c6fd2807SJeff Garzik 3955c6fd2807SJeff Garzik spin_unlock_irqrestore(ap->lock, flags); 3956c6fd2807SJeff Garzik 3957c6fd2807SJeff Garzik return; 3958c6fd2807SJeff Garzik } 3959c6fd2807SJeff Garzik 3960c6fd2807SJeff Garzik /** 3961c6fd2807SJeff Garzik * ata_eh_handle_port_resume - perform port resume operation 3962c6fd2807SJeff Garzik * @ap: port to resume 3963c6fd2807SJeff Garzik * 3964c6fd2807SJeff Garzik * Resume @ap. 3965c6fd2807SJeff Garzik * 3966c6fd2807SJeff Garzik * LOCKING: 3967c6fd2807SJeff Garzik * Kernel thread context (may sleep). 3968c6fd2807SJeff Garzik */ 3969c6fd2807SJeff Garzik static void ata_eh_handle_port_resume(struct ata_port *ap) 3970c6fd2807SJeff Garzik { 39716f9c1ea2STejun Heo struct ata_link *link; 39726f9c1ea2STejun Heo struct ata_device *dev; 3973c6fd2807SJeff Garzik unsigned long flags; 3974c6fd2807SJeff Garzik 3975c6fd2807SJeff Garzik /* are we resuming? */ 3976c6fd2807SJeff Garzik spin_lock_irqsave(ap->lock, flags); 3977c6fd2807SJeff Garzik if (!(ap->pflags & ATA_PFLAG_PM_PENDING) || 3978a7ff60dbSAaron Lu !(ap->pm_mesg.event & PM_EVENT_RESUME)) { 3979c6fd2807SJeff Garzik spin_unlock_irqrestore(ap->lock, flags); 3980c6fd2807SJeff Garzik return; 3981c6fd2807SJeff Garzik } 3982c6fd2807SJeff Garzik spin_unlock_irqrestore(ap->lock, flags); 3983c6fd2807SJeff Garzik 39849666f400STejun Heo WARN_ON(!(ap->pflags & ATA_PFLAG_SUSPENDED)); 3985c6fd2807SJeff Garzik 39866f9c1ea2STejun Heo /* 39876f9c1ea2STejun Heo * Error timestamps are in jiffies which doesn't run while 39886f9c1ea2STejun Heo * suspended and PHY events during resume isn't too uncommon. 39896f9c1ea2STejun Heo * When the two are combined, it can lead to unnecessary speed 39906f9c1ea2STejun Heo * downs if the machine is suspended and resumed repeatedly. 39916f9c1ea2STejun Heo * Clear error history. 39926f9c1ea2STejun Heo */ 39936f9c1ea2STejun Heo ata_for_each_link(link, ap, HOST_FIRST) 39946f9c1ea2STejun Heo ata_for_each_dev(dev, link, ALL) 39956f9c1ea2STejun Heo ata_ering_clear(&dev->ering); 39966f9c1ea2STejun Heo 3997a7ff60dbSAaron Lu ata_acpi_set_state(ap, ap->pm_mesg); 3998bd3adca5SShaohua Li 3999c6fd2807SJeff Garzik if (ap->ops->port_resume) 4000ae867937SKefeng Wang ap->ops->port_resume(ap); 4001c6fd2807SJeff Garzik 40026746544cSTejun Heo /* tell ACPI that we're resuming */ 40036746544cSTejun Heo ata_acpi_on_resume(ap); 40046746544cSTejun Heo 4005bc6e7c4bSDan Williams /* update the flags */ 4006c6fd2807SJeff Garzik spin_lock_irqsave(ap->lock, flags); 4007c6fd2807SJeff Garzik ap->pflags &= ~(ATA_PFLAG_PM_PENDING | ATA_PFLAG_SUSPENDED); 4008c6fd2807SJeff Garzik spin_unlock_irqrestore(ap->lock, flags); 4009c6fd2807SJeff Garzik } 40106ffa01d8STejun Heo #endif /* CONFIG_PM */ 4011