4 * Basic PIO and command management functionality.
6 * This code was split off from ide.c. See ide.c for history and original
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
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>
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>
51 #include <asm/byteorder.h>
53 #include <asm/uaccess.h>
55 #include <asm/bitops.h>
57 #include "ide_modes.h"
59 #if (DISK_RECOVERY_TIME > 0)
61 Error So the User Has To Fix the Compilation And Stop Hacking Port
0x43
62 Does anyone ever use
this anyway
??
65 * For really screwy hardware (hey, at least it *can* be used with Linux)
66 * we can enforce a minimum delay time between successive operations.
68 static unsigned long read_timer (ide_hwif_t
*hwif
)
70 unsigned long t
, flags
;
73 /* FIXME this is completely unsafe! */
74 local_irq_save(flags
);
78 i
|= inb_p(0x40) << 8;
79 local_irq_restore(flags
);
82 #endif /* DISK_RECOVERY_TIME */
84 static inline void set_recovery_timer (ide_hwif_t
*hwif
)
86 #if (DISK_RECOVERY_TIME > 0)
87 hwif
->last_time
= read_timer(hwif
);
88 #endif /* DISK_RECOVERY_TIME */
92 * ide_end_request - complete an IDE I/O
93 * @drive: IDE device for the I/O
95 * @nr_sectors: number of sectors completed
97 * This is our end_request wrapper function. We complete the I/O
98 * update random number input and dequeue the request, which if
99 * it was tagged may be out of order.
102 int ide_end_request (ide_drive_t
*drive
, int uptodate
, int nr_sectors
)
108 spin_lock_irqsave(&ide_lock
, flags
);
109 rq
= HWGROUP(drive
)->rq
;
111 BUG_ON(!(rq
->flags
& REQ_STARTED
));
114 nr_sectors
= rq
->hard_cur_sectors
;
117 * decide whether to reenable DMA -- 3 is a random magic for now,
118 * if we DMA timeout more than 3 times, just stay in PIO
120 if (drive
->state
== DMA_PIO_RETRY
&& drive
->retry_pio
<= 3) {
122 HWGROUP(drive
)->hwif
->ide_dma_on(drive
);
125 if (!end_that_request_first(rq
, uptodate
, nr_sectors
)) {
126 add_disk_randomness(rq
->rq_disk
);
127 if (!blk_rq_tagged(rq
))
128 blkdev_dequeue_request(rq
);
130 blk_queue_end_tag(&drive
->queue
, rq
);
131 HWGROUP(drive
)->rq
= NULL
;
132 end_that_request_last(rq
);
135 spin_unlock_irqrestore(&ide_lock
, flags
);
139 EXPORT_SYMBOL(ide_end_request
);
142 * ide_complete_pm_request - end the current Power Management request
143 * @drive: target drive
146 * This function cleans up the current PM request and stops the queue
149 static void ide_complete_pm_request (ide_drive_t
*drive
, struct request
*rq
)
154 printk("%s: completing PM request, %s\n", drive
->name
,
155 blk_pm_suspend_request(rq
) ? "suspend" : "resume");
157 spin_lock_irqsave(&ide_lock
, flags
);
158 if (blk_pm_suspend_request(rq
)) {
159 blk_stop_queue(&drive
->queue
);
162 blk_start_queue(&drive
->queue
);
164 blkdev_dequeue_request(rq
);
165 HWGROUP(drive
)->rq
= NULL
;
166 end_that_request_last(rq
);
167 spin_unlock_irqrestore(&ide_lock
, flags
);
171 * ide_end_drive_cmd - end an explicit drive command
176 * Clean up after success/failure of an explicit drive command.
177 * These get thrown onto the queue so they are synchronized with
178 * real I/O operations on the drive.
180 * In LBA48 mode we have to read the register set twice to get
181 * all the extra information out.
184 void ide_end_drive_cmd (ide_drive_t
*drive
, u8 stat
, u8 err
)
186 ide_hwif_t
*hwif
= HWIF(drive
);
190 spin_lock_irqsave(&ide_lock
, flags
);
191 rq
= HWGROUP(drive
)->rq
;
192 spin_unlock_irqrestore(&ide_lock
, flags
);
194 if (rq
->flags
& REQ_DRIVE_CMD
) {
195 u8
*args
= (u8
*) rq
->buffer
;
197 rq
->errors
= !OK_STAT(stat
,READY_STAT
,BAD_STAT
);
202 args
[2] = hwif
->INB(IDE_NSECTOR_REG
);
204 } else if (rq
->flags
& REQ_DRIVE_TASK
) {
205 u8
*args
= (u8
*) rq
->buffer
;
207 rq
->errors
= !OK_STAT(stat
,READY_STAT
,BAD_STAT
);
212 args
[2] = hwif
->INB(IDE_NSECTOR_REG
);
213 args
[3] = hwif
->INB(IDE_SECTOR_REG
);
214 args
[4] = hwif
->INB(IDE_LCYL_REG
);
215 args
[5] = hwif
->INB(IDE_HCYL_REG
);
216 args
[6] = hwif
->INB(IDE_SELECT_REG
);
218 } else if (rq
->flags
& REQ_DRIVE_TASKFILE
) {
219 ide_task_t
*args
= (ide_task_t
*) rq
->special
;
221 rq
->errors
= !OK_STAT(stat
,READY_STAT
,BAD_STAT
);
224 if (args
->tf_in_flags
.b
.data
) {
225 u16 data
= hwif
->INW(IDE_DATA_REG
);
226 args
->tfRegister
[IDE_DATA_OFFSET
] = (data
) & 0xFF;
227 args
->hobRegister
[IDE_DATA_OFFSET_HOB
] = (data
>> 8) & 0xFF;
229 args
->tfRegister
[IDE_ERROR_OFFSET
] = err
;
230 args
->tfRegister
[IDE_NSECTOR_OFFSET
] = hwif
->INB(IDE_NSECTOR_REG
);
231 args
->tfRegister
[IDE_SECTOR_OFFSET
] = hwif
->INB(IDE_SECTOR_REG
);
232 args
->tfRegister
[IDE_LCYL_OFFSET
] = hwif
->INB(IDE_LCYL_REG
);
233 args
->tfRegister
[IDE_HCYL_OFFSET
] = hwif
->INB(IDE_HCYL_REG
);
234 args
->tfRegister
[IDE_SELECT_OFFSET
] = hwif
->INB(IDE_SELECT_REG
);
235 args
->tfRegister
[IDE_STATUS_OFFSET
] = stat
;
237 if (drive
->addressing
== 1) {
238 hwif
->OUTB(drive
->ctl
|0x80, IDE_CONTROL_REG_HOB
);
239 args
->hobRegister
[IDE_FEATURE_OFFSET_HOB
] = hwif
->INB(IDE_FEATURE_REG
);
240 args
->hobRegister
[IDE_NSECTOR_OFFSET_HOB
] = hwif
->INB(IDE_NSECTOR_REG
);
241 args
->hobRegister
[IDE_SECTOR_OFFSET_HOB
] = hwif
->INB(IDE_SECTOR_REG
);
242 args
->hobRegister
[IDE_LCYL_OFFSET_HOB
] = hwif
->INB(IDE_LCYL_REG
);
243 args
->hobRegister
[IDE_HCYL_OFFSET_HOB
] = hwif
->INB(IDE_HCYL_REG
);
246 } else if (blk_pm_request(rq
)) {
248 printk("%s: complete_power_step(step: %d, stat: %x, err: %x)\n",
249 drive
->name
, rq
->pm
->pm_step
, stat
, err
);
251 DRIVER(drive
)->complete_power_step(drive
, rq
, stat
, err
);
252 if (rq
->pm
->pm_step
== ide_pm_state_completed
)
253 ide_complete_pm_request(drive
, rq
);
257 spin_lock_irqsave(&ide_lock
, flags
);
258 blkdev_dequeue_request(rq
);
259 HWGROUP(drive
)->rq
= NULL
;
260 end_that_request_last(rq
);
261 spin_unlock_irqrestore(&ide_lock
, flags
);
264 EXPORT_SYMBOL(ide_end_drive_cmd
);
267 * try_to_flush_leftover_data - flush junk
268 * @drive: drive to flush
270 * try_to_flush_leftover_data() is invoked in response to a drive
271 * unexpectedly having its DRQ_STAT bit set. As an alternative to
272 * resetting the drive, this routine tries to clear the condition
273 * by read a sector's worth of data from the drive. Of course,
274 * this may not help if the drive is *waiting* for data from *us*.
276 void try_to_flush_leftover_data (ide_drive_t
*drive
)
278 int i
= (drive
->mult_count
? drive
->mult_count
: 1) * SECTOR_WORDS
;
280 if (drive
->media
!= ide_disk
)
284 u32 wcount
= (i
> 16) ? 16 : i
;
287 HWIF(drive
)->ata_input_data(drive
, buffer
, wcount
);
291 EXPORT_SYMBOL(try_to_flush_leftover_data
);
294 * FIXME Add an ATAPI error
298 * ide_error - handle an error on the IDE
299 * @drive: drive the error occurred on
300 * @msg: message to report
303 * ide_error() takes action based on the error returned by the drive.
304 * For normal I/O that may well include retries. We deal with
305 * both new-style (taskfile) and old style command handling here.
306 * In the case of taskfile command handling there is work left to
310 ide_startstop_t
ide_error (ide_drive_t
*drive
, const char *msg
, u8 stat
)
316 err
= ide_dump_status(drive
, msg
, stat
);
317 if (drive
== NULL
|| (rq
= HWGROUP(drive
)->rq
) == NULL
)
321 /* retry only "normal" I/O: */
322 if (rq
->flags
& (REQ_DRIVE_CMD
| REQ_DRIVE_TASK
)) {
324 ide_end_drive_cmd(drive
, stat
, err
);
327 if (rq
->flags
& REQ_DRIVE_TASKFILE
) {
329 ide_end_drive_cmd(drive
, stat
, err
);
330 // ide_end_taskfile(drive, stat, err);
334 if (stat
& BUSY_STAT
|| ((stat
& WRERR_STAT
) && !drive
->nowerr
)) {
335 /* other bits are useless when BUSY */
336 rq
->errors
|= ERROR_RESET
;
338 if (drive
->media
!= ide_disk
)
341 if (stat
& ERR_STAT
) {
342 /* err has different meaning on cdrom and tape */
343 if (err
== ABRT_ERR
) {
344 if (drive
->select
.b
.lba
&&
345 (hwif
->INB(IDE_COMMAND_REG
) == WIN_SPECIFY
))
346 /* some newer drives don't
347 * support WIN_SPECIFY
350 } else if ((err
& BAD_CRC
) == BAD_CRC
) {
352 /* UDMA crc error -- just retry the operation */
353 } else if (err
& (BBD_ERR
| ECC_ERR
)) {
354 /* retries won't help these */
355 rq
->errors
= ERROR_MAX
;
356 } else if (err
& TRK0_ERR
) {
357 /* help it find track zero */
358 rq
->errors
|= ERROR_RECAL
;
362 if ((stat
& DRQ_STAT
) && rq_data_dir(rq
) != WRITE
)
363 try_to_flush_leftover_data(drive
);
365 if (hwif
->INB(IDE_STATUS_REG
) & (BUSY_STAT
|DRQ_STAT
)) {
367 hwif
->OUTB(WIN_IDLEIMMEDIATE
,IDE_COMMAND_REG
);
369 if (rq
->errors
>= ERROR_MAX
) {
370 DRIVER(drive
)->end_request(drive
, 0, 0);
372 if ((rq
->errors
& ERROR_RESET
) == ERROR_RESET
) {
374 return ide_do_reset(drive
);
376 if ((rq
->errors
& ERROR_RECAL
) == ERROR_RECAL
)
377 drive
->special
.b
.recalibrate
= 1;
383 EXPORT_SYMBOL(ide_error
);
386 * ide_abort - abort pending IDE operatins
387 * @drive: drive the error occurred on
388 * @msg: message to report
390 * ide_abort kills and cleans up when we are about to do a
391 * host initiated reset on active commands. Longer term we
392 * want handlers to have sensible abort handling themselves
394 * This differs fundamentally from ide_error because in
395 * this case the command is doing just fine when we
399 ide_startstop_t
ide_abort(ide_drive_t
*drive
, const char *msg
)
404 if (drive
== NULL
|| (rq
= HWGROUP(drive
)->rq
) == NULL
)
408 /* retry only "normal" I/O: */
409 if (rq
->flags
& (REQ_DRIVE_CMD
| REQ_DRIVE_TASK
)) {
411 ide_end_drive_cmd(drive
, BUSY_STAT
, 0);
414 if (rq
->flags
& REQ_DRIVE_TASKFILE
) {
416 ide_end_drive_cmd(drive
, BUSY_STAT
, 0);
417 // ide_end_taskfile(drive, BUSY_STAT, 0);
421 rq
->errors
|= ERROR_RESET
;
422 DRIVER(drive
)->end_request(drive
, 0, 0);
426 EXPORT_SYMBOL(ide_abort
);
429 * ide_cmd - issue a simple drive command
430 * @drive: drive the command is for
432 * @nsect: sector byte
433 * @handler: handler for the command completion
435 * Issue a simple drive command with interrupts.
436 * The drive must be selected beforehand.
439 void ide_cmd (ide_drive_t
*drive
, u8 cmd
, u8 nsect
, ide_handler_t
*handler
)
441 ide_hwif_t
*hwif
= HWIF(drive
);
443 hwif
->OUTB(drive
->ctl
,IDE_CONTROL_REG
); /* clear nIEN */
444 SELECT_MASK(drive
,0);
445 hwif
->OUTB(nsect
,IDE_NSECTOR_REG
);
446 ide_execute_command(drive
, cmd
, handler
, WAIT_CMD
, NULL
);
449 EXPORT_SYMBOL(ide_cmd
);
452 * drive_cmd_intr - drive command completion interrupt
453 * @drive: drive the completion interrupt occurred on
455 * drive_cmd_intr() is invoked on completion of a special DRIVE_CMD.
456 * We do any necessary daya reading and then wait for the drive to
457 * go non busy. At that point we may read the error data and complete
461 ide_startstop_t
drive_cmd_intr (ide_drive_t
*drive
)
463 struct request
*rq
= HWGROUP(drive
)->rq
;
464 ide_hwif_t
*hwif
= HWIF(drive
);
465 u8
*args
= (u8
*) rq
->buffer
;
466 u8 stat
= hwif
->INB(IDE_STATUS_REG
);
470 if ((stat
& DRQ_STAT
) && args
&& args
[3]) {
471 u8 io_32bit
= drive
->io_32bit
;
473 hwif
->ata_input_data(drive
, &args
[4], args
[3] * SECTOR_WORDS
);
474 drive
->io_32bit
= io_32bit
;
475 while (((stat
= hwif
->INB(IDE_STATUS_REG
)) & BUSY_STAT
) && retries
--)
479 if (!OK_STAT(stat
, READY_STAT
, BAD_STAT
) && DRIVER(drive
) != NULL
)
480 return DRIVER(drive
)->error(drive
, "drive_cmd", stat
);
481 /* calls ide_end_drive_cmd */
482 ide_end_drive_cmd(drive
, stat
, hwif
->INB(IDE_ERROR_REG
));
486 EXPORT_SYMBOL(drive_cmd_intr
);
489 * do_special - issue some special commands
490 * @drive: drive the command is for
492 * do_special() is used to issue WIN_SPECIFY, WIN_RESTORE, and WIN_SETMULT
493 * commands to a drive. It used to do much more, but has been scaled
497 ide_startstop_t
do_special (ide_drive_t
*drive
)
499 special_t
*s
= &drive
->special
;
502 printk("%s: do_special: 0x%02x\n", drive
->name
, s
->all
);
506 if (HWIF(drive
)->tuneproc
!= NULL
)
507 HWIF(drive
)->tuneproc(drive
, drive
->tune_req
);
511 return DRIVER(drive
)->special(drive
);
514 EXPORT_SYMBOL(do_special
);
517 * execute_drive_command - issue special drive command
518 * @drive: the drive to issue th command on
519 * @rq: the request structure holding the command
521 * execute_drive_cmd() issues a special drive command, usually
522 * initiated by ioctl() from the external hdparm program. The
523 * command can be a drive command, drive task or taskfile
524 * operation. Weirdly you can call it with NULL to wait for
525 * all commands to finish. Don't do this as that is due to change
528 ide_startstop_t
execute_drive_cmd (ide_drive_t
*drive
, struct request
*rq
)
530 ide_hwif_t
*hwif
= HWIF(drive
);
531 if (rq
->flags
& REQ_DRIVE_TASKFILE
) {
532 ide_task_t
*args
= rq
->special
;
537 if (args
->tf_out_flags
.all
!= 0)
538 return flagged_taskfile(drive
, args
);
539 return do_rw_taskfile(drive
, args
);
540 } else if (rq
->flags
& REQ_DRIVE_TASK
) {
541 u8
*args
= rq
->buffer
;
547 printk("%s: DRIVE_TASK_CMD ", drive
->name
);
548 printk("cmd=0x%02x ", args
[0]);
549 printk("fr=0x%02x ", args
[1]);
550 printk("ns=0x%02x ", args
[2]);
551 printk("sc=0x%02x ", args
[3]);
552 printk("lcyl=0x%02x ", args
[4]);
553 printk("hcyl=0x%02x ", args
[5]);
554 printk("sel=0x%02x\n", args
[6]);
556 hwif
->OUTB(args
[1], IDE_FEATURE_REG
);
557 hwif
->OUTB(args
[3], IDE_SECTOR_REG
);
558 hwif
->OUTB(args
[4], IDE_LCYL_REG
);
559 hwif
->OUTB(args
[5], IDE_HCYL_REG
);
560 sel
= (args
[6] & ~0x10);
561 if (drive
->select
.b
.unit
)
563 hwif
->OUTB(sel
, IDE_SELECT_REG
);
564 ide_cmd(drive
, args
[0], args
[2], &drive_cmd_intr
);
566 } else if (rq
->flags
& REQ_DRIVE_CMD
) {
567 u8
*args
= rq
->buffer
;
572 printk("%s: DRIVE_CMD ", drive
->name
);
573 printk("cmd=0x%02x ", args
[0]);
574 printk("sc=0x%02x ", args
[1]);
575 printk("fr=0x%02x ", args
[2]);
576 printk("xx=0x%02x\n", args
[3]);
578 if (args
[0] == WIN_SMART
) {
579 hwif
->OUTB(0x4f, IDE_LCYL_REG
);
580 hwif
->OUTB(0xc2, IDE_HCYL_REG
);
581 hwif
->OUTB(args
[2],IDE_FEATURE_REG
);
582 hwif
->OUTB(args
[1],IDE_SECTOR_REG
);
583 ide_cmd(drive
, args
[0], args
[3], &drive_cmd_intr
);
586 hwif
->OUTB(args
[2],IDE_FEATURE_REG
);
587 ide_cmd(drive
, args
[0], args
[1], &drive_cmd_intr
);
593 * NULL is actually a valid way of waiting for
594 * all current requests to be flushed from the queue.
597 printk("%s: DRIVE_CMD (null)\n", drive
->name
);
599 ide_end_drive_cmd(drive
,
600 hwif
->INB(IDE_STATUS_REG
),
601 hwif
->INB(IDE_ERROR_REG
));
605 EXPORT_SYMBOL(execute_drive_cmd
);
608 * start_request - start of I/O and command issuing for IDE
610 * start_request() initiates handling of a new I/O request. It
611 * accepts commands and I/O (read/write) requests. It also does
612 * the final remapping for weird stuff like EZDrive. Once
613 * device mapper can work sector level the EZDrive stuff can go away
615 * FIXME: this function needs a rename
618 ide_startstop_t
start_request (ide_drive_t
*drive
, struct request
*rq
)
620 ide_startstop_t startstop
;
623 BUG_ON(!(rq
->flags
& REQ_STARTED
));
626 printk("%s: start_request: current=0x%08lx\n",
627 HWIF(drive
)->name
, (unsigned long) rq
);
630 /* bail early if we've exceeded max_failures */
631 if (drive
->max_failures
&& (drive
->failures
> drive
->max_failures
)) {
636 * bail early if we've sent a device to sleep, however how to wake
637 * this needs to be a masked flag. FIXME for proper operations.
639 if (drive
->suspend_reset
)
643 if (blk_fs_request(rq
) &&
644 (drive
->media
== ide_disk
|| drive
->media
== ide_floppy
)) {
645 block
+= drive
->sect0
;
647 /* Yecch - this will shift the entire interval,
648 possibly killing some innocent following sector */
649 if (block
== 0 && drive
->remap_0_to_1
== 1)
650 block
= 1; /* redirect MBR access to EZ-Drive partn table */
652 #if (DISK_RECOVERY_TIME > 0)
653 while ((read_timer() - HWIF(drive
)->last_time
) < DISK_RECOVERY_TIME
);
656 if (blk_pm_suspend_request(rq
) &&
657 rq
->pm
->pm_step
== ide_pm_state_start_suspend
)
658 /* Mark drive blocked when starting the suspend sequence. */
660 else if (blk_pm_resume_request(rq
) &&
661 rq
->pm
->pm_step
== ide_pm_state_start_resume
) {
663 * The first thing we do on wakeup is to wait for BSY bit to
664 * go away (with a looong timeout) as a drive on this hwif may
665 * just be POSTing itself.
666 * We do that before even selecting as the "other" device on
667 * the bus may be broken enough to walk on our toes at this
672 printk("%s: Wakeup request inited, waiting for !BSY...\n", drive
->name
);
674 rc
= ide_wait_not_busy(HWIF(drive
), 35000);
676 printk(KERN_WARNING
"%s: bus not ready on wakeup\n", drive
->name
);
678 HWIF(drive
)->OUTB(8, HWIF(drive
)->io_ports
[IDE_CONTROL_OFFSET
]);
679 rc
= ide_wait_not_busy(HWIF(drive
), 10000);
681 printk(KERN_WARNING
"%s: drive not ready on wakeup\n", drive
->name
);
685 if (ide_wait_stat(&startstop
, drive
, drive
->ready_stat
, BUSY_STAT
|DRQ_STAT
, WAIT_READY
)) {
686 printk(KERN_ERR
"%s: drive not ready for command\n", drive
->name
);
689 if (!drive
->special
.all
) {
690 if (rq
->flags
& (REQ_DRIVE_CMD
| REQ_DRIVE_TASK
))
691 return execute_drive_cmd(drive
, rq
);
692 else if (rq
->flags
& REQ_DRIVE_TASKFILE
)
693 return execute_drive_cmd(drive
, rq
);
694 else if (blk_pm_request(rq
)) {
696 printk("%s: start_power_step(step: %d)\n",
697 drive
->name
, rq
->pm
->pm_step
);
699 startstop
= DRIVER(drive
)->start_power_step(drive
, rq
);
700 if (startstop
== ide_stopped
&&
701 rq
->pm
->pm_step
== ide_pm_state_completed
)
702 ide_complete_pm_request(drive
, rq
);
705 return (DRIVER(drive
)->do_request(drive
, rq
, block
));
707 return do_special(drive
);
709 DRIVER(drive
)->end_request(drive
, 0, 0);
713 EXPORT_SYMBOL(start_request
);
716 * ide_stall_queue - pause an IDE device
717 * @drive: drive to stall
718 * @timeout: time to stall for (jiffies)
720 * ide_stall_queue() can be used by a drive to give excess bandwidth back
721 * to the hwgroup by sleeping for timeout jiffies.
724 void ide_stall_queue (ide_drive_t
*drive
, unsigned long timeout
)
726 if (timeout
> WAIT_WORSTCASE
)
727 timeout
= WAIT_WORSTCASE
;
728 drive
->sleep
= timeout
+ jiffies
;
731 EXPORT_SYMBOL(ide_stall_queue
);
733 #define WAKEUP(drive) ((drive)->service_start + 2 * (drive)->service_time)
736 * choose_drive - select a drive to service
737 * @hwgroup: hardware group to select on
739 * choose_drive() selects the next drive which will be serviced.
740 * This is necessary because the IDE layer can't issue commands
741 * to both drives on the same cable, unlike SCSI.
744 static inline ide_drive_t
*choose_drive (ide_hwgroup_t
*hwgroup
)
746 ide_drive_t
*drive
, *best
;
750 drive
= hwgroup
->drive
;
752 if ((!drive
->sleep
|| time_after_eq(jiffies
, drive
->sleep
))
753 && !elv_queue_empty(&drive
->queue
)) {
755 || (drive
->sleep
&& (!best
->sleep
|| 0 < (signed long)(best
->sleep
- drive
->sleep
)))
756 || (!best
->sleep
&& 0 < (signed long)(WAKEUP(best
) - WAKEUP(drive
))))
758 if (!blk_queue_plugged(&drive
->queue
))
762 } while ((drive
= drive
->next
) != hwgroup
->drive
);
763 if (best
&& best
->nice1
&& !best
->sleep
&& best
!= hwgroup
->drive
&& best
->service_time
> WAIT_MIN_SLEEP
) {
764 long t
= (signed long)(WAKEUP(best
) - jiffies
);
765 if (t
>= WAIT_MIN_SLEEP
) {
767 * We *may* have some time to spare, but first let's see if
768 * someone can potentially benefit from our nice mood today..
773 /* FIXME: use time_before */
774 && 0 < (signed long)(WAKEUP(drive
) - (jiffies
- best
->service_time
))
775 && 0 < (signed long)((jiffies
+ t
) - WAKEUP(drive
)))
777 ide_stall_queue(best
, IDE_MIN(t
, 10 * WAIT_MIN_SLEEP
));
780 } while ((drive
= drive
->next
) != best
);
787 * Issue a new request to a drive from hwgroup
788 * Caller must have already done spin_lock_irqsave(&ide_lock, ..);
790 * A hwgroup is a serialized group of IDE interfaces. Usually there is
791 * exactly one hwif (interface) per hwgroup, but buggy controllers (eg. CMD640)
792 * may have both interfaces in a single hwgroup to "serialize" access.
793 * Or possibly multiple ISA interfaces can share a common IRQ by being grouped
794 * together into one hwgroup for serialized access.
796 * Note also that several hwgroups can end up sharing a single IRQ,
797 * possibly along with many other devices. This is especially common in
798 * PCI-based systems with off-board IDE controller cards.
800 * The IDE driver uses the single global ide_lock spinlock to protect
801 * access to the request queues, and to protect the hwgroup->busy flag.
803 * The first thread into the driver for a particular hwgroup sets the
804 * hwgroup->busy flag to indicate that this hwgroup is now active,
805 * and then initiates processing of the top request from the request queue.
807 * Other threads attempting entry notice the busy setting, and will simply
808 * queue their new requests and exit immediately. Note that hwgroup->busy
809 * remains set even when the driver is merely awaiting the next interrupt.
810 * Thus, the meaning is "this hwgroup is busy processing a request".
812 * When processing of a request completes, the completing thread or IRQ-handler
813 * will start the next request from the queue. If no more work remains,
814 * the driver will clear the hwgroup->busy flag and exit.
816 * The ide_lock (spinlock) is used to protect all access to the
817 * hwgroup->busy flag, but is otherwise not needed for most processing in
818 * the driver. This makes the driver much more friendlier to shared IRQs
819 * than previous designs, while remaining 100% (?) SMP safe and capable.
821 /* --BenH: made non-static as ide-pmac.c uses it to kick the hwgroup back
822 * into life on wakeup from machine sleep.
824 void ide_do_request (ide_hwgroup_t
*hwgroup
, int masked_irq
)
829 ide_startstop_t startstop
;
831 /* for atari only: POSSIBLY BROKEN HERE(?) */
832 ide_get_lock(ide_intr
, hwgroup
);
834 /* caller must own ide_lock */
835 BUG_ON(!irqs_disabled());
837 while (!hwgroup
->busy
) {
839 drive
= choose_drive(hwgroup
);
841 unsigned long sleep
= 0;
843 drive
= hwgroup
->drive
;
845 if (drive
->sleep
&& (!sleep
|| 0 < (signed long)(sleep
- drive
->sleep
)))
846 sleep
= drive
->sleep
;
847 } while ((drive
= drive
->next
) != hwgroup
->drive
);
850 * Take a short snooze, and then wake up this hwgroup again.
851 * This gives other hwgroups on the same a chance to
852 * play fairly with us, just in case there are big differences
853 * in relative throughputs.. don't want to hog the cpu too much.
855 if (time_before(sleep
, jiffies
+ WAIT_MIN_SLEEP
))
856 sleep
= jiffies
+ WAIT_MIN_SLEEP
;
858 if (timer_pending(&hwgroup
->timer
))
859 printk(KERN_CRIT
"ide_set_handler: timer already active\n");
861 /* so that ide_timer_expiry knows what to do */
862 hwgroup
->sleeping
= 1;
863 mod_timer(&hwgroup
->timer
, sleep
);
864 /* we purposely leave hwgroup->busy==1
867 /* Ugly, but how can we sleep for the lock
868 * otherwise? perhaps from tq_disk?
876 /* no more work for this hwgroup (for now) */
880 if (hwgroup
->hwif
->sharing_irq
&&
881 hwif
!= hwgroup
->hwif
&&
882 hwif
->io_ports
[IDE_CONTROL_OFFSET
]) {
883 /* set nIEN for previous hwif */
884 SELECT_INTERRUPT(drive
);
886 hwgroup
->hwif
= hwif
;
887 hwgroup
->drive
= drive
;
889 drive
->service_start
= jiffies
;
892 if (!ata_can_queue(drive
)) {
893 if (!ata_pending_commands(drive
))
899 if (blk_queue_plugged(&drive
->queue
)) {
900 if (drive
->using_tcq
)
903 printk(KERN_ERR
"ide: huh? queue was plugged!\n");
908 * we know that the queue isn't empty, but this can happen
909 * if the q->prep_rq_fn() decides to kill a request
911 rq
= elv_next_request(&drive
->queue
);
913 hwgroup
->busy
= !!ata_pending_commands(drive
);
918 * Sanity: don't accept a request that isn't a PM request
919 * if we are currently power managed. This is very important as
920 * blk_stop_queue() doesn't prevent the elv_next_request()
921 * above to return us whatever is in the queue. Since we call
922 * ide_do_request() ourselves, we end up taking requests while
923 * the queue is blocked...
925 * We let requests forced at head of queue with ide-preempt
926 * though. I hope that doesn't happen too much, hopefully not
927 * unless the subdriver triggers such a thing in it's own PM
930 if (drive
->blocked
&& !blk_pm_request(rq
) && !(rq
->flags
& REQ_PREEMPT
)) {
932 printk("%s: a request made it's way while we are power managing...\n", drive
->name
);
934 /* We clear busy, there should be no pending ATA command at this point. */
939 if (!rq
->bio
&& ata_pending_commands(drive
))
945 * Some systems have trouble with IDE IRQs arriving while
946 * the driver is still setting things up. So, here we disable
947 * the IRQ used by this interface while the request is being started.
948 * This may look bad at first, but pretty much the same thing
949 * happens anyway when any interrupt comes in, IDE or otherwise
950 * -- the kernel masks the IRQ while it is being handled.
952 if (masked_irq
!= IDE_NO_IRQ
&& hwif
->irq
!= masked_irq
)
953 disable_irq_nosync(hwif
->irq
);
954 spin_unlock(&ide_lock
);
956 /* allow other IRQs while we start this request */
957 startstop
= start_request(drive
, rq
);
958 spin_lock_irq(&ide_lock
);
959 if (masked_irq
!= IDE_NO_IRQ
&& hwif
->irq
!= masked_irq
)
960 enable_irq(hwif
->irq
);
961 if (startstop
== ide_released
)
963 if (startstop
== ide_stopped
)
968 EXPORT_SYMBOL(ide_do_request
);
971 * Passes the stuff to ide_do_request
973 void do_ide_request(request_queue_t
*q
)
975 ide_do_request(q
->queuedata
, IDE_NO_IRQ
);
979 * un-busy the hwgroup etc, and clear any pending DMA status. we want to
980 * retry the current request in pio mode instead of risking tossing it
983 void ide_dma_timeout_retry(ide_drive_t
*drive
)
985 ide_hwif_t
*hwif
= HWIF(drive
);
989 * end current dma transaction
991 (void) hwif
->ide_dma_end(drive
);
994 * complain a little, later we might remove some of this verbosity
996 printk(KERN_WARNING
"%s: timeout waiting for DMA\n", drive
->name
);
997 (void) hwif
->ide_dma_timeout(drive
);
1000 * disable dma for now, but remember that we did so because of
1001 * a timeout -- we'll reenable after we finish this next request
1002 * (or rather the first chunk of it) in pio.
1005 drive
->state
= DMA_PIO_RETRY
;
1006 (void) hwif
->ide_dma_off_quietly(drive
);
1009 * un-busy drive etc (hwgroup->busy is cleared on return) and
1010 * make sure request is sane
1012 rq
= HWGROUP(drive
)->rq
;
1013 HWGROUP(drive
)->rq
= NULL
;
1016 rq
->sector
= rq
->bio
->bi_sector
;
1017 rq
->current_nr_sectors
= bio_iovec(rq
->bio
)->bv_len
>> 9;
1018 rq
->hard_cur_sectors
= rq
->current_nr_sectors
;
1023 EXPORT_SYMBOL(ide_dma_timeout_retry
);
1026 * ide_timer_expiry - handle lack of an IDE interrupt
1027 * @data: timer callback magic (hwgroup)
1029 * An IDE command has timed out before the expected drive return
1030 * occurred. At this point we attempt to clean up the current
1031 * mess. If the current handler includes an expiry handler then
1032 * we invoke the expiry handler, and providing it is happy the
1033 * work is done. If that fails we apply generic recovery rules
1034 * invoking the handler and checking the drive DMA status. We
1035 * have an excessively incestuous relationship with the DMA
1036 * logic that wants cleaning up.
1039 void ide_timer_expiry (unsigned long data
)
1041 ide_hwgroup_t
*hwgroup
= (ide_hwgroup_t
*) data
;
1042 ide_handler_t
*handler
;
1043 ide_expiry_t
*expiry
;
1044 unsigned long flags
;
1047 spin_lock_irqsave(&ide_lock
, flags
);
1048 del_timer(&hwgroup
->timer
);
1050 if ((handler
= hwgroup
->handler
) == NULL
) {
1052 * Either a marginal timeout occurred
1053 * (got the interrupt just as timer expired),
1054 * or we were "sleeping" to give other devices a chance.
1055 * Either way, we don't really want to complain about anything.
1057 if (hwgroup
->sleeping
) {
1058 hwgroup
->sleeping
= 0;
1062 ide_drive_t
*drive
= hwgroup
->drive
;
1064 printk(KERN_ERR
"ide_timer_expiry: hwgroup->drive was NULL\n");
1065 hwgroup
->handler
= NULL
;
1068 ide_startstop_t startstop
= ide_stopped
;
1069 if (!hwgroup
->busy
) {
1070 hwgroup
->busy
= 1; /* paranoia */
1071 printk(KERN_ERR
"%s: ide_timer_expiry: hwgroup->busy was 0 ??\n", drive
->name
);
1073 if ((expiry
= hwgroup
->expiry
) != NULL
) {
1075 if ((wait
= expiry(drive
)) != 0) {
1077 hwgroup
->timer
.expires
= jiffies
+ wait
;
1078 add_timer(&hwgroup
->timer
);
1079 spin_unlock_irqrestore(&ide_lock
, flags
);
1083 hwgroup
->handler
= NULL
;
1085 * We need to simulate a real interrupt when invoking
1086 * the handler() function, which means we need to
1087 * globally mask the specific IRQ:
1089 spin_unlock(&ide_lock
);
1091 #if DISABLE_IRQ_NOSYNC
1092 disable_irq_nosync(hwif
->irq
);
1094 /* disable_irq_nosync ?? */
1095 disable_irq(hwif
->irq
);
1096 #endif /* DISABLE_IRQ_NOSYNC */
1098 * as if we were handling an interrupt */
1099 local_irq_disable();
1100 if (hwgroup
->poll_timeout
!= 0) {
1101 startstop
= handler(drive
);
1102 } else if (drive_is_ready(drive
)) {
1103 if (drive
->waiting_for_dma
)
1104 (void) hwgroup
->hwif
->ide_dma_lostirq(drive
);
1105 (void)ide_ack_intr(hwif
);
1106 printk(KERN_WARNING
"%s: lost interrupt\n", drive
->name
);
1107 startstop
= handler(drive
);
1109 if (drive
->waiting_for_dma
) {
1110 startstop
= ide_stopped
;
1111 ide_dma_timeout_retry(drive
);
1113 startstop
= DRIVER(drive
)->error(drive
, "irq timeout", hwif
->INB(IDE_STATUS_REG
));
1115 set_recovery_timer(hwif
);
1116 drive
->service_time
= jiffies
- drive
->service_start
;
1117 enable_irq(hwif
->irq
);
1118 spin_lock_irq(&ide_lock
);
1119 if (startstop
== ide_stopped
)
1123 ide_do_request(hwgroup
, IDE_NO_IRQ
);
1124 spin_unlock_irqrestore(&ide_lock
, flags
);
1127 EXPORT_SYMBOL(ide_timer_expiry
);
1130 * unexpected_intr - handle an unexpected IDE interrupt
1131 * @irq: interrupt line
1132 * @hwgroup: hwgroup being processed
1134 * There's nothing really useful we can do with an unexpected interrupt,
1135 * other than reading the status register (to clear it), and logging it.
1136 * There should be no way that an irq can happen before we're ready for it,
1137 * so we needn't worry much about losing an "important" interrupt here.
1139 * On laptops (and "green" PCs), an unexpected interrupt occurs whenever
1140 * the drive enters "idle", "standby", or "sleep" mode, so if the status
1141 * looks "good", we just ignore the interrupt completely.
1143 * This routine assumes __cli() is in effect when called.
1145 * If an unexpected interrupt happens on irq15 while we are handling irq14
1146 * and if the two interfaces are "serialized" (CMD640), then it looks like
1147 * we could screw up by interfering with a new request being set up for
1150 * In reality, this is a non-issue. The new command is not sent unless
1151 * the drive is ready to accept one, in which case we know the drive is
1152 * not trying to interrupt us. And ide_set_handler() is always invoked
1153 * before completing the issuance of any new drive command, so we will not
1154 * be accidentally invoked as a result of any valid command completion
1157 * Note that we must walk the entire hwgroup here. We know which hwif
1158 * is doing the current command, but we don't know which hwif burped
1162 static void unexpected_intr (int irq
, ide_hwgroup_t
*hwgroup
)
1165 ide_hwif_t
*hwif
= hwgroup
->hwif
;
1168 * handle the unexpected interrupt
1171 if (hwif
->irq
== irq
) {
1172 stat
= hwif
->INB(hwif
->io_ports
[IDE_STATUS_OFFSET
]);
1173 if (!OK_STAT(stat
, READY_STAT
, BAD_STAT
)) {
1174 /* Try to not flood the console with msgs */
1175 static unsigned long last_msgtime
, count
;
1177 if (time_after(jiffies
, last_msgtime
+ HZ
)) {
1178 last_msgtime
= jiffies
;
1179 printk(KERN_ERR
"%s%s: unexpected interrupt, "
1180 "status=0x%02x, count=%ld\n",
1182 (hwif
->next
==hwgroup
->hwif
) ? "" : "(?)", stat
, count
);
1186 } while ((hwif
= hwif
->next
) != hwgroup
->hwif
);
1190 * ide_intr - default IDE interrupt handler
1191 * @irq: interrupt number
1192 * @dev_id: hwif group
1193 * @regs: unused weirdness from the kernel irq layer
1195 * This is the default IRQ handler for the IDE layer. You should
1196 * not need to override it. If you do be aware it is subtle in
1199 * hwgroup->hwif is the interface in the group currently performing
1200 * a command. hwgroup->drive is the drive and hwgroup->handler is
1201 * the IRQ handler to call. As we issue a command the handlers
1202 * step through multiple states, reassigning the handler to the
1203 * next step in the process. Unlike a smart SCSI controller IDE
1204 * expects the main processor to sequence the various transfer
1205 * stages. We also manage a poll timer to catch up with most
1206 * timeout situations. There are still a few where the handlers
1207 * don't ever decide to give up.
1209 * The handler eventually returns ide_stopped to indicate the
1210 * request completed. At this point we issue the next request
1211 * on the hwgroup and the process begins again.
1214 irqreturn_t
ide_intr (int irq
, void *dev_id
, struct pt_regs
*regs
)
1216 unsigned long flags
;
1217 ide_hwgroup_t
*hwgroup
= (ide_hwgroup_t
*)dev_id
;
1220 ide_handler_t
*handler
;
1221 ide_startstop_t startstop
;
1223 spin_lock_irqsave(&ide_lock
, flags
);
1224 hwif
= hwgroup
->hwif
;
1226 if (!ide_ack_intr(hwif
)) {
1227 spin_unlock_irqrestore(&ide_lock
, flags
);
1231 if ((handler
= hwgroup
->handler
) == NULL
||
1232 hwgroup
->poll_timeout
!= 0) {
1234 * Not expecting an interrupt from this drive.
1235 * That means this could be:
1236 * (1) an interrupt from another PCI device
1237 * sharing the same PCI INT# as us.
1238 * or (2) a drive just entered sleep or standby mode,
1239 * and is interrupting to let us know.
1240 * or (3) a spurious interrupt of unknown origin.
1242 * For PCI, we cannot tell the difference,
1243 * so in that case we just ignore it and hope it goes away.
1245 * FIXME: unexpected_intr should be hwif-> then we can
1246 * remove all the ifdef PCI crap
1248 #ifdef CONFIG_BLK_DEV_IDEPCI
1249 if (hwif
->pci_dev
&& !hwif
->pci_dev
->vendor
)
1250 #endif /* CONFIG_BLK_DEV_IDEPCI */
1253 * Probably not a shared PCI interrupt,
1254 * so we can safely try to do something about it:
1256 unexpected_intr(irq
, hwgroup
);
1257 #ifdef CONFIG_BLK_DEV_IDEPCI
1260 * Whack the status register, just in case
1261 * we have a leftover pending IRQ.
1263 (void) hwif
->INB(hwif
->io_ports
[IDE_STATUS_OFFSET
]);
1264 #endif /* CONFIG_BLK_DEV_IDEPCI */
1266 spin_unlock_irqrestore(&ide_lock
, flags
);
1269 drive
= hwgroup
->drive
;
1272 * This should NEVER happen, and there isn't much
1273 * we could do about it here.
1275 * [Note - this can occur if the drive is hot unplugged]
1277 spin_unlock_irqrestore(&ide_lock
, flags
);
1280 if (!drive_is_ready(drive
)) {
1282 * This happens regularly when we share a PCI IRQ with
1283 * another device. Unfortunately, it can also happen
1284 * with some buggy drives that trigger the IRQ before
1285 * their status register is up to date. Hopefully we have
1286 * enough advance overhead that the latter isn't a problem.
1288 spin_unlock_irqrestore(&ide_lock
, flags
);
1291 if (!hwgroup
->busy
) {
1292 hwgroup
->busy
= 1; /* paranoia */
1293 printk(KERN_ERR
"%s: ide_intr: hwgroup->busy was 0 ??\n", drive
->name
);
1295 hwgroup
->handler
= NULL
;
1296 del_timer(&hwgroup
->timer
);
1297 spin_unlock(&ide_lock
);
1301 /* service this interrupt, may set handler for next interrupt */
1302 startstop
= handler(drive
);
1303 spin_lock_irq(&ide_lock
);
1306 * Note that handler() may have set things up for another
1307 * interrupt to occur soon, but it cannot happen until
1308 * we exit from this routine, because it will be the
1309 * same irq as is currently being serviced here, and Linux
1310 * won't allow another of the same (on any CPU) until we return.
1312 set_recovery_timer(HWIF(drive
));
1313 drive
->service_time
= jiffies
- drive
->service_start
;
1314 if (startstop
== ide_stopped
) {
1315 if (hwgroup
->handler
== NULL
) { /* paranoia */
1317 ide_do_request(hwgroup
, hwif
->irq
);
1319 printk(KERN_ERR
"%s: ide_intr: huh? expected NULL handler "
1320 "on exit\n", drive
->name
);
1323 spin_unlock_irqrestore(&ide_lock
, flags
);
1327 EXPORT_SYMBOL(ide_intr
);
1330 * ide_init_drive_cmd - initialize a drive command request
1331 * @rq: request object
1333 * Initialize a request before we fill it in and send it down to
1334 * ide_do_drive_cmd. Commands must be set up by this function. Right
1335 * now it doesn't do a lot, but if that changes abusers will have a
1339 void ide_init_drive_cmd (struct request
*rq
)
1341 memset(rq
, 0, sizeof(*rq
));
1342 rq
->flags
= REQ_DRIVE_CMD
;
1345 EXPORT_SYMBOL(ide_init_drive_cmd
);
1348 * ide_do_drive_cmd - issue IDE special command
1349 * @drive: device to issue command
1350 * @rq: request to issue
1351 * @action: action for processing
1353 * This function issues a special IDE device request
1354 * onto the request queue.
1356 * If action is ide_wait, then the rq is queued at the end of the
1357 * request queue, and the function sleeps until it has been processed.
1358 * This is for use when invoked from an ioctl handler.
1360 * If action is ide_preempt, then the rq is queued at the head of
1361 * the request queue, displacing the currently-being-processed
1362 * request and this function returns immediately without waiting
1363 * for the new rq to be completed. This is VERY DANGEROUS, and is
1364 * intended for careful use by the ATAPI tape/cdrom driver code.
1366 * If action is ide_next, then the rq is queued immediately after
1367 * the currently-being-processed-request (if any), and the function
1368 * returns without waiting for the new rq to be completed. As above,
1369 * This is VERY DANGEROUS, and is intended for careful use by the
1370 * ATAPI tape/cdrom driver code.
1372 * If action is ide_end, then the rq is queued at the end of the
1373 * request queue, and the function returns immediately without waiting
1374 * for the new rq to be completed. This is again intended for careful
1375 * use by the ATAPI tape/cdrom driver code.
1378 int ide_do_drive_cmd (ide_drive_t
*drive
, struct request
*rq
, ide_action_t action
)
1380 unsigned long flags
;
1381 ide_hwgroup_t
*hwgroup
= HWGROUP(drive
);
1382 DECLARE_COMPLETION(wait
);
1383 int insert_end
= 1, err
;
1384 int must_wait
= (action
== ide_wait
|| action
== ide_head_wait
);
1386 #ifdef CONFIG_BLK_DEV_PDC4030
1388 * FIXME: there should be a drive or hwif->special
1389 * handler that points here by default, not hacks
1390 * in the ide-io.c code
1392 * FIXME2: That code breaks power management if used with
1393 * this chipset, that really doesn't belong here !
1395 if (HWIF(drive
)->chipset
== ide_pdc4030
&& rq
->buffer
!= NULL
)
1396 return -ENOSYS
; /* special drive cmds not supported */
1399 rq
->rq_status
= RQ_ACTIVE
;
1401 rq
->rq_disk
= drive
->disk
;
1404 * we need to hold an extra reference to request for safe inspection
1409 rq
->waiting
= &wait
;
1412 spin_lock_irqsave(&ide_lock
, flags
);
1413 if (action
== ide_preempt
|| action
== ide_head_wait
) {
1416 rq
->flags
|= REQ_PREEMPT
;
1418 __elv_add_request(&drive
->queue
, rq
, insert_end
, 0);
1419 ide_do_request(hwgroup
, IDE_NO_IRQ
);
1420 spin_unlock_irqrestore(&ide_lock
, flags
);
1424 wait_for_completion(&wait
);
1428 blk_put_request(rq
);
1434 EXPORT_SYMBOL(ide_do_drive_cmd
);