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