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