Linux Audio

Check our new training course

Linux kernel drivers training

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