Import 2.2.8pre2
[davej-history.git] / drivers / scsi / scsicam.c
blob9aa0d8fbc1caa2bd08cf14e14c6b8c40887b1d5f
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 #define __NO_VERSION__
14 #include <linux/module.h>
16 #include <linux/fs.h>
17 #include <linux/genhd.h>
18 #include <linux/kernel.h>
19 #include <linux/blk.h>
20 #include <asm/unaligned.h>
21 #include "scsi.h"
22 #include "hosts.h"
23 #include "sd.h"
25 static int partsize(struct buffer_head *bh, unsigned long capacity,
26 unsigned int *cyls, unsigned int *hds, unsigned int *secs);
27 static int setsize(unsigned long capacity,unsigned int *cyls,unsigned int *hds,
28 unsigned int *secs);
31 * Function : int scsicam_bios_param (Disk *disk, int dev, int *ip)
33 * Purpose : to determine the BIOS mapping used for a drive in a
34 * SCSI-CAM system, storing the results in ip as required
35 * by the HDIO_GETGEO ioctl().
37 * Returns : -1 on failure, 0 on success.
41 int scsicam_bios_param (Disk *disk, /* SCSI disk */
42 kdev_t dev, /* Device major, minor */
43 int *ip /* Heads, sectors, cylinders in that order */) {
45 struct buffer_head *bh;
46 int ret_code;
47 int size = disk->capacity;
48 unsigned long temp_cyl;
50 if (!(bh = bread(MKDEV(MAJOR(dev), MINOR(dev)&~0xf), 0, 1024)))
51 return -1;
53 /* try to infer mapping from partition table */
54 ret_code = partsize (bh, (unsigned long) size, (unsigned int *) ip + 2,
55 (unsigned int *) ip + 0, (unsigned int *) ip + 1);
56 brelse (bh);
58 if (ret_code == -1) {
59 /* pick some standard mapping with at most 1024 cylinders,
60 and at most 62 sectors per track - this works up to
61 7905 MB */
62 ret_code = setsize ((unsigned long) size, (unsigned int *) ip + 2,
63 (unsigned int *) ip + 0, (unsigned int *) ip + 1);
66 /* if something went wrong, then apparently we have to return
67 a geometry with more than 1024 cylinders */
68 if (ret_code || ip[0] > 255 || ip[1] > 63) {
69 ip[0] = 64;
70 ip[1] = 32;
71 temp_cyl = size / (ip[0] * ip[1]);
72 if (temp_cyl > 65534) {
73 ip[0] = 255;
74 ip[1] = 63;
76 ip[2] = size / (ip[0] * ip[1]);
79 return 0;
83 * Function : static int partsize(struct buffer_head *bh, unsigned long
84 * capacity,unsigned int *cyls, unsigned int *hds, unsigned int *secs);
86 * Purpose : to determine the BIOS mapping used to create the partition
87 * table, storing the results in *cyls, *hds, and *secs
89 * Returns : -1 on failure, 0 on success.
93 static int partsize(struct buffer_head *bh, unsigned long capacity,
94 unsigned int *cyls, unsigned int *hds, unsigned int *secs) {
95 struct partition *p, *largest = NULL;
96 int i, largest_cyl;
97 int cyl, ext_cyl, end_head, end_cyl, end_sector;
98 unsigned int logical_end, physical_end, ext_physical_end;
101 if (*(unsigned short *) (bh->b_data+510) == 0xAA55) {
102 for (largest_cyl = -1, p = (struct partition *)
103 (0x1BE + bh->b_data), i = 0; i < 4; ++i, ++p) {
104 if (!p->sys_ind)
105 continue;
106 #ifdef DEBUG
107 printk ("scsicam_bios_param : partition %d has system \n",
109 #endif
110 cyl = p->cyl + ((p->sector & 0xc0) << 2);
111 if (cyl > largest_cyl) {
112 largest_cyl = cyl;
113 largest = p;
118 if (largest) {
119 end_cyl = largest->end_cyl + ((largest->end_sector & 0xc0) << 2);
120 end_head = largest->end_head;
121 end_sector = largest->end_sector & 0x3f;
123 if( end_head + 1 == 0 || end_sector == 0 ) return -1;
125 #ifdef DEBUG
126 printk ("scsicam_bios_param : end at h = %d, c = %d, s = %d\n",
127 end_head, end_cyl, end_sector);
128 #endif
130 physical_end = end_cyl * (end_head + 1) * end_sector +
131 end_head * end_sector + end_sector;
133 /* This is the actual _sector_ number at the end */
134 logical_end = get_unaligned(&largest->start_sect)
135 + get_unaligned(&largest->nr_sects);
137 /* This is for >1023 cylinders */
138 ext_cyl= (logical_end-(end_head * end_sector + end_sector))
139 /(end_head + 1) / end_sector;
140 ext_physical_end = ext_cyl * (end_head + 1) * end_sector +
141 end_head * end_sector + end_sector;
143 #ifdef DEBUG
144 printk("scsicam_bios_param : logical_end=%d physical_end=%d ext_physical_end=%d ext_cyl=%d\n"
145 ,logical_end,physical_end,ext_physical_end,ext_cyl);
146 #endif
148 if ((logical_end == physical_end) ||
149 (end_cyl==1023 && ext_physical_end==logical_end)) {
150 *secs = end_sector;
151 *hds = end_head + 1;
152 *cyls = capacity / ((end_head + 1) * end_sector);
153 return 0;
156 #ifdef DEBUG
157 printk ("scsicam_bios_param : logical (%u) != physical (%u)\n",
158 logical_end, physical_end);
159 #endif
161 return -1;
165 * Function : static int setsize(unsigned long capacity,unsigned int *cyls,
166 * unsigned int *hds, unsigned int *secs);
168 * Purpose : to determine a near-optimal int 0x13 mapping for a
169 * SCSI disk in terms of lost space of size capacity, storing
170 * the results in *cyls, *hds, and *secs.
172 * Returns : -1 on failure, 0 on success.
174 * Extracted from
176 * WORKING X3T9.2
177 * DRAFT 792D
180 * Revision 6
181 * 10-MAR-94
182 * Information technology -
183 * SCSI-2 Common access method
184 * transport and SCSI interface module
186 * ANNEX A :
188 * setsize() converts a read capacity value to int 13h
189 * head-cylinder-sector requirements. It minimizes the value for
190 * number of heads and maximizes the number of cylinders. This
191 * will support rather large disks before the number of heads
192 * will not fit in 4 bits (or 6 bits). This algorithm also
193 * minimizes the number of sectors that will be unused at the end
194 * of the disk while allowing for very large disks to be
195 * accommodated. This algorithm does not use physical geometry.
198 static int setsize(unsigned long capacity,unsigned int *cyls,unsigned int *hds,
199 unsigned int *secs) {
200 unsigned int rv = 0;
201 unsigned long heads, sectors, cylinders, temp;
203 cylinders = 1024L; /* Set number of cylinders to max */
204 sectors = 62L; /* Maximize sectors per track */
206 temp = cylinders * sectors; /* Compute divisor for heads */
207 heads = capacity / temp; /* Compute value for number of heads */
208 if (capacity % temp) { /* If no remainder, done! */
209 heads++; /* Else, increment number of heads */
210 temp = cylinders * heads; /* Compute divisor for sectors */
211 sectors = capacity / temp; /* Compute value for sectors per
212 track */
213 if (capacity % temp) { /* If no remainder, done! */
214 sectors++; /* Else, increment number of sectors */
215 temp = heads * sectors; /* Compute divisor for cylinders */
216 cylinders = capacity / temp;/* Compute number of cylinders */
219 if (cylinders == 0) rv=(unsigned)-1;/* Give error if 0 cylinders */
221 *cyls = (unsigned int) cylinders; /* Stuff return values */
222 *secs = (unsigned int) sectors;
223 *hds = (unsigned int) heads;
224 return(rv);