Add qemu 2.4.0
[kvmfornfv.git] / qemu / roms / seabios / 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 "biosvar.h" // GET_GLOBAL
9 #include "block.h" // process_op
10 #include "hw/ata.h" // process_ata_op
11 #include "hw/ahci.h" // process_ahci_op
12 #include "hw/blockcmd.h" // cdb_*
13 #include "hw/pci.h" // pci_bdf_to_bus
14 #include "hw/rtc.h" // rtc_read
15 #include "hw/virtio-blk.h" // process_virtio_blk_op
16 #include "malloc.h" // malloc_low
17 #include "output.h" // dprintf
18 #include "stacks.h" // stack_hop
19 #include "std/disk.h" // struct dpte_s
20 #include "string.h" // checksum
21 #include "util.h" // process_floppy_op
22
23 u8 FloppyCount VARFSEG;
24 u8 CDCount;
25 struct drive_s *IDMap[3][BUILD_MAX_EXTDRIVE] VARFSEG;
26 u8 *bounce_buf_fl VARFSEG;
27
28 struct drive_s *
29 getDrive(u8 exttype, u8 extdriveoffset)
30 {
31     if (extdriveoffset >= ARRAY_SIZE(IDMap[0]))
32         return NULL;
33     return GET_GLOBAL(IDMap[exttype][extdriveoffset]);
34 }
35
36 int getDriveId(u8 exttype, struct drive_s *drive)
37 {
38     ASSERT32FLAT();
39     int i;
40     for (i = 0; i < ARRAY_SIZE(IDMap[0]); i++)
41         if (getDrive(exttype, i) == drive)
42             return i;
43     return -1;
44 }
45
46 int create_bounce_buf(void)
47 {
48     if (bounce_buf_fl)
49         return 0;
50
51     u8 *buf = malloc_low(CDROM_SECTOR_SIZE);
52     if (!buf) {
53         warn_noalloc();
54         return -1;
55     }
56     bounce_buf_fl = buf;
57     return 0;
58 }
59
60 /****************************************************************
61  * Disk geometry translation
62  ****************************************************************/
63
64 static u8
65 get_translation(struct drive_s *drive)
66 {
67     u8 type = drive->type;
68     if (CONFIG_QEMU && type == DTYPE_ATA) {
69         // Emulators pass in the translation info via nvram.
70         u8 ataid = drive->cntl_id;
71         u8 channel = ataid / 2;
72         u8 translation = rtc_read(CMOS_BIOS_DISKTRANSFLAG + channel/2);
73         translation >>= 2 * (ataid % 4);
74         translation &= 0x03;
75         return translation;
76     }
77
78     // Otherwise use a heuristic to determine translation type.
79     u16 heads = drive->pchs.head;
80     u16 cylinders = drive->pchs.cylinder;
81     u16 spt = drive->pchs.sector;
82     u64 sectors = drive->sectors;
83     u64 psectors = (u64)heads * cylinders * spt;
84     if (!heads || !cylinders || !spt || psectors > sectors)
85         // pchs doesn't look valid - use LBA.
86         return TRANSLATION_LBA;
87
88     if (cylinders <= 1024 && heads <= 16 && spt <= 63)
89         return TRANSLATION_NONE;
90     if (cylinders * heads <= 131072)
91         return TRANSLATION_LARGE;
92     return TRANSLATION_LBA;
93 }
94
95 static void
96 setup_translation(struct drive_s *drive)
97 {
98     u8 translation = get_translation(drive);
99     drive->translation = translation;
100
101     u16 heads = drive->pchs.head ;
102     u16 cylinders = drive->pchs.cylinder;
103     u16 spt = drive->pchs.sector;
104     u64 sectors = drive->sectors;
105     const char *desc = NULL;
106
107     switch (translation) {
108     default:
109     case TRANSLATION_NONE:
110         desc = "none";
111         break;
112     case TRANSLATION_LBA:
113         desc = "lba";
114         spt = 63;
115         if (sectors > 63*255*1024) {
116             heads = 255;
117             cylinders = 1024;
118             break;
119         }
120         u32 sect = (u32)sectors / 63;
121         heads = sect / 1024;
122         if (heads>128)
123             heads = 255;
124         else if (heads>64)
125             heads = 128;
126         else if (heads>32)
127             heads = 64;
128         else if (heads>16)
129             heads = 32;
130         else
131             heads = 16;
132         cylinders = sect / heads;
133         break;
134     case TRANSLATION_RECHS:
135         desc = "r-echs";
136         // Take care not to overflow
137         if (heads==16) {
138             if (cylinders>61439)
139                 cylinders=61439;
140             heads=15;
141             cylinders = (u16)((u32)(cylinders)*16/15);
142         }
143         // then go through the large bitshift process
144     case TRANSLATION_LARGE:
145         if (translation == TRANSLATION_LARGE)
146             desc = "large";
147         while (cylinders > 1024) {
148             cylinders >>= 1;
149             heads <<= 1;
150
151             // If we max out the head count
152             if (heads > 127)
153                 break;
154         }
155         break;
156     }
157     // clip to 1024 cylinders in lchs
158     if (cylinders > 1024)
159         cylinders = 1024;
160     dprintf(1, "drive %p: PCHS=%u/%d/%d translation=%s LCHS=%d/%d/%d s=%d\n"
161             , drive
162             , drive->pchs.cylinder, drive->pchs.head, drive->pchs.sector
163             , desc
164             , cylinders, heads, spt
165             , (u32)sectors);
166
167     drive->lchs.head = heads;
168     drive->lchs.cylinder = cylinders;
169     drive->lchs.sector = spt;
170 }
171
172
173 /****************************************************************
174  * Drive mapping
175  ****************************************************************/
176
177 // Fill in Fixed Disk Parameter Table (located in ebda).
178 static void
179 fill_fdpt(struct drive_s *drive, int hdid)
180 {
181     if (hdid > 1)
182         return;
183
184     u16 nlc = drive->lchs.cylinder;
185     u16 nlh = drive->lchs.head;
186     u16 nls = drive->lchs.sector;
187
188     u16 npc = drive->pchs.cylinder;
189     u16 nph = drive->pchs.head;
190     u16 nps = drive->pchs.sector;
191
192     struct fdpt_s *fdpt = &get_ebda_ptr()->fdpt[hdid];
193     fdpt->precompensation = 0xffff;
194     fdpt->drive_control_byte = 0xc0 | ((nph > 8) << 3);
195     fdpt->landing_zone = npc;
196     fdpt->cylinders = nlc;
197     fdpt->heads = nlh;
198     fdpt->sectors = nls;
199
200     if (nlc != npc || nlh != nph || nls != nps) {
201         // Logical mapping present - use extended structure.
202
203         // complies with Phoenix style Translated Fixed Disk Parameter
204         // Table (FDPT)
205         fdpt->phys_cylinders = npc;
206         fdpt->phys_heads = nph;
207         fdpt->phys_sectors = nps;
208         fdpt->a0h_signature = 0xa0;
209
210         // Checksum structure.
211         fdpt->checksum -= checksum(fdpt, sizeof(*fdpt));
212     }
213
214     if (hdid == 0)
215         SET_IVT(0x41, SEGOFF(get_ebda_seg(), offsetof(
216                                  struct extended_bios_data_area_s, fdpt[0])));
217     else
218         SET_IVT(0x46, SEGOFF(get_ebda_seg(), offsetof(
219                                  struct extended_bios_data_area_s, fdpt[1])));
220 }
221
222 // Find spot to add a drive
223 static void
224 add_drive(struct drive_s **idmap, u8 *count, struct drive_s *drive)
225 {
226     if (*count >= ARRAY_SIZE(IDMap[0])) {
227         warn_noalloc();
228         return;
229     }
230     idmap[*count] = drive;
231     *count = *count + 1;
232 }
233
234 // Map a hard drive
235 void
236 map_hd_drive(struct drive_s *drive)
237 {
238     ASSERT32FLAT();
239     struct bios_data_area_s *bda = MAKE_FLATPTR(SEG_BDA, 0);
240     int hdid = bda->hdcount;
241     dprintf(3, "Mapping hd drive %p to %d\n", drive, hdid);
242     add_drive(IDMap[EXTTYPE_HD], &bda->hdcount, drive);
243
244     // Setup disk geometry translation.
245     setup_translation(drive);
246
247     // Fill "fdpt" structure.
248     fill_fdpt(drive, hdid);
249 }
250
251 // Map a cd
252 void
253 map_cd_drive(struct drive_s *drive)
254 {
255     ASSERT32FLAT();
256     dprintf(3, "Mapping cd drive %p\n", drive);
257     add_drive(IDMap[EXTTYPE_CD], &CDCount, drive);
258 }
259
260 // Map a floppy
261 void
262 map_floppy_drive(struct drive_s *drive)
263 {
264     ASSERT32FLAT();
265     dprintf(3, "Mapping floppy drive %p\n", drive);
266     add_drive(IDMap[EXTTYPE_FLOPPY], &FloppyCount, drive);
267
268     // Update equipment word bits for floppy
269     if (FloppyCount == 1) {
270         // 1 drive, ready for boot
271         set_equipment_flags(0x41, 0x01);
272         SET_BDA(floppy_harddisk_info, 0x07);
273     } else if (FloppyCount >= 2) {
274         // 2 drives, ready for boot
275         set_equipment_flags(0x41, 0x41);
276         SET_BDA(floppy_harddisk_info, 0x77);
277     }
278 }
279
280
281 /****************************************************************
282  * Extended Disk Drive (EDD) get drive parameters
283  ****************************************************************/
284
285 static int
286 fill_generic_edd(u16 seg, struct int13dpt_s *param_far, struct drive_s *drive_gf
287                  , u32 dpte_so, char *iface_type
288                  , int bdf, u8 channel, u16 iobase, u64 device_path)
289 {
290     u16 size = GET_FARVAR(seg, param_far->size);
291     u16 t13 = size == 74;
292
293     // Buffer is too small
294     if (size < 26)
295         return DISK_RET_EPARAM;
296
297     // EDD 1.x
298
299     u8  type    = GET_GLOBALFLAT(drive_gf->type);
300     u16 npc     = GET_GLOBALFLAT(drive_gf->pchs.cylinder);
301     u16 nph     = GET_GLOBALFLAT(drive_gf->pchs.head);
302     u16 nps     = GET_GLOBALFLAT(drive_gf->pchs.sector);
303     u64 lba     = GET_GLOBALFLAT(drive_gf->sectors);
304     u16 blksize = GET_GLOBALFLAT(drive_gf->blksize);
305
306     dprintf(DEBUG_HDL_13, "disk_1348 size=%d t=%d chs=%d,%d,%d lba=%d bs=%d\n"
307             , size, type, npc, nph, nps, (u32)lba, blksize);
308
309     SET_FARVAR(seg, param_far->size, 26);
310     if (lba == (u64)-1) {
311         // 0x74 = removable, media change, lockable, max values
312         SET_FARVAR(seg, param_far->infos, 0x74);
313         SET_FARVAR(seg, param_far->cylinders, 0xffffffff);
314         SET_FARVAR(seg, param_far->heads, 0xffffffff);
315         SET_FARVAR(seg, param_far->spt, 0xffffffff);
316     } else {
317         if (lba > (u64)nps*nph*0x3fff) {
318             SET_FARVAR(seg, param_far->infos, 0x00); // geometry is invalid
319             SET_FARVAR(seg, param_far->cylinders, 0x3fff);
320         } else {
321             SET_FARVAR(seg, param_far->infos, 0x02); // geometry is valid
322             SET_FARVAR(seg, param_far->cylinders, (u32)npc);
323         }
324         SET_FARVAR(seg, param_far->heads, (u32)nph);
325         SET_FARVAR(seg, param_far->spt, (u32)nps);
326     }
327     SET_FARVAR(seg, param_far->sector_count, lba);
328     SET_FARVAR(seg, param_far->blksize, blksize);
329
330     if (size < 30 || !dpte_so)
331         return DISK_RET_SUCCESS;
332
333     // EDD 2.x
334
335     SET_FARVAR(seg, param_far->size, 30);
336     SET_FARVAR(seg, param_far->dpte.segoff, dpte_so);
337
338     if (size < 66 || !iface_type)
339         return DISK_RET_SUCCESS;
340
341     // EDD 3.x
342     SET_FARVAR(seg, param_far->key, 0xbedd);
343     SET_FARVAR(seg, param_far->dpi_length, t13 ? 44 : 36);
344     SET_FARVAR(seg, param_far->reserved1, 0);
345     SET_FARVAR(seg, param_far->reserved2, 0);
346
347     int i;
348     for (i=0; i<sizeof(param_far->iface_type); i++)
349         SET_FARVAR(seg, param_far->iface_type[i], GET_GLOBAL(iface_type[i]));
350
351     if (bdf != -1) {
352         SET_FARVAR(seg, param_far->host_bus[0], 'P');
353         SET_FARVAR(seg, param_far->host_bus[1], 'C');
354         SET_FARVAR(seg, param_far->host_bus[2], 'I');
355         SET_FARVAR(seg, param_far->host_bus[3], ' ');
356
357         u32 path = (pci_bdf_to_bus(bdf) | (pci_bdf_to_dev(bdf) << 8)
358                     | (pci_bdf_to_fn(bdf) << 16));
359         if (t13)
360             path |= channel << 24;
361
362         SET_FARVAR(seg, param_far->iface_path, path);
363     } else {
364         // ISA
365         SET_FARVAR(seg, param_far->host_bus[0], 'I');
366         SET_FARVAR(seg, param_far->host_bus[1], 'S');
367         SET_FARVAR(seg, param_far->host_bus[2], 'A');
368         SET_FARVAR(seg, param_far->host_bus[3], ' ');
369
370         SET_FARVAR(seg, param_far->iface_path, iobase);
371     }
372
373     if (t13) {
374         SET_FARVAR(seg, param_far->t13.device_path[0], device_path);
375         SET_FARVAR(seg, param_far->t13.device_path[1], 0);
376
377         SET_FARVAR(seg, param_far->t13.checksum
378                    , -checksum_far(seg, (void*)param_far+30, 43));
379     } else {
380         SET_FARVAR(seg, param_far->phoenix.device_path, device_path);
381
382         SET_FARVAR(seg, param_far->phoenix.checksum
383                    , -checksum_far(seg, (void*)param_far+30, 35));
384     }
385
386     return DISK_RET_SUCCESS;
387 }
388
389 struct dpte_s DefaultDPTE VARLOW;
390
391 static int
392 fill_ata_edd(u16 seg, struct int13dpt_s *param_far, struct drive_s *drive_gf)
393 {
394     if (!CONFIG_ATA)
395         return DISK_RET_EPARAM;
396
397     // Fill in dpte
398     struct atadrive_s *adrive_gf = container_of(
399         drive_gf, struct atadrive_s, drive);
400     struct ata_channel_s *chan_gf = GET_GLOBALFLAT(adrive_gf->chan_gf);
401     u8 slave = GET_GLOBALFLAT(adrive_gf->slave);
402     u16 iobase2 = GET_GLOBALFLAT(chan_gf->iobase2);
403     u8 irq = GET_GLOBALFLAT(chan_gf->irq);
404     u16 iobase1 = GET_GLOBALFLAT(chan_gf->iobase1);
405     int bdf = GET_GLOBALFLAT(chan_gf->pci_bdf);
406     u8 channel = GET_GLOBALFLAT(chan_gf->chanid);
407
408     u16 options = 0;
409     if (GET_GLOBALFLAT(drive_gf->type) == DTYPE_ATA) {
410         u8 translation = GET_GLOBALFLAT(drive_gf->translation);
411         if (translation != TRANSLATION_NONE) {
412             options |= 1<<3; // CHS translation
413             if (translation == TRANSLATION_LBA)
414                 options |= 1<<9;
415             if (translation == TRANSLATION_RECHS)
416                 options |= 3<<9;
417         }
418     } else {
419         // ATAPI
420         options |= 1<<5; // removable device
421         options |= 1<<6; // atapi device
422     }
423     options |= 1<<4; // lba translation
424     if (CONFIG_ATA_PIO32)
425         options |= 1<<7;
426
427     SET_LOW(DefaultDPTE.iobase1, iobase1);
428     SET_LOW(DefaultDPTE.iobase2, iobase2 + ATA_CB_DC);
429     SET_LOW(DefaultDPTE.prefix, ((slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0)
430                                  | ATA_CB_DH_LBA));
431     SET_LOW(DefaultDPTE.unused, 0xcb);
432     SET_LOW(DefaultDPTE.irq, irq);
433     SET_LOW(DefaultDPTE.blkcount, 1);
434     SET_LOW(DefaultDPTE.dma, 0);
435     SET_LOW(DefaultDPTE.pio, 0);
436     SET_LOW(DefaultDPTE.options, options);
437     SET_LOW(DefaultDPTE.reserved, 0);
438     SET_LOW(DefaultDPTE.revision, 0x11);
439
440     u8 sum = checksum_far(SEG_LOW, &DefaultDPTE, 15);
441     SET_LOW(DefaultDPTE.checksum, -sum);
442
443     return fill_generic_edd(
444         seg, param_far, drive_gf, SEGOFF(SEG_LOW, (u32)&DefaultDPTE).segoff
445         , "ATA     ", bdf, channel, iobase1, slave);
446 }
447
448 int noinline
449 fill_edd(u16 seg, struct int13dpt_s *param_far, struct drive_s *drive_gf)
450 {
451     switch (GET_GLOBALFLAT(drive_gf->type)) {
452     case DTYPE_ATA:
453     case DTYPE_ATA_ATAPI:
454         return fill_ata_edd(seg, param_far, drive_gf);
455     case DTYPE_VIRTIO_BLK:
456     case DTYPE_VIRTIO_SCSI:
457         return fill_generic_edd(
458             seg, param_far, drive_gf, 0xffffffff
459             , "SCSI    ", GET_GLOBALFLAT(drive_gf->cntl_id), 0, 0, 0);
460     default:
461         return fill_generic_edd(seg, param_far, drive_gf, 0, NULL, 0, 0, 0, 0);
462     }
463 }
464
465
466 /****************************************************************
467  * 16bit calling interface
468  ****************************************************************/
469
470 int VISIBLE32FLAT
471 process_atapi_op(struct disk_op_s *op)
472 {
473     switch (op->command) {
474     case CMD_WRITE:
475     case CMD_FORMAT:
476         return DISK_RET_EWRITEPROTECT;
477     default:
478         return scsi_process_op(op);
479     }
480 }
481
482 // Execute a disk_op request.
483 int
484 process_op(struct disk_op_s *op)
485 {
486     ASSERT16();
487     int ret, origcount = op->count;
488     if (origcount * GET_GLOBALFLAT(op->drive_gf->blksize) > 64*1024) {
489         op->count = 0;
490         return DISK_RET_EBOUNDARY;
491     }
492     u8 type = GET_GLOBALFLAT(op->drive_gf->type);
493     switch (type) {
494     case DTYPE_FLOPPY:
495         ret = process_floppy_op(op);
496         break;
497     case DTYPE_ATA:
498         ret = process_ata_op(op);
499         break;
500     case DTYPE_RAMDISK:
501         ret = process_ramdisk_op(op);
502         break;
503     case DTYPE_CDEMU:
504         ret = process_cdemu_op(op);
505         break;
506     case DTYPE_VIRTIO_BLK:
507         ret = process_virtio_blk_op(op);
508         break;
509     case DTYPE_AHCI: ;
510         extern void _cfunc32flat_process_ahci_op(void);
511         ret = call32(_cfunc32flat_process_ahci_op
512                      , (u32)MAKE_FLATPTR(GET_SEG(SS), op), DISK_RET_EPARAM);
513         break;
514     case DTYPE_ATA_ATAPI:
515         ret = process_atapi_op(op);
516         break;
517     case DTYPE_AHCI_ATAPI: ;
518         extern void _cfunc32flat_process_atapi_op(void);
519         ret = call32(_cfunc32flat_process_atapi_op
520                      , (u32)MAKE_FLATPTR(GET_SEG(SS), op), DISK_RET_EPARAM);
521         break;
522     case DTYPE_SDCARD: ;
523         extern void _cfunc32flat_process_sdcard_op(void);
524         ret = call32(_cfunc32flat_process_sdcard_op
525                      , (u32)MAKE_FLATPTR(GET_SEG(SS), op), DISK_RET_EPARAM);
526         break;
527     case DTYPE_USB:
528     case DTYPE_UAS:
529     case DTYPE_VIRTIO_SCSI:
530     case DTYPE_LSI_SCSI:
531     case DTYPE_ESP_SCSI:
532     case DTYPE_MEGASAS:
533         ret = scsi_process_op(op);
534         break;
535     case DTYPE_USB_32:
536     case DTYPE_UAS_32:
537     case DTYPE_PVSCSI: ;
538         extern void _cfunc32flat_scsi_process_op(void);
539         ret = call32(_cfunc32flat_scsi_process_op
540                      , (u32)MAKE_FLATPTR(GET_SEG(SS), op), DISK_RET_EPARAM);
541         break;
542     default:
543         ret = DISK_RET_EPARAM;
544         break;
545     }
546     if (ret && op->count == origcount)
547         // If the count hasn't changed on error, assume no data transferred.
548         op->count = 0;
549     return ret;
550 }
551
552 // Execute a "disk_op_s" request - this runs on the extra stack.
553 static int
554 __send_disk_op(struct disk_op_s *op_far, u16 op_seg)
555 {
556     struct disk_op_s dop;
557     memcpy_far(GET_SEG(SS), &dop
558                , op_seg, op_far
559                , sizeof(dop));
560
561     dprintf(DEBUG_HDL_13, "disk_op d=%p lba=%d buf=%p count=%d cmd=%d\n"
562             , dop.drive_gf, (u32)dop.lba, dop.buf_fl
563             , dop.count, dop.command);
564
565     int status = process_op(&dop);
566
567     // Update count with total sectors transferred.
568     SET_FARVAR(op_seg, op_far->count, dop.count);
569
570     return status;
571 }
572
573 // Execute a "disk_op_s" request by jumping to the extra 16bit stack.
574 int
575 send_disk_op(struct disk_op_s *op)
576 {
577     ASSERT16();
578     if (! CONFIG_DRIVES)
579         return -1;
580
581     return stack_hop((u32)op, GET_SEG(SS), __send_disk_op);
582 }