Linux-2.6.12-rc2
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / ide / ide-io.c
blob248e3cc8b3527dd37bbc8a60417a1658e46d8845
1 /*
2 * IDE I/O functions
4 * Basic PIO and command management functionality.
6 * This code was split off from ide.c. See ide.c for history and original
7 * copyrights.
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by the
11 * Free Software Foundation; either version 2, or (at your option) any
12 * later version.
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
19 * For the avoidance of doubt the "preferred form" of this code is one which
20 * is in an open non patent encumbered format. Where cryptographic key signing
21 * forms part of the process of creating an executable the information
22 * including keys needed to generate an equivalently functional executable
23 * are deemed to be part of the source code.
27 #include <linux/config.h>
28 #include <linux/module.h>
29 #include <linux/types.h>
30 #include <linux/string.h>
31 #include <linux/kernel.h>
32 #include <linux/timer.h>
33 #include <linux/mm.h>
34 #include <linux/interrupt.h>
35 #include <linux/major.h>
36 #include <linux/errno.h>
37 #include <linux/genhd.h>
38 #include <linux/blkpg.h>
39 #include <linux/slab.h>
40 #include <linux/init.h>
41 #include <linux/pci.h>
42 #include <linux/delay.h>
43 #include <linux/ide.h>
44 #include <linux/completion.h>
45 #include <linux/reboot.h>
46 #include <linux/cdrom.h>
47 #include <linux/seq_file.h>
48 #include <linux/device.h>
49 #include <linux/kmod.h>
50 #include <linux/scatterlist.h>
52 #include <asm/byteorder.h>
53 #include <asm/irq.h>
54 #include <asm/uaccess.h>
55 #include <asm/io.h>
56 #include <asm/bitops.h>
58 int __ide_end_request(ide_drive_t *drive, struct request *rq, int uptodate,
59 int nr_sectors)
61 int ret = 1;
63 BUG_ON(!(rq->flags & REQ_STARTED));
66 * if failfast is set on a request, override number of sectors and
67 * complete the whole request right now
69 if (blk_noretry_request(rq) && end_io_error(uptodate))
70 nr_sectors = rq->hard_nr_sectors;
72 if (!blk_fs_request(rq) && end_io_error(uptodate) && !rq->errors)
73 rq->errors = -EIO;
76 * decide whether to reenable DMA -- 3 is a random magic for now,
77 * if we DMA timeout more than 3 times, just stay in PIO
79 if (drive->state == DMA_PIO_RETRY && drive->retry_pio <= 3) {
80 drive->state = 0;
81 HWGROUP(drive)->hwif->ide_dma_on(drive);
84 if (!end_that_request_first(rq, uptodate, nr_sectors)) {
85 add_disk_randomness(rq->rq_disk);
87 if (blk_rq_tagged(rq))
88 blk_queue_end_tag(drive->queue, rq);
90 blkdev_dequeue_request(rq);
91 HWGROUP(drive)->rq = NULL;
92 end_that_request_last(rq);
93 ret = 0;
95 return ret;
97 EXPORT_SYMBOL(__ide_end_request);
99 /**
100 * ide_end_request - complete an IDE I/O
101 * @drive: IDE device for the I/O
102 * @uptodate:
103 * @nr_sectors: number of sectors completed
105 * This is our end_request wrapper function. We complete the I/O
106 * update random number input and dequeue the request, which if
107 * it was tagged may be out of order.
110 int ide_end_request (ide_drive_t *drive, int uptodate, int nr_sectors)
112 struct request *rq;
113 unsigned long flags;
114 int ret = 1;
116 spin_lock_irqsave(&ide_lock, flags);
117 rq = HWGROUP(drive)->rq;
119 if (!nr_sectors)
120 nr_sectors = rq->hard_cur_sectors;
122 if (blk_complete_barrier_rq_locked(drive->queue, rq, nr_sectors))
123 ret = rq->nr_sectors != 0;
124 else
125 ret = __ide_end_request(drive, rq, uptodate, nr_sectors);
127 spin_unlock_irqrestore(&ide_lock, flags);
128 return ret;
130 EXPORT_SYMBOL(ide_end_request);
133 * Power Management state machine. This one is rather trivial for now,
134 * we should probably add more, like switching back to PIO on suspend
135 * to help some BIOSes, re-do the door locking on resume, etc...
138 enum {
139 ide_pm_flush_cache = ide_pm_state_start_suspend,
140 idedisk_pm_standby,
142 idedisk_pm_idle = ide_pm_state_start_resume,
143 ide_pm_restore_dma,
146 static void ide_complete_power_step(ide_drive_t *drive, struct request *rq, u8 stat, u8 error)
148 if (drive->media != ide_disk)
149 return;
151 switch (rq->pm->pm_step) {
152 case ide_pm_flush_cache: /* Suspend step 1 (flush cache) complete */
153 if (rq->pm->pm_state == 4)
154 rq->pm->pm_step = ide_pm_state_completed;
155 else
156 rq->pm->pm_step = idedisk_pm_standby;
157 break;
158 case idedisk_pm_standby: /* Suspend step 2 (standby) complete */
159 rq->pm->pm_step = ide_pm_state_completed;
160 break;
161 case idedisk_pm_idle: /* Resume step 1 (idle) complete */
162 rq->pm->pm_step = ide_pm_restore_dma;
163 break;
167 static ide_startstop_t ide_start_power_step(ide_drive_t *drive, struct request *rq)
169 ide_task_t *args = rq->special;
171 memset(args, 0, sizeof(*args));
173 if (drive->media != ide_disk) {
174 /* skip idedisk_pm_idle for ATAPI devices */
175 if (rq->pm->pm_step == idedisk_pm_idle)
176 rq->pm->pm_step = ide_pm_restore_dma;
179 switch (rq->pm->pm_step) {
180 case ide_pm_flush_cache: /* Suspend step 1 (flush cache) */
181 if (drive->media != ide_disk)
182 break;
183 /* Not supported? Switch to next step now. */
184 if (!drive->wcache || !ide_id_has_flush_cache(drive->id)) {
185 ide_complete_power_step(drive, rq, 0, 0);
186 return ide_stopped;
188 if (ide_id_has_flush_cache_ext(drive->id))
189 args->tfRegister[IDE_COMMAND_OFFSET] = WIN_FLUSH_CACHE_EXT;
190 else
191 args->tfRegister[IDE_COMMAND_OFFSET] = WIN_FLUSH_CACHE;
192 args->command_type = IDE_DRIVE_TASK_NO_DATA;
193 args->handler = &task_no_data_intr;
194 return do_rw_taskfile(drive, args);
196 case idedisk_pm_standby: /* Suspend step 2 (standby) */
197 args->tfRegister[IDE_COMMAND_OFFSET] = WIN_STANDBYNOW1;
198 args->command_type = IDE_DRIVE_TASK_NO_DATA;
199 args->handler = &task_no_data_intr;
200 return do_rw_taskfile(drive, args);
202 case idedisk_pm_idle: /* Resume step 1 (idle) */
203 args->tfRegister[IDE_COMMAND_OFFSET] = WIN_IDLEIMMEDIATE;
204 args->command_type = IDE_DRIVE_TASK_NO_DATA;
205 args->handler = task_no_data_intr;
206 return do_rw_taskfile(drive, args);
208 case ide_pm_restore_dma: /* Resume step 2 (restore DMA) */
210 * Right now, all we do is call hwif->ide_dma_check(drive),
211 * we could be smarter and check for current xfer_speed
212 * in struct drive etc...
214 if ((drive->id->capability & 1) == 0)
215 break;
216 if (drive->hwif->ide_dma_check == NULL)
217 break;
218 drive->hwif->ide_dma_check(drive);
219 break;
221 rq->pm->pm_step = ide_pm_state_completed;
222 return ide_stopped;
226 * ide_complete_pm_request - end the current Power Management request
227 * @drive: target drive
228 * @rq: request
230 * This function cleans up the current PM request and stops the queue
231 * if necessary.
233 static void ide_complete_pm_request (ide_drive_t *drive, struct request *rq)
235 unsigned long flags;
237 #ifdef DEBUG_PM
238 printk("%s: completing PM request, %s\n", drive->name,
239 blk_pm_suspend_request(rq) ? "suspend" : "resume");
240 #endif
241 spin_lock_irqsave(&ide_lock, flags);
242 if (blk_pm_suspend_request(rq)) {
243 blk_stop_queue(drive->queue);
244 } else {
245 drive->blocked = 0;
246 blk_start_queue(drive->queue);
248 blkdev_dequeue_request(rq);
249 HWGROUP(drive)->rq = NULL;
250 end_that_request_last(rq);
251 spin_unlock_irqrestore(&ide_lock, flags);
255 * FIXME: probably move this somewhere else, name is bad too :)
257 u64 ide_get_error_location(ide_drive_t *drive, char *args)
259 u32 high, low;
260 u8 hcyl, lcyl, sect;
261 u64 sector;
263 high = 0;
264 hcyl = args[5];
265 lcyl = args[4];
266 sect = args[3];
268 if (ide_id_has_flush_cache_ext(drive->id)) {
269 low = (hcyl << 16) | (lcyl << 8) | sect;
270 HWIF(drive)->OUTB(drive->ctl|0x80, IDE_CONTROL_REG);
271 high = ide_read_24(drive);
272 } else {
273 u8 cur = HWIF(drive)->INB(IDE_SELECT_REG);
274 if (cur & 0x40) {
275 high = cur & 0xf;
276 low = (hcyl << 16) | (lcyl << 8) | sect;
277 } else {
278 low = hcyl * drive->head * drive->sect;
279 low += lcyl * drive->sect;
280 low += sect - 1;
284 sector = ((u64) high << 24) | low;
285 return sector;
287 EXPORT_SYMBOL(ide_get_error_location);
290 * ide_end_drive_cmd - end an explicit drive command
291 * @drive: command
292 * @stat: status bits
293 * @err: error bits
295 * Clean up after success/failure of an explicit drive command.
296 * These get thrown onto the queue so they are synchronized with
297 * real I/O operations on the drive.
299 * In LBA48 mode we have to read the register set twice to get
300 * all the extra information out.
303 void ide_end_drive_cmd (ide_drive_t *drive, u8 stat, u8 err)
305 ide_hwif_t *hwif = HWIF(drive);
306 unsigned long flags;
307 struct request *rq;
309 spin_lock_irqsave(&ide_lock, flags);
310 rq = HWGROUP(drive)->rq;
311 spin_unlock_irqrestore(&ide_lock, flags);
313 if (rq->flags & REQ_DRIVE_CMD) {
314 u8 *args = (u8 *) rq->buffer;
315 if (rq->errors == 0)
316 rq->errors = !OK_STAT(stat,READY_STAT,BAD_STAT);
318 if (args) {
319 args[0] = stat;
320 args[1] = err;
321 args[2] = hwif->INB(IDE_NSECTOR_REG);
323 } else if (rq->flags & REQ_DRIVE_TASK) {
324 u8 *args = (u8 *) rq->buffer;
325 if (rq->errors == 0)
326 rq->errors = !OK_STAT(stat,READY_STAT,BAD_STAT);
328 if (args) {
329 args[0] = stat;
330 args[1] = err;
331 args[2] = hwif->INB(IDE_NSECTOR_REG);
332 args[3] = hwif->INB(IDE_SECTOR_REG);
333 args[4] = hwif->INB(IDE_LCYL_REG);
334 args[5] = hwif->INB(IDE_HCYL_REG);
335 args[6] = hwif->INB(IDE_SELECT_REG);
337 } else if (rq->flags & REQ_DRIVE_TASKFILE) {
338 ide_task_t *args = (ide_task_t *) rq->special;
339 if (rq->errors == 0)
340 rq->errors = !OK_STAT(stat,READY_STAT,BAD_STAT);
342 if (args) {
343 if (args->tf_in_flags.b.data) {
344 u16 data = hwif->INW(IDE_DATA_REG);
345 args->tfRegister[IDE_DATA_OFFSET] = (data) & 0xFF;
346 args->hobRegister[IDE_DATA_OFFSET] = (data >> 8) & 0xFF;
348 args->tfRegister[IDE_ERROR_OFFSET] = err;
349 /* be sure we're looking at the low order bits */
350 hwif->OUTB(drive->ctl & ~0x80, IDE_CONTROL_REG);
351 args->tfRegister[IDE_NSECTOR_OFFSET] = hwif->INB(IDE_NSECTOR_REG);
352 args->tfRegister[IDE_SECTOR_OFFSET] = hwif->INB(IDE_SECTOR_REG);
353 args->tfRegister[IDE_LCYL_OFFSET] = hwif->INB(IDE_LCYL_REG);
354 args->tfRegister[IDE_HCYL_OFFSET] = hwif->INB(IDE_HCYL_REG);
355 args->tfRegister[IDE_SELECT_OFFSET] = hwif->INB(IDE_SELECT_REG);
356 args->tfRegister[IDE_STATUS_OFFSET] = stat;
358 if (drive->addressing == 1) {
359 hwif->OUTB(drive->ctl|0x80, IDE_CONTROL_REG);
360 args->hobRegister[IDE_FEATURE_OFFSET] = hwif->INB(IDE_FEATURE_REG);
361 args->hobRegister[IDE_NSECTOR_OFFSET] = hwif->INB(IDE_NSECTOR_REG);
362 args->hobRegister[IDE_SECTOR_OFFSET] = hwif->INB(IDE_SECTOR_REG);
363 args->hobRegister[IDE_LCYL_OFFSET] = hwif->INB(IDE_LCYL_REG);
364 args->hobRegister[IDE_HCYL_OFFSET] = hwif->INB(IDE_HCYL_REG);
367 } else if (blk_pm_request(rq)) {
368 #ifdef DEBUG_PM
369 printk("%s: complete_power_step(step: %d, stat: %x, err: %x)\n",
370 drive->name, rq->pm->pm_step, stat, err);
371 #endif
372 ide_complete_power_step(drive, rq, stat, err);
373 if (rq->pm->pm_step == ide_pm_state_completed)
374 ide_complete_pm_request(drive, rq);
375 return;
378 spin_lock_irqsave(&ide_lock, flags);
379 blkdev_dequeue_request(rq);
380 HWGROUP(drive)->rq = NULL;
381 rq->errors = err;
382 end_that_request_last(rq);
383 spin_unlock_irqrestore(&ide_lock, flags);
386 EXPORT_SYMBOL(ide_end_drive_cmd);
389 * try_to_flush_leftover_data - flush junk
390 * @drive: drive to flush
392 * try_to_flush_leftover_data() is invoked in response to a drive
393 * unexpectedly having its DRQ_STAT bit set. As an alternative to
394 * resetting the drive, this routine tries to clear the condition
395 * by read a sector's worth of data from the drive. Of course,
396 * this may not help if the drive is *waiting* for data from *us*.
398 static void try_to_flush_leftover_data (ide_drive_t *drive)
400 int i = (drive->mult_count ? drive->mult_count : 1) * SECTOR_WORDS;
402 if (drive->media != ide_disk)
403 return;
404 while (i > 0) {
405 u32 buffer[16];
406 u32 wcount = (i > 16) ? 16 : i;
408 i -= wcount;
409 HWIF(drive)->ata_input_data(drive, buffer, wcount);
413 static void ide_kill_rq(ide_drive_t *drive, struct request *rq)
415 if (rq->rq_disk) {
416 ide_driver_t *drv;
418 drv = *(ide_driver_t **)rq->rq_disk->private_data;
419 drv->end_request(drive, 0, 0);
420 } else
421 ide_end_request(drive, 0, 0);
424 static ide_startstop_t ide_ata_error(ide_drive_t *drive, struct request *rq, u8 stat, u8 err)
426 ide_hwif_t *hwif = drive->hwif;
428 if (stat & BUSY_STAT || ((stat & WRERR_STAT) && !drive->nowerr)) {
429 /* other bits are useless when BUSY */
430 rq->errors |= ERROR_RESET;
431 } else if (stat & ERR_STAT) {
432 /* err has different meaning on cdrom and tape */
433 if (err == ABRT_ERR) {
434 if (drive->select.b.lba &&
435 /* some newer drives don't support WIN_SPECIFY */
436 hwif->INB(IDE_COMMAND_REG) == WIN_SPECIFY)
437 return ide_stopped;
438 } else if ((err & BAD_CRC) == BAD_CRC) {
439 /* UDMA crc error, just retry the operation */
440 drive->crc_count++;
441 } else if (err & (BBD_ERR | ECC_ERR)) {
442 /* retries won't help these */
443 rq->errors = ERROR_MAX;
444 } else if (err & TRK0_ERR) {
445 /* help it find track zero */
446 rq->errors |= ERROR_RECAL;
450 if ((stat & DRQ_STAT) && rq_data_dir(rq) == READ)
451 try_to_flush_leftover_data(drive);
453 if (hwif->INB(IDE_STATUS_REG) & (BUSY_STAT|DRQ_STAT))
454 /* force an abort */
455 hwif->OUTB(WIN_IDLEIMMEDIATE, IDE_COMMAND_REG);
457 if (rq->errors >= ERROR_MAX || blk_noretry_request(rq))
458 ide_kill_rq(drive, rq);
459 else {
460 if ((rq->errors & ERROR_RESET) == ERROR_RESET) {
461 ++rq->errors;
462 return ide_do_reset(drive);
464 if ((rq->errors & ERROR_RECAL) == ERROR_RECAL)
465 drive->special.b.recalibrate = 1;
466 ++rq->errors;
468 return ide_stopped;
471 static ide_startstop_t ide_atapi_error(ide_drive_t *drive, struct request *rq, u8 stat, u8 err)
473 ide_hwif_t *hwif = drive->hwif;
475 if (stat & BUSY_STAT || ((stat & WRERR_STAT) && !drive->nowerr)) {
476 /* other bits are useless when BUSY */
477 rq->errors |= ERROR_RESET;
478 } else {
479 /* add decoding error stuff */
482 if (hwif->INB(IDE_STATUS_REG) & (BUSY_STAT|DRQ_STAT))
483 /* force an abort */
484 hwif->OUTB(WIN_IDLEIMMEDIATE, IDE_COMMAND_REG);
486 if (rq->errors >= ERROR_MAX) {
487 ide_kill_rq(drive, rq);
488 } else {
489 if ((rq->errors & ERROR_RESET) == ERROR_RESET) {
490 ++rq->errors;
491 return ide_do_reset(drive);
493 ++rq->errors;
496 return ide_stopped;
499 ide_startstop_t
500 __ide_error(ide_drive_t *drive, struct request *rq, u8 stat, u8 err)
502 if (drive->media == ide_disk)
503 return ide_ata_error(drive, rq, stat, err);
504 return ide_atapi_error(drive, rq, stat, err);
507 EXPORT_SYMBOL_GPL(__ide_error);
510 * ide_error - handle an error on the IDE
511 * @drive: drive the error occurred on
512 * @msg: message to report
513 * @stat: status bits
515 * ide_error() takes action based on the error returned by the drive.
516 * For normal I/O that may well include retries. We deal with
517 * both new-style (taskfile) and old style command handling here.
518 * In the case of taskfile command handling there is work left to
519 * do
522 ide_startstop_t ide_error (ide_drive_t *drive, const char *msg, u8 stat)
524 struct request *rq;
525 u8 err;
527 err = ide_dump_status(drive, msg, stat);
529 if ((rq = HWGROUP(drive)->rq) == NULL)
530 return ide_stopped;
532 /* retry only "normal" I/O: */
533 if (rq->flags & (REQ_DRIVE_CMD | REQ_DRIVE_TASK | REQ_DRIVE_TASKFILE)) {
534 rq->errors = 1;
535 ide_end_drive_cmd(drive, stat, err);
536 return ide_stopped;
539 if (rq->rq_disk) {
540 ide_driver_t *drv;
542 drv = *(ide_driver_t **)rq->rq_disk->private_data;
543 return drv->error(drive, rq, stat, err);
544 } else
545 return __ide_error(drive, rq, stat, err);
548 EXPORT_SYMBOL_GPL(ide_error);
550 ide_startstop_t __ide_abort(ide_drive_t *drive, struct request *rq)
552 if (drive->media != ide_disk)
553 rq->errors |= ERROR_RESET;
555 ide_kill_rq(drive, rq);
557 return ide_stopped;
560 EXPORT_SYMBOL_GPL(__ide_abort);
563 * ide_abort - abort pending IDE operatins
564 * @drive: drive the error occurred on
565 * @msg: message to report
567 * ide_abort kills and cleans up when we are about to do a
568 * host initiated reset on active commands. Longer term we
569 * want handlers to have sensible abort handling themselves
571 * This differs fundamentally from ide_error because in
572 * this case the command is doing just fine when we
573 * blow it away.
576 ide_startstop_t ide_abort(ide_drive_t *drive, const char *msg)
578 struct request *rq;
580 if (drive == NULL || (rq = HWGROUP(drive)->rq) == NULL)
581 return ide_stopped;
583 /* retry only "normal" I/O: */
584 if (rq->flags & (REQ_DRIVE_CMD | REQ_DRIVE_TASK | REQ_DRIVE_TASKFILE)) {
585 rq->errors = 1;
586 ide_end_drive_cmd(drive, BUSY_STAT, 0);
587 return ide_stopped;
590 if (rq->rq_disk) {
591 ide_driver_t *drv;
593 drv = *(ide_driver_t **)rq->rq_disk->private_data;
594 return drv->abort(drive, rq);
595 } else
596 return __ide_abort(drive, rq);
600 * ide_cmd - issue a simple drive command
601 * @drive: drive the command is for
602 * @cmd: command byte
603 * @nsect: sector byte
604 * @handler: handler for the command completion
606 * Issue a simple drive command with interrupts.
607 * The drive must be selected beforehand.
610 static void ide_cmd (ide_drive_t *drive, u8 cmd, u8 nsect,
611 ide_handler_t *handler)
613 ide_hwif_t *hwif = HWIF(drive);
614 if (IDE_CONTROL_REG)
615 hwif->OUTB(drive->ctl,IDE_CONTROL_REG); /* clear nIEN */
616 SELECT_MASK(drive,0);
617 hwif->OUTB(nsect,IDE_NSECTOR_REG);
618 ide_execute_command(drive, cmd, handler, WAIT_CMD, NULL);
622 * drive_cmd_intr - drive command completion interrupt
623 * @drive: drive the completion interrupt occurred on
625 * drive_cmd_intr() is invoked on completion of a special DRIVE_CMD.
626 * We do any necessary daya reading and then wait for the drive to
627 * go non busy. At that point we may read the error data and complete
628 * the request
631 static ide_startstop_t drive_cmd_intr (ide_drive_t *drive)
633 struct request *rq = HWGROUP(drive)->rq;
634 ide_hwif_t *hwif = HWIF(drive);
635 u8 *args = (u8 *) rq->buffer;
636 u8 stat = hwif->INB(IDE_STATUS_REG);
637 int retries = 10;
639 local_irq_enable();
640 if ((stat & DRQ_STAT) && args && args[3]) {
641 u8 io_32bit = drive->io_32bit;
642 drive->io_32bit = 0;
643 hwif->ata_input_data(drive, &args[4], args[3] * SECTOR_WORDS);
644 drive->io_32bit = io_32bit;
645 while (((stat = hwif->INB(IDE_STATUS_REG)) & BUSY_STAT) && retries--)
646 udelay(100);
649 if (!OK_STAT(stat, READY_STAT, BAD_STAT))
650 return ide_error(drive, "drive_cmd", stat);
651 /* calls ide_end_drive_cmd */
652 ide_end_drive_cmd(drive, stat, hwif->INB(IDE_ERROR_REG));
653 return ide_stopped;
656 static void ide_init_specify_cmd(ide_drive_t *drive, ide_task_t *task)
658 task->tfRegister[IDE_NSECTOR_OFFSET] = drive->sect;
659 task->tfRegister[IDE_SECTOR_OFFSET] = drive->sect;
660 task->tfRegister[IDE_LCYL_OFFSET] = drive->cyl;
661 task->tfRegister[IDE_HCYL_OFFSET] = drive->cyl>>8;
662 task->tfRegister[IDE_SELECT_OFFSET] = ((drive->head-1)|drive->select.all)&0xBF;
663 task->tfRegister[IDE_COMMAND_OFFSET] = WIN_SPECIFY;
665 task->handler = &set_geometry_intr;
668 static void ide_init_restore_cmd(ide_drive_t *drive, ide_task_t *task)
670 task->tfRegister[IDE_NSECTOR_OFFSET] = drive->sect;
671 task->tfRegister[IDE_COMMAND_OFFSET] = WIN_RESTORE;
673 task->handler = &recal_intr;
676 static void ide_init_setmult_cmd(ide_drive_t *drive, ide_task_t *task)
678 task->tfRegister[IDE_NSECTOR_OFFSET] = drive->mult_req;
679 task->tfRegister[IDE_COMMAND_OFFSET] = WIN_SETMULT;
681 task->handler = &set_multmode_intr;
684 static ide_startstop_t ide_disk_special(ide_drive_t *drive)
686 special_t *s = &drive->special;
687 ide_task_t args;
689 memset(&args, 0, sizeof(ide_task_t));
690 args.command_type = IDE_DRIVE_TASK_NO_DATA;
692 if (s->b.set_geometry) {
693 s->b.set_geometry = 0;
694 ide_init_specify_cmd(drive, &args);
695 } else if (s->b.recalibrate) {
696 s->b.recalibrate = 0;
697 ide_init_restore_cmd(drive, &args);
698 } else if (s->b.set_multmode) {
699 s->b.set_multmode = 0;
700 if (drive->mult_req > drive->id->max_multsect)
701 drive->mult_req = drive->id->max_multsect;
702 ide_init_setmult_cmd(drive, &args);
703 } else if (s->all) {
704 int special = s->all;
705 s->all = 0;
706 printk(KERN_ERR "%s: bad special flag: 0x%02x\n", drive->name, special);
707 return ide_stopped;
710 do_rw_taskfile(drive, &args);
712 return ide_started;
716 * do_special - issue some special commands
717 * @drive: drive the command is for
719 * do_special() is used to issue WIN_SPECIFY, WIN_RESTORE, and WIN_SETMULT
720 * commands to a drive. It used to do much more, but has been scaled
721 * back.
724 static ide_startstop_t do_special (ide_drive_t *drive)
726 special_t *s = &drive->special;
728 #ifdef DEBUG
729 printk("%s: do_special: 0x%02x\n", drive->name, s->all);
730 #endif
731 if (s->b.set_tune) {
732 s->b.set_tune = 0;
733 if (HWIF(drive)->tuneproc != NULL)
734 HWIF(drive)->tuneproc(drive, drive->tune_req);
735 return ide_stopped;
736 } else {
737 if (drive->media == ide_disk)
738 return ide_disk_special(drive);
740 s->all = 0;
741 drive->mult_req = 0;
742 return ide_stopped;
746 void ide_map_sg(ide_drive_t *drive, struct request *rq)
748 ide_hwif_t *hwif = drive->hwif;
749 struct scatterlist *sg = hwif->sg_table;
751 if (hwif->sg_mapped) /* needed by ide-scsi */
752 return;
754 if ((rq->flags & REQ_DRIVE_TASKFILE) == 0) {
755 hwif->sg_nents = blk_rq_map_sg(drive->queue, rq, sg);
756 } else {
757 sg_init_one(sg, rq->buffer, rq->nr_sectors * SECTOR_SIZE);
758 hwif->sg_nents = 1;
762 EXPORT_SYMBOL_GPL(ide_map_sg);
764 void ide_init_sg_cmd(ide_drive_t *drive, struct request *rq)
766 ide_hwif_t *hwif = drive->hwif;
768 hwif->nsect = hwif->nleft = rq->nr_sectors;
769 hwif->cursg = hwif->cursg_ofs = 0;
772 EXPORT_SYMBOL_GPL(ide_init_sg_cmd);
775 * execute_drive_command - issue special drive command
776 * @drive: the drive to issue th command on
777 * @rq: the request structure holding the command
779 * execute_drive_cmd() issues a special drive command, usually
780 * initiated by ioctl() from the external hdparm program. The
781 * command can be a drive command, drive task or taskfile
782 * operation. Weirdly you can call it with NULL to wait for
783 * all commands to finish. Don't do this as that is due to change
786 static ide_startstop_t execute_drive_cmd (ide_drive_t *drive,
787 struct request *rq)
789 ide_hwif_t *hwif = HWIF(drive);
790 if (rq->flags & REQ_DRIVE_TASKFILE) {
791 ide_task_t *args = rq->special;
793 if (!args)
794 goto done;
796 hwif->data_phase = args->data_phase;
798 switch (hwif->data_phase) {
799 case TASKFILE_MULTI_OUT:
800 case TASKFILE_OUT:
801 case TASKFILE_MULTI_IN:
802 case TASKFILE_IN:
803 ide_init_sg_cmd(drive, rq);
804 ide_map_sg(drive, rq);
805 default:
806 break;
809 if (args->tf_out_flags.all != 0)
810 return flagged_taskfile(drive, args);
811 return do_rw_taskfile(drive, args);
812 } else if (rq->flags & REQ_DRIVE_TASK) {
813 u8 *args = rq->buffer;
814 u8 sel;
816 if (!args)
817 goto done;
818 #ifdef DEBUG
819 printk("%s: DRIVE_TASK_CMD ", drive->name);
820 printk("cmd=0x%02x ", args[0]);
821 printk("fr=0x%02x ", args[1]);
822 printk("ns=0x%02x ", args[2]);
823 printk("sc=0x%02x ", args[3]);
824 printk("lcyl=0x%02x ", args[4]);
825 printk("hcyl=0x%02x ", args[5]);
826 printk("sel=0x%02x\n", args[6]);
827 #endif
828 hwif->OUTB(args[1], IDE_FEATURE_REG);
829 hwif->OUTB(args[3], IDE_SECTOR_REG);
830 hwif->OUTB(args[4], IDE_LCYL_REG);
831 hwif->OUTB(args[5], IDE_HCYL_REG);
832 sel = (args[6] & ~0x10);
833 if (drive->select.b.unit)
834 sel |= 0x10;
835 hwif->OUTB(sel, IDE_SELECT_REG);
836 ide_cmd(drive, args[0], args[2], &drive_cmd_intr);
837 return ide_started;
838 } else if (rq->flags & REQ_DRIVE_CMD) {
839 u8 *args = rq->buffer;
841 if (!args)
842 goto done;
843 #ifdef DEBUG
844 printk("%s: DRIVE_CMD ", drive->name);
845 printk("cmd=0x%02x ", args[0]);
846 printk("sc=0x%02x ", args[1]);
847 printk("fr=0x%02x ", args[2]);
848 printk("xx=0x%02x\n", args[3]);
849 #endif
850 if (args[0] == WIN_SMART) {
851 hwif->OUTB(0x4f, IDE_LCYL_REG);
852 hwif->OUTB(0xc2, IDE_HCYL_REG);
853 hwif->OUTB(args[2],IDE_FEATURE_REG);
854 hwif->OUTB(args[1],IDE_SECTOR_REG);
855 ide_cmd(drive, args[0], args[3], &drive_cmd_intr);
856 return ide_started;
858 hwif->OUTB(args[2],IDE_FEATURE_REG);
859 ide_cmd(drive, args[0], args[1], &drive_cmd_intr);
860 return ide_started;
863 done:
865 * NULL is actually a valid way of waiting for
866 * all current requests to be flushed from the queue.
868 #ifdef DEBUG
869 printk("%s: DRIVE_CMD (null)\n", drive->name);
870 #endif
871 ide_end_drive_cmd(drive,
872 hwif->INB(IDE_STATUS_REG),
873 hwif->INB(IDE_ERROR_REG));
874 return ide_stopped;
878 * start_request - start of I/O and command issuing for IDE
880 * start_request() initiates handling of a new I/O request. It
881 * accepts commands and I/O (read/write) requests. It also does
882 * the final remapping for weird stuff like EZDrive. Once
883 * device mapper can work sector level the EZDrive stuff can go away
885 * FIXME: this function needs a rename
888 static ide_startstop_t start_request (ide_drive_t *drive, struct request *rq)
890 ide_startstop_t startstop;
891 sector_t block;
893 BUG_ON(!(rq->flags & REQ_STARTED));
895 #ifdef DEBUG
896 printk("%s: start_request: current=0x%08lx\n",
897 HWIF(drive)->name, (unsigned long) rq);
898 #endif
900 /* bail early if we've exceeded max_failures */
901 if (drive->max_failures && (drive->failures > drive->max_failures)) {
902 goto kill_rq;
905 block = rq->sector;
906 if (blk_fs_request(rq) &&
907 (drive->media == ide_disk || drive->media == ide_floppy)) {
908 block += drive->sect0;
910 /* Yecch - this will shift the entire interval,
911 possibly killing some innocent following sector */
912 if (block == 0 && drive->remap_0_to_1 == 1)
913 block = 1; /* redirect MBR access to EZ-Drive partn table */
915 if (blk_pm_suspend_request(rq) &&
916 rq->pm->pm_step == ide_pm_state_start_suspend)
917 /* Mark drive blocked when starting the suspend sequence. */
918 drive->blocked = 1;
919 else if (blk_pm_resume_request(rq) &&
920 rq->pm->pm_step == ide_pm_state_start_resume) {
922 * The first thing we do on wakeup is to wait for BSY bit to
923 * go away (with a looong timeout) as a drive on this hwif may
924 * just be POSTing itself.
925 * We do that before even selecting as the "other" device on
926 * the bus may be broken enough to walk on our toes at this
927 * point.
929 int rc;
930 #ifdef DEBUG_PM
931 printk("%s: Wakeup request inited, waiting for !BSY...\n", drive->name);
932 #endif
933 rc = ide_wait_not_busy(HWIF(drive), 35000);
934 if (rc)
935 printk(KERN_WARNING "%s: bus not ready on wakeup\n", drive->name);
936 SELECT_DRIVE(drive);
937 HWIF(drive)->OUTB(8, HWIF(drive)->io_ports[IDE_CONTROL_OFFSET]);
938 rc = ide_wait_not_busy(HWIF(drive), 10000);
939 if (rc)
940 printk(KERN_WARNING "%s: drive not ready on wakeup\n", drive->name);
943 SELECT_DRIVE(drive);
944 if (ide_wait_stat(&startstop, drive, drive->ready_stat, BUSY_STAT|DRQ_STAT, WAIT_READY)) {
945 printk(KERN_ERR "%s: drive not ready for command\n", drive->name);
946 return startstop;
948 if (!drive->special.all) {
949 ide_driver_t *drv;
951 if (rq->flags & (REQ_DRIVE_CMD | REQ_DRIVE_TASK))
952 return execute_drive_cmd(drive, rq);
953 else if (rq->flags & REQ_DRIVE_TASKFILE)
954 return execute_drive_cmd(drive, rq);
955 else if (blk_pm_request(rq)) {
956 #ifdef DEBUG_PM
957 printk("%s: start_power_step(step: %d)\n",
958 drive->name, rq->pm->pm_step);
959 #endif
960 startstop = ide_start_power_step(drive, rq);
961 if (startstop == ide_stopped &&
962 rq->pm->pm_step == ide_pm_state_completed)
963 ide_complete_pm_request(drive, rq);
964 return startstop;
967 drv = *(ide_driver_t **)rq->rq_disk->private_data;
968 return drv->do_request(drive, rq, block);
970 return do_special(drive);
971 kill_rq:
972 ide_kill_rq(drive, rq);
973 return ide_stopped;
977 * ide_stall_queue - pause an IDE device
978 * @drive: drive to stall
979 * @timeout: time to stall for (jiffies)
981 * ide_stall_queue() can be used by a drive to give excess bandwidth back
982 * to the hwgroup by sleeping for timeout jiffies.
985 void ide_stall_queue (ide_drive_t *drive, unsigned long timeout)
987 if (timeout > WAIT_WORSTCASE)
988 timeout = WAIT_WORSTCASE;
989 drive->sleep = timeout + jiffies;
990 drive->sleeping = 1;
993 EXPORT_SYMBOL(ide_stall_queue);
995 #define WAKEUP(drive) ((drive)->service_start + 2 * (drive)->service_time)
998 * choose_drive - select a drive to service
999 * @hwgroup: hardware group to select on
1001 * choose_drive() selects the next drive which will be serviced.
1002 * This is necessary because the IDE layer can't issue commands
1003 * to both drives on the same cable, unlike SCSI.
1006 static inline ide_drive_t *choose_drive (ide_hwgroup_t *hwgroup)
1008 ide_drive_t *drive, *best;
1010 repeat:
1011 best = NULL;
1012 drive = hwgroup->drive;
1015 * drive is doing pre-flush, ordered write, post-flush sequence. even
1016 * though that is 3 requests, it must be seen as a single transaction.
1017 * we must not preempt this drive until that is complete
1019 if (blk_queue_flushing(drive->queue)) {
1021 * small race where queue could get replugged during
1022 * the 3-request flush cycle, just yank the plug since
1023 * we want it to finish asap
1025 blk_remove_plug(drive->queue);
1026 return drive;
1029 do {
1030 if ((!drive->sleeping || time_after_eq(jiffies, drive->sleep))
1031 && !elv_queue_empty(drive->queue)) {
1032 if (!best
1033 || (drive->sleeping && (!best->sleeping || time_before(drive->sleep, best->sleep)))
1034 || (!best->sleeping && time_before(WAKEUP(drive), WAKEUP(best))))
1036 if (!blk_queue_plugged(drive->queue))
1037 best = drive;
1040 } while ((drive = drive->next) != hwgroup->drive);
1041 if (best && best->nice1 && !best->sleeping && best != hwgroup->drive && best->service_time > WAIT_MIN_SLEEP) {
1042 long t = (signed long)(WAKEUP(best) - jiffies);
1043 if (t >= WAIT_MIN_SLEEP) {
1045 * We *may* have some time to spare, but first let's see if
1046 * someone can potentially benefit from our nice mood today..
1048 drive = best->next;
1049 do {
1050 if (!drive->sleeping
1051 && time_before(jiffies - best->service_time, WAKEUP(drive))
1052 && time_before(WAKEUP(drive), jiffies + t))
1054 ide_stall_queue(best, min_t(long, t, 10 * WAIT_MIN_SLEEP));
1055 goto repeat;
1057 } while ((drive = drive->next) != best);
1060 return best;
1064 * Issue a new request to a drive from hwgroup
1065 * Caller must have already done spin_lock_irqsave(&ide_lock, ..);
1067 * A hwgroup is a serialized group of IDE interfaces. Usually there is
1068 * exactly one hwif (interface) per hwgroup, but buggy controllers (eg. CMD640)
1069 * may have both interfaces in a single hwgroup to "serialize" access.
1070 * Or possibly multiple ISA interfaces can share a common IRQ by being grouped
1071 * together into one hwgroup for serialized access.
1073 * Note also that several hwgroups can end up sharing a single IRQ,
1074 * possibly along with many other devices. This is especially common in
1075 * PCI-based systems with off-board IDE controller cards.
1077 * The IDE driver uses the single global ide_lock spinlock to protect
1078 * access to the request queues, and to protect the hwgroup->busy flag.
1080 * The first thread into the driver for a particular hwgroup sets the
1081 * hwgroup->busy flag to indicate that this hwgroup is now active,
1082 * and then initiates processing of the top request from the request queue.
1084 * Other threads attempting entry notice the busy setting, and will simply
1085 * queue their new requests and exit immediately. Note that hwgroup->busy
1086 * remains set even when the driver is merely awaiting the next interrupt.
1087 * Thus, the meaning is "this hwgroup is busy processing a request".
1089 * When processing of a request completes, the completing thread or IRQ-handler
1090 * will start the next request from the queue. If no more work remains,
1091 * the driver will clear the hwgroup->busy flag and exit.
1093 * The ide_lock (spinlock) is used to protect all access to the
1094 * hwgroup->busy flag, but is otherwise not needed for most processing in
1095 * the driver. This makes the driver much more friendlier to shared IRQs
1096 * than previous designs, while remaining 100% (?) SMP safe and capable.
1098 static void ide_do_request (ide_hwgroup_t *hwgroup, int masked_irq)
1100 ide_drive_t *drive;
1101 ide_hwif_t *hwif;
1102 struct request *rq;
1103 ide_startstop_t startstop;
1105 /* for atari only: POSSIBLY BROKEN HERE(?) */
1106 ide_get_lock(ide_intr, hwgroup);
1108 /* caller must own ide_lock */
1109 BUG_ON(!irqs_disabled());
1111 while (!hwgroup->busy) {
1112 hwgroup->busy = 1;
1113 drive = choose_drive(hwgroup);
1114 if (drive == NULL) {
1115 int sleeping = 0;
1116 unsigned long sleep = 0; /* shut up, gcc */
1117 hwgroup->rq = NULL;
1118 drive = hwgroup->drive;
1119 do {
1120 if (drive->sleeping && (!sleeping || time_before(drive->sleep, sleep))) {
1121 sleeping = 1;
1122 sleep = drive->sleep;
1124 } while ((drive = drive->next) != hwgroup->drive);
1125 if (sleeping) {
1127 * Take a short snooze, and then wake up this hwgroup again.
1128 * This gives other hwgroups on the same a chance to
1129 * play fairly with us, just in case there are big differences
1130 * in relative throughputs.. don't want to hog the cpu too much.
1132 if (time_before(sleep, jiffies + WAIT_MIN_SLEEP))
1133 sleep = jiffies + WAIT_MIN_SLEEP;
1134 #if 1
1135 if (timer_pending(&hwgroup->timer))
1136 printk(KERN_CRIT "ide_set_handler: timer already active\n");
1137 #endif
1138 /* so that ide_timer_expiry knows what to do */
1139 hwgroup->sleeping = 1;
1140 mod_timer(&hwgroup->timer, sleep);
1141 /* we purposely leave hwgroup->busy==1
1142 * while sleeping */
1143 } else {
1144 /* Ugly, but how can we sleep for the lock
1145 * otherwise? perhaps from tq_disk?
1148 /* for atari only */
1149 ide_release_lock();
1150 hwgroup->busy = 0;
1153 /* no more work for this hwgroup (for now) */
1154 return;
1156 hwif = HWIF(drive);
1157 if (hwgroup->hwif->sharing_irq &&
1158 hwif != hwgroup->hwif &&
1159 hwif->io_ports[IDE_CONTROL_OFFSET]) {
1160 /* set nIEN for previous hwif */
1161 SELECT_INTERRUPT(drive);
1163 hwgroup->hwif = hwif;
1164 hwgroup->drive = drive;
1165 drive->sleeping = 0;
1166 drive->service_start = jiffies;
1168 if (blk_queue_plugged(drive->queue)) {
1169 printk(KERN_ERR "ide: huh? queue was plugged!\n");
1170 break;
1174 * we know that the queue isn't empty, but this can happen
1175 * if the q->prep_rq_fn() decides to kill a request
1177 rq = elv_next_request(drive->queue);
1178 if (!rq) {
1179 hwgroup->busy = 0;
1180 break;
1184 * Sanity: don't accept a request that isn't a PM request
1185 * if we are currently power managed. This is very important as
1186 * blk_stop_queue() doesn't prevent the elv_next_request()
1187 * above to return us whatever is in the queue. Since we call
1188 * ide_do_request() ourselves, we end up taking requests while
1189 * the queue is blocked...
1191 * We let requests forced at head of queue with ide-preempt
1192 * though. I hope that doesn't happen too much, hopefully not
1193 * unless the subdriver triggers such a thing in its own PM
1194 * state machine.
1196 if (drive->blocked && !blk_pm_request(rq) && !(rq->flags & REQ_PREEMPT)) {
1197 /* We clear busy, there should be no pending ATA command at this point. */
1198 hwgroup->busy = 0;
1199 break;
1202 hwgroup->rq = rq;
1205 * Some systems have trouble with IDE IRQs arriving while
1206 * the driver is still setting things up. So, here we disable
1207 * the IRQ used by this interface while the request is being started.
1208 * This may look bad at first, but pretty much the same thing
1209 * happens anyway when any interrupt comes in, IDE or otherwise
1210 * -- the kernel masks the IRQ while it is being handled.
1212 if (masked_irq != IDE_NO_IRQ && hwif->irq != masked_irq)
1213 disable_irq_nosync(hwif->irq);
1214 spin_unlock(&ide_lock);
1215 local_irq_enable();
1216 /* allow other IRQs while we start this request */
1217 startstop = start_request(drive, rq);
1218 spin_lock_irq(&ide_lock);
1219 if (masked_irq != IDE_NO_IRQ && hwif->irq != masked_irq)
1220 enable_irq(hwif->irq);
1221 if (startstop == ide_stopped)
1222 hwgroup->busy = 0;
1227 * Passes the stuff to ide_do_request
1229 void do_ide_request(request_queue_t *q)
1231 ide_drive_t *drive = q->queuedata;
1233 ide_do_request(HWGROUP(drive), IDE_NO_IRQ);
1237 * un-busy the hwgroup etc, and clear any pending DMA status. we want to
1238 * retry the current request in pio mode instead of risking tossing it
1239 * all away
1241 static ide_startstop_t ide_dma_timeout_retry(ide_drive_t *drive, int error)
1243 ide_hwif_t *hwif = HWIF(drive);
1244 struct request *rq;
1245 ide_startstop_t ret = ide_stopped;
1248 * end current dma transaction
1251 if (error < 0) {
1252 printk(KERN_WARNING "%s: DMA timeout error\n", drive->name);
1253 (void)HWIF(drive)->ide_dma_end(drive);
1254 ret = ide_error(drive, "dma timeout error",
1255 hwif->INB(IDE_STATUS_REG));
1256 } else {
1257 printk(KERN_WARNING "%s: DMA timeout retry\n", drive->name);
1258 (void) hwif->ide_dma_timeout(drive);
1262 * disable dma for now, but remember that we did so because of
1263 * a timeout -- we'll reenable after we finish this next request
1264 * (or rather the first chunk of it) in pio.
1266 drive->retry_pio++;
1267 drive->state = DMA_PIO_RETRY;
1268 (void) hwif->ide_dma_off_quietly(drive);
1271 * un-busy drive etc (hwgroup->busy is cleared on return) and
1272 * make sure request is sane
1274 rq = HWGROUP(drive)->rq;
1275 HWGROUP(drive)->rq = NULL;
1277 rq->errors = 0;
1279 if (!rq->bio)
1280 goto out;
1282 rq->sector = rq->bio->bi_sector;
1283 rq->current_nr_sectors = bio_iovec(rq->bio)->bv_len >> 9;
1284 rq->hard_cur_sectors = rq->current_nr_sectors;
1285 rq->buffer = bio_data(rq->bio);
1286 out:
1287 return ret;
1291 * ide_timer_expiry - handle lack of an IDE interrupt
1292 * @data: timer callback magic (hwgroup)
1294 * An IDE command has timed out before the expected drive return
1295 * occurred. At this point we attempt to clean up the current
1296 * mess. If the current handler includes an expiry handler then
1297 * we invoke the expiry handler, and providing it is happy the
1298 * work is done. If that fails we apply generic recovery rules
1299 * invoking the handler and checking the drive DMA status. We
1300 * have an excessively incestuous relationship with the DMA
1301 * logic that wants cleaning up.
1304 void ide_timer_expiry (unsigned long data)
1306 ide_hwgroup_t *hwgroup = (ide_hwgroup_t *) data;
1307 ide_handler_t *handler;
1308 ide_expiry_t *expiry;
1309 unsigned long flags;
1310 unsigned long wait = -1;
1312 spin_lock_irqsave(&ide_lock, flags);
1314 if ((handler = hwgroup->handler) == NULL) {
1316 * Either a marginal timeout occurred
1317 * (got the interrupt just as timer expired),
1318 * or we were "sleeping" to give other devices a chance.
1319 * Either way, we don't really want to complain about anything.
1321 if (hwgroup->sleeping) {
1322 hwgroup->sleeping = 0;
1323 hwgroup->busy = 0;
1325 } else {
1326 ide_drive_t *drive = hwgroup->drive;
1327 if (!drive) {
1328 printk(KERN_ERR "ide_timer_expiry: hwgroup->drive was NULL\n");
1329 hwgroup->handler = NULL;
1330 } else {
1331 ide_hwif_t *hwif;
1332 ide_startstop_t startstop = ide_stopped;
1333 if (!hwgroup->busy) {
1334 hwgroup->busy = 1; /* paranoia */
1335 printk(KERN_ERR "%s: ide_timer_expiry: hwgroup->busy was 0 ??\n", drive->name);
1337 if ((expiry = hwgroup->expiry) != NULL) {
1338 /* continue */
1339 if ((wait = expiry(drive)) > 0) {
1340 /* reset timer */
1341 hwgroup->timer.expires = jiffies + wait;
1342 add_timer(&hwgroup->timer);
1343 spin_unlock_irqrestore(&ide_lock, flags);
1344 return;
1347 hwgroup->handler = NULL;
1349 * We need to simulate a real interrupt when invoking
1350 * the handler() function, which means we need to
1351 * globally mask the specific IRQ:
1353 spin_unlock(&ide_lock);
1354 hwif = HWIF(drive);
1355 #if DISABLE_IRQ_NOSYNC
1356 disable_irq_nosync(hwif->irq);
1357 #else
1358 /* disable_irq_nosync ?? */
1359 disable_irq(hwif->irq);
1360 #endif /* DISABLE_IRQ_NOSYNC */
1361 /* local CPU only,
1362 * as if we were handling an interrupt */
1363 local_irq_disable();
1364 if (hwgroup->polling) {
1365 startstop = handler(drive);
1366 } else if (drive_is_ready(drive)) {
1367 if (drive->waiting_for_dma)
1368 (void) hwgroup->hwif->ide_dma_lostirq(drive);
1369 (void)ide_ack_intr(hwif);
1370 printk(KERN_WARNING "%s: lost interrupt\n", drive->name);
1371 startstop = handler(drive);
1372 } else {
1373 if (drive->waiting_for_dma) {
1374 startstop = ide_dma_timeout_retry(drive, wait);
1375 } else
1376 startstop =
1377 ide_error(drive, "irq timeout", hwif->INB(IDE_STATUS_REG));
1379 drive->service_time = jiffies - drive->service_start;
1380 spin_lock_irq(&ide_lock);
1381 enable_irq(hwif->irq);
1382 if (startstop == ide_stopped)
1383 hwgroup->busy = 0;
1386 ide_do_request(hwgroup, IDE_NO_IRQ);
1387 spin_unlock_irqrestore(&ide_lock, flags);
1391 * unexpected_intr - handle an unexpected IDE interrupt
1392 * @irq: interrupt line
1393 * @hwgroup: hwgroup being processed
1395 * There's nothing really useful we can do with an unexpected interrupt,
1396 * other than reading the status register (to clear it), and logging it.
1397 * There should be no way that an irq can happen before we're ready for it,
1398 * so we needn't worry much about losing an "important" interrupt here.
1400 * On laptops (and "green" PCs), an unexpected interrupt occurs whenever
1401 * the drive enters "idle", "standby", or "sleep" mode, so if the status
1402 * looks "good", we just ignore the interrupt completely.
1404 * This routine assumes __cli() is in effect when called.
1406 * If an unexpected interrupt happens on irq15 while we are handling irq14
1407 * and if the two interfaces are "serialized" (CMD640), then it looks like
1408 * we could screw up by interfering with a new request being set up for
1409 * irq15.
1411 * In reality, this is a non-issue. The new command is not sent unless
1412 * the drive is ready to accept one, in which case we know the drive is
1413 * not trying to interrupt us. And ide_set_handler() is always invoked
1414 * before completing the issuance of any new drive command, so we will not
1415 * be accidentally invoked as a result of any valid command completion
1416 * interrupt.
1418 * Note that we must walk the entire hwgroup here. We know which hwif
1419 * is doing the current command, but we don't know which hwif burped
1420 * mysteriously.
1423 static void unexpected_intr (int irq, ide_hwgroup_t *hwgroup)
1425 u8 stat;
1426 ide_hwif_t *hwif = hwgroup->hwif;
1429 * handle the unexpected interrupt
1431 do {
1432 if (hwif->irq == irq) {
1433 stat = hwif->INB(hwif->io_ports[IDE_STATUS_OFFSET]);
1434 if (!OK_STAT(stat, READY_STAT, BAD_STAT)) {
1435 /* Try to not flood the console with msgs */
1436 static unsigned long last_msgtime, count;
1437 ++count;
1438 if (time_after(jiffies, last_msgtime + HZ)) {
1439 last_msgtime = jiffies;
1440 printk(KERN_ERR "%s%s: unexpected interrupt, "
1441 "status=0x%02x, count=%ld\n",
1442 hwif->name,
1443 (hwif->next==hwgroup->hwif) ? "" : "(?)", stat, count);
1447 } while ((hwif = hwif->next) != hwgroup->hwif);
1451 * ide_intr - default IDE interrupt handler
1452 * @irq: interrupt number
1453 * @dev_id: hwif group
1454 * @regs: unused weirdness from the kernel irq layer
1456 * This is the default IRQ handler for the IDE layer. You should
1457 * not need to override it. If you do be aware it is subtle in
1458 * places
1460 * hwgroup->hwif is the interface in the group currently performing
1461 * a command. hwgroup->drive is the drive and hwgroup->handler is
1462 * the IRQ handler to call. As we issue a command the handlers
1463 * step through multiple states, reassigning the handler to the
1464 * next step in the process. Unlike a smart SCSI controller IDE
1465 * expects the main processor to sequence the various transfer
1466 * stages. We also manage a poll timer to catch up with most
1467 * timeout situations. There are still a few where the handlers
1468 * don't ever decide to give up.
1470 * The handler eventually returns ide_stopped to indicate the
1471 * request completed. At this point we issue the next request
1472 * on the hwgroup and the process begins again.
1475 irqreturn_t ide_intr (int irq, void *dev_id, struct pt_regs *regs)
1477 unsigned long flags;
1478 ide_hwgroup_t *hwgroup = (ide_hwgroup_t *)dev_id;
1479 ide_hwif_t *hwif;
1480 ide_drive_t *drive;
1481 ide_handler_t *handler;
1482 ide_startstop_t startstop;
1484 spin_lock_irqsave(&ide_lock, flags);
1485 hwif = hwgroup->hwif;
1487 if (!ide_ack_intr(hwif)) {
1488 spin_unlock_irqrestore(&ide_lock, flags);
1489 return IRQ_NONE;
1492 if ((handler = hwgroup->handler) == NULL || hwgroup->polling) {
1494 * Not expecting an interrupt from this drive.
1495 * That means this could be:
1496 * (1) an interrupt from another PCI device
1497 * sharing the same PCI INT# as us.
1498 * or (2) a drive just entered sleep or standby mode,
1499 * and is interrupting to let us know.
1500 * or (3) a spurious interrupt of unknown origin.
1502 * For PCI, we cannot tell the difference,
1503 * so in that case we just ignore it and hope it goes away.
1505 * FIXME: unexpected_intr should be hwif-> then we can
1506 * remove all the ifdef PCI crap
1508 #ifdef CONFIG_BLK_DEV_IDEPCI
1509 if (hwif->pci_dev && !hwif->pci_dev->vendor)
1510 #endif /* CONFIG_BLK_DEV_IDEPCI */
1513 * Probably not a shared PCI interrupt,
1514 * so we can safely try to do something about it:
1516 unexpected_intr(irq, hwgroup);
1517 #ifdef CONFIG_BLK_DEV_IDEPCI
1518 } else {
1520 * Whack the status register, just in case
1521 * we have a leftover pending IRQ.
1523 (void) hwif->INB(hwif->io_ports[IDE_STATUS_OFFSET]);
1524 #endif /* CONFIG_BLK_DEV_IDEPCI */
1526 spin_unlock_irqrestore(&ide_lock, flags);
1527 return IRQ_NONE;
1529 drive = hwgroup->drive;
1530 if (!drive) {
1532 * This should NEVER happen, and there isn't much
1533 * we could do about it here.
1535 * [Note - this can occur if the drive is hot unplugged]
1537 spin_unlock_irqrestore(&ide_lock, flags);
1538 return IRQ_HANDLED;
1540 if (!drive_is_ready(drive)) {
1542 * This happens regularly when we share a PCI IRQ with
1543 * another device. Unfortunately, it can also happen
1544 * with some buggy drives that trigger the IRQ before
1545 * their status register is up to date. Hopefully we have
1546 * enough advance overhead that the latter isn't a problem.
1548 spin_unlock_irqrestore(&ide_lock, flags);
1549 return IRQ_NONE;
1551 if (!hwgroup->busy) {
1552 hwgroup->busy = 1; /* paranoia */
1553 printk(KERN_ERR "%s: ide_intr: hwgroup->busy was 0 ??\n", drive->name);
1555 hwgroup->handler = NULL;
1556 del_timer(&hwgroup->timer);
1557 spin_unlock(&ide_lock);
1559 if (drive->unmask)
1560 local_irq_enable();
1561 /* service this interrupt, may set handler for next interrupt */
1562 startstop = handler(drive);
1563 spin_lock_irq(&ide_lock);
1566 * Note that handler() may have set things up for another
1567 * interrupt to occur soon, but it cannot happen until
1568 * we exit from this routine, because it will be the
1569 * same irq as is currently being serviced here, and Linux
1570 * won't allow another of the same (on any CPU) until we return.
1572 drive->service_time = jiffies - drive->service_start;
1573 if (startstop == ide_stopped) {
1574 if (hwgroup->handler == NULL) { /* paranoia */
1575 hwgroup->busy = 0;
1576 ide_do_request(hwgroup, hwif->irq);
1577 } else {
1578 printk(KERN_ERR "%s: ide_intr: huh? expected NULL handler "
1579 "on exit\n", drive->name);
1582 spin_unlock_irqrestore(&ide_lock, flags);
1583 return IRQ_HANDLED;
1587 * ide_init_drive_cmd - initialize a drive command request
1588 * @rq: request object
1590 * Initialize a request before we fill it in and send it down to
1591 * ide_do_drive_cmd. Commands must be set up by this function. Right
1592 * now it doesn't do a lot, but if that changes abusers will have a
1593 * nasty suprise.
1596 void ide_init_drive_cmd (struct request *rq)
1598 memset(rq, 0, sizeof(*rq));
1599 rq->flags = REQ_DRIVE_CMD;
1600 rq->ref_count = 1;
1603 EXPORT_SYMBOL(ide_init_drive_cmd);
1606 * ide_do_drive_cmd - issue IDE special command
1607 * @drive: device to issue command
1608 * @rq: request to issue
1609 * @action: action for processing
1611 * This function issues a special IDE device request
1612 * onto the request queue.
1614 * If action is ide_wait, then the rq is queued at the end of the
1615 * request queue, and the function sleeps until it has been processed.
1616 * This is for use when invoked from an ioctl handler.
1618 * If action is ide_preempt, then the rq is queued at the head of
1619 * the request queue, displacing the currently-being-processed
1620 * request and this function returns immediately without waiting
1621 * for the new rq to be completed. This is VERY DANGEROUS, and is
1622 * intended for careful use by the ATAPI tape/cdrom driver code.
1624 * If action is ide_next, then the rq is queued immediately after
1625 * the currently-being-processed-request (if any), and the function
1626 * returns without waiting for the new rq to be completed. As above,
1627 * This is VERY DANGEROUS, and is intended for careful use by the
1628 * ATAPI tape/cdrom driver code.
1630 * If action is ide_end, then the rq is queued at the end of the
1631 * request queue, and the function returns immediately without waiting
1632 * for the new rq to be completed. This is again intended for careful
1633 * use by the ATAPI tape/cdrom driver code.
1636 int ide_do_drive_cmd (ide_drive_t *drive, struct request *rq, ide_action_t action)
1638 unsigned long flags;
1639 ide_hwgroup_t *hwgroup = HWGROUP(drive);
1640 DECLARE_COMPLETION(wait);
1641 int where = ELEVATOR_INSERT_BACK, err;
1642 int must_wait = (action == ide_wait || action == ide_head_wait);
1644 rq->errors = 0;
1645 rq->rq_status = RQ_ACTIVE;
1648 * we need to hold an extra reference to request for safe inspection
1649 * after completion
1651 if (must_wait) {
1652 rq->ref_count++;
1653 rq->waiting = &wait;
1654 rq->end_io = blk_end_sync_rq;
1657 spin_lock_irqsave(&ide_lock, flags);
1658 if (action == ide_preempt)
1659 hwgroup->rq = NULL;
1660 if (action == ide_preempt || action == ide_head_wait) {
1661 where = ELEVATOR_INSERT_FRONT;
1662 rq->flags |= REQ_PREEMPT;
1664 __elv_add_request(drive->queue, rq, where, 0);
1665 ide_do_request(hwgroup, IDE_NO_IRQ);
1666 spin_unlock_irqrestore(&ide_lock, flags);
1668 err = 0;
1669 if (must_wait) {
1670 wait_for_completion(&wait);
1671 rq->waiting = NULL;
1672 if (rq->errors)
1673 err = -EIO;
1675 blk_put_request(rq);
1678 return err;
1681 EXPORT_SYMBOL(ide_do_drive_cmd);