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