Commit | Line | Data |
---|---|---|
1da177e4 LT |
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 | ||
1da177e4 LT |
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> | |
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> | |
1e86240f | 26 | #include <linux/nmi.h> |
1da177e4 LT |
27 | |
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 | ||
37 | static u8 ide_inb (unsigned long port) | |
38 | { | |
39 | return (u8) inb(port); | |
40 | } | |
41 | ||
42 | static u16 ide_inw (unsigned long port) | |
43 | { | |
44 | return (u16) inw(port); | |
45 | } | |
46 | ||
47 | static void ide_insw (unsigned long port, void *addr, u32 count) | |
48 | { | |
49 | insw(port, addr, count); | |
50 | } | |
51 | ||
1da177e4 LT |
52 | static void ide_insl (unsigned long port, void *addr, u32 count) |
53 | { | |
54 | insl(port, addr, count); | |
55 | } | |
56 | ||
57 | static void ide_outb (u8 val, unsigned long port) | |
58 | { | |
59 | outb(val, port); | |
60 | } | |
61 | ||
62 | static void ide_outbsync (ide_drive_t *drive, u8 addr, unsigned long port) | |
63 | { | |
64 | outb(addr, port); | |
65 | } | |
66 | ||
67 | static void ide_outw (u16 val, unsigned long port) | |
68 | { | |
69 | outw(val, port); | |
70 | } | |
71 | ||
72 | static void ide_outsw (unsigned long port, void *addr, u32 count) | |
73 | { | |
74 | outsw(port, addr, count); | |
75 | } | |
76 | ||
1da177e4 LT |
77 | static void ide_outsl (unsigned long port, void *addr, u32 count) |
78 | { | |
79 | outsl(port, addr, count); | |
80 | } | |
81 | ||
82 | void default_hwif_iops (ide_hwif_t *hwif) | |
83 | { | |
84 | hwif->OUTB = ide_outb; | |
85 | hwif->OUTBSYNC = ide_outbsync; | |
86 | hwif->OUTW = ide_outw; | |
1da177e4 LT |
87 | hwif->OUTSW = ide_outsw; |
88 | hwif->OUTSL = ide_outsl; | |
89 | hwif->INB = ide_inb; | |
90 | hwif->INW = ide_inw; | |
1da177e4 LT |
91 | hwif->INSW = ide_insw; |
92 | hwif->INSL = ide_insl; | |
93 | } | |
94 | ||
1da177e4 LT |
95 | /* |
96 | * MMIO operations, typically used for SATA controllers | |
97 | */ | |
98 | ||
99 | static u8 ide_mm_inb (unsigned long port) | |
100 | { | |
101 | return (u8) readb((void __iomem *) port); | |
102 | } | |
103 | ||
104 | static u16 ide_mm_inw (unsigned long port) | |
105 | { | |
106 | return (u16) readw((void __iomem *) port); | |
107 | } | |
108 | ||
109 | static void ide_mm_insw (unsigned long port, void *addr, u32 count) | |
110 | { | |
111 | __ide_mm_insw((void __iomem *) port, addr, count); | |
112 | } | |
113 | ||
1da177e4 LT |
114 | static void ide_mm_insl (unsigned long port, void *addr, u32 count) |
115 | { | |
116 | __ide_mm_insl((void __iomem *) port, addr, count); | |
117 | } | |
118 | ||
119 | static void ide_mm_outb (u8 value, unsigned long port) | |
120 | { | |
121 | writeb(value, (void __iomem *) port); | |
122 | } | |
123 | ||
124 | static void ide_mm_outbsync (ide_drive_t *drive, u8 value, unsigned long port) | |
125 | { | |
126 | writeb(value, (void __iomem *) port); | |
127 | } | |
128 | ||
129 | static void ide_mm_outw (u16 value, unsigned long port) | |
130 | { | |
131 | writew(value, (void __iomem *) port); | |
132 | } | |
133 | ||
134 | static void ide_mm_outsw (unsigned long port, void *addr, u32 count) | |
135 | { | |
136 | __ide_mm_outsw((void __iomem *) port, addr, count); | |
137 | } | |
138 | ||
1da177e4 LT |
139 | static void ide_mm_outsl (unsigned long port, void *addr, u32 count) |
140 | { | |
141 | __ide_mm_outsl((void __iomem *) port, addr, count); | |
142 | } | |
143 | ||
144 | void 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; | |
1da177e4 LT |
151 | hwif->OUTSW = ide_mm_outsw; |
152 | hwif->OUTSL = ide_mm_outsl; | |
153 | hwif->INB = ide_mm_inb; | |
154 | hwif->INW = ide_mm_inw; | |
1da177e4 LT |
155 | hwif->INSW = ide_mm_insw; |
156 | hwif->INSL = ide_mm_insl; | |
157 | } | |
158 | ||
159 | EXPORT_SYMBOL(default_hwif_mmiops); | |
160 | ||
161 | u32 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 | ||
169 | void 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 | ||
176 | EXPORT_SYMBOL(SELECT_DRIVE); | |
177 | ||
178 | void 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 | ||
186 | void SELECT_MASK (ide_drive_t *drive, int mask) | |
187 | { | |
188 | if (HWIF(drive)->maskproc) | |
189 | HWIF(drive)->maskproc(drive, mask); | |
190 | } | |
191 | ||
192 | void 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 | */ | |
205 | static 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 | */ | |
215 | static 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 | */ | |
237 | static 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 | ||
264 | static 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 | ||
281 | static 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 | ||
298 | void 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 | ||
1da177e4 LT |
306 | /* |
307 | * Beginning of Taskfile OPCODE Library and feature sets. | |
308 | */ | |
309 | void 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 | ||
01745112 BZ |
406 | /* |
407 | * ide_fixstring() cleans up and (optionally) byte-swaps a text string, | |
408 | * removing leading/trailing blanks and compressing internal blanks. | |
409 | * It is primarily used to tidy up the model name/number fields as | |
410 | * returned by the WIN_[P]IDENTIFY commands. | |
411 | */ | |
412 | ||
1da177e4 LT |
413 | void ide_fixstring (u8 *s, const int bytecount, const int byteswap) |
414 | { | |
415 | u8 *p = s, *end = &s[bytecount & ~1]; /* bytecount must be even */ | |
416 | ||
417 | if (byteswap) { | |
418 | /* convert from big-endian to host byte order */ | |
419 | for (p = end ; p != s;) { | |
420 | unsigned short *pp = (unsigned short *) (p -= 2); | |
421 | *pp = ntohs(*pp); | |
422 | } | |
423 | } | |
424 | /* strip leading blanks */ | |
425 | while (s != end && *s == ' ') | |
426 | ++s; | |
427 | /* compress internal blanks and strip trailing blanks */ | |
428 | while (s != end && *s) { | |
429 | if (*s++ != ' ' || (s != end && *s && *s != ' ')) | |
430 | *p++ = *(s-1); | |
431 | } | |
432 | /* wipe out trailing garbage */ | |
433 | while (p != end) | |
434 | *p++ = '\0'; | |
435 | } | |
436 | ||
437 | EXPORT_SYMBOL(ide_fixstring); | |
438 | ||
439 | /* | |
440 | * Needed for PCI irq sharing | |
441 | */ | |
442 | int drive_is_ready (ide_drive_t *drive) | |
443 | { | |
444 | ide_hwif_t *hwif = HWIF(drive); | |
445 | u8 stat = 0; | |
446 | ||
447 | if (drive->waiting_for_dma) | |
448 | return hwif->ide_dma_test_irq(drive); | |
449 | ||
450 | #if 0 | |
451 | /* need to guarantee 400ns since last command was issued */ | |
452 | udelay(1); | |
453 | #endif | |
454 | ||
455 | #ifdef CONFIG_IDEPCI_SHARE_IRQ | |
456 | /* | |
457 | * We do a passive status test under shared PCI interrupts on | |
458 | * cards that truly share the ATA side interrupt, but may also share | |
459 | * an interrupt with another pci card/device. We make no assumptions | |
460 | * about possible isa-pnp and pci-pnp issues yet. | |
461 | */ | |
462 | if (IDE_CONTROL_REG) | |
463 | stat = hwif->INB(IDE_ALTSTATUS_REG); | |
464 | else | |
465 | #endif /* CONFIG_IDEPCI_SHARE_IRQ */ | |
466 | /* Note: this may clear a pending IRQ!! */ | |
467 | stat = hwif->INB(IDE_STATUS_REG); | |
468 | ||
469 | if (stat & BUSY_STAT) | |
470 | /* drive busy: definitely not interrupting */ | |
471 | return 0; | |
472 | ||
473 | /* drive ready: *might* be interrupting */ | |
474 | return 1; | |
475 | } | |
476 | ||
477 | EXPORT_SYMBOL(drive_is_ready); | |
478 | ||
1da177e4 LT |
479 | /* |
480 | * This routine busy-waits for the drive status to be not "busy". | |
481 | * It then checks the status for all of the "good" bits and none | |
482 | * of the "bad" bits, and if all is okay it returns 0. All other | |
74af21cf | 483 | * cases return error -- caller may then invoke ide_error(). |
1da177e4 LT |
484 | * |
485 | * This routine should get fixed to not hog the cpu during extra long waits.. | |
486 | * That could be done by busy-waiting for the first jiffy or two, and then | |
487 | * setting a timer to wake up at half second intervals thereafter, | |
488 | * until timeout is achieved, before timing out. | |
489 | */ | |
aedea591 | 490 | static int __ide_wait_stat(ide_drive_t *drive, u8 good, u8 bad, unsigned long timeout, u8 *rstat) |
1da177e4 | 491 | { |
74af21cf | 492 | ide_hwif_t *hwif = drive->hwif; |
1da177e4 | 493 | unsigned long flags; |
74af21cf BZ |
494 | int i; |
495 | u8 stat; | |
1da177e4 LT |
496 | |
497 | udelay(1); /* spec allows drive 400ns to assert "BUSY" */ | |
498 | if ((stat = hwif->INB(IDE_STATUS_REG)) & BUSY_STAT) { | |
499 | local_irq_set(flags); | |
500 | timeout += jiffies; | |
501 | while ((stat = hwif->INB(IDE_STATUS_REG)) & BUSY_STAT) { | |
502 | if (time_after(jiffies, timeout)) { | |
503 | /* | |
504 | * One last read after the timeout in case | |
505 | * heavy interrupt load made us not make any | |
506 | * progress during the timeout.. | |
507 | */ | |
508 | stat = hwif->INB(IDE_STATUS_REG); | |
509 | if (!(stat & BUSY_STAT)) | |
510 | break; | |
511 | ||
512 | local_irq_restore(flags); | |
74af21cf BZ |
513 | *rstat = stat; |
514 | return -EBUSY; | |
1da177e4 LT |
515 | } |
516 | } | |
517 | local_irq_restore(flags); | |
518 | } | |
519 | /* | |
520 | * Allow status to settle, then read it again. | |
521 | * A few rare drives vastly violate the 400ns spec here, | |
522 | * so we'll wait up to 10usec for a "good" status | |
523 | * rather than expensively fail things immediately. | |
524 | * This fix courtesy of Matthew Faupel & Niccolo Rigacci. | |
525 | */ | |
526 | for (i = 0; i < 10; i++) { | |
527 | udelay(1); | |
74af21cf BZ |
528 | if (OK_STAT((stat = hwif->INB(IDE_STATUS_REG)), good, bad)) { |
529 | *rstat = stat; | |
1da177e4 | 530 | return 0; |
74af21cf | 531 | } |
1da177e4 | 532 | } |
74af21cf BZ |
533 | *rstat = stat; |
534 | return -EFAULT; | |
535 | } | |
536 | ||
537 | /* | |
538 | * In case of error returns error value after doing "*startstop = ide_error()". | |
539 | * The caller should return the updated value of "startstop" in this case, | |
540 | * "startstop" is unchanged when the function returns 0. | |
541 | */ | |
542 | int ide_wait_stat(ide_startstop_t *startstop, ide_drive_t *drive, u8 good, u8 bad, unsigned long timeout) | |
543 | { | |
544 | int err; | |
545 | u8 stat; | |
546 | ||
547 | /* bail early if we've exceeded max_failures */ | |
548 | if (drive->max_failures && (drive->failures > drive->max_failures)) { | |
549 | *startstop = ide_stopped; | |
550 | return 1; | |
551 | } | |
552 | ||
553 | err = __ide_wait_stat(drive, good, bad, timeout, &stat); | |
554 | ||
555 | if (err) { | |
556 | char *s = (err == -EBUSY) ? "status timeout" : "status error"; | |
557 | *startstop = ide_error(drive, s, stat); | |
558 | } | |
559 | ||
560 | return err; | |
1da177e4 LT |
561 | } |
562 | ||
563 | EXPORT_SYMBOL(ide_wait_stat); | |
564 | ||
a5b7e70d BZ |
565 | /** |
566 | * ide_in_drive_list - look for drive in black/white list | |
567 | * @id: drive identifier | |
568 | * @drive_table: list to inspect | |
569 | * | |
570 | * Look for a drive in the blacklist and the whitelist tables | |
571 | * Returns 1 if the drive is found in the table. | |
572 | */ | |
573 | ||
574 | int ide_in_drive_list(struct hd_driveid *id, const struct drive_list_entry *drive_table) | |
575 | { | |
576 | for ( ; drive_table->id_model; drive_table++) | |
577 | if ((!strcmp(drive_table->id_model, id->model)) && | |
578 | (!drive_table->id_firmware || | |
579 | strstr(id->fw_rev, drive_table->id_firmware))) | |
580 | return 1; | |
581 | return 0; | |
582 | } | |
583 | ||
b0244a00 BZ |
584 | EXPORT_SYMBOL_GPL(ide_in_drive_list); |
585 | ||
a5b7e70d BZ |
586 | /* |
587 | * Early UDMA66 devices don't set bit14 to 1, only bit13 is valid. | |
588 | * We list them here and depend on the device side cable detection for them. | |
8588a2b7 BZ |
589 | * |
590 | * Some optical devices with the buggy firmwares have the same problem. | |
a5b7e70d BZ |
591 | */ |
592 | static const struct drive_list_entry ivb_list[] = { | |
593 | { "QUANTUM FIREBALLlct10 05" , "A03.0900" }, | |
8588a2b7 | 594 | { "TSSTcorp CDDVDW SH-S202J" , "SB00" }, |
a5b7e70d BZ |
595 | { NULL , NULL } |
596 | }; | |
597 | ||
1da177e4 LT |
598 | /* |
599 | * All hosts that use the 80c ribbon must use! | |
600 | * The name is derived from upper byte of word 93 and the 80c ribbon. | |
601 | */ | |
602 | u8 eighty_ninty_three (ide_drive_t *drive) | |
603 | { | |
7f8f48af BZ |
604 | ide_hwif_t *hwif = drive->hwif; |
605 | struct hd_driveid *id = drive->id; | |
a5b7e70d | 606 | int ivb = ide_in_drive_list(id, ivb_list); |
7f8f48af | 607 | |
49521f97 BZ |
608 | if (hwif->cbl == ATA_CBL_PATA40_SHORT) |
609 | return 1; | |
610 | ||
a5b7e70d BZ |
611 | if (ivb) |
612 | printk(KERN_DEBUG "%s: skipping word 93 validity check\n", | |
613 | drive->name); | |
614 | ||
615 | if (hwif->cbl != ATA_CBL_PATA80 && !ivb) | |
7f8f48af | 616 | goto no_80w; |
1a1276e7 | 617 | |
6c3c22f3 | 618 | if (ide_dev_is_sata(id)) |
1a1276e7 | 619 | return 1; |
7f8f48af | 620 | |
f68d9320 BZ |
621 | /* |
622 | * FIXME: | |
a5b7e70d | 623 | * - force bit13 (80c cable present) check also for !ivb devices |
f68d9320 BZ |
624 | * (unless the slave device is pre-ATA3) |
625 | */ | |
a5b7e70d | 626 | if ((id->hw_config & 0x4000) || (ivb && (id->hw_config & 0x2000))) |
7f8f48af BZ |
627 | return 1; |
628 | ||
629 | no_80w: | |
630 | if (drive->udma33_warned == 1) | |
631 | return 0; | |
632 | ||
633 | printk(KERN_WARNING "%s: %s side 80-wire cable detection failed, " | |
634 | "limiting max speed to UDMA33\n", | |
49521f97 BZ |
635 | drive->name, |
636 | hwif->cbl == ATA_CBL_PATA80 ? "drive" : "host"); | |
7f8f48af BZ |
637 | |
638 | drive->udma33_warned = 1; | |
639 | ||
640 | return 0; | |
1da177e4 LT |
641 | } |
642 | ||
1da177e4 LT |
643 | int ide_ata66_check (ide_drive_t *drive, ide_task_t *args) |
644 | { | |
645 | if ((args->tfRegister[IDE_COMMAND_OFFSET] == WIN_SETFEATURES) && | |
646 | (args->tfRegister[IDE_SECTOR_OFFSET] > XFER_UDMA_2) && | |
647 | (args->tfRegister[IDE_FEATURE_OFFSET] == SETFEATURES_XFER)) { | |
7f8f48af BZ |
648 | if (eighty_ninty_three(drive) == 0) { |
649 | printk(KERN_WARNING "%s: UDMA speeds >UDMA33 cannot " | |
650 | "be set\n", drive->name); | |
1da177e4 LT |
651 | return 1; |
652 | } | |
653 | } | |
7f8f48af | 654 | |
1da177e4 LT |
655 | return 0; |
656 | } | |
657 | ||
658 | /* | |
659 | * Backside of HDIO_DRIVE_CMD call of SETFEATURES_XFER. | |
660 | * 1 : Safe to update drive->id DMA registers. | |
661 | * 0 : OOPs not allowed. | |
662 | */ | |
663 | int set_transfer (ide_drive_t *drive, ide_task_t *args) | |
664 | { | |
665 | if ((args->tfRegister[IDE_COMMAND_OFFSET] == WIN_SETFEATURES) && | |
666 | (args->tfRegister[IDE_SECTOR_OFFSET] >= XFER_SW_DMA_0) && | |
667 | (args->tfRegister[IDE_FEATURE_OFFSET] == SETFEATURES_XFER) && | |
668 | (drive->id->dma_ultra || | |
669 | drive->id->dma_mword || | |
670 | drive->id->dma_1word)) | |
671 | return 1; | |
672 | ||
673 | return 0; | |
674 | } | |
675 | ||
676 | #ifdef CONFIG_BLK_DEV_IDEDMA | |
677 | static u8 ide_auto_reduce_xfer (ide_drive_t *drive) | |
678 | { | |
679 | if (!drive->crc_count) | |
680 | return drive->current_speed; | |
681 | drive->crc_count = 0; | |
682 | ||
683 | switch(drive->current_speed) { | |
684 | case XFER_UDMA_7: return XFER_UDMA_6; | |
685 | case XFER_UDMA_6: return XFER_UDMA_5; | |
686 | case XFER_UDMA_5: return XFER_UDMA_4; | |
687 | case XFER_UDMA_4: return XFER_UDMA_3; | |
688 | case XFER_UDMA_3: return XFER_UDMA_2; | |
689 | case XFER_UDMA_2: return XFER_UDMA_1; | |
690 | case XFER_UDMA_1: return XFER_UDMA_0; | |
691 | /* | |
692 | * OOPS we do not goto non Ultra DMA modes | |
693 | * without iCRC's available we force | |
694 | * the system to PIO and make the user | |
695 | * invoke the ATA-1 ATA-2 DMA modes. | |
696 | */ | |
697 | case XFER_UDMA_0: | |
698 | default: return XFER_PIO_4; | |
699 | } | |
700 | } | |
701 | #endif /* CONFIG_BLK_DEV_IDEDMA */ | |
702 | ||
8a455134 | 703 | int ide_driveid_update(ide_drive_t *drive) |
1da177e4 | 704 | { |
8a455134 | 705 | ide_hwif_t *hwif = drive->hwif; |
1da177e4 | 706 | struct hd_driveid *id; |
8a455134 | 707 | unsigned long timeout, flags; |
1da177e4 | 708 | |
1da177e4 LT |
709 | /* |
710 | * Re-read drive->id for possible DMA mode | |
711 | * change (copied from ide-probe.c) | |
712 | */ | |
1da177e4 LT |
713 | |
714 | SELECT_MASK(drive, 1); | |
715 | if (IDE_CONTROL_REG) | |
716 | hwif->OUTB(drive->ctl,IDE_CONTROL_REG); | |
717 | msleep(50); | |
718 | hwif->OUTB(WIN_IDENTIFY, IDE_COMMAND_REG); | |
719 | timeout = jiffies + WAIT_WORSTCASE; | |
720 | do { | |
721 | if (time_after(jiffies, timeout)) { | |
722 | SELECT_MASK(drive, 0); | |
723 | return 0; /* drive timed-out */ | |
724 | } | |
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); | |
731 | return 0; | |
732 | } | |
733 | local_irq_save(flags); | |
734 | SELECT_MASK(drive, 0); | |
735 | id = kmalloc(SECTOR_WORDS*4, GFP_ATOMIC); | |
736 | if (!id) { | |
737 | local_irq_restore(flags); | |
738 | return 0; | |
739 | } | |
740 | ata_input_data(drive, id, SECTOR_WORDS); | |
741 | (void) hwif->INB(IDE_STATUS_REG); /* clear drive IRQ */ | |
742 | local_irq_enable(); | |
743 | local_irq_restore(flags); | |
744 | ide_fix_driveid(id); | |
745 | if (id) { | |
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 ? */ | |
750 | kfree(id); | |
751 | } | |
752 | ||
753 | return 1; | |
1da177e4 LT |
754 | } |
755 | ||
74af21cf | 756 | int ide_config_drive_speed(ide_drive_t *drive, u8 speed) |
1da177e4 | 757 | { |
74af21cf BZ |
758 | ide_hwif_t *hwif = drive->hwif; |
759 | int error; | |
1da177e4 LT |
760 | u8 stat; |
761 | ||
762 | // while (HWGROUP(drive)->busy) | |
763 | // msleep(50); | |
764 | ||
765 | #ifdef CONFIG_BLK_DEV_IDEDMA | |
0ae2e178 | 766 | if (hwif->ide_dma_on) /* check if host supports DMA */ |
7469aaf6 | 767 | hwif->dma_host_off(drive); |
1da177e4 LT |
768 | #endif |
769 | ||
770 | /* | |
771 | * Don't use ide_wait_cmd here - it will | |
772 | * attempt to set_geometry and recalibrate, | |
773 | * but for some reason these don't work at | |
774 | * this point (lost interrupt). | |
775 | */ | |
776 | /* | |
777 | * Select the drive, and issue the SETFEATURES command | |
778 | */ | |
779 | disable_irq_nosync(hwif->irq); | |
780 | ||
781 | /* | |
782 | * FIXME: we race against the running IRQ here if | |
783 | * this is called from non IRQ context. If we use | |
784 | * disable_irq() we hang on the error path. Work | |
785 | * is needed. | |
786 | */ | |
787 | ||
788 | udelay(1); | |
789 | SELECT_DRIVE(drive); | |
790 | SELECT_MASK(drive, 0); | |
791 | udelay(1); | |
792 | if (IDE_CONTROL_REG) | |
793 | hwif->OUTB(drive->ctl | 2, IDE_CONTROL_REG); | |
794 | hwif->OUTB(speed, IDE_NSECTOR_REG); | |
795 | hwif->OUTB(SETFEATURES_XFER, IDE_FEATURE_REG); | |
adcd33d4 | 796 | hwif->OUTBSYNC(drive, WIN_SETFEATURES, IDE_COMMAND_REG); |
1da177e4 LT |
797 | if ((IDE_CONTROL_REG) && (drive->quirk_list == 2)) |
798 | hwif->OUTB(drive->ctl, IDE_CONTROL_REG); | |
1da177e4 | 799 | |
74af21cf BZ |
800 | error = __ide_wait_stat(drive, drive->ready_stat, |
801 | BUSY_STAT|DRQ_STAT|ERR_STAT, | |
802 | WAIT_CMD, &stat); | |
1da177e4 LT |
803 | |
804 | SELECT_MASK(drive, 0); | |
805 | ||
806 | enable_irq(hwif->irq); | |
807 | ||
808 | if (error) { | |
809 | (void) ide_dump_status(drive, "set_drive_speed_status", stat); | |
810 | return error; | |
811 | } | |
812 | ||
813 | drive->id->dma_ultra &= ~0xFF00; | |
814 | drive->id->dma_mword &= ~0x0F00; | |
815 | drive->id->dma_1word &= ~0x0F00; | |
816 | ||
817 | #ifdef CONFIG_BLK_DEV_IDEDMA | |
818 | if (speed >= XFER_SW_DMA_0) | |
ccf35289 | 819 | hwif->dma_host_on(drive); |
0ae2e178 | 820 | else if (hwif->ide_dma_on) /* check if host supports DMA */ |
7469aaf6 | 821 | hwif->dma_off_quietly(drive); |
1da177e4 LT |
822 | #endif |
823 | ||
824 | switch(speed) { | |
825 | case XFER_UDMA_7: drive->id->dma_ultra |= 0x8080; break; | |
826 | case XFER_UDMA_6: drive->id->dma_ultra |= 0x4040; break; | |
827 | case XFER_UDMA_5: drive->id->dma_ultra |= 0x2020; break; | |
828 | case XFER_UDMA_4: drive->id->dma_ultra |= 0x1010; break; | |
829 | case XFER_UDMA_3: drive->id->dma_ultra |= 0x0808; break; | |
830 | case XFER_UDMA_2: drive->id->dma_ultra |= 0x0404; break; | |
831 | case XFER_UDMA_1: drive->id->dma_ultra |= 0x0202; break; | |
832 | case XFER_UDMA_0: drive->id->dma_ultra |= 0x0101; break; | |
833 | case XFER_MW_DMA_2: drive->id->dma_mword |= 0x0404; break; | |
834 | case XFER_MW_DMA_1: drive->id->dma_mword |= 0x0202; break; | |
835 | case XFER_MW_DMA_0: drive->id->dma_mword |= 0x0101; break; | |
836 | case XFER_SW_DMA_2: drive->id->dma_1word |= 0x0404; break; | |
837 | case XFER_SW_DMA_1: drive->id->dma_1word |= 0x0202; break; | |
838 | case XFER_SW_DMA_0: drive->id->dma_1word |= 0x0101; break; | |
839 | default: break; | |
840 | } | |
841 | if (!drive->init_speed) | |
842 | drive->init_speed = speed; | |
843 | drive->current_speed = speed; | |
844 | return error; | |
845 | } | |
846 | ||
1da177e4 LT |
847 | /* |
848 | * This should get invoked any time we exit the driver to | |
849 | * wait for an interrupt response from a drive. handler() points | |
850 | * at the appropriate code to handle the next interrupt, and a | |
851 | * timer is started to prevent us from waiting forever in case | |
852 | * something goes wrong (see the ide_timer_expiry() handler later on). | |
853 | * | |
854 | * See also ide_execute_command | |
855 | */ | |
856 | static void __ide_set_handler (ide_drive_t *drive, ide_handler_t *handler, | |
857 | unsigned int timeout, ide_expiry_t *expiry) | |
858 | { | |
859 | ide_hwgroup_t *hwgroup = HWGROUP(drive); | |
860 | ||
861 | if (hwgroup->handler != NULL) { | |
862 | printk(KERN_CRIT "%s: ide_set_handler: handler not null; " | |
863 | "old=%p, new=%p\n", | |
864 | drive->name, hwgroup->handler, handler); | |
865 | } | |
866 | hwgroup->handler = handler; | |
867 | hwgroup->expiry = expiry; | |
868 | hwgroup->timer.expires = jiffies + timeout; | |
23450319 | 869 | hwgroup->req_gen_timer = hwgroup->req_gen; |
1da177e4 LT |
870 | add_timer(&hwgroup->timer); |
871 | } | |
872 | ||
873 | void ide_set_handler (ide_drive_t *drive, ide_handler_t *handler, | |
874 | unsigned int timeout, ide_expiry_t *expiry) | |
875 | { | |
876 | unsigned long flags; | |
877 | spin_lock_irqsave(&ide_lock, flags); | |
878 | __ide_set_handler(drive, handler, timeout, expiry); | |
879 | spin_unlock_irqrestore(&ide_lock, flags); | |
880 | } | |
881 | ||
882 | EXPORT_SYMBOL(ide_set_handler); | |
883 | ||
884 | /** | |
885 | * ide_execute_command - execute an IDE command | |
886 | * @drive: IDE drive to issue the command against | |
887 | * @command: command byte to write | |
888 | * @handler: handler for next phase | |
889 | * @timeout: timeout for command | |
890 | * @expiry: handler to run on timeout | |
891 | * | |
892 | * Helper function to issue an IDE command. This handles the | |
893 | * atomicity requirements, command timing and ensures that the | |
894 | * handler and IRQ setup do not race. All IDE command kick off | |
895 | * should go via this function or do equivalent locking. | |
896 | */ | |
897 | ||
898 | void ide_execute_command(ide_drive_t *drive, task_ioreg_t cmd, ide_handler_t *handler, unsigned timeout, ide_expiry_t *expiry) | |
899 | { | |
900 | unsigned long flags; | |
901 | ide_hwgroup_t *hwgroup = HWGROUP(drive); | |
902 | ide_hwif_t *hwif = HWIF(drive); | |
903 | ||
904 | spin_lock_irqsave(&ide_lock, flags); | |
905 | ||
125e1874 | 906 | BUG_ON(hwgroup->handler); |
1da177e4 LT |
907 | hwgroup->handler = handler; |
908 | hwgroup->expiry = expiry; | |
909 | hwgroup->timer.expires = jiffies + timeout; | |
23450319 | 910 | hwgroup->req_gen_timer = hwgroup->req_gen; |
1da177e4 LT |
911 | add_timer(&hwgroup->timer); |
912 | hwif->OUTBSYNC(drive, cmd, IDE_COMMAND_REG); | |
913 | /* Drive takes 400nS to respond, we must avoid the IRQ being | |
914 | serviced before that. | |
915 | ||
916 | FIXME: we could skip this delay with care on non shared | |
917 | devices | |
918 | */ | |
919 | ndelay(400); | |
920 | spin_unlock_irqrestore(&ide_lock, flags); | |
921 | } | |
922 | ||
923 | EXPORT_SYMBOL(ide_execute_command); | |
924 | ||
925 | ||
926 | /* needed below */ | |
927 | static ide_startstop_t do_reset1 (ide_drive_t *, int); | |
928 | ||
929 | /* | |
930 | * atapi_reset_pollfunc() gets invoked to poll the interface for completion every 50ms | |
931 | * during an atapi drive reset operation. If the drive has not yet responded, | |
932 | * and we have not yet hit our maximum waiting time, then the timer is restarted | |
933 | * for another 50ms. | |
934 | */ | |
935 | static ide_startstop_t atapi_reset_pollfunc (ide_drive_t *drive) | |
936 | { | |
937 | ide_hwgroup_t *hwgroup = HWGROUP(drive); | |
938 | ide_hwif_t *hwif = HWIF(drive); | |
939 | u8 stat; | |
940 | ||
941 | SELECT_DRIVE(drive); | |
942 | udelay (10); | |
943 | ||
944 | if (OK_STAT(stat = hwif->INB(IDE_STATUS_REG), 0, BUSY_STAT)) { | |
945 | printk("%s: ATAPI reset complete\n", drive->name); | |
946 | } else { | |
947 | if (time_before(jiffies, hwgroup->poll_timeout)) { | |
125e1874 | 948 | BUG_ON(HWGROUP(drive)->handler != NULL); |
1da177e4 LT |
949 | ide_set_handler(drive, &atapi_reset_pollfunc, HZ/20, NULL); |
950 | /* continue polling */ | |
951 | return ide_started; | |
952 | } | |
953 | /* end of polling */ | |
954 | hwgroup->polling = 0; | |
955 | printk("%s: ATAPI reset timed-out, status=0x%02x\n", | |
956 | drive->name, stat); | |
957 | /* do it the old fashioned way */ | |
958 | return do_reset1(drive, 1); | |
959 | } | |
960 | /* done polling */ | |
961 | hwgroup->polling = 0; | |
913759ac | 962 | hwgroup->resetting = 0; |
1da177e4 LT |
963 | return ide_stopped; |
964 | } | |
965 | ||
966 | /* | |
967 | * reset_pollfunc() gets invoked to poll the interface for completion every 50ms | |
968 | * during an ide reset operation. If the drives have not yet responded, | |
969 | * and we have not yet hit our maximum waiting time, then the timer is restarted | |
970 | * for another 50ms. | |
971 | */ | |
972 | static ide_startstop_t reset_pollfunc (ide_drive_t *drive) | |
973 | { | |
974 | ide_hwgroup_t *hwgroup = HWGROUP(drive); | |
975 | ide_hwif_t *hwif = HWIF(drive); | |
976 | u8 tmp; | |
977 | ||
978 | if (hwif->reset_poll != NULL) { | |
979 | if (hwif->reset_poll(drive)) { | |
980 | printk(KERN_ERR "%s: host reset_poll failure for %s.\n", | |
981 | hwif->name, drive->name); | |
982 | return ide_stopped; | |
983 | } | |
984 | } | |
985 | ||
986 | if (!OK_STAT(tmp = hwif->INB(IDE_STATUS_REG), 0, BUSY_STAT)) { | |
987 | if (time_before(jiffies, hwgroup->poll_timeout)) { | |
125e1874 | 988 | BUG_ON(HWGROUP(drive)->handler != NULL); |
1da177e4 LT |
989 | ide_set_handler(drive, &reset_pollfunc, HZ/20, NULL); |
990 | /* continue polling */ | |
991 | return ide_started; | |
992 | } | |
993 | printk("%s: reset timed-out, status=0x%02x\n", hwif->name, tmp); | |
994 | drive->failures++; | |
995 | } else { | |
996 | printk("%s: reset: ", hwif->name); | |
997 | if ((tmp = hwif->INB(IDE_ERROR_REG)) == 1) { | |
998 | printk("success\n"); | |
999 | drive->failures = 0; | |
1000 | } else { | |
1001 | drive->failures++; | |
1002 | printk("master: "); | |
1003 | switch (tmp & 0x7f) { | |
1004 | case 1: printk("passed"); | |
1005 | break; | |
1006 | case 2: printk("formatter device error"); | |
1007 | break; | |
1008 | case 3: printk("sector buffer error"); | |
1009 | break; | |
1010 | case 4: printk("ECC circuitry error"); | |
1011 | break; | |
1012 | case 5: printk("controlling MPU error"); | |
1013 | break; | |
1014 | default:printk("error (0x%02x?)", tmp); | |
1015 | } | |
1016 | if (tmp & 0x80) | |
1017 | printk("; slave: failed"); | |
1018 | printk("\n"); | |
1019 | } | |
1020 | } | |
1021 | hwgroup->polling = 0; /* done polling */ | |
913759ac | 1022 | hwgroup->resetting = 0; /* done reset attempt */ |
1da177e4 LT |
1023 | return ide_stopped; |
1024 | } | |
1025 | ||
1026 | static void check_dma_crc(ide_drive_t *drive) | |
1027 | { | |
1028 | #ifdef CONFIG_BLK_DEV_IDEDMA | |
1029 | if (drive->crc_count) { | |
7469aaf6 | 1030 | drive->hwif->dma_off_quietly(drive); |
1da177e4 LT |
1031 | ide_set_xfer_rate(drive, ide_auto_reduce_xfer(drive)); |
1032 | if (drive->current_speed >= XFER_SW_DMA_0) | |
1033 | (void) HWIF(drive)->ide_dma_on(drive); | |
1034 | } else | |
7469aaf6 | 1035 | ide_dma_off(drive); |
1da177e4 LT |
1036 | #endif |
1037 | } | |
1038 | ||
1039 | static void ide_disk_pre_reset(ide_drive_t *drive) | |
1040 | { | |
1041 | int legacy = (drive->id->cfs_enable_2 & 0x0400) ? 0 : 1; | |
1042 | ||
1043 | drive->special.all = 0; | |
1044 | drive->special.b.set_geometry = legacy; | |
1045 | drive->special.b.recalibrate = legacy; | |
1046 | if (OK_TO_RESET_CONTROLLER) | |
1047 | drive->mult_count = 0; | |
1048 | if (!drive->keep_settings && !drive->using_dma) | |
1049 | drive->mult_req = 0; | |
1050 | if (drive->mult_req != drive->mult_count) | |
1051 | drive->special.b.set_multmode = 1; | |
1052 | } | |
1053 | ||
1054 | static void pre_reset(ide_drive_t *drive) | |
1055 | { | |
1056 | if (drive->media == ide_disk) | |
1057 | ide_disk_pre_reset(drive); | |
1058 | else | |
1059 | drive->post_reset = 1; | |
1060 | ||
1061 | if (!drive->keep_settings) { | |
1062 | if (drive->using_dma) { | |
1063 | check_dma_crc(drive); | |
1064 | } else { | |
1065 | drive->unmask = 0; | |
1066 | drive->io_32bit = 0; | |
1067 | } | |
1068 | return; | |
1069 | } | |
1070 | if (drive->using_dma) | |
1071 | check_dma_crc(drive); | |
1072 | ||
1073 | if (HWIF(drive)->pre_reset != NULL) | |
1074 | HWIF(drive)->pre_reset(drive); | |
1075 | ||
513daadd SS |
1076 | if (drive->current_speed != 0xff) |
1077 | drive->desired_speed = drive->current_speed; | |
1078 | drive->current_speed = 0xff; | |
1da177e4 LT |
1079 | } |
1080 | ||
1081 | /* | |
1082 | * do_reset1() attempts to recover a confused drive by resetting it. | |
1083 | * Unfortunately, resetting a disk drive actually resets all devices on | |
1084 | * the same interface, so it can really be thought of as resetting the | |
1085 | * interface rather than resetting the drive. | |
1086 | * | |
1087 | * ATAPI devices have their own reset mechanism which allows them to be | |
1088 | * individually reset without clobbering other devices on the same interface. | |
1089 | * | |
1090 | * Unfortunately, the IDE interface does not generate an interrupt to let | |
1091 | * us know when the reset operation has finished, so we must poll for this. | |
1092 | * Equally poor, though, is the fact that this may a very long time to complete, | |
1093 | * (up to 30 seconds worstcase). So, instead of busy-waiting here for it, | |
1094 | * we set a timer to poll at 50ms intervals. | |
1095 | */ | |
1096 | static ide_startstop_t do_reset1 (ide_drive_t *drive, int do_not_try_atapi) | |
1097 | { | |
1098 | unsigned int unit; | |
1099 | unsigned long flags; | |
1100 | ide_hwif_t *hwif; | |
1101 | ide_hwgroup_t *hwgroup; | |
1102 | ||
1103 | spin_lock_irqsave(&ide_lock, flags); | |
1104 | hwif = HWIF(drive); | |
1105 | hwgroup = HWGROUP(drive); | |
1106 | ||
1107 | /* We must not reset with running handlers */ | |
125e1874 | 1108 | BUG_ON(hwgroup->handler != NULL); |
1da177e4 LT |
1109 | |
1110 | /* For an ATAPI device, first try an ATAPI SRST. */ | |
1111 | if (drive->media != ide_disk && !do_not_try_atapi) { | |
913759ac | 1112 | hwgroup->resetting = 1; |
1da177e4 LT |
1113 | pre_reset(drive); |
1114 | SELECT_DRIVE(drive); | |
1115 | udelay (20); | |
68ad9910 AC |
1116 | hwif->OUTBSYNC(drive, WIN_SRST, IDE_COMMAND_REG); |
1117 | ndelay(400); | |
1da177e4 LT |
1118 | hwgroup->poll_timeout = jiffies + WAIT_WORSTCASE; |
1119 | hwgroup->polling = 1; | |
1120 | __ide_set_handler(drive, &atapi_reset_pollfunc, HZ/20, NULL); | |
1121 | spin_unlock_irqrestore(&ide_lock, flags); | |
1122 | return ide_started; | |
1123 | } | |
1124 | ||
1125 | /* | |
1126 | * First, reset any device state data we were maintaining | |
1127 | * for any of the drives on this interface. | |
1128 | */ | |
1129 | for (unit = 0; unit < MAX_DRIVES; ++unit) | |
1130 | pre_reset(&hwif->drives[unit]); | |
1131 | ||
1132 | #if OK_TO_RESET_CONTROLLER | |
1133 | if (!IDE_CONTROL_REG) { | |
1134 | spin_unlock_irqrestore(&ide_lock, flags); | |
1135 | return ide_stopped; | |
1136 | } | |
1137 | ||
913759ac | 1138 | hwgroup->resetting = 1; |
1da177e4 LT |
1139 | /* |
1140 | * Note that we also set nIEN while resetting the device, | |
1141 | * to mask unwanted interrupts from the interface during the reset. | |
1142 | * However, due to the design of PC hardware, this will cause an | |
1143 | * immediate interrupt due to the edge transition it produces. | |
1144 | * This single interrupt gives us a "fast poll" for drives that | |
1145 | * recover from reset very quickly, saving us the first 50ms wait time. | |
1146 | */ | |
1147 | /* set SRST and nIEN */ | |
1148 | hwif->OUTBSYNC(drive, drive->ctl|6,IDE_CONTROL_REG); | |
1149 | /* more than enough time */ | |
1150 | udelay(10); | |
1151 | if (drive->quirk_list == 2) { | |
1152 | /* clear SRST and nIEN */ | |
1153 | hwif->OUTBSYNC(drive, drive->ctl, IDE_CONTROL_REG); | |
1154 | } else { | |
1155 | /* clear SRST, leave nIEN */ | |
1156 | hwif->OUTBSYNC(drive, drive->ctl|2, IDE_CONTROL_REG); | |
1157 | } | |
1158 | /* more than enough time */ | |
1159 | udelay(10); | |
1160 | hwgroup->poll_timeout = jiffies + WAIT_WORSTCASE; | |
1161 | hwgroup->polling = 1; | |
1162 | __ide_set_handler(drive, &reset_pollfunc, HZ/20, NULL); | |
1163 | ||
1164 | /* | |
1165 | * Some weird controller like resetting themselves to a strange | |
1166 | * state when the disks are reset this way. At least, the Winbond | |
1167 | * 553 documentation says that | |
1168 | */ | |
1169 | if (hwif->resetproc != NULL) { | |
1170 | hwif->resetproc(drive); | |
1171 | } | |
1172 | ||
1173 | #endif /* OK_TO_RESET_CONTROLLER */ | |
1174 | ||
1175 | spin_unlock_irqrestore(&ide_lock, flags); | |
1176 | return ide_started; | |
1177 | } | |
1178 | ||
1179 | /* | |
1180 | * ide_do_reset() is the entry point to the drive/interface reset code. | |
1181 | */ | |
1182 | ||
1183 | ide_startstop_t ide_do_reset (ide_drive_t *drive) | |
1184 | { | |
1185 | return do_reset1(drive, 0); | |
1186 | } | |
1187 | ||
1188 | EXPORT_SYMBOL(ide_do_reset); | |
1189 | ||
1190 | /* | |
1191 | * ide_wait_not_busy() waits for the currently selected device on the hwif | |
1192 | * to report a non-busy status, see comments in probe_hwif(). | |
1193 | */ | |
1194 | int ide_wait_not_busy(ide_hwif_t *hwif, unsigned long timeout) | |
1195 | { | |
1196 | u8 stat = 0; | |
1197 | ||
1198 | while(timeout--) { | |
1199 | /* | |
1200 | * Turn this into a schedule() sleep once I'm sure | |
1201 | * about locking issues (2.5 work ?). | |
1202 | */ | |
1203 | mdelay(1); | |
1204 | stat = hwif->INB(hwif->io_ports[IDE_STATUS_OFFSET]); | |
1205 | if ((stat & BUSY_STAT) == 0) | |
1206 | return 0; | |
1207 | /* | |
1208 | * Assume a value of 0xff means nothing is connected to | |
1209 | * the interface and it doesn't implement the pull-down | |
1210 | * resistor on D7. | |
1211 | */ | |
1212 | if (stat == 0xff) | |
1213 | return -ENODEV; | |
6842f8c8 | 1214 | touch_softlockup_watchdog(); |
1e86240f | 1215 | touch_nmi_watchdog(); |
1da177e4 LT |
1216 | } |
1217 | return -EBUSY; | |
1218 | } | |
1219 | ||
1220 | EXPORT_SYMBOL_GPL(ide_wait_not_busy); | |
1221 |