pcmcia: CompactFlash driver for PA Semi Electra boards
[pv_ops_mirror.git] / drivers / scsi / scsicam.c
blobcd68a66c7bb3cef1d297d9fde33eadcfef967630
1 /*
2 * scsicam.c - SCSI CAM support functions, use for HDIO_GETGEO, etc.
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
10 * For more information, please consult the SCSI-CAM draft.
13 #include <linux/module.h>
14 #include <linux/fs.h>
15 #include <linux/genhd.h>
16 #include <linux/kernel.h>
17 #include <linux/blkdev.h>
18 #include <linux/buffer_head.h>
19 #include <asm/unaligned.h>
21 #include <scsi/scsicam.h>
24 static int setsize(unsigned long capacity, unsigned int *cyls, unsigned int *hds,
25 unsigned int *secs);
27 unsigned char *scsi_bios_ptable(struct block_device *dev)
29 unsigned char *res = kmalloc(66, GFP_KERNEL);
30 if (res) {
31 struct block_device *bdev = dev->bd_contains;
32 Sector sect;
33 void *data = read_dev_sector(bdev, 0, &sect);
34 if (data) {
35 memcpy(res, data + 0x1be, 66);
36 put_dev_sector(sect);
37 } else {
38 kfree(res);
39 res = NULL;
42 return res;
44 EXPORT_SYMBOL(scsi_bios_ptable);
47 * Function : int scsicam_bios_param (struct block_device *bdev, ector_t capacity, int *ip)
49 * Purpose : to determine the BIOS mapping used for a drive in a
50 * SCSI-CAM system, storing the results in ip as required
51 * by the HDIO_GETGEO ioctl().
53 * Returns : -1 on failure, 0 on success.
57 int scsicam_bios_param(struct block_device *bdev, sector_t capacity, int *ip)
59 unsigned char *p;
60 u64 capacity64 = capacity; /* Suppress gcc warning */
61 int ret;
63 p = scsi_bios_ptable(bdev);
64 if (!p)
65 return -1;
67 /* try to infer mapping from partition table */
68 ret = scsi_partsize(p, (unsigned long)capacity, (unsigned int *)ip + 2,
69 (unsigned int *)ip + 0, (unsigned int *)ip + 1);
70 kfree(p);
72 if (ret == -1 && capacity64 < (1ULL << 32)) {
73 /* pick some standard mapping with at most 1024 cylinders,
74 and at most 62 sectors per track - this works up to
75 7905 MB */
76 ret = setsize((unsigned long)capacity, (unsigned int *)ip + 2,
77 (unsigned int *)ip + 0, (unsigned int *)ip + 1);
80 /* if something went wrong, then apparently we have to return
81 a geometry with more than 1024 cylinders */
82 if (ret || ip[0] > 255 || ip[1] > 63) {
83 if ((capacity >> 11) > 65534) {
84 ip[0] = 255;
85 ip[1] = 63;
86 } else {
87 ip[0] = 64;
88 ip[1] = 32;
91 if (capacity > 65535*63*255)
92 ip[2] = 65535;
93 else
94 ip[2] = (unsigned long)capacity / (ip[0] * ip[1]);
97 return 0;
99 EXPORT_SYMBOL(scsicam_bios_param);
102 * Function : static int scsi_partsize(unsigned char *buf, unsigned long
103 * capacity,unsigned int *cyls, unsigned int *hds, unsigned int *secs);
105 * Purpose : to determine the BIOS mapping used to create the partition
106 * table, storing the results in *cyls, *hds, and *secs
108 * Returns : -1 on failure, 0 on success.
112 int scsi_partsize(unsigned char *buf, unsigned long capacity,
113 unsigned int *cyls, unsigned int *hds, unsigned int *secs)
115 struct partition *p = (struct partition *)buf, *largest = NULL;
116 int i, largest_cyl;
117 int cyl, ext_cyl, end_head, end_cyl, end_sector;
118 unsigned int logical_end, physical_end, ext_physical_end;
121 if (*(unsigned short *) (buf + 64) == 0xAA55) {
122 for (largest_cyl = -1, i = 0; i < 4; ++i, ++p) {
123 if (!p->sys_ind)
124 continue;
125 #ifdef DEBUG
126 printk("scsicam_bios_param : partition %d has system \n",
128 #endif
129 cyl = p->cyl + ((p->sector & 0xc0) << 2);
130 if (cyl > largest_cyl) {
131 largest_cyl = cyl;
132 largest = p;
136 if (largest) {
137 end_cyl = largest->end_cyl + ((largest->end_sector & 0xc0) << 2);
138 end_head = largest->end_head;
139 end_sector = largest->end_sector & 0x3f;
141 if (end_head + 1 == 0 || end_sector == 0)
142 return -1;
144 #ifdef DEBUG
145 printk("scsicam_bios_param : end at h = %d, c = %d, s = %d\n",
146 end_head, end_cyl, end_sector);
147 #endif
149 physical_end = end_cyl * (end_head + 1) * end_sector +
150 end_head * end_sector + end_sector;
152 /* This is the actual _sector_ number at the end */
153 logical_end = get_unaligned(&largest->start_sect)
154 + get_unaligned(&largest->nr_sects);
156 /* This is for >1023 cylinders */
157 ext_cyl = (logical_end - (end_head * end_sector + end_sector))
158 / (end_head + 1) / end_sector;
159 ext_physical_end = ext_cyl * (end_head + 1) * end_sector +
160 end_head * end_sector + end_sector;
162 #ifdef DEBUG
163 printk("scsicam_bios_param : logical_end=%d physical_end=%d ext_physical_end=%d ext_cyl=%d\n"
164 ,logical_end, physical_end, ext_physical_end, ext_cyl);
165 #endif
167 if ((logical_end == physical_end) ||
168 (end_cyl == 1023 && ext_physical_end == logical_end)) {
169 *secs = end_sector;
170 *hds = end_head + 1;
171 *cyls = capacity / ((end_head + 1) * end_sector);
172 return 0;
174 #ifdef DEBUG
175 printk("scsicam_bios_param : logical (%u) != physical (%u)\n",
176 logical_end, physical_end);
177 #endif
179 return -1;
181 EXPORT_SYMBOL(scsi_partsize);
184 * Function : static int setsize(unsigned long capacity,unsigned int *cyls,
185 * unsigned int *hds, unsigned int *secs);
187 * Purpose : to determine a near-optimal int 0x13 mapping for a
188 * SCSI disk in terms of lost space of size capacity, storing
189 * the results in *cyls, *hds, and *secs.
191 * Returns : -1 on failure, 0 on success.
193 * Extracted from
195 * WORKING X3T9.2
196 * DRAFT 792D
199 * Revision 6
200 * 10-MAR-94
201 * Information technology -
202 * SCSI-2 Common access method
203 * transport and SCSI interface module
205 * ANNEX A :
207 * setsize() converts a read capacity value to int 13h
208 * head-cylinder-sector requirements. It minimizes the value for
209 * number of heads and maximizes the number of cylinders. This
210 * will support rather large disks before the number of heads
211 * will not fit in 4 bits (or 6 bits). This algorithm also
212 * minimizes the number of sectors that will be unused at the end
213 * of the disk while allowing for very large disks to be
214 * accommodated. This algorithm does not use physical geometry.
217 static int setsize(unsigned long capacity, unsigned int *cyls, unsigned int *hds,
218 unsigned int *secs)
220 unsigned int rv = 0;
221 unsigned long heads, sectors, cylinders, temp;
223 cylinders = 1024L; /* Set number of cylinders to max */
224 sectors = 62L; /* Maximize sectors per track */
226 temp = cylinders * sectors; /* Compute divisor for heads */
227 heads = capacity / temp; /* Compute value for number of heads */
228 if (capacity % temp) { /* If no remainder, done! */
229 heads++; /* Else, increment number of heads */
230 temp = cylinders * heads; /* Compute divisor for sectors */
231 sectors = capacity / temp; /* Compute value for sectors per
232 track */
233 if (capacity % temp) { /* If no remainder, done! */
234 sectors++; /* Else, increment number of sectors */
235 temp = heads * sectors; /* Compute divisor for cylinders */
236 cylinders = capacity / temp; /* Compute number of cylinders */
239 if (cylinders == 0)
240 rv = (unsigned) -1; /* Give error if 0 cylinders */
242 *cyls = (unsigned int) cylinders; /* Stuff return values */
243 *secs = (unsigned int) sectors;
244 *hds = (unsigned int) heads;
245 return (rv);