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