aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/ata/libata-eh.c
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/ata/libata-eh.c')
-rw-r--r--drivers/ata/libata-eh.c922
1 files changed, 668 insertions, 254 deletions
diff --git a/drivers/ata/libata-eh.c b/drivers/ata/libata-eh.c
index ac6ceed4bb60..2eaa39fc65d0 100644
--- a/drivers/ata/libata-eh.c
+++ b/drivers/ata/libata-eh.c
@@ -33,6 +33,7 @@
33 */ 33 */
34 34
35#include <linux/kernel.h> 35#include <linux/kernel.h>
36#include <linux/pci.h>
36#include <scsi/scsi.h> 37#include <scsi/scsi.h>
37#include <scsi/scsi_host.h> 38#include <scsi/scsi_host.h>
38#include <scsi/scsi_eh.h> 39#include <scsi/scsi_eh.h>
@@ -74,7 +75,6 @@ static const unsigned long ata_eh_reset_timeouts[] = {
74}; 75};
75 76
76static void __ata_port_freeze(struct ata_port *ap); 77static void __ata_port_freeze(struct ata_port *ap);
77static void ata_eh_finish(struct ata_port *ap);
78#ifdef CONFIG_PM 78#ifdef CONFIG_PM
79static void ata_eh_handle_port_suspend(struct ata_port *ap); 79static void ata_eh_handle_port_suspend(struct ata_port *ap);
80static void ata_eh_handle_port_resume(struct ata_port *ap); 80static void ata_eh_handle_port_resume(struct ata_port *ap);
@@ -151,6 +151,73 @@ void ata_ehi_clear_desc(struct ata_eh_info *ehi)
151 ehi->desc_len = 0; 151 ehi->desc_len = 0;
152} 152}
153 153
154/**
155 * ata_port_desc - append port description
156 * @ap: target ATA port
157 * @fmt: printf format string
158 *
159 * Format string according to @fmt and append it to port
160 * description. If port description is not empty, " " is added
161 * in-between. This function is to be used while initializing
162 * ata_host. The description is printed on host registration.
163 *
164 * LOCKING:
165 * None.
166 */
167void ata_port_desc(struct ata_port *ap, const char *fmt, ...)
168{
169 va_list args;
170
171 WARN_ON(!(ap->pflags & ATA_PFLAG_INITIALIZING));
172
173 if (ap->link.eh_info.desc_len)
174 __ata_ehi_push_desc(&ap->link.eh_info, " ");
175
176 va_start(args, fmt);
177 __ata_ehi_pushv_desc(&ap->link.eh_info, fmt, args);
178 va_end(args);
179}
180
181#ifdef CONFIG_PCI
182
183/**
184 * ata_port_pbar_desc - append PCI BAR description
185 * @ap: target ATA port
186 * @bar: target PCI BAR
187 * @offset: offset into PCI BAR
188 * @name: name of the area
189 *
190 * If @offset is negative, this function formats a string which
191 * contains the name, address, size and type of the BAR and
192 * appends it to the port description. If @offset is zero or
193 * positive, only name and offsetted address is appended.
194 *
195 * LOCKING:
196 * None.
197 */
198void ata_port_pbar_desc(struct ata_port *ap, int bar, ssize_t offset,
199 const char *name)
200{
201 struct pci_dev *pdev = to_pci_dev(ap->host->dev);
202 char *type = "";
203 unsigned long long start, len;
204
205 if (pci_resource_flags(pdev, bar) & IORESOURCE_MEM)
206 type = "m";
207 else if (pci_resource_flags(pdev, bar) & IORESOURCE_IO)
208 type = "i";
209
210 start = (unsigned long long)pci_resource_start(pdev, bar);
211 len = (unsigned long long)pci_resource_len(pdev, bar);
212
213 if (offset < 0)
214 ata_port_desc(ap, "%s %s%llu@0x%llx", name, type, len, start);
215 else
216 ata_port_desc(ap, "%s 0x%llx", name, start + offset);
217}
218
219#endif /* CONFIG_PCI */
220
154static void ata_ering_record(struct ata_ering *ering, int is_io, 221static void ata_ering_record(struct ata_ering *ering, int is_io,
155 unsigned int err_mask) 222 unsigned int err_mask)
156{ 223{
@@ -195,28 +262,29 @@ static int ata_ering_map(struct ata_ering *ering,
195 262
196static unsigned int ata_eh_dev_action(struct ata_device *dev) 263static unsigned int ata_eh_dev_action(struct ata_device *dev)
197{ 264{
198 struct ata_eh_context *ehc = &dev->ap->eh_context; 265 struct ata_eh_context *ehc = &dev->link->eh_context;
199 266
200 return ehc->i.action | ehc->i.dev_action[dev->devno]; 267 return ehc->i.action | ehc->i.dev_action[dev->devno];
201} 268}
202 269
203static void ata_eh_clear_action(struct ata_device *dev, 270static void ata_eh_clear_action(struct ata_link *link, struct ata_device *dev,
204 struct ata_eh_info *ehi, unsigned int action) 271 struct ata_eh_info *ehi, unsigned int action)
205{ 272{
206 int i; 273 struct ata_device *tdev;
207 274
208 if (!dev) { 275 if (!dev) {
209 ehi->action &= ~action; 276 ehi->action &= ~action;
210 for (i = 0; i < ATA_MAX_DEVICES; i++) 277 ata_link_for_each_dev(tdev, link)
211 ehi->dev_action[i] &= ~action; 278 ehi->dev_action[tdev->devno] &= ~action;
212 } else { 279 } else {
213 /* doesn't make sense for port-wide EH actions */ 280 /* doesn't make sense for port-wide EH actions */
214 WARN_ON(!(action & ATA_EH_PERDEV_MASK)); 281 WARN_ON(!(action & ATA_EH_PERDEV_MASK));
215 282
216 /* break ehi->action into ehi->dev_action */ 283 /* break ehi->action into ehi->dev_action */
217 if (ehi->action & action) { 284 if (ehi->action & action) {
218 for (i = 0; i < ATA_MAX_DEVICES; i++) 285 ata_link_for_each_dev(tdev, link)
219 ehi->dev_action[i] |= ehi->action & action; 286 ehi->dev_action[tdev->devno] |=
287 ehi->action & action;
220 ehi->action &= ~action; 288 ehi->action &= ~action;
221 } 289 }
222 290
@@ -261,7 +329,7 @@ enum scsi_eh_timer_return ata_scsi_timed_out(struct scsi_cmnd *cmd)
261 329
262 ret = EH_HANDLED; 330 ret = EH_HANDLED;
263 spin_lock_irqsave(ap->lock, flags); 331 spin_lock_irqsave(ap->lock, flags);
264 qc = ata_qc_from_tag(ap, ap->active_tag); 332 qc = ata_qc_from_tag(ap, ap->link.active_tag);
265 if (qc) { 333 if (qc) {
266 WARN_ON(qc->scsicmd != cmd); 334 WARN_ON(qc->scsicmd != cmd);
267 qc->flags |= ATA_QCFLAG_EH_SCHEDULED; 335 qc->flags |= ATA_QCFLAG_EH_SCHEDULED;
@@ -290,7 +358,7 @@ enum scsi_eh_timer_return ata_scsi_timed_out(struct scsi_cmnd *cmd)
290void ata_scsi_error(struct Scsi_Host *host) 358void ata_scsi_error(struct Scsi_Host *host)
291{ 359{
292 struct ata_port *ap = ata_shost_to_port(host); 360 struct ata_port *ap = ata_shost_to_port(host);
293 int i, repeat_cnt = ATA_EH_MAX_REPEAT; 361 int i;
294 unsigned long flags; 362 unsigned long flags;
295 363
296 DPRINTK("ENTER\n"); 364 DPRINTK("ENTER\n");
@@ -356,12 +424,17 @@ void ata_scsi_error(struct Scsi_Host *host)
356 __ata_port_freeze(ap); 424 __ata_port_freeze(ap);
357 425
358 spin_unlock_irqrestore(ap->lock, flags); 426 spin_unlock_irqrestore(ap->lock, flags);
427
428 /* initialize eh_tries */
429 ap->eh_tries = ATA_EH_MAX_TRIES;
359 } else 430 } else
360 spin_unlock_wait(ap->lock); 431 spin_unlock_wait(ap->lock);
361 432
362 repeat: 433 repeat:
363 /* invoke error handler */ 434 /* invoke error handler */
364 if (ap->ops->error_handler) { 435 if (ap->ops->error_handler) {
436 struct ata_link *link;
437
365 /* kill fast drain timer */ 438 /* kill fast drain timer */
366 del_timer_sync(&ap->fastdrain_timer); 439 del_timer_sync(&ap->fastdrain_timer);
367 440
@@ -371,12 +444,15 @@ void ata_scsi_error(struct Scsi_Host *host)
371 /* fetch & clear EH info */ 444 /* fetch & clear EH info */
372 spin_lock_irqsave(ap->lock, flags); 445 spin_lock_irqsave(ap->lock, flags);
373 446
374 memset(&ap->eh_context, 0, sizeof(ap->eh_context)); 447 __ata_port_for_each_link(link, ap) {
375 ap->eh_context.i = ap->eh_info; 448 memset(&link->eh_context, 0, sizeof(link->eh_context));
376 memset(&ap->eh_info, 0, sizeof(ap->eh_info)); 449 link->eh_context.i = link->eh_info;
450 memset(&link->eh_info, 0, sizeof(link->eh_info));
451 }
377 452
378 ap->pflags |= ATA_PFLAG_EH_IN_PROGRESS; 453 ap->pflags |= ATA_PFLAG_EH_IN_PROGRESS;
379 ap->pflags &= ~ATA_PFLAG_EH_PENDING; 454 ap->pflags &= ~ATA_PFLAG_EH_PENDING;
455 ap->excl_link = NULL; /* don't maintain exclusion over EH */
380 456
381 spin_unlock_irqrestore(ap->lock, flags); 457 spin_unlock_irqrestore(ap->lock, flags);
382 458
@@ -396,20 +472,18 @@ void ata_scsi_error(struct Scsi_Host *host)
396 spin_lock_irqsave(ap->lock, flags); 472 spin_lock_irqsave(ap->lock, flags);
397 473
398 if (ap->pflags & ATA_PFLAG_EH_PENDING) { 474 if (ap->pflags & ATA_PFLAG_EH_PENDING) {
399 if (--repeat_cnt) { 475 if (--ap->eh_tries) {
400 ata_port_printk(ap, KERN_INFO,
401 "EH pending after completion, "
402 "repeating EH (cnt=%d)\n", repeat_cnt);
403 spin_unlock_irqrestore(ap->lock, flags); 476 spin_unlock_irqrestore(ap->lock, flags);
404 goto repeat; 477 goto repeat;
405 } 478 }
406 ata_port_printk(ap, KERN_ERR, "EH pending after %d " 479 ata_port_printk(ap, KERN_ERR, "EH pending after %d "
407 "tries, giving up\n", ATA_EH_MAX_REPEAT); 480 "tries, giving up\n", ATA_EH_MAX_TRIES);
408 ap->pflags &= ~ATA_PFLAG_EH_PENDING; 481 ap->pflags &= ~ATA_PFLAG_EH_PENDING;
409 } 482 }
410 483
411 /* this run is complete, make sure EH info is clear */ 484 /* this run is complete, make sure EH info is clear */
412 memset(&ap->eh_info, 0, sizeof(ap->eh_info)); 485 __ata_port_for_each_link(link, ap)
486 memset(&link->eh_info, 0, sizeof(link->eh_info));
413 487
414 /* Clear host_eh_scheduled while holding ap->lock such 488 /* Clear host_eh_scheduled while holding ap->lock such
415 * that if exception occurs after this point but 489 * that if exception occurs after this point but
@@ -420,7 +494,7 @@ void ata_scsi_error(struct Scsi_Host *host)
420 494
421 spin_unlock_irqrestore(ap->lock, flags); 495 spin_unlock_irqrestore(ap->lock, flags);
422 } else { 496 } else {
423 WARN_ON(ata_qc_from_tag(ap, ap->active_tag) == NULL); 497 WARN_ON(ata_qc_from_tag(ap, ap->link.active_tag) == NULL);
424 ap->ops->eng_timeout(ap); 498 ap->ops->eng_timeout(ap);
425 } 499 }
426 500
@@ -575,7 +649,7 @@ void ata_eng_timeout(struct ata_port *ap)
575{ 649{
576 DPRINTK("ENTER\n"); 650 DPRINTK("ENTER\n");
577 651
578 ata_qc_timeout(ata_qc_from_tag(ap, ap->active_tag)); 652 ata_qc_timeout(ata_qc_from_tag(ap, ap->link.active_tag));
579 653
580 DPRINTK("EXIT\n"); 654 DPRINTK("EXIT\n");
581} 655}
@@ -718,19 +792,7 @@ void ata_port_schedule_eh(struct ata_port *ap)
718 DPRINTK("port EH scheduled\n"); 792 DPRINTK("port EH scheduled\n");
719} 793}
720 794
721/** 795static int ata_do_link_abort(struct ata_port *ap, struct ata_link *link)
722 * ata_port_abort - abort all qc's on the port
723 * @ap: ATA port to abort qc's for
724 *
725 * Abort all active qc's of @ap and schedule EH.
726 *
727 * LOCKING:
728 * spin_lock_irqsave(host lock)
729 *
730 * RETURNS:
731 * Number of aborted qc's.
732 */
733int ata_port_abort(struct ata_port *ap)
734{ 796{
735 int tag, nr_aborted = 0; 797 int tag, nr_aborted = 0;
736 798
@@ -742,7 +804,7 @@ int ata_port_abort(struct ata_port *ap)
742 for (tag = 0; tag < ATA_MAX_QUEUE; tag++) { 804 for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
743 struct ata_queued_cmd *qc = ata_qc_from_tag(ap, tag); 805 struct ata_queued_cmd *qc = ata_qc_from_tag(ap, tag);
744 806
745 if (qc) { 807 if (qc && (!link || qc->dev->link == link)) {
746 qc->flags |= ATA_QCFLAG_FAILED; 808 qc->flags |= ATA_QCFLAG_FAILED;
747 ata_qc_complete(qc); 809 ata_qc_complete(qc);
748 nr_aborted++; 810 nr_aborted++;
@@ -756,6 +818,40 @@ int ata_port_abort(struct ata_port *ap)
756} 818}
757 819
758/** 820/**
821 * ata_link_abort - abort all qc's on the link
822 * @link: ATA link to abort qc's for
823 *
824 * Abort all active qc's active on @link and schedule EH.
825 *
826 * LOCKING:
827 * spin_lock_irqsave(host lock)
828 *
829 * RETURNS:
830 * Number of aborted qc's.
831 */
832int ata_link_abort(struct ata_link *link)
833{
834 return ata_do_link_abort(link->ap, link);
835}
836
837/**
838 * ata_port_abort - abort all qc's on the port
839 * @ap: ATA port to abort qc's for
840 *
841 * Abort all active qc's of @ap and schedule EH.
842 *
843 * LOCKING:
844 * spin_lock_irqsave(host_set lock)
845 *
846 * RETURNS:
847 * Number of aborted qc's.
848 */
849int ata_port_abort(struct ata_port *ap)
850{
851 return ata_do_link_abort(ap, NULL);
852}
853
854/**
759 * __ata_port_freeze - freeze port 855 * __ata_port_freeze - freeze port
760 * @ap: ATA port to freeze 856 * @ap: ATA port to freeze
761 * 857 *
@@ -810,6 +906,79 @@ int ata_port_freeze(struct ata_port *ap)
810} 906}
811 907
812/** 908/**
909 * sata_async_notification - SATA async notification handler
910 * @ap: ATA port where async notification is received
911 *
912 * Handler to be called when async notification via SDB FIS is
913 * received. This function schedules EH if necessary.
914 *
915 * LOCKING:
916 * spin_lock_irqsave(host lock)
917 *
918 * RETURNS:
919 * 1 if EH is scheduled, 0 otherwise.
920 */
921int sata_async_notification(struct ata_port *ap)
922{
923 u32 sntf;
924 int rc;
925
926 if (!(ap->flags & ATA_FLAG_AN))
927 return 0;
928
929 rc = sata_scr_read(&ap->link, SCR_NOTIFICATION, &sntf);
930 if (rc == 0)
931 sata_scr_write(&ap->link, SCR_NOTIFICATION, sntf);
932
933 if (!ap->nr_pmp_links || rc) {
934 /* PMP is not attached or SNTF is not available */
935 if (!ap->nr_pmp_links) {
936 /* PMP is not attached. Check whether ATAPI
937 * AN is configured. If so, notify media
938 * change.
939 */
940 struct ata_device *dev = ap->link.device;
941
942 if ((dev->class == ATA_DEV_ATAPI) &&
943 (dev->flags & ATA_DFLAG_AN))
944 ata_scsi_media_change_notify(dev);
945 return 0;
946 } else {
947 /* PMP is attached but SNTF is not available.
948 * ATAPI async media change notification is
949 * not used. The PMP must be reporting PHY
950 * status change, schedule EH.
951 */
952 ata_port_schedule_eh(ap);
953 return 1;
954 }
955 } else {
956 /* PMP is attached and SNTF is available */
957 struct ata_link *link;
958
959 /* check and notify ATAPI AN */
960 ata_port_for_each_link(link, ap) {
961 if (!(sntf & (1 << link->pmp)))
962 continue;
963
964 if ((link->device->class == ATA_DEV_ATAPI) &&
965 (link->device->flags & ATA_DFLAG_AN))
966 ata_scsi_media_change_notify(link->device);
967 }
968
969 /* If PMP is reporting that PHY status of some
970 * downstream ports has changed, schedule EH.
971 */
972 if (sntf & (1 << SATA_PMP_CTRL_PORT)) {
973 ata_port_schedule_eh(ap);
974 return 1;
975 }
976
977 return 0;
978 }
979}
980
981/**
813 * ata_eh_freeze_port - EH helper to freeze port 982 * ata_eh_freeze_port - EH helper to freeze port
814 * @ap: ATA port to freeze 983 * @ap: ATA port to freeze
815 * 984 *
@@ -920,9 +1089,10 @@ void ata_eh_qc_retry(struct ata_queued_cmd *qc)
920 * LOCKING: 1089 * LOCKING:
921 * None. 1090 * None.
922 */ 1091 */
923static void ata_eh_detach_dev(struct ata_device *dev) 1092void ata_eh_detach_dev(struct ata_device *dev)
924{ 1093{
925 struct ata_port *ap = dev->ap; 1094 struct ata_link *link = dev->link;
1095 struct ata_port *ap = link->ap;
926 unsigned long flags; 1096 unsigned long flags;
927 1097
928 ata_dev_disable(dev); 1098 ata_dev_disable(dev);
@@ -937,31 +1107,32 @@ static void ata_eh_detach_dev(struct ata_device *dev)
937 } 1107 }
938 1108
939 /* clear per-dev EH actions */ 1109 /* clear per-dev EH actions */
940 ata_eh_clear_action(dev, &ap->eh_info, ATA_EH_PERDEV_MASK); 1110 ata_eh_clear_action(link, dev, &link->eh_info, ATA_EH_PERDEV_MASK);
941 ata_eh_clear_action(dev, &ap->eh_context.i, ATA_EH_PERDEV_MASK); 1111 ata_eh_clear_action(link, dev, &link->eh_context.i, ATA_EH_PERDEV_MASK);
942 1112
943 spin_unlock_irqrestore(ap->lock, flags); 1113 spin_unlock_irqrestore(ap->lock, flags);
944} 1114}
945 1115
946/** 1116/**
947 * ata_eh_about_to_do - about to perform eh_action 1117 * ata_eh_about_to_do - about to perform eh_action
948 * @ap: target ATA port 1118 * @link: target ATA link
949 * @dev: target ATA dev for per-dev action (can be NULL) 1119 * @dev: target ATA dev for per-dev action (can be NULL)
950 * @action: action about to be performed 1120 * @action: action about to be performed
951 * 1121 *
952 * Called just before performing EH actions to clear related bits 1122 * Called just before performing EH actions to clear related bits
953 * in @ap->eh_info such that eh actions are not unnecessarily 1123 * in @link->eh_info such that eh actions are not unnecessarily
954 * repeated. 1124 * repeated.
955 * 1125 *
956 * LOCKING: 1126 * LOCKING:
957 * None. 1127 * None.
958 */ 1128 */
959static void ata_eh_about_to_do(struct ata_port *ap, struct ata_device *dev, 1129void ata_eh_about_to_do(struct ata_link *link, struct ata_device *dev,
960 unsigned int action) 1130 unsigned int action)
961{ 1131{
1132 struct ata_port *ap = link->ap;
1133 struct ata_eh_info *ehi = &link->eh_info;
1134 struct ata_eh_context *ehc = &link->eh_context;
962 unsigned long flags; 1135 unsigned long flags;
963 struct ata_eh_info *ehi = &ap->eh_info;
964 struct ata_eh_context *ehc = &ap->eh_context;
965 1136
966 spin_lock_irqsave(ap->lock, flags); 1137 spin_lock_irqsave(ap->lock, flags);
967 1138
@@ -978,7 +1149,7 @@ static void ata_eh_about_to_do(struct ata_port *ap, struct ata_device *dev,
978 ehi->flags &= ~ATA_EHI_RESET_MODIFIER_MASK; 1149 ehi->flags &= ~ATA_EHI_RESET_MODIFIER_MASK;
979 } 1150 }
980 1151
981 ata_eh_clear_action(dev, ehi, action); 1152 ata_eh_clear_action(link, dev, ehi, action);
982 1153
983 if (!(ehc->i.flags & ATA_EHI_QUIET)) 1154 if (!(ehc->i.flags & ATA_EHI_QUIET))
984 ap->pflags |= ATA_PFLAG_RECOVERED; 1155 ap->pflags |= ATA_PFLAG_RECOVERED;
@@ -988,26 +1159,28 @@ static void ata_eh_about_to_do(struct ata_port *ap, struct ata_device *dev,
988 1159
989/** 1160/**
990 * ata_eh_done - EH action complete 1161 * ata_eh_done - EH action complete
991 * @ap: target ATA port 1162* @ap: target ATA port
992 * @dev: target ATA dev for per-dev action (can be NULL) 1163 * @dev: target ATA dev for per-dev action (can be NULL)
993 * @action: action just completed 1164 * @action: action just completed
994 * 1165 *
995 * Called right after performing EH actions to clear related bits 1166 * Called right after performing EH actions to clear related bits
996 * in @ap->eh_context. 1167 * in @link->eh_context.
997 * 1168 *
998 * LOCKING: 1169 * LOCKING:
999 * None. 1170 * None.
1000 */ 1171 */
1001static void ata_eh_done(struct ata_port *ap, struct ata_device *dev, 1172void ata_eh_done(struct ata_link *link, struct ata_device *dev,
1002 unsigned int action) 1173 unsigned int action)
1003{ 1174{
1175 struct ata_eh_context *ehc = &link->eh_context;
1176
1004 /* if reset is complete, clear all reset actions & reset modifier */ 1177 /* if reset is complete, clear all reset actions & reset modifier */
1005 if (action & ATA_EH_RESET_MASK) { 1178 if (action & ATA_EH_RESET_MASK) {
1006 action |= ATA_EH_RESET_MASK; 1179 action |= ATA_EH_RESET_MASK;
1007 ap->eh_context.i.flags &= ~ATA_EHI_RESET_MODIFIER_MASK; 1180 ehc->i.flags &= ~ATA_EHI_RESET_MODIFIER_MASK;
1008 } 1181 }
1009 1182
1010 ata_eh_clear_action(dev, &ap->eh_context.i, action); 1183 ata_eh_clear_action(link, dev, &ehc->i, action);
1011} 1184}
1012 1185
1013/** 1186/**
@@ -1077,7 +1250,7 @@ static unsigned int ata_read_log_page(struct ata_device *dev,
1077 tf.protocol = ATA_PROT_PIO; 1250 tf.protocol = ATA_PROT_PIO;
1078 1251
1079 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_FROM_DEVICE, 1252 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_FROM_DEVICE,
1080 buf, sectors * ATA_SECT_SIZE); 1253 buf, sectors * ATA_SECT_SIZE, 0);
1081 1254
1082 DPRINTK("EXIT, err_mask=%x\n", err_mask); 1255 DPRINTK("EXIT, err_mask=%x\n", err_mask);
1083 return err_mask; 1256 return err_mask;
@@ -1101,7 +1274,7 @@ static unsigned int ata_read_log_page(struct ata_device *dev,
1101static int ata_eh_read_log_10h(struct ata_device *dev, 1274static int ata_eh_read_log_10h(struct ata_device *dev,
1102 int *tag, struct ata_taskfile *tf) 1275 int *tag, struct ata_taskfile *tf)
1103{ 1276{
1104 u8 *buf = dev->ap->sector_buf; 1277 u8 *buf = dev->link->ap->sector_buf;
1105 unsigned int err_mask; 1278 unsigned int err_mask;
1106 u8 csum; 1279 u8 csum;
1107 int i; 1280 int i;
@@ -1155,7 +1328,7 @@ static unsigned int atapi_eh_request_sense(struct ata_queued_cmd *qc)
1155{ 1328{
1156 struct ata_device *dev = qc->dev; 1329 struct ata_device *dev = qc->dev;
1157 unsigned char *sense_buf = qc->scsicmd->sense_buffer; 1330 unsigned char *sense_buf = qc->scsicmd->sense_buffer;
1158 struct ata_port *ap = dev->ap; 1331 struct ata_port *ap = dev->link->ap;
1159 struct ata_taskfile tf; 1332 struct ata_taskfile tf;
1160 u8 cdb[ATAPI_CDB_LEN]; 1333 u8 cdb[ATAPI_CDB_LEN];
1161 1334
@@ -1191,12 +1364,12 @@ static unsigned int atapi_eh_request_sense(struct ata_queued_cmd *qc)
1191 } 1364 }
1192 1365
1193 return ata_exec_internal(dev, &tf, cdb, DMA_FROM_DEVICE, 1366 return ata_exec_internal(dev, &tf, cdb, DMA_FROM_DEVICE,
1194 sense_buf, SCSI_SENSE_BUFFERSIZE); 1367 sense_buf, SCSI_SENSE_BUFFERSIZE, 0);
1195} 1368}
1196 1369
1197/** 1370/**
1198 * ata_eh_analyze_serror - analyze SError for a failed port 1371 * ata_eh_analyze_serror - analyze SError for a failed port
1199 * @ap: ATA port to analyze SError for 1372 * @link: ATA link to analyze SError for
1200 * 1373 *
1201 * Analyze SError if available and further determine cause of 1374 * Analyze SError if available and further determine cause of
1202 * failure. 1375 * failure.
@@ -1204,11 +1377,12 @@ static unsigned int atapi_eh_request_sense(struct ata_queued_cmd *qc)
1204 * LOCKING: 1377 * LOCKING:
1205 * None. 1378 * None.
1206 */ 1379 */
1207static void ata_eh_analyze_serror(struct ata_port *ap) 1380static void ata_eh_analyze_serror(struct ata_link *link)
1208{ 1381{
1209 struct ata_eh_context *ehc = &ap->eh_context; 1382 struct ata_eh_context *ehc = &link->eh_context;
1210 u32 serror = ehc->i.serror; 1383 u32 serror = ehc->i.serror;
1211 unsigned int err_mask = 0, action = 0; 1384 unsigned int err_mask = 0, action = 0;
1385 u32 hotplug_mask;
1212 1386
1213 if (serror & SERR_PERSISTENT) { 1387 if (serror & SERR_PERSISTENT) {
1214 err_mask |= AC_ERR_ATA_BUS; 1388 err_mask |= AC_ERR_ATA_BUS;
@@ -1227,7 +1401,20 @@ static void ata_eh_analyze_serror(struct ata_port *ap)
1227 err_mask |= AC_ERR_SYSTEM; 1401 err_mask |= AC_ERR_SYSTEM;
1228 action |= ATA_EH_HARDRESET; 1402 action |= ATA_EH_HARDRESET;
1229 } 1403 }
1230 if (serror & (SERR_PHYRDY_CHG | SERR_DEV_XCHG)) 1404
1405 /* Determine whether a hotplug event has occurred. Both
1406 * SError.N/X are considered hotplug events for enabled or
1407 * host links. For disabled PMP links, only N bit is
1408 * considered as X bit is left at 1 for link plugging.
1409 */
1410 hotplug_mask = 0;
1411
1412 if (!(link->flags & ATA_LFLAG_DISABLED) || ata_is_host_link(link))
1413 hotplug_mask = SERR_PHYRDY_CHG | SERR_DEV_XCHG;
1414 else
1415 hotplug_mask = SERR_PHYRDY_CHG;
1416
1417 if (serror & hotplug_mask)
1231 ata_ehi_hotplugged(&ehc->i); 1418 ata_ehi_hotplugged(&ehc->i);
1232 1419
1233 ehc->i.err_mask |= err_mask; 1420 ehc->i.err_mask |= err_mask;
@@ -1236,7 +1423,7 @@ static void ata_eh_analyze_serror(struct ata_port *ap)
1236 1423
1237/** 1424/**
1238 * ata_eh_analyze_ncq_error - analyze NCQ error 1425 * ata_eh_analyze_ncq_error - analyze NCQ error
1239 * @ap: ATA port to analyze NCQ error for 1426 * @link: ATA link to analyze NCQ error for
1240 * 1427 *
1241 * Read log page 10h, determine the offending qc and acquire 1428 * Read log page 10h, determine the offending qc and acquire
1242 * error status TF. For NCQ device errors, all LLDDs have to do 1429 * error status TF. For NCQ device errors, all LLDDs have to do
@@ -1246,10 +1433,11 @@ static void ata_eh_analyze_serror(struct ata_port *ap)
1246 * LOCKING: 1433 * LOCKING:
1247 * Kernel thread context (may sleep). 1434 * Kernel thread context (may sleep).
1248 */ 1435 */
1249static void ata_eh_analyze_ncq_error(struct ata_port *ap) 1436static void ata_eh_analyze_ncq_error(struct ata_link *link)
1250{ 1437{
1251 struct ata_eh_context *ehc = &ap->eh_context; 1438 struct ata_port *ap = link->ap;
1252 struct ata_device *dev = ap->device; 1439 struct ata_eh_context *ehc = &link->eh_context;
1440 struct ata_device *dev = link->device;
1253 struct ata_queued_cmd *qc; 1441 struct ata_queued_cmd *qc;
1254 struct ata_taskfile tf; 1442 struct ata_taskfile tf;
1255 int tag, rc; 1443 int tag, rc;
@@ -1259,7 +1447,7 @@ static void ata_eh_analyze_ncq_error(struct ata_port *ap)
1259 return; 1447 return;
1260 1448
1261 /* is it NCQ device error? */ 1449 /* is it NCQ device error? */
1262 if (!ap->sactive || !(ehc->i.err_mask & AC_ERR_DEV)) 1450 if (!link->sactive || !(ehc->i.err_mask & AC_ERR_DEV))
1263 return; 1451 return;
1264 1452
1265 /* has LLDD analyzed already? */ 1453 /* has LLDD analyzed already? */
@@ -1276,13 +1464,13 @@ static void ata_eh_analyze_ncq_error(struct ata_port *ap)
1276 /* okay, this error is ours */ 1464 /* okay, this error is ours */
1277 rc = ata_eh_read_log_10h(dev, &tag, &tf); 1465 rc = ata_eh_read_log_10h(dev, &tag, &tf);
1278 if (rc) { 1466 if (rc) {
1279 ata_port_printk(ap, KERN_ERR, "failed to read log page 10h " 1467 ata_link_printk(link, KERN_ERR, "failed to read log page 10h "
1280 "(errno=%d)\n", rc); 1468 "(errno=%d)\n", rc);
1281 return; 1469 return;
1282 } 1470 }
1283 1471
1284 if (!(ap->sactive & (1 << tag))) { 1472 if (!(link->sactive & (1 << tag))) {
1285 ata_port_printk(ap, KERN_ERR, "log page 10h reported " 1473 ata_link_printk(link, KERN_ERR, "log page 10h reported "
1286 "inactive tag %d\n", tag); 1474 "inactive tag %d\n", tag);
1287 return; 1475 return;
1288 } 1476 }
@@ -1497,7 +1685,7 @@ static unsigned int ata_eh_speed_down(struct ata_device *dev, int is_io,
1497 /* speed down? */ 1685 /* speed down? */
1498 if (verdict & ATA_EH_SPDN_SPEED_DOWN) { 1686 if (verdict & ATA_EH_SPDN_SPEED_DOWN) {
1499 /* speed down SATA link speed if possible */ 1687 /* speed down SATA link speed if possible */
1500 if (sata_down_spd_limit(dev->ap) == 0) { 1688 if (sata_down_spd_limit(dev->link) == 0) {
1501 action |= ATA_EH_HARDRESET; 1689 action |= ATA_EH_HARDRESET;
1502 goto done; 1690 goto done;
1503 } 1691 }
@@ -1528,7 +1716,7 @@ static unsigned int ata_eh_speed_down(struct ata_device *dev, int is_io,
1528 * SATA. Consider it only for PATA. 1716 * SATA. Consider it only for PATA.
1529 */ 1717 */
1530 if ((verdict & ATA_EH_SPDN_FALLBACK_TO_PIO) && (dev->spdn_cnt >= 2) && 1718 if ((verdict & ATA_EH_SPDN_FALLBACK_TO_PIO) && (dev->spdn_cnt >= 2) &&
1531 (dev->ap->cbl != ATA_CBL_SATA) && 1719 (dev->link->ap->cbl != ATA_CBL_SATA) &&
1532 (dev->xfer_shift != ATA_SHIFT_PIO)) { 1720 (dev->xfer_shift != ATA_SHIFT_PIO)) {
1533 if (ata_down_xfermask_limit(dev, ATA_DNXFER_FORCE_PIO) == 0) { 1721 if (ata_down_xfermask_limit(dev, ATA_DNXFER_FORCE_PIO) == 0) {
1534 dev->spdn_cnt = 0; 1722 dev->spdn_cnt = 0;
@@ -1545,19 +1733,20 @@ static unsigned int ata_eh_speed_down(struct ata_device *dev, int is_io,
1545} 1733}
1546 1734
1547/** 1735/**
1548 * ata_eh_autopsy - analyze error and determine recovery action 1736 * ata_eh_link_autopsy - analyze error and determine recovery action
1549 * @ap: ATA port to perform autopsy on 1737 * @link: host link to perform autopsy on
1550 * 1738 *
1551 * Analyze why @ap failed and determine which recovery action is 1739 * Analyze why @link failed and determine which recovery actions
1552 * needed. This function also sets more detailed AC_ERR_* values 1740 * are needed. This function also sets more detailed AC_ERR_*
1553 * and fills sense data for ATAPI CHECK SENSE. 1741 * values and fills sense data for ATAPI CHECK SENSE.
1554 * 1742 *
1555 * LOCKING: 1743 * LOCKING:
1556 * Kernel thread context (may sleep). 1744 * Kernel thread context (may sleep).
1557 */ 1745 */
1558static void ata_eh_autopsy(struct ata_port *ap) 1746static void ata_eh_link_autopsy(struct ata_link *link)
1559{ 1747{
1560 struct ata_eh_context *ehc = &ap->eh_context; 1748 struct ata_port *ap = link->ap;
1749 struct ata_eh_context *ehc = &link->eh_context;
1561 unsigned int all_err_mask = 0; 1750 unsigned int all_err_mask = 0;
1562 int tag, is_io = 0; 1751 int tag, is_io = 0;
1563 u32 serror; 1752 u32 serror;
@@ -1569,10 +1758,10 @@ static void ata_eh_autopsy(struct ata_port *ap)
1569 return; 1758 return;
1570 1759
1571 /* obtain and analyze SError */ 1760 /* obtain and analyze SError */
1572 rc = sata_scr_read(ap, SCR_ERROR, &serror); 1761 rc = sata_scr_read(link, SCR_ERROR, &serror);
1573 if (rc == 0) { 1762 if (rc == 0) {
1574 ehc->i.serror |= serror; 1763 ehc->i.serror |= serror;
1575 ata_eh_analyze_serror(ap); 1764 ata_eh_analyze_serror(link);
1576 } else if (rc != -EOPNOTSUPP) { 1765 } else if (rc != -EOPNOTSUPP) {
1577 /* SError read failed, force hardreset and probing */ 1766 /* SError read failed, force hardreset and probing */
1578 ata_ehi_schedule_probe(&ehc->i); 1767 ata_ehi_schedule_probe(&ehc->i);
@@ -1581,7 +1770,7 @@ static void ata_eh_autopsy(struct ata_port *ap)
1581 } 1770 }
1582 1771
1583 /* analyze NCQ failure */ 1772 /* analyze NCQ failure */
1584 ata_eh_analyze_ncq_error(ap); 1773 ata_eh_analyze_ncq_error(link);
1585 1774
1586 /* any real error trumps AC_ERR_OTHER */ 1775 /* any real error trumps AC_ERR_OTHER */
1587 if (ehc->i.err_mask & ~AC_ERR_OTHER) 1776 if (ehc->i.err_mask & ~AC_ERR_OTHER)
@@ -1592,7 +1781,7 @@ static void ata_eh_autopsy(struct ata_port *ap)
1592 for (tag = 0; tag < ATA_MAX_QUEUE; tag++) { 1781 for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
1593 struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag); 1782 struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag);
1594 1783
1595 if (!(qc->flags & ATA_QCFLAG_FAILED)) 1784 if (!(qc->flags & ATA_QCFLAG_FAILED) || qc->dev->link != link)
1596 continue; 1785 continue;
1597 1786
1598 /* inherit upper level err_mask */ 1787 /* inherit upper level err_mask */
@@ -1646,20 +1835,43 @@ static void ata_eh_autopsy(struct ata_port *ap)
1646} 1835}
1647 1836
1648/** 1837/**
1649 * ata_eh_report - report error handling to user 1838 * ata_eh_autopsy - analyze error and determine recovery action
1650 * @ap: ATA port EH is going on 1839 * @ap: host port to perform autopsy on
1840 *
1841 * Analyze all links of @ap and determine why they failed and
1842 * which recovery actions are needed.
1843 *
1844 * LOCKING:
1845 * Kernel thread context (may sleep).
1846 */
1847void ata_eh_autopsy(struct ata_port *ap)
1848{
1849 struct ata_link *link;
1850
1851 __ata_port_for_each_link(link, ap)
1852 ata_eh_link_autopsy(link);
1853}
1854
1855/**
1856 * ata_eh_link_report - report error handling to user
1857 * @link: ATA link EH is going on
1651 * 1858 *
1652 * Report EH to user. 1859 * Report EH to user.
1653 * 1860 *
1654 * LOCKING: 1861 * LOCKING:
1655 * None. 1862 * None.
1656 */ 1863 */
1657static void ata_eh_report(struct ata_port *ap) 1864static void ata_eh_link_report(struct ata_link *link)
1658{ 1865{
1659 struct ata_eh_context *ehc = &ap->eh_context; 1866 struct ata_port *ap = link->ap;
1867 struct ata_eh_context *ehc = &link->eh_context;
1660 const char *frozen, *desc; 1868 const char *frozen, *desc;
1869 char tries_buf[6];
1661 int tag, nr_failed = 0; 1870 int tag, nr_failed = 0;
1662 1871
1872 if (ehc->i.flags & ATA_EHI_QUIET)
1873 return;
1874
1663 desc = NULL; 1875 desc = NULL;
1664 if (ehc->i.desc[0] != '\0') 1876 if (ehc->i.desc[0] != '\0')
1665 desc = ehc->i.desc; 1877 desc = ehc->i.desc;
@@ -1667,7 +1879,7 @@ static void ata_eh_report(struct ata_port *ap)
1667 for (tag = 0; tag < ATA_MAX_QUEUE; tag++) { 1879 for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
1668 struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag); 1880 struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag);
1669 1881
1670 if (!(qc->flags & ATA_QCFLAG_FAILED)) 1882 if (!(qc->flags & ATA_QCFLAG_FAILED) || qc->dev->link != link)
1671 continue; 1883 continue;
1672 if (qc->flags & ATA_QCFLAG_SENSE_VALID && !qc->err_mask) 1884 if (qc->flags & ATA_QCFLAG_SENSE_VALID && !qc->err_mask)
1673 continue; 1885 continue;
@@ -1682,22 +1894,48 @@ static void ata_eh_report(struct ata_port *ap)
1682 if (ap->pflags & ATA_PFLAG_FROZEN) 1894 if (ap->pflags & ATA_PFLAG_FROZEN)
1683 frozen = " frozen"; 1895 frozen = " frozen";
1684 1896
1897 memset(tries_buf, 0, sizeof(tries_buf));
1898 if (ap->eh_tries < ATA_EH_MAX_TRIES)
1899 snprintf(tries_buf, sizeof(tries_buf) - 1, " t%d",
1900 ap->eh_tries);
1901
1685 if (ehc->i.dev) { 1902 if (ehc->i.dev) {
1686 ata_dev_printk(ehc->i.dev, KERN_ERR, "exception Emask 0x%x " 1903 ata_dev_printk(ehc->i.dev, KERN_ERR, "exception Emask 0x%x "
1687 "SAct 0x%x SErr 0x%x action 0x%x%s\n", 1904 "SAct 0x%x SErr 0x%x action 0x%x%s%s\n",
1688 ehc->i.err_mask, ap->sactive, ehc->i.serror, 1905 ehc->i.err_mask, link->sactive, ehc->i.serror,
1689 ehc->i.action, frozen); 1906 ehc->i.action, frozen, tries_buf);
1690 if (desc) 1907 if (desc)
1691 ata_dev_printk(ehc->i.dev, KERN_ERR, "%s\n", desc); 1908 ata_dev_printk(ehc->i.dev, KERN_ERR, "%s\n", desc);
1692 } else { 1909 } else {
1693 ata_port_printk(ap, KERN_ERR, "exception Emask 0x%x " 1910 ata_link_printk(link, KERN_ERR, "exception Emask 0x%x "
1694 "SAct 0x%x SErr 0x%x action 0x%x%s\n", 1911 "SAct 0x%x SErr 0x%x action 0x%x%s%s\n",
1695 ehc->i.err_mask, ap->sactive, ehc->i.serror, 1912 ehc->i.err_mask, link->sactive, ehc->i.serror,
1696 ehc->i.action, frozen); 1913 ehc->i.action, frozen, tries_buf);
1697 if (desc) 1914 if (desc)
1698 ata_port_printk(ap, KERN_ERR, "%s\n", desc); 1915 ata_link_printk(link, KERN_ERR, "%s\n", desc);
1699 } 1916 }
1700 1917
1918 if (ehc->i.serror)
1919 ata_port_printk(ap, KERN_ERR,
1920 "SError: { %s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s}\n",
1921 ehc->i.serror & SERR_DATA_RECOVERED ? "RecovData " : "",
1922 ehc->i.serror & SERR_COMM_RECOVERED ? "RecovComm " : "",
1923 ehc->i.serror & SERR_DATA ? "UnrecovData " : "",
1924 ehc->i.serror & SERR_PERSISTENT ? "Persist " : "",
1925 ehc->i.serror & SERR_PROTOCOL ? "Proto " : "",
1926 ehc->i.serror & SERR_INTERNAL ? "HostInt " : "",
1927 ehc->i.serror & SERR_PHYRDY_CHG ? "PHYRdyChg " : "",
1928 ehc->i.serror & SERR_PHY_INT_ERR ? "PHYInt " : "",
1929 ehc->i.serror & SERR_COMM_WAKE ? "CommWake " : "",
1930 ehc->i.serror & SERR_10B_8B_ERR ? "10B8B " : "",
1931 ehc->i.serror & SERR_DISPARITY ? "Dispar " : "",
1932 ehc->i.serror & SERR_CRC ? "BadCRC " : "",
1933 ehc->i.serror & SERR_HANDSHAKE ? "Handshk " : "",
1934 ehc->i.serror & SERR_LINK_SEQ_ERR ? "LinkSeq " : "",
1935 ehc->i.serror & SERR_TRANS_ST_ERROR ? "TrStaTrns " : "",
1936 ehc->i.serror & SERR_UNRECOG_FIS ? "UnrecFIS " : "",
1937 ehc->i.serror & SERR_DEV_XCHG ? "DevExch " : "" );
1938
1701 for (tag = 0; tag < ATA_MAX_QUEUE; tag++) { 1939 for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
1702 static const char *dma_str[] = { 1940 static const char *dma_str[] = {
1703 [DMA_BIDIRECTIONAL] = "bidi", 1941 [DMA_BIDIRECTIONAL] = "bidi",
@@ -1708,7 +1946,8 @@ static void ata_eh_report(struct ata_port *ap)
1708 struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag); 1946 struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag);
1709 struct ata_taskfile *cmd = &qc->tf, *res = &qc->result_tf; 1947 struct ata_taskfile *cmd = &qc->tf, *res = &qc->result_tf;
1710 1948
1711 if (!(qc->flags & ATA_QCFLAG_FAILED) || !qc->err_mask) 1949 if (!(qc->flags & ATA_QCFLAG_FAILED) ||
1950 qc->dev->link != link || !qc->err_mask)
1712 continue; 1951 continue;
1713 1952
1714 ata_dev_printk(qc->dev, KERN_ERR, 1953 ata_dev_printk(qc->dev, KERN_ERR,
@@ -1728,18 +1967,60 @@ static void ata_eh_report(struct ata_port *ap)
1728 res->hob_lbal, res->hob_lbam, res->hob_lbah, 1967 res->hob_lbal, res->hob_lbam, res->hob_lbah,
1729 res->device, qc->err_mask, ata_err_string(qc->err_mask), 1968 res->device, qc->err_mask, ata_err_string(qc->err_mask),
1730 qc->err_mask & AC_ERR_NCQ ? " <F>" : ""); 1969 qc->err_mask & AC_ERR_NCQ ? " <F>" : "");
1970
1971 if (res->command & (ATA_BUSY | ATA_DRDY | ATA_DF | ATA_DRQ |
1972 ATA_ERR) ) {
1973 if (res->command & ATA_BUSY)
1974 ata_dev_printk(qc->dev, KERN_ERR,
1975 "status: { Busy }\n" );
1976 else
1977 ata_dev_printk(qc->dev, KERN_ERR,
1978 "status: { %s%s%s%s}\n",
1979 res->command & ATA_DRDY ? "DRDY " : "",
1980 res->command & ATA_DF ? "DF " : "",
1981 res->command & ATA_DRQ ? "DRQ " : "",
1982 res->command & ATA_ERR ? "ERR " : "" );
1983 }
1984
1985 if (cmd->command != ATA_CMD_PACKET &&
1986 (res->feature & (ATA_ICRC | ATA_UNC | ATA_IDNF |
1987 ATA_ABORTED)))
1988 ata_dev_printk(qc->dev, KERN_ERR,
1989 "error: { %s%s%s%s}\n",
1990 res->feature & ATA_ICRC ? "ICRC " : "",
1991 res->feature & ATA_UNC ? "UNC " : "",
1992 res->feature & ATA_IDNF ? "IDNF " : "",
1993 res->feature & ATA_ABORTED ? "ABRT " : "" );
1731 } 1994 }
1732} 1995}
1733 1996
1734static int ata_do_reset(struct ata_port *ap, ata_reset_fn_t reset, 1997/**
1998 * ata_eh_report - report error handling to user
1999 * @ap: ATA port to report EH about
2000 *
2001 * Report EH to user.
2002 *
2003 * LOCKING:
2004 * None.
2005 */
2006void ata_eh_report(struct ata_port *ap)
2007{
2008 struct ata_link *link;
2009
2010 __ata_port_for_each_link(link, ap)
2011 ata_eh_link_report(link);
2012}
2013
2014static int ata_do_reset(struct ata_link *link, ata_reset_fn_t reset,
1735 unsigned int *classes, unsigned long deadline) 2015 unsigned int *classes, unsigned long deadline)
1736{ 2016{
1737 int i, rc; 2017 struct ata_device *dev;
2018 int rc;
1738 2019
1739 for (i = 0; i < ATA_MAX_DEVICES; i++) 2020 ata_link_for_each_dev(dev, link)
1740 classes[i] = ATA_DEV_UNKNOWN; 2021 classes[dev->devno] = ATA_DEV_UNKNOWN;
1741 2022
1742 rc = reset(ap, classes, deadline); 2023 rc = reset(link, classes, deadline);
1743 if (rc) 2024 if (rc)
1744 return rc; 2025 return rc;
1745 2026
@@ -1747,71 +2028,87 @@ static int ata_do_reset(struct ata_port *ap, ata_reset_fn_t reset,
1747 * is complete and convert all ATA_DEV_UNKNOWN to 2028 * is complete and convert all ATA_DEV_UNKNOWN to
1748 * ATA_DEV_NONE. 2029 * ATA_DEV_NONE.
1749 */ 2030 */
1750 for (i = 0; i < ATA_MAX_DEVICES; i++) 2031 ata_link_for_each_dev(dev, link)
1751 if (classes[i] != ATA_DEV_UNKNOWN) 2032 if (classes[dev->devno] != ATA_DEV_UNKNOWN)
1752 break; 2033 break;
1753 2034
1754 if (i < ATA_MAX_DEVICES) 2035 if (dev) {
1755 for (i = 0; i < ATA_MAX_DEVICES; i++) 2036 ata_link_for_each_dev(dev, link) {
1756 if (classes[i] == ATA_DEV_UNKNOWN) 2037 if (classes[dev->devno] == ATA_DEV_UNKNOWN)
1757 classes[i] = ATA_DEV_NONE; 2038 classes[dev->devno] = ATA_DEV_NONE;
2039 }
2040 }
1758 2041
1759 return 0; 2042 return 0;
1760} 2043}
1761 2044
1762static int ata_eh_followup_srst_needed(int rc, int classify, 2045static int ata_eh_followup_srst_needed(struct ata_link *link,
2046 int rc, int classify,
1763 const unsigned int *classes) 2047 const unsigned int *classes)
1764{ 2048{
2049 if (link->flags & ATA_LFLAG_NO_SRST)
2050 return 0;
1765 if (rc == -EAGAIN) 2051 if (rc == -EAGAIN)
1766 return 1; 2052 return 1;
1767 if (rc != 0) 2053 if (rc != 0)
1768 return 0; 2054 return 0;
1769 if (classify && classes[0] == ATA_DEV_UNKNOWN) 2055 if ((link->ap->flags & ATA_FLAG_PMP) && ata_is_host_link(link))
2056 return 1;
2057 if (classify && !(link->flags & ATA_LFLAG_ASSUME_CLASS) &&
2058 classes[0] == ATA_DEV_UNKNOWN)
1770 return 1; 2059 return 1;
1771 return 0; 2060 return 0;
1772} 2061}
1773 2062
1774static int ata_eh_reset(struct ata_port *ap, int classify, 2063int ata_eh_reset(struct ata_link *link, int classify,
1775 ata_prereset_fn_t prereset, ata_reset_fn_t softreset, 2064 ata_prereset_fn_t prereset, ata_reset_fn_t softreset,
1776 ata_reset_fn_t hardreset, ata_postreset_fn_t postreset) 2065 ata_reset_fn_t hardreset, ata_postreset_fn_t postreset)
1777{ 2066{
1778 struct ata_eh_context *ehc = &ap->eh_context; 2067 struct ata_port *ap = link->ap;
2068 struct ata_eh_context *ehc = &link->eh_context;
1779 unsigned int *classes = ehc->classes; 2069 unsigned int *classes = ehc->classes;
1780 int verbose = !(ehc->i.flags & ATA_EHI_QUIET); 2070 int verbose = !(ehc->i.flags & ATA_EHI_QUIET);
1781 int try = 0; 2071 int try = 0;
2072 struct ata_device *dev;
1782 unsigned long deadline; 2073 unsigned long deadline;
1783 unsigned int action; 2074 unsigned int action;
1784 ata_reset_fn_t reset; 2075 ata_reset_fn_t reset;
1785 int i, rc; 2076 unsigned long flags;
2077 int rc;
1786 2078
1787 /* about to reset */ 2079 /* about to reset */
1788 ata_eh_about_to_do(ap, NULL, ehc->i.action & ATA_EH_RESET_MASK); 2080 spin_lock_irqsave(ap->lock, flags);
2081 ap->pflags |= ATA_PFLAG_RESETTING;
2082 spin_unlock_irqrestore(ap->lock, flags);
2083
2084 ata_eh_about_to_do(link, NULL, ehc->i.action & ATA_EH_RESET_MASK);
1789 2085
1790 /* Determine which reset to use and record in ehc->i.action. 2086 /* Determine which reset to use and record in ehc->i.action.
1791 * prereset() may examine and modify it. 2087 * prereset() may examine and modify it.
1792 */ 2088 */
1793 action = ehc->i.action; 2089 action = ehc->i.action;
1794 ehc->i.action &= ~ATA_EH_RESET_MASK; 2090 ehc->i.action &= ~ATA_EH_RESET_MASK;
1795 if (softreset && (!hardreset || (!sata_set_spd_needed(ap) && 2091 if (softreset && (!hardreset || (!(link->flags & ATA_LFLAG_NO_SRST) &&
2092 !sata_set_spd_needed(link) &&
1796 !(action & ATA_EH_HARDRESET)))) 2093 !(action & ATA_EH_HARDRESET))))
1797 ehc->i.action |= ATA_EH_SOFTRESET; 2094 ehc->i.action |= ATA_EH_SOFTRESET;
1798 else 2095 else
1799 ehc->i.action |= ATA_EH_HARDRESET; 2096 ehc->i.action |= ATA_EH_HARDRESET;
1800 2097
1801 if (prereset) { 2098 if (prereset) {
1802 rc = prereset(ap, jiffies + ATA_EH_PRERESET_TIMEOUT); 2099 rc = prereset(link, jiffies + ATA_EH_PRERESET_TIMEOUT);
1803 if (rc) { 2100 if (rc) {
1804 if (rc == -ENOENT) { 2101 if (rc == -ENOENT) {
1805 ata_port_printk(ap, KERN_DEBUG, 2102 ata_link_printk(link, KERN_DEBUG,
1806 "port disabled. ignoring.\n"); 2103 "port disabled. ignoring.\n");
1807 ap->eh_context.i.action &= ~ATA_EH_RESET_MASK; 2104 ehc->i.action &= ~ATA_EH_RESET_MASK;
1808 2105
1809 for (i = 0; i < ATA_MAX_DEVICES; i++) 2106 ata_link_for_each_dev(dev, link)
1810 classes[i] = ATA_DEV_NONE; 2107 classes[dev->devno] = ATA_DEV_NONE;
1811 2108
1812 rc = 0; 2109 rc = 0;
1813 } else 2110 } else
1814 ata_port_printk(ap, KERN_ERR, 2111 ata_link_printk(link, KERN_ERR,
1815 "prereset failed (errno=%d)\n", rc); 2112 "prereset failed (errno=%d)\n", rc);
1816 goto out; 2113 goto out;
1817 } 2114 }
@@ -1824,8 +2121,8 @@ static int ata_eh_reset(struct ata_port *ap, int classify,
1824 reset = softreset; 2121 reset = softreset;
1825 else { 2122 else {
1826 /* prereset told us not to reset, bang classes and return */ 2123 /* prereset told us not to reset, bang classes and return */
1827 for (i = 0; i < ATA_MAX_DEVICES; i++) 2124 ata_link_for_each_dev(dev, link)
1828 classes[i] = ATA_DEV_NONE; 2125 classes[dev->devno] = ATA_DEV_NONE;
1829 rc = 0; 2126 rc = 0;
1830 goto out; 2127 goto out;
1831 } 2128 }
@@ -1843,7 +2140,7 @@ static int ata_eh_reset(struct ata_port *ap, int classify,
1843 2140
1844 /* shut up during boot probing */ 2141 /* shut up during boot probing */
1845 if (verbose) 2142 if (verbose)
1846 ata_port_printk(ap, KERN_INFO, "%s resetting port\n", 2143 ata_link_printk(link, KERN_INFO, "%s resetting link\n",
1847 reset == softreset ? "soft" : "hard"); 2144 reset == softreset ? "soft" : "hard");
1848 2145
1849 /* mark that this EH session started with reset */ 2146 /* mark that this EH session started with reset */
@@ -1852,49 +2149,54 @@ static int ata_eh_reset(struct ata_port *ap, int classify,
1852 else 2149 else
1853 ehc->i.flags |= ATA_EHI_DID_SOFTRESET; 2150 ehc->i.flags |= ATA_EHI_DID_SOFTRESET;
1854 2151
1855 rc = ata_do_reset(ap, reset, classes, deadline); 2152 rc = ata_do_reset(link, reset, classes, deadline);
1856 2153
1857 if (reset == hardreset && 2154 if (reset == hardreset &&
1858 ata_eh_followup_srst_needed(rc, classify, classes)) { 2155 ata_eh_followup_srst_needed(link, rc, classify, classes)) {
1859 /* okay, let's do follow-up softreset */ 2156 /* okay, let's do follow-up softreset */
1860 reset = softreset; 2157 reset = softreset;
1861 2158
1862 if (!reset) { 2159 if (!reset) {
1863 ata_port_printk(ap, KERN_ERR, 2160 ata_link_printk(link, KERN_ERR,
1864 "follow-up softreset required " 2161 "follow-up softreset required "
1865 "but no softreset avaliable\n"); 2162 "but no softreset avaliable\n");
1866 rc = -EINVAL; 2163 rc = -EINVAL;
1867 goto out; 2164 goto out;
1868 } 2165 }
1869 2166
1870 ata_eh_about_to_do(ap, NULL, ATA_EH_RESET_MASK); 2167 ata_eh_about_to_do(link, NULL, ATA_EH_RESET_MASK);
1871 rc = ata_do_reset(ap, reset, classes, deadline); 2168 rc = ata_do_reset(link, reset, classes, deadline);
1872 2169
1873 if (rc == 0 && classify && 2170 if (rc == 0 && classify && classes[0] == ATA_DEV_UNKNOWN &&
1874 classes[0] == ATA_DEV_UNKNOWN) { 2171 !(link->flags & ATA_LFLAG_ASSUME_CLASS)) {
1875 ata_port_printk(ap, KERN_ERR, 2172 ata_link_printk(link, KERN_ERR,
1876 "classification failed\n"); 2173 "classification failed\n");
1877 rc = -EINVAL; 2174 rc = -EINVAL;
1878 goto out; 2175 goto out;
1879 } 2176 }
1880 } 2177 }
1881 2178
1882 if (rc && try < ARRAY_SIZE(ata_eh_reset_timeouts)) { 2179 /* if we skipped follow-up srst, clear rc */
2180 if (rc == -EAGAIN)
2181 rc = 0;
2182
2183 if (rc && rc != -ERESTART && try < ARRAY_SIZE(ata_eh_reset_timeouts)) {
1883 unsigned long now = jiffies; 2184 unsigned long now = jiffies;
1884 2185
1885 if (time_before(now, deadline)) { 2186 if (time_before(now, deadline)) {
1886 unsigned long delta = deadline - jiffies; 2187 unsigned long delta = deadline - jiffies;
1887 2188
1888 ata_port_printk(ap, KERN_WARNING, "reset failed " 2189 ata_link_printk(link, KERN_WARNING, "reset failed "
1889 "(errno=%d), retrying in %u secs\n", 2190 "(errno=%d), retrying in %u secs\n",
1890 rc, (jiffies_to_msecs(delta) + 999) / 1000); 2191 rc, (jiffies_to_msecs(delta) + 999) / 1000);
1891 2192
1892 schedule_timeout_uninterruptible(delta); 2193 while (delta)
2194 delta = schedule_timeout_uninterruptible(delta);
1893 } 2195 }
1894 2196
1895 if (rc == -EPIPE || 2197 if (rc == -EPIPE ||
1896 try == ARRAY_SIZE(ata_eh_reset_timeouts) - 1) 2198 try == ARRAY_SIZE(ata_eh_reset_timeouts) - 1)
1897 sata_down_spd_limit(ap); 2199 sata_down_spd_limit(link);
1898 if (hardreset) 2200 if (hardreset)
1899 reset = hardreset; 2201 reset = hardreset;
1900 goto retry; 2202 goto retry;
@@ -1903,37 +2205,56 @@ static int ata_eh_reset(struct ata_port *ap, int classify,
1903 if (rc == 0) { 2205 if (rc == 0) {
1904 u32 sstatus; 2206 u32 sstatus;
1905 2207
1906 /* After the reset, the device state is PIO 0 and the 2208 ata_link_for_each_dev(dev, link) {
1907 * controller state is undefined. Record the mode. 2209 /* After the reset, the device state is PIO 0
1908 */ 2210 * and the controller state is undefined.
1909 for (i = 0; i < ATA_MAX_DEVICES; i++) 2211 * Record the mode.
1910 ap->device[i].pio_mode = XFER_PIO_0; 2212 */
2213 dev->pio_mode = XFER_PIO_0;
2214
2215 if (ata_link_offline(link))
2216 continue;
2217
2218 /* apply class override and convert UNKNOWN to NONE */
2219 if (link->flags & ATA_LFLAG_ASSUME_ATA)
2220 classes[dev->devno] = ATA_DEV_ATA;
2221 else if (link->flags & ATA_LFLAG_ASSUME_SEMB)
2222 classes[dev->devno] = ATA_DEV_SEMB_UNSUP; /* not yet */
2223 else if (classes[dev->devno] == ATA_DEV_UNKNOWN)
2224 classes[dev->devno] = ATA_DEV_NONE;
2225 }
1911 2226
1912 /* record current link speed */ 2227 /* record current link speed */
1913 if (sata_scr_read(ap, SCR_STATUS, &sstatus) == 0) 2228 if (sata_scr_read(link, SCR_STATUS, &sstatus) == 0)
1914 ap->sata_spd = (sstatus >> 4) & 0xf; 2229 link->sata_spd = (sstatus >> 4) & 0xf;
1915 2230
1916 if (postreset) 2231 if (postreset)
1917 postreset(ap, classes); 2232 postreset(link, classes);
1918 2233
1919 /* reset successful, schedule revalidation */ 2234 /* reset successful, schedule revalidation */
1920 ata_eh_done(ap, NULL, ehc->i.action & ATA_EH_RESET_MASK); 2235 ata_eh_done(link, NULL, ehc->i.action & ATA_EH_RESET_MASK);
1921 ehc->i.action |= ATA_EH_REVALIDATE; 2236 ehc->i.action |= ATA_EH_REVALIDATE;
1922 } 2237 }
1923 out: 2238 out:
1924 /* clear hotplug flag */ 2239 /* clear hotplug flag */
1925 ehc->i.flags &= ~ATA_EHI_HOTPLUGGED; 2240 ehc->i.flags &= ~ATA_EHI_HOTPLUGGED;
2241
2242 spin_lock_irqsave(ap->lock, flags);
2243 ap->pflags &= ~ATA_PFLAG_RESETTING;
2244 spin_unlock_irqrestore(ap->lock, flags);
2245
1926 return rc; 2246 return rc;
1927} 2247}
1928 2248
1929static int ata_eh_revalidate_and_attach(struct ata_port *ap, 2249static int ata_eh_revalidate_and_attach(struct ata_link *link,
1930 struct ata_device **r_failed_dev) 2250 struct ata_device **r_failed_dev)
1931{ 2251{
1932 struct ata_eh_context *ehc = &ap->eh_context; 2252 struct ata_port *ap = link->ap;
2253 struct ata_eh_context *ehc = &link->eh_context;
1933 struct ata_device *dev; 2254 struct ata_device *dev;
1934 unsigned int new_mask = 0; 2255 unsigned int new_mask = 0;
1935 unsigned long flags; 2256 unsigned long flags;
1936 int i, rc = 0; 2257 int rc = 0;
1937 2258
1938 DPRINTK("ENTER\n"); 2259 DPRINTK("ENTER\n");
1939 2260
@@ -1941,27 +2262,28 @@ static int ata_eh_revalidate_and_attach(struct ata_port *ap,
1941 * be done backwards such that PDIAG- is released by the slave 2262 * be done backwards such that PDIAG- is released by the slave
1942 * device before the master device is identified. 2263 * device before the master device is identified.
1943 */ 2264 */
1944 for (i = ATA_MAX_DEVICES - 1; i >= 0; i--) { 2265 ata_link_for_each_dev_reverse(dev, link) {
1945 unsigned int action, readid_flags = 0; 2266 unsigned int action = ata_eh_dev_action(dev);
1946 2267 unsigned int readid_flags = 0;
1947 dev = &ap->device[i];
1948 action = ata_eh_dev_action(dev);
1949 2268
1950 if (ehc->i.flags & ATA_EHI_DID_RESET) 2269 if (ehc->i.flags & ATA_EHI_DID_RESET)
1951 readid_flags |= ATA_READID_POSTRESET; 2270 readid_flags |= ATA_READID_POSTRESET;
1952 2271
1953 if ((action & ATA_EH_REVALIDATE) && ata_dev_enabled(dev)) { 2272 if ((action & ATA_EH_REVALIDATE) && ata_dev_enabled(dev)) {
1954 if (ata_port_offline(ap)) { 2273 WARN_ON(dev->class == ATA_DEV_PMP);
2274
2275 if (ata_link_offline(link)) {
1955 rc = -EIO; 2276 rc = -EIO;
1956 goto err; 2277 goto err;
1957 } 2278 }
1958 2279
1959 ata_eh_about_to_do(ap, dev, ATA_EH_REVALIDATE); 2280 ata_eh_about_to_do(link, dev, ATA_EH_REVALIDATE);
1960 rc = ata_dev_revalidate(dev, readid_flags); 2281 rc = ata_dev_revalidate(dev, ehc->classes[dev->devno],
2282 readid_flags);
1961 if (rc) 2283 if (rc)
1962 goto err; 2284 goto err;
1963 2285
1964 ata_eh_done(ap, dev, ATA_EH_REVALIDATE); 2286 ata_eh_done(link, dev, ATA_EH_REVALIDATE);
1965 2287
1966 /* Configuration may have changed, reconfigure 2288 /* Configuration may have changed, reconfigure
1967 * transfer mode. 2289 * transfer mode.
@@ -1975,11 +2297,14 @@ static int ata_eh_revalidate_and_attach(struct ata_port *ap,
1975 ata_class_enabled(ehc->classes[dev->devno])) { 2297 ata_class_enabled(ehc->classes[dev->devno])) {
1976 dev->class = ehc->classes[dev->devno]; 2298 dev->class = ehc->classes[dev->devno];
1977 2299
1978 rc = ata_dev_read_id(dev, &dev->class, readid_flags, 2300 if (dev->class == ATA_DEV_PMP)
1979 dev->id); 2301 rc = sata_pmp_attach(dev);
2302 else
2303 rc = ata_dev_read_id(dev, &dev->class,
2304 readid_flags, dev->id);
1980 switch (rc) { 2305 switch (rc) {
1981 case 0: 2306 case 0:
1982 new_mask |= 1 << i; 2307 new_mask |= 1 << dev->devno;
1983 break; 2308 break;
1984 case -ENOENT: 2309 case -ENOENT:
1985 /* IDENTIFY was issued to non-existent 2310 /* IDENTIFY was issued to non-existent
@@ -1997,16 +2322,16 @@ static int ata_eh_revalidate_and_attach(struct ata_port *ap,
1997 } 2322 }
1998 2323
1999 /* PDIAG- should have been released, ask cable type if post-reset */ 2324 /* PDIAG- should have been released, ask cable type if post-reset */
2000 if ((ehc->i.flags & ATA_EHI_DID_RESET) && ap->ops->cable_detect) 2325 if (ata_is_host_link(link) && ap->ops->cable_detect &&
2326 (ehc->i.flags & ATA_EHI_DID_RESET))
2001 ap->cbl = ap->ops->cable_detect(ap); 2327 ap->cbl = ap->ops->cable_detect(ap);
2002 2328
2003 /* Configure new devices forward such that user doesn't see 2329 /* Configure new devices forward such that user doesn't see
2004 * device detection messages backwards. 2330 * device detection messages backwards.
2005 */ 2331 */
2006 for (i = 0; i < ATA_MAX_DEVICES; i++) { 2332 ata_link_for_each_dev(dev, link) {
2007 dev = &ap->device[i]; 2333 if (!(new_mask & (1 << dev->devno)) ||
2008 2334 dev->class == ATA_DEV_PMP)
2009 if (!(new_mask & (1 << i)))
2010 continue; 2335 continue;
2011 2336
2012 ehc->i.flags |= ATA_EHI_PRINTINFO; 2337 ehc->i.flags |= ATA_EHI_PRINTINFO;
@@ -2031,40 +2356,44 @@ static int ata_eh_revalidate_and_attach(struct ata_port *ap,
2031 return rc; 2356 return rc;
2032} 2357}
2033 2358
2034static int ata_port_nr_enabled(struct ata_port *ap) 2359static int ata_link_nr_enabled(struct ata_link *link)
2035{ 2360{
2036 int i, cnt = 0; 2361 struct ata_device *dev;
2362 int cnt = 0;
2037 2363
2038 for (i = 0; i < ATA_MAX_DEVICES; i++) 2364 ata_link_for_each_dev(dev, link)
2039 if (ata_dev_enabled(&ap->device[i])) 2365 if (ata_dev_enabled(dev))
2040 cnt++; 2366 cnt++;
2041 return cnt; 2367 return cnt;
2042} 2368}
2043 2369
2044static int ata_port_nr_vacant(struct ata_port *ap) 2370static int ata_link_nr_vacant(struct ata_link *link)
2045{ 2371{
2046 int i, cnt = 0; 2372 struct ata_device *dev;
2373 int cnt = 0;
2047 2374
2048 for (i = 0; i < ATA_MAX_DEVICES; i++) 2375 ata_link_for_each_dev(dev, link)
2049 if (ap->device[i].class == ATA_DEV_UNKNOWN) 2376 if (dev->class == ATA_DEV_UNKNOWN)
2050 cnt++; 2377 cnt++;
2051 return cnt; 2378 return cnt;
2052} 2379}
2053 2380
2054static int ata_eh_skip_recovery(struct ata_port *ap) 2381static int ata_eh_skip_recovery(struct ata_link *link)
2055{ 2382{
2056 struct ata_eh_context *ehc = &ap->eh_context; 2383 struct ata_eh_context *ehc = &link->eh_context;
2057 int i; 2384 struct ata_device *dev;
2385
2386 /* skip disabled links */
2387 if (link->flags & ATA_LFLAG_DISABLED)
2388 return 1;
2058 2389
2059 /* thaw frozen port, resume link and recover failed devices */ 2390 /* thaw frozen port, resume link and recover failed devices */
2060 if ((ap->pflags & ATA_PFLAG_FROZEN) || 2391 if ((link->ap->pflags & ATA_PFLAG_FROZEN) ||
2061 (ehc->i.flags & ATA_EHI_RESUME_LINK) || ata_port_nr_enabled(ap)) 2392 (ehc->i.flags & ATA_EHI_RESUME_LINK) || ata_link_nr_enabled(link))
2062 return 0; 2393 return 0;
2063 2394
2064 /* skip if class codes for all vacant slots are ATA_DEV_NONE */ 2395 /* skip if class codes for all vacant slots are ATA_DEV_NONE */
2065 for (i = 0; i < ATA_MAX_DEVICES; i++) { 2396 ata_link_for_each_dev(dev, link) {
2066 struct ata_device *dev = &ap->device[i];
2067
2068 if (dev->class == ATA_DEV_UNKNOWN && 2397 if (dev->class == ATA_DEV_UNKNOWN &&
2069 ehc->classes[dev->devno] != ATA_DEV_NONE) 2398 ehc->classes[dev->devno] != ATA_DEV_NONE)
2070 return 0; 2399 return 0;
@@ -2073,10 +2402,9 @@ static int ata_eh_skip_recovery(struct ata_port *ap)
2073 return 1; 2402 return 1;
2074} 2403}
2075 2404
2076static void ata_eh_handle_dev_fail(struct ata_device *dev, int err) 2405static int ata_eh_handle_dev_fail(struct ata_device *dev, int err)
2077{ 2406{
2078 struct ata_port *ap = dev->ap; 2407 struct ata_eh_context *ehc = &dev->link->eh_context;
2079 struct ata_eh_context *ehc = &ap->eh_context;
2080 2408
2081 ehc->tries[dev->devno]--; 2409 ehc->tries[dev->devno]--;
2082 2410
@@ -2092,7 +2420,7 @@ static void ata_eh_handle_dev_fail(struct ata_device *dev, int err)
2092 /* This is the last chance, better to slow 2420 /* This is the last chance, better to slow
2093 * down than lose it. 2421 * down than lose it.
2094 */ 2422 */
2095 sata_down_spd_limit(ap); 2423 sata_down_spd_limit(dev->link);
2096 ata_down_xfermask_limit(dev, ATA_DNXFER_PIO); 2424 ata_down_xfermask_limit(dev, ATA_DNXFER_PIO);
2097 } 2425 }
2098 } 2426 }
@@ -2102,7 +2430,7 @@ static void ata_eh_handle_dev_fail(struct ata_device *dev, int err)
2102 ata_dev_disable(dev); 2430 ata_dev_disable(dev);
2103 2431
2104 /* detach if offline */ 2432 /* detach if offline */
2105 if (ata_port_offline(ap)) 2433 if (ata_link_offline(dev->link))
2106 ata_eh_detach_dev(dev); 2434 ata_eh_detach_dev(dev);
2107 2435
2108 /* probe if requested */ 2436 /* probe if requested */
@@ -2115,12 +2443,16 @@ static void ata_eh_handle_dev_fail(struct ata_device *dev, int err)
2115 ehc->did_probe_mask |= (1 << dev->devno); 2443 ehc->did_probe_mask |= (1 << dev->devno);
2116 ehc->i.action |= ATA_EH_SOFTRESET; 2444 ehc->i.action |= ATA_EH_SOFTRESET;
2117 } 2445 }
2446
2447 return 1;
2118 } else { 2448 } else {
2119 /* soft didn't work? be haaaaard */ 2449 /* soft didn't work? be haaaaard */
2120 if (ehc->i.flags & ATA_EHI_DID_RESET) 2450 if (ehc->i.flags & ATA_EHI_DID_RESET)
2121 ehc->i.action |= ATA_EH_HARDRESET; 2451 ehc->i.action |= ATA_EH_HARDRESET;
2122 else 2452 else
2123 ehc->i.action |= ATA_EH_SOFTRESET; 2453 ehc->i.action |= ATA_EH_SOFTRESET;
2454
2455 return 0;
2124 } 2456 }
2125} 2457}
2126 2458
@@ -2131,12 +2463,13 @@ static void ata_eh_handle_dev_fail(struct ata_device *dev, int err)
2131 * @softreset: softreset method (can be NULL) 2463 * @softreset: softreset method (can be NULL)
2132 * @hardreset: hardreset method (can be NULL) 2464 * @hardreset: hardreset method (can be NULL)
2133 * @postreset: postreset method (can be NULL) 2465 * @postreset: postreset method (can be NULL)
2466 * @r_failed_link: out parameter for failed link
2134 * 2467 *
2135 * This is the alpha and omega, eum and yang, heart and soul of 2468 * This is the alpha and omega, eum and yang, heart and soul of
2136 * libata exception handling. On entry, actions required to 2469 * libata exception handling. On entry, actions required to
2137 * recover the port and hotplug requests are recorded in 2470 * recover each link and hotplug requests are recorded in the
2138 * eh_context. This function executes all the operations with 2471 * link's eh_context. This function executes all the operations
2139 * appropriate retrials and fallbacks to resurrect failed 2472 * with appropriate retrials and fallbacks to resurrect failed
2140 * devices, detach goners and greet newcomers. 2473 * devices, detach goners and greet newcomers.
2141 * 2474 *
2142 * LOCKING: 2475 * LOCKING:
@@ -2145,104 +2478,171 @@ static void ata_eh_handle_dev_fail(struct ata_device *dev, int err)
2145 * RETURNS: 2478 * RETURNS:
2146 * 0 on success, -errno on failure. 2479 * 0 on success, -errno on failure.
2147 */ 2480 */
2148static int ata_eh_recover(struct ata_port *ap, ata_prereset_fn_t prereset, 2481int ata_eh_recover(struct ata_port *ap, ata_prereset_fn_t prereset,
2149 ata_reset_fn_t softreset, ata_reset_fn_t hardreset, 2482 ata_reset_fn_t softreset, ata_reset_fn_t hardreset,
2150 ata_postreset_fn_t postreset) 2483 ata_postreset_fn_t postreset,
2484 struct ata_link **r_failed_link)
2151{ 2485{
2152 struct ata_eh_context *ehc = &ap->eh_context; 2486 struct ata_link *link;
2153 struct ata_device *dev; 2487 struct ata_device *dev;
2154 int i, rc; 2488 int nr_failed_devs, nr_disabled_devs;
2489 int reset, rc;
2490 unsigned long flags;
2155 2491
2156 DPRINTK("ENTER\n"); 2492 DPRINTK("ENTER\n");
2157 2493
2158 /* prep for recovery */ 2494 /* prep for recovery */
2159 for (i = 0; i < ATA_MAX_DEVICES; i++) { 2495 ata_port_for_each_link(link, ap) {
2160 dev = &ap->device[i]; 2496 struct ata_eh_context *ehc = &link->eh_context;
2161 2497
2162 ehc->tries[dev->devno] = ATA_EH_DEV_TRIES; 2498 /* re-enable link? */
2163 2499 if (ehc->i.action & ATA_EH_ENABLE_LINK) {
2164 /* collect port action mask recorded in dev actions */ 2500 ata_eh_about_to_do(link, NULL, ATA_EH_ENABLE_LINK);
2165 ehc->i.action |= ehc->i.dev_action[i] & ~ATA_EH_PERDEV_MASK; 2501 spin_lock_irqsave(ap->lock, flags);
2166 ehc->i.dev_action[i] &= ATA_EH_PERDEV_MASK; 2502 link->flags &= ~ATA_LFLAG_DISABLED;
2167 2503 spin_unlock_irqrestore(ap->lock, flags);
2168 /* process hotplug request */ 2504 ata_eh_done(link, NULL, ATA_EH_ENABLE_LINK);
2169 if (dev->flags & ATA_DFLAG_DETACH) 2505 }
2170 ata_eh_detach_dev(dev);
2171 2506
2172 if (!ata_dev_enabled(dev) && 2507 ata_link_for_each_dev(dev, link) {
2173 ((ehc->i.probe_mask & (1 << dev->devno)) && 2508 if (link->flags & ATA_LFLAG_NO_RETRY)
2174 !(ehc->did_probe_mask & (1 << dev->devno)))) { 2509 ehc->tries[dev->devno] = 1;
2175 ata_eh_detach_dev(dev); 2510 else
2176 ata_dev_init(dev); 2511 ehc->tries[dev->devno] = ATA_EH_DEV_TRIES;
2177 ehc->did_probe_mask |= (1 << dev->devno); 2512
2178 ehc->i.action |= ATA_EH_SOFTRESET; 2513 /* collect port action mask recorded in dev actions */
2514 ehc->i.action |= ehc->i.dev_action[dev->devno] &
2515 ~ATA_EH_PERDEV_MASK;
2516 ehc->i.dev_action[dev->devno] &= ATA_EH_PERDEV_MASK;
2517
2518 /* process hotplug request */
2519 if (dev->flags & ATA_DFLAG_DETACH)
2520 ata_eh_detach_dev(dev);
2521
2522 if (!ata_dev_enabled(dev) &&
2523 ((ehc->i.probe_mask & (1 << dev->devno)) &&
2524 !(ehc->did_probe_mask & (1 << dev->devno)))) {
2525 ata_eh_detach_dev(dev);
2526 ata_dev_init(dev);
2527 ehc->did_probe_mask |= (1 << dev->devno);
2528 ehc->i.action |= ATA_EH_SOFTRESET;
2529 }
2179 } 2530 }
2180 } 2531 }
2181 2532
2182 retry: 2533 retry:
2183 rc = 0; 2534 rc = 0;
2535 nr_failed_devs = 0;
2536 nr_disabled_devs = 0;
2537 reset = 0;
2184 2538
2185 /* if UNLOADING, finish immediately */ 2539 /* if UNLOADING, finish immediately */
2186 if (ap->pflags & ATA_PFLAG_UNLOADING) 2540 if (ap->pflags & ATA_PFLAG_UNLOADING)
2187 goto out; 2541 goto out;
2188 2542
2189 /* skip EH if possible. */ 2543 /* prep for EH */
2190 if (ata_eh_skip_recovery(ap)) 2544 ata_port_for_each_link(link, ap) {
2191 ehc->i.action = 0; 2545 struct ata_eh_context *ehc = &link->eh_context;
2192 2546
2193 for (i = 0; i < ATA_MAX_DEVICES; i++) 2547 /* skip EH if possible. */
2194 ehc->classes[i] = ATA_DEV_UNKNOWN; 2548 if (ata_eh_skip_recovery(link))
2549 ehc->i.action = 0;
2195 2550
2196 /* reset */ 2551 /* do we need to reset? */
2197 if (ehc->i.action & ATA_EH_RESET_MASK) { 2552 if (ehc->i.action & ATA_EH_RESET_MASK)
2198 ata_eh_freeze_port(ap); 2553 reset = 1;
2199 2554
2200 rc = ata_eh_reset(ap, ata_port_nr_vacant(ap), prereset, 2555 ata_link_for_each_dev(dev, link)
2201 softreset, hardreset, postreset); 2556 ehc->classes[dev->devno] = ATA_DEV_UNKNOWN;
2202 if (rc) { 2557 }
2203 ata_port_printk(ap, KERN_ERR, 2558
2204 "reset failed, giving up\n"); 2559 /* reset */
2205 goto out; 2560 if (reset) {
2561 /* if PMP is attached, this function only deals with
2562 * downstream links, port should stay thawed.
2563 */
2564 if (!ap->nr_pmp_links)
2565 ata_eh_freeze_port(ap);
2566
2567 ata_port_for_each_link(link, ap) {
2568 struct ata_eh_context *ehc = &link->eh_context;
2569
2570 if (!(ehc->i.action & ATA_EH_RESET_MASK))
2571 continue;
2572
2573 rc = ata_eh_reset(link, ata_link_nr_vacant(link),
2574 prereset, softreset, hardreset,
2575 postreset);
2576 if (rc) {
2577 ata_link_printk(link, KERN_ERR,
2578 "reset failed, giving up\n");
2579 goto out;
2580 }
2206 } 2581 }
2207 2582
2208 ata_eh_thaw_port(ap); 2583 if (!ap->nr_pmp_links)
2584 ata_eh_thaw_port(ap);
2209 } 2585 }
2210 2586
2211 /* revalidate existing devices and attach new ones */ 2587 /* the rest */
2212 rc = ata_eh_revalidate_and_attach(ap, &dev); 2588 ata_port_for_each_link(link, ap) {
2213 if (rc) 2589 struct ata_eh_context *ehc = &link->eh_context;
2214 goto dev_fail;
2215 2590
2216 /* configure transfer mode if necessary */ 2591 /* revalidate existing devices and attach new ones */
2217 if (ehc->i.flags & ATA_EHI_SETMODE) { 2592 rc = ata_eh_revalidate_and_attach(link, &dev);
2218 rc = ata_set_mode(ap, &dev);
2219 if (rc) 2593 if (rc)
2220 goto dev_fail; 2594 goto dev_fail;
2221 ehc->i.flags &= ~ATA_EHI_SETMODE;
2222 }
2223 2595
2224 goto out; 2596 /* if PMP got attached, return, pmp EH will take care of it */
2597 if (link->device->class == ATA_DEV_PMP) {
2598 ehc->i.action = 0;
2599 return 0;
2600 }
2225 2601
2226 dev_fail: 2602 /* configure transfer mode if necessary */
2227 ata_eh_handle_dev_fail(dev, rc); 2603 if (ehc->i.flags & ATA_EHI_SETMODE) {
2604 rc = ata_set_mode(link, &dev);
2605 if (rc)
2606 goto dev_fail;
2607 ehc->i.flags &= ~ATA_EHI_SETMODE;
2608 }
2228 2609
2229 if (ata_port_nr_enabled(ap)) { 2610 /* this link is okay now */
2230 ata_port_printk(ap, KERN_WARNING, "failed to recover some " 2611 ehc->i.flags = 0;
2231 "devices, retrying in 5 secs\n"); 2612 continue;
2232 ssleep(5); 2613
2233 } else { 2614 dev_fail:
2234 /* no device left, repeat fast */ 2615 nr_failed_devs++;
2235 msleep(500); 2616 if (ata_eh_handle_dev_fail(dev, rc))
2617 nr_disabled_devs++;
2618
2619 if (ap->pflags & ATA_PFLAG_FROZEN) {
2620 /* PMP reset requires working host port.
2621 * Can't retry if it's frozen.
2622 */
2623 if (ap->nr_pmp_links)
2624 goto out;
2625 break;
2626 }
2236 } 2627 }
2237 2628
2238 goto retry; 2629 if (nr_failed_devs) {
2630 if (nr_failed_devs != nr_disabled_devs) {
2631 ata_port_printk(ap, KERN_WARNING, "failed to recover "
2632 "some devices, retrying in 5 secs\n");
2633 ssleep(5);
2634 } else {
2635 /* no device left to recover, repeat fast */
2636 msleep(500);
2637 }
2239 2638
2240 out: 2639 goto retry;
2241 if (rc) {
2242 for (i = 0; i < ATA_MAX_DEVICES; i++)
2243 ata_dev_disable(&ap->device[i]);
2244 } 2640 }
2245 2641
2642 out:
2643 if (rc && r_failed_link)
2644 *r_failed_link = link;
2645
2246 DPRINTK("EXIT, rc=%d\n", rc); 2646 DPRINTK("EXIT, rc=%d\n", rc);
2247 return rc; 2647 return rc;
2248} 2648}
@@ -2257,7 +2657,7 @@ static int ata_eh_recover(struct ata_port *ap, ata_prereset_fn_t prereset,
2257 * LOCKING: 2657 * LOCKING:
2258 * None. 2658 * None.
2259 */ 2659 */
2260static void ata_eh_finish(struct ata_port *ap) 2660void ata_eh_finish(struct ata_port *ap)
2261{ 2661{
2262 int tag; 2662 int tag;
2263 2663
@@ -2287,6 +2687,10 @@ static void ata_eh_finish(struct ata_port *ap)
2287 } 2687 }
2288 } 2688 }
2289 } 2689 }
2690
2691 /* make sure nr_active_links is zero after EH */
2692 WARN_ON(ap->nr_active_links);
2693 ap->nr_active_links = 0;
2290} 2694}
2291 2695
2292/** 2696/**
@@ -2306,9 +2710,19 @@ void ata_do_eh(struct ata_port *ap, ata_prereset_fn_t prereset,
2306 ata_reset_fn_t softreset, ata_reset_fn_t hardreset, 2710 ata_reset_fn_t softreset, ata_reset_fn_t hardreset,
2307 ata_postreset_fn_t postreset) 2711 ata_postreset_fn_t postreset)
2308{ 2712{
2713 struct ata_device *dev;
2714 int rc;
2715
2309 ata_eh_autopsy(ap); 2716 ata_eh_autopsy(ap);
2310 ata_eh_report(ap); 2717 ata_eh_report(ap);
2311 ata_eh_recover(ap, prereset, softreset, hardreset, postreset); 2718
2719 rc = ata_eh_recover(ap, prereset, softreset, hardreset, postreset,
2720 NULL);
2721 if (rc) {
2722 ata_link_for_each_dev(dev, &ap->link)
2723 ata_dev_disable(dev);
2724 }
2725
2312 ata_eh_finish(ap); 2726 ata_eh_finish(ap);
2313} 2727}
2314 2728