VirtualBox

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

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

Devices/Storage: Convert to use new PDMDevHlp guest memory read/write helpers differentiating between metadata/userdata transfers for tracing, bugref:9210

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