From 1ec9b34e7abf37fd00fd04c3f2e1a75c41fe403f Mon Sep 17 00:00:00 2001 From: Ian Moffett Date: Fri, 9 May 2025 22:12:27 -0400 Subject: kernel: ahci: Add sanity checks to port start/stop Signed-off-by: Ian Moffett --- sys/dev/ic/ahci.c | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) (limited to 'sys/dev/ic/ahci.c') diff --git a/sys/dev/ic/ahci.c b/sys/dev/ic/ahci.c index 1eac07b..c0c6fec 100644 --- a/sys/dev/ic/ahci.c +++ b/sys/dev/ic/ahci.c @@ -127,10 +127,15 @@ ahci_hba_reset(struct ahci_hba *hba) static int hba_port_stop(struct hba_port *port) { + const uint32_t RUN_MASK = (AHCI_PXCMD_FR | AHCI_PXCMD_CR); uint32_t cmd, tmp; - /* Stop the port */ + /* Ensure the port is running */ cmd = mmio_read32(&port->cmd); + if (!ISSET(cmd, RUN_MASK)) { + return 0; + } + cmd &= ~(AHCI_PXCMD_ST | AHCI_PXCMD_FRE); mmio_write32(&port->cmd, cmd); @@ -153,10 +158,16 @@ hba_port_stop(struct hba_port *port) static int hba_port_start(struct hba_port *port) { + const uint32_t RUN_MASK = (AHCI_PXCMD_FR | AHCI_PXCMD_CR); uint32_t cmd, tmp; - /* Bring up the port */ + /* Ensure the port is not running */ cmd = mmio_read32(&port->cmd); + if (ISSET(cmd, RUN_MASK)) { + return 0; + } + + /* Bring up the port */ cmd |= AHCI_PXCMD_ST | AHCI_PXCMD_FRE; mmio_write32(&port->cmd, cmd); -- cgit v1.2.3