VirtualBox

source: vbox/trunk/src/VBox/Devices/PC/BIOS/scsi.c@ 43475

Last change on this file since 43475 was 43475, checked in by vboxsync, 12 years ago

BIOS: Support for SCSI CD-ROMs, larger than 64K SCSI transfers.

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

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette