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>
9 #include <linux/module.h>
10 #include <linux/types.h>
11 #include <linux/string.h>
12 #include <linux/kernel.h>
13 #include <linux/timer.h>
15 #include <linux/interrupt.h>
16 #include <linux/major.h>
17 #include <linux/errno.h>
18 #include <linux/genhd.h>
19 #include <linux/blkpg.h>
20 #include <linux/slab.h>
21 #include <linux/pci.h>
22 #include <linux/delay.h>
23 #include <linux/hdreg.h>
24 #include <linux/ide.h>
25 #include <linux/bitops.h>
27 #include <asm/byteorder.h>
29 #include <asm/uaccess.h>
33 * Conventional PIO operations for ATA devices
36 static u8 ide_inb (unsigned long port)
38 return (u8) inb(port);
41 static u16 ide_inw (unsigned long port)
43 return (u16) inw(port);
46 static void ide_insw (unsigned long port, void *addr, u32 count)
48 insw(port, addr, count);
51 static u32 ide_inl (unsigned long port)
53 return (u32) inl(port);
56 static void ide_insl (unsigned long port, void *addr, u32 count)
58 insl(port, addr, count);
61 static void ide_outb (u8 val, unsigned long port)
66 static void ide_outbsync (ide_drive_t *drive, u8 addr, unsigned long port)
71 static void ide_outw (u16 val, unsigned long port)
76 static void ide_outsw (unsigned long port, void *addr, u32 count)
78 outsw(port, addr, count);
81 static void ide_outl (u32 val, unsigned long port)
86 static void ide_outsl (unsigned long port, void *addr, u32 count)
88 outsl(port, addr, count);
91 void default_hwif_iops (ide_hwif_t *hwif)
93 hwif->OUTB = ide_outb;
94 hwif->OUTBSYNC = ide_outbsync;
95 hwif->OUTW = ide_outw;
96 hwif->OUTL = ide_outl;
97 hwif->OUTSW = ide_outsw;
98 hwif->OUTSL = ide_outsl;
102 hwif->INSW = ide_insw;
103 hwif->INSL = ide_insl;
107 * MMIO operations, typically used for SATA controllers
110 static u8 ide_mm_inb (unsigned long port)
112 return (u8) readb((void __iomem *) port);
115 static u16 ide_mm_inw (unsigned long port)
117 return (u16) readw((void __iomem *) port);
120 static void ide_mm_insw (unsigned long port, void *addr, u32 count)
122 __ide_mm_insw((void __iomem *) port, addr, count);
125 static u32 ide_mm_inl (unsigned long port)
127 return (u32) readl((void __iomem *) port);
130 static void ide_mm_insl (unsigned long port, void *addr, u32 count)
132 __ide_mm_insl((void __iomem *) port, addr, count);
135 static void ide_mm_outb (u8 value, unsigned long port)
137 writeb(value, (void __iomem *) port);
140 static void ide_mm_outbsync (ide_drive_t *drive, u8 value, unsigned long port)
142 writeb(value, (void __iomem *) port);
145 static void ide_mm_outw (u16 value, unsigned long port)
147 writew(value, (void __iomem *) port);
150 static void ide_mm_outsw (unsigned long port, void *addr, u32 count)
152 __ide_mm_outsw((void __iomem *) port, addr, count);
155 static void ide_mm_outl (u32 value, unsigned long port)
157 writel(value, (void __iomem *) port);
160 static void ide_mm_outsl (unsigned long port, void *addr, u32 count)
162 __ide_mm_outsl((void __iomem *) port, addr, count);
165 void default_hwif_mmiops (ide_hwif_t *hwif)
167 hwif->OUTB = ide_mm_outb;
168 /* Most systems will need to override OUTBSYNC, alas however
169 this one is controller specific! */
170 hwif->OUTBSYNC = ide_mm_outbsync;
171 hwif->OUTW = ide_mm_outw;
172 hwif->OUTL = ide_mm_outl;
173 hwif->OUTSW = ide_mm_outsw;
174 hwif->OUTSL = ide_mm_outsl;
175 hwif->INB = ide_mm_inb;
176 hwif->INW = ide_mm_inw;
177 hwif->INL = ide_mm_inl;
178 hwif->INSW = ide_mm_insw;
179 hwif->INSL = ide_mm_insl;
182 EXPORT_SYMBOL(default_hwif_mmiops);
184 u32 ide_read_24 (ide_drive_t *drive)
186 u8 hcyl = HWIF(drive)->INB(IDE_HCYL_REG);
187 u8 lcyl = HWIF(drive)->INB(IDE_LCYL_REG);
188 u8 sect = HWIF(drive)->INB(IDE_SECTOR_REG);
189 return (hcyl<<16)|(lcyl<<8)|sect;
192 void SELECT_DRIVE (ide_drive_t *drive)
194 if (HWIF(drive)->selectproc)
195 HWIF(drive)->selectproc(drive);
196 HWIF(drive)->OUTB(drive->select.all, IDE_SELECT_REG);
199 EXPORT_SYMBOL(SELECT_DRIVE);
201 void SELECT_INTERRUPT (ide_drive_t *drive)
203 if (HWIF(drive)->intrproc)
204 HWIF(drive)->intrproc(drive);
206 HWIF(drive)->OUTB(drive->ctl|2, IDE_CONTROL_REG);
209 void SELECT_MASK (ide_drive_t *drive, int mask)
211 if (HWIF(drive)->maskproc)
212 HWIF(drive)->maskproc(drive, mask);
215 void QUIRK_LIST (ide_drive_t *drive)
217 if (HWIF(drive)->quirkproc)
218 drive->quirk_list = HWIF(drive)->quirkproc(drive);
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(ide_drive_t *drive, unsigned long port)
230 (void) HWIF(drive)->INB(port);
231 (void) HWIF(drive)->INB(port);
232 (void) HWIF(drive)->INB(port);
236 * This is used for most PIO data transfers *from* the IDE interface
238 static void ata_input_data(ide_drive_t *drive, void *buffer, u32 wcount)
240 ide_hwif_t *hwif = HWIF(drive);
241 u8 io_32bit = drive->io_32bit;
246 local_irq_save(flags);
247 ata_vlb_sync(drive, IDE_NSECTOR_REG);
248 hwif->INSL(IDE_DATA_REG, buffer, wcount);
249 local_irq_restore(flags);
251 hwif->INSL(IDE_DATA_REG, buffer, wcount);
253 hwif->INSW(IDE_DATA_REG, buffer, wcount<<1);
258 * This is used for most PIO data transfers *to* the IDE interface
260 static void ata_output_data(ide_drive_t *drive, void *buffer, u32 wcount)
262 ide_hwif_t *hwif = HWIF(drive);
263 u8 io_32bit = drive->io_32bit;
268 local_irq_save(flags);
269 ata_vlb_sync(drive, IDE_NSECTOR_REG);
270 hwif->OUTSL(IDE_DATA_REG, buffer, wcount);
271 local_irq_restore(flags);
273 hwif->OUTSL(IDE_DATA_REG, buffer, wcount);
275 hwif->OUTSW(IDE_DATA_REG, buffer, wcount<<1);
280 * The following routines are mainly used by the ATAPI drivers.
282 * These routines will round up any request for an odd number of bytes,
283 * so if an odd bytecount is specified, be sure that there's at least one
284 * extra byte allocated for the buffer.
287 static void atapi_input_bytes(ide_drive_t *drive, void *buffer, u32 bytecount)
289 ide_hwif_t *hwif = HWIF(drive);
292 #if defined(CONFIG_ATARI) || defined(CONFIG_Q40)
293 if (MACH_IS_ATARI || MACH_IS_Q40) {
294 /* Atari has a byte-swapped IDE interface */
295 insw_swapw(IDE_DATA_REG, buffer, bytecount / 2);
298 #endif /* CONFIG_ATARI || CONFIG_Q40 */
299 hwif->ata_input_data(drive, buffer, bytecount / 4);
300 if ((bytecount & 0x03) >= 2)
301 hwif->INSW(IDE_DATA_REG, ((u8 *)buffer)+(bytecount & ~0x03), 1);
304 static void atapi_output_bytes(ide_drive_t *drive, void *buffer, u32 bytecount)
306 ide_hwif_t *hwif = HWIF(drive);
309 #if defined(CONFIG_ATARI) || defined(CONFIG_Q40)
310 if (MACH_IS_ATARI || MACH_IS_Q40) {
311 /* Atari has a byte-swapped IDE interface */
312 outsw_swapw(IDE_DATA_REG, buffer, bytecount / 2);
315 #endif /* CONFIG_ATARI || CONFIG_Q40 */
316 hwif->ata_output_data(drive, buffer, bytecount / 4);
317 if ((bytecount & 0x03) >= 2)
318 hwif->OUTSW(IDE_DATA_REG, ((u8*)buffer)+(bytecount & ~0x03), 1);
321 void default_hwif_transport(ide_hwif_t *hwif)
323 hwif->ata_input_data = ata_input_data;
324 hwif->ata_output_data = ata_output_data;
325 hwif->atapi_input_bytes = atapi_input_bytes;
326 hwif->atapi_output_bytes = atapi_output_bytes;
330 * Beginning of Taskfile OPCODE Library and feature sets.
332 void ide_fix_driveid (struct hd_driveid *id)
334 #ifndef __LITTLE_ENDIAN
339 id->config = __le16_to_cpu(id->config);
340 id->cyls = __le16_to_cpu(id->cyls);
341 id->reserved2 = __le16_to_cpu(id->reserved2);
342 id->heads = __le16_to_cpu(id->heads);
343 id->track_bytes = __le16_to_cpu(id->track_bytes);
344 id->sector_bytes = __le16_to_cpu(id->sector_bytes);
345 id->sectors = __le16_to_cpu(id->sectors);
346 id->vendor0 = __le16_to_cpu(id->vendor0);
347 id->vendor1 = __le16_to_cpu(id->vendor1);
348 id->vendor2 = __le16_to_cpu(id->vendor2);
349 stringcast = (u16 *)&id->serial_no[0];
350 for (i = 0; i < (20/2); i++)
351 stringcast[i] = __le16_to_cpu(stringcast[i]);
352 id->buf_type = __le16_to_cpu(id->buf_type);
353 id->buf_size = __le16_to_cpu(id->buf_size);
354 id->ecc_bytes = __le16_to_cpu(id->ecc_bytes);
355 stringcast = (u16 *)&id->fw_rev[0];
356 for (i = 0; i < (8/2); i++)
357 stringcast[i] = __le16_to_cpu(stringcast[i]);
358 stringcast = (u16 *)&id->model[0];
359 for (i = 0; i < (40/2); i++)
360 stringcast[i] = __le16_to_cpu(stringcast[i]);
361 id->dword_io = __le16_to_cpu(id->dword_io);
362 id->reserved50 = __le16_to_cpu(id->reserved50);
363 id->field_valid = __le16_to_cpu(id->field_valid);
364 id->cur_cyls = __le16_to_cpu(id->cur_cyls);
365 id->cur_heads = __le16_to_cpu(id->cur_heads);
366 id->cur_sectors = __le16_to_cpu(id->cur_sectors);
367 id->cur_capacity0 = __le16_to_cpu(id->cur_capacity0);
368 id->cur_capacity1 = __le16_to_cpu(id->cur_capacity1);
369 id->lba_capacity = __le32_to_cpu(id->lba_capacity);
370 id->dma_1word = __le16_to_cpu(id->dma_1word);
371 id->dma_mword = __le16_to_cpu(id->dma_mword);
372 id->eide_pio_modes = __le16_to_cpu(id->eide_pio_modes);
373 id->eide_dma_min = __le16_to_cpu(id->eide_dma_min);
374 id->eide_dma_time = __le16_to_cpu(id->eide_dma_time);
375 id->eide_pio = __le16_to_cpu(id->eide_pio);
376 id->eide_pio_iordy = __le16_to_cpu(id->eide_pio_iordy);
377 for (i = 0; i < 2; ++i)
378 id->words69_70[i] = __le16_to_cpu(id->words69_70[i]);
379 for (i = 0; i < 4; ++i)
380 id->words71_74[i] = __le16_to_cpu(id->words71_74[i]);
381 id->queue_depth = __le16_to_cpu(id->queue_depth);
382 for (i = 0; i < 4; ++i)
383 id->words76_79[i] = __le16_to_cpu(id->words76_79[i]);
384 id->major_rev_num = __le16_to_cpu(id->major_rev_num);
385 id->minor_rev_num = __le16_to_cpu(id->minor_rev_num);
386 id->command_set_1 = __le16_to_cpu(id->command_set_1);
387 id->command_set_2 = __le16_to_cpu(id->command_set_2);
388 id->cfsse = __le16_to_cpu(id->cfsse);
389 id->cfs_enable_1 = __le16_to_cpu(id->cfs_enable_1);
390 id->cfs_enable_2 = __le16_to_cpu(id->cfs_enable_2);
391 id->csf_default = __le16_to_cpu(id->csf_default);
392 id->dma_ultra = __le16_to_cpu(id->dma_ultra);
393 id->trseuc = __le16_to_cpu(id->trseuc);
394 id->trsEuc = __le16_to_cpu(id->trsEuc);
395 id->CurAPMvalues = __le16_to_cpu(id->CurAPMvalues);
396 id->mprc = __le16_to_cpu(id->mprc);
397 id->hw_config = __le16_to_cpu(id->hw_config);
398 id->acoustic = __le16_to_cpu(id->acoustic);
399 id->msrqs = __le16_to_cpu(id->msrqs);
400 id->sxfert = __le16_to_cpu(id->sxfert);
401 id->sal = __le16_to_cpu(id->sal);
402 id->spg = __le32_to_cpu(id->spg);
403 id->lba_capacity_2 = __le64_to_cpu(id->lba_capacity_2);
404 for (i = 0; i < 22; i++)
405 id->words104_125[i] = __le16_to_cpu(id->words104_125[i]);
406 id->last_lun = __le16_to_cpu(id->last_lun);
407 id->word127 = __le16_to_cpu(id->word127);
408 id->dlf = __le16_to_cpu(id->dlf);
409 id->csfo = __le16_to_cpu(id->csfo);
410 for (i = 0; i < 26; i++)
411 id->words130_155[i] = __le16_to_cpu(id->words130_155[i]);
412 id->word156 = __le16_to_cpu(id->word156);
413 for (i = 0; i < 3; i++)
414 id->words157_159[i] = __le16_to_cpu(id->words157_159[i]);
415 id->cfa_power = __le16_to_cpu(id->cfa_power);
416 for (i = 0; i < 14; i++)
417 id->words161_175[i] = __le16_to_cpu(id->words161_175[i]);
418 for (i = 0; i < 31; i++)
419 id->words176_205[i] = __le16_to_cpu(id->words176_205[i]);
420 for (i = 0; i < 48; i++)
421 id->words206_254[i] = __le16_to_cpu(id->words206_254[i]);
422 id->integrity_word = __le16_to_cpu(id->integrity_word);
424 # error "Please fix <asm/byteorder.h>"
429 /* FIXME: exported for use by the USB storage (isd200.c) code only */
430 EXPORT_SYMBOL(ide_fix_driveid);
432 void ide_fixstring (u8 *s, const int bytecount, const int byteswap)
434 u8 *p = s, *end = &s[bytecount & ~1]; /* bytecount must be even */
437 /* convert from big-endian to host byte order */
438 for (p = end ; p != s;) {
439 unsigned short *pp = (unsigned short *) (p -= 2);
443 /* strip leading blanks */
444 while (s != end && *s == ' ')
446 /* compress internal blanks and strip trailing blanks */
447 while (s != end && *s) {
448 if (*s++ != ' ' || (s != end && *s && *s != ' '))
451 /* wipe out trailing garbage */
456 EXPORT_SYMBOL(ide_fixstring);
459 * Needed for PCI irq sharing
461 int drive_is_ready (ide_drive_t *drive)
463 ide_hwif_t *hwif = HWIF(drive);
466 if (drive->waiting_for_dma)
467 return hwif->ide_dma_test_irq(drive);
470 /* need to guarantee 400ns since last command was issued */
474 #ifdef CONFIG_IDEPCI_SHARE_IRQ
476 * We do a passive status test under shared PCI interrupts on
477 * cards that truly share the ATA side interrupt, but may also share
478 * an interrupt with another pci card/device. We make no assumptions
479 * about possible isa-pnp and pci-pnp issues yet.
482 stat = hwif->INB(IDE_ALTSTATUS_REG);
484 #endif /* CONFIG_IDEPCI_SHARE_IRQ */
485 /* Note: this may clear a pending IRQ!! */
486 stat = hwif->INB(IDE_STATUS_REG);
488 if (stat & BUSY_STAT)
489 /* drive busy: definitely not interrupting */
492 /* drive ready: *might* be interrupting */
496 EXPORT_SYMBOL(drive_is_ready);
499 * Global for All, and taken from ide-pmac.c. Can be called
500 * with spinlock held & IRQs disabled, so don't schedule !
502 int wait_for_ready (ide_drive_t *drive, int timeout)
504 ide_hwif_t *hwif = HWIF(drive);
508 stat = hwif->INB(IDE_STATUS_REG);
509 if (!(stat & BUSY_STAT)) {
510 if (drive->ready_stat == 0)
512 else if ((stat & drive->ready_stat)||(stat & ERR_STAT))
517 if ((stat & ERR_STAT) || timeout <= 0) {
518 if (stat & ERR_STAT) {
519 printk(KERN_ERR "%s: wait_for_ready, "
520 "error status: %x\n", drive->name, stat);
528 * This routine busy-waits for the drive status to be not "busy".
529 * It then checks the status for all of the "good" bits and none
530 * of the "bad" bits, and if all is okay it returns 0. All other
531 * cases return 1 after invoking ide_error() -- caller should just return.
533 * This routine should get fixed to not hog the cpu during extra long waits..
534 * That could be done by busy-waiting for the first jiffy or two, and then
535 * setting a timer to wake up at half second intervals thereafter,
536 * until timeout is achieved, before timing out.
538 int ide_wait_stat (ide_startstop_t *startstop, ide_drive_t *drive, u8 good, u8 bad, unsigned long timeout)
540 ide_hwif_t *hwif = HWIF(drive);
545 /* bail early if we've exceeded max_failures */
546 if (drive->max_failures && (drive->failures > drive->max_failures)) {
547 *startstop = ide_stopped;
551 udelay(1); /* spec allows drive 400ns to assert "BUSY" */
552 if ((stat = hwif->INB(IDE_STATUS_REG)) & BUSY_STAT) {
553 local_irq_set(flags);
555 while ((stat = hwif->INB(IDE_STATUS_REG)) & BUSY_STAT) {
556 if (time_after(jiffies, timeout)) {
558 * One last read after the timeout in case
559 * heavy interrupt load made us not make any
560 * progress during the timeout..
562 stat = hwif->INB(IDE_STATUS_REG);
563 if (!(stat & BUSY_STAT))
566 local_irq_restore(flags);
567 *startstop = ide_error(drive, "status timeout", stat);
571 local_irq_restore(flags);
574 * Allow status to settle, then read it again.
575 * A few rare drives vastly violate the 400ns spec here,
576 * so we'll wait up to 10usec for a "good" status
577 * rather than expensively fail things immediately.
578 * This fix courtesy of Matthew Faupel & Niccolo Rigacci.
580 for (i = 0; i < 10; i++) {
582 if (OK_STAT((stat = hwif->INB(IDE_STATUS_REG)), good, bad))
585 *startstop = ide_error(drive, "status error", stat);
589 EXPORT_SYMBOL(ide_wait_stat);
592 * All hosts that use the 80c ribbon must use!
593 * The name is derived from upper byte of word 93 and the 80c ribbon.
595 u8 eighty_ninty_three (ide_drive_t *drive)
597 if(HWIF(drive)->udma_four == 0)
600 /* Check for SATA but only if we are ATA5 or higher */
601 if (drive->id->hw_config == 0 && (drive->id->major_rev_num & 0x7FE0))
603 if (!(drive->id->hw_config & 0x6000))
605 #ifndef CONFIG_IDEDMA_IVB
606 if(!(drive->id->hw_config & 0x4000))
608 #endif /* CONFIG_IDEDMA_IVB */
612 EXPORT_SYMBOL(eighty_ninty_three);
614 int ide_ata66_check (ide_drive_t *drive, ide_task_t *args)
616 if ((args->tfRegister[IDE_COMMAND_OFFSET] == WIN_SETFEATURES) &&
617 (args->tfRegister[IDE_SECTOR_OFFSET] > XFER_UDMA_2) &&
618 (args->tfRegister[IDE_FEATURE_OFFSET] == SETFEATURES_XFER)) {
619 #ifndef CONFIG_IDEDMA_IVB
620 if ((drive->id->hw_config & 0x6000) == 0) {
621 #else /* !CONFIG_IDEDMA_IVB */
622 if (((drive->id->hw_config & 0x2000) == 0) ||
623 ((drive->id->hw_config & 0x4000) == 0)) {
624 #endif /* CONFIG_IDEDMA_IVB */
625 printk("%s: Speed warnings UDMA 3/4/5 is not "
626 "functional.\n", drive->name);
629 if (!HWIF(drive)->udma_four) {
630 printk("%s: Speed warnings UDMA 3/4/5 is not "
640 * Backside of HDIO_DRIVE_CMD call of SETFEATURES_XFER.
641 * 1 : Safe to update drive->id DMA registers.
642 * 0 : OOPs not allowed.
644 int set_transfer (ide_drive_t *drive, ide_task_t *args)
646 if ((args->tfRegister[IDE_COMMAND_OFFSET] == WIN_SETFEATURES) &&
647 (args->tfRegister[IDE_SECTOR_OFFSET] >= XFER_SW_DMA_0) &&
648 (args->tfRegister[IDE_FEATURE_OFFSET] == SETFEATURES_XFER) &&
649 (drive->id->dma_ultra ||
650 drive->id->dma_mword ||
651 drive->id->dma_1word))
657 #ifdef CONFIG_BLK_DEV_IDEDMA
658 static u8 ide_auto_reduce_xfer (ide_drive_t *drive)
660 if (!drive->crc_count)
661 return drive->current_speed;
662 drive->crc_count = 0;
664 switch(drive->current_speed) {
665 case XFER_UDMA_7: return XFER_UDMA_6;
666 case XFER_UDMA_6: return XFER_UDMA_5;
667 case XFER_UDMA_5: return XFER_UDMA_4;
668 case XFER_UDMA_4: return XFER_UDMA_3;
669 case XFER_UDMA_3: return XFER_UDMA_2;
670 case XFER_UDMA_2: return XFER_UDMA_1;
671 case XFER_UDMA_1: return XFER_UDMA_0;
673 * OOPS we do not goto non Ultra DMA modes
674 * without iCRC's available we force
675 * the system to PIO and make the user
676 * invoke the ATA-1 ATA-2 DMA modes.
679 default: return XFER_PIO_4;
682 #endif /* CONFIG_BLK_DEV_IDEDMA */
687 int ide_driveid_update (ide_drive_t *drive)
689 ide_hwif_t *hwif = HWIF(drive);
690 struct hd_driveid *id;
692 id = kmalloc(SECTOR_WORDS*4, GFP_ATOMIC);
696 taskfile_lib_get_identify(drive, (char *)&id);
700 drive->id->dma_ultra = id->dma_ultra;
701 drive->id->dma_mword = id->dma_mword;
702 drive->id->dma_1word = id->dma_1word;
703 /* anything more ? */
709 * Re-read drive->id for possible DMA mode
710 * change (copied from ide-probe.c)
712 unsigned long timeout, flags;
714 SELECT_MASK(drive, 1);
716 hwif->OUTB(drive->ctl,IDE_CONTROL_REG);
718 hwif->OUTB(WIN_IDENTIFY, IDE_COMMAND_REG);
719 timeout = jiffies + WAIT_WORSTCASE;
721 if (time_after(jiffies, timeout)) {
722 SELECT_MASK(drive, 0);
723 return 0; /* drive timed-out */
725 msleep(50); /* give drive a breather */
726 } while (hwif->INB(IDE_ALTSTATUS_REG) & BUSY_STAT);
727 msleep(50); /* wait for IRQ and DRQ_STAT */
728 if (!OK_STAT(hwif->INB(IDE_STATUS_REG),DRQ_STAT,BAD_R_STAT)) {
729 SELECT_MASK(drive, 0);
730 printk("%s: CHECK for good STATUS\n", drive->name);
733 local_irq_save(flags);
734 SELECT_MASK(drive, 0);
735 id = kmalloc(SECTOR_WORDS*4, GFP_ATOMIC);
737 local_irq_restore(flags);
740 ata_input_data(drive, id, SECTOR_WORDS);
741 (void) hwif->INB(IDE_STATUS_REG); /* clear drive IRQ */
743 local_irq_restore(flags);
746 drive->id->dma_ultra = id->dma_ultra;
747 drive->id->dma_mword = id->dma_mword;
748 drive->id->dma_1word = id->dma_1word;
749 /* anything more ? */
758 * Similar to ide_wait_stat(), except it never calls ide_error internally.
759 * This is a kludge to handle the new ide_config_drive_speed() function,
760 * and should not otherwise be used anywhere. Eventually, the tuneproc's
761 * should be updated to return ide_startstop_t, in which case we can get
762 * rid of this abomination again. :) -ml
764 * It is gone..........
766 * const char *msg == consider adding for verbose errors.
768 int ide_config_drive_speed (ide_drive_t *drive, u8 speed)
770 ide_hwif_t *hwif = HWIF(drive);
774 // while (HWGROUP(drive)->busy)
777 #ifdef CONFIG_BLK_DEV_IDEDMA
778 if (hwif->ide_dma_check) /* check if host supports DMA */
779 hwif->ide_dma_host_off(drive);
783 * Don't use ide_wait_cmd here - it will
784 * attempt to set_geometry and recalibrate,
785 * but for some reason these don't work at
786 * this point (lost interrupt).
789 * Select the drive, and issue the SETFEATURES command
791 disable_irq_nosync(hwif->irq);
794 * FIXME: we race against the running IRQ here if
795 * this is called from non IRQ context. If we use
796 * disable_irq() we hang on the error path. Work
802 SELECT_MASK(drive, 0);
805 hwif->OUTB(drive->ctl | 2, IDE_CONTROL_REG);
806 hwif->OUTB(speed, IDE_NSECTOR_REG);
807 hwif->OUTB(SETFEATURES_XFER, IDE_FEATURE_REG);
808 hwif->OUTB(WIN_SETFEATURES, IDE_COMMAND_REG);
809 if ((IDE_CONTROL_REG) && (drive->quirk_list == 2))
810 hwif->OUTB(drive->ctl, IDE_CONTROL_REG);
813 * Wait for drive to become non-BUSY
815 if ((stat = hwif->INB(IDE_STATUS_REG)) & BUSY_STAT) {
816 unsigned long flags, timeout;
817 local_irq_set(flags);
818 timeout = jiffies + WAIT_CMD;
819 while ((stat = hwif->INB(IDE_STATUS_REG)) & BUSY_STAT) {
820 if (time_after(jiffies, timeout))
823 local_irq_restore(flags);
827 * Allow status to settle, then read it again.
828 * A few rare drives vastly violate the 400ns spec here,
829 * so we'll wait up to 10usec for a "good" status
830 * rather than expensively fail things immediately.
831 * This fix courtesy of Matthew Faupel & Niccolo Rigacci.
833 for (i = 0; i < 10; i++) {
835 if (OK_STAT((stat = hwif->INB(IDE_STATUS_REG)), DRIVE_READY, 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;
854 #ifdef CONFIG_BLK_DEV_IDEDMA
855 if (speed >= XFER_SW_DMA_0)
856 hwif->ide_dma_host_on(drive);
857 else if (hwif->ide_dma_check) /* check if host supports DMA */
858 hwif->ide_dma_off_quietly(drive);
862 case XFER_UDMA_7: drive->id->dma_ultra |= 0x8080; break;
863 case XFER_UDMA_6: drive->id->dma_ultra |= 0x4040; break;
864 case XFER_UDMA_5: drive->id->dma_ultra |= 0x2020; break;
865 case XFER_UDMA_4: drive->id->dma_ultra |= 0x1010; break;
866 case XFER_UDMA_3: drive->id->dma_ultra |= 0x0808; break;
867 case XFER_UDMA_2: drive->id->dma_ultra |= 0x0404; break;
868 case XFER_UDMA_1: drive->id->dma_ultra |= 0x0202; break;
869 case XFER_UDMA_0: drive->id->dma_ultra |= 0x0101; break;
870 case XFER_MW_DMA_2: drive->id->dma_mword |= 0x0404; break;
871 case XFER_MW_DMA_1: drive->id->dma_mword |= 0x0202; break;
872 case XFER_MW_DMA_0: drive->id->dma_mword |= 0x0101; break;
873 case XFER_SW_DMA_2: drive->id->dma_1word |= 0x0404; break;
874 case XFER_SW_DMA_1: drive->id->dma_1word |= 0x0202; break;
875 case XFER_SW_DMA_0: drive->id->dma_1word |= 0x0101; break;
878 if (!drive->init_speed)
879 drive->init_speed = speed;
880 drive->current_speed = speed;
884 EXPORT_SYMBOL(ide_config_drive_speed);
888 * This should get invoked any time we exit the driver to
889 * wait for an interrupt response from a drive. handler() points
890 * at the appropriate code to handle the next interrupt, and a
891 * timer is started to prevent us from waiting forever in case
892 * something goes wrong (see the ide_timer_expiry() handler later on).
894 * See also ide_execute_command
896 static void __ide_set_handler (ide_drive_t *drive, ide_handler_t *handler,
897 unsigned int timeout, ide_expiry_t *expiry)
899 ide_hwgroup_t *hwgroup = HWGROUP(drive);
901 if (hwgroup->handler != NULL) {
902 printk(KERN_CRIT "%s: ide_set_handler: handler not null; "
904 drive->name, hwgroup->handler, handler);
906 hwgroup->handler = handler;
907 hwgroup->expiry = expiry;
908 hwgroup->timer.expires = jiffies + timeout;
909 add_timer(&hwgroup->timer);
912 void ide_set_handler (ide_drive_t *drive, ide_handler_t *handler,
913 unsigned int timeout, ide_expiry_t *expiry)
916 spin_lock_irqsave(&ide_lock, flags);
917 __ide_set_handler(drive, handler, timeout, expiry);
918 spin_unlock_irqrestore(&ide_lock, flags);
921 EXPORT_SYMBOL(ide_set_handler);
924 * ide_execute_command - execute an IDE command
925 * @drive: IDE drive to issue the command against
926 * @command: command byte to write
927 * @handler: handler for next phase
928 * @timeout: timeout for command
929 * @expiry: handler to run on timeout
931 * Helper function to issue an IDE command. This handles the
932 * atomicity requirements, command timing and ensures that the
933 * handler and IRQ setup do not race. All IDE command kick off
934 * should go via this function or do equivalent locking.
937 void ide_execute_command(ide_drive_t *drive, task_ioreg_t cmd, ide_handler_t *handler, unsigned timeout, ide_expiry_t *expiry)
940 ide_hwgroup_t *hwgroup = HWGROUP(drive);
941 ide_hwif_t *hwif = HWIF(drive);
943 spin_lock_irqsave(&ide_lock, flags);
945 BUG_ON(hwgroup->handler);
946 hwgroup->handler = handler;
947 hwgroup->expiry = expiry;
948 hwgroup->timer.expires = jiffies + timeout;
949 add_timer(&hwgroup->timer);
950 hwif->OUTBSYNC(drive, cmd, IDE_COMMAND_REG);
951 /* Drive takes 400nS to respond, we must avoid the IRQ being
952 serviced before that.
954 FIXME: we could skip this delay with care on non shared
958 spin_unlock_irqrestore(&ide_lock, flags);
961 EXPORT_SYMBOL(ide_execute_command);
965 static ide_startstop_t do_reset1 (ide_drive_t *, int);
968 * atapi_reset_pollfunc() gets invoked to poll the interface for completion every 50ms
969 * during an atapi drive reset operation. If the drive has not yet responded,
970 * and we have not yet hit our maximum waiting time, then the timer is restarted
973 static ide_startstop_t atapi_reset_pollfunc (ide_drive_t *drive)
975 ide_hwgroup_t *hwgroup = HWGROUP(drive);
976 ide_hwif_t *hwif = HWIF(drive);
982 if (OK_STAT(stat = hwif->INB(IDE_STATUS_REG), 0, BUSY_STAT)) {
983 printk("%s: ATAPI reset complete\n", drive->name);
985 if (time_before(jiffies, hwgroup->poll_timeout)) {
986 BUG_ON(HWGROUP(drive)->handler != NULL);
987 ide_set_handler(drive, &atapi_reset_pollfunc, HZ/20, NULL);
988 /* continue polling */
992 hwgroup->polling = 0;
993 printk("%s: ATAPI reset timed-out, status=0x%02x\n",
995 /* do it the old fashioned way */
996 return do_reset1(drive, 1);
999 hwgroup->polling = 0;
1004 * reset_pollfunc() gets invoked to poll the interface for completion every 50ms
1005 * during an ide reset operation. If the drives have not yet responded,
1006 * and we have not yet hit our maximum waiting time, then the timer is restarted
1009 static ide_startstop_t reset_pollfunc (ide_drive_t *drive)
1011 ide_hwgroup_t *hwgroup = HWGROUP(drive);
1012 ide_hwif_t *hwif = HWIF(drive);
1015 if (hwif->reset_poll != NULL) {
1016 if (hwif->reset_poll(drive)) {
1017 printk(KERN_ERR "%s: host reset_poll failure for %s.\n",
1018 hwif->name, drive->name);
1023 if (!OK_STAT(tmp = hwif->INB(IDE_STATUS_REG), 0, BUSY_STAT)) {
1024 if (time_before(jiffies, hwgroup->poll_timeout)) {
1025 BUG_ON(HWGROUP(drive)->handler != NULL);
1026 ide_set_handler(drive, &reset_pollfunc, HZ/20, NULL);
1027 /* continue polling */
1030 printk("%s: reset timed-out, status=0x%02x\n", hwif->name, tmp);
1033 printk("%s: reset: ", hwif->name);
1034 if ((tmp = hwif->INB(IDE_ERROR_REG)) == 1) {
1035 printk("success\n");
1036 drive->failures = 0;
1040 switch (tmp & 0x7f) {
1041 case 1: printk("passed");
1043 case 2: printk("formatter device error");
1045 case 3: printk("sector buffer error");
1047 case 4: printk("ECC circuitry error");
1049 case 5: printk("controlling MPU error");
1051 default:printk("error (0x%02x?)", tmp);
1054 printk("; slave: failed");
1058 hwgroup->polling = 0; /* done polling */
1062 static void check_dma_crc(ide_drive_t *drive)
1064 #ifdef CONFIG_BLK_DEV_IDEDMA
1065 if (drive->crc_count) {
1066 (void) HWIF(drive)->ide_dma_off_quietly(drive);
1067 ide_set_xfer_rate(drive, ide_auto_reduce_xfer(drive));
1068 if (drive->current_speed >= XFER_SW_DMA_0)
1069 (void) HWIF(drive)->ide_dma_on(drive);
1071 (void)__ide_dma_off(drive);
1075 static void ide_disk_pre_reset(ide_drive_t *drive)
1077 int legacy = (drive->id->cfs_enable_2 & 0x0400) ? 0 : 1;
1079 drive->special.all = 0;
1080 drive->special.b.set_geometry = legacy;
1081 drive->special.b.recalibrate = legacy;
1082 if (OK_TO_RESET_CONTROLLER)
1083 drive->mult_count = 0;
1084 if (!drive->keep_settings && !drive->using_dma)
1085 drive->mult_req = 0;
1086 if (drive->mult_req != drive->mult_count)
1087 drive->special.b.set_multmode = 1;
1090 static void pre_reset(ide_drive_t *drive)
1092 if (drive->media == ide_disk)
1093 ide_disk_pre_reset(drive);
1095 drive->post_reset = 1;
1097 if (!drive->keep_settings) {
1098 if (drive->using_dma) {
1099 check_dma_crc(drive);
1102 drive->io_32bit = 0;
1106 if (drive->using_dma)
1107 check_dma_crc(drive);
1109 if (HWIF(drive)->pre_reset != NULL)
1110 HWIF(drive)->pre_reset(drive);
1115 * do_reset1() attempts to recover a confused drive by resetting it.
1116 * Unfortunately, resetting a disk drive actually resets all devices on
1117 * the same interface, so it can really be thought of as resetting the
1118 * interface rather than resetting the drive.
1120 * ATAPI devices have their own reset mechanism which allows them to be
1121 * individually reset without clobbering other devices on the same interface.
1123 * Unfortunately, the IDE interface does not generate an interrupt to let
1124 * us know when the reset operation has finished, so we must poll for this.
1125 * Equally poor, though, is the fact that this may a very long time to complete,
1126 * (up to 30 seconds worstcase). So, instead of busy-waiting here for it,
1127 * we set a timer to poll at 50ms intervals.
1129 static ide_startstop_t do_reset1 (ide_drive_t *drive, int do_not_try_atapi)
1132 unsigned long flags;
1134 ide_hwgroup_t *hwgroup;
1136 spin_lock_irqsave(&ide_lock, flags);
1138 hwgroup = HWGROUP(drive);
1140 /* We must not reset with running handlers */
1141 BUG_ON(hwgroup->handler != NULL);
1143 /* For an ATAPI device, first try an ATAPI SRST. */
1144 if (drive->media != ide_disk && !do_not_try_atapi) {
1146 SELECT_DRIVE(drive);
1148 hwif->OUTBSYNC(drive, WIN_SRST, IDE_COMMAND_REG);
1150 hwgroup->poll_timeout = jiffies + WAIT_WORSTCASE;
1151 hwgroup->polling = 1;
1152 __ide_set_handler(drive, &atapi_reset_pollfunc, HZ/20, NULL);
1153 spin_unlock_irqrestore(&ide_lock, flags);
1158 * First, reset any device state data we were maintaining
1159 * for any of the drives on this interface.
1161 for (unit = 0; unit < MAX_DRIVES; ++unit)
1162 pre_reset(&hwif->drives[unit]);
1164 #if OK_TO_RESET_CONTROLLER
1165 if (!IDE_CONTROL_REG) {
1166 spin_unlock_irqrestore(&ide_lock, flags);
1171 * Note that we also set nIEN while resetting the device,
1172 * to mask unwanted interrupts from the interface during the reset.
1173 * However, due to the design of PC hardware, this will cause an
1174 * immediate interrupt due to the edge transition it produces.
1175 * This single interrupt gives us a "fast poll" for drives that
1176 * recover from reset very quickly, saving us the first 50ms wait time.
1178 /* set SRST and nIEN */
1179 hwif->OUTBSYNC(drive, drive->ctl|6,IDE_CONTROL_REG);
1180 /* more than enough time */
1182 if (drive->quirk_list == 2) {
1183 /* clear SRST and nIEN */
1184 hwif->OUTBSYNC(drive, drive->ctl, IDE_CONTROL_REG);
1186 /* clear SRST, leave nIEN */
1187 hwif->OUTBSYNC(drive, drive->ctl|2, IDE_CONTROL_REG);
1189 /* more than enough time */
1191 hwgroup->poll_timeout = jiffies + WAIT_WORSTCASE;
1192 hwgroup->polling = 1;
1193 __ide_set_handler(drive, &reset_pollfunc, HZ/20, NULL);
1196 * Some weird controller like resetting themselves to a strange
1197 * state when the disks are reset this way. At least, the Winbond
1198 * 553 documentation says that
1200 if (hwif->resetproc != NULL) {
1201 hwif->resetproc(drive);
1204 #endif /* OK_TO_RESET_CONTROLLER */
1206 spin_unlock_irqrestore(&ide_lock, flags);
1211 * ide_do_reset() is the entry point to the drive/interface reset code.
1214 ide_startstop_t ide_do_reset (ide_drive_t *drive)
1216 return do_reset1(drive, 0);
1219 EXPORT_SYMBOL(ide_do_reset);
1222 * ide_wait_not_busy() waits for the currently selected device on the hwif
1223 * to report a non-busy status, see comments in probe_hwif().
1225 int ide_wait_not_busy(ide_hwif_t *hwif, unsigned long timeout)
1231 * Turn this into a schedule() sleep once I'm sure
1232 * about locking issues (2.5 work ?).
1235 stat = hwif->INB(hwif->io_ports[IDE_STATUS_OFFSET]);
1236 if ((stat & BUSY_STAT) == 0)
1239 * Assume a value of 0xff means nothing is connected to
1240 * the interface and it doesn't implement the pull-down
1245 touch_softlockup_watchdog();
1250 EXPORT_SYMBOL_GPL(ide_wait_not_busy);