blob: 91a9530a4dcbe4617603d5ad7f2e5636d61b9722 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * scsicam.c - SCSI CAM support functions, use for HDIO_GETGEO, etc.
4 *
5 * Copyright 1993, 1994 Drew Eckhardt
6 * Visionary Computing
7 * (Unix and Linux consulting and custom programming)
8 * drew@Colorado.EDU
9 * +1 (303) 786-7975
10 *
11 * For more information, please consult the SCSI-CAM draft.
12 */
13
14#include <linux/module.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090015#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/fs.h>
17#include <linux/genhd.h>
18#include <linux/kernel.h>
19#include <linux/blkdev.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <asm/unaligned.h>
21
22#include <scsi/scsicam.h>
23
24
25static int setsize(unsigned long capacity, unsigned int *cyls, unsigned int *hds,
26 unsigned int *secs);
27
Rob Landleyeb448202007-11-03 13:30:39 -050028/**
29 * scsi_bios_ptable - Read PC partition table out of first sector of device.
30 * @dev: from this device
31 *
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.
35 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070036unsigned char *scsi_bios_ptable(struct block_device *dev)
37{
Christoph Hellwige63105d2020-03-24 08:25:15 +010038 struct address_space *mapping = dev->bd_contains->bd_inode->i_mapping;
39 unsigned char *res = NULL;
40 struct page *page;
41
42 page = read_mapping_page(mapping, 0, NULL);
43 if (IS_ERR(page))
44 return NULL;
45
46 if (!PageError(page))
47 res = kmemdup(page_address(page) + 0x1be, 66, GFP_KERNEL);
48 put_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 return res;
50}
51EXPORT_SYMBOL(scsi_bios_ptable);
52
Rob Landleyeb448202007-11-03 13:30:39 -050053/**
54 * scsicam_bios_param - Determine geometry of a disk in cylinders/heads/sectors.
55 * @bdev: which device
56 * @capacity: size of the disk in sectors
57 * @ip: return value: ip[0]=heads, ip[1]=sectors, ip[2]=cylinders
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 *
Rob Landleyeb448202007-11-03 13:30:39 -050059 * Description : determine the BIOS mapping/geometry used for a drive in a
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 * SCSI-CAM system, storing the results in ip as required
61 * by the HDIO_GETGEO ioctl().
62 *
63 * Returns : -1 on failure, 0 on success.
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 */
65
66int scsicam_bios_param(struct block_device *bdev, sector_t capacity, int *ip)
67{
68 unsigned char *p;
Alan Cox8d55a7862006-06-25 01:58:58 -070069 u64 capacity64 = capacity; /* Suppress gcc warning */
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 int ret;
71
72 p = scsi_bios_ptable(bdev);
73 if (!p)
74 return -1;
75
76 /* try to infer mapping from partition table */
77 ret = scsi_partsize(p, (unsigned long)capacity, (unsigned int *)ip + 2,
78 (unsigned int *)ip + 0, (unsigned int *)ip + 1);
79 kfree(p);
80
Alan Cox8d55a7862006-06-25 01:58:58 -070081 if (ret == -1 && capacity64 < (1ULL << 32)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 /* pick some standard mapping with at most 1024 cylinders,
83 and at most 62 sectors per track - this works up to
84 7905 MB */
85 ret = setsize((unsigned long)capacity, (unsigned int *)ip + 2,
86 (unsigned int *)ip + 0, (unsigned int *)ip + 1);
87 }
88
89 /* if something went wrong, then apparently we have to return
90 a geometry with more than 1024 cylinders */
91 if (ret || ip[0] > 255 || ip[1] > 63) {
92 if ((capacity >> 11) > 65534) {
93 ip[0] = 255;
94 ip[1] = 63;
95 } else {
96 ip[0] = 64;
97 ip[1] = 32;
98 }
99
100 if (capacity > 65535*63*255)
101 ip[2] = 65535;
102 else
103 ip[2] = (unsigned long)capacity / (ip[0] * ip[1]);
104 }
105
106 return 0;
107}
108EXPORT_SYMBOL(scsicam_bios_param);
109
Rob Landleyeb448202007-11-03 13:30:39 -0500110/**
111 * scsi_partsize - Parse cylinders/heads/sectors from PC partition table
112 * @buf: partition table, see scsi_bios_ptable()
113 * @capacity: size of the disk in sectors
114 * @cyls: put cylinders here
115 * @hds: put heads here
116 * @secs: put sectors here
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 *
Mauro Carvalho Chehab739aca02017-05-12 10:04:14 -0300118 * Determine the BIOS mapping/geometry used to create the partition
119 * table, storing the results in @cyls, @hds, and @secs
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 *
Rob Landleyeb448202007-11-03 13:30:39 -0500121 * Returns: -1 on failure, 0 on success.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 */
123
124int scsi_partsize(unsigned char *buf, unsigned long capacity,
125 unsigned int *cyls, unsigned int *hds, unsigned int *secs)
126{
127 struct partition *p = (struct partition *)buf, *largest = NULL;
128 int i, largest_cyl;
129 int cyl, ext_cyl, end_head, end_cyl, end_sector;
130 unsigned int logical_end, physical_end, ext_physical_end;
131
132
133 if (*(unsigned short *) (buf + 64) == 0xAA55) {
134 for (largest_cyl = -1, i = 0; i < 4; ++i, ++p) {
135 if (!p->sys_ind)
136 continue;
137#ifdef DEBUG
138 printk("scsicam_bios_param : partition %d has system \n",
139 i);
140#endif
141 cyl = p->cyl + ((p->sector & 0xc0) << 2);
142 if (cyl > largest_cyl) {
143 largest_cyl = cyl;
144 largest = p;
145 }
146 }
147 }
148 if (largest) {
149 end_cyl = largest->end_cyl + ((largest->end_sector & 0xc0) << 2);
150 end_head = largest->end_head;
151 end_sector = largest->end_sector & 0x3f;
152
153 if (end_head + 1 == 0 || end_sector == 0)
154 return -1;
155
156#ifdef DEBUG
157 printk("scsicam_bios_param : end at h = %d, c = %d, s = %d\n",
158 end_head, end_cyl, end_sector);
159#endif
160
161 physical_end = end_cyl * (end_head + 1) * end_sector +
162 end_head * end_sector + end_sector;
163
164 /* This is the actual _sector_ number at the end */
Christoph Hellwig678e2752014-10-01 20:31:01 +0200165 logical_end = get_unaligned_le32(&largest->start_sect)
166 + get_unaligned_le32(&largest->nr_sects);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167
168 /* This is for >1023 cylinders */
169 ext_cyl = (logical_end - (end_head * end_sector + end_sector))
170 / (end_head + 1) / end_sector;
171 ext_physical_end = ext_cyl * (end_head + 1) * end_sector +
172 end_head * end_sector + end_sector;
173
174#ifdef DEBUG
175 printk("scsicam_bios_param : logical_end=%d physical_end=%d ext_physical_end=%d ext_cyl=%d\n"
176 ,logical_end, physical_end, ext_physical_end, ext_cyl);
177#endif
178
179 if ((logical_end == physical_end) ||
180 (end_cyl == 1023 && ext_physical_end == logical_end)) {
181 *secs = end_sector;
182 *hds = end_head + 1;
183 *cyls = capacity / ((end_head + 1) * end_sector);
184 return 0;
185 }
186#ifdef DEBUG
187 printk("scsicam_bios_param : logical (%u) != physical (%u)\n",
188 logical_end, physical_end);
189#endif
190 }
191 return -1;
192}
193EXPORT_SYMBOL(scsi_partsize);
194
195/*
196 * Function : static int setsize(unsigned long capacity,unsigned int *cyls,
197 * unsigned int *hds, unsigned int *secs);
198 *
199 * Purpose : to determine a near-optimal int 0x13 mapping for a
200 * SCSI disk in terms of lost space of size capacity, storing
201 * the results in *cyls, *hds, and *secs.
202 *
203 * Returns : -1 on failure, 0 on success.
204 *
205 * Extracted from
206 *
207 * WORKING X3T9.2
208 * DRAFT 792D
Rob Landleyeb448202007-11-03 13:30:39 -0500209 * see http://www.t10.org/ftp/t10/drafts/cam/cam-r12b.pdf
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 *
211 * Revision 6
212 * 10-MAR-94
213 * Information technology -
214 * SCSI-2 Common access method
215 * transport and SCSI interface module
216 *
217 * ANNEX A :
218 *
219 * setsize() converts a read capacity value to int 13h
220 * head-cylinder-sector requirements. It minimizes the value for
221 * number of heads and maximizes the number of cylinders. This
222 * will support rather large disks before the number of heads
223 * will not fit in 4 bits (or 6 bits). This algorithm also
224 * minimizes the number of sectors that will be unused at the end
225 * of the disk while allowing for very large disks to be
226 * accommodated. This algorithm does not use physical geometry.
227 */
228
229static int setsize(unsigned long capacity, unsigned int *cyls, unsigned int *hds,
230 unsigned int *secs)
231{
232 unsigned int rv = 0;
233 unsigned long heads, sectors, cylinders, temp;
234
235 cylinders = 1024L; /* Set number of cylinders to max */
236 sectors = 62L; /* Maximize sectors per track */
237
238 temp = cylinders * sectors; /* Compute divisor for heads */
239 heads = capacity / temp; /* Compute value for number of heads */
240 if (capacity % temp) { /* If no remainder, done! */
241 heads++; /* Else, increment number of heads */
242 temp = cylinders * heads; /* Compute divisor for sectors */
243 sectors = capacity / temp; /* Compute value for sectors per
244 track */
245 if (capacity % temp) { /* If no remainder, done! */
246 sectors++; /* Else, increment number of sectors */
247 temp = heads * sectors; /* Compute divisor for cylinders */
248 cylinders = capacity / temp; /* Compute number of cylinders */
249 }
250 }
251 if (cylinders == 0)
252 rv = (unsigned) -1; /* Give error if 0 cylinders */
253
254 *cyls = (unsigned int) cylinders; /* Stuff return values */
255 *secs = (unsigned int) sectors;
256 *hds = (unsigned int) heads;
257 return (rv);
258}