2 * scsicam.c - SCSI CAM support functions, use for HDIO_GETGEO, etc.
4 * Copyright 1993, 1994 Drew Eckhardt
6 * (Unix and Linux consulting and custom programming)
10 * For more information, please consult the SCSI-CAM draft.
13 #include <linux/module.h>
14 #include <linux/slab.h>
16 #include <linux/genhd.h>
17 #include <linux/kernel.h>
18 #include <linux/blkdev.h>
19 #include <linux/buffer_head.h>
20 #include <asm/unaligned.h>
22 #include <scsi/scsicam.h>
25 static int setsize(unsigned long capacity
, unsigned int *cyls
, unsigned int *hds
,
29 * scsi_bios_ptable - Read PC partition table out of first sector of device.
30 * @dev: from this device
32 * Description: Reads the first sector from the device and returns %0x42 bytes
33 * starting at offset %0x1be.
34 * Returns: partition table in kmalloc(GFP_KERNEL) memory, or NULL on error.
36 unsigned char *scsi_bios_ptable(struct block_device
*dev
)
38 unsigned char *res
= kmalloc(66, GFP_KERNEL
);
40 struct block_device
*bdev
= dev
->bd_contains
;
42 void *data
= read_dev_sector(bdev
, 0, §
);
44 memcpy(res
, data
+ 0x1be, 66);
53 EXPORT_SYMBOL(scsi_bios_ptable
);
56 * scsicam_bios_param - Determine geometry of a disk in cylinders/heads/sectors.
58 * @capacity: size of the disk in sectors
59 * @ip: return value: ip[0]=heads, ip[1]=sectors, ip[2]=cylinders
61 * Description : determine the BIOS mapping/geometry used for a drive in a
62 * SCSI-CAM system, storing the results in ip as required
63 * by the HDIO_GETGEO ioctl().
65 * Returns : -1 on failure, 0 on success.
68 int scsicam_bios_param(struct block_device
*bdev
, sector_t capacity
, int *ip
)
71 u64 capacity64
= capacity
; /* Suppress gcc warning */
74 p
= scsi_bios_ptable(bdev
);
78 /* try to infer mapping from partition table */
79 ret
= scsi_partsize(p
, (unsigned long)capacity
, (unsigned int *)ip
+ 2,
80 (unsigned int *)ip
+ 0, (unsigned int *)ip
+ 1);
83 if (ret
== -1 && capacity64
< (1ULL << 32)) {
84 /* pick some standard mapping with at most 1024 cylinders,
85 and at most 62 sectors per track - this works up to
87 ret
= setsize((unsigned long)capacity
, (unsigned int *)ip
+ 2,
88 (unsigned int *)ip
+ 0, (unsigned int *)ip
+ 1);
91 /* if something went wrong, then apparently we have to return
92 a geometry with more than 1024 cylinders */
93 if (ret
|| ip
[0] > 255 || ip
[1] > 63) {
94 if ((capacity
>> 11) > 65534) {
102 if (capacity
> 65535*63*255)
105 ip
[2] = (unsigned long)capacity
/ (ip
[0] * ip
[1]);
110 EXPORT_SYMBOL(scsicam_bios_param
);
113 * scsi_partsize - Parse cylinders/heads/sectors from PC partition table
114 * @buf: partition table, see scsi_bios_ptable()
115 * @capacity: size of the disk in sectors
116 * @cyls: put cylinders here
117 * @hds: put heads here
118 * @secs: put sectors here
120 * Description: determine the BIOS mapping/geometry used to create the partition
121 * table, storing the results in *cyls, *hds, and *secs
123 * Returns: -1 on failure, 0 on success.
126 int scsi_partsize(unsigned char *buf
, unsigned long capacity
,
127 unsigned int *cyls
, unsigned int *hds
, unsigned int *secs
)
129 struct partition
*p
= (struct partition
*)buf
, *largest
= NULL
;
131 int cyl
, ext_cyl
, end_head
, end_cyl
, end_sector
;
132 unsigned int logical_end
, physical_end
, ext_physical_end
;
135 if (*(unsigned short *) (buf
+ 64) == 0xAA55) {
136 for (largest_cyl
= -1, i
= 0; i
< 4; ++i
, ++p
) {
140 printk("scsicam_bios_param : partition %d has system \n",
143 cyl
= p
->cyl
+ ((p
->sector
& 0xc0) << 2);
144 if (cyl
> largest_cyl
) {
151 end_cyl
= largest
->end_cyl
+ ((largest
->end_sector
& 0xc0) << 2);
152 end_head
= largest
->end_head
;
153 end_sector
= largest
->end_sector
& 0x3f;
155 if (end_head
+ 1 == 0 || end_sector
== 0)
159 printk("scsicam_bios_param : end at h = %d, c = %d, s = %d\n",
160 end_head
, end_cyl
, end_sector
);
163 physical_end
= end_cyl
* (end_head
+ 1) * end_sector
+
164 end_head
* end_sector
+ end_sector
;
166 /* This is the actual _sector_ number at the end */
167 logical_end
= get_unaligned(&largest
->start_sect
)
168 + get_unaligned(&largest
->nr_sects
);
170 /* This is for >1023 cylinders */
171 ext_cyl
= (logical_end
- (end_head
* end_sector
+ end_sector
))
172 / (end_head
+ 1) / end_sector
;
173 ext_physical_end
= ext_cyl
* (end_head
+ 1) * end_sector
+
174 end_head
* end_sector
+ end_sector
;
177 printk("scsicam_bios_param : logical_end=%d physical_end=%d ext_physical_end=%d ext_cyl=%d\n"
178 ,logical_end
, physical_end
, ext_physical_end
, ext_cyl
);
181 if ((logical_end
== physical_end
) ||
182 (end_cyl
== 1023 && ext_physical_end
== logical_end
)) {
185 *cyls
= capacity
/ ((end_head
+ 1) * end_sector
);
189 printk("scsicam_bios_param : logical (%u) != physical (%u)\n",
190 logical_end
, physical_end
);
195 EXPORT_SYMBOL(scsi_partsize
);
198 * Function : static int setsize(unsigned long capacity,unsigned int *cyls,
199 * unsigned int *hds, unsigned int *secs);
201 * Purpose : to determine a near-optimal int 0x13 mapping for a
202 * SCSI disk in terms of lost space of size capacity, storing
203 * the results in *cyls, *hds, and *secs.
205 * Returns : -1 on failure, 0 on success.
211 * see http://www.t10.org/ftp/t10/drafts/cam/cam-r12b.pdf
215 * Information technology -
216 * SCSI-2 Common access method
217 * transport and SCSI interface module
221 * setsize() converts a read capacity value to int 13h
222 * head-cylinder-sector requirements. It minimizes the value for
223 * number of heads and maximizes the number of cylinders. This
224 * will support rather large disks before the number of heads
225 * will not fit in 4 bits (or 6 bits). This algorithm also
226 * minimizes the number of sectors that will be unused at the end
227 * of the disk while allowing for very large disks to be
228 * accommodated. This algorithm does not use physical geometry.
231 static int setsize(unsigned long capacity
, unsigned int *cyls
, unsigned int *hds
,
235 unsigned long heads
, sectors
, cylinders
, temp
;
237 cylinders
= 1024L; /* Set number of cylinders to max */
238 sectors
= 62L; /* Maximize sectors per track */
240 temp
= cylinders
* sectors
; /* Compute divisor for heads */
241 heads
= capacity
/ temp
; /* Compute value for number of heads */
242 if (capacity
% temp
) { /* If no remainder, done! */
243 heads
++; /* Else, increment number of heads */
244 temp
= cylinders
* heads
; /* Compute divisor for sectors */
245 sectors
= capacity
/ temp
; /* Compute value for sectors per
247 if (capacity
% temp
) { /* If no remainder, done! */
248 sectors
++; /* Else, increment number of sectors */
249 temp
= heads
* sectors
; /* Compute divisor for cylinders */
250 cylinders
= capacity
/ temp
; /* Compute number of cylinders */
254 rv
= (unsigned) -1; /* Give error if 0 cylinders */
256 *cyls
= (unsigned int) cylinders
; /* Stuff return values */
257 *secs
= (unsigned int) sectors
;
258 *hds
= (unsigned int) heads
;