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