1c6fd2807SJeff Garzik /* 2c6fd2807SJeff Garzik * libata-eh.c - libata error handling 3c6fd2807SJeff Garzik * 4c6fd2807SJeff Garzik * Maintained by: Jeff Garzik <jgarzik@pobox.com> 5c6fd2807SJeff Garzik * Please ALWAYS copy linux-ide@vger.kernel.org 6c6fd2807SJeff Garzik * on emails. 7c6fd2807SJeff Garzik * 8c6fd2807SJeff Garzik * Copyright 2006 Tejun Heo <htejun@gmail.com> 9c6fd2807SJeff Garzik * 10c6fd2807SJeff Garzik * 11c6fd2807SJeff Garzik * This program is free software; you can redistribute it and/or 12c6fd2807SJeff Garzik * modify it under the terms of the GNU General Public License as 13c6fd2807SJeff Garzik * published by the Free Software Foundation; either version 2, or 14c6fd2807SJeff Garzik * (at your option) any later version. 15c6fd2807SJeff Garzik * 16c6fd2807SJeff Garzik * This program is distributed in the hope that it will be useful, 17c6fd2807SJeff Garzik * but WITHOUT ANY WARRANTY; without even the implied warranty of 18c6fd2807SJeff Garzik * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 19c6fd2807SJeff Garzik * General Public License for more details. 20c6fd2807SJeff Garzik * 21c6fd2807SJeff Garzik * You should have received a copy of the GNU General Public License 22c6fd2807SJeff Garzik * along with this program; see the file COPYING. If not, write to 23c6fd2807SJeff Garzik * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, 24c6fd2807SJeff Garzik * USA. 25c6fd2807SJeff Garzik * 26c6fd2807SJeff Garzik * 27c6fd2807SJeff Garzik * libata documentation is available via 'make {ps|pdf}docs', 28c6fd2807SJeff Garzik * as Documentation/DocBook/libata.* 29c6fd2807SJeff Garzik * 30c6fd2807SJeff Garzik * Hardware documentation available from http://www.t13.org/ and 31c6fd2807SJeff Garzik * http://www.sata-io.org/ 32c6fd2807SJeff Garzik * 33c6fd2807SJeff Garzik */ 34c6fd2807SJeff Garzik 35c6fd2807SJeff Garzik #include <linux/kernel.h> 36242f9dcbSJens Axboe #include <linux/blkdev.h> 3738789fdaSPaul Gortmaker #include <linux/export.h> 382855568bSJeff Garzik #include <linux/pci.h> 39c6fd2807SJeff Garzik #include <scsi/scsi.h> 40c6fd2807SJeff Garzik #include <scsi/scsi_host.h> 41c6fd2807SJeff Garzik #include <scsi/scsi_eh.h> 42c6fd2807SJeff Garzik #include <scsi/scsi_device.h> 43c6fd2807SJeff Garzik #include <scsi/scsi_cmnd.h> 446521148cSRobert Hancock #include <scsi/scsi_dbg.h> 45c6fd2807SJeff Garzik #include "../scsi/scsi_transport_api.h" 46c6fd2807SJeff Garzik 47c6fd2807SJeff Garzik #include <linux/libata.h> 48c6fd2807SJeff Garzik 49c6fd2807SJeff Garzik #include "libata.h" 50c6fd2807SJeff Garzik 517d47e8d4STejun Heo enum { 523884f7b0STejun Heo /* speed down verdicts */ 537d47e8d4STejun Heo ATA_EH_SPDN_NCQ_OFF = (1 << 0), 547d47e8d4STejun Heo ATA_EH_SPDN_SPEED_DOWN = (1 << 1), 557d47e8d4STejun Heo ATA_EH_SPDN_FALLBACK_TO_PIO = (1 << 2), 5676326ac1STejun Heo ATA_EH_SPDN_KEEP_ERRORS = (1 << 3), 573884f7b0STejun Heo 583884f7b0STejun Heo /* error flags */ 593884f7b0STejun Heo ATA_EFLAG_IS_IO = (1 << 0), 6076326ac1STejun Heo ATA_EFLAG_DUBIOUS_XFER = (1 << 1), 61d9027470SGwendal Grignou ATA_EFLAG_OLD_ER = (1 << 31), 623884f7b0STejun Heo 633884f7b0STejun Heo /* error categories */ 643884f7b0STejun Heo ATA_ECAT_NONE = 0, 653884f7b0STejun Heo ATA_ECAT_ATA_BUS = 1, 663884f7b0STejun Heo ATA_ECAT_TOUT_HSM = 2, 673884f7b0STejun Heo ATA_ECAT_UNK_DEV = 3, 6875f9cafcSTejun Heo ATA_ECAT_DUBIOUS_NONE = 4, 6975f9cafcSTejun Heo ATA_ECAT_DUBIOUS_ATA_BUS = 5, 7075f9cafcSTejun Heo ATA_ECAT_DUBIOUS_TOUT_HSM = 6, 7175f9cafcSTejun Heo ATA_ECAT_DUBIOUS_UNK_DEV = 7, 7275f9cafcSTejun Heo ATA_ECAT_NR = 8, 737d47e8d4STejun Heo 7487fbc5a0STejun Heo ATA_EH_CMD_DFL_TIMEOUT = 5000, 7587fbc5a0STejun Heo 760a2c0f56STejun Heo /* always put at least this amount of time between resets */ 770a2c0f56STejun Heo ATA_EH_RESET_COOL_DOWN = 5000, 780a2c0f56STejun Heo 79341c2c95STejun Heo /* Waiting in ->prereset can never be reliable. It's 80341c2c95STejun Heo * sometimes nice to wait there but it can't be depended upon; 81341c2c95STejun Heo * otherwise, we wouldn't be resetting. Just give it enough 82341c2c95STejun Heo * time for most drives to spin up. 8331daabdaSTejun Heo */ 84341c2c95STejun Heo ATA_EH_PRERESET_TIMEOUT = 10000, 85341c2c95STejun Heo ATA_EH_FASTDRAIN_INTERVAL = 3000, 8611fc33daSTejun Heo 8711fc33daSTejun Heo ATA_EH_UA_TRIES = 5, 88c2c7a89cSTejun Heo 89c2c7a89cSTejun Heo /* probe speed down parameters, see ata_eh_schedule_probe() */ 90c2c7a89cSTejun Heo ATA_EH_PROBE_TRIAL_INTERVAL = 60000, /* 1 min */ 91c2c7a89cSTejun Heo ATA_EH_PROBE_TRIALS = 2, 9231daabdaSTejun Heo }; 9331daabdaSTejun Heo 9431daabdaSTejun Heo /* The following table determines how we sequence resets. Each entry 9531daabdaSTejun Heo * represents timeout for that try. The first try can be soft or 9631daabdaSTejun Heo * hardreset. All others are hardreset if available. In most cases 9731daabdaSTejun Heo * the first reset w/ 10sec timeout should succeed. Following entries 9831daabdaSTejun Heo * are mostly for error handling, hotplug and retarded devices. 9931daabdaSTejun Heo */ 10031daabdaSTejun Heo static const unsigned long ata_eh_reset_timeouts[] = { 101341c2c95STejun Heo 10000, /* most drives spin up by 10sec */ 102341c2c95STejun Heo 10000, /* > 99% working drives spin up before 20sec */ 103341c2c95STejun Heo 35000, /* give > 30 secs of idleness for retarded devices */ 104341c2c95STejun Heo 5000, /* and sweet one last chance */ 105d8af0eb6STejun Heo ULONG_MAX, /* > 1 min has elapsed, give up */ 10631daabdaSTejun Heo }; 10731daabdaSTejun Heo 10887fbc5a0STejun Heo static const unsigned long ata_eh_identify_timeouts[] = { 10987fbc5a0STejun Heo 5000, /* covers > 99% of successes and not too boring on failures */ 11087fbc5a0STejun Heo 10000, /* combined time till here is enough even for media access */ 11187fbc5a0STejun Heo 30000, /* for true idiots */ 11287fbc5a0STejun Heo ULONG_MAX, 11387fbc5a0STejun Heo }; 11487fbc5a0STejun Heo 1156013efd8STejun Heo static const unsigned long ata_eh_flush_timeouts[] = { 1166013efd8STejun Heo 15000, /* be generous with flush */ 1176013efd8STejun Heo 15000, /* ditto */ 1186013efd8STejun Heo 30000, /* and even more generous */ 1196013efd8STejun Heo ULONG_MAX, 1206013efd8STejun Heo }; 1216013efd8STejun Heo 12287fbc5a0STejun Heo static const unsigned long ata_eh_other_timeouts[] = { 12387fbc5a0STejun Heo 5000, /* same rationale as identify timeout */ 12487fbc5a0STejun Heo 10000, /* ditto */ 12587fbc5a0STejun Heo /* but no merciful 30sec for other commands, it just isn't worth it */ 12687fbc5a0STejun Heo ULONG_MAX, 12787fbc5a0STejun Heo }; 12887fbc5a0STejun Heo 12987fbc5a0STejun Heo struct ata_eh_cmd_timeout_ent { 13087fbc5a0STejun Heo const u8 *commands; 13187fbc5a0STejun Heo const unsigned long *timeouts; 13287fbc5a0STejun Heo }; 13387fbc5a0STejun Heo 13487fbc5a0STejun Heo /* The following table determines timeouts to use for EH internal 13587fbc5a0STejun Heo * commands. Each table entry is a command class and matches the 13687fbc5a0STejun Heo * commands the entry applies to and the timeout table to use. 13787fbc5a0STejun Heo * 13887fbc5a0STejun Heo * On the retry after a command timed out, the next timeout value from 13987fbc5a0STejun Heo * the table is used. If the table doesn't contain further entries, 14087fbc5a0STejun Heo * the last value is used. 14187fbc5a0STejun Heo * 14287fbc5a0STejun Heo * ehc->cmd_timeout_idx keeps track of which timeout to use per 14387fbc5a0STejun Heo * command class, so if SET_FEATURES times out on the first try, the 14487fbc5a0STejun Heo * next try will use the second timeout value only for that class. 14587fbc5a0STejun Heo */ 14687fbc5a0STejun Heo #define CMDS(cmds...) (const u8 []){ cmds, 0 } 14787fbc5a0STejun Heo static const struct ata_eh_cmd_timeout_ent 14887fbc5a0STejun Heo ata_eh_cmd_timeout_table[ATA_EH_CMD_TIMEOUT_TABLE_SIZE] = { 14987fbc5a0STejun Heo { .commands = CMDS(ATA_CMD_ID_ATA, ATA_CMD_ID_ATAPI), 15087fbc5a0STejun Heo .timeouts = ata_eh_identify_timeouts, }, 15187fbc5a0STejun Heo { .commands = CMDS(ATA_CMD_READ_NATIVE_MAX, ATA_CMD_READ_NATIVE_MAX_EXT), 15287fbc5a0STejun Heo .timeouts = ata_eh_other_timeouts, }, 15387fbc5a0STejun Heo { .commands = CMDS(ATA_CMD_SET_MAX, ATA_CMD_SET_MAX_EXT), 15487fbc5a0STejun Heo .timeouts = ata_eh_other_timeouts, }, 15587fbc5a0STejun Heo { .commands = CMDS(ATA_CMD_SET_FEATURES), 15687fbc5a0STejun Heo .timeouts = ata_eh_other_timeouts, }, 15787fbc5a0STejun Heo { .commands = CMDS(ATA_CMD_INIT_DEV_PARAMS), 15887fbc5a0STejun Heo .timeouts = ata_eh_other_timeouts, }, 1596013efd8STejun Heo { .commands = CMDS(ATA_CMD_FLUSH, ATA_CMD_FLUSH_EXT), 1606013efd8STejun Heo .timeouts = ata_eh_flush_timeouts }, 16187fbc5a0STejun Heo }; 16287fbc5a0STejun Heo #undef CMDS 16387fbc5a0STejun Heo 164c6fd2807SJeff Garzik static void __ata_port_freeze(struct ata_port *ap); 1656ffa01d8STejun Heo #ifdef CONFIG_PM 166c6fd2807SJeff Garzik static void ata_eh_handle_port_suspend(struct ata_port *ap); 167c6fd2807SJeff Garzik static void ata_eh_handle_port_resume(struct ata_port *ap); 1686ffa01d8STejun Heo #else /* CONFIG_PM */ 1696ffa01d8STejun Heo static void ata_eh_handle_port_suspend(struct ata_port *ap) 1706ffa01d8STejun Heo { } 1716ffa01d8STejun Heo 1726ffa01d8STejun Heo static void ata_eh_handle_port_resume(struct ata_port *ap) 1736ffa01d8STejun Heo { } 1746ffa01d8STejun Heo #endif /* CONFIG_PM */ 175c6fd2807SJeff Garzik 176b64bbc39STejun Heo static void __ata_ehi_pushv_desc(struct ata_eh_info *ehi, const char *fmt, 177b64bbc39STejun Heo va_list args) 178b64bbc39STejun Heo { 179b64bbc39STejun Heo ehi->desc_len += vscnprintf(ehi->desc + ehi->desc_len, 180b64bbc39STejun Heo ATA_EH_DESC_LEN - ehi->desc_len, 181b64bbc39STejun Heo fmt, args); 182b64bbc39STejun Heo } 183b64bbc39STejun Heo 184b64bbc39STejun Heo /** 185b64bbc39STejun Heo * __ata_ehi_push_desc - push error description without adding separator 186b64bbc39STejun Heo * @ehi: target EHI 187b64bbc39STejun Heo * @fmt: printf format string 188b64bbc39STejun Heo * 189b64bbc39STejun Heo * Format string according to @fmt and append it to @ehi->desc. 190b64bbc39STejun Heo * 191b64bbc39STejun Heo * LOCKING: 192b64bbc39STejun Heo * spin_lock_irqsave(host lock) 193b64bbc39STejun Heo */ 194b64bbc39STejun Heo void __ata_ehi_push_desc(struct ata_eh_info *ehi, const char *fmt, ...) 195b64bbc39STejun Heo { 196b64bbc39STejun Heo va_list args; 197b64bbc39STejun Heo 198b64bbc39STejun Heo va_start(args, fmt); 199b64bbc39STejun Heo __ata_ehi_pushv_desc(ehi, fmt, args); 200b64bbc39STejun Heo va_end(args); 201b64bbc39STejun Heo } 202b64bbc39STejun Heo 203b64bbc39STejun Heo /** 204b64bbc39STejun Heo * ata_ehi_push_desc - push error description with separator 205b64bbc39STejun Heo * @ehi: target EHI 206b64bbc39STejun Heo * @fmt: printf format string 207b64bbc39STejun Heo * 208b64bbc39STejun Heo * Format string according to @fmt and append it to @ehi->desc. 209b64bbc39STejun Heo * If @ehi->desc is not empty, ", " is added in-between. 210b64bbc39STejun Heo * 211b64bbc39STejun Heo * LOCKING: 212b64bbc39STejun Heo * spin_lock_irqsave(host lock) 213b64bbc39STejun Heo */ 214b64bbc39STejun Heo void ata_ehi_push_desc(struct ata_eh_info *ehi, const char *fmt, ...) 215b64bbc39STejun Heo { 216b64bbc39STejun Heo va_list args; 217b64bbc39STejun Heo 218b64bbc39STejun Heo if (ehi->desc_len) 219b64bbc39STejun Heo __ata_ehi_push_desc(ehi, ", "); 220b64bbc39STejun Heo 221b64bbc39STejun Heo va_start(args, fmt); 222b64bbc39STejun Heo __ata_ehi_pushv_desc(ehi, fmt, args); 223b64bbc39STejun Heo va_end(args); 224b64bbc39STejun Heo } 225b64bbc39STejun Heo 226b64bbc39STejun Heo /** 227b64bbc39STejun Heo * ata_ehi_clear_desc - clean error description 228b64bbc39STejun Heo * @ehi: target EHI 229b64bbc39STejun Heo * 230b64bbc39STejun Heo * Clear @ehi->desc. 231b64bbc39STejun Heo * 232b64bbc39STejun Heo * LOCKING: 233b64bbc39STejun Heo * spin_lock_irqsave(host lock) 234b64bbc39STejun Heo */ 235b64bbc39STejun Heo void ata_ehi_clear_desc(struct ata_eh_info *ehi) 236b64bbc39STejun Heo { 237b64bbc39STejun Heo ehi->desc[0] = '\0'; 238b64bbc39STejun Heo ehi->desc_len = 0; 239b64bbc39STejun Heo } 240b64bbc39STejun Heo 241cbcdd875STejun Heo /** 242cbcdd875STejun Heo * ata_port_desc - append port description 243cbcdd875STejun Heo * @ap: target ATA port 244cbcdd875STejun Heo * @fmt: printf format string 245cbcdd875STejun Heo * 246cbcdd875STejun Heo * Format string according to @fmt and append it to port 247cbcdd875STejun Heo * description. If port description is not empty, " " is added 248cbcdd875STejun Heo * in-between. This function is to be used while initializing 249cbcdd875STejun Heo * ata_host. The description is printed on host registration. 250cbcdd875STejun Heo * 251cbcdd875STejun Heo * LOCKING: 252cbcdd875STejun Heo * None. 253cbcdd875STejun Heo */ 254cbcdd875STejun Heo void ata_port_desc(struct ata_port *ap, const char *fmt, ...) 255cbcdd875STejun Heo { 256cbcdd875STejun Heo va_list args; 257cbcdd875STejun Heo 258cbcdd875STejun Heo WARN_ON(!(ap->pflags & ATA_PFLAG_INITIALIZING)); 259cbcdd875STejun Heo 260cbcdd875STejun Heo if (ap->link.eh_info.desc_len) 261cbcdd875STejun Heo __ata_ehi_push_desc(&ap->link.eh_info, " "); 262cbcdd875STejun Heo 263cbcdd875STejun Heo va_start(args, fmt); 264cbcdd875STejun Heo __ata_ehi_pushv_desc(&ap->link.eh_info, fmt, args); 265cbcdd875STejun Heo va_end(args); 266cbcdd875STejun Heo } 267cbcdd875STejun Heo 268cbcdd875STejun Heo #ifdef CONFIG_PCI 269cbcdd875STejun Heo 270cbcdd875STejun Heo /** 271cbcdd875STejun Heo * ata_port_pbar_desc - append PCI BAR description 272cbcdd875STejun Heo * @ap: target ATA port 273cbcdd875STejun Heo * @bar: target PCI BAR 274cbcdd875STejun Heo * @offset: offset into PCI BAR 275cbcdd875STejun Heo * @name: name of the area 276cbcdd875STejun Heo * 277cbcdd875STejun Heo * If @offset is negative, this function formats a string which 278cbcdd875STejun Heo * contains the name, address, size and type of the BAR and 279cbcdd875STejun Heo * appends it to the port description. If @offset is zero or 280cbcdd875STejun Heo * positive, only name and offsetted address is appended. 281cbcdd875STejun Heo * 282cbcdd875STejun Heo * LOCKING: 283cbcdd875STejun Heo * None. 284cbcdd875STejun Heo */ 285cbcdd875STejun Heo void ata_port_pbar_desc(struct ata_port *ap, int bar, ssize_t offset, 286cbcdd875STejun Heo const char *name) 287cbcdd875STejun Heo { 288cbcdd875STejun Heo struct pci_dev *pdev = to_pci_dev(ap->host->dev); 289cbcdd875STejun Heo char *type = ""; 290cbcdd875STejun Heo unsigned long long start, len; 291cbcdd875STejun Heo 292cbcdd875STejun Heo if (pci_resource_flags(pdev, bar) & IORESOURCE_MEM) 293cbcdd875STejun Heo type = "m"; 294cbcdd875STejun Heo else if (pci_resource_flags(pdev, bar) & IORESOURCE_IO) 295cbcdd875STejun Heo type = "i"; 296cbcdd875STejun Heo 297cbcdd875STejun Heo start = (unsigned long long)pci_resource_start(pdev, bar); 298cbcdd875STejun Heo len = (unsigned long long)pci_resource_len(pdev, bar); 299cbcdd875STejun Heo 300cbcdd875STejun Heo if (offset < 0) 301cbcdd875STejun Heo ata_port_desc(ap, "%s %s%llu@0x%llx", name, type, len, start); 302cbcdd875STejun Heo else 303e6a73ab1SAndrew Morton ata_port_desc(ap, "%s 0x%llx", name, 304e6a73ab1SAndrew Morton start + (unsigned long long)offset); 305cbcdd875STejun Heo } 306cbcdd875STejun Heo 307cbcdd875STejun Heo #endif /* CONFIG_PCI */ 308cbcdd875STejun Heo 30987fbc5a0STejun Heo static int ata_lookup_timeout_table(u8 cmd) 31087fbc5a0STejun Heo { 31187fbc5a0STejun Heo int i; 31287fbc5a0STejun Heo 31387fbc5a0STejun Heo for (i = 0; i < ATA_EH_CMD_TIMEOUT_TABLE_SIZE; i++) { 31487fbc5a0STejun Heo const u8 *cur; 31587fbc5a0STejun Heo 31687fbc5a0STejun Heo for (cur = ata_eh_cmd_timeout_table[i].commands; *cur; cur++) 31787fbc5a0STejun Heo if (*cur == cmd) 31887fbc5a0STejun Heo return i; 31987fbc5a0STejun Heo } 32087fbc5a0STejun Heo 32187fbc5a0STejun Heo return -1; 32287fbc5a0STejun Heo } 32387fbc5a0STejun Heo 32487fbc5a0STejun Heo /** 32587fbc5a0STejun Heo * ata_internal_cmd_timeout - determine timeout for an internal command 32687fbc5a0STejun Heo * @dev: target device 32787fbc5a0STejun Heo * @cmd: internal command to be issued 32887fbc5a0STejun Heo * 32987fbc5a0STejun Heo * Determine timeout for internal command @cmd for @dev. 33087fbc5a0STejun Heo * 33187fbc5a0STejun Heo * LOCKING: 33287fbc5a0STejun Heo * EH context. 33387fbc5a0STejun Heo * 33487fbc5a0STejun Heo * RETURNS: 33587fbc5a0STejun Heo * Determined timeout. 33687fbc5a0STejun Heo */ 33787fbc5a0STejun Heo unsigned long ata_internal_cmd_timeout(struct ata_device *dev, u8 cmd) 33887fbc5a0STejun Heo { 33987fbc5a0STejun Heo struct ata_eh_context *ehc = &dev->link->eh_context; 34087fbc5a0STejun Heo int ent = ata_lookup_timeout_table(cmd); 34187fbc5a0STejun Heo int idx; 34287fbc5a0STejun Heo 34387fbc5a0STejun Heo if (ent < 0) 34487fbc5a0STejun Heo return ATA_EH_CMD_DFL_TIMEOUT; 34587fbc5a0STejun Heo 34687fbc5a0STejun Heo idx = ehc->cmd_timeout_idx[dev->devno][ent]; 34787fbc5a0STejun Heo return ata_eh_cmd_timeout_table[ent].timeouts[idx]; 34887fbc5a0STejun Heo } 34987fbc5a0STejun Heo 35087fbc5a0STejun Heo /** 35187fbc5a0STejun Heo * ata_internal_cmd_timed_out - notification for internal command timeout 35287fbc5a0STejun Heo * @dev: target device 35387fbc5a0STejun Heo * @cmd: internal command which timed out 35487fbc5a0STejun Heo * 35587fbc5a0STejun Heo * Notify EH that internal command @cmd for @dev timed out. This 35687fbc5a0STejun Heo * function should be called only for commands whose timeouts are 35787fbc5a0STejun Heo * determined using ata_internal_cmd_timeout(). 35887fbc5a0STejun Heo * 35987fbc5a0STejun Heo * LOCKING: 36087fbc5a0STejun Heo * EH context. 36187fbc5a0STejun Heo */ 36287fbc5a0STejun Heo void ata_internal_cmd_timed_out(struct ata_device *dev, u8 cmd) 36387fbc5a0STejun Heo { 36487fbc5a0STejun Heo struct ata_eh_context *ehc = &dev->link->eh_context; 36587fbc5a0STejun Heo int ent = ata_lookup_timeout_table(cmd); 36687fbc5a0STejun Heo int idx; 36787fbc5a0STejun Heo 36887fbc5a0STejun Heo if (ent < 0) 36987fbc5a0STejun Heo return; 37087fbc5a0STejun Heo 37187fbc5a0STejun Heo idx = ehc->cmd_timeout_idx[dev->devno][ent]; 37287fbc5a0STejun Heo if (ata_eh_cmd_timeout_table[ent].timeouts[idx + 1] != ULONG_MAX) 37387fbc5a0STejun Heo ehc->cmd_timeout_idx[dev->devno][ent]++; 37487fbc5a0STejun Heo } 37587fbc5a0STejun Heo 3763884f7b0STejun Heo static void ata_ering_record(struct ata_ering *ering, unsigned int eflags, 377c6fd2807SJeff Garzik unsigned int err_mask) 378c6fd2807SJeff Garzik { 379c6fd2807SJeff Garzik struct ata_ering_entry *ent; 380c6fd2807SJeff Garzik 381c6fd2807SJeff Garzik WARN_ON(!err_mask); 382c6fd2807SJeff Garzik 383c6fd2807SJeff Garzik ering->cursor++; 384c6fd2807SJeff Garzik ering->cursor %= ATA_ERING_SIZE; 385c6fd2807SJeff Garzik 386c6fd2807SJeff Garzik ent = &ering->ring[ering->cursor]; 3873884f7b0STejun Heo ent->eflags = eflags; 388c6fd2807SJeff Garzik ent->err_mask = err_mask; 389c6fd2807SJeff Garzik ent->timestamp = get_jiffies_64(); 390c6fd2807SJeff Garzik } 391c6fd2807SJeff Garzik 39276326ac1STejun Heo static struct ata_ering_entry *ata_ering_top(struct ata_ering *ering) 39376326ac1STejun Heo { 39476326ac1STejun Heo struct ata_ering_entry *ent = &ering->ring[ering->cursor]; 39576326ac1STejun Heo 39676326ac1STejun Heo if (ent->err_mask) 39776326ac1STejun Heo return ent; 39876326ac1STejun Heo return NULL; 39976326ac1STejun Heo } 40076326ac1STejun Heo 401d9027470SGwendal Grignou int ata_ering_map(struct ata_ering *ering, 402c6fd2807SJeff Garzik int (*map_fn)(struct ata_ering_entry *, void *), 403c6fd2807SJeff Garzik void *arg) 404c6fd2807SJeff Garzik { 405c6fd2807SJeff Garzik int idx, rc = 0; 406c6fd2807SJeff Garzik struct ata_ering_entry *ent; 407c6fd2807SJeff Garzik 408c6fd2807SJeff Garzik idx = ering->cursor; 409c6fd2807SJeff Garzik do { 410c6fd2807SJeff Garzik ent = &ering->ring[idx]; 411c6fd2807SJeff Garzik if (!ent->err_mask) 412c6fd2807SJeff Garzik break; 413c6fd2807SJeff Garzik rc = map_fn(ent, arg); 414c6fd2807SJeff Garzik if (rc) 415c6fd2807SJeff Garzik break; 416c6fd2807SJeff Garzik idx = (idx - 1 + ATA_ERING_SIZE) % ATA_ERING_SIZE; 417c6fd2807SJeff Garzik } while (idx != ering->cursor); 418c6fd2807SJeff Garzik 419c6fd2807SJeff Garzik return rc; 420c6fd2807SJeff Garzik } 421c6fd2807SJeff Garzik 422d9027470SGwendal Grignou int ata_ering_clear_cb(struct ata_ering_entry *ent, void *void_arg) 423d9027470SGwendal Grignou { 424d9027470SGwendal Grignou ent->eflags |= ATA_EFLAG_OLD_ER; 425d9027470SGwendal Grignou return 0; 426d9027470SGwendal Grignou } 427d9027470SGwendal Grignou 428d9027470SGwendal Grignou static void ata_ering_clear(struct ata_ering *ering) 429d9027470SGwendal Grignou { 430d9027470SGwendal Grignou ata_ering_map(ering, ata_ering_clear_cb, NULL); 431d9027470SGwendal Grignou } 432d9027470SGwendal Grignou 433c6fd2807SJeff Garzik static unsigned int ata_eh_dev_action(struct ata_device *dev) 434c6fd2807SJeff Garzik { 4359af5c9c9STejun Heo struct ata_eh_context *ehc = &dev->link->eh_context; 436c6fd2807SJeff Garzik 437c6fd2807SJeff Garzik return ehc->i.action | ehc->i.dev_action[dev->devno]; 438c6fd2807SJeff Garzik } 439c6fd2807SJeff Garzik 440f58229f8STejun Heo static void ata_eh_clear_action(struct ata_link *link, struct ata_device *dev, 441c6fd2807SJeff Garzik struct ata_eh_info *ehi, unsigned int action) 442c6fd2807SJeff Garzik { 443f58229f8STejun Heo struct ata_device *tdev; 444c6fd2807SJeff Garzik 445c6fd2807SJeff Garzik if (!dev) { 446c6fd2807SJeff Garzik ehi->action &= ~action; 4471eca4365STejun Heo ata_for_each_dev(tdev, link, ALL) 448f58229f8STejun Heo ehi->dev_action[tdev->devno] &= ~action; 449c6fd2807SJeff Garzik } else { 450c6fd2807SJeff Garzik /* doesn't make sense for port-wide EH actions */ 451c6fd2807SJeff Garzik WARN_ON(!(action & ATA_EH_PERDEV_MASK)); 452c6fd2807SJeff Garzik 453c6fd2807SJeff Garzik /* break ehi->action into ehi->dev_action */ 454c6fd2807SJeff Garzik if (ehi->action & action) { 4551eca4365STejun Heo ata_for_each_dev(tdev, link, ALL) 456f58229f8STejun Heo ehi->dev_action[tdev->devno] |= 457f58229f8STejun Heo ehi->action & action; 458c6fd2807SJeff Garzik ehi->action &= ~action; 459c6fd2807SJeff Garzik } 460c6fd2807SJeff Garzik 461c6fd2807SJeff Garzik /* turn off the specified per-dev action */ 462c6fd2807SJeff Garzik ehi->dev_action[dev->devno] &= ~action; 463c6fd2807SJeff Garzik } 464c6fd2807SJeff Garzik } 465c6fd2807SJeff Garzik 466c6fd2807SJeff Garzik /** 467c0c362b6STejun Heo * ata_eh_acquire - acquire EH ownership 468c0c362b6STejun Heo * @ap: ATA port to acquire EH ownership for 469c0c362b6STejun Heo * 470c0c362b6STejun Heo * Acquire EH ownership for @ap. This is the basic exclusion 471c0c362b6STejun Heo * mechanism for ports sharing a host. Only one port hanging off 472c0c362b6STejun Heo * the same host can claim the ownership of EH. 473c0c362b6STejun Heo * 474c0c362b6STejun Heo * LOCKING: 475c0c362b6STejun Heo * EH context. 476c0c362b6STejun Heo */ 477c0c362b6STejun Heo void ata_eh_acquire(struct ata_port *ap) 478c0c362b6STejun Heo { 479c0c362b6STejun Heo mutex_lock(&ap->host->eh_mutex); 480c0c362b6STejun Heo WARN_ON_ONCE(ap->host->eh_owner); 481c0c362b6STejun Heo ap->host->eh_owner = current; 482c0c362b6STejun Heo } 483c0c362b6STejun Heo 484c0c362b6STejun Heo /** 485c0c362b6STejun Heo * ata_eh_release - release EH ownership 486c0c362b6STejun Heo * @ap: ATA port to release EH ownership for 487c0c362b6STejun Heo * 488c0c362b6STejun Heo * Release EH ownership for @ap if the caller. The caller must 489c0c362b6STejun Heo * have acquired EH ownership using ata_eh_acquire() previously. 490c0c362b6STejun Heo * 491c0c362b6STejun Heo * LOCKING: 492c0c362b6STejun Heo * EH context. 493c0c362b6STejun Heo */ 494c0c362b6STejun Heo void ata_eh_release(struct ata_port *ap) 495c0c362b6STejun Heo { 496c0c362b6STejun Heo WARN_ON_ONCE(ap->host->eh_owner != current); 497c0c362b6STejun Heo ap->host->eh_owner = NULL; 498c0c362b6STejun Heo mutex_unlock(&ap->host->eh_mutex); 499c0c362b6STejun Heo } 500c0c362b6STejun Heo 501c0c362b6STejun Heo /** 502c6fd2807SJeff Garzik * ata_scsi_timed_out - SCSI layer time out callback 503c6fd2807SJeff Garzik * @cmd: timed out SCSI command 504c6fd2807SJeff Garzik * 505c6fd2807SJeff Garzik * Handles SCSI layer timeout. We race with normal completion of 506c6fd2807SJeff Garzik * the qc for @cmd. If the qc is already gone, we lose and let 507c6fd2807SJeff Garzik * the scsi command finish (EH_HANDLED). Otherwise, the qc has 508c6fd2807SJeff Garzik * timed out and EH should be invoked. Prevent ata_qc_complete() 509c6fd2807SJeff Garzik * from finishing it by setting EH_SCHEDULED and return 510c6fd2807SJeff Garzik * EH_NOT_HANDLED. 511c6fd2807SJeff Garzik * 512c6fd2807SJeff Garzik * TODO: kill this function once old EH is gone. 513c6fd2807SJeff Garzik * 514c6fd2807SJeff Garzik * LOCKING: 515c6fd2807SJeff Garzik * Called from timer context 516c6fd2807SJeff Garzik * 517c6fd2807SJeff Garzik * RETURNS: 518c6fd2807SJeff Garzik * EH_HANDLED or EH_NOT_HANDLED 519c6fd2807SJeff Garzik */ 520242f9dcbSJens Axboe enum blk_eh_timer_return ata_scsi_timed_out(struct scsi_cmnd *cmd) 521c6fd2807SJeff Garzik { 522c6fd2807SJeff Garzik struct Scsi_Host *host = cmd->device->host; 523c6fd2807SJeff Garzik struct ata_port *ap = ata_shost_to_port(host); 524c6fd2807SJeff Garzik unsigned long flags; 525c6fd2807SJeff Garzik struct ata_queued_cmd *qc; 526242f9dcbSJens Axboe enum blk_eh_timer_return ret; 527c6fd2807SJeff Garzik 528c6fd2807SJeff Garzik DPRINTK("ENTER\n"); 529c6fd2807SJeff Garzik 530c6fd2807SJeff Garzik if (ap->ops->error_handler) { 531242f9dcbSJens Axboe ret = BLK_EH_NOT_HANDLED; 532c6fd2807SJeff Garzik goto out; 533c6fd2807SJeff Garzik } 534c6fd2807SJeff Garzik 535242f9dcbSJens Axboe ret = BLK_EH_HANDLED; 536c6fd2807SJeff Garzik spin_lock_irqsave(ap->lock, flags); 5379af5c9c9STejun Heo qc = ata_qc_from_tag(ap, ap->link.active_tag); 538c6fd2807SJeff Garzik if (qc) { 539c6fd2807SJeff Garzik WARN_ON(qc->scsicmd != cmd); 540c6fd2807SJeff Garzik qc->flags |= ATA_QCFLAG_EH_SCHEDULED; 541c6fd2807SJeff Garzik qc->err_mask |= AC_ERR_TIMEOUT; 542242f9dcbSJens Axboe ret = BLK_EH_NOT_HANDLED; 543c6fd2807SJeff Garzik } 544c6fd2807SJeff Garzik spin_unlock_irqrestore(ap->lock, flags); 545c6fd2807SJeff Garzik 546c6fd2807SJeff Garzik out: 547c6fd2807SJeff Garzik DPRINTK("EXIT, ret=%d\n", ret); 548c6fd2807SJeff Garzik return ret; 549c6fd2807SJeff Garzik } 550c6fd2807SJeff Garzik 551ece180d1STejun Heo static void ata_eh_unload(struct ata_port *ap) 552ece180d1STejun Heo { 553ece180d1STejun Heo struct ata_link *link; 554ece180d1STejun Heo struct ata_device *dev; 555ece180d1STejun Heo unsigned long flags; 556ece180d1STejun Heo 557ece180d1STejun Heo /* Restore SControl IPM and SPD for the next driver and 558ece180d1STejun Heo * disable attached devices. 559ece180d1STejun Heo */ 560ece180d1STejun Heo ata_for_each_link(link, ap, PMP_FIRST) { 561ece180d1STejun Heo sata_scr_write(link, SCR_CONTROL, link->saved_scontrol & 0xff0); 562ece180d1STejun Heo ata_for_each_dev(dev, link, ALL) 563ece180d1STejun Heo ata_dev_disable(dev); 564ece180d1STejun Heo } 565ece180d1STejun Heo 566ece180d1STejun Heo /* freeze and set UNLOADED */ 567ece180d1STejun Heo spin_lock_irqsave(ap->lock, flags); 568ece180d1STejun Heo 569ece180d1STejun Heo ata_port_freeze(ap); /* won't be thawed */ 570ece180d1STejun Heo ap->pflags &= ~ATA_PFLAG_EH_PENDING; /* clear pending from freeze */ 571ece180d1STejun Heo ap->pflags |= ATA_PFLAG_UNLOADED; 572ece180d1STejun Heo 573ece180d1STejun Heo spin_unlock_irqrestore(ap->lock, flags); 574ece180d1STejun Heo } 575ece180d1STejun Heo 576c6fd2807SJeff Garzik /** 577c6fd2807SJeff Garzik * ata_scsi_error - SCSI layer error handler callback 578c6fd2807SJeff Garzik * @host: SCSI host on which error occurred 579c6fd2807SJeff Garzik * 580c6fd2807SJeff Garzik * Handles SCSI-layer-thrown error events. 581c6fd2807SJeff Garzik * 582c6fd2807SJeff Garzik * LOCKING: 583c6fd2807SJeff Garzik * Inherited from SCSI layer (none, can sleep) 584c6fd2807SJeff Garzik * 585c6fd2807SJeff Garzik * RETURNS: 586c6fd2807SJeff Garzik * Zero. 587c6fd2807SJeff Garzik */ 588c6fd2807SJeff Garzik void ata_scsi_error(struct Scsi_Host *host) 589c6fd2807SJeff Garzik { 590c6fd2807SJeff Garzik struct ata_port *ap = ata_shost_to_port(host); 591c6fd2807SJeff Garzik unsigned long flags; 592c34aeebcSJames Bottomley LIST_HEAD(eh_work_q); 593c6fd2807SJeff Garzik 594c6fd2807SJeff Garzik DPRINTK("ENTER\n"); 595c6fd2807SJeff Garzik 596c34aeebcSJames Bottomley spin_lock_irqsave(host->host_lock, flags); 597c34aeebcSJames Bottomley list_splice_init(&host->eh_cmd_q, &eh_work_q); 598c34aeebcSJames Bottomley spin_unlock_irqrestore(host->host_lock, flags); 599c34aeebcSJames Bottomley 6000e0b494cSJames Bottomley ata_scsi_cmd_error_handler(host, ap, &eh_work_q); 6010e0b494cSJames Bottomley 6020e0b494cSJames Bottomley /* If we timed raced normal completion and there is nothing to 6030e0b494cSJames Bottomley recover nr_timedout == 0 why exactly are we doing error recovery ? */ 6040e0b494cSJames Bottomley ata_scsi_port_error_handler(host, ap); 6050e0b494cSJames Bottomley 6060e0b494cSJames Bottomley /* finish or retry handled scmd's and clean up */ 6070e0b494cSJames Bottomley WARN_ON(host->host_failed || !list_empty(&eh_work_q)); 6080e0b494cSJames Bottomley 6090e0b494cSJames Bottomley DPRINTK("EXIT\n"); 6100e0b494cSJames Bottomley } 6110e0b494cSJames Bottomley 6120e0b494cSJames Bottomley /** 6130e0b494cSJames Bottomley * ata_scsi_cmd_error_handler - error callback for a list of commands 6140e0b494cSJames Bottomley * @host: scsi host containing the port 6150e0b494cSJames Bottomley * @ap: ATA port within the host 6160e0b494cSJames Bottomley * @eh_work_q: list of commands to process 6170e0b494cSJames Bottomley * 6180e0b494cSJames Bottomley * process the given list of commands and return those finished to the 6190e0b494cSJames Bottomley * ap->eh_done_q. This function is the first part of the libata error 6200e0b494cSJames Bottomley * handler which processes a given list of failed commands. 6210e0b494cSJames Bottomley */ 6220e0b494cSJames Bottomley void ata_scsi_cmd_error_handler(struct Scsi_Host *host, struct ata_port *ap, 6230e0b494cSJames Bottomley struct list_head *eh_work_q) 6240e0b494cSJames Bottomley { 6250e0b494cSJames Bottomley int i; 6260e0b494cSJames Bottomley unsigned long flags; 6270e0b494cSJames Bottomley 628c429137aSTejun Heo /* make sure sff pio task is not running */ 629c429137aSTejun Heo ata_sff_flush_pio_task(ap); 630c6fd2807SJeff Garzik 631cca3974eSJeff Garzik /* synchronize with host lock and sort out timeouts */ 632c6fd2807SJeff Garzik 633c6fd2807SJeff Garzik /* For new EH, all qcs are finished in one of three ways - 634c6fd2807SJeff Garzik * normal completion, error completion, and SCSI timeout. 635c96f1732SAlan Cox * Both completions can race against SCSI timeout. When normal 636c6fd2807SJeff Garzik * completion wins, the qc never reaches EH. When error 637c6fd2807SJeff Garzik * completion wins, the qc has ATA_QCFLAG_FAILED set. 638c6fd2807SJeff Garzik * 639c6fd2807SJeff Garzik * When SCSI timeout wins, things are a bit more complex. 640c6fd2807SJeff Garzik * Normal or error completion can occur after the timeout but 641c6fd2807SJeff Garzik * before this point. In such cases, both types of 642c6fd2807SJeff Garzik * completions are honored. A scmd is determined to have 643c6fd2807SJeff Garzik * timed out iff its associated qc is active and not failed. 644c6fd2807SJeff Garzik */ 645c6fd2807SJeff Garzik if (ap->ops->error_handler) { 646c6fd2807SJeff Garzik struct scsi_cmnd *scmd, *tmp; 647c6fd2807SJeff Garzik int nr_timedout = 0; 648c6fd2807SJeff Garzik 649c6fd2807SJeff Garzik spin_lock_irqsave(ap->lock, flags); 650c6fd2807SJeff Garzik 651c96f1732SAlan Cox /* This must occur under the ap->lock as we don't want 652c96f1732SAlan Cox a polled recovery to race the real interrupt handler 653c96f1732SAlan Cox 654c96f1732SAlan Cox The lost_interrupt handler checks for any completed but 655c96f1732SAlan Cox non-notified command and completes much like an IRQ handler. 656c96f1732SAlan Cox 657c96f1732SAlan Cox We then fall into the error recovery code which will treat 658c96f1732SAlan Cox this as if normal completion won the race */ 659c96f1732SAlan Cox 660c96f1732SAlan Cox if (ap->ops->lost_interrupt) 661c96f1732SAlan Cox ap->ops->lost_interrupt(ap); 662c96f1732SAlan Cox 6630e0b494cSJames Bottomley list_for_each_entry_safe(scmd, tmp, eh_work_q, eh_entry) { 664c6fd2807SJeff Garzik struct ata_queued_cmd *qc; 665c6fd2807SJeff Garzik 666c6fd2807SJeff Garzik for (i = 0; i < ATA_MAX_QUEUE; i++) { 667c6fd2807SJeff Garzik qc = __ata_qc_from_tag(ap, i); 668c6fd2807SJeff Garzik if (qc->flags & ATA_QCFLAG_ACTIVE && 669c6fd2807SJeff Garzik qc->scsicmd == scmd) 670c6fd2807SJeff Garzik break; 671c6fd2807SJeff Garzik } 672c6fd2807SJeff Garzik 673c6fd2807SJeff Garzik if (i < ATA_MAX_QUEUE) { 674c6fd2807SJeff Garzik /* the scmd has an associated qc */ 675c6fd2807SJeff Garzik if (!(qc->flags & ATA_QCFLAG_FAILED)) { 676c6fd2807SJeff Garzik /* which hasn't failed yet, timeout */ 677c6fd2807SJeff Garzik qc->err_mask |= AC_ERR_TIMEOUT; 678c6fd2807SJeff Garzik qc->flags |= ATA_QCFLAG_FAILED; 679c6fd2807SJeff Garzik nr_timedout++; 680c6fd2807SJeff Garzik } 681c6fd2807SJeff Garzik } else { 682c6fd2807SJeff Garzik /* Normal completion occurred after 683c6fd2807SJeff Garzik * SCSI timeout but before this point. 684c6fd2807SJeff Garzik * Successfully complete it. 685c6fd2807SJeff Garzik */ 686c6fd2807SJeff Garzik scmd->retries = scmd->allowed; 687c6fd2807SJeff Garzik scsi_eh_finish_cmd(scmd, &ap->eh_done_q); 688c6fd2807SJeff Garzik } 689c6fd2807SJeff Garzik } 690c6fd2807SJeff Garzik 691c6fd2807SJeff Garzik /* If we have timed out qcs. They belong to EH from 692c6fd2807SJeff Garzik * this point but the state of the controller is 693c6fd2807SJeff Garzik * unknown. Freeze the port to make sure the IRQ 694c6fd2807SJeff Garzik * handler doesn't diddle with those qcs. This must 695c6fd2807SJeff Garzik * be done atomically w.r.t. setting QCFLAG_FAILED. 696c6fd2807SJeff Garzik */ 697c6fd2807SJeff Garzik if (nr_timedout) 698c6fd2807SJeff Garzik __ata_port_freeze(ap); 699c6fd2807SJeff Garzik 700c6fd2807SJeff Garzik spin_unlock_irqrestore(ap->lock, flags); 701a1e10f7eSTejun Heo 702a1e10f7eSTejun Heo /* initialize eh_tries */ 703a1e10f7eSTejun Heo ap->eh_tries = ATA_EH_MAX_TRIES; 704c6fd2807SJeff Garzik } else 705c6fd2807SJeff Garzik spin_unlock_wait(ap->lock); 706c6fd2807SJeff Garzik 7070e0b494cSJames Bottomley } 7080e0b494cSJames Bottomley EXPORT_SYMBOL(ata_scsi_cmd_error_handler); 7090e0b494cSJames Bottomley 7100e0b494cSJames Bottomley /** 7110e0b494cSJames Bottomley * ata_scsi_port_error_handler - recover the port after the commands 7120e0b494cSJames Bottomley * @host: SCSI host containing the port 7130e0b494cSJames Bottomley * @ap: the ATA port 7140e0b494cSJames Bottomley * 7150e0b494cSJames Bottomley * Handle the recovery of the port @ap after all the commands 7160e0b494cSJames Bottomley * have been recovered. 7170e0b494cSJames Bottomley */ 7180e0b494cSJames Bottomley void ata_scsi_port_error_handler(struct Scsi_Host *host, struct ata_port *ap) 7190e0b494cSJames Bottomley { 7200e0b494cSJames Bottomley unsigned long flags; 721c96f1732SAlan Cox 722c6fd2807SJeff Garzik /* invoke error handler */ 723c6fd2807SJeff Garzik if (ap->ops->error_handler) { 724cf1b86c8STejun Heo struct ata_link *link; 725cf1b86c8STejun Heo 726c0c362b6STejun Heo /* acquire EH ownership */ 727c0c362b6STejun Heo ata_eh_acquire(ap); 728c0c362b6STejun Heo repeat: 7295ddf24c5STejun Heo /* kill fast drain timer */ 7305ddf24c5STejun Heo del_timer_sync(&ap->fastdrain_timer); 7315ddf24c5STejun Heo 732c6fd2807SJeff Garzik /* process port resume request */ 733c6fd2807SJeff Garzik ata_eh_handle_port_resume(ap); 734c6fd2807SJeff Garzik 735c6fd2807SJeff Garzik /* fetch & clear EH info */ 736c6fd2807SJeff Garzik spin_lock_irqsave(ap->lock, flags); 737c6fd2807SJeff Garzik 7381eca4365STejun Heo ata_for_each_link(link, ap, HOST_FIRST) { 73900115e0fSTejun Heo struct ata_eh_context *ehc = &link->eh_context; 74000115e0fSTejun Heo struct ata_device *dev; 74100115e0fSTejun Heo 742cf1b86c8STejun Heo memset(&link->eh_context, 0, sizeof(link->eh_context)); 743cf1b86c8STejun Heo link->eh_context.i = link->eh_info; 744cf1b86c8STejun Heo memset(&link->eh_info, 0, sizeof(link->eh_info)); 74500115e0fSTejun Heo 7461eca4365STejun Heo ata_for_each_dev(dev, link, ENABLED) { 74700115e0fSTejun Heo int devno = dev->devno; 74800115e0fSTejun Heo 74900115e0fSTejun Heo ehc->saved_xfer_mode[devno] = dev->xfer_mode; 75000115e0fSTejun Heo if (ata_ncq_enabled(dev)) 75100115e0fSTejun Heo ehc->saved_ncq_enabled |= 1 << devno; 75200115e0fSTejun Heo } 753cf1b86c8STejun Heo } 754c6fd2807SJeff Garzik 755c6fd2807SJeff Garzik ap->pflags |= ATA_PFLAG_EH_IN_PROGRESS; 756c6fd2807SJeff Garzik ap->pflags &= ~ATA_PFLAG_EH_PENDING; 757da917d69STejun Heo ap->excl_link = NULL; /* don't maintain exclusion over EH */ 758c6fd2807SJeff Garzik 759c6fd2807SJeff Garzik spin_unlock_irqrestore(ap->lock, flags); 760c6fd2807SJeff Garzik 761c6fd2807SJeff Garzik /* invoke EH, skip if unloading or suspended */ 762c6fd2807SJeff Garzik if (!(ap->pflags & (ATA_PFLAG_UNLOADING | ATA_PFLAG_SUSPENDED))) 763c6fd2807SJeff Garzik ap->ops->error_handler(ap); 764ece180d1STejun Heo else { 765ece180d1STejun Heo /* if unloading, commence suicide */ 766ece180d1STejun Heo if ((ap->pflags & ATA_PFLAG_UNLOADING) && 767ece180d1STejun Heo !(ap->pflags & ATA_PFLAG_UNLOADED)) 768ece180d1STejun Heo ata_eh_unload(ap); 769c6fd2807SJeff Garzik ata_eh_finish(ap); 770ece180d1STejun Heo } 771c6fd2807SJeff Garzik 772c6fd2807SJeff Garzik /* process port suspend request */ 773c6fd2807SJeff Garzik ata_eh_handle_port_suspend(ap); 774c6fd2807SJeff Garzik 77525985edcSLucas De Marchi /* Exception might have happened after ->error_handler 776c6fd2807SJeff Garzik * recovered the port but before this point. Repeat 777c6fd2807SJeff Garzik * EH in such case. 778c6fd2807SJeff Garzik */ 779c6fd2807SJeff Garzik spin_lock_irqsave(ap->lock, flags); 780c6fd2807SJeff Garzik 781c6fd2807SJeff Garzik if (ap->pflags & ATA_PFLAG_EH_PENDING) { 782a1e10f7eSTejun Heo if (--ap->eh_tries) { 783c6fd2807SJeff Garzik spin_unlock_irqrestore(ap->lock, flags); 784c6fd2807SJeff Garzik goto repeat; 785c6fd2807SJeff Garzik } 786a9a79dfeSJoe Perches ata_port_err(ap, 787a9a79dfeSJoe Perches "EH pending after %d tries, giving up\n", 788a9a79dfeSJoe Perches ATA_EH_MAX_TRIES); 789914616a3STejun Heo ap->pflags &= ~ATA_PFLAG_EH_PENDING; 790c6fd2807SJeff Garzik } 791c6fd2807SJeff Garzik 792c6fd2807SJeff Garzik /* this run is complete, make sure EH info is clear */ 7931eca4365STejun Heo ata_for_each_link(link, ap, HOST_FIRST) 794cf1b86c8STejun Heo memset(&link->eh_info, 0, sizeof(link->eh_info)); 795c6fd2807SJeff Garzik 796c6fd2807SJeff Garzik /* Clear host_eh_scheduled while holding ap->lock such 797c6fd2807SJeff Garzik * that if exception occurs after this point but 798c6fd2807SJeff Garzik * before EH completion, SCSI midlayer will 799c6fd2807SJeff Garzik * re-initiate EH. 800c6fd2807SJeff Garzik */ 801c6fd2807SJeff Garzik host->host_eh_scheduled = 0; 802c6fd2807SJeff Garzik 803c6fd2807SJeff Garzik spin_unlock_irqrestore(ap->lock, flags); 804c0c362b6STejun Heo ata_eh_release(ap); 805c6fd2807SJeff Garzik } else { 8069af5c9c9STejun Heo WARN_ON(ata_qc_from_tag(ap, ap->link.active_tag) == NULL); 807c6fd2807SJeff Garzik ap->ops->eng_timeout(ap); 808c6fd2807SJeff Garzik } 809c6fd2807SJeff Garzik 810c6fd2807SJeff Garzik scsi_eh_flush_done_q(&ap->eh_done_q); 811c6fd2807SJeff Garzik 812c6fd2807SJeff Garzik /* clean up */ 813c6fd2807SJeff Garzik spin_lock_irqsave(ap->lock, flags); 814c6fd2807SJeff Garzik 815c6fd2807SJeff Garzik if (ap->pflags & ATA_PFLAG_LOADING) 816c6fd2807SJeff Garzik ap->pflags &= ~ATA_PFLAG_LOADING; 817c6fd2807SJeff Garzik else if (ap->pflags & ATA_PFLAG_SCSI_HOTPLUG) 818ad72cf98STejun Heo schedule_delayed_work(&ap->hotplug_task, 0); 819c6fd2807SJeff Garzik 820c6fd2807SJeff Garzik if (ap->pflags & ATA_PFLAG_RECOVERED) 821a9a79dfeSJoe Perches ata_port_info(ap, "EH complete\n"); 822c6fd2807SJeff Garzik 823c6fd2807SJeff Garzik ap->pflags &= ~(ATA_PFLAG_SCSI_HOTPLUG | ATA_PFLAG_RECOVERED); 824c6fd2807SJeff Garzik 825c6fd2807SJeff Garzik /* tell wait_eh that we're done */ 826c6fd2807SJeff Garzik ap->pflags &= ~ATA_PFLAG_EH_IN_PROGRESS; 827c6fd2807SJeff Garzik wake_up_all(&ap->eh_wait_q); 828c6fd2807SJeff Garzik 829c6fd2807SJeff Garzik spin_unlock_irqrestore(ap->lock, flags); 830c6fd2807SJeff Garzik } 8310e0b494cSJames Bottomley EXPORT_SYMBOL_GPL(ata_scsi_port_error_handler); 832c6fd2807SJeff Garzik 833c6fd2807SJeff Garzik /** 834c6fd2807SJeff Garzik * ata_port_wait_eh - Wait for the currently pending EH to complete 835c6fd2807SJeff Garzik * @ap: Port to wait EH for 836c6fd2807SJeff Garzik * 837c6fd2807SJeff Garzik * Wait until the currently pending EH is complete. 838c6fd2807SJeff Garzik * 839c6fd2807SJeff Garzik * LOCKING: 840c6fd2807SJeff Garzik * Kernel thread context (may sleep). 841c6fd2807SJeff Garzik */ 842c6fd2807SJeff Garzik void ata_port_wait_eh(struct ata_port *ap) 843c6fd2807SJeff Garzik { 844c6fd2807SJeff Garzik unsigned long flags; 845c6fd2807SJeff Garzik DEFINE_WAIT(wait); 846c6fd2807SJeff Garzik 847c6fd2807SJeff Garzik retry: 848c6fd2807SJeff Garzik spin_lock_irqsave(ap->lock, flags); 849c6fd2807SJeff Garzik 850c6fd2807SJeff Garzik while (ap->pflags & (ATA_PFLAG_EH_PENDING | ATA_PFLAG_EH_IN_PROGRESS)) { 851c6fd2807SJeff Garzik prepare_to_wait(&ap->eh_wait_q, &wait, TASK_UNINTERRUPTIBLE); 852c6fd2807SJeff Garzik spin_unlock_irqrestore(ap->lock, flags); 853c6fd2807SJeff Garzik schedule(); 854c6fd2807SJeff Garzik spin_lock_irqsave(ap->lock, flags); 855c6fd2807SJeff Garzik } 856c6fd2807SJeff Garzik finish_wait(&ap->eh_wait_q, &wait); 857c6fd2807SJeff Garzik 858c6fd2807SJeff Garzik spin_unlock_irqrestore(ap->lock, flags); 859c6fd2807SJeff Garzik 860c6fd2807SJeff Garzik /* make sure SCSI EH is complete */ 861cca3974eSJeff Garzik if (scsi_host_in_recovery(ap->scsi_host)) { 86297750cebSTejun Heo ata_msleep(ap, 10); 863c6fd2807SJeff Garzik goto retry; 864c6fd2807SJeff Garzik } 865c6fd2807SJeff Garzik } 866c6fd2807SJeff Garzik 8675ddf24c5STejun Heo static int ata_eh_nr_in_flight(struct ata_port *ap) 8685ddf24c5STejun Heo { 8695ddf24c5STejun Heo unsigned int tag; 8705ddf24c5STejun Heo int nr = 0; 8715ddf24c5STejun Heo 8725ddf24c5STejun Heo /* count only non-internal commands */ 8735ddf24c5STejun Heo for (tag = 0; tag < ATA_MAX_QUEUE - 1; tag++) 8745ddf24c5STejun Heo if (ata_qc_from_tag(ap, tag)) 8755ddf24c5STejun Heo nr++; 8765ddf24c5STejun Heo 8775ddf24c5STejun Heo return nr; 8785ddf24c5STejun Heo } 8795ddf24c5STejun Heo 8805ddf24c5STejun Heo void ata_eh_fastdrain_timerfn(unsigned long arg) 8815ddf24c5STejun Heo { 8825ddf24c5STejun Heo struct ata_port *ap = (void *)arg; 8835ddf24c5STejun Heo unsigned long flags; 8845ddf24c5STejun Heo int cnt; 8855ddf24c5STejun Heo 8865ddf24c5STejun Heo spin_lock_irqsave(ap->lock, flags); 8875ddf24c5STejun Heo 8885ddf24c5STejun Heo cnt = ata_eh_nr_in_flight(ap); 8895ddf24c5STejun Heo 8905ddf24c5STejun Heo /* are we done? */ 8915ddf24c5STejun Heo if (!cnt) 8925ddf24c5STejun Heo goto out_unlock; 8935ddf24c5STejun Heo 8945ddf24c5STejun Heo if (cnt == ap->fastdrain_cnt) { 8955ddf24c5STejun Heo unsigned int tag; 8965ddf24c5STejun Heo 8975ddf24c5STejun Heo /* No progress during the last interval, tag all 8985ddf24c5STejun Heo * in-flight qcs as timed out and freeze the port. 8995ddf24c5STejun Heo */ 9005ddf24c5STejun Heo for (tag = 0; tag < ATA_MAX_QUEUE - 1; tag++) { 9015ddf24c5STejun Heo struct ata_queued_cmd *qc = ata_qc_from_tag(ap, tag); 9025ddf24c5STejun Heo if (qc) 9035ddf24c5STejun Heo qc->err_mask |= AC_ERR_TIMEOUT; 9045ddf24c5STejun Heo } 9055ddf24c5STejun Heo 9065ddf24c5STejun Heo ata_port_freeze(ap); 9075ddf24c5STejun Heo } else { 9085ddf24c5STejun Heo /* some qcs have finished, give it another chance */ 9095ddf24c5STejun Heo ap->fastdrain_cnt = cnt; 9105ddf24c5STejun Heo ap->fastdrain_timer.expires = 911341c2c95STejun Heo ata_deadline(jiffies, ATA_EH_FASTDRAIN_INTERVAL); 9125ddf24c5STejun Heo add_timer(&ap->fastdrain_timer); 9135ddf24c5STejun Heo } 9145ddf24c5STejun Heo 9155ddf24c5STejun Heo out_unlock: 9165ddf24c5STejun Heo spin_unlock_irqrestore(ap->lock, flags); 9175ddf24c5STejun Heo } 9185ddf24c5STejun Heo 9195ddf24c5STejun Heo /** 9205ddf24c5STejun Heo * ata_eh_set_pending - set ATA_PFLAG_EH_PENDING and activate fast drain 9215ddf24c5STejun Heo * @ap: target ATA port 9225ddf24c5STejun Heo * @fastdrain: activate fast drain 9235ddf24c5STejun Heo * 9245ddf24c5STejun Heo * Set ATA_PFLAG_EH_PENDING and activate fast drain if @fastdrain 9255ddf24c5STejun Heo * is non-zero and EH wasn't pending before. Fast drain ensures 9265ddf24c5STejun Heo * that EH kicks in in timely manner. 9275ddf24c5STejun Heo * 9285ddf24c5STejun Heo * LOCKING: 9295ddf24c5STejun Heo * spin_lock_irqsave(host lock) 9305ddf24c5STejun Heo */ 9315ddf24c5STejun Heo static void ata_eh_set_pending(struct ata_port *ap, int fastdrain) 9325ddf24c5STejun Heo { 9335ddf24c5STejun Heo int cnt; 9345ddf24c5STejun Heo 9355ddf24c5STejun Heo /* already scheduled? */ 9365ddf24c5STejun Heo if (ap->pflags & ATA_PFLAG_EH_PENDING) 9375ddf24c5STejun Heo return; 9385ddf24c5STejun Heo 9395ddf24c5STejun Heo ap->pflags |= ATA_PFLAG_EH_PENDING; 9405ddf24c5STejun Heo 9415ddf24c5STejun Heo if (!fastdrain) 9425ddf24c5STejun Heo return; 9435ddf24c5STejun Heo 9445ddf24c5STejun Heo /* do we have in-flight qcs? */ 9455ddf24c5STejun Heo cnt = ata_eh_nr_in_flight(ap); 9465ddf24c5STejun Heo if (!cnt) 9475ddf24c5STejun Heo return; 9485ddf24c5STejun Heo 9495ddf24c5STejun Heo /* activate fast drain */ 9505ddf24c5STejun Heo ap->fastdrain_cnt = cnt; 951341c2c95STejun Heo ap->fastdrain_timer.expires = 952341c2c95STejun Heo ata_deadline(jiffies, ATA_EH_FASTDRAIN_INTERVAL); 9535ddf24c5STejun Heo add_timer(&ap->fastdrain_timer); 9545ddf24c5STejun Heo } 9555ddf24c5STejun Heo 956c6fd2807SJeff Garzik /** 957c6fd2807SJeff Garzik * ata_qc_schedule_eh - schedule qc for error handling 958c6fd2807SJeff Garzik * @qc: command to schedule error handling for 959c6fd2807SJeff Garzik * 960c6fd2807SJeff Garzik * Schedule error handling for @qc. EH will kick in as soon as 961c6fd2807SJeff Garzik * other commands are drained. 962c6fd2807SJeff Garzik * 963c6fd2807SJeff Garzik * LOCKING: 964cca3974eSJeff Garzik * spin_lock_irqsave(host lock) 965c6fd2807SJeff Garzik */ 966c6fd2807SJeff Garzik void ata_qc_schedule_eh(struct ata_queued_cmd *qc) 967c6fd2807SJeff Garzik { 968c6fd2807SJeff Garzik struct ata_port *ap = qc->ap; 969fa41efdaSTejun Heo struct request_queue *q = qc->scsicmd->device->request_queue; 970fa41efdaSTejun Heo unsigned long flags; 971c6fd2807SJeff Garzik 972c6fd2807SJeff Garzik WARN_ON(!ap->ops->error_handler); 973c6fd2807SJeff Garzik 974c6fd2807SJeff Garzik qc->flags |= ATA_QCFLAG_FAILED; 9755ddf24c5STejun Heo ata_eh_set_pending(ap, 1); 976c6fd2807SJeff Garzik 977c6fd2807SJeff Garzik /* The following will fail if timeout has already expired. 978c6fd2807SJeff Garzik * ata_scsi_error() takes care of such scmds on EH entry. 979c6fd2807SJeff Garzik * Note that ATA_QCFLAG_FAILED is unconditionally set after 980c6fd2807SJeff Garzik * this function completes. 981c6fd2807SJeff Garzik */ 982fa41efdaSTejun Heo spin_lock_irqsave(q->queue_lock, flags); 983242f9dcbSJens Axboe blk_abort_request(qc->scsicmd->request); 984fa41efdaSTejun Heo spin_unlock_irqrestore(q->queue_lock, flags); 985c6fd2807SJeff Garzik } 986c6fd2807SJeff Garzik 987c6fd2807SJeff Garzik /** 988c6fd2807SJeff Garzik * ata_port_schedule_eh - schedule error handling without a qc 989c6fd2807SJeff Garzik * @ap: ATA port to schedule EH for 990c6fd2807SJeff Garzik * 991c6fd2807SJeff Garzik * Schedule error handling for @ap. EH will kick in as soon as 992c6fd2807SJeff Garzik * all commands are drained. 993c6fd2807SJeff Garzik * 994c6fd2807SJeff Garzik * LOCKING: 995cca3974eSJeff Garzik * spin_lock_irqsave(host lock) 996c6fd2807SJeff Garzik */ 997c6fd2807SJeff Garzik void ata_port_schedule_eh(struct ata_port *ap) 998c6fd2807SJeff Garzik { 999c6fd2807SJeff Garzik WARN_ON(!ap->ops->error_handler); 1000c6fd2807SJeff Garzik 1001f4d6d004STejun Heo if (ap->pflags & ATA_PFLAG_INITIALIZING) 1002f4d6d004STejun Heo return; 1003f4d6d004STejun Heo 10045ddf24c5STejun Heo ata_eh_set_pending(ap, 1); 1005cca3974eSJeff Garzik scsi_schedule_eh(ap->scsi_host); 1006c6fd2807SJeff Garzik 1007c6fd2807SJeff Garzik DPRINTK("port EH scheduled\n"); 1008c6fd2807SJeff Garzik } 1009c6fd2807SJeff Garzik 1010dbd82616STejun Heo static int ata_do_link_abort(struct ata_port *ap, struct ata_link *link) 1011c6fd2807SJeff Garzik { 1012c6fd2807SJeff Garzik int tag, nr_aborted = 0; 1013c6fd2807SJeff Garzik 1014c6fd2807SJeff Garzik WARN_ON(!ap->ops->error_handler); 1015c6fd2807SJeff Garzik 10165ddf24c5STejun Heo /* we're gonna abort all commands, no need for fast drain */ 10175ddf24c5STejun Heo ata_eh_set_pending(ap, 0); 10185ddf24c5STejun Heo 1019c6fd2807SJeff Garzik for (tag = 0; tag < ATA_MAX_QUEUE; tag++) { 1020c6fd2807SJeff Garzik struct ata_queued_cmd *qc = ata_qc_from_tag(ap, tag); 1021c6fd2807SJeff Garzik 1022dbd82616STejun Heo if (qc && (!link || qc->dev->link == link)) { 1023c6fd2807SJeff Garzik qc->flags |= ATA_QCFLAG_FAILED; 1024c6fd2807SJeff Garzik ata_qc_complete(qc); 1025c6fd2807SJeff Garzik nr_aborted++; 1026c6fd2807SJeff Garzik } 1027c6fd2807SJeff Garzik } 1028c6fd2807SJeff Garzik 1029c6fd2807SJeff Garzik if (!nr_aborted) 1030c6fd2807SJeff Garzik ata_port_schedule_eh(ap); 1031c6fd2807SJeff Garzik 1032c6fd2807SJeff Garzik return nr_aborted; 1033c6fd2807SJeff Garzik } 1034c6fd2807SJeff Garzik 1035c6fd2807SJeff Garzik /** 1036dbd82616STejun Heo * ata_link_abort - abort all qc's on the link 1037dbd82616STejun Heo * @link: ATA link to abort qc's for 1038dbd82616STejun Heo * 1039dbd82616STejun Heo * Abort all active qc's active on @link and schedule EH. 1040dbd82616STejun Heo * 1041dbd82616STejun Heo * LOCKING: 1042dbd82616STejun Heo * spin_lock_irqsave(host lock) 1043dbd82616STejun Heo * 1044dbd82616STejun Heo * RETURNS: 1045dbd82616STejun Heo * Number of aborted qc's. 1046dbd82616STejun Heo */ 1047dbd82616STejun Heo int ata_link_abort(struct ata_link *link) 1048dbd82616STejun Heo { 1049dbd82616STejun Heo return ata_do_link_abort(link->ap, link); 1050dbd82616STejun Heo } 1051dbd82616STejun Heo 1052dbd82616STejun Heo /** 1053dbd82616STejun Heo * ata_port_abort - abort all qc's on the port 1054dbd82616STejun Heo * @ap: ATA port to abort qc's for 1055dbd82616STejun Heo * 1056dbd82616STejun Heo * Abort all active qc's of @ap and schedule EH. 1057dbd82616STejun Heo * 1058dbd82616STejun Heo * LOCKING: 1059dbd82616STejun Heo * spin_lock_irqsave(host_set lock) 1060dbd82616STejun Heo * 1061dbd82616STejun Heo * RETURNS: 1062dbd82616STejun Heo * Number of aborted qc's. 1063dbd82616STejun Heo */ 1064dbd82616STejun Heo int ata_port_abort(struct ata_port *ap) 1065dbd82616STejun Heo { 1066dbd82616STejun Heo return ata_do_link_abort(ap, NULL); 1067dbd82616STejun Heo } 1068dbd82616STejun Heo 1069dbd82616STejun Heo /** 1070c6fd2807SJeff Garzik * __ata_port_freeze - freeze port 1071c6fd2807SJeff Garzik * @ap: ATA port to freeze 1072c6fd2807SJeff Garzik * 1073c6fd2807SJeff Garzik * This function is called when HSM violation or some other 1074c6fd2807SJeff Garzik * condition disrupts normal operation of the port. Frozen port 1075c6fd2807SJeff Garzik * is not allowed to perform any operation until the port is 1076c6fd2807SJeff Garzik * thawed, which usually follows a successful reset. 1077c6fd2807SJeff Garzik * 1078c6fd2807SJeff Garzik * ap->ops->freeze() callback can be used for freezing the port 1079c6fd2807SJeff Garzik * hardware-wise (e.g. mask interrupt and stop DMA engine). If a 1080c6fd2807SJeff Garzik * port cannot be frozen hardware-wise, the interrupt handler 1081c6fd2807SJeff Garzik * must ack and clear interrupts unconditionally while the port 1082c6fd2807SJeff Garzik * is frozen. 1083c6fd2807SJeff Garzik * 1084c6fd2807SJeff Garzik * LOCKING: 1085cca3974eSJeff Garzik * spin_lock_irqsave(host lock) 1086c6fd2807SJeff Garzik */ 1087c6fd2807SJeff Garzik static void __ata_port_freeze(struct ata_port *ap) 1088c6fd2807SJeff Garzik { 1089c6fd2807SJeff Garzik WARN_ON(!ap->ops->error_handler); 1090c6fd2807SJeff Garzik 1091c6fd2807SJeff Garzik if (ap->ops->freeze) 1092c6fd2807SJeff Garzik ap->ops->freeze(ap); 1093c6fd2807SJeff Garzik 1094c6fd2807SJeff Garzik ap->pflags |= ATA_PFLAG_FROZEN; 1095c6fd2807SJeff Garzik 109644877b4eSTejun Heo DPRINTK("ata%u port frozen\n", ap->print_id); 1097c6fd2807SJeff Garzik } 1098c6fd2807SJeff Garzik 1099c6fd2807SJeff Garzik /** 1100c6fd2807SJeff Garzik * ata_port_freeze - abort & freeze port 1101c6fd2807SJeff Garzik * @ap: ATA port to freeze 1102c6fd2807SJeff Garzik * 110354c38444SJeff Garzik * Abort and freeze @ap. The freeze operation must be called 110454c38444SJeff Garzik * first, because some hardware requires special operations 110554c38444SJeff Garzik * before the taskfile registers are accessible. 1106c6fd2807SJeff Garzik * 1107c6fd2807SJeff Garzik * LOCKING: 1108cca3974eSJeff Garzik * spin_lock_irqsave(host lock) 1109c6fd2807SJeff Garzik * 1110c6fd2807SJeff Garzik * RETURNS: 1111c6fd2807SJeff Garzik * Number of aborted commands. 1112c6fd2807SJeff Garzik */ 1113c6fd2807SJeff Garzik int ata_port_freeze(struct ata_port *ap) 1114c6fd2807SJeff Garzik { 1115c6fd2807SJeff Garzik int nr_aborted; 1116c6fd2807SJeff Garzik 1117c6fd2807SJeff Garzik WARN_ON(!ap->ops->error_handler); 1118c6fd2807SJeff Garzik 1119c6fd2807SJeff Garzik __ata_port_freeze(ap); 112054c38444SJeff Garzik nr_aborted = ata_port_abort(ap); 1121c6fd2807SJeff Garzik 1122c6fd2807SJeff Garzik return nr_aborted; 1123c6fd2807SJeff Garzik } 1124c6fd2807SJeff Garzik 1125c6fd2807SJeff Garzik /** 11267d77b247STejun Heo * sata_async_notification - SATA async notification handler 11277d77b247STejun Heo * @ap: ATA port where async notification is received 11287d77b247STejun Heo * 11297d77b247STejun Heo * Handler to be called when async notification via SDB FIS is 11307d77b247STejun Heo * received. This function schedules EH if necessary. 11317d77b247STejun Heo * 11327d77b247STejun Heo * LOCKING: 11337d77b247STejun Heo * spin_lock_irqsave(host lock) 11347d77b247STejun Heo * 11357d77b247STejun Heo * RETURNS: 11367d77b247STejun Heo * 1 if EH is scheduled, 0 otherwise. 11377d77b247STejun Heo */ 11387d77b247STejun Heo int sata_async_notification(struct ata_port *ap) 11397d77b247STejun Heo { 11407d77b247STejun Heo u32 sntf; 11417d77b247STejun Heo int rc; 11427d77b247STejun Heo 11437d77b247STejun Heo if (!(ap->flags & ATA_FLAG_AN)) 11447d77b247STejun Heo return 0; 11457d77b247STejun Heo 11467d77b247STejun Heo rc = sata_scr_read(&ap->link, SCR_NOTIFICATION, &sntf); 11477d77b247STejun Heo if (rc == 0) 11487d77b247STejun Heo sata_scr_write(&ap->link, SCR_NOTIFICATION, sntf); 11497d77b247STejun Heo 1150071f44b1STejun Heo if (!sata_pmp_attached(ap) || rc) { 11517d77b247STejun Heo /* PMP is not attached or SNTF is not available */ 1152071f44b1STejun Heo if (!sata_pmp_attached(ap)) { 11537d77b247STejun Heo /* PMP is not attached. Check whether ATAPI 11547d77b247STejun Heo * AN is configured. If so, notify media 11557d77b247STejun Heo * change. 11567d77b247STejun Heo */ 11577d77b247STejun Heo struct ata_device *dev = ap->link.device; 11587d77b247STejun Heo 11597d77b247STejun Heo if ((dev->class == ATA_DEV_ATAPI) && 11607d77b247STejun Heo (dev->flags & ATA_DFLAG_AN)) 11617d77b247STejun Heo ata_scsi_media_change_notify(dev); 11627d77b247STejun Heo return 0; 11637d77b247STejun Heo } else { 11647d77b247STejun Heo /* PMP is attached but SNTF is not available. 11657d77b247STejun Heo * ATAPI async media change notification is 11667d77b247STejun Heo * not used. The PMP must be reporting PHY 11677d77b247STejun Heo * status change, schedule EH. 11687d77b247STejun Heo */ 11697d77b247STejun Heo ata_port_schedule_eh(ap); 11707d77b247STejun Heo return 1; 11717d77b247STejun Heo } 11727d77b247STejun Heo } else { 11737d77b247STejun Heo /* PMP is attached and SNTF is available */ 11747d77b247STejun Heo struct ata_link *link; 11757d77b247STejun Heo 11767d77b247STejun Heo /* check and notify ATAPI AN */ 11771eca4365STejun Heo ata_for_each_link(link, ap, EDGE) { 11787d77b247STejun Heo if (!(sntf & (1 << link->pmp))) 11797d77b247STejun Heo continue; 11807d77b247STejun Heo 11817d77b247STejun Heo if ((link->device->class == ATA_DEV_ATAPI) && 11827d77b247STejun Heo (link->device->flags & ATA_DFLAG_AN)) 11837d77b247STejun Heo ata_scsi_media_change_notify(link->device); 11847d77b247STejun Heo } 11857d77b247STejun Heo 11867d77b247STejun Heo /* If PMP is reporting that PHY status of some 11877d77b247STejun Heo * downstream ports has changed, schedule EH. 11887d77b247STejun Heo */ 11897d77b247STejun Heo if (sntf & (1 << SATA_PMP_CTRL_PORT)) { 11907d77b247STejun Heo ata_port_schedule_eh(ap); 11917d77b247STejun Heo return 1; 11927d77b247STejun Heo } 11937d77b247STejun Heo 11947d77b247STejun Heo return 0; 11957d77b247STejun Heo } 11967d77b247STejun Heo } 11977d77b247STejun Heo 11987d77b247STejun Heo /** 1199c6fd2807SJeff Garzik * ata_eh_freeze_port - EH helper to freeze port 1200c6fd2807SJeff Garzik * @ap: ATA port to freeze 1201c6fd2807SJeff Garzik * 1202c6fd2807SJeff Garzik * Freeze @ap. 1203c6fd2807SJeff Garzik * 1204c6fd2807SJeff Garzik * LOCKING: 1205c6fd2807SJeff Garzik * None. 1206c6fd2807SJeff Garzik */ 1207c6fd2807SJeff Garzik void ata_eh_freeze_port(struct ata_port *ap) 1208c6fd2807SJeff Garzik { 1209c6fd2807SJeff Garzik unsigned long flags; 1210c6fd2807SJeff Garzik 1211c6fd2807SJeff Garzik if (!ap->ops->error_handler) 1212c6fd2807SJeff Garzik return; 1213c6fd2807SJeff Garzik 1214c6fd2807SJeff Garzik spin_lock_irqsave(ap->lock, flags); 1215c6fd2807SJeff Garzik __ata_port_freeze(ap); 1216c6fd2807SJeff Garzik spin_unlock_irqrestore(ap->lock, flags); 1217c6fd2807SJeff Garzik } 1218c6fd2807SJeff Garzik 1219c6fd2807SJeff Garzik /** 1220c6fd2807SJeff Garzik * ata_port_thaw_port - EH helper to thaw port 1221c6fd2807SJeff Garzik * @ap: ATA port to thaw 1222c6fd2807SJeff Garzik * 1223c6fd2807SJeff Garzik * Thaw frozen port @ap. 1224c6fd2807SJeff Garzik * 1225c6fd2807SJeff Garzik * LOCKING: 1226c6fd2807SJeff Garzik * None. 1227c6fd2807SJeff Garzik */ 1228c6fd2807SJeff Garzik void ata_eh_thaw_port(struct ata_port *ap) 1229c6fd2807SJeff Garzik { 1230c6fd2807SJeff Garzik unsigned long flags; 1231c6fd2807SJeff Garzik 1232c6fd2807SJeff Garzik if (!ap->ops->error_handler) 1233c6fd2807SJeff Garzik return; 1234c6fd2807SJeff Garzik 1235c6fd2807SJeff Garzik spin_lock_irqsave(ap->lock, flags); 1236c6fd2807SJeff Garzik 1237c6fd2807SJeff Garzik ap->pflags &= ~ATA_PFLAG_FROZEN; 1238c6fd2807SJeff Garzik 1239c6fd2807SJeff Garzik if (ap->ops->thaw) 1240c6fd2807SJeff Garzik ap->ops->thaw(ap); 1241c6fd2807SJeff Garzik 1242c6fd2807SJeff Garzik spin_unlock_irqrestore(ap->lock, flags); 1243c6fd2807SJeff Garzik 124444877b4eSTejun Heo DPRINTK("ata%u port thawed\n", ap->print_id); 1245c6fd2807SJeff Garzik } 1246c6fd2807SJeff Garzik 1247c6fd2807SJeff Garzik static void ata_eh_scsidone(struct scsi_cmnd *scmd) 1248c6fd2807SJeff Garzik { 1249c6fd2807SJeff Garzik /* nada */ 1250c6fd2807SJeff Garzik } 1251c6fd2807SJeff Garzik 1252c6fd2807SJeff Garzik static void __ata_eh_qc_complete(struct ata_queued_cmd *qc) 1253c6fd2807SJeff Garzik { 1254c6fd2807SJeff Garzik struct ata_port *ap = qc->ap; 1255c6fd2807SJeff Garzik struct scsi_cmnd *scmd = qc->scsicmd; 1256c6fd2807SJeff Garzik unsigned long flags; 1257c6fd2807SJeff Garzik 1258c6fd2807SJeff Garzik spin_lock_irqsave(ap->lock, flags); 1259c6fd2807SJeff Garzik qc->scsidone = ata_eh_scsidone; 1260c6fd2807SJeff Garzik __ata_qc_complete(qc); 1261c6fd2807SJeff Garzik WARN_ON(ata_tag_valid(qc->tag)); 1262c6fd2807SJeff Garzik spin_unlock_irqrestore(ap->lock, flags); 1263c6fd2807SJeff Garzik 1264c6fd2807SJeff Garzik scsi_eh_finish_cmd(scmd, &ap->eh_done_q); 1265c6fd2807SJeff Garzik } 1266c6fd2807SJeff Garzik 1267c6fd2807SJeff Garzik /** 1268c6fd2807SJeff Garzik * ata_eh_qc_complete - Complete an active ATA command from EH 1269c6fd2807SJeff Garzik * @qc: Command to complete 1270c6fd2807SJeff Garzik * 1271c6fd2807SJeff Garzik * Indicate to the mid and upper layers that an ATA command has 1272c6fd2807SJeff Garzik * completed. To be used from EH. 1273c6fd2807SJeff Garzik */ 1274c6fd2807SJeff Garzik void ata_eh_qc_complete(struct ata_queued_cmd *qc) 1275c6fd2807SJeff Garzik { 1276c6fd2807SJeff Garzik struct scsi_cmnd *scmd = qc->scsicmd; 1277c6fd2807SJeff Garzik scmd->retries = scmd->allowed; 1278c6fd2807SJeff Garzik __ata_eh_qc_complete(qc); 1279c6fd2807SJeff Garzik } 1280c6fd2807SJeff Garzik 1281c6fd2807SJeff Garzik /** 1282c6fd2807SJeff Garzik * ata_eh_qc_retry - Tell midlayer to retry an ATA command after EH 1283c6fd2807SJeff Garzik * @qc: Command to retry 1284c6fd2807SJeff Garzik * 1285c6fd2807SJeff Garzik * Indicate to the mid and upper layers that an ATA command 1286c6fd2807SJeff Garzik * should be retried. To be used from EH. 1287c6fd2807SJeff Garzik * 1288c6fd2807SJeff Garzik * SCSI midlayer limits the number of retries to scmd->allowed. 1289c6fd2807SJeff Garzik * scmd->retries is decremented for commands which get retried 1290c6fd2807SJeff Garzik * due to unrelated failures (qc->err_mask is zero). 1291c6fd2807SJeff Garzik */ 1292c6fd2807SJeff Garzik void ata_eh_qc_retry(struct ata_queued_cmd *qc) 1293c6fd2807SJeff Garzik { 1294c6fd2807SJeff Garzik struct scsi_cmnd *scmd = qc->scsicmd; 1295c6fd2807SJeff Garzik if (!qc->err_mask && scmd->retries) 1296c6fd2807SJeff Garzik scmd->retries--; 1297c6fd2807SJeff Garzik __ata_eh_qc_complete(qc); 1298c6fd2807SJeff Garzik } 1299c6fd2807SJeff Garzik 1300c6fd2807SJeff Garzik /** 1301678afac6STejun Heo * ata_dev_disable - disable ATA device 1302678afac6STejun Heo * @dev: ATA device to disable 1303678afac6STejun Heo * 1304678afac6STejun Heo * Disable @dev. 1305678afac6STejun Heo * 1306678afac6STejun Heo * Locking: 1307678afac6STejun Heo * EH context. 1308678afac6STejun Heo */ 1309678afac6STejun Heo void ata_dev_disable(struct ata_device *dev) 1310678afac6STejun Heo { 1311678afac6STejun Heo if (!ata_dev_enabled(dev)) 1312678afac6STejun Heo return; 1313678afac6STejun Heo 1314678afac6STejun Heo if (ata_msg_drv(dev->link->ap)) 1315a9a79dfeSJoe Perches ata_dev_warn(dev, "disabled\n"); 1316678afac6STejun Heo ata_acpi_on_disable(dev); 1317678afac6STejun Heo ata_down_xfermask_limit(dev, ATA_DNXFER_FORCE_PIO0 | ATA_DNXFER_QUIET); 1318678afac6STejun Heo dev->class++; 131999cf610aSTejun Heo 132099cf610aSTejun Heo /* From now till the next successful probe, ering is used to 132199cf610aSTejun Heo * track probe failures. Clear accumulated device error info. 132299cf610aSTejun Heo */ 132399cf610aSTejun Heo ata_ering_clear(&dev->ering); 1324678afac6STejun Heo } 1325678afac6STejun Heo 1326678afac6STejun Heo /** 1327c6fd2807SJeff Garzik * ata_eh_detach_dev - detach ATA device 1328c6fd2807SJeff Garzik * @dev: ATA device to detach 1329c6fd2807SJeff Garzik * 1330c6fd2807SJeff Garzik * Detach @dev. 1331c6fd2807SJeff Garzik * 1332c6fd2807SJeff Garzik * LOCKING: 1333c6fd2807SJeff Garzik * None. 1334c6fd2807SJeff Garzik */ 1335fb7fd614STejun Heo void ata_eh_detach_dev(struct ata_device *dev) 1336c6fd2807SJeff Garzik { 1337f58229f8STejun Heo struct ata_link *link = dev->link; 1338f58229f8STejun Heo struct ata_port *ap = link->ap; 133990484ebfSTejun Heo struct ata_eh_context *ehc = &link->eh_context; 1340c6fd2807SJeff Garzik unsigned long flags; 1341c6fd2807SJeff Garzik 1342c6fd2807SJeff Garzik ata_dev_disable(dev); 1343c6fd2807SJeff Garzik 1344c6fd2807SJeff Garzik spin_lock_irqsave(ap->lock, flags); 1345c6fd2807SJeff Garzik 1346c6fd2807SJeff Garzik dev->flags &= ~ATA_DFLAG_DETACH; 1347c6fd2807SJeff Garzik 1348c6fd2807SJeff Garzik if (ata_scsi_offline_dev(dev)) { 1349c6fd2807SJeff Garzik dev->flags |= ATA_DFLAG_DETACHED; 1350c6fd2807SJeff Garzik ap->pflags |= ATA_PFLAG_SCSI_HOTPLUG; 1351c6fd2807SJeff Garzik } 1352c6fd2807SJeff Garzik 135390484ebfSTejun Heo /* clear per-dev EH info */ 1354f58229f8STejun Heo ata_eh_clear_action(link, dev, &link->eh_info, ATA_EH_PERDEV_MASK); 1355f58229f8STejun Heo ata_eh_clear_action(link, dev, &link->eh_context.i, ATA_EH_PERDEV_MASK); 135690484ebfSTejun Heo ehc->saved_xfer_mode[dev->devno] = 0; 135790484ebfSTejun Heo ehc->saved_ncq_enabled &= ~(1 << dev->devno); 1358c6fd2807SJeff Garzik 1359c6fd2807SJeff Garzik spin_unlock_irqrestore(ap->lock, flags); 1360c6fd2807SJeff Garzik } 1361c6fd2807SJeff Garzik 1362c6fd2807SJeff Garzik /** 1363c6fd2807SJeff Garzik * ata_eh_about_to_do - about to perform eh_action 1364955e57dfSTejun Heo * @link: target ATA link 1365c6fd2807SJeff Garzik * @dev: target ATA dev for per-dev action (can be NULL) 1366c6fd2807SJeff Garzik * @action: action about to be performed 1367c6fd2807SJeff Garzik * 1368c6fd2807SJeff Garzik * Called just before performing EH actions to clear related bits 1369955e57dfSTejun Heo * in @link->eh_info such that eh actions are not unnecessarily 1370955e57dfSTejun Heo * repeated. 1371c6fd2807SJeff Garzik * 1372c6fd2807SJeff Garzik * LOCKING: 1373c6fd2807SJeff Garzik * None. 1374c6fd2807SJeff Garzik */ 1375fb7fd614STejun Heo void ata_eh_about_to_do(struct ata_link *link, struct ata_device *dev, 1376c6fd2807SJeff Garzik unsigned int action) 1377c6fd2807SJeff Garzik { 1378955e57dfSTejun Heo struct ata_port *ap = link->ap; 1379955e57dfSTejun Heo struct ata_eh_info *ehi = &link->eh_info; 1380955e57dfSTejun Heo struct ata_eh_context *ehc = &link->eh_context; 1381c6fd2807SJeff Garzik unsigned long flags; 1382c6fd2807SJeff Garzik 1383c6fd2807SJeff Garzik spin_lock_irqsave(ap->lock, flags); 1384c6fd2807SJeff Garzik 1385955e57dfSTejun Heo ata_eh_clear_action(link, dev, ehi, action); 1386c6fd2807SJeff Garzik 1387a568d1d2STejun Heo /* About to take EH action, set RECOVERED. Ignore actions on 1388a568d1d2STejun Heo * slave links as master will do them again. 1389a568d1d2STejun Heo */ 1390a568d1d2STejun Heo if (!(ehc->i.flags & ATA_EHI_QUIET) && link != ap->slave_link) 1391c6fd2807SJeff Garzik ap->pflags |= ATA_PFLAG_RECOVERED; 1392c6fd2807SJeff Garzik 1393c6fd2807SJeff Garzik spin_unlock_irqrestore(ap->lock, flags); 1394c6fd2807SJeff Garzik } 1395c6fd2807SJeff Garzik 1396c6fd2807SJeff Garzik /** 1397c6fd2807SJeff Garzik * ata_eh_done - EH action complete 1398c6fd2807SJeff Garzik * @ap: target ATA port 1399c6fd2807SJeff Garzik * @dev: target ATA dev for per-dev action (can be NULL) 1400c6fd2807SJeff Garzik * @action: action just completed 1401c6fd2807SJeff Garzik * 1402c6fd2807SJeff Garzik * Called right after performing EH actions to clear related bits 1403955e57dfSTejun Heo * in @link->eh_context. 1404c6fd2807SJeff Garzik * 1405c6fd2807SJeff Garzik * LOCKING: 1406c6fd2807SJeff Garzik * None. 1407c6fd2807SJeff Garzik */ 1408fb7fd614STejun Heo void ata_eh_done(struct ata_link *link, struct ata_device *dev, 1409c6fd2807SJeff Garzik unsigned int action) 1410c6fd2807SJeff Garzik { 1411955e57dfSTejun Heo struct ata_eh_context *ehc = &link->eh_context; 14129af5c9c9STejun Heo 1413955e57dfSTejun Heo ata_eh_clear_action(link, dev, &ehc->i, action); 1414c6fd2807SJeff Garzik } 1415c6fd2807SJeff Garzik 1416c6fd2807SJeff Garzik /** 1417c6fd2807SJeff Garzik * ata_err_string - convert err_mask to descriptive string 1418c6fd2807SJeff Garzik * @err_mask: error mask to convert to string 1419c6fd2807SJeff Garzik * 1420c6fd2807SJeff Garzik * Convert @err_mask to descriptive string. Errors are 1421c6fd2807SJeff Garzik * prioritized according to severity and only the most severe 1422c6fd2807SJeff Garzik * error is reported. 1423c6fd2807SJeff Garzik * 1424c6fd2807SJeff Garzik * LOCKING: 1425c6fd2807SJeff Garzik * None. 1426c6fd2807SJeff Garzik * 1427c6fd2807SJeff Garzik * RETURNS: 1428c6fd2807SJeff Garzik * Descriptive string for @err_mask 1429c6fd2807SJeff Garzik */ 1430c6fd2807SJeff Garzik static const char *ata_err_string(unsigned int err_mask) 1431c6fd2807SJeff Garzik { 1432c6fd2807SJeff Garzik if (err_mask & AC_ERR_HOST_BUS) 1433c6fd2807SJeff Garzik return "host bus error"; 1434c6fd2807SJeff Garzik if (err_mask & AC_ERR_ATA_BUS) 1435c6fd2807SJeff Garzik return "ATA bus error"; 1436c6fd2807SJeff Garzik if (err_mask & AC_ERR_TIMEOUT) 1437c6fd2807SJeff Garzik return "timeout"; 1438c6fd2807SJeff Garzik if (err_mask & AC_ERR_HSM) 1439c6fd2807SJeff Garzik return "HSM violation"; 1440c6fd2807SJeff Garzik if (err_mask & AC_ERR_SYSTEM) 1441c6fd2807SJeff Garzik return "internal error"; 1442c6fd2807SJeff Garzik if (err_mask & AC_ERR_MEDIA) 1443c6fd2807SJeff Garzik return "media error"; 1444c6fd2807SJeff Garzik if (err_mask & AC_ERR_INVALID) 1445c6fd2807SJeff Garzik return "invalid argument"; 1446c6fd2807SJeff Garzik if (err_mask & AC_ERR_DEV) 1447c6fd2807SJeff Garzik return "device error"; 1448c6fd2807SJeff Garzik return "unknown error"; 1449c6fd2807SJeff Garzik } 1450c6fd2807SJeff Garzik 1451c6fd2807SJeff Garzik /** 1452c6fd2807SJeff Garzik * ata_read_log_page - read a specific log page 1453c6fd2807SJeff Garzik * @dev: target device 1454c6fd2807SJeff Garzik * @page: page to read 1455c6fd2807SJeff Garzik * @buf: buffer to store read page 1456c6fd2807SJeff Garzik * @sectors: number of sectors to read 1457c6fd2807SJeff Garzik * 1458c6fd2807SJeff Garzik * Read log page using READ_LOG_EXT command. 1459c6fd2807SJeff Garzik * 1460c6fd2807SJeff Garzik * LOCKING: 1461c6fd2807SJeff Garzik * Kernel thread context (may sleep). 1462c6fd2807SJeff Garzik * 1463c6fd2807SJeff Garzik * RETURNS: 1464c6fd2807SJeff Garzik * 0 on success, AC_ERR_* mask otherwise. 1465c6fd2807SJeff Garzik */ 1466c6fd2807SJeff Garzik static unsigned int ata_read_log_page(struct ata_device *dev, 1467c6fd2807SJeff Garzik u8 page, void *buf, unsigned int sectors) 1468c6fd2807SJeff Garzik { 1469c6fd2807SJeff Garzik struct ata_taskfile tf; 1470c6fd2807SJeff Garzik unsigned int err_mask; 1471c6fd2807SJeff Garzik 1472c6fd2807SJeff Garzik DPRINTK("read log page - page %d\n", page); 1473c6fd2807SJeff Garzik 1474c6fd2807SJeff Garzik ata_tf_init(dev, &tf); 1475c6fd2807SJeff Garzik tf.command = ATA_CMD_READ_LOG_EXT; 1476c6fd2807SJeff Garzik tf.lbal = page; 1477c6fd2807SJeff Garzik tf.nsect = sectors; 1478c6fd2807SJeff Garzik tf.hob_nsect = sectors >> 8; 1479c6fd2807SJeff Garzik tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_LBA48 | ATA_TFLAG_DEVICE; 1480c6fd2807SJeff Garzik tf.protocol = ATA_PROT_PIO; 1481c6fd2807SJeff Garzik 1482c6fd2807SJeff Garzik err_mask = ata_exec_internal(dev, &tf, NULL, DMA_FROM_DEVICE, 14832b789108STejun Heo buf, sectors * ATA_SECT_SIZE, 0); 1484c6fd2807SJeff Garzik 1485c6fd2807SJeff Garzik DPRINTK("EXIT, err_mask=%x\n", err_mask); 1486c6fd2807SJeff Garzik return err_mask; 1487c6fd2807SJeff Garzik } 1488c6fd2807SJeff Garzik 1489c6fd2807SJeff Garzik /** 1490c6fd2807SJeff Garzik * ata_eh_read_log_10h - Read log page 10h for NCQ error details 1491c6fd2807SJeff Garzik * @dev: Device to read log page 10h from 1492c6fd2807SJeff Garzik * @tag: Resulting tag of the failed command 1493c6fd2807SJeff Garzik * @tf: Resulting taskfile registers of the failed command 1494c6fd2807SJeff Garzik * 1495c6fd2807SJeff Garzik * Read log page 10h to obtain NCQ error details and clear error 1496c6fd2807SJeff Garzik * condition. 1497c6fd2807SJeff Garzik * 1498c6fd2807SJeff Garzik * LOCKING: 1499c6fd2807SJeff Garzik * Kernel thread context (may sleep). 1500c6fd2807SJeff Garzik * 1501c6fd2807SJeff Garzik * RETURNS: 1502c6fd2807SJeff Garzik * 0 on success, -errno otherwise. 1503c6fd2807SJeff Garzik */ 1504c6fd2807SJeff Garzik static int ata_eh_read_log_10h(struct ata_device *dev, 1505c6fd2807SJeff Garzik int *tag, struct ata_taskfile *tf) 1506c6fd2807SJeff Garzik { 15079af5c9c9STejun Heo u8 *buf = dev->link->ap->sector_buf; 1508c6fd2807SJeff Garzik unsigned int err_mask; 1509c6fd2807SJeff Garzik u8 csum; 1510c6fd2807SJeff Garzik int i; 1511c6fd2807SJeff Garzik 1512c6fd2807SJeff Garzik err_mask = ata_read_log_page(dev, ATA_LOG_SATA_NCQ, buf, 1); 1513c6fd2807SJeff Garzik if (err_mask) 1514c6fd2807SJeff Garzik return -EIO; 1515c6fd2807SJeff Garzik 1516c6fd2807SJeff Garzik csum = 0; 1517c6fd2807SJeff Garzik for (i = 0; i < ATA_SECT_SIZE; i++) 1518c6fd2807SJeff Garzik csum += buf[i]; 1519c6fd2807SJeff Garzik if (csum) 1520a9a79dfeSJoe Perches ata_dev_warn(dev, "invalid checksum 0x%x on log page 10h\n", 1521a9a79dfeSJoe Perches csum); 1522c6fd2807SJeff Garzik 1523c6fd2807SJeff Garzik if (buf[0] & 0x80) 1524c6fd2807SJeff Garzik return -ENOENT; 1525c6fd2807SJeff Garzik 1526c6fd2807SJeff Garzik *tag = buf[0] & 0x1f; 1527c6fd2807SJeff Garzik 1528c6fd2807SJeff Garzik tf->command = buf[2]; 1529c6fd2807SJeff Garzik tf->feature = buf[3]; 1530c6fd2807SJeff Garzik tf->lbal = buf[4]; 1531c6fd2807SJeff Garzik tf->lbam = buf[5]; 1532c6fd2807SJeff Garzik tf->lbah = buf[6]; 1533c6fd2807SJeff Garzik tf->device = buf[7]; 1534c6fd2807SJeff Garzik tf->hob_lbal = buf[8]; 1535c6fd2807SJeff Garzik tf->hob_lbam = buf[9]; 1536c6fd2807SJeff Garzik tf->hob_lbah = buf[10]; 1537c6fd2807SJeff Garzik tf->nsect = buf[12]; 1538c6fd2807SJeff Garzik tf->hob_nsect = buf[13]; 1539c6fd2807SJeff Garzik 1540c6fd2807SJeff Garzik return 0; 1541c6fd2807SJeff Garzik } 1542c6fd2807SJeff Garzik 1543c6fd2807SJeff Garzik /** 154411fc33daSTejun Heo * atapi_eh_tur - perform ATAPI TEST_UNIT_READY 154511fc33daSTejun Heo * @dev: target ATAPI device 154611fc33daSTejun Heo * @r_sense_key: out parameter for sense_key 154711fc33daSTejun Heo * 154811fc33daSTejun Heo * Perform ATAPI TEST_UNIT_READY. 154911fc33daSTejun Heo * 155011fc33daSTejun Heo * LOCKING: 155111fc33daSTejun Heo * EH context (may sleep). 155211fc33daSTejun Heo * 155311fc33daSTejun Heo * RETURNS: 155411fc33daSTejun Heo * 0 on success, AC_ERR_* mask on failure. 155511fc33daSTejun Heo */ 155611fc33daSTejun Heo static unsigned int atapi_eh_tur(struct ata_device *dev, u8 *r_sense_key) 155711fc33daSTejun Heo { 155811fc33daSTejun Heo u8 cdb[ATAPI_CDB_LEN] = { TEST_UNIT_READY, 0, 0, 0, 0, 0 }; 155911fc33daSTejun Heo struct ata_taskfile tf; 156011fc33daSTejun Heo unsigned int err_mask; 156111fc33daSTejun Heo 156211fc33daSTejun Heo ata_tf_init(dev, &tf); 156311fc33daSTejun Heo 156411fc33daSTejun Heo tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE; 156511fc33daSTejun Heo tf.command = ATA_CMD_PACKET; 156611fc33daSTejun Heo tf.protocol = ATAPI_PROT_NODATA; 156711fc33daSTejun Heo 156811fc33daSTejun Heo err_mask = ata_exec_internal(dev, &tf, cdb, DMA_NONE, NULL, 0, 0); 156911fc33daSTejun Heo if (err_mask == AC_ERR_DEV) 157011fc33daSTejun Heo *r_sense_key = tf.feature >> 4; 157111fc33daSTejun Heo return err_mask; 157211fc33daSTejun Heo } 157311fc33daSTejun Heo 157411fc33daSTejun Heo /** 1575c6fd2807SJeff Garzik * atapi_eh_request_sense - perform ATAPI REQUEST_SENSE 1576c6fd2807SJeff Garzik * @dev: device to perform REQUEST_SENSE to 1577c6fd2807SJeff Garzik * @sense_buf: result sense data buffer (SCSI_SENSE_BUFFERSIZE bytes long) 15783eabddb8STejun Heo * @dfl_sense_key: default sense key to use 1579c6fd2807SJeff Garzik * 1580c6fd2807SJeff Garzik * Perform ATAPI REQUEST_SENSE after the device reported CHECK 1581c6fd2807SJeff Garzik * SENSE. This function is EH helper. 1582c6fd2807SJeff Garzik * 1583c6fd2807SJeff Garzik * LOCKING: 1584c6fd2807SJeff Garzik * Kernel thread context (may sleep). 1585c6fd2807SJeff Garzik * 1586c6fd2807SJeff Garzik * RETURNS: 1587c6fd2807SJeff Garzik * 0 on success, AC_ERR_* mask on failure 1588c6fd2807SJeff Garzik */ 15893eabddb8STejun Heo static unsigned int atapi_eh_request_sense(struct ata_device *dev, 15903eabddb8STejun Heo u8 *sense_buf, u8 dfl_sense_key) 1591c6fd2807SJeff Garzik { 15923eabddb8STejun Heo u8 cdb[ATAPI_CDB_LEN] = 15933eabddb8STejun Heo { REQUEST_SENSE, 0, 0, 0, SCSI_SENSE_BUFFERSIZE, 0 }; 15949af5c9c9STejun Heo struct ata_port *ap = dev->link->ap; 1595c6fd2807SJeff Garzik struct ata_taskfile tf; 1596c6fd2807SJeff Garzik 1597c6fd2807SJeff Garzik DPRINTK("ATAPI request sense\n"); 1598c6fd2807SJeff Garzik 1599c6fd2807SJeff Garzik /* FIXME: is this needed? */ 1600c6fd2807SJeff Garzik memset(sense_buf, 0, SCSI_SENSE_BUFFERSIZE); 1601c6fd2807SJeff Garzik 160256287768SAlbert Lee /* initialize sense_buf with the error register, 160356287768SAlbert Lee * for the case where they are -not- overwritten 160456287768SAlbert Lee */ 1605c6fd2807SJeff Garzik sense_buf[0] = 0x70; 16063eabddb8STejun Heo sense_buf[2] = dfl_sense_key; 160756287768SAlbert Lee 160856287768SAlbert Lee /* some devices time out if garbage left in tf */ 160956287768SAlbert Lee ata_tf_init(dev, &tf); 1610c6fd2807SJeff Garzik 1611c6fd2807SJeff Garzik tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE; 1612c6fd2807SJeff Garzik tf.command = ATA_CMD_PACKET; 1613c6fd2807SJeff Garzik 1614c6fd2807SJeff Garzik /* is it pointless to prefer PIO for "safety reasons"? */ 1615c6fd2807SJeff Garzik if (ap->flags & ATA_FLAG_PIO_DMA) { 16160dc36888STejun Heo tf.protocol = ATAPI_PROT_DMA; 1617c6fd2807SJeff Garzik tf.feature |= ATAPI_PKT_DMA; 1618c6fd2807SJeff Garzik } else { 16190dc36888STejun Heo tf.protocol = ATAPI_PROT_PIO; 1620f2dfc1a1STejun Heo tf.lbam = SCSI_SENSE_BUFFERSIZE; 1621f2dfc1a1STejun Heo tf.lbah = 0; 1622c6fd2807SJeff Garzik } 1623c6fd2807SJeff Garzik 1624c6fd2807SJeff Garzik return ata_exec_internal(dev, &tf, cdb, DMA_FROM_DEVICE, 16252b789108STejun Heo sense_buf, SCSI_SENSE_BUFFERSIZE, 0); 1626c6fd2807SJeff Garzik } 1627c6fd2807SJeff Garzik 1628c6fd2807SJeff Garzik /** 1629c6fd2807SJeff Garzik * ata_eh_analyze_serror - analyze SError for a failed port 16300260731fSTejun Heo * @link: ATA link to analyze SError for 1631c6fd2807SJeff Garzik * 1632c6fd2807SJeff Garzik * Analyze SError if available and further determine cause of 1633c6fd2807SJeff Garzik * failure. 1634c6fd2807SJeff Garzik * 1635c6fd2807SJeff Garzik * LOCKING: 1636c6fd2807SJeff Garzik * None. 1637c6fd2807SJeff Garzik */ 16380260731fSTejun Heo static void ata_eh_analyze_serror(struct ata_link *link) 1639c6fd2807SJeff Garzik { 16400260731fSTejun Heo struct ata_eh_context *ehc = &link->eh_context; 1641c6fd2807SJeff Garzik u32 serror = ehc->i.serror; 1642c6fd2807SJeff Garzik unsigned int err_mask = 0, action = 0; 1643f9df58cbSTejun Heo u32 hotplug_mask; 1644c6fd2807SJeff Garzik 1645e0614db2STejun Heo if (serror & (SERR_PERSISTENT | SERR_DATA)) { 1646c6fd2807SJeff Garzik err_mask |= AC_ERR_ATA_BUS; 1647cf480626STejun Heo action |= ATA_EH_RESET; 1648c6fd2807SJeff Garzik } 1649c6fd2807SJeff Garzik if (serror & SERR_PROTOCOL) { 1650c6fd2807SJeff Garzik err_mask |= AC_ERR_HSM; 1651cf480626STejun Heo action |= ATA_EH_RESET; 1652c6fd2807SJeff Garzik } 1653c6fd2807SJeff Garzik if (serror & SERR_INTERNAL) { 1654c6fd2807SJeff Garzik err_mask |= AC_ERR_SYSTEM; 1655cf480626STejun Heo action |= ATA_EH_RESET; 1656c6fd2807SJeff Garzik } 1657f9df58cbSTejun Heo 1658f9df58cbSTejun Heo /* Determine whether a hotplug event has occurred. Both 1659f9df58cbSTejun Heo * SError.N/X are considered hotplug events for enabled or 1660f9df58cbSTejun Heo * host links. For disabled PMP links, only N bit is 1661f9df58cbSTejun Heo * considered as X bit is left at 1 for link plugging. 1662f9df58cbSTejun Heo */ 1663eb0e85e3STejun Heo if (link->lpm_policy > ATA_LPM_MAX_POWER) 16646b7ae954STejun Heo hotplug_mask = 0; /* hotplug doesn't work w/ LPM */ 16656b7ae954STejun Heo else if (!(link->flags & ATA_LFLAG_DISABLED) || ata_is_host_link(link)) 1666f9df58cbSTejun Heo hotplug_mask = SERR_PHYRDY_CHG | SERR_DEV_XCHG; 1667f9df58cbSTejun Heo else 1668f9df58cbSTejun Heo hotplug_mask = SERR_PHYRDY_CHG; 1669f9df58cbSTejun Heo 1670f9df58cbSTejun Heo if (serror & hotplug_mask) 1671c6fd2807SJeff Garzik ata_ehi_hotplugged(&ehc->i); 1672c6fd2807SJeff Garzik 1673c6fd2807SJeff Garzik ehc->i.err_mask |= err_mask; 1674c6fd2807SJeff Garzik ehc->i.action |= action; 1675c6fd2807SJeff Garzik } 1676c6fd2807SJeff Garzik 1677c6fd2807SJeff Garzik /** 1678c6fd2807SJeff Garzik * ata_eh_analyze_ncq_error - analyze NCQ error 16790260731fSTejun Heo * @link: ATA link to analyze NCQ error for 1680c6fd2807SJeff Garzik * 1681c6fd2807SJeff Garzik * Read log page 10h, determine the offending qc and acquire 1682c6fd2807SJeff Garzik * error status TF. For NCQ device errors, all LLDDs have to do 1683c6fd2807SJeff Garzik * is setting AC_ERR_DEV in ehi->err_mask. This function takes 1684c6fd2807SJeff Garzik * care of the rest. 1685c6fd2807SJeff Garzik * 1686c6fd2807SJeff Garzik * LOCKING: 1687c6fd2807SJeff Garzik * Kernel thread context (may sleep). 1688c6fd2807SJeff Garzik */ 168910acf3b0SMark Lord void ata_eh_analyze_ncq_error(struct ata_link *link) 1690c6fd2807SJeff Garzik { 16910260731fSTejun Heo struct ata_port *ap = link->ap; 16920260731fSTejun Heo struct ata_eh_context *ehc = &link->eh_context; 16930260731fSTejun Heo struct ata_device *dev = link->device; 1694c6fd2807SJeff Garzik struct ata_queued_cmd *qc; 1695c6fd2807SJeff Garzik struct ata_taskfile tf; 1696c6fd2807SJeff Garzik int tag, rc; 1697c6fd2807SJeff Garzik 1698c6fd2807SJeff Garzik /* if frozen, we can't do much */ 1699c6fd2807SJeff Garzik if (ap->pflags & ATA_PFLAG_FROZEN) 1700c6fd2807SJeff Garzik return; 1701c6fd2807SJeff Garzik 1702c6fd2807SJeff Garzik /* is it NCQ device error? */ 17030260731fSTejun Heo if (!link->sactive || !(ehc->i.err_mask & AC_ERR_DEV)) 1704c6fd2807SJeff Garzik return; 1705c6fd2807SJeff Garzik 1706c6fd2807SJeff Garzik /* has LLDD analyzed already? */ 1707c6fd2807SJeff Garzik for (tag = 0; tag < ATA_MAX_QUEUE; tag++) { 1708c6fd2807SJeff Garzik qc = __ata_qc_from_tag(ap, tag); 1709c6fd2807SJeff Garzik 1710c6fd2807SJeff Garzik if (!(qc->flags & ATA_QCFLAG_FAILED)) 1711c6fd2807SJeff Garzik continue; 1712c6fd2807SJeff Garzik 1713c6fd2807SJeff Garzik if (qc->err_mask) 1714c6fd2807SJeff Garzik return; 1715c6fd2807SJeff Garzik } 1716c6fd2807SJeff Garzik 1717c6fd2807SJeff Garzik /* okay, this error is ours */ 1718a09bf4cdSJeff Garzik memset(&tf, 0, sizeof(tf)); 1719c6fd2807SJeff Garzik rc = ata_eh_read_log_10h(dev, &tag, &tf); 1720c6fd2807SJeff Garzik if (rc) { 1721a9a79dfeSJoe Perches ata_link_err(link, "failed to read log page 10h (errno=%d)\n", 1722a9a79dfeSJoe Perches rc); 1723c6fd2807SJeff Garzik return; 1724c6fd2807SJeff Garzik } 1725c6fd2807SJeff Garzik 17260260731fSTejun Heo if (!(link->sactive & (1 << tag))) { 1727a9a79dfeSJoe Perches ata_link_err(link, "log page 10h reported inactive tag %d\n", 1728a9a79dfeSJoe Perches tag); 1729c6fd2807SJeff Garzik return; 1730c6fd2807SJeff Garzik } 1731c6fd2807SJeff Garzik 1732c6fd2807SJeff Garzik /* we've got the perpetrator, condemn it */ 1733c6fd2807SJeff Garzik qc = __ata_qc_from_tag(ap, tag); 1734c6fd2807SJeff Garzik memcpy(&qc->result_tf, &tf, sizeof(tf)); 1735a6116c9eSMark Lord qc->result_tf.flags = ATA_TFLAG_ISADDR | ATA_TFLAG_LBA | ATA_TFLAG_LBA48; 17365335b729STejun Heo qc->err_mask |= AC_ERR_DEV | AC_ERR_NCQ; 1737c6fd2807SJeff Garzik ehc->i.err_mask &= ~AC_ERR_DEV; 1738c6fd2807SJeff Garzik } 1739c6fd2807SJeff Garzik 1740c6fd2807SJeff Garzik /** 1741c6fd2807SJeff Garzik * ata_eh_analyze_tf - analyze taskfile of a failed qc 1742c6fd2807SJeff Garzik * @qc: qc to analyze 1743c6fd2807SJeff Garzik * @tf: Taskfile registers to analyze 1744c6fd2807SJeff Garzik * 1745c6fd2807SJeff Garzik * Analyze taskfile of @qc and further determine cause of 1746c6fd2807SJeff Garzik * failure. This function also requests ATAPI sense data if 174725985edcSLucas De Marchi * available. 1748c6fd2807SJeff Garzik * 1749c6fd2807SJeff Garzik * LOCKING: 1750c6fd2807SJeff Garzik * Kernel thread context (may sleep). 1751c6fd2807SJeff Garzik * 1752c6fd2807SJeff Garzik * RETURNS: 1753c6fd2807SJeff Garzik * Determined recovery action 1754c6fd2807SJeff Garzik */ 1755c6fd2807SJeff Garzik static unsigned int ata_eh_analyze_tf(struct ata_queued_cmd *qc, 1756c6fd2807SJeff Garzik const struct ata_taskfile *tf) 1757c6fd2807SJeff Garzik { 1758c6fd2807SJeff Garzik unsigned int tmp, action = 0; 1759c6fd2807SJeff Garzik u8 stat = tf->command, err = tf->feature; 1760c6fd2807SJeff Garzik 1761c6fd2807SJeff Garzik if ((stat & (ATA_BUSY | ATA_DRQ | ATA_DRDY)) != ATA_DRDY) { 1762c6fd2807SJeff Garzik qc->err_mask |= AC_ERR_HSM; 1763cf480626STejun Heo return ATA_EH_RESET; 1764c6fd2807SJeff Garzik } 1765c6fd2807SJeff Garzik 1766a51d644aSTejun Heo if (stat & (ATA_ERR | ATA_DF)) 1767a51d644aSTejun Heo qc->err_mask |= AC_ERR_DEV; 1768a51d644aSTejun Heo else 1769c6fd2807SJeff Garzik return 0; 1770c6fd2807SJeff Garzik 1771c6fd2807SJeff Garzik switch (qc->dev->class) { 1772c6fd2807SJeff Garzik case ATA_DEV_ATA: 1773c6fd2807SJeff Garzik if (err & ATA_ICRC) 1774c6fd2807SJeff Garzik qc->err_mask |= AC_ERR_ATA_BUS; 1775c6fd2807SJeff Garzik if (err & ATA_UNC) 1776c6fd2807SJeff Garzik qc->err_mask |= AC_ERR_MEDIA; 1777c6fd2807SJeff Garzik if (err & ATA_IDNF) 1778c6fd2807SJeff Garzik qc->err_mask |= AC_ERR_INVALID; 1779c6fd2807SJeff Garzik break; 1780c6fd2807SJeff Garzik 1781c6fd2807SJeff Garzik case ATA_DEV_ATAPI: 1782a569a30dSTejun Heo if (!(qc->ap->pflags & ATA_PFLAG_FROZEN)) { 17833eabddb8STejun Heo tmp = atapi_eh_request_sense(qc->dev, 17843eabddb8STejun Heo qc->scsicmd->sense_buffer, 17853eabddb8STejun Heo qc->result_tf.feature >> 4); 1786c6fd2807SJeff Garzik if (!tmp) { 1787a569a30dSTejun Heo /* ATA_QCFLAG_SENSE_VALID is used to 1788a569a30dSTejun Heo * tell atapi_qc_complete() that sense 1789a569a30dSTejun Heo * data is already valid. 1790c6fd2807SJeff Garzik * 1791c6fd2807SJeff Garzik * TODO: interpret sense data and set 1792c6fd2807SJeff Garzik * appropriate err_mask. 1793c6fd2807SJeff Garzik */ 1794c6fd2807SJeff Garzik qc->flags |= ATA_QCFLAG_SENSE_VALID; 1795c6fd2807SJeff Garzik } else 1796c6fd2807SJeff Garzik qc->err_mask |= tmp; 1797c6fd2807SJeff Garzik } 1798a569a30dSTejun Heo } 1799c6fd2807SJeff Garzik 1800c6fd2807SJeff Garzik if (qc->err_mask & (AC_ERR_HSM | AC_ERR_TIMEOUT | AC_ERR_ATA_BUS)) 1801cf480626STejun Heo action |= ATA_EH_RESET; 1802c6fd2807SJeff Garzik 1803c6fd2807SJeff Garzik return action; 1804c6fd2807SJeff Garzik } 1805c6fd2807SJeff Garzik 180676326ac1STejun Heo static int ata_eh_categorize_error(unsigned int eflags, unsigned int err_mask, 180776326ac1STejun Heo int *xfer_ok) 1808c6fd2807SJeff Garzik { 180976326ac1STejun Heo int base = 0; 181076326ac1STejun Heo 181176326ac1STejun Heo if (!(eflags & ATA_EFLAG_DUBIOUS_XFER)) 181276326ac1STejun Heo *xfer_ok = 1; 181376326ac1STejun Heo 181476326ac1STejun Heo if (!*xfer_ok) 181575f9cafcSTejun Heo base = ATA_ECAT_DUBIOUS_NONE; 181676326ac1STejun Heo 18177d47e8d4STejun Heo if (err_mask & AC_ERR_ATA_BUS) 181876326ac1STejun Heo return base + ATA_ECAT_ATA_BUS; 1819c6fd2807SJeff Garzik 18207d47e8d4STejun Heo if (err_mask & AC_ERR_TIMEOUT) 182176326ac1STejun Heo return base + ATA_ECAT_TOUT_HSM; 18227d47e8d4STejun Heo 18233884f7b0STejun Heo if (eflags & ATA_EFLAG_IS_IO) { 18247d47e8d4STejun Heo if (err_mask & AC_ERR_HSM) 182576326ac1STejun Heo return base + ATA_ECAT_TOUT_HSM; 18267d47e8d4STejun Heo if ((err_mask & 18277d47e8d4STejun Heo (AC_ERR_DEV|AC_ERR_MEDIA|AC_ERR_INVALID)) == AC_ERR_DEV) 182876326ac1STejun Heo return base + ATA_ECAT_UNK_DEV; 1829c6fd2807SJeff Garzik } 1830c6fd2807SJeff Garzik 1831c6fd2807SJeff Garzik return 0; 1832c6fd2807SJeff Garzik } 1833c6fd2807SJeff Garzik 18347d47e8d4STejun Heo struct speed_down_verdict_arg { 1835c6fd2807SJeff Garzik u64 since; 183676326ac1STejun Heo int xfer_ok; 18373884f7b0STejun Heo int nr_errors[ATA_ECAT_NR]; 1838c6fd2807SJeff Garzik }; 1839c6fd2807SJeff Garzik 18407d47e8d4STejun Heo static int speed_down_verdict_cb(struct ata_ering_entry *ent, void *void_arg) 1841c6fd2807SJeff Garzik { 18427d47e8d4STejun Heo struct speed_down_verdict_arg *arg = void_arg; 184376326ac1STejun Heo int cat; 1844c6fd2807SJeff Garzik 1845d9027470SGwendal Grignou if ((ent->eflags & ATA_EFLAG_OLD_ER) || (ent->timestamp < arg->since)) 1846c6fd2807SJeff Garzik return -1; 1847c6fd2807SJeff Garzik 184876326ac1STejun Heo cat = ata_eh_categorize_error(ent->eflags, ent->err_mask, 184976326ac1STejun Heo &arg->xfer_ok); 18507d47e8d4STejun Heo arg->nr_errors[cat]++; 185176326ac1STejun Heo 1852c6fd2807SJeff Garzik return 0; 1853c6fd2807SJeff Garzik } 1854c6fd2807SJeff Garzik 1855c6fd2807SJeff Garzik /** 18567d47e8d4STejun Heo * ata_eh_speed_down_verdict - Determine speed down verdict 1857c6fd2807SJeff Garzik * @dev: Device of interest 1858c6fd2807SJeff Garzik * 1859c6fd2807SJeff Garzik * This function examines error ring of @dev and determines 18607d47e8d4STejun Heo * whether NCQ needs to be turned off, transfer speed should be 18617d47e8d4STejun Heo * stepped down, or falling back to PIO is necessary. 1862c6fd2807SJeff Garzik * 18633884f7b0STejun Heo * ECAT_ATA_BUS : ATA_BUS error for any command 1864c6fd2807SJeff Garzik * 18653884f7b0STejun Heo * ECAT_TOUT_HSM : TIMEOUT for any command or HSM violation for 18663884f7b0STejun Heo * IO commands 18677d47e8d4STejun Heo * 18683884f7b0STejun Heo * ECAT_UNK_DEV : Unknown DEV error for IO commands 1869c6fd2807SJeff Garzik * 187076326ac1STejun Heo * ECAT_DUBIOUS_* : Identical to above three but occurred while 187176326ac1STejun Heo * data transfer hasn't been verified. 187276326ac1STejun Heo * 18733884f7b0STejun Heo * Verdicts are 18747d47e8d4STejun Heo * 18753884f7b0STejun Heo * NCQ_OFF : Turn off NCQ. 18767d47e8d4STejun Heo * 18773884f7b0STejun Heo * SPEED_DOWN : Speed down transfer speed but don't fall back 18783884f7b0STejun Heo * to PIO. 18793884f7b0STejun Heo * 18803884f7b0STejun Heo * FALLBACK_TO_PIO : Fall back to PIO. 18813884f7b0STejun Heo * 18823884f7b0STejun Heo * Even if multiple verdicts are returned, only one action is 188376326ac1STejun Heo * taken per error. An action triggered by non-DUBIOUS errors 188476326ac1STejun Heo * clears ering, while one triggered by DUBIOUS_* errors doesn't. 188576326ac1STejun Heo * This is to expedite speed down decisions right after device is 188676326ac1STejun Heo * initially configured. 18873884f7b0STejun Heo * 188876326ac1STejun Heo * The followings are speed down rules. #1 and #2 deal with 188976326ac1STejun Heo * DUBIOUS errors. 189076326ac1STejun Heo * 189176326ac1STejun Heo * 1. If more than one DUBIOUS_ATA_BUS or DUBIOUS_TOUT_HSM errors 189276326ac1STejun Heo * occurred during last 5 mins, SPEED_DOWN and FALLBACK_TO_PIO. 189376326ac1STejun Heo * 189476326ac1STejun Heo * 2. If more than one DUBIOUS_TOUT_HSM or DUBIOUS_UNK_DEV errors 189576326ac1STejun Heo * occurred during last 5 mins, NCQ_OFF. 189676326ac1STejun Heo * 189776326ac1STejun Heo * 3. If more than 8 ATA_BUS, TOUT_HSM or UNK_DEV errors 189825985edcSLucas De Marchi * occurred during last 5 mins, FALLBACK_TO_PIO 18993884f7b0STejun Heo * 190076326ac1STejun Heo * 4. If more than 3 TOUT_HSM or UNK_DEV errors occurred 19013884f7b0STejun Heo * during last 10 mins, NCQ_OFF. 19023884f7b0STejun Heo * 190376326ac1STejun Heo * 5. If more than 3 ATA_BUS or TOUT_HSM errors, or more than 6 19043884f7b0STejun Heo * UNK_DEV errors occurred during last 10 mins, SPEED_DOWN. 19057d47e8d4STejun Heo * 1906c6fd2807SJeff Garzik * LOCKING: 1907c6fd2807SJeff Garzik * Inherited from caller. 1908c6fd2807SJeff Garzik * 1909c6fd2807SJeff Garzik * RETURNS: 19107d47e8d4STejun Heo * OR of ATA_EH_SPDN_* flags. 1911c6fd2807SJeff Garzik */ 19127d47e8d4STejun Heo static unsigned int ata_eh_speed_down_verdict(struct ata_device *dev) 1913c6fd2807SJeff Garzik { 19147d47e8d4STejun Heo const u64 j5mins = 5LLU * 60 * HZ, j10mins = 10LLU * 60 * HZ; 19157d47e8d4STejun Heo u64 j64 = get_jiffies_64(); 19167d47e8d4STejun Heo struct speed_down_verdict_arg arg; 19177d47e8d4STejun Heo unsigned int verdict = 0; 1918c6fd2807SJeff Garzik 19193884f7b0STejun Heo /* scan past 5 mins of error history */ 19203884f7b0STejun Heo memset(&arg, 0, sizeof(arg)); 19213884f7b0STejun Heo arg.since = j64 - min(j64, j5mins); 19223884f7b0STejun Heo ata_ering_map(&dev->ering, speed_down_verdict_cb, &arg); 19233884f7b0STejun Heo 192476326ac1STejun Heo if (arg.nr_errors[ATA_ECAT_DUBIOUS_ATA_BUS] + 192576326ac1STejun Heo arg.nr_errors[ATA_ECAT_DUBIOUS_TOUT_HSM] > 1) 192676326ac1STejun Heo verdict |= ATA_EH_SPDN_SPEED_DOWN | 192776326ac1STejun Heo ATA_EH_SPDN_FALLBACK_TO_PIO | ATA_EH_SPDN_KEEP_ERRORS; 192876326ac1STejun Heo 192976326ac1STejun Heo if (arg.nr_errors[ATA_ECAT_DUBIOUS_TOUT_HSM] + 193076326ac1STejun Heo arg.nr_errors[ATA_ECAT_DUBIOUS_UNK_DEV] > 1) 193176326ac1STejun Heo verdict |= ATA_EH_SPDN_NCQ_OFF | ATA_EH_SPDN_KEEP_ERRORS; 193276326ac1STejun Heo 19333884f7b0STejun Heo if (arg.nr_errors[ATA_ECAT_ATA_BUS] + 19343884f7b0STejun Heo arg.nr_errors[ATA_ECAT_TOUT_HSM] + 1935663f99b8STejun Heo arg.nr_errors[ATA_ECAT_UNK_DEV] > 6) 19363884f7b0STejun Heo verdict |= ATA_EH_SPDN_FALLBACK_TO_PIO; 19373884f7b0STejun Heo 19387d47e8d4STejun Heo /* scan past 10 mins of error history */ 1939c6fd2807SJeff Garzik memset(&arg, 0, sizeof(arg)); 19407d47e8d4STejun Heo arg.since = j64 - min(j64, j10mins); 19417d47e8d4STejun Heo ata_ering_map(&dev->ering, speed_down_verdict_cb, &arg); 1942c6fd2807SJeff Garzik 19433884f7b0STejun Heo if (arg.nr_errors[ATA_ECAT_TOUT_HSM] + 19443884f7b0STejun Heo arg.nr_errors[ATA_ECAT_UNK_DEV] > 3) 19457d47e8d4STejun Heo verdict |= ATA_EH_SPDN_NCQ_OFF; 19463884f7b0STejun Heo 19473884f7b0STejun Heo if (arg.nr_errors[ATA_ECAT_ATA_BUS] + 19483884f7b0STejun Heo arg.nr_errors[ATA_ECAT_TOUT_HSM] > 3 || 1949663f99b8STejun Heo arg.nr_errors[ATA_ECAT_UNK_DEV] > 6) 19507d47e8d4STejun Heo verdict |= ATA_EH_SPDN_SPEED_DOWN; 1951c6fd2807SJeff Garzik 19527d47e8d4STejun Heo return verdict; 1953c6fd2807SJeff Garzik } 1954c6fd2807SJeff Garzik 1955c6fd2807SJeff Garzik /** 1956c6fd2807SJeff Garzik * ata_eh_speed_down - record error and speed down if necessary 1957c6fd2807SJeff Garzik * @dev: Failed device 19583884f7b0STejun Heo * @eflags: mask of ATA_EFLAG_* flags 1959c6fd2807SJeff Garzik * @err_mask: err_mask of the error 1960c6fd2807SJeff Garzik * 1961c6fd2807SJeff Garzik * Record error and examine error history to determine whether 1962c6fd2807SJeff Garzik * adjusting transmission speed is necessary. It also sets 1963c6fd2807SJeff Garzik * transmission limits appropriately if such adjustment is 1964c6fd2807SJeff Garzik * necessary. 1965c6fd2807SJeff Garzik * 1966c6fd2807SJeff Garzik * LOCKING: 1967c6fd2807SJeff Garzik * Kernel thread context (may sleep). 1968c6fd2807SJeff Garzik * 1969c6fd2807SJeff Garzik * RETURNS: 19707d47e8d4STejun Heo * Determined recovery action. 1971c6fd2807SJeff Garzik */ 19723884f7b0STejun Heo static unsigned int ata_eh_speed_down(struct ata_device *dev, 19733884f7b0STejun Heo unsigned int eflags, unsigned int err_mask) 1974c6fd2807SJeff Garzik { 1975b1c72916STejun Heo struct ata_link *link = ata_dev_phys_link(dev); 197676326ac1STejun Heo int xfer_ok = 0; 19777d47e8d4STejun Heo unsigned int verdict; 19787d47e8d4STejun Heo unsigned int action = 0; 19797d47e8d4STejun Heo 19807d47e8d4STejun Heo /* don't bother if Cat-0 error */ 198176326ac1STejun Heo if (ata_eh_categorize_error(eflags, err_mask, &xfer_ok) == 0) 1982c6fd2807SJeff Garzik return 0; 1983c6fd2807SJeff Garzik 1984c6fd2807SJeff Garzik /* record error and determine whether speed down is necessary */ 19853884f7b0STejun Heo ata_ering_record(&dev->ering, eflags, err_mask); 19867d47e8d4STejun Heo verdict = ata_eh_speed_down_verdict(dev); 1987c6fd2807SJeff Garzik 19887d47e8d4STejun Heo /* turn off NCQ? */ 19897d47e8d4STejun Heo if ((verdict & ATA_EH_SPDN_NCQ_OFF) && 19907d47e8d4STejun Heo (dev->flags & (ATA_DFLAG_PIO | ATA_DFLAG_NCQ | 19917d47e8d4STejun Heo ATA_DFLAG_NCQ_OFF)) == ATA_DFLAG_NCQ) { 19927d47e8d4STejun Heo dev->flags |= ATA_DFLAG_NCQ_OFF; 1993a9a79dfeSJoe Perches ata_dev_warn(dev, "NCQ disabled due to excessive errors\n"); 19947d47e8d4STejun Heo goto done; 19957d47e8d4STejun Heo } 1996c6fd2807SJeff Garzik 19977d47e8d4STejun Heo /* speed down? */ 19987d47e8d4STejun Heo if (verdict & ATA_EH_SPDN_SPEED_DOWN) { 1999c6fd2807SJeff Garzik /* speed down SATA link speed if possible */ 2000a07d499bSTejun Heo if (sata_down_spd_limit(link, 0) == 0) { 2001cf480626STejun Heo action |= ATA_EH_RESET; 20027d47e8d4STejun Heo goto done; 20037d47e8d4STejun Heo } 2004c6fd2807SJeff Garzik 2005c6fd2807SJeff Garzik /* lower transfer mode */ 20067d47e8d4STejun Heo if (dev->spdn_cnt < 2) { 20077d47e8d4STejun Heo static const int dma_dnxfer_sel[] = 20087d47e8d4STejun Heo { ATA_DNXFER_DMA, ATA_DNXFER_40C }; 20097d47e8d4STejun Heo static const int pio_dnxfer_sel[] = 20107d47e8d4STejun Heo { ATA_DNXFER_PIO, ATA_DNXFER_FORCE_PIO0 }; 20117d47e8d4STejun Heo int sel; 2012c6fd2807SJeff Garzik 20137d47e8d4STejun Heo if (dev->xfer_shift != ATA_SHIFT_PIO) 20147d47e8d4STejun Heo sel = dma_dnxfer_sel[dev->spdn_cnt]; 20157d47e8d4STejun Heo else 20167d47e8d4STejun Heo sel = pio_dnxfer_sel[dev->spdn_cnt]; 20177d47e8d4STejun Heo 20187d47e8d4STejun Heo dev->spdn_cnt++; 20197d47e8d4STejun Heo 20207d47e8d4STejun Heo if (ata_down_xfermask_limit(dev, sel) == 0) { 2021cf480626STejun Heo action |= ATA_EH_RESET; 20227d47e8d4STejun Heo goto done; 20237d47e8d4STejun Heo } 20247d47e8d4STejun Heo } 20257d47e8d4STejun Heo } 20267d47e8d4STejun Heo 20277d47e8d4STejun Heo /* Fall back to PIO? Slowing down to PIO is meaningless for 2028663f99b8STejun Heo * SATA ATA devices. Consider it only for PATA and SATAPI. 20297d47e8d4STejun Heo */ 20307d47e8d4STejun Heo if ((verdict & ATA_EH_SPDN_FALLBACK_TO_PIO) && (dev->spdn_cnt >= 2) && 2031663f99b8STejun Heo (link->ap->cbl != ATA_CBL_SATA || dev->class == ATA_DEV_ATAPI) && 20327d47e8d4STejun Heo (dev->xfer_shift != ATA_SHIFT_PIO)) { 20337d47e8d4STejun Heo if (ata_down_xfermask_limit(dev, ATA_DNXFER_FORCE_PIO) == 0) { 20347d47e8d4STejun Heo dev->spdn_cnt = 0; 2035cf480626STejun Heo action |= ATA_EH_RESET; 20367d47e8d4STejun Heo goto done; 20377d47e8d4STejun Heo } 20387d47e8d4STejun Heo } 20397d47e8d4STejun Heo 2040c6fd2807SJeff Garzik return 0; 20417d47e8d4STejun Heo done: 20427d47e8d4STejun Heo /* device has been slowed down, blow error history */ 204376326ac1STejun Heo if (!(verdict & ATA_EH_SPDN_KEEP_ERRORS)) 20447d47e8d4STejun Heo ata_ering_clear(&dev->ering); 20457d47e8d4STejun Heo return action; 2046c6fd2807SJeff Garzik } 2047c6fd2807SJeff Garzik 2048c6fd2807SJeff Garzik /** 20499b1e2658STejun Heo * ata_eh_link_autopsy - analyze error and determine recovery action 20509b1e2658STejun Heo * @link: host link to perform autopsy on 2051c6fd2807SJeff Garzik * 20520260731fSTejun Heo * Analyze why @link failed and determine which recovery actions 20530260731fSTejun Heo * are needed. This function also sets more detailed AC_ERR_* 20540260731fSTejun Heo * values and fills sense data for ATAPI CHECK SENSE. 2055c6fd2807SJeff Garzik * 2056c6fd2807SJeff Garzik * LOCKING: 2057c6fd2807SJeff Garzik * Kernel thread context (may sleep). 2058c6fd2807SJeff Garzik */ 20599b1e2658STejun Heo static void ata_eh_link_autopsy(struct ata_link *link) 2060c6fd2807SJeff Garzik { 20610260731fSTejun Heo struct ata_port *ap = link->ap; 2062936fd732STejun Heo struct ata_eh_context *ehc = &link->eh_context; 2063dfcc173dSTejun Heo struct ata_device *dev; 20643884f7b0STejun Heo unsigned int all_err_mask = 0, eflags = 0; 20653884f7b0STejun Heo int tag; 2066c6fd2807SJeff Garzik u32 serror; 2067c6fd2807SJeff Garzik int rc; 2068c6fd2807SJeff Garzik 2069c6fd2807SJeff Garzik DPRINTK("ENTER\n"); 2070c6fd2807SJeff Garzik 2071c6fd2807SJeff Garzik if (ehc->i.flags & ATA_EHI_NO_AUTOPSY) 2072c6fd2807SJeff Garzik return; 2073c6fd2807SJeff Garzik 2074c6fd2807SJeff Garzik /* obtain and analyze SError */ 2075936fd732STejun Heo rc = sata_scr_read(link, SCR_ERROR, &serror); 2076c6fd2807SJeff Garzik if (rc == 0) { 2077c6fd2807SJeff Garzik ehc->i.serror |= serror; 20780260731fSTejun Heo ata_eh_analyze_serror(link); 20794e57c517STejun Heo } else if (rc != -EOPNOTSUPP) { 2080cf480626STejun Heo /* SError read failed, force reset and probing */ 2081b558edddSTejun Heo ehc->i.probe_mask |= ATA_ALL_DEVICES; 2082cf480626STejun Heo ehc->i.action |= ATA_EH_RESET; 20834e57c517STejun Heo ehc->i.err_mask |= AC_ERR_OTHER; 20844e57c517STejun Heo } 2085c6fd2807SJeff Garzik 2086c6fd2807SJeff Garzik /* analyze NCQ failure */ 20870260731fSTejun Heo ata_eh_analyze_ncq_error(link); 2088c6fd2807SJeff Garzik 2089c6fd2807SJeff Garzik /* any real error trumps AC_ERR_OTHER */ 2090c6fd2807SJeff Garzik if (ehc->i.err_mask & ~AC_ERR_OTHER) 2091c6fd2807SJeff Garzik ehc->i.err_mask &= ~AC_ERR_OTHER; 2092c6fd2807SJeff Garzik 2093c6fd2807SJeff Garzik all_err_mask |= ehc->i.err_mask; 2094c6fd2807SJeff Garzik 2095c6fd2807SJeff Garzik for (tag = 0; tag < ATA_MAX_QUEUE; tag++) { 2096c6fd2807SJeff Garzik struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag); 2097c6fd2807SJeff Garzik 2098b1c72916STejun Heo if (!(qc->flags & ATA_QCFLAG_FAILED) || 2099b1c72916STejun Heo ata_dev_phys_link(qc->dev) != link) 2100c6fd2807SJeff Garzik continue; 2101c6fd2807SJeff Garzik 2102c6fd2807SJeff Garzik /* inherit upper level err_mask */ 2103c6fd2807SJeff Garzik qc->err_mask |= ehc->i.err_mask; 2104c6fd2807SJeff Garzik 2105c6fd2807SJeff Garzik /* analyze TF */ 2106c6fd2807SJeff Garzik ehc->i.action |= ata_eh_analyze_tf(qc, &qc->result_tf); 2107c6fd2807SJeff Garzik 2108c6fd2807SJeff Garzik /* DEV errors are probably spurious in case of ATA_BUS error */ 2109c6fd2807SJeff Garzik if (qc->err_mask & AC_ERR_ATA_BUS) 2110c6fd2807SJeff Garzik qc->err_mask &= ~(AC_ERR_DEV | AC_ERR_MEDIA | 2111c6fd2807SJeff Garzik AC_ERR_INVALID); 2112c6fd2807SJeff Garzik 2113c6fd2807SJeff Garzik /* any real error trumps unknown error */ 2114c6fd2807SJeff Garzik if (qc->err_mask & ~AC_ERR_OTHER) 2115c6fd2807SJeff Garzik qc->err_mask &= ~AC_ERR_OTHER; 2116c6fd2807SJeff Garzik 2117c6fd2807SJeff Garzik /* SENSE_VALID trumps dev/unknown error and revalidation */ 2118f90f0828STejun Heo if (qc->flags & ATA_QCFLAG_SENSE_VALID) 2119c6fd2807SJeff Garzik qc->err_mask &= ~(AC_ERR_DEV | AC_ERR_OTHER); 2120c6fd2807SJeff Garzik 212103faab78STejun Heo /* determine whether the command is worth retrying */ 2122534ead70STejun Heo if (qc->flags & ATA_QCFLAG_IO || 2123534ead70STejun Heo (!(qc->err_mask & AC_ERR_INVALID) && 2124534ead70STejun Heo qc->err_mask != AC_ERR_DEV)) 212503faab78STejun Heo qc->flags |= ATA_QCFLAG_RETRY; 212603faab78STejun Heo 2127c6fd2807SJeff Garzik /* accumulate error info */ 2128c6fd2807SJeff Garzik ehc->i.dev = qc->dev; 2129c6fd2807SJeff Garzik all_err_mask |= qc->err_mask; 2130c6fd2807SJeff Garzik if (qc->flags & ATA_QCFLAG_IO) 21313884f7b0STejun Heo eflags |= ATA_EFLAG_IS_IO; 2132c6fd2807SJeff Garzik } 2133c6fd2807SJeff Garzik 2134c6fd2807SJeff Garzik /* enforce default EH actions */ 2135c6fd2807SJeff Garzik if (ap->pflags & ATA_PFLAG_FROZEN || 2136c6fd2807SJeff Garzik all_err_mask & (AC_ERR_HSM | AC_ERR_TIMEOUT)) 2137cf480626STejun Heo ehc->i.action |= ATA_EH_RESET; 21383884f7b0STejun Heo else if (((eflags & ATA_EFLAG_IS_IO) && all_err_mask) || 21393884f7b0STejun Heo (!(eflags & ATA_EFLAG_IS_IO) && (all_err_mask & ~AC_ERR_DEV))) 2140c6fd2807SJeff Garzik ehc->i.action |= ATA_EH_REVALIDATE; 2141c6fd2807SJeff Garzik 2142dfcc173dSTejun Heo /* If we have offending qcs and the associated failed device, 2143dfcc173dSTejun Heo * perform per-dev EH action only on the offending device. 2144dfcc173dSTejun Heo */ 2145c6fd2807SJeff Garzik if (ehc->i.dev) { 2146c6fd2807SJeff Garzik ehc->i.dev_action[ehc->i.dev->devno] |= 2147c6fd2807SJeff Garzik ehc->i.action & ATA_EH_PERDEV_MASK; 2148c6fd2807SJeff Garzik ehc->i.action &= ~ATA_EH_PERDEV_MASK; 2149c6fd2807SJeff Garzik } 2150c6fd2807SJeff Garzik 21512695e366STejun Heo /* propagate timeout to host link */ 21522695e366STejun Heo if ((all_err_mask & AC_ERR_TIMEOUT) && !ata_is_host_link(link)) 21532695e366STejun Heo ap->link.eh_context.i.err_mask |= AC_ERR_TIMEOUT; 21542695e366STejun Heo 21552695e366STejun Heo /* record error and consider speeding down */ 2156dfcc173dSTejun Heo dev = ehc->i.dev; 21572695e366STejun Heo if (!dev && ((ata_link_max_devices(link) == 1 && 21582695e366STejun Heo ata_dev_enabled(link->device)))) 2159dfcc173dSTejun Heo dev = link->device; 2160dfcc173dSTejun Heo 216176326ac1STejun Heo if (dev) { 216276326ac1STejun Heo if (dev->flags & ATA_DFLAG_DUBIOUS_XFER) 216376326ac1STejun Heo eflags |= ATA_EFLAG_DUBIOUS_XFER; 21643884f7b0STejun Heo ehc->i.action |= ata_eh_speed_down(dev, eflags, all_err_mask); 216576326ac1STejun Heo } 2166dfcc173dSTejun Heo 2167c6fd2807SJeff Garzik DPRINTK("EXIT\n"); 2168c6fd2807SJeff Garzik } 2169c6fd2807SJeff Garzik 2170c6fd2807SJeff Garzik /** 21719b1e2658STejun Heo * ata_eh_autopsy - analyze error and determine recovery action 21729b1e2658STejun Heo * @ap: host port to perform autopsy on 21739b1e2658STejun Heo * 21749b1e2658STejun Heo * Analyze all links of @ap and determine why they failed and 21759b1e2658STejun Heo * which recovery actions are needed. 21769b1e2658STejun Heo * 21779b1e2658STejun Heo * LOCKING: 21789b1e2658STejun Heo * Kernel thread context (may sleep). 21799b1e2658STejun Heo */ 2180fb7fd614STejun Heo void ata_eh_autopsy(struct ata_port *ap) 21819b1e2658STejun Heo { 21829b1e2658STejun Heo struct ata_link *link; 21839b1e2658STejun Heo 21841eca4365STejun Heo ata_for_each_link(link, ap, EDGE) 21859b1e2658STejun Heo ata_eh_link_autopsy(link); 21862695e366STejun Heo 2187b1c72916STejun Heo /* Handle the frigging slave link. Autopsy is done similarly 2188b1c72916STejun Heo * but actions and flags are transferred over to the master 2189b1c72916STejun Heo * link and handled from there. 2190b1c72916STejun Heo */ 2191b1c72916STejun Heo if (ap->slave_link) { 2192b1c72916STejun Heo struct ata_eh_context *mehc = &ap->link.eh_context; 2193b1c72916STejun Heo struct ata_eh_context *sehc = &ap->slave_link->eh_context; 2194b1c72916STejun Heo 2195848e4c68STejun Heo /* transfer control flags from master to slave */ 2196848e4c68STejun Heo sehc->i.flags |= mehc->i.flags & ATA_EHI_TO_SLAVE_MASK; 2197848e4c68STejun Heo 2198848e4c68STejun Heo /* perform autopsy on the slave link */ 2199b1c72916STejun Heo ata_eh_link_autopsy(ap->slave_link); 2200b1c72916STejun Heo 2201848e4c68STejun Heo /* transfer actions from slave to master and clear slave */ 2202b1c72916STejun Heo ata_eh_about_to_do(ap->slave_link, NULL, ATA_EH_ALL_ACTIONS); 2203b1c72916STejun Heo mehc->i.action |= sehc->i.action; 2204b1c72916STejun Heo mehc->i.dev_action[1] |= sehc->i.dev_action[1]; 2205b1c72916STejun Heo mehc->i.flags |= sehc->i.flags; 2206b1c72916STejun Heo ata_eh_done(ap->slave_link, NULL, ATA_EH_ALL_ACTIONS); 2207b1c72916STejun Heo } 2208b1c72916STejun Heo 22092695e366STejun Heo /* Autopsy of fanout ports can affect host link autopsy. 22102695e366STejun Heo * Perform host link autopsy last. 22112695e366STejun Heo */ 2212071f44b1STejun Heo if (sata_pmp_attached(ap)) 22132695e366STejun Heo ata_eh_link_autopsy(&ap->link); 22149b1e2658STejun Heo } 22159b1e2658STejun Heo 22169b1e2658STejun Heo /** 22176521148cSRobert Hancock * ata_get_cmd_descript - get description for ATA command 22186521148cSRobert Hancock * @command: ATA command code to get description for 22196521148cSRobert Hancock * 22206521148cSRobert Hancock * Return a textual description of the given command, or NULL if the 22216521148cSRobert Hancock * command is not known. 22226521148cSRobert Hancock * 22236521148cSRobert Hancock * LOCKING: 22246521148cSRobert Hancock * None 22256521148cSRobert Hancock */ 22266521148cSRobert Hancock const char *ata_get_cmd_descript(u8 command) 22276521148cSRobert Hancock { 22286521148cSRobert Hancock #ifdef CONFIG_ATA_VERBOSE_ERROR 22296521148cSRobert Hancock static const struct 22306521148cSRobert Hancock { 22316521148cSRobert Hancock u8 command; 22326521148cSRobert Hancock const char *text; 22336521148cSRobert Hancock } cmd_descr[] = { 22346521148cSRobert Hancock { ATA_CMD_DEV_RESET, "DEVICE RESET" }, 22356521148cSRobert Hancock { ATA_CMD_CHK_POWER, "CHECK POWER MODE" }, 22366521148cSRobert Hancock { ATA_CMD_STANDBY, "STANDBY" }, 22376521148cSRobert Hancock { ATA_CMD_IDLE, "IDLE" }, 22386521148cSRobert Hancock { ATA_CMD_EDD, "EXECUTE DEVICE DIAGNOSTIC" }, 22396521148cSRobert Hancock { ATA_CMD_DOWNLOAD_MICRO, "DOWNLOAD MICROCODE" }, 22406521148cSRobert Hancock { ATA_CMD_NOP, "NOP" }, 22416521148cSRobert Hancock { ATA_CMD_FLUSH, "FLUSH CACHE" }, 22426521148cSRobert Hancock { ATA_CMD_FLUSH_EXT, "FLUSH CACHE EXT" }, 22436521148cSRobert Hancock { ATA_CMD_ID_ATA, "IDENTIFY DEVICE" }, 22446521148cSRobert Hancock { ATA_CMD_ID_ATAPI, "IDENTIFY PACKET DEVICE" }, 22456521148cSRobert Hancock { ATA_CMD_SERVICE, "SERVICE" }, 22466521148cSRobert Hancock { ATA_CMD_READ, "READ DMA" }, 22476521148cSRobert Hancock { ATA_CMD_READ_EXT, "READ DMA EXT" }, 22486521148cSRobert Hancock { ATA_CMD_READ_QUEUED, "READ DMA QUEUED" }, 22496521148cSRobert Hancock { ATA_CMD_READ_STREAM_EXT, "READ STREAM EXT" }, 22506521148cSRobert Hancock { ATA_CMD_READ_STREAM_DMA_EXT, "READ STREAM DMA EXT" }, 22516521148cSRobert Hancock { ATA_CMD_WRITE, "WRITE DMA" }, 22526521148cSRobert Hancock { ATA_CMD_WRITE_EXT, "WRITE DMA EXT" }, 22536521148cSRobert Hancock { ATA_CMD_WRITE_QUEUED, "WRITE DMA QUEUED EXT" }, 22546521148cSRobert Hancock { ATA_CMD_WRITE_STREAM_EXT, "WRITE STREAM EXT" }, 22556521148cSRobert Hancock { ATA_CMD_WRITE_STREAM_DMA_EXT, "WRITE STREAM DMA EXT" }, 22566521148cSRobert Hancock { ATA_CMD_WRITE_FUA_EXT, "WRITE DMA FUA EXT" }, 22576521148cSRobert Hancock { ATA_CMD_WRITE_QUEUED_FUA_EXT, "WRITE DMA QUEUED FUA EXT" }, 22586521148cSRobert Hancock { ATA_CMD_FPDMA_READ, "READ FPDMA QUEUED" }, 22596521148cSRobert Hancock { ATA_CMD_FPDMA_WRITE, "WRITE FPDMA QUEUED" }, 22606521148cSRobert Hancock { ATA_CMD_PIO_READ, "READ SECTOR(S)" }, 22616521148cSRobert Hancock { ATA_CMD_PIO_READ_EXT, "READ SECTOR(S) EXT" }, 22626521148cSRobert Hancock { ATA_CMD_PIO_WRITE, "WRITE SECTOR(S)" }, 22636521148cSRobert Hancock { ATA_CMD_PIO_WRITE_EXT, "WRITE SECTOR(S) EXT" }, 22646521148cSRobert Hancock { ATA_CMD_READ_MULTI, "READ MULTIPLE" }, 22656521148cSRobert Hancock { ATA_CMD_READ_MULTI_EXT, "READ MULTIPLE EXT" }, 22666521148cSRobert Hancock { ATA_CMD_WRITE_MULTI, "WRITE MULTIPLE" }, 22676521148cSRobert Hancock { ATA_CMD_WRITE_MULTI_EXT, "WRITE MULTIPLE EXT" }, 22686521148cSRobert Hancock { ATA_CMD_WRITE_MULTI_FUA_EXT, "WRITE MULTIPLE FUA EXT" }, 22696521148cSRobert Hancock { ATA_CMD_SET_FEATURES, "SET FEATURES" }, 22706521148cSRobert Hancock { ATA_CMD_SET_MULTI, "SET MULTIPLE MODE" }, 22716521148cSRobert Hancock { ATA_CMD_VERIFY, "READ VERIFY SECTOR(S)" }, 22726521148cSRobert Hancock { ATA_CMD_VERIFY_EXT, "READ VERIFY SECTOR(S) EXT" }, 22736521148cSRobert Hancock { ATA_CMD_WRITE_UNCORR_EXT, "WRITE UNCORRECTABLE EXT" }, 22746521148cSRobert Hancock { ATA_CMD_STANDBYNOW1, "STANDBY IMMEDIATE" }, 22756521148cSRobert Hancock { ATA_CMD_IDLEIMMEDIATE, "IDLE IMMEDIATE" }, 22766521148cSRobert Hancock { ATA_CMD_SLEEP, "SLEEP" }, 22776521148cSRobert Hancock { ATA_CMD_INIT_DEV_PARAMS, "INITIALIZE DEVICE PARAMETERS" }, 22786521148cSRobert Hancock { ATA_CMD_READ_NATIVE_MAX, "READ NATIVE MAX ADDRESS" }, 22796521148cSRobert Hancock { ATA_CMD_READ_NATIVE_MAX_EXT, "READ NATIVE MAX ADDRESS EXT" }, 22806521148cSRobert Hancock { ATA_CMD_SET_MAX, "SET MAX ADDRESS" }, 22816521148cSRobert Hancock { ATA_CMD_SET_MAX_EXT, "SET MAX ADDRESS EXT" }, 22826521148cSRobert Hancock { ATA_CMD_READ_LOG_EXT, "READ LOG EXT" }, 22836521148cSRobert Hancock { ATA_CMD_WRITE_LOG_EXT, "WRITE LOG EXT" }, 22846521148cSRobert Hancock { ATA_CMD_READ_LOG_DMA_EXT, "READ LOG DMA EXT" }, 22856521148cSRobert Hancock { ATA_CMD_WRITE_LOG_DMA_EXT, "WRITE LOG DMA EXT" }, 22866521148cSRobert Hancock { ATA_CMD_TRUSTED_RCV, "TRUSTED RECEIVE" }, 22876521148cSRobert Hancock { ATA_CMD_TRUSTED_RCV_DMA, "TRUSTED RECEIVE DMA" }, 22886521148cSRobert Hancock { ATA_CMD_TRUSTED_SND, "TRUSTED SEND" }, 22896521148cSRobert Hancock { ATA_CMD_TRUSTED_SND_DMA, "TRUSTED SEND DMA" }, 22906521148cSRobert Hancock { ATA_CMD_PMP_READ, "READ BUFFER" }, 22916521148cSRobert Hancock { ATA_CMD_PMP_WRITE, "WRITE BUFFER" }, 22926521148cSRobert Hancock { ATA_CMD_CONF_OVERLAY, "DEVICE CONFIGURATION OVERLAY" }, 22936521148cSRobert Hancock { ATA_CMD_SEC_SET_PASS, "SECURITY SET PASSWORD" }, 22946521148cSRobert Hancock { ATA_CMD_SEC_UNLOCK, "SECURITY UNLOCK" }, 22956521148cSRobert Hancock { ATA_CMD_SEC_ERASE_PREP, "SECURITY ERASE PREPARE" }, 22966521148cSRobert Hancock { ATA_CMD_SEC_ERASE_UNIT, "SECURITY ERASE UNIT" }, 22976521148cSRobert Hancock { ATA_CMD_SEC_FREEZE_LOCK, "SECURITY FREEZE LOCK" }, 22986521148cSRobert Hancock { ATA_CMD_SEC_DISABLE_PASS, "SECURITY DISABLE PASSWORD" }, 22996521148cSRobert Hancock { ATA_CMD_CONFIG_STREAM, "CONFIGURE STREAM" }, 23006521148cSRobert Hancock { ATA_CMD_SMART, "SMART" }, 23016521148cSRobert Hancock { ATA_CMD_MEDIA_LOCK, "DOOR LOCK" }, 23026521148cSRobert Hancock { ATA_CMD_MEDIA_UNLOCK, "DOOR UNLOCK" }, 2303acad7627SFUJITA Tomonori { ATA_CMD_DSM, "DATA SET MANAGEMENT" }, 23046521148cSRobert Hancock { ATA_CMD_CHK_MED_CRD_TYP, "CHECK MEDIA CARD TYPE" }, 23056521148cSRobert Hancock { ATA_CMD_CFA_REQ_EXT_ERR, "CFA REQUEST EXTENDED ERROR" }, 23066521148cSRobert Hancock { ATA_CMD_CFA_WRITE_NE, "CFA WRITE SECTORS WITHOUT ERASE" }, 23076521148cSRobert Hancock { ATA_CMD_CFA_TRANS_SECT, "CFA TRANSLATE SECTOR" }, 23086521148cSRobert Hancock { ATA_CMD_CFA_ERASE, "CFA ERASE SECTORS" }, 23096521148cSRobert Hancock { ATA_CMD_CFA_WRITE_MULT_NE, "CFA WRITE MULTIPLE WITHOUT ERASE" }, 23106521148cSRobert Hancock { ATA_CMD_READ_LONG, "READ LONG (with retries)" }, 23116521148cSRobert Hancock { ATA_CMD_READ_LONG_ONCE, "READ LONG (without retries)" }, 23126521148cSRobert Hancock { ATA_CMD_WRITE_LONG, "WRITE LONG (with retries)" }, 23136521148cSRobert Hancock { ATA_CMD_WRITE_LONG_ONCE, "WRITE LONG (without retries)" }, 23146521148cSRobert Hancock { ATA_CMD_RESTORE, "RECALIBRATE" }, 23156521148cSRobert Hancock { 0, NULL } /* terminate list */ 23166521148cSRobert Hancock }; 23176521148cSRobert Hancock 23186521148cSRobert Hancock unsigned int i; 23196521148cSRobert Hancock for (i = 0; cmd_descr[i].text; i++) 23206521148cSRobert Hancock if (cmd_descr[i].command == command) 23216521148cSRobert Hancock return cmd_descr[i].text; 23226521148cSRobert Hancock #endif 23236521148cSRobert Hancock 23246521148cSRobert Hancock return NULL; 23256521148cSRobert Hancock } 23266521148cSRobert Hancock 23276521148cSRobert Hancock /** 23289b1e2658STejun Heo * ata_eh_link_report - report error handling to user 23290260731fSTejun Heo * @link: ATA link EH is going on 2330c6fd2807SJeff Garzik * 2331c6fd2807SJeff Garzik * Report EH to user. 2332c6fd2807SJeff Garzik * 2333c6fd2807SJeff Garzik * LOCKING: 2334c6fd2807SJeff Garzik * None. 2335c6fd2807SJeff Garzik */ 23369b1e2658STejun Heo static void ata_eh_link_report(struct ata_link *link) 2337c6fd2807SJeff Garzik { 23380260731fSTejun Heo struct ata_port *ap = link->ap; 23390260731fSTejun Heo struct ata_eh_context *ehc = &link->eh_context; 2340c6fd2807SJeff Garzik const char *frozen, *desc; 2341a1e10f7eSTejun Heo char tries_buf[6]; 2342c6fd2807SJeff Garzik int tag, nr_failed = 0; 2343c6fd2807SJeff Garzik 234494ff3d54STejun Heo if (ehc->i.flags & ATA_EHI_QUIET) 234594ff3d54STejun Heo return; 234694ff3d54STejun Heo 2347c6fd2807SJeff Garzik desc = NULL; 2348c6fd2807SJeff Garzik if (ehc->i.desc[0] != '\0') 2349c6fd2807SJeff Garzik desc = ehc->i.desc; 2350c6fd2807SJeff Garzik 2351c6fd2807SJeff Garzik for (tag = 0; tag < ATA_MAX_QUEUE; tag++) { 2352c6fd2807SJeff Garzik struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag); 2353c6fd2807SJeff Garzik 2354b1c72916STejun Heo if (!(qc->flags & ATA_QCFLAG_FAILED) || 2355b1c72916STejun Heo ata_dev_phys_link(qc->dev) != link || 2356e027bd36STejun Heo ((qc->flags & ATA_QCFLAG_QUIET) && 2357e027bd36STejun Heo qc->err_mask == AC_ERR_DEV)) 2358c6fd2807SJeff Garzik continue; 2359c6fd2807SJeff Garzik if (qc->flags & ATA_QCFLAG_SENSE_VALID && !qc->err_mask) 2360c6fd2807SJeff Garzik continue; 2361c6fd2807SJeff Garzik 2362c6fd2807SJeff Garzik nr_failed++; 2363c6fd2807SJeff Garzik } 2364c6fd2807SJeff Garzik 2365c6fd2807SJeff Garzik if (!nr_failed && !ehc->i.err_mask) 2366c6fd2807SJeff Garzik return; 2367c6fd2807SJeff Garzik 2368c6fd2807SJeff Garzik frozen = ""; 2369c6fd2807SJeff Garzik if (ap->pflags & ATA_PFLAG_FROZEN) 2370c6fd2807SJeff Garzik frozen = " frozen"; 2371c6fd2807SJeff Garzik 2372a1e10f7eSTejun Heo memset(tries_buf, 0, sizeof(tries_buf)); 2373a1e10f7eSTejun Heo if (ap->eh_tries < ATA_EH_MAX_TRIES) 2374a1e10f7eSTejun Heo snprintf(tries_buf, sizeof(tries_buf) - 1, " t%d", 2375a1e10f7eSTejun Heo ap->eh_tries); 2376a1e10f7eSTejun Heo 2377c6fd2807SJeff Garzik if (ehc->i.dev) { 2378a9a79dfeSJoe Perches ata_dev_err(ehc->i.dev, "exception Emask 0x%x " 2379a1e10f7eSTejun Heo "SAct 0x%x SErr 0x%x action 0x%x%s%s\n", 2380a1e10f7eSTejun Heo ehc->i.err_mask, link->sactive, ehc->i.serror, 2381a1e10f7eSTejun Heo ehc->i.action, frozen, tries_buf); 2382c6fd2807SJeff Garzik if (desc) 2383a9a79dfeSJoe Perches ata_dev_err(ehc->i.dev, "%s\n", desc); 2384c6fd2807SJeff Garzik } else { 2385a9a79dfeSJoe Perches ata_link_err(link, "exception Emask 0x%x " 2386a1e10f7eSTejun Heo "SAct 0x%x SErr 0x%x action 0x%x%s%s\n", 2387a1e10f7eSTejun Heo ehc->i.err_mask, link->sactive, ehc->i.serror, 2388a1e10f7eSTejun Heo ehc->i.action, frozen, tries_buf); 2389c6fd2807SJeff Garzik if (desc) 2390a9a79dfeSJoe Perches ata_link_err(link, "%s\n", desc); 2391c6fd2807SJeff Garzik } 2392c6fd2807SJeff Garzik 23936521148cSRobert Hancock #ifdef CONFIG_ATA_VERBOSE_ERROR 23941333e194SRobert Hancock if (ehc->i.serror) 2395a9a79dfeSJoe Perches ata_link_err(link, 23961333e194SRobert Hancock "SError: { %s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s}\n", 23971333e194SRobert Hancock ehc->i.serror & SERR_DATA_RECOVERED ? "RecovData " : "", 23981333e194SRobert Hancock ehc->i.serror & SERR_COMM_RECOVERED ? "RecovComm " : "", 23991333e194SRobert Hancock ehc->i.serror & SERR_DATA ? "UnrecovData " : "", 24001333e194SRobert Hancock ehc->i.serror & SERR_PERSISTENT ? "Persist " : "", 24011333e194SRobert Hancock ehc->i.serror & SERR_PROTOCOL ? "Proto " : "", 24021333e194SRobert Hancock ehc->i.serror & SERR_INTERNAL ? "HostInt " : "", 24031333e194SRobert Hancock ehc->i.serror & SERR_PHYRDY_CHG ? "PHYRdyChg " : "", 24041333e194SRobert Hancock ehc->i.serror & SERR_PHY_INT_ERR ? "PHYInt " : "", 24051333e194SRobert Hancock ehc->i.serror & SERR_COMM_WAKE ? "CommWake " : "", 24061333e194SRobert Hancock ehc->i.serror & SERR_10B_8B_ERR ? "10B8B " : "", 24071333e194SRobert Hancock ehc->i.serror & SERR_DISPARITY ? "Dispar " : "", 24081333e194SRobert Hancock ehc->i.serror & SERR_CRC ? "BadCRC " : "", 24091333e194SRobert Hancock ehc->i.serror & SERR_HANDSHAKE ? "Handshk " : "", 24101333e194SRobert Hancock ehc->i.serror & SERR_LINK_SEQ_ERR ? "LinkSeq " : "", 24111333e194SRobert Hancock ehc->i.serror & SERR_TRANS_ST_ERROR ? "TrStaTrns " : "", 24121333e194SRobert Hancock ehc->i.serror & SERR_UNRECOG_FIS ? "UnrecFIS " : "", 24131333e194SRobert Hancock ehc->i.serror & SERR_DEV_XCHG ? "DevExch " : ""); 24146521148cSRobert Hancock #endif 24151333e194SRobert Hancock 2416c6fd2807SJeff Garzik for (tag = 0; tag < ATA_MAX_QUEUE; tag++) { 2417c6fd2807SJeff Garzik struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag); 24188a937581STejun Heo struct ata_taskfile *cmd = &qc->tf, *res = &qc->result_tf; 2419abb6a889STejun Heo const u8 *cdb = qc->cdb; 2420abb6a889STejun Heo char data_buf[20] = ""; 2421abb6a889STejun Heo char cdb_buf[70] = ""; 2422c6fd2807SJeff Garzik 24230260731fSTejun Heo if (!(qc->flags & ATA_QCFLAG_FAILED) || 2424b1c72916STejun Heo ata_dev_phys_link(qc->dev) != link || !qc->err_mask) 2425c6fd2807SJeff Garzik continue; 2426c6fd2807SJeff Garzik 2427abb6a889STejun Heo if (qc->dma_dir != DMA_NONE) { 2428abb6a889STejun Heo static const char *dma_str[] = { 2429abb6a889STejun Heo [DMA_BIDIRECTIONAL] = "bidi", 2430abb6a889STejun Heo [DMA_TO_DEVICE] = "out", 2431abb6a889STejun Heo [DMA_FROM_DEVICE] = "in", 2432abb6a889STejun Heo }; 2433abb6a889STejun Heo static const char *prot_str[] = { 2434abb6a889STejun Heo [ATA_PROT_PIO] = "pio", 2435abb6a889STejun Heo [ATA_PROT_DMA] = "dma", 2436abb6a889STejun Heo [ATA_PROT_NCQ] = "ncq", 24370dc36888STejun Heo [ATAPI_PROT_PIO] = "pio", 24380dc36888STejun Heo [ATAPI_PROT_DMA] = "dma", 2439abb6a889STejun Heo }; 2440abb6a889STejun Heo 2441abb6a889STejun Heo snprintf(data_buf, sizeof(data_buf), " %s %u %s", 2442abb6a889STejun Heo prot_str[qc->tf.protocol], qc->nbytes, 2443abb6a889STejun Heo dma_str[qc->dma_dir]); 2444abb6a889STejun Heo } 2445abb6a889STejun Heo 24466521148cSRobert Hancock if (ata_is_atapi(qc->tf.protocol)) { 24476521148cSRobert Hancock if (qc->scsicmd) 24486521148cSRobert Hancock scsi_print_command(qc->scsicmd); 24496521148cSRobert Hancock else 2450abb6a889STejun Heo snprintf(cdb_buf, sizeof(cdb_buf), 2451abb6a889STejun Heo "cdb %02x %02x %02x %02x %02x %02x %02x %02x " 2452abb6a889STejun Heo "%02x %02x %02x %02x %02x %02x %02x %02x\n ", 2453abb6a889STejun Heo cdb[0], cdb[1], cdb[2], cdb[3], 2454abb6a889STejun Heo cdb[4], cdb[5], cdb[6], cdb[7], 2455abb6a889STejun Heo cdb[8], cdb[9], cdb[10], cdb[11], 2456abb6a889STejun Heo cdb[12], cdb[13], cdb[14], cdb[15]); 24576521148cSRobert Hancock } else { 24586521148cSRobert Hancock const char *descr = ata_get_cmd_descript(cmd->command); 24596521148cSRobert Hancock if (descr) 2460a9a79dfeSJoe Perches ata_dev_err(qc->dev, "failed command: %s\n", 2461a9a79dfeSJoe Perches descr); 24626521148cSRobert Hancock } 2463abb6a889STejun Heo 2464a9a79dfeSJoe Perches ata_dev_err(qc->dev, 24658a937581STejun Heo "cmd %02x/%02x:%02x:%02x:%02x:%02x/%02x:%02x:%02x:%02x:%02x/%02x " 2466abb6a889STejun Heo "tag %d%s\n %s" 24678a937581STejun Heo "res %02x/%02x:%02x:%02x:%02x:%02x/%02x:%02x:%02x:%02x:%02x/%02x " 24685335b729STejun Heo "Emask 0x%x (%s)%s\n", 24698a937581STejun Heo cmd->command, cmd->feature, cmd->nsect, 24708a937581STejun Heo cmd->lbal, cmd->lbam, cmd->lbah, 24718a937581STejun Heo cmd->hob_feature, cmd->hob_nsect, 24728a937581STejun Heo cmd->hob_lbal, cmd->hob_lbam, cmd->hob_lbah, 2473abb6a889STejun Heo cmd->device, qc->tag, data_buf, cdb_buf, 24748a937581STejun Heo res->command, res->feature, res->nsect, 24758a937581STejun Heo res->lbal, res->lbam, res->lbah, 24768a937581STejun Heo res->hob_feature, res->hob_nsect, 24778a937581STejun Heo res->hob_lbal, res->hob_lbam, res->hob_lbah, 24785335b729STejun Heo res->device, qc->err_mask, ata_err_string(qc->err_mask), 24795335b729STejun Heo qc->err_mask & AC_ERR_NCQ ? " <F>" : ""); 24801333e194SRobert Hancock 24816521148cSRobert Hancock #ifdef CONFIG_ATA_VERBOSE_ERROR 24821333e194SRobert Hancock if (res->command & (ATA_BUSY | ATA_DRDY | ATA_DF | ATA_DRQ | 24831333e194SRobert Hancock ATA_ERR)) { 24841333e194SRobert Hancock if (res->command & ATA_BUSY) 2485a9a79dfeSJoe Perches ata_dev_err(qc->dev, "status: { Busy }\n"); 24861333e194SRobert Hancock else 2487a9a79dfeSJoe Perches ata_dev_err(qc->dev, "status: { %s%s%s%s}\n", 24881333e194SRobert Hancock res->command & ATA_DRDY ? "DRDY " : "", 24891333e194SRobert Hancock res->command & ATA_DF ? "DF " : "", 24901333e194SRobert Hancock res->command & ATA_DRQ ? "DRQ " : "", 24911333e194SRobert Hancock res->command & ATA_ERR ? "ERR " : ""); 24921333e194SRobert Hancock } 24931333e194SRobert Hancock 24941333e194SRobert Hancock if (cmd->command != ATA_CMD_PACKET && 24951333e194SRobert Hancock (res->feature & (ATA_ICRC | ATA_UNC | ATA_IDNF | 24961333e194SRobert Hancock ATA_ABORTED))) 2497a9a79dfeSJoe Perches ata_dev_err(qc->dev, "error: { %s%s%s%s}\n", 24981333e194SRobert Hancock res->feature & ATA_ICRC ? "ICRC " : "", 24991333e194SRobert Hancock res->feature & ATA_UNC ? "UNC " : "", 25001333e194SRobert Hancock res->feature & ATA_IDNF ? "IDNF " : "", 25011333e194SRobert Hancock res->feature & ATA_ABORTED ? "ABRT " : ""); 25026521148cSRobert Hancock #endif 2503c6fd2807SJeff Garzik } 2504c6fd2807SJeff Garzik } 2505c6fd2807SJeff Garzik 25069b1e2658STejun Heo /** 25079b1e2658STejun Heo * ata_eh_report - report error handling to user 25089b1e2658STejun Heo * @ap: ATA port to report EH about 25099b1e2658STejun Heo * 25109b1e2658STejun Heo * Report EH to user. 25119b1e2658STejun Heo * 25129b1e2658STejun Heo * LOCKING: 25139b1e2658STejun Heo * None. 25149b1e2658STejun Heo */ 2515fb7fd614STejun Heo void ata_eh_report(struct ata_port *ap) 25169b1e2658STejun Heo { 25179b1e2658STejun Heo struct ata_link *link; 25189b1e2658STejun Heo 25191eca4365STejun Heo ata_for_each_link(link, ap, HOST_FIRST) 25209b1e2658STejun Heo ata_eh_link_report(link); 25219b1e2658STejun Heo } 25229b1e2658STejun Heo 2523cc0680a5STejun Heo static int ata_do_reset(struct ata_link *link, ata_reset_fn_t reset, 2524b1c72916STejun Heo unsigned int *classes, unsigned long deadline, 2525b1c72916STejun Heo bool clear_classes) 2526c6fd2807SJeff Garzik { 2527f58229f8STejun Heo struct ata_device *dev; 2528c6fd2807SJeff Garzik 2529b1c72916STejun Heo if (clear_classes) 25301eca4365STejun Heo ata_for_each_dev(dev, link, ALL) 2531f58229f8STejun Heo classes[dev->devno] = ATA_DEV_UNKNOWN; 2532c6fd2807SJeff Garzik 2533f046519fSTejun Heo return reset(link, classes, deadline); 2534c6fd2807SJeff Garzik } 2535c6fd2807SJeff Garzik 2536e8411fbaSSergei Shtylyov static int ata_eh_followup_srst_needed(struct ata_link *link, int rc) 2537c6fd2807SJeff Garzik { 253845db2f6cSTejun Heo if ((link->flags & ATA_LFLAG_NO_SRST) || ata_link_offline(link)) 2539ae791c05STejun Heo return 0; 25405dbfc9cbSTejun Heo if (rc == -EAGAIN) 2541c6fd2807SJeff Garzik return 1; 2542071f44b1STejun Heo if (sata_pmp_supported(link->ap) && ata_is_host_link(link)) 25433495de73STejun Heo return 1; 2544c6fd2807SJeff Garzik return 0; 2545c6fd2807SJeff Garzik } 2546c6fd2807SJeff Garzik 2547fb7fd614STejun Heo int ata_eh_reset(struct ata_link *link, int classify, 2548c6fd2807SJeff Garzik ata_prereset_fn_t prereset, ata_reset_fn_t softreset, 2549c6fd2807SJeff Garzik ata_reset_fn_t hardreset, ata_postreset_fn_t postreset) 2550c6fd2807SJeff Garzik { 2551afaa5c37STejun Heo struct ata_port *ap = link->ap; 2552b1c72916STejun Heo struct ata_link *slave = ap->slave_link; 2553936fd732STejun Heo struct ata_eh_context *ehc = &link->eh_context; 2554705d2014SBartlomiej Zolnierkiewicz struct ata_eh_context *sehc = slave ? &slave->eh_context : NULL; 2555c6fd2807SJeff Garzik unsigned int *classes = ehc->classes; 2556416dc9edSTejun Heo unsigned int lflags = link->flags; 2557c6fd2807SJeff Garzik int verbose = !(ehc->i.flags & ATA_EHI_QUIET); 2558d8af0eb6STejun Heo int max_tries = 0, try = 0; 2559b1c72916STejun Heo struct ata_link *failed_link; 2560f58229f8STejun Heo struct ata_device *dev; 2561416dc9edSTejun Heo unsigned long deadline, now; 2562c6fd2807SJeff Garzik ata_reset_fn_t reset; 2563afaa5c37STejun Heo unsigned long flags; 2564416dc9edSTejun Heo u32 sstatus; 2565b1c72916STejun Heo int nr_unknown, rc; 2566c6fd2807SJeff Garzik 2567932648b0STejun Heo /* 2568932648b0STejun Heo * Prepare to reset 2569932648b0STejun Heo */ 2570d8af0eb6STejun Heo while (ata_eh_reset_timeouts[max_tries] != ULONG_MAX) 2571d8af0eb6STejun Heo max_tries++; 257205944bdfSTejun Heo if (link->flags & ATA_LFLAG_NO_HRST) 257305944bdfSTejun Heo hardreset = NULL; 257405944bdfSTejun Heo if (link->flags & ATA_LFLAG_NO_SRST) 257505944bdfSTejun Heo softreset = NULL; 2576d8af0eb6STejun Heo 257725985edcSLucas De Marchi /* make sure each reset attempt is at least COOL_DOWN apart */ 257819b72321STejun Heo if (ehc->i.flags & ATA_EHI_DID_RESET) { 25790a2c0f56STejun Heo now = jiffies; 258019b72321STejun Heo WARN_ON(time_after(ehc->last_reset, now)); 258119b72321STejun Heo deadline = ata_deadline(ehc->last_reset, 258219b72321STejun Heo ATA_EH_RESET_COOL_DOWN); 25830a2c0f56STejun Heo if (time_before(now, deadline)) 25840a2c0f56STejun Heo schedule_timeout_uninterruptible(deadline - now); 258519b72321STejun Heo } 25860a2c0f56STejun Heo 2587afaa5c37STejun Heo spin_lock_irqsave(ap->lock, flags); 2588afaa5c37STejun Heo ap->pflags |= ATA_PFLAG_RESETTING; 2589afaa5c37STejun Heo spin_unlock_irqrestore(ap->lock, flags); 2590afaa5c37STejun Heo 2591cf480626STejun Heo ata_eh_about_to_do(link, NULL, ATA_EH_RESET); 2592c6fd2807SJeff Garzik 25931eca4365STejun Heo ata_for_each_dev(dev, link, ALL) { 2594cdeab114STejun Heo /* If we issue an SRST then an ATA drive (not ATAPI) 2595cdeab114STejun Heo * may change configuration and be in PIO0 timing. If 2596cdeab114STejun Heo * we do a hard reset (or are coming from power on) 2597cdeab114STejun Heo * this is true for ATA or ATAPI. Until we've set a 2598cdeab114STejun Heo * suitable controller mode we should not touch the 2599cdeab114STejun Heo * bus as we may be talking too fast. 2600cdeab114STejun Heo */ 2601cdeab114STejun Heo dev->pio_mode = XFER_PIO_0; 2602cdeab114STejun Heo 2603cdeab114STejun Heo /* If the controller has a pio mode setup function 2604cdeab114STejun Heo * then use it to set the chipset to rights. Don't 2605cdeab114STejun Heo * touch the DMA setup as that will be dealt with when 2606cdeab114STejun Heo * configuring devices. 2607cdeab114STejun Heo */ 2608cdeab114STejun Heo if (ap->ops->set_piomode) 2609cdeab114STejun Heo ap->ops->set_piomode(ap, dev); 2610cdeab114STejun Heo } 2611cdeab114STejun Heo 2612cf480626STejun Heo /* prefer hardreset */ 2613932648b0STejun Heo reset = NULL; 2614cf480626STejun Heo ehc->i.action &= ~ATA_EH_RESET; 2615cf480626STejun Heo if (hardreset) { 2616cf480626STejun Heo reset = hardreset; 2617a674050eSTejun Heo ehc->i.action |= ATA_EH_HARDRESET; 26184f7faa3fSTejun Heo } else if (softreset) { 2619cf480626STejun Heo reset = softreset; 2620a674050eSTejun Heo ehc->i.action |= ATA_EH_SOFTRESET; 2621cf480626STejun Heo } 2622c6fd2807SJeff Garzik 2623c6fd2807SJeff Garzik if (prereset) { 2624b1c72916STejun Heo unsigned long deadline = ata_deadline(jiffies, 2625b1c72916STejun Heo ATA_EH_PRERESET_TIMEOUT); 2626b1c72916STejun Heo 2627b1c72916STejun Heo if (slave) { 2628b1c72916STejun Heo sehc->i.action &= ~ATA_EH_RESET; 2629b1c72916STejun Heo sehc->i.action |= ehc->i.action; 2630b1c72916STejun Heo } 2631b1c72916STejun Heo 2632b1c72916STejun Heo rc = prereset(link, deadline); 2633b1c72916STejun Heo 2634b1c72916STejun Heo /* If present, do prereset on slave link too. Reset 2635b1c72916STejun Heo * is skipped iff both master and slave links report 2636b1c72916STejun Heo * -ENOENT or clear ATA_EH_RESET. 2637b1c72916STejun Heo */ 2638b1c72916STejun Heo if (slave && (rc == 0 || rc == -ENOENT)) { 2639b1c72916STejun Heo int tmp; 2640b1c72916STejun Heo 2641b1c72916STejun Heo tmp = prereset(slave, deadline); 2642b1c72916STejun Heo if (tmp != -ENOENT) 2643b1c72916STejun Heo rc = tmp; 2644b1c72916STejun Heo 2645b1c72916STejun Heo ehc->i.action |= sehc->i.action; 2646b1c72916STejun Heo } 2647b1c72916STejun Heo 2648c6fd2807SJeff Garzik if (rc) { 2649c961922bSAlan Cox if (rc == -ENOENT) { 2650a9a79dfeSJoe Perches ata_link_dbg(link, "port disabled--ignoring\n"); 2651cf480626STejun Heo ehc->i.action &= ~ATA_EH_RESET; 26524aa9ab67STejun Heo 26531eca4365STejun Heo ata_for_each_dev(dev, link, ALL) 2654f58229f8STejun Heo classes[dev->devno] = ATA_DEV_NONE; 26554aa9ab67STejun Heo 26564aa9ab67STejun Heo rc = 0; 2657c961922bSAlan Cox } else 2658a9a79dfeSJoe Perches ata_link_err(link, 2659a9a79dfeSJoe Perches "prereset failed (errno=%d)\n", 2660a9a79dfeSJoe Perches rc); 2661fccb6ea5STejun Heo goto out; 2662c6fd2807SJeff Garzik } 2663c6fd2807SJeff Garzik 2664932648b0STejun Heo /* prereset() might have cleared ATA_EH_RESET. If so, 2665d6515e6fSTejun Heo * bang classes, thaw and return. 2666932648b0STejun Heo */ 2667932648b0STejun Heo if (reset && !(ehc->i.action & ATA_EH_RESET)) { 26681eca4365STejun Heo ata_for_each_dev(dev, link, ALL) 2669f58229f8STejun Heo classes[dev->devno] = ATA_DEV_NONE; 2670d6515e6fSTejun Heo if ((ap->pflags & ATA_PFLAG_FROZEN) && 2671d6515e6fSTejun Heo ata_is_host_link(link)) 2672d6515e6fSTejun Heo ata_eh_thaw_port(ap); 2673fccb6ea5STejun Heo rc = 0; 2674fccb6ea5STejun Heo goto out; 2675c6fd2807SJeff Garzik } 2676932648b0STejun Heo } 2677c6fd2807SJeff Garzik 2678c6fd2807SJeff Garzik retry: 2679932648b0STejun Heo /* 2680932648b0STejun Heo * Perform reset 2681932648b0STejun Heo */ 2682dc98c32cSTejun Heo if (ata_is_host_link(link)) 2683dc98c32cSTejun Heo ata_eh_freeze_port(ap); 2684dc98c32cSTejun Heo 2685341c2c95STejun Heo deadline = ata_deadline(jiffies, ata_eh_reset_timeouts[try++]); 268631daabdaSTejun Heo 2687932648b0STejun Heo if (reset) { 2688c6fd2807SJeff Garzik if (verbose) 2689a9a79dfeSJoe Perches ata_link_info(link, "%s resetting link\n", 2690c6fd2807SJeff Garzik reset == softreset ? "soft" : "hard"); 2691c6fd2807SJeff Garzik 2692c6fd2807SJeff Garzik /* mark that this EH session started with reset */ 269319b72321STejun Heo ehc->last_reset = jiffies; 26940d64a233STejun Heo if (reset == hardreset) 26950d64a233STejun Heo ehc->i.flags |= ATA_EHI_DID_HARDRESET; 26960d64a233STejun Heo else 26970d64a233STejun Heo ehc->i.flags |= ATA_EHI_DID_SOFTRESET; 2698c6fd2807SJeff Garzik 2699b1c72916STejun Heo rc = ata_do_reset(link, reset, classes, deadline, true); 2700b1c72916STejun Heo if (rc && rc != -EAGAIN) { 2701b1c72916STejun Heo failed_link = link; 27025dbfc9cbSTejun Heo goto fail; 2703b1c72916STejun Heo } 2704c6fd2807SJeff Garzik 2705b1c72916STejun Heo /* hardreset slave link if existent */ 2706b1c72916STejun Heo if (slave && reset == hardreset) { 2707b1c72916STejun Heo int tmp; 2708b1c72916STejun Heo 2709b1c72916STejun Heo if (verbose) 2710a9a79dfeSJoe Perches ata_link_info(slave, "hard resetting link\n"); 2711b1c72916STejun Heo 2712b1c72916STejun Heo ata_eh_about_to_do(slave, NULL, ATA_EH_RESET); 2713b1c72916STejun Heo tmp = ata_do_reset(slave, reset, classes, deadline, 2714b1c72916STejun Heo false); 2715b1c72916STejun Heo switch (tmp) { 2716b1c72916STejun Heo case -EAGAIN: 2717b1c72916STejun Heo rc = -EAGAIN; 2718b1c72916STejun Heo case 0: 2719b1c72916STejun Heo break; 2720b1c72916STejun Heo default: 2721b1c72916STejun Heo failed_link = slave; 2722b1c72916STejun Heo rc = tmp; 2723b1c72916STejun Heo goto fail; 2724b1c72916STejun Heo } 2725b1c72916STejun Heo } 2726b1c72916STejun Heo 2727b1c72916STejun Heo /* perform follow-up SRST if necessary */ 2728c6fd2807SJeff Garzik if (reset == hardreset && 2729e8411fbaSSergei Shtylyov ata_eh_followup_srst_needed(link, rc)) { 2730c6fd2807SJeff Garzik reset = softreset; 2731c6fd2807SJeff Garzik 2732c6fd2807SJeff Garzik if (!reset) { 2733a9a79dfeSJoe Perches ata_link_err(link, 2734a9a79dfeSJoe Perches "follow-up softreset required but no softreset available\n"); 2735b1c72916STejun Heo failed_link = link; 2736fccb6ea5STejun Heo rc = -EINVAL; 273708cf69d0STejun Heo goto fail; 2738c6fd2807SJeff Garzik } 2739c6fd2807SJeff Garzik 2740cf480626STejun Heo ata_eh_about_to_do(link, NULL, ATA_EH_RESET); 2741b1c72916STejun Heo rc = ata_do_reset(link, reset, classes, deadline, true); 2742fe2c4d01STejun Heo if (rc) { 2743fe2c4d01STejun Heo failed_link = link; 2744fe2c4d01STejun Heo goto fail; 2745fe2c4d01STejun Heo } 2746c6fd2807SJeff Garzik } 2747932648b0STejun Heo } else { 2748932648b0STejun Heo if (verbose) 2749a9a79dfeSJoe Perches ata_link_info(link, 2750a9a79dfeSJoe Perches "no reset method available, skipping reset\n"); 2751932648b0STejun Heo if (!(lflags & ATA_LFLAG_ASSUME_CLASS)) 2752932648b0STejun Heo lflags |= ATA_LFLAG_ASSUME_ATA; 2753932648b0STejun Heo } 2754008a7896STejun Heo 2755932648b0STejun Heo /* 2756932648b0STejun Heo * Post-reset processing 2757932648b0STejun Heo */ 27581eca4365STejun Heo ata_for_each_dev(dev, link, ALL) { 2759416dc9edSTejun Heo /* After the reset, the device state is PIO 0 and the 2760416dc9edSTejun Heo * controller state is undefined. Reset also wakes up 2761416dc9edSTejun Heo * drives from sleeping mode. 2762c6fd2807SJeff Garzik */ 2763f58229f8STejun Heo dev->pio_mode = XFER_PIO_0; 2764054a5fbaSTejun Heo dev->flags &= ~ATA_DFLAG_SLEEPING; 2765c6fd2807SJeff Garzik 27663b761d3dSTejun Heo if (ata_phys_link_offline(ata_dev_phys_link(dev))) 27673b761d3dSTejun Heo continue; 27683b761d3dSTejun Heo 27694ccd3329STejun Heo /* apply class override */ 2770416dc9edSTejun Heo if (lflags & ATA_LFLAG_ASSUME_ATA) 2771ae791c05STejun Heo classes[dev->devno] = ATA_DEV_ATA; 2772416dc9edSTejun Heo else if (lflags & ATA_LFLAG_ASSUME_SEMB) 2773816ab897STejun Heo classes[dev->devno] = ATA_DEV_SEMB_UNSUP; 2774ae791c05STejun Heo } 2775ae791c05STejun Heo 2776008a7896STejun Heo /* record current link speed */ 2777936fd732STejun Heo if (sata_scr_read(link, SCR_STATUS, &sstatus) == 0) 2778936fd732STejun Heo link->sata_spd = (sstatus >> 4) & 0xf; 2779b1c72916STejun Heo if (slave && sata_scr_read(slave, SCR_STATUS, &sstatus) == 0) 2780b1c72916STejun Heo slave->sata_spd = (sstatus >> 4) & 0xf; 2781008a7896STejun Heo 2782dc98c32cSTejun Heo /* thaw the port */ 2783dc98c32cSTejun Heo if (ata_is_host_link(link)) 2784dc98c32cSTejun Heo ata_eh_thaw_port(ap); 2785dc98c32cSTejun Heo 2786f046519fSTejun Heo /* postreset() should clear hardware SError. Although SError 2787f046519fSTejun Heo * is cleared during link resume, clearing SError here is 2788f046519fSTejun Heo * necessary as some PHYs raise hotplug events after SRST. 2789f046519fSTejun Heo * This introduces race condition where hotplug occurs between 2790f046519fSTejun Heo * reset and here. This race is mediated by cross checking 2791f046519fSTejun Heo * link onlineness and classification result later. 2792f046519fSTejun Heo */ 2793b1c72916STejun Heo if (postreset) { 2794cc0680a5STejun Heo postreset(link, classes); 2795b1c72916STejun Heo if (slave) 2796b1c72916STejun Heo postreset(slave, classes); 2797b1c72916STejun Heo } 2798c6fd2807SJeff Garzik 27991e641060STejun Heo /* 28008c56caccSTejun Heo * Some controllers can't be frozen very well and may set spurious 28018c56caccSTejun Heo * error conditions during reset. Clear accumulated error 28028c56caccSTejun Heo * information and re-thaw the port if frozen. As reset is the 28038c56caccSTejun Heo * final recovery action and we cross check link onlineness against 28048c56caccSTejun Heo * device classification later, no hotplug event is lost by this. 28051e641060STejun Heo */ 2806f046519fSTejun Heo spin_lock_irqsave(link->ap->lock, flags); 28071e641060STejun Heo memset(&link->eh_info, 0, sizeof(link->eh_info)); 2808b1c72916STejun Heo if (slave) 28091e641060STejun Heo memset(&slave->eh_info, 0, sizeof(link->eh_info)); 28101e641060STejun Heo ap->pflags &= ~ATA_PFLAG_EH_PENDING; 2811f046519fSTejun Heo spin_unlock_irqrestore(link->ap->lock, flags); 2812f046519fSTejun Heo 28138c56caccSTejun Heo if (ap->pflags & ATA_PFLAG_FROZEN) 28148c56caccSTejun Heo ata_eh_thaw_port(ap); 28158c56caccSTejun Heo 28163b761d3dSTejun Heo /* 28173b761d3dSTejun Heo * Make sure onlineness and classification result correspond. 2818f046519fSTejun Heo * Hotplug could have happened during reset and some 2819f046519fSTejun Heo * controllers fail to wait while a drive is spinning up after 2820f046519fSTejun Heo * being hotplugged causing misdetection. By cross checking 28213b761d3dSTejun Heo * link on/offlineness and classification result, those 28223b761d3dSTejun Heo * conditions can be reliably detected and retried. 2823f046519fSTejun Heo */ 2824b1c72916STejun Heo nr_unknown = 0; 28251eca4365STejun Heo ata_for_each_dev(dev, link, ALL) { 28263b761d3dSTejun Heo if (ata_phys_link_online(ata_dev_phys_link(dev))) { 2827b1c72916STejun Heo if (classes[dev->devno] == ATA_DEV_UNKNOWN) { 2828a9a79dfeSJoe Perches ata_dev_dbg(dev, "link online but device misclassified\n"); 2829f046519fSTejun Heo classes[dev->devno] = ATA_DEV_NONE; 2830b1c72916STejun Heo nr_unknown++; 2831b1c72916STejun Heo } 28323b761d3dSTejun Heo } else if (ata_phys_link_offline(ata_dev_phys_link(dev))) { 28333b761d3dSTejun Heo if (ata_class_enabled(classes[dev->devno])) 2834a9a79dfeSJoe Perches ata_dev_dbg(dev, 2835a9a79dfeSJoe Perches "link offline, clearing class %d to NONE\n", 28363b761d3dSTejun Heo classes[dev->devno]); 28373b761d3dSTejun Heo classes[dev->devno] = ATA_DEV_NONE; 28383b761d3dSTejun Heo } else if (classes[dev->devno] == ATA_DEV_UNKNOWN) { 2839a9a79dfeSJoe Perches ata_dev_dbg(dev, 2840a9a79dfeSJoe Perches "link status unknown, clearing UNKNOWN to NONE\n"); 28413b761d3dSTejun Heo classes[dev->devno] = ATA_DEV_NONE; 28423b761d3dSTejun Heo } 2843f046519fSTejun Heo } 2844f046519fSTejun Heo 2845b1c72916STejun Heo if (classify && nr_unknown) { 2846f046519fSTejun Heo if (try < max_tries) { 2847a9a79dfeSJoe Perches ata_link_warn(link, 2848a9a79dfeSJoe Perches "link online but %d devices misclassified, retrying\n", 28493b761d3dSTejun Heo nr_unknown); 2850b1c72916STejun Heo failed_link = link; 2851f046519fSTejun Heo rc = -EAGAIN; 2852f046519fSTejun Heo goto fail; 2853f046519fSTejun Heo } 2854a9a79dfeSJoe Perches ata_link_warn(link, 28553b761d3dSTejun Heo "link online but %d devices misclassified, " 28563b761d3dSTejun Heo "device detection might fail\n", nr_unknown); 2857f046519fSTejun Heo } 2858f046519fSTejun Heo 2859c6fd2807SJeff Garzik /* reset successful, schedule revalidation */ 2860cf480626STejun Heo ata_eh_done(link, NULL, ATA_EH_RESET); 2861b1c72916STejun Heo if (slave) 2862b1c72916STejun Heo ata_eh_done(slave, NULL, ATA_EH_RESET); 286319b72321STejun Heo ehc->last_reset = jiffies; /* update to completion time */ 2864c6fd2807SJeff Garzik ehc->i.action |= ATA_EH_REVALIDATE; 28656b7ae954STejun Heo link->lpm_policy = ATA_LPM_UNKNOWN; /* reset LPM state */ 2866416dc9edSTejun Heo 2867416dc9edSTejun Heo rc = 0; 2868fccb6ea5STejun Heo out: 2869fccb6ea5STejun Heo /* clear hotplug flag */ 2870fccb6ea5STejun Heo ehc->i.flags &= ~ATA_EHI_HOTPLUGGED; 2871b1c72916STejun Heo if (slave) 2872b1c72916STejun Heo sehc->i.flags &= ~ATA_EHI_HOTPLUGGED; 2873afaa5c37STejun Heo 2874afaa5c37STejun Heo spin_lock_irqsave(ap->lock, flags); 2875afaa5c37STejun Heo ap->pflags &= ~ATA_PFLAG_RESETTING; 2876afaa5c37STejun Heo spin_unlock_irqrestore(ap->lock, flags); 2877afaa5c37STejun Heo 2878c6fd2807SJeff Garzik return rc; 2879416dc9edSTejun Heo 2880416dc9edSTejun Heo fail: 28815958e302STejun Heo /* if SCR isn't accessible on a fan-out port, PMP needs to be reset */ 28825958e302STejun Heo if (!ata_is_host_link(link) && 28835958e302STejun Heo sata_scr_read(link, SCR_STATUS, &sstatus)) 28845958e302STejun Heo rc = -ERESTART; 28855958e302STejun Heo 2886*7a46c078SGwendal Grignou if (try >= max_tries) { 28878ea7645cSTejun Heo /* 28888ea7645cSTejun Heo * Thaw host port even if reset failed, so that the port 28898ea7645cSTejun Heo * can be retried on the next phy event. This risks 28908ea7645cSTejun Heo * repeated EH runs but seems to be a better tradeoff than 28918ea7645cSTejun Heo * shutting down a port after a botched hotplug attempt. 28928ea7645cSTejun Heo */ 28938ea7645cSTejun Heo if (ata_is_host_link(link)) 28948ea7645cSTejun Heo ata_eh_thaw_port(ap); 2895416dc9edSTejun Heo goto out; 28968ea7645cSTejun Heo } 2897416dc9edSTejun Heo 2898416dc9edSTejun Heo now = jiffies; 2899416dc9edSTejun Heo if (time_before(now, deadline)) { 2900416dc9edSTejun Heo unsigned long delta = deadline - now; 2901416dc9edSTejun Heo 2902a9a79dfeSJoe Perches ata_link_warn(failed_link, 29030a2c0f56STejun Heo "reset failed (errno=%d), retrying in %u secs\n", 29040a2c0f56STejun Heo rc, DIV_ROUND_UP(jiffies_to_msecs(delta), 1000)); 2905416dc9edSTejun Heo 2906c0c362b6STejun Heo ata_eh_release(ap); 2907416dc9edSTejun Heo while (delta) 2908416dc9edSTejun Heo delta = schedule_timeout_uninterruptible(delta); 2909c0c362b6STejun Heo ata_eh_acquire(ap); 2910416dc9edSTejun Heo } 2911416dc9edSTejun Heo 2912*7a46c078SGwendal Grignou /* 2913*7a46c078SGwendal Grignou * While disks spinup behind PMP, some controllers fail sending SRST. 2914*7a46c078SGwendal Grignou * They need to be reset - as well as the PMP - before retrying. 2915*7a46c078SGwendal Grignou */ 2916*7a46c078SGwendal Grignou if (rc == -ERESTART) { 2917*7a46c078SGwendal Grignou if (ata_is_host_link(link)) 2918*7a46c078SGwendal Grignou ata_eh_thaw_port(ap); 2919*7a46c078SGwendal Grignou goto out; 2920*7a46c078SGwendal Grignou } 2921*7a46c078SGwendal Grignou 2922b1c72916STejun Heo if (try == max_tries - 1) { 2923a07d499bSTejun Heo sata_down_spd_limit(link, 0); 2924b1c72916STejun Heo if (slave) 2925a07d499bSTejun Heo sata_down_spd_limit(slave, 0); 2926b1c72916STejun Heo } else if (rc == -EPIPE) 2927a07d499bSTejun Heo sata_down_spd_limit(failed_link, 0); 2928b1c72916STejun Heo 2929416dc9edSTejun Heo if (hardreset) 2930416dc9edSTejun Heo reset = hardreset; 2931416dc9edSTejun Heo goto retry; 2932c6fd2807SJeff Garzik } 2933c6fd2807SJeff Garzik 293445fabbb7SElias Oltmanns static inline void ata_eh_pull_park_action(struct ata_port *ap) 293545fabbb7SElias Oltmanns { 293645fabbb7SElias Oltmanns struct ata_link *link; 293745fabbb7SElias Oltmanns struct ata_device *dev; 293845fabbb7SElias Oltmanns unsigned long flags; 293945fabbb7SElias Oltmanns 294045fabbb7SElias Oltmanns /* 294145fabbb7SElias Oltmanns * This function can be thought of as an extended version of 294245fabbb7SElias Oltmanns * ata_eh_about_to_do() specially crafted to accommodate the 294345fabbb7SElias Oltmanns * requirements of ATA_EH_PARK handling. Since the EH thread 294445fabbb7SElias Oltmanns * does not leave the do {} while () loop in ata_eh_recover as 294545fabbb7SElias Oltmanns * long as the timeout for a park request to *one* device on 294645fabbb7SElias Oltmanns * the port has not expired, and since we still want to pick 294745fabbb7SElias Oltmanns * up park requests to other devices on the same port or 294845fabbb7SElias Oltmanns * timeout updates for the same device, we have to pull 294945fabbb7SElias Oltmanns * ATA_EH_PARK actions from eh_info into eh_context.i 295045fabbb7SElias Oltmanns * ourselves at the beginning of each pass over the loop. 295145fabbb7SElias Oltmanns * 295245fabbb7SElias Oltmanns * Additionally, all write accesses to &ap->park_req_pending 295345fabbb7SElias Oltmanns * through INIT_COMPLETION() (see below) or complete_all() 295445fabbb7SElias Oltmanns * (see ata_scsi_park_store()) are protected by the host lock. 295545fabbb7SElias Oltmanns * As a result we have that park_req_pending.done is zero on 295645fabbb7SElias Oltmanns * exit from this function, i.e. when ATA_EH_PARK actions for 295745fabbb7SElias Oltmanns * *all* devices on port ap have been pulled into the 295845fabbb7SElias Oltmanns * respective eh_context structs. If, and only if, 295945fabbb7SElias Oltmanns * park_req_pending.done is non-zero by the time we reach 296045fabbb7SElias Oltmanns * wait_for_completion_timeout(), another ATA_EH_PARK action 296145fabbb7SElias Oltmanns * has been scheduled for at least one of the devices on port 296245fabbb7SElias Oltmanns * ap and we have to cycle over the do {} while () loop in 296345fabbb7SElias Oltmanns * ata_eh_recover() again. 296445fabbb7SElias Oltmanns */ 296545fabbb7SElias Oltmanns 296645fabbb7SElias Oltmanns spin_lock_irqsave(ap->lock, flags); 296745fabbb7SElias Oltmanns INIT_COMPLETION(ap->park_req_pending); 29681eca4365STejun Heo ata_for_each_link(link, ap, EDGE) { 29691eca4365STejun Heo ata_for_each_dev(dev, link, ALL) { 297045fabbb7SElias Oltmanns struct ata_eh_info *ehi = &link->eh_info; 297145fabbb7SElias Oltmanns 297245fabbb7SElias Oltmanns link->eh_context.i.dev_action[dev->devno] |= 297345fabbb7SElias Oltmanns ehi->dev_action[dev->devno] & ATA_EH_PARK; 297445fabbb7SElias Oltmanns ata_eh_clear_action(link, dev, ehi, ATA_EH_PARK); 297545fabbb7SElias Oltmanns } 297645fabbb7SElias Oltmanns } 297745fabbb7SElias Oltmanns spin_unlock_irqrestore(ap->lock, flags); 297845fabbb7SElias Oltmanns } 297945fabbb7SElias Oltmanns 298045fabbb7SElias Oltmanns static void ata_eh_park_issue_cmd(struct ata_device *dev, int park) 298145fabbb7SElias Oltmanns { 298245fabbb7SElias Oltmanns struct ata_eh_context *ehc = &dev->link->eh_context; 298345fabbb7SElias Oltmanns struct ata_taskfile tf; 298445fabbb7SElias Oltmanns unsigned int err_mask; 298545fabbb7SElias Oltmanns 298645fabbb7SElias Oltmanns ata_tf_init(dev, &tf); 298745fabbb7SElias Oltmanns if (park) { 298845fabbb7SElias Oltmanns ehc->unloaded_mask |= 1 << dev->devno; 298945fabbb7SElias Oltmanns tf.command = ATA_CMD_IDLEIMMEDIATE; 299045fabbb7SElias Oltmanns tf.feature = 0x44; 299145fabbb7SElias Oltmanns tf.lbal = 0x4c; 299245fabbb7SElias Oltmanns tf.lbam = 0x4e; 299345fabbb7SElias Oltmanns tf.lbah = 0x55; 299445fabbb7SElias Oltmanns } else { 299545fabbb7SElias Oltmanns ehc->unloaded_mask &= ~(1 << dev->devno); 299645fabbb7SElias Oltmanns tf.command = ATA_CMD_CHK_POWER; 299745fabbb7SElias Oltmanns } 299845fabbb7SElias Oltmanns 299945fabbb7SElias Oltmanns tf.flags |= ATA_TFLAG_DEVICE | ATA_TFLAG_ISADDR; 300045fabbb7SElias Oltmanns tf.protocol |= ATA_PROT_NODATA; 300145fabbb7SElias Oltmanns err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0); 300245fabbb7SElias Oltmanns if (park && (err_mask || tf.lbal != 0xc4)) { 3003a9a79dfeSJoe Perches ata_dev_err(dev, "head unload failed!\n"); 300445fabbb7SElias Oltmanns ehc->unloaded_mask &= ~(1 << dev->devno); 300545fabbb7SElias Oltmanns } 300645fabbb7SElias Oltmanns } 300745fabbb7SElias Oltmanns 30080260731fSTejun Heo static int ata_eh_revalidate_and_attach(struct ata_link *link, 3009c6fd2807SJeff Garzik struct ata_device **r_failed_dev) 3010c6fd2807SJeff Garzik { 30110260731fSTejun Heo struct ata_port *ap = link->ap; 30120260731fSTejun Heo struct ata_eh_context *ehc = &link->eh_context; 3013c6fd2807SJeff Garzik struct ata_device *dev; 30148c3c52a8STejun Heo unsigned int new_mask = 0; 3015c6fd2807SJeff Garzik unsigned long flags; 3016f58229f8STejun Heo int rc = 0; 3017c6fd2807SJeff Garzik 3018c6fd2807SJeff Garzik DPRINTK("ENTER\n"); 3019c6fd2807SJeff Garzik 30208c3c52a8STejun Heo /* For PATA drive side cable detection to work, IDENTIFY must 30218c3c52a8STejun Heo * be done backwards such that PDIAG- is released by the slave 30228c3c52a8STejun Heo * device before the master device is identified. 30238c3c52a8STejun Heo */ 30241eca4365STejun Heo ata_for_each_dev(dev, link, ALL_REVERSE) { 3025f58229f8STejun Heo unsigned int action = ata_eh_dev_action(dev); 3026f58229f8STejun Heo unsigned int readid_flags = 0; 3027c6fd2807SJeff Garzik 3028bff04647STejun Heo if (ehc->i.flags & ATA_EHI_DID_RESET) 3029bff04647STejun Heo readid_flags |= ATA_READID_POSTRESET; 3030bff04647STejun Heo 30319666f400STejun Heo if ((action & ATA_EH_REVALIDATE) && ata_dev_enabled(dev)) { 3032633273a3STejun Heo WARN_ON(dev->class == ATA_DEV_PMP); 3033633273a3STejun Heo 3034b1c72916STejun Heo if (ata_phys_link_offline(ata_dev_phys_link(dev))) { 3035c6fd2807SJeff Garzik rc = -EIO; 30368c3c52a8STejun Heo goto err; 3037c6fd2807SJeff Garzik } 3038c6fd2807SJeff Garzik 30390260731fSTejun Heo ata_eh_about_to_do(link, dev, ATA_EH_REVALIDATE); 3040422c9daaSTejun Heo rc = ata_dev_revalidate(dev, ehc->classes[dev->devno], 3041422c9daaSTejun Heo readid_flags); 3042c6fd2807SJeff Garzik if (rc) 30438c3c52a8STejun Heo goto err; 3044c6fd2807SJeff Garzik 30450260731fSTejun Heo ata_eh_done(link, dev, ATA_EH_REVALIDATE); 3046c6fd2807SJeff Garzik 3047baa1e78aSTejun Heo /* Configuration may have changed, reconfigure 3048baa1e78aSTejun Heo * transfer mode. 3049baa1e78aSTejun Heo */ 3050baa1e78aSTejun Heo ehc->i.flags |= ATA_EHI_SETMODE; 3051baa1e78aSTejun Heo 3052c6fd2807SJeff Garzik /* schedule the scsi_rescan_device() here */ 3053ad72cf98STejun Heo schedule_work(&(ap->scsi_rescan_task)); 3054c6fd2807SJeff Garzik } else if (dev->class == ATA_DEV_UNKNOWN && 3055c6fd2807SJeff Garzik ehc->tries[dev->devno] && 3056c6fd2807SJeff Garzik ata_class_enabled(ehc->classes[dev->devno])) { 3057842faa6cSTejun Heo /* Temporarily set dev->class, it will be 3058842faa6cSTejun Heo * permanently set once all configurations are 3059842faa6cSTejun Heo * complete. This is necessary because new 3060842faa6cSTejun Heo * device configuration is done in two 3061842faa6cSTejun Heo * separate loops. 3062842faa6cSTejun Heo */ 3063c6fd2807SJeff Garzik dev->class = ehc->classes[dev->devno]; 3064c6fd2807SJeff Garzik 3065633273a3STejun Heo if (dev->class == ATA_DEV_PMP) 3066633273a3STejun Heo rc = sata_pmp_attach(dev); 3067633273a3STejun Heo else 3068633273a3STejun Heo rc = ata_dev_read_id(dev, &dev->class, 3069633273a3STejun Heo readid_flags, dev->id); 3070842faa6cSTejun Heo 3071842faa6cSTejun Heo /* read_id might have changed class, store and reset */ 3072842faa6cSTejun Heo ehc->classes[dev->devno] = dev->class; 3073842faa6cSTejun Heo dev->class = ATA_DEV_UNKNOWN; 3074842faa6cSTejun Heo 30758c3c52a8STejun Heo switch (rc) { 30768c3c52a8STejun Heo case 0: 307799cf610aSTejun Heo /* clear error info accumulated during probe */ 307899cf610aSTejun Heo ata_ering_clear(&dev->ering); 3079f58229f8STejun Heo new_mask |= 1 << dev->devno; 30808c3c52a8STejun Heo break; 30818c3c52a8STejun Heo case -ENOENT: 308255a8e2c8STejun Heo /* IDENTIFY was issued to non-existent 308355a8e2c8STejun Heo * device. No need to reset. Just 3084842faa6cSTejun Heo * thaw and ignore the device. 308555a8e2c8STejun Heo */ 308655a8e2c8STejun Heo ata_eh_thaw_port(ap); 3087c6fd2807SJeff Garzik break; 30888c3c52a8STejun Heo default: 30898c3c52a8STejun Heo goto err; 30908c3c52a8STejun Heo } 30918c3c52a8STejun Heo } 3092c6fd2807SJeff Garzik } 3093c6fd2807SJeff Garzik 3094c1c4e8d5STejun Heo /* PDIAG- should have been released, ask cable type if post-reset */ 309533267325STejun Heo if ((ehc->i.flags & ATA_EHI_DID_RESET) && ata_is_host_link(link)) { 309633267325STejun Heo if (ap->ops->cable_detect) 3097c1c4e8d5STejun Heo ap->cbl = ap->ops->cable_detect(ap); 309833267325STejun Heo ata_force_cbl(ap); 309933267325STejun Heo } 3100c1c4e8d5STejun Heo 31018c3c52a8STejun Heo /* Configure new devices forward such that user doesn't see 31028c3c52a8STejun Heo * device detection messages backwards. 31038c3c52a8STejun Heo */ 31041eca4365STejun Heo ata_for_each_dev(dev, link, ALL) { 31054f7c2874STejun Heo if (!(new_mask & (1 << dev->devno))) 31068c3c52a8STejun Heo continue; 31078c3c52a8STejun Heo 3108842faa6cSTejun Heo dev->class = ehc->classes[dev->devno]; 3109842faa6cSTejun Heo 31104f7c2874STejun Heo if (dev->class == ATA_DEV_PMP) 31114f7c2874STejun Heo continue; 31124f7c2874STejun Heo 31138c3c52a8STejun Heo ehc->i.flags |= ATA_EHI_PRINTINFO; 31148c3c52a8STejun Heo rc = ata_dev_configure(dev); 31158c3c52a8STejun Heo ehc->i.flags &= ~ATA_EHI_PRINTINFO; 3116842faa6cSTejun Heo if (rc) { 3117842faa6cSTejun Heo dev->class = ATA_DEV_UNKNOWN; 31188c3c52a8STejun Heo goto err; 3119842faa6cSTejun Heo } 31208c3c52a8STejun Heo 3121c6fd2807SJeff Garzik spin_lock_irqsave(ap->lock, flags); 3122c6fd2807SJeff Garzik ap->pflags |= ATA_PFLAG_SCSI_HOTPLUG; 3123c6fd2807SJeff Garzik spin_unlock_irqrestore(ap->lock, flags); 3124baa1e78aSTejun Heo 312555a8e2c8STejun Heo /* new device discovered, configure xfermode */ 3126baa1e78aSTejun Heo ehc->i.flags |= ATA_EHI_SETMODE; 3127c6fd2807SJeff Garzik } 3128c6fd2807SJeff Garzik 31298c3c52a8STejun Heo return 0; 31308c3c52a8STejun Heo 31318c3c52a8STejun Heo err: 3132c6fd2807SJeff Garzik *r_failed_dev = dev; 31338c3c52a8STejun Heo DPRINTK("EXIT rc=%d\n", rc); 3134c6fd2807SJeff Garzik return rc; 3135c6fd2807SJeff Garzik } 3136c6fd2807SJeff Garzik 31376f1d1e3aSTejun Heo /** 31386f1d1e3aSTejun Heo * ata_set_mode - Program timings and issue SET FEATURES - XFER 31396f1d1e3aSTejun Heo * @link: link on which timings will be programmed 314098a1708dSMartin Olsson * @r_failed_dev: out parameter for failed device 31416f1d1e3aSTejun Heo * 31426f1d1e3aSTejun Heo * Set ATA device disk transfer mode (PIO3, UDMA6, etc.). If 31436f1d1e3aSTejun Heo * ata_set_mode() fails, pointer to the failing device is 31446f1d1e3aSTejun Heo * returned in @r_failed_dev. 31456f1d1e3aSTejun Heo * 31466f1d1e3aSTejun Heo * LOCKING: 31476f1d1e3aSTejun Heo * PCI/etc. bus probe sem. 31486f1d1e3aSTejun Heo * 31496f1d1e3aSTejun Heo * RETURNS: 31506f1d1e3aSTejun Heo * 0 on success, negative errno otherwise 31516f1d1e3aSTejun Heo */ 31526f1d1e3aSTejun Heo int ata_set_mode(struct ata_link *link, struct ata_device **r_failed_dev) 31536f1d1e3aSTejun Heo { 31546f1d1e3aSTejun Heo struct ata_port *ap = link->ap; 315500115e0fSTejun Heo struct ata_device *dev; 315600115e0fSTejun Heo int rc; 31576f1d1e3aSTejun Heo 315876326ac1STejun Heo /* if data transfer is verified, clear DUBIOUS_XFER on ering top */ 31591eca4365STejun Heo ata_for_each_dev(dev, link, ENABLED) { 316076326ac1STejun Heo if (!(dev->flags & ATA_DFLAG_DUBIOUS_XFER)) { 316176326ac1STejun Heo struct ata_ering_entry *ent; 316276326ac1STejun Heo 316376326ac1STejun Heo ent = ata_ering_top(&dev->ering); 316476326ac1STejun Heo if (ent) 316576326ac1STejun Heo ent->eflags &= ~ATA_EFLAG_DUBIOUS_XFER; 316676326ac1STejun Heo } 316776326ac1STejun Heo } 316876326ac1STejun Heo 31696f1d1e3aSTejun Heo /* has private set_mode? */ 31706f1d1e3aSTejun Heo if (ap->ops->set_mode) 317100115e0fSTejun Heo rc = ap->ops->set_mode(link, r_failed_dev); 317200115e0fSTejun Heo else 317300115e0fSTejun Heo rc = ata_do_set_mode(link, r_failed_dev); 317400115e0fSTejun Heo 317500115e0fSTejun Heo /* if transfer mode has changed, set DUBIOUS_XFER on device */ 31761eca4365STejun Heo ata_for_each_dev(dev, link, ENABLED) { 317700115e0fSTejun Heo struct ata_eh_context *ehc = &link->eh_context; 317800115e0fSTejun Heo u8 saved_xfer_mode = ehc->saved_xfer_mode[dev->devno]; 317900115e0fSTejun Heo u8 saved_ncq = !!(ehc->saved_ncq_enabled & (1 << dev->devno)); 318000115e0fSTejun Heo 318100115e0fSTejun Heo if (dev->xfer_mode != saved_xfer_mode || 318200115e0fSTejun Heo ata_ncq_enabled(dev) != saved_ncq) 318300115e0fSTejun Heo dev->flags |= ATA_DFLAG_DUBIOUS_XFER; 318400115e0fSTejun Heo } 318500115e0fSTejun Heo 318600115e0fSTejun Heo return rc; 31876f1d1e3aSTejun Heo } 31886f1d1e3aSTejun Heo 318911fc33daSTejun Heo /** 319011fc33daSTejun Heo * atapi_eh_clear_ua - Clear ATAPI UNIT ATTENTION after reset 319111fc33daSTejun Heo * @dev: ATAPI device to clear UA for 319211fc33daSTejun Heo * 319311fc33daSTejun Heo * Resets and other operations can make an ATAPI device raise 319411fc33daSTejun Heo * UNIT ATTENTION which causes the next operation to fail. This 319511fc33daSTejun Heo * function clears UA. 319611fc33daSTejun Heo * 319711fc33daSTejun Heo * LOCKING: 319811fc33daSTejun Heo * EH context (may sleep). 319911fc33daSTejun Heo * 320011fc33daSTejun Heo * RETURNS: 320111fc33daSTejun Heo * 0 on success, -errno on failure. 320211fc33daSTejun Heo */ 320311fc33daSTejun Heo static int atapi_eh_clear_ua(struct ata_device *dev) 320411fc33daSTejun Heo { 320511fc33daSTejun Heo int i; 320611fc33daSTejun Heo 320711fc33daSTejun Heo for (i = 0; i < ATA_EH_UA_TRIES; i++) { 3208b5357081STejun Heo u8 *sense_buffer = dev->link->ap->sector_buf; 320911fc33daSTejun Heo u8 sense_key = 0; 321011fc33daSTejun Heo unsigned int err_mask; 321111fc33daSTejun Heo 321211fc33daSTejun Heo err_mask = atapi_eh_tur(dev, &sense_key); 321311fc33daSTejun Heo if (err_mask != 0 && err_mask != AC_ERR_DEV) { 3214a9a79dfeSJoe Perches ata_dev_warn(dev, 3215a9a79dfeSJoe Perches "TEST_UNIT_READY failed (err_mask=0x%x)\n", 3216a9a79dfeSJoe Perches err_mask); 321711fc33daSTejun Heo return -EIO; 321811fc33daSTejun Heo } 321911fc33daSTejun Heo 322011fc33daSTejun Heo if (!err_mask || sense_key != UNIT_ATTENTION) 322111fc33daSTejun Heo return 0; 322211fc33daSTejun Heo 322311fc33daSTejun Heo err_mask = atapi_eh_request_sense(dev, sense_buffer, sense_key); 322411fc33daSTejun Heo if (err_mask) { 3225a9a79dfeSJoe Perches ata_dev_warn(dev, "failed to clear " 322611fc33daSTejun Heo "UNIT ATTENTION (err_mask=0x%x)\n", err_mask); 322711fc33daSTejun Heo return -EIO; 322811fc33daSTejun Heo } 322911fc33daSTejun Heo } 323011fc33daSTejun Heo 3231a9a79dfeSJoe Perches ata_dev_warn(dev, "UNIT ATTENTION persists after %d tries\n", 3232a9a79dfeSJoe Perches ATA_EH_UA_TRIES); 323311fc33daSTejun Heo 323411fc33daSTejun Heo return 0; 323511fc33daSTejun Heo } 323611fc33daSTejun Heo 32376013efd8STejun Heo /** 32386013efd8STejun Heo * ata_eh_maybe_retry_flush - Retry FLUSH if necessary 32396013efd8STejun Heo * @dev: ATA device which may need FLUSH retry 32406013efd8STejun Heo * 32416013efd8STejun Heo * If @dev failed FLUSH, it needs to be reported upper layer 32426013efd8STejun Heo * immediately as it means that @dev failed to remap and already 32436013efd8STejun Heo * lost at least a sector and further FLUSH retrials won't make 32446013efd8STejun Heo * any difference to the lost sector. However, if FLUSH failed 32456013efd8STejun Heo * for other reasons, for example transmission error, FLUSH needs 32466013efd8STejun Heo * to be retried. 32476013efd8STejun Heo * 32486013efd8STejun Heo * This function determines whether FLUSH failure retry is 32496013efd8STejun Heo * necessary and performs it if so. 32506013efd8STejun Heo * 32516013efd8STejun Heo * RETURNS: 32526013efd8STejun Heo * 0 if EH can continue, -errno if EH needs to be repeated. 32536013efd8STejun Heo */ 32546013efd8STejun Heo static int ata_eh_maybe_retry_flush(struct ata_device *dev) 32556013efd8STejun Heo { 32566013efd8STejun Heo struct ata_link *link = dev->link; 32576013efd8STejun Heo struct ata_port *ap = link->ap; 32586013efd8STejun Heo struct ata_queued_cmd *qc; 32596013efd8STejun Heo struct ata_taskfile tf; 32606013efd8STejun Heo unsigned int err_mask; 32616013efd8STejun Heo int rc = 0; 32626013efd8STejun Heo 32636013efd8STejun Heo /* did flush fail for this device? */ 32646013efd8STejun Heo if (!ata_tag_valid(link->active_tag)) 32656013efd8STejun Heo return 0; 32666013efd8STejun Heo 32676013efd8STejun Heo qc = __ata_qc_from_tag(ap, link->active_tag); 32686013efd8STejun Heo if (qc->dev != dev || (qc->tf.command != ATA_CMD_FLUSH_EXT && 32696013efd8STejun Heo qc->tf.command != ATA_CMD_FLUSH)) 32706013efd8STejun Heo return 0; 32716013efd8STejun Heo 32726013efd8STejun Heo /* if the device failed it, it should be reported to upper layers */ 32736013efd8STejun Heo if (qc->err_mask & AC_ERR_DEV) 32746013efd8STejun Heo return 0; 32756013efd8STejun Heo 32766013efd8STejun Heo /* flush failed for some other reason, give it another shot */ 32776013efd8STejun Heo ata_tf_init(dev, &tf); 32786013efd8STejun Heo 32796013efd8STejun Heo tf.command = qc->tf.command; 32806013efd8STejun Heo tf.flags |= ATA_TFLAG_DEVICE; 32816013efd8STejun Heo tf.protocol = ATA_PROT_NODATA; 32826013efd8STejun Heo 3283a9a79dfeSJoe Perches ata_dev_warn(dev, "retrying FLUSH 0x%x Emask 0x%x\n", 32846013efd8STejun Heo tf.command, qc->err_mask); 32856013efd8STejun Heo 32866013efd8STejun Heo err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0); 32876013efd8STejun Heo if (!err_mask) { 32886013efd8STejun Heo /* 32896013efd8STejun Heo * FLUSH is complete but there's no way to 32906013efd8STejun Heo * successfully complete a failed command from EH. 32916013efd8STejun Heo * Making sure retry is allowed at least once and 32926013efd8STejun Heo * retrying it should do the trick - whatever was in 32936013efd8STejun Heo * the cache is already on the platter and this won't 32946013efd8STejun Heo * cause infinite loop. 32956013efd8STejun Heo */ 32966013efd8STejun Heo qc->scsicmd->allowed = max(qc->scsicmd->allowed, 1); 32976013efd8STejun Heo } else { 3298a9a79dfeSJoe Perches ata_dev_warn(dev, "FLUSH failed Emask 0x%x\n", 32996013efd8STejun Heo err_mask); 33006013efd8STejun Heo rc = -EIO; 33016013efd8STejun Heo 33026013efd8STejun Heo /* if device failed it, report it to upper layers */ 33036013efd8STejun Heo if (err_mask & AC_ERR_DEV) { 33046013efd8STejun Heo qc->err_mask |= AC_ERR_DEV; 33056013efd8STejun Heo qc->result_tf = tf; 33066013efd8STejun Heo if (!(ap->pflags & ATA_PFLAG_FROZEN)) 33076013efd8STejun Heo rc = 0; 33086013efd8STejun Heo } 33096013efd8STejun Heo } 33106013efd8STejun Heo return rc; 33116013efd8STejun Heo } 33126013efd8STejun Heo 33136b7ae954STejun Heo /** 33146b7ae954STejun Heo * ata_eh_set_lpm - configure SATA interface power management 33156b7ae954STejun Heo * @link: link to configure power management 33166b7ae954STejun Heo * @policy: the link power management policy 33176b7ae954STejun Heo * @r_failed_dev: out parameter for failed device 33186b7ae954STejun Heo * 33196b7ae954STejun Heo * Enable SATA Interface power management. This will enable 33206b7ae954STejun Heo * Device Interface Power Management (DIPM) for min_power 33216b7ae954STejun Heo * policy, and then call driver specific callbacks for 33226b7ae954STejun Heo * enabling Host Initiated Power management. 33236b7ae954STejun Heo * 33246b7ae954STejun Heo * LOCKING: 33256b7ae954STejun Heo * EH context. 33266b7ae954STejun Heo * 33276b7ae954STejun Heo * RETURNS: 33286b7ae954STejun Heo * 0 on success, -errno on failure. 33296b7ae954STejun Heo */ 33306b7ae954STejun Heo static int ata_eh_set_lpm(struct ata_link *link, enum ata_lpm_policy policy, 33316b7ae954STejun Heo struct ata_device **r_failed_dev) 33326b7ae954STejun Heo { 33336c8ea89cSTejun Heo struct ata_port *ap = ata_is_host_link(link) ? link->ap : NULL; 33346b7ae954STejun Heo struct ata_eh_context *ehc = &link->eh_context; 33356b7ae954STejun Heo struct ata_device *dev, *link_dev = NULL, *lpm_dev = NULL; 3336e5005b15STejun Heo enum ata_lpm_policy old_policy = link->lpm_policy; 33375f6f12ccSTejun Heo bool no_dipm = link->ap->flags & ATA_FLAG_NO_DIPM; 33386b7ae954STejun Heo unsigned int hints = ATA_LPM_EMPTY | ATA_LPM_HIPM; 33396b7ae954STejun Heo unsigned int err_mask; 33406b7ae954STejun Heo int rc; 33416b7ae954STejun Heo 33426b7ae954STejun Heo /* if the link or host doesn't do LPM, noop */ 33436b7ae954STejun Heo if ((link->flags & ATA_LFLAG_NO_LPM) || (ap && !ap->ops->set_lpm)) 33446b7ae954STejun Heo return 0; 33456b7ae954STejun Heo 33466b7ae954STejun Heo /* 33476b7ae954STejun Heo * DIPM is enabled only for MIN_POWER as some devices 33486b7ae954STejun Heo * misbehave when the host NACKs transition to SLUMBER. Order 33496b7ae954STejun Heo * device and link configurations such that the host always 33506b7ae954STejun Heo * allows DIPM requests. 33516b7ae954STejun Heo */ 33526b7ae954STejun Heo ata_for_each_dev(dev, link, ENABLED) { 33536b7ae954STejun Heo bool hipm = ata_id_has_hipm(dev->id); 3354ae01b249STejun Heo bool dipm = ata_id_has_dipm(dev->id) && !no_dipm; 33556b7ae954STejun Heo 33566b7ae954STejun Heo /* find the first enabled and LPM enabled devices */ 33576b7ae954STejun Heo if (!link_dev) 33586b7ae954STejun Heo link_dev = dev; 33596b7ae954STejun Heo 33606b7ae954STejun Heo if (!lpm_dev && (hipm || dipm)) 33616b7ae954STejun Heo lpm_dev = dev; 33626b7ae954STejun Heo 33636b7ae954STejun Heo hints &= ~ATA_LPM_EMPTY; 33646b7ae954STejun Heo if (!hipm) 33656b7ae954STejun Heo hints &= ~ATA_LPM_HIPM; 33666b7ae954STejun Heo 33676b7ae954STejun Heo /* disable DIPM before changing link config */ 33686b7ae954STejun Heo if (policy != ATA_LPM_MIN_POWER && dipm) { 33696b7ae954STejun Heo err_mask = ata_dev_set_feature(dev, 33706b7ae954STejun Heo SETFEATURES_SATA_DISABLE, SATA_DIPM); 33716b7ae954STejun Heo if (err_mask && err_mask != AC_ERR_DEV) { 3372a9a79dfeSJoe Perches ata_dev_warn(dev, 33736b7ae954STejun Heo "failed to disable DIPM, Emask 0x%x\n", 33746b7ae954STejun Heo err_mask); 33756b7ae954STejun Heo rc = -EIO; 33766b7ae954STejun Heo goto fail; 33776b7ae954STejun Heo } 33786b7ae954STejun Heo } 33796b7ae954STejun Heo } 33806b7ae954STejun Heo 33816c8ea89cSTejun Heo if (ap) { 33826b7ae954STejun Heo rc = ap->ops->set_lpm(link, policy, hints); 33836b7ae954STejun Heo if (!rc && ap->slave_link) 33846b7ae954STejun Heo rc = ap->ops->set_lpm(ap->slave_link, policy, hints); 33856c8ea89cSTejun Heo } else 33866c8ea89cSTejun Heo rc = sata_pmp_set_lpm(link, policy, hints); 33876b7ae954STejun Heo 33886b7ae954STejun Heo /* 33896b7ae954STejun Heo * Attribute link config failure to the first (LPM) enabled 33906b7ae954STejun Heo * device on the link. 33916b7ae954STejun Heo */ 33926b7ae954STejun Heo if (rc) { 33936b7ae954STejun Heo if (rc == -EOPNOTSUPP) { 33946b7ae954STejun Heo link->flags |= ATA_LFLAG_NO_LPM; 33956b7ae954STejun Heo return 0; 33966b7ae954STejun Heo } 33976b7ae954STejun Heo dev = lpm_dev ? lpm_dev : link_dev; 33986b7ae954STejun Heo goto fail; 33996b7ae954STejun Heo } 34006b7ae954STejun Heo 3401e5005b15STejun Heo /* 3402e5005b15STejun Heo * Low level driver acked the transition. Issue DIPM command 3403e5005b15STejun Heo * with the new policy set. 3404e5005b15STejun Heo */ 3405e5005b15STejun Heo link->lpm_policy = policy; 3406e5005b15STejun Heo if (ap && ap->slave_link) 3407e5005b15STejun Heo ap->slave_link->lpm_policy = policy; 3408e5005b15STejun Heo 34096b7ae954STejun Heo /* host config updated, enable DIPM if transitioning to MIN_POWER */ 34106b7ae954STejun Heo ata_for_each_dev(dev, link, ENABLED) { 3411ae01b249STejun Heo if (policy == ATA_LPM_MIN_POWER && !no_dipm && 3412ae01b249STejun Heo ata_id_has_dipm(dev->id)) { 34136b7ae954STejun Heo err_mask = ata_dev_set_feature(dev, 34146b7ae954STejun Heo SETFEATURES_SATA_ENABLE, SATA_DIPM); 34156b7ae954STejun Heo if (err_mask && err_mask != AC_ERR_DEV) { 3416a9a79dfeSJoe Perches ata_dev_warn(dev, 34176b7ae954STejun Heo "failed to enable DIPM, Emask 0x%x\n", 34186b7ae954STejun Heo err_mask); 34196b7ae954STejun Heo rc = -EIO; 34206b7ae954STejun Heo goto fail; 34216b7ae954STejun Heo } 34226b7ae954STejun Heo } 34236b7ae954STejun Heo } 34246b7ae954STejun Heo 34256b7ae954STejun Heo return 0; 34266b7ae954STejun Heo 34276b7ae954STejun Heo fail: 3428e5005b15STejun Heo /* restore the old policy */ 3429e5005b15STejun Heo link->lpm_policy = old_policy; 3430e5005b15STejun Heo if (ap && ap->slave_link) 3431e5005b15STejun Heo ap->slave_link->lpm_policy = old_policy; 3432e5005b15STejun Heo 34336b7ae954STejun Heo /* if no device or only one more chance is left, disable LPM */ 34346b7ae954STejun Heo if (!dev || ehc->tries[dev->devno] <= 2) { 3435a9a79dfeSJoe Perches ata_link_warn(link, "disabling LPM on the link\n"); 34366b7ae954STejun Heo link->flags |= ATA_LFLAG_NO_LPM; 34376b7ae954STejun Heo } 34386b7ae954STejun Heo if (r_failed_dev) 34396b7ae954STejun Heo *r_failed_dev = dev; 34406b7ae954STejun Heo return rc; 34416b7ae954STejun Heo } 34426b7ae954STejun Heo 34438a745f1fSKristen Carlson Accardi int ata_link_nr_enabled(struct ata_link *link) 3444c6fd2807SJeff Garzik { 3445f58229f8STejun Heo struct ata_device *dev; 3446f58229f8STejun Heo int cnt = 0; 3447c6fd2807SJeff Garzik 34481eca4365STejun Heo ata_for_each_dev(dev, link, ENABLED) 3449c6fd2807SJeff Garzik cnt++; 3450c6fd2807SJeff Garzik return cnt; 3451c6fd2807SJeff Garzik } 3452c6fd2807SJeff Garzik 34530260731fSTejun Heo static int ata_link_nr_vacant(struct ata_link *link) 3454c6fd2807SJeff Garzik { 3455f58229f8STejun Heo struct ata_device *dev; 3456f58229f8STejun Heo int cnt = 0; 3457c6fd2807SJeff Garzik 34581eca4365STejun Heo ata_for_each_dev(dev, link, ALL) 3459f58229f8STejun Heo if (dev->class == ATA_DEV_UNKNOWN) 3460c6fd2807SJeff Garzik cnt++; 3461c6fd2807SJeff Garzik return cnt; 3462c6fd2807SJeff Garzik } 3463c6fd2807SJeff Garzik 34640260731fSTejun Heo static int ata_eh_skip_recovery(struct ata_link *link) 3465c6fd2807SJeff Garzik { 3466672b2d65STejun Heo struct ata_port *ap = link->ap; 34670260731fSTejun Heo struct ata_eh_context *ehc = &link->eh_context; 3468f58229f8STejun Heo struct ata_device *dev; 3469c6fd2807SJeff Garzik 3470f9df58cbSTejun Heo /* skip disabled links */ 3471f9df58cbSTejun Heo if (link->flags & ATA_LFLAG_DISABLED) 3472f9df58cbSTejun Heo return 1; 3473f9df58cbSTejun Heo 3474e2f3d75fSTejun Heo /* skip if explicitly requested */ 3475e2f3d75fSTejun Heo if (ehc->i.flags & ATA_EHI_NO_RECOVERY) 3476e2f3d75fSTejun Heo return 1; 3477e2f3d75fSTejun Heo 3478672b2d65STejun Heo /* thaw frozen port and recover failed devices */ 3479672b2d65STejun Heo if ((ap->pflags & ATA_PFLAG_FROZEN) || ata_link_nr_enabled(link)) 3480672b2d65STejun Heo return 0; 3481672b2d65STejun Heo 3482672b2d65STejun Heo /* reset at least once if reset is requested */ 3483672b2d65STejun Heo if ((ehc->i.action & ATA_EH_RESET) && 3484672b2d65STejun Heo !(ehc->i.flags & ATA_EHI_DID_RESET)) 3485c6fd2807SJeff Garzik return 0; 3486c6fd2807SJeff Garzik 3487c6fd2807SJeff Garzik /* skip if class codes for all vacant slots are ATA_DEV_NONE */ 34881eca4365STejun Heo ata_for_each_dev(dev, link, ALL) { 3489c6fd2807SJeff Garzik if (dev->class == ATA_DEV_UNKNOWN && 3490c6fd2807SJeff Garzik ehc->classes[dev->devno] != ATA_DEV_NONE) 3491c6fd2807SJeff Garzik return 0; 3492c6fd2807SJeff Garzik } 3493c6fd2807SJeff Garzik 3494c6fd2807SJeff Garzik return 1; 3495c6fd2807SJeff Garzik } 3496c6fd2807SJeff Garzik 3497c2c7a89cSTejun Heo static int ata_count_probe_trials_cb(struct ata_ering_entry *ent, void *void_arg) 3498c2c7a89cSTejun Heo { 3499c2c7a89cSTejun Heo u64 interval = msecs_to_jiffies(ATA_EH_PROBE_TRIAL_INTERVAL); 3500c2c7a89cSTejun Heo u64 now = get_jiffies_64(); 3501c2c7a89cSTejun Heo int *trials = void_arg; 3502c2c7a89cSTejun Heo 3503c2c7a89cSTejun Heo if (ent->timestamp < now - min(now, interval)) 3504c2c7a89cSTejun Heo return -1; 3505c2c7a89cSTejun Heo 3506c2c7a89cSTejun Heo (*trials)++; 3507c2c7a89cSTejun Heo return 0; 3508c2c7a89cSTejun Heo } 3509c2c7a89cSTejun Heo 351002c05a27STejun Heo static int ata_eh_schedule_probe(struct ata_device *dev) 351102c05a27STejun Heo { 351202c05a27STejun Heo struct ata_eh_context *ehc = &dev->link->eh_context; 3513c2c7a89cSTejun Heo struct ata_link *link = ata_dev_phys_link(dev); 3514c2c7a89cSTejun Heo int trials = 0; 351502c05a27STejun Heo 351602c05a27STejun Heo if (!(ehc->i.probe_mask & (1 << dev->devno)) || 351702c05a27STejun Heo (ehc->did_probe_mask & (1 << dev->devno))) 351802c05a27STejun Heo return 0; 351902c05a27STejun Heo 352002c05a27STejun Heo ata_eh_detach_dev(dev); 352102c05a27STejun Heo ata_dev_init(dev); 352202c05a27STejun Heo ehc->did_probe_mask |= (1 << dev->devno); 3523cf480626STejun Heo ehc->i.action |= ATA_EH_RESET; 352400115e0fSTejun Heo ehc->saved_xfer_mode[dev->devno] = 0; 352500115e0fSTejun Heo ehc->saved_ncq_enabled &= ~(1 << dev->devno); 352602c05a27STejun Heo 35276b7ae954STejun Heo /* the link maybe in a deep sleep, wake it up */ 35286c8ea89cSTejun Heo if (link->lpm_policy > ATA_LPM_MAX_POWER) { 35296c8ea89cSTejun Heo if (ata_is_host_link(link)) 35306c8ea89cSTejun Heo link->ap->ops->set_lpm(link, ATA_LPM_MAX_POWER, 35316c8ea89cSTejun Heo ATA_LPM_EMPTY); 35326c8ea89cSTejun Heo else 35336c8ea89cSTejun Heo sata_pmp_set_lpm(link, ATA_LPM_MAX_POWER, 35346c8ea89cSTejun Heo ATA_LPM_EMPTY); 35356c8ea89cSTejun Heo } 35366b7ae954STejun Heo 3537c2c7a89cSTejun Heo /* Record and count probe trials on the ering. The specific 3538c2c7a89cSTejun Heo * error mask used is irrelevant. Because a successful device 3539c2c7a89cSTejun Heo * detection clears the ering, this count accumulates only if 3540c2c7a89cSTejun Heo * there are consecutive failed probes. 3541c2c7a89cSTejun Heo * 3542c2c7a89cSTejun Heo * If the count is equal to or higher than ATA_EH_PROBE_TRIALS 3543c2c7a89cSTejun Heo * in the last ATA_EH_PROBE_TRIAL_INTERVAL, link speed is 3544c2c7a89cSTejun Heo * forced to 1.5Gbps. 3545c2c7a89cSTejun Heo * 3546c2c7a89cSTejun Heo * This is to work around cases where failed link speed 3547c2c7a89cSTejun Heo * negotiation results in device misdetection leading to 3548c2c7a89cSTejun Heo * infinite DEVXCHG or PHRDY CHG events. 3549c2c7a89cSTejun Heo */ 3550c2c7a89cSTejun Heo ata_ering_record(&dev->ering, 0, AC_ERR_OTHER); 3551c2c7a89cSTejun Heo ata_ering_map(&dev->ering, ata_count_probe_trials_cb, &trials); 3552c2c7a89cSTejun Heo 3553c2c7a89cSTejun Heo if (trials > ATA_EH_PROBE_TRIALS) 3554c2c7a89cSTejun Heo sata_down_spd_limit(link, 1); 3555c2c7a89cSTejun Heo 355602c05a27STejun Heo return 1; 355702c05a27STejun Heo } 355802c05a27STejun Heo 35599b1e2658STejun Heo static int ata_eh_handle_dev_fail(struct ata_device *dev, int err) 3560fee7ca72STejun Heo { 35619af5c9c9STejun Heo struct ata_eh_context *ehc = &dev->link->eh_context; 3562fee7ca72STejun Heo 3563cf9a590aSTejun Heo /* -EAGAIN from EH routine indicates retry without prejudice. 3564cf9a590aSTejun Heo * The requester is responsible for ensuring forward progress. 3565cf9a590aSTejun Heo */ 3566cf9a590aSTejun Heo if (err != -EAGAIN) 3567fee7ca72STejun Heo ehc->tries[dev->devno]--; 3568fee7ca72STejun Heo 3569fee7ca72STejun Heo switch (err) { 3570fee7ca72STejun Heo case -ENODEV: 3571fee7ca72STejun Heo /* device missing or wrong IDENTIFY data, schedule probing */ 3572fee7ca72STejun Heo ehc->i.probe_mask |= (1 << dev->devno); 3573fee7ca72STejun Heo case -EINVAL: 3574fee7ca72STejun Heo /* give it just one more chance */ 3575fee7ca72STejun Heo ehc->tries[dev->devno] = min(ehc->tries[dev->devno], 1); 3576fee7ca72STejun Heo case -EIO: 3577d89293abSTejun Heo if (ehc->tries[dev->devno] == 1) { 3578fee7ca72STejun Heo /* This is the last chance, better to slow 3579fee7ca72STejun Heo * down than lose it. 3580fee7ca72STejun Heo */ 3581a07d499bSTejun Heo sata_down_spd_limit(ata_dev_phys_link(dev), 0); 3582d89293abSTejun Heo if (dev->pio_mode > XFER_PIO_0) 3583fee7ca72STejun Heo ata_down_xfermask_limit(dev, ATA_DNXFER_PIO); 3584fee7ca72STejun Heo } 3585fee7ca72STejun Heo } 3586fee7ca72STejun Heo 3587fee7ca72STejun Heo if (ata_dev_enabled(dev) && !ehc->tries[dev->devno]) { 3588fee7ca72STejun Heo /* disable device if it has used up all its chances */ 3589fee7ca72STejun Heo ata_dev_disable(dev); 3590fee7ca72STejun Heo 3591fee7ca72STejun Heo /* detach if offline */ 3592b1c72916STejun Heo if (ata_phys_link_offline(ata_dev_phys_link(dev))) 3593fee7ca72STejun Heo ata_eh_detach_dev(dev); 3594fee7ca72STejun Heo 359502c05a27STejun Heo /* schedule probe if necessary */ 359687fbc5a0STejun Heo if (ata_eh_schedule_probe(dev)) { 3597fee7ca72STejun Heo ehc->tries[dev->devno] = ATA_EH_DEV_TRIES; 359887fbc5a0STejun Heo memset(ehc->cmd_timeout_idx[dev->devno], 0, 359987fbc5a0STejun Heo sizeof(ehc->cmd_timeout_idx[dev->devno])); 360087fbc5a0STejun Heo } 36019b1e2658STejun Heo 36029b1e2658STejun Heo return 1; 3603fee7ca72STejun Heo } else { 3604cf480626STejun Heo ehc->i.action |= ATA_EH_RESET; 36059b1e2658STejun Heo return 0; 3606fee7ca72STejun Heo } 3607fee7ca72STejun Heo } 3608fee7ca72STejun Heo 3609c6fd2807SJeff Garzik /** 3610c6fd2807SJeff Garzik * ata_eh_recover - recover host port after error 3611c6fd2807SJeff Garzik * @ap: host port to recover 3612c6fd2807SJeff Garzik * @prereset: prereset method (can be NULL) 3613c6fd2807SJeff Garzik * @softreset: softreset method (can be NULL) 3614c6fd2807SJeff Garzik * @hardreset: hardreset method (can be NULL) 3615c6fd2807SJeff Garzik * @postreset: postreset method (can be NULL) 36169b1e2658STejun Heo * @r_failed_link: out parameter for failed link 3617c6fd2807SJeff Garzik * 3618c6fd2807SJeff Garzik * This is the alpha and omega, eum and yang, heart and soul of 3619c6fd2807SJeff Garzik * libata exception handling. On entry, actions required to 36209b1e2658STejun Heo * recover each link and hotplug requests are recorded in the 36219b1e2658STejun Heo * link's eh_context. This function executes all the operations 36229b1e2658STejun Heo * with appropriate retrials and fallbacks to resurrect failed 3623c6fd2807SJeff Garzik * devices, detach goners and greet newcomers. 3624c6fd2807SJeff Garzik * 3625c6fd2807SJeff Garzik * LOCKING: 3626c6fd2807SJeff Garzik * Kernel thread context (may sleep). 3627c6fd2807SJeff Garzik * 3628c6fd2807SJeff Garzik * RETURNS: 3629c6fd2807SJeff Garzik * 0 on success, -errno on failure. 3630c6fd2807SJeff Garzik */ 3631fb7fd614STejun Heo int ata_eh_recover(struct ata_port *ap, ata_prereset_fn_t prereset, 3632c6fd2807SJeff Garzik ata_reset_fn_t softreset, ata_reset_fn_t hardreset, 36339b1e2658STejun Heo ata_postreset_fn_t postreset, 36349b1e2658STejun Heo struct ata_link **r_failed_link) 3635c6fd2807SJeff Garzik { 36369b1e2658STejun Heo struct ata_link *link; 3637c6fd2807SJeff Garzik struct ata_device *dev; 36386b7ae954STejun Heo int rc, nr_fails; 363945fabbb7SElias Oltmanns unsigned long flags, deadline; 3640c6fd2807SJeff Garzik 3641c6fd2807SJeff Garzik DPRINTK("ENTER\n"); 3642c6fd2807SJeff Garzik 3643c6fd2807SJeff Garzik /* prep for recovery */ 36441eca4365STejun Heo ata_for_each_link(link, ap, EDGE) { 36459b1e2658STejun Heo struct ata_eh_context *ehc = &link->eh_context; 36469b1e2658STejun Heo 3647f9df58cbSTejun Heo /* re-enable link? */ 3648f9df58cbSTejun Heo if (ehc->i.action & ATA_EH_ENABLE_LINK) { 3649f9df58cbSTejun Heo ata_eh_about_to_do(link, NULL, ATA_EH_ENABLE_LINK); 3650f9df58cbSTejun Heo spin_lock_irqsave(ap->lock, flags); 3651f9df58cbSTejun Heo link->flags &= ~ATA_LFLAG_DISABLED; 3652f9df58cbSTejun Heo spin_unlock_irqrestore(ap->lock, flags); 3653f9df58cbSTejun Heo ata_eh_done(link, NULL, ATA_EH_ENABLE_LINK); 3654f9df58cbSTejun Heo } 3655f9df58cbSTejun Heo 36561eca4365STejun Heo ata_for_each_dev(dev, link, ALL) { 3657fd995f70STejun Heo if (link->flags & ATA_LFLAG_NO_RETRY) 3658fd995f70STejun Heo ehc->tries[dev->devno] = 1; 3659fd995f70STejun Heo else 3660c6fd2807SJeff Garzik ehc->tries[dev->devno] = ATA_EH_DEV_TRIES; 3661c6fd2807SJeff Garzik 366279a55b72STejun Heo /* collect port action mask recorded in dev actions */ 36639b1e2658STejun Heo ehc->i.action |= ehc->i.dev_action[dev->devno] & 36649b1e2658STejun Heo ~ATA_EH_PERDEV_MASK; 3665f58229f8STejun Heo ehc->i.dev_action[dev->devno] &= ATA_EH_PERDEV_MASK; 366679a55b72STejun Heo 3667c6fd2807SJeff Garzik /* process hotplug request */ 3668c6fd2807SJeff Garzik if (dev->flags & ATA_DFLAG_DETACH) 3669c6fd2807SJeff Garzik ata_eh_detach_dev(dev); 3670c6fd2807SJeff Garzik 367102c05a27STejun Heo /* schedule probe if necessary */ 367202c05a27STejun Heo if (!ata_dev_enabled(dev)) 367302c05a27STejun Heo ata_eh_schedule_probe(dev); 3674c6fd2807SJeff Garzik } 36759b1e2658STejun Heo } 3676c6fd2807SJeff Garzik 3677c6fd2807SJeff Garzik retry: 3678c6fd2807SJeff Garzik rc = 0; 3679c6fd2807SJeff Garzik 3680c6fd2807SJeff Garzik /* if UNLOADING, finish immediately */ 3681c6fd2807SJeff Garzik if (ap->pflags & ATA_PFLAG_UNLOADING) 3682c6fd2807SJeff Garzik goto out; 3683c6fd2807SJeff Garzik 36849b1e2658STejun Heo /* prep for EH */ 36851eca4365STejun Heo ata_for_each_link(link, ap, EDGE) { 36869b1e2658STejun Heo struct ata_eh_context *ehc = &link->eh_context; 36879b1e2658STejun Heo 3688c6fd2807SJeff Garzik /* skip EH if possible. */ 36890260731fSTejun Heo if (ata_eh_skip_recovery(link)) 3690c6fd2807SJeff Garzik ehc->i.action = 0; 3691c6fd2807SJeff Garzik 36921eca4365STejun Heo ata_for_each_dev(dev, link, ALL) 3693f58229f8STejun Heo ehc->classes[dev->devno] = ATA_DEV_UNKNOWN; 36949b1e2658STejun Heo } 3695c6fd2807SJeff Garzik 3696c6fd2807SJeff Garzik /* reset */ 36971eca4365STejun Heo ata_for_each_link(link, ap, EDGE) { 36989b1e2658STejun Heo struct ata_eh_context *ehc = &link->eh_context; 36999b1e2658STejun Heo 3700cf480626STejun Heo if (!(ehc->i.action & ATA_EH_RESET)) 37019b1e2658STejun Heo continue; 37029b1e2658STejun Heo 37039b1e2658STejun Heo rc = ata_eh_reset(link, ata_link_nr_vacant(link), 3704dc98c32cSTejun Heo prereset, softreset, hardreset, postreset); 3705c6fd2807SJeff Garzik if (rc) { 3706a9a79dfeSJoe Perches ata_link_err(link, "reset failed, giving up\n"); 3707c6fd2807SJeff Garzik goto out; 3708c6fd2807SJeff Garzik } 37099b1e2658STejun Heo } 3710c6fd2807SJeff Garzik 371145fabbb7SElias Oltmanns do { 371245fabbb7SElias Oltmanns unsigned long now; 371345fabbb7SElias Oltmanns 371445fabbb7SElias Oltmanns /* 371545fabbb7SElias Oltmanns * clears ATA_EH_PARK in eh_info and resets 371645fabbb7SElias Oltmanns * ap->park_req_pending 371745fabbb7SElias Oltmanns */ 371845fabbb7SElias Oltmanns ata_eh_pull_park_action(ap); 371945fabbb7SElias Oltmanns 372045fabbb7SElias Oltmanns deadline = jiffies; 37211eca4365STejun Heo ata_for_each_link(link, ap, EDGE) { 37221eca4365STejun Heo ata_for_each_dev(dev, link, ALL) { 372345fabbb7SElias Oltmanns struct ata_eh_context *ehc = &link->eh_context; 372445fabbb7SElias Oltmanns unsigned long tmp; 372545fabbb7SElias Oltmanns 372645fabbb7SElias Oltmanns if (dev->class != ATA_DEV_ATA) 372745fabbb7SElias Oltmanns continue; 372845fabbb7SElias Oltmanns if (!(ehc->i.dev_action[dev->devno] & 372945fabbb7SElias Oltmanns ATA_EH_PARK)) 373045fabbb7SElias Oltmanns continue; 373145fabbb7SElias Oltmanns tmp = dev->unpark_deadline; 373245fabbb7SElias Oltmanns if (time_before(deadline, tmp)) 373345fabbb7SElias Oltmanns deadline = tmp; 373445fabbb7SElias Oltmanns else if (time_before_eq(tmp, jiffies)) 373545fabbb7SElias Oltmanns continue; 373645fabbb7SElias Oltmanns if (ehc->unloaded_mask & (1 << dev->devno)) 373745fabbb7SElias Oltmanns continue; 373845fabbb7SElias Oltmanns 373945fabbb7SElias Oltmanns ata_eh_park_issue_cmd(dev, 1); 374045fabbb7SElias Oltmanns } 374145fabbb7SElias Oltmanns } 374245fabbb7SElias Oltmanns 374345fabbb7SElias Oltmanns now = jiffies; 374445fabbb7SElias Oltmanns if (time_before_eq(deadline, now)) 374545fabbb7SElias Oltmanns break; 374645fabbb7SElias Oltmanns 3747c0c362b6STejun Heo ata_eh_release(ap); 374845fabbb7SElias Oltmanns deadline = wait_for_completion_timeout(&ap->park_req_pending, 374945fabbb7SElias Oltmanns deadline - now); 3750c0c362b6STejun Heo ata_eh_acquire(ap); 375145fabbb7SElias Oltmanns } while (deadline); 37521eca4365STejun Heo ata_for_each_link(link, ap, EDGE) { 37531eca4365STejun Heo ata_for_each_dev(dev, link, ALL) { 375445fabbb7SElias Oltmanns if (!(link->eh_context.unloaded_mask & 375545fabbb7SElias Oltmanns (1 << dev->devno))) 375645fabbb7SElias Oltmanns continue; 375745fabbb7SElias Oltmanns 375845fabbb7SElias Oltmanns ata_eh_park_issue_cmd(dev, 0); 375945fabbb7SElias Oltmanns ata_eh_done(link, dev, ATA_EH_PARK); 376045fabbb7SElias Oltmanns } 376145fabbb7SElias Oltmanns } 376245fabbb7SElias Oltmanns 37639b1e2658STejun Heo /* the rest */ 37646b7ae954STejun Heo nr_fails = 0; 37656b7ae954STejun Heo ata_for_each_link(link, ap, PMP_FIRST) { 37669b1e2658STejun Heo struct ata_eh_context *ehc = &link->eh_context; 37679b1e2658STejun Heo 37686b7ae954STejun Heo if (sata_pmp_attached(ap) && ata_is_host_link(link)) 37696b7ae954STejun Heo goto config_lpm; 37706b7ae954STejun Heo 3771c6fd2807SJeff Garzik /* revalidate existing devices and attach new ones */ 37720260731fSTejun Heo rc = ata_eh_revalidate_and_attach(link, &dev); 3773c6fd2807SJeff Garzik if (rc) 37746b7ae954STejun Heo goto rest_fail; 3775c6fd2807SJeff Garzik 3776633273a3STejun Heo /* if PMP got attached, return, pmp EH will take care of it */ 3777633273a3STejun Heo if (link->device->class == ATA_DEV_PMP) { 3778633273a3STejun Heo ehc->i.action = 0; 3779633273a3STejun Heo return 0; 3780633273a3STejun Heo } 3781633273a3STejun Heo 3782baa1e78aSTejun Heo /* configure transfer mode if necessary */ 3783baa1e78aSTejun Heo if (ehc->i.flags & ATA_EHI_SETMODE) { 37840260731fSTejun Heo rc = ata_set_mode(link, &dev); 37854ae72a1eSTejun Heo if (rc) 37866b7ae954STejun Heo goto rest_fail; 3787baa1e78aSTejun Heo ehc->i.flags &= ~ATA_EHI_SETMODE; 3788c6fd2807SJeff Garzik } 3789c6fd2807SJeff Garzik 379011fc33daSTejun Heo /* If reset has been issued, clear UA to avoid 379111fc33daSTejun Heo * disrupting the current users of the device. 379211fc33daSTejun Heo */ 379311fc33daSTejun Heo if (ehc->i.flags & ATA_EHI_DID_RESET) { 37941eca4365STejun Heo ata_for_each_dev(dev, link, ALL) { 379511fc33daSTejun Heo if (dev->class != ATA_DEV_ATAPI) 379611fc33daSTejun Heo continue; 379711fc33daSTejun Heo rc = atapi_eh_clear_ua(dev); 379811fc33daSTejun Heo if (rc) 37996b7ae954STejun Heo goto rest_fail; 380011fc33daSTejun Heo } 380111fc33daSTejun Heo } 380211fc33daSTejun Heo 38036013efd8STejun Heo /* retry flush if necessary */ 38046013efd8STejun Heo ata_for_each_dev(dev, link, ALL) { 38056013efd8STejun Heo if (dev->class != ATA_DEV_ATA) 38066013efd8STejun Heo continue; 38076013efd8STejun Heo rc = ata_eh_maybe_retry_flush(dev); 38086013efd8STejun Heo if (rc) 38096b7ae954STejun Heo goto rest_fail; 38106013efd8STejun Heo } 38116013efd8STejun Heo 38126b7ae954STejun Heo config_lpm: 381311fc33daSTejun Heo /* configure link power saving */ 38146b7ae954STejun Heo if (link->lpm_policy != ap->target_lpm_policy) { 38156b7ae954STejun Heo rc = ata_eh_set_lpm(link, ap->target_lpm_policy, &dev); 38166b7ae954STejun Heo if (rc) 38176b7ae954STejun Heo goto rest_fail; 38186b7ae954STejun Heo } 3819ca77329fSKristen Carlson Accardi 38209b1e2658STejun Heo /* this link is okay now */ 38219b1e2658STejun Heo ehc->i.flags = 0; 38229b1e2658STejun Heo continue; 3823c6fd2807SJeff Garzik 38246b7ae954STejun Heo rest_fail: 38256b7ae954STejun Heo nr_fails++; 38266b7ae954STejun Heo if (dev) 38270a2c0f56STejun Heo ata_eh_handle_dev_fail(dev, rc); 3828c6fd2807SJeff Garzik 3829b06ce3e5STejun Heo if (ap->pflags & ATA_PFLAG_FROZEN) { 3830b06ce3e5STejun Heo /* PMP reset requires working host port. 3831b06ce3e5STejun Heo * Can't retry if it's frozen. 3832b06ce3e5STejun Heo */ 3833071f44b1STejun Heo if (sata_pmp_attached(ap)) 3834b06ce3e5STejun Heo goto out; 38359b1e2658STejun Heo break; 38369b1e2658STejun Heo } 3837b06ce3e5STejun Heo } 38389b1e2658STejun Heo 38396b7ae954STejun Heo if (nr_fails) 3840c6fd2807SJeff Garzik goto retry; 3841c6fd2807SJeff Garzik 3842c6fd2807SJeff Garzik out: 38439b1e2658STejun Heo if (rc && r_failed_link) 38449b1e2658STejun Heo *r_failed_link = link; 3845c6fd2807SJeff Garzik 3846c6fd2807SJeff Garzik DPRINTK("EXIT, rc=%d\n", rc); 3847c6fd2807SJeff Garzik return rc; 3848c6fd2807SJeff Garzik } 3849c6fd2807SJeff Garzik 3850c6fd2807SJeff Garzik /** 3851c6fd2807SJeff Garzik * ata_eh_finish - finish up EH 3852c6fd2807SJeff Garzik * @ap: host port to finish EH for 3853c6fd2807SJeff Garzik * 3854c6fd2807SJeff Garzik * Recovery is complete. Clean up EH states and retry or finish 3855c6fd2807SJeff Garzik * failed qcs. 3856c6fd2807SJeff Garzik * 3857c6fd2807SJeff Garzik * LOCKING: 3858c6fd2807SJeff Garzik * None. 3859c6fd2807SJeff Garzik */ 3860fb7fd614STejun Heo void ata_eh_finish(struct ata_port *ap) 3861c6fd2807SJeff Garzik { 3862c6fd2807SJeff Garzik int tag; 3863c6fd2807SJeff Garzik 3864c6fd2807SJeff Garzik /* retry or finish qcs */ 3865c6fd2807SJeff Garzik for (tag = 0; tag < ATA_MAX_QUEUE; tag++) { 3866c6fd2807SJeff Garzik struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag); 3867c6fd2807SJeff Garzik 3868c6fd2807SJeff Garzik if (!(qc->flags & ATA_QCFLAG_FAILED)) 3869c6fd2807SJeff Garzik continue; 3870c6fd2807SJeff Garzik 3871c6fd2807SJeff Garzik if (qc->err_mask) { 3872c6fd2807SJeff Garzik /* FIXME: Once EH migration is complete, 3873c6fd2807SJeff Garzik * generate sense data in this function, 3874c6fd2807SJeff Garzik * considering both err_mask and tf. 3875c6fd2807SJeff Garzik */ 387603faab78STejun Heo if (qc->flags & ATA_QCFLAG_RETRY) 3877c6fd2807SJeff Garzik ata_eh_qc_retry(qc); 387803faab78STejun Heo else 387903faab78STejun Heo ata_eh_qc_complete(qc); 3880c6fd2807SJeff Garzik } else { 3881c6fd2807SJeff Garzik if (qc->flags & ATA_QCFLAG_SENSE_VALID) { 3882c6fd2807SJeff Garzik ata_eh_qc_complete(qc); 3883c6fd2807SJeff Garzik } else { 3884c6fd2807SJeff Garzik /* feed zero TF to sense generation */ 3885c6fd2807SJeff Garzik memset(&qc->result_tf, 0, sizeof(qc->result_tf)); 3886c6fd2807SJeff Garzik ata_eh_qc_retry(qc); 3887c6fd2807SJeff Garzik } 3888c6fd2807SJeff Garzik } 3889c6fd2807SJeff Garzik } 3890da917d69STejun Heo 3891da917d69STejun Heo /* make sure nr_active_links is zero after EH */ 3892da917d69STejun Heo WARN_ON(ap->nr_active_links); 3893da917d69STejun Heo ap->nr_active_links = 0; 3894c6fd2807SJeff Garzik } 3895c6fd2807SJeff Garzik 3896c6fd2807SJeff Garzik /** 3897c6fd2807SJeff Garzik * ata_do_eh - do standard error handling 3898c6fd2807SJeff Garzik * @ap: host port to handle error for 3899a1efdabaSTejun Heo * 3900c6fd2807SJeff Garzik * @prereset: prereset method (can be NULL) 3901c6fd2807SJeff Garzik * @softreset: softreset method (can be NULL) 3902c6fd2807SJeff Garzik * @hardreset: hardreset method (can be NULL) 3903c6fd2807SJeff Garzik * @postreset: postreset method (can be NULL) 3904c6fd2807SJeff Garzik * 3905c6fd2807SJeff Garzik * Perform standard error handling sequence. 3906c6fd2807SJeff Garzik * 3907c6fd2807SJeff Garzik * LOCKING: 3908c6fd2807SJeff Garzik * Kernel thread context (may sleep). 3909c6fd2807SJeff Garzik */ 3910c6fd2807SJeff Garzik void ata_do_eh(struct ata_port *ap, ata_prereset_fn_t prereset, 3911c6fd2807SJeff Garzik ata_reset_fn_t softreset, ata_reset_fn_t hardreset, 3912c6fd2807SJeff Garzik ata_postreset_fn_t postreset) 3913c6fd2807SJeff Garzik { 39149b1e2658STejun Heo struct ata_device *dev; 39159b1e2658STejun Heo int rc; 39169b1e2658STejun Heo 39179b1e2658STejun Heo ata_eh_autopsy(ap); 39189b1e2658STejun Heo ata_eh_report(ap); 39199b1e2658STejun Heo 39209b1e2658STejun Heo rc = ata_eh_recover(ap, prereset, softreset, hardreset, postreset, 39219b1e2658STejun Heo NULL); 39229b1e2658STejun Heo if (rc) { 39231eca4365STejun Heo ata_for_each_dev(dev, &ap->link, ALL) 39249b1e2658STejun Heo ata_dev_disable(dev); 39259b1e2658STejun Heo } 39269b1e2658STejun Heo 3927c6fd2807SJeff Garzik ata_eh_finish(ap); 3928c6fd2807SJeff Garzik } 3929c6fd2807SJeff Garzik 3930a1efdabaSTejun Heo /** 3931a1efdabaSTejun Heo * ata_std_error_handler - standard error handler 3932a1efdabaSTejun Heo * @ap: host port to handle error for 3933a1efdabaSTejun Heo * 3934a1efdabaSTejun Heo * Standard error handler 3935a1efdabaSTejun Heo * 3936a1efdabaSTejun Heo * LOCKING: 3937a1efdabaSTejun Heo * Kernel thread context (may sleep). 3938a1efdabaSTejun Heo */ 3939a1efdabaSTejun Heo void ata_std_error_handler(struct ata_port *ap) 3940a1efdabaSTejun Heo { 3941a1efdabaSTejun Heo struct ata_port_operations *ops = ap->ops; 3942a1efdabaSTejun Heo ata_reset_fn_t hardreset = ops->hardreset; 3943a1efdabaSTejun Heo 394457c9efdfSTejun Heo /* ignore built-in hardreset if SCR access is not available */ 3945fe06e5f9STejun Heo if (hardreset == sata_std_hardreset && !sata_scr_valid(&ap->link)) 3946a1efdabaSTejun Heo hardreset = NULL; 3947a1efdabaSTejun Heo 3948a1efdabaSTejun Heo ata_do_eh(ap, ops->prereset, ops->softreset, hardreset, ops->postreset); 3949a1efdabaSTejun Heo } 3950a1efdabaSTejun Heo 39516ffa01d8STejun Heo #ifdef CONFIG_PM 3952c6fd2807SJeff Garzik /** 3953c6fd2807SJeff Garzik * ata_eh_handle_port_suspend - perform port suspend operation 3954c6fd2807SJeff Garzik * @ap: port to suspend 3955c6fd2807SJeff Garzik * 3956c6fd2807SJeff Garzik * Suspend @ap. 3957c6fd2807SJeff Garzik * 3958c6fd2807SJeff Garzik * LOCKING: 3959c6fd2807SJeff Garzik * Kernel thread context (may sleep). 3960c6fd2807SJeff Garzik */ 3961c6fd2807SJeff Garzik static void ata_eh_handle_port_suspend(struct ata_port *ap) 3962c6fd2807SJeff Garzik { 3963c6fd2807SJeff Garzik unsigned long flags; 3964c6fd2807SJeff Garzik int rc = 0; 3965c6fd2807SJeff Garzik 3966c6fd2807SJeff Garzik /* are we suspending? */ 3967c6fd2807SJeff Garzik spin_lock_irqsave(ap->lock, flags); 3968c6fd2807SJeff Garzik if (!(ap->pflags & ATA_PFLAG_PM_PENDING) || 3969c6fd2807SJeff Garzik ap->pm_mesg.event == PM_EVENT_ON) { 3970c6fd2807SJeff Garzik spin_unlock_irqrestore(ap->lock, flags); 3971c6fd2807SJeff Garzik return; 3972c6fd2807SJeff Garzik } 3973c6fd2807SJeff Garzik spin_unlock_irqrestore(ap->lock, flags); 3974c6fd2807SJeff Garzik 3975c6fd2807SJeff Garzik WARN_ON(ap->pflags & ATA_PFLAG_SUSPENDED); 3976c6fd2807SJeff Garzik 397764578a3dSTejun Heo /* tell ACPI we're suspending */ 397864578a3dSTejun Heo rc = ata_acpi_on_suspend(ap); 397964578a3dSTejun Heo if (rc) 398064578a3dSTejun Heo goto out; 398164578a3dSTejun Heo 3982c6fd2807SJeff Garzik /* suspend */ 3983c6fd2807SJeff Garzik ata_eh_freeze_port(ap); 3984c6fd2807SJeff Garzik 3985c6fd2807SJeff Garzik if (ap->ops->port_suspend) 3986c6fd2807SJeff Garzik rc = ap->ops->port_suspend(ap, ap->pm_mesg); 3987c6fd2807SJeff Garzik 3988bd3adca5SShaohua Li ata_acpi_set_state(ap, PMSG_SUSPEND); 398964578a3dSTejun Heo out: 3990c6fd2807SJeff Garzik /* report result */ 3991c6fd2807SJeff Garzik spin_lock_irqsave(ap->lock, flags); 3992c6fd2807SJeff Garzik 3993c6fd2807SJeff Garzik ap->pflags &= ~ATA_PFLAG_PM_PENDING; 3994c6fd2807SJeff Garzik if (rc == 0) 3995c6fd2807SJeff Garzik ap->pflags |= ATA_PFLAG_SUSPENDED; 399664578a3dSTejun Heo else if (ap->pflags & ATA_PFLAG_FROZEN) 3997c6fd2807SJeff Garzik ata_port_schedule_eh(ap); 3998c6fd2807SJeff Garzik 3999c6fd2807SJeff Garzik if (ap->pm_result) { 4000c6fd2807SJeff Garzik *ap->pm_result = rc; 4001c6fd2807SJeff Garzik ap->pm_result = NULL; 4002c6fd2807SJeff Garzik } 4003c6fd2807SJeff Garzik 4004c6fd2807SJeff Garzik spin_unlock_irqrestore(ap->lock, flags); 4005c6fd2807SJeff Garzik 4006c6fd2807SJeff Garzik return; 4007c6fd2807SJeff Garzik } 4008c6fd2807SJeff Garzik 4009c6fd2807SJeff Garzik /** 4010c6fd2807SJeff Garzik * ata_eh_handle_port_resume - perform port resume operation 4011c6fd2807SJeff Garzik * @ap: port to resume 4012c6fd2807SJeff Garzik * 4013c6fd2807SJeff Garzik * Resume @ap. 4014c6fd2807SJeff Garzik * 4015c6fd2807SJeff Garzik * LOCKING: 4016c6fd2807SJeff Garzik * Kernel thread context (may sleep). 4017c6fd2807SJeff Garzik */ 4018c6fd2807SJeff Garzik static void ata_eh_handle_port_resume(struct ata_port *ap) 4019c6fd2807SJeff Garzik { 40206f9c1ea2STejun Heo struct ata_link *link; 40216f9c1ea2STejun Heo struct ata_device *dev; 4022c6fd2807SJeff Garzik unsigned long flags; 40239666f400STejun Heo int rc = 0; 4024c6fd2807SJeff Garzik 4025c6fd2807SJeff Garzik /* are we resuming? */ 4026c6fd2807SJeff Garzik spin_lock_irqsave(ap->lock, flags); 4027c6fd2807SJeff Garzik if (!(ap->pflags & ATA_PFLAG_PM_PENDING) || 4028c6fd2807SJeff Garzik ap->pm_mesg.event != PM_EVENT_ON) { 4029c6fd2807SJeff Garzik spin_unlock_irqrestore(ap->lock, flags); 4030c6fd2807SJeff Garzik return; 4031c6fd2807SJeff Garzik } 4032c6fd2807SJeff Garzik spin_unlock_irqrestore(ap->lock, flags); 4033c6fd2807SJeff Garzik 40349666f400STejun Heo WARN_ON(!(ap->pflags & ATA_PFLAG_SUSPENDED)); 4035c6fd2807SJeff Garzik 40366f9c1ea2STejun Heo /* 40376f9c1ea2STejun Heo * Error timestamps are in jiffies which doesn't run while 40386f9c1ea2STejun Heo * suspended and PHY events during resume isn't too uncommon. 40396f9c1ea2STejun Heo * When the two are combined, it can lead to unnecessary speed 40406f9c1ea2STejun Heo * downs if the machine is suspended and resumed repeatedly. 40416f9c1ea2STejun Heo * Clear error history. 40426f9c1ea2STejun Heo */ 40436f9c1ea2STejun Heo ata_for_each_link(link, ap, HOST_FIRST) 40446f9c1ea2STejun Heo ata_for_each_dev(dev, link, ALL) 40456f9c1ea2STejun Heo ata_ering_clear(&dev->ering); 40466f9c1ea2STejun Heo 4047bd3adca5SShaohua Li ata_acpi_set_state(ap, PMSG_ON); 4048bd3adca5SShaohua Li 4049c6fd2807SJeff Garzik if (ap->ops->port_resume) 4050c6fd2807SJeff Garzik rc = ap->ops->port_resume(ap); 4051c6fd2807SJeff Garzik 40526746544cSTejun Heo /* tell ACPI that we're resuming */ 40536746544cSTejun Heo ata_acpi_on_resume(ap); 40546746544cSTejun Heo 40559666f400STejun Heo /* report result */ 4056c6fd2807SJeff Garzik spin_lock_irqsave(ap->lock, flags); 4057c6fd2807SJeff Garzik ap->pflags &= ~(ATA_PFLAG_PM_PENDING | ATA_PFLAG_SUSPENDED); 4058c6fd2807SJeff Garzik if (ap->pm_result) { 4059c6fd2807SJeff Garzik *ap->pm_result = rc; 4060c6fd2807SJeff Garzik ap->pm_result = NULL; 4061c6fd2807SJeff Garzik } 4062c6fd2807SJeff Garzik spin_unlock_irqrestore(ap->lock, flags); 4063c6fd2807SJeff Garzik } 40646ffa01d8STejun Heo #endif /* CONFIG_PM */ 4065