VirtualBox

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

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