VirtualBox

source: vbox/trunk/src/VBox/Devices/PC/BIOS/scsi.c@ 91881

Last change on this file since 91881 was 89364, checked in by vboxsync, 4 years ago

Devices/PC/BIOS: Remove the skip_b,skip_a mechanism as it is unused now making the device drivers lss complex, bugref:4841

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 18.4 KB
Line 
1/* $Id: scsi.c 89364 2021-05-28 15:44:38Z vboxsync $ */
2/** @file
3 * SCSI host adapter driver to boot from SCSI disks
4 */
5
6/*
7 * Copyright (C) 2004-2020 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 */
17
18#include <stdint.h>
19#include <string.h>
20#include "biosint.h"
21#include "inlines.h"
22#include "pciutil.h"
23#include "ebda.h"
24#include "scsi.h"
25
26
27#if DEBUG_SCSI
28# define DBG_SCSI(...) BX_INFO(__VA_ARGS__)
29#else
30# define DBG_SCSI(...)
31#endif
32
33#define VBSCSI_MAX_DEVICES 16 /* Maximum number of devices a SCSI device currently supported. */
34
35#define VBOX_SCSI_NO_HBA 0xffff
36
37typedef int (* scsi_hba_init)(void __far *pvHba, uint8_t u8Bus, uint8_t u8DevFn);
38typedef int (* scsi_hba_cmd_data_out)(void __far *pvHba, uint8_t idTgt, uint8_t __far *aCDB,
39 uint8_t cbCDB, uint8_t __far *buffer, uint32_t length);
40typedef int (* scsi_hba_cmd_data_in)(void __far *pvHba, uint8_t idTgt, uint8_t __far *aCDB,
41 uint8_t cbCDB, uint8_t __far *buffer, uint32_t length);
42
43typedef struct
44{
45 uint16_t idPciVendor;
46 uint16_t idPciDevice;
47 scsi_hba_init init;
48 scsi_hba_cmd_data_out cmd_data_out;
49 scsi_hba_cmd_data_in cmd_data_in;
50} scsi_hba_t;
51
52/* Machinery to save/restore high bits of EAX. 32-bit port I/O needs to use
53 * EAX, but saving/restoring EAX around each port access would be inefficient.
54 * Instead, each externally callable routine must save the high bits before
55 * modifying them and restore the high bits before exiting.
56 */
57
58/* Note: Reading high EAX bits destroys them - *must* be restored later. */
59uint16_t eax_hi_rd(void);
60#pragma aux eax_hi_rd = \
61 ".386" \
62 "shr eax, 16" \
63 value [ax] modify nomemory;
64
65void eax_hi_wr(uint16_t);
66#pragma aux eax_hi_wr = \
67 ".386" \
68 "shl eax, 16" \
69 parm [ax] modify nomemory;
70
71void inline high_bits_save(uint16_t __far *pu16EaxHi)
72{
73 *pu16EaxHi = eax_hi_rd();
74}
75
76void inline high_bits_restore(uint16_t u16EaxHi)
77{
78 eax_hi_wr(u16EaxHi);
79}
80
81/* Pointers to the HBA specific access routines. */
82scsi_hba_t hbaacc[] =
83{
84 { 0x1000, 0x0030, lsilogic_scsi_init, lsilogic_scsi_cmd_data_out, lsilogic_scsi_cmd_data_in }, /* SPI */
85 { 0x1000, 0x0054, lsilogic_scsi_init, lsilogic_scsi_cmd_data_out, lsilogic_scsi_cmd_data_in }, /* SAS */
86 { 0x104b, 0x1040, buslogic_scsi_init, buslogic_scsi_cmd_data_out, buslogic_scsi_cmd_data_in },
87#ifdef VBOX_WITH_VIRTIO_SCSI
88 { 0x1af4, 0x1048, virtio_scsi_init, virtio_scsi_cmd_data_out, virtio_scsi_cmd_data_in }
89#endif
90};
91
92/**
93 * Allocates 1K of conventional memory.
94 */
95static uint16_t scsi_hba_mem_alloc(void)
96{
97 uint16_t base_mem_kb;
98 uint16_t hba_seg;
99
100 base_mem_kb = read_word(0x00, 0x0413);
101
102 DBG_SCSI("SCSI: %dK of base mem\n", base_mem_kb);
103
104 if (base_mem_kb == 0)
105 return 0;
106
107 base_mem_kb--; /* Allocate one block. */
108 hba_seg = (((uint32_t)base_mem_kb * 1024) >> 4); /* Calculate start segment. */
109
110 write_word(0x00, 0x0413, base_mem_kb);
111
112 return hba_seg;
113}
114
115/**
116 * Read sectors from an attached SCSI device.
117 *
118 * @returns status code.
119 * @param bios_dsk Pointer to disk request packet (in the
120 * EBDA).
121 */
122int scsi_read_sectors(bio_dsk_t __far *bios_dsk)
123{
124 uint8_t rc;
125 cdb_rw16 cdb;
126 uint32_t count;
127 uint16_t hba_seg;
128 uint8_t idx_hba;
129 uint8_t target_id;
130 uint8_t device_id;
131 uint16_t eax_hi;
132
133 device_id = VBOX_GET_SCSI_DEVICE(bios_dsk->drqp.dev_id);
134 if (device_id > BX_MAX_SCSI_DEVICES)
135 BX_PANIC("%s: device_id out of range %d\n", __func__, device_id);
136
137 count = bios_dsk->drqp.nsect;
138
139 high_bits_save(&eax_hi);
140
141 /* Prepare a CDB. */
142 cdb.command = SCSI_READ_16;
143 cdb.lba = swap_64(bios_dsk->drqp.lba);
144 cdb.pad1 = 0;
145 cdb.nsect32 = swap_32(count);
146 cdb.pad2 = 0;
147
148
149 hba_seg = bios_dsk->scsidev[device_id].hba_seg;
150 idx_hba = bios_dsk->scsidev[device_id].idx_hba;
151 target_id = bios_dsk->scsidev[device_id].target_id;
152
153 DBG_SCSI("%s: reading %u sectors, device %d, target %d\n", __func__,
154 count, device_id, bios_dsk->scsidev[device_id].target_id);
155
156 rc = hbaacc[idx_hba].cmd_data_in(hba_seg :> 0, target_id, (void __far *)&cdb, 16,
157 bios_dsk->drqp.buffer, (count * 512L));
158 if (!rc)
159 {
160 bios_dsk->drqp.trsfsectors = count;
161 bios_dsk->drqp.trsfbytes = count * 512L;
162 }
163 DBG_SCSI("%s: transferred %u sectors\n", __func__, bios_dsk->drqp.nsect);
164 high_bits_restore(eax_hi);
165
166 return rc;
167}
168
169/**
170 * Write sectors to an attached SCSI device.
171 *
172 * @returns status code.
173 * @param bios_dsk Pointer to disk request packet (in the
174 * EBDA).
175 */
176int scsi_write_sectors(bio_dsk_t __far *bios_dsk)
177{
178 uint8_t rc;
179 cdb_rw16 cdb;
180 uint32_t count;
181 uint16_t hba_seg;
182 uint8_t idx_hba;
183 uint8_t target_id;
184 uint8_t device_id;
185 uint16_t eax_hi;
186
187 device_id = VBOX_GET_SCSI_DEVICE(bios_dsk->drqp.dev_id);
188 if (device_id > BX_MAX_SCSI_DEVICES)
189 BX_PANIC("%s: device_id out of range %d\n", __func__, device_id);
190
191 count = bios_dsk->drqp.nsect;
192
193 high_bits_save(&eax_hi);
194
195 /* Prepare a CDB. */
196 cdb.command = SCSI_WRITE_16;
197 cdb.lba = swap_64(bios_dsk->drqp.lba);
198 cdb.pad1 = 0;
199 cdb.nsect32 = swap_32(count);
200 cdb.pad2 = 0;
201
202 hba_seg = bios_dsk->scsidev[device_id].hba_seg;
203 idx_hba = bios_dsk->scsidev[device_id].idx_hba;
204 target_id = bios_dsk->scsidev[device_id].target_id;
205
206 DBG_SCSI("%s: writing %u sectors, device %d, target %d\n", __func__,
207 count, device_id, bios_dsk->scsidev[device_id].target_id);
208
209 rc = hbaacc[idx_hba].cmd_data_out(hba_seg :> 0, target_id, (void __far *)&cdb, 16,
210 bios_dsk->drqp.buffer, (count * 512L));
211 if (!rc)
212 {
213 bios_dsk->drqp.trsfsectors = count;
214 bios_dsk->drqp.trsfbytes = (count * 512L);
215 }
216 DBG_SCSI("%s: transferred %u sectors\n", __func__, bios_dsk->drqp.nsect);
217 high_bits_restore(eax_hi);
218
219 return rc;
220}
221
222
223/// @todo move
224#define ATA_DATA_NO 0x00
225#define ATA_DATA_IN 0x01
226#define ATA_DATA_OUT 0x02
227
228/**
229 * Perform a "packet style" read with supplied CDB.
230 *
231 * @returns status code.
232 * @param device_id ID of the device to access.
233 * @param cmdlen Length of the CDB.
234 * @param cmdbuf The CDB buffer.
235 * @param length How much to transfer.
236 * @param inout Read/Write direction indicator.
237 * @param buffer Data buffer to store the data from the device in.
238 */
239uint16_t scsi_cmd_packet(uint16_t device_id, uint8_t cmdlen, char __far *cmdbuf,
240 uint32_t length, uint8_t inout, char __far *buffer)
241{
242 bio_dsk_t __far *bios_dsk = read_word(0x0040, 0x000E) :> &EbdaData->bdisk;
243 uint8_t rc;
244 uint8_t target_id;
245 uint16_t hba_seg;
246 uint8_t idx_hba;
247 uint16_t eax_hi;
248
249 /* Data out is currently not supported. */
250 if (inout == ATA_DATA_OUT) {
251 BX_INFO("%s: DATA_OUT not supported yet\n", __func__);
252 return 1;
253 }
254
255 /* Convert to SCSI specific device number. */
256 device_id = VBOX_GET_SCSI_DEVICE(device_id);
257
258 DBG_SCSI("%s: reading %lu bytes, device %d, target %d\n", __func__,
259 length, device_id, bios_dsk->scsidev[device_id].target_id);
260 DBG_SCSI("%s: reading %u %u-byte sectors\n", __func__,
261 bios_dsk->drqp.nsect, bios_dsk->drqp.sect_sz);
262
263 high_bits_save(&eax_hi);
264 hba_seg = bios_dsk->scsidev[device_id].hba_seg;
265 idx_hba = bios_dsk->scsidev[device_id].idx_hba;
266 target_id = bios_dsk->scsidev[device_id].target_id;
267
268 bios_dsk->drqp.lba = length << 8; /// @todo xfer length limit
269 bios_dsk->drqp.buffer = buffer;
270 bios_dsk->drqp.nsect = length / bios_dsk->drqp.sect_sz;
271
272 DBG_SCSI("%s: reading %u bytes, device %d, target %d\n", __func__,
273 length, device_id, bios_dsk->scsidev[device_id].target_id);
274
275 rc = hbaacc[idx_hba].cmd_data_in(hba_seg :> 0, target_id, (void __far *)cmdbuf, cmdlen,
276 bios_dsk->drqp.buffer, length);
277 if (!rc)
278 bios_dsk->drqp.trsfbytes = length;
279
280 DBG_SCSI("%s: transferred %u bytes\n", __func__, length);
281 high_bits_restore(eax_hi);
282
283 return rc;
284}
285
286/**
287 * Enumerate attached devices.
288 *
289 * @returns nothing.
290 * @param hba_seg Segement of the HBA controller block.
291 * @param idx_hba The HBA driver index used for accessing the enumerated devices.
292 */
293static void scsi_enumerate_attached_devices(uint16_t hba_seg, uint8_t idx_hba)
294{
295 int i;
296 uint8_t buffer[0x0200];
297 bio_dsk_t __far *bios_dsk;
298
299 bios_dsk = read_word(0x0040, 0x000E) :> &EbdaData->bdisk;
300
301 /* Go through target devices. */
302 for (i = 0; i < VBSCSI_MAX_DEVICES; i++)
303 {
304 uint8_t rc;
305 uint8_t aCDB[16];
306 uint8_t hd_index, devcount_scsi;
307
308 aCDB[0] = SCSI_INQUIRY;
309 aCDB[1] = 0;
310 aCDB[2] = 0;
311 aCDB[3] = 0;
312 aCDB[4] = 5; /* Allocation length. */
313 aCDB[5] = 0;
314
315 rc = hbaacc[idx_hba].cmd_data_in(hba_seg :> 0, i, aCDB, 6, buffer, 5);
316 if (rc != 0)
317 {
318 DBG_SCSI("%s: SCSI_INQUIRY failed\n", __func__); /* Not a fatal error if the device doesn't exist. */
319 continue;
320 }
321
322 devcount_scsi = bios_dsk->scsi_devcount;
323
324 /* Check the attached device. */
325 if ( ((buffer[0] & 0xe0) == 0)
326 && ((buffer[0] & 0x1f) == 0x00))
327 {
328 DBG_SCSI("%s: Disk detected at %d\n", __func__, i);
329
330 /* We add the disk only if the maximum is not reached yet. */
331 if (devcount_scsi < BX_MAX_SCSI_DEVICES)
332 {
333 uint64_t sectors, t;
334 uint32_t sector_size, cylinders;
335 uint16_t heads, sectors_per_track;
336 uint8_t hdcount;
337 uint8_t cmos_base;
338
339 /* Issue a read capacity command now. */
340 _fmemset(aCDB, 0, sizeof(aCDB));
341 aCDB[0] = SCSI_SERVICE_ACT;
342 aCDB[1] = SCSI_READ_CAP_16;
343 aCDB[13] = 32; /* Allocation length. */
344
345 rc = hbaacc[idx_hba].cmd_data_in(hba_seg :> 0, i, aCDB, 16, buffer, 32);
346 if (rc != 0)
347 BX_PANIC("%s: SCSI_READ_CAPACITY failed\n", __func__);
348
349 /* The value returned is the last addressable LBA, not
350 * the size, which what "+ 1" is for.
351 */
352 sectors = swap_64(*(uint64_t *)buffer) + 1;
353
354 sector_size = ((uint32_t)buffer[8] << 24)
355 | ((uint32_t)buffer[9] << 16)
356 | ((uint32_t)buffer[10] << 8)
357 | ((uint32_t)buffer[11]);
358
359 /* We only support the disk if sector size is 512 bytes. */
360 if (sector_size != 512)
361 {
362 /* Leave a log entry. */
363 BX_INFO("Disk %d has an unsupported sector size of %u\n", i, sector_size);
364 continue;
365 }
366
367 /* Get logical CHS geometry. */
368 switch (devcount_scsi)
369 {
370 case 0:
371 cmos_base = 0x90;
372 break;
373 case 1:
374 cmos_base = 0x98;
375 break;
376 case 2:
377 cmos_base = 0xA0;
378 break;
379 case 3:
380 cmos_base = 0xA8;
381 break;
382 default:
383 cmos_base = 0;
384 }
385
386 if (cmos_base && inb_cmos(cmos_base + 7))
387 {
388 /* If provided, grab the logical geometry from CMOS. */
389 cylinders = inb_cmos(cmos_base + 0) + (inb_cmos(cmos_base + 1) << 8);
390 heads = inb_cmos(cmos_base + 2);
391 sectors_per_track = inb_cmos(cmos_base + 7);
392 }
393 else
394 {
395 /* Calculate default logical geometry. NB: Very different
396 * from default ATA/SATA logical geometry!
397 */
398 if (sectors >= (uint32_t)4 * 1024 * 1024)
399 {
400 heads = 255;
401 sectors_per_track = 63;
402 /* Approximate x / (255 * 63) using shifts */
403 t = (sectors >> 6) + (sectors >> 12);
404 cylinders = (t >> 8) + (t >> 16);
405 }
406 else if (sectors >= (uint32_t)2 * 1024 * 1024)
407 {
408 heads = 128;
409 sectors_per_track = 32;
410 cylinders = sectors >> 12;
411 }
412 else
413 {
414 heads = 64;
415 sectors_per_track = 32;
416 cylinders = sectors >> 11;
417 }
418 }
419
420 /* Calculate index into the generic disk table. */
421 hd_index = devcount_scsi + BX_MAX_ATA_DEVICES;
422
423 bios_dsk->scsidev[devcount_scsi].hba_seg = hba_seg;
424 bios_dsk->scsidev[devcount_scsi].idx_hba = idx_hba;
425 bios_dsk->scsidev[devcount_scsi].target_id = i;
426 bios_dsk->devices[hd_index].type = DSK_TYPE_SCSI;
427 bios_dsk->devices[hd_index].device = DSK_DEVICE_HD;
428 bios_dsk->devices[hd_index].removable = 0;
429 bios_dsk->devices[hd_index].lock = 0;
430 bios_dsk->devices[hd_index].blksize = sector_size;
431 bios_dsk->devices[hd_index].translation = GEO_TRANSLATION_LBA;
432
433 /* Write LCHS/PCHS values. */
434 bios_dsk->devices[hd_index].lchs.heads = heads;
435 bios_dsk->devices[hd_index].lchs.spt = sectors_per_track;
436 bios_dsk->devices[hd_index].pchs.heads = heads;
437 bios_dsk->devices[hd_index].pchs.spt = sectors_per_track;
438
439 if (cylinders > 1024) {
440 bios_dsk->devices[hd_index].lchs.cylinders = 1024;
441 bios_dsk->devices[hd_index].pchs.cylinders = 1024;
442 } else {
443 bios_dsk->devices[hd_index].lchs.cylinders = (uint16_t)cylinders;
444 bios_dsk->devices[hd_index].pchs.cylinders = (uint16_t)cylinders;
445 }
446
447 BX_INFO("SCSI %d-ID#%d: LCHS=%lu/%u/%u 0x%llx sectors\n", devcount_scsi,
448 i, (uint32_t)cylinders, heads, sectors_per_track, sectors);
449
450 bios_dsk->devices[hd_index].sectors = sectors;
451
452 /* Store the id of the disk in the ata hdidmap. */
453 hdcount = bios_dsk->hdcount;
454 bios_dsk->hdidmap[hdcount] = devcount_scsi + BX_MAX_ATA_DEVICES;
455 hdcount++;
456 bios_dsk->hdcount = hdcount;
457
458 /* Update hdcount in the BDA. */
459 hdcount = read_byte(0x40, 0x75);
460 hdcount++;
461 write_byte(0x40, 0x75, hdcount);
462
463 devcount_scsi++;
464 }
465 else
466 {
467 /* We reached the maximum of SCSI disks we can boot from. We can quit detecting. */
468 break;
469 }
470 }
471 else if ( ((buffer[0] & 0xe0) == 0)
472 && ((buffer[0] & 0x1f) == 0x05))
473 {
474 uint8_t cdcount;
475 uint8_t removable;
476
477 BX_INFO("SCSI %d-ID#%d: CD/DVD-ROM\n", devcount_scsi, i);
478
479 /* Calculate index into the generic device table. */
480 hd_index = devcount_scsi + BX_MAX_ATA_DEVICES;
481
482 removable = buffer[1] & 0x80 ? 1 : 0;
483
484 bios_dsk->scsidev[devcount_scsi].hba_seg = hba_seg;
485 bios_dsk->scsidev[devcount_scsi].idx_hba = idx_hba;
486 bios_dsk->scsidev[devcount_scsi].target_id = i;
487 bios_dsk->devices[hd_index].type = DSK_TYPE_SCSI;
488 bios_dsk->devices[hd_index].device = DSK_DEVICE_CDROM;
489 bios_dsk->devices[hd_index].removable = removable;
490 bios_dsk->devices[hd_index].blksize = 2048;
491 bios_dsk->devices[hd_index].translation = GEO_TRANSLATION_NONE;
492
493 /* Store the ID of the device in the BIOS cdidmap. */
494 cdcount = bios_dsk->cdcount;
495 bios_dsk->cdidmap[cdcount] = devcount_scsi + BX_MAX_ATA_DEVICES;
496 cdcount++;
497 bios_dsk->cdcount = cdcount;
498
499 devcount_scsi++;
500 }
501 else
502 DBG_SCSI("%s: No supported device detected at %d\n", __func__, i);
503
504 bios_dsk->scsi_devcount = devcount_scsi;
505 }
506}
507
508/**
509 * Init the SCSI driver and detect attached disks.
510 */
511void BIOSCALL scsi_init(void)
512{
513 int i;
514 bio_dsk_t __far *bios_dsk;
515
516 bios_dsk = read_word(0x0040, 0x000E) :> &EbdaData->bdisk;
517 bios_dsk->scsi_devcount = 0;
518
519 /* Walk the supported drivers and try to detect the HBA. */
520 for (i = 0; i < sizeof(hbaacc)/sizeof(hbaacc[0]); i++)
521 {
522 uint16_t busdevfn = pci_find_device(hbaacc[i].idPciVendor, hbaacc[i].idPciDevice);
523 if (busdevfn != VBOX_SCSI_NO_HBA)
524 {
525 int rc;
526 uint8_t u8Bus, u8DevFn;
527 uint16_t hba_seg = scsi_hba_mem_alloc();
528 if (hba_seg == 0) /* No point in trying the rest if we are out of memory. */
529 break;
530
531 u8Bus = (busdevfn & 0xff00) >> 8;
532 u8DevFn = busdevfn & 0x00ff;
533
534 DBG_SCSI("SCSI HBA at Bus %u DevFn 0x%x (raw 0x%x)\n", u8Bus, u8DevFn, busdevfn);
535 rc = hbaacc[i].init(hba_seg :> 0, u8Bus, u8DevFn);
536 if (!rc)
537 scsi_enumerate_attached_devices(hba_seg, i);
538 /** @todo Free memory on error. */
539 }
540 }
541}
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette