aboutsummaryrefslogtreecommitdiffstats
path: root/drivers
diff options
context:
space:
mode:
authorBartlomiej Zolnierkiewicz <bzolnier@gmail.com>2009-03-24 18:22:47 -0400
committerBartlomiej Zolnierkiewicz <bzolnier@gmail.com>2009-03-24 18:22:47 -0400
commit327fa1c29466b8fe471a91fc11e9c6171163c81a (patch)
tree9dbbf65c886d5ec68299d563da0cd7df8b97c324 /drivers
parent122f06f8bce406169d61242a3eb667027e27cca7 (diff)
ide: move error handling code to ide-eh.c (v2)
Do some CodingStyle fixups in <linux/ide.h> while at it. v2: Add missing <linux/delay.h> include (reported by Stephen Rothwell). Signed-off-by: Bartlomiej Zolnierkiewicz <bzolnier@gmail.com>
Diffstat (limited to 'drivers')
-rw-r--r--drivers/ide/Makefile2
-rw-r--r--drivers/ide/ide-eh.c428
-rw-r--r--drivers/ide/ide-io.c129
-rw-r--r--drivers/ide/ide-iops.c299
4 files changed, 432 insertions, 426 deletions
diff --git a/drivers/ide/Makefile b/drivers/ide/Makefile
index cbb1aea2aea3..9b4bbe1cdc1a 100644
--- a/drivers/ide/Makefile
+++ b/drivers/ide/Makefile
@@ -6,7 +6,7 @@ EXTRA_CFLAGS += -Idrivers/ide
6 6
7ide-core-y += ide.o ide-ioctls.o ide-io.o ide-iops.o ide-lib.o ide-probe.o \ 7ide-core-y += ide.o ide-ioctls.o ide-io.o ide-iops.o ide-lib.o ide-probe.o \
8 ide-taskfile.o ide-pm.o ide-park.o ide-sysfs.o ide-devsets.o \ 8 ide-taskfile.o ide-pm.o ide-park.o ide-sysfs.o ide-devsets.o \
9 ide-io-std.o 9 ide-io-std.o ide-eh.o
10 10
11# core IDE code 11# core IDE code
12ide-core-$(CONFIG_IDE_XFER_MODE) += ide-pio-blacklist.o ide-xfer-mode.o 12ide-core-$(CONFIG_IDE_XFER_MODE) += ide-pio-blacklist.o ide-xfer-mode.o
diff --git a/drivers/ide/ide-eh.c b/drivers/ide/ide-eh.c
new file mode 100644
index 000000000000..1231b5e486f2
--- /dev/null
+++ b/drivers/ide/ide-eh.c
@@ -0,0 +1,428 @@
1
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;
127 ide_end_drive_cmd(drive, stat, err);
128 return ide_stopped;
129 }
130
131 return __ide_error(drive, rq, stat, err);
132}
133EXPORT_SYMBOL_GPL(ide_error);
134
135static inline void ide_complete_drive_reset(ide_drive_t *drive, int err)
136{
137 struct request *rq = drive->hwif->rq;
138
139 if (rq && blk_special_request(rq) && rq->cmd[0] == REQ_DRIVE_RESET)
140 ide_end_request(drive, err ? err : 1, 0);
141}
142
143/* needed below */
144static ide_startstop_t do_reset1(ide_drive_t *, int);
145
146/*
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.
151 */
152static ide_startstop_t atapi_reset_pollfunc(ide_drive_t *drive)
153{
154 ide_hwif_t *hwif = drive->hwif;
155 u8 stat;
156
157 SELECT_DRIVE(drive);
158 udelay(10);
159 stat = hwif->tp_ops->read_status(hwif);
160
161 if (OK_STAT(stat, 0, ATA_BUSY))
162 printk(KERN_INFO "%s: ATAPI reset complete\n", drive->name);
163 else {
164 if (time_before(jiffies, hwif->poll_timeout)) {
165 ide_set_handler(drive, &atapi_reset_pollfunc, HZ/20,
166 NULL);
167 /* continue polling */
168 return ide_started;
169 }
170 /* end of polling */
171 hwif->polling = 0;
172 printk(KERN_ERR "%s: ATAPI reset timed-out, status=0x%02x\n",
173 drive->name, stat);
174 /* do it the old fashioned way */
175 return do_reset1(drive, 1);
176 }
177 /* done polling */
178 hwif->polling = 0;
179 ide_complete_drive_reset(drive, 0);
180 return ide_stopped;
181}
182
183static void ide_reset_report_error(ide_hwif_t *hwif, u8 err)
184{
185 static const char *err_master_vals[] =
186 { NULL, "passed", "formatter device error",
187 "sector buffer error", "ECC circuitry error",
188 "controlling MPU error" };
189
190 u8 err_master = err & 0x7f;
191
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]);
195 else
196 printk(KERN_CONT "error (0x%02x?)", err);
197 if (err & 0x80)
198 printk(KERN_CONT "; slave: failed");
199 printk(KERN_CONT "\n");
200}
201
202/*
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
206 * for another 50ms.
207 */
208static ide_startstop_t reset_pollfunc(ide_drive_t *drive)
209{
210 ide_hwif_t *hwif = drive->hwif;
211 const struct ide_port_ops *port_ops = hwif->port_ops;
212 u8 tmp;
213 int err = 0;
214
215 if (port_ops && port_ops->reset_poll) {
216 err = port_ops->reset_poll(drive);
217 if (err) {
218 printk(KERN_ERR "%s: host reset_poll failure for %s.\n",
219 hwif->name, drive->name);
220 goto out;
221 }
222 }
223
224 tmp = hwif->tp_ops->read_status(hwif);
225
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 */
230 return ide_started;
231 }
232 printk(KERN_ERR "%s: reset timed-out, status=0x%02x\n",
233 hwif->name, tmp);
234 drive->failures++;
235 err = -EIO;
236 } else {
237 tmp = ide_read_error(drive);
238
239 if (tmp == 1) {
240 printk(KERN_INFO "%s: reset: success\n", hwif->name);
241 drive->failures = 0;
242 } else {
243 ide_reset_report_error(hwif, tmp);
244 drive->failures++;
245 err = -EIO;
246 }
247 }
248out:
249 hwif->polling = 0; /* done polling */
250 ide_complete_drive_reset(drive, err);
251 return ide_stopped;
252}
253
254static void ide_disk_pre_reset(ide_drive_t *drive)
255{
256 int legacy = (drive->id[ATA_ID_CFS_ENABLE_2] & 0x0400) ? 0 : 1;
257
258 drive->special.all = 0;
259 drive->special.b.set_geometry = legacy;
260 drive->special.b.recalibrate = legacy;
261
262 drive->mult_count = 0;
263 drive->dev_flags &= ~IDE_DFLAG_PARKED;
264
265 if ((drive->dev_flags & IDE_DFLAG_KEEP_SETTINGS) == 0 &&
266 (drive->dev_flags & IDE_DFLAG_USING_DMA) == 0)
267 drive->mult_req = 0;
268
269 if (drive->mult_req != drive->mult_count)
270 drive->special.b.set_multmode = 1;
271}
272
273static void pre_reset(ide_drive_t *drive)
274{
275 const struct ide_port_ops *port_ops = drive->hwif->port_ops;
276
277 if (drive->media == ide_disk)
278 ide_disk_pre_reset(drive);
279 else
280 drive->dev_flags |= IDE_DFLAG_POST_RESET;
281
282 if (drive->dev_flags & IDE_DFLAG_USING_DMA) {
283 if (drive->crc_count)
284 ide_check_dma_crc(drive);
285 else
286 ide_dma_off(drive);
287 }
288
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;
292 drive->io_32bit = 0;
293 }
294 return;
295 }
296
297 if (port_ops && port_ops->pre_reset)
298 port_ops->pre_reset(drive);
299
300 if (drive->current_speed != 0xff)
301 drive->desired_speed = drive->current_speed;
302 drive->current_speed = 0xff;
303}
304
305/*
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.
310 *
311 * ATAPI devices have their own reset mechanism which allows them to be
312 * individually reset without clobbering other devices on the same interface.
313 *
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.
319 */
320static ide_startstop_t do_reset1(ide_drive_t *drive, int do_not_try_atapi)
321{
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;
326 ide_drive_t *tdrive;
327 unsigned long flags, timeout;
328 int i;
329 DEFINE_WAIT(wait);
330
331 spin_lock_irqsave(&hwif->lock, flags);
332
333 /* We must not reset with running handlers */
334 BUG_ON(hwif->handler != NULL);
335
336 /* For an ATAPI device, first try an ATAPI SRST. */
337 if (drive->media != ide_disk && !do_not_try_atapi) {
338 pre_reset(drive);
339 SELECT_DRIVE(drive);
340 udelay(20);
341 tp_ops->exec_command(hwif, ATA_CMD_DEV_RESET);
342 ndelay(400);
343 hwif->poll_timeout = jiffies + WAIT_WORSTCASE;
344 hwif->polling = 1;
345 __ide_set_handler(drive, &atapi_reset_pollfunc, HZ/20, NULL);
346 spin_unlock_irqrestore(&hwif->lock, flags);
347 return ide_started;
348 }
349
350 /* We must not disturb devices in the IDE_DFLAG_PARKED state. */
351 do {
352 unsigned long now;
353
354 prepare_to_wait(&ide_park_wq, &wait, TASK_UNINTERRUPTIBLE);
355 timeout = jiffies;
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;
360 }
361
362 now = jiffies;
363 if (time_before_eq(timeout, now))
364 break;
365
366 spin_unlock_irqrestore(&hwif->lock, flags);
367 timeout = schedule_timeout_uninterruptible(timeout - now);
368 spin_lock_irqsave(&hwif->lock, flags);
369 } while (timeout);
370 finish_wait(&ide_park_wq, &wait);
371
372 /*
373 * First, reset any device state data we were maintaining
374 * for any of the drives on this interface.
375 */
376 ide_port_for_each_dev(i, tdrive, hwif)
377 pre_reset(tdrive);
378
379 if (io_ports->ctl_addr == 0) {
380 spin_unlock_irqrestore(&hwif->lock, flags);
381 ide_complete_drive_reset(drive, -ENXIO);
382 return ide_stopped;
383 }
384
385 /*
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.
392 *
393 * TODO: add ->softreset method and stop abusing ->set_irq
394 */
395 /* set SRST and nIEN */
396 tp_ops->set_irq(hwif, 4);
397 /* more than enough time */
398 udelay(10);
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 */
402 udelay(10);
403 hwif->poll_timeout = jiffies + WAIT_WORSTCASE;
404 hwif->polling = 1;
405 __ide_set_handler(drive, &reset_pollfunc, HZ/20, NULL);
406
407 /*
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
411 */
412 port_ops = hwif->port_ops;
413 if (port_ops && port_ops->resetproc)
414 port_ops->resetproc(drive);
415
416 spin_unlock_irqrestore(&hwif->lock, flags);
417 return ide_started;
418}
419
420/*
421 * ide_do_reset() is the entry point to the drive/interface reset code.
422 */
423
424ide_startstop_t ide_do_reset(ide_drive_t *drive)
425{
426 return do_reset1(drive, 0);
427}
428EXPORT_SYMBOL(ide_do_reset);
diff --git a/drivers/ide/ide-io.c b/drivers/ide/ide-io.c
index 74d1a3e68252..2e92497b58aa 100644
--- a/drivers/ide/ide-io.c
+++ b/drivers/ide/ide-io.c
@@ -196,7 +196,7 @@ void ide_end_drive_cmd (ide_drive_t *drive, u8 stat, u8 err)
196} 196}
197EXPORT_SYMBOL(ide_end_drive_cmd); 197EXPORT_SYMBOL(ide_end_drive_cmd);
198 198
199static void ide_kill_rq(ide_drive_t *drive, struct request *rq) 199void ide_kill_rq(ide_drive_t *drive, struct request *rq)
200{ 200{
201 if (rq->rq_disk) { 201 if (rq->rq_disk) {
202 struct ide_driver *drv; 202 struct ide_driver *drv;
@@ -207,133 +207,6 @@ static void ide_kill_rq(ide_drive_t *drive, struct request *rq)
207 ide_end_request(drive, 0, 0); 207 ide_end_request(drive, 0, 0);
208} 208}
209 209
210static ide_startstop_t ide_ata_error(ide_drive_t *drive, struct request *rq, u8 stat, u8 err)
211{
212 ide_hwif_t *hwif = drive->hwif;
213
214 if ((stat & ATA_BUSY) ||
215 ((stat & ATA_DF) && (drive->dev_flags & IDE_DFLAG_NOWERR) == 0)) {
216 /* other bits are useless when BUSY */
217 rq->errors |= ERROR_RESET;
218 } else if (stat & ATA_ERR) {
219 /* err has different meaning on cdrom and tape */
220 if (err == ATA_ABORTED) {
221 if ((drive->dev_flags & IDE_DFLAG_LBA) &&
222 /* some newer drives don't support ATA_CMD_INIT_DEV_PARAMS */
223 hwif->tp_ops->read_status(hwif) == ATA_CMD_INIT_DEV_PARAMS)
224 return ide_stopped;
225 } else if ((err & BAD_CRC) == BAD_CRC) {
226 /* UDMA crc error, just retry the operation */
227 drive->crc_count++;
228 } else if (err & (ATA_BBK | ATA_UNC)) {
229 /* retries won't help these */
230 rq->errors = ERROR_MAX;
231 } else if (err & ATA_TRK0NF) {
232 /* help it find track zero */
233 rq->errors |= ERROR_RECAL;
234 }
235 }
236
237 if ((stat & ATA_DRQ) && rq_data_dir(rq) == READ &&
238 (hwif->host_flags & IDE_HFLAG_ERROR_STOPS_FIFO) == 0) {
239 int nsect = drive->mult_count ? drive->mult_count : 1;
240
241 ide_pad_transfer(drive, READ, nsect * SECTOR_SIZE);
242 }
243
244 if (rq->errors >= ERROR_MAX || blk_noretry_request(rq)) {
245 ide_kill_rq(drive, rq);
246 return ide_stopped;
247 }
248
249 if (hwif->tp_ops->read_status(hwif) & (ATA_BUSY | ATA_DRQ))
250 rq->errors |= ERROR_RESET;
251
252 if ((rq->errors & ERROR_RESET) == ERROR_RESET) {
253 ++rq->errors;
254 return ide_do_reset(drive);
255 }
256
257 if ((rq->errors & ERROR_RECAL) == ERROR_RECAL)
258 drive->special.b.recalibrate = 1;
259
260 ++rq->errors;
261
262 return ide_stopped;
263}
264
265static ide_startstop_t ide_atapi_error(ide_drive_t *drive, struct request *rq, u8 stat, u8 err)
266{
267 ide_hwif_t *hwif = drive->hwif;
268
269 if ((stat & ATA_BUSY) ||
270 ((stat & ATA_DF) && (drive->dev_flags & IDE_DFLAG_NOWERR) == 0)) {
271 /* other bits are useless when BUSY */
272 rq->errors |= ERROR_RESET;
273 } else {
274 /* add decoding error stuff */
275 }
276
277 if (hwif->tp_ops->read_status(hwif) & (ATA_BUSY | ATA_DRQ))
278 /* force an abort */
279 hwif->tp_ops->exec_command(hwif, ATA_CMD_IDLEIMMEDIATE);
280
281 if (rq->errors >= ERROR_MAX) {
282 ide_kill_rq(drive, rq);
283 } else {
284 if ((rq->errors & ERROR_RESET) == ERROR_RESET) {
285 ++rq->errors;
286 return ide_do_reset(drive);
287 }
288 ++rq->errors;
289 }
290
291 return ide_stopped;
292}
293
294static ide_startstop_t
295__ide_error(ide_drive_t *drive, struct request *rq, u8 stat, u8 err)
296{
297 if (drive->media == ide_disk)
298 return ide_ata_error(drive, rq, stat, err);
299 return ide_atapi_error(drive, rq, stat, err);
300}
301
302/**
303 * ide_error - handle an error on the IDE
304 * @drive: drive the error occurred on
305 * @msg: message to report
306 * @stat: status bits
307 *
308 * ide_error() takes action based on the error returned by the drive.
309 * For normal I/O that may well include retries. We deal with
310 * both new-style (taskfile) and old style command handling here.
311 * In the case of taskfile command handling there is work left to
312 * do
313 */
314
315ide_startstop_t ide_error (ide_drive_t *drive, const char *msg, u8 stat)
316{
317 struct request *rq;
318 u8 err;
319
320 err = ide_dump_status(drive, msg, stat);
321
322 rq = drive->hwif->rq;
323 if (rq == NULL)
324 return ide_stopped;
325
326 /* retry only "normal" I/O: */
327 if (!blk_fs_request(rq)) {
328 rq->errors = 1;
329 ide_end_drive_cmd(drive, stat, err);
330 return ide_stopped;
331 }
332
333 return __ide_error(drive, rq, stat, err);
334}
335EXPORT_SYMBOL_GPL(ide_error);
336
337static void ide_tf_set_specify_cmd(ide_drive_t *drive, struct ide_taskfile *tf) 210static void ide_tf_set_specify_cmd(ide_drive_t *drive, struct ide_taskfile *tf)
338{ 211{
339 tf->nsect = drive->sect; 212 tf->nsect = drive->sect;
diff --git a/drivers/ide/ide-iops.c b/drivers/ide/ide-iops.c
index cf6c3036ae7f..e0cfa2d2acc7 100644
--- a/drivers/ide/ide-iops.c
+++ b/drivers/ide/ide-iops.c
@@ -446,8 +446,8 @@ int ide_config_drive_speed(ide_drive_t *drive, u8 speed)
446 * 446 *
447 * See also ide_execute_command 447 * See also ide_execute_command
448 */ 448 */
449static void __ide_set_handler (ide_drive_t *drive, ide_handler_t *handler, 449void __ide_set_handler(ide_drive_t *drive, ide_handler_t *handler,
450 unsigned int timeout, ide_expiry_t *expiry) 450 unsigned int timeout, ide_expiry_t *expiry)
451{ 451{
452 ide_hwif_t *hwif = drive->hwif; 452 ide_hwif_t *hwif = drive->hwif;
453 453
@@ -517,301 +517,6 @@ void ide_execute_pkt_cmd(ide_drive_t *drive)
517} 517}
518EXPORT_SYMBOL_GPL(ide_execute_pkt_cmd); 518EXPORT_SYMBOL_GPL(ide_execute_pkt_cmd);
519 519
520static inline void ide_complete_drive_reset(ide_drive_t *drive, int err)
521{
522 struct request *rq = drive->hwif->rq;
523
524 if (rq && blk_special_request(rq) && rq->cmd[0] == REQ_DRIVE_RESET)
525 ide_end_request(drive, err ? err : 1, 0);
526}
527
528/* needed below */
529static ide_startstop_t do_reset1(ide_drive_t *, int);
530
531/*
532 * atapi_reset_pollfunc() gets invoked to poll the interface for completion
533 * every 50ms during an atapi drive reset operation. If the drive has not yet
534 * responded, and we have not yet hit our maximum waiting time, then the timer
535 * is restarted for another 50ms.
536 */
537static ide_startstop_t atapi_reset_pollfunc(ide_drive_t *drive)
538{
539 ide_hwif_t *hwif = drive->hwif;
540 u8 stat;
541
542 SELECT_DRIVE(drive);
543 udelay(10);
544 stat = hwif->tp_ops->read_status(hwif);
545
546 if (OK_STAT(stat, 0, ATA_BUSY))
547 printk(KERN_INFO "%s: ATAPI reset complete\n", drive->name);
548 else {
549 if (time_before(jiffies, hwif->poll_timeout)) {
550 ide_set_handler(drive, &atapi_reset_pollfunc, HZ/20,
551 NULL);
552 /* continue polling */
553 return ide_started;
554 }
555 /* end of polling */
556 hwif->polling = 0;
557 printk(KERN_ERR "%s: ATAPI reset timed-out, status=0x%02x\n",
558 drive->name, stat);
559 /* do it the old fashioned way */
560 return do_reset1(drive, 1);
561 }
562 /* done polling */
563 hwif->polling = 0;
564 ide_complete_drive_reset(drive, 0);
565 return ide_stopped;
566}
567
568static void ide_reset_report_error(ide_hwif_t *hwif, u8 err)
569{
570 static const char *err_master_vals[] =
571 { NULL, "passed", "formatter device error",
572 "sector buffer error", "ECC circuitry error",
573 "controlling MPU error" };
574
575 u8 err_master = err & 0x7f;
576
577 printk(KERN_ERR "%s: reset: master: ", hwif->name);
578 if (err_master && err_master < 6)
579 printk(KERN_CONT "%s", err_master_vals[err_master]);
580 else
581 printk(KERN_CONT "error (0x%02x?)", err);
582 if (err & 0x80)
583 printk(KERN_CONT "; slave: failed");
584 printk(KERN_CONT "\n");
585}
586
587/*
588 * reset_pollfunc() gets invoked to poll the interface for completion every 50ms
589 * during an ide reset operation. If the drives have not yet responded,
590 * and we have not yet hit our maximum waiting time, then the timer is restarted
591 * for another 50ms.
592 */
593static ide_startstop_t reset_pollfunc(ide_drive_t *drive)
594{
595 ide_hwif_t *hwif = drive->hwif;
596 const struct ide_port_ops *port_ops = hwif->port_ops;
597 u8 tmp;
598 int err = 0;
599
600 if (port_ops && port_ops->reset_poll) {
601 err = port_ops->reset_poll(drive);
602 if (err) {
603 printk(KERN_ERR "%s: host reset_poll failure for %s.\n",
604 hwif->name, drive->name);
605 goto out;
606 }
607 }
608
609 tmp = hwif->tp_ops->read_status(hwif);
610
611 if (!OK_STAT(tmp, 0, ATA_BUSY)) {
612 if (time_before(jiffies, hwif->poll_timeout)) {
613 ide_set_handler(drive, &reset_pollfunc, HZ/20, NULL);
614 /* continue polling */
615 return ide_started;
616 }
617 printk(KERN_ERR "%s: reset timed-out, status=0x%02x\n",
618 hwif->name, tmp);
619 drive->failures++;
620 err = -EIO;
621 } else {
622 tmp = ide_read_error(drive);
623
624 if (tmp == 1) {
625 printk(KERN_INFO "%s: reset: success\n", hwif->name);
626 drive->failures = 0;
627 } else {
628 ide_reset_report_error(hwif, tmp);
629 drive->failures++;
630 err = -EIO;
631 }
632 }
633out:
634 hwif->polling = 0; /* done polling */
635 ide_complete_drive_reset(drive, err);
636 return ide_stopped;
637}
638
639static void ide_disk_pre_reset(ide_drive_t *drive)
640{
641 int legacy = (drive->id[ATA_ID_CFS_ENABLE_2] & 0x0400) ? 0 : 1;
642
643 drive->special.all = 0;
644 drive->special.b.set_geometry = legacy;
645 drive->special.b.recalibrate = legacy;
646
647 drive->mult_count = 0;
648 drive->dev_flags &= ~IDE_DFLAG_PARKED;
649
650 if ((drive->dev_flags & IDE_DFLAG_KEEP_SETTINGS) == 0 &&
651 (drive->dev_flags & IDE_DFLAG_USING_DMA) == 0)
652 drive->mult_req = 0;
653
654 if (drive->mult_req != drive->mult_count)
655 drive->special.b.set_multmode = 1;
656}
657
658static void pre_reset(ide_drive_t *drive)
659{
660 const struct ide_port_ops *port_ops = drive->hwif->port_ops;
661
662 if (drive->media == ide_disk)
663 ide_disk_pre_reset(drive);
664 else
665 drive->dev_flags |= IDE_DFLAG_POST_RESET;
666
667 if (drive->dev_flags & IDE_DFLAG_USING_DMA) {
668 if (drive->crc_count)
669 ide_check_dma_crc(drive);
670 else
671 ide_dma_off(drive);
672 }
673
674 if ((drive->dev_flags & IDE_DFLAG_KEEP_SETTINGS) == 0) {
675 if ((drive->dev_flags & IDE_DFLAG_USING_DMA) == 0) {
676 drive->dev_flags &= ~IDE_DFLAG_UNMASK;
677 drive->io_32bit = 0;
678 }
679 return;
680 }
681
682 if (port_ops && port_ops->pre_reset)
683 port_ops->pre_reset(drive);
684
685 if (drive->current_speed != 0xff)
686 drive->desired_speed = drive->current_speed;
687 drive->current_speed = 0xff;
688}
689
690/*
691 * do_reset1() attempts to recover a confused drive by resetting it.
692 * Unfortunately, resetting a disk drive actually resets all devices on
693 * the same interface, so it can really be thought of as resetting the
694 * interface rather than resetting the drive.
695 *
696 * ATAPI devices have their own reset mechanism which allows them to be
697 * individually reset without clobbering other devices on the same interface.
698 *
699 * Unfortunately, the IDE interface does not generate an interrupt to let
700 * us know when the reset operation has finished, so we must poll for this.
701 * Equally poor, though, is the fact that this may a very long time to complete,
702 * (up to 30 seconds worstcase). So, instead of busy-waiting here for it,
703 * we set a timer to poll at 50ms intervals.
704 */
705static ide_startstop_t do_reset1(ide_drive_t *drive, int do_not_try_atapi)
706{
707 ide_hwif_t *hwif = drive->hwif;
708 struct ide_io_ports *io_ports = &hwif->io_ports;
709 const struct ide_tp_ops *tp_ops = hwif->tp_ops;
710 const struct ide_port_ops *port_ops;
711 ide_drive_t *tdrive;
712 unsigned long flags, timeout;
713 int i;
714 DEFINE_WAIT(wait);
715
716 spin_lock_irqsave(&hwif->lock, flags);
717
718 /* We must not reset with running handlers */
719 BUG_ON(hwif->handler != NULL);
720
721 /* For an ATAPI device, first try an ATAPI SRST. */
722 if (drive->media != ide_disk && !do_not_try_atapi) {
723 pre_reset(drive);
724 SELECT_DRIVE(drive);
725 udelay(20);
726 tp_ops->exec_command(hwif, ATA_CMD_DEV_RESET);
727 ndelay(400);
728 hwif->poll_timeout = jiffies + WAIT_WORSTCASE;
729 hwif->polling = 1;
730 __ide_set_handler(drive, &atapi_reset_pollfunc, HZ/20, NULL);
731 spin_unlock_irqrestore(&hwif->lock, flags);
732 return ide_started;
733 }
734
735 /* We must not disturb devices in the IDE_DFLAG_PARKED state. */
736 do {
737 unsigned long now;
738
739 prepare_to_wait(&ide_park_wq, &wait, TASK_UNINTERRUPTIBLE);
740 timeout = jiffies;
741 ide_port_for_each_present_dev(i, tdrive, hwif) {
742 if ((tdrive->dev_flags & IDE_DFLAG_PARKED) &&
743 time_after(tdrive->sleep, timeout))
744 timeout = tdrive->sleep;
745 }
746
747 now = jiffies;
748 if (time_before_eq(timeout, now))
749 break;
750
751 spin_unlock_irqrestore(&hwif->lock, flags);
752 timeout = schedule_timeout_uninterruptible(timeout - now);
753 spin_lock_irqsave(&hwif->lock, flags);
754 } while (timeout);
755 finish_wait(&ide_park_wq, &wait);
756
757 /*
758 * First, reset any device state data we were maintaining
759 * for any of the drives on this interface.
760 */
761 ide_port_for_each_dev(i, tdrive, hwif)
762 pre_reset(tdrive);
763
764 if (io_ports->ctl_addr == 0) {
765 spin_unlock_irqrestore(&hwif->lock, flags);
766 ide_complete_drive_reset(drive, -ENXIO);
767 return ide_stopped;
768 }
769
770 /*
771 * Note that we also set nIEN while resetting the device,
772 * to mask unwanted interrupts from the interface during the reset.
773 * However, due to the design of PC hardware, this will cause an
774 * immediate interrupt due to the edge transition it produces.
775 * This single interrupt gives us a "fast poll" for drives that
776 * recover from reset very quickly, saving us the first 50ms wait time.
777 *
778 * TODO: add ->softreset method and stop abusing ->set_irq
779 */
780 /* set SRST and nIEN */
781 tp_ops->set_irq(hwif, 4);
782 /* more than enough time */
783 udelay(10);
784 /* clear SRST, leave nIEN (unless device is on the quirk list) */
785 tp_ops->set_irq(hwif, drive->quirk_list == 2);
786 /* more than enough time */
787 udelay(10);
788 hwif->poll_timeout = jiffies + WAIT_WORSTCASE;
789 hwif->polling = 1;
790 __ide_set_handler(drive, &reset_pollfunc, HZ/20, NULL);
791
792 /*
793 * Some weird controller like resetting themselves to a strange
794 * state when the disks are reset this way. At least, the Winbond
795 * 553 documentation says that
796 */
797 port_ops = hwif->port_ops;
798 if (port_ops && port_ops->resetproc)
799 port_ops->resetproc(drive);
800
801 spin_unlock_irqrestore(&hwif->lock, flags);
802 return ide_started;
803}
804
805/*
806 * ide_do_reset() is the entry point to the drive/interface reset code.
807 */
808
809ide_startstop_t ide_do_reset(ide_drive_t *drive)
810{
811 return do_reset1(drive, 0);
812}
813EXPORT_SYMBOL(ide_do_reset);
814
815/* 520/*
816 * ide_wait_not_busy() waits for the currently selected device on the hwif 521 * ide_wait_not_busy() waits for the currently selected device on the hwif
817 * to report a non-busy status, see comments in ide_probe_port(). 522 * to report a non-busy status, see comments in ide_probe_port().