- Kai Germaschewski: ISDN update (including Makefiles)
[davej-history.git] / drivers / scsi / scsicam.c
blob9a7c32e23461271f72d23fc4a1b1ff2ba5db7e88
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"
24 #include <scsi/scsicam.h>
26 static int setsize(unsigned long capacity, unsigned int *cyls, unsigned int *hds,
27 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 int ma = MAJOR(dev);
51 int mi = (MINOR(dev) & ~0xf);
53 int block = 1024;
55 if(blksize_size[ma])
56 block = blksize_size[ma][mi];
58 if (!(bh = bread(MKDEV(ma,mi), 0, block)))
59 return -1;
61 /* try to infer mapping from partition table */
62 ret_code = scsi_partsize(bh, (unsigned long) size, (unsigned int *) ip + 2,
63 (unsigned int *) ip + 0, (unsigned int *) ip + 1);
64 brelse(bh);
66 if (ret_code == -1) {
67 /* pick some standard mapping with at most 1024 cylinders,
68 and at most 62 sectors per track - this works up to
69 7905 MB */
70 ret_code = setsize((unsigned long) size, (unsigned int *) ip + 2,
71 (unsigned int *) ip + 0, (unsigned int *) ip + 1);
73 /* if something went wrong, then apparently we have to return
74 a geometry with more than 1024 cylinders */
75 if (ret_code || ip[0] > 255 || ip[1] > 63) {
76 ip[0] = 64;
77 ip[1] = 32;
78 temp_cyl = size / (ip[0] * ip[1]);
79 if (temp_cyl > 65534) {
80 ip[0] = 255;
81 ip[1] = 63;
83 ip[2] = size / (ip[0] * ip[1]);
85 return 0;
89 * Function : static int scsi_partsize(struct buffer_head *bh, unsigned long
90 * capacity,unsigned int *cyls, unsigned int *hds, unsigned int *secs);
92 * Purpose : to determine the BIOS mapping used to create the partition
93 * table, storing the results in *cyls, *hds, and *secs
95 * Returns : -1 on failure, 0 on success.
99 int scsi_partsize(struct buffer_head *bh, unsigned long capacity,
100 unsigned int *cyls, unsigned int *hds, unsigned int *secs)
102 struct partition *p, *largest = NULL;
103 int i, largest_cyl;
104 int cyl, ext_cyl, end_head, end_cyl, end_sector;
105 unsigned int logical_end, physical_end, ext_physical_end;
108 if (*(unsigned short *) (bh->b_data + 510) == 0xAA55) {
109 for (largest_cyl = -1, p = (struct partition *)
110 (0x1BE + bh->b_data), i = 0; i < 4; ++i, ++p) {
111 if (!p->sys_ind)
112 continue;
113 #ifdef DEBUG
114 printk("scsicam_bios_param : partition %d has system \n",
116 #endif
117 cyl = p->cyl + ((p->sector & 0xc0) << 2);
118 if (cyl > largest_cyl) {
119 largest_cyl = cyl;
120 largest = p;
124 if (largest) {
125 end_cyl = largest->end_cyl + ((largest->end_sector & 0xc0) << 2);
126 end_head = largest->end_head;
127 end_sector = largest->end_sector & 0x3f;
129 if (end_head + 1 == 0 || end_sector == 0)
130 return -1;
132 #ifdef DEBUG
133 printk("scsicam_bios_param : end at h = %d, c = %d, s = %d\n",
134 end_head, end_cyl, end_sector);
135 #endif
137 physical_end = end_cyl * (end_head + 1) * end_sector +
138 end_head * end_sector + end_sector;
140 /* This is the actual _sector_ number at the end */
141 logical_end = get_unaligned(&largest->start_sect)
142 + get_unaligned(&largest->nr_sects);
144 /* This is for >1023 cylinders */
145 ext_cyl = (logical_end - (end_head * end_sector + end_sector))
146 / (end_head + 1) / end_sector;
147 ext_physical_end = ext_cyl * (end_head + 1) * end_sector +
148 end_head * end_sector + end_sector;
150 #ifdef DEBUG
151 printk("scsicam_bios_param : logical_end=%d physical_end=%d ext_physical_end=%d ext_cyl=%d\n"
152 ,logical_end, physical_end, ext_physical_end, ext_cyl);
153 #endif
155 if ((logical_end == physical_end) ||
156 (end_cyl == 1023 && ext_physical_end == logical_end)) {
157 *secs = end_sector;
158 *hds = end_head + 1;
159 *cyls = capacity / ((end_head + 1) * end_sector);
160 return 0;
162 #ifdef DEBUG
163 printk("scsicam_bios_param : logical (%u) != physical (%u)\n",
164 logical_end, physical_end);
165 #endif
167 return -1;
171 * Function : static int setsize(unsigned long capacity,unsigned int *cyls,
172 * unsigned int *hds, unsigned int *secs);
174 * Purpose : to determine a near-optimal int 0x13 mapping for a
175 * SCSI disk in terms of lost space of size capacity, storing
176 * the results in *cyls, *hds, and *secs.
178 * Returns : -1 on failure, 0 on success.
180 * Extracted from
182 * WORKING X3T9.2
183 * DRAFT 792D
186 * Revision 6
187 * 10-MAR-94
188 * Information technology -
189 * SCSI-2 Common access method
190 * transport and SCSI interface module
192 * ANNEX A :
194 * setsize() converts a read capacity value to int 13h
195 * head-cylinder-sector requirements. It minimizes the value for
196 * number of heads and maximizes the number of cylinders. This
197 * will support rather large disks before the number of heads
198 * will not fit in 4 bits (or 6 bits). This algorithm also
199 * minimizes the number of sectors that will be unused at the end
200 * of the disk while allowing for very large disks to be
201 * accommodated. This algorithm does not use physical geometry.
204 static int setsize(unsigned long capacity, unsigned int *cyls, unsigned int *hds,
205 unsigned int *secs)
207 unsigned int rv = 0;
208 unsigned long heads, sectors, cylinders, temp;
210 cylinders = 1024L; /* Set number of cylinders to max */
211 sectors = 62L; /* Maximize sectors per track */
213 temp = cylinders * sectors; /* Compute divisor for heads */
214 heads = capacity / temp; /* Compute value for number of heads */
215 if (capacity % temp) { /* If no remainder, done! */
216 heads++; /* Else, increment number of heads */
217 temp = cylinders * heads; /* Compute divisor for sectors */
218 sectors = capacity / temp; /* Compute value for sectors per
219 track */
220 if (capacity % temp) { /* If no remainder, done! */
221 sectors++; /* Else, increment number of sectors */
222 temp = heads * sectors; /* Compute divisor for cylinders */
223 cylinders = capacity / temp; /* Compute number of cylinders */
226 if (cylinders == 0)
227 rv = (unsigned) -1; /* Give error if 0 cylinders */
229 *cyls = (unsigned int) cylinders; /* Stuff return values */
230 *secs = (unsigned int) sectors;
231 *hds = (unsigned int) heads;
232 return (rv);