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_hwif_t *hwif, 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_hwif_t *hwif, 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 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_exec_command(ide_hwif_t *hwif, u8 cmd)
108 if (hwif->host_flags & IDE_HFLAG_MMIO)
109 writeb(cmd, (void __iomem *)hwif->io_ports.command_addr);
111 outb(cmd, hwif->io_ports.command_addr);
114 static u8 ide_read_status(ide_hwif_t *hwif)
116 if (hwif->host_flags & IDE_HFLAG_MMIO)
117 return readb((void __iomem *)hwif->io_ports.status_addr);
119 return inb(hwif->io_ports.status_addr);
122 static u8 ide_read_altstatus(ide_hwif_t *hwif)
124 if (hwif->host_flags & IDE_HFLAG_MMIO)
125 return readb((void __iomem *)hwif->io_ports.ctl_addr);
127 return inb(hwif->io_ports.ctl_addr);
130 static u8 ide_read_sff_dma_status(ide_hwif_t *hwif)
132 if (hwif->host_flags & IDE_HFLAG_MMIO)
133 return readb((void __iomem *)(hwif->dma_base + ATA_DMA_STATUS));
135 return inb(hwif->dma_base + ATA_DMA_STATUS);
138 static void ide_set_irq(ide_hwif_t *hwif, int on)
140 u8 ctl = ATA_DEVCTL_OBS;
142 if (on == 4) { /* hack for SRST */
149 if (hwif->host_flags & IDE_HFLAG_MMIO)
150 writeb(ctl, (void __iomem *)hwif->io_ports.ctl_addr);
152 outb(ctl, hwif->io_ports.ctl_addr);
155 static void ide_tf_load(ide_drive_t *drive, ide_task_t *task)
157 ide_hwif_t *hwif = drive->hwif;
158 struct ide_io_ports *io_ports = &hwif->io_ports;
159 struct ide_taskfile *tf = &task->tf;
160 void (*tf_outb)(u8 addr, unsigned long port);
161 u8 mmio = (hwif->host_flags & IDE_HFLAG_MMIO) ? 1 : 0;
162 u8 HIHI = (task->tf_flags & IDE_TFLAG_LBA48) ? 0xE0 : 0xEF;
165 tf_outb = ide_mm_outb;
169 if (task->tf_flags & IDE_TFLAG_FLAGGED)
172 if (task->tf_flags & IDE_TFLAG_OUT_DATA) {
173 u16 data = (tf->hob_data << 8) | tf->data;
176 writew(data, (void __iomem *)io_ports->data_addr);
178 outw(data, io_ports->data_addr);
181 if (task->tf_flags & IDE_TFLAG_OUT_HOB_FEATURE)
182 tf_outb(tf->hob_feature, io_ports->feature_addr);
183 if (task->tf_flags & IDE_TFLAG_OUT_HOB_NSECT)
184 tf_outb(tf->hob_nsect, io_ports->nsect_addr);
185 if (task->tf_flags & IDE_TFLAG_OUT_HOB_LBAL)
186 tf_outb(tf->hob_lbal, io_ports->lbal_addr);
187 if (task->tf_flags & IDE_TFLAG_OUT_HOB_LBAM)
188 tf_outb(tf->hob_lbam, io_ports->lbam_addr);
189 if (task->tf_flags & IDE_TFLAG_OUT_HOB_LBAH)
190 tf_outb(tf->hob_lbah, io_ports->lbah_addr);
192 if (task->tf_flags & IDE_TFLAG_OUT_FEATURE)
193 tf_outb(tf->feature, io_ports->feature_addr);
194 if (task->tf_flags & IDE_TFLAG_OUT_NSECT)
195 tf_outb(tf->nsect, io_ports->nsect_addr);
196 if (task->tf_flags & IDE_TFLAG_OUT_LBAL)
197 tf_outb(tf->lbal, io_ports->lbal_addr);
198 if (task->tf_flags & IDE_TFLAG_OUT_LBAM)
199 tf_outb(tf->lbam, io_ports->lbam_addr);
200 if (task->tf_flags & IDE_TFLAG_OUT_LBAH)
201 tf_outb(tf->lbah, io_ports->lbah_addr);
203 if (task->tf_flags & IDE_TFLAG_OUT_DEVICE)
204 tf_outb((tf->device & HIHI) | drive->select.all,
205 io_ports->device_addr);
208 static void ide_tf_read(ide_drive_t *drive, ide_task_t *task)
210 ide_hwif_t *hwif = drive->hwif;
211 struct ide_io_ports *io_ports = &hwif->io_ports;
212 struct ide_taskfile *tf = &task->tf;
213 void (*tf_outb)(u8 addr, unsigned long port);
214 u8 (*tf_inb)(unsigned long port);
215 u8 mmio = (hwif->host_flags & IDE_HFLAG_MMIO) ? 1 : 0;
218 tf_outb = ide_mm_outb;
225 if (task->tf_flags & IDE_TFLAG_IN_DATA) {
229 data = readw((void __iomem *)io_ports->data_addr);
231 data = inw(io_ports->data_addr);
233 tf->data = data & 0xff;
234 tf->hob_data = (data >> 8) & 0xff;
237 /* be sure we're looking at the low order bits */
238 tf_outb(ATA_DEVCTL_OBS & ~0x80, io_ports->ctl_addr);
240 if (task->tf_flags & IDE_TFLAG_IN_NSECT)
241 tf->nsect = tf_inb(io_ports->nsect_addr);
242 if (task->tf_flags & IDE_TFLAG_IN_LBAL)
243 tf->lbal = tf_inb(io_ports->lbal_addr);
244 if (task->tf_flags & IDE_TFLAG_IN_LBAM)
245 tf->lbam = tf_inb(io_ports->lbam_addr);
246 if (task->tf_flags & IDE_TFLAG_IN_LBAH)
247 tf->lbah = tf_inb(io_ports->lbah_addr);
248 if (task->tf_flags & IDE_TFLAG_IN_DEVICE)
249 tf->device = tf_inb(io_ports->device_addr);
251 if (task->tf_flags & IDE_TFLAG_LBA48) {
252 tf_outb(ATA_DEVCTL_OBS | 0x80, io_ports->ctl_addr);
254 if (task->tf_flags & IDE_TFLAG_IN_HOB_FEATURE)
255 tf->hob_feature = tf_inb(io_ports->feature_addr);
256 if (task->tf_flags & IDE_TFLAG_IN_HOB_NSECT)
257 tf->hob_nsect = tf_inb(io_ports->nsect_addr);
258 if (task->tf_flags & IDE_TFLAG_IN_HOB_LBAL)
259 tf->hob_lbal = tf_inb(io_ports->lbal_addr);
260 if (task->tf_flags & IDE_TFLAG_IN_HOB_LBAM)
261 tf->hob_lbam = tf_inb(io_ports->lbam_addr);
262 if (task->tf_flags & IDE_TFLAG_IN_HOB_LBAH)
263 tf->hob_lbah = tf_inb(io_ports->lbah_addr);
268 * Some localbus EIDE interfaces require a special access sequence
269 * when using 32-bit I/O instructions to transfer data. We call this
270 * the "vlb_sync" sequence, which consists of three successive reads
271 * of the sector count register location, with interrupts disabled
272 * to ensure that the reads all happen together.
274 static void ata_vlb_sync(unsigned long port)
282 * This is used for most PIO data transfers *from* the IDE interface
284 * These routines will round up any request for an odd number of bytes,
285 * so if an odd len is specified, be sure that there's at least one
286 * extra byte allocated for the buffer.
288 static void ata_input_data(ide_drive_t *drive, struct request *rq,
289 void *buf, unsigned int len)
291 ide_hwif_t *hwif = drive->hwif;
292 struct ide_io_ports *io_ports = &hwif->io_ports;
293 unsigned long data_addr = io_ports->data_addr;
294 u8 io_32bit = drive->io_32bit;
295 u8 mmio = (hwif->host_flags & IDE_HFLAG_MMIO) ? 1 : 0;
300 unsigned long uninitialized_var(flags);
302 if ((io_32bit & 2) && !mmio) {
303 local_irq_save(flags);
304 ata_vlb_sync(io_ports->nsect_addr);
308 __ide_mm_insl((void __iomem *)data_addr, buf, len / 4);
310 insl(data_addr, buf, len / 4);
312 if ((io_32bit & 2) && !mmio)
313 local_irq_restore(flags);
315 if ((len & 3) >= 2) {
317 __ide_mm_insw((void __iomem *)data_addr,
318 (u8 *)buf + (len & ~3), 1);
320 insw(data_addr, (u8 *)buf + (len & ~3), 1);
324 __ide_mm_insw((void __iomem *)data_addr, buf, len / 2);
326 insw(data_addr, buf, len / 2);
331 * This is used for most PIO data transfers *to* the IDE interface
333 static void ata_output_data(ide_drive_t *drive, struct request *rq,
334 void *buf, unsigned int len)
336 ide_hwif_t *hwif = drive->hwif;
337 struct ide_io_ports *io_ports = &hwif->io_ports;
338 unsigned long data_addr = io_ports->data_addr;
339 u8 io_32bit = drive->io_32bit;
340 u8 mmio = (hwif->host_flags & IDE_HFLAG_MMIO) ? 1 : 0;
343 unsigned long uninitialized_var(flags);
345 if ((io_32bit & 2) && !mmio) {
346 local_irq_save(flags);
347 ata_vlb_sync(io_ports->nsect_addr);
351 __ide_mm_outsl((void __iomem *)data_addr, buf, len / 4);
353 outsl(data_addr, buf, len / 4);
355 if ((io_32bit & 2) && !mmio)
356 local_irq_restore(flags);
358 if ((len & 3) >= 2) {
360 __ide_mm_outsw((void __iomem *)data_addr,
361 (u8 *)buf + (len & ~3), 1);
363 outsw(data_addr, (u8 *)buf + (len & ~3), 1);
367 __ide_mm_outsw((void __iomem *)data_addr, buf, len / 2);
369 outsw(data_addr, buf, len / 2);
373 void default_hwif_transport(ide_hwif_t *hwif)
375 hwif->exec_command = ide_exec_command;
376 hwif->read_status = ide_read_status;
377 hwif->read_altstatus = ide_read_altstatus;
378 hwif->read_sff_dma_status = ide_read_sff_dma_status;
380 hwif->set_irq = ide_set_irq;
382 hwif->tf_load = ide_tf_load;
383 hwif->tf_read = ide_tf_read;
385 hwif->input_data = ata_input_data;
386 hwif->output_data = ata_output_data;
389 void ide_fix_driveid (struct hd_driveid *id)
391 #ifndef __LITTLE_ENDIAN
396 id->config = __le16_to_cpu(id->config);
397 id->cyls = __le16_to_cpu(id->cyls);
398 id->reserved2 = __le16_to_cpu(id->reserved2);
399 id->heads = __le16_to_cpu(id->heads);
400 id->track_bytes = __le16_to_cpu(id->track_bytes);
401 id->sector_bytes = __le16_to_cpu(id->sector_bytes);
402 id->sectors = __le16_to_cpu(id->sectors);
403 id->vendor0 = __le16_to_cpu(id->vendor0);
404 id->vendor1 = __le16_to_cpu(id->vendor1);
405 id->vendor2 = __le16_to_cpu(id->vendor2);
406 stringcast = (u16 *)&id->serial_no[0];
407 for (i = 0; i < (20/2); i++)
408 stringcast[i] = __le16_to_cpu(stringcast[i]);
409 id->buf_type = __le16_to_cpu(id->buf_type);
410 id->buf_size = __le16_to_cpu(id->buf_size);
411 id->ecc_bytes = __le16_to_cpu(id->ecc_bytes);
412 stringcast = (u16 *)&id->fw_rev[0];
413 for (i = 0; i < (8/2); i++)
414 stringcast[i] = __le16_to_cpu(stringcast[i]);
415 stringcast = (u16 *)&id->model[0];
416 for (i = 0; i < (40/2); i++)
417 stringcast[i] = __le16_to_cpu(stringcast[i]);
418 id->dword_io = __le16_to_cpu(id->dword_io);
419 id->reserved50 = __le16_to_cpu(id->reserved50);
420 id->field_valid = __le16_to_cpu(id->field_valid);
421 id->cur_cyls = __le16_to_cpu(id->cur_cyls);
422 id->cur_heads = __le16_to_cpu(id->cur_heads);
423 id->cur_sectors = __le16_to_cpu(id->cur_sectors);
424 id->cur_capacity0 = __le16_to_cpu(id->cur_capacity0);
425 id->cur_capacity1 = __le16_to_cpu(id->cur_capacity1);
426 id->lba_capacity = __le32_to_cpu(id->lba_capacity);
427 id->dma_1word = __le16_to_cpu(id->dma_1word);
428 id->dma_mword = __le16_to_cpu(id->dma_mword);
429 id->eide_pio_modes = __le16_to_cpu(id->eide_pio_modes);
430 id->eide_dma_min = __le16_to_cpu(id->eide_dma_min);
431 id->eide_dma_time = __le16_to_cpu(id->eide_dma_time);
432 id->eide_pio = __le16_to_cpu(id->eide_pio);
433 id->eide_pio_iordy = __le16_to_cpu(id->eide_pio_iordy);
434 for (i = 0; i < 2; ++i)
435 id->words69_70[i] = __le16_to_cpu(id->words69_70[i]);
436 for (i = 0; i < 4; ++i)
437 id->words71_74[i] = __le16_to_cpu(id->words71_74[i]);
438 id->queue_depth = __le16_to_cpu(id->queue_depth);
439 for (i = 0; i < 4; ++i)
440 id->words76_79[i] = __le16_to_cpu(id->words76_79[i]);
441 id->major_rev_num = __le16_to_cpu(id->major_rev_num);
442 id->minor_rev_num = __le16_to_cpu(id->minor_rev_num);
443 id->command_set_1 = __le16_to_cpu(id->command_set_1);
444 id->command_set_2 = __le16_to_cpu(id->command_set_2);
445 id->cfsse = __le16_to_cpu(id->cfsse);
446 id->cfs_enable_1 = __le16_to_cpu(id->cfs_enable_1);
447 id->cfs_enable_2 = __le16_to_cpu(id->cfs_enable_2);
448 id->csf_default = __le16_to_cpu(id->csf_default);
449 id->dma_ultra = __le16_to_cpu(id->dma_ultra);
450 id->trseuc = __le16_to_cpu(id->trseuc);
451 id->trsEuc = __le16_to_cpu(id->trsEuc);
452 id->CurAPMvalues = __le16_to_cpu(id->CurAPMvalues);
453 id->mprc = __le16_to_cpu(id->mprc);
454 id->hw_config = __le16_to_cpu(id->hw_config);
455 id->acoustic = __le16_to_cpu(id->acoustic);
456 id->msrqs = __le16_to_cpu(id->msrqs);
457 id->sxfert = __le16_to_cpu(id->sxfert);
458 id->sal = __le16_to_cpu(id->sal);
459 id->spg = __le32_to_cpu(id->spg);
460 id->lba_capacity_2 = __le64_to_cpu(id->lba_capacity_2);
461 for (i = 0; i < 22; i++)
462 id->words104_125[i] = __le16_to_cpu(id->words104_125[i]);
463 id->last_lun = __le16_to_cpu(id->last_lun);
464 id->word127 = __le16_to_cpu(id->word127);
465 id->dlf = __le16_to_cpu(id->dlf);
466 id->csfo = __le16_to_cpu(id->csfo);
467 for (i = 0; i < 26; i++)
468 id->words130_155[i] = __le16_to_cpu(id->words130_155[i]);
469 id->word156 = __le16_to_cpu(id->word156);
470 for (i = 0; i < 3; i++)
471 id->words157_159[i] = __le16_to_cpu(id->words157_159[i]);
472 id->cfa_power = __le16_to_cpu(id->cfa_power);
473 for (i = 0; i < 14; i++)
474 id->words161_175[i] = __le16_to_cpu(id->words161_175[i]);
475 for (i = 0; i < 31; i++)
476 id->words176_205[i] = __le16_to_cpu(id->words176_205[i]);
477 for (i = 0; i < 48; i++)
478 id->words206_254[i] = __le16_to_cpu(id->words206_254[i]);
479 id->integrity_word = __le16_to_cpu(id->integrity_word);
481 # error "Please fix <asm/byteorder.h>"
487 * ide_fixstring() cleans up and (optionally) byte-swaps a text string,
488 * removing leading/trailing blanks and compressing internal blanks.
489 * It is primarily used to tidy up the model name/number fields as
490 * returned by the WIN_[P]IDENTIFY commands.
493 void ide_fixstring (u8 *s, const int bytecount, const int byteswap)
495 u8 *p = s, *end = &s[bytecount & ~1]; /* bytecount must be even */
498 /* convert from big-endian to host byte order */
499 for (p = end ; p != s;) {
500 unsigned short *pp = (unsigned short *) (p -= 2);
504 /* strip leading blanks */
505 while (s != end && *s == ' ')
507 /* compress internal blanks and strip trailing blanks */
508 while (s != end && *s) {
509 if (*s++ != ' ' || (s != end && *s && *s != ' '))
512 /* wipe out trailing garbage */
517 EXPORT_SYMBOL(ide_fixstring);
520 * Needed for PCI irq sharing
522 int drive_is_ready (ide_drive_t *drive)
524 ide_hwif_t *hwif = HWIF(drive);
527 if (drive->waiting_for_dma)
528 return hwif->dma_ops->dma_test_irq(drive);
531 /* need to guarantee 400ns since last command was issued */
536 * We do a passive status test under shared PCI interrupts on
537 * cards that truly share the ATA side interrupt, but may also share
538 * an interrupt with another pci card/device. We make no assumptions
539 * about possible isa-pnp and pci-pnp issues yet.
541 if (hwif->io_ports.ctl_addr)
542 stat = hwif->read_altstatus(hwif);
544 /* Note: this may clear a pending IRQ!! */
545 stat = hwif->read_status(hwif);
547 if (stat & BUSY_STAT)
548 /* drive busy: definitely not interrupting */
551 /* drive ready: *might* be interrupting */
555 EXPORT_SYMBOL(drive_is_ready);
558 * This routine busy-waits for the drive status to be not "busy".
559 * It then checks the status for all of the "good" bits and none
560 * of the "bad" bits, and if all is okay it returns 0. All other
561 * cases return error -- caller may then invoke ide_error().
563 * This routine should get fixed to not hog the cpu during extra long waits..
564 * That could be done by busy-waiting for the first jiffy or two, and then
565 * setting a timer to wake up at half second intervals thereafter,
566 * until timeout is achieved, before timing out.
568 static int __ide_wait_stat(ide_drive_t *drive, u8 good, u8 bad, unsigned long timeout, u8 *rstat)
570 ide_hwif_t *hwif = drive->hwif;
575 udelay(1); /* spec allows drive 400ns to assert "BUSY" */
576 stat = hwif->read_status(hwif);
578 if (stat & BUSY_STAT) {
579 local_irq_set(flags);
581 while ((stat = hwif->read_status(hwif)) & BUSY_STAT) {
582 if (time_after(jiffies, timeout)) {
584 * One last read after the timeout in case
585 * heavy interrupt load made us not make any
586 * progress during the timeout..
588 stat = hwif->read_status(hwif);
589 if (!(stat & BUSY_STAT))
592 local_irq_restore(flags);
597 local_irq_restore(flags);
600 * Allow status to settle, then read it again.
601 * A few rare drives vastly violate the 400ns spec here,
602 * so we'll wait up to 10usec for a "good" status
603 * rather than expensively fail things immediately.
604 * This fix courtesy of Matthew Faupel & Niccolo Rigacci.
606 for (i = 0; i < 10; i++) {
608 stat = hwif->read_status(hwif);
610 if (OK_STAT(stat, good, bad)) {
620 * In case of error returns error value after doing "*startstop = ide_error()".
621 * The caller should return the updated value of "startstop" in this case,
622 * "startstop" is unchanged when the function returns 0.
624 int ide_wait_stat(ide_startstop_t *startstop, ide_drive_t *drive, u8 good, u8 bad, unsigned long timeout)
629 /* bail early if we've exceeded max_failures */
630 if (drive->max_failures && (drive->failures > drive->max_failures)) {
631 *startstop = ide_stopped;
635 err = __ide_wait_stat(drive, good, bad, timeout, &stat);
638 char *s = (err == -EBUSY) ? "status timeout" : "status error";
639 *startstop = ide_error(drive, s, stat);
645 EXPORT_SYMBOL(ide_wait_stat);
648 * ide_in_drive_list - look for drive in black/white list
649 * @id: drive identifier
650 * @drive_table: list to inspect
652 * Look for a drive in the blacklist and the whitelist tables
653 * Returns 1 if the drive is found in the table.
656 int ide_in_drive_list(struct hd_driveid *id, const struct drive_list_entry *drive_table)
658 for ( ; drive_table->id_model; drive_table++)
659 if ((!strcmp(drive_table->id_model, id->model)) &&
660 (!drive_table->id_firmware ||
661 strstr(id->fw_rev, drive_table->id_firmware)))
666 EXPORT_SYMBOL_GPL(ide_in_drive_list);
669 * Early UDMA66 devices don't set bit14 to 1, only bit13 is valid.
670 * We list them here and depend on the device side cable detection for them.
672 * Some optical devices with the buggy firmwares have the same problem.
674 static const struct drive_list_entry ivb_list[] = {
675 { "QUANTUM FIREBALLlct10 05" , "A03.0900" },
676 { "TSSTcorp CDDVDW SH-S202J" , "SB00" },
677 { "TSSTcorp CDDVDW SH-S202J" , "SB01" },
678 { "TSSTcorp CDDVDW SH-S202N" , "SB00" },
679 { "TSSTcorp CDDVDW SH-S202N" , "SB01" },
680 { "TSSTcorp CDDVDW SH-S202H" , "SB00" },
681 { "TSSTcorp CDDVDW SH-S202H" , "SB01" },
686 * All hosts that use the 80c ribbon must use!
687 * The name is derived from upper byte of word 93 and the 80c ribbon.
689 u8 eighty_ninty_three (ide_drive_t *drive)
691 ide_hwif_t *hwif = drive->hwif;
692 struct hd_driveid *id = drive->id;
693 int ivb = ide_in_drive_list(id, ivb_list);
695 if (hwif->cbl == ATA_CBL_PATA40_SHORT)
699 printk(KERN_DEBUG "%s: skipping word 93 validity check\n",
702 if (ide_dev_is_sata(id) && !ivb)
705 if (hwif->cbl != ATA_CBL_PATA80 && !ivb)
710 * - change master/slave IDENTIFY order
711 * - force bit13 (80c cable present) check also for !ivb devices
712 * (unless the slave device is pre-ATA3)
714 if ((id->hw_config & 0x4000) || (ivb && (id->hw_config & 0x2000)))
718 if (drive->udma33_warned == 1)
721 printk(KERN_WARNING "%s: %s side 80-wire cable detection failed, "
722 "limiting max speed to UDMA33\n",
724 hwif->cbl == ATA_CBL_PATA80 ? "drive" : "host");
726 drive->udma33_warned = 1;
731 int ide_driveid_update(ide_drive_t *drive)
733 ide_hwif_t *hwif = drive->hwif;
734 struct hd_driveid *id;
735 unsigned long timeout, flags;
739 * Re-read drive->id for possible DMA mode
740 * change (copied from ide-probe.c)
743 SELECT_MASK(drive, 1);
744 hwif->set_irq(hwif, 0);
746 hwif->exec_command(hwif, WIN_IDENTIFY);
747 timeout = jiffies + WAIT_WORSTCASE;
749 if (time_after(jiffies, timeout)) {
750 SELECT_MASK(drive, 0);
751 return 0; /* drive timed-out */
754 msleep(50); /* give drive a breather */
755 stat = hwif->read_altstatus(hwif);
756 } while (stat & BUSY_STAT);
758 msleep(50); /* wait for IRQ and DRQ_STAT */
759 stat = hwif->read_status(hwif);
761 if (!OK_STAT(stat, DRQ_STAT, BAD_R_STAT)) {
762 SELECT_MASK(drive, 0);
763 printk("%s: CHECK for good STATUS\n", drive->name);
766 local_irq_save(flags);
767 SELECT_MASK(drive, 0);
768 id = kmalloc(SECTOR_WORDS*4, GFP_ATOMIC);
770 local_irq_restore(flags);
773 hwif->input_data(drive, NULL, id, SECTOR_SIZE);
774 (void)hwif->read_status(hwif); /* clear drive IRQ */
776 local_irq_restore(flags);
779 drive->id->dma_ultra = id->dma_ultra;
780 drive->id->dma_mword = id->dma_mword;
781 drive->id->dma_1word = id->dma_1word;
782 /* anything more ? */
785 if (drive->using_dma && ide_id_dma_bug(drive))
792 int ide_config_drive_speed(ide_drive_t *drive, u8 speed)
794 ide_hwif_t *hwif = drive->hwif;
795 struct ide_io_ports *io_ports = &hwif->io_ports;
799 #ifdef CONFIG_BLK_DEV_IDEDMA
800 if (hwif->dma_ops) /* check if host supports DMA */
801 hwif->dma_ops->dma_host_set(drive, 0);
804 /* Skip setting PIO flow-control modes on pre-EIDE drives */
805 if ((speed & 0xf8) == XFER_PIO_0 && !(drive->id->capability & 0x08))
809 * Don't use ide_wait_cmd here - it will
810 * attempt to set_geometry and recalibrate,
811 * but for some reason these don't work at
812 * this point (lost interrupt).
815 * Select the drive, and issue the SETFEATURES command
817 disable_irq_nosync(hwif->irq);
820 * FIXME: we race against the running IRQ here if
821 * this is called from non IRQ context. If we use
822 * disable_irq() we hang on the error path. Work
828 SELECT_MASK(drive, 0);
830 hwif->set_irq(hwif, 0);
831 hwif->OUTB(SETFEATURES_XFER, io_ports->feature_addr);
832 hwif->OUTB(speed, io_ports->nsect_addr);
833 hwif->exec_command(hwif, WIN_SETFEATURES);
834 if (drive->quirk_list == 2)
835 hwif->set_irq(hwif, 1);
837 error = __ide_wait_stat(drive, drive->ready_stat,
838 BUSY_STAT|DRQ_STAT|ERR_STAT,
841 SELECT_MASK(drive, 0);
843 enable_irq(hwif->irq);
846 (void) ide_dump_status(drive, "set_drive_speed_status", stat);
850 drive->id->dma_ultra &= ~0xFF00;
851 drive->id->dma_mword &= ~0x0F00;
852 drive->id->dma_1word &= ~0x0F00;
855 #ifdef CONFIG_BLK_DEV_IDEDMA
856 if ((speed >= XFER_SW_DMA_0 || (hwif->host_flags & IDE_HFLAG_VDMA)) &&
858 hwif->dma_ops->dma_host_set(drive, 1);
859 else if (hwif->dma_ops) /* check if host supports DMA */
860 ide_dma_off_quietly(drive);
864 case XFER_UDMA_7: drive->id->dma_ultra |= 0x8080; break;
865 case XFER_UDMA_6: drive->id->dma_ultra |= 0x4040; break;
866 case XFER_UDMA_5: drive->id->dma_ultra |= 0x2020; break;
867 case XFER_UDMA_4: drive->id->dma_ultra |= 0x1010; break;
868 case XFER_UDMA_3: drive->id->dma_ultra |= 0x0808; break;
869 case XFER_UDMA_2: drive->id->dma_ultra |= 0x0404; break;
870 case XFER_UDMA_1: drive->id->dma_ultra |= 0x0202; break;
871 case XFER_UDMA_0: drive->id->dma_ultra |= 0x0101; break;
872 case XFER_MW_DMA_2: drive->id->dma_mword |= 0x0404; break;
873 case XFER_MW_DMA_1: drive->id->dma_mword |= 0x0202; break;
874 case XFER_MW_DMA_0: drive->id->dma_mword |= 0x0101; break;
875 case XFER_SW_DMA_2: drive->id->dma_1word |= 0x0404; break;
876 case XFER_SW_DMA_1: drive->id->dma_1word |= 0x0202; break;
877 case XFER_SW_DMA_0: drive->id->dma_1word |= 0x0101; break;
880 if (!drive->init_speed)
881 drive->init_speed = speed;
882 drive->current_speed = speed;
887 * This should get invoked any time we exit the driver to
888 * wait for an interrupt response from a drive. handler() points
889 * at the appropriate code to handle the next interrupt, and a
890 * timer is started to prevent us from waiting forever in case
891 * something goes wrong (see the ide_timer_expiry() handler later on).
893 * See also ide_execute_command
895 static void __ide_set_handler (ide_drive_t *drive, ide_handler_t *handler,
896 unsigned int timeout, ide_expiry_t *expiry)
898 ide_hwgroup_t *hwgroup = HWGROUP(drive);
900 BUG_ON(hwgroup->handler);
901 hwgroup->handler = handler;
902 hwgroup->expiry = expiry;
903 hwgroup->timer.expires = jiffies + timeout;
904 hwgroup->req_gen_timer = hwgroup->req_gen;
905 add_timer(&hwgroup->timer);
908 void ide_set_handler (ide_drive_t *drive, ide_handler_t *handler,
909 unsigned int timeout, ide_expiry_t *expiry)
912 spin_lock_irqsave(&ide_lock, flags);
913 __ide_set_handler(drive, handler, timeout, expiry);
914 spin_unlock_irqrestore(&ide_lock, flags);
917 EXPORT_SYMBOL(ide_set_handler);
920 * ide_execute_command - execute an IDE command
921 * @drive: IDE drive to issue the command against
922 * @command: command byte to write
923 * @handler: handler for next phase
924 * @timeout: timeout for command
925 * @expiry: handler to run on timeout
927 * Helper function to issue an IDE command. This handles the
928 * atomicity requirements, command timing and ensures that the
929 * handler and IRQ setup do not race. All IDE command kick off
930 * should go via this function or do equivalent locking.
933 void ide_execute_command(ide_drive_t *drive, u8 cmd, ide_handler_t *handler,
934 unsigned timeout, ide_expiry_t *expiry)
937 ide_hwif_t *hwif = HWIF(drive);
939 spin_lock_irqsave(&ide_lock, flags);
940 __ide_set_handler(drive, handler, timeout, expiry);
941 hwif->exec_command(hwif, cmd);
943 * Drive takes 400nS to respond, we must avoid the IRQ being
944 * serviced before that.
946 * FIXME: we could skip this delay with care on non shared devices
949 spin_unlock_irqrestore(&ide_lock, flags);
951 EXPORT_SYMBOL(ide_execute_command);
953 void ide_execute_pkt_cmd(ide_drive_t *drive)
955 ide_hwif_t *hwif = drive->hwif;
958 spin_lock_irqsave(&ide_lock, flags);
959 hwif->exec_command(hwif, WIN_PACKETCMD);
961 spin_unlock_irqrestore(&ide_lock, flags);
963 EXPORT_SYMBOL_GPL(ide_execute_pkt_cmd);
965 static inline void ide_complete_drive_reset(ide_drive_t *drive, int err)
967 struct request *rq = drive->hwif->hwgroup->rq;
969 if (rq && blk_special_request(rq) && rq->cmd[0] == REQ_DRIVE_RESET)
970 ide_end_request(drive, err ? err : 1, 0);
974 static ide_startstop_t do_reset1 (ide_drive_t *, int);
977 * atapi_reset_pollfunc() gets invoked to poll the interface for completion every 50ms
978 * during an atapi drive reset operation. If the drive has not yet responded,
979 * and we have not yet hit our maximum waiting time, then the timer is restarted
982 static ide_startstop_t atapi_reset_pollfunc (ide_drive_t *drive)
984 ide_hwif_t *hwif = drive->hwif;
985 ide_hwgroup_t *hwgroup = hwif->hwgroup;
990 stat = hwif->read_status(hwif);
992 if (OK_STAT(stat, 0, BUSY_STAT))
993 printk("%s: ATAPI reset complete\n", drive->name);
995 if (time_before(jiffies, hwgroup->poll_timeout)) {
996 ide_set_handler(drive, &atapi_reset_pollfunc, HZ/20, NULL);
997 /* continue polling */
1000 /* end of polling */
1001 hwgroup->polling = 0;
1002 printk("%s: ATAPI reset timed-out, status=0x%02x\n",
1004 /* do it the old fashioned way */
1005 return do_reset1(drive, 1);
1008 hwgroup->polling = 0;
1009 ide_complete_drive_reset(drive, 0);
1014 * reset_pollfunc() gets invoked to poll the interface for completion every 50ms
1015 * during an ide reset operation. If the drives have not yet responded,
1016 * and we have not yet hit our maximum waiting time, then the timer is restarted
1019 static ide_startstop_t reset_pollfunc (ide_drive_t *drive)
1021 ide_hwgroup_t *hwgroup = HWGROUP(drive);
1022 ide_hwif_t *hwif = HWIF(drive);
1023 const struct ide_port_ops *port_ops = hwif->port_ops;
1027 if (port_ops && port_ops->reset_poll) {
1028 err = port_ops->reset_poll(drive);
1030 printk(KERN_ERR "%s: host reset_poll failure for %s.\n",
1031 hwif->name, drive->name);
1036 tmp = hwif->read_status(hwif);
1038 if (!OK_STAT(tmp, 0, BUSY_STAT)) {
1039 if (time_before(jiffies, hwgroup->poll_timeout)) {
1040 ide_set_handler(drive, &reset_pollfunc, HZ/20, NULL);
1041 /* continue polling */
1044 printk("%s: reset timed-out, status=0x%02x\n", hwif->name, tmp);
1048 printk("%s: reset: ", hwif->name);
1049 tmp = ide_read_error(drive);
1052 printk("success\n");
1053 drive->failures = 0;
1057 switch (tmp & 0x7f) {
1058 case 1: printk("passed");
1060 case 2: printk("formatter device error");
1062 case 3: printk("sector buffer error");
1064 case 4: printk("ECC circuitry error");
1066 case 5: printk("controlling MPU error");
1068 default:printk("error (0x%02x?)", tmp);
1071 printk("; slave: failed");
1077 hwgroup->polling = 0; /* done polling */
1078 ide_complete_drive_reset(drive, err);
1082 static void ide_disk_pre_reset(ide_drive_t *drive)
1084 int legacy = (drive->id->cfs_enable_2 & 0x0400) ? 0 : 1;
1086 drive->special.all = 0;
1087 drive->special.b.set_geometry = legacy;
1088 drive->special.b.recalibrate = legacy;
1089 drive->mult_count = 0;
1090 if (!drive->keep_settings && !drive->using_dma)
1091 drive->mult_req = 0;
1092 if (drive->mult_req != drive->mult_count)
1093 drive->special.b.set_multmode = 1;
1096 static void pre_reset(ide_drive_t *drive)
1098 const struct ide_port_ops *port_ops = drive->hwif->port_ops;
1100 if (drive->media == ide_disk)
1101 ide_disk_pre_reset(drive);
1103 drive->post_reset = 1;
1105 if (drive->using_dma) {
1106 if (drive->crc_count)
1107 ide_check_dma_crc(drive);
1112 if (!drive->keep_settings) {
1113 if (!drive->using_dma) {
1115 drive->io_32bit = 0;
1120 if (port_ops && port_ops->pre_reset)
1121 port_ops->pre_reset(drive);
1123 if (drive->current_speed != 0xff)
1124 drive->desired_speed = drive->current_speed;
1125 drive->current_speed = 0xff;
1129 * do_reset1() attempts to recover a confused drive by resetting it.
1130 * Unfortunately, resetting a disk drive actually resets all devices on
1131 * the same interface, so it can really be thought of as resetting the
1132 * interface rather than resetting the drive.
1134 * ATAPI devices have their own reset mechanism which allows them to be
1135 * individually reset without clobbering other devices on the same interface.
1137 * Unfortunately, the IDE interface does not generate an interrupt to let
1138 * us know when the reset operation has finished, so we must poll for this.
1139 * Equally poor, though, is the fact that this may a very long time to complete,
1140 * (up to 30 seconds worstcase). So, instead of busy-waiting here for it,
1141 * we set a timer to poll at 50ms intervals.
1143 static ide_startstop_t do_reset1 (ide_drive_t *drive, int do_not_try_atapi)
1146 unsigned long flags;
1148 ide_hwgroup_t *hwgroup;
1149 struct ide_io_ports *io_ports;
1150 const struct ide_port_ops *port_ops;
1152 spin_lock_irqsave(&ide_lock, flags);
1154 hwgroup = HWGROUP(drive);
1156 io_ports = &hwif->io_ports;
1158 /* We must not reset with running handlers */
1159 BUG_ON(hwgroup->handler != NULL);
1161 /* For an ATAPI device, first try an ATAPI SRST. */
1162 if (drive->media != ide_disk && !do_not_try_atapi) {
1164 SELECT_DRIVE(drive);
1166 hwif->exec_command(hwif, WIN_SRST);
1168 hwgroup->poll_timeout = jiffies + WAIT_WORSTCASE;
1169 hwgroup->polling = 1;
1170 __ide_set_handler(drive, &atapi_reset_pollfunc, HZ/20, NULL);
1171 spin_unlock_irqrestore(&ide_lock, flags);
1176 * First, reset any device state data we were maintaining
1177 * for any of the drives on this interface.
1179 for (unit = 0; unit < MAX_DRIVES; ++unit)
1180 pre_reset(&hwif->drives[unit]);
1182 if (io_ports->ctl_addr == 0) {
1183 spin_unlock_irqrestore(&ide_lock, flags);
1184 ide_complete_drive_reset(drive, -ENXIO);
1189 * Note that we also set nIEN while resetting the device,
1190 * to mask unwanted interrupts from the interface during the reset.
1191 * However, due to the design of PC hardware, this will cause an
1192 * immediate interrupt due to the edge transition it produces.
1193 * This single interrupt gives us a "fast poll" for drives that
1194 * recover from reset very quickly, saving us the first 50ms wait time.
1196 * TODO: add ->softreset method and stop abusing ->set_irq
1198 /* set SRST and nIEN */
1199 hwif->set_irq(hwif, 4);
1200 /* more than enough time */
1202 /* clear SRST, leave nIEN (unless device is on the quirk list) */
1203 hwif->set_irq(hwif, drive->quirk_list == 2);
1204 /* more than enough time */
1206 hwgroup->poll_timeout = jiffies + WAIT_WORSTCASE;
1207 hwgroup->polling = 1;
1208 __ide_set_handler(drive, &reset_pollfunc, HZ/20, NULL);
1211 * Some weird controller like resetting themselves to a strange
1212 * state when the disks are reset this way. At least, the Winbond
1213 * 553 documentation says that
1215 port_ops = hwif->port_ops;
1216 if (port_ops && port_ops->resetproc)
1217 port_ops->resetproc(drive);
1219 spin_unlock_irqrestore(&ide_lock, flags);
1224 * ide_do_reset() is the entry point to the drive/interface reset code.
1227 ide_startstop_t ide_do_reset (ide_drive_t *drive)
1229 return do_reset1(drive, 0);
1232 EXPORT_SYMBOL(ide_do_reset);
1235 * ide_wait_not_busy() waits for the currently selected device on the hwif
1236 * to report a non-busy status, see comments in ide_probe_port().
1238 int ide_wait_not_busy(ide_hwif_t *hwif, unsigned long timeout)
1244 * Turn this into a schedule() sleep once I'm sure
1245 * about locking issues (2.5 work ?).
1248 stat = hwif->read_status(hwif);
1249 if ((stat & BUSY_STAT) == 0)
1252 * Assume a value of 0xff means nothing is connected to
1253 * the interface and it doesn't implement the pull-down
1258 touch_softlockup_watchdog();
1259 touch_nmi_watchdog();
1264 EXPORT_SYMBOL_GPL(ide_wait_not_busy);