* added 0.99 linux version
[mascara-docs.git] / i386 / linux / linux-2.3.21 / drivers / block / ide-disk.c
blobd2941a1ab7a8cea13ba21161eed934ec0897f03c
1 /*
2 * linux/drivers/block/ide-disk.c Version 1.09 April 23, 1999
4 * Copyright (C) 1994-1998 Linus Torvalds & authors (see below)
5 */
7 /*
8 * Mostly written by Mark Lord <mlord@pobox.com>
9 * and Gadi Oxman <gadio@netvision.net.il>
11 * See linux/MAINTAINERS for address of current maintainer.
13 * This is the IDE/ATA disk driver, as evolved from hd.c and ide.c.
15 * Version 1.00 move disk only code from ide.c to ide-disk.c
16 * support optional byte-swapping of all data
17 * Version 1.01 fix previous byte-swapping code
18 * Version 1.02 remove ", LBA" from drive identification msgs
19 * Version 1.03 fix display of id->buf_size for big-endian
20 * Version 1.04 add /proc configurable settings and S.M.A.R.T support
21 * Version 1.05 add capacity support for ATA3 >= 8GB
22 * Version 1.06 get boot-up messages to show full cyl count
23 * Version 1.07 disable door-locking if it fails
24 * Version 1.08 fixed CHS/LBA translations for ATA4 > 8GB,
25 * process of adding new ATA4 compliance.
26 * fixed problems in allowing fdisk to see
27 * the entire disk.
28 * Version 1.09 added increment of rq->sector in ide_multwrite
29 * added UDMA 3/4 reporting
32 #define IDEDISK_VERSION "1.09"
34 #undef REALLY_SLOW_IO /* most systems can safely undef this */
36 #define _IDE_DISK_C /* Tell linux/hdsmart.h it's really us */
38 #include <linux/config.h>
39 #include <linux/module.h>
40 #include <linux/types.h>
41 #include <linux/string.h>
42 #include <linux/kernel.h>
43 #include <linux/timer.h>
44 #include <linux/mm.h>
45 #include <linux/interrupt.h>
46 #include <linux/major.h>
47 #include <linux/errno.h>
48 #include <linux/genhd.h>
49 #include <linux/malloc.h>
50 #include <linux/delay.h>
51 #include <linux/ide.h>
53 #include <asm/byteorder.h>
54 #include <asm/irq.h>
55 #include <asm/uaccess.h>
56 #include <asm/io.h>
58 #ifdef CONFIG_BLK_DEV_PDC4030
59 #define IS_PDC4030_DRIVE (HWIF(drive)->chipset == ide_pdc4030)
60 #else
61 #define IS_PDC4030_DRIVE (0) /* auto-NULLs out pdc4030 code */
62 #endif
64 static void idedisk_bswap_data (void *buffer, int wcount)
66 u16 *p = buffer;
68 while (wcount--) {
69 *p++ = *p << 8 | *p >> 8;
70 *p++ = *p << 8 | *p >> 8;
74 static inline void idedisk_input_data (ide_drive_t *drive, void *buffer, unsigned int wcount)
76 ide_input_data(drive, buffer, wcount);
77 if (drive->bswap)
78 idedisk_bswap_data(buffer, wcount);
81 static inline void idedisk_output_data (ide_drive_t *drive, void *buffer, unsigned int wcount)
83 if (drive->bswap) {
84 idedisk_bswap_data(buffer, wcount);
85 ide_output_data(drive, buffer, wcount);
86 idedisk_bswap_data(buffer, wcount);
87 } else
88 ide_output_data(drive, buffer, wcount);
92 * lba_capacity_is_ok() performs a sanity check on the claimed "lba_capacity"
93 * value for this drive (from its reported identification information).
95 * Returns: 1 if lba_capacity looks sensible
96 * 0 otherwise
98 * It is called only once for each drive.
100 static int lba_capacity_is_ok (struct hd_driveid *id)
102 unsigned long lba_sects, chs_sects, head, tail;
105 * The ATA spec tells large drives to return
106 * C/H/S = 16383/16/63 independent of their size.
107 * Some drives can be jumpered to use 15 heads instead of 16.
109 if (id->cyls == 16383 && id->sectors == 63 &&
110 (id->heads == 15 || id->heads == 16) &&
111 id->lba_capacity >= 16383*63*id->heads)
112 return 1;
114 lba_sects = id->lba_capacity;
115 chs_sects = id->cyls * id->heads * id->sectors;
117 /* perform a rough sanity check on lba_sects: within 10% is OK */
118 if ((lba_sects - chs_sects) < chs_sects/10)
119 return 1;
121 /* some drives have the word order reversed */
122 head = ((lba_sects >> 16) & 0xffff);
123 tail = (lba_sects & 0xffff);
124 lba_sects = (head | (tail << 16));
125 if ((lba_sects - chs_sects) < chs_sects/10) {
126 id->lba_capacity = lba_sects;
127 return 1; /* lba_capacity is (now) good */
130 return 0; /* lba_capacity value may be bad */
134 * read_intr() is the handler for disk read/multread interrupts
136 static void read_intr (ide_drive_t *drive)
138 byte stat;
139 int i;
140 unsigned int msect, nsect;
141 struct request *rq;
143 if (!OK_STAT(stat=GET_STAT(),DATA_READY,BAD_R_STAT)) {
144 ide_error(drive, "read_intr", stat);
145 return;
147 msect = drive->mult_count;
149 read_next:
150 rq = HWGROUP(drive)->rq;
151 if (msect) {
152 if ((nsect = rq->current_nr_sectors) > msect)
153 nsect = msect;
154 msect -= nsect;
155 } else
156 nsect = 1;
158 * PIO input can take longish times, so we drop the spinlock.
159 * On SMP, bad things might happen if syscall level code adds
160 * a new request while we do this PIO, so we just freeze all
161 * request queue handling while doing the PIO. FIXME
163 idedisk_input_data(drive, rq->buffer, nsect * SECTOR_WORDS);
164 #ifdef DEBUG
165 printk("%s: read: sectors(%ld-%ld), buffer=0x%08lx, remaining=%ld\n",
166 drive->name, rq->sector, rq->sector+nsect-1,
167 (unsigned long) rq->buffer+(nsect<<9), rq->nr_sectors-nsect);
168 #endif
169 rq->sector += nsect;
170 rq->buffer += nsect<<9;
171 rq->errors = 0;
172 i = (rq->nr_sectors -= nsect);
173 if ((rq->current_nr_sectors -= nsect) <= 0)
174 ide_end_request(1, HWGROUP(drive));
175 if (i > 0) {
176 if (msect)
177 goto read_next;
178 ide_set_handler (drive, &read_intr, WAIT_CMD);
183 * write_intr() is the handler for disk write interrupts
185 static void write_intr (ide_drive_t *drive)
187 byte stat;
188 int i;
189 ide_hwgroup_t *hwgroup = HWGROUP(drive);
190 struct request *rq = hwgroup->rq;
191 int error = 0;
193 if (OK_STAT(stat=GET_STAT(),DRIVE_READY,drive->bad_wstat)) {
194 #ifdef DEBUG
195 printk("%s: write: sector %ld, buffer=0x%08lx, remaining=%ld\n",
196 drive->name, rq->sector, (unsigned long) rq->buffer,
197 rq->nr_sectors-1);
198 #endif
199 if ((rq->nr_sectors == 1) ^ ((stat & DRQ_STAT) != 0)) {
200 rq->sector++;
201 rq->buffer += 512;
202 rq->errors = 0;
203 i = --rq->nr_sectors;
204 --rq->current_nr_sectors;
205 if (rq->current_nr_sectors <= 0)
206 ide_end_request(1, hwgroup);
207 if (i > 0) {
208 idedisk_output_data (drive, rq->buffer, SECTOR_WORDS);
209 ide_set_handler (drive, &write_intr, WAIT_CMD);
211 goto out;
213 } else
214 error = 1;
215 out:
216 if (error)
217 ide_error(drive, "write_intr", stat);
221 * ide_multwrite() transfers a block of up to mcount sectors of data
222 * to a drive as part of a disk multiple-sector write operation.
224 void ide_multwrite (ide_drive_t *drive, unsigned int mcount)
226 struct request *rq = &HWGROUP(drive)->wrq;
228 do {
229 unsigned int nsect = rq->current_nr_sectors;
230 if (nsect > mcount)
231 nsect = mcount;
232 mcount -= nsect;
234 idedisk_output_data(drive, rq->buffer, nsect<<7);
235 #ifdef DEBUG
236 printk("%s: multwrite: sector %ld, buffer=0x%08lx, count=%d, remaining=%ld\n",
237 drive->name, rq->sector, (unsigned long) rq->buffer,
238 nsect, rq->nr_sectors - nsect);
239 #endif
240 #ifdef CONFIG_BLK_DEV_PDC4030
241 rq->sector += nsect;
242 #endif
243 if ((rq->nr_sectors -= nsect) <= 0)
244 break;
245 if ((rq->current_nr_sectors -= nsect) == 0) {
246 if ((rq->bh = rq->bh->b_reqnext) != NULL) {
247 rq->current_nr_sectors = rq->bh->b_size>>9;
248 rq->buffer = rq->bh->b_data;
249 } else {
250 panic("%s: buffer list corrupted\n", drive->name);
251 break;
253 } else {
254 rq->buffer += nsect << 9;
256 } while (mcount);
260 * multwrite_intr() is the handler for disk multwrite interrupts
262 static void multwrite_intr (ide_drive_t *drive)
264 byte stat;
265 int i;
266 ide_hwgroup_t *hwgroup = HWGROUP(drive);
267 struct request *rq = &hwgroup->wrq;
268 int error = 0;
270 if (OK_STAT(stat=GET_STAT(),DRIVE_READY,drive->bad_wstat)) {
271 if (stat & DRQ_STAT) {
272 if (rq->nr_sectors) {
273 ide_multwrite(drive, drive->mult_count);
274 ide_set_handler (drive, &multwrite_intr, WAIT_CMD);
275 goto out;
277 } else {
278 if (!rq->nr_sectors) { /* all done? */
279 rq = hwgroup->rq;
280 for (i = rq->nr_sectors; i > 0;){
281 i -= rq->current_nr_sectors;
282 ide_end_request(1, hwgroup);
284 goto out;
287 } else
288 error = 1;
289 out:
290 if (error)
291 ide_error(drive, "multwrite_intr", stat);
295 * set_multmode_intr() is invoked on completion of a WIN_SETMULT cmd.
297 static void set_multmode_intr (ide_drive_t *drive)
299 byte stat = GET_STAT();
301 #if 0
302 if (OK_STAT(stat,READY_STAT,BAD_STAT) || drive->mult_req == 0) {
303 #else
304 if (OK_STAT(stat,READY_STAT,BAD_STAT)) {
305 #endif
306 drive->mult_count = drive->mult_req;
307 } else {
308 drive->mult_req = drive->mult_count = 0;
309 drive->special.b.recalibrate = 1;
310 (void) ide_dump_status(drive, "set_multmode", stat);
315 * set_geometry_intr() is invoked on completion of a WIN_SPECIFY cmd.
317 static void set_geometry_intr (ide_drive_t *drive)
319 byte stat = GET_STAT();
321 if (!OK_STAT(stat,READY_STAT,BAD_STAT))
322 ide_error(drive, "set_geometry_intr", stat);
326 * recal_intr() is invoked on completion of a WIN_RESTORE (recalibrate) cmd.
328 static void recal_intr (ide_drive_t *drive)
330 byte stat = GET_STAT();
332 if (!OK_STAT(stat,READY_STAT,BAD_STAT))
333 ide_error(drive, "recal_intr", stat);
337 * do_rw_disk() issues READ and WRITE commands to a disk,
338 * using LBA if supported, or CHS otherwise, to address sectors.
339 * It also takes care of issuing special DRIVE_CMDs.
341 static void do_rw_disk (ide_drive_t *drive, struct request *rq, unsigned long block)
343 if (IDE_CONTROL_REG)
344 OUT_BYTE(drive->ctl,IDE_CONTROL_REG);
345 OUT_BYTE(rq->nr_sectors,IDE_NSECTOR_REG);
346 #ifdef CONFIG_BLK_DEV_PDC4030
347 if (drive->select.b.lba || IS_PDC4030_DRIVE) {
348 #else /* !CONFIG_BLK_DEV_PDC4030 */
349 if (drive->select.b.lba) {
350 #endif /* CONFIG_BLK_DEV_PDC4030 */
351 #ifdef DEBUG
352 printk("%s: %sing: LBAsect=%ld, sectors=%ld, buffer=0x%08lx\n",
353 drive->name, (rq->cmd==READ)?"read":"writ",
354 block, rq->nr_sectors, (unsigned long) rq->buffer);
355 #endif
356 OUT_BYTE(block,IDE_SECTOR_REG);
357 OUT_BYTE(block>>=8,IDE_LCYL_REG);
358 OUT_BYTE(block>>=8,IDE_HCYL_REG);
359 OUT_BYTE(((block>>8)&0x0f)|drive->select.all,IDE_SELECT_REG);
360 } else {
361 unsigned int sect,head,cyl,track;
362 track = block / drive->sect;
363 sect = block % drive->sect + 1;
364 OUT_BYTE(sect,IDE_SECTOR_REG);
365 head = track % drive->head;
366 cyl = track / drive->head;
367 OUT_BYTE(cyl,IDE_LCYL_REG);
368 OUT_BYTE(cyl>>8,IDE_HCYL_REG);
369 OUT_BYTE(head|drive->select.all,IDE_SELECT_REG);
370 #ifdef DEBUG
371 printk("%s: %sing: CHS=%d/%d/%d, sectors=%ld, buffer=0x%08lx\n",
372 drive->name, (rq->cmd==READ)?"read":"writ", cyl,
373 head, sect, rq->nr_sectors, (unsigned long) rq->buffer);
374 #endif
376 #ifdef CONFIG_BLK_DEV_PDC4030
377 if (IS_PDC4030_DRIVE) {
378 extern void do_pdc4030_io(ide_drive_t *, struct request *);
379 do_pdc4030_io (drive, rq);
380 return;
382 #endif /* CONFIG_BLK_DEV_PDC4030 */
383 if (rq->cmd == READ) {
384 #ifdef CONFIG_BLK_DEV_IDEDMA
385 if (drive->using_dma && !(HWIF(drive)->dmaproc(ide_dma_read, drive)))
386 return;
387 #endif /* CONFIG_BLK_DEV_IDEDMA */
388 ide_set_handler(drive, &read_intr, WAIT_CMD);
389 OUT_BYTE(drive->mult_count ? WIN_MULTREAD : WIN_READ, IDE_COMMAND_REG);
390 return;
392 if (rq->cmd == WRITE) {
393 #ifdef CONFIG_BLK_DEV_IDEDMA
394 if (drive->using_dma && !(HWIF(drive)->dmaproc(ide_dma_write, drive)))
395 return;
396 #endif /* CONFIG_BLK_DEV_IDEDMA */
397 OUT_BYTE(drive->mult_count ? WIN_MULTWRITE : WIN_WRITE, IDE_COMMAND_REG);
398 if (ide_wait_stat(drive, DATA_READY, drive->bad_wstat, WAIT_DRQ)) {
399 printk(KERN_ERR "%s: no DRQ after issuing %s\n", drive->name,
400 drive->mult_count ? "MULTWRITE" : "WRITE");
401 return;
403 if (!drive->unmask)
404 __cli(); /* local CPU only */
405 if (drive->mult_count) {
406 HWGROUP(drive)->wrq = *rq; /* scratchpad */
407 ide_set_handler (drive, &multwrite_intr, WAIT_CMD);
408 ide_multwrite(drive, drive->mult_count);
409 } else {
410 ide_set_handler (drive, &write_intr, WAIT_CMD);
411 idedisk_output_data(drive, rq->buffer, SECTOR_WORDS);
413 return;
415 printk(KERN_ERR "%s: bad command: %d\n", drive->name, rq->cmd);
416 ide_end_request(0, HWGROUP(drive));
419 static int idedisk_open (struct inode *inode, struct file *filp, ide_drive_t *drive)
421 MOD_INC_USE_COUNT;
422 if (drive->removable && drive->usage == 1) {
423 check_disk_change(inode->i_rdev);
425 * Ignore the return code from door_lock,
426 * since the open() has already succeeded,
427 * and the door_lock is irrelevant at this point.
429 if (drive->doorlocking && ide_wait_cmd(drive, WIN_DOORLOCK, 0, 0, 0, NULL))
430 drive->doorlocking = 0;
432 return 0;
435 static void idedisk_release (struct inode *inode, struct file *filp, ide_drive_t *drive)
437 if (drive->removable && !drive->usage) {
438 invalidate_buffers(inode->i_rdev);
439 if (drive->doorlocking && ide_wait_cmd(drive, WIN_DOORUNLOCK, 0, 0, 0, NULL))
440 drive->doorlocking = 0;
442 MOD_DEC_USE_COUNT;
445 static int idedisk_media_change (ide_drive_t *drive)
447 return drive->removable; /* if removable, always assume it was changed */
451 * Compute drive->capacity, the full capacity of the drive
452 * Called with drive->id != NULL.
454 static void init_idedisk_capacity (ide_drive_t *drive)
456 struct hd_driveid *id = drive->id;
457 unsigned long capacity = drive->cyl * drive->head * drive->sect;
459 drive->select.b.lba = 0;
461 /* Determine capacity, and use LBA if the drive properly supports it */
462 if ((id->capability & 2) && lba_capacity_is_ok(id)) {
463 capacity = id->lba_capacity;
464 drive->cyl = capacity / (drive->head * drive->sect);
465 drive->select.b.lba = 1;
467 drive->capacity = capacity;
470 static unsigned long idedisk_capacity (ide_drive_t *drive)
472 return (drive->capacity - drive->sect0);
475 static void idedisk_special (ide_drive_t *drive)
477 special_t *s = &drive->special;
479 if (s->b.set_geometry) {
480 s->b.set_geometry = 0;
481 OUT_BYTE(drive->sect,IDE_SECTOR_REG);
482 OUT_BYTE(drive->cyl,IDE_LCYL_REG);
483 OUT_BYTE(drive->cyl>>8,IDE_HCYL_REG);
484 OUT_BYTE(((drive->head-1)|drive->select.all)&0xBF,IDE_SELECT_REG);
485 if (!IS_PDC4030_DRIVE)
486 ide_cmd(drive, WIN_SPECIFY, drive->sect, &set_geometry_intr);
487 } else if (s->b.recalibrate) {
488 s->b.recalibrate = 0;
489 if (!IS_PDC4030_DRIVE)
490 ide_cmd(drive, WIN_RESTORE, drive->sect, &recal_intr);
491 } else if (s->b.set_multmode) {
492 s->b.set_multmode = 0;
493 if (drive->id && drive->mult_req > drive->id->max_multsect)
494 drive->mult_req = drive->id->max_multsect;
495 if (!IS_PDC4030_DRIVE)
496 ide_cmd(drive, WIN_SETMULT, drive->mult_req, &set_multmode_intr);
497 } else if (s->all) {
498 int special = s->all;
499 s->all = 0;
500 printk(KERN_ERR "%s: bad special flag: 0x%02x\n", drive->name, special);
504 static void idedisk_pre_reset (ide_drive_t *drive)
506 drive->special.all = 0;
507 drive->special.b.set_geometry = 1;
508 drive->special.b.recalibrate = 1;
509 if (OK_TO_RESET_CONTROLLER)
510 drive->mult_count = 0;
511 if (!drive->keep_settings)
512 drive->mult_req = 0;
513 if (drive->mult_req != drive->mult_count)
514 drive->special.b.set_multmode = 1;
517 #ifdef CONFIG_PROC_FS
519 static int smart_enable(ide_drive_t *drive)
521 return ide_wait_cmd(drive, WIN_SMART, 0, SMART_ENABLE, 0, NULL);
524 static int get_smart_values(ide_drive_t *drive, byte *buf)
526 (void) smart_enable(drive);
527 return ide_wait_cmd(drive, WIN_SMART, 0, SMART_READ_VALUES, 1, buf);
530 static int get_smart_thresholds(ide_drive_t *drive, byte *buf)
532 (void) smart_enable(drive);
533 return ide_wait_cmd(drive, WIN_SMART, 0, SMART_READ_THRESHOLDS, 1, buf);
536 static int proc_idedisk_read_cache
537 (char *page, char **start, off_t off, int count, int *eof, void *data)
539 ide_drive_t *drive = (ide_drive_t *) data;
540 char *out = page;
541 int len;
543 if (drive->id)
544 len = sprintf(out,"%i\n", drive->id->buf_size / 2);
545 else
546 len = sprintf(out,"(none)\n");
547 PROC_IDE_READ_RETURN(page,start,off,count,eof,len);
550 static int proc_idedisk_read_smart_thresholds
551 (char *page, char **start, off_t off, int count, int *eof, void *data)
553 ide_drive_t *drive = (ide_drive_t *)data;
554 int len = 0, i = 0;
556 if (!get_smart_thresholds(drive, page)) {
557 unsigned short *val = ((unsigned short *)page) + 2;
558 char *out = ((char *)val) + (SECTOR_WORDS * 4);
559 page = out;
560 do {
561 out += sprintf(out, "%04x%c", le16_to_cpu(*val), (++i & 7) ? ' ' : '\n');
562 val += 1;
563 } while (i < (SECTOR_WORDS * 2));
564 len = out - page;
566 PROC_IDE_READ_RETURN(page,start,off,count,eof,len);
569 static int proc_idedisk_read_smart_values
570 (char *page, char **start, off_t off, int count, int *eof, void *data)
572 ide_drive_t *drive = (ide_drive_t *)data;
573 int len = 0, i = 0;
575 if (!get_smart_values(drive, page)) {
576 unsigned short *val = ((unsigned short *)page) + 2;
577 char *out = ((char *)val) + (SECTOR_WORDS * 4);
578 page = out;
579 do {
580 out += sprintf(out, "%04x%c", le16_to_cpu(*val), (++i & 7) ? ' ' : '\n');
581 val += 1;
582 } while (i < (SECTOR_WORDS * 2));
583 len = out - page;
585 PROC_IDE_READ_RETURN(page,start,off,count,eof,len);
588 static ide_proc_entry_t idedisk_proc[] = {
589 { "cache", S_IFREG|S_IRUGO, proc_idedisk_read_cache, NULL },
590 { "geometry", S_IFREG|S_IRUGO, proc_ide_read_geometry, NULL },
591 { "smart_values", S_IFREG|S_IRUSR, proc_idedisk_read_smart_values, NULL },
592 { "smart_thresholds", S_IFREG|S_IRUSR, proc_idedisk_read_smart_thresholds, NULL },
593 { NULL, 0, NULL, NULL }
596 #else
598 #define idedisk_proc NULL
600 #endif /* CONFIG_PROC_FS */
602 static int set_multcount(ide_drive_t *drive, int arg)
604 struct request rq;
606 if (drive->special.b.set_multmode)
607 return -EBUSY;
608 ide_init_drive_cmd (&rq);
609 drive->mult_req = arg;
610 drive->special.b.set_multmode = 1;
611 (void) ide_do_drive_cmd (drive, &rq, ide_wait);
612 return (drive->mult_count == arg) ? 0 : -EIO;
615 static int set_nowerr(ide_drive_t *drive, int arg)
617 unsigned long flags;
619 if (ide_spin_wait_hwgroup(drive, &flags))
620 return -EBUSY;
621 drive->nowerr = arg;
622 drive->bad_wstat = arg ? BAD_R_STAT : BAD_W_STAT;
623 spin_unlock_irqrestore(&HWGROUP(drive)->spinlock, flags);
624 return 0;
627 static void idedisk_add_settings(ide_drive_t *drive)
629 struct hd_driveid *id = drive->id;
630 int major = HWIF(drive)->major;
631 int minor = drive->select.b.unit << PARTN_BITS;
633 ide_add_setting(drive, "bios_cyl", SETTING_RW, -1, -1, TYPE_INT, 0, 65535, 1, 1, &drive->bios_cyl, NULL);
634 ide_add_setting(drive, "bios_head", SETTING_RW, -1, -1, TYPE_BYTE, 0, 255, 1, 1, &drive->bios_head, NULL);
635 ide_add_setting(drive, "bios_sect", SETTING_RW, -1, -1, TYPE_BYTE, 0, 63, 1, 1, &drive->bios_sect, NULL);
636 ide_add_setting(drive, "bswap", SETTING_READ, -1, -1, TYPE_BYTE, 0, 1, 1, 1, &drive->bswap, NULL);
637 ide_add_setting(drive, "multcount", id ? SETTING_RW : SETTING_READ, HDIO_GET_MULTCOUNT, HDIO_SET_MULTCOUNT, TYPE_BYTE, 0, id ? id->max_multsect : 0, 1, 2, &drive->mult_count, set_multcount);
638 ide_add_setting(drive, "nowerr", SETTING_RW, HDIO_GET_NOWERR, HDIO_SET_NOWERR, TYPE_BYTE, 0, 1, 1, 1, &drive->nowerr, set_nowerr);
639 ide_add_setting(drive, "breada_readahead", SETTING_RW, BLKRAGET, BLKRASET, TYPE_INT, 0, 255, 1, 2, &read_ahead[major], NULL);
640 ide_add_setting(drive, "file_readahead", SETTING_RW, BLKFRAGET, BLKFRASET, TYPE_INTA, 0, INT_MAX, 1, 1024, &max_readahead[major][minor], NULL);
641 ide_add_setting(drive, "max_kb_per_request", SETTING_RW, BLKSECTGET, BLKSECTSET, TYPE_INTA, 1, 255, 1, 2, &max_sectors[major][minor], NULL);
646 * IDE subdriver functions, registered with ide.c
648 static ide_driver_t idedisk_driver = {
649 "ide-disk", /* name */
650 IDEDISK_VERSION, /* version */
651 ide_disk, /* media */
652 0, /* busy */
653 1, /* supports_dma */
654 0, /* supports_dsc_overlap */
655 NULL, /* cleanup */
656 do_rw_disk, /* do_request */
657 NULL, /* end_request */
658 NULL, /* ioctl */
659 idedisk_open, /* open */
660 idedisk_release, /* release */
661 idedisk_media_change, /* media_change */
662 idedisk_pre_reset, /* pre_reset */
663 idedisk_capacity, /* capacity */
664 idedisk_special, /* special */
665 idedisk_proc /* proc */
668 int idedisk_init (void);
669 static ide_module_t idedisk_module = {
670 IDE_DRIVER_MODULE,
671 idedisk_init,
672 &idedisk_driver,
673 NULL
676 static int idedisk_cleanup (ide_drive_t *drive)
678 return ide_unregister_subdriver(drive);
681 static void idedisk_setup (ide_drive_t *drive)
683 struct hd_driveid *id = drive->id;
684 unsigned long capacity;
686 idedisk_add_settings(drive);
688 if (id == NULL)
689 return;
692 * CompactFlash cards and their brethern look just like hard drives
693 * to us, but they are removable and don't have a doorlock mechanism.
695 if (drive->removable && !drive_is_flashcard(drive)) {
697 * Removable disks (eg. SYQUEST); ignore 'WD' drives
699 if (id->model[0] != 'W' || id->model[1] != 'D') {
700 drive->doorlocking = 1;
704 /* Extract geometry if we did not already have one for the drive */
705 if (!drive->cyl || !drive->head || !drive->sect) {
706 drive->cyl = drive->bios_cyl = id->cyls;
707 drive->head = drive->bios_head = id->heads;
708 drive->sect = drive->bios_sect = id->sectors;
711 /* Handle logical geometry translation by the drive */
712 if ((id->field_valid & 1) && id->cur_cyls &&
713 id->cur_heads && (id->cur_heads <= 16) && id->cur_sectors) {
714 drive->cyl = id->cur_cyls;
715 drive->head = id->cur_heads;
716 drive->sect = id->cur_sectors;
719 /* Use physical geometry if what we have still makes no sense */
720 if (drive->head > 16 && id->heads && id->heads <= 16) {
721 drive->cyl = id->cyls;
722 drive->head = id->heads;
723 drive->sect = id->sectors;
726 /* calculate drive capacity, and select LBA if possible */
727 init_idedisk_capacity (drive);
730 * if possible, give fdisk access to more of the drive,
731 * by correcting bios_cyls:
733 capacity = idedisk_capacity (drive);
734 if ((capacity >= (drive->bios_cyl * drive->bios_sect * drive->bios_head)) &&
735 (!drive->forced_geom) && drive->bios_sect && drive->bios_head)
736 drive->bios_cyl = (capacity / drive->bios_sect) / drive->bios_head;
738 #if 0 /* done instead for entire identify block in arch/ide.h stuff */
739 /* fix byte-ordering of buffer size field */
740 id->buf_size = le16_to_cpu(id->buf_size);
741 #endif
742 printk (KERN_INFO "%s: %.40s, %ldMB w/%dkB Cache, CHS=%d/%d/%d",
743 drive->name, id->model,
744 capacity/2048L, id->buf_size/2,
745 drive->bios_cyl, drive->bios_head, drive->bios_sect);
747 if (drive->using_dma) {
748 if ((id->field_valid & 4) && (id->word93 & 0x2000) &&
749 (HWIF(drive)->udma_four) &&
750 (id->dma_ultra & (id->dma_ultra >> 11) & 3)) {
751 printk(", UDMA(66)"); /* UDMA BIOS-enabled! */
752 } else if ((id->field_valid & 4) &&
753 (id->dma_ultra & (id->dma_ultra >> 8) & 7)) {
754 printk(", UDMA(33)"); /* UDMA BIOS-enabled! */
755 } else if (id->field_valid & 4) {
756 printk(", (U)DMA"); /* Can be BIOS-enabled! */
757 } else {
758 printk(", DMA");
761 printk("\n");
763 drive->mult_count = 0;
764 if (id->max_multsect) {
765 #ifdef CONFIG_IDEDISK_MULTI_MODE
766 id->multsect = ((id->max_multsect/2) > 1) ? id->max_multsect : 0;
767 id->multsect_valid = id->multsect ? 1 : 0;
768 drive->mult_req = id->multsect_valid ? id->max_multsect : INITIAL_MULT_COUNT;
769 drive->special.b.set_multmode = drive->mult_req ? 1 : 0;
770 #else /* original, pre IDE-NFG, per request of AC */
771 drive->mult_req = INITIAL_MULT_COUNT;
772 if (drive->mult_req > id->max_multsect)
773 drive->mult_req = id->max_multsect;
774 if (drive->mult_req || ((id->multsect_valid & 1) && id->multsect))
775 drive->special.b.set_multmode = 1;
776 #endif
778 drive->no_io_32bit = id->dword_io ? 1 : 0;
781 int idedisk_init (void)
783 ide_drive_t *drive;
784 int failed = 0;
786 MOD_INC_USE_COUNT;
787 while ((drive = ide_scan_devices (ide_disk, idedisk_driver.name, NULL, failed++)) != NULL) {
788 if (ide_register_subdriver (drive, &idedisk_driver, IDE_SUBDRIVER_VERSION)) {
789 printk (KERN_ERR "ide-disk: %s: Failed to register the driver with ide.c\n", drive->name);
790 continue;
792 idedisk_setup(drive);
793 if ((!drive->head || drive->head > 16) && !drive->select.b.lba) {
794 printk(KERN_ERR "%s: INVALID GEOMETRY: %d PHYSICAL HEADS?\n", drive->name, drive->head);
795 (void) idedisk_cleanup(drive);
796 continue;
798 failed--;
800 ide_register_module(&idedisk_module);
801 MOD_DEC_USE_COUNT;
802 return 0;
805 #ifdef MODULE
806 int init_module (void)
808 return idedisk_init();
811 void cleanup_module (void)
813 ide_drive_t *drive;
814 int failed = 0;
816 while ((drive = ide_scan_devices (ide_disk, idedisk_driver.name, &idedisk_driver, failed)) != NULL) {
817 if (idedisk_cleanup (drive)) {
818 printk (KERN_ERR "%s: cleanup_module() called while still busy\n", drive->name);
819 failed++;
821 /* We must remove proc entries defined in this module.
822 Otherwise we oops while accessing these entries */
823 if (drive->proc)
824 ide_remove_proc_entries(drive->proc, idedisk_proc);
826 ide_unregister_module(&idedisk_module);
828 #endif /* MODULE */