patch-2_6_7-vs1_9_1_12
[linux-2.6.git] / drivers / ide / ide-io.c
1 /*
2  *      IDE I/O functions
3  *
4  *      Basic PIO and command management functionality.
5  *
6  * This code was split off from ide.c. See ide.c for history and original
7  * copyrights.
8  *
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.
13  *
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.
18  *
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.
24  */
25  
26  
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
51 #include <asm/byteorder.h>
52 #include <asm/irq.h>
53 #include <asm/uaccess.h>
54 #include <asm/io.h>
55 #include <asm/bitops.h>
56
57 /**
58  *      ide_end_request         -       complete an IDE I/O
59  *      @drive: IDE device for the I/O
60  *      @uptodate: 
61  *      @nr_sectors: number of sectors completed
62  *
63  *      This is our end_request wrapper function. We complete the I/O
64  *      update random number input and dequeue the request, which if
65  *      it was tagged may be out of order.
66  */
67  
68 int ide_end_request (ide_drive_t *drive, int uptodate, int nr_sectors)
69 {
70         struct request *rq;
71         unsigned long flags;
72         int ret = 1;
73
74         spin_lock_irqsave(&ide_lock, flags);
75         rq = HWGROUP(drive)->rq;
76
77         BUG_ON(!(rq->flags & REQ_STARTED));
78
79         if (!nr_sectors)
80                 nr_sectors = rq->hard_cur_sectors;
81
82         /*
83          * if failfast is set on a request, override number of sectors and
84          * complete the whole request right now
85          */
86         if (blk_noretry_request(rq) && !uptodate)
87                 nr_sectors = rq->hard_nr_sectors;
88
89         /*
90          * decide whether to reenable DMA -- 3 is a random magic for now,
91          * if we DMA timeout more than 3 times, just stay in PIO
92          */
93         if (drive->state == DMA_PIO_RETRY && drive->retry_pio <= 3) {
94                 drive->state = 0;
95                 HWGROUP(drive)->hwif->ide_dma_on(drive);
96         }
97
98         if (!end_that_request_first(rq, uptodate, nr_sectors)) {
99                 add_disk_randomness(rq->rq_disk);
100                 blkdev_dequeue_request(rq);
101                 HWGROUP(drive)->rq = NULL;
102                 end_that_request_last(rq);
103                 ret = 0;
104         }
105         spin_unlock_irqrestore(&ide_lock, flags);
106         return ret;
107 }
108
109 EXPORT_SYMBOL(ide_end_request);
110
111 /**
112  *      ide_complete_pm_request - end the current Power Management request
113  *      @drive: target drive
114  *      @rq: request
115  *
116  *      This function cleans up the current PM request and stops the queue
117  *      if necessary.
118  */
119 static void ide_complete_pm_request (ide_drive_t *drive, struct request *rq)
120 {
121         unsigned long flags;
122
123 #ifdef DEBUG_PM
124         printk("%s: completing PM request, %s\n", drive->name,
125                blk_pm_suspend_request(rq) ? "suspend" : "resume");
126 #endif
127         spin_lock_irqsave(&ide_lock, flags);
128         if (blk_pm_suspend_request(rq)) {
129                 blk_stop_queue(drive->queue);
130         } else {
131                 drive->blocked = 0;
132                 blk_start_queue(drive->queue);
133         }
134         blkdev_dequeue_request(rq);
135         HWGROUP(drive)->rq = NULL;
136         end_that_request_last(rq);
137         spin_unlock_irqrestore(&ide_lock, flags);
138 }
139
140 /**
141  *      ide_end_drive_cmd       -       end an explicit drive command
142  *      @drive: command 
143  *      @stat: status bits
144  *      @err: error bits
145  *
146  *      Clean up after success/failure of an explicit drive command.
147  *      These get thrown onto the queue so they are synchronized with
148  *      real I/O operations on the drive.
149  *
150  *      In LBA48 mode we have to read the register set twice to get
151  *      all the extra information out.
152  */
153  
154 void ide_end_drive_cmd (ide_drive_t *drive, u8 stat, u8 err)
155 {
156         ide_hwif_t *hwif = HWIF(drive);
157         unsigned long flags;
158         struct request *rq;
159
160         spin_lock_irqsave(&ide_lock, flags);
161         rq = HWGROUP(drive)->rq;
162         spin_unlock_irqrestore(&ide_lock, flags);
163
164         if (rq->flags & REQ_DRIVE_CMD) {
165                 u8 *args = (u8 *) rq->buffer;
166                 if (rq->errors == 0)
167                         rq->errors = !OK_STAT(stat,READY_STAT,BAD_STAT);
168
169                 if (args) {
170                         args[0] = stat;
171                         args[1] = err;
172                         args[2] = hwif->INB(IDE_NSECTOR_REG);
173                 }
174         } else if (rq->flags & REQ_DRIVE_TASK) {
175                 u8 *args = (u8 *) rq->buffer;
176                 if (rq->errors == 0)
177                         rq->errors = !OK_STAT(stat,READY_STAT,BAD_STAT);
178
179                 if (args) {
180                         args[0] = stat;
181                         args[1] = err;
182                         args[2] = hwif->INB(IDE_NSECTOR_REG);
183                         args[3] = hwif->INB(IDE_SECTOR_REG);
184                         args[4] = hwif->INB(IDE_LCYL_REG);
185                         args[5] = hwif->INB(IDE_HCYL_REG);
186                         args[6] = hwif->INB(IDE_SELECT_REG);
187                 }
188         } else if (rq->flags & REQ_DRIVE_TASKFILE) {
189                 ide_task_t *args = (ide_task_t *) rq->special;
190                 if (rq->errors == 0)
191                         rq->errors = !OK_STAT(stat,READY_STAT,BAD_STAT);
192                         
193                 if (args) {
194                         if (args->tf_in_flags.b.data) {
195                                 u16 data                                = hwif->INW(IDE_DATA_REG);
196                                 args->tfRegister[IDE_DATA_OFFSET]       = (data) & 0xFF;
197                                 args->hobRegister[IDE_DATA_OFFSET]      = (data >> 8) & 0xFF;
198                         }
199                         args->tfRegister[IDE_ERROR_OFFSET]   = err;
200                         args->tfRegister[IDE_NSECTOR_OFFSET] = hwif->INB(IDE_NSECTOR_REG);
201                         args->tfRegister[IDE_SECTOR_OFFSET]  = hwif->INB(IDE_SECTOR_REG);
202                         args->tfRegister[IDE_LCYL_OFFSET]    = hwif->INB(IDE_LCYL_REG);
203                         args->tfRegister[IDE_HCYL_OFFSET]    = hwif->INB(IDE_HCYL_REG);
204                         args->tfRegister[IDE_SELECT_OFFSET]  = hwif->INB(IDE_SELECT_REG);
205                         args->tfRegister[IDE_STATUS_OFFSET]  = stat;
206
207                         if (drive->addressing == 1) {
208                                 hwif->OUTB(drive->ctl|0x80, IDE_CONTROL_REG);
209                                 args->hobRegister[IDE_FEATURE_OFFSET]   = hwif->INB(IDE_FEATURE_REG);
210                                 args->hobRegister[IDE_NSECTOR_OFFSET]   = hwif->INB(IDE_NSECTOR_REG);
211                                 args->hobRegister[IDE_SECTOR_OFFSET]    = hwif->INB(IDE_SECTOR_REG);
212                                 args->hobRegister[IDE_LCYL_OFFSET]      = hwif->INB(IDE_LCYL_REG);
213                                 args->hobRegister[IDE_HCYL_OFFSET]      = hwif->INB(IDE_HCYL_REG);
214                         }
215                 }
216         } else if (blk_pm_request(rq)) {
217 #ifdef DEBUG_PM
218                 printk("%s: complete_power_step(step: %d, stat: %x, err: %x)\n",
219                         drive->name, rq->pm->pm_step, stat, err);
220 #endif
221                 DRIVER(drive)->complete_power_step(drive, rq, stat, err);
222                 if (rq->pm->pm_step == ide_pm_state_completed)
223                         ide_complete_pm_request(drive, rq);
224                 return;
225         }
226
227         spin_lock_irqsave(&ide_lock, flags);
228         blkdev_dequeue_request(rq);
229         HWGROUP(drive)->rq = NULL;
230         end_that_request_last(rq);
231         spin_unlock_irqrestore(&ide_lock, flags);
232 }
233
234 EXPORT_SYMBOL(ide_end_drive_cmd);
235
236 /**
237  *      try_to_flush_leftover_data      -       flush junk
238  *      @drive: drive to flush
239  *
240  *      try_to_flush_leftover_data() is invoked in response to a drive
241  *      unexpectedly having its DRQ_STAT bit set.  As an alternative to
242  *      resetting the drive, this routine tries to clear the condition
243  *      by read a sector's worth of data from the drive.  Of course,
244  *      this may not help if the drive is *waiting* for data from *us*.
245  */
246 void try_to_flush_leftover_data (ide_drive_t *drive)
247 {
248         int i = (drive->mult_count ? drive->mult_count : 1) * SECTOR_WORDS;
249
250         if (drive->media != ide_disk)
251                 return;
252         while (i > 0) {
253                 u32 buffer[16];
254                 u32 wcount = (i > 16) ? 16 : i;
255
256                 i -= wcount;
257                 HWIF(drive)->ata_input_data(drive, buffer, wcount);
258         }
259 }
260
261 EXPORT_SYMBOL(try_to_flush_leftover_data);
262
263 /*
264  * FIXME Add an ATAPI error
265  */
266
267 /**
268  *      ide_error       -       handle an error on the IDE
269  *      @drive: drive the error occurred on
270  *      @msg: message to report
271  *      @stat: status bits
272  *
273  *      ide_error() takes action based on the error returned by the drive.
274  *      For normal I/O that may well include retries. We deal with
275  *      both new-style (taskfile) and old style command handling here.
276  *      In the case of taskfile command handling there is work left to
277  *      do
278  */
279  
280 ide_startstop_t ide_error (ide_drive_t *drive, const char *msg, u8 stat)
281 {
282         ide_hwif_t *hwif;
283         struct request *rq;
284         u8 err;
285
286         err = ide_dump_status(drive, msg, stat);
287         if (drive == NULL || (rq = HWGROUP(drive)->rq) == NULL)
288                 return ide_stopped;
289
290         hwif = HWIF(drive);
291         /* retry only "normal" I/O: */
292         if (rq->flags & (REQ_DRIVE_CMD | REQ_DRIVE_TASK)) {
293                 rq->errors = 1;
294                 ide_end_drive_cmd(drive, stat, err);
295                 return ide_stopped;
296         }
297         if (rq->flags & REQ_DRIVE_TASKFILE) {
298                 rq->errors = 1;
299                 ide_end_drive_cmd(drive, stat, err);
300                 return ide_stopped;
301         }
302
303         if (stat & BUSY_STAT || ((stat & WRERR_STAT) && !drive->nowerr)) {
304                  /* other bits are useless when BUSY */
305                 rq->errors |= ERROR_RESET;
306         } else {
307                 if (drive->media != ide_disk)
308                         goto media_out;
309
310                 if (stat & ERR_STAT) {
311                         /* err has different meaning on cdrom and tape */
312                         if (err == ABRT_ERR) {
313                                 if (drive->select.b.lba &&
314                                     (hwif->INB(IDE_COMMAND_REG) == WIN_SPECIFY))
315                                         /* some newer drives don't
316                                          * support WIN_SPECIFY
317                                          */
318                                         return ide_stopped;
319                         } else if ((err & BAD_CRC) == BAD_CRC) {
320                                 drive->crc_count++;
321                                 /* UDMA crc error -- just retry the operation */
322                         } else if (err & (BBD_ERR | ECC_ERR)) {
323                                 /* retries won't help these */
324                                 rq->errors = ERROR_MAX;
325                         } else if (err & TRK0_ERR) {
326                                 /* help it find track zero */
327                                 rq->errors |= ERROR_RECAL;
328                         }
329                 }
330 media_out:
331                 if ((stat & DRQ_STAT) && rq_data_dir(rq) != WRITE)
332                         try_to_flush_leftover_data(drive);
333         }
334         if (hwif->INB(IDE_STATUS_REG) & (BUSY_STAT|DRQ_STAT)) {
335                 /* force an abort */
336                 hwif->OUTB(WIN_IDLEIMMEDIATE,IDE_COMMAND_REG);
337         }
338         if (rq->errors >= ERROR_MAX) {
339                 DRIVER(drive)->end_request(drive, 0, 0);
340         } else {
341                 if ((rq->errors & ERROR_RESET) == ERROR_RESET) {
342                         ++rq->errors;
343                         return ide_do_reset(drive);
344                 }
345                 if ((rq->errors & ERROR_RECAL) == ERROR_RECAL)
346                         drive->special.b.recalibrate = 1;
347                 ++rq->errors;
348         }
349         return ide_stopped;
350 }
351
352 EXPORT_SYMBOL(ide_error);
353
354 /**
355  *      ide_abort       -       abort pending IDE operatins
356  *      @drive: drive the error occurred on
357  *      @msg: message to report
358  *
359  *      ide_abort kills and cleans up when we are about to do a 
360  *      host initiated reset on active commands. Longer term we
361  *      want handlers to have sensible abort handling themselves
362  *
363  *      This differs fundamentally from ide_error because in 
364  *      this case the command is doing just fine when we
365  *      blow it away.
366  */
367  
368 ide_startstop_t ide_abort(ide_drive_t *drive, const char *msg)
369 {
370         ide_hwif_t *hwif;
371         struct request *rq;
372
373         if (drive == NULL || (rq = HWGROUP(drive)->rq) == NULL)
374                 return ide_stopped;
375
376         hwif = HWIF(drive);
377         /* retry only "normal" I/O: */
378         if (rq->flags & (REQ_DRIVE_CMD | REQ_DRIVE_TASK)) {
379                 rq->errors = 1;
380                 ide_end_drive_cmd(drive, BUSY_STAT, 0);
381                 return ide_stopped;
382         }
383         if (rq->flags & REQ_DRIVE_TASKFILE) {
384                 rq->errors = 1;
385                 ide_end_drive_cmd(drive, BUSY_STAT, 0);
386                 return ide_stopped;
387         }
388
389         rq->errors |= ERROR_RESET;
390         DRIVER(drive)->end_request(drive, 0, 0);
391         return ide_stopped;
392 }
393
394 EXPORT_SYMBOL(ide_abort);
395
396 /**
397  *      ide_cmd         -       issue a simple drive command
398  *      @drive: drive the command is for
399  *      @cmd: command byte
400  *      @nsect: sector byte
401  *      @handler: handler for the command completion
402  *
403  *      Issue a simple drive command with interrupts.
404  *      The drive must be selected beforehand.
405  */
406
407 void ide_cmd (ide_drive_t *drive, u8 cmd, u8 nsect, ide_handler_t *handler)
408 {
409         ide_hwif_t *hwif = HWIF(drive);
410         if (IDE_CONTROL_REG)
411                 hwif->OUTB(drive->ctl,IDE_CONTROL_REG); /* clear nIEN */
412         SELECT_MASK(drive,0);
413         hwif->OUTB(nsect,IDE_NSECTOR_REG);
414         ide_execute_command(drive, cmd, handler, WAIT_CMD, NULL);
415 }
416
417 EXPORT_SYMBOL(ide_cmd);
418
419 /**
420  *      drive_cmd_intr          -       drive command completion interrupt
421  *      @drive: drive the completion interrupt occurred on
422  *
423  *      drive_cmd_intr() is invoked on completion of a special DRIVE_CMD.
424  *      We do any necessary daya reading and then wait for the drive to
425  *      go non busy. At that point we may read the error data and complete
426  *      the request
427  */
428  
429 ide_startstop_t drive_cmd_intr (ide_drive_t *drive)
430 {
431         struct request *rq = HWGROUP(drive)->rq;
432         ide_hwif_t *hwif = HWIF(drive);
433         u8 *args = (u8 *) rq->buffer;
434         u8 stat = hwif->INB(IDE_STATUS_REG);
435         int retries = 10;
436
437         local_irq_enable();
438         if ((stat & DRQ_STAT) && args && args[3]) {
439                 u8 io_32bit = drive->io_32bit;
440                 drive->io_32bit = 0;
441                 hwif->ata_input_data(drive, &args[4], args[3] * SECTOR_WORDS);
442                 drive->io_32bit = io_32bit;
443                 while (((stat = hwif->INB(IDE_STATUS_REG)) & BUSY_STAT) && retries--)
444                         udelay(100);
445         }
446
447         if (!OK_STAT(stat, READY_STAT, BAD_STAT) && DRIVER(drive) != NULL)
448                 return DRIVER(drive)->error(drive, "drive_cmd", stat);
449                 /* calls ide_end_drive_cmd */
450         ide_end_drive_cmd(drive, stat, hwif->INB(IDE_ERROR_REG));
451         return ide_stopped;
452 }
453
454 EXPORT_SYMBOL(drive_cmd_intr);
455
456 /**
457  *      do_special              -       issue some special commands
458  *      @drive: drive the command is for
459  *
460  *      do_special() is used to issue WIN_SPECIFY, WIN_RESTORE, and WIN_SETMULT
461  *      commands to a drive.  It used to do much more, but has been scaled
462  *      back.
463  */
464
465 ide_startstop_t do_special (ide_drive_t *drive)
466 {
467         special_t *s = &drive->special;
468
469 #ifdef DEBUG
470         printk("%s: do_special: 0x%02x\n", drive->name, s->all);
471 #endif
472         if (s->b.set_tune) {
473                 s->b.set_tune = 0;
474                 if (HWIF(drive)->tuneproc != NULL)
475                         HWIF(drive)->tuneproc(drive, drive->tune_req);
476                 return ide_stopped;
477         }
478         else
479                 return DRIVER(drive)->special(drive);
480 }
481
482 EXPORT_SYMBOL(do_special);
483
484 /**
485  *      execute_drive_command   -       issue special drive command
486  *      @drive: the drive to issue th command on
487  *      @rq: the request structure holding the command
488  *
489  *      execute_drive_cmd() issues a special drive command,  usually 
490  *      initiated by ioctl() from the external hdparm program. The
491  *      command can be a drive command, drive task or taskfile 
492  *      operation. Weirdly you can call it with NULL to wait for
493  *      all commands to finish. Don't do this as that is due to change
494  */
495
496 ide_startstop_t execute_drive_cmd (ide_drive_t *drive, struct request *rq)
497 {
498         ide_hwif_t *hwif = HWIF(drive);
499         if (rq->flags & REQ_DRIVE_TASKFILE) {
500                 ide_task_t *args = rq->special;
501  
502                 if (!args)
503                         goto done;
504  
505                 if (args->tf_out_flags.all != 0) 
506                         return flagged_taskfile(drive, args);
507                 return do_rw_taskfile(drive, args);
508         } else if (rq->flags & REQ_DRIVE_TASK) {
509                 u8 *args = rq->buffer;
510                 u8 sel;
511  
512                 if (!args)
513                         goto done;
514 #ifdef DEBUG
515                 printk("%s: DRIVE_TASK_CMD ", drive->name);
516                 printk("cmd=0x%02x ", args[0]);
517                 printk("fr=0x%02x ", args[1]);
518                 printk("ns=0x%02x ", args[2]);
519                 printk("sc=0x%02x ", args[3]);
520                 printk("lcyl=0x%02x ", args[4]);
521                 printk("hcyl=0x%02x ", args[5]);
522                 printk("sel=0x%02x\n", args[6]);
523 #endif
524                 hwif->OUTB(args[1], IDE_FEATURE_REG);
525                 hwif->OUTB(args[3], IDE_SECTOR_REG);
526                 hwif->OUTB(args[4], IDE_LCYL_REG);
527                 hwif->OUTB(args[5], IDE_HCYL_REG);
528                 sel = (args[6] & ~0x10);
529                 if (drive->select.b.unit)
530                         sel |= 0x10;
531                 hwif->OUTB(sel, IDE_SELECT_REG);
532                 ide_cmd(drive, args[0], args[2], &drive_cmd_intr);
533                 return ide_started;
534         } else if (rq->flags & REQ_DRIVE_CMD) {
535                 u8 *args = rq->buffer;
536
537                 if (!args)
538                         goto done;
539 #ifdef DEBUG
540                 printk("%s: DRIVE_CMD ", drive->name);
541                 printk("cmd=0x%02x ", args[0]);
542                 printk("sc=0x%02x ", args[1]);
543                 printk("fr=0x%02x ", args[2]);
544                 printk("xx=0x%02x\n", args[3]);
545 #endif
546                 if (args[0] == WIN_SMART) {
547                         hwif->OUTB(0x4f, IDE_LCYL_REG);
548                         hwif->OUTB(0xc2, IDE_HCYL_REG);
549                         hwif->OUTB(args[2],IDE_FEATURE_REG);
550                         hwif->OUTB(args[1],IDE_SECTOR_REG);
551                         ide_cmd(drive, args[0], args[3], &drive_cmd_intr);
552                         return ide_started;
553                 }
554                 hwif->OUTB(args[2],IDE_FEATURE_REG);
555                 ide_cmd(drive, args[0], args[1], &drive_cmd_intr);
556                 return ide_started;
557         }
558
559 done:
560         /*
561          * NULL is actually a valid way of waiting for
562          * all current requests to be flushed from the queue.
563          */
564 #ifdef DEBUG
565         printk("%s: DRIVE_CMD (null)\n", drive->name);
566 #endif
567         ide_end_drive_cmd(drive,
568                         hwif->INB(IDE_STATUS_REG),
569                         hwif->INB(IDE_ERROR_REG));
570         return ide_stopped;
571 }
572
573 EXPORT_SYMBOL(execute_drive_cmd);
574
575 /**
576  *      start_request   -       start of I/O and command issuing for IDE
577  *
578  *      start_request() initiates handling of a new I/O request. It
579  *      accepts commands and I/O (read/write) requests. It also does
580  *      the final remapping for weird stuff like EZDrive. Once 
581  *      device mapper can work sector level the EZDrive stuff can go away
582  *
583  *      FIXME: this function needs a rename
584  */
585  
586 ide_startstop_t start_request (ide_drive_t *drive, struct request *rq)
587 {
588         ide_startstop_t startstop;
589         sector_t block;
590
591         BUG_ON(!(rq->flags & REQ_STARTED));
592
593 #ifdef DEBUG
594         printk("%s: start_request: current=0x%08lx\n",
595                 HWIF(drive)->name, (unsigned long) rq);
596 #endif
597
598         /* bail early if we've exceeded max_failures */
599         if (drive->max_failures && (drive->failures > drive->max_failures)) {
600                 goto kill_rq;
601         }
602
603         /*
604          * bail early if we've sent a device to sleep, however how to wake
605          * this needs to be a masked flag.  FIXME for proper operations.
606          */
607         if (drive->suspend_reset)
608                 goto kill_rq;
609
610         block    = rq->sector;
611         if (blk_fs_request(rq) &&
612             (drive->media == ide_disk || drive->media == ide_floppy)) {
613                 block += drive->sect0;
614         }
615         /* Yecch - this will shift the entire interval,
616            possibly killing some innocent following sector */
617         if (block == 0 && drive->remap_0_to_1 == 1)
618                 block = 1;  /* redirect MBR access to EZ-Drive partn table */
619
620         if (blk_pm_suspend_request(rq) &&
621             rq->pm->pm_step == ide_pm_state_start_suspend)
622                 /* Mark drive blocked when starting the suspend sequence. */
623                 drive->blocked = 1;
624         else if (blk_pm_resume_request(rq) &&
625                  rq->pm->pm_step == ide_pm_state_start_resume) {
626                 /* 
627                  * The first thing we do on wakeup is to wait for BSY bit to
628                  * go away (with a looong timeout) as a drive on this hwif may
629                  * just be POSTing itself.
630                  * We do that before even selecting as the "other" device on
631                  * the bus may be broken enough to walk on our toes at this
632                  * point.
633                  */
634                 int rc;
635 #ifdef DEBUG_PM
636                 printk("%s: Wakeup request inited, waiting for !BSY...\n", drive->name);
637 #endif
638                 rc = ide_wait_not_busy(HWIF(drive), 35000);
639                 if (rc)
640                         printk(KERN_WARNING "%s: bus not ready on wakeup\n", drive->name);
641                 SELECT_DRIVE(drive);
642                 HWIF(drive)->OUTB(8, HWIF(drive)->io_ports[IDE_CONTROL_OFFSET]);
643                 rc = ide_wait_not_busy(HWIF(drive), 10000);
644                 if (rc)
645                         printk(KERN_WARNING "%s: drive not ready on wakeup\n", drive->name);
646         }
647
648         SELECT_DRIVE(drive);
649         if (ide_wait_stat(&startstop, drive, drive->ready_stat, BUSY_STAT|DRQ_STAT, WAIT_READY)) {
650                 printk(KERN_ERR "%s: drive not ready for command\n", drive->name);
651                 return startstop;
652         }
653         if (!drive->special.all) {
654                 if (rq->flags & (REQ_DRIVE_CMD | REQ_DRIVE_TASK))
655                         return execute_drive_cmd(drive, rq);
656                 else if (rq->flags & REQ_DRIVE_TASKFILE)
657                         return execute_drive_cmd(drive, rq);
658                 else if (blk_pm_request(rq)) {
659 #ifdef DEBUG_PM
660                         printk("%s: start_power_step(step: %d)\n",
661                                 drive->name, rq->pm->pm_step);
662 #endif
663                         startstop = DRIVER(drive)->start_power_step(drive, rq);
664                         if (startstop == ide_stopped &&
665                             rq->pm->pm_step == ide_pm_state_completed)
666                                 ide_complete_pm_request(drive, rq);
667                         return startstop;
668                 }
669                 return (DRIVER(drive)->do_request(drive, rq, block));
670         }
671         return do_special(drive);
672 kill_rq:
673         DRIVER(drive)->end_request(drive, 0, 0);
674         return ide_stopped;
675 }
676
677 EXPORT_SYMBOL(start_request);
678
679 /**
680  *      ide_stall_queue         -       pause an IDE device
681  *      @drive: drive to stall
682  *      @timeout: time to stall for (jiffies)
683  *
684  *      ide_stall_queue() can be used by a drive to give excess bandwidth back
685  *      to the hwgroup by sleeping for timeout jiffies.
686  */
687  
688 void ide_stall_queue (ide_drive_t *drive, unsigned long timeout)
689 {
690         if (timeout > WAIT_WORSTCASE)
691                 timeout = WAIT_WORSTCASE;
692         drive->sleep = timeout + jiffies;
693 }
694
695 EXPORT_SYMBOL(ide_stall_queue);
696
697 #define WAKEUP(drive)   ((drive)->service_start + 2 * (drive)->service_time)
698
699 /**
700  *      choose_drive            -       select a drive to service
701  *      @hwgroup: hardware group to select on
702  *
703  *      choose_drive() selects the next drive which will be serviced.
704  *      This is necessary because the IDE layer can't issue commands
705  *      to both drives on the same cable, unlike SCSI.
706  */
707  
708 static inline ide_drive_t *choose_drive (ide_hwgroup_t *hwgroup)
709 {
710         ide_drive_t *drive, *best;
711
712 repeat: 
713         best = NULL;
714         drive = hwgroup->drive;
715         do {
716                 if ((!drive->sleep || time_after_eq(jiffies, drive->sleep))
717                     && !elv_queue_empty(drive->queue)) {
718                         if (!best
719                          || (drive->sleep && (!best->sleep || 0 < (signed long)(best->sleep - drive->sleep)))
720                          || (!best->sleep && 0 < (signed long)(WAKEUP(best) - WAKEUP(drive))))
721                         {
722                                 if (!blk_queue_plugged(drive->queue))
723                                         best = drive;
724                         }
725                 }
726         } while ((drive = drive->next) != hwgroup->drive);
727         if (best && best->nice1 && !best->sleep && best != hwgroup->drive && best->service_time > WAIT_MIN_SLEEP) {
728                 long t = (signed long)(WAKEUP(best) - jiffies);
729                 if (t >= WAIT_MIN_SLEEP) {
730                 /*
731                  * We *may* have some time to spare, but first let's see if
732                  * someone can potentially benefit from our nice mood today..
733                  */
734                         drive = best->next;
735                         do {
736                                 if (!drive->sleep
737                                 /* FIXME: use time_before */
738                                  && 0 < (signed long)(WAKEUP(drive) - (jiffies - best->service_time))
739                                  && 0 < (signed long)((jiffies + t) - WAKEUP(drive)))
740                                 {
741                                         ide_stall_queue(best, min_t(long, t, 10 * WAIT_MIN_SLEEP));
742                                         goto repeat;
743                                 }
744                         } while ((drive = drive->next) != best);
745                 }
746         }
747         return best;
748 }
749
750 /*
751  * Issue a new request to a drive from hwgroup
752  * Caller must have already done spin_lock_irqsave(&ide_lock, ..);
753  *
754  * A hwgroup is a serialized group of IDE interfaces.  Usually there is
755  * exactly one hwif (interface) per hwgroup, but buggy controllers (eg. CMD640)
756  * may have both interfaces in a single hwgroup to "serialize" access.
757  * Or possibly multiple ISA interfaces can share a common IRQ by being grouped
758  * together into one hwgroup for serialized access.
759  *
760  * Note also that several hwgroups can end up sharing a single IRQ,
761  * possibly along with many other devices.  This is especially common in
762  * PCI-based systems with off-board IDE controller cards.
763  *
764  * The IDE driver uses the single global ide_lock spinlock to protect
765  * access to the request queues, and to protect the hwgroup->busy flag.
766  *
767  * The first thread into the driver for a particular hwgroup sets the
768  * hwgroup->busy flag to indicate that this hwgroup is now active,
769  * and then initiates processing of the top request from the request queue.
770  *
771  * Other threads attempting entry notice the busy setting, and will simply
772  * queue their new requests and exit immediately.  Note that hwgroup->busy
773  * remains set even when the driver is merely awaiting the next interrupt.
774  * Thus, the meaning is "this hwgroup is busy processing a request".
775  *
776  * When processing of a request completes, the completing thread or IRQ-handler
777  * will start the next request from the queue.  If no more work remains,
778  * the driver will clear the hwgroup->busy flag and exit.
779  *
780  * The ide_lock (spinlock) is used to protect all access to the
781  * hwgroup->busy flag, but is otherwise not needed for most processing in
782  * the driver.  This makes the driver much more friendlier to shared IRQs
783  * than previous designs, while remaining 100% (?) SMP safe and capable.
784  */
785 /* --BenH: made non-static as ide-pmac.c uses it to kick the hwgroup back
786  *         into life on wakeup from machine sleep.
787  */ 
788 void ide_do_request (ide_hwgroup_t *hwgroup, int masked_irq)
789 {
790         ide_drive_t     *drive;
791         ide_hwif_t      *hwif;
792         struct request  *rq;
793         ide_startstop_t startstop;
794
795         /* for atari only: POSSIBLY BROKEN HERE(?) */
796         ide_get_lock(ide_intr, hwgroup);
797
798         /* caller must own ide_lock */
799         BUG_ON(!irqs_disabled());
800
801         while (!hwgroup->busy) {
802                 hwgroup->busy = 1;
803                 drive = choose_drive(hwgroup);
804                 if (drive == NULL) {
805                         unsigned long sleep = 0;
806                         hwgroup->rq = NULL;
807                         drive = hwgroup->drive;
808                         do {
809                                 if (drive->sleep && (!sleep || 0 < (signed long)(sleep - drive->sleep)))
810                                         sleep = drive->sleep;
811                         } while ((drive = drive->next) != hwgroup->drive);
812                         if (sleep) {
813                 /*
814                  * Take a short snooze, and then wake up this hwgroup again.
815                  * This gives other hwgroups on the same a chance to
816                  * play fairly with us, just in case there are big differences
817                  * in relative throughputs.. don't want to hog the cpu too much.
818                  */
819                                 if (time_before(sleep, jiffies + WAIT_MIN_SLEEP))
820                                         sleep = jiffies + WAIT_MIN_SLEEP;
821 #if 1
822                                 if (timer_pending(&hwgroup->timer))
823                                         printk(KERN_CRIT "ide_set_handler: timer already active\n");
824 #endif
825                                 /* so that ide_timer_expiry knows what to do */
826                                 hwgroup->sleeping = 1;
827                                 mod_timer(&hwgroup->timer, sleep);
828                                 /* we purposely leave hwgroup->busy==1
829                                  * while sleeping */
830                         } else {
831                                 /* Ugly, but how can we sleep for the lock
832                                  * otherwise? perhaps from tq_disk?
833                                  */
834
835                                 /* for atari only */
836                                 ide_release_lock();
837                                 hwgroup->busy = 0;
838                         }
839
840                         /* no more work for this hwgroup (for now) */
841                         return;
842                 }
843                 hwif = HWIF(drive);
844                 if (hwgroup->hwif->sharing_irq &&
845                     hwif != hwgroup->hwif &&
846                     hwif->io_ports[IDE_CONTROL_OFFSET]) {
847                         /* set nIEN for previous hwif */
848                         SELECT_INTERRUPT(drive);
849                 }
850                 hwgroup->hwif = hwif;
851                 hwgroup->drive = drive;
852                 drive->sleep = 0;
853                 drive->service_start = jiffies;
854
855                 if (blk_queue_plugged(drive->queue)) {
856                         printk(KERN_ERR "ide: huh? queue was plugged!\n");
857                         break;
858                 }
859
860                 /*
861                  * we know that the queue isn't empty, but this can happen
862                  * if the q->prep_rq_fn() decides to kill a request
863                  */
864                 rq = elv_next_request(drive->queue);
865                 if (!rq) {
866                         hwgroup->busy = 0;
867                         break;
868                 }
869
870                 /*
871                  * Sanity: don't accept a request that isn't a PM request
872                  * if we are currently power managed. This is very important as
873                  * blk_stop_queue() doesn't prevent the elv_next_request()
874                  * above to return us whatever is in the queue. Since we call
875                  * ide_do_request() ourselves, we end up taking requests while
876                  * the queue is blocked...
877                  * 
878                  * We let requests forced at head of queue with ide-preempt
879                  * though. I hope that doesn't happen too much, hopefully not
880                  * unless the subdriver triggers such a thing in its own PM
881                  * state machine.
882                  */
883                 if (drive->blocked && !blk_pm_request(rq) && !(rq->flags & REQ_PREEMPT)) {
884                         /* We clear busy, there should be no pending ATA command at this point. */
885                         hwgroup->busy = 0;
886                         break;
887                 }
888
889                 hwgroup->rq = rq;
890
891                 /*
892                  * Some systems have trouble with IDE IRQs arriving while
893                  * the driver is still setting things up.  So, here we disable
894                  * the IRQ used by this interface while the request is being started.
895                  * This may look bad at first, but pretty much the same thing
896                  * happens anyway when any interrupt comes in, IDE or otherwise
897                  *  -- the kernel masks the IRQ while it is being handled.
898                  */
899                 if (hwif->irq != masked_irq)
900                         disable_irq_nosync(hwif->irq);
901                 spin_unlock(&ide_lock);
902                 local_irq_enable();
903                         /* allow other IRQs while we start this request */
904                 startstop = start_request(drive, rq);
905                 spin_lock_irq(&ide_lock);
906                 if (hwif->irq != masked_irq)
907                         enable_irq(hwif->irq);
908                 if (startstop == ide_stopped)
909                         hwgroup->busy = 0;
910         }
911 }
912
913 EXPORT_SYMBOL(ide_do_request);
914
915 /*
916  * Passes the stuff to ide_do_request
917  */
918 void do_ide_request(request_queue_t *q)
919 {
920         ide_do_request(q->queuedata, IDE_NO_IRQ);
921 }
922
923 /*
924  * un-busy the hwgroup etc, and clear any pending DMA status. we want to
925  * retry the current request in pio mode instead of risking tossing it
926  * all away
927  */
928 static ide_startstop_t ide_dma_timeout_retry(ide_drive_t *drive, int error)
929 {
930         ide_hwif_t *hwif = HWIF(drive);
931         struct request *rq;
932         ide_startstop_t ret = ide_stopped;
933
934         /*
935          * end current dma transaction
936          */
937
938         if (error < 0) {
939                 printk(KERN_WARNING "%s: DMA timeout error\n", drive->name);
940                 (void)HWIF(drive)->ide_dma_end(drive);
941                 ret = DRIVER(drive)->error(drive, "dma timeout error",
942                                                 hwif->INB(IDE_STATUS_REG));
943         } else {
944                 printk(KERN_WARNING "%s: DMA timeout retry\n", drive->name);
945                 (void) hwif->ide_dma_timeout(drive);
946         }
947
948         /*
949          * disable dma for now, but remember that we did so because of
950          * a timeout -- we'll reenable after we finish this next request
951          * (or rather the first chunk of it) in pio.
952          */
953         drive->retry_pio++;
954         drive->state = DMA_PIO_RETRY;
955         (void) hwif->ide_dma_off_quietly(drive);
956
957         /*
958          * un-busy drive etc (hwgroup->busy is cleared on return) and
959          * make sure request is sane
960          */
961         rq = HWGROUP(drive)->rq;
962         HWGROUP(drive)->rq = NULL;
963
964         rq->errors = 0;
965         rq->sector = rq->bio->bi_sector;
966         rq->current_nr_sectors = bio_iovec(rq->bio)->bv_len >> 9;
967         rq->hard_cur_sectors = rq->current_nr_sectors;
968         if (rq->bio)
969                 rq->buffer = NULL;
970
971         return ret;
972 }
973
974 /**
975  *      ide_timer_expiry        -       handle lack of an IDE interrupt
976  *      @data: timer callback magic (hwgroup)
977  *
978  *      An IDE command has timed out before the expected drive return
979  *      occurred. At this point we attempt to clean up the current
980  *      mess. If the current handler includes an expiry handler then
981  *      we invoke the expiry handler, and providing it is happy the
982  *      work is done. If that fails we apply generic recovery rules
983  *      invoking the handler and checking the drive DMA status. We
984  *      have an excessively incestuous relationship with the DMA
985  *      logic that wants cleaning up.
986  */
987  
988 void ide_timer_expiry (unsigned long data)
989 {
990         ide_hwgroup_t   *hwgroup = (ide_hwgroup_t *) data;
991         ide_handler_t   *handler;
992         ide_expiry_t    *expiry;
993         unsigned long   flags;
994         unsigned long   wait = -1;
995
996         spin_lock_irqsave(&ide_lock, flags);
997
998         if ((handler = hwgroup->handler) == NULL) {
999                 /*
1000                  * Either a marginal timeout occurred
1001                  * (got the interrupt just as timer expired),
1002                  * or we were "sleeping" to give other devices a chance.
1003                  * Either way, we don't really want to complain about anything.
1004                  */
1005                 if (hwgroup->sleeping) {
1006                         hwgroup->sleeping = 0;
1007                         hwgroup->busy = 0;
1008                 }
1009         } else {
1010                 ide_drive_t *drive = hwgroup->drive;
1011                 if (!drive) {
1012                         printk(KERN_ERR "ide_timer_expiry: hwgroup->drive was NULL\n");
1013                         hwgroup->handler = NULL;
1014                 } else {
1015                         ide_hwif_t *hwif;
1016                         ide_startstop_t startstop = ide_stopped;
1017                         if (!hwgroup->busy) {
1018                                 hwgroup->busy = 1;      /* paranoia */
1019                                 printk(KERN_ERR "%s: ide_timer_expiry: hwgroup->busy was 0 ??\n", drive->name);
1020                         }
1021                         if ((expiry = hwgroup->expiry) != NULL) {
1022                                 /* continue */
1023                                 if ((wait = expiry(drive)) > 0) {
1024                                         /* reset timer */
1025                                         hwgroup->timer.expires  = jiffies + wait;
1026                                         add_timer(&hwgroup->timer);
1027                                         spin_unlock_irqrestore(&ide_lock, flags);
1028                                         return;
1029                                 }
1030                         }
1031                         hwgroup->handler = NULL;
1032                         /*
1033                          * We need to simulate a real interrupt when invoking
1034                          * the handler() function, which means we need to
1035                          * globally mask the specific IRQ:
1036                          */
1037                         spin_unlock(&ide_lock);
1038                         hwif  = HWIF(drive);
1039 #if DISABLE_IRQ_NOSYNC
1040                         disable_irq_nosync(hwif->irq);
1041 #else
1042                         /* disable_irq_nosync ?? */
1043                         disable_irq(hwif->irq);
1044 #endif /* DISABLE_IRQ_NOSYNC */
1045                         /* local CPU only,
1046                          * as if we were handling an interrupt */
1047                         local_irq_disable();
1048                         if (hwgroup->poll_timeout != 0) {
1049                                 startstop = handler(drive);
1050                         } else if (drive_is_ready(drive)) {
1051                                 if (drive->waiting_for_dma)
1052                                         (void) hwgroup->hwif->ide_dma_lostirq(drive);
1053                                 (void)ide_ack_intr(hwif);
1054                                 printk(KERN_WARNING "%s: lost interrupt\n", drive->name);
1055                                 startstop = handler(drive);
1056                         } else {
1057                                 if (drive->waiting_for_dma) {
1058                                         startstop = ide_dma_timeout_retry(drive, wait);
1059                                 } else
1060                                         startstop =
1061                                         DRIVER(drive)->error(drive, "irq timeout", hwif->INB(IDE_STATUS_REG));
1062                         }
1063                         drive->service_time = jiffies - drive->service_start;
1064                         spin_lock_irq(&ide_lock);
1065                         enable_irq(hwif->irq);
1066                         if (startstop == ide_stopped)
1067                                 hwgroup->busy = 0;
1068                 }
1069         }
1070         ide_do_request(hwgroup, IDE_NO_IRQ);
1071         spin_unlock_irqrestore(&ide_lock, flags);
1072 }
1073
1074 EXPORT_SYMBOL(ide_timer_expiry);
1075
1076 /**
1077  *      unexpected_intr         -       handle an unexpected IDE interrupt
1078  *      @irq: interrupt line
1079  *      @hwgroup: hwgroup being processed
1080  *
1081  *      There's nothing really useful we can do with an unexpected interrupt,
1082  *      other than reading the status register (to clear it), and logging it.
1083  *      There should be no way that an irq can happen before we're ready for it,
1084  *      so we needn't worry much about losing an "important" interrupt here.
1085  *
1086  *      On laptops (and "green" PCs), an unexpected interrupt occurs whenever
1087  *      the drive enters "idle", "standby", or "sleep" mode, so if the status
1088  *      looks "good", we just ignore the interrupt completely.
1089  *
1090  *      This routine assumes __cli() is in effect when called.
1091  *
1092  *      If an unexpected interrupt happens on irq15 while we are handling irq14
1093  *      and if the two interfaces are "serialized" (CMD640), then it looks like
1094  *      we could screw up by interfering with a new request being set up for 
1095  *      irq15.
1096  *
1097  *      In reality, this is a non-issue.  The new command is not sent unless 
1098  *      the drive is ready to accept one, in which case we know the drive is
1099  *      not trying to interrupt us.  And ide_set_handler() is always invoked
1100  *      before completing the issuance of any new drive command, so we will not
1101  *      be accidentally invoked as a result of any valid command completion
1102  *      interrupt.
1103  *
1104  *      Note that we must walk the entire hwgroup here. We know which hwif
1105  *      is doing the current command, but we don't know which hwif burped
1106  *      mysteriously.
1107  */
1108  
1109 static void unexpected_intr (int irq, ide_hwgroup_t *hwgroup)
1110 {
1111         u8 stat;
1112         ide_hwif_t *hwif = hwgroup->hwif;
1113
1114         /*
1115          * handle the unexpected interrupt
1116          */
1117         do {
1118                 if (hwif->irq == irq) {
1119                         stat = hwif->INB(hwif->io_ports[IDE_STATUS_OFFSET]);
1120                         if (!OK_STAT(stat, READY_STAT, BAD_STAT)) {
1121                                 /* Try to not flood the console with msgs */
1122                                 static unsigned long last_msgtime, count;
1123                                 ++count;
1124                                 if (time_after(jiffies, last_msgtime + HZ)) {
1125                                         last_msgtime = jiffies;
1126                                         printk(KERN_ERR "%s%s: unexpected interrupt, "
1127                                                 "status=0x%02x, count=%ld\n",
1128                                                 hwif->name,
1129                                                 (hwif->next==hwgroup->hwif) ? "" : "(?)", stat, count);
1130                                 }
1131                         }
1132                 }
1133         } while ((hwif = hwif->next) != hwgroup->hwif);
1134 }
1135
1136 /**
1137  *      ide_intr        -       default IDE interrupt handler
1138  *      @irq: interrupt number
1139  *      @dev_id: hwif group
1140  *      @regs: unused weirdness from the kernel irq layer
1141  *
1142  *      This is the default IRQ handler for the IDE layer. You should
1143  *      not need to override it. If you do be aware it is subtle in
1144  *      places
1145  *
1146  *      hwgroup->hwif is the interface in the group currently performing
1147  *      a command. hwgroup->drive is the drive and hwgroup->handler is
1148  *      the IRQ handler to call. As we issue a command the handlers
1149  *      step through multiple states, reassigning the handler to the
1150  *      next step in the process. Unlike a smart SCSI controller IDE
1151  *      expects the main processor to sequence the various transfer
1152  *      stages. We also manage a poll timer to catch up with most
1153  *      timeout situations. There are still a few where the handlers
1154  *      don't ever decide to give up.
1155  *
1156  *      The handler eventually returns ide_stopped to indicate the
1157  *      request completed. At this point we issue the next request
1158  *      on the hwgroup and the process begins again.
1159  */
1160  
1161 irqreturn_t ide_intr (int irq, void *dev_id, struct pt_regs *regs)
1162 {
1163         unsigned long flags;
1164         ide_hwgroup_t *hwgroup = (ide_hwgroup_t *)dev_id;
1165         ide_hwif_t *hwif;
1166         ide_drive_t *drive;
1167         ide_handler_t *handler;
1168         ide_startstop_t startstop;
1169
1170         spin_lock_irqsave(&ide_lock, flags);
1171         hwif = hwgroup->hwif;
1172
1173         if (!ide_ack_intr(hwif)) {
1174                 spin_unlock_irqrestore(&ide_lock, flags);
1175                 return IRQ_NONE;
1176         }
1177
1178         if ((handler = hwgroup->handler) == NULL ||
1179             hwgroup->poll_timeout != 0) {
1180                 /*
1181                  * Not expecting an interrupt from this drive.
1182                  * That means this could be:
1183                  *      (1) an interrupt from another PCI device
1184                  *      sharing the same PCI INT# as us.
1185                  * or   (2) a drive just entered sleep or standby mode,
1186                  *      and is interrupting to let us know.
1187                  * or   (3) a spurious interrupt of unknown origin.
1188                  *
1189                  * For PCI, we cannot tell the difference,
1190                  * so in that case we just ignore it and hope it goes away.
1191                  *
1192                  * FIXME: unexpected_intr should be hwif-> then we can
1193                  * remove all the ifdef PCI crap
1194                  */
1195 #ifdef CONFIG_BLK_DEV_IDEPCI
1196                 if (hwif->pci_dev && !hwif->pci_dev->vendor)
1197 #endif  /* CONFIG_BLK_DEV_IDEPCI */
1198                 {
1199                         /*
1200                          * Probably not a shared PCI interrupt,
1201                          * so we can safely try to do something about it:
1202                          */
1203                         unexpected_intr(irq, hwgroup);
1204 #ifdef CONFIG_BLK_DEV_IDEPCI
1205                 } else {
1206                         /*
1207                          * Whack the status register, just in case
1208                          * we have a leftover pending IRQ.
1209                          */
1210                         (void) hwif->INB(hwif->io_ports[IDE_STATUS_OFFSET]);
1211 #endif /* CONFIG_BLK_DEV_IDEPCI */
1212                 }
1213                 spin_unlock_irqrestore(&ide_lock, flags);
1214                 return IRQ_NONE;
1215         }
1216         drive = hwgroup->drive;
1217         if (!drive) {
1218                 /*
1219                  * This should NEVER happen, and there isn't much
1220                  * we could do about it here.
1221                  *
1222                  * [Note - this can occur if the drive is hot unplugged]
1223                  */
1224                 spin_unlock_irqrestore(&ide_lock, flags);
1225                 return IRQ_HANDLED;
1226         }
1227         if (!drive_is_ready(drive)) {
1228                 /*
1229                  * This happens regularly when we share a PCI IRQ with
1230                  * another device.  Unfortunately, it can also happen
1231                  * with some buggy drives that trigger the IRQ before
1232                  * their status register is up to date.  Hopefully we have
1233                  * enough advance overhead that the latter isn't a problem.
1234                  */
1235                 spin_unlock_irqrestore(&ide_lock, flags);
1236                 return IRQ_NONE;
1237         }
1238         if (!hwgroup->busy) {
1239                 hwgroup->busy = 1;      /* paranoia */
1240                 printk(KERN_ERR "%s: ide_intr: hwgroup->busy was 0 ??\n", drive->name);
1241         }
1242         hwgroup->handler = NULL;
1243         del_timer(&hwgroup->timer);
1244         spin_unlock(&ide_lock);
1245
1246         if (drive->unmask)
1247                 local_irq_enable();
1248         /* service this interrupt, may set handler for next interrupt */
1249         startstop = handler(drive);
1250         spin_lock_irq(&ide_lock);
1251
1252         /*
1253          * Note that handler() may have set things up for another
1254          * interrupt to occur soon, but it cannot happen until
1255          * we exit from this routine, because it will be the
1256          * same irq as is currently being serviced here, and Linux
1257          * won't allow another of the same (on any CPU) until we return.
1258          */
1259         drive->service_time = jiffies - drive->service_start;
1260         if (startstop == ide_stopped) {
1261                 if (hwgroup->handler == NULL) { /* paranoia */
1262                         hwgroup->busy = 0;
1263                         ide_do_request(hwgroup, hwif->irq);
1264                 } else {
1265                         printk(KERN_ERR "%s: ide_intr: huh? expected NULL handler "
1266                                 "on exit\n", drive->name);
1267                 }
1268         }
1269         spin_unlock_irqrestore(&ide_lock, flags);
1270         return IRQ_HANDLED;
1271 }
1272
1273 EXPORT_SYMBOL(ide_intr);
1274
1275 /**
1276  *      ide_init_drive_cmd      -       initialize a drive command request
1277  *      @rq: request object
1278  *
1279  *      Initialize a request before we fill it in and send it down to
1280  *      ide_do_drive_cmd. Commands must be set up by this function. Right
1281  *      now it doesn't do a lot, but if that changes abusers will have a
1282  *      nasty suprise.
1283  */
1284
1285 void ide_init_drive_cmd (struct request *rq)
1286 {
1287         memset(rq, 0, sizeof(*rq));
1288         rq->flags = REQ_DRIVE_CMD;
1289 }
1290
1291 EXPORT_SYMBOL(ide_init_drive_cmd);
1292
1293 /**
1294  *      ide_do_drive_cmd        -       issue IDE special command
1295  *      @drive: device to issue command
1296  *      @rq: request to issue
1297  *      @action: action for processing
1298  *
1299  *      This function issues a special IDE device request
1300  *      onto the request queue.
1301  *
1302  *      If action is ide_wait, then the rq is queued at the end of the
1303  *      request queue, and the function sleeps until it has been processed.
1304  *      This is for use when invoked from an ioctl handler.
1305  *
1306  *      If action is ide_preempt, then the rq is queued at the head of
1307  *      the request queue, displacing the currently-being-processed
1308  *      request and this function returns immediately without waiting
1309  *      for the new rq to be completed.  This is VERY DANGEROUS, and is
1310  *      intended for careful use by the ATAPI tape/cdrom driver code.
1311  *
1312  *      If action is ide_next, then the rq is queued immediately after
1313  *      the currently-being-processed-request (if any), and the function
1314  *      returns without waiting for the new rq to be completed.  As above,
1315  *      This is VERY DANGEROUS, and is intended for careful use by the
1316  *      ATAPI tape/cdrom driver code.
1317  *
1318  *      If action is ide_end, then the rq is queued at the end of the
1319  *      request queue, and the function returns immediately without waiting
1320  *      for the new rq to be completed. This is again intended for careful
1321  *      use by the ATAPI tape/cdrom driver code.
1322  */
1323  
1324 int ide_do_drive_cmd (ide_drive_t *drive, struct request *rq, ide_action_t action)
1325 {
1326         unsigned long flags;
1327         ide_hwgroup_t *hwgroup = HWGROUP(drive);
1328         DECLARE_COMPLETION(wait);
1329         int where = ELEVATOR_INSERT_BACK, err;
1330         int must_wait = (action == ide_wait || action == ide_head_wait);
1331
1332 #ifdef CONFIG_BLK_DEV_PDC4030
1333         /*
1334          *      FIXME: there should be a drive or hwif->special
1335          *      handler that points here by default, not hacks
1336          *      in the ide-io.c code
1337          *
1338          *      FIXME2: That code breaks power management if used with
1339          *      this chipset, that really doesn't belong here !
1340          */
1341         if (HWIF(drive)->chipset == ide_pdc4030 && rq->buffer != NULL)
1342                 return -ENOSYS;  /* special drive cmds not supported */
1343 #endif
1344         rq->errors = 0;
1345         rq->rq_status = RQ_ACTIVE;
1346
1347         rq->rq_disk = drive->disk;
1348
1349         /*
1350          * we need to hold an extra reference to request for safe inspection
1351          * after completion
1352          */
1353         if (must_wait) {
1354                 rq->ref_count++;
1355                 rq->waiting = &wait;
1356         }
1357
1358         spin_lock_irqsave(&ide_lock, flags);
1359         if (action == ide_preempt)
1360                 hwgroup->rq = NULL;
1361         if (action == ide_preempt || action == ide_head_wait) {
1362                 where = ELEVATOR_INSERT_FRONT;
1363                 rq->flags |= REQ_PREEMPT;
1364         }
1365         __elv_add_request(drive->queue, rq, where, 0);
1366         ide_do_request(hwgroup, IDE_NO_IRQ);
1367         spin_unlock_irqrestore(&ide_lock, flags);
1368
1369         err = 0;
1370         if (must_wait) {
1371                 wait_for_completion(&wait);
1372                 rq->waiting = NULL;
1373                 if (rq->errors)
1374                         err = -EIO;
1375
1376                 blk_put_request(rq);
1377         }
1378
1379         return err;
1380 }
1381
1382 EXPORT_SYMBOL(ide_do_drive_cmd);