VirtualBox

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

Last change on this file since 63500 was 63478, checked in by vboxsync, 8 years ago

Devices: warnings (clang)

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