[SPARC64]: Add needed pm_power_off symbol.
[linux-2.6/libata-dev.git] / drivers / ide / ide-io.c
blobdea2d4dcc6981848222eb97ea0b47478dcc9df4c
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 void ide_softirq_done(struct request *rq)
60 request_queue_t *q = rq->q;
62 add_disk_randomness(rq->rq_disk);
63 end_that_request_chunk(rq, rq->errors, rq->data_len);
65 spin_lock_irq(q->queue_lock);
66 end_that_request_last(rq, rq->errors);
67 spin_unlock_irq(q->queue_lock);
70 int __ide_end_request(ide_drive_t *drive, struct request *rq, int uptodate,
71 int nr_sectors)
73 unsigned int nbytes;
74 int ret = 1;
76 BUG_ON(!(rq->flags & REQ_STARTED));
79 * if failfast is set on a request, override number of sectors and
80 * complete the whole request right now
82 if (blk_noretry_request(rq) && end_io_error(uptodate))
83 nr_sectors = rq->hard_nr_sectors;
85 if (!blk_fs_request(rq) && end_io_error(uptodate) && !rq->errors)
86 rq->errors = -EIO;
89 * decide whether to reenable DMA -- 3 is a random magic for now,
90 * if we DMA timeout more than 3 times, just stay in PIO
92 if (drive->state == DMA_PIO_RETRY && drive->retry_pio <= 3) {
93 drive->state = 0;
94 HWGROUP(drive)->hwif->ide_dma_on(drive);
98 * For partial completions (or non fs/pc requests), use the regular
99 * direct completion path.
101 nbytes = nr_sectors << 9;
102 if (rq_all_done(rq, nbytes)) {
103 rq->errors = uptodate;
104 rq->data_len = nbytes;
105 blkdev_dequeue_request(rq);
106 HWGROUP(drive)->rq = NULL;
107 blk_complete_request(rq);
108 ret = 0;
109 } else {
110 if (!end_that_request_first(rq, uptodate, nr_sectors)) {
111 add_disk_randomness(rq->rq_disk);
112 blkdev_dequeue_request(rq);
113 HWGROUP(drive)->rq = NULL;
114 end_that_request_last(rq, uptodate);
115 ret = 0;
119 return ret;
121 EXPORT_SYMBOL(__ide_end_request);
124 * ide_end_request - complete an IDE I/O
125 * @drive: IDE device for the I/O
126 * @uptodate:
127 * @nr_sectors: number of sectors completed
129 * This is our end_request wrapper function. We complete the I/O
130 * update random number input and dequeue the request, which if
131 * it was tagged may be out of order.
134 int ide_end_request (ide_drive_t *drive, int uptodate, int nr_sectors)
136 struct request *rq;
137 unsigned long flags;
138 int ret = 1;
141 * room for locking improvements here, the calls below don't
142 * need the queue lock held at all
144 spin_lock_irqsave(&ide_lock, flags);
145 rq = HWGROUP(drive)->rq;
147 if (!nr_sectors)
148 nr_sectors = rq->hard_cur_sectors;
150 ret = __ide_end_request(drive, rq, uptodate, nr_sectors);
152 spin_unlock_irqrestore(&ide_lock, flags);
153 return ret;
155 EXPORT_SYMBOL(ide_end_request);
158 * Power Management state machine. This one is rather trivial for now,
159 * we should probably add more, like switching back to PIO on suspend
160 * to help some BIOSes, re-do the door locking on resume, etc...
163 enum {
164 ide_pm_flush_cache = ide_pm_state_start_suspend,
165 idedisk_pm_standby,
167 idedisk_pm_idle = ide_pm_state_start_resume,
168 ide_pm_restore_dma,
171 static void ide_complete_power_step(ide_drive_t *drive, struct request *rq, u8 stat, u8 error)
173 if (drive->media != ide_disk)
174 return;
176 switch (rq->pm->pm_step) {
177 case ide_pm_flush_cache: /* Suspend step 1 (flush cache) complete */
178 if (rq->pm->pm_state == PM_EVENT_FREEZE)
179 rq->pm->pm_step = ide_pm_state_completed;
180 else
181 rq->pm->pm_step = idedisk_pm_standby;
182 break;
183 case idedisk_pm_standby: /* Suspend step 2 (standby) complete */
184 rq->pm->pm_step = ide_pm_state_completed;
185 break;
186 case idedisk_pm_idle: /* Resume step 1 (idle) complete */
187 rq->pm->pm_step = ide_pm_restore_dma;
188 break;
192 static ide_startstop_t ide_start_power_step(ide_drive_t *drive, struct request *rq)
194 ide_task_t *args = rq->special;
196 memset(args, 0, sizeof(*args));
198 if (drive->media != ide_disk) {
199 /* skip idedisk_pm_idle for ATAPI devices */
200 if (rq->pm->pm_step == idedisk_pm_idle)
201 rq->pm->pm_step = ide_pm_restore_dma;
204 switch (rq->pm->pm_step) {
205 case ide_pm_flush_cache: /* Suspend step 1 (flush cache) */
206 if (drive->media != ide_disk)
207 break;
208 /* Not supported? Switch to next step now. */
209 if (!drive->wcache || !ide_id_has_flush_cache(drive->id)) {
210 ide_complete_power_step(drive, rq, 0, 0);
211 return ide_stopped;
213 if (ide_id_has_flush_cache_ext(drive->id))
214 args->tfRegister[IDE_COMMAND_OFFSET] = WIN_FLUSH_CACHE_EXT;
215 else
216 args->tfRegister[IDE_COMMAND_OFFSET] = WIN_FLUSH_CACHE;
217 args->command_type = IDE_DRIVE_TASK_NO_DATA;
218 args->handler = &task_no_data_intr;
219 return do_rw_taskfile(drive, args);
221 case idedisk_pm_standby: /* Suspend step 2 (standby) */
222 args->tfRegister[IDE_COMMAND_OFFSET] = WIN_STANDBYNOW1;
223 args->command_type = IDE_DRIVE_TASK_NO_DATA;
224 args->handler = &task_no_data_intr;
225 return do_rw_taskfile(drive, args);
227 case idedisk_pm_idle: /* Resume step 1 (idle) */
228 args->tfRegister[IDE_COMMAND_OFFSET] = WIN_IDLEIMMEDIATE;
229 args->command_type = IDE_DRIVE_TASK_NO_DATA;
230 args->handler = task_no_data_intr;
231 return do_rw_taskfile(drive, args);
233 case ide_pm_restore_dma: /* Resume step 2 (restore DMA) */
235 * Right now, all we do is call hwif->ide_dma_check(drive),
236 * we could be smarter and check for current xfer_speed
237 * in struct drive etc...
239 if ((drive->id->capability & 1) == 0)
240 break;
241 if (drive->hwif->ide_dma_check == NULL)
242 break;
243 drive->hwif->ide_dma_check(drive);
244 break;
246 rq->pm->pm_step = ide_pm_state_completed;
247 return ide_stopped;
251 * ide_complete_pm_request - end the current Power Management request
252 * @drive: target drive
253 * @rq: request
255 * This function cleans up the current PM request and stops the queue
256 * if necessary.
258 static void ide_complete_pm_request (ide_drive_t *drive, struct request *rq)
260 unsigned long flags;
262 #ifdef DEBUG_PM
263 printk("%s: completing PM request, %s\n", drive->name,
264 blk_pm_suspend_request(rq) ? "suspend" : "resume");
265 #endif
266 spin_lock_irqsave(&ide_lock, flags);
267 if (blk_pm_suspend_request(rq)) {
268 blk_stop_queue(drive->queue);
269 } else {
270 drive->blocked = 0;
271 blk_start_queue(drive->queue);
273 blkdev_dequeue_request(rq);
274 HWGROUP(drive)->rq = NULL;
275 end_that_request_last(rq, 1);
276 spin_unlock_irqrestore(&ide_lock, flags);
280 * FIXME: probably move this somewhere else, name is bad too :)
282 u64 ide_get_error_location(ide_drive_t *drive, char *args)
284 u32 high, low;
285 u8 hcyl, lcyl, sect;
286 u64 sector;
288 high = 0;
289 hcyl = args[5];
290 lcyl = args[4];
291 sect = args[3];
293 if (ide_id_has_flush_cache_ext(drive->id)) {
294 low = (hcyl << 16) | (lcyl << 8) | sect;
295 HWIF(drive)->OUTB(drive->ctl|0x80, IDE_CONTROL_REG);
296 high = ide_read_24(drive);
297 } else {
298 u8 cur = HWIF(drive)->INB(IDE_SELECT_REG);
299 if (cur & 0x40) {
300 high = cur & 0xf;
301 low = (hcyl << 16) | (lcyl << 8) | sect;
302 } else {
303 low = hcyl * drive->head * drive->sect;
304 low += lcyl * drive->sect;
305 low += sect - 1;
309 sector = ((u64) high << 24) | low;
310 return sector;
312 EXPORT_SYMBOL(ide_get_error_location);
315 * ide_end_drive_cmd - end an explicit drive command
316 * @drive: command
317 * @stat: status bits
318 * @err: error bits
320 * Clean up after success/failure of an explicit drive command.
321 * These get thrown onto the queue so they are synchronized with
322 * real I/O operations on the drive.
324 * In LBA48 mode we have to read the register set twice to get
325 * all the extra information out.
328 void ide_end_drive_cmd (ide_drive_t *drive, u8 stat, u8 err)
330 ide_hwif_t *hwif = HWIF(drive);
331 unsigned long flags;
332 struct request *rq;
334 spin_lock_irqsave(&ide_lock, flags);
335 rq = HWGROUP(drive)->rq;
336 spin_unlock_irqrestore(&ide_lock, flags);
338 if (rq->flags & REQ_DRIVE_CMD) {
339 u8 *args = (u8 *) rq->buffer;
340 if (rq->errors == 0)
341 rq->errors = !OK_STAT(stat,READY_STAT,BAD_STAT);
343 if (args) {
344 args[0] = stat;
345 args[1] = err;
346 args[2] = hwif->INB(IDE_NSECTOR_REG);
348 } else if (rq->flags & REQ_DRIVE_TASK) {
349 u8 *args = (u8 *) rq->buffer;
350 if (rq->errors == 0)
351 rq->errors = !OK_STAT(stat,READY_STAT,BAD_STAT);
353 if (args) {
354 args[0] = stat;
355 args[1] = err;
356 args[2] = hwif->INB(IDE_NSECTOR_REG);
357 args[3] = hwif->INB(IDE_SECTOR_REG);
358 args[4] = hwif->INB(IDE_LCYL_REG);
359 args[5] = hwif->INB(IDE_HCYL_REG);
360 args[6] = hwif->INB(IDE_SELECT_REG);
362 } else if (rq->flags & REQ_DRIVE_TASKFILE) {
363 ide_task_t *args = (ide_task_t *) rq->special;
364 if (rq->errors == 0)
365 rq->errors = !OK_STAT(stat,READY_STAT,BAD_STAT);
367 if (args) {
368 if (args->tf_in_flags.b.data) {
369 u16 data = hwif->INW(IDE_DATA_REG);
370 args->tfRegister[IDE_DATA_OFFSET] = (data) & 0xFF;
371 args->hobRegister[IDE_DATA_OFFSET] = (data >> 8) & 0xFF;
373 args->tfRegister[IDE_ERROR_OFFSET] = err;
374 /* be sure we're looking at the low order bits */
375 hwif->OUTB(drive->ctl & ~0x80, IDE_CONTROL_REG);
376 args->tfRegister[IDE_NSECTOR_OFFSET] = hwif->INB(IDE_NSECTOR_REG);
377 args->tfRegister[IDE_SECTOR_OFFSET] = hwif->INB(IDE_SECTOR_REG);
378 args->tfRegister[IDE_LCYL_OFFSET] = hwif->INB(IDE_LCYL_REG);
379 args->tfRegister[IDE_HCYL_OFFSET] = hwif->INB(IDE_HCYL_REG);
380 args->tfRegister[IDE_SELECT_OFFSET] = hwif->INB(IDE_SELECT_REG);
381 args->tfRegister[IDE_STATUS_OFFSET] = stat;
383 if (drive->addressing == 1) {
384 hwif->OUTB(drive->ctl|0x80, IDE_CONTROL_REG);
385 args->hobRegister[IDE_FEATURE_OFFSET] = hwif->INB(IDE_FEATURE_REG);
386 args->hobRegister[IDE_NSECTOR_OFFSET] = hwif->INB(IDE_NSECTOR_REG);
387 args->hobRegister[IDE_SECTOR_OFFSET] = hwif->INB(IDE_SECTOR_REG);
388 args->hobRegister[IDE_LCYL_OFFSET] = hwif->INB(IDE_LCYL_REG);
389 args->hobRegister[IDE_HCYL_OFFSET] = hwif->INB(IDE_HCYL_REG);
392 } else if (blk_pm_request(rq)) {
393 #ifdef DEBUG_PM
394 printk("%s: complete_power_step(step: %d, stat: %x, err: %x)\n",
395 drive->name, rq->pm->pm_step, stat, err);
396 #endif
397 ide_complete_power_step(drive, rq, stat, err);
398 if (rq->pm->pm_step == ide_pm_state_completed)
399 ide_complete_pm_request(drive, rq);
400 return;
403 spin_lock_irqsave(&ide_lock, flags);
404 blkdev_dequeue_request(rq);
405 HWGROUP(drive)->rq = NULL;
406 rq->errors = err;
407 end_that_request_last(rq, !rq->errors);
408 spin_unlock_irqrestore(&ide_lock, flags);
411 EXPORT_SYMBOL(ide_end_drive_cmd);
414 * try_to_flush_leftover_data - flush junk
415 * @drive: drive to flush
417 * try_to_flush_leftover_data() is invoked in response to a drive
418 * unexpectedly having its DRQ_STAT bit set. As an alternative to
419 * resetting the drive, this routine tries to clear the condition
420 * by read a sector's worth of data from the drive. Of course,
421 * this may not help if the drive is *waiting* for data from *us*.
423 static void try_to_flush_leftover_data (ide_drive_t *drive)
425 int i = (drive->mult_count ? drive->mult_count : 1) * SECTOR_WORDS;
427 if (drive->media != ide_disk)
428 return;
429 while (i > 0) {
430 u32 buffer[16];
431 u32 wcount = (i > 16) ? 16 : i;
433 i -= wcount;
434 HWIF(drive)->ata_input_data(drive, buffer, wcount);
438 static void ide_kill_rq(ide_drive_t *drive, struct request *rq)
440 if (rq->rq_disk) {
441 ide_driver_t *drv;
443 drv = *(ide_driver_t **)rq->rq_disk->private_data;
444 drv->end_request(drive, 0, 0);
445 } else
446 ide_end_request(drive, 0, 0);
449 static ide_startstop_t ide_ata_error(ide_drive_t *drive, struct request *rq, u8 stat, u8 err)
451 ide_hwif_t *hwif = drive->hwif;
453 if (stat & BUSY_STAT || ((stat & WRERR_STAT) && !drive->nowerr)) {
454 /* other bits are useless when BUSY */
455 rq->errors |= ERROR_RESET;
456 } else if (stat & ERR_STAT) {
457 /* err has different meaning on cdrom and tape */
458 if (err == ABRT_ERR) {
459 if (drive->select.b.lba &&
460 /* some newer drives don't support WIN_SPECIFY */
461 hwif->INB(IDE_COMMAND_REG) == WIN_SPECIFY)
462 return ide_stopped;
463 } else if ((err & BAD_CRC) == BAD_CRC) {
464 /* UDMA crc error, just retry the operation */
465 drive->crc_count++;
466 } else if (err & (BBD_ERR | ECC_ERR)) {
467 /* retries won't help these */
468 rq->errors = ERROR_MAX;
469 } else if (err & TRK0_ERR) {
470 /* help it find track zero */
471 rq->errors |= ERROR_RECAL;
475 if ((stat & DRQ_STAT) && rq_data_dir(rq) == READ)
476 try_to_flush_leftover_data(drive);
478 if (hwif->INB(IDE_STATUS_REG) & (BUSY_STAT|DRQ_STAT))
479 /* force an abort */
480 hwif->OUTB(WIN_IDLEIMMEDIATE, IDE_COMMAND_REG);
482 if (rq->errors >= ERROR_MAX || blk_noretry_request(rq))
483 ide_kill_rq(drive, rq);
484 else {
485 if ((rq->errors & ERROR_RESET) == ERROR_RESET) {
486 ++rq->errors;
487 return ide_do_reset(drive);
489 if ((rq->errors & ERROR_RECAL) == ERROR_RECAL)
490 drive->special.b.recalibrate = 1;
491 ++rq->errors;
493 return ide_stopped;
496 static ide_startstop_t ide_atapi_error(ide_drive_t *drive, struct request *rq, u8 stat, u8 err)
498 ide_hwif_t *hwif = drive->hwif;
500 if (stat & BUSY_STAT || ((stat & WRERR_STAT) && !drive->nowerr)) {
501 /* other bits are useless when BUSY */
502 rq->errors |= ERROR_RESET;
503 } else {
504 /* add decoding error stuff */
507 if (hwif->INB(IDE_STATUS_REG) & (BUSY_STAT|DRQ_STAT))
508 /* force an abort */
509 hwif->OUTB(WIN_IDLEIMMEDIATE, IDE_COMMAND_REG);
511 if (rq->errors >= ERROR_MAX) {
512 ide_kill_rq(drive, rq);
513 } else {
514 if ((rq->errors & ERROR_RESET) == ERROR_RESET) {
515 ++rq->errors;
516 return ide_do_reset(drive);
518 ++rq->errors;
521 return ide_stopped;
524 ide_startstop_t
525 __ide_error(ide_drive_t *drive, struct request *rq, u8 stat, u8 err)
527 if (drive->media == ide_disk)
528 return ide_ata_error(drive, rq, stat, err);
529 return ide_atapi_error(drive, rq, stat, err);
532 EXPORT_SYMBOL_GPL(__ide_error);
535 * ide_error - handle an error on the IDE
536 * @drive: drive the error occurred on
537 * @msg: message to report
538 * @stat: status bits
540 * ide_error() takes action based on the error returned by the drive.
541 * For normal I/O that may well include retries. We deal with
542 * both new-style (taskfile) and old style command handling here.
543 * In the case of taskfile command handling there is work left to
544 * do
547 ide_startstop_t ide_error (ide_drive_t *drive, const char *msg, u8 stat)
549 struct request *rq;
550 u8 err;
552 err = ide_dump_status(drive, msg, stat);
554 if ((rq = HWGROUP(drive)->rq) == NULL)
555 return ide_stopped;
557 /* retry only "normal" I/O: */
558 if (rq->flags & (REQ_DRIVE_CMD | REQ_DRIVE_TASK | REQ_DRIVE_TASKFILE)) {
559 rq->errors = 1;
560 ide_end_drive_cmd(drive, stat, err);
561 return ide_stopped;
564 if (rq->rq_disk) {
565 ide_driver_t *drv;
567 drv = *(ide_driver_t **)rq->rq_disk->private_data;
568 return drv->error(drive, rq, stat, err);
569 } else
570 return __ide_error(drive, rq, stat, err);
573 EXPORT_SYMBOL_GPL(ide_error);
575 ide_startstop_t __ide_abort(ide_drive_t *drive, struct request *rq)
577 if (drive->media != ide_disk)
578 rq->errors |= ERROR_RESET;
580 ide_kill_rq(drive, rq);
582 return ide_stopped;
585 EXPORT_SYMBOL_GPL(__ide_abort);
588 * ide_abort - abort pending IDE operations
589 * @drive: drive the error occurred on
590 * @msg: message to report
592 * ide_abort kills and cleans up when we are about to do a
593 * host initiated reset on active commands. Longer term we
594 * want handlers to have sensible abort handling themselves
596 * This differs fundamentally from ide_error because in
597 * this case the command is doing just fine when we
598 * blow it away.
601 ide_startstop_t ide_abort(ide_drive_t *drive, const char *msg)
603 struct request *rq;
605 if (drive == NULL || (rq = HWGROUP(drive)->rq) == NULL)
606 return ide_stopped;
608 /* retry only "normal" I/O: */
609 if (rq->flags & (REQ_DRIVE_CMD | REQ_DRIVE_TASK | REQ_DRIVE_TASKFILE)) {
610 rq->errors = 1;
611 ide_end_drive_cmd(drive, BUSY_STAT, 0);
612 return ide_stopped;
615 if (rq->rq_disk) {
616 ide_driver_t *drv;
618 drv = *(ide_driver_t **)rq->rq_disk->private_data;
619 return drv->abort(drive, rq);
620 } else
621 return __ide_abort(drive, rq);
625 * ide_cmd - issue a simple drive command
626 * @drive: drive the command is for
627 * @cmd: command byte
628 * @nsect: sector byte
629 * @handler: handler for the command completion
631 * Issue a simple drive command with interrupts.
632 * The drive must be selected beforehand.
635 static void ide_cmd (ide_drive_t *drive, u8 cmd, u8 nsect,
636 ide_handler_t *handler)
638 ide_hwif_t *hwif = HWIF(drive);
639 if (IDE_CONTROL_REG)
640 hwif->OUTB(drive->ctl,IDE_CONTROL_REG); /* clear nIEN */
641 SELECT_MASK(drive,0);
642 hwif->OUTB(nsect,IDE_NSECTOR_REG);
643 ide_execute_command(drive, cmd, handler, WAIT_CMD, NULL);
647 * drive_cmd_intr - drive command completion interrupt
648 * @drive: drive the completion interrupt occurred on
650 * drive_cmd_intr() is invoked on completion of a special DRIVE_CMD.
651 * We do any necessary data reading and then wait for the drive to
652 * go non busy. At that point we may read the error data and complete
653 * the request
656 static ide_startstop_t drive_cmd_intr (ide_drive_t *drive)
658 struct request *rq = HWGROUP(drive)->rq;
659 ide_hwif_t *hwif = HWIF(drive);
660 u8 *args = (u8 *) rq->buffer;
661 u8 stat = hwif->INB(IDE_STATUS_REG);
662 int retries = 10;
664 local_irq_enable();
665 if ((stat & DRQ_STAT) && args && args[3]) {
666 u8 io_32bit = drive->io_32bit;
667 drive->io_32bit = 0;
668 hwif->ata_input_data(drive, &args[4], args[3] * SECTOR_WORDS);
669 drive->io_32bit = io_32bit;
670 while (((stat = hwif->INB(IDE_STATUS_REG)) & BUSY_STAT) && retries--)
671 udelay(100);
674 if (!OK_STAT(stat, READY_STAT, BAD_STAT))
675 return ide_error(drive, "drive_cmd", stat);
676 /* calls ide_end_drive_cmd */
677 ide_end_drive_cmd(drive, stat, hwif->INB(IDE_ERROR_REG));
678 return ide_stopped;
681 static void ide_init_specify_cmd(ide_drive_t *drive, ide_task_t *task)
683 task->tfRegister[IDE_NSECTOR_OFFSET] = drive->sect;
684 task->tfRegister[IDE_SECTOR_OFFSET] = drive->sect;
685 task->tfRegister[IDE_LCYL_OFFSET] = drive->cyl;
686 task->tfRegister[IDE_HCYL_OFFSET] = drive->cyl>>8;
687 task->tfRegister[IDE_SELECT_OFFSET] = ((drive->head-1)|drive->select.all)&0xBF;
688 task->tfRegister[IDE_COMMAND_OFFSET] = WIN_SPECIFY;
690 task->handler = &set_geometry_intr;
693 static void ide_init_restore_cmd(ide_drive_t *drive, ide_task_t *task)
695 task->tfRegister[IDE_NSECTOR_OFFSET] = drive->sect;
696 task->tfRegister[IDE_COMMAND_OFFSET] = WIN_RESTORE;
698 task->handler = &recal_intr;
701 static void ide_init_setmult_cmd(ide_drive_t *drive, ide_task_t *task)
703 task->tfRegister[IDE_NSECTOR_OFFSET] = drive->mult_req;
704 task->tfRegister[IDE_COMMAND_OFFSET] = WIN_SETMULT;
706 task->handler = &set_multmode_intr;
709 static ide_startstop_t ide_disk_special(ide_drive_t *drive)
711 special_t *s = &drive->special;
712 ide_task_t args;
714 memset(&args, 0, sizeof(ide_task_t));
715 args.command_type = IDE_DRIVE_TASK_NO_DATA;
717 if (s->b.set_geometry) {
718 s->b.set_geometry = 0;
719 ide_init_specify_cmd(drive, &args);
720 } else if (s->b.recalibrate) {
721 s->b.recalibrate = 0;
722 ide_init_restore_cmd(drive, &args);
723 } else if (s->b.set_multmode) {
724 s->b.set_multmode = 0;
725 if (drive->mult_req > drive->id->max_multsect)
726 drive->mult_req = drive->id->max_multsect;
727 ide_init_setmult_cmd(drive, &args);
728 } else if (s->all) {
729 int special = s->all;
730 s->all = 0;
731 printk(KERN_ERR "%s: bad special flag: 0x%02x\n", drive->name, special);
732 return ide_stopped;
735 do_rw_taskfile(drive, &args);
737 return ide_started;
741 * do_special - issue some special commands
742 * @drive: drive the command is for
744 * do_special() is used to issue WIN_SPECIFY, WIN_RESTORE, and WIN_SETMULT
745 * commands to a drive. It used to do much more, but has been scaled
746 * back.
749 static ide_startstop_t do_special (ide_drive_t *drive)
751 special_t *s = &drive->special;
753 #ifdef DEBUG
754 printk("%s: do_special: 0x%02x\n", drive->name, s->all);
755 #endif
756 if (s->b.set_tune) {
757 s->b.set_tune = 0;
758 if (HWIF(drive)->tuneproc != NULL)
759 HWIF(drive)->tuneproc(drive, drive->tune_req);
760 return ide_stopped;
761 } else {
762 if (drive->media == ide_disk)
763 return ide_disk_special(drive);
765 s->all = 0;
766 drive->mult_req = 0;
767 return ide_stopped;
771 void ide_map_sg(ide_drive_t *drive, struct request *rq)
773 ide_hwif_t *hwif = drive->hwif;
774 struct scatterlist *sg = hwif->sg_table;
776 if (hwif->sg_mapped) /* needed by ide-scsi */
777 return;
779 if ((rq->flags & REQ_DRIVE_TASKFILE) == 0) {
780 hwif->sg_nents = blk_rq_map_sg(drive->queue, rq, sg);
781 } else {
782 sg_init_one(sg, rq->buffer, rq->nr_sectors * SECTOR_SIZE);
783 hwif->sg_nents = 1;
787 EXPORT_SYMBOL_GPL(ide_map_sg);
789 void ide_init_sg_cmd(ide_drive_t *drive, struct request *rq)
791 ide_hwif_t *hwif = drive->hwif;
793 hwif->nsect = hwif->nleft = rq->nr_sectors;
794 hwif->cursg = hwif->cursg_ofs = 0;
797 EXPORT_SYMBOL_GPL(ide_init_sg_cmd);
800 * execute_drive_command - issue special drive command
801 * @drive: the drive to issue the command on
802 * @rq: the request structure holding the command
804 * execute_drive_cmd() issues a special drive command, usually
805 * initiated by ioctl() from the external hdparm program. The
806 * command can be a drive command, drive task or taskfile
807 * operation. Weirdly you can call it with NULL to wait for
808 * all commands to finish. Don't do this as that is due to change
811 static ide_startstop_t execute_drive_cmd (ide_drive_t *drive,
812 struct request *rq)
814 ide_hwif_t *hwif = HWIF(drive);
815 if (rq->flags & REQ_DRIVE_TASKFILE) {
816 ide_task_t *args = rq->special;
818 if (!args)
819 goto done;
821 hwif->data_phase = args->data_phase;
823 switch (hwif->data_phase) {
824 case TASKFILE_MULTI_OUT:
825 case TASKFILE_OUT:
826 case TASKFILE_MULTI_IN:
827 case TASKFILE_IN:
828 ide_init_sg_cmd(drive, rq);
829 ide_map_sg(drive, rq);
830 default:
831 break;
834 if (args->tf_out_flags.all != 0)
835 return flagged_taskfile(drive, args);
836 return do_rw_taskfile(drive, args);
837 } else if (rq->flags & REQ_DRIVE_TASK) {
838 u8 *args = rq->buffer;
839 u8 sel;
841 if (!args)
842 goto done;
843 #ifdef DEBUG
844 printk("%s: DRIVE_TASK_CMD ", drive->name);
845 printk("cmd=0x%02x ", args[0]);
846 printk("fr=0x%02x ", args[1]);
847 printk("ns=0x%02x ", args[2]);
848 printk("sc=0x%02x ", args[3]);
849 printk("lcyl=0x%02x ", args[4]);
850 printk("hcyl=0x%02x ", args[5]);
851 printk("sel=0x%02x\n", args[6]);
852 #endif
853 hwif->OUTB(args[1], IDE_FEATURE_REG);
854 hwif->OUTB(args[3], IDE_SECTOR_REG);
855 hwif->OUTB(args[4], IDE_LCYL_REG);
856 hwif->OUTB(args[5], IDE_HCYL_REG);
857 sel = (args[6] & ~0x10);
858 if (drive->select.b.unit)
859 sel |= 0x10;
860 hwif->OUTB(sel, IDE_SELECT_REG);
861 ide_cmd(drive, args[0], args[2], &drive_cmd_intr);
862 return ide_started;
863 } else if (rq->flags & REQ_DRIVE_CMD) {
864 u8 *args = rq->buffer;
866 if (!args)
867 goto done;
868 #ifdef DEBUG
869 printk("%s: DRIVE_CMD ", drive->name);
870 printk("cmd=0x%02x ", args[0]);
871 printk("sc=0x%02x ", args[1]);
872 printk("fr=0x%02x ", args[2]);
873 printk("xx=0x%02x\n", args[3]);
874 #endif
875 if (args[0] == WIN_SMART) {
876 hwif->OUTB(0x4f, IDE_LCYL_REG);
877 hwif->OUTB(0xc2, IDE_HCYL_REG);
878 hwif->OUTB(args[2],IDE_FEATURE_REG);
879 hwif->OUTB(args[1],IDE_SECTOR_REG);
880 ide_cmd(drive, args[0], args[3], &drive_cmd_intr);
881 return ide_started;
883 hwif->OUTB(args[2],IDE_FEATURE_REG);
884 ide_cmd(drive, args[0], args[1], &drive_cmd_intr);
885 return ide_started;
888 done:
890 * NULL is actually a valid way of waiting for
891 * all current requests to be flushed from the queue.
893 #ifdef DEBUG
894 printk("%s: DRIVE_CMD (null)\n", drive->name);
895 #endif
896 ide_end_drive_cmd(drive,
897 hwif->INB(IDE_STATUS_REG),
898 hwif->INB(IDE_ERROR_REG));
899 return ide_stopped;
903 * start_request - start of I/O and command issuing for IDE
905 * start_request() initiates handling of a new I/O request. It
906 * accepts commands and I/O (read/write) requests. It also does
907 * the final remapping for weird stuff like EZDrive. Once
908 * device mapper can work sector level the EZDrive stuff can go away
910 * FIXME: this function needs a rename
913 static ide_startstop_t start_request (ide_drive_t *drive, struct request *rq)
915 ide_startstop_t startstop;
916 sector_t block;
918 BUG_ON(!(rq->flags & REQ_STARTED));
920 #ifdef DEBUG
921 printk("%s: start_request: current=0x%08lx\n",
922 HWIF(drive)->name, (unsigned long) rq);
923 #endif
925 /* bail early if we've exceeded max_failures */
926 if (drive->max_failures && (drive->failures > drive->max_failures)) {
927 goto kill_rq;
930 block = rq->sector;
931 if (blk_fs_request(rq) &&
932 (drive->media == ide_disk || drive->media == ide_floppy)) {
933 block += drive->sect0;
935 /* Yecch - this will shift the entire interval,
936 possibly killing some innocent following sector */
937 if (block == 0 && drive->remap_0_to_1 == 1)
938 block = 1; /* redirect MBR access to EZ-Drive partn table */
940 if (blk_pm_suspend_request(rq) &&
941 rq->pm->pm_step == ide_pm_state_start_suspend)
942 /* Mark drive blocked when starting the suspend sequence. */
943 drive->blocked = 1;
944 else if (blk_pm_resume_request(rq) &&
945 rq->pm->pm_step == ide_pm_state_start_resume) {
947 * The first thing we do on wakeup is to wait for BSY bit to
948 * go away (with a looong timeout) as a drive on this hwif may
949 * just be POSTing itself.
950 * We do that before even selecting as the "other" device on
951 * the bus may be broken enough to walk on our toes at this
952 * point.
954 int rc;
955 #ifdef DEBUG_PM
956 printk("%s: Wakeup request inited, waiting for !BSY...\n", drive->name);
957 #endif
958 rc = ide_wait_not_busy(HWIF(drive), 35000);
959 if (rc)
960 printk(KERN_WARNING "%s: bus not ready on wakeup\n", drive->name);
961 SELECT_DRIVE(drive);
962 HWIF(drive)->OUTB(8, HWIF(drive)->io_ports[IDE_CONTROL_OFFSET]);
963 rc = ide_wait_not_busy(HWIF(drive), 10000);
964 if (rc)
965 printk(KERN_WARNING "%s: drive not ready on wakeup\n", drive->name);
968 SELECT_DRIVE(drive);
969 if (ide_wait_stat(&startstop, drive, drive->ready_stat, BUSY_STAT|DRQ_STAT, WAIT_READY)) {
970 printk(KERN_ERR "%s: drive not ready for command\n", drive->name);
971 return startstop;
973 if (!drive->special.all) {
974 ide_driver_t *drv;
976 if (rq->flags & (REQ_DRIVE_CMD | REQ_DRIVE_TASK))
977 return execute_drive_cmd(drive, rq);
978 else if (rq->flags & REQ_DRIVE_TASKFILE)
979 return execute_drive_cmd(drive, rq);
980 else if (blk_pm_request(rq)) {
981 #ifdef DEBUG_PM
982 printk("%s: start_power_step(step: %d)\n",
983 drive->name, rq->pm->pm_step);
984 #endif
985 startstop = ide_start_power_step(drive, rq);
986 if (startstop == ide_stopped &&
987 rq->pm->pm_step == ide_pm_state_completed)
988 ide_complete_pm_request(drive, rq);
989 return startstop;
992 drv = *(ide_driver_t **)rq->rq_disk->private_data;
993 return drv->do_request(drive, rq, block);
995 return do_special(drive);
996 kill_rq:
997 ide_kill_rq(drive, rq);
998 return ide_stopped;
1002 * ide_stall_queue - pause an IDE device
1003 * @drive: drive to stall
1004 * @timeout: time to stall for (jiffies)
1006 * ide_stall_queue() can be used by a drive to give excess bandwidth back
1007 * to the hwgroup by sleeping for timeout jiffies.
1010 void ide_stall_queue (ide_drive_t *drive, unsigned long timeout)
1012 if (timeout > WAIT_WORSTCASE)
1013 timeout = WAIT_WORSTCASE;
1014 drive->sleep = timeout + jiffies;
1015 drive->sleeping = 1;
1018 EXPORT_SYMBOL(ide_stall_queue);
1020 #define WAKEUP(drive) ((drive)->service_start + 2 * (drive)->service_time)
1023 * choose_drive - select a drive to service
1024 * @hwgroup: hardware group to select on
1026 * choose_drive() selects the next drive which will be serviced.
1027 * This is necessary because the IDE layer can't issue commands
1028 * to both drives on the same cable, unlike SCSI.
1031 static inline ide_drive_t *choose_drive (ide_hwgroup_t *hwgroup)
1033 ide_drive_t *drive, *best;
1035 repeat:
1036 best = NULL;
1037 drive = hwgroup->drive;
1040 * drive is doing pre-flush, ordered write, post-flush sequence. even
1041 * though that is 3 requests, it must be seen as a single transaction.
1042 * we must not preempt this drive until that is complete
1044 if (blk_queue_flushing(drive->queue)) {
1046 * small race where queue could get replugged during
1047 * the 3-request flush cycle, just yank the plug since
1048 * we want it to finish asap
1050 blk_remove_plug(drive->queue);
1051 return drive;
1054 do {
1055 if ((!drive->sleeping || time_after_eq(jiffies, drive->sleep))
1056 && !elv_queue_empty(drive->queue)) {
1057 if (!best
1058 || (drive->sleeping && (!best->sleeping || time_before(drive->sleep, best->sleep)))
1059 || (!best->sleeping && time_before(WAKEUP(drive), WAKEUP(best))))
1061 if (!blk_queue_plugged(drive->queue))
1062 best = drive;
1065 } while ((drive = drive->next) != hwgroup->drive);
1066 if (best && best->nice1 && !best->sleeping && best != hwgroup->drive && best->service_time > WAIT_MIN_SLEEP) {
1067 long t = (signed long)(WAKEUP(best) - jiffies);
1068 if (t >= WAIT_MIN_SLEEP) {
1070 * We *may* have some time to spare, but first let's see if
1071 * someone can potentially benefit from our nice mood today..
1073 drive = best->next;
1074 do {
1075 if (!drive->sleeping
1076 && time_before(jiffies - best->service_time, WAKEUP(drive))
1077 && time_before(WAKEUP(drive), jiffies + t))
1079 ide_stall_queue(best, min_t(long, t, 10 * WAIT_MIN_SLEEP));
1080 goto repeat;
1082 } while ((drive = drive->next) != best);
1085 return best;
1089 * Issue a new request to a drive from hwgroup
1090 * Caller must have already done spin_lock_irqsave(&ide_lock, ..);
1092 * A hwgroup is a serialized group of IDE interfaces. Usually there is
1093 * exactly one hwif (interface) per hwgroup, but buggy controllers (eg. CMD640)
1094 * may have both interfaces in a single hwgroup to "serialize" access.
1095 * Or possibly multiple ISA interfaces can share a common IRQ by being grouped
1096 * together into one hwgroup for serialized access.
1098 * Note also that several hwgroups can end up sharing a single IRQ,
1099 * possibly along with many other devices. This is especially common in
1100 * PCI-based systems with off-board IDE controller cards.
1102 * The IDE driver uses the single global ide_lock spinlock to protect
1103 * access to the request queues, and to protect the hwgroup->busy flag.
1105 * The first thread into the driver for a particular hwgroup sets the
1106 * hwgroup->busy flag to indicate that this hwgroup is now active,
1107 * and then initiates processing of the top request from the request queue.
1109 * Other threads attempting entry notice the busy setting, and will simply
1110 * queue their new requests and exit immediately. Note that hwgroup->busy
1111 * remains set even when the driver is merely awaiting the next interrupt.
1112 * Thus, the meaning is "this hwgroup is busy processing a request".
1114 * When processing of a request completes, the completing thread or IRQ-handler
1115 * will start the next request from the queue. If no more work remains,
1116 * the driver will clear the hwgroup->busy flag and exit.
1118 * The ide_lock (spinlock) is used to protect all access to the
1119 * hwgroup->busy flag, but is otherwise not needed for most processing in
1120 * the driver. This makes the driver much more friendlier to shared IRQs
1121 * than previous designs, while remaining 100% (?) SMP safe and capable.
1123 static void ide_do_request (ide_hwgroup_t *hwgroup, int masked_irq)
1125 ide_drive_t *drive;
1126 ide_hwif_t *hwif;
1127 struct request *rq;
1128 ide_startstop_t startstop;
1129 int loops = 0;
1131 /* for atari only: POSSIBLY BROKEN HERE(?) */
1132 ide_get_lock(ide_intr, hwgroup);
1134 /* caller must own ide_lock */
1135 BUG_ON(!irqs_disabled());
1137 while (!hwgroup->busy) {
1138 hwgroup->busy = 1;
1139 drive = choose_drive(hwgroup);
1140 if (drive == NULL) {
1141 int sleeping = 0;
1142 unsigned long sleep = 0; /* shut up, gcc */
1143 hwgroup->rq = NULL;
1144 drive = hwgroup->drive;
1145 do {
1146 if (drive->sleeping && (!sleeping || time_before(drive->sleep, sleep))) {
1147 sleeping = 1;
1148 sleep = drive->sleep;
1150 } while ((drive = drive->next) != hwgroup->drive);
1151 if (sleeping) {
1153 * Take a short snooze, and then wake up this hwgroup again.
1154 * This gives other hwgroups on the same a chance to
1155 * play fairly with us, just in case there are big differences
1156 * in relative throughputs.. don't want to hog the cpu too much.
1158 if (time_before(sleep, jiffies + WAIT_MIN_SLEEP))
1159 sleep = jiffies + WAIT_MIN_SLEEP;
1160 #if 1
1161 if (timer_pending(&hwgroup->timer))
1162 printk(KERN_CRIT "ide_set_handler: timer already active\n");
1163 #endif
1164 /* so that ide_timer_expiry knows what to do */
1165 hwgroup->sleeping = 1;
1166 mod_timer(&hwgroup->timer, sleep);
1167 /* we purposely leave hwgroup->busy==1
1168 * while sleeping */
1169 } else {
1170 /* Ugly, but how can we sleep for the lock
1171 * otherwise? perhaps from tq_disk?
1174 /* for atari only */
1175 ide_release_lock();
1176 hwgroup->busy = 0;
1179 /* no more work for this hwgroup (for now) */
1180 return;
1182 again:
1183 hwif = HWIF(drive);
1184 if (hwgroup->hwif->sharing_irq &&
1185 hwif != hwgroup->hwif &&
1186 hwif->io_ports[IDE_CONTROL_OFFSET]) {
1187 /* set nIEN for previous hwif */
1188 SELECT_INTERRUPT(drive);
1190 hwgroup->hwif = hwif;
1191 hwgroup->drive = drive;
1192 drive->sleeping = 0;
1193 drive->service_start = jiffies;
1195 if (blk_queue_plugged(drive->queue)) {
1196 printk(KERN_ERR "ide: huh? queue was plugged!\n");
1197 break;
1201 * we know that the queue isn't empty, but this can happen
1202 * if the q->prep_rq_fn() decides to kill a request
1204 rq = elv_next_request(drive->queue);
1205 if (!rq) {
1206 hwgroup->busy = 0;
1207 break;
1211 * Sanity: don't accept a request that isn't a PM request
1212 * if we are currently power managed. This is very important as
1213 * blk_stop_queue() doesn't prevent the elv_next_request()
1214 * above to return us whatever is in the queue. Since we call
1215 * ide_do_request() ourselves, we end up taking requests while
1216 * the queue is blocked...
1218 * We let requests forced at head of queue with ide-preempt
1219 * though. I hope that doesn't happen too much, hopefully not
1220 * unless the subdriver triggers such a thing in its own PM
1221 * state machine.
1223 * We count how many times we loop here to make sure we service
1224 * all drives in the hwgroup without looping for ever
1226 if (drive->blocked && !blk_pm_request(rq) && !(rq->flags & REQ_PREEMPT)) {
1227 drive = drive->next ? drive->next : hwgroup->drive;
1228 if (loops++ < 4 && !blk_queue_plugged(drive->queue))
1229 goto again;
1230 /* We clear busy, there should be no pending ATA command at this point. */
1231 hwgroup->busy = 0;
1232 break;
1235 hwgroup->rq = rq;
1238 * Some systems have trouble with IDE IRQs arriving while
1239 * the driver is still setting things up. So, here we disable
1240 * the IRQ used by this interface while the request is being started.
1241 * This may look bad at first, but pretty much the same thing
1242 * happens anyway when any interrupt comes in, IDE or otherwise
1243 * -- the kernel masks the IRQ while it is being handled.
1245 if (masked_irq != IDE_NO_IRQ && hwif->irq != masked_irq)
1246 disable_irq_nosync(hwif->irq);
1247 spin_unlock(&ide_lock);
1248 local_irq_enable();
1249 /* allow other IRQs while we start this request */
1250 startstop = start_request(drive, rq);
1251 spin_lock_irq(&ide_lock);
1252 if (masked_irq != IDE_NO_IRQ && hwif->irq != masked_irq)
1253 enable_irq(hwif->irq);
1254 if (startstop == ide_stopped)
1255 hwgroup->busy = 0;
1260 * Passes the stuff to ide_do_request
1262 void do_ide_request(request_queue_t *q)
1264 ide_drive_t *drive = q->queuedata;
1266 ide_do_request(HWGROUP(drive), IDE_NO_IRQ);
1270 * un-busy the hwgroup etc, and clear any pending DMA status. we want to
1271 * retry the current request in pio mode instead of risking tossing it
1272 * all away
1274 static ide_startstop_t ide_dma_timeout_retry(ide_drive_t *drive, int error)
1276 ide_hwif_t *hwif = HWIF(drive);
1277 struct request *rq;
1278 ide_startstop_t ret = ide_stopped;
1281 * end current dma transaction
1284 if (error < 0) {
1285 printk(KERN_WARNING "%s: DMA timeout error\n", drive->name);
1286 (void)HWIF(drive)->ide_dma_end(drive);
1287 ret = ide_error(drive, "dma timeout error",
1288 hwif->INB(IDE_STATUS_REG));
1289 } else {
1290 printk(KERN_WARNING "%s: DMA timeout retry\n", drive->name);
1291 (void) hwif->ide_dma_timeout(drive);
1295 * disable dma for now, but remember that we did so because of
1296 * a timeout -- we'll reenable after we finish this next request
1297 * (or rather the first chunk of it) in pio.
1299 drive->retry_pio++;
1300 drive->state = DMA_PIO_RETRY;
1301 (void) hwif->ide_dma_off_quietly(drive);
1304 * un-busy drive etc (hwgroup->busy is cleared on return) and
1305 * make sure request is sane
1307 rq = HWGROUP(drive)->rq;
1308 HWGROUP(drive)->rq = NULL;
1310 rq->errors = 0;
1312 if (!rq->bio)
1313 goto out;
1315 rq->sector = rq->bio->bi_sector;
1316 rq->current_nr_sectors = bio_iovec(rq->bio)->bv_len >> 9;
1317 rq->hard_cur_sectors = rq->current_nr_sectors;
1318 rq->buffer = bio_data(rq->bio);
1319 out:
1320 return ret;
1324 * ide_timer_expiry - handle lack of an IDE interrupt
1325 * @data: timer callback magic (hwgroup)
1327 * An IDE command has timed out before the expected drive return
1328 * occurred. At this point we attempt to clean up the current
1329 * mess. If the current handler includes an expiry handler then
1330 * we invoke the expiry handler, and providing it is happy the
1331 * work is done. If that fails we apply generic recovery rules
1332 * invoking the handler and checking the drive DMA status. We
1333 * have an excessively incestuous relationship with the DMA
1334 * logic that wants cleaning up.
1337 void ide_timer_expiry (unsigned long data)
1339 ide_hwgroup_t *hwgroup = (ide_hwgroup_t *) data;
1340 ide_handler_t *handler;
1341 ide_expiry_t *expiry;
1342 unsigned long flags;
1343 unsigned long wait = -1;
1345 spin_lock_irqsave(&ide_lock, flags);
1347 if ((handler = hwgroup->handler) == NULL) {
1349 * Either a marginal timeout occurred
1350 * (got the interrupt just as timer expired),
1351 * or we were "sleeping" to give other devices a chance.
1352 * Either way, we don't really want to complain about anything.
1354 if (hwgroup->sleeping) {
1355 hwgroup->sleeping = 0;
1356 hwgroup->busy = 0;
1358 } else {
1359 ide_drive_t *drive = hwgroup->drive;
1360 if (!drive) {
1361 printk(KERN_ERR "ide_timer_expiry: hwgroup->drive was NULL\n");
1362 hwgroup->handler = NULL;
1363 } else {
1364 ide_hwif_t *hwif;
1365 ide_startstop_t startstop = ide_stopped;
1366 if (!hwgroup->busy) {
1367 hwgroup->busy = 1; /* paranoia */
1368 printk(KERN_ERR "%s: ide_timer_expiry: hwgroup->busy was 0 ??\n", drive->name);
1370 if ((expiry = hwgroup->expiry) != NULL) {
1371 /* continue */
1372 if ((wait = expiry(drive)) > 0) {
1373 /* reset timer */
1374 hwgroup->timer.expires = jiffies + wait;
1375 add_timer(&hwgroup->timer);
1376 spin_unlock_irqrestore(&ide_lock, flags);
1377 return;
1380 hwgroup->handler = NULL;
1382 * We need to simulate a real interrupt when invoking
1383 * the handler() function, which means we need to
1384 * globally mask the specific IRQ:
1386 spin_unlock(&ide_lock);
1387 hwif = HWIF(drive);
1388 #if DISABLE_IRQ_NOSYNC
1389 disable_irq_nosync(hwif->irq);
1390 #else
1391 /* disable_irq_nosync ?? */
1392 disable_irq(hwif->irq);
1393 #endif /* DISABLE_IRQ_NOSYNC */
1394 /* local CPU only,
1395 * as if we were handling an interrupt */
1396 local_irq_disable();
1397 if (hwgroup->polling) {
1398 startstop = handler(drive);
1399 } else if (drive_is_ready(drive)) {
1400 if (drive->waiting_for_dma)
1401 (void) hwgroup->hwif->ide_dma_lostirq(drive);
1402 (void)ide_ack_intr(hwif);
1403 printk(KERN_WARNING "%s: lost interrupt\n", drive->name);
1404 startstop = handler(drive);
1405 } else {
1406 if (drive->waiting_for_dma) {
1407 startstop = ide_dma_timeout_retry(drive, wait);
1408 } else
1409 startstop =
1410 ide_error(drive, "irq timeout", hwif->INB(IDE_STATUS_REG));
1412 drive->service_time = jiffies - drive->service_start;
1413 spin_lock_irq(&ide_lock);
1414 enable_irq(hwif->irq);
1415 if (startstop == ide_stopped)
1416 hwgroup->busy = 0;
1419 ide_do_request(hwgroup, IDE_NO_IRQ);
1420 spin_unlock_irqrestore(&ide_lock, flags);
1424 * unexpected_intr - handle an unexpected IDE interrupt
1425 * @irq: interrupt line
1426 * @hwgroup: hwgroup being processed
1428 * There's nothing really useful we can do with an unexpected interrupt,
1429 * other than reading the status register (to clear it), and logging it.
1430 * There should be no way that an irq can happen before we're ready for it,
1431 * so we needn't worry much about losing an "important" interrupt here.
1433 * On laptops (and "green" PCs), an unexpected interrupt occurs whenever
1434 * the drive enters "idle", "standby", or "sleep" mode, so if the status
1435 * looks "good", we just ignore the interrupt completely.
1437 * This routine assumes __cli() is in effect when called.
1439 * If an unexpected interrupt happens on irq15 while we are handling irq14
1440 * and if the two interfaces are "serialized" (CMD640), then it looks like
1441 * we could screw up by interfering with a new request being set up for
1442 * irq15.
1444 * In reality, this is a non-issue. The new command is not sent unless
1445 * the drive is ready to accept one, in which case we know the drive is
1446 * not trying to interrupt us. And ide_set_handler() is always invoked
1447 * before completing the issuance of any new drive command, so we will not
1448 * be accidentally invoked as a result of any valid command completion
1449 * interrupt.
1451 * Note that we must walk the entire hwgroup here. We know which hwif
1452 * is doing the current command, but we don't know which hwif burped
1453 * mysteriously.
1456 static void unexpected_intr (int irq, ide_hwgroup_t *hwgroup)
1458 u8 stat;
1459 ide_hwif_t *hwif = hwgroup->hwif;
1462 * handle the unexpected interrupt
1464 do {
1465 if (hwif->irq == irq) {
1466 stat = hwif->INB(hwif->io_ports[IDE_STATUS_OFFSET]);
1467 if (!OK_STAT(stat, READY_STAT, BAD_STAT)) {
1468 /* Try to not flood the console with msgs */
1469 static unsigned long last_msgtime, count;
1470 ++count;
1471 if (time_after(jiffies, last_msgtime + HZ)) {
1472 last_msgtime = jiffies;
1473 printk(KERN_ERR "%s%s: unexpected interrupt, "
1474 "status=0x%02x, count=%ld\n",
1475 hwif->name,
1476 (hwif->next==hwgroup->hwif) ? "" : "(?)", stat, count);
1480 } while ((hwif = hwif->next) != hwgroup->hwif);
1484 * ide_intr - default IDE interrupt handler
1485 * @irq: interrupt number
1486 * @dev_id: hwif group
1487 * @regs: unused weirdness from the kernel irq layer
1489 * This is the default IRQ handler for the IDE layer. You should
1490 * not need to override it. If you do be aware it is subtle in
1491 * places
1493 * hwgroup->hwif is the interface in the group currently performing
1494 * a command. hwgroup->drive is the drive and hwgroup->handler is
1495 * the IRQ handler to call. As we issue a command the handlers
1496 * step through multiple states, reassigning the handler to the
1497 * next step in the process. Unlike a smart SCSI controller IDE
1498 * expects the main processor to sequence the various transfer
1499 * stages. We also manage a poll timer to catch up with most
1500 * timeout situations. There are still a few where the handlers
1501 * don't ever decide to give up.
1503 * The handler eventually returns ide_stopped to indicate the
1504 * request completed. At this point we issue the next request
1505 * on the hwgroup and the process begins again.
1508 irqreturn_t ide_intr (int irq, void *dev_id, struct pt_regs *regs)
1510 unsigned long flags;
1511 ide_hwgroup_t *hwgroup = (ide_hwgroup_t *)dev_id;
1512 ide_hwif_t *hwif;
1513 ide_drive_t *drive;
1514 ide_handler_t *handler;
1515 ide_startstop_t startstop;
1517 spin_lock_irqsave(&ide_lock, flags);
1518 hwif = hwgroup->hwif;
1520 if (!ide_ack_intr(hwif)) {
1521 spin_unlock_irqrestore(&ide_lock, flags);
1522 return IRQ_NONE;
1525 if ((handler = hwgroup->handler) == NULL || hwgroup->polling) {
1527 * Not expecting an interrupt from this drive.
1528 * That means this could be:
1529 * (1) an interrupt from another PCI device
1530 * sharing the same PCI INT# as us.
1531 * or (2) a drive just entered sleep or standby mode,
1532 * and is interrupting to let us know.
1533 * or (3) a spurious interrupt of unknown origin.
1535 * For PCI, we cannot tell the difference,
1536 * so in that case we just ignore it and hope it goes away.
1538 * FIXME: unexpected_intr should be hwif-> then we can
1539 * remove all the ifdef PCI crap
1541 #ifdef CONFIG_BLK_DEV_IDEPCI
1542 if (hwif->pci_dev && !hwif->pci_dev->vendor)
1543 #endif /* CONFIG_BLK_DEV_IDEPCI */
1546 * Probably not a shared PCI interrupt,
1547 * so we can safely try to do something about it:
1549 unexpected_intr(irq, hwgroup);
1550 #ifdef CONFIG_BLK_DEV_IDEPCI
1551 } else {
1553 * Whack the status register, just in case
1554 * we have a leftover pending IRQ.
1556 (void) hwif->INB(hwif->io_ports[IDE_STATUS_OFFSET]);
1557 #endif /* CONFIG_BLK_DEV_IDEPCI */
1559 spin_unlock_irqrestore(&ide_lock, flags);
1560 return IRQ_NONE;
1562 drive = hwgroup->drive;
1563 if (!drive) {
1565 * This should NEVER happen, and there isn't much
1566 * we could do about it here.
1568 * [Note - this can occur if the drive is hot unplugged]
1570 spin_unlock_irqrestore(&ide_lock, flags);
1571 return IRQ_HANDLED;
1573 if (!drive_is_ready(drive)) {
1575 * This happens regularly when we share a PCI IRQ with
1576 * another device. Unfortunately, it can also happen
1577 * with some buggy drives that trigger the IRQ before
1578 * their status register is up to date. Hopefully we have
1579 * enough advance overhead that the latter isn't a problem.
1581 spin_unlock_irqrestore(&ide_lock, flags);
1582 return IRQ_NONE;
1584 if (!hwgroup->busy) {
1585 hwgroup->busy = 1; /* paranoia */
1586 printk(KERN_ERR "%s: ide_intr: hwgroup->busy was 0 ??\n", drive->name);
1588 hwgroup->handler = NULL;
1589 del_timer(&hwgroup->timer);
1590 spin_unlock(&ide_lock);
1592 if (drive->unmask)
1593 local_irq_enable();
1594 /* service this interrupt, may set handler for next interrupt */
1595 startstop = handler(drive);
1596 spin_lock_irq(&ide_lock);
1599 * Note that handler() may have set things up for another
1600 * interrupt to occur soon, but it cannot happen until
1601 * we exit from this routine, because it will be the
1602 * same irq as is currently being serviced here, and Linux
1603 * won't allow another of the same (on any CPU) until we return.
1605 drive->service_time = jiffies - drive->service_start;
1606 if (startstop == ide_stopped) {
1607 if (hwgroup->handler == NULL) { /* paranoia */
1608 hwgroup->busy = 0;
1609 ide_do_request(hwgroup, hwif->irq);
1610 } else {
1611 printk(KERN_ERR "%s: ide_intr: huh? expected NULL handler "
1612 "on exit\n", drive->name);
1615 spin_unlock_irqrestore(&ide_lock, flags);
1616 return IRQ_HANDLED;
1620 * ide_init_drive_cmd - initialize a drive command request
1621 * @rq: request object
1623 * Initialize a request before we fill it in and send it down to
1624 * ide_do_drive_cmd. Commands must be set up by this function. Right
1625 * now it doesn't do a lot, but if that changes abusers will have a
1626 * nasty suprise.
1629 void ide_init_drive_cmd (struct request *rq)
1631 memset(rq, 0, sizeof(*rq));
1632 rq->flags = REQ_DRIVE_CMD;
1633 rq->ref_count = 1;
1636 EXPORT_SYMBOL(ide_init_drive_cmd);
1639 * ide_do_drive_cmd - issue IDE special command
1640 * @drive: device to issue command
1641 * @rq: request to issue
1642 * @action: action for processing
1644 * This function issues a special IDE device request
1645 * onto the request queue.
1647 * If action is ide_wait, then the rq is queued at the end of the
1648 * request queue, and the function sleeps until it has been processed.
1649 * This is for use when invoked from an ioctl handler.
1651 * If action is ide_preempt, then the rq is queued at the head of
1652 * the request queue, displacing the currently-being-processed
1653 * request and this function returns immediately without waiting
1654 * for the new rq to be completed. This is VERY DANGEROUS, and is
1655 * intended for careful use by the ATAPI tape/cdrom driver code.
1657 * If action is ide_end, then the rq is queued at the end of the
1658 * request queue, and the function returns immediately without waiting
1659 * for the new rq to be completed. This is again intended for careful
1660 * use by the ATAPI tape/cdrom driver code.
1663 int ide_do_drive_cmd (ide_drive_t *drive, struct request *rq, ide_action_t action)
1665 unsigned long flags;
1666 ide_hwgroup_t *hwgroup = HWGROUP(drive);
1667 DECLARE_COMPLETION(wait);
1668 int where = ELEVATOR_INSERT_BACK, err;
1669 int must_wait = (action == ide_wait || action == ide_head_wait);
1671 rq->errors = 0;
1672 rq->rq_status = RQ_ACTIVE;
1675 * we need to hold an extra reference to request for safe inspection
1676 * after completion
1678 if (must_wait) {
1679 rq->ref_count++;
1680 rq->waiting = &wait;
1681 rq->end_io = blk_end_sync_rq;
1684 spin_lock_irqsave(&ide_lock, flags);
1685 if (action == ide_preempt)
1686 hwgroup->rq = NULL;
1687 if (action == ide_preempt || action == ide_head_wait) {
1688 where = ELEVATOR_INSERT_FRONT;
1689 rq->flags |= REQ_PREEMPT;
1691 __elv_add_request(drive->queue, rq, where, 0);
1692 ide_do_request(hwgroup, IDE_NO_IRQ);
1693 spin_unlock_irqrestore(&ide_lock, flags);
1695 err = 0;
1696 if (must_wait) {
1697 wait_for_completion(&wait);
1698 rq->waiting = NULL;
1699 if (rq->errors)
1700 err = -EIO;
1702 blk_put_request(rq);
1705 return err;
1708 EXPORT_SYMBOL(ide_do_drive_cmd);