1 // SPDX-License-Identifier: GPL-2.0
3 * scsicam.c - SCSI CAM support functions, use for HDIO_GETGEO, etc.
5 * Copyright 1993, 1994 Drew Eckhardt
7 * (Unix and Linux consulting and custom programming)
11 * For more information, please consult the SCSI-CAM draft.
14 #include <linux/module.h>
15 #include <linux/slab.h>
17 #include <linux/genhd.h>
18 #include <linux/kernel.h>
19 #include <linux/blkdev.h>
20 #include <linux/msdos_partition.h>
21 #include <asm/unaligned.h>
23 #include <scsi/scsicam.h>
26 * scsi_bios_ptable - Read PC partition table out of first sector of device.
27 * @dev: from this device
29 * Description: Reads the first sector from the device and returns %0x42 bytes
30 * starting at offset %0x1be.
31 * Returns: partition table in kmalloc(GFP_KERNEL) memory, or NULL on error.
33 unsigned char *scsi_bios_ptable(struct block_device
*dev
)
35 struct address_space
*mapping
= bdev_whole(dev
)->bd_inode
->i_mapping
;
36 unsigned char *res
= NULL
;
39 page
= read_mapping_page(mapping
, 0, NULL
);
44 res
= kmemdup(page_address(page
) + 0x1be, 66, GFP_KERNEL
);
48 EXPORT_SYMBOL(scsi_bios_ptable
);
51 * scsi_partsize - Parse cylinders/heads/sectors from PC partition table
52 * @bdev: block device to parse
53 * @capacity: size of the disk in sectors
54 * @geom: output in form of [hds, cylinders, sectors]
56 * Determine the BIOS mapping/geometry used to create the partition
57 * table, storing the results in @geom.
59 * Returns: %false on failure, %true on success.
61 bool scsi_partsize(struct block_device
*bdev
, sector_t capacity
, int geom
[3])
63 int cyl
, ext_cyl
, end_head
, end_cyl
, end_sector
;
64 unsigned int logical_end
, physical_end
, ext_physical_end
;
65 struct msdos_partition
*p
, *largest
= NULL
;
69 buf
= scsi_bios_ptable(bdev
);
73 if (*(unsigned short *) (buf
+ 64) == 0xAA55) {
74 int largest_cyl
= -1, i
;
76 for (i
= 0, p
= buf
; i
< 4; i
++, p
++) {
80 printk("scsicam_bios_param : partition %d has system \n",
83 cyl
= p
->cyl
+ ((p
->sector
& 0xc0) << 2);
84 if (cyl
> largest_cyl
) {
91 end_cyl
= largest
->end_cyl
+ ((largest
->end_sector
& 0xc0) << 2);
92 end_head
= largest
->end_head
;
93 end_sector
= largest
->end_sector
& 0x3f;
95 if (end_head
+ 1 == 0 || end_sector
== 0)
99 printk("scsicam_bios_param : end at h = %d, c = %d, s = %d\n",
100 end_head
, end_cyl
, end_sector
);
103 physical_end
= end_cyl
* (end_head
+ 1) * end_sector
+
104 end_head
* end_sector
+ end_sector
;
106 /* This is the actual _sector_ number at the end */
107 logical_end
= get_unaligned_le32(&largest
->start_sect
)
108 + get_unaligned_le32(&largest
->nr_sects
);
110 /* This is for >1023 cylinders */
111 ext_cyl
= (logical_end
- (end_head
* end_sector
+ end_sector
))
112 / (end_head
+ 1) / end_sector
;
113 ext_physical_end
= ext_cyl
* (end_head
+ 1) * end_sector
+
114 end_head
* end_sector
+ end_sector
;
117 printk("scsicam_bios_param : logical_end=%d physical_end=%d ext_physical_end=%d ext_cyl=%d\n"
118 ,logical_end
, physical_end
, ext_physical_end
, ext_cyl
);
121 if (logical_end
== physical_end
||
122 (end_cyl
== 1023 && ext_physical_end
== logical_end
)) {
123 geom
[0] = end_head
+ 1;
124 geom
[1] = end_sector
;
125 geom
[2] = (unsigned long)capacity
/
126 ((end_head
+ 1) * end_sector
);
131 printk("scsicam_bios_param : logical (%u) != physical (%u)\n",
132 logical_end
, physical_end
);
140 EXPORT_SYMBOL(scsi_partsize
);
143 * Function : static int setsize(unsigned long capacity,unsigned int *cyls,
144 * unsigned int *hds, unsigned int *secs);
146 * Purpose : to determine a near-optimal int 0x13 mapping for a
147 * SCSI disk in terms of lost space of size capacity, storing
148 * the results in *cyls, *hds, and *secs.
150 * Returns : -1 on failure, 0 on success.
156 * see http://www.t10.org/ftp/t10/drafts/cam/cam-r12b.pdf
160 * Information technology -
161 * SCSI-2 Common access method
162 * transport and SCSI interface module
166 * setsize() converts a read capacity value to int 13h
167 * head-cylinder-sector requirements. It minimizes the value for
168 * number of heads and maximizes the number of cylinders. This
169 * will support rather large disks before the number of heads
170 * will not fit in 4 bits (or 6 bits). This algorithm also
171 * minimizes the number of sectors that will be unused at the end
172 * of the disk while allowing for very large disks to be
173 * accommodated. This algorithm does not use physical geometry.
176 static int setsize(unsigned long capacity
, unsigned int *cyls
, unsigned int *hds
,
180 unsigned long heads
, sectors
, cylinders
, temp
;
182 cylinders
= 1024L; /* Set number of cylinders to max */
183 sectors
= 62L; /* Maximize sectors per track */
185 temp
= cylinders
* sectors
; /* Compute divisor for heads */
186 heads
= capacity
/ temp
; /* Compute value for number of heads */
187 if (capacity
% temp
) { /* If no remainder, done! */
188 heads
++; /* Else, increment number of heads */
189 temp
= cylinders
* heads
; /* Compute divisor for sectors */
190 sectors
= capacity
/ temp
; /* Compute value for sectors per
192 if (capacity
% temp
) { /* If no remainder, done! */
193 sectors
++; /* Else, increment number of sectors */
194 temp
= heads
* sectors
; /* Compute divisor for cylinders */
195 cylinders
= capacity
/ temp
; /* Compute number of cylinders */
199 rv
= (unsigned) -1; /* Give error if 0 cylinders */
201 *cyls
= (unsigned int) cylinders
; /* Stuff return values */
202 *secs
= (unsigned int) sectors
;
203 *hds
= (unsigned int) heads
;
208 * scsicam_bios_param - Determine geometry of a disk in cylinders/heads/sectors.
209 * @bdev: which device
210 * @capacity: size of the disk in sectors
211 * @ip: return value: ip[0]=heads, ip[1]=sectors, ip[2]=cylinders
213 * Description : determine the BIOS mapping/geometry used for a drive in a
214 * SCSI-CAM system, storing the results in ip as required
215 * by the HDIO_GETGEO ioctl().
217 * Returns : -1 on failure, 0 on success.
219 int scsicam_bios_param(struct block_device
*bdev
, sector_t capacity
, int *ip
)
221 u64 capacity64
= capacity
; /* Suppress gcc warning */
224 /* try to infer mapping from partition table */
225 if (scsi_partsize(bdev
, capacity
, ip
))
228 if (capacity64
< (1ULL << 32)) {
230 * Pick some standard mapping with at most 1024 cylinders, and
231 * at most 62 sectors per track - this works up to 7905 MB.
233 ret
= setsize((unsigned long)capacity
, (unsigned int *)ip
+ 2,
234 (unsigned int *)ip
+ 0, (unsigned int *)ip
+ 1);
238 * If something went wrong, then apparently we have to return a geometry
239 * with more than 1024 cylinders.
241 if (ret
|| ip
[0] > 255 || ip
[1] > 63) {
242 if ((capacity
>> 11) > 65534) {
250 if (capacity
> 65535*63*255)
253 ip
[2] = (unsigned long)capacity
/ (ip
[0] * ip
[1]);
258 EXPORT_SYMBOL(scsicam_bios_param
);