VirtualBox

source: vbox/trunk/src/VBox/Devices/Storage/DevAHCI.cpp@ 81887

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

DevAHCI: s/ahciPort/aPorts/g. bugref:9218

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 242.0 KB
Line 
1/* $Id: DevAHCI.cpp 81887 2019-11-15 22:29:05Z vboxsync $ */
2/** @file
3 * DevAHCI - AHCI controller device (disk and cdrom).
4 *
5 * Implements the AHCI standard 1.1
6 */
7
8/*
9 * Copyright (C) 2006-2019 Oracle Corporation
10 *
11 * This file is part of VirtualBox Open Source Edition (OSE), as
12 * available from http://www.virtualbox.org. This file is free software;
13 * you can redistribute it and/or modify it under the terms of the GNU
14 * General Public License (GPL) as published by the Free Software
15 * Foundation, in version 2 as it comes in the "COPYING" file of the
16 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
17 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
18 */
19
20/** @page pg_dev_ahci AHCI - Advanced Host Controller Interface Emulation.
21 *
22 * This component implements an AHCI serial ATA controller. The device is split
23 * into two parts. The first part implements the register interface for the
24 * guest and the second one does the data transfer.
25 *
26 * The guest can access the controller in two ways. The first one is the native
27 * way implementing the registers described in the AHCI specification and is
28 * the preferred one. The second implements the I/O ports used for booting from
29 * the hard disk and for guests which don't have an AHCI SATA driver.
30 *
31 * The data is transfered using the extended media interface, asynchronously if
32 * it is supported by the driver below otherwise it weill be done synchronous.
33 * Either way a thread is used to process new requests from the guest.
34 */
35
36
37/*********************************************************************************************************************************
38* Header Files *
39*********************************************************************************************************************************/
40#define LOG_GROUP LOG_GROUP_DEV_AHCI
41#include <VBox/vmm/pdmdev.h>
42#include <VBox/vmm/pdmstorageifs.h>
43#include <VBox/vmm/pdmqueue.h>
44#include <VBox/vmm/pdmthread.h>
45#include <VBox/vmm/pdmcritsect.h>
46#include <VBox/sup.h>
47#include <VBox/scsi.h>
48#include <VBox/ata.h>
49#include <VBox/AssertGuest.h>
50#include <iprt/assert.h>
51#include <iprt/asm.h>
52#include <iprt/string.h>
53#include <iprt/list.h>
54#ifdef IN_RING3
55# include <iprt/param.h>
56# include <iprt/thread.h>
57# include <iprt/semaphore.h>
58# include <iprt/alloc.h>
59# include <iprt/uuid.h>
60# include <iprt/time.h>
61#endif
62#include "VBoxDD.h"
63
64#if defined(VBOX_WITH_DTRACE) \
65 && defined(IN_RING3) \
66 && !defined(VBOX_DEVICE_STRUCT_TESTCASE)
67# include "dtrace/VBoxDD.h"
68#else
69# define VBOXDD_AHCI_REQ_SUBMIT(a,b,c,d) do { } while (0)
70# define VBOXDD_AHCI_REQ_COMPLETED(a,b,c,d) do { } while (0)
71#endif
72
73/** Maximum number of ports available.
74 * Spec defines 32 but we have one allocated for command completion coalescing
75 * and another for a reserved future feature.
76 */
77#define AHCI_MAX_NR_PORTS_IMPL 30
78/** Maximum number of command slots available. */
79#define AHCI_NR_COMMAND_SLOTS 32
80
81/** The current saved state version. */
82#define AHCI_SAVED_STATE_VERSION 9
83/** The saved state version before the ATAPI emulation was removed and the generic SCSI driver was used. */
84#define AHCI_SAVED_STATE_VERSION_PRE_ATAPI_REMOVE 8
85/** The saved state version before changing the port reset logic in an incompatible way. */
86#define AHCI_SAVED_STATE_VERSION_PRE_PORT_RESET_CHANGES 7
87/** Saved state version before the per port hotplug port was added. */
88#define AHCI_SAVED_STATE_VERSION_PRE_HOTPLUG_FLAG 6
89/** Saved state version before legacy ATA emulation was dropped. */
90#define AHCI_SAVED_STATE_VERSION_IDE_EMULATION 5
91/** Saved state version before ATAPI support was added. */
92#define AHCI_SAVED_STATE_VERSION_PRE_ATAPI 3
93/** The saved state version use in VirtualBox 3.0 and earlier.
94 * This was before the config was added and ahciIOTasks was dropped. */
95#define AHCI_SAVED_STATE_VERSION_VBOX_30 2
96/* for Older ATA state Read handling */
97#define ATA_CTL_SAVED_STATE_VERSION 3
98#define ATA_CTL_SAVED_STATE_VERSION_WITHOUT_FULL_SENSE 1
99#define ATA_CTL_SAVED_STATE_VERSION_WITHOUT_EVENT_STATUS 2
100
101/** The maximum number of release log entries per device. */
102#define MAX_LOG_REL_ERRORS 1024
103
104/**
105 * Maximum number of sectors to transfer in a READ/WRITE MULTIPLE request.
106 * Set to 1 to disable multi-sector read support. According to the ATA
107 * specification this must be a power of 2 and it must fit in an 8 bit
108 * value. Thus the only valid values are 1, 2, 4, 8, 16, 32, 64 and 128.
109 */
110#define ATA_MAX_MULT_SECTORS 128
111
112/**
113 * Fastest PIO mode supported by the drive.
114 */
115#define ATA_PIO_MODE_MAX 4
116/**
117 * Fastest MDMA mode supported by the drive.
118 */
119#define ATA_MDMA_MODE_MAX 2
120/**
121 * Fastest UDMA mode supported by the drive.
122 */
123#define ATA_UDMA_MODE_MAX 6
124
125/**
126 * Length of the configurable VPD data (without termination)
127 */
128#define AHCI_SERIAL_NUMBER_LENGTH 20
129#define AHCI_FIRMWARE_REVISION_LENGTH 8
130#define AHCI_MODEL_NUMBER_LENGTH 40
131#define AHCI_ATAPI_INQUIRY_VENDOR_ID_LENGTH 8
132#define AHCI_ATAPI_INQUIRY_PRODUCT_ID_LENGTH 16
133#define AHCI_ATAPI_INQUIRY_REVISION_LENGTH 4
134
135/** ATAPI sense info size. */
136#define ATAPI_SENSE_SIZE 64
137
138/**
139 * Command Header.
140 */
141typedef struct
142{
143 /** Description Information. */
144 uint32_t u32DescInf;
145 /** Command status. */
146 uint32_t u32PRDBC;
147 /** Command Table Base Address. */
148 uint32_t u32CmdTblAddr;
149 /** Command Table Base Address - upper 32-bits. */
150 uint32_t u32CmdTblAddrUp;
151 /** Reserved */
152 uint32_t u32Reserved[4];
153} CmdHdr;
154AssertCompileSize(CmdHdr, 32);
155
156/* Defines for the command header. */
157#define AHCI_CMDHDR_PRDTL_MASK 0xffff0000
158#define AHCI_CMDHDR_PRDTL_ENTRIES(x) ((x & AHCI_CMDHDR_PRDTL_MASK) >> 16)
159#define AHCI_CMDHDR_C RT_BIT(10)
160#define AHCI_CMDHDR_B RT_BIT(9)
161#define AHCI_CMDHDR_R RT_BIT(8)
162#define AHCI_CMDHDR_P RT_BIT(7)
163#define AHCI_CMDHDR_W RT_BIT(6)
164#define AHCI_CMDHDR_A RT_BIT(5)
165#define AHCI_CMDHDR_CFL_MASK 0x1f
166
167#define AHCI_CMDHDR_PRDT_OFFSET 0x80
168#define AHCI_CMDHDR_ACMD_OFFSET 0x40
169
170/* Defines for the command FIS. */
171/* Defines that are used in the first double word. */
172#define AHCI_CMDFIS_TYPE 0 /* The first byte. */
173# define AHCI_CMDFIS_TYPE_H2D 0x27 /* Register - Host to Device FIS. */
174# define AHCI_CMDFIS_TYPE_H2D_SIZE 20 /* Five double words. */
175# define AHCI_CMDFIS_TYPE_D2H 0x34 /* Register - Device to Host FIS. */
176# define AHCI_CMDFIS_TYPE_D2H_SIZE 20 /* Five double words. */
177# define AHCI_CMDFIS_TYPE_SETDEVBITS 0xa1 /* Set Device Bits - Device to Host FIS. */
178# define AHCI_CMDFIS_TYPE_SETDEVBITS_SIZE 8 /* Two double words. */
179# define AHCI_CMDFIS_TYPE_DMAACTD2H 0x39 /* DMA Activate - Device to Host FIS. */
180# define AHCI_CMDFIS_TYPE_DMAACTD2H_SIZE 4 /* One double word. */
181# define AHCI_CMDFIS_TYPE_DMASETUP 0x41 /* DMA Setup - Bidirectional FIS. */
182# define AHCI_CMDFIS_TYPE_DMASETUP_SIZE 28 /* Seven double words. */
183# define AHCI_CMDFIS_TYPE_PIOSETUP 0x5f /* PIO Setup - Device to Host FIS. */
184# define AHCI_CMDFIS_TYPE_PIOSETUP_SIZE 20 /* Five double words. */
185# define AHCI_CMDFIS_TYPE_DATA 0x46 /* Data - Bidirectional FIS. */
186
187#define AHCI_CMDFIS_BITS 1 /* Interrupt and Update bit. */
188#define AHCI_CMDFIS_C RT_BIT(7) /* Host to device. */
189#define AHCI_CMDFIS_I RT_BIT(6) /* Device to Host. */
190#define AHCI_CMDFIS_D RT_BIT(5)
191
192#define AHCI_CMDFIS_CMD 2
193#define AHCI_CMDFIS_FET 3
194
195#define AHCI_CMDFIS_SECTN 4
196#define AHCI_CMDFIS_CYLL 5
197#define AHCI_CMDFIS_CYLH 6
198#define AHCI_CMDFIS_HEAD 7
199
200#define AHCI_CMDFIS_SECTNEXP 8
201#define AHCI_CMDFIS_CYLLEXP 9
202#define AHCI_CMDFIS_CYLHEXP 10
203#define AHCI_CMDFIS_FETEXP 11
204
205#define AHCI_CMDFIS_SECTC 12
206#define AHCI_CMDFIS_SECTCEXP 13
207#define AHCI_CMDFIS_CTL 15
208# define AHCI_CMDFIS_CTL_SRST RT_BIT(2) /* Reset device. */
209# define AHCI_CMDFIS_CTL_NIEN RT_BIT(1) /* Assert or clear interrupt. */
210
211/* For D2H FIS */
212#define AHCI_CMDFIS_STS 2
213#define AHCI_CMDFIS_ERR 3
214
215/** Pointer to a task state. */
216typedef struct AHCIREQ *PAHCIREQ;
217
218/** Task encountered a buffer overflow. */
219#define AHCI_REQ_OVERFLOW RT_BIT_32(0)
220/** Request is a PIO data command, if this flag is not set it either is
221 * a command which does not transfer data or a DMA command based on the transfer size. */
222#define AHCI_REQ_PIO_DATA RT_BIT_32(1)
223/** The request has the SACT register set. */
224#define AHCI_REQ_CLEAR_SACT RT_BIT_32(2)
225/** Flag whether the request is queued. */
226#define AHCI_REQ_IS_QUEUED RT_BIT_32(3)
227/** Flag whether the request is stored on the stack. */
228#define AHCI_REQ_IS_ON_STACK RT_BIT_32(4)
229/** Flag whether this request transfers data from the device to the HBA or
230 * the other way around .*/
231#define AHCI_REQ_XFER_2_HOST RT_BIT_32(5)
232
233/**
234 * A task state.
235 */
236typedef struct AHCIREQ
237{
238 /** The I/O request handle from the driver below associated with this request. */
239 PDMMEDIAEXIOREQ hIoReq;
240 /** Tag of the task. */
241 uint32_t uTag;
242 /** The command Fis for this task. */
243 uint8_t cmdFis[AHCI_CMDFIS_TYPE_H2D_SIZE];
244 /** The ATAPI command data. */
245 uint8_t aATAPICmd[ATAPI_PACKET_SIZE];
246 /** Size of one sector for the ATAPI transfer. */
247 uint32_t cbATAPISector;
248 /** Physical address of the command header. - GC */
249 RTGCPHYS GCPhysCmdHdrAddr;
250 /** Physical address of the PRDT */
251 RTGCPHYS GCPhysPrdtl;
252 /** Number of entries in the PRDTL. */
253 unsigned cPrdtlEntries;
254 /** Data direction. */
255 PDMMEDIAEXIOREQTYPE enmType;
256 /** Start offset. */
257 uint64_t uOffset;
258 /** Number of bytes to transfer. */
259 size_t cbTransfer;
260 /** Flags for this task. */
261 uint32_t fFlags;
262 /** SCSI status code. */
263 uint8_t u8ScsiSts;
264 /** Flag when the buffer is mapped. */
265 bool fMapped;
266 /** Page lock when the buffer is mapped. */
267 PGMPAGEMAPLOCK PgLck;
268} AHCIREQ;
269
270/**
271 * Notifier queue item.
272 */
273typedef struct DEVPORTNOTIFIERQUEUEITEM
274{
275 /** The core part owned by the queue manager. */
276 PDMQUEUEITEMCORE Core;
277 /** The port to process. */
278 uint8_t iPort;
279} DEVPORTNOTIFIERQUEUEITEM, *PDEVPORTNOTIFIERQUEUEITEM;
280
281
282/**
283 * The shared state of an AHCI port.
284 */
285typedef struct AHCIPORT
286{
287 /** Command List Base Address. */
288 uint32_t regCLB;
289 /** Command List Base Address upper bits. */
290 uint32_t regCLBU;
291 /** FIS Base Address. */
292 uint32_t regFB;
293 /** FIS Base Address upper bits. */
294 uint32_t regFBU;
295 /** Interrupt Status. */
296 volatile uint32_t regIS;
297 /** Interrupt Enable. */
298 uint32_t regIE;
299 /** Command. */
300 uint32_t regCMD;
301 /** Task File Data. */
302 uint32_t regTFD;
303 /** Signature */
304 uint32_t regSIG;
305 /** Serial ATA Status. */
306 uint32_t regSSTS;
307 /** Serial ATA Control. */
308 uint32_t regSCTL;
309 /** Serial ATA Error. */
310 uint32_t regSERR;
311 /** Serial ATA Active. */
312 volatile uint32_t regSACT;
313 /** Command Issue. */
314 uint32_t regCI;
315
316 /** Current number of active tasks. */
317 volatile uint32_t cTasksActive;
318 uint32_t u32Alignment1;
319 /** Command List Base Address */
320 volatile RTGCPHYS GCPhysAddrClb;
321 /** FIS Base Address */
322 volatile RTGCPHYS GCPhysAddrFb;
323
324 /** Device is powered on. */
325 bool fPoweredOn;
326 /** Device has spun up. */
327 bool fSpunUp;
328 /** First D2H FIS was sent. */
329 bool fFirstD2HFisSent;
330 /** Attached device is a CD/DVD drive. */
331 bool fATAPI;
332 /** Flag whether this port is in a reset state. */
333 volatile bool fPortReset;
334 /** Flag whether TRIM is supported. */
335 bool fTrimEnabled;
336 /** Flag if we are in a device reset. */
337 bool fResetDevice;
338 /** Flag whether this port is hot plug capable. */
339 bool fHotpluggable;
340 /** Flag whether the port is in redo task mode. */
341 volatile bool fRedo;
342 /** Flag whether the worker thread is sleeping. */
343 volatile bool fWrkThreadSleeping;
344
345 bool afAlignment1[2];
346
347 /** Number of total sectors. */
348 uint64_t cTotalSectors;
349 /** Size of one sector. */
350 uint32_t cbSector;
351 /** Currently configured number of sectors in a multi-sector transfer. */
352 uint32_t cMultSectors;
353 /** The LUN (same as port number). */
354 uint32_t iLUN;
355 /** Set if there is a device present at the port. */
356 bool fPresent;
357 /** Currently active transfer mode (MDMA/UDMA) and speed. */
358 uint8_t uATATransferMode;
359 /** Exponent of logical sectors in a physical sector, number of logical sectors is 2^exp. */
360 uint8_t cLogSectorsPerPhysicalExp;
361 uint8_t bAlignment2;
362 /** ATAPI sense data. */
363 uint8_t abATAPISense[ATAPI_SENSE_SIZE];
364
365 /** Bitmap for finished tasks (R3 -> Guest). */
366 volatile uint32_t u32TasksFinished;
367 /** Bitmap for finished queued tasks (R3 -> Guest). */
368 volatile uint32_t u32QueuedTasksFinished;
369 /** Bitmap for new queued tasks (Guest -> R3). */
370 volatile uint32_t u32TasksNew;
371 /** Bitmap of tasks which must be redone because of a non fatal error. */
372 volatile uint32_t u32TasksRedo;
373
374 /** Current command slot processed.
375 * Accessed by the guest by reading the CMD register.
376 * Holds the command slot of the command processed at the moment. */
377 volatile uint32_t u32CurrentCommandSlot;
378
379 /** Physical geometry of this image. */
380 PDMMEDIAGEOMETRY PCHSGeometry;
381
382 /** The status LED state for this drive. */
383 PDMLED Led;
384
385 /** The event semaphore the processing thread waits on. */
386 SUPSEMEVENT hEvtProcess;
387
388 /** The serial numnber to use for IDENTIFY DEVICE commands. */
389 char szSerialNumber[AHCI_SERIAL_NUMBER_LENGTH+1]; /** < one extra byte for termination */
390 /** The firmware revision to use for IDENTIFY DEVICE commands. */
391 char szFirmwareRevision[AHCI_FIRMWARE_REVISION_LENGTH+1]; /** < one extra byte for termination */
392 /** The model number to use for IDENTIFY DEVICE commands. */
393 char szModelNumber[AHCI_MODEL_NUMBER_LENGTH+1]; /** < one extra byte for termination */
394 /** The vendor identification string for SCSI INQUIRY commands. */
395 char szInquiryVendorId[AHCI_ATAPI_INQUIRY_VENDOR_ID_LENGTH+1];
396 /** The product identification string for SCSI INQUIRY commands. */
397 char szInquiryProductId[AHCI_ATAPI_INQUIRY_PRODUCT_ID_LENGTH+1];
398 /** The revision string for SCSI INQUIRY commands. */
399 char szInquiryRevision[AHCI_ATAPI_INQUIRY_REVISION_LENGTH+1];
400 /** Error counter */
401 uint32_t cErrors;
402
403 uint32_t u32Alignment5;
404} AHCIPORT;
405AssertCompileSizeAlignment(AHCIPORT, 8);
406/** Pointer to the shared state of an AHCI port. */
407typedef AHCIPORT *PAHCIPORT;
408
409
410/**
411 * The ring-3 state of an AHCI port.
412 *
413 * @implements PDMIBASE
414 * @implements PDMIMEDIAPORT
415 * @implements PDMIMEDIAEXPORT
416 */
417typedef struct AHCIPORTR3
418{
419 /** Pointer to the device instance - only to get our bearings in an interface
420 * method, nothing else. */
421 PPDMDEVINSR3 pDevIns;
422
423 /** The LUN (same as port number). */
424 uint32_t iLUN;
425
426 /** Device specific settings (R3 only stuff). */
427 /** Pointer to the attached driver's base interface. */
428 R3PTRTYPE(PPDMIBASE) pDrvBase;
429 /** Pointer to the attached driver's block interface. */
430 R3PTRTYPE(PPDMIMEDIA) pDrvMedia;
431 /** Pointer to the attached driver's extended interface. */
432 R3PTRTYPE(PPDMIMEDIAEX) pDrvMediaEx;
433 /** Port description. */
434 char szDesc[8];
435 /** The base interface. */
436 PDMIBASE IBase;
437 /** The block port interface. */
438 PDMIMEDIAPORT IPort;
439 /** The extended media port interface. */
440 PDMIMEDIAEXPORT IMediaExPort;
441
442 /** Async IO Thread. */
443 R3PTRTYPE(PPDMTHREAD) pAsyncIOThread;
444 /** First task throwing an error. */
445 R3PTRTYPE(volatile PAHCIREQ) pTaskErr;
446
447} AHCIPORTR3;
448AssertCompileSizeAlignment(AHCIPORTR3, 8);
449/** Pointer to the ring-3 state of an AHCI port. */
450typedef AHCIPORTR3 *PAHCIPORTR3;
451
452
453/**
454 * Main AHCI device state.
455 *
456 * @implements PDMILEDPORTS
457 */
458typedef struct AHCI
459{
460 /** Global Host Control register of the HBA
461 * @todo r=bird: Make this a 'name' doxygen comment with { and add a
462 * corrsponding at-} where appropriate. I cannot tell where to put the
463 * latter. */
464
465 /** HBA Capabilities - Readonly */
466 uint32_t regHbaCap;
467 /** HBA Control */
468 uint32_t regHbaCtrl;
469 /** Interrupt Status */
470 uint32_t regHbaIs;
471 /** Ports Implemented - Readonly */
472 uint32_t regHbaPi;
473 /** AHCI Version - Readonly */
474 uint32_t regHbaVs;
475 /** Command completion coalescing control */
476 uint32_t regHbaCccCtl;
477 /** Command completion coalescing ports */
478 uint32_t regHbaCccPorts;
479
480 /** Index register for BIOS access. */
481 uint32_t regIdx;
482
483 /** Countdown timer for command completion coalescing. */
484 TMTIMERHANDLE hHbaCccTimer;
485
486 /** Which port number is used to mark an CCC interrupt */
487 uint8_t uCccPortNr;
488 uint8_t abAlignment1[7];
489
490 /** Timeout value */
491 uint64_t uCccTimeout;
492 /** Number of completions used to assert an interrupt */
493 uint32_t uCccNr;
494 /** Current number of completed commands */
495 uint32_t uCccCurrentNr;
496
497 /** Register structure per port */
498 AHCIPORT aPorts[AHCI_MAX_NR_PORTS_IMPL];
499
500 /** The critical section. */
501 PDMCRITSECT lock;
502
503 /** Bitmask of ports which asserted an interrupt. */
504 volatile uint32_t u32PortsInterrupted;
505 /** Number of I/O threads currently active - used for async controller reset handling. */
506 volatile uint32_t cThreadsActive;
507
508 /** Flag whether the legacy port reset method should be used to make it work with saved states. */
509 bool fLegacyPortResetMethod;
510 /** Enable tiger (10.4.x) SSTS hack or not. */
511 bool fTigerHack;
512 /** Flag whether we have written the first 4bytes in an 8byte MMIO write successfully. */
513 volatile bool f8ByteMMIO4BytesWrittenSuccessfully;
514
515 /** Device is in a reset state.
516 * @todo r=bird: This isn't actually being modified by anyone... */
517 bool fReset;
518 /** Supports 64bit addressing
519 * @todo r=bird: This isn't really being modified by anyone (always false). */
520 bool f64BitAddr;
521 /** Flag whether the controller has BIOS access enabled.
522 * @todo r=bird: Not used, just queried from CFGM. */
523 bool fBootable;
524
525 bool afAlignment2[2];
526
527 /** Number of usable ports on this controller. */
528 uint32_t cPortsImpl;
529 /** Number of usable command slots for each port. */
530 uint32_t cCmdSlotsAvail;
531
532 /** PCI region \#0: Legacy IDE fake, 8 ports. */
533 IOMIOPORTHANDLE hIoPortsLegacyFake0;
534 /** PCI region \#1: Legacy IDE fake, 1 port. */
535 IOMIOPORTHANDLE hIoPortsLegacyFake1;
536 /** PCI region \#2: Legacy IDE fake, 8 ports. */
537 IOMIOPORTHANDLE hIoPortsLegacyFake2;
538 /** PCI region \#3: Legacy IDE fake, 1 port. */
539 IOMIOPORTHANDLE hIoPortsLegacyFake3;
540 /** PCI region \#4: BMDMA I/O port range, 16 ports, used for the Index/Data
541 * pair register access. */
542 IOMIOPORTHANDLE hIoPortIdxData;
543 /** PCI region \#5: MMIO registers. */
544 IOMMMIOHANDLE hMmio;
545} AHCI;
546AssertCompileMemberAlignment(AHCI, aPorts, 8);
547/** Pointer to the state of an AHCI device. */
548typedef AHCI *PAHCI;
549
550
551/**
552 * Main AHCI device ring-3 state.
553 *
554 * @implements PDMILEDPORTS
555 */
556typedef struct AHCIR3
557{
558 /** Pointer to the device instance - only for getting our bearings in
559 * interface methods. */
560 PPDMDEVINSR3 pDevIns;
561
562 /** Status LUN: The base interface. */
563 PDMIBASE IBase;
564 /** Status LUN: Leds interface. */
565 PDMILEDPORTS ILeds;
566 /** Status LUN: Partner of ILeds. */
567 R3PTRTYPE(PPDMILEDCONNECTORS) pLedsConnector;
568 /** Status LUN: Media Notifys. */
569 R3PTRTYPE(PPDMIMEDIANOTIFY) pMediaNotify;
570
571 /** Register structure per port */
572 AHCIPORTR3 aPorts[AHCI_MAX_NR_PORTS_IMPL];
573
574 /** Indicates that PDMDevHlpAsyncNotificationCompleted should be called when
575 * a port is entering the idle state. */
576 bool volatile fSignalIdle;
577 bool afAlignment7[2+4];
578} AHCIR3;
579/** Pointer to the ring-3 state of an AHCI device. */
580typedef AHCIR3 *PAHCIR3;
581
582
583/**
584 * Main AHCI device ring-0 state.
585 */
586typedef struct AHCIR0
587{
588 uint64_t uUnused;
589} AHCIR0;
590/** Pointer to the ring-0 state of an AHCI device. */
591typedef AHCIR0 *PAHCIR0;
592
593
594/**
595 * Main AHCI device raw-mode state.
596 */
597typedef struct AHCIRC
598{
599 uint64_t uUnused;
600} AHCIRC;
601/** Pointer to the raw-mode state of an AHCI device. */
602typedef AHCIRC *PAHCIRC;
603
604
605/** Main AHCI device current context state. */
606typedef CTX_SUFF(AHCI) AHCICC;
607/** Pointer to the current context state of an AHCI device. */
608typedef CTX_SUFF(PAHCI) PAHCICC;
609
610
611/**
612 * Scatter gather list entry.
613 */
614typedef struct
615{
616 /** Data Base Address. */
617 uint32_t u32DBA;
618 /** Data Base Address - Upper 32-bits. */
619 uint32_t u32DBAUp;
620 /** Reserved */
621 uint32_t u32Reserved;
622 /** Description information. */
623 uint32_t u32DescInf;
624} SGLEntry;
625AssertCompileSize(SGLEntry, 16);
626
627#ifdef IN_RING3
628/**
629 * Memory buffer callback.
630 *
631 * @returns nothing.
632 * @param pDevIns The device instance.
633 * @param GCPhys The guest physical address of the memory buffer.
634 * @param pSgBuf The pointer to the host R3 S/G buffer.
635 * @param cbCopy How many bytes to copy between the two buffers.
636 * @param pcbSkip Initially contains the amount of bytes to skip
637 * starting from the guest physical address before
638 * accessing the S/G buffer and start copying data.
639 * On return this contains the remaining amount if
640 * cbCopy < *pcbSkip or 0 otherwise.
641 */
642typedef DECLCALLBACK(void) AHCIR3MEMCOPYCALLBACK(PPDMDEVINS pDevIns, RTGCPHYS GCPhys,
643 PRTSGBUF pSgBuf, size_t cbCopy, size_t *pcbSkip);
644/** Pointer to a memory copy buffer callback. */
645typedef AHCIR3MEMCOPYCALLBACK *PAHCIR3MEMCOPYCALLBACK;
646#endif
647
648/** Defines for a scatter gather list entry. */
649#define SGLENTRY_DBA_READONLY ~(RT_BIT(0))
650#define SGLENTRY_DESCINF_I RT_BIT(31)
651#define SGLENTRY_DESCINF_DBC 0x3fffff
652#define SGLENTRY_DESCINF_READONLY 0x803fffff
653
654/* Defines for the global host control registers for the HBA. */
655
656#define AHCI_HBA_GLOBAL_SIZE 0x100
657
658/* Defines for the HBA Capabilities - Readonly */
659#define AHCI_HBA_CAP_S64A RT_BIT(31)
660#define AHCI_HBA_CAP_SNCQ RT_BIT(30)
661#define AHCI_HBA_CAP_SIS RT_BIT(28)
662#define AHCI_HBA_CAP_SSS RT_BIT(27)
663#define AHCI_HBA_CAP_SALP RT_BIT(26)
664#define AHCI_HBA_CAP_SAL RT_BIT(25)
665#define AHCI_HBA_CAP_SCLO RT_BIT(24)
666#define AHCI_HBA_CAP_ISS (RT_BIT(23) | RT_BIT(22) | RT_BIT(21) | RT_BIT(20))
667# define AHCI_HBA_CAP_ISS_SHIFT(x) (((x) << 20) & AHCI_HBA_CAP_ISS)
668# define AHCI_HBA_CAP_ISS_GEN1 RT_BIT(0)
669# define AHCI_HBA_CAP_ISS_GEN2 RT_BIT(1)
670#define AHCI_HBA_CAP_SNZO RT_BIT(19)
671#define AHCI_HBA_CAP_SAM RT_BIT(18)
672#define AHCI_HBA_CAP_SPM RT_BIT(17)
673#define AHCI_HBA_CAP_PMD RT_BIT(15)
674#define AHCI_HBA_CAP_SSC RT_BIT(14)
675#define AHCI_HBA_CAP_PSC RT_BIT(13)
676#define AHCI_HBA_CAP_NCS (RT_BIT(12) | RT_BIT(11) | RT_BIT(10) | RT_BIT(9) | RT_BIT(8))
677#define AHCI_HBA_CAP_NCS_SET(x) (((x-1) << 8) & AHCI_HBA_CAP_NCS) /* 0's based */
678#define AHCI_HBA_CAP_CCCS RT_BIT(7)
679#define AHCI_HBA_CAP_NP (RT_BIT(4) | RT_BIT(3) | RT_BIT(2) | RT_BIT(1) | RT_BIT(0))
680#define AHCI_HBA_CAP_NP_SET(x) ((x-1) & AHCI_HBA_CAP_NP) /* 0's based */
681
682/* Defines for the HBA Control register - Read/Write */
683#define AHCI_HBA_CTRL_AE RT_BIT(31)
684#define AHCI_HBA_CTRL_IE RT_BIT(1)
685#define AHCI_HBA_CTRL_HR RT_BIT(0)
686#define AHCI_HBA_CTRL_RW_MASK (RT_BIT(0) | RT_BIT(1)) /* Mask for the used bits */
687
688/* Defines for the HBA Version register - Readonly (We support AHCI 1.0) */
689#define AHCI_HBA_VS_MJR (1 << 16)
690#define AHCI_HBA_VS_MNR 0x100
691
692/* Defines for the command completion coalescing control register */
693#define AHCI_HBA_CCC_CTL_TV 0xffff0000
694#define AHCI_HBA_CCC_CTL_TV_SET(x) (x << 16)
695#define AHCI_HBA_CCC_CTL_TV_GET(x) ((x & AHCI_HBA_CCC_CTL_TV) >> 16)
696
697#define AHCI_HBA_CCC_CTL_CC 0xff00
698#define AHCI_HBA_CCC_CTL_CC_SET(x) (x << 8)
699#define AHCI_HBA_CCC_CTL_CC_GET(x) ((x & AHCI_HBA_CCC_CTL_CC) >> 8)
700
701#define AHCI_HBA_CCC_CTL_INT 0xf8
702#define AHCI_HBA_CCC_CTL_INT_SET(x) (x << 3)
703#define AHCI_HBA_CCC_CTL_INT_GET(x) ((x & AHCI_HBA_CCC_CTL_INT) >> 3)
704
705#define AHCI_HBA_CCC_CTL_EN RT_BIT(0)
706
707/* Defines for the port registers. */
708
709#define AHCI_PORT_REGISTER_SIZE 0x80
710
711#define AHCI_PORT_CLB_RESERVED 0xfffffc00 /* For masking out the reserved bits. */
712
713#define AHCI_PORT_FB_RESERVED 0xffffff00 /* For masking out the reserved bits. */
714
715#define AHCI_PORT_IS_CPDS RT_BIT(31)
716#define AHCI_PORT_IS_TFES RT_BIT(30)
717#define AHCI_PORT_IS_HBFS RT_BIT(29)
718#define AHCI_PORT_IS_HBDS RT_BIT(28)
719#define AHCI_PORT_IS_IFS RT_BIT(27)
720#define AHCI_PORT_IS_INFS RT_BIT(26)
721#define AHCI_PORT_IS_OFS RT_BIT(24)
722#define AHCI_PORT_IS_IPMS RT_BIT(23)
723#define AHCI_PORT_IS_PRCS RT_BIT(22)
724#define AHCI_PORT_IS_DIS RT_BIT(7)
725#define AHCI_PORT_IS_PCS RT_BIT(6)
726#define AHCI_PORT_IS_DPS RT_BIT(5)
727#define AHCI_PORT_IS_UFS RT_BIT(4)
728#define AHCI_PORT_IS_SDBS RT_BIT(3)
729#define AHCI_PORT_IS_DSS RT_BIT(2)
730#define AHCI_PORT_IS_PSS RT_BIT(1)
731#define AHCI_PORT_IS_DHRS RT_BIT(0)
732#define AHCI_PORT_IS_READONLY 0xfd8000af /* Readonly mask including reserved bits. */
733
734#define AHCI_PORT_IE_CPDE RT_BIT(31)
735#define AHCI_PORT_IE_TFEE RT_BIT(30)
736#define AHCI_PORT_IE_HBFE RT_BIT(29)
737#define AHCI_PORT_IE_HBDE RT_BIT(28)
738#define AHCI_PORT_IE_IFE RT_BIT(27)
739#define AHCI_PORT_IE_INFE RT_BIT(26)
740#define AHCI_PORT_IE_OFE RT_BIT(24)
741#define AHCI_PORT_IE_IPME RT_BIT(23)
742#define AHCI_PORT_IE_PRCE RT_BIT(22)
743#define AHCI_PORT_IE_DIE RT_BIT(7) /* Not supported for now, readonly. */
744#define AHCI_PORT_IE_PCE RT_BIT(6)
745#define AHCI_PORT_IE_DPE RT_BIT(5)
746#define AHCI_PORT_IE_UFE RT_BIT(4)
747#define AHCI_PORT_IE_SDBE RT_BIT(3)
748#define AHCI_PORT_IE_DSE RT_BIT(2)
749#define AHCI_PORT_IE_PSE RT_BIT(1)
750#define AHCI_PORT_IE_DHRE RT_BIT(0)
751#define AHCI_PORT_IE_READONLY (0xfdc000ff) /* Readonly mask including reserved bits. */
752
753#define AHCI_PORT_CMD_ICC (RT_BIT(28) | RT_BIT(29) | RT_BIT(30) | RT_BIT(31))
754#define AHCI_PORT_CMD_ICC_SHIFT(x) ((x) << 28)
755# define AHCI_PORT_CMD_ICC_IDLE 0x0
756# define AHCI_PORT_CMD_ICC_ACTIVE 0x1
757# define AHCI_PORT_CMD_ICC_PARTIAL 0x2
758# define AHCI_PORT_CMD_ICC_SLUMBER 0x6
759#define AHCI_PORT_CMD_ASP RT_BIT(27) /* Not supported - Readonly */
760#define AHCI_PORT_CMD_ALPE RT_BIT(26) /* Not supported - Readonly */
761#define AHCI_PORT_CMD_DLAE RT_BIT(25)
762#define AHCI_PORT_CMD_ATAPI RT_BIT(24)
763#define AHCI_PORT_CMD_CPD RT_BIT(20)
764#define AHCI_PORT_CMD_ISP RT_BIT(19) /* Readonly */
765#define AHCI_PORT_CMD_HPCP RT_BIT(18)
766#define AHCI_PORT_CMD_PMA RT_BIT(17) /* Not supported - Readonly */
767#define AHCI_PORT_CMD_CPS RT_BIT(16)
768#define AHCI_PORT_CMD_CR RT_BIT(15) /* Readonly */
769#define AHCI_PORT_CMD_FR RT_BIT(14) /* Readonly */
770#define AHCI_PORT_CMD_ISS RT_BIT(13) /* Readonly */
771#define AHCI_PORT_CMD_CCS (RT_BIT(8) | RT_BIT(9) | RT_BIT(10) | RT_BIT(11) | RT_BIT(12))
772#define AHCI_PORT_CMD_CCS_SHIFT(x) (x << 8) /* Readonly */
773#define AHCI_PORT_CMD_FRE RT_BIT(4)
774#define AHCI_PORT_CMD_CLO RT_BIT(3)
775#define AHCI_PORT_CMD_POD RT_BIT(2)
776#define AHCI_PORT_CMD_SUD RT_BIT(1)
777#define AHCI_PORT_CMD_ST RT_BIT(0)
778#define AHCI_PORT_CMD_READONLY (0xff02001f & ~(AHCI_PORT_CMD_ASP | AHCI_PORT_CMD_ALPE | AHCI_PORT_CMD_PMA))
779
780#define AHCI_PORT_SCTL_IPM (RT_BIT(11) | RT_BIT(10) | RT_BIT(9) | RT_BIT(8))
781#define AHCI_PORT_SCTL_IPM_GET(x) ((x & AHCI_PORT_SCTL_IPM) >> 8)
782#define AHCI_PORT_SCTL_SPD (RT_BIT(7) | RT_BIT(6) | RT_BIT(5) | RT_BIT(4))
783#define AHCI_PORT_SCTL_SPD_GET(x) ((x & AHCI_PORT_SCTL_SPD) >> 4)
784#define AHCI_PORT_SCTL_DET (RT_BIT(3) | RT_BIT(2) | RT_BIT(1) | RT_BIT(0))
785#define AHCI_PORT_SCTL_DET_GET(x) (x & AHCI_PORT_SCTL_DET)
786#define AHCI_PORT_SCTL_DET_NINIT 0
787#define AHCI_PORT_SCTL_DET_INIT 1
788#define AHCI_PORT_SCTL_DET_OFFLINE 4
789#define AHCI_PORT_SCTL_READONLY 0xfff
790
791#define AHCI_PORT_SSTS_IPM (RT_BIT(11) | RT_BIT(10) | RT_BIT(9) | RT_BIT(8))
792#define AHCI_PORT_SSTS_IPM_GET(x) ((x & AHCI_PORT_SCTL_IPM) >> 8)
793#define AHCI_PORT_SSTS_SPD (RT_BIT(7) | RT_BIT(6) | RT_BIT(5) | RT_BIT(4))
794#define AHCI_PORT_SSTS_SPD_GET(x) ((x & AHCI_PORT_SCTL_SPD) >> 4)
795#define AHCI_PORT_SSTS_DET (RT_BIT(3) | RT_BIT(2) | RT_BIT(1) | RT_BIT(0))
796#define AHCI_PORT_SSTS_DET_GET(x) (x & AHCI_PORT_SCTL_DET)
797
798#define AHCI_PORT_TFD_BSY RT_BIT(7)
799#define AHCI_PORT_TFD_DRQ RT_BIT(3)
800#define AHCI_PORT_TFD_ERR RT_BIT(0)
801
802#define AHCI_PORT_SERR_X RT_BIT(26)
803#define AHCI_PORT_SERR_W RT_BIT(18)
804#define AHCI_PORT_SERR_N RT_BIT(16)
805
806/* Signatures for attached storage devices. */
807#define AHCI_PORT_SIG_DISK 0x00000101
808#define AHCI_PORT_SIG_ATAPI 0xeb140101
809
810/*
811 * The AHCI spec defines an area of memory where the HBA posts received FIS's from the device.
812 * regFB points to the base of this area.
813 * Every FIS type has an offset where it is posted in this area.
814 */
815#define AHCI_RECFIS_DSFIS_OFFSET 0x00 /* DMA Setup FIS */
816#define AHCI_RECFIS_PSFIS_OFFSET 0x20 /* PIO Setup FIS */
817#define AHCI_RECFIS_RFIS_OFFSET 0x40 /* D2H Register FIS */
818#define AHCI_RECFIS_SDBFIS_OFFSET 0x58 /* Set Device Bits FIS */
819#define AHCI_RECFIS_UFIS_OFFSET 0x60 /* Unknown FIS type */
820
821/** Mask to get the LBA value from a LBA range. */
822#define AHCI_RANGE_LBA_MASK UINT64_C(0xffffffffffff)
823/** Mas to get the length value from a LBA range. */
824#define AHCI_RANGE_LENGTH_MASK UINT64_C(0xffff000000000000)
825/** Returns the length of the range in sectors. */
826#define AHCI_RANGE_LENGTH_GET(val) (((val) & AHCI_RANGE_LENGTH_MASK) >> 48)
827
828/**
829 * AHCI register operator.
830 */
831typedef struct ahci_opreg
832{
833 const char *pszName;
834 VBOXSTRICTRC (*pfnRead )(PPDMDEVINS pDevIns, PAHCI pThis, uint32_t iReg, uint32_t *pu32Value);
835 VBOXSTRICTRC (*pfnWrite)(PPDMDEVINS pDevIns, PAHCI pThis, uint32_t iReg, uint32_t u32Value);
836} AHCIOPREG;
837
838/**
839 * AHCI port register operator.
840 */
841typedef struct pAhciPort_opreg
842{
843 const char *pszName;
844 VBOXSTRICTRC (*pfnRead )(PPDMDEVINS pDevIns, PAHCI pThis, PAHCIPORT pAhciPort, uint32_t iReg, uint32_t *pu32Value);
845 VBOXSTRICTRC (*pfnWrite)(PPDMDEVINS pDevIns, PAHCI pThis, PAHCIPORT pAhciPort, uint32_t iReg, uint32_t u32Value);
846} AHCIPORTOPREG;
847
848
849/*********************************************************************************************************************************
850* Internal Functions *
851*********************************************************************************************************************************/
852#ifndef VBOX_DEVICE_STRUCT_TESTCASE
853RT_C_DECLS_BEGIN
854#ifdef IN_RING3
855static void ahciR3HBAReset(PPDMDEVINS pDevIns, PAHCI pThis, PAHCIR3 pThisCC);
856static int ahciPostFisIntoMemory(PPDMDEVINS pDevIns, PAHCIPORT pAhciPort, unsigned uFisType, uint8_t *pCmdFis);
857static void ahciPostFirstD2HFisIntoMemory(PPDMDEVINS pDevIns, PAHCIPORT pAhciPort);
858static size_t ahciR3CopyBufferToPrdtl(PPDMDEVINS pDevIns, PAHCIREQ pAhciReq, const void *pvSrc, size_t cbSrc, size_t cbSkip);
859static bool ahciR3CancelActiveTasks(PAHCIPORTR3 pAhciPortR3);
860#endif
861RT_C_DECLS_END
862
863
864/*********************************************************************************************************************************
865* Defined Constants And Macros *
866*********************************************************************************************************************************/
867#define AHCI_RTGCPHYS_FROM_U32(Hi, Lo) ( (RTGCPHYS)RT_MAKE_U64(Lo, Hi) )
868
869#ifdef IN_RING3
870
871# ifdef LOG_USE_C99
872# define ahciLog(a) \
873 Log(("R3 P%u: %M", pAhciPort->iLUN, _LogRelRemoveParentheseis a))
874# else
875# define ahciLog(a) \
876 do { Log(("R3 P%u: ", pAhciPort->iLUN)); Log(a); } while(0)
877# endif
878
879#elif defined(IN_RING0)
880
881# ifdef LOG_USE_C99
882# define ahciLog(a) \
883 Log(("R0 P%u: %M", pAhciPort->iLUN, _LogRelRemoveParentheseis a))
884# else
885# define ahciLog(a) \
886 do { Log(("R0 P%u: ", pAhciPort->iLUN)); Log(a); } while(0)
887# endif
888
889#elif defined(IN_RC)
890
891# ifdef LOG_USE_C99
892# define ahciLog(a) \
893 Log(("GC P%u: %M", pAhciPort->iLUN, _LogRelRemoveParentheseis a))
894# else
895# define ahciLog(a) \
896 do { Log(("GC P%u: ", pAhciPort->iLUN)); Log(a); } while(0)
897# endif
898
899#endif
900
901
902
903/**
904 * Update PCI IRQ levels
905 */
906static void ahciHbaClearInterrupt(PPDMDEVINS pDevIns)
907{
908 Log(("%s: Clearing interrupt\n", __FUNCTION__));
909 PDMDevHlpPCISetIrq(pDevIns, 0, 0);
910}
911
912/**
913 * Updates the IRQ level and sets port bit in the global interrupt status register of the HBA.
914 */
915static int ahciHbaSetInterrupt(PPDMDEVINS pDevIns, PAHCI pThis, uint8_t iPort, int rcBusy)
916{
917 Log(("P%u: %s: Setting interrupt\n", iPort, __FUNCTION__));
918
919 int rc = PDMDevHlpCritSectEnter(pDevIns, &pThis->lock, rcBusy);
920 if (rc != VINF_SUCCESS)
921 return rc;
922
923 if (pThis->regHbaCtrl & AHCI_HBA_CTRL_IE)
924 {
925 if ((pThis->regHbaCccCtl & AHCI_HBA_CCC_CTL_EN) && (pThis->regHbaCccPorts & (1 << iPort)))
926 {
927 pThis->uCccCurrentNr++;
928 if (pThis->uCccCurrentNr >= pThis->uCccNr)
929 {
930 /* Reset command completion coalescing state. */
931 PDMDevHlpTimerSetMillies(pDevIns, pThis->hHbaCccTimer, pThis->uCccTimeout);
932 pThis->uCccCurrentNr = 0;
933
934 pThis->u32PortsInterrupted |= (1 << pThis->uCccPortNr);
935 if (!(pThis->u32PortsInterrupted & ~(1 << pThis->uCccPortNr)))
936 {
937 Log(("P%u: %s: Fire interrupt\n", iPort, __FUNCTION__));
938 PDMDevHlpPCISetIrq(pDevIns, 0, 1);
939 }
940 }
941 }
942 else
943 {
944 /* If only the bit of the actual port is set assert an interrupt
945 * because the interrupt status register was already read by the guest
946 * and we need to send a new notification.
947 * Otherwise an interrupt is still pending.
948 */
949 ASMAtomicOrU32((volatile uint32_t *)&pThis->u32PortsInterrupted, (1 << iPort));
950 if (!(pThis->u32PortsInterrupted & ~(1 << iPort)))
951 {
952 Log(("P%u: %s: Fire interrupt\n", iPort, __FUNCTION__));
953 PDMDevHlpPCISetIrq(pDevIns, 0, 1);
954 }
955 }
956 }
957
958 PDMDevHlpCritSectLeave(pDevIns, &pThis->lock);
959 return VINF_SUCCESS;
960}
961
962#ifdef IN_RING3
963
964/**
965 * @callback_method_impl{FNTMTIMERDEV, Assert irq when an CCC timeout occurs.}
966 */
967static DECLCALLBACK(void) ahciCccTimer(PPDMDEVINS pDevIns, PTMTIMER pTimer, void *pvUser)
968{
969 RT_NOREF(pDevIns, pTimer);
970 PAHCI pThis = (PAHCI)pvUser;
971
972 int rc = ahciHbaSetInterrupt(pDevIns, pThis, pThis->uCccPortNr, VERR_IGNORED);
973 AssertRC(rc);
974}
975
976/**
977 * Finishes the port reset of the given port.
978 *
979 * @returns nothing.
980 * @param pDevIns The device instance.
981 * @param pThis The shared AHCI state.
982 * @param pAhciPort The port to finish the reset on, shared bits.
983 * @param pAhciPortR3 The port to finish the reset on, ring-3 bits.
984 */
985static void ahciPortResetFinish(PPDMDEVINS pDevIns, PAHCI pThis, PAHCIPORT pAhciPort, PAHCIPORTR3 pAhciPortR3)
986{
987 ahciLog(("%s: Initiated.\n", __FUNCTION__));
988
989 /* Cancel all tasks first. */
990 bool fAllTasksCanceled = ahciR3CancelActiveTasks(pAhciPortR3);
991 Assert(fAllTasksCanceled); NOREF(fAllTasksCanceled);
992
993 /* Signature for SATA device. */
994 if (pAhciPort->fATAPI)
995 pAhciPort->regSIG = AHCI_PORT_SIG_ATAPI;
996 else
997 pAhciPort->regSIG = AHCI_PORT_SIG_DISK;
998
999 /* We received a COMINIT from the device. Tell the guest. */
1000 ASMAtomicOrU32(&pAhciPort->regIS, AHCI_PORT_IS_PCS);
1001 pAhciPort->regSERR |= AHCI_PORT_SERR_X;
1002 pAhciPort->regTFD |= ATA_STAT_BUSY;
1003
1004 if ((pAhciPort->regCMD & AHCI_PORT_CMD_FRE) && (!pAhciPort->fFirstD2HFisSent))
1005 {
1006 ahciPostFirstD2HFisIntoMemory(pDevIns, pAhciPort);
1007 ASMAtomicOrU32(&pAhciPort->regIS, AHCI_PORT_IS_DHRS);
1008
1009 if (pAhciPort->regIE & AHCI_PORT_IE_DHRE)
1010 {
1011 int rc = ahciHbaSetInterrupt(pDevIns, pThis, pAhciPort->iLUN, VERR_IGNORED);
1012 AssertRC(rc);
1013 }
1014 }
1015
1016 pAhciPort->regSSTS = (0x01 << 8) /* Interface is active. */
1017 | (0x03 << 0); /* Device detected and communication established. */
1018
1019 /*
1020 * Use the maximum allowed speed.
1021 * (Not that it changes anything really)
1022 */
1023 switch (AHCI_PORT_SCTL_SPD_GET(pAhciPort->regSCTL))
1024 {
1025 case 0x01:
1026 pAhciPort->regSSTS |= (0x01 << 4); /* Generation 1 (1.5GBps) speed. */
1027 break;
1028 case 0x02:
1029 case 0x00:
1030 default:
1031 pAhciPort->regSSTS |= (0x02 << 4); /* Generation 2 (3.0GBps) speed. */
1032 break;
1033 }
1034
1035 ASMAtomicXchgBool(&pAhciPort->fPortReset, false);
1036}
1037
1038#endif /* IN_RING3 */
1039
1040/**
1041 * Kicks the I/O thread from RC or R0.
1042 *
1043 * @returns nothing.
1044 * @param pDevIns The device instance.
1045 * @param pAhciPort The port to kick.
1046 */
1047static void ahciIoThreadKick(PPDMDEVINS pDevIns, PAHCIPORT pAhciPort)
1048{
1049 LogFlowFunc(("Signal event semaphore\n"));
1050 int rc = PDMDevHlpSUPSemEventSignal(pDevIns, pAhciPort->hEvtProcess);
1051 AssertRC(rc);
1052}
1053
1054static VBOXSTRICTRC PortCmdIssue_w(PPDMDEVINS pDevIns, PAHCI pThis, PAHCIPORT pAhciPort, uint32_t iReg, uint32_t u32Value)
1055{
1056 ahciLog(("%s: write u32Value=%#010x\n", __FUNCTION__, u32Value));
1057 RT_NOREF(pThis, iReg);
1058
1059 /* Update the CI register first. */
1060 uint32_t uCIValue = ASMAtomicXchgU32(&pAhciPort->u32TasksFinished, 0);
1061 pAhciPort->regCI &= ~uCIValue;
1062
1063 if ( (pAhciPort->regCMD & AHCI_PORT_CMD_CR)
1064 && u32Value > 0)
1065 {
1066 /*
1067 * Clear all tasks which are already marked as busy. The guest
1068 * shouldn't write already busy tasks actually.
1069 */
1070 u32Value &= ~pAhciPort->regCI;
1071
1072 ASMAtomicOrU32(&pAhciPort->u32TasksNew, u32Value);
1073
1074 /* Send a notification to R3 if u32TasksNew was 0 before our write. */
1075 if (ASMAtomicReadBool(&pAhciPort->fWrkThreadSleeping))
1076 ahciIoThreadKick(pDevIns, pAhciPort);
1077 else
1078 ahciLog(("%s: Worker thread busy, no need to kick.\n", __FUNCTION__));
1079 }
1080 else
1081 ahciLog(("%s: Nothing to do (CMD=%08x).\n", __FUNCTION__, pAhciPort->regCMD));
1082
1083 pAhciPort->regCI |= u32Value;
1084
1085 return VINF_SUCCESS;
1086}
1087
1088static VBOXSTRICTRC PortCmdIssue_r(PPDMDEVINS pDevIns, PAHCI pThis, PAHCIPORT pAhciPort, uint32_t iReg, uint32_t *pu32Value)
1089{
1090 RT_NOREF(pDevIns, pThis, iReg);
1091
1092 uint32_t uCIValue = ASMAtomicXchgU32(&pAhciPort->u32TasksFinished, 0);
1093 ahciLog(("%s: read regCI=%#010x uCIValue=%#010x\n", __FUNCTION__, pAhciPort->regCI, uCIValue));
1094
1095 pAhciPort->regCI &= ~uCIValue;
1096 *pu32Value = pAhciPort->regCI;
1097
1098 return VINF_SUCCESS;
1099}
1100
1101static VBOXSTRICTRC PortSActive_w(PPDMDEVINS pDevIns, PAHCI pThis, PAHCIPORT pAhciPort, uint32_t iReg, uint32_t u32Value)
1102{
1103 ahciLog(("%s: write u32Value=%#010x\n", __FUNCTION__, u32Value));
1104 RT_NOREF(pDevIns, pThis, iReg);
1105
1106 pAhciPort->regSACT |= u32Value;
1107
1108 return VINF_SUCCESS;
1109}
1110
1111static VBOXSTRICTRC PortSActive_r(PPDMDEVINS pDevIns, PAHCI pThis, PAHCIPORT pAhciPort, uint32_t iReg, uint32_t *pu32Value)
1112{
1113 RT_NOREF(pDevIns, pThis, iReg);
1114
1115 uint32_t u32TasksFinished = ASMAtomicXchgU32(&pAhciPort->u32QueuedTasksFinished, 0);
1116 pAhciPort->regSACT &= ~u32TasksFinished;
1117
1118 ahciLog(("%s: read regSACT=%#010x regCI=%#010x u32TasksFinished=%#010x\n",
1119 __FUNCTION__, pAhciPort->regSACT, pAhciPort->regCI, u32TasksFinished));
1120
1121 *pu32Value = pAhciPort->regSACT;
1122
1123 return VINF_SUCCESS;
1124}
1125
1126static VBOXSTRICTRC PortSError_w(PPDMDEVINS pDevIns, PAHCI pThis, PAHCIPORT pAhciPort, uint32_t iReg, uint32_t u32Value)
1127{
1128 RT_NOREF(pDevIns, pThis, iReg);
1129 ahciLog(("%s: write u32Value=%#010x\n", __FUNCTION__, u32Value));
1130
1131 if ( (u32Value & AHCI_PORT_SERR_X)
1132 && (pAhciPort->regSERR & AHCI_PORT_SERR_X))
1133 {
1134 ASMAtomicAndU32(&pAhciPort->regIS, ~AHCI_PORT_IS_PCS);
1135 pAhciPort->regTFD |= ATA_STAT_ERR;
1136 pAhciPort->regTFD &= ~(ATA_STAT_DRQ | ATA_STAT_BUSY);
1137 }
1138
1139 if ( (u32Value & AHCI_PORT_SERR_N)
1140 && (pAhciPort->regSERR & AHCI_PORT_SERR_N))
1141 ASMAtomicAndU32(&pAhciPort->regIS, ~AHCI_PORT_IS_PRCS);
1142
1143 pAhciPort->regSERR &= ~u32Value;
1144
1145 return VINF_SUCCESS;
1146}
1147
1148static VBOXSTRICTRC PortSError_r(PPDMDEVINS pDevIns, PAHCI pThis, PAHCIPORT pAhciPort, uint32_t iReg, uint32_t *pu32Value)
1149{
1150 RT_NOREF(pDevIns, pThis, iReg);
1151 ahciLog(("%s: read regSERR=%#010x\n", __FUNCTION__, pAhciPort->regSERR));
1152 *pu32Value = pAhciPort->regSERR;
1153 return VINF_SUCCESS;
1154}
1155
1156static VBOXSTRICTRC PortSControl_w(PPDMDEVINS pDevIns, PAHCI pThis, PAHCIPORT pAhciPort, uint32_t iReg, uint32_t u32Value)
1157{
1158 RT_NOREF(pThis, iReg);
1159 ahciLog(("%s: write u32Value=%#010x\n", __FUNCTION__, u32Value));
1160 ahciLog(("%s: IPM=%d SPD=%d DET=%d\n", __FUNCTION__,
1161 AHCI_PORT_SCTL_IPM_GET(u32Value), AHCI_PORT_SCTL_SPD_GET(u32Value), AHCI_PORT_SCTL_DET_GET(u32Value)));
1162
1163#ifndef IN_RING3
1164 RT_NOREF(pDevIns, pAhciPort, u32Value);
1165 return VINF_IOM_R3_MMIO_WRITE;
1166#else
1167 if ((u32Value & AHCI_PORT_SCTL_DET) == AHCI_PORT_SCTL_DET_INIT)
1168 {
1169 if (!ASMAtomicXchgBool(&pAhciPort->fPortReset, true))
1170 LogRel(("AHCI#%u: Port %d reset\n", pDevIns->iInstance,
1171 pAhciPort->iLUN));
1172
1173 pAhciPort->regSSTS = 0;
1174 pAhciPort->regSIG = UINT32_MAX;
1175 pAhciPort->regTFD = 0x7f;
1176 pAhciPort->fFirstD2HFisSent = false;
1177 pAhciPort->regSCTL = u32Value;
1178 }
1179 else if ( (u32Value & AHCI_PORT_SCTL_DET) == AHCI_PORT_SCTL_DET_NINIT
1180 && (pAhciPort->regSCTL & AHCI_PORT_SCTL_DET) == AHCI_PORT_SCTL_DET_INIT
1181 && pAhciPort->fPresent)
1182 {
1183 /* Do the port reset here, so the guest sees the new status immediately. */
1184 if (pThis->fLegacyPortResetMethod)
1185 {
1186 PAHCIR3 pThisCC = PDMDEVINS_2_DATA_CC(pDevIns, PAHCICC);
1187 PAHCIPORTR3 pAhciPortR3 = &RT_SAFE_SUBSCRIPT(pThisCC->aPorts, pAhciPort->iLUN);
1188 ahciPortResetFinish(pDevIns, pThis, pAhciPort, pAhciPortR3);
1189 pAhciPort->regSCTL = u32Value; /* Update after finishing the reset, so the I/O thread doesn't get a chance to do the reset. */
1190 }
1191 else
1192 {
1193 if (!pThis->fTigerHack)
1194 pAhciPort->regSSTS = 0x1; /* Indicate device presence detected but communication not established. */
1195 else
1196 pAhciPort->regSSTS = 0x0; /* Indicate no device detected after COMRESET. [tiger hack] */
1197 pAhciPort->regSCTL = u32Value; /* Update before kicking the I/O thread. */
1198
1199 /* Kick the thread to finish the reset. */
1200 ahciIoThreadKick(pDevIns, pAhciPort);
1201 }
1202 }
1203 else /* Just update the value if there is no device attached. */
1204 pAhciPort->regSCTL = u32Value;
1205
1206 return VINF_SUCCESS;
1207#endif
1208}
1209
1210static VBOXSTRICTRC PortSControl_r(PPDMDEVINS pDevIns, PAHCI pThis, PAHCIPORT pAhciPort, uint32_t iReg, uint32_t *pu32Value)
1211{
1212 RT_NOREF(pDevIns, pThis, iReg);
1213 ahciLog(("%s: read regSCTL=%#010x\n", __FUNCTION__, pAhciPort->regSCTL));
1214 ahciLog(("%s: IPM=%d SPD=%d DET=%d\n", __FUNCTION__,
1215 AHCI_PORT_SCTL_IPM_GET(pAhciPort->regSCTL), AHCI_PORT_SCTL_SPD_GET(pAhciPort->regSCTL),
1216 AHCI_PORT_SCTL_DET_GET(pAhciPort->regSCTL)));
1217
1218 *pu32Value = pAhciPort->regSCTL;
1219 return VINF_SUCCESS;
1220}
1221
1222static VBOXSTRICTRC PortSStatus_r(PPDMDEVINS pDevIns, PAHCI pThis, PAHCIPORT pAhciPort, uint32_t iReg, uint32_t *pu32Value)
1223{
1224 RT_NOREF(pDevIns, pThis, iReg);
1225 ahciLog(("%s: read regSSTS=%#010x\n", __FUNCTION__, pAhciPort->regSSTS));
1226 ahciLog(("%s: IPM=%d SPD=%d DET=%d\n", __FUNCTION__,
1227 AHCI_PORT_SSTS_IPM_GET(pAhciPort->regSSTS), AHCI_PORT_SSTS_SPD_GET(pAhciPort->regSSTS),
1228 AHCI_PORT_SSTS_DET_GET(pAhciPort->regSSTS)));
1229
1230 *pu32Value = pAhciPort->regSSTS;
1231 return VINF_SUCCESS;
1232}
1233
1234static VBOXSTRICTRC PortSignature_r(PPDMDEVINS pDevIns, PAHCI pThis, PAHCIPORT pAhciPort, uint32_t iReg, uint32_t *pu32Value)
1235{
1236 RT_NOREF(pDevIns, pThis, iReg);
1237 ahciLog(("%s: read regSIG=%#010x\n", __FUNCTION__, pAhciPort->regSIG));
1238 *pu32Value = pAhciPort->regSIG;
1239 return VINF_SUCCESS;
1240}
1241
1242static VBOXSTRICTRC PortTaskFileData_r(PPDMDEVINS pDevIns, PAHCI pThis, PAHCIPORT pAhciPort, uint32_t iReg, uint32_t *pu32Value)
1243{
1244 RT_NOREF(pDevIns, pThis, iReg);
1245 ahciLog(("%s: read regTFD=%#010x\n", __FUNCTION__, pAhciPort->regTFD));
1246 ahciLog(("%s: ERR=%x BSY=%d DRQ=%d ERR=%d\n", __FUNCTION__,
1247 (pAhciPort->regTFD >> 8), (pAhciPort->regTFD & AHCI_PORT_TFD_BSY) >> 7,
1248 (pAhciPort->regTFD & AHCI_PORT_TFD_DRQ) >> 3, (pAhciPort->regTFD & AHCI_PORT_TFD_ERR)));
1249 *pu32Value = pAhciPort->regTFD;
1250 return VINF_SUCCESS;
1251}
1252
1253/**
1254 * Read from the port command register.
1255 */
1256static VBOXSTRICTRC PortCmd_r(PPDMDEVINS pDevIns, PAHCI pThis, PAHCIPORT pAhciPort, uint32_t iReg, uint32_t *pu32Value)
1257{
1258 RT_NOREF(pDevIns, pThis, iReg);
1259 ahciLog(("%s: read regCMD=%#010x\n", __FUNCTION__, pAhciPort->regCMD | AHCI_PORT_CMD_CCS_SHIFT(pAhciPort->u32CurrentCommandSlot)));
1260 ahciLog(("%s: ICC=%d ASP=%d ALPE=%d DLAE=%d ATAPI=%d CPD=%d ISP=%d HPCP=%d PMA=%d CPS=%d CR=%d FR=%d ISS=%d CCS=%d FRE=%d CLO=%d POD=%d SUD=%d ST=%d\n",
1261 __FUNCTION__, (pAhciPort->regCMD & AHCI_PORT_CMD_ICC) >> 28, (pAhciPort->regCMD & AHCI_PORT_CMD_ASP) >> 27,
1262 (pAhciPort->regCMD & AHCI_PORT_CMD_ALPE) >> 26, (pAhciPort->regCMD & AHCI_PORT_CMD_DLAE) >> 25,
1263 (pAhciPort->regCMD & AHCI_PORT_CMD_ATAPI) >> 24, (pAhciPort->regCMD & AHCI_PORT_CMD_CPD) >> 20,
1264 (pAhciPort->regCMD & AHCI_PORT_CMD_ISP) >> 19, (pAhciPort->regCMD & AHCI_PORT_CMD_HPCP) >> 18,
1265 (pAhciPort->regCMD & AHCI_PORT_CMD_PMA) >> 17, (pAhciPort->regCMD & AHCI_PORT_CMD_CPS) >> 16,
1266 (pAhciPort->regCMD & AHCI_PORT_CMD_CR) >> 15, (pAhciPort->regCMD & AHCI_PORT_CMD_FR) >> 14,
1267 (pAhciPort->regCMD & AHCI_PORT_CMD_ISS) >> 13, pAhciPort->u32CurrentCommandSlot,
1268 (pAhciPort->regCMD & AHCI_PORT_CMD_FRE) >> 4, (pAhciPort->regCMD & AHCI_PORT_CMD_CLO) >> 3,
1269 (pAhciPort->regCMD & AHCI_PORT_CMD_POD) >> 2, (pAhciPort->regCMD & AHCI_PORT_CMD_SUD) >> 1,
1270 (pAhciPort->regCMD & AHCI_PORT_CMD_ST)));
1271 *pu32Value = pAhciPort->regCMD | AHCI_PORT_CMD_CCS_SHIFT(pAhciPort->u32CurrentCommandSlot);
1272 return VINF_SUCCESS;
1273}
1274
1275/**
1276 * Write to the port command register.
1277 * This is the register where all the data transfer is started
1278 */
1279static VBOXSTRICTRC PortCmd_w(PPDMDEVINS pDevIns, PAHCI pThis, PAHCIPORT pAhciPort, uint32_t iReg, uint32_t u32Value)
1280{
1281 RT_NOREF(pDevIns, pThis, iReg);
1282 ahciLog(("%s: write u32Value=%#010x\n", __FUNCTION__, u32Value));
1283 ahciLog(("%s: ICC=%d ASP=%d ALPE=%d DLAE=%d ATAPI=%d CPD=%d ISP=%d HPCP=%d PMA=%d CPS=%d CR=%d FR=%d ISS=%d CCS=%d FRE=%d CLO=%d POD=%d SUD=%d ST=%d\n",
1284 __FUNCTION__, (u32Value & AHCI_PORT_CMD_ICC) >> 28, (u32Value & AHCI_PORT_CMD_ASP) >> 27,
1285 (u32Value & AHCI_PORT_CMD_ALPE) >> 26, (u32Value & AHCI_PORT_CMD_DLAE) >> 25,
1286 (u32Value & AHCI_PORT_CMD_ATAPI) >> 24, (u32Value & AHCI_PORT_CMD_CPD) >> 20,
1287 (u32Value & AHCI_PORT_CMD_ISP) >> 19, (u32Value & AHCI_PORT_CMD_HPCP) >> 18,
1288 (u32Value & AHCI_PORT_CMD_PMA) >> 17, (u32Value & AHCI_PORT_CMD_CPS) >> 16,
1289 (u32Value & AHCI_PORT_CMD_CR) >> 15, (u32Value & AHCI_PORT_CMD_FR) >> 14,
1290 (u32Value & AHCI_PORT_CMD_ISS) >> 13, (u32Value & AHCI_PORT_CMD_CCS) >> 8,
1291 (u32Value & AHCI_PORT_CMD_FRE) >> 4, (u32Value & AHCI_PORT_CMD_CLO) >> 3,
1292 (u32Value & AHCI_PORT_CMD_POD) >> 2, (u32Value & AHCI_PORT_CMD_SUD) >> 1,
1293 (u32Value & AHCI_PORT_CMD_ST)));
1294
1295 /* The PxCMD.CCS bits are R/O and maintained separately. */
1296 u32Value &= ~AHCI_PORT_CMD_CCS;
1297
1298 if (pAhciPort->fPoweredOn && pAhciPort->fSpunUp)
1299 {
1300 if (u32Value & AHCI_PORT_CMD_CLO)
1301 {
1302 ahciLog(("%s: Command list override requested\n", __FUNCTION__));
1303 u32Value &= ~(AHCI_PORT_TFD_BSY | AHCI_PORT_TFD_DRQ);
1304 /* Clear the CLO bit. */
1305 u32Value &= ~(AHCI_PORT_CMD_CLO);
1306 }
1307
1308 if (u32Value & AHCI_PORT_CMD_ST)
1309 {
1310 /*
1311 * Set engine state to running if there is a device attached and
1312 * IS.PCS is clear.
1313 */
1314 if ( pAhciPort->fPresent
1315 && !(pAhciPort->regIS & AHCI_PORT_IS_PCS))
1316 {
1317 ahciLog(("%s: Engine starts\n", __FUNCTION__));
1318 u32Value |= AHCI_PORT_CMD_CR;
1319
1320 /* If there is something in CI, kick the I/O thread. */
1321 if ( pAhciPort->regCI > 0
1322 && ASMAtomicReadBool(&pAhciPort->fWrkThreadSleeping))
1323 {
1324 ASMAtomicOrU32(&pAhciPort->u32TasksNew, pAhciPort->regCI);
1325 LogFlowFunc(("Signal event semaphore\n"));
1326 int rc = PDMDevHlpSUPSemEventSignal(pDevIns, pAhciPort->hEvtProcess);
1327 AssertRC(rc);
1328 }
1329 }
1330 else
1331 {
1332 if (!pAhciPort->fPresent)
1333 ahciLog(("%s: No pDrvBase, clearing PxCMD.CR!\n", __FUNCTION__));
1334 else
1335 ahciLog(("%s: PxIS.PCS set (PxIS=%#010x), clearing PxCMD.CR!\n", __FUNCTION__, pAhciPort->regIS));
1336
1337 u32Value &= ~AHCI_PORT_CMD_CR;
1338 }
1339 }
1340 else
1341 {
1342 ahciLog(("%s: Engine stops\n", __FUNCTION__));
1343 /* Clear command issue register. */
1344 pAhciPort->regCI = 0;
1345 pAhciPort->regSACT = 0;
1346 /* Clear current command slot. */
1347 pAhciPort->u32CurrentCommandSlot = 0;
1348 u32Value &= ~AHCI_PORT_CMD_CR;
1349 }
1350 }
1351 else if (pAhciPort->fPresent)
1352 {
1353 if ((u32Value & AHCI_PORT_CMD_POD) && (pAhciPort->regCMD & AHCI_PORT_CMD_CPS) && !pAhciPort->fPoweredOn)
1354 {
1355 ahciLog(("%s: Power on the device\n", __FUNCTION__));
1356 pAhciPort->fPoweredOn = true;
1357
1358 /*
1359 * Set states in the Port Signature and SStatus registers.
1360 */
1361 if (pAhciPort->fATAPI)
1362 pAhciPort->regSIG = AHCI_PORT_SIG_ATAPI;
1363 else
1364 pAhciPort->regSIG = AHCI_PORT_SIG_DISK;
1365 pAhciPort->regSSTS = (0x01 << 8) | /* Interface is active. */
1366 (0x02 << 4) | /* Generation 2 (3.0GBps) speed. */
1367 (0x03 << 0); /* Device detected and communication established. */
1368
1369 if (pAhciPort->regCMD & AHCI_PORT_CMD_FRE)
1370 {
1371#ifndef IN_RING3
1372 return VINF_IOM_R3_MMIO_WRITE;
1373#else
1374 ahciPostFirstD2HFisIntoMemory(pDevIns, pAhciPort);
1375 ASMAtomicOrU32(&pAhciPort->regIS, AHCI_PORT_IS_DHRS);
1376
1377 if (pAhciPort->regIE & AHCI_PORT_IE_DHRE)
1378 {
1379 int rc = ahciHbaSetInterrupt(pDevIns, pThis, pAhciPort->iLUN, VERR_IGNORED);
1380 AssertRC(rc);
1381 }
1382#endif
1383 }
1384 }
1385
1386 if ((u32Value & AHCI_PORT_CMD_SUD) && pAhciPort->fPoweredOn && !pAhciPort->fSpunUp)
1387 {
1388 ahciLog(("%s: Spin up the device\n", __FUNCTION__));
1389 pAhciPort->fSpunUp = true;
1390 }
1391 }
1392 else
1393 ahciLog(("%s: No pDrvBase, no fPoweredOn + fSpunUp, doing nothing!\n", __FUNCTION__));
1394
1395 if (u32Value & AHCI_PORT_CMD_FRE)
1396 {
1397 ahciLog(("%s: FIS receive enabled\n", __FUNCTION__));
1398
1399 u32Value |= AHCI_PORT_CMD_FR;
1400
1401 /* Send the first D2H FIS only if it wasn't already sent. */
1402 if ( !pAhciPort->fFirstD2HFisSent
1403 && pAhciPort->fPresent)
1404 {
1405#ifndef IN_RING3
1406 return VINF_IOM_R3_MMIO_WRITE;
1407#else
1408 ahciPostFirstD2HFisIntoMemory(pDevIns, pAhciPort);
1409 pAhciPort->fFirstD2HFisSent = true;
1410#endif
1411 }
1412 }
1413 else if (!(u32Value & AHCI_PORT_CMD_FRE))
1414 {
1415 ahciLog(("%s: FIS receive disabled\n", __FUNCTION__));
1416 u32Value &= ~AHCI_PORT_CMD_FR;
1417 }
1418
1419 pAhciPort->regCMD = u32Value;
1420
1421 return VINF_SUCCESS;
1422}
1423
1424/**
1425 * Read from the port interrupt enable register.
1426 */
1427static VBOXSTRICTRC PortIntrEnable_r(PPDMDEVINS pDevIns, PAHCI pThis, PAHCIPORT pAhciPort, uint32_t iReg, uint32_t *pu32Value)
1428{
1429 RT_NOREF(pDevIns, pThis, iReg);
1430 ahciLog(("%s: read regIE=%#010x\n", __FUNCTION__, pAhciPort->regIE));
1431 ahciLog(("%s: CPDE=%d TFEE=%d HBFE=%d HBDE=%d IFE=%d INFE=%d OFE=%d IPME=%d PRCE=%d DIE=%d PCE=%d DPE=%d UFE=%d SDBE=%d DSE=%d PSE=%d DHRE=%d\n",
1432 __FUNCTION__, (pAhciPort->regIE & AHCI_PORT_IE_CPDE) >> 31, (pAhciPort->regIE & AHCI_PORT_IE_TFEE) >> 30,
1433 (pAhciPort->regIE & AHCI_PORT_IE_HBFE) >> 29, (pAhciPort->regIE & AHCI_PORT_IE_HBDE) >> 28,
1434 (pAhciPort->regIE & AHCI_PORT_IE_IFE) >> 27, (pAhciPort->regIE & AHCI_PORT_IE_INFE) >> 26,
1435 (pAhciPort->regIE & AHCI_PORT_IE_OFE) >> 24, (pAhciPort->regIE & AHCI_PORT_IE_IPME) >> 23,
1436 (pAhciPort->regIE & AHCI_PORT_IE_PRCE) >> 22, (pAhciPort->regIE & AHCI_PORT_IE_DIE) >> 7,
1437 (pAhciPort->regIE & AHCI_PORT_IE_PCE) >> 6, (pAhciPort->regIE & AHCI_PORT_IE_DPE) >> 5,
1438 (pAhciPort->regIE & AHCI_PORT_IE_UFE) >> 4, (pAhciPort->regIE & AHCI_PORT_IE_SDBE) >> 3,
1439 (pAhciPort->regIE & AHCI_PORT_IE_DSE) >> 2, (pAhciPort->regIE & AHCI_PORT_IE_PSE) >> 1,
1440 (pAhciPort->regIE & AHCI_PORT_IE_DHRE)));
1441 *pu32Value = pAhciPort->regIE;
1442 return VINF_SUCCESS;
1443}
1444
1445/**
1446 * Write to the port interrupt enable register.
1447 */
1448static VBOXSTRICTRC PortIntrEnable_w(PPDMDEVINS pDevIns, PAHCI pThis, PAHCIPORT pAhciPort, uint32_t iReg, uint32_t u32Value)
1449{
1450 RT_NOREF(iReg);
1451 ahciLog(("%s: write u32Value=%#010x\n", __FUNCTION__, u32Value));
1452 ahciLog(("%s: CPDE=%d TFEE=%d HBFE=%d HBDE=%d IFE=%d INFE=%d OFE=%d IPME=%d PRCE=%d DIE=%d PCE=%d DPE=%d UFE=%d SDBE=%d DSE=%d PSE=%d DHRE=%d\n",
1453 __FUNCTION__, (u32Value & AHCI_PORT_IE_CPDE) >> 31, (u32Value & AHCI_PORT_IE_TFEE) >> 30,
1454 (u32Value & AHCI_PORT_IE_HBFE) >> 29, (u32Value & AHCI_PORT_IE_HBDE) >> 28,
1455 (u32Value & AHCI_PORT_IE_IFE) >> 27, (u32Value & AHCI_PORT_IE_INFE) >> 26,
1456 (u32Value & AHCI_PORT_IE_OFE) >> 24, (u32Value & AHCI_PORT_IE_IPME) >> 23,
1457 (u32Value & AHCI_PORT_IE_PRCE) >> 22, (u32Value & AHCI_PORT_IE_DIE) >> 7,
1458 (u32Value & AHCI_PORT_IE_PCE) >> 6, (u32Value & AHCI_PORT_IE_DPE) >> 5,
1459 (u32Value & AHCI_PORT_IE_UFE) >> 4, (u32Value & AHCI_PORT_IE_SDBE) >> 3,
1460 (u32Value & AHCI_PORT_IE_DSE) >> 2, (u32Value & AHCI_PORT_IE_PSE) >> 1,
1461 (u32Value & AHCI_PORT_IE_DHRE)));
1462
1463 u32Value &= AHCI_PORT_IE_READONLY;
1464
1465 /* Check if some a interrupt status bit changed*/
1466 uint32_t u32IntrStatus = ASMAtomicReadU32(&pAhciPort->regIS);
1467
1468 int rc = VINF_SUCCESS;
1469 if (u32Value & u32IntrStatus)
1470 rc = ahciHbaSetInterrupt(pDevIns, pThis, pAhciPort->iLUN, VINF_IOM_R3_MMIO_WRITE);
1471
1472 if (rc == VINF_SUCCESS)
1473 pAhciPort->regIE = u32Value;
1474
1475 return rc;
1476}
1477
1478/**
1479 * Read from the port interrupt status register.
1480 */
1481static VBOXSTRICTRC PortIntrSts_r(PPDMDEVINS pDevIns, PAHCI pThis, PAHCIPORT pAhciPort, uint32_t iReg, uint32_t *pu32Value)
1482{
1483 RT_NOREF(pDevIns, pThis, iReg);
1484 ahciLog(("%s: read regIS=%#010x\n", __FUNCTION__, pAhciPort->regIS));
1485 ahciLog(("%s: CPDS=%d TFES=%d HBFS=%d HBDS=%d IFS=%d INFS=%d OFS=%d IPMS=%d PRCS=%d DIS=%d PCS=%d DPS=%d UFS=%d SDBS=%d DSS=%d PSS=%d DHRS=%d\n",
1486 __FUNCTION__, (pAhciPort->regIS & AHCI_PORT_IS_CPDS) >> 31, (pAhciPort->regIS & AHCI_PORT_IS_TFES) >> 30,
1487 (pAhciPort->regIS & AHCI_PORT_IS_HBFS) >> 29, (pAhciPort->regIS & AHCI_PORT_IS_HBDS) >> 28,
1488 (pAhciPort->regIS & AHCI_PORT_IS_IFS) >> 27, (pAhciPort->regIS & AHCI_PORT_IS_INFS) >> 26,
1489 (pAhciPort->regIS & AHCI_PORT_IS_OFS) >> 24, (pAhciPort->regIS & AHCI_PORT_IS_IPMS) >> 23,
1490 (pAhciPort->regIS & AHCI_PORT_IS_PRCS) >> 22, (pAhciPort->regIS & AHCI_PORT_IS_DIS) >> 7,
1491 (pAhciPort->regIS & AHCI_PORT_IS_PCS) >> 6, (pAhciPort->regIS & AHCI_PORT_IS_DPS) >> 5,
1492 (pAhciPort->regIS & AHCI_PORT_IS_UFS) >> 4, (pAhciPort->regIS & AHCI_PORT_IS_SDBS) >> 3,
1493 (pAhciPort->regIS & AHCI_PORT_IS_DSS) >> 2, (pAhciPort->regIS & AHCI_PORT_IS_PSS) >> 1,
1494 (pAhciPort->regIS & AHCI_PORT_IS_DHRS)));
1495 *pu32Value = pAhciPort->regIS;
1496 return VINF_SUCCESS;
1497}
1498
1499/**
1500 * Write to the port interrupt status register.
1501 */
1502static VBOXSTRICTRC PortIntrSts_w(PPDMDEVINS pDevIns, PAHCI pThis, PAHCIPORT pAhciPort, uint32_t iReg, uint32_t u32Value)
1503{
1504 RT_NOREF(pDevIns, pThis, iReg);
1505 ahciLog(("%s: write u32Value=%#010x\n", __FUNCTION__, u32Value));
1506 ASMAtomicAndU32(&pAhciPort->regIS, ~(u32Value & AHCI_PORT_IS_READONLY));
1507
1508 return VINF_SUCCESS;
1509}
1510
1511/**
1512 * Read from the port FIS base address upper 32bit register.
1513 */
1514static VBOXSTRICTRC PortFisAddrUp_r(PPDMDEVINS pDevIns, PAHCI pThis, PAHCIPORT pAhciPort, uint32_t iReg, uint32_t *pu32Value)
1515{
1516 RT_NOREF(pDevIns, pThis, iReg);
1517 ahciLog(("%s: read regFBU=%#010x\n", __FUNCTION__, pAhciPort->regFBU));
1518 *pu32Value = pAhciPort->regFBU;
1519 return VINF_SUCCESS;
1520}
1521
1522/**
1523 * Write to the port FIS base address upper 32bit register.
1524 */
1525static VBOXSTRICTRC PortFisAddrUp_w(PPDMDEVINS pDevIns, PAHCI pThis, PAHCIPORT pAhciPort, uint32_t iReg, uint32_t u32Value)
1526{
1527 RT_NOREF(pDevIns, pThis, iReg);
1528 ahciLog(("%s: write u32Value=%#010x\n", __FUNCTION__, u32Value));
1529
1530 pAhciPort->regFBU = u32Value;
1531 pAhciPort->GCPhysAddrFb = AHCI_RTGCPHYS_FROM_U32(pAhciPort->regFBU, pAhciPort->regFB);
1532
1533 return VINF_SUCCESS;
1534}
1535
1536/**
1537 * Read from the port FIS base address register.
1538 */
1539static VBOXSTRICTRC PortFisAddr_r(PPDMDEVINS pDevIns, PAHCI pThis, PAHCIPORT pAhciPort, uint32_t iReg, uint32_t *pu32Value)
1540{
1541 RT_NOREF(pDevIns, pThis, iReg);
1542 ahciLog(("%s: read regFB=%#010x\n", __FUNCTION__, pAhciPort->regFB));
1543 *pu32Value = pAhciPort->regFB;
1544 return VINF_SUCCESS;
1545}
1546
1547/**
1548 * Write to the port FIS base address register.
1549 */
1550static VBOXSTRICTRC PortFisAddr_w(PPDMDEVINS pDevIns, PAHCI pThis, PAHCIPORT pAhciPort, uint32_t iReg, uint32_t u32Value)
1551{
1552 RT_NOREF(pDevIns, pThis, iReg);
1553 ahciLog(("%s: write u32Value=%#010x\n", __FUNCTION__, u32Value));
1554
1555 Assert(!(u32Value & ~AHCI_PORT_FB_RESERVED));
1556
1557 pAhciPort->regFB = (u32Value & AHCI_PORT_FB_RESERVED);
1558 pAhciPort->GCPhysAddrFb = AHCI_RTGCPHYS_FROM_U32(pAhciPort->regFBU, pAhciPort->regFB);
1559
1560 return VINF_SUCCESS;
1561}
1562
1563/**
1564 * Write to the port command list base address upper 32bit register.
1565 */
1566static VBOXSTRICTRC PortCmdLstAddrUp_w(PPDMDEVINS pDevIns, PAHCI pThis, PAHCIPORT pAhciPort, uint32_t iReg, uint32_t u32Value)
1567{
1568 RT_NOREF(pDevIns, pThis, iReg);
1569 ahciLog(("%s: write u32Value=%#010x\n", __FUNCTION__, u32Value));
1570
1571 pAhciPort->regCLBU = u32Value;
1572 pAhciPort->GCPhysAddrClb = AHCI_RTGCPHYS_FROM_U32(pAhciPort->regCLBU, pAhciPort->regCLB);
1573
1574 return VINF_SUCCESS;
1575}
1576
1577/**
1578 * Read from the port command list base address upper 32bit register.
1579 */
1580static VBOXSTRICTRC PortCmdLstAddrUp_r(PPDMDEVINS pDevIns, PAHCI pThis, PAHCIPORT pAhciPort, uint32_t iReg, uint32_t *pu32Value)
1581{
1582 RT_NOREF(pDevIns, pThis, iReg);
1583 ahciLog(("%s: read regCLBU=%#010x\n", __FUNCTION__, pAhciPort->regCLBU));
1584 *pu32Value = pAhciPort->regCLBU;
1585 return VINF_SUCCESS;
1586}
1587
1588/**
1589 * Read from the port command list base address register.
1590 */
1591static VBOXSTRICTRC PortCmdLstAddr_r(PPDMDEVINS pDevIns, PAHCI pThis, PAHCIPORT pAhciPort, uint32_t iReg, uint32_t *pu32Value)
1592{
1593 RT_NOREF(pDevIns, pThis, iReg);
1594 ahciLog(("%s: read regCLB=%#010x\n", __FUNCTION__, pAhciPort->regCLB));
1595 *pu32Value = pAhciPort->regCLB;
1596 return VINF_SUCCESS;
1597}
1598
1599/**
1600 * Write to the port command list base address register.
1601 */
1602static VBOXSTRICTRC PortCmdLstAddr_w(PPDMDEVINS pDevIns, PAHCI pThis, PAHCIPORT pAhciPort, uint32_t iReg, uint32_t u32Value)
1603{
1604 RT_NOREF(pDevIns, pThis, iReg);
1605 ahciLog(("%s: write u32Value=%#010x\n", __FUNCTION__, u32Value));
1606
1607 Assert(!(u32Value & ~AHCI_PORT_CLB_RESERVED));
1608
1609 pAhciPort->regCLB = (u32Value & AHCI_PORT_CLB_RESERVED);
1610 pAhciPort->GCPhysAddrClb = AHCI_RTGCPHYS_FROM_U32(pAhciPort->regCLBU, pAhciPort->regCLB);
1611
1612 return VINF_SUCCESS;
1613}
1614
1615/**
1616 * Read from the global Version register.
1617 */
1618static VBOXSTRICTRC HbaVersion_r(PPDMDEVINS pDevIns, PAHCI pThis, uint32_t iReg, uint32_t *pu32Value)
1619{
1620 RT_NOREF(pDevIns, iReg);
1621 Log(("%s: read regHbaVs=%#010x\n", __FUNCTION__, pThis->regHbaVs));
1622 *pu32Value = pThis->regHbaVs;
1623 return VINF_SUCCESS;
1624}
1625
1626/**
1627 * Read from the global Ports implemented register.
1628 */
1629static VBOXSTRICTRC HbaPortsImplemented_r(PPDMDEVINS pDevIns, PAHCI pThis, uint32_t iReg, uint32_t *pu32Value)
1630{
1631 RT_NOREF(pDevIns, iReg);
1632 Log(("%s: read regHbaPi=%#010x\n", __FUNCTION__, pThis->regHbaPi));
1633 *pu32Value = pThis->regHbaPi;
1634 return VINF_SUCCESS;
1635}
1636
1637/**
1638 * Write to the global interrupt status register.
1639 */
1640static VBOXSTRICTRC HbaInterruptStatus_w(PPDMDEVINS pDevIns, PAHCI pThis, uint32_t iReg, uint32_t u32Value)
1641{
1642 RT_NOREF(iReg);
1643 Log(("%s: write u32Value=%#010x\n", __FUNCTION__, u32Value));
1644
1645 int rc = PDMDevHlpCritSectEnter(pDevIns, &pThis->lock, VINF_IOM_R3_MMIO_WRITE);
1646 if (rc != VINF_SUCCESS)
1647 return rc;
1648
1649 pThis->regHbaIs &= ~(u32Value);
1650
1651 /*
1652 * Update interrupt status register and check for ports who
1653 * set the interrupt inbetween.
1654 */
1655 bool fClear = true;
1656 pThis->regHbaIs |= ASMAtomicXchgU32(&pThis->u32PortsInterrupted, 0);
1657 if (!pThis->regHbaIs)
1658 {
1659 unsigned i = 0;
1660
1661 /* Check if the cleared ports have a interrupt status bit set. */
1662 while ((u32Value > 0) && (i < AHCI_MAX_NR_PORTS_IMPL))
1663 {
1664 if (u32Value & 0x01)
1665 {
1666 PAHCIPORT pAhciPort = &pThis->aPorts[i];
1667
1668 if (pAhciPort->regIE & pAhciPort->regIS)
1669 {
1670 Log(("%s: Interrupt status of port %u set -> Set interrupt again\n", __FUNCTION__, i));
1671 ASMAtomicOrU32(&pThis->u32PortsInterrupted, 1 << i);
1672 fClear = false;
1673 break;
1674 }
1675 }
1676 u32Value >>= 1;
1677 i++;
1678 }
1679 }
1680 else
1681 fClear = false;
1682
1683 if (fClear)
1684 ahciHbaClearInterrupt(pDevIns);
1685 else
1686 {
1687 Log(("%s: Not clearing interrupt: u32PortsInterrupted=%#010x\n", __FUNCTION__, pThis->u32PortsInterrupted));
1688 /*
1689 * We need to set the interrupt again because the I/O APIC does not set it again even if the
1690 * line is still high.
1691 * We need to clear it first because the PCI bus only calls the interrupt controller if the state changes.
1692 */
1693 PDMDevHlpPCISetIrq(pDevIns, 0, 0);
1694 PDMDevHlpPCISetIrq(pDevIns, 0, 1);
1695 }
1696
1697 PDMDevHlpCritSectLeave(pDevIns, &pThis->lock);
1698 return VINF_SUCCESS;
1699}
1700
1701/**
1702 * Read from the global interrupt status register.
1703 */
1704static VBOXSTRICTRC HbaInterruptStatus_r(PPDMDEVINS pDevIns, PAHCI pThis, uint32_t iReg, uint32_t *pu32Value)
1705{
1706 RT_NOREF(iReg);
1707
1708 int rc = PDMDevHlpCritSectEnter(pDevIns, &pThis->lock, VINF_IOM_R3_MMIO_READ);
1709 if (rc != VINF_SUCCESS)
1710 return rc;
1711
1712 uint32_t u32PortsInterrupted = ASMAtomicXchgU32(&pThis->u32PortsInterrupted, 0);
1713
1714 PDMDevHlpCritSectLeave(pDevIns, &pThis->lock);
1715 Log(("%s: read regHbaIs=%#010x u32PortsInterrupted=%#010x\n", __FUNCTION__, pThis->regHbaIs, u32PortsInterrupted));
1716
1717 pThis->regHbaIs |= u32PortsInterrupted;
1718
1719#ifdef LOG_ENABLED
1720 Log(("%s:", __FUNCTION__));
1721 uint32_t const cPortsImpl = RT_MIN(pThis->cPortsImpl, RT_ELEMENTS(pThis->aPorts));
1722 for (unsigned i = 0; i < cPortsImpl; i++)
1723 {
1724 if ((pThis->regHbaIs >> i) & 0x01)
1725 Log((" P%d", i));
1726 }
1727 Log(("\n"));
1728#endif
1729
1730 *pu32Value = pThis->regHbaIs;
1731
1732 return VINF_SUCCESS;
1733}
1734
1735/**
1736 * Write to the global control register.
1737 */
1738static VBOXSTRICTRC HbaControl_w(PPDMDEVINS pDevIns, PAHCI pThis, uint32_t iReg, uint32_t u32Value)
1739{
1740 Log(("%s: write u32Value=%#010x\n"
1741 "%s: AE=%d IE=%d HR=%d\n",
1742 __FUNCTION__, u32Value,
1743 __FUNCTION__, (u32Value & AHCI_HBA_CTRL_AE) >> 31, (u32Value & AHCI_HBA_CTRL_IE) >> 1,
1744 (u32Value & AHCI_HBA_CTRL_HR)));
1745 RT_NOREF(iReg);
1746
1747#ifndef IN_RING3
1748 RT_NOREF(pDevIns, pThis, u32Value);
1749 return VINF_IOM_R3_MMIO_WRITE;
1750#else
1751 /*
1752 * Increase the active thread counter because we might set the host controller
1753 * reset bit.
1754 */
1755 ASMAtomicIncU32(&pThis->cThreadsActive);
1756 ASMAtomicWriteU32(&pThis->regHbaCtrl, (u32Value & AHCI_HBA_CTRL_RW_MASK) | AHCI_HBA_CTRL_AE);
1757
1758 /*
1759 * Do the HBA reset if requested and there is no other active thread at the moment,
1760 * the work is deferred to the last active thread otherwise.
1761 */
1762 uint32_t cThreadsActive = ASMAtomicDecU32(&pThis->cThreadsActive);
1763 if ( (u32Value & AHCI_HBA_CTRL_HR)
1764 && !cThreadsActive)
1765 ahciR3HBAReset(pDevIns, pThis, PDMDEVINS_2_DATA_CC(pDevIns, PAHCICC));
1766
1767 return VINF_SUCCESS;
1768#endif
1769}
1770
1771/**
1772 * Read the global control register.
1773 */
1774static VBOXSTRICTRC HbaControl_r(PPDMDEVINS pDevIns, PAHCI pThis, uint32_t iReg, uint32_t *pu32Value)
1775{
1776 RT_NOREF(pDevIns, iReg);
1777 Log(("%s: read regHbaCtrl=%#010x\n"
1778 "%s: AE=%d IE=%d HR=%d\n",
1779 __FUNCTION__, pThis->regHbaCtrl,
1780 __FUNCTION__, (pThis->regHbaCtrl & AHCI_HBA_CTRL_AE) >> 31, (pThis->regHbaCtrl & AHCI_HBA_CTRL_IE) >> 1,
1781 (pThis->regHbaCtrl & AHCI_HBA_CTRL_HR)));
1782 *pu32Value = pThis->regHbaCtrl;
1783 return VINF_SUCCESS;
1784}
1785
1786/**
1787 * Read the global capabilities register.
1788 */
1789static VBOXSTRICTRC HbaCapabilities_r(PPDMDEVINS pDevIns, PAHCI pThis, uint32_t iReg, uint32_t *pu32Value)
1790{
1791 RT_NOREF(pDevIns, iReg);
1792 Log(("%s: read regHbaCap=%#010x\n"
1793 "%s: S64A=%d SNCQ=%d SIS=%d SSS=%d SALP=%d SAL=%d SCLO=%d ISS=%d SNZO=%d SAM=%d SPM=%d PMD=%d SSC=%d PSC=%d NCS=%d NP=%d\n",
1794 __FUNCTION__, pThis->regHbaCap,
1795 __FUNCTION__, (pThis->regHbaCap & AHCI_HBA_CAP_S64A) >> 31, (pThis->regHbaCap & AHCI_HBA_CAP_SNCQ) >> 30,
1796 (pThis->regHbaCap & AHCI_HBA_CAP_SIS) >> 28, (pThis->regHbaCap & AHCI_HBA_CAP_SSS) >> 27,
1797 (pThis->regHbaCap & AHCI_HBA_CAP_SALP) >> 26, (pThis->regHbaCap & AHCI_HBA_CAP_SAL) >> 25,
1798 (pThis->regHbaCap & AHCI_HBA_CAP_SCLO) >> 24, (pThis->regHbaCap & AHCI_HBA_CAP_ISS) >> 20,
1799 (pThis->regHbaCap & AHCI_HBA_CAP_SNZO) >> 19, (pThis->regHbaCap & AHCI_HBA_CAP_SAM) >> 18,
1800 (pThis->regHbaCap & AHCI_HBA_CAP_SPM) >> 17, (pThis->regHbaCap & AHCI_HBA_CAP_PMD) >> 15,
1801 (pThis->regHbaCap & AHCI_HBA_CAP_SSC) >> 14, (pThis->regHbaCap & AHCI_HBA_CAP_PSC) >> 13,
1802 (pThis->regHbaCap & AHCI_HBA_CAP_NCS) >> 8, (pThis->regHbaCap & AHCI_HBA_CAP_NP)));
1803 *pu32Value = pThis->regHbaCap;
1804 return VINF_SUCCESS;
1805}
1806
1807/**
1808 * Write to the global command completion coalescing control register.
1809 */
1810static VBOXSTRICTRC HbaCccCtl_w(PPDMDEVINS pDevIns, PAHCI pThis, uint32_t iReg, uint32_t u32Value)
1811{
1812 RT_NOREF(iReg);
1813 Log(("%s: write u32Value=%#010x\n"
1814 "%s: TV=%d CC=%d INT=%d EN=%d\n",
1815 __FUNCTION__, u32Value,
1816 __FUNCTION__, AHCI_HBA_CCC_CTL_TV_GET(u32Value), AHCI_HBA_CCC_CTL_CC_GET(u32Value),
1817 AHCI_HBA_CCC_CTL_INT_GET(u32Value), (u32Value & AHCI_HBA_CCC_CTL_EN)));
1818
1819 pThis->regHbaCccCtl = u32Value;
1820 pThis->uCccTimeout = AHCI_HBA_CCC_CTL_TV_GET(u32Value);
1821 pThis->uCccPortNr = AHCI_HBA_CCC_CTL_INT_GET(u32Value);
1822 pThis->uCccNr = AHCI_HBA_CCC_CTL_CC_GET(u32Value);
1823
1824 if (u32Value & AHCI_HBA_CCC_CTL_EN)
1825 PDMDevHlpTimerSetMillies(pDevIns, pThis->hHbaCccTimer, pThis->uCccTimeout); /* Arm the timer */
1826 else
1827 PDMDevHlpTimerStop(pDevIns, pThis->hHbaCccTimer);
1828
1829 return VINF_SUCCESS;
1830}
1831
1832/**
1833 * Read the global command completion coalescing control register.
1834 */
1835static VBOXSTRICTRC HbaCccCtl_r(PPDMDEVINS pDevIns, PAHCI pThis, uint32_t iReg, uint32_t *pu32Value)
1836{
1837 RT_NOREF(pDevIns, iReg);
1838 Log(("%s: read regHbaCccCtl=%#010x\n"
1839 "%s: TV=%d CC=%d INT=%d EN=%d\n",
1840 __FUNCTION__, pThis->regHbaCccCtl,
1841 __FUNCTION__, AHCI_HBA_CCC_CTL_TV_GET(pThis->regHbaCccCtl), AHCI_HBA_CCC_CTL_CC_GET(pThis->regHbaCccCtl),
1842 AHCI_HBA_CCC_CTL_INT_GET(pThis->regHbaCccCtl), (pThis->regHbaCccCtl & AHCI_HBA_CCC_CTL_EN)));
1843 *pu32Value = pThis->regHbaCccCtl;
1844 return VINF_SUCCESS;
1845}
1846
1847/**
1848 * Write to the global command completion coalescing ports register.
1849 */
1850static VBOXSTRICTRC HbaCccPorts_w(PPDMDEVINS pDevIns, PAHCI pThis, uint32_t iReg, uint32_t u32Value)
1851{
1852 RT_NOREF(pDevIns, iReg);
1853 Log(("%s: write u32Value=%#010x\n", __FUNCTION__, u32Value));
1854
1855 pThis->regHbaCccPorts = u32Value;
1856
1857 return VINF_SUCCESS;
1858}
1859
1860/**
1861 * Read the global command completion coalescing ports register.
1862 */
1863static VBOXSTRICTRC HbaCccPorts_r(PPDMDEVINS pDevIns, PAHCI pThis, uint32_t iReg, uint32_t *pu32Value)
1864{
1865 RT_NOREF(pDevIns, iReg);
1866 Log(("%s: read regHbaCccPorts=%#010x\n", __FUNCTION__, pThis->regHbaCccPorts));
1867
1868#ifdef LOG_ENABLED
1869 Log(("%s:", __FUNCTION__));
1870 uint32_t const cPortsImpl = RT_MIN(pThis->cPortsImpl, RT_ELEMENTS(pThis->aPorts));
1871 for (unsigned i = 0; i < cPortsImpl; i++)
1872 {
1873 if ((pThis->regHbaCccPorts >> i) & 0x01)
1874 Log((" P%d", i));
1875 }
1876 Log(("\n"));
1877#endif
1878
1879 *pu32Value = pThis->regHbaCccPorts;
1880 return VINF_SUCCESS;
1881}
1882
1883/**
1884 * Invalid write to global register
1885 */
1886static VBOXSTRICTRC HbaInvalid_w(PPDMDEVINS pDevIns, PAHCI pThis, uint32_t iReg, uint32_t u32Value)
1887{
1888 RT_NOREF(pDevIns, pThis, iReg, u32Value);
1889 Log(("%s: Write denied!!! iReg=%u u32Value=%#010x\n", __FUNCTION__, iReg, u32Value));
1890 return VINF_SUCCESS;
1891}
1892
1893/**
1894 * Invalid Port write.
1895 */
1896static VBOXSTRICTRC PortInvalid_w(PPDMDEVINS pDevIns, PAHCI pThis, PAHCIPORT pAhciPort, uint32_t iReg, uint32_t u32Value)
1897{
1898 RT_NOREF(pDevIns, pThis, pAhciPort, iReg, u32Value);
1899 ahciLog(("%s: Write denied!!! iReg=%u u32Value=%#010x\n", __FUNCTION__, iReg, u32Value));
1900 return VINF_SUCCESS;
1901}
1902
1903/**
1904 * Invalid Port read.
1905 */
1906static VBOXSTRICTRC PortInvalid_r(PPDMDEVINS pDevIns, PAHCI pThis, PAHCIPORT pAhciPort, uint32_t iReg, uint32_t *pu32Value)
1907{
1908 RT_NOREF(pDevIns, pThis, pAhciPort, iReg, pu32Value);
1909 ahciLog(("%s: Read denied!!! iReg=%u\n", __FUNCTION__, iReg));
1910 return VINF_SUCCESS;
1911}
1912
1913/**
1914 * Register descriptor table for global HBA registers
1915 */
1916static const AHCIOPREG g_aOpRegs[] =
1917{
1918 {"HbaCapabilites", HbaCapabilities_r, HbaInvalid_w}, /* Readonly */
1919 {"HbaControl" , HbaControl_r, HbaControl_w},
1920 {"HbaInterruptStatus", HbaInterruptStatus_r, HbaInterruptStatus_w},
1921 {"HbaPortsImplemented", HbaPortsImplemented_r, HbaInvalid_w}, /* Readonly */
1922 {"HbaVersion", HbaVersion_r, HbaInvalid_w}, /* ReadOnly */
1923 {"HbaCccCtl", HbaCccCtl_r, HbaCccCtl_w},
1924 {"HbaCccPorts", HbaCccPorts_r, HbaCccPorts_w},
1925};
1926
1927/**
1928 * Register descriptor table for port registers
1929 */
1930static const AHCIPORTOPREG g_aPortOpRegs[] =
1931{
1932 {"PortCmdLstAddr", PortCmdLstAddr_r, PortCmdLstAddr_w},
1933 {"PortCmdLstAddrUp", PortCmdLstAddrUp_r, PortCmdLstAddrUp_w},
1934 {"PortFisAddr", PortFisAddr_r, PortFisAddr_w},
1935 {"PortFisAddrUp", PortFisAddrUp_r, PortFisAddrUp_w},
1936 {"PortIntrSts", PortIntrSts_r, PortIntrSts_w},
1937 {"PortIntrEnable", PortIntrEnable_r, PortIntrEnable_w},
1938 {"PortCmd", PortCmd_r, PortCmd_w},
1939 {"PortReserved1", PortInvalid_r, PortInvalid_w}, /* Not used. */
1940 {"PortTaskFileData", PortTaskFileData_r, PortInvalid_w}, /* Readonly */
1941 {"PortSignature", PortSignature_r, PortInvalid_w}, /* Readonly */
1942 {"PortSStatus", PortSStatus_r, PortInvalid_w}, /* Readonly */
1943 {"PortSControl", PortSControl_r, PortSControl_w},
1944 {"PortSError", PortSError_r, PortSError_w},
1945 {"PortSActive", PortSActive_r, PortSActive_w},
1946 {"PortCmdIssue", PortCmdIssue_r, PortCmdIssue_w},
1947 {"PortReserved2", PortInvalid_r, PortInvalid_w}, /* Not used. */
1948};
1949
1950#ifdef IN_RING3
1951
1952/**
1953 * Reset initiated by system software for one port.
1954 *
1955 * @param pAhciPort The port to reset, shared bits.
1956 * @param pAhciPortR3 The port to reset, ring-3 bits.
1957 */
1958static void ahciR3PortSwReset(PAHCIPORT pAhciPort, PAHCIPORTR3 pAhciPortR3)
1959{
1960 bool fAllTasksCanceled;
1961
1962 /* Cancel all tasks first. */
1963 fAllTasksCanceled = ahciR3CancelActiveTasks(pAhciPortR3);
1964 Assert(fAllTasksCanceled);
1965
1966 Assert(pAhciPort->cTasksActive == 0);
1967
1968 pAhciPort->regIS = 0;
1969 pAhciPort->regIE = 0;
1970 pAhciPort->regCMD = AHCI_PORT_CMD_CPD | /* Cold presence detection */
1971 AHCI_PORT_CMD_SUD | /* Device has spun up. */
1972 AHCI_PORT_CMD_POD; /* Port is powered on. */
1973
1974 /* Hotplugging supported?. */
1975 if (pAhciPort->fHotpluggable)
1976 pAhciPort->regCMD |= AHCI_PORT_CMD_HPCP;
1977
1978 pAhciPort->regTFD = (1 << 8) | ATA_STAT_SEEK | ATA_STAT_WRERR;
1979 pAhciPort->regSIG = UINT32_MAX;
1980 pAhciPort->regSSTS = 0;
1981 pAhciPort->regSCTL = 0;
1982 pAhciPort->regSERR = 0;
1983 pAhciPort->regSACT = 0;
1984 pAhciPort->regCI = 0;
1985
1986 pAhciPort->fResetDevice = false;
1987 pAhciPort->fPoweredOn = true;
1988 pAhciPort->fSpunUp = true;
1989 pAhciPort->cMultSectors = ATA_MAX_MULT_SECTORS;
1990 pAhciPort->uATATransferMode = ATA_MODE_UDMA | 6;
1991
1992 pAhciPort->u32TasksNew = 0;
1993 pAhciPort->u32TasksRedo = 0;
1994 pAhciPort->u32TasksFinished = 0;
1995 pAhciPort->u32QueuedTasksFinished = 0;
1996 pAhciPort->u32CurrentCommandSlot = 0;
1997
1998 if (pAhciPort->fPresent)
1999 {
2000 pAhciPort->regCMD |= AHCI_PORT_CMD_CPS; /* Indicate that there is a device on that port */
2001
2002 if (pAhciPort->fPoweredOn)
2003 {
2004 /*
2005 * Set states in the Port Signature and SStatus registers.
2006 */
2007 if (pAhciPort->fATAPI)
2008 pAhciPort->regSIG = AHCI_PORT_SIG_ATAPI;
2009 else
2010 pAhciPort->regSIG = AHCI_PORT_SIG_DISK;
2011 pAhciPort->regSSTS = (0x01 << 8) | /* Interface is active. */
2012 (0x02 << 4) | /* Generation 2 (3.0GBps) speed. */
2013 (0x03 << 0); /* Device detected and communication established. */
2014 }
2015 }
2016}
2017
2018/**
2019 * Hardware reset used for machine power on and reset.
2020 *
2021 * @param pAhciPort The port to reset.
2022 */
2023static void ahciPortHwReset(PAHCIPORT pAhciPort)
2024{
2025 /* Reset the address registers. */
2026 pAhciPort->regCLB = 0;
2027 pAhciPort->regCLBU = 0;
2028 pAhciPort->regFB = 0;
2029 pAhciPort->regFBU = 0;
2030
2031 /* Reset calculated addresses. */
2032 pAhciPort->GCPhysAddrClb = 0;
2033 pAhciPort->GCPhysAddrFb = 0;
2034}
2035
2036/**
2037 * Create implemented ports bitmap.
2038 *
2039 * @returns 32bit bitmask with a bit set for every implemented port.
2040 * @param cPorts Number of ports.
2041 */
2042static uint32_t ahciGetPortsImplemented(unsigned cPorts)
2043{
2044 uint32_t uPortsImplemented = 0;
2045
2046 for (unsigned i = 0; i < cPorts; i++)
2047 uPortsImplemented |= (1 << i);
2048
2049 return uPortsImplemented;
2050}
2051
2052/**
2053 * Reset the entire HBA.
2054 *
2055 * @param pDevIns The device instance.
2056 * @param pThis The shared AHCI state.
2057 * @param pThisCC The ring-3 AHCI state.
2058 */
2059static void ahciR3HBAReset(PPDMDEVINS pDevIns, PAHCI pThis, PAHCIR3 pThisCC)
2060{
2061 unsigned i;
2062 int rc = VINF_SUCCESS;
2063
2064 LogRel(("AHCI#%u: Reset the HBA\n", pDevIns->iInstance));
2065
2066 /* Stop the CCC timer. */
2067 if (pThis->regHbaCccCtl & AHCI_HBA_CCC_CTL_EN)
2068 {
2069 rc = PDMDevHlpTimerStop(pDevIns, pThis->hHbaCccTimer);
2070 if (RT_FAILURE(rc))
2071 AssertMsgFailed(("%s: Failed to stop timer!\n", __FUNCTION__));
2072 }
2073
2074 /* Reset every port */
2075 uint32_t const cPortsImpl = RT_MIN(pThis->cPortsImpl, RT_ELEMENTS(pThisCC->aPorts));
2076 for (i = 0; i < cPortsImpl; i++)
2077 {
2078 PAHCIPORT pAhciPort = &pThis->aPorts[i];
2079 PAHCIPORTR3 pAhciPortR3 = &pThisCC->aPorts[i];
2080
2081 pAhciPort->iLUN = i;
2082 pAhciPortR3->iLUN = i;
2083 ahciR3PortSwReset(pAhciPort, pAhciPortR3);
2084 }
2085
2086 /* Init Global registers */
2087 pThis->regHbaCap = AHCI_HBA_CAP_ISS_SHIFT(AHCI_HBA_CAP_ISS_GEN2)
2088 | AHCI_HBA_CAP_S64A /* 64bit addressing supported */
2089 | AHCI_HBA_CAP_SAM /* AHCI mode only */
2090 | AHCI_HBA_CAP_SNCQ /* Support native command queuing */
2091 | AHCI_HBA_CAP_SSS /* Staggered spin up */
2092 | AHCI_HBA_CAP_CCCS /* Support command completion coalescing */
2093 | AHCI_HBA_CAP_NCS_SET(pThis->cCmdSlotsAvail) /* Number of command slots we support */
2094 | AHCI_HBA_CAP_NP_SET(pThis->cPortsImpl); /* Number of supported ports */
2095 pThis->regHbaCtrl = AHCI_HBA_CTRL_AE;
2096 pThis->regHbaPi = ahciGetPortsImplemented(pThis->cPortsImpl);
2097 pThis->regHbaVs = AHCI_HBA_VS_MJR | AHCI_HBA_VS_MNR;
2098 pThis->regHbaCccCtl = 0;
2099 pThis->regHbaCccPorts = 0;
2100 pThis->uCccTimeout = 0;
2101 pThis->uCccPortNr = 0;
2102 pThis->uCccNr = 0;
2103
2104 /* Clear pending interrupts. */
2105 pThis->regHbaIs = 0;
2106 pThis->u32PortsInterrupted = 0;
2107 ahciHbaClearInterrupt(pDevIns);
2108
2109 pThis->f64BitAddr = false;
2110 pThis->u32PortsInterrupted = 0;
2111 pThis->f8ByteMMIO4BytesWrittenSuccessfully = false;
2112 /* Clear the HBA Reset bit */
2113 pThis->regHbaCtrl &= ~AHCI_HBA_CTRL_HR;
2114}
2115
2116#endif /* IN_RING3 */
2117
2118/**
2119 * Reads from a AHCI controller register.
2120 *
2121 * @returns Strict VBox status code.
2122 *
2123 * @param pDevIns The device instance.
2124 * @param pThis The shared AHCI state.
2125 * @param uReg The register to write.
2126 * @param pv Where to store the result.
2127 * @param cb Number of bytes read.
2128 */
2129static VBOXSTRICTRC ahciRegisterRead(PPDMDEVINS pDevIns, PAHCI pThis, uint32_t uReg, void *pv, unsigned cb)
2130{
2131 VBOXSTRICTRC rc;
2132 uint32_t iReg;
2133
2134 /*
2135 * If the access offset is smaller than AHCI_HBA_GLOBAL_SIZE the guest accesses the global registers.
2136 * Otherwise it accesses the registers of a port.
2137 */
2138 if (uReg < AHCI_HBA_GLOBAL_SIZE)
2139 {
2140 iReg = uReg >> 2;
2141 Log3(("%s: Trying to read from global register %u\n", __FUNCTION__, iReg));
2142 if (iReg < RT_ELEMENTS(g_aOpRegs))
2143 {
2144 const AHCIOPREG *pReg = &g_aOpRegs[iReg];
2145 rc = pReg->pfnRead(pDevIns, pThis, iReg, (uint32_t *)pv);
2146 }
2147 else
2148 {
2149 Log3(("%s: Trying to read global register %u/%u!!!\n", __FUNCTION__, iReg, RT_ELEMENTS(g_aOpRegs)));
2150 *(uint32_t *)pv = 0;
2151 rc = VINF_SUCCESS;
2152 }
2153 }
2154 else
2155 {
2156 uint32_t iRegOffset;
2157 uint32_t iPort;
2158
2159 /* Calculate accessed port. */
2160 uReg -= AHCI_HBA_GLOBAL_SIZE;
2161 iPort = uReg / AHCI_PORT_REGISTER_SIZE;
2162 iRegOffset = (uReg % AHCI_PORT_REGISTER_SIZE);
2163 iReg = iRegOffset >> 2;
2164
2165 Log3(("%s: Trying to read from port %u and register %u\n", __FUNCTION__, iPort, iReg));
2166
2167 if (RT_LIKELY( iPort < RT_MIN(pThis->cPortsImpl, RT_ELEMENTS(pThis->aPorts))
2168 && iReg < RT_ELEMENTS(g_aPortOpRegs)))
2169 {
2170 const AHCIPORTOPREG *pPortReg = &g_aPortOpRegs[iReg];
2171 rc = pPortReg->pfnRead(pDevIns, pThis, &pThis->aPorts[iPort], iReg, (uint32_t *)pv);
2172 }
2173 else
2174 {
2175 Log3(("%s: Trying to read port %u register %u/%u!!!\n", __FUNCTION__, iPort, iReg, RT_ELEMENTS(g_aPortOpRegs)));
2176 rc = VINF_IOM_MMIO_UNUSED_00;
2177 }
2178
2179 /*
2180 * Windows Vista tries to read one byte from some registers instead of four.
2181 * Correct the value according to the read size.
2182 */
2183 if (RT_SUCCESS(rc) && cb != sizeof(uint32_t))
2184 {
2185 switch (cb)
2186 {
2187 case 1:
2188 {
2189 uint8_t uNewValue;
2190 uint8_t *p = (uint8_t *)pv;
2191
2192 iRegOffset &= 3;
2193 Log3(("%s: iRegOffset=%u\n", __FUNCTION__, iRegOffset));
2194 uNewValue = p[iRegOffset];
2195 /* Clear old value */
2196 *(uint32_t *)pv = 0;
2197 *(uint8_t *)pv = uNewValue;
2198 break;
2199 }
2200 default:
2201 ASSERT_GUEST_MSG_FAILED(("%s: unsupported access width cb=%d iPort=%x iRegOffset=%x iReg=%x!!!\n",
2202 __FUNCTION__, cb, iPort, iRegOffset, iReg));
2203 }
2204 }
2205 }
2206
2207 return rc;
2208}
2209
2210/**
2211 * Writes a value to one of the AHCI controller registers.
2212 *
2213 * @returns Strict VBox status code.
2214 *
2215 * @param pDevIns The device instance.
2216 * @param pThis The shared AHCI state.
2217 * @param offReg The offset of the register to write to.
2218 * @param u32Value The value to write.
2219 */
2220static VBOXSTRICTRC ahciRegisterWrite(PPDMDEVINS pDevIns, PAHCI pThis, uint32_t offReg, uint32_t u32Value)
2221{
2222 VBOXSTRICTRC rc;
2223 uint32_t iReg;
2224
2225 /*
2226 * If the access offset is smaller than 100h the guest accesses the global registers.
2227 * Otherwise it accesses the registers of a port.
2228 */
2229 if (offReg < AHCI_HBA_GLOBAL_SIZE)
2230 {
2231 Log3(("Write global HBA register\n"));
2232 iReg = offReg >> 2;
2233 if (iReg < RT_ELEMENTS(g_aOpRegs))
2234 {
2235 const AHCIOPREG *pReg = &g_aOpRegs[iReg];
2236 rc = pReg->pfnWrite(pDevIns, pThis, iReg, u32Value);
2237 }
2238 else
2239 {
2240 Log3(("%s: Trying to write global register %u/%u!!!\n", __FUNCTION__, iReg, RT_ELEMENTS(g_aOpRegs)));
2241 rc = VINF_SUCCESS;
2242 }
2243 }
2244 else
2245 {
2246 uint32_t iPort;
2247 Log3(("Write Port register\n"));
2248 /* Calculate accessed port. */
2249 offReg -= AHCI_HBA_GLOBAL_SIZE;
2250 iPort = offReg / AHCI_PORT_REGISTER_SIZE;
2251 iReg = (offReg % AHCI_PORT_REGISTER_SIZE) >> 2;
2252 Log3(("%s: Trying to write to port %u and register %u\n", __FUNCTION__, iPort, iReg));
2253 if (RT_LIKELY( iPort < RT_MIN(pThis->cPortsImpl, RT_ELEMENTS(pThis->aPorts))
2254 && iReg < RT_ELEMENTS(g_aPortOpRegs)))
2255 {
2256 const AHCIPORTOPREG *pPortReg = &g_aPortOpRegs[iReg];
2257 rc = pPortReg->pfnWrite(pDevIns, pThis, &pThis->aPorts[iPort], iReg, u32Value);
2258 }
2259 else
2260 {
2261 Log3(("%s: Trying to write port %u register %u/%u!!!\n", __FUNCTION__, iPort, iReg, RT_ELEMENTS(g_aPortOpRegs)));
2262 rc = VINF_SUCCESS;
2263 }
2264 }
2265
2266 return rc;
2267}
2268
2269
2270/**
2271 * @callback_method_impl{IOMMMIONEWWRITE}
2272 */
2273static DECLCALLBACK(VBOXSTRICTRC) ahciMMIORead(PPDMDEVINS pDevIns, void *pvUser, RTGCPHYS off, void *pv, unsigned cb)
2274{
2275 PAHCI pThis = PDMDEVINS_2_DATA(pDevIns, PAHCI);
2276 Log2(("#%d ahciMMIORead: pvUser=%p:{%.*Rhxs} cb=%d off=%RGp\n", pDevIns->iInstance, pv, cb, pv, cb, off));
2277 RT_NOREF(pvUser);
2278
2279 VBOXSTRICTRC rc = ahciRegisterRead(pDevIns, pThis, off, pv, cb);
2280
2281 Log2(("#%d ahciMMIORead: return pvUser=%p:{%.*Rhxs} cb=%d off=%RGp rc=%Rrc\n",
2282 pDevIns->iInstance, pv, cb, pv, cb, off, VBOXSTRICTRC_VAL(rc)));
2283 return rc;
2284}
2285
2286/**
2287 * @callback_method_impl{IOMMMIONEWWRITE}
2288 */
2289static DECLCALLBACK(VBOXSTRICTRC) ahciMMIOWrite(PPDMDEVINS pDevIns, void *pvUser, RTGCPHYS off, void const *pv, unsigned cb)
2290{
2291 PAHCI pThis = PDMDEVINS_2_DATA(pDevIns, PAHCI);
2292
2293 Assert(cb == 4 || cb == 8); /* Assert IOM flags & sanity */
2294 Assert(!(off & (cb - 1))); /* Ditto. */
2295
2296 /* Break up 64 bits writes into two dword writes. */
2297 /** @todo Eliminate this code once the IOM/EM starts taking care of these
2298 * situations. */
2299 if (cb == 8)
2300 {
2301 /*
2302 * Only write the first 4 bytes if they weren't already.
2303 * It is possible that the last write to the register caused a world
2304 * switch and we entered this function again.
2305 * Writing the first 4 bytes again could cause indeterminate behavior
2306 * which can cause errors in the guest.
2307 */
2308 VBOXSTRICTRC rc = VINF_SUCCESS;
2309 if (!pThis->f8ByteMMIO4BytesWrittenSuccessfully)
2310 {
2311 rc = ahciMMIOWrite(pDevIns, pvUser, off, pv, 4);
2312 if (rc != VINF_SUCCESS)
2313 return rc;
2314
2315 pThis->f8ByteMMIO4BytesWrittenSuccessfully = true;
2316 }
2317
2318 rc = ahciMMIOWrite(pDevIns, pvUser, off + 4, (uint8_t *)pv + 4, 4);
2319 /*
2320 * Reset flag again so that the first 4 bytes are written again on the next
2321 * 8byte MMIO access.
2322 */
2323 if (rc == VINF_SUCCESS)
2324 pThis->f8ByteMMIO4BytesWrittenSuccessfully = false;
2325
2326 return rc;
2327 }
2328
2329 /* Do the access. */
2330 Log2(("#%d ahciMMIOWrite: pvUser=%p:{%.*Rhxs} cb=%d GCPhysAddr=%RGp\n", pDevIns->iInstance, pv, cb, pv, cb, off));
2331 return ahciRegisterWrite(pDevIns, pThis, off, *(uint32_t const *)pv);
2332}
2333
2334
2335/**
2336 * @callback_method_impl{FNIOMIOPORTNEWOUT,
2337 * Fake IDE port handler provided to make solaris happy.}
2338 */
2339static DECLCALLBACK(VBOXSTRICTRC)
2340ahciLegacyFakeWrite(PPDMDEVINS pDevIns, void *pvUser, RTIOPORT offPort, uint32_t u32, unsigned cb)
2341{
2342 RT_NOREF(pDevIns, pvUser, offPort, u32, cb);
2343 ASSERT_GUEST_MSG_FAILED(("Should not happen\n"));
2344 return VINF_SUCCESS;
2345}
2346
2347/**
2348 * @callback_method_impl{FNIOMIOPORTNEWIN,
2349 * Fake IDE port handler provided to make solaris happy.}
2350 */
2351static DECLCALLBACK(VBOXSTRICTRC)
2352ahciLegacyFakeRead(PPDMDEVINS pDevIns, void *pvUser, RTIOPORT offPort, uint32_t *pu32, unsigned cb)
2353{
2354 /** @todo we should set *pu32 to something. */
2355 RT_NOREF(pDevIns, pvUser, offPort, pu32, cb);
2356 ASSERT_GUEST_MSG_FAILED(("Should not happen\n"));
2357 return VINF_SUCCESS;
2358}
2359
2360
2361/**
2362 * @callback_method_impl{FNIOMIOPORTNEWOUT,
2363 * I/O port handler for writes to the index/data register pair.}
2364 */
2365static DECLCALLBACK(VBOXSTRICTRC) ahciIdxDataWrite(PPDMDEVINS pDevIns, void *pvUser, RTIOPORT offPort, uint32_t u32, unsigned cb)
2366{
2367 PAHCI pThis = PDMDEVINS_2_DATA(pDevIns, PAHCI);
2368 VBOXSTRICTRC rc = VINF_SUCCESS;
2369 RT_NOREF(pvUser, cb);
2370
2371 if (offPort >= 8)
2372 {
2373 ASSERT_GUEST(cb == 4);
2374
2375 uint32_t const iReg = (offPort - 8) / 4;
2376 if (iReg == 0)
2377 {
2378 /* Write the index register. */
2379 pThis->regIdx = u32;
2380 }
2381 else
2382 {
2383 /** @todo range check? */
2384 ASSERT_GUEST(iReg == 1);
2385 rc = ahciRegisterWrite(pDevIns, pThis, pThis->regIdx, u32);
2386 if (rc == VINF_IOM_R3_MMIO_WRITE)
2387 rc = VINF_IOM_R3_IOPORT_WRITE;
2388 }
2389 }
2390 /* else: ignore */
2391
2392 Log2(("#%d ahciIdxDataWrite: pu32=%p:{%.*Rhxs} cb=%d offPort=%#x rc=%Rrc\n",
2393 pDevIns->iInstance, &u32, cb, &u32, cb, offPort, VBOXSTRICTRC_VAL(rc)));
2394 return rc;
2395}
2396
2397/**
2398 * @callback_method_impl{FNIOMIOPORTNEWOUT,
2399 * I/O port handler for reads from the index/data register pair.}
2400 */
2401static DECLCALLBACK(VBOXSTRICTRC) ahciIdxDataRead(PPDMDEVINS pDevIns, void *pvUser, RTIOPORT offPort, uint32_t *pu32, unsigned cb)
2402{
2403 PAHCI pThis = PDMDEVINS_2_DATA(pDevIns, PAHCI);
2404 VBOXSTRICTRC rc = VINF_SUCCESS;
2405 RT_NOREF(pvUser);
2406
2407 if (offPort >= 8)
2408 {
2409 ASSERT_GUEST(cb == 4);
2410
2411 uint32_t const iReg = (offPort - 8) / 4;
2412 if (iReg == 0)
2413 {
2414 /* Read the index register. */
2415 *pu32 = pThis->regIdx;
2416 }
2417 else
2418 {
2419 /** @todo range check? */
2420 ASSERT_GUEST(iReg == 1);
2421 rc = ahciRegisterRead(pDevIns, pThis, pThis->regIdx, pu32, cb);
2422 if (rc == VINF_IOM_R3_MMIO_READ)
2423 rc = VINF_IOM_R3_IOPORT_READ;
2424 else if (rc == VINF_IOM_MMIO_UNUSED_00)
2425 rc = VERR_IOM_IOPORT_UNUSED;
2426 }
2427 }
2428 else
2429 *pu32 = UINT32_MAX;
2430
2431 Log2(("#%d ahciIdxDataRead: pu32=%p:{%.*Rhxs} cb=%d offPort=%#x rc=%Rrc\n",
2432 pDevIns->iInstance, pu32, cb, pu32, cb, offPort, VBOXSTRICTRC_VAL(rc)));
2433 return rc;
2434}
2435
2436#ifdef IN_RING3
2437
2438/* -=-=-=-=-=- PAHCI::ILeds -=-=-=-=-=- */
2439
2440/**
2441 * Gets the pointer to the status LED of a unit.
2442 *
2443 * @returns VBox status code.
2444 * @param pInterface Pointer to the interface structure containing the called function pointer.
2445 * @param iLUN The unit which status LED we desire.
2446 * @param ppLed Where to store the LED pointer.
2447 */
2448static DECLCALLBACK(int) ahciR3Status_QueryStatusLed(PPDMILEDPORTS pInterface, unsigned iLUN, PPDMLED *ppLed)
2449{
2450 PAHCICC pThisCC = RT_FROM_MEMBER(pInterface, AHCICC, ILeds);
2451 if (iLUN < AHCI_MAX_NR_PORTS_IMPL)
2452 {
2453 PAHCI pThis = PDMDEVINS_2_DATA(pThisCC->pDevIns, PAHCI);
2454 *ppLed = &pThis->aPorts[iLUN].Led;
2455 Assert((*ppLed)->u32Magic == PDMLED_MAGIC);
2456 return VINF_SUCCESS;
2457 }
2458 return VERR_PDM_LUN_NOT_FOUND;
2459}
2460
2461/**
2462 * @interface_method_impl{PDMIBASE,pfnQueryInterface}
2463 */
2464static DECLCALLBACK(void *) ahciR3Status_QueryInterface(PPDMIBASE pInterface, const char *pszIID)
2465{
2466 PAHCICC pThisCC = RT_FROM_MEMBER(pInterface, AHCICC, IBase);
2467 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIBASE, &pThisCC->IBase);
2468 PDMIBASE_RETURN_INTERFACE(pszIID, PDMILEDPORTS, &pThisCC->ILeds);
2469 return NULL;
2470}
2471
2472/**
2473 * @interface_method_impl{PDMIBASE,pfnQueryInterface}
2474 */
2475static DECLCALLBACK(void *) ahciR3PortQueryInterface(PPDMIBASE pInterface, const char *pszIID)
2476{
2477 PAHCIPORTR3 pAhciPortR3 = RT_FROM_MEMBER(pInterface, AHCIPORTR3, IBase);
2478 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIBASE, &pAhciPortR3->IBase);
2479 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIMEDIAPORT, &pAhciPortR3->IPort);
2480 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIMEDIAEXPORT, &pAhciPortR3->IMediaExPort);
2481 return NULL;
2482}
2483
2484/**
2485 * @interface_method_impl{PDMIMEDIAPORT,pfnQueryDeviceLocation}
2486 */
2487static DECLCALLBACK(int) ahciR3PortQueryDeviceLocation(PPDMIMEDIAPORT pInterface, const char **ppcszController,
2488 uint32_t *piInstance, uint32_t *piLUN)
2489{
2490 PAHCIPORTR3 pAhciPortR3 = RT_FROM_MEMBER(pInterface, AHCIPORTR3, IPort);
2491 PPDMDEVINS pDevIns = pAhciPortR3->pDevIns;
2492
2493 AssertPtrReturn(ppcszController, VERR_INVALID_POINTER);
2494 AssertPtrReturn(piInstance, VERR_INVALID_POINTER);
2495 AssertPtrReturn(piLUN, VERR_INVALID_POINTER);
2496
2497 *ppcszController = pDevIns->pReg->szName;
2498 *piInstance = pDevIns->iInstance;
2499 *piLUN = pAhciPortR3->iLUN;
2500
2501 return VINF_SUCCESS;
2502}
2503
2504/**
2505 * @interface_method_impl{PDMIMEDIAPORT,pfnQueryScsiInqStrings}
2506 */
2507static DECLCALLBACK(int) ahciR3PortQueryScsiInqStrings(PPDMIMEDIAPORT pInterface, const char **ppszVendorId,
2508 const char **ppszProductId, const char **ppszRevision)
2509{
2510 PAHCIPORTR3 pAhciPortR3 = RT_FROM_MEMBER(pInterface, AHCIPORTR3, IPort);
2511 PAHCI pThis = PDMDEVINS_2_DATA(pAhciPortR3->pDevIns, PAHCI);
2512 PAHCIPORT pAhciPort = &RT_SAFE_SUBSCRIPT(pThis->aPorts, pAhciPortR3->iLUN);
2513
2514 if (ppszVendorId)
2515 *ppszVendorId = &pAhciPort->szInquiryVendorId[0];
2516 if (ppszProductId)
2517 *ppszProductId = &pAhciPort->szInquiryProductId[0];
2518 if (ppszRevision)
2519 *ppszRevision = &pAhciPort->szInquiryRevision[0];
2520 return VINF_SUCCESS;
2521}
2522
2523#ifdef LOG_ENABLED
2524
2525/**
2526 * Dump info about the FIS
2527 *
2528 * @returns nothing
2529 * @param pAhciPort The port the command FIS was read from.
2530 * @param cmdFis The FIS to print info from.
2531 */
2532static void ahciDumpFisInfo(PAHCIPORT pAhciPort, uint8_t *cmdFis)
2533{
2534 ahciLog(("%s: *** Begin FIS info dump. ***\n", __FUNCTION__));
2535 /* Print FIS type. */
2536 switch (cmdFis[AHCI_CMDFIS_TYPE])
2537 {
2538 case AHCI_CMDFIS_TYPE_H2D:
2539 {
2540 ahciLog(("%s: Command Fis type: H2D\n", __FUNCTION__));
2541 ahciLog(("%s: Command Fis size: %d bytes\n", __FUNCTION__, AHCI_CMDFIS_TYPE_H2D_SIZE));
2542 if (cmdFis[AHCI_CMDFIS_BITS] & AHCI_CMDFIS_C)
2543 ahciLog(("%s: Command register update\n", __FUNCTION__));
2544 else
2545 ahciLog(("%s: Control register update\n", __FUNCTION__));
2546 ahciLog(("%s: CMD=%#04x \"%s\"\n", __FUNCTION__, cmdFis[AHCI_CMDFIS_CMD], ATACmdText(cmdFis[AHCI_CMDFIS_CMD])));
2547 ahciLog(("%s: FEAT=%#04x\n", __FUNCTION__, cmdFis[AHCI_CMDFIS_FET]));
2548 ahciLog(("%s: SECTN=%#04x\n", __FUNCTION__, cmdFis[AHCI_CMDFIS_SECTN]));
2549 ahciLog(("%s: CYLL=%#04x\n", __FUNCTION__, cmdFis[AHCI_CMDFIS_CYLL]));
2550 ahciLog(("%s: CYLH=%#04x\n", __FUNCTION__, cmdFis[AHCI_CMDFIS_CYLH]));
2551 ahciLog(("%s: HEAD=%#04x\n", __FUNCTION__, cmdFis[AHCI_CMDFIS_HEAD]));
2552
2553 ahciLog(("%s: SECTNEXP=%#04x\n", __FUNCTION__, cmdFis[AHCI_CMDFIS_SECTNEXP]));
2554 ahciLog(("%s: CYLLEXP=%#04x\n", __FUNCTION__, cmdFis[AHCI_CMDFIS_CYLLEXP]));
2555 ahciLog(("%s: CYLHEXP=%#04x\n", __FUNCTION__, cmdFis[AHCI_CMDFIS_CYLHEXP]));
2556 ahciLog(("%s: FETEXP=%#04x\n", __FUNCTION__, cmdFis[AHCI_CMDFIS_FETEXP]));
2557
2558 ahciLog(("%s: SECTC=%#04x\n", __FUNCTION__, cmdFis[AHCI_CMDFIS_SECTC]));
2559 ahciLog(("%s: SECTCEXP=%#04x\n", __FUNCTION__, cmdFis[AHCI_CMDFIS_SECTCEXP]));
2560 ahciLog(("%s: CTL=%#04x\n", __FUNCTION__, cmdFis[AHCI_CMDFIS_CTL]));
2561 if (cmdFis[AHCI_CMDFIS_CTL] & AHCI_CMDFIS_CTL_SRST)
2562 ahciLog(("%s: Reset bit is set\n", __FUNCTION__));
2563 break;
2564 }
2565 case AHCI_CMDFIS_TYPE_D2H:
2566 {
2567 ahciLog(("%s: Command Fis type D2H\n", __FUNCTION__));
2568 ahciLog(("%s: Command Fis size: %d\n", __FUNCTION__, AHCI_CMDFIS_TYPE_D2H_SIZE));
2569 break;
2570 }
2571 case AHCI_CMDFIS_TYPE_SETDEVBITS:
2572 {
2573 ahciLog(("%s: Command Fis type Set Device Bits\n", __FUNCTION__));
2574 ahciLog(("%s: Command Fis size: %d\n", __FUNCTION__, AHCI_CMDFIS_TYPE_SETDEVBITS_SIZE));
2575 break;
2576 }
2577 case AHCI_CMDFIS_TYPE_DMAACTD2H:
2578 {
2579 ahciLog(("%s: Command Fis type DMA Activate H2D\n", __FUNCTION__));
2580 ahciLog(("%s: Command Fis size: %d\n", __FUNCTION__, AHCI_CMDFIS_TYPE_DMAACTD2H_SIZE));
2581 break;
2582 }
2583 case AHCI_CMDFIS_TYPE_DMASETUP:
2584 {
2585 ahciLog(("%s: Command Fis type DMA Setup\n", __FUNCTION__));
2586 ahciLog(("%s: Command Fis size: %d\n", __FUNCTION__, AHCI_CMDFIS_TYPE_DMASETUP_SIZE));
2587 break;
2588 }
2589 case AHCI_CMDFIS_TYPE_PIOSETUP:
2590 {
2591 ahciLog(("%s: Command Fis type PIO Setup\n", __FUNCTION__));
2592 ahciLog(("%s: Command Fis size: %d\n", __FUNCTION__, AHCI_CMDFIS_TYPE_PIOSETUP_SIZE));
2593 break;
2594 }
2595 case AHCI_CMDFIS_TYPE_DATA:
2596 {
2597 ahciLog(("%s: Command Fis type Data\n", __FUNCTION__));
2598 break;
2599 }
2600 default:
2601 ahciLog(("%s: ERROR Unknown command FIS type\n", __FUNCTION__));
2602 break;
2603 }
2604 ahciLog(("%s: *** End FIS info dump. ***\n", __FUNCTION__));
2605}
2606
2607/**
2608 * Dump info about the command header
2609 *
2610 * @returns nothing
2611 * @param pAhciPort Pointer to the port the command header was read from.
2612 * @param pCmdHdr The command header to print info from.
2613 */
2614static void ahciDumpCmdHdrInfo(PAHCIPORT pAhciPort, CmdHdr *pCmdHdr)
2615{
2616 ahciLog(("%s: *** Begin command header info dump. ***\n", __FUNCTION__));
2617 ahciLog(("%s: Number of Scatter/Gatther List entries: %u\n", __FUNCTION__, AHCI_CMDHDR_PRDTL_ENTRIES(pCmdHdr->u32DescInf)));
2618 if (pCmdHdr->u32DescInf & AHCI_CMDHDR_C)
2619 ahciLog(("%s: Clear busy upon R_OK\n", __FUNCTION__));
2620 if (pCmdHdr->u32DescInf & AHCI_CMDHDR_B)
2621 ahciLog(("%s: BIST Fis\n", __FUNCTION__));
2622 if (pCmdHdr->u32DescInf & AHCI_CMDHDR_R)
2623 ahciLog(("%s: Device Reset Fis\n", __FUNCTION__));
2624 if (pCmdHdr->u32DescInf & AHCI_CMDHDR_P)
2625 ahciLog(("%s: Command prefetchable\n", __FUNCTION__));
2626 if (pCmdHdr->u32DescInf & AHCI_CMDHDR_W)
2627 ahciLog(("%s: Device write\n", __FUNCTION__));
2628 else
2629 ahciLog(("%s: Device read\n", __FUNCTION__));
2630 if (pCmdHdr->u32DescInf & AHCI_CMDHDR_A)
2631 ahciLog(("%s: ATAPI command\n", __FUNCTION__));
2632 else
2633 ahciLog(("%s: ATA command\n", __FUNCTION__));
2634
2635 ahciLog(("%s: Command FIS length %u DW\n", __FUNCTION__, (pCmdHdr->u32DescInf & AHCI_CMDHDR_CFL_MASK)));
2636 ahciLog(("%s: *** End command header info dump. ***\n", __FUNCTION__));
2637}
2638
2639#endif /* LOG_ENABLED */
2640
2641/**
2642 * Post the first D2H FIS from the device into guest memory.
2643 *
2644 * @returns nothing
2645 * @param pDevIns The device instance.
2646 * @param pAhciPort Pointer to the port which "receives" the FIS.
2647 */
2648static void ahciPostFirstD2HFisIntoMemory(PPDMDEVINS pDevIns, PAHCIPORT pAhciPort)
2649{
2650 uint8_t d2hFis[AHCI_CMDFIS_TYPE_D2H_SIZE];
2651
2652 pAhciPort->fFirstD2HFisSent = true;
2653
2654 ahciLog(("%s: Sending First D2H FIS from FIFO\n", __FUNCTION__));
2655 memset(&d2hFis[0], 0, sizeof(d2hFis));
2656 d2hFis[AHCI_CMDFIS_TYPE] = AHCI_CMDFIS_TYPE_D2H;
2657 d2hFis[AHCI_CMDFIS_ERR] = 0x01;
2658
2659 d2hFis[AHCI_CMDFIS_STS] = 0x00;
2660
2661 /* Set the signature based on the device type. */
2662 if (pAhciPort->fATAPI)
2663 {
2664 d2hFis[AHCI_CMDFIS_CYLL] = 0x14;
2665 d2hFis[AHCI_CMDFIS_CYLH] = 0xeb;
2666 }
2667 else
2668 {
2669 d2hFis[AHCI_CMDFIS_CYLL] = 0x00;
2670 d2hFis[AHCI_CMDFIS_CYLH] = 0x00;
2671 }
2672
2673 d2hFis[AHCI_CMDFIS_HEAD] = 0x00;
2674 d2hFis[AHCI_CMDFIS_SECTN] = 0x01;
2675 d2hFis[AHCI_CMDFIS_SECTC] = 0x01;
2676
2677 pAhciPort->regTFD = (1 << 8) | ATA_STAT_SEEK | ATA_STAT_WRERR;
2678 if (!pAhciPort->fATAPI)
2679 pAhciPort->regTFD |= ATA_STAT_READY;
2680
2681 ahciPostFisIntoMemory(pDevIns, pAhciPort, AHCI_CMDFIS_TYPE_D2H, d2hFis);
2682}
2683
2684/**
2685 * Post the FIS in the memory area allocated by the guest and set interrupt if necessary.
2686 *
2687 * @returns VBox status code
2688 * @param pDevIns The device instance.
2689 * @param pAhciPort The port which "receives" the FIS.
2690 * @param uFisType The type of the FIS.
2691 * @param pCmdFis Pointer to the FIS which is to be posted into memory.
2692 */
2693static int ahciPostFisIntoMemory(PPDMDEVINS pDevIns, PAHCIPORT pAhciPort, unsigned uFisType, uint8_t *pCmdFis)
2694{
2695 int rc = VINF_SUCCESS;
2696 RTGCPHYS GCPhysAddrRecFis = pAhciPort->GCPhysAddrFb;
2697 unsigned cbFis = 0;
2698
2699 ahciLog(("%s: pAhciPort=%p uFisType=%u pCmdFis=%p\n", __FUNCTION__, pAhciPort, uFisType, pCmdFis));
2700
2701 if (pAhciPort->regCMD & AHCI_PORT_CMD_FRE)
2702 {
2703 AssertMsg(GCPhysAddrRecFis, ("%s: GCPhysAddrRecFis is 0\n", __FUNCTION__));
2704
2705 /* Determine the offset and size of the FIS based on uFisType. */
2706 switch (uFisType)
2707 {
2708 case AHCI_CMDFIS_TYPE_D2H:
2709 {
2710 GCPhysAddrRecFis += AHCI_RECFIS_RFIS_OFFSET;
2711 cbFis = AHCI_CMDFIS_TYPE_D2H_SIZE;
2712 break;
2713 }
2714 case AHCI_CMDFIS_TYPE_SETDEVBITS:
2715 {
2716 GCPhysAddrRecFis += AHCI_RECFIS_SDBFIS_OFFSET;
2717 cbFis = AHCI_CMDFIS_TYPE_SETDEVBITS_SIZE;
2718 break;
2719 }
2720 case AHCI_CMDFIS_TYPE_DMASETUP:
2721 {
2722 GCPhysAddrRecFis += AHCI_RECFIS_DSFIS_OFFSET;
2723 cbFis = AHCI_CMDFIS_TYPE_DMASETUP_SIZE;
2724 break;
2725 }
2726 case AHCI_CMDFIS_TYPE_PIOSETUP:
2727 {
2728 GCPhysAddrRecFis += AHCI_RECFIS_PSFIS_OFFSET;
2729 cbFis = AHCI_CMDFIS_TYPE_PIOSETUP_SIZE;
2730 break;
2731 }
2732 default:
2733 /*
2734 * We should post the unknown FIS into memory too but this never happens because
2735 * we know which FIS types we generate. ;)
2736 */
2737 AssertMsgFailed(("%s: Unknown FIS type!\n", __FUNCTION__));
2738 }
2739
2740 /* Post the FIS into memory. */
2741 ahciLog(("%s: PDMDevHlpPCIPhysWrite GCPhysAddrRecFis=%RGp cbFis=%u\n", __FUNCTION__, GCPhysAddrRecFis, cbFis));
2742 PDMDevHlpPCIPhysWrite(pDevIns, GCPhysAddrRecFis, pCmdFis, cbFis);
2743 }
2744
2745 return rc;
2746}
2747
2748DECLINLINE(void) ahciReqSetStatus(PAHCIREQ pAhciReq, uint8_t u8Error, uint8_t u8Status)
2749{
2750 pAhciReq->cmdFis[AHCI_CMDFIS_ERR] = u8Error;
2751 pAhciReq->cmdFis[AHCI_CMDFIS_STS] = u8Status;
2752}
2753
2754static void ataPadString(uint8_t *pbDst, const char *pbSrc, uint32_t cbSize)
2755{
2756 for (uint32_t i = 0; i < cbSize; i++)
2757 {
2758 if (*pbSrc)
2759 pbDst[i ^ 1] = *pbSrc++;
2760 else
2761 pbDst[i ^ 1] = ' ';
2762 }
2763}
2764
2765static uint32_t ataChecksum(void* ptr, size_t count)
2766{
2767 uint8_t u8Sum = 0xa5, *p = (uint8_t*)ptr;
2768 size_t i;
2769
2770 for (i = 0; i < count; i++)
2771 {
2772 u8Sum += *p++;
2773 }
2774
2775 return (uint8_t)-(int32_t)u8Sum;
2776}
2777
2778static int ahciIdentifySS(PAHCI pThis, PAHCIPORT pAhciPort, PAHCIPORTR3 pAhciPortR3, void *pvBuf)
2779{
2780 uint16_t *p = (uint16_t *)pvBuf;
2781 memset(p, 0, 512);
2782 p[0] = RT_H2LE_U16(0x0040);
2783 p[1] = RT_H2LE_U16(RT_MIN(pAhciPort->PCHSGeometry.cCylinders, 16383));
2784 p[3] = RT_H2LE_U16(pAhciPort->PCHSGeometry.cHeads);
2785 /* Block size; obsolete, but required for the BIOS. */
2786 p[5] = RT_H2LE_U16(512);
2787 p[6] = RT_H2LE_U16(pAhciPort->PCHSGeometry.cSectors);
2788 ataPadString((uint8_t *)(p + 10), pAhciPort->szSerialNumber, AHCI_SERIAL_NUMBER_LENGTH); /* serial number */
2789 p[20] = RT_H2LE_U16(3); /* XXX: retired, cache type */
2790 p[21] = RT_H2LE_U16(512); /* XXX: retired, cache size in sectors */
2791 p[22] = RT_H2LE_U16(0); /* ECC bytes per sector */
2792 ataPadString((uint8_t *)(p + 23), pAhciPort->szFirmwareRevision, AHCI_FIRMWARE_REVISION_LENGTH); /* firmware version */
2793 ataPadString((uint8_t *)(p + 27), pAhciPort->szModelNumber, AHCI_MODEL_NUMBER_LENGTH); /* model */
2794#if ATA_MAX_MULT_SECTORS > 1
2795 p[47] = RT_H2LE_U16(0x8000 | ATA_MAX_MULT_SECTORS);
2796#endif
2797 p[48] = RT_H2LE_U16(1); /* dword I/O, used by the BIOS */
2798 p[49] = RT_H2LE_U16(1 << 11 | 1 << 9 | 1 << 8); /* DMA and LBA supported */
2799 p[50] = RT_H2LE_U16(1 << 14); /* No drive specific standby timer minimum */
2800 p[51] = RT_H2LE_U16(240); /* PIO transfer cycle */
2801 p[52] = RT_H2LE_U16(240); /* DMA transfer cycle */
2802 p[53] = RT_H2LE_U16(1 | 1 << 1 | 1 << 2); /* words 54-58,64-70,88 valid */
2803 p[54] = RT_H2LE_U16(RT_MIN(pAhciPort->PCHSGeometry.cCylinders, 16383));
2804 p[55] = RT_H2LE_U16(pAhciPort->PCHSGeometry.cHeads);
2805 p[56] = RT_H2LE_U16(pAhciPort->PCHSGeometry.cSectors);
2806 p[57] = RT_H2LE_U16(RT_MIN(pAhciPort->PCHSGeometry.cCylinders, 16383) * pAhciPort->PCHSGeometry.cHeads * pAhciPort->PCHSGeometry.cSectors);
2807 p[58] = RT_H2LE_U16(RT_MIN(pAhciPort->PCHSGeometry.cCylinders, 16383) * pAhciPort->PCHSGeometry.cHeads * pAhciPort->PCHSGeometry.cSectors >> 16);
2808 if (pAhciPort->cMultSectors)
2809 p[59] = RT_H2LE_U16(0x100 | pAhciPort->cMultSectors);
2810 if (pAhciPort->cTotalSectors <= (1 << 28) - 1)
2811 {
2812 p[60] = RT_H2LE_U16(pAhciPort->cTotalSectors);
2813 p[61] = RT_H2LE_U16(pAhciPort->cTotalSectors >> 16);
2814 }
2815 else
2816 {
2817 /* Report maximum number of sectors possible with LBA28 */
2818 p[60] = RT_H2LE_U16(((1 << 28) - 1) & 0xffff);
2819 p[61] = RT_H2LE_U16(((1 << 28) - 1) >> 16);
2820 }
2821 p[63] = RT_H2LE_U16(ATA_TRANSFER_ID(ATA_MODE_MDMA, ATA_MDMA_MODE_MAX, pAhciPort->uATATransferMode)); /* MDMA modes supported / mode enabled */
2822 p[64] = RT_H2LE_U16(ATA_PIO_MODE_MAX > 2 ? (1 << (ATA_PIO_MODE_MAX - 2)) - 1 : 0); /* PIO modes beyond PIO2 supported */
2823 p[65] = RT_H2LE_U16(120); /* minimum DMA multiword tx cycle time */
2824 p[66] = RT_H2LE_U16(120); /* recommended DMA multiword tx cycle time */
2825 p[67] = RT_H2LE_U16(120); /* minimum PIO cycle time without flow control */
2826 p[68] = RT_H2LE_U16(120); /* minimum PIO cycle time with IORDY flow control */
2827 if ( pAhciPort->fTrimEnabled
2828 || pAhciPort->cbSector != 512
2829 || pAhciPortR3->pDrvMedia->pfnIsNonRotational(pAhciPortR3->pDrvMedia))
2830 {
2831 p[80] = RT_H2LE_U16(0x1f0); /* support everything up to ATA/ATAPI-8 ACS */
2832 p[81] = RT_H2LE_U16(0x28); /* conforms to ATA/ATAPI-8 ACS */
2833 }
2834 else
2835 {
2836 p[80] = RT_H2LE_U16(0x7e); /* support everything up to ATA/ATAPI-6 */
2837 p[81] = RT_H2LE_U16(0x22); /* conforms to ATA/ATAPI-6 */
2838 }
2839 p[82] = RT_H2LE_U16(1 << 3 | 1 << 5 | 1 << 6); /* supports power management, write cache and look-ahead */
2840 p[83] = RT_H2LE_U16(1 << 14 | 1 << 10 | 1 << 12 | 1 << 13); /* supports LBA48, FLUSH CACHE and FLUSH CACHE EXT */
2841 p[84] = RT_H2LE_U16(1 << 14);
2842 p[85] = RT_H2LE_U16(1 << 3 | 1 << 5 | 1 << 6); /* enabled power management, write cache and look-ahead */
2843 p[86] = RT_H2LE_U16(1 << 10 | 1 << 12 | 1 << 13); /* enabled LBA48, FLUSH CACHE and FLUSH CACHE EXT */
2844 p[87] = RT_H2LE_U16(1 << 14);
2845 p[88] = RT_H2LE_U16(ATA_TRANSFER_ID(ATA_MODE_UDMA, ATA_UDMA_MODE_MAX, pAhciPort->uATATransferMode)); /* UDMA modes supported / mode enabled */
2846 p[93] = RT_H2LE_U16(0x00);
2847 p[100] = RT_H2LE_U16(pAhciPort->cTotalSectors);
2848 p[101] = RT_H2LE_U16(pAhciPort->cTotalSectors >> 16);
2849 p[102] = RT_H2LE_U16(pAhciPort->cTotalSectors >> 32);
2850 p[103] = RT_H2LE_U16(pAhciPort->cTotalSectors >> 48);
2851
2852 /* valid information, more than one logical sector per physical sector, 2^cLogSectorsPerPhysicalExp logical sectors per physical sector */
2853 if (pAhciPort->cLogSectorsPerPhysicalExp)
2854 p[106] = RT_H2LE_U16(RT_BIT(14) | RT_BIT(13) | pAhciPort->cLogSectorsPerPhysicalExp);
2855
2856 if (pAhciPort->cbSector != 512)
2857 {
2858 uint32_t cSectorSizeInWords = pAhciPort->cbSector / sizeof(uint16_t);
2859 /* Enable reporting of logical sector size. */
2860 p[106] |= RT_H2LE_U16(RT_BIT(12) | RT_BIT(14));
2861 p[117] = RT_H2LE_U16(cSectorSizeInWords);
2862 p[118] = RT_H2LE_U16(cSectorSizeInWords >> 16);
2863 }
2864
2865 if (pAhciPortR3->pDrvMedia->pfnIsNonRotational(pAhciPortR3->pDrvMedia))
2866 p[217] = RT_H2LE_U16(1); /* Non-rotational medium */
2867
2868 if (pAhciPort->fTrimEnabled) /** @todo Set bit 14 in word 69 too? (Deterministic read after TRIM). */
2869 p[169] = RT_H2LE_U16(1); /* DATA SET MANAGEMENT command supported. */
2870
2871 /* The following are SATA specific */
2872 p[75] = RT_H2LE_U16(pThis->cCmdSlotsAvail - 1); /* Number of commands we support, 0's based */
2873 p[76] = RT_H2LE_U16((1 << 8) | (1 << 2)); /* Native command queuing and Serial ATA Gen2 (3.0 Gbps) speed supported */
2874
2875 uint32_t uCsum = ataChecksum(p, 510);
2876 p[255] = RT_H2LE_U16(0xa5 | (uCsum << 8)); /* Integrity word */
2877
2878 return VINF_SUCCESS;
2879}
2880
2881static int ahciR3AtapiIdentify(PPDMDEVINS pDevIns, PAHCIREQ pAhciReq, PAHCIPORT pAhciPort, size_t cbData, size_t *pcbData)
2882{
2883 uint16_t p[256];
2884
2885 memset(p, 0, 512);
2886 /* Removable CDROM, 50us response, 12 byte packets */
2887 p[0] = RT_H2LE_U16(2 << 14 | 5 << 8 | 1 << 7 | 2 << 5 | 0 << 0);
2888 ataPadString((uint8_t *)(p + 10), pAhciPort->szSerialNumber, AHCI_SERIAL_NUMBER_LENGTH); /* serial number */
2889 p[20] = RT_H2LE_U16(3); /* XXX: retired, cache type */
2890 p[21] = RT_H2LE_U16(512); /* XXX: retired, cache size in sectors */
2891 ataPadString((uint8_t *)(p + 23), pAhciPort->szFirmwareRevision, AHCI_FIRMWARE_REVISION_LENGTH); /* firmware version */
2892 ataPadString((uint8_t *)(p + 27), pAhciPort->szModelNumber, AHCI_MODEL_NUMBER_LENGTH); /* model */
2893 p[49] = RT_H2LE_U16(1 << 11 | 1 << 9 | 1 << 8); /* DMA and LBA supported */
2894 p[50] = RT_H2LE_U16(1 << 14); /* No drive specific standby timer minimum */
2895 p[51] = RT_H2LE_U16(240); /* PIO transfer cycle */
2896 p[52] = RT_H2LE_U16(240); /* DMA transfer cycle */
2897 p[53] = RT_H2LE_U16(1 << 1 | 1 << 2); /* words 64-70,88 are valid */
2898 p[63] = RT_H2LE_U16(ATA_TRANSFER_ID(ATA_MODE_MDMA, ATA_MDMA_MODE_MAX, pAhciPort->uATATransferMode)); /* MDMA modes supported / mode enabled */
2899 p[64] = RT_H2LE_U16(ATA_PIO_MODE_MAX > 2 ? (1 << (ATA_PIO_MODE_MAX - 2)) - 1 : 0); /* PIO modes beyond PIO2 supported */
2900 p[65] = RT_H2LE_U16(120); /* minimum DMA multiword tx cycle time */
2901 p[66] = RT_H2LE_U16(120); /* recommended DMA multiword tx cycle time */
2902 p[67] = RT_H2LE_U16(120); /* minimum PIO cycle time without flow control */
2903 p[68] = RT_H2LE_U16(120); /* minimum PIO cycle time with IORDY flow control */
2904 p[73] = RT_H2LE_U16(0x003e); /* ATAPI CDROM major */
2905 p[74] = RT_H2LE_U16(9); /* ATAPI CDROM minor */
2906 p[80] = RT_H2LE_U16(0x7e); /* support everything up to ATA/ATAPI-6 */
2907 p[81] = RT_H2LE_U16(0x22); /* conforms to ATA/ATAPI-6 */
2908 p[82] = RT_H2LE_U16(1 << 4 | 1 << 9); /* supports packet command set and DEVICE RESET */
2909 p[83] = RT_H2LE_U16(1 << 14);
2910 p[84] = RT_H2LE_U16(1 << 14);
2911 p[85] = RT_H2LE_U16(1 << 4 | 1 << 9); /* enabled packet command set and DEVICE RESET */
2912 p[86] = RT_H2LE_U16(0);
2913 p[87] = RT_H2LE_U16(1 << 14);
2914 p[88] = RT_H2LE_U16(ATA_TRANSFER_ID(ATA_MODE_UDMA, ATA_UDMA_MODE_MAX, pAhciPort->uATATransferMode)); /* UDMA modes supported / mode enabled */
2915 p[93] = RT_H2LE_U16((1 | 1 << 1) << ((pAhciPort->iLUN & 1) == 0 ? 0 : 8) | 1 << 13 | 1 << 14);
2916
2917 /* The following are SATA specific */
2918 p[75] = RT_H2LE_U16(31); /* We support 32 commands */
2919 p[76] = RT_H2LE_U16((1 << 8) | (1 << 2)); /* Native command queuing and Serial ATA Gen2 (3.0 Gbps) speed supported */
2920
2921 /* Copy the buffer in to the scatter gather list. */
2922 *pcbData = ahciR3CopyBufferToPrdtl(pDevIns, pAhciReq, (void *)&p[0], RT_MIN(cbData, sizeof(p)), 0 /* cbSkip */);
2923 return VINF_SUCCESS;
2924}
2925
2926/**
2927 * Reset all values after a reset of the attached storage device.
2928 *
2929 * @returns nothing
2930 * @param pDevIns The device instance.
2931 * @param pThis The shared AHCI state.
2932 * @param pAhciPort The port the device is attached to, shared bits.
2933 * @param pAhciReq The state to get the tag number from.
2934 */
2935static void ahciFinishStorageDeviceReset(PPDMDEVINS pDevIns, PAHCI pThis, PAHCIPORT pAhciPort, PAHCIREQ pAhciReq)
2936{
2937 int rc;
2938
2939 /* Send a status good D2H FIS. */
2940 pAhciPort->fResetDevice = false;
2941 if (pAhciPort->regCMD & AHCI_PORT_CMD_FRE)
2942 ahciPostFirstD2HFisIntoMemory(pDevIns, pAhciPort);
2943
2944 /* As this is the first D2H FIS after the reset update the signature in the SIG register of the port. */
2945 if (pAhciPort->fATAPI)
2946 pAhciPort->regSIG = AHCI_PORT_SIG_ATAPI;
2947 else
2948 pAhciPort->regSIG = AHCI_PORT_SIG_DISK;
2949 ASMAtomicOrU32(&pAhciPort->u32TasksFinished, (1 << pAhciReq->uTag));
2950
2951 rc = ahciHbaSetInterrupt(pDevIns, pThis, pAhciPort->iLUN, VERR_IGNORED);
2952 AssertRC(rc);
2953}
2954
2955/**
2956 * Initiates a device reset caused by ATA_DEVICE_RESET (ATAPI only).
2957 *
2958 * @returns nothing.
2959 * @param pDevIns The device instance.
2960 * @param pThis The shared AHCI state.
2961 * @param pAhciPort The device to reset.
2962 * @param pAhciReq The task state.
2963 */
2964static void ahciDeviceReset(PPDMDEVINS pDevIns, PAHCI pThis, PAHCIPORT pAhciPort, PAHCIREQ pAhciReq)
2965{
2966 ASMAtomicWriteBool(&pAhciPort->fResetDevice, true);
2967
2968 /*
2969 * Because this ATAPI only and ATAPI can't have
2970 * more than one command active at a time the task counter should be 0
2971 * and it is possible to finish the reset now.
2972 */
2973 Assert(ASMAtomicReadU32(&pAhciPort->cTasksActive) == 0);
2974 ahciFinishStorageDeviceReset(pDevIns, pThis, pAhciPort, pAhciReq);
2975}
2976
2977/**
2978 * Create a PIO setup FIS and post it into the memory area of the guest.
2979 *
2980 * @returns nothing.
2981 * @param pDevIns The device instance.
2982 * @param pThis The shared AHCI state.
2983 * @param pAhciPort The port of the SATA controller.
2984 * @param cbTransfer Transfer size of the request.
2985 * @param pCmdFis Pointer to the command FIS from the guest.
2986 * @param fRead Flag whether this is a read request.
2987 * @param fInterrupt If an interrupt should be send to the guest.
2988 */
2989static void ahciSendPioSetupFis(PPDMDEVINS pDevIns, PAHCI pThis, PAHCIPORT pAhciPort,
2990 size_t cbTransfer, uint8_t *pCmdFis, bool fRead, bool fInterrupt)
2991{
2992 uint8_t abPioSetupFis[20];
2993 bool fAssertIntr = false;
2994
2995 ahciLog(("%s: building PIO setup Fis\n", __FUNCTION__));
2996
2997 AssertMsg( cbTransfer > 0
2998 && cbTransfer <= 65534,
2999 ("Can't send PIO setup FIS for requests with 0 bytes to transfer or greater than 65534\n"));
3000
3001 if (pAhciPort->regCMD & AHCI_PORT_CMD_FRE)
3002 {
3003 memset(&abPioSetupFis[0], 0, sizeof(abPioSetupFis));
3004 abPioSetupFis[AHCI_CMDFIS_TYPE] = AHCI_CMDFIS_TYPE_PIOSETUP;
3005 abPioSetupFis[AHCI_CMDFIS_BITS] = (fInterrupt ? AHCI_CMDFIS_I : 0);
3006 if (fRead)
3007 abPioSetupFis[AHCI_CMDFIS_BITS] |= AHCI_CMDFIS_D;
3008 abPioSetupFis[AHCI_CMDFIS_STS] = pCmdFis[AHCI_CMDFIS_STS];
3009 abPioSetupFis[AHCI_CMDFIS_ERR] = pCmdFis[AHCI_CMDFIS_ERR];
3010 abPioSetupFis[AHCI_CMDFIS_SECTN] = pCmdFis[AHCI_CMDFIS_SECTN];
3011 abPioSetupFis[AHCI_CMDFIS_CYLL] = pCmdFis[AHCI_CMDFIS_CYLL];
3012 abPioSetupFis[AHCI_CMDFIS_CYLH] = pCmdFis[AHCI_CMDFIS_CYLH];
3013 abPioSetupFis[AHCI_CMDFIS_HEAD] = pCmdFis[AHCI_CMDFIS_HEAD];
3014 abPioSetupFis[AHCI_CMDFIS_SECTNEXP] = pCmdFis[AHCI_CMDFIS_SECTNEXP];
3015 abPioSetupFis[AHCI_CMDFIS_CYLLEXP] = pCmdFis[AHCI_CMDFIS_CYLLEXP];
3016 abPioSetupFis[AHCI_CMDFIS_CYLHEXP] = pCmdFis[AHCI_CMDFIS_CYLHEXP];
3017 abPioSetupFis[AHCI_CMDFIS_SECTC] = pCmdFis[AHCI_CMDFIS_SECTC];
3018 abPioSetupFis[AHCI_CMDFIS_SECTCEXP] = pCmdFis[AHCI_CMDFIS_SECTCEXP];
3019
3020 /* Set transfer count. */
3021 abPioSetupFis[16] = (cbTransfer >> 8) & 0xff;
3022 abPioSetupFis[17] = cbTransfer & 0xff;
3023
3024 /* Update registers. */
3025 pAhciPort->regTFD = (pCmdFis[AHCI_CMDFIS_ERR] << 8) | pCmdFis[AHCI_CMDFIS_STS];
3026
3027 ahciPostFisIntoMemory(pDevIns, pAhciPort, AHCI_CMDFIS_TYPE_PIOSETUP, abPioSetupFis);
3028
3029 if (fInterrupt)
3030 {
3031 ASMAtomicOrU32(&pAhciPort->regIS, AHCI_PORT_IS_PSS);
3032 /* Check if we should assert an interrupt */
3033 if (pAhciPort->regIE & AHCI_PORT_IE_PSE)
3034 fAssertIntr = true;
3035 }
3036
3037 if (fAssertIntr)
3038 {
3039 int rc = ahciHbaSetInterrupt(pDevIns, pThis, pAhciPort->iLUN, VERR_IGNORED);
3040 AssertRC(rc);
3041 }
3042 }
3043}
3044
3045/**
3046 * Build a D2H FIS and post into the memory area of the guest.
3047 *
3048 * @returns Nothing
3049 * @param pDevIns The device instance.
3050 * @param pThis The shared AHCI state.
3051 * @param pAhciPort The port of the SATA controller.
3052 * @param uTag The tag of the request.
3053 * @param pCmdFis Pointer to the command FIS from the guest.
3054 * @param fInterrupt If an interrupt should be send to the guest.
3055 */
3056static void ahciSendD2HFis(PPDMDEVINS pDevIns, PAHCI pThis, PAHCIPORT pAhciPort, uint32_t uTag, uint8_t *pCmdFis, bool fInterrupt)
3057{
3058 uint8_t d2hFis[20];
3059 bool fAssertIntr = false;
3060
3061 ahciLog(("%s: building D2H Fis\n", __FUNCTION__));
3062
3063 if (pAhciPort->regCMD & AHCI_PORT_CMD_FRE)
3064 {
3065 memset(&d2hFis[0], 0, sizeof(d2hFis));
3066 d2hFis[AHCI_CMDFIS_TYPE] = AHCI_CMDFIS_TYPE_D2H;
3067 d2hFis[AHCI_CMDFIS_BITS] = (fInterrupt ? AHCI_CMDFIS_I : 0);
3068 d2hFis[AHCI_CMDFIS_STS] = pCmdFis[AHCI_CMDFIS_STS];
3069 d2hFis[AHCI_CMDFIS_ERR] = pCmdFis[AHCI_CMDFIS_ERR];
3070 d2hFis[AHCI_CMDFIS_SECTN] = pCmdFis[AHCI_CMDFIS_SECTN];
3071 d2hFis[AHCI_CMDFIS_CYLL] = pCmdFis[AHCI_CMDFIS_CYLL];
3072 d2hFis[AHCI_CMDFIS_CYLH] = pCmdFis[AHCI_CMDFIS_CYLH];
3073 d2hFis[AHCI_CMDFIS_HEAD] = pCmdFis[AHCI_CMDFIS_HEAD];
3074 d2hFis[AHCI_CMDFIS_SECTNEXP] = pCmdFis[AHCI_CMDFIS_SECTNEXP];
3075 d2hFis[AHCI_CMDFIS_CYLLEXP] = pCmdFis[AHCI_CMDFIS_CYLLEXP];
3076 d2hFis[AHCI_CMDFIS_CYLHEXP] = pCmdFis[AHCI_CMDFIS_CYLHEXP];
3077 d2hFis[AHCI_CMDFIS_SECTC] = pCmdFis[AHCI_CMDFIS_SECTC];
3078 d2hFis[AHCI_CMDFIS_SECTCEXP] = pCmdFis[AHCI_CMDFIS_SECTCEXP];
3079
3080 /* Update registers. */
3081 pAhciPort->regTFD = (pCmdFis[AHCI_CMDFIS_ERR] << 8) | pCmdFis[AHCI_CMDFIS_STS];
3082
3083 ahciPostFisIntoMemory(pDevIns, pAhciPort, AHCI_CMDFIS_TYPE_D2H, d2hFis);
3084
3085 if (pCmdFis[AHCI_CMDFIS_STS] & ATA_STAT_ERR)
3086 {
3087 /* Error bit is set. */
3088 ASMAtomicOrU32(&pAhciPort->regIS, AHCI_PORT_IS_TFES);
3089 if (pAhciPort->regIE & AHCI_PORT_IE_TFEE)
3090 fAssertIntr = true;
3091 /*
3092 * Don't mark the command slot as completed because the guest
3093 * needs it to identify the failed command.
3094 */
3095 }
3096 else if (fInterrupt)
3097 {
3098 ASMAtomicOrU32(&pAhciPort->regIS, AHCI_PORT_IS_DHRS);
3099 /* Check if we should assert an interrupt */
3100 if (pAhciPort->regIE & AHCI_PORT_IE_DHRE)
3101 fAssertIntr = true;
3102
3103 /* Mark command as completed. */
3104 ASMAtomicOrU32(&pAhciPort->u32TasksFinished, RT_BIT_32(uTag));
3105 }
3106
3107 if (fAssertIntr)
3108 {
3109 int rc = ahciHbaSetInterrupt(pDevIns, pThis, pAhciPort->iLUN, VERR_IGNORED);
3110 AssertRC(rc);
3111 }
3112 }
3113}
3114
3115/**
3116 * Build a SDB Fis and post it into the memory area of the guest.
3117 *
3118 * @returns Nothing
3119 * @param pDevIns The device instance.
3120 * @param pThis The shared AHCI state.
3121 * @param pAhciPort The port for which the SDB Fis is send, shared bits.
3122 * @param pAhciPortR3 The port for which the SDB Fis is send, ring-3 bits.
3123 * @param uFinishedTasks Bitmask of finished tasks.
3124 * @param fInterrupt If an interrupt should be asserted.
3125 */
3126static void ahciSendSDBFis(PPDMDEVINS pDevIns, PAHCI pThis, PAHCIPORT pAhciPort, PAHCIPORTR3 pAhciPortR3,
3127 uint32_t uFinishedTasks, bool fInterrupt)
3128{
3129 uint32_t sdbFis[2];
3130 bool fAssertIntr = false;
3131 PAHCIREQ pTaskErr = ASMAtomicReadPtrT(&pAhciPortR3->pTaskErr, PAHCIREQ);
3132
3133 ahciLog(("%s: Building SDB FIS\n", __FUNCTION__));
3134
3135 if (pAhciPort->regCMD & AHCI_PORT_CMD_FRE)
3136 {
3137 memset(&sdbFis[0], 0, sizeof(sdbFis));
3138 sdbFis[0] = AHCI_CMDFIS_TYPE_SETDEVBITS;
3139 sdbFis[0] |= (fInterrupt ? (1 << 14) : 0);
3140 if (RT_UNLIKELY(pTaskErr))
3141 {
3142 sdbFis[0] = pTaskErr->cmdFis[AHCI_CMDFIS_ERR];
3143 sdbFis[0] |= (pTaskErr->cmdFis[AHCI_CMDFIS_STS] & 0x77) << 16; /* Some bits are marked as reserved and thus are masked out. */
3144
3145 /* Update registers. */
3146 pAhciPort->regTFD = (pTaskErr->cmdFis[AHCI_CMDFIS_ERR] << 8) | pTaskErr->cmdFis[AHCI_CMDFIS_STS];
3147 }
3148 else
3149 {
3150 sdbFis[0] = 0;
3151 sdbFis[0] |= (ATA_STAT_READY | ATA_STAT_SEEK) << 16;
3152 pAhciPort->regTFD = ATA_STAT_READY | ATA_STAT_SEEK;
3153 }
3154
3155 sdbFis[1] = pAhciPort->u32QueuedTasksFinished | uFinishedTasks;
3156
3157 ahciPostFisIntoMemory(pDevIns, pAhciPort, AHCI_CMDFIS_TYPE_SETDEVBITS, (uint8_t *)sdbFis);
3158
3159 if (RT_UNLIKELY(pTaskErr))
3160 {
3161 /* Error bit is set. */
3162 ASMAtomicOrU32(&pAhciPort->regIS, AHCI_PORT_IS_TFES);
3163 if (pAhciPort->regIE & AHCI_PORT_IE_TFEE)
3164 fAssertIntr = true;
3165 }
3166
3167 if (fInterrupt)
3168 {
3169 ASMAtomicOrU32(&pAhciPort->regIS, AHCI_PORT_IS_SDBS);
3170 /* Check if we should assert an interrupt */
3171 if (pAhciPort->regIE & AHCI_PORT_IE_SDBE)
3172 fAssertIntr = true;
3173 }
3174
3175 ASMAtomicOrU32(&pAhciPort->u32QueuedTasksFinished, uFinishedTasks);
3176
3177 if (fAssertIntr)
3178 {
3179 int rc = ahciHbaSetInterrupt(pDevIns, pThis, pAhciPort->iLUN, VERR_IGNORED);
3180 AssertRC(rc);
3181 }
3182 }
3183}
3184
3185static uint32_t ahciGetNSectors(uint8_t *pCmdFis, bool fLBA48)
3186{
3187 /* 0 means either 256 (LBA28) or 65536 (LBA48) sectors. */
3188 if (fLBA48)
3189 {
3190 if (!pCmdFis[AHCI_CMDFIS_SECTC] && !pCmdFis[AHCI_CMDFIS_SECTCEXP])
3191 return 65536;
3192 else
3193 return pCmdFis[AHCI_CMDFIS_SECTCEXP] << 8 | pCmdFis[AHCI_CMDFIS_SECTC];
3194 }
3195 else
3196 {
3197 if (!pCmdFis[AHCI_CMDFIS_SECTC])
3198 return 256;
3199 else
3200 return pCmdFis[AHCI_CMDFIS_SECTC];
3201 }
3202}
3203
3204static uint64_t ahciGetSector(PAHCIPORT pAhciPort, uint8_t *pCmdFis, bool fLBA48)
3205{
3206 uint64_t iLBA;
3207 if (pCmdFis[AHCI_CMDFIS_HEAD] & 0x40)
3208 {
3209 /* any LBA variant */
3210 if (fLBA48)
3211 {
3212 /* LBA48 */
3213 iLBA = ((uint64_t)pCmdFis[AHCI_CMDFIS_CYLHEXP] << 40) |
3214 ((uint64_t)pCmdFis[AHCI_CMDFIS_CYLLEXP] << 32) |
3215 ((uint64_t)pCmdFis[AHCI_CMDFIS_SECTNEXP] << 24) |
3216 ((uint64_t)pCmdFis[AHCI_CMDFIS_CYLH] << 16) |
3217 ((uint64_t)pCmdFis[AHCI_CMDFIS_CYLL] << 8) |
3218 pCmdFis[AHCI_CMDFIS_SECTN];
3219 }
3220 else
3221 {
3222 /* LBA */
3223 iLBA = ((pCmdFis[AHCI_CMDFIS_HEAD] & 0x0f) << 24) | (pCmdFis[AHCI_CMDFIS_CYLH] << 16) |
3224 (pCmdFis[AHCI_CMDFIS_CYLL] << 8) | pCmdFis[AHCI_CMDFIS_SECTN];
3225 }
3226 }
3227 else
3228 {
3229 /* CHS */
3230 iLBA = ((pCmdFis[AHCI_CMDFIS_CYLH] << 8) | pCmdFis[AHCI_CMDFIS_CYLL]) * pAhciPort->PCHSGeometry.cHeads * pAhciPort->PCHSGeometry.cSectors +
3231 (pCmdFis[AHCI_CMDFIS_HEAD] & 0x0f) * pAhciPort->PCHSGeometry.cSectors +
3232 (pCmdFis[AHCI_CMDFIS_SECTN] - 1);
3233 }
3234 return iLBA;
3235}
3236
3237static uint64_t ahciGetSectorQueued(uint8_t *pCmdFis)
3238{
3239 uint64_t uLBA;
3240
3241 uLBA = ((uint64_t)pCmdFis[AHCI_CMDFIS_CYLHEXP] << 40) |
3242 ((uint64_t)pCmdFis[AHCI_CMDFIS_CYLLEXP] << 32) |
3243 ((uint64_t)pCmdFis[AHCI_CMDFIS_SECTNEXP] << 24) |
3244 ((uint64_t)pCmdFis[AHCI_CMDFIS_CYLH] << 16) |
3245 ((uint64_t)pCmdFis[AHCI_CMDFIS_CYLL] << 8) |
3246 pCmdFis[AHCI_CMDFIS_SECTN];
3247
3248 return uLBA;
3249}
3250
3251DECLINLINE(uint32_t) ahciGetNSectorsQueued(uint8_t *pCmdFis)
3252{
3253 if (!pCmdFis[AHCI_CMDFIS_FETEXP] && !pCmdFis[AHCI_CMDFIS_FET])
3254 return 65536;
3255 else
3256 return pCmdFis[AHCI_CMDFIS_FETEXP] << 8 | pCmdFis[AHCI_CMDFIS_FET];
3257}
3258
3259/**
3260 * Copy from guest to host memory worker.
3261 *
3262 * @copydoc AHCIR3MEMCOPYCALLBACK
3263 */
3264static DECLCALLBACK(void) ahciR3CopyBufferFromGuestWorker(PPDMDEVINS pDevIns, RTGCPHYS GCPhys, PRTSGBUF pSgBuf,
3265 size_t cbCopy, size_t *pcbSkip)
3266{
3267 size_t cbSkipped = RT_MIN(cbCopy, *pcbSkip);
3268 cbCopy -= cbSkipped;
3269 GCPhys += cbSkipped;
3270 *pcbSkip -= cbSkipped;
3271
3272 while (cbCopy)
3273 {
3274 size_t cbSeg = cbCopy;
3275 void *pvSeg = RTSgBufGetNextSegment(pSgBuf, &cbSeg);
3276
3277 AssertPtr(pvSeg);
3278 PDMDevHlpPhysRead(pDevIns, GCPhys, pvSeg, cbSeg);
3279 GCPhys += cbSeg;
3280 cbCopy -= cbSeg;
3281 }
3282}
3283
3284/**
3285 * Copy from host to guest memory worker.
3286 *
3287 * @copydoc AHCIR3MEMCOPYCALLBACK
3288 */
3289static DECLCALLBACK(void) ahciR3CopyBufferToGuestWorker(PPDMDEVINS pDevIns, RTGCPHYS GCPhys, PRTSGBUF pSgBuf,
3290 size_t cbCopy, size_t *pcbSkip)
3291{
3292 size_t cbSkipped = RT_MIN(cbCopy, *pcbSkip);
3293 cbCopy -= cbSkipped;
3294 GCPhys += cbSkipped;
3295 *pcbSkip -= cbSkipped;
3296
3297 while (cbCopy)
3298 {
3299 size_t cbSeg = cbCopy;
3300 void *pvSeg = RTSgBufGetNextSegment(pSgBuf, &cbSeg);
3301
3302 AssertPtr(pvSeg);
3303 PDMDevHlpPCIPhysWrite(pDevIns, GCPhys, pvSeg, cbSeg);
3304 GCPhys += cbSeg;
3305 cbCopy -= cbSeg;
3306 }
3307}
3308
3309/**
3310 * Walks the PRDTL list copying data between the guest and host memory buffers.
3311 *
3312 * @returns Amount of bytes copied.
3313 * @param pDevIns The device instance.
3314 * @param pAhciReq AHCI request structure.
3315 * @param pfnCopyWorker The copy method to apply for each guest buffer.
3316 * @param pSgBuf The host S/G buffer.
3317 * @param cbSkip How many bytes to skip in advance before starting to copy.
3318 * @param cbCopy How many bytes to copy.
3319 */
3320static size_t ahciR3PrdtlWalk(PPDMDEVINS pDevIns, PAHCIREQ pAhciReq,
3321 PAHCIR3MEMCOPYCALLBACK pfnCopyWorker,
3322 PRTSGBUF pSgBuf, size_t cbSkip, size_t cbCopy)
3323{
3324 RTGCPHYS GCPhysPrdtl = pAhciReq->GCPhysPrdtl;
3325 unsigned cPrdtlEntries = pAhciReq->cPrdtlEntries;
3326 size_t cbCopied = 0;
3327
3328 /*
3329 * Add the amount to skip to the host buffer size to avoid a
3330 * few conditionals later on.
3331 */
3332 cbCopy += cbSkip;
3333
3334 AssertMsgReturn(cPrdtlEntries > 0, ("Copying 0 bytes is not possible\n"), 0);
3335
3336 do
3337 {
3338 SGLEntry aPrdtlEntries[32];
3339 uint32_t cPrdtlEntriesRead = cPrdtlEntries < RT_ELEMENTS(aPrdtlEntries)
3340 ? cPrdtlEntries
3341 : RT_ELEMENTS(aPrdtlEntries);
3342
3343 PDMDevHlpPhysRead(pDevIns, GCPhysPrdtl, &aPrdtlEntries[0],
3344 cPrdtlEntriesRead * sizeof(SGLEntry));
3345
3346 for (uint32_t i = 0; (i < cPrdtlEntriesRead) && cbCopy; i++)
3347 {
3348 RTGCPHYS GCPhysAddrDataBase = AHCI_RTGCPHYS_FROM_U32(aPrdtlEntries[i].u32DBAUp, aPrdtlEntries[i].u32DBA);
3349 uint32_t cbThisCopy = (aPrdtlEntries[i].u32DescInf & SGLENTRY_DESCINF_DBC) + 1;
3350
3351 cbThisCopy = (uint32_t)RT_MIN(cbThisCopy, cbCopy);
3352
3353 /* Copy into SG entry. */
3354 pfnCopyWorker(pDevIns, GCPhysAddrDataBase, pSgBuf, cbThisCopy, &cbSkip);
3355
3356 cbCopy -= cbThisCopy;
3357 cbCopied += cbThisCopy;
3358 }
3359
3360 GCPhysPrdtl += cPrdtlEntriesRead * sizeof(SGLEntry);
3361 cPrdtlEntries -= cPrdtlEntriesRead;
3362 } while (cPrdtlEntries && cbCopy);
3363
3364 if (cbCopied < cbCopy)
3365 pAhciReq->fFlags |= AHCI_REQ_OVERFLOW;
3366
3367 return cbCopied;
3368}
3369
3370/**
3371 * Copies a data buffer into the S/G buffer set up by the guest.
3372 *
3373 * @returns Amount of bytes copied to the PRDTL.
3374 * @param pDevIns The device instance.
3375 * @param pAhciReq AHCI request structure.
3376 * @param pSgBuf The S/G buffer to copy from.
3377 * @param cbSkip How many bytes to skip in advance before starting to copy.
3378 * @param cbCopy How many bytes to copy.
3379 */
3380static size_t ahciR3CopySgBufToPrdtl(PPDMDEVINS pDevIns, PAHCIREQ pAhciReq, PRTSGBUF pSgBuf, size_t cbSkip, size_t cbCopy)
3381{
3382 return ahciR3PrdtlWalk(pDevIns, pAhciReq, ahciR3CopyBufferToGuestWorker, pSgBuf, cbSkip, cbCopy);
3383}
3384
3385/**
3386 * Copies the S/G buffer into a data buffer.
3387 *
3388 * @returns Amount of bytes copied from the PRDTL.
3389 * @param pDevIns The device instance.
3390 * @param pAhciReq AHCI request structure.
3391 * @param pSgBuf The S/G buffer to copy into.
3392 * @param cbSkip How many bytes to skip in advance before starting to copy.
3393 * @param cbCopy How many bytes to copy.
3394 */
3395static size_t ahciR3CopySgBufFromPrdtl(PPDMDEVINS pDevIns, PAHCIREQ pAhciReq, PRTSGBUF pSgBuf, size_t cbSkip, size_t cbCopy)
3396{
3397 return ahciR3PrdtlWalk(pDevIns, pAhciReq, ahciR3CopyBufferFromGuestWorker, pSgBuf, cbSkip, cbCopy);
3398}
3399
3400/**
3401 * Copy a simple memory buffer to the guest memory buffer.
3402 *
3403 * @returns Amount of bytes copied from the PRDTL.
3404 * @param pDevIns The device instance.
3405 * @param pAhciReq AHCI request structure.
3406 * @param pvSrc The buffer to copy from.
3407 * @param cbSrc How many bytes to copy.
3408 * @param cbSkip How many bytes to skip initially.
3409 */
3410static size_t ahciR3CopyBufferToPrdtl(PPDMDEVINS pDevIns, PAHCIREQ pAhciReq, const void *pvSrc, size_t cbSrc, size_t cbSkip)
3411{
3412 RTSGSEG Seg;
3413 RTSGBUF SgBuf;
3414 Seg.pvSeg = (void *)pvSrc;
3415 Seg.cbSeg = cbSrc;
3416 RTSgBufInit(&SgBuf, &Seg, 1);
3417 return ahciR3CopySgBufToPrdtl(pDevIns, pAhciReq, &SgBuf, cbSkip, cbSrc);
3418}
3419
3420/**
3421 * Calculates the size of the guest buffer described by the PRDT.
3422 *
3423 * @returns VBox status code.
3424 * @param pDevIns The device instance.
3425 * @param pThis The AHCI controller device instance.
3426 * @param pAhciReq AHCI request structure.
3427 * @param pcbPrdt Where to store the size of the guest buffer.
3428 */
3429static int ahciR3PrdtQuerySize(PPDMDEVINS pDevIns, PAHCIREQ pAhciReq, size_t *pcbPrdt)
3430{
3431 RTGCPHYS GCPhysPrdtl = pAhciReq->GCPhysPrdtl;
3432 unsigned cPrdtlEntries = pAhciReq->cPrdtlEntries;
3433 size_t cbPrdt = 0;
3434
3435 do
3436 {
3437 SGLEntry aPrdtlEntries[32];
3438 uint32_t const cPrdtlEntriesRead = RT_MIN(cPrdtlEntries, RT_ELEMENTS(aPrdtlEntries));
3439
3440 PDMDevHlpPhysRead(pDevIns, GCPhysPrdtl, &aPrdtlEntries[0], cPrdtlEntriesRead * sizeof(SGLEntry));
3441
3442 for (uint32_t i = 0; i < cPrdtlEntriesRead; i++)
3443 cbPrdt += (aPrdtlEntries[i].u32DescInf & SGLENTRY_DESCINF_DBC) + 1;
3444
3445 GCPhysPrdtl += cPrdtlEntriesRead * sizeof(SGLEntry);
3446 cPrdtlEntries -= cPrdtlEntriesRead;
3447 } while (cPrdtlEntries);
3448
3449 *pcbPrdt = cbPrdt;
3450 return VINF_SUCCESS;
3451}
3452
3453/**
3454 * Cancels all active tasks on the port.
3455 *
3456 * @returns Whether all active tasks were canceled.
3457 * @param pAhciPortR3 The AHCI port, ring-3 bits.
3458 */
3459static bool ahciR3CancelActiveTasks(PAHCIPORTR3 pAhciPortR3)
3460{
3461 if (pAhciPortR3->pDrvMediaEx)
3462 {
3463 int rc = pAhciPortR3->pDrvMediaEx->pfnIoReqCancelAll(pAhciPortR3->pDrvMediaEx);
3464 AssertRC(rc);
3465 }
3466 return true; /* always true for now because tasks don't use guest memory as the buffer which makes canceling a task impossible. */
3467}
3468
3469/**
3470 * Creates the array of ranges to trim.
3471 *
3472 * @returns VBox status code.
3473 * @param pDevIns The device instance.
3474 * @param pAhciPort AHCI port state.
3475 * @param pAhciReq The request handling the TRIM request.
3476 * @param idxRangeStart Index of the first range to start copying.
3477 * @param paRanges Where to store the ranges.
3478 * @param cRanges Number of ranges fitting into the array.
3479 * @param pcRanges Where to store the amount of ranges actually copied on success.
3480 */
3481static int ahciTrimRangesCreate(PPDMDEVINS pDevIns, PAHCIPORT pAhciPort, PAHCIREQ pAhciReq, uint32_t idxRangeStart,
3482 PRTRANGE paRanges, uint32_t cRanges, uint32_t *pcRanges)
3483{
3484 SGLEntry aPrdtlEntries[32];
3485 uint64_t aRanges[64];
3486 uint32_t cPrdtlEntries = pAhciReq->cPrdtlEntries;
3487 RTGCPHYS GCPhysPrdtl = pAhciReq->GCPhysPrdtl;
3488 int rc = VERR_PDM_MEDIAEX_IOBUF_OVERFLOW;
3489 uint32_t idxRange = 0;
3490
3491 LogFlowFunc(("pAhciPort=%#p pAhciReq=%#p\n", pAhciPort, pAhciReq));
3492
3493 AssertMsgReturn(pAhciReq->enmType == PDMMEDIAEXIOREQTYPE_DISCARD, ("This is not a trim request\n"), VERR_INVALID_PARAMETER);
3494
3495 if (!cPrdtlEntries)
3496 pAhciReq->fFlags |= AHCI_REQ_OVERFLOW;
3497
3498 /* Convert the ranges from ATA to our format. */
3499 while ( cPrdtlEntries
3500 && idxRange < cRanges)
3501 {
3502 uint32_t cPrdtlEntriesRead = RT_MIN(cPrdtlEntries, RT_ELEMENTS(aPrdtlEntries));
3503
3504 rc = VINF_SUCCESS;
3505 PDMDevHlpPhysRead(pDevIns, GCPhysPrdtl, &aPrdtlEntries[0], cPrdtlEntriesRead * sizeof(SGLEntry));
3506
3507 for (uint32_t i = 0; i < cPrdtlEntriesRead && idxRange < cRanges; i++)
3508 {
3509 RTGCPHYS GCPhysAddrDataBase = AHCI_RTGCPHYS_FROM_U32(aPrdtlEntries[i].u32DBAUp, aPrdtlEntries[i].u32DBA);
3510 uint32_t cbThisCopy = (aPrdtlEntries[i].u32DescInf & SGLENTRY_DESCINF_DBC) + 1;
3511
3512 cbThisCopy = RT_MIN(cbThisCopy, sizeof(aRanges));
3513
3514 /* Copy into buffer. */
3515 PDMDevHlpPhysRead(pDevIns, GCPhysAddrDataBase, aRanges, cbThisCopy);
3516
3517 for (unsigned idxRangeSrc = 0; idxRangeSrc < RT_ELEMENTS(aRanges) && idxRange < cRanges; idxRangeSrc++)
3518 {
3519 /* Skip range if told to do so. */
3520 if (!idxRangeStart)
3521 {
3522 aRanges[idxRangeSrc] = RT_H2LE_U64(aRanges[idxRangeSrc]);
3523 if (AHCI_RANGE_LENGTH_GET(aRanges[idxRangeSrc]) != 0)
3524 {
3525 paRanges[idxRange].offStart = (aRanges[idxRangeSrc] & AHCI_RANGE_LBA_MASK) * pAhciPort->cbSector;
3526 paRanges[idxRange].cbRange = AHCI_RANGE_LENGTH_GET(aRanges[idxRangeSrc]) * pAhciPort->cbSector;
3527 idxRange++;
3528 }
3529 else
3530 break;
3531 }
3532 else
3533 idxRangeStart--;
3534 }
3535 }
3536
3537 GCPhysPrdtl += cPrdtlEntriesRead * sizeof(SGLEntry);
3538 cPrdtlEntries -= cPrdtlEntriesRead;
3539 }
3540
3541 *pcRanges = idxRange;
3542
3543 LogFlowFunc(("returns rc=%Rrc\n", rc));
3544 return rc;
3545}
3546
3547/**
3548 * Allocates a new AHCI request.
3549 *
3550 * @returns A new AHCI request structure or NULL if out of memory.
3551 * @param pAhciPortR3 The AHCI port, ring-3 bits.
3552 * @param uTag The tag to assign.
3553 */
3554static PAHCIREQ ahciR3ReqAlloc(PAHCIPORTR3 pAhciPortR3, uint32_t uTag)
3555{
3556 PAHCIREQ pAhciReq = NULL;
3557 PDMMEDIAEXIOREQ hIoReq = NULL;
3558
3559 int rc = pAhciPortR3->pDrvMediaEx->pfnIoReqAlloc(pAhciPortR3->pDrvMediaEx, &hIoReq, (void **)&pAhciReq,
3560 uTag, PDMIMEDIAEX_F_SUSPEND_ON_RECOVERABLE_ERR);
3561 if (RT_SUCCESS(rc))
3562 {
3563 pAhciReq->hIoReq = hIoReq;
3564 pAhciReq->fMapped = false;
3565 }
3566 else
3567 pAhciReq = NULL;
3568 return pAhciReq;
3569}
3570
3571/**
3572 * Frees a given AHCI request structure.
3573 *
3574 * @returns nothing.
3575 * @param pAhciPort The AHCI port, shared bits.
3576 * @param pAhciPortR3 The AHCI port, ring-3 bits.
3577 * @param pAhciReq The request to free.
3578 */
3579static void ahciR3ReqFree(PAHCIPORTR3 pAhciPortR3, PAHCIREQ pAhciReq)
3580{
3581 if ( pAhciReq
3582 && !(pAhciReq->fFlags & AHCI_REQ_IS_ON_STACK))
3583 {
3584 int rc = pAhciPortR3->pDrvMediaEx->pfnIoReqFree(pAhciPortR3->pDrvMediaEx, pAhciReq->hIoReq);
3585 AssertRC(rc);
3586 }
3587}
3588
3589/**
3590 * Complete a data transfer task by freeing all occupied resources
3591 * and notifying the guest.
3592 *
3593 * @returns Flag whether the given request was canceled inbetween;
3594 *
3595 * @param pDevIns The device instance.
3596 * @param pThis The shared AHCI state.
3597 * @param pThisCC The ring-3 AHCI state.
3598 * @param pAhciPort Pointer to the port where to request completed, shared bits.
3599 * @param pAhciPortR3 Pointer to the port where to request completed, ring-3 bits.
3600 * @param pAhciReq Pointer to the task which finished.
3601 * @param rcReq IPRT status code of the completed request.
3602 */
3603static bool ahciR3TransferComplete(PPDMDEVINS pDevIns, PAHCI pThis, PAHCICC pThisCC,
3604 PAHCIPORT pAhciPort, PAHCIPORTR3 pAhciPortR3, PAHCIREQ pAhciReq, int rcReq)
3605{
3606 bool fCanceled = false;
3607
3608 LogFlowFunc(("pAhciPort=%p pAhciReq=%p rcReq=%d\n",
3609 pAhciPort, pAhciReq, rcReq));
3610
3611 VBOXDD_AHCI_REQ_COMPLETED(pAhciReq, rcReq, pAhciReq->uOffset, pAhciReq->cbTransfer);
3612
3613 if (pAhciReq->fMapped)
3614 PDMDevHlpPhysReleasePageMappingLock(pDevIns, &pAhciReq->PgLck);
3615
3616 if (rcReq != VERR_PDM_MEDIAEX_IOREQ_CANCELED)
3617 {
3618 if (pAhciReq->enmType == PDMMEDIAEXIOREQTYPE_READ)
3619 pAhciPort->Led.Actual.s.fReading = 0;
3620 else if (pAhciReq->enmType == PDMMEDIAEXIOREQTYPE_WRITE)
3621 pAhciPort->Led.Actual.s.fWriting = 0;
3622 else if (pAhciReq->enmType == PDMMEDIAEXIOREQTYPE_DISCARD)
3623 pAhciPort->Led.Actual.s.fWriting = 0;
3624 else if (pAhciReq->enmType == PDMMEDIAEXIOREQTYPE_SCSI)
3625 {
3626 pAhciPort->Led.Actual.s.fWriting = 0;
3627 pAhciPort->Led.Actual.s.fReading = 0;
3628 }
3629
3630 if (RT_FAILURE(rcReq))
3631 {
3632 /* Log the error. */
3633 if (pAhciPort->cErrors++ < MAX_LOG_REL_ERRORS)
3634 {
3635 if (pAhciReq->enmType == PDMMEDIAEXIOREQTYPE_FLUSH)
3636 LogRel(("AHCI#%uP%u: Flush returned rc=%Rrc\n",
3637 pDevIns->iInstance, pAhciPort->iLUN, rcReq));
3638 else if (pAhciReq->enmType == PDMMEDIAEXIOREQTYPE_DISCARD)
3639 LogRel(("AHCI#%uP%u: Trim returned rc=%Rrc\n",
3640 pDevIns->iInstance, pAhciPort->iLUN, rcReq));
3641 else
3642 LogRel(("AHCI#%uP%u: %s at offset %llu (%zu bytes left) returned rc=%Rrc\n",
3643 pDevIns->iInstance, pAhciPort->iLUN,
3644 pAhciReq->enmType == PDMMEDIAEXIOREQTYPE_READ
3645 ? "Read"
3646 : "Write",
3647 pAhciReq->uOffset,
3648 pAhciReq->cbTransfer, rcReq));
3649 }
3650
3651 ahciReqSetStatus(pAhciReq, ID_ERR, ATA_STAT_READY | ATA_STAT_ERR);
3652 /*
3653 * We have to duplicate the request here as the underlying I/O
3654 * request will be freed later.
3655 */
3656 PAHCIREQ pReqDup = (PAHCIREQ)RTMemDup(pAhciReq, sizeof(AHCIREQ));
3657 if ( pReqDup
3658 && !ASMAtomicCmpXchgPtr(&pAhciPortR3->pTaskErr, pReqDup, NULL))
3659 RTMemFree(pReqDup);
3660 }
3661 else
3662 {
3663 /* Status will be set already for non I/O requests. */
3664 if (pAhciReq->enmType == PDMMEDIAEXIOREQTYPE_SCSI)
3665 {
3666 if (pAhciReq->u8ScsiSts == SCSI_STATUS_OK)
3667 {
3668 ahciReqSetStatus(pAhciReq, 0, ATA_STAT_READY | ATA_STAT_SEEK);
3669 pAhciReq->cmdFis[AHCI_CMDFIS_SECTN] = (pAhciReq->cmdFis[AHCI_CMDFIS_SECTN] & ~7)
3670 | ((pAhciReq->fFlags & AHCI_REQ_XFER_2_HOST) ? ATAPI_INT_REASON_IO : 0)
3671 | (!pAhciReq->cbTransfer ? ATAPI_INT_REASON_CD : 0);
3672 }
3673 else
3674 {
3675 ahciReqSetStatus(pAhciReq, pAhciPort->abATAPISense[2] << 4, ATA_STAT_READY | ATA_STAT_ERR);
3676 pAhciReq->cmdFis[AHCI_CMDFIS_SECTN] = (pAhciReq->cmdFis[AHCI_CMDFIS_SECTN] & ~7) |
3677 ATAPI_INT_REASON_IO | ATAPI_INT_REASON_CD;
3678 pAhciReq->cbTransfer = 0;
3679 LogFlowFunc(("SCSI request completed with %u status\n", pAhciReq->u8ScsiSts));
3680 }
3681 }
3682 else if (pAhciReq->enmType != PDMMEDIAEXIOREQTYPE_INVALID)
3683 ahciReqSetStatus(pAhciReq, 0, ATA_STAT_READY | ATA_STAT_SEEK);
3684
3685 /* Write updated command header into memory of the guest. */
3686 uint32_t u32PRDBC = 0;
3687 if (pAhciReq->enmType != PDMMEDIAEXIOREQTYPE_INVALID)
3688 {
3689 size_t cbXfer = 0;
3690 int rc = pAhciPortR3->pDrvMediaEx->pfnIoReqQueryXferSize(pAhciPortR3->pDrvMediaEx, pAhciReq->hIoReq, &cbXfer);
3691 AssertRC(rc);
3692 u32PRDBC = (uint32_t)RT_MIN(cbXfer, pAhciReq->cbTransfer);
3693 }
3694 else
3695 u32PRDBC = (uint32_t)pAhciReq->cbTransfer;
3696
3697 PDMDevHlpPCIPhysWrite(pDevIns, pAhciReq->GCPhysCmdHdrAddr + RT_UOFFSETOF(CmdHdr, u32PRDBC),
3698 &u32PRDBC, sizeof(u32PRDBC));
3699
3700 if (pAhciReq->fFlags & AHCI_REQ_OVERFLOW)
3701 {
3702 /*
3703 * The guest tried to transfer more data than there is space in the buffer.
3704 * Terminate task and set the overflow bit.
3705 */
3706 /* Notify the guest. */
3707 ASMAtomicOrU32(&pAhciPort->regIS, AHCI_PORT_IS_OFS);
3708 if (pAhciPort->regIE & AHCI_PORT_IE_OFE)
3709 ahciHbaSetInterrupt(pDevIns, pThis, pAhciPort->iLUN, VERR_IGNORED);
3710 }
3711 }
3712
3713 /*
3714 * Make a copy of the required data now and free the request. Otherwise the guest
3715 * might issue a new request with the same tag and we run into a conflict when allocating
3716 * a new request with the same tag later on.
3717 */
3718 uint32_t fFlags = pAhciReq->fFlags;
3719 uint32_t uTag = pAhciReq->uTag;
3720 size_t cbTransfer = pAhciReq->cbTransfer;
3721 bool fRead = pAhciReq->enmType == PDMMEDIAEXIOREQTYPE_READ;
3722 uint8_t cmdFis[AHCI_CMDFIS_TYPE_H2D_SIZE];
3723 memcpy(&cmdFis[0], &pAhciReq->cmdFis[0], sizeof(cmdFis));
3724
3725 ahciR3ReqFree(pAhciPortR3, pAhciReq);
3726
3727 /* Post a PIO setup FIS first if this is a PIO command which transfers data. */
3728 if (fFlags & AHCI_REQ_PIO_DATA)
3729 ahciSendPioSetupFis(pDevIns, pThis, pAhciPort, cbTransfer, &cmdFis[0], fRead, false /* fInterrupt */);
3730
3731 if (fFlags & AHCI_REQ_CLEAR_SACT)
3732 {
3733 if (RT_SUCCESS(rcReq) && !ASMAtomicReadPtrT(&pAhciPortR3->pTaskErr, PAHCIREQ))
3734 ASMAtomicOrU32(&pAhciPort->u32QueuedTasksFinished, RT_BIT_32(uTag));
3735 }
3736
3737 if (fFlags & AHCI_REQ_IS_QUEUED)
3738 {
3739 /*
3740 * Always raise an interrupt after task completion; delaying
3741 * this (interrupt coalescing) increases latency and has a significant
3742 * impact on performance (see @bugref{5071})
3743 */
3744 ahciSendSDBFis(pDevIns, pThis, pAhciPort, pAhciPortR3, 0, true);
3745 }
3746 else
3747 ahciSendD2HFis(pDevIns, pThis, pAhciPort, uTag, &cmdFis[0], true);
3748 }
3749 else
3750 {
3751 /*
3752 * Task was canceled, do the cleanup but DO NOT access the guest memory!
3753 * The guest might use it for other things now because it doesn't know about that task anymore.
3754 */
3755 fCanceled = true;
3756
3757 /* Leave a log message about the canceled request. */
3758 if (pAhciPort->cErrors++ < MAX_LOG_REL_ERRORS)
3759 {
3760 if (pAhciReq->enmType == PDMMEDIAEXIOREQTYPE_FLUSH)
3761 LogRel(("AHCI#%uP%u: Canceled flush returned rc=%Rrc\n",
3762 pDevIns->iInstance, pAhciPort->iLUN, rcReq));
3763 else if (pAhciReq->enmType == PDMMEDIAEXIOREQTYPE_DISCARD)
3764 LogRel(("AHCI#%uP%u: Canceled trim returned rc=%Rrc\n",
3765 pDevIns->iInstance,pAhciPort->iLUN, rcReq));
3766 else
3767 LogRel(("AHCI#%uP%u: Canceled %s at offset %llu (%zu bytes left) returned rc=%Rrc\n",
3768 pDevIns->iInstance, pAhciPort->iLUN,
3769 pAhciReq->enmType == PDMMEDIAEXIOREQTYPE_READ
3770 ? "read"
3771 : "write",
3772 pAhciReq->uOffset,
3773 pAhciReq->cbTransfer, rcReq));
3774 }
3775
3776 ahciR3ReqFree(pAhciPortR3, pAhciReq);
3777 }
3778
3779 /*
3780 * Decrement the active task counter as the last step or we might run into a
3781 * hang during power off otherwise (see @bugref{7859}).
3782 * Before it could happen that we signal PDM that we are done while we still have to
3783 * copy the data to the guest but EMT might be busy destroying the driver chains
3784 * below us while we have to delegate copying data to EMT instead of doing it
3785 * on this thread.
3786 */
3787 ASMAtomicDecU32(&pAhciPort->cTasksActive);
3788
3789 if (pAhciPort->cTasksActive == 0 && pThisCC->fSignalIdle)
3790 PDMDevHlpAsyncNotificationCompleted(pDevIns);
3791
3792 return fCanceled;
3793}
3794
3795/**
3796 * @interface_method_impl{PDMIMEDIAEXPORT,pfnIoReqCopyFromBuf}
3797 */
3798static DECLCALLBACK(int) ahciR3IoReqCopyFromBuf(PPDMIMEDIAEXPORT pInterface, PDMMEDIAEXIOREQ hIoReq,
3799 void *pvIoReqAlloc, uint32_t offDst, PRTSGBUF pSgBuf,
3800 size_t cbCopy)
3801{
3802 PAHCIPORTR3 pAhciPortR3 = RT_FROM_MEMBER(pInterface, AHCIPORTR3, IMediaExPort);
3803 int rc = VINF_SUCCESS;
3804 PAHCIREQ pIoReq = (PAHCIREQ)pvIoReqAlloc;
3805 RT_NOREF(hIoReq);
3806
3807 ahciR3CopySgBufToPrdtl(pAhciPortR3->pDevIns, pIoReq, pSgBuf, offDst, cbCopy);
3808
3809 if (pIoReq->fFlags & AHCI_REQ_OVERFLOW)
3810 rc = VERR_PDM_MEDIAEX_IOBUF_OVERFLOW;
3811
3812 return rc;
3813}
3814
3815/**
3816 * @interface_method_impl{PDMIMEDIAEXPORT,pfnIoReqCopyToBuf}
3817 */
3818static DECLCALLBACK(int) ahciR3IoReqCopyToBuf(PPDMIMEDIAEXPORT pInterface, PDMMEDIAEXIOREQ hIoReq,
3819 void *pvIoReqAlloc, uint32_t offSrc, PRTSGBUF pSgBuf,
3820 size_t cbCopy)
3821{
3822 PAHCIPORTR3 pAhciPortR3 = RT_FROM_MEMBER(pInterface, AHCIPORTR3, IMediaExPort);
3823 int rc = VINF_SUCCESS;
3824 PAHCIREQ pIoReq = (PAHCIREQ)pvIoReqAlloc;
3825 RT_NOREF(hIoReq);
3826
3827 ahciR3CopySgBufFromPrdtl(pAhciPortR3->pDevIns, pIoReq, pSgBuf, offSrc, cbCopy);
3828 if (pIoReq->fFlags & AHCI_REQ_OVERFLOW)
3829 rc = VERR_PDM_MEDIAEX_IOBUF_UNDERRUN;
3830
3831 return rc;
3832}
3833
3834/**
3835 * @interface_method_impl{PDMIMEDIAEXPORT,pfnIoReqQueryBuf}
3836 */
3837static DECLCALLBACK(int) ahciR3IoReqQueryBuf(PPDMIMEDIAEXPORT pInterface, PDMMEDIAEXIOREQ hIoReq,
3838 void *pvIoReqAlloc, void **ppvBuf, size_t *pcbBuf)
3839{
3840 PAHCIPORTR3 pAhciPortR3 = RT_FROM_MEMBER(pInterface, AHCIPORTR3, IMediaExPort);
3841 PPDMDEVINS pDevIns = pAhciPortR3->pDevIns;
3842 PAHCIREQ pIoReq = (PAHCIREQ)pvIoReqAlloc;
3843 int rc = VERR_NOT_SUPPORTED;
3844 RT_NOREF(hIoReq);
3845
3846 /* Only allow single 4KB page aligned buffers at the moment. */
3847 if ( pIoReq->cPrdtlEntries == 1
3848 && pIoReq->cbTransfer == _4K)
3849 {
3850 RTGCPHYS GCPhysPrdt = pIoReq->GCPhysPrdtl;
3851 SGLEntry PrdtEntry;
3852
3853 PDMDevHlpPhysRead(pDevIns, GCPhysPrdt, &PrdtEntry, sizeof(SGLEntry));
3854
3855 RTGCPHYS GCPhysAddrDataBase = AHCI_RTGCPHYS_FROM_U32(PrdtEntry.u32DBAUp, PrdtEntry.u32DBA);
3856 uint32_t cbData = (PrdtEntry.u32DescInf & SGLENTRY_DESCINF_DBC) + 1;
3857
3858 if ( cbData >= _4K
3859 && !(GCPhysAddrDataBase & (_4K - 1)))
3860 {
3861 rc = PDMDevHlpPhysGCPhys2CCPtr(pDevIns, GCPhysAddrDataBase, 0, ppvBuf, &pIoReq->PgLck);
3862 if (RT_SUCCESS(rc))
3863 {
3864 pIoReq->fMapped = true;
3865 *pcbBuf = cbData;
3866 }
3867 else
3868 rc = VERR_NOT_SUPPORTED;
3869 }
3870 }
3871
3872 return rc;
3873}
3874
3875/**
3876 * @interface_method_impl{PDMIMEDIAEXPORT,pfnIoReqQueryDiscardRanges}
3877 */
3878static DECLCALLBACK(int) ahciR3IoReqQueryDiscardRanges(PPDMIMEDIAEXPORT pInterface, PDMMEDIAEXIOREQ hIoReq,
3879 void *pvIoReqAlloc, uint32_t idxRangeStart,
3880 uint32_t cRanges, PRTRANGE paRanges,
3881 uint32_t *pcRanges)
3882{
3883 PAHCIPORTR3 pAhciPortR3 = RT_FROM_MEMBER(pInterface, AHCIPORTR3, IMediaExPort);
3884 PPDMDEVINS pDevIns = pAhciPortR3->pDevIns;
3885 PAHCI pThis = PDMDEVINS_2_DATA(pDevIns, PAHCI);
3886 PAHCIPORT pAhciPort = &RT_SAFE_SUBSCRIPT(pThis->aPorts, pAhciPortR3->iLUN);
3887 PAHCIREQ pIoReq = (PAHCIREQ)pvIoReqAlloc;
3888 RT_NOREF(hIoReq);
3889
3890 return ahciTrimRangesCreate(pDevIns, pAhciPort, pIoReq, idxRangeStart, paRanges, cRanges, pcRanges);
3891}
3892
3893/**
3894 * @interface_method_impl{PDMIMEDIAEXPORT,pfnIoReqCompleteNotify}
3895 */
3896static DECLCALLBACK(int) ahciR3IoReqCompleteNotify(PPDMIMEDIAEXPORT pInterface, PDMMEDIAEXIOREQ hIoReq,
3897 void *pvIoReqAlloc, int rcReq)
3898{
3899 PAHCIPORTR3 pAhciPortR3 = RT_FROM_MEMBER(pInterface, AHCIPORTR3, IMediaExPort);
3900 PPDMDEVINS pDevIns = pAhciPortR3->pDevIns;
3901 PAHCIR3 pThisCC = PDMDEVINS_2_DATA_CC(pDevIns, PAHCICC);
3902 PAHCI pThis = PDMDEVINS_2_DATA(pDevIns, PAHCI);
3903 PAHCIPORT pAhciPort = &RT_SAFE_SUBSCRIPT(pThis->aPorts, pAhciPortR3->iLUN);
3904 PAHCIREQ pIoReq = (PAHCIREQ)pvIoReqAlloc;
3905 RT_NOREF(hIoReq);
3906
3907 ahciR3TransferComplete(pDevIns, pThis, pThisCC, pAhciPort, pAhciPortR3, pIoReq, rcReq);
3908 return VINF_SUCCESS;
3909}
3910
3911/**
3912 * @interface_method_impl{PDMIMEDIAEXPORT,pfnIoReqStateChanged}
3913 */
3914static DECLCALLBACK(void) ahciR3IoReqStateChanged(PPDMIMEDIAEXPORT pInterface, PDMMEDIAEXIOREQ hIoReq,
3915 void *pvIoReqAlloc, PDMMEDIAEXIOREQSTATE enmState)
3916{
3917 PAHCIPORTR3 pAhciPortR3 = RT_FROM_MEMBER(pInterface, AHCIPORTR3, IMediaExPort);
3918 PPDMDEVINS pDevIns = pAhciPortR3->pDevIns;
3919 PAHCIR3 pThisCC = PDMDEVINS_2_DATA_CC(pDevIns, PAHCICC);
3920 PAHCI pThis = PDMDEVINS_2_DATA(pDevIns, PAHCI);
3921 PAHCIPORT pAhciPort = &RT_SAFE_SUBSCRIPT(pThis->aPorts, pAhciPortR3->iLUN);
3922 RT_NOREF(hIoReq, pvIoReqAlloc);
3923
3924 switch (enmState)
3925 {
3926 case PDMMEDIAEXIOREQSTATE_SUSPENDED:
3927 {
3928 /* Make sure the request is not accounted for so the VM can suspend successfully. */
3929 uint32_t cTasksActive = ASMAtomicDecU32(&pAhciPort->cTasksActive);
3930 if (!cTasksActive && pThisCC->fSignalIdle)
3931 PDMDevHlpAsyncNotificationCompleted(pDevIns);
3932 break;
3933 }
3934 case PDMMEDIAEXIOREQSTATE_ACTIVE:
3935 /* Make sure the request is accounted for so the VM suspends only when the request is complete. */
3936 ASMAtomicIncU32(&pAhciPort->cTasksActive);
3937 break;
3938 default:
3939 AssertMsgFailed(("Invalid request state given %u\n", enmState));
3940 }
3941}
3942
3943/**
3944 * @interface_method_impl{PDMIMEDIAEXPORT,pfnMediumEjected}
3945 */
3946static DECLCALLBACK(void) ahciR3MediumEjected(PPDMIMEDIAEXPORT pInterface)
3947{
3948 PAHCIPORTR3 pAhciPortR3 = RT_FROM_MEMBER(pInterface, AHCIPORTR3, IMediaExPort);
3949 PPDMDEVINS pDevIns = pAhciPortR3->pDevIns;
3950 PAHCIR3 pThisCC = PDMDEVINS_2_DATA_CC(pDevIns, PAHCICC);
3951 PAHCI pThis = PDMDEVINS_2_DATA(pDevIns, PAHCI);
3952 PAHCIPORT pAhciPort = &RT_SAFE_SUBSCRIPT(pThis->aPorts, pAhciPortR3->iLUN);
3953
3954 if (pThisCC->pMediaNotify)
3955 {
3956 int rc = VMR3ReqCallNoWait(PDMDevHlpGetVM(pDevIns), VMCPUID_ANY,
3957 (PFNRT)pThisCC->pMediaNotify->pfnEjected, 2,
3958 pThisCC->pMediaNotify, pAhciPort->iLUN);
3959 AssertRC(rc);
3960 }
3961}
3962
3963/**
3964 * Process an non read/write ATA command.
3965 *
3966 * @returns The direction of the data transfer
3967 * @param pDevIns The device instance.
3968 * @param pThis The shared AHCI state.
3969 * @param pAhciPort The AHCI port of the request, shared bits.
3970 * @param pAhciPortR3 The AHCI port of the request, ring-3 bits.
3971 * @param pAhciReq The AHCI request state.
3972 * @param pCmdFis Pointer to the command FIS.
3973 */
3974static PDMMEDIAEXIOREQTYPE ahciProcessCmd(PPDMDEVINS pDevIns, PAHCI pThis, PAHCIPORT pAhciPort, PAHCIPORTR3 pAhciPortR3,
3975 PAHCIREQ pAhciReq, uint8_t *pCmdFis)
3976{
3977 PDMMEDIAEXIOREQTYPE enmType = PDMMEDIAEXIOREQTYPE_INVALID;
3978 bool fLBA48 = false;
3979
3980 AssertMsg(pCmdFis[AHCI_CMDFIS_TYPE] == AHCI_CMDFIS_TYPE_H2D, ("FIS is not a host to device Fis!!\n"));
3981
3982 pAhciReq->cbTransfer = 0;
3983
3984 switch (pCmdFis[AHCI_CMDFIS_CMD])
3985 {
3986 case ATA_IDENTIFY_DEVICE:
3987 {
3988 if (pAhciPortR3->pDrvMedia && !pAhciPort->fATAPI)
3989 {
3990 uint16_t u16Temp[256];
3991
3992 /* Fill the buffer. */
3993 ahciIdentifySS(pThis, pAhciPort, pAhciPortR3, u16Temp);
3994
3995 /* Copy the buffer. */
3996 size_t cbCopied = ahciR3CopyBufferToPrdtl(pDevIns, pAhciReq, &u16Temp[0], sizeof(u16Temp), 0 /* cbSkip */);
3997
3998 pAhciReq->fFlags |= AHCI_REQ_PIO_DATA;
3999 pAhciReq->cbTransfer = cbCopied;
4000 ahciReqSetStatus(pAhciReq, 0, ATA_STAT_READY | ATA_STAT_SEEK);
4001 }
4002 else
4003 ahciReqSetStatus(pAhciReq, ABRT_ERR, ATA_STAT_READY | ATA_STAT_SEEK | ATA_STAT_ERR);
4004 break;
4005 }
4006 case ATA_READ_NATIVE_MAX_ADDRESS_EXT:
4007 case ATA_READ_NATIVE_MAX_ADDRESS:
4008 break;
4009 case ATA_SET_FEATURES:
4010 {
4011 switch (pCmdFis[AHCI_CMDFIS_FET])
4012 {
4013 case 0x02: /* write cache enable */
4014 case 0xaa: /* read look-ahead enable */
4015 case 0x55: /* read look-ahead disable */
4016 case 0xcc: /* reverting to power-on defaults enable */
4017 case 0x66: /* reverting to power-on defaults disable */
4018 ahciReqSetStatus(pAhciReq, 0, ATA_STAT_READY | ATA_STAT_SEEK);
4019 break;
4020 case 0x82: /* write cache disable */
4021 enmType = PDMMEDIAEXIOREQTYPE_FLUSH;
4022 break;
4023 case 0x03:
4024 {
4025 /* set transfer mode */
4026 Log2(("%s: transfer mode %#04x\n", __FUNCTION__, pCmdFis[AHCI_CMDFIS_SECTC]));
4027 switch (pCmdFis[AHCI_CMDFIS_SECTC] & 0xf8)
4028 {
4029 case 0x00: /* PIO default */
4030 case 0x08: /* PIO mode */
4031 break;
4032 case ATA_MODE_MDMA: /* MDMA mode */
4033 pAhciPort->uATATransferMode = (pCmdFis[AHCI_CMDFIS_SECTC] & 0xf8) | RT_MIN(pCmdFis[AHCI_CMDFIS_SECTC] & 0x07, ATA_MDMA_MODE_MAX);
4034 break;
4035 case ATA_MODE_UDMA: /* UDMA mode */
4036 pAhciPort->uATATransferMode = (pCmdFis[AHCI_CMDFIS_SECTC] & 0xf8) | RT_MIN(pCmdFis[AHCI_CMDFIS_SECTC] & 0x07, ATA_UDMA_MODE_MAX);
4037 break;
4038 }
4039 ahciReqSetStatus(pAhciReq, 0, ATA_STAT_READY | ATA_STAT_SEEK);
4040 break;
4041 }
4042 default:
4043 ahciReqSetStatus(pAhciReq, ABRT_ERR, ATA_STAT_READY | ATA_STAT_ERR);
4044 }
4045 break;
4046 }
4047 case ATA_DEVICE_RESET:
4048 {
4049 if (!pAhciPort->fATAPI)
4050 ahciReqSetStatus(pAhciReq, ABRT_ERR, ATA_STAT_READY | ATA_STAT_ERR);
4051 else
4052 {
4053 /* Reset the device. */
4054 ahciDeviceReset(pDevIns, pThis, pAhciPort, pAhciReq);
4055 }
4056 break;
4057 }
4058 case ATA_FLUSH_CACHE_EXT:
4059 case ATA_FLUSH_CACHE:
4060 enmType = PDMMEDIAEXIOREQTYPE_FLUSH;
4061 break;
4062 case ATA_PACKET:
4063 if (!pAhciPort->fATAPI)
4064 ahciReqSetStatus(pAhciReq, ABRT_ERR, ATA_STAT_READY | ATA_STAT_ERR);
4065 else
4066 enmType = PDMMEDIAEXIOREQTYPE_SCSI;
4067 break;
4068 case ATA_IDENTIFY_PACKET_DEVICE:
4069 if (!pAhciPort->fATAPI)
4070 ahciReqSetStatus(pAhciReq, ABRT_ERR, ATA_STAT_READY | ATA_STAT_ERR);
4071 else
4072 {
4073 size_t cbData;
4074 ahciR3AtapiIdentify(pDevIns, pAhciReq, pAhciPort, 512, &cbData);
4075
4076 pAhciReq->fFlags |= AHCI_REQ_PIO_DATA;
4077 pAhciReq->cbTransfer = cbData;
4078 pAhciReq->cmdFis[AHCI_CMDFIS_SECTN] = (pAhciReq->cmdFis[AHCI_CMDFIS_SECTN] & ~7)
4079 | ((pAhciReq->fFlags & AHCI_REQ_XFER_2_HOST) ? ATAPI_INT_REASON_IO : 0)
4080 | (!pAhciReq->cbTransfer ? ATAPI_INT_REASON_CD : 0);
4081
4082 ahciReqSetStatus(pAhciReq, 0, ATA_STAT_READY | ATA_STAT_SEEK);
4083 }
4084 break;
4085 case ATA_SET_MULTIPLE_MODE:
4086 if ( pCmdFis[AHCI_CMDFIS_SECTC] != 0
4087 && ( pCmdFis[AHCI_CMDFIS_SECTC] > ATA_MAX_MULT_SECTORS
4088 || (pCmdFis[AHCI_CMDFIS_SECTC] & (pCmdFis[AHCI_CMDFIS_SECTC] - 1)) != 0))
4089 ahciReqSetStatus(pAhciReq, ABRT_ERR, ATA_STAT_READY | ATA_STAT_ERR);
4090 else
4091 {
4092 Log2(("%s: set multi sector count to %d\n", __FUNCTION__, pCmdFis[AHCI_CMDFIS_SECTC]));
4093 pAhciPort->cMultSectors = pCmdFis[AHCI_CMDFIS_SECTC];
4094 ahciReqSetStatus(pAhciReq, 0, ATA_STAT_READY | ATA_STAT_SEEK);
4095 }
4096 break;
4097 case ATA_STANDBY_IMMEDIATE:
4098 break; /* Do nothing. */
4099 case ATA_CHECK_POWER_MODE:
4100 pAhciReq->cmdFis[AHCI_CMDFIS_SECTC] = 0xff; /* drive active or idle */
4101 RT_FALL_THRU();
4102 case ATA_INITIALIZE_DEVICE_PARAMETERS:
4103 case ATA_IDLE_IMMEDIATE:
4104 case ATA_RECALIBRATE:
4105 case ATA_NOP:
4106 case ATA_READ_VERIFY_SECTORS_EXT:
4107 case ATA_READ_VERIFY_SECTORS:
4108 case ATA_READ_VERIFY_SECTORS_WITHOUT_RETRIES:
4109 case ATA_SLEEP:
4110 ahciReqSetStatus(pAhciReq, 0, ATA_STAT_READY | ATA_STAT_SEEK);
4111 break;
4112 case ATA_READ_DMA_EXT:
4113 fLBA48 = true;
4114 RT_FALL_THRU();
4115 case ATA_READ_DMA:
4116 {
4117 pAhciReq->cbTransfer = ahciGetNSectors(pCmdFis, fLBA48) * pAhciPort->cbSector;
4118 pAhciReq->uOffset = ahciGetSector(pAhciPort, pCmdFis, fLBA48) * pAhciPort->cbSector;
4119 enmType = PDMMEDIAEXIOREQTYPE_READ;
4120 break;
4121 }
4122 case ATA_WRITE_DMA_EXT:
4123 fLBA48 = true;
4124 RT_FALL_THRU();
4125 case ATA_WRITE_DMA:
4126 {
4127 pAhciReq->cbTransfer = ahciGetNSectors(pCmdFis, fLBA48) * pAhciPort->cbSector;
4128 pAhciReq->uOffset = ahciGetSector(pAhciPort, pCmdFis, fLBA48) * pAhciPort->cbSector;
4129 enmType = PDMMEDIAEXIOREQTYPE_WRITE;
4130 break;
4131 }
4132 case ATA_READ_FPDMA_QUEUED:
4133 {
4134 pAhciReq->cbTransfer = ahciGetNSectorsQueued(pCmdFis) * pAhciPort->cbSector;
4135 pAhciReq->uOffset = ahciGetSectorQueued(pCmdFis) * pAhciPort->cbSector;
4136 pAhciReq->fFlags |= AHCI_REQ_IS_QUEUED;
4137 enmType = PDMMEDIAEXIOREQTYPE_READ;
4138 break;
4139 }
4140 case ATA_WRITE_FPDMA_QUEUED:
4141 {
4142 pAhciReq->cbTransfer = ahciGetNSectorsQueued(pCmdFis) * pAhciPort->cbSector;
4143 pAhciReq->uOffset = ahciGetSectorQueued(pCmdFis) * pAhciPort->cbSector;
4144 pAhciReq->fFlags |= AHCI_REQ_IS_QUEUED;
4145 enmType = PDMMEDIAEXIOREQTYPE_WRITE;
4146 break;
4147 }
4148 case ATA_READ_LOG_EXT:
4149 {
4150 size_t cbLogRead = ((pCmdFis[AHCI_CMDFIS_SECTCEXP] << 8) | pCmdFis[AHCI_CMDFIS_SECTC]) * 512;
4151 unsigned offLogRead = ((pCmdFis[AHCI_CMDFIS_CYLLEXP] << 8) | pCmdFis[AHCI_CMDFIS_CYLL]) * 512;
4152 unsigned iPage = pCmdFis[AHCI_CMDFIS_SECTN];
4153
4154 LogFlow(("Trying to read %zu bytes starting at offset %u from page %u\n", cbLogRead, offLogRead, iPage));
4155
4156 uint8_t aBuf[512];
4157
4158 memset(aBuf, 0, sizeof(aBuf));
4159
4160 if (offLogRead + cbLogRead <= sizeof(aBuf))
4161 {
4162 switch (iPage)
4163 {
4164 case 0x10:
4165 {
4166 LogFlow(("Reading error page\n"));
4167 PAHCIREQ pTaskErr = ASMAtomicXchgPtrT(&pAhciPortR3->pTaskErr, NULL, PAHCIREQ);
4168 if (pTaskErr)
4169 {
4170 aBuf[0] = (pTaskErr->fFlags & AHCI_REQ_IS_QUEUED) ? pTaskErr->uTag : (1 << 7);
4171 aBuf[2] = pTaskErr->cmdFis[AHCI_CMDFIS_STS];
4172 aBuf[3] = pTaskErr->cmdFis[AHCI_CMDFIS_ERR];
4173 aBuf[4] = pTaskErr->cmdFis[AHCI_CMDFIS_SECTN];
4174 aBuf[5] = pTaskErr->cmdFis[AHCI_CMDFIS_CYLL];
4175 aBuf[6] = pTaskErr->cmdFis[AHCI_CMDFIS_CYLH];
4176 aBuf[7] = pTaskErr->cmdFis[AHCI_CMDFIS_HEAD];
4177 aBuf[8] = pTaskErr->cmdFis[AHCI_CMDFIS_SECTNEXP];
4178 aBuf[9] = pTaskErr->cmdFis[AHCI_CMDFIS_CYLLEXP];
4179 aBuf[10] = pTaskErr->cmdFis[AHCI_CMDFIS_CYLHEXP];
4180 aBuf[12] = pTaskErr->cmdFis[AHCI_CMDFIS_SECTC];
4181 aBuf[13] = pTaskErr->cmdFis[AHCI_CMDFIS_SECTCEXP];
4182
4183 /* Calculate checksum */
4184 uint8_t uChkSum = 0;
4185 for (unsigned i = 0; i < RT_ELEMENTS(aBuf)-1; i++)
4186 uChkSum += aBuf[i];
4187
4188 aBuf[511] = (uint8_t)-(int8_t)uChkSum;
4189
4190 /* Finally free the error task state structure because it is completely unused now. */
4191 RTMemFree(pTaskErr);
4192 }
4193
4194 /*
4195 * Reading this log page results in an abort of all outstanding commands
4196 * and clearing the SActive register and TaskFile register.
4197 *
4198 * See SATA2 1.2 spec chapter 4.2.3.4
4199 */
4200 bool fAbortedAll = ahciR3CancelActiveTasks(pAhciPortR3);
4201 Assert(fAbortedAll); NOREF(fAbortedAll);
4202 ahciSendSDBFis(pDevIns, pThis, pAhciPort, pAhciPortR3, UINT32_C(0xffffffff), true);
4203
4204 break;
4205 }
4206 }
4207
4208 /* Copy the buffer. */
4209 size_t cbCopied = ahciR3CopyBufferToPrdtl(pDevIns, pAhciReq, &aBuf[offLogRead], cbLogRead, 0 /* cbSkip */);
4210
4211 pAhciReq->fFlags |= AHCI_REQ_PIO_DATA;
4212 pAhciReq->cbTransfer = cbCopied;
4213 }
4214
4215 break;
4216 }
4217 case ATA_DATA_SET_MANAGEMENT:
4218 {
4219 if (pAhciPort->fTrimEnabled)
4220 {
4221 /* Check that the trim bit is set and all other bits are 0. */
4222 if ( !(pAhciReq->cmdFis[AHCI_CMDFIS_FET] & UINT16_C(0x01))
4223 || (pAhciReq->cmdFis[AHCI_CMDFIS_FET] & ~UINT16_C(0x1)))
4224 ahciReqSetStatus(pAhciReq, ABRT_ERR, ATA_STAT_READY | ATA_STAT_ERR);
4225 else
4226 enmType = PDMMEDIAEXIOREQTYPE_DISCARD;
4227 break;
4228 }
4229 /* else: fall through and report error to the guest. */
4230 }
4231 RT_FALL_THRU();
4232 /* All not implemented commands go below. */
4233 case ATA_SECURITY_FREEZE_LOCK:
4234 case ATA_SMART:
4235 case ATA_NV_CACHE:
4236 case ATA_IDLE:
4237 case ATA_TRUSTED_RECEIVE_DMA: /* Windows 8+ */
4238 ahciReqSetStatus(pAhciReq, ABRT_ERR, ATA_STAT_READY | ATA_STAT_ERR);
4239 break;
4240 default: /* For debugging purposes. */
4241 AssertMsgFailed(("Unknown command issued (%#x)\n", pCmdFis[AHCI_CMDFIS_CMD]));
4242 ahciReqSetStatus(pAhciReq, ABRT_ERR, ATA_STAT_READY | ATA_STAT_ERR);
4243 }
4244
4245 return enmType;
4246}
4247
4248/**
4249 * Retrieve a command FIS from guest memory.
4250 *
4251 * @returns whether the H2D FIS was successfully read from the guest memory.
4252 * @param pDevIns The device instance.
4253 * @param pThis The shared AHCI state.
4254 * @param pAhciPort The AHCI port of the request.
4255 * @param pAhciReq The state of the actual task.
4256 */
4257static bool ahciPortTaskGetCommandFis(PPDMDEVINS pDevIns, PAHCI pThis, PAHCIPORT pAhciPort, PAHCIREQ pAhciReq)
4258{
4259 AssertMsgReturn(pAhciPort->GCPhysAddrClb && pAhciPort->GCPhysAddrFb,
4260 ("%s: GCPhysAddrClb and/or GCPhysAddrFb are 0\n", __FUNCTION__),
4261 false);
4262
4263 /*
4264 * First we are reading the command header pointed to by regCLB.
4265 * From this we get the address of the command table which we are reading too.
4266 * We can process the Command FIS afterwards.
4267 */
4268 CmdHdr cmdHdr;
4269 pAhciReq->GCPhysCmdHdrAddr = pAhciPort->GCPhysAddrClb + pAhciReq->uTag * sizeof(CmdHdr);
4270 LogFlow(("%s: PDMDevHlpPhysRead GCPhysAddrCmdLst=%RGp cbCmdHdr=%u\n", __FUNCTION__,
4271 pAhciReq->GCPhysCmdHdrAddr, sizeof(CmdHdr)));
4272 PDMDevHlpPhysRead(pDevIns, pAhciReq->GCPhysCmdHdrAddr, &cmdHdr, sizeof(CmdHdr));
4273
4274#ifdef LOG_ENABLED
4275 /* Print some infos about the command header. */
4276 ahciDumpCmdHdrInfo(pAhciPort, &cmdHdr);
4277#endif
4278
4279 RTGCPHYS GCPhysAddrCmdTbl = AHCI_RTGCPHYS_FROM_U32(cmdHdr.u32CmdTblAddrUp, cmdHdr.u32CmdTblAddr);
4280
4281 AssertMsgReturn((cmdHdr.u32DescInf & AHCI_CMDHDR_CFL_MASK) * sizeof(uint32_t) == AHCI_CMDFIS_TYPE_H2D_SIZE,
4282 ("This is not a command FIS!!\n"),
4283 false);
4284
4285 /* Read the command Fis. */
4286 LogFlow(("%s: PDMDevHlpPhysRead GCPhysAddrCmdTbl=%RGp cbCmdFis=%u\n", __FUNCTION__, GCPhysAddrCmdTbl, AHCI_CMDFIS_TYPE_H2D_SIZE));
4287 PDMDevHlpPhysRead(pDevIns, GCPhysAddrCmdTbl, &pAhciReq->cmdFis[0], AHCI_CMDFIS_TYPE_H2D_SIZE);
4288
4289 AssertMsgReturn(pAhciReq->cmdFis[AHCI_CMDFIS_TYPE] == AHCI_CMDFIS_TYPE_H2D,
4290 ("This is not a command FIS\n"),
4291 false);
4292
4293 /* Set transfer direction. */
4294 pAhciReq->fFlags |= (cmdHdr.u32DescInf & AHCI_CMDHDR_W) ? 0 : AHCI_REQ_XFER_2_HOST;
4295
4296 /* If this is an ATAPI command read the atapi command. */
4297 if (cmdHdr.u32DescInf & AHCI_CMDHDR_A)
4298 {
4299 GCPhysAddrCmdTbl += AHCI_CMDHDR_ACMD_OFFSET;
4300 PDMDevHlpPhysRead(pDevIns, GCPhysAddrCmdTbl, &pAhciReq->aATAPICmd[0], ATAPI_PACKET_SIZE);
4301 }
4302
4303 /* We "received" the FIS. Clear the BSY bit in regTFD. */
4304 if ((cmdHdr.u32DescInf & AHCI_CMDHDR_C) && (pAhciReq->fFlags & AHCI_REQ_CLEAR_SACT))
4305 {
4306 /*
4307 * We need to send a FIS which clears the busy bit if this is a queued command so that the guest can queue other commands.
4308 * but this FIS does not assert an interrupt
4309 */
4310 ahciSendD2HFis(pDevIns, pThis, pAhciPort, pAhciReq->uTag, pAhciReq->cmdFis, false);
4311 pAhciPort->regTFD &= ~AHCI_PORT_TFD_BSY;
4312 }
4313
4314 pAhciReq->GCPhysPrdtl = AHCI_RTGCPHYS_FROM_U32(cmdHdr.u32CmdTblAddrUp, cmdHdr.u32CmdTblAddr) + AHCI_CMDHDR_PRDT_OFFSET;
4315 pAhciReq->cPrdtlEntries = AHCI_CMDHDR_PRDTL_ENTRIES(cmdHdr.u32DescInf);
4316
4317#ifdef LOG_ENABLED
4318 /* Print some infos about the FIS. */
4319 ahciDumpFisInfo(pAhciPort, &pAhciReq->cmdFis[0]);
4320
4321 /* Print the PRDT */
4322 ahciLog(("PRDT address %RGp number of entries %u\n", pAhciReq->GCPhysPrdtl, pAhciReq->cPrdtlEntries));
4323 RTGCPHYS GCPhysPrdtl = pAhciReq->GCPhysPrdtl;
4324
4325 for (unsigned i = 0; i < pAhciReq->cPrdtlEntries; i++)
4326 {
4327 SGLEntry SGEntry;
4328
4329 ahciLog(("Entry %u at address %RGp\n", i, GCPhysPrdtl));
4330 PDMDevHlpPhysRead(pDevIns, GCPhysPrdtl, &SGEntry, sizeof(SGLEntry));
4331
4332 RTGCPHYS GCPhysDataAddr = AHCI_RTGCPHYS_FROM_U32(SGEntry.u32DBAUp, SGEntry.u32DBA);
4333 ahciLog(("GCPhysAddr=%RGp Size=%u\n", GCPhysDataAddr, SGEntry.u32DescInf & SGLENTRY_DESCINF_DBC));
4334
4335 GCPhysPrdtl += sizeof(SGLEntry);
4336 }
4337#endif
4338
4339 return true;
4340}
4341
4342/**
4343 * Submits a given request for execution.
4344 *
4345 * @returns Flag whether the request was canceled inbetween.
4346 * @param pDevIns The device instance.
4347 * @param pThis The shared AHCI state.
4348 * @param pThisCC The ring-3 AHCI state.
4349 * @param pAhciPort The port the request is for.
4350 * @param pAhciReq The request to submit.
4351 * @param enmType The request type.
4352 */
4353static bool ahciR3ReqSubmit(PPDMDEVINS pDevIns, PAHCI pThis, PAHCICC pThisCC, PAHCIPORT pAhciPort, PAHCIPORTR3 pAhciPortR3,
4354 PAHCIREQ pAhciReq, PDMMEDIAEXIOREQTYPE enmType)
4355{
4356 int rc = VINF_SUCCESS;
4357 bool fReqCanceled = false;
4358
4359 VBOXDD_AHCI_REQ_SUBMIT(pAhciReq, pAhciReq->enmType, pAhciReq->uOffset, pAhciReq->cbTransfer);
4360
4361 if (enmType == PDMMEDIAEXIOREQTYPE_FLUSH)
4362 rc = pAhciPortR3->pDrvMediaEx->pfnIoReqFlush(pAhciPortR3->pDrvMediaEx, pAhciReq->hIoReq);
4363 else if (enmType == PDMMEDIAEXIOREQTYPE_DISCARD)
4364 {
4365 uint32_t cRangesMax;
4366
4367 /* The data buffer contains LBA range entries. Each range is 8 bytes big. */
4368 if (!pAhciReq->cmdFis[AHCI_CMDFIS_SECTC] && !pAhciReq->cmdFis[AHCI_CMDFIS_SECTCEXP])
4369 cRangesMax = 65536 * 512 / 8;
4370 else
4371 cRangesMax = pAhciReq->cmdFis[AHCI_CMDFIS_SECTC] * 512 / 8;
4372
4373 pAhciPort->Led.Asserted.s.fWriting = pAhciPort->Led.Actual.s.fWriting = 1;
4374 rc = pAhciPortR3->pDrvMediaEx->pfnIoReqDiscard(pAhciPortR3->pDrvMediaEx, pAhciReq->hIoReq,
4375 cRangesMax);
4376 }
4377 else if (enmType == PDMMEDIAEXIOREQTYPE_READ)
4378 {
4379 pAhciPort->Led.Asserted.s.fReading = pAhciPort->Led.Actual.s.fReading = 1;
4380 rc = pAhciPortR3->pDrvMediaEx->pfnIoReqRead(pAhciPortR3->pDrvMediaEx, pAhciReq->hIoReq,
4381 pAhciReq->uOffset, pAhciReq->cbTransfer);
4382 }
4383 else if (enmType == PDMMEDIAEXIOREQTYPE_WRITE)
4384 {
4385 pAhciPort->Led.Asserted.s.fWriting = pAhciPort->Led.Actual.s.fWriting = 1;
4386 rc = pAhciPortR3->pDrvMediaEx->pfnIoReqWrite(pAhciPortR3->pDrvMediaEx, pAhciReq->hIoReq,
4387 pAhciReq->uOffset, pAhciReq->cbTransfer);
4388 }
4389 else if (enmType == PDMMEDIAEXIOREQTYPE_SCSI)
4390 {
4391 size_t cbBuf = 0;
4392
4393 if (pAhciReq->cPrdtlEntries)
4394 rc = ahciR3PrdtQuerySize(pDevIns, pAhciReq, &cbBuf);
4395 pAhciReq->cbTransfer = cbBuf;
4396 if (RT_SUCCESS(rc))
4397 {
4398 if (cbBuf && (pAhciReq->fFlags & AHCI_REQ_XFER_2_HOST))
4399 pAhciPort->Led.Asserted.s.fReading = pAhciPort->Led.Actual.s.fReading = 1;
4400 else if (cbBuf)
4401 pAhciPort->Led.Asserted.s.fWriting = pAhciPort->Led.Actual.s.fWriting = 1;
4402 rc = pAhciPortR3->pDrvMediaEx->pfnIoReqSendScsiCmd(pAhciPortR3->pDrvMediaEx, pAhciReq->hIoReq,
4403 0, &pAhciReq->aATAPICmd[0], ATAPI_PACKET_SIZE,
4404 PDMMEDIAEXIOREQSCSITXDIR_UNKNOWN, NULL, cbBuf,
4405 &pAhciPort->abATAPISense[0], sizeof(pAhciPort->abATAPISense), NULL,
4406 &pAhciReq->u8ScsiSts, 30 * RT_MS_1SEC);
4407 }
4408 }
4409
4410 if (rc == VINF_SUCCESS)
4411 fReqCanceled = ahciR3TransferComplete(pDevIns, pThis, pThisCC, pAhciPort, pAhciPortR3, pAhciReq, VINF_SUCCESS);
4412 else if (rc != VINF_PDM_MEDIAEX_IOREQ_IN_PROGRESS)
4413 fReqCanceled = ahciR3TransferComplete(pDevIns, pThis, pThisCC, pAhciPort, pAhciPortR3, pAhciReq, rc);
4414
4415 return fReqCanceled;
4416}
4417
4418/**
4419 * Prepares the command for execution coping it from guest memory and doing a few
4420 * validation checks on it.
4421 *
4422 * @returns Whether the command was successfully fetched from guest memory and
4423 * can be continued.
4424 * @param pDevIns The device instance.
4425 * @param pThis The shared AHCI state.
4426 * @param pAhciPort The AHCI port the request is for.
4427 * @param pAhciReq Request structure to copy the command to.
4428 */
4429static bool ahciR3CmdPrepare(PPDMDEVINS pDevIns, PAHCI pThis, PAHCIPORT pAhciPort, PAHCIREQ pAhciReq)
4430{
4431 /* Set current command slot */
4432 ASMAtomicWriteU32(&pAhciPort->u32CurrentCommandSlot, pAhciReq->uTag);
4433
4434 bool fContinue = ahciPortTaskGetCommandFis(pDevIns, pThis, pAhciPort, pAhciReq);
4435 if (fContinue)
4436 {
4437 /* Mark the task as processed by the HBA if this is a queued task so that it doesn't occur in the CI register anymore. */
4438 if (pAhciPort->regSACT & RT_BIT_32(pAhciReq->uTag))
4439 {
4440 pAhciReq->fFlags |= AHCI_REQ_CLEAR_SACT;
4441 ASMAtomicOrU32(&pAhciPort->u32TasksFinished, RT_BIT_32(pAhciReq->uTag));
4442 }
4443
4444 if (pAhciReq->cmdFis[AHCI_CMDFIS_BITS] & AHCI_CMDFIS_C)
4445 {
4446 /*
4447 * It is possible that the request counter can get one higher than the maximum because
4448 * the request counter is decremented after the guest was notified about the completed
4449 * request (see @bugref{7859}). If the completing thread is preempted in between the
4450 * guest might already issue another request before the request counter is decremented
4451 * which would trigger the following assertion incorrectly in the past.
4452 */
4453 AssertLogRelMsg(ASMAtomicReadU32(&pAhciPort->cTasksActive) <= AHCI_NR_COMMAND_SLOTS,
4454 ("AHCI#%uP%u: There are more than %u (+1) requests active",
4455 pDevIns->iInstance, pAhciPort->iLUN,
4456 AHCI_NR_COMMAND_SLOTS));
4457 ASMAtomicIncU32(&pAhciPort->cTasksActive);
4458 }
4459 else
4460 {
4461 /* If the reset bit is set put the device into reset state. */
4462 if (pAhciReq->cmdFis[AHCI_CMDFIS_CTL] & AHCI_CMDFIS_CTL_SRST)
4463 {
4464 ahciLog(("%s: Setting device into reset state\n", __FUNCTION__));
4465 pAhciPort->fResetDevice = true;
4466 ahciSendD2HFis(pDevIns, pThis, pAhciPort, pAhciReq->uTag, pAhciReq->cmdFis, true);
4467 }
4468 else if (pAhciPort->fResetDevice) /* The bit is not set and we are in a reset state. */
4469 ahciFinishStorageDeviceReset(pDevIns, pThis, pAhciPort, pAhciReq);
4470 else /* We are not in a reset state update the control registers. */
4471 AssertMsgFailed(("%s: Update the control register\n", __FUNCTION__));
4472
4473 fContinue = false;
4474 }
4475 }
4476 else
4477 {
4478 /*
4479 * Couldn't find anything in either the AHCI or SATA spec which
4480 * indicates what should be done if the FIS is not read successfully.
4481 * The closest thing is in the state machine, stating that the device
4482 * should go into idle state again (SATA spec 1.0 chapter 8.7.1).
4483 * Do the same here and ignore any corrupt FIS types, after all
4484 * the guest messed up everything and this behavior is undefined.
4485 */
4486 fContinue = false;
4487 }
4488
4489 return fContinue;
4490}
4491
4492/**
4493 * @callback_method_impl{FNPDMTHREADDEV, The async IO thread for one port.}
4494 */
4495static DECLCALLBACK(int) ahciAsyncIOLoop(PPDMDEVINS pDevIns, PPDMTHREAD pThread)
4496{
4497 PAHCIPORTR3 pAhciPortR3 = (PAHCIPORTR3)pThread->pvUser;
4498 PAHCI pThis = PDMDEVINS_2_DATA(pDevIns, PAHCI);
4499 PAHCIR3 pThisCC = PDMDEVINS_2_DATA_CC(pDevIns, PAHCICC);
4500 PAHCIPORT pAhciPort = &RT_SAFE_SUBSCRIPT(pThis->aPorts, pAhciPortR3->iLUN);
4501 int rc = VINF_SUCCESS;
4502
4503 ahciLog(("%s: Port %d entering async IO loop.\n", __FUNCTION__, pAhciPort->iLUN));
4504
4505 if (pThread->enmState == PDMTHREADSTATE_INITIALIZING)
4506 return VINF_SUCCESS;
4507
4508 while (pThread->enmState == PDMTHREADSTATE_RUNNING)
4509 {
4510 unsigned idx = 0;
4511 uint32_t u32Tasks = 0;
4512 uint32_t u32RegHbaCtrl = 0;
4513
4514 ASMAtomicWriteBool(&pAhciPort->fWrkThreadSleeping, true);
4515 u32Tasks = ASMAtomicXchgU32(&pAhciPort->u32TasksNew, 0);
4516 if (!u32Tasks)
4517 {
4518 Assert(ASMAtomicReadBool(&pAhciPort->fWrkThreadSleeping));
4519 rc = PDMDevHlpSUPSemEventWaitNoResume(pDevIns, pAhciPort->hEvtProcess, RT_INDEFINITE_WAIT);
4520 AssertLogRelMsgReturn(RT_SUCCESS(rc) || rc == VERR_INTERRUPTED, ("%Rrc\n", rc), rc);
4521 if (RT_UNLIKELY(pThread->enmState != PDMTHREADSTATE_RUNNING))
4522 break;
4523 LogFlowFunc(("Woken up with rc=%Rrc\n", rc));
4524 u32Tasks = ASMAtomicXchgU32(&pAhciPort->u32TasksNew, 0);
4525 }
4526
4527 ASMAtomicWriteBool(&pAhciPort->fWrkThreadSleeping, false);
4528 ASMAtomicIncU32(&pThis->cThreadsActive);
4529
4530 /* Check whether the thread should be suspended. */
4531 if (pThisCC->fSignalIdle)
4532 {
4533 if (!ASMAtomicDecU32(&pThis->cThreadsActive))
4534 PDMDevHlpAsyncNotificationCompleted(pDevIns);
4535 continue;
4536 }
4537
4538 /*
4539 * Check whether the global host controller bit is set and go to sleep immediately again
4540 * if it is set.
4541 */
4542 u32RegHbaCtrl = ASMAtomicReadU32(&pThis->regHbaCtrl);
4543 if ( u32RegHbaCtrl & AHCI_HBA_CTRL_HR
4544 && !ASMAtomicDecU32(&pThis->cThreadsActive))
4545 {
4546 ahciR3HBAReset(pDevIns, pThis, pThisCC);
4547 if (pThisCC->fSignalIdle)
4548 PDMDevHlpAsyncNotificationCompleted(pDevIns);
4549 continue;
4550 }
4551
4552 idx = ASMBitFirstSetU32(u32Tasks);
4553 while ( idx
4554 && !pAhciPort->fPortReset)
4555 {
4556 bool fReqCanceled = false;
4557
4558 /* Decrement to get the slot number. */
4559 idx--;
4560 ahciLog(("%s: Processing command at slot %d\n", __FUNCTION__, idx));
4561
4562 PAHCIREQ pAhciReq = ahciR3ReqAlloc(pAhciPortR3, idx);
4563 if (RT_LIKELY(pAhciReq))
4564 {
4565 pAhciReq->uTag = idx;
4566 pAhciReq->fFlags = 0;
4567
4568 bool fContinue = ahciR3CmdPrepare(pDevIns, pThis, pAhciPort, pAhciReq);
4569 if (fContinue)
4570 {
4571 PDMMEDIAEXIOREQTYPE enmType = ahciProcessCmd(pDevIns, pThis, pAhciPort, pAhciPortR3,
4572 pAhciReq, pAhciReq->cmdFis);
4573 pAhciReq->enmType = enmType;
4574
4575 if (enmType != PDMMEDIAEXIOREQTYPE_INVALID)
4576 fReqCanceled = ahciR3ReqSubmit(pDevIns, pThis, pThisCC, pAhciPort, pAhciPortR3, pAhciReq, enmType);
4577 else
4578 fReqCanceled = ahciR3TransferComplete(pDevIns, pThis, pThisCC, pAhciPort, pAhciPortR3,
4579 pAhciReq, VINF_SUCCESS);
4580 } /* Command */
4581 else
4582 ahciR3ReqFree(pAhciPortR3, pAhciReq);
4583 }
4584 else /* !Request allocated, use on stack variant to signal the error. */
4585 {
4586 AHCIREQ Req;
4587 Req.uTag = idx;
4588 Req.fFlags = AHCI_REQ_IS_ON_STACK;
4589 Req.fMapped = false;
4590 Req.cbTransfer = 0;
4591 Req.uOffset = 0;
4592 Req.enmType = PDMMEDIAEXIOREQTYPE_INVALID;
4593
4594 bool fContinue = ahciR3CmdPrepare(pDevIns, pThis, pAhciPort, &Req);
4595 if (fContinue)
4596 fReqCanceled = ahciR3TransferComplete(pDevIns, pThis, pThisCC, pAhciPort, pAhciPortR3, &Req, VERR_NO_MEMORY);
4597 }
4598
4599 /*
4600 * Don't process other requests if the last one was canceled,
4601 * the others are not valid anymore.
4602 */
4603 if (fReqCanceled)
4604 break;
4605
4606 u32Tasks &= ~RT_BIT_32(idx); /* Clear task bit. */
4607 idx = ASMBitFirstSetU32(u32Tasks);
4608 } /* while tasks available */
4609
4610 /* Check whether a port reset was active. */
4611 if ( ASMAtomicReadBool(&pAhciPort->fPortReset)
4612 && (pAhciPort->regSCTL & AHCI_PORT_SCTL_DET) == AHCI_PORT_SCTL_DET_NINIT)
4613 ahciPortResetFinish(pDevIns, pThis, pAhciPort, pAhciPortR3);
4614
4615 /*
4616 * Check whether a host controller reset is pending and execute the reset
4617 * if this is the last active thread.
4618 */
4619 u32RegHbaCtrl = ASMAtomicReadU32(&pThis->regHbaCtrl);
4620 uint32_t cThreadsActive = ASMAtomicDecU32(&pThis->cThreadsActive);
4621 if ( (u32RegHbaCtrl & AHCI_HBA_CTRL_HR)
4622 && !cThreadsActive)
4623 ahciR3HBAReset(pDevIns, pThis, pThisCC);
4624
4625 if (!cThreadsActive && pThisCC->fSignalIdle)
4626 PDMDevHlpAsyncNotificationCompleted(pDevIns);
4627 } /* While running */
4628
4629 ahciLog(("%s: Port %d async IO thread exiting\n", __FUNCTION__, pAhciPort->iLUN));
4630 return VINF_SUCCESS;
4631}
4632
4633/**
4634 * @callback_method_impl{FNPDMTHREADWAKEUPDEV}
4635 */
4636static DECLCALLBACK(int) ahciAsyncIOLoopWakeUp(PPDMDEVINS pDevIns, PPDMTHREAD pThread)
4637{
4638 PAHCIPORTR3 pAhciPortR3 = (PAHCIPORTR3)pThread->pvUser;
4639 PAHCI pThis = PDMDEVINS_2_DATA(pDevIns, PAHCI);
4640 PAHCIPORT pAhciPort = &RT_SAFE_SUBSCRIPT(pThis->aPorts, pAhciPortR3->iLUN);
4641 return PDMDevHlpSUPSemEventSignal(pDevIns, pAhciPort->hEvtProcess);
4642}
4643
4644/* -=-=-=-=- DBGF -=-=-=-=- */
4645
4646/**
4647 * AHCI status info callback.
4648 *
4649 * @param pDevIns The device instance.
4650 * @param pHlp The output helpers.
4651 * @param pszArgs The arguments.
4652 */
4653static DECLCALLBACK(void) ahciR3Info(PPDMDEVINS pDevIns, PCDBGFINFOHLP pHlp, const char *pszArgs)
4654{
4655 RT_NOREF(pszArgs);
4656 PAHCI pThis = PDMDEVINS_2_DATA(pDevIns, PAHCI);
4657
4658 /*
4659 * Show info.
4660 */
4661 pHlp->pfnPrintf(pHlp,
4662 "%s#%d: mmio=%RGp ports=%u GC=%RTbool R0=%RTbool\n",
4663 pDevIns->pReg->szName,
4664 pDevIns->iInstance,
4665 PDMDevHlpMmioGetMappingAddress(pDevIns, pThis->hMmio),
4666 pThis->cPortsImpl,
4667 pDevIns->fRCEnabled,
4668 pDevIns->fR0Enabled);
4669
4670 /*
4671 * Show global registers.
4672 */
4673 pHlp->pfnPrintf(pHlp, "HbaCap=%#x\n", pThis->regHbaCap);
4674 pHlp->pfnPrintf(pHlp, "HbaCtrl=%#x\n", pThis->regHbaCtrl);
4675 pHlp->pfnPrintf(pHlp, "HbaIs=%#x\n", pThis->regHbaIs);
4676 pHlp->pfnPrintf(pHlp, "HbaPi=%#x\n", pThis->regHbaPi);
4677 pHlp->pfnPrintf(pHlp, "HbaVs=%#x\n", pThis->regHbaVs);
4678 pHlp->pfnPrintf(pHlp, "HbaCccCtl=%#x\n", pThis->regHbaCccCtl);
4679 pHlp->pfnPrintf(pHlp, "HbaCccPorts=%#x\n", pThis->regHbaCccPorts);
4680 pHlp->pfnPrintf(pHlp, "PortsInterrupted=%#x\n", pThis->u32PortsInterrupted);
4681
4682 /*
4683 * Per port data.
4684 */
4685 uint32_t const cPortsImpl = RT_MIN(pThis->cPortsImpl, RT_ELEMENTS(pThis->aPorts));
4686 for (unsigned i = 0; i < cPortsImpl; i++)
4687 {
4688 PAHCIPORT pThisPort = &pThis->aPorts[i];
4689
4690 pHlp->pfnPrintf(pHlp, "Port %d: device-attached=%RTbool\n", pThisPort->iLUN, pThisPort->fPresent);
4691 pHlp->pfnPrintf(pHlp, "PortClb=%#x\n", pThisPort->regCLB);
4692 pHlp->pfnPrintf(pHlp, "PortClbU=%#x\n", pThisPort->regCLBU);
4693 pHlp->pfnPrintf(pHlp, "PortFb=%#x\n", pThisPort->regFB);
4694 pHlp->pfnPrintf(pHlp, "PortFbU=%#x\n", pThisPort->regFBU);
4695 pHlp->pfnPrintf(pHlp, "PortIs=%#x\n", pThisPort->regIS);
4696 pHlp->pfnPrintf(pHlp, "PortIe=%#x\n", pThisPort->regIE);
4697 pHlp->pfnPrintf(pHlp, "PortCmd=%#x\n", pThisPort->regCMD);
4698 pHlp->pfnPrintf(pHlp, "PortTfd=%#x\n", pThisPort->regTFD);
4699 pHlp->pfnPrintf(pHlp, "PortSig=%#x\n", pThisPort->regSIG);
4700 pHlp->pfnPrintf(pHlp, "PortSSts=%#x\n", pThisPort->regSSTS);
4701 pHlp->pfnPrintf(pHlp, "PortSCtl=%#x\n", pThisPort->regSCTL);
4702 pHlp->pfnPrintf(pHlp, "PortSErr=%#x\n", pThisPort->regSERR);
4703 pHlp->pfnPrintf(pHlp, "PortSAct=%#x\n", pThisPort->regSACT);
4704 pHlp->pfnPrintf(pHlp, "PortCi=%#x\n", pThisPort->regCI);
4705 pHlp->pfnPrintf(pHlp, "PortPhysClb=%RGp\n", pThisPort->GCPhysAddrClb);
4706 pHlp->pfnPrintf(pHlp, "PortPhysFb=%RGp\n", pThisPort->GCPhysAddrFb);
4707 pHlp->pfnPrintf(pHlp, "PortActTasksActive=%u\n", pThisPort->cTasksActive);
4708 pHlp->pfnPrintf(pHlp, "PortPoweredOn=%RTbool\n", pThisPort->fPoweredOn);
4709 pHlp->pfnPrintf(pHlp, "PortSpunUp=%RTbool\n", pThisPort->fSpunUp);
4710 pHlp->pfnPrintf(pHlp, "PortFirstD2HFisSent=%RTbool\n", pThisPort->fFirstD2HFisSent);
4711 pHlp->pfnPrintf(pHlp, "PortATAPI=%RTbool\n", pThisPort->fATAPI);
4712 pHlp->pfnPrintf(pHlp, "PortTasksFinished=%#x\n", pThisPort->u32TasksFinished);
4713 pHlp->pfnPrintf(pHlp, "PortQueuedTasksFinished=%#x\n", pThisPort->u32QueuedTasksFinished);
4714 pHlp->pfnPrintf(pHlp, "PortTasksNew=%#x\n", pThisPort->u32TasksNew);
4715 pHlp->pfnPrintf(pHlp, "\n");
4716 }
4717}
4718
4719/* -=-=-=-=- Helper -=-=-=-=- */
4720
4721/**
4722 * Checks if all asynchronous I/O is finished, both AHCI and IDE.
4723 *
4724 * Used by ahciR3Reset, ahciR3Suspend and ahciR3PowerOff. ahciR3SavePrep makes
4725 * use of it in strict builds (which is why it's up here).
4726 *
4727 * @returns true if quiesced, false if busy.
4728 * @param pDevIns The device instance.
4729 */
4730static bool ahciR3AllAsyncIOIsFinished(PPDMDEVINS pDevIns)
4731{
4732 PAHCI pThis = PDMDEVINS_2_DATA(pDevIns, PAHCI);
4733
4734 if (pThis->cThreadsActive)
4735 return false;
4736
4737 for (uint32_t i = 0; i < RT_ELEMENTS(pThis->aPorts); i++)
4738 {
4739 PAHCIPORT pThisPort = &pThis->aPorts[i];
4740 if (pThisPort->fPresent)
4741 {
4742 if ( (pThisPort->cTasksActive != 0)
4743 || (pThisPort->u32TasksNew != 0))
4744 return false;
4745 }
4746 }
4747 return true;
4748}
4749
4750/* -=-=-=-=- Saved State -=-=-=-=- */
4751
4752/**
4753 * @callback_method_impl{FNSSMDEVSAVEPREP}
4754 */
4755static DECLCALLBACK(int) ahciR3SavePrep(PPDMDEVINS pDevIns, PSSMHANDLE pSSM)
4756{
4757 RT_NOREF(pDevIns, pSSM);
4758 Assert(ahciR3AllAsyncIOIsFinished(pDevIns));
4759 return VINF_SUCCESS;
4760}
4761
4762/**
4763 * @callback_method_impl{FNSSMDEVLOADPREP}
4764 */
4765static DECLCALLBACK(int) ahciR3LoadPrep(PPDMDEVINS pDevIns, PSSMHANDLE pSSM)
4766{
4767 RT_NOREF(pDevIns, pSSM);
4768 Assert(ahciR3AllAsyncIOIsFinished(pDevIns));
4769 return VINF_SUCCESS;
4770}
4771
4772/**
4773 * @callback_method_impl{FNSSMDEVLIVEEXEC}
4774 */
4775static DECLCALLBACK(int) ahciR3LiveExec(PPDMDEVINS pDevIns, PSSMHANDLE pSSM, uint32_t uPass)
4776{
4777 PAHCI pThis = PDMDEVINS_2_DATA(pDevIns, PAHCI);
4778 PCPDMDEVHLPR3 pHlp = pDevIns->pHlpR3;
4779 RT_NOREF(uPass);
4780
4781 /* config. */
4782 pHlp->pfnSSMPutU32(pSSM, pThis->cPortsImpl);
4783 for (uint32_t i = 0; i < AHCI_MAX_NR_PORTS_IMPL; i++)
4784 {
4785 pHlp->pfnSSMPutBool(pSSM, pThis->aPorts[i].fPresent);
4786 pHlp->pfnSSMPutBool(pSSM, pThis->aPorts[i].fHotpluggable);
4787 pHlp->pfnSSMPutStrZ(pSSM, pThis->aPorts[i].szSerialNumber);
4788 pHlp->pfnSSMPutStrZ(pSSM, pThis->aPorts[i].szFirmwareRevision);
4789 pHlp->pfnSSMPutStrZ(pSSM, pThis->aPorts[i].szModelNumber);
4790 }
4791
4792 static const char *s_apszIdeEmuPortNames[4] = { "PrimaryMaster", "PrimarySlave", "SecondaryMaster", "SecondarySlave" };
4793 for (uint32_t i = 0; i < RT_ELEMENTS(s_apszIdeEmuPortNames); i++)
4794 {
4795 uint32_t iPort;
4796 int rc = pHlp->pfnCFGMQueryU32Def(pDevIns->pCfg, s_apszIdeEmuPortNames[i], &iPort, i);
4797 AssertRCReturn(rc, rc);
4798 pHlp->pfnSSMPutU32(pSSM, iPort);
4799 }
4800
4801 return VINF_SSM_DONT_CALL_AGAIN;
4802}
4803
4804/**
4805 * @callback_method_impl{FNSSMDEVSAVEEXEC}
4806 */
4807static DECLCALLBACK(int) ahciR3SaveExec(PPDMDEVINS pDevIns, PSSMHANDLE pSSM)
4808{
4809 PAHCI pThis = PDMDEVINS_2_DATA(pDevIns, PAHCI);
4810 PCPDMDEVHLPR3 pHlp = pDevIns->pHlpR3;
4811 uint32_t i;
4812 int rc;
4813
4814 Assert(!pThis->f8ByteMMIO4BytesWrittenSuccessfully);
4815
4816 /* The config */
4817 rc = ahciR3LiveExec(pDevIns, pSSM, SSM_PASS_FINAL);
4818 AssertRCReturn(rc, rc);
4819
4820 /* The main device structure. */
4821 pHlp->pfnSSMPutU32(pSSM, pThis->regHbaCap);
4822 pHlp->pfnSSMPutU32(pSSM, pThis->regHbaCtrl);
4823 pHlp->pfnSSMPutU32(pSSM, pThis->regHbaIs);
4824 pHlp->pfnSSMPutU32(pSSM, pThis->regHbaPi);
4825 pHlp->pfnSSMPutU32(pSSM, pThis->regHbaVs);
4826 pHlp->pfnSSMPutU32(pSSM, pThis->regHbaCccCtl);
4827 pHlp->pfnSSMPutU32(pSSM, pThis->regHbaCccPorts);
4828 pHlp->pfnSSMPutU8(pSSM, pThis->uCccPortNr);
4829 pHlp->pfnSSMPutU64(pSSM, pThis->uCccTimeout);
4830 pHlp->pfnSSMPutU32(pSSM, pThis->uCccNr);
4831 pHlp->pfnSSMPutU32(pSSM, pThis->uCccCurrentNr);
4832 pHlp->pfnSSMPutU32(pSSM, pThis->u32PortsInterrupted);
4833 pHlp->pfnSSMPutBool(pSSM, pThis->fReset);
4834 pHlp->pfnSSMPutBool(pSSM, pThis->f64BitAddr);
4835 pHlp->pfnSSMPutBool(pSSM, pDevIns->fR0Enabled);
4836 pHlp->pfnSSMPutBool(pSSM, pDevIns->fRCEnabled);
4837 pHlp->pfnSSMPutBool(pSSM, pThis->fLegacyPortResetMethod);
4838
4839 /* Now every port. */
4840 for (i = 0; i < AHCI_MAX_NR_PORTS_IMPL; i++)
4841 {
4842 Assert(pThis->aPorts[i].cTasksActive == 0);
4843 pHlp->pfnSSMPutU32(pSSM, pThis->aPorts[i].regCLB);
4844 pHlp->pfnSSMPutU32(pSSM, pThis->aPorts[i].regCLBU);
4845 pHlp->pfnSSMPutU32(pSSM, pThis->aPorts[i].regFB);
4846 pHlp->pfnSSMPutU32(pSSM, pThis->aPorts[i].regFBU);
4847 pHlp->pfnSSMPutGCPhys(pSSM, pThis->aPorts[i].GCPhysAddrClb);
4848 pHlp->pfnSSMPutGCPhys(pSSM, pThis->aPorts[i].GCPhysAddrFb);
4849 pHlp->pfnSSMPutU32(pSSM, pThis->aPorts[i].regIS);
4850 pHlp->pfnSSMPutU32(pSSM, pThis->aPorts[i].regIE);
4851 pHlp->pfnSSMPutU32(pSSM, pThis->aPorts[i].regCMD);
4852 pHlp->pfnSSMPutU32(pSSM, pThis->aPorts[i].regTFD);
4853 pHlp->pfnSSMPutU32(pSSM, pThis->aPorts[i].regSIG);
4854 pHlp->pfnSSMPutU32(pSSM, pThis->aPorts[i].regSSTS);
4855 pHlp->pfnSSMPutU32(pSSM, pThis->aPorts[i].regSCTL);
4856 pHlp->pfnSSMPutU32(pSSM, pThis->aPorts[i].regSERR);
4857 pHlp->pfnSSMPutU32(pSSM, pThis->aPorts[i].regSACT);
4858 pHlp->pfnSSMPutU32(pSSM, pThis->aPorts[i].regCI);
4859 pHlp->pfnSSMPutU32(pSSM, pThis->aPorts[i].PCHSGeometry.cCylinders);
4860 pHlp->pfnSSMPutU32(pSSM, pThis->aPorts[i].PCHSGeometry.cHeads);
4861 pHlp->pfnSSMPutU32(pSSM, pThis->aPorts[i].PCHSGeometry.cSectors);
4862 pHlp->pfnSSMPutU64(pSSM, pThis->aPorts[i].cTotalSectors);
4863 pHlp->pfnSSMPutU32(pSSM, pThis->aPorts[i].cMultSectors);
4864 pHlp->pfnSSMPutU8(pSSM, pThis->aPorts[i].uATATransferMode);
4865 pHlp->pfnSSMPutBool(pSSM, pThis->aPorts[i].fResetDevice);
4866 pHlp->pfnSSMPutBool(pSSM, pThis->aPorts[i].fPoweredOn);
4867 pHlp->pfnSSMPutBool(pSSM, pThis->aPorts[i].fSpunUp);
4868 pHlp->pfnSSMPutU32(pSSM, pThis->aPorts[i].u32TasksFinished);
4869 pHlp->pfnSSMPutU32(pSSM, pThis->aPorts[i].u32QueuedTasksFinished);
4870 pHlp->pfnSSMPutU32(pSSM, pThis->aPorts[i].u32CurrentCommandSlot);
4871
4872 /* ATAPI saved state. */
4873 pHlp->pfnSSMPutBool(pSSM, pThis->aPorts[i].fATAPI);
4874 pHlp->pfnSSMPutMem(pSSM, &pThis->aPorts[i].abATAPISense[0], sizeof(pThis->aPorts[i].abATAPISense));
4875 }
4876
4877 return pHlp->pfnSSMPutU32(pSSM, UINT32_MAX); /* sanity/terminator */
4878}
4879
4880/**
4881 * Loads a saved legacy ATA emulated device state.
4882 *
4883 * @returns VBox status code.
4884 * @param pHlp The device helper call table.
4885 * @param pSSM The handle to the saved state.
4886 */
4887static int ahciR3LoadLegacyEmulationState(PCPDMDEVHLPR3 pHlp, PSSMHANDLE pSSM)
4888{
4889 int rc;
4890 uint32_t u32Version;
4891 uint32_t u32;
4892 uint32_t u32IOBuffer;
4893
4894 /* Test for correct version. */
4895 rc = pHlp->pfnSSMGetU32(pSSM, &u32Version);
4896 AssertRCReturn(rc, rc);
4897 LogFlow(("LoadOldSavedStates u32Version = %d\n", u32Version));
4898
4899 if ( u32Version != ATA_CTL_SAVED_STATE_VERSION
4900 && u32Version != ATA_CTL_SAVED_STATE_VERSION_WITHOUT_FULL_SENSE
4901 && u32Version != ATA_CTL_SAVED_STATE_VERSION_WITHOUT_EVENT_STATUS)
4902 {
4903 AssertMsgFailed(("u32Version=%d\n", u32Version));
4904 return VERR_SSM_UNSUPPORTED_DATA_UNIT_VERSION;
4905 }
4906
4907 pHlp->pfnSSMSkip(pSSM, 19 + 5 * sizeof(bool) + 8 /* sizeof(BMDMAState) */);
4908
4909 for (uint32_t j = 0; j < 2; j++)
4910 {
4911 pHlp->pfnSSMSkip(pSSM, 88 + 5 * sizeof(bool) );
4912
4913 if (u32Version > ATA_CTL_SAVED_STATE_VERSION_WITHOUT_FULL_SENSE)
4914 pHlp->pfnSSMSkip(pSSM, 64);
4915 else
4916 pHlp->pfnSSMSkip(pSSM, 2);
4917 /** @todo triple-check this hack after passthrough is working */
4918 pHlp->pfnSSMSkip(pSSM, 1);
4919
4920 if (u32Version > ATA_CTL_SAVED_STATE_VERSION_WITHOUT_EVENT_STATUS)
4921 pHlp->pfnSSMSkip(pSSM, 4);
4922
4923 pHlp->pfnSSMSkip(pSSM, sizeof(PDMLED));
4924 pHlp->pfnSSMGetU32(pSSM, &u32IOBuffer);
4925 if (u32IOBuffer)
4926 pHlp->pfnSSMSkip(pSSM, u32IOBuffer);
4927 }
4928
4929 rc = pHlp->pfnSSMGetU32(pSSM, &u32);
4930 if (RT_FAILURE(rc))
4931 return rc;
4932 if (u32 != ~0U)
4933 {
4934 AssertMsgFailed(("u32=%#x expected ~0\n", u32));
4935 rc = VERR_SSM_DATA_UNIT_FORMAT_CHANGED;
4936 return rc;
4937 }
4938
4939 return VINF_SUCCESS;
4940}
4941
4942/**
4943 * @callback_method_impl{FNSSMDEVLOADEXEC}
4944 */
4945static DECLCALLBACK(int) ahciR3LoadExec(PPDMDEVINS pDevIns, PSSMHANDLE pSSM, uint32_t uVersion, uint32_t uPass)
4946{
4947 PAHCI pThis = PDMDEVINS_2_DATA(pDevIns, PAHCI);
4948 PCPDMDEVHLPR3 pHlp = pDevIns->pHlpR3;
4949 uint32_t u32;
4950 int rc;
4951
4952 if ( uVersion > AHCI_SAVED_STATE_VERSION
4953 || uVersion < AHCI_SAVED_STATE_VERSION_VBOX_30)
4954 return VERR_SSM_UNSUPPORTED_DATA_UNIT_VERSION;
4955
4956 /* Deal with the priod after removing the saved IDE bits where the saved
4957 state version remained unchanged. */
4958 if ( uVersion == AHCI_SAVED_STATE_VERSION_IDE_EMULATION
4959 && pHlp->pfnSSMHandleRevision(pSSM) >= 79045
4960 && pHlp->pfnSSMHandleRevision(pSSM) < 79201)
4961 uVersion++;
4962
4963 /*
4964 * Check whether we have to resort to the legacy port reset method to
4965 * prevent older BIOS versions from failing after a reset.
4966 */
4967 if (uVersion <= AHCI_SAVED_STATE_VERSION_PRE_PORT_RESET_CHANGES)
4968 pThis->fLegacyPortResetMethod = true;
4969
4970 /* Verify config. */
4971 if (uVersion > AHCI_SAVED_STATE_VERSION_VBOX_30)
4972 {
4973 rc = pHlp->pfnSSMGetU32(pSSM, &u32);
4974 AssertRCReturn(rc, rc);
4975 if (u32 != pThis->cPortsImpl)
4976 {
4977 LogRel(("AHCI: Config mismatch: cPortsImpl - saved=%u config=%u\n", u32, pThis->cPortsImpl));
4978 if ( u32 < pThis->cPortsImpl
4979 || u32 > AHCI_MAX_NR_PORTS_IMPL)
4980 return pHlp->pfnSSMSetCfgError(pSSM, RT_SRC_POS, N_("Config mismatch: cPortsImpl - saved=%u config=%u"),
4981 u32, pThis->cPortsImpl);
4982 }
4983
4984 for (uint32_t i = 0; i < AHCI_MAX_NR_PORTS_IMPL; i++)
4985 {
4986 bool fInUse;
4987 rc = pHlp->pfnSSMGetBool(pSSM, &fInUse);
4988 AssertRCReturn(rc, rc);
4989 if (fInUse != pThis->aPorts[i].fPresent)
4990 return pHlp->pfnSSMSetCfgError(pSSM, RT_SRC_POS,
4991 N_("The %s VM is missing a device on port %u. Please make sure the source and target VMs have compatible storage configurations"),
4992 fInUse ? "target" : "source", i);
4993
4994 if (uVersion > AHCI_SAVED_STATE_VERSION_PRE_HOTPLUG_FLAG)
4995 {
4996 bool fHotpluggable;
4997 rc = pHlp->pfnSSMGetBool(pSSM, &fHotpluggable);
4998 AssertRCReturn(rc, rc);
4999 if (fHotpluggable != pThis->aPorts[i].fHotpluggable)
5000 return pHlp->pfnSSMSetCfgError(pSSM, RT_SRC_POS,
5001 N_("AHCI: Port %u config mismatch: Hotplug flag - saved=%RTbool config=%RTbool\n"),
5002 i, fHotpluggable, pThis->aPorts[i].fHotpluggable);
5003 }
5004 else
5005 Assert(pThis->aPorts[i].fHotpluggable);
5006
5007 char szSerialNumber[AHCI_SERIAL_NUMBER_LENGTH+1];
5008 rc = pHlp->pfnSSMGetStrZ(pSSM, szSerialNumber, sizeof(szSerialNumber));
5009 AssertRCReturn(rc, rc);
5010 if (strcmp(szSerialNumber, pThis->aPorts[i].szSerialNumber))
5011 LogRel(("AHCI: Port %u config mismatch: Serial number - saved='%s' config='%s'\n",
5012 i, szSerialNumber, pThis->aPorts[i].szSerialNumber));
5013
5014 char szFirmwareRevision[AHCI_FIRMWARE_REVISION_LENGTH+1];
5015 rc = pHlp->pfnSSMGetStrZ(pSSM, szFirmwareRevision, sizeof(szFirmwareRevision));
5016 AssertRCReturn(rc, rc);
5017 if (strcmp(szFirmwareRevision, pThis->aPorts[i].szFirmwareRevision))
5018 LogRel(("AHCI: Port %u config mismatch: Firmware revision - saved='%s' config='%s'\n",
5019 i, szFirmwareRevision, pThis->aPorts[i].szFirmwareRevision));
5020
5021 char szModelNumber[AHCI_MODEL_NUMBER_LENGTH+1];
5022 rc = pHlp->pfnSSMGetStrZ(pSSM, szModelNumber, sizeof(szModelNumber));
5023 AssertRCReturn(rc, rc);
5024 if (strcmp(szModelNumber, pThis->aPorts[i].szModelNumber))
5025 LogRel(("AHCI: Port %u config mismatch: Model number - saved='%s' config='%s'\n",
5026 i, szModelNumber, pThis->aPorts[i].szModelNumber));
5027 }
5028
5029 static const char *s_apszIdeEmuPortNames[4] = { "PrimaryMaster", "PrimarySlave", "SecondaryMaster", "SecondarySlave" };
5030 for (uint32_t i = 0; i < RT_ELEMENTS(s_apszIdeEmuPortNames); i++)
5031 {
5032 uint32_t iPort;
5033 rc = pHlp->pfnCFGMQueryU32Def(pDevIns->pCfg, s_apszIdeEmuPortNames[i], &iPort, i);
5034 AssertRCReturn(rc, rc);
5035
5036 uint32_t iPortSaved;
5037 rc = pHlp->pfnSSMGetU32(pSSM, &iPortSaved);
5038 AssertRCReturn(rc, rc);
5039
5040 if (iPortSaved != iPort)
5041 return pHlp->pfnSSMSetCfgError(pSSM, RT_SRC_POS, N_("IDE %s config mismatch: saved=%u config=%u"),
5042 s_apszIdeEmuPortNames[i], iPortSaved, iPort);
5043 }
5044 }
5045
5046 if (uPass == SSM_PASS_FINAL)
5047 {
5048 /* Restore data. */
5049
5050 /* The main device structure. */
5051 pHlp->pfnSSMGetU32(pSSM, &pThis->regHbaCap);
5052 pHlp->pfnSSMGetU32(pSSM, &pThis->regHbaCtrl);
5053 pHlp->pfnSSMGetU32(pSSM, &pThis->regHbaIs);
5054 pHlp->pfnSSMGetU32(pSSM, &pThis->regHbaPi);
5055 pHlp->pfnSSMGetU32(pSSM, &pThis->regHbaVs);
5056 pHlp->pfnSSMGetU32(pSSM, &pThis->regHbaCccCtl);
5057 pHlp->pfnSSMGetU32(pSSM, &pThis->regHbaCccPorts);
5058 pHlp->pfnSSMGetU8(pSSM, &pThis->uCccPortNr);
5059 pHlp->pfnSSMGetU64(pSSM, &pThis->uCccTimeout);
5060 pHlp->pfnSSMGetU32(pSSM, &pThis->uCccNr);
5061 pHlp->pfnSSMGetU32(pSSM, &pThis->uCccCurrentNr);
5062
5063 pHlp->pfnSSMGetU32V(pSSM, &pThis->u32PortsInterrupted);
5064 pHlp->pfnSSMGetBool(pSSM, &pThis->fReset);
5065 pHlp->pfnSSMGetBool(pSSM, &pThis->f64BitAddr);
5066 bool fIgn;
5067 pHlp->pfnSSMGetBool(pSSM, &fIgn); /* Was fR0Enabled, which should never have been saved! */
5068 pHlp->pfnSSMGetBool(pSSM, &fIgn); /* Was fGCEnabled, which should never have been saved! */
5069 if (uVersion > AHCI_SAVED_STATE_VERSION_PRE_PORT_RESET_CHANGES)
5070 pHlp->pfnSSMGetBool(pSSM, &pThis->fLegacyPortResetMethod);
5071
5072 /* Now every port. */
5073 for (uint32_t i = 0; i < AHCI_MAX_NR_PORTS_IMPL; i++)
5074 {
5075 PAHCIPORT pAhciPort = &pThis->aPorts[i];
5076
5077 pHlp->pfnSSMGetU32(pSSM, &pThis->aPorts[i].regCLB);
5078 pHlp->pfnSSMGetU32(pSSM, &pThis->aPorts[i].regCLBU);
5079 pHlp->pfnSSMGetU32(pSSM, &pThis->aPorts[i].regFB);
5080 pHlp->pfnSSMGetU32(pSSM, &pThis->aPorts[i].regFBU);
5081 pHlp->pfnSSMGetGCPhysV(pSSM, &pThis->aPorts[i].GCPhysAddrClb);
5082 pHlp->pfnSSMGetGCPhysV(pSSM, &pThis->aPorts[i].GCPhysAddrFb);
5083 pHlp->pfnSSMGetU32V(pSSM, &pThis->aPorts[i].regIS);
5084 pHlp->pfnSSMGetU32(pSSM, &pThis->aPorts[i].regIE);
5085 pHlp->pfnSSMGetU32(pSSM, &pThis->aPorts[i].regCMD);
5086 pHlp->pfnSSMGetU32(pSSM, &pThis->aPorts[i].regTFD);
5087 pHlp->pfnSSMGetU32(pSSM, &pThis->aPorts[i].regSIG);
5088 pHlp->pfnSSMGetU32(pSSM, &pThis->aPorts[i].regSSTS);
5089 pHlp->pfnSSMGetU32(pSSM, &pThis->aPorts[i].regSCTL);
5090 pHlp->pfnSSMGetU32(pSSM, &pThis->aPorts[i].regSERR);
5091 pHlp->pfnSSMGetU32V(pSSM, &pThis->aPorts[i].regSACT);
5092 pHlp->pfnSSMGetU32V(pSSM, &pThis->aPorts[i].regCI);
5093 pHlp->pfnSSMGetU32(pSSM, &pThis->aPorts[i].PCHSGeometry.cCylinders);
5094 pHlp->pfnSSMGetU32(pSSM, &pThis->aPorts[i].PCHSGeometry.cHeads);
5095 pHlp->pfnSSMGetU32(pSSM, &pThis->aPorts[i].PCHSGeometry.cSectors);
5096 pHlp->pfnSSMGetU64(pSSM, &pThis->aPorts[i].cTotalSectors);
5097 pHlp->pfnSSMGetU32(pSSM, &pThis->aPorts[i].cMultSectors);
5098 pHlp->pfnSSMGetU8(pSSM, &pThis->aPorts[i].uATATransferMode);
5099 pHlp->pfnSSMGetBool(pSSM, &pThis->aPorts[i].fResetDevice);
5100
5101 if (uVersion <= AHCI_SAVED_STATE_VERSION_VBOX_30)
5102 pHlp->pfnSSMSkip(pSSM, AHCI_NR_COMMAND_SLOTS * sizeof(uint8_t)); /* no active data here */
5103
5104 if (uVersion < AHCI_SAVED_STATE_VERSION_IDE_EMULATION)
5105 {
5106 /* The old positions in the FIFO, not required. */
5107 pHlp->pfnSSMSkip(pSSM, 2*sizeof(uint8_t));
5108 }
5109 pHlp->pfnSSMGetBool(pSSM, &pThis->aPorts[i].fPoweredOn);
5110 pHlp->pfnSSMGetBool(pSSM, &pThis->aPorts[i].fSpunUp);
5111 pHlp->pfnSSMGetU32V(pSSM, &pThis->aPorts[i].u32TasksFinished);
5112 pHlp->pfnSSMGetU32V(pSSM, &pThis->aPorts[i].u32QueuedTasksFinished);
5113
5114 if (uVersion >= AHCI_SAVED_STATE_VERSION_IDE_EMULATION)
5115 pHlp->pfnSSMGetU32V(pSSM, &pThis->aPorts[i].u32CurrentCommandSlot);
5116
5117 if (uVersion > AHCI_SAVED_STATE_VERSION_PRE_ATAPI)
5118 {
5119 pHlp->pfnSSMGetBool(pSSM, &pThis->aPorts[i].fATAPI);
5120 pHlp->pfnSSMGetMem(pSSM, pThis->aPorts[i].abATAPISense, sizeof(pThis->aPorts[i].abATAPISense));
5121 if (uVersion <= AHCI_SAVED_STATE_VERSION_PRE_ATAPI_REMOVE)
5122 {
5123 pHlp->pfnSSMSkip(pSSM, 1); /* cNotifiedMediaChange. */
5124 pHlp->pfnSSMSkip(pSSM, 4); /* MediaEventStatus */
5125 }
5126 }
5127 else if (pThis->aPorts[i].fATAPI)
5128 return pHlp->pfnSSMSetCfgError(pSSM, RT_SRC_POS, N_("Config mismatch: atapi - saved=false config=true"));
5129
5130 /* Check if we have tasks pending. */
5131 uint32_t fTasksOutstanding = pAhciPort->regCI & ~pAhciPort->u32TasksFinished;
5132 uint32_t fQueuedTasksOutstanding = pAhciPort->regSACT & ~pAhciPort->u32QueuedTasksFinished;
5133
5134 pAhciPort->u32TasksNew = fTasksOutstanding | fQueuedTasksOutstanding;
5135
5136 if (pAhciPort->u32TasksNew)
5137 {
5138 /*
5139 * There are tasks pending. The VM was saved after a task failed
5140 * because of non-fatal error. Set the redo flag.
5141 */
5142 pAhciPort->fRedo = true;
5143 }
5144 }
5145
5146 if (uVersion <= AHCI_SAVED_STATE_VERSION_IDE_EMULATION)
5147 {
5148 for (uint32_t i = 0; i < 2; i++)
5149 {
5150 rc = ahciR3LoadLegacyEmulationState(pHlp, pSSM);
5151 if(RT_FAILURE(rc))
5152 return rc;
5153 }
5154 }
5155
5156 rc = pHlp->pfnSSMGetU32(pSSM, &u32);
5157 if (RT_FAILURE(rc))
5158 return rc;
5159 AssertMsgReturn(u32 == UINT32_MAX, ("%#x\n", u32), VERR_SSM_DATA_UNIT_FORMAT_CHANGED);
5160 }
5161
5162 return VINF_SUCCESS;
5163}
5164
5165/* -=-=-=-=- device PDM interface -=-=-=-=- */
5166
5167/**
5168 * Configure the attached device for a port.
5169 *
5170 * Used by ahciR3Construct and ahciR3Attach.
5171 *
5172 * @returns VBox status code
5173 * @param pDevIns The device instance data.
5174 * @param pAhciPort The port for which the device is to be configured, shared bits.
5175 * @param pAhciPortR3 The port for which the device is to be configured, ring-3 bits.
5176 */
5177static int ahciR3ConfigureLUN(PPDMDEVINS pDevIns, PAHCIPORT pAhciPort, PAHCIPORTR3 pAhciPortR3)
5178{
5179 /* Query the media interface. */
5180 pAhciPortR3->pDrvMedia = PDMIBASE_QUERY_INTERFACE(pAhciPortR3->pDrvBase, PDMIMEDIA);
5181 AssertMsgReturn(VALID_PTR(pAhciPortR3->pDrvMedia),
5182 ("AHCI configuration error: LUN#%d misses the basic media interface!\n", pAhciPort->iLUN),
5183 VERR_PDM_MISSING_INTERFACE);
5184
5185 /* Get the extended media interface. */
5186 pAhciPortR3->pDrvMediaEx = PDMIBASE_QUERY_INTERFACE(pAhciPortR3->pDrvBase, PDMIMEDIAEX);
5187 AssertMsgReturn(VALID_PTR(pAhciPortR3->pDrvMediaEx),
5188 ("AHCI configuration error: LUN#%d misses the extended media interface!\n", pAhciPort->iLUN),
5189 VERR_PDM_MISSING_INTERFACE);
5190
5191 /*
5192 * Validate type.
5193 */
5194 PDMMEDIATYPE enmType = pAhciPortR3->pDrvMedia->pfnGetType(pAhciPortR3->pDrvMedia);
5195 AssertMsgReturn(enmType == PDMMEDIATYPE_HARD_DISK || enmType == PDMMEDIATYPE_CDROM || enmType == PDMMEDIATYPE_DVD,
5196 ("AHCI configuration error: LUN#%d isn't a disk or cd/dvd. enmType=%u\n", pAhciPort->iLUN, enmType),
5197 VERR_PDM_UNSUPPORTED_BLOCK_TYPE);
5198
5199 int rc = pAhciPortR3->pDrvMediaEx->pfnIoReqAllocSizeSet(pAhciPortR3->pDrvMediaEx, sizeof(AHCIREQ));
5200 if (RT_FAILURE(rc))
5201 return PDMDevHlpVMSetError(pDevIns, rc, RT_SRC_POS,
5202 N_("AHCI configuration error: LUN#%u: Failed to set I/O request size!"),
5203 pAhciPort->iLUN);
5204
5205 uint32_t fFeatures = 0;
5206 rc = pAhciPortR3->pDrvMediaEx->pfnQueryFeatures(pAhciPortR3->pDrvMediaEx, &fFeatures);
5207 if (RT_FAILURE(rc))
5208 return PDMDevHlpVMSetError(pDevIns, rc, RT_SRC_POS,
5209 N_("AHCI configuration error: LUN#%u: Failed to query features of device"),
5210 pAhciPort->iLUN);
5211
5212 if (fFeatures & PDMIMEDIAEX_FEATURE_F_DISCARD)
5213 pAhciPort->fTrimEnabled = true;
5214
5215 pAhciPort->fPresent = true;
5216
5217 pAhciPort->fATAPI = (enmType == PDMMEDIATYPE_CDROM || enmType == PDMMEDIATYPE_DVD)
5218 && RT_BOOL(fFeatures & PDMIMEDIAEX_FEATURE_F_RAWSCSICMD);
5219 if (pAhciPort->fATAPI)
5220 {
5221 pAhciPort->PCHSGeometry.cCylinders = 0;
5222 pAhciPort->PCHSGeometry.cHeads = 0;
5223 pAhciPort->PCHSGeometry.cSectors = 0;
5224 LogRel(("AHCI: LUN#%d: CD/DVD\n", pAhciPort->iLUN));
5225 }
5226 else
5227 {
5228 pAhciPort->cbSector = pAhciPortR3->pDrvMedia->pfnGetSectorSize(pAhciPortR3->pDrvMedia);
5229 pAhciPort->cTotalSectors = pAhciPortR3->pDrvMedia->pfnGetSize(pAhciPortR3->pDrvMedia) / pAhciPort->cbSector;
5230 rc = pAhciPortR3->pDrvMedia->pfnBiosGetPCHSGeometry(pAhciPortR3->pDrvMedia, &pAhciPort->PCHSGeometry);
5231 if (rc == VERR_PDM_MEDIA_NOT_MOUNTED)
5232 {
5233 pAhciPort->PCHSGeometry.cCylinders = 0;
5234 pAhciPort->PCHSGeometry.cHeads = 16; /*??*/
5235 pAhciPort->PCHSGeometry.cSectors = 63; /*??*/
5236 }
5237 else if (rc == VERR_PDM_GEOMETRY_NOT_SET)
5238 {
5239 pAhciPort->PCHSGeometry.cCylinders = 0; /* autodetect marker */
5240 rc = VINF_SUCCESS;
5241 }
5242 AssertRC(rc);
5243
5244 if ( pAhciPort->PCHSGeometry.cCylinders == 0
5245 || pAhciPort->PCHSGeometry.cHeads == 0
5246 || pAhciPort->PCHSGeometry.cSectors == 0)
5247 {
5248 uint64_t cCylinders = pAhciPort->cTotalSectors / (16 * 63);
5249 pAhciPort->PCHSGeometry.cCylinders = RT_MAX(RT_MIN(cCylinders, 16383), 1);
5250 pAhciPort->PCHSGeometry.cHeads = 16;
5251 pAhciPort->PCHSGeometry.cSectors = 63;
5252 /* Set the disk geometry information. Ignore errors. */
5253 pAhciPortR3->pDrvMedia->pfnBiosSetPCHSGeometry(pAhciPortR3->pDrvMedia, &pAhciPort->PCHSGeometry);
5254 rc = VINF_SUCCESS;
5255 }
5256 LogRel(("AHCI: LUN#%d: disk, PCHS=%u/%u/%u, total number of sectors %Ld\n",
5257 pAhciPort->iLUN, pAhciPort->PCHSGeometry.cCylinders,
5258 pAhciPort->PCHSGeometry.cHeads, pAhciPort->PCHSGeometry.cSectors,
5259 pAhciPort->cTotalSectors));
5260 if (pAhciPort->fTrimEnabled)
5261 LogRel(("AHCI: LUN#%d: Enabled TRIM support\n", pAhciPort->iLUN));
5262 }
5263 return rc;
5264}
5265
5266/**
5267 * Callback employed by ahciR3Suspend and ahciR3PowerOff.
5268 *
5269 * @returns true if we've quiesced, false if we're still working.
5270 * @param pDevIns The device instance.
5271 */
5272static DECLCALLBACK(bool) ahciR3IsAsyncSuspendOrPowerOffDone(PPDMDEVINS pDevIns)
5273{
5274 if (!ahciR3AllAsyncIOIsFinished(pDevIns))
5275 return false;
5276
5277 PAHCIR3 pThisCC = PDMDEVINS_2_DATA_CC(pDevIns, PAHCICC);
5278 ASMAtomicWriteBool(&pThisCC->fSignalIdle, false);
5279 return true;
5280}
5281
5282/**
5283 * Common worker for ahciR3Suspend and ahciR3PowerOff.
5284 */
5285static void ahciR3SuspendOrPowerOff(PPDMDEVINS pDevIns)
5286{
5287 PAHCIR3 pThisCC = PDMDEVINS_2_DATA_CC(pDevIns, PAHCICC);
5288
5289 ASMAtomicWriteBool(&pThisCC->fSignalIdle, true);
5290 if (!ahciR3AllAsyncIOIsFinished(pDevIns))
5291 PDMDevHlpSetAsyncNotification(pDevIns, ahciR3IsAsyncSuspendOrPowerOffDone);
5292 else
5293 ASMAtomicWriteBool(&pThisCC->fSignalIdle, false);
5294
5295 for (uint32_t i = 0; i < RT_ELEMENTS(pThisCC->aPorts); i++)
5296 {
5297 PAHCIPORTR3 pThisPort = &pThisCC->aPorts[i];
5298 if (pThisPort->pDrvMediaEx)
5299 pThisPort->pDrvMediaEx->pfnNotifySuspend(pThisPort->pDrvMediaEx);
5300 }
5301}
5302
5303/**
5304 * Suspend notification.
5305 *
5306 * @param pDevIns The device instance data.
5307 */
5308static DECLCALLBACK(void) ahciR3Suspend(PPDMDEVINS pDevIns)
5309{
5310 Log(("ahciR3Suspend\n"));
5311 ahciR3SuspendOrPowerOff(pDevIns);
5312}
5313
5314/**
5315 * Resume notification.
5316 *
5317 * @param pDevIns The device instance data.
5318 */
5319static DECLCALLBACK(void) ahciR3Resume(PPDMDEVINS pDevIns)
5320{
5321 PAHCI pThis = PDMDEVINS_2_DATA(pDevIns, PAHCI);
5322
5323 /*
5324 * Check if one of the ports has pending tasks.
5325 * Queue a notification item again in this case.
5326 */
5327 for (unsigned i = 0; i < RT_ELEMENTS(pThis->aPorts); i++)
5328 {
5329 PAHCIPORT pAhciPort = &pThis->aPorts[i];
5330
5331 if (pAhciPort->u32TasksRedo)
5332 {
5333 pAhciPort->u32TasksNew |= pAhciPort->u32TasksRedo;
5334 pAhciPort->u32TasksRedo = 0;
5335
5336 Assert(pAhciPort->fRedo);
5337 pAhciPort->fRedo = false;
5338
5339 /* Notify the async IO thread. */
5340 int rc = PDMDevHlpSUPSemEventSignal(pDevIns, pAhciPort->hEvtProcess);
5341 AssertRC(rc);
5342 }
5343 }
5344
5345 Log(("%s:\n", __FUNCTION__));
5346}
5347
5348/**
5349 * Initializes the VPD data of a attached device.
5350 *
5351 * @returns VBox status code.
5352 * @param pDevIns The device instance.
5353 * @param pAhciPort The attached device, shared bits.
5354 * @param pAhciPortR3 The attached device, ring-3 bits.
5355 * @param pszName Name of the port to get the CFGM node.
5356 */
5357static int ahciR3VpdInit(PPDMDEVINS pDevIns, PAHCIPORT pAhciPort, PAHCIPORTR3 pAhciPortR3, const char *pszName)
5358{
5359 PCPDMDEVHLPR3 pHlp = pDevIns->pHlpR3;
5360
5361 /* Generate a default serial number. */
5362 char szSerial[AHCI_SERIAL_NUMBER_LENGTH+1];
5363 RTUUID Uuid;
5364
5365 int rc = VINF_SUCCESS;
5366 if (pAhciPortR3->pDrvMedia)
5367 rc = pAhciPortR3->pDrvMedia->pfnGetUuid(pAhciPortR3->pDrvMedia, &Uuid);
5368 else
5369 RTUuidClear(&Uuid);
5370
5371 if (RT_FAILURE(rc) || RTUuidIsNull(&Uuid))
5372 {
5373 /* Generate a predictable serial for drives which don't have a UUID. */
5374 RTStrPrintf(szSerial, sizeof(szSerial), "VB%x-1a2b3c4d", pAhciPort->iLUN);
5375 }
5376 else
5377 RTStrPrintf(szSerial, sizeof(szSerial), "VB%08x-%08x", Uuid.au32[0], Uuid.au32[3]);
5378
5379 /* Get user config if present using defaults otherwise. */
5380 PCFGMNODE pCfgNode = pHlp->pfnCFGMGetChild(pDevIns->pCfg, pszName);
5381 rc = pHlp->pfnCFGMQueryStringDef(pCfgNode, "SerialNumber", pAhciPort->szSerialNumber,
5382 sizeof(pAhciPort->szSerialNumber), szSerial);
5383 if (RT_FAILURE(rc))
5384 {
5385 if (rc == VERR_CFGM_NOT_ENOUGH_SPACE)
5386 return PDMDEV_SET_ERROR(pDevIns, VERR_INVALID_PARAMETER,
5387 N_("AHCI configuration error: \"SerialNumber\" is longer than 20 bytes"));
5388 return PDMDEV_SET_ERROR(pDevIns, rc, N_("AHCI configuration error: failed to read \"SerialNumber\" as string"));
5389 }
5390
5391 rc = pHlp->pfnCFGMQueryStringDef(pCfgNode, "FirmwareRevision", pAhciPort->szFirmwareRevision,
5392 sizeof(pAhciPort->szFirmwareRevision), "1.0");
5393 if (RT_FAILURE(rc))
5394 {
5395 if (rc == VERR_CFGM_NOT_ENOUGH_SPACE)
5396 return PDMDEV_SET_ERROR(pDevIns, VERR_INVALID_PARAMETER,
5397 N_("AHCI configuration error: \"FirmwareRevision\" is longer than 8 bytes"));
5398 return PDMDEV_SET_ERROR(pDevIns, rc, N_("AHCI configuration error: failed to read \"FirmwareRevision\" as string"));
5399 }
5400
5401 rc = pHlp->pfnCFGMQueryStringDef(pCfgNode, "ModelNumber", pAhciPort->szModelNumber, sizeof(pAhciPort->szModelNumber),
5402 pAhciPort->fATAPI ? "VBOX CD-ROM" : "VBOX HARDDISK");
5403 if (RT_FAILURE(rc))
5404 {
5405 if (rc == VERR_CFGM_NOT_ENOUGH_SPACE)
5406 return PDMDEV_SET_ERROR(pDevIns, VERR_INVALID_PARAMETER,
5407 N_("AHCI configuration error: \"ModelNumber\" is longer than 40 bytes"));
5408 return PDMDEV_SET_ERROR(pDevIns, rc, N_("AHCI configuration error: failed to read \"ModelNumber\" as string"));
5409 }
5410
5411 rc = pHlp->pfnCFGMQueryU8Def(pCfgNode, "LogicalSectorsPerPhysical", &pAhciPort->cLogSectorsPerPhysicalExp, 0);
5412 if (RT_FAILURE(rc))
5413 return PDMDEV_SET_ERROR(pDevIns, rc,
5414 N_("AHCI configuration error: failed to read \"LogicalSectorsPerPhysical\" as integer"));
5415 if (pAhciPort->cLogSectorsPerPhysicalExp >= 16)
5416 return PDMDEV_SET_ERROR(pDevIns, rc,
5417 N_("AHCI configuration error: \"LogicalSectorsPerPhysical\" must be between 0 and 15"));
5418
5419 /* There are three other identification strings for CD drives used for INQUIRY */
5420 if (pAhciPort->fATAPI)
5421 {
5422 rc = pHlp->pfnCFGMQueryStringDef(pCfgNode, "ATAPIVendorId", pAhciPort->szInquiryVendorId,
5423 sizeof(pAhciPort->szInquiryVendorId), "VBOX");
5424 if (RT_FAILURE(rc))
5425 {
5426 if (rc == VERR_CFGM_NOT_ENOUGH_SPACE)
5427 return PDMDEV_SET_ERROR(pDevIns, VERR_INVALID_PARAMETER,
5428 N_("AHCI configuration error: \"ATAPIVendorId\" is longer than 16 bytes"));
5429 return PDMDEV_SET_ERROR(pDevIns, rc, N_("AHCI configuration error: failed to read \"ATAPIVendorId\" as string"));
5430 }
5431
5432 rc = pHlp->pfnCFGMQueryStringDef(pCfgNode, "ATAPIProductId", pAhciPort->szInquiryProductId,
5433 sizeof(pAhciPort->szInquiryProductId), "CD-ROM");
5434 if (RT_FAILURE(rc))
5435 {
5436 if (rc == VERR_CFGM_NOT_ENOUGH_SPACE)
5437 return PDMDEV_SET_ERROR(pDevIns, VERR_INVALID_PARAMETER,
5438 N_("AHCI configuration error: \"ATAPIProductId\" is longer than 16 bytes"));
5439 return PDMDEV_SET_ERROR(pDevIns, rc, N_("AHCI configuration error: failed to read \"ATAPIProductId\" as string"));
5440 }
5441
5442 rc = pHlp->pfnCFGMQueryStringDef(pCfgNode, "ATAPIRevision", pAhciPort->szInquiryRevision,
5443 sizeof(pAhciPort->szInquiryRevision), "1.0");
5444 if (RT_FAILURE(rc))
5445 {
5446 if (rc == VERR_CFGM_NOT_ENOUGH_SPACE)
5447 return PDMDEV_SET_ERROR(pDevIns, VERR_INVALID_PARAMETER,
5448 N_("AHCI configuration error: \"ATAPIRevision\" is longer than 4 bytes"));
5449 return PDMDEV_SET_ERROR(pDevIns, rc, N_("AHCI configuration error: failed to read \"ATAPIRevision\" as string"));
5450 }
5451 }
5452
5453 return rc;
5454}
5455
5456
5457/**
5458 * Detach notification.
5459 *
5460 * One harddisk at one port has been unplugged.
5461 * The VM is suspended at this point.
5462 *
5463 * @param pDevIns The device instance.
5464 * @param iLUN The logical unit which is being detached.
5465 * @param fFlags Flags, combination of the PDMDEVATT_FLAGS_* \#defines.
5466 */
5467static DECLCALLBACK(void) ahciR3Detach(PPDMDEVINS pDevIns, unsigned iLUN, uint32_t fFlags)
5468{
5469 PAHCI pThis = PDMDEVINS_2_DATA(pDevIns, PAHCI);
5470 PAHCIR3 pThisCC = PDMDEVINS_2_DATA_CC(pDevIns, PAHCICC);
5471 int rc = VINF_SUCCESS;
5472
5473 Log(("%s:\n", __FUNCTION__));
5474
5475 AssertMsgReturnVoid(iLUN < RT_MIN(pThis->cPortsImpl, RT_ELEMENTS(pThisCC->aPorts)), ("iLUN=%u", iLUN));
5476 PAHCIPORT pAhciPort = &pThis->aPorts[iLUN];
5477 PAHCIPORTR3 pAhciPortR3 = &pThisCC->aPorts[iLUN];
5478 AssertMsgReturnVoid( pAhciPort->fHotpluggable
5479 || (fFlags & PDM_TACH_FLAGS_NOT_HOT_PLUG),
5480 ("AHCI: Port %d is not marked hotpluggable\n", pAhciPort->iLUN));
5481
5482
5483 if (pAhciPortR3->pAsyncIOThread)
5484 {
5485 int rcThread;
5486 /* Destroy the thread. */
5487 rc = PDMDevHlpThreadDestroy(pDevIns, pAhciPortR3->pAsyncIOThread, &rcThread);
5488 if (RT_FAILURE(rc) || RT_FAILURE(rcThread))
5489 AssertMsgFailed(("%s Failed to destroy async IO thread rc=%Rrc rcThread=%Rrc\n", __FUNCTION__, rc, rcThread));
5490
5491 pAhciPortR3->pAsyncIOThread = NULL;
5492 pAhciPort->fWrkThreadSleeping = true;
5493 }
5494
5495 if (!(fFlags & PDM_TACH_FLAGS_NOT_HOT_PLUG))
5496 {
5497 /*
5498 * Inform the guest about the removed device.
5499 */
5500 pAhciPort->regSSTS = 0;
5501 pAhciPort->regSIG = 0;
5502 /*
5503 * Clear CR bit too to prevent submission of new commands when CI is written
5504 * (AHCI Spec 1.2: 7.4 Interaction of the Command List and Port Change Status).
5505 */
5506 ASMAtomicAndU32(&pAhciPort->regCMD, ~(AHCI_PORT_CMD_CPS | AHCI_PORT_CMD_CR));
5507 ASMAtomicOrU32(&pAhciPort->regIS, AHCI_PORT_IS_CPDS | AHCI_PORT_IS_PRCS | AHCI_PORT_IS_PCS);
5508 ASMAtomicOrU32(&pAhciPort->regSERR, AHCI_PORT_SERR_X | AHCI_PORT_SERR_N);
5509 if (pAhciPort->regIE & (AHCI_PORT_IE_CPDE | AHCI_PORT_IE_PCE | AHCI_PORT_IE_PRCE))
5510 ahciHbaSetInterrupt(pDevIns, pThis, pAhciPort->iLUN, VERR_IGNORED);
5511 }
5512
5513 /*
5514 * Zero some important members.
5515 */
5516 pAhciPortR3->pDrvBase = NULL;
5517 pAhciPortR3->pDrvMedia = NULL;
5518 pAhciPortR3->pDrvMediaEx = NULL;
5519 pAhciPort->fPresent = false;
5520}
5521
5522/**
5523 * Attach command.
5524 *
5525 * This is called when we change block driver for one port.
5526 * The VM is suspended at this point.
5527 *
5528 * @returns VBox status code.
5529 * @param pDevIns The device instance.
5530 * @param iLUN The logical unit which is being detached.
5531 * @param fFlags Flags, combination of the PDMDEVATT_FLAGS_* \#defines.
5532 */
5533static DECLCALLBACK(int) ahciR3Attach(PPDMDEVINS pDevIns, unsigned iLUN, uint32_t fFlags)
5534{
5535 PAHCI pThis = PDMDEVINS_2_DATA(pDevIns, PAHCI);
5536 PAHCIR3 pThisCC = PDMDEVINS_2_DATA_CC(pDevIns, PAHCICC);
5537 int rc;
5538
5539 Log(("%s:\n", __FUNCTION__));
5540
5541 /* the usual paranoia */
5542 AssertMsgReturn(iLUN < RT_MIN(pThis->cPortsImpl, RT_ELEMENTS(pThisCC->aPorts)), ("iLUN=%u", iLUN), VERR_PDM_LUN_NOT_FOUND);
5543 PAHCIPORT pAhciPort = &pThis->aPorts[iLUN];
5544 PAHCIPORTR3 pAhciPortR3 = &pThisCC->aPorts[iLUN];
5545 AssertRelease(!pAhciPortR3->pDrvBase);
5546 AssertRelease(!pAhciPortR3->pDrvMedia);
5547 AssertRelease(!pAhciPortR3->pDrvMediaEx);
5548 Assert(pAhciPort->iLUN == iLUN);
5549 Assert(pAhciPortR3->iLUN == iLUN);
5550
5551 AssertMsgReturn( pAhciPort->fHotpluggable
5552 || (fFlags & PDM_TACH_FLAGS_NOT_HOT_PLUG),
5553 ("AHCI: Port %d is not marked hotpluggable\n", pAhciPort->iLUN),
5554 VERR_INVALID_PARAMETER);
5555
5556 /*
5557 * Try attach the block device and get the interfaces,
5558 * required as well as optional.
5559 */
5560 rc = PDMDevHlpDriverAttach(pDevIns, pAhciPort->iLUN, &pAhciPortR3->IBase, &pAhciPortR3->pDrvBase, pAhciPortR3->szDesc);
5561 if (RT_SUCCESS(rc))
5562 rc = ahciR3ConfigureLUN(pDevIns, pAhciPort, pAhciPortR3);
5563 else
5564 AssertMsgFailed(("Failed to attach LUN#%d. rc=%Rrc\n", pAhciPort->iLUN, rc));
5565
5566 if (RT_FAILURE(rc))
5567 {
5568 pAhciPortR3->pDrvBase = NULL;
5569 pAhciPortR3->pDrvMedia = NULL;
5570 pAhciPortR3->pDrvMediaEx = NULL;
5571 pAhciPort->fPresent = false;
5572 }
5573 else
5574 {
5575 rc = PDMDevHlpSUPSemEventCreate(pDevIns, &pAhciPort->hEvtProcess);
5576 if (RT_FAILURE(rc))
5577 return PDMDevHlpVMSetError(pDevIns, rc, RT_SRC_POS,
5578 N_("AHCI: Failed to create SUP event semaphore"));
5579
5580 /* Create the async IO thread. */
5581 rc = PDMDevHlpThreadCreate(pDevIns, &pAhciPortR3->pAsyncIOThread, pAhciPortR3, ahciAsyncIOLoop,
5582 ahciAsyncIOLoopWakeUp, 0, RTTHREADTYPE_IO, pAhciPortR3->szDesc);
5583 if (RT_FAILURE(rc))
5584 return rc;
5585
5586 /*
5587 * Init vendor product data.
5588 */
5589 if (RT_SUCCESS(rc))
5590 rc = ahciR3VpdInit(pDevIns, pAhciPort, pAhciPortR3, pAhciPortR3->szDesc);
5591
5592 /* Inform the guest about the added device in case of hotplugging. */
5593 if ( RT_SUCCESS(rc)
5594 && !(fFlags & PDM_TACH_FLAGS_NOT_HOT_PLUG))
5595 {
5596 AssertMsgReturn(pAhciPort->fHotpluggable,
5597 ("AHCI: Port %d is not marked hotpluggable\n", pAhciPort->iLUN),
5598 VERR_NOT_SUPPORTED);
5599
5600 /*
5601 * Initialize registers
5602 */
5603 ASMAtomicOrU32(&pAhciPort->regCMD, AHCI_PORT_CMD_CPS);
5604 ASMAtomicOrU32(&pAhciPort->regIS, AHCI_PORT_IS_CPDS | AHCI_PORT_IS_PRCS | AHCI_PORT_IS_PCS);
5605 ASMAtomicOrU32(&pAhciPort->regSERR, AHCI_PORT_SERR_X | AHCI_PORT_SERR_N);
5606
5607 if (pAhciPort->fATAPI)
5608 pAhciPort->regSIG = AHCI_PORT_SIG_ATAPI;
5609 else
5610 pAhciPort->regSIG = AHCI_PORT_SIG_DISK;
5611 pAhciPort->regSSTS = (0x01 << 8) | /* Interface is active. */
5612 (0x02 << 4) | /* Generation 2 (3.0GBps) speed. */
5613 (0x03 << 0); /* Device detected and communication established. */
5614
5615 if ( (pAhciPort->regIE & AHCI_PORT_IE_CPDE)
5616 || (pAhciPort->regIE & AHCI_PORT_IE_PCE)
5617 || (pAhciPort->regIE & AHCI_PORT_IE_PRCE))
5618 ahciHbaSetInterrupt(pDevIns, pThis, pAhciPort->iLUN, VERR_IGNORED);
5619 }
5620
5621 }
5622
5623 return rc;
5624}
5625
5626/**
5627 * Common reset worker.
5628 *
5629 * @param pDevIns The device instance data.
5630 */
5631static int ahciR3ResetCommon(PPDMDEVINS pDevIns)
5632{
5633 PAHCI pThis = PDMDEVINS_2_DATA(pDevIns, PAHCI);
5634 PAHCIR3 pThisCC = PDMDEVINS_2_DATA_CC(pDevIns, PAHCICC);
5635 ahciR3HBAReset(pDevIns, pThis, pThisCC);
5636
5637 /* Hardware reset for the ports. */
5638 for (uint32_t i = 0; i < RT_ELEMENTS(pThis->aPorts); i++)
5639 ahciPortHwReset(&pThis->aPorts[i]);
5640 return VINF_SUCCESS;
5641}
5642
5643/**
5644 * Callback employed by ahciR3Reset.
5645 *
5646 * @returns true if we've quiesced, false if we're still working.
5647 * @param pDevIns The device instance.
5648 */
5649static DECLCALLBACK(bool) ahciR3IsAsyncResetDone(PPDMDEVINS pDevIns)
5650{
5651 PAHCIR3 pThisCC = PDMDEVINS_2_DATA_CC(pDevIns, PAHCICC);
5652
5653 if (!ahciR3AllAsyncIOIsFinished(pDevIns))
5654 return false;
5655 ASMAtomicWriteBool(&pThisCC->fSignalIdle, false);
5656
5657 ahciR3ResetCommon(pDevIns);
5658 return true;
5659}
5660
5661/**
5662 * Reset notification.
5663 *
5664 * @param pDevIns The device instance data.
5665 */
5666static DECLCALLBACK(void) ahciR3Reset(PPDMDEVINS pDevIns)
5667{
5668 PAHCIR3 pThisCC = PDMDEVINS_2_DATA_CC(pDevIns, PAHCICC);
5669
5670 ASMAtomicWriteBool(&pThisCC->fSignalIdle, true);
5671 if (!ahciR3AllAsyncIOIsFinished(pDevIns))
5672 PDMDevHlpSetAsyncNotification(pDevIns, ahciR3IsAsyncResetDone);
5673 else
5674 {
5675 ASMAtomicWriteBool(&pThisCC->fSignalIdle, false);
5676 ahciR3ResetCommon(pDevIns);
5677 }
5678}
5679
5680/**
5681 * Poweroff notification.
5682 *
5683 * @param pDevIns Pointer to the device instance
5684 */
5685static DECLCALLBACK(void) ahciR3PowerOff(PPDMDEVINS pDevIns)
5686{
5687 Log(("achiR3PowerOff\n"));
5688 ahciR3SuspendOrPowerOff(pDevIns);
5689}
5690
5691/**
5692 * Destroy a driver instance.
5693 *
5694 * Most VM resources are freed by the VM. This callback is provided so that any non-VM
5695 * resources can be freed correctly.
5696 *
5697 * @param pDevIns The device instance data.
5698 */
5699static DECLCALLBACK(int) ahciR3Destruct(PPDMDEVINS pDevIns)
5700{
5701 PDMDEV_CHECK_VERSIONS_RETURN_QUIET(pDevIns);
5702 PAHCI pThis = PDMDEVINS_2_DATA(pDevIns, PAHCI);
5703 int rc = VINF_SUCCESS;
5704
5705 /*
5706 * At this point the async I/O thread is suspended and will not enter
5707 * this module again. So, no coordination is needed here and PDM
5708 * will take care of terminating and cleaning up the thread.
5709 */
5710 if (PDMDevHlpCritSectIsInitialized(pDevIns, &pThis->lock))
5711 {
5712 PDMDevHlpTimerDestroy(pDevIns, pThis->hHbaCccTimer);
5713 pThis->hHbaCccTimer = NIL_TMTIMERHANDLE;
5714
5715 Log(("%s: Destruct every port\n", __FUNCTION__));
5716 uint32_t const cPortsImpl = RT_MIN(pThis->cPortsImpl, RT_ELEMENTS(pThis->aPorts));
5717 for (unsigned iActPort = 0; iActPort < cPortsImpl; iActPort++)
5718 {
5719 PAHCIPORT pAhciPort = &pThis->aPorts[iActPort];
5720
5721 if (pAhciPort->hEvtProcess != NIL_SUPSEMEVENT)
5722 {
5723 PDMDevHlpSUPSemEventClose(pDevIns, pAhciPort->hEvtProcess);
5724 pAhciPort->hEvtProcess = NIL_SUPSEMEVENT;
5725 }
5726 }
5727
5728 PDMDevHlpCritSectDelete(pDevIns, &pThis->lock);
5729 }
5730
5731 return rc;
5732}
5733
5734/**
5735 * @interface_method_impl{PDMDEVREG,pfnConstruct}
5736 */
5737static DECLCALLBACK(int) ahciR3Construct(PPDMDEVINS pDevIns, int iInstance, PCFGMNODE pCfg)
5738{
5739 PDMDEV_CHECK_VERSIONS_RETURN(pDevIns);
5740 PAHCI pThis = PDMDEVINS_2_DATA(pDevIns, PAHCI);
5741 PAHCIR3 pThisCC = PDMDEVINS_2_DATA_CC(pDevIns, PAHCICC);
5742 PCPDMDEVHLPR3 pHlp = pDevIns->pHlpR3;
5743 PPDMIBASE pBase;
5744 int rc;
5745 unsigned i;
5746 uint32_t cbTotalBufferSize = 0; /** @todo r=bird: cbTotalBufferSize isn't ever set. */
5747
5748 LogFlowFunc(("pThis=%#p\n", pThis));
5749 /*
5750 * Initialize the instance data (everything touched by the destructor need
5751 * to be initialized here!).
5752 */
5753 PPDMPCIDEV pPciDev = pDevIns->apPciDevs[0];
5754 PDMPCIDEV_ASSERT_VALID(pDevIns, pPciDev);
5755
5756 PDMPciDevSetVendorId(pPciDev, 0x8086); /* Intel */
5757 PDMPciDevSetDeviceId(pPciDev, 0x2829); /* ICH-8M */
5758 PDMPciDevSetCommand(pPciDev, 0x0000);
5759#ifdef VBOX_WITH_MSI_DEVICES
5760 PDMPciDevSetStatus(pPciDev, VBOX_PCI_STATUS_CAP_LIST);
5761 PDMPciDevSetCapabilityList(pPciDev, 0x80);
5762#else
5763 PDMPciDevSetCapabilityList(pPciDev, 0x70);
5764#endif
5765 PDMPciDevSetRevisionId(pPciDev, 0x02);
5766 PDMPciDevSetClassProg(pPciDev, 0x01);
5767 PDMPciDevSetClassSub(pPciDev, 0x06);
5768 PDMPciDevSetClassBase(pPciDev, 0x01);
5769 PDMPciDevSetBaseAddress(pPciDev, 5, false, false, false, 0x00000000);
5770
5771 PDMPciDevSetInterruptLine(pPciDev, 0x00);
5772 PDMPciDevSetInterruptPin(pPciDev, 0x01);
5773
5774 PDMPciDevSetByte(pPciDev, 0x70, VBOX_PCI_CAP_ID_PM); /* Capability ID: PCI Power Management Interface */
5775 PDMPciDevSetByte(pPciDev, 0x71, 0xa8); /* next */
5776 PDMPciDevSetByte(pPciDev, 0x72, 0x03); /* version ? */
5777
5778 PDMPciDevSetByte(pPciDev, 0x90, 0x40); /* AHCI mode. */
5779 PDMPciDevSetByte(pPciDev, 0x92, 0x3f);
5780 PDMPciDevSetByte(pPciDev, 0x94, 0x80);
5781 PDMPciDevSetByte(pPciDev, 0x95, 0x01);
5782 PDMPciDevSetByte(pPciDev, 0x97, 0x78);
5783
5784 PDMPciDevSetByte(pPciDev, 0xa8, 0x12); /* SATACR capability */
5785 PDMPciDevSetByte(pPciDev, 0xa9, 0x00); /* next */
5786 PDMPciDevSetWord(pPciDev, 0xaa, 0x0010); /* Revision */
5787 PDMPciDevSetDWord(pPciDev, 0xac, 0x00000028); /* SATA Capability Register 1 */
5788
5789 pThis->cThreadsActive = 0;
5790
5791 pThisCC->pDevIns = pDevIns;
5792 pThisCC->IBase.pfnQueryInterface = ahciR3Status_QueryInterface;
5793 pThisCC->ILeds.pfnQueryStatusLed = ahciR3Status_QueryStatusLed;
5794
5795 /* Initialize port members. */
5796 for (i = 0; i < AHCI_MAX_NR_PORTS_IMPL; i++)
5797 {
5798 PAHCIPORT pAhciPort = &pThis->aPorts[i];
5799 PAHCIPORTR3 pAhciPortR3 = &pThisCC->aPorts[i];
5800 pAhciPortR3->pDevIns = pDevIns;
5801 pAhciPort->iLUN = i;
5802 pAhciPortR3->iLUN = i;
5803 pAhciPort->Led.u32Magic = PDMLED_MAGIC;
5804 pAhciPortR3->pDrvBase = NULL;
5805 pAhciPortR3->pAsyncIOThread = NULL;
5806 pAhciPort->hEvtProcess = NIL_SUPSEMEVENT;
5807 pAhciPort->fHotpluggable = true;
5808 }
5809
5810 /*
5811 * Init locks, using explicit locking where necessary.
5812 */
5813 rc = PDMDevHlpSetDeviceCritSect(pDevIns, PDMDevHlpCritSectGetNop(pDevIns));
5814 AssertRCReturn(rc, rc);
5815
5816 rc = PDMDevHlpCritSectInit(pDevIns, &pThis->lock, RT_SRC_POS, "AHCI#%u", iInstance);
5817 if (RT_FAILURE(rc))
5818 {
5819 Log(("%s: Failed to create critical section.\n", __FUNCTION__));
5820 return rc;
5821 }
5822
5823 /*
5824 * Validate and read configuration.
5825 */
5826 PDMDEV_VALIDATE_CONFIG_RETURN(pDevIns,
5827 "PrimaryMaster|PrimarySlave|SecondaryMaster"
5828 "|SecondarySlave|PortCount|Bootable|CmdSlotsAvail|TigerHack",
5829 "Port*");
5830
5831 rc = pHlp->pfnCFGMQueryU32Def(pCfg, "PortCount", &pThis->cPortsImpl, AHCI_MAX_NR_PORTS_IMPL);
5832 if (RT_FAILURE(rc))
5833 return PDMDEV_SET_ERROR(pDevIns, rc, N_("AHCI configuration error: failed to read PortCount as integer"));
5834 Log(("%s: cPortsImpl=%u\n", __FUNCTION__, pThis->cPortsImpl));
5835 if (pThis->cPortsImpl > AHCI_MAX_NR_PORTS_IMPL)
5836 return PDMDevHlpVMSetError(pDevIns, VERR_INVALID_PARAMETER, RT_SRC_POS,
5837 N_("AHCI configuration error: PortCount=%u should not exceed %u"),
5838 pThis->cPortsImpl, AHCI_MAX_NR_PORTS_IMPL);
5839 if (pThis->cPortsImpl < 1)
5840 return PDMDevHlpVMSetError(pDevIns, VERR_INVALID_PARAMETER, RT_SRC_POS,
5841 N_("AHCI configuration error: PortCount=%u should be at least 1"),
5842 pThis->cPortsImpl);
5843
5844 rc = pHlp->pfnCFGMQueryBoolDef(pCfg, "Bootable", &pThis->fBootable, true);
5845 if (RT_FAILURE(rc))
5846 return PDMDEV_SET_ERROR(pDevIns, rc,
5847 N_("AHCI configuration error: failed to read Bootable as boolean"));
5848
5849 rc = pHlp->pfnCFGMQueryU32Def(pCfg, "CmdSlotsAvail", &pThis->cCmdSlotsAvail, AHCI_NR_COMMAND_SLOTS);
5850 if (RT_FAILURE(rc))
5851 return PDMDEV_SET_ERROR(pDevIns, rc, N_("AHCI configuration error: failed to read CmdSlotsAvail as integer"));
5852 Log(("%s: cCmdSlotsAvail=%u\n", __FUNCTION__, pThis->cCmdSlotsAvail));
5853 if (pThis->cCmdSlotsAvail > AHCI_NR_COMMAND_SLOTS)
5854 return PDMDevHlpVMSetError(pDevIns, VERR_INVALID_PARAMETER, RT_SRC_POS,
5855 N_("AHCI configuration error: CmdSlotsAvail=%u should not exceed %u"),
5856 pThis->cPortsImpl, AHCI_NR_COMMAND_SLOTS);
5857 if (pThis->cCmdSlotsAvail < 1)
5858 return PDMDevHlpVMSetError(pDevIns, VERR_INVALID_PARAMETER, RT_SRC_POS,
5859 N_("AHCI configuration error: CmdSlotsAvail=%u should be at least 1"),
5860 pThis->cCmdSlotsAvail);
5861 bool fTigerHack;
5862 rc = pHlp->pfnCFGMQueryBoolDef(pCfg, "TigerHack", &fTigerHack, false);
5863 if (RT_FAILURE(rc))
5864 return PDMDEV_SET_ERROR(pDevIns, rc, N_("AHCI configuration error: failed to read TigerHack as boolean"));
5865
5866
5867 /*
5868 * Register the PCI device, it's I/O regions.
5869 */
5870 rc = PDMDevHlpPCIRegister(pDevIns, pPciDev);
5871 if (RT_FAILURE(rc))
5872 return rc;
5873
5874#ifdef VBOX_WITH_MSI_DEVICES
5875 PDMMSIREG MsiReg;
5876 RT_ZERO(MsiReg);
5877 MsiReg.cMsiVectors = 1;
5878 MsiReg.iMsiCapOffset = 0x80;
5879 MsiReg.iMsiNextOffset = 0x70;
5880 rc = PDMDevHlpPCIRegisterMsi(pDevIns, &MsiReg);
5881 if (RT_FAILURE(rc))
5882 {
5883 PCIDevSetCapabilityList(pPciDev, 0x70);
5884 /* That's OK, we can work without MSI */
5885 }
5886#endif
5887
5888 /*
5889 * Solaris 10 U5 fails to map the AHCI register space when the sets (0..3)
5890 * for the legacy IDE registers are not available. We set up "fake" entries
5891 * in the PCI configuration register. That means they are available but
5892 * read and writes from/to them have no effect. No guest should access them
5893 * anyway because the controller is marked as AHCI in the Programming
5894 * interface and we don't have an option to change to IDE emulation (real
5895 * hardware provides an option in the BIOS to switch to it which also changes
5896 * device Id and other things in the PCI configuration space).
5897 */
5898 rc = PDMDevHlpPCIIORegionCreateIo(pDevIns, 0 /*iPciRegion*/, 8 /*cPorts*/,
5899 ahciLegacyFakeWrite, ahciLegacyFakeRead, NULL /*pvUser*/,
5900 "AHCI Fake #0", NULL /*paExtDescs*/, &pThis->hIoPortsLegacyFake0);
5901 AssertRCReturn(rc, PDMDEV_SET_ERROR(pDevIns, rc, N_("AHCI cannot register PCI I/O region")));
5902
5903 rc = PDMDevHlpPCIIORegionCreateIo(pDevIns, 1 /*iPciRegion*/, 1 /*cPorts*/,
5904 ahciLegacyFakeWrite, ahciLegacyFakeRead, NULL /*pvUser*/,
5905 "AHCI Fake #1", NULL /*paExtDescs*/, &pThis->hIoPortsLegacyFake1);
5906 AssertRCReturn(rc, PDMDEV_SET_ERROR(pDevIns, rc, N_("AHCI cannot register PCI I/O region")));
5907
5908 rc = PDMDevHlpPCIIORegionCreateIo(pDevIns, 2 /*iPciRegion*/, 8 /*cPorts*/,
5909 ahciLegacyFakeWrite, ahciLegacyFakeRead, NULL /*pvUser*/,
5910 "AHCI Fake #2", NULL /*paExtDescs*/, &pThis->hIoPortsLegacyFake2);
5911 AssertRCReturn(rc, PDMDEV_SET_ERROR(pDevIns, rc, N_("AHCI cannot register PCI I/O region")));
5912
5913 rc = PDMDevHlpPCIIORegionCreateIo(pDevIns, 3 /*iPciRegion*/, 1 /*cPorts*/,
5914 ahciLegacyFakeWrite, ahciLegacyFakeRead, NULL /*pvUser*/,
5915 "AHCI Fake #3", NULL /*paExtDescs*/, &pThis->hIoPortsLegacyFake3);
5916 AssertRCReturn(rc, PDMDEV_SET_ERROR(pDevIns, rc, N_("AHCI cannot register PCI I/O region")));
5917
5918 /*
5919 * The non-fake PCI I/O regions:
5920 * Note! The 4352 byte MMIO region will be rounded up to PAGE_SIZE.
5921 */
5922 rc = PDMDevHlpPCIIORegionCreateIo(pDevIns, 4 /*iPciRegion*/, 0x10 /*cPorts*/,
5923 ahciIdxDataWrite, ahciIdxDataRead, NULL /*pvUser*/,
5924 "AHCI IDX/DATA", NULL /*paExtDescs*/, &pThis->hIoPortIdxData);
5925 AssertRCReturn(rc, PDMDEV_SET_ERROR(pDevIns, rc, N_("AHCI cannot register PCI I/O region for BMDMA")));
5926
5927
5928 /** @todo change this to IOMMMIO_FLAGS_WRITE_ONLY_DWORD once EM/IOM starts
5929 * handling 2nd DWORD failures on split accesses correctly. */
5930 rc = PDMDevHlpPCIIORegionCreateMmio(pDevIns, 5 /*iPciRegion*/, 4352 /*cbRegion*/, PCI_ADDRESS_SPACE_MEM,
5931 ahciMMIOWrite, ahciMMIORead, NULL /*pvUser*/,
5932 IOMMMIO_FLAGS_READ_DWORD | IOMMMIO_FLAGS_WRITE_DWORD_QWORD_READ_MISSING,
5933 "AHCI", &pThis->hMmio);
5934 AssertRCReturn(rc, PDMDEV_SET_ERROR(pDevIns, rc, N_("AHCI cannot register PCI memory region for registers")));
5935
5936 /*
5937 * Create the timer for command completion coalescing feature.
5938 */
5939 rc = PDMDevHlpTimerCreate(pDevIns, TMCLOCK_VIRTUAL, ahciCccTimer, pThis,
5940 TMTIMER_FLAGS_NO_CRIT_SECT, "AHCI CCC Timer", &pThis->hHbaCccTimer);
5941 AssertRCReturn(rc, rc);
5942
5943 /*
5944 * Initialize ports.
5945 */
5946
5947 /* Initialize static members on every port. */
5948 for (i = 0; i < AHCI_MAX_NR_PORTS_IMPL; i++)
5949 ahciPortHwReset(&pThis->aPorts[i]);
5950
5951 /* Attach drivers to every available port. */
5952 for (i = 0; i < pThis->cPortsImpl; i++)
5953 {
5954 PAHCIPORT pAhciPort = &pThis->aPorts[i];
5955 PAHCIPORTR3 pAhciPortR3 = &pThisCC->aPorts[i];
5956
5957 RTStrPrintf(pAhciPortR3->szDesc, sizeof(pAhciPortR3->szDesc), "Port%u", i);
5958
5959 /*
5960 * Init interfaces.
5961 */
5962 pAhciPortR3->IBase.pfnQueryInterface = ahciR3PortQueryInterface;
5963 pAhciPortR3->IMediaExPort.pfnIoReqCompleteNotify = ahciR3IoReqCompleteNotify;
5964 pAhciPortR3->IMediaExPort.pfnIoReqCopyFromBuf = ahciR3IoReqCopyFromBuf;
5965 pAhciPortR3->IMediaExPort.pfnIoReqCopyToBuf = ahciR3IoReqCopyToBuf;
5966 pAhciPortR3->IMediaExPort.pfnIoReqQueryBuf = ahciR3IoReqQueryBuf;
5967 pAhciPortR3->IMediaExPort.pfnIoReqQueryDiscardRanges = ahciR3IoReqQueryDiscardRanges;
5968 pAhciPortR3->IMediaExPort.pfnIoReqStateChanged = ahciR3IoReqStateChanged;
5969 pAhciPortR3->IMediaExPort.pfnMediumEjected = ahciR3MediumEjected;
5970 pAhciPortR3->IPort.pfnQueryDeviceLocation = ahciR3PortQueryDeviceLocation;
5971 pAhciPortR3->IPort.pfnQueryScsiInqStrings = ahciR3PortQueryScsiInqStrings;
5972 pAhciPort->fWrkThreadSleeping = true;
5973
5974 /* Query per port configuration options if available. */
5975 PCFGMNODE pCfgPort = pHlp->pfnCFGMGetChild(pDevIns->pCfg, pAhciPortR3->szDesc);
5976 if (pCfgPort)
5977 {
5978 rc = pHlp->pfnCFGMQueryBoolDef(pCfgPort, "Hotpluggable", &pAhciPort->fHotpluggable, true);
5979 if (RT_FAILURE(rc))
5980 return PDMDEV_SET_ERROR(pDevIns, rc, N_("AHCI configuration error: failed to read Hotpluggable as boolean"));
5981 }
5982
5983 /*
5984 * Attach the block driver
5985 */
5986 rc = PDMDevHlpDriverAttach(pDevIns, pAhciPort->iLUN, &pAhciPortR3->IBase, &pAhciPortR3->pDrvBase, pAhciPortR3->szDesc);
5987 if (RT_SUCCESS(rc))
5988 {
5989 rc = ahciR3ConfigureLUN(pDevIns, pAhciPort, pAhciPortR3);
5990 if (RT_FAILURE(rc))
5991 {
5992 Log(("%s: Failed to configure the %s.\n", __FUNCTION__, pAhciPortR3->szDesc));
5993 return rc;
5994 }
5995
5996 /* Mark that a device is present on that port */
5997 if (i < 6)
5998 pPciDev->abConfig[0x93] |= (1 << i);
5999
6000 /*
6001 * Init vendor product data.
6002 */
6003 rc = ahciR3VpdInit(pDevIns, pAhciPort, pAhciPortR3, pAhciPortR3->szDesc);
6004 if (RT_FAILURE(rc))
6005 return rc;
6006
6007 rc = PDMDevHlpSUPSemEventCreate(pDevIns, &pAhciPort->hEvtProcess);
6008 if (RT_FAILURE(rc))
6009 return PDMDevHlpVMSetError(pDevIns, rc, RT_SRC_POS,
6010 N_("AHCI: Failed to create SUP event semaphore"));
6011
6012 rc = PDMDevHlpThreadCreate(pDevIns, &pAhciPortR3->pAsyncIOThread, pAhciPortR3, ahciAsyncIOLoop,
6013 ahciAsyncIOLoopWakeUp, 0, RTTHREADTYPE_IO, pAhciPortR3->szDesc);
6014 if (RT_FAILURE(rc))
6015 return PDMDevHlpVMSetError(pDevIns, rc, RT_SRC_POS,
6016 N_("AHCI: Failed to create worker thread %s"), pAhciPortR3->szDesc);
6017 }
6018 else if (rc == VERR_PDM_NO_ATTACHED_DRIVER)
6019 {
6020 pAhciPortR3->pDrvBase = NULL;
6021 pAhciPort->fPresent = false;
6022 rc = VINF_SUCCESS;
6023 LogRel(("AHCI: %s: No driver attached\n", pAhciPortR3->szDesc));
6024 }
6025 else
6026 return PDMDevHlpVMSetError(pDevIns, rc, RT_SRC_POS,
6027 N_("AHCI: Failed to attach drive to %s"), pAhciPortR3->szDesc);
6028 }
6029
6030 /*
6031 * Attach status driver (optional).
6032 */
6033 rc = PDMDevHlpDriverAttach(pDevIns, PDM_STATUS_LUN, &pThisCC->IBase, &pBase, "Status Port");
6034 if (RT_SUCCESS(rc))
6035 {
6036 pThisCC->pLedsConnector = PDMIBASE_QUERY_INTERFACE(pBase, PDMILEDCONNECTORS);
6037 pThisCC->pMediaNotify = PDMIBASE_QUERY_INTERFACE(pBase, PDMIMEDIANOTIFY);
6038 }
6039 else
6040 AssertMsgReturn(rc == VERR_PDM_NO_ATTACHED_DRIVER, ("Failed to attach to status driver. rc=%Rrc\n", rc),
6041 PDMDEV_SET_ERROR(pDevIns, rc, N_("AHCI cannot attach to status driver")));
6042
6043 /*
6044 * Saved state.
6045 */
6046 rc = PDMDevHlpSSMRegisterEx(pDevIns, AHCI_SAVED_STATE_VERSION, sizeof(*pThis) + cbTotalBufferSize, NULL,
6047 NULL, ahciR3LiveExec, NULL,
6048 ahciR3SavePrep, ahciR3SaveExec, NULL,
6049 ahciR3LoadPrep, ahciR3LoadExec, NULL);
6050 if (RT_FAILURE(rc))
6051 return rc;
6052
6053 /*
6054 * Register the info item.
6055 */
6056 char szTmp[128];
6057 RTStrPrintf(szTmp, sizeof(szTmp), "%s%d", pDevIns->pReg->szName, pDevIns->iInstance);
6058 PDMDevHlpDBGFInfoRegister(pDevIns, szTmp, "AHCI info", ahciR3Info);
6059
6060 return ahciR3ResetCommon(pDevIns);
6061}
6062
6063#else /* !IN_RING3 */
6064
6065/**
6066 * @callback_method_impl{PDMDEVREGR0,pfnConstruct}
6067 */
6068static DECLCALLBACK(int) ahciRZConstruct(PPDMDEVINS pDevIns)
6069{
6070 PDMDEV_CHECK_VERSIONS_RETURN(pDevIns);
6071 PAHCI pThis = PDMDEVINS_2_DATA(pDevIns, PAHCI);
6072
6073 int rc = PDMDevHlpSetDeviceCritSect(pDevIns, PDMDevHlpCritSectGetNop(pDevIns));
6074 AssertRCReturn(rc, rc);
6075
6076 rc = PDMDevHlpIoPortSetUpContext(pDevIns, pThis->hIoPortsLegacyFake0, ahciLegacyFakeWrite, ahciLegacyFakeRead, NULL /*pvUser*/);
6077 AssertRCReturn(rc, rc);
6078 rc = PDMDevHlpIoPortSetUpContext(pDevIns, pThis->hIoPortsLegacyFake1, ahciLegacyFakeWrite, ahciLegacyFakeRead, NULL /*pvUser*/);
6079 AssertRCReturn(rc, rc);
6080 rc = PDMDevHlpIoPortSetUpContext(pDevIns, pThis->hIoPortsLegacyFake2, ahciLegacyFakeWrite, ahciLegacyFakeRead, NULL /*pvUser*/);
6081 AssertRCReturn(rc, rc);
6082 rc = PDMDevHlpIoPortSetUpContext(pDevIns, pThis->hIoPortsLegacyFake3, ahciLegacyFakeWrite, ahciLegacyFakeRead, NULL /*pvUser*/);
6083 AssertRCReturn(rc, rc);
6084
6085 rc = PDMDevHlpIoPortSetUpContext(pDevIns, pThis->hIoPortIdxData, ahciIdxDataWrite, ahciIdxDataRead, NULL /*pvUser*/);
6086 AssertRCReturn(rc, rc);
6087
6088 rc = PDMDevHlpMmioSetUpContext(pDevIns, pThis->hMmio, ahciMMIOWrite, ahciMMIORead, NULL /*pvUser*/);
6089 AssertRCReturn(rc, rc);
6090
6091 return VINF_SUCCESS;
6092}
6093
6094#endif /* !IN_RING3 */
6095
6096/**
6097 * The device registration structure.
6098 */
6099const PDMDEVREG g_DeviceAHCI =
6100{
6101 /* .u32Version = */ PDM_DEVREG_VERSION,
6102 /* .uReserved0 = */ 0,
6103 /* .szName = */ "ahci",
6104 /* .fFlags = */ PDM_DEVREG_FLAGS_DEFAULT_BITS | PDM_DEVREG_FLAGS_RZ | PDM_DEVREG_FLAGS_NEW_STYLE
6105 | PDM_DEVREG_FLAGS_FIRST_SUSPEND_NOTIFICATION | PDM_DEVREG_FLAGS_FIRST_POWEROFF_NOTIFICATION
6106 | PDM_DEVREG_FLAGS_FIRST_RESET_NOTIFICATION,
6107 /* .fClass = */ PDM_DEVREG_CLASS_STORAGE,
6108 /* .cMaxInstances = */ ~0U,
6109 /* .uSharedVersion = */ 42,
6110 /* .cbInstanceShared = */ sizeof(AHCI),
6111 /* .cbInstanceCC = */ sizeof(AHCICC),
6112 /* .cbInstanceRC = */ sizeof(AHCIRC),
6113 /* .cMaxPciDevices = */ 1,
6114 /* .cMaxMsixVectors = */ 0,
6115 /* .pszDescription = */ "Intel AHCI controller.\n",
6116#if defined(IN_RING3)
6117 /* .pszRCMod = */ "VBoxDDRC.rc",
6118 /* .pszR0Mod = */ "VBoxDDR0.r0",
6119 /* .pfnConstruct = */ ahciR3Construct,
6120 /* .pfnDestruct = */ ahciR3Destruct,
6121 /* .pfnRelocate = */ NULL,
6122 /* .pfnMemSetup = */ NULL,
6123 /* .pfnPowerOn = */ NULL,
6124 /* .pfnReset = */ ahciR3Reset,
6125 /* .pfnSuspend = */ ahciR3Suspend,
6126 /* .pfnResume = */ ahciR3Resume,
6127 /* .pfnAttach = */ ahciR3Attach,
6128 /* .pfnDetach = */ ahciR3Detach,
6129 /* .pfnQueryInterface = */ NULL,
6130 /* .pfnInitComplete = */ NULL,
6131 /* .pfnPowerOff = */ ahciR3PowerOff,
6132 /* .pfnSoftReset = */ NULL,
6133 /* .pfnReserved0 = */ NULL,
6134 /* .pfnReserved1 = */ NULL,
6135 /* .pfnReserved2 = */ NULL,
6136 /* .pfnReserved3 = */ NULL,
6137 /* .pfnReserved4 = */ NULL,
6138 /* .pfnReserved5 = */ NULL,
6139 /* .pfnReserved6 = */ NULL,
6140 /* .pfnReserved7 = */ NULL,
6141#elif defined(IN_RING0)
6142 /* .pfnEarlyConstruct = */ NULL,
6143 /* .pfnConstruct = */ ahciRZConstruct,
6144 /* .pfnDestruct = */ NULL,
6145 /* .pfnFinalDestruct = */ NULL,
6146 /* .pfnRequest = */ NULL,
6147 /* .pfnReserved0 = */ NULL,
6148 /* .pfnReserved1 = */ NULL,
6149 /* .pfnReserved2 = */ NULL,
6150 /* .pfnReserved3 = */ NULL,
6151 /* .pfnReserved4 = */ NULL,
6152 /* .pfnReserved5 = */ NULL,
6153 /* .pfnReserved6 = */ NULL,
6154 /* .pfnReserved7 = */ NULL,
6155#elif defined(IN_RC)
6156 /* .pfnConstruct = */ ahciRZConstruct,
6157 /* .pfnReserved0 = */ NULL,
6158 /* .pfnReserved1 = */ NULL,
6159 /* .pfnReserved2 = */ NULL,
6160 /* .pfnReserved3 = */ NULL,
6161 /* .pfnReserved4 = */ NULL,
6162 /* .pfnReserved5 = */ NULL,
6163 /* .pfnReserved6 = */ NULL,
6164 /* .pfnReserved7 = */ NULL,
6165#else
6166# error "Not in IN_RING3, IN_RING0 or IN_RC!"
6167#endif
6168 /* .u32VersionEnd = */ PDM_DEVREG_VERSION
6169};
6170
6171#endif /* !VBOX_DEVICE_STRUCT_TESTCASE */
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette