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