#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: mscp_subr.c,v 1.49 2024/02/10 08:24:51 andvar Exp $");
#include <sys/param.h>
#include <sys/device.h>
#include <sys/buf.h>
#include <sys/bufq.h>
#include <sys/systm.h>
#include <sys/proc.h>
#include <sys/kmem.h>
#include <sys/bus.h>
#include <machine/sid.h>
#include <dev/mscp/mscp.h>
#include <dev/mscp/mscpreg.h>
#include <dev/mscp/mscpvar.h>
#include "ra.h"
#include "mt.h"
#define b_forw b_hash.le_next
int mscp_match(device_t, cfdata_t, void *);
void mscp_attach(device_t, device_t, void *);
void mscp_start(struct mscp_softc *);
int mscp_init(struct mscp_softc *);
void mscp_initds(struct mscp_softc *);
int mscp_waitstep(struct mscp_softc *, int, int);
CFATTACH_DECL_NEW(mscpbus, sizeof(struct mscp_softc),
mscp_match, mscp_attach, NULL, NULL);
#define READ_SA (bus_space_read_2(mi->mi_iot, mi->mi_sah, 0))
#define READ_IP (bus_space_read_2(mi->mi_iot, mi->mi_iph, 0))
#define WRITE_IP(x) bus_space_write_2(mi->mi_iot, mi->mi_iph, 0, (x))
#define WRITE_SW(x) bus_space_write_2(mi->mi_iot, mi->mi_swh, 0, (x))
struct mscp mscp_cold_reply;
int mscp_cold_unit;
#define NITEMS 4
static inline void
mscp_free_workitems(struct mscp_softc *mi)
{
struct mscp_work *mw;
while (!SLIST_EMPTY(&mi->mi_freelist)) {
mw = SLIST_FIRST(&mi->mi_freelist);
SLIST_REMOVE_HEAD(&mi->mi_freelist, mw_list);
kmem_free(mw, sizeof(*mw));
}
}
#define DELAYTEN 1000
int
mscp_waitstep(struct mscp_softc *mi, int mask, int result)
{
int status = 1;
if ((READ_SA & mask) != result) {
volatile int count = 0;
while ((READ_SA & mask) != result) {
DELAY(10000);
count += 1;
if (count > DELAYTEN)
break;
}
if (count > DELAYTEN)
status = 0;
}
return status;
}
int
mscp_match(device_t parent, cfdata_t match, void *aux)
{
struct mscp_attach_args *ma = aux;
#if NRA || NRACD || NRX
if (ma->ma_type & MSCPBUS_DISK)
return 1;
#endif
#if NMT
if (ma->ma_type & MSCPBUS_TAPE)
return 1;
#endif
return 0;
};
void
mscp_attach(device_t parent, device_t self, void *aux)
{
struct mscp_attach_args *ma = aux;
struct mscp_softc *mi = device_private(self);
struct mscp *mp2;
volatile struct mscp *mp;
volatile int i;
int timeout, error, unit;
mi->mi_dev = self;
mi->mi_mc = ma->ma_mc;
mi->mi_me = NULL;
mi->mi_type = ma->ma_type;
mi->mi_uda = ma->ma_uda;
mi->mi_dmat = ma->ma_dmat;
mi->mi_dmam = ma->ma_dmam;
mi->mi_iot = ma->ma_iot;
mi->mi_iph = ma->ma_iph;
mi->mi_sah = ma->ma_sah;
mi->mi_swh = ma->ma_swh;
mi->mi_ivec = ma->ma_ivec;
mi->mi_adapnr = ma->ma_adapnr;
mi->mi_ctlrnr = ma->ma_ctlrnr;
*ma->ma_softc = mi;
mutex_init(&mi->mi_mtx, MUTEX_DEFAULT, IPL_VM);
SLIST_INIT(&mi->mi_freelist);
error = workqueue_create(&mi->mi_wq, "mscp_wq", mscp_worker, NULL,
PRI_NONE, IPL_VM, 0);
if (error != 0) {
aprint_error_dev(mi->mi_dev, "could not create workqueue");
return;
}
for (i = 0; i < NITEMS; i++) {
struct mscp_work *mw;
mw = kmem_zalloc(sizeof(*mw), KM_SLEEP);
SLIST_INSERT_HEAD(&mi->mi_freelist, mw, mw_list);
}
mi->mi_cmd.mri_size = NCMD;
mi->mi_cmd.mri_desc = mi->mi_uda->mp_ca.ca_cmddsc;
mi->mi_cmd.mri_ring = mi->mi_uda->mp_cmd;
mi->mi_rsp.mri_size = NRSP;
mi->mi_rsp.mri_desc = mi->mi_uda->mp_ca.ca_rspdsc;
mi->mi_rsp.mri_ring = mi->mi_uda->mp_rsp;
bufq_alloc(&mi->mi_resq, "fcfs", 0);
if (mscp_init(mi)) {
aprint_error_dev(mi->mi_dev, "can't init, controller hung\n");
return;
}
for (i = 0; i < NCMD; i++) {
mi->mi_mxiuse |= (1 << i);
if (bus_dmamap_create(mi->mi_dmat, (64*1024), 16, (64*1024),
0, BUS_DMA_NOWAIT, &mi->mi_xi[i].mxi_dmam)) {
printf("Couldn't alloc dmamap %d\n", i);
return;
}
}
#if NRA || NRACD || NRX
if (ma->ma_type & MSCPBUS_DISK) {
extern struct mscp_device ra_device;
mi->mi_me = &ra_device;
}
#endif
#if NMT
if (ma->ma_type & MSCPBUS_TAPE) {
extern struct mscp_device mt_device;
mi->mi_me = &mt_device;
}
#endif
for (unit = 0; unit <= MSCP_MAX_UNIT; ++unit) {
mp = mscp_getcp(mi, MSCP_DONTWAIT);
if (mp == NULL)
panic("mscpattach: no packets");
mp->mscp_opcode = M_OP_GETUNITST;
mp->mscp_unit = unit;
mp->mscp_modifier = M_GUM_NEXTUNIT;
*mp->mscp_addr |= MSCP_OWN | MSCP_INT;
mscp_cold_reply.mscp_opcode = 0;
mscp_cold_unit = mp->mscp_unit;
i = bus_space_read_2(mi->mi_iot, mi->mi_iph, 0);
mp = &mscp_cold_reply;
timeout = 1000;
while (!mp->mscp_opcode) {
if ( --timeout == 0) {
printf("%s: no Get Unit Status response\n",
device_xname(mi->mi_dev));
return;
}
DELAY(10000);
}
if (mp->mscp_unit < unit)
return;
if (mp->mscp_unit > unit)
unit = mp->mscp_unit;
switch (mp->mscp_status & M_ST_MASK) {
case M_ST_SUCCESS:
case M_ST_AVAILABLE:
break;
case M_ST_OFFLINE:
switch (mp->mscp_status & ~M_ST_MASK) {
case M_OFFLINE_UNKNOWN:
mi->mi_ierr = 3;
break;
case M_OFFLINE_UNMOUNTED:
break;
default:
printf("%s: unit %d off line: ",
device_xname(mi->mi_dev), mp->mscp_unit);
mp2 = __UNVOLATILE(mp);
mscp_printevent(mp2);
break;
}
break;
default:
aprint_error_dev(mi->mi_dev,
"unable to get unit status: ");
mscp_printevent(__UNVOLATILE(mp));
return;
}
}
}
int
mscp_init(struct mscp_softc *mi)
{
struct mscp *mp;
volatile int i;
int status, count;
unsigned int j = 0;
mi->mi_cmd.mri_next = 0;
mi->mi_rsp.mri_next = 0;
mi->mi_flags |= MSC_IGNOREINTR;
if ((mi->mi_type & MSCPBUS_KDB) == 0)
WRITE_IP(0); ;
status = mscp_waitstep(mi, MP_STEP1, MP_STEP1);
if (status == 0)
return 1;
if (READ_SA & MP_ERR) {
(*mi->mi_mc->mc_saerror)(device_parent(mi->mi_dev), 0);
return 1;
}
WRITE_SW(MP_ERR | (NCMDL2 << 11) | (NRSPL2 << 8) |
MP_IE | (mi->mi_ivec >> 2));
status = mscp_waitstep(mi, STEP1MASK, STEP1GOOD);
if (status == 0) {
(*mi->mi_mc->mc_saerror)(device_parent(mi->mi_dev), 0);
return 1;
}
WRITE_SW(((mi->mi_dmam->dm_segs[0].ds_addr & 0xffff) +
offsetof(struct mscp_pack, mp_ca.ca_rspdsc[0])) |
(vax_cputype == VAX_780 || vax_cputype == VAX_8600 ? MP_PI : 0));
status = mscp_waitstep(mi, STEP2MASK, STEP2GOOD(mi->mi_ivec >> 2));
if (status == 0) {
(*mi->mi_mc->mc_saerror)(device_parent(mi->mi_dev), 0);
return 1;
}
WRITE_SW((mi->mi_dmam->dm_segs[0].ds_addr >> 16));
status = mscp_waitstep(mi, STEP3MASK, STEP3GOOD);
if (status == 0) {
(*mi->mi_mc->mc_saerror)(device_parent(mi->mi_dev), 0);
return 1;
}
i = READ_SA & 0377;
printf(": version %d model %d\n", i & 15, i >> 4);
#define BURST 4
if (mi->mi_type & MSCPBUS_UDA) {
WRITE_SW(MP_GO | (BURST - 1) << 2);
printf("%s: DMA burst size set to %d\n",
device_xname(mi->mi_dev), BURST);
}
WRITE_SW(MP_GO);
mscp_initds(mi);
mi->mi_flags &= ~MSC_IGNOREINTR;
mi->mi_credits = MSCP_MINCREDITS + 1;
mp = mscp_getcp(mi, MSCP_DONTWAIT);
mi->mi_credits = 0;
mp->mscp_opcode = M_OP_SETCTLRC;
mp->mscp_unit = mp->mscp_modifier = mp->mscp_flags =
mp->mscp_sccc.sccc_version = mp->mscp_sccc.sccc_hosttimo =
mp->mscp_sccc.sccc_time = mp->mscp_sccc.sccc_time1 =
mp->mscp_sccc.sccc_errlgfl = 0;
mp->mscp_sccc.sccc_ctlrflags = M_CF_ATTN | M_CF_MISC | M_CF_THIS;
*mp->mscp_addr |= MSCP_OWN | MSCP_INT;
READ_IP;
count = 0;
while (count < DELAYTEN) {
if (((volatile int)mi->mi_flags & MSC_READY) != 0)
break;
if ((j = READ_SA) & MP_ERR)
goto out;
DELAY(10000);
count += 1;
}
if (count == DELAYTEN) {
out:
aprint_error_dev(mi->mi_dev, "couldn't set ctlr characteristics, sa=%x\n", j);
return 1;
}
return 0;
}
void
mscp_initds(struct mscp_softc *mi)
{
struct mscp_pack *ud = mi->mi_uda;
struct mscp *mp;
int i;
for (i = 0, mp = ud->mp_rsp; i < NRSP; i++, mp++) {
ud->mp_ca.ca_rspdsc[i] = MSCP_OWN | MSCP_INT |
(mi->mi_dmam->dm_segs[0].ds_addr +
offsetof(struct mscp_pack, mp_rsp[i].mscp_cmdref));
mp->mscp_addr = &ud->mp_ca.ca_rspdsc[i];
mp->mscp_msglen = MSCP_MSGLEN;
}
for (i = 0, mp = ud->mp_cmd; i < NCMD; i++, mp++) {
ud->mp_ca.ca_cmddsc[i] = MSCP_INT |
(mi->mi_dmam->dm_segs[0].ds_addr +
offsetof(struct mscp_pack, mp_cmd[i].mscp_cmdref));
mp->mscp_addr = &ud->mp_ca.ca_cmddsc[i];
mp->mscp_msglen = MSCP_MSGLEN;
if (mi->mi_type & MSCPBUS_TAPE)
mp->mscp_vcid = 1;
}
}
static void mscp_kickaway(struct mscp_softc *);
void
mscp_intr(struct mscp_softc *mi)
{
struct mscp_pack *ud = mi->mi_uda;
if (mi->mi_flags & MSC_IGNOREINTR)
return;
if (ud->mp_ca.ca_rspint) {
ud->mp_ca.ca_rspint = 0;
mscp_dorsp(mi);
}
if (ud->mp_ca.ca_cmdint) {
ud->mp_ca.ca_cmdint = 0;
MSCP_DOCMD(mi);
}
if (bufq_peek(mi->mi_resq))
mscp_kickaway(mi);
}
int
mscp_print(void *aux, const char *name)
{
struct drive_attach_args *da = aux;
struct mscp *mp = da->da_mp;
int type = mp->mscp_guse.guse_mediaid;
if (name) {
aprint_normal("%c%c", MSCP_MID_CHAR(2, type),
MSCP_MID_CHAR(1, type));
if (MSCP_MID_ECH(0, type))
aprint_normal("%c", MSCP_MID_CHAR(0, type));
aprint_normal("%d at %s drive %d", MSCP_MID_NUM(type), name,
mp->mscp_unit);
}
return UNCONF;
}
void
mscp_strategy(struct buf *bp, device_t usc)
{
struct mscp_softc *mi = device_private(usc);
int s = spluba();
bufq_put(mi->mi_resq, bp);
mscp_kickaway(mi);
splx(s);
}
void
mscp_kickaway(struct mscp_softc *mi)
{
struct buf *bp;
struct mscp *mp;
int next;
while ((bp = bufq_peek(mi->mi_resq)) != NULL) {
if ((mp = mscp_getcp(mi, MSCP_DONTWAIT)) == NULL) {
if (mi->mi_credits > MSCP_MINCREDITS)
printf("%s: command ring too small\n",
device_xname(device_parent(mi->mi_dev)));
return;
}
if ((next = (ffs(mi->mi_mxiuse) - 1)) < 0)
panic("no mxi buffers");
mi->mi_mxiuse &= ~(1 << next);
if (mi->mi_xi[next].mxi_inuse)
panic("mxi inuse");
mp->mscp_opcode =
(bp->b_flags & B_READ) ? M_OP_READ : M_OP_WRITE;
mp->mscp_cmdref = next;
mi->mi_xi[next].mxi_bp = bp;
mi->mi_xi[next].mxi_mp = mp;
mi->mi_xi[next].mxi_inuse = 1;
bp->b_resid = next;
(*mi->mi_me->me_fillin)(bp, mp);
(*mi->mi_mc->mc_go)(device_parent(mi->mi_dev),
&mi->mi_xi[next]);
(void)bufq_get(mi->mi_resq);
}
}
void
mscp_dgo(struct mscp_softc *mi, struct mscp_xi *mxi)
{
struct mscp *mp;
mp = mxi->mxi_mp;
mp->mscp_seq.seq_buffer = mxi->mxi_dmam->dm_segs[0].ds_addr;
*mp->mscp_addr |= MSCP_OWN | MSCP_INT;
READ_IP;
}
#ifdef DIAGNOSTIC
void
mscp_hexdump(struct mscp *mp)
{
long *p = (long *) mp;
int i = mp->mscp_msglen;
if (i > 256)
i = 256;
i /= sizeof (*p);
while (--i >= 0)
printf("0x%x ", (int)*p++);
printf("\n");
}
#endif
static char unknown_msg[] = "unknown subcode";
static const char *succ_msgs[] = {
"normal",
"spin down ignored",
"still connected",
unknown_msg,
"dup. unit #",
unknown_msg,
unknown_msg,
unknown_msg,
"already online",
unknown_msg,
unknown_msg,
unknown_msg,
unknown_msg,
unknown_msg,
unknown_msg,
unknown_msg,
"still online",
};
static const char *icmd_msgs[] = {
"invalid msg length",
};
static const char *offl_msgs[] = {
"unknown drive",
"not mounted",
"inoperative",
unknown_msg,
"duplicate",
unknown_msg,
unknown_msg,
unknown_msg,
"in diagnosis",
};
static const char *media_fmt_msgs[] = {
"fct unread - edc",
"invalid sector header",
"not 512 sectors",
"not formatted",
"fct ecc",
};
static const char *wrprot_msgs[] = {
unknown_msg,
"software",
"hardware",
};
static const char *data_msgs[] = {
"forced error",
unknown_msg,
"header compare",
"sync timeout",
unknown_msg,
unknown_msg,
unknown_msg,
"uncorrectable ecc",
"1 symbol ecc",
"2 symbol ecc",
"3 symbol ecc",
"4 symbol ecc",
"5 symbol ecc",
"6 symbol ecc",
"7 symbol ecc",
"8 symbol ecc",
};
static const char *host_buffer_msgs[] = {
unknown_msg,
"odd xfer addr",
"odd xfer count",
"non-exist. memory",
"memory parity",
};
static const char *cntlr_msgs[] = {
unknown_msg,
"serdes overrun",
"edc",
"inconsistent internal data struct",
};
static const char *drive_msgs[] = {
unknown_msg,
"sdi command timeout",
"ctlr detected protocol",
"positioner",
"lost rd/wr ready",
"drive clock dropout",
"lost recvr ready",
"drive detected error",
"ctlr detected pulse or parity",
};
struct code_decode {
const char *cdc_msg;
int cdc_nsubcodes;
const char **cdc_submsgs;
} code_decode[] = {
#define SC(m) sizeof (m) / sizeof (m[0]), m
{"success", SC(succ_msgs)},
{"invalid command", SC(icmd_msgs)},
{"command aborted", 0, 0},
{"unit offline", SC(offl_msgs)},
{"unit available", 0, 0},
{"media format error", SC(media_fmt_msgs)},
{"write protected", SC(wrprot_msgs)},
{"compare error", 0, 0},
{"data error", SC(data_msgs)},
{"host buffer access error", SC(host_buffer_msgs)},
{"controller error", SC(cntlr_msgs)},
{"drive error", SC(drive_msgs)},
#undef SC
};
void
mscp_printevent(struct mscp *mp)
{
int event = mp->mscp_event;
struct code_decode *cdc;
int c, sc;
const char *cm, *scm;
c = event & M_ST_MASK;
sc = (c != 6 ? event >> 5 : event >> 12) & 0x7ff;
if (c >= sizeof code_decode / sizeof code_decode[0])
cm = "- unknown code", scm = "??";
else {
cdc = &code_decode[c];
cm = cdc->cdc_msg;
if (sc >= cdc->cdc_nsubcodes)
scm = unknown_msg;
else
scm = cdc->cdc_submsgs[sc];
}
printf(" %s (%s) (code %d, subcode %d)\n", cm, scm, c, sc);
}
static const char *codemsg[16] = {
"lbn", "code 1", "code 2", "code 3",
"code 4", "code 5", "rbn", "code 7",
"code 8", "code 9", "code 10", "code 11",
"code 12", "code 13", "code 14", "code 15"
};
int
mscp_decodeerror(const char *name, struct mscp *mp, struct mscp_softc *mi)
{
int issoft;
if (((mp->mscp_event & M_ST_MASK) == 11) && (mi->mi_ierr++ < 3))
return 1;
issoft = mp->mscp_flags & (M_LF_SUCC | M_LF_CONT);
#define BADCODE(h) (codemsg[(unsigned)(h) >> 28])
#define BADLBN(h) ((h) & 0xfffffff)
printf("%s: drive %d %s error datagram%s:", name, mp->mscp_unit,
issoft ? "soft" : "hard",
mp->mscp_flags & M_LF_CONT ? " (continuing)" : "");
switch (mp->mscp_format & 0377) {
case M_FM_CTLRERR:
break;
case M_FM_BUSADDR:
printf(" memory addr 0x%x:", (int)mp->mscp_erd.erd_busaddr);
break;
case M_FM_DISKTRN:
printf(" unit %d: level %d retry %d, %s %d:",
mp->mscp_unit,
mp->mscp_erd.erd_level, mp->mscp_erd.erd_retry,
BADCODE(mp->mscp_erd.erd_hdr),
(int)BADLBN(mp->mscp_erd.erd_hdr));
break;
case M_FM_SDI:
printf(" unit %d: %s %d:", mp->mscp_unit,
BADCODE(mp->mscp_erd.erd_hdr),
(int)BADLBN(mp->mscp_erd.erd_hdr));
break;
case M_FM_SMLDSK:
printf(" unit %d: small disk error, cyl %d:",
mp->mscp_unit, mp->mscp_erd.erd_sdecyl);
break;
case M_FM_TAPETRN:
printf(" unit %d: tape transfer error, grp 0x%x event 0%o:",
mp->mscp_unit, mp->mscp_erd.erd_sdecyl, mp->mscp_event);
break;
case M_FM_STIERR:
printf(" unit %d: STI error, event 0%o:", mp->mscp_unit,
mp->mscp_event);
break;
default:
printf(" unit %d: unknown error, format 0x%x:",
mp->mscp_unit, mp->mscp_format);
}
mscp_printevent(mp);
return 0;
#undef BADCODE
#undef BADLBN
}