2 * linux/drivers/block/ide-disk.c Version 1.09 April 23, 1999
4 * Copyright (C) 1994-1998 Linus Torvalds & authors (see below)
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
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>
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>
55 #include <asm/uaccess.h>
58 #ifdef CONFIG_BLK_DEV_PDC4030
59 #define IS_PDC4030_DRIVE (HWIF(drive)->chipset == ide_pdc4030)
61 #define IS_PDC4030_DRIVE (0) /* auto-NULLs out pdc4030 code */
64 static void idedisk_bswap_data (void *buffer
, int 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
);
78 idedisk_bswap_data(buffer
, wcount
);
81 static inline void idedisk_output_data (ide_drive_t
*drive
, void *buffer
, unsigned int wcount
)
84 idedisk_bswap_data(buffer
, wcount
);
85 ide_output_data(drive
, buffer
, wcount
);
86 idedisk_bswap_data(buffer
, wcount
);
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
98 static int lba_capacity_is_ok (struct hd_driveid
*id
)
100 unsigned long lba_sects
= id
->lba_capacity
;
101 unsigned long chs_sects
= id
->cyls
* id
->heads
* id
->sectors
;
102 unsigned long _10_percent
= chs_sects
/ 10;
105 * very large drives (8GB+) may lie about the number of cylinders
106 * This is a split test for drives 8 Gig and Bigger only.
108 if ((id
->lba_capacity
>= 16514064) && (id
->cyls
== 0x3fff) &&
109 (id
->heads
== 16) && (id
->sectors
== 63)) {
110 id
->cyls
= lba_sects
/ (16 * 63); /* correct cyls */
111 return 1; /* lba_capacity is our only option */
114 * ... and at least one TLA VBC has POS instead of brain and can't
117 if ((id
->lba_capacity
>= 15481935) && (id
->cyls
== 0x3fff) &&
118 (id
->heads
== 15) && (id
->sectors
== 63)) {
119 id
->cyls
= lba_sects
/ (15 * 63); /* correct cyls */
120 return 1; /* lba_capacity is our only option */
122 /* perform a rough sanity check on lba_sects: within 10% is "okay" */
123 if ((lba_sects
- chs_sects
) < _10_percent
) {
124 return 1; /* lba_capacity is good */
126 /* some drives have the word order reversed */
127 lba_sects
= (lba_sects
<< 16) | (lba_sects
>> 16);
128 if ((lba_sects
- chs_sects
) < _10_percent
) {
129 id
->lba_capacity
= lba_sects
; /* fix it */
130 return 1; /* lba_capacity is (now) good */
132 return 0; /* lba_capacity value is bad */
136 * read_intr() is the handler for disk read/multread interrupts
138 static void read_intr (ide_drive_t
*drive
)
142 unsigned int msect
, nsect
;
145 if (!OK_STAT(stat
=GET_STAT(),DATA_READY
,BAD_R_STAT
)) {
146 ide_error(drive
, "read_intr", stat
);
149 msect
= drive
->mult_count
;
152 rq
= HWGROUP(drive
)->rq
;
154 if ((nsect
= rq
->current_nr_sectors
) > msect
)
160 * PIO input can take longish times, so we drop the spinlock.
161 * On SMP, bad things might happen if syscall level code adds
162 * a new request while we do this PIO, so we just freeze all
163 * request queue handling while doing the PIO. FIXME
165 idedisk_input_data(drive
, rq
->buffer
, nsect
* SECTOR_WORDS
);
167 printk("%s: read: sectors(%ld-%ld), buffer=0x%08lx, remaining=%ld\n",
168 drive
->name
, rq
->sector
, rq
->sector
+nsect
-1,
169 (unsigned long) rq
->buffer
+(nsect
<<9), rq
->nr_sectors
-nsect
);
172 rq
->buffer
+= nsect
<<9;
174 i
= (rq
->nr_sectors
-= nsect
);
175 if ((rq
->current_nr_sectors
-= nsect
) <= 0)
176 ide_end_request(1, HWGROUP(drive
));
180 ide_set_handler (drive
, &read_intr
, WAIT_CMD
);
185 * write_intr() is the handler for disk write interrupts
187 static void write_intr (ide_drive_t
*drive
)
191 ide_hwgroup_t
*hwgroup
= HWGROUP(drive
);
192 struct request
*rq
= hwgroup
->rq
;
195 if (OK_STAT(stat
=GET_STAT(),DRIVE_READY
,drive
->bad_wstat
)) {
197 printk("%s: write: sector %ld, buffer=0x%08lx, remaining=%ld\n",
198 drive
->name
, rq
->sector
, (unsigned long) rq
->buffer
,
201 if ((rq
->nr_sectors
== 1) ^ ((stat
& DRQ_STAT
) != 0)) {
205 i
= --rq
->nr_sectors
;
206 --rq
->current_nr_sectors
;
207 if (rq
->current_nr_sectors
<= 0)
208 ide_end_request(1, hwgroup
);
210 idedisk_output_data (drive
, rq
->buffer
, SECTOR_WORDS
);
211 ide_set_handler (drive
, &write_intr
, WAIT_CMD
);
219 ide_error(drive
, "write_intr", stat
);
223 * ide_multwrite() transfers a block of up to mcount sectors of data
224 * to a drive as part of a disk multiple-sector write operation.
226 void ide_multwrite (ide_drive_t
*drive
, unsigned int mcount
)
228 struct request
*rq
= &HWGROUP(drive
)->wrq
;
231 unsigned int nsect
= rq
->current_nr_sectors
;
236 idedisk_output_data(drive
, rq
->buffer
, nsect
<<7);
238 printk("%s: multwrite: sector %ld, buffer=0x%08lx, count=%d, remaining=%ld\n",
239 drive
->name
, rq
->sector
, (unsigned long) rq
->buffer
,
240 nsect
, rq
->nr_sectors
- nsect
);
242 #ifdef CONFIG_BLK_DEV_PDC4030
245 if ((rq
->nr_sectors
-= nsect
) <= 0)
247 if ((rq
->current_nr_sectors
-= nsect
) == 0) {
248 if ((rq
->bh
= rq
->bh
->b_reqnext
) != NULL
) {
249 rq
->current_nr_sectors
= rq
->bh
->b_size
>>9;
250 rq
->buffer
= rq
->bh
->b_data
;
252 panic("%s: buffer list corrupted\n", drive
->name
);
256 rq
->buffer
+= nsect
<< 9;
262 * multwrite_intr() is the handler for disk multwrite interrupts
264 static void multwrite_intr (ide_drive_t
*drive
)
268 ide_hwgroup_t
*hwgroup
= HWGROUP(drive
);
269 struct request
*rq
= &hwgroup
->wrq
;
272 if (OK_STAT(stat
=GET_STAT(),DRIVE_READY
,drive
->bad_wstat
)) {
273 if (stat
& DRQ_STAT
) {
274 if (rq
->nr_sectors
) {
275 ide_multwrite(drive
, drive
->mult_count
);
276 ide_set_handler (drive
, &multwrite_intr
, WAIT_CMD
);
280 if (!rq
->nr_sectors
) { /* all done? */
282 for (i
= rq
->nr_sectors
; i
> 0;){
283 i
-= rq
->current_nr_sectors
;
284 ide_end_request(1, hwgroup
);
293 ide_error(drive
, "multwrite_intr", stat
);
297 * set_multmode_intr() is invoked on completion of a WIN_SETMULT cmd.
299 static void set_multmode_intr (ide_drive_t
*drive
)
301 byte stat
= GET_STAT();
304 if (OK_STAT(stat
,READY_STAT
,BAD_STAT
) || drive
->mult_req
== 0) {
306 if (OK_STAT(stat
,READY_STAT
,BAD_STAT
)) {
308 drive
->mult_count
= drive
->mult_req
;
310 drive
->mult_req
= drive
->mult_count
= 0;
311 drive
->special
.b
.recalibrate
= 1;
312 (void) ide_dump_status(drive
, "set_multmode", stat
);
317 * set_geometry_intr() is invoked on completion of a WIN_SPECIFY cmd.
319 static void set_geometry_intr (ide_drive_t
*drive
)
321 byte stat
= GET_STAT();
323 if (!OK_STAT(stat
,READY_STAT
,BAD_STAT
))
324 ide_error(drive
, "set_geometry_intr", stat
);
328 * recal_intr() is invoked on completion of a WIN_RESTORE (recalibrate) cmd.
330 static void recal_intr (ide_drive_t
*drive
)
332 byte stat
= GET_STAT();
334 if (!OK_STAT(stat
,READY_STAT
,BAD_STAT
))
335 ide_error(drive
, "recal_intr", stat
);
339 * do_rw_disk() issues READ and WRITE commands to a disk,
340 * using LBA if supported, or CHS otherwise, to address sectors.
341 * It also takes care of issuing special DRIVE_CMDs.
343 static void do_rw_disk (ide_drive_t
*drive
, struct request
*rq
, unsigned long block
)
346 OUT_BYTE(drive
->ctl
,IDE_CONTROL_REG
);
347 OUT_BYTE(rq
->nr_sectors
,IDE_NSECTOR_REG
);
348 #ifdef CONFIG_BLK_DEV_PDC4030
349 if (drive
->select
.b
.lba
|| IS_PDC4030_DRIVE
) {
350 #else /* !CONFIG_BLK_DEV_PDC4030 */
351 if (drive
->select
.b
.lba
) {
352 #endif /* CONFIG_BLK_DEV_PDC4030 */
354 printk("%s: %sing: LBAsect=%ld, sectors=%ld, buffer=0x%08lx\n",
355 drive
->name
, (rq
->cmd
==READ
)?"read":"writ",
356 block
, rq
->nr_sectors
, (unsigned long) rq
->buffer
);
358 OUT_BYTE(block
,IDE_SECTOR_REG
);
359 OUT_BYTE(block
>>=8,IDE_LCYL_REG
);
360 OUT_BYTE(block
>>=8,IDE_HCYL_REG
);
361 OUT_BYTE(((block
>>8)&0x0f)|drive
->select
.all
,IDE_SELECT_REG
);
363 unsigned int sect
,head
,cyl
,track
;
364 track
= block
/ drive
->sect
;
365 sect
= block
% drive
->sect
+ 1;
366 OUT_BYTE(sect
,IDE_SECTOR_REG
);
367 head
= track
% drive
->head
;
368 cyl
= track
/ drive
->head
;
369 OUT_BYTE(cyl
,IDE_LCYL_REG
);
370 OUT_BYTE(cyl
>>8,IDE_HCYL_REG
);
371 OUT_BYTE(head
|drive
->select
.all
,IDE_SELECT_REG
);
373 printk("%s: %sing: CHS=%d/%d/%d, sectors=%ld, buffer=0x%08lx\n",
374 drive
->name
, (rq
->cmd
==READ
)?"read":"writ", cyl
,
375 head
, sect
, rq
->nr_sectors
, (unsigned long) rq
->buffer
);
378 #ifdef CONFIG_BLK_DEV_PDC4030
379 if (IS_PDC4030_DRIVE
) {
380 extern void do_pdc4030_io(ide_drive_t
*, struct request
*);
381 do_pdc4030_io (drive
, rq
);
384 #endif /* CONFIG_BLK_DEV_PDC4030 */
385 if (rq
->cmd
== READ
) {
386 #ifdef CONFIG_BLK_DEV_IDEDMA
387 if (drive
->using_dma
&& !(HWIF(drive
)->dmaproc(ide_dma_read
, drive
)))
389 #endif /* CONFIG_BLK_DEV_IDEDMA */
390 ide_set_handler(drive
, &read_intr
, WAIT_CMD
);
391 OUT_BYTE(drive
->mult_count
? WIN_MULTREAD
: WIN_READ
, IDE_COMMAND_REG
);
394 if (rq
->cmd
== WRITE
) {
395 #ifdef CONFIG_BLK_DEV_IDEDMA
396 if (drive
->using_dma
&& !(HWIF(drive
)->dmaproc(ide_dma_write
, drive
)))
398 #endif /* CONFIG_BLK_DEV_IDEDMA */
399 OUT_BYTE(drive
->mult_count
? WIN_MULTWRITE
: WIN_WRITE
, IDE_COMMAND_REG
);
400 if (ide_wait_stat(drive
, DATA_READY
, drive
->bad_wstat
, WAIT_DRQ
)) {
401 printk(KERN_ERR
"%s: no DRQ after issuing %s\n", drive
->name
,
402 drive
->mult_count
? "MULTWRITE" : "WRITE");
406 __cli(); /* local CPU only */
407 if (drive
->mult_count
) {
408 HWGROUP(drive
)->wrq
= *rq
; /* scratchpad */
409 ide_set_handler (drive
, &multwrite_intr
, WAIT_CMD
);
410 ide_multwrite(drive
, drive
->mult_count
);
412 ide_set_handler (drive
, &write_intr
, WAIT_CMD
);
413 idedisk_output_data(drive
, rq
->buffer
, SECTOR_WORDS
);
417 printk(KERN_ERR
"%s: bad command: %d\n", drive
->name
, rq
->cmd
);
418 ide_end_request(0, HWGROUP(drive
));
421 static int idedisk_open (struct inode
*inode
, struct file
*filp
, ide_drive_t
*drive
)
424 if (drive
->removable
&& drive
->usage
== 1) {
425 check_disk_change(inode
->i_rdev
);
427 * Ignore the return code from door_lock,
428 * since the open() has already succeeded,
429 * and the door_lock is irrelevant at this point.
431 if (drive
->doorlocking
&& ide_wait_cmd(drive
, WIN_DOORLOCK
, 0, 0, 0, NULL
))
432 drive
->doorlocking
= 0;
437 static void idedisk_release (struct inode
*inode
, struct file
*filp
, ide_drive_t
*drive
)
439 if (drive
->removable
&& !drive
->usage
) {
440 invalidate_buffers(inode
->i_rdev
);
441 if (drive
->doorlocking
&& ide_wait_cmd(drive
, WIN_DOORUNLOCK
, 0, 0, 0, NULL
))
442 drive
->doorlocking
= 0;
447 static int idedisk_media_change (ide_drive_t
*drive
)
449 return drive
->removable
; /* if removable, always assume it was changed */
453 * current_capacity() returns the capacity (in sectors) of a drive
454 * according to its current geometry/LBA settings.
456 static unsigned long idedisk_capacity (ide_drive_t
*drive
)
458 struct hd_driveid
*id
= drive
->id
;
459 unsigned long capacity
= drive
->cyl
* drive
->head
* drive
->sect
;
461 drive
->select
.b
.lba
= 0;
462 /* Determine capacity, and use LBA if the drive properly supports it */
463 if (id
!= NULL
&& (id
->capability
& 2) && lba_capacity_is_ok(id
)) {
464 if (id
->lba_capacity
>= capacity
) {
465 drive
->cyl
= id
->lba_capacity
/ (drive
->head
* drive
->sect
);
466 capacity
= id
->lba_capacity
;
467 drive
->select
.b
.lba
= 1;
470 return (capacity
- drive
->sect0
);
473 static void idedisk_special (ide_drive_t
*drive
)
475 special_t
*s
= &drive
->special
;
477 if (s
->b
.set_geometry
) {
478 s
->b
.set_geometry
= 0;
479 OUT_BYTE(drive
->sect
,IDE_SECTOR_REG
);
480 OUT_BYTE(drive
->cyl
,IDE_LCYL_REG
);
481 OUT_BYTE(drive
->cyl
>>8,IDE_HCYL_REG
);
482 OUT_BYTE(((drive
->head
-1)|drive
->select
.all
)&0xBF,IDE_SELECT_REG
);
483 if (!IS_PDC4030_DRIVE
)
484 ide_cmd(drive
, WIN_SPECIFY
, drive
->sect
, &set_geometry_intr
);
485 } else if (s
->b
.recalibrate
) {
486 s
->b
.recalibrate
= 0;
487 if (!IS_PDC4030_DRIVE
)
488 ide_cmd(drive
, WIN_RESTORE
, drive
->sect
, &recal_intr
);
489 } else if (s
->b
.set_multmode
) {
490 s
->b
.set_multmode
= 0;
491 if (drive
->id
&& drive
->mult_req
> drive
->id
->max_multsect
)
492 drive
->mult_req
= drive
->id
->max_multsect
;
493 if (!IS_PDC4030_DRIVE
)
494 ide_cmd(drive
, WIN_SETMULT
, drive
->mult_req
, &set_multmode_intr
);
496 int special
= s
->all
;
498 printk(KERN_ERR
"%s: bad special flag: 0x%02x\n", drive
->name
, special
);
502 static void idedisk_pre_reset (ide_drive_t
*drive
)
504 drive
->special
.all
= 0;
505 drive
->special
.b
.set_geometry
= 1;
506 drive
->special
.b
.recalibrate
= 1;
507 if (OK_TO_RESET_CONTROLLER
)
508 drive
->mult_count
= 0;
509 if (!drive
->keep_settings
)
511 if (drive
->mult_req
!= drive
->mult_count
)
512 drive
->special
.b
.set_multmode
= 1;
515 #ifdef CONFIG_PROC_FS
517 static int smart_enable(ide_drive_t
*drive
)
519 return ide_wait_cmd(drive
, WIN_SMART
, 0, SMART_ENABLE
, 0, NULL
);
522 static int get_smart_values(ide_drive_t
*drive
, byte
*buf
)
524 (void) smart_enable(drive
);
525 return ide_wait_cmd(drive
, WIN_SMART
, 0, SMART_READ_VALUES
, 1, buf
);
528 static int get_smart_thresholds(ide_drive_t
*drive
, byte
*buf
)
530 (void) smart_enable(drive
);
531 return ide_wait_cmd(drive
, WIN_SMART
, 0, SMART_READ_THRESHOLDS
, 1, buf
);
534 static int proc_idedisk_read_cache
535 (char *page
, char **start
, off_t off
, int count
, int *eof
, void *data
)
537 ide_drive_t
*drive
= (ide_drive_t
*) data
;
542 len
= sprintf(out
,"%i\n", drive
->id
->buf_size
/ 2);
544 len
= sprintf(out
,"(none)\n");
545 PROC_IDE_READ_RETURN(page
,start
,off
,count
,eof
,len
);
548 static int proc_idedisk_read_smart_thresholds
549 (char *page
, char **start
, off_t off
, int count
, int *eof
, void *data
)
551 ide_drive_t
*drive
= (ide_drive_t
*)data
;
554 if (!get_smart_thresholds(drive
, page
)) {
555 unsigned short *val
= ((unsigned short *)page
) + 2;
556 char *out
= ((char *)val
) + (SECTOR_WORDS
* 4);
559 out
+= sprintf(out
, "%04x%c", le16_to_cpu(*val
), (++i
& 7) ? ' ' : '\n');
561 } while (i
< (SECTOR_WORDS
* 2));
564 PROC_IDE_READ_RETURN(page
,start
,off
,count
,eof
,len
);
567 static int proc_idedisk_read_smart_values
568 (char *page
, char **start
, off_t off
, int count
, int *eof
, void *data
)
570 ide_drive_t
*drive
= (ide_drive_t
*)data
;
573 if (!get_smart_values(drive
, page
)) {
574 unsigned short *val
= ((unsigned short *)page
) + 2;
575 char *out
= ((char *)val
) + (SECTOR_WORDS
* 4);
578 out
+= sprintf(out
, "%04x%c", le16_to_cpu(*val
), (++i
& 7) ? ' ' : '\n');
580 } while (i
< (SECTOR_WORDS
* 2));
583 PROC_IDE_READ_RETURN(page
,start
,off
,count
,eof
,len
);
586 static ide_proc_entry_t idedisk_proc
[] = {
587 { "cache", S_IFREG
|S_IRUGO
, proc_idedisk_read_cache
, NULL
},
588 { "geometry", S_IFREG
|S_IRUGO
, proc_ide_read_geometry
, NULL
},
589 { "smart_values", S_IFREG
|S_IRUSR
, proc_idedisk_read_smart_values
, NULL
},
590 { "smart_thresholds", S_IFREG
|S_IRUSR
, proc_idedisk_read_smart_thresholds
, NULL
},
591 { NULL
, 0, NULL
, NULL
}
596 #define idedisk_proc NULL
598 #endif /* CONFIG_PROC_FS */
600 static int set_multcount(ide_drive_t
*drive
, int arg
)
604 if (drive
->special
.b
.set_multmode
)
606 ide_init_drive_cmd (&rq
);
607 drive
->mult_req
= arg
;
608 drive
->special
.b
.set_multmode
= 1;
609 (void) ide_do_drive_cmd (drive
, &rq
, ide_wait
);
610 return (drive
->mult_count
== arg
) ? 0 : -EIO
;
613 static int set_nowerr(ide_drive_t
*drive
, int arg
)
617 if (ide_spin_wait_hwgroup(drive
, &flags
))
620 drive
->bad_wstat
= arg
? BAD_R_STAT
: BAD_W_STAT
;
621 spin_unlock_irqrestore(&HWGROUP(drive
)->spinlock
, flags
);
625 static void idedisk_add_settings(ide_drive_t
*drive
)
627 struct hd_driveid
*id
= drive
->id
;
628 int major
= HWIF(drive
)->major
;
629 int minor
= drive
->select
.b
.unit
<< PARTN_BITS
;
631 ide_add_setting(drive
, "bios_cyl", SETTING_RW
, -1, -1, TYPE_SHORT
, 0, 65535, 1, 1, &drive
->bios_cyl
, NULL
);
632 ide_add_setting(drive
, "bios_head", SETTING_RW
, -1, -1, TYPE_BYTE
, 0, 255, 1, 1, &drive
->bios_head
, NULL
);
633 ide_add_setting(drive
, "bios_sect", SETTING_RW
, -1, -1, TYPE_BYTE
, 0, 63, 1, 1, &drive
->bios_sect
, NULL
);
634 ide_add_setting(drive
, "bswap", SETTING_READ
, -1, -1, TYPE_BYTE
, 0, 1, 1, 1, &drive
->bswap
, NULL
);
635 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
);
636 ide_add_setting(drive
, "nowerr", SETTING_RW
, HDIO_GET_NOWERR
, HDIO_SET_NOWERR
, TYPE_BYTE
, 0, 1, 1, 1, &drive
->nowerr
, set_nowerr
);
637 ide_add_setting(drive
, "breada_readahead", SETTING_RW
, BLKRAGET
, BLKRASET
, TYPE_INT
, 0, 255, 1, 2, &read_ahead
[major
], NULL
);
638 ide_add_setting(drive
, "file_readahead", SETTING_RW
, BLKFRAGET
, BLKFRASET
, TYPE_INTA
, 0, INT_MAX
, 1, 1024, &max_readahead
[major
][minor
], NULL
);
639 ide_add_setting(drive
, "max_kb_per_request", SETTING_RW
, BLKSECTGET
, BLKSECTSET
, TYPE_INTA
, 1, 255, 1, 2, &max_sectors
[major
][minor
], NULL
);
644 * IDE subdriver functions, registered with ide.c
646 static ide_driver_t idedisk_driver
= {
647 "ide-disk", /* name */
648 IDEDISK_VERSION
, /* version */
649 ide_disk
, /* media */
651 1, /* supports_dma */
652 0, /* supports_dsc_overlap */
654 do_rw_disk
, /* do_request */
655 NULL
, /* end_request */
657 idedisk_open
, /* open */
658 idedisk_release
, /* release */
659 idedisk_media_change
, /* media_change */
660 idedisk_pre_reset
, /* pre_reset */
661 idedisk_capacity
, /* capacity */
662 idedisk_special
, /* special */
663 idedisk_proc
/* proc */
666 int idedisk_init (void);
667 static ide_module_t idedisk_module
= {
674 static int idedisk_cleanup (ide_drive_t
*drive
)
676 return ide_unregister_subdriver(drive
);
679 static void idedisk_setup (ide_drive_t
*drive
)
681 struct hd_driveid
*id
= drive
->id
;
682 unsigned long capacity
, check
;
684 idedisk_add_settings(drive
);
690 * CompactFlash cards and their brethern look just like hard drives
691 * to us, but they are removable and don't have a doorlock mechanism.
693 if (drive
->removable
&& !drive_is_flashcard(drive
)) {
695 * Removable disks (eg. SYQUEST); ignore 'WD' drives
697 if (id
->model
[0] != 'W' || id
->model
[1] != 'D') {
698 drive
->doorlocking
= 1;
702 /* Extract geometry if we did not already have one for the drive */
703 if (!drive
->cyl
|| !drive
->head
|| !drive
->sect
) {
704 drive
->cyl
= drive
->bios_cyl
= id
->cyls
;
705 drive
->head
= drive
->bios_head
= id
->heads
;
706 drive
->sect
= drive
->bios_sect
= id
->sectors
;
708 /* Handle logical geometry translation by the drive */
709 if ((id
->field_valid
& 1) && id
->cur_cyls
&&
710 id
->cur_heads
&& (id
->cur_heads
<= 16) && id
->cur_sectors
) {
712 * Extract the physical drive geometry for our use.
713 * Note that we purposely do *not* update the bios info.
714 * This way, programs that use it (like fdisk) will
715 * still have the same logical view as the BIOS does,
716 * which keeps the partition table from being screwed.
718 * An exception to this is the cylinder count,
719 * which we reexamine later on to correct for 1024 limitations.
721 drive
->cyl
= id
->cur_cyls
;
722 drive
->head
= id
->cur_heads
;
723 drive
->sect
= id
->cur_sectors
;
725 /* check for word-swapped "capacity" field in id information */
726 capacity
= drive
->cyl
* drive
->head
* drive
->sect
;
727 check
= (id
->cur_capacity0
<< 16) | id
->cur_capacity1
;
728 if (check
== capacity
) { /* was it swapped? */
729 /* yes, bring it into little-endian order: */
730 id
->cur_capacity0
= (capacity
>> 0) & 0xffff;
731 id
->cur_capacity1
= (capacity
>> 16) & 0xffff;
734 /* Use physical geometry if what we have still makes no sense */
735 if ((!drive
->head
|| drive
->head
> 16) &&
736 id
->heads
&& id
->heads
<= 16) {
737 if ((id
->lba_capacity
> 16514064) || (id
->cyls
== 0x3fff)) {
738 id
->cyls
= ((int)(id
->lba_capacity
/(id
->heads
* id
->sectors
)));
740 drive
->cyl
= id
->cur_cyls
= id
->cyls
;
741 drive
->head
= id
->cur_heads
= id
->heads
;
742 drive
->sect
= id
->cur_sectors
= id
->sectors
;
745 /* calculate drive capacity, and select LBA if possible */
746 capacity
= idedisk_capacity (drive
);
749 * if possible, give fdisk access to more of the drive,
750 * by correcting bios_cyls:
752 if ((capacity
>= (drive
->bios_cyl
* drive
->bios_sect
* drive
->bios_head
)) &&
753 (!drive
->forced_geom
) && drive
->bios_sect
&& drive
->bios_head
) {
754 drive
->bios_cyl
= (capacity
/ drive
->bios_sect
) / drive
->bios_head
;
756 printk("Fixing Geometry :: CHS=%d/%d/%d to CHS=%d/%d/%d\n",
758 drive
->id
->cur_heads
,
759 drive
->id
->cur_sectors
,
764 drive
->id
->cur_cyls
= drive
->bios_cyl
;
765 drive
->id
->cur_heads
= drive
->bios_head
;
766 drive
->id
->cur_sectors
= drive
->bios_sect
;
769 #if 0 /* done instead for entire identify block in arch/ide.h stuff */
770 /* fix byte-ordering of buffer size field */
771 id
->buf_size
= le16_to_cpu(id
->buf_size
);
773 printk (KERN_INFO
"%s: %.40s, %ldMB w/%dkB Cache, CHS=%d/%d/%d",
774 drive
->name
, id
->model
,
775 capacity
/2048L, id
->buf_size
/2,
776 drive
->bios_cyl
, drive
->bios_head
, drive
->bios_sect
);
778 if (drive
->using_dma
) {
779 if ((id
->field_valid
& 4) && (id
->word93
& 0x2000) &&
780 (id
->dma_ultra
& (id
->dma_ultra
>> 11) & 3)) {
781 printk(", UDMA(66)"); /* UDMA BIOS-enabled! */
782 } else if ((id
->field_valid
& 4) &&
783 (id
->dma_ultra
& (id
->dma_ultra
>> 8) & 7)) {
784 printk(", UDMA(33)"); /* UDMA BIOS-enabled! */
785 } else if (id
->field_valid
& 4) {
786 printk(", (U)DMA"); /* Can be BIOS-enabled! */
793 if (drive
->select
.b
.lba
) {
794 if (*(int *)&id
->cur_capacity0
< id
->lba_capacity
) {
796 printk(" CurSects=%d, LBASects=%d, ",
797 *(int *)&id
->cur_capacity0
, id
->lba_capacity
);
799 *(int *)&id
->cur_capacity0
= id
->lba_capacity
;
801 printk( "Fixed CurSects=%d\n", *(int *)&id
->cur_capacity0
);
806 drive
->mult_count
= 0;
807 if (id
->max_multsect
) {
808 #ifdef CONFIG_IDEDISK_MULTI_MODE
809 id
->multsect
= ((id
->max_multsect
/2) > 1) ? id
->max_multsect
: 0;
810 id
->multsect_valid
= id
->multsect
? 1 : 0;
811 drive
->mult_req
= id
->multsect_valid
? id
->max_multsect
: INITIAL_MULT_COUNT
;
812 drive
->special
.b
.set_multmode
= drive
->mult_req
? 1 : 0;
813 #else /* original, pre IDE-NFG, per request of AC */
814 drive
->mult_req
= INITIAL_MULT_COUNT
;
815 if (drive
->mult_req
> id
->max_multsect
)
816 drive
->mult_req
= id
->max_multsect
;
817 if (drive
->mult_req
|| ((id
->multsect_valid
& 1) && id
->multsect
))
818 drive
->special
.b
.set_multmode
= 1;
821 drive
->no_io_32bit
= id
->dword_io
? 1 : 0;
824 int idedisk_init (void)
830 while ((drive
= ide_scan_devices (ide_disk
, idedisk_driver
.name
, NULL
, failed
++)) != NULL
) {
831 if (ide_register_subdriver (drive
, &idedisk_driver
, IDE_SUBDRIVER_VERSION
)) {
832 printk (KERN_ERR
"ide-disk: %s: Failed to register the driver with ide.c\n", drive
->name
);
835 idedisk_setup(drive
);
836 if ((!drive
->head
|| drive
->head
> 16) && !drive
->select
.b
.lba
) {
837 printk(KERN_ERR
"%s: INVALID GEOMETRY: %d PHYSICAL HEADS?\n", drive
->name
, drive
->head
);
838 (void) idedisk_cleanup(drive
);
843 ide_register_module(&idedisk_module
);
849 int init_module (void)
851 return idedisk_init();
854 void cleanup_module (void)
859 while ((drive
= ide_scan_devices (ide_disk
, idedisk_driver
.name
, &idedisk_driver
, failed
)) != NULL
) {
860 if (idedisk_cleanup (drive
)) {
861 printk (KERN_ERR
"%s: cleanup_module() called while still busy\n", drive
->name
);
864 /* We must remove proc entries defined in this module.
865 Otherwise we oops while accessing these entries */
867 ide_remove_proc_entries(drive
->proc
, idedisk_proc
);
869 ide_unregister_module(&idedisk_module
);