blob: 0ffdb8f2995f7a9dc59d14f912ce1dafd94fcc3e [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>
Matthew Wilcox (Oracle)4ee60ec2021-05-06 18:02:27 -070020#include <linux/pagemap.h>
Christoph Hellwig1442f762020-03-24 08:25:26 +010021#include <linux/msdos_partition.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <asm/unaligned.h>
23
24#include <scsi/scsicam.h>
25
Rob Landleyeb448202007-11-03 13:30:39 -050026/**
27 * scsi_bios_ptable - Read PC partition table out of first sector of device.
28 * @dev: from this device
29 *
30 * Description: Reads the first sector from the device and returns %0x42 bytes
31 * starting at offset %0x1be.
32 * Returns: partition table in kmalloc(GFP_KERNEL) memory, or NULL on error.
33 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070034unsigned char *scsi_bios_ptable(struct block_device *dev)
35{
Christoph Hellwiga954ea82020-11-23 13:29:55 +010036 struct address_space *mapping = bdev_whole(dev)->bd_inode->i_mapping;
Christoph Hellwige63105d2020-03-24 08:25:15 +010037 unsigned char *res = NULL;
38 struct page *page;
39
40 page = read_mapping_page(mapping, 0, NULL);
41 if (IS_ERR(page))
42 return NULL;
43
44 if (!PageError(page))
45 res = kmemdup(page_address(page) + 0x1be, 66, GFP_KERNEL);
46 put_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 return res;
48}
49EXPORT_SYMBOL(scsi_bios_ptable);
50
Rob Landleyeb448202007-11-03 13:30:39 -050051/**
Rob Landleyeb448202007-11-03 13:30:39 -050052 * scsi_partsize - Parse cylinders/heads/sectors from PC partition table
Christoph Hellwiga10183d2020-03-24 08:25:17 +010053 * @bdev: block device to parse
Rob Landleyeb448202007-11-03 13:30:39 -050054 * @capacity: size of the disk in sectors
Christoph Hellwiga10183d2020-03-24 08:25:17 +010055 * @geom: output in form of [hds, cylinders, sectors]
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 *
Mauro Carvalho Chehab739aca02017-05-12 10:04:14 -030057 * Determine the BIOS mapping/geometry used to create the partition
Christoph Hellwiga10183d2020-03-24 08:25:17 +010058 * table, storing the results in @geom.
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 *
Christoph Hellwiga10183d2020-03-24 08:25:17 +010060 * Returns: %false on failure, %true on success.
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 */
Christoph Hellwiga10183d2020-03-24 08:25:17 +010062bool scsi_partsize(struct block_device *bdev, sector_t capacity, int geom[3])
Linus Torvalds1da177e2005-04-16 15:20:36 -070063{
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 int cyl, ext_cyl, end_head, end_cyl, end_sector;
65 unsigned int logical_end, physical_end, ext_physical_end;
Christoph Hellwig1442f762020-03-24 08:25:26 +010066 struct msdos_partition *p, *largest = NULL;
Christoph Hellwiga10183d2020-03-24 08:25:17 +010067 void *buf;
68 int ret = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
Christoph Hellwiga10183d2020-03-24 08:25:17 +010070 buf = scsi_bios_ptable(bdev);
71 if (!buf)
72 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
74 if (*(unsigned short *) (buf + 64) == 0xAA55) {
Christoph Hellwiga10183d2020-03-24 08:25:17 +010075 int largest_cyl = -1, i;
76
77 for (i = 0, p = buf; i < 4; i++, p++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 if (!p->sys_ind)
79 continue;
80#ifdef DEBUG
81 printk("scsicam_bios_param : partition %d has system \n",
82 i);
83#endif
84 cyl = p->cyl + ((p->sector & 0xc0) << 2);
85 if (cyl > largest_cyl) {
86 largest_cyl = cyl;
87 largest = p;
88 }
89 }
90 }
91 if (largest) {
92 end_cyl = largest->end_cyl + ((largest->end_sector & 0xc0) << 2);
93 end_head = largest->end_head;
94 end_sector = largest->end_sector & 0x3f;
95
96 if (end_head + 1 == 0 || end_sector == 0)
Christoph Hellwiga10183d2020-03-24 08:25:17 +010097 goto out_free_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
99#ifdef DEBUG
100 printk("scsicam_bios_param : end at h = %d, c = %d, s = %d\n",
101 end_head, end_cyl, end_sector);
102#endif
103
104 physical_end = end_cyl * (end_head + 1) * end_sector +
105 end_head * end_sector + end_sector;
106
107 /* This is the actual _sector_ number at the end */
Christoph Hellwig678e2752014-10-01 20:31:01 +0200108 logical_end = get_unaligned_le32(&largest->start_sect)
109 + get_unaligned_le32(&largest->nr_sects);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110
111 /* This is for >1023 cylinders */
112 ext_cyl = (logical_end - (end_head * end_sector + end_sector))
113 / (end_head + 1) / end_sector;
114 ext_physical_end = ext_cyl * (end_head + 1) * end_sector +
115 end_head * end_sector + end_sector;
116
117#ifdef DEBUG
118 printk("scsicam_bios_param : logical_end=%d physical_end=%d ext_physical_end=%d ext_cyl=%d\n"
119 ,logical_end, physical_end, ext_physical_end, ext_cyl);
120#endif
121
Christoph Hellwiga10183d2020-03-24 08:25:17 +0100122 if (logical_end == physical_end ||
123 (end_cyl == 1023 && ext_physical_end == logical_end)) {
124 geom[0] = end_head + 1;
125 geom[1] = end_sector;
126 geom[2] = (unsigned long)capacity /
127 ((end_head + 1) * end_sector);
128 ret = true;
129 goto out_free_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 }
131#ifdef DEBUG
132 printk("scsicam_bios_param : logical (%u) != physical (%u)\n",
133 logical_end, physical_end);
134#endif
135 }
Christoph Hellwiga10183d2020-03-24 08:25:17 +0100136
137out_free_buf:
138 kfree(buf);
139 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140}
141EXPORT_SYMBOL(scsi_partsize);
142
143/*
144 * Function : static int setsize(unsigned long capacity,unsigned int *cyls,
145 * unsigned int *hds, unsigned int *secs);
146 *
147 * Purpose : to determine a near-optimal int 0x13 mapping for a
148 * SCSI disk in terms of lost space of size capacity, storing
149 * the results in *cyls, *hds, and *secs.
150 *
151 * Returns : -1 on failure, 0 on success.
152 *
153 * Extracted from
154 *
155 * WORKING X3T9.2
156 * DRAFT 792D
Rob Landleyeb448202007-11-03 13:30:39 -0500157 * see http://www.t10.org/ftp/t10/drafts/cam/cam-r12b.pdf
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 *
159 * Revision 6
160 * 10-MAR-94
161 * Information technology -
162 * SCSI-2 Common access method
163 * transport and SCSI interface module
164 *
165 * ANNEX A :
166 *
167 * setsize() converts a read capacity value to int 13h
168 * head-cylinder-sector requirements. It minimizes the value for
169 * number of heads and maximizes the number of cylinders. This
170 * will support rather large disks before the number of heads
171 * will not fit in 4 bits (or 6 bits). This algorithm also
172 * minimizes the number of sectors that will be unused at the end
173 * of the disk while allowing for very large disks to be
174 * accommodated. This algorithm does not use physical geometry.
175 */
176
177static int setsize(unsigned long capacity, unsigned int *cyls, unsigned int *hds,
178 unsigned int *secs)
179{
180 unsigned int rv = 0;
181 unsigned long heads, sectors, cylinders, temp;
182
183 cylinders = 1024L; /* Set number of cylinders to max */
184 sectors = 62L; /* Maximize sectors per track */
185
186 temp = cylinders * sectors; /* Compute divisor for heads */
187 heads = capacity / temp; /* Compute value for number of heads */
188 if (capacity % temp) { /* If no remainder, done! */
189 heads++; /* Else, increment number of heads */
190 temp = cylinders * heads; /* Compute divisor for sectors */
191 sectors = capacity / temp; /* Compute value for sectors per
192 track */
193 if (capacity % temp) { /* If no remainder, done! */
194 sectors++; /* Else, increment number of sectors */
195 temp = heads * sectors; /* Compute divisor for cylinders */
196 cylinders = capacity / temp; /* Compute number of cylinders */
197 }
198 }
199 if (cylinders == 0)
200 rv = (unsigned) -1; /* Give error if 0 cylinders */
201
202 *cyls = (unsigned int) cylinders; /* Stuff return values */
203 *secs = (unsigned int) sectors;
204 *hds = (unsigned int) heads;
205 return (rv);
206}
Christoph Hellwig26ae3532020-03-24 08:25:16 +0100207
208/**
209 * scsicam_bios_param - Determine geometry of a disk in cylinders/heads/sectors.
210 * @bdev: which device
211 * @capacity: size of the disk in sectors
212 * @ip: return value: ip[0]=heads, ip[1]=sectors, ip[2]=cylinders
213 *
214 * Description : determine the BIOS mapping/geometry used for a drive in a
215 * SCSI-CAM system, storing the results in ip as required
216 * by the HDIO_GETGEO ioctl().
217 *
218 * Returns : -1 on failure, 0 on success.
219 */
220int scsicam_bios_param(struct block_device *bdev, sector_t capacity, int *ip)
221{
Christoph Hellwig26ae3532020-03-24 08:25:16 +0100222 u64 capacity64 = capacity; /* Suppress gcc warning */
Christoph Hellwiga10183d2020-03-24 08:25:17 +0100223 int ret = 0;
Christoph Hellwig26ae3532020-03-24 08:25:16 +0100224
225 /* try to infer mapping from partition table */
Christoph Hellwiga10183d2020-03-24 08:25:17 +0100226 if (scsi_partsize(bdev, capacity, ip))
227 return 0;
Christoph Hellwig26ae3532020-03-24 08:25:16 +0100228
Christoph Hellwiga10183d2020-03-24 08:25:17 +0100229 if (capacity64 < (1ULL << 32)) {
Christoph Hellwig26ae3532020-03-24 08:25:16 +0100230 /*
231 * Pick some standard mapping with at most 1024 cylinders, and
232 * at most 62 sectors per track - this works up to 7905 MB.
233 */
234 ret = setsize((unsigned long)capacity, (unsigned int *)ip + 2,
235 (unsigned int *)ip + 0, (unsigned int *)ip + 1);
236 }
237
238 /*
239 * If something went wrong, then apparently we have to return a geometry
240 * with more than 1024 cylinders.
241 */
242 if (ret || ip[0] > 255 || ip[1] > 63) {
243 if ((capacity >> 11) > 65534) {
244 ip[0] = 255;
245 ip[1] = 63;
246 } else {
247 ip[0] = 64;
248 ip[1] = 32;
249 }
250
251 if (capacity > 65535*63*255)
252 ip[2] = 65535;
253 else
254 ip[2] = (unsigned long)capacity / (ip[0] * ip[1]);
255 }
256
257 return 0;
258}
259EXPORT_SYMBOL(scsicam_bios_param);