VirtualBox

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

Last change on this file since 81804 was 81804, checked in by vboxsync, 6 years ago

DevBusLogic.cpp: Converting it to the new PDM device style - eliminated the wake-up queue. Simplifed/fixed device naming. bugref:9218

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

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