blob: 7892be5d089dff389f8a0b234178c651ad2d0c76 [file] [log] [blame]
Kevin O'Connorc892b132009-08-11 21:59:37 -04001// 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
Kevin O'Connorc892b132009-08-11 21:59:37 -04008#include "biosvar.h" // GET_GLOBAL
Kevin O'Connor135f3f62013-09-14 23:57:26 -04009#include "block.h" // process_op
Kevin O'Connor5d369d82013-09-02 20:48:46 -040010#include "hw/ata.h" // process_ata_op
11#include "hw/ahci.h" // process_ahci_op
Kevin O'Connor5d369d82013-09-02 20:48:46 -040012#include "hw/blockcmd.h" // cdb_*
Kevin O'Connor39ca4982014-05-10 11:42:22 -040013#include "hw/pci.h" // pci_bdf_to_bus
Kevin O'Connor8b7861c2013-09-15 02:29:06 -040014#include "hw/rtc.h" // rtc_read
Kevin O'Connor2d2fa312013-09-14 21:55:26 -040015#include "hw/virtio-blk.h" // process_virtio_blk_op
Kevin O'Connor9dea5902013-09-14 20:23:54 -040016#include "malloc.h" // malloc_low
Kevin O'Connor2d2fa312013-09-14 21:55:26 -040017#include "output.h" // dprintf
Kevin O'Connor3df600b2013-09-14 19:28:55 -040018#include "stacks.h" // stack_hop
Kevin O'Connor135f3f62013-09-14 23:57:26 -040019#include "std/disk.h" // struct dpte_s
Kevin O'Connorfa9c66a2013-09-14 19:10:40 -040020#include "string.h" // checksum
Kevin O'Connor135f3f62013-09-14 23:57:26 -040021#include "util.h" // process_floppy_op
Kevin O'Connorc892b132009-08-11 21:59:37 -040022
Kevin O'Connor89a2f962013-02-18 23:36:03 -050023u8 FloppyCount VARFSEG;
Kevin O'Connora0842f82010-12-29 11:05:46 -050024u8 CDCount;
Kevin O'Connore52ad392013-02-20 23:48:22 -050025struct drive_s *IDMap[3][BUILD_MAX_EXTDRIVE] VARFSEG;
Kevin O'Connor89a2f962013-02-18 23:36:03 -050026u8 *bounce_buf_fl VARFSEG;
Kevin O'Connorc892b132009-08-11 21:59:37 -040027
Kevin O'Connor77d227b2009-10-22 21:48:39 -040028struct drive_s *
29getDrive(u8 exttype, u8 extdriveoffset)
30{
Kevin O'Connora0842f82010-12-29 11:05:46 -050031 if (extdriveoffset >= ARRAY_SIZE(IDMap[0]))
Kevin O'Connor77d227b2009-10-22 21:48:39 -040032 return NULL;
Kevin O'Connor1902c942013-10-26 11:48:06 -040033 return GET_GLOBAL(IDMap[exttype][extdriveoffset]);
Kevin O'Connor77d227b2009-10-22 21:48:39 -040034}
35
Kevin O'Connor1902c942013-10-26 11:48:06 -040036int getDriveId(u8 exttype, struct drive_s *drive)
Gleb Natapov4c90a202010-12-07 13:50:54 +020037{
Kevin O'Connor1902c942013-10-26 11:48:06 -040038 ASSERT32FLAT();
Gleb Natapov4c90a202010-12-07 13:50:54 +020039 int i;
Kevin O'Connora0842f82010-12-29 11:05:46 -050040 for (i = 0; i < ARRAY_SIZE(IDMap[0]); i++)
Kevin O'Connor1902c942013-10-26 11:48:06 -040041 if (getDrive(exttype, i) == drive)
Gleb Natapov4c90a202010-12-07 13:50:54 +020042 return i;
Gleb Natapov4c90a202010-12-07 13:50:54 +020043 return -1;
44}
Kevin O'Connorc892b132009-08-11 21:59:37 -040045
Kevin O'Connord83c87b2013-01-21 01:14:12 -050046int create_bounce_buf(void)
Gerd Hoffmannd7a7cf32011-08-04 19:36:27 +020047{
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}
Kevin O'Connor3c5e0e12010-12-27 08:37:38 -050059
Kevin O'Connorc892b132009-08-11 21:59:37 -040060/****************************************************************
61 * Disk geometry translation
62 ****************************************************************/
63
64static u8
Kevin O'Connor1902c942013-10-26 11:48:06 -040065get_translation(struct drive_s *drive)
Kevin O'Connorc892b132009-08-11 21:59:37 -040066{
Kevin O'Connor1902c942013-10-26 11:48:06 -040067 u8 type = drive->type;
Kevin O'Connor897fb112013-02-07 23:32:48 -050068 if (CONFIG_QEMU && type == DTYPE_ATA) {
Kevin O'Connorc892b132009-08-11 21:59:37 -040069 // Emulators pass in the translation info via nvram.
Kevin O'Connor1902c942013-10-26 11:48:06 -040070 u8 ataid = drive->cntl_id;
Kevin O'Connorc892b132009-08-11 21:59:37 -040071 u8 channel = ataid / 2;
Kevin O'Connor8b7861c2013-09-15 02:29:06 -040072 u8 translation = rtc_read(CMOS_BIOS_DISKTRANSFLAG + channel/2);
Kevin O'Connorc892b132009-08-11 21:59:37 -040073 translation >>= 2 * (ataid % 4);
74 translation &= 0x03;
75 return translation;
76 }
77
Kevin O'Connord2d1de02010-02-17 01:07:36 -050078 // Otherwise use a heuristic to determine translation type.
Kevin O'Connor1902c942013-10-26 11:48:06 -040079 u16 heads = drive->pchs.head;
80 u16 cylinders = drive->pchs.cylinder;
81 u16 spt = drive->pchs.sector;
82 u64 sectors = drive->sectors;
Kevin O'Connord2d1de02010-02-17 01:07:36 -050083 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;
Kevin O'Connorc892b132009-08-11 21:59:37 -040087
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
Kevin O'Connor697e63c2010-12-27 21:08:53 -050095static void
Kevin O'Connor1902c942013-10-26 11:48:06 -040096setup_translation(struct drive_s *drive)
Kevin O'Connorc892b132009-08-11 21:59:37 -040097{
Kevin O'Connor1902c942013-10-26 11:48:06 -040098 u8 translation = get_translation(drive);
99 drive->translation = translation;
Kevin O'Connorc892b132009-08-11 21:59:37 -0400100
Kevin O'Connor1902c942013-10-26 11:48:06 -0400101 u16 heads = drive->pchs.head ;
102 u16 cylinders = drive->pchs.cylinder;
103 u16 spt = drive->pchs.sector;
104 u64 sectors = drive->sectors;
Kevin O'Connor6f6a74d2009-11-09 19:15:50 -0500105 const char *desc = NULL;
Kevin O'Connorc892b132009-08-11 21:59:37 -0400106
Kevin O'Connorc892b132009-08-11 21:59:37 -0400107 switch (translation) {
Kevin O'Connorc604f2f2009-10-24 19:56:11 -0400108 default:
Kevin O'Connorc892b132009-08-11 21:59:37 -0400109 case TRANSLATION_NONE:
Kevin O'Connorc604f2f2009-10-24 19:56:11 -0400110 desc = "none";
Kevin O'Connorc892b132009-08-11 21:59:37 -0400111 break;
112 case TRANSLATION_LBA:
Kevin O'Connorc604f2f2009-10-24 19:56:11 -0400113 desc = "lba";
Kevin O'Connorc892b132009-08-11 21:59:37 -0400114 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:
Kevin O'Connorc604f2f2009-10-24 19:56:11 -0400135 desc = "r-echs";
Kevin O'Connorc892b132009-08-11 21:59:37 -0400136 // 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)
Kevin O'Connorc604f2f2009-10-24 19:56:11 -0400146 desc = "large";
Kevin O'Connorc892b132009-08-11 21:59:37 -0400147 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;
Kevin O'Connord2d1de02010-02-17 01:07:36 -0500160 dprintf(1, "drive %p: PCHS=%u/%d/%d translation=%s LCHS=%d/%d/%d s=%d\n"
Kevin O'Connor1902c942013-10-26 11:48:06 -0400161 , drive
162 , drive->pchs.cylinder, drive->pchs.head, drive->pchs.sector
Kevin O'Connorc604f2f2009-10-24 19:56:11 -0400163 , desc
Kevin O'Connord2d1de02010-02-17 01:07:36 -0500164 , cylinders, heads, spt
165 , (u32)sectors);
Kevin O'Connorc892b132009-08-11 21:59:37 -0400166
Kevin O'Connor1902c942013-10-26 11:48:06 -0400167 drive->lchs.head = heads;
168 drive->lchs.cylinder = cylinders;
169 drive->lchs.sector = spt;
Kevin O'Connorc892b132009-08-11 21:59:37 -0400170}
171
172
173/****************************************************************
174 * Drive mapping
175 ****************************************************************/
176
177// Fill in Fixed Disk Parameter Table (located in ebda).
178static void
Kevin O'Connor1902c942013-10-26 11:48:06 -0400179fill_fdpt(struct drive_s *drive, int hdid)
Kevin O'Connorc892b132009-08-11 21:59:37 -0400180{
Kevin O'Connor34ec7b02009-09-20 20:04:06 -0400181 if (hdid > 1)
Kevin O'Connorc892b132009-08-11 21:59:37 -0400182 return;
183
Kevin O'Connor1902c942013-10-26 11:48:06 -0400184 u16 nlc = drive->lchs.cylinder;
185 u16 nlh = drive->lchs.head;
186 u16 nls = drive->lchs.sector;
Kevin O'Connorc892b132009-08-11 21:59:37 -0400187
Kevin O'Connor1902c942013-10-26 11:48:06 -0400188 u16 npc = drive->pchs.cylinder;
189 u16 nph = drive->pchs.head;
190 u16 nps = drive->pchs.sector;
Kevin O'Connorc892b132009-08-11 21:59:37 -0400191
Kevin O'Connor34ec7b02009-09-20 20:04:06 -0400192 struct fdpt_s *fdpt = &get_ebda_ptr()->fdpt[hdid];
Kevin O'Connorc892b132009-08-11 21:59:37 -0400193 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;
Kevin O'Connor8ab9a342013-09-28 23:34:49 -0400198 fdpt->sectors = nls;
Kevin O'Connorc892b132009-08-11 21:59:37 -0400199
Kevin O'Connor8ab9a342013-09-28 23:34:49 -0400200 if (nlc != npc || nlh != nph || nls != nps) {
Kevin O'Connor085debd2010-01-03 22:24:18 -0500201 // Logical mapping present - use extended structure.
Kevin O'Connorc892b132009-08-11 21:59:37 -0400202
Kevin O'Connor085debd2010-01-03 22:24:18 -0500203 // complies with Phoenix style Translated Fixed Disk Parameter
204 // Table (FDPT)
205 fdpt->phys_cylinders = npc;
206 fdpt->phys_heads = nph;
Kevin O'Connor8ab9a342013-09-28 23:34:49 -0400207 fdpt->phys_sectors = nps;
Kevin O'Connor085debd2010-01-03 22:24:18 -0500208 fdpt->a0h_signature = 0xa0;
Kevin O'Connorc892b132009-08-11 21:59:37 -0400209
Kevin O'Connor085debd2010-01-03 22:24:18 -0500210 // Checksum structure.
211 fdpt->checksum -= checksum(fdpt, sizeof(*fdpt));
212 }
Kevin O'Connorc892b132009-08-11 21:59:37 -0400213
Kevin O'Connor34ec7b02009-09-20 20:04:06 -0400214 if (hdid == 0)
Kevin O'Connor9f985422009-09-09 11:34:39 -0400215 SET_IVT(0x41, SEGOFF(get_ebda_seg(), offsetof(
216 struct extended_bios_data_area_s, fdpt[0])));
Kevin O'Connorc892b132009-08-11 21:59:37 -0400217 else
Kevin O'Connor9f985422009-09-09 11:34:39 -0400218 SET_IVT(0x46, SEGOFF(get_ebda_seg(), offsetof(
219 struct extended_bios_data_area_s, fdpt[1])));
Kevin O'Connorc892b132009-08-11 21:59:37 -0400220}
221
Kevin O'Connord254dc22009-11-25 18:49:06 -0500222// Find spot to add a drive
223static void
Kevin O'Connor1902c942013-10-26 11:48:06 -0400224add_drive(struct drive_s **idmap, u8 *count, struct drive_s *drive)
Kevin O'Connord254dc22009-11-25 18:49:06 -0500225{
Kevin O'Connora0842f82010-12-29 11:05:46 -0500226 if (*count >= ARRAY_SIZE(IDMap[0])) {
Kevin O'Connorcfdc13f2010-02-14 13:07:54 -0500227 warn_noalloc();
Kevin O'Connord254dc22009-11-25 18:49:06 -0500228 return;
229 }
Kevin O'Connor1902c942013-10-26 11:48:06 -0400230 idmap[*count] = drive;
Kevin O'Connord254dc22009-11-25 18:49:06 -0500231 *count = *count + 1;
Kevin O'Connord254dc22009-11-25 18:49:06 -0500232}
233
Kevin O'Connor3c5e0e12010-12-27 08:37:38 -0500234// Map a hard drive
235void
Kevin O'Connor1902c942013-10-26 11:48:06 -0400236map_hd_drive(struct drive_s *drive)
Kevin O'Connor3c5e0e12010-12-27 08:37:38 -0500237{
238 ASSERT32FLAT();
239 struct bios_data_area_s *bda = MAKE_FLATPTR(SEG_BDA, 0);
240 int hdid = bda->hdcount;
Kevin O'Connor1902c942013-10-26 11:48:06 -0400241 dprintf(3, "Mapping hd drive %p to %d\n", drive, hdid);
242 add_drive(IDMap[EXTTYPE_HD], &bda->hdcount, drive);
Kevin O'Connor3c5e0e12010-12-27 08:37:38 -0500243
Kevin O'Connor697e63c2010-12-27 21:08:53 -0500244 // Setup disk geometry translation.
Kevin O'Connor1902c942013-10-26 11:48:06 -0400245 setup_translation(drive);
Kevin O'Connor697e63c2010-12-27 21:08:53 -0500246
Kevin O'Connor3c5e0e12010-12-27 08:37:38 -0500247 // Fill "fdpt" structure.
Kevin O'Connor1902c942013-10-26 11:48:06 -0400248 fill_fdpt(drive, hdid);
Kevin O'Connor3c5e0e12010-12-27 08:37:38 -0500249}
250
Kevin O'Connorc892b132009-08-11 21:59:37 -0400251// Map a cd
252void
Kevin O'Connor1902c942013-10-26 11:48:06 -0400253map_cd_drive(struct drive_s *drive)
Kevin O'Connorc892b132009-08-11 21:59:37 -0400254{
Kevin O'Connor1902c942013-10-26 11:48:06 -0400255 ASSERT32FLAT();
256 dprintf(3, "Mapping cd drive %p\n", drive);
257 add_drive(IDMap[EXTTYPE_CD], &CDCount, drive);
Kevin O'Connorc892b132009-08-11 21:59:37 -0400258}
259
Kevin O'Connor0a0e42e2009-08-16 12:09:44 -0400260// Map a floppy
261void
Kevin O'Connor1902c942013-10-26 11:48:06 -0400262map_floppy_drive(struct drive_s *drive)
Kevin O'Connor0a0e42e2009-08-16 12:09:44 -0400263{
Kevin O'Connor1902c942013-10-26 11:48:06 -0400264 ASSERT32FLAT();
265 dprintf(3, "Mapping floppy drive %p\n", drive);
266 add_drive(IDMap[EXTTYPE_FLOPPY], &FloppyCount, drive);
Kevin O'Connor0a0e42e2009-08-16 12:09:44 -0400267
268 // Update equipment word bits for floppy
Kevin O'Connora0842f82010-12-29 11:05:46 -0500269 if (FloppyCount == 1) {
Kevin O'Connor0a0e42e2009-08-16 12:09:44 -0400270 // 1 drive, ready for boot
Kevin O'Connore51316d2012-06-10 09:09:22 -0400271 set_equipment_flags(0x41, 0x01);
Kevin O'Connor0a0e42e2009-08-16 12:09:44 -0400272 SET_BDA(floppy_harddisk_info, 0x07);
Kevin O'Connora0842f82010-12-29 11:05:46 -0500273 } else if (FloppyCount >= 2) {
Kevin O'Connor0a0e42e2009-08-16 12:09:44 -0400274 // 2 drives, ready for boot
Kevin O'Connore51316d2012-06-10 09:09:22 -0400275 set_equipment_flags(0x41, 0x41);
Kevin O'Connor0a0e42e2009-08-16 12:09:44 -0400276 SET_BDA(floppy_harddisk_info, 0x77);
277 }
278}
279
Kevin O'Connorc892b132009-08-11 21:59:37 -0400280
281/****************************************************************
Kevin O'Connor39ca4982014-05-10 11:42:22 -0400282 * Extended Disk Drive (EDD) get drive parameters
283 ****************************************************************/
284
Kevin O'Connor9e735bb2014-05-10 12:46:57 -0400285static int
286fill_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)
Kevin O'Connor39ca4982014-05-10 11:42:22 -0400289{
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);
Kevin O'Connorf8387e22014-05-10 11:54:48 -0400310 if (lba == (u64)-1) {
Kevin O'Connor39ca4982014-05-10 11:42:22 -0400311 // 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);
Kevin O'Connor39ca4982014-05-10 11:42:22 -0400316 } 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);
Kevin O'Connor39ca4982014-05-10 11:42:22 -0400326 }
Kevin O'Connorf8387e22014-05-10 11:54:48 -0400327 SET_FARVAR(seg, param_far->sector_count, lba);
Kevin O'Connor39ca4982014-05-10 11:42:22 -0400328 SET_FARVAR(seg, param_far->blksize, blksize);
329
Kevin O'Connor9e735bb2014-05-10 12:46:57 -0400330 if (size < 30 || !dpte_so)
Kevin O'Connor39ca4982014-05-10 11:42:22 -0400331 return DISK_RET_SUCCESS;
332
333 // EDD 2.x
334
Kevin O'Connor39ca4982014-05-10 11:42:22 -0400335 SET_FARVAR(seg, param_far->size, 30);
Kevin O'Connor9e735bb2014-05-10 12:46:57 -0400336 SET_FARVAR(seg, param_far->dpte.segoff, dpte_so);
Kevin O'Connor39ca4982014-05-10 11:42:22 -0400337
Kevin O'Connor9e735bb2014-05-10 12:46:57 -0400338 if (size < 66 || !iface_type)
Kevin O'Connor39ca4982014-05-10 11:42:22 -0400339 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
Kevin O'Connor9e735bb2014-05-10 12:46:57 -0400347 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
Kevin O'Connor39ca4982014-05-10 11:42:22 -0400351 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
Kevin O'Connor9e735bb2014-05-10 12:46:57 -0400370 SET_FARVAR(seg, param_far->iface_path, iobase);
Kevin O'Connor39ca4982014-05-10 11:42:22 -0400371 }
372
Kevin O'Connor39ca4982014-05-10 11:42:22 -0400373 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
Kevin O'Connor9e735bb2014-05-10 12:46:57 -0400389struct dpte_s DefaultDPTE VARLOW;
390
391static int
392fill_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
448int noinline
449fill_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
Kevin O'Connor39ca4982014-05-10 11:42:22 -0400465
466/****************************************************************
Kevin O'Connoraf5aabb2009-08-16 18:48:38 -0400467 * 16bit calling interface
468 ****************************************************************/
469
Kevin O'Connor4b400a12013-12-24 00:46:15 -0500470int VISIBLE32FLAT
Paolo Bonzini1e749c82011-11-16 13:02:53 +0100471process_scsi_op(struct disk_op_s *op)
472{
Paolo Bonzini1e749c82011-11-16 13:02:53 +0100473 switch (op->command) {
474 case CMD_READ:
475 return cdb_read(op);
476 case CMD_WRITE:
477 return cdb_write(op);
478 case CMD_FORMAT:
479 case CMD_RESET:
480 case CMD_ISREADY:
481 case CMD_VERIFY:
482 case CMD_SEEK:
483 return DISK_RET_SUCCESS;
484 default:
Paolo Bonzini1e749c82011-11-16 13:02:53 +0100485 return DISK_RET_EPARAM;
486 }
487}
488
Kevin O'Connor7c80bb82013-10-02 21:28:08 -0400489int VISIBLE32FLAT
Kevin O'Connorbd6afe52012-07-21 12:01:12 -0400490process_atapi_op(struct disk_op_s *op)
491{
492 switch (op->command) {
493 case CMD_WRITE:
494 case CMD_FORMAT:
495 return DISK_RET_EWRITEPROTECT;
496 default:
497 return process_scsi_op(op);
498 }
499}
500
Kevin O'Connoraf5aabb2009-08-16 18:48:38 -0400501// Execute a disk_op request.
Kevin O'Connor36c93a52009-09-12 19:35:04 -0400502int
Kevin O'Connoraf5aabb2009-08-16 18:48:38 -0400503process_op(struct disk_op_s *op)
504{
Kevin O'Connord7e998f2010-02-15 22:48:28 -0500505 ASSERT16();
Kevin O'Connor518955f2013-12-17 17:57:48 -0500506 int ret, origcount = op->count;
Kevin O'Connor1902c942013-10-26 11:48:06 -0400507 u8 type = GET_GLOBALFLAT(op->drive_gf->type);
Kevin O'Connoraf5aabb2009-08-16 18:48:38 -0400508 switch (type) {
Kevin O'Connora3855ad2009-08-16 21:59:40 -0400509 case DTYPE_FLOPPY:
Kevin O'Connor518955f2013-12-17 17:57:48 -0500510 ret = process_floppy_op(op);
511 break;
Kevin O'Connoraf5aabb2009-08-16 18:48:38 -0400512 case DTYPE_ATA:
Kevin O'Connor518955f2013-12-17 17:57:48 -0500513 ret = process_ata_op(op);
514 break;
Kevin O'Connora3855ad2009-08-16 21:59:40 -0400515 case DTYPE_RAMDISK:
Kevin O'Connor518955f2013-12-17 17:57:48 -0500516 ret = process_ramdisk_op(op);
517 break;
Kevin O'Connor36c93a52009-09-12 19:35:04 -0400518 case DTYPE_CDEMU:
Kevin O'Connor518955f2013-12-17 17:57:48 -0500519 ret = process_cdemu_op(op);
520 break;
Paolo Bonzini0e7fb5f2011-11-16 13:02:55 +0100521 case DTYPE_VIRTIO_BLK:
Kevin O'Connor518955f2013-12-17 17:57:48 -0500522 ret = process_virtio_blk_op(op);
523 break;
Kevin O'Connor1902c942013-10-26 11:48:06 -0400524 case DTYPE_AHCI: ;
Kevin O'Connor7c80bb82013-10-02 21:28:08 -0400525 extern void _cfunc32flat_process_ahci_op(void);
Kevin O'Connor518955f2013-12-17 17:57:48 -0500526 ret = call32(_cfunc32flat_process_ahci_op
527 , (u32)MAKE_FLATPTR(GET_SEG(SS), op), DISK_RET_EPARAM);
528 break;
Kevin O'Connorbd6afe52012-07-21 12:01:12 -0400529 case DTYPE_ATA_ATAPI:
Kevin O'Connor518955f2013-12-17 17:57:48 -0500530 ret = process_atapi_op(op);
531 break;
Kevin O'Connor1902c942013-10-26 11:48:06 -0400532 case DTYPE_AHCI_ATAPI: ;
Kevin O'Connor7c80bb82013-10-02 21:28:08 -0400533 extern void _cfunc32flat_process_atapi_op(void);
Kevin O'Connor518955f2013-12-17 17:57:48 -0500534 ret = call32(_cfunc32flat_process_atapi_op
535 , (u32)MAKE_FLATPTR(GET_SEG(SS), op), DISK_RET_EPARAM);
536 break;
Paolo Bonzini1e749c82011-11-16 13:02:53 +0100537 case DTYPE_USB:
Gerd Hoffmanne53e30d2012-07-20 10:59:24 +0200538 case DTYPE_UAS:
Paolo Bonzinic5c488f2012-02-27 17:22:23 +0100539 case DTYPE_VIRTIO_SCSI:
Gerd Hoffmann9d6bac12012-07-20 10:59:25 +0200540 case DTYPE_LSI_SCSI:
Paolo Bonzini7a39e722012-08-06 13:15:06 +0200541 case DTYPE_ESP_SCSI:
Hannes Reinecke2df70bf2012-11-13 15:03:31 +0100542 case DTYPE_MEGASAS:
Kevin O'Connor518955f2013-12-17 17:57:48 -0500543 ret = process_scsi_op(op);
544 break;
Kevin O'Connorde30dad2013-12-30 22:09:04 -0500545 case DTYPE_USB_32:
546 case DTYPE_UAS_32:
Kevin O'Connor4b400a12013-12-24 00:46:15 -0500547 case DTYPE_PVSCSI: ;
548 extern void _cfunc32flat_process_scsi_op(void);
549 ret = call32(_cfunc32flat_process_scsi_op
550 , (u32)MAKE_FLATPTR(GET_SEG(SS), op), DISK_RET_EPARAM);
551 break;
Kevin O'Connoraf5aabb2009-08-16 18:48:38 -0400552 default:
Kevin O'Connor518955f2013-12-17 17:57:48 -0500553 ret = DISK_RET_EPARAM;
554 break;
Kevin O'Connoraf5aabb2009-08-16 18:48:38 -0400555 }
Kevin O'Connor518955f2013-12-17 17:57:48 -0500556 if (ret && op->count == origcount)
557 // If the count hasn't changed on error, assume no data transferred.
558 op->count = 0;
559 return ret;
Kevin O'Connoraf5aabb2009-08-16 18:48:38 -0400560}
561
Kevin O'Connor46b82622012-05-13 12:10:30 -0400562// Execute a "disk_op_s" request - this runs on the extra stack.
Kevin O'Connoraf5aabb2009-08-16 18:48:38 -0400563static int
564__send_disk_op(struct disk_op_s *op_far, u16 op_seg)
565{
566 struct disk_op_s dop;
567 memcpy_far(GET_SEG(SS), &dop
568 , op_seg, op_far
569 , sizeof(dop));
570
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400571 dprintf(DEBUG_HDL_13, "disk_op d=%p lba=%d buf=%p count=%d cmd=%d\n"
Kevin O'Connor1902c942013-10-26 11:48:06 -0400572 , dop.drive_gf, (u32)dop.lba, dop.buf_fl
Kevin O'Connoraf5aabb2009-08-16 18:48:38 -0400573 , dop.count, dop.command);
574
Kevin O'Connoraf5aabb2009-08-16 18:48:38 -0400575 int status = process_op(&dop);
576
Kevin O'Connoraf5aabb2009-08-16 18:48:38 -0400577 // Update count with total sectors transferred.
578 SET_FARVAR(op_seg, op_far->count, dop.count);
579
580 return status;
581}
582
Kevin O'Connor46b82622012-05-13 12:10:30 -0400583// Execute a "disk_op_s" request by jumping to the extra 16bit stack.
Kevin O'Connoraf5aabb2009-08-16 18:48:38 -0400584int
585send_disk_op(struct disk_op_s *op)
586{
Kevin O'Connord7e998f2010-02-15 22:48:28 -0500587 ASSERT16();
Kevin O'Connoraf5aabb2009-08-16 18:48:38 -0400588 if (! CONFIG_DRIVES)
589 return -1;
590
Kevin O'Connorbca07362010-03-20 20:41:38 -0400591 return stack_hop((u32)op, GET_SEG(SS), __send_disk_op);
Kevin O'Connoraf5aabb2009-08-16 18:48:38 -0400592}