ide: pass error value to ide_complete_rq()
[linux-2.6-block.git] / drivers / ide / ide-eh.c
1
2 #include <linux/kernel.h>
3 #include <linux/ide.h>
4 #include <linux/delay.h>
5
6 static ide_startstop_t ide_ata_error(ide_drive_t *drive, struct request *rq,
7                                      u8 stat, u8 err)
8 {
9         ide_hwif_t *hwif = drive->hwif;
10
11         if ((stat & ATA_BUSY) ||
12             ((stat & ATA_DF) && (drive->dev_flags & IDE_DFLAG_NOWERR) == 0)) {
13                 /* other bits are useless when BUSY */
14                 rq->errors |= ERROR_RESET;
15         } else if (stat & ATA_ERR) {
16                 /* err has different meaning on cdrom and tape */
17                 if (err == ATA_ABORTED) {
18                         if ((drive->dev_flags & IDE_DFLAG_LBA) &&
19                             /* some newer drives don't support ATA_CMD_INIT_DEV_PARAMS */
20                             hwif->tp_ops->read_status(hwif) == ATA_CMD_INIT_DEV_PARAMS)
21                                 return ide_stopped;
22                 } else if ((err & BAD_CRC) == BAD_CRC) {
23                         /* UDMA crc error, just retry the operation */
24                         drive->crc_count++;
25                 } else if (err & (ATA_BBK | ATA_UNC)) {
26                         /* retries won't help these */
27                         rq->errors = ERROR_MAX;
28                 } else if (err & ATA_TRK0NF) {
29                         /* help it find track zero */
30                         rq->errors |= ERROR_RECAL;
31                 }
32         }
33
34         if ((stat & ATA_DRQ) && rq_data_dir(rq) == READ &&
35             (hwif->host_flags & IDE_HFLAG_ERROR_STOPS_FIFO) == 0) {
36                 int nsect = drive->mult_count ? drive->mult_count : 1;
37
38                 ide_pad_transfer(drive, READ, nsect * SECTOR_SIZE);
39         }
40
41         if (rq->errors >= ERROR_MAX || blk_noretry_request(rq)) {
42                 ide_kill_rq(drive, rq);
43                 return ide_stopped;
44         }
45
46         if (hwif->tp_ops->read_status(hwif) & (ATA_BUSY | ATA_DRQ))
47                 rq->errors |= ERROR_RESET;
48
49         if ((rq->errors & ERROR_RESET) == ERROR_RESET) {
50                 ++rq->errors;
51                 return ide_do_reset(drive);
52         }
53
54         if ((rq->errors & ERROR_RECAL) == ERROR_RECAL)
55                 drive->special.b.recalibrate = 1;
56
57         ++rq->errors;
58
59         return ide_stopped;
60 }
61
62 static ide_startstop_t ide_atapi_error(ide_drive_t *drive, struct request *rq,
63                                        u8 stat, u8 err)
64 {
65         ide_hwif_t *hwif = drive->hwif;
66
67         if ((stat & ATA_BUSY) ||
68             ((stat & ATA_DF) && (drive->dev_flags & IDE_DFLAG_NOWERR) == 0)) {
69                 /* other bits are useless when BUSY */
70                 rq->errors |= ERROR_RESET;
71         } else {
72                 /* add decoding error stuff */
73         }
74
75         if (hwif->tp_ops->read_status(hwif) & (ATA_BUSY | ATA_DRQ))
76                 /* force an abort */
77                 hwif->tp_ops->exec_command(hwif, ATA_CMD_IDLEIMMEDIATE);
78
79         if (rq->errors >= ERROR_MAX) {
80                 ide_kill_rq(drive, rq);
81         } else {
82                 if ((rq->errors & ERROR_RESET) == ERROR_RESET) {
83                         ++rq->errors;
84                         return ide_do_reset(drive);
85                 }
86                 ++rq->errors;
87         }
88
89         return ide_stopped;
90 }
91
92 static ide_startstop_t __ide_error(ide_drive_t *drive, struct request *rq,
93                                    u8 stat, u8 err)
94 {
95         if (drive->media == ide_disk)
96                 return ide_ata_error(drive, rq, stat, err);
97         return ide_atapi_error(drive, rq, stat, err);
98 }
99
100 /**
101  *      ide_error       -       handle an error on the IDE
102  *      @drive: drive the error occurred on
103  *      @msg: message to report
104  *      @stat: status bits
105  *
106  *      ide_error() takes action based on the error returned by the drive.
107  *      For normal I/O that may well include retries. We deal with
108  *      both new-style (taskfile) and old style command handling here.
109  *      In the case of taskfile command handling there is work left to
110  *      do
111  */
112
113 ide_startstop_t ide_error(ide_drive_t *drive, const char *msg, u8 stat)
114 {
115         struct request *rq;
116         u8 err;
117
118         err = ide_dump_status(drive, msg, stat);
119
120         rq = drive->hwif->rq;
121         if (rq == NULL)
122                 return ide_stopped;
123
124         /* retry only "normal" I/O: */
125         if (!blk_fs_request(rq)) {
126                 if (rq->cmd_type == REQ_TYPE_ATA_TASKFILE) {
127                         struct ide_cmd *cmd = rq->special;
128
129                         if (cmd)
130                                 ide_complete_cmd(drive, cmd, stat, err);
131                 } else if (blk_pm_request(rq)) {
132                         rq->errors = 1;
133                         ide_complete_pm_rq(drive, rq);
134                         return ide_stopped;
135                 }
136                 rq->errors = err;
137                 ide_complete_rq(drive, err ? -EIO : 0);
138                 return ide_stopped;
139         }
140
141         return __ide_error(drive, rq, stat, err);
142 }
143 EXPORT_SYMBOL_GPL(ide_error);
144
145 static inline void ide_complete_drive_reset(ide_drive_t *drive, int err)
146 {
147         struct request *rq = drive->hwif->rq;
148
149         if (rq && blk_special_request(rq) && rq->cmd[0] == REQ_DRIVE_RESET)
150                 ide_end_request(drive, err ? err : 1, 0);
151 }
152
153 /* needed below */
154 static ide_startstop_t do_reset1(ide_drive_t *, int);
155
156 /*
157  * atapi_reset_pollfunc() gets invoked to poll the interface for completion
158  * every 50ms during an atapi drive reset operation.  If the drive has not yet
159  * responded, and we have not yet hit our maximum waiting time, then the timer
160  * is restarted for another 50ms.
161  */
162 static ide_startstop_t atapi_reset_pollfunc(ide_drive_t *drive)
163 {
164         ide_hwif_t *hwif = drive->hwif;
165         u8 stat;
166
167         SELECT_DRIVE(drive);
168         udelay(10);
169         stat = hwif->tp_ops->read_status(hwif);
170
171         if (OK_STAT(stat, 0, ATA_BUSY))
172                 printk(KERN_INFO "%s: ATAPI reset complete\n", drive->name);
173         else {
174                 if (time_before(jiffies, hwif->poll_timeout)) {
175                         ide_set_handler(drive, &atapi_reset_pollfunc, HZ/20,
176                                         NULL);
177                         /* continue polling */
178                         return ide_started;
179                 }
180                 /* end of polling */
181                 hwif->polling = 0;
182                 printk(KERN_ERR "%s: ATAPI reset timed-out, status=0x%02x\n",
183                         drive->name, stat);
184                 /* do it the old fashioned way */
185                 return do_reset1(drive, 1);
186         }
187         /* done polling */
188         hwif->polling = 0;
189         ide_complete_drive_reset(drive, 0);
190         return ide_stopped;
191 }
192
193 static void ide_reset_report_error(ide_hwif_t *hwif, u8 err)
194 {
195         static const char *err_master_vals[] =
196                 { NULL, "passed", "formatter device error",
197                   "sector buffer error", "ECC circuitry error",
198                   "controlling MPU error" };
199
200         u8 err_master = err & 0x7f;
201
202         printk(KERN_ERR "%s: reset: master: ", hwif->name);
203         if (err_master && err_master < 6)
204                 printk(KERN_CONT "%s", err_master_vals[err_master]);
205         else
206                 printk(KERN_CONT "error (0x%02x?)", err);
207         if (err & 0x80)
208                 printk(KERN_CONT "; slave: failed");
209         printk(KERN_CONT "\n");
210 }
211
212 /*
213  * reset_pollfunc() gets invoked to poll the interface for completion every 50ms
214  * during an ide reset operation. If the drives have not yet responded,
215  * and we have not yet hit our maximum waiting time, then the timer is restarted
216  * for another 50ms.
217  */
218 static ide_startstop_t reset_pollfunc(ide_drive_t *drive)
219 {
220         ide_hwif_t *hwif = drive->hwif;
221         const struct ide_port_ops *port_ops = hwif->port_ops;
222         u8 tmp;
223         int err = 0;
224
225         if (port_ops && port_ops->reset_poll) {
226                 err = port_ops->reset_poll(drive);
227                 if (err) {
228                         printk(KERN_ERR "%s: host reset_poll failure for %s.\n",
229                                 hwif->name, drive->name);
230                         goto out;
231                 }
232         }
233
234         tmp = hwif->tp_ops->read_status(hwif);
235
236         if (!OK_STAT(tmp, 0, ATA_BUSY)) {
237                 if (time_before(jiffies, hwif->poll_timeout)) {
238                         ide_set_handler(drive, &reset_pollfunc, HZ/20, NULL);
239                         /* continue polling */
240                         return ide_started;
241                 }
242                 printk(KERN_ERR "%s: reset timed-out, status=0x%02x\n",
243                         hwif->name, tmp);
244                 drive->failures++;
245                 err = -EIO;
246         } else  {
247                 tmp = ide_read_error(drive);
248
249                 if (tmp == 1) {
250                         printk(KERN_INFO "%s: reset: success\n", hwif->name);
251                         drive->failures = 0;
252                 } else {
253                         ide_reset_report_error(hwif, tmp);
254                         drive->failures++;
255                         err = -EIO;
256                 }
257         }
258 out:
259         hwif->polling = 0;      /* done polling */
260         ide_complete_drive_reset(drive, err);
261         return ide_stopped;
262 }
263
264 static void ide_disk_pre_reset(ide_drive_t *drive)
265 {
266         int legacy = (drive->id[ATA_ID_CFS_ENABLE_2] & 0x0400) ? 0 : 1;
267
268         drive->special.all = 0;
269         drive->special.b.set_geometry = legacy;
270         drive->special.b.recalibrate  = legacy;
271
272         drive->mult_count = 0;
273         drive->dev_flags &= ~IDE_DFLAG_PARKED;
274
275         if ((drive->dev_flags & IDE_DFLAG_KEEP_SETTINGS) == 0 &&
276             (drive->dev_flags & IDE_DFLAG_USING_DMA) == 0)
277                 drive->mult_req = 0;
278
279         if (drive->mult_req != drive->mult_count)
280                 drive->special.b.set_multmode = 1;
281 }
282
283 static void pre_reset(ide_drive_t *drive)
284 {
285         const struct ide_port_ops *port_ops = drive->hwif->port_ops;
286
287         if (drive->media == ide_disk)
288                 ide_disk_pre_reset(drive);
289         else
290                 drive->dev_flags |= IDE_DFLAG_POST_RESET;
291
292         if (drive->dev_flags & IDE_DFLAG_USING_DMA) {
293                 if (drive->crc_count)
294                         ide_check_dma_crc(drive);
295                 else
296                         ide_dma_off(drive);
297         }
298
299         if ((drive->dev_flags & IDE_DFLAG_KEEP_SETTINGS) == 0) {
300                 if ((drive->dev_flags & IDE_DFLAG_USING_DMA) == 0) {
301                         drive->dev_flags &= ~IDE_DFLAG_UNMASK;
302                         drive->io_32bit = 0;
303                 }
304                 return;
305         }
306
307         if (port_ops && port_ops->pre_reset)
308                 port_ops->pre_reset(drive);
309
310         if (drive->current_speed != 0xff)
311                 drive->desired_speed = drive->current_speed;
312         drive->current_speed = 0xff;
313 }
314
315 /*
316  * do_reset1() attempts to recover a confused drive by resetting it.
317  * Unfortunately, resetting a disk drive actually resets all devices on
318  * the same interface, so it can really be thought of as resetting the
319  * interface rather than resetting the drive.
320  *
321  * ATAPI devices have their own reset mechanism which allows them to be
322  * individually reset without clobbering other devices on the same interface.
323  *
324  * Unfortunately, the IDE interface does not generate an interrupt to let
325  * us know when the reset operation has finished, so we must poll for this.
326  * Equally poor, though, is the fact that this may a very long time to complete,
327  * (up to 30 seconds worstcase).  So, instead of busy-waiting here for it,
328  * we set a timer to poll at 50ms intervals.
329  */
330 static ide_startstop_t do_reset1(ide_drive_t *drive, int do_not_try_atapi)
331 {
332         ide_hwif_t *hwif = drive->hwif;
333         struct ide_io_ports *io_ports = &hwif->io_ports;
334         const struct ide_tp_ops *tp_ops = hwif->tp_ops;
335         const struct ide_port_ops *port_ops;
336         ide_drive_t *tdrive;
337         unsigned long flags, timeout;
338         int i;
339         DEFINE_WAIT(wait);
340
341         spin_lock_irqsave(&hwif->lock, flags);
342
343         /* We must not reset with running handlers */
344         BUG_ON(hwif->handler != NULL);
345
346         /* For an ATAPI device, first try an ATAPI SRST. */
347         if (drive->media != ide_disk && !do_not_try_atapi) {
348                 pre_reset(drive);
349                 SELECT_DRIVE(drive);
350                 udelay(20);
351                 tp_ops->exec_command(hwif, ATA_CMD_DEV_RESET);
352                 ndelay(400);
353                 hwif->poll_timeout = jiffies + WAIT_WORSTCASE;
354                 hwif->polling = 1;
355                 __ide_set_handler(drive, &atapi_reset_pollfunc, HZ/20, NULL);
356                 spin_unlock_irqrestore(&hwif->lock, flags);
357                 return ide_started;
358         }
359
360         /* We must not disturb devices in the IDE_DFLAG_PARKED state. */
361         do {
362                 unsigned long now;
363
364                 prepare_to_wait(&ide_park_wq, &wait, TASK_UNINTERRUPTIBLE);
365                 timeout = jiffies;
366                 ide_port_for_each_present_dev(i, tdrive, hwif) {
367                         if ((tdrive->dev_flags & IDE_DFLAG_PARKED) &&
368                             time_after(tdrive->sleep, timeout))
369                                 timeout = tdrive->sleep;
370                 }
371
372                 now = jiffies;
373                 if (time_before_eq(timeout, now))
374                         break;
375
376                 spin_unlock_irqrestore(&hwif->lock, flags);
377                 timeout = schedule_timeout_uninterruptible(timeout - now);
378                 spin_lock_irqsave(&hwif->lock, flags);
379         } while (timeout);
380         finish_wait(&ide_park_wq, &wait);
381
382         /*
383          * First, reset any device state data we were maintaining
384          * for any of the drives on this interface.
385          */
386         ide_port_for_each_dev(i, tdrive, hwif)
387                 pre_reset(tdrive);
388
389         if (io_ports->ctl_addr == 0) {
390                 spin_unlock_irqrestore(&hwif->lock, flags);
391                 ide_complete_drive_reset(drive, -ENXIO);
392                 return ide_stopped;
393         }
394
395         /*
396          * Note that we also set nIEN while resetting the device,
397          * to mask unwanted interrupts from the interface during the reset.
398          * However, due to the design of PC hardware, this will cause an
399          * immediate interrupt due to the edge transition it produces.
400          * This single interrupt gives us a "fast poll" for drives that
401          * recover from reset very quickly, saving us the first 50ms wait time.
402          *
403          * TODO: add ->softreset method and stop abusing ->set_irq
404          */
405         /* set SRST and nIEN */
406         tp_ops->set_irq(hwif, 4);
407         /* more than enough time */
408         udelay(10);
409         /* clear SRST, leave nIEN (unless device is on the quirk list) */
410         tp_ops->set_irq(hwif, drive->quirk_list == 2);
411         /* more than enough time */
412         udelay(10);
413         hwif->poll_timeout = jiffies + WAIT_WORSTCASE;
414         hwif->polling = 1;
415         __ide_set_handler(drive, &reset_pollfunc, HZ/20, NULL);
416
417         /*
418          * Some weird controller like resetting themselves to a strange
419          * state when the disks are reset this way. At least, the Winbond
420          * 553 documentation says that
421          */
422         port_ops = hwif->port_ops;
423         if (port_ops && port_ops->resetproc)
424                 port_ops->resetproc(drive);
425
426         spin_unlock_irqrestore(&hwif->lock, flags);
427         return ide_started;
428 }
429
430 /*
431  * ide_do_reset() is the entry point to the drive/interface reset code.
432  */
433
434 ide_startstop_t ide_do_reset(ide_drive_t *drive)
435 {
436         return do_reset1(drive, 0);
437 }
438 EXPORT_SYMBOL(ide_do_reset);