VirtualBox

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

Last change on this file since 38791 was 38782, checked in by vboxsync, 13 years ago

Typos.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 124.6 KB
Line 
1/* $Id: DevBusLogic.cpp 38782 2011-09-16 16:15:00Z vboxsync $ */
2/** @file
3 * VBox storage devices: BusLogic SCSI host adapter BT-958.
4 */
5
6/*
7 * Copyright (C) 2006-2009 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 */
17
18/* Implemented looking at the driver source in the linux kernel (drivers/scsi/BusLogic.[ch]).
19 * See also: http://www.drdobbs.com/184410111
20 */
21
22/*******************************************************************************
23* Header Files *
24*******************************************************************************/
25//#define DEBUG
26#define LOG_GROUP LOG_GROUP_DEV_BUSLOGIC
27#include <VBox/vmm/pdmdev.h>
28#include <VBox/vmm/pdmifs.h>
29#include <VBox/vmm/pdmcritsect.h>
30#include <VBox/scsi.h>
31#include <iprt/asm.h>
32#include <iprt/assert.h>
33#include <iprt/string.h>
34#include <iprt/log.h>
35#ifdef IN_RING3
36# include <iprt/alloc.h>
37# include <iprt/memcache.h>
38# include <iprt/param.h>
39# include <iprt/uuid.h>
40#endif
41
42#include "VBoxSCSI.h"
43#include "VBoxDD.h"
44
45/* Maximum number of attached devices the adapter can handle. */
46#define BUSLOGIC_MAX_DEVICES 16
47
48/* Maximum number of scatter gather elements this device can handle. */
49#define BUSLOGIC_MAX_SCATTER_GATHER_LIST_SIZE 128
50
51/* Size of the command buffer. */
52#define BUSLOGIC_COMMAND_SIZE_MAX 5
53
54/* Size of the reply buffer. */
55#define BUSLOGIC_REPLY_SIZE_MAX 64
56
57/* I/O port registered in the ISA compatible range to let the BIOS access
58 * the controller.
59 */
60#define BUSLOGIC_ISA_IO_PORT 0x330
61
62/** State saved version. */
63#define BUSLOGIC_SAVED_STATE_MINOR_VERSION 2
64
65/** Saved state version before the suspend on error feature was implemented. */
66#define BUSLOGIC_SAVED_STATE_MINOR_PRE_ERROR_HANDLING 1
67
68/**
69 * State of a device attached to the buslogic host adapter.
70 *
71 * @implements PDMIBASE
72 * @implements PDMISCSIPORT
73 * @implements PDMILEDPORTS
74 */
75typedef struct BUSLOGICDEVICE
76{
77 /** Pointer to the owning buslogic device instance. - R3 pointer */
78 R3PTRTYPE(struct BUSLOGIC *) pBusLogicR3;
79 /** Pointer to the owning buslogic device instance. - R0 pointer */
80 R0PTRTYPE(struct BUSLOGIC *) pBusLogicR0;
81 /** Pointer to the owning buslogic device instance. - RC pointer */
82 RCPTRTYPE(struct BUSLOGIC *) pBusLogicRC;
83
84 /** Flag whether device is present. */
85 bool fPresent;
86 /** LUN of the device. */
87 RTUINT iLUN;
88
89#if HC_ARCH_BITS == 64
90 uint32_t Alignment0;
91#endif
92
93 /** Our base interface. */
94 PDMIBASE IBase;
95 /** SCSI port interface. */
96 PDMISCSIPORT ISCSIPort;
97 /** Led interface. */
98 PDMILEDPORTS ILed;
99 /** Pointer to the attached driver's base interface. */
100 R3PTRTYPE(PPDMIBASE) pDrvBase;
101 /** Pointer to the underlying SCSI connector interface. */
102 R3PTRTYPE(PPDMISCSICONNECTOR) pDrvSCSIConnector;
103 /** The status LED state for this device. */
104 PDMLED Led;
105
106#if HC_ARCH_BITS == 64
107 uint32_t Alignment1;
108#endif
109
110 /** Number of outstanding tasks on the port. */
111 volatile uint32_t cOutstandingRequests;
112
113} BUSLOGICDEVICE, *PBUSLOGICDEVICE;
114
115/*
116 * Commands the BusLogic adapter supports.
117 */
118enum BUSLOGICCOMMAND
119{
120 BUSLOGICCOMMAND_TEST_COMMAND_COMPLETE_INTERRUPT = 0x00,
121 BUSLOGICCOMMAND_INITIALIZE_MAILBOX = 0x01,
122 BUSLOGICCOMMAND_EXECUTE_MAILBOX_COMMAND = 0x02,
123 BUSLOGICCOMMAND_EXECUTE_BIOS_COMMAND = 0x03,
124 BUSLOGICCOMMAND_INQUIRE_BOARD_ID = 0x04,
125 BUSLOGICCOMMAND_ENABLE_OUTGOING_MAILBOX_AVAILABLE_INTERRUPT = 0x05,
126 BUSLOGICCOMMAND_SET_SCSI_SELECTION_TIMEOUT = 0x06,
127 BUSLOGICCOMMAND_SET_PREEMPT_TIME_ON_BUS = 0x07,
128 BUSLOGICCOMMAND_SET_TIME_OFF_BUS = 0x08,
129 BUSLOGICCOMMAND_SET_BUS_TRANSFER_RATE = 0x09,
130 BUSLOGICCOMMAND_INQUIRE_INSTALLED_DEVICES_ID_0_TO_7 = 0x0a,
131 BUSLOGICCOMMAND_INQUIRE_CONFIGURATION = 0x0b,
132 BUSLOGICCOMMAND_ENABLE_TARGET_MODE = 0x0c,
133 BUSLOGICCOMMAND_INQUIRE_SETUP_INFORMATION = 0x0d,
134 BUSLOGICCOMMAND_WRITE_ADAPTER_LOCAL_RAM = 0x1a,
135 BUSLOGICCOMMAND_READ_ADAPTER_LOCAL_RAM = 0x1b,
136 BUSLOGICCOMMAND_WRITE_BUSMASTER_CHIP_FIFO = 0x1c,
137 BUSLOGICCOMMAND_READ_BUSMASTER_CHIP_FIFO = 0x1d,
138 BUSLOGICCOMMAND_ECHO_COMMAND_DATA = 0x1f,
139 BUSLOGICCOMMAND_HOST_ADAPTER_DIAGNOSTIC = 0x20,
140 BUSLOGICCOMMAND_SET_ADAPTER_OPTIONS = 0x21,
141 BUSLOGICCOMMAND_INQUIRE_INSTALLED_DEVICES_ID_8_TO_15 = 0x23,
142 BUSLOGICCOMMAND_INQUIRE_TARGET_DEVICES = 0x24,
143 BUSLOGICCOMMAND_DISABLE_HOST_ADAPTER_INTERRUPT = 0x25,
144 BUSLOGICCOMMAND_EXT_BIOS_INFO = 0x28,
145 BUSLOGICCOMMAND_UNLOCK_MAILBOX = 0x29,
146 BUSLOGICCOMMAND_INITIALIZE_EXTENDED_MAILBOX = 0x81,
147 BUSLOGICCOMMAND_EXECUTE_SCSI_COMMAND = 0x83,
148 BUSLOGICCOMMAND_INQUIRE_FIRMWARE_VERSION_3RD_LETTER = 0x84,
149 BUSLOGICCOMMAND_INQUIRE_FIRMWARE_VERSION_LETTER = 0x85,
150 BUSLOGICCOMMAND_INQUIRE_PCI_HOST_ADAPTER_INFORMATION = 0x86,
151 BUSLOGICCOMMAND_INQUIRE_HOST_ADAPTER_MODEL_NUMBER = 0x8b,
152 BUSLOGICCOMMAND_INQUIRE_SYNCHRONOUS_PERIOD = 0x8c,
153 BUSLOGICCOMMAND_INQUIRE_EXTENDED_SETUP_INFORMATION = 0x8d,
154 BUSLOGICCOMMAND_ENABLE_STRICT_ROUND_ROBIN_MODE = 0x8f,
155 BUSLOGICCOMMAND_STORE_HOST_ADAPTER_LOCAL_RAM = 0x90,
156 BUSLOGICCOMMAND_FETCH_HOST_ADAPTER_LOCAL_RAM = 0x91,
157 BUSLOGICCOMMAND_STORE_LOCAL_DATA_IN_EEPROM = 0x92,
158 BUSLOGICCOMMAND_UPLOAD_AUTO_SCSI_CODE = 0x94,
159 BUSLOGICCOMMAND_MODIFY_IO_ADDRESS = 0x95,
160 BUSLOGICCOMMAND_SET_CCB_FORMAT = 0x96,
161 BUSLOGICCOMMAND_WRITE_INQUIRY_BUFFER = 0x9a,
162 BUSLOGICCOMMAND_READ_INQUIRY_BUFFER = 0x9b,
163 BUSLOGICCOMMAND_FLASH_ROM_UPLOAD_DOWNLOAD = 0xa7,
164 BUSLOGICCOMMAND_READ_SCAM_DATA = 0xa8,
165 BUSLOGICCOMMAND_WRITE_SCAM_DATA = 0xa9
166} BUSLOGICCOMMAND;
167
168#pragma pack(1)
169/**
170 * Auto SCSI structure which is located
171 * in host adapter RAM and contains several
172 * configuration parameters.
173 */
174typedef struct AutoSCSIRam
175{
176 uint8_t aInternalSignature[2];
177 uint8_t cbInformation;
178 uint8_t aHostAdaptertype[6];
179 uint8_t uReserved1;
180 bool fFloppyEnabled: 1;
181 bool fFloppySecondary: 1;
182 bool fLevelSensitiveInterrupt: 1;
183 unsigned char uReserved2: 2;
184 unsigned char uSystemRAMAreForBIOS: 3;
185 unsigned char uDMAChannel: 7;
186 bool fDMAAutoConfiguration: 1;
187 unsigned char uIrqChannel: 7;
188 bool fIrqAutoConfiguration: 1;
189 uint8_t uDMATransferRate;
190 uint8_t uSCSIId;
191 bool fLowByteTerminated: 1;
192 bool fParityCheckingEnabled: 1;
193 bool fHighByteTerminated: 1;
194 bool fNoisyCablingEnvironment: 1;
195 bool fFastSynchronousNeogtiation: 1;
196 bool fBusResetEnabled: 1;
197 bool fReserved3: 1;
198 bool fActiveNegotiationEnabled: 1;
199 uint8_t uBusOnDelay;
200 uint8_t uBusOffDelay;
201 bool fHostAdapterBIOSEnabled: 1;
202 bool fBIOSRedirectionOfInt19: 1;
203 bool fExtendedTranslation: 1;
204 bool fMapRemovableAsFixed: 1;
205 bool fReserved4: 1;
206 bool fBIOSSupportsMoreThan2Drives: 1;
207 bool fBIOSInterruptMode: 1;
208 bool fFlopticalSupport: 1;
209 uint16_t u16DeviceEnabledMask;
210 uint16_t u16WidePermittedMask;
211 uint16_t u16FastPermittedMask;
212 uint16_t u16SynchronousPermittedMask;
213 uint16_t u16DisconnectPermittedMask;
214 uint16_t u16SendStartUnitCommandMask;
215 uint16_t u16IgnoreInBIOSScanMask;
216 unsigned char uPCIInterruptPin: 2;
217 unsigned char uHostAdapterIoPortAddress: 2;
218 bool fStrictRoundRobinMode: 1;
219 bool fVesaBusSpeedGreaterThan33MHz: 1;
220 bool fVesaBurstWrite: 1;
221 bool fVesaBurstRead: 1;
222 uint16_t u16UltraPermittedMask;
223 uint32_t uReserved5;
224 uint8_t uReserved6;
225 uint8_t uAutoSCSIMaximumLUN;
226 bool fReserved7: 1;
227 bool fSCAMDominant: 1;
228 bool fSCAMenabled: 1;
229 bool fSCAMLevel2: 1;
230 unsigned char uReserved8: 4;
231 bool fInt13Extension: 1;
232 bool fReserved9: 1;
233 bool fCDROMBoot: 1;
234 unsigned char uReserved10: 5;
235 unsigned char uBootTargetId: 4;
236 unsigned char uBootChannel: 4;
237 bool fForceBusDeviceScanningOrder: 1;
238 unsigned char uReserved11: 7;
239 uint16_t u16NonTaggedToAlternateLunPermittedMask;
240 uint16_t u16RenegotiateSyncAfterCheckConditionMask;
241 uint8_t aReserved12[10];
242 uint8_t aManufacturingDiagnostic[2];
243 uint16_t u16Checksum;
244} AutoSCSIRam, *PAutoSCSIRam;
245AssertCompileSize(AutoSCSIRam, 64);
246#pragma pack()
247
248#pragma pack(1)
249/**
250 * The local Ram.
251 */
252typedef union HostAdapterLocalRam
253{
254 /* Byte view. */
255 uint8_t u8View[256];
256 /* Structured view. */
257 struct
258 {
259 /** Offset 0 - 63 is for BIOS. */
260 uint8_t u8Bios[64];
261 /** Auto SCSI structure. */
262 AutoSCSIRam autoSCSIData;
263 } structured;
264} HostAdapterLocalRam, *PHostAdapterLocalRam;
265AssertCompileSize(HostAdapterLocalRam, 256);
266#pragma pack()
267
268/** Pointer to a task state structure. */
269typedef struct BUSLOGICTASKSTATE *PBUSLOGICTASKSTATE;
270
271/**
272 * Main BusLogic device state.
273 *
274 * @extends PCIDEVICE
275 * @implements PDMILEDPORTS
276 */
277typedef struct BUSLOGIC
278{
279 /** The PCI device structure. */
280 PCIDEVICE dev;
281 /** Pointer to the device instance - HC ptr */
282 PPDMDEVINSR3 pDevInsR3;
283 /** Pointer to the device instance - R0 ptr */
284 PPDMDEVINSR0 pDevInsR0;
285 /** Pointer to the device instance - RC ptr. */
286 PPDMDEVINSRC pDevInsRC;
287
288 /** Whether R0 is enabled. */
289 bool fR0Enabled;
290 /** Whether RC is enabled. */
291 bool fGCEnabled;
292
293 /** Base address of the I/O ports. */
294 RTIOPORT IOPortBase;
295 /** Base address of the memory mapping. */
296 RTGCPHYS MMIOBase;
297 /** Status register - Readonly. */
298 volatile uint8_t regStatus;
299 /** Interrupt register - Readonly. */
300 volatile uint8_t regInterrupt;
301 /** Geometry register - Readonly. */
302 volatile uint8_t regGeometry;
303
304 /** Local RAM for the fetch hostadapter local RAM request.
305 * I don't know how big the buffer really is but the maximum
306 * seems to be 256 bytes because the offset and count field in the command request
307 * are only one byte big.
308 */
309 HostAdapterLocalRam LocalRam;
310
311 /** Command code the guest issued. */
312 uint8_t uOperationCode;
313 /** Buffer for the command parameters the adapter is currently receiving from the guest.
314 * Size of the largest command which is possible.
315 */
316 uint8_t aCommandBuffer[BUSLOGIC_COMMAND_SIZE_MAX]; /* Size of the biggest request. */
317 /** Current position in the command buffer. */
318 uint8_t iParameter;
319 /** Parameters left until the command is complete. */
320 uint8_t cbCommandParametersLeft;
321
322 /** Whether we are using the RAM or reply buffer. */
323 bool fUseLocalRam;
324 /** Buffer to store reply data from the controller to the guest. */
325 uint8_t aReplyBuffer[BUSLOGIC_REPLY_SIZE_MAX]; /* Size of the biggest reply. */
326 /** Position in the buffer we are reading next. */
327 uint8_t iReply;
328 /** Bytes left until the reply buffer is empty. */
329 uint8_t cbReplyParametersLeft;
330
331 /** Flag whether IRQs are enabled. */
332 bool fIRQEnabled;
333 /** Flag whether the ISA I/O port range is disabled
334 * to prevent the BIOs to access the device. */
335 bool fISAEnabled;
336
337 /** Number of mailboxes the guest set up. */
338 uint32_t cMailbox;
339
340#if HC_ARCH_BITS == 64
341 uint32_t Alignment0;
342#endif
343
344 /** Physical base address of the outgoing mailboxes. */
345 RTGCPHYS GCPhysAddrMailboxOutgoingBase;
346 /** Current outgoing mailbox position. */
347 uint32_t uMailboxOutgoingPositionCurrent;
348 /** Number of mailboxes ready. */
349 volatile uint32_t cMailboxesReady;
350 /** Whether a notification to R3 was send. */
351 volatile bool fNotificationSend;
352
353#if HC_ARCH_BITS == 64
354 uint32_t Alignment1;
355#endif
356
357 /** Physical base address of the incoming mailboxes. */
358 RTGCPHYS GCPhysAddrMailboxIncomingBase;
359 /** Current incoming mailbox position. */
360 uint32_t uMailboxIncomingPositionCurrent;
361
362 /** Whether strict round robin is enabled. */
363 bool fStrictRoundRobinMode;
364 /** Whether the extended LUN CCB format is enabled for 32 possible logical units. */
365 bool fExtendedLunCCBFormat;
366
367 /** Queue to send tasks to R3. - HC ptr */
368 R3PTRTYPE(PPDMQUEUE) pNotifierQueueR3;
369 /** Queue to send tasks to R3. - HC ptr */
370 R0PTRTYPE(PPDMQUEUE) pNotifierQueueR0;
371 /** Queue to send tasks to R3. - RC ptr */
372 RCPTRTYPE(PPDMQUEUE) pNotifierQueueRC;
373
374 uint32_t Alignment2;
375
376 /** Critical section protecting access to the interrupt status register. */
377 PDMCRITSECT CritSectIntr;
378
379 /** Cache for task states. */
380 R3PTRTYPE(RTMEMCACHE) hTaskCache;
381
382 /** Device state for BIOS access. */
383 VBOXSCSI VBoxSCSI;
384
385 /** BusLogic device states. */
386 BUSLOGICDEVICE aDeviceStates[BUSLOGIC_MAX_DEVICES];
387
388 /** The base interface.
389 * @todo use PDMDEVINS::IBase */
390 PDMIBASE IBase;
391 /** Status Port - Leds interface. */
392 PDMILEDPORTS ILeds;
393 /** Partner of ILeds. */
394 R3PTRTYPE(PPDMILEDCONNECTORS) pLedsConnector;
395
396#if HC_ARCH_BITS == 64
397 uint32_t Alignment3;
398#endif
399
400 /** Indicates that PDMDevHlpAsyncNotificationCompleted should be called when
401 * a port is entering the idle state. */
402 bool volatile fSignalIdle;
403 /** Flag whether we have tasks which need to be processed again. */
404 bool volatile fRedo;
405 /** List of tasks which can be redone. */
406 R3PTRTYPE(volatile PBUSLOGICTASKSTATE) pTasksRedoHead;
407
408#ifdef LOG_ENABLED
409# if HC_ARCH_BITS == 64
410 uint32_t Alignment4;
411# endif
412
413 volatile uint32_t cInMailboxesReady;
414#endif
415
416} BUSLOGIC, *PBUSLOGIC;
417
418/** Register offsets in the I/O port space. */
419#define BUSLOGIC_REGISTER_CONTROL 0 /* Writeonly */
420/** Fields for the control register. */
421# define BUSLOGIC_REGISTER_CONTROL_SCSI_BUSRESET RT_BIT(4)
422# define BUSLOGIC_REGISTER_CONTROL_INTERRUPT_RESET RT_BIT(5)
423# define BUSLOGIC_REGISTER_CONTROL_SOFT_RESET RT_BIT(6)
424# define BUSLOGIC_REGISTER_CONTROL_HARD_RESET RT_BIT(7)
425
426#define BUSLOGIC_REGISTER_STATUS 0 /* Readonly */
427/** Fields for the status register. */
428# define BUSLOGIC_REGISTER_STATUS_COMMAND_INVALID RT_BIT(0)
429# define BUSLOGIC_REGISTER_STATUS_DATA_IN_REGISTER_READY RT_BIT(2)
430# define BUSLOGIC_REGISTER_STATUS_COMMAND_PARAMETER_REGISTER_BUSY RT_BIT(3)
431# define BUSLOGIC_REGISTER_STATUS_HOST_ADAPTER_READY RT_BIT(4)
432# define BUSLOGIC_REGISTER_STATUS_INITIALIZATION_REQUIRED RT_BIT(5)
433# define BUSLOGIC_REGISTER_STATUS_DIAGNOSTIC_FAILURE RT_BIT(6)
434# define BUSLOGIC_REGISTER_STATUS_DIAGNOSTIC_ACTIVE RT_BIT(7)
435
436#define BUSLOGIC_REGISTER_COMMAND 1 /* Writeonly */
437#define BUSLOGIC_REGISTER_DATAIN 1 /* Readonly */
438#define BUSLOGIC_REGISTER_INTERRUPT 2 /* Readonly */
439/** Fields for the interrupt register. */
440# define BUSLOGIC_REGISTER_INTERRUPT_INCOMING_MAILBOX_LOADED RT_BIT(0)
441# define BUSLOGIC_REGISTER_INTERRUPT_OUTCOMING_MAILBOX_AVAILABLE RT_BIT(1)
442# define BUSLOGIC_REGISTER_INTERRUPT_COMMAND_COMPLETE RT_BIT(2)
443# define BUSLOGIC_REGISTER_INTERRUPT_EXTERNAL_BUS_RESET RT_BIT(3)
444# define BUSLOGIC_REGISTER_INTERRUPT_INTERRUPT_VALID RT_BIT(7)
445
446#define BUSLOGIC_REGISTER_GEOMETRY 3 /* Readonly */
447# define BUSLOGIC_REGISTER_GEOMETRY_EXTENTED_TRANSLATION_ENABLED RT_BIT(7)
448
449/* Structure for the INQUIRE_PCI_HOST_ADAPTER_INFORMATION reply. */
450#pragma pack(1)
451typedef struct ReplyInquirePCIHostAdapterInformation
452{
453 uint8_t IsaIOPort;
454 uint8_t IRQ;
455 unsigned char LowByteTerminated:1;
456 unsigned char HighByteTerminated:1;
457 unsigned char uReserved:2; /* Reserved. */
458 unsigned char JP1:1; /* Whatever that means. */
459 unsigned char JP2:1; /* Whatever that means. */
460 unsigned char JP3:1; /* Whatever that means. */
461 /** Whether the provided info is valid. */
462 unsigned char InformationIsValid: 1;
463 uint8_t uReserved2; /* Reserved. */
464} ReplyInquirePCIHostAdapterInformation, *PReplyInquirePCIHostAdapterInformation;
465AssertCompileSize(ReplyInquirePCIHostAdapterInformation, 4);
466#pragma pack()
467
468/* Structure for the INQUIRE_CONFIGURATION reply. */
469#pragma pack(1)
470typedef struct ReplyInquireConfiguration
471{
472 unsigned char uReserved1: 5;
473 bool fDmaChannel5: 1;
474 bool fDmaChannel6: 1;
475 bool fDmaChannel7: 1;
476 bool fIrqChannel9: 1;
477 bool fIrqChannel10: 1;
478 bool fIrqChannel11: 1;
479 bool fIrqChannel12: 1;
480 unsigned char uReserved2: 1;
481 bool fIrqChannel14: 1;
482 bool fIrqChannel15: 1;
483 unsigned char uReserved3: 1;
484 unsigned char uHostAdapterId: 4;
485 unsigned char uReserved4: 4;
486} ReplyInquireConfiguration, *PReplyInquireConfiguration;
487AssertCompileSize(ReplyInquireConfiguration, 3);
488#pragma pack()
489
490/* Structure for the INQUIRE_SETUP_INFORMATION reply. */
491#pragma pack(1)
492typedef struct ReplyInquireSetupInformationSynchronousValue
493{
494 unsigned char uOffset: 4;
495 unsigned char uTransferPeriod: 3;
496 bool fSynchronous: 1;
497}ReplyInquireSetupInformationSynchronousValue, *PReplyInquireSetupInformationSynchronousValue;
498AssertCompileSize(ReplyInquireSetupInformationSynchronousValue, 1);
499#pragma pack()
500
501#pragma pack(1)
502typedef struct ReplyInquireSetupInformation
503{
504 bool fSynchronousInitiationEnabled: 1;
505 bool fParityCheckingEnabled: 1;
506 unsigned char uReserved1: 6;
507 uint8_t uBusTransferRate;
508 uint8_t uPreemptTimeOnBus;
509 uint8_t uTimeOffBus;
510 uint8_t cMailbox;
511 uint8_t MailboxAddress[3];
512 ReplyInquireSetupInformationSynchronousValue SynchronousValuesId0To7[8];
513 uint8_t uDisconnectPermittedId0To7;
514 uint8_t uSignature;
515 uint8_t uCharacterD;
516 uint8_t uHostBusType;
517 uint8_t uWideTransferPermittedId0To7;
518 uint8_t uWideTransfersActiveId0To7;
519 ReplyInquireSetupInformationSynchronousValue SynchronousValuesId8To15[8];
520 uint8_t uDisconnectPermittedId8To15;
521 uint8_t uReserved2;
522 uint8_t uWideTransferPermittedId8To15;
523 uint8_t uWideTransfersActiveId8To15;
524} ReplyInquireSetupInformation, *PReplyInquireSetupInformation;
525AssertCompileSize(ReplyInquireSetupInformation, 34);
526#pragma pack()
527
528/* Structure for the INQUIRE_EXTENDED_SETUP_INFORMATION. */
529#pragma pack(1)
530typedef struct ReplyInquireExtendedSetupInformation
531{
532 uint8_t uBusType;
533 uint8_t uBiosAddress;
534 uint16_t u16ScatterGatherLimit;
535 uint8_t cMailbox;
536 uint32_t uMailboxAddressBase;
537 unsigned char uReserved1: 2;
538 bool fFastEISA: 1;
539 unsigned char uReserved2: 3;
540 bool fLevelSensitiveInterrupt: 1;
541 unsigned char uReserved3: 1;
542 unsigned char aFirmwareRevision[3];
543 bool fHostWideSCSI: 1;
544 bool fHostDifferentialSCSI: 1;
545 bool fHostSupportsSCAM: 1;
546 bool fHostUltraSCSI: 1;
547 bool fHostSmartTermination: 1;
548 unsigned char uReserved4: 3;
549} ReplyInquireExtendedSetupInformation, *PReplyInquireExtendedSetupInformation;
550AssertCompileSize(ReplyInquireExtendedSetupInformation, 14);
551#pragma pack()
552
553/* Structure for the INITIALIZE EXTENDED MAILBOX request. */
554#pragma pack(1)
555typedef struct RequestInitializeExtendedMailbox
556{
557 /** Number of mailboxes in guest memory. */
558 uint8_t cMailbox;
559 /** Physical address of the first mailbox. */
560 uint32_t uMailboxBaseAddress;
561} RequestInitializeExtendedMailbox, *PRequestInitializeExtendedMailbox;
562AssertCompileSize(RequestInitializeExtendedMailbox, 5);
563#pragma pack()
564
565/*
566 * Structure of a mailbox in guest memory.
567 * The incoming and outgoing mailbox have the same size
568 * but the incoming one has some more fields defined which
569 * are marked as reserved in the outgoing one.
570 * The last field is also different from the type.
571 * For outgoing mailboxes it is the action and
572 * for incoming ones the completion status code for the task.
573 * We use one structure for both types.
574 */
575#pragma pack(1)
576typedef struct Mailbox
577{
578 /** Physical address of the CCB structure in the guest memory. */
579 uint32_t u32PhysAddrCCB;
580 /** Type specific data. */
581 union
582 {
583 /** For outgoing mailboxes. */
584 struct
585 {
586 /** Reserved */
587 uint8_t uReserved[3];
588 /** Action code. */
589 uint8_t uActionCode;
590 } out;
591 /** For incoming mailboxes. */
592 struct
593 {
594 /** The host adapter status after finishing the request. */
595 uint8_t uHostAdapterStatus;
596 /** The status of the device which executed the request after executing it. */
597 uint8_t uTargetDeviceStatus;
598 /** Reserved. */
599 uint8_t uReserved;
600 /** The completion status code of the request. */
601 uint8_t uCompletionCode;
602 } in;
603 } u;
604} Mailbox, *PMailbox;
605AssertCompileSize(Mailbox, 8);
606#pragma pack()
607
608/*
609 * Action codes for outgoing mailboxes.
610 */
611enum BUSLOGIC_MAILBOX_OUTGOING_ACTION
612{
613 BUSLOGIC_MAILBOX_OUTGOING_ACTION_FREE = 0x00,
614 BUSLOGIC_MAILBOX_OUTGOING_ACTION_START_COMMAND = 0x01,
615 BUSLOGIC_MAILBOX_OUTGOING_ACTION_ABORT_COMMAND = 0x02
616};
617
618/*
619 * Completion codes for incoming mailboxes.
620 */
621enum BUSLOGIC_MAILBOX_INCOMING_COMPLETION
622{
623 BUSLOGIC_MAILBOX_INCOMING_COMPLETION_FREE = 0x00,
624 BUSLOGIC_MAILBOX_INCOMING_COMPLETION_WITHOUT_ERROR = 0x01,
625 BUSLOGIC_MAILBOX_INCOMING_COMPLETION_ABORTED = 0x02,
626 BUSLOGIC_MAILBOX_INCOMING_COMPLETION_ABORTED_NOT_FOUND = 0x03,
627 BUSLOGIC_MAILBOX_INCOMING_COMPLETION_WITH_ERROR = 0x04,
628 BUSLOGIC_MAILBOX_INCOMING_COMPLETION_INVALID_CCB = 0x05
629};
630
631/*
632 * Host adapter status for incoming mailboxes.
633 */
634enum BUSLOGIC_MAILBOX_INCOMING_ADAPTER_STATUS
635{
636 BUSLOGIC_MAILBOX_INCOMING_ADAPTER_STATUS_CMD_COMPLETED = 0x00,
637 BUSLOGIC_MAILBOX_INCOMING_ADAPTER_STATUS_LINKED_CMD_COMPLETED = 0x0a,
638 BUSLOGIC_MAILBOX_INCOMING_ADAPTER_STATUS_LINKED_CMD_COMPLETED_WITH_FLAG = 0x0b,
639 BUSLOGIC_MAILBOX_INCOMING_ADAPTER_STATUS_DATA_UNDERUN = 0x0c,
640 BUSLOGIC_MAILBOX_INCOMING_ADAPTER_STATUS_SCSI_SELECTION_TIMEOUT = 0x11,
641 BUSLOGIC_MAILBOX_INCOMING_ADAPTER_STATUS_DATA_OVERRUN = 0x12,
642 BUSLOGIC_MAILBOX_INCOMING_ADAPTER_STATUS_UNEXPECTED_BUS_FREE = 0x13,
643 BUSLOGIC_MAILBOX_INCOMING_ADAPTER_STATUS_INVALID_BUS_PHASE_REQUESTED = 0x14,
644 BUSLOGIC_MAILBOX_INCOMING_ADAPTER_STATUS_INVALID_OUTGOING_MAILBOX_ACTION_CODE = 0x15,
645 BUSLOGIC_MAILBOX_INCOMING_ADAPTER_STATUS_INVALID_COMMAND_OPERATION_CODE = 0x16,
646 BUSLOGIC_MAILBOX_INCOMING_ADAPTER_STATUS_LINKED_CCB_HAS_INVALID_LUN = 0x17,
647 BUSLOGIC_MAILBOX_INCOMING_ADAPTER_STATUS_INVALID_COMMAND_PARAMETER = 0x1a,
648 BUSLOGIC_MAILBOX_INCOMING_ADAPTER_STATUS_AUTO_REQUEST_SENSE_FAILED = 0x1b,
649 BUSLOGIC_MAILBOX_INCOMING_ADAPTER_STATUS_TAGGED_QUEUING_MESSAGE_REJECTED = 0x1c,
650 BUSLOGIC_MAILBOX_INCOMING_ADAPTER_STATUS_UNSUPPORTED_MESSAGE_RECEIVED = 0x1d,
651 BUSLOGIC_MAILBOX_INCOMING_ADAPTER_STATUS_HOST_ADAPTER_HARDWARE_FAILED = 0x20,
652 BUSLOGIC_MAILBOX_INCOMING_ADAPTER_STATUS_TARGET_FAILED_RESPONSE_TO_ATN = 0x21,
653 BUSLOGIC_MAILBOX_INCOMING_ADAPTER_STATUS_HOST_ADAPTER_ASSERTED_RST = 0x22,
654 BUSLOGIC_MAILBOX_INCOMING_ADAPTER_STATUS_OTHER_DEVICE_ASSERTED_RST = 0x23,
655 BUSLOGIC_MAILBOX_INCOMING_ADAPTER_STATUS_TARGET_DEVICE_RECONNECTED_IMPROPERLY = 0x24,
656 BUSLOGIC_MAILBOX_INCOMING_ADAPTER_STATUS_HOST_ADAPTER_ASSERTED_BUS_DEVICE_RESET = 0x25,
657 BUSLOGIC_MAILBOX_INCOMING_ADAPTER_STATUS_ABORT_QUEUE_GENERATED = 0x26,
658 BUSLOGIC_MAILBOX_INCOMING_ADAPTER_STATUS_HOST_ADAPTER_SOFTWARE_ERROR = 0x27,
659 BUSLOGIC_MAILBOX_INCOMING_ADAPTER_STATUS_HOST_ADAPTER_HARDWARE_TIMEOUT_ERROR = 0x30,
660 BUSLOGIC_MAILBOX_INCOMING_ADAPTER_STATUS_SCSI_PARITY_ERROR_DETECTED = 0x34
661};
662
663/*
664 * Device status codes for incoming mailboxes.
665 */
666enum BUSLOGIC_MAILBOX_INCOMING_DEVICE_STATUS
667{
668 BUSLOGIC_MAILBOX_INCOMING_DEVICE_STATUS_OPERATION_GOOD = 0x00,
669 BUSLOGIC_MAILBOX_INCOMING_DEVICE_STATUS_CHECK_CONDITION = 0x02,
670 BUSLOGIC_MAILBOX_INCOMING_DEVICE_STATUS_DEVICE_BUSY = 0x08
671};
672
673/*
674 * Opcode types for CCB.
675 */
676enum BUSLOGIC_CCB_OPCODE
677{
678 BUSLOGIC_CCB_OPCODE_INITIATOR_CCB = 0x00,
679 BUSLOGIC_CCB_OPCODE_TARGET_CCB = 0x01,
680 BUSLOGIC_CCB_OPCODE_INITIATOR_CCB_SCATTER_GATHER = 0x02,
681 BUSLOGIC_CCB_OPCODE_INITIATOR_CCB_RESIDUAL_DATA_LENGTH = 0x03,
682 BUSLOGIC_CCB_OPCODE_INITIATOR_CCB_RESIDUAL_SCATTER_GATHER = 0x04,
683 BUSLOGIC_CCB_OPCODE_BUS_DEVICE_RESET = 0x81
684};
685
686/*
687 * Data transfer direction.
688 */
689enum BUSLOGIC_CCB_DIRECTION
690{
691 BUSLOGIC_CCB_DIRECTION_UNKNOWN = 0x00,
692 BUSLOGIC_CCB_DIRECTION_IN = 0x01,
693 BUSLOGIC_CCB_DIRECTION_OUT = 0x02,
694 BUSLOGIC_CCB_DIRECTION_NO_DATA = 0x03
695};
696
697/*
698 * The command control block for a SCSI request.
699 */
700#pragma pack(1)
701typedef struct CommandControlBlock
702{
703 /** Opcode. */
704 uint8_t uOpcode;
705 /** Reserved */
706 unsigned char uReserved1: 3;
707 /** Data direction for the request. */
708 unsigned char uDataDirection: 2;
709 /** Whether the request is tag queued. */
710 bool fTagQueued: 1;
711 /** Queue tag mode. */
712 unsigned char uQueueTag: 2;
713 /** Length of the SCSI CDB. */
714 uint8_t cbCDB;
715 /** Sense data length. */
716 uint8_t cbSenseData;
717 /** Data length. */
718 uint32_t cbData;
719 /** Data pointer.
720 * This points to the data region or a scatter gather list based on the opcode.
721 */
722 uint32_t u32PhysAddrData;
723 /** Reserved. */
724 uint8_t uReserved2[2];
725 /** Host adapter status. */
726 uint8_t uHostAdapterStatus;
727 /** Device adapter status. */
728 uint8_t uDeviceStatus;
729 /** The device the request is send to. */
730 uint8_t uTargetId;
731 /**The LUN in the device. */
732 unsigned char uLogicalUnit: 5;
733 /** Legacy tag. */
734 bool fLegacyTagEnable: 1;
735 /** Legacy queue tag. */
736 unsigned char uLegacyQueueTag: 2;
737 /** The SCSI CDB. */
738 uint8_t aCDB[12]; /* A CDB can be 12 bytes long. */
739 /** Reserved. */
740 uint8_t uReserved3[6];
741 /** Sense data pointer. */
742 uint32_t u32PhysAddrSenseData;
743} CommandControlBlock, *PCommandControlBlock;
744AssertCompileSize(CommandControlBlock, 40);
745#pragma pack()
746
747#pragma pack(1)
748typedef struct ScatterGatherEntry
749{
750 uint32_t cbSegment;
751 uint32_t u32PhysAddrSegmentBase;
752} ScatterGatherEntry, *PScatterGatherEntry;
753AssertCompileSize(ScatterGatherEntry, 8);
754#pragma pack()
755
756/*
757 * Task state for a CCB request.
758 */
759typedef struct BUSLOGICTASKSTATE
760{
761 /** Next in the redo list. */
762 PBUSLOGICTASKSTATE pRedoNext;
763 /** Device this task is assigned to. */
764 R3PTRTYPE(PBUSLOGICDEVICE) pTargetDeviceR3;
765 /** The command control block from the guest. */
766 CommandControlBlock CommandControlBlockGuest;
767 /** Mailbox read from guest memory. */
768 Mailbox MailboxGuest;
769 /** The SCSI request we pass to the underlying SCSI engine. */
770 PDMSCSIREQUEST PDMScsiRequest;
771 /** Data buffer segment */
772 RTSGSEG DataSeg;
773 /** Pointer to the R3 sense buffer. */
774 uint8_t *pbSenseBuffer;
775 /** Flag whether this is a request from the BIOS. */
776 bool fBIOS;
777} BUSLOGICTASKSTATE;
778
779#ifndef VBOX_DEVICE_STRUCT_TESTCASE
780
781#define PDMIBASE_2_PBUSLOGICDEVICE(pInterface) ( (PBUSLOGICDEVICE)((uintptr_t)(pInterface) - RT_OFFSETOF(BUSLOGICDEVICE, IBase)) )
782#define PDMISCSIPORT_2_PBUSLOGICDEVICE(pInterface) ( (PBUSLOGICDEVICE)((uintptr_t)(pInterface) - RT_OFFSETOF(BUSLOGICDEVICE, ISCSIPort)) )
783#define PDMILEDPORTS_2_PBUSLOGICDEVICE(pInterface) ( (PBUSLOGICDEVICE)((uintptr_t)(pInterface) - RT_OFFSETOF(BUSLOGICDEVICE, ILed)) )
784#define PDMIBASE_2_PBUSLOGIC(pInterface) ( (PBUSLOGIC)((uintptr_t)(pInterface) - RT_OFFSETOF(BUSLOGIC, IBase)) )
785#define PDMILEDPORTS_2_PBUSLOGIC(pInterface) ( (PBUSLOGIC)((uintptr_t)(pInterface) - RT_OFFSETOF(BUSLOGIC, ILeds)) )
786
787/**
788 * Deasserts the interrupt line of the BusLogic adapter.
789 *
790 * @returns nothing
791 * @param pBuslogic Pointer to the BusLogic device instance.
792 */
793static void buslogicClearInterrupt(PBUSLOGIC pBusLogic)
794{
795 LogFlowFunc(("pBusLogic=%#p\n", pBusLogic));
796 pBusLogic->regInterrupt = 0;
797 PDMDevHlpPCISetIrq(pBusLogic->CTX_SUFF(pDevIns), 0, 0);
798}
799
800/**
801 * Assert IRQ line of the BusLogic adapter.
802 *
803 * @returns nothing.
804 * @param pBusLogic Pointer to the BusLogic device instance.
805 * @param fSuppressIrq Flag to suppress IRQ generation regardless of fIRQEnabled
806 */
807static void buslogicSetInterrupt(PBUSLOGIC pBusLogic, bool fSuppressIrq)
808{
809 LogFlowFunc(("pBusLogic=%#p\n", pBusLogic));
810 pBusLogic->regInterrupt |= BUSLOGIC_REGISTER_INTERRUPT_INTERRUPT_VALID;
811 if (pBusLogic->fIRQEnabled && !fSuppressIrq)
812 PDMDevHlpPCISetIrq(pBusLogic->CTX_SUFF(pDevIns), 0, 1);
813}
814
815#if defined(IN_RING3)
816
817/**
818 * Advances the mailbox pointer to the next slot.
819 */
820DECLINLINE(void) buslogicOutgoingMailboxAdvance(PBUSLOGIC pBusLogic)
821{
822 pBusLogic->uMailboxOutgoingPositionCurrent = (pBusLogic->uMailboxOutgoingPositionCurrent + 1) % pBusLogic->cMailbox;
823}
824
825/**
826 * Returns the physical address of the next outgoing mailbox to process.
827 */
828DECLINLINE(RTGCPHYS) buslogicOutgoingMailboxGetGCPhys(PBUSLOGIC pBusLogic)
829{
830 return pBusLogic->GCPhysAddrMailboxOutgoingBase + (pBusLogic->uMailboxOutgoingPositionCurrent * sizeof(Mailbox));
831}
832
833/**
834 * Initialize local RAM of host adapter with default values.
835 *
836 * @returns nothing.
837 * @param pBusLogic.
838 */
839static void buslogicInitializeLocalRam(PBUSLOGIC pBusLogic)
840{
841 /*
842 * These values are mostly from what I think is right
843 * looking at the dmesg output from a Linux guest inside
844 * a VMware server VM.
845 *
846 * So they don't have to be right :)
847 */
848 memset(pBusLogic->LocalRam.u8View, 0, sizeof(HostAdapterLocalRam));
849 pBusLogic->LocalRam.structured.autoSCSIData.fLevelSensitiveInterrupt = true;
850 pBusLogic->LocalRam.structured.autoSCSIData.fParityCheckingEnabled = true;
851 pBusLogic->LocalRam.structured.autoSCSIData.fExtendedTranslation = true; /* Same as in geometry register. */
852 pBusLogic->LocalRam.structured.autoSCSIData.u16DeviceEnabledMask = ~0; /* All enabled. Maybe mask out non present devices? */
853 pBusLogic->LocalRam.structured.autoSCSIData.u16WidePermittedMask = ~0;
854 pBusLogic->LocalRam.structured.autoSCSIData.u16FastPermittedMask = ~0;
855 pBusLogic->LocalRam.structured.autoSCSIData.u16SynchronousPermittedMask = ~0;
856 pBusLogic->LocalRam.structured.autoSCSIData.u16DisconnectPermittedMask = ~0;
857 pBusLogic->LocalRam.structured.autoSCSIData.fStrictRoundRobinMode = pBusLogic->fStrictRoundRobinMode;
858 pBusLogic->LocalRam.structured.autoSCSIData.u16UltraPermittedMask = ~0;
859 /* @todo calculate checksum? */
860}
861
862/**
863 * Do a hardware reset of the buslogic adapter.
864 *
865 * @returns VBox status code.
866 * @param pBusLogic Pointer to the BusLogic device instance.
867 */
868static int buslogicHwReset(PBUSLOGIC pBusLogic)
869{
870 LogFlowFunc(("pBusLogic=%#p\n", pBusLogic));
871
872 /* Reset registers to default value. */
873 pBusLogic->regStatus = BUSLOGIC_REGISTER_STATUS_HOST_ADAPTER_READY | BUSLOGIC_REGISTER_STATUS_INITIALIZATION_REQUIRED;
874 pBusLogic->regInterrupt = 0;
875 pBusLogic->regGeometry = BUSLOGIC_REGISTER_GEOMETRY_EXTENTED_TRANSLATION_ENABLED;
876 pBusLogic->uOperationCode = 0xff; /* No command executing. */
877 pBusLogic->iParameter = 0;
878 pBusLogic->cbCommandParametersLeft = 0;
879 pBusLogic->fIRQEnabled = true;
880 pBusLogic->fISAEnabled = true;
881 pBusLogic->uMailboxOutgoingPositionCurrent = 0;
882 pBusLogic->uMailboxIncomingPositionCurrent = 0;
883
884 buslogicInitializeLocalRam(pBusLogic);
885 vboxscsiInitialize(&pBusLogic->VBoxSCSI);
886
887 return VINF_SUCCESS;
888}
889#endif
890
891/**
892 * Resets the command state machine for the next command and notifies the guest.
893 *
894 * @returns nothing.
895 * @param pBusLogic Pointer to the BusLogic device instance
896 * @param fSuppressIrq Flag to suppress IRQ generation regardless of current state
897 */
898static void buslogicCommandComplete(PBUSLOGIC pBusLogic, bool fSuppressIrq)
899{
900 LogFlowFunc(("pBusLogic=%#p\n", pBusLogic));
901
902 pBusLogic->fUseLocalRam = false;
903 pBusLogic->regStatus |= BUSLOGIC_REGISTER_STATUS_HOST_ADAPTER_READY;
904 pBusLogic->iReply = 0;
905
906 /* Modify I/O address does not generate an interrupt. */
907 if (pBusLogic->uOperationCode != BUSLOGICCOMMAND_EXECUTE_MAILBOX_COMMAND)
908 {
909 /* Notify that the command is complete. */
910 pBusLogic->regStatus &= ~BUSLOGIC_REGISTER_STATUS_DATA_IN_REGISTER_READY;
911 pBusLogic->regInterrupt |= BUSLOGIC_REGISTER_INTERRUPT_COMMAND_COMPLETE;
912
913 buslogicSetInterrupt(pBusLogic, fSuppressIrq);
914 }
915
916 pBusLogic->uOperationCode = 0xff;
917 pBusLogic->iParameter = 0;
918}
919
920#if defined(IN_RING3)
921/**
922 * Initiates a hard reset which was issued from the guest.
923 *
924 * @returns nothing
925 * @param pBusLogic Pointer to the BusLogic device instance.
926 */
927static void buslogicIntiateHardReset(PBUSLOGIC pBusLogic)
928{
929 LogFlowFunc(("pBusLogic=%#p\n", pBusLogic));
930
931 buslogicHwReset(pBusLogic);
932
933 /* We set the diagnostic active in the status register. */
934 pBusLogic->regStatus |= BUSLOGIC_REGISTER_STATUS_DIAGNOSTIC_ACTIVE;
935}
936
937/**
938 * Send a mailbox with set status codes to the guest.
939 *
940 * @returns nothing.
941 * @param pBusLogic Pointer to the BusLogic device instance.
942 * @param pTaskState Pointer to the task state with the mailbox to send.
943 * @param uHostAdapterStatus The host adapter status code to set.
944 * @param uDeviceStatus The target device status to set.
945 * @param uMailboxCompletionCode Completion status code to set in the mailbox.
946 */
947static void buslogicSendIncomingMailbox(PBUSLOGIC pBusLogic, PBUSLOGICTASKSTATE pTaskState,
948 uint8_t uHostAdapterStatus, uint8_t uDeviceStatus,
949 uint8_t uMailboxCompletionCode)
950{
951 pTaskState->MailboxGuest.u.in.uHostAdapterStatus = uHostAdapterStatus;
952 pTaskState->MailboxGuest.u.in.uTargetDeviceStatus = uDeviceStatus;
953 pTaskState->MailboxGuest.u.in.uCompletionCode = uMailboxCompletionCode;
954
955 int rc = PDMCritSectEnter(&pBusLogic->CritSectIntr, VINF_SUCCESS);
956 AssertRC(rc);
957 RTGCPHYS GCPhysAddrMailboxIncoming = pBusLogic->GCPhysAddrMailboxIncomingBase + (pBusLogic->uMailboxIncomingPositionCurrent * sizeof(Mailbox));
958 RTGCPHYS GCPhysAddrCCB = (RTGCPHYS)pTaskState->MailboxGuest.u32PhysAddrCCB;
959
960 LogFlowFunc(("Completing CCB %RGp\n", GCPhysAddrCCB));
961
962 /* Update CCB. */
963 pTaskState->CommandControlBlockGuest.uHostAdapterStatus = uHostAdapterStatus;
964 pTaskState->CommandControlBlockGuest.uDeviceStatus = uDeviceStatus;
965 /* @todo: this is wrong - writing too much! */
966 PDMDevHlpPhysWrite(pBusLogic->CTX_SUFF(pDevIns), GCPhysAddrCCB, &pTaskState->CommandControlBlockGuest, sizeof(CommandControlBlock));
967
968#ifdef RT_STRICT
969 Mailbox Tmp;
970 PDMDevHlpPhysRead(pBusLogic->CTX_SUFF(pDevIns), GCPhysAddrMailboxIncoming, &Tmp, sizeof(Mailbox));
971 Assert(Tmp.u.in.uCompletionCode == BUSLOGIC_MAILBOX_INCOMING_COMPLETION_FREE);
972#endif
973
974 /* Update mailbox. */
975 PDMDevHlpPhysWrite(pBusLogic->CTX_SUFF(pDevIns), GCPhysAddrMailboxIncoming, &pTaskState->MailboxGuest, sizeof(Mailbox));
976
977 /* Advance to next mailbox position. */
978 pBusLogic->uMailboxIncomingPositionCurrent++;
979 if (pBusLogic->uMailboxIncomingPositionCurrent >= pBusLogic->cMailbox)
980 pBusLogic->uMailboxIncomingPositionCurrent = 0;
981
982#ifdef LOG_ENABLED
983 ASMAtomicIncU32(&pBusLogic->cInMailboxesReady);
984#endif
985
986 pBusLogic->regInterrupt |= BUSLOGIC_REGISTER_INTERRUPT_INCOMING_MAILBOX_LOADED;
987 buslogicSetInterrupt(pBusLogic, false);
988
989 PDMCritSectLeave(&pBusLogic->CritSectIntr);
990}
991
992#if defined(DEBUG)
993/**
994 * Dumps the content of a mailbox for debugging purposes.
995 *
996 * @return nothing
997 * @param pMailbox The mailbox to dump.
998 * @param fOutgoing true if dumping the outgoing state.
999 * false if dumping the incoming state.
1000 */
1001static void buslogicDumpMailboxInfo(PMailbox pMailbox, bool fOutgoing)
1002{
1003 Log(("%s: Dump for %s mailbox:\n", __FUNCTION__, fOutgoing ? "outgoing" : "incoming"));
1004 Log(("%s: u32PhysAddrCCB=%#x\n", __FUNCTION__, pMailbox->u32PhysAddrCCB));
1005 if (fOutgoing)
1006 {
1007 Log(("%s: uActionCode=%u\n", __FUNCTION__, pMailbox->u.out.uActionCode));
1008 }
1009 else
1010 {
1011 Log(("%s: uHostAdapterStatus=%u\n", __FUNCTION__, pMailbox->u.in.uHostAdapterStatus));
1012 Log(("%s: uTargetDeviceStatus=%u\n", __FUNCTION__, pMailbox->u.in.uTargetDeviceStatus));
1013 Log(("%s: uCompletionCode=%u\n", __FUNCTION__, pMailbox->u.in.uCompletionCode));
1014 }
1015}
1016
1017/**
1018 * Dumps the content of a command control block for debugging purposes.
1019 *
1020 * @returns nothing.
1021 * @param pCCB Pointer to the command control block to dump.
1022 */
1023static void buslogicDumpCCBInfo(PCommandControlBlock pCCB)
1024{
1025 Log(("%s: Dump for Command Control Block:\n", __FUNCTION__));
1026 Log(("%s: uOpCode=%#x\n", __FUNCTION__, pCCB->uOpcode));
1027 Log(("%s: uDataDirection=%u\n", __FUNCTION__, pCCB->uDataDirection));
1028 Log(("%s: fTagQueued=%d\n", __FUNCTION__, pCCB->fTagQueued));
1029 Log(("%s: uQueueTag=%u\n", __FUNCTION__, pCCB->uQueueTag));
1030 Log(("%s: cbCDB=%u\n", __FUNCTION__, pCCB->cbCDB));
1031 Log(("%s: cbSenseData=%u\n", __FUNCTION__, pCCB->cbSenseData));
1032 Log(("%s: cbData=%u\n", __FUNCTION__, pCCB->cbData));
1033 Log(("%s: u32PhysAddrData=%#x\n", __FUNCTION__, pCCB->u32PhysAddrData));
1034 Log(("%s: uHostAdapterStatus=%u\n", __FUNCTION__, pCCB->uHostAdapterStatus));
1035 Log(("%s: uDeviceStatus=%u\n", __FUNCTION__, pCCB->uDeviceStatus));
1036 Log(("%s: uTargetId=%u\n", __FUNCTION__, pCCB->uTargetId));
1037 Log(("%s: uLogicalUnit=%u\n", __FUNCTION__, pCCB->uLogicalUnit));
1038 Log(("%s: fLegacyTagEnable=%u\n", __FUNCTION__, pCCB->fLegacyTagEnable));
1039 Log(("%s: uLegacyQueueTag=%u\n", __FUNCTION__, pCCB->uLegacyQueueTag));
1040 Log(("%s: uCDB[0]=%#x\n", __FUNCTION__, pCCB->aCDB[0]));
1041 for (int i = 1; i < pCCB->cbCDB; i++)
1042 Log(("%s: uCDB[%d]=%u\n", __FUNCTION__, i, pCCB->aCDB[i]));
1043 Log(("%s: u32PhysAddrSenseData=%#x\n", __FUNCTION__, pCCB->u32PhysAddrSenseData));
1044}
1045#endif
1046
1047/**
1048 * Allocate data buffer.
1049 *
1050 * @returns VBox status code.
1051 * @param pTaskState Pointer to the task state.
1052 */
1053static int buslogicDataBufferAlloc(PBUSLOGICTASKSTATE pTaskState)
1054{
1055 PPDMDEVINS pDevIns = pTaskState->CTX_SUFF(pTargetDevice)->CTX_SUFF(pBusLogic)->CTX_SUFF(pDevIns);
1056
1057 if ( (pTaskState->CommandControlBlockGuest.uDataDirection != BUSLOGIC_CCB_DIRECTION_NO_DATA)
1058 && (pTaskState->CommandControlBlockGuest.cbData > 0))
1059 {
1060 /*
1061 * @todo: Check following assumption and what residual means.
1062 *
1063 * The BusLogic adapter can handle two different data buffer formats.
1064 * The first one is that the data pointer entry in the CCB points to
1065 * the buffer directly. In second mode the data pointer points to a
1066 * scatter gather list which describes the buffer.
1067 */
1068 if ( (pTaskState->CommandControlBlockGuest.uOpcode == BUSLOGIC_CCB_OPCODE_INITIATOR_CCB_SCATTER_GATHER)
1069 || (pTaskState->CommandControlBlockGuest.uOpcode == BUSLOGIC_CCB_OPCODE_INITIATOR_CCB_RESIDUAL_SCATTER_GATHER))
1070 {
1071 uint32_t cScatterGatherGCRead;
1072 uint32_t iScatterGatherEntry;
1073 ScatterGatherEntry aScatterGatherReadGC[32]; /* Number of scatter gather list entries read from guest memory. */
1074 uint32_t cScatterGatherGCLeft = pTaskState->CommandControlBlockGuest.cbData / sizeof(ScatterGatherEntry);
1075 RTGCPHYS GCPhysAddrScatterGatherCurrent = (RTGCPHYS)pTaskState->CommandControlBlockGuest.u32PhysAddrData;
1076 size_t cbDataToTransfer = 0;
1077
1078 /* Count number of bytes to transfer. */
1079 do
1080 {
1081 cScatterGatherGCRead = (cScatterGatherGCLeft < RT_ELEMENTS(aScatterGatherReadGC))
1082 ? cScatterGatherGCLeft
1083 : RT_ELEMENTS(aScatterGatherReadGC);
1084 cScatterGatherGCLeft -= cScatterGatherGCRead;
1085
1086 /* Read the SG entries. */
1087 PDMDevHlpPhysRead(pDevIns, GCPhysAddrScatterGatherCurrent, &aScatterGatherReadGC[0],
1088 cScatterGatherGCRead * sizeof(ScatterGatherEntry));
1089
1090 for (iScatterGatherEntry = 0; iScatterGatherEntry < cScatterGatherGCRead; iScatterGatherEntry++)
1091 {
1092 RTGCPHYS GCPhysAddrDataBase;
1093
1094 Log(("%s: iScatterGatherEntry=%u\n", __FUNCTION__, iScatterGatherEntry));
1095
1096 GCPhysAddrDataBase = (RTGCPHYS)aScatterGatherReadGC[iScatterGatherEntry].u32PhysAddrSegmentBase;
1097 cbDataToTransfer += aScatterGatherReadGC[iScatterGatherEntry].cbSegment;
1098
1099 Log(("%s: GCPhysAddrDataBase=%RGp cbDataToTransfer=%u\n",
1100 __FUNCTION__, GCPhysAddrDataBase,
1101 aScatterGatherReadGC[iScatterGatherEntry].cbSegment));
1102 }
1103
1104 /* Set address to the next entries to read. */
1105 GCPhysAddrScatterGatherCurrent += cScatterGatherGCRead * sizeof(ScatterGatherEntry);
1106 } while (cScatterGatherGCLeft > 0);
1107
1108 Log(("%s: cbDataToTransfer=%d\n", __FUNCTION__, cbDataToTransfer));
1109
1110 /* Allocate buffer */
1111 pTaskState->DataSeg.cbSeg = cbDataToTransfer;
1112 pTaskState->DataSeg.pvSeg = RTMemAlloc(pTaskState->DataSeg.cbSeg);
1113 if (!pTaskState->DataSeg.pvSeg)
1114 return VERR_NO_MEMORY;
1115
1116 /* Copy the data if needed */
1117 if (pTaskState->CommandControlBlockGuest.uDataDirection == BUSLOGIC_CCB_DIRECTION_OUT)
1118 {
1119 cScatterGatherGCLeft = pTaskState->CommandControlBlockGuest.cbData / sizeof(ScatterGatherEntry);
1120 GCPhysAddrScatterGatherCurrent = (RTGCPHYS)pTaskState->CommandControlBlockGuest.u32PhysAddrData;
1121 uint8_t *pbData = (uint8_t *)pTaskState->DataSeg.pvSeg;
1122
1123 do
1124 {
1125 cScatterGatherGCRead = (cScatterGatherGCLeft < RT_ELEMENTS(aScatterGatherReadGC))
1126 ? cScatterGatherGCLeft
1127 : RT_ELEMENTS(aScatterGatherReadGC);
1128 cScatterGatherGCLeft -= cScatterGatherGCRead;
1129
1130 /* Read the SG entries. */
1131 PDMDevHlpPhysRead(pDevIns, GCPhysAddrScatterGatherCurrent, &aScatterGatherReadGC[0],
1132 cScatterGatherGCRead * sizeof(ScatterGatherEntry));
1133
1134 for (iScatterGatherEntry = 0; iScatterGatherEntry < cScatterGatherGCRead; iScatterGatherEntry++)
1135 {
1136 RTGCPHYS GCPhysAddrDataBase;
1137
1138 Log(("%s: iScatterGatherEntry=%u\n", __FUNCTION__, iScatterGatherEntry));
1139
1140 GCPhysAddrDataBase = (RTGCPHYS)aScatterGatherReadGC[iScatterGatherEntry].u32PhysAddrSegmentBase;
1141 cbDataToTransfer = aScatterGatherReadGC[iScatterGatherEntry].cbSegment;
1142
1143 Log(("%s: GCPhysAddrDataBase=%RGp cbDataToTransfer=%u\n", __FUNCTION__, GCPhysAddrDataBase, cbDataToTransfer));
1144
1145 PDMDevHlpPhysRead(pDevIns, GCPhysAddrDataBase, pbData, cbDataToTransfer);
1146 pbData += cbDataToTransfer;
1147 }
1148
1149 /* Set address to the next entries to read. */
1150 GCPhysAddrScatterGatherCurrent += cScatterGatherGCRead * sizeof(ScatterGatherEntry);
1151 } while (cScatterGatherGCLeft > 0);
1152 }
1153
1154 }
1155 else if ( pTaskState->CommandControlBlockGuest.uOpcode == BUSLOGIC_CCB_OPCODE_INITIATOR_CCB
1156 || pTaskState->CommandControlBlockGuest.uOpcode == BUSLOGIC_CCB_OPCODE_INITIATOR_CCB_RESIDUAL_DATA_LENGTH)
1157 {
1158 /* The buffer is not scattered. */
1159 RTGCPHYS GCPhysAddrDataBase = (RTGCPHYS)pTaskState->CommandControlBlockGuest.u32PhysAddrData;
1160
1161 AssertMsg(GCPhysAddrDataBase != 0, ("Physical address is 0\n"));
1162
1163 pTaskState->DataSeg.cbSeg = pTaskState->CommandControlBlockGuest.cbData;
1164 pTaskState->DataSeg.pvSeg = RTMemAlloc(pTaskState->DataSeg.cbSeg);
1165 if (!pTaskState->DataSeg.pvSeg)
1166 return VERR_NO_MEMORY;
1167
1168 Log(("Non scattered buffer:\n"));
1169 Log(("u32PhysAddrData=%#x\n", pTaskState->CommandControlBlockGuest.u32PhysAddrData));
1170 Log(("cbData=%u\n", pTaskState->CommandControlBlockGuest.cbData));
1171 Log(("GCPhysAddrDataBase=0x%RGp\n", GCPhysAddrDataBase));
1172
1173 /* Copy the data into the buffer. */
1174 PDMDevHlpPhysRead(pDevIns, GCPhysAddrDataBase, pTaskState->DataSeg.pvSeg, pTaskState->DataSeg.cbSeg);
1175 }
1176 }
1177
1178 return VINF_SUCCESS;
1179}
1180
1181/**
1182 * Free allocated resources used for the scatter gather list.
1183 *
1184 * @returns nothing.
1185 * @param pTaskState Pointer to the task state.
1186 */
1187static void buslogicDataBufferFree(PBUSLOGICTASKSTATE pTaskState)
1188{
1189 PPDMDEVINS pDevIns = pTaskState->CTX_SUFF(pTargetDevice)->CTX_SUFF(pBusLogic)->CTX_SUFF(pDevIns);
1190
1191 if ( (pTaskState->CommandControlBlockGuest.cbData > 0)
1192 && ( (pTaskState->CommandControlBlockGuest.uDataDirection == BUSLOGIC_CCB_DIRECTION_IN)
1193 || (pTaskState->CommandControlBlockGuest.uDataDirection == BUSLOGIC_CCB_DIRECTION_UNKNOWN)))
1194 {
1195 if ( (pTaskState->CommandControlBlockGuest.uOpcode == BUSLOGIC_CCB_OPCODE_INITIATOR_CCB_SCATTER_GATHER)
1196 || (pTaskState->CommandControlBlockGuest.uOpcode == BUSLOGIC_CCB_OPCODE_INITIATOR_CCB_RESIDUAL_SCATTER_GATHER))
1197 {
1198 uint32_t cScatterGatherGCRead;
1199 uint32_t iScatterGatherEntry;
1200 ScatterGatherEntry aScatterGatherReadGC[32]; /* Number of scatter gather list entries read from guest memory. */
1201 uint32_t cScatterGatherGCLeft = pTaskState->CommandControlBlockGuest.cbData / sizeof(ScatterGatherEntry);
1202 RTGCPHYS GCPhysAddrScatterGatherCurrent = (RTGCPHYS)pTaskState->CommandControlBlockGuest.u32PhysAddrData;
1203 uint8_t *pbData = (uint8_t *)pTaskState->DataSeg.pvSeg;
1204
1205 do
1206 {
1207 cScatterGatherGCRead = (cScatterGatherGCLeft < RT_ELEMENTS(aScatterGatherReadGC))
1208 ? cScatterGatherGCLeft
1209 : RT_ELEMENTS(aScatterGatherReadGC);
1210 cScatterGatherGCLeft -= cScatterGatherGCRead;
1211
1212 /* Read the SG entries. */
1213 PDMDevHlpPhysRead(pDevIns, GCPhysAddrScatterGatherCurrent, &aScatterGatherReadGC[0],
1214 cScatterGatherGCRead * sizeof(ScatterGatherEntry));
1215
1216 for (iScatterGatherEntry = 0; iScatterGatherEntry < cScatterGatherGCRead; iScatterGatherEntry++)
1217 {
1218 RTGCPHYS GCPhysAddrDataBase;
1219 size_t cbDataToTransfer;
1220
1221 Log(("%s: iScatterGatherEntry=%u\n", __FUNCTION__, iScatterGatherEntry));
1222
1223 GCPhysAddrDataBase = (RTGCPHYS)aScatterGatherReadGC[iScatterGatherEntry].u32PhysAddrSegmentBase;
1224 cbDataToTransfer = aScatterGatherReadGC[iScatterGatherEntry].cbSegment;
1225
1226 Log(("%s: GCPhysAddrDataBase=%RGp cbDataToTransfer=%u\n", __FUNCTION__, GCPhysAddrDataBase, cbDataToTransfer));
1227
1228 PDMDevHlpPhysWrite(pDevIns, GCPhysAddrDataBase, pbData, cbDataToTransfer);
1229 pbData += cbDataToTransfer;
1230 }
1231
1232 /* Set address to the next entries to read. */
1233 GCPhysAddrScatterGatherCurrent += cScatterGatherGCRead * sizeof(ScatterGatherEntry);
1234 } while (cScatterGatherGCLeft > 0);
1235
1236 }
1237 else if ( pTaskState->CommandControlBlockGuest.uOpcode == BUSLOGIC_CCB_OPCODE_INITIATOR_CCB
1238 || pTaskState->CommandControlBlockGuest.uOpcode == BUSLOGIC_CCB_OPCODE_INITIATOR_CCB_RESIDUAL_DATA_LENGTH)
1239 {
1240 /* The buffer is not scattered. */
1241 RTGCPHYS GCPhysAddrDataBase = (RTGCPHYS)pTaskState->CommandControlBlockGuest.u32PhysAddrData;
1242
1243 AssertMsg(GCPhysAddrDataBase != 0, ("Physical address is 0\n"));
1244
1245 Log(("Non scattered buffer:\n"));
1246 Log(("u32PhysAddrData=%#x\n", pTaskState->CommandControlBlockGuest.u32PhysAddrData));
1247 Log(("cbData=%u\n", pTaskState->CommandControlBlockGuest.cbData));
1248 Log(("GCPhysAddrDataBase=0x%RGp\n", GCPhysAddrDataBase));
1249
1250 /* Copy the data into the guest memory. */
1251 PDMDevHlpPhysWrite(pDevIns, GCPhysAddrDataBase, pTaskState->DataSeg.pvSeg, pTaskState->DataSeg.cbSeg);
1252 }
1253 }
1254
1255 RTMemFree(pTaskState->DataSeg.pvSeg);
1256 pTaskState->DataSeg.pvSeg = NULL;
1257 pTaskState->DataSeg.cbSeg = 0;
1258}
1259
1260/**
1261 * Free the sense buffer.
1262 *
1263 * @returns nothing.
1264 * @param pTaskState Pointer to the task state.
1265 * @param fCopy If sense data should be copied to guest memory.
1266 */
1267static void buslogicSenseBufferFree(PBUSLOGICTASKSTATE pTaskState, bool fCopy)
1268{
1269 PPDMDEVINS pDevIns = pTaskState->CTX_SUFF(pTargetDevice)->CTX_SUFF(pBusLogic)->CTX_SUFF(pDevIns);
1270 RTGCPHYS GCPhysAddrSenseBuffer = (RTGCPHYS)pTaskState->CommandControlBlockGuest.u32PhysAddrSenseData;
1271 uint32_t cbSenseBuffer = pTaskState->CommandControlBlockGuest.cbSenseData;
1272
1273 /* Copy into guest memory. */
1274 if (fCopy)
1275 PDMDevHlpPhysWrite(pDevIns, GCPhysAddrSenseBuffer, pTaskState->pbSenseBuffer, cbSenseBuffer);
1276
1277 RTMemFree(pTaskState->pbSenseBuffer);
1278 pTaskState->pbSenseBuffer = NULL;
1279}
1280
1281/**
1282 * Alloc the sense buffer.
1283 *
1284 * @returns VBox status code.
1285 * @param pTaskState Pointer to the task state.
1286 * @note Current assumption is that the sense buffer is not scattered and does not cross a page boundary.
1287 */
1288static int buslogicSenseBufferAlloc(PBUSLOGICTASKSTATE pTaskState)
1289{
1290 PPDMDEVINS pDevIns = pTaskState->CTX_SUFF(pTargetDevice)->CTX_SUFF(pBusLogic)->CTX_SUFF(pDevIns);
1291 uint32_t cbSenseBuffer = pTaskState->CommandControlBlockGuest.cbSenseData;
1292
1293 pTaskState->pbSenseBuffer = (uint8_t *)RTMemAllocZ(cbSenseBuffer);
1294 if (!pTaskState->pbSenseBuffer)
1295 return VERR_NO_MEMORY;
1296
1297 return VINF_SUCCESS;
1298}
1299#endif /* IN_RING3 */
1300
1301/**
1302 * Parses the command buffer and executes it.
1303 *
1304 * @returns VBox status code.
1305 * @param pBusLogic Pointer to the BusLogic device instance.
1306 */
1307static int buslogicProcessCommand(PBUSLOGIC pBusLogic)
1308{
1309 int rc = VINF_SUCCESS;
1310 bool fSuppressIrq = false;
1311
1312 LogFlowFunc(("pBusLogic=%#p\n", pBusLogic));
1313 AssertMsg(pBusLogic->uOperationCode != 0xff, ("There is no command to execute\n"));
1314
1315 switch (pBusLogic->uOperationCode)
1316 {
1317 case BUSLOGICCOMMAND_INQUIRE_PCI_HOST_ADAPTER_INFORMATION:
1318 {
1319 PReplyInquirePCIHostAdapterInformation pReply = (PReplyInquirePCIHostAdapterInformation)pBusLogic->aReplyBuffer;
1320 memset(pReply, 0, sizeof(ReplyInquirePCIHostAdapterInformation));
1321
1322 /* It seems VMware does not provide valid information here too, lets do the same :) */
1323 pReply->InformationIsValid = 0;
1324 pReply->IsaIOPort = 0xff; /* Make it invalid. */
1325 pBusLogic->cbReplyParametersLeft = sizeof(ReplyInquirePCIHostAdapterInformation);
1326 break;
1327 }
1328 case BUSLOGICCOMMAND_MODIFY_IO_ADDRESS:
1329 {
1330 pBusLogic->cbReplyParametersLeft = 0;
1331 if (pBusLogic->aCommandBuffer[0] == 0x06)
1332 {
1333 Log(("Disabling ISA I/O ports.\n"));
1334 pBusLogic->fISAEnabled = false;
1335 }
1336 fSuppressIrq = true;
1337 break;
1338 }
1339 case BUSLOGICCOMMAND_INQUIRE_BOARD_ID:
1340 {
1341 /* The special option byte is important: If it is '0' or 'B', Windows NT drivers
1342 * for Adaptec AHA-154x may claim the adapter. The BusLogic drivers will claim
1343 * the adapter only when the byte is *not* '0' or 'B'.
1344 */
1345 pBusLogic->aReplyBuffer[0] = 'A'; /* Firmware option bytes */
1346 pBusLogic->aReplyBuffer[1] = 'A'; /* Special option byte */
1347
1348 /* We report version 5.07B. This reply will provide the first two digits. */
1349 pBusLogic->aReplyBuffer[2] = '5'; /* Major version 5 */
1350 pBusLogic->aReplyBuffer[3] = '0'; /* Minor version 0 */
1351 pBusLogic->cbReplyParametersLeft = 4; /* Reply is 4 bytes long */
1352 break;
1353 }
1354 case BUSLOGICCOMMAND_INQUIRE_FIRMWARE_VERSION_3RD_LETTER:
1355 {
1356 pBusLogic->aReplyBuffer[0] = '7';
1357 pBusLogic->cbReplyParametersLeft = 1;
1358 break;
1359 }
1360 case BUSLOGICCOMMAND_INQUIRE_FIRMWARE_VERSION_LETTER:
1361 {
1362 pBusLogic->aReplyBuffer[0] = 'B';
1363 pBusLogic->cbReplyParametersLeft = 1;
1364 break;
1365 }
1366 case BUSLOGICCOMMAND_INQUIRE_HOST_ADAPTER_MODEL_NUMBER:
1367 {
1368 /* The reply length is set by the guest and is found in the first byte of the command buffer. */
1369 pBusLogic->cbReplyParametersLeft = pBusLogic->aCommandBuffer[0];
1370 memset(pBusLogic->aReplyBuffer, ' ', pBusLogic->cbReplyParametersLeft);
1371 const char aModelName[] = "958";
1372 int cCharsToTransfer = (pBusLogic->cbReplyParametersLeft <= (sizeof(aModelName) - 1))
1373 ? pBusLogic->cbReplyParametersLeft
1374 : sizeof(aModelName) - 1;
1375
1376 for (int i = 0; i < cCharsToTransfer; i++)
1377 pBusLogic->aReplyBuffer[i] = aModelName[i];
1378
1379 break;
1380 }
1381 case BUSLOGICCOMMAND_INQUIRE_CONFIGURATION:
1382 {
1383 pBusLogic->cbReplyParametersLeft = sizeof(ReplyInquireConfiguration);
1384 PReplyInquireConfiguration pReply = (PReplyInquireConfiguration)pBusLogic->aReplyBuffer;
1385 memset(pReply, 0, sizeof(ReplyInquireConfiguration));
1386
1387 pReply->uHostAdapterId = 7; /* The controller has always 7 as ID. */
1388 /*
1389 * The rest of this reply only applies for ISA adapters.
1390 * This is a PCI adapter so they are not important and are skipped.
1391 */
1392 break;
1393 }
1394 case BUSLOGICCOMMAND_INQUIRE_EXTENDED_SETUP_INFORMATION:
1395 {
1396 /* The reply length is set by the guest and is found in the first byte of the command buffer. */
1397 pBusLogic->cbReplyParametersLeft = pBusLogic->aCommandBuffer[0];
1398 PReplyInquireExtendedSetupInformation pReply = (PReplyInquireExtendedSetupInformation)pBusLogic->aReplyBuffer;
1399 memset(pReply, 0, sizeof(ReplyInquireExtendedSetupInformation));
1400
1401 //@todo: should this reflect the RAM contents (AutoSCSIRam)?
1402 pReply->uBusType = 'E'; /* EISA style */
1403 pReply->u16ScatterGatherLimit = 8192;
1404 pReply->fLevelSensitiveInterrupt = true;
1405 pReply->fHostWideSCSI = true;
1406 pReply->fHostUltraSCSI = true;
1407 memcpy(pReply->aFirmwareRevision, "07B", sizeof(pReply->aFirmwareRevision));
1408
1409 break;
1410 }
1411 case BUSLOGICCOMMAND_INQUIRE_SETUP_INFORMATION:
1412 {
1413 /* The reply length is set by the guest and is found in the first byte of the command buffer. */
1414 pBusLogic->cbReplyParametersLeft = pBusLogic->aCommandBuffer[0];
1415 PReplyInquireSetupInformation pReply = (PReplyInquireSetupInformation)pBusLogic->aReplyBuffer;
1416 memset(pReply, 0, sizeof(ReplyInquireSetupInformation));
1417 break;
1418 }
1419 case BUSLOGICCOMMAND_FETCH_HOST_ADAPTER_LOCAL_RAM:
1420 {
1421 /*
1422 * First element in the command buffer contains start offset to read from
1423 * and second one the number of bytes to read.
1424 */
1425 uint8_t uOffset = pBusLogic->aCommandBuffer[0];
1426 pBusLogic->cbReplyParametersLeft = pBusLogic->aCommandBuffer[1];
1427
1428 pBusLogic->fUseLocalRam = true;
1429 pBusLogic->iReply = uOffset;
1430 break;
1431 }
1432 case BUSLOGICCOMMAND_INITIALIZE_EXTENDED_MAILBOX:
1433 {
1434 PRequestInitializeExtendedMailbox pRequest = (PRequestInitializeExtendedMailbox)pBusLogic->aCommandBuffer;
1435
1436 pBusLogic->cMailbox = pRequest->cMailbox;
1437 pBusLogic->GCPhysAddrMailboxOutgoingBase = (RTGCPHYS)pRequest->uMailboxBaseAddress;
1438 /* The area for incoming mailboxes is right after the last entry of outgoing mailboxes. */
1439 pBusLogic->GCPhysAddrMailboxIncomingBase = (RTGCPHYS)pRequest->uMailboxBaseAddress + (pBusLogic->cMailbox * sizeof(Mailbox));
1440
1441 Log(("GCPhysAddrMailboxOutgoingBase=%RGp\n", pBusLogic->GCPhysAddrMailboxOutgoingBase));
1442 Log(("GCPhysAddrMailboxIncomingBase=%RGp\n", pBusLogic->GCPhysAddrMailboxIncomingBase));
1443 Log(("cMailboxes=%u\n", pBusLogic->cMailbox));
1444
1445 pBusLogic->regStatus &= ~BUSLOGIC_REGISTER_STATUS_INITIALIZATION_REQUIRED;
1446 pBusLogic->cbReplyParametersLeft = 0;
1447 break;
1448 }
1449 case BUSLOGICCOMMAND_ENABLE_STRICT_ROUND_ROBIN_MODE:
1450 {
1451 if (pBusLogic->aCommandBuffer[0] == 0)
1452 pBusLogic->fStrictRoundRobinMode = false;
1453 else if (pBusLogic->aCommandBuffer[0] == 1)
1454 pBusLogic->fStrictRoundRobinMode = true;
1455 else
1456 AssertMsgFailed(("Invalid round robin mode %d\n", pBusLogic->aCommandBuffer[0]));
1457
1458 pBusLogic->cbReplyParametersLeft = 0;
1459 break;
1460 }
1461 case BUSLOGICCOMMAND_SET_CCB_FORMAT:
1462 {
1463 if (pBusLogic->aCommandBuffer[0] == 0)
1464 pBusLogic->fExtendedLunCCBFormat = false;
1465 else if (pBusLogic->aCommandBuffer[0] == 1)
1466 pBusLogic->fExtendedLunCCBFormat = true;
1467 else
1468 AssertMsgFailed(("Invalid CCB format %d\n", pBusLogic->aCommandBuffer[0]));
1469
1470 pBusLogic->cbReplyParametersLeft = 0;
1471 break;
1472 }
1473 case BUSLOGICCOMMAND_INQUIRE_TARGET_DEVICES:
1474 {
1475 /* Each bit which is set in the 16bit wide variable means a present device. */
1476 uint16_t u16TargetsPresentMask = 0;
1477
1478 for (uint8_t i = 0; i < RT_ELEMENTS(pBusLogic->aDeviceStates); i++)
1479 {
1480 if (pBusLogic->aDeviceStates[i].fPresent)
1481 u16TargetsPresentMask |= (1 << i);
1482 }
1483 pBusLogic->aReplyBuffer[0] = (uint8_t)u16TargetsPresentMask;
1484 pBusLogic->aReplyBuffer[1] = (uint8_t)(u16TargetsPresentMask >> 8);
1485 pBusLogic->cbReplyParametersLeft = 2;
1486 break;
1487 }
1488 case BUSLOGICCOMMAND_INQUIRE_SYNCHRONOUS_PERIOD:
1489 {
1490 pBusLogic->cbReplyParametersLeft = pBusLogic->aCommandBuffer[0];
1491
1492 for (uint8_t i = 0; i < pBusLogic->cbReplyParametersLeft; i++)
1493 pBusLogic->aReplyBuffer[i] = 0; /* @todo Figure if we need something other here. It's not needed for the linux driver */
1494
1495 break;
1496 }
1497 case BUSLOGICCOMMAND_DISABLE_HOST_ADAPTER_INTERRUPT:
1498 {
1499 if (pBusLogic->aCommandBuffer[0] == 0)
1500 pBusLogic->fIRQEnabled = false;
1501 else
1502 pBusLogic->fIRQEnabled = true;
1503 /* No interrupt signaled regardless of enable/disable. */
1504 fSuppressIrq = true;
1505 break;
1506 }
1507 case BUSLOGICCOMMAND_ECHO_COMMAND_DATA:
1508 {
1509 pBusLogic->aReplyBuffer[0] = pBusLogic->aCommandBuffer[0];
1510 pBusLogic->cbReplyParametersLeft = 1;
1511 break;
1512 }
1513 case BUSLOGICCOMMAND_SET_PREEMPT_TIME_ON_BUS:
1514 {
1515 pBusLogic->cbReplyParametersLeft = 0;
1516 pBusLogic->LocalRam.structured.autoSCSIData.uBusOnDelay = pBusLogic->aCommandBuffer[0];
1517 Log(("Bus-on time: %d\n", pBusLogic->aCommandBuffer[0]));
1518 break;
1519 }
1520 case BUSLOGICCOMMAND_SET_TIME_OFF_BUS:
1521 {
1522 pBusLogic->cbReplyParametersLeft = 0;
1523 pBusLogic->LocalRam.structured.autoSCSIData.uBusOffDelay = pBusLogic->aCommandBuffer[0];
1524 Log(("Bus-off time: %d\n", pBusLogic->aCommandBuffer[0]));
1525 break;
1526 }
1527 case BUSLOGICCOMMAND_EXT_BIOS_INFO:
1528 case BUSLOGICCOMMAND_UNLOCK_MAILBOX:
1529 /* Commands valid for Adaptec 154xC which we don't handle since
1530 * we pretend being 154xB compatible. Just mark the command as invalid.
1531 */
1532 Log(("Command %#x not valid for this adapter\n", pBusLogic->uOperationCode));
1533 pBusLogic->cbReplyParametersLeft = 0;
1534 pBusLogic->regStatus |= BUSLOGIC_REGISTER_STATUS_COMMAND_INVALID;
1535 break;
1536 case BUSLOGICCOMMAND_EXECUTE_MAILBOX_COMMAND: /* Should be handled already. */
1537 default:
1538 AssertMsgFailed(("Invalid command %#x\n", pBusLogic->uOperationCode));
1539 }
1540
1541 Log(("uOperationCode=%#x, cbReplyParametersLeft=%d\n", pBusLogic->uOperationCode, pBusLogic->cbReplyParametersLeft));
1542
1543 /* Set the data in ready bit in the status register in case the command has a reply. */
1544 if (pBusLogic->cbReplyParametersLeft)
1545 pBusLogic->regStatus |= BUSLOGIC_REGISTER_STATUS_DATA_IN_REGISTER_READY;
1546 else
1547 buslogicCommandComplete(pBusLogic, fSuppressIrq);
1548
1549 return rc;
1550}
1551
1552/**
1553 * Read a register from the BusLogic adapter.
1554 *
1555 * @returns VBox status code.
1556 * @param pBusLogic Pointer to the BusLogic instance data.
1557 * @param iRegister The index of the register to read.
1558 * @param pu32 Where to store the register content.
1559 */
1560static int buslogicRegisterRead(PBUSLOGIC pBusLogic, unsigned iRegister, uint32_t *pu32)
1561{
1562 int rc = VINF_SUCCESS;
1563
1564 switch (iRegister)
1565 {
1566 case BUSLOGIC_REGISTER_STATUS:
1567 {
1568 *pu32 = pBusLogic->regStatus;
1569 /*
1570 * If the diagnostic active bit is set we are in a hard reset initiated from the guest.
1571 * The guest reads the status register and waits that the host adapter ready bit is set.
1572 */
1573 if (pBusLogic->regStatus & BUSLOGIC_REGISTER_STATUS_DIAGNOSTIC_ACTIVE)
1574 {
1575 pBusLogic->regStatus &= ~BUSLOGIC_REGISTER_STATUS_DIAGNOSTIC_ACTIVE;
1576 pBusLogic->regStatus |= BUSLOGIC_REGISTER_STATUS_HOST_ADAPTER_READY;
1577 }
1578 break;
1579 }
1580 case BUSLOGIC_REGISTER_DATAIN:
1581 {
1582 if (pBusLogic->fUseLocalRam)
1583 *pu32 = pBusLogic->LocalRam.u8View[pBusLogic->iReply];
1584 else
1585 *pu32 = pBusLogic->aReplyBuffer[pBusLogic->iReply];
1586
1587 pBusLogic->iReply++;
1588 pBusLogic->cbReplyParametersLeft--;
1589
1590 LogFlowFunc(("cbReplyParametersLeft=%u\n", pBusLogic->cbReplyParametersLeft));
1591 if (!pBusLogic->cbReplyParametersLeft)
1592 {
1593 /*
1594 * Reply finished, set command complete bit, unset data in ready bit and
1595 * interrupt the guest if enabled.
1596 */
1597 buslogicCommandComplete(pBusLogic, false);
1598 }
1599 break;
1600 }
1601 case BUSLOGIC_REGISTER_INTERRUPT:
1602 {
1603 *pu32 = pBusLogic->regInterrupt;
1604 break;
1605 }
1606 case BUSLOGIC_REGISTER_GEOMETRY:
1607 {
1608 *pu32 = pBusLogic->regGeometry;
1609 break;
1610 }
1611 default:
1612 *pu32 = UINT32_C(0xffffffff);
1613 }
1614
1615 Log2(("%s: pu32=%p:{%.*Rhxs} iRegister=%d rc=%Rrc\n",
1616 __FUNCTION__, pu32, 1, pu32, iRegister, rc));
1617
1618 return rc;
1619}
1620
1621/**
1622 * Write a value to a register.
1623 *
1624 * @returns VBox status code.
1625 * @param pBusLogic Pointer to the BusLogic instance data.
1626 * @param iRegister The index of the register to read.
1627 * @param uVal The value to write.
1628 */
1629static int buslogicRegisterWrite(PBUSLOGIC pBusLogic, unsigned iRegister, uint8_t uVal)
1630{
1631 int rc = VINF_SUCCESS;
1632
1633 switch (iRegister)
1634 {
1635 case BUSLOGIC_REGISTER_CONTROL:
1636 {
1637 rc = PDMCritSectEnter(&pBusLogic->CritSectIntr, VINF_IOM_HC_IOPORT_WRITE);
1638 if (rc != VINF_SUCCESS)
1639 return rc;
1640
1641#ifdef LOG_ENABLED
1642 uint32_t cMailboxesReady = ASMAtomicXchgU32(&pBusLogic->cInMailboxesReady, 0);
1643 Log(("%u incoming mailboxes are ready when this interrupt was cleared\n", cMailboxesReady));
1644#endif
1645
1646 if (uVal & BUSLOGIC_REGISTER_CONTROL_INTERRUPT_RESET)
1647 buslogicClearInterrupt(pBusLogic);
1648
1649 PDMCritSectLeave(&pBusLogic->CritSectIntr);
1650
1651 if ((uVal & BUSLOGIC_REGISTER_CONTROL_HARD_RESET) || (uVal & BUSLOGIC_REGISTER_CONTROL_SOFT_RESET))
1652 {
1653#ifdef IN_RING3
1654 buslogicIntiateHardReset(pBusLogic);
1655#else
1656 rc = VINF_IOM_HC_IOPORT_WRITE;
1657#endif
1658 }
1659
1660 break;
1661 }
1662 case BUSLOGIC_REGISTER_COMMAND:
1663 {
1664 /* Fast path for mailbox execution command. */
1665 if ((uVal == BUSLOGICCOMMAND_EXECUTE_MAILBOX_COMMAND) && (pBusLogic->uOperationCode == 0xff))
1666 {
1667 ASMAtomicIncU32(&pBusLogic->cMailboxesReady);
1668 if (!ASMAtomicXchgBool(&pBusLogic->fNotificationSend, true))
1669 {
1670 /* Send new notification to the queue. */
1671 PPDMQUEUEITEMCORE pItem = PDMQueueAlloc(pBusLogic->CTX_SUFF(pNotifierQueue));
1672 AssertMsg(pItem, ("Allocating item for queue failed\n"));
1673 PDMQueueInsert(pBusLogic->CTX_SUFF(pNotifierQueue), (PPDMQUEUEITEMCORE)pItem);
1674 }
1675
1676 return rc;
1677 }
1678
1679 /*
1680 * Check if we are already fetch command parameters from the guest.
1681 * If not we initialize executing a new command.
1682 */
1683 if (pBusLogic->uOperationCode == 0xff)
1684 {
1685 pBusLogic->uOperationCode = uVal;
1686 pBusLogic->iParameter = 0;
1687
1688 /* Mark host adapter as busy and clear the invalid status bit. */
1689 pBusLogic->regStatus &= ~(BUSLOGIC_REGISTER_STATUS_HOST_ADAPTER_READY | BUSLOGIC_REGISTER_STATUS_COMMAND_INVALID);
1690
1691 /* Get the number of bytes for parameters from the command code. */
1692 switch (pBusLogic->uOperationCode)
1693 {
1694 case BUSLOGICCOMMAND_INQUIRE_FIRMWARE_VERSION_LETTER:
1695 case BUSLOGICCOMMAND_INQUIRE_BOARD_ID:
1696 case BUSLOGICCOMMAND_INQUIRE_FIRMWARE_VERSION_3RD_LETTER:
1697 case BUSLOGICCOMMAND_INQUIRE_PCI_HOST_ADAPTER_INFORMATION:
1698 case BUSLOGICCOMMAND_INQUIRE_CONFIGURATION:
1699 case BUSLOGICCOMMAND_INQUIRE_TARGET_DEVICES:
1700 pBusLogic->cbCommandParametersLeft = 0;
1701 break;
1702 case BUSLOGICCOMMAND_MODIFY_IO_ADDRESS:
1703 case BUSLOGICCOMMAND_INQUIRE_EXTENDED_SETUP_INFORMATION:
1704 case BUSLOGICCOMMAND_INQUIRE_SETUP_INFORMATION:
1705 case BUSLOGICCOMMAND_INQUIRE_HOST_ADAPTER_MODEL_NUMBER:
1706 case BUSLOGICCOMMAND_ENABLE_STRICT_ROUND_ROBIN_MODE:
1707 case BUSLOGICCOMMAND_SET_CCB_FORMAT:
1708 case BUSLOGICCOMMAND_INQUIRE_SYNCHRONOUS_PERIOD:
1709 case BUSLOGICCOMMAND_DISABLE_HOST_ADAPTER_INTERRUPT:
1710 case BUSLOGICCOMMAND_ECHO_COMMAND_DATA:
1711 case BUSLOGICCOMMAND_SET_PREEMPT_TIME_ON_BUS:
1712 case BUSLOGICCOMMAND_SET_TIME_OFF_BUS:
1713 pBusLogic->cbCommandParametersLeft = 1;
1714 break;
1715 case BUSLOGICCOMMAND_FETCH_HOST_ADAPTER_LOCAL_RAM:
1716 pBusLogic->cbCommandParametersLeft = 2;
1717 break;
1718 case BUSLOGICCOMMAND_INITIALIZE_EXTENDED_MAILBOX:
1719 pBusLogic->cbCommandParametersLeft = sizeof(RequestInitializeExtendedMailbox);
1720 break;
1721 case BUSLOGICCOMMAND_EXT_BIOS_INFO:
1722 case BUSLOGICCOMMAND_UNLOCK_MAILBOX:
1723 /* Invalid commands. */
1724 pBusLogic->cbCommandParametersLeft = 0;
1725 break;
1726 case BUSLOGICCOMMAND_EXECUTE_MAILBOX_COMMAND: /* Should not come here anymore. */
1727 default:
1728 AssertMsgFailed(("Invalid operation code %#x\n", uVal));
1729 }
1730 }
1731 else
1732 {
1733 /*
1734 * The real adapter would set the Command register busy bit in the status register.
1735 * The guest has to wait until it is unset.
1736 * We don't need to do it because the guest does not continue execution while we are in this
1737 * function.
1738 */
1739 pBusLogic->aCommandBuffer[pBusLogic->iParameter] = uVal;
1740 pBusLogic->iParameter++;
1741 pBusLogic->cbCommandParametersLeft--;
1742 }
1743
1744 /* Start execution of command if there are no parameters left. */
1745 if (!pBusLogic->cbCommandParametersLeft)
1746 {
1747 rc = buslogicProcessCommand(pBusLogic);
1748 AssertMsgRC(rc, ("Processing command failed rc=%Rrc\n", rc));
1749 }
1750 break;
1751 }
1752 default:
1753 AssertMsgFailed(("Register not available\n"));
1754 rc = VERR_IOM_IOPORT_UNUSED;
1755 }
1756
1757 return rc;
1758}
1759
1760/**
1761 * Memory mapped I/O Handler for read operations.
1762 *
1763 * @returns VBox status code.
1764 *
1765 * @param pDevIns The device instance.
1766 * @param pvUser User argument.
1767 * @param GCPhysAddr Physical address (in GC) where the read starts.
1768 * @param pv Where to store the result.
1769 * @param cb Number of bytes read.
1770 */
1771PDMBOTHCBDECL(int) buslogicMMIORead(PPDMDEVINS pDevIns, void *pvUser,
1772 RTGCPHYS GCPhysAddr, void *pv, unsigned cb)
1773{
1774 /* the linux driver does not make use of the MMIO area. */
1775 AssertMsgFailed(("MMIO Read\n"));
1776 return VINF_SUCCESS;
1777}
1778
1779/**
1780 * Memory mapped I/O Handler for write operations.
1781 *
1782 * @returns VBox status code.
1783 *
1784 * @param pDevIns The device instance.
1785 * @param pvUser User argument.
1786 * @param GCPhysAddr Physical address (in GC) where the read starts.
1787 * @param pv Where to fetch the result.
1788 * @param cb Number of bytes to write.
1789 */
1790PDMBOTHCBDECL(int) buslogicMMIOWrite(PPDMDEVINS pDevIns, void *pvUser,
1791 RTGCPHYS GCPhysAddr, void const *pv, unsigned cb)
1792{
1793 /* the linux driver does not make use of the MMIO area. */
1794 AssertMsgFailed(("MMIO Write\n"));
1795 return VINF_SUCCESS;
1796}
1797
1798/**
1799 * Port I/O Handler for IN operations.
1800 *
1801 * @returns VBox status code.
1802 *
1803 * @param pDevIns The device instance.
1804 * @param pvUser User argument.
1805 * @param uPort Port number used for the IN operation.
1806 * @param pu32 Where to store the result.
1807 * @param cb Number of bytes read.
1808 */
1809PDMBOTHCBDECL(int) buslogicIOPortRead (PPDMDEVINS pDevIns, void *pvUser,
1810 RTIOPORT Port, uint32_t *pu32, unsigned cb)
1811{
1812 PBUSLOGIC pBusLogic = PDMINS_2_DATA(pDevIns, PBUSLOGIC);;
1813 unsigned iRegister = Port - pBusLogic->IOPortBase;
1814
1815 Assert(cb == 1);
1816
1817 return buslogicRegisterRead(pBusLogic, iRegister, pu32);
1818}
1819
1820/**
1821 * Port I/O Handler for OUT operations.
1822 *
1823 * @returns VBox status code.
1824 *
1825 * @param pDevIns The device instance.
1826 * @param pvUser User argument.
1827 * @param uPort Port number used for the IN operation.
1828 * @param u32 The value to output.
1829 * @param cb The value size in bytes.
1830 */
1831PDMBOTHCBDECL(int) buslogicIOPortWrite (PPDMDEVINS pDevIns, void *pvUser,
1832 RTIOPORT Port, uint32_t u32, unsigned cb)
1833{
1834 PBUSLOGIC pBusLogic = PDMINS_2_DATA(pDevIns, PBUSLOGIC);
1835 int rc = VINF_SUCCESS;
1836 unsigned iRegister = Port - pBusLogic->IOPortBase;
1837 uint8_t uVal = (uint8_t)u32;
1838
1839 Assert(cb == 1);
1840
1841 rc = buslogicRegisterWrite(pBusLogic, iRegister, (uint8_t)uVal);
1842
1843 Log2(("#%d %s: pvUser=%#p cb=%d u32=%#x Port=%#x rc=%Rrc\n",
1844 pDevIns->iInstance, __FUNCTION__, pvUser, cb, u32, Port, rc));
1845
1846 return rc;
1847}
1848
1849#ifdef IN_RING3
1850/**
1851 * Port I/O Handler for IN operations - legacy port.
1852 *
1853 * @returns VBox status code.
1854 *
1855 * @param pDevIns The device instance.
1856 * @param pvUser User argument.
1857 * @param uPort Port number used for the IN operation.
1858 * @param pu32 Where to store the result.
1859 * @param cb Number of bytes read.
1860 */
1861static int buslogicIsaIOPortRead (PPDMDEVINS pDevIns, void *pvUser,
1862 RTIOPORT Port, uint32_t *pu32, unsigned cb)
1863{
1864 int rc;
1865 PBUSLOGIC pBusLogic = PDMINS_2_DATA(pDevIns, PBUSLOGIC);
1866
1867 Assert(cb == 1);
1868
1869 if (!pBusLogic->fISAEnabled)
1870 return VINF_SUCCESS;
1871
1872 rc = vboxscsiReadRegister(&pBusLogic->VBoxSCSI, (Port - BUSLOGIC_ISA_IO_PORT), pu32);
1873
1874 //Log2(("%s: pu32=%p:{%.*Rhxs} iRegister=%d rc=%Rrc\n",
1875 // __FUNCTION__, pu32, 1, pu32, (Port - BUSLOGIC_ISA_IO_PORT), rc));
1876
1877 return rc;
1878}
1879
1880static void buslogicWarningDiskFull(PPDMDEVINS pDevIns)
1881{
1882 int rc;
1883 LogRel(("BusLogic#%d: Host disk full\n", pDevIns->iInstance));
1884 rc = PDMDevHlpVMSetRuntimeError(pDevIns, VMSETRTERR_FLAGS_SUSPEND | VMSETRTERR_FLAGS_NO_WAIT, "DevBusLogic_DISKFULL",
1885 N_("Host system reported disk full. VM execution is suspended. You can resume after freeing some space"));
1886 AssertRC(rc);
1887}
1888
1889static void buslogicWarningFileTooBig(PPDMDEVINS pDevIns)
1890{
1891 int rc;
1892 LogRel(("BusLogic#%d: File too big\n", pDevIns->iInstance));
1893 rc = PDMDevHlpVMSetRuntimeError(pDevIns, VMSETRTERR_FLAGS_SUSPEND | VMSETRTERR_FLAGS_NO_WAIT, "DevBusLogic_FILETOOBIG",
1894 N_("Host system reported that the file size limit of the host file system has been exceeded. VM execution is suspended. You need to move your virtual hard disk to a filesystem which allows bigger files"));
1895 AssertRC(rc);
1896}
1897
1898static void buslogicWarningISCSI(PPDMDEVINS pDevIns)
1899{
1900 int rc;
1901 LogRel(("BusLogic#%d: iSCSI target unavailable\n", pDevIns->iInstance));
1902 rc = PDMDevHlpVMSetRuntimeError(pDevIns, VMSETRTERR_FLAGS_SUSPEND | VMSETRTERR_FLAGS_NO_WAIT, "DevBusLogic_ISCSIDOWN",
1903 N_("The iSCSI target has stopped responding. VM execution is suspended. You can resume when it is available again"));
1904 AssertRC(rc);
1905}
1906
1907static void buslogicWarningUnknown(PPDMDEVINS pDevIns, int rc)
1908{
1909 int rc2;
1910 LogRel(("BusLogic#%d: Unknown but recoverable error has occurred (rc=%Rrc)\n", pDevIns->iInstance, rc));
1911 rc2 = PDMDevHlpVMSetRuntimeError(pDevIns, VMSETRTERR_FLAGS_SUSPEND | VMSETRTERR_FLAGS_NO_WAIT, "DevBusLogic_UNKNOWN",
1912 N_("An unknown but recoverable I/O error has occurred (rc=%Rrc). VM execution is suspended. You can resume when the error is fixed"), rc);
1913 AssertRC(rc2);
1914}
1915
1916static void buslogicRedoSetWarning(PBUSLOGIC pThis, int rc)
1917{
1918 if (rc == VERR_DISK_FULL)
1919 buslogicWarningDiskFull(pThis->CTX_SUFF(pDevIns));
1920 else if (rc == VERR_FILE_TOO_BIG)
1921 buslogicWarningFileTooBig(pThis->CTX_SUFF(pDevIns));
1922 else if (rc == VERR_BROKEN_PIPE || rc == VERR_NET_CONNECTION_REFUSED)
1923 {
1924 /* iSCSI connection abort (first error) or failure to reestablish
1925 * connection (second error). Pause VM. On resume we'll retry. */
1926 buslogicWarningISCSI(pThis->CTX_SUFF(pDevIns));
1927 }
1928 else
1929 buslogicWarningUnknown(pThis->CTX_SUFF(pDevIns), rc);
1930}
1931
1932
1933static int buslogicPrepareBIOSSCSIRequest(PBUSLOGIC pBusLogic)
1934{
1935 int rc;
1936 PBUSLOGICTASKSTATE pTaskState;
1937 uint32_t uTargetDevice;
1938
1939 rc = RTMemCacheAllocEx(pBusLogic->hTaskCache, (void **)&pTaskState);
1940 AssertMsgRCReturn(rc, ("Getting task from cache failed rc=%Rrc\n", rc), rc);
1941
1942 pTaskState->fBIOS = true;
1943
1944 rc = vboxscsiSetupRequest(&pBusLogic->VBoxSCSI, &pTaskState->PDMScsiRequest, &uTargetDevice);
1945 AssertMsgRCReturn(rc, ("Setting up SCSI request failed rc=%Rrc\n", rc), rc);
1946
1947 pTaskState->PDMScsiRequest.pvUser = pTaskState;
1948
1949 pTaskState->CTX_SUFF(pTargetDevice) = &pBusLogic->aDeviceStates[uTargetDevice];
1950
1951 if (!pTaskState->CTX_SUFF(pTargetDevice)->fPresent)
1952 {
1953 /* Device is not present. */
1954 AssertMsg(pTaskState->PDMScsiRequest.pbCDB[0] == SCSI_INQUIRY,
1955 ("Device is not present but command is not inquiry\n"));
1956
1957 SCSIINQUIRYDATA ScsiInquiryData;
1958
1959 memset(&ScsiInquiryData, 0, sizeof(SCSIINQUIRYDATA));
1960 ScsiInquiryData.u5PeripheralDeviceType = SCSI_INQUIRY_DATA_PERIPHERAL_DEVICE_TYPE_UNKNOWN;
1961 ScsiInquiryData.u3PeripheralQualifier = SCSI_INQUIRY_DATA_PERIPHERAL_QUALIFIER_NOT_CONNECTED_NOT_SUPPORTED;
1962
1963 memcpy(pBusLogic->VBoxSCSI.pBuf, &ScsiInquiryData, 5);
1964
1965 rc = vboxscsiRequestFinished(&pBusLogic->VBoxSCSI, &pTaskState->PDMScsiRequest);
1966 AssertMsgRCReturn(rc, ("Finishing BIOS SCSI request failed rc=%Rrc\n", rc), rc);
1967
1968 RTMemCacheFree(pBusLogic->hTaskCache, pTaskState);
1969 }
1970 else
1971 {
1972 LogFlowFunc(("before increment %u\n", pTaskState->CTX_SUFF(pTargetDevice)->cOutstandingRequests));
1973 ASMAtomicIncU32(&pTaskState->CTX_SUFF(pTargetDevice)->cOutstandingRequests);
1974 LogFlowFunc(("after increment %u\n", pTaskState->CTX_SUFF(pTargetDevice)->cOutstandingRequests));
1975
1976 rc = pTaskState->CTX_SUFF(pTargetDevice)->pDrvSCSIConnector->pfnSCSIRequestSend(pTaskState->CTX_SUFF(pTargetDevice)->pDrvSCSIConnector,
1977 &pTaskState->PDMScsiRequest);
1978 AssertMsgRC(rc, ("Sending request to SCSI layer failed rc=%Rrc\n", rc));
1979 }
1980
1981 return rc;
1982}
1983
1984/**
1985 * Port I/O Handler for OUT operations - legacy port.
1986 *
1987 * @returns VBox status code.
1988 *
1989 * @param pDevIns The device instance.
1990 * @param pvUser User argument.
1991 * @param uPort Port number used for the IN operation.
1992 * @param u32 The value to output.
1993 * @param cb The value size in bytes.
1994 */
1995static int buslogicIsaIOPortWrite (PPDMDEVINS pDevIns, void *pvUser,
1996 RTIOPORT Port, uint32_t u32, unsigned cb)
1997{
1998 int rc;
1999 PBUSLOGIC pBusLogic = PDMINS_2_DATA(pDevIns, PBUSLOGIC);
2000
2001 Log2(("#%d %s: pvUser=%#p cb=%d u32=%#x Port=%#x\n",
2002 pDevIns->iInstance, __FUNCTION__, pvUser, cb, u32, Port));
2003
2004 Assert(cb == 1);
2005
2006 if (!pBusLogic->fISAEnabled)
2007 return VINF_SUCCESS;
2008
2009 rc = vboxscsiWriteRegister(&pBusLogic->VBoxSCSI, (Port - BUSLOGIC_ISA_IO_PORT), (uint8_t)u32);
2010 if (rc == VERR_MORE_DATA)
2011 {
2012 rc = buslogicPrepareBIOSSCSIRequest(pBusLogic);
2013 AssertRC(rc);
2014 }
2015 else if (RT_FAILURE(rc))
2016 AssertMsgFailed(("Writing BIOS register failed %Rrc\n", rc));
2017
2018 return VINF_SUCCESS;
2019}
2020
2021/**
2022 * Port I/O Handler for primary port range OUT string operations.
2023 * @see FNIOMIOPORTOUTSTRING for details.
2024 */
2025static DECLCALLBACK(int) buslogicIsaIOPortWriteStr(PPDMDEVINS pDevIns, void *pvUser, RTIOPORT Port, RTGCPTR *pGCPtrSrc, PRTGCUINTREG pcTransfer, unsigned cb)
2026{
2027 PBUSLOGIC pBusLogic = PDMINS_2_DATA(pDevIns, PBUSLOGIC);
2028 int rc;
2029
2030 Log2(("#%d %s: pvUser=%#p cb=%d Port=%#x\n",
2031 pDevIns->iInstance, __FUNCTION__, pvUser, cb, Port));
2032
2033 rc = vboxscsiWriteString(pDevIns, &pBusLogic->VBoxSCSI, (Port - BUSLOGIC_ISA_IO_PORT),
2034 pGCPtrSrc, pcTransfer, cb);
2035 if (rc == VERR_MORE_DATA)
2036 {
2037 rc = buslogicPrepareBIOSSCSIRequest(pBusLogic);
2038 AssertRC(rc);
2039 }
2040 else if (RT_FAILURE(rc))
2041 AssertMsgFailed(("Writing BIOS register failed %Rrc\n", rc));
2042
2043 return rc;
2044}
2045
2046/**
2047 * Port I/O Handler for primary port range IN string operations.
2048 * @see FNIOMIOPORTINSTRING for details.
2049 */
2050static DECLCALLBACK(int) buslogicIsaIOPortReadStr(PPDMDEVINS pDevIns, void *pvUser, RTIOPORT Port, RTGCPTR *pGCPtrDst, PRTGCUINTREG pcTransfer, unsigned cb)
2051{
2052 PBUSLOGIC pBusLogic = PDMINS_2_DATA(pDevIns, PBUSLOGIC);
2053
2054 LogFlowFunc(("#%d %s: pvUser=%#p cb=%d Port=%#x\n",
2055 pDevIns->iInstance, __FUNCTION__, pvUser, cb, Port));
2056
2057 return vboxscsiReadString(pDevIns, &pBusLogic->VBoxSCSI, (Port - BUSLOGIC_ISA_IO_PORT),
2058 pGCPtrDst, pcTransfer, cb);
2059}
2060
2061static DECLCALLBACK(int) buslogicMMIOMap(PPCIDEVICE pPciDev, /*unsigned*/ int iRegion,
2062 RTGCPHYS GCPhysAddress, uint32_t cb,
2063 PCIADDRESSSPACE enmType)
2064{
2065 PPDMDEVINS pDevIns = pPciDev->pDevIns;
2066 PBUSLOGIC pThis = PDMINS_2_DATA(pDevIns, PBUSLOGIC);
2067 int rc = VINF_SUCCESS;
2068
2069 Log2(("%s: registering MMIO area at GCPhysAddr=%RGp cb=%u\n", __FUNCTION__, GCPhysAddress, cb));
2070
2071 Assert(cb >= 32);
2072
2073 if (enmType == PCI_ADDRESS_SPACE_MEM)
2074 {
2075 /* We use the assigned size here, because we currently only support page aligned MMIO ranges. */
2076 rc = PDMDevHlpMMIORegister(pDevIns, GCPhysAddress, cb, NULL,
2077 buslogicMMIOWrite, buslogicMMIORead, NULL, "BusLogic");
2078 if (RT_FAILURE(rc))
2079 return rc;
2080
2081 if (pThis->fR0Enabled)
2082 {
2083 rc = PDMDevHlpMMIORegisterR0(pDevIns, GCPhysAddress, cb, 0,
2084 "buslogicMMIOWrite", "buslogicMMIORead", NULL);
2085 if (RT_FAILURE(rc))
2086 return rc;
2087 }
2088
2089 if (pThis->fGCEnabled)
2090 {
2091 rc = PDMDevHlpMMIORegisterRC(pDevIns, GCPhysAddress, cb, 0,
2092 "buslogicMMIOWrite", "buslogicMMIORead", NULL);
2093 if (RT_FAILURE(rc))
2094 return rc;
2095 }
2096
2097 pThis->MMIOBase = GCPhysAddress;
2098 }
2099 else if (enmType == PCI_ADDRESS_SPACE_IO)
2100 {
2101 rc = PDMDevHlpIOPortRegister(pDevIns, (RTIOPORT)GCPhysAddress, 32,
2102 NULL, buslogicIOPortWrite, buslogicIOPortRead, NULL, NULL, "BusLogic");
2103 if (RT_FAILURE(rc))
2104 return rc;
2105
2106 if (pThis->fR0Enabled)
2107 {
2108 rc = PDMDevHlpIOPortRegisterR0(pDevIns, (RTIOPORT)GCPhysAddress, 32,
2109 0, "buslogicIOPortWrite", "buslogicIOPortRead", NULL, NULL, "BusLogic");
2110 if (RT_FAILURE(rc))
2111 return rc;
2112 }
2113
2114 if (pThis->fGCEnabled)
2115 {
2116 rc = PDMDevHlpIOPortRegisterRC(pDevIns, (RTIOPORT)GCPhysAddress, 32,
2117 0, "buslogicIOPortWrite", "buslogicIOPortRead", NULL, NULL, "BusLogic");
2118 if (RT_FAILURE(rc))
2119 return rc;
2120 }
2121
2122 pThis->IOPortBase = (RTIOPORT)GCPhysAddress;
2123 }
2124 else
2125 AssertMsgFailed(("Invalid enmType=%d\n", enmType));
2126
2127 return rc;
2128}
2129
2130static DECLCALLBACK(int) buslogicDeviceSCSIRequestCompleted(PPDMISCSIPORT pInterface, PPDMSCSIREQUEST pSCSIRequest,
2131 int rcCompletion, bool fRedo, int rcReq)
2132{
2133 int rc;
2134 PBUSLOGICTASKSTATE pTaskState = (PBUSLOGICTASKSTATE)pSCSIRequest->pvUser;
2135 PBUSLOGICDEVICE pBusLogicDevice = pTaskState->CTX_SUFF(pTargetDevice);
2136 PBUSLOGIC pBusLogic = pBusLogicDevice->CTX_SUFF(pBusLogic);
2137
2138 LogFlowFunc(("before decrement %u\n", pBusLogicDevice->cOutstandingRequests));
2139 ASMAtomicDecU32(&pBusLogicDevice->cOutstandingRequests);
2140 LogFlowFunc(("after decrement %u\n", pBusLogicDevice->cOutstandingRequests));
2141
2142 if (fRedo)
2143 {
2144 if (!pTaskState->fBIOS)
2145 {
2146 buslogicDataBufferFree(pTaskState);
2147
2148 if (pTaskState->pbSenseBuffer)
2149 buslogicSenseBufferFree(pTaskState, false /* fCopy */);
2150 }
2151
2152 /* Add to the list. */
2153 do
2154 {
2155 pTaskState->pRedoNext = ASMAtomicReadPtrT(&pBusLogic->pTasksRedoHead, PBUSLOGICTASKSTATE);
2156 } while (!ASMAtomicCmpXchgPtr(&pBusLogic->pTasksRedoHead, pTaskState, pTaskState->pRedoNext));
2157
2158 /* Suspend the VM if not done already. */
2159 if (!ASMAtomicXchgBool(&pBusLogic->fRedo, true))
2160 buslogicRedoSetWarning(pBusLogic, rcReq);
2161 }
2162 else
2163 {
2164 if (pTaskState->fBIOS)
2165 {
2166 rc = vboxscsiRequestFinished(&pBusLogic->VBoxSCSI, pSCSIRequest);
2167 AssertMsgRC(rc, ("Finishing BIOS SCSI request failed rc=%Rrc\n", rc));
2168 }
2169 else
2170 {
2171 buslogicDataBufferFree(pTaskState);
2172
2173 if (pTaskState->pbSenseBuffer)
2174 buslogicSenseBufferFree(pTaskState, (rcCompletion != SCSI_STATUS_OK));
2175
2176 if (rcCompletion == SCSI_STATUS_OK)
2177 buslogicSendIncomingMailbox(pBusLogic, pTaskState,
2178 BUSLOGIC_MAILBOX_INCOMING_ADAPTER_STATUS_CMD_COMPLETED,
2179 BUSLOGIC_MAILBOX_INCOMING_DEVICE_STATUS_OPERATION_GOOD,
2180 BUSLOGIC_MAILBOX_INCOMING_COMPLETION_WITHOUT_ERROR);
2181 else if (rcCompletion == SCSI_STATUS_CHECK_CONDITION)
2182 buslogicSendIncomingMailbox(pBusLogic, pTaskState,
2183 BUSLOGIC_MAILBOX_INCOMING_ADAPTER_STATUS_CMD_COMPLETED,
2184 BUSLOGIC_MAILBOX_INCOMING_DEVICE_STATUS_CHECK_CONDITION,
2185 BUSLOGIC_MAILBOX_INCOMING_COMPLETION_WITH_ERROR);
2186 else
2187 AssertMsgFailed(("invalid completion status %d\n", rcCompletion));
2188 }
2189
2190 /* Add task to the cache. */
2191 RTMemCacheFree(pBusLogic->hTaskCache, pTaskState);
2192 }
2193
2194 if (pBusLogicDevice->cOutstandingRequests == 0 && pBusLogic->fSignalIdle)
2195 PDMDevHlpAsyncNotificationCompleted(pBusLogic->pDevInsR3);
2196
2197 return VINF_SUCCESS;
2198}
2199
2200static DECLCALLBACK(int) buslogicQueryDeviceLocation(PPDMISCSIPORT pInterface, const char **ppcszController,
2201 uint32_t *piInstance, uint32_t *piLUN)
2202{
2203 PBUSLOGICDEVICE pBusLogicDevice = PDMISCSIPORT_2_PBUSLOGICDEVICE(pInterface);
2204 PPDMDEVINS pDevIns = pBusLogicDevice->CTX_SUFF(pBusLogic)->CTX_SUFF(pDevIns);
2205
2206 AssertPtrReturn(ppcszController, VERR_INVALID_POINTER);
2207 AssertPtrReturn(piInstance, VERR_INVALID_POINTER);
2208 AssertPtrReturn(piLUN, VERR_INVALID_POINTER);
2209
2210 *ppcszController = pDevIns->pReg->szName;
2211 *piInstance = pDevIns->iInstance;
2212 *piLUN = pBusLogicDevice->iLUN;
2213
2214 return VINF_SUCCESS;
2215}
2216
2217static int buslogicDeviceSCSIRequestSetup(PBUSLOGIC pBusLogic, PBUSLOGICTASKSTATE pTaskState)
2218{
2219 int rc = VINF_SUCCESS;
2220
2221 /* Fetch CCB. */
2222 RTGCPHYS GCPhysAddrCCB = (RTGCPHYS)pTaskState->MailboxGuest.u32PhysAddrCCB;
2223 PDMDevHlpPhysRead(pBusLogic->CTX_SUFF(pDevIns), GCPhysAddrCCB,
2224 &pTaskState->CommandControlBlockGuest, sizeof(CommandControlBlock));
2225
2226 PBUSLOGICDEVICE pTargetDevice = &pBusLogic->aDeviceStates[pTaskState->CommandControlBlockGuest.uTargetId];
2227 pTaskState->CTX_SUFF(pTargetDevice) = pTargetDevice;
2228
2229#ifdef DEBUG
2230 buslogicDumpCCBInfo(&pTaskState->CommandControlBlockGuest);
2231#endif
2232
2233 /* Alloc required buffers. */
2234 rc = buslogicDataBufferAlloc(pTaskState);
2235 AssertMsgRC(rc, ("Alloc failed rc=%Rrc\n", rc));
2236
2237 if (pTaskState->CommandControlBlockGuest.cbSenseData)
2238 {
2239 rc = buslogicSenseBufferAlloc(pTaskState);
2240 AssertMsgRC(rc, ("Mapping sense buffer failed rc=%Rrc\n", rc));
2241 }
2242
2243 /* Check if device is present on bus. If not return error immediately and don't process this further. */
2244 if (!pBusLogic->aDeviceStates[pTaskState->CommandControlBlockGuest.uTargetId].fPresent)
2245 {
2246 buslogicDataBufferFree(pTaskState);
2247
2248 if (pTaskState->pbSenseBuffer)
2249 buslogicSenseBufferFree(pTaskState, true);
2250
2251 buslogicSendIncomingMailbox(pBusLogic, pTaskState,
2252 BUSLOGIC_MAILBOX_INCOMING_ADAPTER_STATUS_SCSI_SELECTION_TIMEOUT,
2253 BUSLOGIC_MAILBOX_INCOMING_DEVICE_STATUS_OPERATION_GOOD,
2254 BUSLOGIC_MAILBOX_INCOMING_COMPLETION_WITH_ERROR);
2255
2256 RTMemCacheFree(pBusLogic->hTaskCache, pTaskState);
2257 }
2258 else
2259 {
2260 /* Setup SCSI request. */
2261 pTaskState->PDMScsiRequest.uLogicalUnit = pTaskState->CommandControlBlockGuest.uLogicalUnit;
2262
2263 if (pTaskState->CommandControlBlockGuest.uDataDirection == BUSLOGIC_CCB_DIRECTION_UNKNOWN)
2264 pTaskState->PDMScsiRequest.uDataDirection = PDMSCSIREQUESTTXDIR_UNKNOWN;
2265 else if (pTaskState->CommandControlBlockGuest.uDataDirection == BUSLOGIC_CCB_DIRECTION_IN)
2266 pTaskState->PDMScsiRequest.uDataDirection = PDMSCSIREQUESTTXDIR_FROM_DEVICE;
2267 else if (pTaskState->CommandControlBlockGuest.uDataDirection == BUSLOGIC_CCB_DIRECTION_OUT)
2268 pTaskState->PDMScsiRequest.uDataDirection = PDMSCSIREQUESTTXDIR_TO_DEVICE;
2269 else if (pTaskState->CommandControlBlockGuest.uDataDirection == BUSLOGIC_CCB_DIRECTION_NO_DATA)
2270 pTaskState->PDMScsiRequest.uDataDirection = PDMSCSIREQUESTTXDIR_NONE;
2271 else
2272 AssertMsgFailed(("Invalid data direction type %d\n", pTaskState->CommandControlBlockGuest.uDataDirection));
2273
2274 pTaskState->PDMScsiRequest.cbCDB = pTaskState->CommandControlBlockGuest.cbCDB;
2275 pTaskState->PDMScsiRequest.pbCDB = pTaskState->CommandControlBlockGuest.aCDB;
2276 if (pTaskState->DataSeg.cbSeg)
2277 {
2278 pTaskState->PDMScsiRequest.cbScatterGather = pTaskState->DataSeg.cbSeg;
2279 pTaskState->PDMScsiRequest.cScatterGatherEntries = 1;
2280 pTaskState->PDMScsiRequest.paScatterGatherHead = &pTaskState->DataSeg;
2281 }
2282 else
2283 {
2284 pTaskState->PDMScsiRequest.cbScatterGather = 0;
2285 pTaskState->PDMScsiRequest.cScatterGatherEntries = 0;
2286 pTaskState->PDMScsiRequest.paScatterGatherHead = NULL;
2287 }
2288 pTaskState->PDMScsiRequest.cbSenseBuffer = pTaskState->CommandControlBlockGuest.cbSenseData;
2289 pTaskState->PDMScsiRequest.pbSenseBuffer = pTaskState->pbSenseBuffer;
2290 pTaskState->PDMScsiRequest.pvUser = pTaskState;
2291
2292 ASMAtomicIncU32(&pTargetDevice->cOutstandingRequests);
2293 rc = pTargetDevice->pDrvSCSIConnector->pfnSCSIRequestSend(pTargetDevice->pDrvSCSIConnector, &pTaskState->PDMScsiRequest);
2294 AssertMsgRC(rc, ("Sending request to SCSI layer failed rc=%Rrc\n", rc));
2295 }
2296
2297 return rc;
2298}
2299
2300/**
2301 * Read mailbox from the guest and execute command.
2302 *
2303 * @returns VBox status code.
2304 * @param pBusLogic Pointer to the BusLogic instance data.
2305 */
2306static int buslogicProcessMailboxNext(PBUSLOGIC pBusLogic)
2307{
2308 PBUSLOGICTASKSTATE pTaskState = NULL;
2309 RTGCPHYS GCPhysAddrMailboxCurrent;
2310 int rc;
2311
2312 rc = RTMemCacheAllocEx(pBusLogic->hTaskCache, (void **)&pTaskState);
2313 AssertMsgReturn(RT_SUCCESS(rc) && (pTaskState != NULL), ("Failed to get task state from cache\n"), rc);
2314
2315 pTaskState->fBIOS = false;
2316
2317 if (!pBusLogic->fStrictRoundRobinMode)
2318 {
2319 /* Search for a filled mailbox - stop if we have scanned all mailboxes. */
2320 uint8_t uMailboxPosCur = pBusLogic->uMailboxOutgoingPositionCurrent;
2321
2322 do
2323 {
2324 /* Fetch mailbox from guest memory. */
2325 GCPhysAddrMailboxCurrent = buslogicOutgoingMailboxGetGCPhys(pBusLogic);
2326
2327 PDMDevHlpPhysRead(pBusLogic->CTX_SUFF(pDevIns), GCPhysAddrMailboxCurrent,
2328 &pTaskState->MailboxGuest, sizeof(Mailbox));
2329
2330 /* Check the next mailbox. */
2331 buslogicOutgoingMailboxAdvance(pBusLogic);
2332 } while ( pTaskState->MailboxGuest.u.out.uActionCode == BUSLOGIC_MAILBOX_OUTGOING_ACTION_FREE
2333 && uMailboxPosCur != pBusLogic->uMailboxOutgoingPositionCurrent);
2334 }
2335 else
2336 {
2337 /* Fetch mailbox from guest memory. */
2338 GCPhysAddrMailboxCurrent = buslogicOutgoingMailboxGetGCPhys(pBusLogic);
2339
2340 PDMDevHlpPhysRead(pBusLogic->CTX_SUFF(pDevIns), GCPhysAddrMailboxCurrent,
2341 &pTaskState->MailboxGuest, sizeof(Mailbox));
2342 }
2343
2344 /*
2345 * Check if the mailbox is actually loaded.
2346 * It might be possible that the guest notified us without
2347 * a loaded mailbox. Do nothing in that case but leave a
2348 * log entry.
2349 */
2350 if (pTaskState->MailboxGuest.u.out.uActionCode == BUSLOGIC_MAILBOX_OUTGOING_ACTION_FREE)
2351 {
2352 Log(("No loaded mailbox left\n"));
2353 RTMemCacheFree(pBusLogic->hTaskCache, pTaskState);
2354 return VERR_NO_DATA;
2355 }
2356
2357 LogFlow(("Got loaded mailbox at slot %u, CCB phys %RGp\n", pBusLogic->uMailboxOutgoingPositionCurrent, pTaskState->MailboxGuest.u32PhysAddrCCB));
2358#ifdef DEBUG
2359 buslogicDumpMailboxInfo(&pTaskState->MailboxGuest, true);
2360#endif
2361
2362 /* We got the mailbox, mark it as free in the guest. */
2363 uint8_t uActionCode = BUSLOGIC_MAILBOX_OUTGOING_ACTION_FREE;
2364 PDMDevHlpPhysWrite(pBusLogic->CTX_SUFF(pDevIns), GCPhysAddrMailboxCurrent + RT_OFFSETOF(Mailbox, u.out.uActionCode), &uActionCode, sizeof(uActionCode));
2365
2366 if (pTaskState->MailboxGuest.u.out.uActionCode == BUSLOGIC_MAILBOX_OUTGOING_ACTION_START_COMMAND)
2367 rc = buslogicDeviceSCSIRequestSetup(pBusLogic, pTaskState);
2368 else if (pTaskState->MailboxGuest.u.out.uActionCode == BUSLOGIC_MAILBOX_OUTGOING_ACTION_ABORT_COMMAND)
2369 {
2370 AssertMsgFailed(("Not implemented yet\n"));
2371 }
2372 else
2373 AssertMsgFailed(("Invalid outgoing mailbox action code %u\n", pTaskState->MailboxGuest.u.out.uActionCode));
2374
2375 AssertRC(rc);
2376
2377 /* Advance to the next mailbox. */
2378 if (pBusLogic->fStrictRoundRobinMode)
2379 buslogicOutgoingMailboxAdvance(pBusLogic);
2380
2381 return rc;
2382}
2383
2384/**
2385 * Transmit queue consumer
2386 * Queue a new async task.
2387 *
2388 * @returns Success indicator.
2389 * If false the item will not be removed and the flushing will stop.
2390 * @param pDevIns The device instance.
2391 * @param pItem The item to consume. Upon return this item will be freed.
2392 */
2393static DECLCALLBACK(bool) buslogicNotifyQueueConsumer(PPDMDEVINS pDevIns, PPDMQUEUEITEMCORE pItem)
2394{
2395 PBUSLOGIC pBusLogic = PDMINS_2_DATA(pDevIns, PBUSLOGIC);
2396
2397 /* Reset notification send flag now. */
2398 Assert(pBusLogic->fNotificationSend);
2399 ASMAtomicXchgBool(&pBusLogic->fNotificationSend, false);
2400 ASMAtomicXchgU32(&pBusLogic->cMailboxesReady, 0); /* @todo: Actually not required anymore but to stay compatible with older saved states. */
2401
2402 /* Process mailboxes. */
2403 int rc;
2404 do
2405 {
2406 rc = buslogicProcessMailboxNext(pBusLogic);
2407 AssertMsg(RT_SUCCESS(rc) || rc == VERR_NO_DATA, ("Processing mailbox failed rc=%Rrc\n", rc));
2408 } while (RT_SUCCESS(rc));
2409
2410 return true;
2411}
2412
2413/**
2414 * Kicks the controller to process pending tasks after the VM was resumed
2415 * or loaded from a saved state.
2416 *
2417 * @returns nothing.
2418 * @param pThis The BusLogic device instance.
2419 */
2420static void buslogicKick(PBUSLOGIC pThis)
2421{
2422 if (pThis->fRedo)
2423 {
2424 pThis->fRedo = false;
2425 if (pThis->VBoxSCSI.fBusy)
2426 {
2427
2428 /* The BIOS had a request active when we got suspended. Resume it. */
2429 int rc = buslogicPrepareBIOSSCSIRequest(pThis);
2430 AssertRC(rc);
2431 }
2432 else
2433 {
2434 /* Queue all pending tasks again. */
2435 PBUSLOGICTASKSTATE pTaskState = pThis->pTasksRedoHead;
2436
2437 pThis->pTasksRedoHead = NULL;
2438
2439 while (pTaskState)
2440 {
2441 PBUSLOGICTASKSTATE pCur = pTaskState;
2442
2443 int rc = buslogicDeviceSCSIRequestSetup(pThis, pCur);
2444 AssertRC(rc);
2445
2446 pTaskState = pTaskState->pRedoNext;
2447 }
2448 }
2449 }
2450}
2451
2452static DECLCALLBACK(int) buslogicLiveExec(PPDMDEVINS pDevIns, PSSMHANDLE pSSM, uint32_t uPass)
2453{
2454 PBUSLOGIC pThis = PDMINS_2_DATA(pDevIns, PBUSLOGIC);
2455
2456 /* Save the device config. */
2457 for (unsigned i = 0; i < RT_ELEMENTS(pThis->aDeviceStates); i++)
2458 SSMR3PutBool(pSSM, pThis->aDeviceStates[i].fPresent);
2459
2460 return VINF_SSM_DONT_CALL_AGAIN;
2461}
2462
2463static DECLCALLBACK(int) buslogicSaveExec(PPDMDEVINS pDevIns, PSSMHANDLE pSSM)
2464{
2465 PBUSLOGIC pBusLogic = PDMINS_2_DATA(pDevIns, PBUSLOGIC);
2466
2467 /* Every device first. */
2468 for (unsigned i = 0; i < RT_ELEMENTS(pBusLogic->aDeviceStates); i++)
2469 {
2470 PBUSLOGICDEVICE pDevice = &pBusLogic->aDeviceStates[i];
2471
2472 AssertMsg(!pDevice->cOutstandingRequests,
2473 ("There are still outstanding requests on this device\n"));
2474 SSMR3PutBool(pSSM, pDevice->fPresent);
2475 SSMR3PutU32(pSSM, pDevice->cOutstandingRequests);
2476 }
2477 /* Now the main device state. */
2478 SSMR3PutU8 (pSSM, pBusLogic->regStatus);
2479 SSMR3PutU8 (pSSM, pBusLogic->regInterrupt);
2480 SSMR3PutU8 (pSSM, pBusLogic->regGeometry);
2481 SSMR3PutMem (pSSM, &pBusLogic->LocalRam, sizeof(pBusLogic->LocalRam));
2482 SSMR3PutU8 (pSSM, pBusLogic->uOperationCode);
2483 SSMR3PutMem (pSSM, &pBusLogic->aCommandBuffer, sizeof(pBusLogic->aCommandBuffer));
2484 SSMR3PutU8 (pSSM, pBusLogic->iParameter);
2485 SSMR3PutU8 (pSSM, pBusLogic->cbCommandParametersLeft);
2486 SSMR3PutBool (pSSM, pBusLogic->fUseLocalRam);
2487 SSMR3PutMem (pSSM, pBusLogic->aReplyBuffer, sizeof(pBusLogic->aReplyBuffer));
2488 SSMR3PutU8 (pSSM, pBusLogic->iReply);
2489 SSMR3PutU8 (pSSM, pBusLogic->cbReplyParametersLeft);
2490 SSMR3PutBool (pSSM, pBusLogic->fIRQEnabled);
2491 SSMR3PutBool (pSSM, pBusLogic->fISAEnabled);
2492 SSMR3PutU32 (pSSM, pBusLogic->cMailbox);
2493 SSMR3PutGCPhys(pSSM, pBusLogic->GCPhysAddrMailboxOutgoingBase);
2494 SSMR3PutU32 (pSSM, pBusLogic->uMailboxOutgoingPositionCurrent);
2495 SSMR3PutU32 (pSSM, pBusLogic->cMailboxesReady);
2496 SSMR3PutBool (pSSM, pBusLogic->fNotificationSend);
2497 SSMR3PutGCPhys(pSSM, pBusLogic->GCPhysAddrMailboxIncomingBase);
2498 SSMR3PutU32 (pSSM, pBusLogic->uMailboxIncomingPositionCurrent);
2499 SSMR3PutBool (pSSM, pBusLogic->fStrictRoundRobinMode);
2500 SSMR3PutBool (pSSM, pBusLogic->fExtendedLunCCBFormat);
2501 /* Now the data for the BIOS interface. */
2502 SSMR3PutU8 (pSSM, pBusLogic->VBoxSCSI.regIdentify);
2503 SSMR3PutU8 (pSSM, pBusLogic->VBoxSCSI.uTargetDevice);
2504 SSMR3PutU8 (pSSM, pBusLogic->VBoxSCSI.uTxDir);
2505 SSMR3PutU8 (pSSM, pBusLogic->VBoxSCSI.cbCDB);
2506 SSMR3PutMem (pSSM, pBusLogic->VBoxSCSI.aCDB, sizeof(pBusLogic->VBoxSCSI.aCDB));
2507 SSMR3PutU8 (pSSM, pBusLogic->VBoxSCSI.iCDB);
2508 SSMR3PutU32 (pSSM, pBusLogic->VBoxSCSI.cbBuf);
2509 SSMR3PutU32 (pSSM, pBusLogic->VBoxSCSI.iBuf);
2510 SSMR3PutBool (pSSM, pBusLogic->VBoxSCSI.fBusy);
2511 SSMR3PutU8 (pSSM, pBusLogic->VBoxSCSI.enmState);
2512 if (pBusLogic->VBoxSCSI.cbBuf)
2513 SSMR3PutMem(pSSM, pBusLogic->VBoxSCSI.pBuf, pBusLogic->VBoxSCSI.cbBuf);
2514
2515 /*
2516 * Save the physical addresses of the command control blocks of still pending tasks.
2517 * They are processed again on resume.
2518 *
2519 * The number of pending tasks needs to be determined first.
2520 */
2521 uint32_t cTasks = 0;
2522
2523 PBUSLOGICTASKSTATE pTaskState = pBusLogic->pTasksRedoHead;
2524 if (pBusLogic->fRedo)
2525 {
2526 while (pTaskState)
2527 {
2528 cTasks++;
2529 pTaskState = pTaskState->pRedoNext;
2530 }
2531 }
2532 SSMR3PutU32(pSSM, cTasks);
2533
2534 /* Write the address of every task now. */
2535 pTaskState = pBusLogic->pTasksRedoHead;
2536 while (pTaskState)
2537 {
2538 SSMR3PutU32(pSSM, pTaskState->MailboxGuest.u32PhysAddrCCB);
2539 pTaskState = pTaskState->pRedoNext;
2540 }
2541
2542 return SSMR3PutU32(pSSM, ~0);
2543}
2544
2545static DECLCALLBACK(int) buslogicLoadDone(PPDMDEVINS pDevIns, PSSMHANDLE pSSM)
2546{
2547 PBUSLOGIC pThis = PDMINS_2_DATA(pDevIns, PBUSLOGIC);
2548
2549 buslogicKick(pThis);
2550 return VINF_SUCCESS;
2551}
2552
2553static DECLCALLBACK(int) buslogicLoadExec(PPDMDEVINS pDevIns, PSSMHANDLE pSSM, uint32_t uVersion, uint32_t uPass)
2554{
2555 PBUSLOGIC pBusLogic = PDMINS_2_DATA(pDevIns, PBUSLOGIC);
2556 int rc = VINF_SUCCESS;
2557
2558 /* We support saved states only from this and older versions. */
2559 if (uVersion > BUSLOGIC_SAVED_STATE_MINOR_VERSION)
2560 return VERR_SSM_UNSUPPORTED_DATA_UNIT_VERSION;
2561
2562 /* Every device first. */
2563 for (unsigned i = 0; i < RT_ELEMENTS(pBusLogic->aDeviceStates); i++)
2564 {
2565 PBUSLOGICDEVICE pDevice = &pBusLogic->aDeviceStates[i];
2566
2567 AssertMsg(!pDevice->cOutstandingRequests,
2568 ("There are still outstanding requests on this device\n"));
2569 bool fPresent;
2570 rc = SSMR3GetBool(pSSM, &fPresent);
2571 AssertRCReturn(rc, rc);
2572 if (pDevice->fPresent != fPresent)
2573 return SSMR3SetCfgError(pSSM, RT_SRC_POS, N_("Target %u config mismatch: config=%RTbool state=%RTbool"), i, pDevice->fPresent, fPresent);
2574
2575 if (uPass == SSM_PASS_FINAL)
2576 SSMR3GetU32(pSSM, (uint32_t *)&pDevice->cOutstandingRequests);
2577 }
2578
2579 if (uPass != SSM_PASS_FINAL)
2580 return VINF_SUCCESS;
2581
2582 /* Now the main device state. */
2583 SSMR3GetU8 (pSSM, (uint8_t *)&pBusLogic->regStatus);
2584 SSMR3GetU8 (pSSM, (uint8_t *)&pBusLogic->regInterrupt);
2585 SSMR3GetU8 (pSSM, (uint8_t *)&pBusLogic->regGeometry);
2586 SSMR3GetMem (pSSM, &pBusLogic->LocalRam, sizeof(pBusLogic->LocalRam));
2587 SSMR3GetU8 (pSSM, &pBusLogic->uOperationCode);
2588 SSMR3GetMem (pSSM, &pBusLogic->aCommandBuffer, sizeof(pBusLogic->aCommandBuffer));
2589 SSMR3GetU8 (pSSM, &pBusLogic->iParameter);
2590 SSMR3GetU8 (pSSM, &pBusLogic->cbCommandParametersLeft);
2591 SSMR3GetBool (pSSM, &pBusLogic->fUseLocalRam);
2592 SSMR3GetMem (pSSM, pBusLogic->aReplyBuffer, sizeof(pBusLogic->aReplyBuffer));
2593 SSMR3GetU8 (pSSM, &pBusLogic->iReply);
2594 SSMR3GetU8 (pSSM, &pBusLogic->cbReplyParametersLeft);
2595 SSMR3GetBool (pSSM, &pBusLogic->fIRQEnabled);
2596 SSMR3GetBool (pSSM, &pBusLogic->fISAEnabled);
2597 SSMR3GetU32 (pSSM, &pBusLogic->cMailbox);
2598 SSMR3GetGCPhys(pSSM, &pBusLogic->GCPhysAddrMailboxOutgoingBase);
2599 SSMR3GetU32 (pSSM, &pBusLogic->uMailboxOutgoingPositionCurrent);
2600 SSMR3GetU32 (pSSM, (uint32_t *)&pBusLogic->cMailboxesReady);
2601 SSMR3GetBool (pSSM, (bool *)&pBusLogic->fNotificationSend);
2602 SSMR3GetGCPhys(pSSM, &pBusLogic->GCPhysAddrMailboxIncomingBase);
2603 SSMR3GetU32 (pSSM, &pBusLogic->uMailboxIncomingPositionCurrent);
2604 SSMR3GetBool (pSSM, &pBusLogic->fStrictRoundRobinMode);
2605 SSMR3GetBool (pSSM, &pBusLogic->fExtendedLunCCBFormat);
2606 /* Now the data for the BIOS interface. */
2607 SSMR3GetU8 (pSSM, &pBusLogic->VBoxSCSI.regIdentify);
2608 SSMR3GetU8 (pSSM, &pBusLogic->VBoxSCSI.uTargetDevice);
2609 SSMR3GetU8 (pSSM, &pBusLogic->VBoxSCSI.uTxDir);
2610 SSMR3GetU8 (pSSM, &pBusLogic->VBoxSCSI.cbCDB);
2611 SSMR3GetMem (pSSM, pBusLogic->VBoxSCSI.aCDB, sizeof(pBusLogic->VBoxSCSI.aCDB));
2612 SSMR3GetU8 (pSSM, &pBusLogic->VBoxSCSI.iCDB);
2613 SSMR3GetU32 (pSSM, &pBusLogic->VBoxSCSI.cbBuf);
2614 SSMR3GetU32 (pSSM, &pBusLogic->VBoxSCSI.iBuf);
2615 SSMR3GetBool(pSSM, (bool *)&pBusLogic->VBoxSCSI.fBusy);
2616 SSMR3GetU8 (pSSM, (uint8_t *)&pBusLogic->VBoxSCSI.enmState);
2617 if (pBusLogic->VBoxSCSI.cbBuf)
2618 {
2619 pBusLogic->VBoxSCSI.pBuf = (uint8_t *)RTMemAllocZ(pBusLogic->VBoxSCSI.cbBuf);
2620 if (!pBusLogic->VBoxSCSI.pBuf)
2621 {
2622 LogRel(("BusLogic: Out of memory during restore.\n"));
2623 return PDMDEV_SET_ERROR(pDevIns, VERR_NO_MEMORY,
2624 N_("BusLogic: Out of memory during restore\n"));
2625 }
2626 SSMR3GetMem(pSSM, pBusLogic->VBoxSCSI.pBuf, pBusLogic->VBoxSCSI.cbBuf);
2627 }
2628
2629 if (pBusLogic->VBoxSCSI.fBusy)
2630 pBusLogic->fRedo = true;
2631
2632 if (uVersion > BUSLOGIC_SAVED_STATE_MINOR_PRE_ERROR_HANDLING)
2633 {
2634 /* Check if there are pending tasks saved. */
2635 uint32_t cTasks = 0;
2636
2637 SSMR3GetU32(pSSM, &cTasks);
2638
2639 if (cTasks)
2640 pBusLogic->fRedo = true;
2641
2642 for (uint32_t i = 0; i < cTasks; i++)
2643 {
2644 PBUSLOGICTASKSTATE pTaskState = (PBUSLOGICTASKSTATE)RTMemCacheAlloc(pBusLogic->hTaskCache);
2645 if (!pTaskState)
2646 {
2647 rc = VERR_NO_MEMORY;
2648 break;
2649 }
2650
2651 rc = SSMR3GetU32(pSSM, &pTaskState->MailboxGuest.u32PhysAddrCCB);
2652 if (RT_FAILURE(rc))
2653 {
2654 RTMemCacheFree(pBusLogic->hTaskCache, pTaskState);
2655 break;
2656 }
2657
2658 /* Link into the list. */
2659 pTaskState->pRedoNext = pBusLogic->pTasksRedoHead;
2660 pBusLogic->pTasksRedoHead = pTaskState;
2661 }
2662 }
2663
2664 if (RT_SUCCESS(rc))
2665 {
2666 uint32_t u32;
2667 rc = SSMR3GetU32(pSSM, &u32);
2668 if (RT_SUCCESS(rc))
2669 AssertMsgReturn(u32 == ~0U, ("%#x\n", u32), VERR_SSM_DATA_UNIT_FORMAT_CHANGED);
2670 }
2671
2672 return rc;
2673}
2674
2675/**
2676 * Gets the pointer to the status LED of a device - called from the SCSi driver.
2677 *
2678 * @returns VBox status code.
2679 * @param pInterface Pointer to the interface structure containing the called function pointer.
2680 * @param iLUN The unit which status LED we desire. Always 0 here as the driver
2681 * doesn't know about other LUN's.
2682 * @param ppLed Where to store the LED pointer.
2683 */
2684static DECLCALLBACK(int) buslogicDeviceQueryStatusLed(PPDMILEDPORTS pInterface, unsigned iLUN, PPDMLED *ppLed)
2685{
2686 PBUSLOGICDEVICE pDevice = PDMILEDPORTS_2_PBUSLOGICDEVICE(pInterface);
2687 if (iLUN == 0)
2688 {
2689 *ppLed = &pDevice->Led;
2690 Assert((*ppLed)->u32Magic == PDMLED_MAGIC);
2691 return VINF_SUCCESS;
2692 }
2693 return VERR_PDM_LUN_NOT_FOUND;
2694}
2695
2696/**
2697 * @interface_method_impl{PDMIBASE,pfnQueryInterface}
2698 */
2699static DECLCALLBACK(void *) buslogicDeviceQueryInterface(PPDMIBASE pInterface, const char *pszIID)
2700{
2701 PBUSLOGICDEVICE pDevice = PDMIBASE_2_PBUSLOGICDEVICE(pInterface);
2702 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIBASE, &pDevice->IBase);
2703 PDMIBASE_RETURN_INTERFACE(pszIID, PDMISCSIPORT, &pDevice->ISCSIPort);
2704 PDMIBASE_RETURN_INTERFACE(pszIID, PDMILEDPORTS, &pDevice->ILed);
2705 return NULL;
2706}
2707
2708/**
2709 * Gets the pointer to the status LED of a unit.
2710 *
2711 * @returns VBox status code.
2712 * @param pInterface Pointer to the interface structure containing the called function pointer.
2713 * @param iLUN The unit which status LED we desire.
2714 * @param ppLed Where to store the LED pointer.
2715 */
2716static DECLCALLBACK(int) buslogicStatusQueryStatusLed(PPDMILEDPORTS pInterface, unsigned iLUN, PPDMLED *ppLed)
2717{
2718 PBUSLOGIC pBusLogic = PDMILEDPORTS_2_PBUSLOGIC(pInterface);
2719 if (iLUN < BUSLOGIC_MAX_DEVICES)
2720 {
2721 *ppLed = &pBusLogic->aDeviceStates[iLUN].Led;
2722 Assert((*ppLed)->u32Magic == PDMLED_MAGIC);
2723 return VINF_SUCCESS;
2724 }
2725 return VERR_PDM_LUN_NOT_FOUND;
2726}
2727
2728/**
2729 * @interface_method_impl{PDMIBASE,pfnQueryInterface}
2730 */
2731static DECLCALLBACK(void *) buslogicStatusQueryInterface(PPDMIBASE pInterface, const char *pszIID)
2732{
2733 PBUSLOGIC pThis = PDMIBASE_2_PBUSLOGIC(pInterface);
2734 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIBASE, &pThis->IBase);
2735 PDMIBASE_RETURN_INTERFACE(pszIID, PDMILEDPORTS, &pThis->ILeds);
2736 return NULL;
2737}
2738
2739/* -=-=-=-=- Helper -=-=-=-=- */
2740
2741 /**
2742 * Checks if all asynchronous I/O is finished.
2743 *
2744 * Used by buslogicReset, buslogicSuspend and buslogicPowerOff.
2745 *
2746 * @returns true if quiesced, false if busy.
2747 * @param pDevIns The device instance.
2748 */
2749static bool buslogicR3AllAsyncIOIsFinished(PPDMDEVINS pDevIns)
2750{
2751 PBUSLOGIC pThis = PDMINS_2_DATA(pDevIns, PBUSLOGIC);
2752
2753 for (uint32_t i = 0; i < RT_ELEMENTS(pThis->aDeviceStates); i++)
2754 {
2755 PBUSLOGICDEVICE pThisDevice = &pThis->aDeviceStates[i];
2756 if (pThisDevice->pDrvBase)
2757 {
2758 if (pThisDevice->cOutstandingRequests != 0)
2759 return false;
2760 }
2761 }
2762
2763 return true;
2764}
2765
2766/**
2767 * Callback employed by buslogicR3Suspend and buslogicR3PowerOff..
2768 *
2769 * @returns true if we've quiesced, false if we're still working.
2770 * @param pDevIns The device instance.
2771 */
2772static DECLCALLBACK(bool) buslogicR3IsAsyncSuspendOrPowerOffDone(PPDMDEVINS pDevIns)
2773{
2774 if (!buslogicR3AllAsyncIOIsFinished(pDevIns))
2775 return false;
2776
2777 PBUSLOGIC pThis = PDMINS_2_DATA(pDevIns, PBUSLOGIC);
2778 ASMAtomicWriteBool(&pThis->fSignalIdle, false);
2779 return true;
2780}
2781
2782/**
2783 * Common worker for ahciR3Suspend and ahciR3PowerOff.
2784 */
2785static void buslogicR3SuspendOrPowerOff(PPDMDEVINS pDevIns, bool fPowerOff)
2786{
2787 PBUSLOGIC pThis = PDMINS_2_DATA(pDevIns, PBUSLOGIC);
2788
2789 ASMAtomicWriteBool(&pThis->fSignalIdle, true);
2790 if (!buslogicR3AllAsyncIOIsFinished(pDevIns))
2791 PDMDevHlpSetAsyncNotification(pDevIns, buslogicR3IsAsyncSuspendOrPowerOffDone);
2792 else
2793 {
2794 ASMAtomicWriteBool(&pThis->fSignalIdle, false);
2795
2796 AssertMsg(!pThis->fNotificationSend, ("The PDM Queue should be empty at this point\n"));
2797
2798 if (pThis->fRedo)
2799 {
2800 if (fPowerOff)
2801 {
2802 /* Free tasks which would have been queued again on resume. */
2803 PBUSLOGICTASKSTATE pTaskState = pThis->pTasksRedoHead;
2804
2805 pThis->pTasksRedoHead = NULL;
2806
2807 while (pTaskState)
2808 {
2809 PBUSLOGICTASKSTATE pFree;
2810
2811 pFree = pTaskState;
2812 pTaskState = pTaskState->pRedoNext;
2813
2814 RTMemCacheFree(pThis->hTaskCache, pFree);
2815 }
2816 pThis->fRedo = false;
2817 }
2818 else if (pThis->VBoxSCSI.fBusy)
2819 {
2820 /* Destroy the task because the BIOS interface has all necessary information. */
2821 Assert(pThis->pTasksRedoHead->fBIOS);
2822 Assert(!pThis->pTasksRedoHead->pRedoNext);
2823
2824 RTMemCacheFree(pThis->hTaskCache, pThis->pTasksRedoHead);
2825 pThis->pTasksRedoHead = NULL;
2826 }
2827 }
2828 }
2829}
2830
2831/**
2832 * Suspend notification.
2833 *
2834 * @param pDevIns The device instance data.
2835 */
2836static DECLCALLBACK(void) buslogicSuspend(PPDMDEVINS pDevIns)
2837{
2838 Log(("buslogicSuspend\n"));
2839 buslogicR3SuspendOrPowerOff(pDevIns, false /* fPoweroff */);
2840}
2841
2842/**
2843 * Resume notification.
2844 *
2845 * @param pDevIns The device instance data.
2846 */
2847static DECLCALLBACK(void) buslogicResume(PPDMDEVINS pDevIns)
2848{
2849 Log(("buslogicResume\n"));
2850 PBUSLOGIC pThis = PDMINS_2_DATA(pDevIns, PBUSLOGIC);
2851 buslogicKick(pThis);
2852}
2853
2854
2855/**
2856 * Detach notification.
2857 *
2858 * One harddisk at one port has been unplugged.
2859 * The VM is suspended at this point.
2860 *
2861 * @param pDevIns The device instance.
2862 * @param iLUN The logical unit which is being detached.
2863 * @param fFlags Flags, combination of the PDMDEVATT_FLAGS_* \#defines.
2864 */
2865static DECLCALLBACK(void) buslogicDetach(PPDMDEVINS pDevIns, unsigned iLUN, uint32_t fFlags)
2866{
2867 PBUSLOGIC pThis = PDMINS_2_DATA(pDevIns, PBUSLOGIC);
2868 PBUSLOGICDEVICE pDevice = &pThis->aDeviceStates[iLUN];
2869
2870 Log(("%s:\n", __FUNCTION__));
2871
2872 AssertMsg(fFlags & PDM_TACH_FLAGS_NOT_HOT_PLUG,
2873 ("BusLogic: Device does not support hotplugging\n"));
2874
2875 /*
2876 * Zero some important members.
2877 */
2878 pDevice->pDrvBase = NULL;
2879 pDevice->fPresent = false;
2880 pDevice->pDrvSCSIConnector = NULL;
2881}
2882
2883/**
2884 * Attach command.
2885 *
2886 * This is called when we change block driver.
2887 *
2888 * @returns VBox status code.
2889 * @param pDevIns The device instance.
2890 * @param iLUN The logical unit which is being detached.
2891 * @param fFlags Flags, combination of the PDMDEVATT_FLAGS_* \#defines.
2892 */
2893static DECLCALLBACK(int) buslogicAttach(PPDMDEVINS pDevIns, unsigned iLUN, uint32_t fFlags)
2894{
2895 PBUSLOGIC pThis = PDMINS_2_DATA(pDevIns, PBUSLOGIC);
2896 PBUSLOGICDEVICE pDevice = &pThis->aDeviceStates[iLUN];
2897 int rc;
2898
2899 AssertMsgReturn(fFlags & PDM_TACH_FLAGS_NOT_HOT_PLUG,
2900 ("BusLogic: Device does not support hotplugging\n"),
2901 VERR_INVALID_PARAMETER);
2902
2903 /* the usual paranoia */
2904 AssertRelease(!pDevice->pDrvBase);
2905 AssertRelease(!pDevice->pDrvSCSIConnector);
2906 Assert(pDevice->iLUN == iLUN);
2907
2908 /*
2909 * Try attach the block device and get the interfaces,
2910 * required as well as optional.
2911 */
2912 rc = PDMDevHlpDriverAttach(pDevIns, pDevice->iLUN, &pDevice->IBase, &pDevice->pDrvBase, NULL);
2913 if (RT_SUCCESS(rc))
2914 {
2915 /* Get SCSI connector interface. */
2916 pDevice->pDrvSCSIConnector = PDMIBASE_QUERY_INTERFACE(pDevice->pDrvBase, PDMISCSICONNECTOR);
2917 AssertMsgReturn(pDevice->pDrvSCSIConnector, ("Missing SCSI interface below\n"), VERR_PDM_MISSING_INTERFACE);
2918 pDevice->fPresent = true;
2919 }
2920 else
2921 AssertMsgFailed(("Failed to attach LUN#%d. rc=%Rrc\n", pDevice->iLUN, rc));
2922
2923 if (RT_FAILURE(rc))
2924 {
2925 pDevice->pDrvBase = NULL;
2926 pDevice->pDrvSCSIConnector = NULL;
2927 }
2928 return rc;
2929}
2930
2931/**
2932 * Callback employed by buslogicR3Reset.
2933 *
2934 * @returns true if we've quiesced, false if we're still working.
2935 * @param pDevIns The device instance.
2936 */
2937static DECLCALLBACK(bool) buslogicR3IsAsyncResetDone(PPDMDEVINS pDevIns)
2938{
2939 PBUSLOGIC pThis = PDMINS_2_DATA(pDevIns, PBUSLOGIC);
2940
2941 if (!buslogicR3AllAsyncIOIsFinished(pDevIns))
2942 return false;
2943 ASMAtomicWriteBool(&pThis->fSignalIdle, false);
2944
2945 buslogicHwReset(pThis);
2946 return true;
2947}
2948
2949/**
2950 * @copydoc FNPDMDEVRESET
2951 */
2952static DECLCALLBACK(void) buslogicReset(PPDMDEVINS pDevIns)
2953{
2954 PBUSLOGIC pThis = PDMINS_2_DATA(pDevIns, PBUSLOGIC);
2955
2956 ASMAtomicWriteBool(&pThis->fSignalIdle, true);
2957 if (!buslogicR3AllAsyncIOIsFinished(pDevIns))
2958 PDMDevHlpSetAsyncNotification(pDevIns, buslogicR3IsAsyncResetDone);
2959 else
2960 {
2961 ASMAtomicWriteBool(&pThis->fSignalIdle, false);
2962 buslogicHwReset(pThis);
2963 }
2964}
2965
2966static DECLCALLBACK(void) buslogicRelocate(PPDMDEVINS pDevIns, RTGCINTPTR offDelta)
2967{
2968 uint32_t i;
2969 PBUSLOGIC pBusLogic = PDMINS_2_DATA(pDevIns, PBUSLOGIC);
2970
2971 pBusLogic->pDevInsRC = PDMDEVINS_2_RCPTR(pDevIns);
2972 pBusLogic->pNotifierQueueRC = PDMQueueRCPtr(pBusLogic->pNotifierQueueR3);
2973
2974 for (i = 0; i < BUSLOGIC_MAX_DEVICES; i++)
2975 {
2976 PBUSLOGICDEVICE pDevice = &pBusLogic->aDeviceStates[i];
2977
2978 pDevice->pBusLogicRC = PDMINS_2_DATA_RCPTR(pDevIns);
2979 }
2980
2981}
2982
2983/**
2984 * Poweroff notification.
2985 *
2986 * @param pDevIns Pointer to the device instance
2987 */
2988static DECLCALLBACK(void) buslogicPowerOff(PPDMDEVINS pDevIns)
2989{
2990 Log(("buslogicPowerOff\n"));
2991 buslogicR3SuspendOrPowerOff(pDevIns, true /* fPoweroff */);
2992}
2993
2994/**
2995 * Destroy a driver instance.
2996 *
2997 * Most VM resources are freed by the VM. This callback is provided so that any non-VM
2998 * resources can be freed correctly.
2999 *
3000 * @param pDevIns The device instance data.
3001 */
3002static DECLCALLBACK(int) buslogicDestruct(PPDMDEVINS pDevIns)
3003{
3004 PBUSLOGIC pThis = PDMINS_2_DATA(pDevIns, PBUSLOGIC);
3005 PDMDEV_CHECK_VERSIONS_RETURN_QUIET(pDevIns);
3006
3007 PDMR3CritSectDelete(&pThis->CritSectIntr);
3008
3009 /*
3010 * Free all tasks which are still hanging around
3011 * (Power off after the VM was suspended).
3012 */
3013 if (pThis->fRedo)
3014 {
3015 /* Free tasks which would have been queued again on resume. */
3016 PBUSLOGICTASKSTATE pTaskState = pThis->pTasksRedoHead;
3017
3018 pThis->pTasksRedoHead = NULL;
3019
3020 while (pTaskState)
3021 {
3022 PBUSLOGICTASKSTATE pFree;
3023
3024 pFree = pTaskState;
3025 pTaskState = pTaskState->pRedoNext;
3026
3027 RTMemCacheFree(pThis->hTaskCache, pFree);
3028 }
3029 pThis->fRedo = false;
3030 }
3031
3032 int rc = RTMemCacheDestroy(pThis->hTaskCache);
3033 AssertMsgRC(rc, ("Destroying task cache failed rc=%Rrc\n", rc));
3034
3035 return rc;
3036}
3037
3038/**
3039 * @interface_method_impl{PDMDEVREG,pfnConstruct}
3040 */
3041static DECLCALLBACK(int) buslogicConstruct(PPDMDEVINS pDevIns, int iInstance, PCFGMNODE pCfg)
3042{
3043 PBUSLOGIC pThis = PDMINS_2_DATA(pDevIns, PBUSLOGIC);
3044 int rc = VINF_SUCCESS;
3045 bool fBootable = true;
3046 PDMDEV_CHECK_VERSIONS_RETURN(pDevIns);
3047
3048 /*
3049 * Validate and read configuration.
3050 */
3051 if (!CFGMR3AreValuesValid(pCfg,
3052 "GCEnabled\0"
3053 "R0Enabled\0"
3054 "Bootable\0"))
3055 return PDMDEV_SET_ERROR(pDevIns, VERR_PDM_DEVINS_UNKNOWN_CFG_VALUES,
3056 N_("BusLogic configuration error: unknown option specified"));
3057
3058 rc = CFGMR3QueryBoolDef(pCfg, "GCEnabled", &pThis->fGCEnabled, true);
3059 if (RT_FAILURE(rc))
3060 return PDMDEV_SET_ERROR(pDevIns, rc,
3061 N_("BusLogic configuration error: failed to read GCEnabled as boolean"));
3062 Log(("%s: fGCEnabled=%d\n", __FUNCTION__, pThis->fGCEnabled));
3063
3064 rc = CFGMR3QueryBoolDef(pCfg, "R0Enabled", &pThis->fR0Enabled, true);
3065 if (RT_FAILURE(rc))
3066 return PDMDEV_SET_ERROR(pDevIns, rc,
3067 N_("BusLogic configuration error: failed to read R0Enabled as boolean"));
3068 Log(("%s: fR0Enabled=%d\n", __FUNCTION__, pThis->fR0Enabled));
3069 rc = CFGMR3QueryBoolDef(pCfg, "Bootable", &fBootable, true);
3070 if (RT_FAILURE(rc))
3071 return PDMDEV_SET_ERROR(pDevIns, rc,
3072 N_("BusLogic configuration error: failed to read Bootable as boolean"));
3073 Log(("%s: fBootable=%RTbool\n", __FUNCTION__, fBootable));
3074
3075 pThis->pDevInsR3 = pDevIns;
3076 pThis->pDevInsR0 = PDMDEVINS_2_R0PTR(pDevIns);
3077 pThis->pDevInsRC = PDMDEVINS_2_RCPTR(pDevIns);
3078 pThis->IBase.pfnQueryInterface = buslogicStatusQueryInterface;
3079 pThis->ILeds.pfnQueryStatusLed = buslogicStatusQueryStatusLed;
3080
3081 PCIDevSetVendorId (&pThis->dev, 0x104b); /* BusLogic */
3082 PCIDevSetDeviceId (&pThis->dev, 0x1040); /* BT-958 */
3083 PCIDevSetCommand (&pThis->dev, 0x0003);
3084 PCIDevSetRevisionId (&pThis->dev, 0x01);
3085 PCIDevSetClassProg (&pThis->dev, 0x00); /* SCSI */
3086 PCIDevSetClassSub (&pThis->dev, 0x00); /* SCSI */
3087 PCIDevSetClassBase (&pThis->dev, 0x01); /* Mass storage */
3088 PCIDevSetBaseAddress (&pThis->dev, 0, true /*IO*/, false /*Pref*/, false /*64-bit*/, 0x00000000);
3089 PCIDevSetBaseAddress (&pThis->dev, 1, false /*IO*/, false /*Pref*/, false /*64-bit*/, 0x00000000);
3090 PCIDevSetSubSystemVendorId(&pThis->dev, 0x104b);
3091 PCIDevSetSubSystemId (&pThis->dev, 0x1040);
3092 PCIDevSetInterruptLine (&pThis->dev, 0x00);
3093 PCIDevSetInterruptPin (&pThis->dev, 0x01);
3094
3095 /*
3096 * Register the PCI device, it's I/O regions.
3097 */
3098 rc = PDMDevHlpPCIRegister (pDevIns, &pThis->dev);
3099 if (RT_FAILURE(rc))
3100 return rc;
3101
3102 rc = PDMDevHlpPCIIORegionRegister(pDevIns, 0, 32, PCI_ADDRESS_SPACE_IO, buslogicMMIOMap);
3103 if (RT_FAILURE(rc))
3104 return rc;
3105
3106 rc = PDMDevHlpPCIIORegionRegister(pDevIns, 1, 32, PCI_ADDRESS_SPACE_MEM, buslogicMMIOMap);
3107 if (RT_FAILURE(rc))
3108 return rc;
3109
3110 if (fBootable)
3111 {
3112 /* Register I/O port space in ISA region for BIOS access. */
3113 rc = PDMDevHlpIOPortRegister(pDevIns, BUSLOGIC_ISA_IO_PORT, 3, NULL,
3114 buslogicIsaIOPortWrite, buslogicIsaIOPortRead,
3115 buslogicIsaIOPortWriteStr, buslogicIsaIOPortReadStr,
3116 "BusLogic BIOS");
3117 if (RT_FAILURE(rc))
3118 return PDMDEV_SET_ERROR(pDevIns, rc, N_("BusLogic cannot register legacy I/O handlers"));
3119 }
3120
3121 /* Initialize task cache. */
3122 rc = RTMemCacheCreate(&pThis->hTaskCache, sizeof(BUSLOGICTASKSTATE), 0, UINT32_MAX,
3123 NULL, NULL, NULL, 0);
3124 if (RT_FAILURE(rc))
3125 return PDMDEV_SET_ERROR(pDevIns, rc,
3126 N_("BusLogic: Failed to initialize task cache\n"));
3127
3128 /* Initialize task queue. */
3129 rc = PDMDevHlpQueueCreate(pDevIns, sizeof(PDMQUEUEITEMCORE), 5, 0,
3130 buslogicNotifyQueueConsumer, true, "BusLogicTask", &pThis->pNotifierQueueR3);
3131 if (RT_FAILURE(rc))
3132 return rc;
3133 pThis->pNotifierQueueR0 = PDMQueueR0Ptr(pThis->pNotifierQueueR3);
3134 pThis->pNotifierQueueRC = PDMQueueRCPtr(pThis->pNotifierQueueR3);
3135
3136 rc = PDMDevHlpCritSectInit(pDevIns, &pThis->CritSectIntr, RT_SRC_POS, "BusLogic-Intr#%u", pDevIns->iInstance);
3137 if (RT_FAILURE(rc))
3138 return PDMDEV_SET_ERROR(pDevIns, rc,
3139 N_("BusLogic: cannot create critical section"));
3140
3141 /* Initialize per device state. */
3142 for (unsigned i = 0; i < RT_ELEMENTS(pThis->aDeviceStates); i++)
3143 {
3144 char szName[24];
3145 PBUSLOGICDEVICE pDevice = &pThis->aDeviceStates[i];
3146
3147 RTStrPrintf(szName, sizeof(szName), "Device%d", i);
3148
3149 /* Initialize static parts of the device. */
3150 pDevice->iLUN = i;
3151 pDevice->pBusLogicR3 = pThis;
3152 pDevice->pBusLogicR0 = PDMINS_2_DATA_R0PTR(pDevIns);
3153 pDevice->pBusLogicRC = PDMINS_2_DATA_RCPTR(pDevIns);
3154 pDevice->Led.u32Magic = PDMLED_MAGIC;
3155 pDevice->IBase.pfnQueryInterface = buslogicDeviceQueryInterface;
3156 pDevice->ISCSIPort.pfnSCSIRequestCompleted = buslogicDeviceSCSIRequestCompleted;
3157 pDevice->ISCSIPort.pfnQueryDeviceLocation = buslogicQueryDeviceLocation;
3158 pDevice->ILed.pfnQueryStatusLed = buslogicDeviceQueryStatusLed;
3159
3160 /* Attach SCSI driver. */
3161 rc = PDMDevHlpDriverAttach(pDevIns, pDevice->iLUN, &pDevice->IBase, &pDevice->pDrvBase, szName);
3162 if (RT_SUCCESS(rc))
3163 {
3164 /* Get SCSI connector interface. */
3165 pDevice->pDrvSCSIConnector = PDMIBASE_QUERY_INTERFACE(pDevice->pDrvBase, PDMISCSICONNECTOR);
3166 AssertMsgReturn(pDevice->pDrvSCSIConnector, ("Missing SCSI interface below\n"), VERR_PDM_MISSING_INTERFACE);
3167
3168 pDevice->fPresent = true;
3169 }
3170 else if (rc == VERR_PDM_NO_ATTACHED_DRIVER)
3171 {
3172 pDevice->pDrvBase = NULL;
3173 pDevice->fPresent = false;
3174 rc = VINF_SUCCESS;
3175 Log(("BusLogic: no driver attached to device %s\n", szName));
3176 }
3177 else
3178 {
3179 AssertLogRelMsgFailed(("BusLogic: Failed to attach %s\n", szName));
3180 return rc;
3181 }
3182 }
3183
3184 /*
3185 * Attach status driver (optional).
3186 */
3187 PPDMIBASE pBase;
3188 rc = PDMDevHlpDriverAttach(pDevIns, PDM_STATUS_LUN, &pThis->IBase, &pBase, "Status Port");
3189 if (RT_SUCCESS(rc))
3190 pThis->pLedsConnector = PDMIBASE_QUERY_INTERFACE(pBase, PDMILEDCONNECTORS);
3191 else if (rc != VERR_PDM_NO_ATTACHED_DRIVER)
3192 {
3193 AssertMsgFailed(("Failed to attach to status driver. rc=%Rrc\n", rc));
3194 return PDMDEV_SET_ERROR(pDevIns, rc, N_("BusLogic cannot attach to status driver"));
3195 }
3196
3197 rc = PDMDevHlpSSMRegisterEx(pDevIns, BUSLOGIC_SAVED_STATE_MINOR_VERSION, sizeof(*pThis), NULL,
3198 NULL, buslogicLiveExec, NULL,
3199 NULL, buslogicSaveExec, NULL,
3200 NULL, buslogicLoadExec, buslogicLoadDone);
3201 if (RT_FAILURE(rc))
3202 return PDMDEV_SET_ERROR(pDevIns, rc, N_("BusLogic cannot register save state handlers"));
3203
3204 rc = buslogicHwReset(pThis);
3205 AssertMsgRC(rc, ("hardware reset of BusLogic host adapter failed rc=%Rrc\n", rc));
3206
3207 return rc;
3208}
3209
3210/**
3211 * The device registration structure.
3212 */
3213const PDMDEVREG g_DeviceBusLogic =
3214{
3215 /* u32Version */
3216 PDM_DEVREG_VERSION,
3217 /* szName */
3218 "buslogic",
3219 /* szRCMod */
3220 "VBoxDDGC.gc",
3221 /* szR0Mod */
3222 "VBoxDDR0.r0",
3223 /* pszDescription */
3224 "BusLogic BT-958 SCSI host adapter.\n",
3225 /* fFlags */
3226 PDM_DEVREG_FLAGS_DEFAULT_BITS | PDM_DEVREG_FLAGS_RC | PDM_DEVREG_FLAGS_R0 |
3227 PDM_DEVREG_FLAGS_FIRST_SUSPEND_NOTIFICATION | PDM_DEVREG_FLAGS_FIRST_POWEROFF_NOTIFICATION,
3228 /* fClass */
3229 PDM_DEVREG_CLASS_STORAGE,
3230 /* cMaxInstances */
3231 ~0,
3232 /* cbInstance */
3233 sizeof(BUSLOGIC),
3234 /* pfnConstruct */
3235 buslogicConstruct,
3236 /* pfnDestruct */
3237 buslogicDestruct,
3238 /* pfnRelocate */
3239 buslogicRelocate,
3240 /* pfnIOCtl */
3241 NULL,
3242 /* pfnPowerOn */
3243 NULL,
3244 /* pfnReset */
3245 buslogicReset,
3246 /* pfnSuspend */
3247 buslogicSuspend,
3248 /* pfnResume */
3249 buslogicResume,
3250 /* pfnAttach */
3251 buslogicAttach,
3252 /* pfnDetach */
3253 buslogicDetach,
3254 /* pfnQueryInterface. */
3255 NULL,
3256 /* pfnInitComplete */
3257 NULL,
3258 /* pfnPowerOff */
3259 buslogicPowerOff,
3260 /* pfnSoftReset */
3261 NULL,
3262 /* u32VersionEnd */
3263 PDM_DEVREG_VERSION
3264};
3265
3266#endif /* IN_RING3 */
3267#endif /* !VBOX_DEVICE_STRUCT_TESTCASE */
3268
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