blob: e9abf2c3c33544c047ec2db7f29f1f295da0a397 [file] [log] [blame]
Bartlomiej Zolnierkiewicz327fa1c2009-03-24 23:22:47 +01001
2#include <linux/kernel.h>
3#include <linux/ide.h>
4#include <linux/delay.h>
5
6static 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)
Bartlomiej Zolnierkiewiczca1b96e2009-05-17 19:12:21 +020055 drive->special_flags |= IDE_SFLAG_RECALIBRATE;
Bartlomiej Zolnierkiewicz327fa1c2009-03-24 23:22:47 +010056
57 ++rq->errors;
58
59 return ide_stopped;
60}
61
62static 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
92static 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
113ide_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)) {
Bartlomiej Zolnierkiewicza09485d2009-03-27 12:46:31 +0100126 if (rq->cmd_type == REQ_TYPE_ATA_TASKFILE) {
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100127 struct ide_cmd *cmd = rq->special;
Bartlomiej Zolnierkiewicza09485d2009-03-27 12:46:31 +0100128
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100129 if (cmd)
130 ide_complete_cmd(drive, cmd, stat, err);
Bartlomiej Zolnierkiewicza09485d2009-03-27 12:46:31 +0100131 } else if (blk_pm_request(rq)) {
Bartlomiej Zolnierkiewicz6902a532009-03-27 12:46:43 +0100132 rq->errors = 1;
Bartlomiej Zolnierkiewicza09485d2009-03-27 12:46:31 +0100133 ide_complete_pm_rq(drive, rq);
134 return ide_stopped;
135 }
Bartlomiej Zolnierkiewicz6902a532009-03-27 12:46:43 +0100136 rq->errors = err;
Bartlomiej Zolnierkiewiczf974b192009-03-27 12:46:44 +0100137 ide_complete_rq(drive, err ? -EIO : 0, blk_rq_bytes(rq));
Bartlomiej Zolnierkiewicz327fa1c2009-03-24 23:22:47 +0100138 return ide_stopped;
139 }
140
141 return __ide_error(drive, rq, stat, err);
142}
143EXPORT_SYMBOL_GPL(ide_error);
144
145static inline void ide_complete_drive_reset(ide_drive_t *drive, int err)
146{
147 struct request *rq = drive->hwif->rq;
148
Bartlomiej Zolnierkiewicz89f78b322009-03-27 12:46:43 +0100149 if (rq && blk_special_request(rq) && rq->cmd[0] == REQ_DRIVE_RESET) {
150 if (err <= 0 && rq->errors == 0)
151 rq->errors = -EIO;
Bartlomiej Zolnierkiewicz5e955242009-06-23 11:27:27 +0000152 ide_complete_rq(drive, err ? err : 0, blk_rq_bytes(rq));
Bartlomiej Zolnierkiewicz89f78b322009-03-27 12:46:43 +0100153 }
Bartlomiej Zolnierkiewicz327fa1c2009-03-24 23:22:47 +0100154}
155
156/* needed below */
157static ide_startstop_t do_reset1(ide_drive_t *, int);
158
159/*
160 * atapi_reset_pollfunc() gets invoked to poll the interface for completion
161 * every 50ms during an atapi drive reset operation. If the drive has not yet
162 * responded, and we have not yet hit our maximum waiting time, then the timer
163 * is restarted for another 50ms.
164 */
165static ide_startstop_t atapi_reset_pollfunc(ide_drive_t *drive)
166{
167 ide_hwif_t *hwif = drive->hwif;
Sergei Shtylyovfdd88f02009-03-31 20:15:33 +0200168 const struct ide_tp_ops *tp_ops = hwif->tp_ops;
Bartlomiej Zolnierkiewicz327fa1c2009-03-24 23:22:47 +0100169 u8 stat;
170
Sergei Shtylyovfdd88f02009-03-31 20:15:33 +0200171 tp_ops->dev_select(drive);
Bartlomiej Zolnierkiewicz327fa1c2009-03-24 23:22:47 +0100172 udelay(10);
Sergei Shtylyovfdd88f02009-03-31 20:15:33 +0200173 stat = tp_ops->read_status(hwif);
Bartlomiej Zolnierkiewicz327fa1c2009-03-24 23:22:47 +0100174
175 if (OK_STAT(stat, 0, ATA_BUSY))
176 printk(KERN_INFO "%s: ATAPI reset complete\n", drive->name);
177 else {
178 if (time_before(jiffies, hwif->poll_timeout)) {
Bartlomiej Zolnierkiewicz60c0cd02009-03-27 12:46:46 +0100179 ide_set_handler(drive, &atapi_reset_pollfunc, HZ/20);
Bartlomiej Zolnierkiewicz327fa1c2009-03-24 23:22:47 +0100180 /* continue polling */
181 return ide_started;
182 }
183 /* end of polling */
184 hwif->polling = 0;
185 printk(KERN_ERR "%s: ATAPI reset timed-out, status=0x%02x\n",
186 drive->name, stat);
187 /* do it the old fashioned way */
188 return do_reset1(drive, 1);
189 }
190 /* done polling */
191 hwif->polling = 0;
192 ide_complete_drive_reset(drive, 0);
193 return ide_stopped;
194}
195
196static void ide_reset_report_error(ide_hwif_t *hwif, u8 err)
197{
198 static const char *err_master_vals[] =
199 { NULL, "passed", "formatter device error",
200 "sector buffer error", "ECC circuitry error",
201 "controlling MPU error" };
202
203 u8 err_master = err & 0x7f;
204
205 printk(KERN_ERR "%s: reset: master: ", hwif->name);
206 if (err_master && err_master < 6)
207 printk(KERN_CONT "%s", err_master_vals[err_master]);
208 else
209 printk(KERN_CONT "error (0x%02x?)", err);
210 if (err & 0x80)
211 printk(KERN_CONT "; slave: failed");
212 printk(KERN_CONT "\n");
213}
214
215/*
216 * reset_pollfunc() gets invoked to poll the interface for completion every 50ms
217 * during an ide reset operation. If the drives have not yet responded,
218 * and we have not yet hit our maximum waiting time, then the timer is restarted
219 * for another 50ms.
220 */
221static ide_startstop_t reset_pollfunc(ide_drive_t *drive)
222{
223 ide_hwif_t *hwif = drive->hwif;
224 const struct ide_port_ops *port_ops = hwif->port_ops;
225 u8 tmp;
226 int err = 0;
227
228 if (port_ops && port_ops->reset_poll) {
229 err = port_ops->reset_poll(drive);
230 if (err) {
231 printk(KERN_ERR "%s: host reset_poll failure for %s.\n",
232 hwif->name, drive->name);
233 goto out;
234 }
235 }
236
237 tmp = hwif->tp_ops->read_status(hwif);
238
239 if (!OK_STAT(tmp, 0, ATA_BUSY)) {
240 if (time_before(jiffies, hwif->poll_timeout)) {
Bartlomiej Zolnierkiewicz60c0cd02009-03-27 12:46:46 +0100241 ide_set_handler(drive, &reset_pollfunc, HZ/20);
Bartlomiej Zolnierkiewicz327fa1c2009-03-24 23:22:47 +0100242 /* continue polling */
243 return ide_started;
244 }
245 printk(KERN_ERR "%s: reset timed-out, status=0x%02x\n",
246 hwif->name, tmp);
247 drive->failures++;
248 err = -EIO;
249 } else {
250 tmp = ide_read_error(drive);
251
252 if (tmp == 1) {
253 printk(KERN_INFO "%s: reset: success\n", hwif->name);
254 drive->failures = 0;
255 } else {
256 ide_reset_report_error(hwif, tmp);
257 drive->failures++;
258 err = -EIO;
259 }
260 }
261out:
262 hwif->polling = 0; /* done polling */
263 ide_complete_drive_reset(drive, err);
264 return ide_stopped;
265}
266
267static void ide_disk_pre_reset(ide_drive_t *drive)
268{
269 int legacy = (drive->id[ATA_ID_CFS_ENABLE_2] & 0x0400) ? 0 : 1;
270
Bartlomiej Zolnierkiewiczca1b96e2009-05-17 19:12:21 +0200271 drive->special_flags =
272 legacy ? (IDE_SFLAG_SET_GEOMETRY | IDE_SFLAG_RECALIBRATE) : 0;
Bartlomiej Zolnierkiewicz327fa1c2009-03-24 23:22:47 +0100273
274 drive->mult_count = 0;
275 drive->dev_flags &= ~IDE_DFLAG_PARKED;
276
277 if ((drive->dev_flags & IDE_DFLAG_KEEP_SETTINGS) == 0 &&
278 (drive->dev_flags & IDE_DFLAG_USING_DMA) == 0)
279 drive->mult_req = 0;
280
281 if (drive->mult_req != drive->mult_count)
Bartlomiej Zolnierkiewiczca1b96e2009-05-17 19:12:21 +0200282 drive->special_flags |= IDE_SFLAG_SET_MULTMODE;
Bartlomiej Zolnierkiewicz327fa1c2009-03-24 23:22:47 +0100283}
284
285static void pre_reset(ide_drive_t *drive)
286{
287 const struct ide_port_ops *port_ops = drive->hwif->port_ops;
288
289 if (drive->media == ide_disk)
290 ide_disk_pre_reset(drive);
291 else
292 drive->dev_flags |= IDE_DFLAG_POST_RESET;
293
294 if (drive->dev_flags & IDE_DFLAG_USING_DMA) {
295 if (drive->crc_count)
296 ide_check_dma_crc(drive);
297 else
298 ide_dma_off(drive);
299 }
300
301 if ((drive->dev_flags & IDE_DFLAG_KEEP_SETTINGS) == 0) {
302 if ((drive->dev_flags & IDE_DFLAG_USING_DMA) == 0) {
303 drive->dev_flags &= ~IDE_DFLAG_UNMASK;
304 drive->io_32bit = 0;
305 }
306 return;
307 }
308
309 if (port_ops && port_ops->pre_reset)
310 port_ops->pre_reset(drive);
311
312 if (drive->current_speed != 0xff)
313 drive->desired_speed = drive->current_speed;
314 drive->current_speed = 0xff;
315}
316
317/*
318 * do_reset1() attempts to recover a confused drive by resetting it.
319 * Unfortunately, resetting a disk drive actually resets all devices on
320 * the same interface, so it can really be thought of as resetting the
321 * interface rather than resetting the drive.
322 *
323 * ATAPI devices have their own reset mechanism which allows them to be
324 * individually reset without clobbering other devices on the same interface.
325 *
326 * Unfortunately, the IDE interface does not generate an interrupt to let
327 * us know when the reset operation has finished, so we must poll for this.
328 * Equally poor, though, is the fact that this may a very long time to complete,
329 * (up to 30 seconds worstcase). So, instead of busy-waiting here for it,
330 * we set a timer to poll at 50ms intervals.
331 */
332static ide_startstop_t do_reset1(ide_drive_t *drive, int do_not_try_atapi)
333{
334 ide_hwif_t *hwif = drive->hwif;
335 struct ide_io_ports *io_ports = &hwif->io_ports;
336 const struct ide_tp_ops *tp_ops = hwif->tp_ops;
337 const struct ide_port_ops *port_ops;
338 ide_drive_t *tdrive;
339 unsigned long flags, timeout;
340 int i;
341 DEFINE_WAIT(wait);
342
343 spin_lock_irqsave(&hwif->lock, flags);
344
345 /* We must not reset with running handlers */
346 BUG_ON(hwif->handler != NULL);
347
348 /* For an ATAPI device, first try an ATAPI SRST. */
349 if (drive->media != ide_disk && !do_not_try_atapi) {
350 pre_reset(drive);
Sergei Shtylyovfdd88f02009-03-31 20:15:33 +0200351 tp_ops->dev_select(drive);
Bartlomiej Zolnierkiewicz327fa1c2009-03-24 23:22:47 +0100352 udelay(20);
353 tp_ops->exec_command(hwif, ATA_CMD_DEV_RESET);
354 ndelay(400);
355 hwif->poll_timeout = jiffies + WAIT_WORSTCASE;
356 hwif->polling = 1;
Bartlomiej Zolnierkiewicz60c0cd02009-03-27 12:46:46 +0100357 __ide_set_handler(drive, &atapi_reset_pollfunc, HZ/20);
Bartlomiej Zolnierkiewicz327fa1c2009-03-24 23:22:47 +0100358 spin_unlock_irqrestore(&hwif->lock, flags);
359 return ide_started;
360 }
361
362 /* We must not disturb devices in the IDE_DFLAG_PARKED state. */
363 do {
364 unsigned long now;
365
366 prepare_to_wait(&ide_park_wq, &wait, TASK_UNINTERRUPTIBLE);
367 timeout = jiffies;
368 ide_port_for_each_present_dev(i, tdrive, hwif) {
369 if ((tdrive->dev_flags & IDE_DFLAG_PARKED) &&
370 time_after(tdrive->sleep, timeout))
371 timeout = tdrive->sleep;
372 }
373
374 now = jiffies;
375 if (time_before_eq(timeout, now))
376 break;
377
378 spin_unlock_irqrestore(&hwif->lock, flags);
379 timeout = schedule_timeout_uninterruptible(timeout - now);
380 spin_lock_irqsave(&hwif->lock, flags);
381 } while (timeout);
382 finish_wait(&ide_park_wq, &wait);
383
384 /*
385 * First, reset any device state data we were maintaining
386 * for any of the drives on this interface.
387 */
388 ide_port_for_each_dev(i, tdrive, hwif)
389 pre_reset(tdrive);
390
391 if (io_ports->ctl_addr == 0) {
392 spin_unlock_irqrestore(&hwif->lock, flags);
393 ide_complete_drive_reset(drive, -ENXIO);
394 return ide_stopped;
395 }
396
397 /*
398 * Note that we also set nIEN while resetting the device,
399 * to mask unwanted interrupts from the interface during the reset.
400 * However, due to the design of PC hardware, this will cause an
401 * immediate interrupt due to the edge transition it produces.
402 * This single interrupt gives us a "fast poll" for drives that
403 * recover from reset very quickly, saving us the first 50ms wait time.
Bartlomiej Zolnierkiewicz327fa1c2009-03-24 23:22:47 +0100404 */
405 /* set SRST and nIEN */
Sergei Shtylyovecf3a312009-03-31 20:15:30 +0200406 tp_ops->write_devctl(hwif, ATA_SRST | ATA_NIEN | ATA_DEVCTL_OBS);
Bartlomiej Zolnierkiewicz327fa1c2009-03-24 23:22:47 +0100407 /* more than enough time */
408 udelay(10);
409 /* clear SRST, leave nIEN (unless device is on the quirk list) */
Bartlomiej Zolnierkiewicz734affd2009-06-07 15:37:10 +0200410 tp_ops->write_devctl(hwif,
411 ((drive->dev_flags & IDE_DFLAG_NIEN_QUIRK) ? 0 : ATA_NIEN) |
412 ATA_DEVCTL_OBS);
Bartlomiej Zolnierkiewicz327fa1c2009-03-24 23:22:47 +0100413 /* more than enough time */
414 udelay(10);
415 hwif->poll_timeout = jiffies + WAIT_WORSTCASE;
416 hwif->polling = 1;
Bartlomiej Zolnierkiewicz60c0cd02009-03-27 12:46:46 +0100417 __ide_set_handler(drive, &reset_pollfunc, HZ/20);
Bartlomiej Zolnierkiewicz327fa1c2009-03-24 23:22:47 +0100418
419 /*
420 * Some weird controller like resetting themselves to a strange
421 * state when the disks are reset this way. At least, the Winbond
422 * 553 documentation says that
423 */
424 port_ops = hwif->port_ops;
425 if (port_ops && port_ops->resetproc)
426 port_ops->resetproc(drive);
427
428 spin_unlock_irqrestore(&hwif->lock, flags);
429 return ide_started;
430}
431
432/*
433 * ide_do_reset() is the entry point to the drive/interface reset code.
434 */
435
436ide_startstop_t ide_do_reset(ide_drive_t *drive)
437{
438 return do_reset1(drive, 0);
439}
440EXPORT_SYMBOL(ide_do_reset);