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