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