Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
| 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 Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 15 | #include <linux/slab.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | #include <linux/fs.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | #include <linux/kernel.h> |
| 18 | #include <linux/blkdev.h> |
Matthew Wilcox (Oracle) | 4ee60ec | 2021-05-06 18:02:27 -0700 | [diff] [blame] | 19 | #include <linux/pagemap.h> |
Christoph Hellwig | 1442f76 | 2020-03-24 08:25:26 +0100 | [diff] [blame] | 20 | #include <linux/msdos_partition.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | #include <asm/unaligned.h> |
| 22 | |
| 23 | #include <scsi/scsicam.h> |
| 24 | |
Rob Landley | eb44820c | 2007-11-03 13:30:39 -0500 | [diff] [blame] | 25 | /** |
| 26 | * scsi_bios_ptable - Read PC partition table out of first sector of device. |
| 27 | * @dev: from this device |
| 28 | * |
| 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. |
| 32 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | unsigned char *scsi_bios_ptable(struct block_device *dev) |
| 34 | { |
Christoph Hellwig | a954ea8 | 2020-11-23 13:29:55 +0100 | [diff] [blame] | 35 | struct address_space *mapping = bdev_whole(dev)->bd_inode->i_mapping; |
Christoph Hellwig | e63105d | 2020-03-24 08:25:15 +0100 | [diff] [blame] | 36 | unsigned char *res = NULL; |
Matthew Wilcox (Oracle) | 5fb9bfe | 2022-02-09 12:58:22 -0500 | [diff] [blame] | 37 | struct folio *folio; |
Christoph Hellwig | e63105d | 2020-03-24 08:25:15 +0100 | [diff] [blame] | 38 | |
Matthew Wilcox (Oracle) | 5fb9bfe | 2022-02-09 12:58:22 -0500 | [diff] [blame] | 39 | folio = read_mapping_folio(mapping, 0, NULL); |
| 40 | if (IS_ERR(folio)) |
Christoph Hellwig | e63105d | 2020-03-24 08:25:15 +0100 | [diff] [blame] | 41 | return NULL; |
| 42 | |
Matthew Wilcox (Oracle) | 5fb9bfe | 2022-02-09 12:58:22 -0500 | [diff] [blame] | 43 | res = kmemdup(folio_address(folio) + 0x1be, 66, GFP_KERNEL); |
| 44 | folio_put(folio); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | return res; |
| 46 | } |
| 47 | EXPORT_SYMBOL(scsi_bios_ptable); |
| 48 | |
Rob Landley | eb44820c | 2007-11-03 13:30:39 -0500 | [diff] [blame] | 49 | /** |
Rob Landley | eb44820c | 2007-11-03 13:30:39 -0500 | [diff] [blame] | 50 | * scsi_partsize - Parse cylinders/heads/sectors from PC partition table |
Christoph Hellwig | a10183d | 2020-03-24 08:25:17 +0100 | [diff] [blame] | 51 | * @bdev: block device to parse |
Rob Landley | eb44820c | 2007-11-03 13:30:39 -0500 | [diff] [blame] | 52 | * @capacity: size of the disk in sectors |
Christoph Hellwig | a10183d | 2020-03-24 08:25:17 +0100 | [diff] [blame] | 53 | * @geom: output in form of [hds, cylinders, sectors] |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | * |
Mauro Carvalho Chehab | 739aca0 | 2017-05-12 10:04:14 -0300 | [diff] [blame] | 55 | * Determine the BIOS mapping/geometry used to create the partition |
Christoph Hellwig | a10183d | 2020-03-24 08:25:17 +0100 | [diff] [blame] | 56 | * table, storing the results in @geom. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | * |
Christoph Hellwig | a10183d | 2020-03-24 08:25:17 +0100 | [diff] [blame] | 58 | * Returns: %false on failure, %true on success. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | */ |
Christoph Hellwig | a10183d | 2020-03-24 08:25:17 +0100 | [diff] [blame] | 60 | bool scsi_partsize(struct block_device *bdev, sector_t capacity, int geom[3]) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | int cyl, ext_cyl, end_head, end_cyl, end_sector; |
| 63 | unsigned int logical_end, physical_end, ext_physical_end; |
Christoph Hellwig | 1442f76 | 2020-03-24 08:25:26 +0100 | [diff] [blame] | 64 | struct msdos_partition *p, *largest = NULL; |
Christoph Hellwig | a10183d | 2020-03-24 08:25:17 +0100 | [diff] [blame] | 65 | void *buf; |
| 66 | int ret = false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | |
Christoph Hellwig | a10183d | 2020-03-24 08:25:17 +0100 | [diff] [blame] | 68 | buf = scsi_bios_ptable(bdev); |
| 69 | if (!buf) |
| 70 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | |
| 72 | if (*(unsigned short *) (buf + 64) == 0xAA55) { |
Christoph Hellwig | a10183d | 2020-03-24 08:25:17 +0100 | [diff] [blame] | 73 | int largest_cyl = -1, i; |
| 74 | |
| 75 | for (i = 0, p = buf; i < 4; i++, p++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | if (!p->sys_ind) |
| 77 | continue; |
| 78 | #ifdef DEBUG |
| 79 | printk("scsicam_bios_param : partition %d has system \n", |
| 80 | i); |
| 81 | #endif |
| 82 | cyl = p->cyl + ((p->sector & 0xc0) << 2); |
| 83 | if (cyl > largest_cyl) { |
| 84 | largest_cyl = cyl; |
| 85 | largest = p; |
| 86 | } |
| 87 | } |
| 88 | } |
| 89 | if (largest) { |
| 90 | end_cyl = largest->end_cyl + ((largest->end_sector & 0xc0) << 2); |
| 91 | end_head = largest->end_head; |
| 92 | end_sector = largest->end_sector & 0x3f; |
| 93 | |
| 94 | if (end_head + 1 == 0 || end_sector == 0) |
Christoph Hellwig | a10183d | 2020-03-24 08:25:17 +0100 | [diff] [blame] | 95 | goto out_free_buf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | |
| 97 | #ifdef DEBUG |
| 98 | printk("scsicam_bios_param : end at h = %d, c = %d, s = %d\n", |
| 99 | end_head, end_cyl, end_sector); |
| 100 | #endif |
| 101 | |
| 102 | physical_end = end_cyl * (end_head + 1) * end_sector + |
| 103 | end_head * end_sector + end_sector; |
| 104 | |
| 105 | /* This is the actual _sector_ number at the end */ |
Christoph Hellwig | 678e275 | 2014-10-01 20:31:01 +0200 | [diff] [blame] | 106 | logical_end = get_unaligned_le32(&largest->start_sect) |
| 107 | + get_unaligned_le32(&largest->nr_sects); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | |
| 109 | /* This is for >1023 cylinders */ |
| 110 | ext_cyl = (logical_end - (end_head * end_sector + end_sector)) |
| 111 | / (end_head + 1) / end_sector; |
| 112 | ext_physical_end = ext_cyl * (end_head + 1) * end_sector + |
| 113 | end_head * end_sector + end_sector; |
| 114 | |
| 115 | #ifdef DEBUG |
| 116 | printk("scsicam_bios_param : logical_end=%d physical_end=%d ext_physical_end=%d ext_cyl=%d\n" |
| 117 | ,logical_end, physical_end, ext_physical_end, ext_cyl); |
| 118 | #endif |
| 119 | |
Christoph Hellwig | a10183d | 2020-03-24 08:25:17 +0100 | [diff] [blame] | 120 | if (logical_end == physical_end || |
| 121 | (end_cyl == 1023 && ext_physical_end == logical_end)) { |
| 122 | geom[0] = end_head + 1; |
| 123 | geom[1] = end_sector; |
| 124 | geom[2] = (unsigned long)capacity / |
| 125 | ((end_head + 1) * end_sector); |
| 126 | ret = true; |
| 127 | goto out_free_buf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | } |
| 129 | #ifdef DEBUG |
| 130 | printk("scsicam_bios_param : logical (%u) != physical (%u)\n", |
| 131 | logical_end, physical_end); |
| 132 | #endif |
| 133 | } |
Christoph Hellwig | a10183d | 2020-03-24 08:25:17 +0100 | [diff] [blame] | 134 | |
| 135 | out_free_buf: |
| 136 | kfree(buf); |
| 137 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | } |
| 139 | EXPORT_SYMBOL(scsi_partsize); |
| 140 | |
| 141 | /* |
| 142 | * Function : static int setsize(unsigned long capacity,unsigned int *cyls, |
| 143 | * unsigned int *hds, unsigned int *secs); |
| 144 | * |
| 145 | * Purpose : to determine a near-optimal int 0x13 mapping for a |
| 146 | * SCSI disk in terms of lost space of size capacity, storing |
| 147 | * the results in *cyls, *hds, and *secs. |
| 148 | * |
| 149 | * Returns : -1 on failure, 0 on success. |
| 150 | * |
| 151 | * Extracted from |
| 152 | * |
| 153 | * WORKING X3T9.2 |
| 154 | * DRAFT 792D |
Rob Landley | eb44820c | 2007-11-03 13:30:39 -0500 | [diff] [blame] | 155 | * see http://www.t10.org/ftp/t10/drafts/cam/cam-r12b.pdf |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 | * |
| 157 | * Revision 6 |
| 158 | * 10-MAR-94 |
| 159 | * Information technology - |
| 160 | * SCSI-2 Common access method |
| 161 | * transport and SCSI interface module |
| 162 | * |
| 163 | * ANNEX A : |
| 164 | * |
| 165 | * setsize() converts a read capacity value to int 13h |
| 166 | * head-cylinder-sector requirements. It minimizes the value for |
| 167 | * number of heads and maximizes the number of cylinders. This |
| 168 | * will support rather large disks before the number of heads |
| 169 | * will not fit in 4 bits (or 6 bits). This algorithm also |
| 170 | * minimizes the number of sectors that will be unused at the end |
| 171 | * of the disk while allowing for very large disks to be |
| 172 | * accommodated. This algorithm does not use physical geometry. |
| 173 | */ |
| 174 | |
| 175 | static int setsize(unsigned long capacity, unsigned int *cyls, unsigned int *hds, |
| 176 | unsigned int *secs) |
| 177 | { |
| 178 | unsigned int rv = 0; |
| 179 | unsigned long heads, sectors, cylinders, temp; |
| 180 | |
| 181 | cylinders = 1024L; /* Set number of cylinders to max */ |
| 182 | sectors = 62L; /* Maximize sectors per track */ |
| 183 | |
| 184 | temp = cylinders * sectors; /* Compute divisor for heads */ |
| 185 | heads = capacity / temp; /* Compute value for number of heads */ |
| 186 | if (capacity % temp) { /* If no remainder, done! */ |
| 187 | heads++; /* Else, increment number of heads */ |
| 188 | temp = cylinders * heads; /* Compute divisor for sectors */ |
| 189 | sectors = capacity / temp; /* Compute value for sectors per |
| 190 | track */ |
| 191 | if (capacity % temp) { /* If no remainder, done! */ |
| 192 | sectors++; /* Else, increment number of sectors */ |
| 193 | temp = heads * sectors; /* Compute divisor for cylinders */ |
| 194 | cylinders = capacity / temp; /* Compute number of cylinders */ |
| 195 | } |
| 196 | } |
| 197 | if (cylinders == 0) |
| 198 | rv = (unsigned) -1; /* Give error if 0 cylinders */ |
| 199 | |
| 200 | *cyls = (unsigned int) cylinders; /* Stuff return values */ |
| 201 | *secs = (unsigned int) sectors; |
| 202 | *hds = (unsigned int) heads; |
| 203 | return (rv); |
| 204 | } |
Christoph Hellwig | 26ae353 | 2020-03-24 08:25:16 +0100 | [diff] [blame] | 205 | |
| 206 | /** |
| 207 | * scsicam_bios_param - Determine geometry of a disk in cylinders/heads/sectors. |
| 208 | * @bdev: which device |
| 209 | * @capacity: size of the disk in sectors |
| 210 | * @ip: return value: ip[0]=heads, ip[1]=sectors, ip[2]=cylinders |
| 211 | * |
| 212 | * Description : determine the BIOS mapping/geometry used for a drive in a |
| 213 | * SCSI-CAM system, storing the results in ip as required |
| 214 | * by the HDIO_GETGEO ioctl(). |
| 215 | * |
| 216 | * Returns : -1 on failure, 0 on success. |
| 217 | */ |
| 218 | int scsicam_bios_param(struct block_device *bdev, sector_t capacity, int *ip) |
| 219 | { |
Christoph Hellwig | 26ae353 | 2020-03-24 08:25:16 +0100 | [diff] [blame] | 220 | u64 capacity64 = capacity; /* Suppress gcc warning */ |
Christoph Hellwig | a10183d | 2020-03-24 08:25:17 +0100 | [diff] [blame] | 221 | int ret = 0; |
Christoph Hellwig | 26ae353 | 2020-03-24 08:25:16 +0100 | [diff] [blame] | 222 | |
| 223 | /* try to infer mapping from partition table */ |
Christoph Hellwig | a10183d | 2020-03-24 08:25:17 +0100 | [diff] [blame] | 224 | if (scsi_partsize(bdev, capacity, ip)) |
| 225 | return 0; |
Christoph Hellwig | 26ae353 | 2020-03-24 08:25:16 +0100 | [diff] [blame] | 226 | |
Christoph Hellwig | a10183d | 2020-03-24 08:25:17 +0100 | [diff] [blame] | 227 | if (capacity64 < (1ULL << 32)) { |
Christoph Hellwig | 26ae353 | 2020-03-24 08:25:16 +0100 | [diff] [blame] | 228 | /* |
| 229 | * Pick some standard mapping with at most 1024 cylinders, and |
| 230 | * at most 62 sectors per track - this works up to 7905 MB. |
| 231 | */ |
| 232 | ret = setsize((unsigned long)capacity, (unsigned int *)ip + 2, |
| 233 | (unsigned int *)ip + 0, (unsigned int *)ip + 1); |
| 234 | } |
| 235 | |
| 236 | /* |
| 237 | * If something went wrong, then apparently we have to return a geometry |
| 238 | * with more than 1024 cylinders. |
| 239 | */ |
| 240 | if (ret || ip[0] > 255 || ip[1] > 63) { |
| 241 | if ((capacity >> 11) > 65534) { |
| 242 | ip[0] = 255; |
| 243 | ip[1] = 63; |
| 244 | } else { |
| 245 | ip[0] = 64; |
| 246 | ip[1] = 32; |
| 247 | } |
| 248 | |
| 249 | if (capacity > 65535*63*255) |
| 250 | ip[2] = 65535; |
| 251 | else |
| 252 | ip[2] = (unsigned long)capacity / (ip[0] * ip[1]); |
| 253 | } |
| 254 | |
| 255 | return 0; |
| 256 | } |
| 257 | EXPORT_SYMBOL(scsicam_bios_param); |