#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: si_sebuf.c,v 1.34 2025/12/16 18:24:47 andvar Exp $");
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/errno.h>
#include <sys/kernel.h>
#include <sys/kmem.h>
#include <sys/device.h>
#include <sys/buf.h>
#include <sys/proc.h>
#include <dev/scsipi/scsi_all.h>
#include <dev/scsipi/scsipi_all.h>
#include <dev/scsipi/scsipi_debug.h>
#include <dev/scsipi/scsiconf.h>
#include <machine/autoconf.h>
#include <dev/ic/ncr5380reg.h>
#include <dev/ic/ncr5380var.h>
#include "sereg.h"
#include "sevar.h"
#define MIN_DMA_LEN 128
#define MAX_DMA_LEN 0xE000
struct se_dma_handle {
int dh_flags;
#define SIDH_BUSY 1
#define SIDH_OUT 2
u_char * dh_addr;
int dh_maplen;
long dh_dma;
};
struct se_softc {
struct ncr5380_softc ncr_sc;
volatile struct se_regs *sc_regs;
int sc_adapter_type;
int sc_adapter_iv;
int sc_options;
int sc_reqlen;
struct se_dma_handle *sc_dma;
void *sc_dmacmd;
};
#define SE_NO_DISCONNECT 0xff
#define SE_NO_PARITY_CHK 0xff00
#define SE_FORCE_POLLING 0x10000
#define SE_DISABLE_DMA 0x20000
void se_dma_alloc(struct ncr5380_softc *);
void se_dma_free(struct ncr5380_softc *);
void se_dma_poll(struct ncr5380_softc *);
void se_dma_setup(struct ncr5380_softc *);
void se_dma_start(struct ncr5380_softc *);
void se_dma_eop(struct ncr5380_softc *);
void se_dma_stop(struct ncr5380_softc *);
void se_intr_on (struct ncr5380_softc *);
void se_intr_off(struct ncr5380_softc *);
static int se_intr(void *);
static void se_reset(struct ncr5380_softc *);
static int se_match(device_t, cfdata_t, void *);
static void se_attach(device_t, device_t, void *);
CFATTACH_DECL_NEW(si_sebuf, sizeof(struct se_softc),
se_match, se_attach, NULL, NULL);
static void se_minphys(struct buf *);
int se_options = SE_DISABLE_DMA | SE_FORCE_POLLING | 0xff;
int se_dma_intr_timo = 500;
int se_debug = 0;
static int
se_match(device_t parent, cfdata_t cf, void *args)
{
struct sebuf_attach_args *aa = args;
if (strcmp(aa->name, "se"))
return 0;
return 1;
}
static void
se_attach(device_t parent, device_t self, void *args)
{
struct se_softc *sc = device_private(self);
struct ncr5380_softc *ncr_sc = &sc->ncr_sc;
struct cfdata *cf = device_cfdata(self);
struct sebuf_attach_args *aa = args;
volatile struct se_regs *regs;
int i;
ncr_sc->sc_dev = self;
if (cf->cf_flags)
sc->sc_options = cf->cf_flags;
else
sc->sc_options = se_options;
aprint_normal(": options=0x%x\n", sc->sc_options);
sc->sc_adapter_type = aa->ca.ca_bustype;
sc->sc_adapter_iv = aa->ca.ca_intvec;
sc->sc_regs = regs = aa->regs;
ncr_sc->sc_pio_out = ncr5380_pio_out;
ncr_sc->sc_pio_in = ncr5380_pio_in;
#if 0
ncr_sc->sc_dma_alloc = se_dma_alloc;
ncr_sc->sc_dma_free = se_dma_free;
ncr_sc->sc_dma_setup = se_dma_setup;
ncr_sc->sc_dma_start = se_dma_start;
ncr_sc->sc_dma_poll = se_dma_poll;
ncr_sc->sc_dma_eop = se_dma_eop;
ncr_sc->sc_dma_stop = se_dma_stop;
ncr_sc->sc_intr_on = se_intr_on;
ncr_sc->sc_intr_off = se_intr_off;
#endif
isr_add_vectored(se_intr, (void *)sc,
aa->ca.ca_intpri, aa->ca.ca_intvec);
se_reset(ncr_sc);
ncr_sc->sc_no_disconnect =
(sc->sc_options & SE_NO_DISCONNECT);
ncr_sc->sc_parity_disable =
(sc->sc_options & SE_NO_PARITY_CHK) >> 8;
if (sc->sc_options & SE_FORCE_POLLING)
ncr_sc->sc_flags |= NCR5380_FORCE_POLLING;
#if 1
if (sc->sc_options & SE_DISABLE_DMA) {
ncr_sc->sc_dma_alloc = NULL;
}
#endif
ncr_sc->sc_min_dma_len = MIN_DMA_LEN;
ncr_sc->sci_r0 = ®s->ncrregs[0];
ncr_sc->sci_r1 = ®s->ncrregs[1];
ncr_sc->sci_r2 = ®s->ncrregs[2];
ncr_sc->sci_r3 = ®s->ncrregs[3];
ncr_sc->sci_r4 = ®s->ncrregs[4];
ncr_sc->sci_r5 = ®s->ncrregs[5];
ncr_sc->sci_r6 = ®s->ncrregs[6];
ncr_sc->sci_r7 = ®s->ncrregs[7];
ncr_sc->sc_rev = NCR_VARIANT_NCR5380;
i = SCI_OPENINGS * sizeof(struct se_dma_handle);
sc->sc_dma = kmem_alloc(i, KM_SLEEP);
for (i = 0; i < SCI_OPENINGS; i++)
sc->sc_dma[i].dh_flags = 0;
ncr_sc->sc_channel.chan_id = 7;
ncr_sc->sc_adapter.adapt_minphys = se_minphys;
ncr5380_attach(ncr_sc);
}
static void
se_reset(struct ncr5380_softc *ncr_sc)
{
struct se_softc *sc = (struct se_softc *)ncr_sc;
volatile struct se_regs *se = sc->sc_regs;
#ifdef DEBUG
if (se_debug) {
printf("%s\n", __func__);
}
#endif
se->se_csr = 0;
delay(10);
se->se_csr = SE_CSR_SCSI_RES ;
delay(10);
se->dma_addr = 0;
se->dma_cntr = 0;
se->se_ivec = sc->sc_adapter_iv;
}
void
se_intr_on(struct ncr5380_softc *ncr_sc)
{
struct se_softc *sc = (struct se_softc *)ncr_sc;
volatile struct se_regs *se = sc->sc_regs;
se->se_csr &= ~SE_CSR_SEND;
se->dma_cntr = 0;
se->dma_addr = 0;
se->se_csr |= SE_CSR_INTR_EN;
}
void
se_intr_off(struct ncr5380_softc *ncr_sc)
{
struct se_softc *sc = (struct se_softc *)ncr_sc;
volatile struct se_regs *se = sc->sc_regs;
se->se_csr &= ~SE_CSR_INTR_EN;
}
void
se_dma_setup(struct ncr5380_softc *ncr_sc)
{
struct se_softc *sc = (struct se_softc *)ncr_sc;
struct sci_req *sr = ncr_sc->sc_current;
struct se_dma_handle *dh = sr->sr_dma_hand;
volatile struct se_regs *se = sc->sc_regs;
long data_pa;
int xlen;
data_pa = 0;
data_pa += (ncr_sc->sc_dataptr - dh->dh_addr);
if (data_pa & 1)
panic("%s: bad pa=0x%lx", __func__, data_pa);
xlen = ncr_sc->sc_datalen;
xlen &= ~1;
sc->sc_reqlen = xlen;
#ifdef DEBUG
if (se_debug & 2) {
printf("%s: dh=%p, pa=0x%lx, xlen=0x%x\n",
__func__, dh, data_pa, xlen);
}
#endif
if (dh->dh_flags & SIDH_OUT) {
se->se_csr |= SE_CSR_SEND;
} else {
se->se_csr &= ~SE_CSR_SEND;
}
se->dma_addr = (ushort)(data_pa & 0xFFFF);
se->dma_cntr = 0;
}
void
se_dma_start(struct ncr5380_softc *ncr_sc)
{
struct se_softc *sc = (struct se_softc *)ncr_sc;
struct sci_req *sr = ncr_sc->sc_current;
struct se_dma_handle *dh = sr->sr_dma_hand;
volatile struct se_regs *se = sc->sc_regs;
int s, xlen;
xlen = sc->sc_reqlen;
s = splhigh();
se->dma_cntr = (ushort)(xlen & 0xFFFF);
if (dh->dh_flags & SIDH_OUT) {
*ncr_sc->sci_tcmd = PHASE_DATA_OUT;
SCI_CLR_INTR(ncr_sc);
*ncr_sc->sci_icmd = SCI_ICMD_DATA;
*ncr_sc->sci_mode |= (SCI_MODE_DMA | SCI_MODE_DMA_IE);
*ncr_sc->sci_dma_send = 0;
} else {
*ncr_sc->sci_tcmd = PHASE_DATA_IN;
SCI_CLR_INTR(ncr_sc);
*ncr_sc->sci_icmd = 0;
*ncr_sc->sci_mode |= (SCI_MODE_DMA | SCI_MODE_DMA_IE);
*ncr_sc->sci_irecv = 0;
}
se->se_csr |= SE_CSR_INTR_EN;
splx(s);
ncr_sc->sc_state |= NCR_DOINGDMA;
#ifdef DEBUG
if (se_debug & 2) {
printf("%s: started, flags=0x%x\n",
__func__, ncr_sc->sc_state);
}
#endif
}
void
se_dma_eop(struct ncr5380_softc *ncr_sc)
{
}
void
se_dma_stop(struct ncr5380_softc *ncr_sc)
{
struct se_softc *sc = (struct se_softc *)ncr_sc;
struct sci_req *sr = ncr_sc->sc_current;
struct se_dma_handle *dh = sr->sr_dma_hand;
volatile struct se_regs *se = sc->sc_regs;
int resid, ntrans;
if ((ncr_sc->sc_state & NCR_DOINGDMA) == 0) {
#ifdef DEBUG
printf("%s: DMA not running\n", __func__);
#endif
return;
}
ncr_sc->sc_state &= ~NCR_DOINGDMA;
se->se_csr &= ~SE_CSR_INTR_EN;
*ncr_sc->sci_tcmd = PHASE_INVALID;
if (ncr_sc->sc_state & NCR_ABORTING)
goto out;
resid = se->dma_cntr & 0xFFFF;
if (dh->dh_flags & SIDH_OUT)
if ((resid > 0) && (resid < sc->sc_reqlen))
resid++;
ntrans = sc->sc_reqlen - resid;
#ifdef DEBUG
if (se_debug & 2) {
printf("%s: resid=0x%x ntrans=0x%x\n",
__func__, resid, ntrans);
}
#endif
if (ntrans < MIN_DMA_LEN) {
printf("se: fifo count: 0x%x\n", resid);
ncr_sc->sc_state |= NCR_ABORTING;
goto out;
}
if (ntrans > ncr_sc->sc_datalen)
panic("%s: excess transfer", __func__);
ncr_sc->sc_dataptr += ntrans;
ncr_sc->sc_datalen -= ntrans;
out:
se->dma_addr = 0;
se->dma_cntr = 0;
*ncr_sc->sci_mode &= ~(SCI_MODE_DMA | SCI_MODE_DMA_IE);
*ncr_sc->sci_icmd = 0;
}
static void
se_minphys(struct buf *bp)
{
if (bp->b_bcount > MAX_DMA_LEN)
bp->b_bcount = MAX_DMA_LEN;
minphys(bp);
}
int
se_intr(void *arg)
{
struct se_softc *sc = arg;
volatile struct se_regs *se = sc->sc_regs;
int claimed;
u_short csr;
claimed = 0;
csr = se->se_csr;
NCR_TRACE("se_intr: csr=0x%x\n", csr);
if (csr & SE_CSR_SBC_IP) {
claimed = ncr5380_intr(&sc->ncr_sc);
#ifdef DEBUG
if (!claimed) {
printf("%s: spurious from SBC\n", __func__);
}
#endif
claimed = 1;
}
return claimed;
}
void
se_dma_alloc(struct ncr5380_softc *ncr_sc)
{
struct se_softc *sc = (struct se_softc *)ncr_sc;
struct sci_req *sr = ncr_sc->sc_current;
struct scsipi_xfer *xs = sr->sr_xs;
struct se_dma_handle *dh;
int i, xlen;
u_long addr;
#ifdef DIAGNOSTIC
if (sr->sr_dma_hand != NULL)
panic("%s: already have DMA handle", __func__);
#endif
addr = (u_long)ncr_sc->sc_dataptr;
xlen = ncr_sc->sc_datalen;
if ((addr & 1) || (xlen & 1)) {
printf("%s: misaligned.\n", __func__);
return;
}
if (xlen < MIN_DMA_LEN)
panic("%s: xlen=0x%x", __func__, xlen);
if (xlen > MAX_DMA_LEN) {
printf("%s: excessive xlen=0x%x\n", __func__, xlen);
ncr_sc->sc_datalen = xlen = MAX_DMA_LEN;
}
for (i = 0; i < SCI_OPENINGS; i++) {
if ((sc->sc_dma[i].dh_flags & SIDH_BUSY) == 0)
goto found;
}
panic("se: no free DMA handles.");
found:
dh = &sc->sc_dma[i];
dh->dh_flags = SIDH_BUSY;
if (xs->xs_control & XS_CTL_DATA_OUT)
dh->dh_flags |= SIDH_OUT;
dh->dh_addr = (uint8_t *)addr;
dh->dh_maplen = xlen;
dh->dh_dma = 0;
if (!dh->dh_dma) {
printf("%s: can't remap %p/0x%x\n",
__func__, dh->dh_addr, dh->dh_maplen);
dh->dh_flags = 0;
return;
}
sr->sr_dma_hand = dh;
}
void
se_dma_free(struct ncr5380_softc *ncr_sc)
{
struct sci_req *sr = ncr_sc->sc_current;
struct se_dma_handle *dh = sr->sr_dma_hand;
#ifdef DIAGNOSTIC
if (dh == NULL)
panic("%s: no DMA handle", __func__);
#endif
if (ncr_sc->sc_state & NCR_DOINGDMA)
panic("%s: free while in progress", __func__);
if (dh->dh_flags & SIDH_BUSY) {
dh->dh_dma = 0;
dh->dh_flags = 0;
}
sr->sr_dma_hand = NULL;
}
#define CSR_MASK SE_CSR_SBC_IP
#define POLL_TIMO 50000
void
se_dma_poll(struct ncr5380_softc *ncr_sc)
{
struct se_softc *sc = (struct se_softc *)ncr_sc;
struct sci_req *sr = ncr_sc->sc_current;
volatile struct se_regs *se = sc->sc_regs;
int tmo;
if (ncr_sc->sc_state & NCR_ABORTING)
return;
tmo = POLL_TIMO;
for (;;) {
if (se->se_csr & CSR_MASK)
break;
if (--tmo <= 0) {
printf("se: DMA timeout (while polling)\n");
sr->sr_flags |= SR_OVERDUE;
break;
}
delay(100);
}
NCR_TRACE("se_dma_poll: waited %d\n",
POLL_TIMO - tmo);
#ifdef DEBUG
if (se_debug & 2) {
printf("%s: done, csr=0x%x\n", __func__, se->se_csr);
}
#endif
}