VirtualBox

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

Last change on this file since 90791 was 90791, checked in by vboxsync, 3 years ago

Devices: More VALID_PTR -> RT_VALID_PTR/AssertPtr.

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