patch-2_6_7-vs1_9_1_12
[linux-2.6.git] / drivers / ide / ide-iops.c
1 /*
2  * linux/drivers/ide/ide-iops.c Version 0.37    Mar 05, 2003
3  *
4  *  Copyright (C) 2000-2002     Andre Hedrick <andre@linux-ide.org>
5  *  Copyright (C) 2003          Red Hat <alan@redhat.com>
6  *
7  */
8
9 #include <linux/config.h>
10 #include <linux/module.h>
11 #include <linux/types.h>
12 #include <linux/string.h>
13 #include <linux/kernel.h>
14 #include <linux/timer.h>
15 #include <linux/mm.h>
16 #include <linux/interrupt.h>
17 #include <linux/major.h>
18 #include <linux/errno.h>
19 #include <linux/genhd.h>
20 #include <linux/blkpg.h>
21 #include <linux/slab.h>
22 #include <linux/pci.h>
23 #include <linux/delay.h>
24 #include <linux/hdreg.h>
25 #include <linux/ide.h>
26
27 #include <asm/byteorder.h>
28 #include <asm/irq.h>
29 #include <asm/uaccess.h>
30 #include <asm/io.h>
31 #include <asm/bitops.h>
32
33 /*
34  *      Conventional PIO operations for ATA devices
35  */
36
37 static u8 ide_inb (unsigned long port)
38 {
39         return (u8) inb(port);
40 }
41
42 static u16 ide_inw (unsigned long port)
43 {
44         return (u16) inw(port);
45 }
46
47 static void ide_insw (unsigned long port, void *addr, u32 count)
48 {
49         insw(port, addr, count);
50 }
51
52 static u32 ide_inl (unsigned long port)
53 {
54         return (u32) inl(port);
55 }
56
57 static void ide_insl (unsigned long port, void *addr, u32 count)
58 {
59         insl(port, addr, count);
60 }
61
62 static void ide_outb (u8 val, unsigned long port)
63 {
64         outb(val, port);
65 }
66
67 static void ide_outbsync (ide_drive_t *drive, u8 addr, unsigned long port)
68 {
69         outb(addr, port);
70 }
71
72 static void ide_outw (u16 val, unsigned long port)
73 {
74         outw(val, port);
75 }
76
77 static void ide_outsw (unsigned long port, void *addr, u32 count)
78 {
79         outsw(port, addr, count);
80 }
81
82 static void ide_outl (u32 val, unsigned long port)
83 {
84         outl(val, port);
85 }
86
87 static void ide_outsl (unsigned long port, void *addr, u32 count)
88 {
89         outsl(port, addr, count);
90 }
91
92 void default_hwif_iops (ide_hwif_t *hwif)
93 {
94         hwif->OUTB      = ide_outb;
95         hwif->OUTBSYNC  = ide_outbsync;
96         hwif->OUTW      = ide_outw;
97         hwif->OUTL      = ide_outl;
98         hwif->OUTSW     = ide_outsw;
99         hwif->OUTSL     = ide_outsl;
100         hwif->INB       = ide_inb;
101         hwif->INW       = ide_inw;
102         hwif->INL       = ide_inl;
103         hwif->INSW      = ide_insw;
104         hwif->INSL      = ide_insl;
105 }
106
107 EXPORT_SYMBOL(default_hwif_iops);
108
109 /*
110  *      MMIO operations, typically used for SATA controllers
111  */
112
113 static u8 ide_mm_inb (unsigned long port)
114 {
115         return (u8) readb(port);
116 }
117
118 static u16 ide_mm_inw (unsigned long port)
119 {
120         return (u16) readw(port);
121 }
122
123 static void ide_mm_insw (unsigned long port, void *addr, u32 count)
124 {
125         __ide_mm_insw(port, addr, count);
126 }
127
128 static u32 ide_mm_inl (unsigned long port)
129 {
130         return (u32) readl(port);
131 }
132
133 static void ide_mm_insl (unsigned long port, void *addr, u32 count)
134 {
135         __ide_mm_insl(port, addr, count);
136 }
137
138 static void ide_mm_outb (u8 value, unsigned long port)
139 {
140         writeb(value, port);
141 }
142
143 static void ide_mm_outbsync (ide_drive_t *drive, u8 value, unsigned long port)
144 {
145         writeb(value, port);    
146 }
147
148 static void ide_mm_outw (u16 value, unsigned long port)
149 {
150         writew(value, port);
151 }
152
153 static void ide_mm_outsw (unsigned long port, void *addr, u32 count)
154 {
155         __ide_mm_outsw(port, addr, count);
156 }
157
158 static void ide_mm_outl (u32 value, unsigned long port)
159 {
160         writel(value, port);
161 }
162
163 static void ide_mm_outsl (unsigned long port, void *addr, u32 count)
164 {
165         __ide_mm_outsl(port, addr, count);
166 }
167
168 void default_hwif_mmiops (ide_hwif_t *hwif)
169 {
170         hwif->OUTB      = ide_mm_outb;
171         /* Most systems will need to override OUTBSYNC, alas however
172            this one is controller specific! */
173         hwif->OUTBSYNC  = ide_mm_outbsync;
174         hwif->OUTW      = ide_mm_outw;
175         hwif->OUTL      = ide_mm_outl;
176         hwif->OUTSW     = ide_mm_outsw;
177         hwif->OUTSL     = ide_mm_outsl;
178         hwif->INB       = ide_mm_inb;
179         hwif->INW       = ide_mm_inw;
180         hwif->INL       = ide_mm_inl;
181         hwif->INSW      = ide_mm_insw;
182         hwif->INSL      = ide_mm_insl;
183 }
184
185 EXPORT_SYMBOL(default_hwif_mmiops);
186
187 void default_hwif_transport (ide_hwif_t *hwif)
188 {
189         hwif->ata_input_data            = ata_input_data;
190         hwif->ata_output_data           = ata_output_data;
191         hwif->atapi_input_bytes         = atapi_input_bytes;
192         hwif->atapi_output_bytes        = atapi_output_bytes;
193 }
194
195 EXPORT_SYMBOL(default_hwif_transport);
196
197 u32 ide_read_24 (ide_drive_t *drive)
198 {
199         u8 hcyl = HWIF(drive)->INB(IDE_HCYL_REG);
200         u8 lcyl = HWIF(drive)->INB(IDE_LCYL_REG);
201         u8 sect = HWIF(drive)->INB(IDE_SECTOR_REG);
202         return (hcyl<<16)|(lcyl<<8)|sect;
203 }
204
205 EXPORT_SYMBOL(ide_read_24);
206
207 void SELECT_DRIVE (ide_drive_t *drive)
208 {
209         if (HWIF(drive)->selectproc)
210                 HWIF(drive)->selectproc(drive);
211         HWIF(drive)->OUTB(drive->select.all, IDE_SELECT_REG);
212 }
213
214 EXPORT_SYMBOL(SELECT_DRIVE);
215
216 void SELECT_INTERRUPT (ide_drive_t *drive)
217 {
218         if (HWIF(drive)->intrproc)
219                 HWIF(drive)->intrproc(drive);
220         else
221                 HWIF(drive)->OUTB(drive->ctl|2, IDE_CONTROL_REG);
222 }
223
224 EXPORT_SYMBOL(SELECT_INTERRUPT);
225
226 void SELECT_MASK (ide_drive_t *drive, int mask)
227 {
228         if (HWIF(drive)->maskproc)
229                 HWIF(drive)->maskproc(drive, mask);
230 }
231
232 EXPORT_SYMBOL(SELECT_MASK);
233
234 void QUIRK_LIST (ide_drive_t *drive)
235 {
236         if (HWIF(drive)->quirkproc)
237                 drive->quirk_list = HWIF(drive)->quirkproc(drive);
238 }
239
240 EXPORT_SYMBOL(QUIRK_LIST);
241
242 /*
243  * Some localbus EIDE interfaces require a special access sequence
244  * when using 32-bit I/O instructions to transfer data.  We call this
245  * the "vlb_sync" sequence, which consists of three successive reads
246  * of the sector count register location, with interrupts disabled
247  * to ensure that the reads all happen together.
248  */
249 void ata_vlb_sync (ide_drive_t *drive, unsigned long port)
250 {
251         (void) HWIF(drive)->INB(port);
252         (void) HWIF(drive)->INB(port);
253         (void) HWIF(drive)->INB(port);
254 }
255
256 EXPORT_SYMBOL(ata_vlb_sync);
257
258 /*
259  * This is used for most PIO data transfers *from* the IDE interface
260  */
261 void ata_input_data (ide_drive_t *drive, void *buffer, u32 wcount)
262 {
263         ide_hwif_t *hwif        = HWIF(drive);
264         u8 io_32bit             = drive->io_32bit;
265
266         if (io_32bit) {
267                 if (io_32bit & 2) {
268                         unsigned long flags;
269                         local_irq_save(flags);
270                         ata_vlb_sync(drive, IDE_NSECTOR_REG);
271                         hwif->INSL(IDE_DATA_REG, buffer, wcount);
272                         local_irq_restore(flags);
273                 } else
274                         hwif->INSL(IDE_DATA_REG, buffer, wcount);
275         } else {
276                 hwif->INSW(IDE_DATA_REG, buffer, wcount<<1);
277         }
278 }
279
280 EXPORT_SYMBOL(ata_input_data);
281
282 /*
283  * This is used for most PIO data transfers *to* the IDE interface
284  */
285 void ata_output_data (ide_drive_t *drive, void *buffer, u32 wcount)
286 {
287         ide_hwif_t *hwif        = HWIF(drive);
288         u8 io_32bit             = drive->io_32bit;
289
290         if (io_32bit) {
291                 if (io_32bit & 2) {
292                         unsigned long flags;
293                         local_irq_save(flags);
294                         ata_vlb_sync(drive, IDE_NSECTOR_REG);
295                         hwif->OUTSL(IDE_DATA_REG, buffer, wcount);
296                         local_irq_restore(flags);
297                 } else
298                         hwif->OUTSL(IDE_DATA_REG, buffer, wcount);
299         } else {
300                 hwif->OUTSW(IDE_DATA_REG, buffer, wcount<<1);
301         }
302 }
303
304 EXPORT_SYMBOL(ata_output_data);
305
306 /*
307  * The following routines are mainly used by the ATAPI drivers.
308  *
309  * These routines will round up any request for an odd number of bytes,
310  * so if an odd bytecount is specified, be sure that there's at least one
311  * extra byte allocated for the buffer.
312  */
313
314 void atapi_input_bytes (ide_drive_t *drive, void *buffer, u32 bytecount)
315 {
316         ide_hwif_t *hwif = HWIF(drive);
317
318         ++bytecount;
319 #if defined(CONFIG_ATARI) || defined(CONFIG_Q40)
320         if (MACH_IS_ATARI || MACH_IS_Q40) {
321                 /* Atari has a byte-swapped IDE interface */
322                 insw_swapw(IDE_DATA_REG, buffer, bytecount / 2);
323                 return;
324         }
325 #endif /* CONFIG_ATARI || CONFIG_Q40 */
326         hwif->ata_input_data(drive, buffer, bytecount / 4);
327         if ((bytecount & 0x03) >= 2)
328                 hwif->INSW(IDE_DATA_REG, ((u8 *)buffer)+(bytecount & ~0x03), 1);
329 }
330
331 EXPORT_SYMBOL(atapi_input_bytes);
332
333 void atapi_output_bytes (ide_drive_t *drive, void *buffer, u32 bytecount)
334 {
335         ide_hwif_t *hwif = HWIF(drive);
336
337         ++bytecount;
338 #if defined(CONFIG_ATARI) || defined(CONFIG_Q40)
339         if (MACH_IS_ATARI || MACH_IS_Q40) {
340                 /* Atari has a byte-swapped IDE interface */
341                 outsw_swapw(IDE_DATA_REG, buffer, bytecount / 2);
342                 return;
343         }
344 #endif /* CONFIG_ATARI || CONFIG_Q40 */
345         hwif->ata_output_data(drive, buffer, bytecount / 4);
346         if ((bytecount & 0x03) >= 2)
347                 hwif->OUTSW(IDE_DATA_REG, ((u8*)buffer)+(bytecount & ~0x03), 1);
348 }
349
350 EXPORT_SYMBOL(atapi_output_bytes);
351
352 /*
353  * Beginning of Taskfile OPCODE Library and feature sets.
354  */
355 void ide_fix_driveid (struct hd_driveid *id)
356 {
357 #ifndef __LITTLE_ENDIAN
358 # ifdef __BIG_ENDIAN
359         int i;
360         u16 *stringcast;
361
362         id->config         = __le16_to_cpu(id->config);
363         id->cyls           = __le16_to_cpu(id->cyls);
364         id->reserved2      = __le16_to_cpu(id->reserved2);
365         id->heads          = __le16_to_cpu(id->heads);
366         id->track_bytes    = __le16_to_cpu(id->track_bytes);
367         id->sector_bytes   = __le16_to_cpu(id->sector_bytes);
368         id->sectors        = __le16_to_cpu(id->sectors);
369         id->vendor0        = __le16_to_cpu(id->vendor0);
370         id->vendor1        = __le16_to_cpu(id->vendor1);
371         id->vendor2        = __le16_to_cpu(id->vendor2);
372         stringcast = (u16 *)&id->serial_no[0];
373         for (i = 0; i < (20/2); i++)
374                 stringcast[i] = __le16_to_cpu(stringcast[i]);
375         id->buf_type       = __le16_to_cpu(id->buf_type);
376         id->buf_size       = __le16_to_cpu(id->buf_size);
377         id->ecc_bytes      = __le16_to_cpu(id->ecc_bytes);
378         stringcast = (u16 *)&id->fw_rev[0];
379         for (i = 0; i < (8/2); i++)
380                 stringcast[i] = __le16_to_cpu(stringcast[i]);
381         stringcast = (u16 *)&id->model[0];
382         for (i = 0; i < (40/2); i++)
383                 stringcast[i] = __le16_to_cpu(stringcast[i]);
384         id->dword_io       = __le16_to_cpu(id->dword_io);
385         id->reserved50     = __le16_to_cpu(id->reserved50);
386         id->field_valid    = __le16_to_cpu(id->field_valid);
387         id->cur_cyls       = __le16_to_cpu(id->cur_cyls);
388         id->cur_heads      = __le16_to_cpu(id->cur_heads);
389         id->cur_sectors    = __le16_to_cpu(id->cur_sectors);
390         id->cur_capacity0  = __le16_to_cpu(id->cur_capacity0);
391         id->cur_capacity1  = __le16_to_cpu(id->cur_capacity1);
392         id->lba_capacity   = __le32_to_cpu(id->lba_capacity);
393         id->dma_1word      = __le16_to_cpu(id->dma_1word);
394         id->dma_mword      = __le16_to_cpu(id->dma_mword);
395         id->eide_pio_modes = __le16_to_cpu(id->eide_pio_modes);
396         id->eide_dma_min   = __le16_to_cpu(id->eide_dma_min);
397         id->eide_dma_time  = __le16_to_cpu(id->eide_dma_time);
398         id->eide_pio       = __le16_to_cpu(id->eide_pio);
399         id->eide_pio_iordy = __le16_to_cpu(id->eide_pio_iordy);
400         for (i = 0; i < 2; ++i)
401                 id->words69_70[i] = __le16_to_cpu(id->words69_70[i]);
402         for (i = 0; i < 4; ++i)
403                 id->words71_74[i] = __le16_to_cpu(id->words71_74[i]);
404         id->queue_depth    = __le16_to_cpu(id->queue_depth);
405         for (i = 0; i < 4; ++i)
406                 id->words76_79[i] = __le16_to_cpu(id->words76_79[i]);
407         id->major_rev_num  = __le16_to_cpu(id->major_rev_num);
408         id->minor_rev_num  = __le16_to_cpu(id->minor_rev_num);
409         id->command_set_1  = __le16_to_cpu(id->command_set_1);
410         id->command_set_2  = __le16_to_cpu(id->command_set_2);
411         id->cfsse          = __le16_to_cpu(id->cfsse);
412         id->cfs_enable_1   = __le16_to_cpu(id->cfs_enable_1);
413         id->cfs_enable_2   = __le16_to_cpu(id->cfs_enable_2);
414         id->csf_default    = __le16_to_cpu(id->csf_default);
415         id->dma_ultra      = __le16_to_cpu(id->dma_ultra);
416         id->trseuc         = __le16_to_cpu(id->trseuc);
417         id->trsEuc         = __le16_to_cpu(id->trsEuc);
418         id->CurAPMvalues   = __le16_to_cpu(id->CurAPMvalues);
419         id->mprc           = __le16_to_cpu(id->mprc);
420         id->hw_config      = __le16_to_cpu(id->hw_config);
421         id->acoustic       = __le16_to_cpu(id->acoustic);
422         id->msrqs          = __le16_to_cpu(id->msrqs);
423         id->sxfert         = __le16_to_cpu(id->sxfert);
424         id->sal            = __le16_to_cpu(id->sal);
425         id->spg            = __le32_to_cpu(id->spg);
426         id->lba_capacity_2 = __le64_to_cpu(id->lba_capacity_2);
427         for (i = 0; i < 22; i++)
428                 id->words104_125[i]   = __le16_to_cpu(id->words104_125[i]);
429         id->last_lun       = __le16_to_cpu(id->last_lun);
430         id->word127        = __le16_to_cpu(id->word127);
431         id->dlf            = __le16_to_cpu(id->dlf);
432         id->csfo           = __le16_to_cpu(id->csfo);
433         for (i = 0; i < 26; i++)
434                 id->words130_155[i] = __le16_to_cpu(id->words130_155[i]);
435         id->word156        = __le16_to_cpu(id->word156);
436         for (i = 0; i < 3; i++)
437                 id->words157_159[i] = __le16_to_cpu(id->words157_159[i]);
438         id->cfa_power      = __le16_to_cpu(id->cfa_power);
439         for (i = 0; i < 14; i++)
440                 id->words161_175[i] = __le16_to_cpu(id->words161_175[i]);
441         for (i = 0; i < 31; i++)
442                 id->words176_205[i] = __le16_to_cpu(id->words176_205[i]);
443         for (i = 0; i < 48; i++)
444                 id->words206_254[i] = __le16_to_cpu(id->words206_254[i]);
445         id->integrity_word  = __le16_to_cpu(id->integrity_word);
446 # else
447 #  error "Please fix <asm/byteorder.h>"
448 # endif
449 #endif
450 }
451
452 EXPORT_SYMBOL(ide_fix_driveid);
453
454 void ide_fixstring (u8 *s, const int bytecount, const int byteswap)
455 {
456         u8 *p = s, *end = &s[bytecount & ~1]; /* bytecount must be even */
457
458         if (byteswap) {
459                 /* convert from big-endian to host byte order */
460                 for (p = end ; p != s;) {
461                         unsigned short *pp = (unsigned short *) (p -= 2);
462                         *pp = ntohs(*pp);
463                 }
464         }
465         /* strip leading blanks */
466         while (s != end && *s == ' ')
467                 ++s;
468         /* compress internal blanks and strip trailing blanks */
469         while (s != end && *s) {
470                 if (*s++ != ' ' || (s != end && *s && *s != ' '))
471                         *p++ = *(s-1);
472         }
473         /* wipe out trailing garbage */
474         while (p != end)
475                 *p++ = '\0';
476 }
477
478 EXPORT_SYMBOL(ide_fixstring);
479
480 /*
481  * Needed for PCI irq sharing
482  */
483 int drive_is_ready (ide_drive_t *drive)
484 {
485         ide_hwif_t *hwif        = HWIF(drive);
486         u8 stat                 = 0;
487
488         if (drive->waiting_for_dma)
489                 return hwif->ide_dma_test_irq(drive);
490
491 #if 0
492         /* need to guarantee 400ns since last command was issued */
493         udelay(1);
494 #endif
495
496 #ifdef CONFIG_IDEPCI_SHARE_IRQ
497         /*
498          * We do a passive status test under shared PCI interrupts on
499          * cards that truly share the ATA side interrupt, but may also share
500          * an interrupt with another pci card/device.  We make no assumptions
501          * about possible isa-pnp and pci-pnp issues yet.
502          */
503         if (IDE_CONTROL_REG)
504                 stat = hwif->INB(IDE_ALTSTATUS_REG);
505         else
506 #endif /* CONFIG_IDEPCI_SHARE_IRQ */
507                 /* Note: this may clear a pending IRQ!! */
508                 stat = hwif->INB(IDE_STATUS_REG);
509
510         if (stat & BUSY_STAT)
511                 /* drive busy:  definitely not interrupting */
512                 return 0;
513
514         /* drive ready: *might* be interrupting */
515         return 1;
516 }
517
518 EXPORT_SYMBOL(drive_is_ready);
519
520 /*
521  * Global for All, and taken from ide-pmac.c. Can be called
522  * with spinlock held & IRQs disabled, so don't schedule !
523  */
524 int wait_for_ready (ide_drive_t *drive, int timeout)
525 {
526         ide_hwif_t *hwif        = HWIF(drive);
527         u8 stat                 = 0;
528
529         while(--timeout) {
530                 stat = hwif->INB(IDE_STATUS_REG);
531                 if (!(stat & BUSY_STAT)) {
532                         if (drive->ready_stat == 0)
533                                 break;
534                         else if ((stat & drive->ready_stat)||(stat & ERR_STAT))
535                                 break;
536                 }
537                 mdelay(1);
538         }
539         if ((stat & ERR_STAT) || timeout <= 0) {
540                 if (stat & ERR_STAT) {
541                         printk(KERN_ERR "%s: wait_for_ready, "
542                                 "error status: %x\n", drive->name, stat);
543                 }
544                 return 1;
545         }
546         return 0;
547 }
548
549 EXPORT_SYMBOL(wait_for_ready);
550
551 /*
552  * This routine busy-waits for the drive status to be not "busy".
553  * It then checks the status for all of the "good" bits and none
554  * of the "bad" bits, and if all is okay it returns 0.  All other
555  * cases return 1 after invoking ide_error() -- caller should just return.
556  *
557  * This routine should get fixed to not hog the cpu during extra long waits..
558  * That could be done by busy-waiting for the first jiffy or two, and then
559  * setting a timer to wake up at half second intervals thereafter,
560  * until timeout is achieved, before timing out.
561  */
562 int ide_wait_stat (ide_startstop_t *startstop, ide_drive_t *drive, u8 good, u8 bad, unsigned long timeout)
563 {
564         ide_hwif_t *hwif = HWIF(drive);
565         u8 stat;
566         int i;
567         unsigned long flags;
568  
569         /* bail early if we've exceeded max_failures */
570         if (drive->max_failures && (drive->failures > drive->max_failures)) {
571                 *startstop = ide_stopped;
572                 return 1;
573         }
574
575         udelay(1);      /* spec allows drive 400ns to assert "BUSY" */
576         if ((stat = hwif->INB(IDE_STATUS_REG)) & BUSY_STAT) {
577                 local_irq_set(flags);
578                 timeout += jiffies;
579                 while ((stat = hwif->INB(IDE_STATUS_REG)) & BUSY_STAT) {
580                         if (time_after(jiffies, timeout)) {
581                                 /*
582                                  * One last read after the timeout in case
583                                  * heavy interrupt load made us not make any
584                                  * progress during the timeout..
585                                  */
586                                 stat = hwif->INB(IDE_STATUS_REG);
587                                 if (!(stat & BUSY_STAT))
588                                         break;
589
590                                 local_irq_restore(flags);
591                                 *startstop = DRIVER(drive)->error(drive, "status timeout", stat);
592                                 return 1;
593                         }
594                 }
595                 local_irq_restore(flags);
596         }
597         /*
598          * Allow status to settle, then read it again.
599          * A few rare drives vastly violate the 400ns spec here,
600          * so we'll wait up to 10usec for a "good" status
601          * rather than expensively fail things immediately.
602          * This fix courtesy of Matthew Faupel & Niccolo Rigacci.
603          */
604         for (i = 0; i < 10; i++) {
605                 udelay(1);
606                 if (OK_STAT((stat = hwif->INB(IDE_STATUS_REG)), good, bad))
607                         return 0;
608         }
609         *startstop = DRIVER(drive)->error(drive, "status error", stat);
610         return 1;
611 }
612
613 EXPORT_SYMBOL(ide_wait_stat);
614
615 /*
616  *  All hosts that use the 80c ribbon must use!
617  *  The name is derived from upper byte of word 93 and the 80c ribbon.
618  */
619 u8 eighty_ninty_three (ide_drive_t *drive)
620 {
621 #if 0
622         if (!HWIF(drive)->udma_four)
623                 return 0;
624
625         if (drive->id->major_rev_num) {
626                 int hssbd = 0;
627                 int i;
628                 /*
629                  * Determine highest Supported SPEC
630                  */
631                 for (i=1; i<=15; i++)
632                         if (drive->id->major_rev_num & (1<<i))
633                                 hssbd++;
634
635                 switch (hssbd) {
636                         case 7:
637                         case 6:
638                         case 5:
639                 /* ATA-4 and older do not support above Ultra 33 */
640                         default:
641                                 return 0;
642                 }
643         }
644
645         return ((u8) (
646 #ifndef CONFIG_IDEDMA_IVB
647                 (drive->id->hw_config & 0x4000) &&
648 #endif /* CONFIG_IDEDMA_IVB */
649                  (drive->id->hw_config & 0x6000)) ? 1 : 0);
650
651 #else
652
653         return ((u8) ((HWIF(drive)->udma_four) &&
654 #ifndef CONFIG_IDEDMA_IVB
655                         (drive->id->hw_config & 0x4000) &&
656 #endif /* CONFIG_IDEDMA_IVB */
657                         (drive->id->hw_config & 0x6000)) ? 1 : 0);
658 #endif
659 }
660
661 EXPORT_SYMBOL(eighty_ninty_three);
662
663 int ide_ata66_check (ide_drive_t *drive, ide_task_t *args)
664 {
665         if ((args->tfRegister[IDE_COMMAND_OFFSET] == WIN_SETFEATURES) &&
666             (args->tfRegister[IDE_SECTOR_OFFSET] > XFER_UDMA_2) &&
667             (args->tfRegister[IDE_FEATURE_OFFSET] == SETFEATURES_XFER)) {
668 #ifndef CONFIG_IDEDMA_IVB
669                 if ((drive->id->hw_config & 0x6000) == 0) {
670 #else /* !CONFIG_IDEDMA_IVB */
671                 if (((drive->id->hw_config & 0x2000) == 0) ||
672                     ((drive->id->hw_config & 0x4000) == 0)) {
673 #endif /* CONFIG_IDEDMA_IVB */
674                         printk("%s: Speed warnings UDMA 3/4/5 is not "
675                                 "functional.\n", drive->name);
676                         return 1;
677                 }
678                 if (!HWIF(drive)->udma_four) {
679                         printk("%s: Speed warnings UDMA 3/4/5 is not "
680                                 "functional.\n",
681                                 HWIF(drive)->name);
682                         return 1;
683                 }
684         }
685         return 0;
686 }
687
688 EXPORT_SYMBOL(ide_ata66_check);
689
690 /*
691  * Backside of HDIO_DRIVE_CMD call of SETFEATURES_XFER.
692  * 1 : Safe to update drive->id DMA registers.
693  * 0 : OOPs not allowed.
694  */
695 int set_transfer (ide_drive_t *drive, ide_task_t *args)
696 {
697         if ((args->tfRegister[IDE_COMMAND_OFFSET] == WIN_SETFEATURES) &&
698             (args->tfRegister[IDE_SECTOR_OFFSET] >= XFER_SW_DMA_0) &&
699             (args->tfRegister[IDE_FEATURE_OFFSET] == SETFEATURES_XFER) &&
700             (drive->id->dma_ultra ||
701              drive->id->dma_mword ||
702              drive->id->dma_1word))
703                 return 1;
704
705         return 0;
706 }
707
708 EXPORT_SYMBOL(set_transfer);
709
710 u8 ide_auto_reduce_xfer (ide_drive_t *drive)
711 {
712         if (!drive->crc_count)
713                 return drive->current_speed;
714         drive->crc_count = 0;
715
716         switch(drive->current_speed) {
717                 case XFER_UDMA_7:       return XFER_UDMA_6;
718                 case XFER_UDMA_6:       return XFER_UDMA_5;
719                 case XFER_UDMA_5:       return XFER_UDMA_4;
720                 case XFER_UDMA_4:       return XFER_UDMA_3;
721                 case XFER_UDMA_3:       return XFER_UDMA_2;
722                 case XFER_UDMA_2:       return XFER_UDMA_1;
723                 case XFER_UDMA_1:       return XFER_UDMA_0;
724                         /*
725                          * OOPS we do not goto non Ultra DMA modes
726                          * without iCRC's available we force
727                          * the system to PIO and make the user
728                          * invoke the ATA-1 ATA-2 DMA modes.
729                          */
730                 case XFER_UDMA_0:
731                 default:                return XFER_PIO_4;
732         }
733 }
734
735 EXPORT_SYMBOL(ide_auto_reduce_xfer);
736
737 /*
738  * Update the 
739  */
740 int ide_driveid_update (ide_drive_t *drive)
741 {
742         ide_hwif_t *hwif        = HWIF(drive);
743         struct hd_driveid *id;
744 #if 0
745         id = kmalloc(SECTOR_WORDS*4, GFP_ATOMIC);
746         if (!id)
747                 return 0;
748
749         taskfile_lib_get_identify(drive, (char *)&id);
750
751         ide_fix_driveid(id);
752         if (id) {
753                 drive->id->dma_ultra = id->dma_ultra;
754                 drive->id->dma_mword = id->dma_mword;
755                 drive->id->dma_1word = id->dma_1word;
756                 /* anything more ? */
757                 kfree(id);
758         }
759         return 1;
760 #else
761         /*
762          * Re-read drive->id for possible DMA mode
763          * change (copied from ide-probe.c)
764          */
765         unsigned long timeout, flags;
766
767         SELECT_MASK(drive, 1);
768         if (IDE_CONTROL_REG)
769                 hwif->OUTB(drive->ctl,IDE_CONTROL_REG);
770         msleep(50);
771         hwif->OUTB(WIN_IDENTIFY, IDE_COMMAND_REG);
772         timeout = jiffies + WAIT_WORSTCASE;
773         do {
774                 if (time_after(jiffies, timeout)) {
775                         SELECT_MASK(drive, 0);
776                         return 0;       /* drive timed-out */
777                 }
778                 msleep(50);     /* give drive a breather */
779         } while (hwif->INB(IDE_ALTSTATUS_REG) & BUSY_STAT);
780         msleep(50);     /* wait for IRQ and DRQ_STAT */
781         if (!OK_STAT(hwif->INB(IDE_STATUS_REG),DRQ_STAT,BAD_R_STAT)) {
782                 SELECT_MASK(drive, 0);
783                 printk("%s: CHECK for good STATUS\n", drive->name);
784                 return 0;
785         }
786         local_irq_save(flags);
787         SELECT_MASK(drive, 0);
788         id = kmalloc(SECTOR_WORDS*4, GFP_ATOMIC);
789         if (!id) {
790                 local_irq_restore(flags);
791                 return 0;
792         }
793         ata_input_data(drive, id, SECTOR_WORDS);
794         (void) hwif->INB(IDE_STATUS_REG);       /* clear drive IRQ */
795         local_irq_enable();
796         local_irq_restore(flags);
797         ide_fix_driveid(id);
798         if (id) {
799                 drive->id->dma_ultra = id->dma_ultra;
800                 drive->id->dma_mword = id->dma_mword;
801                 drive->id->dma_1word = id->dma_1word;
802                 /* anything more ? */
803                 kfree(id);
804         }
805
806         return 1;
807 #endif
808 }
809
810 EXPORT_SYMBOL(ide_driveid_update);
811
812 /*
813  * Similar to ide_wait_stat(), except it never calls ide_error internally.
814  * This is a kludge to handle the new ide_config_drive_speed() function,
815  * and should not otherwise be used anywhere.  Eventually, the tuneproc's
816  * should be updated to return ide_startstop_t, in which case we can get
817  * rid of this abomination again.  :)   -ml
818  *
819  * It is gone..........
820  *
821  * const char *msg == consider adding for verbose errors.
822  */
823 int ide_config_drive_speed (ide_drive_t *drive, u8 speed)
824 {
825         ide_hwif_t *hwif        = HWIF(drive);
826         int     i, error        = 1;
827         u8 stat;
828
829 //      while (HWGROUP(drive)->busy)
830 //              msleep(50);
831
832 #ifdef CONFIG_BLK_DEV_IDEDMA
833         if (hwif->ide_dma_check)         /* check if host supports DMA */
834                 hwif->ide_dma_host_off(drive);
835 #endif
836
837         /*
838          * Don't use ide_wait_cmd here - it will
839          * attempt to set_geometry and recalibrate,
840          * but for some reason these don't work at
841          * this point (lost interrupt).
842          */
843         /*
844          * Select the drive, and issue the SETFEATURES command
845          */
846         disable_irq_nosync(hwif->irq);
847         
848         /*
849          *      FIXME: we race against the running IRQ here if
850          *      this is called from non IRQ context. If we use
851          *      disable_irq() we hang on the error path. Work
852          *      is needed.
853          */
854          
855         udelay(1);
856         SELECT_DRIVE(drive);
857         SELECT_MASK(drive, 0);
858         udelay(1);
859         if (IDE_CONTROL_REG)
860                 hwif->OUTB(drive->ctl | 2, IDE_CONTROL_REG);
861         hwif->OUTB(speed, IDE_NSECTOR_REG);
862         hwif->OUTB(SETFEATURES_XFER, IDE_FEATURE_REG);
863         hwif->OUTB(WIN_SETFEATURES, IDE_COMMAND_REG);
864         if ((IDE_CONTROL_REG) && (drive->quirk_list == 2))
865                 hwif->OUTB(drive->ctl, IDE_CONTROL_REG);
866         udelay(1);
867         /*
868          * Wait for drive to become non-BUSY
869          */
870         if ((stat = hwif->INB(IDE_STATUS_REG)) & BUSY_STAT) {
871                 unsigned long flags, timeout;
872                 local_irq_set(flags);
873                 timeout = jiffies + WAIT_CMD;
874                 while ((stat = hwif->INB(IDE_STATUS_REG)) & BUSY_STAT) {
875                         if (time_after(jiffies, timeout))
876                                 break;
877                 }
878                 local_irq_restore(flags);
879         }
880
881         /*
882          * Allow status to settle, then read it again.
883          * A few rare drives vastly violate the 400ns spec here,
884          * so we'll wait up to 10usec for a "good" status
885          * rather than expensively fail things immediately.
886          * This fix courtesy of Matthew Faupel & Niccolo Rigacci.
887          */
888         for (i = 0; i < 10; i++) {
889                 udelay(1);
890                 if (OK_STAT((stat = hwif->INB(IDE_STATUS_REG)), DRIVE_READY, BUSY_STAT|DRQ_STAT|ERR_STAT)) {
891                         error = 0;
892                         break;
893                 }
894         }
895
896         SELECT_MASK(drive, 0);
897
898         enable_irq(hwif->irq);
899
900         if (error) {
901                 (void) ide_dump_status(drive, "set_drive_speed_status", stat);
902                 return error;
903         }
904
905         drive->id->dma_ultra &= ~0xFF00;
906         drive->id->dma_mword &= ~0x0F00;
907         drive->id->dma_1word &= ~0x0F00;
908
909 #ifdef CONFIG_BLK_DEV_IDEDMA
910         if (speed >= XFER_SW_DMA_0)
911                 hwif->ide_dma_host_on(drive);
912         else if (hwif->ide_dma_check)   /* check if host supports DMA */
913                 hwif->ide_dma_off_quietly(drive);
914 #endif
915
916         switch(speed) {
917                 case XFER_UDMA_7:   drive->id->dma_ultra |= 0x8080; break;
918                 case XFER_UDMA_6:   drive->id->dma_ultra |= 0x4040; break;
919                 case XFER_UDMA_5:   drive->id->dma_ultra |= 0x2020; break;
920                 case XFER_UDMA_4:   drive->id->dma_ultra |= 0x1010; break;
921                 case XFER_UDMA_3:   drive->id->dma_ultra |= 0x0808; break;
922                 case XFER_UDMA_2:   drive->id->dma_ultra |= 0x0404; break;
923                 case XFER_UDMA_1:   drive->id->dma_ultra |= 0x0202; break;
924                 case XFER_UDMA_0:   drive->id->dma_ultra |= 0x0101; break;
925                 case XFER_MW_DMA_2: drive->id->dma_mword |= 0x0404; break;
926                 case XFER_MW_DMA_1: drive->id->dma_mword |= 0x0202; break;
927                 case XFER_MW_DMA_0: drive->id->dma_mword |= 0x0101; break;
928                 case XFER_SW_DMA_2: drive->id->dma_1word |= 0x0404; break;
929                 case XFER_SW_DMA_1: drive->id->dma_1word |= 0x0202; break;
930                 case XFER_SW_DMA_0: drive->id->dma_1word |= 0x0101; break;
931                 default: break;
932         }
933         if (!drive->init_speed)
934                 drive->init_speed = speed;
935         drive->current_speed = speed;
936         return error;
937 }
938
939 EXPORT_SYMBOL(ide_config_drive_speed);
940
941
942 /*
943  * This should get invoked any time we exit the driver to
944  * wait for an interrupt response from a drive.  handler() points
945  * at the appropriate code to handle the next interrupt, and a
946  * timer is started to prevent us from waiting forever in case
947  * something goes wrong (see the ide_timer_expiry() handler later on).
948  *
949  * See also ide_execute_command
950  */
951 void __ide_set_handler (ide_drive_t *drive, ide_handler_t *handler,
952                       unsigned int timeout, ide_expiry_t *expiry)
953 {
954         ide_hwgroup_t *hwgroup = HWGROUP(drive);
955
956         if (hwgroup->handler != NULL) {
957                 printk(KERN_CRIT "%s: ide_set_handler: handler not null; "
958                         "old=%p, new=%p\n",
959                         drive->name, hwgroup->handler, handler);
960         }
961         hwgroup->handler        = handler;
962         hwgroup->expiry         = expiry;
963         hwgroup->timer.expires  = jiffies + timeout;
964         add_timer(&hwgroup->timer);
965 }
966
967 EXPORT_SYMBOL(__ide_set_handler);
968
969 void ide_set_handler (ide_drive_t *drive, ide_handler_t *handler,
970                       unsigned int timeout, ide_expiry_t *expiry)
971 {
972         unsigned long flags;
973         spin_lock_irqsave(&ide_lock, flags);
974         __ide_set_handler(drive, handler, timeout, expiry);
975         spin_unlock_irqrestore(&ide_lock, flags);
976 }
977
978 EXPORT_SYMBOL(ide_set_handler);
979  
980 /**
981  *      ide_execute_command     -       execute an IDE command
982  *      @drive: IDE drive to issue the command against
983  *      @command: command byte to write
984  *      @handler: handler for next phase
985  *      @timeout: timeout for command
986  *      @expiry:  handler to run on timeout
987  *
988  *      Helper function to issue an IDE command. This handles the
989  *      atomicity requirements, command timing and ensures that the 
990  *      handler and IRQ setup do not race. All IDE command kick off
991  *      should go via this function or do equivalent locking.
992  */
993  
994 void ide_execute_command(ide_drive_t *drive, task_ioreg_t cmd, ide_handler_t *handler, unsigned timeout, ide_expiry_t *expiry)
995 {
996         unsigned long flags;
997         ide_hwgroup_t *hwgroup = HWGROUP(drive);
998         ide_hwif_t *hwif = HWIF(drive);
999         
1000         spin_lock_irqsave(&ide_lock, flags);
1001         
1002         if(hwgroup->handler)
1003                 BUG();
1004         hwgroup->handler        = handler;
1005         hwgroup->expiry         = expiry;
1006         hwgroup->timer.expires  = jiffies + timeout;
1007         add_timer(&hwgroup->timer);
1008         hwif->OUTBSYNC(drive, cmd, IDE_COMMAND_REG);
1009         /* Drive takes 400nS to respond, we must avoid the IRQ being
1010            serviced before that. 
1011            
1012            FIXME: we could skip this delay with care on non shared
1013            devices 
1014         */
1015         ndelay(400);
1016         spin_unlock_irqrestore(&ide_lock, flags);
1017 }
1018
1019 EXPORT_SYMBOL(ide_execute_command);
1020
1021
1022 /* needed below */
1023 static ide_startstop_t do_reset1 (ide_drive_t *, int);
1024
1025 /*
1026  * atapi_reset_pollfunc() gets invoked to poll the interface for completion every 50ms
1027  * during an atapi drive reset operation. If the drive has not yet responded,
1028  * and we have not yet hit our maximum waiting time, then the timer is restarted
1029  * for another 50ms.
1030  */
1031 static ide_startstop_t atapi_reset_pollfunc (ide_drive_t *drive)
1032 {
1033         ide_hwgroup_t *hwgroup  = HWGROUP(drive);
1034         ide_hwif_t *hwif        = HWIF(drive);
1035         u8 stat;
1036
1037         SELECT_DRIVE(drive);
1038         udelay (10);
1039
1040         if (OK_STAT(stat = hwif->INB(IDE_STATUS_REG), 0, BUSY_STAT)) {
1041                 printk("%s: ATAPI reset complete\n", drive->name);
1042         } else {
1043                 if (time_before(jiffies, hwgroup->poll_timeout)) {
1044                         if (HWGROUP(drive)->handler != NULL)
1045                                 BUG();
1046                         ide_set_handler(drive, &atapi_reset_pollfunc, HZ/20, NULL);
1047                         /* continue polling */
1048                         return ide_started;
1049                 }
1050                 /* end of polling */
1051                 hwgroup->poll_timeout = 0;
1052                 printk("%s: ATAPI reset timed-out, status=0x%02x\n",
1053                                 drive->name, stat);
1054                 /* do it the old fashioned way */
1055                 return do_reset1(drive, 1);
1056         }
1057         /* done polling */
1058         hwgroup->poll_timeout = 0;
1059         return ide_stopped;
1060 }
1061
1062 /*
1063  * reset_pollfunc() gets invoked to poll the interface for completion every 50ms
1064  * during an ide reset operation. If the drives have not yet responded,
1065  * and we have not yet hit our maximum waiting time, then the timer is restarted
1066  * for another 50ms.
1067  */
1068 static ide_startstop_t reset_pollfunc (ide_drive_t *drive)
1069 {
1070         ide_hwgroup_t *hwgroup  = HWGROUP(drive);
1071         ide_hwif_t *hwif        = HWIF(drive);
1072         u8 tmp;
1073
1074         if (hwif->reset_poll != NULL) {
1075                 if (hwif->reset_poll(drive)) {
1076                         printk(KERN_ERR "%s: host reset_poll failure for %s.\n",
1077                                 hwif->name, drive->name);
1078                         return ide_stopped;
1079                 }
1080         }
1081
1082         if (!OK_STAT(tmp = hwif->INB(IDE_STATUS_REG), 0, BUSY_STAT)) {
1083                 if (time_before(jiffies, hwgroup->poll_timeout)) {
1084                         if (HWGROUP(drive)->handler != NULL)
1085                                 BUG();
1086                         ide_set_handler(drive, &reset_pollfunc, HZ/20, NULL);
1087                         /* continue polling */
1088                         return ide_started;
1089                 }
1090                 printk("%s: reset timed-out, status=0x%02x\n", hwif->name, tmp);
1091                 drive->failures++;
1092         } else  {
1093                 printk("%s: reset: ", hwif->name);
1094                 if ((tmp = hwif->INB(IDE_ERROR_REG)) == 1) {
1095                         printk("success\n");
1096                         drive->failures = 0;
1097                 } else {
1098                         drive->failures++;
1099 #if FANCY_STATUS_DUMPS
1100                         printk("master: ");
1101                         switch (tmp & 0x7f) {
1102                                 case 1: printk("passed");
1103                                         break;
1104                                 case 2: printk("formatter device error");
1105                                         break;
1106                                 case 3: printk("sector buffer error");
1107                                         break;
1108                                 case 4: printk("ECC circuitry error");
1109                                         break;
1110                                 case 5: printk("controlling MPU error");
1111                                         break;
1112                                 default:printk("error (0x%02x?)", tmp);
1113                         }
1114                         if (tmp & 0x80)
1115                                 printk("; slave: failed");
1116                         printk("\n");
1117 #else
1118                         printk("failed\n");
1119 #endif /* FANCY_STATUS_DUMPS */
1120                 }
1121         }
1122         hwgroup->poll_timeout = 0;      /* done polling */
1123         return ide_stopped;
1124 }
1125
1126 static void check_dma_crc(ide_drive_t *drive)
1127 {
1128 #ifdef CONFIG_BLK_DEV_IDEDMA
1129         if (drive->crc_count) {
1130                 (void) HWIF(drive)->ide_dma_off_quietly(drive);
1131                 ide_set_xfer_rate(drive, ide_auto_reduce_xfer(drive));
1132                 if (drive->current_speed >= XFER_SW_DMA_0)
1133                         (void) HWIF(drive)->ide_dma_on(drive);
1134         } else
1135                 (void)__ide_dma_off(drive);
1136 #endif
1137 }
1138
1139 void pre_reset (ide_drive_t *drive)
1140 {
1141         DRIVER(drive)->pre_reset(drive);
1142
1143         if (!drive->keep_settings) {
1144                 if (drive->using_dma) {
1145                         check_dma_crc(drive);
1146                 } else {
1147                         drive->unmask = 0;
1148                         drive->io_32bit = 0;
1149                 }
1150                 return;
1151         }
1152         if (drive->using_dma)
1153                 check_dma_crc(drive);
1154
1155         if (HWIF(drive)->pre_reset != NULL)
1156                 HWIF(drive)->pre_reset(drive);
1157
1158 }
1159
1160 /*
1161  * do_reset1() attempts to recover a confused drive by resetting it.
1162  * Unfortunately, resetting a disk drive actually resets all devices on
1163  * the same interface, so it can really be thought of as resetting the
1164  * interface rather than resetting the drive.
1165  *
1166  * ATAPI devices have their own reset mechanism which allows them to be
1167  * individually reset without clobbering other devices on the same interface.
1168  *
1169  * Unfortunately, the IDE interface does not generate an interrupt to let
1170  * us know when the reset operation has finished, so we must poll for this.
1171  * Equally poor, though, is the fact that this may a very long time to complete,
1172  * (up to 30 seconds worstcase).  So, instead of busy-waiting here for it,
1173  * we set a timer to poll at 50ms intervals.
1174  */
1175 static ide_startstop_t do_reset1 (ide_drive_t *drive, int do_not_try_atapi)
1176 {
1177         unsigned int unit;
1178         unsigned long flags;
1179         ide_hwif_t *hwif;
1180         ide_hwgroup_t *hwgroup;
1181         
1182         spin_lock_irqsave(&ide_lock, flags);
1183         hwif = HWIF(drive);
1184         hwgroup = HWGROUP(drive);
1185
1186         /* We must not reset with running handlers */
1187         if(hwgroup->handler != NULL)
1188                 BUG();
1189
1190         /* For an ATAPI device, first try an ATAPI SRST. */
1191         if (drive->media != ide_disk && !do_not_try_atapi) {
1192                 pre_reset(drive);
1193                 SELECT_DRIVE(drive);
1194                 udelay (20);
1195                 hwif->OUTB(WIN_SRST, IDE_COMMAND_REG);
1196                 hwgroup->poll_timeout = jiffies + WAIT_WORSTCASE;
1197                 __ide_set_handler(drive, &atapi_reset_pollfunc, HZ/20, NULL);
1198                 spin_unlock_irqrestore(&ide_lock, flags);
1199                 return ide_started;
1200         }
1201
1202         /*
1203          * First, reset any device state data we were maintaining
1204          * for any of the drives on this interface.
1205          */
1206         for (unit = 0; unit < MAX_DRIVES; ++unit)
1207                 pre_reset(&hwif->drives[unit]);
1208
1209 #if OK_TO_RESET_CONTROLLER
1210         if (!IDE_CONTROL_REG) {
1211                 spin_unlock_irqrestore(&ide_lock, flags);
1212                 return ide_stopped;
1213         }
1214
1215         /*
1216          * Note that we also set nIEN while resetting the device,
1217          * to mask unwanted interrupts from the interface during the reset.
1218          * However, due to the design of PC hardware, this will cause an
1219          * immediate interrupt due to the edge transition it produces.
1220          * This single interrupt gives us a "fast poll" for drives that
1221          * recover from reset very quickly, saving us the first 50ms wait time.
1222          */
1223         /* set SRST and nIEN */
1224         hwif->OUTBSYNC(drive, drive->ctl|6,IDE_CONTROL_REG);
1225         /* more than enough time */
1226         udelay(10);
1227         if (drive->quirk_list == 2) {
1228                 /* clear SRST and nIEN */
1229                 hwif->OUTBSYNC(drive, drive->ctl, IDE_CONTROL_REG);
1230         } else {
1231                 /* clear SRST, leave nIEN */
1232                 hwif->OUTBSYNC(drive, drive->ctl|2, IDE_CONTROL_REG);
1233         }
1234         /* more than enough time */
1235         udelay(10);
1236         hwgroup->poll_timeout = jiffies + WAIT_WORSTCASE;
1237         __ide_set_handler(drive, &reset_pollfunc, HZ/20, NULL);
1238
1239         /*
1240          * Some weird controller like resetting themselves to a strange
1241          * state when the disks are reset this way. At least, the Winbond
1242          * 553 documentation says that
1243          */
1244         if (hwif->resetproc != NULL) {
1245                 hwif->resetproc(drive);
1246         }
1247         
1248 #endif  /* OK_TO_RESET_CONTROLLER */
1249
1250         spin_unlock_irqrestore(&ide_lock, flags);
1251         return ide_started;
1252 }
1253
1254 /*
1255  * ide_do_reset() is the entry point to the drive/interface reset code.
1256  */
1257
1258 ide_startstop_t ide_do_reset (ide_drive_t *drive)
1259 {
1260         return do_reset1(drive, 0);
1261 }
1262
1263 EXPORT_SYMBOL(ide_do_reset);
1264
1265 /*
1266  * ide_wait_not_busy() waits for the currently selected device on the hwif
1267  * to report a non-busy status, see comments in probe_hwif().
1268  */
1269 int ide_wait_not_busy(ide_hwif_t *hwif, unsigned long timeout)
1270 {
1271         u8 stat = 0;
1272
1273         while(timeout--) {
1274                 /*
1275                  * Turn this into a schedule() sleep once I'm sure
1276                  * about locking issues (2.5 work ?).
1277                  */
1278                 mdelay(1);
1279                 stat = hwif->INB(hwif->io_ports[IDE_STATUS_OFFSET]);
1280                 if ((stat & BUSY_STAT) == 0)
1281                         return 0;
1282                 /*
1283                  * Assume a value of 0xff means nothing is connected to
1284                  * the interface and it doesn't implement the pull-down
1285                  * resistor on D7.
1286                  */
1287                 if (stat == 0xff)
1288                         return -ENODEV;
1289         }
1290         return -EBUSY;
1291 }
1292
1293 EXPORT_SYMBOL_GPL(ide_wait_not_busy);
1294