qemu-e2k/hw
Niklas Cassel e2a5d9b3d9 hw/ide/ahci: simplify and document PxCI handling
The AHCI spec states that:
For NCQ, PxCI is cleared on command queued successfully.

For non-NCQ, PxCI is cleared on command completed successfully.
(A non-NCQ command that completes with error does not clear PxCI.)

The current QEMU implementation either clears PxCI in check_cmd(),
or in ahci_cmd_done().

check_cmd() will clear PxCI for a command if handle_cmd() returns 0.
handle_cmd() will return -1 if BUSY or DRQ is set.

The QEMU implementation for NCQ commands will currently not set BUSY
or DRQ, so they will always have PxCI cleared by handle_cmd().
ahci_cmd_done() will never even get called for NCQ commands.

Non-NCQ commands are executed by ide_bus_exec_cmd().
Non-NCQ commands in QEMU are implemented either in a sync or in an async
way.

For non-NCQ commands implemented in a sync way, the command handler will
return true, and when ide_bus_exec_cmd() sees that a command handler
returns true, it will call ide_cmd_done() (which will call
ahci_cmd_done()). For a command implemented in a sync way,
ahci_cmd_done() will do nothing (since busy_slot is not set). Instead,
after ide_bus_exec_cmd() has finished, check_cmd() will clear PxCI for
these commands.

For non-NCQ commands implemented in an async way (using either aiocb or
pio_aiocb), the command handler will return false, ide_bus_exec_cmd()
will not call ide_cmd_done(), instead it is expected that the async
callback function will call ide_cmd_done() once the async command is
done. handle_cmd() will set busy_slot, if and only if BUSY or DRQ is
set, and this is checked _after_ ide_bus_exec_cmd() has returned.
handle_cmd() will return -1, so check_cmd() will not clear PxCI.
When the async callback calls ide_cmd_done() (which will call
ahci_cmd_done()), it will see that busy_slot is set, and
ahci_cmd_done() will clear PxCI.

This seems racy, since busy_slot is set _after_ ide_bus_exec_cmd() has
returned. The callback might come before busy_slot gets set. And it is
quite confusing that ahci_cmd_done() will be called for all non-NCQ
commands when the command is done, but will only clear PxCI in certain
cases, even though it will always write a D2H FIS and raise an IRQ.

Even worse, in the case where ahci_cmd_done() does not clear PxCI, it
still raises an IRQ. Host software might thus read an old PxCI value,
since PxCI is cleared (by check_cmd()) after the IRQ has been raised.

Try to simplify this by always setting busy_slot for non-NCQ commands,
such that ahci_cmd_done() will always be responsible for clearing PxCI
for non-NCQ commands.

For NCQ commands, clear PxCI when we receive the D2H FIS, but before
raising the IRQ, see AHCI 1.3.1, section 5.3.8, states RegFIS:Entry and
RegFIS:ClearCI.

Signed-off-by: Niklas Cassel <niklas.cassel@wdc.com>
Message-id: 20230609140844.202795-5-nks@flawful.org
Signed-off-by: John Snow <jsnow@redhat.com>
2023-09-06 22:48:04 -04:00
..
9pfs hw/9pfs: spelling fixes 2023-07-25 17:15:47 +03:00
acpi
adc
alpha
arm hw/sd: Introduce a "sd-card" SPI variant model 2023-09-01 11:40:04 +02:00
audio
avr
block m25p80: Introduce an helper to retrieve the BlockBackend of a device 2023-09-01 11:40:04 +02:00
char hw/char/pl011: Replace magic values by register field definitions 2023-08-31 19:47:43 +02:00
core hw: Add compat machines for 8.2 2023-08-23 12:06:39 +02:00
cpu
cris
cxl
display hw/display: spelling fixes 2023-08-31 19:47:43 +02:00
dma hw/dma/etraxfs: Include missing 'exec/memory.h' header 2023-08-31 19:47:43 +02:00
gpio hw/gpio/nrf51: implement DETECT signal 2023-08-22 17:30:59 +01:00
hppa
hyperv
i2c aspeed queue: 2023-09-06 11:14:55 -04:00
i386 hw/i386: Rename 'hw/kvm/clock.h' -> 'hw/i386/kvm/clock.h' 2023-08-31 19:47:43 +02:00
ide hw/ide/ahci: simplify and document PxCI handling 2023-09-06 22:48:04 -04:00
input
intc accel: Remove HAX accelerator 2023-08-31 19:46:43 +02:00
ipack
ipmi
isa
loongarch hw/loongarch: Fix ACPI processor id off-by-one error 2023-08-24 16:58:16 +08:00
m68k hw: Add compat machines for 8.2 2023-08-23 12:06:39 +02:00
mem
microblaze hw/ssi: Check for duplicate CS indexes 2023-09-01 11:40:04 +02:00
mips hw/mips: spelling fixes 2023-08-31 19:47:43 +02:00
misc Add i.MX7 SRC device implementation 2023-08-31 09:45:17 +01:00
net hw/net/i82596: Include missing 'exec/address-spaces.h' header 2023-08-31 19:47:43 +02:00
nios2
nubus
nvme hw/nvme: fix null pointer access in ruh update 2023-08-09 15:32:32 +02:00
nvram hw/nvram: Avoid unnecessary Xilinx eFuse backstore write 2023-07-17 11:05:52 +01:00
openrisc
pci pci: Fix the update of interrupt disable bit in PCI_COMMAND register 2023-08-11 12:15:24 -04:00
pci-bridge hw/pci-bridge/cxl_upstream.c: Use g_new0() in build_cdat_table() 2023-08-03 16:06:49 -04:00
pci-host hw/pci-host: Allow extended config space access for Designware PCIe host 2023-08-11 12:15:24 -04:00
pcmcia
ppc target-arm queue: 2023-08-24 10:08:33 -04:00
rdma
remote
riscv hw/sd: Introduce a "sd-card" SPI variant model 2023-09-01 11:40:04 +02:00
rtc hw/rtc/aspeed_rtc: Use 64-bit offset for holding time_t difference 2023-08-31 09:45:18 +01:00
rx
s390x hw/s390x/s390-virtio-ccw: Remove superfluous code to set the NIC model 2023-08-23 12:06:44 +02:00
scsi
sd aspeed queue: 2023-09-06 11:14:55 -04:00
sensor hw/i2c: spelling fixes 2023-08-31 19:47:43 +02:00
sh4
smbios
sparc other architectures: spelling fixes 2023-07-25 17:14:07 +03:00
sparc64
ssi hw/ssi: Check for duplicate CS indexes 2023-09-01 11:40:04 +02:00
timer
tpm
tricore
usb hw/usb/hcd-xhci: Avoid variable-length array in xhci_get_port_bandwidth() 2023-08-31 19:47:43 +02:00
vfio
virtio virtio-crypto: verify src&dst buffer length for sym request 2023-08-03 16:16:17 -04:00
watchdog
xen bulk: Do not declare function prototypes using 'extern' keyword 2023-08-31 19:47:43 +02:00
xenpv
xtensa target/xtensa: Include missing 'qemu/atomic.h' header 2023-08-31 19:47:43 +02:00
Kconfig
meson.build