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