VirtualBox

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

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

Synced code and comment again.

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

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