VirtualBox

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

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

Alignment.

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