/*
* This is reported to fix some odd failures when disklabeling
* SCSI disks in SYS_INST.
*/
#define SLOWSCSI
/*
* Copyright (c) 1988 University of Utah.
* Copyright (c) 1990, 1993
* The Regents of the University of California. All rights reserved.
*
* This code is derived from software contributed to Berkeley by
* Van Jacobson of Lawrence Berkeley Laboratory and the Systems
* Programming Group of the University of Utah Computer Science Department.
*
* 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.
*
* from: Utah $Hdr: scsi.c 1.3 90/01/27$
*
* @(#)scsi.c 8.1 (Berkeley) 6/10/93
*/
/* wait for xfer to start or svc_req interrupt */
while ((hd->scsi_ssts & SSTS_BUSY) == 0) {
if (hd->scsi_ints || --wait < 0)
return 0;
DELAY(1);
}
return 1;
}
static int
ixfer_out(volatile struct scsidevice *hd, int len, uint8_t *buf)
{
int wait = scsi_data_wait;
for (; len > 0; --len) {
while (hd->scsi_ssts & SSTS_DREG_FULL) {
if (hd->scsi_ints || --wait < 0)
return len;
DELAY(1);
}
hd->scsi_dreg = *buf++;
}
return 0;
}
static int
ixfer_in(volatile struct scsidevice *hd, int len, uint8_t *buf)
{
int wait = scsi_data_wait;
for (; len > 0; --len) {
while (hd->scsi_ssts & SSTS_DREG_EMPTY) {
if (hd->scsi_ints || --wait < 0) {
while (! (hd->scsi_ssts & SSTS_DREG_EMPTY)) {
*buf++ = hd->scsi_dreg;
--len;
}
return len;
}
DELAY(1);
}
*buf++ = hd->scsi_dreg;
}
return len;
}
static int
scsiicmd(struct scsi_softc *hs, int target, uint8_t *cbuf, int clen,
uint8_t *buf, int len, uint8_t xferphase)
{
volatile struct scsidevice *hd = (void *)hs->sc_addr;
uint8_t phase, ints;
int wait;
/* select the SCSI bus (it's an error if bus isn't free) */
if (issue_select(hd, target, hs->sc_scsi_addr))
return -2;
if (wait_for_select(hd))
return -2;
/*
* Wait for a phase change (or error) then let the device
* sequence us through the various SCSI phases.
*/
hs->sc_stat = -1;
phase = CMD_PHASE;
while (1) {
wait = scsi_cmd_wait;
switch (phase) {
case CMD_PHASE:
if (ixfer_start(hd, clen, phase, wait))
if (ixfer_out(hd, clen, cbuf))
goto abort;
phase = xferphase;
break;
case DATA_IN_PHASE:
if (len <= 0)
goto abort;
wait = scsi_data_wait;
if (ixfer_start(hd, len, phase, wait) ||
!(hd->scsi_ssts & SSTS_DREG_EMPTY))
ixfer_in(hd, len, buf);
phase = STATUS_PHASE;
break;
case DATA_OUT_PHASE:
if (len <= 0)
goto abort;
wait = scsi_data_wait;
if (ixfer_start(hd, len, phase, wait))
if (ixfer_out(hd, len, buf))
goto abort;
phase = STATUS_PHASE;
break;
default:
printf("scsi%d: unexpected scsi phase %d\n",
hs - scsi_softc, phase);
goto abort;
}
#ifdef SLOWSCSI
/*
* XXX we have weird transient problems with booting from
* slow scsi disks on fast machines. I have never been
* able to pin the problem down, but a large delay here
* seems to always work.
*/
DELAY(1000);
#endif
/* wait for last command to complete */
while ((ints = hd->scsi_ints) == 0) {
if (--wait < 0)
goto abort;
DELAY(1);
}
hd->scsi_ints = ints;
if (ints & INTS_SRV_REQ)
phase = hd->scsi_psns & PHASE;
else if (ints & INTS_DISCON)
goto out;
else if ((ints & INTS_CMD_DONE) == 0)
goto abort;
}
abort:
scsiabort(hs, hd);
out:
return hs->sc_stat;
}
int
scsi_test_unit_rdy(int ctlr, int slave)
{
struct scsi_softc *hs = &scsi_softc[ctlr];
static struct scsi_cdb6 cdb = { CMD_TEST_UNIT_READY };