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