aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/ide/ide-io.c
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/ide/ide-io.c')
-rw-r--r--drivers/ide/ide-io.c856
1 files changed, 272 insertions, 584 deletions
diff --git a/drivers/ide/ide-io.c b/drivers/ide/ide-io.c
index cc35d6dbd410..cc163319dfbd 100644
--- a/drivers/ide/ide-io.c
+++ b/drivers/ide/ide-io.c
@@ -84,11 +84,11 @@ static int __ide_end_request(ide_drive_t *drive, struct request *rq,
84 ide_dma_on(drive); 84 ide_dma_on(drive);
85 } 85 }
86 86
87 if (!__blk_end_request(rq, error, nr_bytes)) { 87 if (!blk_end_request(rq, error, nr_bytes))
88 if (dequeue)
89 HWGROUP(drive)->rq = NULL;
90 ret = 0; 88 ret = 0;
91 } 89
90 if (ret == 0 && dequeue)
91 drive->hwif->rq = NULL;
92 92
93 return ret; 93 return ret;
94} 94}
@@ -107,16 +107,7 @@ static int __ide_end_request(ide_drive_t *drive, struct request *rq,
107int ide_end_request (ide_drive_t *drive, int uptodate, int nr_sectors) 107int ide_end_request (ide_drive_t *drive, int uptodate, int nr_sectors)
108{ 108{
109 unsigned int nr_bytes = nr_sectors << 9; 109 unsigned int nr_bytes = nr_sectors << 9;
110 struct request *rq; 110 struct request *rq = drive->hwif->rq;
111 unsigned long flags;
112 int ret = 1;
113
114 /*
115 * room for locking improvements here, the calls below don't
116 * need the queue lock held at all
117 */
118 spin_lock_irqsave(&ide_lock, flags);
119 rq = HWGROUP(drive)->rq;
120 111
121 if (!nr_bytes) { 112 if (!nr_bytes) {
122 if (blk_pc_request(rq)) 113 if (blk_pc_request(rq))
@@ -125,105 +116,10 @@ int ide_end_request (ide_drive_t *drive, int uptodate, int nr_sectors)
125 nr_bytes = rq->hard_cur_sectors << 9; 116 nr_bytes = rq->hard_cur_sectors << 9;
126 } 117 }
127 118
128 ret = __ide_end_request(drive, rq, uptodate, nr_bytes, 1); 119 return __ide_end_request(drive, rq, uptodate, nr_bytes, 1);
129
130 spin_unlock_irqrestore(&ide_lock, flags);
131 return ret;
132} 120}
133EXPORT_SYMBOL(ide_end_request); 121EXPORT_SYMBOL(ide_end_request);
134 122
135static void ide_complete_power_step(ide_drive_t *drive, struct request *rq)
136{
137 struct request_pm_state *pm = rq->data;
138
139#ifdef DEBUG_PM
140 printk(KERN_INFO "%s: complete_power_step(step: %d)\n",
141 drive->name, pm->pm_step);
142#endif
143 if (drive->media != ide_disk)
144 return;
145
146 switch (pm->pm_step) {
147 case IDE_PM_FLUSH_CACHE: /* Suspend step 1 (flush cache) */
148 if (pm->pm_state == PM_EVENT_FREEZE)
149 pm->pm_step = IDE_PM_COMPLETED;
150 else
151 pm->pm_step = IDE_PM_STANDBY;
152 break;
153 case IDE_PM_STANDBY: /* Suspend step 2 (standby) */
154 pm->pm_step = IDE_PM_COMPLETED;
155 break;
156 case IDE_PM_RESTORE_PIO: /* Resume step 1 (restore PIO) */
157 pm->pm_step = IDE_PM_IDLE;
158 break;
159 case IDE_PM_IDLE: /* Resume step 2 (idle)*/
160 pm->pm_step = IDE_PM_RESTORE_DMA;
161 break;
162 }
163}
164
165static ide_startstop_t ide_start_power_step(ide_drive_t *drive, struct request *rq)
166{
167 struct request_pm_state *pm = rq->data;
168 ide_task_t *args = rq->special;
169
170 memset(args, 0, sizeof(*args));
171
172 switch (pm->pm_step) {
173 case IDE_PM_FLUSH_CACHE: /* Suspend step 1 (flush cache) */
174 if (drive->media != ide_disk)
175 break;
176 /* Not supported? Switch to next step now. */
177 if (ata_id_flush_enabled(drive->id) == 0 ||
178 (drive->dev_flags & IDE_DFLAG_WCACHE) == 0) {
179 ide_complete_power_step(drive, rq);
180 return ide_stopped;
181 }
182 if (ata_id_flush_ext_enabled(drive->id))
183 args->tf.command = ATA_CMD_FLUSH_EXT;
184 else
185 args->tf.command = ATA_CMD_FLUSH;
186 goto out_do_tf;
187 case IDE_PM_STANDBY: /* Suspend step 2 (standby) */
188 args->tf.command = ATA_CMD_STANDBYNOW1;
189 goto out_do_tf;
190 case IDE_PM_RESTORE_PIO: /* Resume step 1 (restore PIO) */
191 ide_set_max_pio(drive);
192 /*
193 * skip IDE_PM_IDLE for ATAPI devices
194 */
195 if (drive->media != ide_disk)
196 pm->pm_step = IDE_PM_RESTORE_DMA;
197 else
198 ide_complete_power_step(drive, rq);
199 return ide_stopped;
200 case IDE_PM_IDLE: /* Resume step 2 (idle) */
201 args->tf.command = ATA_CMD_IDLEIMMEDIATE;
202 goto out_do_tf;
203 case IDE_PM_RESTORE_DMA: /* Resume step 3 (restore DMA) */
204 /*
205 * Right now, all we do is call ide_set_dma(drive),
206 * we could be smarter and check for current xfer_speed
207 * in struct drive etc...
208 */
209 if (drive->hwif->dma_ops == NULL)
210 break;
211 /*
212 * TODO: respect IDE_DFLAG_USING_DMA
213 */
214 ide_set_dma(drive);
215 break;
216 }
217
218 pm->pm_step = IDE_PM_COMPLETED;
219 return ide_stopped;
220
221out_do_tf:
222 args->tf_flags = IDE_TFLAG_TF | IDE_TFLAG_DEVICE;
223 args->data_phase = TASKFILE_NO_DATA;
224 return do_rw_taskfile(drive, args);
225}
226
227/** 123/**
228 * ide_end_dequeued_request - complete an IDE I/O 124 * ide_end_dequeued_request - complete an IDE I/O
229 * @drive: IDE device for the I/O 125 * @drive: IDE device for the I/O
@@ -242,48 +138,12 @@ out_do_tf:
242int ide_end_dequeued_request(ide_drive_t *drive, struct request *rq, 138int ide_end_dequeued_request(ide_drive_t *drive, struct request *rq,
243 int uptodate, int nr_sectors) 139 int uptodate, int nr_sectors)
244{ 140{
245 unsigned long flags;
246 int ret;
247
248 spin_lock_irqsave(&ide_lock, flags);
249 BUG_ON(!blk_rq_started(rq)); 141 BUG_ON(!blk_rq_started(rq));
250 ret = __ide_end_request(drive, rq, uptodate, nr_sectors << 9, 0);
251 spin_unlock_irqrestore(&ide_lock, flags);
252 142
253 return ret; 143 return __ide_end_request(drive, rq, uptodate, nr_sectors << 9, 0);
254} 144}
255EXPORT_SYMBOL_GPL(ide_end_dequeued_request); 145EXPORT_SYMBOL_GPL(ide_end_dequeued_request);
256 146
257
258/**
259 * ide_complete_pm_request - end the current Power Management request
260 * @drive: target drive
261 * @rq: request
262 *
263 * This function cleans up the current PM request and stops the queue
264 * if necessary.
265 */
266static void ide_complete_pm_request (ide_drive_t *drive, struct request *rq)
267{
268 unsigned long flags;
269
270#ifdef DEBUG_PM
271 printk("%s: completing PM request, %s\n", drive->name,
272 blk_pm_suspend_request(rq) ? "suspend" : "resume");
273#endif
274 spin_lock_irqsave(&ide_lock, flags);
275 if (blk_pm_suspend_request(rq)) {
276 blk_stop_queue(drive->queue);
277 } else {
278 drive->dev_flags &= ~IDE_DFLAG_BLOCKED;
279 blk_start_queue(drive->queue);
280 }
281 HWGROUP(drive)->rq = NULL;
282 if (__blk_end_request(rq, 0, 0))
283 BUG();
284 spin_unlock_irqrestore(&ide_lock, flags);
285}
286
287/** 147/**
288 * ide_end_drive_cmd - end an explicit drive command 148 * ide_end_drive_cmd - end an explicit drive command
289 * @drive: command 149 * @drive: command
@@ -300,19 +160,12 @@ static void ide_complete_pm_request (ide_drive_t *drive, struct request *rq)
300 160
301void ide_end_drive_cmd (ide_drive_t *drive, u8 stat, u8 err) 161void ide_end_drive_cmd (ide_drive_t *drive, u8 stat, u8 err)
302{ 162{
303 unsigned long flags; 163 ide_hwif_t *hwif = drive->hwif;
304 struct request *rq; 164 struct request *rq = hwif->rq;
305
306 spin_lock_irqsave(&ide_lock, flags);
307 rq = HWGROUP(drive)->rq;
308 spin_unlock_irqrestore(&ide_lock, flags);
309 165
310 if (rq->cmd_type == REQ_TYPE_ATA_TASKFILE) { 166 if (rq->cmd_type == REQ_TYPE_ATA_TASKFILE) {
311 ide_task_t *task = (ide_task_t *)rq->special; 167 ide_task_t *task = (ide_task_t *)rq->special;
312 168
313 if (rq->errors == 0)
314 rq->errors = !OK_STAT(stat, ATA_DRDY, BAD_STAT);
315
316 if (task) { 169 if (task) {
317 struct ide_taskfile *tf = &task->tf; 170 struct ide_taskfile *tf = &task->tf;
318 171
@@ -333,23 +186,22 @@ void ide_end_drive_cmd (ide_drive_t *drive, u8 stat, u8 err)
333 return; 186 return;
334 } 187 }
335 188
336 spin_lock_irqsave(&ide_lock, flags); 189 hwif->rq = NULL;
337 HWGROUP(drive)->rq = NULL; 190
338 rq->errors = err; 191 rq->errors = err;
339 if (unlikely(__blk_end_request(rq, (rq->errors ? -EIO : 0), 192
340 blk_rq_bytes(rq)))) 193 if (unlikely(blk_end_request(rq, (rq->errors ? -EIO : 0),
194 blk_rq_bytes(rq))))
341 BUG(); 195 BUG();
342 spin_unlock_irqrestore(&ide_lock, flags);
343} 196}
344
345EXPORT_SYMBOL(ide_end_drive_cmd); 197EXPORT_SYMBOL(ide_end_drive_cmd);
346 198
347static void ide_kill_rq(ide_drive_t *drive, struct request *rq) 199static void ide_kill_rq(ide_drive_t *drive, struct request *rq)
348{ 200{
349 if (rq->rq_disk) { 201 if (rq->rq_disk) {
350 ide_driver_t *drv; 202 struct ide_driver *drv;
351 203
352 drv = *(ide_driver_t **)rq->rq_disk->private_data; 204 drv = *(struct ide_driver **)rq->rq_disk->private_data;
353 drv->end_request(drive, 0, 0); 205 drv->end_request(drive, 0, 0);
354 } else 206 } else
355 ide_end_request(drive, 0, 0); 207 ide_end_request(drive, 0, 0);
@@ -439,7 +291,7 @@ static ide_startstop_t ide_atapi_error(ide_drive_t *drive, struct request *rq, u
439 return ide_stopped; 291 return ide_stopped;
440} 292}
441 293
442ide_startstop_t 294static ide_startstop_t
443__ide_error(ide_drive_t *drive, struct request *rq, u8 stat, u8 err) 295__ide_error(ide_drive_t *drive, struct request *rq, u8 stat, u8 err)
444{ 296{
445 if (drive->media == ide_disk) 297 if (drive->media == ide_disk)
@@ -447,8 +299,6 @@ __ide_error(ide_drive_t *drive, struct request *rq, u8 stat, u8 err)
447 return ide_atapi_error(drive, rq, stat, err); 299 return ide_atapi_error(drive, rq, stat, err);
448} 300}
449 301
450EXPORT_SYMBOL_GPL(__ide_error);
451
452/** 302/**
453 * ide_error - handle an error on the IDE 303 * ide_error - handle an error on the IDE
454 * @drive: drive the error occurred on 304 * @drive: drive the error occurred on
@@ -469,7 +319,8 @@ ide_startstop_t ide_error (ide_drive_t *drive, const char *msg, u8 stat)
469 319
470 err = ide_dump_status(drive, msg, stat); 320 err = ide_dump_status(drive, msg, stat);
471 321
472 if ((rq = HWGROUP(drive)->rq) == NULL) 322 rq = drive->hwif->rq;
323 if (rq == NULL)
473 return ide_stopped; 324 return ide_stopped;
474 325
475 /* retry only "normal" I/O: */ 326 /* retry only "normal" I/O: */
@@ -479,15 +330,8 @@ ide_startstop_t ide_error (ide_drive_t *drive, const char *msg, u8 stat)
479 return ide_stopped; 330 return ide_stopped;
480 } 331 }
481 332
482 if (rq->rq_disk) { 333 return __ide_error(drive, rq, stat, err);
483 ide_driver_t *drv;
484
485 drv = *(ide_driver_t **)rq->rq_disk->private_data;
486 return drv->error(drive, rq, stat, err);
487 } else
488 return __ide_error(drive, rq, stat, err);
489} 334}
490
491EXPORT_SYMBOL_GPL(ide_error); 335EXPORT_SYMBOL_GPL(ide_error);
492 336
493static void ide_tf_set_specify_cmd(ide_drive_t *drive, struct ide_taskfile *tf) 337static void ide_tf_set_specify_cmd(ide_drive_t *drive, struct ide_taskfile *tf)
@@ -574,9 +418,6 @@ void ide_map_sg(ide_drive_t *drive, struct request *rq)
574 ide_hwif_t *hwif = drive->hwif; 418 ide_hwif_t *hwif = drive->hwif;
575 struct scatterlist *sg = hwif->sg_table; 419 struct scatterlist *sg = hwif->sg_table;
576 420
577 if (hwif->sg_mapped) /* needed by ide-scsi */
578 return;
579
580 if (rq->cmd_type != REQ_TYPE_ATA_TASKFILE) { 421 if (rq->cmd_type != REQ_TYPE_ATA_TASKFILE) {
581 hwif->sg_nents = blk_rq_map_sg(drive->queue, rq, sg); 422 hwif->sg_nents = blk_rq_map_sg(drive->queue, rq, sg);
582 } else { 423 } else {
@@ -613,7 +454,7 @@ EXPORT_SYMBOL_GPL(ide_init_sg_cmd);
613static ide_startstop_t execute_drive_cmd (ide_drive_t *drive, 454static ide_startstop_t execute_drive_cmd (ide_drive_t *drive,
614 struct request *rq) 455 struct request *rq)
615{ 456{
616 ide_hwif_t *hwif = HWIF(drive); 457 ide_hwif_t *hwif = drive->hwif;
617 ide_task_t *task = rq->special; 458 ide_task_t *task = rq->special;
618 459
619 if (task) { 460 if (task) {
@@ -720,40 +561,6 @@ static ide_startstop_t ide_special_rq(ide_drive_t *drive, struct request *rq)
720 } 561 }
721} 562}
722 563
723static void ide_check_pm_state(ide_drive_t *drive, struct request *rq)
724{
725 struct request_pm_state *pm = rq->data;
726
727 if (blk_pm_suspend_request(rq) &&
728 pm->pm_step == IDE_PM_START_SUSPEND)
729 /* Mark drive blocked when starting the suspend sequence. */
730 drive->dev_flags |= IDE_DFLAG_BLOCKED;
731 else if (blk_pm_resume_request(rq) &&
732 pm->pm_step == IDE_PM_START_RESUME) {
733 /*
734 * The first thing we do on wakeup is to wait for BSY bit to
735 * go away (with a looong timeout) as a drive on this hwif may
736 * just be POSTing itself.
737 * We do that before even selecting as the "other" device on
738 * the bus may be broken enough to walk on our toes at this
739 * point.
740 */
741 ide_hwif_t *hwif = drive->hwif;
742 int rc;
743#ifdef DEBUG_PM
744 printk("%s: Wakeup request inited, waiting for !BSY...\n", drive->name);
745#endif
746 rc = ide_wait_not_busy(hwif, 35000);
747 if (rc)
748 printk(KERN_WARNING "%s: bus not ready on wakeup\n", drive->name);
749 SELECT_DRIVE(drive);
750 hwif->tp_ops->set_irq(hwif, 1);
751 rc = ide_wait_not_busy(hwif, 100000);
752 if (rc)
753 printk(KERN_WARNING "%s: drive not ready on wakeup\n", drive->name);
754 }
755}
756
757/** 564/**
758 * start_request - start of I/O and command issuing for IDE 565 * start_request - start of I/O and command issuing for IDE
759 * 566 *
@@ -771,7 +578,7 @@ static ide_startstop_t start_request (ide_drive_t *drive, struct request *rq)
771 578
772#ifdef DEBUG 579#ifdef DEBUG
773 printk("%s: start_request: current=0x%08lx\n", 580 printk("%s: start_request: current=0x%08lx\n",
774 HWIF(drive)->name, (unsigned long) rq); 581 drive->hwif->name, (unsigned long) rq);
775#endif 582#endif
776 583
777 /* bail early if we've exceeded max_failures */ 584 /* bail early if we've exceeded max_failures */
@@ -790,7 +597,7 @@ static ide_startstop_t start_request (ide_drive_t *drive, struct request *rq)
790 return startstop; 597 return startstop;
791 } 598 }
792 if (!drive->special.all) { 599 if (!drive->special.all) {
793 ide_driver_t *drv; 600 struct ide_driver *drv;
794 601
795 /* 602 /*
796 * We reset the drive so we need to issue a SETFEATURES. 603 * We reset the drive so we need to issue a SETFEATURES.
@@ -823,7 +630,7 @@ static ide_startstop_t start_request (ide_drive_t *drive, struct request *rq)
823 */ 630 */
824 return ide_special_rq(drive, rq); 631 return ide_special_rq(drive, rq);
825 632
826 drv = *(ide_driver_t **)rq->rq_disk->private_data; 633 drv = *(struct ide_driver **)rq->rq_disk->private_data;
827 634
828 return drv->do_request(drive, rq, rq->sector); 635 return drv->do_request(drive, rq, rq->sector);
829 } 636 }
@@ -839,7 +646,7 @@ kill_rq:
839 * @timeout: time to stall for (jiffies) 646 * @timeout: time to stall for (jiffies)
840 * 647 *
841 * ide_stall_queue() can be used by a drive to give excess bandwidth back 648 * ide_stall_queue() can be used by a drive to give excess bandwidth back
842 * to the hwgroup by sleeping for timeout jiffies. 649 * to the port by sleeping for timeout jiffies.
843 */ 650 */
844 651
845void ide_stall_queue (ide_drive_t *drive, unsigned long timeout) 652void ide_stall_queue (ide_drive_t *drive, unsigned long timeout)
@@ -849,206 +656,117 @@ void ide_stall_queue (ide_drive_t *drive, unsigned long timeout)
849 drive->sleep = timeout + jiffies; 656 drive->sleep = timeout + jiffies;
850 drive->dev_flags |= IDE_DFLAG_SLEEPING; 657 drive->dev_flags |= IDE_DFLAG_SLEEPING;
851} 658}
852
853EXPORT_SYMBOL(ide_stall_queue); 659EXPORT_SYMBOL(ide_stall_queue);
854 660
855#define WAKEUP(drive) ((drive)->service_start + 2 * (drive)->service_time) 661static inline int ide_lock_port(ide_hwif_t *hwif)
662{
663 if (hwif->busy)
664 return 1;
856 665
857/** 666 hwif->busy = 1;
858 * choose_drive - select a drive to service 667
859 * @hwgroup: hardware group to select on 668 return 0;
860 * 669}
861 * choose_drive() selects the next drive which will be serviced. 670
862 * This is necessary because the IDE layer can't issue commands 671static inline void ide_unlock_port(ide_hwif_t *hwif)
863 * to both drives on the same cable, unlike SCSI. 672{
864 */ 673 hwif->busy = 0;
865 674}
866static inline ide_drive_t *choose_drive (ide_hwgroup_t *hwgroup) 675
676static inline int ide_lock_host(struct ide_host *host, ide_hwif_t *hwif)
867{ 677{
868 ide_drive_t *drive, *best; 678 int rc = 0;
869 679
870repeat: 680 if (host->host_flags & IDE_HFLAG_SERIALIZE) {
871 best = NULL; 681 rc = test_and_set_bit_lock(IDE_HOST_BUSY, &host->host_busy);
872 drive = hwgroup->drive; 682 if (rc == 0) {
683 /* for atari only */
684 ide_get_lock(ide_intr, hwif);
685 }
686 }
687 return rc;
688}
689
690static inline void ide_unlock_host(struct ide_host *host)
691{
692 if (host->host_flags & IDE_HFLAG_SERIALIZE) {
693 /* for atari only */
694 ide_release_lock();
695 clear_bit_unlock(IDE_HOST_BUSY, &host->host_busy);
696 }
697}
698
699/*
700 * Issue a new request to a device.
701 */
702void do_ide_request(struct request_queue *q)
703{
704 ide_drive_t *drive = q->queuedata;
705 ide_hwif_t *hwif = drive->hwif;
706 struct ide_host *host = hwif->host;
707 struct request *rq = NULL;
708 ide_startstop_t startstop;
873 709
874 /* 710 /*
875 * drive is doing pre-flush, ordered write, post-flush sequence. even 711 * drive is doing pre-flush, ordered write, post-flush sequence. even
876 * though that is 3 requests, it must be seen as a single transaction. 712 * though that is 3 requests, it must be seen as a single transaction.
877 * we must not preempt this drive until that is complete 713 * we must not preempt this drive until that is complete
878 */ 714 */
879 if (blk_queue_flushing(drive->queue)) { 715 if (blk_queue_flushing(q))
880 /* 716 /*
881 * small race where queue could get replugged during 717 * small race where queue could get replugged during
882 * the 3-request flush cycle, just yank the plug since 718 * the 3-request flush cycle, just yank the plug since
883 * we want it to finish asap 719 * we want it to finish asap
884 */ 720 */
885 blk_remove_plug(drive->queue); 721 blk_remove_plug(q);
886 return drive;
887 }
888 722
889 do { 723 spin_unlock_irq(q->queue_lock);
890 u8 dev_s = !!(drive->dev_flags & IDE_DFLAG_SLEEPING);
891 u8 best_s = (best && !!(best->dev_flags & IDE_DFLAG_SLEEPING));
892
893 if ((dev_s == 0 || time_after_eq(jiffies, drive->sleep)) &&
894 !elv_queue_empty(drive->queue)) {
895 if (best == NULL ||
896 (dev_s && (best_s == 0 || time_before(drive->sleep, best->sleep))) ||
897 (best_s == 0 && time_before(WAKEUP(drive), WAKEUP(best)))) {
898 if (!blk_queue_plugged(drive->queue))
899 best = drive;
900 }
901 }
902 } while ((drive = drive->next) != hwgroup->drive);
903 724
904 if (best && (best->dev_flags & IDE_DFLAG_NICE1) && 725 if (ide_lock_host(host, hwif))
905 (best->dev_flags & IDE_DFLAG_SLEEPING) == 0 && 726 goto plug_device_2;
906 best != hwgroup->drive && best->service_time > WAIT_MIN_SLEEP) {
907 long t = (signed long)(WAKEUP(best) - jiffies);
908 if (t >= WAIT_MIN_SLEEP) {
909 /*
910 * We *may* have some time to spare, but first let's see if
911 * someone can potentially benefit from our nice mood today..
912 */
913 drive = best->next;
914 do {
915 if ((drive->dev_flags & IDE_DFLAG_SLEEPING) == 0
916 && time_before(jiffies - best->service_time, WAKEUP(drive))
917 && time_before(WAKEUP(drive), jiffies + t))
918 {
919 ide_stall_queue(best, min_t(long, t, 10 * WAIT_MIN_SLEEP));
920 goto repeat;
921 }
922 } while ((drive = drive->next) != best);
923 }
924 }
925 return best;
926}
927 727
928/* 728 spin_lock_irq(&hwif->lock);
929 * Issue a new request to a drive from hwgroup
930 * Caller must have already done spin_lock_irqsave(&ide_lock, ..);
931 *
932 * A hwgroup is a serialized group of IDE interfaces. Usually there is
933 * exactly one hwif (interface) per hwgroup, but buggy controllers (eg. CMD640)
934 * may have both interfaces in a single hwgroup to "serialize" access.
935 * Or possibly multiple ISA interfaces can share a common IRQ by being grouped
936 * together into one hwgroup for serialized access.
937 *
938 * Note also that several hwgroups can end up sharing a single IRQ,
939 * possibly along with many other devices. This is especially common in
940 * PCI-based systems with off-board IDE controller cards.
941 *
942 * The IDE driver uses the single global ide_lock spinlock to protect
943 * access to the request queues, and to protect the hwgroup->busy flag.
944 *
945 * The first thread into the driver for a particular hwgroup sets the
946 * hwgroup->busy flag to indicate that this hwgroup is now active,
947 * and then initiates processing of the top request from the request queue.
948 *
949 * Other threads attempting entry notice the busy setting, and will simply
950 * queue their new requests and exit immediately. Note that hwgroup->busy
951 * remains set even when the driver is merely awaiting the next interrupt.
952 * Thus, the meaning is "this hwgroup is busy processing a request".
953 *
954 * When processing of a request completes, the completing thread or IRQ-handler
955 * will start the next request from the queue. If no more work remains,
956 * the driver will clear the hwgroup->busy flag and exit.
957 *
958 * The ide_lock (spinlock) is used to protect all access to the
959 * hwgroup->busy flag, but is otherwise not needed for most processing in
960 * the driver. This makes the driver much more friendlier to shared IRQs
961 * than previous designs, while remaining 100% (?) SMP safe and capable.
962 */
963static void ide_do_request (ide_hwgroup_t *hwgroup, int masked_irq)
964{
965 ide_drive_t *drive;
966 ide_hwif_t *hwif;
967 struct request *rq;
968 ide_startstop_t startstop;
969 int loops = 0;
970 729
971 /* caller must own ide_lock */ 730 if (!ide_lock_port(hwif)) {
972 BUG_ON(!irqs_disabled()); 731 ide_hwif_t *prev_port;
732repeat:
733 prev_port = hwif->host->cur_port;
734 hwif->rq = NULL;
973 735
974 while (!hwgroup->busy) { 736 if (drive->dev_flags & IDE_DFLAG_SLEEPING) {
975 hwgroup->busy = 1; 737 if (time_before(drive->sleep, jiffies)) {
976 /* for atari only */ 738 ide_unlock_port(hwif);
977 ide_get_lock(ide_intr, hwgroup); 739 goto plug_device;
978 drive = choose_drive(hwgroup);
979 if (drive == NULL) {
980 int sleeping = 0;
981 unsigned long sleep = 0; /* shut up, gcc */
982 hwgroup->rq = NULL;
983 drive = hwgroup->drive;
984 do {
985 if ((drive->dev_flags & IDE_DFLAG_SLEEPING) &&
986 (sleeping == 0 ||
987 time_before(drive->sleep, sleep))) {
988 sleeping = 1;
989 sleep = drive->sleep;
990 }
991 } while ((drive = drive->next) != hwgroup->drive);
992 if (sleeping) {
993 /*
994 * Take a short snooze, and then wake up this hwgroup again.
995 * This gives other hwgroups on the same a chance to
996 * play fairly with us, just in case there are big differences
997 * in relative throughputs.. don't want to hog the cpu too much.
998 */
999 if (time_before(sleep, jiffies + WAIT_MIN_SLEEP))
1000 sleep = jiffies + WAIT_MIN_SLEEP;
1001#if 1
1002 if (timer_pending(&hwgroup->timer))
1003 printk(KERN_CRIT "ide_set_handler: timer already active\n");
1004#endif
1005 /* so that ide_timer_expiry knows what to do */
1006 hwgroup->sleeping = 1;
1007 hwgroup->req_gen_timer = hwgroup->req_gen;
1008 mod_timer(&hwgroup->timer, sleep);
1009 /* we purposely leave hwgroup->busy==1
1010 * while sleeping */
1011 } else {
1012 /* Ugly, but how can we sleep for the lock
1013 * otherwise? perhaps from tq_disk?
1014 */
1015
1016 /* for atari only */
1017 ide_release_lock();
1018 hwgroup->busy = 0;
1019 } 740 }
1020
1021 /* no more work for this hwgroup (for now) */
1022 return;
1023 } 741 }
1024 again: 742
1025 hwif = HWIF(drive); 743 if ((hwif->host->host_flags & IDE_HFLAG_SERIALIZE) &&
1026 if (hwgroup->hwif->sharing_irq && hwif != hwgroup->hwif) { 744 hwif != prev_port) {
1027 /* 745 /*
1028 * set nIEN for previous hwif, drives in the 746 * set nIEN for previous port, drives in the
1029 * quirk_list may not like intr setups/cleanups 747 * quirk_list may not like intr setups/cleanups
1030 */ 748 */
1031 if (drive->quirk_list != 1) 749 if (prev_port && prev_port->cur_dev->quirk_list == 0)
1032 hwif->tp_ops->set_irq(hwif, 0); 750 prev_port->tp_ops->set_irq(prev_port, 0);
1033 }
1034 hwgroup->hwif = hwif;
1035 hwgroup->drive = drive;
1036 drive->dev_flags &= ~(IDE_DFLAG_SLEEPING | IDE_DFLAG_PARKED);
1037 drive->service_start = jiffies;
1038 751
1039 if (blk_queue_plugged(drive->queue)) { 752 hwif->host->cur_port = hwif;
1040 printk(KERN_ERR "ide: huh? queue was plugged!\n");
1041 break;
1042 } 753 }
754 hwif->cur_dev = drive;
755 drive->dev_flags &= ~(IDE_DFLAG_SLEEPING | IDE_DFLAG_PARKED);
1043 756
757 spin_unlock_irq(&hwif->lock);
758 spin_lock_irq(q->queue_lock);
1044 /* 759 /*
1045 * we know that the queue isn't empty, but this can happen 760 * we know that the queue isn't empty, but this can happen
1046 * if the q->prep_rq_fn() decides to kill a request 761 * if the q->prep_rq_fn() decides to kill a request
1047 */ 762 */
1048 rq = elv_next_request(drive->queue); 763 rq = elv_next_request(drive->queue);
764 spin_unlock_irq(q->queue_lock);
765 spin_lock_irq(&hwif->lock);
766
1049 if (!rq) { 767 if (!rq) {
1050 hwgroup->busy = 0; 768 ide_unlock_port(hwif);
1051 break; 769 goto out;
1052 } 770 }
1053 771
1054 /* 772 /*
@@ -1063,63 +781,50 @@ static void ide_do_request (ide_hwgroup_t *hwgroup, int masked_irq)
1063 * though. I hope that doesn't happen too much, hopefully not 781 * though. I hope that doesn't happen too much, hopefully not
1064 * unless the subdriver triggers such a thing in its own PM 782 * unless the subdriver triggers such a thing in its own PM
1065 * state machine. 783 * state machine.
1066 *
1067 * We count how many times we loop here to make sure we service
1068 * all drives in the hwgroup without looping for ever
1069 */ 784 */
1070 if ((drive->dev_flags & IDE_DFLAG_BLOCKED) && 785 if ((drive->dev_flags & IDE_DFLAG_BLOCKED) &&
1071 blk_pm_request(rq) == 0 && 786 blk_pm_request(rq) == 0 &&
1072 (rq->cmd_flags & REQ_PREEMPT) == 0) { 787 (rq->cmd_flags & REQ_PREEMPT) == 0) {
1073 drive = drive->next ? drive->next : hwgroup->drive; 788 /* there should be no pending command at this point */
1074 if (loops++ < 4 && !blk_queue_plugged(drive->queue)) 789 ide_unlock_port(hwif);
1075 goto again; 790 goto plug_device;
1076 /* We clear busy, there should be no pending ATA command at this point. */
1077 hwgroup->busy = 0;
1078 break;
1079 } 791 }
1080 792
1081 hwgroup->rq = rq; 793 hwif->rq = rq;
1082 794
1083 /* 795 spin_unlock_irq(&hwif->lock);
1084 * Some systems have trouble with IDE IRQs arriving while
1085 * the driver is still setting things up. So, here we disable
1086 * the IRQ used by this interface while the request is being started.
1087 * This may look bad at first, but pretty much the same thing
1088 * happens anyway when any interrupt comes in, IDE or otherwise
1089 * -- the kernel masks the IRQ while it is being handled.
1090 */
1091 if (masked_irq != IDE_NO_IRQ && hwif->irq != masked_irq)
1092 disable_irq_nosync(hwif->irq);
1093 spin_unlock(&ide_lock);
1094 local_irq_enable_in_hardirq();
1095 /* allow other IRQs while we start this request */
1096 startstop = start_request(drive, rq); 796 startstop = start_request(drive, rq);
1097 spin_lock_irq(&ide_lock); 797 spin_lock_irq(&hwif->lock);
1098 if (masked_irq != IDE_NO_IRQ && hwif->irq != masked_irq)
1099 enable_irq(hwif->irq);
1100 if (startstop == ide_stopped)
1101 hwgroup->busy = 0;
1102 }
1103}
1104 798
1105/* 799 if (startstop == ide_stopped)
1106 * Passes the stuff to ide_do_request 800 goto repeat;
1107 */ 801 } else
1108void do_ide_request(struct request_queue *q) 802 goto plug_device;
1109{ 803out:
1110 ide_drive_t *drive = q->queuedata; 804 spin_unlock_irq(&hwif->lock);
1111 805 if (rq == NULL)
1112 ide_do_request(HWGROUP(drive), IDE_NO_IRQ); 806 ide_unlock_host(host);
807 spin_lock_irq(q->queue_lock);
808 return;
809
810plug_device:
811 spin_unlock_irq(&hwif->lock);
812 ide_unlock_host(host);
813plug_device_2:
814 spin_lock_irq(q->queue_lock);
815
816 if (!elv_queue_empty(q))
817 blk_plug_device(q);
1113} 818}
1114 819
1115/* 820/*
1116 * un-busy the hwgroup etc, and clear any pending DMA status. we want to 821 * un-busy the port etc, and clear any pending DMA status. we want to
1117 * retry the current request in pio mode instead of risking tossing it 822 * retry the current request in pio mode instead of risking tossing it
1118 * all away 823 * all away
1119 */ 824 */
1120static ide_startstop_t ide_dma_timeout_retry(ide_drive_t *drive, int error) 825static ide_startstop_t ide_dma_timeout_retry(ide_drive_t *drive, int error)
1121{ 826{
1122 ide_hwif_t *hwif = HWIF(drive); 827 ide_hwif_t *hwif = drive->hwif;
1123 struct request *rq; 828 struct request *rq;
1124 ide_startstop_t ret = ide_stopped; 829 ide_startstop_t ret = ide_stopped;
1125 830
@@ -1147,15 +852,14 @@ static ide_startstop_t ide_dma_timeout_retry(ide_drive_t *drive, int error)
1147 ide_dma_off_quietly(drive); 852 ide_dma_off_quietly(drive);
1148 853
1149 /* 854 /*
1150 * un-busy drive etc (hwgroup->busy is cleared on return) and 855 * un-busy drive etc and make sure request is sane
1151 * make sure request is sane
1152 */ 856 */
1153 rq = HWGROUP(drive)->rq;
1154 857
858 rq = hwif->rq;
1155 if (!rq) 859 if (!rq)
1156 goto out; 860 goto out;
1157 861
1158 HWGROUP(drive)->rq = NULL; 862 hwif->rq = NULL;
1159 863
1160 rq->errors = 0; 864 rq->errors = 0;
1161 865
@@ -1170,9 +874,20 @@ out:
1170 return ret; 874 return ret;
1171} 875}
1172 876
877static void ide_plug_device(ide_drive_t *drive)
878{
879 struct request_queue *q = drive->queue;
880 unsigned long flags;
881
882 spin_lock_irqsave(q->queue_lock, flags);
883 if (!elv_queue_empty(q))
884 blk_plug_device(q);
885 spin_unlock_irqrestore(q->queue_lock, flags);
886}
887
1173/** 888/**
1174 * ide_timer_expiry - handle lack of an IDE interrupt 889 * ide_timer_expiry - handle lack of an IDE interrupt
1175 * @data: timer callback magic (hwgroup) 890 * @data: timer callback magic (hwif)
1176 * 891 *
1177 * An IDE command has timed out before the expected drive return 892 * An IDE command has timed out before the expected drive return
1178 * occurred. At this point we attempt to clean up the current 893 * occurred. At this point we attempt to clean up the current
@@ -1186,93 +901,87 @@ out:
1186 901
1187void ide_timer_expiry (unsigned long data) 902void ide_timer_expiry (unsigned long data)
1188{ 903{
1189 ide_hwgroup_t *hwgroup = (ide_hwgroup_t *) data; 904 ide_hwif_t *hwif = (ide_hwif_t *)data;
905 ide_drive_t *uninitialized_var(drive);
1190 ide_handler_t *handler; 906 ide_handler_t *handler;
1191 ide_expiry_t *expiry;
1192 unsigned long flags; 907 unsigned long flags;
1193 unsigned long wait = -1; 908 unsigned long wait = -1;
909 int plug_device = 0;
1194 910
1195 spin_lock_irqsave(&ide_lock, flags); 911 spin_lock_irqsave(&hwif->lock, flags);
1196 912
1197 if (((handler = hwgroup->handler) == NULL) || 913 handler = hwif->handler;
1198 (hwgroup->req_gen != hwgroup->req_gen_timer)) { 914
915 if (handler == NULL || hwif->req_gen != hwif->req_gen_timer) {
1199 /* 916 /*
1200 * Either a marginal timeout occurred 917 * Either a marginal timeout occurred
1201 * (got the interrupt just as timer expired), 918 * (got the interrupt just as timer expired),
1202 * or we were "sleeping" to give other devices a chance. 919 * or we were "sleeping" to give other devices a chance.
1203 * Either way, we don't really want to complain about anything. 920 * Either way, we don't really want to complain about anything.
1204 */ 921 */
1205 if (hwgroup->sleeping) {
1206 hwgroup->sleeping = 0;
1207 hwgroup->busy = 0;
1208 }
1209 } else { 922 } else {
1210 ide_drive_t *drive = hwgroup->drive; 923 ide_expiry_t *expiry = hwif->expiry;
1211 if (!drive) { 924 ide_startstop_t startstop = ide_stopped;
1212 printk(KERN_ERR "ide_timer_expiry: hwgroup->drive was NULL\n"); 925
1213 hwgroup->handler = NULL; 926 drive = hwif->cur_dev;
1214 } else { 927
1215 ide_hwif_t *hwif; 928 if (expiry) {
1216 ide_startstop_t startstop = ide_stopped; 929 wait = expiry(drive);
1217 if (!hwgroup->busy) { 930 if (wait > 0) { /* continue */
1218 hwgroup->busy = 1; /* paranoia */ 931 /* reset timer */
1219 printk(KERN_ERR "%s: ide_timer_expiry: hwgroup->busy was 0 ??\n", drive->name); 932 hwif->timer.expires = jiffies + wait;
1220 } 933 hwif->req_gen_timer = hwif->req_gen;
1221 if ((expiry = hwgroup->expiry) != NULL) { 934 add_timer(&hwif->timer);
1222 /* continue */ 935 spin_unlock_irqrestore(&hwif->lock, flags);
1223 if ((wait = expiry(drive)) > 0) { 936 return;
1224 /* reset timer */
1225 hwgroup->timer.expires = jiffies + wait;
1226 hwgroup->req_gen_timer = hwgroup->req_gen;
1227 add_timer(&hwgroup->timer);
1228 spin_unlock_irqrestore(&ide_lock, flags);
1229 return;
1230 }
1231 } 937 }
1232 hwgroup->handler = NULL;
1233 /*
1234 * We need to simulate a real interrupt when invoking
1235 * the handler() function, which means we need to
1236 * globally mask the specific IRQ:
1237 */
1238 spin_unlock(&ide_lock);
1239 hwif = HWIF(drive);
1240 /* disable_irq_nosync ?? */
1241 disable_irq(hwif->irq);
1242 /* local CPU only,
1243 * as if we were handling an interrupt */
1244 local_irq_disable();
1245 if (hwgroup->polling) {
1246 startstop = handler(drive);
1247 } else if (drive_is_ready(drive)) {
1248 if (drive->waiting_for_dma)
1249 hwif->dma_ops->dma_lost_irq(drive);
1250 (void)ide_ack_intr(hwif);
1251 printk(KERN_WARNING "%s: lost interrupt\n", drive->name);
1252 startstop = handler(drive);
1253 } else {
1254 if (drive->waiting_for_dma) {
1255 startstop = ide_dma_timeout_retry(drive, wait);
1256 } else
1257 startstop =
1258 ide_error(drive, "irq timeout",
1259 hwif->tp_ops->read_status(hwif));
1260 }
1261 drive->service_time = jiffies - drive->service_start;
1262 spin_lock_irq(&ide_lock);
1263 enable_irq(hwif->irq);
1264 if (startstop == ide_stopped)
1265 hwgroup->busy = 0;
1266 } 938 }
939 hwif->handler = NULL;
940 /*
941 * We need to simulate a real interrupt when invoking
942 * the handler() function, which means we need to
943 * globally mask the specific IRQ:
944 */
945 spin_unlock(&hwif->lock);
946 /* disable_irq_nosync ?? */
947 disable_irq(hwif->irq);
948 /* local CPU only, as if we were handling an interrupt */
949 local_irq_disable();
950 if (hwif->polling) {
951 startstop = handler(drive);
952 } else if (drive_is_ready(drive)) {
953 if (drive->waiting_for_dma)
954 hwif->dma_ops->dma_lost_irq(drive);
955 (void)ide_ack_intr(hwif);
956 printk(KERN_WARNING "%s: lost interrupt\n",
957 drive->name);
958 startstop = handler(drive);
959 } else {
960 if (drive->waiting_for_dma)
961 startstop = ide_dma_timeout_retry(drive, wait);
962 else
963 startstop = ide_error(drive, "irq timeout",
964 hwif->tp_ops->read_status(hwif));
965 }
966 spin_lock_irq(&hwif->lock);
967 enable_irq(hwif->irq);
968 if (startstop == ide_stopped) {
969 ide_unlock_port(hwif);
970 plug_device = 1;
971 }
972 }
973 spin_unlock_irqrestore(&hwif->lock, flags);
974
975 if (plug_device) {
976 ide_unlock_host(hwif->host);
977 ide_plug_device(drive);
1267 } 978 }
1268 ide_do_request(hwgroup, IDE_NO_IRQ);
1269 spin_unlock_irqrestore(&ide_lock, flags);
1270} 979}
1271 980
1272/** 981/**
1273 * unexpected_intr - handle an unexpected IDE interrupt 982 * unexpected_intr - handle an unexpected IDE interrupt
1274 * @irq: interrupt line 983 * @irq: interrupt line
1275 * @hwgroup: hwgroup being processed 984 * @hwif: port being processed
1276 * 985 *
1277 * There's nothing really useful we can do with an unexpected interrupt, 986 * There's nothing really useful we can do with an unexpected interrupt,
1278 * other than reading the status register (to clear it), and logging it. 987 * other than reading the status register (to clear it), and logging it.
@@ -1296,52 +1005,38 @@ void ide_timer_expiry (unsigned long data)
1296 * before completing the issuance of any new drive command, so we will not 1005 * before completing the issuance of any new drive command, so we will not
1297 * be accidentally invoked as a result of any valid command completion 1006 * be accidentally invoked as a result of any valid command completion
1298 * interrupt. 1007 * interrupt.
1299 *
1300 * Note that we must walk the entire hwgroup here. We know which hwif
1301 * is doing the current command, but we don't know which hwif burped
1302 * mysteriously.
1303 */ 1008 */
1304
1305static void unexpected_intr (int irq, ide_hwgroup_t *hwgroup)
1306{
1307 u8 stat;
1308 ide_hwif_t *hwif = hwgroup->hwif;
1309 1009
1310 /* 1010static void unexpected_intr(int irq, ide_hwif_t *hwif)
1311 * handle the unexpected interrupt 1011{
1312 */ 1012 u8 stat = hwif->tp_ops->read_status(hwif);
1313 do { 1013
1314 if (hwif->irq == irq) { 1014 if (!OK_STAT(stat, ATA_DRDY, BAD_STAT)) {
1315 stat = hwif->tp_ops->read_status(hwif); 1015 /* Try to not flood the console with msgs */
1316 1016 static unsigned long last_msgtime, count;
1317 if (!OK_STAT(stat, ATA_DRDY, BAD_STAT)) { 1017 ++count;
1318 /* Try to not flood the console with msgs */ 1018
1319 static unsigned long last_msgtime, count; 1019 if (time_after(jiffies, last_msgtime + HZ)) {
1320 ++count; 1020 last_msgtime = jiffies;
1321 if (time_after(jiffies, last_msgtime + HZ)) { 1021 printk(KERN_ERR "%s: unexpected interrupt, "
1322 last_msgtime = jiffies; 1022 "status=0x%02x, count=%ld\n",
1323 printk(KERN_ERR "%s%s: unexpected interrupt, " 1023 hwif->name, stat, count);
1324 "status=0x%02x, count=%ld\n",
1325 hwif->name,
1326 (hwif->next==hwgroup->hwif) ? "" : "(?)", stat, count);
1327 }
1328 }
1329 } 1024 }
1330 } while ((hwif = hwif->next) != hwgroup->hwif); 1025 }
1331} 1026}
1332 1027
1333/** 1028/**
1334 * ide_intr - default IDE interrupt handler 1029 * ide_intr - default IDE interrupt handler
1335 * @irq: interrupt number 1030 * @irq: interrupt number
1336 * @dev_id: hwif group 1031 * @dev_id: hwif
1337 * @regs: unused weirdness from the kernel irq layer 1032 * @regs: unused weirdness from the kernel irq layer
1338 * 1033 *
1339 * This is the default IRQ handler for the IDE layer. You should 1034 * This is the default IRQ handler for the IDE layer. You should
1340 * not need to override it. If you do be aware it is subtle in 1035 * not need to override it. If you do be aware it is subtle in
1341 * places 1036 * places
1342 * 1037 *
1343 * hwgroup->hwif is the interface in the group currently performing 1038 * hwif is the interface in the group currently performing
1344 * a command. hwgroup->drive is the drive and hwgroup->handler is 1039 * a command. hwif->cur_dev is the drive and hwif->handler is
1345 * the IRQ handler to call. As we issue a command the handlers 1040 * the IRQ handler to call. As we issue a command the handlers
1346 * step through multiple states, reassigning the handler to the 1041 * step through multiple states, reassigning the handler to the
1347 * next step in the process. Unlike a smart SCSI controller IDE 1042 * next step in the process. Unlike a smart SCSI controller IDE
@@ -1352,27 +1047,32 @@ static void unexpected_intr (int irq, ide_hwgroup_t *hwgroup)
1352 * 1047 *
1353 * The handler eventually returns ide_stopped to indicate the 1048 * The handler eventually returns ide_stopped to indicate the
1354 * request completed. At this point we issue the next request 1049 * request completed. At this point we issue the next request
1355 * on the hwgroup and the process begins again. 1050 * on the port and the process begins again.
1356 */ 1051 */
1357 1052
1358irqreturn_t ide_intr (int irq, void *dev_id) 1053irqreturn_t ide_intr (int irq, void *dev_id)
1359{ 1054{
1360 unsigned long flags; 1055 ide_hwif_t *hwif = (ide_hwif_t *)dev_id;
1361 ide_hwgroup_t *hwgroup = (ide_hwgroup_t *)dev_id; 1056 ide_drive_t *uninitialized_var(drive);
1362 ide_hwif_t *hwif;
1363 ide_drive_t *drive;
1364 ide_handler_t *handler; 1057 ide_handler_t *handler;
1058 unsigned long flags;
1365 ide_startstop_t startstop; 1059 ide_startstop_t startstop;
1060 irqreturn_t irq_ret = IRQ_NONE;
1061 int plug_device = 0;
1366 1062
1367 spin_lock_irqsave(&ide_lock, flags); 1063 if (hwif->host->host_flags & IDE_HFLAG_SERIALIZE) {
1368 hwif = hwgroup->hwif; 1064 if (hwif != hwif->host->cur_port)
1369 1065 goto out_early;
1370 if (!ide_ack_intr(hwif)) {
1371 spin_unlock_irqrestore(&ide_lock, flags);
1372 return IRQ_NONE;
1373 } 1066 }
1374 1067
1375 if ((handler = hwgroup->handler) == NULL || hwgroup->polling) { 1068 spin_lock_irqsave(&hwif->lock, flags);
1069
1070 if (!ide_ack_intr(hwif))
1071 goto out;
1072
1073 handler = hwif->handler;
1074
1075 if (handler == NULL || hwif->polling) {
1376 /* 1076 /*
1377 * Not expecting an interrupt from this drive. 1077 * Not expecting an interrupt from this drive.
1378 * That means this could be: 1078 * That means this could be:
@@ -1396,7 +1096,7 @@ irqreturn_t ide_intr (int irq, void *dev_id)
1396 * Probably not a shared PCI interrupt, 1096 * Probably not a shared PCI interrupt,
1397 * so we can safely try to do something about it: 1097 * so we can safely try to do something about it:
1398 */ 1098 */
1399 unexpected_intr(irq, hwgroup); 1099 unexpected_intr(irq, hwif);
1400#ifdef CONFIG_BLK_DEV_IDEPCI 1100#ifdef CONFIG_BLK_DEV_IDEPCI
1401 } else { 1101 } else {
1402 /* 1102 /*
@@ -1406,21 +1106,12 @@ irqreturn_t ide_intr (int irq, void *dev_id)
1406 (void)hwif->tp_ops->read_status(hwif); 1106 (void)hwif->tp_ops->read_status(hwif);
1407#endif /* CONFIG_BLK_DEV_IDEPCI */ 1107#endif /* CONFIG_BLK_DEV_IDEPCI */
1408 } 1108 }
1409 spin_unlock_irqrestore(&ide_lock, flags); 1109 goto out;
1410 return IRQ_NONE;
1411 }
1412 drive = hwgroup->drive;
1413 if (!drive) {
1414 /*
1415 * This should NEVER happen, and there isn't much
1416 * we could do about it here.
1417 *
1418 * [Note - this can occur if the drive is hot unplugged]
1419 */
1420 spin_unlock_irqrestore(&ide_lock, flags);
1421 return IRQ_HANDLED;
1422 } 1110 }
1423 if (!drive_is_ready(drive)) { 1111
1112 drive = hwif->cur_dev;
1113
1114 if (!drive_is_ready(drive))
1424 /* 1115 /*
1425 * This happens regularly when we share a PCI IRQ with 1116 * This happens regularly when we share a PCI IRQ with
1426 * another device. Unfortunately, it can also happen 1117 * another device. Unfortunately, it can also happen
@@ -1428,17 +1119,12 @@ irqreturn_t ide_intr (int irq, void *dev_id)
1428 * their status register is up to date. Hopefully we have 1119 * their status register is up to date. Hopefully we have
1429 * enough advance overhead that the latter isn't a problem. 1120 * enough advance overhead that the latter isn't a problem.
1430 */ 1121 */
1431 spin_unlock_irqrestore(&ide_lock, flags); 1122 goto out;
1432 return IRQ_NONE; 1123
1433 } 1124 hwif->handler = NULL;
1434 if (!hwgroup->busy) { 1125 hwif->req_gen++;
1435 hwgroup->busy = 1; /* paranoia */ 1126 del_timer(&hwif->timer);
1436 printk(KERN_ERR "%s: ide_intr: hwgroup->busy was 0 ??\n", drive->name); 1127 spin_unlock(&hwif->lock);
1437 }
1438 hwgroup->handler = NULL;
1439 hwgroup->req_gen++;
1440 del_timer(&hwgroup->timer);
1441 spin_unlock(&ide_lock);
1442 1128
1443 if (hwif->port_ops && hwif->port_ops->clear_irq) 1129 if (hwif->port_ops && hwif->port_ops->clear_irq)
1444 hwif->port_ops->clear_irq(drive); 1130 hwif->port_ops->clear_irq(drive);
@@ -1449,7 +1135,7 @@ irqreturn_t ide_intr (int irq, void *dev_id)
1449 /* service this interrupt, may set handler for next interrupt */ 1135 /* service this interrupt, may set handler for next interrupt */
1450 startstop = handler(drive); 1136 startstop = handler(drive);
1451 1137
1452 spin_lock_irq(&ide_lock); 1138 spin_lock_irq(&hwif->lock);
1453 /* 1139 /*
1454 * Note that handler() may have set things up for another 1140 * Note that handler() may have set things up for another
1455 * interrupt to occur soon, but it cannot happen until 1141 * interrupt to occur soon, but it cannot happen until
@@ -1457,18 +1143,21 @@ irqreturn_t ide_intr (int irq, void *dev_id)
1457 * same irq as is currently being serviced here, and Linux 1143 * same irq as is currently being serviced here, and Linux
1458 * won't allow another of the same (on any CPU) until we return. 1144 * won't allow another of the same (on any CPU) until we return.
1459 */ 1145 */
1460 drive->service_time = jiffies - drive->service_start;
1461 if (startstop == ide_stopped) { 1146 if (startstop == ide_stopped) {
1462 if (hwgroup->handler == NULL) { /* paranoia */ 1147 BUG_ON(hwif->handler);
1463 hwgroup->busy = 0; 1148 ide_unlock_port(hwif);
1464 ide_do_request(hwgroup, hwif->irq); 1149 plug_device = 1;
1465 } else {
1466 printk(KERN_ERR "%s: ide_intr: huh? expected NULL handler "
1467 "on exit\n", drive->name);
1468 }
1469 } 1150 }
1470 spin_unlock_irqrestore(&ide_lock, flags); 1151 irq_ret = IRQ_HANDLED;
1471 return IRQ_HANDLED; 1152out:
1153 spin_unlock_irqrestore(&hwif->lock, flags);
1154out_early:
1155 if (plug_device) {
1156 ide_unlock_host(hwif->host);
1157 ide_plug_device(drive);
1158 }
1159
1160 return irq_ret;
1472} 1161}
1473 1162
1474/** 1163/**
@@ -1488,16 +1177,15 @@ irqreturn_t ide_intr (int irq, void *dev_id)
1488 1177
1489void ide_do_drive_cmd(ide_drive_t *drive, struct request *rq) 1178void ide_do_drive_cmd(ide_drive_t *drive, struct request *rq)
1490{ 1179{
1180 struct request_queue *q = drive->queue;
1491 unsigned long flags; 1181 unsigned long flags;
1492 ide_hwgroup_t *hwgroup = HWGROUP(drive);
1493 1182
1494 spin_lock_irqsave(&ide_lock, flags); 1183 drive->hwif->rq = NULL;
1495 hwgroup->rq = NULL;
1496 __elv_add_request(drive->queue, rq, ELEVATOR_INSERT_FRONT, 0);
1497 blk_start_queueing(drive->queue);
1498 spin_unlock_irqrestore(&ide_lock, flags);
1499}
1500 1184
1185 spin_lock_irqsave(q->queue_lock, flags);
1186 __elv_add_request(q, rq, ELEVATOR_INSERT_FRONT, 0);
1187 spin_unlock_irqrestore(q->queue_lock, flags);
1188}
1501EXPORT_SYMBOL(ide_do_drive_cmd); 1189EXPORT_SYMBOL(ide_do_drive_cmd);
1502 1190
1503void ide_pktcmd_tf_load(ide_drive_t *drive, u32 tf_flags, u16 bcount, u8 dma) 1191void ide_pktcmd_tf_load(ide_drive_t *drive, u32 tf_flags, u16 bcount, u8 dma)