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); 1546ffa01d8STejun Heo #ifdef CONFIG_PM 155c6fd2807SJeff Garzik static void ata_eh_handle_port_suspend(struct ata_port *ap); 156c6fd2807SJeff Garzik static void ata_eh_handle_port_resume(struct ata_port *ap); 1576ffa01d8STejun Heo #else /* CONFIG_PM */ 1586ffa01d8STejun Heo static void ata_eh_handle_port_suspend(struct ata_port *ap) 1596ffa01d8STejun Heo { } 1606ffa01d8STejun Heo 1616ffa01d8STejun Heo static void ata_eh_handle_port_resume(struct ata_port *ap) 1626ffa01d8STejun Heo { } 1636ffa01d8STejun Heo #endif /* CONFIG_PM */ 164c6fd2807SJeff Garzik 1650d74d872SMathieu Malaterre static __printf(2, 0) void __ata_ehi_pushv_desc(struct ata_eh_info *ehi, 1660d74d872SMathieu Malaterre const char *fmt, va_list args) 167b64bbc39STejun Heo { 168b64bbc39STejun Heo ehi->desc_len += vscnprintf(ehi->desc + ehi->desc_len, 169b64bbc39STejun Heo ATA_EH_DESC_LEN - ehi->desc_len, 170b64bbc39STejun Heo fmt, args); 171b64bbc39STejun Heo } 172b64bbc39STejun Heo 173b64bbc39STejun Heo /** 174b64bbc39STejun Heo * __ata_ehi_push_desc - push error description without adding separator 175b64bbc39STejun Heo * @ehi: target EHI 176b64bbc39STejun Heo * @fmt: printf format string 177b64bbc39STejun Heo * 178b64bbc39STejun Heo * Format string according to @fmt and append it to @ehi->desc. 179b64bbc39STejun Heo * 180b64bbc39STejun Heo * LOCKING: 181b64bbc39STejun Heo * spin_lock_irqsave(host lock) 182b64bbc39STejun Heo */ 183b64bbc39STejun Heo void __ata_ehi_push_desc(struct ata_eh_info *ehi, const char *fmt, ...) 184b64bbc39STejun Heo { 185b64bbc39STejun Heo va_list args; 186b64bbc39STejun Heo 187b64bbc39STejun Heo va_start(args, fmt); 188b64bbc39STejun Heo __ata_ehi_pushv_desc(ehi, fmt, args); 189b64bbc39STejun Heo va_end(args); 190b64bbc39STejun Heo } 191a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(__ata_ehi_push_desc); 192b64bbc39STejun Heo 193b64bbc39STejun Heo /** 194b64bbc39STejun Heo * ata_ehi_push_desc - push error description with separator 195b64bbc39STejun Heo * @ehi: target EHI 196b64bbc39STejun Heo * @fmt: printf format string 197b64bbc39STejun Heo * 198b64bbc39STejun Heo * Format string according to @fmt and append it to @ehi->desc. 199b64bbc39STejun Heo * If @ehi->desc is not empty, ", " is added in-between. 200b64bbc39STejun Heo * 201b64bbc39STejun Heo * LOCKING: 202b64bbc39STejun Heo * spin_lock_irqsave(host lock) 203b64bbc39STejun Heo */ 204b64bbc39STejun Heo void ata_ehi_push_desc(struct ata_eh_info *ehi, const char *fmt, ...) 205b64bbc39STejun Heo { 206b64bbc39STejun Heo va_list args; 207b64bbc39STejun Heo 208b64bbc39STejun Heo if (ehi->desc_len) 209b64bbc39STejun Heo __ata_ehi_push_desc(ehi, ", "); 210b64bbc39STejun Heo 211b64bbc39STejun Heo va_start(args, fmt); 212b64bbc39STejun Heo __ata_ehi_pushv_desc(ehi, fmt, args); 213b64bbc39STejun Heo va_end(args); 214b64bbc39STejun Heo } 215a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_ehi_push_desc); 216b64bbc39STejun Heo 217b64bbc39STejun Heo /** 218b64bbc39STejun Heo * ata_ehi_clear_desc - clean error description 219b64bbc39STejun Heo * @ehi: target EHI 220b64bbc39STejun Heo * 221b64bbc39STejun Heo * Clear @ehi->desc. 222b64bbc39STejun Heo * 223b64bbc39STejun Heo * LOCKING: 224b64bbc39STejun Heo * spin_lock_irqsave(host lock) 225b64bbc39STejun Heo */ 226b64bbc39STejun Heo void ata_ehi_clear_desc(struct ata_eh_info *ehi) 227b64bbc39STejun Heo { 228b64bbc39STejun Heo ehi->desc[0] = '\0'; 229b64bbc39STejun Heo ehi->desc_len = 0; 230b64bbc39STejun Heo } 231a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_ehi_clear_desc); 232b64bbc39STejun Heo 233cbcdd875STejun Heo /** 234cbcdd875STejun Heo * ata_port_desc - append port description 235cbcdd875STejun Heo * @ap: target ATA port 236cbcdd875STejun Heo * @fmt: printf format string 237cbcdd875STejun Heo * 238cbcdd875STejun Heo * Format string according to @fmt and append it to port 239cbcdd875STejun Heo * description. If port description is not empty, " " is added 240cbcdd875STejun Heo * in-between. This function is to be used while initializing 241cbcdd875STejun Heo * ata_host. The description is printed on host registration. 242cbcdd875STejun Heo * 243cbcdd875STejun Heo * LOCKING: 244cbcdd875STejun Heo * None. 245cbcdd875STejun Heo */ 246cbcdd875STejun Heo void ata_port_desc(struct ata_port *ap, const char *fmt, ...) 247cbcdd875STejun Heo { 248cbcdd875STejun Heo va_list args; 249cbcdd875STejun Heo 250cbcdd875STejun Heo WARN_ON(!(ap->pflags & ATA_PFLAG_INITIALIZING)); 251cbcdd875STejun Heo 252cbcdd875STejun Heo if (ap->link.eh_info.desc_len) 253cbcdd875STejun Heo __ata_ehi_push_desc(&ap->link.eh_info, " "); 254cbcdd875STejun Heo 255cbcdd875STejun Heo va_start(args, fmt); 256cbcdd875STejun Heo __ata_ehi_pushv_desc(&ap->link.eh_info, fmt, args); 257cbcdd875STejun Heo va_end(args); 258cbcdd875STejun Heo } 259a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_port_desc); 260cbcdd875STejun Heo 261cbcdd875STejun Heo #ifdef CONFIG_PCI 262cbcdd875STejun Heo /** 263cbcdd875STejun Heo * ata_port_pbar_desc - append PCI BAR description 264cbcdd875STejun Heo * @ap: target ATA port 265cbcdd875STejun Heo * @bar: target PCI BAR 266cbcdd875STejun Heo * @offset: offset into PCI BAR 267cbcdd875STejun Heo * @name: name of the area 268cbcdd875STejun Heo * 269cbcdd875STejun Heo * If @offset is negative, this function formats a string which 270cbcdd875STejun Heo * contains the name, address, size and type of the BAR and 271cbcdd875STejun Heo * appends it to the port description. If @offset is zero or 272cbcdd875STejun Heo * positive, only name and offsetted address is appended. 273cbcdd875STejun Heo * 274cbcdd875STejun Heo * LOCKING: 275cbcdd875STejun Heo * None. 276cbcdd875STejun Heo */ 277cbcdd875STejun Heo void ata_port_pbar_desc(struct ata_port *ap, int bar, ssize_t offset, 278cbcdd875STejun Heo const char *name) 279cbcdd875STejun Heo { 280cbcdd875STejun Heo struct pci_dev *pdev = to_pci_dev(ap->host->dev); 281cbcdd875STejun Heo char *type = ""; 282cbcdd875STejun Heo unsigned long long start, len; 283cbcdd875STejun Heo 284cbcdd875STejun Heo if (pci_resource_flags(pdev, bar) & IORESOURCE_MEM) 285cbcdd875STejun Heo type = "m"; 286cbcdd875STejun Heo else if (pci_resource_flags(pdev, bar) & IORESOURCE_IO) 287cbcdd875STejun Heo type = "i"; 288cbcdd875STejun Heo 289cbcdd875STejun Heo start = (unsigned long long)pci_resource_start(pdev, bar); 290cbcdd875STejun Heo len = (unsigned long long)pci_resource_len(pdev, bar); 291cbcdd875STejun Heo 292cbcdd875STejun Heo if (offset < 0) 293cbcdd875STejun Heo ata_port_desc(ap, "%s %s%llu@0x%llx", name, type, len, start); 294cbcdd875STejun Heo else 295e6a73ab1SAndrew Morton ata_port_desc(ap, "%s 0x%llx", name, 296e6a73ab1SAndrew Morton start + (unsigned long long)offset); 297cbcdd875STejun Heo } 298a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_port_pbar_desc); 299cbcdd875STejun Heo #endif /* CONFIG_PCI */ 300cbcdd875STejun Heo 30187fbc5a0STejun Heo static int ata_lookup_timeout_table(u8 cmd) 30287fbc5a0STejun Heo { 30387fbc5a0STejun Heo int i; 30487fbc5a0STejun Heo 30587fbc5a0STejun Heo for (i = 0; i < ATA_EH_CMD_TIMEOUT_TABLE_SIZE; i++) { 30687fbc5a0STejun Heo const u8 *cur; 30787fbc5a0STejun Heo 30887fbc5a0STejun Heo for (cur = ata_eh_cmd_timeout_table[i].commands; *cur; cur++) 30987fbc5a0STejun Heo if (*cur == cmd) 31087fbc5a0STejun Heo return i; 31187fbc5a0STejun Heo } 31287fbc5a0STejun Heo 31387fbc5a0STejun Heo return -1; 31487fbc5a0STejun Heo } 31587fbc5a0STejun Heo 31687fbc5a0STejun Heo /** 31787fbc5a0STejun Heo * ata_internal_cmd_timeout - determine timeout for an internal command 31887fbc5a0STejun Heo * @dev: target device 31987fbc5a0STejun Heo * @cmd: internal command to be issued 32087fbc5a0STejun Heo * 32187fbc5a0STejun Heo * Determine timeout for internal command @cmd for @dev. 32287fbc5a0STejun Heo * 32387fbc5a0STejun Heo * LOCKING: 32487fbc5a0STejun Heo * EH context. 32587fbc5a0STejun Heo * 32687fbc5a0STejun Heo * RETURNS: 32787fbc5a0STejun Heo * Determined timeout. 32887fbc5a0STejun Heo */ 329e06233f9SSergey Shtylyov unsigned int ata_internal_cmd_timeout(struct ata_device *dev, u8 cmd) 33087fbc5a0STejun Heo { 33187fbc5a0STejun Heo struct ata_eh_context *ehc = &dev->link->eh_context; 33287fbc5a0STejun Heo int ent = ata_lookup_timeout_table(cmd); 33387fbc5a0STejun Heo int idx; 33487fbc5a0STejun Heo 33587fbc5a0STejun Heo if (ent < 0) 33687fbc5a0STejun Heo return ATA_EH_CMD_DFL_TIMEOUT; 33787fbc5a0STejun Heo 33887fbc5a0STejun Heo idx = ehc->cmd_timeout_idx[dev->devno][ent]; 33987fbc5a0STejun Heo return ata_eh_cmd_timeout_table[ent].timeouts[idx]; 34087fbc5a0STejun Heo } 34187fbc5a0STejun Heo 34287fbc5a0STejun Heo /** 34387fbc5a0STejun Heo * ata_internal_cmd_timed_out - notification for internal command timeout 34487fbc5a0STejun Heo * @dev: target device 34587fbc5a0STejun Heo * @cmd: internal command which timed out 34687fbc5a0STejun Heo * 34787fbc5a0STejun Heo * Notify EH that internal command @cmd for @dev timed out. This 34887fbc5a0STejun Heo * function should be called only for commands whose timeouts are 34987fbc5a0STejun Heo * determined using ata_internal_cmd_timeout(). 35087fbc5a0STejun Heo * 35187fbc5a0STejun Heo * LOCKING: 35287fbc5a0STejun Heo * EH context. 35387fbc5a0STejun Heo */ 35487fbc5a0STejun Heo void ata_internal_cmd_timed_out(struct ata_device *dev, u8 cmd) 35587fbc5a0STejun Heo { 35687fbc5a0STejun Heo struct ata_eh_context *ehc = &dev->link->eh_context; 35787fbc5a0STejun Heo int ent = ata_lookup_timeout_table(cmd); 35887fbc5a0STejun Heo int idx; 35987fbc5a0STejun Heo 36087fbc5a0STejun Heo if (ent < 0) 36187fbc5a0STejun Heo return; 36287fbc5a0STejun Heo 36387fbc5a0STejun Heo idx = ehc->cmd_timeout_idx[dev->devno][ent]; 364e06233f9SSergey Shtylyov if (ata_eh_cmd_timeout_table[ent].timeouts[idx + 1] != UINT_MAX) 36587fbc5a0STejun Heo ehc->cmd_timeout_idx[dev->devno][ent]++; 36687fbc5a0STejun Heo } 36787fbc5a0STejun Heo 3683884f7b0STejun Heo static void ata_ering_record(struct ata_ering *ering, unsigned int eflags, 369c6fd2807SJeff Garzik unsigned int err_mask) 370c6fd2807SJeff Garzik { 371c6fd2807SJeff Garzik struct ata_ering_entry *ent; 372c6fd2807SJeff Garzik 373c6fd2807SJeff Garzik WARN_ON(!err_mask); 374c6fd2807SJeff Garzik 375c6fd2807SJeff Garzik ering->cursor++; 376c6fd2807SJeff Garzik ering->cursor %= ATA_ERING_SIZE; 377c6fd2807SJeff Garzik 378c6fd2807SJeff Garzik ent = &ering->ring[ering->cursor]; 3793884f7b0STejun Heo ent->eflags = eflags; 380c6fd2807SJeff Garzik ent->err_mask = err_mask; 381c6fd2807SJeff Garzik ent->timestamp = get_jiffies_64(); 382c6fd2807SJeff Garzik } 383c6fd2807SJeff Garzik 38476326ac1STejun Heo static struct ata_ering_entry *ata_ering_top(struct ata_ering *ering) 38576326ac1STejun Heo { 38676326ac1STejun Heo struct ata_ering_entry *ent = &ering->ring[ering->cursor]; 38776326ac1STejun Heo 38876326ac1STejun Heo if (ent->err_mask) 38976326ac1STejun Heo return ent; 39076326ac1STejun Heo return NULL; 39176326ac1STejun Heo } 39276326ac1STejun Heo 393d9027470SGwendal Grignou int ata_ering_map(struct ata_ering *ering, 394c6fd2807SJeff Garzik int (*map_fn)(struct ata_ering_entry *, void *), 395c6fd2807SJeff Garzik void *arg) 396c6fd2807SJeff Garzik { 397c6fd2807SJeff Garzik int idx, rc = 0; 398c6fd2807SJeff Garzik struct ata_ering_entry *ent; 399c6fd2807SJeff Garzik 400c6fd2807SJeff Garzik idx = ering->cursor; 401c6fd2807SJeff Garzik do { 402c6fd2807SJeff Garzik ent = &ering->ring[idx]; 403c6fd2807SJeff Garzik if (!ent->err_mask) 404c6fd2807SJeff Garzik break; 405c6fd2807SJeff Garzik rc = map_fn(ent, arg); 406c6fd2807SJeff Garzik if (rc) 407c6fd2807SJeff Garzik break; 408c6fd2807SJeff Garzik idx = (idx - 1 + ATA_ERING_SIZE) % ATA_ERING_SIZE; 409c6fd2807SJeff Garzik } while (idx != ering->cursor); 410c6fd2807SJeff Garzik 411c6fd2807SJeff Garzik return rc; 412c6fd2807SJeff Garzik } 413c6fd2807SJeff Garzik 41460428407SH Hartley Sweeten static int ata_ering_clear_cb(struct ata_ering_entry *ent, void *void_arg) 415d9027470SGwendal Grignou { 416d9027470SGwendal Grignou ent->eflags |= ATA_EFLAG_OLD_ER; 417d9027470SGwendal Grignou return 0; 418d9027470SGwendal Grignou } 419d9027470SGwendal Grignou 420d9027470SGwendal Grignou static void ata_ering_clear(struct ata_ering *ering) 421d9027470SGwendal Grignou { 422d9027470SGwendal Grignou ata_ering_map(ering, ata_ering_clear_cb, NULL); 423d9027470SGwendal Grignou } 424d9027470SGwendal Grignou 425c6fd2807SJeff Garzik static unsigned int ata_eh_dev_action(struct ata_device *dev) 426c6fd2807SJeff Garzik { 4279af5c9c9STejun Heo struct ata_eh_context *ehc = &dev->link->eh_context; 428c6fd2807SJeff Garzik 429c6fd2807SJeff Garzik return ehc->i.action | ehc->i.dev_action[dev->devno]; 430c6fd2807SJeff Garzik } 431c6fd2807SJeff Garzik 432f58229f8STejun Heo static void ata_eh_clear_action(struct ata_link *link, struct ata_device *dev, 433c6fd2807SJeff Garzik struct ata_eh_info *ehi, unsigned int action) 434c6fd2807SJeff Garzik { 435f58229f8STejun Heo struct ata_device *tdev; 436c6fd2807SJeff Garzik 437c6fd2807SJeff Garzik if (!dev) { 438c6fd2807SJeff Garzik ehi->action &= ~action; 4391eca4365STejun Heo ata_for_each_dev(tdev, link, ALL) 440f58229f8STejun Heo ehi->dev_action[tdev->devno] &= ~action; 441c6fd2807SJeff Garzik } else { 442c6fd2807SJeff Garzik /* doesn't make sense for port-wide EH actions */ 443c6fd2807SJeff Garzik WARN_ON(!(action & ATA_EH_PERDEV_MASK)); 444c6fd2807SJeff Garzik 445c6fd2807SJeff Garzik /* break ehi->action into ehi->dev_action */ 446c6fd2807SJeff Garzik if (ehi->action & action) { 4471eca4365STejun Heo ata_for_each_dev(tdev, link, ALL) 448f58229f8STejun Heo ehi->dev_action[tdev->devno] |= 449f58229f8STejun Heo ehi->action & action; 450c6fd2807SJeff Garzik ehi->action &= ~action; 451c6fd2807SJeff Garzik } 452c6fd2807SJeff Garzik 453c6fd2807SJeff Garzik /* turn off the specified per-dev action */ 454c6fd2807SJeff Garzik ehi->dev_action[dev->devno] &= ~action; 455c6fd2807SJeff Garzik } 456c6fd2807SJeff Garzik } 457c6fd2807SJeff Garzik 458c6fd2807SJeff Garzik /** 459c0c362b6STejun Heo * ata_eh_acquire - acquire EH ownership 460c0c362b6STejun Heo * @ap: ATA port to acquire EH ownership for 461c0c362b6STejun Heo * 462c0c362b6STejun Heo * Acquire EH ownership for @ap. This is the basic exclusion 463c0c362b6STejun Heo * mechanism for ports sharing a host. Only one port hanging off 464c0c362b6STejun Heo * the same host can claim the ownership of EH. 465c0c362b6STejun Heo * 466c0c362b6STejun Heo * LOCKING: 467c0c362b6STejun Heo * EH context. 468c0c362b6STejun Heo */ 469c0c362b6STejun Heo void ata_eh_acquire(struct ata_port *ap) 470c0c362b6STejun Heo { 471c0c362b6STejun Heo mutex_lock(&ap->host->eh_mutex); 472c0c362b6STejun Heo WARN_ON_ONCE(ap->host->eh_owner); 473c0c362b6STejun Heo ap->host->eh_owner = current; 474c0c362b6STejun Heo } 475c0c362b6STejun Heo 476c0c362b6STejun Heo /** 477c0c362b6STejun Heo * ata_eh_release - release EH ownership 478c0c362b6STejun Heo * @ap: ATA port to release EH ownership for 479c0c362b6STejun Heo * 480c0c362b6STejun Heo * Release EH ownership for @ap if the caller. The caller must 481c0c362b6STejun Heo * have acquired EH ownership using ata_eh_acquire() previously. 482c0c362b6STejun Heo * 483c0c362b6STejun Heo * LOCKING: 484c0c362b6STejun Heo * EH context. 485c0c362b6STejun Heo */ 486c0c362b6STejun Heo void ata_eh_release(struct ata_port *ap) 487c0c362b6STejun Heo { 488c0c362b6STejun Heo WARN_ON_ONCE(ap->host->eh_owner != current); 489c0c362b6STejun Heo ap->host->eh_owner = NULL; 490c0c362b6STejun Heo mutex_unlock(&ap->host->eh_mutex); 491c0c362b6STejun Heo } 492c0c362b6STejun Heo 493ece180d1STejun Heo static void ata_eh_unload(struct ata_port *ap) 494ece180d1STejun Heo { 495ece180d1STejun Heo struct ata_link *link; 496ece180d1STejun Heo struct ata_device *dev; 497ece180d1STejun Heo unsigned long flags; 498ece180d1STejun Heo 499ece180d1STejun Heo /* Restore SControl IPM and SPD for the next driver and 500ece180d1STejun Heo * disable attached devices. 501ece180d1STejun Heo */ 502ece180d1STejun Heo ata_for_each_link(link, ap, PMP_FIRST) { 503ece180d1STejun Heo sata_scr_write(link, SCR_CONTROL, link->saved_scontrol & 0xff0); 504ece180d1STejun Heo ata_for_each_dev(dev, link, ALL) 505ece180d1STejun Heo ata_dev_disable(dev); 506ece180d1STejun Heo } 507ece180d1STejun Heo 508ece180d1STejun Heo /* freeze and set UNLOADED */ 509ece180d1STejun Heo spin_lock_irqsave(ap->lock, flags); 510ece180d1STejun Heo 511ece180d1STejun Heo ata_port_freeze(ap); /* won't be thawed */ 512ece180d1STejun Heo ap->pflags &= ~ATA_PFLAG_EH_PENDING; /* clear pending from freeze */ 513ece180d1STejun Heo ap->pflags |= ATA_PFLAG_UNLOADED; 514ece180d1STejun Heo 515ece180d1STejun Heo spin_unlock_irqrestore(ap->lock, flags); 516ece180d1STejun Heo } 517ece180d1STejun Heo 518c6fd2807SJeff Garzik /** 519c6fd2807SJeff Garzik * ata_scsi_error - SCSI layer error handler callback 520c6fd2807SJeff Garzik * @host: SCSI host on which error occurred 521c6fd2807SJeff Garzik * 522c6fd2807SJeff Garzik * Handles SCSI-layer-thrown error events. 523c6fd2807SJeff Garzik * 524c6fd2807SJeff Garzik * LOCKING: 525c6fd2807SJeff Garzik * Inherited from SCSI layer (none, can sleep) 526c6fd2807SJeff Garzik * 527c6fd2807SJeff Garzik * RETURNS: 528c6fd2807SJeff Garzik * Zero. 529c6fd2807SJeff Garzik */ 530c6fd2807SJeff Garzik void ata_scsi_error(struct Scsi_Host *host) 531c6fd2807SJeff Garzik { 532c6fd2807SJeff Garzik struct ata_port *ap = ata_shost_to_port(host); 533c6fd2807SJeff Garzik unsigned long flags; 534c34aeebcSJames Bottomley LIST_HEAD(eh_work_q); 535c6fd2807SJeff Garzik 536c34aeebcSJames Bottomley spin_lock_irqsave(host->host_lock, flags); 537c34aeebcSJames Bottomley list_splice_init(&host->eh_cmd_q, &eh_work_q); 538c34aeebcSJames Bottomley spin_unlock_irqrestore(host->host_lock, flags); 539c34aeebcSJames Bottomley 5400e0b494cSJames Bottomley ata_scsi_cmd_error_handler(host, ap, &eh_work_q); 5410e0b494cSJames Bottomley 5420e0b494cSJames Bottomley /* If we timed raced normal completion and there is nothing to 5430e0b494cSJames Bottomley recover nr_timedout == 0 why exactly are we doing error recovery ? */ 5440e0b494cSJames Bottomley ata_scsi_port_error_handler(host, ap); 5450e0b494cSJames Bottomley 5460e0b494cSJames Bottomley /* finish or retry handled scmd's and clean up */ 54772d8c36eSWei Fang WARN_ON(!list_empty(&eh_work_q)); 5480e0b494cSJames Bottomley 5490e0b494cSJames Bottomley } 5500e0b494cSJames Bottomley 5510e0b494cSJames Bottomley /** 5520e0b494cSJames Bottomley * ata_scsi_cmd_error_handler - error callback for a list of commands 5530e0b494cSJames Bottomley * @host: scsi host containing the port 5540e0b494cSJames Bottomley * @ap: ATA port within the host 5550e0b494cSJames Bottomley * @eh_work_q: list of commands to process 5560e0b494cSJames Bottomley * 5570e0b494cSJames Bottomley * process the given list of commands and return those finished to the 5580e0b494cSJames Bottomley * ap->eh_done_q. This function is the first part of the libata error 5590e0b494cSJames Bottomley * handler which processes a given list of failed commands. 5600e0b494cSJames Bottomley */ 5610e0b494cSJames Bottomley void ata_scsi_cmd_error_handler(struct Scsi_Host *host, struct ata_port *ap, 5620e0b494cSJames Bottomley struct list_head *eh_work_q) 5630e0b494cSJames Bottomley { 5640e0b494cSJames Bottomley int i; 5650e0b494cSJames Bottomley unsigned long flags; 5660e0b494cSJames Bottomley 567c429137aSTejun Heo /* make sure sff pio task is not running */ 568c429137aSTejun Heo ata_sff_flush_pio_task(ap); 569c6fd2807SJeff Garzik 570cca3974eSJeff Garzik /* synchronize with host lock and sort out timeouts */ 571c6fd2807SJeff Garzik 572c6fd2807SJeff Garzik /* For new EH, all qcs are finished in one of three ways - 573c6fd2807SJeff Garzik * normal completion, error completion, and SCSI timeout. 574c96f1732SAlan Cox * Both completions can race against SCSI timeout. When normal 575c6fd2807SJeff Garzik * completion wins, the qc never reaches EH. When error 576c6fd2807SJeff Garzik * completion wins, the qc has ATA_QCFLAG_FAILED set. 577c6fd2807SJeff Garzik * 578c6fd2807SJeff Garzik * When SCSI timeout wins, things are a bit more complex. 579c6fd2807SJeff Garzik * Normal or error completion can occur after the timeout but 580c6fd2807SJeff Garzik * before this point. In such cases, both types of 581c6fd2807SJeff Garzik * completions are honored. A scmd is determined to have 582c6fd2807SJeff Garzik * timed out iff its associated qc is active and not failed. 583c6fd2807SJeff Garzik */ 584a4f08141SPaul E. McKenney spin_lock_irqsave(ap->lock, flags); 585c6fd2807SJeff Garzik if (ap->ops->error_handler) { 586c6fd2807SJeff Garzik struct scsi_cmnd *scmd, *tmp; 587c6fd2807SJeff Garzik int nr_timedout = 0; 588c6fd2807SJeff Garzik 589c96f1732SAlan Cox /* This must occur under the ap->lock as we don't want 590c96f1732SAlan Cox a polled recovery to race the real interrupt handler 591c96f1732SAlan Cox 592c96f1732SAlan Cox The lost_interrupt handler checks for any completed but 593c96f1732SAlan Cox non-notified command and completes much like an IRQ handler. 594c96f1732SAlan Cox 595c96f1732SAlan Cox We then fall into the error recovery code which will treat 596c96f1732SAlan Cox this as if normal completion won the race */ 597c96f1732SAlan Cox 598c96f1732SAlan Cox if (ap->ops->lost_interrupt) 599c96f1732SAlan Cox ap->ops->lost_interrupt(ap); 600c96f1732SAlan Cox 6010e0b494cSJames Bottomley list_for_each_entry_safe(scmd, tmp, eh_work_q, eh_entry) { 602c6fd2807SJeff Garzik struct ata_queued_cmd *qc; 603c6fd2807SJeff Garzik 604258c4e5cSJens Axboe ata_qc_for_each_raw(ap, qc, i) { 605c6fd2807SJeff Garzik if (qc->flags & ATA_QCFLAG_ACTIVE && 606c6fd2807SJeff Garzik qc->scsicmd == scmd) 607c6fd2807SJeff Garzik break; 608c6fd2807SJeff Garzik } 609c6fd2807SJeff Garzik 610c6fd2807SJeff Garzik if (i < ATA_MAX_QUEUE) { 611c6fd2807SJeff Garzik /* the scmd has an associated qc */ 612c6fd2807SJeff Garzik if (!(qc->flags & ATA_QCFLAG_FAILED)) { 613c6fd2807SJeff Garzik /* which hasn't failed yet, timeout */ 614c6fd2807SJeff Garzik qc->err_mask |= AC_ERR_TIMEOUT; 615c6fd2807SJeff Garzik qc->flags |= ATA_QCFLAG_FAILED; 616c6fd2807SJeff Garzik nr_timedout++; 617c6fd2807SJeff Garzik } 618c6fd2807SJeff Garzik } else { 619c6fd2807SJeff Garzik /* Normal completion occurred after 620c6fd2807SJeff Garzik * SCSI timeout but before this point. 621c6fd2807SJeff Garzik * Successfully complete it. 622c6fd2807SJeff Garzik */ 623c6fd2807SJeff Garzik scmd->retries = scmd->allowed; 624c6fd2807SJeff Garzik scsi_eh_finish_cmd(scmd, &ap->eh_done_q); 625c6fd2807SJeff Garzik } 626c6fd2807SJeff Garzik } 627c6fd2807SJeff Garzik 628c6fd2807SJeff Garzik /* If we have timed out qcs. They belong to EH from 629c6fd2807SJeff Garzik * this point but the state of the controller is 630c6fd2807SJeff Garzik * unknown. Freeze the port to make sure the IRQ 631c6fd2807SJeff Garzik * handler doesn't diddle with those qcs. This must 632c6fd2807SJeff Garzik * be done atomically w.r.t. setting QCFLAG_FAILED. 633c6fd2807SJeff Garzik */ 634c6fd2807SJeff Garzik if (nr_timedout) 635c6fd2807SJeff Garzik __ata_port_freeze(ap); 636c6fd2807SJeff Garzik 637a1e10f7eSTejun Heo 638a1e10f7eSTejun Heo /* initialize eh_tries */ 639a1e10f7eSTejun Heo ap->eh_tries = ATA_EH_MAX_TRIES; 640a4f08141SPaul E. McKenney } 641a4f08141SPaul E. McKenney spin_unlock_irqrestore(ap->lock, flags); 642c6fd2807SJeff Garzik 6430e0b494cSJames Bottomley } 6440e0b494cSJames Bottomley EXPORT_SYMBOL(ata_scsi_cmd_error_handler); 6450e0b494cSJames Bottomley 6460e0b494cSJames Bottomley /** 6470e0b494cSJames Bottomley * ata_scsi_port_error_handler - recover the port after the commands 6480e0b494cSJames Bottomley * @host: SCSI host containing the port 6490e0b494cSJames Bottomley * @ap: the ATA port 6500e0b494cSJames Bottomley * 6510e0b494cSJames Bottomley * Handle the recovery of the port @ap after all the commands 6520e0b494cSJames Bottomley * have been recovered. 6530e0b494cSJames Bottomley */ 6540e0b494cSJames Bottomley void ata_scsi_port_error_handler(struct Scsi_Host *host, struct ata_port *ap) 6550e0b494cSJames Bottomley { 6560e0b494cSJames Bottomley unsigned long flags; 657c96f1732SAlan Cox 658c6fd2807SJeff Garzik /* invoke error handler */ 659c6fd2807SJeff Garzik if (ap->ops->error_handler) { 660cf1b86c8STejun Heo struct ata_link *link; 661cf1b86c8STejun Heo 662c0c362b6STejun Heo /* acquire EH ownership */ 663c0c362b6STejun Heo ata_eh_acquire(ap); 664c0c362b6STejun Heo repeat: 6655ddf24c5STejun Heo /* kill fast drain timer */ 6665ddf24c5STejun Heo del_timer_sync(&ap->fastdrain_timer); 6675ddf24c5STejun Heo 668c6fd2807SJeff Garzik /* process port resume request */ 669c6fd2807SJeff Garzik ata_eh_handle_port_resume(ap); 670c6fd2807SJeff Garzik 671c6fd2807SJeff Garzik /* fetch & clear EH info */ 672c6fd2807SJeff Garzik spin_lock_irqsave(ap->lock, flags); 673c6fd2807SJeff Garzik 6741eca4365STejun Heo ata_for_each_link(link, ap, HOST_FIRST) { 67500115e0fSTejun Heo struct ata_eh_context *ehc = &link->eh_context; 67600115e0fSTejun Heo struct ata_device *dev; 67700115e0fSTejun Heo 678cf1b86c8STejun Heo memset(&link->eh_context, 0, sizeof(link->eh_context)); 679cf1b86c8STejun Heo link->eh_context.i = link->eh_info; 680cf1b86c8STejun Heo memset(&link->eh_info, 0, sizeof(link->eh_info)); 68100115e0fSTejun Heo 6821eca4365STejun Heo ata_for_each_dev(dev, link, ENABLED) { 68300115e0fSTejun Heo int devno = dev->devno; 68400115e0fSTejun Heo 68500115e0fSTejun Heo ehc->saved_xfer_mode[devno] = dev->xfer_mode; 68600115e0fSTejun Heo if (ata_ncq_enabled(dev)) 68700115e0fSTejun Heo ehc->saved_ncq_enabled |= 1 << devno; 68800115e0fSTejun Heo } 689cf1b86c8STejun Heo } 690c6fd2807SJeff Garzik 691c6fd2807SJeff Garzik ap->pflags |= ATA_PFLAG_EH_IN_PROGRESS; 692c6fd2807SJeff Garzik ap->pflags &= ~ATA_PFLAG_EH_PENDING; 693da917d69STejun Heo ap->excl_link = NULL; /* don't maintain exclusion over EH */ 694c6fd2807SJeff Garzik 695c6fd2807SJeff Garzik spin_unlock_irqrestore(ap->lock, flags); 696c6fd2807SJeff Garzik 697c6fd2807SJeff Garzik /* invoke EH, skip if unloading or suspended */ 698c6fd2807SJeff Garzik if (!(ap->pflags & (ATA_PFLAG_UNLOADING | ATA_PFLAG_SUSPENDED))) 699c6fd2807SJeff Garzik ap->ops->error_handler(ap); 700ece180d1STejun Heo else { 701ece180d1STejun Heo /* if unloading, commence suicide */ 702ece180d1STejun Heo if ((ap->pflags & ATA_PFLAG_UNLOADING) && 703ece180d1STejun Heo !(ap->pflags & ATA_PFLAG_UNLOADED)) 704ece180d1STejun Heo ata_eh_unload(ap); 705c6fd2807SJeff Garzik ata_eh_finish(ap); 706ece180d1STejun Heo } 707c6fd2807SJeff Garzik 708c6fd2807SJeff Garzik /* process port suspend request */ 709c6fd2807SJeff Garzik ata_eh_handle_port_suspend(ap); 710c6fd2807SJeff Garzik 71125985edcSLucas De Marchi /* Exception might have happened after ->error_handler 712c6fd2807SJeff Garzik * recovered the port but before this point. Repeat 713c6fd2807SJeff Garzik * EH in such case. 714c6fd2807SJeff Garzik */ 715c6fd2807SJeff Garzik spin_lock_irqsave(ap->lock, flags); 716c6fd2807SJeff Garzik 717c6fd2807SJeff Garzik if (ap->pflags & ATA_PFLAG_EH_PENDING) { 718a1e10f7eSTejun Heo if (--ap->eh_tries) { 719c6fd2807SJeff Garzik spin_unlock_irqrestore(ap->lock, flags); 720c6fd2807SJeff Garzik goto repeat; 721c6fd2807SJeff Garzik } 722a9a79dfeSJoe Perches ata_port_err(ap, 723a9a79dfeSJoe Perches "EH pending after %d tries, giving up\n", 724a9a79dfeSJoe Perches ATA_EH_MAX_TRIES); 725914616a3STejun Heo ap->pflags &= ~ATA_PFLAG_EH_PENDING; 726c6fd2807SJeff Garzik } 727c6fd2807SJeff Garzik 728c6fd2807SJeff Garzik /* this run is complete, make sure EH info is clear */ 7291eca4365STejun Heo ata_for_each_link(link, ap, HOST_FIRST) 730cf1b86c8STejun Heo memset(&link->eh_info, 0, sizeof(link->eh_info)); 731c6fd2807SJeff Garzik 732e4a9c373SDan Williams /* end eh (clear host_eh_scheduled) while holding 733e4a9c373SDan Williams * ap->lock such that if exception occurs after this 734e4a9c373SDan Williams * point but before EH completion, SCSI midlayer will 735c6fd2807SJeff Garzik * re-initiate EH. 736c6fd2807SJeff Garzik */ 737e4a9c373SDan Williams ap->ops->end_eh(ap); 738c6fd2807SJeff Garzik 739c6fd2807SJeff Garzik spin_unlock_irqrestore(ap->lock, flags); 740c0c362b6STejun Heo ata_eh_release(ap); 741c6fd2807SJeff Garzik } else { 7429af5c9c9STejun Heo WARN_ON(ata_qc_from_tag(ap, ap->link.active_tag) == NULL); 743c6fd2807SJeff Garzik ap->ops->eng_timeout(ap); 744c6fd2807SJeff Garzik } 745c6fd2807SJeff Garzik 746c6fd2807SJeff Garzik scsi_eh_flush_done_q(&ap->eh_done_q); 747c6fd2807SJeff Garzik 748c6fd2807SJeff Garzik /* clean up */ 749c6fd2807SJeff Garzik spin_lock_irqsave(ap->lock, flags); 750c6fd2807SJeff Garzik 751c6fd2807SJeff Garzik if (ap->pflags & ATA_PFLAG_LOADING) 752c6fd2807SJeff Garzik ap->pflags &= ~ATA_PFLAG_LOADING; 7536f54120eSJason Yan else if ((ap->pflags & ATA_PFLAG_SCSI_HOTPLUG) && 7546f54120eSJason Yan !(ap->flags & ATA_FLAG_SAS_HOST)) 755ad72cf98STejun Heo schedule_delayed_work(&ap->hotplug_task, 0); 756c6fd2807SJeff Garzik 757c6fd2807SJeff Garzik if (ap->pflags & ATA_PFLAG_RECOVERED) 758a9a79dfeSJoe Perches ata_port_info(ap, "EH complete\n"); 759c6fd2807SJeff Garzik 760c6fd2807SJeff Garzik ap->pflags &= ~(ATA_PFLAG_SCSI_HOTPLUG | ATA_PFLAG_RECOVERED); 761c6fd2807SJeff Garzik 762c6fd2807SJeff Garzik /* tell wait_eh that we're done */ 763c6fd2807SJeff Garzik ap->pflags &= ~ATA_PFLAG_EH_IN_PROGRESS; 764c6fd2807SJeff Garzik wake_up_all(&ap->eh_wait_q); 765c6fd2807SJeff Garzik 766c6fd2807SJeff Garzik spin_unlock_irqrestore(ap->lock, flags); 767c6fd2807SJeff Garzik } 7680e0b494cSJames Bottomley EXPORT_SYMBOL_GPL(ata_scsi_port_error_handler); 769c6fd2807SJeff Garzik 770c6fd2807SJeff Garzik /** 771c6fd2807SJeff Garzik * ata_port_wait_eh - Wait for the currently pending EH to complete 772c6fd2807SJeff Garzik * @ap: Port to wait EH for 773c6fd2807SJeff Garzik * 774c6fd2807SJeff Garzik * Wait until the currently pending EH is complete. 775c6fd2807SJeff Garzik * 776c6fd2807SJeff Garzik * LOCKING: 777c6fd2807SJeff Garzik * Kernel thread context (may sleep). 778c6fd2807SJeff Garzik */ 779c6fd2807SJeff Garzik void ata_port_wait_eh(struct ata_port *ap) 780c6fd2807SJeff Garzik { 781c6fd2807SJeff Garzik unsigned long flags; 782c6fd2807SJeff Garzik DEFINE_WAIT(wait); 783c6fd2807SJeff Garzik 784c6fd2807SJeff Garzik retry: 785c6fd2807SJeff Garzik spin_lock_irqsave(ap->lock, flags); 786c6fd2807SJeff Garzik 787c6fd2807SJeff Garzik while (ap->pflags & (ATA_PFLAG_EH_PENDING | ATA_PFLAG_EH_IN_PROGRESS)) { 788c6fd2807SJeff Garzik prepare_to_wait(&ap->eh_wait_q, &wait, TASK_UNINTERRUPTIBLE); 789c6fd2807SJeff Garzik spin_unlock_irqrestore(ap->lock, flags); 790c6fd2807SJeff Garzik schedule(); 791c6fd2807SJeff Garzik spin_lock_irqsave(ap->lock, flags); 792c6fd2807SJeff Garzik } 793c6fd2807SJeff Garzik finish_wait(&ap->eh_wait_q, &wait); 794c6fd2807SJeff Garzik 795c6fd2807SJeff Garzik spin_unlock_irqrestore(ap->lock, flags); 796c6fd2807SJeff Garzik 797c6fd2807SJeff Garzik /* make sure SCSI EH is complete */ 798cca3974eSJeff Garzik if (scsi_host_in_recovery(ap->scsi_host)) { 79997750cebSTejun Heo ata_msleep(ap, 10); 800c6fd2807SJeff Garzik goto retry; 801c6fd2807SJeff Garzik } 802c6fd2807SJeff Garzik } 80381c757bcSDan Williams EXPORT_SYMBOL_GPL(ata_port_wait_eh); 804c6fd2807SJeff Garzik 805afae461aSSergey Shtylyov static unsigned int ata_eh_nr_in_flight(struct ata_port *ap) 8065ddf24c5STejun Heo { 807258c4e5cSJens Axboe struct ata_queued_cmd *qc; 8085ddf24c5STejun Heo unsigned int tag; 809afae461aSSergey Shtylyov unsigned int nr = 0; 8105ddf24c5STejun Heo 8115ddf24c5STejun Heo /* count only non-internal commands */ 812258c4e5cSJens Axboe ata_qc_for_each(ap, qc, tag) { 813258c4e5cSJens Axboe if (qc) 8145ddf24c5STejun Heo nr++; 8159d207accSJens Axboe } 8165ddf24c5STejun Heo 8175ddf24c5STejun Heo return nr; 8185ddf24c5STejun Heo } 8195ddf24c5STejun Heo 820b93ab338SKees Cook void ata_eh_fastdrain_timerfn(struct timer_list *t) 8215ddf24c5STejun Heo { 822b93ab338SKees Cook struct ata_port *ap = from_timer(ap, t, fastdrain_timer); 8235ddf24c5STejun Heo unsigned long flags; 824afae461aSSergey Shtylyov unsigned int cnt; 8255ddf24c5STejun Heo 8265ddf24c5STejun Heo spin_lock_irqsave(ap->lock, flags); 8275ddf24c5STejun Heo 8285ddf24c5STejun Heo cnt = ata_eh_nr_in_flight(ap); 8295ddf24c5STejun Heo 8305ddf24c5STejun Heo /* are we done? */ 8315ddf24c5STejun Heo if (!cnt) 8325ddf24c5STejun Heo goto out_unlock; 8335ddf24c5STejun Heo 8345ddf24c5STejun Heo if (cnt == ap->fastdrain_cnt) { 835258c4e5cSJens Axboe struct ata_queued_cmd *qc; 8365ddf24c5STejun Heo unsigned int tag; 8375ddf24c5STejun Heo 8385ddf24c5STejun Heo /* No progress during the last interval, tag all 8395ddf24c5STejun Heo * in-flight qcs as timed out and freeze the port. 8405ddf24c5STejun Heo */ 841258c4e5cSJens Axboe ata_qc_for_each(ap, qc, tag) { 8425ddf24c5STejun Heo if (qc) 8435ddf24c5STejun Heo qc->err_mask |= AC_ERR_TIMEOUT; 8445ddf24c5STejun Heo } 8455ddf24c5STejun Heo 8465ddf24c5STejun Heo ata_port_freeze(ap); 8475ddf24c5STejun Heo } else { 8485ddf24c5STejun Heo /* some qcs have finished, give it another chance */ 8495ddf24c5STejun Heo ap->fastdrain_cnt = cnt; 8505ddf24c5STejun Heo ap->fastdrain_timer.expires = 851341c2c95STejun Heo ata_deadline(jiffies, ATA_EH_FASTDRAIN_INTERVAL); 8525ddf24c5STejun Heo add_timer(&ap->fastdrain_timer); 8535ddf24c5STejun Heo } 8545ddf24c5STejun Heo 8555ddf24c5STejun Heo out_unlock: 8565ddf24c5STejun Heo spin_unlock_irqrestore(ap->lock, flags); 8575ddf24c5STejun Heo } 8585ddf24c5STejun Heo 8595ddf24c5STejun Heo /** 8605ddf24c5STejun Heo * ata_eh_set_pending - set ATA_PFLAG_EH_PENDING and activate fast drain 8615ddf24c5STejun Heo * @ap: target ATA port 8625ddf24c5STejun Heo * @fastdrain: activate fast drain 8635ddf24c5STejun Heo * 8645ddf24c5STejun Heo * Set ATA_PFLAG_EH_PENDING and activate fast drain if @fastdrain 8655ddf24c5STejun Heo * is non-zero and EH wasn't pending before. Fast drain ensures 8665ddf24c5STejun Heo * that EH kicks in in timely manner. 8675ddf24c5STejun Heo * 8685ddf24c5STejun Heo * LOCKING: 8695ddf24c5STejun Heo * spin_lock_irqsave(host lock) 8705ddf24c5STejun Heo */ 8715ddf24c5STejun Heo static void ata_eh_set_pending(struct ata_port *ap, int fastdrain) 8725ddf24c5STejun Heo { 873afae461aSSergey Shtylyov unsigned int cnt; 8745ddf24c5STejun Heo 8755ddf24c5STejun Heo /* already scheduled? */ 8765ddf24c5STejun Heo if (ap->pflags & ATA_PFLAG_EH_PENDING) 8775ddf24c5STejun Heo return; 8785ddf24c5STejun Heo 8795ddf24c5STejun Heo ap->pflags |= ATA_PFLAG_EH_PENDING; 8805ddf24c5STejun Heo 8815ddf24c5STejun Heo if (!fastdrain) 8825ddf24c5STejun Heo return; 8835ddf24c5STejun Heo 8845ddf24c5STejun Heo /* do we have in-flight qcs? */ 8855ddf24c5STejun Heo cnt = ata_eh_nr_in_flight(ap); 8865ddf24c5STejun Heo if (!cnt) 8875ddf24c5STejun Heo return; 8885ddf24c5STejun Heo 8895ddf24c5STejun Heo /* activate fast drain */ 8905ddf24c5STejun Heo ap->fastdrain_cnt = cnt; 891341c2c95STejun Heo ap->fastdrain_timer.expires = 892341c2c95STejun Heo ata_deadline(jiffies, ATA_EH_FASTDRAIN_INTERVAL); 8935ddf24c5STejun Heo add_timer(&ap->fastdrain_timer); 8945ddf24c5STejun Heo } 8955ddf24c5STejun Heo 896c6fd2807SJeff Garzik /** 897c6fd2807SJeff Garzik * ata_qc_schedule_eh - schedule qc for error handling 898c6fd2807SJeff Garzik * @qc: command to schedule error handling for 899c6fd2807SJeff Garzik * 900c6fd2807SJeff Garzik * Schedule error handling for @qc. EH will kick in as soon as 901c6fd2807SJeff Garzik * other commands are drained. 902c6fd2807SJeff Garzik * 903c6fd2807SJeff Garzik * LOCKING: 904cca3974eSJeff Garzik * spin_lock_irqsave(host lock) 905c6fd2807SJeff Garzik */ 906c6fd2807SJeff Garzik void ata_qc_schedule_eh(struct ata_queued_cmd *qc) 907c6fd2807SJeff Garzik { 908c6fd2807SJeff Garzik struct ata_port *ap = qc->ap; 909c6fd2807SJeff Garzik 910c6fd2807SJeff Garzik WARN_ON(!ap->ops->error_handler); 911c6fd2807SJeff Garzik 912c6fd2807SJeff Garzik qc->flags |= ATA_QCFLAG_FAILED; 9135ddf24c5STejun Heo ata_eh_set_pending(ap, 1); 914c6fd2807SJeff Garzik 915c6fd2807SJeff Garzik /* The following will fail if timeout has already expired. 916c6fd2807SJeff Garzik * ata_scsi_error() takes care of such scmds on EH entry. 917c6fd2807SJeff Garzik * Note that ATA_QCFLAG_FAILED is unconditionally set after 918c6fd2807SJeff Garzik * this function completes. 919c6fd2807SJeff Garzik */ 920c8329cd5SBart Van Assche blk_abort_request(scsi_cmd_to_rq(qc->scsicmd)); 921c6fd2807SJeff Garzik } 922c6fd2807SJeff Garzik 923c6fd2807SJeff Garzik /** 924e4a9c373SDan Williams * ata_std_sched_eh - non-libsas ata_ports issue eh with this common routine 925e4a9c373SDan Williams * @ap: ATA port to schedule EH for 926e4a9c373SDan Williams * 927e4a9c373SDan Williams * LOCKING: inherited from ata_port_schedule_eh 928e4a9c373SDan Williams * spin_lock_irqsave(host lock) 929e4a9c373SDan Williams */ 930e4a9c373SDan Williams void ata_std_sched_eh(struct ata_port *ap) 931e4a9c373SDan Williams { 932e4a9c373SDan Williams WARN_ON(!ap->ops->error_handler); 933e4a9c373SDan Williams 934e4a9c373SDan Williams if (ap->pflags & ATA_PFLAG_INITIALIZING) 935e4a9c373SDan Williams return; 936e4a9c373SDan Williams 937e4a9c373SDan Williams ata_eh_set_pending(ap, 1); 938e4a9c373SDan Williams scsi_schedule_eh(ap->scsi_host); 939e4a9c373SDan Williams 940c318458cSHannes Reinecke trace_ata_std_sched_eh(ap); 941e4a9c373SDan Williams } 942e4a9c373SDan Williams EXPORT_SYMBOL_GPL(ata_std_sched_eh); 943e4a9c373SDan Williams 944e4a9c373SDan Williams /** 945e4a9c373SDan Williams * ata_std_end_eh - non-libsas ata_ports complete eh with this common routine 946e4a9c373SDan Williams * @ap: ATA port to end EH for 947e4a9c373SDan Williams * 948e4a9c373SDan Williams * In the libata object model there is a 1:1 mapping of ata_port to 949e4a9c373SDan Williams * shost, so host fields can be directly manipulated under ap->lock, in 950e4a9c373SDan Williams * the libsas case we need to hold a lock at the ha->level to coordinate 951e4a9c373SDan Williams * these events. 952e4a9c373SDan Williams * 953e4a9c373SDan Williams * LOCKING: 954e4a9c373SDan Williams * spin_lock_irqsave(host lock) 955e4a9c373SDan Williams */ 956e4a9c373SDan Williams void ata_std_end_eh(struct ata_port *ap) 957e4a9c373SDan Williams { 958e4a9c373SDan Williams struct Scsi_Host *host = ap->scsi_host; 959e4a9c373SDan Williams 960e4a9c373SDan Williams host->host_eh_scheduled = 0; 961e4a9c373SDan Williams } 962e4a9c373SDan Williams EXPORT_SYMBOL(ata_std_end_eh); 963e4a9c373SDan Williams 964e4a9c373SDan Williams 965e4a9c373SDan Williams /** 966c6fd2807SJeff Garzik * ata_port_schedule_eh - schedule error handling without a qc 967c6fd2807SJeff Garzik * @ap: ATA port to schedule EH for 968c6fd2807SJeff Garzik * 969c6fd2807SJeff Garzik * Schedule error handling for @ap. EH will kick in as soon as 970c6fd2807SJeff Garzik * all commands are drained. 971c6fd2807SJeff Garzik * 972c6fd2807SJeff Garzik * LOCKING: 973cca3974eSJeff Garzik * spin_lock_irqsave(host lock) 974c6fd2807SJeff Garzik */ 975c6fd2807SJeff Garzik void ata_port_schedule_eh(struct ata_port *ap) 976c6fd2807SJeff Garzik { 977e4a9c373SDan Williams /* see: ata_std_sched_eh, unless you know better */ 978e4a9c373SDan Williams ap->ops->sched_eh(ap); 979c6fd2807SJeff Garzik } 980a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_port_schedule_eh); 981c6fd2807SJeff Garzik 982dbd82616STejun Heo static int ata_do_link_abort(struct ata_port *ap, struct ata_link *link) 983c6fd2807SJeff Garzik { 984258c4e5cSJens Axboe struct ata_queued_cmd *qc; 985c6fd2807SJeff Garzik int tag, nr_aborted = 0; 986c6fd2807SJeff Garzik 987c6fd2807SJeff Garzik WARN_ON(!ap->ops->error_handler); 988c6fd2807SJeff Garzik 9895ddf24c5STejun Heo /* we're gonna abort all commands, no need for fast drain */ 9905ddf24c5STejun Heo ata_eh_set_pending(ap, 0); 9915ddf24c5STejun Heo 99228361c40SJens Axboe /* include internal tag in iteration */ 993258c4e5cSJens Axboe ata_qc_for_each_with_internal(ap, qc, tag) { 994dbd82616STejun Heo if (qc && (!link || qc->dev->link == link)) { 995c6fd2807SJeff Garzik qc->flags |= ATA_QCFLAG_FAILED; 996c6fd2807SJeff Garzik ata_qc_complete(qc); 997c6fd2807SJeff Garzik nr_aborted++; 998c6fd2807SJeff Garzik } 999c6fd2807SJeff Garzik } 1000c6fd2807SJeff Garzik 1001c6fd2807SJeff Garzik if (!nr_aborted) 1002c6fd2807SJeff Garzik ata_port_schedule_eh(ap); 1003c6fd2807SJeff Garzik 1004c6fd2807SJeff Garzik return nr_aborted; 1005c6fd2807SJeff Garzik } 1006c6fd2807SJeff Garzik 1007c6fd2807SJeff Garzik /** 1008dbd82616STejun Heo * ata_link_abort - abort all qc's on the link 1009dbd82616STejun Heo * @link: ATA link to abort qc's for 1010dbd82616STejun Heo * 1011dbd82616STejun Heo * Abort all active qc's active on @link and schedule EH. 1012dbd82616STejun Heo * 1013dbd82616STejun Heo * LOCKING: 1014dbd82616STejun Heo * spin_lock_irqsave(host lock) 1015dbd82616STejun Heo * 1016dbd82616STejun Heo * RETURNS: 1017dbd82616STejun Heo * Number of aborted qc's. 1018dbd82616STejun Heo */ 1019dbd82616STejun Heo int ata_link_abort(struct ata_link *link) 1020dbd82616STejun Heo { 1021dbd82616STejun Heo return ata_do_link_abort(link->ap, link); 1022dbd82616STejun Heo } 1023a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_link_abort); 1024dbd82616STejun Heo 1025dbd82616STejun Heo /** 1026dbd82616STejun Heo * ata_port_abort - abort all qc's on the port 1027dbd82616STejun Heo * @ap: ATA port to abort qc's for 1028dbd82616STejun Heo * 1029dbd82616STejun Heo * Abort all active qc's of @ap and schedule EH. 1030dbd82616STejun Heo * 1031dbd82616STejun Heo * LOCKING: 1032dbd82616STejun Heo * spin_lock_irqsave(host_set lock) 1033dbd82616STejun Heo * 1034dbd82616STejun Heo * RETURNS: 1035dbd82616STejun Heo * Number of aborted qc's. 1036dbd82616STejun Heo */ 1037dbd82616STejun Heo int ata_port_abort(struct ata_port *ap) 1038dbd82616STejun Heo { 1039dbd82616STejun Heo return ata_do_link_abort(ap, NULL); 1040dbd82616STejun Heo } 1041a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_port_abort); 1042dbd82616STejun Heo 1043dbd82616STejun Heo /** 1044c6fd2807SJeff Garzik * __ata_port_freeze - freeze port 1045c6fd2807SJeff Garzik * @ap: ATA port to freeze 1046c6fd2807SJeff Garzik * 1047c6fd2807SJeff Garzik * This function is called when HSM violation or some other 1048c6fd2807SJeff Garzik * condition disrupts normal operation of the port. Frozen port 1049c6fd2807SJeff Garzik * is not allowed to perform any operation until the port is 1050c6fd2807SJeff Garzik * thawed, which usually follows a successful reset. 1051c6fd2807SJeff Garzik * 1052c6fd2807SJeff Garzik * ap->ops->freeze() callback can be used for freezing the port 1053c6fd2807SJeff Garzik * hardware-wise (e.g. mask interrupt and stop DMA engine). If a 1054c6fd2807SJeff Garzik * port cannot be frozen hardware-wise, the interrupt handler 1055c6fd2807SJeff Garzik * must ack and clear interrupts unconditionally while the port 1056c6fd2807SJeff Garzik * is frozen. 1057c6fd2807SJeff Garzik * 1058c6fd2807SJeff Garzik * LOCKING: 1059cca3974eSJeff Garzik * spin_lock_irqsave(host lock) 1060c6fd2807SJeff Garzik */ 1061c6fd2807SJeff Garzik static void __ata_port_freeze(struct ata_port *ap) 1062c6fd2807SJeff Garzik { 1063c6fd2807SJeff Garzik WARN_ON(!ap->ops->error_handler); 1064c6fd2807SJeff Garzik 1065c6fd2807SJeff Garzik if (ap->ops->freeze) 1066c6fd2807SJeff Garzik ap->ops->freeze(ap); 1067c6fd2807SJeff Garzik 1068c6fd2807SJeff Garzik ap->pflags |= ATA_PFLAG_FROZEN; 1069c6fd2807SJeff Garzik 1070c318458cSHannes Reinecke trace_ata_port_freeze(ap); 1071c6fd2807SJeff Garzik } 1072c6fd2807SJeff Garzik 1073c6fd2807SJeff Garzik /** 1074c6fd2807SJeff Garzik * ata_port_freeze - abort & freeze port 1075c6fd2807SJeff Garzik * @ap: ATA port to freeze 1076c6fd2807SJeff Garzik * 107754c38444SJeff Garzik * Abort and freeze @ap. The freeze operation must be called 107854c38444SJeff Garzik * first, because some hardware requires special operations 107954c38444SJeff Garzik * before the taskfile registers are accessible. 1080c6fd2807SJeff Garzik * 1081c6fd2807SJeff Garzik * LOCKING: 1082cca3974eSJeff Garzik * spin_lock_irqsave(host lock) 1083c6fd2807SJeff Garzik * 1084c6fd2807SJeff Garzik * RETURNS: 1085c6fd2807SJeff Garzik * Number of aborted commands. 1086c6fd2807SJeff Garzik */ 1087c6fd2807SJeff Garzik int ata_port_freeze(struct ata_port *ap) 1088c6fd2807SJeff Garzik { 1089c6fd2807SJeff Garzik WARN_ON(!ap->ops->error_handler); 1090c6fd2807SJeff Garzik 1091c6fd2807SJeff Garzik __ata_port_freeze(ap); 1092c6fd2807SJeff Garzik 1093cb6e73aaSye xingchen return ata_port_abort(ap); 1094c6fd2807SJeff Garzik } 1095a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_port_freeze); 1096c6fd2807SJeff Garzik 1097c6fd2807SJeff Garzik /** 1098c6fd2807SJeff Garzik * ata_eh_freeze_port - EH helper to freeze port 1099c6fd2807SJeff Garzik * @ap: ATA port to freeze 1100c6fd2807SJeff Garzik * 1101c6fd2807SJeff Garzik * Freeze @ap. 1102c6fd2807SJeff Garzik * 1103c6fd2807SJeff Garzik * LOCKING: 1104c6fd2807SJeff Garzik * None. 1105c6fd2807SJeff Garzik */ 1106c6fd2807SJeff Garzik void ata_eh_freeze_port(struct ata_port *ap) 1107c6fd2807SJeff Garzik { 1108c6fd2807SJeff Garzik unsigned long flags; 1109c6fd2807SJeff Garzik 1110c6fd2807SJeff Garzik if (!ap->ops->error_handler) 1111c6fd2807SJeff Garzik return; 1112c6fd2807SJeff Garzik 1113c6fd2807SJeff Garzik spin_lock_irqsave(ap->lock, flags); 1114c6fd2807SJeff Garzik __ata_port_freeze(ap); 1115c6fd2807SJeff Garzik spin_unlock_irqrestore(ap->lock, flags); 1116c6fd2807SJeff Garzik } 1117a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_eh_freeze_port); 1118c6fd2807SJeff Garzik 1119c6fd2807SJeff Garzik /** 112094bd5719SMauro Carvalho Chehab * ata_eh_thaw_port - EH helper to thaw port 1121c6fd2807SJeff Garzik * @ap: ATA port to thaw 1122c6fd2807SJeff Garzik * 1123c6fd2807SJeff Garzik * Thaw frozen port @ap. 1124c6fd2807SJeff Garzik * 1125c6fd2807SJeff Garzik * LOCKING: 1126c6fd2807SJeff Garzik * None. 1127c6fd2807SJeff Garzik */ 1128c6fd2807SJeff Garzik void ata_eh_thaw_port(struct ata_port *ap) 1129c6fd2807SJeff Garzik { 1130c6fd2807SJeff Garzik unsigned long flags; 1131c6fd2807SJeff Garzik 1132c6fd2807SJeff Garzik if (!ap->ops->error_handler) 1133c6fd2807SJeff Garzik return; 1134c6fd2807SJeff Garzik 1135c6fd2807SJeff Garzik spin_lock_irqsave(ap->lock, flags); 1136c6fd2807SJeff Garzik 1137c6fd2807SJeff Garzik ap->pflags &= ~ATA_PFLAG_FROZEN; 1138c6fd2807SJeff Garzik 1139c6fd2807SJeff Garzik if (ap->ops->thaw) 1140c6fd2807SJeff Garzik ap->ops->thaw(ap); 1141c6fd2807SJeff Garzik 1142c6fd2807SJeff Garzik spin_unlock_irqrestore(ap->lock, flags); 1143c6fd2807SJeff Garzik 1144c318458cSHannes Reinecke trace_ata_port_thaw(ap); 1145c6fd2807SJeff Garzik } 1146c6fd2807SJeff Garzik 1147c6fd2807SJeff Garzik static void ata_eh_scsidone(struct scsi_cmnd *scmd) 1148c6fd2807SJeff Garzik { 1149c6fd2807SJeff Garzik /* nada */ 1150c6fd2807SJeff Garzik } 1151c6fd2807SJeff Garzik 1152c6fd2807SJeff Garzik static void __ata_eh_qc_complete(struct ata_queued_cmd *qc) 1153c6fd2807SJeff Garzik { 1154c6fd2807SJeff Garzik struct ata_port *ap = qc->ap; 1155c6fd2807SJeff Garzik struct scsi_cmnd *scmd = qc->scsicmd; 1156c6fd2807SJeff Garzik unsigned long flags; 1157c6fd2807SJeff Garzik 1158c6fd2807SJeff Garzik spin_lock_irqsave(ap->lock, flags); 1159c6fd2807SJeff Garzik qc->scsidone = ata_eh_scsidone; 1160c6fd2807SJeff Garzik __ata_qc_complete(qc); 1161c6fd2807SJeff Garzik WARN_ON(ata_tag_valid(qc->tag)); 1162c6fd2807SJeff Garzik spin_unlock_irqrestore(ap->lock, flags); 1163c6fd2807SJeff Garzik 1164c6fd2807SJeff Garzik scsi_eh_finish_cmd(scmd, &ap->eh_done_q); 1165c6fd2807SJeff Garzik } 1166c6fd2807SJeff Garzik 1167c6fd2807SJeff Garzik /** 1168c6fd2807SJeff Garzik * ata_eh_qc_complete - Complete an active ATA command from EH 1169c6fd2807SJeff Garzik * @qc: Command to complete 1170c6fd2807SJeff Garzik * 1171c6fd2807SJeff Garzik * Indicate to the mid and upper layers that an ATA command has 1172c6fd2807SJeff Garzik * completed. To be used from EH. 1173c6fd2807SJeff Garzik */ 1174c6fd2807SJeff Garzik void ata_eh_qc_complete(struct ata_queued_cmd *qc) 1175c6fd2807SJeff Garzik { 1176c6fd2807SJeff Garzik struct scsi_cmnd *scmd = qc->scsicmd; 1177c6fd2807SJeff Garzik scmd->retries = scmd->allowed; 1178c6fd2807SJeff Garzik __ata_eh_qc_complete(qc); 1179c6fd2807SJeff Garzik } 1180c6fd2807SJeff Garzik 1181c6fd2807SJeff Garzik /** 1182c6fd2807SJeff Garzik * ata_eh_qc_retry - Tell midlayer to retry an ATA command after EH 1183c6fd2807SJeff Garzik * @qc: Command to retry 1184c6fd2807SJeff Garzik * 1185c6fd2807SJeff Garzik * Indicate to the mid and upper layers that an ATA command 1186c6fd2807SJeff Garzik * should be retried. To be used from EH. 1187c6fd2807SJeff Garzik * 1188c6fd2807SJeff Garzik * SCSI midlayer limits the number of retries to scmd->allowed. 1189f13e2201SGwendal Grignou * scmd->allowed is incremented for commands which get retried 1190c6fd2807SJeff Garzik * due to unrelated failures (qc->err_mask is zero). 1191c6fd2807SJeff Garzik */ 1192c6fd2807SJeff Garzik void ata_eh_qc_retry(struct ata_queued_cmd *qc) 1193c6fd2807SJeff Garzik { 1194c6fd2807SJeff Garzik struct scsi_cmnd *scmd = qc->scsicmd; 1195f13e2201SGwendal Grignou if (!qc->err_mask) 1196f13e2201SGwendal Grignou scmd->allowed++; 1197c6fd2807SJeff Garzik __ata_eh_qc_complete(qc); 1198c6fd2807SJeff Garzik } 1199c6fd2807SJeff Garzik 1200c6fd2807SJeff Garzik /** 1201678afac6STejun Heo * ata_dev_disable - disable ATA device 1202678afac6STejun Heo * @dev: ATA device to disable 1203678afac6STejun Heo * 1204678afac6STejun Heo * Disable @dev. 1205678afac6STejun Heo * 1206678afac6STejun Heo * Locking: 1207678afac6STejun Heo * EH context. 1208678afac6STejun Heo */ 1209678afac6STejun Heo void ata_dev_disable(struct ata_device *dev) 1210678afac6STejun Heo { 1211678afac6STejun Heo if (!ata_dev_enabled(dev)) 1212678afac6STejun Heo return; 1213678afac6STejun Heo 12141c95a27cSHannes Reinecke ata_dev_warn(dev, "disable device\n"); 1215678afac6STejun Heo ata_acpi_on_disable(dev); 1216678afac6STejun Heo ata_down_xfermask_limit(dev, ATA_DNXFER_FORCE_PIO0 | ATA_DNXFER_QUIET); 1217678afac6STejun Heo dev->class++; 121899cf610aSTejun Heo 121999cf610aSTejun Heo /* From now till the next successful probe, ering is used to 122099cf610aSTejun Heo * track probe failures. Clear accumulated device error info. 122199cf610aSTejun Heo */ 122299cf610aSTejun Heo ata_ering_clear(&dev->ering); 1223678afac6STejun Heo } 1224a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_dev_disable); 1225678afac6STejun Heo 1226678afac6STejun Heo /** 1227c6fd2807SJeff Garzik * ata_eh_detach_dev - detach ATA device 1228c6fd2807SJeff Garzik * @dev: ATA device to detach 1229c6fd2807SJeff Garzik * 1230c6fd2807SJeff Garzik * Detach @dev. 1231c6fd2807SJeff Garzik * 1232c6fd2807SJeff Garzik * LOCKING: 1233c6fd2807SJeff Garzik * None. 1234c6fd2807SJeff Garzik */ 1235fb7fd614STejun Heo void ata_eh_detach_dev(struct ata_device *dev) 1236c6fd2807SJeff Garzik { 1237f58229f8STejun Heo struct ata_link *link = dev->link; 1238f58229f8STejun Heo struct ata_port *ap = link->ap; 123990484ebfSTejun Heo struct ata_eh_context *ehc = &link->eh_context; 1240c6fd2807SJeff Garzik unsigned long flags; 1241c6fd2807SJeff Garzik 1242c6fd2807SJeff Garzik ata_dev_disable(dev); 1243c6fd2807SJeff Garzik 1244c6fd2807SJeff Garzik spin_lock_irqsave(ap->lock, flags); 1245c6fd2807SJeff Garzik 1246c6fd2807SJeff Garzik dev->flags &= ~ATA_DFLAG_DETACH; 1247c6fd2807SJeff Garzik 1248c6fd2807SJeff Garzik if (ata_scsi_offline_dev(dev)) { 1249c6fd2807SJeff Garzik dev->flags |= ATA_DFLAG_DETACHED; 1250c6fd2807SJeff Garzik ap->pflags |= ATA_PFLAG_SCSI_HOTPLUG; 1251c6fd2807SJeff Garzik } 1252c6fd2807SJeff Garzik 125390484ebfSTejun Heo /* clear per-dev EH info */ 1254f58229f8STejun Heo ata_eh_clear_action(link, dev, &link->eh_info, ATA_EH_PERDEV_MASK); 1255f58229f8STejun Heo ata_eh_clear_action(link, dev, &link->eh_context.i, ATA_EH_PERDEV_MASK); 125690484ebfSTejun Heo ehc->saved_xfer_mode[dev->devno] = 0; 125790484ebfSTejun Heo ehc->saved_ncq_enabled &= ~(1 << dev->devno); 1258c6fd2807SJeff Garzik 1259c6fd2807SJeff Garzik spin_unlock_irqrestore(ap->lock, flags); 1260c6fd2807SJeff Garzik } 1261c6fd2807SJeff Garzik 1262c6fd2807SJeff Garzik /** 1263c6fd2807SJeff Garzik * ata_eh_about_to_do - about to perform eh_action 1264955e57dfSTejun Heo * @link: target ATA link 1265c6fd2807SJeff Garzik * @dev: target ATA dev for per-dev action (can be NULL) 1266c6fd2807SJeff Garzik * @action: action about to be performed 1267c6fd2807SJeff Garzik * 1268c6fd2807SJeff Garzik * Called just before performing EH actions to clear related bits 1269955e57dfSTejun Heo * in @link->eh_info such that eh actions are not unnecessarily 1270955e57dfSTejun Heo * repeated. 1271c6fd2807SJeff Garzik * 1272c6fd2807SJeff Garzik * LOCKING: 1273c6fd2807SJeff Garzik * None. 1274c6fd2807SJeff Garzik */ 1275fb7fd614STejun Heo void ata_eh_about_to_do(struct ata_link *link, struct ata_device *dev, 1276c6fd2807SJeff Garzik unsigned int action) 1277c6fd2807SJeff Garzik { 1278955e57dfSTejun Heo struct ata_port *ap = link->ap; 1279955e57dfSTejun Heo struct ata_eh_info *ehi = &link->eh_info; 1280955e57dfSTejun Heo struct ata_eh_context *ehc = &link->eh_context; 1281c6fd2807SJeff Garzik unsigned long flags; 1282c6fd2807SJeff Garzik 1283c318458cSHannes Reinecke trace_ata_eh_about_to_do(link, dev ? dev->devno : 0, action); 1284c318458cSHannes Reinecke 1285c6fd2807SJeff Garzik spin_lock_irqsave(ap->lock, flags); 1286c6fd2807SJeff Garzik 1287955e57dfSTejun Heo ata_eh_clear_action(link, dev, ehi, action); 1288c6fd2807SJeff Garzik 1289a568d1d2STejun Heo /* About to take EH action, set RECOVERED. Ignore actions on 1290a568d1d2STejun Heo * slave links as master will do them again. 1291a568d1d2STejun Heo */ 1292a568d1d2STejun Heo if (!(ehc->i.flags & ATA_EHI_QUIET) && link != ap->slave_link) 1293c6fd2807SJeff Garzik ap->pflags |= ATA_PFLAG_RECOVERED; 1294c6fd2807SJeff Garzik 1295c6fd2807SJeff Garzik spin_unlock_irqrestore(ap->lock, flags); 1296c6fd2807SJeff Garzik } 1297c6fd2807SJeff Garzik 1298c6fd2807SJeff Garzik /** 1299c6fd2807SJeff Garzik * ata_eh_done - EH action complete 13002f60e1abSJonathan Corbet * @link: ATA link for which EH actions are complete 1301c6fd2807SJeff Garzik * @dev: target ATA dev for per-dev action (can be NULL) 1302c6fd2807SJeff Garzik * @action: action just completed 1303c6fd2807SJeff Garzik * 1304c6fd2807SJeff Garzik * Called right after performing EH actions to clear related bits 1305955e57dfSTejun Heo * in @link->eh_context. 1306c6fd2807SJeff Garzik * 1307c6fd2807SJeff Garzik * LOCKING: 1308c6fd2807SJeff Garzik * None. 1309c6fd2807SJeff Garzik */ 1310fb7fd614STejun Heo void ata_eh_done(struct ata_link *link, struct ata_device *dev, 1311c6fd2807SJeff Garzik unsigned int action) 1312c6fd2807SJeff Garzik { 1313955e57dfSTejun Heo struct ata_eh_context *ehc = &link->eh_context; 13149af5c9c9STejun Heo 1315c318458cSHannes Reinecke trace_ata_eh_done(link, dev ? dev->devno : 0, action); 1316c318458cSHannes Reinecke 1317955e57dfSTejun Heo ata_eh_clear_action(link, dev, &ehc->i, action); 1318c6fd2807SJeff Garzik } 1319c6fd2807SJeff Garzik 1320c6fd2807SJeff Garzik /** 1321c6fd2807SJeff Garzik * ata_err_string - convert err_mask to descriptive string 1322c6fd2807SJeff Garzik * @err_mask: error mask to convert to string 1323c6fd2807SJeff Garzik * 1324c6fd2807SJeff Garzik * Convert @err_mask to descriptive string. Errors are 1325c6fd2807SJeff Garzik * prioritized according to severity and only the most severe 1326c6fd2807SJeff Garzik * error is reported. 1327c6fd2807SJeff Garzik * 1328c6fd2807SJeff Garzik * LOCKING: 1329c6fd2807SJeff Garzik * None. 1330c6fd2807SJeff Garzik * 1331c6fd2807SJeff Garzik * RETURNS: 1332c6fd2807SJeff Garzik * Descriptive string for @err_mask 1333c6fd2807SJeff Garzik */ 1334c6fd2807SJeff Garzik static const char *ata_err_string(unsigned int err_mask) 1335c6fd2807SJeff Garzik { 1336c6fd2807SJeff Garzik if (err_mask & AC_ERR_HOST_BUS) 1337c6fd2807SJeff Garzik return "host bus error"; 1338c6fd2807SJeff Garzik if (err_mask & AC_ERR_ATA_BUS) 1339c6fd2807SJeff Garzik return "ATA bus error"; 1340c6fd2807SJeff Garzik if (err_mask & AC_ERR_TIMEOUT) 1341c6fd2807SJeff Garzik return "timeout"; 1342c6fd2807SJeff Garzik if (err_mask & AC_ERR_HSM) 1343c6fd2807SJeff Garzik return "HSM violation"; 1344c6fd2807SJeff Garzik if (err_mask & AC_ERR_SYSTEM) 1345c6fd2807SJeff Garzik return "internal error"; 1346c6fd2807SJeff Garzik if (err_mask & AC_ERR_MEDIA) 1347c6fd2807SJeff Garzik return "media error"; 1348c6fd2807SJeff Garzik if (err_mask & AC_ERR_INVALID) 1349c6fd2807SJeff Garzik return "invalid argument"; 1350c6fd2807SJeff Garzik if (err_mask & AC_ERR_DEV) 1351c6fd2807SJeff Garzik return "device error"; 135254fb131bSDamien Le Moal if (err_mask & AC_ERR_NCQ) 135354fb131bSDamien Le Moal return "NCQ error"; 135454fb131bSDamien Le Moal if (err_mask & AC_ERR_NODEV_HINT) 135554fb131bSDamien Le Moal return "Polling detection error"; 1356c6fd2807SJeff Garzik return "unknown error"; 1357c6fd2807SJeff Garzik } 1358c6fd2807SJeff Garzik 1359c6fd2807SJeff Garzik /** 136011fc33daSTejun Heo * atapi_eh_tur - perform ATAPI TEST_UNIT_READY 136111fc33daSTejun Heo * @dev: target ATAPI device 136211fc33daSTejun Heo * @r_sense_key: out parameter for sense_key 136311fc33daSTejun Heo * 136411fc33daSTejun Heo * Perform ATAPI TEST_UNIT_READY. 136511fc33daSTejun Heo * 136611fc33daSTejun Heo * LOCKING: 136711fc33daSTejun Heo * EH context (may sleep). 136811fc33daSTejun Heo * 136911fc33daSTejun Heo * RETURNS: 137011fc33daSTejun Heo * 0 on success, AC_ERR_* mask on failure. 137111fc33daSTejun Heo */ 13723dc67440SAaron Lu unsigned int atapi_eh_tur(struct ata_device *dev, u8 *r_sense_key) 137311fc33daSTejun Heo { 137411fc33daSTejun Heo u8 cdb[ATAPI_CDB_LEN] = { TEST_UNIT_READY, 0, 0, 0, 0, 0 }; 137511fc33daSTejun Heo struct ata_taskfile tf; 137611fc33daSTejun Heo unsigned int err_mask; 137711fc33daSTejun Heo 137811fc33daSTejun Heo ata_tf_init(dev, &tf); 137911fc33daSTejun Heo 138011fc33daSTejun Heo tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE; 138111fc33daSTejun Heo tf.command = ATA_CMD_PACKET; 138211fc33daSTejun Heo tf.protocol = ATAPI_PROT_NODATA; 138311fc33daSTejun Heo 138411fc33daSTejun Heo err_mask = ata_exec_internal(dev, &tf, cdb, DMA_NONE, NULL, 0, 0); 138511fc33daSTejun Heo if (err_mask == AC_ERR_DEV) 1386efcef265SSergey Shtylyov *r_sense_key = tf.error >> 4; 138711fc33daSTejun Heo return err_mask; 138811fc33daSTejun Heo } 138911fc33daSTejun Heo 139011fc33daSTejun Heo /** 1391e87fd28cSHannes Reinecke * ata_eh_request_sense - perform REQUEST_SENSE_DATA_EXT 13922f60e1abSJonathan Corbet * @qc: qc to perform REQUEST_SENSE_SENSE_DATA_EXT to 1393e87fd28cSHannes Reinecke * 1394e87fd28cSHannes Reinecke * Perform REQUEST_SENSE_DATA_EXT after the device reported CHECK 1395e87fd28cSHannes Reinecke * SENSE. This function is an EH helper. 1396e87fd28cSHannes Reinecke * 1397e87fd28cSHannes Reinecke * LOCKING: 1398e87fd28cSHannes Reinecke * Kernel thread context (may sleep). 1399e87fd28cSHannes Reinecke */ 1400b46c760eSNiklas Cassel static void ata_eh_request_sense(struct ata_queued_cmd *qc) 1401e87fd28cSHannes Reinecke { 1402b46c760eSNiklas Cassel struct scsi_cmnd *cmd = qc->scsicmd; 1403e87fd28cSHannes Reinecke struct ata_device *dev = qc->dev; 1404e87fd28cSHannes Reinecke struct ata_taskfile tf; 1405e87fd28cSHannes Reinecke unsigned int err_mask; 1406e87fd28cSHannes Reinecke 1407e87fd28cSHannes Reinecke if (qc->ap->pflags & ATA_PFLAG_FROZEN) { 1408e87fd28cSHannes Reinecke ata_dev_warn(dev, "sense data available but port frozen\n"); 1409e87fd28cSHannes Reinecke return; 1410e87fd28cSHannes Reinecke } 1411e87fd28cSHannes Reinecke 1412d238ffd5SHannes Reinecke if (!cmd || qc->flags & ATA_QCFLAG_SENSE_VALID) 1413e87fd28cSHannes Reinecke return; 1414e87fd28cSHannes Reinecke 1415e87fd28cSHannes Reinecke if (!ata_id_sense_reporting_enabled(dev->id)) { 1416e87fd28cSHannes Reinecke ata_dev_warn(qc->dev, "sense data reporting disabled\n"); 1417e87fd28cSHannes Reinecke return; 1418e87fd28cSHannes Reinecke } 1419e87fd28cSHannes Reinecke 1420e87fd28cSHannes Reinecke ata_tf_init(dev, &tf); 1421e87fd28cSHannes Reinecke tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE; 1422e87fd28cSHannes Reinecke tf.flags |= ATA_TFLAG_LBA | ATA_TFLAG_LBA48; 1423e87fd28cSHannes Reinecke tf.command = ATA_CMD_REQ_SENSE_DATA; 1424e87fd28cSHannes Reinecke tf.protocol = ATA_PROT_NODATA; 1425e87fd28cSHannes Reinecke 1426e87fd28cSHannes Reinecke err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0); 1427e87fd28cSHannes Reinecke /* Ignore err_mask; ATA_ERR might be set */ 1428efcef265SSergey Shtylyov if (tf.status & ATA_SENSE) { 142906dbde5fSHannes Reinecke ata_scsi_set_sense(dev, cmd, tf.lbah, tf.lbam, tf.lbal); 1430e87fd28cSHannes Reinecke qc->flags |= ATA_QCFLAG_SENSE_VALID; 1431e87fd28cSHannes Reinecke } else { 1432e87fd28cSHannes Reinecke ata_dev_warn(dev, "request sense failed stat %02x emask %x\n", 1433efcef265SSergey Shtylyov tf.status, err_mask); 1434e87fd28cSHannes Reinecke } 1435e87fd28cSHannes Reinecke } 1436e87fd28cSHannes Reinecke 1437e87fd28cSHannes Reinecke /** 1438c6fd2807SJeff Garzik * atapi_eh_request_sense - perform ATAPI REQUEST_SENSE 1439c6fd2807SJeff Garzik * @dev: device to perform REQUEST_SENSE to 1440c6fd2807SJeff Garzik * @sense_buf: result sense data buffer (SCSI_SENSE_BUFFERSIZE bytes long) 14413eabddb8STejun Heo * @dfl_sense_key: default sense key to use 1442c6fd2807SJeff Garzik * 1443c6fd2807SJeff Garzik * Perform ATAPI REQUEST_SENSE after the device reported CHECK 1444c6fd2807SJeff Garzik * SENSE. This function is EH helper. 1445c6fd2807SJeff Garzik * 1446c6fd2807SJeff Garzik * LOCKING: 1447c6fd2807SJeff Garzik * Kernel thread context (may sleep). 1448c6fd2807SJeff Garzik * 1449c6fd2807SJeff Garzik * RETURNS: 1450c6fd2807SJeff Garzik * 0 on success, AC_ERR_* mask on failure 1451c6fd2807SJeff Garzik */ 14523dc67440SAaron Lu unsigned int atapi_eh_request_sense(struct ata_device *dev, 14533eabddb8STejun Heo u8 *sense_buf, u8 dfl_sense_key) 1454c6fd2807SJeff Garzik { 14553eabddb8STejun Heo u8 cdb[ATAPI_CDB_LEN] = 14563eabddb8STejun Heo { REQUEST_SENSE, 0, 0, 0, SCSI_SENSE_BUFFERSIZE, 0 }; 14579af5c9c9STejun Heo struct ata_port *ap = dev->link->ap; 1458c6fd2807SJeff Garzik struct ata_taskfile tf; 1459c6fd2807SJeff Garzik 1460c6fd2807SJeff Garzik memset(sense_buf, 0, SCSI_SENSE_BUFFERSIZE); 1461c6fd2807SJeff Garzik 146256287768SAlbert Lee /* initialize sense_buf with the error register, 146356287768SAlbert Lee * for the case where they are -not- overwritten 146456287768SAlbert Lee */ 1465c6fd2807SJeff Garzik sense_buf[0] = 0x70; 14663eabddb8STejun Heo sense_buf[2] = dfl_sense_key; 146756287768SAlbert Lee 146856287768SAlbert Lee /* some devices time out if garbage left in tf */ 146956287768SAlbert Lee ata_tf_init(dev, &tf); 1470c6fd2807SJeff Garzik 1471c6fd2807SJeff Garzik tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE; 1472c6fd2807SJeff Garzik tf.command = ATA_CMD_PACKET; 1473c6fd2807SJeff Garzik 1474c6fd2807SJeff Garzik /* is it pointless to prefer PIO for "safety reasons"? */ 1475c6fd2807SJeff Garzik if (ap->flags & ATA_FLAG_PIO_DMA) { 14760dc36888STejun Heo tf.protocol = ATAPI_PROT_DMA; 1477c6fd2807SJeff Garzik tf.feature |= ATAPI_PKT_DMA; 1478c6fd2807SJeff Garzik } else { 14790dc36888STejun Heo tf.protocol = ATAPI_PROT_PIO; 1480f2dfc1a1STejun Heo tf.lbam = SCSI_SENSE_BUFFERSIZE; 1481f2dfc1a1STejun Heo tf.lbah = 0; 1482c6fd2807SJeff Garzik } 1483c6fd2807SJeff Garzik 1484c6fd2807SJeff Garzik return ata_exec_internal(dev, &tf, cdb, DMA_FROM_DEVICE, 14852b789108STejun Heo sense_buf, SCSI_SENSE_BUFFERSIZE, 0); 1486c6fd2807SJeff Garzik } 1487c6fd2807SJeff Garzik 1488c6fd2807SJeff Garzik /** 1489c6fd2807SJeff Garzik * ata_eh_analyze_serror - analyze SError for a failed port 14900260731fSTejun Heo * @link: ATA link to analyze SError for 1491c6fd2807SJeff Garzik * 1492c6fd2807SJeff Garzik * Analyze SError if available and further determine cause of 1493c6fd2807SJeff Garzik * failure. 1494c6fd2807SJeff Garzik * 1495c6fd2807SJeff Garzik * LOCKING: 1496c6fd2807SJeff Garzik * None. 1497c6fd2807SJeff Garzik */ 14980260731fSTejun Heo static void ata_eh_analyze_serror(struct ata_link *link) 1499c6fd2807SJeff Garzik { 15000260731fSTejun Heo struct ata_eh_context *ehc = &link->eh_context; 1501c6fd2807SJeff Garzik u32 serror = ehc->i.serror; 1502c6fd2807SJeff Garzik unsigned int err_mask = 0, action = 0; 1503f9df58cbSTejun Heo u32 hotplug_mask; 1504c6fd2807SJeff Garzik 1505e0614db2STejun Heo if (serror & (SERR_PERSISTENT | SERR_DATA)) { 1506c6fd2807SJeff Garzik err_mask |= AC_ERR_ATA_BUS; 1507cf480626STejun Heo action |= ATA_EH_RESET; 1508c6fd2807SJeff Garzik } 1509c6fd2807SJeff Garzik if (serror & SERR_PROTOCOL) { 1510c6fd2807SJeff Garzik err_mask |= AC_ERR_HSM; 1511cf480626STejun Heo action |= ATA_EH_RESET; 1512c6fd2807SJeff Garzik } 1513c6fd2807SJeff Garzik if (serror & SERR_INTERNAL) { 1514c6fd2807SJeff Garzik err_mask |= AC_ERR_SYSTEM; 1515cf480626STejun Heo action |= ATA_EH_RESET; 1516c6fd2807SJeff Garzik } 1517f9df58cbSTejun Heo 1518f9df58cbSTejun Heo /* Determine whether a hotplug event has occurred. Both 1519f9df58cbSTejun Heo * SError.N/X are considered hotplug events for enabled or 1520f9df58cbSTejun Heo * host links. For disabled PMP links, only N bit is 1521f9df58cbSTejun Heo * considered as X bit is left at 1 for link plugging. 1522f9df58cbSTejun Heo */ 1523eb0e85e3STejun Heo if (link->lpm_policy > ATA_LPM_MAX_POWER) 15246b7ae954STejun Heo hotplug_mask = 0; /* hotplug doesn't work w/ LPM */ 15256b7ae954STejun Heo else if (!(link->flags & ATA_LFLAG_DISABLED) || ata_is_host_link(link)) 1526f9df58cbSTejun Heo hotplug_mask = SERR_PHYRDY_CHG | SERR_DEV_XCHG; 1527f9df58cbSTejun Heo else 1528f9df58cbSTejun Heo hotplug_mask = SERR_PHYRDY_CHG; 1529f9df58cbSTejun Heo 1530f9df58cbSTejun Heo if (serror & hotplug_mask) 1531c6fd2807SJeff Garzik ata_ehi_hotplugged(&ehc->i); 1532c6fd2807SJeff Garzik 1533c6fd2807SJeff Garzik ehc->i.err_mask |= err_mask; 1534c6fd2807SJeff Garzik ehc->i.action |= action; 1535c6fd2807SJeff Garzik } 1536c6fd2807SJeff Garzik 1537c6fd2807SJeff Garzik /** 1538c6fd2807SJeff Garzik * ata_eh_analyze_tf - analyze taskfile of a failed qc 1539c6fd2807SJeff Garzik * @qc: qc to analyze 1540c6fd2807SJeff Garzik * 1541c6fd2807SJeff Garzik * Analyze taskfile of @qc and further determine cause of 1542c6fd2807SJeff Garzik * failure. This function also requests ATAPI sense data if 154325985edcSLucas De Marchi * available. 1544c6fd2807SJeff Garzik * 1545c6fd2807SJeff Garzik * LOCKING: 1546c6fd2807SJeff Garzik * Kernel thread context (may sleep). 1547c6fd2807SJeff Garzik * 1548c6fd2807SJeff Garzik * RETURNS: 1549c6fd2807SJeff Garzik * Determined recovery action 1550c6fd2807SJeff Garzik */ 1551*e3b1fff6SNiklas Cassel static unsigned int ata_eh_analyze_tf(struct ata_queued_cmd *qc) 1552c6fd2807SJeff Garzik { 1553*e3b1fff6SNiklas Cassel const struct ata_taskfile *tf = &qc->result_tf; 1554c6fd2807SJeff Garzik unsigned int tmp, action = 0; 1555efcef265SSergey Shtylyov u8 stat = tf->status, err = tf->error; 1556c6fd2807SJeff Garzik 1557c6fd2807SJeff Garzik if ((stat & (ATA_BUSY | ATA_DRQ | ATA_DRDY)) != ATA_DRDY) { 1558c6fd2807SJeff Garzik qc->err_mask |= AC_ERR_HSM; 1559cf480626STejun Heo return ATA_EH_RESET; 1560c6fd2807SJeff Garzik } 1561c6fd2807SJeff Garzik 1562e87fd28cSHannes Reinecke if (stat & (ATA_ERR | ATA_DF)) { 1563a51d644aSTejun Heo qc->err_mask |= AC_ERR_DEV; 1564e87fd28cSHannes Reinecke /* 1565e87fd28cSHannes Reinecke * Sense data reporting does not work if the 1566e87fd28cSHannes Reinecke * device fault bit is set. 1567e87fd28cSHannes Reinecke */ 1568e87fd28cSHannes Reinecke if (stat & ATA_DF) 1569e87fd28cSHannes Reinecke stat &= ~ATA_SENSE; 1570e87fd28cSHannes Reinecke } else { 1571c6fd2807SJeff Garzik return 0; 1572e87fd28cSHannes Reinecke } 1573c6fd2807SJeff Garzik 1574c6fd2807SJeff Garzik switch (qc->dev->class) { 15759162c657SHannes Reinecke case ATA_DEV_ZAC: 1576e87fd28cSHannes Reinecke if (stat & ATA_SENSE) 1577b46c760eSNiklas Cassel ata_eh_request_sense(qc); 1578df561f66SGustavo A. R. Silva fallthrough; 1579ca156e00STejun Heo case ATA_DEV_ATA: 1580c6fd2807SJeff Garzik if (err & ATA_ICRC) 1581c6fd2807SJeff Garzik qc->err_mask |= AC_ERR_ATA_BUS; 1582eec7e1c1SAlexey Asemov if (err & (ATA_UNC | ATA_AMNF)) 1583c6fd2807SJeff Garzik qc->err_mask |= AC_ERR_MEDIA; 1584c6fd2807SJeff Garzik if (err & ATA_IDNF) 1585c6fd2807SJeff Garzik qc->err_mask |= AC_ERR_INVALID; 1586c6fd2807SJeff Garzik break; 1587c6fd2807SJeff Garzik 1588c6fd2807SJeff Garzik case ATA_DEV_ATAPI: 1589a569a30dSTejun Heo if (!(qc->ap->pflags & ATA_PFLAG_FROZEN)) { 15903eabddb8STejun Heo tmp = atapi_eh_request_sense(qc->dev, 15913eabddb8STejun Heo qc->scsicmd->sense_buffer, 1592efcef265SSergey Shtylyov qc->result_tf.error >> 4); 15933852e373SHannes Reinecke if (!tmp) 1594c6fd2807SJeff Garzik qc->flags |= ATA_QCFLAG_SENSE_VALID; 15953852e373SHannes Reinecke else 1596c6fd2807SJeff Garzik qc->err_mask |= tmp; 1597c6fd2807SJeff Garzik } 1598a569a30dSTejun Heo } 1599c6fd2807SJeff Garzik 16003852e373SHannes Reinecke if (qc->flags & ATA_QCFLAG_SENSE_VALID) { 1601b8e162f9SBart Van Assche enum scsi_disposition ret = scsi_check_sense(qc->scsicmd); 16023852e373SHannes Reinecke /* 160379487259SDamien Le Moal * SUCCESS here means that the sense code could be 16043852e373SHannes Reinecke * evaluated and should be passed to the upper layers 16053852e373SHannes Reinecke * for correct evaluation. 160679487259SDamien Le Moal * FAILED means the sense code could not be interpreted 16073852e373SHannes Reinecke * and the device would need to be reset. 16083852e373SHannes Reinecke * NEEDS_RETRY and ADD_TO_MLQUEUE means that the 16093852e373SHannes Reinecke * command would need to be retried. 16103852e373SHannes Reinecke */ 16113852e373SHannes Reinecke if (ret == NEEDS_RETRY || ret == ADD_TO_MLQUEUE) { 16123852e373SHannes Reinecke qc->flags |= ATA_QCFLAG_RETRY; 16133852e373SHannes Reinecke qc->err_mask |= AC_ERR_OTHER; 16143852e373SHannes Reinecke } else if (ret != SUCCESS) { 16153852e373SHannes Reinecke qc->err_mask |= AC_ERR_HSM; 16163852e373SHannes Reinecke } 16173852e373SHannes Reinecke } 1618c6fd2807SJeff Garzik if (qc->err_mask & (AC_ERR_HSM | AC_ERR_TIMEOUT | AC_ERR_ATA_BUS)) 1619cf480626STejun Heo action |= ATA_EH_RESET; 1620c6fd2807SJeff Garzik 1621c6fd2807SJeff Garzik return action; 1622c6fd2807SJeff Garzik } 1623c6fd2807SJeff Garzik 162476326ac1STejun Heo static int ata_eh_categorize_error(unsigned int eflags, unsigned int err_mask, 162576326ac1STejun Heo int *xfer_ok) 1626c6fd2807SJeff Garzik { 162776326ac1STejun Heo int base = 0; 162876326ac1STejun Heo 162976326ac1STejun Heo if (!(eflags & ATA_EFLAG_DUBIOUS_XFER)) 163076326ac1STejun Heo *xfer_ok = 1; 163176326ac1STejun Heo 163276326ac1STejun Heo if (!*xfer_ok) 163375f9cafcSTejun Heo base = ATA_ECAT_DUBIOUS_NONE; 163476326ac1STejun Heo 16357d47e8d4STejun Heo if (err_mask & AC_ERR_ATA_BUS) 163676326ac1STejun Heo return base + ATA_ECAT_ATA_BUS; 1637c6fd2807SJeff Garzik 16387d47e8d4STejun Heo if (err_mask & AC_ERR_TIMEOUT) 163976326ac1STejun Heo return base + ATA_ECAT_TOUT_HSM; 16407d47e8d4STejun Heo 16413884f7b0STejun Heo if (eflags & ATA_EFLAG_IS_IO) { 16427d47e8d4STejun Heo if (err_mask & AC_ERR_HSM) 164376326ac1STejun Heo return base + ATA_ECAT_TOUT_HSM; 16447d47e8d4STejun Heo if ((err_mask & 16457d47e8d4STejun Heo (AC_ERR_DEV|AC_ERR_MEDIA|AC_ERR_INVALID)) == AC_ERR_DEV) 164676326ac1STejun Heo return base + ATA_ECAT_UNK_DEV; 1647c6fd2807SJeff Garzik } 1648c6fd2807SJeff Garzik 1649c6fd2807SJeff Garzik return 0; 1650c6fd2807SJeff Garzik } 1651c6fd2807SJeff Garzik 16527d47e8d4STejun Heo struct speed_down_verdict_arg { 1653c6fd2807SJeff Garzik u64 since; 165476326ac1STejun Heo int xfer_ok; 16553884f7b0STejun Heo int nr_errors[ATA_ECAT_NR]; 1656c6fd2807SJeff Garzik }; 1657c6fd2807SJeff Garzik 16587d47e8d4STejun Heo static int speed_down_verdict_cb(struct ata_ering_entry *ent, void *void_arg) 1659c6fd2807SJeff Garzik { 16607d47e8d4STejun Heo struct speed_down_verdict_arg *arg = void_arg; 166176326ac1STejun Heo int cat; 1662c6fd2807SJeff Garzik 1663d9027470SGwendal Grignou if ((ent->eflags & ATA_EFLAG_OLD_ER) || (ent->timestamp < arg->since)) 1664c6fd2807SJeff Garzik return -1; 1665c6fd2807SJeff Garzik 166676326ac1STejun Heo cat = ata_eh_categorize_error(ent->eflags, ent->err_mask, 166776326ac1STejun Heo &arg->xfer_ok); 16687d47e8d4STejun Heo arg->nr_errors[cat]++; 166976326ac1STejun Heo 1670c6fd2807SJeff Garzik return 0; 1671c6fd2807SJeff Garzik } 1672c6fd2807SJeff Garzik 1673c6fd2807SJeff Garzik /** 16747d47e8d4STejun Heo * ata_eh_speed_down_verdict - Determine speed down verdict 1675c6fd2807SJeff Garzik * @dev: Device of interest 1676c6fd2807SJeff Garzik * 1677c6fd2807SJeff Garzik * This function examines error ring of @dev and determines 16787d47e8d4STejun Heo * whether NCQ needs to be turned off, transfer speed should be 16797d47e8d4STejun Heo * stepped down, or falling back to PIO is necessary. 1680c6fd2807SJeff Garzik * 16813884f7b0STejun Heo * ECAT_ATA_BUS : ATA_BUS error for any command 1682c6fd2807SJeff Garzik * 16833884f7b0STejun Heo * ECAT_TOUT_HSM : TIMEOUT for any command or HSM violation for 16843884f7b0STejun Heo * IO commands 16857d47e8d4STejun Heo * 16863884f7b0STejun Heo * ECAT_UNK_DEV : Unknown DEV error for IO commands 1687c6fd2807SJeff Garzik * 168876326ac1STejun Heo * ECAT_DUBIOUS_* : Identical to above three but occurred while 168976326ac1STejun Heo * data transfer hasn't been verified. 169076326ac1STejun Heo * 16913884f7b0STejun Heo * Verdicts are 16927d47e8d4STejun Heo * 16933884f7b0STejun Heo * NCQ_OFF : Turn off NCQ. 16947d47e8d4STejun Heo * 16953884f7b0STejun Heo * SPEED_DOWN : Speed down transfer speed but don't fall back 16963884f7b0STejun Heo * to PIO. 16973884f7b0STejun Heo * 16983884f7b0STejun Heo * FALLBACK_TO_PIO : Fall back to PIO. 16993884f7b0STejun Heo * 17003884f7b0STejun Heo * Even if multiple verdicts are returned, only one action is 170176326ac1STejun Heo * taken per error. An action triggered by non-DUBIOUS errors 170276326ac1STejun Heo * clears ering, while one triggered by DUBIOUS_* errors doesn't. 170376326ac1STejun Heo * This is to expedite speed down decisions right after device is 170476326ac1STejun Heo * initially configured. 17053884f7b0STejun Heo * 17064091fb95SMasahiro Yamada * The following are speed down rules. #1 and #2 deal with 170776326ac1STejun Heo * DUBIOUS errors. 170876326ac1STejun Heo * 170976326ac1STejun Heo * 1. If more than one DUBIOUS_ATA_BUS or DUBIOUS_TOUT_HSM errors 171076326ac1STejun Heo * occurred during last 5 mins, SPEED_DOWN and FALLBACK_TO_PIO. 171176326ac1STejun Heo * 171276326ac1STejun Heo * 2. If more than one DUBIOUS_TOUT_HSM or DUBIOUS_UNK_DEV errors 171376326ac1STejun Heo * occurred during last 5 mins, NCQ_OFF. 171476326ac1STejun Heo * 171576326ac1STejun Heo * 3. If more than 8 ATA_BUS, TOUT_HSM or UNK_DEV errors 171625985edcSLucas De Marchi * occurred during last 5 mins, FALLBACK_TO_PIO 17173884f7b0STejun Heo * 171876326ac1STejun Heo * 4. If more than 3 TOUT_HSM or UNK_DEV errors occurred 17193884f7b0STejun Heo * during last 10 mins, NCQ_OFF. 17203884f7b0STejun Heo * 172176326ac1STejun Heo * 5. If more than 3 ATA_BUS or TOUT_HSM errors, or more than 6 17223884f7b0STejun Heo * UNK_DEV errors occurred during last 10 mins, SPEED_DOWN. 17237d47e8d4STejun Heo * 1724c6fd2807SJeff Garzik * LOCKING: 1725c6fd2807SJeff Garzik * Inherited from caller. 1726c6fd2807SJeff Garzik * 1727c6fd2807SJeff Garzik * RETURNS: 17287d47e8d4STejun Heo * OR of ATA_EH_SPDN_* flags. 1729c6fd2807SJeff Garzik */ 17307d47e8d4STejun Heo static unsigned int ata_eh_speed_down_verdict(struct ata_device *dev) 1731c6fd2807SJeff Garzik { 17327d47e8d4STejun Heo const u64 j5mins = 5LLU * 60 * HZ, j10mins = 10LLU * 60 * HZ; 17337d47e8d4STejun Heo u64 j64 = get_jiffies_64(); 17347d47e8d4STejun Heo struct speed_down_verdict_arg arg; 17357d47e8d4STejun Heo unsigned int verdict = 0; 1736c6fd2807SJeff Garzik 17373884f7b0STejun Heo /* scan past 5 mins of error history */ 17383884f7b0STejun Heo memset(&arg, 0, sizeof(arg)); 17393884f7b0STejun Heo arg.since = j64 - min(j64, j5mins); 17403884f7b0STejun Heo ata_ering_map(&dev->ering, speed_down_verdict_cb, &arg); 17413884f7b0STejun Heo 174276326ac1STejun Heo if (arg.nr_errors[ATA_ECAT_DUBIOUS_ATA_BUS] + 174376326ac1STejun Heo arg.nr_errors[ATA_ECAT_DUBIOUS_TOUT_HSM] > 1) 174476326ac1STejun Heo verdict |= ATA_EH_SPDN_SPEED_DOWN | 174576326ac1STejun Heo ATA_EH_SPDN_FALLBACK_TO_PIO | ATA_EH_SPDN_KEEP_ERRORS; 174676326ac1STejun Heo 174776326ac1STejun Heo if (arg.nr_errors[ATA_ECAT_DUBIOUS_TOUT_HSM] + 174876326ac1STejun Heo arg.nr_errors[ATA_ECAT_DUBIOUS_UNK_DEV] > 1) 174976326ac1STejun Heo verdict |= ATA_EH_SPDN_NCQ_OFF | ATA_EH_SPDN_KEEP_ERRORS; 175076326ac1STejun Heo 17513884f7b0STejun Heo if (arg.nr_errors[ATA_ECAT_ATA_BUS] + 17523884f7b0STejun Heo arg.nr_errors[ATA_ECAT_TOUT_HSM] + 1753663f99b8STejun Heo arg.nr_errors[ATA_ECAT_UNK_DEV] > 6) 17543884f7b0STejun Heo verdict |= ATA_EH_SPDN_FALLBACK_TO_PIO; 17553884f7b0STejun Heo 17567d47e8d4STejun Heo /* scan past 10 mins of error history */ 1757c6fd2807SJeff Garzik memset(&arg, 0, sizeof(arg)); 17587d47e8d4STejun Heo arg.since = j64 - min(j64, j10mins); 17597d47e8d4STejun Heo ata_ering_map(&dev->ering, speed_down_verdict_cb, &arg); 1760c6fd2807SJeff Garzik 17613884f7b0STejun Heo if (arg.nr_errors[ATA_ECAT_TOUT_HSM] + 17623884f7b0STejun Heo arg.nr_errors[ATA_ECAT_UNK_DEV] > 3) 17637d47e8d4STejun Heo verdict |= ATA_EH_SPDN_NCQ_OFF; 17643884f7b0STejun Heo 17653884f7b0STejun Heo if (arg.nr_errors[ATA_ECAT_ATA_BUS] + 17663884f7b0STejun Heo arg.nr_errors[ATA_ECAT_TOUT_HSM] > 3 || 1767663f99b8STejun Heo arg.nr_errors[ATA_ECAT_UNK_DEV] > 6) 17687d47e8d4STejun Heo verdict |= ATA_EH_SPDN_SPEED_DOWN; 1769c6fd2807SJeff Garzik 17707d47e8d4STejun Heo return verdict; 1771c6fd2807SJeff Garzik } 1772c6fd2807SJeff Garzik 1773c6fd2807SJeff Garzik /** 1774c6fd2807SJeff Garzik * ata_eh_speed_down - record error and speed down if necessary 1775c6fd2807SJeff Garzik * @dev: Failed device 17763884f7b0STejun Heo * @eflags: mask of ATA_EFLAG_* flags 1777c6fd2807SJeff Garzik * @err_mask: err_mask of the error 1778c6fd2807SJeff Garzik * 1779c6fd2807SJeff Garzik * Record error and examine error history to determine whether 1780c6fd2807SJeff Garzik * adjusting transmission speed is necessary. It also sets 1781c6fd2807SJeff Garzik * transmission limits appropriately if such adjustment is 1782c6fd2807SJeff Garzik * necessary. 1783c6fd2807SJeff Garzik * 1784c6fd2807SJeff Garzik * LOCKING: 1785c6fd2807SJeff Garzik * Kernel thread context (may sleep). 1786c6fd2807SJeff Garzik * 1787c6fd2807SJeff Garzik * RETURNS: 17887d47e8d4STejun Heo * Determined recovery action. 1789c6fd2807SJeff Garzik */ 17903884f7b0STejun Heo static unsigned int ata_eh_speed_down(struct ata_device *dev, 17913884f7b0STejun Heo unsigned int eflags, unsigned int err_mask) 1792c6fd2807SJeff Garzik { 1793b1c72916STejun Heo struct ata_link *link = ata_dev_phys_link(dev); 179476326ac1STejun Heo int xfer_ok = 0; 17957d47e8d4STejun Heo unsigned int verdict; 17967d47e8d4STejun Heo unsigned int action = 0; 17977d47e8d4STejun Heo 17987d47e8d4STejun Heo /* don't bother if Cat-0 error */ 179976326ac1STejun Heo if (ata_eh_categorize_error(eflags, err_mask, &xfer_ok) == 0) 1800c6fd2807SJeff Garzik return 0; 1801c6fd2807SJeff Garzik 1802c6fd2807SJeff Garzik /* record error and determine whether speed down is necessary */ 18033884f7b0STejun Heo ata_ering_record(&dev->ering, eflags, err_mask); 18047d47e8d4STejun Heo verdict = ata_eh_speed_down_verdict(dev); 1805c6fd2807SJeff Garzik 18067d47e8d4STejun Heo /* turn off NCQ? */ 18077d47e8d4STejun Heo if ((verdict & ATA_EH_SPDN_NCQ_OFF) && 18087d47e8d4STejun Heo (dev->flags & (ATA_DFLAG_PIO | ATA_DFLAG_NCQ | 18097d47e8d4STejun Heo ATA_DFLAG_NCQ_OFF)) == ATA_DFLAG_NCQ) { 18107d47e8d4STejun Heo dev->flags |= ATA_DFLAG_NCQ_OFF; 1811a9a79dfeSJoe Perches ata_dev_warn(dev, "NCQ disabled due to excessive errors\n"); 18127d47e8d4STejun Heo goto done; 18137d47e8d4STejun Heo } 1814c6fd2807SJeff Garzik 18157d47e8d4STejun Heo /* speed down? */ 18167d47e8d4STejun Heo if (verdict & ATA_EH_SPDN_SPEED_DOWN) { 1817c6fd2807SJeff Garzik /* speed down SATA link speed if possible */ 1818a07d499bSTejun Heo if (sata_down_spd_limit(link, 0) == 0) { 1819cf480626STejun Heo action |= ATA_EH_RESET; 18207d47e8d4STejun Heo goto done; 18217d47e8d4STejun Heo } 1822c6fd2807SJeff Garzik 1823c6fd2807SJeff Garzik /* lower transfer mode */ 18247d47e8d4STejun Heo if (dev->spdn_cnt < 2) { 18257d47e8d4STejun Heo static const int dma_dnxfer_sel[] = 18267d47e8d4STejun Heo { ATA_DNXFER_DMA, ATA_DNXFER_40C }; 18277d47e8d4STejun Heo static const int pio_dnxfer_sel[] = 18287d47e8d4STejun Heo { ATA_DNXFER_PIO, ATA_DNXFER_FORCE_PIO0 }; 18297d47e8d4STejun Heo int sel; 1830c6fd2807SJeff Garzik 18317d47e8d4STejun Heo if (dev->xfer_shift != ATA_SHIFT_PIO) 18327d47e8d4STejun Heo sel = dma_dnxfer_sel[dev->spdn_cnt]; 18337d47e8d4STejun Heo else 18347d47e8d4STejun Heo sel = pio_dnxfer_sel[dev->spdn_cnt]; 18357d47e8d4STejun Heo 18367d47e8d4STejun Heo dev->spdn_cnt++; 18377d47e8d4STejun Heo 18387d47e8d4STejun Heo if (ata_down_xfermask_limit(dev, sel) == 0) { 1839cf480626STejun Heo action |= ATA_EH_RESET; 18407d47e8d4STejun Heo goto done; 18417d47e8d4STejun Heo } 18427d47e8d4STejun Heo } 18437d47e8d4STejun Heo } 18447d47e8d4STejun Heo 18457d47e8d4STejun Heo /* Fall back to PIO? Slowing down to PIO is meaningless for 1846663f99b8STejun Heo * SATA ATA devices. Consider it only for PATA and SATAPI. 18477d47e8d4STejun Heo */ 18487d47e8d4STejun Heo if ((verdict & ATA_EH_SPDN_FALLBACK_TO_PIO) && (dev->spdn_cnt >= 2) && 1849663f99b8STejun Heo (link->ap->cbl != ATA_CBL_SATA || dev->class == ATA_DEV_ATAPI) && 18507d47e8d4STejun Heo (dev->xfer_shift != ATA_SHIFT_PIO)) { 18517d47e8d4STejun Heo if (ata_down_xfermask_limit(dev, ATA_DNXFER_FORCE_PIO) == 0) { 18527d47e8d4STejun Heo dev->spdn_cnt = 0; 1853cf480626STejun Heo action |= ATA_EH_RESET; 18547d47e8d4STejun Heo goto done; 18557d47e8d4STejun Heo } 18567d47e8d4STejun Heo } 18577d47e8d4STejun Heo 1858c6fd2807SJeff Garzik return 0; 18597d47e8d4STejun Heo done: 18607d47e8d4STejun Heo /* device has been slowed down, blow error history */ 186176326ac1STejun Heo if (!(verdict & ATA_EH_SPDN_KEEP_ERRORS)) 18627d47e8d4STejun Heo ata_ering_clear(&dev->ering); 18637d47e8d4STejun Heo return action; 1864c6fd2807SJeff Garzik } 1865c6fd2807SJeff Garzik 1866c6fd2807SJeff Garzik /** 18678d899e70SMark Lord * ata_eh_worth_retry - analyze error and decide whether to retry 18688d899e70SMark Lord * @qc: qc to possibly retry 18698d899e70SMark Lord * 18708d899e70SMark Lord * Look at the cause of the error and decide if a retry 18718d899e70SMark Lord * might be useful or not. We don't want to retry media errors 18728d899e70SMark Lord * because the drive itself has probably already taken 10-30 seconds 18738d899e70SMark Lord * doing its own internal retries before reporting the failure. 18748d899e70SMark Lord */ 18758d899e70SMark Lord static inline int ata_eh_worth_retry(struct ata_queued_cmd *qc) 18768d899e70SMark Lord { 18771eaca39aSBian Yu if (qc->err_mask & AC_ERR_MEDIA) 18788d899e70SMark Lord return 0; /* don't retry media errors */ 18798d899e70SMark Lord if (qc->flags & ATA_QCFLAG_IO) 18808d899e70SMark Lord return 1; /* otherwise retry anything from fs stack */ 18818d899e70SMark Lord if (qc->err_mask & AC_ERR_INVALID) 18828d899e70SMark Lord return 0; /* don't retry these */ 18838d899e70SMark Lord return qc->err_mask != AC_ERR_DEV; /* retry if not dev error */ 18848d899e70SMark Lord } 18858d899e70SMark Lord 18868d899e70SMark Lord /** 18877eb49509SDamien Le Moal * ata_eh_quiet - check if we need to be quiet about a command error 18887eb49509SDamien Le Moal * @qc: qc to check 18897eb49509SDamien Le Moal * 18907eb49509SDamien Le Moal * Look at the qc flags anbd its scsi command request flags to determine 18917eb49509SDamien Le Moal * if we need to be quiet about the command failure. 18927eb49509SDamien Le Moal */ 18937eb49509SDamien Le Moal static inline bool ata_eh_quiet(struct ata_queued_cmd *qc) 18947eb49509SDamien Le Moal { 1895c8329cd5SBart Van Assche if (qc->scsicmd && scsi_cmd_to_rq(qc->scsicmd)->rq_flags & RQF_QUIET) 18967eb49509SDamien Le Moal qc->flags |= ATA_QCFLAG_QUIET; 18977eb49509SDamien Le Moal return qc->flags & ATA_QCFLAG_QUIET; 18987eb49509SDamien Le Moal } 18997eb49509SDamien Le Moal 19007eb49509SDamien Le Moal /** 19019b1e2658STejun Heo * ata_eh_link_autopsy - analyze error and determine recovery action 19029b1e2658STejun Heo * @link: host link to perform autopsy on 1903c6fd2807SJeff Garzik * 19040260731fSTejun Heo * Analyze why @link failed and determine which recovery actions 19050260731fSTejun Heo * are needed. This function also sets more detailed AC_ERR_* 19060260731fSTejun Heo * values and fills sense data for ATAPI CHECK SENSE. 1907c6fd2807SJeff Garzik * 1908c6fd2807SJeff Garzik * LOCKING: 1909c6fd2807SJeff Garzik * Kernel thread context (may sleep). 1910c6fd2807SJeff Garzik */ 19119b1e2658STejun Heo static void ata_eh_link_autopsy(struct ata_link *link) 1912c6fd2807SJeff Garzik { 19130260731fSTejun Heo struct ata_port *ap = link->ap; 1914936fd732STejun Heo struct ata_eh_context *ehc = &link->eh_context; 1915258c4e5cSJens Axboe struct ata_queued_cmd *qc; 1916dfcc173dSTejun Heo struct ata_device *dev; 19173884f7b0STejun Heo unsigned int all_err_mask = 0, eflags = 0; 19187eb49509SDamien Le Moal int tag, nr_failed = 0, nr_quiet = 0; 1919c6fd2807SJeff Garzik u32 serror; 1920c6fd2807SJeff Garzik int rc; 1921c6fd2807SJeff Garzik 1922c6fd2807SJeff Garzik if (ehc->i.flags & ATA_EHI_NO_AUTOPSY) 1923c6fd2807SJeff Garzik return; 1924c6fd2807SJeff Garzik 1925c6fd2807SJeff Garzik /* obtain and analyze SError */ 1926936fd732STejun Heo rc = sata_scr_read(link, SCR_ERROR, &serror); 1927c6fd2807SJeff Garzik if (rc == 0) { 1928c6fd2807SJeff Garzik ehc->i.serror |= serror; 19290260731fSTejun Heo ata_eh_analyze_serror(link); 19304e57c517STejun Heo } else if (rc != -EOPNOTSUPP) { 1931cf480626STejun Heo /* SError read failed, force reset and probing */ 1932b558edddSTejun Heo ehc->i.probe_mask |= ATA_ALL_DEVICES; 1933cf480626STejun Heo ehc->i.action |= ATA_EH_RESET; 19344e57c517STejun Heo ehc->i.err_mask |= AC_ERR_OTHER; 19354e57c517STejun Heo } 1936c6fd2807SJeff Garzik 1937c6fd2807SJeff Garzik /* analyze NCQ failure */ 19380260731fSTejun Heo ata_eh_analyze_ncq_error(link); 1939c6fd2807SJeff Garzik 1940c6fd2807SJeff Garzik /* any real error trumps AC_ERR_OTHER */ 1941c6fd2807SJeff Garzik if (ehc->i.err_mask & ~AC_ERR_OTHER) 1942c6fd2807SJeff Garzik ehc->i.err_mask &= ~AC_ERR_OTHER; 1943c6fd2807SJeff Garzik 1944c6fd2807SJeff Garzik all_err_mask |= ehc->i.err_mask; 1945c6fd2807SJeff Garzik 1946258c4e5cSJens Axboe ata_qc_for_each_raw(ap, qc, tag) { 1947b1c72916STejun Heo if (!(qc->flags & ATA_QCFLAG_FAILED) || 1948b1c72916STejun Heo ata_dev_phys_link(qc->dev) != link) 1949c6fd2807SJeff Garzik continue; 1950c6fd2807SJeff Garzik 1951c6fd2807SJeff Garzik /* inherit upper level err_mask */ 1952c6fd2807SJeff Garzik qc->err_mask |= ehc->i.err_mask; 1953c6fd2807SJeff Garzik 1954c6fd2807SJeff Garzik /* analyze TF */ 1955*e3b1fff6SNiklas Cassel ehc->i.action |= ata_eh_analyze_tf(qc); 1956c6fd2807SJeff Garzik 1957c6fd2807SJeff Garzik /* DEV errors are probably spurious in case of ATA_BUS error */ 1958c6fd2807SJeff Garzik if (qc->err_mask & AC_ERR_ATA_BUS) 1959c6fd2807SJeff Garzik qc->err_mask &= ~(AC_ERR_DEV | AC_ERR_MEDIA | 1960c6fd2807SJeff Garzik AC_ERR_INVALID); 1961c6fd2807SJeff Garzik 1962c6fd2807SJeff Garzik /* any real error trumps unknown error */ 1963c6fd2807SJeff Garzik if (qc->err_mask & ~AC_ERR_OTHER) 1964c6fd2807SJeff Garzik qc->err_mask &= ~AC_ERR_OTHER; 1965c6fd2807SJeff Garzik 1966804689adSDamien Le Moal /* 1967804689adSDamien Le Moal * SENSE_VALID trumps dev/unknown error and revalidation. Upper 1968804689adSDamien Le Moal * layers will determine whether the command is worth retrying 1969804689adSDamien Le Moal * based on the sense data and device class/type. Otherwise, 1970804689adSDamien Le Moal * determine directly if the command is worth retrying using its 1971804689adSDamien Le Moal * error mask and flags. 1972804689adSDamien Le Moal */ 1973f90f0828STejun Heo if (qc->flags & ATA_QCFLAG_SENSE_VALID) 1974c6fd2807SJeff Garzik qc->err_mask &= ~(AC_ERR_DEV | AC_ERR_OTHER); 1975804689adSDamien Le Moal else if (ata_eh_worth_retry(qc)) 197603faab78STejun Heo qc->flags |= ATA_QCFLAG_RETRY; 197703faab78STejun Heo 1978c6fd2807SJeff Garzik /* accumulate error info */ 1979c6fd2807SJeff Garzik ehc->i.dev = qc->dev; 1980c6fd2807SJeff Garzik all_err_mask |= qc->err_mask; 1981c6fd2807SJeff Garzik if (qc->flags & ATA_QCFLAG_IO) 19823884f7b0STejun Heo eflags |= ATA_EFLAG_IS_IO; 1983255c03d1SHannes Reinecke trace_ata_eh_link_autopsy_qc(qc); 19847eb49509SDamien Le Moal 19857eb49509SDamien Le Moal /* Count quiet errors */ 19867eb49509SDamien Le Moal if (ata_eh_quiet(qc)) 19877eb49509SDamien Le Moal nr_quiet++; 19887eb49509SDamien Le Moal nr_failed++; 1989c6fd2807SJeff Garzik } 1990c6fd2807SJeff Garzik 19917eb49509SDamien Le Moal /* If all failed commands requested silence, then be quiet */ 19927eb49509SDamien Le Moal if (nr_quiet == nr_failed) 19937eb49509SDamien Le Moal ehc->i.flags |= ATA_EHI_QUIET; 19947eb49509SDamien Le Moal 1995c6fd2807SJeff Garzik /* enforce default EH actions */ 1996c6fd2807SJeff Garzik if (ap->pflags & ATA_PFLAG_FROZEN || 1997c6fd2807SJeff Garzik all_err_mask & (AC_ERR_HSM | AC_ERR_TIMEOUT)) 1998cf480626STejun Heo ehc->i.action |= ATA_EH_RESET; 19993884f7b0STejun Heo else if (((eflags & ATA_EFLAG_IS_IO) && all_err_mask) || 20003884f7b0STejun Heo (!(eflags & ATA_EFLAG_IS_IO) && (all_err_mask & ~AC_ERR_DEV))) 2001c6fd2807SJeff Garzik ehc->i.action |= ATA_EH_REVALIDATE; 2002c6fd2807SJeff Garzik 2003dfcc173dSTejun Heo /* If we have offending qcs and the associated failed device, 2004dfcc173dSTejun Heo * perform per-dev EH action only on the offending device. 2005dfcc173dSTejun Heo */ 2006c6fd2807SJeff Garzik if (ehc->i.dev) { 2007c6fd2807SJeff Garzik ehc->i.dev_action[ehc->i.dev->devno] |= 2008c6fd2807SJeff Garzik ehc->i.action & ATA_EH_PERDEV_MASK; 2009c6fd2807SJeff Garzik ehc->i.action &= ~ATA_EH_PERDEV_MASK; 2010c6fd2807SJeff Garzik } 2011c6fd2807SJeff Garzik 20122695e366STejun Heo /* propagate timeout to host link */ 20132695e366STejun Heo if ((all_err_mask & AC_ERR_TIMEOUT) && !ata_is_host_link(link)) 20142695e366STejun Heo ap->link.eh_context.i.err_mask |= AC_ERR_TIMEOUT; 20152695e366STejun Heo 20162695e366STejun Heo /* record error and consider speeding down */ 2017dfcc173dSTejun Heo dev = ehc->i.dev; 20182695e366STejun Heo if (!dev && ((ata_link_max_devices(link) == 1 && 20192695e366STejun Heo ata_dev_enabled(link->device)))) 2020dfcc173dSTejun Heo dev = link->device; 2021dfcc173dSTejun Heo 202276326ac1STejun Heo if (dev) { 202376326ac1STejun Heo if (dev->flags & ATA_DFLAG_DUBIOUS_XFER) 202476326ac1STejun Heo eflags |= ATA_EFLAG_DUBIOUS_XFER; 20253884f7b0STejun Heo ehc->i.action |= ata_eh_speed_down(dev, eflags, all_err_mask); 2026255c03d1SHannes Reinecke trace_ata_eh_link_autopsy(dev, ehc->i.action, all_err_mask); 2027f1601113SRameshwar Prasad Sahu } 2028c6fd2807SJeff Garzik } 2029c6fd2807SJeff Garzik 2030c6fd2807SJeff Garzik /** 20319b1e2658STejun Heo * ata_eh_autopsy - analyze error and determine recovery action 20329b1e2658STejun Heo * @ap: host port to perform autopsy on 20339b1e2658STejun Heo * 20349b1e2658STejun Heo * Analyze all links of @ap and determine why they failed and 20359b1e2658STejun Heo * which recovery actions are needed. 20369b1e2658STejun Heo * 20379b1e2658STejun Heo * LOCKING: 20389b1e2658STejun Heo * Kernel thread context (may sleep). 20399b1e2658STejun Heo */ 2040fb7fd614STejun Heo void ata_eh_autopsy(struct ata_port *ap) 20419b1e2658STejun Heo { 20429b1e2658STejun Heo struct ata_link *link; 20439b1e2658STejun Heo 20441eca4365STejun Heo ata_for_each_link(link, ap, EDGE) 20459b1e2658STejun Heo ata_eh_link_autopsy(link); 20462695e366STejun Heo 2047b1c72916STejun Heo /* Handle the frigging slave link. Autopsy is done similarly 2048b1c72916STejun Heo * but actions and flags are transferred over to the master 2049b1c72916STejun Heo * link and handled from there. 2050b1c72916STejun Heo */ 2051b1c72916STejun Heo if (ap->slave_link) { 2052b1c72916STejun Heo struct ata_eh_context *mehc = &ap->link.eh_context; 2053b1c72916STejun Heo struct ata_eh_context *sehc = &ap->slave_link->eh_context; 2054b1c72916STejun Heo 2055848e4c68STejun Heo /* transfer control flags from master to slave */ 2056848e4c68STejun Heo sehc->i.flags |= mehc->i.flags & ATA_EHI_TO_SLAVE_MASK; 2057848e4c68STejun Heo 2058848e4c68STejun Heo /* perform autopsy on the slave link */ 2059b1c72916STejun Heo ata_eh_link_autopsy(ap->slave_link); 2060b1c72916STejun Heo 2061848e4c68STejun Heo /* transfer actions from slave to master and clear slave */ 2062b1c72916STejun Heo ata_eh_about_to_do(ap->slave_link, NULL, ATA_EH_ALL_ACTIONS); 2063b1c72916STejun Heo mehc->i.action |= sehc->i.action; 2064b1c72916STejun Heo mehc->i.dev_action[1] |= sehc->i.dev_action[1]; 2065b1c72916STejun Heo mehc->i.flags |= sehc->i.flags; 2066b1c72916STejun Heo ata_eh_done(ap->slave_link, NULL, ATA_EH_ALL_ACTIONS); 2067b1c72916STejun Heo } 2068b1c72916STejun Heo 20692695e366STejun Heo /* Autopsy of fanout ports can affect host link autopsy. 20702695e366STejun Heo * Perform host link autopsy last. 20712695e366STejun Heo */ 2072071f44b1STejun Heo if (sata_pmp_attached(ap)) 20732695e366STejun Heo ata_eh_link_autopsy(&ap->link); 20749b1e2658STejun Heo } 20759b1e2658STejun Heo 20769b1e2658STejun Heo /** 2077d4520903SHannes Reinecke * ata_get_cmd_name - get name for ATA command 2078d4520903SHannes Reinecke * @command: ATA command code to get name for 20796521148cSRobert Hancock * 2080d4520903SHannes Reinecke * Return a textual name of the given command or "unknown" 20816521148cSRobert Hancock * 20826521148cSRobert Hancock * LOCKING: 20836521148cSRobert Hancock * None 20846521148cSRobert Hancock */ 2085d4520903SHannes Reinecke const char *ata_get_cmd_name(u8 command) 20866521148cSRobert Hancock { 20876521148cSRobert Hancock #ifdef CONFIG_ATA_VERBOSE_ERROR 20886521148cSRobert Hancock static const struct 20896521148cSRobert Hancock { 20906521148cSRobert Hancock u8 command; 20916521148cSRobert Hancock const char *text; 20926521148cSRobert Hancock } cmd_descr[] = { 20936521148cSRobert Hancock { ATA_CMD_DEV_RESET, "DEVICE RESET" }, 20946521148cSRobert Hancock { ATA_CMD_CHK_POWER, "CHECK POWER MODE" }, 20956521148cSRobert Hancock { ATA_CMD_STANDBY, "STANDBY" }, 20966521148cSRobert Hancock { ATA_CMD_IDLE, "IDLE" }, 20976521148cSRobert Hancock { ATA_CMD_EDD, "EXECUTE DEVICE DIAGNOSTIC" }, 20986521148cSRobert Hancock { ATA_CMD_DOWNLOAD_MICRO, "DOWNLOAD MICROCODE" }, 20993915c3b5SRobert Hancock { ATA_CMD_DOWNLOAD_MICRO_DMA, "DOWNLOAD MICROCODE DMA" }, 21006521148cSRobert Hancock { ATA_CMD_NOP, "NOP" }, 21016521148cSRobert Hancock { ATA_CMD_FLUSH, "FLUSH CACHE" }, 21026521148cSRobert Hancock { ATA_CMD_FLUSH_EXT, "FLUSH CACHE EXT" }, 21036521148cSRobert Hancock { ATA_CMD_ID_ATA, "IDENTIFY DEVICE" }, 21046521148cSRobert Hancock { ATA_CMD_ID_ATAPI, "IDENTIFY PACKET DEVICE" }, 21056521148cSRobert Hancock { ATA_CMD_SERVICE, "SERVICE" }, 21066521148cSRobert Hancock { ATA_CMD_READ, "READ DMA" }, 21076521148cSRobert Hancock { ATA_CMD_READ_EXT, "READ DMA EXT" }, 21086521148cSRobert Hancock { ATA_CMD_READ_QUEUED, "READ DMA QUEUED" }, 21096521148cSRobert Hancock { ATA_CMD_READ_STREAM_EXT, "READ STREAM EXT" }, 21106521148cSRobert Hancock { ATA_CMD_READ_STREAM_DMA_EXT, "READ STREAM DMA EXT" }, 21116521148cSRobert Hancock { ATA_CMD_WRITE, "WRITE DMA" }, 21126521148cSRobert Hancock { ATA_CMD_WRITE_EXT, "WRITE DMA EXT" }, 21136521148cSRobert Hancock { ATA_CMD_WRITE_QUEUED, "WRITE DMA QUEUED EXT" }, 21146521148cSRobert Hancock { ATA_CMD_WRITE_STREAM_EXT, "WRITE STREAM EXT" }, 21156521148cSRobert Hancock { ATA_CMD_WRITE_STREAM_DMA_EXT, "WRITE STREAM DMA EXT" }, 21166521148cSRobert Hancock { ATA_CMD_WRITE_FUA_EXT, "WRITE DMA FUA EXT" }, 21176521148cSRobert Hancock { ATA_CMD_WRITE_QUEUED_FUA_EXT, "WRITE DMA QUEUED FUA EXT" }, 21186521148cSRobert Hancock { ATA_CMD_FPDMA_READ, "READ FPDMA QUEUED" }, 21196521148cSRobert Hancock { ATA_CMD_FPDMA_WRITE, "WRITE FPDMA QUEUED" }, 21203915c3b5SRobert Hancock { ATA_CMD_FPDMA_SEND, "SEND FPDMA QUEUED" }, 21213915c3b5SRobert Hancock { ATA_CMD_FPDMA_RECV, "RECEIVE FPDMA QUEUED" }, 21226521148cSRobert Hancock { ATA_CMD_PIO_READ, "READ SECTOR(S)" }, 21236521148cSRobert Hancock { ATA_CMD_PIO_READ_EXT, "READ SECTOR(S) EXT" }, 21246521148cSRobert Hancock { ATA_CMD_PIO_WRITE, "WRITE SECTOR(S)" }, 21256521148cSRobert Hancock { ATA_CMD_PIO_WRITE_EXT, "WRITE SECTOR(S) EXT" }, 21266521148cSRobert Hancock { ATA_CMD_READ_MULTI, "READ MULTIPLE" }, 21276521148cSRobert Hancock { ATA_CMD_READ_MULTI_EXT, "READ MULTIPLE EXT" }, 21286521148cSRobert Hancock { ATA_CMD_WRITE_MULTI, "WRITE MULTIPLE" }, 21296521148cSRobert Hancock { ATA_CMD_WRITE_MULTI_EXT, "WRITE MULTIPLE EXT" }, 21306521148cSRobert Hancock { ATA_CMD_WRITE_MULTI_FUA_EXT, "WRITE MULTIPLE FUA EXT" }, 21316521148cSRobert Hancock { ATA_CMD_SET_FEATURES, "SET FEATURES" }, 21326521148cSRobert Hancock { ATA_CMD_SET_MULTI, "SET MULTIPLE MODE" }, 21336521148cSRobert Hancock { ATA_CMD_VERIFY, "READ VERIFY SECTOR(S)" }, 21346521148cSRobert Hancock { ATA_CMD_VERIFY_EXT, "READ VERIFY SECTOR(S) EXT" }, 21356521148cSRobert Hancock { ATA_CMD_WRITE_UNCORR_EXT, "WRITE UNCORRECTABLE EXT" }, 21366521148cSRobert Hancock { ATA_CMD_STANDBYNOW1, "STANDBY IMMEDIATE" }, 21376521148cSRobert Hancock { ATA_CMD_IDLEIMMEDIATE, "IDLE IMMEDIATE" }, 21386521148cSRobert Hancock { ATA_CMD_SLEEP, "SLEEP" }, 21396521148cSRobert Hancock { ATA_CMD_INIT_DEV_PARAMS, "INITIALIZE DEVICE PARAMETERS" }, 21406521148cSRobert Hancock { ATA_CMD_READ_NATIVE_MAX, "READ NATIVE MAX ADDRESS" }, 21416521148cSRobert Hancock { ATA_CMD_READ_NATIVE_MAX_EXT, "READ NATIVE MAX ADDRESS EXT" }, 21426521148cSRobert Hancock { ATA_CMD_SET_MAX, "SET MAX ADDRESS" }, 21436521148cSRobert Hancock { ATA_CMD_SET_MAX_EXT, "SET MAX ADDRESS EXT" }, 21446521148cSRobert Hancock { ATA_CMD_READ_LOG_EXT, "READ LOG EXT" }, 21456521148cSRobert Hancock { ATA_CMD_WRITE_LOG_EXT, "WRITE LOG EXT" }, 21466521148cSRobert Hancock { ATA_CMD_READ_LOG_DMA_EXT, "READ LOG DMA EXT" }, 21476521148cSRobert Hancock { ATA_CMD_WRITE_LOG_DMA_EXT, "WRITE LOG DMA EXT" }, 21483915c3b5SRobert Hancock { ATA_CMD_TRUSTED_NONDATA, "TRUSTED NON-DATA" }, 21496521148cSRobert Hancock { ATA_CMD_TRUSTED_RCV, "TRUSTED RECEIVE" }, 21506521148cSRobert Hancock { ATA_CMD_TRUSTED_RCV_DMA, "TRUSTED RECEIVE DMA" }, 21516521148cSRobert Hancock { ATA_CMD_TRUSTED_SND, "TRUSTED SEND" }, 21526521148cSRobert Hancock { ATA_CMD_TRUSTED_SND_DMA, "TRUSTED SEND DMA" }, 21536521148cSRobert Hancock { ATA_CMD_PMP_READ, "READ BUFFER" }, 21543915c3b5SRobert Hancock { ATA_CMD_PMP_READ_DMA, "READ BUFFER DMA" }, 21556521148cSRobert Hancock { ATA_CMD_PMP_WRITE, "WRITE BUFFER" }, 21563915c3b5SRobert Hancock { ATA_CMD_PMP_WRITE_DMA, "WRITE BUFFER DMA" }, 21576521148cSRobert Hancock { ATA_CMD_CONF_OVERLAY, "DEVICE CONFIGURATION OVERLAY" }, 21586521148cSRobert Hancock { ATA_CMD_SEC_SET_PASS, "SECURITY SET PASSWORD" }, 21596521148cSRobert Hancock { ATA_CMD_SEC_UNLOCK, "SECURITY UNLOCK" }, 21606521148cSRobert Hancock { ATA_CMD_SEC_ERASE_PREP, "SECURITY ERASE PREPARE" }, 21616521148cSRobert Hancock { ATA_CMD_SEC_ERASE_UNIT, "SECURITY ERASE UNIT" }, 21626521148cSRobert Hancock { ATA_CMD_SEC_FREEZE_LOCK, "SECURITY FREEZE LOCK" }, 21636521148cSRobert Hancock { ATA_CMD_SEC_DISABLE_PASS, "SECURITY DISABLE PASSWORD" }, 21646521148cSRobert Hancock { ATA_CMD_CONFIG_STREAM, "CONFIGURE STREAM" }, 21656521148cSRobert Hancock { ATA_CMD_SMART, "SMART" }, 21666521148cSRobert Hancock { ATA_CMD_MEDIA_LOCK, "DOOR LOCK" }, 21676521148cSRobert Hancock { ATA_CMD_MEDIA_UNLOCK, "DOOR UNLOCK" }, 2168acad7627SFUJITA Tomonori { ATA_CMD_DSM, "DATA SET MANAGEMENT" }, 21696521148cSRobert Hancock { ATA_CMD_CHK_MED_CRD_TYP, "CHECK MEDIA CARD TYPE" }, 21706521148cSRobert Hancock { ATA_CMD_CFA_REQ_EXT_ERR, "CFA REQUEST EXTENDED ERROR" }, 21716521148cSRobert Hancock { ATA_CMD_CFA_WRITE_NE, "CFA WRITE SECTORS WITHOUT ERASE" }, 21726521148cSRobert Hancock { ATA_CMD_CFA_TRANS_SECT, "CFA TRANSLATE SECTOR" }, 21736521148cSRobert Hancock { ATA_CMD_CFA_ERASE, "CFA ERASE SECTORS" }, 21746521148cSRobert Hancock { ATA_CMD_CFA_WRITE_MULT_NE, "CFA WRITE MULTIPLE WITHOUT ERASE" }, 21753915c3b5SRobert Hancock { ATA_CMD_REQ_SENSE_DATA, "REQUEST SENSE DATA EXT" }, 21763915c3b5SRobert Hancock { ATA_CMD_SANITIZE_DEVICE, "SANITIZE DEVICE" }, 217728a3fc22SHannes Reinecke { ATA_CMD_ZAC_MGMT_IN, "ZAC MANAGEMENT IN" }, 217827708a95SHannes Reinecke { ATA_CMD_ZAC_MGMT_OUT, "ZAC MANAGEMENT OUT" }, 21796521148cSRobert Hancock { ATA_CMD_READ_LONG, "READ LONG (with retries)" }, 21806521148cSRobert Hancock { ATA_CMD_READ_LONG_ONCE, "READ LONG (without retries)" }, 21816521148cSRobert Hancock { ATA_CMD_WRITE_LONG, "WRITE LONG (with retries)" }, 21826521148cSRobert Hancock { ATA_CMD_WRITE_LONG_ONCE, "WRITE LONG (without retries)" }, 21836521148cSRobert Hancock { ATA_CMD_RESTORE, "RECALIBRATE" }, 21846521148cSRobert Hancock { 0, NULL } /* terminate list */ 21856521148cSRobert Hancock }; 21866521148cSRobert Hancock 21876521148cSRobert Hancock unsigned int i; 21886521148cSRobert Hancock for (i = 0; cmd_descr[i].text; i++) 21896521148cSRobert Hancock if (cmd_descr[i].command == command) 21906521148cSRobert Hancock return cmd_descr[i].text; 21916521148cSRobert Hancock #endif 21926521148cSRobert Hancock 2193d4520903SHannes Reinecke return "unknown"; 21946521148cSRobert Hancock } 2195d4520903SHannes Reinecke EXPORT_SYMBOL_GPL(ata_get_cmd_name); 21966521148cSRobert Hancock 21976521148cSRobert Hancock /** 21989b1e2658STejun Heo * ata_eh_link_report - report error handling to user 21990260731fSTejun Heo * @link: ATA link EH is going on 2200c6fd2807SJeff Garzik * 2201c6fd2807SJeff Garzik * Report EH to user. 2202c6fd2807SJeff Garzik * 2203c6fd2807SJeff Garzik * LOCKING: 2204c6fd2807SJeff Garzik * None. 2205c6fd2807SJeff Garzik */ 22069b1e2658STejun Heo static void ata_eh_link_report(struct ata_link *link) 2207c6fd2807SJeff Garzik { 22080260731fSTejun Heo struct ata_port *ap = link->ap; 22090260731fSTejun Heo struct ata_eh_context *ehc = &link->eh_context; 2210258c4e5cSJens Axboe struct ata_queued_cmd *qc; 2211c6fd2807SJeff Garzik const char *frozen, *desc; 2212462098b0SLevente Kurusa char tries_buf[6] = ""; 2213c6fd2807SJeff Garzik int tag, nr_failed = 0; 2214c6fd2807SJeff Garzik 221594ff3d54STejun Heo if (ehc->i.flags & ATA_EHI_QUIET) 221694ff3d54STejun Heo return; 221794ff3d54STejun Heo 2218c6fd2807SJeff Garzik desc = NULL; 2219c6fd2807SJeff Garzik if (ehc->i.desc[0] != '\0') 2220c6fd2807SJeff Garzik desc = ehc->i.desc; 2221c6fd2807SJeff Garzik 2222258c4e5cSJens Axboe ata_qc_for_each_raw(ap, qc, tag) { 2223b1c72916STejun Heo if (!(qc->flags & ATA_QCFLAG_FAILED) || 2224b1c72916STejun Heo ata_dev_phys_link(qc->dev) != link || 2225e027bd36STejun Heo ((qc->flags & ATA_QCFLAG_QUIET) && 2226e027bd36STejun Heo qc->err_mask == AC_ERR_DEV)) 2227c6fd2807SJeff Garzik continue; 2228c6fd2807SJeff Garzik if (qc->flags & ATA_QCFLAG_SENSE_VALID && !qc->err_mask) 2229c6fd2807SJeff Garzik continue; 2230c6fd2807SJeff Garzik 2231c6fd2807SJeff Garzik nr_failed++; 2232c6fd2807SJeff Garzik } 2233c6fd2807SJeff Garzik 2234c6fd2807SJeff Garzik if (!nr_failed && !ehc->i.err_mask) 2235c6fd2807SJeff Garzik return; 2236c6fd2807SJeff Garzik 2237c6fd2807SJeff Garzik frozen = ""; 2238c6fd2807SJeff Garzik if (ap->pflags & ATA_PFLAG_FROZEN) 2239c6fd2807SJeff Garzik frozen = " frozen"; 2240c6fd2807SJeff Garzik 2241a1e10f7eSTejun Heo if (ap->eh_tries < ATA_EH_MAX_TRIES) 2242462098b0SLevente Kurusa snprintf(tries_buf, sizeof(tries_buf), " t%d", 2243a1e10f7eSTejun Heo ap->eh_tries); 2244a1e10f7eSTejun Heo 2245c6fd2807SJeff Garzik if (ehc->i.dev) { 2246a9a79dfeSJoe Perches ata_dev_err(ehc->i.dev, "exception Emask 0x%x " 2247a1e10f7eSTejun Heo "SAct 0x%x SErr 0x%x action 0x%x%s%s\n", 2248a1e10f7eSTejun Heo ehc->i.err_mask, link->sactive, ehc->i.serror, 2249a1e10f7eSTejun Heo ehc->i.action, frozen, tries_buf); 2250c6fd2807SJeff Garzik if (desc) 2251a9a79dfeSJoe Perches ata_dev_err(ehc->i.dev, "%s\n", desc); 2252c6fd2807SJeff Garzik } else { 2253a9a79dfeSJoe Perches ata_link_err(link, "exception Emask 0x%x " 2254a1e10f7eSTejun Heo "SAct 0x%x SErr 0x%x action 0x%x%s%s\n", 2255a1e10f7eSTejun Heo ehc->i.err_mask, link->sactive, ehc->i.serror, 2256a1e10f7eSTejun Heo ehc->i.action, frozen, tries_buf); 2257c6fd2807SJeff Garzik if (desc) 2258a9a79dfeSJoe Perches ata_link_err(link, "%s\n", desc); 2259c6fd2807SJeff Garzik } 2260c6fd2807SJeff Garzik 22616521148cSRobert Hancock #ifdef CONFIG_ATA_VERBOSE_ERROR 22621333e194SRobert Hancock if (ehc->i.serror) 2263a9a79dfeSJoe Perches ata_link_err(link, 22641333e194SRobert Hancock "SError: { %s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s}\n", 22651333e194SRobert Hancock ehc->i.serror & SERR_DATA_RECOVERED ? "RecovData " : "", 22661333e194SRobert Hancock ehc->i.serror & SERR_COMM_RECOVERED ? "RecovComm " : "", 22671333e194SRobert Hancock ehc->i.serror & SERR_DATA ? "UnrecovData " : "", 22681333e194SRobert Hancock ehc->i.serror & SERR_PERSISTENT ? "Persist " : "", 22691333e194SRobert Hancock ehc->i.serror & SERR_PROTOCOL ? "Proto " : "", 22701333e194SRobert Hancock ehc->i.serror & SERR_INTERNAL ? "HostInt " : "", 22711333e194SRobert Hancock ehc->i.serror & SERR_PHYRDY_CHG ? "PHYRdyChg " : "", 22721333e194SRobert Hancock ehc->i.serror & SERR_PHY_INT_ERR ? "PHYInt " : "", 22731333e194SRobert Hancock ehc->i.serror & SERR_COMM_WAKE ? "CommWake " : "", 22741333e194SRobert Hancock ehc->i.serror & SERR_10B_8B_ERR ? "10B8B " : "", 22751333e194SRobert Hancock ehc->i.serror & SERR_DISPARITY ? "Dispar " : "", 22761333e194SRobert Hancock ehc->i.serror & SERR_CRC ? "BadCRC " : "", 22771333e194SRobert Hancock ehc->i.serror & SERR_HANDSHAKE ? "Handshk " : "", 22781333e194SRobert Hancock ehc->i.serror & SERR_LINK_SEQ_ERR ? "LinkSeq " : "", 22791333e194SRobert Hancock ehc->i.serror & SERR_TRANS_ST_ERROR ? "TrStaTrns " : "", 22801333e194SRobert Hancock ehc->i.serror & SERR_UNRECOG_FIS ? "UnrecFIS " : "", 22811333e194SRobert Hancock ehc->i.serror & SERR_DEV_XCHG ? "DevExch " : ""); 22826521148cSRobert Hancock #endif 22831333e194SRobert Hancock 2284258c4e5cSJens Axboe ata_qc_for_each_raw(ap, qc, tag) { 22858a937581STejun Heo struct ata_taskfile *cmd = &qc->tf, *res = &qc->result_tf; 2286abb6a889STejun Heo char data_buf[20] = ""; 2287abb6a889STejun Heo char cdb_buf[70] = ""; 2288c6fd2807SJeff Garzik 22890260731fSTejun Heo if (!(qc->flags & ATA_QCFLAG_FAILED) || 2290b1c72916STejun Heo ata_dev_phys_link(qc->dev) != link || !qc->err_mask) 2291c6fd2807SJeff Garzik continue; 2292c6fd2807SJeff Garzik 2293abb6a889STejun Heo if (qc->dma_dir != DMA_NONE) { 2294abb6a889STejun Heo static const char *dma_str[] = { 2295abb6a889STejun Heo [DMA_BIDIRECTIONAL] = "bidi", 2296abb6a889STejun Heo [DMA_TO_DEVICE] = "out", 2297abb6a889STejun Heo [DMA_FROM_DEVICE] = "in", 2298abb6a889STejun Heo }; 2299fb1b8b11SGeert Uytterhoeven const char *prot_str = NULL; 2300abb6a889STejun Heo 2301fb1b8b11SGeert Uytterhoeven switch (qc->tf.protocol) { 2302fb1b8b11SGeert Uytterhoeven case ATA_PROT_UNKNOWN: 2303fb1b8b11SGeert Uytterhoeven prot_str = "unknown"; 2304fb1b8b11SGeert Uytterhoeven break; 2305fb1b8b11SGeert Uytterhoeven case ATA_PROT_NODATA: 2306fb1b8b11SGeert Uytterhoeven prot_str = "nodata"; 2307fb1b8b11SGeert Uytterhoeven break; 2308fb1b8b11SGeert Uytterhoeven case ATA_PROT_PIO: 2309fb1b8b11SGeert Uytterhoeven prot_str = "pio"; 2310fb1b8b11SGeert Uytterhoeven break; 2311fb1b8b11SGeert Uytterhoeven case ATA_PROT_DMA: 2312fb1b8b11SGeert Uytterhoeven prot_str = "dma"; 2313fb1b8b11SGeert Uytterhoeven break; 2314fb1b8b11SGeert Uytterhoeven case ATA_PROT_NCQ: 2315fb1b8b11SGeert Uytterhoeven prot_str = "ncq dma"; 2316fb1b8b11SGeert Uytterhoeven break; 2317fb1b8b11SGeert Uytterhoeven case ATA_PROT_NCQ_NODATA: 2318fb1b8b11SGeert Uytterhoeven prot_str = "ncq nodata"; 2319fb1b8b11SGeert Uytterhoeven break; 2320fb1b8b11SGeert Uytterhoeven case ATAPI_PROT_NODATA: 2321fb1b8b11SGeert Uytterhoeven prot_str = "nodata"; 2322fb1b8b11SGeert Uytterhoeven break; 2323fb1b8b11SGeert Uytterhoeven case ATAPI_PROT_PIO: 2324fb1b8b11SGeert Uytterhoeven prot_str = "pio"; 2325fb1b8b11SGeert Uytterhoeven break; 2326fb1b8b11SGeert Uytterhoeven case ATAPI_PROT_DMA: 2327fb1b8b11SGeert Uytterhoeven prot_str = "dma"; 2328fb1b8b11SGeert Uytterhoeven break; 2329fb1b8b11SGeert Uytterhoeven } 2330abb6a889STejun Heo snprintf(data_buf, sizeof(data_buf), " %s %u %s", 2331fb1b8b11SGeert Uytterhoeven prot_str, qc->nbytes, dma_str[qc->dma_dir]); 2332abb6a889STejun Heo } 2333abb6a889STejun Heo 23346521148cSRobert Hancock if (ata_is_atapi(qc->tf.protocol)) { 2335a13b0c9dSHannes Reinecke const u8 *cdb = qc->cdb; 2336a13b0c9dSHannes Reinecke size_t cdb_len = qc->dev->cdb_len; 2337a13b0c9dSHannes Reinecke 2338cbba5b0eSHannes Reinecke if (qc->scsicmd) { 2339cbba5b0eSHannes Reinecke cdb = qc->scsicmd->cmnd; 2340cbba5b0eSHannes Reinecke cdb_len = qc->scsicmd->cmd_len; 2341cbba5b0eSHannes Reinecke } 2342cbba5b0eSHannes Reinecke __scsi_format_command(cdb_buf, sizeof(cdb_buf), 2343cbba5b0eSHannes Reinecke cdb, cdb_len); 2344d4520903SHannes Reinecke } else 2345a9a79dfeSJoe Perches ata_dev_err(qc->dev, "failed command: %s\n", 2346d4520903SHannes Reinecke ata_get_cmd_name(cmd->command)); 2347abb6a889STejun Heo 2348a9a79dfeSJoe Perches ata_dev_err(qc->dev, 23498a937581STejun Heo "cmd %02x/%02x:%02x:%02x:%02x:%02x/%02x:%02x:%02x:%02x:%02x/%02x " 2350abb6a889STejun Heo "tag %d%s\n %s" 23518a937581STejun Heo "res %02x/%02x:%02x:%02x:%02x:%02x/%02x:%02x:%02x:%02x:%02x/%02x " 23525335b729STejun Heo "Emask 0x%x (%s)%s\n", 23538a937581STejun Heo cmd->command, cmd->feature, cmd->nsect, 23548a937581STejun Heo cmd->lbal, cmd->lbam, cmd->lbah, 23558a937581STejun Heo cmd->hob_feature, cmd->hob_nsect, 23568a937581STejun Heo cmd->hob_lbal, cmd->hob_lbam, cmd->hob_lbah, 2357abb6a889STejun Heo cmd->device, qc->tag, data_buf, cdb_buf, 2358efcef265SSergey Shtylyov res->status, res->error, res->nsect, 23598a937581STejun Heo res->lbal, res->lbam, res->lbah, 23608a937581STejun Heo res->hob_feature, res->hob_nsect, 23618a937581STejun Heo res->hob_lbal, res->hob_lbam, res->hob_lbah, 23625335b729STejun Heo res->device, qc->err_mask, ata_err_string(qc->err_mask), 23635335b729STejun Heo qc->err_mask & AC_ERR_NCQ ? " <F>" : ""); 23641333e194SRobert Hancock 23656521148cSRobert Hancock #ifdef CONFIG_ATA_VERBOSE_ERROR 2366efcef265SSergey Shtylyov if (res->status & (ATA_BUSY | ATA_DRDY | ATA_DF | ATA_DRQ | 2367e87fd28cSHannes Reinecke ATA_SENSE | ATA_ERR)) { 2368efcef265SSergey Shtylyov if (res->status & ATA_BUSY) 2369a9a79dfeSJoe Perches ata_dev_err(qc->dev, "status: { Busy }\n"); 23701333e194SRobert Hancock else 2371e87fd28cSHannes Reinecke ata_dev_err(qc->dev, "status: { %s%s%s%s%s}\n", 2372efcef265SSergey Shtylyov res->status & ATA_DRDY ? "DRDY " : "", 2373efcef265SSergey Shtylyov res->status & ATA_DF ? "DF " : "", 2374efcef265SSergey Shtylyov res->status & ATA_DRQ ? "DRQ " : "", 2375efcef265SSergey Shtylyov res->status & ATA_SENSE ? "SENSE " : "", 2376efcef265SSergey Shtylyov res->status & ATA_ERR ? "ERR " : ""); 23771333e194SRobert Hancock } 23781333e194SRobert Hancock 23791333e194SRobert Hancock if (cmd->command != ATA_CMD_PACKET && 2380efcef265SSergey Shtylyov (res->error & (ATA_ICRC | ATA_UNC | ATA_AMNF | ATA_IDNF | 2381efcef265SSergey Shtylyov ATA_ABORTED))) 2382eec7e1c1SAlexey Asemov ata_dev_err(qc->dev, "error: { %s%s%s%s%s}\n", 2383efcef265SSergey Shtylyov res->error & ATA_ICRC ? "ICRC " : "", 2384efcef265SSergey Shtylyov res->error & ATA_UNC ? "UNC " : "", 2385efcef265SSergey Shtylyov res->error & ATA_AMNF ? "AMNF " : "", 2386efcef265SSergey Shtylyov res->error & ATA_IDNF ? "IDNF " : "", 2387efcef265SSergey Shtylyov res->error & ATA_ABORTED ? "ABRT " : ""); 23886521148cSRobert Hancock #endif 2389c6fd2807SJeff Garzik } 2390c6fd2807SJeff Garzik } 2391c6fd2807SJeff Garzik 23929b1e2658STejun Heo /** 23939b1e2658STejun Heo * ata_eh_report - report error handling to user 23949b1e2658STejun Heo * @ap: ATA port to report EH about 23959b1e2658STejun Heo * 23969b1e2658STejun Heo * Report EH to user. 23979b1e2658STejun Heo * 23989b1e2658STejun Heo * LOCKING: 23999b1e2658STejun Heo * None. 24009b1e2658STejun Heo */ 2401fb7fd614STejun Heo void ata_eh_report(struct ata_port *ap) 24029b1e2658STejun Heo { 24039b1e2658STejun Heo struct ata_link *link; 24049b1e2658STejun Heo 24051eca4365STejun Heo ata_for_each_link(link, ap, HOST_FIRST) 24069b1e2658STejun Heo ata_eh_link_report(link); 24079b1e2658STejun Heo } 24089b1e2658STejun Heo 2409cc0680a5STejun Heo static int ata_do_reset(struct ata_link *link, ata_reset_fn_t reset, 2410b1c72916STejun Heo unsigned int *classes, unsigned long deadline, 2411b1c72916STejun Heo bool clear_classes) 2412c6fd2807SJeff Garzik { 2413f58229f8STejun Heo struct ata_device *dev; 2414c6fd2807SJeff Garzik 2415b1c72916STejun Heo if (clear_classes) 24161eca4365STejun Heo ata_for_each_dev(dev, link, ALL) 2417f58229f8STejun Heo classes[dev->devno] = ATA_DEV_UNKNOWN; 2418c6fd2807SJeff Garzik 2419f046519fSTejun Heo return reset(link, classes, deadline); 2420c6fd2807SJeff Garzik } 2421c6fd2807SJeff Garzik 2422e8411fbaSSergei Shtylyov static int ata_eh_followup_srst_needed(struct ata_link *link, int rc) 2423c6fd2807SJeff Garzik { 242445db2f6cSTejun Heo if ((link->flags & ATA_LFLAG_NO_SRST) || ata_link_offline(link)) 2425ae791c05STejun Heo return 0; 24265dbfc9cbSTejun Heo if (rc == -EAGAIN) 2427c6fd2807SJeff Garzik return 1; 2428071f44b1STejun Heo if (sata_pmp_supported(link->ap) && ata_is_host_link(link)) 24293495de73STejun Heo return 1; 2430c6fd2807SJeff Garzik return 0; 2431c6fd2807SJeff Garzik } 2432c6fd2807SJeff Garzik 2433fb7fd614STejun Heo int ata_eh_reset(struct ata_link *link, int classify, 2434c6fd2807SJeff Garzik ata_prereset_fn_t prereset, ata_reset_fn_t softreset, 2435c6fd2807SJeff Garzik ata_reset_fn_t hardreset, ata_postreset_fn_t postreset) 2436c6fd2807SJeff Garzik { 2437afaa5c37STejun Heo struct ata_port *ap = link->ap; 2438b1c72916STejun Heo struct ata_link *slave = ap->slave_link; 2439936fd732STejun Heo struct ata_eh_context *ehc = &link->eh_context; 2440705d2014SBartlomiej Zolnierkiewicz struct ata_eh_context *sehc = slave ? &slave->eh_context : NULL; 2441c6fd2807SJeff Garzik unsigned int *classes = ehc->classes; 2442416dc9edSTejun Heo unsigned int lflags = link->flags; 2443c6fd2807SJeff Garzik int verbose = !(ehc->i.flags & ATA_EHI_QUIET); 2444d8af0eb6STejun Heo int max_tries = 0, try = 0; 2445b1c72916STejun Heo struct ata_link *failed_link; 2446f58229f8STejun Heo struct ata_device *dev; 2447416dc9edSTejun Heo unsigned long deadline, now; 2448c6fd2807SJeff Garzik ata_reset_fn_t reset; 2449afaa5c37STejun Heo unsigned long flags; 2450416dc9edSTejun Heo u32 sstatus; 2451b1c72916STejun Heo int nr_unknown, rc; 2452c6fd2807SJeff Garzik 2453932648b0STejun Heo /* 2454932648b0STejun Heo * Prepare to reset 2455932648b0STejun Heo */ 2456d8af0eb6STejun Heo while (ata_eh_reset_timeouts[max_tries] != ULONG_MAX) 2457d8af0eb6STejun Heo max_tries++; 2458ca6d43b0SDan Williams if (link->flags & ATA_LFLAG_RST_ONCE) 2459ca6d43b0SDan Williams max_tries = 1; 246005944bdfSTejun Heo if (link->flags & ATA_LFLAG_NO_HRST) 246105944bdfSTejun Heo hardreset = NULL; 246205944bdfSTejun Heo if (link->flags & ATA_LFLAG_NO_SRST) 246305944bdfSTejun Heo softreset = NULL; 2464d8af0eb6STejun Heo 246525985edcSLucas De Marchi /* make sure each reset attempt is at least COOL_DOWN apart */ 246619b72321STejun Heo if (ehc->i.flags & ATA_EHI_DID_RESET) { 24670a2c0f56STejun Heo now = jiffies; 246819b72321STejun Heo WARN_ON(time_after(ehc->last_reset, now)); 246919b72321STejun Heo deadline = ata_deadline(ehc->last_reset, 247019b72321STejun Heo ATA_EH_RESET_COOL_DOWN); 24710a2c0f56STejun Heo if (time_before(now, deadline)) 24720a2c0f56STejun Heo schedule_timeout_uninterruptible(deadline - now); 247319b72321STejun Heo } 24740a2c0f56STejun Heo 2475afaa5c37STejun Heo spin_lock_irqsave(ap->lock, flags); 2476afaa5c37STejun Heo ap->pflags |= ATA_PFLAG_RESETTING; 2477afaa5c37STejun Heo spin_unlock_irqrestore(ap->lock, flags); 2478afaa5c37STejun Heo 2479cf480626STejun Heo ata_eh_about_to_do(link, NULL, ATA_EH_RESET); 2480c6fd2807SJeff Garzik 24811eca4365STejun Heo ata_for_each_dev(dev, link, ALL) { 2482cdeab114STejun Heo /* If we issue an SRST then an ATA drive (not ATAPI) 2483cdeab114STejun Heo * may change configuration and be in PIO0 timing. If 2484cdeab114STejun Heo * we do a hard reset (or are coming from power on) 2485cdeab114STejun Heo * this is true for ATA or ATAPI. Until we've set a 2486cdeab114STejun Heo * suitable controller mode we should not touch the 2487cdeab114STejun Heo * bus as we may be talking too fast. 2488cdeab114STejun Heo */ 2489cdeab114STejun Heo dev->pio_mode = XFER_PIO_0; 24905416912aSAaron Lu dev->dma_mode = 0xff; 2491cdeab114STejun Heo 2492cdeab114STejun Heo /* If the controller has a pio mode setup function 2493cdeab114STejun Heo * then use it to set the chipset to rights. Don't 2494cdeab114STejun Heo * touch the DMA setup as that will be dealt with when 2495cdeab114STejun Heo * configuring devices. 2496cdeab114STejun Heo */ 2497cdeab114STejun Heo if (ap->ops->set_piomode) 2498cdeab114STejun Heo ap->ops->set_piomode(ap, dev); 2499cdeab114STejun Heo } 2500cdeab114STejun Heo 2501cf480626STejun Heo /* prefer hardreset */ 2502932648b0STejun Heo reset = NULL; 2503cf480626STejun Heo ehc->i.action &= ~ATA_EH_RESET; 2504cf480626STejun Heo if (hardreset) { 2505cf480626STejun Heo reset = hardreset; 2506a674050eSTejun Heo ehc->i.action |= ATA_EH_HARDRESET; 25074f7faa3fSTejun Heo } else if (softreset) { 2508cf480626STejun Heo reset = softreset; 2509a674050eSTejun Heo ehc->i.action |= ATA_EH_SOFTRESET; 2510cf480626STejun Heo } 2511c6fd2807SJeff Garzik 2512c6fd2807SJeff Garzik if (prereset) { 2513b1c72916STejun Heo unsigned long deadline = ata_deadline(jiffies, 2514b1c72916STejun Heo ATA_EH_PRERESET_TIMEOUT); 2515b1c72916STejun Heo 2516b1c72916STejun Heo if (slave) { 2517b1c72916STejun Heo sehc->i.action &= ~ATA_EH_RESET; 2518b1c72916STejun Heo sehc->i.action |= ehc->i.action; 2519b1c72916STejun Heo } 2520b1c72916STejun Heo 2521b1c72916STejun Heo rc = prereset(link, deadline); 2522b1c72916STejun Heo 2523b1c72916STejun Heo /* If present, do prereset on slave link too. Reset 2524b1c72916STejun Heo * is skipped iff both master and slave links report 2525b1c72916STejun Heo * -ENOENT or clear ATA_EH_RESET. 2526b1c72916STejun Heo */ 2527b1c72916STejun Heo if (slave && (rc == 0 || rc == -ENOENT)) { 2528b1c72916STejun Heo int tmp; 2529b1c72916STejun Heo 2530b1c72916STejun Heo tmp = prereset(slave, deadline); 2531b1c72916STejun Heo if (tmp != -ENOENT) 2532b1c72916STejun Heo rc = tmp; 2533b1c72916STejun Heo 2534b1c72916STejun Heo ehc->i.action |= sehc->i.action; 2535b1c72916STejun Heo } 2536b1c72916STejun Heo 2537c6fd2807SJeff Garzik if (rc) { 2538c961922bSAlan Cox if (rc == -ENOENT) { 2539a9a79dfeSJoe Perches ata_link_dbg(link, "port disabled--ignoring\n"); 2540cf480626STejun Heo ehc->i.action &= ~ATA_EH_RESET; 25414aa9ab67STejun Heo 25421eca4365STejun Heo ata_for_each_dev(dev, link, ALL) 2543f58229f8STejun Heo classes[dev->devno] = ATA_DEV_NONE; 25444aa9ab67STejun Heo 25454aa9ab67STejun Heo rc = 0; 2546c961922bSAlan Cox } else 2547a9a79dfeSJoe Perches ata_link_err(link, 2548a9a79dfeSJoe Perches "prereset failed (errno=%d)\n", 2549a9a79dfeSJoe Perches rc); 2550fccb6ea5STejun Heo goto out; 2551c6fd2807SJeff Garzik } 2552c6fd2807SJeff Garzik 2553932648b0STejun Heo /* prereset() might have cleared ATA_EH_RESET. If so, 2554d6515e6fSTejun Heo * bang classes, thaw and return. 2555932648b0STejun Heo */ 2556932648b0STejun Heo if (reset && !(ehc->i.action & ATA_EH_RESET)) { 25571eca4365STejun Heo ata_for_each_dev(dev, link, ALL) 2558f58229f8STejun Heo classes[dev->devno] = ATA_DEV_NONE; 2559d6515e6fSTejun Heo if ((ap->pflags & ATA_PFLAG_FROZEN) && 2560d6515e6fSTejun Heo ata_is_host_link(link)) 2561d6515e6fSTejun Heo ata_eh_thaw_port(ap); 2562fccb6ea5STejun Heo rc = 0; 2563fccb6ea5STejun Heo goto out; 2564c6fd2807SJeff Garzik } 2565932648b0STejun Heo } 2566c6fd2807SJeff Garzik 2567c6fd2807SJeff Garzik retry: 2568932648b0STejun Heo /* 2569932648b0STejun Heo * Perform reset 2570932648b0STejun Heo */ 2571dc98c32cSTejun Heo if (ata_is_host_link(link)) 2572dc98c32cSTejun Heo ata_eh_freeze_port(ap); 2573dc98c32cSTejun Heo 2574341c2c95STejun Heo deadline = ata_deadline(jiffies, ata_eh_reset_timeouts[try++]); 257531daabdaSTejun Heo 2576932648b0STejun Heo if (reset) { 2577c6fd2807SJeff Garzik if (verbose) 2578a9a79dfeSJoe Perches ata_link_info(link, "%s resetting link\n", 2579c6fd2807SJeff Garzik reset == softreset ? "soft" : "hard"); 2580c6fd2807SJeff Garzik 2581c6fd2807SJeff Garzik /* mark that this EH session started with reset */ 258219b72321STejun Heo ehc->last_reset = jiffies; 2583f8ec26d0SHannes Reinecke if (reset == hardreset) { 25840d64a233STejun Heo ehc->i.flags |= ATA_EHI_DID_HARDRESET; 2585f8ec26d0SHannes Reinecke trace_ata_link_hardreset_begin(link, classes, deadline); 2586f8ec26d0SHannes Reinecke } else { 25870d64a233STejun Heo ehc->i.flags |= ATA_EHI_DID_SOFTRESET; 2588f8ec26d0SHannes Reinecke trace_ata_link_softreset_begin(link, classes, deadline); 2589f8ec26d0SHannes Reinecke } 2590c6fd2807SJeff Garzik 2591b1c72916STejun Heo rc = ata_do_reset(link, reset, classes, deadline, true); 2592f8ec26d0SHannes Reinecke if (reset == hardreset) 2593f8ec26d0SHannes Reinecke trace_ata_link_hardreset_end(link, classes, rc); 2594f8ec26d0SHannes Reinecke else 2595f8ec26d0SHannes Reinecke trace_ata_link_softreset_end(link, classes, rc); 2596b1c72916STejun Heo if (rc && rc != -EAGAIN) { 2597b1c72916STejun Heo failed_link = link; 25985dbfc9cbSTejun Heo goto fail; 2599b1c72916STejun Heo } 2600c6fd2807SJeff Garzik 2601b1c72916STejun Heo /* hardreset slave link if existent */ 2602b1c72916STejun Heo if (slave && reset == hardreset) { 2603b1c72916STejun Heo int tmp; 2604b1c72916STejun Heo 2605b1c72916STejun Heo if (verbose) 2606a9a79dfeSJoe Perches ata_link_info(slave, "hard resetting link\n"); 2607b1c72916STejun Heo 2608b1c72916STejun Heo ata_eh_about_to_do(slave, NULL, ATA_EH_RESET); 2609f8ec26d0SHannes Reinecke trace_ata_slave_hardreset_begin(slave, classes, 2610f8ec26d0SHannes Reinecke deadline); 2611b1c72916STejun Heo tmp = ata_do_reset(slave, reset, classes, deadline, 2612b1c72916STejun Heo false); 2613f8ec26d0SHannes Reinecke trace_ata_slave_hardreset_end(slave, classes, tmp); 2614b1c72916STejun Heo switch (tmp) { 2615b1c72916STejun Heo case -EAGAIN: 2616b1c72916STejun Heo rc = -EAGAIN; 2617e06abcc6SGustavo A. R. Silva break; 2618b1c72916STejun Heo case 0: 2619b1c72916STejun Heo break; 2620b1c72916STejun Heo default: 2621b1c72916STejun Heo failed_link = slave; 2622b1c72916STejun Heo rc = tmp; 2623b1c72916STejun Heo goto fail; 2624b1c72916STejun Heo } 2625b1c72916STejun Heo } 2626b1c72916STejun Heo 2627b1c72916STejun Heo /* perform follow-up SRST if necessary */ 2628c6fd2807SJeff Garzik if (reset == hardreset && 2629e8411fbaSSergei Shtylyov ata_eh_followup_srst_needed(link, rc)) { 2630c6fd2807SJeff Garzik reset = softreset; 2631c6fd2807SJeff Garzik 2632c6fd2807SJeff Garzik if (!reset) { 2633a9a79dfeSJoe Perches ata_link_err(link, 2634a9a79dfeSJoe Perches "follow-up softreset required but no softreset available\n"); 2635b1c72916STejun Heo failed_link = link; 2636fccb6ea5STejun Heo rc = -EINVAL; 263708cf69d0STejun Heo goto fail; 2638c6fd2807SJeff Garzik } 2639c6fd2807SJeff Garzik 2640cf480626STejun Heo ata_eh_about_to_do(link, NULL, ATA_EH_RESET); 2641f8ec26d0SHannes Reinecke trace_ata_link_softreset_begin(link, classes, deadline); 2642b1c72916STejun Heo rc = ata_do_reset(link, reset, classes, deadline, true); 2643f8ec26d0SHannes Reinecke trace_ata_link_softreset_end(link, classes, rc); 2644fe2c4d01STejun Heo if (rc) { 2645fe2c4d01STejun Heo failed_link = link; 2646fe2c4d01STejun Heo goto fail; 2647fe2c4d01STejun Heo } 2648c6fd2807SJeff Garzik } 2649932648b0STejun Heo } else { 2650932648b0STejun Heo if (verbose) 2651a9a79dfeSJoe Perches ata_link_info(link, 2652a9a79dfeSJoe Perches "no reset method available, skipping reset\n"); 2653932648b0STejun Heo if (!(lflags & ATA_LFLAG_ASSUME_CLASS)) 2654932648b0STejun Heo lflags |= ATA_LFLAG_ASSUME_ATA; 2655932648b0STejun Heo } 2656008a7896STejun Heo 2657932648b0STejun Heo /* 2658932648b0STejun Heo * Post-reset processing 2659932648b0STejun Heo */ 26601eca4365STejun Heo ata_for_each_dev(dev, link, ALL) { 2661416dc9edSTejun Heo /* After the reset, the device state is PIO 0 and the 2662416dc9edSTejun Heo * controller state is undefined. Reset also wakes up 2663416dc9edSTejun Heo * drives from sleeping mode. 2664c6fd2807SJeff Garzik */ 2665f58229f8STejun Heo dev->pio_mode = XFER_PIO_0; 2666054a5fbaSTejun Heo dev->flags &= ~ATA_DFLAG_SLEEPING; 2667c6fd2807SJeff Garzik 26683b761d3dSTejun Heo if (ata_phys_link_offline(ata_dev_phys_link(dev))) 26693b761d3dSTejun Heo continue; 26703b761d3dSTejun Heo 26714ccd3329STejun Heo /* apply class override */ 2672416dc9edSTejun Heo if (lflags & ATA_LFLAG_ASSUME_ATA) 2673ae791c05STejun Heo classes[dev->devno] = ATA_DEV_ATA; 2674416dc9edSTejun Heo else if (lflags & ATA_LFLAG_ASSUME_SEMB) 2675816ab897STejun Heo classes[dev->devno] = ATA_DEV_SEMB_UNSUP; 2676ae791c05STejun Heo } 2677ae791c05STejun Heo 2678008a7896STejun Heo /* record current link speed */ 2679936fd732STejun Heo if (sata_scr_read(link, SCR_STATUS, &sstatus) == 0) 2680936fd732STejun Heo link->sata_spd = (sstatus >> 4) & 0xf; 2681b1c72916STejun Heo if (slave && sata_scr_read(slave, SCR_STATUS, &sstatus) == 0) 2682b1c72916STejun Heo slave->sata_spd = (sstatus >> 4) & 0xf; 2683008a7896STejun Heo 2684dc98c32cSTejun Heo /* thaw the port */ 2685dc98c32cSTejun Heo if (ata_is_host_link(link)) 2686dc98c32cSTejun Heo ata_eh_thaw_port(ap); 2687dc98c32cSTejun Heo 2688f046519fSTejun Heo /* postreset() should clear hardware SError. Although SError 2689f046519fSTejun Heo * is cleared during link resume, clearing SError here is 2690f046519fSTejun Heo * necessary as some PHYs raise hotplug events after SRST. 2691f046519fSTejun Heo * This introduces race condition where hotplug occurs between 2692f046519fSTejun Heo * reset and here. This race is mediated by cross checking 2693f046519fSTejun Heo * link onlineness and classification result later. 2694f046519fSTejun Heo */ 2695b1c72916STejun Heo if (postreset) { 2696cc0680a5STejun Heo postreset(link, classes); 2697f8ec26d0SHannes Reinecke trace_ata_link_postreset(link, classes, rc); 2698f8ec26d0SHannes Reinecke if (slave) { 2699b1c72916STejun Heo postreset(slave, classes); 2700f8ec26d0SHannes Reinecke trace_ata_slave_postreset(slave, classes, rc); 2701f8ec26d0SHannes Reinecke } 2702b1c72916STejun Heo } 2703c6fd2807SJeff Garzik 27041e641060STejun Heo /* 27058c56caccSTejun Heo * Some controllers can't be frozen very well and may set spurious 27068c56caccSTejun Heo * error conditions during reset. Clear accumulated error 27078c56caccSTejun Heo * information and re-thaw the port if frozen. As reset is the 27088c56caccSTejun Heo * final recovery action and we cross check link onlineness against 27098c56caccSTejun Heo * device classification later, no hotplug event is lost by this. 27101e641060STejun Heo */ 2711f046519fSTejun Heo spin_lock_irqsave(link->ap->lock, flags); 27121e641060STejun Heo memset(&link->eh_info, 0, sizeof(link->eh_info)); 2713b1c72916STejun Heo if (slave) 27141e641060STejun Heo memset(&slave->eh_info, 0, sizeof(link->eh_info)); 27151e641060STejun Heo ap->pflags &= ~ATA_PFLAG_EH_PENDING; 2716f046519fSTejun Heo spin_unlock_irqrestore(link->ap->lock, flags); 2717f046519fSTejun Heo 27188c56caccSTejun Heo if (ap->pflags & ATA_PFLAG_FROZEN) 27198c56caccSTejun Heo ata_eh_thaw_port(ap); 27208c56caccSTejun Heo 27213b761d3dSTejun Heo /* 27223b761d3dSTejun Heo * Make sure onlineness and classification result correspond. 2723f046519fSTejun Heo * Hotplug could have happened during reset and some 2724f046519fSTejun Heo * controllers fail to wait while a drive is spinning up after 2725f046519fSTejun Heo * being hotplugged causing misdetection. By cross checking 27263b761d3dSTejun Heo * link on/offlineness and classification result, those 27273b761d3dSTejun Heo * conditions can be reliably detected and retried. 2728f046519fSTejun Heo */ 2729b1c72916STejun Heo nr_unknown = 0; 27301eca4365STejun Heo ata_for_each_dev(dev, link, ALL) { 27313b761d3dSTejun Heo if (ata_phys_link_online(ata_dev_phys_link(dev))) { 2732b1c72916STejun Heo if (classes[dev->devno] == ATA_DEV_UNKNOWN) { 2733a9a79dfeSJoe Perches ata_dev_dbg(dev, "link online but device misclassified\n"); 2734f046519fSTejun Heo classes[dev->devno] = ATA_DEV_NONE; 2735b1c72916STejun Heo nr_unknown++; 2736b1c72916STejun Heo } 27373b761d3dSTejun Heo } else if (ata_phys_link_offline(ata_dev_phys_link(dev))) { 27383b761d3dSTejun Heo if (ata_class_enabled(classes[dev->devno])) 2739a9a79dfeSJoe Perches ata_dev_dbg(dev, 2740a9a79dfeSJoe Perches "link offline, clearing class %d to NONE\n", 27413b761d3dSTejun Heo classes[dev->devno]); 27423b761d3dSTejun Heo classes[dev->devno] = ATA_DEV_NONE; 27433b761d3dSTejun Heo } else if (classes[dev->devno] == ATA_DEV_UNKNOWN) { 2744a9a79dfeSJoe Perches ata_dev_dbg(dev, 2745a9a79dfeSJoe Perches "link status unknown, clearing UNKNOWN to NONE\n"); 27463b761d3dSTejun Heo classes[dev->devno] = ATA_DEV_NONE; 27473b761d3dSTejun Heo } 2748f046519fSTejun Heo } 2749f046519fSTejun Heo 2750b1c72916STejun Heo if (classify && nr_unknown) { 2751f046519fSTejun Heo if (try < max_tries) { 2752a9a79dfeSJoe Perches ata_link_warn(link, 2753a9a79dfeSJoe Perches "link online but %d devices misclassified, retrying\n", 27543b761d3dSTejun Heo nr_unknown); 2755b1c72916STejun Heo failed_link = link; 2756f046519fSTejun Heo rc = -EAGAIN; 2757f046519fSTejun Heo goto fail; 2758f046519fSTejun Heo } 2759a9a79dfeSJoe Perches ata_link_warn(link, 27603b761d3dSTejun Heo "link online but %d devices misclassified, " 27613b761d3dSTejun Heo "device detection might fail\n", nr_unknown); 2762f046519fSTejun Heo } 2763f046519fSTejun Heo 2764c6fd2807SJeff Garzik /* reset successful, schedule revalidation */ 2765cf480626STejun Heo ata_eh_done(link, NULL, ATA_EH_RESET); 2766b1c72916STejun Heo if (slave) 2767b1c72916STejun Heo ata_eh_done(slave, NULL, ATA_EH_RESET); 276819b72321STejun Heo ehc->last_reset = jiffies; /* update to completion time */ 2769c6fd2807SJeff Garzik ehc->i.action |= ATA_EH_REVALIDATE; 27706b7ae954STejun Heo link->lpm_policy = ATA_LPM_UNKNOWN; /* reset LPM state */ 2771416dc9edSTejun Heo 2772416dc9edSTejun Heo rc = 0; 2773fccb6ea5STejun Heo out: 2774fccb6ea5STejun Heo /* clear hotplug flag */ 2775fccb6ea5STejun Heo ehc->i.flags &= ~ATA_EHI_HOTPLUGGED; 2776b1c72916STejun Heo if (slave) 2777b1c72916STejun Heo sehc->i.flags &= ~ATA_EHI_HOTPLUGGED; 2778afaa5c37STejun Heo 2779afaa5c37STejun Heo spin_lock_irqsave(ap->lock, flags); 2780afaa5c37STejun Heo ap->pflags &= ~ATA_PFLAG_RESETTING; 2781afaa5c37STejun Heo spin_unlock_irqrestore(ap->lock, flags); 2782afaa5c37STejun Heo 2783c6fd2807SJeff Garzik return rc; 2784416dc9edSTejun Heo 2785416dc9edSTejun Heo fail: 27865958e302STejun Heo /* if SCR isn't accessible on a fan-out port, PMP needs to be reset */ 27875958e302STejun Heo if (!ata_is_host_link(link) && 27885958e302STejun Heo sata_scr_read(link, SCR_STATUS, &sstatus)) 27895958e302STejun Heo rc = -ERESTART; 27905958e302STejun Heo 27917a46c078SGwendal Grignou if (try >= max_tries) { 27928ea7645cSTejun Heo /* 27938ea7645cSTejun Heo * Thaw host port even if reset failed, so that the port 27948ea7645cSTejun Heo * can be retried on the next phy event. This risks 27958ea7645cSTejun Heo * repeated EH runs but seems to be a better tradeoff than 27968ea7645cSTejun Heo * shutting down a port after a botched hotplug attempt. 27978ea7645cSTejun Heo */ 27988ea7645cSTejun Heo if (ata_is_host_link(link)) 27998ea7645cSTejun Heo ata_eh_thaw_port(ap); 2800416dc9edSTejun Heo goto out; 28018ea7645cSTejun Heo } 2802416dc9edSTejun Heo 2803416dc9edSTejun Heo now = jiffies; 2804416dc9edSTejun Heo if (time_before(now, deadline)) { 2805416dc9edSTejun Heo unsigned long delta = deadline - now; 2806416dc9edSTejun Heo 2807a9a79dfeSJoe Perches ata_link_warn(failed_link, 28080a2c0f56STejun Heo "reset failed (errno=%d), retrying in %u secs\n", 28090a2c0f56STejun Heo rc, DIV_ROUND_UP(jiffies_to_msecs(delta), 1000)); 2810416dc9edSTejun Heo 2811c0c362b6STejun Heo ata_eh_release(ap); 2812416dc9edSTejun Heo while (delta) 2813416dc9edSTejun Heo delta = schedule_timeout_uninterruptible(delta); 2814c0c362b6STejun Heo ata_eh_acquire(ap); 2815416dc9edSTejun Heo } 2816416dc9edSTejun Heo 28177a46c078SGwendal Grignou /* 28187a46c078SGwendal Grignou * While disks spinup behind PMP, some controllers fail sending SRST. 28197a46c078SGwendal Grignou * They need to be reset - as well as the PMP - before retrying. 28207a46c078SGwendal Grignou */ 28217a46c078SGwendal Grignou if (rc == -ERESTART) { 28227a46c078SGwendal Grignou if (ata_is_host_link(link)) 28237a46c078SGwendal Grignou ata_eh_thaw_port(ap); 28247a46c078SGwendal Grignou goto out; 28257a46c078SGwendal Grignou } 28267a46c078SGwendal Grignou 2827b1c72916STejun Heo if (try == max_tries - 1) { 2828a07d499bSTejun Heo sata_down_spd_limit(link, 0); 2829b1c72916STejun Heo if (slave) 2830a07d499bSTejun Heo sata_down_spd_limit(slave, 0); 2831b1c72916STejun Heo } else if (rc == -EPIPE) 2832a07d499bSTejun Heo sata_down_spd_limit(failed_link, 0); 2833b1c72916STejun Heo 2834416dc9edSTejun Heo if (hardreset) 2835416dc9edSTejun Heo reset = hardreset; 2836416dc9edSTejun Heo goto retry; 2837c6fd2807SJeff Garzik } 2838c6fd2807SJeff Garzik 283945fabbb7SElias Oltmanns static inline void ata_eh_pull_park_action(struct ata_port *ap) 284045fabbb7SElias Oltmanns { 284145fabbb7SElias Oltmanns struct ata_link *link; 284245fabbb7SElias Oltmanns struct ata_device *dev; 284345fabbb7SElias Oltmanns unsigned long flags; 284445fabbb7SElias Oltmanns 284545fabbb7SElias Oltmanns /* 284645fabbb7SElias Oltmanns * This function can be thought of as an extended version of 284745fabbb7SElias Oltmanns * ata_eh_about_to_do() specially crafted to accommodate the 284845fabbb7SElias Oltmanns * requirements of ATA_EH_PARK handling. Since the EH thread 284945fabbb7SElias Oltmanns * does not leave the do {} while () loop in ata_eh_recover as 285045fabbb7SElias Oltmanns * long as the timeout for a park request to *one* device on 285145fabbb7SElias Oltmanns * the port has not expired, and since we still want to pick 285245fabbb7SElias Oltmanns * up park requests to other devices on the same port or 285345fabbb7SElias Oltmanns * timeout updates for the same device, we have to pull 285445fabbb7SElias Oltmanns * ATA_EH_PARK actions from eh_info into eh_context.i 285545fabbb7SElias Oltmanns * ourselves at the beginning of each pass over the loop. 285645fabbb7SElias Oltmanns * 285745fabbb7SElias Oltmanns * Additionally, all write accesses to &ap->park_req_pending 285816735d02SWolfram Sang * through reinit_completion() (see below) or complete_all() 285945fabbb7SElias Oltmanns * (see ata_scsi_park_store()) are protected by the host lock. 286045fabbb7SElias Oltmanns * As a result we have that park_req_pending.done is zero on 286145fabbb7SElias Oltmanns * exit from this function, i.e. when ATA_EH_PARK actions for 286245fabbb7SElias Oltmanns * *all* devices on port ap have been pulled into the 286345fabbb7SElias Oltmanns * respective eh_context structs. If, and only if, 286445fabbb7SElias Oltmanns * park_req_pending.done is non-zero by the time we reach 286545fabbb7SElias Oltmanns * wait_for_completion_timeout(), another ATA_EH_PARK action 286645fabbb7SElias Oltmanns * has been scheduled for at least one of the devices on port 286745fabbb7SElias Oltmanns * ap and we have to cycle over the do {} while () loop in 286845fabbb7SElias Oltmanns * ata_eh_recover() again. 286945fabbb7SElias Oltmanns */ 287045fabbb7SElias Oltmanns 287145fabbb7SElias Oltmanns spin_lock_irqsave(ap->lock, flags); 287216735d02SWolfram Sang reinit_completion(&ap->park_req_pending); 28731eca4365STejun Heo ata_for_each_link(link, ap, EDGE) { 28741eca4365STejun Heo ata_for_each_dev(dev, link, ALL) { 287545fabbb7SElias Oltmanns struct ata_eh_info *ehi = &link->eh_info; 287645fabbb7SElias Oltmanns 287745fabbb7SElias Oltmanns link->eh_context.i.dev_action[dev->devno] |= 287845fabbb7SElias Oltmanns ehi->dev_action[dev->devno] & ATA_EH_PARK; 287945fabbb7SElias Oltmanns ata_eh_clear_action(link, dev, ehi, ATA_EH_PARK); 288045fabbb7SElias Oltmanns } 288145fabbb7SElias Oltmanns } 288245fabbb7SElias Oltmanns spin_unlock_irqrestore(ap->lock, flags); 288345fabbb7SElias Oltmanns } 288445fabbb7SElias Oltmanns 288545fabbb7SElias Oltmanns static void ata_eh_park_issue_cmd(struct ata_device *dev, int park) 288645fabbb7SElias Oltmanns { 288745fabbb7SElias Oltmanns struct ata_eh_context *ehc = &dev->link->eh_context; 288845fabbb7SElias Oltmanns struct ata_taskfile tf; 288945fabbb7SElias Oltmanns unsigned int err_mask; 289045fabbb7SElias Oltmanns 289145fabbb7SElias Oltmanns ata_tf_init(dev, &tf); 289245fabbb7SElias Oltmanns if (park) { 289345fabbb7SElias Oltmanns ehc->unloaded_mask |= 1 << dev->devno; 289445fabbb7SElias Oltmanns tf.command = ATA_CMD_IDLEIMMEDIATE; 289545fabbb7SElias Oltmanns tf.feature = 0x44; 289645fabbb7SElias Oltmanns tf.lbal = 0x4c; 289745fabbb7SElias Oltmanns tf.lbam = 0x4e; 289845fabbb7SElias Oltmanns tf.lbah = 0x55; 289945fabbb7SElias Oltmanns } else { 290045fabbb7SElias Oltmanns ehc->unloaded_mask &= ~(1 << dev->devno); 290145fabbb7SElias Oltmanns tf.command = ATA_CMD_CHK_POWER; 290245fabbb7SElias Oltmanns } 290345fabbb7SElias Oltmanns 290445fabbb7SElias Oltmanns tf.flags |= ATA_TFLAG_DEVICE | ATA_TFLAG_ISADDR; 2905bd18bc04SHannes Reinecke tf.protocol = ATA_PROT_NODATA; 290645fabbb7SElias Oltmanns err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0); 290745fabbb7SElias Oltmanns if (park && (err_mask || tf.lbal != 0xc4)) { 2908a9a79dfeSJoe Perches ata_dev_err(dev, "head unload failed!\n"); 290945fabbb7SElias Oltmanns ehc->unloaded_mask &= ~(1 << dev->devno); 291045fabbb7SElias Oltmanns } 291145fabbb7SElias Oltmanns } 291245fabbb7SElias Oltmanns 29130260731fSTejun Heo static int ata_eh_revalidate_and_attach(struct ata_link *link, 2914c6fd2807SJeff Garzik struct ata_device **r_failed_dev) 2915c6fd2807SJeff Garzik { 29160260731fSTejun Heo struct ata_port *ap = link->ap; 29170260731fSTejun Heo struct ata_eh_context *ehc = &link->eh_context; 2918c6fd2807SJeff Garzik struct ata_device *dev; 29198c3c52a8STejun Heo unsigned int new_mask = 0; 2920c6fd2807SJeff Garzik unsigned long flags; 2921f58229f8STejun Heo int rc = 0; 2922c6fd2807SJeff Garzik 29238c3c52a8STejun Heo /* For PATA drive side cable detection to work, IDENTIFY must 29248c3c52a8STejun Heo * be done backwards such that PDIAG- is released by the slave 29258c3c52a8STejun Heo * device before the master device is identified. 29268c3c52a8STejun Heo */ 29271eca4365STejun Heo ata_for_each_dev(dev, link, ALL_REVERSE) { 2928f58229f8STejun Heo unsigned int action = ata_eh_dev_action(dev); 2929f58229f8STejun Heo unsigned int readid_flags = 0; 2930c6fd2807SJeff Garzik 2931bff04647STejun Heo if (ehc->i.flags & ATA_EHI_DID_RESET) 2932bff04647STejun Heo readid_flags |= ATA_READID_POSTRESET; 2933bff04647STejun Heo 29349666f400STejun Heo if ((action & ATA_EH_REVALIDATE) && ata_dev_enabled(dev)) { 2935633273a3STejun Heo WARN_ON(dev->class == ATA_DEV_PMP); 2936633273a3STejun Heo 2937b1c72916STejun Heo if (ata_phys_link_offline(ata_dev_phys_link(dev))) { 2938c6fd2807SJeff Garzik rc = -EIO; 29398c3c52a8STejun Heo goto err; 2940c6fd2807SJeff Garzik } 2941c6fd2807SJeff Garzik 29420260731fSTejun Heo ata_eh_about_to_do(link, dev, ATA_EH_REVALIDATE); 2943422c9daaSTejun Heo rc = ata_dev_revalidate(dev, ehc->classes[dev->devno], 2944422c9daaSTejun Heo readid_flags); 2945c6fd2807SJeff Garzik if (rc) 29468c3c52a8STejun Heo goto err; 2947c6fd2807SJeff Garzik 29480260731fSTejun Heo ata_eh_done(link, dev, ATA_EH_REVALIDATE); 2949c6fd2807SJeff Garzik 2950baa1e78aSTejun Heo /* Configuration may have changed, reconfigure 2951baa1e78aSTejun Heo * transfer mode. 2952baa1e78aSTejun Heo */ 2953baa1e78aSTejun Heo ehc->i.flags |= ATA_EHI_SETMODE; 2954baa1e78aSTejun Heo 2955c6fd2807SJeff Garzik /* schedule the scsi_rescan_device() here */ 2956ad72cf98STejun Heo schedule_work(&(ap->scsi_rescan_task)); 2957c6fd2807SJeff Garzik } else if (dev->class == ATA_DEV_UNKNOWN && 2958c6fd2807SJeff Garzik ehc->tries[dev->devno] && 2959c6fd2807SJeff Garzik ata_class_enabled(ehc->classes[dev->devno])) { 2960842faa6cSTejun Heo /* Temporarily set dev->class, it will be 2961842faa6cSTejun Heo * permanently set once all configurations are 2962842faa6cSTejun Heo * complete. This is necessary because new 2963842faa6cSTejun Heo * device configuration is done in two 2964842faa6cSTejun Heo * separate loops. 2965842faa6cSTejun Heo */ 2966c6fd2807SJeff Garzik dev->class = ehc->classes[dev->devno]; 2967c6fd2807SJeff Garzik 2968633273a3STejun Heo if (dev->class == ATA_DEV_PMP) 2969633273a3STejun Heo rc = sata_pmp_attach(dev); 2970633273a3STejun Heo else 2971633273a3STejun Heo rc = ata_dev_read_id(dev, &dev->class, 2972633273a3STejun Heo readid_flags, dev->id); 2973842faa6cSTejun Heo 2974842faa6cSTejun Heo /* read_id might have changed class, store and reset */ 2975842faa6cSTejun Heo ehc->classes[dev->devno] = dev->class; 2976842faa6cSTejun Heo dev->class = ATA_DEV_UNKNOWN; 2977842faa6cSTejun Heo 29788c3c52a8STejun Heo switch (rc) { 29798c3c52a8STejun Heo case 0: 298099cf610aSTejun Heo /* clear error info accumulated during probe */ 298199cf610aSTejun Heo ata_ering_clear(&dev->ering); 2982f58229f8STejun Heo new_mask |= 1 << dev->devno; 29838c3c52a8STejun Heo break; 29848c3c52a8STejun Heo case -ENOENT: 298555a8e2c8STejun Heo /* IDENTIFY was issued to non-existent 298655a8e2c8STejun Heo * device. No need to reset. Just 2987842faa6cSTejun Heo * thaw and ignore the device. 298855a8e2c8STejun Heo */ 298955a8e2c8STejun Heo ata_eh_thaw_port(ap); 2990c6fd2807SJeff Garzik break; 29918c3c52a8STejun Heo default: 29928c3c52a8STejun Heo goto err; 29938c3c52a8STejun Heo } 29948c3c52a8STejun Heo } 2995c6fd2807SJeff Garzik } 2996c6fd2807SJeff Garzik 2997c1c4e8d5STejun Heo /* PDIAG- should have been released, ask cable type if post-reset */ 299833267325STejun Heo if ((ehc->i.flags & ATA_EHI_DID_RESET) && ata_is_host_link(link)) { 299933267325STejun Heo if (ap->ops->cable_detect) 3000c1c4e8d5STejun Heo ap->cbl = ap->ops->cable_detect(ap); 300133267325STejun Heo ata_force_cbl(ap); 300233267325STejun Heo } 3003c1c4e8d5STejun Heo 30048c3c52a8STejun Heo /* Configure new devices forward such that user doesn't see 30058c3c52a8STejun Heo * device detection messages backwards. 30068c3c52a8STejun Heo */ 30071eca4365STejun Heo ata_for_each_dev(dev, link, ALL) { 30084f7c2874STejun Heo if (!(new_mask & (1 << dev->devno))) 30098c3c52a8STejun Heo continue; 30108c3c52a8STejun Heo 3011842faa6cSTejun Heo dev->class = ehc->classes[dev->devno]; 3012842faa6cSTejun Heo 30134f7c2874STejun Heo if (dev->class == ATA_DEV_PMP) 30144f7c2874STejun Heo continue; 30154f7c2874STejun Heo 30168c3c52a8STejun Heo ehc->i.flags |= ATA_EHI_PRINTINFO; 30178c3c52a8STejun Heo rc = ata_dev_configure(dev); 30188c3c52a8STejun Heo ehc->i.flags &= ~ATA_EHI_PRINTINFO; 3019842faa6cSTejun Heo if (rc) { 3020842faa6cSTejun Heo dev->class = ATA_DEV_UNKNOWN; 30218c3c52a8STejun Heo goto err; 3022842faa6cSTejun Heo } 30238c3c52a8STejun Heo 3024c6fd2807SJeff Garzik spin_lock_irqsave(ap->lock, flags); 3025c6fd2807SJeff Garzik ap->pflags |= ATA_PFLAG_SCSI_HOTPLUG; 3026c6fd2807SJeff Garzik spin_unlock_irqrestore(ap->lock, flags); 3027baa1e78aSTejun Heo 302855a8e2c8STejun Heo /* new device discovered, configure xfermode */ 3029baa1e78aSTejun Heo ehc->i.flags |= ATA_EHI_SETMODE; 3030c6fd2807SJeff Garzik } 3031c6fd2807SJeff Garzik 30328c3c52a8STejun Heo return 0; 30338c3c52a8STejun Heo 30348c3c52a8STejun Heo err: 3035c6fd2807SJeff Garzik *r_failed_dev = dev; 3036c6fd2807SJeff Garzik return rc; 3037c6fd2807SJeff Garzik } 3038c6fd2807SJeff Garzik 30396f1d1e3aSTejun Heo /** 30406f1d1e3aSTejun Heo * ata_set_mode - Program timings and issue SET FEATURES - XFER 30416f1d1e3aSTejun Heo * @link: link on which timings will be programmed 304298a1708dSMartin Olsson * @r_failed_dev: out parameter for failed device 30436f1d1e3aSTejun Heo * 30446f1d1e3aSTejun Heo * Set ATA device disk transfer mode (PIO3, UDMA6, etc.). If 30456f1d1e3aSTejun Heo * ata_set_mode() fails, pointer to the failing device is 30466f1d1e3aSTejun Heo * returned in @r_failed_dev. 30476f1d1e3aSTejun Heo * 30486f1d1e3aSTejun Heo * LOCKING: 30496f1d1e3aSTejun Heo * PCI/etc. bus probe sem. 30506f1d1e3aSTejun Heo * 30516f1d1e3aSTejun Heo * RETURNS: 30526f1d1e3aSTejun Heo * 0 on success, negative errno otherwise 30536f1d1e3aSTejun Heo */ 30546f1d1e3aSTejun Heo int ata_set_mode(struct ata_link *link, struct ata_device **r_failed_dev) 30556f1d1e3aSTejun Heo { 30566f1d1e3aSTejun Heo struct ata_port *ap = link->ap; 305700115e0fSTejun Heo struct ata_device *dev; 305800115e0fSTejun Heo int rc; 30596f1d1e3aSTejun Heo 306076326ac1STejun Heo /* if data transfer is verified, clear DUBIOUS_XFER on ering top */ 30611eca4365STejun Heo ata_for_each_dev(dev, link, ENABLED) { 306276326ac1STejun Heo if (!(dev->flags & ATA_DFLAG_DUBIOUS_XFER)) { 306376326ac1STejun Heo struct ata_ering_entry *ent; 306476326ac1STejun Heo 306576326ac1STejun Heo ent = ata_ering_top(&dev->ering); 306676326ac1STejun Heo if (ent) 306776326ac1STejun Heo ent->eflags &= ~ATA_EFLAG_DUBIOUS_XFER; 306876326ac1STejun Heo } 306976326ac1STejun Heo } 307076326ac1STejun Heo 30716f1d1e3aSTejun Heo /* has private set_mode? */ 30726f1d1e3aSTejun Heo if (ap->ops->set_mode) 307300115e0fSTejun Heo rc = ap->ops->set_mode(link, r_failed_dev); 307400115e0fSTejun Heo else 307500115e0fSTejun Heo rc = ata_do_set_mode(link, r_failed_dev); 307600115e0fSTejun Heo 307700115e0fSTejun Heo /* if transfer mode has changed, set DUBIOUS_XFER on device */ 30781eca4365STejun Heo ata_for_each_dev(dev, link, ENABLED) { 307900115e0fSTejun Heo struct ata_eh_context *ehc = &link->eh_context; 308000115e0fSTejun Heo u8 saved_xfer_mode = ehc->saved_xfer_mode[dev->devno]; 308100115e0fSTejun Heo u8 saved_ncq = !!(ehc->saved_ncq_enabled & (1 << dev->devno)); 308200115e0fSTejun Heo 308300115e0fSTejun Heo if (dev->xfer_mode != saved_xfer_mode || 308400115e0fSTejun Heo ata_ncq_enabled(dev) != saved_ncq) 308500115e0fSTejun Heo dev->flags |= ATA_DFLAG_DUBIOUS_XFER; 308600115e0fSTejun Heo } 308700115e0fSTejun Heo 308800115e0fSTejun Heo return rc; 30896f1d1e3aSTejun Heo } 30906f1d1e3aSTejun Heo 309111fc33daSTejun Heo /** 309211fc33daSTejun Heo * atapi_eh_clear_ua - Clear ATAPI UNIT ATTENTION after reset 309311fc33daSTejun Heo * @dev: ATAPI device to clear UA for 309411fc33daSTejun Heo * 309511fc33daSTejun Heo * Resets and other operations can make an ATAPI device raise 309611fc33daSTejun Heo * UNIT ATTENTION which causes the next operation to fail. This 309711fc33daSTejun Heo * function clears UA. 309811fc33daSTejun Heo * 309911fc33daSTejun Heo * LOCKING: 310011fc33daSTejun Heo * EH context (may sleep). 310111fc33daSTejun Heo * 310211fc33daSTejun Heo * RETURNS: 310311fc33daSTejun Heo * 0 on success, -errno on failure. 310411fc33daSTejun Heo */ 310511fc33daSTejun Heo static int atapi_eh_clear_ua(struct ata_device *dev) 310611fc33daSTejun Heo { 310711fc33daSTejun Heo int i; 310811fc33daSTejun Heo 310911fc33daSTejun Heo for (i = 0; i < ATA_EH_UA_TRIES; i++) { 3110b5357081STejun Heo u8 *sense_buffer = dev->link->ap->sector_buf; 311111fc33daSTejun Heo u8 sense_key = 0; 311211fc33daSTejun Heo unsigned int err_mask; 311311fc33daSTejun Heo 311411fc33daSTejun Heo err_mask = atapi_eh_tur(dev, &sense_key); 311511fc33daSTejun Heo if (err_mask != 0 && err_mask != AC_ERR_DEV) { 3116a9a79dfeSJoe Perches ata_dev_warn(dev, 3117a9a79dfeSJoe Perches "TEST_UNIT_READY failed (err_mask=0x%x)\n", 3118a9a79dfeSJoe Perches err_mask); 311911fc33daSTejun Heo return -EIO; 312011fc33daSTejun Heo } 312111fc33daSTejun Heo 312211fc33daSTejun Heo if (!err_mask || sense_key != UNIT_ATTENTION) 312311fc33daSTejun Heo return 0; 312411fc33daSTejun Heo 312511fc33daSTejun Heo err_mask = atapi_eh_request_sense(dev, sense_buffer, sense_key); 312611fc33daSTejun Heo if (err_mask) { 3127a9a79dfeSJoe Perches ata_dev_warn(dev, "failed to clear " 312811fc33daSTejun Heo "UNIT ATTENTION (err_mask=0x%x)\n", err_mask); 312911fc33daSTejun Heo return -EIO; 313011fc33daSTejun Heo } 313111fc33daSTejun Heo } 313211fc33daSTejun Heo 3133a9a79dfeSJoe Perches ata_dev_warn(dev, "UNIT ATTENTION persists after %d tries\n", 3134a9a79dfeSJoe Perches ATA_EH_UA_TRIES); 313511fc33daSTejun Heo 313611fc33daSTejun Heo return 0; 313711fc33daSTejun Heo } 313811fc33daSTejun Heo 31396013efd8STejun Heo /** 31406013efd8STejun Heo * ata_eh_maybe_retry_flush - Retry FLUSH if necessary 31416013efd8STejun Heo * @dev: ATA device which may need FLUSH retry 31426013efd8STejun Heo * 31436013efd8STejun Heo * If @dev failed FLUSH, it needs to be reported upper layer 31446013efd8STejun Heo * immediately as it means that @dev failed to remap and already 31456013efd8STejun Heo * lost at least a sector and further FLUSH retrials won't make 31466013efd8STejun Heo * any difference to the lost sector. However, if FLUSH failed 31476013efd8STejun Heo * for other reasons, for example transmission error, FLUSH needs 31486013efd8STejun Heo * to be retried. 31496013efd8STejun Heo * 31506013efd8STejun Heo * This function determines whether FLUSH failure retry is 31516013efd8STejun Heo * necessary and performs it if so. 31526013efd8STejun Heo * 31536013efd8STejun Heo * RETURNS: 31546013efd8STejun Heo * 0 if EH can continue, -errno if EH needs to be repeated. 31556013efd8STejun Heo */ 31566013efd8STejun Heo static int ata_eh_maybe_retry_flush(struct ata_device *dev) 31576013efd8STejun Heo { 31586013efd8STejun Heo struct ata_link *link = dev->link; 31596013efd8STejun Heo struct ata_port *ap = link->ap; 31606013efd8STejun Heo struct ata_queued_cmd *qc; 31616013efd8STejun Heo struct ata_taskfile tf; 31626013efd8STejun Heo unsigned int err_mask; 31636013efd8STejun Heo int rc = 0; 31646013efd8STejun Heo 31656013efd8STejun Heo /* did flush fail for this device? */ 31666013efd8STejun Heo if (!ata_tag_valid(link->active_tag)) 31676013efd8STejun Heo return 0; 31686013efd8STejun Heo 31696013efd8STejun Heo qc = __ata_qc_from_tag(ap, link->active_tag); 31706013efd8STejun Heo if (qc->dev != dev || (qc->tf.command != ATA_CMD_FLUSH_EXT && 31716013efd8STejun Heo qc->tf.command != ATA_CMD_FLUSH)) 31726013efd8STejun Heo return 0; 31736013efd8STejun Heo 31746013efd8STejun Heo /* if the device failed it, it should be reported to upper layers */ 31756013efd8STejun Heo if (qc->err_mask & AC_ERR_DEV) 31766013efd8STejun Heo return 0; 31776013efd8STejun Heo 31786013efd8STejun Heo /* flush failed for some other reason, give it another shot */ 31796013efd8STejun Heo ata_tf_init(dev, &tf); 31806013efd8STejun Heo 31816013efd8STejun Heo tf.command = qc->tf.command; 31826013efd8STejun Heo tf.flags |= ATA_TFLAG_DEVICE; 31836013efd8STejun Heo tf.protocol = ATA_PROT_NODATA; 31846013efd8STejun Heo 3185a9a79dfeSJoe Perches ata_dev_warn(dev, "retrying FLUSH 0x%x Emask 0x%x\n", 31866013efd8STejun Heo tf.command, qc->err_mask); 31876013efd8STejun Heo 31886013efd8STejun Heo err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0); 31896013efd8STejun Heo if (!err_mask) { 31906013efd8STejun Heo /* 31916013efd8STejun Heo * FLUSH is complete but there's no way to 31926013efd8STejun Heo * successfully complete a failed command from EH. 31936013efd8STejun Heo * Making sure retry is allowed at least once and 31946013efd8STejun Heo * retrying it should do the trick - whatever was in 31956013efd8STejun Heo * the cache is already on the platter and this won't 31966013efd8STejun Heo * cause infinite loop. 31976013efd8STejun Heo */ 31986013efd8STejun Heo qc->scsicmd->allowed = max(qc->scsicmd->allowed, 1); 31996013efd8STejun Heo } else { 3200a9a79dfeSJoe Perches ata_dev_warn(dev, "FLUSH failed Emask 0x%x\n", 32016013efd8STejun Heo err_mask); 32026013efd8STejun Heo rc = -EIO; 32036013efd8STejun Heo 32046013efd8STejun Heo /* if device failed it, report it to upper layers */ 32056013efd8STejun Heo if (err_mask & AC_ERR_DEV) { 32066013efd8STejun Heo qc->err_mask |= AC_ERR_DEV; 32076013efd8STejun Heo qc->result_tf = tf; 32086013efd8STejun Heo if (!(ap->pflags & ATA_PFLAG_FROZEN)) 32096013efd8STejun Heo rc = 0; 32106013efd8STejun Heo } 32116013efd8STejun Heo } 32126013efd8STejun Heo return rc; 32136013efd8STejun Heo } 32146013efd8STejun Heo 32156b7ae954STejun Heo /** 32166b7ae954STejun Heo * ata_eh_set_lpm - configure SATA interface power management 32176b7ae954STejun Heo * @link: link to configure power management 32186b7ae954STejun Heo * @policy: the link power management policy 32196b7ae954STejun Heo * @r_failed_dev: out parameter for failed device 32206b7ae954STejun Heo * 32216b7ae954STejun Heo * Enable SATA Interface power management. This will enable 3222f4ac6476SHans de Goede * Device Interface Power Management (DIPM) for min_power and 3223f4ac6476SHans de Goede * medium_power_with_dipm policies, and then call driver specific 3224f4ac6476SHans de Goede * callbacks for enabling Host Initiated Power management. 32256b7ae954STejun Heo * 32266b7ae954STejun Heo * LOCKING: 32276b7ae954STejun Heo * EH context. 32286b7ae954STejun Heo * 32296b7ae954STejun Heo * RETURNS: 32306b7ae954STejun Heo * 0 on success, -errno on failure. 32316b7ae954STejun Heo */ 32326b7ae954STejun Heo static int ata_eh_set_lpm(struct ata_link *link, enum ata_lpm_policy policy, 32336b7ae954STejun Heo struct ata_device **r_failed_dev) 32346b7ae954STejun Heo { 32356c8ea89cSTejun Heo struct ata_port *ap = ata_is_host_link(link) ? link->ap : NULL; 32366b7ae954STejun Heo struct ata_eh_context *ehc = &link->eh_context; 32376b7ae954STejun Heo struct ata_device *dev, *link_dev = NULL, *lpm_dev = NULL; 3238e5005b15STejun Heo enum ata_lpm_policy old_policy = link->lpm_policy; 32395f6f12ccSTejun Heo bool no_dipm = link->ap->flags & ATA_FLAG_NO_DIPM; 32406b7ae954STejun Heo unsigned int hints = ATA_LPM_EMPTY | ATA_LPM_HIPM; 32416b7ae954STejun Heo unsigned int err_mask; 32426b7ae954STejun Heo int rc; 32436b7ae954STejun Heo 32446b7ae954STejun Heo /* if the link or host doesn't do LPM, noop */ 32454c9029e7SBartlomiej Zolnierkiewicz if (!IS_ENABLED(CONFIG_SATA_HOST) || 32464c9029e7SBartlomiej Zolnierkiewicz (link->flags & ATA_LFLAG_NO_LPM) || (ap && !ap->ops->set_lpm)) 32476b7ae954STejun Heo return 0; 32486b7ae954STejun Heo 32496b7ae954STejun Heo /* 32506b7ae954STejun Heo * DIPM is enabled only for MIN_POWER as some devices 32516b7ae954STejun Heo * misbehave when the host NACKs transition to SLUMBER. Order 32526b7ae954STejun Heo * device and link configurations such that the host always 32536b7ae954STejun Heo * allows DIPM requests. 32546b7ae954STejun Heo */ 32556b7ae954STejun Heo ata_for_each_dev(dev, link, ENABLED) { 32566b7ae954STejun Heo bool hipm = ata_id_has_hipm(dev->id); 3257ae01b249STejun Heo bool dipm = ata_id_has_dipm(dev->id) && !no_dipm; 32586b7ae954STejun Heo 32596b7ae954STejun Heo /* find the first enabled and LPM enabled devices */ 32606b7ae954STejun Heo if (!link_dev) 32616b7ae954STejun Heo link_dev = dev; 32626b7ae954STejun Heo 32636b7ae954STejun Heo if (!lpm_dev && (hipm || dipm)) 32646b7ae954STejun Heo lpm_dev = dev; 32656b7ae954STejun Heo 32666b7ae954STejun Heo hints &= ~ATA_LPM_EMPTY; 32676b7ae954STejun Heo if (!hipm) 32686b7ae954STejun Heo hints &= ~ATA_LPM_HIPM; 32696b7ae954STejun Heo 32706b7ae954STejun Heo /* disable DIPM before changing link config */ 3271f4ac6476SHans de Goede if (policy < ATA_LPM_MED_POWER_WITH_DIPM && dipm) { 32726b7ae954STejun Heo err_mask = ata_dev_set_feature(dev, 32736b7ae954STejun Heo SETFEATURES_SATA_DISABLE, SATA_DIPM); 32746b7ae954STejun Heo if (err_mask && err_mask != AC_ERR_DEV) { 3275a9a79dfeSJoe Perches ata_dev_warn(dev, 32766b7ae954STejun Heo "failed to disable DIPM, Emask 0x%x\n", 32776b7ae954STejun Heo err_mask); 32786b7ae954STejun Heo rc = -EIO; 32796b7ae954STejun Heo goto fail; 32806b7ae954STejun Heo } 32816b7ae954STejun Heo } 32826b7ae954STejun Heo } 32836b7ae954STejun Heo 32846c8ea89cSTejun Heo if (ap) { 32856b7ae954STejun Heo rc = ap->ops->set_lpm(link, policy, hints); 32866b7ae954STejun Heo if (!rc && ap->slave_link) 32876b7ae954STejun Heo rc = ap->ops->set_lpm(ap->slave_link, policy, hints); 32886c8ea89cSTejun Heo } else 32896c8ea89cSTejun Heo rc = sata_pmp_set_lpm(link, policy, hints); 32906b7ae954STejun Heo 32916b7ae954STejun Heo /* 32926b7ae954STejun Heo * Attribute link config failure to the first (LPM) enabled 32936b7ae954STejun Heo * device on the link. 32946b7ae954STejun Heo */ 32956b7ae954STejun Heo if (rc) { 32966b7ae954STejun Heo if (rc == -EOPNOTSUPP) { 32976b7ae954STejun Heo link->flags |= ATA_LFLAG_NO_LPM; 32986b7ae954STejun Heo return 0; 32996b7ae954STejun Heo } 33006b7ae954STejun Heo dev = lpm_dev ? lpm_dev : link_dev; 33016b7ae954STejun Heo goto fail; 33026b7ae954STejun Heo } 33036b7ae954STejun Heo 3304e5005b15STejun Heo /* 3305e5005b15STejun Heo * Low level driver acked the transition. Issue DIPM command 3306e5005b15STejun Heo * with the new policy set. 3307e5005b15STejun Heo */ 3308e5005b15STejun Heo link->lpm_policy = policy; 3309e5005b15STejun Heo if (ap && ap->slave_link) 3310e5005b15STejun Heo ap->slave_link->lpm_policy = policy; 3311e5005b15STejun Heo 33126b7ae954STejun Heo /* host config updated, enable DIPM if transitioning to MIN_POWER */ 33136b7ae954STejun Heo ata_for_each_dev(dev, link, ENABLED) { 3314f4ac6476SHans de Goede if (policy >= ATA_LPM_MED_POWER_WITH_DIPM && !no_dipm && 3315ae01b249STejun Heo ata_id_has_dipm(dev->id)) { 33166b7ae954STejun Heo err_mask = ata_dev_set_feature(dev, 33176b7ae954STejun Heo SETFEATURES_SATA_ENABLE, SATA_DIPM); 33186b7ae954STejun Heo if (err_mask && err_mask != AC_ERR_DEV) { 3319a9a79dfeSJoe Perches ata_dev_warn(dev, 33206b7ae954STejun Heo "failed to enable DIPM, Emask 0x%x\n", 33216b7ae954STejun Heo err_mask); 33226b7ae954STejun Heo rc = -EIO; 33236b7ae954STejun Heo goto fail; 33246b7ae954STejun Heo } 33256b7ae954STejun Heo } 33266b7ae954STejun Heo } 33276b7ae954STejun Heo 332809c5b480SGabriele Mazzotta link->last_lpm_change = jiffies; 332909c5b480SGabriele Mazzotta link->flags |= ATA_LFLAG_CHANGED; 333009c5b480SGabriele Mazzotta 33316b7ae954STejun Heo return 0; 33326b7ae954STejun Heo 33336b7ae954STejun Heo fail: 3334e5005b15STejun Heo /* restore the old policy */ 3335e5005b15STejun Heo link->lpm_policy = old_policy; 3336e5005b15STejun Heo if (ap && ap->slave_link) 3337e5005b15STejun Heo ap->slave_link->lpm_policy = old_policy; 3338e5005b15STejun Heo 33396b7ae954STejun Heo /* if no device or only one more chance is left, disable LPM */ 33406b7ae954STejun Heo if (!dev || ehc->tries[dev->devno] <= 2) { 3341a9a79dfeSJoe Perches ata_link_warn(link, "disabling LPM on the link\n"); 33426b7ae954STejun Heo link->flags |= ATA_LFLAG_NO_LPM; 33436b7ae954STejun Heo } 33446b7ae954STejun Heo if (r_failed_dev) 33456b7ae954STejun Heo *r_failed_dev = dev; 33466b7ae954STejun Heo return rc; 33476b7ae954STejun Heo } 33486b7ae954STejun Heo 33498a745f1fSKristen Carlson Accardi int ata_link_nr_enabled(struct ata_link *link) 3350c6fd2807SJeff Garzik { 3351f58229f8STejun Heo struct ata_device *dev; 3352f58229f8STejun Heo int cnt = 0; 3353c6fd2807SJeff Garzik 33541eca4365STejun Heo ata_for_each_dev(dev, link, ENABLED) 3355c6fd2807SJeff Garzik cnt++; 3356c6fd2807SJeff Garzik return cnt; 3357c6fd2807SJeff Garzik } 3358c6fd2807SJeff Garzik 33590260731fSTejun Heo static int ata_link_nr_vacant(struct ata_link *link) 3360c6fd2807SJeff Garzik { 3361f58229f8STejun Heo struct ata_device *dev; 3362f58229f8STejun Heo int cnt = 0; 3363c6fd2807SJeff Garzik 33641eca4365STejun Heo ata_for_each_dev(dev, link, ALL) 3365f58229f8STejun Heo if (dev->class == ATA_DEV_UNKNOWN) 3366c6fd2807SJeff Garzik cnt++; 3367c6fd2807SJeff Garzik return cnt; 3368c6fd2807SJeff Garzik } 3369c6fd2807SJeff Garzik 33700260731fSTejun Heo static int ata_eh_skip_recovery(struct ata_link *link) 3371c6fd2807SJeff Garzik { 3372672b2d65STejun Heo struct ata_port *ap = link->ap; 33730260731fSTejun Heo struct ata_eh_context *ehc = &link->eh_context; 3374f58229f8STejun Heo struct ata_device *dev; 3375c6fd2807SJeff Garzik 3376f9df58cbSTejun Heo /* skip disabled links */ 3377f9df58cbSTejun Heo if (link->flags & ATA_LFLAG_DISABLED) 3378f9df58cbSTejun Heo return 1; 3379f9df58cbSTejun Heo 3380e2f3d75fSTejun Heo /* skip if explicitly requested */ 3381e2f3d75fSTejun Heo if (ehc->i.flags & ATA_EHI_NO_RECOVERY) 3382e2f3d75fSTejun Heo return 1; 3383e2f3d75fSTejun Heo 3384672b2d65STejun Heo /* thaw frozen port and recover failed devices */ 3385672b2d65STejun Heo if ((ap->pflags & ATA_PFLAG_FROZEN) || ata_link_nr_enabled(link)) 3386672b2d65STejun Heo return 0; 3387672b2d65STejun Heo 3388672b2d65STejun Heo /* reset at least once if reset is requested */ 3389672b2d65STejun Heo if ((ehc->i.action & ATA_EH_RESET) && 3390672b2d65STejun Heo !(ehc->i.flags & ATA_EHI_DID_RESET)) 3391c6fd2807SJeff Garzik return 0; 3392c6fd2807SJeff Garzik 3393c6fd2807SJeff Garzik /* skip if class codes for all vacant slots are ATA_DEV_NONE */ 33941eca4365STejun Heo ata_for_each_dev(dev, link, ALL) { 3395c6fd2807SJeff Garzik if (dev->class == ATA_DEV_UNKNOWN && 3396c6fd2807SJeff Garzik ehc->classes[dev->devno] != ATA_DEV_NONE) 3397c6fd2807SJeff Garzik return 0; 3398c6fd2807SJeff Garzik } 3399c6fd2807SJeff Garzik 3400c6fd2807SJeff Garzik return 1; 3401c6fd2807SJeff Garzik } 3402c6fd2807SJeff Garzik 3403c2c7a89cSTejun Heo static int ata_count_probe_trials_cb(struct ata_ering_entry *ent, void *void_arg) 3404c2c7a89cSTejun Heo { 3405c2c7a89cSTejun Heo u64 interval = msecs_to_jiffies(ATA_EH_PROBE_TRIAL_INTERVAL); 3406c2c7a89cSTejun Heo u64 now = get_jiffies_64(); 3407c2c7a89cSTejun Heo int *trials = void_arg; 3408c2c7a89cSTejun Heo 34096868225eSLin Ming if ((ent->eflags & ATA_EFLAG_OLD_ER) || 34106868225eSLin Ming (ent->timestamp < now - min(now, interval))) 3411c2c7a89cSTejun Heo return -1; 3412c2c7a89cSTejun Heo 3413c2c7a89cSTejun Heo (*trials)++; 3414c2c7a89cSTejun Heo return 0; 3415c2c7a89cSTejun Heo } 3416c2c7a89cSTejun Heo 341702c05a27STejun Heo static int ata_eh_schedule_probe(struct ata_device *dev) 341802c05a27STejun Heo { 341902c05a27STejun Heo struct ata_eh_context *ehc = &dev->link->eh_context; 3420c2c7a89cSTejun Heo struct ata_link *link = ata_dev_phys_link(dev); 3421c2c7a89cSTejun Heo int trials = 0; 342202c05a27STejun Heo 342302c05a27STejun Heo if (!(ehc->i.probe_mask & (1 << dev->devno)) || 342402c05a27STejun Heo (ehc->did_probe_mask & (1 << dev->devno))) 342502c05a27STejun Heo return 0; 342602c05a27STejun Heo 342702c05a27STejun Heo ata_eh_detach_dev(dev); 342802c05a27STejun Heo ata_dev_init(dev); 342902c05a27STejun Heo ehc->did_probe_mask |= (1 << dev->devno); 3430cf480626STejun Heo ehc->i.action |= ATA_EH_RESET; 343100115e0fSTejun Heo ehc->saved_xfer_mode[dev->devno] = 0; 343200115e0fSTejun Heo ehc->saved_ncq_enabled &= ~(1 << dev->devno); 343302c05a27STejun Heo 34346b7ae954STejun Heo /* the link maybe in a deep sleep, wake it up */ 34356c8ea89cSTejun Heo if (link->lpm_policy > ATA_LPM_MAX_POWER) { 34366c8ea89cSTejun Heo if (ata_is_host_link(link)) 34376c8ea89cSTejun Heo link->ap->ops->set_lpm(link, ATA_LPM_MAX_POWER, 34386c8ea89cSTejun Heo ATA_LPM_EMPTY); 34396c8ea89cSTejun Heo else 34406c8ea89cSTejun Heo sata_pmp_set_lpm(link, ATA_LPM_MAX_POWER, 34416c8ea89cSTejun Heo ATA_LPM_EMPTY); 34426c8ea89cSTejun Heo } 34436b7ae954STejun Heo 3444c2c7a89cSTejun Heo /* Record and count probe trials on the ering. The specific 3445c2c7a89cSTejun Heo * error mask used is irrelevant. Because a successful device 3446c2c7a89cSTejun Heo * detection clears the ering, this count accumulates only if 3447c2c7a89cSTejun Heo * there are consecutive failed probes. 3448c2c7a89cSTejun Heo * 3449c2c7a89cSTejun Heo * If the count is equal to or higher than ATA_EH_PROBE_TRIALS 3450c2c7a89cSTejun Heo * in the last ATA_EH_PROBE_TRIAL_INTERVAL, link speed is 3451c2c7a89cSTejun Heo * forced to 1.5Gbps. 3452c2c7a89cSTejun Heo * 3453c2c7a89cSTejun Heo * This is to work around cases where failed link speed 3454c2c7a89cSTejun Heo * negotiation results in device misdetection leading to 3455c2c7a89cSTejun Heo * infinite DEVXCHG or PHRDY CHG events. 3456c2c7a89cSTejun Heo */ 3457c2c7a89cSTejun Heo ata_ering_record(&dev->ering, 0, AC_ERR_OTHER); 3458c2c7a89cSTejun Heo ata_ering_map(&dev->ering, ata_count_probe_trials_cb, &trials); 3459c2c7a89cSTejun Heo 3460c2c7a89cSTejun Heo if (trials > ATA_EH_PROBE_TRIALS) 3461c2c7a89cSTejun Heo sata_down_spd_limit(link, 1); 3462c2c7a89cSTejun Heo 346302c05a27STejun Heo return 1; 346402c05a27STejun Heo } 346502c05a27STejun Heo 34669b1e2658STejun Heo static int ata_eh_handle_dev_fail(struct ata_device *dev, int err) 3467fee7ca72STejun Heo { 34689af5c9c9STejun Heo struct ata_eh_context *ehc = &dev->link->eh_context; 3469fee7ca72STejun Heo 3470cf9a590aSTejun Heo /* -EAGAIN from EH routine indicates retry without prejudice. 3471cf9a590aSTejun Heo * The requester is responsible for ensuring forward progress. 3472cf9a590aSTejun Heo */ 3473cf9a590aSTejun Heo if (err != -EAGAIN) 3474fee7ca72STejun Heo ehc->tries[dev->devno]--; 3475fee7ca72STejun Heo 3476fee7ca72STejun Heo switch (err) { 3477fee7ca72STejun Heo case -ENODEV: 3478fee7ca72STejun Heo /* device missing or wrong IDENTIFY data, schedule probing */ 3479fee7ca72STejun Heo ehc->i.probe_mask |= (1 << dev->devno); 3480df561f66SGustavo A. R. Silva fallthrough; 3481fee7ca72STejun Heo case -EINVAL: 3482fee7ca72STejun Heo /* give it just one more chance */ 3483fee7ca72STejun Heo ehc->tries[dev->devno] = min(ehc->tries[dev->devno], 1); 3484df561f66SGustavo A. R. Silva fallthrough; 3485fee7ca72STejun Heo case -EIO: 3486d89293abSTejun Heo if (ehc->tries[dev->devno] == 1) { 3487fee7ca72STejun Heo /* This is the last chance, better to slow 3488fee7ca72STejun Heo * down than lose it. 3489fee7ca72STejun Heo */ 3490a07d499bSTejun Heo sata_down_spd_limit(ata_dev_phys_link(dev), 0); 3491d89293abSTejun Heo if (dev->pio_mode > XFER_PIO_0) 3492fee7ca72STejun Heo ata_down_xfermask_limit(dev, ATA_DNXFER_PIO); 3493fee7ca72STejun Heo } 3494fee7ca72STejun Heo } 3495fee7ca72STejun Heo 3496fee7ca72STejun Heo if (ata_dev_enabled(dev) && !ehc->tries[dev->devno]) { 3497fee7ca72STejun Heo /* disable device if it has used up all its chances */ 3498fee7ca72STejun Heo ata_dev_disable(dev); 3499fee7ca72STejun Heo 3500fee7ca72STejun Heo /* detach if offline */ 3501b1c72916STejun Heo if (ata_phys_link_offline(ata_dev_phys_link(dev))) 3502fee7ca72STejun Heo ata_eh_detach_dev(dev); 3503fee7ca72STejun Heo 350402c05a27STejun Heo /* schedule probe if necessary */ 350587fbc5a0STejun Heo if (ata_eh_schedule_probe(dev)) { 3506fee7ca72STejun Heo ehc->tries[dev->devno] = ATA_EH_DEV_TRIES; 350787fbc5a0STejun Heo memset(ehc->cmd_timeout_idx[dev->devno], 0, 350887fbc5a0STejun Heo sizeof(ehc->cmd_timeout_idx[dev->devno])); 350987fbc5a0STejun Heo } 35109b1e2658STejun Heo 35119b1e2658STejun Heo return 1; 3512fee7ca72STejun Heo } else { 3513cf480626STejun Heo ehc->i.action |= ATA_EH_RESET; 35149b1e2658STejun Heo return 0; 3515fee7ca72STejun Heo } 3516fee7ca72STejun Heo } 3517fee7ca72STejun Heo 3518c6fd2807SJeff Garzik /** 3519c6fd2807SJeff Garzik * ata_eh_recover - recover host port after error 3520c6fd2807SJeff Garzik * @ap: host port to recover 3521c6fd2807SJeff Garzik * @prereset: prereset method (can be NULL) 3522c6fd2807SJeff Garzik * @softreset: softreset method (can be NULL) 3523c6fd2807SJeff Garzik * @hardreset: hardreset method (can be NULL) 3524c6fd2807SJeff Garzik * @postreset: postreset method (can be NULL) 35259b1e2658STejun Heo * @r_failed_link: out parameter for failed link 3526c6fd2807SJeff Garzik * 3527c6fd2807SJeff Garzik * This is the alpha and omega, eum and yang, heart and soul of 3528c6fd2807SJeff Garzik * libata exception handling. On entry, actions required to 35299b1e2658STejun Heo * recover each link and hotplug requests are recorded in the 35309b1e2658STejun Heo * link's eh_context. This function executes all the operations 35319b1e2658STejun Heo * with appropriate retrials and fallbacks to resurrect failed 3532c6fd2807SJeff Garzik * devices, detach goners and greet newcomers. 3533c6fd2807SJeff Garzik * 3534c6fd2807SJeff Garzik * LOCKING: 3535c6fd2807SJeff Garzik * Kernel thread context (may sleep). 3536c6fd2807SJeff Garzik * 3537c6fd2807SJeff Garzik * RETURNS: 3538c6fd2807SJeff Garzik * 0 on success, -errno on failure. 3539c6fd2807SJeff Garzik */ 3540fb7fd614STejun Heo int ata_eh_recover(struct ata_port *ap, ata_prereset_fn_t prereset, 3541c6fd2807SJeff Garzik ata_reset_fn_t softreset, ata_reset_fn_t hardreset, 35429b1e2658STejun Heo ata_postreset_fn_t postreset, 35439b1e2658STejun Heo struct ata_link **r_failed_link) 3544c6fd2807SJeff Garzik { 35459b1e2658STejun Heo struct ata_link *link; 3546c6fd2807SJeff Garzik struct ata_device *dev; 35476b7ae954STejun Heo int rc, nr_fails; 354845fabbb7SElias Oltmanns unsigned long flags, deadline; 3549c6fd2807SJeff Garzik 3550c6fd2807SJeff Garzik /* prep for recovery */ 35511eca4365STejun Heo ata_for_each_link(link, ap, EDGE) { 35529b1e2658STejun Heo struct ata_eh_context *ehc = &link->eh_context; 35539b1e2658STejun Heo 3554f9df58cbSTejun Heo /* re-enable link? */ 3555f9df58cbSTejun Heo if (ehc->i.action & ATA_EH_ENABLE_LINK) { 3556f9df58cbSTejun Heo ata_eh_about_to_do(link, NULL, ATA_EH_ENABLE_LINK); 3557f9df58cbSTejun Heo spin_lock_irqsave(ap->lock, flags); 3558f9df58cbSTejun Heo link->flags &= ~ATA_LFLAG_DISABLED; 3559f9df58cbSTejun Heo spin_unlock_irqrestore(ap->lock, flags); 3560f9df58cbSTejun Heo ata_eh_done(link, NULL, ATA_EH_ENABLE_LINK); 3561f9df58cbSTejun Heo } 3562f9df58cbSTejun Heo 35631eca4365STejun Heo ata_for_each_dev(dev, link, ALL) { 3564fd995f70STejun Heo if (link->flags & ATA_LFLAG_NO_RETRY) 3565fd995f70STejun Heo ehc->tries[dev->devno] = 1; 3566fd995f70STejun Heo else 3567c6fd2807SJeff Garzik ehc->tries[dev->devno] = ATA_EH_DEV_TRIES; 3568c6fd2807SJeff Garzik 356979a55b72STejun Heo /* collect port action mask recorded in dev actions */ 35709b1e2658STejun Heo ehc->i.action |= ehc->i.dev_action[dev->devno] & 35719b1e2658STejun Heo ~ATA_EH_PERDEV_MASK; 3572f58229f8STejun Heo ehc->i.dev_action[dev->devno] &= ATA_EH_PERDEV_MASK; 357379a55b72STejun Heo 3574c6fd2807SJeff Garzik /* process hotplug request */ 3575c6fd2807SJeff Garzik if (dev->flags & ATA_DFLAG_DETACH) 3576c6fd2807SJeff Garzik ata_eh_detach_dev(dev); 3577c6fd2807SJeff Garzik 357802c05a27STejun Heo /* schedule probe if necessary */ 357902c05a27STejun Heo if (!ata_dev_enabled(dev)) 358002c05a27STejun Heo ata_eh_schedule_probe(dev); 3581c6fd2807SJeff Garzik } 35829b1e2658STejun Heo } 3583c6fd2807SJeff Garzik 3584c6fd2807SJeff Garzik retry: 3585c6fd2807SJeff Garzik rc = 0; 3586c6fd2807SJeff Garzik 3587c6fd2807SJeff Garzik /* if UNLOADING, finish immediately */ 3588c6fd2807SJeff Garzik if (ap->pflags & ATA_PFLAG_UNLOADING) 3589c6fd2807SJeff Garzik goto out; 3590c6fd2807SJeff Garzik 35919b1e2658STejun Heo /* prep for EH */ 35921eca4365STejun Heo ata_for_each_link(link, ap, EDGE) { 35939b1e2658STejun Heo struct ata_eh_context *ehc = &link->eh_context; 35949b1e2658STejun Heo 3595c6fd2807SJeff Garzik /* skip EH if possible. */ 35960260731fSTejun Heo if (ata_eh_skip_recovery(link)) 3597c6fd2807SJeff Garzik ehc->i.action = 0; 3598c6fd2807SJeff Garzik 35991eca4365STejun Heo ata_for_each_dev(dev, link, ALL) 3600f58229f8STejun Heo ehc->classes[dev->devno] = ATA_DEV_UNKNOWN; 36019b1e2658STejun Heo } 3602c6fd2807SJeff Garzik 3603c6fd2807SJeff Garzik /* reset */ 36041eca4365STejun Heo ata_for_each_link(link, ap, EDGE) { 36059b1e2658STejun Heo struct ata_eh_context *ehc = &link->eh_context; 36069b1e2658STejun Heo 3607cf480626STejun Heo if (!(ehc->i.action & ATA_EH_RESET)) 36089b1e2658STejun Heo continue; 36099b1e2658STejun Heo 36109b1e2658STejun Heo rc = ata_eh_reset(link, ata_link_nr_vacant(link), 3611dc98c32cSTejun Heo prereset, softreset, hardreset, postreset); 3612c6fd2807SJeff Garzik if (rc) { 3613a9a79dfeSJoe Perches ata_link_err(link, "reset failed, giving up\n"); 3614c6fd2807SJeff Garzik goto out; 3615c6fd2807SJeff Garzik } 36169b1e2658STejun Heo } 3617c6fd2807SJeff Garzik 361845fabbb7SElias Oltmanns do { 361945fabbb7SElias Oltmanns unsigned long now; 362045fabbb7SElias Oltmanns 362145fabbb7SElias Oltmanns /* 362245fabbb7SElias Oltmanns * clears ATA_EH_PARK in eh_info and resets 362345fabbb7SElias Oltmanns * ap->park_req_pending 362445fabbb7SElias Oltmanns */ 362545fabbb7SElias Oltmanns ata_eh_pull_park_action(ap); 362645fabbb7SElias Oltmanns 362745fabbb7SElias Oltmanns deadline = jiffies; 36281eca4365STejun Heo ata_for_each_link(link, ap, EDGE) { 36291eca4365STejun Heo ata_for_each_dev(dev, link, ALL) { 363045fabbb7SElias Oltmanns struct ata_eh_context *ehc = &link->eh_context; 363145fabbb7SElias Oltmanns unsigned long tmp; 363245fabbb7SElias Oltmanns 36339162c657SHannes Reinecke if (dev->class != ATA_DEV_ATA && 36349162c657SHannes Reinecke dev->class != ATA_DEV_ZAC) 363545fabbb7SElias Oltmanns continue; 363645fabbb7SElias Oltmanns if (!(ehc->i.dev_action[dev->devno] & 363745fabbb7SElias Oltmanns ATA_EH_PARK)) 363845fabbb7SElias Oltmanns continue; 363945fabbb7SElias Oltmanns tmp = dev->unpark_deadline; 364045fabbb7SElias Oltmanns if (time_before(deadline, tmp)) 364145fabbb7SElias Oltmanns deadline = tmp; 364245fabbb7SElias Oltmanns else if (time_before_eq(tmp, jiffies)) 364345fabbb7SElias Oltmanns continue; 364445fabbb7SElias Oltmanns if (ehc->unloaded_mask & (1 << dev->devno)) 364545fabbb7SElias Oltmanns continue; 364645fabbb7SElias Oltmanns 364745fabbb7SElias Oltmanns ata_eh_park_issue_cmd(dev, 1); 364845fabbb7SElias Oltmanns } 364945fabbb7SElias Oltmanns } 365045fabbb7SElias Oltmanns 365145fabbb7SElias Oltmanns now = jiffies; 365245fabbb7SElias Oltmanns if (time_before_eq(deadline, now)) 365345fabbb7SElias Oltmanns break; 365445fabbb7SElias Oltmanns 3655c0c362b6STejun Heo ata_eh_release(ap); 365645fabbb7SElias Oltmanns deadline = wait_for_completion_timeout(&ap->park_req_pending, 365745fabbb7SElias Oltmanns deadline - now); 3658c0c362b6STejun Heo ata_eh_acquire(ap); 365945fabbb7SElias Oltmanns } while (deadline); 36601eca4365STejun Heo ata_for_each_link(link, ap, EDGE) { 36611eca4365STejun Heo ata_for_each_dev(dev, link, ALL) { 366245fabbb7SElias Oltmanns if (!(link->eh_context.unloaded_mask & 366345fabbb7SElias Oltmanns (1 << dev->devno))) 366445fabbb7SElias Oltmanns continue; 366545fabbb7SElias Oltmanns 366645fabbb7SElias Oltmanns ata_eh_park_issue_cmd(dev, 0); 366745fabbb7SElias Oltmanns ata_eh_done(link, dev, ATA_EH_PARK); 366845fabbb7SElias Oltmanns } 366945fabbb7SElias Oltmanns } 367045fabbb7SElias Oltmanns 36719b1e2658STejun Heo /* the rest */ 36726b7ae954STejun Heo nr_fails = 0; 36736b7ae954STejun Heo ata_for_each_link(link, ap, PMP_FIRST) { 36749b1e2658STejun Heo struct ata_eh_context *ehc = &link->eh_context; 36759b1e2658STejun Heo 36766b7ae954STejun Heo if (sata_pmp_attached(ap) && ata_is_host_link(link)) 36776b7ae954STejun Heo goto config_lpm; 36786b7ae954STejun Heo 3679c6fd2807SJeff Garzik /* revalidate existing devices and attach new ones */ 36800260731fSTejun Heo rc = ata_eh_revalidate_and_attach(link, &dev); 3681c6fd2807SJeff Garzik if (rc) 36826b7ae954STejun Heo goto rest_fail; 3683c6fd2807SJeff Garzik 3684633273a3STejun Heo /* if PMP got attached, return, pmp EH will take care of it */ 3685633273a3STejun Heo if (link->device->class == ATA_DEV_PMP) { 3686633273a3STejun Heo ehc->i.action = 0; 3687633273a3STejun Heo return 0; 3688633273a3STejun Heo } 3689633273a3STejun Heo 3690baa1e78aSTejun Heo /* configure transfer mode if necessary */ 3691baa1e78aSTejun Heo if (ehc->i.flags & ATA_EHI_SETMODE) { 36920260731fSTejun Heo rc = ata_set_mode(link, &dev); 36934ae72a1eSTejun Heo if (rc) 36946b7ae954STejun Heo goto rest_fail; 3695baa1e78aSTejun Heo ehc->i.flags &= ~ATA_EHI_SETMODE; 3696c6fd2807SJeff Garzik } 3697c6fd2807SJeff Garzik 369811fc33daSTejun Heo /* If reset has been issued, clear UA to avoid 369911fc33daSTejun Heo * disrupting the current users of the device. 370011fc33daSTejun Heo */ 370111fc33daSTejun Heo if (ehc->i.flags & ATA_EHI_DID_RESET) { 37021eca4365STejun Heo ata_for_each_dev(dev, link, ALL) { 370311fc33daSTejun Heo if (dev->class != ATA_DEV_ATAPI) 370411fc33daSTejun Heo continue; 370511fc33daSTejun Heo rc = atapi_eh_clear_ua(dev); 370611fc33daSTejun Heo if (rc) 37076b7ae954STejun Heo goto rest_fail; 370821334205SAaron Lu if (zpodd_dev_enabled(dev)) 370921334205SAaron Lu zpodd_post_poweron(dev); 371011fc33daSTejun Heo } 371111fc33daSTejun Heo } 371211fc33daSTejun Heo 37136013efd8STejun Heo /* retry flush if necessary */ 37146013efd8STejun Heo ata_for_each_dev(dev, link, ALL) { 37159162c657SHannes Reinecke if (dev->class != ATA_DEV_ATA && 37169162c657SHannes Reinecke dev->class != ATA_DEV_ZAC) 37176013efd8STejun Heo continue; 37186013efd8STejun Heo rc = ata_eh_maybe_retry_flush(dev); 37196013efd8STejun Heo if (rc) 37206b7ae954STejun Heo goto rest_fail; 37216013efd8STejun Heo } 37226013efd8STejun Heo 37236b7ae954STejun Heo config_lpm: 372411fc33daSTejun Heo /* configure link power saving */ 37256b7ae954STejun Heo if (link->lpm_policy != ap->target_lpm_policy) { 37266b7ae954STejun Heo rc = ata_eh_set_lpm(link, ap->target_lpm_policy, &dev); 37276b7ae954STejun Heo if (rc) 37286b7ae954STejun Heo goto rest_fail; 37296b7ae954STejun Heo } 3730ca77329fSKristen Carlson Accardi 37319b1e2658STejun Heo /* this link is okay now */ 37329b1e2658STejun Heo ehc->i.flags = 0; 37339b1e2658STejun Heo continue; 3734c6fd2807SJeff Garzik 37356b7ae954STejun Heo rest_fail: 37366b7ae954STejun Heo nr_fails++; 37376b7ae954STejun Heo if (dev) 37380a2c0f56STejun Heo ata_eh_handle_dev_fail(dev, rc); 3739c6fd2807SJeff Garzik 3740b06ce3e5STejun Heo if (ap->pflags & ATA_PFLAG_FROZEN) { 3741b06ce3e5STejun Heo /* PMP reset requires working host port. 3742b06ce3e5STejun Heo * Can't retry if it's frozen. 3743b06ce3e5STejun Heo */ 3744071f44b1STejun Heo if (sata_pmp_attached(ap)) 3745b06ce3e5STejun Heo goto out; 37469b1e2658STejun Heo break; 37479b1e2658STejun Heo } 3748b06ce3e5STejun Heo } 37499b1e2658STejun Heo 37506b7ae954STejun Heo if (nr_fails) 3751c6fd2807SJeff Garzik goto retry; 3752c6fd2807SJeff Garzik 3753c6fd2807SJeff Garzik out: 37549b1e2658STejun Heo if (rc && r_failed_link) 37559b1e2658STejun Heo *r_failed_link = link; 3756c6fd2807SJeff Garzik 3757c6fd2807SJeff Garzik return rc; 3758c6fd2807SJeff Garzik } 3759c6fd2807SJeff Garzik 3760c6fd2807SJeff Garzik /** 3761c6fd2807SJeff Garzik * ata_eh_finish - finish up EH 3762c6fd2807SJeff Garzik * @ap: host port to finish EH for 3763c6fd2807SJeff Garzik * 3764c6fd2807SJeff Garzik * Recovery is complete. Clean up EH states and retry or finish 3765c6fd2807SJeff Garzik * failed qcs. 3766c6fd2807SJeff Garzik * 3767c6fd2807SJeff Garzik * LOCKING: 3768c6fd2807SJeff Garzik * None. 3769c6fd2807SJeff Garzik */ 3770fb7fd614STejun Heo void ata_eh_finish(struct ata_port *ap) 3771c6fd2807SJeff Garzik { 3772258c4e5cSJens Axboe struct ata_queued_cmd *qc; 3773c6fd2807SJeff Garzik int tag; 3774c6fd2807SJeff Garzik 3775c6fd2807SJeff Garzik /* retry or finish qcs */ 3776258c4e5cSJens Axboe ata_qc_for_each_raw(ap, qc, tag) { 3777c6fd2807SJeff Garzik if (!(qc->flags & ATA_QCFLAG_FAILED)) 3778c6fd2807SJeff Garzik continue; 3779c6fd2807SJeff Garzik 3780c6fd2807SJeff Garzik if (qc->err_mask) { 3781c6fd2807SJeff Garzik /* FIXME: Once EH migration is complete, 3782c6fd2807SJeff Garzik * generate sense data in this function, 3783c6fd2807SJeff Garzik * considering both err_mask and tf. 3784c6fd2807SJeff Garzik */ 378503faab78STejun Heo if (qc->flags & ATA_QCFLAG_RETRY) 3786c6fd2807SJeff Garzik ata_eh_qc_retry(qc); 378703faab78STejun Heo else 378803faab78STejun Heo ata_eh_qc_complete(qc); 3789c6fd2807SJeff Garzik } else { 3790c6fd2807SJeff Garzik if (qc->flags & ATA_QCFLAG_SENSE_VALID) { 3791c6fd2807SJeff Garzik ata_eh_qc_complete(qc); 3792c6fd2807SJeff Garzik } else { 3793c6fd2807SJeff Garzik /* feed zero TF to sense generation */ 3794c6fd2807SJeff Garzik memset(&qc->result_tf, 0, sizeof(qc->result_tf)); 3795c6fd2807SJeff Garzik ata_eh_qc_retry(qc); 3796c6fd2807SJeff Garzik } 3797c6fd2807SJeff Garzik } 3798c6fd2807SJeff Garzik } 3799da917d69STejun Heo 3800da917d69STejun Heo /* make sure nr_active_links is zero after EH */ 3801da917d69STejun Heo WARN_ON(ap->nr_active_links); 3802da917d69STejun Heo ap->nr_active_links = 0; 3803c6fd2807SJeff Garzik } 3804c6fd2807SJeff Garzik 3805c6fd2807SJeff Garzik /** 3806c6fd2807SJeff Garzik * ata_do_eh - do standard error handling 3807c6fd2807SJeff Garzik * @ap: host port to handle error for 3808a1efdabaSTejun Heo * 3809c6fd2807SJeff Garzik * @prereset: prereset method (can be NULL) 3810c6fd2807SJeff Garzik * @softreset: softreset method (can be NULL) 3811c6fd2807SJeff Garzik * @hardreset: hardreset method (can be NULL) 3812c6fd2807SJeff Garzik * @postreset: postreset method (can be NULL) 3813c6fd2807SJeff Garzik * 3814c6fd2807SJeff Garzik * Perform standard error handling sequence. 3815c6fd2807SJeff Garzik * 3816c6fd2807SJeff Garzik * LOCKING: 3817c6fd2807SJeff Garzik * Kernel thread context (may sleep). 3818c6fd2807SJeff Garzik */ 3819c6fd2807SJeff Garzik void ata_do_eh(struct ata_port *ap, ata_prereset_fn_t prereset, 3820c6fd2807SJeff Garzik ata_reset_fn_t softreset, ata_reset_fn_t hardreset, 3821c6fd2807SJeff Garzik ata_postreset_fn_t postreset) 3822c6fd2807SJeff Garzik { 38239b1e2658STejun Heo struct ata_device *dev; 38249b1e2658STejun Heo int rc; 38259b1e2658STejun Heo 38269b1e2658STejun Heo ata_eh_autopsy(ap); 38279b1e2658STejun Heo ata_eh_report(ap); 38289b1e2658STejun Heo 38299b1e2658STejun Heo rc = ata_eh_recover(ap, prereset, softreset, hardreset, postreset, 38309b1e2658STejun Heo NULL); 38319b1e2658STejun Heo if (rc) { 38321eca4365STejun Heo ata_for_each_dev(dev, &ap->link, ALL) 38339b1e2658STejun Heo ata_dev_disable(dev); 38349b1e2658STejun Heo } 38359b1e2658STejun Heo 3836c6fd2807SJeff Garzik ata_eh_finish(ap); 3837c6fd2807SJeff Garzik } 3838c6fd2807SJeff Garzik 3839a1efdabaSTejun Heo /** 3840a1efdabaSTejun Heo * ata_std_error_handler - standard error handler 3841a1efdabaSTejun Heo * @ap: host port to handle error for 3842a1efdabaSTejun Heo * 3843a1efdabaSTejun Heo * Standard error handler 3844a1efdabaSTejun Heo * 3845a1efdabaSTejun Heo * LOCKING: 3846a1efdabaSTejun Heo * Kernel thread context (may sleep). 3847a1efdabaSTejun Heo */ 3848a1efdabaSTejun Heo void ata_std_error_handler(struct ata_port *ap) 3849a1efdabaSTejun Heo { 3850a1efdabaSTejun Heo struct ata_port_operations *ops = ap->ops; 3851a1efdabaSTejun Heo ata_reset_fn_t hardreset = ops->hardreset; 3852a1efdabaSTejun Heo 385357c9efdfSTejun Heo /* ignore built-in hardreset if SCR access is not available */ 3854fe06e5f9STejun Heo if (hardreset == sata_std_hardreset && !sata_scr_valid(&ap->link)) 3855a1efdabaSTejun Heo hardreset = NULL; 3856a1efdabaSTejun Heo 3857a1efdabaSTejun Heo ata_do_eh(ap, ops->prereset, ops->softreset, hardreset, ops->postreset); 3858a1efdabaSTejun Heo } 3859a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_std_error_handler); 3860a1efdabaSTejun Heo 38616ffa01d8STejun Heo #ifdef CONFIG_PM 3862c6fd2807SJeff Garzik /** 3863c6fd2807SJeff Garzik * ata_eh_handle_port_suspend - perform port suspend operation 3864c6fd2807SJeff Garzik * @ap: port to suspend 3865c6fd2807SJeff Garzik * 3866c6fd2807SJeff Garzik * Suspend @ap. 3867c6fd2807SJeff Garzik * 3868c6fd2807SJeff Garzik * LOCKING: 3869c6fd2807SJeff Garzik * Kernel thread context (may sleep). 3870c6fd2807SJeff Garzik */ 3871c6fd2807SJeff Garzik static void ata_eh_handle_port_suspend(struct ata_port *ap) 3872c6fd2807SJeff Garzik { 3873c6fd2807SJeff Garzik unsigned long flags; 3874c6fd2807SJeff Garzik int rc = 0; 38753dc67440SAaron Lu struct ata_device *dev; 3876c6fd2807SJeff Garzik 3877c6fd2807SJeff Garzik /* are we suspending? */ 3878c6fd2807SJeff Garzik spin_lock_irqsave(ap->lock, flags); 3879c6fd2807SJeff Garzik if (!(ap->pflags & ATA_PFLAG_PM_PENDING) || 3880a7ff60dbSAaron Lu ap->pm_mesg.event & PM_EVENT_RESUME) { 3881c6fd2807SJeff Garzik spin_unlock_irqrestore(ap->lock, flags); 3882c6fd2807SJeff Garzik return; 3883c6fd2807SJeff Garzik } 3884c6fd2807SJeff Garzik spin_unlock_irqrestore(ap->lock, flags); 3885c6fd2807SJeff Garzik 3886c6fd2807SJeff Garzik WARN_ON(ap->pflags & ATA_PFLAG_SUSPENDED); 3887c6fd2807SJeff Garzik 38883dc67440SAaron Lu /* 38893dc67440SAaron Lu * If we have a ZPODD attached, check its zero 38903dc67440SAaron Lu * power ready status before the port is frozen. 3891a7ff60dbSAaron Lu * Only needed for runtime suspend. 38923dc67440SAaron Lu */ 3893a7ff60dbSAaron Lu if (PMSG_IS_AUTO(ap->pm_mesg)) { 38943dc67440SAaron Lu ata_for_each_dev(dev, &ap->link, ENABLED) { 38953dc67440SAaron Lu if (zpodd_dev_enabled(dev)) 38963dc67440SAaron Lu zpodd_on_suspend(dev); 38973dc67440SAaron Lu } 3898a7ff60dbSAaron Lu } 38993dc67440SAaron Lu 3900c6fd2807SJeff Garzik /* suspend */ 3901c6fd2807SJeff Garzik ata_eh_freeze_port(ap); 3902c6fd2807SJeff Garzik 3903c6fd2807SJeff Garzik if (ap->ops->port_suspend) 3904c6fd2807SJeff Garzik rc = ap->ops->port_suspend(ap, ap->pm_mesg); 3905c6fd2807SJeff Garzik 3906a7ff60dbSAaron Lu ata_acpi_set_state(ap, ap->pm_mesg); 39072a7b02eaSSergey Shtylyov 3908bc6e7c4bSDan Williams /* update the flags */ 3909c6fd2807SJeff Garzik spin_lock_irqsave(ap->lock, flags); 3910c6fd2807SJeff Garzik 3911c6fd2807SJeff Garzik ap->pflags &= ~ATA_PFLAG_PM_PENDING; 3912c6fd2807SJeff Garzik if (rc == 0) 3913c6fd2807SJeff Garzik ap->pflags |= ATA_PFLAG_SUSPENDED; 391464578a3dSTejun Heo else if (ap->pflags & ATA_PFLAG_FROZEN) 3915c6fd2807SJeff Garzik ata_port_schedule_eh(ap); 3916c6fd2807SJeff Garzik 3917c6fd2807SJeff Garzik spin_unlock_irqrestore(ap->lock, flags); 3918c6fd2807SJeff Garzik 3919c6fd2807SJeff Garzik return; 3920c6fd2807SJeff Garzik } 3921c6fd2807SJeff Garzik 3922c6fd2807SJeff Garzik /** 3923c6fd2807SJeff Garzik * ata_eh_handle_port_resume - perform port resume operation 3924c6fd2807SJeff Garzik * @ap: port to resume 3925c6fd2807SJeff Garzik * 3926c6fd2807SJeff Garzik * Resume @ap. 3927c6fd2807SJeff Garzik * 3928c6fd2807SJeff Garzik * LOCKING: 3929c6fd2807SJeff Garzik * Kernel thread context (may sleep). 3930c6fd2807SJeff Garzik */ 3931c6fd2807SJeff Garzik static void ata_eh_handle_port_resume(struct ata_port *ap) 3932c6fd2807SJeff Garzik { 39336f9c1ea2STejun Heo struct ata_link *link; 39346f9c1ea2STejun Heo struct ata_device *dev; 3935c6fd2807SJeff Garzik unsigned long flags; 3936c6fd2807SJeff Garzik 3937c6fd2807SJeff Garzik /* are we resuming? */ 3938c6fd2807SJeff Garzik spin_lock_irqsave(ap->lock, flags); 3939c6fd2807SJeff Garzik if (!(ap->pflags & ATA_PFLAG_PM_PENDING) || 3940a7ff60dbSAaron Lu !(ap->pm_mesg.event & PM_EVENT_RESUME)) { 3941c6fd2807SJeff Garzik spin_unlock_irqrestore(ap->lock, flags); 3942c6fd2807SJeff Garzik return; 3943c6fd2807SJeff Garzik } 3944c6fd2807SJeff Garzik spin_unlock_irqrestore(ap->lock, flags); 3945c6fd2807SJeff Garzik 39469666f400STejun Heo WARN_ON(!(ap->pflags & ATA_PFLAG_SUSPENDED)); 3947c6fd2807SJeff Garzik 39486f9c1ea2STejun Heo /* 39496f9c1ea2STejun Heo * Error timestamps are in jiffies which doesn't run while 39506f9c1ea2STejun Heo * suspended and PHY events during resume isn't too uncommon. 39516f9c1ea2STejun Heo * When the two are combined, it can lead to unnecessary speed 39526f9c1ea2STejun Heo * downs if the machine is suspended and resumed repeatedly. 39536f9c1ea2STejun Heo * Clear error history. 39546f9c1ea2STejun Heo */ 39556f9c1ea2STejun Heo ata_for_each_link(link, ap, HOST_FIRST) 39566f9c1ea2STejun Heo ata_for_each_dev(dev, link, ALL) 39576f9c1ea2STejun Heo ata_ering_clear(&dev->ering); 39586f9c1ea2STejun Heo 3959a7ff60dbSAaron Lu ata_acpi_set_state(ap, ap->pm_mesg); 3960bd3adca5SShaohua Li 3961c6fd2807SJeff Garzik if (ap->ops->port_resume) 3962ae867937SKefeng Wang ap->ops->port_resume(ap); 3963c6fd2807SJeff Garzik 39646746544cSTejun Heo /* tell ACPI that we're resuming */ 39656746544cSTejun Heo ata_acpi_on_resume(ap); 39666746544cSTejun Heo 3967bc6e7c4bSDan Williams /* update the flags */ 3968c6fd2807SJeff Garzik spin_lock_irqsave(ap->lock, flags); 3969c6fd2807SJeff Garzik ap->pflags &= ~(ATA_PFLAG_PM_PENDING | ATA_PFLAG_SUSPENDED); 3970c6fd2807SJeff Garzik spin_unlock_irqrestore(ap->lock, flags); 3971c6fd2807SJeff Garzik } 39726ffa01d8STejun Heo #endif /* CONFIG_PM */ 3973