1 | /* $Id: scsi.c 82162 2019-11-25 10:30:40Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * SCSI host adapter driver to boot from SCSI disks
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2004-2019 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | */
|
---|
17 |
|
---|
18 | #include <stdint.h>
|
---|
19 | #include <string.h>
|
---|
20 | #include "biosint.h"
|
---|
21 | #include "inlines.h"
|
---|
22 | #include "pciutil.h"
|
---|
23 | #include "ebda.h"
|
---|
24 | #include "scsi.h"
|
---|
25 |
|
---|
26 |
|
---|
27 | #if DEBUG_SCSI
|
---|
28 | # define DBG_SCSI(...) BX_INFO(__VA_ARGS__)
|
---|
29 | #else
|
---|
30 | # define DBG_SCSI(...)
|
---|
31 | #endif
|
---|
32 |
|
---|
33 | #define VBSCSI_BUSY (1 << 0)
|
---|
34 | #define VBSCSI_ERROR (1 << 1)
|
---|
35 |
|
---|
36 | /* The I/O port of the BusLogic SCSI adapter. */
|
---|
37 | #define BUSLOGIC_BIOS_IO_PORT 0x430
|
---|
38 | /* The I/O port of the LsiLogic SCSI adapter. */
|
---|
39 | #define LSILOGIC_BIOS_IO_PORT 0x434
|
---|
40 | /* The I/O port of the LsiLogic SAS adapter. */
|
---|
41 | #define LSILOGIC_SAS_BIOS_IO_PORT 0x438
|
---|
42 |
|
---|
43 | #define VBSCSI_REGISTER_STATUS 0
|
---|
44 | #define VBSCSI_REGISTER_COMMAND 0
|
---|
45 | #define VBSCSI_REGISTER_DATA_IN 1
|
---|
46 | #define VBSCSI_REGISTER_IDENTIFY 2
|
---|
47 | #define VBSCSI_REGISTER_RESET 3
|
---|
48 | #define VBSCSI_REGISTER_DEVSTAT 3
|
---|
49 |
|
---|
50 | #define VBSCSI_MAX_DEVICES 16 /* Maximum number of devices a SCSI device can have. */
|
---|
51 |
|
---|
52 | /* Data transfer direction. */
|
---|
53 | #define SCSI_TXDIR_FROM_DEVICE 0
|
---|
54 | #define SCSI_TXDIR_TO_DEVICE 1
|
---|
55 |
|
---|
56 | void insb_discard(unsigned nbytes, unsigned port);
|
---|
57 | #pragma aux insb_discard = \
|
---|
58 | ".286" \
|
---|
59 | "again:" \
|
---|
60 | "in al,dx" \
|
---|
61 | "loop again" \
|
---|
62 | parm [cx] [dx] modify exact [cx ax] nomemory;
|
---|
63 |
|
---|
64 |
|
---|
65 | int scsi_cmd_data_in(uint16_t io_base, uint8_t target_id, uint8_t __far *aCDB,
|
---|
66 | uint8_t cbCDB, uint8_t __far *buffer, uint32_t length)
|
---|
67 | {
|
---|
68 | /* Check that the adapter is ready. */
|
---|
69 | uint8_t status, sizes;
|
---|
70 | uint16_t i;
|
---|
71 |
|
---|
72 | do
|
---|
73 | status = inb(io_base + VBSCSI_REGISTER_STATUS);
|
---|
74 | while (status & VBSCSI_BUSY);
|
---|
75 |
|
---|
76 | sizes = ((length >> 12) & 0xF0) | ((cbCDB == 16) ? 0 : cbCDB);
|
---|
77 | outb(io_base + VBSCSI_REGISTER_COMMAND, target_id); /* Write the target ID. */
|
---|
78 | outb(io_base + VBSCSI_REGISTER_COMMAND, SCSI_TXDIR_FROM_DEVICE); /* Write the transfer direction. */
|
---|
79 | outb(io_base + VBSCSI_REGISTER_COMMAND, sizes); /* Write CDB size and top bufsize bits. */
|
---|
80 | outb(io_base + VBSCSI_REGISTER_COMMAND, length); /* Write the buffer size. */
|
---|
81 | outb(io_base + VBSCSI_REGISTER_COMMAND, (length >> 8));
|
---|
82 | for (i = 0; i < cbCDB; i++) /* Write the CDB. */
|
---|
83 | outb(io_base + VBSCSI_REGISTER_COMMAND, aCDB[i]);
|
---|
84 |
|
---|
85 | /* Now wait for the command to complete. */
|
---|
86 | do
|
---|
87 | status = inb(io_base + VBSCSI_REGISTER_STATUS);
|
---|
88 | while (status & VBSCSI_BUSY);
|
---|
89 |
|
---|
90 | /* If any error occurred, inform the caller and don't bother reading the data. */
|
---|
91 | if (status & VBSCSI_ERROR) {
|
---|
92 | outb(io_base + VBSCSI_REGISTER_RESET, 0);
|
---|
93 |
|
---|
94 | status = inb(io_base + VBSCSI_REGISTER_DEVSTAT);
|
---|
95 | DBG_SCSI("%s: read failed, device status %02X\n", __func__, status);
|
---|
96 | return 4; /* Sector not found */
|
---|
97 | }
|
---|
98 |
|
---|
99 | /* Read in the data. The transfer length may be exactly 64K or more,
|
---|
100 | * which needs a bit of care when we're using 16-bit 'rep ins'.
|
---|
101 | */
|
---|
102 | while (length > 32768) {
|
---|
103 | DBG_SCSI("%s: reading 32K to %X:%X\n", __func__, FP_SEG(buffer), FP_OFF(buffer));
|
---|
104 | rep_insb(buffer, 32768, io_base + VBSCSI_REGISTER_DATA_IN);
|
---|
105 | length -= 32768;
|
---|
106 | buffer = (FP_SEG(buffer) + (32768 >> 4)) :> FP_OFF(buffer);
|
---|
107 | }
|
---|
108 |
|
---|
109 | DBG_SCSI("%s: reading %ld bytes to %X:%X\n", __func__, length, FP_SEG(buffer), FP_OFF(buffer));
|
---|
110 | rep_insb(buffer, length, io_base + VBSCSI_REGISTER_DATA_IN);
|
---|
111 |
|
---|
112 | return 0;
|
---|
113 | }
|
---|
114 |
|
---|
115 | int scsi_cmd_data_out(uint16_t io_base, uint8_t target_id, uint8_t __far *aCDB,
|
---|
116 | uint8_t cbCDB, uint8_t __far *buffer, uint32_t length)
|
---|
117 | {
|
---|
118 | /* Check that the adapter is ready. */
|
---|
119 | uint8_t status, sizes;
|
---|
120 | uint16_t i;
|
---|
121 |
|
---|
122 | do
|
---|
123 | status = inb(io_base + VBSCSI_REGISTER_STATUS);
|
---|
124 | while (status & VBSCSI_BUSY);
|
---|
125 |
|
---|
126 |
|
---|
127 | sizes = ((length >> 12) & 0xF0) | ((cbCDB == 16) ? 0 : cbCDB);
|
---|
128 | outb(io_base + VBSCSI_REGISTER_COMMAND, target_id); /* Write the target ID. */
|
---|
129 | outb(io_base + VBSCSI_REGISTER_COMMAND, SCSI_TXDIR_TO_DEVICE); /* Write the transfer direction. */
|
---|
130 | outb(io_base + VBSCSI_REGISTER_COMMAND, sizes); /* Write CDB size and top bufsize bits. */
|
---|
131 | outb(io_base + VBSCSI_REGISTER_COMMAND, length); /* Write the buffer size. */
|
---|
132 | outb(io_base + VBSCSI_REGISTER_COMMAND, (length >> 8));
|
---|
133 | for (i = 0; i < cbCDB; i++) /* Write the CDB. */
|
---|
134 | outb(io_base + VBSCSI_REGISTER_COMMAND, aCDB[i]);
|
---|
135 |
|
---|
136 | /* Write out the data. The transfer length may be exactly 64K or more,
|
---|
137 | * which needs a bit of care when we're using 16-bit 'rep outs'.
|
---|
138 | */
|
---|
139 | while (length > 32768) {
|
---|
140 | DBG_SCSI("%s: writing 32K from %X:%X\n", __func__, FP_SEG(buffer), FP_OFF(buffer));
|
---|
141 | rep_outsb(buffer, 32768, io_base + VBSCSI_REGISTER_DATA_IN);
|
---|
142 | length -= 32768;
|
---|
143 | buffer = (FP_SEG(buffer) + (32768 >> 4)) :> FP_OFF(buffer);
|
---|
144 | }
|
---|
145 |
|
---|
146 | DBG_SCSI("%s: writing %ld bytes from %X:%X\n", __func__, length, FP_SEG(buffer), FP_OFF(buffer));
|
---|
147 | rep_outsb(buffer, length, io_base + VBSCSI_REGISTER_DATA_IN);
|
---|
148 |
|
---|
149 | /* Now wait for the command to complete. */
|
---|
150 | do
|
---|
151 | status = inb(io_base + VBSCSI_REGISTER_STATUS);
|
---|
152 | while (status & VBSCSI_BUSY);
|
---|
153 |
|
---|
154 | /* If any error occurred, inform the caller. */
|
---|
155 | if (status & VBSCSI_ERROR) {
|
---|
156 | outb(io_base + VBSCSI_REGISTER_RESET, 0);
|
---|
157 |
|
---|
158 | status = inb(io_base + VBSCSI_REGISTER_DEVSTAT);
|
---|
159 | DBG_SCSI("%s: write failed, device status %02X\n", __func__, status);
|
---|
160 | return 4; /* Sector not found */
|
---|
161 | }
|
---|
162 |
|
---|
163 | return 0;
|
---|
164 | }
|
---|
165 |
|
---|
166 | /**
|
---|
167 | * Read sectors from an attached SCSI device.
|
---|
168 | *
|
---|
169 | * @returns status code.
|
---|
170 | * @param bios_dsk Pointer to disk request packet (in the
|
---|
171 | * EBDA).
|
---|
172 | */
|
---|
173 | int scsi_read_sectors(bio_dsk_t __far *bios_dsk)
|
---|
174 | {
|
---|
175 | uint8_t rc;
|
---|
176 | cdb_rw16 cdb;
|
---|
177 | uint32_t count;
|
---|
178 | uint16_t io_base;
|
---|
179 | uint8_t target_id;
|
---|
180 | uint8_t device_id;
|
---|
181 |
|
---|
182 | device_id = VBOX_GET_SCSI_DEVICE(bios_dsk->drqp.dev_id);
|
---|
183 | if (device_id > BX_MAX_SCSI_DEVICES)
|
---|
184 | BX_PANIC("%s: device_id out of range %d\n", __func__, device_id);
|
---|
185 |
|
---|
186 | count = bios_dsk->drqp.nsect;
|
---|
187 |
|
---|
188 | /* Prepare a CDB. */
|
---|
189 | cdb.command = SCSI_READ_16;
|
---|
190 | cdb.lba = swap_64(bios_dsk->drqp.lba);
|
---|
191 | cdb.pad1 = 0;
|
---|
192 | cdb.nsect32 = swap_32(count);
|
---|
193 | cdb.pad2 = 0;
|
---|
194 |
|
---|
195 |
|
---|
196 | io_base = bios_dsk->scsidev[device_id].io_base;
|
---|
197 | target_id = bios_dsk->scsidev[device_id].target_id;
|
---|
198 |
|
---|
199 | DBG_SCSI("%s: reading %u sectors, device %d, target %d\n", __func__,
|
---|
200 | count, device_id, bios_dsk->scsidev[device_id].target_id);
|
---|
201 |
|
---|
202 | rc = scsi_cmd_data_in(io_base, target_id, (void __far *)&cdb, 16,
|
---|
203 | bios_dsk->drqp.buffer, (count * 512L));
|
---|
204 |
|
---|
205 | if (!rc)
|
---|
206 | {
|
---|
207 | bios_dsk->drqp.trsfsectors = count;
|
---|
208 | bios_dsk->drqp.trsfbytes = count * 512L;
|
---|
209 | }
|
---|
210 | DBG_SCSI("%s: transferred %u sectors\n", __func__, bios_dsk->drqp.nsect);
|
---|
211 |
|
---|
212 | return rc;
|
---|
213 | }
|
---|
214 |
|
---|
215 | /**
|
---|
216 | * Write sectors to an attached SCSI device.
|
---|
217 | *
|
---|
218 | * @returns status code.
|
---|
219 | * @param bios_dsk Pointer to disk request packet (in the
|
---|
220 | * EBDA).
|
---|
221 | */
|
---|
222 | int scsi_write_sectors(bio_dsk_t __far *bios_dsk)
|
---|
223 | {
|
---|
224 | uint8_t rc;
|
---|
225 | cdb_rw16 cdb;
|
---|
226 | uint32_t count;
|
---|
227 | uint16_t io_base;
|
---|
228 | uint8_t target_id;
|
---|
229 | uint8_t device_id;
|
---|
230 |
|
---|
231 | device_id = VBOX_GET_SCSI_DEVICE(bios_dsk->drqp.dev_id);
|
---|
232 | if (device_id > BX_MAX_SCSI_DEVICES)
|
---|
233 | BX_PANIC("%s: device_id out of range %d\n", __func__, device_id);
|
---|
234 |
|
---|
235 | count = bios_dsk->drqp.nsect;
|
---|
236 |
|
---|
237 | /* Prepare a CDB. */
|
---|
238 | cdb.command = SCSI_WRITE_16;
|
---|
239 | cdb.lba = swap_64(bios_dsk->drqp.lba);
|
---|
240 | cdb.pad1 = 0;
|
---|
241 | cdb.nsect32 = swap_32(count);
|
---|
242 | cdb.pad2 = 0;
|
---|
243 |
|
---|
244 | io_base = bios_dsk->scsidev[device_id].io_base;
|
---|
245 | target_id = bios_dsk->scsidev[device_id].target_id;
|
---|
246 |
|
---|
247 | DBG_SCSI("%s: writing %u sectors, device %d, target %d\n", __func__,
|
---|
248 | count, device_id, bios_dsk->scsidev[device_id].target_id);
|
---|
249 |
|
---|
250 | rc = scsi_cmd_data_out(io_base, target_id, (void __far *)&cdb, 16,
|
---|
251 | bios_dsk->drqp.buffer, (count * 512L));
|
---|
252 |
|
---|
253 | if (!rc)
|
---|
254 | {
|
---|
255 | bios_dsk->drqp.trsfsectors = count;
|
---|
256 | bios_dsk->drqp.trsfbytes = (count * 512L);
|
---|
257 | }
|
---|
258 | DBG_SCSI("%s: transferred %u sectors\n", __func__, bios_dsk->drqp.nsect);
|
---|
259 |
|
---|
260 | return rc;
|
---|
261 | }
|
---|
262 |
|
---|
263 |
|
---|
264 | /// @todo move
|
---|
265 | #define ATA_DATA_NO 0x00
|
---|
266 | #define ATA_DATA_IN 0x01
|
---|
267 | #define ATA_DATA_OUT 0x02
|
---|
268 |
|
---|
269 | /**
|
---|
270 | * Perform a "packet style" read with supplied CDB.
|
---|
271 | *
|
---|
272 | * @returns status code.
|
---|
273 | * @param device_id ID of the device to access.
|
---|
274 | * @param cmdlen Length of the CDB.
|
---|
275 | * @param cmdbuf The CDB buffer.
|
---|
276 | * @param before How much to skip before reading into the provided data buffer.
|
---|
277 | * @param length How much to transfer.
|
---|
278 | * @param inout Read/Write direction indicator.
|
---|
279 | * @param buffer Data buffer to store the data from the device in.
|
---|
280 | */
|
---|
281 | uint16_t scsi_cmd_packet(uint16_t device_id, uint8_t cmdlen, char __far *cmdbuf,
|
---|
282 | uint16_t before, uint32_t length, uint8_t inout, char __far *buffer)
|
---|
283 | {
|
---|
284 | bio_dsk_t __far *bios_dsk = read_word(0x0040, 0x000E) :> &EbdaData->bdisk;
|
---|
285 | uint32_t read_len;
|
---|
286 | uint8_t status, sizes;
|
---|
287 | uint16_t i;
|
---|
288 | uint16_t io_base;
|
---|
289 | uint8_t target_id;
|
---|
290 |
|
---|
291 | /* Data out is currently not supported. */
|
---|
292 | if (inout == ATA_DATA_OUT) {
|
---|
293 | BX_INFO("%s: DATA_OUT not supported yet\n", __func__);
|
---|
294 | return 1;
|
---|
295 | }
|
---|
296 |
|
---|
297 | /* Convert to SCSI specific device number. */
|
---|
298 | device_id = VBOX_GET_SCSI_DEVICE(device_id);
|
---|
299 |
|
---|
300 | DBG_SCSI("%s: reading %lu bytes, skip %u/%u, device %d, target %d\n", __func__,
|
---|
301 | length, bios_dsk->drqp.skip_b, bios_dsk->drqp.skip_a,
|
---|
302 | device_id, bios_dsk->scsidev[device_id].target_id);
|
---|
303 | DBG_SCSI("%s: reading %u %u-byte sectors\n", __func__,
|
---|
304 | bios_dsk->drqp.nsect, bios_dsk->drqp.sect_sz);
|
---|
305 |
|
---|
306 | cmdlen -= 2; /* ATAPI uses 12-byte command packets for a READ 10. */
|
---|
307 |
|
---|
308 | io_base = bios_dsk->scsidev[device_id].io_base;
|
---|
309 | target_id = bios_dsk->scsidev[device_id].target_id;
|
---|
310 |
|
---|
311 | /* Wait until the adapter is ready. */
|
---|
312 | do
|
---|
313 | status = inb(io_base + VBSCSI_REGISTER_STATUS);
|
---|
314 | while (status & VBSCSI_BUSY);
|
---|
315 |
|
---|
316 | /* On the SCSI level, we have to transfer whole sectors. */
|
---|
317 | /* NB: With proper residual length support, this should not be necessary; we should
|
---|
318 | * be able to avoid transferring the 'after' part of the sector.
|
---|
319 | */
|
---|
320 | read_len = length + before + bios_dsk->drqp.skip_a;
|
---|
321 |
|
---|
322 | sizes = (((read_len) >> 12) & 0xF0) | cmdlen;
|
---|
323 | outb(io_base + VBSCSI_REGISTER_COMMAND, target_id); /* Write the target ID. */
|
---|
324 | outb(io_base + VBSCSI_REGISTER_COMMAND, SCSI_TXDIR_FROM_DEVICE); /* Write the transfer direction. */
|
---|
325 | outb(io_base + VBSCSI_REGISTER_COMMAND, sizes); /* Write the CDB size. */
|
---|
326 | outb(io_base + VBSCSI_REGISTER_COMMAND, read_len); /* Write the buffer size. */
|
---|
327 | outb(io_base + VBSCSI_REGISTER_COMMAND, (read_len) >> 8);
|
---|
328 | for (i = 0; i < cmdlen; i++) /* Write the CDB. */
|
---|
329 | outb(io_base + VBSCSI_REGISTER_COMMAND, cmdbuf[i]);
|
---|
330 |
|
---|
331 | /* Now wait for the command to complete. */
|
---|
332 | do
|
---|
333 | status = inb(io_base + VBSCSI_REGISTER_STATUS);
|
---|
334 | while (status & VBSCSI_BUSY);
|
---|
335 |
|
---|
336 | /* If any error occurred, inform the caller and don't bother reading the data. */
|
---|
337 | if (status & VBSCSI_ERROR) {
|
---|
338 | outb(io_base + VBSCSI_REGISTER_RESET, 0);
|
---|
339 |
|
---|
340 | status = inb(io_base + VBSCSI_REGISTER_DEVSTAT);
|
---|
341 | DBG_SCSI("%s: read failed, device status %02X\n", __func__, status);
|
---|
342 | return 3;
|
---|
343 | }
|
---|
344 |
|
---|
345 | /* Transfer the data read from the device. */
|
---|
346 |
|
---|
347 | if (before) /* If necessary, throw away data which needs to be skipped. */
|
---|
348 | insb_discard(before, io_base + VBSCSI_REGISTER_DATA_IN);
|
---|
349 |
|
---|
350 | bios_dsk->drqp.trsfbytes = length;
|
---|
351 |
|
---|
352 | /* The requested length may be exactly 64K or more, which needs
|
---|
353 | * a bit of care when we're using 16-bit 'rep ins'.
|
---|
354 | */
|
---|
355 | while (length > 32768) {
|
---|
356 | DBG_SCSI("%s: reading 32K to %X:%X\n", __func__, FP_SEG(buffer), FP_OFF(buffer));
|
---|
357 | rep_insb(buffer, 32768, io_base + VBSCSI_REGISTER_DATA_IN);
|
---|
358 | length -= 32768;
|
---|
359 | buffer = (FP_SEG(buffer) + (32768 >> 4)) :> FP_OFF(buffer);
|
---|
360 | }
|
---|
361 |
|
---|
362 | DBG_SCSI("%s: reading %ld bytes to %X:%X\n", __func__, length, FP_SEG(buffer), FP_OFF(buffer));
|
---|
363 | rep_insb(buffer, length, io_base + VBSCSI_REGISTER_DATA_IN);
|
---|
364 |
|
---|
365 | if (bios_dsk->drqp.skip_a) /* If necessary, throw away more data. */
|
---|
366 | insb_discard(bios_dsk->drqp.skip_a, io_base + VBSCSI_REGISTER_DATA_IN);
|
---|
367 |
|
---|
368 | return 0;
|
---|
369 | }
|
---|
370 |
|
---|
371 | /**
|
---|
372 | * Enumerate attached devices.
|
---|
373 | *
|
---|
374 | * @returns nothing.
|
---|
375 | * @param io_base The I/O base port of the controller.
|
---|
376 | */
|
---|
377 | void scsi_enumerate_attached_devices(uint16_t io_base)
|
---|
378 | {
|
---|
379 | int i;
|
---|
380 | uint8_t buffer[0x0200];
|
---|
381 | bio_dsk_t __far *bios_dsk;
|
---|
382 |
|
---|
383 | bios_dsk = read_word(0x0040, 0x000E) :> &EbdaData->bdisk;
|
---|
384 |
|
---|
385 | /* Go through target devices. */
|
---|
386 | for (i = 0; i < VBSCSI_MAX_DEVICES; i++)
|
---|
387 | {
|
---|
388 | uint8_t rc;
|
---|
389 | uint8_t aCDB[16];
|
---|
390 | uint8_t hd_index, devcount_scsi;
|
---|
391 |
|
---|
392 | aCDB[0] = SCSI_INQUIRY;
|
---|
393 | aCDB[1] = 0;
|
---|
394 | aCDB[2] = 0;
|
---|
395 | aCDB[3] = 0;
|
---|
396 | aCDB[4] = 5; /* Allocation length. */
|
---|
397 | aCDB[5] = 0;
|
---|
398 |
|
---|
399 | rc = scsi_cmd_data_in(io_base, i, aCDB, 6, buffer, 5);
|
---|
400 | if (rc != 0)
|
---|
401 | BX_PANIC("%s: SCSI_INQUIRY failed\n", __func__);
|
---|
402 |
|
---|
403 | devcount_scsi = bios_dsk->scsi_devcount;
|
---|
404 |
|
---|
405 | /* Check the attached device. */
|
---|
406 | if ( ((buffer[0] & 0xe0) == 0)
|
---|
407 | && ((buffer[0] & 0x1f) == 0x00))
|
---|
408 | {
|
---|
409 | DBG_SCSI("%s: Disk detected at %d\n", __func__, i);
|
---|
410 |
|
---|
411 | /* We add the disk only if the maximum is not reached yet. */
|
---|
412 | if (devcount_scsi < BX_MAX_SCSI_DEVICES)
|
---|
413 | {
|
---|
414 | uint64_t sectors, t;
|
---|
415 | uint32_t sector_size, cylinders;
|
---|
416 | uint16_t heads, sectors_per_track;
|
---|
417 | uint8_t hdcount;
|
---|
418 | uint8_t cmos_base;
|
---|
419 |
|
---|
420 | /* Issue a read capacity command now. */
|
---|
421 | _fmemset(aCDB, 0, sizeof(aCDB));
|
---|
422 | aCDB[0] = SCSI_SERVICE_ACT;
|
---|
423 | aCDB[1] = SCSI_READ_CAP_16;
|
---|
424 | aCDB[13] = 32; /* Allocation length. */
|
---|
425 |
|
---|
426 | rc = scsi_cmd_data_in(io_base, i, aCDB, 16, buffer, 32);
|
---|
427 | if (rc != 0)
|
---|
428 | BX_PANIC("%s: SCSI_READ_CAPACITY failed\n", __func__);
|
---|
429 |
|
---|
430 | /* The value returned is the last addressable LBA, not
|
---|
431 | * the size, which what "+ 1" is for.
|
---|
432 | */
|
---|
433 | sectors = swap_64(*(uint64_t *)buffer) + 1;
|
---|
434 |
|
---|
435 | sector_size = ((uint32_t)buffer[8] << 24)
|
---|
436 | | ((uint32_t)buffer[9] << 16)
|
---|
437 | | ((uint32_t)buffer[10] << 8)
|
---|
438 | | ((uint32_t)buffer[11]);
|
---|
439 |
|
---|
440 | /* We only support the disk if sector size is 512 bytes. */
|
---|
441 | if (sector_size != 512)
|
---|
442 | {
|
---|
443 | /* Leave a log entry. */
|
---|
444 | BX_INFO("Disk %d has an unsupported sector size of %u\n", i, sector_size);
|
---|
445 | continue;
|
---|
446 | }
|
---|
447 |
|
---|
448 | /* Get logical CHS geometry. */
|
---|
449 | switch (devcount_scsi)
|
---|
450 | {
|
---|
451 | case 0:
|
---|
452 | cmos_base = 0x90;
|
---|
453 | break;
|
---|
454 | case 1:
|
---|
455 | cmos_base = 0x98;
|
---|
456 | break;
|
---|
457 | case 2:
|
---|
458 | cmos_base = 0xA0;
|
---|
459 | break;
|
---|
460 | case 3:
|
---|
461 | cmos_base = 0xA8;
|
---|
462 | break;
|
---|
463 | default:
|
---|
464 | cmos_base = 0;
|
---|
465 | }
|
---|
466 |
|
---|
467 | if (cmos_base && inb_cmos(cmos_base + 7))
|
---|
468 | {
|
---|
469 | /* If provided, grab the logical geometry from CMOS. */
|
---|
470 | cylinders = inb_cmos(cmos_base + 0) + (inb_cmos(cmos_base + 1) << 8);
|
---|
471 | heads = inb_cmos(cmos_base + 2);
|
---|
472 | sectors_per_track = inb_cmos(cmos_base + 7);
|
---|
473 | }
|
---|
474 | else
|
---|
475 | {
|
---|
476 | /* Calculate default logical geometry. NB: Very different
|
---|
477 | * from default ATA/SATA logical geometry!
|
---|
478 | */
|
---|
479 | if (sectors >= (uint32_t)4 * 1024 * 1024)
|
---|
480 | {
|
---|
481 | heads = 255;
|
---|
482 | sectors_per_track = 63;
|
---|
483 | /* Approximate x / (255 * 63) using shifts */
|
---|
484 | t = (sectors >> 6) + (sectors >> 12);
|
---|
485 | cylinders = (t >> 8) + (t >> 16);
|
---|
486 | }
|
---|
487 | else if (sectors >= (uint32_t)2 * 1024 * 1024)
|
---|
488 | {
|
---|
489 | heads = 128;
|
---|
490 | sectors_per_track = 32;
|
---|
491 | cylinders = sectors >> 12;
|
---|
492 | }
|
---|
493 | else
|
---|
494 | {
|
---|
495 | heads = 64;
|
---|
496 | sectors_per_track = 32;
|
---|
497 | cylinders = sectors >> 11;
|
---|
498 | }
|
---|
499 | }
|
---|
500 |
|
---|
501 | /* Calculate index into the generic disk table. */
|
---|
502 | hd_index = devcount_scsi + BX_MAX_ATA_DEVICES;
|
---|
503 |
|
---|
504 | bios_dsk->scsidev[devcount_scsi].io_base = io_base;
|
---|
505 | bios_dsk->scsidev[devcount_scsi].target_id = i;
|
---|
506 | bios_dsk->devices[hd_index].type = DSK_TYPE_SCSI;
|
---|
507 | bios_dsk->devices[hd_index].device = DSK_DEVICE_HD;
|
---|
508 | bios_dsk->devices[hd_index].removable = 0;
|
---|
509 | bios_dsk->devices[hd_index].lock = 0;
|
---|
510 | bios_dsk->devices[hd_index].blksize = sector_size;
|
---|
511 | bios_dsk->devices[hd_index].translation = GEO_TRANSLATION_LBA;
|
---|
512 |
|
---|
513 | /* Write LCHS/PCHS values. */
|
---|
514 | bios_dsk->devices[hd_index].lchs.heads = heads;
|
---|
515 | bios_dsk->devices[hd_index].lchs.spt = sectors_per_track;
|
---|
516 | bios_dsk->devices[hd_index].pchs.heads = heads;
|
---|
517 | bios_dsk->devices[hd_index].pchs.spt = sectors_per_track;
|
---|
518 |
|
---|
519 | if (cylinders > 1024) {
|
---|
520 | bios_dsk->devices[hd_index].lchs.cylinders = 1024;
|
---|
521 | bios_dsk->devices[hd_index].pchs.cylinders = 1024;
|
---|
522 | } else {
|
---|
523 | bios_dsk->devices[hd_index].lchs.cylinders = (uint16_t)cylinders;
|
---|
524 | bios_dsk->devices[hd_index].pchs.cylinders = (uint16_t)cylinders;
|
---|
525 | }
|
---|
526 |
|
---|
527 | BX_INFO("SCSI %d-ID#%d: LCHS=%lu/%u/%u 0x%llx sectors\n", devcount_scsi,
|
---|
528 | i, (uint32_t)cylinders, heads, sectors_per_track, sectors);
|
---|
529 |
|
---|
530 | bios_dsk->devices[hd_index].sectors = sectors;
|
---|
531 |
|
---|
532 | /* Store the id of the disk in the ata hdidmap. */
|
---|
533 | hdcount = bios_dsk->hdcount;
|
---|
534 | bios_dsk->hdidmap[hdcount] = devcount_scsi + BX_MAX_ATA_DEVICES;
|
---|
535 | hdcount++;
|
---|
536 | bios_dsk->hdcount = hdcount;
|
---|
537 |
|
---|
538 | /* Update hdcount in the BDA. */
|
---|
539 | hdcount = read_byte(0x40, 0x75);
|
---|
540 | hdcount++;
|
---|
541 | write_byte(0x40, 0x75, hdcount);
|
---|
542 |
|
---|
543 | devcount_scsi++;
|
---|
544 | }
|
---|
545 | else
|
---|
546 | {
|
---|
547 | /* We reached the maximum of SCSI disks we can boot from. We can quit detecting. */
|
---|
548 | break;
|
---|
549 | }
|
---|
550 | }
|
---|
551 | else if ( ((buffer[0] & 0xe0) == 0)
|
---|
552 | && ((buffer[0] & 0x1f) == 0x05))
|
---|
553 | {
|
---|
554 | uint8_t cdcount;
|
---|
555 | uint8_t removable;
|
---|
556 |
|
---|
557 | BX_INFO("SCSI %d-ID#%d: CD/DVD-ROM\n", devcount_scsi, i);
|
---|
558 |
|
---|
559 | /* Calculate index into the generic device table. */
|
---|
560 | hd_index = devcount_scsi + BX_MAX_ATA_DEVICES;
|
---|
561 |
|
---|
562 | removable = buffer[1] & 0x80 ? 1 : 0;
|
---|
563 |
|
---|
564 | bios_dsk->scsidev[devcount_scsi].io_base = io_base;
|
---|
565 | bios_dsk->scsidev[devcount_scsi].target_id = i;
|
---|
566 | bios_dsk->devices[hd_index].type = DSK_TYPE_SCSI;
|
---|
567 | bios_dsk->devices[hd_index].device = DSK_DEVICE_CDROM;
|
---|
568 | bios_dsk->devices[hd_index].removable = removable;
|
---|
569 | bios_dsk->devices[hd_index].blksize = 2048;
|
---|
570 | bios_dsk->devices[hd_index].translation = GEO_TRANSLATION_NONE;
|
---|
571 |
|
---|
572 | /* Store the ID of the device in the BIOS cdidmap. */
|
---|
573 | cdcount = bios_dsk->cdcount;
|
---|
574 | bios_dsk->cdidmap[cdcount] = devcount_scsi + BX_MAX_ATA_DEVICES;
|
---|
575 | cdcount++;
|
---|
576 | bios_dsk->cdcount = cdcount;
|
---|
577 |
|
---|
578 | devcount_scsi++;
|
---|
579 | }
|
---|
580 | else
|
---|
581 | DBG_SCSI("%s: No supported device detected at %d\n", __func__, i);
|
---|
582 |
|
---|
583 | bios_dsk->scsi_devcount = devcount_scsi;
|
---|
584 | }
|
---|
585 | }
|
---|
586 |
|
---|
587 | void scsi_pci_init(uint16_t vendor_id, uint16_t device_id)
|
---|
588 | {
|
---|
589 | uint16_t bus_dev_fn;
|
---|
590 |
|
---|
591 | bus_dev_fn = pci_find_device(vendor_id, device_id);
|
---|
592 | if (bus_dev_fn == -1) {
|
---|
593 | DBG_SCSI("%s: Adapter %x:%x not found, how come?!\n", __func__, vendor_id, device_id);
|
---|
594 | return;
|
---|
595 | }
|
---|
596 |
|
---|
597 | DBG_SCSI("%s: Adapter %x:%x found at %x, enabling BM\n", __func__, vendor_id, device_id, bus_dev_fn);
|
---|
598 | /* Enable PCI memory, I/O, bus mastering access in command register. */
|
---|
599 | pci_write_config_word(bus_dev_fn >> 8, (uint8_t)bus_dev_fn, 4, 0x7);
|
---|
600 | }
|
---|
601 |
|
---|
602 | /**
|
---|
603 | * Init the SCSI driver and detect attached disks.
|
---|
604 | */
|
---|
605 | void BIOSCALL scsi_init(void)
|
---|
606 | {
|
---|
607 | uint8_t identifier;
|
---|
608 | bio_dsk_t __far *bios_dsk;
|
---|
609 |
|
---|
610 | bios_dsk = read_word(0x0040, 0x000E) :> &EbdaData->bdisk;
|
---|
611 |
|
---|
612 | bios_dsk->scsi_devcount = 0;
|
---|
613 |
|
---|
614 | identifier = 0;
|
---|
615 |
|
---|
616 | /* Detect the BusLogic adapter. */
|
---|
617 | outb(BUSLOGIC_BIOS_IO_PORT+VBSCSI_REGISTER_IDENTIFY, 0x55);
|
---|
618 | identifier = inb(BUSLOGIC_BIOS_IO_PORT+VBSCSI_REGISTER_IDENTIFY);
|
---|
619 |
|
---|
620 | if (identifier == 0x55)
|
---|
621 | {
|
---|
622 | /* Detected - Enumerate attached devices. */
|
---|
623 | DBG_SCSI("%s: BusLogic SCSI adapter detected\n", __func__);
|
---|
624 | outb(BUSLOGIC_BIOS_IO_PORT+VBSCSI_REGISTER_RESET, 0);
|
---|
625 | scsi_enumerate_attached_devices(BUSLOGIC_BIOS_IO_PORT);
|
---|
626 | scsi_pci_init(0x104B, 0x1040);
|
---|
627 | }
|
---|
628 | else
|
---|
629 | {
|
---|
630 | DBG_SCSI("%s: BusLogic SCSI adapter not detected\n", __func__);
|
---|
631 | }
|
---|
632 |
|
---|
633 | /* Detect the LSI Logic parallel SCSI adapter. */
|
---|
634 | outb(LSILOGIC_BIOS_IO_PORT+VBSCSI_REGISTER_IDENTIFY, 0x55);
|
---|
635 | identifier = inb(LSILOGIC_BIOS_IO_PORT+VBSCSI_REGISTER_IDENTIFY);
|
---|
636 |
|
---|
637 | if (identifier == 0x55)
|
---|
638 | {
|
---|
639 | /* Detected - Enumerate attached devices. */
|
---|
640 | DBG_SCSI("%s: LSI Logic SCSI adapter detected\n", __func__);
|
---|
641 | outb(LSILOGIC_BIOS_IO_PORT+VBSCSI_REGISTER_RESET, 0);
|
---|
642 | scsi_enumerate_attached_devices(LSILOGIC_BIOS_IO_PORT);
|
---|
643 | scsi_pci_init(0x1000, 0x0030);
|
---|
644 | }
|
---|
645 | else
|
---|
646 | {
|
---|
647 | DBG_SCSI("%s: LSI Logic SCSI adapter not detected\n", __func__);
|
---|
648 | }
|
---|
649 |
|
---|
650 | /* Detect the LSI Logic SAS adapter. */
|
---|
651 | outb(LSILOGIC_SAS_BIOS_IO_PORT+VBSCSI_REGISTER_IDENTIFY, 0x55);
|
---|
652 | identifier = inb(LSILOGIC_SAS_BIOS_IO_PORT+VBSCSI_REGISTER_IDENTIFY);
|
---|
653 |
|
---|
654 | if (identifier == 0x55)
|
---|
655 | {
|
---|
656 | /* Detected - Enumerate attached devices. */
|
---|
657 | DBG_SCSI("%s: LSI Logic SAS adapter detected\n", __func__);
|
---|
658 | outb(LSILOGIC_SAS_BIOS_IO_PORT+VBSCSI_REGISTER_RESET, 0);
|
---|
659 | scsi_enumerate_attached_devices(LSILOGIC_SAS_BIOS_IO_PORT);
|
---|
660 | scsi_pci_init(0x1000, 0x0054);
|
---|
661 | }
|
---|
662 | else
|
---|
663 | {
|
---|
664 | DBG_SCSI("%s: LSI Logic SAS adapter not detected\n", __func__);
|
---|
665 | }
|
---|
666 | }
|
---|