VirtualBox

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

Last change on this file since 81835 was 81835, checked in by vboxsync, 5 years ago

DevBusLogic.cpp: Use pThis like everyone else. bugref:9218

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