VirtualBox

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

Last change on this file was 100984, checked in by vboxsync, 8 months ago

BIOS: Support also ISA BusLogic/Adaptec HBAs, not just PCI (see bugref:6549).

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

© 2023 Oracle
ContactPrivacy policyTerms of Use