bcm_dma: don't dereference NULL softc

This file defines a small API to be used by other drivers. If any of
these functions are called before the bcm_dma device has attached we
should handle the error gracefully. Fix a formatting quirk while here.

Reviewed by:	manu
MFC after:	1 week
Differential Revision:	https://reviews.freebsd.org/D38756
This commit is contained in:
Mitchell Horne 2023-02-24 13:19:54 -04:00
parent 9873b17169
commit 07ccf71451

View File

@ -328,6 +328,9 @@ bcm_dma_allocate(int req_ch)
int ch = BCM_DMA_CH_INVALID; int ch = BCM_DMA_CH_INVALID;
int i; int i;
if (sc == NULL)
return (BCM_DMA_CH_INVALID);
if (req_ch >= BCM_DMA_CH_MAX) if (req_ch >= BCM_DMA_CH_MAX)
return (BCM_DMA_CH_INVALID); return (BCM_DMA_CH_INVALID);
@ -343,13 +346,10 @@ bcm_dma_allocate(int req_ch)
break; break;
} }
} }
} } else if (sc->sc_dma_ch[req_ch].flags & BCM_DMA_CH_FREE) {
else { ch = req_ch;
if (sc->sc_dma_ch[req_ch].flags & BCM_DMA_CH_FREE) { sc->sc_dma_ch[ch].flags &= ~BCM_DMA_CH_FREE;
ch = req_ch; sc->sc_dma_ch[ch].flags |= BCM_DMA_CH_USED;
sc->sc_dma_ch[ch].flags &= ~BCM_DMA_CH_FREE;
sc->sc_dma_ch[ch].flags |= BCM_DMA_CH_USED;
}
} }
mtx_unlock(&sc->sc_mtx); mtx_unlock(&sc->sc_mtx);
@ -364,6 +364,9 @@ bcm_dma_free(int ch)
{ {
struct bcm_dma_softc *sc = bcm_dma_sc; struct bcm_dma_softc *sc = bcm_dma_sc;
if (sc == NULL)
return (-1);
if (ch < 0 || ch >= BCM_DMA_CH_MAX) if (ch < 0 || ch >= BCM_DMA_CH_MAX)
return (-1); return (-1);
@ -392,6 +395,9 @@ bcm_dma_setup_intr(int ch, void (*func)(int, void *), void *arg)
struct bcm_dma_softc *sc = bcm_dma_sc; struct bcm_dma_softc *sc = bcm_dma_sc;
struct bcm_dma_cb *cb; struct bcm_dma_cb *cb;
if (sc == NULL)
return (-1);
if (ch < 0 || ch >= BCM_DMA_CH_MAX) if (ch < 0 || ch >= BCM_DMA_CH_MAX)
return (-1); return (-1);
@ -531,6 +537,9 @@ bcm_dma_reg_dump(int ch)
int i; int i;
uint32_t reg; uint32_t reg;
if (sc == NULL)
return;
if (ch < 0 || ch >= BCM_DMA_CH_MAX) if (ch < 0 || ch >= BCM_DMA_CH_MAX)
return; return;
@ -558,6 +567,9 @@ bcm_dma_start(int ch, vm_paddr_t src, vm_paddr_t dst, int len)
struct bcm_dma_softc *sc = bcm_dma_sc; struct bcm_dma_softc *sc = bcm_dma_sc;
struct bcm_dma_cb *cb; struct bcm_dma_cb *cb;
if (sc == NULL)
return (-1);
if (ch < 0 || ch >= BCM_DMA_CH_MAX) if (ch < 0 || ch >= BCM_DMA_CH_MAX)
return (-1); return (-1);
@ -597,6 +609,9 @@ bcm_dma_length(int ch)
struct bcm_dma_softc *sc = bcm_dma_sc; struct bcm_dma_softc *sc = bcm_dma_sc;
struct bcm_dma_cb *cb; struct bcm_dma_cb *cb;
if (sc == NULL)
return (0);
if (ch < 0 || ch >= BCM_DMA_CH_MAX) if (ch < 0 || ch >= BCM_DMA_CH_MAX)
return (0); return (0);