[38699] | 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 | /*
|
---|
[98103] | 7 | * Copyright (C) 2004-2023 Oracle and/or its affiliates.
|
---|
[38699] | 8 | *
|
---|
[96407] | 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
|
---|
[38699] | 26 | */
|
---|
| 27 |
|
---|
| 28 | #include <stdint.h>
|
---|
| 29 | #include <string.h>
|
---|
| 30 | #include "biosint.h"
|
---|
| 31 | #include "inlines.h"
|
---|
[67805] | 32 | #include "pciutil.h"
|
---|
[38699] | 33 | #include "ebda.h"
|
---|
[81412] | 34 | #include "scsi.h"
|
---|
[38699] | 35 |
|
---|
| 36 |
|
---|
[43475] | 37 | #if DEBUG_SCSI
|
---|
| 38 | # define DBG_SCSI(...) BX_INFO(__VA_ARGS__)
|
---|
[39355] | 39 | #else
|
---|
[43475] | 40 | # define DBG_SCSI(...)
|
---|
[39355] | 41 | #endif
|
---|
| 42 |
|
---|
[89168] | 43 | #define VBSCSI_MAX_DEVICES 16 /* Maximum number of devices a SCSI device currently supported. */
|
---|
[39355] | 44 |
|
---|
[89168] | 45 | #define VBOX_SCSI_NO_HBA 0xffff
|
---|
[38699] | 46 |
|
---|
[89364] | 47 | typedef int (* scsi_hba_init)(void __far *pvHba, uint8_t u8Bus, uint8_t u8DevFn);
|
---|
[89168] | 48 | typedef 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);
|
---|
| 50 | typedef int (* scsi_hba_cmd_data_in)(void __far *pvHba, uint8_t idTgt, uint8_t __far *aCDB,
|
---|
[89364] | 51 | uint8_t cbCDB, uint8_t __far *buffer, uint32_t length);
|
---|
[38699] | 52 |
|
---|
[89168] | 53 | typedef 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;
|
---|
[38699] | 61 |
|
---|
[89168] | 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 | */
|
---|
[38699] | 67 |
|
---|
[89168] | 68 | /* Note: Reading high EAX bits destroys them - *must* be restored later. */
|
---|
| 69 | uint16_t eax_hi_rd(void);
|
---|
| 70 | #pragma aux eax_hi_rd = \
|
---|
| 71 | ".386" \
|
---|
| 72 | "shr eax, 16" \
|
---|
| 73 | value [ax] modify nomemory;
|
---|
[43475] | 74 |
|
---|
[89168] | 75 | void eax_hi_wr(uint16_t);
|
---|
| 76 | #pragma aux eax_hi_wr = \
|
---|
| 77 | ".386" \
|
---|
| 78 | "shl eax, 16" \
|
---|
| 79 | parm [ax] modify nomemory;
|
---|
[43475] | 80 |
|
---|
[89168] | 81 | void inline high_bits_save(uint16_t __far *pu16EaxHi)
|
---|
[38699] | 82 | {
|
---|
[89168] | 83 | *pu16EaxHi = eax_hi_rd();
|
---|
| 84 | }
|
---|
[38699] | 85 |
|
---|
[89168] | 86 | void inline high_bits_restore(uint16_t u16EaxHi)
|
---|
| 87 | {
|
---|
| 88 | eax_hi_wr(u16EaxHi);
|
---|
[38699] | 89 | }
|
---|
| 90 |
|
---|
[89168] | 91 | /* Pointers to the HBA specific access routines. */
|
---|
| 92 | scsi_hba_t hbaacc[] =
|
---|
[38699] | 93 | {
|
---|
[89168] | 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 | };
|
---|
[38699] | 101 |
|
---|
[89168] | 102 | /**
|
---|
| 103 | * Allocates 1K of conventional memory.
|
---|
| 104 | */
|
---|
| 105 | static uint16_t scsi_hba_mem_alloc(void)
|
---|
| 106 | {
|
---|
| 107 | uint16_t base_mem_kb;
|
---|
| 108 | uint16_t hba_seg;
|
---|
[38699] | 109 |
|
---|
[89168] | 110 | base_mem_kb = read_word(0x00, 0x0413);
|
---|
[48123] | 111 |
|
---|
[89168] | 112 | DBG_SCSI("SCSI: %dK of base mem\n", base_mem_kb);
|
---|
[38699] | 113 |
|
---|
[89168] | 114 | if (base_mem_kb == 0)
|
---|
| 115 | return 0;
|
---|
[38699] | 116 |
|
---|
[89168] | 117 | base_mem_kb--; /* Allocate one block. */
|
---|
| 118 | hba_seg = (((uint32_t)base_mem_kb * 1024) >> 4); /* Calculate start segment. */
|
---|
[46241] | 119 |
|
---|
[89168] | 120 | write_word(0x00, 0x0413, base_mem_kb);
|
---|
[38699] | 121 |
|
---|
[89168] | 122 | return hba_seg;
|
---|
[38699] | 123 | }
|
---|
| 124 |
|
---|
| 125 | /**
|
---|
[39366] | 126 | * Read sectors from an attached SCSI device.
|
---|
[38699] | 127 | *
|
---|
| 128 | * @returns status code.
|
---|
[48123] | 129 | * @param bios_dsk Pointer to disk request packet (in the
|
---|
[39366] | 130 | * EBDA).
|
---|
[38699] | 131 | */
|
---|
[39366] | 132 | int scsi_read_sectors(bio_dsk_t __far *bios_dsk)
|
---|
[38699] | 133 | {
|
---|
| 134 | uint8_t rc;
|
---|
[58724] | 135 | cdb_rw16 cdb;
|
---|
| 136 | uint32_t count;
|
---|
[89168] | 137 | uint16_t hba_seg;
|
---|
| 138 | uint8_t idx_hba;
|
---|
[38699] | 139 | uint8_t target_id;
|
---|
[39366] | 140 | uint8_t device_id;
|
---|
[89168] | 141 | uint16_t eax_hi;
|
---|
[38699] | 142 |
|
---|
[42811] | 143 | device_id = VBOX_GET_SCSI_DEVICE(bios_dsk->drqp.dev_id);
|
---|
[38699] | 144 | if (device_id > BX_MAX_SCSI_DEVICES)
|
---|
[50176] | 145 | BX_PANIC("%s: device_id out of range %d\n", __func__, device_id);
|
---|
[38699] | 146 |
|
---|
[39366] | 147 | count = bios_dsk->drqp.nsect;
|
---|
| 148 |
|
---|
[89168] | 149 | high_bits_save(&eax_hi);
|
---|
| 150 |
|
---|
[39355] | 151 | /* Prepare a CDB. */
|
---|
[58724] | 152 | cdb.command = SCSI_READ_16;
|
---|
| 153 | cdb.lba = swap_64(bios_dsk->drqp.lba);
|
---|
[39355] | 154 | cdb.pad1 = 0;
|
---|
[58724] | 155 | cdb.nsect32 = swap_32(count);
|
---|
[39355] | 156 | cdb.pad2 = 0;
|
---|
| 157 |
|
---|
[38699] | 158 |
|
---|
[89168] | 159 | hba_seg = bios_dsk->scsidev[device_id].hba_seg;
|
---|
| 160 | idx_hba = bios_dsk->scsidev[device_id].idx_hba;
|
---|
[39347] | 161 | target_id = bios_dsk->scsidev[device_id].target_id;
|
---|
[38699] | 162 |
|
---|
[46240] | 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 |
|
---|
[89168] | 166 | rc = hbaacc[idx_hba].cmd_data_in(hba_seg :> 0, target_id, (void __far *)&cdb, 16,
|
---|
[89364] | 167 | bios_dsk->drqp.buffer, (count * 512L));
|
---|
[38699] | 168 | if (!rc)
|
---|
| 169 | {
|
---|
[39355] | 170 | bios_dsk->drqp.trsfsectors = count;
|
---|
[43475] | 171 | bios_dsk->drqp.trsfbytes = count * 512L;
|
---|
[38699] | 172 | }
|
---|
[46240] | 173 | DBG_SCSI("%s: transferred %u sectors\n", __func__, bios_dsk->drqp.nsect);
|
---|
[89168] | 174 | high_bits_restore(eax_hi);
|
---|
[38699] | 175 |
|
---|
| 176 | return rc;
|
---|
| 177 | }
|
---|
| 178 |
|
---|
| 179 | /**
|
---|
[39366] | 180 | * Write sectors to an attached SCSI device.
|
---|
[38699] | 181 | *
|
---|
| 182 | * @returns status code.
|
---|
[48123] | 183 | * @param bios_dsk Pointer to disk request packet (in the
|
---|
[39366] | 184 | * EBDA).
|
---|
[38699] | 185 | */
|
---|
[39366] | 186 | int scsi_write_sectors(bio_dsk_t __far *bios_dsk)
|
---|
[38699] | 187 | {
|
---|
| 188 | uint8_t rc;
|
---|
[58724] | 189 | cdb_rw16 cdb;
|
---|
| 190 | uint32_t count;
|
---|
[89168] | 191 | uint16_t hba_seg;
|
---|
| 192 | uint8_t idx_hba;
|
---|
[38699] | 193 | uint8_t target_id;
|
---|
[39366] | 194 | uint8_t device_id;
|
---|
[89168] | 195 | uint16_t eax_hi;
|
---|
[38699] | 196 |
|
---|
[42811] | 197 | device_id = VBOX_GET_SCSI_DEVICE(bios_dsk->drqp.dev_id);
|
---|
[38699] | 198 | if (device_id > BX_MAX_SCSI_DEVICES)
|
---|
[50176] | 199 | BX_PANIC("%s: device_id out of range %d\n", __func__, device_id);
|
---|
[38699] | 200 |
|
---|
[39366] | 201 | count = bios_dsk->drqp.nsect;
|
---|
| 202 |
|
---|
[89168] | 203 | high_bits_save(&eax_hi);
|
---|
| 204 |
|
---|
[39355] | 205 | /* Prepare a CDB. */
|
---|
[58724] | 206 | cdb.command = SCSI_WRITE_16;
|
---|
| 207 | cdb.lba = swap_64(bios_dsk->drqp.lba);
|
---|
[39355] | 208 | cdb.pad1 = 0;
|
---|
[58724] | 209 | cdb.nsect32 = swap_32(count);
|
---|
[39355] | 210 | cdb.pad2 = 0;
|
---|
| 211 |
|
---|
[89168] | 212 | hba_seg = bios_dsk->scsidev[device_id].hba_seg;
|
---|
| 213 | idx_hba = bios_dsk->scsidev[device_id].idx_hba;
|
---|
[39347] | 214 | target_id = bios_dsk->scsidev[device_id].target_id;
|
---|
[38699] | 215 |
|
---|
[46240] | 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 |
|
---|
[89168] | 219 | rc = hbaacc[idx_hba].cmd_data_out(hba_seg :> 0, target_id, (void __far *)&cdb, 16,
|
---|
| 220 | bios_dsk->drqp.buffer, (count * 512L));
|
---|
[38699] | 221 | if (!rc)
|
---|
| 222 | {
|
---|
[39355] | 223 | bios_dsk->drqp.trsfsectors = count;
|
---|
[43475] | 224 | bios_dsk->drqp.trsfbytes = (count * 512L);
|
---|
[38699] | 225 | }
|
---|
[46240] | 226 | DBG_SCSI("%s: transferred %u sectors\n", __func__, bios_dsk->drqp.nsect);
|
---|
[89168] | 227 | high_bits_restore(eax_hi);
|
---|
[38699] | 228 |
|
---|
| 229 | return rc;
|
---|
| 230 | }
|
---|
| 231 |
|
---|
[43475] | 232 |
|
---|
[63562] | 233 | /// @todo move
|
---|
[43475] | 234 | #define ATA_DATA_NO 0x00
|
---|
| 235 | #define ATA_DATA_IN 0x01
|
---|
| 236 | #define ATA_DATA_OUT 0x02
|
---|
| 237 |
|
---|
[38699] | 238 | /**
|
---|
[43475] | 239 | * Perform a "packet style" read with supplied CDB.
|
---|
| 240 | *
|
---|
| 241 | * @returns status code.
|
---|
[64369] | 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.
|
---|
[43475] | 248 | */
|
---|
[48123] | 249 | uint16_t scsi_cmd_packet(uint16_t device_id, uint8_t cmdlen, char __far *cmdbuf,
|
---|
[89364] | 250 | uint32_t length, uint8_t inout, char __far *buffer)
|
---|
[43475] | 251 | {
|
---|
| 252 | bio_dsk_t __far *bios_dsk = read_word(0x0040, 0x000E) :> &EbdaData->bdisk;
|
---|
[89168] | 253 | uint8_t rc;
|
---|
[43475] | 254 | uint8_t target_id;
|
---|
[89168] | 255 | uint16_t hba_seg;
|
---|
| 256 | uint8_t idx_hba;
|
---|
| 257 | uint16_t eax_hi;
|
---|
[43475] | 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 |
|
---|
[89364] | 268 | DBG_SCSI("%s: reading %lu bytes, device %d, target %d\n", __func__,
|
---|
| 269 | length, device_id, bios_dsk->scsidev[device_id].target_id);
|
---|
[43475] | 270 | DBG_SCSI("%s: reading %u %u-byte sectors\n", __func__,
|
---|
| 271 | bios_dsk->drqp.nsect, bios_dsk->drqp.sect_sz);
|
---|
| 272 |
|
---|
[89168] | 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;
|
---|
[43475] | 276 | target_id = bios_dsk->scsidev[device_id].target_id;
|
---|
| 277 |
|
---|
[89168] | 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;
|
---|
[43475] | 281 |
|
---|
[89168] | 282 | DBG_SCSI("%s: reading %u bytes, device %d, target %d\n", __func__,
|
---|
| 283 | length, device_id, bios_dsk->scsidev[device_id].target_id);
|
---|
[43672] | 284 |
|
---|
[89168] | 285 | rc = hbaacc[idx_hba].cmd_data_in(hba_seg :> 0, target_id, (void __far *)cmdbuf, cmdlen,
|
---|
[89364] | 286 | bios_dsk->drqp.buffer, length);
|
---|
[89168] | 287 | if (!rc)
|
---|
| 288 | bios_dsk->drqp.trsfbytes = length;
|
---|
[43475] | 289 |
|
---|
[89168] | 290 | DBG_SCSI("%s: transferred %u bytes\n", __func__, length);
|
---|
| 291 | high_bits_restore(eax_hi);
|
---|
[43475] | 292 |
|
---|
[89168] | 293 | return rc;
|
---|
[43475] | 294 | }
|
---|
| 295 |
|
---|
| 296 | /**
|
---|
[38699] | 297 | * Enumerate attached devices.
|
---|
| 298 | *
|
---|
[89169] | 299 | * @param hba_seg Segement of the HBA controller block.
|
---|
| 300 | * @param idx_hba The HBA driver index used for accessing the enumerated devices.
|
---|
[38699] | 301 | */
|
---|
[89168] | 302 | static void scsi_enumerate_attached_devices(uint16_t hba_seg, uint8_t idx_hba)
|
---|
[38699] | 303 | {
|
---|
| 304 | int i;
|
---|
| 305 | uint8_t buffer[0x0200];
|
---|
[39347] | 306 | bio_dsk_t __far *bios_dsk;
|
---|
[38699] | 307 |
|
---|
[39347] | 308 | bios_dsk = read_word(0x0040, 0x000E) :> &EbdaData->bdisk;
|
---|
[38699] | 309 |
|
---|
| 310 | /* Go through target devices. */
|
---|
| 311 | for (i = 0; i < VBSCSI_MAX_DEVICES; i++)
|
---|
| 312 | {
|
---|
| 313 | uint8_t rc;
|
---|
[58724] | 314 | uint8_t aCDB[16];
|
---|
[43475] | 315 | uint8_t hd_index, devcount_scsi;
|
---|
[38699] | 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 |
|
---|
[89364] | 324 | rc = hbaacc[idx_hba].cmd_data_in(hba_seg :> 0, i, aCDB, 6, buffer, 5);
|
---|
[38699] | 325 | if (rc != 0)
|
---|
[89168] | 326 | {
|
---|
| 327 | DBG_SCSI("%s: SCSI_INQUIRY failed\n", __func__); /* Not a fatal error if the device doesn't exist. */
|
---|
| 328 | continue;
|
---|
| 329 | }
|
---|
[38699] | 330 |
|
---|
[55132] | 331 | devcount_scsi = bios_dsk->scsi_devcount;
|
---|
| 332 |
|
---|
[43475] | 333 | /* Check the attached device. */
|
---|
[38699] | 334 | if ( ((buffer[0] & 0xe0) == 0)
|
---|
| 335 | && ((buffer[0] & 0x1f) == 0x00))
|
---|
| 336 | {
|
---|
[43475] | 337 | DBG_SCSI("%s: Disk detected at %d\n", __func__, i);
|
---|
[38699] | 338 |
|
---|
| 339 | /* We add the disk only if the maximum is not reached yet. */
|
---|
[55132] | 340 | if (devcount_scsi < BX_MAX_SCSI_DEVICES)
|
---|
[38699] | 341 | {
|
---|
[58724] | 342 | uint64_t sectors, t;
|
---|
| 343 | uint32_t sector_size, cylinders;
|
---|
[38699] | 344 | uint16_t heads, sectors_per_track;
|
---|
[43475] | 345 | uint8_t hdcount;
|
---|
[50091] | 346 | uint8_t cmos_base;
|
---|
[38699] | 347 |
|
---|
| 348 | /* Issue a read capacity command now. */
|
---|
| 349 | _fmemset(aCDB, 0, sizeof(aCDB));
|
---|
[58724] | 350 | aCDB[0] = SCSI_SERVICE_ACT;
|
---|
| 351 | aCDB[1] = SCSI_READ_CAP_16;
|
---|
| 352 | aCDB[13] = 32; /* Allocation length. */
|
---|
[38699] | 353 |
|
---|
[89364] | 354 | rc = hbaacc[idx_hba].cmd_data_in(hba_seg :> 0, i, aCDB, 16, buffer, 32);
|
---|
[38699] | 355 | if (rc != 0)
|
---|
[43475] | 356 | BX_PANIC("%s: SCSI_READ_CAPACITY failed\n", __func__);
|
---|
[38699] | 357 |
|
---|
[58724] | 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;
|
---|
[38699] | 362 |
|
---|
[58724] | 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]);
|
---|
[38699] | 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 |
|
---|
[50091] | 376 | /* Get logical CHS geometry. */
|
---|
| 377 | switch (devcount_scsi)
|
---|
[38699] | 378 | {
|
---|
[50091] | 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;
|
---|
[38699] | 393 | }
|
---|
[50091] | 394 |
|
---|
| 395 | if (cmos_base && inb_cmos(cmos_base + 7))
|
---|
[38699] | 396 | {
|
---|
[50091] | 397 | /* If provided, grab the logical geometry from CMOS. */
|
---|
[92290] | 398 | cylinders = get_cmos_word(cmos_base /*, cmos_base + 1*/);
|
---|
[50091] | 399 | heads = inb_cmos(cmos_base + 2);
|
---|
| 400 | sectors_per_track = inb_cmos(cmos_base + 7);
|
---|
[38699] | 401 | }
|
---|
| 402 | else
|
---|
| 403 | {
|
---|
[50091] | 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;
|
---|
[58724] | 411 | /* Approximate x / (255 * 63) using shifts */
|
---|
| 412 | t = (sectors >> 6) + (sectors >> 12);
|
---|
| 413 | cylinders = (t >> 8) + (t >> 16);
|
---|
[50091] | 414 | }
|
---|
| 415 | else if (sectors >= (uint32_t)2 * 1024 * 1024)
|
---|
| 416 | {
|
---|
| 417 | heads = 128;
|
---|
| 418 | sectors_per_track = 32;
|
---|
[58724] | 419 | cylinders = sectors >> 12;
|
---|
[50091] | 420 | }
|
---|
| 421 | else
|
---|
| 422 | {
|
---|
| 423 | heads = 64;
|
---|
| 424 | sectors_per_track = 32;
|
---|
[58724] | 425 | cylinders = sectors >> 11;
|
---|
[50091] | 426 | }
|
---|
[38699] | 427 | }
|
---|
| 428 |
|
---|
[39346] | 429 | /* Calculate index into the generic disk table. */
|
---|
[43475] | 430 | hd_index = devcount_scsi + BX_MAX_ATA_DEVICES;
|
---|
[38699] | 431 |
|
---|
[89168] | 432 | bios_dsk->scsidev[devcount_scsi].hba_seg = hba_seg;
|
---|
| 433 | bios_dsk->scsidev[devcount_scsi].idx_hba = idx_hba;
|
---|
[43475] | 434 | bios_dsk->scsidev[devcount_scsi].target_id = i;
|
---|
[39651] | 435 | bios_dsk->devices[hd_index].type = DSK_TYPE_SCSI;
|
---|
| 436 | bios_dsk->devices[hd_index].device = DSK_DEVICE_HD;
|
---|
[39347] | 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;
|
---|
[39651] | 440 | bios_dsk->devices[hd_index].translation = GEO_TRANSLATION_LBA;
|
---|
[39346] | 441 |
|
---|
[58724] | 442 | /* Write LCHS/PCHS values. */
|
---|
[39347] | 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;
|
---|
[58724] | 447 |
|
---|
| 448 | if (cylinders > 1024) {
|
---|
| 449 | bios_dsk->devices[hd_index].lchs.cylinders = 1024;
|
---|
[39347] | 450 | bios_dsk->devices[hd_index].pchs.cylinders = 1024;
|
---|
[58724] | 451 | } else {
|
---|
| 452 | bios_dsk->devices[hd_index].lchs.cylinders = (uint16_t)cylinders;
|
---|
[39347] | 453 | bios_dsk->devices[hd_index].pchs.cylinders = (uint16_t)cylinders;
|
---|
[58724] | 454 | }
|
---|
[38699] | 455 |
|
---|
[58724] | 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 |
|
---|
[39347] | 459 | bios_dsk->devices[hd_index].sectors = sectors;
|
---|
[38699] | 460 |
|
---|
| 461 | /* Store the id of the disk in the ata hdidmap. */
|
---|
[39347] | 462 | hdcount = bios_dsk->hdcount;
|
---|
[43475] | 463 | bios_dsk->hdidmap[hdcount] = devcount_scsi + BX_MAX_ATA_DEVICES;
|
---|
[38699] | 464 | hdcount++;
|
---|
[39347] | 465 | bios_dsk->hdcount = hdcount;
|
---|
[38699] | 466 |
|
---|
| 467 | /* Update hdcount in the BDA. */
|
---|
| 468 | hdcount = read_byte(0x40, 0x75);
|
---|
| 469 | hdcount++;
|
---|
| 470 | write_byte(0x40, 0x75, hdcount);
|
---|
| 471 |
|
---|
[43475] | 472 | devcount_scsi++;
|
---|
[38699] | 473 | }
|
---|
| 474 | else
|
---|
| 475 | {
|
---|
| 476 | /* We reached the maximum of SCSI disks we can boot from. We can quit detecting. */
|
---|
| 477 | break;
|
---|
| 478 | }
|
---|
| 479 | }
|
---|
[43475] | 480 | else if ( ((buffer[0] & 0xe0) == 0)
|
---|
| 481 | && ((buffer[0] & 0x1f) == 0x05))
|
---|
| 482 | {
|
---|
| 483 | uint8_t cdcount;
|
---|
| 484 | uint8_t removable;
|
---|
| 485 |
|
---|
[43757] | 486 | BX_INFO("SCSI %d-ID#%d: CD/DVD-ROM\n", devcount_scsi, i);
|
---|
[43475] | 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 |
|
---|
[89168] | 493 | bios_dsk->scsidev[devcount_scsi].hba_seg = hba_seg;
|
---|
| 494 | bios_dsk->scsidev[devcount_scsi].idx_hba = idx_hba;
|
---|
[43475] | 495 | bios_dsk->scsidev[devcount_scsi].target_id = i;
|
---|
[70333] | 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;
|
---|
[43475] | 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 | }
|
---|
[38699] | 510 | else
|
---|
[43475] | 511 | DBG_SCSI("%s: No supported device detected at %d\n", __func__, i);
|
---|
[55132] | 512 |
|
---|
| 513 | bios_dsk->scsi_devcount = devcount_scsi;
|
---|
[38699] | 514 | }
|
---|
| 515 | }
|
---|
| 516 |
|
---|
| 517 | /**
|
---|
| 518 | * Init the SCSI driver and detect attached disks.
|
---|
| 519 | */
|
---|
| 520 | void BIOSCALL scsi_init(void)
|
---|
| 521 | {
|
---|
[89168] | 522 | int i;
|
---|
[39347] | 523 | bio_dsk_t __far *bios_dsk;
|
---|
[38699] | 524 |
|
---|
[39347] | 525 | bios_dsk = read_word(0x0040, 0x000E) :> &EbdaData->bdisk;
|
---|
[43475] | 526 | bios_dsk->scsi_devcount = 0;
|
---|
[38699] | 527 |
|
---|
[89168] | 528 | /* Walk the supported drivers and try to detect the HBA. */
|
---|
| 529 | for (i = 0; i < sizeof(hbaacc)/sizeof(hbaacc[0]); i++)
|
---|
[38699] | 530 | {
|
---|
[89168] | 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;
|
---|
[38699] | 539 |
|
---|
[89168] | 540 | u8Bus = (busdevfn & 0xff00) >> 8;
|
---|
| 541 | u8DevFn = busdevfn & 0x00ff;
|
---|
[38699] | 542 |
|
---|
[89168] | 543 | DBG_SCSI("SCSI HBA at Bus %u DevFn 0x%x (raw 0x%x)\n", u8Bus, u8DevFn, busdevfn);
|
---|
[89364] | 544 | rc = hbaacc[i].init(hba_seg :> 0, u8Bus, u8DevFn);
|
---|
[89168] | 545 | if (!rc)
|
---|
| 546 | scsi_enumerate_attached_devices(hba_seg, i);
|
---|
| 547 | /** @todo Free memory on error. */
|
---|
| 548 | }
|
---|
[38699] | 549 | }
|
---|
| 550 | }
|
---|