ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / drivers / scsi / scsicam.c
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
15 #include <linux/fs.h>
16 #include <linux/genhd.h>
17 #include <linux/kernel.h>
18 #include <linux/blkdev.h>
19 #include <linux/buffer_head.h>
20 #include <asm/unaligned.h>
21 #include "scsi.h"
22 #include "hosts.h"
23 #include <scsi/scsicam.h>
24
25 static int setsize(unsigned long capacity, unsigned int *cyls, unsigned int *hds,
26                    unsigned int *secs);
27
28 unsigned char *scsi_bios_ptable(struct block_device *dev)
29 {
30         unsigned char *res = kmalloc(66, GFP_KERNEL);
31         if (res) {
32                 struct block_device *bdev = dev->bd_contains;
33                 struct buffer_head *bh = __bread(bdev, 0, block_size(bdev));
34                 if (bh) {
35                         memcpy(res, bh->b_data + 0x1be, 66);
36                         brelse(bh);
37                 } else {
38                         kfree(res);
39                         res = NULL;
40                 }
41         }
42         return res;
43 }
44
45 /*
46  * Function : int scsicam_bios_param (struct block_device *bdev, ector_t capacity, int *ip)
47  *
48  * Purpose : to determine the BIOS mapping used for a drive in a 
49  *      SCSI-CAM system, storing the results in ip as required
50  *      by the HDIO_GETGEO ioctl().
51  *
52  * Returns : -1 on failure, 0 on success.
53  *
54  */
55
56 int scsicam_bios_param(struct block_device *bdev, sector_t capacity, int *ip)
57 {
58         unsigned char *p;
59         int ret;
60
61         p = scsi_bios_ptable(bdev);
62         if (!p)
63                 return -1;
64
65         /* try to infer mapping from partition table */
66         ret = scsi_partsize(p, (unsigned long)capacity, (unsigned int *)ip + 2,
67                                (unsigned int *)ip + 0, (unsigned int *)ip + 1);
68         kfree(p);
69
70         if (ret == -1) {
71                 /* pick some standard mapping with at most 1024 cylinders,
72                    and at most 62 sectors per track - this works up to
73                    7905 MB */
74                 ret = setsize((unsigned long)capacity, (unsigned int *)ip + 2,
75                        (unsigned int *)ip + 0, (unsigned int *)ip + 1);
76         }
77
78         /* if something went wrong, then apparently we have to return
79            a geometry with more than 1024 cylinders */
80         if (ret || ip[0] > 255 || ip[1] > 63) {
81                 if ((capacity >> 11) > 65534) {
82                         ip[0] = 255;
83                         ip[1] = 63;
84                 } else {
85                         ip[0] = 64;
86                         ip[1] = 32;
87                 }
88
89                 if (capacity > 65535*63*255)
90                         ip[2] = 65535;
91                 else
92                         ip[2] = (unsigned long)capacity / (ip[0] * ip[1]);
93         }
94
95         return 0;
96 }
97
98 /*
99  * Function : static int scsi_partsize(unsigned char *buf, unsigned long 
100  *     capacity,unsigned int *cyls, unsigned int *hds, unsigned int *secs);
101  *
102  * Purpose : to determine the BIOS mapping used to create the partition
103  *      table, storing the results in *cyls, *hds, and *secs 
104  *
105  * Returns : -1 on failure, 0 on success.
106  *
107  */
108
109 int scsi_partsize(unsigned char *buf, unsigned long capacity,
110                unsigned int *cyls, unsigned int *hds, unsigned int *secs)
111 {
112         struct partition *p = (struct partition *)buf, *largest = NULL;
113         int i, largest_cyl;
114         int cyl, ext_cyl, end_head, end_cyl, end_sector;
115         unsigned int logical_end, physical_end, ext_physical_end;
116
117
118         if (*(unsigned short *) (buf + 64) == 0xAA55) {
119                 for (largest_cyl = -1, i = 0; i < 4; ++i, ++p) {
120                         if (!p->sys_ind)
121                                 continue;
122 #ifdef DEBUG
123                         printk("scsicam_bios_param : partition %d has system \n",
124                                i);
125 #endif
126                         cyl = p->cyl + ((p->sector & 0xc0) << 2);
127                         if (cyl > largest_cyl) {
128                                 largest_cyl = cyl;
129                                 largest = p;
130                         }
131                 }
132         }
133         if (largest) {
134                 end_cyl = largest->end_cyl + ((largest->end_sector & 0xc0) << 2);
135                 end_head = largest->end_head;
136                 end_sector = largest->end_sector & 0x3f;
137
138                 if (end_head + 1 == 0 || end_sector == 0)
139                         return -1;
140
141 #ifdef DEBUG
142                 printk("scsicam_bios_param : end at h = %d, c = %d, s = %d\n",
143                        end_head, end_cyl, end_sector);
144 #endif
145
146                 physical_end = end_cyl * (end_head + 1) * end_sector +
147                     end_head * end_sector + end_sector;
148
149                 /* This is the actual _sector_ number at the end */
150                 logical_end = get_unaligned(&largest->start_sect)
151                     + get_unaligned(&largest->nr_sects);
152
153                 /* This is for >1023 cylinders */
154                 ext_cyl = (logical_end - (end_head * end_sector + end_sector))
155                     / (end_head + 1) / end_sector;
156                 ext_physical_end = ext_cyl * (end_head + 1) * end_sector +
157                     end_head * end_sector + end_sector;
158
159 #ifdef DEBUG
160                 printk("scsicam_bios_param : logical_end=%d physical_end=%d ext_physical_end=%d ext_cyl=%d\n"
161                   ,logical_end, physical_end, ext_physical_end, ext_cyl);
162 #endif
163
164                 if ((logical_end == physical_end) ||
165                   (end_cyl == 1023 && ext_physical_end == logical_end)) {
166                         *secs = end_sector;
167                         *hds = end_head + 1;
168                         *cyls = capacity / ((end_head + 1) * end_sector);
169                         return 0;
170                 }
171 #ifdef DEBUG
172                 printk("scsicam_bios_param : logical (%u) != physical (%u)\n",
173                        logical_end, physical_end);
174 #endif
175         }
176         return -1;
177 }
178
179 /*
180  * Function : static int setsize(unsigned long capacity,unsigned int *cyls,
181  *      unsigned int *hds, unsigned int *secs);
182  *
183  * Purpose : to determine a near-optimal int 0x13 mapping for a
184  *      SCSI disk in terms of lost space of size capacity, storing
185  *      the results in *cyls, *hds, and *secs.
186  *
187  * Returns : -1 on failure, 0 on success.
188  *
189  * Extracted from
190  *
191  * WORKING                                                    X3T9.2
192  * DRAFT                                                        792D
193  *
194  *
195  *                                                        Revision 6
196  *                                                         10-MAR-94
197  * Information technology -
198  * SCSI-2 Common access method
199  * transport and SCSI interface module
200  * 
201  * ANNEX A :
202  *
203  * setsize() converts a read capacity value to int 13h
204  * head-cylinder-sector requirements. It minimizes the value for
205  * number of heads and maximizes the number of cylinders. This
206  * will support rather large disks before the number of heads
207  * will not fit in 4 bits (or 6 bits). This algorithm also
208  * minimizes the number of sectors that will be unused at the end
209  * of the disk while allowing for very large disks to be
210  * accommodated. This algorithm does not use physical geometry. 
211  */
212
213 static int setsize(unsigned long capacity, unsigned int *cyls, unsigned int *hds,
214                    unsigned int *secs)
215 {
216         unsigned int rv = 0;
217         unsigned long heads, sectors, cylinders, temp;
218
219         cylinders = 1024L;      /* Set number of cylinders to max */
220         sectors = 62L;          /* Maximize sectors per track */
221
222         temp = cylinders * sectors;     /* Compute divisor for heads */
223         heads = capacity / temp;        /* Compute value for number of heads */
224         if (capacity % temp) {  /* If no remainder, done! */
225                 heads++;        /* Else, increment number of heads */
226                 temp = cylinders * heads;       /* Compute divisor for sectors */
227                 sectors = capacity / temp;      /* Compute value for sectors per
228                                                    track */
229                 if (capacity % temp) {  /* If no remainder, done! */
230                         sectors++;      /* Else, increment number of sectors */
231                         temp = heads * sectors;         /* Compute divisor for cylinders */
232                         cylinders = capacity / temp;    /* Compute number of cylinders */
233                 }
234         }
235         if (cylinders == 0)
236                 rv = (unsigned) -1;     /* Give error if 0 cylinders */
237
238         *cyls = (unsigned int) cylinders;       /* Stuff return values */
239         *secs = (unsigned int) sectors;
240         *hds = (unsigned int) heads;
241         return (rv);
242 }