|
|
1.1 root 1: /*
2: * linux/drivers/block/ide.c Version 5.53 Jun 24, 1997
3: *
4: * Copyright (C) 1994-1996 Linus Torvalds & authors (see below)
5: */
6: #define _IDE_C /* needed by <linux/blk.h> */
7:
8: /*
9: * Maintained by Mark Lord <[email protected]>
10: * and Gadi Oxman <[email protected]>
11: *
12: * This is the multiple IDE interface driver, as evolved from hd.c.
13: * It supports up to four IDE interfaces, on one or more IRQs (usually 14 & 15).
14: * There can be up to two drives per interface, as per the ATA-2 spec.
15: *
16: * Primary: ide0, port 0x1f0; major=3; hda is minor=0; hdb is minor=64
17: * Secondary: ide1, port 0x170; major=22; hdc is minor=0; hdd is minor=64
18: * Tertiary: ide2, port 0x???; major=33; hde is minor=0; hdf is minor=64
19: * Quaternary: ide3, port 0x???; major=34; hdg is minor=0; hdh is minor=64
20: *
21: * It is easy to extend ide.c to handle more than four interfaces:
22: *
23: * Change the MAX_HWIFS constant in ide.h.
24: *
25: * Define some new major numbers (in major.h), and insert them into
26: * the ide_hwif_to_major table in ide.c.
27: *
28: * Fill in the extra values for the new interfaces into the two tables
29: * inside ide.c: default_io_base[] and default_irqs[].
30: *
31: * Create the new request handlers by cloning "do_ide3_request()"
32: * for each new interface, and add them to the switch statement
33: * in the ide_init() function in ide.c.
34: *
35: * Recompile, create the new /dev/ entries, and it will probably work.
36: *
37: * From hd.c:
38: * |
39: * | It traverses the request-list, using interrupts to jump between functions.
40: * | As nearly all functions can be called within interrupts, we may not sleep.
41: * | Special care is recommended. Have Fun!
42: * |
43: * | modified by Drew Eckhardt to check nr of hd's from the CMOS.
44: * |
45: * | Thanks to Branko Lankester, [email protected], who found a bug
46: * | in the early extended-partition checks and added DM partitions.
47: * |
48: * | Early work on error handling by Mika Liljeberg ([email protected]).
49: * |
50: * | IRQ-unmask, drive-id, multiple-mode, support for ">16 heads",
51: * | and general streamlining by Mark Lord ([email protected]).
52: *
53: * October, 1994 -- Complete line-by-line overhaul for linux 1.1.x, by:
54: *
55: * Mark Lord ([email protected]) (IDE Perf.Pkg)
56: * Delman Lee ([email protected]) ("Mr. atdisk2")
57: * Scott Snyder ([email protected]) (ATAPI IDE cd-rom)
58: *
59: * This was a rewrite of just about everything from hd.c, though some original
60: * code is still sprinkled about. Think of it as a major evolution, with
61: * inspiration from lots of linux users, esp. [email protected]
62: *
63: * Version 1.0 ALPHA initial code, primary i/f working okay
64: * Version 1.3 BETA dual i/f on shared irq tested & working!
65: * Version 1.4 BETA added auto probing for irq(s)
66: * Version 1.5 BETA added ALPHA (untested) support for IDE cd-roms,
67: * ...
68: * Version 3.5 correct the bios_cyl field if it's too small
69: * (linux 1.1.76) (to help fdisk with brain-dead BIOSs)
70: * Version 3.6 cosmetic corrections to comments and stuff
71: * (linux 1.1.77) reorganise probing code to make it understandable
72: * added halfway retry to probing for drive identification
73: * added "hdx=noprobe" command line option
74: * allow setting multmode even when identification fails
75: * Version 3.7 move set_geometry=1 from do_identify() to ide_init()
76: * increase DRQ_WAIT to eliminate nuisance messages
77: * wait for DRQ_STAT instead of DATA_READY during probing
78: * (courtesy of Gary Thomas [email protected])
79: * Version 3.8 fixed byte-swapping for confused Mitsumi cdrom drives
80: * update of ide-cd.c from Scott, allows blocksize=1024
81: * cdrom probe fixes, inspired by [email protected]
82: * Version 3.9 don't use LBA if lba_capacity looks funny
83: * correct the drive capacity calculations
84: * fix probing for old Seagates without IDE_ALTSTATUS_REG
85: * fix byte-ordering for some NEC cdrom drives
86: * Version 3.10 disable multiple mode by default; was causing trouble
87: * Version 3.11 fix mis-identification of old WD disks as cdroms
88: * Version 3,12 simplify logic for selecting initial mult_count
89: * (fixes problems with buggy WD drives)
90: * Version 3.13 remove excess "multiple mode disabled" messages
91: * Version 3.14 fix ide_error() handling of BUSY_STAT
92: * fix byte-swapped cdrom strings (again.. arghh!)
93: * ignore INDEX bit when checking the ALTSTATUS reg
94: * Version 3.15 add SINGLE_THREADED flag for use with dual-CMD i/f
95: * ignore WRERR_STAT for non-write operations
96: * added vlb_sync support for DC-2000A & others,
97: * (incl. some Promise chips), courtesy of Frank Gockel
98: * Version 3.16 convert vlb_32bit and vlb_sync into runtime flags
99: * add ioctls to get/set VLB flags (HDIO_[SG]ET_CHIPSET)
100: * rename SINGLE_THREADED to SUPPORT_SERIALIZE,
101: * add boot flag to "serialize" operation for CMD i/f
102: * add optional support for DTC2278 interfaces,
103: * courtesy of [email protected] (Dyan Wile).
104: * add boot flag to enable "dtc2278" probe
105: * add probe to avoid EATA (SCSI) interfaces,
106: * courtesy of [email protected].
107: * Version 4.00 tidy up verify_area() calls - [email protected]
108: * add flag to ignore WRERR_STAT for some drives
109: * courtesy of [email protected]
110: * assembly syntax tweak to vlb_sync
111: * removable drive support from [email protected]
112: * add transparent support for DiskManager-6.0x "Dynamic
113: * Disk Overlay" (DDO), most of this is in genhd.c
114: * eliminate "multiple mode turned off" message at boot
115: * Version 4.10 fix bug in ioctl for "hdparm -c3"
116: * fix DM6:DDO support -- now works with LILO, fdisk, ...
117: * don't treat some naughty WD drives as removable
118: * Version 4.11 updated DM6 support using info provided by OnTrack
119: * Version 5.00 major overhaul, multmode setting fixed, vlb_sync fixed
120: * added support for 3rd/4th/alternative IDE ports
121: * created ide.h; ide-cd.c now compiles separate from ide.c
122: * hopefully fixed infinite "unexpected_intr" from cdroms
123: * zillions of other changes and restructuring
124: * somehow reduced overall memory usage by several kB
125: * probably slowed things down slightly, but worth it
126: * Version 5.01 AT LAST!! Finally understood why "unexpected_intr"
127: * was happening at various times/places: whenever the
128: * ide-interface's ctl_port was used to "mask" the irq,
129: * it also would trigger an edge in the process of masking
130: * which would result in a self-inflicted interrupt!!
131: * (such a stupid way to build a hardware interrupt mask).
132: * This is now fixed (after a year of head-scratching).
133: * Version 5.02 got rid of need for {enable,disable}_irq_list()
134: * Version 5.03 tune-ups, comments, remove "busy wait" from drive resets
135: * removed PROBE_FOR_IRQS option -- no longer needed
136: * OOOPS! fixed "bad access" bug for 2nd drive on an i/f
137: * Version 5.04 changed "ira %d" to "irq %d" in DEBUG message
138: * added more comments, cleaned up unexpected_intr()
139: * OOOPS! fixed null pointer problem in ide reset code
140: * added autodetect for Triton chipset -- no effect yet
141: * Version 5.05 OOOPS! fixed bug in revalidate_disk()
142: * OOOPS! fixed bug in ide_do_request()
143: * added ATAPI reset sequence for cdroms
144: * Version 5.10 added Bus-Mastered DMA support for Triton Chipset
145: * some (mostly) cosmetic changes
146: * Version 5.11 added ht6560b support by [email protected]
147: * reworked PCI scanning code
148: * added automatic RZ1000 detection/support
149: * added automatic PCI CMD640 detection/support
150: * added option for VLB CMD640 support
151: * tweaked probe to find cdrom on hdb with disks on hda,hdc
152: * Version 5.12 some performance tuning
153: * added message to alert user to bad /dev/hd[cd] entries
154: * OOOPS! fixed bug in atapi reset
155: * driver now forces "serialize" again for all cmd640 chips
156: * noticed REALLY_SLOW_IO had no effect, moved it to ide.c
157: * made do_drive_cmd() into public ide_do_drive_cmd()
158: * Version 5.13 fixed typo ('B'), thanks to [email protected]
159: * fixed ht6560b support
160: * Version 5.13b (sss) fix problem in calling ide_cdrom_setup()
161: * don't bother invalidating nonexistent partitions
162: * Version 5.14 fixes to cmd640 support.. maybe it works now(?)
163: * added & tested full EZ-DRIVE support -- don't use LILO!
164: * don't enable 2nd CMD640 PCI port during init - conflict
165: * Version 5.15 bug fix in init_cmd640_vlb()
166: * bug fix in interrupt sharing code
167: * Version 5.16 ugh.. fix "serialize" support, broken in 5.15
168: * remove "Huh?" from cmd640 code
169: * added qd6580 interface speed select from Colten Edwards
170: * Version 5.17 kludge around bug in BIOS32 on Intel triton motherboards
171: * Version 5.18 new CMD640 code, moved to cmd640.c, #include'd for now
172: * new UMC8672 code, moved to umc8672.c, #include'd for now
173: * disallow turning on DMA when h/w not capable of DMA
174: * Version 5.19 fix potential infinite timeout on resets
175: * extend reset poll into a general purpose polling scheme
176: * add atapi tape drive support from Gadi Oxman
177: * simplify exit from _intr routines -- no IDE_DO_REQUEST
178: * Version 5.20 leave current rq on blkdev request list during I/O
179: * generalized ide_do_drive_cmd() for tape/cdrom driver use
180: * Version 5.21 fix nasty cdrom/tape bug (ide_preempt was messed up)
181: * Version 5.22 fix ide_xlate_1024() to work with/without drive->id
182: * Version 5.23 miscellaneous touch-ups
183: * Version 5.24 fix #if's for SUPPORT_CMD640
184: * Version 5.25 more touch-ups, fix cdrom resets, ...
185: * cmd640.c now configs/compiles separate from ide.c
186: * Version 5.26 keep_settings now maintains the using_dma flag
187: * fix [EZD] remap message to only output at boot time
188: * fix "bad /dev/ entry" message to say hdc, not hdc0
189: * fix ide_xlate_1024() to respect user specified CHS
190: * use CHS from partn table if it looks translated
191: * re-merged flags chipset,vlb_32bit,vlb_sync into io_32bit
192: * keep track of interface chipset type, when known
193: * add generic PIO mode "tuneproc" mechanism
194: * fix cmd640_vlb option
195: * fix ht6560b support (was completely broken)
196: * umc8672.c now configures/compiles separate from ide.c
197: * move dtc2278 support to dtc2278.c
198: * move ht6560b support to ht6560b.c
199: * move qd6580 support to qd6580.c
200: * add ali14xx support in ali14xx.c
201: * Version 5.27 add [no]autotune parameters to help cmd640
202: * move rz1000 support to rz1000.c
203: * Version 5.28 #include "ide_modes.h"
204: * fix disallow_unmask: now per-interface "no_unmask" bit
205: * force io_32bit to be the same on drive pairs of dtc2278
206: * improved IDE tape error handling, and tape DMA support
207: * bugfix in ide_do_drive_cmd() for cdroms + serialize
208: * Version 5.29 fixed non-IDE check for too many physical heads
209: * don't use LBA if capacity is smaller than CHS
210: * Version 5.30 remove real_devices kludge, formerly used by genhd.c
211: * Version 5.32 change "KB" to "kB"
212: * fix serialize (was broken in kernel 1.3.72)
213: * add support for "hdparm -I"
214: * use common code for disk/tape/cdrom IDE_DRIVE_CMDs
215: * add support for Promise DC4030VL caching card
216: * improved serialize support
217: * put partition check back into alphabetical order
218: * add config option for PCMCIA baggage
219: * try to make PCMCIA support safer to use
220: * improve security on ioctls(): all are suser() only
221: * Version 5.33 improve handling of HDIO_DRIVE_CMDs that read data
222: * Version 5.34 fix irq-sharing problem from 5.33
223: * fix cdrom ioctl problem from 5.33
224: * Version 5.35 cosmetic changes
225: * fix cli() problem in try_to_identify()
226: * Version 5.36 fixes to optional PCMCIA support
227: * Version 5.37 don't use DMA when "noautotune" is specified
228: * Version 5.37a (go) fix shared irq probing (was broken in kernel 1.3.72)
229: * call unplug_device() from ide_do_drive_cmd()
230: * Version 5.38 add "hdx=none" option, courtesy of Joel Maslak
231: * mask drive irq after use, if sharing with another hwif
232: * add code to help debug weird cmd640 problems
233: * Version 5.39 fix horrible error in earlier irq sharing "fix"
234: * Version 5.40 fix serialization -- was broken in 5.39
235: * help sharing by masking device irq after probing
236: * Version 5.41 more fixes to irq sharing/serialize detection
237: * disable io_32bit by default on drive reset
238: * Version 5.42 simplify irq-masking after probe
239: * fix NULL pointer deref in save_match()
240: * Version 5.43 Ugh.. unexpected_intr is back: try to exterminate it
241: * Version 5.44 Fix for "irq probe failed" on cmd640
242: * change path on message regarding MAKEDEV.ide
243: * add a throttle to the unexpected_intr() messages
244: * Version 5.45 fix ugly parameter parsing bugs (thanks Derek)
245: * include Gadi's magic fix for cmd640 unexpected_intr
246: * include mc68000 patches from Geert Uytterhoeven
247: * add Gadi's fix for PCMCIA cdroms
248: * Version 5.46 remove the mc68000 #ifdefs for 2.0.x
249: * Version 5.47 fix set_tune race condition
250: * fix bug in earlier PCMCIA cdrom update
251: * Version 5.48 if def'd, invoke CMD640_DUMP_REGS when irq probe fails
252: * lengthen the do_reset1() pulse, for laptops
253: * add idebus=xx parameter for cmd640 and ali chipsets
254: * no_unmask flag now per-drive instead of per-hwif
255: * fix tune_req so that it gets done immediately
256: * fix missing restore_flags() in ide_ioctl
257: * prevent use of io_32bit on cmd640 with no prefetch
258: * Version 5.49 fix minor quirks in probing routines
259: * Version 5.50 allow values as small as 20 for idebus=
260: * Version 5.51 force non io_32bit in drive_cmd_intr()
261: * change delay_10ms() to delay_50ms() to fix problems
262: * Version 5.52 fix incorrect invalidation of removable devices
263: * add "hdx=slow" command line option
264: * Version 5.53 add ATAPI floppy drive support
265: * change default media for type 0 to floppy
266: * add support for Exabyte Nest
267: * add missing set_blocksize() in revalidate_disk()
268: * handle bad status bit sequencing in ide_wait_stat()
269: * support partition table translations with 255 heads
270: * probe all interfaces by default
271: * add probe for the i82371AB chipset
272: * acknowledge media change on removable drives
273: * add work-around for BMI drives
274: * remove "LBA" from boot messages
275: * Version 5.53.1 add UDMA "CRC retry" support
276: * Version 5.53.2 add Promise/33 auto-detection and DMA support
277: * fix MC_ERR handling
278: * fix mis-detection of NEC cdrom as floppy
279: * issue ATAPI reset and re-probe after "no response"
280: *
281: * Some additional driver compile-time options are in ide.h
282: *
283: * To do, in likely order of completion:
284: * - modify kernel to obtain BIOS geometry for drives on 2nd/3rd/4th i/f
285: */
286:
287: #undef REALLY_SLOW_IO /* most systems can safely undef this */
288:
289: #include <linux/config.h>
290: #include <linux/types.h>
291: #include <linux/string.h>
292: #include <linux/kernel.h>
293: #include <linux/delay.h>
294: #include <linux/timer.h>
295: #include <linux/mm.h>
296: #include <linux/ioport.h>
297: #include <linux/interrupt.h>
298: #include <linux/major.h>
299: #include <linux/blkdev.h>
300: #include <linux/errno.h>
301: #include <linux/hdreg.h>
302: #include <linux/genhd.h>
303: #include <linux/malloc.h>
304:
1.1.1.2 ! root 305: #include <ahci.h>
! 306:
1.1 root 307: #include <asm/byteorder.h>
308: #include <asm/irq.h>
309: #include <asm/segment.h>
310: #include <asm/io.h>
311:
312: #ifdef CONFIG_PCI
313: #include <linux/bios32.h>
314: #include <linux/pci.h>
315: #endif /* CONFIG_PCI */
316:
317: #include "ide.h"
318: #include "ide_modes.h"
319:
320: #ifdef CONFIG_BLK_DEV_PROMISE
321: #include "promise.h"
322: #define IS_PROMISE_DRIVE (HWIF(drive)->chipset == ide_promise)
323: #else
324: #define IS_PROMISE_DRIVE (0) /* auto-NULLs out Promise code */
325: #endif /* CONFIG_BLK_DEV_PROMISE */
326:
327: static const byte ide_hwif_to_major[MAX_HWIFS] = {IDE0_MAJOR, IDE1_MAJOR, IDE2_MAJOR, IDE3_MAJOR};
328: static const unsigned short default_io_base[MAX_HWIFS] = {0x1f0, 0x170, 0x1e8, 0x168};
329: static const byte default_irqs[MAX_HWIFS] = {14, 15, 11, 10};
330: static int idebus_parameter; /* holds the "idebus=" parameter */
331: static int system_bus_speed; /* holds what we think is VESA/PCI bus speed */
332:
333: /*
334: * This is declared extern in ide.h, for access by other IDE modules:
335: */
336: ide_hwif_t ide_hwifs[MAX_HWIFS]; /* master data repository */
337:
338: #if (DISK_RECOVERY_TIME > 0)
339: /*
340: * For really screwy hardware (hey, at least it *can* be used with Linux)
341: * we can enforce a minimum delay time between successive operations.
342: */
343: static unsigned long read_timer(void)
344: {
345: unsigned long t, flags;
346: int i;
347:
348: save_flags(flags);
349: cli();
350: t = jiffies * 11932;
351: outb_p(0, 0x43);
352: i = inb_p(0x40);
353: i |= inb(0x40) << 8;
354: restore_flags(flags);
355: return (t - i);
356: }
357:
358: static void set_recovery_timer (ide_hwif_t *hwif)
359: {
360: hwif->last_time = read_timer();
361: }
362: #define SET_RECOVERY_TIMER(drive) set_recovery_timer (drive)
363:
364: #else
365:
366: #define SET_RECOVERY_TIMER(drive)
367:
368: #endif /* DISK_RECOVERY_TIME */
369:
370:
371: /*
372: * Do not even *think* about calling this!
373: */
374: static void init_hwif_data (unsigned int index)
375: {
376: byte *p;
377: unsigned int unit;
378: ide_hwif_t *hwif = &ide_hwifs[index];
379:
380: /* bulk initialize hwif & drive info with zeros */
381: p = ((byte *) hwif) + sizeof(ide_hwif_t);
382: do {
383: *--p = 0;
384: } while (p > (byte *) hwif);
385:
386: /* fill in any non-zero initial values */
387: hwif->index = index;
388: hwif->io_base = default_io_base[index];
389: hwif->ctl_port = hwif->io_base ? hwif->io_base+0x206 : 0x000;
390: #ifdef CONFIG_BLK_DEV_HD
391: if (hwif->io_base == HD_DATA)
392: hwif->noprobe = 1; /* may be overridden by ide_setup() */
393: #endif /* CONFIG_BLK_DEV_HD */
394: hwif->major = ide_hwif_to_major[index];
395: hwif->name[0] = 'i';
396: hwif->name[1] = 'd';
397: hwif->name[2] = 'e';
398: hwif->name[3] = '0' + index;
399: #ifdef CONFIG_BLK_DEV_IDETAPE
400: hwif->tape_drive = NULL;
401: #endif /* CONFIG_BLK_DEV_IDETAPE */
402: for (unit = 0; unit < MAX_DRIVES; ++unit) {
403: ide_drive_t *drive = &hwif->drives[unit];
404:
405: drive->select.all = (unit<<4)|0xa0;
406: drive->hwif = hwif;
407: drive->ctl = 0x08;
408: drive->ready_stat = READY_STAT;
409: drive->bad_wstat = BAD_W_STAT;
410: drive->special.b.recalibrate = 1;
411: drive->special.b.set_geometry = 1;
412: drive->name[0] = 'h';
413: drive->name[1] = 'd';
1.1.1.2 ! root 414: #ifdef MACH
! 415: drive->name[2] = '0' + (index * MAX_DRIVES) + unit;
! 416: #else
1.1 root 417: drive->name[2] = 'a' + (index * MAX_DRIVES) + unit;
1.1.1.2 ! root 418: #endif
1.1 root 419: }
420: }
421:
422: /*
423: * init_ide_data() sets reasonable default values into all fields
424: * of all instances of the hwifs and drives, but only on the first call.
425: * Subsequent calls have no effect (they don't wipe out anything).
426: *
427: * This routine is normally called at driver initialization time,
428: * but may also be called MUCH earlier during kernel "command-line"
429: * parameter processing. As such, we cannot depend on any other parts
430: * of the kernel (such as memory allocation) to be functioning yet.
431: *
432: * This is too bad, as otherwise we could dynamically allocate the
433: * ide_drive_t structs as needed, rather than always consuming memory
434: * for the max possible number (MAX_HWIFS * MAX_DRIVES) of them.
435: */
436: #define MAGIC_COOKIE 0x12345678
437: static void init_ide_data (void)
438: {
439: unsigned int index;
440: static unsigned long magic_cookie = MAGIC_COOKIE;
441:
442: if (magic_cookie != MAGIC_COOKIE)
443: return; /* already initialized */
444: magic_cookie = 0;
445:
446: for (index = 0; index < MAX_HWIFS; ++index)
447: init_hwif_data(index);
448:
449: idebus_parameter = 0;
450: system_bus_speed = 0;
451: }
452:
453: /*
454: * ide_system_bus_speed() returns what we think is the system VESA/PCI
455: * bus speed (in Mhz). This is used for calculating interface PIO timings.
456: * The default is 40 for known PCI systems, 50 otherwise.
457: * The "idebus=xx" parameter can be used to override this value.
458: * The actual value to be used is computed/displayed the first time through.
459: */
460: int ide_system_bus_speed (void)
461: {
462: if (!system_bus_speed) {
463: if (idebus_parameter)
464: system_bus_speed = idebus_parameter; /* user supplied value */
465: #ifdef CONFIG_PCI
466: else if (pcibios_present())
467: system_bus_speed = 40; /* safe default value for PCI */
468: #endif /* CONFIG_PCI */
469: else
470: system_bus_speed = 50; /* safe default value for VESA and PCI */
471: printk("ide: Assuming %dMhz system bus speed for PIO modes; override with idebus=xx\n", system_bus_speed);
472: }
473: return system_bus_speed;
474: }
475:
476: #if SUPPORT_VLB_SYNC
477: /*
478: * Some localbus EIDE interfaces require a special access sequence
479: * when using 32-bit I/O instructions to transfer data. We call this
480: * the "vlb_sync" sequence, which consists of three successive reads
481: * of the sector count register location, with interrupts disabled
482: * to ensure that the reads all happen together.
483: */
484: static inline void do_vlb_sync (unsigned short port) {
485: (void) inb (port);
486: (void) inb (port);
487: (void) inb (port);
488: }
489: #endif /* SUPPORT_VLB_SYNC */
490:
491: /*
492: * This is used for most PIO data transfers *from* the IDE interface
493: */
494: void ide_input_data (ide_drive_t *drive, void *buffer, unsigned int wcount)
495: {
496: unsigned short io_base = HWIF(drive)->io_base;
497: unsigned short data_reg = io_base+IDE_DATA_OFFSET;
498: byte io_32bit = drive->io_32bit;
499:
500: if (io_32bit) {
501: #if SUPPORT_VLB_SYNC
502: if (io_32bit & 2) {
503: cli();
504: do_vlb_sync(io_base+IDE_NSECTOR_OFFSET);
505: insl(data_reg, buffer, wcount);
506: if (drive->unmask)
507: sti();
508: } else
509: #endif /* SUPPORT_VLB_SYNC */
510: insl(data_reg, buffer, wcount);
511: } else {
512: #if SUPPORT_SLOW_DATA_PORTS
513: if (drive->slow) {
514: unsigned short *ptr = (unsigned short *) buffer;
515: while (wcount--) {
516: *ptr++ = inw_p(data_reg);
517: *ptr++ = inw_p(data_reg);
518: }
519: } else
520: #endif /* SUPPORT_SLOW_DATA_PORTS */
521: insw(data_reg, buffer, wcount<<1);
522: }
523: }
524:
525: /*
526: * This is used for most PIO data transfers *to* the IDE interface
527: */
528: void ide_output_data (ide_drive_t *drive, void *buffer, unsigned int wcount)
529: {
530: unsigned short io_base = HWIF(drive)->io_base;
531: unsigned short data_reg = io_base+IDE_DATA_OFFSET;
532: byte io_32bit = drive->io_32bit;
533:
534: if (io_32bit) {
535: #if SUPPORT_VLB_SYNC
536: if (io_32bit & 2) {
537: cli();
538: do_vlb_sync(io_base+IDE_NSECTOR_OFFSET);
539: outsl(data_reg, buffer, wcount);
540: if (drive->unmask)
541: sti();
542: } else
543: #endif /* SUPPORT_VLB_SYNC */
544: outsl(data_reg, buffer, wcount);
545: } else {
546: #if SUPPORT_SLOW_DATA_PORTS
547: if (drive->slow) {
548: unsigned short *ptr = (unsigned short *) buffer;
549: while (wcount--) {
550: outw_p(*ptr++, data_reg);
551: outw_p(*ptr++, data_reg);
552: }
553: } else
554: #endif /* SUPPORT_SLOW_DATA_PORTS */
555: outsw(data_reg, buffer, wcount<<1);
556: }
557: }
558:
559: /*
560: * The following routines are mainly used by the ATAPI drivers.
561: *
562: * These routines will round up any request for an odd number of bytes,
563: * so if an odd bytecount is specified, be sure that there's at least one
564: * extra byte allocated for the buffer.
565: */
566: void atapi_input_bytes (ide_drive_t *drive, void *buffer, unsigned int bytecount)
567: {
568: ++bytecount;
569: ide_input_data (drive, buffer, bytecount / 4);
570: if ((bytecount & 0x03) >= 2)
571: insw (IDE_DATA_REG, ((byte *)buffer) + (bytecount & ~0x03), 1);
572: }
573:
574: void atapi_output_bytes (ide_drive_t *drive, void *buffer, unsigned int bytecount)
575: {
576: ++bytecount;
577: ide_output_data (drive, buffer, bytecount / 4);
578: if ((bytecount & 0x03) >= 2)
579: outsw (IDE_DATA_REG, ((byte *)buffer) + (bytecount & ~0x03), 1);
580: }
581:
582: /*
583: * This should get invoked any time we exit the driver to
584: * wait for an interrupt response from a drive. handler() points
585: * at the appropriate code to handle the next interrupt, and a
586: * timer is started to prevent us from waiting forever in case
587: * something goes wrong (see the timer_expiry() handler later on).
588: */
589: void ide_set_handler (ide_drive_t *drive, ide_handler_t *handler, unsigned int timeout)
590: {
591: ide_hwgroup_t *hwgroup = HWGROUP(drive);
592: #ifdef DEBUG
593: if (hwgroup->handler != NULL) {
594: printk("%s: ide_set_handler: handler not null; old=%p, new=%p\n",
595: drive->name, hwgroup->handler, handler);
596: }
597: #endif
598: hwgroup->handler = handler;
599: hwgroup->timer.expires = jiffies + timeout;
600: add_timer(&(hwgroup->timer));
601: }
602:
603: /*
604: * lba_capacity_is_ok() performs a sanity check on the claimed "lba_capacity"
605: * value for this drive (from its reported identification information).
606: *
607: * Returns: 1 if lba_capacity looks sensible
608: * 0 otherwise
1.1.1.2 ! root 609: *
! 610: * Note: we must not change id->cyls here, otherwise a second call
! 611: * of this routine might no longer find lba_capacity ok.
1.1 root 612: */
613: static int lba_capacity_is_ok (struct hd_driveid *id)
614: {
615: unsigned long lba_sects = id->lba_capacity;
616: unsigned long chs_sects = id->cyls * id->heads * id->sectors;
617: unsigned long _10_percent = chs_sects / 10;
618:
1.1.1.2 ! root 619: /*
! 620: * The ATA spec tells large drives to return
! 621: * C/H/S = 16383/16/63 independent of their size.
! 622: * Some drives can be jumpered to use 15 heads instead of 16.
! 623: */
! 624: if (id->cyls == 16383 && id->sectors == 63 &&
! 625: (id->heads == 15 || id->heads == 16) &&
! 626: id->lba_capacity >= 16383*63*id->heads)
1.1 root 627: return 1; /* lba_capacity is our only option */
1.1.1.2 ! root 628:
1.1 root 629: /* perform a rough sanity check on lba_sects: within 10% is "okay" */
630: if ((lba_sects - chs_sects) < _10_percent)
631: return 1; /* lba_capacity is good */
632:
633: /* some drives have the word order reversed */
634: lba_sects = (lba_sects << 16) | (lba_sects >> 16);
635: if ((lba_sects - chs_sects) < _10_percent) {
636: id->lba_capacity = lba_sects; /* fix it */
637: return 1; /* lba_capacity is (now) good */
638: }
639: return 0; /* lba_capacity value is bad */
640: }
641:
642: /*
643: * current_capacity() returns the capacity (in sectors) of a drive
644: * according to its current geometry/LBA settings.
1.1.1.2 ! root 645: *
! 646: * It also sets select.b.lba.
1.1 root 647: */
648: static unsigned long current_capacity (ide_drive_t *drive)
649: {
650: struct hd_driveid *id = drive->id;
1.1.1.2 ! root 651: unsigned long capacity;
1.1 root 652:
653: if (!drive->present)
654: return 0;
655: #ifdef CONFIG_BLK_DEV_IDEFLOPPY
656: if (drive->media == ide_floppy)
657: return idefloppy_capacity(drive);
658: #endif /* CONFIG_BLK_DEV_IDEFLOPPY */
659: if (drive->media != ide_disk)
660: return 0x7fffffff; /* cdrom or tape */
1.1.1.2 ! root 661:
1.1 root 662: drive->select.b.lba = 0;
663: /* Determine capacity, and use LBA if the drive properly supports it */
1.1.1.2 ! root 664: capacity = drive->cyl * drive->head * drive->sect;
1.1 root 665: if (id != NULL && (id->capability & 2) && lba_capacity_is_ok(id)) {
666: if (id->lba_capacity >= capacity) {
667: capacity = id->lba_capacity;
668: drive->select.b.lba = 1;
669: }
670: }
671: return (capacity - drive->sect0);
672: }
673:
674: /*
675: * ide_geninit() is called exactly *once* for each major, from genhd.c,
676: * at the beginning of the initial partition check for the drives.
677: */
678: static void ide_geninit (struct gendisk *gd)
679: {
680: unsigned int unit;
681: ide_hwif_t *hwif = gd->real_devices;
682:
683: for (unit = 0; unit < gd->nr_real; ++unit) {
684: ide_drive_t *drive = &hwif->drives[unit];
685: #ifdef CONFIG_BLK_DEV_IDECD
686: if (drive->present && drive->media == ide_cdrom)
687: ide_cdrom_setup(drive);
688: #endif /* CONFIG_BLK_DEV_IDECD */
689: #ifdef CONFIG_BLK_DEV_IDETAPE
690: if (drive->present && drive->media == ide_tape)
691: idetape_setup(drive);
692: #endif /* CONFIG_BLK_DEV_IDETAPE */
693: #ifdef CONFIG_BLK_DEV_IDEFLOPPY
694: if (drive->present && drive->media == ide_floppy)
695: idefloppy_setup(drive);
696: #endif /* CONFIG_BLK_DEV_IDEFLOPPY */
697: drive->part[0].nr_sects = current_capacity(drive);
698: if (!drive->present || (drive->media != ide_disk && drive->media != ide_floppy) ||
699: !drive->part[0].nr_sects) {
700: drive->part[0].start_sect = -1; /* skip partition check */
701: }
702: }
703: }
704:
705: /*
706: * init_gendisk() (as opposed to ide_geninit) is called for each major device,
707: * after probing for drives, to allocate partition tables and other data
708: * structures needed for the routines in genhd.c. ide_geninit() gets called
709: * somewhat later, during the partition check.
710: */
711: static void init_gendisk (ide_hwif_t *hwif)
712: {
713: struct gendisk *gd, **gdp;
714: unsigned int unit, units, minors;
715: int *bs;
716:
717: /* figure out maximum drive number on the interface */
718: for (units = MAX_DRIVES; units > 0; --units) {
719: if (hwif->drives[units-1].present)
720: break;
721: }
722: minors = units * (1<<PARTN_BITS);
723: gd = kmalloc (sizeof(struct gendisk), GFP_KERNEL);
724: gd->sizes = kmalloc (minors * sizeof(int), GFP_KERNEL);
725: gd->part = kmalloc (minors * sizeof(struct hd_struct), GFP_KERNEL);
726: bs = kmalloc (minors*sizeof(int), GFP_KERNEL);
727:
728: memset(gd->part, 0, minors * sizeof(struct hd_struct));
729:
730: /* cdroms and msdos f/s are examples of non-1024 blocksizes */
731: blksize_size[hwif->major] = bs;
732: for (unit = 0; unit < minors; ++unit)
733: *bs++ = BLOCK_SIZE;
734:
735: for (unit = 0; unit < units; ++unit)
736: hwif->drives[unit].part = &gd->part[unit << PARTN_BITS];
737:
738: gd->major = hwif->major; /* our major device number */
739: gd->major_name = IDE_MAJOR_NAME; /* treated special in genhd.c */
740: gd->minor_shift = PARTN_BITS; /* num bits for partitions */
741: gd->max_p = 1<<PARTN_BITS; /* 1 + max partitions / drive */
742: gd->max_nr = units; /* max num real drives */
743: gd->nr_real = units; /* current num real drives */
744: gd->init = ide_geninit; /* initialization function */
745: gd->real_devices= hwif; /* ptr to internal data */
746: gd->next = NULL; /* linked list of major devs */
747:
748: for (gdp = &gendisk_head; *gdp; gdp = &((*gdp)->next)) ;
749: hwif->gd = *gdp = gd; /* link onto tail of list */
750: }
751:
752: static void do_reset1 (ide_drive_t *, int); /* needed below */
753:
754: #ifdef CONFIG_BLK_DEV_IDEATAPI
755: /*
756: * atapi_reset_pollfunc() gets invoked to poll the interface for completion every 50ms
757: * during an atapi drive reset operation. If the drive has not yet responded,
758: * and we have not yet hit our maximum waiting time, then the timer is restarted
759: * for another 50ms.
760: */
761: static void atapi_reset_pollfunc (ide_drive_t *drive)
762: {
763: ide_hwgroup_t *hwgroup = HWGROUP(drive);
764: byte stat;
765:
766: OUT_BYTE (drive->select.all, IDE_SELECT_REG);
767: udelay (10);
768:
769: if (OK_STAT(stat=GET_STAT(), 0, BUSY_STAT)) {
770: printk("%s: ATAPI reset complete\n", drive->name);
771: } else {
772: if (jiffies < hwgroup->poll_timeout) {
773: ide_set_handler (drive, &atapi_reset_pollfunc, HZ/20);
774: return; /* continue polling */
775: }
776: hwgroup->poll_timeout = 0; /* end of polling */
777: printk("%s: ATAPI reset timed-out, status=0x%02x\n", drive->name, stat);
778: do_reset1 (drive, 1); /* do it the old fashioned way */
779: return;
780: }
781: hwgroup->poll_timeout = 0; /* done polling */
782: }
783: #endif /* CONFIG_BLK_DEV_IDEATAPI */
784:
785: /*
786: * reset_pollfunc() gets invoked to poll the interface for completion every 50ms
787: * during an ide reset operation. If the drives have not yet responded,
788: * and we have not yet hit our maximum waiting time, then the timer is restarted
789: * for another 50ms.
790: */
791: static void reset_pollfunc (ide_drive_t *drive)
792: {
793: ide_hwgroup_t *hwgroup = HWGROUP(drive);
794: ide_hwif_t *hwif = HWIF(drive);
795: byte tmp;
796:
797: if (!OK_STAT(tmp=GET_STAT(), 0, BUSY_STAT)) {
798: if (jiffies < hwgroup->poll_timeout) {
799: ide_set_handler (drive, &reset_pollfunc, HZ/20);
800: return; /* continue polling */
801: }
802: printk("%s: reset timed-out, status=0x%02x\n", hwif->name, tmp);
803: } else {
804: printk("%s: reset: ", hwif->name);
805: if ((tmp = GET_ERR()) == 1)
806: printk("success\n");
807: else {
808: #if FANCY_STATUS_DUMPS
809: printk("master: ");
810: switch (tmp & 0x7f) {
811: case 1: printk("passed");
812: break;
813: case 2: printk("formatter device error");
814: break;
815: case 3: printk("sector buffer error");
816: break;
817: case 4: printk("ECC circuitry error");
818: break;
819: case 5: printk("controlling MPU error");
820: break;
821: default:printk("error (0x%02x?)", tmp);
822: }
823: if (tmp & 0x80)
824: printk("; slave: failed");
825: printk("\n");
826: #else
827: printk("failed\n");
828: #endif /* FANCY_STATUS_DUMPS */
829: }
830: }
831: hwgroup->poll_timeout = 0; /* done polling */
832: }
833:
834: /*
835: * do_reset1() attempts to recover a confused drive by resetting it.
836: * Unfortunately, resetting a disk drive actually resets all devices on
837: * the same interface, so it can really be thought of as resetting the
838: * interface rather than resetting the drive.
839: *
840: * ATAPI devices have their own reset mechanism which allows them to be
841: * individually reset without clobbering other devices on the same interface.
842: *
843: * Unfortunately, the IDE interface does not generate an interrupt to let
844: * us know when the reset operation has finished, so we must poll for this.
845: * Equally poor, though, is the fact that this may a very long time to complete,
846: * (up to 30 seconds worstcase). So, instead of busy-waiting here for it,
847: * we set a timer to poll at 50ms intervals.
848: */
849: static void do_reset1 (ide_drive_t *drive, int do_not_try_atapi)
850: {
851: unsigned int unit;
852: unsigned long flags;
853: ide_hwif_t *hwif = HWIF(drive);
854: ide_hwgroup_t *hwgroup = HWGROUP(drive);
855:
856: save_flags(flags);
857: cli(); /* Why ? */
858:
859: #ifdef CONFIG_BLK_DEV_IDEATAPI
860: /* For an ATAPI device, first try an ATAPI SRST. */
861: if (drive->media != ide_disk) {
862: if (!do_not_try_atapi) {
863: if (!drive->keep_settings) {
864: drive->unmask = 0;
865: drive->io_32bit = 0;
866: }
867: OUT_BYTE (drive->select.all, IDE_SELECT_REG);
868: udelay (20);
869: OUT_BYTE (WIN_SRST, IDE_COMMAND_REG);
870: hwgroup->poll_timeout = jiffies + WAIT_WORSTCASE;
871: ide_set_handler (drive, &atapi_reset_pollfunc, HZ/20);
872: restore_flags (flags);
873: return;
874: }
875: }
876: #endif /* CONFIG_BLK_DEV_IDEATAPI */
877:
878: /*
879: * First, reset any device state data we were maintaining
880: * for any of the drives on this interface.
881: */
882: for (unit = 0; unit < MAX_DRIVES; ++unit) {
883: ide_drive_t *rdrive = &hwif->drives[unit];
884: #ifdef CONFIG_BLK_DEV_IDETAPE
885: if (rdrive->media == ide_tape)
886: rdrive->tape.reset_issued = 1;
887: #endif /* CONFIG_BLK_DEV_IDETAPE */
888: rdrive->special.all = 0;
889: rdrive->special.b.set_geometry = 1;
890: rdrive->special.b.recalibrate = 1;
891: if (OK_TO_RESET_CONTROLLER)
892: rdrive->mult_count = 0;
893: if (!rdrive->keep_settings) {
894: rdrive->mult_req = 0;
895: rdrive->unmask = 0;
896: rdrive->io_32bit = 0;
897: if (rdrive->using_dma) {
898: rdrive->using_dma = 0;
899: printk("%s: disabled DMA\n", rdrive->name);
900: }
901: }
902: if (rdrive->mult_req != rdrive->mult_count)
903: rdrive->special.b.set_multmode = 1;
904: }
905:
906: #if OK_TO_RESET_CONTROLLER
907: /*
908: * Note that we also set nIEN while resetting the device,
909: * to mask unwanted interrupts from the interface during the reset.
910: * However, due to the design of PC hardware, this will cause an
911: * immediate interrupt due to the edge transition it produces.
912: * This single interrupt gives us a "fast poll" for drives that
913: * recover from reset very quickly, saving us the first 50ms wait time.
914: */
915: OUT_BYTE(drive->ctl|6,IDE_CONTROL_REG); /* set SRST and nIEN */
916: udelay(10); /* more than enough time */
917: OUT_BYTE(drive->ctl|2,IDE_CONTROL_REG); /* clear SRST, leave nIEN */
918: udelay(10); /* more than enough time */
919: hwgroup->poll_timeout = jiffies + WAIT_WORSTCASE;
920: ide_set_handler (drive, &reset_pollfunc, HZ/20);
921: #endif /* OK_TO_RESET_CONTROLLER */
922:
923: restore_flags (flags);
924: }
925:
926: /*
927: * ide_do_reset() is the entry point to the drive/interface reset code.
928: */
929: void ide_do_reset (ide_drive_t *drive)
930: {
931: do_reset1 (drive, 0);
932: #ifdef CONFIG_BLK_DEV_IDETAPE
933: if (drive->media == ide_tape)
934: drive->tape.reset_issued=1;
935: #endif /* CONFIG_BLK_DEV_IDETAPE */
936: }
937:
938: /*
939: * Clean up after success/failure of an explicit drive cmd
940: */
941: void ide_end_drive_cmd (ide_drive_t *drive, byte stat, byte err)
942: {
943: unsigned long flags;
944: struct request *rq = HWGROUP(drive)->rq;
945:
946: if (rq->cmd == IDE_DRIVE_CMD) {
947: byte *args = (byte *) rq->buffer;
948: rq->errors = !OK_STAT(stat,READY_STAT,BAD_STAT);
949: if (args) {
950: args[0] = stat;
951: args[1] = err;
952: args[2] = IN_BYTE(IDE_NSECTOR_REG);
953: }
954: }
955: save_flags(flags);
956: cli();
957: blk_dev[MAJOR(rq->rq_dev)].current_request = rq->next;
958: HWGROUP(drive)->rq = NULL;
959: rq->rq_status = RQ_INACTIVE;
960: if (rq->sem != NULL)
961: up(rq->sem);
962: restore_flags(flags);
963: }
964:
965: /*
966: * Error reporting, in human readable form (luxurious, but a memory hog).
967: */
968: byte ide_dump_status (ide_drive_t *drive, const char *msg, byte stat)
969: {
970: unsigned long flags;
971: byte err = 0;
972:
973: save_flags (flags);
974: sti();
975: printk("%s: %s: status=0x%02x", drive->name, msg, stat);
976: #if FANCY_STATUS_DUMPS
977: if (drive->media == ide_disk) {
978: printk(" { ");
979: if (stat & BUSY_STAT)
980: printk("Busy ");
981: else {
982: if (stat & READY_STAT) printk("DriveReady ");
983: if (stat & WRERR_STAT) printk("DeviceFault ");
984: if (stat & SEEK_STAT) printk("SeekComplete ");
985: if (stat & DRQ_STAT) printk("DataRequest ");
986: if (stat & ECC_STAT) printk("CorrectedError ");
987: if (stat & INDEX_STAT) printk("Index ");
988: if (stat & ERR_STAT) printk("Error ");
989: }
990: printk("}");
991: }
992: #endif /* FANCY_STATUS_DUMPS */
993: printk("\n");
994: if ((stat & (BUSY_STAT|ERR_STAT)) == ERR_STAT) {
995: err = GET_ERR();
996: printk("%s: %s: error=0x%02x", drive->name, msg, err);
997: #if FANCY_STATUS_DUMPS
998: if (drive->media == ide_disk) {
999: printk(" { ");
1000: if (err & ICRC_ERR) printk((err & ABRT_ERR) ? "BadCRC " : "BadSector ");
1001: if (err & ECC_ERR) printk("UncorrectableError ");
1002: if (err & ID_ERR) printk("SectorIdNotFound ");
1003: if (err & ABRT_ERR) printk("DriveStatusError ");
1004: if (err & TRK0_ERR) printk("TrackZeroNotFound ");
1005: if (err & MARK_ERR) printk("AddrMarkNotFound ");
1006: printk("}");
1007: if (err & (BBD_ERR|ECC_ERR|ID_ERR|MARK_ERR)) {
1008: byte cur = IN_BYTE(IDE_SELECT_REG);
1009: if (cur & 0x40) { /* using LBA? */
1010: printk(", LBAsect=%ld", (unsigned long)
1011: ((cur&0xf)<<24)
1012: |(IN_BYTE(IDE_HCYL_REG)<<16)
1013: |(IN_BYTE(IDE_LCYL_REG)<<8)
1014: | IN_BYTE(IDE_SECTOR_REG));
1015: } else {
1016: printk(", CHS=%d/%d/%d",
1017: (IN_BYTE(IDE_HCYL_REG)<<8) +
1018: IN_BYTE(IDE_LCYL_REG),
1019: cur & 0xf,
1020: IN_BYTE(IDE_SECTOR_REG));
1021: }
1022: if (HWGROUP(drive)->rq)
1023: printk(", sector=%ld", HWGROUP(drive)->rq->sector);
1024: }
1025: }
1026: #endif /* FANCY_STATUS_DUMPS */
1027: printk("\n");
1028: }
1029: restore_flags (flags);
1030: return err;
1031: }
1032:
1033: /*
1034: * try_to_flush_leftover_data() is invoked in response to a drive
1035: * unexpectedly having its DRQ_STAT bit set. As an alternative to
1036: * resetting the drive, this routine tries to clear the condition
1037: * by read a sector's worth of data from the drive. Of course,
1038: * this may not help if the drive is *waiting* for data from *us*.
1039: */
1040: static void try_to_flush_leftover_data (ide_drive_t *drive)
1041: {
1042: int i = (drive->mult_count ? drive->mult_count : 1) * SECTOR_WORDS;
1043:
1044: while (i > 0) {
1045: unsigned long buffer[16];
1046: unsigned int wcount = (i > 16) ? 16 : i;
1047: i -= wcount;
1048: ide_input_data (drive, buffer, wcount);
1049: }
1050: }
1051:
1052: /*
1053: * ide_error() takes action based on the error returned by the controller.
1054: */
1055: void ide_error (ide_drive_t *drive, const char *msg, byte stat)
1056: {
1057: struct request *rq;
1058: byte err;
1059:
1060: err = ide_dump_status(drive, msg, stat);
1061: if ((rq = HWGROUP(drive)->rq) == NULL || drive == NULL)
1062: return;
1063: /* retry only "normal" I/O: */
1064: if (rq->cmd == IDE_DRIVE_CMD) {
1065: rq->errors = 1;
1066: ide_end_drive_cmd(drive, stat, err);
1067: return;
1068: }
1069: if (stat & BUSY_STAT) { /* other bits are useless when BUSY */
1070: rq->errors |= ERROR_RESET;
1071: } else {
1072: if (drive->media == ide_disk && (stat & ERR_STAT)) {
1073: /* err has different meaning on cdrom and tape */
1074: if (err == ABRT_ERR) {
1075: if (drive->select.b.lba && IN_BYTE(IDE_COMMAND_REG) == WIN_SPECIFY)
1076: return; /* some newer drives don't support WIN_SPECIFY */
1077: } else if ((err & (ABRT_ERR | ICRC_ERR)) == (ABRT_ERR | ICRC_ERR))
1078: ; /* UDMA crc error -- just retry the operation */
1079: else if (err & (BBD_ERR | ECC_ERR)) /* retries won't help these */
1080: rq->errors = ERROR_MAX;
1081: else if (err & TRK0_ERR) /* help it find track zero */
1082: rq->errors |= ERROR_RECAL;
1083: else if (err & MC_ERR)
1084: drive->special.b.mc = 1;
1085: }
1086: if ((stat & DRQ_STAT) && rq->cmd != WRITE)
1087: try_to_flush_leftover_data(drive);
1088: }
1089: if (GET_STAT() & (BUSY_STAT|DRQ_STAT))
1090: rq->errors |= ERROR_RESET; /* Mmmm.. timing problem */
1091:
1092: if (rq->errors >= ERROR_MAX) {
1093: #ifdef CONFIG_BLK_DEV_IDETAPE
1094: if (drive->media == ide_tape) {
1095: rq->errors = 0;
1096: idetape_end_request(0, HWGROUP(drive));
1097: } else
1098: #endif /* CONFIG_BLK_DEV_IDETAPE */
1099: #ifdef CONFIG_BLK_DEV_IDEFLOPPY
1100: if (drive->media == ide_floppy) {
1101: rq->errors = 0;
1102: idefloppy_end_request(0, HWGROUP(drive));
1103: } else
1104: #endif /* CONFIG_BLK_DEV_IDEFLOPPY */
1105: #ifdef CONFIG_BLK_DEV_IDESCSI
1106: if (drive->media == ide_scsi) {
1107: rq->errors = 0;
1108: idescsi_end_request(0, HWGROUP(drive));
1109: } else
1110: #endif /* CONFIG_BLK_DEV_IDESCSI */
1111: ide_end_request(0, HWGROUP(drive));
1112: }
1113: else {
1114: if ((rq->errors & ERROR_RESET) == ERROR_RESET) {
1115: ++rq->errors;
1116: ide_do_reset(drive);
1117: return;
1118: } else if ((rq->errors & ERROR_RECAL) == ERROR_RECAL)
1119: drive->special.b.recalibrate = 1;
1120: ++rq->errors;
1121: }
1122: }
1123:
1124: /*
1125: * read_intr() is the handler for disk read/multread interrupts
1126: */
1127: static void read_intr (ide_drive_t *drive)
1128: {
1129: byte stat;
1130: int i;
1131: unsigned int msect, nsect;
1132: struct request *rq;
1133:
1134: if (!OK_STAT(stat=GET_STAT(),DATA_READY,BAD_R_STAT)) {
1135: ide_error(drive, "read_intr", stat);
1136: return;
1137: }
1138: msect = drive->mult_count;
1139: read_next:
1140: rq = HWGROUP(drive)->rq;
1141: if (msect) {
1142: if ((nsect = rq->current_nr_sectors) > msect)
1143: nsect = msect;
1144: msect -= nsect;
1145: } else
1146: nsect = 1;
1.1.1.2 ! root 1147: i = rq->nr_sectors - nsect;
! 1148: if (i > 0 && !msect)
! 1149: ide_set_handler (drive, &read_intr, WAIT_CMD);
1.1 root 1150: ide_input_data(drive, rq->buffer, nsect * SECTOR_WORDS);
1151: #ifdef DEBUG
1152: printk("%s: read: sectors(%ld-%ld), buffer=0x%08lx, remaining=%ld\n",
1153: drive->name, rq->sector, rq->sector+nsect-1,
1154: (unsigned long) rq->buffer+(nsect<<9), rq->nr_sectors-nsect);
1155: #endif
1156: rq->sector += nsect;
1157: rq->buffer += nsect<<9;
1158: rq->errors = 0;
1.1.1.2 ! root 1159: rq->nr_sectors = i;
1.1 root 1160: if ((rq->current_nr_sectors -= nsect) <= 0)
1161: ide_end_request(1, HWGROUP(drive));
1.1.1.2 ! root 1162: if (i > 0 && msect)
! 1163: goto read_next;
1.1 root 1164: }
1165:
1166: /*
1167: * write_intr() is the handler for disk write interrupts
1168: */
1169: static void write_intr (ide_drive_t *drive)
1170: {
1171: byte stat;
1172: int i;
1173: ide_hwgroup_t *hwgroup = HWGROUP(drive);
1174: struct request *rq = hwgroup->rq;
1175:
1176: if (OK_STAT(stat=GET_STAT(),DRIVE_READY,drive->bad_wstat)) {
1177: #ifdef DEBUG
1178: printk("%s: write: sector %ld, buffer=0x%08lx, remaining=%ld\n",
1179: drive->name, rq->sector, (unsigned long) rq->buffer,
1180: rq->nr_sectors-1);
1181: #endif
1182: if ((rq->nr_sectors == 1) ^ ((stat & DRQ_STAT) != 0)) {
1183: rq->sector++;
1184: rq->buffer += 512;
1185: rq->errors = 0;
1186: i = --rq->nr_sectors;
1187: --rq->current_nr_sectors;
1188: if (rq->current_nr_sectors <= 0)
1189: ide_end_request(1, hwgroup);
1190: if (i > 0) {
1191: ide_set_handler (drive, &write_intr, WAIT_CMD);
1.1.1.2 ! root 1192: ide_output_data (drive, rq->buffer, SECTOR_WORDS);
1.1 root 1193: }
1194: return;
1195: }
1196: }
1197: ide_error(drive, "write_intr", stat);
1198: }
1199:
1200: /*
1201: * ide_multwrite() transfers a block of up to mcount sectors of data
1202: * to a drive as part of a disk multiple-sector write operation.
1203: */
1204: void ide_multwrite (ide_drive_t *drive, unsigned int mcount)
1205: {
1206: struct request *rq = &HWGROUP(drive)->wrq;
1207:
1208: do {
1209: unsigned int nsect = rq->current_nr_sectors;
1210: if (nsect > mcount)
1211: nsect = mcount;
1212: mcount -= nsect;
1213:
1214: ide_output_data(drive, rq->buffer, nsect<<7);
1215: #ifdef DEBUG
1216: printk("%s: multwrite: sector %ld, buffer=0x%08lx, count=%d, remaining=%ld\n",
1217: drive->name, rq->sector, (unsigned long) rq->buffer,
1218: nsect, rq->nr_sectors - nsect);
1219: #endif
1220: if ((rq->nr_sectors -= nsect) <= 0)
1221: break;
1222: if ((rq->current_nr_sectors -= nsect) == 0) {
1223: if ((rq->bh = rq->bh->b_reqnext) != NULL) {
1224: rq->current_nr_sectors = rq->bh->b_size>>9;
1225: rq->buffer = rq->bh->b_data;
1226: } else {
1227: panic("%s: buffer list corrupted\n", drive->name);
1228: break;
1229: }
1230: } else {
1231: rq->buffer += nsect << 9;
1232: }
1233: } while (mcount);
1234: }
1235:
1236: /*
1237: * multwrite_intr() is the handler for disk multwrite interrupts
1238: */
1239: static void multwrite_intr (ide_drive_t *drive)
1240: {
1241: byte stat;
1242: int i;
1243: ide_hwgroup_t *hwgroup = HWGROUP(drive);
1244: struct request *rq = &hwgroup->wrq;
1245:
1246: if (OK_STAT(stat=GET_STAT(),DRIVE_READY,drive->bad_wstat)) {
1247: if (stat & DRQ_STAT) {
1248: if (rq->nr_sectors) {
1249: ide_set_handler (drive, &multwrite_intr, WAIT_CMD);
1.1.1.2 ! root 1250: ide_multwrite(drive, drive->mult_count);
1.1 root 1251: return;
1252: }
1253: } else {
1254: if (!rq->nr_sectors) { /* all done? */
1255: rq = hwgroup->rq;
1256: for (i = rq->nr_sectors; i > 0;){
1257: i -= rq->current_nr_sectors;
1258: ide_end_request(1, hwgroup);
1259: }
1260: return;
1261: }
1262: }
1263: }
1264: ide_error(drive, "multwrite_intr", stat);
1265: }
1266:
1267: /*
1268: * Issue a simple drive command
1269: * The drive must be selected beforehand.
1270: */
1271: static void ide_cmd(ide_drive_t *drive, byte cmd, byte nsect, ide_handler_t *handler)
1272: {
1273: ide_set_handler (drive, handler, WAIT_CMD);
1274: OUT_BYTE(drive->ctl,IDE_CONTROL_REG);
1275: OUT_BYTE(nsect,IDE_NSECTOR_REG);
1276: OUT_BYTE(cmd,IDE_COMMAND_REG);
1277: }
1278:
1279: /*
1280: * set_multmode_intr() is invoked on completion of a WIN_SETMULT cmd.
1281: */
1282: static void set_multmode_intr (ide_drive_t *drive)
1283: {
1284: byte stat = GET_STAT();
1285:
1286: sti();
1287: if (OK_STAT(stat,READY_STAT,BAD_STAT)) {
1288: drive->mult_count = drive->mult_req;
1289: } else {
1290: drive->mult_req = drive->mult_count = 0;
1291: drive->special.b.recalibrate = 1;
1292: (void) ide_dump_status(drive, "set_multmode", stat);
1293: }
1294: }
1295:
1296: /*
1297: * set_geometry_intr() is invoked on completion of a WIN_SPECIFY cmd.
1298: */
1299: static void set_geometry_intr (ide_drive_t *drive)
1300: {
1301: byte stat = GET_STAT();
1302:
1303: sti();
1304: if (!OK_STAT(stat,READY_STAT,BAD_STAT))
1305: ide_error(drive, "set_geometry_intr", stat);
1306: }
1307:
1308: /*
1309: * recal_intr() is invoked on completion of a WIN_RESTORE (recalibrate) cmd.
1310: */
1311: static void recal_intr (ide_drive_t *drive)
1312: {
1313: byte stat = GET_STAT();
1314:
1315: sti();
1316: if (!OK_STAT(stat,READY_STAT,BAD_STAT))
1317: ide_error(drive, "recal_intr", stat);
1318: }
1319:
1320: /*
1321: * mc_intr() is invoked on completion of a WIN_ACKMC cmd.
1322: */
1323: static void mc_intr (ide_drive_t *drive)
1324: {
1325: byte stat = GET_STAT();
1326:
1327: sti();
1328: if (!OK_STAT(stat,READY_STAT,BAD_STAT))
1329: ide_error(drive, "mc_intr", stat);
1330: drive->special.b.mc = 0;
1331: }
1332:
1333: /*
1334: * drive_cmd_intr() is invoked on completion of a special DRIVE_CMD.
1335: */
1336: static void drive_cmd_intr (ide_drive_t *drive)
1337: {
1338: struct request *rq = HWGROUP(drive)->rq;
1339: byte *args = (byte *) rq->buffer;
1340: byte stat = GET_STAT();
1341:
1342: sti();
1343: if ((stat & DRQ_STAT) && args && args[3]) {
1344: byte io_32bit = drive->io_32bit;
1345: drive->io_32bit = 0;
1346: ide_input_data(drive, &args[4], args[3] * SECTOR_WORDS);
1347: drive->io_32bit = io_32bit;
1348: stat = GET_STAT();
1349: }
1350: if (OK_STAT(stat,READY_STAT,BAD_STAT))
1351: ide_end_drive_cmd (drive, stat, GET_ERR());
1352: else
1353: ide_error(drive, "drive_cmd", stat); /* calls ide_end_drive_cmd */
1354: }
1355:
1356: /*
1357: * do_special() is used to issue WIN_SPECIFY, WIN_RESTORE, and WIN_SETMULT
1358: * commands to a drive. It used to do much more, but has been scaled back.
1359: */
1360: static inline void do_special (ide_drive_t *drive)
1361: {
1362: special_t *s = &drive->special;
1363:
1364: #ifdef DEBUG
1365: printk("%s: do_special: 0x%02x\n", drive->name, s->all);
1366: #endif
1367: if (s->b.set_geometry) {
1368: s->b.set_geometry = 0;
1369: if (drive->media == ide_disk && !drive->no_geom) {
1370: OUT_BYTE(drive->sect,IDE_SECTOR_REG);
1371: OUT_BYTE(drive->cyl,IDE_LCYL_REG);
1372: OUT_BYTE(drive->cyl>>8,IDE_HCYL_REG);
1373: OUT_BYTE(((drive->head-1)|drive->select.all)&0xBF,IDE_SELECT_REG);
1374: if (!IS_PROMISE_DRIVE)
1375: ide_cmd(drive, WIN_SPECIFY, drive->sect, &set_geometry_intr);
1376: }
1377: } else if (s->b.recalibrate) {
1378: s->b.recalibrate = 0;
1379: if (drive->media == ide_disk && !IS_PROMISE_DRIVE)
1380: ide_cmd(drive, WIN_RESTORE, drive->sect, &recal_intr);
1381: } else if (s->b.set_tune) {
1382: ide_tuneproc_t *tuneproc = HWIF(drive)->tuneproc;
1383: s->b.set_tune = 0;
1384: if (tuneproc != NULL)
1385: tuneproc(drive, drive->tune_req);
1386: } else if (s->b.set_multmode) {
1387: s->b.set_multmode = 0;
1388: if (drive->media == ide_disk) {
1389: if (drive->id && drive->mult_req > drive->id->max_multsect)
1390: drive->mult_req = drive->id->max_multsect;
1391: if (!IS_PROMISE_DRIVE)
1392: ide_cmd(drive, WIN_SETMULT, drive->mult_req, &set_multmode_intr);
1393: } else
1394: drive->mult_req = 0;
1395: } else if (s->b.mc) {
1396: s->b.mc = 0;
1397: if (drive->media == ide_disk && !IS_PROMISE_DRIVE)
1398: ide_cmd(drive, WIN_ACKMC, drive->sect, &mc_intr);
1399: } else if (s->all) {
1400: int special = s->all;
1401: s->all = 0;
1402: printk("%s: bad special flag: 0x%02x\n", drive->name, special);
1403: }
1404: }
1405:
1406: /*
1407: * This routine busy-waits for the drive status to be not "busy".
1408: * It then checks the status for all of the "good" bits and none
1409: * of the "bad" bits, and if all is okay it returns 0. All other
1410: * cases return 1 after invoking ide_error() -- caller should just return.
1411: *
1412: * This routine should get fixed to not hog the cpu during extra long waits..
1413: * That could be done by busy-waiting for the first jiffy or two, and then
1414: * setting a timer to wake up at half second intervals thereafter,
1415: * until timeout is achieved, before timing out.
1416: */
1417: int ide_wait_stat (ide_drive_t *drive, byte good, byte bad, unsigned long timeout)
1418: {
1419: byte stat;
1420: unsigned long flags;
1421:
1422: udelay(1); /* spec allows drive 400ns to assert "BUSY" */
1423: if ((stat = GET_STAT()) & BUSY_STAT) {
1424: save_flags(flags);
1425: sti();
1426: timeout += jiffies;
1427: while ((stat = GET_STAT()) & BUSY_STAT) {
1428: if (jiffies > timeout) {
1429: restore_flags(flags);
1430: ide_error(drive, "status timeout", stat);
1431: return 1;
1432: }
1433: }
1434: restore_flags(flags);
1435: }
1436: udelay(1); /* allow status to settle, then read it again */
1437: if (OK_STAT((stat = GET_STAT()), good, bad))
1438: return 0;
1439: ide_error(drive, "status error", stat);
1440: return 1;
1441: }
1442:
1443: /*
1444: * do_rw_disk() issues READ and WRITE commands to a disk,
1445: * using LBA if supported, or CHS otherwise, to address sectors.
1446: * It also takes care of issuing special DRIVE_CMDs.
1447: */
1448: static inline void do_rw_disk (ide_drive_t *drive, struct request *rq, unsigned long block)
1449: {
1450: ide_hwif_t *hwif = HWIF(drive);
1451: unsigned short io_base = hwif->io_base;
1452: #ifdef CONFIG_BLK_DEV_PROMISE
1453: int use_promise_io = 0;
1454: #endif /* CONFIG_BLK_DEV_PROMISE */
1455:
1456: OUT_BYTE(drive->ctl,IDE_CONTROL_REG);
1457: OUT_BYTE(rq->nr_sectors,io_base+IDE_NSECTOR_OFFSET);
1458: #ifdef CONFIG_BLK_DEV_PROMISE
1459: if (IS_PROMISE_DRIVE) {
1460: if (hwif->is_promise2 || rq->cmd == READ) {
1461: use_promise_io = 1;
1462: }
1463: }
1464: if (drive->select.b.lba || use_promise_io) {
1465: #else /* !CONFIG_BLK_DEV_PROMISE */
1466: if (drive->select.b.lba) {
1467: #endif /* CONFIG_BLK_DEV_PROMISE */
1468: #ifdef DEBUG
1469: printk("%s: %sing: LBAsect=%ld, sectors=%ld, buffer=0x%08lx\n",
1470: drive->name, (rq->cmd==READ)?"read":"writ",
1471: block, rq->nr_sectors, (unsigned long) rq->buffer);
1472: #endif
1473: OUT_BYTE(block,io_base+IDE_SECTOR_OFFSET);
1474: OUT_BYTE(block>>=8,io_base+IDE_LCYL_OFFSET);
1475: OUT_BYTE(block>>=8,io_base+IDE_HCYL_OFFSET);
1476: OUT_BYTE(((block>>8)&0x0f)|drive->select.all,io_base+IDE_SELECT_OFFSET);
1477: } else {
1478: unsigned int sect,head,cyl,track;
1479: track = block / drive->sect;
1480: sect = block % drive->sect + 1;
1481: OUT_BYTE(sect,io_base+IDE_SECTOR_OFFSET);
1482: head = track % drive->head;
1483: cyl = track / drive->head;
1484: OUT_BYTE(cyl,io_base+IDE_LCYL_OFFSET);
1485: OUT_BYTE(cyl>>8,io_base+IDE_HCYL_OFFSET);
1486: OUT_BYTE(head|drive->select.all,io_base+IDE_SELECT_OFFSET);
1487: #ifdef DEBUG
1488: printk("%s: %sing: CHS=%d/%d/%d, sectors=%ld, buffer=0x%08lx\n",
1489: drive->name, (rq->cmd==READ)?"read":"writ", cyl,
1490: head, sect, rq->nr_sectors, (unsigned long) rq->buffer);
1491: #endif
1492: }
1493: #ifdef CONFIG_BLK_DEV_PROMISE
1494: if (use_promise_io) {
1495: do_promise_io (drive, rq);
1496: return;
1497: }
1498: #endif /* CONFIG_BLK_DEV_PROMISE */
1499: if (rq->cmd == READ) {
1500: #ifdef CONFIG_BLK_DEV_TRITON
1501: if (drive->using_dma && !(HWIF(drive)->dmaproc(ide_dma_read, drive)))
1502: return;
1503: #endif /* CONFIG_BLK_DEV_TRITON */
1504: ide_set_handler(drive, &read_intr, WAIT_CMD);
1505: OUT_BYTE(drive->mult_count ? WIN_MULTREAD : WIN_READ, io_base+IDE_COMMAND_OFFSET);
1506: return;
1507: }
1508: if (rq->cmd == WRITE) {
1509: #ifdef CONFIG_BLK_DEV_TRITON
1510: if (drive->using_dma && !(HWIF(drive)->dmaproc(ide_dma_write, drive)))
1511: return;
1512: #endif /* CONFIG_BLK_DEV_TRITON */
1.1.1.2 ! root 1513: if (drive->mult_count)
! 1514: ide_set_handler (drive, &multwrite_intr, WAIT_CMD);
! 1515: else
! 1516: ide_set_handler (drive, &write_intr, WAIT_CMD);
1.1 root 1517: OUT_BYTE(drive->mult_count ? WIN_MULTWRITE : WIN_WRITE, io_base+IDE_COMMAND_OFFSET);
1518: if (ide_wait_stat(drive, DATA_READY, drive->bad_wstat, WAIT_DRQ)) {
1519: printk("%s: no DRQ after issuing %s\n", drive->name,
1520: drive->mult_count ? "MULTWRITE" : "WRITE");
1521: return;
1522: }
1523: if (!drive->unmask)
1524: cli();
1525: if (drive->mult_count) {
1526: HWGROUP(drive)->wrq = *rq; /* scratchpad */
1527: ide_multwrite(drive, drive->mult_count);
1528: } else {
1529: ide_output_data(drive, rq->buffer, SECTOR_WORDS);
1530: }
1531: return;
1532: }
1533: printk("%s: bad command: %d\n", drive->name, rq->cmd);
1534: ide_end_request(0, HWGROUP(drive));
1535: }
1536:
1537: /*
1538: * execute_drive_cmd() issues a special drive command,
1539: * usually initiated by ioctl() from the external hdparm program.
1540: */
1541: static void execute_drive_cmd (ide_drive_t *drive, struct request *rq)
1542: {
1.1.1.2 ! root 1543: byte *args = (byte *)rq->buffer;
1.1 root 1544: if (args) {
1545: #ifdef DEBUG
1546: printk("%s: DRIVE_CMD cmd=0x%02x sc=0x%02x fr=0x%02x xx=0x%02x\n",
1547: drive->name, args[0], args[1], args[2], args[3]);
1548: #endif
1549: OUT_BYTE(args[2],IDE_FEATURE_REG);
1550: ide_cmd(drive, args[0], args[1], &drive_cmd_intr);
1551: return;
1552: } else {
1553: /*
1554: * NULL is actually a valid way of waiting for
1555: * all current requests to be flushed from the queue.
1556: */
1557: #ifdef DEBUG
1558: printk("%s: DRIVE_CMD (null)\n", drive->name);
1559: #endif
1560: ide_end_drive_cmd(drive, GET_STAT(), GET_ERR());
1561: return;
1562: }
1563: }
1564:
1565: /*
1566: * do_request() initiates handling of a new I/O request
1567: */
1568: static inline void do_request (ide_hwif_t *hwif, struct request *rq)
1569: {
1570: unsigned int minor, unit;
1571: unsigned long block, blockend;
1572: ide_drive_t *drive;
1573:
1574: sti();
1575: #ifdef DEBUG
1576: printk("%s: do_request: current=0x%08lx\n", hwif->name, (unsigned long) rq);
1577: #endif
1578: minor = MINOR(rq->rq_dev);
1579: unit = minor >> PARTN_BITS;
1580: if (MAJOR(rq->rq_dev) != hwif->major || unit >= MAX_DRIVES) {
1581: printk("%s: bad device number: %s\n",
1582: hwif->name, kdevname(rq->rq_dev));
1583: goto kill_rq;
1584: }
1585: drive = &hwif->drives[unit];
1586: #ifdef DEBUG
1587: if (rq->bh && !buffer_locked(rq->bh)) {
1588: printk("%s: block not locked\n", drive->name);
1589: goto kill_rq;
1590: }
1591: #endif
1592: block = rq->sector;
1593: blockend = block + rq->nr_sectors;
1594: if ((blockend < block) || (blockend > drive->part[minor&PARTN_MASK].nr_sects)) {
1.1.1.2 ! root 1595: #ifdef MACH
! 1596: printk ("%s%c: bad access: block=%ld, count=%ld, blockend=%ld, nr_sects%ld\n",
! 1597: drive->name, (minor&PARTN_MASK)?'0'+(minor&PARTN_MASK):' ',
! 1598: block, rq->nr_sectors, blockend, drive->part[minor&PARTN_MASK].nr_sects);
! 1599: #else
1.1 root 1600: printk("%s%c: bad access: block=%ld, count=%ld\n", drive->name,
1601: (minor&PARTN_MASK)?'0'+(minor&PARTN_MASK):' ', block, rq->nr_sectors);
1.1.1.2 ! root 1602: #endif
1.1 root 1603: goto kill_rq;
1604: }
1605: block += drive->part[minor&PARTN_MASK].start_sect + drive->sect0;
1606: #if FAKE_FDISK_FOR_EZDRIVE
1607: if (block == 0 && drive->remap_0_to_1)
1608: block = 1; /* redirect MBR access to EZ-Drive partn table */
1609: #endif /* FAKE_FDISK_FOR_EZDRIVE */
1610: ((ide_hwgroup_t *)hwif->hwgroup)->drive = drive;
1611: #if (DISK_RECOVERY_TIME > 0)
1612: while ((read_timer() - hwif->last_time) < DISK_RECOVERY_TIME);
1613: #endif
1614:
1615: #ifdef CONFIG_BLK_DEV_IDETAPE
1616: POLL_HWIF_TAPE_DRIVE; /* macro from ide-tape.h */
1617: #endif /* CONFIG_BLK_DEV_IDETAPE */
1618:
1619: SELECT_DRIVE(hwif,drive);
1620: if (ide_wait_stat(drive, drive->ready_stat, BUSY_STAT|DRQ_STAT, WAIT_READY)) {
1621: printk("%s: drive not ready for command\n", drive->name);
1622: return;
1623: }
1.1.1.2 ! root 1624:
1.1 root 1625: if (!drive->special.all) {
1626: if (rq->cmd == IDE_DRIVE_CMD) {
1627: execute_drive_cmd(drive, rq);
1628: return;
1629: }
1630: #ifdef CONFIG_BLK_DEV_IDEATAPI
1631: switch (drive->media) {
1632: case ide_disk:
1633: do_rw_disk (drive, rq, block);
1634: return;
1635: #ifdef CONFIG_BLK_DEV_IDECD
1636: case ide_cdrom:
1637: ide_do_rw_cdrom (drive, block);
1638: return;
1639: #endif /* CONFIG_BLK_DEV_IDECD */
1640: #ifdef CONFIG_BLK_DEV_IDETAPE
1641: case ide_tape:
1642: idetape_do_request (drive, rq, block);
1643: return;
1644: #endif /* CONFIG_BLK_DEV_IDETAPE */
1645: #ifdef CONFIG_BLK_DEV_IDEFLOPPY
1646: case ide_floppy:
1647: idefloppy_do_request (drive, rq, block);
1648: return;
1649: #endif /* CONFIG_BLK_DEV_IDEFLOPPY */
1650: #ifdef CONFIG_BLK_DEV_IDESCSI
1651: case ide_scsi:
1652: idescsi_do_request (drive, rq, block);
1653: return;
1654: #endif /* CONFIG_BLK_DEV_IDESCSI */
1655:
1656: default:
1657: printk("%s: media type %d not supported\n",
1658: drive->name, drive->media);
1659: goto kill_rq;
1660: }
1661: #else
1662: do_rw_disk (drive, rq, block); /* simpler and faster */
1663: return;
1.1.1.2 ! root 1664: #endif /* CONFIG_BLK_DEV_IDEATAPI */
1.1 root 1665: }
1666: do_special(drive);
1667: return;
1668: kill_rq:
1669: ide_end_request(0, hwif->hwgroup);
1670: }
1671:
1672: /*
1673: * The driver enables interrupts as much as possible. In order to do this,
1674: * (a) the device-interrupt is always masked before entry, and
1675: * (b) the timeout-interrupt is always disabled before entry.
1676: *
1677: * If we enter here from, say irq14, and then start a new request for irq15,
1678: * (possible with "serialize" option) then we cannot ensure that we exit
1679: * before the irq15 hits us. So, we must be careful not to let this bother us.
1680: *
1681: * Interrupts are still masked (by default) whenever we are exchanging
1682: * data/cmds with a drive, because some drives seem to have very poor
1683: * tolerance for latency during I/O. For devices which don't suffer from
1684: * this problem (most don't), the unmask flag can be set using the "hdparm"
1685: * utility, to permit other interrupts during data/cmd transfers.
1686: */
1687: void ide_do_request (ide_hwgroup_t *hwgroup)
1688: {
1689: cli(); /* paranoia */
1690: if (hwgroup->handler != NULL) {
1691: printk("%s: EEeekk!! handler not NULL in ide_do_request()\n", hwgroup->hwif->name);
1692: return;
1693: }
1694: do {
1695: ide_hwif_t *hwif = hwgroup->hwif;
1696: struct request *rq;
1697: if ((rq = hwgroup->rq) == NULL) {
1698: if (hwif->sharing_irq && hwgroup->drive) /* set nIEN */
1699: OUT_BYTE(hwgroup->drive->ctl|2,hwif->ctl_port);
1700: /*
1701: * hwgroup->next_hwif is different from hwgroup->hwif
1702: * only when a request is inserted using "ide_next".
1703: * This saves wear and tear on IDE tapes.
1704: */
1705: hwif = hwgroup->next_hwif;
1706: do {
1707: rq = blk_dev[hwif->major].current_request;
1708: if (rq != NULL && rq->rq_status != RQ_INACTIVE)
1709: goto got_rq;
1710: } while ((hwif = hwif->next) != hwgroup->next_hwif);
1711: hwgroup->active = 0;
1712: return; /* no work left for this hwgroup */
1713: }
1.1.1.2 ! root 1714: got_rq:
1.1 root 1715: do_request(hwgroup->hwif = hwgroup->next_hwif = hwif, hwgroup->rq = rq);
1716: cli();
1717: } while (hwgroup->handler == NULL);
1718: }
1719:
1720: /*
1721: * do_hwgroup_request() invokes ide_do_request() after first masking
1722: * all possible interrupts for the current hwgroup. This prevents race
1723: * conditions in the event that an unexpected interrupt occurs while
1724: * we are in the driver.
1725: *
1726: * Note that when an interrupt is used to reenter the driver, the first level
1727: * handler will already have masked the irq that triggered, but any other ones
1728: * for the hwgroup will still be unmasked. The driver tries to be careful
1729: * about such things.
1730: */
1731: static void do_hwgroup_request (ide_hwgroup_t *hwgroup)
1732: {
1733: if (hwgroup->handler == NULL) {
1734: ide_hwif_t *hgif = hwgroup->hwif;
1735: ide_hwif_t *hwif = hgif;
1736: hwgroup->active = 1;
1737: do {
1738: disable_irq(hwif->irq);
1739: } while ((hwif = hwif->next) != hgif);
1740: ide_do_request (hwgroup);
1741: do {
1742: enable_irq(hwif->irq);
1743: } while ((hwif = hwif->next) != hgif);
1744: }
1745: }
1746:
1747: static void do_ide0_request (void) /* invoked with cli() */
1748: {
1749: do_hwgroup_request (ide_hwifs[0].hwgroup);
1750: }
1751:
1752: #if MAX_HWIFS > 1
1753: static void do_ide1_request (void) /* invoked with cli() */
1754: {
1755: do_hwgroup_request (ide_hwifs[1].hwgroup);
1756: }
1757: #endif
1758:
1759: #if MAX_HWIFS > 2
1760: static void do_ide2_request (void) /* invoked with cli() */
1761: {
1762: do_hwgroup_request (ide_hwifs[2].hwgroup);
1763: }
1764: #endif
1765:
1766: #if MAX_HWIFS > 3
1767: static void do_ide3_request (void) /* invoked with cli() */
1768: {
1769: do_hwgroup_request (ide_hwifs[3].hwgroup);
1770: }
1771: #endif
1772:
1773: static void timer_expiry (unsigned long data)
1774: {
1775: ide_hwgroup_t *hwgroup = (ide_hwgroup_t *) data;
1776: ide_drive_t *drive = hwgroup->drive;
1777: unsigned long flags;
1778:
1779: save_flags(flags);
1780: cli();
1781:
1782: if (hwgroup->poll_timeout != 0) { /* polling in progress? */
1783: ide_handler_t *handler = hwgroup->handler;
1784: hwgroup->handler = NULL;
1785: handler(drive);
1786: } else if (hwgroup->handler == NULL) { /* not waiting for anything? */
1787: sti(); /* drive must have responded just as the timer expired */
1788: printk("%s: marginal timeout\n", drive->name);
1789: } else {
1790: hwgroup->handler = NULL; /* abort the operation */
1791: if (hwgroup->hwif->dmaproc)
1792: (void) hwgroup->hwif->dmaproc (ide_dma_abort, drive);
1793: ide_error(drive, "irq timeout", GET_STAT());
1794: }
1795: if (hwgroup->handler == NULL)
1796: do_hwgroup_request (hwgroup);
1797: restore_flags(flags);
1798: }
1799:
1800: /*
1801: * There's nothing really useful we can do with an unexpected interrupt,
1802: * other than reading the status register (to clear it), and logging it.
1803: * There should be no way that an irq can happen before we're ready for it,
1804: * so we needn't worry much about losing an "important" interrupt here.
1805: *
1806: * On laptops (and "green" PCs), an unexpected interrupt occurs whenever the
1807: * drive enters "idle", "standby", or "sleep" mode, so if the status looks
1808: * "good", we just ignore the interrupt completely.
1809: *
1810: * This routine assumes cli() is in effect when called.
1811: *
1812: * If an unexpected interrupt happens on irq15 while we are handling irq14
1813: * and if the two interfaces are "serialized" (CMD640), then it looks like
1814: * we could screw up by interfering with a new request being set up for irq15.
1815: *
1816: * In reality, this is a non-issue. The new command is not sent unless the
1817: * drive is ready to accept one, in which case we know the drive is not
1818: * trying to interrupt us. And ide_set_handler() is always invoked before
1819: * completing the issuance of any new drive command, so we will not be
1820: * accidently invoked as a result of any valid command completion interrupt.
1821: *
1822: */
1823: static void unexpected_intr (int irq, ide_hwgroup_t *hwgroup)
1824: {
1825: byte stat;
1826: unsigned int unit;
1827: ide_hwif_t *hwif = hwgroup->hwif;
1828:
1829: /*
1830: * handle the unexpected interrupt
1831: */
1832: do {
1833: if (hwif->irq == irq) {
1834: for (unit = 0; unit < MAX_DRIVES; ++unit) {
1835: ide_drive_t *drive = &hwif->drives[unit];
1836: if (!drive->present)
1837: continue;
1838: SELECT_DRIVE(hwif,drive);
1839: udelay(100); /* Ugly, but wait_stat() may not be safe here */
1840: if (!OK_STAT(stat=GET_STAT(), drive->ready_stat, BAD_STAT)) {
1841: /* Try to not flood the console with msgs */
1842: static unsigned long last_msgtime = 0;
1843: if ((last_msgtime + (HZ/2)) < jiffies) {
1844: last_msgtime = jiffies;
1845: (void) ide_dump_status(drive, "unexpected_intr", stat);
1846: }
1847: }
1848: if ((stat & DRQ_STAT))
1849: try_to_flush_leftover_data(drive);
1850: }
1851: }
1852: } while ((hwif = hwif->next) != hwgroup->hwif);
1853: SELECT_DRIVE(hwif,hwgroup->drive); /* Ugh.. probably interrupts current I/O */
1854: udelay(100); /* Ugly, but wait_stat() may not be safe here */
1855: }
1856:
1857: /*
1858: * entry point for all interrupts, caller does cli() for us
1859: */
1860: void ide_intr (int irq, void *dev_id, struct pt_regs *regs)
1861: {
1862: ide_hwgroup_t *hwgroup = dev_id;
1863: ide_handler_t *handler;
1864:
1865: if (irq == hwgroup->hwif->irq && (handler = hwgroup->handler) != NULL) {
1866: ide_drive_t *drive = hwgroup->drive;
1867: hwgroup->handler = NULL;
1868: del_timer(&(hwgroup->timer));
1869: if (drive->unmask)
1870: sti();
1871: handler(drive);
1872: cli(); /* this is necessary, as next rq may be different irq */
1873: if (hwgroup->handler == NULL) {
1874: SET_RECOVERY_TIMER(HWIF(drive));
1875: ide_do_request(hwgroup);
1876: }
1877: } else {
1878: unexpected_intr(irq, hwgroup);
1879: }
1880: cli();
1881: }
1882:
1883: /*
1884: * get_info_ptr() returns the (ide_drive_t *) for a given device number.
1885: * It returns NULL if the given device number does not match any present drives.
1886: */
1887: static ide_drive_t *get_info_ptr (kdev_t i_rdev)
1888: {
1889: int major = MAJOR(i_rdev);
1890: unsigned int h;
1891:
1892: for (h = 0; h < MAX_HWIFS; ++h) {
1893: ide_hwif_t *hwif = &ide_hwifs[h];
1894: if (hwif->present && major == hwif->major) {
1895: unsigned unit = DEVICE_NR(i_rdev);
1896: if (unit < MAX_DRIVES) {
1897: ide_drive_t *drive = &hwif->drives[unit];
1898: if (drive->present)
1899: return drive;
1900: } else if (major == IDE0_MAJOR && unit < 4) {
1901: printk("ide: probable bad entry for /dev/hd%c\n", 'a'+unit);
1902: printk("ide: to fix it, run: /usr/src/linux/scripts/MAKEDEV.ide\n");
1903: }
1904: break;
1905: }
1906: }
1907: return NULL;
1908: }
1909:
1910: /*
1911: * This function is intended to be used prior to invoking ide_do_drive_cmd().
1912: */
1913: void ide_init_drive_cmd (struct request *rq)
1914: {
1915: rq->buffer = NULL;
1916: rq->cmd = IDE_DRIVE_CMD;
1917: rq->sector = 0;
1918: rq->nr_sectors = 0;
1919: rq->current_nr_sectors = 0;
1920: rq->sem = NULL;
1921: rq->bh = NULL;
1922: rq->bhtail = NULL;
1923: rq->next = NULL;
1924:
1925: #if 0 /* these are done each time through ide_do_drive_cmd() */
1926: rq->errors = 0;
1927: rq->rq_status = RQ_ACTIVE;
1928: rq->rq_dev = ????;
1929: #endif
1930: }
1931:
1932: /*
1933: * This function issues a special IDE device request
1934: * onto the request queue.
1935: *
1936: * If action is ide_wait, then the rq is queued at the end of the
1937: * request queue, and the function sleeps until it has been processed.
1938: * This is for use when invoked from an ioctl handler.
1939: *
1940: * If action is ide_preempt, then the rq is queued at the head of
1941: * the request queue, displacing the currently-being-processed
1942: * request and this function returns immediately without waiting
1943: * for the new rq to be completed. This is VERY DANGEROUS, and is
1944: * intended for careful use by the ATAPI tape/cdrom driver code.
1945: *
1946: * If action is ide_next, then the rq is queued immediately after
1947: * the currently-being-processed-request (if any), and the function
1948: * returns without waiting for the new rq to be completed. As above,
1949: * This is VERY DANGEROUS, and is intended for careful use by the
1950: * ATAPI tape/cdrom driver code.
1951: *
1952: * If action is ide_end, then the rq is queued at the end of the
1953: * request queue, and the function returns immediately without waiting
1954: * for the new rq to be completed. This is again intended for careful
1955: * use by the ATAPI tape/cdrom driver code. (Currently used by ide-tape.c,
1956: * when operating in the pipelined operation mode).
1957: */
1958: int ide_do_drive_cmd (ide_drive_t *drive, struct request *rq, ide_action_t action)
1959: {
1960: unsigned long flags;
1961: unsigned int major = HWIF(drive)->major;
1962: struct request *cur_rq;
1963: struct blk_dev_struct *bdev = &blk_dev[major];
1964: struct semaphore sem = MUTEX_LOCKED;
1965:
1966: if (IS_PROMISE_DRIVE && rq->buffer != NULL)
1967: return -ENOSYS; /* special drive cmds not supported */
1968: rq->errors = 0;
1969: rq->rq_status = RQ_ACTIVE;
1970: rq->rq_dev = MKDEV(major,(drive->select.b.unit)<<PARTN_BITS);
1971: if (action == ide_wait)
1972: rq->sem = &sem;
1973: unplug_device(bdev);
1974:
1975: save_flags(flags);
1976: cli();
1977: if (action == ide_next)
1978: HWGROUP(drive)->next_hwif = HWIF(drive);
1979: cur_rq = bdev->current_request;
1980:
1981: if (cur_rq == NULL || action == ide_preempt) {
1982: rq->next = cur_rq;
1983: bdev->current_request = rq;
1984: if (action == ide_preempt)
1985: HWGROUP(drive)->rq = NULL;
1986: } else {
1987: if (action == ide_wait || action == ide_end) {
1988: while (cur_rq->next != NULL) /* find end of list */
1989: cur_rq = cur_rq->next;
1990: }
1991: rq->next = cur_rq->next;
1992: cur_rq->next = rq;
1993: }
1994: if (!HWGROUP(drive)->active) {
1995: do_hwgroup_request(HWGROUP(drive));
1996: cli();
1997: }
1998: if (action == ide_wait && rq->rq_status != RQ_INACTIVE)
1999: down(&sem); /* wait for it to be serviced */
2000: restore_flags(flags);
2001: return rq->errors ? -EIO : 0; /* return -EIO if errors */
2002: }
2003:
2004: static int ide_open(struct inode * inode, struct file * filp)
2005: {
2006: ide_drive_t *drive;
2007: unsigned long flags;
2008:
2009: if ((drive = get_info_ptr(inode->i_rdev)) == NULL)
2010: return -ENXIO;
2011: save_flags(flags);
2012: cli();
2013: while (drive->busy)
2014: sleep_on(&drive->wqueue);
2015: drive->usage++;
2016: restore_flags(flags);
2017: #ifdef CONFIG_BLK_DEV_IDECD
2018: if (drive->media == ide_cdrom)
2019: return ide_cdrom_open (inode, filp, drive);
2020: #endif /* CONFIG_BLK_DEV_IDECD */
2021: #ifdef CONFIG_BLK_DEV_IDETAPE
2022: if (drive->media == ide_tape)
2023: return idetape_blkdev_open (inode, filp, drive);
2024: #endif /* CONFIG_BLK_DEV_IDETAPE */
2025: #ifdef CONFIG_BLK_DEV_IDEFLOPPY
2026: if (drive->media == ide_floppy)
2027: return idefloppy_open (inode, filp, drive);
2028: #endif /* CONFIG_BLK_DEV_IDEFLOPPY */
2029: #ifdef CONFIG_BLK_DEV_IDESCSI
2030: if (drive->media == ide_scsi)
2031: return idescsi_open (inode, filp, drive);
2032: #endif /* CONFIG_BLK_DEV_IDESCSI */
2033: if (drive->removable && drive->usage == 1) {
2034: byte door_lock[] = {WIN_DOORLOCK,0,0,0};
2035: struct request rq;
2036: check_disk_change(inode->i_rdev);
2037: ide_init_drive_cmd (&rq);
1.1.1.2 ! root 2038: rq.buffer = (char *)door_lock;
1.1 root 2039: /*
2040: * Ignore the return code from door_lock,
2041: * since the open() has already succeeded,
2042: * and the door_lock is irrelevant at this point.
2043: */
2044: (void) ide_do_drive_cmd(drive, &rq, ide_wait);
2045: }
2046: return 0;
2047: }
2048:
2049: /*
2050: * Releasing a block device means we sync() it, so that it can safely
2051: * be forgotten about...
2052: */
2053: static void ide_release(struct inode * inode, struct file * file)
2054: {
2055: ide_drive_t *drive;
2056:
2057: if ((drive = get_info_ptr(inode->i_rdev)) != NULL) {
2058: fsync_dev(inode->i_rdev);
2059: drive->usage--;
2060: #ifdef CONFIG_BLK_DEV_IDECD
2061: if (drive->media == ide_cdrom) {
2062: ide_cdrom_release (inode, file, drive);
2063: return;
2064: }
2065: #endif /* CONFIG_BLK_DEV_IDECD */
2066: #ifdef CONFIG_BLK_DEV_IDETAPE
2067: if (drive->media == ide_tape) {
2068: idetape_blkdev_release (inode, file, drive);
2069: return;
2070: }
2071: #endif /* CONFIG_BLK_DEV_IDETAPE */
2072: #ifdef CONFIG_BLK_DEV_IDEFLOPPY
2073: if (drive->media == ide_floppy) {
2074: idefloppy_release (inode, file, drive);
2075: return;
2076: }
2077: #endif /* CONFIG_BLK_DEV_IDEFLOPPY */
2078: #ifdef CONFIG_BLK_DEV_IDESCSI
2079: if (drive->media == ide_scsi) {
2080: idescsi_ide_release (inode, file, drive);
2081: return;
2082: }
2083: #endif /* CONFIG_BLK_DEV_IDESCSI */
2084: if (drive->removable && !drive->usage) {
2085: byte door_unlock[] = {WIN_DOORUNLOCK,0,0,0};
2086: struct request rq;
2087: invalidate_buffers(inode->i_rdev);
2088: ide_init_drive_cmd (&rq);
1.1.1.2 ! root 2089: rq.buffer = (char *)door_unlock;
1.1 root 2090: (void) ide_do_drive_cmd(drive, &rq, ide_wait);
2091: }
2092: }
2093: }
2094:
2095: /*
2096: * This routine is called to flush all partitions and partition tables
2097: * for a changed disk, and then re-read the new partition table.
2098: * If we are revalidating a disk because of a media change, then we
2099: * enter with usage == 0. If we are using an ioctl, we automatically have
2100: * usage == 1 (we need an open channel to use an ioctl :-), so this
2101: * is our limit.
2102: */
2103: static int revalidate_disk(kdev_t i_rdev)
2104: {
2105: ide_drive_t *drive;
2106: unsigned int p, major, minor;
2107: long flags;
2108:
2109: if ((drive = get_info_ptr(i_rdev)) == NULL)
2110: return -ENODEV;
2111:
2112: major = MAJOR(i_rdev);
2113: minor = drive->select.b.unit << PARTN_BITS;
2114: save_flags(flags);
2115: cli();
2116: if (drive->busy || (drive->usage > 1)) {
2117: restore_flags(flags);
2118: return -EBUSY;
2119: };
2120: drive->busy = 1;
2121: restore_flags(flags);
2122:
2123: for (p = 0; p < (1<<PARTN_BITS); ++p) {
2124: if (drive->part[p].nr_sects > 0) {
2125: kdev_t devp = MKDEV(major, minor+p);
2126: fsync_dev (devp);
2127: invalidate_inodes (devp);
2128: invalidate_buffers (devp);
2129: set_blocksize(devp, 1024);
2130: }
2131: drive->part[p].start_sect = 0;
2132: drive->part[p].nr_sects = 0;
2133: };
2134:
2135: drive->part[0].nr_sects = current_capacity(drive);
2136: if ((drive->media != ide_disk && drive->media != ide_floppy) || !drive->part[0].nr_sects)
2137: drive->part[0].start_sect = -1;
2138: resetup_one_dev(HWIF(drive)->gd, drive->select.b.unit);
2139:
2140: drive->busy = 0;
2141: wake_up(&drive->wqueue);
2142: return 0;
2143: }
2144:
2145: static int write_fs_long (unsigned long useraddr, long value)
2146: {
2147: int err;
2148:
2149: if (NULL == (long *)useraddr)
2150: return -EINVAL;
2151: if ((err = verify_area(VERIFY_WRITE, (long *)useraddr, sizeof(long))))
2152: return err;
2153: put_user((unsigned)value, (long *) useraddr);
2154: return 0;
2155: }
2156:
2157: static int ide_ioctl (struct inode *inode, struct file *file,
2158: unsigned int cmd, unsigned long arg)
2159: {
2160: int err;
2161: ide_drive_t *drive;
2162: unsigned long flags;
2163: struct request rq;
2164:
2165: if (!inode || !(inode->i_rdev))
2166: return -EINVAL;
2167: if ((drive = get_info_ptr(inode->i_rdev)) == NULL)
2168: return -ENODEV;
2169: ide_init_drive_cmd (&rq);
2170: switch (cmd) {
2171: case HDIO_GETGEO:
2172: {
2173: struct hd_geometry *loc = (struct hd_geometry *) arg;
2174: if (!loc || (drive->media != ide_disk && drive->media != ide_floppy)) return -EINVAL;
1.1.1.2 ! root 2175: #ifdef MACH
! 2176: loc->heads = drive->bios_head;
! 2177: loc->sectors = drive->bios_sect;
! 2178: loc->cylinders = drive->bios_cyl;
! 2179: loc->start
! 2180: = (drive->part[MINOR(inode->i_rdev)&PARTN_MASK]
! 2181: .start_sect);
! 2182: #else
1.1 root 2183: err = verify_area(VERIFY_WRITE, loc, sizeof(*loc));
2184: if (err) return err;
2185: put_user(drive->bios_head, (byte *) &loc->heads);
2186: put_user(drive->bios_sect, (byte *) &loc->sectors);
2187: put_user(drive->bios_cyl, (unsigned short *) &loc->cylinders);
2188: put_user((unsigned)drive->part[MINOR(inode->i_rdev)&PARTN_MASK].start_sect,
2189: (unsigned long *) &loc->start);
1.1.1.2 ! root 2190: #endif
1.1 root 2191: return 0;
2192: }
2193: case BLKFLSBUF:
2194: if (!suser()) return -EACCES;
2195: fsync_dev(inode->i_rdev);
2196: invalidate_buffers(inode->i_rdev);
2197: return 0;
2198:
2199: case BLKRASET:
2200: if (!suser()) return -EACCES;
2201: if(arg > 0xff) return -EINVAL;
2202: read_ahead[MAJOR(inode->i_rdev)] = arg;
2203: return 0;
2204:
2205: case BLKRAGET:
2206: return write_fs_long(arg, read_ahead[MAJOR(inode->i_rdev)]);
2207:
2208: case BLKGETSIZE: /* Return device size */
2209: return write_fs_long(arg, drive->part[MINOR(inode->i_rdev)&PARTN_MASK].nr_sects);
2210: case BLKRRPART: /* Re-read partition tables */
2211: if (!suser()) return -EACCES;
2212: return revalidate_disk(inode->i_rdev);
2213:
2214: case HDIO_GET_KEEPSETTINGS:
2215: return write_fs_long(arg, drive->keep_settings);
2216:
2217: case HDIO_GET_UNMASKINTR:
2218: return write_fs_long(arg, drive->unmask);
2219:
2220: case HDIO_GET_DMA:
2221: return write_fs_long(arg, drive->using_dma);
2222:
2223: case HDIO_GET_32BIT:
2224: return write_fs_long(arg, drive->io_32bit);
2225:
2226: case HDIO_GET_MULTCOUNT:
2227: return write_fs_long(arg, drive->mult_count);
2228:
2229: case HDIO_GET_IDENTITY:
2230: if (!arg || (MINOR(inode->i_rdev) & PARTN_MASK))
2231: return -EINVAL;
2232: if (drive->id == NULL)
2233: return -ENOMSG;
2234: err = verify_area(VERIFY_WRITE, (char *)arg, sizeof(*drive->id));
2235: if (!err)
2236: memcpy_tofs((char *)arg, (char *)drive->id, sizeof(*drive->id));
2237: return err;
2238:
2239: case HDIO_GET_NOWERR:
2240: return write_fs_long(arg, drive->bad_wstat == BAD_R_STAT);
2241:
2242: case HDIO_SET_DMA:
2243: if (!suser()) return -EACCES;
2244: #ifdef CONFIG_BLK_DEV_IDECD
2245: if (drive->media == ide_cdrom)
2246: return -EPERM;
2247: #endif /* CONFIG_BLK_DEV_IDECD */
2248: if (!drive->id || !(drive->id->capability & 1) || !HWIF(drive)->dmaproc)
2249: return -EPERM;
2250: case HDIO_SET_KEEPSETTINGS:
2251: case HDIO_SET_UNMASKINTR:
2252: case HDIO_SET_NOWERR:
2253: if (arg > 1)
2254: return -EINVAL;
2255: case HDIO_SET_32BIT:
2256: if (!suser()) return -EACCES;
2257: if ((MINOR(inode->i_rdev) & PARTN_MASK))
2258: return -EINVAL;
2259: save_flags(flags);
2260: cli();
2261: switch (cmd) {
2262: case HDIO_SET_DMA:
2263: if (!(HWIF(drive)->dmaproc)) {
2264: restore_flags(flags);
2265: return -EPERM;
2266: }
2267: drive->using_dma = arg;
2268: break;
2269: case HDIO_SET_KEEPSETTINGS:
2270: drive->keep_settings = arg;
2271: break;
2272: case HDIO_SET_UNMASKINTR:
2273: if (arg && drive->no_unmask) {
2274: restore_flags(flags);
2275: return -EPERM;
2276: }
2277: drive->unmask = arg;
2278: break;
2279: case HDIO_SET_NOWERR:
2280: drive->bad_wstat = arg ? BAD_R_STAT : BAD_W_STAT;
2281: break;
2282: case HDIO_SET_32BIT:
2283: if (arg > (1 + (SUPPORT_VLB_SYNC<<1))) {
2284: restore_flags(flags);
2285: return -EINVAL;
2286: }
2287: if (arg && drive->no_io_32bit) {
2288: restore_flags(flags);
2289: return -EPERM;
2290: }
2291: drive->io_32bit = arg;
2292: #ifdef CONFIG_BLK_DEV_DTC2278
2293: if (HWIF(drive)->chipset == ide_dtc2278)
2294: HWIF(drive)->drives[!drive->select.b.unit].io_32bit = arg;
2295: #endif /* CONFIG_BLK_DEV_DTC2278 */
2296: break;
2297: }
2298: restore_flags(flags);
2299: return 0;
2300:
2301: case HDIO_SET_MULTCOUNT:
2302: if (!suser()) return -EACCES;
2303: if (MINOR(inode->i_rdev) & PARTN_MASK)
2304: return -EINVAL;
2305: if (drive->id && arg > drive->id->max_multsect)
2306: return -EINVAL;
2307: save_flags(flags);
2308: cli();
2309: if (drive->special.b.set_multmode) {
2310: restore_flags(flags);
2311: return -EBUSY;
2312: }
2313: drive->mult_req = arg;
2314: drive->special.b.set_multmode = 1;
2315: restore_flags(flags);
2316: (void) ide_do_drive_cmd (drive, &rq, ide_wait);
2317: return (drive->mult_count == arg) ? 0 : -EIO;
2318:
2319: case HDIO_DRIVE_CMD:
2320: {
2321: byte args[4], *argbuf = args;
2322: int argsize = 4;
2323: if (!suser() || securelevel > 0) return -EACCES;
2324: if (NULL == (void *) arg) {
2325: err = ide_do_drive_cmd(drive, &rq, ide_wait);
2326: } else if (!(err = verify_area(VERIFY_READ,(void *)arg, 4))) {
2327: memcpy_fromfs(args, (void *)arg, 4);
2328: if (args[3]) {
2329: argsize = 4 + (SECTOR_WORDS * 4 * args[3]);
2330: argbuf = kmalloc(argsize, GFP_KERNEL);
2331: if (argbuf == NULL)
2332: return -ENOMEM;
2333: argbuf[0] = args[0];
2334: argbuf[1] = args[1];
2335: argbuf[2] = args[2];
2336: argbuf[3] = args[3];
2337: }
2338: if (!(err = verify_area(VERIFY_WRITE,(void *)arg, argsize))) {
1.1.1.2 ! root 2339: rq.buffer = (char *)argbuf;
1.1 root 2340: err = ide_do_drive_cmd(drive, &rq, ide_wait);
2341: memcpy_tofs((void *)arg, argbuf, argsize);
2342: }
2343: if (argsize > 4)
2344: kfree(argbuf);
2345: }
2346: return err;
2347: }
2348: case HDIO_SET_PIO_MODE:
2349: if (!suser()) return -EACCES;
2350: if (MINOR(inode->i_rdev) & PARTN_MASK)
2351: return -EINVAL;
2352: if (!HWIF(drive)->tuneproc)
2353: return -ENOSYS;
2354: save_flags(flags);
2355: cli();
2356: if (drive->special.b.set_tune) {
2357: restore_flags(flags);
2358: return -EBUSY;
2359: }
2360: drive->tune_req = (byte) arg;
2361: drive->special.b.set_tune = 1;
2362: restore_flags(flags);
2363: (void) ide_do_drive_cmd (drive, &rq, ide_wait);
2364: return 0;
2365:
2366: RO_IOCTLS(inode->i_rdev, arg);
2367:
2368: default:
2369: #ifdef CONFIG_BLK_DEV_IDECD
2370: if (drive->media == ide_cdrom)
2371: return ide_cdrom_ioctl(drive, inode, file, cmd, arg);
2372: #endif /* CONFIG_BLK_DEV_IDECD */
2373: #ifdef CONFIG_BLK_DEV_IDETAPE
2374: if (drive->media == ide_tape)
2375: return idetape_blkdev_ioctl(drive, inode, file, cmd, arg);
2376: #endif /* CONFIG_BLK_DEV_IDETAPE */
2377: #ifdef CONFIG_BLK_DEV_IDEFLOPPY
2378: if (drive->media == ide_floppy)
2379: return idefloppy_ioctl(drive, inode, file, cmd, arg);
2380: #endif /* CONFIG_BLK_DEV_IDEFLOPPY */
2381: #ifdef CONFIG_BLK_DEV_IDESCSI
2382: if (drive->media == ide_scsi)
2383: return idescsi_ioctl(drive, inode, file, cmd, arg);
2384: #endif /* CONFIG_BLK_DEV_IDESCSI */
2385: return -EPERM;
2386: }
2387: }
2388:
2389: static int ide_check_media_change (kdev_t i_rdev)
2390: {
2391: ide_drive_t *drive;
2392:
2393: if ((drive = get_info_ptr(i_rdev)) == NULL)
2394: return -ENODEV;
2395: #ifdef CONFIG_BLK_DEV_IDECD
2396: if (drive->media == ide_cdrom)
2397: return ide_cdrom_check_media_change (drive);
2398: #endif /* CONFIG_BLK_DEV_IDECD */
2399: #ifdef CONFIG_BLK_DEV_IDEFLOPPY
2400: if (drive->media == ide_floppy)
2401: return idefloppy_media_change (drive);
2402: #endif /* CONFIG_BLK_DEV_IDEFLOPPY */
2403: if (drive->removable) /* for disks */
2404: return 1; /* always assume it was changed */
2405: return 0;
2406: }
2407:
2408: void ide_fixstring (byte *s, const int bytecount, const int byteswap)
2409: {
2410: byte *p = s, *end = &s[bytecount & ~1]; /* bytecount must be even */
2411:
2412: if (byteswap) {
2413: /* convert from big-endian to host byte order */
2414: for (p = end ; p != s;) {
2415: unsigned short *pp = (unsigned short *) (p -= 2);
2416: *pp = ntohs(*pp);
2417: }
2418: }
2419:
2420: /* strip leading blanks */
2421: while (s != end && *s == ' ')
2422: ++s;
2423:
2424: /* compress internal blanks and strip trailing blanks */
2425: while (s != end && *s) {
2426: if (*s++ != ' ' || (s != end && *s && *s != ' '))
2427: *p++ = *(s-1);
2428: }
2429:
2430: /* wipe out trailing garbage */
2431: while (p != end)
2432: *p++ = '\0';
2433: }
2434:
2435: static inline void do_identify (ide_drive_t *drive, byte cmd)
2436: {
2437: int bswap;
2438: struct hd_driveid *id;
2439: unsigned long capacity, check;
2440:
2441: id = drive->id = kmalloc (SECTOR_WORDS*4, GFP_KERNEL);
2442: ide_input_data(drive, id, SECTOR_WORDS);/* read 512 bytes of id info */
2443: sti();
2444:
2445: #if defined (CONFIG_SCSI_EATA_DMA) || defined (CONFIG_SCSI_EATA_PIO) || defined (CONFIG_SCSI_EATA)
2446: /*
1.1.1.2 ! root 2447: * EATA SCSI controllers do a hardware ATA emulation:
1.1 root 2448: * Ignore them if there is a driver for them available.
2449: */
2450: if ((id->model[0] == 'P' && id->model[1] == 'M')
2451: || (id->model[0] == 'S' && id->model[1] == 'K')) {
2452: printk("%s: EATA SCSI HBA %.10s\n", drive->name, id->model);
2453: drive->present = 0;
2454: return;
2455: }
2456: #endif
2457:
2458: /*
2459: * WIN_IDENTIFY returns little-endian info,
2460: * WIN_PIDENTIFY *usually* returns little-endian info.
2461: */
2462: bswap = 1;
2463: if (cmd == WIN_PIDENTIFY) {
2464: if ((id->model[0] == 'N' && id->model[1] == 'E') /* NEC */
2465: || (id->model[0] == 'F' && id->model[1] == 'X') /* Mitsumi */
2466: || (id->model[0] == 'P' && id->model[1] == 'i'))/* Pioneer */
2467: bswap = 0; /* Vertos drives may still be weird */
2468: }
2469: ide_fixstring (id->model, sizeof(id->model), bswap);
2470: ide_fixstring (id->fw_rev, sizeof(id->fw_rev), bswap);
2471: ide_fixstring (id->serial_no, sizeof(id->serial_no), bswap);
2472:
1.1.1.2 ! root 2473: if (strstr((char *)id->model, "E X A B Y T E N E S T"))
1.1 root 2474: return;
2475:
2476: #ifdef CONFIG_BLK_DEV_IDEATAPI
2477: /*
2478: * Check for an ATAPI device
2479: */
2480: if (cmd == WIN_PIDENTIFY) {
2481: byte type = (id->config >> 8) & 0x1f;
2482: printk("%s: %s, ATAPI ", drive->name, id->model);
2483: #ifdef CONFIG_BLK_DEV_PROMISE
2484: if (HWIF(drive)->is_promise2) {
2485: printk(" -- not supported on 2nd Promise port\n");
2486: drive->present = 0;
2487: return;
2488: }
2489: #endif /* CONFIG_BLK_DEV_PROMISE */
2490: if (!drive->ide_scsi) switch (type) {
2491: case 0:
1.1.1.2 ! root 2492: if (!strstr((char *)id->model, "oppy") &&
! 2493: !strstr((char *)id->model, "poyp") &&
! 2494: !strstr((char *)id->model, "ZIP"))
1.1 root 2495: printk("cdrom or floppy?, assuming ");
1.1.1.2 ! root 2496: if (drive->media != ide_cdrom &&
! 2497: !strstr((char *)id->model, "CD-ROM")) {
1.1 root 2498: #ifdef CONFIG_BLK_DEV_IDEFLOPPY
2499: printk("FLOPPY drive\n");
2500: drive->media = ide_floppy;
2501: if (idefloppy_identify_device(drive, id))
2502: drive->present = 1;
2503: return;
2504: #else
2505: printk("FLOPPY ");
2506: break;
2507: #endif /* CONFIG_BLK_DEV_IDEFLOPPY */
2508: }
2509: /* Early cdrom models used zero */
2510: case 5:
2511: #ifdef CONFIG_BLK_DEV_IDECD
2512: printk ("CDROM drive\n");
2513: drive->media = ide_cdrom;
2514: drive->present = 1;
2515: drive->removable = 1;
2516: return;
2517: #else
2518: printk ("CDROM ");
2519: break;
2520: #endif /* CONFIG_BLK_DEV_IDECD */
2521: case 1:
2522: #ifdef CONFIG_BLK_DEV_IDETAPE
2523: printk ("TAPE drive");
2524: if (idetape_identify_device (drive,id)) {
2525: drive->media = ide_tape;
2526: drive->present = 1;
2527: drive->removable = 1;
2528: if (drive->autotune != 2 && HWIF(drive)->dmaproc != NULL) {
2529: if (!HWIF(drive)->dmaproc(ide_dma_check, drive))
2530: printk(", DMA");
2531: }
2532: printk("\n");
2533: }
2534: else {
2535: drive->present = 0;
2536: printk ("\nide-tape: the tape is not supported by this version of the driver\n");
2537: }
2538: return;
2539: #else
2540: printk ("TAPE ");
2541: break;
2542: #endif /* CONFIG_BLK_DEV_IDETAPE */
2543: default:
2544: drive->present = 0;
2545: printk("Type %d - Unknown device\n", type);
2546: return;
2547: }
2548: #ifdef CONFIG_BLK_DEV_IDESCSI
2549: printk("drive - enabling SCSI emulation\n");
2550: drive->media = ide_scsi;
2551: drive->present = 1;
2552: idescsi_setup(drive);
2553: #else
2554: drive->present = 0;
2555: printk("- not supported by this kernel\n");
2556: #endif /* CONFIG_BLK_DEV_IDESCSI */
2557: return;
2558: }
2559: #endif /* CONFIG_BLK_DEV_IDEATAPI */
2560:
2561: /* check for removable disks (eg. SYQUEST), ignore 'WD' drives */
2562: if (id->config & (1<<7)) { /* removable disk ? */
2563: if (id->model[0] != 'W' || id->model[1] != 'D')
2564: drive->removable = 1;
2565: }
2566:
2567: /* SunDisk drives: treat as non-removable, force one unit */
2568: if (id->model[0] == 'S' && id->model[1] == 'u') {
2569: drive->removable = 0;
2570: if (drive->select.all & (1<<4)) {
2571: drive->present = 0;
2572: return;
2573: }
2574: }
2575:
2576: drive->media = ide_disk;
2577: /* Extract geometry if we did not already have one for the drive */
2578: if (!drive->present) {
2579: drive->present = 1;
2580: drive->cyl = drive->bios_cyl = id->cyls;
2581: drive->head = drive->bios_head = id->heads;
2582: drive->sect = drive->bios_sect = id->sectors;
2583: }
2584: /* Handle logical geometry translation by the drive */
2585: if ((id->field_valid & 1) && id->cur_cyls && id->cur_heads
1.1.1.2 ! root 2586: && (id->cur_heads <= 16) && id->cur_sectors) {
1.1 root 2587: /*
2588: * Extract the physical drive geometry for our use.
2589: * Note that we purposely do *not* update the bios info.
2590: * This way, programs that use it (like fdisk) will
2591: * still have the same logical view as the BIOS does,
2592: * which keeps the partition table from being screwed.
2593: *
2594: * An exception to this is the cylinder count,
2595: * which we reexamine later on to correct for 1024 limitations.
2596: */
2597: drive->cyl = id->cur_cyls;
2598: drive->head = id->cur_heads;
2599: drive->sect = id->cur_sectors;
2600:
2601: /* check for word-swapped "capacity" field in id information */
2602: capacity = drive->cyl * drive->head * drive->sect;
2603: check = (id->cur_capacity0 << 16) | id->cur_capacity1;
2604: if (check == capacity) { /* was it swapped? */
2605: /* yes, bring it into little-endian order: */
2606: id->cur_capacity0 = (capacity >> 0) & 0xffff;
2607: id->cur_capacity1 = (capacity >> 16) & 0xffff;
2608: }
2609: }
2610: /* Use physical geometry if what we have still makes no sense */
1.1.1.2 ! root 2611: if ((!drive->head || drive->head > 16) &&
! 2612: id->heads && id->heads <= 16) {
1.1 root 2613: drive->cyl = id->cyls;
2614: drive->head = id->heads;
2615: drive->sect = id->sectors;
2616: }
2617:
2618: /* calculate drive capacity, and select LBA if possible */
1.1.1.2 ! root 2619: capacity = current_capacity (drive);
1.1 root 2620:
1.1.1.2 ! root 2621: /*
! 2622: * if possible, give fdisk access to more of the drive,
! 2623: * by correcting bios_cyls:
! 2624: */
! 2625: if (capacity > drive->bios_cyl * drive->bios_head * drive->bios_sect
! 2626: && !drive->forced_geom && drive->bios_sect && drive->bios_head) {
! 2627: int cyl = (capacity / drive->bios_sect) / drive->bios_head;
! 2628: if (cyl <= 65535)
! 2629: drive->bios_cyl = cyl;
! 2630: else {
! 2631: /* OK until 539 GB */
! 2632: drive->bios_sect = 63;
! 2633: drive->bios_head = 255;
! 2634: drive->bios_cyl = capacity / (63*255);
! 2635: }
1.1 root 2636: }
2637:
1.1.1.2 ! root 2638: if (!strncmp((char *)id->model, "BMI ", 4) &&
! 2639: strstr((char *)id->model, " ENHANCED IDE ") &&
1.1 root 2640: drive->select.b.lba)
2641: drive->no_geom = 1;
2642:
2643: printk ("%s: %.40s, %ldMB w/%dkB Cache, CHS=%d/%d/%d",
2644: drive->name, id->model, current_capacity(drive)/2048L, id->buf_size/2,
2645: drive->bios_cyl, drive->bios_head, drive->bios_sect);
2646:
2647: drive->mult_count = 0;
2648: if (id->max_multsect) {
2649: drive->mult_req = INITIAL_MULT_COUNT;
2650: if (drive->mult_req > id->max_multsect)
2651: drive->mult_req = id->max_multsect;
2652: if (drive->mult_req || ((id->multsect_valid & 1) && id->multsect))
2653: drive->special.b.set_multmode = 1;
2654: }
2655: if (drive->autotune != 2 && HWIF(drive)->dmaproc != NULL) {
2656: if (!(HWIF(drive)->dmaproc(ide_dma_check, drive))) {
2657: if ((id->field_valid & 4) && (id->dma_ultra & (id->dma_ultra >> 8) & 7))
2658: printk(", UDMA");
2659: else
2660: printk(", DMA");
2661: }
2662: }
2663: printk("\n");
2664: }
2665:
2666: /*
2667: * Delay for *at least* 50ms. As we don't know how much time is left
2668: * until the next tick occurs, we wait an extra tick to be safe.
2669: * This is used only during the probing/polling for drives at boot time.
2670: */
2671: static void delay_50ms (void)
2672: {
2673: unsigned long timer = jiffies + ((HZ + 19)/20) + 1;
2674: while (timer > jiffies);
2675: }
2676:
2677: /*
2678: * try_to_identify() sends an ATA(PI) IDENTIFY request to a drive
2679: * and waits for a response. It also monitors irqs while this is
2680: * happening, in hope of automatically determining which one is
2681: * being used by the interface.
2682: *
2683: * Returns: 0 device was identified
2684: * 1 device timed-out (no response to identify request)
2685: * 2 device aborted the command (refused to identify itself)
2686: */
2687: static int try_to_identify (ide_drive_t *drive, byte cmd)
2688: {
2689: int hd_status, rc;
2690: unsigned long timeout;
2691: unsigned long irqs_on = 0;
2692: int irq_off;
2693:
2694: if (!HWIF(drive)->irq) { /* already got an IRQ? */
2695: probe_irq_off(probe_irq_on()); /* clear dangling irqs */
2696: irqs_on = probe_irq_on(); /* start monitoring irqs */
2697: OUT_BYTE(drive->ctl,IDE_CONTROL_REG); /* enable device irq */
2698: }
2699:
2700: delay_50ms(); /* take a deep breath */
2701: if ((IN_BYTE(IDE_ALTSTATUS_REG) ^ IN_BYTE(IDE_STATUS_REG)) & ~INDEX_STAT) {
2702: printk("%s: probing with STATUS instead of ALTSTATUS\n", drive->name);
2703: hd_status = IDE_STATUS_REG; /* ancient Seagate drives */
2704: } else
2705: hd_status = IDE_ALTSTATUS_REG; /* use non-intrusive polling */
2706:
2707: #if CONFIG_BLK_DEV_PROMISE
2708: if (IS_PROMISE_DRIVE) {
2709: if (promise_cmd(drive,PROMISE_IDENTIFY)) {
2710: if (irqs_on)
2711: (void) probe_irq_off(irqs_on);
2712: return 1;
2713: }
2714: } else
2715: #endif /* CONFIG_BLK_DEV_PROMISE */
2716: OUT_BYTE(cmd,IDE_COMMAND_REG); /* ask drive for ID */
2717: timeout = ((cmd == WIN_IDENTIFY) ? WAIT_WORSTCASE : WAIT_PIDENTIFY) / 2;
2718: timeout += jiffies;
2719: do {
2720: if (jiffies > timeout) {
2721: if (irqs_on)
2722: (void) probe_irq_off(irqs_on);
2723: return 1; /* drive timed-out */
2724: }
2725: delay_50ms(); /* give drive a breather */
2726: } while (IN_BYTE(hd_status) & BUSY_STAT);
2727:
2728: delay_50ms(); /* wait for IRQ and DRQ_STAT */
2729: if (OK_STAT(GET_STAT(),DRQ_STAT,BAD_R_STAT)) {
2730: unsigned long flags;
2731: save_flags(flags);
2732: cli(); /* some systems need this */
2733: do_identify(drive, cmd); /* drive returned ID */
2734: rc = 0; /* drive responded with ID */
2735: (void) GET_STAT(); /* clear drive IRQ */
2736: restore_flags(flags);
2737: } else
2738: rc = 2; /* drive refused ID */
2739: if (!HWIF(drive)->irq) {
2740: irq_off = probe_irq_off(irqs_on); /* get our irq number */
2741: if (irq_off > 0) {
2742: HWIF(drive)->irq = irq_off; /* save it for later */
2743: irqs_on = probe_irq_on();
2744: OUT_BYTE(drive->ctl|2,IDE_CONTROL_REG); /* mask device irq */
2745: udelay(5);
2746: (void) probe_irq_off(irqs_on);
2747: (void) probe_irq_off(probe_irq_on()); /* clear self-inflicted irq */
2748: (void) GET_STAT(); /* clear drive IRQ */
2749:
2750: } else { /* Mmmm.. multiple IRQs.. don't know which was ours */
2751: printk("%s: IRQ probe failed (%d)\n", drive->name, irq_off);
2752: #ifdef CONFIG_BLK_DEV_CMD640
2753: #ifdef CMD640_DUMP_REGS
2754: if (HWIF(drive)->chipset == ide_cmd640) {
2755: printk("%s: Hmmm.. probably a driver problem.\n", drive->name);
2756: CMD640_DUMP_REGS;
2757: }
2758: #endif /* CMD640_DUMP_REGS */
2759: #endif /* CONFIG_BLK_DEV_CMD640 */
2760: }
2761: }
2762: return rc;
2763: }
2764:
2765: /*
2766: * do_probe() has the difficult job of finding a drive if it exists,
2767: * without getting hung up if it doesn't exist, without trampling on
2768: * ethernet cards, and without leaving any IRQs dangling to haunt us later.
2769: *
2770: * If a drive is "known" to exist (from CMOS or kernel parameters),
2771: * but does not respond right away, the probe will "hang in there"
2772: * for the maximum wait time (about 30 seconds), otherwise it will
2773: * exit much more quickly.
2774: *
2775: * Returns: 0 device was identified
2776: * 1 device timed-out (no response to identify request)
2777: * 2 device aborted the command (refused to identify itself)
2778: * 3 bad status from device (possible for ATAPI drives)
2779: * 4 probe was not attempted because failure was obvious
2780: */
2781: static int do_probe (ide_drive_t *drive, byte cmd)
2782: {
2783: int rc;
2784: ide_hwif_t *hwif = HWIF(drive);
2785: unsigned long timeout;
2786: #ifdef CONFIG_BLK_DEV_IDEATAPI
2787: if (drive->present) { /* avoid waiting for inappropriate probes */
2788: if ((drive->media != ide_disk) && (cmd == WIN_IDENTIFY))
2789: return 4;
2790: }
2791: #endif /* CONFIG_BLK_DEV_IDEATAPI */
2792: #ifdef DEBUG
2793: printk("probing for %s: present=%d, media=%d, probetype=%s\n",
2794: drive->name, drive->present, drive->media,
2795: (cmd == WIN_IDENTIFY) ? "ATA" : "ATAPI");
2796: #endif
2797: SELECT_DRIVE(hwif,drive);
2798: delay_50ms();
2799: if (IN_BYTE(IDE_SELECT_REG) != drive->select.all && !drive->present) {
2800: OUT_BYTE(0xa0,IDE_SELECT_REG); /* exit with drive0 selected */
2801: delay_50ms(); /* allow BUSY_STAT to assert & clear */
2802: return 3; /* no i/f present: avoid killing ethernet cards */
2803: }
2804:
2805: if (OK_STAT(GET_STAT(),READY_STAT,BUSY_STAT)
2806: || drive->present || cmd == WIN_PIDENTIFY)
2807: {
2808: if ((rc = try_to_identify(drive,cmd))) /* send cmd and wait */
2809: rc = try_to_identify(drive,cmd); /* failed: try again */
2810: if (rc == 1 && cmd == WIN_PIDENTIFY && drive->autotune != 2) {
2811: printk("%s: no response (status = 0x%02x), resetting drive\n", drive->name, GET_STAT());
2812: delay_50ms();
2813: OUT_BYTE (drive->select.all, IDE_SELECT_REG);
2814: delay_50ms();
2815: OUT_BYTE(WIN_SRST, IDE_COMMAND_REG);
2816: timeout = jiffies;
2817: while ((GET_STAT() & BUSY_STAT) && jiffies < timeout + WAIT_WORSTCASE)
2818: delay_50ms();
2819: rc = try_to_identify(drive, cmd);
2820: }
2821: if (rc == 1)
2822: printk("%s: no response (status = 0x%02x)\n", drive->name, GET_STAT());
2823: (void) GET_STAT(); /* ensure drive irq is clear */
2824: } else {
2825: rc = 3; /* not present or maybe ATAPI */
2826: }
2827: if (drive->select.b.unit != 0) {
2828: OUT_BYTE(0xa0,IDE_SELECT_REG); /* exit with drive0 selected */
2829: delay_50ms();
2830: (void) GET_STAT(); /* ensure drive irq is clear */
2831: }
2832: return rc;
2833: }
2834:
2835: static void enable_nest (ide_drive_t *drive)
2836: {
2837: unsigned long timeout;
2838:
2839: printk("%s: enabling %s -- ", HWIF(drive)->name, drive->id->model);
2840: SELECT_DRIVE(HWIF(drive), drive);
2841: delay_50ms();
2842: OUT_BYTE(EXABYTE_ENABLE_NEST, IDE_COMMAND_REG);
2843: timeout = jiffies + WAIT_WORSTCASE;
2844: do {
2845: if (jiffies > timeout) {
2846: printk("failed (timeout)\n");
2847: return;
2848: }
2849: delay_50ms();
2850: } while (GET_STAT() & BUSY_STAT);
2851: delay_50ms();
2852: if (!OK_STAT(GET_STAT(), 0, BAD_STAT))
2853: printk("failed (status = 0x%02x)\n", GET_STAT());
2854: else
2855: printk("success\n");
2856: if (do_probe(drive, WIN_IDENTIFY) >= 2) { /* if !(success||timed-out) */
2857: #ifdef CONFIG_BLK_DEV_IDEATAPI
2858: (void) do_probe(drive, WIN_PIDENTIFY); /* look for ATAPI device */
2859: #endif /* CONFIG_BLK_DEV_IDEATAPI */
2860: }
2861: }
2862:
2863: /*
2864: * probe_for_drive() tests for existence of a given drive using do_probe().
2865: *
2866: * Returns: 0 no device was found
2867: * 1 device was found (note: drive->present might still be 0)
2868: */
2869: static inline byte probe_for_drive (ide_drive_t *drive)
2870: {
2871: if (drive->noprobe) /* skip probing? */
2872: return drive->present;
2873: if (do_probe(drive, WIN_IDENTIFY) >= 2) { /* if !(success||timed-out) */
2874: #ifdef CONFIG_BLK_DEV_IDEATAPI
2875: (void) do_probe(drive, WIN_PIDENTIFY); /* look for ATAPI device */
2876: #endif /* CONFIG_BLK_DEV_IDEATAPI */
2877: }
1.1.1.2 ! root 2878: if (drive->id && strstr((char *)drive->id->model, "E X A B Y T E N E S T"))
1.1 root 2879: enable_nest(drive);
2880: if (!drive->present)
2881: return 0; /* drive not found */
2882: if (drive->id == NULL) { /* identification failed? */
2883: if (drive->media == ide_disk) {
2884: printk ("%s: non-IDE drive, CHS=%d/%d/%d\n",
2885: drive->name, drive->cyl, drive->head, drive->sect);
2886: }
2887: #ifdef CONFIG_BLK_DEV_IDECD
2888: else if (drive->media == ide_cdrom) {
2889: printk("%s: ATAPI cdrom (?)\n", drive->name);
2890: }
2891: #endif /* CONFIG_BLK_DEV_IDECD */
2892: else {
2893: drive->present = 0; /* nuke it */
2894: }
2895: }
2896: return 1; /* drive was found */
2897: }
2898:
2899: /*
2900: * We query CMOS about hard disks : it could be that we have a SCSI/ESDI/etc
2901: * controller that is BIOS compatible with ST-506, and thus showing up in our
2902: * BIOS table, but not register compatible, and therefore not present in CMOS.
2903: *
2904: * Furthermore, we will assume that our ST-506 drives <if any> are the primary
2905: * drives in the system -- the ones reflected as drive 1 or 2. The first
2906: * drive is stored in the high nibble of CMOS byte 0x12, the second in the low
2907: * nibble. This will be either a 4 bit drive type or 0xf indicating use byte
2908: * 0x19 for an 8 bit type, drive 1, 0x1a for drive 2 in CMOS. A non-zero value
2909: * means we have an AT controller hard disk for that drive.
2910: *
2911: * Of course, there is no guarantee that either drive is actually on the
2912: * "primary" IDE interface, but we don't bother trying to sort that out here.
2913: * If a drive is not actually on the primary interface, then these parameters
2914: * will be ignored. This results in the user having to supply the logical
2915: * drive geometry as a boot parameter for each drive not on the primary i/f.
2916: *
2917: * The only "perfect" way to handle this would be to modify the setup.[cS] code
2918: * to do BIOS calls Int13h/Fn08h and Int13h/Fn48h to get all of the drive info
2919: * for us during initialization. I have the necessary docs -- any takers? -ml
2920: */
2921: static void probe_cmos_for_drives (ide_hwif_t *hwif)
2922: {
2923: #ifdef __i386__
2924: extern struct drive_info_struct drive_info;
2925: byte cmos_disks, *BIOS = (byte *) &drive_info;
2926: int unit;
2927:
2928: #ifdef CONFIG_BLK_DEV_PROMISE
2929: if (hwif->is_promise2)
2930: return;
2931: #endif /* CONFIG_BLK_DEV_PROMISE */
2932: outb_p(0x12,0x70); /* specify CMOS address 0x12 */
2933: cmos_disks = inb_p(0x71); /* read the data from 0x12 */
2934: /* Extract drive geometry from CMOS+BIOS if not already setup */
2935: for (unit = 0; unit < MAX_DRIVES; ++unit) {
2936: ide_drive_t *drive = &hwif->drives[unit];
2937: if ((cmos_disks & (0xf0 >> (unit*4))) && !drive->present && !drive->nobios) {
1.1.1.2 ! root 2938: unsigned short cyl = *(unsigned short *)BIOS;
! 2939: unsigned char head = *(BIOS+2);
! 2940: unsigned char sect = *(BIOS+14);
! 2941: unsigned char ctl = *(BIOS+8);
! 2942: if (cyl > 0 && head > 0 && sect > 0 && sect < 64) {
! 2943: drive->cyl = drive->bios_cyl = cyl;
! 2944: drive->head = drive->bios_head = head;
! 2945: drive->sect = drive->bios_sect = sect;
! 2946: drive->ctl = ctl;
! 2947: drive->present = 1;
! 2948: printk("hd%d: got CHS=%d/%d/%d CTL=%x from BIOS\n",
! 2949: unit, cyl, head, sect, ctl);
! 2950:
! 2951: } else {
! 2952: printk("hd%d: CHS=%d/%d/%d CTL=%x from BIOS ignored\n",
! 2953: unit, cyl, head, sect, ctl);
! 2954: }
1.1 root 2955: }
2956: BIOS += 16;
2957: }
2958: #endif
2959: }
2960:
2961: /*
2962: * This routine only knows how to look for drive units 0 and 1
2963: * on an interface, so any setting of MAX_DRIVES > 2 won't work here.
2964: */
2965: static void probe_hwif (ide_hwif_t *hwif)
2966: {
2967: unsigned int unit;
2968:
2969: if (hwif->noprobe)
2970: return;
2971: if (hwif->io_base == HD_DATA)
2972: probe_cmos_for_drives (hwif);
2973: #if CONFIG_BLK_DEV_PROMISE
2974: if (!hwif->is_promise2 &&
2975: (check_region(hwif->io_base,8) || check_region(hwif->ctl_port,1))) {
2976: #else
2977: if (check_region(hwif->io_base,8) || check_region(hwif->ctl_port,1)) {
2978: #endif /* CONFIG_BLK_DEV_PROMISE */
2979: int msgout = 0;
2980: for (unit = 0; unit < MAX_DRIVES; ++unit) {
2981: ide_drive_t *drive = &hwif->drives[unit];
2982: if (drive->present) {
2983: drive->present = 0;
2984: printk("%s: ERROR, PORTS ALREADY IN USE\n", drive->name);
2985: msgout = 1;
2986: }
2987: }
2988: if (!msgout)
2989: printk("%s: ports already in use, skipping probe\n", hwif->name);
2990: } else {
2991: unsigned long flags;
2992: save_flags(flags);
2993:
2994: sti(); /* needed for jiffies and irq probing */
2995: /*
2996: * Second drive should only exist if first drive was found,
2997: * but a lot of cdrom drives are configured as single slaves.
2998: */
2999: for (unit = 0; unit < MAX_DRIVES; ++unit) {
3000: ide_drive_t *drive = &hwif->drives[unit];
3001: (void) probe_for_drive (drive);
3002: if (drive->present && drive->media == ide_disk) {
3003: if ((!drive->head || drive->head > 16) && !drive->select.b.lba) {
3004: printk("%s: INVALID GEOMETRY: %d PHYSICAL HEADS?\n",
3005: drive->name, drive->head);
3006: drive->present = 0;
3007: }
3008: }
3009: if (drive->present && !hwif->present) {
3010: hwif->present = 1;
3011: request_region(hwif->io_base, 8, hwif->name);
3012: request_region(hwif->ctl_port, 1, hwif->name);
3013: }
3014: }
3015: restore_flags(flags);
3016: for (unit = 0; unit < MAX_DRIVES; ++unit) {
3017: ide_drive_t *drive = &hwif->drives[unit];
3018: if (drive->present && drive->media != ide_tape) {
3019: ide_tuneproc_t *tuneproc = HWIF(drive)->tuneproc;
3020: if (tuneproc != NULL && drive->autotune == 1)
3021: tuneproc(drive, 255); /* auto-tune PIO mode */
3022: }
3023: }
3024: }
3025: }
3026:
3027: /*
3028: * stridx() returns the offset of c within s,
3029: * or -1 if c is '\0' or not found within s.
3030: */
3031: static int stridx (const char *s, char c)
3032: {
3033: char *i = strchr(s, c);
3034: return (i && c) ? i - s : -1;
3035: }
3036:
3037: /*
3038: * match_parm() does parsing for ide_setup():
3039: *
3040: * 1. the first char of s must be '='.
3041: * 2. if the remainder matches one of the supplied keywords,
3042: * the index (1 based) of the keyword is negated and returned.
3043: * 3. if the remainder is a series of no more than max_vals numbers
3044: * separated by commas, the numbers are saved in vals[] and a
3045: * count of how many were saved is returned. Base10 is assumed,
3046: * and base16 is allowed when prefixed with "0x".
3047: * 4. otherwise, zero is returned.
3048: */
3049: static int match_parm (char *s, const char *keywords[], int vals[], int max_vals)
3050: {
3051: static const char *decimal = "0123456789";
3052: static const char *hex = "0123456789abcdef";
3053: int i, n;
3054:
3055: if (*s++ == '=') {
3056: /*
3057: * Try matching against the supplied keywords,
3058: * and return -(index+1) if we match one
3059: */
3060: if (keywords != NULL) {
3061: for (i = 0; *keywords != NULL; ++i) {
3062: if (!strcmp(s, *keywords++))
3063: return -(i+1);
3064: }
3065: }
3066: /*
3067: * Look for a series of no more than "max_vals"
3068: * numeric values separated by commas, in base10,
3069: * or base16 when prefixed with "0x".
3070: * Return a count of how many were found.
3071: */
3072: for (n = 0; (i = stridx(decimal, *s)) >= 0;) {
3073: vals[n] = i;
3074: while ((i = stridx(decimal, *++s)) >= 0)
3075: vals[n] = (vals[n] * 10) + i;
3076: if (*s == 'x' && !vals[n]) {
3077: while ((i = stridx(hex, *++s)) >= 0)
3078: vals[n] = (vals[n] * 0x10) + i;
3079: }
3080: if (++n == max_vals)
3081: break;
3082: if (*s == ',')
3083: ++s;
3084: }
3085: if (!*s)
3086: return n;
3087: }
3088: return 0; /* zero = nothing matched */
3089: }
3090:
3091: /*
3092: * ide_setup() gets called VERY EARLY during initialization,
3093: * to handle kernel "command line" strings beginning with "hdx="
3094: * or "ide". Here is the complete set currently supported:
3095: *
3096: * "hdx=" is recognized for all "x" from "a" to "h", such as "hdc".
3097: * "idex=" is recognized for all "x" from "0" to "3", such as "ide1".
3098: *
3099: * "hdx=noprobe" : drive may be present, but do not probe for it
3100: * "hdx=none" : drive is NOT present, ignore cmos and do not probe
3101: * "hdx=nowerr" : ignore the WRERR_STAT bit on this drive
3102: * "hdx=cdrom" : drive is present, and is a cdrom drive
3103: * "hdx=cyl,head,sect" : disk drive is present, with specified geometry
3104: * "hdx=autotune" : driver will attempt to tune interface speed
3105: * to the fastest PIO mode supported,
3106: * if possible for this drive only.
3107: * Not fully supported by all chipset types,
3108: * and quite likely to cause trouble with
3109: * older/odd IDE drives.
3110: *
3111: * "idebus=xx" : inform IDE driver of VESA/PCI bus speed in Mhz,
3112: * where "xx" is between 20 and 66 inclusive,
3113: * used when tuning chipset PIO modes.
3114: * For PCI bus, 25 is correct for a P75 system,
3115: * 30 is correct for P90,P120,P180 systems,
3116: * and 33 is used for P100,P133,P166 systems.
3117: * If in doubt, use idebus=33 for PCI.
3118: * As for VLB, it is safest to not specify it.
3119: *
3120: * "idex=noprobe" : do not attempt to access/use this interface
3121: * "idex=base" : probe for an interface at the addr specified,
3122: * where "base" is usually 0x1f0 or 0x170
3123: * and "ctl" is assumed to be "base"+0x206
3124: * "idex=base,ctl" : specify both base and ctl
3125: * "idex=base,ctl,irq" : specify base, ctl, and irq number
3126: * "idex=autotune" : driver will attempt to tune interface speed
3127: * to the fastest PIO mode supported,
3128: * for all drives on this interface.
3129: * Not fully supported by all chipset types,
3130: * and quite likely to cause trouble with
3131: * older/odd IDE drives.
3132: * "idex=noautotune" : driver will NOT attempt to tune interface speed
3133: * This is the default for most chipsets,
3134: * except the cmd640.
3135: * "idex=serialize" : do not overlap operations on idex and ide(x^1)
3136: *
3137: * The following are valid ONLY on ide0,
3138: * and the defaults for the base,ctl ports must not be altered.
3139: *
3140: * "ide0=dtc2278" : probe/support DTC2278 interface
3141: * "ide0=ht6560b" : probe/support HT6560B interface
3142: * "ide0=cmd640_vlb" : *REQUIRED* for VLB cards with the CMD640 chip
3143: * (not for PCI -- automatically detected)
3144: * "ide0=qd6580" : probe/support qd6580 interface
3145: * "ide0=ali14xx" : probe/support ali14xx chipsets (ALI M1439, M1443, M1445)
3146: * "ide0=umc8672" : probe/support umc8672 chipsets
3147: */
3148: void ide_setup (char *s)
3149: {
3150: int i, vals[3];
3151: ide_hwif_t *hwif;
3152: ide_drive_t *drive;
3153: unsigned int hw, unit;
3154: const char max_drive = 'a' + ((MAX_HWIFS * MAX_DRIVES) - 1);
3155: const char max_hwif = '0' + (MAX_HWIFS - 1);
3156:
3157: printk("ide_setup: %s", s);
3158: init_ide_data ();
3159:
3160: /*
3161: * Look for drive options: "hdx="
3162: */
3163: if (s[0] == 'h' && s[1] == 'd' && s[2] >= 'a' && s[2] <= max_drive) {
3164: const char *hd_words[] = {"none", "noprobe", "nowerr", "cdrom",
3165: "serialize", "autotune", "noautotune",
3166: "slow", "ide-scsi", NULL};
3167: unit = s[2] - 'a';
3168: hw = unit / MAX_DRIVES;
3169: unit = unit % MAX_DRIVES;
3170: hwif = &ide_hwifs[hw];
3171: drive = &hwif->drives[unit];
3172: switch (match_parm(&s[3], hd_words, vals, 3)) {
3173: case -1: /* "none" */
3174: drive->nobios = 1; /* drop into "noprobe" */
3175: case -2: /* "noprobe" */
3176: drive->noprobe = 1;
3177: goto done;
3178: case -3: /* "nowerr" */
3179: drive->bad_wstat = BAD_R_STAT;
3180: hwif->noprobe = 0;
3181: goto done;
3182: case -4: /* "cdrom" */
3183: drive->present = 1;
3184: drive->media = ide_cdrom;
3185: hwif->noprobe = 0;
3186: goto done;
3187: case -5: /* "serialize" */
3188: printk(" -- USE \"ide%d=serialize\" INSTEAD", hw);
3189: goto do_serialize;
3190: case -6: /* "autotune" */
3191: drive->autotune = 1;
3192: goto done;
3193: case -7: /* "noautotune" */
3194: drive->autotune = 2;
3195: goto done;
3196: case -8: /* "slow" */
3197: drive->slow = 1;
3198: goto done;
3199: case -9: /* "ide-scsi" */
3200: drive->ide_scsi = 1;
3201: goto done;
3202: case 3: /* cyl,head,sect */
3203: drive->media = ide_disk;
3204: drive->cyl = drive->bios_cyl = vals[0];
3205: drive->head = drive->bios_head = vals[1];
3206: drive->sect = drive->bios_sect = vals[2];
3207: drive->present = 1;
3208: drive->forced_geom = 1;
3209: hwif->noprobe = 0;
3210: goto done;
3211: default:
3212: goto bad_option;
3213: }
3214: }
3215:
3216: if (s[0] != 'i' || s[1] != 'd' || s[2] != 'e')
3217: goto bad_option;
3218: /*
3219: * Look for bus speed option: "idebus="
3220: */
3221: if (s[3] == 'b' && s[4] == 'u' && s[5] == 's') {
3222: if (match_parm(&s[6], NULL, vals, 1) != 1)
3223: goto bad_option;
3224: if (vals[0] >= 20 && vals[0] <= 66)
3225: idebus_parameter = vals[0];
3226: else
3227: printk(" -- BAD BUS SPEED! Expected value from 20 to 66");
3228: goto done;
3229: }
3230: /*
3231: * Look for interface options: "idex="
3232: */
3233: if (s[3] >= '0' && s[3] <= max_hwif) {
3234: /*
3235: * Be VERY CAREFUL changing this: note hardcoded indexes below
3236: */
3237: const char *ide_words[] = {"noprobe", "serialize", "autotune", "noautotune",
3238: "qd6580", "ht6560b", "cmd640_vlb", "dtc2278", "umc8672", "ali14xx", "dc4030", NULL};
3239: hw = s[3] - '0';
3240: hwif = &ide_hwifs[hw];
3241: i = match_parm(&s[4], ide_words, vals, 3);
3242:
3243: /*
3244: * Cryptic check to ensure chipset not already set for hwif:
3245: */
3246: if (i > 0 || i <= -5) {
3247: if (hwif->chipset != ide_unknown)
3248: goto bad_option;
3249: if (i <= -5) {
3250: if (ide_hwifs[1].chipset != ide_unknown)
3251: goto bad_option;
3252: /*
3253: * Interface keywords work only for ide0:
3254: */
3255: if (hw != 0)
3256: goto bad_hwif;
3257: }
3258: }
3259:
3260: switch (i) {
3261: #ifdef CONFIG_BLK_DEV_PROMISE
3262: case -11: /* "dc4030" */
3263: {
3264: setup_dc4030(hwif);
3265: goto done;
3266: }
3267: #endif /* CONFIG_BLK_DEV_PROMISE */
3268: #ifdef CONFIG_BLK_DEV_ALI14XX
3269: case -10: /* "ali14xx" */
3270: {
3271: extern void init_ali14xx (void);
3272: init_ali14xx();
3273: goto done;
3274: }
3275: #endif /* CONFIG_BLK_DEV_ALI14XX */
3276: #ifdef CONFIG_BLK_DEV_UMC8672
3277: case -9: /* "umc8672" */
3278: {
3279: extern void init_umc8672 (void);
3280: init_umc8672();
3281: goto done;
3282: }
3283: #endif /* CONFIG_BLK_DEV_UMC8672 */
3284: #ifdef CONFIG_BLK_DEV_DTC2278
3285: case -8: /* "dtc2278" */
3286: {
3287: extern void init_dtc2278 (void);
3288: init_dtc2278();
3289: goto done;
3290: }
3291: #endif /* CONFIG_BLK_DEV_DTC2278 */
3292: #ifdef CONFIG_BLK_DEV_CMD640
3293: case -7: /* "cmd640_vlb" */
3294: {
3295: extern int cmd640_vlb; /* flag for cmd640.c */
3296: cmd640_vlb = 1;
3297: goto done;
3298: }
3299: #endif /* CONFIG_BLK_DEV_CMD640 */
3300: #ifdef CONFIG_BLK_DEV_HT6560B
3301: case -6: /* "ht6560b" */
3302: {
3303: extern void init_ht6560b (void);
3304: init_ht6560b();
3305: goto done;
3306: }
3307: #endif /* CONFIG_BLK_DEV_HT6560B */
3308: #if CONFIG_BLK_DEV_QD6580
3309: case -5: /* "qd6580" (has secondary i/f) */
3310: {
3311: extern void init_qd6580 (void);
3312: init_qd6580();
3313: goto done;
3314: }
3315: #endif /* CONFIG_BLK_DEV_QD6580 */
3316: case -4: /* "noautotune" */
3317: hwif->drives[0].autotune = 2;
3318: hwif->drives[1].autotune = 2;
3319: goto done;
3320: case -3: /* "autotune" */
3321: hwif->drives[0].autotune = 1;
3322: hwif->drives[1].autotune = 1;
3323: goto done;
3324: case -2: /* "serialize" */
3325: do_serialize:
3326: ide_hwifs[hw].serialized = 1; /* serialize */
3327: ide_hwifs[hw^1].serialized = 1; /* with mate */
3328: goto done;
3329:
3330: case -1: /* "noprobe" */
3331: hwif->noprobe = 1;
3332: goto done;
3333:
3334: case 1: /* base */
3335: vals[1] = vals[0] + 0x206; /* default ctl */
3336: case 2: /* base,ctl */
3337: vals[2] = 0; /* default irq = probe for it */
3338: case 3: /* base,ctl,irq */
3339: hwif->io_base = vals[0];
3340: hwif->ctl_port = vals[1];
3341: hwif->irq = vals[2];
3342: hwif->noprobe = 0;
3343: hwif->chipset = ide_generic;
3344: goto done;
3345:
3346: case 0: goto bad_option;
3347: default:
3348: printk(" -- SUPPORT NOT CONFIGURED IN THIS KERNEL\n");
3349: return;
3350: }
3351: }
3352: bad_option:
3353: printk(" -- BAD OPTION\n");
3354: return;
3355: bad_hwif:
3356: printk("-- NOT SUPPORTED ON ide%d", hw);
3357: done:
3358: printk("\n");
3359: }
3360:
3361: /*
3362: * This routine is called from the partition-table code in genhd.c
3363: * to "convert" a drive to a logical geometry with fewer than 1024 cyls.
3364: *
3365: * The second parameter, "xparm", determines exactly how the translation
3366: * will be handled:
3367: * 0 = convert to CHS with fewer than 1024 cyls
3368: * using the same method as Ontrack DiskManager.
3369: * 1 = same as "0", plus offset everything by 63 sectors.
3370: * -1 = similar to "0", plus redirect sector 0 to sector 1.
3371: * >1 = convert to a CHS geometry with "xparm" heads.
3372: *
3373: * Returns 0 if the translation was not possible, if the device was not
3374: * an IDE disk drive, or if a geometry was "forced" on the commandline.
3375: * Returns 1 if the geometry translation was successful.
3376: */
1.1.1.2 ! root 3377:
1.1 root 3378: int ide_xlate_1024 (kdev_t i_rdev, int xparm, const char *msg)
3379: {
3380: ide_drive_t *drive;
3381: static const byte head_vals[] = {4, 8, 16, 32, 64, 128, 255, 0};
3382: const byte *heads = head_vals;
3383: unsigned long tracks;
3384:
1.1.1.2 ! root 3385: drive = get_info_ptr(i_rdev);
! 3386: if (!drive)
! 3387: return 0;
! 3388:
! 3389: if (drive->forced_geom)
1.1 root 3390: return 0;
3391:
3392: if (xparm > 1 && xparm <= drive->bios_head && drive->bios_sect == 63)
3393: return 0; /* we already have a translation */
3394:
3395: printk("%s ", msg);
3396:
1.1.1.2 ! root 3397: if (xparm < 0 && (drive->bios_cyl * drive->bios_head * drive->bios_sect) < (1024 * 16 * 63)) {
! 3398: return 0; /* small disk: no translation needed */
! 3399: }
! 3400:
1.1 root 3401: if (drive->id) {
3402: drive->cyl = drive->id->cyls;
3403: drive->head = drive->id->heads;
3404: drive->sect = drive->id->sectors;
3405: }
3406: drive->bios_cyl = drive->cyl;
3407: drive->bios_head = drive->head;
3408: drive->bios_sect = drive->sect;
3409: drive->special.b.set_geometry = 1;
3410:
3411: tracks = drive->bios_cyl * drive->bios_head * drive->bios_sect / 63;
3412: drive->bios_sect = 63;
3413: if (xparm > 1) {
3414: drive->bios_head = xparm;
3415: drive->bios_cyl = tracks / drive->bios_head;
3416: } else {
3417: while (drive->bios_cyl >= 1024) {
3418: drive->bios_head = *heads;
3419: drive->bios_cyl = tracks / drive->bios_head;
3420: if (0 == *++heads)
3421: break;
3422: }
3423: #if FAKE_FDISK_FOR_EZDRIVE
3424: if (xparm == -1) {
3425: drive->remap_0_to_1 = 1;
3426: msg = "0->1";
3427: } else
3428: #endif /* FAKE_FDISK_FOR_EZDRIVE */
3429: if (xparm == 1) {
3430: drive->sect0 = 63;
3431: drive->bios_cyl = (tracks - 1) / drive->bios_head;
3432: msg = "+63";
3433: }
3434: printk("[remap %s] ", msg);
3435: }
3436: drive->part[0].nr_sects = current_capacity(drive);
3437: printk("[%d/%d/%d]", drive->bios_cyl, drive->bios_head, drive->bios_sect);
3438: return 1;
3439: }
3440:
3441: #if MAX_HWIFS > 1
3442: /*
3443: * save_match() is used to simplify logic in init_irq() below.
3444: *
3445: * A loophole here is that we may not know about a particular
3446: * hwif's irq until after that hwif is actually probed/initialized..
3447: * This could be a problem for the case where an hwif is on a
3448: * dual interface that requires serialization (eg. cmd640) and another
3449: * hwif using one of the same irqs is initialized beforehand.
3450: *
3451: * This routine detects and reports such situations, but does not fix them.
3452: */
3453: static void save_match (ide_hwif_t *hwif, ide_hwif_t *new, ide_hwif_t **match)
3454: {
3455: ide_hwif_t *m = *match;
3456:
3457: if (m && m->hwgroup && m->hwgroup != new->hwgroup) {
3458: if (!new->hwgroup)
3459: return;
3460: printk("%s: potential irq problem with %s and %s\n", hwif->name, new->name, m->name);
3461: }
3462: if (!m || m->irq != hwif->irq) /* don't undo a prior perfect match */
3463: *match = new;
3464: }
3465: #endif /* MAX_HWIFS > 1 */
3466:
3467: /*
3468: * This routine sets up the irq for an ide interface, and creates a new
3469: * hwgroup for the irq/hwif if none was previously assigned.
3470: *
3471: * Much of the code is for correctly detecting/handling irq sharing
3472: * and irq serialization situations. This is somewhat complex because
3473: * it handles static as well as dynamic (PCMCIA) IDE interfaces.
3474: *
3475: * The SA_INTERRUPT in sa_flags means ide_intr() is always entered with
3476: * interrupts completely disabled. This can be bad for interrupt latency,
3477: * but anything else has led to problems on some machines. We re-enable
3478: * interrupts as much as we can safely do in most places.
3479: */
3480: static int init_irq (ide_hwif_t *hwif)
3481: {
3482: unsigned long flags;
3483: #if MAX_HWIFS > 1
3484: unsigned int index;
3485: #endif /* MAX_HWIFS > 1 */
3486: ide_hwgroup_t *hwgroup;
3487: ide_hwif_t *match = NULL;
3488:
3489: save_flags(flags);
3490: cli();
3491:
3492: hwif->hwgroup = NULL;
3493: #if MAX_HWIFS > 1
3494: /*
3495: * Group up with any other hwifs that share our irq(s).
3496: */
3497: for (index = 0; index < MAX_HWIFS; index++) {
3498: ide_hwif_t *h = &ide_hwifs[index];
3499: if (h->hwgroup) { /* scan only initialized hwif's */
3500: if (hwif->irq == h->irq) {
3501: hwif->sharing_irq = h->sharing_irq = 1;
3502: save_match(hwif, h, &match);
3503: }
3504: if (hwif->serialized) {
3505: ide_hwif_t *mate = &ide_hwifs[hwif->index^1];
3506: if (index == mate->index || h->irq == mate->irq)
3507: save_match(hwif, h, &match);
3508: }
3509: if (h->serialized) {
3510: ide_hwif_t *mate = &ide_hwifs[h->index^1];
3511: if (hwif->irq == mate->irq)
3512: save_match(hwif, h, &match);
3513: }
3514: }
3515: }
3516: #endif /* MAX_HWIFS > 1 */
3517: /*
3518: * If we are still without a hwgroup, then form a new one
3519: */
3520: if (match) {
3521: hwgroup = match->hwgroup;
3522: } else {
3523: hwgroup = kmalloc(sizeof(ide_hwgroup_t), GFP_KERNEL);
3524: hwgroup->hwif = hwgroup->next_hwif = hwif->next = hwif;
3525: hwgroup->rq = NULL;
3526: hwgroup->handler = NULL;
3527: if (hwif->drives[0].present)
3528: hwgroup->drive = &hwif->drives[0];
3529: else
3530: hwgroup->drive = &hwif->drives[1];
3531: hwgroup->poll_timeout = 0;
3532: hwgroup->active = 0;
3533: init_timer(&hwgroup->timer);
3534: hwgroup->timer.function = &timer_expiry;
3535: hwgroup->timer.data = (unsigned long) hwgroup;
3536: }
3537:
3538: /*
3539: * Allocate the irq, if not already obtained for another hwif
3540: */
3541: if (!match || match->irq != hwif->irq) {
3542: if (request_irq(hwif->irq, ide_intr, SA_INTERRUPT, hwif->name, hwgroup)) {
3543: if (!match)
3544: kfree(hwgroup);
3545: restore_flags(flags);
3546: return 1;
3547: }
3548: }
3549:
3550: /*
3551: * Everything is okay, so link us into the hwgroup
3552: */
3553: hwif->hwgroup = hwgroup;
3554: hwif->next = hwgroup->hwif->next;
3555: hwgroup->hwif->next = hwif;
3556:
3557: restore_flags(flags); /* safe now that hwif->hwgroup is set up */
3558:
3559: printk("%s at 0x%03x-0x%03x,0x%03x on irq %d", hwif->name,
3560: hwif->io_base, hwif->io_base+7, hwif->ctl_port, hwif->irq);
3561: if (match)
3562: printk(" (%sed with %s)", hwif->sharing_irq ? "shar" : "serializ", match->name);
3563: printk("\n");
3564: return 0;
3565: }
3566:
3567: static struct file_operations ide_fops = {
3568: NULL, /* lseek - default */
3569: block_read, /* read - general block-dev read */
3570: block_write, /* write - general block-dev write */
3571: NULL, /* readdir - bad */
3572: NULL, /* select */
3573: ide_ioctl, /* ioctl */
3574: NULL, /* mmap */
3575: ide_open, /* open */
3576: ide_release, /* release */
3577: block_fsync /* fsync */
3578: ,NULL, /* fasync */
3579: ide_check_media_change, /* check_media_change */
3580: revalidate_disk /* revalidate */
3581: };
3582:
3583: #ifdef CONFIG_PCI
3584: #if defined(CONFIG_BLK_DEV_RZ1000) || defined(CONFIG_BLK_DEV_TRITON)
3585:
3586: typedef void (ide_pci_init_proc_t)(byte, byte);
3587:
3588: /*
3589: * ide_probe_pci() scans PCI for a specific vendor/device function,
3590: * and invokes the supplied init routine for each instance detected.
3591: */
3592: static void ide_probe_pci (unsigned short vendor, unsigned short device, ide_pci_init_proc_t *init, int func_adj)
3593: {
3594: unsigned long flags;
3595: unsigned index;
3596: byte fn, bus;
3597:
3598: save_flags(flags);
3599: cli();
3600: for (index = 0; !pcibios_find_device (vendor, device, index, &bus, &fn); ++index) {
3601: init (bus, fn + func_adj);
3602: }
3603: restore_flags(flags);
3604: }
3605:
3606: #endif /* defined(CONFIG_BLK_DEV_RZ1000) || defined(CONFIG_BLK_DEV_TRITON) */
3607:
3608: static void ide_probe_promise_20246(void)
3609: {
3610: byte fn, bus;
3611: unsigned short io[6], count = 0;
3612: unsigned int reg, tmp, i;
3613: ide_hwif_t *hwif;
3614:
3615: memset(io, 0, 6 * sizeof(unsigned short));
3616: if (pcibios_find_device(PCI_VENDOR_ID_PROMISE, PCI_DEVICE_ID_PROMISE_20246, 0, &bus, &fn))
3617: return;
3618: printk("ide: Promise Technology IDE Ultra-DMA 33 on PCI bus %d function %d\n", bus, fn);
3619: for (reg = PCI_BASE_ADDRESS_0; reg <= PCI_BASE_ADDRESS_5; reg += 4) {
3620: pcibios_read_config_dword(bus, fn, reg, &tmp);
3621: if (tmp & PCI_BASE_ADDRESS_SPACE_IO)
3622: io[count++] = tmp & PCI_BASE_ADDRESS_IO_MASK;
3623: }
3624: for (i = 2; i < 4; i++) {
3625: hwif = ide_hwifs + i;
3626: if (hwif->chipset == ide_generic) {
3627: printk("ide%d: overridden with command line parameter\n", i);
3628: return;
3629: }
3630: tmp = (i - 2) * 2;
3631: if (!io[tmp] || !io[tmp + 1]) {
3632: printk("ide%d: invalid port address %x, %x -- aborting\n", i, io[tmp], io[tmp + 1]);
3633: return;
3634: }
3635: hwif->io_base = io[tmp];
3636: hwif->ctl_port = io[tmp + 1] + 2;
3637: hwif->noprobe = 0;
3638: }
3639: }
3640:
3641: #endif /* CONFIG_PCI */
3642:
3643: /*
3644: * ide_init_pci() finds/initializes "known" PCI IDE interfaces
3645: *
3646: * This routine should ideally be using pcibios_find_class() to find
3647: * all IDE interfaces, but that function causes some systems to "go weird".
3648: */
3649: static void probe_for_hwifs (void)
3650: {
3651: #ifdef CONFIG_PCI
3652: /*
3653: * Find/initialize PCI IDE interfaces
3654: */
3655: if (pcibios_present()) {
3656: #ifdef CONFIG_BLK_DEV_RZ1000
3657: ide_pci_init_proc_t init_rz1000;
3658: ide_probe_pci (PCI_VENDOR_ID_PCTECH, PCI_DEVICE_ID_PCTECH_RZ1000, &init_rz1000, 0);
3659: ide_probe_pci (PCI_VENDOR_ID_PCTECH, PCI_DEVICE_ID_PCTECH_RZ1001, &init_rz1000, 0);
3660: #endif /* CONFIG_BLK_DEV_RZ1000 */
3661: #ifdef CONFIG_BLK_DEV_TRITON
3662: /*
3663: * Apparently the BIOS32 services on Intel motherboards are
3664: * buggy and won't find the PCI_DEVICE_ID_INTEL_82371_1 for us.
3665: * So instead, we search for PCI_DEVICE_ID_INTEL_82371_0,
3666: * and then add 1.
3667: */
3668: ide_probe_pci (PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82371_0, &ide_init_triton, 1);
3669: ide_probe_pci (PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82371SB_1, &ide_init_triton, 0);
3670: ide_probe_pci (PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82371AB, &ide_init_triton, 0);
1.1.1.2 ! root 3671: ide_probe_pci (PCI_VENDOR_ID_SI, PCI_DEVICE_ID_SI_5513, &ide_init_triton, 0);
! 3672: ide_probe_pci (PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_82C586_1, &ide_init_triton, 0);
! 3673: ide_probe_pci (PCI_VENDOR_ID_AL, PCI_DEVICE_ID_AL_M5229, &ide_init_triton, 0);
1.1 root 3674: #endif /* CONFIG_BLK_DEV_TRITON */
3675: ide_probe_promise_20246();
3676: }
3677: #endif /* CONFIG_PCI */
3678: #ifdef CONFIG_BLK_DEV_CMD640
3679: {
3680: extern void ide_probe_for_cmd640x (void);
3681: ide_probe_for_cmd640x();
3682: }
3683: #endif
3684: #ifdef CONFIG_BLK_DEV_PROMISE
3685: init_dc4030();
3686: #endif
1.1.1.2 ! root 3687: ahci_probe_pci();
1.1 root 3688: }
3689:
3690: static int hwif_init (int h)
3691: {
3692: ide_hwif_t *hwif = &ide_hwifs[h];
3693: void (*rfn)(void);
1.1.1.2 ! root 3694:
1.1 root 3695: if (!hwif->present)
3696: return 0;
3697: if (!hwif->irq) {
3698: if (!(hwif->irq = default_irqs[h])) {
3699: printk("%s: DISABLED, NO IRQ\n", hwif->name);
3700: return (hwif->present = 0);
3701: }
3702: }
3703: #ifdef CONFIG_BLK_DEV_HD
3704: if (hwif->irq == HD_IRQ && hwif->io_base != HD_DATA) {
3705: printk("%s: CANNOT SHARE IRQ WITH OLD HARDDISK DRIVER (hd.c)\n", hwif->name);
3706: return (hwif->present = 0);
3707: }
3708: #endif /* CONFIG_BLK_DEV_HD */
1.1.1.2 ! root 3709:
1.1 root 3710: hwif->present = 0; /* we set it back to 1 if all is ok below */
3711: switch (hwif->major) {
3712: case IDE0_MAJOR: rfn = &do_ide0_request; break;
3713: #if MAX_HWIFS > 1
3714: case IDE1_MAJOR: rfn = &do_ide1_request; break;
3715: #endif
3716: #if MAX_HWIFS > 2
3717: case IDE2_MAJOR: rfn = &do_ide2_request; break;
3718: #endif
3719: #if MAX_HWIFS > 3
3720: case IDE3_MAJOR: rfn = &do_ide3_request; break;
3721: #endif
3722: default:
3723: printk("%s: request_fn NOT DEFINED\n", hwif->name);
3724: return (hwif->present = 0);
3725: }
3726: if (register_blkdev (hwif->major, hwif->name, &ide_fops)) {
3727: printk("%s: UNABLE TO GET MAJOR NUMBER %d\n", hwif->name, hwif->major);
3728: } else if (init_irq (hwif)) {
3729: printk("%s: UNABLE TO GET IRQ %d\n", hwif->name, hwif->irq);
3730: (void) unregister_blkdev (hwif->major, hwif->name);
3731: } else {
3732: init_gendisk(hwif);
3733: blk_dev[hwif->major].request_fn = rfn;
3734: read_ahead[hwif->major] = 8; /* (4kB) */
3735: hwif->present = 1; /* success */
3736: }
3737: return hwif->present;
3738: }
3739:
3740: /*
3741: * This is gets invoked once during initialization, to set *everything* up
3742: */
3743: int ide_init (void)
3744: {
3745: int index;
3746:
3747: init_ide_data ();
3748: /*
3749: * Probe for special "known" interface chipsets
3750: */
3751: probe_for_hwifs ();
3752:
3753: /*
3754: * Probe for drives in the usual way.. CMOS/BIOS, then poke at ports
3755: */
3756: for (index = 0; index < MAX_HWIFS; ++index)
3757: probe_hwif (&ide_hwifs[index]);
3758: for (index = 0; index < MAX_HWIFS; ++index)
3759: hwif_init (index);
3760:
3761: #ifdef CONFIG_BLK_DEV_IDETAPE
3762: idetape_register_chrdev(); /* Register character device interface to the ide tape */
3763: #endif /* CONFIG_BLK_DEV_IDETAPE */
1.1.1.2 ! root 3764:
1.1 root 3765: return 0;
3766: }
3767:
3768: #ifdef CONFIG_BLK_DEV_IDE_PCMCIA
3769: int ide_register(int io_base, int ctl_port, int irq)
3770: {
3771: int index, i, rc = -1;
3772: ide_hwif_t *hwif;
3773: ide_drive_t *drive;
3774: unsigned long flags;
3775:
3776: save_flags(flags);
3777: cli();
3778: for (index = 0; index < MAX_HWIFS; ++index) {
3779: hwif = &ide_hwifs[index];
3780: if (hwif->present) {
3781: if (hwif->io_base == io_base || hwif->ctl_port == ctl_port)
3782: break; /* this ide port already exists */
3783: } else {
3784: hwif->io_base = io_base;
3785: hwif->ctl_port = ctl_port;
3786: hwif->irq = irq;
3787: hwif->noprobe = 0;
3788: probe_hwif(hwif);
3789: if (!hwif_init(index))
3790: break;
3791: for (i = 0; i < hwif->gd->nr_real; i++) {
3792: drive = &hwif->drives[i];
3793: revalidate_disk(MKDEV(hwif->major, i<<PARTN_BITS));
3794: #ifdef CONFIG_BLK_DEV_IDECD
3795: if (drive->present && drive->media == ide_cdrom)
3796: ide_cdrom_setup(drive);
3797: #endif /* CONFIG_BLK_DEV_IDECD */
3798: }
3799: rc = index;
3800: break;
3801: }
3802: }
3803: restore_flags(flags);
3804: return rc;
3805: }
3806:
3807: void ide_unregister (unsigned int index)
3808: {
3809: struct gendisk *gd, **gdp;
3810: ide_hwif_t *hwif, *g;
3811: ide_hwgroup_t *hwgroup;
3812: int irq_count = 0;
3813: unsigned long flags;
3814:
3815: if (index >= MAX_HWIFS)
3816: return;
3817: save_flags(flags);
3818: cli();
3819: hwif = &ide_hwifs[index];
3820: if (!hwif->present || hwif->drives[0].busy || hwif->drives[1].busy) {
3821: restore_flags(flags);
3822: return;
3823: }
3824: hwif->present = 0;
3825: hwgroup = hwif->hwgroup;
3826:
3827: /*
3828: * free the irq if we were the only hwif using it
3829: */
3830: g = hwgroup->hwif;
3831: do {
3832: if (g->irq == hwif->irq)
3833: ++irq_count;
3834: g = g->next;
3835: } while (g != hwgroup->hwif);
3836: if (irq_count == 1)
3837: free_irq(hwif->irq, hwgroup);
3838:
3839: /*
3840: * Note that we only release the standard ports,
3841: * and do not even try to handle any extra ports
3842: * allocated for weird IDE interface chipsets.
3843: */
3844: release_region(hwif->io_base, 8);
3845: release_region(hwif->ctl_port, 1);
3846:
3847: /*
3848: * Remove us from the hwgroup, and free
3849: * the hwgroup if we were the only member
3850: */
3851: while (hwgroup->hwif->next != hwif)
3852: hwgroup->hwif = hwgroup->hwif->next;
3853: hwgroup->hwif->next = hwif->next;
3854: if (hwgroup->hwif == hwif)
3855: hwgroup->hwif = hwif->next;
3856: if (hwgroup->next_hwif == hwif)
3857: hwgroup->next_hwif = hwif->next;
3858: if (hwgroup->hwif == hwif)
3859: kfree(hwgroup);
3860:
3861: /*
3862: * Remove us from the kernel's knowledge
3863: */
3864: unregister_blkdev(hwif->major, hwif->name);
3865: kfree(blksize_size[hwif->major]);
3866: blk_dev[hwif->major].request_fn = NULL;
3867: blksize_size[hwif->major] = NULL;
3868: for (gdp = &gendisk_head; *gdp; gdp = &((*gdp)->next))
3869: if (*gdp == hwif->gd)
3870: break;
3871: if (*gdp == NULL)
3872: printk("gd not in disk chain!\n");
3873: else {
3874: gd = *gdp; *gdp = gd->next;
3875: kfree(gd->sizes);
3876: kfree(gd->part);
3877: kfree(gd);
3878: }
3879: init_hwif_data (index); /* restore hwif data to pristine status */
3880: restore_flags(flags);
3881: }
3882: #endif /* CONFIG_BLK_DEV_IDE_PCMCIA */
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.