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