1 // Disk setup and access
3 // Copyright (C) 2008,2009 Kevin O'Connor <kevin@koconnor.net>
4 // Copyright (C) 2002 MandrakeSoft S.A.
6 // This file may be distributed under the terms of the GNU LGPLv3 license.
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
30 u8 FloppyCount VARFSEG;
32 struct drive_s *IDMap[3][BUILD_MAX_EXTDRIVE] VARFSEG;
33 u8 *bounce_buf_fl VARFSEG;
36 getDrive(u8 exttype, u8 extdriveoffset)
38 if (extdriveoffset >= ARRAY_SIZE(IDMap[0]))
40 return GET_GLOBAL(IDMap[exttype][extdriveoffset]);
43 int getDriveId(u8 exttype, struct drive_s *drive)
47 for (i = 0; i < ARRAY_SIZE(IDMap[0]); i++)
48 if (getDrive(exttype, i) == drive)
53 int create_bounce_buf(void)
58 u8 *buf = malloc_low(CDROM_SECTOR_SIZE);
67 /****************************************************************
68 * Disk geometry translation
69 ****************************************************************/
72 get_translation(struct drive_s *drive)
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);
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;
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;
101 setup_translation(struct drive_s *drive)
103 u8 translation = get_translation(drive);
104 drive->translation = translation;
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;
112 switch (translation) {
114 case TRANSLATION_NONE:
117 case TRANSLATION_LBA:
120 if (sectors > 63*255*1024) {
125 u32 sect = (u32)sectors / 63;
137 cylinders = sect / heads;
139 case TRANSLATION_RECHS:
141 // Take care not to overflow
146 cylinders = (u16)((u32)(cylinders)*16/15);
148 // then go through the large bitshift process
149 case TRANSLATION_LARGE:
150 if (translation == TRANSLATION_LARGE)
152 while (cylinders > 1024) {
156 // If we max out the head count
162 // clip to 1024 cylinders in lchs
163 if (cylinders > 1024)
165 dprintf(1, "drive %p: PCHS=%u/%d/%d translation=%s LCHS=%d/%d/%d s=%d\n"
167 , drive->pchs.cylinder, drive->pchs.head, drive->pchs.sector
169 , cylinders, heads, spt
172 drive->lchs.head = heads;
173 drive->lchs.cylinder = cylinders;
174 drive->lchs.sector = spt;
178 /****************************************************************
180 ****************************************************************/
182 // Fill in Fixed Disk Parameter Table (located in ebda).
184 fill_fdpt(struct drive_s *drive, int hdid)
189 u16 nlc = drive->lchs.cylinder;
190 u16 nlh = drive->lchs.head;
191 u16 nls = drive->lchs.sector;
193 u16 npc = drive->pchs.cylinder;
194 u16 nph = drive->pchs.head;
195 u16 nps = drive->pchs.sector;
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;
205 if (nlc != npc || nlh != nph || nls != nps) {
206 // Logical mapping present - use extended structure.
208 // complies with Phoenix style Translated Fixed Disk Parameter
210 fdpt->phys_cylinders = npc;
211 fdpt->phys_heads = nph;
212 fdpt->phys_sectors = nps;
213 fdpt->a0h_signature = 0xa0;
215 // Checksum structure.
216 fdpt->checksum -= checksum(fdpt, sizeof(*fdpt));
220 SET_IVT(0x41, SEGOFF(get_ebda_seg(), offsetof(
221 struct extended_bios_data_area_s, fdpt[0])));
223 SET_IVT(0x46, SEGOFF(get_ebda_seg(), offsetof(
224 struct extended_bios_data_area_s, fdpt[1])));
227 // Find spot to add a drive
229 add_drive(struct drive_s **idmap, u8 *count, struct drive_s *drive)
231 if (*count >= ARRAY_SIZE(IDMap[0])) {
235 idmap[*count] = drive;
241 map_hd_drive(struct drive_s *drive)
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);
249 // Setup disk geometry translation.
250 setup_translation(drive);
252 // Fill "fdpt" structure.
253 fill_fdpt(drive, hdid);
258 map_cd_drive(struct drive_s *drive)
261 dprintf(3, "Mapping cd drive %p\n", drive);
262 add_drive(IDMap[EXTTYPE_CD], &CDCount, drive);
267 map_floppy_drive(struct drive_s *drive)
270 dprintf(3, "Mapping floppy drive %p\n", drive);
271 add_drive(IDMap[EXTTYPE_FLOPPY], &FloppyCount, drive);
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);
286 /****************************************************************
287 * Extended Disk Drive (EDD) get drive parameters
288 ****************************************************************/
290 // flags for bus_iface field in fill_generic_edd()
293 #define EDD_BUS_MASK 0x0f
295 #define EDD_SCSI 0x20
296 #define EDD_IFACE_MASK 0xf0
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)
304 struct int13dpt_s *param_far = (void*)(edd.offset+0);
305 u16 size = GET_FARVAR(seg, param_far->size);
306 u16 t13 = size == 74;
308 // Buffer is too small
310 return DISK_RET_EPARAM;
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);
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);
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);
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);
336 SET_FARVAR(seg, param_far->infos, 0x02); // geometry is valid
337 SET_FARVAR(seg, param_far->cylinders, (u32)npc);
339 SET_FARVAR(seg, param_far->heads, (u32)nph);
340 SET_FARVAR(seg, param_far->spt, (u32)nps);
342 SET_FARVAR(seg, param_far->sector_count, lba);
343 SET_FARVAR(seg, param_far->blksize, blksize);
345 if (size < 30 || !dpte_so)
346 return DISK_RET_SUCCESS;
350 SET_FARVAR(seg, param_far->size, 30);
351 SET_FARVAR(seg, param_far->dpte.segoff, dpte_so);
353 if (size < 66 || !bus_iface)
354 return DISK_RET_SUCCESS;
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);
362 const char *host_bus = "ISA ";
363 if ((bus_iface & EDD_BUS_MASK) == EDD_PCI) {
366 // Phoenix v3 spec (pre t13) did not define the PCI channel field
367 iface_path &= 0x00ffffff;
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);
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));
379 SET_FARVAR(seg, param_far->t13.device_path[0], device_path);
380 SET_FARVAR(seg, param_far->t13.device_path[1], 0);
382 SET_FARVAR(seg, param_far->t13.checksum
383 , -checksum_far(seg, (void*)param_far+30, 43));
385 SET_FARVAR(seg, param_far->phoenix.device_path, device_path);
387 SET_FARVAR(seg, param_far->phoenix.checksum
388 , -checksum_far(seg, (void*)param_far+30, 35));
391 return DISK_RET_SUCCESS;
394 // Build an EDD "iface_path" field for a PCI device
396 edd_pci_path(u16 bdf, u8 channel)
398 return (pci_bdf_to_bus(bdf) | (pci_bdf_to_dev(bdf) << 8)
399 | (pci_bdf_to_fn(bdf) << 16) | ((u32)channel << 24));
402 struct dpte_s DefaultDPTE VARLOW;
404 // EDD info for ATA and ATAPI drives
406 fill_ata_edd(struct segoff_s edd, struct drive_s *drive_gf)
409 return DISK_RET_EPARAM;
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);
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)
429 if (translation == TRANSLATION_RECHS)
434 options |= 1<<5; // removable device
435 options |= 1<<6; // atapi device
437 options |= 1<<4; // lba translation
438 if (CONFIG_ATA_PIO32)
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)
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);
454 u8 sum = checksum_far(SEG_LOW, &DefaultDPTE, 15);
455 SET_LOW(DefaultDPTE.checksum, -sum);
457 u32 bustype = EDD_ISA, ifpath = iobase1;
460 ifpath = edd_pci_path(bdf, channel);
462 return fill_generic_edd(
463 edd, drive_gf, SEGOFF(SEG_LOW, (u32)&DefaultDPTE).segoff
464 , bustype | EDD_ATA, ifpath, slave);
467 // Fill Extended Disk Drive (EDD) "Get drive parameters" info for a drive
469 fill_edd(struct segoff_s edd, struct drive_s *drive_gf)
471 switch (GET_GLOBALFLAT(drive_gf->type)) {
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);
481 return fill_generic_edd(edd, drive_gf, 0, 0, 0, 0);
486 /****************************************************************
487 * Disk driver dispatch
488 ****************************************************************/
490 // Fallback handler for command requests not implemented by drivers
492 default_process_op(struct disk_op_s *op)
494 switch (op->command) {
500 // Return success if the driver doesn't implement these commands
501 return DISK_RET_SUCCESS;
503 return DISK_RET_EPARAM;
507 // Command dispatch for disk drivers that run in both 16bit and 32bit mode
509 process_op_both(struct disk_op_s *op)
511 switch (GET_GLOBALFLAT(op->drive_gf->type)) {
512 case DTYPE_ATA_ATAPI:
513 return ata_atapi_process_op(op);
515 return usb_process_op(op);
517 return uas_process_op(op);
519 return lsi_scsi_process_op(op);
521 return esp_scsi_process_op(op);
523 return megasas_process_op(op);
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)
533 // Command dispatch for disk drivers that only run in 32bit mode
535 process_op_32(struct disk_op_s *op)
538 switch (op->drive_gf->type) {
539 case DTYPE_VIRTIO_BLK:
540 return virtio_blk_process_op(op);
542 return ahci_process_op(op);
543 case DTYPE_AHCI_ATAPI:
544 return ahci_atapi_process_op(op);
546 return sdcard_process_op(op);
548 return usb_process_op(op);
550 return uas_process_op(op);
551 case DTYPE_VIRTIO_SCSI:
552 return virtio_scsi_process_op(op);
554 return pvscsi_process_op(op);
556 return process_op_both(op);
560 // Command dispatch for disk drivers that only run in 16bit mode
562 process_op_16(struct disk_op_s *op)
565 switch (GET_GLOBALFLAT(op->drive_gf->type)) {
567 return floppy_process_op(op);
569 return ata_process_op(op);
571 return ramdisk_process_op(op);
573 return cdemu_process_op(op);
575 return process_op_both(op);
579 // Execute a disk_op_s request.
581 process_op(struct disk_op_s *op)
583 int ret, origcount = op->count;
584 if (origcount * GET_GLOBALFLAT(op->drive_gf->blksize) > 64*1024) {
586 return DISK_RET_EBOUNDARY;
589 ret = process_op_16(op);
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.
598 // Execute a "disk_op_s" request - this runs on the extra stack.
600 __send_disk_op(struct disk_op_s *op_far, u16 op_seg)
602 struct disk_op_s dop;
603 memcpy_far(GET_SEG(SS), &dop
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);
611 int status = process_op(&dop);
613 // Update count with total sectors transferred.
614 SET_FARVAR(op_seg, op_far->count, dop.count);
619 // Execute a "disk_op_s" request by jumping to the extra 16bit stack.
621 send_disk_op(struct disk_op_s *op)
627 return stack_hop(__send_disk_op, op, GET_SEG(SS));