VirtualBox

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

Last change on this file since 88037 was 88037, checked in by vboxsync, 4 years ago

DevBusLogic: Track last completed command, properly clear mailbox setup on reset, validate PCI ISA I/O arguments (see bugref:5112).

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