VirtualBox

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

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

Relevant PDMDevHlpPhysWrite changed to PDMDevHlpPCIPhysWrite. If this breaks anything, add PDM_DO_NOT_RESPECT_PCI_BM_BIT to VMM_COMMON_DEFS to disable the feature.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 157.1 KB
Line 
1/* $Id: DevBusLogic.cpp 44902 2013-03-02 02:28:37Z vboxsync $ */
2/** @file
3 * VBox storage devices - BusLogic SCSI host adapter BT-958.
4 *
5 * Based on the Multi-Master Ultra SCSI Systems Technical Reference Manual.
6 */
7
8/*
9 * Copyright (C) 2006-2013 Oracle Corporation
10 *
11 * This file is part of VirtualBox Open Source Edition (OSE), as
12 * available from http://www.virtualbox.org. This file is free software;
13 * you can redistribute it and/or modify it under the terms of the GNU
14 * General Public License (GPL) as published by the Free Software
15 * Foundation, in version 2 as it comes in the "COPYING" file of the
16 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
17 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
18 */
19
20
21/*******************************************************************************
22* Header Files *
23*******************************************************************************/
24#define LOG_GROUP LOG_GROUP_DEV_BUSLOGIC
25#include <VBox/vmm/pdmdev.h>
26#include <VBox/vmm/pdmifs.h>
27#include <VBox/vmm/pdmcritsect.h>
28#include <VBox/scsi.h>
29#include <iprt/asm.h>
30#include <iprt/assert.h>
31#include <iprt/string.h>
32#include <iprt/log.h>
33#ifdef IN_RING3
34# include <iprt/alloc.h>
35# include <iprt/memcache.h>
36# include <iprt/param.h>
37# include <iprt/uuid.h>
38#endif
39
40#include "VBoxSCSI.h"
41#include "VBoxDD.h"
42
43
44/*******************************************************************************
45* Defined Constants And Macros *
46*******************************************************************************/
47/** Maximum number of attached devices the adapter can handle. */
48#define BUSLOGIC_MAX_DEVICES 16
49
50/** Maximum number of scatter gather elements this device can handle. */
51#define BUSLOGIC_MAX_SCATTER_GATHER_LIST_SIZE 128
52
53/** Size of the command buffer. */
54#define BUSLOGIC_COMMAND_SIZE_MAX 5
55
56/** Size of the reply buffer. */
57#define BUSLOGIC_REPLY_SIZE_MAX 64
58
59/** Custom fixed I/O ports for BIOS controller access.
60 * Note that these should not be in the ISA range (below 400h) to avoid
61 * conflicts with ISA device probing. Addresses in the 300h-340h range should be
62 * especially avoided.
63 */
64#define BUSLOGIC_BIOS_IO_PORT 0x430
65
66/** State saved version. */
67#define BUSLOGIC_SAVED_STATE_MINOR_VERSION 3
68
69/** Saved state version before the suspend on error feature was implemented. */
70#define BUSLOGIC_SAVED_STATE_MINOR_PRE_ERROR_HANDLING 1
71/** Saved state version before 24-bit mailbox support was implemented. */
72#define BUSLOGIC_SAVED_STATE_MINOR_PRE_24BIT_MBOX 2
73
74/** The duration of software-initiated reset (in nano seconds).
75 * Not documented, set to 50 ms. */
76#define BUSLOGIC_RESET_DURATION_NS UINT64_C(50000000)
77
78
79/*******************************************************************************
80* Structures and Typedefs *
81*******************************************************************************/
82/**
83 * State of a device attached to the buslogic host adapter.
84 *
85 * @implements PDMIBASE
86 * @implements PDMISCSIPORT
87 * @implements PDMILEDPORTS
88 */
89typedef struct BUSLOGICDEVICE
90{
91 /** Pointer to the owning buslogic device instance. - R3 pointer */
92 R3PTRTYPE(struct BUSLOGIC *) pBusLogicR3;
93 /** Pointer to the owning buslogic device instance. - R0 pointer */
94 R0PTRTYPE(struct BUSLOGIC *) pBusLogicR0;
95 /** Pointer to the owning buslogic device instance. - RC pointer */
96 RCPTRTYPE(struct BUSLOGIC *) pBusLogicRC;
97
98 /** Flag whether device is present. */
99 bool fPresent;
100 /** LUN of the device. */
101 RTUINT iLUN;
102
103#if HC_ARCH_BITS == 64
104 uint32_t Alignment0;
105#endif
106
107 /** Our base interface. */
108 PDMIBASE IBase;
109 /** SCSI port interface. */
110 PDMISCSIPORT ISCSIPort;
111 /** Led interface. */
112 PDMILEDPORTS ILed;
113 /** Pointer to the attached driver's base interface. */
114 R3PTRTYPE(PPDMIBASE) pDrvBase;
115 /** Pointer to the underlying SCSI connector interface. */
116 R3PTRTYPE(PPDMISCSICONNECTOR) pDrvSCSIConnector;
117 /** The status LED state for this device. */
118 PDMLED Led;
119
120#if HC_ARCH_BITS == 64
121 uint32_t Alignment1;
122#endif
123
124 /** Number of outstanding tasks on the port. */
125 volatile uint32_t cOutstandingRequests;
126
127} BUSLOGICDEVICE, *PBUSLOGICDEVICE;
128
129/**
130 * Commands the BusLogic adapter supports.
131 */
132enum BUSLOGICCOMMAND
133{
134 BUSLOGICCOMMAND_TEST_CMDC_INTERRUPT = 0x00,
135 BUSLOGICCOMMAND_INITIALIZE_MAILBOX = 0x01,
136 BUSLOGICCOMMAND_EXECUTE_MAILBOX_COMMAND = 0x02,
137 BUSLOGICCOMMAND_EXECUTE_BIOS_COMMAND = 0x03,
138 BUSLOGICCOMMAND_INQUIRE_BOARD_ID = 0x04,
139 BUSLOGICCOMMAND_ENABLE_OUTGOING_MAILBOX_AVAILABLE_INTERRUPT = 0x05,
140 BUSLOGICCOMMAND_SET_SCSI_SELECTION_TIMEOUT = 0x06,
141 BUSLOGICCOMMAND_SET_PREEMPT_TIME_ON_BUS = 0x07,
142 BUSLOGICCOMMAND_SET_TIME_OFF_BUS = 0x08,
143 BUSLOGICCOMMAND_SET_BUS_TRANSFER_RATE = 0x09,
144 BUSLOGICCOMMAND_INQUIRE_INSTALLED_DEVICES_ID_0_TO_7 = 0x0a,
145 BUSLOGICCOMMAND_INQUIRE_CONFIGURATION = 0x0b,
146 BUSLOGICCOMMAND_ENABLE_TARGET_MODE = 0x0c,
147 BUSLOGICCOMMAND_INQUIRE_SETUP_INFORMATION = 0x0d,
148 BUSLOGICCOMMAND_WRITE_ADAPTER_LOCAL_RAM = 0x1a,
149 BUSLOGICCOMMAND_READ_ADAPTER_LOCAL_RAM = 0x1b,
150 BUSLOGICCOMMAND_WRITE_BUSMASTER_CHIP_FIFO = 0x1c,
151 BUSLOGICCOMMAND_READ_BUSMASTER_CHIP_FIFO = 0x1d,
152 BUSLOGICCOMMAND_ECHO_COMMAND_DATA = 0x1f,
153 BUSLOGICCOMMAND_HOST_ADAPTER_DIAGNOSTIC = 0x20,
154 BUSLOGICCOMMAND_SET_ADAPTER_OPTIONS = 0x21,
155 BUSLOGICCOMMAND_INQUIRE_INSTALLED_DEVICES_ID_8_TO_15 = 0x23,
156 BUSLOGICCOMMAND_INQUIRE_TARGET_DEVICES = 0x24,
157 BUSLOGICCOMMAND_DISABLE_HOST_ADAPTER_INTERRUPT = 0x25,
158 BUSLOGICCOMMAND_EXT_BIOS_INFO = 0x28,
159 BUSLOGICCOMMAND_UNLOCK_MAILBOX = 0x29,
160 BUSLOGICCOMMAND_INITIALIZE_EXTENDED_MAILBOX = 0x81,
161 BUSLOGICCOMMAND_EXECUTE_SCSI_COMMAND = 0x83,
162 BUSLOGICCOMMAND_INQUIRE_FIRMWARE_VERSION_3RD_LETTER = 0x84,
163 BUSLOGICCOMMAND_INQUIRE_FIRMWARE_VERSION_LETTER = 0x85,
164 BUSLOGICCOMMAND_INQUIRE_PCI_HOST_ADAPTER_INFORMATION = 0x86,
165 BUSLOGICCOMMAND_INQUIRE_HOST_ADAPTER_MODEL_NUMBER = 0x8b,
166 BUSLOGICCOMMAND_INQUIRE_SYNCHRONOUS_PERIOD = 0x8c,
167 BUSLOGICCOMMAND_INQUIRE_EXTENDED_SETUP_INFORMATION = 0x8d,
168 BUSLOGICCOMMAND_ENABLE_STRICT_ROUND_ROBIN_MODE = 0x8f,
169 BUSLOGICCOMMAND_STORE_HOST_ADAPTER_LOCAL_RAM = 0x90,
170 BUSLOGICCOMMAND_FETCH_HOST_ADAPTER_LOCAL_RAM = 0x91,
171 BUSLOGICCOMMAND_STORE_LOCAL_DATA_IN_EEPROM = 0x92,
172 BUSLOGICCOMMAND_UPLOAD_AUTO_SCSI_CODE = 0x94,
173 BUSLOGICCOMMAND_MODIFY_IO_ADDRESS = 0x95,
174 BUSLOGICCOMMAND_SET_CCB_FORMAT = 0x96,
175 BUSLOGICCOMMAND_WRITE_INQUIRY_BUFFER = 0x9a,
176 BUSLOGICCOMMAND_READ_INQUIRY_BUFFER = 0x9b,
177 BUSLOGICCOMMAND_FLASH_ROM_UPLOAD_DOWNLOAD = 0xa7,
178 BUSLOGICCOMMAND_READ_SCAM_DATA = 0xa8,
179 BUSLOGICCOMMAND_WRITE_SCAM_DATA = 0xa9
180} BUSLOGICCOMMAND;
181
182#pragma pack(1)
183/**
184 * Auto SCSI structure which is located
185 * in host adapter RAM and contains several
186 * configuration parameters.
187 */
188typedef struct AutoSCSIRam
189{
190 uint8_t aInternalSignature[2];
191 uint8_t cbInformation;
192 uint8_t aHostAdaptertype[6];
193 uint8_t uReserved1;
194 bool fFloppyEnabled : 1;
195 bool fFloppySecondary : 1;
196 bool fLevelSensitiveInterrupt : 1;
197 unsigned char uReserved2 : 2;
198 unsigned char uSystemRAMAreForBIOS : 3;
199 unsigned char uDMAChannel : 7;
200 bool fDMAAutoConfiguration : 1;
201 unsigned char uIrqChannel : 7;
202 bool fIrqAutoConfiguration : 1;
203 uint8_t uDMATransferRate;
204 uint8_t uSCSIId;
205 bool fLowByteTerminated : 1;
206 bool fParityCheckingEnabled : 1;
207 bool fHighByteTerminated : 1;
208 bool fNoisyCablingEnvironment : 1;
209 bool fFastSynchronousNeogtiation : 1;
210 bool fBusResetEnabled : 1;
211 bool fReserved3 : 1;
212 bool fActiveNegotiationEnabled : 1;
213 uint8_t uBusOnDelay;
214 uint8_t uBusOffDelay;
215 bool fHostAdapterBIOSEnabled : 1;
216 bool fBIOSRedirectionOfInt19 : 1;
217 bool fExtendedTranslation : 1;
218 bool fMapRemovableAsFixed : 1;
219 bool fReserved4 : 1;
220 bool fBIOSSupportsMoreThan2Drives : 1;
221 bool fBIOSInterruptMode : 1;
222 bool fFlopticalSupport : 1;
223 uint16_t u16DeviceEnabledMask;
224 uint16_t u16WidePermittedMask;
225 uint16_t u16FastPermittedMask;
226 uint16_t u16SynchronousPermittedMask;
227 uint16_t u16DisconnectPermittedMask;
228 uint16_t u16SendStartUnitCommandMask;
229 uint16_t u16IgnoreInBIOSScanMask;
230 unsigned char uPCIInterruptPin : 2;
231 unsigned char uHostAdapterIoPortAddress : 2;
232 bool fStrictRoundRobinMode : 1;
233 bool fVesaBusSpeedGreaterThan33MHz : 1;
234 bool fVesaBurstWrite : 1;
235 bool fVesaBurstRead : 1;
236 uint16_t u16UltraPermittedMask;
237 uint32_t uReserved5;
238 uint8_t uReserved6;
239 uint8_t uAutoSCSIMaximumLUN;
240 bool fReserved7 : 1;
241 bool fSCAMDominant : 1;
242 bool fSCAMenabled : 1;
243 bool fSCAMLevel2 : 1;
244 unsigned char uReserved8 : 4;
245 bool fInt13Extension : 1;
246 bool fReserved9 : 1;
247 bool fCDROMBoot : 1;
248 unsigned char uReserved10 : 5;
249 unsigned char uBootTargetId : 4;
250 unsigned char uBootChannel : 4;
251 bool fForceBusDeviceScanningOrder : 1;
252 unsigned char uReserved11 : 7;
253 uint16_t u16NonTaggedToAlternateLunPermittedMask;
254 uint16_t u16RenegotiateSyncAfterCheckConditionMask;
255 uint8_t aReserved12[10];
256 uint8_t aManufacturingDiagnostic[2];
257 uint16_t u16Checksum;
258} AutoSCSIRam, *PAutoSCSIRam;
259AssertCompileSize(AutoSCSIRam, 64);
260#pragma pack()
261
262/**
263 * The local Ram.
264 */
265typedef union HostAdapterLocalRam
266{
267 /** Byte view. */
268 uint8_t u8View[256];
269 /** Structured view. */
270 struct
271 {
272 /** Offset 0 - 63 is for BIOS. */
273 uint8_t u8Bios[64];
274 /** Auto SCSI structure. */
275 AutoSCSIRam autoSCSIData;
276 } structured;
277} HostAdapterLocalRam, *PHostAdapterLocalRam;
278AssertCompileSize(HostAdapterLocalRam, 256);
279
280
281/** Ugly 24-bit big-endian addressing. */
282typedef struct
283{
284 uint8_t hi;
285 uint8_t mid;
286 uint8_t lo;
287} Addr24, Len24;
288AssertCompileSize(Addr24, 3);
289
290#define ADDR_TO_U32(x) (((x).hi << 16) | ((x).mid << 8) | (x).lo)
291#define LEN_TO_U32 ADDR_TO_U32
292#define U32_TO_ADDR(a, x) do {(a).hi = (x) >> 16; (a).mid = (x) >> 8; (a).lo = (x);} while(0)
293#define U32_TO_LEN U32_TO_ADDR
294
295/** @name Compatible ISA base I/O port addresses. Disabled if zero.
296 * @{ */
297#define NUM_ISA_BASES 8
298#define MAX_ISA_BASE (NUM_ISA_BASES - 1)
299#define ISA_BASE_DISABLED 6
300
301static uint16_t const g_aISABases[NUM_ISA_BASES] =
302{
303 0x330, 0x334, 0x230, 0x234, 0x130, 0x134, 0, 0
304};
305/** @} */
306
307/** Pointer to a task state structure. */
308typedef struct BUSLOGICTASKSTATE *PBUSLOGICTASKSTATE;
309
310/**
311 * Main BusLogic device state.
312 *
313 * @extends PCIDEVICE
314 * @implements PDMILEDPORTS
315 */
316typedef struct BUSLOGIC
317{
318 /** The PCI device structure. */
319 PCIDEVICE dev;
320 /** Pointer to the device instance - HC ptr */
321 PPDMDEVINSR3 pDevInsR3;
322 /** Pointer to the device instance - R0 ptr */
323 PPDMDEVINSR0 pDevInsR0;
324 /** Pointer to the device instance - RC ptr. */
325 PPDMDEVINSRC pDevInsRC;
326
327 /** Whether R0 is enabled. */
328 bool fR0Enabled;
329 /** Whether RC is enabled. */
330 bool fGCEnabled;
331
332 /** Base address of the I/O ports. */
333 RTIOPORT IOPortBase;
334 /** Base address of the memory mapping. */
335 RTGCPHYS MMIOBase;
336 /** Status register - Readonly. */
337 volatile uint8_t regStatus;
338 /** Interrupt register - Readonly. */
339 volatile uint8_t regInterrupt;
340 /** Geometry register - Readonly. */
341 volatile uint8_t regGeometry;
342 /** Pending (delayed) interrupt. */
343 uint8_t uPendingIntr;
344
345 /** Local RAM for the fetch hostadapter local RAM request.
346 * I don't know how big the buffer really is but the maximum
347 * seems to be 256 bytes because the offset and count field in the command request
348 * are only one byte big.
349 */
350 HostAdapterLocalRam LocalRam;
351
352 /** Command code the guest issued. */
353 uint8_t uOperationCode;
354 /** Buffer for the command parameters the adapter is currently receiving from the guest.
355 * Size of the largest command which is possible.
356 */
357 uint8_t aCommandBuffer[BUSLOGIC_COMMAND_SIZE_MAX]; /* Size of the biggest request. */
358 /** Current position in the command buffer. */
359 uint8_t iParameter;
360 /** Parameters left until the command is complete. */
361 uint8_t cbCommandParametersLeft;
362
363 /** Whether we are using the RAM or reply buffer. */
364 bool fUseLocalRam;
365 /** Buffer to store reply data from the controller to the guest. */
366 uint8_t aReplyBuffer[BUSLOGIC_REPLY_SIZE_MAX]; /* Size of the biggest reply. */
367 /** Position in the buffer we are reading next. */
368 uint8_t iReply;
369 /** Bytes left until the reply buffer is empty. */
370 uint8_t cbReplyParametersLeft;
371
372 /** Flag whether IRQs are enabled. */
373 bool fIRQEnabled;
374 /** Flag whether the ISA I/O port range is disabled
375 * to prevent the BIOS to access the device. */
376 bool fISAEnabled; /**< @todo unused, to be removed */
377 /** Flag whether 24-bit mailboxes are in use (default is 32-bit). */
378 bool fMbxIs24Bit;
379 /** ISA I/O port base (encoded in FW-compatible format). */
380 uint8_t uISABaseCode;
381
382 /** ISA I/O port base (disabled if zero). */
383 RTIOPORT IOISABase;
384 /** Default ISA I/O port base in FW-compatible format. */
385 uint8_t uDefaultISABaseCode;
386
387 /** Number of mailboxes the guest set up. */
388 uint32_t cMailbox;
389
390#if HC_ARCH_BITS == 64
391 uint32_t Alignment0;
392#endif
393
394 /** Time when HBA reset was last initiated. */ /**< @todo does this need to be saved? */
395 uint64_t u64ResetTime;
396 /** Physical base address of the outgoing mailboxes. */
397 RTGCPHYS GCPhysAddrMailboxOutgoingBase;
398 /** Current outgoing mailbox position. */
399 uint32_t uMailboxOutgoingPositionCurrent;
400 /** Number of mailboxes ready. */
401 volatile uint32_t cMailboxesReady;
402 /** Whether a notification to R3 was send. */
403 volatile bool fNotificationSend;
404
405#if HC_ARCH_BITS == 64
406 uint32_t Alignment1;
407#endif
408
409 /** Physical base address of the incoming mailboxes. */
410 RTGCPHYS GCPhysAddrMailboxIncomingBase;
411 /** Current incoming mailbox position. */
412 uint32_t uMailboxIncomingPositionCurrent;
413
414 /** Whether strict round robin is enabled. */
415 bool fStrictRoundRobinMode;
416 /** Whether the extended LUN CCB format is enabled for 32 possible logical units. */
417 bool fExtendedLunCCBFormat;
418
419 /** Queue to send tasks to R3. - HC ptr */
420 R3PTRTYPE(PPDMQUEUE) pNotifierQueueR3;
421 /** Queue to send tasks to R3. - HC ptr */
422 R0PTRTYPE(PPDMQUEUE) pNotifierQueueR0;
423 /** Queue to send tasks to R3. - RC ptr */
424 RCPTRTYPE(PPDMQUEUE) pNotifierQueueRC;
425
426 uint32_t Alignment2;
427
428 /** Critical section protecting access to the interrupt status register. */
429 PDMCRITSECT CritSectIntr;
430
431 /** Cache for task states. */
432 R3PTRTYPE(RTMEMCACHE) hTaskCache;
433
434 /** Device state for BIOS access. */
435 VBOXSCSI VBoxSCSI;
436
437 /** BusLogic device states. */
438 BUSLOGICDEVICE aDeviceStates[BUSLOGIC_MAX_DEVICES];
439
440 /** The base interface.
441 * @todo use PDMDEVINS::IBase */
442 PDMIBASE IBase;
443 /** Status Port - Leds interface. */
444 PDMILEDPORTS ILeds;
445 /** Partner of ILeds. */
446 R3PTRTYPE(PPDMILEDCONNECTORS) pLedsConnector;
447
448#if HC_ARCH_BITS == 64
449 uint32_t Alignment3;
450#endif
451
452 /** Indicates that PDMDevHlpAsyncNotificationCompleted should be called when
453 * a port is entering the idle state. */
454 bool volatile fSignalIdle;
455 /** Flag whether we have tasks which need to be processed again. */
456 bool volatile fRedo;
457 /** List of tasks which can be redone. */
458 R3PTRTYPE(volatile PBUSLOGICTASKSTATE) pTasksRedoHead;
459
460#ifdef LOG_ENABLED
461# if HC_ARCH_BITS == 64
462 uint32_t Alignment4;
463# endif
464
465 volatile uint32_t cInMailboxesReady;
466#endif
467
468} BUSLOGIC, *PBUSLOGIC;
469
470/** Register offsets in the I/O port space. */
471#define BUSLOGIC_REGISTER_CONTROL 0 /**< Writeonly */
472/** Fields for the control register. */
473# define BUSLOGIC_REGISTER_CONTROL_SCSI_BUSRESET RT_BIT(4)
474# define BUSLOGIC_REGISTER_CONTROL_INTERRUPT_RESET RT_BIT(5)
475# define BUSLOGIC_REGISTER_CONTROL_SOFT_RESET RT_BIT(6)
476# define BUSLOGIC_REGISTER_CONTROL_HARD_RESET RT_BIT(7)
477
478#define BUSLOGIC_REGISTER_STATUS 0 /**< Readonly */
479/** Fields for the status register. */
480# define BUSLOGIC_REGISTER_STATUS_COMMAND_INVALID RT_BIT(0)
481# define BUSLOGIC_REGISTER_STATUS_DATA_IN_REGISTER_READY RT_BIT(2)
482# define BUSLOGIC_REGISTER_STATUS_COMMAND_PARAMETER_REGISTER_BUSY RT_BIT(3)
483# define BUSLOGIC_REGISTER_STATUS_HOST_ADAPTER_READY RT_BIT(4)
484# define BUSLOGIC_REGISTER_STATUS_INITIALIZATION_REQUIRED RT_BIT(5)
485# define BUSLOGIC_REGISTER_STATUS_DIAGNOSTIC_FAILURE RT_BIT(6)
486# define BUSLOGIC_REGISTER_STATUS_DIAGNOSTIC_ACTIVE RT_BIT(7)
487
488#define BUSLOGIC_REGISTER_COMMAND 1 /**< Writeonly */
489#define BUSLOGIC_REGISTER_DATAIN 1 /**< Readonly */
490#define BUSLOGIC_REGISTER_INTERRUPT 2 /**< Readonly */
491/** Fields for the interrupt register. */
492# define BUSLOGIC_REGISTER_INTERRUPT_INCOMING_MAILBOX_LOADED RT_BIT(0)
493# define BUSLOGIC_REGISTER_INTERRUPT_OUTGOING_MAILBOX_AVAILABLE RT_BIT(1)
494# define BUSLOGIC_REGISTER_INTERRUPT_COMMAND_COMPLETE RT_BIT(2)
495# define BUSLOGIC_REGISTER_INTERRUPT_EXTERNAL_BUS_RESET RT_BIT(3)
496# define BUSLOGIC_REGISTER_INTERRUPT_INTERRUPT_VALID RT_BIT(7)
497
498#define BUSLOGIC_REGISTER_GEOMETRY 3 /* Readonly */
499# define BUSLOGIC_REGISTER_GEOMETRY_EXTENTED_TRANSLATION_ENABLED RT_BIT(7)
500
501/** Structure for the INQUIRE_PCI_HOST_ADAPTER_INFORMATION reply. */
502typedef struct ReplyInquirePCIHostAdapterInformation
503{
504 uint8_t IsaIOPort;
505 uint8_t IRQ;
506 unsigned char LowByteTerminated : 1;
507 unsigned char HighByteTerminated : 1;
508 unsigned char uReserved : 2; /* Reserved. */
509 unsigned char JP1 : 1; /* Whatever that means. */
510 unsigned char JP2 : 1; /* Whatever that means. */
511 unsigned char JP3 : 1; /* Whatever that means. */
512 /** Whether the provided info is valid. */
513 unsigned char InformationIsValid: 1;
514 uint8_t uReserved2; /* Reserved. */
515} ReplyInquirePCIHostAdapterInformation, *PReplyInquirePCIHostAdapterInformation;
516AssertCompileSize(ReplyInquirePCIHostAdapterInformation, 4);
517
518/** Structure for the INQUIRE_CONFIGURATION reply. */
519typedef struct ReplyInquireConfiguration
520{
521 unsigned char uReserved1 : 5;
522 bool fDmaChannel5 : 1;
523 bool fDmaChannel6 : 1;
524 bool fDmaChannel7 : 1;
525 bool fIrqChannel9 : 1;
526 bool fIrqChannel10 : 1;
527 bool fIrqChannel11 : 1;
528 bool fIrqChannel12 : 1;
529 unsigned char uReserved2 : 1;
530 bool fIrqChannel14 : 1;
531 bool fIrqChannel15 : 1;
532 unsigned char uReserved3 : 1;
533 unsigned char uHostAdapterId : 4;
534 unsigned char uReserved4 : 4;
535} ReplyInquireConfiguration, *PReplyInquireConfiguration;
536AssertCompileSize(ReplyInquireConfiguration, 3);
537
538/** Structure for the INQUIRE_SETUP_INFORMATION reply. */
539typedef struct ReplyInquireSetupInformationSynchronousValue
540{
541 unsigned char uOffset : 4;
542 unsigned char uTransferPeriod : 3;
543 bool fSynchronous : 1;
544}ReplyInquireSetupInformationSynchronousValue, *PReplyInquireSetupInformationSynchronousValue;
545AssertCompileSize(ReplyInquireSetupInformationSynchronousValue, 1);
546
547typedef struct ReplyInquireSetupInformation
548{
549 bool fSynchronousInitiationEnabled : 1;
550 bool fParityCheckingEnabled : 1;
551 unsigned char uReserved1 : 6;
552 uint8_t uBusTransferRate;
553 uint8_t uPreemptTimeOnBus;
554 uint8_t uTimeOffBus;
555 uint8_t cMailbox;
556 Addr24 MailboxAddress;
557 ReplyInquireSetupInformationSynchronousValue SynchronousValuesId0To7[8];
558 uint8_t uDisconnectPermittedId0To7;
559 uint8_t uSignature;
560 uint8_t uCharacterD;
561 uint8_t uHostBusType;
562 uint8_t uWideTransferPermittedId0To7;
563 uint8_t uWideTransfersActiveId0To7;
564 ReplyInquireSetupInformationSynchronousValue SynchronousValuesId8To15[8];
565 uint8_t uDisconnectPermittedId8To15;
566 uint8_t uReserved2;
567 uint8_t uWideTransferPermittedId8To15;
568 uint8_t uWideTransfersActiveId8To15;
569} ReplyInquireSetupInformation, *PReplyInquireSetupInformation;
570AssertCompileSize(ReplyInquireSetupInformation, 34);
571
572/** Structure for the INQUIRE_EXTENDED_SETUP_INFORMATION. */
573#pragma pack(1)
574typedef struct ReplyInquireExtendedSetupInformation
575{
576 uint8_t uBusType;
577 uint8_t uBiosAddress;
578 uint16_t u16ScatterGatherLimit;
579 uint8_t cMailbox;
580 uint32_t uMailboxAddressBase;
581 unsigned char uReserved1 : 2;
582 bool fFastEISA : 1;
583 unsigned char uReserved2 : 3;
584 bool fLevelSensitiveInterrupt : 1;
585 unsigned char uReserved3 : 1;
586 unsigned char aFirmwareRevision[3];
587 bool fHostWideSCSI : 1;
588 bool fHostDifferentialSCSI : 1;
589 bool fHostSupportsSCAM : 1;
590 bool fHostUltraSCSI : 1;
591 bool fHostSmartTermination : 1;
592 unsigned char uReserved4 : 3;
593} ReplyInquireExtendedSetupInformation, *PReplyInquireExtendedSetupInformation;
594AssertCompileSize(ReplyInquireExtendedSetupInformation, 14);
595#pragma pack()
596
597/** Structure for the INITIALIZE EXTENDED MAILBOX request. */
598#pragma pack(1)
599typedef struct RequestInitializeExtendedMailbox
600{
601 /** Number of mailboxes in guest memory. */
602 uint8_t cMailbox;
603 /** Physical address of the first mailbox. */
604 uint32_t uMailboxBaseAddress;
605} RequestInitializeExtendedMailbox, *PRequestInitializeExtendedMailbox;
606AssertCompileSize(RequestInitializeExtendedMailbox, 5);
607#pragma pack()
608
609/** Structure for the INITIALIZE MAILBOX request. */
610typedef struct
611{
612 /** Number of mailboxes to set up. */
613 uint8_t cMailbox;
614 /** Physical address of the first mailbox. */
615 Addr24 aMailboxBaseAddr;
616} RequestInitMbx, *PRequestInitMbx;
617AssertCompileSize(RequestInitMbx, 4);
618
619/**
620 * Structure of a mailbox in guest memory.
621 * The incoming and outgoing mailbox have the same size
622 * but the incoming one has some more fields defined which
623 * are marked as reserved in the outgoing one.
624 * The last field is also different from the type.
625 * For outgoing mailboxes it is the action and
626 * for incoming ones the completion status code for the task.
627 * We use one structure for both types.
628 */
629typedef struct Mailbox32
630{
631 /** Physical address of the CCB structure in the guest memory. */
632 uint32_t u32PhysAddrCCB;
633 /** Type specific data. */
634 union
635 {
636 /** For outgoing mailboxes. */
637 struct
638 {
639 /** Reserved */
640 uint8_t uReserved[3];
641 /** Action code. */
642 uint8_t uActionCode;
643 } out;
644 /** For incoming mailboxes. */
645 struct
646 {
647 /** The host adapter status after finishing the request. */
648 uint8_t uHostAdapterStatus;
649 /** The status of the device which executed the request after executing it. */
650 uint8_t uTargetDeviceStatus;
651 /** Reserved. */
652 uint8_t uReserved;
653 /** The completion status code of the request. */
654 uint8_t uCompletionCode;
655 } in;
656 } u;
657} Mailbox32, *PMailbox32;
658AssertCompileSize(Mailbox32, 8);
659
660/** Old style 24-bit mailbox entry. */
661typedef struct Mailbox24
662{
663 /** Mailbox command (incoming) or state (outgoing). */
664 uint8_t uCmdState;
665 /** Physical address of the CCB structure in the guest memory. */
666 Addr24 aPhysAddrCCB;
667} Mailbox24, *PMailbox24;
668AssertCompileSize(Mailbox24, 4);
669
670/**
671 * Action codes for outgoing mailboxes.
672 */
673enum BUSLOGIC_MAILBOX_OUTGOING_ACTION
674{
675 BUSLOGIC_MAILBOX_OUTGOING_ACTION_FREE = 0x00,
676 BUSLOGIC_MAILBOX_OUTGOING_ACTION_START_COMMAND = 0x01,
677 BUSLOGIC_MAILBOX_OUTGOING_ACTION_ABORT_COMMAND = 0x02
678};
679
680/**
681 * Completion codes for incoming mailboxes.
682 */
683enum BUSLOGIC_MAILBOX_INCOMING_COMPLETION
684{
685 BUSLOGIC_MAILBOX_INCOMING_COMPLETION_FREE = 0x00,
686 BUSLOGIC_MAILBOX_INCOMING_COMPLETION_WITHOUT_ERROR = 0x01,
687 BUSLOGIC_MAILBOX_INCOMING_COMPLETION_ABORTED = 0x02,
688 BUSLOGIC_MAILBOX_INCOMING_COMPLETION_ABORTED_NOT_FOUND = 0x03,
689 BUSLOGIC_MAILBOX_INCOMING_COMPLETION_WITH_ERROR = 0x04,
690 BUSLOGIC_MAILBOX_INCOMING_COMPLETION_INVALID_CCB = 0x05
691};
692
693/**
694 * Host adapter status for incoming mailboxes.
695 */
696enum BUSLOGIC_MAILBOX_INCOMING_ADAPTER_STATUS
697{
698 BUSLOGIC_MAILBOX_INCOMING_ADAPTER_STATUS_CMD_COMPLETED = 0x00,
699 BUSLOGIC_MAILBOX_INCOMING_ADAPTER_STATUS_LINKED_CMD_COMPLETED = 0x0a,
700 BUSLOGIC_MAILBOX_INCOMING_ADAPTER_STATUS_LINKED_CMD_COMPLETED_WITH_FLAG = 0x0b,
701 BUSLOGIC_MAILBOX_INCOMING_ADAPTER_STATUS_DATA_UNDERUN = 0x0c,
702 BUSLOGIC_MAILBOX_INCOMING_ADAPTER_STATUS_SCSI_SELECTION_TIMEOUT = 0x11,
703 BUSLOGIC_MAILBOX_INCOMING_ADAPTER_STATUS_DATA_OVERRUN = 0x12,
704 BUSLOGIC_MAILBOX_INCOMING_ADAPTER_STATUS_UNEXPECTED_BUS_FREE = 0x13,
705 BUSLOGIC_MAILBOX_INCOMING_ADAPTER_STATUS_INVALID_BUS_PHASE_REQUESTED = 0x14,
706 BUSLOGIC_MAILBOX_INCOMING_ADAPTER_STATUS_INVALID_OUTGOING_MAILBOX_ACTION_CODE = 0x15,
707 BUSLOGIC_MAILBOX_INCOMING_ADAPTER_STATUS_INVALID_COMMAND_OPERATION_CODE = 0x16,
708 BUSLOGIC_MAILBOX_INCOMING_ADAPTER_STATUS_LINKED_CCB_HAS_INVALID_LUN = 0x17,
709 BUSLOGIC_MAILBOX_INCOMING_ADAPTER_STATUS_INVALID_COMMAND_PARAMETER = 0x1a,
710 BUSLOGIC_MAILBOX_INCOMING_ADAPTER_STATUS_AUTO_REQUEST_SENSE_FAILED = 0x1b,
711 BUSLOGIC_MAILBOX_INCOMING_ADAPTER_STATUS_TAGGED_QUEUING_MESSAGE_REJECTED = 0x1c,
712 BUSLOGIC_MAILBOX_INCOMING_ADAPTER_STATUS_UNSUPPORTED_MESSAGE_RECEIVED = 0x1d,
713 BUSLOGIC_MAILBOX_INCOMING_ADAPTER_STATUS_HOST_ADAPTER_HARDWARE_FAILED = 0x20,
714 BUSLOGIC_MAILBOX_INCOMING_ADAPTER_STATUS_TARGET_FAILED_RESPONSE_TO_ATN = 0x21,
715 BUSLOGIC_MAILBOX_INCOMING_ADAPTER_STATUS_HOST_ADAPTER_ASSERTED_RST = 0x22,
716 BUSLOGIC_MAILBOX_INCOMING_ADAPTER_STATUS_OTHER_DEVICE_ASSERTED_RST = 0x23,
717 BUSLOGIC_MAILBOX_INCOMING_ADAPTER_STATUS_TARGET_DEVICE_RECONNECTED_IMPROPERLY = 0x24,
718 BUSLOGIC_MAILBOX_INCOMING_ADAPTER_STATUS_HOST_ADAPTER_ASSERTED_BUS_DEVICE_RESET = 0x25,
719 BUSLOGIC_MAILBOX_INCOMING_ADAPTER_STATUS_ABORT_QUEUE_GENERATED = 0x26,
720 BUSLOGIC_MAILBOX_INCOMING_ADAPTER_STATUS_HOST_ADAPTER_SOFTWARE_ERROR = 0x27,
721 BUSLOGIC_MAILBOX_INCOMING_ADAPTER_STATUS_HOST_ADAPTER_HARDWARE_TIMEOUT_ERROR = 0x30,
722 BUSLOGIC_MAILBOX_INCOMING_ADAPTER_STATUS_SCSI_PARITY_ERROR_DETECTED = 0x34
723};
724
725/**
726 * Device status codes for incoming mailboxes.
727 */
728enum BUSLOGIC_MAILBOX_INCOMING_DEVICE_STATUS
729{
730 BUSLOGIC_MAILBOX_INCOMING_DEVICE_STATUS_OPERATION_GOOD = 0x00,
731 BUSLOGIC_MAILBOX_INCOMING_DEVICE_STATUS_CHECK_CONDITION = 0x02,
732 BUSLOGIC_MAILBOX_INCOMING_DEVICE_STATUS_DEVICE_BUSY = 0x08
733};
734
735/**
736 * Opcode types for CCB.
737 */
738enum BUSLOGIC_CCB_OPCODE
739{
740 BUSLOGIC_CCB_OPCODE_INITIATOR_CCB = 0x00,
741 BUSLOGIC_CCB_OPCODE_TARGET_CCB = 0x01,
742 BUSLOGIC_CCB_OPCODE_INITIATOR_CCB_SCATTER_GATHER = 0x02,
743 BUSLOGIC_CCB_OPCODE_INITIATOR_CCB_RESIDUAL_DATA_LENGTH = 0x03,
744 BUSLOGIC_CCB_OPCODE_INITIATOR_CCB_RESIDUAL_SCATTER_GATHER = 0x04,
745 BUSLOGIC_CCB_OPCODE_BUS_DEVICE_RESET = 0x81
746};
747
748/**
749 * Data transfer direction.
750 */
751enum BUSLOGIC_CCB_DIRECTION
752{
753 BUSLOGIC_CCB_DIRECTION_UNKNOWN = 0x00,
754 BUSLOGIC_CCB_DIRECTION_IN = 0x01,
755 BUSLOGIC_CCB_DIRECTION_OUT = 0x02,
756 BUSLOGIC_CCB_DIRECTION_NO_DATA = 0x03
757};
758
759/**
760 * The command control block for a SCSI request.
761 */
762typedef struct CCB32
763{
764 /** Opcode. */
765 uint8_t uOpcode;
766 /** Reserved */
767 unsigned char uReserved1 : 3;
768 /** Data direction for the request. */
769 unsigned char uDataDirection : 2;
770 /** Whether the request is tag queued. */
771 bool fTagQueued : 1;
772 /** Queue tag mode. */
773 unsigned char uQueueTag : 2;
774 /** Length of the SCSI CDB. */
775 uint8_t cbCDB;
776 /** Sense data length. */
777 uint8_t cbSenseData;
778 /** Data length. */
779 uint32_t cbData;
780 /** Data pointer.
781 * This points to the data region or a scatter gather list based on the opcode.
782 */
783 uint32_t u32PhysAddrData;
784 /** Reserved. */
785 uint8_t uReserved2[2];
786 /** Host adapter status. */
787 uint8_t uHostAdapterStatus;
788 /** Device adapter status. */
789 uint8_t uDeviceStatus;
790 /** The device the request is sent to. */
791 uint8_t uTargetId;
792 /**The LUN in the device. */
793 unsigned char uLogicalUnit : 5;
794 /** Legacy tag. */
795 bool fLegacyTagEnable : 1;
796 /** Legacy queue tag. */
797 unsigned char uLegacyQueueTag : 2;
798 /** The SCSI CDB. (A CDB can be 12 bytes long.) */
799 uint8_t abCDB[12];
800 /** Reserved. */
801 uint8_t uReserved3[6];
802 /** Sense data pointer. */
803 uint32_t u32PhysAddrSenseData;
804} CCB32, *PCCB32;
805AssertCompileSize(CCB32, 40);
806
807
808/**
809 * The 24-bit command control block.
810 */
811typedef struct CCB24
812{
813 /** Opcode. */
814 uint8_t uOpcode;
815 /** The LUN in the device. */
816 unsigned char uLogicalUnit : 3;
817 /** Data direction for the request. */
818 unsigned char uDataDirection : 2;
819 /** The target device ID. */
820 unsigned char uTargetId : 3;
821 /** Length of the SCSI CDB. */
822 uint8_t cbCDB;
823 /** Sense data length. */
824 uint8_t cbSenseData;
825 /** Data length. */
826 Len24 acbData;
827 /** Data pointer.
828 * This points to the data region or a scatter gather list based on the opc
829 */
830 Addr24 aPhysAddrData;
831 /** Pointer to next CCB for linked commands. */
832 Addr24 aPhysAddrLink;
833 /** Command linking identifier. */
834 uint8_t uLinkId;
835 /** Host adapter status. */
836 uint8_t uHostAdapterStatus;
837 /** Device adapter status. */
838 uint8_t uDeviceStatus;
839 /** Two unused bytes. */
840 uint8_t aReserved[2];
841 /** The SCSI CDB. (A CDB can be 12 bytes long.) */
842 uint8_t abCDB[12];
843} CCB24, *PCCB24;
844AssertCompileSize(CCB24, 30);
845
846/**
847 * The common 24-bit/32-bit command control block. The 32-bit CCB is laid out
848 * such that many fields are in the same location as in the older 24-bit CCB.
849 */
850typedef struct CCBC
851{
852 /** Opcode. */
853 uint8_t uOpcode;
854 /** The LUN in the device. */
855 unsigned char uPad1 : 3;
856 /** Data direction for the request. */
857 unsigned char uDataDirection : 2;
858 /** The target device ID. */
859 unsigned char uPad2 : 3;
860 /** Length of the SCSI CDB. */
861 uint8_t cbCDB;
862 /** Sense data length. */
863 uint8_t cbSenseData;
864 uint8_t aPad1[10];
865 /** Host adapter status. */
866 uint8_t uHostAdapterStatus;
867 /** Device adapter status. */
868 uint8_t uDeviceStatus;
869 uint8_t aPad2[2];
870 /** The SCSI CDB (up to 12 bytes). */
871 uint8_t abCDB[12];
872} CCBC, *PCCBC;
873AssertCompileSize(CCB24, 30);
874
875/* Make sure that the 24-bit/32-bit/common CCB offsets match. */
876AssertCompileMemberOffset(CCBC, cbCDB, 2);
877AssertCompileMemberOffset(CCB24, cbCDB, 2);
878AssertCompileMemberOffset(CCB32, cbCDB, 2);
879AssertCompileMemberOffset(CCBC, uHostAdapterStatus, 14);
880AssertCompileMemberOffset(CCB24, uHostAdapterStatus, 14);
881AssertCompileMemberOffset(CCB32, uHostAdapterStatus, 14);
882AssertCompileMemberOffset(CCBC, abCDB, 18);
883AssertCompileMemberOffset(CCB24, abCDB, 18);
884AssertCompileMemberOffset(CCB32, abCDB, 18);
885
886/** A union of all CCB types (24-bit/32-bit/common). */
887typedef union CCBU
888{
889 CCB32 n; /**< New 32-bit CCB. */
890 CCB24 o; /**< Old 24-bit CCB. */
891 CCBC c; /**< Common CCB subset. */
892} CCBU, *PCCBU;
893
894/** 32-bit scatter-gather list entry. */
895typedef struct SGE32
896{
897 uint32_t cbSegment;
898 uint32_t u32PhysAddrSegmentBase;
899} SGE32, *PSGE32;
900AssertCompileSize(SGE32, 8);
901
902/** 24-bit scatter-gather list entry. */
903typedef struct SGE24
904{
905 Len24 acbSegment;
906 Addr24 aPhysAddrSegmentBase;
907} SGE24, *PSGE24;
908AssertCompileSize(SGE24, 6);
909
910/**
911 * The structure for the "Execute SCSI Command" command.
912 */
913typedef struct ESCMD
914{
915 /** Data length. */
916 uint32_t cbData;
917 /** Data pointer. */
918 uint32_t u32PhysAddrData;
919 /** The device the request is sent to. */
920 uint8_t uTargetId;
921 /** The LUN in the device. */
922 uint8_t uLogicalUnit;
923 /** Reserved */
924 unsigned char uReserved1 : 3;
925 /** Data direction for the request. */
926 unsigned char uDataDirection : 2;
927 /** Reserved */
928 unsigned char uReserved2 : 3;
929 /** Length of the SCSI CDB. */
930 uint8_t cbCDB;
931 /** The SCSI CDB. (A CDB can be 12 bytes long.) */
932 uint8_t abCDB[12];
933} ESCMD, *PESCMD;
934AssertCompileSize(ESCMD, 24);
935
936/**
937 * Task state for a CCB request.
938 */
939typedef struct BUSLOGICTASKSTATE
940{
941 /** Next in the redo list. */
942 PBUSLOGICTASKSTATE pRedoNext;
943 /** Device this task is assigned to. */
944 R3PTRTYPE(PBUSLOGICDEVICE) pTargetDeviceR3;
945 /** The command control block from the guest. */
946 CCBU CommandControlBlockGuest;
947 /** Mailbox read from guest memory. */
948 Mailbox32 MailboxGuest;
949 /** The SCSI request we pass to the underlying SCSI engine. */
950 PDMSCSIREQUEST PDMScsiRequest;
951 /** Data buffer segment */
952 RTSGSEG DataSeg;
953 /** Pointer to the R3 sense buffer. */
954 uint8_t *pbSenseBuffer;
955 /** Flag whether this is a request from the BIOS. */
956 bool fBIOS;
957 /** 24-bit request flag (default is 32-bit). */
958 bool fIs24Bit;
959 /** S/G entry size (depends on the above flag). */
960 uint8_t cbSGEntry;
961} BUSLOGICTASKSTATE;
962
963#ifndef VBOX_DEVICE_STRUCT_TESTCASE
964
965#define PDMIBASE_2_PBUSLOGICDEVICE(pInterface) ( (PBUSLOGICDEVICE)((uintptr_t)(pInterface) - RT_OFFSETOF(BUSLOGICDEVICE, IBase)) )
966#define PDMISCSIPORT_2_PBUSLOGICDEVICE(pInterface) ( (PBUSLOGICDEVICE)((uintptr_t)(pInterface) - RT_OFFSETOF(BUSLOGICDEVICE, ISCSIPort)) )
967#define PDMILEDPORTS_2_PBUSLOGICDEVICE(pInterface) ( (PBUSLOGICDEVICE)((uintptr_t)(pInterface) - RT_OFFSETOF(BUSLOGICDEVICE, ILed)) )
968#define PDMIBASE_2_PBUSLOGIC(pInterface) ( (PBUSLOGIC)((uintptr_t)(pInterface) - RT_OFFSETOF(BUSLOGIC, IBase)) )
969#define PDMILEDPORTS_2_PBUSLOGIC(pInterface) ( (PBUSLOGIC)((uintptr_t)(pInterface) - RT_OFFSETOF(BUSLOGIC, ILeds)) )
970
971/*******************************************************************************
972* Internal Functions *
973*******************************************************************************/
974static int buslogicR3RegisterISARange(PBUSLOGIC pBusLogic, uint8_t uBaseCode);
975
976
977/**
978 * Assert IRQ line of the BusLogic adapter.
979 *
980 * @returns nothing.
981 * @param pBusLogic Pointer to the BusLogic device instance.
982 * @param fSuppressIrq Flag to suppress IRQ generation regardless of fIRQEnabled
983 * @param uFlag Type of interrupt being generated.
984 */
985static void buslogicSetInterrupt(PBUSLOGIC pBusLogic, bool fSuppressIrq, uint8_t uIrqType)
986{
987 LogFlowFunc(("pBusLogic=%#p\n", pBusLogic));
988
989 /* The CMDC interrupt has priority over IMBL and MBOR. */
990 if (uIrqType & (BUSLOGIC_REGISTER_INTERRUPT_INCOMING_MAILBOX_LOADED | BUSLOGIC_REGISTER_INTERRUPT_OUTGOING_MAILBOX_AVAILABLE))
991 {
992 if (!(pBusLogic->regInterrupt & BUSLOGIC_REGISTER_INTERRUPT_COMMAND_COMPLETE))
993 pBusLogic->regInterrupt |= uIrqType; /* Report now. */
994 else
995 pBusLogic->uPendingIntr |= uIrqType; /* Report later. */
996 }
997 else if (uIrqType & BUSLOGIC_REGISTER_INTERRUPT_COMMAND_COMPLETE)
998 {
999 Assert(!pBusLogic->regInterrupt);
1000 pBusLogic->regInterrupt |= uIrqType;
1001 }
1002 else
1003 AssertMsgFailed(("Invalid interrupt state!\n"));
1004
1005 pBusLogic->regInterrupt |= BUSLOGIC_REGISTER_INTERRUPT_INTERRUPT_VALID;
1006 if (pBusLogic->fIRQEnabled && !fSuppressIrq)
1007 PDMDevHlpPCISetIrq(pBusLogic->CTX_SUFF(pDevIns), 0, 1);
1008}
1009
1010/**
1011 * Deasserts the interrupt line of the BusLogic adapter.
1012 *
1013 * @returns nothing
1014 * @param pBuslogic Pointer to the BusLogic device instance.
1015 */
1016static void buslogicClearInterrupt(PBUSLOGIC pBusLogic)
1017{
1018 LogFlowFunc(("pBusLogic=%#p\n", pBusLogic));
1019 pBusLogic->regInterrupt = 0;
1020 PDMDevHlpPCISetIrq(pBusLogic->CTX_SUFF(pDevIns), 0, 0);
1021 /* If there's another pending interrupt, report it now. */
1022 if (pBusLogic->uPendingIntr)
1023 {
1024 buslogicSetInterrupt(pBusLogic, false, pBusLogic->uPendingIntr);
1025 pBusLogic->uPendingIntr = 0;
1026 }
1027}
1028
1029#if defined(IN_RING3)
1030
1031/**
1032 * Advances the mailbox pointer to the next slot.
1033 */
1034DECLINLINE(void) buslogicR3OutgoingMailboxAdvance(PBUSLOGIC pBusLogic)
1035{
1036 pBusLogic->uMailboxOutgoingPositionCurrent = (pBusLogic->uMailboxOutgoingPositionCurrent + 1) % pBusLogic->cMailbox;
1037}
1038
1039/**
1040 * Initialize local RAM of host adapter with default values.
1041 *
1042 * @returns nothing.
1043 * @param pBusLogic.
1044 */
1045static void buslogicR3InitializeLocalRam(PBUSLOGIC pBusLogic)
1046{
1047 /*
1048 * These values are mostly from what I think is right
1049 * looking at the dmesg output from a Linux guest inside
1050 * a VMware server VM.
1051 *
1052 * So they don't have to be right :)
1053 */
1054 memset(pBusLogic->LocalRam.u8View, 0, sizeof(HostAdapterLocalRam));
1055 pBusLogic->LocalRam.structured.autoSCSIData.fLevelSensitiveInterrupt = true;
1056 pBusLogic->LocalRam.structured.autoSCSIData.fParityCheckingEnabled = true;
1057 pBusLogic->LocalRam.structured.autoSCSIData.fExtendedTranslation = true; /* Same as in geometry register. */
1058 pBusLogic->LocalRam.structured.autoSCSIData.u16DeviceEnabledMask = ~0; /* All enabled. Maybe mask out non present devices? */
1059 pBusLogic->LocalRam.structured.autoSCSIData.u16WidePermittedMask = ~0;
1060 pBusLogic->LocalRam.structured.autoSCSIData.u16FastPermittedMask = ~0;
1061 pBusLogic->LocalRam.structured.autoSCSIData.u16SynchronousPermittedMask = ~0;
1062 pBusLogic->LocalRam.structured.autoSCSIData.u16DisconnectPermittedMask = ~0;
1063 pBusLogic->LocalRam.structured.autoSCSIData.fStrictRoundRobinMode = pBusLogic->fStrictRoundRobinMode;
1064 pBusLogic->LocalRam.structured.autoSCSIData.u16UltraPermittedMask = ~0;
1065 /** @todo calculate checksum? */
1066}
1067
1068/**
1069 * Do a hardware reset of the buslogic adapter.
1070 *
1071 * @returns VBox status code.
1072 * @param pBusLogic Pointer to the BusLogic device instance.
1073 * @param fResetIO Flag determining whether ISA I/O should be reset.
1074 */
1075static int buslogicR3HwReset(PBUSLOGIC pBusLogic, bool fResetIO)
1076{
1077 LogFlowFunc(("pBusLogic=%#p\n", pBusLogic));
1078
1079 /* Reset registers to default value. */
1080 pBusLogic->regStatus = BUSLOGIC_REGISTER_STATUS_HOST_ADAPTER_READY | BUSLOGIC_REGISTER_STATUS_INITIALIZATION_REQUIRED;
1081 pBusLogic->regInterrupt = 0;
1082 pBusLogic->uPendingIntr = 0;
1083 pBusLogic->regGeometry = BUSLOGIC_REGISTER_GEOMETRY_EXTENTED_TRANSLATION_ENABLED;
1084 pBusLogic->uOperationCode = 0xff; /* No command executing. */
1085 pBusLogic->iParameter = 0;
1086 pBusLogic->cbCommandParametersLeft = 0;
1087 pBusLogic->fIRQEnabled = true;
1088 pBusLogic->uMailboxOutgoingPositionCurrent = 0;
1089 pBusLogic->uMailboxIncomingPositionCurrent = 0;
1090
1091 /* Guest-initiated HBA reset does not affect ISA port I/O. */
1092 if (fResetIO)
1093 {
1094 buslogicR3RegisterISARange(pBusLogic, pBusLogic->uDefaultISABaseCode);
1095 }
1096 buslogicR3InitializeLocalRam(pBusLogic);
1097 vboxscsiInitialize(&pBusLogic->VBoxSCSI);
1098
1099 return VINF_SUCCESS;
1100}
1101
1102#endif /* IN_RING3 */
1103
1104/**
1105 * Resets the command state machine for the next command and notifies the guest.
1106 *
1107 * @returns nothing.
1108 * @param pBusLogic Pointer to the BusLogic device instance
1109 * @param fSuppressIrq Flag to suppress IRQ generation regardless of current state
1110 */
1111static void buslogicCommandComplete(PBUSLOGIC pBusLogic, bool fSuppressIrq)
1112{
1113 LogFlowFunc(("pBusLogic=%#p\n", pBusLogic));
1114
1115 pBusLogic->fUseLocalRam = false;
1116 pBusLogic->regStatus |= BUSLOGIC_REGISTER_STATUS_HOST_ADAPTER_READY;
1117 pBusLogic->iReply = 0;
1118
1119 /* Modify I/O address does not generate an interrupt. */
1120 if (pBusLogic->uOperationCode != BUSLOGICCOMMAND_EXECUTE_MAILBOX_COMMAND)
1121 {
1122 /* Notify that the command is complete. */
1123 pBusLogic->regStatus &= ~BUSLOGIC_REGISTER_STATUS_DATA_IN_REGISTER_READY;
1124 buslogicSetInterrupt(pBusLogic, fSuppressIrq, BUSLOGIC_REGISTER_INTERRUPT_COMMAND_COMPLETE);
1125 }
1126
1127 pBusLogic->uOperationCode = 0xff;
1128 pBusLogic->iParameter = 0;
1129}
1130
1131#if defined(IN_RING3)
1132
1133/**
1134 * Initiates a hard reset which was issued from the guest.
1135 *
1136 * @returns nothing
1137 * @param pBusLogic Pointer to the BusLogic device instance.
1138 * @param fHardReset Flag initiating a hard (vs. soft) reset.
1139 */
1140static void buslogicR3InitiateReset(PBUSLOGIC pBusLogic, bool fHardReset)
1141{
1142 LogFlowFunc(("pBusLogic=%#p fHardReset=%d\n", pBusLogic, fHardReset));
1143
1144 buslogicR3HwReset(pBusLogic, false);
1145
1146 if (fHardReset)
1147 {
1148 /* Set the diagnostic active bit in the status register and clear the ready state. */
1149 pBusLogic->regStatus |= BUSLOGIC_REGISTER_STATUS_DIAGNOSTIC_ACTIVE;
1150 pBusLogic->regStatus &= ~BUSLOGIC_REGISTER_STATUS_HOST_ADAPTER_READY;
1151
1152 /* Remember when the guest initiated a reset (after we're done resetting). */
1153 pBusLogic->u64ResetTime = PDMDevHlpTMTimeVirtGetNano(pBusLogic->CTX_SUFF(pDevIns));
1154 }
1155}
1156
1157/**
1158 * Send a mailbox with set status codes to the guest.
1159 *
1160 * @returns nothing.
1161 * @param pBusLogic Pointer to the BusLogic device instance.
1162 * @param pTaskState Pointer to the task state with the mailbox to send.
1163 * @param uHostAdapterStatus The host adapter status code to set.
1164 * @param uDeviceStatus The target device status to set.
1165 * @param uMailboxCompletionCode Completion status code to set in the mailbox.
1166 */
1167static void buslogicR3SendIncomingMailbox(PBUSLOGIC pBusLogic, PBUSLOGICTASKSTATE pTaskState,
1168 uint8_t uHostAdapterStatus, uint8_t uDeviceStatus,
1169 uint8_t uMailboxCompletionCode)
1170{
1171 pTaskState->MailboxGuest.u.in.uHostAdapterStatus = uHostAdapterStatus;
1172 pTaskState->MailboxGuest.u.in.uTargetDeviceStatus = uDeviceStatus;
1173 pTaskState->MailboxGuest.u.in.uCompletionCode = uMailboxCompletionCode;
1174
1175 int rc = PDMCritSectEnter(&pBusLogic->CritSectIntr, VINF_SUCCESS);
1176 AssertRC(rc);
1177 RTGCPHYS GCPhysAddrCCB = pTaskState->MailboxGuest.u32PhysAddrCCB;
1178 RTGCPHYS GCPhysAddrMailboxIncoming = pBusLogic->GCPhysAddrMailboxIncomingBase
1179 + ( pBusLogic->uMailboxIncomingPositionCurrent
1180 * (pTaskState->fIs24Bit ? sizeof(Mailbox24) : sizeof(Mailbox32)) );
1181 LogFlowFunc(("Completing CCB %RGp hstat=%u, dstat=%u, outgoing mailbox at %RGp\n", GCPhysAddrCCB,
1182 uHostAdapterStatus, uDeviceStatus, GCPhysAddrMailboxIncoming));
1183
1184 /* Update CCB. */
1185 pTaskState->CommandControlBlockGuest.c.uHostAdapterStatus = uHostAdapterStatus;
1186 pTaskState->CommandControlBlockGuest.c.uDeviceStatus = uDeviceStatus;
1187 /* Rewrite CCB up to the CDB; perhaps more than necessary. */
1188 PDMDevHlpPCIPhysWrite(pBusLogic->CTX_SUFF(pDevIns), GCPhysAddrCCB,
1189 &pTaskState->CommandControlBlockGuest, RT_OFFSETOF(CCBC, abCDB));
1190
1191# ifdef RT_STRICT
1192 uint8_t uCode;
1193 unsigned uCodeOffs = pTaskState->fIs24Bit ? RT_OFFSETOF(Mailbox24, uCmdState) : RT_OFFSETOF(Mailbox32, u.out.uActionCode);
1194 PDMDevHlpPhysRead(pBusLogic->CTX_SUFF(pDevIns), GCPhysAddrMailboxIncoming + uCodeOffs, &uCode, sizeof(uCode));
1195 Assert(uCode == BUSLOGIC_MAILBOX_INCOMING_COMPLETION_FREE);
1196# endif
1197
1198 /* Update mailbox. */
1199 if (pTaskState->fIs24Bit)
1200 {
1201 Mailbox24 Mbx24;
1202
1203 Mbx24.uCmdState = pTaskState->MailboxGuest.u.in.uCompletionCode;
1204 U32_TO_ADDR(Mbx24.aPhysAddrCCB, pTaskState->MailboxGuest.u32PhysAddrCCB);
1205 Log(("24-bit mailbox: completion code=%u, CCB at %RGp\n", Mbx24.uCmdState, (RTGCPHYS)ADDR_TO_U32(Mbx24.aPhysAddrCCB)));
1206 PDMDevHlpPCIPhysWrite(pBusLogic->CTX_SUFF(pDevIns), GCPhysAddrMailboxIncoming, &Mbx24, sizeof(Mailbox24));
1207 }
1208 else
1209 {
1210 Log(("32-bit mailbox: completion code=%u, CCB at %RGp\n", pTaskState->MailboxGuest.u.in.uCompletionCode, (RTGCPHYS)pTaskState->MailboxGuest.u32PhysAddrCCB));
1211 PDMDevHlpPCIPhysWrite(pBusLogic->CTX_SUFF(pDevIns), GCPhysAddrMailboxIncoming,
1212 &pTaskState->MailboxGuest, sizeof(Mailbox32));
1213 }
1214
1215 /* Advance to next mailbox position. */
1216 pBusLogic->uMailboxIncomingPositionCurrent++;
1217 if (pBusLogic->uMailboxIncomingPositionCurrent >= pBusLogic->cMailbox)
1218 pBusLogic->uMailboxIncomingPositionCurrent = 0;
1219
1220# ifdef LOG_ENABLED
1221 ASMAtomicIncU32(&pBusLogic->cInMailboxesReady);
1222# endif
1223
1224 buslogicSetInterrupt(pBusLogic, false, BUSLOGIC_REGISTER_INTERRUPT_INCOMING_MAILBOX_LOADED);
1225
1226 PDMCritSectLeave(&pBusLogic->CritSectIntr);
1227}
1228
1229# ifdef LOG_ENABLED
1230
1231/**
1232 * Dumps the content of a mailbox for debugging purposes.
1233 *
1234 * @return nothing
1235 * @param pMailbox The mailbox to dump.
1236 * @param fOutgoing true if dumping the outgoing state.
1237 * false if dumping the incoming state.
1238 */
1239static void buslogicR3DumpMailboxInfo(PMailbox32 pMailbox, bool fOutgoing)
1240{
1241 Log(("%s: Dump for %s mailbox:\n", __FUNCTION__, fOutgoing ? "outgoing" : "incoming"));
1242 Log(("%s: u32PhysAddrCCB=%#x\n", __FUNCTION__, pMailbox->u32PhysAddrCCB));
1243 if (fOutgoing)
1244 {
1245 Log(("%s: uActionCode=%u\n", __FUNCTION__, pMailbox->u.out.uActionCode));
1246 }
1247 else
1248 {
1249 Log(("%s: uHostAdapterStatus=%u\n", __FUNCTION__, pMailbox->u.in.uHostAdapterStatus));
1250 Log(("%s: uTargetDeviceStatus=%u\n", __FUNCTION__, pMailbox->u.in.uTargetDeviceStatus));
1251 Log(("%s: uCompletionCode=%u\n", __FUNCTION__, pMailbox->u.in.uCompletionCode));
1252 }
1253}
1254
1255/**
1256 * Dumps the content of a command control block for debugging purposes.
1257 *
1258 * @returns nothing.
1259 * @param pCCB Pointer to the command control block to dump.
1260 * @param fIs24BitCCB Flag to determine CCB format.
1261 */
1262static void buslogicR3DumpCCBInfo(PCCBU pCCB, bool fIs24BitCCB)
1263{
1264 Log(("%s: Dump for %s Command Control Block:\n", __FUNCTION__, fIs24BitCCB ? "24-bit" : "32-bit"));
1265 Log(("%s: uOpCode=%#x\n", __FUNCTION__, pCCB->c.uOpcode));
1266 Log(("%s: uDataDirection=%u\n", __FUNCTION__, pCCB->c.uDataDirection));
1267 Log(("%s: cbCDB=%u\n", __FUNCTION__, pCCB->c.cbCDB));
1268 Log(("%s: cbSenseData=%u\n", __FUNCTION__, pCCB->c.cbSenseData));
1269 Log(("%s: uHostAdapterStatus=%u\n", __FUNCTION__, pCCB->c.uHostAdapterStatus));
1270 Log(("%s: uDeviceStatus=%u\n", __FUNCTION__, pCCB->c.uDeviceStatus));
1271 if (fIs24BitCCB)
1272 {
1273 Log(("%s: cbData=%u\n", __FUNCTION__, LEN_TO_U32(pCCB->o.acbData)));
1274 Log(("%s: PhysAddrData=%#x\n", __FUNCTION__, ADDR_TO_U32(pCCB->o.aPhysAddrData)));
1275 Log(("%s: uTargetId=%u\n", __FUNCTION__, pCCB->o.uTargetId));
1276 Log(("%s: uLogicalUnit=%u\n", __FUNCTION__, pCCB->o.uLogicalUnit));
1277 }
1278 else
1279 {
1280 Log(("%s: cbData=%u\n", __FUNCTION__, pCCB->n.cbData));
1281 Log(("%s: PhysAddrData=%#x\n", __FUNCTION__, pCCB->n.u32PhysAddrData));
1282 Log(("%s: uTargetId=%u\n", __FUNCTION__, pCCB->n.uTargetId));
1283 Log(("%s: uLogicalUnit=%u\n", __FUNCTION__, pCCB->n.uLogicalUnit));
1284 Log(("%s: fTagQueued=%d\n", __FUNCTION__, pCCB->n.fTagQueued));
1285 Log(("%s: uQueueTag=%u\n", __FUNCTION__, pCCB->n.uQueueTag));
1286 Log(("%s: fLegacyTagEnable=%u\n", __FUNCTION__, pCCB->n.fLegacyTagEnable));
1287 Log(("%s: uLegacyQueueTag=%u\n", __FUNCTION__, pCCB->n.uLegacyQueueTag));
1288 Log(("%s: PhysAddrSenseData=%#x\n", __FUNCTION__, pCCB->n.u32PhysAddrSenseData));
1289 }
1290 Log(("%s: uCDB[0]=%#x\n", __FUNCTION__, pCCB->c.abCDB[0]));
1291 for (int i = 1; i < pCCB->c.cbCDB; i++)
1292 Log(("%s: uCDB[%d]=%u\n", __FUNCTION__, i, pCCB->c.abCDB[i]));
1293}
1294
1295# endif /* LOG_ENABLED */
1296
1297/**
1298 * Allocate data buffer.
1299 *
1300 * @param pTaskState Pointer to the task state.
1301 * @param GCSGList Guest physical address of S/G list.
1302 * @param cEntries Number of list entries to read.
1303 * @param pSGEList Pointer to 32-bit S/G list storage.
1304 */
1305static void buslogicR3ReadSGEntries(PBUSLOGICTASKSTATE pTaskState, RTGCPHYS GCSGList, uint32_t cEntries, SGE32 *pSGEList)
1306{
1307 PPDMDEVINS pDevIns = pTaskState->CTX_SUFF(pTargetDevice)->CTX_SUFF(pBusLogic)->CTX_SUFF(pDevIns);
1308 SGE24 aSGE24[32];
1309 Assert(cEntries <= RT_ELEMENTS(aSGE24));
1310
1311 /* Read the S/G entries. Convert 24-bit entries to 32-bit format. */
1312 if (pTaskState->fIs24Bit)
1313 {
1314 Log2(("Converting %u 24-bit S/G entries to 32-bit\n", cEntries));
1315 PDMDevHlpPhysRead(pDevIns, GCSGList, &aSGE24, cEntries * sizeof(SGE24));
1316 for (uint32_t i = 0; i < cEntries; ++i)
1317 {
1318 pSGEList[i].cbSegment = LEN_TO_U32(aSGE24[i].acbSegment);
1319 pSGEList[i].u32PhysAddrSegmentBase = ADDR_TO_U32(aSGE24[i].aPhysAddrSegmentBase);
1320 }
1321 }
1322 else
1323 PDMDevHlpPhysRead(pDevIns, GCSGList, pSGEList, cEntries * sizeof(SGE32));
1324}
1325
1326/**
1327 * Allocate data buffer.
1328 *
1329 * @returns VBox status code.
1330 * @param pTaskState Pointer to the task state.
1331 */
1332static int buslogicR3DataBufferAlloc(PBUSLOGICTASKSTATE pTaskState)
1333{
1334 PPDMDEVINS pDevIns = pTaskState->CTX_SUFF(pTargetDevice)->CTX_SUFF(pBusLogic)->CTX_SUFF(pDevIns);
1335 uint32_t cbDataCCB;
1336 uint32_t u32PhysAddrCCB;
1337
1338 /* Extract the data length and physical address from the CCB. */
1339 if (pTaskState->fIs24Bit)
1340 {
1341 u32PhysAddrCCB = ADDR_TO_U32(pTaskState->CommandControlBlockGuest.o.aPhysAddrData);
1342 cbDataCCB = LEN_TO_U32(pTaskState->CommandControlBlockGuest.o.acbData);
1343 }
1344 else
1345 {
1346 u32PhysAddrCCB = pTaskState->CommandControlBlockGuest.n.u32PhysAddrData;
1347 cbDataCCB = pTaskState->CommandControlBlockGuest.n.cbData;
1348 }
1349
1350 if ( (pTaskState->CommandControlBlockGuest.c.uDataDirection != BUSLOGIC_CCB_DIRECTION_NO_DATA)
1351 && cbDataCCB)
1352 {
1353 /** @todo Check following assumption and what residual means. */
1354 /*
1355 * The BusLogic adapter can handle two different data buffer formats.
1356 * The first one is that the data pointer entry in the CCB points to
1357 * the buffer directly. In second mode the data pointer points to a
1358 * scatter gather list which describes the buffer.
1359 */
1360 if ( (pTaskState->CommandControlBlockGuest.c.uOpcode == BUSLOGIC_CCB_OPCODE_INITIATOR_CCB_SCATTER_GATHER)
1361 || (pTaskState->CommandControlBlockGuest.c.uOpcode == BUSLOGIC_CCB_OPCODE_INITIATOR_CCB_RESIDUAL_SCATTER_GATHER))
1362 {
1363 uint32_t cScatterGatherGCRead;
1364 uint32_t iScatterGatherEntry;
1365 SGE32 aScatterGatherReadGC[32]; /* A buffer for scatter gather list entries read from guest memory. */
1366 uint32_t cScatterGatherGCLeft = cbDataCCB / pTaskState->cbSGEntry;
1367 RTGCPHYS GCPhysAddrScatterGatherCurrent = u32PhysAddrCCB;
1368 size_t cbDataToTransfer = 0;
1369
1370 /* Count number of bytes to transfer. */
1371 do
1372 {
1373 cScatterGatherGCRead = (cScatterGatherGCLeft < RT_ELEMENTS(aScatterGatherReadGC))
1374 ? cScatterGatherGCLeft
1375 : RT_ELEMENTS(aScatterGatherReadGC);
1376 cScatterGatherGCLeft -= cScatterGatherGCRead;
1377
1378 buslogicR3ReadSGEntries(pTaskState, GCPhysAddrScatterGatherCurrent, cScatterGatherGCRead, aScatterGatherReadGC);
1379
1380 for (iScatterGatherEntry = 0; iScatterGatherEntry < cScatterGatherGCRead; iScatterGatherEntry++)
1381 {
1382 RTGCPHYS GCPhysAddrDataBase;
1383
1384 Log(("%s: iScatterGatherEntry=%u\n", __FUNCTION__, iScatterGatherEntry));
1385
1386 GCPhysAddrDataBase = (RTGCPHYS)aScatterGatherReadGC[iScatterGatherEntry].u32PhysAddrSegmentBase;
1387 cbDataToTransfer += aScatterGatherReadGC[iScatterGatherEntry].cbSegment;
1388
1389 Log(("%s: GCPhysAddrDataBase=%RGp cbDataToTransfer=%u\n",
1390 __FUNCTION__, GCPhysAddrDataBase,
1391 aScatterGatherReadGC[iScatterGatherEntry].cbSegment));
1392 }
1393
1394 /* Set address to the next entries to read. */
1395 GCPhysAddrScatterGatherCurrent += cScatterGatherGCRead * pTaskState->cbSGEntry;
1396 } while (cScatterGatherGCLeft > 0);
1397
1398 Log(("%s: cbDataToTransfer=%d\n", __FUNCTION__, cbDataToTransfer));
1399
1400 /* Allocate buffer */
1401 pTaskState->DataSeg.cbSeg = cbDataToTransfer;
1402 pTaskState->DataSeg.pvSeg = RTMemAlloc(pTaskState->DataSeg.cbSeg);
1403 if (!pTaskState->DataSeg.pvSeg)
1404 return VERR_NO_MEMORY;
1405
1406 /* Copy the data if needed */
1407 if ( (pTaskState->CommandControlBlockGuest.c.uDataDirection == BUSLOGIC_CCB_DIRECTION_OUT)
1408 || (pTaskState->CommandControlBlockGuest.c.uDataDirection == BUSLOGIC_CCB_DIRECTION_UNKNOWN))
1409 {
1410 cScatterGatherGCLeft = cbDataCCB / pTaskState->cbSGEntry;
1411 GCPhysAddrScatterGatherCurrent = u32PhysAddrCCB;
1412 uint8_t *pbData = (uint8_t *)pTaskState->DataSeg.pvSeg;
1413
1414 do
1415 {
1416 cScatterGatherGCRead = (cScatterGatherGCLeft < RT_ELEMENTS(aScatterGatherReadGC))
1417 ? cScatterGatherGCLeft
1418 : RT_ELEMENTS(aScatterGatherReadGC);
1419 cScatterGatherGCLeft -= cScatterGatherGCRead;
1420
1421 buslogicR3ReadSGEntries(pTaskState, GCPhysAddrScatterGatherCurrent, cScatterGatherGCRead, aScatterGatherReadGC);
1422
1423 for (iScatterGatherEntry = 0; iScatterGatherEntry < cScatterGatherGCRead; iScatterGatherEntry++)
1424 {
1425 RTGCPHYS GCPhysAddrDataBase;
1426
1427 Log(("%s: iScatterGatherEntry=%u\n", __FUNCTION__, iScatterGatherEntry));
1428
1429 GCPhysAddrDataBase = (RTGCPHYS)aScatterGatherReadGC[iScatterGatherEntry].u32PhysAddrSegmentBase;
1430 cbDataToTransfer = aScatterGatherReadGC[iScatterGatherEntry].cbSegment;
1431
1432 Log(("%s: GCPhysAddrDataBase=%RGp cbDataToTransfer=%u\n", __FUNCTION__, GCPhysAddrDataBase, cbDataToTransfer));
1433
1434 PDMDevHlpPhysRead(pDevIns, GCPhysAddrDataBase, pbData, cbDataToTransfer);
1435 pbData += cbDataToTransfer;
1436 }
1437
1438 /* Set address to the next entries to read. */
1439 GCPhysAddrScatterGatherCurrent += cScatterGatherGCRead * pTaskState->cbSGEntry;
1440 } while (cScatterGatherGCLeft > 0);
1441 }
1442
1443 }
1444 else if ( pTaskState->CommandControlBlockGuest.c.uOpcode == BUSLOGIC_CCB_OPCODE_INITIATOR_CCB
1445 || pTaskState->CommandControlBlockGuest.c.uOpcode == BUSLOGIC_CCB_OPCODE_INITIATOR_CCB_RESIDUAL_DATA_LENGTH)
1446 {
1447 /* The buffer is not scattered. */
1448 RTGCPHYS GCPhysAddrDataBase = u32PhysAddrCCB;
1449
1450 AssertMsg(GCPhysAddrDataBase != 0, ("Physical address is 0\n"));
1451
1452 pTaskState->DataSeg.cbSeg = cbDataCCB;
1453 pTaskState->DataSeg.pvSeg = RTMemAlloc(pTaskState->DataSeg.cbSeg);
1454 if (!pTaskState->DataSeg.pvSeg)
1455 return VERR_NO_MEMORY;
1456
1457 Log(("Non scattered buffer:\n"));
1458 Log(("u32PhysAddrData=%#x\n", u32PhysAddrCCB));
1459 Log(("cbData=%u\n", cbDataCCB));
1460 Log(("GCPhysAddrDataBase=0x%RGp\n", GCPhysAddrDataBase));
1461
1462 /* Copy the data into the buffer. */
1463 PDMDevHlpPhysRead(pDevIns, GCPhysAddrDataBase, pTaskState->DataSeg.pvSeg, pTaskState->DataSeg.cbSeg);
1464 }
1465 }
1466
1467 return VINF_SUCCESS;
1468}
1469
1470/**
1471 * Free allocated resources used for the scatter gather list.
1472 *
1473 * @returns nothing.
1474 * @param pTaskState Pointer to the task state.
1475 */
1476static void buslogicR3DataBufferFree(PBUSLOGICTASKSTATE pTaskState)
1477{
1478 PPDMDEVINS pDevIns = pTaskState->CTX_SUFF(pTargetDevice)->CTX_SUFF(pBusLogic)->CTX_SUFF(pDevIns);
1479 uint32_t cbDataCCB;
1480 uint32_t u32PhysAddrCCB;
1481
1482 /* Extract the data length and physical address from the CCB. */
1483 if (pTaskState->fIs24Bit)
1484 {
1485 u32PhysAddrCCB = ADDR_TO_U32(pTaskState->CommandControlBlockGuest.o.aPhysAddrData);
1486 cbDataCCB = LEN_TO_U32(pTaskState->CommandControlBlockGuest.o.acbData);
1487 }
1488 else
1489 {
1490 u32PhysAddrCCB = pTaskState->CommandControlBlockGuest.n.u32PhysAddrData;
1491 cbDataCCB = pTaskState->CommandControlBlockGuest.n.cbData;
1492 }
1493
1494#if 1
1495 /* Hack for NT 10/91: A CCB describes a 2K buffer, but TEST UNIT READY is executed. This command
1496 * returns no data, hence the buffer must be left alone!
1497 */
1498 if (pTaskState->CommandControlBlockGuest.c.abCDB[0] == 0)
1499 cbDataCCB = 0;
1500#endif
1501
1502 LogFlowFunc(("pTaskState=%#p cbDataCCB=%u direction=%u cbSeg=%u\n", pTaskState, cbDataCCB,
1503 pTaskState->CommandControlBlockGuest.c.uDataDirection, pTaskState->DataSeg.cbSeg));
1504
1505 if ( (cbDataCCB > 0)
1506 && ( (pTaskState->CommandControlBlockGuest.c.uDataDirection == BUSLOGIC_CCB_DIRECTION_IN)
1507 || (pTaskState->CommandControlBlockGuest.c.uDataDirection == BUSLOGIC_CCB_DIRECTION_UNKNOWN)))
1508 {
1509 if ( (pTaskState->CommandControlBlockGuest.c.uOpcode == BUSLOGIC_CCB_OPCODE_INITIATOR_CCB_SCATTER_GATHER)
1510 || (pTaskState->CommandControlBlockGuest.c.uOpcode == BUSLOGIC_CCB_OPCODE_INITIATOR_CCB_RESIDUAL_SCATTER_GATHER))
1511 {
1512 uint32_t cScatterGatherGCRead;
1513 uint32_t iScatterGatherEntry;
1514 SGE32 aScatterGatherReadGC[32]; /* Number of scatter gather list entries read from guest memory. */
1515 uint32_t cScatterGatherGCLeft = cbDataCCB / pTaskState->cbSGEntry;
1516 RTGCPHYS GCPhysAddrScatterGatherCurrent = u32PhysAddrCCB;
1517 uint8_t *pbData = (uint8_t *)pTaskState->DataSeg.pvSeg;
1518
1519 do
1520 {
1521 cScatterGatherGCRead = (cScatterGatherGCLeft < RT_ELEMENTS(aScatterGatherReadGC))
1522 ? cScatterGatherGCLeft
1523 : RT_ELEMENTS(aScatterGatherReadGC);
1524 cScatterGatherGCLeft -= cScatterGatherGCRead;
1525
1526 buslogicR3ReadSGEntries(pTaskState, GCPhysAddrScatterGatherCurrent, cScatterGatherGCRead, aScatterGatherReadGC);
1527
1528 for (iScatterGatherEntry = 0; iScatterGatherEntry < cScatterGatherGCRead; iScatterGatherEntry++)
1529 {
1530 RTGCPHYS GCPhysAddrDataBase;
1531 size_t cbDataToTransfer;
1532
1533 Log(("%s: iScatterGatherEntry=%u\n", __FUNCTION__, iScatterGatherEntry));
1534
1535 GCPhysAddrDataBase = (RTGCPHYS)aScatterGatherReadGC[iScatterGatherEntry].u32PhysAddrSegmentBase;
1536 cbDataToTransfer = aScatterGatherReadGC[iScatterGatherEntry].cbSegment;
1537
1538 Log(("%s: GCPhysAddrDataBase=%RGp cbDataToTransfer=%u\n", __FUNCTION__, GCPhysAddrDataBase, cbDataToTransfer));
1539
1540 PDMDevHlpPCIPhysWrite(pDevIns, GCPhysAddrDataBase, pbData, cbDataToTransfer);
1541 pbData += cbDataToTransfer;
1542 }
1543
1544 /* Set address to the next entries to read. */
1545 GCPhysAddrScatterGatherCurrent += cScatterGatherGCRead * pTaskState->cbSGEntry;
1546 } while (cScatterGatherGCLeft > 0);
1547
1548 }
1549 else if ( pTaskState->CommandControlBlockGuest.c.uOpcode == BUSLOGIC_CCB_OPCODE_INITIATOR_CCB
1550 || pTaskState->CommandControlBlockGuest.c.uOpcode == BUSLOGIC_CCB_OPCODE_INITIATOR_CCB_RESIDUAL_DATA_LENGTH)
1551 {
1552 /* The buffer is not scattered. */
1553 RTGCPHYS GCPhysAddrDataBase = u32PhysAddrCCB;
1554
1555 AssertMsg(GCPhysAddrDataBase != 0, ("Physical address is 0\n"));
1556
1557 Log(("Non-scattered buffer:\n"));
1558 Log(("u32PhysAddrData=%#x\n", u32PhysAddrCCB));
1559 Log(("cbData=%u\n", cbDataCCB));
1560 Log(("GCPhysAddrDataBase=0x%RGp\n", GCPhysAddrDataBase));
1561
1562 /* Copy the data into the guest memory. */
1563 PDMDevHlpPCIPhysWrite(pDevIns, GCPhysAddrDataBase, pTaskState->DataSeg.pvSeg, pTaskState->DataSeg.cbSeg);
1564 }
1565
1566 }
1567 /* Update residual data length. */
1568 if ( (pTaskState->CommandControlBlockGuest.c.uOpcode == BUSLOGIC_CCB_OPCODE_INITIATOR_CCB_RESIDUAL_DATA_LENGTH)
1569 || (pTaskState->CommandControlBlockGuest.c.uOpcode == BUSLOGIC_CCB_OPCODE_INITIATOR_CCB_RESIDUAL_SCATTER_GATHER))
1570 {
1571 uint32_t cbResidual;
1572
1573 /** @todo we need to get the actual transfer length from the VSCSI layer?! */
1574 cbResidual = 0; //LEN_TO_U32(pTaskState->CCBGuest.acbData) - ???;
1575 if (pTaskState->fIs24Bit)
1576 U32_TO_LEN(pTaskState->CommandControlBlockGuest.o.acbData, cbResidual);
1577 else
1578 pTaskState->CommandControlBlockGuest.n.cbData = cbResidual;
1579 }
1580
1581 RTMemFree(pTaskState->DataSeg.pvSeg);
1582 pTaskState->DataSeg.pvSeg = NULL;
1583 pTaskState->DataSeg.cbSeg = 0;
1584}
1585
1586/** Convert sense buffer length taking into account shortcut values. */
1587static uint32_t buslogicR3ConvertSenseBufferLength(uint32_t cbSense)
1588{
1589 /* Convert special sense buffer length values. */
1590 if (cbSense == 0)
1591 cbSense = 14; /* 0 means standard 14-byte buffer. */
1592 else if (cbSense == 1)
1593 cbSense = 0; /* 1 means no sense data. */
1594 else if (cbSense < 8)
1595 AssertMsgFailed(("Reserved cbSense value of %d used!\n", cbSense));
1596
1597 return cbSense;
1598}
1599
1600/**
1601 * Free the sense buffer.
1602 *
1603 * @returns nothing.
1604 * @param pTaskState Pointer to the task state.
1605 * @param fCopy If sense data should be copied to guest memory.
1606 */
1607static void buslogicR3SenseBufferFree(PBUSLOGICTASKSTATE pTaskState, bool fCopy)
1608{
1609 uint32_t cbSenseBuffer;
1610
1611 cbSenseBuffer = buslogicR3ConvertSenseBufferLength(pTaskState->CommandControlBlockGuest.c.cbSenseData);
1612
1613 /* Copy the sense buffer into guest memory if requested. */
1614 if (fCopy && cbSenseBuffer)
1615 {
1616 PPDMDEVINS pDevIns = pTaskState->CTX_SUFF(pTargetDevice)->CTX_SUFF(pBusLogic)->CTX_SUFF(pDevIns);
1617 RTGCPHYS GCPhysAddrSenseBuffer;
1618
1619 /* With 32-bit CCBs, the (optional) sense buffer physical address is provided separately.
1620 * On the other hand, with 24-bit CCBs, the sense buffer is simply located at the end of
1621 * the CCB, right after the variable-length CDB.
1622 */
1623 if (pTaskState->fIs24Bit)
1624 {
1625 GCPhysAddrSenseBuffer = pTaskState->MailboxGuest.u32PhysAddrCCB;
1626 GCPhysAddrSenseBuffer += pTaskState->CommandControlBlockGuest.c.cbCDB + RT_OFFSETOF(CCB24, abCDB);
1627 }
1628 else
1629 GCPhysAddrSenseBuffer = pTaskState->CommandControlBlockGuest.n.u32PhysAddrSenseData;
1630
1631 PDMDevHlpPCIPhysWrite(pDevIns, GCPhysAddrSenseBuffer, pTaskState->pbSenseBuffer, cbSenseBuffer);
1632 }
1633
1634 RTMemFree(pTaskState->pbSenseBuffer);
1635 pTaskState->pbSenseBuffer = NULL;
1636}
1637
1638/**
1639 * Alloc the sense buffer.
1640 *
1641 * @returns VBox status code.
1642 * @param pTaskState Pointer to the task state.
1643 * @note Current assumption is that the sense buffer is not scattered and does not cross a page boundary.
1644 */
1645static int buslogicR3SenseBufferAlloc(PBUSLOGICTASKSTATE pTaskState)
1646{
1647 PPDMDEVINS pDevIns = pTaskState->CTX_SUFF(pTargetDevice)->CTX_SUFF(pBusLogic)->CTX_SUFF(pDevIns);
1648 uint32_t cbSenseBuffer;
1649
1650 pTaskState->pbSenseBuffer = NULL;
1651
1652 cbSenseBuffer = buslogicR3ConvertSenseBufferLength(pTaskState->CommandControlBlockGuest.c.cbSenseData);
1653 if (cbSenseBuffer)
1654 {
1655 pTaskState->pbSenseBuffer = (uint8_t *)RTMemAllocZ(cbSenseBuffer);
1656 if (!pTaskState->pbSenseBuffer)
1657 return VERR_NO_MEMORY;
1658 }
1659
1660 return VINF_SUCCESS;
1661}
1662
1663#endif /* IN_RING3 */
1664
1665/**
1666 * Parses the command buffer and executes it.
1667 *
1668 * @returns VBox status code.
1669 * @param pBusLogic Pointer to the BusLogic device instance.
1670 */
1671static int buslogicProcessCommand(PBUSLOGIC pBusLogic)
1672{
1673 int rc = VINF_SUCCESS;
1674 bool fSuppressIrq = false;
1675
1676 LogFlowFunc(("pBusLogic=%#p\n", pBusLogic));
1677 AssertMsg(pBusLogic->uOperationCode != 0xff, ("There is no command to execute\n"));
1678
1679 switch (pBusLogic->uOperationCode)
1680 {
1681 case BUSLOGICCOMMAND_TEST_CMDC_INTERRUPT:
1682 /* Valid command, no reply. */
1683 pBusLogic->cbReplyParametersLeft = 0;
1684 break;
1685 case BUSLOGICCOMMAND_INQUIRE_PCI_HOST_ADAPTER_INFORMATION:
1686 {
1687 PReplyInquirePCIHostAdapterInformation pReply = (PReplyInquirePCIHostAdapterInformation)pBusLogic->aReplyBuffer;
1688 memset(pReply, 0, sizeof(ReplyInquirePCIHostAdapterInformation));
1689
1690 /* It seems VMware does not provide valid information here too, lets do the same :) */
1691 pReply->InformationIsValid = 0;
1692 pReply->IsaIOPort = pBusLogic->uISABaseCode;
1693 pReply->IRQ = PCIDevGetInterruptLine(&pBusLogic->dev);
1694 pBusLogic->cbReplyParametersLeft = sizeof(ReplyInquirePCIHostAdapterInformation);
1695 break;
1696 }
1697 case BUSLOGICCOMMAND_MODIFY_IO_ADDRESS:
1698 {
1699 /* Modify the ISA-compatible I/O port base. Note that this technically
1700 * violates the PCI spec, as this address is not reported through PCI.
1701 * However, it is required for compatibility with old drivers.
1702 */
1703#ifdef IN_RING3
1704 Log(("ISA I/O for PCI (code %x)\n", pBusLogic->aCommandBuffer[0]));
1705 buslogicR3RegisterISARange(pBusLogic, pBusLogic->aCommandBuffer[0]);
1706 pBusLogic->cbReplyParametersLeft = 0;
1707 fSuppressIrq = true;
1708 break;
1709#else
1710 AssertMsgFailed(("Must never get here!\n"));
1711#endif
1712 }
1713 case BUSLOGICCOMMAND_INQUIRE_BOARD_ID:
1714 {
1715 /* The special option byte is important: If it is '0' or 'B', Windows NT drivers
1716 * for Adaptec AHA-154x may claim the adapter. The BusLogic drivers will claim
1717 * the adapter only when the byte is *not* '0' or 'B'.
1718 */
1719 pBusLogic->aReplyBuffer[0] = 'A'; /* Firmware option bytes */
1720 pBusLogic->aReplyBuffer[1] = 'A'; /* Special option byte */
1721
1722 /* We report version 5.07B. This reply will provide the first two digits. */
1723 pBusLogic->aReplyBuffer[2] = '5'; /* Major version 5 */
1724 pBusLogic->aReplyBuffer[3] = '0'; /* Minor version 0 */
1725 pBusLogic->cbReplyParametersLeft = 4; /* Reply is 4 bytes long */
1726 break;
1727 }
1728 case BUSLOGICCOMMAND_INQUIRE_FIRMWARE_VERSION_3RD_LETTER:
1729 {
1730 pBusLogic->aReplyBuffer[0] = '7';
1731 pBusLogic->cbReplyParametersLeft = 1;
1732 break;
1733 }
1734 case BUSLOGICCOMMAND_INQUIRE_FIRMWARE_VERSION_LETTER:
1735 {
1736 pBusLogic->aReplyBuffer[0] = 'B';
1737 pBusLogic->cbReplyParametersLeft = 1;
1738 break;
1739 }
1740 case BUSLOGICCOMMAND_SET_ADAPTER_OPTIONS:
1741 /* The parameter list length is determined by the first byte of the command buffer. */
1742 if (pBusLogic->iParameter == 1)
1743 {
1744 /* First pass - set the number of following parameter bytes. */
1745 pBusLogic->cbCommandParametersLeft = pBusLogic->aCommandBuffer[0];
1746 Log(("Set HA options: %u bytes follow\n", pBusLogic->cbCommandParametersLeft));
1747 }
1748 else
1749 {
1750 /* Second pass - process received data. */
1751 Log(("Set HA options: received %u bytes\n", pBusLogic->aCommandBuffer[0]));
1752 /* We ignore the data - it only concerns the SCSI hardware protocol. */
1753 }
1754 pBusLogic->cbReplyParametersLeft = 0;
1755 break;
1756
1757 case BUSLOGICCOMMAND_INQUIRE_HOST_ADAPTER_MODEL_NUMBER:
1758 {
1759 /* The reply length is set by the guest and is found in the first byte of the command buffer. */
1760 pBusLogic->cbReplyParametersLeft = pBusLogic->aCommandBuffer[0];
1761 memset(pBusLogic->aReplyBuffer, ' ', pBusLogic->cbReplyParametersLeft);
1762 const char aModelName[] = "958";
1763 int cCharsToTransfer = (pBusLogic->cbReplyParametersLeft <= (sizeof(aModelName) - 1))
1764 ? pBusLogic->cbReplyParametersLeft
1765 : sizeof(aModelName) - 1;
1766
1767 for (int i = 0; i < cCharsToTransfer; i++)
1768 pBusLogic->aReplyBuffer[i] = aModelName[i];
1769
1770 break;
1771 }
1772 case BUSLOGICCOMMAND_INQUIRE_CONFIGURATION:
1773 {
1774 uint8_t uPciIrq = PCIDevGetInterruptLine(&pBusLogic->dev);
1775
1776 pBusLogic->cbReplyParametersLeft = sizeof(ReplyInquireConfiguration);
1777 PReplyInquireConfiguration pReply = (PReplyInquireConfiguration)pBusLogic->aReplyBuffer;
1778 memset(pReply, 0, sizeof(ReplyInquireConfiguration));
1779
1780 pReply->uHostAdapterId = 7; /* The controller has always 7 as ID. */
1781 pReply->fDmaChannel6 = 1; /* DMA channel 6 is a good default. */
1782 /* The PCI IRQ is not necessarily representable in this structure.
1783 * If that is the case, the guest likely won't function correctly,
1784 * therefore we log a warning.
1785 */
1786 switch (uPciIrq)
1787 {
1788 case 9: pReply->fIrqChannel9 = 1; break;
1789 case 10: pReply->fIrqChannel10 = 1; break;
1790 case 11: pReply->fIrqChannel11 = 1; break;
1791 case 12: pReply->fIrqChannel12 = 1; break;
1792 case 14: pReply->fIrqChannel14 = 1; break;
1793 case 15: pReply->fIrqChannel15 = 1; break;
1794 default:
1795 LogRel(("Warning: PCI IRQ %d cannot be represented as ISA!\n", uPciIrq));
1796 break;
1797 }
1798 break;
1799 }
1800 case BUSLOGICCOMMAND_INQUIRE_EXTENDED_SETUP_INFORMATION:
1801 {
1802 /* Some Adaptec AHA-154x drivers (e.g. OS/2) execute this command and expect
1803 * it to fail. If it succeeds, the drivers refuse to load. However, some newer
1804 * Adaptec 154x models supposedly support it too??
1805 */
1806
1807 /* The reply length is set by the guest and is found in the first byte of the command buffer. */
1808 pBusLogic->cbReplyParametersLeft = pBusLogic->aCommandBuffer[0];
1809 PReplyInquireExtendedSetupInformation pReply = (PReplyInquireExtendedSetupInformation)pBusLogic->aReplyBuffer;
1810 memset(pReply, 0, sizeof(ReplyInquireExtendedSetupInformation));
1811
1812 /** @todo should this reflect the RAM contents (AutoSCSIRam)? */
1813 pReply->uBusType = 'E'; /* EISA style */
1814 pReply->u16ScatterGatherLimit = 8192;
1815 pReply->cMailbox = pBusLogic->cMailbox;
1816 pReply->uMailboxAddressBase = (uint32_t)pBusLogic->GCPhysAddrMailboxOutgoingBase;
1817 pReply->fLevelSensitiveInterrupt = true;
1818 pReply->fHostWideSCSI = true;
1819 pReply->fHostUltraSCSI = true;
1820 memcpy(pReply->aFirmwareRevision, "07B", sizeof(pReply->aFirmwareRevision));
1821
1822 break;
1823 }
1824 case BUSLOGICCOMMAND_INQUIRE_SETUP_INFORMATION:
1825 {
1826 /* The reply length is set by the guest and is found in the first byte of the command buffer. */
1827 pBusLogic->cbReplyParametersLeft = pBusLogic->aCommandBuffer[0];
1828 PReplyInquireSetupInformation pReply = (PReplyInquireSetupInformation)pBusLogic->aReplyBuffer;
1829 memset(pReply, 0, sizeof(ReplyInquireSetupInformation));
1830 pReply->fSynchronousInitiationEnabled = true;
1831 pReply->fParityCheckingEnabled = true;
1832 pReply->cMailbox = pBusLogic->cMailbox;
1833 U32_TO_ADDR(pReply->MailboxAddress, pBusLogic->GCPhysAddrMailboxOutgoingBase);
1834 pReply->uSignature = 'B';
1835 /* The 'D' signature prevents Adaptec's OS/2 drivers from getting too
1836 * friendly with BusLogic hardware and upsetting the HBA state.
1837 */
1838 pReply->uCharacterD = 'D'; /* BusLogic model. */
1839 pReply->uHostBusType = 'F'; /* PCI bus. */
1840 break;
1841 }
1842 case BUSLOGICCOMMAND_FETCH_HOST_ADAPTER_LOCAL_RAM:
1843 {
1844 /*
1845 * First element in the command buffer contains start offset to read from
1846 * and second one the number of bytes to read.
1847 */
1848 uint8_t uOffset = pBusLogic->aCommandBuffer[0];
1849 pBusLogic->cbReplyParametersLeft = pBusLogic->aCommandBuffer[1];
1850
1851 pBusLogic->fUseLocalRam = true;
1852 pBusLogic->iReply = uOffset;
1853 break;
1854 }
1855 case BUSLOGICCOMMAND_INITIALIZE_MAILBOX:
1856 {
1857 PRequestInitMbx pRequest = (PRequestInitMbx)pBusLogic->aCommandBuffer;
1858
1859 pBusLogic->fMbxIs24Bit = true;
1860 pBusLogic->cMailbox = pRequest->cMailbox;
1861 pBusLogic->GCPhysAddrMailboxOutgoingBase = (RTGCPHYS)ADDR_TO_U32(pRequest->aMailboxBaseAddr);
1862 /* The area for incoming mailboxes is right after the last entry of outgoing mailboxes. */
1863 pBusLogic->GCPhysAddrMailboxIncomingBase = pBusLogic->GCPhysAddrMailboxOutgoingBase + (pBusLogic->cMailbox * sizeof(Mailbox24));
1864
1865 Log(("GCPhysAddrMailboxOutgoingBase=%RGp\n", pBusLogic->GCPhysAddrMailboxOutgoingBase));
1866 Log(("GCPhysAddrMailboxIncomingBase=%RGp\n", pBusLogic->GCPhysAddrMailboxIncomingBase));
1867 Log(("cMailboxes=%u (24-bit mode)\n", pBusLogic->cMailbox));
1868 LogRel(("Initialized 24-bit mailbox, %d entries at %08x\n", pRequest->cMailbox, ADDR_TO_U32(pRequest->aMailboxBaseAddr)));
1869
1870 pBusLogic->regStatus &= ~BUSLOGIC_REGISTER_STATUS_INITIALIZATION_REQUIRED;
1871 pBusLogic->cbReplyParametersLeft = 0;
1872 break;
1873 }
1874 case BUSLOGICCOMMAND_INITIALIZE_EXTENDED_MAILBOX:
1875 {
1876 PRequestInitializeExtendedMailbox pRequest = (PRequestInitializeExtendedMailbox)pBusLogic->aCommandBuffer;
1877
1878 pBusLogic->fMbxIs24Bit = false;
1879 pBusLogic->cMailbox = pRequest->cMailbox;
1880 pBusLogic->GCPhysAddrMailboxOutgoingBase = (RTGCPHYS)pRequest->uMailboxBaseAddress;
1881 /* The area for incoming mailboxes is right after the last entry of outgoing mailboxes. */
1882 pBusLogic->GCPhysAddrMailboxIncomingBase = (RTGCPHYS)pRequest->uMailboxBaseAddress + (pBusLogic->cMailbox * sizeof(Mailbox32));
1883
1884 Log(("GCPhysAddrMailboxOutgoingBase=%RGp\n", pBusLogic->GCPhysAddrMailboxOutgoingBase));
1885 Log(("GCPhysAddrMailboxIncomingBase=%RGp\n", pBusLogic->GCPhysAddrMailboxIncomingBase));
1886 Log(("cMailboxes=%u (32-bit mode)\n", pBusLogic->cMailbox));
1887 LogRel(("Initialized 32-bit mailbox, %d entries at %08x\n", pRequest->cMailbox, pRequest->uMailboxBaseAddress));
1888
1889 pBusLogic->regStatus &= ~BUSLOGIC_REGISTER_STATUS_INITIALIZATION_REQUIRED;
1890 pBusLogic->cbReplyParametersLeft = 0;
1891 break;
1892 }
1893 case BUSLOGICCOMMAND_ENABLE_STRICT_ROUND_ROBIN_MODE:
1894 {
1895 if (pBusLogic->aCommandBuffer[0] == 0)
1896 pBusLogic->fStrictRoundRobinMode = false;
1897 else if (pBusLogic->aCommandBuffer[0] == 1)
1898 pBusLogic->fStrictRoundRobinMode = true;
1899 else
1900 AssertMsgFailed(("Invalid round robin mode %d\n", pBusLogic->aCommandBuffer[0]));
1901
1902 pBusLogic->cbReplyParametersLeft = 0;
1903 break;
1904 }
1905 case BUSLOGICCOMMAND_SET_CCB_FORMAT:
1906 {
1907 if (pBusLogic->aCommandBuffer[0] == 0)
1908 pBusLogic->fExtendedLunCCBFormat = false;
1909 else if (pBusLogic->aCommandBuffer[0] == 1)
1910 pBusLogic->fExtendedLunCCBFormat = true;
1911 else
1912 AssertMsgFailed(("Invalid CCB format %d\n", pBusLogic->aCommandBuffer[0]));
1913
1914 pBusLogic->cbReplyParametersLeft = 0;
1915 break;
1916 }
1917 case BUSLOGICCOMMAND_INQUIRE_INSTALLED_DEVICES_ID_0_TO_7:
1918 /* This is supposed to send TEST UNIT READY to each target/LUN.
1919 * We cheat and skip that, since we already know what's attached
1920 */
1921 memset(pBusLogic->aReplyBuffer, 0, 8);
1922 for (int i = 0; i < 8; ++i)
1923 {
1924 if (pBusLogic->aDeviceStates[i].fPresent)
1925 pBusLogic->aReplyBuffer[i] = 1;
1926 }
1927 pBusLogic->aReplyBuffer[7] = 0; /* HA hardcoded at ID 7. */
1928 pBusLogic->cbReplyParametersLeft = 8;
1929 break;
1930 case BUSLOGICCOMMAND_INQUIRE_INSTALLED_DEVICES_ID_8_TO_15:
1931 /* See note about cheating above. */
1932 memset(pBusLogic->aReplyBuffer, 0, 8);
1933 for (int i = 0; i < 8; ++i)
1934 {
1935 if (pBusLogic->aDeviceStates[i + 8].fPresent)
1936 pBusLogic->aReplyBuffer[i] = 1;
1937 }
1938 pBusLogic->cbReplyParametersLeft = 8;
1939 break;
1940 case BUSLOGICCOMMAND_INQUIRE_TARGET_DEVICES:
1941 {
1942 /* Each bit which is set in the 16bit wide variable means a present device. */
1943 uint16_t u16TargetsPresentMask = 0;
1944
1945 for (uint8_t i = 0; i < RT_ELEMENTS(pBusLogic->aDeviceStates); i++)
1946 {
1947 if (pBusLogic->aDeviceStates[i].fPresent)
1948 u16TargetsPresentMask |= (1 << i);
1949 }
1950 pBusLogic->aReplyBuffer[0] = (uint8_t)u16TargetsPresentMask;
1951 pBusLogic->aReplyBuffer[1] = (uint8_t)(u16TargetsPresentMask >> 8);
1952 pBusLogic->cbReplyParametersLeft = 2;
1953 break;
1954 }
1955 case BUSLOGICCOMMAND_INQUIRE_SYNCHRONOUS_PERIOD:
1956 {
1957 pBusLogic->cbReplyParametersLeft = pBusLogic->aCommandBuffer[0];
1958
1959 for (uint8_t i = 0; i < pBusLogic->cbReplyParametersLeft; i++)
1960 pBusLogic->aReplyBuffer[i] = 0; /** @todo Figure if we need something other here. It's not needed for the linux driver */
1961
1962 break;
1963 }
1964 case BUSLOGICCOMMAND_DISABLE_HOST_ADAPTER_INTERRUPT:
1965 {
1966 if (pBusLogic->aCommandBuffer[0] == 0)
1967 pBusLogic->fIRQEnabled = false;
1968 else
1969 pBusLogic->fIRQEnabled = true;
1970 /* No interrupt signaled regardless of enable/disable. */
1971 fSuppressIrq = true;
1972 break;
1973 }
1974 case BUSLOGICCOMMAND_ECHO_COMMAND_DATA:
1975 {
1976 pBusLogic->aReplyBuffer[0] = pBusLogic->aCommandBuffer[0];
1977 pBusLogic->cbReplyParametersLeft = 1;
1978 break;
1979 }
1980 case BUSLOGICCOMMAND_SET_PREEMPT_TIME_ON_BUS:
1981 {
1982 pBusLogic->cbReplyParametersLeft = 0;
1983 pBusLogic->LocalRam.structured.autoSCSIData.uBusOnDelay = pBusLogic->aCommandBuffer[0];
1984 Log(("Bus-on time: %d\n", pBusLogic->aCommandBuffer[0]));
1985 break;
1986 }
1987 case BUSLOGICCOMMAND_SET_TIME_OFF_BUS:
1988 {
1989 pBusLogic->cbReplyParametersLeft = 0;
1990 pBusLogic->LocalRam.structured.autoSCSIData.uBusOffDelay = pBusLogic->aCommandBuffer[0];
1991 Log(("Bus-off time: %d\n", pBusLogic->aCommandBuffer[0]));
1992 break;
1993 }
1994 case BUSLOGICCOMMAND_SET_BUS_TRANSFER_RATE:
1995 {
1996 pBusLogic->cbReplyParametersLeft = 0;
1997 pBusLogic->LocalRam.structured.autoSCSIData.uDMATransferRate = pBusLogic->aCommandBuffer[0];
1998 Log(("Bus transfer rate: %02X\n", pBusLogic->aCommandBuffer[0]));
1999 break;
2000 }
2001 default:
2002 AssertMsgFailed(("Invalid command %#x\n", pBusLogic->uOperationCode));
2003 case BUSLOGICCOMMAND_EXT_BIOS_INFO:
2004 case BUSLOGICCOMMAND_UNLOCK_MAILBOX:
2005 /* Commands valid for Adaptec 154xC which we don't handle since
2006 * we pretend being 154xB compatible. Just mark the command as invalid.
2007 */
2008 Log(("Command %#x not valid for this adapter\n", pBusLogic->uOperationCode));
2009 pBusLogic->cbReplyParametersLeft = 0;
2010 pBusLogic->regStatus |= BUSLOGIC_REGISTER_STATUS_COMMAND_INVALID;
2011 break;
2012 case BUSLOGICCOMMAND_EXECUTE_MAILBOX_COMMAND: /* Should be handled already. */
2013 AssertMsgFailed(("Invalid mailbox execute state!\n"));
2014 }
2015
2016 Log(("uOperationCode=%#x, cbReplyParametersLeft=%d\n", pBusLogic->uOperationCode, pBusLogic->cbReplyParametersLeft));
2017
2018 /* Set the data in ready bit in the status register in case the command has a reply. */
2019 if (pBusLogic->cbReplyParametersLeft)
2020 pBusLogic->regStatus |= BUSLOGIC_REGISTER_STATUS_DATA_IN_REGISTER_READY;
2021 else if (!pBusLogic->cbCommandParametersLeft)
2022 buslogicCommandComplete(pBusLogic, fSuppressIrq);
2023
2024 return rc;
2025}
2026
2027/**
2028 * Read a register from the BusLogic adapter.
2029 *
2030 * @returns VBox status code.
2031 * @param pBusLogic Pointer to the BusLogic instance data.
2032 * @param iRegister The index of the register to read.
2033 * @param pu32 Where to store the register content.
2034 */
2035static int buslogicRegisterRead(PBUSLOGIC pBusLogic, unsigned iRegister, uint32_t *pu32)
2036{
2037 int rc = VINF_SUCCESS;
2038
2039 switch (iRegister)
2040 {
2041 case BUSLOGIC_REGISTER_STATUS:
2042 {
2043 *pu32 = pBusLogic->regStatus;
2044
2045 /* If the diagnostic active bit is set, we are in a guest-initiated
2046 * hard reset. If the guest reads the status register and waits for
2047 * the host adapter ready bit to be set, we terminate the reset right
2048 * away. However, guests may also expect the reset condition to clear
2049 * automatically after a period of time, in which case we can't show
2050 * the DIAG bit at all.
2051 */
2052 if (pBusLogic->regStatus & BUSLOGIC_REGISTER_STATUS_DIAGNOSTIC_ACTIVE)
2053 {
2054 uint64_t u64AccessTime = PDMDevHlpTMTimeVirtGetNano(pBusLogic->CTX_SUFF(pDevIns));
2055
2056 pBusLogic->regStatus &= ~BUSLOGIC_REGISTER_STATUS_DIAGNOSTIC_ACTIVE;
2057 pBusLogic->regStatus |= BUSLOGIC_REGISTER_STATUS_HOST_ADAPTER_READY;
2058
2059 if (u64AccessTime - pBusLogic->u64ResetTime > BUSLOGIC_RESET_DURATION_NS)
2060 {
2061 /* If reset already expired, let the guest see that right away. */
2062 *pu32 = pBusLogic->regStatus;
2063 pBusLogic->u64ResetTime = 0;
2064 }
2065 }
2066 break;
2067 }
2068 case BUSLOGIC_REGISTER_DATAIN:
2069 {
2070 if (pBusLogic->fUseLocalRam)
2071 *pu32 = pBusLogic->LocalRam.u8View[pBusLogic->iReply];
2072 else
2073 *pu32 = pBusLogic->aReplyBuffer[pBusLogic->iReply];
2074
2075 /* Careful about underflow - guest can read data register even if
2076 * no data is available.
2077 */
2078 if (pBusLogic->cbReplyParametersLeft)
2079 {
2080 pBusLogic->iReply++;
2081 pBusLogic->cbReplyParametersLeft--;
2082 if (!pBusLogic->cbReplyParametersLeft)
2083 {
2084 /*
2085 * Reply finished, set command complete bit, unset data-in ready bit and
2086 * interrupt the guest if enabled.
2087 */
2088 buslogicCommandComplete(pBusLogic, false);
2089 }
2090 }
2091 LogFlowFunc(("data=%02x, iReply=%d, cbReplyParametersLeft=%u\n", *pu32,
2092 pBusLogic->iReply, pBusLogic->cbReplyParametersLeft));
2093 break;
2094 }
2095 case BUSLOGIC_REGISTER_INTERRUPT:
2096 {
2097 *pu32 = pBusLogic->regInterrupt;
2098 break;
2099 }
2100 case BUSLOGIC_REGISTER_GEOMETRY:
2101 {
2102 *pu32 = pBusLogic->regGeometry;
2103 break;
2104 }
2105 default:
2106 *pu32 = UINT32_C(0xffffffff);
2107 }
2108
2109 Log2(("%s: pu32=%p:{%.*Rhxs} iRegister=%d rc=%Rrc\n",
2110 __FUNCTION__, pu32, 1, pu32, iRegister, rc));
2111
2112 return rc;
2113}
2114
2115/**
2116 * Write a value to a register.
2117 *
2118 * @returns VBox status code.
2119 * @param pBusLogic Pointer to the BusLogic instance data.
2120 * @param iRegister The index of the register to read.
2121 * @param uVal The value to write.
2122 */
2123static int buslogicRegisterWrite(PBUSLOGIC pBusLogic, unsigned iRegister, uint8_t uVal)
2124{
2125 int rc = VINF_SUCCESS;
2126
2127 switch (iRegister)
2128 {
2129 case BUSLOGIC_REGISTER_CONTROL:
2130 {
2131 if ((uVal & BUSLOGIC_REGISTER_CONTROL_HARD_RESET) || (uVal & BUSLOGIC_REGISTER_CONTROL_SOFT_RESET))
2132 {
2133#ifdef IN_RING3
2134 bool fHardReset = !!(uVal & BUSLOGIC_REGISTER_CONTROL_HARD_RESET);
2135
2136 LogRel(("BusLogic: %s reset\n", fHardReset ? "hard" : "soft"));
2137 buslogicR3InitiateReset(pBusLogic, fHardReset);
2138#else
2139 rc = VINF_IOM_R3_IOPORT_WRITE;
2140#endif
2141 break;
2142 }
2143
2144 rc = PDMCritSectEnter(&pBusLogic->CritSectIntr, VINF_IOM_R3_IOPORT_WRITE);
2145 if (rc != VINF_SUCCESS)
2146 return rc;
2147
2148#ifdef LOG_ENABLED
2149 uint32_t cMailboxesReady = ASMAtomicXchgU32(&pBusLogic->cInMailboxesReady, 0);
2150 Log(("%u incoming mailboxes were ready when this interrupt was cleared\n", cMailboxesReady));
2151#endif
2152
2153 if (uVal & BUSLOGIC_REGISTER_CONTROL_INTERRUPT_RESET)
2154 buslogicClearInterrupt(pBusLogic);
2155
2156 PDMCritSectLeave(&pBusLogic->CritSectIntr);
2157
2158 break;
2159 }
2160 case BUSLOGIC_REGISTER_COMMAND:
2161 {
2162 /* Fast path for mailbox execution command. */
2163 if ((uVal == BUSLOGICCOMMAND_EXECUTE_MAILBOX_COMMAND) && (pBusLogic->uOperationCode == 0xff))
2164 {
2165 /* If there are no mailboxes configured, don't even try to do anything. */
2166 if (pBusLogic->cMailbox) {
2167 ASMAtomicIncU32(&pBusLogic->cMailboxesReady);
2168 if (!ASMAtomicXchgBool(&pBusLogic->fNotificationSend, true))
2169 {
2170 /* Send new notification to the queue. */
2171 PPDMQUEUEITEMCORE pItem = PDMQueueAlloc(pBusLogic->CTX_SUFF(pNotifierQueue));
2172 AssertMsg(pItem, ("Allocating item for queue failed\n"));
2173 PDMQueueInsert(pBusLogic->CTX_SUFF(pNotifierQueue), (PPDMQUEUEITEMCORE)pItem);
2174 }
2175 }
2176
2177 return rc;
2178 }
2179
2180 /*
2181 * Check if we are already fetch command parameters from the guest.
2182 * If not we initialize executing a new command.
2183 */
2184 if (pBusLogic->uOperationCode == 0xff)
2185 {
2186 pBusLogic->uOperationCode = uVal;
2187 pBusLogic->iParameter = 0;
2188
2189 /* Mark host adapter as busy and clear the invalid status bit. */
2190 pBusLogic->regStatus &= ~(BUSLOGIC_REGISTER_STATUS_HOST_ADAPTER_READY | BUSLOGIC_REGISTER_STATUS_COMMAND_INVALID);
2191
2192 /* Get the number of bytes for parameters from the command code. */
2193 switch (pBusLogic->uOperationCode)
2194 {
2195 case BUSLOGICCOMMAND_TEST_CMDC_INTERRUPT:
2196 case BUSLOGICCOMMAND_INQUIRE_FIRMWARE_VERSION_LETTER:
2197 case BUSLOGICCOMMAND_INQUIRE_BOARD_ID:
2198 case BUSLOGICCOMMAND_INQUIRE_FIRMWARE_VERSION_3RD_LETTER:
2199 case BUSLOGICCOMMAND_INQUIRE_PCI_HOST_ADAPTER_INFORMATION:
2200 case BUSLOGICCOMMAND_INQUIRE_CONFIGURATION:
2201 case BUSLOGICCOMMAND_INQUIRE_INSTALLED_DEVICES_ID_0_TO_7:
2202 case BUSLOGICCOMMAND_INQUIRE_INSTALLED_DEVICES_ID_8_TO_15:
2203 case BUSLOGICCOMMAND_INQUIRE_TARGET_DEVICES:
2204 pBusLogic->cbCommandParametersLeft = 0;
2205 break;
2206 case BUSLOGICCOMMAND_MODIFY_IO_ADDRESS:
2207 case BUSLOGICCOMMAND_INQUIRE_EXTENDED_SETUP_INFORMATION:
2208 case BUSLOGICCOMMAND_INQUIRE_SETUP_INFORMATION:
2209 case BUSLOGICCOMMAND_INQUIRE_HOST_ADAPTER_MODEL_NUMBER:
2210 case BUSLOGICCOMMAND_ENABLE_STRICT_ROUND_ROBIN_MODE:
2211 case BUSLOGICCOMMAND_SET_CCB_FORMAT:
2212 case BUSLOGICCOMMAND_INQUIRE_SYNCHRONOUS_PERIOD:
2213 case BUSLOGICCOMMAND_DISABLE_HOST_ADAPTER_INTERRUPT:
2214 case BUSLOGICCOMMAND_ECHO_COMMAND_DATA:
2215 case BUSLOGICCOMMAND_SET_PREEMPT_TIME_ON_BUS:
2216 case BUSLOGICCOMMAND_SET_TIME_OFF_BUS:
2217 case BUSLOGICCOMMAND_SET_BUS_TRANSFER_RATE:
2218 pBusLogic->cbCommandParametersLeft = 1;
2219 break;
2220 case BUSLOGICCOMMAND_FETCH_HOST_ADAPTER_LOCAL_RAM:
2221 pBusLogic->cbCommandParametersLeft = 2;
2222 break;
2223 case BUSLOGICCOMMAND_INITIALIZE_MAILBOX:
2224 pBusLogic->cbCommandParametersLeft = sizeof(RequestInitMbx);
2225 break;
2226 case BUSLOGICCOMMAND_INITIALIZE_EXTENDED_MAILBOX:
2227 pBusLogic->cbCommandParametersLeft = sizeof(RequestInitializeExtendedMailbox);
2228 break;
2229 case BUSLOGICCOMMAND_SET_ADAPTER_OPTIONS:
2230 /* There must be at least one byte following this command. */
2231 pBusLogic->cbCommandParametersLeft = 1;
2232 break;
2233 case BUSLOGICCOMMAND_EXT_BIOS_INFO:
2234 case BUSLOGICCOMMAND_UNLOCK_MAILBOX:
2235 /* Invalid commands. */
2236 pBusLogic->cbCommandParametersLeft = 0;
2237 break;
2238 case BUSLOGICCOMMAND_EXECUTE_MAILBOX_COMMAND: /* Should not come here anymore. */
2239 default:
2240 AssertMsgFailed(("Invalid operation code %#x\n", uVal));
2241 }
2242 }
2243 else
2244 {
2245#ifndef IN_RING3
2246 /* This command must be executed in R3 as it rehooks the ISA I/O port. */
2247 if (pBusLogic->uOperationCode == BUSLOGICCOMMAND_MODIFY_IO_ADDRESS)
2248 {
2249 rc = VINF_IOM_R3_IOPORT_WRITE;
2250 break;
2251 }
2252#endif
2253 /*
2254 * The real adapter would set the Command register busy bit in the status register.
2255 * The guest has to wait until it is unset.
2256 * We don't need to do it because the guest does not continue execution while we are in this
2257 * function.
2258 */
2259 pBusLogic->aCommandBuffer[pBusLogic->iParameter] = uVal;
2260 pBusLogic->iParameter++;
2261 pBusLogic->cbCommandParametersLeft--;
2262 }
2263
2264 /* Start execution of command if there are no parameters left. */
2265 if (!pBusLogic->cbCommandParametersLeft)
2266 {
2267 rc = buslogicProcessCommand(pBusLogic);
2268 AssertMsgRC(rc, ("Processing command failed rc=%Rrc\n", rc));
2269 }
2270 break;
2271 }
2272
2273 /* On BusLogic adapters, the interrupt and geometry registers are R/W.
2274 * That is different from Adaptec 154x where those are read only.
2275 */
2276 case BUSLOGIC_REGISTER_INTERRUPT:
2277 pBusLogic->regInterrupt = uVal;
2278 break;
2279
2280 case BUSLOGIC_REGISTER_GEOMETRY:
2281 pBusLogic->regGeometry = uVal;
2282 break;
2283
2284 default:
2285 AssertMsgFailed(("Register not available\n"));
2286 rc = VERR_IOM_IOPORT_UNUSED;
2287 }
2288
2289 return rc;
2290}
2291
2292/**
2293 * Memory mapped I/O Handler for read operations.
2294 *
2295 * @returns VBox status code.
2296 *
2297 * @param pDevIns The device instance.
2298 * @param pvUser User argument.
2299 * @param GCPhysAddr Physical address (in GC) where the read starts.
2300 * @param pv Where to store the result.
2301 * @param cb Number of bytes read.
2302 */
2303PDMBOTHCBDECL(int) buslogicMMIORead(PPDMDEVINS pDevIns, void *pvUser, RTGCPHYS GCPhysAddr, void *pv, unsigned cb)
2304{
2305 /* the linux driver does not make use of the MMIO area. */
2306 AssertMsgFailed(("MMIO Read\n"));
2307 return VINF_SUCCESS;
2308}
2309
2310/**
2311 * Memory mapped I/O Handler for write operations.
2312 *
2313 * @returns VBox status code.
2314 *
2315 * @param pDevIns The device instance.
2316 * @param pvUser User argument.
2317 * @param GCPhysAddr Physical address (in GC) where the read starts.
2318 * @param pv Where to fetch the result.
2319 * @param cb Number of bytes to write.
2320 */
2321PDMBOTHCBDECL(int) buslogicMMIOWrite(PPDMDEVINS pDevIns, void *pvUser, RTGCPHYS GCPhysAddr, void const *pv, unsigned cb)
2322{
2323 /* the linux driver does not make use of the MMIO area. */
2324 AssertMsgFailed(("MMIO Write\n"));
2325 return VINF_SUCCESS;
2326}
2327
2328/**
2329 * Port I/O Handler for IN operations.
2330 *
2331 * @returns VBox status code.
2332 *
2333 * @param pDevIns The device instance.
2334 * @param pvUser User argument.
2335 * @param uPort Port number used for the IN operation.
2336 * @param pu32 Where to store the result.
2337 * @param cb Number of bytes read.
2338 */
2339PDMBOTHCBDECL(int) buslogicIOPortRead(PPDMDEVINS pDevIns, void *pvUser, RTIOPORT Port, uint32_t *pu32, unsigned cb)
2340{
2341 PBUSLOGIC pBusLogic = PDMINS_2_DATA(pDevIns, PBUSLOGIC);
2342 unsigned iRegister = Port % 4;
2343
2344 Assert(cb == 1);
2345
2346 return buslogicRegisterRead(pBusLogic, iRegister, pu32);
2347}
2348
2349/**
2350 * Port I/O Handler for OUT operations.
2351 *
2352 * @returns VBox status code.
2353 *
2354 * @param pDevIns The device instance.
2355 * @param pvUser User argument.
2356 * @param uPort Port number used for the IN operation.
2357 * @param u32 The value to output.
2358 * @param cb The value size in bytes.
2359 */
2360PDMBOTHCBDECL(int) buslogicIOPortWrite(PPDMDEVINS pDevIns, void *pvUser, RTIOPORT Port, uint32_t u32, unsigned cb)
2361{
2362 PBUSLOGIC pBusLogic = PDMINS_2_DATA(pDevIns, PBUSLOGIC);
2363 int rc = VINF_SUCCESS;
2364 unsigned iRegister = Port % 4;
2365 uint8_t uVal = (uint8_t)u32;
2366
2367 Assert(cb == 1);
2368
2369 rc = buslogicRegisterWrite(pBusLogic, iRegister, (uint8_t)uVal);
2370
2371 Log2(("#%d %s: pvUser=%#p cb=%d u32=%#x Port=%#x rc=%Rrc\n",
2372 pDevIns->iInstance, __FUNCTION__, pvUser, cb, u32, Port, rc));
2373
2374 return rc;
2375}
2376
2377#ifdef IN_RING3
2378
2379static int buslogicR3PrepareBIOSSCSIRequest(PBUSLOGIC pBusLogic)
2380{
2381 int rc;
2382 PBUSLOGICTASKSTATE pTaskState;
2383 uint32_t uTargetDevice;
2384
2385 rc = RTMemCacheAllocEx(pBusLogic->hTaskCache, (void **)&pTaskState);
2386 AssertMsgRCReturn(rc, ("Getting task from cache failed rc=%Rrc\n", rc), rc);
2387
2388 pTaskState->fBIOS = true;
2389
2390 rc = vboxscsiSetupRequest(&pBusLogic->VBoxSCSI, &pTaskState->PDMScsiRequest, &uTargetDevice);
2391 AssertMsgRCReturn(rc, ("Setting up SCSI request failed rc=%Rrc\n", rc), rc);
2392
2393 pTaskState->PDMScsiRequest.pvUser = pTaskState;
2394
2395 pTaskState->CTX_SUFF(pTargetDevice) = &pBusLogic->aDeviceStates[uTargetDevice];
2396
2397 if (!pTaskState->CTX_SUFF(pTargetDevice)->fPresent)
2398 {
2399 /* Device is not present. */
2400 AssertMsg(pTaskState->PDMScsiRequest.pbCDB[0] == SCSI_INQUIRY,
2401 ("Device is not present but command is not inquiry\n"));
2402
2403 SCSIINQUIRYDATA ScsiInquiryData;
2404
2405 memset(&ScsiInquiryData, 0, sizeof(SCSIINQUIRYDATA));
2406 ScsiInquiryData.u5PeripheralDeviceType = SCSI_INQUIRY_DATA_PERIPHERAL_DEVICE_TYPE_UNKNOWN;
2407 ScsiInquiryData.u3PeripheralQualifier = SCSI_INQUIRY_DATA_PERIPHERAL_QUALIFIER_NOT_CONNECTED_NOT_SUPPORTED;
2408
2409 memcpy(pBusLogic->VBoxSCSI.pbBuf, &ScsiInquiryData, 5);
2410
2411 rc = vboxscsiRequestFinished(&pBusLogic->VBoxSCSI, &pTaskState->PDMScsiRequest, SCSI_STATUS_OK);
2412 AssertMsgRCReturn(rc, ("Finishing BIOS SCSI request failed rc=%Rrc\n", rc), rc);
2413
2414 RTMemCacheFree(pBusLogic->hTaskCache, pTaskState);
2415 }
2416 else
2417 {
2418 LogFlowFunc(("before increment %u\n", pTaskState->CTX_SUFF(pTargetDevice)->cOutstandingRequests));
2419 ASMAtomicIncU32(&pTaskState->CTX_SUFF(pTargetDevice)->cOutstandingRequests);
2420 LogFlowFunc(("after increment %u\n", pTaskState->CTX_SUFF(pTargetDevice)->cOutstandingRequests));
2421
2422 rc = pTaskState->CTX_SUFF(pTargetDevice)->pDrvSCSIConnector->pfnSCSIRequestSend(pTaskState->CTX_SUFF(pTargetDevice)->pDrvSCSIConnector,
2423 &pTaskState->PDMScsiRequest);
2424 AssertMsgRC(rc, ("Sending request to SCSI layer failed rc=%Rrc\n", rc));
2425 }
2426
2427 return rc;
2428}
2429
2430
2431/**
2432 * Port I/O Handler for IN operations - BIOS port.
2433 *
2434 * @returns VBox status code.
2435 *
2436 * @param pDevIns The device instance.
2437 * @param pvUser User argument.
2438 * @param uPort Port number used for the IN operation.
2439 * @param pu32 Where to store the result.
2440 * @param cb Number of bytes read.
2441 */
2442static DECLCALLBACK(int) buslogicR3BiosIoPortRead(PPDMDEVINS pDevIns, void *pvUser, RTIOPORT Port, uint32_t *pu32, unsigned cb)
2443{
2444 int rc;
2445 PBUSLOGIC pBusLogic = PDMINS_2_DATA(pDevIns, PBUSLOGIC);
2446
2447 Assert(cb == 1);
2448
2449 rc = vboxscsiReadRegister(&pBusLogic->VBoxSCSI, (Port - BUSLOGIC_BIOS_IO_PORT), pu32);
2450
2451 //Log2(("%s: pu32=%p:{%.*Rhxs} iRegister=%d rc=%Rrc\n",
2452 // __FUNCTION__, pu32, 1, pu32, (Port - BUSLOGIC_BIOS_IO_PORT), rc));
2453
2454 return rc;
2455}
2456
2457/**
2458 * Port I/O Handler for OUT operations - BIOS port.
2459 *
2460 * @returns VBox status code.
2461 *
2462 * @param pDevIns The device instance.
2463 * @param pvUser User argument.
2464 * @param uPort Port number used for the IN operation.
2465 * @param u32 The value to output.
2466 * @param cb The value size in bytes.
2467 */
2468static DECLCALLBACK(int) buslogicR3BiosIoPortWrite(PPDMDEVINS pDevIns, void *pvUser, RTIOPORT Port, uint32_t u32, unsigned cb)
2469{
2470 int rc;
2471 PBUSLOGIC pBusLogic = PDMINS_2_DATA(pDevIns, PBUSLOGIC);
2472
2473 Log2(("#%d %s: pvUser=%#p cb=%d u32=%#x Port=%#x\n",
2474 pDevIns->iInstance, __FUNCTION__, pvUser, cb, u32, Port));
2475
2476 Assert(cb == 1);
2477
2478 rc = vboxscsiWriteRegister(&pBusLogic->VBoxSCSI, (Port - BUSLOGIC_BIOS_IO_PORT), (uint8_t)u32);
2479 if (rc == VERR_MORE_DATA)
2480 {
2481 rc = buslogicR3PrepareBIOSSCSIRequest(pBusLogic);
2482 AssertRC(rc);
2483 }
2484 else if (RT_FAILURE(rc))
2485 AssertMsgFailed(("Writing BIOS register failed %Rrc\n", rc));
2486
2487 return VINF_SUCCESS;
2488}
2489
2490/**
2491 * Port I/O Handler for primary port range OUT string operations.
2492 * @see FNIOMIOPORTOUTSTRING for details.
2493 */
2494static DECLCALLBACK(int) buslogicR3BiosIoPortWriteStr(PPDMDEVINS pDevIns, void *pvUser, RTIOPORT Port, RTGCPTR *pGCPtrSrc,
2495 PRTGCUINTREG pcTransfer, unsigned cb)
2496{
2497 PBUSLOGIC pBusLogic = PDMINS_2_DATA(pDevIns, PBUSLOGIC);
2498 int rc;
2499
2500 Log2(("#%d %s: pvUser=%#p cb=%d Port=%#x\n",
2501 pDevIns->iInstance, __FUNCTION__, pvUser, cb, Port));
2502
2503 rc = vboxscsiWriteString(pDevIns, &pBusLogic->VBoxSCSI, (Port - BUSLOGIC_BIOS_IO_PORT),
2504 pGCPtrSrc, pcTransfer, cb);
2505 if (rc == VERR_MORE_DATA)
2506 {
2507 rc = buslogicR3PrepareBIOSSCSIRequest(pBusLogic);
2508 AssertRC(rc);
2509 }
2510 else if (RT_FAILURE(rc))
2511 AssertMsgFailed(("Writing BIOS register failed %Rrc\n", rc));
2512
2513 return rc;
2514}
2515
2516/**
2517 * Port I/O Handler for primary port range IN string operations.
2518 * @see FNIOMIOPORTINSTRING for details.
2519 */
2520static DECLCALLBACK(int) buslogicR3BiosIoPortReadStr(PPDMDEVINS pDevIns, void *pvUser, RTIOPORT Port, RTGCPTR *pGCPtrDst,
2521 PRTGCUINTREG pcTransfer, unsigned cb)
2522{
2523 PBUSLOGIC pBusLogic = PDMINS_2_DATA(pDevIns, PBUSLOGIC);
2524
2525 LogFlowFunc(("#%d %s: pvUser=%#p cb=%d Port=%#x\n",
2526 pDevIns->iInstance, __FUNCTION__, pvUser, cb, Port));
2527
2528 return vboxscsiReadString(pDevIns, &pBusLogic->VBoxSCSI, (Port - BUSLOGIC_BIOS_IO_PORT),
2529 pGCPtrDst, pcTransfer, cb);
2530}
2531
2532/**
2533 * Update the ISA I/O range.
2534 *
2535 * @returns nothing.
2536 * @param pBusLogic Pointer to the BusLogic device instance.
2537 * @param uBaseCode Encoded ISA I/O base; only low 3 bits are used.
2538 */
2539static int buslogicR3RegisterISARange(PBUSLOGIC pBusLogic, uint8_t uBaseCode)
2540{
2541 uint8_t uCode = uBaseCode & MAX_ISA_BASE;
2542 uint16_t uNewBase = g_aISABases[uCode];
2543 int rc = VINF_SUCCESS;
2544
2545 LogFlowFunc(("ISA I/O code %02X, new base %X\n", uBaseCode, uNewBase));
2546
2547 /* Check if the same port range is already registered. */
2548 if (uNewBase != pBusLogic->IOISABase)
2549 {
2550 /* Unregister the old range, if any. */
2551 if (pBusLogic->IOISABase)
2552 rc = PDMDevHlpIOPortDeregister(pBusLogic->CTX_SUFF(pDevIns), pBusLogic->IOISABase, 4);
2553
2554 if (RT_SUCCESS(rc))
2555 {
2556 pBusLogic->IOISABase = 0; /* First mark as unregistered. */
2557 pBusLogic->uISABaseCode = ISA_BASE_DISABLED;
2558
2559 if (uNewBase)
2560 {
2561 /* Register the new range if requested. */
2562 rc = PDMDevHlpIOPortRegister(pBusLogic->CTX_SUFF(pDevIns), uNewBase, 4, NULL,
2563 buslogicIOPortWrite, buslogicIOPortRead,
2564 NULL, NULL,
2565 "BusLogic ISA");
2566 if (RT_SUCCESS(rc))
2567 {
2568 pBusLogic->IOISABase = uNewBase;
2569 pBusLogic->uISABaseCode = uCode;
2570 }
2571 }
2572 }
2573 if (RT_SUCCESS(rc))
2574 {
2575 if (uNewBase)
2576 {
2577 Log(("ISA I/O base: %x\n", uNewBase));
2578 LogRel(("BusLogic: ISA I/O base: %x\n", uNewBase));
2579 }
2580 else
2581 {
2582 Log(("Disabling ISA I/O ports.\n"));
2583 LogRel(("BusLogic: ISA I/O disabled\n"));
2584 }
2585 }
2586
2587 }
2588 return rc;
2589}
2590
2591static void buslogicR3WarningDiskFull(PPDMDEVINS pDevIns)
2592{
2593 int rc;
2594 LogRel(("BusLogic#%d: Host disk full\n", pDevIns->iInstance));
2595 rc = PDMDevHlpVMSetRuntimeError(pDevIns, VMSETRTERR_FLAGS_SUSPEND | VMSETRTERR_FLAGS_NO_WAIT, "DevBusLogic_DISKFULL",
2596 N_("Host system reported disk full. VM execution is suspended. You can resume after freeing some space"));
2597 AssertRC(rc);
2598}
2599
2600static void buslogicR3WarningFileTooBig(PPDMDEVINS pDevIns)
2601{
2602 int rc;
2603 LogRel(("BusLogic#%d: File too big\n", pDevIns->iInstance));
2604 rc = PDMDevHlpVMSetRuntimeError(pDevIns, VMSETRTERR_FLAGS_SUSPEND | VMSETRTERR_FLAGS_NO_WAIT, "DevBusLogic_FILETOOBIG",
2605 N_("Host system reported that the file size limit of the host file system has been exceeded. VM execution is suspended. You need to move your virtual hard disk to a filesystem which allows bigger files"));
2606 AssertRC(rc);
2607}
2608
2609static void buslogicR3WarningISCSI(PPDMDEVINS pDevIns)
2610{
2611 int rc;
2612 LogRel(("BusLogic#%d: iSCSI target unavailable\n", pDevIns->iInstance));
2613 rc = PDMDevHlpVMSetRuntimeError(pDevIns, VMSETRTERR_FLAGS_SUSPEND | VMSETRTERR_FLAGS_NO_WAIT, "DevBusLogic_ISCSIDOWN",
2614 N_("The iSCSI target has stopped responding. VM execution is suspended. You can resume when it is available again"));
2615 AssertRC(rc);
2616}
2617
2618static void buslogicR3WarningUnknown(PPDMDEVINS pDevIns, int rc)
2619{
2620 int rc2;
2621 LogRel(("BusLogic#%d: Unknown but recoverable error has occurred (rc=%Rrc)\n", pDevIns->iInstance, rc));
2622 rc2 = PDMDevHlpVMSetRuntimeError(pDevIns, VMSETRTERR_FLAGS_SUSPEND | VMSETRTERR_FLAGS_NO_WAIT, "DevBusLogic_UNKNOWN",
2623 N_("An unknown but recoverable I/O error has occurred (rc=%Rrc). VM execution is suspended. You can resume when the error is fixed"), rc);
2624 AssertRC(rc2);
2625}
2626
2627static void buslogicR3RedoSetWarning(PBUSLOGIC pThis, int rc)
2628{
2629 if (rc == VERR_DISK_FULL)
2630 buslogicR3WarningDiskFull(pThis->CTX_SUFF(pDevIns));
2631 else if (rc == VERR_FILE_TOO_BIG)
2632 buslogicR3WarningFileTooBig(pThis->CTX_SUFF(pDevIns));
2633 else if (rc == VERR_BROKEN_PIPE || rc == VERR_NET_CONNECTION_REFUSED)
2634 {
2635 /* iSCSI connection abort (first error) or failure to reestablish
2636 * connection (second error). Pause VM. On resume we'll retry. */
2637 buslogicR3WarningISCSI(pThis->CTX_SUFF(pDevIns));
2638 }
2639 else
2640 buslogicR3WarningUnknown(pThis->CTX_SUFF(pDevIns), rc);
2641}
2642
2643
2644static DECLCALLBACK(int) buslogicR3MmioMap(PPCIDEVICE pPciDev, /*unsigned*/ int iRegion,
2645 RTGCPHYS GCPhysAddress, uint32_t cb, PCIADDRESSSPACE enmType)
2646{
2647 PPDMDEVINS pDevIns = pPciDev->pDevIns;
2648 PBUSLOGIC pThis = PDMINS_2_DATA(pDevIns, PBUSLOGIC);
2649 int rc = VINF_SUCCESS;
2650
2651 Log2(("%s: registering MMIO area at GCPhysAddr=%RGp cb=%u\n", __FUNCTION__, GCPhysAddress, cb));
2652
2653 Assert(cb >= 32);
2654
2655 if (enmType == PCI_ADDRESS_SPACE_MEM)
2656 {
2657 /* We use the assigned size here, because we currently only support page aligned MMIO ranges. */
2658 rc = PDMDevHlpMMIORegister(pDevIns, GCPhysAddress, cb, NULL /*pvUser*/,
2659 IOMMMIO_FLAGS_READ_PASSTHRU | IOMMMIO_FLAGS_WRITE_PASSTHRU,
2660 buslogicMMIOWrite, buslogicMMIORead, "BusLogic MMIO");
2661 if (RT_FAILURE(rc))
2662 return rc;
2663
2664 if (pThis->fR0Enabled)
2665 {
2666 rc = PDMDevHlpMMIORegisterR0(pDevIns, GCPhysAddress, cb, NIL_RTR0PTR /*pvUser*/,
2667 "buslogicMMIOWrite", "buslogicMMIORead");
2668 if (RT_FAILURE(rc))
2669 return rc;
2670 }
2671
2672 if (pThis->fGCEnabled)
2673 {
2674 rc = PDMDevHlpMMIORegisterRC(pDevIns, GCPhysAddress, cb, NIL_RTRCPTR /*pvUser*/,
2675 "buslogicMMIOWrite", "buslogicMMIORead");
2676 if (RT_FAILURE(rc))
2677 return rc;
2678 }
2679
2680 pThis->MMIOBase = GCPhysAddress;
2681 }
2682 else if (enmType == PCI_ADDRESS_SPACE_IO)
2683 {
2684 rc = PDMDevHlpIOPortRegister(pDevIns, (RTIOPORT)GCPhysAddress, 32,
2685 NULL, buslogicIOPortWrite, buslogicIOPortRead, NULL, NULL, "BusLogic PCI");
2686 if (RT_FAILURE(rc))
2687 return rc;
2688
2689 if (pThis->fR0Enabled)
2690 {
2691 rc = PDMDevHlpIOPortRegisterR0(pDevIns, (RTIOPORT)GCPhysAddress, 32,
2692 0, "buslogicIOPortWrite", "buslogicIOPortRead", NULL, NULL, "BusLogic PCI");
2693 if (RT_FAILURE(rc))
2694 return rc;
2695 }
2696
2697 if (pThis->fGCEnabled)
2698 {
2699 rc = PDMDevHlpIOPortRegisterRC(pDevIns, (RTIOPORT)GCPhysAddress, 32,
2700 0, "buslogicIOPortWrite", "buslogicIOPortRead", NULL, NULL, "BusLogic PCI");
2701 if (RT_FAILURE(rc))
2702 return rc;
2703 }
2704
2705 pThis->IOPortBase = (RTIOPORT)GCPhysAddress;
2706 }
2707 else
2708 AssertMsgFailed(("Invalid enmType=%d\n", enmType));
2709
2710 return rc;
2711}
2712
2713static DECLCALLBACK(int) buslogicR3DeviceSCSIRequestCompleted(PPDMISCSIPORT pInterface, PPDMSCSIREQUEST pSCSIRequest,
2714 int rcCompletion, bool fRedo, int rcReq)
2715{
2716 int rc;
2717 PBUSLOGICTASKSTATE pTaskState = (PBUSLOGICTASKSTATE)pSCSIRequest->pvUser;
2718 PBUSLOGICDEVICE pBusLogicDevice = pTaskState->CTX_SUFF(pTargetDevice);
2719 PBUSLOGIC pBusLogic = pBusLogicDevice->CTX_SUFF(pBusLogic);
2720
2721 LogFlowFunc(("before decrement %u\n", pBusLogicDevice->cOutstandingRequests));
2722 ASMAtomicDecU32(&pBusLogicDevice->cOutstandingRequests);
2723 LogFlowFunc(("after decrement %u\n", pBusLogicDevice->cOutstandingRequests));
2724
2725 if (fRedo)
2726 {
2727 if (!pTaskState->fBIOS)
2728 {
2729 buslogicR3DataBufferFree(pTaskState);
2730
2731 if (pTaskState->pbSenseBuffer)
2732 buslogicR3SenseBufferFree(pTaskState, false /* fCopy */);
2733 }
2734
2735 /* Add to the list. */
2736 do
2737 {
2738 pTaskState->pRedoNext = ASMAtomicReadPtrT(&pBusLogic->pTasksRedoHead, PBUSLOGICTASKSTATE);
2739 } while (!ASMAtomicCmpXchgPtr(&pBusLogic->pTasksRedoHead, pTaskState, pTaskState->pRedoNext));
2740
2741 /* Suspend the VM if not done already. */
2742 if (!ASMAtomicXchgBool(&pBusLogic->fRedo, true))
2743 buslogicR3RedoSetWarning(pBusLogic, rcReq);
2744 }
2745 else
2746 {
2747 if (pTaskState->fBIOS)
2748 {
2749 rc = vboxscsiRequestFinished(&pBusLogic->VBoxSCSI, pSCSIRequest, rcCompletion);
2750 AssertMsgRC(rc, ("Finishing BIOS SCSI request failed rc=%Rrc\n", rc));
2751 }
2752 else
2753 {
2754 buslogicR3DataBufferFree(pTaskState);
2755
2756 if (pTaskState->pbSenseBuffer)
2757 buslogicR3SenseBufferFree(pTaskState, (rcCompletion != SCSI_STATUS_OK));
2758
2759 if (rcCompletion == SCSI_STATUS_OK)
2760 buslogicR3SendIncomingMailbox(pBusLogic, pTaskState,
2761 BUSLOGIC_MAILBOX_INCOMING_ADAPTER_STATUS_CMD_COMPLETED,
2762 BUSLOGIC_MAILBOX_INCOMING_DEVICE_STATUS_OPERATION_GOOD,
2763 BUSLOGIC_MAILBOX_INCOMING_COMPLETION_WITHOUT_ERROR);
2764 else if (rcCompletion == SCSI_STATUS_CHECK_CONDITION)
2765 buslogicR3SendIncomingMailbox(pBusLogic, pTaskState,
2766 BUSLOGIC_MAILBOX_INCOMING_ADAPTER_STATUS_CMD_COMPLETED,
2767 BUSLOGIC_MAILBOX_INCOMING_DEVICE_STATUS_CHECK_CONDITION,
2768 BUSLOGIC_MAILBOX_INCOMING_COMPLETION_WITH_ERROR);
2769 else
2770 AssertMsgFailed(("invalid completion status %d\n", rcCompletion));
2771 }
2772#ifdef LOG_ENABLED
2773 buslogicR3DumpCCBInfo(&pTaskState->CommandControlBlockGuest, pTaskState->fIs24Bit);
2774#endif
2775
2776 /* Remove task from the cache. */
2777 RTMemCacheFree(pBusLogic->hTaskCache, pTaskState);
2778 }
2779
2780 if (pBusLogicDevice->cOutstandingRequests == 0 && pBusLogic->fSignalIdle)
2781 PDMDevHlpAsyncNotificationCompleted(pBusLogic->pDevInsR3);
2782
2783 return VINF_SUCCESS;
2784}
2785
2786static DECLCALLBACK(int) buslogicR3QueryDeviceLocation(PPDMISCSIPORT pInterface, const char **ppcszController,
2787 uint32_t *piInstance, uint32_t *piLUN)
2788{
2789 PBUSLOGICDEVICE pBusLogicDevice = PDMISCSIPORT_2_PBUSLOGICDEVICE(pInterface);
2790 PPDMDEVINS pDevIns = pBusLogicDevice->CTX_SUFF(pBusLogic)->CTX_SUFF(pDevIns);
2791
2792 AssertPtrReturn(ppcszController, VERR_INVALID_POINTER);
2793 AssertPtrReturn(piInstance, VERR_INVALID_POINTER);
2794 AssertPtrReturn(piLUN, VERR_INVALID_POINTER);
2795
2796 *ppcszController = pDevIns->pReg->szName;
2797 *piInstance = pDevIns->iInstance;
2798 *piLUN = pBusLogicDevice->iLUN;
2799
2800 return VINF_SUCCESS;
2801}
2802
2803static int buslogicR3DeviceSCSIRequestSetup(PBUSLOGIC pBusLogic, PBUSLOGICTASKSTATE pTaskState)
2804{
2805 int rc = VINF_SUCCESS;
2806 uint8_t uTargetIdCCB;
2807 PBUSLOGICDEVICE pTargetDevice;
2808
2809 /* Fetch the CCB from guest memory. */
2810 /** @todo How much do we really have to read? */
2811 RTGCPHYS GCPhysAddrCCB = (RTGCPHYS)pTaskState->MailboxGuest.u32PhysAddrCCB;
2812 PDMDevHlpPhysRead(pBusLogic->CTX_SUFF(pDevIns), GCPhysAddrCCB,
2813 &pTaskState->CommandControlBlockGuest, sizeof(CCB32));
2814
2815 uTargetIdCCB = pTaskState->fIs24Bit ? pTaskState->CommandControlBlockGuest.o.uTargetId : pTaskState->CommandControlBlockGuest.n.uTargetId;
2816 pTargetDevice = &pBusLogic->aDeviceStates[uTargetIdCCB];
2817 pTaskState->CTX_SUFF(pTargetDevice) = pTargetDevice;
2818
2819#ifdef LOG_ENABLED
2820 buslogicR3DumpCCBInfo(&pTaskState->CommandControlBlockGuest, pTaskState->fIs24Bit);
2821#endif
2822
2823 /* Alloc required buffers. */
2824 rc = buslogicR3DataBufferAlloc(pTaskState);
2825 AssertMsgRC(rc, ("Alloc failed rc=%Rrc\n", rc));
2826
2827 rc = buslogicR3SenseBufferAlloc(pTaskState);
2828 AssertMsgRC(rc, ("Mapping sense buffer failed rc=%Rrc\n", rc));
2829
2830 /* Check if device is present on bus. If not return error immediately and don't process this further. */
2831 if (!pBusLogic->aDeviceStates[uTargetIdCCB].fPresent)
2832 {
2833 buslogicR3DataBufferFree(pTaskState);
2834
2835 if (pTaskState->pbSenseBuffer)
2836 buslogicR3SenseBufferFree(pTaskState, true);
2837
2838 buslogicR3SendIncomingMailbox(pBusLogic, pTaskState,
2839 BUSLOGIC_MAILBOX_INCOMING_ADAPTER_STATUS_SCSI_SELECTION_TIMEOUT,
2840 BUSLOGIC_MAILBOX_INCOMING_DEVICE_STATUS_OPERATION_GOOD,
2841 BUSLOGIC_MAILBOX_INCOMING_COMPLETION_WITH_ERROR);
2842
2843 RTMemCacheFree(pBusLogic->hTaskCache, pTaskState);
2844 }
2845 else
2846 {
2847 /* Setup SCSI request. */
2848 pTaskState->PDMScsiRequest.uLogicalUnit = pTaskState->fIs24Bit ? pTaskState->CommandControlBlockGuest.o.uLogicalUnit
2849 : pTaskState->CommandControlBlockGuest.n.uLogicalUnit;
2850
2851 if (pTaskState->CommandControlBlockGuest.c.uDataDirection == BUSLOGIC_CCB_DIRECTION_UNKNOWN)
2852 pTaskState->PDMScsiRequest.uDataDirection = PDMSCSIREQUESTTXDIR_UNKNOWN;
2853 else if (pTaskState->CommandControlBlockGuest.c.uDataDirection == BUSLOGIC_CCB_DIRECTION_IN)
2854 pTaskState->PDMScsiRequest.uDataDirection = PDMSCSIREQUESTTXDIR_FROM_DEVICE;
2855 else if (pTaskState->CommandControlBlockGuest.c.uDataDirection == BUSLOGIC_CCB_DIRECTION_OUT)
2856 pTaskState->PDMScsiRequest.uDataDirection = PDMSCSIREQUESTTXDIR_TO_DEVICE;
2857 else if (pTaskState->CommandControlBlockGuest.c.uDataDirection == BUSLOGIC_CCB_DIRECTION_NO_DATA)
2858 pTaskState->PDMScsiRequest.uDataDirection = PDMSCSIREQUESTTXDIR_NONE;
2859 else
2860 AssertMsgFailed(("Invalid data direction type %d\n", pTaskState->CommandControlBlockGuest.c.uDataDirection));
2861
2862 pTaskState->PDMScsiRequest.cbCDB = pTaskState->CommandControlBlockGuest.c.cbCDB;
2863 pTaskState->PDMScsiRequest.pbCDB = pTaskState->CommandControlBlockGuest.c.abCDB;
2864 if (pTaskState->DataSeg.cbSeg)
2865 {
2866 pTaskState->PDMScsiRequest.cbScatterGather = pTaskState->DataSeg.cbSeg;
2867 pTaskState->PDMScsiRequest.cScatterGatherEntries = 1;
2868 pTaskState->PDMScsiRequest.paScatterGatherHead = &pTaskState->DataSeg;
2869 }
2870 else
2871 {
2872 pTaskState->PDMScsiRequest.cbScatterGather = 0;
2873 pTaskState->PDMScsiRequest.cScatterGatherEntries = 0;
2874 pTaskState->PDMScsiRequest.paScatterGatherHead = NULL;
2875 }
2876 pTaskState->PDMScsiRequest.cbSenseBuffer = buslogicR3ConvertSenseBufferLength(pTaskState->CommandControlBlockGuest.c.cbSenseData);
2877 pTaskState->PDMScsiRequest.pbSenseBuffer = pTaskState->pbSenseBuffer;
2878 pTaskState->PDMScsiRequest.pvUser = pTaskState;
2879
2880 ASMAtomicIncU32(&pTargetDevice->cOutstandingRequests);
2881 rc = pTargetDevice->pDrvSCSIConnector->pfnSCSIRequestSend(pTargetDevice->pDrvSCSIConnector, &pTaskState->PDMScsiRequest);
2882 AssertMsgRC(rc, ("Sending request to SCSI layer failed rc=%Rrc\n", rc));
2883 }
2884
2885 return rc;
2886}
2887
2888/**
2889 * Read a mailbox from guest memory. Convert 24-bit mailboxes to
2890 * 32-bit format.
2891 *
2892 * @returns Mailbox guest physical address.
2893 * @param pBusLogic Pointer to the BusLogic instance data.
2894 * @param pTaskStat Pointer to the task state being set up.
2895 */
2896static RTGCPHYS buslogicR3ReadOutgoingMailbox(PBUSLOGIC pBusLogic, PBUSLOGICTASKSTATE pTaskState)
2897{
2898 RTGCPHYS GCMailbox;
2899
2900 if (pBusLogic->fMbxIs24Bit)
2901 {
2902 Mailbox24 Mbx24;
2903
2904 GCMailbox = pBusLogic->GCPhysAddrMailboxOutgoingBase + (pBusLogic->uMailboxOutgoingPositionCurrent * sizeof(Mailbox24));
2905 PDMDevHlpPhysRead(pBusLogic->CTX_SUFF(pDevIns), GCMailbox, &Mbx24, sizeof(Mailbox24));
2906 pTaskState->MailboxGuest.u32PhysAddrCCB = ADDR_TO_U32(Mbx24.aPhysAddrCCB);
2907 pTaskState->MailboxGuest.u.out.uActionCode = Mbx24.uCmdState;
2908 }
2909 else
2910 {
2911 GCMailbox = pBusLogic->GCPhysAddrMailboxOutgoingBase + (pBusLogic->uMailboxOutgoingPositionCurrent * sizeof(Mailbox32));
2912 PDMDevHlpPhysRead(pBusLogic->CTX_SUFF(pDevIns), GCMailbox, &pTaskState->MailboxGuest, sizeof(Mailbox32));
2913 }
2914
2915 return GCMailbox;
2916}
2917
2918/**
2919 * Read mailbox from the guest and execute command.
2920 *
2921 * @returns VBox status code.
2922 * @param pBusLogic Pointer to the BusLogic instance data.
2923 */
2924static int buslogicR3ProcessMailboxNext(PBUSLOGIC pBusLogic)
2925{
2926 PBUSLOGICTASKSTATE pTaskState = NULL;
2927 RTGCPHYS GCPhysAddrMailboxCurrent;
2928 int rc;
2929
2930 rc = RTMemCacheAllocEx(pBusLogic->hTaskCache, (void **)&pTaskState);
2931 AssertMsgReturn(RT_SUCCESS(rc) && (pTaskState != NULL), ("Failed to get task state from cache\n"), rc);
2932
2933 pTaskState->fBIOS = false;
2934 pTaskState->fIs24Bit = pBusLogic->fMbxIs24Bit;
2935 pTaskState->cbSGEntry = pBusLogic->fMbxIs24Bit ? sizeof(SGE24) : sizeof(SGE32);
2936
2937 if (!pBusLogic->fStrictRoundRobinMode)
2938 {
2939 /* Search for a filled mailbox - stop if we have scanned all mailboxes. */
2940 uint8_t uMailboxPosCur = pBusLogic->uMailboxOutgoingPositionCurrent;
2941
2942 do
2943 {
2944 /* Fetch mailbox from guest memory. */
2945 GCPhysAddrMailboxCurrent = buslogicR3ReadOutgoingMailbox(pBusLogic,pTaskState);
2946
2947 /* Check the next mailbox. */
2948 buslogicR3OutgoingMailboxAdvance(pBusLogic);
2949 } while ( pTaskState->MailboxGuest.u.out.uActionCode == BUSLOGIC_MAILBOX_OUTGOING_ACTION_FREE
2950 && uMailboxPosCur != pBusLogic->uMailboxOutgoingPositionCurrent);
2951 }
2952 else
2953 {
2954 /* Fetch mailbox from guest memory. */
2955 GCPhysAddrMailboxCurrent = buslogicR3ReadOutgoingMailbox(pBusLogic,pTaskState);
2956 }
2957
2958 /*
2959 * Check if the mailbox is actually loaded.
2960 * It might be possible that the guest notified us without
2961 * a loaded mailbox. Do nothing in that case but leave a
2962 * log entry.
2963 */
2964 if (pTaskState->MailboxGuest.u.out.uActionCode == BUSLOGIC_MAILBOX_OUTGOING_ACTION_FREE)
2965 {
2966 Log(("No loaded mailbox left\n"));
2967 RTMemCacheFree(pBusLogic->hTaskCache, pTaskState);
2968 return VERR_NO_DATA;
2969 }
2970
2971 LogFlow(("Got loaded mailbox at slot %u, CCB phys %RGp\n", pBusLogic->uMailboxOutgoingPositionCurrent, (RTGCPHYS)pTaskState->MailboxGuest.u32PhysAddrCCB));
2972#ifdef LOG_ENABLED
2973 buslogicR3DumpMailboxInfo(&pTaskState->MailboxGuest, true);
2974#endif
2975
2976 /* We got the mailbox, mark it as free in the guest. */
2977 uint8_t uActionCode = BUSLOGIC_MAILBOX_OUTGOING_ACTION_FREE;
2978 unsigned uCodeOffs = pTaskState->fIs24Bit ? RT_OFFSETOF(Mailbox24, uCmdState) : RT_OFFSETOF(Mailbox32, u.out.uActionCode);
2979 PDMDevHlpPCIPhysWrite(pBusLogic->CTX_SUFF(pDevIns), GCPhysAddrMailboxCurrent + uCodeOffs, &uActionCode, sizeof(uActionCode));
2980
2981 if (pTaskState->MailboxGuest.u.out.uActionCode == BUSLOGIC_MAILBOX_OUTGOING_ACTION_START_COMMAND)
2982 rc = buslogicR3DeviceSCSIRequestSetup(pBusLogic, pTaskState);
2983 else if (pTaskState->MailboxGuest.u.out.uActionCode == BUSLOGIC_MAILBOX_OUTGOING_ACTION_ABORT_COMMAND)
2984 {
2985 AssertMsgFailed(("Not implemented yet\n"));
2986 }
2987 else
2988 AssertMsgFailed(("Invalid outgoing mailbox action code %u\n", pTaskState->MailboxGuest.u.out.uActionCode));
2989
2990 AssertRC(rc);
2991
2992 /* Advance to the next mailbox. */
2993 if (pBusLogic->fStrictRoundRobinMode)
2994 buslogicR3OutgoingMailboxAdvance(pBusLogic);
2995
2996 return rc;
2997}
2998
2999/**
3000 * Transmit queue consumer
3001 * Queue a new async task.
3002 *
3003 * @returns Success indicator.
3004 * If false the item will not be removed and the flushing will stop.
3005 * @param pDevIns The device instance.
3006 * @param pItem The item to consume. Upon return this item will be freed.
3007 */
3008static DECLCALLBACK(bool) buslogicR3NotifyQueueConsumer(PPDMDEVINS pDevIns, PPDMQUEUEITEMCORE pItem)
3009{
3010 PBUSLOGIC pBusLogic = PDMINS_2_DATA(pDevIns, PBUSLOGIC);
3011
3012 /* Reset notification send flag now. */
3013 Assert(pBusLogic->fNotificationSend);
3014 ASMAtomicXchgBool(&pBusLogic->fNotificationSend, false);
3015 ASMAtomicXchgU32(&pBusLogic->cMailboxesReady, 0); /** @todo Actually not required anymore but to stay compatible with older saved states. */
3016
3017 /* Process mailboxes. */
3018 int rc;
3019 do
3020 {
3021 rc = buslogicR3ProcessMailboxNext(pBusLogic);
3022 AssertMsg(RT_SUCCESS(rc) || rc == VERR_NO_DATA, ("Processing mailbox failed rc=%Rrc\n", rc));
3023 } while (RT_SUCCESS(rc));
3024
3025 return true;
3026}
3027
3028/**
3029 * Kicks the controller to process pending tasks after the VM was resumed
3030 * or loaded from a saved state.
3031 *
3032 * @returns nothing.
3033 * @param pThis The BusLogic device instance.
3034 */
3035static void buslogicR3Kick(PBUSLOGIC pThis)
3036{
3037 if (pThis->fRedo)
3038 {
3039 pThis->fRedo = false;
3040 if (pThis->VBoxSCSI.fBusy)
3041 {
3042
3043 /* The BIOS had a request active when we got suspended. Resume it. */
3044 int rc = buslogicR3PrepareBIOSSCSIRequest(pThis);
3045 AssertRC(rc);
3046 }
3047 else
3048 {
3049 /* Queue all pending tasks again. */
3050 PBUSLOGICTASKSTATE pTaskState = pThis->pTasksRedoHead;
3051
3052 pThis->pTasksRedoHead = NULL;
3053
3054 while (pTaskState)
3055 {
3056 PBUSLOGICTASKSTATE pCur = pTaskState;
3057
3058 int rc = buslogicR3DeviceSCSIRequestSetup(pThis, pCur);
3059 AssertRC(rc);
3060
3061 pTaskState = pTaskState->pRedoNext;
3062 }
3063 }
3064 }
3065}
3066
3067/** @callback_method_impl{FNSSMDEVLIVEEXEC} */
3068static DECLCALLBACK(int) buslogicR3LiveExec(PPDMDEVINS pDevIns, PSSMHANDLE pSSM, uint32_t uPass)
3069{
3070 PBUSLOGIC pThis = PDMINS_2_DATA(pDevIns, PBUSLOGIC);
3071
3072 /* Save the device config. */
3073 for (unsigned i = 0; i < RT_ELEMENTS(pThis->aDeviceStates); i++)
3074 SSMR3PutBool(pSSM, pThis->aDeviceStates[i].fPresent);
3075
3076 return VINF_SSM_DONT_CALL_AGAIN;
3077}
3078
3079/** @callback_method_impl{FNSSMDEVSAVEEXEC} */
3080static DECLCALLBACK(int) buslogicR3SaveExec(PPDMDEVINS pDevIns, PSSMHANDLE pSSM)
3081{
3082 PBUSLOGIC pBusLogic = PDMINS_2_DATA(pDevIns, PBUSLOGIC);
3083
3084 /* Every device first. */
3085 for (unsigned i = 0; i < RT_ELEMENTS(pBusLogic->aDeviceStates); i++)
3086 {
3087 PBUSLOGICDEVICE pDevice = &pBusLogic->aDeviceStates[i];
3088
3089 AssertMsg(!pDevice->cOutstandingRequests,
3090 ("There are still outstanding requests on this device\n"));
3091 SSMR3PutBool(pSSM, pDevice->fPresent);
3092 SSMR3PutU32(pSSM, pDevice->cOutstandingRequests);
3093 }
3094 /* Now the main device state. */
3095 SSMR3PutU8 (pSSM, pBusLogic->regStatus);
3096 SSMR3PutU8 (pSSM, pBusLogic->regInterrupt);
3097 SSMR3PutU8 (pSSM, pBusLogic->regGeometry);
3098 SSMR3PutMem (pSSM, &pBusLogic->LocalRam, sizeof(pBusLogic->LocalRam));
3099 SSMR3PutU8 (pSSM, pBusLogic->uOperationCode);
3100 SSMR3PutMem (pSSM, &pBusLogic->aCommandBuffer, sizeof(pBusLogic->aCommandBuffer));
3101 SSMR3PutU8 (pSSM, pBusLogic->iParameter);
3102 SSMR3PutU8 (pSSM, pBusLogic->cbCommandParametersLeft);
3103 SSMR3PutBool (pSSM, pBusLogic->fUseLocalRam);
3104 SSMR3PutMem (pSSM, pBusLogic->aReplyBuffer, sizeof(pBusLogic->aReplyBuffer));
3105 SSMR3PutU8 (pSSM, pBusLogic->iReply);
3106 SSMR3PutU8 (pSSM, pBusLogic->cbReplyParametersLeft);
3107 SSMR3PutBool (pSSM, pBusLogic->fIRQEnabled);
3108 SSMR3PutU8 (pSSM, pBusLogic->uISABaseCode);
3109 SSMR3PutU32 (pSSM, pBusLogic->cMailbox);
3110 SSMR3PutBool (pSSM, pBusLogic->fMbxIs24Bit);
3111 SSMR3PutGCPhys(pSSM, pBusLogic->GCPhysAddrMailboxOutgoingBase);
3112 SSMR3PutU32 (pSSM, pBusLogic->uMailboxOutgoingPositionCurrent);
3113 SSMR3PutU32 (pSSM, pBusLogic->cMailboxesReady);
3114 SSMR3PutBool (pSSM, pBusLogic->fNotificationSend);
3115 SSMR3PutGCPhys(pSSM, pBusLogic->GCPhysAddrMailboxIncomingBase);
3116 SSMR3PutU32 (pSSM, pBusLogic->uMailboxIncomingPositionCurrent);
3117 SSMR3PutBool (pSSM, pBusLogic->fStrictRoundRobinMode);
3118 SSMR3PutBool (pSSM, pBusLogic->fExtendedLunCCBFormat);
3119 /* Now the data for the BIOS interface. */
3120 SSMR3PutU8 (pSSM, pBusLogic->VBoxSCSI.regIdentify);
3121 SSMR3PutU8 (pSSM, pBusLogic->VBoxSCSI.uTargetDevice);
3122 SSMR3PutU8 (pSSM, pBusLogic->VBoxSCSI.uTxDir);
3123 SSMR3PutU8 (pSSM, pBusLogic->VBoxSCSI.cbCDB);
3124 SSMR3PutMem (pSSM, pBusLogic->VBoxSCSI.abCDB, sizeof(pBusLogic->VBoxSCSI.abCDB));
3125 SSMR3PutU8 (pSSM, pBusLogic->VBoxSCSI.iCDB);
3126 SSMR3PutU32 (pSSM, pBusLogic->VBoxSCSI.cbBuf);
3127 SSMR3PutU32 (pSSM, pBusLogic->VBoxSCSI.iBuf);
3128 SSMR3PutBool (pSSM, pBusLogic->VBoxSCSI.fBusy);
3129 SSMR3PutU8 (pSSM, pBusLogic->VBoxSCSI.enmState);
3130 if (pBusLogic->VBoxSCSI.cbBuf)
3131 SSMR3PutMem(pSSM, pBusLogic->VBoxSCSI.pbBuf, pBusLogic->VBoxSCSI.cbBuf);
3132
3133 /*
3134 * Save the physical addresses of the command control blocks of still pending tasks.
3135 * They are processed again on resume.
3136 *
3137 * The number of pending tasks needs to be determined first.
3138 */
3139 uint32_t cTasks = 0;
3140
3141 PBUSLOGICTASKSTATE pTaskState = pBusLogic->pTasksRedoHead;
3142 if (pBusLogic->fRedo)
3143 {
3144 while (pTaskState)
3145 {
3146 cTasks++;
3147 pTaskState = pTaskState->pRedoNext;
3148 }
3149 }
3150 SSMR3PutU32(pSSM, cTasks);
3151
3152 /* Write the address of every task now. */
3153 pTaskState = pBusLogic->pTasksRedoHead;
3154 while (pTaskState)
3155 {
3156 SSMR3PutU32(pSSM, pTaskState->MailboxGuest.u32PhysAddrCCB);
3157 pTaskState = pTaskState->pRedoNext;
3158 }
3159
3160 return SSMR3PutU32(pSSM, ~0);
3161}
3162
3163/** @callback_method_impl{FNSSMDEVLOADDONE} */
3164static DECLCALLBACK(int) buslogicR3LoadDone(PPDMDEVINS pDevIns, PSSMHANDLE pSSM)
3165{
3166 PBUSLOGIC pThis = PDMINS_2_DATA(pDevIns, PBUSLOGIC);
3167
3168 buslogicR3RegisterISARange(pThis, pThis->uISABaseCode);
3169 buslogicR3Kick(pThis);
3170 return VINF_SUCCESS;
3171}
3172
3173/** @callback_method_impl{FNSSMDEVLOADEXEC} */
3174static DECLCALLBACK(int) buslogicR3LoadExec(PPDMDEVINS pDevIns, PSSMHANDLE pSSM, uint32_t uVersion, uint32_t uPass)
3175{
3176 PBUSLOGIC pBusLogic = PDMINS_2_DATA(pDevIns, PBUSLOGIC);
3177 int rc = VINF_SUCCESS;
3178
3179 /* We support saved states only from this and older versions. */
3180 if (uVersion > BUSLOGIC_SAVED_STATE_MINOR_VERSION)
3181 return VERR_SSM_UNSUPPORTED_DATA_UNIT_VERSION;
3182
3183 /* Every device first. */
3184 for (unsigned i = 0; i < RT_ELEMENTS(pBusLogic->aDeviceStates); i++)
3185 {
3186 PBUSLOGICDEVICE pDevice = &pBusLogic->aDeviceStates[i];
3187
3188 AssertMsg(!pDevice->cOutstandingRequests,
3189 ("There are still outstanding requests on this device\n"));
3190 bool fPresent;
3191 rc = SSMR3GetBool(pSSM, &fPresent);
3192 AssertRCReturn(rc, rc);
3193 if (pDevice->fPresent != fPresent)
3194 return SSMR3SetCfgError(pSSM, RT_SRC_POS, N_("Target %u config mismatch: config=%RTbool state=%RTbool"), i, pDevice->fPresent, fPresent);
3195
3196 if (uPass == SSM_PASS_FINAL)
3197 SSMR3GetU32(pSSM, (uint32_t *)&pDevice->cOutstandingRequests);
3198 }
3199
3200 if (uPass != SSM_PASS_FINAL)
3201 return VINF_SUCCESS;
3202
3203 /* Now the main device state. */
3204 SSMR3GetU8 (pSSM, (uint8_t *)&pBusLogic->regStatus);
3205 SSMR3GetU8 (pSSM, (uint8_t *)&pBusLogic->regInterrupt);
3206 SSMR3GetU8 (pSSM, (uint8_t *)&pBusLogic->regGeometry);
3207 SSMR3GetMem (pSSM, &pBusLogic->LocalRam, sizeof(pBusLogic->LocalRam));
3208 SSMR3GetU8 (pSSM, &pBusLogic->uOperationCode);
3209 SSMR3GetMem (pSSM, &pBusLogic->aCommandBuffer, sizeof(pBusLogic->aCommandBuffer));
3210 SSMR3GetU8 (pSSM, &pBusLogic->iParameter);
3211 SSMR3GetU8 (pSSM, &pBusLogic->cbCommandParametersLeft);
3212 SSMR3GetBool (pSSM, &pBusLogic->fUseLocalRam);
3213 SSMR3GetMem (pSSM, pBusLogic->aReplyBuffer, sizeof(pBusLogic->aReplyBuffer));
3214 SSMR3GetU8 (pSSM, &pBusLogic->iReply);
3215 SSMR3GetU8 (pSSM, &pBusLogic->cbReplyParametersLeft);
3216 SSMR3GetBool (pSSM, &pBusLogic->fIRQEnabled);
3217 SSMR3GetU8 (pSSM, &pBusLogic->uISABaseCode);
3218 SSMR3GetU32 (pSSM, &pBusLogic->cMailbox);
3219 if (uVersion > BUSLOGIC_SAVED_STATE_MINOR_PRE_24BIT_MBOX)
3220 SSMR3GetBool (pSSM, &pBusLogic->fMbxIs24Bit);
3221 SSMR3GetGCPhys(pSSM, &pBusLogic->GCPhysAddrMailboxOutgoingBase);
3222 SSMR3GetU32 (pSSM, &pBusLogic->uMailboxOutgoingPositionCurrent);
3223 SSMR3GetU32 (pSSM, (uint32_t *)&pBusLogic->cMailboxesReady);
3224 SSMR3GetBool (pSSM, (bool *)&pBusLogic->fNotificationSend);
3225 SSMR3GetGCPhys(pSSM, &pBusLogic->GCPhysAddrMailboxIncomingBase);
3226 SSMR3GetU32 (pSSM, &pBusLogic->uMailboxIncomingPositionCurrent);
3227 SSMR3GetBool (pSSM, &pBusLogic->fStrictRoundRobinMode);
3228 SSMR3GetBool (pSSM, &pBusLogic->fExtendedLunCCBFormat);
3229 /* Now the data for the BIOS interface. */
3230 SSMR3GetU8 (pSSM, &pBusLogic->VBoxSCSI.regIdentify);
3231 SSMR3GetU8 (pSSM, &pBusLogic->VBoxSCSI.uTargetDevice);
3232 SSMR3GetU8 (pSSM, &pBusLogic->VBoxSCSI.uTxDir);
3233 SSMR3GetU8 (pSSM, &pBusLogic->VBoxSCSI.cbCDB);
3234 SSMR3GetMem (pSSM, pBusLogic->VBoxSCSI.abCDB, sizeof(pBusLogic->VBoxSCSI.abCDB));
3235 SSMR3GetU8 (pSSM, &pBusLogic->VBoxSCSI.iCDB);
3236 SSMR3GetU32 (pSSM, &pBusLogic->VBoxSCSI.cbBuf);
3237 SSMR3GetU32 (pSSM, &pBusLogic->VBoxSCSI.iBuf);
3238 SSMR3GetBool(pSSM, (bool *)&pBusLogic->VBoxSCSI.fBusy);
3239 SSMR3GetU8 (pSSM, (uint8_t *)&pBusLogic->VBoxSCSI.enmState);
3240 if (pBusLogic->VBoxSCSI.cbBuf)
3241 {
3242 pBusLogic->VBoxSCSI.pbBuf = (uint8_t *)RTMemAllocZ(pBusLogic->VBoxSCSI.cbBuf);
3243 if (!pBusLogic->VBoxSCSI.pbBuf)
3244 {
3245 LogRel(("BusLogic: Out of memory during restore.\n"));
3246 return PDMDEV_SET_ERROR(pDevIns, VERR_NO_MEMORY,
3247 N_("BusLogic: Out of memory during restore\n"));
3248 }
3249 SSMR3GetMem(pSSM, pBusLogic->VBoxSCSI.pbBuf, pBusLogic->VBoxSCSI.cbBuf);
3250 }
3251
3252 if (pBusLogic->VBoxSCSI.fBusy)
3253 pBusLogic->fRedo = true;
3254
3255 if (uVersion > BUSLOGIC_SAVED_STATE_MINOR_PRE_ERROR_HANDLING)
3256 {
3257 /* Check if there are pending tasks saved. */
3258 uint32_t cTasks = 0;
3259
3260 SSMR3GetU32(pSSM, &cTasks);
3261
3262 if (cTasks)
3263 pBusLogic->fRedo = true;
3264
3265 for (uint32_t i = 0; i < cTasks; i++)
3266 {
3267 PBUSLOGICTASKSTATE pTaskState = (PBUSLOGICTASKSTATE)RTMemCacheAlloc(pBusLogic->hTaskCache);
3268 if (!pTaskState)
3269 {
3270 rc = VERR_NO_MEMORY;
3271 break;
3272 }
3273
3274 rc = SSMR3GetU32(pSSM, &pTaskState->MailboxGuest.u32PhysAddrCCB);
3275 if (RT_FAILURE(rc))
3276 {
3277 RTMemCacheFree(pBusLogic->hTaskCache, pTaskState);
3278 break;
3279 }
3280
3281 /* Link into the list. */
3282 pTaskState->pRedoNext = pBusLogic->pTasksRedoHead;
3283 pBusLogic->pTasksRedoHead = pTaskState;
3284 }
3285 }
3286
3287 if (RT_SUCCESS(rc))
3288 {
3289 uint32_t u32;
3290 rc = SSMR3GetU32(pSSM, &u32);
3291 if (RT_SUCCESS(rc))
3292 AssertMsgReturn(u32 == ~0U, ("%#x\n", u32), VERR_SSM_DATA_UNIT_FORMAT_CHANGED);
3293 }
3294
3295 return rc;
3296}
3297
3298/**
3299 * Gets the pointer to the status LED of a device - called from the SCSI driver.
3300 *
3301 * @returns VBox status code.
3302 * @param pInterface Pointer to the interface structure containing the called function pointer.
3303 * @param iLUN The unit which status LED we desire. Always 0 here as the driver
3304 * doesn't know about other LUN's.
3305 * @param ppLed Where to store the LED pointer.
3306 */
3307static DECLCALLBACK(int) buslogicR3DeviceQueryStatusLed(PPDMILEDPORTS pInterface, unsigned iLUN, PPDMLED *ppLed)
3308{
3309 PBUSLOGICDEVICE pDevice = PDMILEDPORTS_2_PBUSLOGICDEVICE(pInterface);
3310 if (iLUN == 0)
3311 {
3312 *ppLed = &pDevice->Led;
3313 Assert((*ppLed)->u32Magic == PDMLED_MAGIC);
3314 return VINF_SUCCESS;
3315 }
3316 return VERR_PDM_LUN_NOT_FOUND;
3317}
3318
3319/**
3320 * @interface_method_impl{PDMIBASE,pfnQueryInterface}
3321 */
3322static DECLCALLBACK(void *) buslogicR3DeviceQueryInterface(PPDMIBASE pInterface, const char *pszIID)
3323{
3324 PBUSLOGICDEVICE pDevice = PDMIBASE_2_PBUSLOGICDEVICE(pInterface);
3325 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIBASE, &pDevice->IBase);
3326 PDMIBASE_RETURN_INTERFACE(pszIID, PDMISCSIPORT, &pDevice->ISCSIPort);
3327 PDMIBASE_RETURN_INTERFACE(pszIID, PDMILEDPORTS, &pDevice->ILed);
3328 return NULL;
3329}
3330
3331/**
3332 * Gets the pointer to the status LED of a unit.
3333 *
3334 * @returns VBox status code.
3335 * @param pInterface Pointer to the interface structure containing the called function pointer.
3336 * @param iLUN The unit which status LED we desire.
3337 * @param ppLed Where to store the LED pointer.
3338 */
3339static DECLCALLBACK(int) buslogicR3StatusQueryStatusLed(PPDMILEDPORTS pInterface, unsigned iLUN, PPDMLED *ppLed)
3340{
3341 PBUSLOGIC pBusLogic = PDMILEDPORTS_2_PBUSLOGIC(pInterface);
3342 if (iLUN < BUSLOGIC_MAX_DEVICES)
3343 {
3344 *ppLed = &pBusLogic->aDeviceStates[iLUN].Led;
3345 Assert((*ppLed)->u32Magic == PDMLED_MAGIC);
3346 return VINF_SUCCESS;
3347 }
3348 return VERR_PDM_LUN_NOT_FOUND;
3349}
3350
3351/**
3352 * @interface_method_impl{PDMIBASE,pfnQueryInterface}
3353 */
3354static DECLCALLBACK(void *) buslogicR3StatusQueryInterface(PPDMIBASE pInterface, const char *pszIID)
3355{
3356 PBUSLOGIC pThis = PDMIBASE_2_PBUSLOGIC(pInterface);
3357 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIBASE, &pThis->IBase);
3358 PDMIBASE_RETURN_INTERFACE(pszIID, PDMILEDPORTS, &pThis->ILeds);
3359 return NULL;
3360}
3361
3362/**
3363 * BusLogic debugger info callback.
3364 *
3365 * @param pDevIns The device instance.
3366 * @param pHlp The output helpers.
3367 * @param pszArgs The arguments.
3368 */
3369static DECLCALLBACK(void) buslogicR3Info(PPDMDEVINS pDevIns, PCDBGFINFOHLP pHlp, const char *pszArgs)
3370{
3371 PBUSLOGIC pThis = PDMINS_2_DATA(pDevIns, PBUSLOGIC);
3372 unsigned i;
3373 bool fVerbose = false;
3374
3375 /* Parse arguments. */
3376 if (pszArgs)
3377 fVerbose = strstr(pszArgs, "verbose") != NULL;
3378
3379 /* Show basic information. */
3380 pHlp->pfnPrintf(pHlp,
3381 "%s#%d: PCI I/O=%RTiop ISA I/O=%RTiop MMIO=%RGp IRQ=%u GC=%RTbool R0=%RTbool\n",
3382 pDevIns->pReg->szName,
3383 pDevIns->iInstance,
3384 pThis->IOPortBase, pThis->IOISABase, pThis->MMIOBase,
3385 PCIDevGetInterruptLine(&pThis->dev),
3386 !!pThis->fGCEnabled, !!pThis->fR0Enabled);
3387
3388 /* Print mailbox state. */
3389 if (pThis->regStatus & BUSLOGIC_REGISTER_STATUS_INITIALIZATION_REQUIRED)
3390 pHlp->pfnPrintf(pHlp, "Mailbox not initialized\n");
3391 else
3392 pHlp->pfnPrintf(pHlp, "%u-bit mailbox with %u entries at %RGp\n",
3393 pThis->fMbxIs24Bit ? 24 : 32, pThis->cMailbox,
3394 pThis->GCPhysAddrMailboxOutgoingBase);
3395
3396 /* Print register contents. */
3397 pHlp->pfnPrintf(pHlp, "Registers: STAT=%02x INTR=%02x GEOM=%02x\n",
3398 pThis->regStatus, pThis->regInterrupt, pThis->regGeometry);
3399
3400 /* Print the current command, if any. */
3401 if (pThis->uOperationCode != 0xff )
3402 pHlp->pfnPrintf(pHlp, "Current command: %02X\n", pThis->uOperationCode);
3403
3404 if (fVerbose && (pThis->regStatus & BUSLOGIC_REGISTER_STATUS_INITIALIZATION_REQUIRED) == 0)
3405 {
3406 RTGCPHYS GCMailbox;
3407
3408 /* Dump the mailbox contents. */
3409 if (pThis->fMbxIs24Bit)
3410 {
3411 Mailbox24 Mbx24;
3412
3413 /* Outgoing mailbox, 24-bit format. */
3414 GCMailbox = pThis->GCPhysAddrMailboxOutgoingBase;
3415 pHlp->pfnPrintf(pHlp, " Outgoing mailbox entries (24-bit) at %06X:\n", GCMailbox);
3416 for (i = 0; i < pThis->cMailbox; ++i)
3417 {
3418 PDMDevHlpPhysRead(pThis->CTX_SUFF(pDevIns), GCMailbox, &Mbx24, sizeof(Mailbox24));
3419 pHlp->pfnPrintf(pHlp, " slot %03d: CCB at %06X action code %02X", i, ADDR_TO_U32(Mbx24.aPhysAddrCCB), Mbx24.uCmdState);
3420 pHlp->pfnPrintf(pHlp, "%s\n", pThis->uMailboxOutgoingPositionCurrent == i ? " *" : "");
3421 GCMailbox += sizeof(Mailbox24);
3422 }
3423
3424 /* Incoming mailbox, 24-bit format. */
3425 GCMailbox = pThis->GCPhysAddrMailboxOutgoingBase + (pThis->cMailbox * sizeof(Mailbox24));
3426 pHlp->pfnPrintf(pHlp, " Incoming mailbox entries (24-bit) at %06X:\n", GCMailbox);
3427 for (i = 0; i < pThis->cMailbox; ++i)
3428 {
3429 PDMDevHlpPhysRead(pThis->CTX_SUFF(pDevIns), GCMailbox, &Mbx24, sizeof(Mailbox24));
3430 pHlp->pfnPrintf(pHlp, " slot %03d: CCB at %06X completion code %02X", i, ADDR_TO_U32(Mbx24.aPhysAddrCCB), Mbx24.uCmdState);
3431 pHlp->pfnPrintf(pHlp, "%s\n", pThis->uMailboxIncomingPositionCurrent == i ? " *" : "");
3432 GCMailbox += sizeof(Mailbox24);
3433 }
3434
3435 }
3436 else
3437 {
3438 Mailbox32 Mbx32;
3439
3440 /* Outgoing mailbox, 32-bit format. */
3441 GCMailbox = pThis->GCPhysAddrMailboxOutgoingBase;
3442 pHlp->pfnPrintf(pHlp, " Outgoing mailbox entries (32-bit) at %08X:\n", (uint32_t)GCMailbox);
3443 for (i = 0; i < pThis->cMailbox; ++i)
3444 {
3445 PDMDevHlpPhysRead(pThis->CTX_SUFF(pDevIns), GCMailbox, &Mbx32, sizeof(Mailbox32));
3446 pHlp->pfnPrintf(pHlp, " slot %03d: CCB at %08X action code %02X", i, Mbx32.u32PhysAddrCCB, Mbx32.u.out.uActionCode);
3447 pHlp->pfnPrintf(pHlp, "%s\n", pThis->uMailboxOutgoingPositionCurrent == i ? " *" : "");
3448 GCMailbox += sizeof(Mailbox32);
3449 }
3450
3451 /* Incoming mailbox, 32-bit format. */
3452 GCMailbox = pThis->GCPhysAddrMailboxOutgoingBase;
3453 pHlp->pfnPrintf(pHlp, " Outgoing mailbox entries (32-bit) at %08X:\n", (uint32_t)GCMailbox);
3454 for (i = 0; i < pThis->cMailbox; ++i)
3455 {
3456 PDMDevHlpPhysRead(pThis->CTX_SUFF(pDevIns), GCMailbox, &Mbx32, sizeof(Mailbox32));
3457 pHlp->pfnPrintf(pHlp, " slot %03d: CCB at %08X completion code %02X BTSTAT %02X SDSTAT %02X", i,
3458 Mbx32.u32PhysAddrCCB, Mbx32.u.in.uCompletionCode, Mbx32.u.in.uHostAdapterStatus, Mbx32.u.in.uTargetDeviceStatus);
3459 pHlp->pfnPrintf(pHlp, "%s\n", pThis->uMailboxOutgoingPositionCurrent == i ? " *" : "");
3460 GCMailbox += sizeof(Mailbox32);
3461 }
3462
3463 }
3464 }
3465}
3466
3467/* -=-=-=-=- Helper -=-=-=-=- */
3468
3469 /**
3470 * Checks if all asynchronous I/O is finished.
3471 *
3472 * Used by buslogicR3Reset, buslogicR3Suspend and buslogicR3PowerOff.
3473 *
3474 * @returns true if quiesced, false if busy.
3475 * @param pDevIns The device instance.
3476 */
3477static bool buslogicR3AllAsyncIOIsFinished(PPDMDEVINS pDevIns)
3478{
3479 PBUSLOGIC pThis = PDMINS_2_DATA(pDevIns, PBUSLOGIC);
3480
3481 for (uint32_t i = 0; i < RT_ELEMENTS(pThis->aDeviceStates); i++)
3482 {
3483 PBUSLOGICDEVICE pThisDevice = &pThis->aDeviceStates[i];
3484 if (pThisDevice->pDrvBase)
3485 {
3486 if (pThisDevice->cOutstandingRequests != 0)
3487 return false;
3488 }
3489 }
3490
3491 return true;
3492}
3493
3494/**
3495 * Callback employed by buslogicR3Suspend and buslogicR3PowerOff..
3496 *
3497 * @returns true if we've quiesced, false if we're still working.
3498 * @param pDevIns The device instance.
3499 */
3500static DECLCALLBACK(bool) buslogicR3IsAsyncSuspendOrPowerOffDone(PPDMDEVINS pDevIns)
3501{
3502 if (!buslogicR3AllAsyncIOIsFinished(pDevIns))
3503 return false;
3504
3505 PBUSLOGIC pThis = PDMINS_2_DATA(pDevIns, PBUSLOGIC);
3506 ASMAtomicWriteBool(&pThis->fSignalIdle, false);
3507 return true;
3508}
3509
3510/**
3511 * Common worker for ahciR3Suspend and ahciR3PowerOff.
3512 */
3513static void buslogicR3SuspendOrPowerOff(PPDMDEVINS pDevIns, bool fPowerOff)
3514{
3515 PBUSLOGIC pThis = PDMINS_2_DATA(pDevIns, PBUSLOGIC);
3516
3517 ASMAtomicWriteBool(&pThis->fSignalIdle, true);
3518 if (!buslogicR3AllAsyncIOIsFinished(pDevIns))
3519 PDMDevHlpSetAsyncNotification(pDevIns, buslogicR3IsAsyncSuspendOrPowerOffDone);
3520 else
3521 {
3522 ASMAtomicWriteBool(&pThis->fSignalIdle, false);
3523
3524 AssertMsg(!pThis->fNotificationSend, ("The PDM Queue should be empty at this point\n"));
3525
3526 if (pThis->fRedo)
3527 {
3528 if (fPowerOff)
3529 {
3530 /* Free tasks which would have been queued again on resume. */
3531 PBUSLOGICTASKSTATE pTaskState = pThis->pTasksRedoHead;
3532
3533 pThis->pTasksRedoHead = NULL;
3534
3535 while (pTaskState)
3536 {
3537 PBUSLOGICTASKSTATE pFree;
3538
3539 pFree = pTaskState;
3540 pTaskState = pTaskState->pRedoNext;
3541
3542 RTMemCacheFree(pThis->hTaskCache, pFree);
3543 }
3544 pThis->fRedo = false;
3545 }
3546 else if (pThis->VBoxSCSI.fBusy)
3547 {
3548 /* Destroy the task because the BIOS interface has all necessary information. */
3549 Assert(pThis->pTasksRedoHead->fBIOS);
3550 Assert(!pThis->pTasksRedoHead->pRedoNext);
3551
3552 RTMemCacheFree(pThis->hTaskCache, pThis->pTasksRedoHead);
3553 pThis->pTasksRedoHead = NULL;
3554 }
3555 }
3556 }
3557}
3558
3559/**
3560 * Suspend notification.
3561 *
3562 * @param pDevIns The device instance data.
3563 */
3564static DECLCALLBACK(void) buslogicR3Suspend(PPDMDEVINS pDevIns)
3565{
3566 Log(("buslogicR3Suspend\n"));
3567 buslogicR3SuspendOrPowerOff(pDevIns, false /* fPoweroff */);
3568}
3569
3570/**
3571 * Resume notification.
3572 *
3573 * @param pDevIns The device instance data.
3574 */
3575static DECLCALLBACK(void) buslogicR3Resume(PPDMDEVINS pDevIns)
3576{
3577 Log(("buslogicR3Resume\n"));
3578 PBUSLOGIC pThis = PDMINS_2_DATA(pDevIns, PBUSLOGIC);
3579 buslogicR3Kick(pThis);
3580}
3581
3582
3583/**
3584 * Detach notification.
3585 *
3586 * One harddisk at one port has been unplugged.
3587 * The VM is suspended at this point.
3588 *
3589 * @param pDevIns The device instance.
3590 * @param iLUN The logical unit which is being detached.
3591 * @param fFlags Flags, combination of the PDMDEVATT_FLAGS_* \#defines.
3592 */
3593static DECLCALLBACK(void) buslogicR3Detach(PPDMDEVINS pDevIns, unsigned iLUN, uint32_t fFlags)
3594{
3595 PBUSLOGIC pThis = PDMINS_2_DATA(pDevIns, PBUSLOGIC);
3596 PBUSLOGICDEVICE pDevice = &pThis->aDeviceStates[iLUN];
3597
3598 Log(("%s:\n", __FUNCTION__));
3599
3600 AssertMsg(fFlags & PDM_TACH_FLAGS_NOT_HOT_PLUG,
3601 ("BusLogic: Device does not support hotplugging\n"));
3602
3603 /*
3604 * Zero some important members.
3605 */
3606 pDevice->pDrvBase = NULL;
3607 pDevice->fPresent = false;
3608 pDevice->pDrvSCSIConnector = NULL;
3609}
3610
3611/**
3612 * Attach command.
3613 *
3614 * This is called when we change block driver.
3615 *
3616 * @returns VBox status code.
3617 * @param pDevIns The device instance.
3618 * @param iLUN The logical unit which is being detached.
3619 * @param fFlags Flags, combination of the PDMDEVATT_FLAGS_* \#defines.
3620 */
3621static DECLCALLBACK(int) buslogicR3Attach(PPDMDEVINS pDevIns, unsigned iLUN, uint32_t fFlags)
3622{
3623 PBUSLOGIC pThis = PDMINS_2_DATA(pDevIns, PBUSLOGIC);
3624 PBUSLOGICDEVICE pDevice = &pThis->aDeviceStates[iLUN];
3625 int rc;
3626
3627 AssertMsgReturn(fFlags & PDM_TACH_FLAGS_NOT_HOT_PLUG,
3628 ("BusLogic: Device does not support hotplugging\n"),
3629 VERR_INVALID_PARAMETER);
3630
3631 /* the usual paranoia */
3632 AssertRelease(!pDevice->pDrvBase);
3633 AssertRelease(!pDevice->pDrvSCSIConnector);
3634 Assert(pDevice->iLUN == iLUN);
3635
3636 /*
3637 * Try attach the block device and get the interfaces,
3638 * required as well as optional.
3639 */
3640 rc = PDMDevHlpDriverAttach(pDevIns, pDevice->iLUN, &pDevice->IBase, &pDevice->pDrvBase, NULL);
3641 if (RT_SUCCESS(rc))
3642 {
3643 /* Get SCSI connector interface. */
3644 pDevice->pDrvSCSIConnector = PDMIBASE_QUERY_INTERFACE(pDevice->pDrvBase, PDMISCSICONNECTOR);
3645 AssertMsgReturn(pDevice->pDrvSCSIConnector, ("Missing SCSI interface below\n"), VERR_PDM_MISSING_INTERFACE);
3646 pDevice->fPresent = true;
3647 }
3648 else
3649 AssertMsgFailed(("Failed to attach LUN#%d. rc=%Rrc\n", pDevice->iLUN, rc));
3650
3651 if (RT_FAILURE(rc))
3652 {
3653 pDevice->pDrvBase = NULL;
3654 pDevice->pDrvSCSIConnector = NULL;
3655 }
3656 return rc;
3657}
3658
3659/**
3660 * Callback employed by buslogicR3Reset.
3661 *
3662 * @returns true if we've quiesced, false if we're still working.
3663 * @param pDevIns The device instance.
3664 */
3665static DECLCALLBACK(bool) buslogicR3IsAsyncResetDone(PPDMDEVINS pDevIns)
3666{
3667 PBUSLOGIC pThis = PDMINS_2_DATA(pDevIns, PBUSLOGIC);
3668
3669 if (!buslogicR3AllAsyncIOIsFinished(pDevIns))
3670 return false;
3671 ASMAtomicWriteBool(&pThis->fSignalIdle, false);
3672
3673 buslogicR3HwReset(pThis, true);
3674 return true;
3675}
3676
3677/**
3678 * @copydoc FNPDMDEVRESET
3679 */
3680static DECLCALLBACK(void) buslogicR3Reset(PPDMDEVINS pDevIns)
3681{
3682 PBUSLOGIC pThis = PDMINS_2_DATA(pDevIns, PBUSLOGIC);
3683
3684 ASMAtomicWriteBool(&pThis->fSignalIdle, true);
3685 if (!buslogicR3AllAsyncIOIsFinished(pDevIns))
3686 PDMDevHlpSetAsyncNotification(pDevIns, buslogicR3IsAsyncResetDone);
3687 else
3688 {
3689 ASMAtomicWriteBool(&pThis->fSignalIdle, false);
3690 buslogicR3HwReset(pThis, true);
3691 }
3692}
3693
3694static DECLCALLBACK(void) buslogicR3Relocate(PPDMDEVINS pDevIns, RTGCINTPTR offDelta)
3695{
3696 PBUSLOGIC pThis = PDMINS_2_DATA(pDevIns, PBUSLOGIC);
3697
3698 pThis->pDevInsRC = PDMDEVINS_2_RCPTR(pDevIns);
3699 pThis->pNotifierQueueRC = PDMQueueRCPtr(pThis->pNotifierQueueR3);
3700
3701 for (uint32_t i = 0; i < BUSLOGIC_MAX_DEVICES; i++)
3702 {
3703 PBUSLOGICDEVICE pDevice = &pThis->aDeviceStates[i];
3704
3705 pDevice->pBusLogicRC = PDMINS_2_DATA_RCPTR(pDevIns);
3706 }
3707
3708}
3709
3710/**
3711 * Poweroff notification.
3712 *
3713 * @param pDevIns Pointer to the device instance
3714 */
3715static DECLCALLBACK(void) buslogicR3PowerOff(PPDMDEVINS pDevIns)
3716{
3717 Log(("buslogicR3PowerOff\n"));
3718 buslogicR3SuspendOrPowerOff(pDevIns, true /* fPoweroff */);
3719}
3720
3721/**
3722 * Destroy a driver instance.
3723 *
3724 * Most VM resources are freed by the VM. This callback is provided so that any non-VM
3725 * resources can be freed correctly.
3726 *
3727 * @param pDevIns The device instance data.
3728 */
3729static DECLCALLBACK(int) buslogicR3Destruct(PPDMDEVINS pDevIns)
3730{
3731 PBUSLOGIC pThis = PDMINS_2_DATA(pDevIns, PBUSLOGIC);
3732 PDMDEV_CHECK_VERSIONS_RETURN_QUIET(pDevIns);
3733
3734 PDMR3CritSectDelete(&pThis->CritSectIntr);
3735
3736 /*
3737 * Free all tasks which are still hanging around
3738 * (Power off after the VM was suspended).
3739 */
3740 if (pThis->fRedo)
3741 {
3742 /* Free tasks which would have been queued again on resume. */
3743 PBUSLOGICTASKSTATE pTaskState = pThis->pTasksRedoHead;
3744
3745 pThis->pTasksRedoHead = NULL;
3746
3747 while (pTaskState)
3748 {
3749 PBUSLOGICTASKSTATE pFree;
3750
3751 pFree = pTaskState;
3752 pTaskState = pTaskState->pRedoNext;
3753
3754 RTMemCacheFree(pThis->hTaskCache, pFree);
3755 }
3756 pThis->fRedo = false;
3757 }
3758
3759 int rc = RTMemCacheDestroy(pThis->hTaskCache);
3760 AssertMsgRC(rc, ("Destroying task cache failed rc=%Rrc\n", rc));
3761
3762 return rc;
3763}
3764
3765/**
3766 * @interface_method_impl{PDMDEVREG,pfnConstruct}
3767 */
3768static DECLCALLBACK(int) buslogicR3Construct(PPDMDEVINS pDevIns, int iInstance, PCFGMNODE pCfg)
3769{
3770 PBUSLOGIC pThis = PDMINS_2_DATA(pDevIns, PBUSLOGIC);
3771 int rc = VINF_SUCCESS;
3772 bool fBootable = true;
3773 char achISACompat[16];
3774 PDMDEV_CHECK_VERSIONS_RETURN(pDevIns);
3775
3776 /*
3777 * Init instance data (do early because of constructor).
3778 */
3779 pThis->hTaskCache = NIL_RTMEMCACHE;
3780 pThis->pDevInsR3 = pDevIns;
3781 pThis->pDevInsR0 = PDMDEVINS_2_R0PTR(pDevIns);
3782 pThis->pDevInsRC = PDMDEVINS_2_RCPTR(pDevIns);
3783 pThis->IBase.pfnQueryInterface = buslogicR3StatusQueryInterface;
3784 pThis->ILeds.pfnQueryStatusLed = buslogicR3StatusQueryStatusLed;
3785
3786 PCIDevSetVendorId (&pThis->dev, 0x104b); /* BusLogic */
3787 PCIDevSetDeviceId (&pThis->dev, 0x1040); /* BT-958 */
3788 PCIDevSetCommand (&pThis->dev, 0x0003);
3789 PCIDevSetRevisionId (&pThis->dev, 0x01);
3790 PCIDevSetClassProg (&pThis->dev, 0x00); /* SCSI */
3791 PCIDevSetClassSub (&pThis->dev, 0x00); /* SCSI */
3792 PCIDevSetClassBase (&pThis->dev, 0x01); /* Mass storage */
3793 PCIDevSetBaseAddress (&pThis->dev, 0, true /*IO*/, false /*Pref*/, false /*64-bit*/, 0x00000000);
3794 PCIDevSetBaseAddress (&pThis->dev, 1, false /*IO*/, false /*Pref*/, false /*64-bit*/, 0x00000000);
3795 PCIDevSetSubSystemVendorId(&pThis->dev, 0x104b);
3796 PCIDevSetSubSystemId (&pThis->dev, 0x1040);
3797 PCIDevSetInterruptLine (&pThis->dev, 0x00);
3798 PCIDevSetInterruptPin (&pThis->dev, 0x01);
3799
3800 /*
3801 * Validate and read configuration.
3802 */
3803 if (!CFGMR3AreValuesValid(pCfg,
3804 "GCEnabled\0"
3805 "R0Enabled\0"
3806 "Bootable\0"
3807 "ISACompat\0"))
3808 return PDMDEV_SET_ERROR(pDevIns, VERR_PDM_DEVINS_UNKNOWN_CFG_VALUES,
3809 N_("BusLogic configuration error: unknown option specified"));
3810
3811 rc = CFGMR3QueryBoolDef(pCfg, "GCEnabled", &pThis->fGCEnabled, true);
3812 if (RT_FAILURE(rc))
3813 return PDMDEV_SET_ERROR(pDevIns, rc,
3814 N_("BusLogic configuration error: failed to read GCEnabled as boolean"));
3815 Log(("%s: fGCEnabled=%d\n", __FUNCTION__, pThis->fGCEnabled));
3816
3817 rc = CFGMR3QueryBoolDef(pCfg, "R0Enabled", &pThis->fR0Enabled, true);
3818 if (RT_FAILURE(rc))
3819 return PDMDEV_SET_ERROR(pDevIns, rc,
3820 N_("BusLogic configuration error: failed to read R0Enabled as boolean"));
3821 Log(("%s: fR0Enabled=%d\n", __FUNCTION__, pThis->fR0Enabled));
3822 rc = CFGMR3QueryBoolDef(pCfg, "Bootable", &fBootable, true);
3823 if (RT_FAILURE(rc))
3824 return PDMDEV_SET_ERROR(pDevIns, rc,
3825 N_("BusLogic configuration error: failed to read Bootable as boolean"));
3826 Log(("%s: fBootable=%RTbool\n", __FUNCTION__, fBootable));
3827 rc = CFGMR3QueryStringDef(pCfg, "ISACompat", achISACompat, sizeof(achISACompat), "Alternate");
3828 if (RT_FAILURE(rc))
3829 return PDMDEV_SET_ERROR(pDevIns, rc,
3830 N_("BusLogic configuration error: failed to read ISACompat as string"));
3831 Log(("%s: ISACompat=%s\n", __FUNCTION__, achISACompat));
3832
3833 /* Grok the ISACompat setting. */
3834 if (!strcmp(achISACompat, "Disabled"))
3835 pThis->uDefaultISABaseCode = ISA_BASE_DISABLED;
3836 else if (!strcmp(achISACompat, "Primary"))
3837 pThis->uDefaultISABaseCode = 0; /* I/O base at 330h. */
3838 else if (!strcmp(achISACompat, "Alternate"))
3839 pThis->uDefaultISABaseCode = 1; /* I/O base at 334h. */
3840 else
3841 return PDMDEV_SET_ERROR(pDevIns, VERR_PDM_DEVINS_UNKNOWN_CFG_VALUES,
3842 N_("BusLogic configuration error: invalid ISACompat setting"));
3843
3844 /*
3845 * Register the PCI device and its I/O regions.
3846 */
3847 rc = PDMDevHlpPCIRegister(pDevIns, &pThis->dev);
3848 if (RT_FAILURE(rc))
3849 return rc;
3850
3851 rc = PDMDevHlpPCIIORegionRegister(pDevIns, 0, 32, PCI_ADDRESS_SPACE_IO, buslogicR3MmioMap);
3852 if (RT_FAILURE(rc))
3853 return rc;
3854
3855 rc = PDMDevHlpPCIIORegionRegister(pDevIns, 1, 32, PCI_ADDRESS_SPACE_MEM, buslogicR3MmioMap);
3856 if (RT_FAILURE(rc))
3857 return rc;
3858
3859 if (fBootable)
3860 {
3861 /* Register I/O port space for BIOS access. */
3862 rc = PDMDevHlpIOPortRegister(pDevIns, BUSLOGIC_BIOS_IO_PORT, 4, NULL,
3863 buslogicR3BiosIoPortWrite, buslogicR3BiosIoPortRead,
3864 buslogicR3BiosIoPortWriteStr, buslogicR3BiosIoPortReadStr,
3865 "BusLogic BIOS");
3866 if (RT_FAILURE(rc))
3867 return PDMDEV_SET_ERROR(pDevIns, rc, N_("BusLogic cannot register BIOS I/O handlers"));
3868 }
3869
3870 /* Set up the compatibility I/O range. */
3871 rc = buslogicR3RegisterISARange(pThis, pThis->uDefaultISABaseCode);
3872 if (RT_FAILURE(rc))
3873 return PDMDEV_SET_ERROR(pDevIns, rc, N_("BusLogic cannot register ISA I/O handlers"));
3874
3875 /* Initialize task cache. */
3876 rc = RTMemCacheCreate(&pThis->hTaskCache, sizeof(BUSLOGICTASKSTATE), 0, UINT32_MAX,
3877 NULL, NULL, NULL, 0);
3878 if (RT_FAILURE(rc))
3879 return PDMDEV_SET_ERROR(pDevIns, rc,
3880 N_("BusLogic: Failed to initialize task cache\n"));
3881
3882 /* Initialize task queue. */
3883 rc = PDMDevHlpQueueCreate(pDevIns, sizeof(PDMQUEUEITEMCORE), 5, 0,
3884 buslogicR3NotifyQueueConsumer, true, "BusLogicTask", &pThis->pNotifierQueueR3);
3885 if (RT_FAILURE(rc))
3886 return rc;
3887 pThis->pNotifierQueueR0 = PDMQueueR0Ptr(pThis->pNotifierQueueR3);
3888 pThis->pNotifierQueueRC = PDMQueueRCPtr(pThis->pNotifierQueueR3);
3889
3890 rc = PDMDevHlpCritSectInit(pDevIns, &pThis->CritSectIntr, RT_SRC_POS, "BusLogic-Intr#%u", pDevIns->iInstance);
3891 if (RT_FAILURE(rc))
3892 return PDMDEV_SET_ERROR(pDevIns, rc, N_("BusLogic: cannot create critical section"));
3893
3894 /* Initialize per device state. */
3895 for (unsigned i = 0; i < RT_ELEMENTS(pThis->aDeviceStates); i++)
3896 {
3897 char szName[24];
3898 PBUSLOGICDEVICE pDevice = &pThis->aDeviceStates[i];
3899
3900 RTStrPrintf(szName, sizeof(szName), "Device%u", i);
3901
3902 /* Initialize static parts of the device. */
3903 pDevice->iLUN = i;
3904 pDevice->pBusLogicR3 = pThis;
3905 pDevice->pBusLogicR0 = PDMINS_2_DATA_R0PTR(pDevIns);
3906 pDevice->pBusLogicRC = PDMINS_2_DATA_RCPTR(pDevIns);
3907 pDevice->Led.u32Magic = PDMLED_MAGIC;
3908 pDevice->IBase.pfnQueryInterface = buslogicR3DeviceQueryInterface;
3909 pDevice->ISCSIPort.pfnSCSIRequestCompleted = buslogicR3DeviceSCSIRequestCompleted;
3910 pDevice->ISCSIPort.pfnQueryDeviceLocation = buslogicR3QueryDeviceLocation;
3911 pDevice->ILed.pfnQueryStatusLed = buslogicR3DeviceQueryStatusLed;
3912
3913 /* Attach SCSI driver. */
3914 rc = PDMDevHlpDriverAttach(pDevIns, pDevice->iLUN, &pDevice->IBase, &pDevice->pDrvBase, szName);
3915 if (RT_SUCCESS(rc))
3916 {
3917 /* Get SCSI connector interface. */
3918 pDevice->pDrvSCSIConnector = PDMIBASE_QUERY_INTERFACE(pDevice->pDrvBase, PDMISCSICONNECTOR);
3919 AssertMsgReturn(pDevice->pDrvSCSIConnector, ("Missing SCSI interface below\n"), VERR_PDM_MISSING_INTERFACE);
3920
3921 pDevice->fPresent = true;
3922 }
3923 else if (rc == VERR_PDM_NO_ATTACHED_DRIVER)
3924 {
3925 pDevice->pDrvBase = NULL;
3926 pDevice->fPresent = false;
3927 rc = VINF_SUCCESS;
3928 Log(("BusLogic: no driver attached to device %s\n", szName));
3929 }
3930 else
3931 {
3932 AssertLogRelMsgFailed(("BusLogic: Failed to attach %s\n", szName));
3933 return rc;
3934 }
3935 }
3936
3937 /*
3938 * Attach status driver (optional).
3939 */
3940 PPDMIBASE pBase;
3941 rc = PDMDevHlpDriverAttach(pDevIns, PDM_STATUS_LUN, &pThis->IBase, &pBase, "Status Port");
3942 if (RT_SUCCESS(rc))
3943 pThis->pLedsConnector = PDMIBASE_QUERY_INTERFACE(pBase, PDMILEDCONNECTORS);
3944 else if (rc != VERR_PDM_NO_ATTACHED_DRIVER)
3945 {
3946 AssertMsgFailed(("Failed to attach to status driver. rc=%Rrc\n", rc));
3947 return PDMDEV_SET_ERROR(pDevIns, rc, N_("BusLogic cannot attach to status driver"));
3948 }
3949
3950 rc = PDMDevHlpSSMRegisterEx(pDevIns, BUSLOGIC_SAVED_STATE_MINOR_VERSION, sizeof(*pThis), NULL,
3951 NULL, buslogicR3LiveExec, NULL,
3952 NULL, buslogicR3SaveExec, NULL,
3953 NULL, buslogicR3LoadExec, buslogicR3LoadDone);
3954 if (RT_FAILURE(rc))
3955 return PDMDEV_SET_ERROR(pDevIns, rc, N_("BusLogic cannot register save state handlers"));
3956
3957 /*
3958 * Register the debugger info callback.
3959 */
3960 char szTmp[128];
3961 RTStrPrintf(szTmp, sizeof(szTmp), "%s%d", pDevIns->pReg->szName, pDevIns->iInstance);
3962 PDMDevHlpDBGFInfoRegister(pDevIns, szTmp, "BusLogic HBA info", buslogicR3Info);
3963
3964 rc = buslogicR3HwReset(pThis, true);
3965 AssertMsgRC(rc, ("hardware reset of BusLogic host adapter failed rc=%Rrc\n", rc));
3966
3967 return rc;
3968}
3969
3970/**
3971 * The device registration structure.
3972 */
3973const PDMDEVREG g_DeviceBusLogic =
3974{
3975 /* u32Version */
3976 PDM_DEVREG_VERSION,
3977 /* szName */
3978 "buslogic",
3979 /* szRCMod */
3980 "VBoxDDGC.gc",
3981 /* szR0Mod */
3982 "VBoxDDR0.r0",
3983 /* pszDescription */
3984 "BusLogic BT-958 SCSI host adapter.\n",
3985 /* fFlags */
3986 PDM_DEVREG_FLAGS_DEFAULT_BITS | PDM_DEVREG_FLAGS_RC | PDM_DEVREG_FLAGS_R0 |
3987 PDM_DEVREG_FLAGS_FIRST_SUSPEND_NOTIFICATION | PDM_DEVREG_FLAGS_FIRST_POWEROFF_NOTIFICATION |
3988 PDM_DEVREG_FLAGS_FIRST_RESET_NOTIFICATION,
3989 /* fClass */
3990 PDM_DEVREG_CLASS_STORAGE,
3991 /* cMaxInstances */
3992 ~0U,
3993 /* cbInstance */
3994 sizeof(BUSLOGIC),
3995 /* pfnConstruct */
3996 buslogicR3Construct,
3997 /* pfnDestruct */
3998 buslogicR3Destruct,
3999 /* pfnRelocate */
4000 buslogicR3Relocate,
4001 /* pfnIOCtl */
4002 NULL,
4003 /* pfnPowerOn */
4004 NULL,
4005 /* pfnReset */
4006 buslogicR3Reset,
4007 /* pfnSuspend */
4008 buslogicR3Suspend,
4009 /* pfnResume */
4010 buslogicR3Resume,
4011 /* pfnAttach */
4012 buslogicR3Attach,
4013 /* pfnDetach */
4014 buslogicR3Detach,
4015 /* pfnQueryInterface. */
4016 NULL,
4017 /* pfnInitComplete */
4018 NULL,
4019 /* pfnPowerOff */
4020 buslogicR3PowerOff,
4021 /* pfnSoftReset */
4022 NULL,
4023 /* u32VersionEnd */
4024 PDM_DEVREG_VERSION
4025};
4026
4027#endif /* IN_RING3 */
4028#endif /* !VBOX_DEVICE_STRUCT_TESTCASE */
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