/* $NetBSD: mscp_subr.c,v 1.49 2024/02/10 08:24:51 andvar Exp $ */
/*
* Copyright (c) 1988 Regents of the University of California.
* All rights reserved.
*
* This code is derived from software contributed to Berkeley by
* Chris Torek.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* @(#)mscp.c 7.5 (Berkeley) 12/16/90
*/
/*
* Copyright (c) 1996 Ludd, University of Lule}, Sweden.
*
* This code is derived from software contributed to Berkeley by
* Chris Torek.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* @(#)mscp.c 7.5 (Berkeley) 12/16/90
*/
/*
* This function is for delay during init. Some MSCP clone card (Dilog)
* can't handle fast read from its registers, and therefore need
* a delay between them.
*/
#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;
}
/*
* Go out to init the bus, so that we can give commands
* to its devices.
*/
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;
}
}
mi->mi_me = &mt_device;
}
#endif
/*
* Go out and search for sub-units on this MSCP bus,
* and call config_found for each found.
*/
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;
while (!mp->mscp_opcode) {
if ( --timeout == 0) {
printf("%s: no Get Unit Status response\n",
device_xname(mi->mi_dev));
return;
}
DELAY(10000);
}
/*
* Got a slave response. If the unit is there, use it.
*/
/*
* If we get a lower number, we have circulated around all
* devices and are finished, otherwise try to find next unit.
*/
if (mp->mscp_unit < unit)
return;
/*
* If a higher number, use it to skip non-present devices
*/
if (mp->mscp_unit > unit)
unit = mp->mscp_unit;
switch (mp->mscp_status & M_ST_MASK) {
case M_ST_SUCCESS: /* worked */
case M_ST_AVAILABLE: /* found another drive */
break; /* use it */
case M_ST_OFFLINE:
/*
* Figure out why it is off line. It may be because
* it is nonexistent, or because it is spun down, or
* for some other reason.
*/
switch (mp->mscp_status & ~M_ST_MASK) {
case M_OFFLINE_UNKNOWN:
/*
* No such drive, and there are none with
* higher unit numbers either, if we are
* using M_GUM_NEXTUNIT.
*/
mi->mi_ierr = 3;
break; /* return */
case M_OFFLINE_UNMOUNTED:
/*
* The drive is not spun up. Use it anyway.
*
* N.B.: this seems to be a common occurrence
* after a power failure. The first attempt
* to bring it on line seems to spin it up
* (and thus takes several minutes). Perhaps
* we should note here that the on-line may
* take longer than usual.
*/
break;
default:
/*
* In service, or something else unusable.
*/
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;
}
}
}
/*
* The ctlr gets initialised, normally after boot but may also be
* done if the ctlr gets in an unknown state. Returns 1 if init
* fails, 0 otherwise.
*/
int
mscp_init(struct mscp_softc *mi)
{
struct mscp *mp;
volatile int i;
int status, count;
unsigned int j = 0;
/*
* While we are thinking about it, reset the next command
* and response indices.
*/
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); /* Kick off */;
status = mscp_waitstep(mi, MP_STEP1, MP_STEP1);/* Wait to it wakes up */
if (status == 0)
return 1; /* Init failed */
if (READ_SA & MP_ERR) {
(*mi->mi_mc->mc_saerror)(device_parent(mi->mi_dev), 0);
return 1;
}
/* step3 */
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 /* XXX */
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;
/*
* Set up all necessary info in the bus softc struct, get a
* mscp packet and set characteristics for this controller.
*/
mi->mi_credits = MSCP_MINCREDITS + 1;
mp = mscp_getcp(mi, MSCP_DONTWAIT);
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;
}
/*
* Initialise the various data structures that control the mscp protocol.
*/
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;
}
}
if (mi->mi_flags & MSC_IGNOREINTR)
return;
/*
* Check for response and command ring transitions.
*/
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 there are any not-yet-handled request, try them now.
*/
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;
}
/*
* common strategy routine for all types of MSCP devices.
*/
void
mscp_strategy(struct buf *bp, device_t usc)
{
struct mscp_softc *mi = device_private(usc);
int s = spluba();
while ((bp = bufq_peek(mi->mi_resq)) != NULL) {
/*
* Ok; we are ready to try to start a xfer. Get a MSCP packet
* and try to start...
*/
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)));
/*
* By some (strange) reason we didn't get a MSCP packet.
* Just return and wait for free packets.
*/
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");
/*
* Set up the MSCP packet and ask the ctlr to start.
*/
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);
}
}
/*
* Fill in the MSCP packet and move the buffer to the I/O wait queue.
*/
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
/*
* Dump the entire contents of an MSCP packet in hex. Mainly useful
* for debugging....
*/
void
mscp_hexdump(struct mscp *mp)
{
long *p = (long *) mp;
int i = mp->mscp_msglen;
if (i > 256) /* sanity */
i = 256;
i /= sizeof (*p); /* ASSUMES MULTIPLE OF sizeof(long) */
while (--i >= 0)
printf("0x%x ", (int)*p++);
printf("\n");
}
#endif
/*
* MSCP error reporting
*/
/*
* Messages for the various subcodes.
*/
static char unknown_msg[] = "unknown subcode";
/*
* Print the decoded error event from an MSCP error datagram.
*/
void
mscp_printevent(struct mscp *mp)
{
int event = mp->mscp_event;
struct code_decode *cdc;
int c, sc;
const char *cm, *scm;
/*
* The code is the lower six bits of the event number (aka
* status). If that is 6 (write protect), the subcode is in
* bits 12-15; otherwise, it is in bits 5-11.
* I WONDER WHAT THE OTHER BITS ARE FOR. IT SURE WOULD BE
* NICE IF DEC SOLD DOCUMENTATION FOR THEIR OWN CONTROLLERS.
*/
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"
};
/*
* Print the code and logical block number for an error packet.
* THIS IS PROBABLY PECULIAR TO DISK DRIVES. IT SURE WOULD BE
* NICE IF DEC SOLD DOCUMENTATION FOR THEIR OWN CONTROLLERS.
*/
int
mscp_decodeerror(const char *name, struct mscp *mp, struct mscp_softc *mi)
{
int issoft;
/*
* We will get three sdi errors of type 11 after autoconfig
* is finished; depending of searching for non-existing units.
* How can we avoid this???
*/
if (((mp->mscp_event & M_ST_MASK) == 11) && (mi->mi_ierr++ < 3))
return 1;
/*
* For bad blocks, mp->mscp_erd.erd_hdr identifies a code and
* the logical block number. Code 0 is a regular block; code 6
* is a replacement block. The remaining codes are currently
* undefined. The code is in the upper four bits of the header
* (bits 0-27 are the lbn).
*/
issoft = mp->mscp_flags & (M_LF_SUCC | M_LF_CONT);
#define BADCODE(h) (codemsg[(unsigned)(h) >> 28])
#define BADLBN(h) ((h) & 0xfffffff)