1 /*
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 */
9
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>
17 #include <linux/mm.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>
28
29 #include <asm/byteorder.h>
30 #include <asm/irq.h>
31 #include <asm/uaccess.h>
32 #include <asm/io.h>
33 #include <asm/bitops.h>
34
35 /*
36 * IDE operator we assign to an unplugged device so that
37 * we don't trash new hardware assigned the same resources
38 */
39
ide_unplugged_inb(unsigned long port)40 static u8 ide_unplugged_inb (unsigned long port)
41 {
42 return 0xFF;
43 }
44
ide_unplugged_inw(unsigned long port)45 static u16 ide_unplugged_inw (unsigned long port)
46 {
47 return 0xFFFF;
48 }
49
ide_unplugged_insw(unsigned long port,void * addr,u32 count)50 static void ide_unplugged_insw (unsigned long port, void *addr, u32 count)
51 {
52 }
53
ide_unplugged_inl(unsigned long port)54 static u32 ide_unplugged_inl (unsigned long port)
55 {
56 return 0xFFFFFFFF;
57 }
58
ide_unplugged_insl(unsigned long port,void * addr,u32 count)59 static void ide_unplugged_insl (unsigned long port, void *addr, u32 count)
60 {
61 }
62
ide_unplugged_outb(u8 addr,unsigned long port)63 static void ide_unplugged_outb (u8 addr, unsigned long port)
64 {
65 }
66
ide_unplugged_outbsync(ide_drive_t * drive,u8 addr,unsigned long port)67 static void ide_unplugged_outbsync (ide_drive_t *drive, u8 addr, unsigned long port)
68 {
69 }
70
ide_unplugged_outw(u16 addr,unsigned long port)71 static void ide_unplugged_outw (u16 addr, unsigned long port)
72 {
73 }
74
ide_unplugged_outsw(unsigned long port,void * addr,u32 count)75 static void ide_unplugged_outsw (unsigned long port, void *addr, u32 count)
76 {
77 }
78
ide_unplugged_outl(u32 addr,unsigned long port)79 static void ide_unplugged_outl (u32 addr, unsigned long port)
80 {
81 }
82
ide_unplugged_outsl(unsigned long port,void * addr,u32 count)83 static void ide_unplugged_outsl (unsigned long port, void *addr, u32 count)
84 {
85 }
86
unplugged_hwif_iops(ide_hwif_t * hwif)87 void unplugged_hwif_iops (ide_hwif_t *hwif)
88 {
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;
100 }
101
102 EXPORT_SYMBOL(unplugged_hwif_iops);
103
104 /*
105 * Conventional PIO operations for ATA devices
106 */
107
ide_inb(unsigned long port)108 static u8 ide_inb (unsigned long port)
109 {
110 return (u8) inb(port);
111 }
112
ide_inw(unsigned long port)113 static u16 ide_inw (unsigned long port)
114 {
115 return (u16) inw(port);
116 }
117
ide_insw(unsigned long port,void * addr,u32 count)118 static void ide_insw (unsigned long port, void *addr, u32 count)
119 {
120 return insw(port, addr, count);
121 }
122
ide_inl(unsigned long port)123 static u32 ide_inl (unsigned long port)
124 {
125 return (u32) inl(port);
126 }
127
ide_insl(unsigned long port,void * addr,u32 count)128 static void ide_insl (unsigned long port, void *addr, u32 count)
129 {
130 insl(port, addr, count);
131 }
132
ide_outb(u8 addr,unsigned long port)133 static void ide_outb (u8 addr, unsigned long port)
134 {
135 outb(addr, port);
136 }
137
ide_outbsync(ide_drive_t * drive,u8 addr,unsigned long port)138 static void ide_outbsync (ide_drive_t *drive, u8 addr, unsigned long port)
139 {
140 outb(addr, port);
141 }
142
ide_outw(u16 addr,unsigned long port)143 static void ide_outw (u16 addr, unsigned long port)
144 {
145 outw(addr, port);
146 }
147
ide_outsw(unsigned long port,void * addr,u32 count)148 static void ide_outsw (unsigned long port, void *addr, u32 count)
149 {
150 outsw(port, addr, count);
151 }
152
ide_outl(u32 addr,unsigned long port)153 static void ide_outl (u32 addr, unsigned long port)
154 {
155 outl(addr, port);
156 }
157
ide_outsl(unsigned long port,void * addr,u32 count)158 static void ide_outsl (unsigned long port, void *addr, u32 count)
159 {
160 outsl(port, addr, count);
161 }
162
default_hwif_iops(ide_hwif_t * hwif)163 void default_hwif_iops (ide_hwif_t *hwif)
164 {
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;
171 hwif->INB = ide_inb;
172 hwif->INW = ide_inw;
173 hwif->INL = ide_inl;
174 hwif->INSW = ide_insw;
175 hwif->INSL = ide_insl;
176 }
177
178 EXPORT_SYMBOL(default_hwif_iops);
179
180 /*
181 * MMIO operations, typically used for SATA controllers
182 */
183
ide_mm_inb(unsigned long port)184 static u8 ide_mm_inb (unsigned long port)
185 {
186 return (u8) readb(port);
187 }
188
ide_mm_inw(unsigned long port)189 static u16 ide_mm_inw (unsigned long port)
190 {
191 return (u16) readw(port);
192 }
193
ide_mm_insw(unsigned long port,void * addr,u32 count)194 static void ide_mm_insw (unsigned long port, void *addr, u32 count)
195 {
196 __ide_mm_insw(port, addr, count);
197 }
198
ide_mm_inl(unsigned long port)199 static u32 ide_mm_inl (unsigned long port)
200 {
201 return (u32) readl(port);
202 }
203
ide_mm_insl(unsigned long port,void * addr,u32 count)204 static void ide_mm_insl (unsigned long port, void *addr, u32 count)
205 {
206 __ide_mm_insl(port, addr, count);
207 }
208
ide_mm_outb(u8 value,unsigned long port)209 static void ide_mm_outb (u8 value, unsigned long port)
210 {
211 writeb(value, port);
212 }
213
ide_mm_outbsync(ide_drive_t * drive,u8 value,unsigned long port)214 static void ide_mm_outbsync (ide_drive_t *drive, u8 value, unsigned long port)
215 {
216 writeb(value, port);
217 }
218
ide_mm_outw(u16 value,unsigned long port)219 static void ide_mm_outw (u16 value, unsigned long port)
220 {
221 writew(value, port);
222 }
223
ide_mm_outsw(unsigned long port,void * addr,u32 count)224 static void ide_mm_outsw (unsigned long port, void *addr, u32 count)
225 {
226 __ide_mm_outsw(port, addr, count);
227 }
228
ide_mm_outl(u32 value,unsigned long port)229 static void ide_mm_outl (u32 value, unsigned long port)
230 {
231 writel(value, port);
232 }
233
ide_mm_outsl(unsigned long port,void * addr,u32 count)234 static void ide_mm_outsl (unsigned long port, void *addr, u32 count)
235 {
236 __ide_mm_outsl(port, addr, count);
237 }
238
default_hwif_mmiops(ide_hwif_t * hwif)239 void default_hwif_mmiops (ide_hwif_t *hwif)
240 {
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;
254 }
255
256 EXPORT_SYMBOL(default_hwif_mmiops);
257
default_hwif_transport(ide_hwif_t * hwif)258 void default_hwif_transport (ide_hwif_t *hwif)
259 {
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;
264 }
265
266 EXPORT_SYMBOL(default_hwif_transport);
267
read_24(ide_drive_t * drive)268 u32 read_24 (ide_drive_t *drive)
269 {
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;
274 }
275
276 EXPORT_SYMBOL(read_24);
277
SELECT_DRIVE(ide_drive_t * drive)278 void SELECT_DRIVE (ide_drive_t *drive)
279 {
280 if (HWIF(drive)->selectproc)
281 HWIF(drive)->selectproc(drive);
282 HWIF(drive)->OUTB(drive->select.all, IDE_SELECT_REG);
283 }
284
285 EXPORT_SYMBOL(SELECT_DRIVE);
286
SELECT_INTERRUPT(ide_drive_t * drive)287 void SELECT_INTERRUPT (ide_drive_t *drive)
288 {
289 if (HWIF(drive)->intrproc)
290 HWIF(drive)->intrproc(drive);
291 else
292 HWIF(drive)->OUTB(drive->ctl|2, IDE_CONTROL_REG);
293 }
294
295 EXPORT_SYMBOL(SELECT_INTERRUPT);
296
SELECT_MASK(ide_drive_t * drive,int mask)297 void SELECT_MASK (ide_drive_t *drive, int mask)
298 {
299 if (HWIF(drive)->maskproc)
300 HWIF(drive)->maskproc(drive, mask);
301 }
302
303 EXPORT_SYMBOL(SELECT_MASK);
304
QUIRK_LIST(ide_drive_t * drive)305 void QUIRK_LIST (ide_drive_t *drive)
306 {
307 if (HWIF(drive)->quirkproc)
308 drive->quirk_list = HWIF(drive)->quirkproc(drive);
309 }
310
311 EXPORT_SYMBOL(QUIRK_LIST);
312
313 /*
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.
319 */
ata_vlb_sync(ide_drive_t * drive,ide_ioreg_t port)320 void ata_vlb_sync (ide_drive_t *drive, ide_ioreg_t port)
321 {
322 (void) HWIF(drive)->INB(port);
323 (void) HWIF(drive)->INB(port);
324 (void) HWIF(drive)->INB(port);
325 }
326
327 EXPORT_SYMBOL(ata_vlb_sync);
328
329 /*
330 * This is used for most PIO data transfers *from* the IDE interface
331 */
ata_input_data(ide_drive_t * drive,void * buffer,u32 wcount)332 void ata_input_data (ide_drive_t *drive, void *buffer, u32 wcount)
333 {
334 ide_hwif_t *hwif = HWIF(drive);
335 u8 io_32bit = drive->io_32bit;
336
337 if (io_32bit) {
338 if (io_32bit & 2) {
339 unsigned long flags;
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);
344 } else
345 hwif->INSL(IDE_DATA_REG, buffer, wcount);
346 } else {
347 hwif->INSW(IDE_DATA_REG, buffer, wcount<<1);
348 }
349 }
350
351 EXPORT_SYMBOL(ata_input_data);
352
353 /*
354 * This is used for most PIO data transfers *to* the IDE interface
355 */
ata_output_data(ide_drive_t * drive,void * buffer,u32 wcount)356 void ata_output_data (ide_drive_t *drive, void *buffer, u32 wcount)
357 {
358 ide_hwif_t *hwif = HWIF(drive);
359 u8 io_32bit = drive->io_32bit;
360
361 if (io_32bit) {
362 if (io_32bit & 2) {
363 unsigned long flags;
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);
368 } else
369 hwif->OUTSL(IDE_DATA_REG, buffer, wcount);
370 } else {
371 hwif->OUTSW(IDE_DATA_REG, buffer, wcount<<1);
372 }
373 }
374
375 EXPORT_SYMBOL(ata_output_data);
376
377 /*
378 * The following routines are mainly used by the ATAPI drivers.
379 *
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.
383 */
384
atapi_input_bytes(ide_drive_t * drive,void * buffer,u32 bytecount)385 void atapi_input_bytes (ide_drive_t *drive, void *buffer, u32 bytecount)
386 {
387 ide_hwif_t *hwif = HWIF(drive);
388
389 ++bytecount;
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);
394 return;
395 }
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);
400 }
401
402 EXPORT_SYMBOL(atapi_input_bytes);
403
atapi_output_bytes(ide_drive_t * drive,void * buffer,u32 bytecount)404 void atapi_output_bytes (ide_drive_t *drive, void *buffer, u32 bytecount)
405 {
406 ide_hwif_t *hwif = HWIF(drive);
407
408 ++bytecount;
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);
413 return;
414 }
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);
419 }
420
421 EXPORT_SYMBOL(atapi_output_bytes);
422
423 /*
424 * Beginning of Taskfile OPCODE Library and feature sets.
425 */
ide_fix_driveid(struct hd_driveid * id)426 void ide_fix_driveid (struct hd_driveid *id)
427 {
428 #ifndef __LITTLE_ENDIAN
429 # ifdef __BIG_ENDIAN
430 int i;
431 u16 *stringcast;
432
433 #ifdef __mc68000__
434 if (!MACH_IS_AMIGA && !MACH_IS_MAC && !MACH_IS_Q40 && !MACH_IS_ATARI)
435 return;
436
437 #ifdef M68K_IDE_SWAPW
438 if (M68K_IDE_SWAPW) { /* fix bus byteorder first */
439 u_char *p = (u_char *)id;
440 u_char t;
441 for (i = 0; i < 512; i += 2) {
442 t = p[i];
443 p[i] = p[i+1];
444 p[i+1] = t;
445 }
446 }
447 #endif
448 #endif /* __mc68000__ */
449
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);
534 # else
535 # error "Please fix <asm/byteorder.h>"
536 # endif
537 #endif
538 }
539
540 EXPORT_SYMBOL(ide_fix_driveid);
541
ide_fixstring(u8 * s,const int bytecount,const int byteswap)542 void ide_fixstring (u8 *s, const int bytecount, const int byteswap)
543 {
544 u8 *p = s, *end = &s[bytecount & ~1]; /* bytecount must be even */
545
546 if (byteswap) {
547 /* convert from big-endian to host byte order */
548 for (p = end ; p != s;) {
549 unsigned short *pp = (unsigned short *) (p -= 2);
550 *pp = ntohs(*pp);
551 }
552 }
553 /* strip leading blanks */
554 while (s != end && *s == ' ')
555 ++s;
556 /* compress internal blanks and strip trailing blanks */
557 while (s != end && *s) {
558 if (*s++ != ' ' || (s != end && *s && *s != ' '))
559 *p++ = *(s-1);
560 }
561 /* wipe out trailing garbage */
562 while (p != end)
563 *p++ = '\0';
564 }
565
566 EXPORT_SYMBOL(ide_fixstring);
567
568 /*
569 * Needed for PCI irq sharing
570 */
drive_is_ready(ide_drive_t * drive)571 int drive_is_ready (ide_drive_t *drive)
572 {
573 ide_hwif_t *hwif = HWIF(drive);
574 u8 stat = 0;
575
576 if (drive->waiting_for_dma)
577 return hwif->ide_dma_test_irq(drive);
578
579 #if 0
580 /* need to guarantee 400ns since last command was issued */
581 udelay(1);
582 #endif
583
584 #ifdef CONFIG_IDEPCI_SHARE_IRQ
585 /*
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.
590 */
591 if (IDE_CONTROL_REG)
592 stat = hwif->INB(IDE_ALTSTATUS_REG);
593 else
594 #endif /* CONFIG_IDEPCI_SHARE_IRQ */
595 /* Note: this may clear a pending IRQ!! */
596 stat = hwif->INB(IDE_STATUS_REG);
597
598 if (stat & BUSY_STAT)
599 /* drive busy: definitely not interrupting */
600 return 0;
601
602 /* drive ready: *might* be interrupting */
603 return 1;
604 }
605
606 EXPORT_SYMBOL(drive_is_ready);
607
608 /*
609 * Global for All, and taken from ide-pmac.c. Can be called
610 * with spinlock held & IRQs disabled, so don't schedule !
611 */
wait_for_ready(ide_drive_t * drive,int timeout)612 int wait_for_ready (ide_drive_t *drive, int timeout)
613 {
614 ide_hwif_t *hwif = HWIF(drive);
615 u8 stat = 0;
616
617 while(--timeout) {
618 stat = hwif->INB(IDE_STATUS_REG);
619 if (!(stat & BUSY_STAT)) {
620 if (drive->ready_stat == 0)
621 break;
622 else if ((stat & drive->ready_stat)||(stat & ERR_STAT))
623 break;
624 }
625 mdelay(1);
626 }
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);
631 }
632 return 1;
633 }
634 return 0;
635 }
636
637 EXPORT_SYMBOL(wait_for_ready);
638
639 /*
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.
644 *
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.
649 */
ide_wait_stat(ide_startstop_t * startstop,ide_drive_t * drive,u8 good,u8 bad,unsigned long timeout)650 int ide_wait_stat (ide_startstop_t *startstop, ide_drive_t *drive, u8 good, u8 bad, unsigned long timeout)
651 {
652 ide_hwif_t *hwif = HWIF(drive);
653 u8 stat;
654 int i;
655 unsigned long flags;
656
657 /* bail early if we've exceeded max_failures */
658 if (drive->max_failures && (drive->failures > drive->max_failures)) {
659 *startstop = ide_stopped;
660 return 1;
661 }
662
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);
666 timeout += jiffies;
667 while ((stat = hwif->INB(IDE_STATUS_REG)) & BUSY_STAT) {
668 if (time_after(jiffies, timeout)) {
669 /*
670 * One last read after the timeout in case
671 * heavy interrupt load made us not make any
672 * progress during the timeout..
673 */
674 stat = hwif->INB(IDE_STATUS_REG);
675 if (!(stat & BUSY_STAT))
676 break;
677
678 local_irq_restore(flags);
679 *startstop = DRIVER(drive)->error(drive, "status timeout", stat);
680 return 1;
681 }
682 }
683 local_irq_restore(flags);
684 }
685 /*
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.
691 */
692 for (i = 0; i < 10; i++) {
693 udelay(1);
694 if (OK_STAT((stat = hwif->INB(IDE_STATUS_REG)), good, bad))
695 return 0;
696 }
697 *startstop = DRIVER(drive)->error(drive, "status error", stat);
698 return 1;
699 }
700
701 EXPORT_SYMBOL(ide_wait_stat);
702
703 /*
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.
706 */
eighty_ninty_three(ide_drive_t * drive)707 u8 eighty_ninty_three (ide_drive_t *drive)
708 {
709 #if 0
710 if (!HWIF(drive)->udma_four)
711 return 0;
712
713 if (drive->id->major_rev_num) {
714 int hssbd = 0;
715 int i;
716 /*
717 * Determime highest Supported SPEC
718 */
719 for (i=1; i<=15; i++)
720 if (drive->id->major_rev_num & (1<<i))
721 hssbd++;
722
723 switch (hssbd) {
724 case 7:
725 case 6:
726 case 5:
727 /* ATA-4 and older do not support above Ultra 33 */
728 default:
729 return 0;
730 }
731 }
732
733 return ((u8) (
734 #ifndef CONFIG_IDEDMA_IVB
735 (drive->id->hw_config & 0x4000) &&
736 #endif /* CONFIG_IDEDMA_IVB */
737 (drive->id->hw_config & 0x6000)) ? 1 : 0);
738
739 #else
740
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);
746 #endif
747 }
748
749 EXPORT_SYMBOL(eighty_ninty_three);
750
ide_ata66_check(ide_drive_t * drive,ide_task_t * args)751 int ide_ata66_check (ide_drive_t *drive, ide_task_t *args)
752 {
753 /* SATA has no cable restrictions */
754 if (HWIF(drive)->sata)
755 return 0;
756
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);
768 return 1;
769 }
770 if (!HWIF(drive)->udma_four) {
771 printk("%s: Speed warnings UDMA 3/4/5 is not "
772 "functional.\n",
773 HWIF(drive)->name);
774 return 1;
775 }
776 }
777 return 0;
778 }
779
780 EXPORT_SYMBOL(ide_ata66_check);
781
782 /*
783 * Backside of HDIO_DRIVE_CMD call of SETFEATURES_XFER.
784 * 1 : Safe to update drive->id DMA registers.
785 * 0 : OOPs not allowed.
786 */
787 int set_transfer (ide_drive_t *drive, ide_task_t *args)
788 {
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))
795 return 1;
796
797 return 0;
798 }
799
800 EXPORT_SYMBOL(set_transfer);
801
802 u8 ide_auto_reduce_xfer (ide_drive_t *drive)
803 {
804 if (!drive->crc_count)
805 return drive->current_speed;
806 drive->crc_count = 0;
807
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;
816 /*
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.
821 */
822 case XFER_UDMA_0:
823 default: return XFER_PIO_4;
824 }
825 }
826
827 EXPORT_SYMBOL(ide_auto_reduce_xfer);
828
829 /*
830 * Update the
831 */
832 int ide_driveid_update (ide_drive_t *drive)
833 {
834 ide_hwif_t *hwif = HWIF(drive);
835 struct hd_driveid *id;
836 #if 0
837 id = kmalloc(SECTOR_WORDS*4, GFP_ATOMIC);
838 if (!id)
839 return 0;
840
841 taskfile_lib_get_identify(drive, (char *)&id);
842
843 ide_fix_driveid(id);
844 if (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 ? */
849 kfree(id);
850 }
851 return 1;
852 #else
853 /*
854 * Re-read drive->id for possible DMA mode
855 * change (copied from ide-probe.c)
856 */
857 unsigned long timeout, flags;
858
859 SELECT_MASK(drive, 1);
860 if (IDE_CONTROL_REG)
861 hwif->OUTB(drive->ctl,IDE_CONTROL_REG);
862 ide_delay_50ms();
863 hwif->OUTB(WIN_IDENTIFY, IDE_COMMAND_REG);
864 timeout = jiffies + WAIT_WORSTCASE;
865 do {
866 if (time_after(jiffies, timeout)) {
867 SELECT_MASK(drive, 0);
868 return 0; /* drive timed-out */
869 }
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);
876 return 0;
877 }
878 local_irq_save(flags);
879 SELECT_MASK(drive, 0);
880 id = kmalloc(SECTOR_WORDS*4, GFP_ATOMIC);
881 if (!id) {
882 local_irq_restore(flags);
883 return 0;
884 }
885 ata_input_data(drive, id, SECTOR_WORDS);
886 (void) hwif->INB(IDE_STATUS_REG); /* clear drive IRQ */
887 local_irq_enable();
888 local_irq_restore(flags);
889 ide_fix_driveid(id);
890
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 ? */
895 kfree(id);
896
897 return 1;
898 #endif
899 }
900
901 EXPORT_SYMBOL(ide_driveid_update);
902
903 /*
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
909 *
910 * It is gone..........
911 *
912 * const char *msg == consider adding for verbose errors.
913 */
914 int ide_config_drive_speed (ide_drive_t *drive, u8 speed)
915 {
916 ide_hwif_t *hwif = HWIF(drive);
917 int i, error = 1;
918 u8 stat;
919
920 // while (HWGROUP(drive)->busy)
921 // ide_delay_50ms();
922
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) */
926
927 /*
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).
932 */
933 /*
934 * Select the drive, and issue the SETFEATURES command
935 */
936 disable_irq_nosync(hwif->irq);
937 udelay(1);
938 SELECT_DRIVE(drive);
939 SELECT_MASK(drive, 0);
940 udelay(1);
941 if (IDE_CONTROL_REG)
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);
948 udelay(1);
949 /*
950 * Wait for drive to become non-BUSY
951 */
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))
958 break;
959 }
960 local_irq_restore(flags);
961 }
962
963 /*
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.
969 */
970 for (i = 0; i < 10; i++) {
971 udelay(1);
972 if (OK_STAT((stat = hwif->INB(IDE_STATUS_REG)), DRIVE_READY, BUSY_STAT|DRQ_STAT|ERR_STAT)) {
973 error = 0;
974 break;
975 }
976 }
977
978 SELECT_MASK(drive, 0);
979
980 enable_irq(hwif->irq);
981
982 if (error) {
983 (void) ide_dump_status(drive, "set_drive_speed_status", stat);
984 return error;
985 }
986
987 drive->id->dma_ultra &= ~0xFF00;
988 drive->id->dma_mword &= ~0x0F00;
989 drive->id->dma_1word &= ~0x0F00;
990
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);
994 else
995 hwif->ide_dma_off_quietly(drive);
996 #endif /* (CONFIG_BLK_DEV_IDEDMA) && !(CONFIG_DMA_NONPCI) */
997
998 switch(speed) {
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;
1013 default: break;
1014 }
1015 if (!drive->init_speed)
1016 drive->init_speed = speed;
1017 drive->current_speed = speed;
1018 return error;
1019 }
1020
1021 EXPORT_SYMBOL(ide_config_drive_speed);
1022
1023
1024 /*
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).
1030 *
1031 * See also ide_execute_command
1032 */
1033 void __ide_set_handler (ide_drive_t *drive, ide_handler_t *handler,
1034 unsigned int timeout, ide_expiry_t *expiry)
1035 {
1036 ide_hwgroup_t *hwgroup = HWGROUP(drive);
1037
1038 if (hwgroup->handler != NULL) {
1039 printk(KERN_CRIT "%s: ide_set_handler: handler not null; "
1040 "old=%p, new=%p\n",
1041 drive->name, hwgroup->handler, handler);
1042 BUG();
1043 }
1044 hwgroup->handler = handler;
1045 hwgroup->expiry = expiry;
1046 hwgroup->timer.expires = jiffies + timeout;
1047 add_timer(&hwgroup->timer);
1048 }
1049
1050 EXPORT_SYMBOL(__ide_set_handler);
1051
1052 void ide_set_handler (ide_drive_t *drive, ide_handler_t *handler,
1053 unsigned int timeout, ide_expiry_t *expiry)
1054 {
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);
1059 }
1060
1061 EXPORT_SYMBOL(ide_set_handler);
1062
1063 /**
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
1070 *
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.
1075 */
1076
1077 void ide_execute_command(ide_drive_t *drive, task_ioreg_t cmd, ide_handler_t *handler, unsigned timeout, ide_expiry_t *expiry)
1078 {
1079 unsigned long flags;
1080 ide_hwgroup_t *hwgroup = HWGROUP(drive);
1081 ide_hwif_t *hwif = HWIF(drive);
1082
1083 spin_lock_irqsave(&io_request_lock, flags);
1084
1085 if(hwgroup->handler)
1086 BUG();
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.
1094
1095 FIXME: we could skip this delay with care on non shared
1096 devices
1097
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)
1101 */
1102 ndelay(400);
1103 spin_unlock_irqrestore(&io_request_lock, flags);
1104 }
1105
1106 EXPORT_SYMBOL(ide_execute_command);
1107
1108 /* needed below */
1109 static ide_startstop_t do_reset1 (ide_drive_t *, int);
1110
1111 /*
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
1115 * for another 50ms.
1116 */
1117 static ide_startstop_t atapi_reset_pollfunc (ide_drive_t *drive)
1118 {
1119 ide_hwgroup_t *hwgroup = HWGROUP(drive);
1120 ide_hwif_t *hwif = HWIF(drive);
1121 u8 stat;
1122
1123 SELECT_DRIVE(drive);
1124 udelay (10);
1125
1126 if (OK_STAT(stat = hwif->INB(IDE_STATUS_REG), 0, BUSY_STAT)) {
1127 printk("%s: ATAPI reset complete\n", drive->name);
1128 } else {
1129 if (time_before(jiffies, hwgroup->poll_timeout)) {
1130 if (HWGROUP(drive)->handler != NULL)
1131 BUG();
1132 ide_set_handler(drive, &atapi_reset_pollfunc, HZ/20, NULL);
1133 /* continue polling */
1134 return ide_started;
1135 }
1136 /* end of polling */
1137 hwgroup->poll_timeout = 0;
1138 printk("%s: ATAPI reset timed-out, status=0x%02x\n",
1139 drive->name, stat);
1140 /* do it the old fashioned way */
1141 return do_reset1(drive, 1);
1142 }
1143 /* done polling */
1144 hwgroup->poll_timeout = 0;
1145 return ide_stopped;
1146 }
1147
1148 /*
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
1152 * for another 50ms.
1153 */
1154 static ide_startstop_t reset_pollfunc (ide_drive_t *drive)
1155 {
1156 ide_hwgroup_t *hwgroup = HWGROUP(drive);
1157 ide_hwif_t *hwif = HWIF(drive);
1158 u8 tmp;
1159
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);
1164 return ide_stopped;
1165 }
1166 }
1167
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)
1171 BUG();
1172 ide_set_handler(drive, &reset_pollfunc, HZ/20, NULL);
1173 /* continue polling */
1174 return ide_started;
1175 }
1176 printk("%s: reset timed-out, status=0x%02x\n", hwif->name, tmp);
1177 drive->failures++;
1178 } else {
1179 printk("%s: reset: ", hwif->name);
1180 if ((tmp = hwif->INB(IDE_ERROR_REG)) == 1) {
1181 printk("success\n");
1182 drive->failures = 0;
1183 } else {
1184 drive->failures++;
1185 #if FANCY_STATUS_DUMPS
1186 printk("master: ");
1187 switch (tmp & 0x7f) {
1188 case 1: printk("passed");
1189 break;
1190 case 2: printk("formatter device error");
1191 break;
1192 case 3: printk("sector buffer error");
1193 break;
1194 case 4: printk("ECC circuitry error");
1195 break;
1196 case 5: printk("controlling MPU error");
1197 break;
1198 default:printk("error (0x%02x?)", tmp);
1199 }
1200 if (tmp & 0x80)
1201 printk("; slave: failed");
1202 printk("\n");
1203 #else
1204 printk("failed\n");
1205 #endif /* FANCY_STATUS_DUMPS */
1206 }
1207 }
1208 hwgroup->poll_timeout = 0; /* done polling */
1209 return ide_stopped;
1210 }
1211
1212 void check_dma_crc (ide_drive_t *drive)
1213 {
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);
1219 } else {
1220 (void) HWIF(drive)->ide_dma_off(drive);
1221 }
1222 }
1223
1224 void pre_reset (ide_drive_t *drive)
1225 {
1226 DRIVER(drive)->pre_reset(drive);
1227
1228 if (!drive->keep_settings) {
1229 if (drive->using_dma) {
1230 check_dma_crc(drive);
1231 } else {
1232 drive->unmask = 0;
1233 drive->io_32bit = 0;
1234 }
1235 return;
1236 }
1237 if (drive->using_dma)
1238 check_dma_crc(drive);
1239
1240 if (HWIF(drive)->pre_reset != NULL)
1241 HWIF(drive)->pre_reset(drive);
1242
1243 }
1244
1245 /*
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.
1250 *
1251 * ATAPI devices have their own reset mechanism which allows them to be
1252 * individually reset without clobbering other devices on the same interface.
1253 *
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.
1259 */
1260
1261 static ide_startstop_t do_reset1 (ide_drive_t *drive, int do_not_try_atapi)
1262 {
1263 unsigned int unit;
1264 unsigned long flags;
1265 ide_hwif_t *hwif;
1266 ide_hwgroup_t *hwgroup;
1267
1268 spin_lock_irqsave(&io_request_lock, flags);
1269
1270 hwgroup = HWGROUP(drive);
1271 hwif = HWIF(drive);
1272
1273 /* We must not reset with running handlers */
1274 if(hwgroup->handler != NULL)
1275 BUG();
1276
1277 /* For an ATAPI device, first try an ATAPI SRST. */
1278 if (drive->media != ide_disk && !do_not_try_atapi) {
1279 pre_reset(drive);
1280 SELECT_DRIVE(drive);
1281 udelay (20);
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);
1286 return ide_started;
1287 }
1288
1289 /*
1290 * First, reset any device state data we were maintaining
1291 * for any of the drives on this interface.
1292 */
1293 for (unit = 0; unit < MAX_DRIVES; ++unit)
1294 pre_reset(&hwif->drives[unit]);
1295
1296 #if OK_TO_RESET_CONTROLLER
1297 if (!IDE_CONTROL_REG) {
1298 spin_unlock_irqrestore(&io_request_lock, flags);
1299 return ide_stopped;
1300 }
1301
1302 /*
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.
1309 */
1310 /* set SRST and nIEN */
1311 hwif->OUTBSYNC(drive, drive->ctl|6,IDE_CONTROL_REG);
1312 /* more than enough time */
1313 udelay(10);
1314 if (drive->quirk_list == 2) {
1315 /* clear SRST and nIEN */
1316 hwif->OUTBSYNC(drive, drive->ctl, IDE_CONTROL_REG);
1317 } else {
1318 /* clear SRST, leave nIEN */
1319 hwif->OUTBSYNC(drive, drive->ctl|2, IDE_CONTROL_REG);
1320 }
1321 /* more than enough time */
1322 udelay(10);
1323
1324 hwgroup->poll_timeout = jiffies + WAIT_WORSTCASE;
1325 __ide_set_handler(drive, &reset_pollfunc, HZ/20, NULL);
1326 /*
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
1330 */
1331 if (hwif->resetproc != NULL) {
1332 hwif->resetproc(drive);
1333 }
1334
1335 #endif /* OK_TO_RESET_CONTROLLER */
1336 spin_unlock_irqrestore(&io_request_lock, flags);
1337 return ide_started;
1338 }
1339
1340 /*
1341 * ide_do_reset() is the entry point to the drive/interface reset code.
1342 *
1343 * Caller must not hold the io_request lock.
1344 */
1345
1346 ide_startstop_t ide_do_reset (ide_drive_t *drive)
1347 {
1348 return do_reset1(drive, 0);
1349 }
1350
1351 EXPORT_SYMBOL(ide_do_reset);
1352
1353