[38699] | 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 | /*
|
---|
[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 |
|
---|
[100984] | 45 | #define VBOX_SCSI_NO_HBA 0xffff
|
---|
[38699] | 46 |
|
---|
[100984] | 47 | typedef uint16_t (* scsi_hba_detect)(void);
|
---|
[89364] | 48 | typedef int (* scsi_hba_init)(void __far *pvHba, uint8_t u8Bus, uint8_t u8DevFn);
|
---|
[89168] | 49 | typedef 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);
|
---|
| 51 | typedef int (* scsi_hba_cmd_data_in)(void __far *pvHba, uint8_t idTgt, uint8_t __far *aCDB,
|
---|
[89364] | 52 | uint8_t cbCDB, uint8_t __far *buffer, uint32_t length);
|
---|
[38699] | 53 |
|
---|
[89168] | 54 | typedef struct
|
---|
| 55 | {
|
---|
| 56 | uint16_t idPciVendor;
|
---|
| 57 | uint16_t idPciDevice;
|
---|
[100984] | 58 | scsi_hba_detect detect;
|
---|
[89168] | 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;
|
---|
[38699] | 63 |
|
---|
[89168] | 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 | */
|
---|
[38699] | 69 |
|
---|
[89168] | 70 | /* Note: Reading high EAX bits destroys them - *must* be restored later. */
|
---|
| 71 | uint16_t eax_hi_rd(void);
|
---|
| 72 | #pragma aux eax_hi_rd = \
|
---|
| 73 | ".386" \
|
---|
| 74 | "shr eax, 16" \
|
---|
| 75 | value [ax] modify nomemory;
|
---|
[43475] | 76 |
|
---|
[89168] | 77 | void eax_hi_wr(uint16_t);
|
---|
| 78 | #pragma aux eax_hi_wr = \
|
---|
| 79 | ".386" \
|
---|
| 80 | "shl eax, 16" \
|
---|
| 81 | parm [ax] modify nomemory;
|
---|
[43475] | 82 |
|
---|
[89168] | 83 | void inline high_bits_save(uint16_t __far *pu16EaxHi)
|
---|
[38699] | 84 | {
|
---|
[89168] | 85 | *pu16EaxHi = eax_hi_rd();
|
---|
| 86 | }
|
---|
[38699] | 87 |
|
---|
[89168] | 88 | void inline high_bits_restore(uint16_t u16EaxHi)
|
---|
| 89 | {
|
---|
| 90 | eax_hi_wr(u16EaxHi);
|
---|
[38699] | 91 | }
|
---|
| 92 |
|
---|
[89168] | 93 | /* Pointers to the HBA specific access routines. */
|
---|
| 94 | scsi_hba_t hbaacc[] =
|
---|
[38699] | 95 | {
|
---|
[100984] | 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 */
|
---|
[89168] | 99 | #ifdef VBOX_WITH_VIRTIO_SCSI
|
---|
[100984] | 100 | { 0x1af4, 0x1048, NULL, virtio_scsi_init, virtio_scsi_cmd_data_out, virtio_scsi_cmd_data_in },
|
---|
[89168] | 101 | #endif
|
---|
[100984] | 102 | { 0xffff, 0xffff, btaha_scsi_detect, btaha_scsi_init, buslogic_scsi_cmd_data_out, buslogic_scsi_cmd_data_in } /* ISA */
|
---|
[89168] | 103 | };
|
---|
[38699] | 104 |
|
---|
[89168] | 105 | /**
|
---|
| 106 | * Allocates 1K of conventional memory.
|
---|
| 107 | */
|
---|
| 108 | static uint16_t scsi_hba_mem_alloc(void)
|
---|
| 109 | {
|
---|
| 110 | uint16_t base_mem_kb;
|
---|
| 111 | uint16_t hba_seg;
|
---|
[38699] | 112 |
|
---|
[89168] | 113 | base_mem_kb = read_word(0x00, 0x0413);
|
---|
[48123] | 114 |
|
---|
[89168] | 115 | DBG_SCSI("SCSI: %dK of base mem\n", base_mem_kb);
|
---|
[38699] | 116 |
|
---|
[89168] | 117 | if (base_mem_kb == 0)
|
---|
| 118 | return 0;
|
---|
[38699] | 119 |
|
---|
[89168] | 120 | base_mem_kb--; /* Allocate one block. */
|
---|
| 121 | hba_seg = (((uint32_t)base_mem_kb * 1024) >> 4); /* Calculate start segment. */
|
---|
[46241] | 122 |
|
---|
[89168] | 123 | write_word(0x00, 0x0413, base_mem_kb);
|
---|
[38699] | 124 |
|
---|
[89168] | 125 | return hba_seg;
|
---|
[38699] | 126 | }
|
---|
| 127 |
|
---|
| 128 | /**
|
---|
[39366] | 129 | * Read sectors from an attached SCSI device.
|
---|
[38699] | 130 | *
|
---|
| 131 | * @returns status code.
|
---|
[48123] | 132 | * @param bios_dsk Pointer to disk request packet (in the
|
---|
[39366] | 133 | * EBDA).
|
---|
[38699] | 134 | */
|
---|
[39366] | 135 | int scsi_read_sectors(bio_dsk_t __far *bios_dsk)
|
---|
[38699] | 136 | {
|
---|
| 137 | uint8_t rc;
|
---|
[58724] | 138 | cdb_rw16 cdb;
|
---|
| 139 | uint32_t count;
|
---|
[89168] | 140 | uint16_t hba_seg;
|
---|
| 141 | uint8_t idx_hba;
|
---|
[38699] | 142 | uint8_t target_id;
|
---|
[39366] | 143 | uint8_t device_id;
|
---|
[89168] | 144 | uint16_t eax_hi;
|
---|
[38699] | 145 |
|
---|
[42811] | 146 | device_id = VBOX_GET_SCSI_DEVICE(bios_dsk->drqp.dev_id);
|
---|
[38699] | 147 | if (device_id > BX_MAX_SCSI_DEVICES)
|
---|
[50176] | 148 | BX_PANIC("%s: device_id out of range %d\n", __func__, device_id);
|
---|
[38699] | 149 |
|
---|
[39366] | 150 | count = bios_dsk->drqp.nsect;
|
---|
| 151 |
|
---|
[89168] | 152 | high_bits_save(&eax_hi);
|
---|
| 153 |
|
---|
[39355] | 154 | /* Prepare a CDB. */
|
---|
[58724] | 155 | cdb.command = SCSI_READ_16;
|
---|
| 156 | cdb.lba = swap_64(bios_dsk->drqp.lba);
|
---|
[39355] | 157 | cdb.pad1 = 0;
|
---|
[58724] | 158 | cdb.nsect32 = swap_32(count);
|
---|
[39355] | 159 | cdb.pad2 = 0;
|
---|
| 160 |
|
---|
[38699] | 161 |
|
---|
[89168] | 162 | hba_seg = bios_dsk->scsidev[device_id].hba_seg;
|
---|
| 163 | idx_hba = bios_dsk->scsidev[device_id].idx_hba;
|
---|
[39347] | 164 | target_id = bios_dsk->scsidev[device_id].target_id;
|
---|
[38699] | 165 |
|
---|
[46240] | 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 |
|
---|
[89168] | 169 | rc = hbaacc[idx_hba].cmd_data_in(hba_seg :> 0, target_id, (void __far *)&cdb, 16,
|
---|
[89364] | 170 | bios_dsk->drqp.buffer, (count * 512L));
|
---|
[38699] | 171 | if (!rc)
|
---|
| 172 | {
|
---|
[39355] | 173 | bios_dsk->drqp.trsfsectors = count;
|
---|
[43475] | 174 | bios_dsk->drqp.trsfbytes = count * 512L;
|
---|
[38699] | 175 | }
|
---|
[46240] | 176 | DBG_SCSI("%s: transferred %u sectors\n", __func__, bios_dsk->drqp.nsect);
|
---|
[89168] | 177 | high_bits_restore(eax_hi);
|
---|
[38699] | 178 |
|
---|
| 179 | return rc;
|
---|
| 180 | }
|
---|
| 181 |
|
---|
| 182 | /**
|
---|
[39366] | 183 | * Write sectors to an attached SCSI device.
|
---|
[38699] | 184 | *
|
---|
| 185 | * @returns status code.
|
---|
[48123] | 186 | * @param bios_dsk Pointer to disk request packet (in the
|
---|
[39366] | 187 | * EBDA).
|
---|
[38699] | 188 | */
|
---|
[39366] | 189 | int scsi_write_sectors(bio_dsk_t __far *bios_dsk)
|
---|
[38699] | 190 | {
|
---|
| 191 | uint8_t rc;
|
---|
[58724] | 192 | cdb_rw16 cdb;
|
---|
| 193 | uint32_t count;
|
---|
[89168] | 194 | uint16_t hba_seg;
|
---|
| 195 | uint8_t idx_hba;
|
---|
[38699] | 196 | uint8_t target_id;
|
---|
[39366] | 197 | uint8_t device_id;
|
---|
[89168] | 198 | uint16_t eax_hi;
|
---|
[38699] | 199 |
|
---|
[42811] | 200 | device_id = VBOX_GET_SCSI_DEVICE(bios_dsk->drqp.dev_id);
|
---|
[38699] | 201 | if (device_id > BX_MAX_SCSI_DEVICES)
|
---|
[50176] | 202 | BX_PANIC("%s: device_id out of range %d\n", __func__, device_id);
|
---|
[38699] | 203 |
|
---|
[39366] | 204 | count = bios_dsk->drqp.nsect;
|
---|
| 205 |
|
---|
[89168] | 206 | high_bits_save(&eax_hi);
|
---|
| 207 |
|
---|
[39355] | 208 | /* Prepare a CDB. */
|
---|
[58724] | 209 | cdb.command = SCSI_WRITE_16;
|
---|
| 210 | cdb.lba = swap_64(bios_dsk->drqp.lba);
|
---|
[39355] | 211 | cdb.pad1 = 0;
|
---|
[58724] | 212 | cdb.nsect32 = swap_32(count);
|
---|
[39355] | 213 | cdb.pad2 = 0;
|
---|
| 214 |
|
---|
[89168] | 215 | hba_seg = bios_dsk->scsidev[device_id].hba_seg;
|
---|
| 216 | idx_hba = bios_dsk->scsidev[device_id].idx_hba;
|
---|
[39347] | 217 | target_id = bios_dsk->scsidev[device_id].target_id;
|
---|
[38699] | 218 |
|
---|
[46240] | 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 |
|
---|
[89168] | 222 | rc = hbaacc[idx_hba].cmd_data_out(hba_seg :> 0, target_id, (void __far *)&cdb, 16,
|
---|
| 223 | bios_dsk->drqp.buffer, (count * 512L));
|
---|
[38699] | 224 | if (!rc)
|
---|
| 225 | {
|
---|
[39355] | 226 | bios_dsk->drqp.trsfsectors = count;
|
---|
[43475] | 227 | bios_dsk->drqp.trsfbytes = (count * 512L);
|
---|
[38699] | 228 | }
|
---|
[46240] | 229 | DBG_SCSI("%s: transferred %u sectors\n", __func__, bios_dsk->drqp.nsect);
|
---|
[89168] | 230 | high_bits_restore(eax_hi);
|
---|
[38699] | 231 |
|
---|
| 232 | return rc;
|
---|
| 233 | }
|
---|
| 234 |
|
---|
[43475] | 235 |
|
---|
[63562] | 236 | /// @todo move
|
---|
[43475] | 237 | #define ATA_DATA_NO 0x00
|
---|
| 238 | #define ATA_DATA_IN 0x01
|
---|
| 239 | #define ATA_DATA_OUT 0x02
|
---|
| 240 |
|
---|
[38699] | 241 | /**
|
---|
[43475] | 242 | * Perform a "packet style" read with supplied CDB.
|
---|
| 243 | *
|
---|
| 244 | * @returns status code.
|
---|
[64369] | 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.
|
---|
[43475] | 251 | */
|
---|
[48123] | 252 | uint16_t scsi_cmd_packet(uint16_t device_id, uint8_t cmdlen, char __far *cmdbuf,
|
---|
[89364] | 253 | uint32_t length, uint8_t inout, char __far *buffer)
|
---|
[43475] | 254 | {
|
---|
| 255 | bio_dsk_t __far *bios_dsk = read_word(0x0040, 0x000E) :> &EbdaData->bdisk;
|
---|
[89168] | 256 | uint8_t rc;
|
---|
[43475] | 257 | uint8_t target_id;
|
---|
[89168] | 258 | uint16_t hba_seg;
|
---|
| 259 | uint8_t idx_hba;
|
---|
| 260 | uint16_t eax_hi;
|
---|
[43475] | 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 |
|
---|
[89364] | 271 | DBG_SCSI("%s: reading %lu bytes, device %d, target %d\n", __func__,
|
---|
| 272 | length, device_id, bios_dsk->scsidev[device_id].target_id);
|
---|
[43475] | 273 | DBG_SCSI("%s: reading %u %u-byte sectors\n", __func__,
|
---|
| 274 | bios_dsk->drqp.nsect, bios_dsk->drqp.sect_sz);
|
---|
| 275 |
|
---|
[89168] | 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;
|
---|
[43475] | 279 | target_id = bios_dsk->scsidev[device_id].target_id;
|
---|
| 280 |
|
---|
[89168] | 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;
|
---|
[43475] | 284 |
|
---|
[89168] | 285 | DBG_SCSI("%s: reading %u bytes, device %d, target %d\n", __func__,
|
---|
| 286 | length, device_id, bios_dsk->scsidev[device_id].target_id);
|
---|
[43672] | 287 |
|
---|
[89168] | 288 | rc = hbaacc[idx_hba].cmd_data_in(hba_seg :> 0, target_id, (void __far *)cmdbuf, cmdlen,
|
---|
[89364] | 289 | bios_dsk->drqp.buffer, length);
|
---|
[89168] | 290 | if (!rc)
|
---|
| 291 | bios_dsk->drqp.trsfbytes = length;
|
---|
[43475] | 292 |
|
---|
[89168] | 293 | DBG_SCSI("%s: transferred %u bytes\n", __func__, length);
|
---|
| 294 | high_bits_restore(eax_hi);
|
---|
[43475] | 295 |
|
---|
[89168] | 296 | return rc;
|
---|
[43475] | 297 | }
|
---|
| 298 |
|
---|
| 299 | /**
|
---|
[38699] | 300 | * Enumerate attached devices.
|
---|
| 301 | *
|
---|
[89169] | 302 | * @param hba_seg Segement of the HBA controller block.
|
---|
| 303 | * @param idx_hba The HBA driver index used for accessing the enumerated devices.
|
---|
[38699] | 304 | */
|
---|
[89168] | 305 | static void scsi_enumerate_attached_devices(uint16_t hba_seg, uint8_t idx_hba)
|
---|
[38699] | 306 | {
|
---|
| 307 | int i;
|
---|
| 308 | uint8_t buffer[0x0200];
|
---|
[39347] | 309 | bio_dsk_t __far *bios_dsk;
|
---|
[38699] | 310 |
|
---|
[39347] | 311 | bios_dsk = read_word(0x0040, 0x000E) :> &EbdaData->bdisk;
|
---|
[38699] | 312 |
|
---|
| 313 | /* Go through target devices. */
|
---|
| 314 | for (i = 0; i < VBSCSI_MAX_DEVICES; i++)
|
---|
| 315 | {
|
---|
| 316 | uint8_t rc;
|
---|
[58724] | 317 | uint8_t aCDB[16];
|
---|
[43475] | 318 | uint8_t hd_index, devcount_scsi;
|
---|
[38699] | 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 |
|
---|
[89364] | 327 | rc = hbaacc[idx_hba].cmd_data_in(hba_seg :> 0, i, aCDB, 6, buffer, 5);
|
---|
[38699] | 328 | if (rc != 0)
|
---|
[89168] | 329 | {
|
---|
| 330 | DBG_SCSI("%s: SCSI_INQUIRY failed\n", __func__); /* Not a fatal error if the device doesn't exist. */
|
---|
| 331 | continue;
|
---|
| 332 | }
|
---|
[38699] | 333 |
|
---|
[55132] | 334 | devcount_scsi = bios_dsk->scsi_devcount;
|
---|
| 335 |
|
---|
[43475] | 336 | /* Check the attached device. */
|
---|
[38699] | 337 | if ( ((buffer[0] & 0xe0) == 0)
|
---|
| 338 | && ((buffer[0] & 0x1f) == 0x00))
|
---|
| 339 | {
|
---|
[43475] | 340 | DBG_SCSI("%s: Disk detected at %d\n", __func__, i);
|
---|
[38699] | 341 |
|
---|
| 342 | /* We add the disk only if the maximum is not reached yet. */
|
---|
[55132] | 343 | if (devcount_scsi < BX_MAX_SCSI_DEVICES)
|
---|
[38699] | 344 | {
|
---|
[58724] | 345 | uint64_t sectors, t;
|
---|
| 346 | uint32_t sector_size, cylinders;
|
---|
[38699] | 347 | uint16_t heads, sectors_per_track;
|
---|
[43475] | 348 | uint8_t hdcount;
|
---|
[50091] | 349 | uint8_t cmos_base;
|
---|
[38699] | 350 |
|
---|
| 351 | /* Issue a read capacity command now. */
|
---|
| 352 | _fmemset(aCDB, 0, sizeof(aCDB));
|
---|
[58724] | 353 | aCDB[0] = SCSI_SERVICE_ACT;
|
---|
| 354 | aCDB[1] = SCSI_READ_CAP_16;
|
---|
| 355 | aCDB[13] = 32; /* Allocation length. */
|
---|
[38699] | 356 |
|
---|
[89364] | 357 | rc = hbaacc[idx_hba].cmd_data_in(hba_seg :> 0, i, aCDB, 16, buffer, 32);
|
---|
[38699] | 358 | if (rc != 0)
|
---|
[43475] | 359 | BX_PANIC("%s: SCSI_READ_CAPACITY failed\n", __func__);
|
---|
[38699] | 360 |
|
---|
[58724] | 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;
|
---|
[38699] | 365 |
|
---|
[58724] | 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]);
|
---|
[38699] | 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 |
|
---|
[50091] | 379 | /* Get logical CHS geometry. */
|
---|
| 380 | switch (devcount_scsi)
|
---|
[38699] | 381 | {
|
---|
[50091] | 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;
|
---|
[38699] | 396 | }
|
---|
[50091] | 397 |
|
---|
| 398 | if (cmos_base && inb_cmos(cmos_base + 7))
|
---|
[38699] | 399 | {
|
---|
[50091] | 400 | /* If provided, grab the logical geometry from CMOS. */
|
---|
[92290] | 401 | cylinders = get_cmos_word(cmos_base /*, cmos_base + 1*/);
|
---|
[50091] | 402 | heads = inb_cmos(cmos_base + 2);
|
---|
| 403 | sectors_per_track = inb_cmos(cmos_base + 7);
|
---|
[38699] | 404 | }
|
---|
| 405 | else
|
---|
| 406 | {
|
---|
[50091] | 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;
|
---|
[58724] | 414 | /* Approximate x / (255 * 63) using shifts */
|
---|
| 415 | t = (sectors >> 6) + (sectors >> 12);
|
---|
| 416 | cylinders = (t >> 8) + (t >> 16);
|
---|
[50091] | 417 | }
|
---|
| 418 | else if (sectors >= (uint32_t)2 * 1024 * 1024)
|
---|
| 419 | {
|
---|
| 420 | heads = 128;
|
---|
| 421 | sectors_per_track = 32;
|
---|
[58724] | 422 | cylinders = sectors >> 12;
|
---|
[50091] | 423 | }
|
---|
| 424 | else
|
---|
| 425 | {
|
---|
| 426 | heads = 64;
|
---|
| 427 | sectors_per_track = 32;
|
---|
[58724] | 428 | cylinders = sectors >> 11;
|
---|
[50091] | 429 | }
|
---|
[38699] | 430 | }
|
---|
| 431 |
|
---|
[39346] | 432 | /* Calculate index into the generic disk table. */
|
---|
[43475] | 433 | hd_index = devcount_scsi + BX_MAX_ATA_DEVICES;
|
---|
[38699] | 434 |
|
---|
[89168] | 435 | bios_dsk->scsidev[devcount_scsi].hba_seg = hba_seg;
|
---|
| 436 | bios_dsk->scsidev[devcount_scsi].idx_hba = idx_hba;
|
---|
[43475] | 437 | bios_dsk->scsidev[devcount_scsi].target_id = i;
|
---|
[39651] | 438 | bios_dsk->devices[hd_index].type = DSK_TYPE_SCSI;
|
---|
| 439 | bios_dsk->devices[hd_index].device = DSK_DEVICE_HD;
|
---|
[39347] | 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;
|
---|
[39651] | 443 | bios_dsk->devices[hd_index].translation = GEO_TRANSLATION_LBA;
|
---|
[39346] | 444 |
|
---|
[58724] | 445 | /* Write LCHS/PCHS values. */
|
---|
[39347] | 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;
|
---|
[58724] | 450 |
|
---|
| 451 | if (cylinders > 1024) {
|
---|
| 452 | bios_dsk->devices[hd_index].lchs.cylinders = 1024;
|
---|
[39347] | 453 | bios_dsk->devices[hd_index].pchs.cylinders = 1024;
|
---|
[58724] | 454 | } else {
|
---|
| 455 | bios_dsk->devices[hd_index].lchs.cylinders = (uint16_t)cylinders;
|
---|
[39347] | 456 | bios_dsk->devices[hd_index].pchs.cylinders = (uint16_t)cylinders;
|
---|
[58724] | 457 | }
|
---|
[38699] | 458 |
|
---|
[58724] | 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 |
|
---|
[39347] | 462 | bios_dsk->devices[hd_index].sectors = sectors;
|
---|
[38699] | 463 |
|
---|
| 464 | /* Store the id of the disk in the ata hdidmap. */
|
---|
[39347] | 465 | hdcount = bios_dsk->hdcount;
|
---|
[43475] | 466 | bios_dsk->hdidmap[hdcount] = devcount_scsi + BX_MAX_ATA_DEVICES;
|
---|
[38699] | 467 | hdcount++;
|
---|
[39347] | 468 | bios_dsk->hdcount = hdcount;
|
---|
[38699] | 469 |
|
---|
| 470 | /* Update hdcount in the BDA. */
|
---|
| 471 | hdcount = read_byte(0x40, 0x75);
|
---|
| 472 | hdcount++;
|
---|
| 473 | write_byte(0x40, 0x75, hdcount);
|
---|
| 474 |
|
---|
[43475] | 475 | devcount_scsi++;
|
---|
[38699] | 476 | }
|
---|
| 477 | else
|
---|
| 478 | {
|
---|
| 479 | /* We reached the maximum of SCSI disks we can boot from. We can quit detecting. */
|
---|
| 480 | break;
|
---|
| 481 | }
|
---|
| 482 | }
|
---|
[43475] | 483 | else if ( ((buffer[0] & 0xe0) == 0)
|
---|
| 484 | && ((buffer[0] & 0x1f) == 0x05))
|
---|
| 485 | {
|
---|
| 486 | uint8_t cdcount;
|
---|
| 487 | uint8_t removable;
|
---|
| 488 |
|
---|
[43757] | 489 | BX_INFO("SCSI %d-ID#%d: CD/DVD-ROM\n", devcount_scsi, i);
|
---|
[43475] | 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 |
|
---|
[89168] | 496 | bios_dsk->scsidev[devcount_scsi].hba_seg = hba_seg;
|
---|
| 497 | bios_dsk->scsidev[devcount_scsi].idx_hba = idx_hba;
|
---|
[43475] | 498 | bios_dsk->scsidev[devcount_scsi].target_id = i;
|
---|
[70333] | 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;
|
---|
[43475] | 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 | }
|
---|
[38699] | 513 | else
|
---|
[43475] | 514 | DBG_SCSI("%s: No supported device detected at %d\n", __func__, i);
|
---|
[55132] | 515 |
|
---|
| 516 | bios_dsk->scsi_devcount = devcount_scsi;
|
---|
[38699] | 517 | }
|
---|
| 518 | }
|
---|
| 519 |
|
---|
| 520 | /**
|
---|
| 521 | * Init the SCSI driver and detect attached disks.
|
---|
| 522 | */
|
---|
| 523 | void BIOSCALL scsi_init(void)
|
---|
| 524 | {
|
---|
[89168] | 525 | int i;
|
---|
[39347] | 526 | bio_dsk_t __far *bios_dsk;
|
---|
[38699] | 527 |
|
---|
[39347] | 528 | bios_dsk = read_word(0x0040, 0x000E) :> &EbdaData->bdisk;
|
---|
[43475] | 529 | bios_dsk->scsi_devcount = 0;
|
---|
[38699] | 530 |
|
---|
[89168] | 531 | /* Walk the supported drivers and try to detect the HBA. */
|
---|
| 532 | for (i = 0; i < sizeof(hbaacc)/sizeof(hbaacc[0]); i++)
|
---|
[38699] | 533 | {
|
---|
[100984] | 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 |
|
---|
[89168] | 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;
|
---|
[38699] | 549 |
|
---|
[89168] | 550 | u8Bus = (busdevfn & 0xff00) >> 8;
|
---|
| 551 | u8DevFn = busdevfn & 0x00ff;
|
---|
[38699] | 552 |
|
---|
[89168] | 553 | DBG_SCSI("SCSI HBA at Bus %u DevFn 0x%x (raw 0x%x)\n", u8Bus, u8DevFn, busdevfn);
|
---|
[89364] | 554 | rc = hbaacc[i].init(hba_seg :> 0, u8Bus, u8DevFn);
|
---|
[89168] | 555 | if (!rc)
|
---|
| 556 | scsi_enumerate_attached_devices(hba_seg, i);
|
---|
| 557 | /** @todo Free memory on error. */
|
---|
| 558 | }
|
---|
[38699] | 559 | }
|
---|
| 560 | }
|
---|