2 * Copyright (C) 2000-2002 Andre Hedrick <andre@linux-ide.org>
3 * Copyright (C) 2003 Red Hat <alan@redhat.com>
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>
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>
26 #include <asm/byteorder.h>
28 #include <asm/uaccess.h>
32 * Conventional PIO operations for ATA devices
35 static u8 ide_inb (unsigned long port)
37 return (u8) inb(port);
40 static void ide_outb (u8 val, unsigned long port)
45 static void ide_outbsync (ide_drive_t *drive, u8 addr, unsigned long port)
50 void default_hwif_iops (ide_hwif_t *hwif)
52 hwif->OUTB = ide_outb;
53 hwif->OUTBSYNC = ide_outbsync;
58 * MMIO operations, typically used for SATA controllers
61 static u8 ide_mm_inb (unsigned long port)
63 return (u8) readb((void __iomem *) port);
66 static void ide_mm_outb (u8 value, unsigned long port)
68 writeb(value, (void __iomem *) port);
71 static void ide_mm_outbsync (ide_drive_t *drive, u8 value, unsigned long port)
73 writeb(value, (void __iomem *) port);
76 void default_hwif_mmiops (ide_hwif_t *hwif)
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;
85 EXPORT_SYMBOL(default_hwif_mmiops);
87 void SELECT_DRIVE (ide_drive_t *drive)
89 ide_hwif_t *hwif = drive->hwif;
90 const struct ide_port_ops *port_ops = hwif->port_ops;
92 if (port_ops && port_ops->selectproc)
93 port_ops->selectproc(drive);
95 hwif->OUTB(drive->select.all, hwif->io_ports.device_addr);
98 static void SELECT_MASK(ide_drive_t *drive, int mask)
100 const struct ide_port_ops *port_ops = drive->hwif->port_ops;
102 if (port_ops && port_ops->maskproc)
103 port_ops->maskproc(drive, mask);
106 static void ide_tf_load(ide_drive_t *drive, ide_task_t *task)
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;
116 tf_outb = ide_mm_outb;
120 if (task->tf_flags & IDE_TFLAG_FLAGGED)
123 ide_set_irq(drive, 1);
124 SELECT_MASK(drive, 0);
126 if (task->tf_flags & IDE_TFLAG_OUT_DATA) {
127 u16 data = (tf->hob_data << 8) | tf->data;
130 writew(data, (void __iomem *)io_ports->data_addr);
132 outw(data, io_ports->data_addr);
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);
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);
157 if (task->tf_flags & IDE_TFLAG_OUT_DEVICE)
158 tf_outb((tf->device & HIHI) | drive->select.all,
159 io_ports->device_addr);
162 static void ide_tf_read(ide_drive_t *drive, ide_task_t *task)
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;
172 tf_outb = ide_mm_outb;
179 if (task->tf_flags & IDE_TFLAG_IN_DATA) {
183 data = readw((void __iomem *)io_ports->data_addr);
185 data = inw(io_ports->data_addr);
187 tf->data = data & 0xff;
188 tf->hob_data = (data >> 8) & 0xff;
191 /* be sure we're looking at the low order bits */
192 tf_outb(drive->ctl & ~0x80, io_ports->ctl_addr);
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);
205 if (task->tf_flags & IDE_TFLAG_LBA48) {
206 tf_outb(drive->ctl | 0x80, io_ports->ctl_addr);
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);
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.
228 static void ata_vlb_sync(unsigned long port)
236 * This is used for most PIO data transfers *from* the IDE interface
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.
242 static void ata_input_data(ide_drive_t *drive, struct request *rq,
243 void *buf, unsigned int len)
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;
254 unsigned long uninitialized_var(flags);
256 if ((io_32bit & 2) && !mmio) {
257 local_irq_save(flags);
258 ata_vlb_sync(io_ports->nsect_addr);
262 __ide_mm_insl((void __iomem *)data_addr, buf, len / 4);
264 insl(data_addr, buf, len / 4);
266 if ((io_32bit & 2) && !mmio)
267 local_irq_restore(flags);
269 if ((len & 3) >= 2) {
271 __ide_mm_insw((void __iomem *)data_addr,
272 (u8 *)buf + (len & ~3), 1);
274 insw(data_addr, (u8 *)buf + (len & ~3), 1);
278 __ide_mm_insw((void __iomem *)data_addr, buf, len / 2);
280 insw(data_addr, buf, len / 2);
285 * This is used for most PIO data transfers *to* the IDE interface
287 static void ata_output_data(ide_drive_t *drive, struct request *rq,
288 void *buf, unsigned int len)
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;
297 unsigned long uninitialized_var(flags);
299 if ((io_32bit & 2) && !mmio) {
300 local_irq_save(flags);
301 ata_vlb_sync(io_ports->nsect_addr);
305 __ide_mm_outsl((void __iomem *)data_addr, buf, len / 4);
307 outsl(data_addr, buf, len / 4);
309 if ((io_32bit & 2) && !mmio)
310 local_irq_restore(flags);
312 if ((len & 3) >= 2) {
314 __ide_mm_outsw((void __iomem *)data_addr,
315 (u8 *)buf + (len & ~3), 1);
317 outsw(data_addr, (u8 *)buf + (len & ~3), 1);
321 __ide_mm_outsw((void __iomem *)data_addr, buf, len / 2);
323 outsw(data_addr, buf, len / 2);
327 void default_hwif_transport(ide_hwif_t *hwif)
329 hwif->tf_load = ide_tf_load;
330 hwif->tf_read = ide_tf_read;
332 hwif->input_data = ata_input_data;
333 hwif->output_data = ata_output_data;
336 void ide_fix_driveid (struct hd_driveid *id)
338 #ifndef __LITTLE_ENDIAN
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);
428 # error "Please fix <asm/byteorder.h>"
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.
440 void ide_fixstring (u8 *s, const int bytecount, const int byteswap)
442 u8 *p = s, *end = &s[bytecount & ~1]; /* bytecount must be even */
445 /* convert from big-endian to host byte order */
446 for (p = end ; p != s;) {
447 unsigned short *pp = (unsigned short *) (p -= 2);
451 /* strip leading blanks */
452 while (s != end && *s == ' ')
454 /* compress internal blanks and strip trailing blanks */
455 while (s != end && *s) {
456 if (*s++ != ' ' || (s != end && *s && *s != ' '))
459 /* wipe out trailing garbage */
464 EXPORT_SYMBOL(ide_fixstring);
467 * Needed for PCI irq sharing
469 int drive_is_ready (ide_drive_t *drive)
471 ide_hwif_t *hwif = HWIF(drive);
474 if (drive->waiting_for_dma)
475 return hwif->dma_ops->dma_test_irq(drive);
478 /* need to guarantee 400ns since last command was issued */
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.
488 if (hwif->io_ports.ctl_addr)
489 stat = ide_read_altstatus(drive);
491 /* Note: this may clear a pending IRQ!! */
492 stat = ide_read_status(drive);
494 if (stat & BUSY_STAT)
495 /* drive busy: definitely not interrupting */
498 /* drive ready: *might* be interrupting */
502 EXPORT_SYMBOL(drive_is_ready);
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().
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.
515 static int __ide_wait_stat(ide_drive_t *drive, u8 good, u8 bad, unsigned long timeout, u8 *rstat)
521 udelay(1); /* spec allows drive 400ns to assert "BUSY" */
522 stat = ide_read_status(drive);
524 if (stat & BUSY_STAT) {
525 local_irq_set(flags);
527 while ((stat = ide_read_status(drive)) & BUSY_STAT) {
528 if (time_after(jiffies, timeout)) {
530 * One last read after the timeout in case
531 * heavy interrupt load made us not make any
532 * progress during the timeout..
534 stat = ide_read_status(drive);
535 if (!(stat & BUSY_STAT))
538 local_irq_restore(flags);
543 local_irq_restore(flags);
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.
552 for (i = 0; i < 10; i++) {
554 stat = ide_read_status(drive);
556 if (OK_STAT(stat, good, bad)) {
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.
570 int ide_wait_stat(ide_startstop_t *startstop, ide_drive_t *drive, u8 good, u8 bad, unsigned long timeout)
575 /* bail early if we've exceeded max_failures */
576 if (drive->max_failures && (drive->failures > drive->max_failures)) {
577 *startstop = ide_stopped;
581 err = __ide_wait_stat(drive, good, bad, timeout, &stat);
584 char *s = (err == -EBUSY) ? "status timeout" : "status error";
585 *startstop = ide_error(drive, s, stat);
591 EXPORT_SYMBOL(ide_wait_stat);
594 * ide_in_drive_list - look for drive in black/white list
595 * @id: drive identifier
596 * @drive_table: list to inspect
598 * Look for a drive in the blacklist and the whitelist tables
599 * Returns 1 if the drive is found in the table.
602 int ide_in_drive_list(struct hd_driveid *id, const struct drive_list_entry *drive_table)
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)))
612 EXPORT_SYMBOL_GPL(ide_in_drive_list);
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.
618 * Some optical devices with the buggy firmwares have the same problem.
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" },
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.
635 u8 eighty_ninty_three (ide_drive_t *drive)
637 ide_hwif_t *hwif = drive->hwif;
638 struct hd_driveid *id = drive->id;
639 int ivb = ide_in_drive_list(id, ivb_list);
641 if (hwif->cbl == ATA_CBL_PATA40_SHORT)
645 printk(KERN_DEBUG "%s: skipping word 93 validity check\n",
648 if (ide_dev_is_sata(id) && !ivb)
651 if (hwif->cbl != ATA_CBL_PATA80 && !ivb)
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)
660 if ((id->hw_config & 0x4000) || (ivb && (id->hw_config & 0x2000)))
664 if (drive->udma33_warned == 1)
667 printk(KERN_WARNING "%s: %s side 80-wire cable detection failed, "
668 "limiting max speed to UDMA33\n",
670 hwif->cbl == ATA_CBL_PATA80 ? "drive" : "host");
672 drive->udma33_warned = 1;
677 int ide_driveid_update(ide_drive_t *drive)
679 ide_hwif_t *hwif = drive->hwif;
680 struct hd_driveid *id;
681 unsigned long timeout, flags;
685 * Re-read drive->id for possible DMA mode
686 * change (copied from ide-probe.c)
689 SELECT_MASK(drive, 1);
690 ide_set_irq(drive, 0);
692 hwif->OUTBSYNC(drive, WIN_IDENTIFY, hwif->io_ports.command_addr);
693 timeout = jiffies + WAIT_WORSTCASE;
695 if (time_after(jiffies, timeout)) {
696 SELECT_MASK(drive, 0);
697 return 0; /* drive timed-out */
700 msleep(50); /* give drive a breather */
701 stat = ide_read_altstatus(drive);
702 } while (stat & BUSY_STAT);
704 msleep(50); /* wait for IRQ and DRQ_STAT */
705 stat = ide_read_status(drive);
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);
712 local_irq_save(flags);
713 SELECT_MASK(drive, 0);
714 id = kmalloc(SECTOR_WORDS*4, GFP_ATOMIC);
716 local_irq_restore(flags);
719 hwif->input_data(drive, NULL, id, SECTOR_SIZE);
720 (void)ide_read_status(drive); /* clear drive IRQ */
722 local_irq_restore(flags);
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 ? */
731 if (drive->using_dma && ide_id_dma_bug(drive))
738 int ide_config_drive_speed(ide_drive_t *drive, u8 speed)
740 ide_hwif_t *hwif = drive->hwif;
741 struct ide_io_ports *io_ports = &hwif->io_ports;
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);
750 /* Skip setting PIO flow-control modes on pre-EIDE drives */
751 if ((speed & 0xf8) == XFER_PIO_0 && !(drive->id->capability & 0x08))
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).
761 * Select the drive, and issue the SETFEATURES command
763 disable_irq_nosync(hwif->irq);
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
774 SELECT_MASK(drive, 0);
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);
783 error = __ide_wait_stat(drive, drive->ready_stat,
784 BUSY_STAT|DRQ_STAT|ERR_STAT,
787 SELECT_MASK(drive, 0);
789 enable_irq(hwif->irq);
792 (void) ide_dump_status(drive, "set_drive_speed_status", stat);
796 drive->id->dma_ultra &= ~0xFF00;
797 drive->id->dma_mword &= ~0x0F00;
798 drive->id->dma_1word &= ~0x0F00;
801 #ifdef CONFIG_BLK_DEV_IDEDMA
802 if ((speed >= XFER_SW_DMA_0 || (hwif->host_flags & IDE_HFLAG_VDMA)) &&
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);
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;
826 if (!drive->init_speed)
827 drive->init_speed = speed;
828 drive->current_speed = speed;
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).
839 * See also ide_execute_command
841 static void __ide_set_handler (ide_drive_t *drive, ide_handler_t *handler,
842 unsigned int timeout, ide_expiry_t *expiry)
844 ide_hwgroup_t *hwgroup = HWGROUP(drive);
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);
854 void ide_set_handler (ide_drive_t *drive, ide_handler_t *handler,
855 unsigned int timeout, ide_expiry_t *expiry)
858 spin_lock_irqsave(&ide_lock, flags);
859 __ide_set_handler(drive, handler, timeout, expiry);
860 spin_unlock_irqrestore(&ide_lock, flags);
863 EXPORT_SYMBOL(ide_set_handler);
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
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.
879 void ide_execute_command(ide_drive_t *drive, u8 cmd, ide_handler_t *handler,
880 unsigned timeout, ide_expiry_t *expiry)
883 ide_hwif_t *hwif = HWIF(drive);
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);
889 * Drive takes 400nS to respond, we must avoid the IRQ being
890 * serviced before that.
892 * FIXME: we could skip this delay with care on non shared devices
895 spin_unlock_irqrestore(&ide_lock, flags);
897 EXPORT_SYMBOL(ide_execute_command);
899 void ide_execute_pkt_cmd(ide_drive_t *drive)
901 ide_hwif_t *hwif = drive->hwif;
904 spin_lock_irqsave(&ide_lock, flags);
905 hwif->OUTBSYNC(drive, WIN_PACKETCMD, hwif->io_ports.command_addr);
907 spin_unlock_irqrestore(&ide_lock, flags);
909 EXPORT_SYMBOL_GPL(ide_execute_pkt_cmd);
912 static ide_startstop_t do_reset1 (ide_drive_t *, int);
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
920 static ide_startstop_t atapi_reset_pollfunc (ide_drive_t *drive)
922 ide_hwgroup_t *hwgroup = HWGROUP(drive);
927 stat = ide_read_status(drive);
929 if (OK_STAT(stat, 0, BUSY_STAT))
930 printk("%s: ATAPI reset complete\n", drive->name);
932 if (time_before(jiffies, hwgroup->poll_timeout)) {
933 ide_set_handler(drive, &atapi_reset_pollfunc, HZ/20, NULL);
934 /* continue polling */
938 hwgroup->polling = 0;
939 printk("%s: ATAPI reset timed-out, status=0x%02x\n",
941 /* do it the old fashioned way */
942 return do_reset1(drive, 1);
945 hwgroup->polling = 0;
946 hwgroup->resetting = 0;
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
956 static ide_startstop_t reset_pollfunc (ide_drive_t *drive)
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;
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);
971 tmp = ide_read_status(drive);
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 */
979 printk("%s: reset timed-out, status=0x%02x\n", hwif->name, tmp);
982 printk("%s: reset: ", hwif->name);
983 tmp = ide_read_error(drive);
991 switch (tmp & 0x7f) {
992 case 1: printk("passed");
994 case 2: printk("formatter device error");
996 case 3: printk("sector buffer error");
998 case 4: printk("ECC circuitry error");
1000 case 5: printk("controlling MPU error");
1002 default:printk("error (0x%02x?)", tmp);
1005 printk("; slave: failed");
1009 hwgroup->polling = 0; /* done polling */
1010 hwgroup->resetting = 0; /* done reset attempt */
1014 static void ide_disk_pre_reset(ide_drive_t *drive)
1016 int legacy = (drive->id->cfs_enable_2 & 0x0400) ? 0 : 1;
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;
1028 static void pre_reset(ide_drive_t *drive)
1030 const struct ide_port_ops *port_ops = drive->hwif->port_ops;
1032 if (drive->media == ide_disk)
1033 ide_disk_pre_reset(drive);
1035 drive->post_reset = 1;
1037 if (drive->using_dma) {
1038 if (drive->crc_count)
1039 ide_check_dma_crc(drive);
1044 if (!drive->keep_settings) {
1045 if (!drive->using_dma) {
1047 drive->io_32bit = 0;
1052 if (port_ops && port_ops->pre_reset)
1053 port_ops->pre_reset(drive);
1055 if (drive->current_speed != 0xff)
1056 drive->desired_speed = drive->current_speed;
1057 drive->current_speed = 0xff;
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.
1066 * ATAPI devices have their own reset mechanism which allows them to be
1067 * individually reset without clobbering other devices on the same interface.
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.
1075 static ide_startstop_t do_reset1 (ide_drive_t *drive, int do_not_try_atapi)
1078 unsigned long flags;
1080 ide_hwgroup_t *hwgroup;
1081 struct ide_io_ports *io_ports;
1082 const struct ide_port_ops *port_ops;
1085 spin_lock_irqsave(&ide_lock, flags);
1087 hwgroup = HWGROUP(drive);
1089 io_ports = &hwif->io_ports;
1091 /* We must not reset with running handlers */
1092 BUG_ON(hwgroup->handler != NULL);
1094 /* For an ATAPI device, first try an ATAPI SRST. */
1095 if (drive->media != ide_disk && !do_not_try_atapi) {
1096 hwgroup->resetting = 1;
1098 SELECT_DRIVE(drive);
1100 hwif->OUTBSYNC(drive, WIN_SRST, io_ports->command_addr);
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);
1110 * First, reset any device state data we were maintaining
1111 * for any of the drives on this interface.
1113 for (unit = 0; unit < MAX_DRIVES; ++unit)
1114 pre_reset(&hwif->drives[unit]);
1116 if (io_ports->ctl_addr == 0) {
1117 spin_unlock_irqrestore(&ide_lock, flags);
1121 hwgroup->resetting = 1;
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.
1130 /* set SRST and nIEN */
1131 hwif->OUTBSYNC(drive, drive->ctl|6, io_ports->ctl_addr);
1132 /* more than enough time */
1134 if (drive->quirk_list == 2)
1135 ctl = drive->ctl; /* clear SRST and nIEN */
1137 ctl = drive->ctl | 2; /* clear SRST, leave nIEN */
1138 hwif->OUTBSYNC(drive, ctl, io_ports->ctl_addr);
1139 /* more than enough time */
1141 hwgroup->poll_timeout = jiffies + WAIT_WORSTCASE;
1142 hwgroup->polling = 1;
1143 __ide_set_handler(drive, &reset_pollfunc, HZ/20, NULL);
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
1150 port_ops = hwif->port_ops;
1151 if (port_ops && port_ops->resetproc)
1152 port_ops->resetproc(drive);
1154 spin_unlock_irqrestore(&ide_lock, flags);
1159 * ide_do_reset() is the entry point to the drive/interface reset code.
1162 ide_startstop_t ide_do_reset (ide_drive_t *drive)
1164 return do_reset1(drive, 0);
1167 EXPORT_SYMBOL(ide_do_reset);
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().
1173 int ide_wait_not_busy(ide_hwif_t *hwif, unsigned long timeout)
1179 * Turn this into a schedule() sleep once I'm sure
1180 * about locking issues (2.5 work ?).
1183 stat = hwif->INB(hwif->io_ports.status_addr);
1184 if ((stat & BUSY_STAT) == 0)
1187 * Assume a value of 0xff means nothing is connected to
1188 * the interface and it doesn't implement the pull-down
1193 touch_softlockup_watchdog();
1194 touch_nmi_watchdog();
1199 EXPORT_SYMBOL_GPL(ide_wait_not_busy);