VirtualBox

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

Last change on this file since 99739 was 99739, checked in by vboxsync, 18 months ago

*: doxygen corrections (mostly about removing @returns from functions returning void).

  • 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 99739 2023-05-11 01:01:08Z 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 * @param hba_seg Segement of the HBA controller block.
300 * @param idx_hba The HBA driver index used for accessing the enumerated devices.
301 */
302static void scsi_enumerate_attached_devices(uint16_t hba_seg, uint8_t idx_hba)
303{
304 int i;
305 uint8_t buffer[0x0200];
306 bio_dsk_t __far *bios_dsk;
307
308 bios_dsk = read_word(0x0040, 0x000E) :> &EbdaData->bdisk;
309
310 /* Go through target devices. */
311 for (i = 0; i < VBSCSI_MAX_DEVICES; i++)
312 {
313 uint8_t rc;
314 uint8_t aCDB[16];
315 uint8_t hd_index, devcount_scsi;
316
317 aCDB[0] = SCSI_INQUIRY;
318 aCDB[1] = 0;
319 aCDB[2] = 0;
320 aCDB[3] = 0;
321 aCDB[4] = 5; /* Allocation length. */
322 aCDB[5] = 0;
323
324 rc = hbaacc[idx_hba].cmd_data_in(hba_seg :> 0, i, aCDB, 6, buffer, 5);
325 if (rc != 0)
326 {
327 DBG_SCSI("%s: SCSI_INQUIRY failed\n", __func__); /* Not a fatal error if the device doesn't exist. */
328 continue;
329 }
330
331 devcount_scsi = bios_dsk->scsi_devcount;
332
333 /* Check the attached device. */
334 if ( ((buffer[0] & 0xe0) == 0)
335 && ((buffer[0] & 0x1f) == 0x00))
336 {
337 DBG_SCSI("%s: Disk detected at %d\n", __func__, i);
338
339 /* We add the disk only if the maximum is not reached yet. */
340 if (devcount_scsi < BX_MAX_SCSI_DEVICES)
341 {
342 uint64_t sectors, t;
343 uint32_t sector_size, cylinders;
344 uint16_t heads, sectors_per_track;
345 uint8_t hdcount;
346 uint8_t cmos_base;
347
348 /* Issue a read capacity command now. */
349 _fmemset(aCDB, 0, sizeof(aCDB));
350 aCDB[0] = SCSI_SERVICE_ACT;
351 aCDB[1] = SCSI_READ_CAP_16;
352 aCDB[13] = 32; /* Allocation length. */
353
354 rc = hbaacc[idx_hba].cmd_data_in(hba_seg :> 0, i, aCDB, 16, buffer, 32);
355 if (rc != 0)
356 BX_PANIC("%s: SCSI_READ_CAPACITY failed\n", __func__);
357
358 /* The value returned is the last addressable LBA, not
359 * the size, which what "+ 1" is for.
360 */
361 sectors = swap_64(*(uint64_t *)buffer) + 1;
362
363 sector_size = ((uint32_t)buffer[8] << 24)
364 | ((uint32_t)buffer[9] << 16)
365 | ((uint32_t)buffer[10] << 8)
366 | ((uint32_t)buffer[11]);
367
368 /* We only support the disk if sector size is 512 bytes. */
369 if (sector_size != 512)
370 {
371 /* Leave a log entry. */
372 BX_INFO("Disk %d has an unsupported sector size of %u\n", i, sector_size);
373 continue;
374 }
375
376 /* Get logical CHS geometry. */
377 switch (devcount_scsi)
378 {
379 case 0:
380 cmos_base = 0x90;
381 break;
382 case 1:
383 cmos_base = 0x98;
384 break;
385 case 2:
386 cmos_base = 0xA0;
387 break;
388 case 3:
389 cmos_base = 0xA8;
390 break;
391 default:
392 cmos_base = 0;
393 }
394
395 if (cmos_base && inb_cmos(cmos_base + 7))
396 {
397 /* If provided, grab the logical geometry from CMOS. */
398 cylinders = get_cmos_word(cmos_base /*, cmos_base + 1*/);
399 heads = inb_cmos(cmos_base + 2);
400 sectors_per_track = inb_cmos(cmos_base + 7);
401 }
402 else
403 {
404 /* Calculate default logical geometry. NB: Very different
405 * from default ATA/SATA logical geometry!
406 */
407 if (sectors >= (uint32_t)4 * 1024 * 1024)
408 {
409 heads = 255;
410 sectors_per_track = 63;
411 /* Approximate x / (255 * 63) using shifts */
412 t = (sectors >> 6) + (sectors >> 12);
413 cylinders = (t >> 8) + (t >> 16);
414 }
415 else if (sectors >= (uint32_t)2 * 1024 * 1024)
416 {
417 heads = 128;
418 sectors_per_track = 32;
419 cylinders = sectors >> 12;
420 }
421 else
422 {
423 heads = 64;
424 sectors_per_track = 32;
425 cylinders = sectors >> 11;
426 }
427 }
428
429 /* Calculate index into the generic disk table. */
430 hd_index = devcount_scsi + BX_MAX_ATA_DEVICES;
431
432 bios_dsk->scsidev[devcount_scsi].hba_seg = hba_seg;
433 bios_dsk->scsidev[devcount_scsi].idx_hba = idx_hba;
434 bios_dsk->scsidev[devcount_scsi].target_id = i;
435 bios_dsk->devices[hd_index].type = DSK_TYPE_SCSI;
436 bios_dsk->devices[hd_index].device = DSK_DEVICE_HD;
437 bios_dsk->devices[hd_index].removable = 0;
438 bios_dsk->devices[hd_index].lock = 0;
439 bios_dsk->devices[hd_index].blksize = sector_size;
440 bios_dsk->devices[hd_index].translation = GEO_TRANSLATION_LBA;
441
442 /* Write LCHS/PCHS values. */
443 bios_dsk->devices[hd_index].lchs.heads = heads;
444 bios_dsk->devices[hd_index].lchs.spt = sectors_per_track;
445 bios_dsk->devices[hd_index].pchs.heads = heads;
446 bios_dsk->devices[hd_index].pchs.spt = sectors_per_track;
447
448 if (cylinders > 1024) {
449 bios_dsk->devices[hd_index].lchs.cylinders = 1024;
450 bios_dsk->devices[hd_index].pchs.cylinders = 1024;
451 } else {
452 bios_dsk->devices[hd_index].lchs.cylinders = (uint16_t)cylinders;
453 bios_dsk->devices[hd_index].pchs.cylinders = (uint16_t)cylinders;
454 }
455
456 BX_INFO("SCSI %d-ID#%d: LCHS=%lu/%u/%u 0x%llx sectors\n", devcount_scsi,
457 i, (uint32_t)cylinders, heads, sectors_per_track, sectors);
458
459 bios_dsk->devices[hd_index].sectors = sectors;
460
461 /* Store the id of the disk in the ata hdidmap. */
462 hdcount = bios_dsk->hdcount;
463 bios_dsk->hdidmap[hdcount] = devcount_scsi + BX_MAX_ATA_DEVICES;
464 hdcount++;
465 bios_dsk->hdcount = hdcount;
466
467 /* Update hdcount in the BDA. */
468 hdcount = read_byte(0x40, 0x75);
469 hdcount++;
470 write_byte(0x40, 0x75, hdcount);
471
472 devcount_scsi++;
473 }
474 else
475 {
476 /* We reached the maximum of SCSI disks we can boot from. We can quit detecting. */
477 break;
478 }
479 }
480 else if ( ((buffer[0] & 0xe0) == 0)
481 && ((buffer[0] & 0x1f) == 0x05))
482 {
483 uint8_t cdcount;
484 uint8_t removable;
485
486 BX_INFO("SCSI %d-ID#%d: CD/DVD-ROM\n", devcount_scsi, i);
487
488 /* Calculate index into the generic device table. */
489 hd_index = devcount_scsi + BX_MAX_ATA_DEVICES;
490
491 removable = buffer[1] & 0x80 ? 1 : 0;
492
493 bios_dsk->scsidev[devcount_scsi].hba_seg = hba_seg;
494 bios_dsk->scsidev[devcount_scsi].idx_hba = idx_hba;
495 bios_dsk->scsidev[devcount_scsi].target_id = i;
496 bios_dsk->devices[hd_index].type = DSK_TYPE_SCSI;
497 bios_dsk->devices[hd_index].device = DSK_DEVICE_CDROM;
498 bios_dsk->devices[hd_index].removable = removable;
499 bios_dsk->devices[hd_index].blksize = 2048;
500 bios_dsk->devices[hd_index].translation = GEO_TRANSLATION_NONE;
501
502 /* Store the ID of the device in the BIOS cdidmap. */
503 cdcount = bios_dsk->cdcount;
504 bios_dsk->cdidmap[cdcount] = devcount_scsi + BX_MAX_ATA_DEVICES;
505 cdcount++;
506 bios_dsk->cdcount = cdcount;
507
508 devcount_scsi++;
509 }
510 else
511 DBG_SCSI("%s: No supported device detected at %d\n", __func__, i);
512
513 bios_dsk->scsi_devcount = devcount_scsi;
514 }
515}
516
517/**
518 * Init the SCSI driver and detect attached disks.
519 */
520void BIOSCALL scsi_init(void)
521{
522 int i;
523 bio_dsk_t __far *bios_dsk;
524
525 bios_dsk = read_word(0x0040, 0x000E) :> &EbdaData->bdisk;
526 bios_dsk->scsi_devcount = 0;
527
528 /* Walk the supported drivers and try to detect the HBA. */
529 for (i = 0; i < sizeof(hbaacc)/sizeof(hbaacc[0]); i++)
530 {
531 uint16_t busdevfn = pci_find_device(hbaacc[i].idPciVendor, hbaacc[i].idPciDevice);
532 if (busdevfn != VBOX_SCSI_NO_HBA)
533 {
534 int rc;
535 uint8_t u8Bus, u8DevFn;
536 uint16_t hba_seg = scsi_hba_mem_alloc();
537 if (hba_seg == 0) /* No point in trying the rest if we are out of memory. */
538 break;
539
540 u8Bus = (busdevfn & 0xff00) >> 8;
541 u8DevFn = busdevfn & 0x00ff;
542
543 DBG_SCSI("SCSI HBA at Bus %u DevFn 0x%x (raw 0x%x)\n", u8Bus, u8DevFn, busdevfn);
544 rc = hbaacc[i].init(hba_seg :> 0, u8Bus, u8DevFn);
545 if (!rc)
546 scsi_enumerate_attached_devices(hba_seg, i);
547 /** @todo Free memory on error. */
548 }
549 }
550}
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