File: /usr/src/linux/drivers/ide/ide.c
1 /*
2 * linux/drivers/ide/ide.c Version 6.31 June 9, 2000
3 *
4 * Copyright (C) 1994-1998 Linus Torvalds & authors (see below)
5 */
6
7 /*
8 * Mostly written by Mark Lord <mlord@pobox.com>
9 * and Gadi Oxman <gadio@netvision.net.il>
10 * and Andre Hedrick <andre@linux-ide.org>
11 *
12 * See linux/MAINTAINERS for address of current maintainer.
13 *
14 * This is the multiple IDE interface driver, as evolved from hd.c.
15 * It supports up to MAX_HWIFS IDE interfaces, on one or more IRQs (usually 14 & 15).
16 * There can be up to two drives per interface, as per the ATA-2 spec.
17 *
18 * Primary: ide0, port 0x1f0; major=3; hda is minor=0; hdb is minor=64
19 * Secondary: ide1, port 0x170; major=22; hdc is minor=0; hdd is minor=64
20 * Tertiary: ide2, port 0x???; major=33; hde is minor=0; hdf is minor=64
21 * Quaternary: ide3, port 0x???; major=34; hdg is minor=0; hdh is minor=64
22 * ...
23 *
24 * From hd.c:
25 * |
26 * | It traverses the request-list, using interrupts to jump between functions.
27 * | As nearly all functions can be called within interrupts, we may not sleep.
28 * | Special care is recommended. Have Fun!
29 * |
30 * | modified by Drew Eckhardt to check nr of hd's from the CMOS.
31 * |
32 * | Thanks to Branko Lankester, lankeste@fwi.uva.nl, who found a bug
33 * | in the early extended-partition checks and added DM partitions.
34 * |
35 * | Early work on error handling by Mika Liljeberg (liljeber@cs.Helsinki.FI).
36 * |
37 * | IRQ-unmask, drive-id, multiple-mode, support for ">16 heads",
38 * | and general streamlining by Mark Lord (mlord@pobox.com).
39 *
40 * October, 1994 -- Complete line-by-line overhaul for linux 1.1.x, by:
41 *
42 * Mark Lord (mlord@pobox.com) (IDE Perf.Pkg)
43 * Delman Lee (delman@ieee.org) ("Mr. atdisk2")
44 * Scott Snyder (snyder@fnald0.fnal.gov) (ATAPI IDE cd-rom)
45 *
46 * This was a rewrite of just about everything from hd.c, though some original
47 * code is still sprinkled about. Think of it as a major evolution, with
48 * inspiration from lots of linux users, esp. hamish@zot.apana.org.au
49 *
50 * Version 1.0 ALPHA initial code, primary i/f working okay
51 * Version 1.3 BETA dual i/f on shared irq tested & working!
52 * Version 1.4 BETA added auto probing for irq(s)
53 * Version 1.5 BETA added ALPHA (untested) support for IDE cd-roms,
54 * ...
55 * Version 5.50 allow values as small as 20 for idebus=
56 * Version 5.51 force non io_32bit in drive_cmd_intr()
57 * change delay_10ms() to delay_50ms() to fix problems
58 * Version 5.52 fix incorrect invalidation of removable devices
59 * add "hdx=slow" command line option
60 * Version 5.60 start to modularize the driver; the disk and ATAPI
61 * drivers can be compiled as loadable modules.
62 * move IDE probe code to ide-probe.c
63 * move IDE disk code to ide-disk.c
64 * add support for generic IDE device subdrivers
65 * add m68k code from Geert Uytterhoeven
66 * probe all interfaces by default
67 * add ioctl to (re)probe an interface
68 * Version 6.00 use per device request queues
69 * attempt to optimize shared hwgroup performance
70 * add ioctl to manually adjust bandwidth algorithms
71 * add kerneld support for the probe module
72 * fix bug in ide_error()
73 * fix bug in the first ide_get_lock() call for Atari
74 * don't flush leftover data for ATAPI devices
75 * Version 6.01 clear hwgroup->active while the hwgroup sleeps
76 * support HDIO_GETGEO for floppies
77 * Version 6.02 fix ide_ack_intr() call
78 * check partition table on floppies
79 * Version 6.03 handle bad status bit sequencing in ide_wait_stat()
80 * Version 6.10 deleted old entries from this list of updates
81 * replaced triton.c with ide-dma.c generic PCI DMA
82 * added support for BIOS-enabled UltraDMA
83 * rename all "promise" things to "pdc4030"
84 * fix EZ-DRIVE handling on small disks
85 * Version 6.11 fix probe error in ide_scan_devices()
86 * fix ancient "jiffies" polling bugs
87 * mask all hwgroup interrupts on each irq entry
88 * Version 6.12 integrate ioctl and proc interfaces
89 * fix parsing of "idex=" command line parameter
90 * Version 6.13 add support for ide4/ide5 courtesy rjones@orchestream.com
91 * Version 6.14 fixed IRQ sharing among PCI devices
92 * Version 6.15 added SMP awareness to IDE drivers
93 * Version 6.16 fixed various bugs; even more SMP friendly
94 * Version 6.17 fix for newest EZ-Drive problem
95 * Version 6.18 default unpartitioned-disk translation now "BIOS LBA"
96 * Version 6.19 Re-design for a UNIFORM driver for all platforms,
97 * model based on suggestions from Russell King and
98 * Geert Uytterhoeven
99 * Promise DC4030VL now supported.
100 * add support for ide6/ide7
101 * delay_50ms() changed to ide_delay_50ms() and exported.
102 * Version 6.20 Added/Fixed Generic ATA-66 support and hwif detection.
103 * Added hdx=flash to allow for second flash disk
104 * detection w/o the hang loop.
105 * Added support for ide8/ide9
106 * Added idex=ata66 for the quirky chipsets that are
107 * ATA-66 compliant, but have yet to determine a method
108 * of verification of the 80c cable presence.
109 * Specifically Promise's PDC20262 chipset.
110 * Version 6.21 Fixing/Fixed SMP spinlock issue with insight from an old
111 * hat that clarified original low level driver design.
112 * Version 6.30 Added SMP support; fixed multmode issues. -ml
113 * Version 6.31 Debug Share INTR's and request queue streaming
114 * Native ATA-100 support
115 * Prep for Cascades Project
116 *
117 * Some additional driver compile-time options are in ./include/linux/ide.h
118 *
119 * To do, in likely order of completion:
120 * - modify kernel to obtain BIOS geometry for drives on 2nd/3rd/4th i/f
121 *
122 */
123
124 #define REVISION "Revision: 6.31"
125 #define VERSION "Id: ide.c 6.31 2000/06/09"
126
127 #undef REALLY_SLOW_IO /* most systems can safely undef this */
128
129 #define _IDE_C /* Tell ide.h it's really us */
130
131 #include <linux/config.h>
132 #include <linux/module.h>
133 #include <linux/types.h>
134 #include <linux/string.h>
135 #include <linux/kernel.h>
136 #include <linux/timer.h>
137 #include <linux/mm.h>
138 #include <linux/interrupt.h>
139 #include <linux/major.h>
140 #include <linux/errno.h>
141 #include <linux/genhd.h>
142 #include <linux/blkpg.h>
143 #include <linux/slab.h>
144 #ifndef MODULE
145 #include <linux/init.h>
146 #endif /* MODULE */
147 #include <linux/pci.h>
148 #include <linux/delay.h>
149 #include <linux/ide.h>
150 #include <linux/devfs_fs_kernel.h>
151 #include <linux/completion.h>
152
153 #include <asm/byteorder.h>
154 #include <asm/irq.h>
155 #include <asm/uaccess.h>
156 #include <asm/io.h>
157 #include <asm/bitops.h>
158
159 #include "ide_modes.h"
160
161 #ifdef CONFIG_KMOD
162 #include <linux/kmod.h>
163 #endif /* CONFIG_KMOD */
164
165 /* default maximum number of failures */
166 #define IDE_DEFAULT_MAX_FAILURES 1
167
168 static const byte ide_hwif_to_major[] = { IDE0_MAJOR, IDE1_MAJOR, IDE2_MAJOR, IDE3_MAJOR, IDE4_MAJOR, IDE5_MAJOR, IDE6_MAJOR, IDE7_MAJOR, IDE8_MAJOR, IDE9_MAJOR };
169
170 static int idebus_parameter; /* holds the "idebus=" parameter */
171 static int system_bus_speed; /* holds what we think is VESA/PCI bus speed */
172 static int initializing; /* set while initializing built-in drivers */
173
174 #ifdef CONFIG_BLK_DEV_IDEPCI
175 static int ide_scan_direction; /* THIS was formerly 2.2.x pci=reverse */
176 #endif /* CONFIG_BLK_DEV_IDEPCI */
177
178 #if defined(__mc68000__) || defined(CONFIG_APUS)
179 /*
180 * ide_lock is used by the Atari code to obtain access to the IDE interrupt,
181 * which is shared between several drivers.
182 */
183 static int ide_lock;
184 #endif /* __mc68000__ || CONFIG_APUS */
185
186 int noautodma = 0;
187
188 /*
189 * ide_modules keeps track of the available IDE chipset/probe/driver modules.
190 */
191 ide_module_t *ide_modules;
192 ide_module_t *ide_probe;
193
194 /*
195 * This is declared extern in ide.h, for access by other IDE modules:
196 */
197 ide_hwif_t ide_hwifs[MAX_HWIFS]; /* master data repository */
198
199 #if (DISK_RECOVERY_TIME > 0)
200 /*
201 * For really screwy hardware (hey, at least it *can* be used with Linux)
202 * we can enforce a minimum delay time between successive operations.
203 */
204 static unsigned long read_timer (void)
205 {
206 unsigned long t, flags;
207 int i;
208
209 __save_flags(flags); /* local CPU only */
210 __cli(); /* local CPU only */
211 t = jiffies * 11932;
212 outb_p(0, 0x43);
213 i = inb_p(0x40);
214 i |= inb(0x40) << 8;
215 __restore_flags(flags); /* local CPU only */
216 return (t - i);
217 }
218 #endif /* DISK_RECOVERY_TIME */
219
220 static inline void set_recovery_timer (ide_hwif_t *hwif)
221 {
222 #if (DISK_RECOVERY_TIME > 0)
223 hwif->last_time = read_timer();
224 #endif /* DISK_RECOVERY_TIME */
225 }
226
227 /*
228 * Do not even *think* about calling this!
229 */
230 static void init_hwif_data (unsigned int index)
231 {
232 unsigned int unit;
233 hw_regs_t hw;
234 ide_hwif_t *hwif = &ide_hwifs[index];
235
236 /* bulk initialize hwif & drive info with zeros */
237 memset(hwif, 0, sizeof(ide_hwif_t));
238 memset(&hw, 0, sizeof(hw_regs_t));
239
240 /* fill in any non-zero initial values */
241 hwif->index = index;
242 ide_init_hwif_ports(&hw, ide_default_io_base(index), 0, &hwif->irq);
243 memcpy(&hwif->hw, &hw, sizeof(hw));
244 memcpy(hwif->io_ports, hw.io_ports, sizeof(hw.io_ports));
245 hwif->noprobe = !hwif->io_ports[IDE_DATA_OFFSET];
246 #ifdef CONFIG_BLK_DEV_HD
247 if (hwif->io_ports[IDE_DATA_OFFSET] == HD_DATA)
248 hwif->noprobe = 1; /* may be overridden by ide_setup() */
249 #endif /* CONFIG_BLK_DEV_HD */
250 hwif->major = ide_hwif_to_major[index];
251 hwif->name[0] = 'i';
252 hwif->name[1] = 'd';
253 hwif->name[2] = 'e';
254 hwif->name[3] = '0' + index;
255 hwif->bus_state = BUSSTATE_ON;
256 for (unit = 0; unit < MAX_DRIVES; ++unit) {
257 ide_drive_t *drive = &hwif->drives[unit];
258
259 drive->media = ide_disk;
260 drive->select.all = (unit<<4)|0xa0;
261 drive->hwif = hwif;
262 drive->ctl = 0x08;
263 drive->ready_stat = READY_STAT;
264 drive->bad_wstat = BAD_W_STAT;
265 drive->special.b.recalibrate = 1;
266 drive->special.b.set_geometry = 1;
267 drive->name[0] = 'h';
268 drive->name[1] = 'd';
269 drive->name[2] = 'a' + (index * MAX_DRIVES) + unit;
270 drive->max_failures = IDE_DEFAULT_MAX_FAILURES;
271 init_waitqueue_head(&drive->wqueue);
272 }
273 }
274
275 /*
276 * init_ide_data() sets reasonable default values into all fields
277 * of all instances of the hwifs and drives, but only on the first call.
278 * Subsequent calls have no effect (they don't wipe out anything).
279 *
280 * This routine is normally called at driver initialization time,
281 * but may also be called MUCH earlier during kernel "command-line"
282 * parameter processing. As such, we cannot depend on any other parts
283 * of the kernel (such as memory allocation) to be functioning yet.
284 *
285 * This is too bad, as otherwise we could dynamically allocate the
286 * ide_drive_t structs as needed, rather than always consuming memory
287 * for the max possible number (MAX_HWIFS * MAX_DRIVES) of them.
288 */
289 #define MAGIC_COOKIE 0x12345678
290 static void __init init_ide_data (void)
291 {
292 unsigned int index;
293 static unsigned long magic_cookie = MAGIC_COOKIE;
294
295 if (magic_cookie != MAGIC_COOKIE)
296 return; /* already initialized */
297 magic_cookie = 0;
298
299 /* Initialise all interface structures */
300 for (index = 0; index < MAX_HWIFS; ++index)
301 init_hwif_data(index);
302
303 /* Add default hw interfaces */
304 ide_init_default_hwifs();
305
306 idebus_parameter = 0;
307 system_bus_speed = 0;
308 }
309
310 /*
311 * CompactFlash cards and their brethern pretend to be removable hard disks, except:
312 * (1) they never have a slave unit, and
313 * (2) they don't have doorlock mechanisms.
314 * This test catches them, and is invoked elsewhere when setting appropriate config bits.
315 *
316 * FIXME: This treatment is probably applicable for *all* PCMCIA (PC CARD) devices,
317 * so in linux 2.3.x we should change this to just treat all PCMCIA drives this way,
318 * and get rid of the model-name tests below (too big of an interface change for 2.2.x).
319 * At that time, we might also consider parameterizing the timeouts and retries,
320 * since these are MUCH faster than mechanical drives. -M.Lord
321 */
322 int drive_is_flashcard (ide_drive_t *drive)
323 {
324 struct hd_driveid *id = drive->id;
325
326 if (drive->removable && id != NULL) {
327 if (id->config == 0x848a) return 1; /* CompactFlash */
328 if (!strncmp(id->model, "KODAK ATA_FLASH", 15) /* Kodak */
329 || !strncmp(id->model, "Hitachi CV", 10) /* Hitachi */
330 || !strncmp(id->model, "SunDisk SDCFB", 13) /* SunDisk */
331 || !strncmp(id->model, "HAGIWARA HPC", 12) /* Hagiwara */
332 || !strncmp(id->model, "LEXAR ATA_FLASH", 15) /* Lexar */
333 || !strncmp(id->model, "ATA_FLASH", 9)) /* Simple Tech */
334 {
335 return 1; /* yes, it is a flash memory card */
336 }
337 }
338 return 0; /* no, it is not a flash memory card */
339 }
340
341 /*
342 * ide_system_bus_speed() returns what we think is the system VESA/PCI
343 * bus speed (in MHz). This is used for calculating interface PIO timings.
344 * The default is 40 for known PCI systems, 50 otherwise.
345 * The "idebus=xx" parameter can be used to override this value.
346 * The actual value to be used is computed/displayed the first time through.
347 */
348 int ide_system_bus_speed (void)
349 {
350 if (!system_bus_speed) {
351 if (idebus_parameter)
352 system_bus_speed = idebus_parameter; /* user supplied value */
353 #ifdef CONFIG_PCI
354 else if (pci_present())
355 system_bus_speed = 33; /* safe default value for PCI */
356 #endif /* CONFIG_PCI */
357 else
358 system_bus_speed = 50; /* safe default value for VESA and PCI */
359 printk("ide: Assuming %dMHz system bus speed for PIO modes%s\n", system_bus_speed,
360 idebus_parameter ? "" : "; override with idebus=xx");
361 }
362 return system_bus_speed;
363 }
364
365 #if SUPPORT_VLB_SYNC
366 /*
367 * Some localbus EIDE interfaces require a special access sequence
368 * when using 32-bit I/O instructions to transfer data. We call this
369 * the "vlb_sync" sequence, which consists of three successive reads
370 * of the sector count register location, with interrupts disabled
371 * to ensure that the reads all happen together.
372 */
373 static inline void do_vlb_sync (ide_ioreg_t port) {
374 (void) inb (port);
375 (void) inb (port);
376 (void) inb (port);
377 }
378 #endif /* SUPPORT_VLB_SYNC */
379
380 /*
381 * This is used for most PIO data transfers *from* the IDE interface
382 */
383 void ide_input_data (ide_drive_t *drive, void *buffer, unsigned int wcount)
384 {
385 byte io_32bit;
386
387 /* first check if this controller has defined a special function
388 * for handling polled ide transfers
389 */
390
391 if(HWIF(drive)->ideproc) {
392 HWIF(drive)->ideproc(ideproc_ide_input_data,
393 drive, buffer, wcount);
394 return;
395 }
396
397 io_32bit = drive->io_32bit;
398
399 if (io_32bit) {
400 #if SUPPORT_VLB_SYNC
401 if (io_32bit & 2) {
402 unsigned long flags;
403 __save_flags(flags); /* local CPU only */
404 __cli(); /* local CPU only */
405 do_vlb_sync(IDE_NSECTOR_REG);
406 insl(IDE_DATA_REG, buffer, wcount);
407 __restore_flags(flags); /* local CPU only */
408 } else
409 #endif /* SUPPORT_VLB_SYNC */
410 insl(IDE_DATA_REG, buffer, wcount);
411 } else {
412 #if SUPPORT_SLOW_DATA_PORTS
413 if (drive->slow) {
414 unsigned short *ptr = (unsigned short *) buffer;
415 while (wcount--) {
416 *ptr++ = inw_p(IDE_DATA_REG);
417 *ptr++ = inw_p(IDE_DATA_REG);
418 }
419 } else
420 #endif /* SUPPORT_SLOW_DATA_PORTS */
421 insw(IDE_DATA_REG, buffer, wcount<<1);
422 }
423 }
424
425 /*
426 * This is used for most PIO data transfers *to* the IDE interface
427 */
428 void ide_output_data (ide_drive_t *drive, void *buffer, unsigned int wcount)
429 {
430 byte io_32bit;
431
432 if(HWIF(drive)->ideproc) {
433 HWIF(drive)->ideproc(ideproc_ide_output_data,
434 drive, buffer, wcount);
435 return;
436 }
437
438 io_32bit = drive->io_32bit;
439
440 if (io_32bit) {
441 #if SUPPORT_VLB_SYNC
442 if (io_32bit & 2) {
443 unsigned long flags;
444 __save_flags(flags); /* local CPU only */
445 __cli(); /* local CPU only */
446 do_vlb_sync(IDE_NSECTOR_REG);
447 outsl(IDE_DATA_REG, buffer, wcount);
448 __restore_flags(flags); /* local CPU only */
449 } else
450 #endif /* SUPPORT_VLB_SYNC */
451 outsl(IDE_DATA_REG, buffer, wcount);
452 } else {
453 #if SUPPORT_SLOW_DATA_PORTS
454 if (drive->slow) {
455 unsigned short *ptr = (unsigned short *) buffer;
456 while (wcount--) {
457 outw_p(*ptr++, IDE_DATA_REG);
458 outw_p(*ptr++, IDE_DATA_REG);
459 }
460 } else
461 #endif /* SUPPORT_SLOW_DATA_PORTS */
462 outsw(IDE_DATA_REG, buffer, wcount<<1);
463 }
464 }
465
466 /*
467 * The following routines are mainly used by the ATAPI drivers.
468 *
469 * These routines will round up any request for an odd number of bytes,
470 * so if an odd bytecount is specified, be sure that there's at least one
471 * extra byte allocated for the buffer.
472 */
473 void atapi_input_bytes (ide_drive_t *drive, void *buffer, unsigned int bytecount)
474 {
475 if(HWIF(drive)->ideproc) {
476 HWIF(drive)->ideproc(ideproc_atapi_input_bytes,
477 drive, buffer, bytecount);
478 return;
479 }
480
481 ++bytecount;
482 #if defined(CONFIG_ATARI) || defined(CONFIG_Q40)
483 if (MACH_IS_ATARI || MACH_IS_Q40) {
484 /* Atari has a byte-swapped IDE interface */
485 insw_swapw(IDE_DATA_REG, buffer, bytecount / 2);
486 return;
487 }
488 #endif /* CONFIG_ATARI */
489 ide_input_data (drive, buffer, bytecount / 4);
490 if ((bytecount & 0x03) >= 2)
491 insw (IDE_DATA_REG, ((byte *)buffer) + (bytecount & ~0x03), 1);
492 }
493
494 void atapi_output_bytes (ide_drive_t *drive, void *buffer, unsigned int bytecount)
495 {
496 if(HWIF(drive)->ideproc) {
497 HWIF(drive)->ideproc(ideproc_atapi_output_bytes,
498 drive, buffer, bytecount);
499 return;
500 }
501
502 ++bytecount;
503 #if defined(CONFIG_ATARI) || defined(CONFIG_Q40)
504 if (MACH_IS_ATARI || MACH_IS_Q40) {
505 /* Atari has a byte-swapped IDE interface */
506 outsw_swapw(IDE_DATA_REG, buffer, bytecount / 2);
507 return;
508 }
509 #endif /* CONFIG_ATARI */
510 ide_output_data (drive, buffer, bytecount / 4);
511 if ((bytecount & 0x03) >= 2)
512 outsw (IDE_DATA_REG, ((byte *)buffer) + (bytecount & ~0x03), 1);
513 }
514
515 /*
516 * Needed for PCI irq sharing
517 */
518 static inline int drive_is_ready (ide_drive_t *drive)
519 {
520 byte stat = 0;
521 if (drive->waiting_for_dma)
522 return HWIF(drive)->dmaproc(ide_dma_test_irq, drive);
523 #if 0
524 udelay(1); /* need to guarantee 400ns since last command was issued */
525 #endif
526
527 #ifdef CONFIG_IDEPCI_SHARE_IRQ
528 /*
529 * We do a passive status test under shared PCI interrupts on
530 * cards that truly share the ATA side interrupt, but may also share
531 * an interrupt with another pci card/device. We make no assumptions
532 * about possible isa-pnp and pci-pnp issues yet.
533 */
534 if (IDE_CONTROL_REG)
535 stat = GET_ALTSTAT();
536 else
537 #endif /* CONFIG_IDEPCI_SHARE_IRQ */
538 stat = GET_STAT(); /* Note: this may clear a pending IRQ!! */
539
540 if (stat & BUSY_STAT)
541 return 0; /* drive busy: definitely not interrupting */
542 return 1; /* drive ready: *might* be interrupting */
543 }
544
545 /*
546 * This is our end_request replacement function.
547 */
548 void ide_end_request (byte uptodate, ide_hwgroup_t *hwgroup)
549 {
550 struct request *rq;
551 unsigned long flags;
552 ide_drive_t *drive = hwgroup->drive;
553
554 spin_lock_irqsave(&io_request_lock, flags);
555 rq = hwgroup->rq;
556
557 /*
558 * decide whether to reenable DMA -- 3 is a random magic for now,
559 * if we DMA timeout more than 3 times, just stay in PIO
560 */
561 if (drive->state == DMA_PIO_RETRY && drive->retry_pio <= 3) {
562 drive->state = 0;
563 hwgroup->hwif->dmaproc(ide_dma_on, drive);
564 }
565
566 if (!end_that_request_first(rq, uptodate, hwgroup->drive->name)) {
567 add_blkdev_randomness(MAJOR(rq->rq_dev));
568 blkdev_dequeue_request(rq);
569 hwgroup->rq = NULL;
570 end_that_request_last(rq);
571 }
572 spin_unlock_irqrestore(&io_request_lock, flags);
573 }
574
575 /*
576 * This should get invoked any time we exit the driver to
577 * wait for an interrupt response from a drive. handler() points
578 * at the appropriate code to handle the next interrupt, and a
579 * timer is started to prevent us from waiting forever in case
580 * something goes wrong (see the ide_timer_expiry() handler later on).
581 */
582 void ide_set_handler (ide_drive_t *drive, ide_handler_t *handler,
583 unsigned int timeout, ide_expiry_t *expiry)
584 {
585 unsigned long flags;
586 ide_hwgroup_t *hwgroup = HWGROUP(drive);
587
588 spin_lock_irqsave(&io_request_lock, flags);
589 if (hwgroup->handler != NULL) {
590 printk("%s: ide_set_handler: handler not null; old=%p, new=%p\n",
591 drive->name, hwgroup->handler, handler);
592 }
593 hwgroup->handler = handler;
594 hwgroup->expiry = expiry;
595 hwgroup->timer.expires = jiffies + timeout;
596 add_timer(&hwgroup->timer);
597 spin_unlock_irqrestore(&io_request_lock, flags);
598 }
599
600 /*
601 * current_capacity() returns the capacity (in sectors) of a drive
602 * according to its current geometry/LBA settings.
603 */
604 unsigned long current_capacity (ide_drive_t *drive)
605 {
606 if (!drive->present)
607 return 0;
608 if (drive->driver != NULL)
609 return DRIVER(drive)->capacity(drive);
610 return 0;
611 }
612
613 extern struct block_device_operations ide_fops[];
614 /*
615 * ide_geninit() is called exactly *once* for each interface.
616 */
617 void ide_geninit (ide_hwif_t *hwif)
618 {
619 unsigned int unit;
620 struct gendisk *gd = hwif->gd;
621
622 for (unit = 0; unit < MAX_DRIVES; ++unit) {
623 ide_drive_t *drive = &hwif->drives[unit];
624
625 if (!drive->present)
626 continue;
627 if (drive->media!=ide_disk && drive->media!=ide_floppy)
628 continue;
629 register_disk(gd,MKDEV(hwif->major,unit<<PARTN_BITS),
630 #ifdef CONFIG_BLK_DEV_ISAPNP
631 (drive->forced_geom && drive->noprobe) ? 1 :
632 #endif /* CONFIG_BLK_DEV_ISAPNP */
633 1<<PARTN_BITS, ide_fops,
634 current_capacity(drive));
635 }
636 }
637
638 static ide_startstop_t do_reset1 (ide_drive_t *, int); /* needed below */
639
640 /*
641 * atapi_reset_pollfunc() gets invoked to poll the interface for completion every 50ms
642 * during an atapi drive reset operation. If the drive has not yet responded,
643 * and we have not yet hit our maximum waiting time, then the timer is restarted
644 * for another 50ms.
645 */
646 static ide_startstop_t atapi_reset_pollfunc (ide_drive_t *drive)
647 {
648 ide_hwgroup_t *hwgroup = HWGROUP(drive);
649 byte stat;
650
651 SELECT_DRIVE(HWIF(drive),drive);
652 udelay (10);
653
654 if (OK_STAT(stat=GET_STAT(), 0, BUSY_STAT)) {
655 printk("%s: ATAPI reset complete\n", drive->name);
656 } else {
657 if (0 < (signed long)(hwgroup->poll_timeout - jiffies)) {
658 ide_set_handler (drive, &atapi_reset_pollfunc, HZ/20, NULL);
659 return ide_started; /* continue polling */
660 }
661 hwgroup->poll_timeout = 0; /* end of polling */
662 printk("%s: ATAPI reset timed-out, status=0x%02x\n", drive->name, stat);
663 return do_reset1 (drive, 1); /* do it the old fashioned way */
664 }
665 hwgroup->poll_timeout = 0; /* done polling */
666 return ide_stopped;
667 }
668
669 /*
670 * reset_pollfunc() gets invoked to poll the interface for completion every 50ms
671 * during an ide reset operation. If the drives have not yet responded,
672 * and we have not yet hit our maximum waiting time, then the timer is restarted
673 * for another 50ms.
674 */
675 static ide_startstop_t reset_pollfunc (ide_drive_t *drive)
676 {
677 ide_hwgroup_t *hwgroup = HWGROUP(drive);
678 ide_hwif_t *hwif = HWIF(drive);
679 byte tmp;
680
681 if (!OK_STAT(tmp=GET_STAT(), 0, BUSY_STAT)) {
682 if (0 < (signed long)(hwgroup->poll_timeout - jiffies)) {
683 ide_set_handler (drive, &reset_pollfunc, HZ/20, NULL);
684 return ide_started; /* continue polling */
685 }
686 printk("%s: reset timed-out, status=0x%02x\n", hwif->name, tmp);
687 drive->failures++;
688 } else {
689 printk("%s: reset: ", hwif->name);
690 if ((tmp = GET_ERR()) == 1) {
691 printk("success\n");
692 drive->failures = 0;
693 } else {
694 drive->failures++;
695 #if FANCY_STATUS_DUMPS
696 printk("master: ");
697 switch (tmp & 0x7f) {
698 case 1: printk("passed");
699 break;
700 case 2: printk("formatter device error");
701 break;
702 case 3: printk("sector buffer error");
703 break;
704 case 4: printk("ECC circuitry error");
705 break;
706 case 5: printk("controlling MPU error");
707 break;
708 default:printk("error (0x%02x?)", tmp);
709 }
710 if (tmp & 0x80)
711 printk("; slave: failed");
712 printk("\n");
713 #else
714 printk("failed\n");
715 #endif /* FANCY_STATUS_DUMPS */
716 }
717 }
718 hwgroup->poll_timeout = 0; /* done polling */
719 return ide_stopped;
720 }
721
722 static void check_dma_crc (ide_drive_t *drive)
723 {
724 if (drive->crc_count) {
725 (void) HWIF(drive)->dmaproc(ide_dma_off_quietly, drive);
726 if ((HWIF(drive)->speedproc) != NULL)
727 HWIF(drive)->speedproc(drive, ide_auto_reduce_xfer(drive));
728 if (drive->current_speed >= XFER_SW_DMA_0)
729 (void) HWIF(drive)->dmaproc(ide_dma_on, drive);
730 } else {
731 (void) HWIF(drive)->dmaproc(ide_dma_off, drive);
732 }
733 }
734
735 static void pre_reset (ide_drive_t *drive)
736 {
737 if (drive->driver != NULL)
738 DRIVER(drive)->pre_reset(drive);
739
740 if (!drive->keep_settings) {
741 if (drive->using_dma) {
742 check_dma_crc(drive);
743 } else {
744 drive->unmask = 0;
745 drive->io_32bit = 0;
746 }
747 return;
748 }
749 if (drive->using_dma)
750 check_dma_crc(drive);
751 }
752
753 /*
754 * do_reset1() attempts to recover a confused drive by resetting it.
755 * Unfortunately, resetting a disk drive actually resets all devices on
756 * the same interface, so it can really be thought of as resetting the
757 * interface rather than resetting the drive.
758 *
759 * ATAPI devices have their own reset mechanism which allows them to be
760 * individually reset without clobbering other devices on the same interface.
761 *
762 * Unfortunately, the IDE interface does not generate an interrupt to let
763 * us know when the reset operation has finished, so we must poll for this.
764 * Equally poor, though, is the fact that this may a very long time to complete,
765 * (up to 30 seconds worstcase). So, instead of busy-waiting here for it,
766 * we set a timer to poll at 50ms intervals.
767 */
768 static ide_startstop_t do_reset1 (ide_drive_t *drive, int do_not_try_atapi)
769 {
770 unsigned int unit;
771 unsigned long flags;
772 ide_hwif_t *hwif = HWIF(drive);
773 ide_hwgroup_t *hwgroup = HWGROUP(drive);
774
775 __save_flags(flags); /* local CPU only */
776 __cli(); /* local CPU only */
777
778 /* For an ATAPI device, first try an ATAPI SRST. */
779 if (drive->media != ide_disk && !do_not_try_atapi) {
780 pre_reset(drive);
781 SELECT_DRIVE(hwif,drive);
782 udelay (20);
783 OUT_BYTE (WIN_SRST, IDE_COMMAND_REG);
784 hwgroup->poll_timeout = jiffies + WAIT_WORSTCASE;
785 ide_set_handler (drive, &atapi_reset_pollfunc, HZ/20, NULL);
786 __restore_flags (flags); /* local CPU only */
787 return ide_started;
788 }
789
790 /*
791 * First, reset any device state data we were maintaining
792 * for any of the drives on this interface.
793 */
794 for (unit = 0; unit < MAX_DRIVES; ++unit)
795 pre_reset(&hwif->drives[unit]);
796
797 #if OK_TO_RESET_CONTROLLER
798 if (!IDE_CONTROL_REG) {
799 __restore_flags(flags);
800 return ide_stopped;
801 }
802 /*
803 * Note that we also set nIEN while resetting the device,
804 * to mask unwanted interrupts from the interface during the reset.
805 * However, due to the design of PC hardware, this will cause an
806 * immediate interrupt due to the edge transition it produces.
807 * This single interrupt gives us a "fast poll" for drives that
808 * recover from reset very quickly, saving us the first 50ms wait time.
809 */
810 OUT_BYTE(drive->ctl|6,IDE_CONTROL_REG); /* set SRST and nIEN */
811 udelay(10); /* more than enough time */
812 OUT_BYTE(drive->ctl|2,IDE_CONTROL_REG); /* clear SRST, leave nIEN */
813 udelay(10); /* more than enough time */
814 hwgroup->poll_timeout = jiffies + WAIT_WORSTCASE;
815 ide_set_handler (drive, &reset_pollfunc, HZ/20, NULL);
816
817 /*
818 * Some weird controller like resetting themselves to a strange
819 * state when the disks are reset this way. At least, the Winbond
820 * 553 documentation says that
821 */
822 if (hwif->resetproc != NULL)
823 hwif->resetproc(drive);
824
825 #endif /* OK_TO_RESET_CONTROLLER */
826
827 __restore_flags (flags); /* local CPU only */
828 return ide_started;
829 }
830
831 /*
832 * ide_do_reset() is the entry point to the drive/interface reset code.
833 */
834 ide_startstop_t ide_do_reset (ide_drive_t *drive)
835 {
836 return do_reset1 (drive, 0);
837 }
838
839 /*
840 * Clean up after success/failure of an explicit drive cmd
841 */
842 void ide_end_drive_cmd (ide_drive_t *drive, byte stat, byte err)
843 {
844 unsigned long flags;
845 struct request *rq;
846
847 spin_lock_irqsave(&io_request_lock, flags);
848 rq = HWGROUP(drive)->rq;
849 spin_unlock_irqrestore(&io_request_lock, flags);
850
851 if (rq->cmd == IDE_DRIVE_CMD) {
852 byte *args = (byte *) rq->buffer;
853 rq->errors = !OK_STAT(stat,READY_STAT,BAD_STAT);
854 if (args) {
855 args[0] = stat;
856 args[1] = err;
857 args[2] = IN_BYTE(IDE_NSECTOR_REG);
858 }
859 } else if (rq->cmd == IDE_DRIVE_TASK) {
860 byte *args = (byte *) rq->buffer;
861 rq->errors = !OK_STAT(stat,READY_STAT,BAD_STAT);
862 if (args) {
863 args[0] = stat;
864 args[1] = err;
865 args[2] = IN_BYTE(IDE_NSECTOR_REG);
866 args[3] = IN_BYTE(IDE_SECTOR_REG);
867 args[4] = IN_BYTE(IDE_LCYL_REG);
868 args[5] = IN_BYTE(IDE_HCYL_REG);
869 args[6] = IN_BYTE(IDE_SELECT_REG);
870 }
871 }
872 spin_lock_irqsave(&io_request_lock, flags);
873 blkdev_dequeue_request(rq);
874 HWGROUP(drive)->rq = NULL;
875 end_that_request_last(rq);
876 spin_unlock_irqrestore(&io_request_lock, flags);
877 }
878
879 /*
880 * Error reporting, in human readable form (luxurious, but a memory hog).
881 */
882 byte ide_dump_status (ide_drive_t *drive, const char *msg, byte stat)
883 {
884 unsigned long flags;
885 byte err = 0;
886
887 __save_flags (flags); /* local CPU only */
888 ide__sti(); /* local CPU only */
889 printk("%s: %s: status=0x%02x", drive->name, msg, stat);
890 #if FANCY_STATUS_DUMPS
891 printk(" { ");
892 if (stat & BUSY_STAT)
893 printk("Busy ");
894 else {
895 if (stat & READY_STAT) printk("DriveReady ");
896 if (stat & WRERR_STAT) printk("DeviceFault ");
897 if (stat & SEEK_STAT) printk("SeekComplete ");
898 if (stat & DRQ_STAT) printk("DataRequest ");
899 if (stat & ECC_STAT) printk("CorrectedError ");
900 if (stat & INDEX_STAT) printk("Index ");
901 if (stat & ERR_STAT) printk("Error ");
902 }
903 printk("}");
904 #endif /* FANCY_STATUS_DUMPS */
905 printk("\n");
906 if ((stat & (BUSY_STAT|ERR_STAT)) == ERR_STAT) {
907 err = GET_ERR();
908 printk("%s: %s: error=0x%02x", drive->name, msg, err);
909 #if FANCY_STATUS_DUMPS
910 if (drive->media == ide_disk) {
911 printk(" { ");
912 if (err & ABRT_ERR) printk("DriveStatusError ");
913 if (err & ICRC_ERR) printk("%s", (err & ABRT_ERR) ? "BadCRC " : "BadSector ");
914 if (err & ECC_ERR) printk("UncorrectableError ");
915 if (err & ID_ERR) printk("SectorIdNotFound ");
916 if (err & TRK0_ERR) printk("TrackZeroNotFound ");
917 if (err & MARK_ERR) printk("AddrMarkNotFound ");
918 printk("}");
919 if ((err & (BBD_ERR | ABRT_ERR)) == BBD_ERR || (err & (ECC_ERR|ID_ERR|MARK_ERR))) {
920 byte cur = IN_BYTE(IDE_SELECT_REG);
921 if (cur & 0x40) { /* using LBA? */
922 printk(", LBAsect=%ld", (unsigned long)
923 ((cur&0xf)<<24)
924 |(IN_BYTE(IDE_HCYL_REG)<<16)
925 |(IN_BYTE(IDE_LCYL_REG)<<8)
926 | IN_BYTE(IDE_SECTOR_REG));
927 } else {
928 printk(", CHS=%d/%d/%d",
929 (IN_BYTE(IDE_HCYL_REG)<<8) +
930 IN_BYTE(IDE_LCYL_REG),
931 cur & 0xf,
932 IN_BYTE(IDE_SECTOR_REG));
933 }
934 if (HWGROUP(drive) && HWGROUP(drive)->rq)
935 printk(", sector=%ld", HWGROUP(drive)->rq->sector);
936 }
937 }
938 #endif /* FANCY_STATUS_DUMPS */
939 printk("\n");
940 }
941 __restore_flags (flags); /* local CPU only */
942 return err;
943 }
944
945 /*
946 * try_to_flush_leftover_data() is invoked in response to a drive
947 * unexpectedly having its DRQ_STAT bit set. As an alternative to
948 * resetting the drive, this routine tries to clear the condition
949 * by read a sector's worth of data from the drive. Of course,
950 * this may not help if the drive is *waiting* for data from *us*.
951 */
952 static void try_to_flush_leftover_data (ide_drive_t *drive)
953 {
954 int i = (drive->mult_count ? drive->mult_count : 1) * SECTOR_WORDS;
955
956 if (drive->media != ide_disk)
957 return;
958 while (i > 0) {
959 u32 buffer[16];
960 unsigned int wcount = (i > 16) ? 16 : i;
961 i -= wcount;
962 ide_input_data (drive, buffer, wcount);
963 }
964 }
965
966 /*
967 * ide_error() takes action based on the error returned by the drive.
968 */
969 ide_startstop_t ide_error (ide_drive_t *drive, const char *msg, byte stat)
970 {
971 struct request *rq;
972 byte err;
973
974 err = ide_dump_status(drive, msg, stat);
975 if (drive == NULL || (rq = HWGROUP(drive)->rq) == NULL)
976 return ide_stopped;
977 /* retry only "normal" I/O: */
978 if (rq->cmd == IDE_DRIVE_CMD || rq->cmd == IDE_DRIVE_TASK) {
979 rq->errors = 1;
980 ide_end_drive_cmd(drive, stat, err);
981 return ide_stopped;
982 }
983 if (stat & BUSY_STAT || ((stat & WRERR_STAT) && !drive->nowerr)) { /* other bits are useless when BUSY */
984 rq->errors |= ERROR_RESET;
985 } else {
986 if (drive->media == ide_disk && (stat & ERR_STAT)) {
987 /* err has different meaning on cdrom and tape */
988 if (err == ABRT_ERR) {
989 if (drive->select.b.lba && IN_BYTE(IDE_COMMAND_REG) == WIN_SPECIFY)
990 return ide_stopped; /* some newer drives don't support WIN_SPECIFY */
991 } else if ((err & (ABRT_ERR | ICRC_ERR)) == (ABRT_ERR | ICRC_ERR)) {
992 drive->crc_count++; /* UDMA crc error -- just retry the operation */
993 } else if (err & (BBD_ERR | ECC_ERR)) /* retries won't help these */
994 rq->errors = ERROR_MAX;
995 else if (err & TRK0_ERR) /* help it find track zero */
996 rq->errors |= ERROR_RECAL;
997 }
998 if ((stat & DRQ_STAT) && rq->cmd != WRITE)
999 try_to_flush_leftover_data(drive);
1000 }
1001 if (GET_STAT() & (BUSY_STAT|DRQ_STAT))
1002 OUT_BYTE(WIN_IDLEIMMEDIATE,IDE_COMMAND_REG); /* force an abort */
1003
1004 if (rq->errors >= ERROR_MAX) {
1005 if (drive->driver != NULL)
1006 DRIVER(drive)->end_request(0, HWGROUP(drive));
1007 else
1008 ide_end_request(0, HWGROUP(drive));
1009 } else {
1010 if ((rq->errors & ERROR_RESET) == ERROR_RESET) {
1011 ++rq->errors;
1012 return ide_do_reset(drive);
1013 }
1014 if ((rq->errors & ERROR_RECAL) == ERROR_RECAL)
1015 drive->special.b.recalibrate = 1;
1016 ++rq->errors;
1017 }
1018 return ide_stopped;
1019 }
1020
1021 /*
1022 * Issue a simple drive command
1023 * The drive must be selected beforehand.
1024 */
1025 void ide_cmd (ide_drive_t *drive, byte cmd, byte nsect, ide_handler_t *handler)
1026 {
1027 ide_set_handler (drive, handler, WAIT_CMD, NULL);
1028 if (IDE_CONTROL_REG)
1029 OUT_BYTE(drive->ctl,IDE_CONTROL_REG); /* clear nIEN */
1030 SELECT_MASK(HWIF(drive),drive,0);
1031 OUT_BYTE(nsect,IDE_NSECTOR_REG);
1032 OUT_BYTE(cmd,IDE_COMMAND_REG);
1033 }
1034
1035 /*
1036 * drive_cmd_intr() is invoked on completion of a special DRIVE_CMD.
1037 */
1038 static ide_startstop_t drive_cmd_intr (ide_drive_t *drive)
1039 {
1040 struct request *rq = HWGROUP(drive)->rq;
1041 byte *args = (byte *) rq->buffer;
1042 byte stat = GET_STAT();
1043 int retries = 10;
1044
1045 ide__sti(); /* local CPU only */
1046 if ((stat & DRQ_STAT) && args && args[3]) {
1047 byte io_32bit = drive->io_32bit;
1048 drive->io_32bit = 0;
1049 ide_input_data(drive, &args[4], args[3] * SECTOR_WORDS);
1050 drive->io_32bit = io_32bit;
1051 while (((stat = GET_STAT()) & BUSY_STAT) && retries--)
1052 udelay(100);
1053 }
1054
1055 if (!OK_STAT(stat, READY_STAT, BAD_STAT))
1056 return ide_error(drive, "drive_cmd", stat); /* calls ide_end_drive_cmd */
1057 ide_end_drive_cmd (drive, stat, GET_ERR());
1058 return ide_stopped;
1059 }
1060
1061 /*
1062 * do_special() is used to issue WIN_SPECIFY, WIN_RESTORE, and WIN_SETMULT
1063 * commands to a drive. It used to do much more, but has been scaled back.
1064 */
1065 static ide_startstop_t do_special (ide_drive_t *drive)
1066 {
1067 special_t *s = &drive->special;
1068
1069 #ifdef DEBUG
1070 printk("%s: do_special: 0x%02x\n", drive->name, s->all);
1071 #endif
1072 if (s->b.set_tune) {
1073 ide_tuneproc_t *tuneproc = HWIF(drive)->tuneproc;
1074 s->b.set_tune = 0;
1075 if (tuneproc != NULL)
1076 tuneproc(drive, drive->tune_req);
1077 } else if (drive->driver != NULL) {
1078 return DRIVER(drive)->special(drive);
1079 } else if (s->all) {
1080 printk("%s: bad special flag: 0x%02x\n", drive->name, s->all);
1081 s->all = 0;
1082 }
1083 return ide_stopped;
1084 }
1085
1086 /*
1087 * This routine busy-waits for the drive status to be not "busy".
1088 * It then checks the status for all of the "good" bits and none
1089 * of the "bad" bits, and if all is okay it returns 0. All other
1090 * cases return 1 after invoking ide_error() -- caller should just return.
1091 *
1092 * This routine should get fixed to not hog the cpu during extra long waits..
1093 * That could be done by busy-waiting for the first jiffy or two, and then
1094 * setting a timer to wake up at half second intervals thereafter,
1095 * until timeout is achieved, before timing out.
1096 */
1097 int ide_wait_stat (ide_startstop_t *startstop, ide_drive_t *drive, byte good, byte bad, unsigned long timeout) {
1098 byte stat;
1099 int i;
1100 unsigned long flags;
1101
1102 /* bail early if we've exceeded max_failures */
1103 if (drive->max_failures && (drive->failures > drive->max_failures)) {
1104 *startstop = ide_stopped;
1105 return 1;
1106 }
1107
1108 udelay(1); /* spec allows drive 400ns to assert "BUSY" */
1109 if ((stat = GET_STAT()) & BUSY_STAT) {
1110 __save_flags(flags); /* local CPU only */
1111 ide__sti(); /* local CPU only */
1112 timeout += jiffies;
1113 while ((stat = GET_STAT()) & BUSY_STAT) {
1114 if (0 < (signed long)(jiffies - timeout)) {
1115 __restore_flags(flags); /* local CPU only */
1116 *startstop = ide_error(drive, "status timeout", stat);
1117 return 1;
1118 }
1119 }
1120 __restore_flags(flags); /* local CPU only */
1121 }
1122 /*
1123 * Allow status to settle, then read it again.
1124 * A few rare drives vastly violate the 400ns spec here,
1125 * so we'll wait up to 10usec for a "good" status
1126 * rather than expensively fail things immediately.
1127 * This fix courtesy of Matthew Faupel & Niccolo Rigacci.
1128 */
1129 for (i = 0; i < 10; i++) {
1130 udelay(1);
1131 if (OK_STAT((stat = GET_STAT()), good, bad))
1132 return 0;
1133 }
1134 *startstop = ide_error(drive, "status error", stat);
1135 return 1;
1136 }
1137
1138 /*
1139 * execute_drive_cmd() issues a special drive command,
1140 * usually initiated by ioctl() from the external hdparm program.
1141 */
1142 static ide_startstop_t execute_drive_cmd (ide_drive_t *drive, struct request *rq)
1143 {
1144 byte *args = rq->buffer;
1145 if (args && rq->cmd == IDE_DRIVE_TASK) {
1146 byte sel;
1147 #ifdef DEBUG
1148 printk("%s: DRIVE_TASK_CMD data=x%02x cmd=0x%02x fr=0x%02x ns=0x%02x sc=0x%02x lcyl=0x%02x hcyl=0x%02x sel=0x%02x\n",
1149 drive->name, args[0], args[1], args[2], args[3], args[4], args[5], args[6]);
1150 #endif
1151 OUT_BYTE(args[1], IDE_FEATURE_REG);
1152 OUT_BYTE(args[3], IDE_SECTOR_REG);
1153 OUT_BYTE(args[4], IDE_LCYL_REG);
1154 OUT_BYTE(args[5], IDE_HCYL_REG);
1155 sel = (args[6] & ~0x10);
1156 if (drive->select.b.unit)
1157 sel |= 0x10;
1158 OUT_BYTE(sel, IDE_SELECT_REG);
1159 ide_cmd(drive, args[0], args[2], &drive_cmd_intr);
1160 return ide_started;
1161 } else if (args) {
1162 #ifdef DEBUG
1163 printk("%s: DRIVE_CMD cmd=0x%02x sc=0x%02x fr=0x%02x xx=0x%02x\n",
1164 drive->name, args[0], args[1], args[2], args[3]);
1165 #endif
1166 if (args[0] == WIN_SMART) {
1167 OUT_BYTE(0x4f, IDE_LCYL_REG);
1168 OUT_BYTE(0xc2, IDE_HCYL_REG);
1169 OUT_BYTE(args[2],IDE_FEATURE_REG);
1170 OUT_BYTE(args[1],IDE_SECTOR_REG);
1171 ide_cmd(drive, args[0], args[3], &drive_cmd_intr);
1172 return ide_started;
1173 }
1174 OUT_BYTE(args[2],IDE_FEATURE_REG);
1175 ide_cmd(drive, args[0], args[1], &drive_cmd_intr);
1176 return ide_started;
1177 } else {
1178 /*
1179 * NULL is actually a valid way of waiting for
1180 * all current requests to be flushed from the queue.
1181 */
1182 #ifdef DEBUG
1183 printk("%s: DRIVE_CMD (null)\n", drive->name);
1184 #endif
1185 ide_end_drive_cmd(drive, GET_STAT(), GET_ERR());
1186 return ide_stopped;
1187 }
1188 }
1189
1190 /*
1191 * start_request() initiates handling of a new I/O request
1192 */
1193 static ide_startstop_t start_request (ide_drive_t *drive)
1194 {
1195 ide_startstop_t startstop;
1196 unsigned long block, blockend;
1197 struct request *rq = blkdev_entry_next_request(&drive->queue.queue_head);
1198 unsigned int minor = MINOR(rq->rq_dev), unit = minor >> PARTN_BITS;
1199 ide_hwif_t *hwif = HWIF(drive);
1200
1201 #ifdef DEBUG
1202 printk("%s: start_request: current=0x%08lx\n", hwif->name, (unsigned long) rq);
1203 #endif
1204 /* bail early if we've exceeded max_failures */
1205 if (drive->max_failures && (drive->failures > drive->max_failures)) {
1206 goto kill_rq;
1207 }
1208
1209 if (unit >= MAX_DRIVES) {
1210 printk("%s: bad device number: %s\n", hwif->name, kdevname(rq->rq_dev));
1211 goto kill_rq;
1212 }
1213 #ifdef DEBUG
1214 if (rq->bh && !buffer_locked(rq->bh)) {
1215 printk("%s: block not locked\n", drive->name);
1216 goto kill_rq;
1217 }
1218 #endif
1219 block = rq->sector;
1220 blockend = block + rq->nr_sectors;
1221
1222 if ((rq->cmd == READ || rq->cmd == WRITE) &&
1223 (drive->media == ide_disk || drive->media == ide_floppy)) {
1224 if ((blockend < block) || (blockend > drive->part[minor&PARTN_MASK].nr_sects)) {
1225 printk("%s%c: bad access: block=%ld, count=%ld\n", drive->name,
1226 (minor&PARTN_MASK)?'0'+(minor&PARTN_MASK):' ', block, rq->nr_sectors);
1227 goto kill_rq;
1228 }
1229 block += drive->part[minor&PARTN_MASK].start_sect + drive->sect0;
1230 }
1231 /* Yecch - this will shift the entire interval,
1232 possibly killing some innocent following sector */
1233 if (block == 0 && drive->remap_0_to_1 == 1)
1234 block = 1; /* redirect MBR access to EZ-Drive partn table */
1235
1236 #if (DISK_RECOVERY_TIME > 0)
1237 while ((read_timer() - hwif->last_time) < DISK_RECOVERY_TIME);
1238 #endif
1239
1240 SELECT_DRIVE(hwif, drive);
1241 if (ide_wait_stat(&startstop, drive, drive->ready_stat, BUSY_STAT|DRQ_STAT, WAIT_READY)) {
1242 printk("%s: drive not ready for command\n", drive->name);
1243 return startstop;
1244 }
1245 if (!drive->special.all) {
1246 if (rq->cmd == IDE_DRIVE_CMD || rq->cmd == IDE_DRIVE_TASK) {
1247 return execute_drive_cmd(drive, rq);
1248 }
1249 if (drive->driver != NULL) {
1250 return (DRIVER(drive)->do_request(drive, rq, block));
1251 }
1252 printk("%s: media type %d not supported\n", drive->name, drive->media);
1253 goto kill_rq;
1254 }
1255 return do_special(drive);
1256 kill_rq:
1257 if (drive->driver != NULL)
1258 DRIVER(drive)->end_request(0, HWGROUP(drive));
1259 else
1260 ide_end_request(0, HWGROUP(drive));
1261 return ide_stopped;
1262 }
1263
1264 ide_startstop_t restart_request (ide_drive_t *drive)
1265 {
1266 ide_hwgroup_t *hwgroup = HWGROUP(drive);
1267 unsigned long flags;
1268
1269 spin_lock_irqsave(&io_request_lock, flags);
1270 hwgroup->handler = NULL;
1271 del_timer(&hwgroup->timer);
1272 spin_unlock_irqrestore(&io_request_lock, flags);
1273
1274 return start_request(drive);
1275 }
1276
1277 /*
1278 * ide_stall_queue() can be used by a drive to give excess bandwidth back
1279 * to the hwgroup by sleeping for timeout jiffies.
1280 */
1281 void ide_stall_queue (ide_drive_t *drive, unsigned long timeout)
1282 {
1283 if (timeout > WAIT_WORSTCASE)
1284 timeout = WAIT_WORSTCASE;
1285 drive->sleep = timeout + jiffies;
1286 }
1287
1288 #define WAKEUP(drive) ((drive)->service_start + 2 * (drive)->service_time)
1289
1290 /*
1291 * choose_drive() selects the next drive which will be serviced.
1292 */
1293 static inline ide_drive_t *choose_drive (ide_hwgroup_t *hwgroup)
1294 {
1295 ide_drive_t *drive, *best;
1296
1297 repeat:
1298 best = NULL;
1299 drive = hwgroup->drive;
1300 do {
1301 if (!list_empty(&drive->queue.queue_head) && (!drive->sleep || 0 <= (signed long)(jiffies - drive->sleep))) {
1302 if (!best
1303 || (drive->sleep && (!best->sleep || 0 < (signed long)(best->sleep - drive->sleep)))
1304 || (!best->sleep && 0 < (signed long)(WAKEUP(best) - WAKEUP(drive))))
1305 {
1306 if( !drive->queue.plugged )
1307 best = drive;
1308 }
1309 }
1310 } while ((drive = drive->next) != hwgroup->drive);
1311 if (best && best->nice1 && !best->sleep && best != hwgroup->drive && best->service_time > WAIT_MIN_SLEEP) {
1312 long t = (signed long)(WAKEUP(best) - jiffies);
1313 if (t >= WAIT_MIN_SLEEP) {
1314 /*
1315 * We *may* have some time to spare, but first let's see if
1316 * someone can potentially benefit from our nice mood today..
1317 */
1318 drive = best->next;
1319 do {
1320 if (!drive->sleep
1321 && 0 < (signed long)(WAKEUP(drive) - (jiffies - best->service_time))
1322 && 0 < (signed long)((jiffies + t) - WAKEUP(drive)))
1323 {
1324 ide_stall_queue(best, IDE_MIN(t, 10 * WAIT_MIN_SLEEP));
1325 goto repeat;
1326 }
1327 } while ((drive = drive->next) != best);
1328 }
1329 }
1330 return best;
1331 }
1332
1333 /*
1334 * Issue a new request to a drive from hwgroup
1335 * Caller must have already done spin_lock_irqsave(&io_request_lock, ..);
1336 *
1337 * A hwgroup is a serialized group of IDE interfaces. Usually there is
1338 * exactly one hwif (interface) per hwgroup, but buggy controllers (eg. CMD640)
1339 * may have both interfaces in a single hwgroup to "serialize" access.
1340 * Or possibly multiple ISA interfaces can share a common IRQ by being grouped
1341 * together into one hwgroup for serialized access.
1342 *
1343 * Note also that several hwgroups can end up sharing a single IRQ,
1344 * possibly along with many other devices. This is especially common in
1345 * PCI-based systems with off-board IDE controller cards.
1346 *
1347 * The IDE driver uses the single global io_request_lock spinlock to protect
1348 * access to the request queues, and to protect the hwgroup->busy flag.
1349 *
1350 * The first thread into the driver for a particular hwgroup sets the
1351 * hwgroup->busy flag to indicate that this hwgroup is now active,
1352 * and then initiates processing of the top request from the request queue.
1353 *
1354 * Other threads attempting entry notice the busy setting, and will simply
1355 * queue their new requests and exit immediately. Note that hwgroup->busy
1356 * remains set even when the driver is merely awaiting the next interrupt.
1357 * Thus, the meaning is "this hwgroup is busy processing a request".
1358 *
1359 * When processing of a request completes, the completing thread or IRQ-handler
1360 * will start the next request from the queue. If no more work remains,
1361 * the driver will clear the hwgroup->busy flag and exit.
1362 *
1363 * The io_request_lock (spinlock) is used to protect all access to the
1364 * hwgroup->busy flag, but is otherwise not needed for most processing in
1365 * the driver. This makes the driver much more friendlier to shared IRQs
1366 * than previous designs, while remaining 100% (?) SMP safe and capable.
1367 */
1368 static void ide_do_request(ide_hwgroup_t *hwgroup, int masked_irq)
1369 {
1370 ide_drive_t *drive;
1371 ide_hwif_t *hwif;
1372 ide_startstop_t startstop;
1373
1374 ide_get_lock(&ide_lock, ide_intr, hwgroup); /* for atari only: POSSIBLY BROKEN HERE(?) */
1375
1376 __cli(); /* necessary paranoia: ensure IRQs are masked on local CPU */
1377
1378 while (!hwgroup->busy) {
1379 hwgroup->busy = 1;
1380 drive = choose_drive(hwgroup);
1381 if (drive == NULL) {
1382 unsigned long sleep = 0;
1383 hwgroup->rq = NULL;
1384 drive = hwgroup->drive;
1385 do {
1386 if (drive->sleep && (!sleep || 0 < (signed long)(sleep - drive->sleep)))
1387 sleep = drive->sleep;
1388 } while ((drive = drive->next) != hwgroup->drive);
1389 if (sleep) {
1390 /*
1391 * Take a short snooze, and then wake up this hwgroup again.
1392 * This gives other hwgroups on the same a chance to
1393 * play fairly with us, just in case there are big differences
1394 * in relative throughputs.. don't want to hog the cpu too much.
1395 */
1396 if (0 < (signed long)(jiffies + WAIT_MIN_SLEEP - sleep))
1397 sleep = jiffies + WAIT_MIN_SLEEP;
1398 #if 1
1399 if (timer_pending(&hwgroup->timer))
1400 printk("ide_set_handler: timer already active\n");
1401 #endif
1402 hwgroup->sleeping = 1; /* so that ide_timer_expiry knows what to do */
1403 mod_timer(&hwgroup->timer, sleep);
1404 /* we purposely leave hwgroup->busy==1 while sleeping */
1405 } else {
1406 /* Ugly, but how can we sleep for the lock otherwise? perhaps from tq_disk? */
1407 ide_release_lock(&ide_lock); /* for atari only */
1408 hwgroup->busy = 0;
1409 }
1410 return; /* no more work for this hwgroup (for now) */
1411 }
1412 hwif = HWIF(drive);
1413 if (hwgroup->hwif->sharing_irq && hwif != hwgroup->hwif && hwif->io_ports[IDE_CONTROL_OFFSET]) {
1414 /* set nIEN for previous hwif */
1415 SELECT_INTERRUPT(hwif, drive);
1416 }
1417 hwgroup->hwif = hwif;
1418 hwgroup->drive = drive;
1419 drive->sleep = 0;
1420 drive->service_start = jiffies;
1421
1422 if ( drive->queue.plugged ) /* paranoia */
1423 printk("%s: Huh? nuking plugged queue\n", drive->name);
1424 hwgroup->rq = blkdev_entry_next_request(&drive->queue.queue_head);
1425 /*
1426 * Some systems have trouble with IDE IRQs arriving while
1427 * the driver is still setting things up. So, here we disable
1428 * the IRQ used by this interface while the request is being started.
1429 * This may look bad at first, but pretty much the same thing
1430 * happens anyway when any interrupt comes in, IDE or otherwise
1431 * -- the kernel masks the IRQ while it is being handled.
1432 */
1433 if (masked_irq && hwif->irq != masked_irq)
1434 disable_irq_nosync(hwif->irq);
1435 spin_unlock(&io_request_lock);
1436 ide__sti(); /* allow other IRQs while we start this request */
1437 startstop = start_request(drive);
1438 spin_lock_irq(&io_request_lock);
1439 if (masked_irq && hwif->irq != masked_irq)
1440 enable_irq(hwif->irq);
1441 if (startstop == ide_stopped)
1442 hwgroup->busy = 0;
1443 }
1444 }
1445
1446 /*
1447 * ide_get_queue() returns the queue which corresponds to a given device.
1448 */
1449 request_queue_t *ide_get_queue (kdev_t dev)
1450 {
1451 ide_hwif_t *hwif = (ide_hwif_t *)blk_dev[MAJOR(dev)].data;
1452
1453 return &hwif->drives[DEVICE_NR(dev) & 1].queue;
1454 }
1455
1456 /*
1457 * Passes the stuff to ide_do_request
1458 */
1459 void do_ide_request(request_queue_t *q)
1460 {
1461 ide_do_request(q->queuedata, 0);
1462 }
1463
1464 /*
1465 * un-busy the hwgroup etc, and clear any pending DMA status. we want to
1466 * retry the current request in pio mode instead of risking tossing it
1467 * all away
1468 */
1469 void ide_dma_timeout_retry(ide_drive_t *drive)
1470 {
1471 ide_hwif_t *hwif = HWIF(drive);
1472 struct request *rq;
1473
1474 /*
1475 * end current dma transaction
1476 */
1477 (void) hwif->dmaproc(ide_dma_end, drive);
1478
1479 /*
1480 * complain a little, later we might remove some of this verbosity
1481 */
1482 printk("%s: timeout waiting for DMA\n", drive->name);
1483 (void) hwif->dmaproc(ide_dma_timeout, drive);
1484
1485 /*
1486 * disable dma for now, but remember that we did so because of
1487 * a timeout -- we'll reenable after we finish this next request
1488 * (or rather the first chunk of it) in pio.
1489 */
1490 drive->retry_pio++;
1491 drive->state = DMA_PIO_RETRY;
1492 (void) hwif->dmaproc(ide_dma_off_quietly, drive);
1493
1494 /*
1495 * un-busy drive etc (hwgroup->busy is cleared on return) and
1496 * make sure request is sane
1497 */
1498 rq = HWGROUP(drive)->rq;
1499 HWGROUP(drive)->rq = NULL;
1500
1501 rq->errors = 0;
1502 rq->sector = rq->bh->b_rsector;
1503 rq->current_nr_sectors = rq->bh->b_size >> 9;
1504 rq->buffer = rq->bh->b_data;
1505 }
1506
1507 /*
1508 * ide_timer_expiry() is our timeout function for all drive operations.
1509 * But note that it can also be invoked as a result of a "sleep" operation
1510 * triggered by the mod_timer() call in ide_do_request.
1511 */
1512 void ide_timer_expiry (unsigned long data)
1513 {
1514 ide_hwgroup_t *hwgroup = (ide_hwgroup_t *) data;
1515 ide_handler_t *handler;
1516 ide_expiry_t *expiry;
1517 unsigned long flags;
1518 unsigned long wait;
1519
1520 spin_lock_irqsave(&io_request_lock, flags);
1521 del_timer(&hwgroup->timer);
1522
1523 if ((handler = hwgroup->handler) == NULL) {
1524 /*
1525 * Either a marginal timeout occurred
1526 * (got the interrupt just as timer expired),
1527 * or we were "sleeping" to give other devices a chance.
1528 * Either way, we don't really want to complain about anything.
1529 */
1530 if (hwgroup->sleeping) {
1531 hwgroup->sleeping = 0;
1532 hwgroup->busy = 0;
1533 }
1534 } else {
1535 ide_drive_t *drive = hwgroup->drive;
1536 if (!drive) {
1537 printk("ide_timer_expiry: hwgroup->drive was NULL\n");
1538 hwgroup->handler = NULL;
1539 } else {
1540 ide_hwif_t *hwif;
1541 ide_startstop_t startstop;
1542 if (!hwgroup->busy) {
1543 hwgroup->busy = 1; /* paranoia */
1544 printk("%s: ide_timer_expiry: hwgroup->busy was 0 ??\n", drive->name);
1545 }
1546 if ((expiry = hwgroup->expiry) != NULL) {
1547 /* continue */
1548 if ((wait = expiry(drive)) != 0) {
1549 /* reset timer */
1550 hwgroup->timer.expires = jiffies + wait;
1551 add_timer(&hwgroup->timer);
1552 spin_unlock_irqrestore(&io_request_lock, flags);
1553 return;
1554 }
1555 }
1556 hwgroup->handler = NULL;
1557 /*
1558 * We need to simulate a real interrupt when invoking
1559 * the handler() function, which means we need to globally
1560 * mask the specific IRQ:
1561 */
1562 spin_unlock(&io_request_lock);
1563 hwif = HWIF(drive);
1564 #if DISABLE_IRQ_NOSYNC
1565 disable_irq_nosync(hwif->irq);
1566 #else
1567 disable_irq(hwif->irq); /* disable_irq_nosync ?? */
1568 #endif /* DISABLE_IRQ_NOSYNC */
1569 __cli(); /* local CPU only, as if we were handling an interrupt */
1570 if (hwgroup->poll_timeout != 0) {
1571 startstop = handler(drive);
1572 } else if (drive_is_ready(drive)) {
1573 if (drive->waiting_for_dma)
1574 (void) hwgroup->hwif->dmaproc(ide_dma_lostirq, drive);
1575 (void)ide_ack_intr(hwif);
1576 printk("%s: lost interrupt\n", drive->name);
1577 startstop = handler(drive);
1578 } else {
1579 if (drive->waiting_for_dma) {
1580 startstop = ide_stopped;
1581 ide_dma_timeout_retry(drive);
1582 } else
1583 startstop = ide_error(drive, "irq timeout", GET_STAT());
1584 }
1585 set_recovery_timer(hwif);
1586 drive->service_time = jiffies - drive->service_start;
1587 enable_irq(hwif->irq);
1588 spin_lock_irq(&io_request_lock);
1589 if (startstop == ide_stopped)
1590 hwgroup->busy = 0;
1591 }
1592 }
1593 ide_do_request(hwgroup, 0);
1594 spin_unlock_irqrestore(&io_request_lock, flags);
1595 }
1596
1597 /*
1598 * There's nothing really useful we can do with an unexpected interrupt,
1599 * other than reading the status register (to clear it), and logging it.
1600 * There should be no way that an irq can happen before we're ready for it,
1601 * so we needn't worry much about losing an "important" interrupt here.
1602 *
1603 * On laptops (and "green" PCs), an unexpected interrupt occurs whenever the
1604 * drive enters "idle", "standby", or "sleep" mode, so if the status looks
1605 * "good", we just ignore the interrupt completely.
1606 *
1607 * This routine assumes __cli() is in effect when called.
1608 *
1609 * If an unexpected interrupt happens on irq15 while we are handling irq14
1610 * and if the two interfaces are "serialized" (CMD640), then it looks like
1611 * we could screw up by interfering with a new request being set up for irq15.
1612 *
1613 * In reality, this is a non-issue. The new command is not sent unless the
1614 * drive is ready to accept one, in which case we know the drive is not
1615 * trying to interrupt us. And ide_set_handler() is always invoked before
1616 * completing the issuance of any new drive command, so we will not be
1617 * accidently invoked as a result of any valid command completion interrupt.
1618 *
1619 */
1620 static void unexpected_intr (int irq, ide_hwgroup_t *hwgroup)
1621 {
1622 byte stat;
1623 ide_hwif_t *hwif = hwgroup->hwif;
1624
1625 /*
1626 * handle the unexpected interrupt
1627 */
1628 do {
1629 if (hwif->irq == irq) {
1630 stat = IN_BYTE(hwif->io_ports[IDE_STATUS_OFFSET]);
1631 if (!OK_STAT(stat, READY_STAT, BAD_STAT)) {
1632 /* Try to not flood the console with msgs */
1633 static unsigned long last_msgtime, count;
1634 ++count;
1635 if (0 < (signed long)(jiffies - (last_msgtime + HZ))) {
1636 last_msgtime = jiffies;
1637 printk("%s%s: unexpected interrupt, status=0x%02x, count=%ld\n",
1638 hwif->name, (hwif->next == hwgroup->hwif) ? "" : "(?)", stat, count);
1639 }
1640 }
1641 }
1642 } while ((hwif = hwif->next) != hwgroup->hwif);
1643 }
1644
1645 /*
1646 * entry point for all interrupts, caller does __cli() for us
1647 */
1648 void ide_intr (int irq, void *dev_id, struct pt_regs *regs)
1649 {
1650 unsigned long flags;
1651 ide_hwgroup_t *hwgroup = (ide_hwgroup_t *)dev_id;
1652 ide_hwif_t *hwif;
1653 ide_drive_t *drive;
1654 ide_handler_t *handler;
1655 ide_startstop_t startstop;
1656
1657 spin_lock_irqsave(&io_request_lock, flags);
1658 hwif = hwgroup->hwif;
1659
1660 if (!ide_ack_intr(hwif)) {
1661 spin_unlock_irqrestore(&io_request_lock, flags);
1662 return;
1663 }
1664
1665 if ((handler = hwgroup->handler) == NULL || hwgroup->poll_timeout != 0) {
1666 /*
1667 * Not expecting an interrupt from this drive.
1668 * That means this could be:
1669 * (1) an interrupt from another PCI device
1670 * sharing the same PCI INT# as us.
1671 * or (2) a drive just entered sleep or standby mode,
1672 * and is interrupting to let us know.
1673 * or (3) a spurious interrupt of unknown origin.
1674 *
1675 * For PCI, we cannot tell the difference,
1676 * so in that case we just ignore it and hope it goes away.
1677 */
1678 #ifdef CONFIG_BLK_DEV_IDEPCI
1679 if (IDE_PCI_DEVID_EQ(hwif->pci_devid, IDE_PCI_DEVID_NULL))
1680 #endif /* CONFIG_BLK_DEV_IDEPCI */
1681 {
1682 /*
1683 * Probably not a shared PCI interrupt,
1684 * so we can safely try to do something about it:
1685 */
1686 unexpected_intr(irq, hwgroup);
1687 #ifdef CONFIG_BLK_DEV_IDEPCI
1688 } else {
1689 /*
1690 * Whack the status register, just in case we have a leftover pending IRQ.
1691 */
1692 (void) IN_BYTE(hwif->io_ports[IDE_STATUS_OFFSET]);
1693 #endif /* CONFIG_BLK_DEV_IDEPCI */
1694 }
1695 spin_unlock_irqrestore(&io_request_lock, flags);
1696 return;
1697 }
1698 drive = hwgroup->drive;
1699 if (!drive) {
1700 /*
1701 * This should NEVER happen, and there isn't much we could do about it here.
1702 */
1703 spin_unlock_irqrestore(&io_request_lock, flags);
1704 return;
1705 }
1706 if (!drive_is_ready(drive)) {
1707 /*
1708 * This happens regularly when we share a PCI IRQ with another device.
1709 * Unfortunately, it can also happen with some buggy drives that trigger
1710 * the IRQ before their status register is up to date. Hopefully we have
1711 * enough advance overhead that the latter isn't a problem.
1712 */
1713 spin_unlock_irqrestore(&io_request_lock, flags);
1714 return;
1715 }
1716 if (!hwgroup->busy) {
1717 hwgroup->busy = 1; /* paranoia */
1718 printk("%s: ide_intr: hwgroup->busy was 0 ??\n", drive->name);
1719 }
1720 hwgroup->handler = NULL;
1721 del_timer(&hwgroup->timer);
1722 spin_unlock(&io_request_lock);
1723
1724 if (drive->unmask)
1725 ide__sti(); /* local CPU only */
1726 startstop = handler(drive); /* service this interrupt, may set handler for next interrupt */
1727 spin_lock_irq(&io_request_lock);
1728
1729 /*
1730 * Note that handler() may have set things up for another
1731 * interrupt to occur soon, but it cannot happen until
1732 * we exit from this routine, because it will be the
1733 * same irq as is currently being serviced here, and Linux
1734 * won't allow another of the same (on any CPU) until we return.
1735 */
1736 set_recovery_timer(HWIF(drive));
1737 drive->service_time = jiffies - drive->service_start;
1738 if (startstop == ide_stopped) {
1739 if (hwgroup->handler == NULL) { /* paranoia */
1740 hwgroup->busy = 0;
1741 ide_do_request(hwgroup, hwif->irq);
1742 } else {
1743 printk("%s: ide_intr: huh? expected NULL handler on exit\n", drive->name);
1744 }
1745 }
1746 spin_unlock_irqrestore(&io_request_lock, flags);
1747 }
1748
1749 /*
1750 * get_info_ptr() returns the (ide_drive_t *) for a given device number.
1751 * It returns NULL if the given device number does not match any present drives.
1752 */
1753 ide_drive_t *get_info_ptr (kdev_t i_rdev)
1754 {
1755 int major = MAJOR(i_rdev);
1756 #if 0
1757 int minor = MINOR(i_rdev) & PARTN_MASK;
1758 #endif
1759 unsigned int h;
1760
1761 for (h = 0; h < MAX_HWIFS; ++h) {
1762 ide_hwif_t *hwif = &ide_hwifs[h];
1763 if (hwif->present && major == hwif->major) {
1764 unsigned unit = DEVICE_NR(i_rdev);
1765 if (unit < MAX_DRIVES) {
1766 ide_drive_t *drive = &hwif->drives[unit];
1767 #if 0
1768 if ((drive->present) && (drive->part[minor].nr_sects))
1769 #else
1770 if (drive->present)
1771 #endif
1772 return drive;
1773 }
1774 break;
1775 }
1776 }
1777 return NULL;
1778 }
1779
1780 /*
1781 * This function is intended to be used prior to invoking ide_do_drive_cmd().
1782 */
1783 void ide_init_drive_cmd (struct request *rq)
1784 {
1785 memset(rq, 0, sizeof(*rq));
1786 rq->cmd = IDE_DRIVE_CMD;
1787 }
1788
1789 /*
1790 * This function issues a special IDE device request
1791 * onto the request queue.
1792 *
1793 * If action is ide_wait, then the rq is queued at the end of the
1794 * request queue, and the function sleeps until it has been processed.
1795 * This is for use when invoked from an ioctl handler.
1796 *
1797 * If action is ide_preempt, then the rq is queued at the head of
1798 * the request queue, displacing the currently-being-processed
1799 * request and this function returns immediately without waiting
1800 * for the new rq to be completed. This is VERY DANGEROUS, and is
1801 * intended for careful use by the ATAPI tape/cdrom driver code.
1802 *
1803 * If action is ide_next, then the rq is queued immediately after
1804 * the currently-being-processed-request (if any), and the function
1805 * returns without waiting for the new rq to be completed. As above,
1806 * This is VERY DANGEROUS, and is intended for careful use by the
1807 * ATAPI tape/cdrom driver code.
1808 *
1809 * If action is ide_end, then the rq is queued at the end of the
1810 * request queue, and the function returns immediately without waiting
1811 * for the new rq to be completed. This is again intended for careful
1812 * use by the ATAPI tape/cdrom driver code.
1813 */
1814 int ide_do_drive_cmd (ide_drive_t *drive, struct request *rq, ide_action_t action)
1815 {
1816 unsigned long flags;
1817 ide_hwgroup_t *hwgroup = HWGROUP(drive);
1818 unsigned int major = HWIF(drive)->major;
1819 struct list_head *queue_head = &drive->queue.queue_head;
1820 DECLARE_COMPLETION(wait);
1821
1822 #ifdef CONFIG_BLK_DEV_PDC4030
1823 if (HWIF(drive)->chipset == ide_pdc4030 && rq->buffer != NULL)
1824 return -ENOSYS; /* special drive cmds not supported */
1825 #endif
1826 rq->errors = 0;
1827 rq->rq_status = RQ_ACTIVE;
1828 rq->rq_dev = MKDEV(major,(drive->select.b.unit)<<PARTN_BITS);
1829 if (action == ide_wait)
1830 rq->waiting = &wait;
1831 spin_lock_irqsave(&io_request_lock, flags);
1832 if (list_empty(queue_head) || action == ide_preempt) {
1833 if (action == ide_preempt)
1834 hwgroup->rq = NULL;
1835 } else {
1836 if (action == ide_wait || action == ide_end) {
1837 queue_head = queue_head->prev;
1838 } else
1839 queue_head = queue_head->next;
1840 }
1841 list_add(&rq->queue, queue_head);
1842 ide_do_request(hwgroup, 0);
1843 spin_unlock_irqrestore(&io_request_lock, flags);
1844 if (action == ide_wait) {
1845 wait_for_completion(&wait); /* wait for it to be serviced */
1846 return rq->errors ? -EIO : 0; /* return -EIO if errors */
1847 }
1848 return 0;
1849
1850 }
1851
1852 /*
1853 * This routine is called to flush all partitions and partition tables
1854 * for a changed disk, and then re-read the new partition table.
1855 * If we are revalidating a disk because of a media change, then we
1856 * enter with usage == 0. If we are using an ioctl, we automatically have
1857 * usage == 1 (we need an open channel to use an ioctl :-), so this
1858 * is our limit.
1859 */
1860 int ide_revalidate_disk (kdev_t i_rdev)
1861 {
1862 ide_drive_t *drive;
1863 ide_hwgroup_t *hwgroup;
1864 unsigned int p, major, minor;
1865 long flags;
1866
1867 if ((drive = get_info_ptr(i_rdev)) == NULL)
1868 return -ENODEV;
1869 major = MAJOR(i_rdev);
1870 minor = drive->select.b.unit << PARTN_BITS;
1871 hwgroup = HWGROUP(drive);
1872 spin_lock_irqsave(&io_request_lock, flags);
1873 if (drive->busy || (drive->usage > 1)) {
1874 spin_unlock_irqrestore(&io_request_lock, flags);
1875 return -EBUSY;
1876 };
1877 drive->busy = 1;
1878 MOD_INC_USE_COUNT;
1879 spin_unlock_irqrestore(&io_request_lock, flags);
1880
1881 for (p = 0; p < (1<<PARTN_BITS); ++p) {
1882 if (drive->part[p].nr_sects > 0) {
1883 kdev_t devp = MKDEV(major, minor+p);
1884 invalidate_device(devp, 1);
1885 set_blocksize(devp, 1024);
1886 }
1887 drive->part[p].start_sect = 0;
1888 drive->part[p].nr_sects = 0;
1889 };
1890
1891 if (DRIVER(drive)->revalidate)
1892 DRIVER(drive)->revalidate(drive);
1893
1894 drive->busy = 0;
1895 wake_up(&drive->wqueue);
1896 MOD_DEC_USE_COUNT;
1897 return 0;
1898 }
1899
1900 static void revalidate_drives (void)
1901 {
1902 ide_hwif_t *hwif;
1903 ide_drive_t *drive;
1904 int index, unit;
1905
1906 for (index = 0; index < MAX_HWIFS; ++index) {
1907 hwif = &ide_hwifs[index];
1908 for (unit = 0; unit < MAX_DRIVES; ++unit) {
1909 drive = &ide_hwifs[index].drives[unit];
1910 if (drive->revalidate) {
1911 drive->revalidate = 0;
1912 if (!initializing)
1913 (void) ide_revalidate_disk(MKDEV(hwif->major, unit<<PARTN_BITS));
1914 }
1915 }
1916 }
1917 }
1918
1919 static void ide_probe_module (void)
1920 {
1921 if (!ide_probe) {
1922 #if defined(CONFIG_KMOD) && defined(CONFIG_BLK_DEV_IDE_MODULE)
1923 (void) request_module("ide-probe-mod");
1924 #endif /* (CONFIG_KMOD) && (CONFIG_BLK_DEV_IDE_MODULE) */
1925 } else {
1926 (void) ide_probe->init();
1927 }
1928 revalidate_drives();
1929 }
1930
1931 static void ide_driver_module (void)
1932 {
1933 int index;
1934 ide_module_t *module = ide_modules;
1935
1936 for (index = 0; index < MAX_HWIFS; ++index)
1937 if (ide_hwifs[index].present)
1938 goto search;
1939 ide_probe_module();
1940 search:
1941 while (module) {
1942 (void) module->init();
1943 module = module->next;
1944 }
1945 revalidate_drives();
1946 }
1947
1948 static int ide_open (struct inode * inode, struct file * filp)
1949 {
1950 ide_drive_t *drive;
1951 int rc;
1952
1953 if ((drive = get_info_ptr(inode->i_rdev)) == NULL)
1954 return -ENXIO;
1955 MOD_INC_USE_COUNT;
1956 if (drive->driver == NULL)
1957 ide_driver_module();
1958 #ifdef CONFIG_KMOD
1959 if (drive->driver == NULL) {
1960 if (drive->media == ide_disk)
1961 (void) request_module("ide-disk");
1962 if (drive->media == ide_cdrom)
1963 (void) request_module("ide-cd");
1964 if (drive->media == ide_tape)
1965 (void) request_module("ide-tape");
1966 if (drive->media == ide_floppy)
1967 (void) request_module("ide-floppy");
1968 }
1969 #endif /* CONFIG_KMOD */
1970 while (drive->busy)
1971 sleep_on(&drive->wqueue);
1972 drive->usage++;
1973 if (drive->driver != NULL) {
1974 if ((rc = DRIVER(drive)->open(inode, filp, drive)))
1975 MOD_DEC_USE_COUNT;
1976 return rc;
1977 }
1978 printk ("%s: driver not present\n", drive->name);
1979 drive->usage--;
1980 MOD_DEC_USE_COUNT;
1981 return -ENXIO;
1982 }
1983
1984 /*
1985 * Releasing a block device means we sync() it, so that it can safely
1986 * be forgotten about...
1987 */
1988 static int ide_release (struct inode * inode, struct file * file)
1989 {
1990 ide_drive_t *drive;
1991
1992 if ((drive = get_info_ptr(inode->i_rdev)) != NULL) {
1993 drive->usage--;
1994 if (drive->driver != NULL)
1995 DRIVER(drive)->release(inode, file, drive);
1996 MOD_DEC_USE_COUNT;
1997 }
1998 return 0;
1999 }
2000
2001 int ide_replace_subdriver (ide_drive_t *drive, const char *driver)
2002 {
2003 if (!drive->present || drive->busy || drive->usage)
2004 goto abort;
2005 if (drive->driver != NULL && DRIVER(drive)->cleanup(drive))
2006 goto abort;
2007 strncpy(drive->driver_req, driver, 9);
2008 ide_driver_module();
2009 drive->driver_req[0] = 0;
2010 ide_driver_module();
2011 if (DRIVER(drive) && !strcmp(DRIVER(drive)->name, driver))
2012 return 0;
2013 abort:
2014 return 1;
2015 }
2016
2017 #ifdef CONFIG_PROC_FS
2018 ide_proc_entry_t generic_subdriver_entries[] = {
2019 { "capacity", S_IFREG|S_IRUGO, proc_ide_read_capacity, NULL },
2020 { NULL, 0, NULL, NULL }
2021 };
2022 #endif
2023
2024 /*
2025 * Note that we only release the standard ports,
2026 * and do not even try to handle any extra ports
2027 * allocated for weird IDE interface chipsets.
2028 */
2029 void hwif_unregister (ide_hwif_t *hwif)
2030 {
2031 if (hwif->straight8) {
2032 ide_release_region(hwif->io_ports[IDE_DATA_OFFSET], 8);
2033 goto jump_eight;
2034 }
2035 if (hwif->io_ports[IDE_DATA_OFFSET])
2036 ide_release_region(hwif->io_ports[IDE_DATA_OFFSET], 1);
2037 if (hwif->io_ports[IDE_ERROR_OFFSET])
2038 ide_release_region(hwif->io_ports[IDE_ERROR_OFFSET], 1);
2039 if (hwif->io_ports[IDE_NSECTOR_OFFSET])
2040 ide_release_region(hwif->io_ports[IDE_NSECTOR_OFFSET], 1);
2041 if (hwif->io_ports[IDE_SECTOR_OFFSET])
2042 ide_release_region(hwif->io_ports[IDE_SECTOR_OFFSET], 1);
2043 if (hwif->io_ports[IDE_LCYL_OFFSET])
2044 ide_release_region(hwif->io_ports[IDE_LCYL_OFFSET], 1);
2045 if (hwif->io_ports[IDE_HCYL_OFFSET])
2046 ide_release_region(hwif->io_ports[IDE_HCYL_OFFSET], 1);
2047 if (hwif->io_ports[IDE_SELECT_OFFSET])
2048 ide_release_region(hwif->io_ports[IDE_SELECT_OFFSET], 1);
2049 if (hwif->io_ports[IDE_STATUS_OFFSET])
2050 ide_release_region(hwif->io_ports[IDE_STATUS_OFFSET], 1);
2051 jump_eight:
2052 if (hwif->io_ports[IDE_CONTROL_OFFSET])
2053 ide_release_region(hwif->io_ports[IDE_CONTROL_OFFSET], 1);
2054 #if defined(CONFIG_AMIGA) || defined(CONFIG_MAC)
2055 if (hwif->io_ports[IDE_IRQ_OFFSET])
2056 ide_release_region(hwif->io_ports[IDE_IRQ_OFFSET], 1);
2057 #endif /* (CONFIG_AMIGA) || (CONFIG_MAC) */
2058 }
2059
2060 void ide_unregister (unsigned int index)
2061 {
2062 struct gendisk *gd;
2063 ide_drive_t *drive, *d;
2064 ide_hwif_t *hwif, *g;
2065 ide_hwgroup_t *hwgroup;
2066 int irq_count = 0, unit, i;
2067 unsigned long flags;
2068 unsigned int p, minor;
2069 ide_hwif_t old_hwif;
2070
2071 if (index >= MAX_HWIFS)
2072 return;
2073 save_flags(flags); /* all CPUs */
2074 cli(); /* all CPUs */
2075 hwif = &ide_hwifs[index];
2076 if (!hwif->present)
2077 goto abort;
2078 for (unit = 0; unit < MAX_DRIVES; ++unit) {
2079 drive = &hwif->drives[unit];
2080 if (!drive->present)
2081 continue;
2082 if (drive->busy || drive->usage)
2083 goto abort;
2084 if (drive->driver != NULL && DRIVER(drive)->cleanup(drive))
2085 goto abort;
2086 }
2087 hwif->present = 0;
2088
2089 /*
2090 * All clear? Then blow away the buffer cache
2091 */
2092 sti();
2093 for (unit = 0; unit < MAX_DRIVES; ++unit) {
2094 drive = &hwif->drives[unit];
2095 if (!drive->present)
2096 continue;
2097 minor = drive->select.b.unit << PARTN_BITS;
2098 for (p = 0; p < (1<<PARTN_BITS); ++p) {
2099 if (drive->part[p].nr_sects > 0) {
2100 kdev_t devp = MKDEV(hwif->major, minor+p);
2101 invalidate_device(devp, 0);
2102 }
2103 }
2104 #ifdef CONFIG_PROC_FS
2105 destroy_proc_ide_drives(hwif);
2106 #endif
2107 }
2108 cli();
2109 hwgroup = hwif->hwgroup;
2110
2111 /*
2112 * free the irq if we were the only hwif using it
2113 */
2114 g = hwgroup->hwif;
2115 do {
2116 if (g->irq == hwif->irq)
2117 ++irq_count;
2118 g = g->next;
2119 } while (g != hwgroup->hwif);
2120 if (irq_count == 1)
2121 free_irq(hwif->irq, hwgroup);
2122
2123 /*
2124 * Note that we only release the standard ports,
2125 * and do not even try to handle any extra ports
2126 * allocated for weird IDE interface chipsets.
2127 */
2128 hwif_unregister(hwif);
2129
2130 /*
2131 * Remove us from the hwgroup, and free
2132 * the hwgroup if we were the only member
2133 */
2134 d = hwgroup->drive;
2135 for (i = 0; i < MAX_DRIVES; ++i) {
2136 drive = &hwif->drives[i];
2137 if (drive->de) {
2138 devfs_unregister (drive->de);
2139 drive->de = NULL;
2140 }
2141 if (!drive->present)
2142 continue;
2143 while (hwgroup->drive->next != drive)
2144 hwgroup->drive = hwgroup->drive->next;
2145 hwgroup->drive->next = drive->next;
2146 if (hwgroup->drive == drive)
2147 hwgroup->drive = NULL;
2148 if (drive->id != NULL) {
2149 kfree(drive->id);
2150 drive->id = NULL;
2151 }
2152 drive->present = 0;
2153 blk_cleanup_queue(&drive->queue);
2154 }
2155 if (d->present)
2156 hwgroup->drive = d;
2157 while (hwgroup->hwif->next != hwif)
2158 hwgroup->hwif = hwgroup->hwif->next;
2159 hwgroup->hwif->next = hwif->next;
2160 if (hwgroup->hwif == hwif)
2161 kfree(hwgroup);
2162 else
2163 hwgroup->hwif = HWIF(hwgroup->drive);
2164
2165 #if defined(CONFIG_BLK_DEV_IDEDMA) && !defined(CONFIG_DMA_NONPCI)
2166 if (hwif->dma_base) {
2167 (void) ide_release_dma(hwif);
2168 hwif->dma_base = 0;
2169 }
2170 #endif /* (CONFIG_BLK_DEV_IDEDMA) && !(CONFIG_DMA_NONPCI) */
2171
2172 /*
2173 * Remove us from the kernel's knowledge
2174 */
2175 unregister_blkdev(hwif->major, hwif->name);
2176 kfree(blksize_size[hwif->major]);
2177 kfree(max_sectors[hwif->major]);
2178 kfree(max_readahead[hwif->major]);
2179 blk_dev[hwif->major].data = NULL;
2180 blk_dev[hwif->major].queue = NULL;
2181 blksize_size[hwif->major] = NULL;
2182 gd = hwif->gd;
2183 if (gd) {
2184 del_gendisk(gd);
2185 kfree(gd->sizes);
2186 kfree(gd->part);
2187 if (gd->de_arr)
2188 kfree (gd->de_arr);
2189 if (gd->flags)
2190 kfree (gd->flags);
2191 kfree(gd);
2192 hwif->gd = NULL;
2193 }
2194 old_hwif = *hwif;
2195 init_hwif_data (index); /* restore hwif data to pristine status */
2196 hwif->hwgroup = old_hwif.hwgroup;
2197 hwif->tuneproc = old_hwif.tuneproc;
2198 hwif->speedproc = old_hwif.speedproc;
2199 hwif->selectproc = old_hwif.selectproc;
2200 hwif->resetproc = old_hwif.resetproc;
2201 hwif->intrproc = old_hwif.intrproc;
2202 hwif->maskproc = old_hwif.maskproc;
2203 hwif->quirkproc = old_hwif.quirkproc;
2204 hwif->rwproc = old_hwif.rwproc;
2205 hwif->ideproc = old_hwif.ideproc;
2206 hwif->dmaproc = old_hwif.dmaproc;
2207 hwif->busproc = old_hwif.busproc;
2208 hwif->bus_state = old_hwif.bus_state;
2209 hwif->dma_base = old_hwif.dma_base;
2210 hwif->dma_extra = old_hwif.dma_extra;
2211 hwif->config_data = old_hwif.config_data;
2212 hwif->select_data = old_hwif.select_data;
2213 hwif->proc = old_hwif.proc;
2214 #ifndef CONFIG_BLK_DEV_IDECS
2215 hwif->irq = old_hwif.irq;
2216 #endif /* CONFIG_BLK_DEV_IDECS */
2217 hwif->major = old_hwif.major;
2218 hwif->chipset = old_hwif.chipset;
2219 hwif->autodma = old_hwif.autodma;
2220 hwif->udma_four = old_hwif.udma_four;
2221 #ifdef CONFIG_BLK_DEV_IDEPCI
2222 hwif->pci_dev = old_hwif.pci_dev;
2223 hwif->pci_devid = old_hwif.pci_devid;
2224 #endif /* CONFIG_BLK_DEV_IDEPCI */
2225 hwif->straight8 = old_hwif.straight8;
2226 hwif->hwif_data = old_hwif.hwif_data;
2227 abort:
2228 restore_flags(flags); /* all CPUs */
2229 }
2230
2231 /*
2232 * Setup hw_regs_t structure described by parameters. You
2233 * may set up the hw structure yourself OR use this routine to
2234 * do it for you.
2235 */
2236 void ide_setup_ports ( hw_regs_t *hw,
2237 ide_ioreg_t base, int *offsets,
2238 ide_ioreg_t ctrl, ide_ioreg_t intr,
2239 ide_ack_intr_t *ack_intr, int irq)
2240 {
2241 int i;
2242
2243 for (i = 0; i < IDE_NR_PORTS; i++) {
2244 if (offsets[i] == -1) {
2245 switch(i) {
2246 case IDE_CONTROL_OFFSET:
2247 hw->io_ports[i] = ctrl;
2248 break;
2249 #if defined(CONFIG_AMIGA) || defined(CONFIG_MAC)
2250 case IDE_IRQ_OFFSET:
2251 hw->io_ports[i] = intr;
2252 break;
2253 #endif /* (CONFIG_AMIGA) || (CONFIG_MAC) */
2254 default:
2255 hw->io_ports[i] = 0;
2256 break;
2257 }
2258 } else {
2259 hw->io_ports[i] = base + offsets[i];
2260 }
2261 }
2262 hw->irq = irq;
2263 hw->dma = NO_DMA;
2264 hw->ack_intr = ack_intr;
2265 }
2266
2267 /*
2268 * Register an IDE interface, specifing exactly the registers etc
2269 * Set init=1 iff calling before probes have taken place.
2270 */
2271 int ide_register_hw (hw_regs_t *hw, ide_hwif_t **hwifp)
2272 {
2273 int index, retry = 1;
2274 ide_hwif_t *hwif;
2275
2276 do {
2277 for (index = 0; index < MAX_HWIFS; ++index) {
2278 hwif = &ide_hwifs[index];
2279 if (hwif->hw.io_ports[IDE_DATA_OFFSET] == hw->io_ports[IDE_DATA_OFFSET])
2280 goto found;
2281 }
2282 for (index = 0; index < MAX_HWIFS; ++index) {
2283 hwif = &ide_hwifs[index];
2284 if ((!hwif->present && !hwif->mate && !initializing) ||
2285 (!hwif->hw.io_ports[IDE_DATA_OFFSET] && initializing))
2286 goto found;
2287 }
2288 for (index = 0; index < MAX_HWIFS; index++)
2289 ide_unregister(index);
2290 } while (retry--);
2291 return -1;
2292 found:
2293 if (hwif->present)
2294 ide_unregister(index);
2295 if (hwif->present)
2296 return -1;
2297 memcpy(&hwif->hw, hw, sizeof(*hw));
2298 memcpy(hwif->io_ports, hwif->hw.io_ports, sizeof(hwif->hw.io_ports));
2299 hwif->irq = hw->irq;
2300 hwif->noprobe = 0;
2301
2302 if (!initializing) {
2303 ide_probe_module();
2304 #ifdef CONFIG_PROC_FS
2305 create_proc_ide_interfaces();
2306 #endif
2307 ide_driver_module();
2308 }
2309
2310 if (hwifp)
2311 *hwifp = hwif;
2312
2313 return (initializing || hwif->present) ? index : -1;
2314 }
2315
2316 /*
2317 * Compatability function with existing drivers. If you want
2318 * something different, use the function above.
2319 */
2320 int ide_register (int arg1, int arg2, int irq)
2321 {
2322 hw_regs_t hw;
2323 ide_init_hwif_ports(&hw, (ide_ioreg_t) arg1, (ide_ioreg_t) arg2, NULL);
2324 hw.irq = irq;
2325 return ide_register_hw(&hw, NULL);
2326 }
2327
2328 void ide_add_setting (ide_drive_t *drive, const char *name, int rw, int read_ioctl, int write_ioctl, int data_type, int min, int max, int mul_factor, int div_factor, void *data, ide_procset_t *set)
2329 {
2330 ide_settings_t **p = (ide_settings_t **) &drive->settings, *setting = NULL;
2331
2332 while ((*p) && strcmp((*p)->name, name) < 0)
2333 p = &((*p)->next);
2334 if ((setting = kmalloc(sizeof(*setting), GFP_KERNEL)) == NULL)
2335 goto abort;
2336 memset(setting, 0, sizeof(*setting));
2337 if ((setting->name = kmalloc(strlen(name) + 1, GFP_KERNEL)) == NULL)
2338 goto abort;
2339 strcpy(setting->name, name); setting->rw = rw;
2340 setting->read_ioctl = read_ioctl; setting->write_ioctl = write_ioctl;
2341 setting->data_type = data_type; setting->min = min;
2342 setting->max = max; setting->mul_factor = mul_factor;
2343 setting->div_factor = div_factor; setting->data = data;
2344 setting->set = set; setting->next = *p;
2345 if (drive->driver)
2346 setting->auto_remove = 1;
2347 *p = setting;
2348 return;
2349 abort:
2350 if (setting)
2351 kfree(setting);
2352 }
2353
2354 void ide_remove_setting (ide_drive_t *drive, char *name)
2355 {
2356 ide_settings_t **p = (ide_settings_t **) &drive->settings, *setting;
2357
2358 while ((*p) && strcmp((*p)->name, name))
2359 p = &((*p)->next);
2360 if ((setting = (*p)) == NULL)
2361 return;
2362 (*p) = setting->next;
2363 kfree(setting->name);
2364 kfree(setting);
2365 }
2366
2367 static ide_settings_t *ide_find_setting_by_ioctl (ide_drive_t *drive, int cmd)
2368 {
2369 ide_settings_t *setting = drive->settings;
2370
2371 while (setting) {
2372 if (setting->read_ioctl == cmd || setting->write_ioctl == cmd)
2373 break;
2374 setting = setting->next;
2375 }
2376 return setting;
2377 }
2378
2379 ide_settings_t *ide_find_setting_by_name (ide_drive_t *drive, char *name)
2380 {
2381 ide_settings_t *setting = drive->settings;
2382
2383 while (setting) {
2384 if (strcmp(setting->name, name) == 0)
2385 break;
2386 setting = setting->next;
2387 }
2388 return setting;
2389 }
2390
2391 static void auto_remove_settings (ide_drive_t *drive)
2392 {
2393 ide_settings_t *setting;
2394 repeat:
2395 setting = drive->settings;
2396 while (setting) {
2397 if (setting->auto_remove) {
2398 ide_remove_setting(drive, setting->name);
2399 goto repeat;
2400 }
2401 setting = setting->next;
2402 }
2403 }
2404
2405 int ide_read_setting (ide_drive_t *drive, ide_settings_t *setting)
2406 {
2407 int val = -EINVAL;
2408 unsigned long flags;
2409
2410 if ((setting->rw & SETTING_READ)) {
2411 spin_lock_irqsave(&io_request_lock, flags);
2412 switch(setting->data_type) {
2413 case TYPE_BYTE:
2414 val = *((u8 *) setting->data);
2415 break;
2416 case TYPE_SHORT:
2417 val = *((u16 *) setting->data);
2418 break;
2419 case TYPE_INT:
2420 case TYPE_INTA:
2421 val = *((u32 *) setting->data);
2422 break;
2423 }
2424 spin_unlock_irqrestore(&io_request_lock, flags);
2425 }
2426 return val;
2427 }
2428
2429 int ide_spin_wait_hwgroup (ide_drive_t *drive)
2430 {
2431 ide_hwgroup_t *hwgroup = HWGROUP(drive);
2432 unsigned long timeout = jiffies + (3 * HZ);
2433
2434 spin_lock_irq(&io_request_lock);
2435
2436 while (hwgroup->busy) {
2437 unsigned long lflags;
2438 spin_unlock_irq(&io_request_lock);
2439 __save_flags(lflags); /* local CPU only */
2440 __sti(); /* local CPU only; needed for jiffies */
2441 if (0 < (signed long)(jiffies - timeout)) {
2442 __restore_flags(lflags); /* local CPU only */
2443 printk("%s: channel busy\n", drive->name);
2444 return -EBUSY;
2445 }
2446 __restore_flags(lflags); /* local CPU only */
2447 spin_lock_irq(&io_request_lock);
2448 }
2449 return 0;
2450 }
2451
2452 /*
2453 * FIXME: This should be changed to enqueue a special request
2454 * to the driver to change settings, and then wait on a sema for completion.
2455 * The current scheme of polling is kludgey, though safe enough.
2456 */
2457 int ide_write_setting (ide_drive_t *drive, ide_settings_t *setting, int val)
2458 {
2459 int i;
2460 u32 *p;
2461
2462 if (!capable(CAP_SYS_ADMIN))
2463 return -EACCES;
2464 if (!(setting->rw & SETTING_WRITE))
2465 return -EPERM;
2466 if (val < setting->min || val > setting->max)
2467 return -EINVAL;
2468 if (setting->set)
2469 return setting->set(drive, val);
2470 if (ide_spin_wait_hwgroup(drive))
2471 return -EBUSY;
2472 switch (setting->data_type) {
2473 case TYPE_BYTE:
2474 *((u8 *) setting->data) = val;
2475 break;
2476 case TYPE_SHORT:
2477 *((u16 *) setting->data) = val;
2478 break;
2479 case TYPE_INT:
2480 *((u32 *) setting->data) = val;
2481 break;
2482 case TYPE_INTA:
2483 p = (u32 *) setting->data;
2484 for (i = 0; i < 1 << PARTN_BITS; i++, p++)
2485 *p = val;
2486 break;
2487 }
2488 spin_unlock_irq(&io_request_lock);
2489 return 0;
2490 }
2491
2492 static int set_io_32bit(ide_drive_t *drive, int arg)
2493 {
2494 drive->io_32bit = arg;
2495 #ifdef CONFIG_BLK_DEV_DTC2278
2496 if (HWIF(drive)->chipset == ide_dtc2278)
2497 HWIF(drive)->drives[!drive->select.b.unit].io_32bit = arg;
2498 #endif /* CONFIG_BLK_DEV_DTC2278 */
2499 return 0;
2500 }
2501
2502 static int set_using_dma (ide_drive_t *drive, int arg)
2503 {
2504 if (!drive->driver || !DRIVER(drive)->supports_dma)
2505 return -EPERM;
2506 if (!drive->id || !(drive->id->capability & 1) || !HWIF(drive)->dmaproc)
2507 return -EPERM;
2508 if (HWIF(drive)->dmaproc(arg ? ide_dma_on : ide_dma_off, drive))
2509 return -EIO;
2510 return 0;
2511 }
2512
2513 static int set_pio_mode (ide_drive_t *drive, int arg)
2514 {
2515 struct request rq;
2516
2517 if (!HWIF(drive)->tuneproc)
2518 return -ENOSYS;
2519 if (drive->special.b.set_tune)
2520 return -EBUSY;
2521 ide_init_drive_cmd(&rq);
2522 drive->tune_req = (byte) arg;
2523 drive->special.b.set_tune = 1;
2524 (void) ide_do_drive_cmd (drive, &rq, ide_wait);
2525 return 0;
2526 }
2527
2528 void ide_add_generic_settings (ide_drive_t *drive)
2529 {
2530 /*
2531 * drive setting name read/write access read ioctl write ioctl data type min max mul_factor div_factor data pointer set function
2532 */
2533 ide_add_setting(drive, "io_32bit", drive->no_io_32bit ? SETTING_READ : SETTING_RW, HDIO_GET_32BIT, HDIO_SET_32BIT, TYPE_BYTE, 0, 1 + (SUPPORT_VLB_SYNC << 1), 1, 1, &drive->io_32bit, set_io_32bit);
2534 ide_add_setting(drive, "keepsettings", SETTING_RW, HDIO_GET_KEEPSETTINGS, HDIO_SET_KEEPSETTINGS, TYPE_BYTE, 0, 1, 1, 1, &drive->keep_settings, NULL);
2535 ide_add_setting(drive, "nice1", SETTING_RW, -1, -1, TYPE_BYTE, 0, 1, 1, 1, &drive->nice1, NULL);
2536 ide_add_setting(drive, "pio_mode", SETTING_WRITE, -1, HDIO_SET_PIO_MODE, TYPE_BYTE, 0, 255, 1, 1, NULL, set_pio_mode);
2537 ide_add_setting(drive, "slow", SETTING_RW, -1, -1, TYPE_BYTE, 0, 1, 1, 1, &drive->slow, NULL);
2538 ide_add_setting(drive, "unmaskirq", drive->no_unmask ? SETTING_READ : SETTING_RW, HDIO_GET_UNMASKINTR, HDIO_SET_UNMASKINTR, TYPE_BYTE, 0, 1, 1, 1, &drive->unmask, NULL);
2539 ide_add_setting(drive, "using_dma", SETTING_RW, HDIO_GET_DMA, HDIO_SET_DMA, TYPE_BYTE, 0, 1, 1, 1, &drive->using_dma, set_using_dma);
2540 ide_add_setting(drive, "ide_scsi", SETTING_RW, -1, -1, TYPE_BYTE, 0, 1, 1, 1, &drive->scsi, NULL);
2541 ide_add_setting(drive, "init_speed", SETTING_RW, -1, -1, TYPE_BYTE, 0, 69, 1, 1, &drive->init_speed, NULL);
2542 ide_add_setting(drive, "current_speed", SETTING_RW, -1, -1, TYPE_BYTE, 0, 69, 1, 1, &drive->current_speed, NULL);
2543 ide_add_setting(drive, "number", SETTING_RW, -1, -1, TYPE_BYTE, 0, 3, 1, 1, &drive->dn, NULL);
2544 }
2545
2546 int ide_wait_cmd (ide_drive_t *drive, int cmd, int nsect, int feature, int sectors, byte *buf)
2547 {
2548 struct request rq;
2549 byte buffer[4];
2550
2551 if (!buf)
2552 buf = buffer;
2553 memset(buf, 0, 4 + SECTOR_WORDS * 4 * sectors);
2554 ide_init_drive_cmd(&rq);
2555 rq.buffer = buf;
2556 *buf++ = cmd;
2557 *buf++ = nsect;
2558 *buf++ = feature;
2559 *buf++ = sectors;
2560 return ide_do_drive_cmd(drive, &rq, ide_wait);
2561 }
2562
2563 int ide_wait_cmd_task (ide_drive_t *drive, byte *buf)
2564 {
2565 struct request rq;
2566
2567 ide_init_drive_cmd(&rq);
2568 rq.cmd = IDE_DRIVE_TASK;
2569 rq.buffer = buf;
2570 return ide_do_drive_cmd(drive, &rq, ide_wait);
2571 }
2572
2573 /*
2574 * Delay for *at least* 50ms. As we don't know how much time is left
2575 * until the next tick occurs, we wait an extra tick to be safe.
2576 * This is used only during the probing/polling for drives at boot time.
2577 *
2578 * However, its usefullness may be needed in other places, thus we export it now.
2579 * The future may change this to a millisecond setable delay.
2580 */
2581 void ide_delay_50ms (void)
2582 {
2583 #ifndef CONFIG_BLK_DEV_IDECS
2584 mdelay(50);
2585 #else
2586 __set_current_state(TASK_UNINTERRUPTIBLE);
2587 schedule_timeout(HZ/20);
2588 #endif /* CONFIG_BLK_DEV_IDECS */
2589 }
2590
2591 int system_bus_clock (void)
2592 {
2593 return((int) ((!system_bus_speed) ? ide_system_bus_speed() : system_bus_speed ));
2594 }
2595
2596 static int ide_ioctl (struct inode *inode, struct file *file,
2597 unsigned int cmd, unsigned long arg)
2598 {
2599 int err = 0, major, minor;
2600 ide_drive_t *drive;
2601 struct request rq;
2602 kdev_t dev;
2603 ide_settings_t *setting;
2604
2605 if (!inode || !(dev = inode->i_rdev))
2606 return -EINVAL;
2607 major = MAJOR(dev); minor = MINOR(dev);
2608 if ((drive = get_info_ptr(inode->i_rdev)) == NULL)
2609 return -ENODEV;
2610
2611 if ((setting = ide_find_setting_by_ioctl(drive, cmd)) != NULL) {
2612 if (cmd == setting->read_ioctl) {
2613 err = ide_read_setting(drive, setting);
2614 return err >= 0 ? put_user(err, (long *) arg) : err;
2615 } else {
2616 if ((MINOR(inode->i_rdev) & PARTN_MASK))
2617 return -EINVAL;
2618 return ide_write_setting(drive, setting, arg);
2619 }
2620 }
2621
2622 ide_init_drive_cmd (&rq);
2623 switch (cmd) {
2624 case HDIO_GETGEO:
2625 {
2626 struct hd_geometry *loc = (struct hd_geometry *) arg;
2627 unsigned short bios_cyl = drive->bios_cyl; /* truncate */
2628 if (!loc || (drive->media != ide_disk && drive->media != ide_floppy)) return -EINVAL;
2629 if (put_user(drive->bios_head, (byte *) &loc->heads)) return -EFAULT;
2630 if (put_user(drive->bios_sect, (byte *) &loc->sectors)) return -EFAULT;
2631 if (put_user(bios_cyl, (unsigned short *) &loc->cylinders)) return -EFAULT;
2632 if (put_user((unsigned)drive->part[MINOR(inode->i_rdev)&PARTN_MASK].start_sect,
2633 (unsigned long *) &loc->start)) return -EFAULT;
2634 return 0;
2635 }
2636
2637 case HDIO_GETGEO_BIG:
2638 {
2639 struct hd_big_geometry *loc = (struct hd_big_geometry *) arg;
2640 if (!loc || (drive->media != ide_disk && drive->media != ide_floppy)) return -EINVAL;
2641 if (put_user(drive->bios_head, (byte *) &loc->heads)) return -EFAULT;
2642 if (put_user(drive->bios_sect, (byte *) &loc->sectors)) return -EFAULT;
2643 if (put_user(drive->bios_cyl, (unsigned int *) &loc->cylinders)) return -EFAULT;
2644 if (put_user((unsigned)drive->part[MINOR(inode->i_rdev)&PARTN_MASK].start_sect,
2645 (unsigned long *) &loc->start)) return -EFAULT;
2646 return 0;
2647 }
2648
2649 case HDIO_GETGEO_BIG_RAW:
2650 {
2651 struct hd_big_geometry *loc = (struct hd_big_geometry *) arg;
2652 if (!loc || (drive->media != ide_disk && drive->media != ide_floppy)) return -EINVAL;
2653 if (put_user(drive->head, (byte *) &loc->heads)) return -EFAULT;
2654 if (put_user(drive->sect, (byte *) &loc->sectors)) return -EFAULT;
2655 if (put_user(drive->cyl, (unsigned int *) &loc->cylinders)) return -EFAULT;
2656 if (put_user((unsigned)drive->part[MINOR(inode->i_rdev)&PARTN_MASK].start_sect,
2657 (unsigned long *) &loc->start)) return -EFAULT;
2658 return 0;
2659 }
2660
2661 case BLKGETSIZE: /* Return device size */
2662 return put_user(drive->part[MINOR(inode->i_rdev)&PARTN_MASK].nr_sects, (long *) arg);
2663 case BLKGETSIZE64:
2664 return put_user((u64)drive->part[MINOR(inode->i_rdev)&PARTN_MASK].nr_sects << 9, (u64 *) arg);
2665
2666 case BLKRRPART: /* Re-read partition tables */
2667 if (!capable(CAP_SYS_ADMIN)) return -EACCES;
2668 return ide_revalidate_disk(inode->i_rdev);
2669
2670 case HDIO_OBSOLETE_IDENTITY:
2671 case HDIO_GET_IDENTITY:
2672 if (MINOR(inode->i_rdev) & PARTN_MASK)
2673 return -EINVAL;
2674 if (drive->id == NULL)
2675 return -ENOMSG;
2676 if (copy_to_user((char *)arg, (char *)drive->id, (cmd == HDIO_GET_IDENTITY) ? sizeof(*drive->id) : 142))
2677 return -EFAULT;
2678 return 0;
2679
2680 case HDIO_GET_NICE:
2681 return put_user(drive->dsc_overlap << IDE_NICE_DSC_OVERLAP |
2682 drive->atapi_overlap << IDE_NICE_ATAPI_OVERLAP |
2683 drive->nice0 << IDE_NICE_0 |
2684 drive->nice1 << IDE_NICE_1 |
2685 drive->nice2 << IDE_NICE_2,
2686 (long *) arg);
2687 case HDIO_DRIVE_CMD:
2688 {
2689 byte args[4], *argbuf = args;
2690 byte xfer_rate = 0;
2691 int argsize = 4;
2692 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO)) return -EACCES;
2693 if (NULL == (void *) arg)
2694 return ide_do_drive_cmd(drive, &rq, ide_wait);
2695 if (copy_from_user(args, (void *)arg, 4))
2696 return -EFAULT;
2697 if (args[3]) {
2698 argsize = 4 + (SECTOR_WORDS * 4 * args[3]);
2699 argbuf = kmalloc(argsize, GFP_KERNEL);
2700 if (argbuf == NULL)
2701 return -ENOMEM;
2702 memcpy(argbuf, args, 4);
2703 }
2704
2705 if (set_transfer(drive, args[0], args[1], args[2])) {
2706 xfer_rate = args[1];
2707 if (ide_ata66_check(drive, args[0], args[1], args[2]))
2708 goto abort;
2709 }
2710
2711 err = ide_wait_cmd(drive, args[0], args[1], args[2], args[3], argbuf);
2712
2713 if (!err && xfer_rate) {
2714 /* active-retuning-calls future */
2715 if ((HWIF(drive)->speedproc) != NULL)
2716 HWIF(drive)->speedproc(drive, xfer_rate);
2717 ide_driveid_update(drive);
2718 }
2719 abort:
2720 if (copy_to_user((void *)arg, argbuf, argsize))
2721 err = -EFAULT;
2722 if (argsize > 4)
2723 kfree(argbuf);
2724 return err;
2725 }
2726 case HDIO_DRIVE_TASK:
2727 {
2728 byte args[7], *argbuf = args;
2729 int argsize = 7;
2730 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO)) return -EACCES;
2731 if (copy_from_user(args, (void *)arg, 7))
2732 return -EFAULT;
2733 err = ide_wait_cmd_task(drive, argbuf);
2734 if (copy_to_user((void *)arg, argbuf, argsize))
2735 err = -EFAULT;
2736 return err;
2737 }
2738 case HDIO_SCAN_HWIF:
2739 {
2740 int args[3];
2741 if (!capable(CAP_SYS_ADMIN)) return -EACCES;
2742 if (copy_from_user(args, (void *)arg, 3 * sizeof(int)))
2743 return -EFAULT;
2744 if (ide_register(args[0], args[1], args[2]) == -1)
2745 return -EIO;
2746 return 0;
2747 }
2748 case HDIO_UNREGISTER_HWIF:
2749 if (!capable(CAP_SYS_ADMIN)) return -EACCES;
2750 /* (arg > MAX_HWIFS) checked in function */
2751 ide_unregister(arg);
2752 return 0;
2753 case HDIO_SET_NICE:
2754 if (!capable(CAP_SYS_ADMIN)) return -EACCES;
2755 if (drive->driver == NULL)
2756 return -EPERM;
2757 if (arg != (arg & ((1 << IDE_NICE_DSC_OVERLAP) | (1 << IDE_NICE_1))))
2758 return -EPERM;
2759 drive->dsc_overlap = (arg >> IDE_NICE_DSC_OVERLAP) & 1;
2760 if (drive->dsc_overlap && !DRIVER(drive)->supports_dsc_overlap) {
2761 drive->dsc_overlap = 0;
2762 return -EPERM;
2763 }
2764 drive->nice1 = (arg >> IDE_NICE_1) & 1;
2765 return 0;
2766 case HDIO_DRIVE_RESET:
2767 if (!capable(CAP_SYS_ADMIN)) return -EACCES;
2768 (void) ide_do_reset(drive);
2769 if (drive->suspend_reset) {
2770 /*
2771 * APM WAKE UP todo !!
2772 * int nogoodpower = 1;
2773 * while(nogoodpower) {
2774 * check_power1() or check_power2()
2775 * nogoodpower = 0;
2776 * }
2777 * HWIF(drive)->multiproc(drive);
2778 */
2779 return ide_revalidate_disk(inode->i_rdev);
2780 }
2781 return 0;
2782
2783 case BLKROSET:
2784 case BLKROGET:
2785 case BLKFLSBUF:
2786 case BLKSSZGET:
2787 case BLKPG:
2788 case BLKELVGET:
2789 case BLKELVSET:
2790 case BLKBSZGET:
2791 case BLKBSZSET:
2792 return blk_ioctl(inode->i_rdev, cmd, arg);
2793
2794 case HDIO_GET_BUSSTATE:
2795 if (!capable(CAP_SYS_ADMIN))
2796 return -EACCES;
2797 if (put_user(HWIF(drive)->bus_state, (long *)arg))
2798 return -EFAULT;
2799 return 0;
2800
2801 case HDIO_SET_BUSSTATE:
2802 if (!capable(CAP_SYS_ADMIN))
2803 return -EACCES;
2804 if (HWIF(drive)->busproc)
2805 HWIF(drive)->busproc(HWIF(drive), arg);
2806 return 0;
2807
2808 default:
2809 if (drive->driver != NULL)
2810 return DRIVER(drive)->ioctl(drive, inode, file, cmd, arg);
2811 return -EPERM;
2812 }
2813 }
2814
2815 static int ide_check_media_change (kdev_t i_rdev)
2816 {
2817 ide_drive_t *drive;
2818
2819 if ((drive = get_info_ptr(i_rdev)) == NULL)
2820 return -ENODEV;
2821 if (drive->driver != NULL)
2822 return DRIVER(drive)->media_change(drive);
2823 return 0;
2824 }
2825
2826 void ide_fixstring (byte *s, const int bytecount, const int byteswap)
2827 {
2828 byte *p = s, *end = &s[bytecount & ~1]; /* bytecount must be even */
2829
2830 if (byteswap) {
2831 /* convert from big-endian to host byte order */
2832 for (p = end ; p != s;) {
2833 unsigned short *pp = (unsigned short *) (p -= 2);
2834 *pp = ntohs(*pp);
2835 }
2836 }
2837
2838 /* strip leading blanks */
2839 while (s != end && *s == ' ')
2840 ++s;
2841
2842 /* compress internal blanks and strip trailing blanks */
2843 while (s != end && *s) {
2844 if (*s++ != ' ' || (s != end && *s && *s != ' '))
2845 *p++ = *(s-1);
2846 }
2847
2848 /* wipe out trailing garbage */
2849 while (p != end)
2850 *p++ = '\0';
2851 }
2852
2853 /*
2854 * stridx() returns the offset of c within s,
2855 * or -1 if c is '\0' or not found within s.
2856 */
2857 static int __init stridx (const char *s, char c)
2858 {
2859 char *i = strchr(s, c);
2860 return (i && c) ? i - s : -1;
2861 }
2862
2863 /*
2864 * match_parm() does parsing for ide_setup():
2865 *
2866 * 1. the first char of s must be '='.
2867 * 2. if the remainder matches one of the supplied keywords,
2868 * the index (1 based) of the keyword is negated and returned.
2869 * 3. if the remainder is a series of no more than max_vals numbers
2870 * separated by commas, the numbers are saved in vals[] and a
2871 * count of how many were saved is returned. Base10 is assumed,
2872 * and base16 is allowed when prefixed with "0x".
2873 * 4. otherwise, zero is returned.
2874 */
2875 static int __init match_parm (char *s, const char *keywords[], int vals[], int max_vals)
2876 {
2877 static const char *decimal = "0123456789";
2878 static const char *hex = "0123456789abcdef";
2879 int i, n;
2880
2881 if (*s++ == '=') {
2882 /*
2883 * Try matching against the supplied keywords,
2884 * and return -(index+1) if we match one
2885 */
2886 if (keywords != NULL) {
2887 for (i = 0; *keywords != NULL; ++i) {
2888 if (!strcmp(s, *keywords++))
2889 return -(i+1);
2890 }
2891 }
2892 /*
2893 * Look for a series of no more than "max_vals"
2894 * numeric values separated by commas, in base10,
2895 * or base16 when prefixed with "0x".
2896 * Return a count of how many were found.
2897 */
2898 for (n = 0; (i = stridx(decimal, *s)) >= 0;) {
2899 vals[n] = i;
2900 while ((i = stridx(decimal, *++s)) >= 0)
2901 vals[n] = (vals[n] * 10) + i;
2902 if (*s == 'x' && !vals[n]) {
2903 while ((i = stridx(hex, *++s)) >= 0)
2904 vals[n] = (vals[n] * 0x10) + i;
2905 }
2906 if (++n == max_vals)
2907 break;
2908 if (*s == ',' || *s == ';')
2909 ++s;
2910 }
2911 if (!*s)
2912 return n;
2913 }
2914 return 0; /* zero = nothing matched */
2915 }
2916
2917 /*
2918 * ide_setup() gets called VERY EARLY during initialization,
2919 * to handle kernel "command line" strings beginning with "hdx="
2920 * or "ide". Here is the complete set currently supported:
2921 *
2922 * "hdx=" is recognized for all "x" from "a" to "h", such as "hdc".
2923 * "idex=" is recognized for all "x" from "0" to "3", such as "ide1".
2924 *
2925 * "hdx=noprobe" : drive may be present, but do not probe for it
2926 * "hdx=none" : drive is NOT present, ignore cmos and do not probe
2927 * "hdx=nowerr" : ignore the WRERR_STAT bit on this drive
2928 * "hdx=cdrom" : drive is present, and is a cdrom drive
2929 * "hdx=cyl,head,sect" : disk drive is present, with specified geometry
2930 * "hdx=noremap" : do not remap 0->1 even though EZD was detected
2931 * "hdx=autotune" : driver will attempt to tune interface speed
2932 * to the fastest PIO mode supported,
2933 * if possible for this drive only.
2934 * Not fully supported by all chipset types,
2935 * and quite likely to cause trouble with
2936 * older/odd IDE drives.
2937 *
2938 * "hdx=slow" : insert a huge pause after each access to the data
2939 * port. Should be used only as a last resort.
2940 *
2941 * "hdx=swapdata" : when the drive is a disk, byte swap all data
2942 * "hdx=bswap" : same as above..........
2943 * "hdxlun=xx" : set the drive last logical unit.
2944 * "hdx=flash" : allows for more than one ata_flash disk to be
2945 * registered. In most cases, only one device
2946 * will be present.
2947 * "hdx=scsi" : the return of the ide-scsi flag, this is useful for
2948 * allowwing ide-floppy, ide-tape, and ide-cdrom|writers
2949 * to use ide-scsi emulation on a device specific option.
2950 * "idebus=xx" : inform IDE driver of VESA/PCI bus speed in MHz,
2951 * where "xx" is between 20 and 66 inclusive,
2952 * used when tuning chipset PIO modes.
2953 * For PCI bus, 25 is correct for a P75 system,
2954 * 30 is correct for P90,P120,P180 systems,
2955 * and 33 is used for P100,P133,P166 systems.
2956 * If in doubt, use idebus=33 for PCI.
2957 * As for VLB, it is safest to not specify it.
2958 *
2959 * "idex=noprobe" : do not attempt to access/use this interface
2960 * "idex=base" : probe for an interface at the addr specified,
2961 * where "base" is usually 0x1f0 or 0x170
2962 * and "ctl" is assumed to be "base"+0x206
2963 * "idex=base,ctl" : specify both base and ctl
2964 * "idex=base,ctl,irq" : specify base, ctl, and irq number
2965 * "idex=autotune" : driver will attempt to tune interface speed
2966 * to the fastest PIO mode supported,
2967 * for all drives on this interface.
2968 * Not fully supported by all chipset types,
2969 * and quite likely to cause trouble with
2970 * older/odd IDE drives.
2971 * "idex=noautotune" : driver will NOT attempt to tune interface speed
2972 * This is the default for most chipsets,
2973 * except the cmd640.
2974 * "idex=serialize" : do not overlap operations on idex and ide(x^1)
2975 * "idex=four" : four drives on idex and ide(x^1) share same ports
2976 * "idex=reset" : reset interface before first use
2977 * "idex=dma" : enable DMA by default on both drives if possible
2978 * "idex=ata66" : informs the interface that it has an 80c cable
2979 * for chipsets that are ATA-66 capable, but
2980 * the ablity to bit test for detection is
2981 * currently unknown.
2982 * "ide=reverse" : Formerly called to pci sub-system, but now local.
2983 *
2984 * The following are valid ONLY on ide0, (except dc4030)
2985 * and the defaults for the base,ctl ports must not be altered.
2986 *
2987 * "ide0=dtc2278" : probe/support DTC2278 interface
2988 * "ide0=ht6560b" : probe/support HT6560B interface
2989 * "ide0=cmd640_vlb" : *REQUIRED* for VLB cards with the CMD640 chip
2990 * (not for PCI -- automatically detected)
2991 * "ide0=qd65xx" : probe/support qd65xx interface
2992 * "ide0=ali14xx" : probe/support ali14xx chipsets (ALI M1439, M1443, M1445)
2993 * "ide0=umc8672" : probe/support umc8672 chipsets
2994 * "idex=dc4030" : probe/support Promise DC4030VL interface
2995 * "ide=doubler" : probe/support IDE doublers on Amiga
2996 */
2997 int __init ide_setup (char *s)
2998 {
2999 int i, vals[3];
3000 ide_hwif_t *hwif;
3001 ide_drive_t *drive;
3002 unsigned int hw, unit;
3003 const char max_drive = 'a' + ((MAX_HWIFS * MAX_DRIVES) - 1);
3004 const char max_hwif = '0' + (MAX_HWIFS - 1);
3005
3006
3007 if (strncmp(s,"hd",2) == 0 && s[2] == '=') /* hd= is for hd.c */
3008 return 0; /* driver and not us */
3009
3010 if (strncmp(s,"ide",3) &&
3011 strncmp(s,"idebus",6) &&
3012 strncmp(s,"hd",2)) /* hdx= & hdxlun= */
3013 return 0;
3014
3015 printk("ide_setup: %s", s);
3016 init_ide_data ();
3017
3018 #ifdef CONFIG_BLK_DEV_IDEDOUBLER
3019 if (!strcmp(s, "ide=doubler")) {
3020 extern int ide_doubler;
3021
3022 printk(" : Enabled support for IDE doublers\n");
3023 ide_doubler = 1;
3024 return 1;
3025 }
3026 #endif /* CONFIG_BLK_DEV_IDEDOUBLER */
3027
3028 if (!strcmp(s, "ide=nodma")) {
3029 printk("IDE: Prevented DMA\n");
3030 noautodma = 1;
3031 return 1;
3032 }
3033
3034 #ifdef CONFIG_BLK_DEV_IDEPCI
3035 if (!strcmp(s, "ide=reverse")) {
3036 ide_scan_direction = 1;
3037 printk(" : Enabled support for IDE inverse scan order.\n");
3038 return 1;
3039 }
3040 #endif /* CONFIG_BLK_DEV_IDEPCI */
3041
3042 /*
3043 * Look for drive options: "hdx="
3044 */
3045 if (s[0] == 'h' && s[1] == 'd' && s[2] >= 'a' && s[2] <= max_drive) {
3046 const char *hd_words[] = {"none", "noprobe", "nowerr", "cdrom",
3047 "serialize", "autotune", "noautotune",
3048 "slow", "swapdata", "bswap", "flash",
3049 "remap", "noremap", "scsi", NULL};
3050 unit = s[2] - 'a';
3051 hw = unit / MAX_DRIVES;
3052 unit = unit % MAX_DRIVES;
3053 hwif = &ide_hwifs[hw];
3054 drive = &hwif->drives[unit];
3055 if (strncmp(s + 4, "ide-", 4) == 0) {
3056 strncpy(drive->driver_req, s + 4, 9);
3057 goto done;
3058 }
3059 /*
3060 * Look for last lun option: "hdxlun="
3061 */
3062 if (s[3] ==