2 #include <linux/kernel.h>
4 #include <linux/delay.h>
6 static ide_startstop_t ide_ata_error(ide_drive_t *drive, struct request *rq,
9 ide_hwif_t *hwif = drive->hwif;
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)
22 } else if ((err & BAD_CRC) == BAD_CRC) {
23 /* UDMA crc error, just retry the operation */
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;
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;
38 ide_pad_transfer(drive, READ, nsect * SECTOR_SIZE);
41 if (rq->errors >= ERROR_MAX || blk_noretry_request(rq)) {
42 ide_kill_rq(drive, rq);
46 if (hwif->tp_ops->read_status(hwif) & (ATA_BUSY | ATA_DRQ))
47 rq->errors |= ERROR_RESET;
49 if ((rq->errors & ERROR_RESET) == ERROR_RESET) {
51 return ide_do_reset(drive);
54 if ((rq->errors & ERROR_RECAL) == ERROR_RECAL)
55 drive->special.b.recalibrate = 1;
62 static ide_startstop_t ide_atapi_error(ide_drive_t *drive, struct request *rq,
65 ide_hwif_t *hwif = drive->hwif;
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;
72 /* add decoding error stuff */
75 if (hwif->tp_ops->read_status(hwif) & (ATA_BUSY | ATA_DRQ))
77 hwif->tp_ops->exec_command(hwif, ATA_CMD_IDLEIMMEDIATE);
79 if (rq->errors >= ERROR_MAX) {
80 ide_kill_rq(drive, rq);
82 if ((rq->errors & ERROR_RESET) == ERROR_RESET) {
84 return ide_do_reset(drive);
92 static ide_startstop_t __ide_error(ide_drive_t *drive, struct request *rq,
95 if (drive->media == ide_disk)
96 return ide_ata_error(drive, rq, stat, err);
97 return ide_atapi_error(drive, rq, stat, err);
101 * ide_error - handle an error on the IDE
102 * @drive: drive the error occurred on
103 * @msg: message to report
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
113 ide_startstop_t ide_error(ide_drive_t *drive, const char *msg, u8 stat)
118 err = ide_dump_status(drive, msg, stat);
120 rq = drive->hwif->rq;
124 /* retry only "normal" I/O: */
125 if (!blk_fs_request(rq)) {
127 ide_end_drive_cmd(drive, stat, err);
131 return __ide_error(drive, rq, stat, err);
133 EXPORT_SYMBOL_GPL(ide_error);
135 static inline void ide_complete_drive_reset(ide_drive_t *drive, int err)
137 struct request *rq = drive->hwif->rq;
139 if (rq && blk_special_request(rq) && rq->cmd[0] == REQ_DRIVE_RESET)
140 ide_end_request(drive, err ? err : 1, 0);
144 static ide_startstop_t do_reset1(ide_drive_t *, int);
147 * atapi_reset_pollfunc() gets invoked to poll the interface for completion
148 * every 50ms during an atapi drive reset operation. If the drive has not yet
149 * responded, and we have not yet hit our maximum waiting time, then the timer
150 * is restarted for another 50ms.
152 static ide_startstop_t atapi_reset_pollfunc(ide_drive_t *drive)
154 ide_hwif_t *hwif = drive->hwif;
159 stat = hwif->tp_ops->read_status(hwif);
161 if (OK_STAT(stat, 0, ATA_BUSY))
162 printk(KERN_INFO "%s: ATAPI reset complete\n", drive->name);
164 if (time_before(jiffies, hwif->poll_timeout)) {
165 ide_set_handler(drive, &atapi_reset_pollfunc, HZ/20,
167 /* continue polling */
172 printk(KERN_ERR "%s: ATAPI reset timed-out, status=0x%02x\n",
174 /* do it the old fashioned way */
175 return do_reset1(drive, 1);
179 ide_complete_drive_reset(drive, 0);
183 static void ide_reset_report_error(ide_hwif_t *hwif, u8 err)
185 static const char *err_master_vals[] =
186 { NULL, "passed", "formatter device error",
187 "sector buffer error", "ECC circuitry error",
188 "controlling MPU error" };
190 u8 err_master = err & 0x7f;
192 printk(KERN_ERR "%s: reset: master: ", hwif->name);
193 if (err_master && err_master < 6)
194 printk(KERN_CONT "%s", err_master_vals[err_master]);
196 printk(KERN_CONT "error (0x%02x?)", err);
198 printk(KERN_CONT "; slave: failed");
199 printk(KERN_CONT "\n");
203 * reset_pollfunc() gets invoked to poll the interface for completion every 50ms
204 * during an ide reset operation. If the drives have not yet responded,
205 * and we have not yet hit our maximum waiting time, then the timer is restarted
208 static ide_startstop_t reset_pollfunc(ide_drive_t *drive)
210 ide_hwif_t *hwif = drive->hwif;
211 const struct ide_port_ops *port_ops = hwif->port_ops;
215 if (port_ops && port_ops->reset_poll) {
216 err = port_ops->reset_poll(drive);
218 printk(KERN_ERR "%s: host reset_poll failure for %s.\n",
219 hwif->name, drive->name);
224 tmp = hwif->tp_ops->read_status(hwif);
226 if (!OK_STAT(tmp, 0, ATA_BUSY)) {
227 if (time_before(jiffies, hwif->poll_timeout)) {
228 ide_set_handler(drive, &reset_pollfunc, HZ/20, NULL);
229 /* continue polling */
232 printk(KERN_ERR "%s: reset timed-out, status=0x%02x\n",
237 tmp = ide_read_error(drive);
240 printk(KERN_INFO "%s: reset: success\n", hwif->name);
243 ide_reset_report_error(hwif, tmp);
249 hwif->polling = 0; /* done polling */
250 ide_complete_drive_reset(drive, err);
254 static void ide_disk_pre_reset(ide_drive_t *drive)
256 int legacy = (drive->id[ATA_ID_CFS_ENABLE_2] & 0x0400) ? 0 : 1;
258 drive->special.all = 0;
259 drive->special.b.set_geometry = legacy;
260 drive->special.b.recalibrate = legacy;
262 drive->mult_count = 0;
263 drive->dev_flags &= ~IDE_DFLAG_PARKED;
265 if ((drive->dev_flags & IDE_DFLAG_KEEP_SETTINGS) == 0 &&
266 (drive->dev_flags & IDE_DFLAG_USING_DMA) == 0)
269 if (drive->mult_req != drive->mult_count)
270 drive->special.b.set_multmode = 1;
273 static void pre_reset(ide_drive_t *drive)
275 const struct ide_port_ops *port_ops = drive->hwif->port_ops;
277 if (drive->media == ide_disk)
278 ide_disk_pre_reset(drive);
280 drive->dev_flags |= IDE_DFLAG_POST_RESET;
282 if (drive->dev_flags & IDE_DFLAG_USING_DMA) {
283 if (drive->crc_count)
284 ide_check_dma_crc(drive);
289 if ((drive->dev_flags & IDE_DFLAG_KEEP_SETTINGS) == 0) {
290 if ((drive->dev_flags & IDE_DFLAG_USING_DMA) == 0) {
291 drive->dev_flags &= ~IDE_DFLAG_UNMASK;
297 if (port_ops && port_ops->pre_reset)
298 port_ops->pre_reset(drive);
300 if (drive->current_speed != 0xff)
301 drive->desired_speed = drive->current_speed;
302 drive->current_speed = 0xff;
306 * do_reset1() attempts to recover a confused drive by resetting it.
307 * Unfortunately, resetting a disk drive actually resets all devices on
308 * the same interface, so it can really be thought of as resetting the
309 * interface rather than resetting the drive.
311 * ATAPI devices have their own reset mechanism which allows them to be
312 * individually reset without clobbering other devices on the same interface.
314 * Unfortunately, the IDE interface does not generate an interrupt to let
315 * us know when the reset operation has finished, so we must poll for this.
316 * Equally poor, though, is the fact that this may a very long time to complete,
317 * (up to 30 seconds worstcase). So, instead of busy-waiting here for it,
318 * we set a timer to poll at 50ms intervals.
320 static ide_startstop_t do_reset1(ide_drive_t *drive, int do_not_try_atapi)
322 ide_hwif_t *hwif = drive->hwif;
323 struct ide_io_ports *io_ports = &hwif->io_ports;
324 const struct ide_tp_ops *tp_ops = hwif->tp_ops;
325 const struct ide_port_ops *port_ops;
327 unsigned long flags, timeout;
331 spin_lock_irqsave(&hwif->lock, flags);
333 /* We must not reset with running handlers */
334 BUG_ON(hwif->handler != NULL);
336 /* For an ATAPI device, first try an ATAPI SRST. */
337 if (drive->media != ide_disk && !do_not_try_atapi) {
341 tp_ops->exec_command(hwif, ATA_CMD_DEV_RESET);
343 hwif->poll_timeout = jiffies + WAIT_WORSTCASE;
345 __ide_set_handler(drive, &atapi_reset_pollfunc, HZ/20, NULL);
346 spin_unlock_irqrestore(&hwif->lock, flags);
350 /* We must not disturb devices in the IDE_DFLAG_PARKED state. */
354 prepare_to_wait(&ide_park_wq, &wait, TASK_UNINTERRUPTIBLE);
356 ide_port_for_each_present_dev(i, tdrive, hwif) {
357 if ((tdrive->dev_flags & IDE_DFLAG_PARKED) &&
358 time_after(tdrive->sleep, timeout))
359 timeout = tdrive->sleep;
363 if (time_before_eq(timeout, now))
366 spin_unlock_irqrestore(&hwif->lock, flags);
367 timeout = schedule_timeout_uninterruptible(timeout - now);
368 spin_lock_irqsave(&hwif->lock, flags);
370 finish_wait(&ide_park_wq, &wait);
373 * First, reset any device state data we were maintaining
374 * for any of the drives on this interface.
376 ide_port_for_each_dev(i, tdrive, hwif)
379 if (io_ports->ctl_addr == 0) {
380 spin_unlock_irqrestore(&hwif->lock, flags);
381 ide_complete_drive_reset(drive, -ENXIO);
386 * Note that we also set nIEN while resetting the device,
387 * to mask unwanted interrupts from the interface during the reset.
388 * However, due to the design of PC hardware, this will cause an
389 * immediate interrupt due to the edge transition it produces.
390 * This single interrupt gives us a "fast poll" for drives that
391 * recover from reset very quickly, saving us the first 50ms wait time.
393 * TODO: add ->softreset method and stop abusing ->set_irq
395 /* set SRST and nIEN */
396 tp_ops->set_irq(hwif, 4);
397 /* more than enough time */
399 /* clear SRST, leave nIEN (unless device is on the quirk list) */
400 tp_ops->set_irq(hwif, drive->quirk_list == 2);
401 /* more than enough time */
403 hwif->poll_timeout = jiffies + WAIT_WORSTCASE;
405 __ide_set_handler(drive, &reset_pollfunc, HZ/20, NULL);
408 * Some weird controller like resetting themselves to a strange
409 * state when the disks are reset this way. At least, the Winbond
410 * 553 documentation says that
412 port_ops = hwif->port_ops;
413 if (port_ops && port_ops->resetproc)
414 port_ops->resetproc(drive);
416 spin_unlock_irqrestore(&hwif->lock, flags);
421 * ide_do_reset() is the entry point to the drive/interface reset code.
424 ide_startstop_t ide_do_reset(ide_drive_t *drive)
426 return do_reset1(drive, 0);
428 EXPORT_SYMBOL(ide_do_reset);