VirtualBox

source: vbox/trunk/src/VBox/Devices/Storage/DevBusLogic.cpp@ 28065

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

Storage: Convert from PDMDATASEG to RTSGSEG to avoid casting between those two in VBoxHDD and more async I/O updates

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 106.6 KB
Line 
1/* $Id: DevBusLogic.cpp 28065 2010-04-07 20:54:34Z vboxsync $ */
2/** @file
3 * VBox storage devices: BusLogic SCSI host adapter BT-958.
4 */
5
6/*
7 * Copyright (C) 2006-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/* Implemented looking at the driver source in the linux kernel (drivers/scsi/BusLogic.[ch]). */
23
24/*******************************************************************************
25* Header Files *
26*******************************************************************************/
27//#define DEBUG
28#define LOG_GROUP LOG_GROUP_DEV_BUSLOGIC
29#include <VBox/pdmdev.h>
30#include <VBox/pdmifs.h>
31#include <VBox/scsi.h>
32#include <iprt/assert.h>
33#include <iprt/string.h>
34#include <iprt/log.h>
35#ifdef IN_RING3
36# include <iprt/alloc.h>
37# include <iprt/memcache.h>
38# include <iprt/param.h>
39# include <iprt/uuid.h>
40#endif
41
42#include "VBoxSCSI.h"
43#include "../Builtins.h"
44
45/* Maximum number of attached devices the adapter can handle. */
46#define BUSLOGIC_MAX_DEVICES 16
47
48/* Maximum number of scatter gather elements this device can handle. */
49#define BUSLOGIC_MAX_SCATTER_GATHER_LIST_SIZE 128
50
51/* Size of the command buffer. */
52#define BUSLOGIC_COMMAND_SIZE_MAX 5
53
54/* Size of the reply buffer. */
55#define BUSLOGIC_REPLY_SIZE_MAX 64
56
57/* I/O port registered in the ISA compatible range to let the BIOS access
58 * the controller.
59 */
60#define BUSLOGIC_ISA_IO_PORT 0x330
61
62/** State saved version. */
63#define BUSLOGIC_SAVED_STATE_MINOR_VERSION 1
64
65/**
66 * State of a device attached to the buslogic host adapter.
67 *
68 * @implements PDMIBASE
69 * @implements PDMISCSIPORT
70 * @implements PDMILEDPORTS
71 */
72typedef struct BUSLOGICDEVICE
73{
74 /** Pointer to the owning buslogic device instance. - R3 pointer */
75 R3PTRTYPE(struct BUSLOGIC *) pBusLogicR3;
76 /** Pointer to the owning buslogic device instance. - R0 pointer */
77 R0PTRTYPE(struct BUSLOGIC *) pBusLogicR0;
78 /** Pointer to the owning buslogic device instance. - RC pointer */
79 RCPTRTYPE(struct BUSLOGIC *) pBusLogicRC;
80
81 /** Flag whether device is present. */
82 bool fPresent;
83 /** LUN of the device. */
84 RTUINT iLUN;
85
86#if HC_ARCH_BITS == 64
87 uint32_t Alignment0;
88#endif
89
90 /** Our base interace. */
91 PDMIBASE IBase;
92 /** SCSI port interface. */
93 PDMISCSIPORT ISCSIPort;
94 /** Led interface. */
95 PDMILEDPORTS ILed;
96 /** Pointer to the attached driver's base interface. */
97 R3PTRTYPE(PPDMIBASE) pDrvBase;
98 /** Pointer to the underlying SCSI connector interface. */
99 R3PTRTYPE(PPDMISCSICONNECTOR) pDrvSCSIConnector;
100 /** The status LED state for this device. */
101 PDMLED Led;
102
103#if HC_ARCH_BITS == 64
104 uint32_t Alignment1;
105#endif
106
107 /** Number of outstanding tasks on the port. */
108 volatile uint32_t cOutstandingRequests;
109
110} BUSLOGICDEVICE, *PBUSLOGICDEVICE;
111
112/*
113 * Commands the BusLogic adapter supports.
114 */
115enum BUSLOGICCOMMAND
116{
117 BUSLOGICCOMMAND_TEST_COMMAND_COMPLETE_INTERRUPT = 0x00,
118 BUSLOGICCOMMAND_INITIALIZE_MAILBOX = 0x01,
119 BUSLOGICCOMMAND_EXECUTE_MAILBOX_COMMAND = 0x02,
120 BUSLOGICCOMMAND_EXECUTE_BIOS_COMMAND = 0x03,
121 BUSLOGICCOMMAND_INQUIRE_BOARD_ID = 0x04,
122 BUSLOGICCOMMAND_ENABLE_OUTGOING_MAILBOX_AVAILABLE_INTERRUPT = 0x05,
123 BUSLOGICCOMMAND_SET_SCSI_SELECTION_TIMEOUT = 0x06,
124 BUSLOGICCOMMAND_SET_PREEMPT_TIME_ON_BUS = 0x07,
125 BUSLOGICCOMMAND_SET_TIME_OFF_BUS = 0x08,
126 BUSLOGICCOMMAND_SET_BUS_TRANSFER_RATE = 0x09,
127 BUSLOGICCOMMAND_INQUIRE_INSTALLED_DEVICES_ID_0_TO_7 = 0x0a,
128 BUSLOGICCOMMAND_INQUIRE_CONFIGURATION = 0x0b,
129 BUSLOGICCOMMAND_ENABLE_TARGET_MODE = 0x0c,
130 BUSLOGICCOMMAND_INQUIRE_SETUP_INFORMATION = 0x0d,
131 BUSLOGICCOMMAND_WRITE_ADAPTER_LOCAL_RAM = 0x1a,
132 BUSLOGICCOMMAND_READ_ADAPTER_LOCAL_RAM = 0x1b,
133 BUSLOGICCOMMAND_WRITE_BUSMASTER_CHIP_FIFO = 0x1c,
134 BUSLOGICCOMMAND_READ_BUSMASTER_CHIP_FIFO = 0x1d,
135 BUSLOGICCOMMAND_ECHO_COMMAND_DATA = 0x1f,
136 BUSLOGICCOMMAND_HOST_ADAPTER_DIAGNOSTIC = 0x20,
137 BUSLOGICCOMMAND_SET_ADAPTER_OPTIONS = 0x21,
138 BUSLOGICCOMMAND_INQUIRE_INSTALLED_DEVICES_ID_8_TO_15 = 0x23,
139 BUSLOGICCOMMAND_INQUIRE_TARGET_DEVICES = 0x24,
140 BUSLOGICCOMMAND_DISABLE_HOST_ADAPTER_INTERRUPT = 0x25,
141 BUSLOGICCOMMAND_INITIALIZE_EXTENDED_MAILBOX = 0x81,
142 BUSLOGICCOMMAND_EXECUTE_SCSI_COMMAND = 0x83,
143 BUSLOGICCOMMAND_INQUIRE_FIRMWARE_VERSION_3RD_LETTER = 0x84,
144 BUSLOGICCOMMAND_INQUIRE_FIRMWARE_VERSION_LETTER = 0x85,
145 BUSLOGICCOMMAND_INQUIRE_PCI_HOST_ADAPTER_INFORMATION = 0x86,
146 BUSLOGICCOMMAND_INQUIRE_HOST_ADAPTER_MODEL_NUMBER = 0x8b,
147 BUSLOGICCOMMAND_INQUIRE_SYNCHRONOUS_PERIOD = 0x8c,
148 BUSLOGICCOMMAND_INQUIRE_EXTENDED_SETUP_INFORMATION = 0x8d,
149 BUSLOGICCOMMAND_ENABLE_STRICT_ROUND_ROBIN_MODE = 0x8f,
150 BUSLOGICCOMMAND_STORE_HOST_ADAPTER_LOCAL_RAM = 0x90,
151 BUSLOGICCOMMAND_FETCH_HOST_ADAPTER_LOCAL_RAM = 0x91,
152 BUSLOGICCOMMAND_STORE_LOCAL_DATA_IN_EEPROM = 0x92,
153 BUSLOGICCOMMAND_UPLOAD_AUTO_SCSI_CODE = 0x94,
154 BUSLOGICCOMMAND_MODIFY_IO_ADDRESS = 0x95,
155 BUSLOGICCOMMAND_SET_CCB_FORMAT = 0x96,
156 BUSLOGICCOMMAND_WRITE_INQUIRY_BUFFER = 0x9a,
157 BUSLOGICCOMMAND_READ_INQUIRY_BUFFER = 0x9b,
158 BUSLOGICCOMMAND_FLASH_ROM_UPLOAD_DOWNLOAD = 0xa7,
159 BUSLOGICCOMMAND_READ_SCAM_DATA = 0xa8,
160 BUSLOGICCOMMAND_WRITE_SCAM_DATA = 0xa9
161} BUSLOGICCOMMAND;
162
163#pragma pack(1)
164/**
165 * Auto SCSI structure which is located
166 * in host adapter RAM and contains several
167 * configuration parameters.
168 */
169typedef struct AutoSCSIRam
170{
171 uint8_t aInternalSignature[2];
172 uint8_t cbInformation;
173 uint8_t aHostAdaptertype[6];
174 uint8_t uReserved1;
175 bool fFloppyEnabled: 1;
176 bool fFloppySecondary: 1;
177 bool fLevelSensitiveInterrupt: 1;
178 unsigned char uReserved2: 2;
179 unsigned char uSystemRAMAreForBIOS: 3;
180 unsigned char uDMAChannel: 7;
181 bool fDMAAutoConfiguration: 1;
182 unsigned char uIrqChannel: 7;
183 bool fIrqAutoConfiguration: 1;
184 uint8_t uDMATransferRate;
185 uint8_t uSCSIId;
186 bool fLowByteTerminated: 1;
187 bool fParityCheckingEnabled: 1;
188 bool fHighByteTerminated: 1;
189 bool fNoisyCablingEnvironment: 1;
190 bool fFastSynchronousNeogtiation: 1;
191 bool fBusResetEnabled: 1;
192 bool fReserved3: 1;
193 bool fActiveNegotiationEnabled: 1;
194 uint8_t uBusOnDelay;
195 uint8_t uBusOffDelay;
196 bool fHostAdapterBIOSEnabled: 1;
197 bool fBIOSRedirectionOfInt19: 1;
198 bool fExtendedTranslation: 1;
199 bool fMapRemovableAsFixed: 1;
200 bool fReserved4: 1;
201 bool fBIOSSupportsMoreThan2Drives: 1;
202 bool fBIOSInterruptMode: 1;
203 bool fFlopticalSupport: 1;
204 uint16_t u16DeviceEnabledMask;
205 uint16_t u16WidePermittedMask;
206 uint16_t u16FastPermittedMask;
207 uint16_t u16SynchronousPermittedMask;
208 uint16_t u16DisconnectPermittedMask;
209 uint16_t u16SendStartUnitCommandMask;
210 uint16_t u16IgnoreInBIOSScanMask;
211 unsigned char uPCIInterruptPin: 2;
212 unsigned char uHostAdapterIoPortAddress: 2;
213 bool fStrictRoundRobinMode: 1;
214 bool fVesaBusSpeedGreaterThan33MHz: 1;
215 bool fVesaBurstWrite: 1;
216 bool fVesaBurstRead: 1;
217 uint16_t u16UltraPermittedMask;
218 uint32_t uReserved5;
219 uint8_t uReserved6;
220 uint8_t uAutoSCSIMaximumLUN;
221 bool fReserved7: 1;
222 bool fSCAMDominant: 1;
223 bool fSCAMenabled: 1;
224 bool fSCAMLevel2: 1;
225 unsigned char uReserved8: 4;
226 bool fInt13Extension: 1;
227 bool fReserved9: 1;
228 bool fCDROMBoot: 1;
229 unsigned char uReserved10: 5;
230 unsigned char uBootTargetId: 4;
231 unsigned char uBootChannel: 4;
232 bool fForceBusDeviceScanningOrder: 1;
233 unsigned char uReserved11: 7;
234 uint16_t u16NonTaggedToAlternateLunPermittedMask;
235 uint16_t u16RenegotiateSyncAfterCheckConditionMask;
236 uint8_t aReserved12[10];
237 uint8_t aManufacturingDiagnostic[2];
238 uint16_t u16Checksum;
239} AutoSCSIRam, *PAutoSCSIRam;
240AssertCompileSize(AutoSCSIRam, 64);
241#pragma pack()
242
243#pragma pack(1)
244/**
245 * The local Ram.
246 */
247typedef union HostAdapterLocalRam
248{
249 /* Byte view. */
250 uint8_t u8View[256];
251 /* Structured view. */
252 struct
253 {
254 /** Offset 0 - 63 is for BIOS. */
255 uint8_t u8Bios[64];
256 /** Auto SCSI structure. */
257 AutoSCSIRam autoSCSIData;
258 } structured;
259} HostAdapterLocalRam, *PHostAdapterLocalRam;
260AssertCompileSize(HostAdapterLocalRam, 256);
261#pragma pack()
262
263/**
264 * Main BusLogic device state.
265 *
266 * @extends PCIDEVICE
267 * @implements PDMILEDPORTS
268 */
269typedef struct BUSLOGIC
270{
271 /** The PCI device structure. */
272 PCIDEVICE dev;
273 /** Pointer to the device instance - HC ptr */
274 PPDMDEVINSR3 pDevInsR3;
275 /** Pointer to the device instance - R0 ptr */
276 PPDMDEVINSR0 pDevInsR0;
277 /** Pointer to the device instance - RC ptr. */
278 PPDMDEVINSRC pDevInsRC;
279
280 /* Whether R0 is enabled. */
281 bool fR0Enabled;
282 /** Whether GC is enabled. */
283 bool fGCEnabled;
284
285 /** Base address of the I/O ports. */
286 RTIOPORT IOPortBase;
287 /** Base address of the memory mapping. */
288 RTGCPHYS MMIOBase;
289 /** Status register - Readonly. */
290 volatile uint8_t regStatus;
291 /** Interrupt register - Readonly. */
292 volatile uint8_t regInterrupt;
293 /** Geometry register - Readonly. */
294 volatile uint8_t regGeometry;
295
296 /** Local RAM for the fetch hostadapter local RAM request.
297 * I don't know how big the buffer really is but the maximum
298 * seems to be 256 bytes because the offset and count field in the command request
299 * are only one byte big.
300 */
301 HostAdapterLocalRam LocalRam;
302
303 /** Command code the guest issued. */
304 uint8_t uOperationCode;
305 /** Buffer for the command parameters the adapter is currently receiving from the guest.
306 * Size of the largest command which is possible.
307 */
308 uint8_t aCommandBuffer[BUSLOGIC_COMMAND_SIZE_MAX]; /* Size of the biggest request. */
309 /** Current position in the command buffer. */
310 uint8_t iParameter;
311 /** Parameters left until the command is complete. */
312 uint8_t cbCommandParametersLeft;
313
314 /** Whether we are using the RAM or reply buffer. */
315 bool fUseLocalRam;
316 /** Buffer to store reply data from the controller to the guest. */
317 uint8_t aReplyBuffer[BUSLOGIC_REPLY_SIZE_MAX]; /* Size of the biggest reply. */
318 /** Position in the buffer we are reading next. */
319 uint8_t iReply;
320 /** Bytes left until the reply buffer is empty. */
321 uint8_t cbReplyParametersLeft;
322
323 /** Flag whether IRQs are enabled. */
324 bool fIRQEnabled;
325 /** Flag whether the ISA I/O port range is disabled
326 * to prevent the BIOs to access the device. */
327 bool fISAEnabled;
328
329 /** Number of mailboxes the guest set up. */
330 uint32_t cMailbox;
331
332#if HC_ARCH_BITS == 64
333 uint32_t Alignment0;
334#endif
335
336 /** Physical base address of the outgoing mailboxes. */
337 RTGCPHYS GCPhysAddrMailboxOutgoingBase;
338 /** Current outgoing mailbox position. */
339 uint32_t uMailboxOutgoingPositionCurrent;
340 /** Number of mailboxes ready. */
341 volatile uint32_t cMailboxesReady;
342 /** Whether a notification to R3 was send. */
343 volatile bool fNotificationSend;
344
345#if HC_ARCH_BITS == 64
346 uint32_t Alignment1;
347#endif
348
349 /** Physical base address of the incoming mailboxes. */
350 RTGCPHYS GCPhysAddrMailboxIncomingBase;
351 /** Current incoming mailbox position. */
352 uint32_t uMailboxIncomingPositionCurrent;
353
354 /** Whether strict round robin is enabled. */
355 bool fStrictRoundRobinMode;
356 /** Whether the extended LUN CCB format is enabled for 32 possible logical units. */
357 bool fExtendedLunCCBFormat;
358
359 /** Queue to send tasks to R3. - HC ptr */
360 R3PTRTYPE(PPDMQUEUE) pNotifierQueueR3;
361 /** Queue to send tasks to R3. - HC ptr */
362 R0PTRTYPE(PPDMQUEUE) pNotifierQueueR0;
363 /** Queue to send tasks to R3. - RC ptr */
364 RCPTRTYPE(PPDMQUEUE) pNotifierQueueRC;
365
366#if HC_ARCH_BITS == 64
367 uint32_t Alignment2;
368#endif
369
370 /** Cache for task states. */
371 R3PTRTYPE(RTMEMCACHE) hTaskCache;
372
373 /** Device state for BIOS access. */
374 VBOXSCSI VBoxSCSI;
375
376 /** BusLogic device states. */
377 BUSLOGICDEVICE aDeviceStates[BUSLOGIC_MAX_DEVICES];
378
379 /** The base interface.
380 * @todo use PDMDEVINS::IBase */
381 PDMIBASE IBase;
382 /** Status Port - Leds interface. */
383 PDMILEDPORTS ILeds;
384 /** Partner of ILeds. */
385 R3PTRTYPE(PPDMILEDCONNECTORS) pLedsConnector;
386} BUSLOGIC, *PBUSLOGIC;
387
388/** Register offsets in the I/O port space. */
389#define BUSLOGIC_REGISTER_CONTROL 0 /* Writeonly */
390/** Fields for the control register. */
391# define BUSLOGIC_REGISTER_CONTROL_SCSI_BUSRESET RT_BIT(4)
392# define BUSLOGIC_REGISTER_CONTROL_INTERRUPT_RESET RT_BIT(5)
393# define BUSLOGIC_REGISTER_CONTROL_SOFT_RESET RT_BIT(6)
394# define BUSLOGIC_REGISTER_CONTROL_HARD_RESET RT_BIT(7)
395
396#define BUSLOGIC_REGISTER_STATUS 0 /* Readonly */
397/** Fields for the status register. */
398# define BUSLOGIC_REGISTER_STATUS_COMMAND_INVALID RT_BIT(0)
399# define BUSLOGIC_REGISTER_STATUS_DATA_IN_REGISTER_READY RT_BIT(2)
400# define BUSLOGIC_REGISTER_STATUS_COMMAND_PARAMETER_REGISTER_BUSY RT_BIT(3)
401# define BUSLOGIC_REGISTER_STATUS_HOST_ADAPTER_READY RT_BIT(4)
402# define BUSLOGIC_REGISTER_STATUS_INITIALIZATION_REQUIRED RT_BIT(5)
403# define BUSLOGIC_REGISTER_STATUS_DIAGNOSTIC_FAILURE RT_BIT(6)
404# define BUSLOGIC_REGISTER_STATUS_DIAGNOSTIC_ACTIVE RT_BIT(7)
405
406#define BUSLOGIC_REGISTER_COMMAND 1 /* Writeonly */
407#define BUSLOGIC_REGISTER_DATAIN 1 /* Readonly */
408#define BUSLOGIC_REGISTER_INTERRUPT 2 /* Readonly */
409/** Fields for the interrupt register. */
410# define BUSLOGIC_REGISTER_INTERRUPT_INCOMING_MAILBOX_LOADED RT_BIT(0)
411# define BUSLOGIC_REGISTER_INTERRUPT_OUTCOMING_MAILBOX_AVAILABLE RT_BIT(1)
412# define BUSLOGIC_REGISTER_INTERRUPT_COMMAND_COMPLETE RT_BIT(2)
413# define BUSLOGIC_REGISTER_INTERRUPT_EXTERNAL_BUS_RESET RT_BIT(3)
414# define BUSLOGIC_REGISTER_INTERRUPT_INTERRUPT_VALID RT_BIT(7)
415
416#define BUSLOGIC_REGISTER_GEOMETRY 3 /* Readonly */
417# define BUSLOGIC_REGISTER_GEOMETRY_EXTENTED_TRANSLATION_ENABLED RT_BIT(7)
418
419/* Structure for the INQUIRE_PCI_HOST_ADAPTER_INFORMATION reply. */
420#pragma pack(1)
421typedef struct ReplyInquirePCIHostAdapterInformation
422{
423 uint8_t IsaIOPort;
424 uint8_t IRQ;
425 unsigned char LowByteTerminated:1;
426 unsigned char HighByteTerminated:1;
427 unsigned char uReserved:2; /* Reserved. */
428 unsigned char JP1:1; /* Whatever that means. */
429 unsigned char JP2:1; /* Whatever that means. */
430 unsigned char JP3:1; /* Whatever that means. */
431 /** Whether the provided info is valid. */
432 unsigned char InformationIsValid: 1;
433 uint8_t uReserved2; /* Reserved. */
434} ReplyInquirePCIHostAdapterInformation, *PReplyInquirePCIHostAdapterInformation;
435AssertCompileSize(ReplyInquirePCIHostAdapterInformation, 4);
436#pragma pack()
437
438/* Structure for the INQUIRE_CONFIGURATION reply. */
439#pragma pack(1)
440typedef struct ReplyInquireConfiguration
441{
442 unsigned char uReserved1: 5;
443 bool fDmaChannel5: 1;
444 bool fDmaChannel6: 1;
445 bool fDmaChannel7: 1;
446 bool fIrqChannel9: 1;
447 bool fIrqChannel10: 1;
448 bool fIrqChannel11: 1;
449 bool fIrqChannel12: 1;
450 unsigned char uReserved2: 1;
451 bool fIrqChannel14: 1;
452 bool fIrqChannel15: 1;
453 unsigned char uReserved3: 1;
454 unsigned char uHostAdapterId: 4;
455 unsigned char uReserved4: 4;
456} ReplyInquireConfiguration, *PReplyInquireConfiguration;
457AssertCompileSize(ReplyInquireConfiguration, 3);
458#pragma pack()
459
460/* Structure for the INQUIRE_SETUP_INFORMATION reply. */
461#pragma pack(1)
462typedef struct ReplyInquireSetupInformationSynchronousValue
463{
464 unsigned char uOffset: 4;
465 unsigned char uTransferPeriod: 3;
466 bool fSynchronous: 1;
467}ReplyInquireSetupInformationSynchronousValue, *PReplyInquireSetupInformationSynchronousValue;
468AssertCompileSize(ReplyInquireSetupInformationSynchronousValue, 1);
469#pragma pack()
470
471#pragma pack(1)
472typedef struct ReplyInquireSetupInformation
473{
474 bool fSynchronousInitiationEnabled: 1;
475 bool fParityCheckingEnabled: 1;
476 unsigned char uReserved1: 6;
477 uint8_t uBusTransferRate;
478 uint8_t uPreemptTimeOnBus;
479 uint8_t uTimeOffBus;
480 uint8_t cMailbox;
481 uint8_t MailboxAddress[3];
482 ReplyInquireSetupInformationSynchronousValue SynchronousValuesId0To7[8];
483 uint8_t uDisconnectPermittedId0To7;
484 uint8_t uSignature;
485 uint8_t uCharacterD;
486 uint8_t uHostBusType;
487 uint8_t uWideTransferPermittedId0To7;
488 uint8_t uWideTransfersActiveId0To7;
489 ReplyInquireSetupInformationSynchronousValue SynchronousValuesId8To15[8];
490 uint8_t uDisconnectPermittedId8To15;
491 uint8_t uReserved2;
492 uint8_t uWideTransferPermittedId8To15;
493 uint8_t uWideTransfersActiveId8To15;
494} ReplyInquireSetupInformation, *PReplyInquireSetupInformation;
495AssertCompileSize(ReplyInquireSetupInformation, 34);
496#pragma pack()
497
498/* Structure for the INQUIRE_EXTENDED_SETUP_INFORMATION. */
499#pragma pack(1)
500typedef struct ReplyInquireExtendedSetupInformation
501{
502 uint8_t uBusType;
503 uint8_t uBiosAddress;
504 uint16_t u16ScatterGatherLimit;
505 uint8_t cMailbox;
506 uint32_t uMailboxAddressBase;
507 unsigned char uReserved1: 2;
508 bool fFastEISA: 1;
509 unsigned char uReserved2: 3;
510 bool fLevelSensitiveInterrupt: 1;
511 unsigned char uReserved3: 1;
512 unsigned char aFirmwareRevision[3];
513 bool fHostWideSCSI: 1;
514 bool fHostDifferentialSCSI: 1;
515 bool fHostSupportsSCAM: 1;
516 bool fHostUltraSCSI: 1;
517 bool fHostSmartTermination: 1;
518 unsigned char uReserved4: 3;
519} ReplyInquireExtendedSetupInformation, *PReplyInquireExtendedSetupInformation;
520AssertCompileSize(ReplyInquireExtendedSetupInformation, 14);
521#pragma pack()
522
523/* Structure for the INITIALIZE EXTENDED MAILBOX request. */
524#pragma pack(1)
525typedef struct RequestInitializeExtendedMailbox
526{
527 /** Number of mailboxes in guest memory. */
528 uint8_t cMailbox;
529 /** Physical address of the first mailbox. */
530 uint32_t uMailboxBaseAddress;
531} RequestInitializeExtendedMailbox, *PRequestInitializeExtendedMailbox;
532AssertCompileSize(RequestInitializeExtendedMailbox, 5);
533#pragma pack()
534
535/*
536 * Structure of a mailbox in guest memory.
537 * The incoming and outgoing mailbox have the same size
538 * but the incoming one has some more fields defined which
539 * are marked as reserved in the outgoing one.
540 * The last field is also different from the type.
541 * For outgoing mailboxes it is the action and
542 * for incoming ones the completion status code for the task.
543 * We use one structure for both types.
544 */
545#pragma pack(1)
546typedef struct Mailbox
547{
548 /** Physical adress of the CCB structure in the guest memory. */
549 uint32_t u32PhysAddrCCB;
550 /** Type specific data. */
551 union
552 {
553 /** For outgoing mailboxes. */
554 struct
555 {
556 /** Reserved */
557 uint8_t uReserved[3];
558 /** Action code. */
559 uint8_t uActionCode;
560 } out;
561 /** For incoming mailboxes. */
562 struct
563 {
564 /** The host adapter status after finishing the request. */
565 uint8_t uHostAdapterStatus;
566 /** The status of the device which executed the request after executing it. */
567 uint8_t uTargetDeviceStatus;
568 /** Reserved. */
569 uint8_t uReserved;
570 /** The completion status code of the request. */
571 uint8_t uCompletionCode;
572 } in;
573 } u;
574} Mailbox, *PMailbox;
575AssertCompileSize(Mailbox, 8);
576#pragma pack()
577
578/*
579 * Action codes for outgoing mailboxes.
580 */
581enum BUSLOGIC_MAILBOX_OUTGOING_ACTION
582{
583 BUSLOGIC_MAILBOX_OUTGOING_ACTION_FREE = 0x00,
584 BUSLOGIC_MAILBOX_OUTGOING_ACTION_START_COMMAND = 0x01,
585 BUSLOGIC_MAILBOX_OUTGOING_ACTION_ABORT_COMMAND = 0x02
586};
587
588/*
589 * Completion codes for incoming mailboxes.
590 */
591enum BUSLOGIC_MAILBOX_INCOMING_COMPLETION
592{
593 BUSLOGIC_MAILBOX_INCOMING_COMPLETION_FREE = 0x00,
594 BUSLOGIC_MAILBOX_INCOMING_COMPLETION_WITHOUT_ERROR = 0x01,
595 BUSLOGIC_MAILBOX_INCOMING_COMPLETION_ABORTED = 0x02,
596 BUSLOGIC_MAILBOX_INCOMING_COMPLETION_ABORTED_NOT_FOUND = 0x03,
597 BUSLOGIC_MAILBOX_INCOMING_COMPLETION_WITH_ERROR = 0x04,
598 BUSLOGIC_MAILBOX_INCOMING_COMPLETION_INVALID_CCB = 0x05
599};
600
601/*
602 * Host adapter status for incoming mailboxes.
603 */
604enum BUSLOGIC_MAILBOX_INCOMING_ADAPTER_STATUS
605{
606 BUSLOGIC_MAILBOX_INCOMING_ADAPTER_STATUS_CMD_COMPLETED = 0x00,
607 BUSLOGIC_MAILBOX_INCOMING_ADAPTER_STATUS_LINKED_CMD_COMPLETED = 0x0a,
608 BUSLOGIC_MAILBOX_INCOMING_ADAPTER_STATUS_LINKED_CMD_COMPLETED_WITH_FLAG = 0x0b,
609 BUSLOGIC_MAILBOX_INCOMING_ADAPTER_STATUS_DATA_UNDERUN = 0x0c,
610 BUSLOGIC_MAILBOX_INCOMING_ADAPTER_STATUS_SCSI_SELECTION_TIMEOUT = 0x11,
611 BUSLOGIC_MAILBOX_INCOMING_ADAPTER_STATUS_DATA_OVERRUN = 0x12,
612 BUSLOGIC_MAILBOX_INCOMING_ADAPTER_STATUS_UNEXPECTED_BUS_FREE = 0x13,
613 BUSLOGIC_MAILBOX_INCOMING_ADAPTER_STATUS_INVALID_BUS_PHASE_REQUESTED = 0x14,
614 BUSLOGIC_MAILBOX_INCOMING_ADAPTER_STATUS_INVALID_OUTGOING_MAILBOX_ACTION_CODE = 0x15,
615 BUSLOGIC_MAILBOX_INCOMING_ADAPTER_STATUS_INVALID_COMMAND_OPERATION_CODE = 0x16,
616 BUSLOGIC_MAILBOX_INCOMING_ADAPTER_STATUS_LINKED_CCB_HAS_INVALID_LUN = 0x17,
617 BUSLOGIC_MAILBOX_INCOMING_ADAPTER_STATUS_INVALID_COMMAND_PARAMETER = 0x1a,
618 BUSLOGIC_MAILBOX_INCOMING_ADAPTER_STATUS_AUTO_REQUEST_SENSE_FAILED = 0x1b,
619 BUSLOGIC_MAILBOX_INCOMING_ADAPTER_STATUS_TAGGED_QUEUING_MESSAGE_REJECTED = 0x1c,
620 BUSLOGIC_MAILBOX_INCOMING_ADAPTER_STATUS_UNSUPPORTED_MESSAGE_RECEIVED = 0x1d,
621 BUSLOGIC_MAILBOX_INCOMING_ADAPTER_STATUS_HOST_ADAPTER_HARDWARE_FAILED = 0x20,
622 BUSLOGIC_MAILBOX_INCOMING_ADAPTER_STATUS_TARGET_FAILED_RESPONSE_TO_ATN = 0x21,
623 BUSLOGIC_MAILBOX_INCOMING_ADAPTER_STATUS_HOST_ADAPTER_ASSERTED_RST = 0x22,
624 BUSLOGIC_MAILBOX_INCOMING_ADAPTER_STATUS_OTHER_DEVICE_ASSERTED_RST = 0x23,
625 BUSLOGIC_MAILBOX_INCOMING_ADAPTER_STATUS_TARGET_DEVICE_RECONNECTED_IMPROPERLY = 0x24,
626 BUSLOGIC_MAILBOX_INCOMING_ADAPTER_STATUS_HOST_ADAPTER_ASSERTED_BUS_DEVICE_RESET = 0x25,
627 BUSLOGIC_MAILBOX_INCOMING_ADAPTER_STATUS_ABORT_QUEUE_GENERATED = 0x26,
628 BUSLOGIC_MAILBOX_INCOMING_ADAPTER_STATUS_HOST_ADAPTER_SOFTWARE_ERROR = 0x27,
629 BUSLOGIC_MAILBOX_INCOMING_ADAPTER_STATUS_HOST_ADAPTER_HARDWARE_TIMEOUT_ERROR = 0x30,
630 BUSLOGIC_MAILBOX_INCOMING_ADAPTER_STATUS_SCSI_PARITY_ERROR_DETECTED = 0x34
631};
632
633/*
634 * Device status codes for incoming mailboxes.
635 */
636enum BUSLOGIC_MAILBOX_INCOMING_DEVICE_STATUS
637{
638 BUSLOGIC_MAILBOX_INCOMING_DEVICE_STATUS_OPERATION_GOOD = 0x00,
639 BUSLOGIC_MAILBOX_INCOMING_DEVICE_STATUS_CHECK_CONDITION = 0x02,
640 BUSLOGIC_MAILBOX_INCOMING_DEVICE_STATUS_DEVICE_BUSY = 0x08
641};
642
643/*
644 * Opcode types for CCB.
645 */
646enum BUSLOGIC_CCB_OPCODE
647{
648 BUSLOGIC_CCB_OPCODE_INITIATOR_CCB = 0x00,
649 BUSLOGIC_CCB_OPCODE_TARGET_CCB = 0x01,
650 BUSLOGIC_CCB_OPCODE_INITIATOR_CCB_SCATTER_GATHER = 0x02,
651 BUSLOGIC_CCB_OPCODE_INITIATOR_CCB_RESIDUAL_DATA_LENGTH = 0x03,
652 BUSLOGIC_CCB_OPCODE_INITIATOR_CCB_RESIDUAL_SCATTER_GATHER = 0x04,
653 BUSLOGIC_CCB_OPCODE_BUS_DEVICE_RESET = 0x81
654};
655
656/*
657 * Data transfer direction.
658 */
659enum BUSLOGIC_CCB_DIRECTION
660{
661 BUSLOGIC_CCB_DIRECTION_UNKNOWN = 0x00,
662 BUSLOGIC_CCB_DIRECTION_IN = 0x01,
663 BUSLOGIC_CCB_DIRECTION_OUT = 0x02,
664 BUSLOGIC_CCB_DIRECTION_NO_DATA = 0x03
665};
666
667/*
668 * The command control block for a SCSI request.
669 */
670#pragma pack(1)
671typedef struct CommandControlBlock
672{
673 /** Opcode. */
674 uint8_t uOpcode;
675 /** Reserved */
676 unsigned char uReserved1: 3;
677 /** Data direction for the request. */
678 unsigned char uDataDirection: 2;
679 /** Whether the request is tag queued. */
680 bool fTagQueued: 1;
681 /** Queue tag mode. */
682 unsigned char uQueueTag: 2;
683 /** Length of the SCSI CDB. */
684 uint8_t cbCDB;
685 /** Sense data length. */
686 uint8_t cbSenseData;
687 /** Data length. */
688 uint32_t cbData;
689 /** Data pointer.
690 * This points to the data region or a scatter gather list based on the opcode.
691 */
692 uint32_t u32PhysAddrData;
693 /** Reserved. */
694 uint8_t uReserved2[2];
695 /** Host adapter status. */
696 uint8_t uHostAdapterStatus;
697 /** Device adapter status. */
698 uint8_t uDeviceStatus;
699 /** The device the request is send to. */
700 uint8_t uTargetId;
701 /**The LUN in the device. */
702 unsigned char uLogicalUnit: 5;
703 /** Legacy tag. */
704 bool fLegacyTagEnable: 1;
705 /** Legacy queue tag. */
706 unsigned char uLegacyQueueTag: 2;
707 /** The SCSI CDB. */
708 uint8_t aCDB[12]; /* A CDB can be 12 bytes long. */
709 /** Reserved. */
710 uint8_t uReserved3[6];
711 /** Sense data pointer. */
712 uint32_t u32PhysAddrSenseData;
713} CommandControlBlock, *PCommandControlBlock;
714AssertCompileSize(CommandControlBlock, 40);
715#pragma pack()
716
717#pragma pack(1)
718typedef struct ScatterGatherEntry
719{
720 uint32_t cbSegment;
721 uint32_t u32PhysAddrSegmentBase;
722} ScatterGatherEntry, *PScatterGatherEntry;
723AssertCompileSize(ScatterGatherEntry, 8);
724#pragma pack()
725
726/*
727 * Task state for a CCB request.
728 */
729typedef struct BUSLOGICTASKSTATE
730{
731 /** Device this task is assigned to. */
732 R3PTRTYPE(PBUSLOGICDEVICE) pTargetDeviceR3;
733 /** The command control block from the guest. */
734 CommandControlBlock CommandControlBlockGuest;
735 /** Mailbox read from guest memory. */
736 Mailbox MailboxGuest;
737 /** The SCSI request we pass to the underlying SCSI engine. */
738 PDMSCSIREQUEST PDMScsiRequest;
739 /** Data buffer segment */
740 RTSGSEG DataSeg;
741 /** Pointer to the R3 sense buffer. */
742 uint8_t *pbSenseBuffer;
743 /** Flag whether this is a request from the BIOS. */
744 bool fBIOS;
745} BUSLOGICTASKSTATE, *PBUSLOGICTASKSTATE;
746
747#ifndef VBOX_DEVICE_STRUCT_TESTCASE
748
749RT_C_DECLS_BEGIN
750PDMBOTHCBDECL(int) buslogicIOPortWrite (PPDMDEVINS pDevIns, void *pvUser,
751 RTIOPORT Port, uint32_t u32, unsigned cb);
752PDMBOTHCBDECL(int) buslogicIOPortRead (PPDMDEVINS pDevIns, void *pvUser,
753 RTIOPORT Port, uint32_t *pu32, unsigned cb);
754PDMBOTHCBDECL(int) buslogicMMIOWrite(PPDMDEVINS pDevIns, void *pvUser,
755 RTGCPHYS GCPhysAddr, void *pv, unsigned cb);
756PDMBOTHCBDECL(int) buslogicMMIORead(PPDMDEVINS pDevIns, void *pvUser,
757 RTGCPHYS GCPhysAddr, void *pv, unsigned cb);
758RT_C_DECLS_END
759
760#define PDMIBASE_2_PBUSLOGICDEVICE(pInterface) ( (PBUSLOGICDEVICE)((uintptr_t)(pInterface) - RT_OFFSETOF(BUSLOGICDEVICE, IBase)) )
761#define PDMISCSIPORT_2_PBUSLOGICDEVICE(pInterface) ( (PBUSLOGICDEVICE)((uintptr_t)(pInterface) - RT_OFFSETOF(BUSLOGICDEVICE, ISCSIPort)) )
762#define PDMILEDPORTS_2_PBUSLOGICDEVICE(pInterface) ( (PBUSLOGICDEVICE)((uintptr_t)(pInterface) - RT_OFFSETOF(BUSLOGICDEVICE, ILed)) )
763#define PDMIBASE_2_PBUSLOGIC(pInterface) ( (PBUSLOGIC)((uintptr_t)(pInterface) - RT_OFFSETOF(BUSLOGIC, IBase)) )
764#define PDMILEDPORTS_2_PBUSLOGIC(pInterface) ( (PBUSLOGIC)((uintptr_t)(pInterface) - RT_OFFSETOF(BUSLOGIC, ILeds)) )
765
766/**
767 * Deasserts the interrupt line of the BusLogic adapter.
768 *
769 * @returns nothing
770 * @param pBuslogic Pointer to the BusLogic device instance.
771 */
772static void buslogicClearInterrupt(PBUSLOGIC pBusLogic)
773{
774 LogFlowFunc(("pBusLogic=%#p\n", pBusLogic));
775 pBusLogic->regInterrupt = 0;
776 PDMDevHlpPCISetIrqNoWait(pBusLogic->CTX_SUFF(pDevIns), 0, 0);
777}
778
779/**
780 * Assert IRQ line of the BusLogic adapter.
781 *
782 * @returns nothing.
783 * @param pBusLogic Pointer to the BusLogic device instance.
784 */
785static void buslogicSetInterrupt(PBUSLOGIC pBusLogic)
786{
787 LogFlowFunc(("pBusLogic=%#p\n", pBusLogic));
788 pBusLogic->regInterrupt |= BUSLOGIC_REGISTER_INTERRUPT_INTERRUPT_VALID;
789 PDMDevHlpPCISetIrqNoWait(pBusLogic->CTX_SUFF(pDevIns), 0, 1);
790}
791
792#if defined(IN_RING3)
793/**
794 * Initialize local RAM of host adapter with default values.
795 *
796 * @returns nothing.
797 * @param pBusLogic.
798 */
799static void buslogicInitializeLocalRam(PBUSLOGIC pBusLogic)
800{
801 /*
802 * These values are mostly from what I think is right
803 * looking at the dmesg output from a Linux guest inside
804 * a VMware server VM.
805 *
806 * So they don't have to be right :)
807 */
808 memset(pBusLogic->LocalRam.u8View, 0, sizeof(HostAdapterLocalRam));
809 pBusLogic->LocalRam.structured.autoSCSIData.fLevelSensitiveInterrupt = true;
810 pBusLogic->LocalRam.structured.autoSCSIData.fParityCheckingEnabled = true;
811 pBusLogic->LocalRam.structured.autoSCSIData.fExtendedTranslation = true; /* Same as in geometry register. */
812 pBusLogic->LocalRam.structured.autoSCSIData.u16DeviceEnabledMask = ~0; /* All enabled. Maybe mask out non present devices? */
813 pBusLogic->LocalRam.structured.autoSCSIData.u16WidePermittedMask = ~0;
814 pBusLogic->LocalRam.structured.autoSCSIData.u16FastPermittedMask = ~0;
815 pBusLogic->LocalRam.structured.autoSCSIData.u16SynchronousPermittedMask = ~0;
816 pBusLogic->LocalRam.structured.autoSCSIData.u16DisconnectPermittedMask = ~0;
817 pBusLogic->LocalRam.structured.autoSCSIData.fStrictRoundRobinMode = pBusLogic->fStrictRoundRobinMode;
818 pBusLogic->LocalRam.structured.autoSCSIData.u16UltraPermittedMask = ~0;
819 /* @todo calculate checksum? */
820}
821
822/**
823 * Do a hardware reset of the buslogic adapter.
824 *
825 * @returns VBox status code.
826 * @param pBusLogic Pointer to the BusLogic device instance.
827 */
828static int buslogicHwReset(PBUSLOGIC pBusLogic)
829{
830 LogFlowFunc(("pBusLogic=%#p\n", pBusLogic));
831
832 /* Reset registers to default value. */
833 pBusLogic->regStatus = BUSLOGIC_REGISTER_STATUS_HOST_ADAPTER_READY;
834 pBusLogic->regInterrupt = 0;
835 pBusLogic->regGeometry = BUSLOGIC_REGISTER_GEOMETRY_EXTENTED_TRANSLATION_ENABLED;
836 pBusLogic->uOperationCode = 0xff; /* No command executing. */
837 pBusLogic->iParameter = 0;
838 pBusLogic->cbCommandParametersLeft = 0;
839 pBusLogic->fIRQEnabled = true;
840 pBusLogic->fISAEnabled = true;
841 pBusLogic->uMailboxOutgoingPositionCurrent = 0;
842 pBusLogic->uMailboxIncomingPositionCurrent = 0;
843
844 buslogicInitializeLocalRam(pBusLogic);
845 vboxscsiInitialize(&pBusLogic->VBoxSCSI);
846
847 return VINF_SUCCESS;
848}
849#endif
850
851/**
852 * Resets the command state machine for the next command and notifies the guest.
853 *
854 * @returns nothing.
855 * @param pBusLogic Pointer to the BusLogic device instance
856 */
857static void buslogicCommandComplete(PBUSLOGIC pBusLogic)
858{
859 LogFlowFunc(("pBusLogic=%#p\n", pBusLogic));
860
861 pBusLogic->fUseLocalRam = false;
862 pBusLogic->regStatus |= BUSLOGIC_REGISTER_STATUS_HOST_ADAPTER_READY;
863 pBusLogic->iReply = 0;
864
865 /* Modify I/O address does not generate an interrupt. */
866 if ( (pBusLogic->uOperationCode != BUSLOGICCOMMAND_MODIFY_IO_ADDRESS)
867 && (pBusLogic->uOperationCode != BUSLOGICCOMMAND_EXECUTE_MAILBOX_COMMAND))
868 {
869 /* Notify that the command is complete. */
870 pBusLogic->regStatus &= ~BUSLOGIC_REGISTER_STATUS_DATA_IN_REGISTER_READY;
871 pBusLogic->regInterrupt |= BUSLOGIC_REGISTER_INTERRUPT_COMMAND_COMPLETE;
872
873 if (pBusLogic->fIRQEnabled)
874 buslogicSetInterrupt(pBusLogic);
875 }
876
877 pBusLogic->uOperationCode = 0xff;
878 pBusLogic->iParameter = 0;
879}
880
881#if defined(IN_RING3)
882/**
883 * Initiates a hard reset which was issued from the guest.
884 *
885 * @returns nothing
886 * @param pBusLogic Pointer to the BusLogic device instance.
887 */
888static void buslogicIntiateHardReset(PBUSLOGIC pBusLogic)
889{
890 LogFlowFunc(("pBusLogic=%#p\n", pBusLogic));
891
892 buslogicHwReset(pBusLogic);
893
894 /* We set the diagnostic active in the status register. */
895 pBusLogic->regStatus |= BUSLOGIC_REGISTER_STATUS_DIAGNOSTIC_ACTIVE;
896}
897
898/**
899 * Send a mailbox with set status codes to the guest.
900 *
901 * @returns nothing.
902 * @param pBusLogicR Pointer to the BubsLogic device instance.
903 * @param pTaskState Pointer to the task state with the mailbox to send.
904 * @param uHostAdapterStatus The host adapter status code to set.
905 * @param uDeviceStatus The target device status to set.
906 * @param uMailboxCompletionCode Completion status code to set in the mailbox.
907 */
908static void buslogicSendIncomingMailbox(PBUSLOGIC pBusLogic, PBUSLOGICTASKSTATE pTaskState,
909 uint8_t uHostAdapterStatus, uint8_t uDeviceStatus,
910 uint8_t uMailboxCompletionCode)
911{
912 pTaskState->MailboxGuest.u.in.uHostAdapterStatus = uHostAdapterStatus;
913 pTaskState->MailboxGuest.u.in.uTargetDeviceStatus = uDeviceStatus;
914 pTaskState->MailboxGuest.u.in.uCompletionCode = uMailboxCompletionCode;
915
916 RTGCPHYS GCPhysAddrMailboxIncoming = pBusLogic->GCPhysAddrMailboxIncomingBase + (pBusLogic->uMailboxIncomingPositionCurrent * sizeof(Mailbox));
917 RTGCPHYS GCPhysAddrCCB = (RTGCPHYS)pTaskState->MailboxGuest.u32PhysAddrCCB;
918
919 /* Update CCB. */
920 pTaskState->CommandControlBlockGuest.uHostAdapterStatus = uHostAdapterStatus;
921 pTaskState->CommandControlBlockGuest.uDeviceStatus = uDeviceStatus;
922 PDMDevHlpPhysWrite(pBusLogic->CTX_SUFF(pDevIns), GCPhysAddrCCB, &pTaskState->CommandControlBlockGuest, sizeof(CommandControlBlock));
923
924 /* Update mailbox. */
925 PDMDevHlpPhysWrite(pBusLogic->CTX_SUFF(pDevIns), GCPhysAddrMailboxIncoming, &pTaskState->MailboxGuest, sizeof(Mailbox));
926
927 /* Advance to next mailbox position. */
928 pBusLogic->uMailboxIncomingPositionCurrent++;
929 if (pBusLogic->uMailboxIncomingPositionCurrent >= pBusLogic->cMailbox)
930 pBusLogic->uMailboxIncomingPositionCurrent = 0;
931
932 pBusLogic->regInterrupt |= BUSLOGIC_REGISTER_INTERRUPT_INCOMING_MAILBOX_LOADED;
933 if (pBusLogic->fIRQEnabled)
934 buslogicSetInterrupt(pBusLogic);
935}
936
937#if defined(DEBUG)
938/**
939 * Dumps the content of a mailbox for debugging purposes.
940 *
941 * @return nothing
942 * @param pMailbox The mialbox to dump.
943 * @param fOutgoing true if dumping the outgoing state.
944 * false if dumping the incoming state.
945 */
946static void buslogicDumpMailboxInfo(PMailbox pMailbox, bool fOutgoing)
947{
948 Log(("%s: Dump for %s mailbox:\n", __FUNCTION__, fOutgoing ? "outgoing" : "incoming"));
949 Log(("%s: u32PhysAddrCCB=%#x\n", __FUNCTION__, pMailbox->u32PhysAddrCCB));
950 if (fOutgoing)
951 {
952 Log(("%s: uActionCode=%u\n", __FUNCTION__, pMailbox->u.out.uActionCode));
953 }
954 else
955 {
956 Log(("%s: uHostAdapterStatus=%u\n", __FUNCTION__, pMailbox->u.in.uHostAdapterStatus));
957 Log(("%s: uTargetDeviceStatus=%u\n", __FUNCTION__, pMailbox->u.in.uTargetDeviceStatus));
958 Log(("%s: uCompletionCode=%u\n", __FUNCTION__, pMailbox->u.in.uCompletionCode));
959 }
960}
961
962/**
963 * Dumps the content of a command control block for debugging purposes.
964 *
965 * @returns nothing.
966 * @param pCCB Pointer to the command control block to dump.
967 */
968static void buslogicDumpCCBInfo(PCommandControlBlock pCCB)
969{
970 Log(("%s: Dump for Command Control Block:\n", __FUNCTION__));
971 Log(("%s: uOpCode=%#x\n", __FUNCTION__, pCCB->uOpcode));
972 Log(("%s: uDataDirection=%u\n", __FUNCTION__, pCCB->uDataDirection));
973 Log(("%s: fTagQueued=%d\n", __FUNCTION__, pCCB->fTagQueued));
974 Log(("%s: uQueueTag=%u\n", __FUNCTION__, pCCB->uQueueTag));
975 Log(("%s: cbCDB=%u\n", __FUNCTION__, pCCB->cbCDB));
976 Log(("%s: cbSenseData=%u\n", __FUNCTION__, pCCB->cbSenseData));
977 Log(("%s: cbData=%u\n", __FUNCTION__, pCCB->cbData));
978 Log(("%s: u32PhysAddrData=%#x\n", __FUNCTION__, pCCB->u32PhysAddrData));
979 Log(("%s: uHostAdapterStatus=%u\n", __FUNCTION__, pCCB->uHostAdapterStatus));
980 Log(("%s: uDeviceStatus=%u\n", __FUNCTION__, pCCB->uDeviceStatus));
981 Log(("%s: uTargetId=%u\n", __FUNCTION__, pCCB->uTargetId));
982 Log(("%s: uLogicalUnit=%u\n", __FUNCTION__, pCCB->uLogicalUnit));
983 Log(("%s: fLegacyTagEnable=%u\n", __FUNCTION__, pCCB->fLegacyTagEnable));
984 Log(("%s: uLegacyQueueTag=%u\n", __FUNCTION__, pCCB->uLegacyQueueTag));
985 Log(("%s: uCDB[0]=%#x\n", __FUNCTION__, pCCB->aCDB[0]));
986 for (int i = 1; i < pCCB->cbCDB; i++)
987 Log(("%s: uCDB[%d]=%u\n", __FUNCTION__, i, pCCB->aCDB[i]));
988 Log(("%s: u32PhysAddrSenseData=%#x\n", __FUNCTION__, pCCB->u32PhysAddrSenseData));
989}
990#endif
991
992/**
993 * Allocate data buffer.
994 *
995 * @returns VBox status code.
996 * @param pTaskState Pointer to the task state.
997 */
998static int buslogicDataBufferAlloc(PBUSLOGICTASKSTATE pTaskState)
999{
1000 PPDMDEVINS pDevIns = pTaskState->CTX_SUFF(pTargetDevice)->CTX_SUFF(pBusLogic)->CTX_SUFF(pDevIns);
1001
1002 if ( (pTaskState->CommandControlBlockGuest.uDataDirection != BUSLOGIC_CCB_DIRECTION_NO_DATA)
1003 && (pTaskState->CommandControlBlockGuest.cbData > 0))
1004 {
1005 /*
1006 * @todo: Check following assumption and what residual means.
1007 *
1008 * The BusLogic adapter can handle two different data buffer formats.
1009 * The first one is that the data pointer entry in the CCB points to
1010 * the buffer directly. In second mode the data pointer points to a
1011 * scatter gather list which describes the buffer.
1012 */
1013 if ( (pTaskState->CommandControlBlockGuest.uOpcode == BUSLOGIC_CCB_OPCODE_INITIATOR_CCB_SCATTER_GATHER)
1014 || (pTaskState->CommandControlBlockGuest.uOpcode == BUSLOGIC_CCB_OPCODE_INITIATOR_CCB_RESIDUAL_SCATTER_GATHER))
1015 {
1016 uint32_t cScatterGatherGCRead;
1017 uint32_t iScatterGatherEntry;
1018 ScatterGatherEntry aScatterGatherReadGC[32]; /* Number of scatter gather list entries read from guest memory. */
1019 uint32_t cScatterGatherGCLeft = pTaskState->CommandControlBlockGuest.cbData / sizeof(ScatterGatherEntry);
1020 RTGCPHYS GCPhysAddrScatterGatherCurrent = (RTGCPHYS)pTaskState->CommandControlBlockGuest.u32PhysAddrData;
1021 size_t cbDataToTransfer = 0;
1022
1023 /* Count number of bytes to transfer. */
1024 do
1025 {
1026 cScatterGatherGCRead = (cScatterGatherGCLeft < RT_ELEMENTS(aScatterGatherReadGC))
1027 ? cScatterGatherGCLeft
1028 : RT_ELEMENTS(aScatterGatherReadGC);
1029 cScatterGatherGCLeft -= cScatterGatherGCRead;
1030
1031 /* Read the SG entries. */
1032 PDMDevHlpPhysRead(pDevIns, GCPhysAddrScatterGatherCurrent, &aScatterGatherReadGC[0],
1033 cScatterGatherGCRead * sizeof(ScatterGatherEntry));
1034
1035 for (iScatterGatherEntry = 0; iScatterGatherEntry < cScatterGatherGCRead; iScatterGatherEntry++)
1036 {
1037 RTGCPHYS GCPhysAddrDataBase;
1038
1039 Log(("%s: iScatterGatherEntry=%u\n", __FUNCTION__, iScatterGatherEntry));
1040
1041 GCPhysAddrDataBase = (RTGCPHYS)aScatterGatherReadGC[iScatterGatherEntry].u32PhysAddrSegmentBase;
1042 cbDataToTransfer += aScatterGatherReadGC[iScatterGatherEntry].cbSegment;
1043
1044 Log(("%s: GCPhysAddrDataBase=%RGp cbDataToTransfer=%u\n",
1045 __FUNCTION__, GCPhysAddrDataBase,
1046 aScatterGatherReadGC[iScatterGatherEntry].cbSegment));
1047 }
1048
1049 /* Set address to the next entries to read. */
1050 GCPhysAddrScatterGatherCurrent += cScatterGatherGCRead * sizeof(ScatterGatherEntry);
1051 } while (cScatterGatherGCLeft > 0);
1052
1053 Log(("%s: cbDataToTransfer=%d\n", __FUNCTION__, cbDataToTransfer));
1054
1055 /* Allocate buffer */
1056 pTaskState->DataSeg.cbSeg = cbDataToTransfer;
1057 pTaskState->DataSeg.pvSeg = RTMemAlloc(pTaskState->DataSeg.cbSeg);
1058 if (!pTaskState->DataSeg.pvSeg)
1059 return VERR_NO_MEMORY;
1060
1061 /* Copy the data if needed */
1062 if (pTaskState->CommandControlBlockGuest.uDataDirection == BUSLOGIC_CCB_DIRECTION_OUT)
1063 {
1064 cScatterGatherGCLeft = pTaskState->CommandControlBlockGuest.cbData / sizeof(ScatterGatherEntry);
1065 GCPhysAddrScatterGatherCurrent = (RTGCPHYS)pTaskState->CommandControlBlockGuest.u32PhysAddrData;
1066 uint8_t *pbData = (uint8_t *)pTaskState->DataSeg.pvSeg;
1067
1068 do
1069 {
1070 cScatterGatherGCRead = (cScatterGatherGCLeft < RT_ELEMENTS(aScatterGatherReadGC))
1071 ? cScatterGatherGCLeft
1072 : RT_ELEMENTS(aScatterGatherReadGC);
1073 cScatterGatherGCLeft -= cScatterGatherGCRead;
1074
1075 /* Read the SG entries. */
1076 PDMDevHlpPhysRead(pDevIns, GCPhysAddrScatterGatherCurrent, &aScatterGatherReadGC[0],
1077 cScatterGatherGCRead * sizeof(ScatterGatherEntry));
1078
1079 for (iScatterGatherEntry = 0; iScatterGatherEntry < cScatterGatherGCRead; iScatterGatherEntry++)
1080 {
1081 RTGCPHYS GCPhysAddrDataBase;
1082
1083 Log(("%s: iScatterGatherEntry=%u\n", __FUNCTION__, iScatterGatherEntry));
1084
1085 GCPhysAddrDataBase = (RTGCPHYS)aScatterGatherReadGC[iScatterGatherEntry].u32PhysAddrSegmentBase;
1086 cbDataToTransfer = aScatterGatherReadGC[iScatterGatherEntry].cbSegment;
1087
1088 Log(("%s: GCPhysAddrDataBase=%RGp cbDataToTransfer=%u\n", __FUNCTION__, GCPhysAddrDataBase, cbDataToTransfer));
1089
1090 PDMDevHlpPhysRead(pDevIns, GCPhysAddrDataBase, pbData, cbDataToTransfer);
1091 pbData += cbDataToTransfer;
1092 }
1093
1094 /* Set address to the next entries to read. */
1095 GCPhysAddrScatterGatherCurrent += cScatterGatherGCRead * sizeof(ScatterGatherEntry);
1096 } while (cScatterGatherGCLeft > 0);
1097 }
1098
1099 }
1100 else if ( pTaskState->CommandControlBlockGuest.uOpcode == BUSLOGIC_CCB_OPCODE_INITIATOR_CCB
1101 || pTaskState->CommandControlBlockGuest.uOpcode == BUSLOGIC_CCB_OPCODE_INITIATOR_CCB_RESIDUAL_DATA_LENGTH)
1102 {
1103 /* The buffer is not scattered. */
1104 RTGCPHYS GCPhysAddrDataBase = (RTGCPHYS)pTaskState->CommandControlBlockGuest.u32PhysAddrData;
1105
1106 AssertMsg(GCPhysAddrDataBase != 0, ("Physical address is 0\n"));
1107
1108 pTaskState->DataSeg.cbSeg = pTaskState->CommandControlBlockGuest.cbData;
1109 pTaskState->DataSeg.pvSeg = RTMemAlloc(pTaskState->DataSeg.cbSeg);
1110 if (!pTaskState->DataSeg.pvSeg)
1111 return VERR_NO_MEMORY;
1112
1113 Log(("Non scattered buffer:\n"));
1114 Log(("u32PhysAddrData=%#x\n", pTaskState->CommandControlBlockGuest.u32PhysAddrData));
1115 Log(("cbData=%u\n", pTaskState->CommandControlBlockGuest.cbData));
1116 Log(("GCPhysAddrDataBase=0x%RGp\n", GCPhysAddrDataBase));
1117
1118 /* Copy the data into the buffer. */
1119 PDMDevHlpPhysRead(pDevIns, GCPhysAddrDataBase, pTaskState->DataSeg.pvSeg, pTaskState->DataSeg.cbSeg);
1120 }
1121 }
1122
1123 return VINF_SUCCESS;
1124}
1125
1126/**
1127 * Free allocated resources used for the scatter gather list.
1128 *
1129 * @returns nothing.
1130 * @param pTaskState Pointer to the task state.
1131 */
1132static void buslogicDataBufferFree(PBUSLOGICTASKSTATE pTaskState)
1133{
1134 PPDMDEVINS pDevIns = pTaskState->CTX_SUFF(pTargetDevice)->CTX_SUFF(pBusLogic)->CTX_SUFF(pDevIns);
1135
1136 if ( (pTaskState->CommandControlBlockGuest.cbData > 0)
1137 && ( (pTaskState->CommandControlBlockGuest.uDataDirection == BUSLOGIC_CCB_DIRECTION_IN)
1138 || (pTaskState->CommandControlBlockGuest.uDataDirection == BUSLOGIC_CCB_DIRECTION_UNKNOWN)))
1139 {
1140 if ( (pTaskState->CommandControlBlockGuest.uOpcode == BUSLOGIC_CCB_OPCODE_INITIATOR_CCB_SCATTER_GATHER)
1141 || (pTaskState->CommandControlBlockGuest.uOpcode == BUSLOGIC_CCB_OPCODE_INITIATOR_CCB_RESIDUAL_SCATTER_GATHER))
1142 {
1143 uint32_t cScatterGatherGCRead;
1144 uint32_t iScatterGatherEntry;
1145 ScatterGatherEntry aScatterGatherReadGC[32]; /* Number of scatter gather list entries read from guest memory. */
1146 uint32_t cScatterGatherGCLeft = pTaskState->CommandControlBlockGuest.cbData / sizeof(ScatterGatherEntry);
1147 RTGCPHYS GCPhysAddrScatterGatherCurrent = (RTGCPHYS)pTaskState->CommandControlBlockGuest.u32PhysAddrData;
1148 uint8_t *pbData = (uint8_t *)pTaskState->DataSeg.pvSeg;
1149
1150 do
1151 {
1152 cScatterGatherGCRead = (cScatterGatherGCLeft < RT_ELEMENTS(aScatterGatherReadGC))
1153 ? cScatterGatherGCLeft
1154 : RT_ELEMENTS(aScatterGatherReadGC);
1155 cScatterGatherGCLeft -= cScatterGatherGCRead;
1156
1157 /* Read the SG entries. */
1158 PDMDevHlpPhysRead(pDevIns, GCPhysAddrScatterGatherCurrent, &aScatterGatherReadGC[0],
1159 cScatterGatherGCRead * sizeof(ScatterGatherEntry));
1160
1161 for (iScatterGatherEntry = 0; iScatterGatherEntry < cScatterGatherGCRead; iScatterGatherEntry++)
1162 {
1163 RTGCPHYS GCPhysAddrDataBase;
1164 size_t cbDataToTransfer;
1165
1166 Log(("%s: iScatterGatherEntry=%u\n", __FUNCTION__, iScatterGatherEntry));
1167
1168 GCPhysAddrDataBase = (RTGCPHYS)aScatterGatherReadGC[iScatterGatherEntry].u32PhysAddrSegmentBase;
1169 cbDataToTransfer = aScatterGatherReadGC[iScatterGatherEntry].cbSegment;
1170
1171 Log(("%s: GCPhysAddrDataBase=%RGp cbDataToTransfer=%u\n", __FUNCTION__, GCPhysAddrDataBase, cbDataToTransfer));
1172
1173 PDMDevHlpPhysWrite(pDevIns, GCPhysAddrDataBase, pbData, cbDataToTransfer);
1174 pbData += cbDataToTransfer;
1175 }
1176
1177 /* Set address to the next entries to read. */
1178 GCPhysAddrScatterGatherCurrent += cScatterGatherGCRead * sizeof(ScatterGatherEntry);
1179 } while (cScatterGatherGCLeft > 0);
1180
1181 }
1182 else if ( pTaskState->CommandControlBlockGuest.uOpcode == BUSLOGIC_CCB_OPCODE_INITIATOR_CCB
1183 || pTaskState->CommandControlBlockGuest.uOpcode == BUSLOGIC_CCB_OPCODE_INITIATOR_CCB_RESIDUAL_DATA_LENGTH)
1184 {
1185 /* The buffer is not scattered. */
1186 RTGCPHYS GCPhysAddrDataBase = (RTGCPHYS)pTaskState->CommandControlBlockGuest.u32PhysAddrData;
1187
1188 AssertMsg(GCPhysAddrDataBase != 0, ("Physical address is 0\n"));
1189
1190 Log(("Non scattered buffer:\n"));
1191 Log(("u32PhysAddrData=%#x\n", pTaskState->CommandControlBlockGuest.u32PhysAddrData));
1192 Log(("cbData=%u\n", pTaskState->CommandControlBlockGuest.cbData));
1193 Log(("GCPhysAddrDataBase=0x%RGp\n", GCPhysAddrDataBase));
1194
1195 /* Copy the data into the guest memory. */
1196 PDMDevHlpPhysWrite(pDevIns, GCPhysAddrDataBase, pTaskState->DataSeg.pvSeg, pTaskState->DataSeg.cbSeg);
1197 }
1198 }
1199
1200 RTMemFree(pTaskState->DataSeg.pvSeg);
1201 pTaskState->DataSeg.pvSeg = NULL;
1202 pTaskState->DataSeg.cbSeg = 0;
1203}
1204
1205/**
1206 * Free the sense buffer.
1207 *
1208 * @returns nothing.
1209 * @param pTaskState Pointer to the task state.
1210 * @param fCopy If sense data should be copied to guest memory.
1211 */
1212static void buslogicSenseBufferFree(PBUSLOGICTASKSTATE pTaskState, bool fCopy)
1213{
1214 PPDMDEVINS pDevIns = pTaskState->CTX_SUFF(pTargetDevice)->CTX_SUFF(pBusLogic)->CTX_SUFF(pDevIns);
1215 RTGCPHYS GCPhysAddrSenseBuffer = (RTGCPHYS)pTaskState->CommandControlBlockGuest.u32PhysAddrSenseData;
1216 uint32_t cbSenseBuffer = pTaskState->CommandControlBlockGuest.cbSenseData;
1217
1218 /* Copy into guest memory. */
1219 if (fCopy)
1220 PDMDevHlpPhysWrite(pDevIns, GCPhysAddrSenseBuffer, pTaskState->pbSenseBuffer, cbSenseBuffer);
1221
1222 RTMemFree(pTaskState->pbSenseBuffer);
1223 pTaskState->pbSenseBuffer = NULL;
1224}
1225
1226/**
1227 * Alloc the sense buffer.
1228 *
1229 * @returns VBox status code.
1230 * @param pTaskState Pointer to the task state.
1231 * @note Current assumption is that the sense buffer is not scattered and does not cross a page boundary.
1232 */
1233static int buslogicSenseBufferAlloc(PBUSLOGICTASKSTATE pTaskState)
1234{
1235 PPDMDEVINS pDevIns = pTaskState->CTX_SUFF(pTargetDevice)->CTX_SUFF(pBusLogic)->CTX_SUFF(pDevIns);
1236 uint32_t cbSenseBuffer = pTaskState->CommandControlBlockGuest.cbSenseData;
1237
1238 pTaskState->pbSenseBuffer = (uint8_t *)RTMemAllocZ(cbSenseBuffer);
1239 if (!pTaskState->pbSenseBuffer)
1240 return VERR_NO_MEMORY;
1241
1242 return VINF_SUCCESS;
1243}
1244#endif /* IN_RING3 */
1245
1246/**
1247 * Parses the command buffer and executes it.
1248 *
1249 * @returns VBox status code.
1250 * @param pBusLogic Pointer to the BusLogic device instance.
1251 */
1252static int buslogicProcessCommand(PBUSLOGIC pBusLogic)
1253{
1254 int rc = VINF_SUCCESS;
1255
1256 LogFlowFunc(("pBusLogic=%#p\n", pBusLogic));
1257 AssertMsg(pBusLogic->uOperationCode != 0xff, ("There is no command to execute\n"));
1258
1259 switch (pBusLogic->uOperationCode)
1260 {
1261 case BUSLOGICCOMMAND_INQUIRE_PCI_HOST_ADAPTER_INFORMATION:
1262 {
1263 PReplyInquirePCIHostAdapterInformation pReply = (PReplyInquirePCIHostAdapterInformation)pBusLogic->aReplyBuffer;
1264 memset(pReply, 0, sizeof(ReplyInquirePCIHostAdapterInformation));
1265
1266 /* It seems VMware does not provide valid information here too, lets do the same :) */
1267 pReply->InformationIsValid = 0;
1268 pReply->IsaIOPort = 0xff; /* Make it invalid. */
1269 pBusLogic->cbReplyParametersLeft = sizeof(ReplyInquirePCIHostAdapterInformation);
1270 break;
1271 }
1272 case BUSLOGICCOMMAND_MODIFY_IO_ADDRESS:
1273 {
1274 pBusLogic->cbReplyParametersLeft = 0;
1275 if (pBusLogic->aCommandBuffer[0] == 0x06)
1276 {
1277 Log(("Disabling ISA I/O ports.\n"));
1278 pBusLogic->fISAEnabled = false;
1279 }
1280 break;
1281 }
1282 case BUSLOGICCOMMAND_INQUIRE_BOARD_ID:
1283 {
1284 pBusLogic->aReplyBuffer[0] = '0'; /* @todo figure out what to write here. */
1285 pBusLogic->aReplyBuffer[1] = '0'; /* @todo figure out what to write here. */
1286
1287 /* We report version 5.07B. This reply will provide the first two digits. */
1288 pBusLogic->aReplyBuffer[2] = '5'; /* Major version 5 */
1289 pBusLogic->aReplyBuffer[3] = '0'; /* Minor version 0 */
1290 pBusLogic->cbReplyParametersLeft = 4; /* Reply is 4 bytes long */
1291 break;
1292 }
1293 case BUSLOGICCOMMAND_INQUIRE_FIRMWARE_VERSION_3RD_LETTER:
1294 {
1295 pBusLogic->aReplyBuffer[0] = '7';
1296 pBusLogic->cbReplyParametersLeft = 1;
1297 break;
1298 }
1299 case BUSLOGICCOMMAND_INQUIRE_FIRMWARE_VERSION_LETTER:
1300 {
1301 pBusLogic->aReplyBuffer[0] = 'B';
1302 pBusLogic->cbReplyParametersLeft = 1;
1303 break;
1304 }
1305 case BUSLOGICCOMMAND_INQUIRE_HOST_ADAPTER_MODEL_NUMBER:
1306 {
1307 /* The reply length is set by the guest and is found in the first byte of the command buffer. */
1308 pBusLogic->cbReplyParametersLeft = pBusLogic->aCommandBuffer[0];
1309 memset(pBusLogic->aReplyBuffer, 0, pBusLogic->cbReplyParametersLeft);
1310 const char aModelName[] = "958";
1311 int cCharsToTransfer = (pBusLogic->cbReplyParametersLeft <= sizeof(aModelName))
1312 ? pBusLogic->cbReplyParametersLeft
1313 : sizeof(aModelName);
1314
1315 for (int i = 0; i < cCharsToTransfer; i++)
1316 pBusLogic->aReplyBuffer[i] = aModelName[i];
1317
1318 break;
1319 }
1320 case BUSLOGICCOMMAND_INQUIRE_CONFIGURATION:
1321 {
1322 pBusLogic->cbReplyParametersLeft = sizeof(ReplyInquireConfiguration);
1323 PReplyInquireConfiguration pReply = (PReplyInquireConfiguration)pBusLogic->aReplyBuffer;
1324 memset(pReply, 0, sizeof(ReplyInquireConfiguration));
1325
1326 pReply->uHostAdapterId = 7; /* The controller has always 7 as ID. */
1327 /*
1328 * The rest of this reply only applies for ISA adapters.
1329 * This is a PCI adapter so they are not important and are skipped.
1330 */
1331 break;
1332 }
1333 case BUSLOGICCOMMAND_INQUIRE_EXTENDED_SETUP_INFORMATION:
1334 {
1335 /* The reply length is set by the guest and is found in the first byte of the command buffer. */
1336 pBusLogic->cbReplyParametersLeft = pBusLogic->aCommandBuffer[0];
1337 PReplyInquireExtendedSetupInformation pReply = (PReplyInquireExtendedSetupInformation)pBusLogic->aReplyBuffer;
1338 memset(pReply, 0, sizeof(ReplyInquireExtendedSetupInformation));
1339
1340 pReply->fHostWideSCSI = true;
1341 pReply->fHostUltraSCSI = true;
1342 pReply->u16ScatterGatherLimit = 8192;
1343 pBusLogic->regStatus |= BUSLOGIC_REGISTER_STATUS_INITIALIZATION_REQUIRED;
1344
1345 break;
1346 }
1347 case BUSLOGICCOMMAND_INQUIRE_SETUP_INFORMATION:
1348 {
1349 /* The reply length is set by the guest and is found in the first byte of the command buffer. */
1350 pBusLogic->cbReplyParametersLeft = pBusLogic->aCommandBuffer[0];
1351 PReplyInquireSetupInformation pReply = (PReplyInquireSetupInformation)pBusLogic->aReplyBuffer;
1352 memset(pReply, 0, sizeof(ReplyInquireSetupInformation));
1353 break;
1354 }
1355 case BUSLOGICCOMMAND_FETCH_HOST_ADAPTER_LOCAL_RAM:
1356 {
1357 /*
1358 * First element in the command buffer contains start offset to read from
1359 * and second one the number of bytes to read.
1360 */
1361 uint8_t uOffset = pBusLogic->aCommandBuffer[0];
1362 pBusLogic->cbReplyParametersLeft = pBusLogic->aCommandBuffer[1];
1363
1364 pBusLogic->fUseLocalRam = true;
1365 pBusLogic->iReply = uOffset;
1366 break;
1367 }
1368 case BUSLOGICCOMMAND_INITIALIZE_EXTENDED_MAILBOX:
1369 {
1370 PRequestInitializeExtendedMailbox pRequest = (PRequestInitializeExtendedMailbox)pBusLogic->aCommandBuffer;
1371
1372 pBusLogic->cMailbox = pRequest->cMailbox;
1373 pBusLogic->GCPhysAddrMailboxOutgoingBase = (RTGCPHYS)pRequest->uMailboxBaseAddress;
1374 /* The area for incoming mailboxes is right after the last entry of outgoing mailboxes. */
1375 pBusLogic->GCPhysAddrMailboxIncomingBase = (RTGCPHYS)pRequest->uMailboxBaseAddress + (pBusLogic->cMailbox * sizeof(Mailbox));
1376
1377 Log(("GCPhysAddrMailboxOutgoingBase=%RGp\n", pBusLogic->GCPhysAddrMailboxOutgoingBase));
1378 Log(("GCPhysAddrMailboxOutgoingBase=%RGp\n", pBusLogic->GCPhysAddrMailboxIncomingBase));
1379 Log(("cMailboxes=%u\n", pBusLogic->cMailbox));
1380
1381 pBusLogic->cbReplyParametersLeft = 0;
1382 break;
1383 }
1384 case BUSLOGICCOMMAND_ENABLE_STRICT_ROUND_ROBIN_MODE:
1385 {
1386 if (pBusLogic->aCommandBuffer[0] == 0)
1387 pBusLogic->fStrictRoundRobinMode = false;
1388 else if (pBusLogic->aCommandBuffer[0] == 1)
1389 pBusLogic->fStrictRoundRobinMode = true;
1390 else
1391 AssertMsgFailed(("Invalid round robin mode %d\n", pBusLogic->aCommandBuffer[0]));
1392
1393 pBusLogic->cbReplyParametersLeft = 0;
1394 break;
1395 }
1396 case BUSLOGICCOMMAND_SET_CCB_FORMAT:
1397 {
1398 if (pBusLogic->aCommandBuffer[0] == 0)
1399 pBusLogic->fExtendedLunCCBFormat = false;
1400 else if (pBusLogic->aCommandBuffer[0] == 1)
1401 pBusLogic->fExtendedLunCCBFormat = true;
1402 else
1403 AssertMsgFailed(("Invalid CCB format %d\n", pBusLogic->aCommandBuffer[0]));
1404
1405 pBusLogic->cbReplyParametersLeft = 0;
1406 break;
1407 }
1408 case BUSLOGICCOMMAND_INQUIRE_TARGET_DEVICES:
1409 {
1410 /* Each bit which is set in the 16bit wide variable means a present device. */
1411 uint16_t u16TargetsPresentMask = 0;
1412
1413 for (uint8_t i = 0; i < RT_ELEMENTS(pBusLogic->aDeviceStates); i++)
1414 {
1415 if (pBusLogic->aDeviceStates[i].fPresent)
1416 u16TargetsPresentMask |= (1 << i);
1417 }
1418 pBusLogic->aReplyBuffer[0] = (uint8_t)u16TargetsPresentMask;
1419 pBusLogic->aReplyBuffer[1] = (uint8_t)(u16TargetsPresentMask >> 8);
1420 pBusLogic->cbReplyParametersLeft = 2;
1421 break;
1422 }
1423 case BUSLOGICCOMMAND_INQUIRE_SYNCHRONOUS_PERIOD:
1424 {
1425 pBusLogic->cbReplyParametersLeft = pBusLogic->aCommandBuffer[0];
1426
1427 for (uint8_t i = 0; i < pBusLogic->cbReplyParametersLeft; i++)
1428 pBusLogic->aReplyBuffer[i] = 0; /* @todo Figure if we need something other here. It's not needed for the linux driver */
1429
1430 break;
1431 }
1432 case BUSLOGICCOMMAND_DISABLE_HOST_ADAPTER_INTERRUPT:
1433 {
1434 if (pBusLogic->aCommandBuffer[0] == 0)
1435 pBusLogic->fIRQEnabled = false;
1436 else
1437 pBusLogic->fIRQEnabled = true;
1438 break;
1439 }
1440 case BUSLOGICCOMMAND_EXECUTE_MAILBOX_COMMAND: /* Should be handled already. */
1441 default:
1442 AssertMsgFailed(("Invalid command %#x\n", pBusLogic->uOperationCode));
1443 }
1444
1445 Log(("cbReplyParametersLeft=%d\n", pBusLogic->cbReplyParametersLeft));
1446
1447 /* Set the data in ready bit in the status register in case the command has a reply. */
1448 if (pBusLogic->cbReplyParametersLeft)
1449 pBusLogic->regStatus |= BUSLOGIC_REGISTER_STATUS_DATA_IN_REGISTER_READY;
1450 else
1451 buslogicCommandComplete(pBusLogic);
1452
1453 return rc;
1454}
1455
1456/**
1457 * Read a register from the BusLogic adapter.
1458 *
1459 * @returns VBox status code.
1460 * @param pBusLogic Pointer to the BusLogic instance data.
1461 * @param iRegister The index of the register to read.
1462 * @param pu32 Where to store the register content.
1463 */
1464static int buslogicRegisterRead(PBUSLOGIC pBusLogic, unsigned iRegister, uint32_t *pu32)
1465{
1466 int rc = VINF_SUCCESS;
1467
1468 switch (iRegister)
1469 {
1470 case BUSLOGIC_REGISTER_STATUS:
1471 {
1472 *pu32 = pBusLogic->regStatus;
1473 /*
1474 * If the diagnostic active bit is set we are in a hard reset initiated from the guest.
1475 * The guest reads the status register and waits that the host adapter ready bit is set.
1476 */
1477 if (pBusLogic->regStatus & BUSLOGIC_REGISTER_STATUS_DIAGNOSTIC_ACTIVE)
1478 {
1479 pBusLogic->regStatus &= ~BUSLOGIC_REGISTER_STATUS_DIAGNOSTIC_ACTIVE;
1480 pBusLogic->regStatus |= BUSLOGIC_REGISTER_STATUS_HOST_ADAPTER_READY;
1481 }
1482 break;
1483 }
1484 case BUSLOGIC_REGISTER_DATAIN:
1485 {
1486 if (pBusLogic->fUseLocalRam)
1487 *pu32 = pBusLogic->LocalRam.u8View[pBusLogic->iReply];
1488 else
1489 *pu32 = pBusLogic->aReplyBuffer[pBusLogic->iReply];
1490
1491 pBusLogic->iReply++;
1492 pBusLogic->cbReplyParametersLeft--;
1493
1494 if (!pBusLogic->cbReplyParametersLeft)
1495 {
1496 /*
1497 * Reply finished, set command complete bit, unset data in ready bit and
1498 * interrupt the guest if enabled.
1499 */
1500 buslogicCommandComplete(pBusLogic);
1501 }
1502 break;
1503 }
1504 case BUSLOGIC_REGISTER_INTERRUPT:
1505 {
1506 *pu32 = pBusLogic->regInterrupt;
1507 break;
1508 }
1509 case BUSLOGIC_REGISTER_GEOMETRY:
1510 {
1511 *pu32 = pBusLogic->regGeometry;
1512 break;
1513 }
1514 default:
1515 *pu32 = UINT32_C(0xffffffff);
1516 }
1517
1518 Log2(("%s: pu32=%p:{%.*Rhxs} iRegister=%d rc=%Rrc\n",
1519 __FUNCTION__, pu32, 1, pu32, iRegister, rc));
1520
1521 return rc;
1522}
1523
1524/**
1525 * Write a value to a register.
1526 *
1527 * @returns VBox status code.
1528 * @param pBusLogic Pointer to the BusLogic instance data.
1529 * @param iRegister The index of the register to read.
1530 * @param uVal The value to write.
1531 */
1532static int buslogicRegisterWrite(PBUSLOGIC pBusLogic, unsigned iRegister, uint8_t uVal)
1533{
1534 int rc = VINF_SUCCESS;
1535
1536 switch (iRegister)
1537 {
1538 case BUSLOGIC_REGISTER_CONTROL:
1539 {
1540 if (uVal & BUSLOGIC_REGISTER_CONTROL_INTERRUPT_RESET)
1541 buslogicClearInterrupt(pBusLogic);
1542
1543 if ((uVal & BUSLOGIC_REGISTER_CONTROL_HARD_RESET) || (uVal & BUSLOGIC_REGISTER_CONTROL_SOFT_RESET))
1544 {
1545#ifdef IN_RING3
1546 buslogicIntiateHardReset(pBusLogic);
1547#else
1548 rc = VINF_IOM_HC_IOPORT_WRITE;
1549#endif
1550 }
1551
1552 break;
1553 }
1554 case BUSLOGIC_REGISTER_COMMAND:
1555 {
1556 /* Fast path for mailbox execution command. */
1557 if ((uVal == BUSLOGICCOMMAND_EXECUTE_MAILBOX_COMMAND) && (pBusLogic->uOperationCode == 0xff))
1558 {
1559 ASMAtomicIncU32(&pBusLogic->cMailboxesReady);
1560 if (!ASMAtomicXchgBool(&pBusLogic->fNotificationSend, true))
1561 {
1562 /* Send new notification to the queue. */
1563 PPDMQUEUEITEMCORE pItem = PDMQueueAlloc(pBusLogic->CTX_SUFF(pNotifierQueue));
1564 AssertMsg(pItem, ("Allocating item for queue failed\n"));
1565 PDMQueueInsert(pBusLogic->CTX_SUFF(pNotifierQueue), (PPDMQUEUEITEMCORE)pItem);
1566 }
1567
1568 return rc;
1569 }
1570
1571 /*
1572 * Check if we are already fetch command parameters from the guest.
1573 * If not we initialize executing a new command.
1574 */
1575 if (pBusLogic->uOperationCode == 0xff)
1576 {
1577 pBusLogic->uOperationCode = uVal;
1578 pBusLogic->iParameter = 0;
1579
1580 /* Mark host adapter as busy. */
1581 pBusLogic->regStatus &= ~BUSLOGIC_REGISTER_STATUS_HOST_ADAPTER_READY;
1582
1583 /* Get the number of bytes for parameters from the command code. */
1584 switch (pBusLogic->uOperationCode)
1585 {
1586 case BUSLOGICCOMMAND_INQUIRE_FIRMWARE_VERSION_LETTER:
1587 case BUSLOGICCOMMAND_INQUIRE_BOARD_ID:
1588 case BUSLOGICCOMMAND_INQUIRE_FIRMWARE_VERSION_3RD_LETTER:
1589 case BUSLOGICCOMMAND_INQUIRE_PCI_HOST_ADAPTER_INFORMATION:
1590 case BUSLOGICCOMMAND_INQUIRE_CONFIGURATION:
1591 case BUSLOGICCOMMAND_INQUIRE_TARGET_DEVICES:
1592 pBusLogic->cbCommandParametersLeft = 0;
1593 break;
1594 case BUSLOGICCOMMAND_MODIFY_IO_ADDRESS:
1595 case BUSLOGICCOMMAND_INQUIRE_EXTENDED_SETUP_INFORMATION:
1596 case BUSLOGICCOMMAND_INQUIRE_SETUP_INFORMATION:
1597 case BUSLOGICCOMMAND_INQUIRE_HOST_ADAPTER_MODEL_NUMBER:
1598 case BUSLOGICCOMMAND_ENABLE_STRICT_ROUND_ROBIN_MODE:
1599 case BUSLOGICCOMMAND_SET_CCB_FORMAT:
1600 case BUSLOGICCOMMAND_INQUIRE_SYNCHRONOUS_PERIOD:
1601 case BUSLOGICCOMMAND_DISABLE_HOST_ADAPTER_INTERRUPT:
1602 pBusLogic->cbCommandParametersLeft = 1;
1603 break;
1604 case BUSLOGICCOMMAND_FETCH_HOST_ADAPTER_LOCAL_RAM:
1605 pBusLogic->cbCommandParametersLeft = 2;
1606 break;
1607 case BUSLOGICCOMMAND_INITIALIZE_EXTENDED_MAILBOX:
1608 pBusLogic->cbCommandParametersLeft = sizeof(RequestInitializeExtendedMailbox);
1609 break;
1610 case BUSLOGICCOMMAND_EXECUTE_MAILBOX_COMMAND: /* Should not come here anymore. */
1611 default:
1612 AssertMsgFailed(("Invalid operation code %#x\n", uVal));
1613 }
1614 }
1615 else
1616 {
1617 /*
1618 * The real adapter would set the Command register busy bit in the status register.
1619 * The guest has to wait until it is unset.
1620 * We don't need to do it because the guest does not continue execution while we are in this
1621 * function.
1622 */
1623 pBusLogic->aCommandBuffer[pBusLogic->iParameter] = uVal;
1624 pBusLogic->iParameter++;
1625 pBusLogic->cbCommandParametersLeft--;
1626 }
1627
1628 /* Start execution of command if there are no parameters left. */
1629 if (!pBusLogic->cbCommandParametersLeft)
1630 {
1631 rc = buslogicProcessCommand(pBusLogic);
1632 AssertMsgRC(rc, ("Processing command failed rc=%Rrc\n", rc));
1633 }
1634 break;
1635 }
1636 default:
1637 AssertMsgFailed(("Register not available\n"));
1638 rc = VERR_IOM_IOPORT_UNUSED;
1639 }
1640
1641 return rc;
1642}
1643
1644/**
1645 * Memory mapped I/O Handler for read operations.
1646 *
1647 * @returns VBox status code.
1648 *
1649 * @param pDevIns The device instance.
1650 * @param pvUser User argument.
1651 * @param GCPhysAddr Physical address (in GC) where the read starts.
1652 * @param pv Where to store the result.
1653 * @param cb Number of bytes read.
1654 */
1655PDMBOTHCBDECL(int) buslogicMMIORead(PPDMDEVINS pDevIns, void *pvUser,
1656 RTGCPHYS GCPhysAddr, void *pv, unsigned cb)
1657{
1658 /* the linux driver does not make use of the MMIO area. */
1659 AssertMsgFailed(("MMIO Read\n"));
1660 return VINF_SUCCESS;
1661}
1662
1663/**
1664 * Memory mapped I/O Handler for write operations.
1665 *
1666 * @returns VBox status code.
1667 *
1668 * @param pDevIns The device instance.
1669 * @param pvUser User argument.
1670 * @param GCPhysAddr Physical address (in GC) where the read starts.
1671 * @param pv Where to fetch the result.
1672 * @param cb Number of bytes to write.
1673 */
1674PDMBOTHCBDECL(int) buslogicMMIOWrite(PPDMDEVINS pDevIns, void *pvUser,
1675 RTGCPHYS GCPhysAddr, void *pv, unsigned cb)
1676{
1677 /* the linux driver does not make use of the MMIO area. */
1678 AssertMsgFailed(("MMIO Write\n"));
1679 return VINF_SUCCESS;
1680}
1681
1682/**
1683 * Port I/O Handler for IN operations.
1684 *
1685 * @returns VBox status code.
1686 *
1687 * @param pDevIns The device instance.
1688 * @param pvUser User argument.
1689 * @param uPort Port number used for the IN operation.
1690 * @param pu32 Where to store the result.
1691 * @param cb Number of bytes read.
1692 */
1693PDMBOTHCBDECL(int) buslogicIOPortRead (PPDMDEVINS pDevIns, void *pvUser,
1694 RTIOPORT Port, uint32_t *pu32, unsigned cb)
1695{
1696 PBUSLOGIC pBusLogic = PDMINS_2_DATA(pDevIns, PBUSLOGIC);;
1697 unsigned iRegister = Port - pBusLogic->IOPortBase;
1698
1699 Assert(cb == 1);
1700
1701 return buslogicRegisterRead(pBusLogic, iRegister, pu32);
1702}
1703
1704/**
1705 * Port I/O Handler for OUT operations.
1706 *
1707 * @returns VBox status code.
1708 *
1709 * @param pDevIns The device instance.
1710 * @param pvUser User argument.
1711 * @param uPort Port number used for the IN operation.
1712 * @param u32 The value to output.
1713 * @param cb The value size in bytes.
1714 */
1715PDMBOTHCBDECL(int) buslogicIOPortWrite (PPDMDEVINS pDevIns, void *pvUser,
1716 RTIOPORT Port, uint32_t u32, unsigned cb)
1717{
1718 PBUSLOGIC pBusLogic = PDMINS_2_DATA(pDevIns, PBUSLOGIC);
1719 int rc = VINF_SUCCESS;
1720 unsigned iRegister = Port - pBusLogic->IOPortBase;
1721 uint8_t uVal = (uint8_t)u32;
1722
1723 Assert(cb == 1);
1724
1725 rc = buslogicRegisterWrite(pBusLogic, iRegister, (uint8_t)uVal);
1726
1727 Log2(("#%d %s: pvUser=%#p cb=%d u32=%#x Port=%#x rc=%Rrc\n",
1728 pDevIns->iInstance, __FUNCTION__, pvUser, cb, u32, Port, rc));
1729
1730 return rc;
1731}
1732
1733#ifdef IN_RING3
1734/**
1735 * Port I/O Handler for IN operations - legacy port.
1736 *
1737 * @returns VBox status code.
1738 *
1739 * @param pDevIns The device instance.
1740 * @param pvUser User argument.
1741 * @param uPort Port number used for the IN operation.
1742 * @param pu32 Where to store the result.
1743 * @param cb Number of bytes read.
1744 */
1745static int buslogicIsaIOPortRead (PPDMDEVINS pDevIns, void *pvUser,
1746 RTIOPORT Port, uint32_t *pu32, unsigned cb)
1747{
1748 int rc;
1749 PBUSLOGIC pBusLogic = PDMINS_2_DATA(pDevIns, PBUSLOGIC);
1750
1751 Assert(cb == 1);
1752
1753 if (!pBusLogic->fISAEnabled)
1754 return VERR_IOM_IOPORT_UNUSED;
1755
1756 rc = vboxscsiReadRegister(&pBusLogic->VBoxSCSI, (Port - BUSLOGIC_ISA_IO_PORT), pu32);
1757
1758 //Log2(("%s: pu32=%p:{%.*Rhxs} iRegister=%d rc=%Rrc\n",
1759 // __FUNCTION__, pu32, 1, pu32, (Port - BUSLOGIC_ISA_IO_PORT), rc));
1760
1761 return rc;
1762}
1763
1764static int buslogicPrepareBIOSSCSIRequest(PBUSLOGIC pBusLogic)
1765{
1766 int rc;
1767 PBUSLOGICTASKSTATE pTaskState;
1768 uint32_t uTargetDevice;
1769
1770 rc = RTMemCacheAllocEx(pBusLogic->hTaskCache, (void **)&pTaskState);
1771 AssertMsgRCReturn(rc, ("Getting task from cache failed rc=%Rrc\n", rc), rc);
1772
1773 pTaskState->fBIOS = true;
1774
1775 rc = vboxscsiSetupRequest(&pBusLogic->VBoxSCSI, &pTaskState->PDMScsiRequest, &uTargetDevice);
1776 AssertMsgRCReturn(rc, ("Setting up SCSI request failed rc=%Rrc\n", rc), rc);
1777
1778 pTaskState->PDMScsiRequest.pvUser = pTaskState;
1779
1780 pTaskState->CTX_SUFF(pTargetDevice) = &pBusLogic->aDeviceStates[uTargetDevice];
1781
1782 if (!pTaskState->CTX_SUFF(pTargetDevice)->fPresent)
1783 {
1784 /* Device is not present. */
1785 AssertMsg(pTaskState->PDMScsiRequest.pbCDB[0] == SCSI_INQUIRY,
1786 ("Device is not present but command is not inquiry\n"));
1787
1788 SCSIINQUIRYDATA ScsiInquiryData;
1789
1790 memset(&ScsiInquiryData, 0, sizeof(SCSIINQUIRYDATA));
1791 ScsiInquiryData.u5PeripheralDeviceType = SCSI_INQUIRY_DATA_PERIPHERAL_DEVICE_TYPE_UNKNOWN;
1792 ScsiInquiryData.u3PeripheralQualifier = SCSI_INQUIRY_DATA_PERIPHERAL_QUALIFIER_NOT_CONNECTED_NOT_SUPPORTED;
1793
1794 memcpy(pBusLogic->VBoxSCSI.pBuf, &ScsiInquiryData, 5);
1795
1796 rc = vboxscsiRequestFinished(&pBusLogic->VBoxSCSI, &pTaskState->PDMScsiRequest);
1797 AssertMsgRCReturn(rc, ("Finishing BIOS SCSI request failed rc=%Rrc\n", rc), rc);
1798
1799 RTMemCacheFree(pBusLogic->hTaskCache, pTaskState);
1800 }
1801 else
1802 {
1803 LogFlowFunc(("before increment %u\n", pTaskState->CTX_SUFF(pTargetDevice)->cOutstandingRequests));
1804 ASMAtomicIncU32(&pTaskState->CTX_SUFF(pTargetDevice)->cOutstandingRequests);
1805 LogFlowFunc(("after increment %u\n", pTaskState->CTX_SUFF(pTargetDevice)->cOutstandingRequests));
1806
1807 rc = pTaskState->CTX_SUFF(pTargetDevice)->pDrvSCSIConnector->pfnSCSIRequestSend(pTaskState->CTX_SUFF(pTargetDevice)->pDrvSCSIConnector,
1808 &pTaskState->PDMScsiRequest);
1809 AssertMsgRCReturn(rc, ("Sending request to SCSI layer failed rc=%Rrc\n", rc), rc);
1810 }
1811
1812 return rc;
1813}
1814
1815/**
1816 * Port I/O Handler for OUT operations - legacy port.
1817 *
1818 * @returns VBox status code.
1819 *
1820 * @param pDevIns The device instance.
1821 * @param pvUser User argument.
1822 * @param uPort Port number used for the IN operation.
1823 * @param u32 The value to output.
1824 * @param cb The value size in bytes.
1825 */
1826static int buslogicIsaIOPortWrite (PPDMDEVINS pDevIns, void *pvUser,
1827 RTIOPORT Port, uint32_t u32, unsigned cb)
1828{
1829 int rc;
1830 PBUSLOGIC pBusLogic = PDMINS_2_DATA(pDevIns, PBUSLOGIC);
1831
1832 Log2(("#%d %s: pvUser=%#p cb=%d u32=%#x Port=%#x\n",
1833 pDevIns->iInstance, __FUNCTION__, pvUser, cb, u32, Port));
1834
1835 Assert(cb == 1);
1836
1837 if (!pBusLogic->fISAEnabled)
1838 return VERR_IOM_IOPORT_UNUSED;
1839
1840 rc = vboxscsiWriteRegister(&pBusLogic->VBoxSCSI, (Port - BUSLOGIC_ISA_IO_PORT), (uint8_t)u32);
1841 if (rc == VERR_MORE_DATA)
1842 {
1843 rc = buslogicPrepareBIOSSCSIRequest(pBusLogic);
1844 AssertRC(rc);
1845 }
1846 else if (RT_FAILURE(rc))
1847 AssertMsgFailed(("Writing BIOS register failed %Rrc\n", rc));
1848
1849 return VINF_SUCCESS;
1850}
1851
1852/**
1853 * Port I/O Handler for primary port range OUT string operations.
1854 * @see FNIOMIOPORTOUTSTRING for details.
1855 */
1856static DECLCALLBACK(int) buslogicIsaIOPortWriteStr(PPDMDEVINS pDevIns, void *pvUser, RTIOPORT Port, RTGCPTR *pGCPtrSrc, PRTGCUINTREG pcTransfer, unsigned cb)
1857{
1858 PBUSLOGIC pBusLogic = PDMINS_2_DATA(pDevIns, PBUSLOGIC);
1859 int rc;
1860
1861 Log2(("#%d %s: pvUser=%#p cb=%d Port=%#x\n",
1862 pDevIns->iInstance, __FUNCTION__, pvUser, cb, Port));
1863
1864 rc = vboxscsiWriteString(pDevIns, &pBusLogic->VBoxSCSI, (Port - BUSLOGIC_ISA_IO_PORT),
1865 pGCPtrSrc, pcTransfer, cb);
1866 if (rc == VERR_MORE_DATA)
1867 {
1868 rc = buslogicPrepareBIOSSCSIRequest(pBusLogic);
1869 AssertRC(rc);
1870 }
1871 else if (RT_FAILURE(rc))
1872 AssertMsgFailed(("Writing BIOS register failed %Rrc\n", rc));
1873
1874 return rc;
1875}
1876
1877/**
1878 * Port I/O Handler for primary port range IN string operations.
1879 * @see FNIOMIOPORTINSTRING for details.
1880 */
1881static DECLCALLBACK(int) buslogicIsaIOPortReadStr(PPDMDEVINS pDevIns, void *pvUser, RTIOPORT Port, RTGCPTR *pGCPtrDst, PRTGCUINTREG pcTransfer, unsigned cb)
1882{
1883 PBUSLOGIC pBusLogic = PDMINS_2_DATA(pDevIns, PBUSLOGIC);
1884
1885 LogFlowFunc(("#%d %s: pvUser=%#p cb=%d Port=%#x\n",
1886 pDevIns->iInstance, __FUNCTION__, pvUser, cb, Port));
1887
1888 return vboxscsiReadString(pDevIns, &pBusLogic->VBoxSCSI, (Port - BUSLOGIC_ISA_IO_PORT),
1889 pGCPtrDst, pcTransfer, cb);
1890}
1891
1892static DECLCALLBACK(int) buslogicMMIOMap(PPCIDEVICE pPciDev, /*unsigned*/ int iRegion,
1893 RTGCPHYS GCPhysAddress, uint32_t cb,
1894 PCIADDRESSSPACE enmType)
1895{
1896 PPDMDEVINS pDevIns = pPciDev->pDevIns;
1897 PBUSLOGIC pThis = PDMINS_2_DATA(pDevIns, PBUSLOGIC);
1898 int rc = VINF_SUCCESS;
1899
1900 Log2(("%s: registering MMIO area at GCPhysAddr=%RGp cb=%u\n", __FUNCTION__, GCPhysAddress, cb));
1901
1902 Assert(cb >= 32);
1903
1904 if (enmType == PCI_ADDRESS_SPACE_MEM)
1905 {
1906 /* We use the assigned size here, because we currently only support page aligned MMIO ranges. */
1907 rc = PDMDevHlpMMIORegister(pDevIns, GCPhysAddress, cb, NULL,
1908 buslogicMMIOWrite, buslogicMMIORead, NULL, "BusLogic");
1909 if (RT_FAILURE(rc))
1910 return rc;
1911
1912 if (pThis->fR0Enabled)
1913 {
1914 rc = PDMDevHlpMMIORegisterR0(pDevIns, GCPhysAddress, cb, 0,
1915 "buslogicMMIOWrite", "buslogicMMIORead", NULL);
1916 if (RT_FAILURE(rc))
1917 return rc;
1918 }
1919
1920 if (pThis->fGCEnabled)
1921 {
1922 rc = PDMDevHlpMMIORegisterRC(pDevIns, GCPhysAddress, cb, 0,
1923 "buslogicMMIOWrite", "buslogicMMIORead", NULL);
1924 if (RT_FAILURE(rc))
1925 return rc;
1926 }
1927
1928 pThis->MMIOBase = GCPhysAddress;
1929 }
1930 else if (enmType == PCI_ADDRESS_SPACE_IO)
1931 {
1932 rc = PDMDevHlpIOPortRegister(pDevIns, (RTIOPORT)GCPhysAddress, 32,
1933 NULL, buslogicIOPortWrite, buslogicIOPortRead, NULL, NULL, "BusLogic");
1934 if (RT_FAILURE(rc))
1935 return rc;
1936
1937 if (pThis->fR0Enabled)
1938 {
1939 rc = PDMDevHlpIOPortRegisterR0(pDevIns, (RTIOPORT)GCPhysAddress, 32,
1940 0, "buslogicIOPortWrite", "buslogicIOPortRead", NULL, NULL, "BusLogic");
1941 if (RT_FAILURE(rc))
1942 return rc;
1943 }
1944
1945 if (pThis->fGCEnabled)
1946 {
1947 rc = PDMDevHlpIOPortRegisterRC(pDevIns, (RTIOPORT)GCPhysAddress, 32,
1948 0, "buslogicIOPortWrite", "buslogicIOPortRead", NULL, NULL, "BusLogic");
1949 if (RT_FAILURE(rc))
1950 return rc;
1951 }
1952
1953 pThis->IOPortBase = (RTIOPORT)GCPhysAddress;
1954 }
1955 else
1956 AssertMsgFailed(("Invalid enmType=%d\n", enmType));
1957
1958 return rc;
1959}
1960
1961static DECLCALLBACK(int) buslogicDeviceSCSIRequestCompleted(PPDMISCSIPORT pInterface, PPDMSCSIREQUEST pSCSIRequest, int rcCompletion)
1962{
1963 int rc;
1964 PBUSLOGICTASKSTATE pTaskState = (PBUSLOGICTASKSTATE)pSCSIRequest->pvUser;
1965 PBUSLOGICDEVICE pBusLogicDevice = pTaskState->CTX_SUFF(pTargetDevice);
1966 PBUSLOGIC pBusLogic = pBusLogicDevice->CTX_SUFF(pBusLogic);
1967
1968 LogFlowFunc(("before decrement %u\n", pBusLogicDevice->cOutstandingRequests));
1969 ASMAtomicDecU32(&pBusLogicDevice->cOutstandingRequests);
1970 LogFlowFunc(("after decrement %u\n", pBusLogicDevice->cOutstandingRequests));
1971
1972 if (pTaskState->fBIOS)
1973 {
1974 rc = vboxscsiRequestFinished(&pBusLogic->VBoxSCSI, pSCSIRequest);
1975 AssertMsgRC(rc, ("Finishing BIOS SCSI request failed rc=%Rrc\n", rc));
1976 }
1977 else
1978 {
1979 buslogicDataBufferFree(pTaskState);
1980
1981 if (pTaskState->pbSenseBuffer)
1982 buslogicSenseBufferFree(pTaskState, (rcCompletion != SCSI_STATUS_OK));
1983
1984 buslogicSendIncomingMailbox(pBusLogic, pTaskState,
1985 BUSLOGIC_MAILBOX_INCOMING_ADAPTER_STATUS_CMD_COMPLETED,
1986 BUSLOGIC_MAILBOX_INCOMING_DEVICE_STATUS_OPERATION_GOOD,
1987 BUSLOGIC_MAILBOX_INCOMING_COMPLETION_WITHOUT_ERROR);
1988 }
1989
1990 /* Add task to the cache. */
1991 RTMemCacheFree(pBusLogic->hTaskCache, pTaskState);
1992 return VINF_SUCCESS;
1993}
1994
1995/**
1996 * Read mailbox from the guest and execute command.
1997 *
1998 * @returns VBox status code.
1999 * @param pBusLogic Pointer to the BusLogic instance data.
2000 */
2001static int buslogicProcessMailboxNext(PBUSLOGIC pBusLogic)
2002{
2003 PBUSLOGICTASKSTATE pTaskState = NULL;
2004 RTGCPHYS GCPhysAddrMailboxCurrent;
2005 int rc;
2006
2007 rc = RTMemCacheAllocEx(pBusLogic->hTaskCache, (void **)&pTaskState);
2008 AssertMsgReturn(RT_SUCCESS(rc) && (pTaskState != NULL), ("Failed to get task state from cache\n"), rc);
2009
2010 pTaskState->fBIOS = false;
2011
2012 if (!pBusLogic->fStrictRoundRobinMode)
2013 {
2014 /* Search for a filled mailbox. */
2015 do
2016 {
2017 /* Fetch mailbox from guest memory. */
2018 GCPhysAddrMailboxCurrent = pBusLogic->GCPhysAddrMailboxOutgoingBase + (pBusLogic->uMailboxOutgoingPositionCurrent * sizeof(Mailbox));
2019
2020 PDMDevHlpPhysRead(pBusLogic->CTX_SUFF(pDevIns), GCPhysAddrMailboxCurrent,
2021 &pTaskState->MailboxGuest, sizeof(Mailbox));
2022
2023 pBusLogic->uMailboxOutgoingPositionCurrent++;
2024
2025 /* Check if we reached the end and start from the beginning if so. */
2026 if (pBusLogic->uMailboxOutgoingPositionCurrent >= pBusLogic->cMailbox)
2027 pBusLogic->uMailboxOutgoingPositionCurrent = 0;
2028 } while (pTaskState->MailboxGuest.u.out.uActionCode == BUSLOGIC_MAILBOX_OUTGOING_ACTION_FREE);
2029 }
2030 else
2031 {
2032 /* Fetch mailbox from guest memory. */
2033 GCPhysAddrMailboxCurrent = pBusLogic->GCPhysAddrMailboxOutgoingBase + (pBusLogic->uMailboxOutgoingPositionCurrent * sizeof(Mailbox));
2034
2035 PDMDevHlpPhysRead(pBusLogic->CTX_SUFF(pDevIns), GCPhysAddrMailboxCurrent,
2036 &pTaskState->MailboxGuest, sizeof(Mailbox));
2037 }
2038
2039#ifdef DEBUG
2040 buslogicDumpMailboxInfo(&pTaskState->MailboxGuest, true);
2041#endif
2042
2043 if (pTaskState->MailboxGuest.u.out.uActionCode == BUSLOGIC_MAILBOX_OUTGOING_ACTION_START_COMMAND)
2044 {
2045 /* Fetch CCB now. */
2046 RTGCPHYS GCPhysAddrCCB = (RTGCPHYS)pTaskState->MailboxGuest.u32PhysAddrCCB;
2047 PDMDevHlpPhysRead(pBusLogic->CTX_SUFF(pDevIns), GCPhysAddrCCB,
2048 &pTaskState->CommandControlBlockGuest, sizeof(CommandControlBlock));
2049
2050 PBUSLOGICDEVICE pTargetDevice = &pBusLogic->aDeviceStates[pTaskState->CommandControlBlockGuest.uTargetId];
2051 pTaskState->CTX_SUFF(pTargetDevice) = pTargetDevice;
2052
2053#ifdef DEBUG
2054 buslogicDumpCCBInfo(&pTaskState->CommandControlBlockGuest);
2055#endif
2056
2057 /* Alloc required buffers. */
2058 rc = buslogicDataBufferAlloc(pTaskState);
2059 AssertMsgRC(rc, ("Alloc failed rc=%Rrc\n", rc));
2060
2061 if (pTaskState->CommandControlBlockGuest.cbSenseData)
2062 {
2063 rc = buslogicSenseBufferAlloc(pTaskState);
2064 AssertMsgRC(rc, ("Mapping sense buffer failed rc=%Rrc\n", rc));
2065 }
2066
2067 /* Check if device is present on bus. If not return error immediately and don't process this further. */
2068 if (!pBusLogic->aDeviceStates[pTaskState->CommandControlBlockGuest.uTargetId].fPresent)
2069 {
2070 buslogicDataBufferFree(pTaskState);
2071
2072 if (pTaskState->pbSenseBuffer)
2073 buslogicSenseBufferFree(pTaskState, true);
2074
2075 buslogicSendIncomingMailbox(pBusLogic, pTaskState,
2076 BUSLOGIC_MAILBOX_INCOMING_ADAPTER_STATUS_SCSI_SELECTION_TIMEOUT,
2077 BUSLOGIC_MAILBOX_INCOMING_DEVICE_STATUS_OPERATION_GOOD,
2078 BUSLOGIC_MAILBOX_INCOMING_COMPLETION_WITH_ERROR);
2079
2080 RTMemCacheFree(pBusLogic->hTaskCache, pTaskState);
2081 }
2082 else
2083 {
2084 /* Setup SCSI request. */
2085 pTaskState->PDMScsiRequest.uLogicalUnit = pTaskState->CommandControlBlockGuest.uLogicalUnit;
2086
2087 if (pTaskState->CommandControlBlockGuest.uDataDirection == BUSLOGIC_CCB_DIRECTION_UNKNOWN)
2088 pTaskState->PDMScsiRequest.uDataDirection = PDMSCSIREQUESTTXDIR_UNKNOWN;
2089 else if (pTaskState->CommandControlBlockGuest.uDataDirection == BUSLOGIC_CCB_DIRECTION_IN)
2090 pTaskState->PDMScsiRequest.uDataDirection = PDMSCSIREQUESTTXDIR_FROM_DEVICE;
2091 else if (pTaskState->CommandControlBlockGuest.uDataDirection == BUSLOGIC_CCB_DIRECTION_OUT)
2092 pTaskState->PDMScsiRequest.uDataDirection = PDMSCSIREQUESTTXDIR_TO_DEVICE;
2093 else if (pTaskState->CommandControlBlockGuest.uDataDirection == BUSLOGIC_CCB_DIRECTION_NO_DATA)
2094 pTaskState->PDMScsiRequest.uDataDirection = PDMSCSIREQUESTTXDIR_NONE;
2095 else
2096 AssertMsgFailed(("Invalid data direction type %d\n", pTaskState->CommandControlBlockGuest.uDataDirection));
2097
2098 pTaskState->PDMScsiRequest.cbCDB = pTaskState->CommandControlBlockGuest.cbCDB;
2099 pTaskState->PDMScsiRequest.pbCDB = pTaskState->CommandControlBlockGuest.aCDB;
2100 if (pTaskState->DataSeg.cbSeg)
2101 {
2102 pTaskState->PDMScsiRequest.cbScatterGather = pTaskState->DataSeg.cbSeg;
2103 pTaskState->PDMScsiRequest.cScatterGatherEntries = 1;
2104 pTaskState->PDMScsiRequest.paScatterGatherHead = &pTaskState->DataSeg;
2105 }
2106 else
2107 {
2108 pTaskState->PDMScsiRequest.cbScatterGather = 0;
2109 pTaskState->PDMScsiRequest.cScatterGatherEntries = 0;
2110 pTaskState->PDMScsiRequest.paScatterGatherHead = NULL;
2111 }
2112 pTaskState->PDMScsiRequest.cbSenseBuffer = pTaskState->CommandControlBlockGuest.cbSenseData;
2113 pTaskState->PDMScsiRequest.pbSenseBuffer = pTaskState->pbSenseBuffer;
2114 pTaskState->PDMScsiRequest.pvUser = pTaskState;
2115
2116 LogFlowFunc(("before increment %u\n", pTargetDevice->cOutstandingRequests));
2117 ASMAtomicIncU32(&pTargetDevice->cOutstandingRequests);
2118 LogFlowFunc(("after increment %u\n", pTargetDevice->cOutstandingRequests));
2119 rc = pTargetDevice->pDrvSCSIConnector->pfnSCSIRequestSend(pTargetDevice->pDrvSCSIConnector, &pTaskState->PDMScsiRequest);
2120 AssertMsgRC(rc, ("Sending request to SCSI layer failed rc=%Rrc\n", rc));
2121 }
2122 }
2123 else if (pTaskState->MailboxGuest.u.out.uActionCode == BUSLOGIC_MAILBOX_OUTGOING_ACTION_ABORT_COMMAND)
2124 {
2125 AssertMsgFailed(("Not implemented yet\n"));
2126 }
2127 else
2128 AssertMsgFailed(("Invalid outgoing mailbox action code %u\n", pTaskState->MailboxGuest.u.out.uActionCode));
2129
2130 /* We got the mailbox, mark it as free in the guest. */
2131 pTaskState->MailboxGuest.u.out.uActionCode = BUSLOGIC_MAILBOX_OUTGOING_ACTION_FREE;
2132 PDMDevHlpPhysWrite(pBusLogic->CTX_SUFF(pDevIns), GCPhysAddrMailboxCurrent, &pTaskState->MailboxGuest, sizeof(Mailbox));
2133
2134 if (pBusLogic->fStrictRoundRobinMode)
2135 {
2136 pBusLogic->uMailboxOutgoingPositionCurrent++;
2137
2138 /* Check if we reached the end and start from the beginning if so. */
2139 if (pBusLogic->uMailboxOutgoingPositionCurrent >= pBusLogic->cMailbox)
2140 pBusLogic->uMailboxOutgoingPositionCurrent = 0;
2141 }
2142
2143 return rc;
2144}
2145
2146/**
2147 * Transmit queue consumer
2148 * Queue a new async task.
2149 *
2150 * @returns Success indicator.
2151 * If false the item will not be removed and the flushing will stop.
2152 * @param pDevIns The device instance.
2153 * @param pItem The item to consume. Upon return this item will be freed.
2154 */
2155static DECLCALLBACK(bool) buslogicNotifyQueueConsumer(PPDMDEVINS pDevIns, PPDMQUEUEITEMCORE pItem)
2156{
2157 PBUSLOGIC pBusLogic = PDMINS_2_DATA(pDevIns, PBUSLOGIC);
2158
2159 AssertMsg(pBusLogic->cMailboxesReady > 0, ("Got notification without any mailboxes ready\n"));
2160
2161 /* Reset notification send flag now. */
2162 ASMAtomicXchgBool(&pBusLogic->fNotificationSend, false);
2163
2164 /* Process mailboxes. */
2165 do
2166 {
2167 int rc;
2168
2169 rc = buslogicProcessMailboxNext(pBusLogic);
2170 AssertMsgRC(rc, ("Processing mailbox failed rc=%Rrc\n", rc));
2171 } while (ASMAtomicDecU32(&pBusLogic->cMailboxesReady) > 0);
2172
2173 return true;
2174}
2175
2176static DECLCALLBACK(int) buslogicLiveExec(PPDMDEVINS pDevIns, PSSMHANDLE pSSM, uint32_t uPass)
2177{
2178 PBUSLOGIC pThis = PDMINS_2_DATA(pDevIns, PBUSLOGIC);
2179
2180 /* Save the device config. */
2181 for (unsigned i = 0; i < RT_ELEMENTS(pThis->aDeviceStates); i++)
2182 SSMR3PutBool(pSSM, pThis->aDeviceStates[i].fPresent);
2183
2184 return VINF_SSM_DONT_CALL_AGAIN;
2185}
2186
2187static DECLCALLBACK(int) buslogicSaveExec(PPDMDEVINS pDevIns, PSSMHANDLE pSSM)
2188{
2189 PBUSLOGIC pBusLogic = PDMINS_2_DATA(pDevIns, PBUSLOGIC);
2190
2191 /* Every device first. */
2192 for (unsigned i = 0; i < RT_ELEMENTS(pBusLogic->aDeviceStates); i++)
2193 {
2194 PBUSLOGICDEVICE pDevice = &pBusLogic->aDeviceStates[i];
2195
2196 AssertMsg(!pDevice->cOutstandingRequests,
2197 ("There are still outstanding requests on this device\n"));
2198 SSMR3PutBool(pSSM, pDevice->fPresent);
2199 SSMR3PutU32(pSSM, pDevice->cOutstandingRequests);
2200 }
2201 /* Now the main device state. */
2202 SSMR3PutU8 (pSSM, pBusLogic->regStatus);
2203 SSMR3PutU8 (pSSM, pBusLogic->regInterrupt);
2204 SSMR3PutU8 (pSSM, pBusLogic->regGeometry);
2205 SSMR3PutMem (pSSM, &pBusLogic->LocalRam, sizeof(pBusLogic->LocalRam));
2206 SSMR3PutU8 (pSSM, pBusLogic->uOperationCode);
2207 SSMR3PutMem (pSSM, &pBusLogic->aCommandBuffer, sizeof(pBusLogic->aCommandBuffer));
2208 SSMR3PutU8 (pSSM, pBusLogic->iParameter);
2209 SSMR3PutU8 (pSSM, pBusLogic->cbCommandParametersLeft);
2210 SSMR3PutBool (pSSM, pBusLogic->fUseLocalRam);
2211 SSMR3PutMem (pSSM, pBusLogic->aReplyBuffer, sizeof(pBusLogic->aReplyBuffer));
2212 SSMR3PutU8 (pSSM, pBusLogic->iReply);
2213 SSMR3PutU8 (pSSM, pBusLogic->cbReplyParametersLeft);
2214 SSMR3PutBool (pSSM, pBusLogic->fIRQEnabled);
2215 SSMR3PutBool (pSSM, pBusLogic->fISAEnabled);
2216 SSMR3PutU32 (pSSM, pBusLogic->cMailbox);
2217 SSMR3PutGCPhys(pSSM, pBusLogic->GCPhysAddrMailboxOutgoingBase);
2218 SSMR3PutU32 (pSSM, pBusLogic->uMailboxOutgoingPositionCurrent);
2219 SSMR3PutU32 (pSSM, pBusLogic->cMailboxesReady);
2220 SSMR3PutBool (pSSM, pBusLogic->fNotificationSend);
2221 SSMR3PutGCPhys(pSSM, pBusLogic->GCPhysAddrMailboxIncomingBase);
2222 SSMR3PutU32 (pSSM, pBusLogic->uMailboxIncomingPositionCurrent);
2223 SSMR3PutBool (pSSM, pBusLogic->fStrictRoundRobinMode);
2224 SSMR3PutBool (pSSM, pBusLogic->fExtendedLunCCBFormat);
2225 /* Now the data for the BIOS interface. */
2226 SSMR3PutU8 (pSSM, pBusLogic->VBoxSCSI.regIdentify);
2227 SSMR3PutU8 (pSSM, pBusLogic->VBoxSCSI.uTargetDevice);
2228 SSMR3PutU8 (pSSM, pBusLogic->VBoxSCSI.uTxDir);
2229 SSMR3PutU8 (pSSM, pBusLogic->VBoxSCSI.cbCDB);
2230 SSMR3PutMem (pSSM, pBusLogic->VBoxSCSI.aCDB, sizeof(pBusLogic->VBoxSCSI.aCDB));
2231 SSMR3PutU8 (pSSM, pBusLogic->VBoxSCSI.iCDB);
2232 SSMR3PutU32 (pSSM, pBusLogic->VBoxSCSI.cbBuf);
2233 SSMR3PutU32 (pSSM, pBusLogic->VBoxSCSI.iBuf);
2234 SSMR3PutBool (pSSM, pBusLogic->VBoxSCSI.fBusy);
2235 SSMR3PutU8 (pSSM, pBusLogic->VBoxSCSI.enmState);
2236 if (pBusLogic->VBoxSCSI.cbCDB)
2237 SSMR3PutMem(pSSM, pBusLogic->VBoxSCSI.pBuf, pBusLogic->VBoxSCSI.cbBuf);
2238
2239 return SSMR3PutU32(pSSM, ~0);
2240}
2241
2242static DECLCALLBACK(int) buslogicLoadExec(PPDMDEVINS pDevIns, PSSMHANDLE pSSM, uint32_t uVersion, uint32_t uPass)
2243{
2244 PBUSLOGIC pBusLogic = PDMINS_2_DATA(pDevIns, PBUSLOGIC);
2245 int rc;
2246
2247 /* We support saved states only from this and older versions. */
2248 if (uVersion > BUSLOGIC_SAVED_STATE_MINOR_VERSION)
2249 return VERR_SSM_UNSUPPORTED_DATA_UNIT_VERSION;
2250
2251 /* Every device first. */
2252 for (unsigned i = 0; i < RT_ELEMENTS(pBusLogic->aDeviceStates); i++)
2253 {
2254 PBUSLOGICDEVICE pDevice = &pBusLogic->aDeviceStates[i];
2255
2256 AssertMsg(!pDevice->cOutstandingRequests,
2257 ("There are still outstanding requests on this device\n"));
2258 bool fPresent;
2259 rc = SSMR3GetBool(pSSM, &fPresent);
2260 AssertRCReturn(rc, rc);
2261 if (pDevice->fPresent != fPresent)
2262 return SSMR3SetCfgError(pSSM, RT_SRC_POS, N_("Target %u config mismatch: config=%RTbool state=%RTbool"), i, pDevice->fPresent, fPresent);
2263
2264 if (uPass == SSM_PASS_FINAL)
2265 SSMR3GetU32(pSSM, (uint32_t *)&pDevice->cOutstandingRequests);
2266 }
2267
2268 if (uPass != SSM_PASS_FINAL)
2269 return VINF_SUCCESS;
2270
2271 /* Now the main device state. */
2272 SSMR3GetU8 (pSSM, (uint8_t *)&pBusLogic->regStatus);
2273 SSMR3GetU8 (pSSM, (uint8_t *)&pBusLogic->regInterrupt);
2274 SSMR3GetU8 (pSSM, (uint8_t *)&pBusLogic->regGeometry);
2275 SSMR3GetMem (pSSM, &pBusLogic->LocalRam, sizeof(pBusLogic->LocalRam));
2276 SSMR3GetU8 (pSSM, &pBusLogic->uOperationCode);
2277 SSMR3GetMem (pSSM, &pBusLogic->aCommandBuffer, sizeof(pBusLogic->aCommandBuffer));
2278 SSMR3GetU8 (pSSM, &pBusLogic->iParameter);
2279 SSMR3GetU8 (pSSM, &pBusLogic->cbCommandParametersLeft);
2280 SSMR3GetBool (pSSM, &pBusLogic->fUseLocalRam);
2281 SSMR3GetMem (pSSM, pBusLogic->aReplyBuffer, sizeof(pBusLogic->aReplyBuffer));
2282 SSMR3GetU8 (pSSM, &pBusLogic->iReply);
2283 SSMR3GetU8 (pSSM, &pBusLogic->cbReplyParametersLeft);
2284 SSMR3GetBool (pSSM, &pBusLogic->fIRQEnabled);
2285 SSMR3GetBool (pSSM, &pBusLogic->fISAEnabled);
2286 SSMR3GetU32 (pSSM, &pBusLogic->cMailbox);
2287 SSMR3GetGCPhys(pSSM, &pBusLogic->GCPhysAddrMailboxOutgoingBase);
2288 SSMR3GetU32 (pSSM, &pBusLogic->uMailboxOutgoingPositionCurrent);
2289 SSMR3GetU32 (pSSM, (uint32_t *)&pBusLogic->cMailboxesReady);
2290 SSMR3GetBool (pSSM, (bool *)&pBusLogic->fNotificationSend);
2291 SSMR3GetGCPhys(pSSM, &pBusLogic->GCPhysAddrMailboxIncomingBase);
2292 SSMR3GetU32 (pSSM, &pBusLogic->uMailboxIncomingPositionCurrent);
2293 SSMR3GetBool (pSSM, &pBusLogic->fStrictRoundRobinMode);
2294 SSMR3GetBool (pSSM, &pBusLogic->fExtendedLunCCBFormat);
2295 /* Now the data for the BIOS interface. */
2296 SSMR3GetU8 (pSSM, &pBusLogic->VBoxSCSI.regIdentify);
2297 SSMR3GetU8 (pSSM, &pBusLogic->VBoxSCSI.uTargetDevice);
2298 SSMR3GetU8 (pSSM, &pBusLogic->VBoxSCSI.uTxDir);
2299 SSMR3GetU8 (pSSM, &pBusLogic->VBoxSCSI.cbCDB);
2300 SSMR3GetMem (pSSM, pBusLogic->VBoxSCSI.aCDB, sizeof(pBusLogic->VBoxSCSI.aCDB));
2301 SSMR3GetU8 (pSSM, &pBusLogic->VBoxSCSI.iCDB);
2302 SSMR3GetU32 (pSSM, &pBusLogic->VBoxSCSI.cbBuf);
2303 SSMR3GetU32 (pSSM, &pBusLogic->VBoxSCSI.iBuf);
2304 SSMR3GetBool(pSSM, (bool *)&pBusLogic->VBoxSCSI.fBusy);
2305 SSMR3GetU8 (pSSM, (uint8_t *)&pBusLogic->VBoxSCSI.enmState);
2306 if (pBusLogic->VBoxSCSI.cbCDB)
2307 {
2308 pBusLogic->VBoxSCSI.pBuf = (uint8_t *)RTMemAllocZ(pBusLogic->VBoxSCSI.cbCDB);
2309 if (!pBusLogic->VBoxSCSI.pBuf)
2310 {
2311 LogRel(("BusLogic: Out of memory during restore.\n"));
2312 return PDMDEV_SET_ERROR(pDevIns, VERR_NO_MEMORY,
2313 N_("BusLogic: Out of memory during restore\n"));
2314 }
2315 SSMR3GetMem(pSSM, pBusLogic->VBoxSCSI.pBuf, pBusLogic->VBoxSCSI.cbBuf);
2316 }
2317
2318 uint32_t u32;
2319 rc = SSMR3GetU32(pSSM, &u32);
2320 if (RT_FAILURE(rc))
2321 return rc;
2322 AssertMsgReturn(u32 == ~0U, ("%#x\n", u32), VERR_SSM_DATA_UNIT_FORMAT_CHANGED);
2323
2324 return VINF_SUCCESS;
2325}
2326
2327/**
2328 * Gets the pointer to the status LED of a device - called from the SCSi driver.
2329 *
2330 * @returns VBox status code.
2331 * @param pInterface Pointer to the interface structure containing the called function pointer.
2332 * @param iLUN The unit which status LED we desire. Always 0 here as the driver
2333 * doesn't know about other LUN's.
2334 * @param ppLed Where to store the LED pointer.
2335 */
2336static DECLCALLBACK(int) buslogicDeviceQueryStatusLed(PPDMILEDPORTS pInterface, unsigned iLUN, PPDMLED *ppLed)
2337{
2338 PBUSLOGICDEVICE pDevice = PDMILEDPORTS_2_PBUSLOGICDEVICE(pInterface);
2339 if (iLUN == 0)
2340 {
2341 *ppLed = &pDevice->Led;
2342 Assert((*ppLed)->u32Magic == PDMLED_MAGIC);
2343 return VINF_SUCCESS;
2344 }
2345 return VERR_PDM_LUN_NOT_FOUND;
2346}
2347
2348/**
2349 * @interface_method_impl{PDMIBASE,pfnQueryInterface}
2350 */
2351static DECLCALLBACK(void *) buslogicDeviceQueryInterface(PPDMIBASE pInterface, const char *pszIID)
2352{
2353 PBUSLOGICDEVICE pDevice = PDMIBASE_2_PBUSLOGICDEVICE(pInterface);
2354 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIBASE, &pDevice->IBase);
2355 PDMIBASE_RETURN_INTERFACE(pszIID, PDMISCSIPORT, &pDevice->ISCSIPort);
2356 PDMIBASE_RETURN_INTERFACE(pszIID, PDMILEDPORTS, &pDevice->ILed);
2357 return NULL;
2358}
2359
2360/**
2361 * Gets the pointer to the status LED of a unit.
2362 *
2363 * @returns VBox status code.
2364 * @param pInterface Pointer to the interface structure containing the called function pointer.
2365 * @param iLUN The unit which status LED we desire.
2366 * @param ppLed Where to store the LED pointer.
2367 */
2368static DECLCALLBACK(int) buslogicStatusQueryStatusLed(PPDMILEDPORTS pInterface, unsigned iLUN, PPDMLED *ppLed)
2369{
2370 PBUSLOGIC pBusLogic = PDMILEDPORTS_2_PBUSLOGIC(pInterface);
2371 if (iLUN < BUSLOGIC_MAX_DEVICES)
2372 {
2373 *ppLed = &pBusLogic->aDeviceStates[iLUN].Led;
2374 Assert((*ppLed)->u32Magic == PDMLED_MAGIC);
2375 return VINF_SUCCESS;
2376 }
2377 return VERR_PDM_LUN_NOT_FOUND;
2378}
2379
2380/**
2381 * @interface_method_impl{PDMIBASE,pfnQueryInterface}
2382 */
2383static DECLCALLBACK(void *) buslogicStatusQueryInterface(PPDMIBASE pInterface, const char *pszIID)
2384{
2385 PBUSLOGIC pThis = PDMIBASE_2_PBUSLOGIC(pInterface);
2386 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIBASE, &pThis->IBase);
2387 PDMIBASE_RETURN_INTERFACE(pszIID, PDMILEDPORTS, &pThis->ILeds);
2388 return NULL;
2389}
2390
2391/**
2392 * Detach notification.
2393 *
2394 * One harddisk at one port has been unplugged.
2395 * The VM is suspended at this point.
2396 *
2397 * @param pDevIns The device instance.
2398 * @param iLUN The logical unit which is being detached.
2399 * @param fFlags Flags, combination of the PDMDEVATT_FLAGS_* \#defines.
2400 */
2401static DECLCALLBACK(void) buslogicDetach(PPDMDEVINS pDevIns, unsigned iLUN, uint32_t fFlags)
2402{
2403 PBUSLOGIC pThis = PDMINS_2_DATA(pDevIns, PBUSLOGIC);
2404 PBUSLOGICDEVICE pDevice = &pThis->aDeviceStates[iLUN];
2405
2406 Log(("%s:\n", __FUNCTION__));
2407
2408 AssertMsg(fFlags & PDM_TACH_FLAGS_NOT_HOT_PLUG,
2409 ("BusLogic: Device does not support hotplugging\n"));
2410
2411 /*
2412 * Zero some important members.
2413 */
2414 pDevice->pDrvBase = NULL;
2415 pDevice->fPresent = false;
2416 pDevice->pDrvSCSIConnector = NULL;
2417}
2418
2419/**
2420 * Attach command.
2421 *
2422 * This is called when we change block driver.
2423 *
2424 * @returns VBox status code.
2425 * @param pDevIns The device instance.
2426 * @param iLUN The logical unit which is being detached.
2427 * @param fFlags Flags, combination of the PDMDEVATT_FLAGS_* \#defines.
2428 */
2429static DECLCALLBACK(int) buslogicAttach(PPDMDEVINS pDevIns, unsigned iLUN, uint32_t fFlags)
2430{
2431 PBUSLOGIC pThis = PDMINS_2_DATA(pDevIns, PBUSLOGIC);
2432 PBUSLOGICDEVICE pDevice = &pThis->aDeviceStates[iLUN];
2433 int rc;
2434
2435 AssertMsgReturn(fFlags & PDM_TACH_FLAGS_NOT_HOT_PLUG,
2436 ("BusLogic: Device does not support hotplugging\n"),
2437 VERR_INVALID_PARAMETER);
2438
2439 /* the usual paranoia */
2440 AssertRelease(!pDevice->pDrvBase);
2441 AssertRelease(!pDevice->pDrvSCSIConnector);
2442 Assert(pDevice->iLUN == iLUN);
2443
2444 /*
2445 * Try attach the block device and get the interfaces,
2446 * required as well as optional.
2447 */
2448 rc = PDMDevHlpDriverAttach(pDevIns, pDevice->iLUN, &pDevice->IBase, &pDevice->pDrvBase, NULL);
2449 if (RT_SUCCESS(rc))
2450 {
2451 /* Get SCSI connector interface. */
2452 pDevice->pDrvSCSIConnector = PDMIBASE_QUERY_INTERFACE(pDevice->pDrvBase, PDMISCSICONNECTOR);
2453 AssertMsgReturn(pDevice->pDrvSCSIConnector, ("Missing SCSI interface below\n"), VERR_PDM_MISSING_INTERFACE);
2454 pDevice->fPresent = true;
2455 }
2456 else
2457 AssertMsgFailed(("Failed to attach LUN#%d. rc=%Rrc\n", pDevice->iLUN, rc));
2458
2459 if (RT_FAILURE(rc))
2460 {
2461 pDevice->pDrvBase = NULL;
2462 pDevice->pDrvSCSIConnector = NULL;
2463 }
2464 return rc;
2465}
2466
2467static DECLCALLBACK(void) buslogicRelocate(PPDMDEVINS pDevIns, RTGCINTPTR offDelta)
2468{
2469 uint32_t i;
2470 PBUSLOGIC pBusLogic = PDMINS_2_DATA(pDevIns, PBUSLOGIC);
2471
2472 pBusLogic->pDevInsRC = PDMDEVINS_2_RCPTR(pDevIns);
2473 pBusLogic->pNotifierQueueRC = PDMQueueRCPtr(pBusLogic->pNotifierQueueR3);
2474
2475 for (i = 0; i < BUSLOGIC_MAX_DEVICES; i++)
2476 {
2477 PBUSLOGICDEVICE pDevice = &pBusLogic->aDeviceStates[i];
2478
2479 pDevice->pBusLogicRC = PDMINS_2_DATA_RCPTR(pDevIns);
2480 }
2481
2482}
2483
2484/**
2485 * Reset notification.
2486 *
2487 * @returns VBox status.
2488 * @param pDevIns The device instance data.
2489 */
2490static DECLCALLBACK(void) buslogicReset(PPDMDEVINS pDevIns)
2491{
2492 PBUSLOGIC pThis = PDMINS_2_DATA(pDevIns, PBUSLOGIC);
2493
2494 buslogicHwReset(pThis);
2495}
2496
2497/**
2498 * Destroy a driver instance.
2499 *
2500 * Most VM resources are freed by the VM. This callback is provided so that any non-VM
2501 * resources can be freed correctly.
2502 *
2503 * @param pDevIns The device instance data.
2504 */
2505static DECLCALLBACK(int) buslogicDestruct(PPDMDEVINS pDevIns)
2506{
2507 PBUSLOGIC pThis = PDMINS_2_DATA(pDevIns, PBUSLOGIC);
2508 PDMDEV_CHECK_VERSIONS_RETURN_QUIET(pDevIns);
2509
2510 int rc = RTMemCacheDestroy(pThis->hTaskCache);
2511 AssertMsgRC(rc, ("Destroying task cache failed rc=%Rrc\n", rc));
2512
2513 return rc;
2514}
2515
2516/**
2517 * @interface_method_impl{PDMDEVREG,pfnConstruct}
2518 */
2519static DECLCALLBACK(int) buslogicConstruct(PPDMDEVINS pDevIns, int iInstance, PCFGMNODE pCfg)
2520{
2521 PBUSLOGIC pThis = PDMINS_2_DATA(pDevIns, PBUSLOGIC);
2522 int rc = VINF_SUCCESS;
2523 PDMDEV_CHECK_VERSIONS_RETURN(pDevIns);
2524
2525 /*
2526 * Validate and read configuration.
2527 */
2528 if (!CFGMR3AreValuesValid(pCfg,
2529 "GCEnabled\0"
2530 "R0Enabled\0"))
2531 return PDMDEV_SET_ERROR(pDevIns, VERR_PDM_DEVINS_UNKNOWN_CFG_VALUES,
2532 N_("BusLogic configuration error: unknown option specified"));
2533
2534 rc = CFGMR3QueryBoolDef(pCfg, "GCEnabled", &pThis->fGCEnabled, true);
2535 if (RT_FAILURE(rc))
2536 return PDMDEV_SET_ERROR(pDevIns, rc,
2537 N_("BusLogic configuration error: failed to read GCEnabled as boolean"));
2538 Log(("%s: fGCEnabled=%d\n", __FUNCTION__, pThis->fGCEnabled));
2539
2540 rc = CFGMR3QueryBoolDef(pCfg, "R0Enabled", &pThis->fR0Enabled, true);
2541 if (RT_FAILURE(rc))
2542 return PDMDEV_SET_ERROR(pDevIns, rc,
2543 N_("BusLogic configuration error: failed to read R0Enabled as boolean"));
2544 Log(("%s: fR0Enabled=%d\n", __FUNCTION__, pThis->fR0Enabled));
2545
2546
2547 pThis->pDevInsR3 = pDevIns;
2548 pThis->pDevInsR0 = PDMDEVINS_2_R0PTR(pDevIns);
2549 pThis->pDevInsRC = PDMDEVINS_2_RCPTR(pDevIns);
2550 pThis->IBase.pfnQueryInterface = buslogicStatusQueryInterface;
2551 pThis->ILeds.pfnQueryStatusLed = buslogicStatusQueryStatusLed;
2552
2553 PCIDevSetVendorId (&pThis->dev, 0x104b); /* BusLogic */
2554 PCIDevSetDeviceId (&pThis->dev, 0x1040); /* BT-958 */
2555 PCIDevSetCommand (&pThis->dev, 0x0003);
2556 PCIDevSetRevisionId (&pThis->dev, 0x01);
2557 PCIDevSetClassProg (&pThis->dev, 0x00); /* SCSI */
2558 PCIDevSetClassSub (&pThis->dev, 0x00); /* SCSI */
2559 PCIDevSetClassBase (&pThis->dev, 0x01); /* Mass storage */
2560 PCIDevSetBaseAddress (&pThis->dev, 0, true /*IO*/, false /*Pref*/, false /*64-bit*/, 0x00000000);
2561 PCIDevSetBaseAddress (&pThis->dev, 1, false /*IO*/, false /*Pref*/, false /*64-bit*/, 0x00000000);
2562 PCIDevSetSubSystemVendorId(&pThis->dev, 0x104b);
2563 PCIDevSetSubSystemId (&pThis->dev, 0x1040);
2564 PCIDevSetInterruptLine (&pThis->dev, 0x00);
2565 PCIDevSetInterruptPin (&pThis->dev, 0x01);
2566
2567 /*
2568 * Register the PCI device, it's I/O regions.
2569 */
2570 rc = PDMDevHlpPCIRegister (pDevIns, &pThis->dev);
2571 if (RT_FAILURE(rc))
2572 return rc;
2573
2574 rc = PDMDevHlpPCIIORegionRegister(pDevIns, 0, 32, PCI_ADDRESS_SPACE_IO, buslogicMMIOMap);
2575 if (RT_FAILURE(rc))
2576 return rc;
2577
2578 rc = PDMDevHlpPCIIORegionRegister(pDevIns, 1, 32, PCI_ADDRESS_SPACE_MEM, buslogicMMIOMap);
2579 if (RT_FAILURE(rc))
2580 return rc;
2581
2582 /* Register I/O port space in ISA region for BIOS access. */
2583 rc = PDMDevHlpIOPortRegister(pDevIns, BUSLOGIC_ISA_IO_PORT, 3, NULL,
2584 buslogicIsaIOPortWrite, buslogicIsaIOPortRead,
2585 buslogicIsaIOPortWriteStr, buslogicIsaIOPortReadStr,
2586 "BusLogic BIOS");
2587 if (RT_FAILURE(rc))
2588 return PDMDEV_SET_ERROR(pDevIns, rc, N_("BusLogic cannot register legacy I/O handlers"));
2589
2590 /* Initialize task cache. */
2591 rc = RTMemCacheCreate(&pThis->hTaskCache, sizeof(BUSLOGICTASKSTATE), 0, UINT32_MAX,
2592 NULL, NULL, NULL, 0);
2593 if (RT_FAILURE(rc))
2594 return PDMDEV_SET_ERROR(pDevIns, rc,
2595 N_("BusLogic: Failed to initialize task cache\n"));
2596
2597 /* Intialize task queue. */
2598 rc = PDMDevHlpQueueCreate(pDevIns, sizeof(PDMQUEUEITEMCORE), 5, 0,
2599 buslogicNotifyQueueConsumer, true, "BugLogicTask", &pThis->pNotifierQueueR3);
2600 if (RT_FAILURE(rc))
2601 return rc;
2602 pThis->pNotifierQueueR0 = PDMQueueR0Ptr(pThis->pNotifierQueueR3);
2603 pThis->pNotifierQueueRC = PDMQueueRCPtr(pThis->pNotifierQueueR3);
2604
2605 /* Initialize per device state. */
2606 for (unsigned i = 0; i < RT_ELEMENTS(pThis->aDeviceStates); i++)
2607 {
2608 char szName[24];
2609 PBUSLOGICDEVICE pDevice = &pThis->aDeviceStates[i];
2610
2611 RTStrPrintf(szName, sizeof(szName), "Device%d", i);
2612
2613 /* Initialize static parts of the device. */
2614 pDevice->iLUN = i;
2615 pDevice->pBusLogicR3 = pThis;
2616 pDevice->pBusLogicR0 = PDMINS_2_DATA_R0PTR(pDevIns);
2617 pDevice->pBusLogicRC = PDMINS_2_DATA_RCPTR(pDevIns);
2618 pDevice->Led.u32Magic = PDMLED_MAGIC;
2619 pDevice->IBase.pfnQueryInterface = buslogicDeviceQueryInterface;
2620 pDevice->ISCSIPort.pfnSCSIRequestCompleted = buslogicDeviceSCSIRequestCompleted;
2621 pDevice->ILed.pfnQueryStatusLed = buslogicDeviceQueryStatusLed;
2622
2623 /* Attach SCSI driver. */
2624 rc = PDMDevHlpDriverAttach(pDevIns, pDevice->iLUN, &pDevice->IBase, &pDevice->pDrvBase, szName);
2625 if (RT_SUCCESS(rc))
2626 {
2627 /* Get SCSI connector interface. */
2628 pDevice->pDrvSCSIConnector = PDMIBASE_QUERY_INTERFACE(pDevice->pDrvBase, PDMISCSICONNECTOR);
2629 AssertMsgReturn(pDevice->pDrvSCSIConnector, ("Missing SCSI interface below\n"), VERR_PDM_MISSING_INTERFACE);
2630
2631 pDevice->fPresent = true;
2632 }
2633 else if (rc == VERR_PDM_NO_ATTACHED_DRIVER)
2634 {
2635 pDevice->pDrvBase = NULL;
2636 pDevice->fPresent = false;
2637 rc = VINF_SUCCESS;
2638 Log(("BusLogic: no driver attached to device %s\n", szName));
2639 }
2640 else
2641 {
2642 AssertLogRelMsgFailed(("BusLogic: Failed to attach %s\n", szName));
2643 return rc;
2644 }
2645 }
2646
2647 /*
2648 * Attach status driver (optional).
2649 */
2650 PPDMIBASE pBase;
2651 rc = PDMDevHlpDriverAttach(pDevIns, PDM_STATUS_LUN, &pThis->IBase, &pBase, "Status Port");
2652 if (RT_SUCCESS(rc))
2653 pThis->pLedsConnector = PDMIBASE_QUERY_INTERFACE(pBase, PDMILEDCONNECTORS);
2654 else if (rc != VERR_PDM_NO_ATTACHED_DRIVER)
2655 {
2656 AssertMsgFailed(("Failed to attach to status driver. rc=%Rrc\n", rc));
2657 return PDMDEV_SET_ERROR(pDevIns, rc, N_("BusLogic cannot attach to status driver"));
2658 }
2659
2660 rc = PDMDevHlpSSMRegister3(pDevIns, BUSLOGIC_SAVED_STATE_MINOR_VERSION, sizeof(*pThis),
2661 buslogicLiveExec, buslogicSaveExec, buslogicLoadExec);
2662 if (RT_FAILURE(rc))
2663 return PDMDEV_SET_ERROR(pDevIns, rc, N_("BusLogic cannot register save state handlers"));
2664
2665 rc = buslogicHwReset(pThis);
2666 AssertMsgRC(rc, ("hardware reset of BusLogic host adapter failed rc=%Rrc\n", rc));
2667
2668 return rc;
2669}
2670
2671/**
2672 * The device registration structure.
2673 */
2674const PDMDEVREG g_DeviceBusLogic =
2675{
2676 /* u32Version */
2677 PDM_DEVREG_VERSION,
2678 /* szName */
2679 "buslogic",
2680 /* szRCMod */
2681 "VBoxDDGC.gc",
2682 /* szR0Mod */
2683 "VBoxDDR0.r0",
2684 /* pszDescription */
2685 "BusLogic BT-958 SCSI host adapter.\n",
2686 /* fFlags */
2687 PDM_DEVREG_FLAGS_DEFAULT_BITS | PDM_DEVREG_FLAGS_RC | PDM_DEVREG_FLAGS_R0,
2688 /* fClass */
2689 PDM_DEVREG_CLASS_STORAGE,
2690 /* cMaxInstances */
2691 ~0,
2692 /* cbInstance */
2693 sizeof(BUSLOGIC),
2694 /* pfnConstruct */
2695 buslogicConstruct,
2696 /* pfnDestruct */
2697 buslogicDestruct,
2698 /* pfnRelocate */
2699 buslogicRelocate,
2700 /* pfnIOCtl */
2701 NULL,
2702 /* pfnPowerOn */
2703 NULL,
2704 /* pfnReset */
2705 buslogicReset,
2706 /* pfnSuspend */
2707 NULL,
2708 /* pfnResume */
2709 NULL,
2710 /* pfnAttach */
2711 buslogicAttach,
2712 /* pfnDetach */
2713 buslogicDetach,
2714 /* pfnQueryInterface. */
2715 NULL,
2716 /* pfnInitComplete */
2717 NULL,
2718 /* pfnPowerOff */
2719 NULL,
2720 /* pfnSoftReset */
2721 NULL,
2722 /* u32VersionEnd */
2723 PDM_DEVREG_VERSION
2724};
2725
2726#endif /* IN_RING3 */
2727#endif /* !VBOX_DEVICE_STRUCT_TESTCASE */
2728
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