VirtualBox

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

Last change on this file since 28684 was 28684, checked in by vboxsync, 15 years ago

VBoxSCSI: Always reset the state if the protocol is violated and make it possible to reset the state from the BIOS

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 17.1 KB
Line 
1/* $Id: scsi.c 28684 2010-04-24 14:22:22Z vboxsync $ */
2/** @file
3 * SCSI host adapter driver to boot from SCSI disks
4 */
5
6/*
7 * Copyright (C) 2004-2009 Sun Microsystems, Inc.
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 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
18 * Clara, CA 95054 USA or visit http://www.sun.com if you need
19 * additional information or have any questions.
20 */
21
22/* The I/O port of the BusLogic SCSI adapter. */
23#define BUSLOGIC_ISA_IO_PORT 0x330
24/* The I/O port of the LsiLogic SCSI adapter. */
25#define LSILOGIC_ISA_IO_PORT 0x340
26
27#define VBOXSCSI_REGISTER_STATUS 0
28#define VBOXSCSI_REGISTER_COMMAND 0
29#define VBOXSCSI_REGISTER_DATA_IN 1
30#define VBOXSCSI_REGISTER_IDENTIFY 2
31#define VBOXSCSI_REGISTER_RESET 3
32
33#define VBOXSCSI_MAX_DEVICES 16 /* Maximum number of devices a SCSI device can have. */
34
35/* Command opcodes. */
36#define SCSI_INQUIRY 0x12
37#define SCSI_READ_CAPACITY 0x25
38#define SCSI_READ_10 0x28
39#define SCSI_WRITE_10 0x2a
40
41/* Data transfer direction. */
42#define SCSI_TXDIR_FROM_DEVICE 0
43#define SCSI_TXDIR_TO_DEVICE 1
44
45#define VBOXSCSI_BUSY (1 << 0)
46
47//#define VBOX_SCSI_DEBUG 1 /* temporary */
48
49#ifdef VBOX_SCSI_DEBUG
50# define VBOXSCSI_DEBUG(a...) BX_INFO(a)
51#else
52# define VBOXSCSI_DEBUG(a...)
53#endif
54
55int scsi_cmd_data_in(io_base, device_id, cdb_segment, aCDB, cbCDB, segment, offset, cbBuffer)
56 Bit16u io_base, aCDB, offset, cbBuffer, segment, cdb_segment;
57 Bit8u device_id, cbCDB;
58{
59 /* Check that the adapter is ready. */
60 Bit8u status;
61 Bit16u i;
62
63 do
64 {
65 status = inb(io_base+VBOXSCSI_REGISTER_STATUS);
66 } while (status & VBOXSCSI_BUSY);
67
68 /* Write target ID. */
69 outb(io_base+VBOXSCSI_REGISTER_COMMAND, device_id);
70 /* Write transfer direction. */
71 outb(io_base+VBOXSCSI_REGISTER_COMMAND, SCSI_TXDIR_FROM_DEVICE);
72 /* Write the CDB size. */
73 outb(io_base+VBOXSCSI_REGISTER_COMMAND, cbCDB);
74 /* Write buffer size. */
75 outb(io_base+VBOXSCSI_REGISTER_COMMAND, cbBuffer);
76 outb(io_base+VBOXSCSI_REGISTER_COMMAND, (cbBuffer >> 8));
77 /* Write the CDB. */
78 for (i = 0; i < cbCDB; i++)
79 outb(io_base+VBOXSCSI_REGISTER_COMMAND, read_byte(cdb_segment, aCDB + i));
80
81 /* Now wait for the command to complete. */
82 do
83 {
84 status = inb(io_base+VBOXSCSI_REGISTER_STATUS);
85 } while (status & VBOXSCSI_BUSY);
86
87#if 0
88 /* Get the read data. */
89 for (i = 0; i < cbBuffer; i++)
90 {
91 Bit8u data;
92
93 data = inb(io_base+VBOXSCSI_REGISTER_DATA_IN);
94 write_byte(segment, offset+i, data);
95
96 VBOXSCSI_DEBUG("buffer[%d]=%x\n", i, data);
97 }
98#else
99 io_base = io_base + VBOXSCSI_REGISTER_DATA_IN;
100
101ASM_START
102 push bp
103 mov bp, sp
104 mov di, _scsi_cmd_data_in.offset + 2[bp]
105 mov ax, _scsi_cmd_data_in.segment + 2[bp]
106 mov cx, _scsi_cmd_data_in.cbBuffer + 2[bp]
107
108 mov es, ax ;; segment in es
109 mov dx, _scsi_cmd_data_in.io_base + 2[bp] ;; SCSI data read port
110
111 rep
112 insb ;; CX dwords transfered from port(DX) to ES:[DI]
113
114 pop bp
115ASM_END
116#endif
117
118 return 0;
119}
120
121int scsi_cmd_data_out(io_base, device_id, cdb_segment, aCDB, cbCDB, segment, offset, cbBuffer)
122 Bit16u io_base, aCDB, offset, cbBuffer, segment, cdb_segment;
123 Bit8u device_id, cbCDB;
124{
125 /* Check that the adapter is ready. */
126 Bit8u status;
127 Bit16u i;
128
129 do
130 {
131 status = inb(io_base+VBOXSCSI_REGISTER_STATUS);
132 } while (status & VBOXSCSI_BUSY);
133
134 /* Write target ID. */
135 outb(io_base+VBOXSCSI_REGISTER_COMMAND, device_id);
136 /* Write transfer direction. */
137 outb(io_base+VBOXSCSI_REGISTER_COMMAND, SCSI_TXDIR_TO_DEVICE);
138 /* Write the CDB size. */
139 outb(io_base+VBOXSCSI_REGISTER_COMMAND, cbCDB);
140 /* Write buffer size. */
141 outb(io_base+VBOXSCSI_REGISTER_COMMAND, cbBuffer);
142 outb(io_base+VBOXSCSI_REGISTER_COMMAND, (cbBuffer >> 8));
143 /* Write the CDB. */
144 for (i = 0; i < cbCDB; i++)
145 outb(io_base+VBOXSCSI_REGISTER_COMMAND, read_byte(cdb_segment, aCDB + i));
146
147 /* Write data to I/O port. */
148 for (i = 0; i < cbBuffer; i++)
149 {
150 Bit8u data;
151
152 data = read_byte(segment, offset+i);
153 outb(io_base+VBOXSCSI_REGISTER_DATA_IN, data);
154
155 VBOXSCSI_DEBUG("buffer[%d]=%x\n", i, data);
156 }
157
158 /* Now wait for the command to complete. */
159 do
160 {
161 status = inb(io_base+VBOXSCSI_REGISTER_STATUS);
162 } while (status & VBOXSCSI_BUSY);
163
164 return 0;
165}
166
167
168/**
169 * Read sectors from an attached scsi device.
170 *
171 * @returns status code.
172 * @param device_id Id of the SCSI device to read from.
173 * @param count The number of sectors to read.
174 * @param lba The start sector to read from.
175 * @param segment The segment the buffer is in.
176 * @param offset The offset of the buffer.
177 */
178int scsi_read_sectors(device_id, count, lba, segment, offset)
179 Bit8u device_id;
180 Bit16u count, segment, offset;
181 Bit32u lba;
182{
183 Bit8u rc;
184 Bit8u aCDB[10];
185 Bit16u io_base, ebda_seg;
186 Bit8u target_id;
187
188 if (device_id > BX_MAX_SCSI_DEVICES)
189 BX_PANIC("scsi_read_sectors: device_id out of range %d\n", device_id);
190
191 ebda_seg = read_word(0x0040, 0x000E);
192 // Reset count of transferred data
193 write_word(ebda_seg, &EbdaData->ata.trsfsectors,0);
194 write_dword(ebda_seg, &EbdaData->ata.trsfbytes,0L);
195
196 /* Prepare CDB */
197 write_byte(get_SS(), aCDB + 0, SCSI_READ_10);
198 write_byte(get_SS(), aCDB + 1, 0);
199 write_byte(get_SS(), aCDB + 2, (uint8_t)(lba >> 24));
200 write_byte(get_SS(), aCDB + 3, (uint8_t)(lba >> 16));
201 write_byte(get_SS(), aCDB + 4, (uint8_t)(lba >> 8));
202 write_byte(get_SS(), aCDB + 5, (uint8_t)(lba));
203 write_byte(get_SS(), aCDB + 6, 0);
204 write_byte(get_SS(), aCDB + 7, (uint8_t)(count >> 8));
205 write_byte(get_SS(), aCDB + 8, (uint8_t)(count));
206 write_byte(get_SS(), aCDB + 9, 0);
207
208 io_base = read_word(ebda_seg, &EbdaData->scsi.devices[device_id].io_base);
209 target_id = read_byte(ebda_seg, &EbdaData->scsi.devices[device_id].target_id);
210
211 rc = scsi_cmd_data_in(io_base, target_id, get_SS(), aCDB, 10, segment, offset, (count * 512));
212
213 if (!rc)
214 {
215 write_word(ebda_seg, &EbdaData->ata.trsfsectors, count);
216 write_dword(ebda_seg, &EbdaData->ata.trsfbytes, (count * 512));
217 }
218
219 return rc;
220}
221
222/**
223 * Write sectors to an attached scsi device.
224 *
225 * @returns status code.
226 * @param device_id Id of the SCSI device to write to.
227 * @param count The number of sectors to write.
228 * @param lba The start sector to write to.
229 * @param segment The segment the buffer is in.
230 * @param offset The offset of the buffer.
231 */
232int scsi_write_sectors(device_id, count, lba, segment, offset)
233 Bit8u device_id;
234 Bit16u count, segment, offset;
235 Bit32u lba;
236{
237 Bit8u rc;
238 Bit8u aCDB[10];
239 Bit16u io_base, ebda_seg;
240 Bit8u target_id;
241
242 if (device_id > BX_MAX_SCSI_DEVICES)
243 BX_PANIC("scsi_write_sectors: device_id out of range %d\n", device_id);
244
245 ebda_seg = read_word(0x0040, 0x000E);
246 // Reset count of transferred data
247 write_word(ebda_seg, &EbdaData->ata.trsfsectors,0);
248 write_dword(ebda_seg, &EbdaData->ata.trsfbytes,0L);
249
250 /* Prepare CDB */
251 write_byte(get_SS(), aCDB + 0, SCSI_WRITE_10);
252 write_byte(get_SS(), aCDB + 1, 0);
253 write_byte(get_SS(), aCDB + 2, (uint8_t)(lba >> 24));
254 write_byte(get_SS(), aCDB + 3, (uint8_t)(lba >> 16));
255 write_byte(get_SS(), aCDB + 4, (uint8_t)(lba >> 8));
256 write_byte(get_SS(), aCDB + 5, (uint8_t)(lba));
257 write_byte(get_SS(), aCDB + 6, 0);
258 write_byte(get_SS(), aCDB + 7, (uint8_t)(count >> 8));
259 write_byte(get_SS(), aCDB + 8, (uint8_t)(count));
260 write_byte(get_SS(), aCDB + 9, 0);
261
262 io_base = read_word(ebda_seg, &EbdaData->scsi.devices[device_id].io_base);
263 target_id = read_byte(ebda_seg, &EbdaData->scsi.devices[device_id].target_id);
264
265 rc = scsi_cmd_data_out(io_base, target_id, get_SS(), aCDB, 10, segment, offset, (count * 512));
266
267 if (!rc)
268 {
269 write_word(ebda_seg, &EbdaData->ata.trsfsectors, count);
270 write_dword(ebda_seg, &EbdaData->ata.trsfbytes, (count * 512));
271 }
272
273 return rc;
274}
275
276/**
277 * Enumerate attached devices.
278 *
279 * @returns nothing.
280 * @param io_base The I/O base port of the controller.
281 */
282void scsi_enumerate_attached_devices(io_base)
283 Bit16u io_base;
284{
285 Bit16u ebda_seg;
286 Bit8u i;
287 Bit8u buffer[0x0200];
288
289 ebda_seg = read_word(0x0040, 0x000E);
290
291 write_byte(ebda_seg, &EbdaData->scsi.hdcount, 0);
292
293 /* Go through target devices. */
294 for (i = 0; i < VBOXSCSI_MAX_DEVICES; i++)
295 {
296 Bit8u rc;
297 Bit8u z;
298 Bit8u aCDB[10];
299
300 write_byte(get_SS(), aCDB + 0, SCSI_INQUIRY);
301 write_byte(get_SS(), aCDB + 1, 0);
302 write_byte(get_SS(), aCDB + 2, 0);
303 write_byte(get_SS(), aCDB + 3, 0);
304 write_byte(get_SS(), aCDB + 4, 5); /* Allocation length. */
305 write_byte(get_SS(), aCDB + 5, 0);
306
307 rc = scsi_cmd_data_in(io_base, i, get_SS(), aCDB, 6, get_SS(), buffer, 5);
308 if (rc != 0)
309 BX_PANIC("scsi_enumerate_attached_devices: SCSI_INQUIRY failed\n");
310
311 /* Check if there is a disk attached. */
312 if ( ((buffer[0] & 0xe0) == 0)
313 && ((buffer[0] & 0x1f) == 0x00))
314 {
315 VBOXSCSI_DEBUG("scsi_enumerate_attached_devices: Disk detected at %d\n", i);
316
317 /* We add the disk only if the maximum is not reached yet. */
318 if (read_byte(ebda_seg, &EbdaData->scsi.hdcount) < BX_MAX_SCSI_DEVICES)
319 {
320 Bit32u sectors, sector_size, cylinders;
321 Bit16u heads, sectors_per_track;
322 Bit8u hdcount, hdcount_scsi;
323
324 /* Issue a read capacity command now. */
325 write_byte(get_SS(), aCDB + 0, SCSI_READ_CAPACITY);
326 memsetb(get_SS(), aCDB + 1, 0, 9);
327
328 rc = scsi_cmd_data_in(io_base, i, get_SS(), aCDB, 10, get_SS(), buffer, 8);
329 if (rc != 0)
330 BX_PANIC("scsi_enumerate_attached_devices: SCSI_READ_CAPACITY failed\n");
331
332 /* Build sector number and size from the buffer. */
333 sectors = ((uint32_t)read_byte(get_SS(), buffer + 0) << 24)
334 | ((uint32_t)read_byte(get_SS(), buffer + 1) << 16)
335 | ((uint32_t)read_byte(get_SS(), buffer + 2) << 8)
336 | ((uint32_t)read_byte(get_SS(), buffer + 3));
337
338 sector_size = ((uint32_t)read_byte(get_SS(), buffer + 4) << 24)
339 | ((uint32_t)read_byte(get_SS(), buffer + 5) << 16)
340 | ((uint32_t)read_byte(get_SS(), buffer + 6) << 8)
341 | ((uint32_t)read_byte(get_SS(), buffer + 7));
342
343 /* We only support the disk if sector size is 512 bytes. */
344 if (sector_size != 512)
345 {
346 /* Leave a log entry. */
347 BX_INFO("Disk %d has an unsupported sector size of %u\n", i, sector_size);
348 continue;
349 }
350
351 /* We need to calculate the geometry for the disk. */
352 if (io_base == BUSLOGIC_ISA_IO_PORT)
353 {
354 /* This is from the BusLogic driver in the Linux kernel. */
355 if (sectors >= 4 * 1024 * 1024)
356 {
357 heads = 255;
358 sectors_per_track = 63;
359 }
360 else if (sectors >= 2 * 1024 * 1024)
361 {
362 heads = 128;
363 sectors_per_track = 32;
364 }
365 else
366 {
367 heads = 64;
368 sectors_per_track = 32;
369 }
370 cylinders = (uint32_t)(sectors / (heads * sectors_per_track));
371 }
372 else if (io_base == LSILOGIC_ISA_IO_PORT)
373 {
374 /* This is from the BusLogic driver in the Linux kernel. */
375 if (sectors >= 4 * 1024 * 1024)
376 {
377 heads = 255;
378 sectors_per_track = 63;
379 }
380 else if (sectors >= 2 * 1024 * 1024)
381 {
382 heads = 128;
383 sectors_per_track = 32;
384 }
385 else
386 {
387 heads = 64;
388 sectors_per_track = 32;
389 }
390 cylinders = (uint32_t)(sectors / (heads * sectors_per_track));
391 }
392 hdcount_scsi = read_byte(ebda_seg, &EbdaData->scsi.hdcount);
393
394 write_word(ebda_seg, &EbdaData->scsi.devices[hdcount_scsi].io_base, io_base);
395 write_byte(ebda_seg, &EbdaData->scsi.devices[hdcount_scsi].target_id, i);
396 write_byte(ebda_seg, &EbdaData->scsi.devices[hdcount_scsi].device_info.type, ATA_TYPE_SCSI);
397 write_byte(ebda_seg, &EbdaData->scsi.devices[hdcount_scsi].device_info.device, ATA_DEVICE_HD);
398 write_byte(ebda_seg, &EbdaData->scsi.devices[hdcount_scsi].device_info.removable, 0);
399 write_byte(ebda_seg, &EbdaData->scsi.devices[hdcount_scsi].device_info.lock, 0);
400 write_byte(ebda_seg, &EbdaData->scsi.devices[hdcount_scsi].device_info.mode, ATA_MODE_PIO16);
401 write_word(ebda_seg, &EbdaData->scsi.devices[hdcount_scsi].device_info.blksize, sector_size);
402 write_byte(ebda_seg, &EbdaData->scsi.devices[hdcount_scsi].device_info.translation, ATA_TRANSLATION_LBA);
403
404 /* Write lchs values. */
405 write_word(ebda_seg, &EbdaData->scsi.devices[hdcount_scsi].device_info.lchs.heads, heads);
406 write_word(ebda_seg, &EbdaData->scsi.devices[hdcount_scsi].device_info.lchs.spt, sectors_per_track);
407 if (cylinders > 1024)
408 write_word(ebda_seg, &EbdaData->scsi.devices[hdcount_scsi].device_info.lchs.cylinders, 1024);
409 else
410 write_word(ebda_seg, &EbdaData->scsi.devices[hdcount_scsi].device_info.lchs.cylinders, (Bit16u)cylinders);
411
412 /* Write pchs values. */
413 write_word(ebda_seg, &EbdaData->scsi.devices[hdcount_scsi].device_info.pchs.heads, heads);
414 write_word(ebda_seg, &EbdaData->scsi.devices[hdcount_scsi].device_info.pchs.spt, sectors_per_track);
415 if (cylinders > 1024)
416 write_word(ebda_seg, &EbdaData->scsi.devices[hdcount_scsi].device_info.pchs.cylinders, 1024);
417 else
418 write_word(ebda_seg, &EbdaData->scsi.devices[hdcount_scsi].device_info.pchs.cylinders, (Bit16u)cylinders);
419
420 write_dword(ebda_seg, &EbdaData->scsi.devices[hdcount_scsi].device_info.sectors, sectors);
421
422 /* Store the id of the disk in the ata hdidmap. */
423 hdcount = read_byte(ebda_seg, &EbdaData->ata.hdcount);
424 write_byte(ebda_seg, &EbdaData->ata.hdidmap[hdcount], hdcount_scsi + BX_MAX_ATA_DEVICES);
425 hdcount++;
426 write_byte(ebda_seg, &EbdaData->ata.hdcount, hdcount);
427
428 hdcount_scsi++;
429 write_byte(ebda_seg, &EbdaData->scsi.hdcount, hdcount_scsi);
430 }
431 else
432 {
433 /* We reached the maximum of SCSI disks we can boot from. We can quit detecting. */
434 break;
435 }
436 }
437 else
438 VBOXSCSI_DEBUG("scsi_enumerate_attached_devices: No disk detected at %d\n", i);
439 }
440}
441
442/**
443 * Init the SCSI driver and detect attached disks.
444 */
445void scsi_init( )
446{
447 Bit8u identifier;
448
449 identifier = 0;
450
451 /* Detect BusLogic adapter. */
452 outb(BUSLOGIC_ISA_IO_PORT+VBOXSCSI_REGISTER_IDENTIFY, 0x55);
453 identifier = inb(BUSLOGIC_ISA_IO_PORT+VBOXSCSI_REGISTER_IDENTIFY);
454
455 if (identifier == 0x55)
456 {
457 /* Detected - Enumerate attached devices. */
458 VBOXSCSI_DEBUG("scsi_init: BusLogic SCSI adapter detected\n");
459 outb(BUSLOGIC_ISA_IO_PORT+VBOXSCSI_REGISTER_RESET, 0);
460 scsi_enumerate_attached_devices(BUSLOGIC_ISA_IO_PORT);
461 }
462 else
463 {
464 VBOXSCSI_DEBUG("scsi_init: BusLogic SCSI adapter not detected\n");
465 }
466
467 /* Detect LsiLogic adapter. */
468 outb(LSILOGIC_ISA_IO_PORT+VBOXSCSI_REGISTER_IDENTIFY, 0x55);
469 identifier = inb(LSILOGIC_ISA_IO_PORT+VBOXSCSI_REGISTER_IDENTIFY);
470
471 if (identifier == 0x55)
472 {
473 /* Detected - Enumerate attached devices. */
474 VBOXSCSI_DEBUG("scsi_init: LsiLogic SCSI adapter detected\n");
475 outb(LSILOGIC_ISA_IO_PORT+VBOXSCSI_REGISTER_RESET, 0);
476 scsi_enumerate_attached_devices(LSILOGIC_ISA_IO_PORT);
477 }
478 else
479 {
480 VBOXSCSI_DEBUG("scsi_init: LsiLogic SCSI adapter not detected\n");
481 }
482}
483
Note: See TracBrowser for help on using the repository browser.

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