VirtualBox

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

Last change on this file since 34339 was 34009, checked in by vboxsync, 14 years ago

Devices/Storage: Introduce Bootable CFGM key to enable BIOS access

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 121.3 KB
Line 
1/* $Id: DevBusLogic.cpp 34009 2010-11-11 20:14:36Z 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 int buslogicDeviceSCSIRequestSetup(PBUSLOGIC pBusLogic, PBUSLOGICTASKSTATE pTaskState)
2143{
2144 int rc = VINF_SUCCESS;
2145
2146 /* Fetch CCB. */
2147 RTGCPHYS GCPhysAddrCCB = (RTGCPHYS)pTaskState->MailboxGuest.u32PhysAddrCCB;
2148 PDMDevHlpPhysRead(pBusLogic->CTX_SUFF(pDevIns), GCPhysAddrCCB,
2149 &pTaskState->CommandControlBlockGuest, sizeof(CommandControlBlock));
2150
2151 PBUSLOGICDEVICE pTargetDevice = &pBusLogic->aDeviceStates[pTaskState->CommandControlBlockGuest.uTargetId];
2152 pTaskState->CTX_SUFF(pTargetDevice) = pTargetDevice;
2153
2154#ifdef DEBUG
2155 buslogicDumpCCBInfo(&pTaskState->CommandControlBlockGuest);
2156#endif
2157
2158 /* Alloc required buffers. */
2159 rc = buslogicDataBufferAlloc(pTaskState);
2160 AssertMsgRC(rc, ("Alloc failed rc=%Rrc\n", rc));
2161
2162 if (pTaskState->CommandControlBlockGuest.cbSenseData)
2163 {
2164 rc = buslogicSenseBufferAlloc(pTaskState);
2165 AssertMsgRC(rc, ("Mapping sense buffer failed rc=%Rrc\n", rc));
2166 }
2167
2168 /* Check if device is present on bus. If not return error immediately and don't process this further. */
2169 if (!pBusLogic->aDeviceStates[pTaskState->CommandControlBlockGuest.uTargetId].fPresent)
2170 {
2171 buslogicDataBufferFree(pTaskState);
2172
2173 if (pTaskState->pbSenseBuffer)
2174 buslogicSenseBufferFree(pTaskState, true);
2175
2176 buslogicSendIncomingMailbox(pBusLogic, pTaskState,
2177 BUSLOGIC_MAILBOX_INCOMING_ADAPTER_STATUS_SCSI_SELECTION_TIMEOUT,
2178 BUSLOGIC_MAILBOX_INCOMING_DEVICE_STATUS_OPERATION_GOOD,
2179 BUSLOGIC_MAILBOX_INCOMING_COMPLETION_WITH_ERROR);
2180
2181 RTMemCacheFree(pBusLogic->hTaskCache, pTaskState);
2182 }
2183 else
2184 {
2185 /* Setup SCSI request. */
2186 pTaskState->PDMScsiRequest.uLogicalUnit = pTaskState->CommandControlBlockGuest.uLogicalUnit;
2187
2188 if (pTaskState->CommandControlBlockGuest.uDataDirection == BUSLOGIC_CCB_DIRECTION_UNKNOWN)
2189 pTaskState->PDMScsiRequest.uDataDirection = PDMSCSIREQUESTTXDIR_UNKNOWN;
2190 else if (pTaskState->CommandControlBlockGuest.uDataDirection == BUSLOGIC_CCB_DIRECTION_IN)
2191 pTaskState->PDMScsiRequest.uDataDirection = PDMSCSIREQUESTTXDIR_FROM_DEVICE;
2192 else if (pTaskState->CommandControlBlockGuest.uDataDirection == BUSLOGIC_CCB_DIRECTION_OUT)
2193 pTaskState->PDMScsiRequest.uDataDirection = PDMSCSIREQUESTTXDIR_TO_DEVICE;
2194 else if (pTaskState->CommandControlBlockGuest.uDataDirection == BUSLOGIC_CCB_DIRECTION_NO_DATA)
2195 pTaskState->PDMScsiRequest.uDataDirection = PDMSCSIREQUESTTXDIR_NONE;
2196 else
2197 AssertMsgFailed(("Invalid data direction type %d\n", pTaskState->CommandControlBlockGuest.uDataDirection));
2198
2199 pTaskState->PDMScsiRequest.cbCDB = pTaskState->CommandControlBlockGuest.cbCDB;
2200 pTaskState->PDMScsiRequest.pbCDB = pTaskState->CommandControlBlockGuest.aCDB;
2201 if (pTaskState->DataSeg.cbSeg)
2202 {
2203 pTaskState->PDMScsiRequest.cbScatterGather = pTaskState->DataSeg.cbSeg;
2204 pTaskState->PDMScsiRequest.cScatterGatherEntries = 1;
2205 pTaskState->PDMScsiRequest.paScatterGatherHead = &pTaskState->DataSeg;
2206 }
2207 else
2208 {
2209 pTaskState->PDMScsiRequest.cbScatterGather = 0;
2210 pTaskState->PDMScsiRequest.cScatterGatherEntries = 0;
2211 pTaskState->PDMScsiRequest.paScatterGatherHead = NULL;
2212 }
2213 pTaskState->PDMScsiRequest.cbSenseBuffer = pTaskState->CommandControlBlockGuest.cbSenseData;
2214 pTaskState->PDMScsiRequest.pbSenseBuffer = pTaskState->pbSenseBuffer;
2215 pTaskState->PDMScsiRequest.pvUser = pTaskState;
2216
2217 ASMAtomicIncU32(&pTargetDevice->cOutstandingRequests);
2218 rc = pTargetDevice->pDrvSCSIConnector->pfnSCSIRequestSend(pTargetDevice->pDrvSCSIConnector, &pTaskState->PDMScsiRequest);
2219 AssertMsgRC(rc, ("Sending request to SCSI layer failed rc=%Rrc\n", rc));
2220 }
2221
2222 return rc;
2223}
2224
2225/**
2226 * Read mailbox from the guest and execute command.
2227 *
2228 * @returns VBox status code.
2229 * @param pBusLogic Pointer to the BusLogic instance data.
2230 */
2231static int buslogicProcessMailboxNext(PBUSLOGIC pBusLogic)
2232{
2233 PBUSLOGICTASKSTATE pTaskState = NULL;
2234 RTGCPHYS GCPhysAddrMailboxCurrent;
2235 int rc;
2236
2237 rc = RTMemCacheAllocEx(pBusLogic->hTaskCache, (void **)&pTaskState);
2238 AssertMsgReturn(RT_SUCCESS(rc) && (pTaskState != NULL), ("Failed to get task state from cache\n"), rc);
2239
2240 pTaskState->fBIOS = false;
2241
2242 if (!pBusLogic->fStrictRoundRobinMode)
2243 {
2244 /* Search for a filled mailbox. */
2245 do
2246 {
2247 /* Fetch mailbox from guest memory. */
2248 GCPhysAddrMailboxCurrent = pBusLogic->GCPhysAddrMailboxOutgoingBase + (pBusLogic->uMailboxOutgoingPositionCurrent * sizeof(Mailbox));
2249
2250 PDMDevHlpPhysRead(pBusLogic->CTX_SUFF(pDevIns), GCPhysAddrMailboxCurrent,
2251 &pTaskState->MailboxGuest, sizeof(Mailbox));
2252
2253 pBusLogic->uMailboxOutgoingPositionCurrent++;
2254
2255 /* Check if we reached the end and start from the beginning if so. */
2256 if (pBusLogic->uMailboxOutgoingPositionCurrent >= pBusLogic->cMailbox)
2257 pBusLogic->uMailboxOutgoingPositionCurrent = 0;
2258 } while (pTaskState->MailboxGuest.u.out.uActionCode == BUSLOGIC_MAILBOX_OUTGOING_ACTION_FREE);
2259 }
2260 else
2261 {
2262 /* Fetch mailbox from guest memory. */
2263 GCPhysAddrMailboxCurrent = pBusLogic->GCPhysAddrMailboxOutgoingBase + (pBusLogic->uMailboxOutgoingPositionCurrent * sizeof(Mailbox));
2264
2265 PDMDevHlpPhysRead(pBusLogic->CTX_SUFF(pDevIns), GCPhysAddrMailboxCurrent,
2266 &pTaskState->MailboxGuest, sizeof(Mailbox));
2267 }
2268
2269#ifdef DEBUG
2270 buslogicDumpMailboxInfo(&pTaskState->MailboxGuest, true);
2271#endif
2272
2273 if (pTaskState->MailboxGuest.u.out.uActionCode == BUSLOGIC_MAILBOX_OUTGOING_ACTION_START_COMMAND)
2274 rc = buslogicDeviceSCSIRequestSetup(pBusLogic, pTaskState);
2275 else if (pTaskState->MailboxGuest.u.out.uActionCode == BUSLOGIC_MAILBOX_OUTGOING_ACTION_ABORT_COMMAND)
2276 {
2277 AssertMsgFailed(("Not implemented yet\n"));
2278 }
2279 else
2280 AssertMsgFailed(("Invalid outgoing mailbox action code %u\n", pTaskState->MailboxGuest.u.out.uActionCode));
2281
2282 AssertRC(rc);
2283
2284 /* We got the mailbox, mark it as free in the guest. */
2285 pTaskState->MailboxGuest.u.out.uActionCode = BUSLOGIC_MAILBOX_OUTGOING_ACTION_FREE;
2286 PDMDevHlpPhysWrite(pBusLogic->CTX_SUFF(pDevIns), GCPhysAddrMailboxCurrent, &pTaskState->MailboxGuest, sizeof(Mailbox));
2287
2288 if (pBusLogic->fStrictRoundRobinMode)
2289 {
2290 pBusLogic->uMailboxOutgoingPositionCurrent++;
2291
2292 /* Check if we reached the end and start from the beginning if so. */
2293 if (pBusLogic->uMailboxOutgoingPositionCurrent >= pBusLogic->cMailbox)
2294 pBusLogic->uMailboxOutgoingPositionCurrent = 0;
2295 }
2296
2297 return rc;
2298}
2299
2300/**
2301 * Transmit queue consumer
2302 * Queue a new async task.
2303 *
2304 * @returns Success indicator.
2305 * If false the item will not be removed and the flushing will stop.
2306 * @param pDevIns The device instance.
2307 * @param pItem The item to consume. Upon return this item will be freed.
2308 */
2309static DECLCALLBACK(bool) buslogicNotifyQueueConsumer(PPDMDEVINS pDevIns, PPDMQUEUEITEMCORE pItem)
2310{
2311 PBUSLOGIC pBusLogic = PDMINS_2_DATA(pDevIns, PBUSLOGIC);
2312
2313 AssertMsg(pBusLogic->cMailboxesReady > 0, ("Got notification without any mailboxes ready\n"));
2314
2315 /* Reset notification send flag now. */
2316 ASMAtomicXchgBool(&pBusLogic->fNotificationSend, false);
2317
2318 /* Process mailboxes. */
2319 do
2320 {
2321 int rc;
2322
2323 rc = buslogicProcessMailboxNext(pBusLogic);
2324 AssertMsgRC(rc, ("Processing mailbox failed rc=%Rrc\n", rc));
2325 } while (ASMAtomicDecU32(&pBusLogic->cMailboxesReady) > 0);
2326
2327 return true;
2328}
2329
2330/**
2331 * Kicks the controller to process pending tasks after the VM was resumed
2332 * or loaded from a saved state.
2333 *
2334 * @returns nothing.
2335 * @param pThis The LsiLogic device instance.
2336 */
2337static void buslogicKick(PBUSLOGIC pThis)
2338{
2339 if (pThis->fRedo)
2340 {
2341 pThis->fRedo = false;
2342 if (pThis->VBoxSCSI.fBusy)
2343 {
2344
2345 /* The BIOS had a request active when we got suspended. Resume it. */
2346 int rc = buslogicPrepareBIOSSCSIRequest(pThis);
2347 AssertRC(rc);
2348 }
2349 else
2350 {
2351 /* Queue all pending tasks again. */
2352 PBUSLOGICTASKSTATE pTaskState = pThis->pTasksRedoHead;
2353
2354 pThis->pTasksRedoHead = NULL;
2355
2356 while (pTaskState)
2357 {
2358 PBUSLOGICTASKSTATE pCur = pTaskState;
2359
2360 int rc = buslogicDeviceSCSIRequestSetup(pThis, pCur);
2361 AssertRC(rc);
2362
2363 pTaskState = pTaskState->pRedoNext;
2364 }
2365 }
2366 }
2367}
2368
2369static DECLCALLBACK(int) buslogicLiveExec(PPDMDEVINS pDevIns, PSSMHANDLE pSSM, uint32_t uPass)
2370{
2371 PBUSLOGIC pThis = PDMINS_2_DATA(pDevIns, PBUSLOGIC);
2372
2373 /* Save the device config. */
2374 for (unsigned i = 0; i < RT_ELEMENTS(pThis->aDeviceStates); i++)
2375 SSMR3PutBool(pSSM, pThis->aDeviceStates[i].fPresent);
2376
2377 return VINF_SSM_DONT_CALL_AGAIN;
2378}
2379
2380static DECLCALLBACK(int) buslogicSaveExec(PPDMDEVINS pDevIns, PSSMHANDLE pSSM)
2381{
2382 PBUSLOGIC pBusLogic = PDMINS_2_DATA(pDevIns, PBUSLOGIC);
2383
2384 /* Every device first. */
2385 for (unsigned i = 0; i < RT_ELEMENTS(pBusLogic->aDeviceStates); i++)
2386 {
2387 PBUSLOGICDEVICE pDevice = &pBusLogic->aDeviceStates[i];
2388
2389 AssertMsg(!pDevice->cOutstandingRequests,
2390 ("There are still outstanding requests on this device\n"));
2391 SSMR3PutBool(pSSM, pDevice->fPresent);
2392 SSMR3PutU32(pSSM, pDevice->cOutstandingRequests);
2393 }
2394 /* Now the main device state. */
2395 SSMR3PutU8 (pSSM, pBusLogic->regStatus);
2396 SSMR3PutU8 (pSSM, pBusLogic->regInterrupt);
2397 SSMR3PutU8 (pSSM, pBusLogic->regGeometry);
2398 SSMR3PutMem (pSSM, &pBusLogic->LocalRam, sizeof(pBusLogic->LocalRam));
2399 SSMR3PutU8 (pSSM, pBusLogic->uOperationCode);
2400 SSMR3PutMem (pSSM, &pBusLogic->aCommandBuffer, sizeof(pBusLogic->aCommandBuffer));
2401 SSMR3PutU8 (pSSM, pBusLogic->iParameter);
2402 SSMR3PutU8 (pSSM, pBusLogic->cbCommandParametersLeft);
2403 SSMR3PutBool (pSSM, pBusLogic->fUseLocalRam);
2404 SSMR3PutMem (pSSM, pBusLogic->aReplyBuffer, sizeof(pBusLogic->aReplyBuffer));
2405 SSMR3PutU8 (pSSM, pBusLogic->iReply);
2406 SSMR3PutU8 (pSSM, pBusLogic->cbReplyParametersLeft);
2407 SSMR3PutBool (pSSM, pBusLogic->fIRQEnabled);
2408 SSMR3PutBool (pSSM, pBusLogic->fISAEnabled);
2409 SSMR3PutU32 (pSSM, pBusLogic->cMailbox);
2410 SSMR3PutGCPhys(pSSM, pBusLogic->GCPhysAddrMailboxOutgoingBase);
2411 SSMR3PutU32 (pSSM, pBusLogic->uMailboxOutgoingPositionCurrent);
2412 SSMR3PutU32 (pSSM, pBusLogic->cMailboxesReady);
2413 SSMR3PutBool (pSSM, pBusLogic->fNotificationSend);
2414 SSMR3PutGCPhys(pSSM, pBusLogic->GCPhysAddrMailboxIncomingBase);
2415 SSMR3PutU32 (pSSM, pBusLogic->uMailboxIncomingPositionCurrent);
2416 SSMR3PutBool (pSSM, pBusLogic->fStrictRoundRobinMode);
2417 SSMR3PutBool (pSSM, pBusLogic->fExtendedLunCCBFormat);
2418 /* Now the data for the BIOS interface. */
2419 SSMR3PutU8 (pSSM, pBusLogic->VBoxSCSI.regIdentify);
2420 SSMR3PutU8 (pSSM, pBusLogic->VBoxSCSI.uTargetDevice);
2421 SSMR3PutU8 (pSSM, pBusLogic->VBoxSCSI.uTxDir);
2422 SSMR3PutU8 (pSSM, pBusLogic->VBoxSCSI.cbCDB);
2423 SSMR3PutMem (pSSM, pBusLogic->VBoxSCSI.aCDB, sizeof(pBusLogic->VBoxSCSI.aCDB));
2424 SSMR3PutU8 (pSSM, pBusLogic->VBoxSCSI.iCDB);
2425 SSMR3PutU32 (pSSM, pBusLogic->VBoxSCSI.cbBuf);
2426 SSMR3PutU32 (pSSM, pBusLogic->VBoxSCSI.iBuf);
2427 SSMR3PutBool (pSSM, pBusLogic->VBoxSCSI.fBusy);
2428 SSMR3PutU8 (pSSM, pBusLogic->VBoxSCSI.enmState);
2429 if (pBusLogic->VBoxSCSI.cbBuf)
2430 SSMR3PutMem(pSSM, pBusLogic->VBoxSCSI.pBuf, pBusLogic->VBoxSCSI.cbBuf);
2431
2432 /*
2433 * Save the physical addresses of the command control blocks of still pending tasks.
2434 * They are processed again on resume.
2435 *
2436 * The number of pending tasks needs to be determined first.
2437 */
2438 uint32_t cTasks = 0;
2439
2440 PBUSLOGICTASKSTATE pTaskState = pBusLogic->pTasksRedoHead;
2441 if (pBusLogic->fRedo)
2442 {
2443 while (pTaskState)
2444 {
2445 cTasks++;
2446 pTaskState = pTaskState->pRedoNext;
2447 }
2448 }
2449 SSMR3PutU32(pSSM, cTasks);
2450
2451 /* Write the address of every task now. */
2452 pTaskState = pBusLogic->pTasksRedoHead;
2453 while (pTaskState)
2454 {
2455 SSMR3PutU32(pSSM, pTaskState->MailboxGuest.u32PhysAddrCCB);
2456 pTaskState = pTaskState->pRedoNext;
2457 }
2458
2459 return SSMR3PutU32(pSSM, ~0);
2460}
2461
2462static DECLCALLBACK(int) buslogicLoadDone(PPDMDEVINS pDevIns, PSSMHANDLE pSSM)
2463{
2464 PBUSLOGIC pThis = PDMINS_2_DATA(pDevIns, PBUSLOGIC);
2465
2466 buslogicKick(pThis);
2467 return VINF_SUCCESS;
2468}
2469
2470static DECLCALLBACK(int) buslogicLoadExec(PPDMDEVINS pDevIns, PSSMHANDLE pSSM, uint32_t uVersion, uint32_t uPass)
2471{
2472 PBUSLOGIC pBusLogic = PDMINS_2_DATA(pDevIns, PBUSLOGIC);
2473 int rc = VINF_SUCCESS;
2474
2475 /* We support saved states only from this and older versions. */
2476 if (uVersion > BUSLOGIC_SAVED_STATE_MINOR_VERSION)
2477 return VERR_SSM_UNSUPPORTED_DATA_UNIT_VERSION;
2478
2479 /* Every device first. */
2480 for (unsigned i = 0; i < RT_ELEMENTS(pBusLogic->aDeviceStates); i++)
2481 {
2482 PBUSLOGICDEVICE pDevice = &pBusLogic->aDeviceStates[i];
2483
2484 AssertMsg(!pDevice->cOutstandingRequests,
2485 ("There are still outstanding requests on this device\n"));
2486 bool fPresent;
2487 rc = SSMR3GetBool(pSSM, &fPresent);
2488 AssertRCReturn(rc, rc);
2489 if (pDevice->fPresent != fPresent)
2490 return SSMR3SetCfgError(pSSM, RT_SRC_POS, N_("Target %u config mismatch: config=%RTbool state=%RTbool"), i, pDevice->fPresent, fPresent);
2491
2492 if (uPass == SSM_PASS_FINAL)
2493 SSMR3GetU32(pSSM, (uint32_t *)&pDevice->cOutstandingRequests);
2494 }
2495
2496 if (uPass != SSM_PASS_FINAL)
2497 return VINF_SUCCESS;
2498
2499 /* Now the main device state. */
2500 SSMR3GetU8 (pSSM, (uint8_t *)&pBusLogic->regStatus);
2501 SSMR3GetU8 (pSSM, (uint8_t *)&pBusLogic->regInterrupt);
2502 SSMR3GetU8 (pSSM, (uint8_t *)&pBusLogic->regGeometry);
2503 SSMR3GetMem (pSSM, &pBusLogic->LocalRam, sizeof(pBusLogic->LocalRam));
2504 SSMR3GetU8 (pSSM, &pBusLogic->uOperationCode);
2505 SSMR3GetMem (pSSM, &pBusLogic->aCommandBuffer, sizeof(pBusLogic->aCommandBuffer));
2506 SSMR3GetU8 (pSSM, &pBusLogic->iParameter);
2507 SSMR3GetU8 (pSSM, &pBusLogic->cbCommandParametersLeft);
2508 SSMR3GetBool (pSSM, &pBusLogic->fUseLocalRam);
2509 SSMR3GetMem (pSSM, pBusLogic->aReplyBuffer, sizeof(pBusLogic->aReplyBuffer));
2510 SSMR3GetU8 (pSSM, &pBusLogic->iReply);
2511 SSMR3GetU8 (pSSM, &pBusLogic->cbReplyParametersLeft);
2512 SSMR3GetBool (pSSM, &pBusLogic->fIRQEnabled);
2513 SSMR3GetBool (pSSM, &pBusLogic->fISAEnabled);
2514 SSMR3GetU32 (pSSM, &pBusLogic->cMailbox);
2515 SSMR3GetGCPhys(pSSM, &pBusLogic->GCPhysAddrMailboxOutgoingBase);
2516 SSMR3GetU32 (pSSM, &pBusLogic->uMailboxOutgoingPositionCurrent);
2517 SSMR3GetU32 (pSSM, (uint32_t *)&pBusLogic->cMailboxesReady);
2518 SSMR3GetBool (pSSM, (bool *)&pBusLogic->fNotificationSend);
2519 SSMR3GetGCPhys(pSSM, &pBusLogic->GCPhysAddrMailboxIncomingBase);
2520 SSMR3GetU32 (pSSM, &pBusLogic->uMailboxIncomingPositionCurrent);
2521 SSMR3GetBool (pSSM, &pBusLogic->fStrictRoundRobinMode);
2522 SSMR3GetBool (pSSM, &pBusLogic->fExtendedLunCCBFormat);
2523 /* Now the data for the BIOS interface. */
2524 SSMR3GetU8 (pSSM, &pBusLogic->VBoxSCSI.regIdentify);
2525 SSMR3GetU8 (pSSM, &pBusLogic->VBoxSCSI.uTargetDevice);
2526 SSMR3GetU8 (pSSM, &pBusLogic->VBoxSCSI.uTxDir);
2527 SSMR3GetU8 (pSSM, &pBusLogic->VBoxSCSI.cbCDB);
2528 SSMR3GetMem (pSSM, pBusLogic->VBoxSCSI.aCDB, sizeof(pBusLogic->VBoxSCSI.aCDB));
2529 SSMR3GetU8 (pSSM, &pBusLogic->VBoxSCSI.iCDB);
2530 SSMR3GetU32 (pSSM, &pBusLogic->VBoxSCSI.cbBuf);
2531 SSMR3GetU32 (pSSM, &pBusLogic->VBoxSCSI.iBuf);
2532 SSMR3GetBool(pSSM, (bool *)&pBusLogic->VBoxSCSI.fBusy);
2533 SSMR3GetU8 (pSSM, (uint8_t *)&pBusLogic->VBoxSCSI.enmState);
2534 if (pBusLogic->VBoxSCSI.cbBuf)
2535 {
2536 pBusLogic->VBoxSCSI.pBuf = (uint8_t *)RTMemAllocZ(pBusLogic->VBoxSCSI.cbBuf);
2537 if (!pBusLogic->VBoxSCSI.pBuf)
2538 {
2539 LogRel(("BusLogic: Out of memory during restore.\n"));
2540 return PDMDEV_SET_ERROR(pDevIns, VERR_NO_MEMORY,
2541 N_("BusLogic: Out of memory during restore\n"));
2542 }
2543 SSMR3GetMem(pSSM, pBusLogic->VBoxSCSI.pBuf, pBusLogic->VBoxSCSI.cbBuf);
2544 }
2545
2546 if (pBusLogic->VBoxSCSI.fBusy)
2547 pBusLogic->fRedo = true;
2548
2549 if (uVersion > BUSLOGIC_SAVED_STATE_MINOR_PRE_ERROR_HANDLING)
2550 {
2551 /* Check if there are pending tasks saved. */
2552 uint32_t cTasks = 0;
2553
2554 SSMR3GetU32(pSSM, &cTasks);
2555
2556 if (cTasks)
2557 pBusLogic->fRedo = true;
2558
2559 for (uint32_t i = 0; i < cTasks; i++)
2560 {
2561 PBUSLOGICTASKSTATE pTaskState = (PBUSLOGICTASKSTATE)RTMemCacheAlloc(pBusLogic->hTaskCache);
2562 if (!pTaskState)
2563 {
2564 rc = VERR_NO_MEMORY;
2565 break;
2566 }
2567
2568 rc = SSMR3GetU32(pSSM, &pTaskState->MailboxGuest.u32PhysAddrCCB);
2569 if (RT_FAILURE(rc))
2570 {
2571 RTMemCacheFree(pBusLogic->hTaskCache, pTaskState);
2572 break;
2573 }
2574
2575 /* Link into the list. */
2576 pTaskState->pRedoNext = pBusLogic->pTasksRedoHead;
2577 pBusLogic->pTasksRedoHead = pTaskState;
2578 }
2579 }
2580
2581 if (RT_SUCCESS(rc))
2582 {
2583 uint32_t u32;
2584 rc = SSMR3GetU32(pSSM, &u32);
2585 if (RT_SUCCESS(rc))
2586 AssertMsgReturn(u32 == ~0U, ("%#x\n", u32), VERR_SSM_DATA_UNIT_FORMAT_CHANGED);
2587 }
2588
2589 return rc;
2590}
2591
2592/**
2593 * Gets the pointer to the status LED of a device - called from the SCSi driver.
2594 *
2595 * @returns VBox status code.
2596 * @param pInterface Pointer to the interface structure containing the called function pointer.
2597 * @param iLUN The unit which status LED we desire. Always 0 here as the driver
2598 * doesn't know about other LUN's.
2599 * @param ppLed Where to store the LED pointer.
2600 */
2601static DECLCALLBACK(int) buslogicDeviceQueryStatusLed(PPDMILEDPORTS pInterface, unsigned iLUN, PPDMLED *ppLed)
2602{
2603 PBUSLOGICDEVICE pDevice = PDMILEDPORTS_2_PBUSLOGICDEVICE(pInterface);
2604 if (iLUN == 0)
2605 {
2606 *ppLed = &pDevice->Led;
2607 Assert((*ppLed)->u32Magic == PDMLED_MAGIC);
2608 return VINF_SUCCESS;
2609 }
2610 return VERR_PDM_LUN_NOT_FOUND;
2611}
2612
2613/**
2614 * @interface_method_impl{PDMIBASE,pfnQueryInterface}
2615 */
2616static DECLCALLBACK(void *) buslogicDeviceQueryInterface(PPDMIBASE pInterface, const char *pszIID)
2617{
2618 PBUSLOGICDEVICE pDevice = PDMIBASE_2_PBUSLOGICDEVICE(pInterface);
2619 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIBASE, &pDevice->IBase);
2620 PDMIBASE_RETURN_INTERFACE(pszIID, PDMISCSIPORT, &pDevice->ISCSIPort);
2621 PDMIBASE_RETURN_INTERFACE(pszIID, PDMILEDPORTS, &pDevice->ILed);
2622 return NULL;
2623}
2624
2625/**
2626 * Gets the pointer to the status LED of a unit.
2627 *
2628 * @returns VBox status code.
2629 * @param pInterface Pointer to the interface structure containing the called function pointer.
2630 * @param iLUN The unit which status LED we desire.
2631 * @param ppLed Where to store the LED pointer.
2632 */
2633static DECLCALLBACK(int) buslogicStatusQueryStatusLed(PPDMILEDPORTS pInterface, unsigned iLUN, PPDMLED *ppLed)
2634{
2635 PBUSLOGIC pBusLogic = PDMILEDPORTS_2_PBUSLOGIC(pInterface);
2636 if (iLUN < BUSLOGIC_MAX_DEVICES)
2637 {
2638 *ppLed = &pBusLogic->aDeviceStates[iLUN].Led;
2639 Assert((*ppLed)->u32Magic == PDMLED_MAGIC);
2640 return VINF_SUCCESS;
2641 }
2642 return VERR_PDM_LUN_NOT_FOUND;
2643}
2644
2645/**
2646 * @interface_method_impl{PDMIBASE,pfnQueryInterface}
2647 */
2648static DECLCALLBACK(void *) buslogicStatusQueryInterface(PPDMIBASE pInterface, const char *pszIID)
2649{
2650 PBUSLOGIC pThis = PDMIBASE_2_PBUSLOGIC(pInterface);
2651 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIBASE, &pThis->IBase);
2652 PDMIBASE_RETURN_INTERFACE(pszIID, PDMILEDPORTS, &pThis->ILeds);
2653 return NULL;
2654}
2655
2656/* -=-=-=-=- Helper -=-=-=-=- */
2657
2658 /**
2659 * Checks if all asynchronous I/O is finished.
2660 *
2661 * Used by lsilogicReset, lsilogicSuspend and lsilogicPowerOff.
2662 *
2663 * @returns true if quiesced, false if busy.
2664 * @param pDevIns The device instance.
2665 */
2666static bool buslogicR3AllAsyncIOIsFinished(PPDMDEVINS pDevIns)
2667{
2668 PBUSLOGIC pThis = PDMINS_2_DATA(pDevIns, PBUSLOGIC);
2669
2670 for (uint32_t i = 0; i < RT_ELEMENTS(pThis->aDeviceStates); i++)
2671 {
2672 PBUSLOGICDEVICE pThisDevice = &pThis->aDeviceStates[i];
2673 if (pThisDevice->pDrvBase)
2674 {
2675 if (pThisDevice->cOutstandingRequests != 0)
2676 return false;
2677 }
2678 }
2679
2680 return true;
2681}
2682
2683/**
2684 * Callback employed by lsilogicR3Suspend and lsilogicR3PowerOff..
2685 *
2686 * @returns true if we've quiesced, false if we're still working.
2687 * @param pDevIns The device instance.
2688 */
2689static DECLCALLBACK(bool) buslogicR3IsAsyncSuspendOrPowerOffDone(PPDMDEVINS pDevIns)
2690{
2691 if (!buslogicR3AllAsyncIOIsFinished(pDevIns))
2692 return false;
2693
2694 PBUSLOGIC pThis = PDMINS_2_DATA(pDevIns, PBUSLOGIC);
2695 ASMAtomicWriteBool(&pThis->fSignalIdle, false);
2696 return true;
2697}
2698
2699/**
2700 * Common worker for ahciR3Suspend and ahciR3PowerOff.
2701 */
2702static void buslogicR3SuspendOrPowerOff(PPDMDEVINS pDevIns, bool fPowerOff)
2703{
2704 PBUSLOGIC pThis = PDMINS_2_DATA(pDevIns, PBUSLOGIC);
2705
2706 ASMAtomicWriteBool(&pThis->fSignalIdle, true);
2707 if (!buslogicR3AllAsyncIOIsFinished(pDevIns))
2708 PDMDevHlpSetAsyncNotification(pDevIns, buslogicR3IsAsyncSuspendOrPowerOffDone);
2709 else
2710 {
2711 ASMAtomicWriteBool(&pThis->fSignalIdle, false);
2712
2713 AssertMsg(!pThis->fNotificationSend, ("The PDM Queue should be empty at this point\n"));
2714
2715 if (pThis->fRedo)
2716 {
2717 if (fPowerOff)
2718 {
2719 /* Free tasks which would have been queued again on resume. */
2720 PBUSLOGICTASKSTATE pTaskState = pThis->pTasksRedoHead;
2721
2722 pThis->pTasksRedoHead = NULL;
2723
2724 while (pTaskState)
2725 {
2726 PBUSLOGICTASKSTATE pFree;
2727
2728 pFree = pTaskState;
2729 pTaskState = pTaskState->pRedoNext;
2730
2731 RTMemCacheFree(pThis->hTaskCache, pFree);
2732 }
2733 pThis->fRedo = false;
2734 }
2735 else if (pThis->VBoxSCSI.fBusy)
2736 {
2737 /* Destroy the task because the BIOS interface has all necessary information. */
2738 Assert(pThis->pTasksRedoHead->fBIOS);
2739 Assert(!pThis->pTasksRedoHead->pRedoNext);
2740
2741 RTMemCacheFree(pThis->hTaskCache, pThis->pTasksRedoHead);
2742 pThis->pTasksRedoHead = NULL;
2743 }
2744 }
2745 }
2746}
2747
2748/**
2749 * Suspend notification.
2750 *
2751 * @param pDevIns The device instance data.
2752 */
2753static DECLCALLBACK(void) buslogicSuspend(PPDMDEVINS pDevIns)
2754{
2755 Log(("buslogicSuspend\n"));
2756 buslogicR3SuspendOrPowerOff(pDevIns, false /* fPoweroff */);
2757}
2758
2759/**
2760 * Resume notification.
2761 *
2762 * @param pDevIns The device instance data.
2763 */
2764static DECLCALLBACK(void) buslogicResume(PPDMDEVINS pDevIns)
2765{
2766 Log(("buslogicResume\n"));
2767 PBUSLOGIC pThis = PDMINS_2_DATA(pDevIns, PBUSLOGIC);
2768 buslogicKick(pThis);
2769}
2770
2771
2772/**
2773 * Detach notification.
2774 *
2775 * One harddisk at one port has been unplugged.
2776 * The VM is suspended at this point.
2777 *
2778 * @param pDevIns The device instance.
2779 * @param iLUN The logical unit which is being detached.
2780 * @param fFlags Flags, combination of the PDMDEVATT_FLAGS_* \#defines.
2781 */
2782static DECLCALLBACK(void) buslogicDetach(PPDMDEVINS pDevIns, unsigned iLUN, uint32_t fFlags)
2783{
2784 PBUSLOGIC pThis = PDMINS_2_DATA(pDevIns, PBUSLOGIC);
2785 PBUSLOGICDEVICE pDevice = &pThis->aDeviceStates[iLUN];
2786
2787 Log(("%s:\n", __FUNCTION__));
2788
2789 AssertMsg(fFlags & PDM_TACH_FLAGS_NOT_HOT_PLUG,
2790 ("BusLogic: Device does not support hotplugging\n"));
2791
2792 /*
2793 * Zero some important members.
2794 */
2795 pDevice->pDrvBase = NULL;
2796 pDevice->fPresent = false;
2797 pDevice->pDrvSCSIConnector = NULL;
2798}
2799
2800/**
2801 * Attach command.
2802 *
2803 * This is called when we change block driver.
2804 *
2805 * @returns VBox status code.
2806 * @param pDevIns The device instance.
2807 * @param iLUN The logical unit which is being detached.
2808 * @param fFlags Flags, combination of the PDMDEVATT_FLAGS_* \#defines.
2809 */
2810static DECLCALLBACK(int) buslogicAttach(PPDMDEVINS pDevIns, unsigned iLUN, uint32_t fFlags)
2811{
2812 PBUSLOGIC pThis = PDMINS_2_DATA(pDevIns, PBUSLOGIC);
2813 PBUSLOGICDEVICE pDevice = &pThis->aDeviceStates[iLUN];
2814 int rc;
2815
2816 AssertMsgReturn(fFlags & PDM_TACH_FLAGS_NOT_HOT_PLUG,
2817 ("BusLogic: Device does not support hotplugging\n"),
2818 VERR_INVALID_PARAMETER);
2819
2820 /* the usual paranoia */
2821 AssertRelease(!pDevice->pDrvBase);
2822 AssertRelease(!pDevice->pDrvSCSIConnector);
2823 Assert(pDevice->iLUN == iLUN);
2824
2825 /*
2826 * Try attach the block device and get the interfaces,
2827 * required as well as optional.
2828 */
2829 rc = PDMDevHlpDriverAttach(pDevIns, pDevice->iLUN, &pDevice->IBase, &pDevice->pDrvBase, NULL);
2830 if (RT_SUCCESS(rc))
2831 {
2832 /* Get SCSI connector interface. */
2833 pDevice->pDrvSCSIConnector = PDMIBASE_QUERY_INTERFACE(pDevice->pDrvBase, PDMISCSICONNECTOR);
2834 AssertMsgReturn(pDevice->pDrvSCSIConnector, ("Missing SCSI interface below\n"), VERR_PDM_MISSING_INTERFACE);
2835 pDevice->fPresent = true;
2836 }
2837 else
2838 AssertMsgFailed(("Failed to attach LUN#%d. rc=%Rrc\n", pDevice->iLUN, rc));
2839
2840 if (RT_FAILURE(rc))
2841 {
2842 pDevice->pDrvBase = NULL;
2843 pDevice->pDrvSCSIConnector = NULL;
2844 }
2845 return rc;
2846}
2847
2848/**
2849 * Callback employed by buslogicR3Reset.
2850 *
2851 * @returns true if we've quiesced, false if we're still working.
2852 * @param pDevIns The device instance.
2853 */
2854static DECLCALLBACK(bool) buslogicR3IsAsyncResetDone(PPDMDEVINS pDevIns)
2855{
2856 PBUSLOGIC pThis = PDMINS_2_DATA(pDevIns, PBUSLOGIC);
2857
2858 if (!buslogicR3AllAsyncIOIsFinished(pDevIns))
2859 return false;
2860 ASMAtomicWriteBool(&pThis->fSignalIdle, false);
2861
2862 buslogicHwReset(pThis);
2863 return true;
2864}
2865
2866/**
2867 * @copydoc FNPDMDEVRESET
2868 */
2869static DECLCALLBACK(void) buslogicReset(PPDMDEVINS pDevIns)
2870{
2871 PBUSLOGIC pThis = PDMINS_2_DATA(pDevIns, PBUSLOGIC);
2872
2873 ASMAtomicWriteBool(&pThis->fSignalIdle, true);
2874 if (!buslogicR3AllAsyncIOIsFinished(pDevIns))
2875 PDMDevHlpSetAsyncNotification(pDevIns, buslogicR3IsAsyncResetDone);
2876 else
2877 {
2878 ASMAtomicWriteBool(&pThis->fSignalIdle, false);
2879 buslogicHwReset(pThis);
2880 }
2881}
2882
2883static DECLCALLBACK(void) buslogicRelocate(PPDMDEVINS pDevIns, RTGCINTPTR offDelta)
2884{
2885 uint32_t i;
2886 PBUSLOGIC pBusLogic = PDMINS_2_DATA(pDevIns, PBUSLOGIC);
2887
2888 pBusLogic->pDevInsRC = PDMDEVINS_2_RCPTR(pDevIns);
2889 pBusLogic->pNotifierQueueRC = PDMQueueRCPtr(pBusLogic->pNotifierQueueR3);
2890
2891 for (i = 0; i < BUSLOGIC_MAX_DEVICES; i++)
2892 {
2893 PBUSLOGICDEVICE pDevice = &pBusLogic->aDeviceStates[i];
2894
2895 pDevice->pBusLogicRC = PDMINS_2_DATA_RCPTR(pDevIns);
2896 }
2897
2898}
2899
2900/**
2901 * Poweroff notification.
2902 *
2903 * @param pDevIns Pointer to the device instance
2904 */
2905static DECLCALLBACK(void) buslogicPowerOff(PPDMDEVINS pDevIns)
2906{
2907 Log(("buslogicPowerOff\n"));
2908 buslogicR3SuspendOrPowerOff(pDevIns, true /* fPoweroff */);
2909}
2910
2911/**
2912 * Destroy a driver instance.
2913 *
2914 * Most VM resources are freed by the VM. This callback is provided so that any non-VM
2915 * resources can be freed correctly.
2916 *
2917 * @param pDevIns The device instance data.
2918 */
2919static DECLCALLBACK(int) buslogicDestruct(PPDMDEVINS pDevIns)
2920{
2921 PBUSLOGIC pThis = PDMINS_2_DATA(pDevIns, PBUSLOGIC);
2922 PDMDEV_CHECK_VERSIONS_RETURN_QUIET(pDevIns);
2923
2924 PDMR3CritSectDelete(&pThis->CritSectIntr);
2925
2926 /*
2927 * Free all tasks which are still hanging around
2928 * (Power off after the VM was suspended).
2929 */
2930 if (pThis->fRedo)
2931 {
2932 /* Free tasks which would have been queued again on resume. */
2933 PBUSLOGICTASKSTATE pTaskState = pThis->pTasksRedoHead;
2934
2935 pThis->pTasksRedoHead = NULL;
2936
2937 while (pTaskState)
2938 {
2939 PBUSLOGICTASKSTATE pFree;
2940
2941 pFree = pTaskState;
2942 pTaskState = pTaskState->pRedoNext;
2943
2944 RTMemCacheFree(pThis->hTaskCache, pFree);
2945 }
2946 pThis->fRedo = false;
2947 }
2948
2949 int rc = RTMemCacheDestroy(pThis->hTaskCache);
2950 AssertMsgRC(rc, ("Destroying task cache failed rc=%Rrc\n", rc));
2951
2952 return rc;
2953}
2954
2955/**
2956 * @interface_method_impl{PDMDEVREG,pfnConstruct}
2957 */
2958static DECLCALLBACK(int) buslogicConstruct(PPDMDEVINS pDevIns, int iInstance, PCFGMNODE pCfg)
2959{
2960 PBUSLOGIC pThis = PDMINS_2_DATA(pDevIns, PBUSLOGIC);
2961 int rc = VINF_SUCCESS;
2962 bool fBootable = true;
2963 PDMDEV_CHECK_VERSIONS_RETURN(pDevIns);
2964
2965 /*
2966 * Validate and read configuration.
2967 */
2968 if (!CFGMR3AreValuesValid(pCfg,
2969 "GCEnabled\0"
2970 "R0Enabled\0"
2971 "Bootable\0"))
2972 return PDMDEV_SET_ERROR(pDevIns, VERR_PDM_DEVINS_UNKNOWN_CFG_VALUES,
2973 N_("BusLogic configuration error: unknown option specified"));
2974
2975 rc = CFGMR3QueryBoolDef(pCfg, "GCEnabled", &pThis->fGCEnabled, true);
2976 if (RT_FAILURE(rc))
2977 return PDMDEV_SET_ERROR(pDevIns, rc,
2978 N_("BusLogic configuration error: failed to read GCEnabled as boolean"));
2979 Log(("%s: fGCEnabled=%d\n", __FUNCTION__, pThis->fGCEnabled));
2980
2981 rc = CFGMR3QueryBoolDef(pCfg, "R0Enabled", &pThis->fR0Enabled, true);
2982 if (RT_FAILURE(rc))
2983 return PDMDEV_SET_ERROR(pDevIns, rc,
2984 N_("BusLogic configuration error: failed to read R0Enabled as boolean"));
2985 Log(("%s: fR0Enabled=%d\n", __FUNCTION__, pThis->fR0Enabled));
2986 rc = CFGMR3QueryBoolDef(pCfg, "Bootable", &fBootable, true);
2987 if (RT_FAILURE(rc))
2988 return PDMDEV_SET_ERROR(pDevIns, rc,
2989 N_("BusLogic configuration error: failed to read Bootable as boolean"));
2990 Log(("%s: fBootable=%RTbool\n", __FUNCTION__, fBootable));
2991
2992 pThis->pDevInsR3 = pDevIns;
2993 pThis->pDevInsR0 = PDMDEVINS_2_R0PTR(pDevIns);
2994 pThis->pDevInsRC = PDMDEVINS_2_RCPTR(pDevIns);
2995 pThis->IBase.pfnQueryInterface = buslogicStatusQueryInterface;
2996 pThis->ILeds.pfnQueryStatusLed = buslogicStatusQueryStatusLed;
2997
2998 PCIDevSetVendorId (&pThis->dev, 0x104b); /* BusLogic */
2999 PCIDevSetDeviceId (&pThis->dev, 0x1040); /* BT-958 */
3000 PCIDevSetCommand (&pThis->dev, 0x0003);
3001 PCIDevSetRevisionId (&pThis->dev, 0x01);
3002 PCIDevSetClassProg (&pThis->dev, 0x00); /* SCSI */
3003 PCIDevSetClassSub (&pThis->dev, 0x00); /* SCSI */
3004 PCIDevSetClassBase (&pThis->dev, 0x01); /* Mass storage */
3005 PCIDevSetBaseAddress (&pThis->dev, 0, true /*IO*/, false /*Pref*/, false /*64-bit*/, 0x00000000);
3006 PCIDevSetBaseAddress (&pThis->dev, 1, false /*IO*/, false /*Pref*/, false /*64-bit*/, 0x00000000);
3007 PCIDevSetSubSystemVendorId(&pThis->dev, 0x104b);
3008 PCIDevSetSubSystemId (&pThis->dev, 0x1040);
3009 PCIDevSetInterruptLine (&pThis->dev, 0x00);
3010 PCIDevSetInterruptPin (&pThis->dev, 0x01);
3011
3012 /*
3013 * Register the PCI device, it's I/O regions.
3014 */
3015 rc = PDMDevHlpPCIRegister (pDevIns, &pThis->dev);
3016 if (RT_FAILURE(rc))
3017 return rc;
3018
3019 rc = PDMDevHlpPCIIORegionRegister(pDevIns, 0, 32, PCI_ADDRESS_SPACE_IO, buslogicMMIOMap);
3020 if (RT_FAILURE(rc))
3021 return rc;
3022
3023 rc = PDMDevHlpPCIIORegionRegister(pDevIns, 1, 32, PCI_ADDRESS_SPACE_MEM, buslogicMMIOMap);
3024 if (RT_FAILURE(rc))
3025 return rc;
3026
3027 if (fBootable)
3028 {
3029 /* Register I/O port space in ISA region for BIOS access. */
3030 rc = PDMDevHlpIOPortRegister(pDevIns, BUSLOGIC_ISA_IO_PORT, 3, NULL,
3031 buslogicIsaIOPortWrite, buslogicIsaIOPortRead,
3032 buslogicIsaIOPortWriteStr, buslogicIsaIOPortReadStr,
3033 "BusLogic BIOS");
3034 if (RT_FAILURE(rc))
3035 return PDMDEV_SET_ERROR(pDevIns, rc, N_("BusLogic cannot register legacy I/O handlers"));
3036 }
3037
3038 /* Initialize task cache. */
3039 rc = RTMemCacheCreate(&pThis->hTaskCache, sizeof(BUSLOGICTASKSTATE), 0, UINT32_MAX,
3040 NULL, NULL, NULL, 0);
3041 if (RT_FAILURE(rc))
3042 return PDMDEV_SET_ERROR(pDevIns, rc,
3043 N_("BusLogic: Failed to initialize task cache\n"));
3044
3045 /* Initialize task queue. */
3046 rc = PDMDevHlpQueueCreate(pDevIns, sizeof(PDMQUEUEITEMCORE), 5, 0,
3047 buslogicNotifyQueueConsumer, true, "BugLogicTask", &pThis->pNotifierQueueR3);
3048 if (RT_FAILURE(rc))
3049 return rc;
3050 pThis->pNotifierQueueR0 = PDMQueueR0Ptr(pThis->pNotifierQueueR3);
3051 pThis->pNotifierQueueRC = PDMQueueRCPtr(pThis->pNotifierQueueR3);
3052
3053 rc = PDMDevHlpCritSectInit(pDevIns, &pThis->CritSectIntr, RT_SRC_POS, "BusLogic-Intr");
3054 if (RT_FAILURE(rc))
3055 return PDMDEV_SET_ERROR(pDevIns, rc,
3056 N_("BusLogic: cannot create critical section"));
3057
3058 /* Initialize per device state. */
3059 for (unsigned i = 0; i < RT_ELEMENTS(pThis->aDeviceStates); i++)
3060 {
3061 char szName[24];
3062 PBUSLOGICDEVICE pDevice = &pThis->aDeviceStates[i];
3063
3064 RTStrPrintf(szName, sizeof(szName), "Device%d", i);
3065
3066 /* Initialize static parts of the device. */
3067 pDevice->iLUN = i;
3068 pDevice->pBusLogicR3 = pThis;
3069 pDevice->pBusLogicR0 = PDMINS_2_DATA_R0PTR(pDevIns);
3070 pDevice->pBusLogicRC = PDMINS_2_DATA_RCPTR(pDevIns);
3071 pDevice->Led.u32Magic = PDMLED_MAGIC;
3072 pDevice->IBase.pfnQueryInterface = buslogicDeviceQueryInterface;
3073 pDevice->ISCSIPort.pfnSCSIRequestCompleted = buslogicDeviceSCSIRequestCompleted;
3074 pDevice->ILed.pfnQueryStatusLed = buslogicDeviceQueryStatusLed;
3075
3076 /* Attach SCSI driver. */
3077 rc = PDMDevHlpDriverAttach(pDevIns, pDevice->iLUN, &pDevice->IBase, &pDevice->pDrvBase, szName);
3078 if (RT_SUCCESS(rc))
3079 {
3080 /* Get SCSI connector interface. */
3081 pDevice->pDrvSCSIConnector = PDMIBASE_QUERY_INTERFACE(pDevice->pDrvBase, PDMISCSICONNECTOR);
3082 AssertMsgReturn(pDevice->pDrvSCSIConnector, ("Missing SCSI interface below\n"), VERR_PDM_MISSING_INTERFACE);
3083
3084 pDevice->fPresent = true;
3085 }
3086 else if (rc == VERR_PDM_NO_ATTACHED_DRIVER)
3087 {
3088 pDevice->pDrvBase = NULL;
3089 pDevice->fPresent = false;
3090 rc = VINF_SUCCESS;
3091 Log(("BusLogic: no driver attached to device %s\n", szName));
3092 }
3093 else
3094 {
3095 AssertLogRelMsgFailed(("BusLogic: Failed to attach %s\n", szName));
3096 return rc;
3097 }
3098 }
3099
3100 /*
3101 * Attach status driver (optional).
3102 */
3103 PPDMIBASE pBase;
3104 rc = PDMDevHlpDriverAttach(pDevIns, PDM_STATUS_LUN, &pThis->IBase, &pBase, "Status Port");
3105 if (RT_SUCCESS(rc))
3106 pThis->pLedsConnector = PDMIBASE_QUERY_INTERFACE(pBase, PDMILEDCONNECTORS);
3107 else if (rc != VERR_PDM_NO_ATTACHED_DRIVER)
3108 {
3109 AssertMsgFailed(("Failed to attach to status driver. rc=%Rrc\n", rc));
3110 return PDMDEV_SET_ERROR(pDevIns, rc, N_("BusLogic cannot attach to status driver"));
3111 }
3112
3113 rc = PDMDevHlpSSMRegisterEx(pDevIns, BUSLOGIC_SAVED_STATE_MINOR_VERSION, sizeof(*pThis), NULL,
3114 NULL, buslogicLiveExec, NULL,
3115 NULL, buslogicSaveExec, NULL,
3116 NULL, buslogicLoadExec, buslogicLoadDone);
3117 if (RT_FAILURE(rc))
3118 return PDMDEV_SET_ERROR(pDevIns, rc, N_("BusLogic cannot register save state handlers"));
3119
3120 rc = buslogicHwReset(pThis);
3121 AssertMsgRC(rc, ("hardware reset of BusLogic host adapter failed rc=%Rrc\n", rc));
3122
3123 return rc;
3124}
3125
3126/**
3127 * The device registration structure.
3128 */
3129const PDMDEVREG g_DeviceBusLogic =
3130{
3131 /* u32Version */
3132 PDM_DEVREG_VERSION,
3133 /* szName */
3134 "buslogic",
3135 /* szRCMod */
3136 "VBoxDDGC.gc",
3137 /* szR0Mod */
3138 "VBoxDDR0.r0",
3139 /* pszDescription */
3140 "BusLogic BT-958 SCSI host adapter.\n",
3141 /* fFlags */
3142 PDM_DEVREG_FLAGS_DEFAULT_BITS | PDM_DEVREG_FLAGS_RC | PDM_DEVREG_FLAGS_R0 |
3143 PDM_DEVREG_FLAGS_FIRST_SUSPEND_NOTIFICATION | PDM_DEVREG_FLAGS_FIRST_POWEROFF_NOTIFICATION,
3144 /* fClass */
3145 PDM_DEVREG_CLASS_STORAGE,
3146 /* cMaxInstances */
3147 ~0,
3148 /* cbInstance */
3149 sizeof(BUSLOGIC),
3150 /* pfnConstruct */
3151 buslogicConstruct,
3152 /* pfnDestruct */
3153 buslogicDestruct,
3154 /* pfnRelocate */
3155 buslogicRelocate,
3156 /* pfnIOCtl */
3157 NULL,
3158 /* pfnPowerOn */
3159 NULL,
3160 /* pfnReset */
3161 buslogicReset,
3162 /* pfnSuspend */
3163 buslogicSuspend,
3164 /* pfnResume */
3165 buslogicResume,
3166 /* pfnAttach */
3167 buslogicAttach,
3168 /* pfnDetach */
3169 buslogicDetach,
3170 /* pfnQueryInterface. */
3171 NULL,
3172 /* pfnInitComplete */
3173 NULL,
3174 /* pfnPowerOff */
3175 buslogicPowerOff,
3176 /* pfnSoftReset */
3177 NULL,
3178 /* u32VersionEnd */
3179 PDM_DEVREG_VERSION
3180};
3181
3182#endif /* IN_RING3 */
3183#endif /* !VBOX_DEVICE_STRUCT_TESTCASE */
3184
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