add ahci support
[seabios.git] / src / block.c
1 // Disk setup and access
2 //
3 // Copyright (C) 2008,2009  Kevin O'Connor <kevin@koconnor.net>
4 // Copyright (C) 2002  MandrakeSoft S.A.
5 //
6 // This file may be distributed under the terms of the GNU LGPLv3 license.
7
8 #include "disk.h" // struct ata_s
9 #include "biosvar.h" // GET_GLOBAL
10 #include "cmos.h" // inb_cmos
11 #include "util.h" // dprintf
12 #include "ata.h" // process_ata_op
13 #include "ahci.h" // process_ahci_op
14 #include "usb-msc.h" // process_usb_op
15 #include "virtio-blk.h" // process_virtio_op
16
17 struct drives_s Drives VAR16VISIBLE;
18
19 struct drive_s *
20 getDrive(u8 exttype, u8 extdriveoffset)
21 {
22     if (extdriveoffset >= ARRAY_SIZE(Drives.idmap[0]))
23         return NULL;
24     struct drive_s *drive_gf = GET_GLOBAL(Drives.idmap[exttype][extdriveoffset]);
25     if (!drive_gf)
26         return NULL;
27     return GLOBALFLAT2GLOBAL(drive_gf);
28 }
29
30
31 /****************************************************************
32  * Disk geometry translation
33  ****************************************************************/
34
35 static u8
36 get_translation(struct drive_s *drive_g)
37 {
38     u8 type = GET_GLOBAL(drive_g->type);
39     if (! CONFIG_COREBOOT && type == DTYPE_ATA) {
40         // Emulators pass in the translation info via nvram.
41         u8 ataid = GET_GLOBAL(drive_g->cntl_id);
42         u8 channel = ataid / 2;
43         u8 translation = inb_cmos(CMOS_BIOS_DISKTRANSFLAG + channel/2);
44         translation >>= 2 * (ataid % 4);
45         translation &= 0x03;
46         return translation;
47     }
48
49     // Otherwise use a heuristic to determine translation type.
50     u16 heads = GET_GLOBAL(drive_g->pchs.heads);
51     u16 cylinders = GET_GLOBAL(drive_g->pchs.cylinders);
52     u16 spt = GET_GLOBAL(drive_g->pchs.spt);
53     u64 sectors = GET_GLOBAL(drive_g->sectors);
54     u64 psectors = (u64)heads * cylinders * spt;
55     if (!heads || !cylinders || !spt || psectors > sectors)
56         // pchs doesn't look valid - use LBA.
57         return TRANSLATION_LBA;
58
59     if (cylinders <= 1024 && heads <= 16 && spt <= 63)
60         return TRANSLATION_NONE;
61     if (cylinders * heads <= 131072)
62         return TRANSLATION_LARGE;
63     return TRANSLATION_LBA;
64 }
65
66 void
67 setup_translation(struct drive_s *drive_g)
68 {
69     u8 translation = get_translation(drive_g);
70     SET_GLOBAL(drive_g->translation, translation);
71
72     u16 heads = GET_GLOBAL(drive_g->pchs.heads);
73     u16 cylinders = GET_GLOBAL(drive_g->pchs.cylinders);
74     u16 spt = GET_GLOBAL(drive_g->pchs.spt);
75     u64 sectors = GET_GLOBAL(drive_g->sectors);
76     const char *desc = NULL;
77
78     switch (translation) {
79     default:
80     case TRANSLATION_NONE:
81         desc = "none";
82         break;
83     case TRANSLATION_LBA:
84         desc = "lba";
85         spt = 63;
86         if (sectors > 63*255*1024) {
87             heads = 255;
88             cylinders = 1024;
89             break;
90         }
91         u32 sect = (u32)sectors / 63;
92         heads = sect / 1024;
93         if (heads>128)
94             heads = 255;
95         else if (heads>64)
96             heads = 128;
97         else if (heads>32)
98             heads = 64;
99         else if (heads>16)
100             heads = 32;
101         else
102             heads = 16;
103         cylinders = sect / heads;
104         break;
105     case TRANSLATION_RECHS:
106         desc = "r-echs";
107         // Take care not to overflow
108         if (heads==16) {
109             if (cylinders>61439)
110                 cylinders=61439;
111             heads=15;
112             cylinders = (u16)((u32)(cylinders)*16/15);
113         }
114         // then go through the large bitshift process
115     case TRANSLATION_LARGE:
116         if (translation == TRANSLATION_LARGE)
117             desc = "large";
118         while (cylinders > 1024) {
119             cylinders >>= 1;
120             heads <<= 1;
121
122             // If we max out the head count
123             if (heads > 127)
124                 break;
125         }
126         break;
127     }
128     // clip to 1024 cylinders in lchs
129     if (cylinders > 1024)
130         cylinders = 1024;
131     dprintf(1, "drive %p: PCHS=%u/%d/%d translation=%s LCHS=%d/%d/%d s=%d\n"
132             , drive_g
133             , drive_g->pchs.cylinders, drive_g->pchs.heads, drive_g->pchs.spt
134             , desc
135             , cylinders, heads, spt
136             , (u32)sectors);
137
138     SET_GLOBAL(drive_g->lchs.heads, heads);
139     SET_GLOBAL(drive_g->lchs.cylinders, cylinders);
140     SET_GLOBAL(drive_g->lchs.spt, spt);
141 }
142
143
144 /****************************************************************
145  * Drive mapping
146  ****************************************************************/
147
148 // Fill in Fixed Disk Parameter Table (located in ebda).
149 static void
150 fill_fdpt(struct drive_s *drive_g, int hdid)
151 {
152     if (hdid > 1)
153         return;
154
155     u16 nlc   = GET_GLOBAL(drive_g->lchs.cylinders);
156     u16 nlh   = GET_GLOBAL(drive_g->lchs.heads);
157     u16 nlspt = GET_GLOBAL(drive_g->lchs.spt);
158
159     u16 npc   = GET_GLOBAL(drive_g->pchs.cylinders);
160     u16 nph   = GET_GLOBAL(drive_g->pchs.heads);
161     u16 npspt = GET_GLOBAL(drive_g->pchs.spt);
162
163     struct fdpt_s *fdpt = &get_ebda_ptr()->fdpt[hdid];
164     fdpt->precompensation = 0xffff;
165     fdpt->drive_control_byte = 0xc0 | ((nph > 8) << 3);
166     fdpt->landing_zone = npc;
167     fdpt->cylinders = nlc;
168     fdpt->heads = nlh;
169     fdpt->sectors = nlspt;
170
171     if (nlc != npc || nlh != nph || nlspt != npspt) {
172         // Logical mapping present - use extended structure.
173
174         // complies with Phoenix style Translated Fixed Disk Parameter
175         // Table (FDPT)
176         fdpt->phys_cylinders = npc;
177         fdpt->phys_heads = nph;
178         fdpt->phys_sectors = npspt;
179         fdpt->a0h_signature = 0xa0;
180
181         // Checksum structure.
182         fdpt->checksum -= checksum(fdpt, sizeof(*fdpt));
183     }
184
185     if (hdid == 0)
186         SET_IVT(0x41, SEGOFF(get_ebda_seg(), offsetof(
187                                  struct extended_bios_data_area_s, fdpt[0])));
188     else
189         SET_IVT(0x46, SEGOFF(get_ebda_seg(), offsetof(
190                                  struct extended_bios_data_area_s, fdpt[1])));
191 }
192
193 // Map a drive (that was registered via add_bcv_hd)
194 void
195 map_hd_drive(struct drive_s *drive_g)
196 {
197     // fill hdidmap
198     u8 hdcount = GET_BDA(hdcount);
199     if (hdcount >= ARRAY_SIZE(Drives.idmap[0])) {
200         warn_noalloc();
201         return;
202     }
203     dprintf(3, "Mapping hd drive %p to %d\n", drive_g, hdcount);
204     Drives.idmap[EXTTYPE_HD][hdcount] = drive_g;
205     SET_BDA(hdcount, hdcount + 1);
206
207     // Fill "fdpt" structure.
208     fill_fdpt(drive_g, hdcount);
209 }
210
211 // Find spot to add a drive
212 static void
213 add_ordered_drive(struct drive_s **idmap, u8 *count, struct drive_s *drive_g)
214 {
215     if (*count >= ARRAY_SIZE(Drives.idmap[0])) {
216         warn_noalloc();
217         return;
218     }
219     struct drive_s **pos = &idmap[*count];
220     *count = *count + 1;
221     if (CONFIG_THREADS) {
222         // Add to idmap with assured drive order.
223         struct drive_s **end = pos;
224         for (;;) {
225             struct drive_s **prev = pos - 1;
226             if (prev < idmap)
227                 break;
228             struct drive_s *prevdrive = *prev;
229             if (prevdrive->type < drive_g->type
230                 || (prevdrive->type == drive_g->type
231                     && prevdrive->cntl_id < drive_g->cntl_id))
232                 break;
233             pos--;
234         }
235         if (pos != end)
236             memmove(pos+1, pos, (void*)end-(void*)pos);
237     }
238     *pos = drive_g;
239 }
240
241 // Map a cd
242 void
243 map_cd_drive(struct drive_s *drive_g)
244 {
245     dprintf(3, "Mapping cd drive %p\n", drive_g);
246     add_ordered_drive(Drives.idmap[EXTTYPE_CD], &Drives.cdcount, drive_g);
247 }
248
249 // Map a floppy
250 void
251 map_floppy_drive(struct drive_s *drive_g)
252 {
253     // fill idmap
254     dprintf(3, "Mapping floppy drive %p\n", drive_g);
255     add_ordered_drive(Drives.idmap[EXTTYPE_FLOPPY], &Drives.floppycount
256                       , drive_g);
257
258     // Update equipment word bits for floppy
259     if (Drives.floppycount == 1) {
260         // 1 drive, ready for boot
261         SETBITS_BDA(equipment_list_flags, 0x01);
262         SET_BDA(floppy_harddisk_info, 0x07);
263     } else if (Drives.floppycount >= 2) {
264         // 2 drives, ready for boot
265         SETBITS_BDA(equipment_list_flags, 0x41);
266         SET_BDA(floppy_harddisk_info, 0x77);
267     }
268 }
269
270
271 /****************************************************************
272  * 16bit calling interface
273  ****************************************************************/
274
275 // Execute a disk_op request.
276 int
277 process_op(struct disk_op_s *op)
278 {
279     ASSERT16();
280     u8 type = GET_GLOBAL(op->drive_g->type);
281     switch (type) {
282     case DTYPE_FLOPPY:
283         return process_floppy_op(op);
284     case DTYPE_ATA:
285         return process_ata_op(op);
286     case DTYPE_ATAPI:
287         return process_atapi_op(op);
288     case DTYPE_RAMDISK:
289         return process_ramdisk_op(op);
290     case DTYPE_CDEMU:
291         return process_cdemu_op(op);
292     case DTYPE_USB:
293         return process_usb_op(op);
294     case DTYPE_VIRTIO:
295         return process_virtio_op(op);
296     case DTYPE_AHCI:
297         return process_ahci_op(op);
298     default:
299         op->count = 0;
300         return DISK_RET_EPARAM;
301     }
302 }
303
304 // Execute a "disk_op_s" request - this runs on a stack in the ebda.
305 static int
306 __send_disk_op(struct disk_op_s *op_far, u16 op_seg)
307 {
308     struct disk_op_s dop;
309     memcpy_far(GET_SEG(SS), &dop
310                , op_seg, op_far
311                , sizeof(dop));
312
313     dprintf(DEBUG_HDL_13, "disk_op d=%p lba=%d buf=%p count=%d cmd=%d\n"
314             , dop.drive_g, (u32)dop.lba, dop.buf_fl
315             , dop.count, dop.command);
316
317     int status = process_op(&dop);
318
319     // Update count with total sectors transferred.
320     SET_FARVAR(op_seg, op_far->count, dop.count);
321
322     return status;
323 }
324
325 // Execute a "disk_op_s" request by jumping to a stack in the ebda.
326 int
327 send_disk_op(struct disk_op_s *op)
328 {
329     ASSERT16();
330     if (! CONFIG_DRIVES)
331         return -1;
332
333     return stack_hop((u32)op, GET_SEG(SS), __send_disk_op);
334 }