1 /*
2 * linux/drivers/block/ide.c Version 5.13 Sep 4, 1995
3 *
4 * Copyright (C) 1994, 1995 Linus Torvalds & authors (see below)
5 */
6
7 /*
8 * This is the multiple IDE interface driver, as evolved from hd.c.
9 * It supports up to four IDE interfaces, on one or more IRQs (usually 14 & 15).
10 * There can be up to two drives per interface, as per the ATA-2 spec.
11 *
12 * Primary i/f: ide0: major=3; (hda) minor=0; (hdb) minor=64
13 * Secondary i/f: ide1: major=22; (hdc or hd1a) minor=0; (hdd or hd1b) minor=64
14 * Tertiary i/f: ide2: major=33; (hde) minor=0; (hdf) minor=64
15 * Quaternary i/f: ide3: major=34; (hdg) minor=0; (hdh) minor=64
16 *
17 * From hd.c:
18 * |
19 * | It traverses the request-list, using interrupts to jump between functions.
20 * | As nearly all functions can be called within interrupts, we may not sleep.
21 * | Special care is recommended. Have Fun!
22 * |
23 * | modified by Drew Eckhardt to check nr of hd's from the CMOS.
24 * |
25 * | Thanks to Branko Lankester, lankeste@fwi.uva.nl, who found a bug
26 * | in the early extended-partition checks and added DM partitions.
27 * |
28 * | Early work on error handling by Mika Liljeberg (liljeber@cs.Helsinki.FI).
29 * |
30 * | IRQ-unmask, drive-id, multiple-mode, support for ">16 heads",
31 * | and general streamlining by Mark Lord (mlord@bnr.ca).
32 *
33 * October, 1994 -- Complete line-by-line overhaul for linux 1.1.x, by:
34 *
35 * Mark Lord (mlord@bnr.ca) (IDE Perf.Pkg)
36 * Delman Lee (delman@mipg.upenn.edu) ("Mr. atdisk2")
37 * Petri Mattila (ptjmatti@kruuna.helsinki.fi) (EIDE stuff)
38 * Scott Snyder (snyder@fnald0.fnal.gov) (ATAPI IDE cd-rom)
39 *
40 * Maintained by Mark Lord (mlord@bnr.ca): ide.c, ide.h, triton.c, hd.c, ..
41 *
42 * This was a rewrite of just about everything from hd.c, though some original
43 * code is still sprinkled about. Think of it as a major evolution, with
44 * inspiration from lots of linux users, esp. hamish@zot.apana.org.au
45 *
46 * Version 1.0 ALPHA initial code, primary i/f working okay
47 * Version 1.3 BETA dual i/f on shared irq tested & working!
48 * Version 1.4 BETA added auto probing for irq(s)
49 * Version 1.5 BETA added ALPHA (untested) support for IDE cd-roms,
50 * ...
51 * Version 3.5 correct the bios_cyl field if it's too small
52 * (linux 1.1.76) (to help fdisk with brain-dead BIOSs)
53 * Version 3.6 cosmetic corrections to comments and stuff
54 * (linux 1.1.77) reorganise probing code to make it understandable
55 * added halfway retry to probing for drive identification
56 * added "hdx=noprobe" command line option
57 * allow setting multmode even when identification fails
58 * Version 3.7 move set_geometry=1 from do_identify() to ide_init()
59 * increase DRQ_WAIT to eliminate nuisance messages
60 * wait for DRQ_STAT instead of DATA_READY during probing
61 * (courtesy of Gary Thomas gary@efland.UU.NET)
62 * Version 3.8 fixed byte-swapping for confused Mitsumi cdrom drives
63 * update of ide-cd.c from Scott, allows blocksize=1024
64 * cdrom probe fixes, inspired by jprang@uni-duisburg.de
65 * Version 3.9 don't use LBA if lba_capacity looks funny
66 * correct the drive capacity calculations
67 * fix probing for old Seagates without IDE_ALTSTATUS_REG
68 * fix byte-ordering for some NEC cdrom drives
69 * Version 3.10 disable multiple mode by default; was causing trouble
70 * Version 3.11 fix mis-identification of old WD disks as cdroms
71 * Version 3,12 simplify logic for selecting initial mult_count
72 * (fixes problems with buggy WD drives)
73 * Version 3.13 remove excess "multiple mode disabled" messages
74 * Version 3.14 fix ide_error() handling of BUSY_STAT
75 * fix byte-swapped cdrom strings (again.. arghh!)
76 * ignore INDEX bit when checking the ALTSTATUS reg
77 * Version 3.15 add SINGLE_THREADED flag for use with dual-CMD i/f
78 * ignore WRERR_STAT for non-write operations
79 * added vlb_sync support for DC-2000A & others,
80 * (incl. some Promise chips), courtesy of Frank Gockel
81 * Version 3.16 convert vlb_32bit and vlb_sync into runtime flags
82 * add ioctls to get/set VLB flags (HDIO_[SG]ET_CHIPSET)
83 * rename SINGLE_THREADED to SUPPORT_SERIALIZE,
84 * add boot flag to "serialize" operation for CMD i/f
85 * add optional support for DTC2278 interfaces,
86 * courtesy of andy@cercle.cts.com (Dyan Wile).
87 * add boot flag to enable "dtc2278" probe
88 * add probe to avoid EATA (SCSI) interfaces,
89 * courtesy of neuffer@goofy.zdv.uni-mainz.de.
90 * Version 4.00 tidy up verify_area() calls - heiko@colossus.escape.de
91 * add flag to ignore WRERR_STAT for some drives
92 * courtesy of David.H.West@um.cc.umich.edu
93 * assembly syntax tweak to vlb_sync
94 * removeable drive support from scuba@cs.tu-berlin.de
95 * add transparent support for DiskManager-6.0x "Dynamic
96 * Disk Overlay" (DDO), most of this in in genhd.c
97 * eliminate "multiple mode turned off" message at boot
98 * Version 4.10 fix bug in ioctl for "hdparm -c3"
99 * fix DM6:DDO support -- now works with LILO, fdisk, ...
100 * don't treat some naughty WD drives as removeable
101 * Version 4.11 updated DM6 support using info provided by OnTrack
102 * Version 5.00 major overhaul, multmode setting fixed, vlb_sync fixed
103 * added support for 3rd/4th/alternative IDE ports
104 * created ide.h; ide-cd.c now compiles separate from ide.c
105 * hopefully fixed infinite "unexpected_intr" from cdroms
106 * zillions of other changes and restructuring
107 * somehow reduced overall memory usage by several kB
108 * probably slowed things down slightly, but worth it
109 * Version 5.01 AT LAST!! Finally understood why "unexpected_intr"
110 * was happening at various times/places: whenever the
111 * ide-interface's ctl_port was used to "mask" the irq,
112 * it also would trigger an edge in the process of masking
113 * which would result in a self-inflicted interrupt!!
114 * (such a stupid way to build a hardware interrupt mask).
115 * This is now fixed (after a year of head-scratching).
116 * Version 5.02 got rid of need for {enable,disable}_irq_list()
117 * Version 5.03 tune-ups, comments, remove "busy wait" from drive resets
118 * removed PROBE_FOR_IRQS option -- no longer needed
119 * OOOPS! fixed "bad access" bug for 2nd drive on an i/f
120 * Version 5.04 changed "ira %d" to "irq %d" in DEBUG message
121 * added more comments, cleaned up unexpected_intr()
122 * OOOPS! fixed null pointer problem in ide reset code
123 * added autodetect for Triton chipset -- no effect yet
124 * Version 5.05 OOOPS! fixed bug in revalidate_disk()
125 * OOOPS! fixed bug in ide_do_request()
126 * added ATAPI reset sequence for cdroms
127 * Version 5.10 added Bus-Mastered DMA support for Triton Chipset
128 * some (mostly) cosmetic changes
129 * Version 5.11 added ht6560b support by malafoss@snakemail.hut.fi
130 * reworked PCI scanning code
131 * added automatic RZ1000 detection/support
132 * added automatic PCI CMD640 detection/support
133 * added option for VLB CMD640 support
134 * tweaked probe to find cdrom on hdb with disks on hda,hdc
135 * Version 5.12 some performance tuning
136 * added message to alert user to bad /dev/hd[cd] entries
137 * OOOPS! fixed bug in atapi reset
138 * driver now forces "serialize" again for all cmd640 chips
139 * noticed REALLY_SLOW_IO had no effect, moved it to ide.c
140 * made do_drive_cmd() into public ide_do_drive_cmd()
141 * Version 5.13 fixed typo ('B'), thanks to houston@boyd.geog.mcgill.ca
142 * fixed ht6560b support
143 *
144 * Driver compile-time options are in ide.h
145 *
146 * To do, in likely order of completion:
147 * - add ioctls to get/set interface timings on cmd640, ht6560b, triton
148 * - modify kernel to obtain BIOS geometry for drives on 2nd/3rd/4th i/f
149 * - improved CMD support: probably handing this off to someone else
150 * - find someone to work on IDE *tape drive* support
151 */
152
153 #undef REALLY_SLOW_IO /* most systems can safely undef this */
154
155 #include <linux/config.h>
156 #include <linux/types.h>
157 #include <linux/string.h>
158 #include <linux/kernel.h>
159 #include <linux/delay.h>
160 #include <linux/timer.h>
161 #include <linux/mm.h>
162 #include <linux/ioport.h>
163 #include <linux/interrupt.h>
164 #include <linux/major.h>
165 #include <linux/blkdev.h>
166 #include <linux/errno.h>
167 #include <linux/hdreg.h>
168 #include <linux/genhd.h>
169 #include <asm/byteorder.h>
170 #include <asm/irq.h>
171 #include <asm/segment.h>
172 #include <asm/io.h>
173
174 #ifdef CONFIG_PCI
175 #include <linux/bios32.h>
176 #include <linux/pci.h>
177 #endif /* CONFIG_PCI */
178
179 #include "ide.h"
180
181 ide_hwif_t ide_hwifs[MAX_HWIFS]; /* hwif info */
182 static ide_hwgroup_t *irq_to_hwgroup [16];
183 static const byte ide_hwif_to_major[MAX_HWIFS] = {IDE0_MAJOR, IDE1_MAJOR, IDE2_MAJOR, IDE3_MAJOR};
184
185 static const unsigned short default_io_base[MAX_HWIFS] = {0x1f0, 0x170, 0x1e8, 0x168};
186 static const byte default_irqs[MAX_HWIFS] = {14, 15, 11, 10};
187 static int single_threaded = 0; /* "serialize" option */
188 static int disallow_unmask = 0; /* for buggy hardware */
189
190 #if (DISK_RECOVERY_TIME > 0)
191 /*
192 * For really screwy hardware (hey, at least it *can* be used with Linux)
193 * we can enforce a minimum delay time between successive operations.
194 */
195 static unsigned long read_timer(void)
/* ![[previous]](../icons/n_left.png)
![[next]](../icons/right.png)
![[first]](../icons/n_first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
196 {
197 unsigned long t, flags;
198 int i;
199
200 save_flags(flags);
201 cli();
202 t = jiffies * 11932;
203 outb_p(0, 0x43);
204 i = inb_p(0x40);
205 i |= inb(0x40) << 8;
206 restore_flags(flags);
207 return (t - i);
208 }
209
210 void ide_set_recovery_timer (ide_hwif_t *hwif)
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
211 {
212 hwif->last_time = read_timer();
213 }
214 #endif /* DISK_RECOVERY_TIME */
215
216 /*
217 * init_ide_data() sets reasonable default values into all fields
218 * of all instances of the hwifs and drives, but only on the first call.
219 * Subsequent calls have no effect (they don't wipe out anything).
220 *
221 * This routine is normally called at driver initialization time,
222 * but may also be called MUCH earlier during kernel "command-line"
223 * parameter processing. As such, we cannot depend on any other parts
224 * of the kernel (such as memory allocation) to be functioning yet.
225 *
226 * This is too bad, as otherwise we could dynamically allocate the
227 * ide_drive_t structs as needed, rather than always consuming memory
228 * for the max possible number (MAX_HWIFS * MAX_DRIVES) of them.
229 */
230 #define MAGIC_COOKIE 0x12345678
231 static void init_ide_data (void)
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
232 {
233 byte *p;
234 unsigned int h, unit;
235 static unsigned long magic_cookie = MAGIC_COOKIE;
236
237 if (magic_cookie != MAGIC_COOKIE)
238 return; /* already initialized */
239 magic_cookie = 0;
240
241 for (h = 0; h < 16; ++h)
242 irq_to_hwgroup[h] = NULL;
243
244 /* bulk initialize hwif & drive info with zeros */
245 p = ((byte *) ide_hwifs) + sizeof(ide_hwifs);
246 do {
247 *--p = 0;
248 } while (p > (byte *) ide_hwifs);
249
250 for (h = 0; h < MAX_HWIFS; ++h) {
251 ide_hwif_t *hwif = &ide_hwifs[h];
252
253 /* fill in any non-zero initial values */
254 hwif->noprobe = (h > 1);
255 hwif->io_base = default_io_base[h];
256 hwif->ctl_port = hwif->io_base ? hwif->io_base+0x206 : 0x000;
257 #ifdef CONFIG_BLK_DEV_HD
258 if (hwif->io_base == HD_DATA)
259 hwif->noprobe = 1; /* may be overriden by ide_setup() */
260 #endif /* CONFIG_BLK_DEV_HD */
261 hwif->major = ide_hwif_to_major[h];
262 hwif->name[0] = 'i';
263 hwif->name[1] = 'd';
264 hwif->name[2] = 'e';
265 hwif->name[3] = '0' + h;
266
267 for (unit = 0; unit < MAX_DRIVES; ++unit) {
268 ide_drive_t *drive = &hwif->drives[unit];
269
270 /* fill in any non-zero initial values */
271 drive->select.all = (unit<<4)|0xa0;
272 drive->hwif = hwif;
273 drive->ctl = 0x08;
274 drive->ready_stat = READY_STAT;
275 drive->bad_wstat = BAD_W_STAT;
276 drive->special.b.recalibrate = 1;
277 drive->special.b.set_geometry = 1;
278 drive->name[0] = 'h';
279 drive->name[1] = 'd';
280 drive->name[2] = 'a' + (h * MAX_DRIVES) + unit;
281 }
282 }
283 }
284
285 #define VLB_SYNC 1
286 /*
287 * Some localbus EIDE interfaces require a special access sequence
288 * when using 32-bit I/O instructions to transfer data. We call this
289 * the "vlb_sync" sequence, which consists of three successive reads
290 * of the sector count register location, with interrupts disabled
291 * to ensure that the reads all happen together.
292 */
293 static inline void do_vlb_sync (unsigned short port) {
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
294 (void) inb (port);
295 (void) inb (port);
296 (void) inb (port);
297 }
298
299 /*
300 * This is used for most PIO data transfers *from* the IDE interface
301 */
302 void ide_input_data (ide_drive_t *drive, void *buffer, unsigned int wcount)
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
303 {
304 unsigned short io_base = HWIF(drive)->io_base;
305 unsigned short data_reg = io_base+IDE_DATA_OFFSET;
306
307 if (drive->vlb_32bit) {
308 #ifdef VLB_SYNC
309 if (drive->vlb_sync) {
310 cli();
311 do_vlb_sync(io_base+IDE_NSECTOR_OFFSET);
312 insl(data_reg, buffer, wcount);
313 if (drive->unmask)
314 sti();
315 } else
316 #endif /* VLB_SYNC */
317 insl(data_reg, buffer, wcount);
318 } else
319 insw(data_reg, buffer, wcount<<1);
320 }
321
322 /*
323 * This is used for most PIO data transfers *to* the IDE interface
324 */
325 void ide_output_data (ide_drive_t *drive, void *buffer, unsigned int wcount)
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
326 {
327 unsigned short io_base = HWIF(drive)->io_base;
328 unsigned short data_reg = io_base+IDE_DATA_OFFSET;
329
330 if (drive->vlb_32bit) {
331 #ifdef VLB_SYNC
332 if (drive->vlb_sync) {
333 cli();
334 do_vlb_sync(io_base+IDE_NSECTOR_OFFSET);
335 outsl(data_reg, buffer, wcount);
336 if (drive->unmask)
337 sti();
338 } else
339 #endif /* VLB_SYNC */
340 outsl(data_reg, buffer, wcount);
341 } else
342 outsw(data_reg, buffer, wcount<<1);
343 }
344
345 #if SUPPORT_HT6560B
346 /*
347 * This routine handles interface switching for the peculiar hardware design
348 * on the F.G.I./Holtek HT-6560B VLB IDE interface.
349 * The HT-6560B can only enable one IDE port at a time, and requires a
350 * silly sequence (below) whenever we switch between primary and secondary.
351 *
352 * Apparently, systems with multiple CMD640 chips may need something similar..
353 *
354 * This algorithm courtesy of malafoss@snakemail.hut.fi
355 */
356
357 void ide_hwif_select (ide_hwif_t *hwif)
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
358 {
359 static byte current_select = 0;
360
361 if (hwif->select != current_select) {
362 unsigned long flags;
363 save_flags (flags);
364 cli();
365 current_select = hwif->select;
366 (void) inb(0x3e6);
367 (void) inb(0x3e6);
368 (void) inb(0x3e6);
369 (void) inb(0x3e6);
370 outb(current_select,0x3e6);
371 restore_flags (flags);
372 }
373 }
374 #endif /* SUPPORT_HT6560B */
375
376 /*
377 * This should get invoked any time we exit the driver to
378 * wait for an interrupt response from a drive. handler() points
379 * at the appropriate code to handle the next interrupt, and a
380 * timer is started to prevent us from waiting forever in case
381 * something goes wrong (see the timer_expiry() handler later on).
382 */
383 void ide_set_handler (ide_drive_t *drive, ide_handler_t *handler)
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
384 {
385 ide_hwgroup_t *hwgroup = HWGROUP(drive);
386 #ifdef DEBUG
387 if (hwgroup->handler != NULL)
388 printk("%s: ide_set_handler: old handler not null\n", drive->name);
389 #endif
390 hwgroup->handler = handler;
391 hwgroup->timer.expires = jiffies + WAIT_CMD;
392 add_timer(&(hwgroup->timer));
393 }
394
395 /*
396 * lba_capacity_is_ok() performs a sanity check on the claimed "lba_capacity"
397 * value for this drive (from its reported identification information).
398 *
399 * Returns: 1 if lba_capacity looks sensible
400 * 0 otherwise
401 */
402 static int lba_capacity_is_ok (struct hd_driveid *id)
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
403 {
404 unsigned long lba_sects = id->lba_capacity;
405 unsigned long chs_sects = id->cyls * id->heads * id->sectors;
406 unsigned long _10_percent = chs_sects / 10;
407
408 /* perform a rough sanity check on lba_sects: within 10% is "okay" */
409 if ((lba_sects - chs_sects) < _10_percent)
410 return 1; /* lba_capacity is good */
411
412 /* some drives have the word order reversed */
413 lba_sects = (lba_sects << 16) | (lba_sects >> 16);
414 if ((lba_sects - chs_sects) < _10_percent) {
415 id->lba_capacity = lba_sects; /* fix it */
416 return 1; /* lba_capacity is (now) good */
417 }
418 return 0; /* lba_capacity value is bad */
419 }
420
421 /*
422 * current_capacity() returns the capacity (in sectors) of a drive
423 * according to its current geometry/LBA settings.
424 */
425 static unsigned long current_capacity (ide_drive_t *drive)
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
426 {
427 struct hd_driveid *id = drive->id;
428 unsigned long capacity;
429
430 if (!drive->present)
431 return 0;
432 if (drive->media != disk)
433 return 0x1fffff; /* cdrom */
434 /* Determine capacity, and use LBA if the drive properly supports it */
435 if (id != NULL && (id->capability & 2) && lba_capacity_is_ok(id)) {
436 drive->select.b.lba = 1;
437 capacity = id->lba_capacity;
438 } else {
439 drive->select.b.lba = 0;
440 capacity = drive->cyl * drive->head * drive->sect;
441 }
442 return (capacity - drive->sect0);
443 }
444
445 /*
446 * ide_geninit() is called exactly *once* for each major, from genhd.c,
447 * at the beginning of the initial partition check for the drives.
448 */
449 static void ide_geninit (struct gendisk *gd)
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
450 {
451 unsigned int unit;
452 ide_hwif_t *hwif = gd->real_devices;
453
454 for (unit = 0; unit < gd->nr_real; ++unit) {
455 ide_drive_t *drive = &hwif->drives[unit];
456 drive->part[0].nr_sects = current_capacity(drive);
457 if (!drive->present || drive->media != disk) {
458 drive->part[0].start_sect = -1; /* skip partition check */
459 }
460 }
461 /*
462 * The partition check in genhd.c needs this string to identify
463 * our minor devices by name for display purposes.
464 * Note that doing this will prevent us from working correctly
465 * if ever called a second time for this major (never happens).
466 */
467 gd->real_devices = hwif->drives[0].name; /* name of first drive */
468 }
469
470 /*
471 * ide_alloc(): memory allocation for use *only* during driver initialization.
472 * If "within_area" is non-zero, the memory will be allocated such that
473 * it lies entirely within a "within_area" sized area (eg. 4096). This is
474 * needed for DMA stuff. "within_area" must be a power of two (not validated).
475 * All allocations are longword aligned.
476 */
477 static unsigned long ide_mem_start = 0uL; /* used by ide_alloc() */
478
479 void *ide_alloc (unsigned long bytecount, unsigned long within_area)
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
480 {
481 const unsigned long longsize_m1 = (sizeof(long) - 1);
482 void *p;
483
484 if (!ide_mem_start)
485 panic("ide: ide_alloc() not valid now\n");
486 ide_mem_start = (ide_mem_start + longsize_m1) & ~longsize_m1;
487 if (within_area) {
488 unsigned long fraction = within_area - (ide_mem_start & (within_area - 1));
489 if (fraction < bytecount)
490 ide_mem_start += fraction; /* realign to a new page */
491 }
492 p = (void *) ide_mem_start;
493 ide_mem_start += (bytecount + longsize_m1) & ~longsize_m1;
494 return p;
495 }
496
497 /*
498 * init_gendisk() (as opposed to ide_geninit) is called for each major device,
499 * after probing for drives, to allocate partition tables and other data
500 * structures needed for the routines in genhd.c. ide_geninit() gets called
501 * somewhat later, during the partition check.
502 */
503 static void init_gendisk (ide_hwif_t *hwif)
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
504 {
505 struct gendisk *gd;
506 unsigned int unit, units, minors;
507 int *bs;
508
509 /* figure out maximum drive number on the interface */
510 for (units = MAX_DRIVES; units > 0; --units) {
511 if (hwif->drives[units-1].present)
512 break;
513 }
514 minors = units * (1<<PARTN_BITS);
515 gd = ide_alloc (sizeof(struct gendisk), 0);
516 gd->sizes = ide_alloc (minors * sizeof(int), 0);
517 gd->part = ide_alloc (minors * sizeof(struct hd_struct), 0);
518 bs = ide_alloc (minors*sizeof(int), 0);
519
520 /* cdroms and msdos f/s are examples of non-1024 blocksizes */
521 blksize_size[hwif->major] = bs;
522 for (unit = 0; unit < minors; ++unit)
523 *bs++ = BLOCK_SIZE;
524
525 for (unit = 0; unit < units; ++unit)
526 hwif->drives[unit].part = &gd->part[unit << PARTN_BITS];
527
528 gd->major = hwif->major; /* our major device number */
529 gd->major_name = IDE_MAJOR_NAME; /* treated special in genhd.c */
530 gd->minor_shift = PARTN_BITS; /* num bits for partitions */
531 gd->max_p = 1<<PARTN_BITS; /* 1 + max partitions / drive */
532 gd->max_nr = units; /* max num real drives */
533 gd->nr_real = units; /* current num real drives */
534 gd->init = ide_geninit; /* initialization function */
535 gd->real_devices= hwif; /* ptr to internal data */
536
537 gd->next = gendisk_head; /* link new major into list */
538 hwif->gd = gendisk_head = gd;
539 }
540
541 static void unexpected_intr (int, ide_hwgroup_t *);
542 /*
543 * reset_ihandler() is a dummy interrupt handler which we install during
544 * an ide interface reset operation. This prevents other devices in the
545 * same hwgroup from being serviced while we play around with shared resources.
546 * If it ever gets invoked, we call unexpected_intr(), which treats the event
547 * the same as a timer_expiry().
548 */
549 static void reset_ihandler (ide_drive_t *drive)
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
550 {
551 unsigned long flags;
552
553 save_flags(flags);
554 cli();
555 unexpected_intr (HWIF(drive)->irq, HWGROUP(drive));
556 restore_flags(flags);
557 }
558
559 /*
560 * start_reset_timer() sets up a timer event for 50ms in the future,
561 * to poll for completion of an ide reset operation (no interrupt to help us).
562 */
563 static void start_reset_timer (ide_hwif_t *hwif)
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
564 {
565 ide_hwgroup_t *hwgroup = hwif->hwgroup;
566
567 hwgroup->reset_timeout = jiffies + WAIT_WORSTCASE; /* max waiting time */
568 hwgroup->handler = &reset_ihandler; /* dummy irq handler */
569 hwgroup->timer.expires = jiffies + (HZ/20); /* polling interval */
570 add_timer(&(hwgroup->timer));
571 }
572
573 #ifdef CONFIG_BLK_DEV_IDECD
574 /*
575 * atapi_reset_handler() gets invoked to poll the interface for completion every 50ms
576 * during an atapi drive reset operation. If the drive has not yet responded,
577 * and we have not yet hit our maximum waiting time, then the timer is restarted
578 * for another 50ms.
579 *
580 * Returns 1 if waiting for another 50ms, returns 0 otherwise.
581 */
582 static int atapi_reset_handler (ide_hwgroup_t *hwgroup)
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
583 {
584 ide_hwif_t *hwif = hwgroup->hwif;
585 ide_drive_t *drive = hwgroup->drive;
586 byte stat;
587
588 OUT_BYTE (drive->select.all, IDE_SELECT_REG);
589 udelay (10);
590
591 if (!OK_STAT(stat=GET_STAT(), 0, BUSY_STAT)) {
592 if (jiffies < hwgroup->reset_timeout) {
593 start_reset_timer (hwif);
594 return 1;
595 }
596 printk("%s: ATAPI reset timed-out, status=0x%02x\n", drive->name, stat);
597 return ide_do_reset (drive); /* do it the old fashioned way */
598 }
599 hwgroup->doing_atapi_reset = 0;
600 hwgroup->handler = NULL; /* allow new requests to be processed */
601 hwgroup->reset_timeout = 0; /* signal end of ide reset operation */
602 printk("%s: ATAPI reset complete\n", drive->name);
603 return 0;
604 }
605 #endif /* CONFIG_BLK_DEV_IDECD */
606
607 /*
608 * reset_handler() gets invoked to poll the interface for completion every 50ms
609 * during an ide reset operation. If the drives have not yet responded,
610 * and we have not yet hit our maximum waiting time, then the timer is restarted
611 * for another 50ms.
612 *
613 * Returns 1 if waiting for another 50ms, returns 0 otherwise.
614 */
615 static int reset_handler (ide_hwgroup_t *hwgroup)
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
616 {
617 ide_hwif_t *hwif = hwgroup->hwif;
618 ide_drive_t *drive = hwgroup->drive;
619 byte tmp;
620
621 #ifdef CONFIG_BLK_DEV_IDECD
622 if (hwgroup->doing_atapi_reset)
623 return atapi_reset_handler(hwgroup);
624 #endif /* CONFIG_BLK_DEV_IDECD */
625
626 if (!OK_STAT(tmp=GET_STAT(), 0, BUSY_STAT)) {
627 if (jiffies < hwgroup->reset_timeout) {
628 start_reset_timer (hwif);
629 return 1;
630 }
631 printk("%s: reset timed-out, status=0x%02x\n", hwif->name, tmp);
632 } else {
633 printk("%s: reset: ", hwif->name);
634 if ((tmp = GET_ERR()) == 1)
635 printk("success\n");
636 else {
637 printk("master: ");
638 switch (tmp & 0x7f) {
639 case 1: printk("passed");
640 break;
641 case 2: printk("formatter device error");
642 break;
643 case 3: printk("sector buffer error");
644 break;
645 case 4: printk("ECC circuitry error");
646 break;
647 case 5: printk("controlling MPU error");
648 break;
649 default:printk("error (0x%02x?)", tmp);
650 }
651 if (tmp & 0x80)
652 printk("; slave: failed");
653 printk("\n");
654 }
655 }
656 hwgroup->handler = NULL; /* allow new requests to be processed */
657 hwgroup->reset_timeout = 0; /* signal end of ide reset operation */
658 return 0;
659 }
660
661 /*
662 * ide_do_reset() attempts to recover a confused drive by resetting it.
663 * Unfortunately, resetting a disk drive actually resets all devices on
664 * the same interface, so it can really be thought of as resetting the
665 * interface rather than resetting the drive.
666 *
667 * ATAPI devices have their own reset mechanism which allows them to be
668 * individually reset without clobbering other devices on the same interface.
669 *
670 * Unfortunately, the IDE interface does not generate an interrupt to let
671 * us know when the reset operation has finished, so we must poll for this.
672 * Equally poor, though, is the fact that this may a very long time to complete,
673 * (up to 30 seconds worstcase). So, instead of busy-waiting here for it,
674 * we set a timer to poll at 50ms intervals.
675 */
676 int ide_do_reset (ide_drive_t *drive)
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
677 {
678 unsigned int unit;
679 unsigned long flags;
680 ide_hwif_t *hwif = HWIF(drive);
681 ide_hwgroup_t *hwgroup = HWGROUP(drive);
682
683 save_flags(flags);
684 cli(); /* Why ? */
685
686 #ifdef CONFIG_BLK_DEV_IDECD
687 /* For an ATAPI device, first try an ATAPI SRST. */
688 if (drive->media == cdrom) {
689 if (!hwgroup->doing_atapi_reset) {
690 hwgroup->doing_atapi_reset = 1;
691 if (!drive->keep_settings)
692 drive->unmask = 0;
693 OUT_BYTE (drive->select.all, IDE_SELECT_REG);
694 udelay (20);
695 OUT_BYTE (WIN_SRST, IDE_COMMAND_REG);
696 hwgroup->reset_timeout = jiffies + WAIT_WORSTCASE;
697 start_reset_timer (hwif); /* begin periodic polling */
698 restore_flags (flags);
699 return 1;
700 }
701 }
702 hwgroup->doing_atapi_reset = 0;
703 #endif /* CONFIG_BLK_DEV_IDECD */
704
705 /*
706 * First, reset any device state data we were maintaining
707 * for any of the drives on this interface.
708 */
709 for (unit = 0; unit < MAX_DRIVES; ++unit) {
710 ide_drive_t *rdrive = &hwif->drives[unit];
711 rdrive->special.b.set_geometry = 1;
712 rdrive->special.b.recalibrate = 1;
713 rdrive->special.b.set_multmode = 0;
714 if (OK_TO_RESET_CONTROLLER)
715 rdrive->mult_count = 0;
716 if (!rdrive->keep_settings) {
717 rdrive->mult_req = 0;
718 rdrive->unmask = 0;
719 }
720 if (rdrive->mult_req != rdrive->mult_count)
721 rdrive->special.b.set_multmode = 1;
722 }
723
724 #if OK_TO_RESET_CONTROLLER
725 /*
726 * Note that we also set nIEN while resetting the device,
727 * to mask unwanted interrupts from the interface during the reset.
728 * However, due to the design of PC hardware, this will cause an
729 * immediate interrupt due to the edge transition it produces.
730 * This single interrupt gives us a "fast poll" for drives that
731 * recover from reset very quickly, saving us the first 50ms wait time.
732 */
733 OUT_BYTE(drive->ctl|6,IDE_CONTROL_REG); /* set SRST and nIEN */
734 udelay(5); /* more than enough time */
735 OUT_BYTE(drive->ctl|2,IDE_CONTROL_REG); /* clear SRST, leave nIEN */
736 hwgroup->reset_timeout = jiffies + WAIT_WORSTCASE;
737 start_reset_timer (hwif); /* begin periodic polling */
738 #endif /* OK_TO_RESET_CONTROLLER */
739
740 restore_flags (flags);
741 return OK_TO_RESET_CONTROLLER; /* 1 = we are waiting, 0 = done */
742 }
743
744 /*
745 * Clean up after success/failure of an explicit (ioctl) drive cmd
746 */
747 static void end_drive_cmd (ide_drive_t *drive, byte stat, byte err)
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
748 {
749 unsigned long flags;
750 struct request *rq = HWGROUP(drive)->rq;
751 byte *args = (byte *) rq->buffer;
752
753 rq->errors = !OK_STAT(stat,READY_STAT,BAD_STAT);
754 if (args) {
755 args[0] = stat;
756 args[1] = err;
757 args[2] = IN_BYTE(IDE_NSECTOR_REG);
758 }
759 save_flags(flags);
760 cli();
761 up(rq->sem);
762 HWGROUP(drive)->rq = NULL;
763 restore_flags(flags);
764 }
765
766 /*
767 * Error reporting, in human readable form (luxurious, but a memory hog).
768 */
769 byte ide_dump_status (ide_drive_t *drive, const char *msg, byte stat)
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
770 {
771 unsigned long flags;
772 byte err = 0;
773
774 save_flags (flags);
775 sti();
776 printk("%s: %s: status=0x%02x", drive->name, msg, stat);
777 #if FANCY_STATUS_DUMPS
778 if (drive->media == disk) {
779 printk(" { ");
780 if (stat & BUSY_STAT)
781 printk("Busy ");
782 else {
783 if (stat & READY_STAT) printk("DriveReady ");
784 if (stat & WRERR_STAT) printk("DeviceFault ");
785 if (stat & SEEK_STAT) printk("SeekComplete ");
786 if (stat & DRQ_STAT) printk("DataRequest ");
787 if (stat & ECC_STAT) printk("CorrectedError ");
788 if (stat & INDEX_STAT) printk("Index ");
789 if (stat & ERR_STAT) printk("Error ");
790 }
791 printk("}");
792 }
793 #endif /* FANCY_STATUS_DUMPS */
794 printk("\n");
795 if ((stat & (BUSY_STAT|ERR_STAT)) == ERR_STAT) {
796 err = GET_ERR();
797 printk("%s: %s: error=0x%02x", drive->name, msg, err);
798 #if FANCY_STATUS_DUMPS
799 if (drive->media == disk) {
800 printk(" { ");
801 if (err & BBD_ERR) printk("BadSector ");
802 if (err & ECC_ERR) printk("UncorrectableError ");
803 if (err & ID_ERR) printk("SectorIdNotFound ");
804 if (err & ABRT_ERR) printk("DriveStatusError ");
805 if (err & TRK0_ERR) printk("TrackZeroNotFound ");
806 if (err & MARK_ERR) printk("AddrMarkNotFound ");
807 printk("}");
808 if (err & (BBD_ERR|ECC_ERR|ID_ERR|MARK_ERR)) {
809 byte cur = IN_BYTE(IDE_SELECT_REG);
810 if (cur & 0x40) { /* using LBA? */
811 printk(", LBAsect=%ld", (unsigned long)
812 ((cur&0xf)<<24)
813 |(IN_BYTE(IDE_HCYL_REG)<<16)
814 |(IN_BYTE(IDE_LCYL_REG)<<8)
815 | IN_BYTE(IDE_SECTOR_REG));
816 } else {
817 printk(", CHS=%d/%d/%d",
818 (IN_BYTE(IDE_HCYL_REG)<<8) +
819 IN_BYTE(IDE_LCYL_REG),
820 cur & 0xf,
821 IN_BYTE(IDE_SECTOR_REG));
822 }
823 if (HWGROUP(drive)->rq)
824 printk(", sector=%ld", HWGROUP(drive)->rq->sector);
825 }
826 }
827 #endif /* FANCY_STATUS_DUMPS */
828 printk("\n");
829 }
830 restore_flags (flags);
831 return err;
832 }
833
834 /*
835 * try_to_flush_leftover_data() is invoked in response to a drive
836 * unexpectedly having its DRQ_STAT bit set. As an alternative to
837 * resetting the drive, this routine tries to clear the condition
838 * by read a sector's worth of data from the drive. Of course,
839 * this may not help if the drive is *waiting* for data from *us*.
840 */
841 static void try_to_flush_leftover_data (ide_drive_t *drive)
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
842 {
843 int i = (drive->mult_count ? drive->mult_count : 1) * SECTOR_WORDS;
844
845 while (i > 0) {
846 unsigned long buffer[16];
847 unsigned int wcount = (i > 16) ? 16 : i;
848 i -= wcount;
849 ide_input_data (drive, buffer, wcount);
850 }
851 }
852
853 /*
854 * ide_error() takes action based on the error returned by the controller.
855 *
856 * Returns 1 if an ide reset operation has been initiated, in which case
857 * the caller MUST simply return from the driver (through however many levels).
858 * Returns 0 otherwise.
859 */
860 int ide_error (ide_drive_t *drive, const char *msg, byte stat)
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
861 {
862 struct request *rq;
863 byte err;
864
865 err = ide_dump_status(drive, msg, stat);
866 if ((rq = HWGROUP(drive)->rq) == NULL || drive == NULL)
867 return 0;
868 if (rq->cmd == IDE_DRIVE_CMD) { /* never retry an explicit DRIVE_CMD */
869 end_drive_cmd(drive, stat, err);
870 return 0;
871 }
872 if (stat & BUSY_STAT) { /* other bits are useless when BUSY */
873 rq->errors |= ERROR_RESET;
874 } else {
875 if (drive->media == disk && (stat & ERR_STAT)) {
876 /* err has different meaning on cdrom */
877 if (err & BBD_ERR) /* retries won't help this! */
878 rq->errors = ERROR_MAX;
879 else if (err & TRK0_ERR) /* help it find track zero */
880 rq->errors |= ERROR_RECAL;
881 }
882 if ((stat & DRQ_STAT) && rq->cmd != WRITE)
883 try_to_flush_leftover_data(drive);
884 }
885 if (GET_STAT() & (BUSY_STAT|DRQ_STAT))
886 rq->errors |= ERROR_RESET; /* Mmmm.. timing problem */
887
888 #ifdef CONFIG_BLK_DEV_TRITON
889 if (rq->errors > 3 && drive->using_dma) { /* DMA troubles? */
890 drive->using_dma = 0;
891 printk("%s: DMA disabled\n", drive->name);
892 --rq->errors;
893 return 0;
894 }
895 #endif /* CONFIG_BLK_DEV_TRITON */
896 if (rq->errors >= ERROR_MAX)
897 ide_end_request(0, HWGROUP(drive));
898 else {
899 if ((rq->errors & ERROR_RESET) == ERROR_RESET) {
900 ++rq->errors;
901 return ide_do_reset(drive);
902 } else if ((rq->errors & ERROR_RECAL) == ERROR_RECAL)
903 drive->special.b.recalibrate = 1;
904 ++rq->errors;
905 }
906 return 0;
907 }
908
909 /*
910 * read_intr() is the handler for disk read/multread interrupts
911 */
912 static void read_intr (ide_drive_t *drive)
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
913 {
914 byte stat;
915 int i;
916 unsigned int msect, nsect;
917 struct request *rq;
918
919 if (!OK_STAT(stat=GET_STAT(),DATA_READY,BAD_R_STAT)) {
920 sti();
921 if (!ide_error(drive, "read_intr", stat))
922 IDE_DO_REQUEST;
923 return;
924 }
925 msect = drive->mult_count;
926 read_next:
927 rq = HWGROUP(drive)->rq;
928 if (msect) {
929 if ((nsect = rq->current_nr_sectors) > msect)
930 nsect = msect;
931 msect -= nsect;
932 } else
933 nsect = 1;
934 ide_input_data(drive, rq->buffer, nsect * SECTOR_WORDS);
935 #ifdef DEBUG
936 printk("%s: read: sectors(%ld-%ld), buffer=0x%08lx, remaining=%ld\n",
937 drive->name, rq->sector, rq->sector+nsect-1,
938 (unsigned long) rq->buffer+(nsect<<9), rq->nr_sectors-nsect);
939 #endif
940 rq->sector += nsect;
941 rq->buffer += nsect<<9;
942 rq->errors = 0;
943 i = (rq->nr_sectors -= nsect);
944 if ((rq->current_nr_sectors -= nsect) <= 0)
945 ide_end_request(1, HWGROUP(drive));
946 if (i > 0) {
947 if (msect)
948 goto read_next;
949 ide_set_handler (drive, &read_intr);
950 return;
951 }
952 IDE_DO_REQUEST;
953 }
954
955 /*
956 * write_intr() is the handler for disk write interrupts
957 */
958 static void write_intr (ide_drive_t *drive)
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
959 {
960 byte stat;
961 int i;
962 struct request *rq = HWGROUP(drive)->rq;
963
964 if (OK_STAT(stat=GET_STAT(),DRIVE_READY,drive->bad_wstat)) {
965 #ifdef DEBUG
966 printk("%s: write: sector %ld, buffer=0x%08lx, remaining=%ld\n",
967 drive->name, rq->sector, (unsigned long) rq->buffer,
968 rq->nr_sectors-1);
969 #endif
970 if ((rq->nr_sectors == 1) ^ ((stat & DRQ_STAT) != 0)) {
971 rq->sector++;
972 rq->buffer += 512;
973 rq->errors = 0;
974 i = --rq->nr_sectors;
975 --rq->current_nr_sectors;
976 if (rq->current_nr_sectors <= 0)
977 ide_end_request(1, HWGROUP(drive));
978 if (i > 0) {
979 ide_output_data (drive, rq->buffer, SECTOR_WORDS);
980 ide_set_handler (drive, &write_intr);
981 return;
982 }
983 IDE_DO_REQUEST;
984 return;
985 }
986 }
987 sti();
988 if (!ide_error(drive, "write_intr", stat))
989 IDE_DO_REQUEST;
990 }
991
992 /*
993 * multwrite() transfers a block of one or more sectors of data to a drive
994 * as part of a disk multwrite operation.
995 */
996 static void multwrite (ide_drive_t *drive)
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
997 {
998 struct request *rq = &HWGROUP(drive)->wrq;
999 unsigned int mcount = drive->mult_count;
1000
1001 do {
1002 unsigned int nsect = rq->current_nr_sectors;
1003 if (nsect > mcount)
1004 nsect = mcount;
1005 mcount -= nsect;
1006
1007 ide_output_data(drive, rq->buffer, nsect<<7);
1008 #ifdef DEBUG
1009 printk("%s: multwrite: sector %ld, buffer=0x%08lx, count=%d, remaining=%ld\n",
1010 drive->name, rq->sector, (unsigned long) rq->buffer,
1011 nsect, rq->nr_sectors - nsect);
1012 #endif
1013 if ((rq->nr_sectors -= nsect) <= 0)
1014 break;
1015 if ((rq->current_nr_sectors -= nsect) == 0) {
1016 if ((rq->bh = rq->bh->b_reqnext) != NULL) {
1017 rq->current_nr_sectors = rq->bh->b_size>>9;
1018 rq->buffer = rq->bh->b_data;
1019 } else {
1020 panic("%s: buffer list corrupted\n", drive->name);
1021 break;
1022 }
1023 } else {
1024 rq->buffer += nsect << 9;
1025 }
1026 } while (mcount);
1027 }
1028
1029 /*
1030 * write_intr() is the handler for disk multwrite interrupts
1031 */
1032 static void multwrite_intr (ide_drive_t *drive)
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
1033 {
1034 byte stat;
1035 int i;
1036 struct request *rq = &HWGROUP(drive)->wrq;
1037
1038 if (OK_STAT(stat=GET_STAT(),DRIVE_READY,drive->bad_wstat)) {
1039 if (stat & DRQ_STAT) {
1040 if (rq->nr_sectors) {
1041 multwrite(drive);
1042 ide_set_handler (drive, &multwrite_intr);
1043 return;
1044 }
1045 } else {
1046 if (!rq->nr_sectors) { /* all done? */
1047 rq = HWGROUP(drive)->rq;
1048 for (i = rq->nr_sectors; i > 0;){
1049 i -= rq->current_nr_sectors;
1050 ide_end_request(1, HWGROUP(drive));
1051 }
1052 IDE_DO_REQUEST;
1053 return;
1054 }
1055 }
1056 }
1057 sti();
1058 if (!ide_error(drive, "multwrite_intr", stat))
1059 IDE_DO_REQUEST;
1060 }
1061
1062 /*
1063 * Issue a simple drive command
1064 * The drive must be selected beforehand.
1065 */
1066 static void ide_cmd(ide_drive_t *drive, byte cmd, byte nsect, ide_handler_t *handler)
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
1067 {
1068 ide_set_handler (drive, handler);
1069 OUT_BYTE(drive->ctl,IDE_CONTROL_REG);
1070 OUT_BYTE(nsect,IDE_NSECTOR_REG);
1071 OUT_BYTE(cmd,IDE_COMMAND_REG);
1072 }
1073
1074 /*
1075 * set_multmode_intr() is invoked on completion of a WIN_SETMULT cmd.
1076 */
1077 static void set_multmode_intr (ide_drive_t *drive)
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
1078 {
1079 byte stat = GET_STAT();
1080
1081 sti();
1082 if (OK_STAT(stat,READY_STAT,BAD_STAT)) {
1083 drive->mult_count = drive->mult_req;
1084 } else {
1085 drive->mult_req = drive->mult_count = 0;
1086 drive->special.b.recalibrate = 1;
1087 (void) ide_dump_status(drive, "set_multmode", stat);
1088 }
1089 IDE_DO_REQUEST;
1090 }
1091
1092 /*
1093 * set_geometry_intr() is invoked on completion of a WIN_SPECIFY cmd.
1094 */
1095 static void set_geometry_intr (ide_drive_t *drive)
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
1096 {
1097 byte stat = GET_STAT();
1098
1099 sti();
1100 if (!OK_STAT(stat,READY_STAT,BAD_STAT))
1101 if (ide_error(drive, "set_geometry_intr", stat))
1102 return;
1103 IDE_DO_REQUEST;
1104 }
1105
1106 /*
1107 * recal_intr() is invoked on completion of a WIN_RESTORE (recalibrate) cmd.
1108 */
1109 static void recal_intr (ide_drive_t *drive)
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
1110 {
1111 byte stat = GET_STAT();
1112
1113 sti();
1114 if (!OK_STAT(stat,READY_STAT,BAD_STAT))
1115 if (ide_error(drive, "recal_intr", stat))
1116 return;
1117 IDE_DO_REQUEST;
1118 }
1119
1120 /*
1121 * drive_cmd_intr() is invoked on completion of a special DRIVE_CMD.
1122 */
1123 static void drive_cmd_intr (ide_drive_t *drive)
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
1124 {
1125 byte stat = GET_STAT();
1126
1127 sti();
1128 if (OK_STAT(stat,READY_STAT,BAD_STAT))
1129 end_drive_cmd (drive, stat, GET_ERR());
1130 else if (ide_error(drive, "drive_cmd", stat)) /* calls end_drive_cmd */
1131 return;
1132 IDE_DO_REQUEST;
1133 }
1134
1135 /*
1136 * do_special() is used to issue WIN_SPECIFY, WIN_RESTORE, and WIN_SETMULT
1137 * commands to a drive. It used to do much more, but has been scaled back
1138 * in recent updates, and could be completely eliminated with a bit more effort.
1139 */
1140 static inline void do_special (ide_drive_t *drive)
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
1141 {
1142 special_t *s = &drive->special;
1143 #ifdef DEBUG
1144 printk("%s: do_special: 0x%02x\n", drive->name, s->all);
1145 #endif
1146 if (s->b.set_geometry) {
1147 s->b.set_geometry = 0;
1148 if (drive->media == disk) {
1149 OUT_BYTE(drive->sect,IDE_SECTOR_REG);
1150 OUT_BYTE(drive->cyl,IDE_LCYL_REG);
1151 OUT_BYTE(drive->cyl>>8,IDE_HCYL_REG);
1152 OUT_BYTE(((drive->head-1)|drive->select.all)&0xBF,IDE_SELECT_REG);
1153 ide_cmd(drive, WIN_SPECIFY, drive->sect, &set_geometry_intr);
1154 }
1155 } else if (s->b.recalibrate) {
1156 s->b.recalibrate = 0;
1157 if (drive->media == disk) {
1158 ide_cmd(drive, WIN_RESTORE, drive->sect, &recal_intr);
1159 }
1160 } else if (s->b.set_multmode) {
1161 s->b.set_multmode = 0;
1162 if (drive->media == disk) {
1163 if (drive->id && drive->mult_req > drive->id->max_multsect)
1164 drive->mult_req = drive->id->max_multsect;
1165 ide_cmd(drive, WIN_SETMULT, drive->mult_req, &set_multmode_intr);
1166 } else
1167 drive->mult_req = 0;
1168 } else if (s->all) {
1169 s->all = 0;
1170 printk("%s: bad special flag: 0x%02x\n", drive->name, s->all);
1171 }
1172 }
1173
1174 /*
1175 * This routine busy-waits for the drive status to be not "busy".
1176 * It then checks the status for all of the "good" bits and none
1177 * of the "bad" bits, and if all is okay it returns 0. All other
1178 * cases return 1 after invoking ide_error()
1179 *
1180 * This routine should get fixed to not hog the cpu during extra long waits..
1181 * That could be done by busy-waiting for the first jiffy or two, and then
1182 * setting a timer to wake up at half second intervals thereafter,
1183 * until WAIT_WORSTCASE is achieved, before timing out.
1184 */
1185 int ide_wait_stat (ide_drive_t *drive, byte good, byte bad, unsigned long timeout)
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
1186 {
1187 byte stat;
1188 unsigned long flags;
1189 test:
1190 udelay(1); /* spec allows drive 400ns to change "BUSY" */
1191 if (OK_STAT((stat = GET_STAT()), good, bad))
1192 return 0; /* fast exit for most frequent case */
1193 if (!(stat & BUSY_STAT)) {
1194 (void) ide_error(drive, "status error", stat);
1195 return 1;
1196 }
1197
1198 save_flags(flags);
1199 sti();
1200 timeout += jiffies;
1201 do {
1202 if (!((stat = GET_STAT()) & BUSY_STAT)) {
1203 restore_flags(flags);
1204 goto test;
1205 }
1206 } while (jiffies <= timeout);
1207
1208 restore_flags(flags);
1209 (void) ide_error(drive, "status timeout", GET_STAT());
1210 return 1;
1211 }
1212
1213 /*
1214 * do_rw_disk() issues WIN_{MULT}READ and WIN_{MULT}WRITE commands to a disk,
1215 * using LBA if supported, or CHS otherwise, to address sectors. It also takes
1216 * care of issuing special DRIVE_CMDs.
1217 */
1218 static inline void do_rw_disk (ide_drive_t *drive, struct request *rq, unsigned long block)
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
1219 {
1220 unsigned short io_base = HWIF(drive)->io_base;
1221
1222 OUT_BYTE(drive->ctl,IDE_CONTROL_REG);
1223 OUT_BYTE(rq->nr_sectors,io_base+IDE_NSECTOR_OFFSET);
1224 if (drive->select.b.lba) {
1225 #ifdef DEBUG
1226 printk("%s: %sing: LBAsect=%ld, sectors=%ld, buffer=0x%08lx\n",
1227 drive->name, (rq->cmd==READ)?"read":"writ",
1228 block, rq->nr_sectors, (unsigned long) rq->buffer);
1229 #endif
1230 OUT_BYTE(block,io_base+IDE_SECTOR_OFFSET);
1231 OUT_BYTE(block>>=8,io_base+IDE_LCYL_OFFSET);
1232 OUT_BYTE(block>>=8,io_base+IDE_HCYL_OFFSET);
1233 OUT_BYTE(((block>>8)&0x0f)|drive->select.all,io_base+IDE_SELECT_OFFSET);
1234 } else {
1235 unsigned int sect,head,cyl,track;
1236 track = block / drive->sect;
1237 sect = block % drive->sect + 1;
1238 OUT_BYTE(sect,io_base+IDE_SECTOR_OFFSET);
1239 head = track % drive->head;
1240 cyl = track / drive->head;
1241 OUT_BYTE(cyl,io_base+IDE_LCYL_OFFSET);
1242 OUT_BYTE(cyl>>8,io_base+IDE_HCYL_OFFSET);
1243 OUT_BYTE(head|drive->select.all,io_base+IDE_SELECT_OFFSET);
1244 #ifdef DEBUG
1245 printk("%s: %sing: CHS=%d/%d/%d, sectors=%ld, buffer=0x%08lx\n",
1246 drive->name, (rq->cmd==READ)?"read":"writ", cyl,
1247 head, sect, rq->nr_sectors, (unsigned long) rq->buffer);
1248 #endif
1249 }
1250 if (rq->cmd == READ) {
1251 #ifdef CONFIG_BLK_DEV_TRITON
1252 if (drive->using_dma && !(HWIF(drive)->dmaproc(ide_dma_read, drive)))
1253 return;
1254 #endif /* CONFIG_BLK_DEV_TRITON */
1255 ide_set_handler(drive, &read_intr);
1256 OUT_BYTE(drive->mult_count ? WIN_MULTREAD : WIN_READ, io_base+IDE_COMMAND_OFFSET);
1257 return;
1258 }
1259 if (rq->cmd == WRITE) {
1260 #ifdef CONFIG_BLK_DEV_TRITON
1261 if (drive->using_dma && !(HWIF(drive)->dmaproc(ide_dma_write, drive)))
1262 return;
1263 #endif /* CONFIG_BLK_DEV_TRITON */
1264 OUT_BYTE(drive->mult_count ? WIN_MULTWRITE : WIN_WRITE, io_base+IDE_COMMAND_OFFSET);
1265 if (ide_wait_stat(drive, DATA_READY, drive->bad_wstat, WAIT_DRQ)) {
1266 printk("%s: no DRQ after issuing %s\n", drive->name,
1267 drive->mult_count ? "MULTWRITE" : "WRITE");
1268 return;
1269 }
1270 if (!drive->unmask)
1271 cli();
1272 if (drive->mult_count) {
1273 HWGROUP(drive)->wrq = *rq; /* scratchpad */
1274 ide_set_handler (drive, &multwrite_intr);
1275 multwrite(drive);
1276 } else {
1277 ide_set_handler (drive, &write_intr);
1278 ide_output_data(drive, rq->buffer, SECTOR_WORDS);
1279 }
1280 return;
1281 }
1282 if (rq->cmd == IDE_DRIVE_CMD) {
1283 byte *args = rq->buffer;
1284 if (args) {
1285 printk("%s: DRIVE_CMD cmd=0x%02x sc=0x%02x fr=0x%02x\n",
1286 drive->name, args[0], args[1], args[2]);
1287 OUT_BYTE(args[2],io_base+IDE_FEATURE_OFFSET);
1288 ide_cmd(drive, args[0], args[1], &drive_cmd_intr);
1289 return;
1290 } else {
1291 /*
1292 * NULL is actually a valid way of waiting for
1293 * all current requests to be flushed from the queue.
1294 */
1295 #ifdef DEBUG
1296 printk("%s: DRIVE_CMD (null)\n", drive->name);
1297 #endif
1298 end_drive_cmd(drive, GET_STAT(), GET_ERR());
1299 return;
1300 }
1301 }
1302 printk("%s: bad command: %d\n", drive->name, rq->cmd);
1303 ide_end_request(0, HWGROUP(drive));
1304 }
1305
1306 /*
1307 * do_request() initiates handling of a new I/O request
1308 */
1309 static inline void do_request (ide_hwif_t *hwif, struct request *rq)
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
1310 {
1311 unsigned int minor, unit;
1312 unsigned long block, blockend;
1313 ide_drive_t *drive;
1314
1315 sti();
1316 #ifdef DEBUG
1317 printk("%s: ide_do_request: current=0x%08lx\n", hwif->name, (unsigned long) rq);
1318 #endif
1319 minor = MINOR(rq->dev);
1320 unit = minor >> PARTN_BITS;
1321 if (MAJOR(rq->dev) != hwif->major || unit >= MAX_DRIVES) {
1322 printk("%s: bad device number: 0x%04x\n", hwif->name, rq->dev);
1323 goto kill_rq;
1324 }
1325 drive = &hwif->drives[unit];
1326 #ifdef DEBUG
1327 if (rq->bh && !rq->bh->b_lock) {
1328 printk("%s: block not locked\n", drive->name);
1329 goto kill_rq;
1330 }
1331 #endif
1332 block = rq->sector;
1333 blockend = block + rq->nr_sectors;
1334 if ((blockend < block) || (blockend > drive->part[minor&PARTN_MASK].nr_sects)) {
1335 printk("%s%c: bad access: block=%ld, count=%ld\n", drive->name,
1336 (minor&PARTN_MASK)?'0'+(minor&PARTN_MASK):' ', block, rq->nr_sectors);
1337 goto kill_rq;
1338 }
1339 block += drive->part[minor&PARTN_MASK].start_sect + drive->sect0;
1340 ((ide_hwgroup_t *)hwif->hwgroup)->drive = drive;
1341 #if (DISK_RECOVERY_TIME > 0)
1342 while ((read_timer() - hwif->last_time) < DISK_RECOVERY_TIME);
1343 #endif
1344 #if SUPPORT_HT6560B
1345 if (hwif->select)
1346 ide_hwif_select (hwif);
1347 #endif
1348 OUT_BYTE(drive->select.all,IDE_SELECT_REG);
1349 if (ide_wait_stat(drive, drive->ready_stat, BUSY_STAT|DRQ_STAT, WAIT_READY)) {
1350 printk("%s: drive not ready for command\n", drive->name);
1351 return;
1352
1353 }
1354 if (!drive->special.all) {
1355 #ifdef CONFIG_BLK_DEV_IDECD
1356 switch (drive->media) {
1357 case disk:
1358 do_rw_disk (drive, rq, block);
1359 return;
1360 case cdrom:
1361 ide_do_rw_cdrom (drive, block);
1362 return;
1363 default:
1364 printk("%s: media type %d not supported\n",
1365 drive->name, drive->media);
1366 goto kill_rq;
1367 }
1368 #else
1369 do_rw_disk (drive, rq, block); /* simpler and faster */
1370 return;
1371 #endif;
1372 }
1373 do_special(drive);
1374 return;
1375 kill_rq:
1376 ide_end_request(0, hwif->hwgroup);
1377 }
1378
1379 /*
1380 * The driver enables interrupts as much as possible. In order to do this,
1381 * (a) the device-interrupt is always masked before entry, and
1382 * (b) the timeout-interrupt is always disabled before entry.
1383 *
1384 * If we enter here from, say irq14, and then start a new request for irq15,
1385 * (possible with "serialize" option) then we cannot ensure that we exit
1386 * before the irq15 hits us. So, we must be careful not to let this bother us.
1387 *
1388 * Interrupts are still masked (by default) whenever we are exchanging
1389 * data/cmds with a drive, because some drives seem to have very poor
1390 * tolerance for latency during I/O. For devices which don't suffer from
1391 * this problem (most don't), the unmask flag can be set using the "hdparm"
1392 * utility, to permit other interrupts during data/cmd transfers.
1393 */
1394 void ide_do_request (ide_hwgroup_t *hwgroup)
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
1395 {
1396 cli(); /* paranoia */
1397 if (hwgroup->handler != NULL) {
1398 printk("%s: EEeekk!! handler not NULL in ide_do_request()\n", hwgroup->hwif->name);
1399 return;
1400 }
1401 do {
1402 ide_hwif_t *hwif = hwgroup->hwif;
1403 struct request *rq;
1404 if ((rq = hwgroup->rq) == NULL) {
1405 hwgroup->drive = NULL; /* paranoia */
1406 do {
1407 rq = blk_dev[hwif->major].current_request;
1408 if (rq != NULL && rq->dev != -1)
1409 goto got_rq;
1410 } while ((hwif = hwif->next) != hwgroup->hwif);
1411 return; /* no work left for this hwgroup */
1412 got_rq:
1413 blk_dev[hwif->major].current_request = rq->next;
1414 }
1415 do_request(hwgroup->hwif = hwif, hwgroup->rq = rq);
1416 cli();
1417 } while (hwgroup->handler == NULL);
1418 }
1419
1420 /*
1421 * do_hwgroup_request() invokes ide_do_request() after first masking
1422 * all possible interrupts for the current hwgroup. This prevents race
1423 * conditions in the event that an unexpected interrupt occurs while
1424 * we are in the driver.
1425 *
1426 * Note that when an interrupt is used to reenter the driver, the first level
1427 * handler will already have masked the irq that triggered, but any other ones
1428 * for the hwgroup will still be unmasked. The driver tries to be careful
1429 * about such things.
1430 */
1431 static void do_hwgroup_request (ide_hwgroup_t *hwgroup)
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
1432 {
1433 if (hwgroup->handler == NULL) {
1434 ide_hwif_t *hgif = hwgroup->hwif;
1435 ide_hwif_t *hwif = hgif;
1436 do {
1437 disable_irq(hwif->irq);
1438 } while ((hwif = hwif->next) != hgif);
1439 ide_do_request (hwgroup);
1440 do {
1441 enable_irq(hwif->irq);
1442 } while ((hwif = hwif->next) != hgif);
1443 }
1444 }
1445
1446 static void do_ide0_request (void) /* invoked with cli() */
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
1447 {
1448 do_hwgroup_request (ide_hwifs[0].hwgroup);
1449 }
1450
1451 static void do_ide1_request (void) /* invoked with cli() */
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
1452 {
1453 do_hwgroup_request (ide_hwifs[1].hwgroup);
1454 }
1455
1456 static void do_ide2_request (void) /* invoked with cli() */
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
1457 {
1458 do_hwgroup_request (ide_hwifs[2].hwgroup);
1459 }
1460
1461 static void do_ide3_request (void) /* invoked with cli() */
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
1462 {
1463 do_hwgroup_request (ide_hwifs[3].hwgroup);
1464 }
1465
1466 static void timer_expiry (unsigned long data)
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
1467 {
1468 ide_hwgroup_t *hwgroup = (ide_hwgroup_t *) data;
1469 ide_drive_t *drive = hwgroup->drive;
1470 unsigned long flags;
1471
1472 save_flags(flags);
1473 cli();
1474
1475 if (hwgroup->reset_timeout != 0) { /* ide reset in progress? */
1476 if (!reset_handler(hwgroup))
1477 do_hwgroup_request (hwgroup);
1478 } else if (hwgroup->handler == NULL) { /* not waiting for anything? */
1479 sti(); /* drive must have responded just as the timer expired */
1480 printk("%s: marginal timeout\n", drive->name);
1481 } else { /* drive not responding */
1482 hwgroup->handler = NULL;
1483 if (hwgroup->hwif->dmaproc)
1484 (void) hwgroup->hwif->dmaproc (ide_dma_abort, drive);
1485 if (!ide_error(drive, "irq timeout", GET_STAT()))
1486 do_hwgroup_request (hwgroup);
1487 }
1488 restore_flags(flags);
1489 }
1490
1491 /*
1492 * There's nothing really useful we can do with an unexpected interrupt,
1493 * other than reading the status register (to clear it), and logging it.
1494 * There should be no way that an irq can happen before we're ready for it,
1495 * so we needn't worry much about losing an "important" interrupt here.
1496 *
1497 * On laptops (and "green" PCs), an unexpected interrupt occurs whenever the
1498 * drive enters "idle", "standby", or "sleep" mode, so if the status looks
1499 * "good", we just ignore the interrupt completely.
1500 *
1501 * This routine assumes cli() is in effect when called.
1502 *
1503 * If an unexpected interrupt happens on irq15 while we are handling irq14
1504 * and if the two interfaces are "serialized" (CMD640B), then it looks like
1505 * we could screw up by interfering with a new request being set up for irq15.
1506 *
1507 * In reality, this is a non-issue. The new command is not sent unless the
1508 * drive is ready to accept one, in which case we know the drive is not
1509 * trying to interrupt us. And ide_set_handler() is always invoked before
1510 * completing the issuance of any new drive command, so we will not be
1511 * accidently invoked as a result of any valid command completion interrupt.
1512 *
1513 */
1514 static void unexpected_intr (int irq, ide_hwgroup_t *hwgroup)
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
1515 {
1516 byte stat;
1517 unsigned int unit;
1518 ide_hwif_t *hwif = hwgroup->hwif;
1519
1520 /*
1521 * check for ide reset in progress
1522 */
1523 if (hwgroup->reset_timeout != 0) {
1524 if (!reset_handler(hwgroup))
1525 do_hwgroup_request (hwgroup);
1526 return;
1527 }
1528
1529 /*
1530 * handle the unexpected interrupt
1531 */
1532 do {
1533 if (hwif->irq == irq) {
1534 #if SUPPORT_HT6560B
1535 if (hwif->select)
1536 ide_hwif_select (hwif);
1537 #endif
1538 for (unit = 0; unit < MAX_DRIVES; ++unit) {
1539 ide_drive_t *drive = &hwif->drives[unit];
1540 if (!drive->present)
1541 continue;
1542 if (!OK_STAT(stat=GET_STAT(), drive->ready_stat, BAD_STAT))
1543 (void) ide_dump_status(drive, "unexpected_intr", stat);
1544 if ((stat & DRQ_STAT))
1545 try_to_flush_leftover_data(drive);
1546 }
1547 }
1548 } while ((hwif = hwif->next) != hwgroup->hwif);
1549 }
1550
1551 /*
1552 * entry point for all interrupts, caller does cli() for us
1553 */
1554 static void ide_intr (int irq, struct pt_regs *regs)
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
1555 {
1556 ide_hwgroup_t *hwgroup = irq_to_hwgroup[irq];
1557 ide_handler_t *handler;
1558
1559 if (irq == hwgroup->hwif->irq && (handler = hwgroup->handler) != NULL) {
1560 ide_drive_t *drive = hwgroup->drive;
1561 hwgroup->handler = NULL;
1562 del_timer(&(hwgroup->timer));
1563 if (drive->unmask)
1564 sti();
1565 handler(drive);
1566 } else {
1567 unexpected_intr(irq, hwgroup);
1568 }
1569 cli();
1570 }
1571
1572 /*
1573 * get_info_ptr() returns the (ide_drive_t *) for a given device number.
1574 * It returns NULL if the given device number does not match any present drives.
1575 */
1576 static ide_drive_t *get_info_ptr (int i_rdev)
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
1577 {
1578 int major = MAJOR(i_rdev);
1579 unsigned int h;
1580
1581 for (h = 0; h < MAX_HWIFS; ++h) {
1582 ide_hwif_t *hwif = &ide_hwifs[h];
1583 if (hwif->present && major == hwif->major) {
1584 unsigned unit = DEVICE_NR(i_rdev);
1585 if (unit < MAX_DRIVES) {
1586 ide_drive_t *drive = &hwif->drives[unit];
1587 if (drive->present)
1588 return drive;
1589 } else if (major == IDE0_MAJOR && unit < 4) {
1590 printk("ide: probable bad entry for /dev/hd%c%d\n",
1591 'a' + unit, MINOR(i_rdev) & PARTN_MASK);
1592 printk("ide: to fix it, run: /usr/src/linux/drivers/block/MAKEDEV.ide\n");
1593 }
1594 break;
1595 }
1596 }
1597 return NULL;
1598 }
1599
1600 /*
1601 * This function issues a specific IDE drive command onto the
1602 * tail of the request queue, and waits for it to be completed.
1603 * If arg is NULL, it goes through all the motions,
1604 * but without actually sending a command to the drive.
1605 */
1606 int ide_do_drive_cmd(int rdev, char *args)
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
1607 {
1608 unsigned long flags;
1609 unsigned int major = MAJOR(rdev);
1610 struct request rq, *cur_rq;
1611 struct blk_dev_struct *bdev;
1612 struct semaphore sem = MUTEX_LOCKED;
1613
1614 /* build up a special request, and add it to the queue */
1615 rq.buffer = args;
1616 rq.cmd = IDE_DRIVE_CMD;
1617 rq.errors = 0;
1618 rq.sector = 0;
1619 rq.nr_sectors = 0;
1620 rq.current_nr_sectors = 0;
1621 rq.sem = &sem;
1622 rq.bh = NULL;
1623 rq.bhtail = NULL;
1624 rq.next = NULL;
1625 rq.dev = rdev;
1626 bdev = &blk_dev[major];
1627
1628 save_flags(flags);
1629 cli();
1630 cur_rq = bdev->current_request;
1631 if (cur_rq == NULL) { /* empty request list? */
1632 bdev->current_request = &rq; /* service ours immediately */
1633 bdev->request_fn();
1634 } else {
1635 while (cur_rq->next != NULL) /* find end of request list */
1636 cur_rq = cur_rq->next;
1637 cur_rq->next = &rq; /* add rq to the end */
1638 }
1639
1640 down(&sem); /* wait for it to be serviced */
1641 restore_flags(flags);
1642 return rq.errors ? -EIO : 0; /* return -EIO if errors */
1643 }
1644
1645 static int ide_open(struct inode * inode, struct file * filp)
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
1646 {
1647 ide_drive_t *drive;
1648 unsigned long flags;
1649
1650 if ((drive = get_info_ptr(inode->i_rdev)) == NULL)
1651 return -ENODEV;
1652 save_flags(flags);
1653 cli();
1654 while (drive->busy)
1655 sleep_on(&drive->wqueue);
1656 drive->usage++;
1657 restore_flags(flags);
1658 #ifdef CONFIG_BLK_DEV_IDECD
1659 if (drive->media == cdrom)
1660 return ide_cdrom_open (inode, filp, drive);
1661 #endif /* CONFIG_BLK_DEV_IDECD */
1662 if (drive->removeable) {
1663 byte door_lock[] = {WIN_DOORLOCK,0,0,0};
1664 check_disk_change(inode->i_rdev);
1665 ide_do_drive_cmd(inode->i_rdev, door_lock);
1666 }
1667 return 0;
1668 }
1669
1670 /*
1671 * Releasing a block device means we sync() it, so that it can safely
1672 * be forgotten about...
1673 */
1674 static void ide_release(struct inode * inode, struct file * file)
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
1675 {
1676 ide_drive_t *drive;
1677
1678 if ((drive = get_info_ptr(inode->i_rdev)) != NULL) {
1679 sync_dev(inode->i_rdev);
1680 drive->usage--;
1681 #ifdef CONFIG_BLK_DEV_IDECD
1682 if (drive->media == cdrom)
1683 ide_cdrom_release (inode, file, drive);
1684 else
1685 #endif /* CONFIG_BLK_DEV_IDECD */
1686 if (drive->removeable) {
1687 byte door_unlock[] = {WIN_DOORUNLOCK,0,0,0};
1688 invalidate_buffers(inode->i_rdev);
1689 ide_do_drive_cmd(inode->i_rdev, door_unlock);
1690 }
1691 }
1692 }
1693
1694 /*
1695 * This routine is called to flush all partitions and partition tables
1696 * for a changed disk, and then re-read the new partition table.
1697 * If we are revalidating a disk because of a media change, then we
1698 * enter with usage == 0. If we are using an ioctl, we automatically have
1699 * usage == 1 (we need an open channel to use an ioctl :-), so this
1700 * is our limit.
1701 */
1702 static int revalidate_disk(dev_t i_rdev)
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
1703 {
1704 ide_drive_t *drive;
1705 unsigned int p, major, minor;
1706 long flags;
1707
1708 if ((drive = get_info_ptr(i_rdev)) == NULL)
1709 return -ENODEV;
1710
1711 major = MAJOR(i_rdev) << 8;
1712 minor = drive->select.b.unit << PARTN_BITS;
1713 save_flags(flags);
1714 cli();
1715 if (drive->busy || (drive->usage > 1)) {
1716 restore_flags(flags);
1717 return -EBUSY;
1718 };
1719 drive->busy = 1;
1720 restore_flags(flags);
1721
1722 for (p = 0; p < (1<<PARTN_BITS); ++p) {
1723 sync_dev (major | (minor + p));
1724 invalidate_inodes (major | (minor + p));
1725 invalidate_buffers (major | (minor + p));
1726 drive->part[p].start_sect = 0;
1727 drive->part[p].nr_sects = 0;
1728 };
1729
1730 drive->part[0].nr_sects = current_capacity(drive);
1731 if (drive->media == disk)
1732 resetup_one_dev(HWIF(drive)->gd, drive->select.b.unit);
1733
1734 drive->busy = 0;
1735 wake_up(&drive->wqueue);
1736 return 0;
1737 }
1738
1739 static int write_fs_long (unsigned long useraddr, long value)
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
1740 {
1741 int err;
1742
1743 if (NULL == (long *)useraddr)
1744 return -EINVAL;
1745 if ((err = verify_area(VERIFY_WRITE, (long *)useraddr, sizeof(long))))
1746 return err;
1747 put_user((unsigned)value, (long *) useraddr);
1748 return 0;
1749 }
1750
1751 static int ide_ioctl (struct inode *inode, struct file *file,
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
1752 unsigned int cmd, unsigned long arg)
1753 {
1754 struct hd_geometry *loc = (struct hd_geometry *) arg;
1755 int err;
1756 ide_drive_t *drive;
1757 unsigned long flags;
1758
1759 if (!inode || !inode->i_rdev)
1760 return -EINVAL;
1761 if ((drive = get_info_ptr(inode->i_rdev)) == NULL)
1762 return -ENODEV;
1763 switch (cmd) {
1764 case HDIO_GETGEO:
1765 if (!loc || drive->media != disk) return -EINVAL;
1766 err = verify_area(VERIFY_WRITE, loc, sizeof(*loc));
1767 if (err) return err;
1768 put_user(drive->bios_head, (byte *) &loc->heads);
1769 put_user(drive->bios_sect, (byte *) &loc->sectors);
1770 put_user(drive->bios_cyl, (unsigned short *) &loc->cylinders);
1771 put_user((unsigned)drive->part[inode->i_rdev&PARTN_MASK].start_sect,
1772 (unsigned long *) &loc->start);
1773 return 0;
1774
1775 case BLKFLSBUF:
1776 if(!suser()) return -EACCES;
1777 fsync_dev(inode->i_rdev);
1778 invalidate_buffers(inode->i_rdev);
1779 return 0;
1780
1781 case BLKRASET:
1782 if(!suser()) return -EACCES;
1783 if(arg > 0xff) return -EINVAL;
1784 read_ahead[MAJOR(inode->i_rdev)] = arg;
1785 return 0;
1786
1787 case BLKRAGET:
1788 return write_fs_long(arg, read_ahead[MAJOR(inode->i_rdev)]);
1789
1790 case BLKGETSIZE: /* Return device size */
1791 return write_fs_long(arg, drive->part[inode->i_rdev&PARTN_MASK].nr_sects);
1792 case BLKRRPART: /* Re-read partition tables */
1793 return revalidate_disk(inode->i_rdev);
1794
1795 case HDIO_GET_KEEPSETTINGS:
1796 return write_fs_long(arg, drive->keep_settings);
1797
1798 case HDIO_GET_UNMASKINTR:
1799 return write_fs_long(arg, drive->unmask);
1800
1801 case HDIO_GET_DMA:
1802 return write_fs_long(arg, drive->using_dma);
1803
1804 case HDIO_GET_CHIPSET:
1805 return write_fs_long(arg, drive->chipset);
1806
1807 case HDIO_GET_MULTCOUNT:
1808 return write_fs_long(arg, drive->mult_count);
1809
1810 case HDIO_GET_IDENTITY:
1811 if (!arg || (inode->i_rdev & PARTN_MASK))
1812 return -EINVAL;
1813 if (drive->id == NULL)
1814 return -ENOMSG;
1815 err = verify_area(VERIFY_WRITE, (char *)arg, sizeof(*drive->id));
1816 if (!err)
1817 memcpy_tofs((char *)arg, (char *)drive->id, sizeof(*drive->id));
1818 return err;
1819
1820 case HDIO_GET_NOWERR:
1821 return write_fs_long(arg, drive->bad_wstat == BAD_R_STAT);
1822
1823 case HDIO_SET_DMA:
1824 if (drive->media != disk)
1825 return -EPERM;
1826 if (!drive->id || !(drive->id->capability & 1) || !HWIF(drive)->dmaproc)
1827 return -EPERM;
1828 case HDIO_SET_KEEPSETTINGS:
1829 case HDIO_SET_UNMASKINTR:
1830 case HDIO_SET_NOWERR:
1831 if (arg > 1)
1832 return -EINVAL;
1833 case HDIO_SET_CHIPSET:
1834 if (!suser())
1835 return -EACCES;
1836 if ((inode->i_rdev & PARTN_MASK))
1837 return -EINVAL;
1838 save_flags(flags);
1839 cli();
1840 switch (cmd) {
1841 case HDIO_SET_DMA:
1842 drive->using_dma = arg;
1843 break;
1844 case HDIO_SET_KEEPSETTINGS:
1845 drive->keep_settings = arg;
1846 break;
1847 case HDIO_SET_UNMASKINTR:
1848 if (arg && disallow_unmask) {
1849 restore_flags(flags);
1850 return -EPERM;
1851 }
1852 drive->unmask = arg;
1853 break;
1854 case HDIO_SET_NOWERR:
1855 drive->bad_wstat = arg ? BAD_R_STAT : BAD_W_STAT;
1856 break;
1857 case HDIO_SET_CHIPSET:
1858 drive->chipset = arg;
1859 drive->vlb_32bit = (arg & 1);
1860 drive->vlb_sync = (arg & 2) >> 1;
1861 #ifndef VLB_SYNC
1862 if (drive->vlb_sync)
1863 printk("%s: VLB_SYNC not supported by this kernel\n", drive->name);
1864 #endif
1865 break;
1866 }
1867 restore_flags(flags);
1868 return 0;
1869
1870 case HDIO_SET_MULTCOUNT:
1871 if (!suser())
1872 return -EACCES;
1873 if (inode->i_rdev & PARTN_MASK)
1874 return -EINVAL;
1875 if ((drive->id != NULL) && (arg > drive->id->max_multsect))
1876 return -EINVAL;
1877 save_flags(flags);
1878 cli();
1879 if (drive->special.b.set_multmode) {
1880 restore_flags(flags);
1881 return -EBUSY;
1882 }
1883 drive->mult_req = arg;
1884 drive->special.b.set_multmode = 1;
1885 restore_flags(flags);
1886 ide_do_drive_cmd (inode->i_rdev, NULL);
1887 return (drive->mult_count == arg) ? 0 : -EIO;
1888
1889 case HDIO_DRIVE_CMD:
1890 {
1891 unsigned long args;
1892
1893 if (NULL == (long *) arg)
1894 err = ide_do_drive_cmd(inode->i_rdev,NULL);
1895 else {
1896 if (!(err = verify_area(VERIFY_READ,(long *)arg,sizeof(long))))
1897 {
1898 args = get_user((long *)arg);
1899 if (!(err = verify_area(VERIFY_WRITE,(long *)arg,sizeof(long)))) {
1900 err = ide_do_drive_cmd(inode->i_rdev,(char *)&args);
1901 put_user(args,(long *)arg);
1902 }
1903 }
1904 }
1905 return err;
1906 }
1907
1908 RO_IOCTLS(inode->i_rdev, arg);
1909
1910 default:
1911 #ifdef CONFIG_BLK_DEV_IDECD
1912 if (drive->media == cdrom)
1913 return ide_cdrom_ioctl(drive, inode, file, cmd, arg);
1914 #endif /* CONFIG_BLK_DEV_IDECD */
1915 return -EPERM;
1916 }
1917 }
1918
1919 static int ide_check_media_change (dev_t i_rdev)
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
1920 {
1921 ide_drive_t *drive;
1922
1923 if ((drive = get_info_ptr(i_rdev)) == NULL)
1924 return -ENODEV;
1925 #ifdef CONFIG_BLK_DEV_IDECD
1926 if (drive->media == cdrom)
1927 return ide_cdrom_check_media_change (drive);
1928 #endif /* CONFIG_BLK_DEV_IDECD */
1929 if (drive->removeable) /* for disks */
1930 return 1; /* always assume it was changed */
1931 return 0;
1932 }
1933
1934 static void fixstring (byte *s, const int bytecount, const int byteswap)
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
1935 {
1936 byte *p = s, *end = &s[bytecount & ~1]; /* bytecount must be even */
1937
1938 if (byteswap) {
1939 /* convert from big-endian to host byte order */
1940 for (p = end ; p != s;) {
1941 unsigned short *pp = (unsigned short *) (p -= 2);
1942 *pp = ntohs(*pp);
1943 }
1944 }
1945
1946 /* strip leading blanks */
1947 while (s != end && *s == ' ')
1948 ++s;
1949
1950 /* compress internal blanks and strip trailing blanks */
1951 while (s != end && *s) {
1952 if (*s++ != ' ' || (s != end && *s && *s != ' '))
1953 *p++ = *(s-1);
1954 }
1955
1956 /* wipe out trailing garbage */
1957 while (p != end)
1958 *p++ = '\0';
1959 }
1960
1961 static inline void do_identify (ide_drive_t *drive, byte cmd)
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
1962 {
1963 int bswap;
1964 struct hd_driveid *id;
1965 unsigned long capacity, check;
1966
1967 id = drive->id = ide_alloc (SECTOR_WORDS*4, 0);
1968 ide_input_data(drive, id, SECTOR_WORDS); /* read 512 bytes of id info */
1969 sti();
1970
1971 /*
1972 * EATA SCSI controllers do a hardware ATA emulation: ignore them
1973 */
1974 if ((id->model[0] == 'P' && id->model[1] == 'M')
1975 || (id->model[0] == 'S' && id->model[1] == 'K')) {
1976 printk("%s: EATA SCSI HBA %.10s\n", drive->name, id->model);
1977 drive->present = 0;
1978 return;
1979 }
1980
1981 /*
1982 * WIN_IDENTIFY returns little-endian info,
1983 * WIN_PIDENTIFY *usually* returns little-endian info.
1984 */
1985 bswap = 1;
1986 if (cmd == WIN_PIDENTIFY) {
1987 if ((id->model[0] == 'N' && id->model[1] == 'E') /* NEC */
1988 || (id->model[0] == 'F' && id->model[1] == 'X') /* Mitsumi */
1989 || (id->model[0] == 'P' && id->model[1] == 'i'))/* Pioneer */
1990 bswap = 0; /* Vertos drives may still be weird */
1991 }
1992 fixstring (id->model, sizeof(id->model), bswap);
1993 fixstring (id->fw_rev, sizeof(id->fw_rev), bswap);
1994 fixstring (id->serial_no, sizeof(id->serial_no), bswap);
1995
1996 /*
1997 * Check for an ATAPI device
1998 */
1999 if (cmd == WIN_PIDENTIFY) {
2000 #ifdef CONFIG_BLK_DEV_IDECD
2001 byte type = (id->config >> 8) & 0x0f;
2002 #endif /* CONFIG_BLK_DEV_IDECD */
2003 printk("%s: %s, ATAPI, ", drive->name, id->model);
2004 drive->media = cdrom;
2005 #ifdef CONFIG_BLK_DEV_IDECD
2006 if (type == 0 || type == 5)
2007 printk("CDROM drive\n");
2008 else
2009 printk("UNKNOWN device\n");
2010 drive->present = 1;
2011 drive->removeable = 1;
2012 #else
2013 printk("not supported by this kernel\n");
2014 #endif /* CONFIG_BLK_DEV_IDECD */
2015 return;
2016 }
2017
2018 /* check for removeable disks (eg. SYQUEST), ignore 'WD' drives */
2019 if (id->config & (1<<7)) { /* removeable disk ? */
2020 if (id->model[0] != 'W' || id->model[1] != 'D')
2021 drive->removeable = 1;
2022 }
2023
2024 drive->media = disk;
2025 /* Extract geometry if we did not already have one for the drive */
2026 if (!drive->present) {
2027 drive->present = 1;
2028 drive->cyl = drive->bios_cyl = id->cyls;
2029 drive->head = drive->bios_head = id->heads;
2030 drive->sect = drive->bios_sect = id->sectors;
2031 }
2032 /* Handle logical geometry translation by the drive */
2033 if ((id->field_valid & 1) && id->cur_cyls && id->cur_heads
2034 && (id->cur_heads <= 16) && id->cur_sectors)
2035 {
2036 /*
2037 * Extract the physical drive geometry for our use.
2038 * Note that we purposely do *not* update the bios info.
2039 * This way, programs that use it (like fdisk) will
2040 * still have the same logical view as the BIOS does,
2041 * which keeps the partition table from being screwed.
2042 *
2043 * An exception to this is the cylinder count,
2044 * which we reexamine later on to correct for 1024 limitations.
2045 */
2046 drive->cyl = id->cur_cyls;
2047 drive->head = id->cur_heads;
2048 drive->sect = id->cur_sectors;
2049
2050 /* check for word-swapped "capacity" field in id information */
2051 capacity = drive->cyl * drive->head * drive->sect;
2052 check = (id->cur_capacity0 << 16) | id->cur_capacity1;
2053 if (check == capacity) { /* was it swapped? */
2054 /* yes, bring it into little-endian order: */
2055 id->cur_capacity0 = (capacity >> 0) & 0xffff;
2056 id->cur_capacity1 = (capacity >> 16) & 0xffff;
2057 }
2058 }
2059 /* Use physical geometry if what we have still makes no sense */
2060 if ((!drive->head || drive->head > 16) && id->heads && id->heads <= 16) {
2061 drive->cyl = id->cyls;
2062 drive->head = id->heads;
2063 drive->sect = id->sectors;
2064 }
2065 /* Correct the number of cyls if the bios value is too small */
2066 if (drive->sect == drive->bios_sect && drive->head == drive->bios_head) {
2067 if (drive->cyl > drive->bios_cyl)
2068 drive->bios_cyl = drive->cyl;
2069 }
2070
2071 (void) current_capacity (drive); /* initialize LBA selection */
2072
2073 printk ("%s: %.40s, %ldMB w/%dKB Cache, %sCHS=%d/%d/%d",
2074 drive->name, id->model, current_capacity(drive)/2048L, id->buf_size/2,
2075 drive->select.b.lba ? "LBA, " : "",
2076 drive->bios_cyl, drive->bios_head, drive->bios_sect);
2077
2078 drive->mult_count = 0;
2079 if (id->max_multsect) {
2080 drive->mult_req = INITIAL_MULT_COUNT;
2081 if (drive->mult_req > id->max_multsect)
2082 drive->mult_req = id->max_multsect;
2083 if (drive->mult_req || ((id->multsect_valid & 1) && id->multsect))
2084 drive->special.b.set_multmode = 1;
2085 }
2086 if (HWIF(drive)->dmaproc != NULL) { /* hwif supports DMA? */
2087 if (!(HWIF(drive)->dmaproc(ide_dma_check, drive)))
2088 printk(", DMA");
2089 }
2090 printk("\n");
2091 }
2092
2093 /*
2094 * Delay for *at least* 10ms. As we don't know how much time is left
2095 * until the next tick occurs, we wait an extra tick to be safe.
2096 */
2097 static void delay_10ms (void)
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
2098 {
2099 unsigned long timer = jiffies + (HZ + 99)/100 + 1;
2100 while (timer > jiffies);
2101 }
2102
2103 /*
2104 * try_to_identify() sends an ATA(PI) IDENTIFY request to a drive
2105 * and waits for a response. It also monitors irqs while this is
2106 * happening, in hope of automatically determining which one is
2107 * being used by the interface.
2108 *
2109 * Returns: 0 device was identified
2110 * 1 device timed-out (no response to identify request)
2111 * 2 device aborted the command (refused to identify itself)
2112 */
2113 static int try_to_identify (ide_drive_t *drive, byte cmd)
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
2114 {
2115 int hd_status, rc;
2116 unsigned long timeout;
2117 int irqs = 0;
2118
2119 if (!HWIF(drive)->irq) { /* already got an IRQ? */
2120 probe_irq_off(probe_irq_on()); /* clear dangling irqs */
2121 irqs = probe_irq_on(); /* start monitoring irqs */
2122 OUT_BYTE(drive->ctl,IDE_CONTROL_REG); /* enable device irq */
2123 }
2124
2125 delay_10ms(); /* take a deep breath */
2126 if ((IN_BYTE(IDE_ALTSTATUS_REG) ^ IN_BYTE(IDE_STATUS_REG)) & ~INDEX_STAT) {
2127 printk("%s: probing with STATUS instead of ALTSTATUS\n", drive->name);
2128 hd_status = IDE_STATUS_REG; /* ancient Seagate drives */
2129 } else
2130 hd_status = IDE_ALTSTATUS_REG; /* use non-intrusive polling */
2131
2132 OUT_BYTE(cmd,IDE_COMMAND_REG); /* ask drive for ID */
2133 timeout = ((cmd == WIN_IDENTIFY) ? WAIT_WORSTCASE : WAIT_PIDENTIFY) / 2;
2134 timeout += jiffies;
2135 do {
2136 if (jiffies > timeout) {
2137 if (!HWIF(drive)->irq)
2138 (void) probe_irq_off(irqs);
2139 return 1; /* drive timed-out */
2140 }
2141 delay_10ms(); /* give drive a breather */
2142 } while (IN_BYTE(hd_status) & BUSY_STAT);
2143
2144 delay_10ms(); /* wait for IRQ and DRQ_STAT */
2145 if (OK_STAT(GET_STAT(),DRQ_STAT,BAD_R_STAT)) {
2146 cli(); /* some systems need this */
2147 do_identify(drive, cmd); /* drive returned ID */
2148 rc = 0; /* success */
2149 } else
2150 rc = 2; /* drive refused ID */
2151 if (!HWIF(drive)->irq) {
2152 irqs = probe_irq_off(irqs); /* get irq number */
2153 if (irqs > 0)
2154 HWIF(drive)->irq = irqs;
2155 else /* Mmmm.. multiple IRQs */
2156 printk("%s: IRQ probe failed (%d)\n", drive->name, irqs);
2157 }
2158 return rc;
2159 }
2160
2161 /*
2162 * do_probe() has the difficult job of finding a drive if it exists,
2163 * without getting hung up if it doesn't exist, without trampling on
2164 * ethernet cards, and without leaving any IRQs dangling to haunt us later.
2165 *
2166 * If a drive is "known" to exist (from CMOS or kernel parameters),
2167 * but does not respond right away, the probe will "hang in there"
2168 * for the maximum wait time (about 30 seconds), otherwise it will
2169 * exit much more quickly.
2170 *
2171 * Returns: 0 device was identified
2172 * 1 device timed-out (no response to identify request)
2173 * 2 device aborted the command (refused to identify itself)
2174 * 3 bad status from device (possible for ATAPI drives)
2175 * 4 probe was not attempted because failure was obvious
2176 */
2177 static int do_probe (ide_drive_t *drive, byte cmd)
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
2178 {
2179 int rc;
2180 #ifdef CONFIG_BLK_DEV_IDECD
2181 if (drive->present) { /* avoid waiting for inappropriate probes */
2182 if ((drive->media == cdrom) && (cmd == WIN_IDENTIFY))
2183 return 4;
2184 }
2185 #endif /* CONFIG_BLK_DEV_IDECD */
2186 #ifdef DEBUG
2187 printk("probing for %s: present=%d, type=%s, probetype=%s\n",
2188 drive->name, drive->present, drive->media ? "cdrom":"disk",
2189 (cmd == WIN_IDENTIFY) ? "ATA" : "ATAPI");
2190 #endif
2191 #if SUPPORT_HT6560B
2192 if (HWIF(drive)->select)
2193 ide_hwif_select (HWIF(drive));
2194 #endif
2195 OUT_BYTE(drive->select.all,IDE_SELECT_REG); /* select target drive */
2196 delay_10ms(); /* wait for BUSY_STAT */
2197 if (IN_BYTE(IDE_SELECT_REG) != drive->select.all && !drive->present) {
2198 OUT_BYTE(0xa0,IDE_SELECT_REG); /* exit with drive0 selected */
2199 return 3; /* no i/f present: avoid killing ethernet cards */
2200 }
2201
2202 if (OK_STAT(GET_STAT(),READY_STAT,BUSY_STAT)
2203 || drive->present || cmd == WIN_PIDENTIFY)
2204 {
2205 if ((rc = try_to_identify(drive,cmd))) /* send cmd and wait */
2206 rc = try_to_identify(drive,cmd); /* failed: try again */
2207 if (rc == 1)
2208 printk("%s: no response (status = 0x%02x)\n", drive->name, GET_STAT());
2209 (void) GET_STAT(); /* ensure drive irq is clear */
2210 } else {
2211 rc = 3; /* not present or maybe ATAPI */
2212 }
2213 if (drive->select.b.unit != 0) {
2214 OUT_BYTE(0xa0,IDE_SELECT_REG); /* exit with drive0 selected */
2215 delay_10ms();
2216 (void) GET_STAT(); /* ensure drive irq is clear */
2217 }
2218 return rc;
2219 }
2220
2221 /*
2222 * probe_for_drive() tests for existance of a given drive using do_probe().
2223 *
2224 * Returns: 0 no device was found
2225 * 1 device was found (note: drive->present might still be 0)
2226 */
2227 static inline byte probe_for_drive (ide_drive_t *drive)
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
2228 {
2229 if (drive->noprobe) /* skip probing? */
2230 return drive->present;
2231 if (do_probe(drive, WIN_IDENTIFY) >= 2) { /* if !(success||timed-out) */
2232 #ifdef CONFIG_BLK_DEV_IDECD
2233 (void) do_probe(drive, WIN_PIDENTIFY); /* look for ATAPI device */
2234 #endif /* CONFIG_BLK_DEV_IDECD */
2235 }
2236 if (!drive->present)
2237 return 0; /* drive not found */
2238 if (drive->id == NULL) { /* identification failed? */
2239 if (drive->media == disk) {
2240 printk ("%s: non-IDE drive, CHS=%d/%d/%d\n",
2241 drive->name, drive->cyl, drive->head, drive->sect);
2242 }
2243 #ifdef CONFIG_BLK_DEV_IDECD
2244 else if (drive->media == cdrom) {
2245 printk("%s: ATAPI cdrom (?)\n", drive->name);
2246 }
2247 #endif /* CONFIG_BLK_DEV_IDECD */
2248 else {
2249 drive->present = 0; /* nuke it */
2250 return 1; /* drive was found */
2251 }
2252 }
2253 if (drive->media == disk && !drive->select.b.lba) {
2254 if (!drive->head || drive->head > 16) {
2255 printk("%s: INVALID GEOMETRY: %d PHYSICAL HEADS?\n",
2256 drive->name, drive->head);
2257 drive->present = 0;
2258 }
2259 }
2260 return 1; /* drive was found */
2261 }
2262
2263 /*
2264 * This routine only knows how to look for drive units 0 and 1
2265 * on an interface, so any setting of MAX_DRIVES > 2 won't work here.
2266 */
2267 static void probe_for_drives (ide_hwif_t *hwif)
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
2268 {
2269 unsigned int unit;
2270
2271 if (check_region(hwif->io_base,8) || check_region(hwif->ctl_port,1)) {
2272 int msgout = 0;
2273 for (unit = 0; unit < MAX_DRIVES; ++unit) {
2274 ide_drive_t *drive = &hwif->drives[unit];
2275 if (drive->present) {
2276 drive->present = 0;
2277 printk("%s: ERROR, PORTS ALREADY IN USE\n", drive->name);
2278 msgout = 1;
2279 }
2280 }
2281 if (!msgout)
2282 printk("%s: ports already in use, skipping probe\n", hwif->name);
2283 } else {
2284 unsigned long flags;
2285 save_flags(flags);
2286
2287 #if (MAX_DRIVES > 2)
2288 printk("%s: probing for first 2 of %d possible drives\n", hwif->name, MAX_DRIVES);
2289 #endif
2290 sti(); /* needed for jiffies and irq probing */
2291 /*
2292 * Second drive should only exist if first drive was found,
2293 * but a lot of cdrom drives seem to be configured as slave-only
2294 */
2295 for (unit = 0; unit < 2; ++unit) /* note the hardcoded '2' */
2296 (void) probe_for_drive(&hwif->drives[unit]);
2297 for (unit = 0; unit < MAX_DRIVES; ++unit) {
2298 ide_drive_t *drive = &hwif->drives[unit];
2299 if (drive->present) {
2300 hwif->present = 1;
2301 request_region(hwif->io_base, 8, hwif->name);
2302 request_region(hwif->ctl_port, 1, hwif->name);
2303 #ifdef CONFIG_BLK_DEV_IDECD
2304 if (drive->media == cdrom)
2305 ide_cdrom_setup(drive);
2306 #endif /* CONFIG_BLK_DEV_IDECD */
2307 break;
2308 }
2309 }
2310 restore_flags(flags);
2311 }
2312 }
2313
2314 #if SUPPORT_DTC2278
2315 /*
2316 * From: andy@cercle.cts.com (Dyan Wile)
2317 *
2318 * Below is a patch for DTC-2278 - alike software-programmable controllers
2319 * The code enables the secondary IDE controller and the PIO4 (3?) timings on
2320 * the primary (EIDE). You may probably have to enable the 32-bit support to
2321 * get the full speed. You better get the disk interrupts disabled ( hdparm -u0
2322 * /dev/hd.. ) for the drives connected to the EIDE interface. (I get my
2323 * filesystem corrupted with -u1, but under heavy disk load only :-)
2324 *
2325 * From: mlord@bnr.ca -- this chipset is now forced to use the "serialize" feature,
2326 * which hopefully will make it more reliable to use.. maybe it has the same bugs
2327 * as the CMD640B and RZ1000 ??
2328 */
2329
2330 #if SET_DTC2278_MODE4
2331 static void sub22 (char b, char c)
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
2332 {
2333 int i;
2334
2335 for(i = 0; i < 3; ++i) {
2336 inb(0x3f6);
2337 outb_p(b,0xb0);
2338 inb(0x3f6);
2339 outb_p(c,0xb4);
2340 inb(0x3f6);
2341 if(inb(0xb4) == c) {
2342 outb_p(7,0xb0);
2343 inb(0x3f6);
2344 return; /* success */
2345 }
2346 }
2347 }
2348 #endif /* SET_DTC2278_MODE4 */
2349
2350 static void init_dtc2278 (void)
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
2351 {
2352 cli();
2353 #if SET_DTC2278_MODE4
2354 /*
2355 * This enables PIO mode4 (3?) on the first interface
2356 */
2357 sub22(1,0xc3);
2358 sub22(0,0xa0);
2359 #endif /* SET_DTC2278_MODE4 */
2360 /*
2361 * This enables the second interface
2362 */
2363 outb_p(4,0xb0);
2364 inb(0x3f6);
2365 outb_p(0x20,0xb4);
2366 inb(0x3f6);
2367
2368 sti();
2369 }
2370 #endif /* SUPPORT_DTC2278 */
2371
2372 #if SUPPORT_CMD640
2373 /*
2374 * ??? fixme:
2375 */
2376 byte read_cmd640_vlb (byte port, byte reg)
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
2377 {
2378 byte val;
2379
2380 unsigned long flags;
2381 save_flags(flags);
2382 cli();
2383 outw(reg, port);
2384 val = inb(port+4);
2385 restore_flags(flags);
2386 return val;
2387 }
2388
2389 void write_cmd640_vlb (byte port, byte reg, byte val)
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
2390 {
2391 unsigned long flags;
2392 save_flags(flags);
2393 cli();
2394 outw(reg, port);
2395 outw(val, port+4);
2396 restore_flags(flags);
2397 }
2398
2399 void init_cmd640_vlb (void)
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
2400 {
2401 byte reg;
2402 unsigned short port = 0x178;
2403
2404 single_threaded = 1;
2405 printk("ide: buggy CMD640 interface: serialized, ");
2406 reg = read_cmd640_vlb(port, 0x50);
2407 if (reg == 0xff || (reg & 0x90) != 0x90) {
2408 #if TRY_CMD640_VLB_AT_0x78
2409 port = 0x78;
2410 if (reg == 0xff || (reg & 0x90) != 0x90)
2411 #endif
2412 {
2413 disallow_unmask = 1;
2414 printk("(probe failed) disabled unmasking\n");
2415 return;
2416 }
2417 }
2418 write_cmd640_vlb(port, 0x51, read_cmd640_vlb(port, 0x51)|0xc8);
2419 printk("disabled read-ahead, enabled secondary\n");
2420
2421 }
2422 #endif /* SUPPORT_CMD640 */
2423
2424 /*
2425 * stridx() returns the offset of c within s,
2426 * or -1 if c is '\0' or not found within s.
2427 */
2428 static int stridx (const char *s, char c)
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
2429 {
2430 char *i = strchr(s, c);
2431 return (i && c) ? i - s : -1;
2432 }
2433
2434 /*
2435 * match_parm() does parsing for ide_setup():
2436 *
2437 * 1. the first char of s must be '='.
2438 * 2. if the remainder matches one of the supplied keywords,
2439 * the index (1 based) of the keyword is negated and returned.
2440 * 3. if the remainder is a series of no more than max_vals numbers
2441 * separated by commas, the numbers are saved in vals[] and a
2442 * count of how many were saved is returned. Base10 is assumed,
2443 * and base16 is allowed when prefixed with "0x".
2444 * 4. otherwise, zero is returned.
2445 */
2446 static int match_parm (char *s, const char *keywords[], int vals[], int max_vals)
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
2447 {
2448 static const char *decimal = "0123456789";
2449 static const char *hex = "0123456789abcdef";
2450 int i, n;
2451
2452 if (*s++ == '=') {
2453 /*
2454 * Try matching against the supplied keywords,
2455 * and return -(index+1) if we match one
2456 */
2457 for (i = 0; *keywords != NULL; ++i) {
2458 if (!strcmp(s, *keywords++))
2459 return -(i+1);
2460 }
2461 /*
2462 * Look for a series of no more than "max_vals"
2463 * numeric values separated by commas, in base10,
2464 * or base16 when prefixed with "0x".
2465 * Return a count of how many were found.
2466 */
2467 for (n = 0; (i = stridx(decimal, *s)) >= 0;) {
2468 vals[n] = i;
2469 while ((i = stridx(decimal, *++s)) >= 0)
2470 vals[n] = (vals[n] * 10) + i;
2471 if (*s == 'x' && !vals[n]) {
2472 while ((i = stridx(hex, *++s)) >= 0)
2473 vals[n] = (vals[n] * 0x10) + i;
2474 }
2475 if (++n == max_vals)
2476 break;
2477 if (*s == ',')
2478 ++s;
2479 }
2480 if (!*s)
2481 return n;
2482 }
2483 return 0; /* zero = nothing matched */
2484 }
2485
2486 /*
2487 * ide_setup() gets called VERY EARLY during initialization,
2488 * to handle kernel "command line" strings beginning with "hdx="
2489 * or "ide". Here is the complete set currently supported:
2490 *
2491 * "hdx=" is recognized for all "x" from "a" to "h", such as "hdc".
2492 * "idex=" is recognized for all "x" from "0" to "3", such as "ide1".
2493 *
2494 * "hdx=noprobe" : drive may be present, but do not probe for it
2495 * "hdx=nowerr" : ignore the WRERR_STAT bit on this drive
2496 * "hdx=cdrom" : drive is present, and is a cdrom drive
2497 * "hdx=cyl,head,sect" : disk drive is present, with specified geometry
2498 *
2499 * "idex=noprobe" : do not attempt to access/use this interface
2500 * "idex=base" : probe for an interface at the addr specified,
2501 * where "base" is usually 0x1f0 or 0x170
2502 * and "ctl" is assumed to be "base"+0x206
2503 * "idex=base,ctl" : specify both base and ctl
2504 * "idex=base,ctl,irq" : specify base, ctl, and irq number
2505 *
2506 * The following two are valid ONLY on ide0 or ide1,
2507 * and the defaults for the base,ctl ports must not be altered.
2508 *
2509 * "idex=serialize" : do not overlap operations on ide0 and ide1.
2510 * "idex=dtc2278" : enables use of DTC2278 secondary i/f
2511 * "idex=ht6560b" : enables use of HT6560B secondary i/f
2512 * "idex=cmd640_vlb" : required for VLB cards with the CMD640 chip
2513 * (PCI version will be automatically detected)
2514 */
2515 void ide_setup (char *s)
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
2516 {
2517 int vals[3];
2518 ide_hwif_t *hwif;
2519 ide_drive_t *drive;
2520 unsigned int hw, unit;
2521 const char max_drive = 'a' + ((MAX_HWIFS * MAX_DRIVES) - 1);
2522 const char max_hwif = '0' + (MAX_HWIFS - 1);
2523
2524 printk("ide_setup: %s", s);
2525 init_ide_data ();
2526
2527 /*
2528 * Look for drive options: "hdx="
2529 */
2530 if (s[0] == 'h' && s[1] == 'd' && s[2] >= 'a' && s[2] <= max_drive) {
2531 const char *hd_words[] = {"noprobe", "nowerr", "cdrom", "serialize", NULL};
2532 unit = s[2] - 'a';
2533 hw = unit / MAX_DRIVES;
2534 unit = unit % MAX_DRIVES;
2535 hwif = &ide_hwifs[hw];
2536 drive = &hwif->drives[unit];
2537 switch (match_parm(&s[3], hd_words, vals, 3)) {
2538 case -1: /* "noprobe" */
2539 drive->noprobe = 1;
2540 goto done;
2541 case -2: /* "nowerr" */
2542 drive->bad_wstat = BAD_R_STAT;
2543 hwif->noprobe = 0;
2544 goto done;
2545 case -3: /* "cdrom" */
2546 drive->present = 1;
2547 drive->media = cdrom;
2548 hwif->noprobe = 0;
2549 goto done;
2550 case -4: /* "serialize" */
2551 printk(" -- USE ""ide%c=serialize"" INSTEAD", '0'+hw);
2552 goto do_serialize;
2553 case 3: /* cyl,head,sect */
2554 drive->media = disk;
2555 drive->cyl = drive->bios_cyl = vals[0];
2556 drive->head = drive->bios_head = vals[1];
2557 drive->sect = drive->bios_sect = vals[2];
2558 drive->present = 1;
2559 hwif->noprobe = 0;
2560 goto done;
2561 default:
2562 goto bad_option;
2563 }
2564 }
2565 /*
2566 * Look for interface options: "idex="
2567 */
2568 if (s[0] == 'i' && s[1] == 'd' && s[2] == 'e' && s[3] >= '0' && s[3] <= max_hwif) {
2569 const char *ide_words[] = {"noprobe", "serialize", "dtc2278", "ht6560b", "cmd640_vlb", NULL};
2570 hw = s[3] - '0';
2571 hwif = &ide_hwifs[hw];
2572
2573 switch (match_parm(&s[4], ide_words, vals, 3)) {
2574 #if SUPPORT_CMD640
2575 case -5: /* "cmd640_vlb" */
2576 if (hw > 1) goto bad_hwif;
2577 init_cmd640_vlb();
2578 goto do_serialize; /* not necessary once we implement the above */
2579 break;
2580 #endif /* SUPPORT_CMD640 */
2581 #if SUPPORT_HT6560B
2582 case -4: /* "ht6560b" */
2583 if (hw > 1) goto bad_hwif;
2584 /*
2585 * Using 0x1c and 0x1d apparently selects a
2586 * faster interface speed than 0x3c and 0x3d.
2587 *
2588 * Need to add an ioctl to select between them.
2589 */
2590 if (check_region(0x3e6,1)) {
2591 printk(" -- HT6560 PORT 0x3e6 ALREADY IN USE");
2592 goto done;
2593 }
2594 request_region(0x3e6, 1, hwif->name);
2595 ide_hwifs[0].select = 0x3c;
2596 ide_hwifs[1].select = 0x3d;
2597 goto do_serialize;
2598 #endif /* SUPPORT_HT6560B */
2599 #if SUPPORT_DTC2278
2600 case -3: /* "dtc2278" */
2601 if (hw > 1) goto bad_hwif;
2602 init_dtc2278();
2603 goto do_serialize;
2604 #endif /* SUPPORT_DTC2278 */
2605 case -2: /* "serialize" */
2606 do_serialize:
2607 if (hw > 1) goto bad_hwif;
2608 single_threaded = 1;
2609 goto done;
2610 case -1: /* "noprobe" */
2611 hwif->noprobe = 1;
2612 goto done;
2613 case 1: /* base */
2614 vals[1] = vals[0] + 0x206; /* default ctl */
2615 case 2: /* base,ctl */
2616 vals[2] = 0; /* default irq = probe for it */
2617 case 3: /* base,ctl,irq */
2618 hwif->io_base = vals[0];
2619 hwif->ctl_port = vals[1];
2620 hwif->irq = vals[2];
2621 hwif->noprobe = 0;
2622 goto done;
2623 }
2624 }
2625 bad_option:
2626 printk(" -- BAD OPTION\n");
2627 return;
2628 bad_hwif:
2629 printk("-- NOT SUPPORTED ON ide%d", hw);
2630 done:
2631 printk("\n");
2632 }
2633
2634 /*
2635 * This routine is called from the partition-table code in genhd.c
2636 * to "convert" a drive to a logical geometry with fewer than 1024 cyls
2637 * It mimics the method used by Ontrack Disk Manager.
2638 */
2639 int ide_xlate_1024 (dev_t i_rdev, int need_offset, const char *msg)
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
2640 {
2641 ide_drive_t *drive;
2642 static const byte head_vals[] = {4, 8, 16, 32, 64, 128, 255, 0};
2643 const byte *heads = head_vals;
2644 unsigned long tracks;
2645
2646 if ((drive = get_info_ptr(i_rdev)) == NULL || drive->id == NULL)
2647 return 0;
2648
2649 drive->cyl = drive->bios_cyl = drive->id->cyls;
2650 drive->head = drive->bios_head = drive->id->heads;
2651 drive->sect = drive->bios_sect = drive->id->sectors;
2652 drive->special.b.set_geometry = 1;
2653
2654 tracks = drive->bios_cyl * drive->bios_head * drive->bios_sect / 63;
2655 drive->bios_sect = 63;
2656 while (drive->bios_cyl >= 1024) {
2657 drive->bios_head = *heads;
2658 drive->bios_cyl = tracks / drive->bios_head;
2659 if (0 == *++heads)
2660 break;
2661 }
2662 if (need_offset) {
2663 drive->sect0 = 63;
2664 drive->bios_cyl = (tracks - 1) / drive->bios_head;
2665 }
2666 drive->part[0].nr_sects = current_capacity(drive);
2667 printk("%s [+%d,%d/%d/%d]", msg, drive->sect0, drive->bios_cyl, drive->bios_head, drive->bios_sect);
2668 return 1;
2669 }
2670
2671 /*
2672 * We query CMOS about hard disks : it could be that we have a SCSI/ESDI/etc
2673 * controller that is BIOS compatible with ST-506, and thus showing up in our
2674 * BIOS table, but not register compatible, and therefore not present in CMOS.
2675 *
2676 * Furthermore, we will assume that our ST-506 drives <if any> are the primary
2677 * drives in the system -- the ones reflected as drive 1 or 2. The first
2678 * drive is stored in the high nibble of CMOS byte 0x12, the second in the low
2679 * nibble. This will be either a 4 bit drive type or 0xf indicating use byte
2680 * 0x19 for an 8 bit type, drive 1, 0x1a for drive 2 in CMOS. A non-zero value
2681 * means we have an AT controller hard disk for that drive.
2682 *
2683 * Of course, there is no guarantee that either drive is actually on the
2684 * "primary" IDE interface, but we don't bother trying to sort that out here.
2685 * If a drive is not actually on the primary interface, then these parameters
2686 * will be ignored. This results in the user having to supply the logical
2687 * drive geometry as a boot parameter for each drive not on the primary i/f.
2688 *
2689 * The only "perfect" way to handle this would be to modify the setup.[cS] code
2690 * to do BIOS calls Int13h/Fn08h and Int13h/Fn48h to get all of the drive info
2691 * for us during initialization. I have the necessary docs -- any takers? -ml
2692 */
2693
2694 static void probe_cmos_for_drives (ide_hwif_t *hwif)
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
2695 {
2696 #ifdef __i386__
2697 extern struct drive_info_struct drive_info;
2698 byte cmos_disks, *BIOS = (byte *) &drive_info;
2699 int unit;
2700
2701 outb_p(0x12,0x70); /* specify CMOS address 0x12 */
2702 cmos_disks = inb_p(0x71); /* read the data from 0x12 */
2703 /* Extract drive geometry from CMOS+BIOS if not already setup */
2704 for (unit = 0; unit < MAX_DRIVES; ++unit) {
2705 ide_drive_t *drive = &hwif->drives[unit];
2706 if ((cmos_disks & (0xf0 >> (unit*4))) && !drive->present) {
2707 drive->cyl = drive->bios_cyl = *(unsigned short *)BIOS;
2708 drive->head = drive->bios_head = *(BIOS+2);
2709 drive->sect = drive->bios_sect = *(BIOS+14);
2710 drive->ctl = *(BIOS+8);
2711 drive->present = 1;
2712 }
2713 BIOS += 16;
2714 }
2715 #endif
2716 }
2717
2718 /*
2719 * This routine sets up the irq for an ide interface, and creates a new
2720 * hwgroup for the irq/hwif if none was previously assigned.
2721 *
2722 * The SA_INTERRUPT in sa_flags means ide_intr() is always entered with
2723 * interrupts completely disabled. This can be bad for interrupt latency,
2724 * but anything else has led to problems on some machines. We re-enable
2725 * interrupts as much as we can safely do in most places.
2726 */
2727 static int init_irq (ide_hwif_t *hwif)
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
2728 {
2729 unsigned long flags;
2730 ide_hwgroup_t *hwgroup;
2731
2732 /*
2733 * First, we try to grab the irq
2734 */
2735 save_flags(flags);
2736 cli();
2737 if (request_irq(hwif->irq, ide_intr, SA_INTERRUPT, hwif->name)) {
2738 restore_flags(flags);
2739 printk(" -- FAILED!");
2740 return 1;
2741 }
2742
2743 /*
2744 * Got the irq, now set everything else up
2745 */
2746 if ((hwgroup = irq_to_hwgroup[hwif->irq]) == NULL) {
2747 hwgroup = ide_alloc (sizeof(ide_hwgroup_t), 0);
2748 irq_to_hwgroup[hwif->irq] = hwgroup;
2749 hwgroup->hwif = hwif->next = hwif;
2750 hwgroup->rq = NULL;
2751 hwgroup->handler = NULL;
2752 hwgroup->drive = NULL;
2753 hwgroup->reset_timeout = 0;
2754 #ifdef CONFIG_BLK_DEV_IDECD
2755 hwgroup->doing_atapi_reset = 0;
2756 #endif /* CONFIG_BLK_DEV_IDECD */
2757 init_timer(&hwgroup->timer);
2758 hwgroup->timer.function = &timer_expiry;
2759 hwgroup->timer.data = (unsigned long) hwgroup;
2760 } else {
2761 hwif->next = hwgroup->hwif->next;
2762 hwgroup->hwif->next = hwif;
2763 }
2764 hwif->hwgroup = hwgroup;
2765
2766 restore_flags(flags); /* safe now that hwif->hwgroup is set up */
2767
2768 printk("%s at 0x%03x-0x%03x,0x%03x on irq %d", hwif->name,
2769 hwif->io_base, hwif->io_base+7, hwif->ctl_port, hwif->irq);
2770 if (hwgroup->hwif != hwif) {
2771 char *name = hwgroup->hwif->name;
2772 if (hwgroup->hwif->irq == hwif->irq)
2773 printk(" (shared with %s)", name);
2774 else
2775 printk(" (serialized with %s)", name);
2776 }
2777 printk("\n");
2778 return 0;
2779 }
2780
2781 static struct file_operations ide_fops = {
2782 NULL, /* lseek - default */
2783 block_read, /* read - general block-dev read */
2784 block_write, /* write - general block-dev write */
2785 NULL, /* readdir - bad */
2786 NULL, /* select */
2787 ide_ioctl, /* ioctl */
2788 NULL, /* mmap */
2789 ide_open, /* open */
2790 ide_release, /* release */
2791 block_fsync /* fsync */
2792 ,NULL, /* fasync */
2793 ide_check_media_change, /* check_media_change */
2794 revalidate_disk /* revalidate */
2795 };
2796
2797 #ifdef CONFIG_PCI
2798
2799 void ide_pci_access_error (int rc)
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
2800 {
2801 printk("ide: pcibios access failed - %s\n", pcibios_strerror(rc));
2802 }
2803
2804 #if SUPPORT_RZ1000 || SUPPORT_CMD640
2805 void buggy_interface_fallback (int rc)
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
2806 {
2807 ide_pci_access_error (rc);
2808 single_threaded = 1;
2809 disallow_unmask = 1;
2810 printk("serialized, disabled unmasking\n");
2811 }
2812 #endif /* SUPPORT_RZ1000 || SUPPORT_CMD640 */
2813
2814 #if SUPPORT_RZ1000
2815 void init_rz1000 (byte bus, byte fn)
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
2816 {
2817 int rc;
2818 unsigned short reg;
2819
2820 printk("ide: buggy RZ1000 interface: ");
2821 if ((rc = pcibios_read_config_word(bus, fn, 0x40, ®))
2822 || (rc = pcibios_write_config_word(bus, fn, 0x40, reg & 0xdfff)))
2823 buggy_interface_fallback (rc);
2824 else
2825 printk("disabled read-ahead\n");
2826 }
2827 #endif /* SUPPORT_RZ1000 */
2828
2829 #if SUPPORT_CMD640
2830 void init_cmd640 (byte bus, byte fn)
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
2831 {
2832 int rc;
2833 unsigned char reg;
2834
2835 single_threaded = 1;
2836 printk("ide: buggy CMD640 interface: ");
2837 if ((rc = pcibios_read_config_byte(bus, fn, 0x51, ®))
2838 || (rc = pcibios_write_config_byte(bus, fn, 0x51, reg | 0xc8)))
2839 buggy_interface_fallback (rc);
2840 else
2841 printk("serialized, disabled read-ahead, enabled secondary\n");
2842 }
2843 #endif /* SUPPORT_CMD640 */
2844
2845 typedef void (ide_pci_init_proc_t)(byte, byte);
2846
2847 /*
2848 * ide_probe_pci() scans PCI for a specific vendor/device function,
2849 * and invokes the supplied init routine for each instance detected.
2850 */
2851 static void ide_probe_pci (unsigned short vendor, unsigned short device, ide_pci_init_proc_t *init)
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
2852 {
2853 unsigned long flags;
2854 unsigned index = 0;
2855 byte fn, bus;
2856 int rc;
2857
2858 save_flags(flags);
2859 cli();
2860 for (index = 0; !pcibios_find_device (vendor, device, index, &bus, &fn); ++index) {
2861 unsigned short command;
2862 if ((rc = pcibios_read_config_word (bus, fn, PCI_COMMAND, &command))) {
2863 ide_pci_access_error (rc);
2864 } else if (command & 1) { /* is device enabled? */
2865 init (bus, fn);
2866 }
2867 }
2868 restore_flags(flags);
2869 }
2870
2871 /*
2872 * ide_init_pci() finds/initializes "known" PCI IDE interfaces
2873 *
2874 * This routine should ideally be using pcibios_find_class() to find
2875 * all IDE interfaces, but that function causes some systems to "go weird".
2876 */
2877 static void ide_init_pci (void)
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
2878 {
2879 #if SUPPORT_RZ1000
2880 ide_probe_pci (PCI_VENDOR_ID_PCTECH, PCI_DEVICE_ID_PCTECH_RZ1000, &init_rz1000);
2881 #endif
2882 #if SUPPORT_CMD640
2883 ide_probe_pci (PCI_VENDOR_ID_CMD, PCI_DEVICE_ID_CMD_640, &init_cmd640);
2884 #endif
2885 #ifdef CONFIG_BLK_DEV_TRITON
2886 ide_probe_pci (PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82371, &ide_init_triton);
2887 #endif
2888 }
2889 #endif /* CONFIG_PCI */
2890
2891 /*
2892 * This is gets invoked once during initialization, to set *everything* up
2893 */
2894 unsigned long ide_init (unsigned long mem_start, unsigned long mem_end)
/* ![[previous]](../icons/left.png)
![[next]](../icons/n_right.png)
![[first]](../icons/first.png)
![[last]](../icons/n_last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
2895 {
2896 int h;
2897
2898 ide_mem_start = mem_start; /* for ide_alloc () */
2899 init_ide_data ();
2900 /*
2901 * First, we determine what hardware is present
2902 */
2903
2904 #ifdef CONFIG_PCI
2905 /*
2906 * Find/initialize PCI IDE interfaces
2907 */
2908 if (pcibios_present())
2909 ide_init_pci ();
2910 #endif /* CONFIG_PCI */
2911
2912 /*
2913 * Probe for drives in the usual way.. CMOS/BIOS, then poke at ports
2914 */
2915 for (h = 0; h < MAX_HWIFS; ++h) {
2916 ide_hwif_t *hwif = &ide_hwifs[h];
2917 if (!hwif->noprobe) {
2918 if (hwif->io_base == HD_DATA)
2919 probe_cmos_for_drives (hwif);
2920 probe_for_drives (hwif);
2921 }
2922 if (hwif->present) {
2923 if (!hwif->irq) {
2924 if (!(hwif->irq = default_irqs[h])) {
2925 printk("%s: DISABLED, NO IRQ\n", hwif->name);
2926 hwif->present = 0;
2927 continue;
2928 }
2929 }
2930 #ifdef CONFIG_BLK_DEV_HD
2931 if (hwif->irq == HD_IRQ && hwif->io_base != HD_DATA) {
2932 printk("%s: CANNOT SHARE IRQ WITH OLD HARDDISK DRIVER (hd.c)\n", hwif->name);
2933 hwif->present = 0;
2934 }
2935 #endif /* CONFIG_BLK_DEV_HD */
2936 }
2937 }
2938
2939 /*
2940 * Now we try to set up irqs and major devices for what was found
2941 */
2942 for (h = MAX_HWIFS-1; h >= 0; --h) {
2943 void (*rfn)(void);
2944 ide_hwif_t *hwif = &ide_hwifs[h];
2945 if (!hwif->present)
2946 continue;
2947 hwif->present = 0; /* we set it back to 1 if all is ok below */
2948 if (h == 0 && single_threaded) {
2949 if (ide_hwifs[1].present) {
2950 if (irq_to_hwgroup[hwif->irq] != NULL) {
2951 printk("%s: SERIALIZE BUG!\n", hwif->name);
2952 continue;
2953 }
2954 irq_to_hwgroup[hwif->irq] = irq_to_hwgroup[ide_hwifs[1].irq];
2955 }
2956 }
2957 switch (hwif->major) {
2958 case IDE0_MAJOR: rfn = &do_ide0_request; break;
2959 case IDE1_MAJOR: rfn = &do_ide1_request; break;
2960 case IDE2_MAJOR: rfn = &do_ide2_request; break;
2961 case IDE3_MAJOR: rfn = &do_ide3_request; break;
2962 default:
2963 printk("%s: request_fn NOT DEFINED\n", hwif->name);
2964 continue;
2965 }
2966 if (register_blkdev (hwif->major, hwif->name, &ide_fops)) {
2967 printk("%s: UNABLE TO GET MAJOR NUMBER %d\n", hwif->name, hwif->major);
2968 } else if (init_irq (hwif)) {
2969 printk("%s: UNABLE TO GET IRQ %d\n", hwif->name, hwif->irq);
2970 (void) unregister_blkdev (hwif->major, hwif->name);
2971 } else {
2972 init_gendisk(hwif);
2973 blk_dev[hwif->major].request_fn = rfn;
2974 read_ahead[hwif->major] = 8; /* (4kB) */
2975 hwif->present = 1; /* success */
2976 }
2977 }
2978 mem_start = ide_mem_start;
2979 ide_mem_start = 0uL; /* prevent further use of ide_alloc() */
2980 return mem_start;
2981 }