VirtualBox

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

Last change on this file since 75100 was 75100, checked in by vboxsync, 7 years ago

BusLogic: Added option to emulate an ISA device without any PCI bits.

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