blob: 18cf3a66a1a327f2405124953fca468a826bc055 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/drivers/ide/ide-iops.c Version 0.37 Mar 05, 2003
3 *
4 * Copyright (C) 2000-2002 Andre Hedrick <andre@linux-ide.org>
5 * Copyright (C) 2003 Red Hat <alan@redhat.com>
6 *
7 */
8
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#include <linux/module.h>
10#include <linux/types.h>
11#include <linux/string.h>
12#include <linux/kernel.h>
13#include <linux/timer.h>
14#include <linux/mm.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>
Michal Schmidt1e862402006-07-30 03:03:29 -070026#include <linux/nmi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
28#include <asm/byteorder.h>
29#include <asm/irq.h>
30#include <asm/uaccess.h>
31#include <asm/io.h>
32
33/*
34 * Conventional PIO operations for ATA devices
35 */
36
37static u8 ide_inb (unsigned long port)
38{
39 return (u8) inb(port);
40}
41
42static u16 ide_inw (unsigned long port)
43{
44 return (u16) inw(port);
45}
46
47static void ide_insw (unsigned long port, void *addr, u32 count)
48{
49 insw(port, addr, count);
50}
51
Linus Torvalds1da177e2005-04-16 15:20:36 -070052static void ide_insl (unsigned long port, void *addr, u32 count)
53{
54 insl(port, addr, count);
55}
56
57static void ide_outb (u8 val, unsigned long port)
58{
59 outb(val, port);
60}
61
62static void ide_outbsync (ide_drive_t *drive, u8 addr, unsigned long port)
63{
64 outb(addr, port);
65}
66
67static void ide_outw (u16 val, unsigned long port)
68{
69 outw(val, port);
70}
71
72static void ide_outsw (unsigned long port, void *addr, u32 count)
73{
74 outsw(port, addr, count);
75}
76
Linus Torvalds1da177e2005-04-16 15:20:36 -070077static void ide_outsl (unsigned long port, void *addr, u32 count)
78{
79 outsl(port, addr, count);
80}
81
82void default_hwif_iops (ide_hwif_t *hwif)
83{
84 hwif->OUTB = ide_outb;
85 hwif->OUTBSYNC = ide_outbsync;
86 hwif->OUTW = ide_outw;
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 hwif->OUTSW = ide_outsw;
88 hwif->OUTSL = ide_outsl;
89 hwif->INB = ide_inb;
90 hwif->INW = ide_inw;
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 hwif->INSW = ide_insw;
92 hwif->INSL = ide_insl;
93}
94
Linus Torvalds1da177e2005-04-16 15:20:36 -070095/*
96 * MMIO operations, typically used for SATA controllers
97 */
98
99static u8 ide_mm_inb (unsigned long port)
100{
101 return (u8) readb((void __iomem *) port);
102}
103
104static u16 ide_mm_inw (unsigned long port)
105{
106 return (u16) readw((void __iomem *) port);
107}
108
109static void ide_mm_insw (unsigned long port, void *addr, u32 count)
110{
111 __ide_mm_insw((void __iomem *) port, addr, count);
112}
113
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114static void ide_mm_insl (unsigned long port, void *addr, u32 count)
115{
116 __ide_mm_insl((void __iomem *) port, addr, count);
117}
118
119static void ide_mm_outb (u8 value, unsigned long port)
120{
121 writeb(value, (void __iomem *) port);
122}
123
124static void ide_mm_outbsync (ide_drive_t *drive, u8 value, unsigned long port)
125{
126 writeb(value, (void __iomem *) port);
127}
128
129static void ide_mm_outw (u16 value, unsigned long port)
130{
131 writew(value, (void __iomem *) port);
132}
133
134static void ide_mm_outsw (unsigned long port, void *addr, u32 count)
135{
136 __ide_mm_outsw((void __iomem *) port, addr, count);
137}
138
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139static void ide_mm_outsl (unsigned long port, void *addr, u32 count)
140{
141 __ide_mm_outsl((void __iomem *) port, addr, count);
142}
143
144void default_hwif_mmiops (ide_hwif_t *hwif)
145{
146 hwif->OUTB = ide_mm_outb;
147 /* Most systems will need to override OUTBSYNC, alas however
148 this one is controller specific! */
149 hwif->OUTBSYNC = ide_mm_outbsync;
150 hwif->OUTW = ide_mm_outw;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 hwif->OUTSW = ide_mm_outsw;
152 hwif->OUTSL = ide_mm_outsl;
153 hwif->INB = ide_mm_inb;
154 hwif->INW = ide_mm_inw;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 hwif->INSW = ide_mm_insw;
156 hwif->INSL = ide_mm_insl;
157}
158
159EXPORT_SYMBOL(default_hwif_mmiops);
160
161u32 ide_read_24 (ide_drive_t *drive)
162{
163 u8 hcyl = HWIF(drive)->INB(IDE_HCYL_REG);
164 u8 lcyl = HWIF(drive)->INB(IDE_LCYL_REG);
165 u8 sect = HWIF(drive)->INB(IDE_SECTOR_REG);
166 return (hcyl<<16)|(lcyl<<8)|sect;
167}
168
169void SELECT_DRIVE (ide_drive_t *drive)
170{
171 if (HWIF(drive)->selectproc)
172 HWIF(drive)->selectproc(drive);
173 HWIF(drive)->OUTB(drive->select.all, IDE_SELECT_REG);
174}
175
176EXPORT_SYMBOL(SELECT_DRIVE);
177
178void SELECT_INTERRUPT (ide_drive_t *drive)
179{
180 if (HWIF(drive)->intrproc)
181 HWIF(drive)->intrproc(drive);
182 else
183 HWIF(drive)->OUTB(drive->ctl|2, IDE_CONTROL_REG);
184}
185
186void SELECT_MASK (ide_drive_t *drive, int mask)
187{
188 if (HWIF(drive)->maskproc)
189 HWIF(drive)->maskproc(drive, mask);
190}
191
192void QUIRK_LIST (ide_drive_t *drive)
193{
194 if (HWIF(drive)->quirkproc)
195 drive->quirk_list = HWIF(drive)->quirkproc(drive);
196}
197
198/*
199 * Some localbus EIDE interfaces require a special access sequence
200 * when using 32-bit I/O instructions to transfer data. We call this
201 * the "vlb_sync" sequence, which consists of three successive reads
202 * of the sector count register location, with interrupts disabled
203 * to ensure that the reads all happen together.
204 */
205static void ata_vlb_sync(ide_drive_t *drive, unsigned long port)
206{
207 (void) HWIF(drive)->INB(port);
208 (void) HWIF(drive)->INB(port);
209 (void) HWIF(drive)->INB(port);
210}
211
212/*
213 * This is used for most PIO data transfers *from* the IDE interface
214 */
215static void ata_input_data(ide_drive_t *drive, void *buffer, u32 wcount)
216{
217 ide_hwif_t *hwif = HWIF(drive);
218 u8 io_32bit = drive->io_32bit;
219
220 if (io_32bit) {
221 if (io_32bit & 2) {
222 unsigned long flags;
223 local_irq_save(flags);
224 ata_vlb_sync(drive, IDE_NSECTOR_REG);
225 hwif->INSL(IDE_DATA_REG, buffer, wcount);
226 local_irq_restore(flags);
227 } else
228 hwif->INSL(IDE_DATA_REG, buffer, wcount);
229 } else {
230 hwif->INSW(IDE_DATA_REG, buffer, wcount<<1);
231 }
232}
233
234/*
235 * This is used for most PIO data transfers *to* the IDE interface
236 */
237static void ata_output_data(ide_drive_t *drive, void *buffer, u32 wcount)
238{
239 ide_hwif_t *hwif = HWIF(drive);
240 u8 io_32bit = drive->io_32bit;
241
242 if (io_32bit) {
243 if (io_32bit & 2) {
244 unsigned long flags;
245 local_irq_save(flags);
246 ata_vlb_sync(drive, IDE_NSECTOR_REG);
247 hwif->OUTSL(IDE_DATA_REG, buffer, wcount);
248 local_irq_restore(flags);
249 } else
250 hwif->OUTSL(IDE_DATA_REG, buffer, wcount);
251 } else {
252 hwif->OUTSW(IDE_DATA_REG, buffer, wcount<<1);
253 }
254}
255
256/*
257 * The following routines are mainly used by the ATAPI drivers.
258 *
259 * These routines will round up any request for an odd number of bytes,
260 * so if an odd bytecount is specified, be sure that there's at least one
261 * extra byte allocated for the buffer.
262 */
263
264static void atapi_input_bytes(ide_drive_t *drive, void *buffer, u32 bytecount)
265{
266 ide_hwif_t *hwif = HWIF(drive);
267
268 ++bytecount;
269#if defined(CONFIG_ATARI) || defined(CONFIG_Q40)
270 if (MACH_IS_ATARI || MACH_IS_Q40) {
271 /* Atari has a byte-swapped IDE interface */
272 insw_swapw(IDE_DATA_REG, buffer, bytecount / 2);
273 return;
274 }
275#endif /* CONFIG_ATARI || CONFIG_Q40 */
276 hwif->ata_input_data(drive, buffer, bytecount / 4);
277 if ((bytecount & 0x03) >= 2)
278 hwif->INSW(IDE_DATA_REG, ((u8 *)buffer)+(bytecount & ~0x03), 1);
279}
280
281static void atapi_output_bytes(ide_drive_t *drive, void *buffer, u32 bytecount)
282{
283 ide_hwif_t *hwif = HWIF(drive);
284
285 ++bytecount;
286#if defined(CONFIG_ATARI) || defined(CONFIG_Q40)
287 if (MACH_IS_ATARI || MACH_IS_Q40) {
288 /* Atari has a byte-swapped IDE interface */
289 outsw_swapw(IDE_DATA_REG, buffer, bytecount / 2);
290 return;
291 }
292#endif /* CONFIG_ATARI || CONFIG_Q40 */
293 hwif->ata_output_data(drive, buffer, bytecount / 4);
294 if ((bytecount & 0x03) >= 2)
295 hwif->OUTSW(IDE_DATA_REG, ((u8*)buffer)+(bytecount & ~0x03), 1);
296}
297
298void default_hwif_transport(ide_hwif_t *hwif)
299{
300 hwif->ata_input_data = ata_input_data;
301 hwif->ata_output_data = ata_output_data;
302 hwif->atapi_input_bytes = atapi_input_bytes;
303 hwif->atapi_output_bytes = atapi_output_bytes;
304}
305
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306/*
307 * Beginning of Taskfile OPCODE Library and feature sets.
308 */
309void ide_fix_driveid (struct hd_driveid *id)
310{
311#ifndef __LITTLE_ENDIAN
312# ifdef __BIG_ENDIAN
313 int i;
314 u16 *stringcast;
315
316 id->config = __le16_to_cpu(id->config);
317 id->cyls = __le16_to_cpu(id->cyls);
318 id->reserved2 = __le16_to_cpu(id->reserved2);
319 id->heads = __le16_to_cpu(id->heads);
320 id->track_bytes = __le16_to_cpu(id->track_bytes);
321 id->sector_bytes = __le16_to_cpu(id->sector_bytes);
322 id->sectors = __le16_to_cpu(id->sectors);
323 id->vendor0 = __le16_to_cpu(id->vendor0);
324 id->vendor1 = __le16_to_cpu(id->vendor1);
325 id->vendor2 = __le16_to_cpu(id->vendor2);
326 stringcast = (u16 *)&id->serial_no[0];
327 for (i = 0; i < (20/2); i++)
328 stringcast[i] = __le16_to_cpu(stringcast[i]);
329 id->buf_type = __le16_to_cpu(id->buf_type);
330 id->buf_size = __le16_to_cpu(id->buf_size);
331 id->ecc_bytes = __le16_to_cpu(id->ecc_bytes);
332 stringcast = (u16 *)&id->fw_rev[0];
333 for (i = 0; i < (8/2); i++)
334 stringcast[i] = __le16_to_cpu(stringcast[i]);
335 stringcast = (u16 *)&id->model[0];
336 for (i = 0; i < (40/2); i++)
337 stringcast[i] = __le16_to_cpu(stringcast[i]);
338 id->dword_io = __le16_to_cpu(id->dword_io);
339 id->reserved50 = __le16_to_cpu(id->reserved50);
340 id->field_valid = __le16_to_cpu(id->field_valid);
341 id->cur_cyls = __le16_to_cpu(id->cur_cyls);
342 id->cur_heads = __le16_to_cpu(id->cur_heads);
343 id->cur_sectors = __le16_to_cpu(id->cur_sectors);
344 id->cur_capacity0 = __le16_to_cpu(id->cur_capacity0);
345 id->cur_capacity1 = __le16_to_cpu(id->cur_capacity1);
346 id->lba_capacity = __le32_to_cpu(id->lba_capacity);
347 id->dma_1word = __le16_to_cpu(id->dma_1word);
348 id->dma_mword = __le16_to_cpu(id->dma_mword);
349 id->eide_pio_modes = __le16_to_cpu(id->eide_pio_modes);
350 id->eide_dma_min = __le16_to_cpu(id->eide_dma_min);
351 id->eide_dma_time = __le16_to_cpu(id->eide_dma_time);
352 id->eide_pio = __le16_to_cpu(id->eide_pio);
353 id->eide_pio_iordy = __le16_to_cpu(id->eide_pio_iordy);
354 for (i = 0; i < 2; ++i)
355 id->words69_70[i] = __le16_to_cpu(id->words69_70[i]);
356 for (i = 0; i < 4; ++i)
357 id->words71_74[i] = __le16_to_cpu(id->words71_74[i]);
358 id->queue_depth = __le16_to_cpu(id->queue_depth);
359 for (i = 0; i < 4; ++i)
360 id->words76_79[i] = __le16_to_cpu(id->words76_79[i]);
361 id->major_rev_num = __le16_to_cpu(id->major_rev_num);
362 id->minor_rev_num = __le16_to_cpu(id->minor_rev_num);
363 id->command_set_1 = __le16_to_cpu(id->command_set_1);
364 id->command_set_2 = __le16_to_cpu(id->command_set_2);
365 id->cfsse = __le16_to_cpu(id->cfsse);
366 id->cfs_enable_1 = __le16_to_cpu(id->cfs_enable_1);
367 id->cfs_enable_2 = __le16_to_cpu(id->cfs_enable_2);
368 id->csf_default = __le16_to_cpu(id->csf_default);
369 id->dma_ultra = __le16_to_cpu(id->dma_ultra);
370 id->trseuc = __le16_to_cpu(id->trseuc);
371 id->trsEuc = __le16_to_cpu(id->trsEuc);
372 id->CurAPMvalues = __le16_to_cpu(id->CurAPMvalues);
373 id->mprc = __le16_to_cpu(id->mprc);
374 id->hw_config = __le16_to_cpu(id->hw_config);
375 id->acoustic = __le16_to_cpu(id->acoustic);
376 id->msrqs = __le16_to_cpu(id->msrqs);
377 id->sxfert = __le16_to_cpu(id->sxfert);
378 id->sal = __le16_to_cpu(id->sal);
379 id->spg = __le32_to_cpu(id->spg);
380 id->lba_capacity_2 = __le64_to_cpu(id->lba_capacity_2);
381 for (i = 0; i < 22; i++)
382 id->words104_125[i] = __le16_to_cpu(id->words104_125[i]);
383 id->last_lun = __le16_to_cpu(id->last_lun);
384 id->word127 = __le16_to_cpu(id->word127);
385 id->dlf = __le16_to_cpu(id->dlf);
386 id->csfo = __le16_to_cpu(id->csfo);
387 for (i = 0; i < 26; i++)
388 id->words130_155[i] = __le16_to_cpu(id->words130_155[i]);
389 id->word156 = __le16_to_cpu(id->word156);
390 for (i = 0; i < 3; i++)
391 id->words157_159[i] = __le16_to_cpu(id->words157_159[i]);
392 id->cfa_power = __le16_to_cpu(id->cfa_power);
393 for (i = 0; i < 14; i++)
394 id->words161_175[i] = __le16_to_cpu(id->words161_175[i]);
395 for (i = 0; i < 31; i++)
396 id->words176_205[i] = __le16_to_cpu(id->words176_205[i]);
397 for (i = 0; i < 48; i++)
398 id->words206_254[i] = __le16_to_cpu(id->words206_254[i]);
399 id->integrity_word = __le16_to_cpu(id->integrity_word);
400# else
401# error "Please fix <asm/byteorder.h>"
402# endif
403#endif
404}
405
406/* FIXME: exported for use by the USB storage (isd200.c) code only */
407EXPORT_SYMBOL(ide_fix_driveid);
408
409void ide_fixstring (u8 *s, const int bytecount, const int byteswap)
410{
411 u8 *p = s, *end = &s[bytecount & ~1]; /* bytecount must be even */
412
413 if (byteswap) {
414 /* convert from big-endian to host byte order */
415 for (p = end ; p != s;) {
416 unsigned short *pp = (unsigned short *) (p -= 2);
417 *pp = ntohs(*pp);
418 }
419 }
420 /* strip leading blanks */
421 while (s != end && *s == ' ')
422 ++s;
423 /* compress internal blanks and strip trailing blanks */
424 while (s != end && *s) {
425 if (*s++ != ' ' || (s != end && *s && *s != ' '))
426 *p++ = *(s-1);
427 }
428 /* wipe out trailing garbage */
429 while (p != end)
430 *p++ = '\0';
431}
432
433EXPORT_SYMBOL(ide_fixstring);
434
435/*
436 * Needed for PCI irq sharing
437 */
438int drive_is_ready (ide_drive_t *drive)
439{
440 ide_hwif_t *hwif = HWIF(drive);
441 u8 stat = 0;
442
443 if (drive->waiting_for_dma)
444 return hwif->ide_dma_test_irq(drive);
445
446#if 0
447 /* need to guarantee 400ns since last command was issued */
448 udelay(1);
449#endif
450
451#ifdef CONFIG_IDEPCI_SHARE_IRQ
452 /*
453 * We do a passive status test under shared PCI interrupts on
454 * cards that truly share the ATA side interrupt, but may also share
455 * an interrupt with another pci card/device. We make no assumptions
456 * about possible isa-pnp and pci-pnp issues yet.
457 */
458 if (IDE_CONTROL_REG)
459 stat = hwif->INB(IDE_ALTSTATUS_REG);
460 else
461#endif /* CONFIG_IDEPCI_SHARE_IRQ */
462 /* Note: this may clear a pending IRQ!! */
463 stat = hwif->INB(IDE_STATUS_REG);
464
465 if (stat & BUSY_STAT)
466 /* drive busy: definitely not interrupting */
467 return 0;
468
469 /* drive ready: *might* be interrupting */
470 return 1;
471}
472
473EXPORT_SYMBOL(drive_is_ready);
474
475/*
476 * Global for All, and taken from ide-pmac.c. Can be called
477 * with spinlock held & IRQs disabled, so don't schedule !
478 */
479int wait_for_ready (ide_drive_t *drive, int timeout)
480{
481 ide_hwif_t *hwif = HWIF(drive);
482 u8 stat = 0;
483
484 while(--timeout) {
485 stat = hwif->INB(IDE_STATUS_REG);
486 if (!(stat & BUSY_STAT)) {
487 if (drive->ready_stat == 0)
488 break;
489 else if ((stat & drive->ready_stat)||(stat & ERR_STAT))
490 break;
491 }
492 mdelay(1);
493 }
494 if ((stat & ERR_STAT) || timeout <= 0) {
495 if (stat & ERR_STAT) {
496 printk(KERN_ERR "%s: wait_for_ready, "
497 "error status: %x\n", drive->name, stat);
498 }
499 return 1;
500 }
501 return 0;
502}
503
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504/*
505 * This routine busy-waits for the drive status to be not "busy".
506 * It then checks the status for all of the "good" bits and none
507 * of the "bad" bits, and if all is okay it returns 0. All other
508 * cases return 1 after invoking ide_error() -- caller should just return.
509 *
510 * This routine should get fixed to not hog the cpu during extra long waits..
511 * That could be done by busy-waiting for the first jiffy or two, and then
512 * setting a timer to wake up at half second intervals thereafter,
513 * until timeout is achieved, before timing out.
514 */
515int ide_wait_stat (ide_startstop_t *startstop, ide_drive_t *drive, u8 good, u8 bad, unsigned long timeout)
516{
517 ide_hwif_t *hwif = HWIF(drive);
518 u8 stat;
519 int i;
520 unsigned long flags;
521
522 /* bail early if we've exceeded max_failures */
523 if (drive->max_failures && (drive->failures > drive->max_failures)) {
524 *startstop = ide_stopped;
525 return 1;
526 }
527
528 udelay(1); /* spec allows drive 400ns to assert "BUSY" */
529 if ((stat = hwif->INB(IDE_STATUS_REG)) & BUSY_STAT) {
530 local_irq_set(flags);
531 timeout += jiffies;
532 while ((stat = hwif->INB(IDE_STATUS_REG)) & BUSY_STAT) {
533 if (time_after(jiffies, timeout)) {
534 /*
535 * One last read after the timeout in case
536 * heavy interrupt load made us not make any
537 * progress during the timeout..
538 */
539 stat = hwif->INB(IDE_STATUS_REG);
540 if (!(stat & BUSY_STAT))
541 break;
542
543 local_irq_restore(flags);
544 *startstop = ide_error(drive, "status timeout", stat);
545 return 1;
546 }
547 }
548 local_irq_restore(flags);
549 }
550 /*
551 * Allow status to settle, then read it again.
552 * A few rare drives vastly violate the 400ns spec here,
553 * so we'll wait up to 10usec for a "good" status
554 * rather than expensively fail things immediately.
555 * This fix courtesy of Matthew Faupel & Niccolo Rigacci.
556 */
557 for (i = 0; i < 10; i++) {
558 udelay(1);
559 if (OK_STAT((stat = hwif->INB(IDE_STATUS_REG)), good, bad))
560 return 0;
561 }
562 *startstop = ide_error(drive, "status error", stat);
563 return 1;
564}
565
566EXPORT_SYMBOL(ide_wait_stat);
567
Bartlomiej Zolnierkiewicza5b7e702007-08-20 22:42:56 +0200568/**
569 * ide_in_drive_list - look for drive in black/white list
570 * @id: drive identifier
571 * @drive_table: list to inspect
572 *
573 * Look for a drive in the blacklist and the whitelist tables
574 * Returns 1 if the drive is found in the table.
575 */
576
577int ide_in_drive_list(struct hd_driveid *id, const struct drive_list_entry *drive_table)
578{
579 for ( ; drive_table->id_model; drive_table++)
580 if ((!strcmp(drive_table->id_model, id->model)) &&
581 (!drive_table->id_firmware ||
582 strstr(id->fw_rev, drive_table->id_firmware)))
583 return 1;
584 return 0;
585}
586
587/*
588 * Early UDMA66 devices don't set bit14 to 1, only bit13 is valid.
589 * We list them here and depend on the device side cable detection for them.
590 */
591static const struct drive_list_entry ivb_list[] = {
592 { "QUANTUM FIREBALLlct10 05" , "A03.0900" },
593 { NULL , NULL }
594};
595
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596/*
597 * All hosts that use the 80c ribbon must use!
598 * The name is derived from upper byte of word 93 and the 80c ribbon.
599 */
600u8 eighty_ninty_three (ide_drive_t *drive)
601{
Bartlomiej Zolnierkiewicz7f8f48a2007-05-10 00:01:10 +0200602 ide_hwif_t *hwif = drive->hwif;
603 struct hd_driveid *id = drive->id;
Bartlomiej Zolnierkiewicza5b7e702007-08-20 22:42:56 +0200604 int ivb = ide_in_drive_list(id, ivb_list);
Bartlomiej Zolnierkiewicz7f8f48a2007-05-10 00:01:10 +0200605
Bartlomiej Zolnierkiewicz49521f92007-07-09 23:17:58 +0200606 if (hwif->cbl == ATA_CBL_PATA40_SHORT)
607 return 1;
608
Bartlomiej Zolnierkiewicza5b7e702007-08-20 22:42:56 +0200609 if (ivb)
610 printk(KERN_DEBUG "%s: skipping word 93 validity check\n",
611 drive->name);
612
613 if (hwif->cbl != ATA_CBL_PATA80 && !ivb)
Bartlomiej Zolnierkiewicz7f8f48a2007-05-10 00:01:10 +0200614 goto no_80w;
Alan Cox1a1276e2006-06-28 04:26:58 -0700615
616 /* Check for SATA but only if we are ATA5 or higher */
Bartlomiej Zolnierkiewicz7f8f48a2007-05-10 00:01:10 +0200617 if (id->hw_config == 0 && (id->major_rev_num & 0x7FE0))
Alan Cox1a1276e2006-06-28 04:26:58 -0700618 return 1;
Bartlomiej Zolnierkiewicz7f8f48a2007-05-10 00:01:10 +0200619
Bartlomiej Zolnierkiewiczf68d9322007-03-26 23:03:18 +0200620 /*
621 * FIXME:
622 * - change master/slave IDENTIFY order
Bartlomiej Zolnierkiewicza5b7e702007-08-20 22:42:56 +0200623 * - force bit13 (80c cable present) check also for !ivb devices
Bartlomiej Zolnierkiewiczf68d9322007-03-26 23:03:18 +0200624 * (unless the slave device is pre-ATA3)
625 */
Bartlomiej Zolnierkiewicz7f8f48a2007-05-10 00:01:10 +0200626#ifndef CONFIG_IDEDMA_IVB
Bartlomiej Zolnierkiewicza5b7e702007-08-20 22:42:56 +0200627 if ((id->hw_config & 0x4000) || (ivb && (id->hw_config & 0x2000)))
Bartlomiej Zolnierkiewicz7f8f48a2007-05-10 00:01:10 +0200628#else
629 if (id->hw_config & 0x6000)
630#endif
631 return 1;
632
633no_80w:
634 if (drive->udma33_warned == 1)
635 return 0;
636
637 printk(KERN_WARNING "%s: %s side 80-wire cable detection failed, "
638 "limiting max speed to UDMA33\n",
Bartlomiej Zolnierkiewicz49521f92007-07-09 23:17:58 +0200639 drive->name,
640 hwif->cbl == ATA_CBL_PATA80 ? "drive" : "host");
Bartlomiej Zolnierkiewicz7f8f48a2007-05-10 00:01:10 +0200641
642 drive->udma33_warned = 1;
643
644 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645}
646
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647int ide_ata66_check (ide_drive_t *drive, ide_task_t *args)
648{
649 if ((args->tfRegister[IDE_COMMAND_OFFSET] == WIN_SETFEATURES) &&
650 (args->tfRegister[IDE_SECTOR_OFFSET] > XFER_UDMA_2) &&
651 (args->tfRegister[IDE_FEATURE_OFFSET] == SETFEATURES_XFER)) {
Bartlomiej Zolnierkiewicz7f8f48a2007-05-10 00:01:10 +0200652 if (eighty_ninty_three(drive) == 0) {
653 printk(KERN_WARNING "%s: UDMA speeds >UDMA33 cannot "
654 "be set\n", drive->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 return 1;
656 }
657 }
Bartlomiej Zolnierkiewicz7f8f48a2007-05-10 00:01:10 +0200658
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 return 0;
660}
661
662/*
663 * Backside of HDIO_DRIVE_CMD call of SETFEATURES_XFER.
664 * 1 : Safe to update drive->id DMA registers.
665 * 0 : OOPs not allowed.
666 */
667int set_transfer (ide_drive_t *drive, ide_task_t *args)
668{
669 if ((args->tfRegister[IDE_COMMAND_OFFSET] == WIN_SETFEATURES) &&
670 (args->tfRegister[IDE_SECTOR_OFFSET] >= XFER_SW_DMA_0) &&
671 (args->tfRegister[IDE_FEATURE_OFFSET] == SETFEATURES_XFER) &&
672 (drive->id->dma_ultra ||
673 drive->id->dma_mword ||
674 drive->id->dma_1word))
675 return 1;
676
677 return 0;
678}
679
680#ifdef CONFIG_BLK_DEV_IDEDMA
681static u8 ide_auto_reduce_xfer (ide_drive_t *drive)
682{
683 if (!drive->crc_count)
684 return drive->current_speed;
685 drive->crc_count = 0;
686
687 switch(drive->current_speed) {
688 case XFER_UDMA_7: return XFER_UDMA_6;
689 case XFER_UDMA_6: return XFER_UDMA_5;
690 case XFER_UDMA_5: return XFER_UDMA_4;
691 case XFER_UDMA_4: return XFER_UDMA_3;
692 case XFER_UDMA_3: return XFER_UDMA_2;
693 case XFER_UDMA_2: return XFER_UDMA_1;
694 case XFER_UDMA_1: return XFER_UDMA_0;
695 /*
696 * OOPS we do not goto non Ultra DMA modes
697 * without iCRC's available we force
698 * the system to PIO and make the user
699 * invoke the ATA-1 ATA-2 DMA modes.
700 */
701 case XFER_UDMA_0:
702 default: return XFER_PIO_4;
703 }
704}
705#endif /* CONFIG_BLK_DEV_IDEDMA */
706
707/*
708 * Update the
709 */
710int ide_driveid_update (ide_drive_t *drive)
711{
712 ide_hwif_t *hwif = HWIF(drive);
713 struct hd_driveid *id;
714#if 0
715 id = kmalloc(SECTOR_WORDS*4, GFP_ATOMIC);
716 if (!id)
717 return 0;
718
719 taskfile_lib_get_identify(drive, (char *)&id);
720
721 ide_fix_driveid(id);
722 if (id) {
723 drive->id->dma_ultra = id->dma_ultra;
724 drive->id->dma_mword = id->dma_mword;
725 drive->id->dma_1word = id->dma_1word;
726 /* anything more ? */
727 kfree(id);
728 }
729 return 1;
730#else
731 /*
732 * Re-read drive->id for possible DMA mode
733 * change (copied from ide-probe.c)
734 */
735 unsigned long timeout, flags;
736
737 SELECT_MASK(drive, 1);
738 if (IDE_CONTROL_REG)
739 hwif->OUTB(drive->ctl,IDE_CONTROL_REG);
740 msleep(50);
741 hwif->OUTB(WIN_IDENTIFY, IDE_COMMAND_REG);
742 timeout = jiffies + WAIT_WORSTCASE;
743 do {
744 if (time_after(jiffies, timeout)) {
745 SELECT_MASK(drive, 0);
746 return 0; /* drive timed-out */
747 }
748 msleep(50); /* give drive a breather */
749 } while (hwif->INB(IDE_ALTSTATUS_REG) & BUSY_STAT);
750 msleep(50); /* wait for IRQ and DRQ_STAT */
751 if (!OK_STAT(hwif->INB(IDE_STATUS_REG),DRQ_STAT,BAD_R_STAT)) {
752 SELECT_MASK(drive, 0);
753 printk("%s: CHECK for good STATUS\n", drive->name);
754 return 0;
755 }
756 local_irq_save(flags);
757 SELECT_MASK(drive, 0);
758 id = kmalloc(SECTOR_WORDS*4, GFP_ATOMIC);
759 if (!id) {
760 local_irq_restore(flags);
761 return 0;
762 }
763 ata_input_data(drive, id, SECTOR_WORDS);
764 (void) hwif->INB(IDE_STATUS_REG); /* clear drive IRQ */
765 local_irq_enable();
766 local_irq_restore(flags);
767 ide_fix_driveid(id);
768 if (id) {
769 drive->id->dma_ultra = id->dma_ultra;
770 drive->id->dma_mword = id->dma_mword;
771 drive->id->dma_1word = id->dma_1word;
772 /* anything more ? */
773 kfree(id);
774 }
775
776 return 1;
777#endif
778}
779
780/*
781 * Similar to ide_wait_stat(), except it never calls ide_error internally.
782 * This is a kludge to handle the new ide_config_drive_speed() function,
783 * and should not otherwise be used anywhere. Eventually, the tuneproc's
784 * should be updated to return ide_startstop_t, in which case we can get
785 * rid of this abomination again. :) -ml
786 *
787 * It is gone..........
788 *
789 * const char *msg == consider adding for verbose errors.
790 */
791int ide_config_drive_speed (ide_drive_t *drive, u8 speed)
792{
793 ide_hwif_t *hwif = HWIF(drive);
794 int i, error = 1;
795 u8 stat;
796
797// while (HWGROUP(drive)->busy)
798// msleep(50);
799
800#ifdef CONFIG_BLK_DEV_IDEDMA
801 if (hwif->ide_dma_check) /* check if host supports DMA */
Bartlomiej Zolnierkiewicz7469aaf2007-02-17 02:40:26 +0100802 hwif->dma_host_off(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803#endif
804
805 /*
806 * Don't use ide_wait_cmd here - it will
807 * attempt to set_geometry and recalibrate,
808 * but for some reason these don't work at
809 * this point (lost interrupt).
810 */
811 /*
812 * Select the drive, and issue the SETFEATURES command
813 */
814 disable_irq_nosync(hwif->irq);
815
816 /*
817 * FIXME: we race against the running IRQ here if
818 * this is called from non IRQ context. If we use
819 * disable_irq() we hang on the error path. Work
820 * is needed.
821 */
822
823 udelay(1);
824 SELECT_DRIVE(drive);
825 SELECT_MASK(drive, 0);
826 udelay(1);
827 if (IDE_CONTROL_REG)
828 hwif->OUTB(drive->ctl | 2, IDE_CONTROL_REG);
829 hwif->OUTB(speed, IDE_NSECTOR_REG);
830 hwif->OUTB(SETFEATURES_XFER, IDE_FEATURE_REG);
Bartlomiej Zolnierkiewiczadcd33d2007-08-20 22:42:56 +0200831 hwif->OUTBSYNC(drive, WIN_SETFEATURES, IDE_COMMAND_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 if ((IDE_CONTROL_REG) && (drive->quirk_list == 2))
833 hwif->OUTB(drive->ctl, IDE_CONTROL_REG);
834 udelay(1);
835 /*
836 * Wait for drive to become non-BUSY
837 */
838 if ((stat = hwif->INB(IDE_STATUS_REG)) & BUSY_STAT) {
839 unsigned long flags, timeout;
840 local_irq_set(flags);
841 timeout = jiffies + WAIT_CMD;
842 while ((stat = hwif->INB(IDE_STATUS_REG)) & BUSY_STAT) {
843 if (time_after(jiffies, timeout))
844 break;
845 }
846 local_irq_restore(flags);
847 }
848
849 /*
850 * Allow status to settle, then read it again.
851 * A few rare drives vastly violate the 400ns spec here,
852 * so we'll wait up to 10usec for a "good" status
853 * rather than expensively fail things immediately.
854 * This fix courtesy of Matthew Faupel & Niccolo Rigacci.
855 */
856 for (i = 0; i < 10; i++) {
857 udelay(1);
Bartlomiej Zolnierkiewiczadcd33d2007-08-20 22:42:56 +0200858 if (OK_STAT((stat = hwif->INB(IDE_STATUS_REG)), drive->ready_stat, BUSY_STAT|DRQ_STAT|ERR_STAT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 error = 0;
860 break;
861 }
862 }
863
864 SELECT_MASK(drive, 0);
865
866 enable_irq(hwif->irq);
867
868 if (error) {
869 (void) ide_dump_status(drive, "set_drive_speed_status", stat);
870 return error;
871 }
872
873 drive->id->dma_ultra &= ~0xFF00;
874 drive->id->dma_mword &= ~0x0F00;
875 drive->id->dma_1word &= ~0x0F00;
876
877#ifdef CONFIG_BLK_DEV_IDEDMA
878 if (speed >= XFER_SW_DMA_0)
Bartlomiej Zolnierkiewiczccf35282007-02-17 02:40:26 +0100879 hwif->dma_host_on(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 else if (hwif->ide_dma_check) /* check if host supports DMA */
Bartlomiej Zolnierkiewicz7469aaf2007-02-17 02:40:26 +0100881 hwif->dma_off_quietly(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882#endif
883
884 switch(speed) {
885 case XFER_UDMA_7: drive->id->dma_ultra |= 0x8080; break;
886 case XFER_UDMA_6: drive->id->dma_ultra |= 0x4040; break;
887 case XFER_UDMA_5: drive->id->dma_ultra |= 0x2020; break;
888 case XFER_UDMA_4: drive->id->dma_ultra |= 0x1010; break;
889 case XFER_UDMA_3: drive->id->dma_ultra |= 0x0808; break;
890 case XFER_UDMA_2: drive->id->dma_ultra |= 0x0404; break;
891 case XFER_UDMA_1: drive->id->dma_ultra |= 0x0202; break;
892 case XFER_UDMA_0: drive->id->dma_ultra |= 0x0101; break;
893 case XFER_MW_DMA_2: drive->id->dma_mword |= 0x0404; break;
894 case XFER_MW_DMA_1: drive->id->dma_mword |= 0x0202; break;
895 case XFER_MW_DMA_0: drive->id->dma_mword |= 0x0101; break;
896 case XFER_SW_DMA_2: drive->id->dma_1word |= 0x0404; break;
897 case XFER_SW_DMA_1: drive->id->dma_1word |= 0x0202; break;
898 case XFER_SW_DMA_0: drive->id->dma_1word |= 0x0101; break;
899 default: break;
900 }
901 if (!drive->init_speed)
902 drive->init_speed = speed;
903 drive->current_speed = speed;
904 return error;
905}
906
907EXPORT_SYMBOL(ide_config_drive_speed);
908
909
910/*
911 * This should get invoked any time we exit the driver to
912 * wait for an interrupt response from a drive. handler() points
913 * at the appropriate code to handle the next interrupt, and a
914 * timer is started to prevent us from waiting forever in case
915 * something goes wrong (see the ide_timer_expiry() handler later on).
916 *
917 * See also ide_execute_command
918 */
919static void __ide_set_handler (ide_drive_t *drive, ide_handler_t *handler,
920 unsigned int timeout, ide_expiry_t *expiry)
921{
922 ide_hwgroup_t *hwgroup = HWGROUP(drive);
923
924 if (hwgroup->handler != NULL) {
925 printk(KERN_CRIT "%s: ide_set_handler: handler not null; "
926 "old=%p, new=%p\n",
927 drive->name, hwgroup->handler, handler);
928 }
929 hwgroup->handler = handler;
930 hwgroup->expiry = expiry;
931 hwgroup->timer.expires = jiffies + timeout;
Suleiman Souhlal23450312007-04-10 22:38:37 +0200932 hwgroup->req_gen_timer = hwgroup->req_gen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 add_timer(&hwgroup->timer);
934}
935
936void ide_set_handler (ide_drive_t *drive, ide_handler_t *handler,
937 unsigned int timeout, ide_expiry_t *expiry)
938{
939 unsigned long flags;
940 spin_lock_irqsave(&ide_lock, flags);
941 __ide_set_handler(drive, handler, timeout, expiry);
942 spin_unlock_irqrestore(&ide_lock, flags);
943}
944
945EXPORT_SYMBOL(ide_set_handler);
946
947/**
948 * ide_execute_command - execute an IDE command
949 * @drive: IDE drive to issue the command against
950 * @command: command byte to write
951 * @handler: handler for next phase
952 * @timeout: timeout for command
953 * @expiry: handler to run on timeout
954 *
955 * Helper function to issue an IDE command. This handles the
956 * atomicity requirements, command timing and ensures that the
957 * handler and IRQ setup do not race. All IDE command kick off
958 * should go via this function or do equivalent locking.
959 */
960
961void ide_execute_command(ide_drive_t *drive, task_ioreg_t cmd, ide_handler_t *handler, unsigned timeout, ide_expiry_t *expiry)
962{
963 unsigned long flags;
964 ide_hwgroup_t *hwgroup = HWGROUP(drive);
965 ide_hwif_t *hwif = HWIF(drive);
966
967 spin_lock_irqsave(&ide_lock, flags);
968
Eric Sesterhenn125e1872006-06-23 02:06:06 -0700969 BUG_ON(hwgroup->handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 hwgroup->handler = handler;
971 hwgroup->expiry = expiry;
972 hwgroup->timer.expires = jiffies + timeout;
Suleiman Souhlal23450312007-04-10 22:38:37 +0200973 hwgroup->req_gen_timer = hwgroup->req_gen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 add_timer(&hwgroup->timer);
975 hwif->OUTBSYNC(drive, cmd, IDE_COMMAND_REG);
976 /* Drive takes 400nS to respond, we must avoid the IRQ being
977 serviced before that.
978
979 FIXME: we could skip this delay with care on non shared
980 devices
981 */
982 ndelay(400);
983 spin_unlock_irqrestore(&ide_lock, flags);
984}
985
986EXPORT_SYMBOL(ide_execute_command);
987
988
989/* needed below */
990static ide_startstop_t do_reset1 (ide_drive_t *, int);
991
992/*
993 * atapi_reset_pollfunc() gets invoked to poll the interface for completion every 50ms
994 * during an atapi drive reset operation. If the drive has not yet responded,
995 * and we have not yet hit our maximum waiting time, then the timer is restarted
996 * for another 50ms.
997 */
998static ide_startstop_t atapi_reset_pollfunc (ide_drive_t *drive)
999{
1000 ide_hwgroup_t *hwgroup = HWGROUP(drive);
1001 ide_hwif_t *hwif = HWIF(drive);
1002 u8 stat;
1003
1004 SELECT_DRIVE(drive);
1005 udelay (10);
1006
1007 if (OK_STAT(stat = hwif->INB(IDE_STATUS_REG), 0, BUSY_STAT)) {
1008 printk("%s: ATAPI reset complete\n", drive->name);
1009 } else {
1010 if (time_before(jiffies, hwgroup->poll_timeout)) {
Eric Sesterhenn125e1872006-06-23 02:06:06 -07001011 BUG_ON(HWGROUP(drive)->handler != NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 ide_set_handler(drive, &atapi_reset_pollfunc, HZ/20, NULL);
1013 /* continue polling */
1014 return ide_started;
1015 }
1016 /* end of polling */
1017 hwgroup->polling = 0;
1018 printk("%s: ATAPI reset timed-out, status=0x%02x\n",
1019 drive->name, stat);
1020 /* do it the old fashioned way */
1021 return do_reset1(drive, 1);
1022 }
1023 /* done polling */
1024 hwgroup->polling = 0;
Alan Cox913759a2006-10-03 01:14:33 -07001025 hwgroup->resetting = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 return ide_stopped;
1027}
1028
1029/*
1030 * reset_pollfunc() gets invoked to poll the interface for completion every 50ms
1031 * during an ide reset operation. If the drives have not yet responded,
1032 * and we have not yet hit our maximum waiting time, then the timer is restarted
1033 * for another 50ms.
1034 */
1035static ide_startstop_t reset_pollfunc (ide_drive_t *drive)
1036{
1037 ide_hwgroup_t *hwgroup = HWGROUP(drive);
1038 ide_hwif_t *hwif = HWIF(drive);
1039 u8 tmp;
1040
1041 if (hwif->reset_poll != NULL) {
1042 if (hwif->reset_poll(drive)) {
1043 printk(KERN_ERR "%s: host reset_poll failure for %s.\n",
1044 hwif->name, drive->name);
1045 return ide_stopped;
1046 }
1047 }
1048
1049 if (!OK_STAT(tmp = hwif->INB(IDE_STATUS_REG), 0, BUSY_STAT)) {
1050 if (time_before(jiffies, hwgroup->poll_timeout)) {
Eric Sesterhenn125e1872006-06-23 02:06:06 -07001051 BUG_ON(HWGROUP(drive)->handler != NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 ide_set_handler(drive, &reset_pollfunc, HZ/20, NULL);
1053 /* continue polling */
1054 return ide_started;
1055 }
1056 printk("%s: reset timed-out, status=0x%02x\n", hwif->name, tmp);
1057 drive->failures++;
1058 } else {
1059 printk("%s: reset: ", hwif->name);
1060 if ((tmp = hwif->INB(IDE_ERROR_REG)) == 1) {
1061 printk("success\n");
1062 drive->failures = 0;
1063 } else {
1064 drive->failures++;
1065 printk("master: ");
1066 switch (tmp & 0x7f) {
1067 case 1: printk("passed");
1068 break;
1069 case 2: printk("formatter device error");
1070 break;
1071 case 3: printk("sector buffer error");
1072 break;
1073 case 4: printk("ECC circuitry error");
1074 break;
1075 case 5: printk("controlling MPU error");
1076 break;
1077 default:printk("error (0x%02x?)", tmp);
1078 }
1079 if (tmp & 0x80)
1080 printk("; slave: failed");
1081 printk("\n");
1082 }
1083 }
1084 hwgroup->polling = 0; /* done polling */
Alan Cox913759a2006-10-03 01:14:33 -07001085 hwgroup->resetting = 0; /* done reset attempt */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 return ide_stopped;
1087}
1088
1089static void check_dma_crc(ide_drive_t *drive)
1090{
1091#ifdef CONFIG_BLK_DEV_IDEDMA
1092 if (drive->crc_count) {
Bartlomiej Zolnierkiewicz7469aaf2007-02-17 02:40:26 +01001093 drive->hwif->dma_off_quietly(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 ide_set_xfer_rate(drive, ide_auto_reduce_xfer(drive));
1095 if (drive->current_speed >= XFER_SW_DMA_0)
1096 (void) HWIF(drive)->ide_dma_on(drive);
1097 } else
Bartlomiej Zolnierkiewicz7469aaf2007-02-17 02:40:26 +01001098 ide_dma_off(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099#endif
1100}
1101
1102static void ide_disk_pre_reset(ide_drive_t *drive)
1103{
1104 int legacy = (drive->id->cfs_enable_2 & 0x0400) ? 0 : 1;
1105
1106 drive->special.all = 0;
1107 drive->special.b.set_geometry = legacy;
1108 drive->special.b.recalibrate = legacy;
1109 if (OK_TO_RESET_CONTROLLER)
1110 drive->mult_count = 0;
1111 if (!drive->keep_settings && !drive->using_dma)
1112 drive->mult_req = 0;
1113 if (drive->mult_req != drive->mult_count)
1114 drive->special.b.set_multmode = 1;
1115}
1116
1117static void pre_reset(ide_drive_t *drive)
1118{
1119 if (drive->media == ide_disk)
1120 ide_disk_pre_reset(drive);
1121 else
1122 drive->post_reset = 1;
1123
1124 if (!drive->keep_settings) {
1125 if (drive->using_dma) {
1126 check_dma_crc(drive);
1127 } else {
1128 drive->unmask = 0;
1129 drive->io_32bit = 0;
1130 }
1131 return;
1132 }
1133 if (drive->using_dma)
1134 check_dma_crc(drive);
1135
1136 if (HWIF(drive)->pre_reset != NULL)
1137 HWIF(drive)->pre_reset(drive);
1138
Suleiman Souhlal513daad2007-03-26 23:03:20 +02001139 if (drive->current_speed != 0xff)
1140 drive->desired_speed = drive->current_speed;
1141 drive->current_speed = 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142}
1143
1144/*
1145 * do_reset1() attempts to recover a confused drive by resetting it.
1146 * Unfortunately, resetting a disk drive actually resets all devices on
1147 * the same interface, so it can really be thought of as resetting the
1148 * interface rather than resetting the drive.
1149 *
1150 * ATAPI devices have their own reset mechanism which allows them to be
1151 * individually reset without clobbering other devices on the same interface.
1152 *
1153 * Unfortunately, the IDE interface does not generate an interrupt to let
1154 * us know when the reset operation has finished, so we must poll for this.
1155 * Equally poor, though, is the fact that this may a very long time to complete,
1156 * (up to 30 seconds worstcase). So, instead of busy-waiting here for it,
1157 * we set a timer to poll at 50ms intervals.
1158 */
1159static ide_startstop_t do_reset1 (ide_drive_t *drive, int do_not_try_atapi)
1160{
1161 unsigned int unit;
1162 unsigned long flags;
1163 ide_hwif_t *hwif;
1164 ide_hwgroup_t *hwgroup;
1165
1166 spin_lock_irqsave(&ide_lock, flags);
1167 hwif = HWIF(drive);
1168 hwgroup = HWGROUP(drive);
1169
1170 /* We must not reset with running handlers */
Eric Sesterhenn125e1872006-06-23 02:06:06 -07001171 BUG_ON(hwgroup->handler != NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172
1173 /* For an ATAPI device, first try an ATAPI SRST. */
1174 if (drive->media != ide_disk && !do_not_try_atapi) {
Alan Cox913759a2006-10-03 01:14:33 -07001175 hwgroup->resetting = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 pre_reset(drive);
1177 SELECT_DRIVE(drive);
1178 udelay (20);
Alan Cox68ad9912005-06-27 15:24:25 -07001179 hwif->OUTBSYNC(drive, WIN_SRST, IDE_COMMAND_REG);
1180 ndelay(400);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 hwgroup->poll_timeout = jiffies + WAIT_WORSTCASE;
1182 hwgroup->polling = 1;
1183 __ide_set_handler(drive, &atapi_reset_pollfunc, HZ/20, NULL);
1184 spin_unlock_irqrestore(&ide_lock, flags);
1185 return ide_started;
1186 }
1187
1188 /*
1189 * First, reset any device state data we were maintaining
1190 * for any of the drives on this interface.
1191 */
1192 for (unit = 0; unit < MAX_DRIVES; ++unit)
1193 pre_reset(&hwif->drives[unit]);
1194
1195#if OK_TO_RESET_CONTROLLER
1196 if (!IDE_CONTROL_REG) {
1197 spin_unlock_irqrestore(&ide_lock, flags);
1198 return ide_stopped;
1199 }
1200
Alan Cox913759a2006-10-03 01:14:33 -07001201 hwgroup->resetting = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 /*
1203 * Note that we also set nIEN while resetting the device,
1204 * to mask unwanted interrupts from the interface during the reset.
1205 * However, due to the design of PC hardware, this will cause an
1206 * immediate interrupt due to the edge transition it produces.
1207 * This single interrupt gives us a "fast poll" for drives that
1208 * recover from reset very quickly, saving us the first 50ms wait time.
1209 */
1210 /* set SRST and nIEN */
1211 hwif->OUTBSYNC(drive, drive->ctl|6,IDE_CONTROL_REG);
1212 /* more than enough time */
1213 udelay(10);
1214 if (drive->quirk_list == 2) {
1215 /* clear SRST and nIEN */
1216 hwif->OUTBSYNC(drive, drive->ctl, IDE_CONTROL_REG);
1217 } else {
1218 /* clear SRST, leave nIEN */
1219 hwif->OUTBSYNC(drive, drive->ctl|2, IDE_CONTROL_REG);
1220 }
1221 /* more than enough time */
1222 udelay(10);
1223 hwgroup->poll_timeout = jiffies + WAIT_WORSTCASE;
1224 hwgroup->polling = 1;
1225 __ide_set_handler(drive, &reset_pollfunc, HZ/20, NULL);
1226
1227 /*
1228 * Some weird controller like resetting themselves to a strange
1229 * state when the disks are reset this way. At least, the Winbond
1230 * 553 documentation says that
1231 */
1232 if (hwif->resetproc != NULL) {
1233 hwif->resetproc(drive);
1234 }
1235
1236#endif /* OK_TO_RESET_CONTROLLER */
1237
1238 spin_unlock_irqrestore(&ide_lock, flags);
1239 return ide_started;
1240}
1241
1242/*
1243 * ide_do_reset() is the entry point to the drive/interface reset code.
1244 */
1245
1246ide_startstop_t ide_do_reset (ide_drive_t *drive)
1247{
1248 return do_reset1(drive, 0);
1249}
1250
1251EXPORT_SYMBOL(ide_do_reset);
1252
1253/*
1254 * ide_wait_not_busy() waits for the currently selected device on the hwif
1255 * to report a non-busy status, see comments in probe_hwif().
1256 */
1257int ide_wait_not_busy(ide_hwif_t *hwif, unsigned long timeout)
1258{
1259 u8 stat = 0;
1260
1261 while(timeout--) {
1262 /*
1263 * Turn this into a schedule() sleep once I'm sure
1264 * about locking issues (2.5 work ?).
1265 */
1266 mdelay(1);
1267 stat = hwif->INB(hwif->io_ports[IDE_STATUS_OFFSET]);
1268 if ((stat & BUSY_STAT) == 0)
1269 return 0;
1270 /*
1271 * Assume a value of 0xff means nothing is connected to
1272 * the interface and it doesn't implement the pull-down
1273 * resistor on D7.
1274 */
1275 if (stat == 0xff)
1276 return -ENODEV;
Ingo Molnar6842f8c2006-02-03 03:04:55 -08001277 touch_softlockup_watchdog();
Michal Schmidt1e862402006-07-30 03:03:29 -07001278 touch_nmi_watchdog();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 }
1280 return -EBUSY;
1281}
1282
1283EXPORT_SYMBOL_GPL(ide_wait_not_busy);
1284