VirtualBox

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

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

BusLogic: Added a semi-credible AHA-1540B emulation.

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