VirtualBox

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

Last change on this file since 34968 was 34433, checked in by vboxsync, 14 years ago

Storage: Introduce interface to query the location of a medium (device + instance + LUN) in a VM to generate IDs which are constant for saved states

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