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