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.c282
1 files changed, 85 insertions, 197 deletions
diff --git a/drivers/ide/ide-io.c b/drivers/ide/ide-io.c
index ecacc008fdaf..1c36a8e83d36 100644
--- a/drivers/ide/ide-io.c
+++ b/drivers/ide/ide-io.c
@@ -426,9 +426,6 @@ void ide_map_sg(ide_drive_t *drive, struct request *rq)
426 ide_hwif_t *hwif = drive->hwif; 426 ide_hwif_t *hwif = drive->hwif;
427 struct scatterlist *sg = hwif->sg_table; 427 struct scatterlist *sg = hwif->sg_table;
428 428
429 if (hwif->sg_mapped) /* needed by ide-scsi */
430 return;
431
432 if (rq->cmd_type != REQ_TYPE_ATA_TASKFILE) { 429 if (rq->cmd_type != REQ_TYPE_ATA_TASKFILE) {
433 hwif->sg_nents = blk_rq_map_sg(drive->queue, rq, sg); 430 hwif->sg_nents = blk_rq_map_sg(drive->queue, rq, sg);
434 } else { 431 } else {
@@ -667,85 +664,10 @@ void ide_stall_queue (ide_drive_t *drive, unsigned long timeout)
667 drive->sleep = timeout + jiffies; 664 drive->sleep = timeout + jiffies;
668 drive->dev_flags |= IDE_DFLAG_SLEEPING; 665 drive->dev_flags |= IDE_DFLAG_SLEEPING;
669} 666}
670
671EXPORT_SYMBOL(ide_stall_queue); 667EXPORT_SYMBOL(ide_stall_queue);
672 668
673#define WAKEUP(drive) ((drive)->service_start + 2 * (drive)->service_time)
674
675/**
676 * choose_drive - select a drive to service
677 * @hwgroup: hardware group to select on
678 *
679 * choose_drive() selects the next drive which will be serviced.
680 * This is necessary because the IDE layer can't issue commands
681 * to both drives on the same cable, unlike SCSI.
682 */
683
684static inline ide_drive_t *choose_drive (ide_hwgroup_t *hwgroup)
685{
686 ide_drive_t *drive, *best;
687
688repeat:
689 best = NULL;
690 drive = hwgroup->drive;
691
692 /*
693 * drive is doing pre-flush, ordered write, post-flush sequence. even
694 * though that is 3 requests, it must be seen as a single transaction.
695 * we must not preempt this drive until that is complete
696 */
697 if (blk_queue_flushing(drive->queue)) {
698 /*
699 * small race where queue could get replugged during
700 * the 3-request flush cycle, just yank the plug since
701 * we want it to finish asap
702 */
703 blk_remove_plug(drive->queue);
704 return drive;
705 }
706
707 do {
708 u8 dev_s = !!(drive->dev_flags & IDE_DFLAG_SLEEPING);
709 u8 best_s = (best && !!(best->dev_flags & IDE_DFLAG_SLEEPING));
710
711 if ((dev_s == 0 || time_after_eq(jiffies, drive->sleep)) &&
712 !elv_queue_empty(drive->queue)) {
713 if (best == NULL ||
714 (dev_s && (best_s == 0 || time_before(drive->sleep, best->sleep))) ||
715 (best_s == 0 && time_before(WAKEUP(drive), WAKEUP(best)))) {
716 if (!blk_queue_plugged(drive->queue))
717 best = drive;
718 }
719 }
720 } while ((drive = drive->next) != hwgroup->drive);
721
722 if (best && (best->dev_flags & IDE_DFLAG_NICE1) &&
723 (best->dev_flags & IDE_DFLAG_SLEEPING) == 0 &&
724 best != hwgroup->drive && best->service_time > WAIT_MIN_SLEEP) {
725 long t = (signed long)(WAKEUP(best) - jiffies);
726 if (t >= WAIT_MIN_SLEEP) {
727 /*
728 * We *may* have some time to spare, but first let's see if
729 * someone can potentially benefit from our nice mood today..
730 */
731 drive = best->next;
732 do {
733 if ((drive->dev_flags & IDE_DFLAG_SLEEPING) == 0
734 && time_before(jiffies - best->service_time, WAKEUP(drive))
735 && time_before(WAKEUP(drive), jiffies + t))
736 {
737 ide_stall_queue(best, min_t(long, t, 10 * WAIT_MIN_SLEEP));
738 goto repeat;
739 }
740 } while ((drive = drive->next) != best);
741 }
742 }
743 return best;
744}
745
746/* 669/*
747 * Issue a new request to a drive from hwgroup 670 * Issue a new request to a drive from hwgroup
748 * Caller must have already done spin_lock_irqsave(&hwgroup->lock, ..);
749 * 671 *
750 * A hwgroup is a serialized group of IDE interfaces. Usually there is 672 * A hwgroup is a serialized group of IDE interfaces. Usually there is
751 * exactly one hwif (interface) per hwgroup, but buggy controllers (eg. CMD640) 673 * exactly one hwif (interface) per hwgroup, but buggy controllers (eg. CMD640)
@@ -757,8 +679,7 @@ repeat:
757 * possibly along with many other devices. This is especially common in 679 * possibly along with many other devices. This is especially common in
758 * PCI-based systems with off-board IDE controller cards. 680 * PCI-based systems with off-board IDE controller cards.
759 * 681 *
760 * The IDE driver uses a per-hwgroup spinlock to protect 682 * The IDE driver uses a per-hwgroup lock to protect the hwgroup->busy flag.
761 * access to the request queues, and to protect the hwgroup->busy flag.
762 * 683 *
763 * The first thread into the driver for a particular hwgroup sets the 684 * The first thread into the driver for a particular hwgroup sets the
764 * hwgroup->busy flag to indicate that this hwgroup is now active, 685 * hwgroup->busy flag to indicate that this hwgroup is now active,
@@ -778,69 +699,41 @@ repeat:
778 * the driver. This makes the driver much more friendlier to shared IRQs 699 * the driver. This makes the driver much more friendlier to shared IRQs
779 * than previous designs, while remaining 100% (?) SMP safe and capable. 700 * than previous designs, while remaining 100% (?) SMP safe and capable.
780 */ 701 */
781static void ide_do_request (ide_hwgroup_t *hwgroup, int masked_irq) 702void do_ide_request(struct request_queue *q)
782{ 703{
783 ide_drive_t *drive; 704 ide_drive_t *drive = q->queuedata;
784 ide_hwif_t *hwif; 705 ide_hwif_t *hwif = drive->hwif;
706 ide_hwgroup_t *hwgroup = hwif->hwgroup;
785 struct request *rq; 707 struct request *rq;
786 ide_startstop_t startstop; 708 ide_startstop_t startstop;
787 int loops = 0; 709
788 710 /*
789 /* caller must own hwgroup->lock */ 711 * drive is doing pre-flush, ordered write, post-flush sequence. even
790 BUG_ON(!irqs_disabled()); 712 * though that is 3 requests, it must be seen as a single transaction.
791 713 * we must not preempt this drive until that is complete
792 while (!hwgroup->busy) { 714 */
793 hwgroup->busy = 1; 715 if (blk_queue_flushing(q))
794 /* for atari only */
795 ide_get_lock(ide_intr, hwgroup);
796 drive = choose_drive(hwgroup);
797 if (drive == NULL) {
798 int sleeping = 0;
799 unsigned long sleep = 0; /* shut up, gcc */
800 hwgroup->rq = NULL;
801 drive = hwgroup->drive;
802 do {
803 if ((drive->dev_flags & IDE_DFLAG_SLEEPING) &&
804 (sleeping == 0 ||
805 time_before(drive->sleep, sleep))) {
806 sleeping = 1;
807 sleep = drive->sleep;
808 }
809 } while ((drive = drive->next) != hwgroup->drive);
810 if (sleeping) {
811 /* 716 /*
812 * Take a short snooze, and then wake up this hwgroup again. 717 * small race where queue could get replugged during
813 * This gives other hwgroups on the same a chance to 718 * the 3-request flush cycle, just yank the plug since
814 * play fairly with us, just in case there are big differences 719 * we want it to finish asap
815 * in relative throughputs.. don't want to hog the cpu too much.
816 */ 720 */
817 if (time_before(sleep, jiffies + WAIT_MIN_SLEEP)) 721 blk_remove_plug(q);
818 sleep = jiffies + WAIT_MIN_SLEEP;
819#if 1
820 if (timer_pending(&hwgroup->timer))
821 printk(KERN_CRIT "ide_set_handler: timer already active\n");
822#endif
823 /* so that ide_timer_expiry knows what to do */
824 hwgroup->sleeping = 1;
825 hwgroup->req_gen_timer = hwgroup->req_gen;
826 mod_timer(&hwgroup->timer, sleep);
827 /* we purposely leave hwgroup->busy==1
828 * while sleeping */
829 } else {
830 /* Ugly, but how can we sleep for the lock
831 * otherwise? perhaps from tq_disk?
832 */
833 722
834 /* for atari only */ 723 spin_unlock_irq(q->queue_lock);
835 ide_release_lock(); 724 spin_lock_irq(&hwgroup->lock);
836 hwgroup->busy = 0; 725
837 } 726 if (!ide_lock_hwgroup(hwgroup)) {
727repeat:
728 hwgroup->rq = NULL;
838 729
839 /* no more work for this hwgroup (for now) */ 730 if (drive->dev_flags & IDE_DFLAG_SLEEPING) {
840 return; 731 if (time_before(drive->sleep, jiffies)) {
732 ide_unlock_hwgroup(hwgroup);
733 goto plug_device;
734 }
841 } 735 }
842 again: 736
843 hwif = HWIF(drive);
844 if (hwif != hwgroup->hwif) { 737 if (hwif != hwgroup->hwif) {
845 /* 738 /*
846 * set nIEN for previous hwif, drives in the 739 * set nIEN for previous hwif, drives in the
@@ -852,16 +745,20 @@ static void ide_do_request (ide_hwgroup_t *hwgroup, int masked_irq)
852 hwgroup->hwif = hwif; 745 hwgroup->hwif = hwif;
853 hwgroup->drive = drive; 746 hwgroup->drive = drive;
854 drive->dev_flags &= ~(IDE_DFLAG_SLEEPING | IDE_DFLAG_PARKED); 747 drive->dev_flags &= ~(IDE_DFLAG_SLEEPING | IDE_DFLAG_PARKED);
855 drive->service_start = jiffies;
856 748
749 spin_unlock_irq(&hwgroup->lock);
750 spin_lock_irq(q->queue_lock);
857 /* 751 /*
858 * we know that the queue isn't empty, but this can happen 752 * we know that the queue isn't empty, but this can happen
859 * if the q->prep_rq_fn() decides to kill a request 753 * if the q->prep_rq_fn() decides to kill a request
860 */ 754 */
861 rq = elv_next_request(drive->queue); 755 rq = elv_next_request(drive->queue);
756 spin_unlock_irq(q->queue_lock);
757 spin_lock_irq(&hwgroup->lock);
758
862 if (!rq) { 759 if (!rq) {
863 hwgroup->busy = 0; 760 ide_unlock_hwgroup(hwgroup);
864 break; 761 goto out;
865 } 762 }
866 763
867 /* 764 /*
@@ -876,53 +773,36 @@ static void ide_do_request (ide_hwgroup_t *hwgroup, int masked_irq)
876 * though. I hope that doesn't happen too much, hopefully not 773 * though. I hope that doesn't happen too much, hopefully not
877 * unless the subdriver triggers such a thing in its own PM 774 * unless the subdriver triggers such a thing in its own PM
878 * state machine. 775 * state machine.
879 *
880 * We count how many times we loop here to make sure we service
881 * all drives in the hwgroup without looping for ever
882 */ 776 */
883 if ((drive->dev_flags & IDE_DFLAG_BLOCKED) && 777 if ((drive->dev_flags & IDE_DFLAG_BLOCKED) &&
884 blk_pm_request(rq) == 0 && 778 blk_pm_request(rq) == 0 &&
885 (rq->cmd_flags & REQ_PREEMPT) == 0) { 779 (rq->cmd_flags & REQ_PREEMPT) == 0) {
886 drive = drive->next ? drive->next : hwgroup->drive; 780 /* there should be no pending command at this point */
887 if (loops++ < 4 && !blk_queue_plugged(drive->queue)) 781 ide_unlock_hwgroup(hwgroup);
888 goto again; 782 goto plug_device;
889 /* We clear busy, there should be no pending ATA command at this point. */
890 hwgroup->busy = 0;
891 break;
892 } 783 }
893 784
894 hwgroup->rq = rq; 785 hwgroup->rq = rq;
895 786
896 /* 787 spin_unlock_irq(&hwgroup->lock);
897 * Some systems have trouble with IDE IRQs arriving while
898 * the driver is still setting things up. So, here we disable
899 * the IRQ used by this interface while the request is being started.
900 * This may look bad at first, but pretty much the same thing
901 * happens anyway when any interrupt comes in, IDE or otherwise
902 * -- the kernel masks the IRQ while it is being handled.
903 */
904 if (masked_irq != IDE_NO_IRQ && hwif->irq != masked_irq)
905 disable_irq_nosync(hwif->irq);
906 spin_unlock(&hwgroup->lock);
907 local_irq_enable_in_hardirq();
908 /* allow other IRQs while we start this request */
909 startstop = start_request(drive, rq); 788 startstop = start_request(drive, rq);
910 spin_lock_irq(&hwgroup->lock); 789 spin_lock_irq(&hwgroup->lock);
911 if (masked_irq != IDE_NO_IRQ && hwif->irq != masked_irq) 790
912 enable_irq(hwif->irq);
913 if (startstop == ide_stopped) 791 if (startstop == ide_stopped)
914 hwgroup->busy = 0; 792 goto repeat;
915 } 793 } else
916} 794 goto plug_device;
795out:
796 spin_unlock_irq(&hwgroup->lock);
797 spin_lock_irq(q->queue_lock);
798 return;
917 799
918/* 800plug_device:
919 * Passes the stuff to ide_do_request 801 spin_unlock_irq(&hwgroup->lock);
920 */ 802 spin_lock_irq(q->queue_lock);
921void do_ide_request(struct request_queue *q)
922{
923 ide_drive_t *drive = q->queuedata;
924 803
925 ide_do_request(HWGROUP(drive), IDE_NO_IRQ); 804 if (!elv_queue_empty(q))
805 blk_plug_device(q);
926} 806}
927 807
928/* 808/*
@@ -983,6 +863,17 @@ out:
983 return ret; 863 return ret;
984} 864}
985 865
866static void ide_plug_device(ide_drive_t *drive)
867{
868 struct request_queue *q = drive->queue;
869 unsigned long flags;
870
871 spin_lock_irqsave(q->queue_lock, flags);
872 if (!elv_queue_empty(q))
873 blk_plug_device(q);
874 spin_unlock_irqrestore(q->queue_lock, flags);
875}
876
986/** 877/**
987 * ide_timer_expiry - handle lack of an IDE interrupt 878 * ide_timer_expiry - handle lack of an IDE interrupt
988 * @data: timer callback magic (hwgroup) 879 * @data: timer callback magic (hwgroup)
@@ -1000,10 +891,12 @@ out:
1000void ide_timer_expiry (unsigned long data) 891void ide_timer_expiry (unsigned long data)
1001{ 892{
1002 ide_hwgroup_t *hwgroup = (ide_hwgroup_t *) data; 893 ide_hwgroup_t *hwgroup = (ide_hwgroup_t *) data;
894 ide_drive_t *uninitialized_var(drive);
1003 ide_handler_t *handler; 895 ide_handler_t *handler;
1004 ide_expiry_t *expiry; 896 ide_expiry_t *expiry;
1005 unsigned long flags; 897 unsigned long flags;
1006 unsigned long wait = -1; 898 unsigned long wait = -1;
899 int plug_device = 0;
1007 900
1008 spin_lock_irqsave(&hwgroup->lock, flags); 901 spin_lock_irqsave(&hwgroup->lock, flags);
1009 902
@@ -1015,22 +908,15 @@ void ide_timer_expiry (unsigned long data)
1015 * or we were "sleeping" to give other devices a chance. 908 * or we were "sleeping" to give other devices a chance.
1016 * Either way, we don't really want to complain about anything. 909 * Either way, we don't really want to complain about anything.
1017 */ 910 */
1018 if (hwgroup->sleeping) {
1019 hwgroup->sleeping = 0;
1020 hwgroup->busy = 0;
1021 }
1022 } else { 911 } else {
1023 ide_drive_t *drive = hwgroup->drive; 912 drive = hwgroup->drive;
1024 if (!drive) { 913 if (!drive) {
1025 printk(KERN_ERR "ide_timer_expiry: hwgroup->drive was NULL\n"); 914 printk(KERN_ERR "ide_timer_expiry: hwgroup->drive was NULL\n");
1026 hwgroup->handler = NULL; 915 hwgroup->handler = NULL;
1027 } else { 916 } else {
1028 ide_hwif_t *hwif; 917 ide_hwif_t *hwif;
1029 ide_startstop_t startstop = ide_stopped; 918 ide_startstop_t startstop = ide_stopped;
1030 if (!hwgroup->busy) { 919
1031 hwgroup->busy = 1; /* paranoia */
1032 printk(KERN_ERR "%s: ide_timer_expiry: hwgroup->busy was 0 ??\n", drive->name);
1033 }
1034 if ((expiry = hwgroup->expiry) != NULL) { 920 if ((expiry = hwgroup->expiry) != NULL) {
1035 /* continue */ 921 /* continue */
1036 if ((wait = expiry(drive)) > 0) { 922 if ((wait = expiry(drive)) > 0) {
@@ -1071,15 +957,18 @@ void ide_timer_expiry (unsigned long data)
1071 ide_error(drive, "irq timeout", 957 ide_error(drive, "irq timeout",
1072 hwif->tp_ops->read_status(hwif)); 958 hwif->tp_ops->read_status(hwif));
1073 } 959 }
1074 drive->service_time = jiffies - drive->service_start;
1075 spin_lock_irq(&hwgroup->lock); 960 spin_lock_irq(&hwgroup->lock);
1076 enable_irq(hwif->irq); 961 enable_irq(hwif->irq);
1077 if (startstop == ide_stopped) 962 if (startstop == ide_stopped) {
1078 hwgroup->busy = 0; 963 ide_unlock_hwgroup(hwgroup);
964 plug_device = 1;
965 }
1079 } 966 }
1080 } 967 }
1081 ide_do_request(hwgroup, IDE_NO_IRQ);
1082 spin_unlock_irqrestore(&hwgroup->lock, flags); 968 spin_unlock_irqrestore(&hwgroup->lock, flags);
969
970 if (plug_device)
971 ide_plug_device(drive);
1083} 972}
1084 973
1085/** 974/**
@@ -1173,10 +1062,11 @@ irqreturn_t ide_intr (int irq, void *dev_id)
1173 unsigned long flags; 1062 unsigned long flags;
1174 ide_hwgroup_t *hwgroup = (ide_hwgroup_t *)dev_id; 1063 ide_hwgroup_t *hwgroup = (ide_hwgroup_t *)dev_id;
1175 ide_hwif_t *hwif = hwgroup->hwif; 1064 ide_hwif_t *hwif = hwgroup->hwif;
1176 ide_drive_t *drive; 1065 ide_drive_t *uninitialized_var(drive);
1177 ide_handler_t *handler; 1066 ide_handler_t *handler;
1178 ide_startstop_t startstop; 1067 ide_startstop_t startstop;
1179 irqreturn_t irq_ret = IRQ_NONE; 1068 irqreturn_t irq_ret = IRQ_NONE;
1069 int plug_device = 0;
1180 1070
1181 spin_lock_irqsave(&hwgroup->lock, flags); 1071 spin_lock_irqsave(&hwgroup->lock, flags);
1182 1072
@@ -1241,10 +1131,6 @@ irqreturn_t ide_intr (int irq, void *dev_id)
1241 */ 1131 */
1242 goto out; 1132 goto out;
1243 1133
1244 if (!hwgroup->busy) {
1245 hwgroup->busy = 1; /* paranoia */
1246 printk(KERN_ERR "%s: ide_intr: hwgroup->busy was 0 ??\n", drive->name);
1247 }
1248 hwgroup->handler = NULL; 1134 hwgroup->handler = NULL;
1249 hwgroup->req_gen++; 1135 hwgroup->req_gen++;
1250 del_timer(&hwgroup->timer); 1136 del_timer(&hwgroup->timer);
@@ -1267,20 +1153,22 @@ irqreturn_t ide_intr (int irq, void *dev_id)
1267 * same irq as is currently being serviced here, and Linux 1153 * same irq as is currently being serviced here, and Linux
1268 * won't allow another of the same (on any CPU) until we return. 1154 * won't allow another of the same (on any CPU) until we return.
1269 */ 1155 */
1270 drive->service_time = jiffies - drive->service_start;
1271 if (startstop == ide_stopped) { 1156 if (startstop == ide_stopped) {
1272 if (hwgroup->handler == NULL) { /* paranoia */ 1157 if (hwgroup->handler == NULL) { /* paranoia */
1273 hwgroup->busy = 0; 1158 ide_unlock_hwgroup(hwgroup);
1274 ide_do_request(hwgroup, hwif->irq); 1159 plug_device = 1;
1275 } else { 1160 } else
1276 printk(KERN_ERR "%s: ide_intr: huh? expected NULL handler " 1161 printk(KERN_ERR "%s: %s: huh? expected NULL handler "
1277 "on exit\n", drive->name); 1162 "on exit\n", __func__, drive->name);
1278 }
1279 } 1163 }
1280out_handled: 1164out_handled:
1281 irq_ret = IRQ_HANDLED; 1165 irq_ret = IRQ_HANDLED;
1282out: 1166out:
1283 spin_unlock_irqrestore(&hwgroup->lock, flags); 1167 spin_unlock_irqrestore(&hwgroup->lock, flags);
1168
1169 if (plug_device)
1170 ide_plug_device(drive);
1171
1284 return irq_ret; 1172 return irq_ret;
1285} 1173}
1286 1174