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