VirtualBox

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

Last change on this file was 106061, checked in by vboxsync, 4 months ago

Copyright year updates by scm.

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