2 * linux/drivers/ide/ide-iops.c Version 0.37 Mar 05, 2003
4 * Copyright (C) 2000-2002 Andre Hedrick <andre@linux-ide.org>
5 * Copyright (C) 2003 Red Hat <alan@redhat.com>
10 #include <linux/config.h>
11 #define __NO_VERSION__
12 #include <linux/module.h>
13 #include <linux/types.h>
14 #include <linux/string.h>
15 #include <linux/kernel.h>
16 #include <linux/timer.h>
18 #include <linux/interrupt.h>
19 #include <linux/major.h>
20 #include <linux/errno.h>
21 #include <linux/genhd.h>
22 #include <linux/blkpg.h>
23 #include <linux/slab.h>
24 #include <linux/pci.h>
25 #include <linux/delay.h>
26 #include <linux/hdreg.h>
27 #include <linux/ide.h>
29 #include <asm/byteorder.h>
31 #include <asm/uaccess.h>
33 #include <asm/bitops.h>
36 * IDE operator we assign to an unplugged device so that
37 * we don't trash new hardware assigned the same resources
40 static u8 ide_unplugged_inb (unsigned long port)
45 static u16 ide_unplugged_inw (unsigned long port)
50 static void ide_unplugged_insw (unsigned long port, void *addr, u32 count)
54 static u32 ide_unplugged_inl (unsigned long port)
59 static void ide_unplugged_insl (unsigned long port, void *addr, u32 count)
63 static void ide_unplugged_outb (u8 addr, unsigned long port)
67 static void ide_unplugged_outbsync (ide_drive_t *drive, u8 addr, unsigned long port)
71 static void ide_unplugged_outw (u16 addr, unsigned long port)
75 static void ide_unplugged_outsw (unsigned long port, void *addr, u32 count)
79 static void ide_unplugged_outl (u32 addr, unsigned long port)
83 static void ide_unplugged_outsl (unsigned long port, void *addr, u32 count)
87 void unplugged_hwif_iops (ide_hwif_t *hwif)
89 hwif->OUTB = ide_unplugged_outb;
90 hwif->OUTBSYNC = ide_unplugged_outbsync;
91 hwif->OUTW = ide_unplugged_outw;
92 hwif->OUTL = ide_unplugged_outl;
93 hwif->OUTSW = ide_unplugged_outsw;
94 hwif->OUTSL = ide_unplugged_outsl;
95 hwif->INB = ide_unplugged_inb;
96 hwif->INW = ide_unplugged_inw;
97 hwif->INL = ide_unplugged_inl;
98 hwif->INSW = ide_unplugged_insw;
99 hwif->INSL = ide_unplugged_insl;
102 EXPORT_SYMBOL(unplugged_hwif_iops);
105 * Conventional PIO operations for ATA devices
108 static u8 ide_inb (unsigned long port)
110 return (u8) inb(port);
113 static u16 ide_inw (unsigned long port)
115 return (u16) inw(port);
118 static void ide_insw (unsigned long port, void *addr, u32 count)
120 return insw(port, addr, count);
123 static u32 ide_inl (unsigned long port)
125 return (u32) inl(port);
128 static void ide_insl (unsigned long port, void *addr, u32 count)
130 insl(port, addr, count);
133 static void ide_outb (u8 addr, unsigned long port)
138 static void ide_outbsync (ide_drive_t *drive, u8 addr, unsigned long port)
143 static void ide_outw (u16 addr, unsigned long port)
148 static void ide_outsw (unsigned long port, void *addr, u32 count)
150 outsw(port, addr, count);
153 static void ide_outl (u32 addr, unsigned long port)
158 static void ide_outsl (unsigned long port, void *addr, u32 count)
160 outsl(port, addr, count);
163 void default_hwif_iops (ide_hwif_t *hwif)
165 hwif->OUTB = ide_outb;
166 hwif->OUTBSYNC = ide_outbsync;
167 hwif->OUTW = ide_outw;
168 hwif->OUTL = ide_outl;
169 hwif->OUTSW = ide_outsw;
170 hwif->OUTSL = ide_outsl;
174 hwif->INSW = ide_insw;
175 hwif->INSL = ide_insl;
178 EXPORT_SYMBOL(default_hwif_iops);
181 * MMIO operations, typically used for SATA controllers
184 static u8 ide_mm_inb (unsigned long port)
186 return (u8) readb(port);
189 static u16 ide_mm_inw (unsigned long port)
191 return (u16) readw(port);
194 static void ide_mm_insw (unsigned long port, void *addr, u32 count)
196 __ide_mm_insw(port, addr, count);
199 static u32 ide_mm_inl (unsigned long port)
201 return (u32) readl(port);
204 static void ide_mm_insl (unsigned long port, void *addr, u32 count)
206 __ide_mm_insl(port, addr, count);
209 static void ide_mm_outb (u8 value, unsigned long port)
214 static void ide_mm_outbsync (ide_drive_t *drive, u8 value, unsigned long port)
219 static void ide_mm_outw (u16 value, unsigned long port)
224 static void ide_mm_outsw (unsigned long port, void *addr, u32 count)
226 __ide_mm_outsw(port, addr, count);
229 static void ide_mm_outl (u32 value, unsigned long port)
234 static void ide_mm_outsl (unsigned long port, void *addr, u32 count)
236 __ide_mm_outsl(port, addr, count);
239 void default_hwif_mmiops (ide_hwif_t *hwif)
241 hwif->OUTB = ide_mm_outb;
242 /* Most systems will need to override OUTBSYNC, alas however
243 this one is controller specific! */
244 hwif->OUTBSYNC = ide_mm_outbsync;
245 hwif->OUTW = ide_mm_outw;
246 hwif->OUTL = ide_mm_outl;
247 hwif->OUTSW = ide_mm_outsw;
248 hwif->OUTSL = ide_mm_outsl;
249 hwif->INB = ide_mm_inb;
250 hwif->INW = ide_mm_inw;
251 hwif->INL = ide_mm_inl;
252 hwif->INSW = ide_mm_insw;
253 hwif->INSL = ide_mm_insl;
256 EXPORT_SYMBOL(default_hwif_mmiops);
258 void default_hwif_transport (ide_hwif_t *hwif)
260 hwif->ata_input_data = ata_input_data;
261 hwif->ata_output_data = ata_output_data;
262 hwif->atapi_input_bytes = atapi_input_bytes;
263 hwif->atapi_output_bytes = atapi_output_bytes;
266 EXPORT_SYMBOL(default_hwif_transport);
268 u32 read_24 (ide_drive_t *drive)
270 u8 hcyl = HWIF(drive)->INB(IDE_HCYL_REG);
271 u8 lcyl = HWIF(drive)->INB(IDE_LCYL_REG);
272 u8 sect = HWIF(drive)->INB(IDE_SECTOR_REG);
273 return (hcyl<<16)|(lcyl<<8)|sect;
276 EXPORT_SYMBOL(read_24);
278 void SELECT_DRIVE (ide_drive_t *drive)
280 if (HWIF(drive)->selectproc)
281 HWIF(drive)->selectproc(drive);
282 HWIF(drive)->OUTB(drive->select.all, IDE_SELECT_REG);
285 EXPORT_SYMBOL(SELECT_DRIVE);
287 void SELECT_INTERRUPT (ide_drive_t *drive)
289 if (HWIF(drive)->intrproc)
290 HWIF(drive)->intrproc(drive);
292 HWIF(drive)->OUTB(drive->ctl|2, IDE_CONTROL_REG);
295 EXPORT_SYMBOL(SELECT_INTERRUPT);
297 void SELECT_MASK (ide_drive_t *drive, int mask)
299 if (HWIF(drive)->maskproc)
300 HWIF(drive)->maskproc(drive, mask);
303 EXPORT_SYMBOL(SELECT_MASK);
305 void QUIRK_LIST (ide_drive_t *drive)
307 if (HWIF(drive)->quirkproc)
308 drive->quirk_list = HWIF(drive)->quirkproc(drive);
311 EXPORT_SYMBOL(QUIRK_LIST);
314 * Some localbus EIDE interfaces require a special access sequence
315 * when using 32-bit I/O instructions to transfer data. We call this
316 * the "vlb_sync" sequence, which consists of three successive reads
317 * of the sector count register location, with interrupts disabled
318 * to ensure that the reads all happen together.
320 void ata_vlb_sync (ide_drive_t *drive, ide_ioreg_t port)
322 (void) HWIF(drive)->INB(port);
323 (void) HWIF(drive)->INB(port);
324 (void) HWIF(drive)->INB(port);
327 EXPORT_SYMBOL(ata_vlb_sync);
330 * This is used for most PIO data transfers *from* the IDE interface
332 void ata_input_data (ide_drive_t *drive, void *buffer, u32 wcount)
334 ide_hwif_t *hwif = HWIF(drive);
335 u8 io_32bit = drive->io_32bit;
340 local_irq_save(flags);
341 ata_vlb_sync(drive, IDE_NSECTOR_REG);
342 hwif->INSL(IDE_DATA_REG, buffer, wcount);
343 local_irq_restore(flags);
345 hwif->INSL(IDE_DATA_REG, buffer, wcount);
347 hwif->INSW(IDE_DATA_REG, buffer, wcount<<1);
351 EXPORT_SYMBOL(ata_input_data);
354 * This is used for most PIO data transfers *to* the IDE interface
356 void ata_output_data (ide_drive_t *drive, void *buffer, u32 wcount)
358 ide_hwif_t *hwif = HWIF(drive);
359 u8 io_32bit = drive->io_32bit;
364 local_irq_save(flags);
365 ata_vlb_sync(drive, IDE_NSECTOR_REG);
366 hwif->OUTSL(IDE_DATA_REG, buffer, wcount);
367 local_irq_restore(flags);
369 hwif->OUTSL(IDE_DATA_REG, buffer, wcount);
371 hwif->OUTSW(IDE_DATA_REG, buffer, wcount<<1);
375 EXPORT_SYMBOL(ata_output_data);
378 * The following routines are mainly used by the ATAPI drivers.
380 * These routines will round up any request for an odd number of bytes,
381 * so if an odd bytecount is specified, be sure that there's at least one
382 * extra byte allocated for the buffer.
385 void atapi_input_bytes (ide_drive_t *drive, void *buffer, u32 bytecount)
387 ide_hwif_t *hwif = HWIF(drive);
390 #if defined(CONFIG_ATARI) || defined(CONFIG_Q40)
391 if (MACH_IS_ATARI || MACH_IS_Q40) {
392 /* Atari has a byte-swapped IDE interface */
393 insw_swapw(IDE_DATA_REG, buffer, bytecount / 2);
396 #endif /* CONFIG_ATARI || CONFIG_Q40 */
397 hwif->ata_input_data(drive, buffer, bytecount / 4);
398 if ((bytecount & 0x03) >= 2)
399 hwif->INSW(IDE_DATA_REG, ((u8 *)buffer)+(bytecount & ~0x03), 1);
402 EXPORT_SYMBOL(atapi_input_bytes);
404 void atapi_output_bytes (ide_drive_t *drive, void *buffer, u32 bytecount)
406 ide_hwif_t *hwif = HWIF(drive);
409 #if defined(CONFIG_ATARI) || defined(CONFIG_Q40)
410 if (MACH_IS_ATARI || MACH_IS_Q40) {
411 /* Atari has a byte-swapped IDE interface */
412 outsw_swapw(IDE_DATA_REG, buffer, bytecount / 2);
415 #endif /* CONFIG_ATARI || CONFIG_Q40 */
416 hwif->ata_output_data(drive, buffer, bytecount / 4);
417 if ((bytecount & 0x03) >= 2)
418 hwif->OUTSW(IDE_DATA_REG, ((u8*)buffer)+(bytecount & ~0x03), 1);
421 EXPORT_SYMBOL(atapi_output_bytes);
424 * Beginning of Taskfile OPCODE Library and feature sets.
426 void ide_fix_driveid (struct hd_driveid *id)
428 #ifndef __LITTLE_ENDIAN
434 if (!MACH_IS_AMIGA && !MACH_IS_MAC && !MACH_IS_Q40 && !MACH_IS_ATARI)
437 #ifdef M68K_IDE_SWAPW
438 if (M68K_IDE_SWAPW) { /* fix bus byteorder first */
439 u_char *p = (u_char *)id;
441 for (i = 0; i < 512; i += 2) {
448 #endif /* __mc68000__ */
450 id->config = __le16_to_cpu(id->config);
451 id->cyls = __le16_to_cpu(id->cyls);
452 id->reserved2 = __le16_to_cpu(id->reserved2);
453 id->heads = __le16_to_cpu(id->heads);
454 id->track_bytes = __le16_to_cpu(id->track_bytes);
455 id->sector_bytes = __le16_to_cpu(id->sector_bytes);
456 id->sectors = __le16_to_cpu(id->sectors);
457 id->vendor0 = __le16_to_cpu(id->vendor0);
458 id->vendor1 = __le16_to_cpu(id->vendor1);
459 id->vendor2 = __le16_to_cpu(id->vendor2);
460 stringcast = (u16 *)&id->serial_no[0];
461 for (i = 0; i < (20/2); i++)
462 stringcast[i] = __le16_to_cpu(stringcast[i]);
463 id->buf_type = __le16_to_cpu(id->buf_type);
464 id->buf_size = __le16_to_cpu(id->buf_size);
465 id->ecc_bytes = __le16_to_cpu(id->ecc_bytes);
466 stringcast = (u16 *)&id->fw_rev[0];
467 for (i = 0; i < (8/2); i++)
468 stringcast[i] = __le16_to_cpu(stringcast[i]);
469 stringcast = (u16 *)&id->model[0];
470 for (i = 0; i < (40/2); i++)
471 stringcast[i] = __le16_to_cpu(stringcast[i]);
472 id->dword_io = __le16_to_cpu(id->dword_io);
473 id->reserved50 = __le16_to_cpu(id->reserved50);
474 id->field_valid = __le16_to_cpu(id->field_valid);
475 id->cur_cyls = __le16_to_cpu(id->cur_cyls);
476 id->cur_heads = __le16_to_cpu(id->cur_heads);
477 id->cur_sectors = __le16_to_cpu(id->cur_sectors);
478 id->cur_capacity0 = __le16_to_cpu(id->cur_capacity0);
479 id->cur_capacity1 = __le16_to_cpu(id->cur_capacity1);
480 id->lba_capacity = __le32_to_cpu(id->lba_capacity);
481 id->dma_1word = __le16_to_cpu(id->dma_1word);
482 id->dma_mword = __le16_to_cpu(id->dma_mword);
483 id->eide_pio_modes = __le16_to_cpu(id->eide_pio_modes);
484 id->eide_dma_min = __le16_to_cpu(id->eide_dma_min);
485 id->eide_dma_time = __le16_to_cpu(id->eide_dma_time);
486 id->eide_pio = __le16_to_cpu(id->eide_pio);
487 id->eide_pio_iordy = __le16_to_cpu(id->eide_pio_iordy);
488 for (i = 0; i < 2; ++i)
489 id->words69_70[i] = __le16_to_cpu(id->words69_70[i]);
490 for (i = 0; i < 4; ++i)
491 id->words71_74[i] = __le16_to_cpu(id->words71_74[i]);
492 id->queue_depth = __le16_to_cpu(id->queue_depth);
493 for (i = 0; i < 4; ++i)
494 id->words76_79[i] = __le16_to_cpu(id->words76_79[i]);
495 id->major_rev_num = __le16_to_cpu(id->major_rev_num);
496 id->minor_rev_num = __le16_to_cpu(id->minor_rev_num);
497 id->command_set_1 = __le16_to_cpu(id->command_set_1);
498 id->command_set_2 = __le16_to_cpu(id->command_set_2);
499 id->cfsse = __le16_to_cpu(id->cfsse);
500 id->cfs_enable_1 = __le16_to_cpu(id->cfs_enable_1);
501 id->cfs_enable_2 = __le16_to_cpu(id->cfs_enable_2);
502 id->csf_default = __le16_to_cpu(id->csf_default);
503 id->dma_ultra = __le16_to_cpu(id->dma_ultra);
504 id->trseuc = __le16_to_cpu(id->trseuc);
505 id->trsEuc = __le16_to_cpu(id->trsEuc);
506 id->CurAPMvalues = __le16_to_cpu(id->CurAPMvalues);
507 id->mprc = __le16_to_cpu(id->mprc);
508 id->hw_config = __le16_to_cpu(id->hw_config);
509 id->acoustic = __le16_to_cpu(id->acoustic);
510 id->msrqs = __le16_to_cpu(id->msrqs);
511 id->sxfert = __le16_to_cpu(id->sxfert);
512 id->sal = __le16_to_cpu(id->sal);
513 id->spg = __le32_to_cpu(id->spg);
514 id->lba_capacity_2 = __le64_to_cpu(id->lba_capacity_2);
515 for (i = 0; i < 22; i++)
516 id->words104_125[i] = __le16_to_cpu(id->words104_125[i]);
517 id->last_lun = __le16_to_cpu(id->last_lun);
518 id->word127 = __le16_to_cpu(id->word127);
519 id->dlf = __le16_to_cpu(id->dlf);
520 id->csfo = __le16_to_cpu(id->csfo);
521 for (i = 0; i < 26; i++)
522 id->words130_155[i] = __le16_to_cpu(id->words130_155[i]);
523 id->word156 = __le16_to_cpu(id->word156);
524 for (i = 0; i < 3; i++)
525 id->words157_159[i] = __le16_to_cpu(id->words157_159[i]);
526 id->cfa_power = __le16_to_cpu(id->cfa_power);
527 for (i = 0; i < 14; i++)
528 id->words161_175[i] = __le16_to_cpu(id->words161_175[i]);
529 for (i = 0; i < 31; i++)
530 id->words176_205[i] = __le16_to_cpu(id->words176_205[i]);
531 for (i = 0; i < 48; i++)
532 id->words206_254[i] = __le16_to_cpu(id->words206_254[i]);
533 id->integrity_word = __le16_to_cpu(id->integrity_word);
535 # error "Please fix <asm/byteorder.h>"
540 EXPORT_SYMBOL(ide_fix_driveid);
542 void ide_fixstring (u8 *s, const int bytecount, const int byteswap)
544 u8 *p = s, *end = &s[bytecount & ~1]; /* bytecount must be even */
547 /* convert from big-endian to host byte order */
548 for (p = end ; p != s;) {
549 unsigned short *pp = (unsigned short *) (p -= 2);
553 /* strip leading blanks */
554 while (s != end && *s == ' ')
556 /* compress internal blanks and strip trailing blanks */
557 while (s != end && *s) {
558 if (*s++ != ' ' || (s != end && *s && *s != ' '))
561 /* wipe out trailing garbage */
566 EXPORT_SYMBOL(ide_fixstring);
569 * Needed for PCI irq sharing
571 int drive_is_ready (ide_drive_t *drive)
573 ide_hwif_t *hwif = HWIF(drive);
576 if (drive->waiting_for_dma)
577 return hwif->ide_dma_test_irq(drive);
580 /* need to guarantee 400ns since last command was issued */
584 #ifdef CONFIG_IDEPCI_SHARE_IRQ
586 * We do a passive status test under shared PCI interrupts on
587 * cards that truly share the ATA side interrupt, but may also share
588 * an interrupt with another pci card/device. We make no assumptions
589 * about possible isa-pnp and pci-pnp issues yet.
592 stat = hwif->INB(IDE_ALTSTATUS_REG);
594 #endif /* CONFIG_IDEPCI_SHARE_IRQ */
595 /* Note: this may clear a pending IRQ!! */
596 stat = hwif->INB(IDE_STATUS_REG);
598 if (stat & BUSY_STAT)
599 /* drive busy: definitely not interrupting */
602 /* drive ready: *might* be interrupting */
606 EXPORT_SYMBOL(drive_is_ready);
609 * Global for All, and taken from ide-pmac.c. Can be called
610 * with spinlock held & IRQs disabled, so don't schedule !
612 int wait_for_ready (ide_drive_t *drive, int timeout)
614 ide_hwif_t *hwif = HWIF(drive);
618 stat = hwif->INB(IDE_STATUS_REG);
619 if (!(stat & BUSY_STAT)) {
620 if (drive->ready_stat == 0)
622 else if ((stat & drive->ready_stat)||(stat & ERR_STAT))
627 if ((stat & ERR_STAT) || timeout <= 0) {
628 if (stat & ERR_STAT) {
629 printk(KERN_ERR "%s: wait_for_ready, "
630 "error status: %x\n", drive->name, stat);
637 EXPORT_SYMBOL(wait_for_ready);
640 * This routine busy-waits for the drive status to be not "busy".
641 * It then checks the status for all of the "good" bits and none
642 * of the "bad" bits, and if all is okay it returns 0. All other
643 * cases return 1 after invoking ide_error() -- caller should just return.
645 * This routine should get fixed to not hog the cpu during extra long waits..
646 * That could be done by busy-waiting for the first jiffy or two, and then
647 * setting a timer to wake up at half second intervals thereafter,
648 * until timeout is achieved, before timing out.
650 int ide_wait_stat (ide_startstop_t *startstop, ide_drive_t *drive, u8 good, u8 bad, unsigned long timeout)
652 ide_hwif_t *hwif = HWIF(drive);
657 /* bail early if we've exceeded max_failures */
658 if (drive->max_failures && (drive->failures > drive->max_failures)) {
659 *startstop = ide_stopped;
663 udelay(1); /* spec allows drive 400ns to assert "BUSY" */
664 if ((stat = hwif->INB(IDE_STATUS_REG)) & BUSY_STAT) {
665 local_irq_set(flags);
667 while ((stat = hwif->INB(IDE_STATUS_REG)) & BUSY_STAT) {
668 if (time_after(jiffies, timeout)) {
670 * One last read after the timeout in case
671 * heavy interrupt load made us not make any
672 * progress during the timeout..
674 stat = hwif->INB(IDE_STATUS_REG);
675 if (!(stat & BUSY_STAT))
678 local_irq_restore(flags);
679 *startstop = DRIVER(drive)->error(drive, "status timeout", stat);
683 local_irq_restore(flags);
686 * Allow status to settle, then read it again.
687 * A few rare drives vastly violate the 400ns spec here,
688 * so we'll wait up to 10usec for a "good" status
689 * rather than expensively fail things immediately.
690 * This fix courtesy of Matthew Faupel & Niccolo Rigacci.
692 for (i = 0; i < 10; i++) {
694 if (OK_STAT((stat = hwif->INB(IDE_STATUS_REG)), good, bad))
697 *startstop = DRIVER(drive)->error(drive, "status error", stat);
701 EXPORT_SYMBOL(ide_wait_stat);
704 * All hosts that use the 80c ribbon must use!
705 * The name is derived from upper byte of word 93 and the 80c ribbon.
707 u8 eighty_ninty_three (ide_drive_t *drive)
710 if (!HWIF(drive)->udma_four)
713 if (drive->id->major_rev_num) {
717 * Determime highest Supported SPEC
719 for (i=1; i<=15; i++)
720 if (drive->id->major_rev_num & (1<<i))
727 /* ATA-4 and older do not support above Ultra 33 */
734 #ifndef CONFIG_IDEDMA_IVB
735 (drive->id->hw_config & 0x4000) &&
736 #endif /* CONFIG_IDEDMA_IVB */
737 (drive->id->hw_config & 0x6000)) ? 1 : 0);
741 return ((u8) ((HWIF(drive)->udma_four) &&
742 #ifndef CONFIG_IDEDMA_IVB
743 (drive->id->hw_config & 0x4000) &&
744 #endif /* CONFIG_IDEDMA_IVB */
745 (drive->id->hw_config & 0x6000)) ? 1 : 0);
749 EXPORT_SYMBOL(eighty_ninty_three);
751 int ide_ata66_check (ide_drive_t *drive, ide_task_t *args)
753 /* SATA has no cable restrictions */
754 if (HWIF(drive)->sata)
757 if ((args->tfRegister[IDE_COMMAND_OFFSET] == WIN_SETFEATURES) &&
758 (args->tfRegister[IDE_SECTOR_OFFSET] > XFER_UDMA_2) &&
759 (args->tfRegister[IDE_FEATURE_OFFSET] == SETFEATURES_XFER)) {
760 #ifndef CONFIG_IDEDMA_IVB
761 if ((drive->id->hw_config & 0x6000) == 0) {
762 #else /* !CONFIG_IDEDMA_IVB */
763 if (((drive->id->hw_config & 0x2000) == 0) ||
764 ((drive->id->hw_config & 0x4000) == 0)) {
765 #endif /* CONFIG_IDEDMA_IVB */
766 printk("%s: Speed warnings UDMA 3/4/5 is not "
767 "functional.\n", drive->name);
770 if (!HWIF(drive)->udma_four) {
771 printk("%s: Speed warnings UDMA 3/4/5 is not "
780 EXPORT_SYMBOL(ide_ata66_check);
783 * Backside of HDIO_DRIVE_CMD call of SETFEATURES_XFER.
784 * 1 : Safe to update drive->id DMA registers.
785 * 0 : OOPs not allowed.
787 int set_transfer (ide_drive_t *drive, ide_task_t *args)
789 if ((args->tfRegister[IDE_COMMAND_OFFSET] == WIN_SETFEATURES) &&
790 (args->tfRegister[IDE_SECTOR_OFFSET] >= XFER_SW_DMA_0) &&
791 (args->tfRegister[IDE_FEATURE_OFFSET] == SETFEATURES_XFER) &&
792 (drive->id->dma_ultra ||
793 drive->id->dma_mword ||
794 drive->id->dma_1word))
800 EXPORT_SYMBOL(set_transfer);
802 u8 ide_auto_reduce_xfer (ide_drive_t *drive)
804 if (!drive->crc_count)
805 return drive->current_speed;
806 drive->crc_count = 0;
808 switch(drive->current_speed) {
809 case XFER_UDMA_7: return XFER_UDMA_6;
810 case XFER_UDMA_6: return XFER_UDMA_5;
811 case XFER_UDMA_5: return XFER_UDMA_4;
812 case XFER_UDMA_4: return XFER_UDMA_3;
813 case XFER_UDMA_3: return XFER_UDMA_2;
814 case XFER_UDMA_2: return XFER_UDMA_1;
815 case XFER_UDMA_1: return XFER_UDMA_0;
817 * OOPS we do not goto non Ultra DMA modes
818 * without iCRC's available we force
819 * the system to PIO and make the user
820 * invoke the ATA-1 ATA-2 DMA modes.
823 default: return XFER_PIO_4;
827 EXPORT_SYMBOL(ide_auto_reduce_xfer);
832 int ide_driveid_update (ide_drive_t *drive)
834 ide_hwif_t *hwif = HWIF(drive);
835 struct hd_driveid *id;
837 id = kmalloc(SECTOR_WORDS*4, GFP_ATOMIC);
841 taskfile_lib_get_identify(drive, (char *)&id);
845 drive->id->dma_ultra = id->dma_ultra;
846 drive->id->dma_mword = id->dma_mword;
847 drive->id->dma_1word = id->dma_1word;
848 /* anything more ? */
854 * Re-read drive->id for possible DMA mode
855 * change (copied from ide-probe.c)
857 unsigned long timeout, flags;
859 SELECT_MASK(drive, 1);
861 hwif->OUTB(drive->ctl,IDE_CONTROL_REG);
863 hwif->OUTB(WIN_IDENTIFY, IDE_COMMAND_REG);
864 timeout = jiffies + WAIT_WORSTCASE;
866 if (time_after(jiffies, timeout)) {
867 SELECT_MASK(drive, 0);
868 return 0; /* drive timed-out */
870 ide_delay_50ms(); /* give drive a breather */
871 } while (hwif->INB(IDE_ALTSTATUS_REG) & BUSY_STAT);
872 ide_delay_50ms(); /* wait for IRQ and DRQ_STAT */
873 if (!OK_STAT(hwif->INB(IDE_STATUS_REG),DRQ_STAT,BAD_R_STAT)) {
874 SELECT_MASK(drive, 0);
875 printk("%s: CHECK for good STATUS\n", drive->name);
878 local_irq_save(flags);
879 SELECT_MASK(drive, 0);
880 id = kmalloc(SECTOR_WORDS*4, GFP_ATOMIC);
882 local_irq_restore(flags);
885 ata_input_data(drive, id, SECTOR_WORDS);
886 (void) hwif->INB(IDE_STATUS_REG); /* clear drive IRQ */
888 local_irq_restore(flags);
891 drive->id->dma_ultra = id->dma_ultra;
892 drive->id->dma_mword = id->dma_mword;
893 drive->id->dma_1word = id->dma_1word;
894 /* anything more ? */
901 EXPORT_SYMBOL(ide_driveid_update);
904 * Similar to ide_wait_stat(), except it never calls ide_error internally.
905 * This is a kludge to handle the new ide_config_drive_speed() function,
906 * and should not otherwise be used anywhere. Eventually, the tuneproc's
907 * should be updated to return ide_startstop_t, in which case we can get
908 * rid of this abomination again. :) -ml
910 * It is gone..........
912 * const char *msg == consider adding for verbose errors.
914 int ide_config_drive_speed (ide_drive_t *drive, u8 speed)
916 ide_hwif_t *hwif = HWIF(drive);
920 // while (HWGROUP(drive)->busy)
923 #if defined(CONFIG_BLK_DEV_IDEDMA) && !defined(CONFIG_DMA_NONPCI)
924 hwif->ide_dma_host_off(drive);
925 #endif /* (CONFIG_BLK_DEV_IDEDMA) && !(CONFIG_DMA_NONPCI) */
928 * Don't use ide_wait_cmd here - it will
929 * attempt to set_geometry and recalibrate,
930 * but for some reason these don't work at
931 * this point (lost interrupt).
934 * Select the drive, and issue the SETFEATURES command
936 disable_irq_nosync(hwif->irq);
939 SELECT_MASK(drive, 0);
942 hwif->OUTB(drive->ctl | 2, IDE_CONTROL_REG);
943 hwif->OUTB(speed, IDE_NSECTOR_REG);
944 hwif->OUTB(SETFEATURES_XFER, IDE_FEATURE_REG);
945 hwif->OUTB(WIN_SETFEATURES, IDE_COMMAND_REG);
946 if ((IDE_CONTROL_REG) && (drive->quirk_list == 2))
947 hwif->OUTB(drive->ctl, IDE_CONTROL_REG);
950 * Wait for drive to become non-BUSY
952 if ((stat = hwif->INB(IDE_STATUS_REG)) & BUSY_STAT) {
953 unsigned long flags, timeout;
954 local_irq_set(flags);
955 timeout = jiffies + WAIT_CMD;
956 while ((stat = hwif->INB(IDE_STATUS_REG)) & BUSY_STAT) {
957 if (time_after(jiffies, timeout))
960 local_irq_restore(flags);
964 * Allow status to settle, then read it again.
965 * A few rare drives vastly violate the 400ns spec here,
966 * so we'll wait up to 10usec for a "good" status
967 * rather than expensively fail things immediately.
968 * This fix courtesy of Matthew Faupel & Niccolo Rigacci.
970 for (i = 0; i < 10; i++) {
972 if (OK_STAT((stat = hwif->INB(IDE_STATUS_REG)), DRIVE_READY, BUSY_STAT|DRQ_STAT|ERR_STAT)) {
978 SELECT_MASK(drive, 0);
980 enable_irq(hwif->irq);
983 (void) ide_dump_status(drive, "set_drive_speed_status", stat);
987 drive->id->dma_ultra &= ~0xFF00;
988 drive->id->dma_mword &= ~0x0F00;
989 drive->id->dma_1word &= ~0x0F00;
991 #if defined(CONFIG_BLK_DEV_IDEDMA) && !defined(CONFIG_DMA_NONPCI)
992 if (speed >= XFER_SW_DMA_0)
993 hwif->ide_dma_host_on(drive);
995 hwif->ide_dma_off_quietly(drive);
996 #endif /* (CONFIG_BLK_DEV_IDEDMA) && !(CONFIG_DMA_NONPCI) */
999 case XFER_UDMA_7: drive->id->dma_ultra |= 0x8080; break;
1000 case XFER_UDMA_6: drive->id->dma_ultra |= 0x4040; break;
1001 case XFER_UDMA_5: drive->id->dma_ultra |= 0x2020; break;
1002 case XFER_UDMA_4: drive->id->dma_ultra |= 0x1010; break;
1003 case XFER_UDMA_3: drive->id->dma_ultra |= 0x0808; break;
1004 case XFER_UDMA_2: drive->id->dma_ultra |= 0x0404; break;
1005 case XFER_UDMA_1: drive->id->dma_ultra |= 0x0202; break;
1006 case XFER_UDMA_0: drive->id->dma_ultra |= 0x0101; break;
1007 case XFER_MW_DMA_2: drive->id->dma_mword |= 0x0404; break;
1008 case XFER_MW_DMA_1: drive->id->dma_mword |= 0x0202; break;
1009 case XFER_MW_DMA_0: drive->id->dma_mword |= 0x0101; break;
1010 case XFER_SW_DMA_2: drive->id->dma_1word |= 0x0404; break;
1011 case XFER_SW_DMA_1: drive->id->dma_1word |= 0x0202; break;
1012 case XFER_SW_DMA_0: drive->id->dma_1word |= 0x0101; break;
1015 if (!drive->init_speed)
1016 drive->init_speed = speed;
1017 drive->current_speed = speed;
1021 EXPORT_SYMBOL(ide_config_drive_speed);
1025 * This should get invoked any time we exit the driver to
1026 * wait for an interrupt response from a drive. handler() points
1027 * at the appropriate code to handle the next interrupt, and a
1028 * timer is started to prevent us from waiting forever in case
1029 * something goes wrong (see the ide_timer_expiry() handler later on).
1031 * See also ide_execute_command
1033 void __ide_set_handler (ide_drive_t *drive, ide_handler_t *handler,
1034 unsigned int timeout, ide_expiry_t *expiry)
1036 ide_hwgroup_t *hwgroup = HWGROUP(drive);
1038 if (hwgroup->handler != NULL) {
1039 printk(KERN_CRIT "%s: ide_set_handler: handler not null; "
1041 drive->name, hwgroup->handler, handler);
1044 hwgroup->handler = handler;
1045 hwgroup->expiry = expiry;
1046 hwgroup->timer.expires = jiffies + timeout;
1047 add_timer(&hwgroup->timer);
1050 EXPORT_SYMBOL(__ide_set_handler);
1052 void ide_set_handler (ide_drive_t *drive, ide_handler_t *handler,
1053 unsigned int timeout, ide_expiry_t *expiry)
1055 unsigned long flags;
1056 spin_lock_irqsave(&io_request_lock, flags);
1057 __ide_set_handler(drive, handler, timeout, expiry);
1058 spin_unlock_irqrestore(&io_request_lock, flags);
1061 EXPORT_SYMBOL(ide_set_handler);
1064 * ide_execute_command - execute an IDE command
1065 * @drive: IDE drive to issue the command against
1066 * @command: command byte to write
1067 * @handler: handler for next phase
1068 * @timeout: timeout for command
1069 * @expiry: handler to run on timeout
1071 * Helper function to issue an IDE command. This handles the
1072 * atomicity requirements, command timing and ensures that the
1073 * handler and IRQ setup do not race. All IDE command kick off
1074 * should go via this function or do equivalent locking.
1077 void ide_execute_command(ide_drive_t *drive, task_ioreg_t cmd, ide_handler_t *handler, unsigned timeout, ide_expiry_t *expiry)
1079 unsigned long flags;
1080 ide_hwgroup_t *hwgroup = HWGROUP(drive);
1081 ide_hwif_t *hwif = HWIF(drive);
1083 spin_lock_irqsave(&io_request_lock, flags);
1085 if(hwgroup->handler)
1087 hwgroup->handler = handler;
1088 hwgroup->expiry = expiry;
1089 hwgroup->timer.expires = jiffies + timeout;
1090 add_timer(&hwgroup->timer);
1091 hwif->OUTBSYNC(drive, cmd, IDE_COMMAND_REG);
1092 /* Drive takes 400nS to respond, we must avoid the IRQ being
1093 serviced before that.
1095 FIXME: we could skip this delay with care on non shared
1098 For DMA transfers highpoint have a neat trick we could
1099 use. When they take an IRQ they check STS but also that
1100 the DMA count is not zero (see hpt's own driver)
1103 spin_unlock_irqrestore(&io_request_lock, flags);
1106 EXPORT_SYMBOL(ide_execute_command);
1109 static ide_startstop_t do_reset1 (ide_drive_t *, int);
1112 * atapi_reset_pollfunc() gets invoked to poll the interface for completion every 50ms
1113 * during an atapi drive reset operation. If the drive has not yet responded,
1114 * and we have not yet hit our maximum waiting time, then the timer is restarted
1117 static ide_startstop_t atapi_reset_pollfunc (ide_drive_t *drive)
1119 ide_hwgroup_t *hwgroup = HWGROUP(drive);
1120 ide_hwif_t *hwif = HWIF(drive);
1123 SELECT_DRIVE(drive);
1126 if (OK_STAT(stat = hwif->INB(IDE_STATUS_REG), 0, BUSY_STAT)) {
1127 printk("%s: ATAPI reset complete\n", drive->name);
1129 if (time_before(jiffies, hwgroup->poll_timeout)) {
1130 if (HWGROUP(drive)->handler != NULL)
1132 ide_set_handler(drive, &atapi_reset_pollfunc, HZ/20, NULL);
1133 /* continue polling */
1136 /* end of polling */
1137 hwgroup->poll_timeout = 0;
1138 printk("%s: ATAPI reset timed-out, status=0x%02x\n",
1140 /* do it the old fashioned way */
1141 return do_reset1(drive, 1);
1144 hwgroup->poll_timeout = 0;
1149 * reset_pollfunc() gets invoked to poll the interface for completion every 50ms
1150 * during an ide reset operation. If the drives have not yet responded,
1151 * and we have not yet hit our maximum waiting time, then the timer is restarted
1154 static ide_startstop_t reset_pollfunc (ide_drive_t *drive)
1156 ide_hwgroup_t *hwgroup = HWGROUP(drive);
1157 ide_hwif_t *hwif = HWIF(drive);
1160 if (hwif->reset_poll != NULL) {
1161 if (hwif->reset_poll(drive)) {
1162 printk(KERN_ERR "%s: host reset_poll failure for %s.\n",
1163 hwif->name, drive->name);
1168 if (!OK_STAT(tmp = hwif->INB(IDE_STATUS_REG), 0, BUSY_STAT)) {
1169 if (time_before(jiffies, hwgroup->poll_timeout)) {
1170 if (HWGROUP(drive)->handler != NULL)
1172 ide_set_handler(drive, &reset_pollfunc, HZ/20, NULL);
1173 /* continue polling */
1176 printk("%s: reset timed-out, status=0x%02x\n", hwif->name, tmp);
1179 printk("%s: reset: ", hwif->name);
1180 if ((tmp = hwif->INB(IDE_ERROR_REG)) == 1) {
1181 printk("success\n");
1182 drive->failures = 0;
1185 #if FANCY_STATUS_DUMPS
1187 switch (tmp & 0x7f) {
1188 case 1: printk("passed");
1190 case 2: printk("formatter device error");
1192 case 3: printk("sector buffer error");
1194 case 4: printk("ECC circuitry error");
1196 case 5: printk("controlling MPU error");
1198 default:printk("error (0x%02x?)", tmp);
1201 printk("; slave: failed");
1205 #endif /* FANCY_STATUS_DUMPS */
1208 hwgroup->poll_timeout = 0; /* done polling */
1212 void check_dma_crc (ide_drive_t *drive)
1214 if (drive->crc_count) {
1215 (void) HWIF(drive)->ide_dma_off_quietly(drive);
1216 ide_set_xfer_rate(drive, ide_auto_reduce_xfer(drive));
1217 if (drive->current_speed >= XFER_SW_DMA_0)
1218 (void) HWIF(drive)->ide_dma_on(drive);
1220 (void) HWIF(drive)->ide_dma_off(drive);
1224 void pre_reset (ide_drive_t *drive)
1226 DRIVER(drive)->pre_reset(drive);
1228 if (!drive->keep_settings) {
1229 if (drive->using_dma) {
1230 check_dma_crc(drive);
1233 drive->io_32bit = 0;
1237 if (drive->using_dma)
1238 check_dma_crc(drive);
1240 if (HWIF(drive)->pre_reset != NULL)
1241 HWIF(drive)->pre_reset(drive);
1246 * do_reset1() attempts to recover a confused drive by resetting it.
1247 * Unfortunately, resetting a disk drive actually resets all devices on
1248 * the same interface, so it can really be thought of as resetting the
1249 * interface rather than resetting the drive.
1251 * ATAPI devices have their own reset mechanism which allows them to be
1252 * individually reset without clobbering other devices on the same interface.
1254 * Unfortunately, the IDE interface does not generate an interrupt to let
1255 * us know when the reset operation has finished, so we must poll for this.
1256 * Equally poor, though, is the fact that this may a very long time to complete,
1257 * (up to 30 seconds worstcase). So, instead of busy-waiting here for it,
1258 * we set a timer to poll at 50ms intervals.
1261 static ide_startstop_t do_reset1 (ide_drive_t *drive, int do_not_try_atapi)
1264 unsigned long flags;
1266 ide_hwgroup_t *hwgroup;
1268 spin_lock_irqsave(&io_request_lock, flags);
1270 hwgroup = HWGROUP(drive);
1273 /* We must not reset with running handlers */
1274 if(hwgroup->handler != NULL)
1277 /* For an ATAPI device, first try an ATAPI SRST. */
1278 if (drive->media != ide_disk && !do_not_try_atapi) {
1280 SELECT_DRIVE(drive);
1282 hwif->OUTB(WIN_SRST, IDE_COMMAND_REG);
1283 hwgroup->poll_timeout = jiffies + WAIT_WORSTCASE;
1284 __ide_set_handler(drive, &atapi_reset_pollfunc, HZ/20, NULL);
1285 spin_unlock_irqrestore(&io_request_lock, flags);
1290 * First, reset any device state data we were maintaining
1291 * for any of the drives on this interface.
1293 for (unit = 0; unit < MAX_DRIVES; ++unit)
1294 pre_reset(&hwif->drives[unit]);
1296 #if OK_TO_RESET_CONTROLLER
1297 if (!IDE_CONTROL_REG) {
1298 spin_unlock_irqrestore(&io_request_lock, flags);
1303 * Note that we also set nIEN while resetting the device,
1304 * to mask unwanted interrupts from the interface during the reset.
1305 * However, due to the design of PC hardware, this will cause an
1306 * immediate interrupt due to the edge transition it produces.
1307 * This single interrupt gives us a "fast poll" for drives that
1308 * recover from reset very quickly, saving us the first 50ms wait time.
1310 /* set SRST and nIEN */
1311 hwif->OUTBSYNC(drive, drive->ctl|6,IDE_CONTROL_REG);
1312 /* more than enough time */
1314 if (drive->quirk_list == 2) {
1315 /* clear SRST and nIEN */
1316 hwif->OUTBSYNC(drive, drive->ctl, IDE_CONTROL_REG);
1318 /* clear SRST, leave nIEN */
1319 hwif->OUTBSYNC(drive, drive->ctl|2, IDE_CONTROL_REG);
1321 /* more than enough time */
1324 hwgroup->poll_timeout = jiffies + WAIT_WORSTCASE;
1325 __ide_set_handler(drive, &reset_pollfunc, HZ/20, NULL);
1327 * Some weird controller like resetting themselves to a strange
1328 * state when the disks are reset this way. At least, the Winbond
1329 * 553 documentation says that
1331 if (hwif->resetproc != NULL) {
1332 hwif->resetproc(drive);
1335 #endif /* OK_TO_RESET_CONTROLLER */
1336 spin_unlock_irqrestore(&io_request_lock, flags);
1341 * ide_do_reset() is the entry point to the drive/interface reset code.
1343 * Caller must not hold the io_request lock.
1346 ide_startstop_t ide_do_reset (ide_drive_t *drive)
1348 return do_reset1(drive, 0);
1351 EXPORT_SYMBOL(ide_do_reset);