1*53019Smarc #ifndef DEBUG 2*53019Smarc #define DEBUG 3*53019Smarc #endif 441480Smckusick /* 541480Smckusick * Copyright (c) 1990 The Regents of the University of California. 641480Smckusick * All rights reserved. 741480Smckusick * 841480Smckusick * This code is derived from software contributed to Berkeley by 941480Smckusick * Van Jacobson of Lawrence Berkeley Laboratory. 1041480Smckusick * 1141480Smckusick * %sccs.include.redist.c% 1241480Smckusick * 13*53019Smarc * @(#)scsi.c 7.6 (Berkeley) 03/19/92 1441480Smckusick */ 1541480Smckusick 1641480Smckusick /* 1741480Smckusick * HP9000/3xx 98658 SCSI host adaptor driver. 1841480Smckusick */ 1941480Smckusick #include "scsi.h" 2041480Smckusick #if NSCSI > 0 2141480Smckusick 2241480Smckusick #ifndef lint 2346285Smckusick static char rcsid[] = "$Header: scsi.c,v 1.4 91/01/17 12:50:18 mike Exp $"; 2441480Smckusick #endif 2541480Smckusick 2645788Sbostic #include "sys/param.h" 2745788Sbostic #include "sys/systm.h" 2845788Sbostic #include "sys/buf.h" 2941480Smckusick #include "device.h" 3045788Sbostic 3141480Smckusick #include "scsivar.h" 3241480Smckusick #include "scsireg.h" 3341480Smckusick #include "dmavar.h" 3441480Smckusick 3545788Sbostic #include "../include/cpu.h" 3645788Sbostic #include "../hp300/isr.h" 3741480Smckusick 3846285Smckusick /* 3946285Smckusick * SCSI delays 4046285Smckusick * In u-seconds, primarily for state changes on the SPC. 4146285Smckusick */ 4246285Smckusick #define SCSI_CMD_WAIT 1000 /* wait per step of 'immediate' cmds */ 4346285Smckusick #define SCSI_DATA_WAIT 1000 /* wait per data in/out step */ 4446285Smckusick #define SCSI_INIT_WAIT 50000 /* wait per step (both) during init */ 4546285Smckusick 4641480Smckusick extern void isrlink(); 4741480Smckusick extern void _insque(); 4841480Smckusick extern void _remque(); 4941480Smckusick 5041480Smckusick int scsiinit(), scsigo(), scsiintr(), scsixfer(); 5141480Smckusick void scsistart(), scsidone(), scsifree(), scsireset(); 5241480Smckusick struct driver scsidriver = { 5341480Smckusick scsiinit, "scsi", (int (*)())scsistart, scsigo, scsiintr, 5441480Smckusick (int (*)())scsidone, 5541480Smckusick }; 5641480Smckusick 5741480Smckusick struct scsi_softc scsi_softc[NSCSI]; 5841480Smckusick struct isr scsi_isr[NSCSI]; 5941480Smckusick 6046285Smckusick int scsi_cmd_wait = SCSI_CMD_WAIT; 6146285Smckusick int scsi_data_wait = SCSI_DATA_WAIT; 6246285Smckusick int scsi_init_wait = SCSI_INIT_WAIT; 6346285Smckusick 6441480Smckusick int scsi_nosync = 1; /* inhibit sync xfers if 1 */ 6545514Smckusick int scsi_pridma = 0; /* use "priority" dma */ 6641480Smckusick 6741480Smckusick #ifdef DEBUG 6841480Smckusick int scsi_debug = 0; 6941480Smckusick #define WAITHIST 7041480Smckusick #endif 7141480Smckusick 7241480Smckusick #ifdef WAITHIST 7345514Smckusick #define MAXWAIT 1022 7441480Smckusick u_int ixstart_wait[MAXWAIT+2]; 7541480Smckusick u_int ixin_wait[MAXWAIT+2]; 7641480Smckusick u_int ixout_wait[MAXWAIT+2]; 7741480Smckusick u_int mxin_wait[MAXWAIT+2]; 7846285Smckusick u_int mxin2_wait[MAXWAIT+2]; 7941480Smckusick u_int cxin_wait[MAXWAIT+2]; 8041480Smckusick u_int fxfr_wait[MAXWAIT+2]; 8141480Smckusick u_int sgo_wait[MAXWAIT+2]; 8241480Smckusick #define HIST(h,w) (++h[((w)>MAXWAIT? MAXWAIT : ((w) < 0 ? -1 : (w))) + 1]); 8341480Smckusick #else 8441480Smckusick #define HIST(h,w) 8541480Smckusick #endif 8641480Smckusick 8741480Smckusick #define b_cylin b_resid 8841480Smckusick 8941480Smckusick static void 9041480Smckusick scsiabort(hs, hd, where) 9141480Smckusick register struct scsi_softc *hs; 9241480Smckusick volatile register struct scsidevice *hd; 9341480Smckusick char *where; 9441480Smckusick { 9541480Smckusick int len; 96*53019Smarc int maxtries; /* XXX - kludge till I understand whats *supposed* to happen */ 97*53019Smarc int startlen; /* XXX - kludge till I understand whats *supposed* to happen */ 9841480Smckusick u_char junk; 9941480Smckusick 10041480Smckusick printf("scsi%d: abort from %s: phase=0x%x, ssts=0x%x, ints=0x%x\n", 10141480Smckusick hs->sc_hc->hp_unit, where, hd->scsi_psns, hd->scsi_ssts, 10241480Smckusick hd->scsi_ints); 10341480Smckusick 10441480Smckusick hd->scsi_ints = hd->scsi_ints; 10541480Smckusick hd->scsi_csr = 0; 10641480Smckusick if (hd->scsi_psns == 0 || (hd->scsi_ssts & SSTS_INITIATOR) == 0) 10741480Smckusick /* no longer connected to scsi target */ 10841480Smckusick return; 10941480Smckusick 11041480Smckusick /* get the number of bytes remaining in current xfer + fudge */ 11141480Smckusick len = (hd->scsi_tch << 16) | (hd->scsi_tcm << 8) | hd->scsi_tcl; 11241480Smckusick 11341480Smckusick /* for that many bus cycles, try to send an abort msg */ 114*53019Smarc for (startlen = (len += 1024); (hd->scsi_ssts & SSTS_INITIATOR) && --len >= 0; ) { 11541480Smckusick hd->scsi_scmd = SCMD_SET_ATN; 116*53019Smarc maxtries = 1000; 11741480Smckusick while ((hd->scsi_psns & PSNS_REQ) == 0) { 11841480Smckusick if (! (hd->scsi_ssts & SSTS_INITIATOR)) 11941480Smckusick goto out; 12041480Smckusick DELAY(1); 121*53019Smarc if (--maxtries == 0) { 122*53019Smarc printf("-- scsiabort gave up after 1000 tries (startlen = %d len = %d)\n", 123*53019Smarc startlen, len); 124*53019Smarc goto out2; 125*53019Smarc } 126*53019Smarc 12741480Smckusick } 128*53019Smarc out2: 12941480Smckusick if ((hd->scsi_psns & PHASE) == MESG_OUT_PHASE) 13041480Smckusick hd->scsi_scmd = SCMD_RST_ATN; 13141480Smckusick hd->scsi_pctl = hd->scsi_psns & PHASE; 13241480Smckusick if (hd->scsi_psns & PHASE_IO) { 13341480Smckusick /* one of the input phases - read & discard a byte */ 13441480Smckusick hd->scsi_scmd = SCMD_SET_ACK; 13541480Smckusick if (hd->scsi_tmod == 0) 13641480Smckusick while (hd->scsi_psns & PSNS_REQ) 13741480Smckusick DELAY(1); 13841480Smckusick junk = hd->scsi_temp; 13941480Smckusick } else { 14041480Smckusick /* one of the output phases - send an abort msg */ 14141480Smckusick hd->scsi_temp = MSG_ABORT; 14241480Smckusick hd->scsi_scmd = SCMD_SET_ACK; 14341480Smckusick if (hd->scsi_tmod == 0) 14441480Smckusick while (hd->scsi_psns & PSNS_REQ) 14541480Smckusick DELAY(1); 14641480Smckusick } 14741480Smckusick hd->scsi_scmd = SCMD_RST_ACK; 14841480Smckusick } 14941480Smckusick out: 15041480Smckusick /* 15141480Smckusick * Either the abort was successful & the bus is disconnected or 15241480Smckusick * the device didn't listen. If the latter, announce the problem. 15341480Smckusick * Either way, reset the card & the SPC. 15441480Smckusick */ 15541480Smckusick if (len < 0 && hs) 15641480Smckusick printf("scsi%d: abort failed. phase=0x%x, ssts=0x%x\n", 15741480Smckusick hs->sc_hc->hp_unit, hd->scsi_psns, hd->scsi_ssts); 15841480Smckusick 15941480Smckusick if (! ((junk = hd->scsi_ints) & INTS_RESEL)) { 16041480Smckusick hd->scsi_sctl |= SCTL_CTRLRST; 16141480Smckusick DELAY(1); 16241480Smckusick hd->scsi_sctl &=~ SCTL_CTRLRST; 16341480Smckusick hd->scsi_hconf = 0; 16441480Smckusick hd->scsi_ints = hd->scsi_ints; 16541480Smckusick } 16641480Smckusick } 16741480Smckusick 16846285Smckusick /* 16946285Smckusick * XXX Set/reset long delays. 17046285Smckusick * 17146285Smckusick * if delay == 0, reset default delays 17246285Smckusick * if delay < 0, set both delays to default long initialization values 17346285Smckusick * if delay > 0, set both delays to this value 17446285Smckusick * 17546285Smckusick * Used when a devices is expected to respond slowly (e.g. during 17646285Smckusick * initialization). 17746285Smckusick */ 17846285Smckusick void 17946285Smckusick scsi_delay(delay) 18046285Smckusick int delay; 18146285Smckusick { 18246285Smckusick static int saved_cmd_wait, saved_data_wait; 18346285Smckusick 18446285Smckusick if (delay) { 18546285Smckusick saved_cmd_wait = scsi_cmd_wait; 18646285Smckusick saved_data_wait = scsi_data_wait; 18746285Smckusick if (delay > 0) 18846285Smckusick scsi_cmd_wait = scsi_data_wait = delay; 18946285Smckusick else 19046285Smckusick scsi_cmd_wait = scsi_data_wait = scsi_init_wait; 19146285Smckusick } else { 19246285Smckusick scsi_cmd_wait = saved_cmd_wait; 19346285Smckusick scsi_data_wait = saved_data_wait; 19446285Smckusick } 19546285Smckusick } 19646285Smckusick 19741480Smckusick int 19841480Smckusick scsiinit(hc) 19941480Smckusick register struct hp_ctlr *hc; 20041480Smckusick { 20141480Smckusick register struct scsi_softc *hs = &scsi_softc[hc->hp_unit]; 20241480Smckusick register struct scsidevice *hd = (struct scsidevice *)hc->hp_addr; 20341480Smckusick 20441480Smckusick if ((hd->scsi_id & ID_MASK) != SCSI_ID) 20541480Smckusick return(0); 20641480Smckusick hc->hp_ipl = SCSI_IPL(hd->scsi_csr); 20741480Smckusick hs->sc_hc = hc; 20841480Smckusick hs->sc_dq.dq_unit = hc->hp_unit; 20941480Smckusick hs->sc_dq.dq_driver = &scsidriver; 21041480Smckusick hs->sc_sq.dq_forw = hs->sc_sq.dq_back = &hs->sc_sq; 21141480Smckusick scsi_isr[hc->hp_unit].isr_intr = scsiintr; 21241480Smckusick scsi_isr[hc->hp_unit].isr_ipl = hc->hp_ipl; 21341480Smckusick scsi_isr[hc->hp_unit].isr_arg = hc->hp_unit; 21441480Smckusick isrlink(&scsi_isr[hc->hp_unit]); 21541480Smckusick scsireset(hc->hp_unit); 21641480Smckusick return(1); 21741480Smckusick } 21841480Smckusick 21941480Smckusick void 22041480Smckusick scsireset(unit) 22141480Smckusick register int unit; 22241480Smckusick { 22341480Smckusick register struct scsi_softc *hs = &scsi_softc[unit]; 22441480Smckusick volatile register struct scsidevice *hd = 22541480Smckusick (struct scsidevice *)hs->sc_hc->hp_addr; 22641480Smckusick u_int i; 22741480Smckusick 22841480Smckusick if (hs->sc_flags & SCSI_ALIVE) 22941480Smckusick scsiabort(hs, hd, "reset"); 23041480Smckusick 23141480Smckusick printf("scsi%d: ", unit); 23241480Smckusick 23341480Smckusick hd->scsi_id = 0xFF; 23441480Smckusick DELAY(100); 23541480Smckusick /* 23641480Smckusick * Disable interrupts then reset the FUJI chip. 23741480Smckusick */ 23841480Smckusick hd->scsi_csr = 0; 23941480Smckusick hd->scsi_sctl = SCTL_DISABLE | SCTL_CTRLRST; 24041480Smckusick hd->scsi_scmd = 0; 24141480Smckusick hd->scsi_tmod = 0; 24241480Smckusick hd->scsi_pctl = 0; 24341480Smckusick hd->scsi_temp = 0; 24441480Smckusick hd->scsi_tch = 0; 24541480Smckusick hd->scsi_tcm = 0; 24641480Smckusick hd->scsi_tcl = 0; 24741480Smckusick hd->scsi_ints = 0; 24841480Smckusick 24941480Smckusick if ((hd->scsi_id & ID_WORD_DMA) == 0) { 25041480Smckusick hs->sc_flags |= SCSI_DMA32; 25141480Smckusick printf("32 bit dma, "); 25241480Smckusick } 25341480Smckusick 25441480Smckusick /* Determine Max Synchronous Transfer Rate */ 25541480Smckusick if (scsi_nosync) 25641480Smckusick i = 3; 25741480Smckusick else 25841480Smckusick i = SCSI_SYNC_XFER(hd->scsi_hconf); 25941480Smckusick switch (i) { 26041480Smckusick case 0: 26141480Smckusick hs->sc_sync = TMOD_SYNC | 0x3e; /* 250 nsecs */ 26241480Smckusick printf("250ns sync"); 26341480Smckusick break; 26441480Smckusick case 1: 26541480Smckusick hs->sc_sync = TMOD_SYNC | 0x5e; /* 375 nsecs */ 26641480Smckusick printf("375ns sync"); 26741480Smckusick break; 26841480Smckusick case 2: 26941480Smckusick hs->sc_sync = TMOD_SYNC | 0x7d; /* 500 nsecs */ 27041480Smckusick printf("500ns sync"); 27141480Smckusick break; 27241480Smckusick case 3: 27341480Smckusick hs->sc_sync = 0; 27441480Smckusick printf("async"); 27541480Smckusick break; 27641480Smckusick } 27741480Smckusick 27841480Smckusick /* 27941480Smckusick * Configure the FUJI chip with its SCSI address, all 28041480Smckusick * interrupts enabled & appropriate parity. 28141480Smckusick */ 28241480Smckusick i = (~hd->scsi_hconf) & 0x7; 28341480Smckusick hs->sc_scsi_addr = 1 << i; 28441480Smckusick hd->scsi_bdid = i; 28541480Smckusick if (hd->scsi_hconf & HCONF_PARITY) 28641480Smckusick hd->scsi_sctl = SCTL_DISABLE | SCTL_ABRT_ENAB | 28741480Smckusick SCTL_SEL_ENAB | SCTL_RESEL_ENAB | 28841480Smckusick SCTL_INTR_ENAB | SCTL_PARITY_ENAB; 28941480Smckusick else { 29041480Smckusick hd->scsi_sctl = SCTL_DISABLE | SCTL_ABRT_ENAB | 29141480Smckusick SCTL_SEL_ENAB | SCTL_RESEL_ENAB | 29241480Smckusick SCTL_INTR_ENAB; 29341480Smckusick printf(", no parity"); 29441480Smckusick } 29541480Smckusick hd->scsi_sctl &=~ SCTL_DISABLE; 29641480Smckusick 29741480Smckusick printf(", scsi id %d\n", i); 29841480Smckusick hs->sc_flags |= SCSI_ALIVE; 29941480Smckusick } 30041480Smckusick 30141480Smckusick static void 30241480Smckusick scsierror(hs, hd, ints) 30341480Smckusick register struct scsi_softc *hs; 30441480Smckusick volatile register struct scsidevice *hd; 30541480Smckusick u_char ints; 30641480Smckusick { 30741480Smckusick int unit = hs->sc_hc->hp_unit; 30841480Smckusick char *sep = ""; 30941480Smckusick 31041480Smckusick printf("scsi%d: ", unit); 31141480Smckusick if (ints & INTS_RST) { 31241480Smckusick DELAY(100); 31341480Smckusick if (hd->scsi_hconf & HCONF_SD) 31441480Smckusick printf("spurious RST interrupt"); 31541480Smckusick else 31641480Smckusick printf("hardware error - check fuse"); 31741480Smckusick sep = ", "; 31841480Smckusick } 31941480Smckusick if ((ints & INTS_HARD_ERR) || hd->scsi_serr) { 32041480Smckusick if (hd->scsi_serr & SERR_SCSI_PAR) { 32141480Smckusick printf("%sparity err", sep); 32241480Smckusick sep = ", "; 32341480Smckusick } 32441480Smckusick if (hd->scsi_serr & SERR_SPC_PAR) { 32541480Smckusick printf("%sSPC parity err", sep); 32641480Smckusick sep = ", "; 32741480Smckusick } 32841480Smckusick if (hd->scsi_serr & SERR_TC_PAR) { 32941480Smckusick printf("%sTC parity err", sep); 33041480Smckusick sep = ", "; 33141480Smckusick } 33241480Smckusick if (hd->scsi_serr & SERR_PHASE_ERR) { 33341480Smckusick printf("%sphase err", sep); 33441480Smckusick sep = ", "; 33541480Smckusick } 33641480Smckusick if (hd->scsi_serr & SERR_SHORT_XFR) { 33741480Smckusick printf("%ssync short transfer err", sep); 33841480Smckusick sep = ", "; 33941480Smckusick } 34041480Smckusick if (hd->scsi_serr & SERR_OFFSET) { 34141480Smckusick printf("%ssync offset error", sep); 34241480Smckusick sep = ", "; 34341480Smckusick } 34441480Smckusick } 34541480Smckusick if (ints & INTS_TIMEOUT) 34641480Smckusick printf("%sSPC select timeout error", sep); 34741480Smckusick if (ints & INTS_SRV_REQ) 34841480Smckusick printf("%sspurious SRV_REQ interrupt", sep); 34941480Smckusick if (ints & INTS_CMD_DONE) 35041480Smckusick printf("%sspurious CMD_DONE interrupt", sep); 35141480Smckusick if (ints & INTS_DISCON) 35241480Smckusick printf("%sspurious disconnect interrupt", sep); 35341480Smckusick if (ints & INTS_RESEL) 35441480Smckusick printf("%sspurious reselect interrupt", sep); 35541480Smckusick if (ints & INTS_SEL) 35641480Smckusick printf("%sspurious select interrupt", sep); 35741480Smckusick printf("\n"); 35841480Smckusick } 35941480Smckusick 36041480Smckusick static int 36141480Smckusick issue_select(hd, target, our_addr) 36241480Smckusick volatile register struct scsidevice *hd; 36341480Smckusick u_char target, our_addr; 36441480Smckusick { 36541480Smckusick if (hd->scsi_ssts & (SSTS_INITIATOR|SSTS_TARGET|SSTS_BUSY)) 36641480Smckusick return (1); 36741480Smckusick 36841480Smckusick if (hd->scsi_ints & INTS_DISCON) 36941480Smckusick hd->scsi_ints = INTS_DISCON; 37041480Smckusick 37141480Smckusick hd->scsi_pctl = 0; 37241480Smckusick hd->scsi_temp = (1 << target) | our_addr; 37341480Smckusick /* select timeout is hardcoded to 2ms */ 37441480Smckusick hd->scsi_tch = 0; 37541480Smckusick hd->scsi_tcm = 32; 37641480Smckusick hd->scsi_tcl = 4; 37741480Smckusick 37841480Smckusick hd->scsi_scmd = SCMD_SELECT; 37941480Smckusick return (0); 38041480Smckusick } 38141480Smckusick 38241480Smckusick static int 38341480Smckusick wait_for_select(hd) 38441480Smckusick volatile register struct scsidevice *hd; 38541480Smckusick { 38641480Smckusick u_char ints; 38741480Smckusick 38841480Smckusick while ((ints = hd->scsi_ints) == 0) 38941480Smckusick DELAY(1); 39041480Smckusick hd->scsi_ints = ints; 39141480Smckusick return (!(hd->scsi_ssts & SSTS_INITIATOR)); 39241480Smckusick } 39341480Smckusick 39441480Smckusick static int 39541480Smckusick ixfer_start(hd, len, phase, wait) 39641480Smckusick volatile register struct scsidevice *hd; 39741480Smckusick int len; 39841480Smckusick u_char phase; 39941480Smckusick register int wait; 40041480Smckusick { 40141480Smckusick 40241480Smckusick hd->scsi_tch = len >> 16; 40341480Smckusick hd->scsi_tcm = len >> 8; 40441480Smckusick hd->scsi_tcl = len; 40541480Smckusick hd->scsi_pctl = phase; 40641480Smckusick hd->scsi_tmod = 0; /*XXX*/ 40741480Smckusick hd->scsi_scmd = SCMD_XFR | SCMD_PROG_XFR; 40841480Smckusick 40941480Smckusick /* wait for xfer to start or svc_req interrupt */ 41041480Smckusick while ((hd->scsi_ssts & SSTS_BUSY) == 0) { 41141480Smckusick if (hd->scsi_ints || --wait < 0) { 41241480Smckusick #ifdef DEBUG 41341480Smckusick if (scsi_debug) 41441480Smckusick printf("ixfer_start fail: i%x, w%d\n", 41541480Smckusick hd->scsi_ints, wait); 41641480Smckusick #endif 41741480Smckusick HIST(ixstart_wait, wait) 41841480Smckusick return (0); 41941480Smckusick } 42041480Smckusick DELAY(1); 42141480Smckusick } 42241480Smckusick HIST(ixstart_wait, wait) 42341480Smckusick return (1); 42441480Smckusick } 42541480Smckusick 42641480Smckusick static int 42741480Smckusick ixfer_out(hd, len, buf) 42841480Smckusick volatile register struct scsidevice *hd; 42941480Smckusick int len; 43041480Smckusick register u_char *buf; 43141480Smckusick { 43241480Smckusick register int wait = scsi_data_wait; 43341480Smckusick 43441480Smckusick for (; len > 0; --len) { 43541480Smckusick while (hd->scsi_ssts & SSTS_DREG_FULL) { 43641480Smckusick if (hd->scsi_ints || --wait < 0) { 43741480Smckusick #ifdef DEBUG 43841480Smckusick if (scsi_debug) 43941480Smckusick printf("ixfer_out fail: l%d i%x w%d\n", 44041480Smckusick len, hd->scsi_ints, wait); 44141480Smckusick #endif 44241480Smckusick HIST(ixout_wait, wait) 44341480Smckusick return (len); 44441480Smckusick } 44541480Smckusick DELAY(1); 44641480Smckusick } 44741480Smckusick hd->scsi_dreg = *buf++; 44841480Smckusick } 44941480Smckusick HIST(ixout_wait, wait) 45041480Smckusick return (0); 45141480Smckusick } 45241480Smckusick 45341480Smckusick static void 45441480Smckusick ixfer_in(hd, len, buf) 45541480Smckusick volatile register struct scsidevice *hd; 45641480Smckusick int len; 45741480Smckusick register u_char *buf; 45841480Smckusick { 45941480Smckusick register int wait = scsi_data_wait; 46041480Smckusick 46141480Smckusick for (; len > 0; --len) { 46241480Smckusick while (hd->scsi_ssts & SSTS_DREG_EMPTY) { 46341480Smckusick if (hd->scsi_ints || --wait < 0) { 46441480Smckusick while (! (hd->scsi_ssts & SSTS_DREG_EMPTY)) { 46541480Smckusick *buf++ = hd->scsi_dreg; 46641480Smckusick --len; 46741480Smckusick } 46841480Smckusick #ifdef DEBUG 46941480Smckusick if (scsi_debug) 47041480Smckusick printf("ixfer_in fail: l%d i%x w%d\n", 47141480Smckusick len, hd->scsi_ints, wait); 47241480Smckusick #endif 47341480Smckusick HIST(ixin_wait, wait) 47441480Smckusick return; 47541480Smckusick } 47641480Smckusick DELAY(1); 47741480Smckusick } 47841480Smckusick *buf++ = hd->scsi_dreg; 47941480Smckusick } 48041480Smckusick HIST(ixin_wait, wait) 48141480Smckusick } 48241480Smckusick 48341480Smckusick static int 48441480Smckusick mxfer_in(hd, len, buf, phase) 48541480Smckusick volatile register struct scsidevice *hd; 48641480Smckusick register int len; 48741480Smckusick register u_char *buf; 48841480Smckusick register u_char phase; 48941480Smckusick { 49041480Smckusick register int wait = scsi_cmd_wait; 49141480Smckusick register int i; 49241480Smckusick 49341480Smckusick hd->scsi_tmod = 0; 49441480Smckusick for (i = 0; i < len; ++i) { 49541480Smckusick /* 49646285Smckusick * manual sez: reset ATN before ACK is sent. 49746285Smckusick */ 49846285Smckusick if (hd->scsi_psns & PSNS_ATN) 49946285Smckusick hd->scsi_scmd = SCMD_RST_ATN; 50046285Smckusick /* 50141480Smckusick * wait for the request line (which says the target 50241480Smckusick * wants to give us data). If the phase changes while 50341480Smckusick * we're waiting, we're done. 50441480Smckusick */ 50541480Smckusick while ((hd->scsi_psns & PSNS_REQ) == 0) { 50641480Smckusick if (--wait < 0) { 50741480Smckusick HIST(mxin_wait, wait) 50841480Smckusick return (-1); 50941480Smckusick } 51041480Smckusick if ((hd->scsi_psns & PHASE) != phase || 51141480Smckusick (hd->scsi_ssts & SSTS_INITIATOR) == 0) 51241480Smckusick goto out; 51341480Smckusick 51441480Smckusick DELAY(1); 51541480Smckusick } 51641480Smckusick /* 51741480Smckusick * set ack (which says we're ready for the data, wait for 51841480Smckusick * req to go away (target says data is available), grab the 51941480Smckusick * data, then reset ack (say we've got the data). 52041480Smckusick */ 52141480Smckusick hd->scsi_pctl = phase; 52241480Smckusick hd->scsi_scmd = SCMD_SET_ACK; 52341480Smckusick while (hd->scsi_psns & PSNS_REQ) { 52441480Smckusick if (--wait < 0) { 52541480Smckusick HIST(mxin_wait, wait) 52641480Smckusick return (-2); 52741480Smckusick } 52841480Smckusick DELAY(1); 52941480Smckusick } 53041480Smckusick *buf++ = hd->scsi_temp; 53141480Smckusick hd->scsi_scmd = SCMD_RST_ACK; 53241480Smckusick } 53341480Smckusick out: 53441480Smckusick HIST(mxin_wait, wait) 53546285Smckusick /* 53646285Smckusick * Wait for manual transfer to finish. 53746285Smckusick * Avoids occasional "unexpected phase" errors in finishxfer 53846285Smckusick * formerly addressed by per-slave delays. 53946285Smckusick */ 54046285Smckusick wait = scsi_cmd_wait; 54146285Smckusick while ((hd->scsi_ssts & SSTS_ACTIVE) == SSTS_INITIATOR) { 54246285Smckusick if (--wait < 0) 54346285Smckusick break; 54446285Smckusick DELAY(1); 54546285Smckusick } 54646285Smckusick HIST(mxin2_wait, wait) 54741480Smckusick return (i); 54841480Smckusick } 54941480Smckusick 55041480Smckusick /* 55141480Smckusick * SCSI 'immediate' command: issue a command to some SCSI device 55241480Smckusick * and get back an 'immediate' response (i.e., do programmed xfer 55341480Smckusick * to get the response data). 'cbuf' is a buffer containing a scsi 55441480Smckusick * command of length clen bytes. 'buf' is a buffer of length 'len' 55541480Smckusick * bytes for data. The transfer direction is determined by the device 55641480Smckusick * (i.e., by the scsi bus data xfer phase). If 'len' is zero, the 55741480Smckusick * command must supply no data. 'xferphase' is the bus phase the 55841480Smckusick * caller expects to happen after the command is issued. It should 55941480Smckusick * be one of DATA_IN_PHASE, DATA_OUT_PHASE or STATUS_PHASE. 56041480Smckusick */ 56141480Smckusick static int 56241480Smckusick scsiicmd(hs, target, cbuf, clen, buf, len, xferphase) 56341480Smckusick struct scsi_softc *hs; 56441480Smckusick int target; 56541480Smckusick u_char *cbuf; 56641480Smckusick int clen; 56741480Smckusick u_char *buf; 56841480Smckusick int len; 56941480Smckusick u_char xferphase; 57041480Smckusick { 57141480Smckusick volatile register struct scsidevice *hd = 57241480Smckusick (struct scsidevice *)hs->sc_hc->hp_addr; 57341480Smckusick u_char phase, ints; 57441480Smckusick register int wait; 57541480Smckusick 57641480Smckusick /* select the SCSI bus (it's an error if bus isn't free) */ 57741480Smckusick if (issue_select(hd, target, hs->sc_scsi_addr)) 57841480Smckusick return (-1); 57941480Smckusick if (wait_for_select(hd)) 58041480Smckusick return (-1); 58141480Smckusick /* 58241480Smckusick * Wait for a phase change (or error) then let the device 58341480Smckusick * sequence us through the various SCSI phases. 58441480Smckusick */ 58541480Smckusick hs->sc_stat[0] = 0xff; 58641480Smckusick hs->sc_msg[0] = 0xff; 58741480Smckusick phase = CMD_PHASE; 58841480Smckusick while (1) { 58941480Smckusick wait = scsi_cmd_wait; 59041480Smckusick switch (phase) { 59141480Smckusick 59241480Smckusick case CMD_PHASE: 59341480Smckusick if (ixfer_start(hd, clen, phase, wait)) 59441480Smckusick if (ixfer_out(hd, clen, cbuf)) 59541480Smckusick goto abort; 59641480Smckusick phase = xferphase; 59741480Smckusick break; 59841480Smckusick 59941480Smckusick case DATA_IN_PHASE: 60041480Smckusick if (len <= 0) 60141480Smckusick goto abort; 60241480Smckusick wait = scsi_data_wait; 60341480Smckusick if (ixfer_start(hd, len, phase, wait) || 60441480Smckusick !(hd->scsi_ssts & SSTS_DREG_EMPTY)) 60541480Smckusick ixfer_in(hd, len, buf); 60641480Smckusick phase = STATUS_PHASE; 60741480Smckusick break; 60841480Smckusick 60941480Smckusick case DATA_OUT_PHASE: 61041480Smckusick if (len <= 0) 61141480Smckusick goto abort; 61241480Smckusick wait = scsi_data_wait; 61341480Smckusick if (ixfer_start(hd, len, phase, wait)) { 61441480Smckusick if (ixfer_out(hd, len, buf)) 61541480Smckusick goto abort; 61641480Smckusick } 61741480Smckusick phase = STATUS_PHASE; 61841480Smckusick break; 61941480Smckusick 62041480Smckusick case STATUS_PHASE: 62141480Smckusick wait = scsi_data_wait; 62241480Smckusick if (ixfer_start(hd, sizeof(hs->sc_stat), phase, wait) || 62341480Smckusick !(hd->scsi_ssts & SSTS_DREG_EMPTY)) 62441480Smckusick ixfer_in(hd, sizeof(hs->sc_stat), hs->sc_stat); 62541480Smckusick phase = MESG_IN_PHASE; 62641480Smckusick break; 62741480Smckusick 62841480Smckusick case MESG_IN_PHASE: 62941480Smckusick if (ixfer_start(hd, sizeof(hs->sc_msg), phase, wait) || 63041480Smckusick !(hd->scsi_ssts & SSTS_DREG_EMPTY)) { 63141480Smckusick ixfer_in(hd, sizeof(hs->sc_msg), hs->sc_msg); 63241480Smckusick hd->scsi_scmd = SCMD_RST_ACK; 63341480Smckusick } 63441480Smckusick phase = BUS_FREE_PHASE; 63541480Smckusick break; 63641480Smckusick 63741480Smckusick case BUS_FREE_PHASE: 63841480Smckusick goto out; 63941480Smckusick 64041480Smckusick default: 64141480Smckusick printf("scsi%d: unexpected phase %d in icmd from %d\n", 64241480Smckusick hs->sc_hc->hp_unit, phase, target); 64341480Smckusick goto abort; 64441480Smckusick } 64541480Smckusick /* wait for last command to complete */ 64641480Smckusick while ((ints = hd->scsi_ints) == 0) { 64741480Smckusick if (--wait < 0) { 64841480Smckusick HIST(cxin_wait, wait) 64941480Smckusick goto abort; 65041480Smckusick } 65141480Smckusick DELAY(1); 65241480Smckusick } 65341480Smckusick HIST(cxin_wait, wait) 65441480Smckusick hd->scsi_ints = ints; 65541480Smckusick if (ints & INTS_SRV_REQ) 65641480Smckusick phase = hd->scsi_psns & PHASE; 65741480Smckusick else if (ints & INTS_DISCON) 65841480Smckusick goto out; 65941480Smckusick else if ((ints & INTS_CMD_DONE) == 0) { 66041480Smckusick scsierror(hs, hd, ints); 66141480Smckusick goto abort; 66241480Smckusick } 66341480Smckusick } 66441480Smckusick abort: 66541480Smckusick scsiabort(hs, hd, "icmd"); 66641480Smckusick out: 66741480Smckusick return (hs->sc_stat[0]); 66841480Smckusick } 66941480Smckusick 67041480Smckusick /* 67141480Smckusick * Finish SCSI xfer command: After the completion interrupt from 67241480Smckusick * a read/write operation, sequence through the final phases in 67341480Smckusick * programmed i/o. This routine is a lot like scsiicmd except we 67441480Smckusick * skip (and don't allow) the select, cmd out and data in/out phases. 67541480Smckusick */ 67641480Smckusick static void 67741480Smckusick finishxfer(hs, hd, target) 67841480Smckusick struct scsi_softc *hs; 67941480Smckusick volatile register struct scsidevice *hd; 68041480Smckusick int target; 68141480Smckusick { 68241480Smckusick u_char phase, ints; 68341480Smckusick 68441480Smckusick /* 68541480Smckusick * We specified padding xfer so we ended with either a phase 68641480Smckusick * change interrupt (normal case) or an error interrupt (handled 68741480Smckusick * elsewhere). Reset the board dma logic then try to get the 68841480Smckusick * completion status & command done msg. The reset confuses 68941480Smckusick * the SPC REQ/ACK logic so we have to do any status/msg input 69041480Smckusick * operations via 'manual xfer'. 69141480Smckusick */ 69241480Smckusick if (hd->scsi_ssts & SSTS_BUSY) { 69341480Smckusick int wait = scsi_cmd_wait; 69441480Smckusick 69541480Smckusick /* wait for dma operation to finish */ 69641480Smckusick while (hd->scsi_ssts & SSTS_BUSY) { 69741480Smckusick if (--wait < 0) { 69841480Smckusick #ifdef DEBUG 69941480Smckusick if (scsi_debug) 70041480Smckusick printf("finishxfer fail: ssts %x\n", 70141480Smckusick hd->scsi_ssts); 70241480Smckusick #endif 70341480Smckusick HIST(fxfr_wait, wait) 70441480Smckusick goto abort; 70541480Smckusick } 70641480Smckusick } 70741480Smckusick HIST(fxfr_wait, wait) 70841480Smckusick } 70941480Smckusick hd->scsi_scmd |= SCMD_PROG_XFR; 71041480Smckusick hd->scsi_sctl |= SCTL_CTRLRST; 71141480Smckusick DELAY(1); 71241480Smckusick hd->scsi_sctl &=~ SCTL_CTRLRST; 71341480Smckusick hd->scsi_hconf = 0; 714*53019Smarc /* 715*53019Smarc * The following delay is definitely needed when trying to 716*53019Smarc * write on a write protected disk (in the optical jukebox anyways), 717*53019Smarc * but we shall see if other unexplained machine freezeups 718*53019Smarc * also stop occuring... A value of 5 seems to work but 719*53019Smarc * 10 seems safer considering the potential consequences. 720*53019Smarc */ 721*53019Smarc DELAY(10); 72241480Smckusick hs->sc_stat[0] = 0xff; 72341480Smckusick hs->sc_msg[0] = 0xff; 72441480Smckusick hd->scsi_csr = 0; 72541480Smckusick hd->scsi_ints = ints = hd->scsi_ints; 72641480Smckusick while (1) { 72741480Smckusick phase = hd->scsi_psns & PHASE; 72841480Smckusick switch (phase) { 72941480Smckusick 73041480Smckusick case STATUS_PHASE: 73141480Smckusick if (mxfer_in(hd, sizeof(hs->sc_stat), hs->sc_stat, 73241480Smckusick phase) <= 0) 73341480Smckusick goto abort; 73441480Smckusick break; 73541480Smckusick 73641480Smckusick case MESG_IN_PHASE: 73741480Smckusick if (mxfer_in(hd, sizeof(hs->sc_msg), hs->sc_msg, 73841480Smckusick phase) < 0) 73941480Smckusick goto abort; 74041480Smckusick break; 74141480Smckusick 74241480Smckusick case BUS_FREE_PHASE: 74341480Smckusick return; 74441480Smckusick 74541480Smckusick default: 74641480Smckusick printf("scsi%d: unexpected phase %d in finishxfer from %d\n", 74741480Smckusick hs->sc_hc->hp_unit, phase, target); 74841480Smckusick goto abort; 74941480Smckusick } 75041480Smckusick if (ints = hd->scsi_ints) { 75141480Smckusick hd->scsi_ints = ints; 75241480Smckusick if (ints & INTS_DISCON) 75341480Smckusick return; 75441480Smckusick else if (ints & ~(INTS_SRV_REQ|INTS_CMD_DONE)) { 75541480Smckusick scsierror(hs, hd, ints); 75641480Smckusick break; 75741480Smckusick } 75841480Smckusick } 75941480Smckusick if ((hd->scsi_ssts & SSTS_INITIATOR) == 0) 76041480Smckusick return; 76141480Smckusick } 76241480Smckusick abort: 76341480Smckusick scsiabort(hs, hd, "finishxfer"); 76441480Smckusick hs->sc_stat[0] = 0xfe; 76541480Smckusick } 76641480Smckusick 76741480Smckusick int 76841480Smckusick scsi_test_unit_rdy(ctlr, slave, unit) 76941480Smckusick int ctlr, slave, unit; 77041480Smckusick { 77141480Smckusick register struct scsi_softc *hs = &scsi_softc[ctlr]; 77241480Smckusick static struct scsi_cdb6 cdb = { CMD_TEST_UNIT_READY }; 77341480Smckusick 77441480Smckusick cdb.lun = unit; 77541480Smckusick return (scsiicmd(hs, slave, &cdb, sizeof(cdb), (u_char *)0, 0, 77641480Smckusick STATUS_PHASE)); 77741480Smckusick } 77841480Smckusick 77941480Smckusick int 78041480Smckusick scsi_request_sense(ctlr, slave, unit, buf, len) 78141480Smckusick int ctlr, slave, unit; 78241480Smckusick u_char *buf; 78341480Smckusick unsigned len; 78441480Smckusick { 78541480Smckusick register struct scsi_softc *hs = &scsi_softc[ctlr]; 78641480Smckusick static struct scsi_cdb6 cdb = { CMD_REQUEST_SENSE }; 78741480Smckusick 78841480Smckusick cdb.lun = unit; 78941480Smckusick cdb.len = len; 79041480Smckusick return (scsiicmd(hs, slave, &cdb, sizeof(cdb), buf, len, DATA_IN_PHASE)); 79141480Smckusick } 79241480Smckusick 79341480Smckusick int 79441480Smckusick scsi_immed_command(ctlr, slave, unit, cdb, buf, len, rd) 79541480Smckusick int ctlr, slave, unit; 79641480Smckusick struct scsi_fmt_cdb *cdb; 79741480Smckusick u_char *buf; 79841480Smckusick unsigned len; 79941480Smckusick { 80041480Smckusick register struct scsi_softc *hs = &scsi_softc[ctlr]; 80141480Smckusick 80241480Smckusick cdb->cdb[1] |= unit << 5; 80341480Smckusick return (scsiicmd(hs, slave, cdb->cdb, cdb->len, buf, len, 80441480Smckusick rd != 0? DATA_IN_PHASE : DATA_OUT_PHASE)); 80541480Smckusick } 80641480Smckusick 80741480Smckusick /* 80841480Smckusick * The following routines are test-and-transfer i/o versions of read/write 80941480Smckusick * for things like reading disk labels and writing core dumps. The 81041480Smckusick * routine scsigo should be used for normal data transfers, NOT these 81141480Smckusick * routines. 81241480Smckusick */ 81341480Smckusick int 81441480Smckusick scsi_tt_read(ctlr, slave, unit, buf, len, blk, bshift) 81541480Smckusick int ctlr, slave, unit; 81641480Smckusick u_char *buf; 81741480Smckusick u_int len; 81841480Smckusick daddr_t blk; 81941480Smckusick int bshift; 82041480Smckusick { 82141480Smckusick register struct scsi_softc *hs = &scsi_softc[ctlr]; 82241480Smckusick struct scsi_cdb10 cdb; 82341480Smckusick int stat; 82441480Smckusick int old_wait = scsi_data_wait; 82541480Smckusick 82641480Smckusick scsi_data_wait = 300000; 82741480Smckusick bzero(&cdb, sizeof(cdb)); 82841480Smckusick cdb.cmd = CMD_READ_EXT; 82941480Smckusick cdb.lun = unit; 83041480Smckusick blk >>= bshift; 83141480Smckusick cdb.lbah = blk >> 24; 83241480Smckusick cdb.lbahm = blk >> 16; 83341480Smckusick cdb.lbalm = blk >> 8; 83441480Smckusick cdb.lbal = blk; 83541480Smckusick cdb.lenh = len >> (8 + DEV_BSHIFT + bshift); 83641480Smckusick cdb.lenl = len >> (DEV_BSHIFT + bshift); 83741480Smckusick stat = scsiicmd(hs, slave, &cdb, sizeof(cdb), buf, len, DATA_IN_PHASE); 83841480Smckusick scsi_data_wait = old_wait; 83941480Smckusick return (stat); 84041480Smckusick } 84141480Smckusick 84241480Smckusick int 84341480Smckusick scsi_tt_write(ctlr, slave, unit, buf, len, blk, bshift) 84441480Smckusick int ctlr, slave, unit; 84541480Smckusick u_char *buf; 84641480Smckusick u_int len; 84741480Smckusick daddr_t blk; 84841480Smckusick int bshift; 84941480Smckusick { 85041480Smckusick register struct scsi_softc *hs = &scsi_softc[ctlr]; 85141480Smckusick struct scsi_cdb10 cdb; 85241480Smckusick int stat; 85341480Smckusick int old_wait = scsi_data_wait; 85441480Smckusick 85541480Smckusick scsi_data_wait = 300000; 85641480Smckusick 85741480Smckusick bzero(&cdb, sizeof(cdb)); 85841480Smckusick cdb.cmd = CMD_WRITE_EXT; 85941480Smckusick cdb.lun = unit; 86041480Smckusick blk >>= bshift; 86141480Smckusick cdb.lbah = blk >> 24; 86241480Smckusick cdb.lbahm = blk >> 16; 86341480Smckusick cdb.lbalm = blk >> 8; 86441480Smckusick cdb.lbal = blk; 86541480Smckusick cdb.lenh = len >> (8 + DEV_BSHIFT + bshift); 86641480Smckusick cdb.lenl = len >> (DEV_BSHIFT + bshift); 86741480Smckusick stat = scsiicmd(hs, slave, &cdb, sizeof(cdb), buf, len, DATA_OUT_PHASE); 86841480Smckusick scsi_data_wait = old_wait; 86941480Smckusick return (stat); 87041480Smckusick } 87141480Smckusick 87241480Smckusick int 87341480Smckusick scsireq(dq) 87441480Smckusick register struct devqueue *dq; 87541480Smckusick { 87641480Smckusick register struct devqueue *hq; 87741480Smckusick 87841480Smckusick hq = &scsi_softc[dq->dq_ctlr].sc_sq; 87941480Smckusick insque(dq, hq->dq_back); 88041480Smckusick if (dq->dq_back == hq) 88141480Smckusick return(1); 88241480Smckusick return(0); 88341480Smckusick } 88441480Smckusick 88541480Smckusick int 88641480Smckusick scsiustart(unit) 88741480Smckusick int unit; 88841480Smckusick { 88941480Smckusick register struct scsi_softc *hs = &scsi_softc[unit]; 89041480Smckusick 89141480Smckusick hs->sc_dq.dq_ctlr = DMA0 | DMA1; 89241480Smckusick if (dmareq(&hs->sc_dq)) 89341480Smckusick return(1); 89441480Smckusick return(0); 89541480Smckusick } 89641480Smckusick 89741480Smckusick void 89841480Smckusick scsistart(unit) 89941480Smckusick int unit; 90041480Smckusick { 90141480Smckusick register struct devqueue *dq; 90241480Smckusick 90341480Smckusick dq = scsi_softc[unit].sc_sq.dq_forw; 90441480Smckusick (dq->dq_driver->d_go)(dq->dq_unit); 90541480Smckusick } 90641480Smckusick 90741480Smckusick int 90841480Smckusick scsigo(ctlr, slave, unit, bp, cdb, pad) 90941480Smckusick int ctlr, slave, unit; 91041480Smckusick struct buf *bp; 91141480Smckusick struct scsi_fmt_cdb *cdb; 91241480Smckusick int pad; 91341480Smckusick { 91441480Smckusick register struct scsi_softc *hs = &scsi_softc[ctlr]; 91541480Smckusick volatile register struct scsidevice *hd = 91641480Smckusick (struct scsidevice *)hs->sc_hc->hp_addr; 91741480Smckusick int i, dmaflags; 91841480Smckusick u_char phase, ints, cmd; 91941480Smckusick 92041480Smckusick cdb->cdb[1] |= unit << 5; 92141480Smckusick 92241480Smckusick /* select the SCSI bus (it's an error if bus isn't free) */ 92341480Smckusick if (issue_select(hd, slave, hs->sc_scsi_addr) || wait_for_select(hd)) { 92441480Smckusick dmafree(&hs->sc_dq); 92541480Smckusick return (1); 92641480Smckusick } 92741480Smckusick /* 92841480Smckusick * Wait for a phase change (or error) then let the device 92941480Smckusick * sequence us through command phase (we may have to take 93041480Smckusick * a msg in/out before doing the command). If the disk has 93141480Smckusick * to do a seek, it may be a long time until we get a change 93241480Smckusick * to data phase so, in the absense of an explicit phase 93341480Smckusick * change, we assume data phase will be coming up and tell 93441480Smckusick * the SPC to start a transfer whenever it does. We'll get 93541480Smckusick * a service required interrupt later if this assumption is 93641480Smckusick * wrong. Otherwise we'll get a service required int when 93741480Smckusick * the transfer changes to status phase. 93841480Smckusick */ 93941480Smckusick phase = CMD_PHASE; 94041480Smckusick while (1) { 94141480Smckusick register int wait = scsi_cmd_wait; 94241480Smckusick 94341480Smckusick switch (phase) { 94441480Smckusick 94541480Smckusick case CMD_PHASE: 94641480Smckusick if (ixfer_start(hd, cdb->len, phase, wait)) 94741480Smckusick if (ixfer_out(hd, cdb->len, cdb->cdb)) 94841480Smckusick goto abort; 94941480Smckusick break; 95041480Smckusick 95141480Smckusick case MESG_IN_PHASE: 95241480Smckusick if (ixfer_start(hd, sizeof(hs->sc_msg), phase, wait)|| 95341480Smckusick !(hd->scsi_ssts & SSTS_DREG_EMPTY)) { 95441480Smckusick ixfer_in(hd, sizeof(hs->sc_msg), hs->sc_msg); 95541480Smckusick hd->scsi_scmd = SCMD_RST_ACK; 95641480Smckusick } 95741480Smckusick phase = BUS_FREE_PHASE; 95841480Smckusick break; 95941480Smckusick 96041480Smckusick case DATA_IN_PHASE: 96141480Smckusick case DATA_OUT_PHASE: 96241480Smckusick goto out; 96341480Smckusick 96441480Smckusick default: 96541480Smckusick printf("scsi%d: unexpected phase %d in go from %d\n", 96641480Smckusick hs->sc_hc->hp_unit, phase, slave); 96741480Smckusick goto abort; 96841480Smckusick } 96941480Smckusick while ((ints = hd->scsi_ints) == 0) { 97041480Smckusick if (--wait < 0) { 97141480Smckusick HIST(sgo_wait, wait) 97241480Smckusick goto abort; 97341480Smckusick } 97441480Smckusick DELAY(1); 97541480Smckusick } 97641480Smckusick HIST(sgo_wait, wait) 97741480Smckusick hd->scsi_ints = ints; 97841480Smckusick if (ints & INTS_SRV_REQ) 97941480Smckusick phase = hd->scsi_psns & PHASE; 98041480Smckusick else if (ints & INTS_CMD_DONE) 98141480Smckusick goto out; 98241480Smckusick else { 98341480Smckusick scsierror(hs, hd, ints); 98441480Smckusick goto abort; 98541480Smckusick } 98641480Smckusick } 98741480Smckusick out: 98841480Smckusick /* 98941480Smckusick * Reset the card dma logic, setup the dma channel then 99041480Smckusick * get the dio part of the card set for a dma xfer. 99141480Smckusick */ 99241480Smckusick hd->scsi_hconf = 0; 99345514Smckusick cmd = CSR_IE; 99441480Smckusick dmaflags = DMAGO_NOINT; 99545514Smckusick if (scsi_pridma) 99645514Smckusick dmaflags |= DMAGO_PRI; 99741480Smckusick if (bp->b_flags & B_READ) 99841480Smckusick dmaflags |= DMAGO_READ; 99941480Smckusick if ((hs->sc_flags & SCSI_DMA32) && 100041480Smckusick ((int)bp->b_un.b_addr & 3) == 0 && (bp->b_bcount & 3) == 0) { 100141480Smckusick cmd |= CSR_DMA32; 100241480Smckusick dmaflags |= DMAGO_LWORD; 100341480Smckusick } else 100441480Smckusick dmaflags |= DMAGO_WORD; 100541480Smckusick dmago(hs->sc_dq.dq_ctlr, bp->b_un.b_addr, bp->b_bcount, dmaflags); 100641480Smckusick 100741480Smckusick if (bp->b_flags & B_READ) { 100841480Smckusick cmd |= CSR_DMAIN; 100941480Smckusick phase = DATA_IN_PHASE; 101041480Smckusick } else 101141480Smckusick phase = DATA_OUT_PHASE; 101245514Smckusick /* 101345514Smckusick * DMA enable bits must be set after size and direction bits. 101445514Smckusick */ 101541480Smckusick hd->scsi_csr = cmd; 101645514Smckusick hd->scsi_csr |= (CSR_DE0 << hs->sc_dq.dq_ctlr); 101741480Smckusick /* 101841480Smckusick * Setup the SPC for the transfer. We don't want to take 101941480Smckusick * first a command complete then a service required interrupt 102041480Smckusick * at the end of the transfer so we try to disable the cmd 102141480Smckusick * complete by setting the transfer counter to more bytes 102241480Smckusick * than we expect. (XXX - This strategy may have to be 102341480Smckusick * modified to deal with devices that return variable length 102441480Smckusick * blocks, e.g., some tape drives.) 102541480Smckusick */ 102641480Smckusick cmd = SCMD_XFR; 102741480Smckusick i = (unsigned)bp->b_bcount; 102841480Smckusick if (pad) { 102941480Smckusick cmd |= SCMD_PAD; 103041480Smckusick /* 103141480Smckusick * XXX - If we don't do this, the last 2 or 4 bytes 103241480Smckusick * (depending on word/lword DMA) of a read get trashed. 103341480Smckusick * It looks like it is necessary for the DMA to complete 103441480Smckusick * before the SPC goes into "pad mode"??? Note: if we 103541480Smckusick * also do this on a write, the request never completes. 103641480Smckusick */ 103741480Smckusick if (bp->b_flags & B_READ) 103841480Smckusick i += 2; 103941480Smckusick #ifdef DEBUG 104041480Smckusick hs->sc_flags |= SCSI_PAD; 104141480Smckusick if (i & 1) 104241480Smckusick printf("scsi%d: odd byte count: %d bytes @ %d\n", 104341480Smckusick ctlr, i, bp->b_cylin); 104441480Smckusick #endif 104541480Smckusick } else 104641480Smckusick i += 4; 104741480Smckusick hd->scsi_tch = i >> 16; 104841480Smckusick hd->scsi_tcm = i >> 8; 104941480Smckusick hd->scsi_tcl = i; 105041480Smckusick hd->scsi_pctl = phase; 105141480Smckusick hd->scsi_tmod = 0; 105241480Smckusick hd->scsi_scmd = cmd; 105341480Smckusick hs->sc_flags |= SCSI_IO; 105441480Smckusick return (0); 105541480Smckusick abort: 105641480Smckusick scsiabort(hs, hd, "go"); 105741480Smckusick dmafree(&hs->sc_dq); 105841480Smckusick return (1); 105941480Smckusick } 106041480Smckusick 106141480Smckusick void 106241480Smckusick scsidone(unit) 106341480Smckusick register int unit; 106441480Smckusick { 106541480Smckusick volatile register struct scsidevice *hd = 106641480Smckusick (struct scsidevice *)scsi_softc[unit].sc_hc->hp_addr; 106741480Smckusick 106845514Smckusick #ifdef DEBUG 106945514Smckusick if (scsi_debug) 107045514Smckusick printf("scsi%d: done called!\n"); 107145514Smckusick #endif 107241480Smckusick /* dma operation is done -- turn off card dma */ 107341480Smckusick hd->scsi_csr &=~ (CSR_DE1|CSR_DE0); 107441480Smckusick } 107541480Smckusick 107641480Smckusick int 107741480Smckusick scsiintr(unit) 107841480Smckusick register int unit; 107941480Smckusick { 108041480Smckusick register struct scsi_softc *hs = &scsi_softc[unit]; 108141480Smckusick volatile register struct scsidevice *hd = 108241480Smckusick (struct scsidevice *)hs->sc_hc->hp_addr; 108341480Smckusick register u_char ints; 108441480Smckusick register struct devqueue *dq; 108541480Smckusick 108641480Smckusick if ((hd->scsi_csr & (CSR_IE|CSR_IR)) != (CSR_IE|CSR_IR)) 108741480Smckusick return (0); 108841480Smckusick 108941480Smckusick ints = hd->scsi_ints; 109041480Smckusick if ((ints & INTS_SRV_REQ) && (hs->sc_flags & SCSI_IO)) { 109141480Smckusick /* 109241480Smckusick * this should be the normal i/o completion case. 109341480Smckusick * get the status & cmd complete msg then let the 109441480Smckusick * device driver look at what happened. 109541480Smckusick */ 109641480Smckusick #ifdef DEBUG 109741480Smckusick int len = (hd->scsi_tch << 16) | (hd->scsi_tcm << 8) | 109841480Smckusick hd->scsi_tcl; 109941480Smckusick if (!(hs->sc_flags & SCSI_PAD)) 110041480Smckusick len -= 4; 110141480Smckusick hs->sc_flags &=~ SCSI_PAD; 110241480Smckusick #endif 110341480Smckusick dq = hs->sc_sq.dq_forw; 110446285Smckusick finishxfer(hs, hd, dq->dq_slave); 110541480Smckusick hs->sc_flags &=~ SCSI_IO; 110641480Smckusick dmafree(&hs->sc_dq); 110741480Smckusick (dq->dq_driver->d_intr)(dq->dq_unit, hs->sc_stat[0]); 110841480Smckusick } else { 110941480Smckusick /* Something unexpected happened -- deal with it. */ 111041480Smckusick hd->scsi_ints = ints; 111141480Smckusick hd->scsi_csr = 0; 111241480Smckusick scsierror(hs, hd, ints); 111341480Smckusick scsiabort(hs, hd, "intr"); 111441480Smckusick if (hs->sc_flags & SCSI_IO) { 111541480Smckusick hs->sc_flags &=~ SCSI_IO; 111641480Smckusick dmafree(&hs->sc_dq); 111741480Smckusick dq = hs->sc_sq.dq_forw; 111841480Smckusick (dq->dq_driver->d_intr)(dq->dq_unit, -1); 111941480Smckusick } 112041480Smckusick } 112141480Smckusick return(1); 112241480Smckusick } 112341480Smckusick 112441480Smckusick void 112541480Smckusick scsifree(dq) 112641480Smckusick register struct devqueue *dq; 112741480Smckusick { 112841480Smckusick register struct devqueue *hq; 112941480Smckusick 113041480Smckusick hq = &scsi_softc[dq->dq_ctlr].sc_sq; 113141480Smckusick remque(dq); 113241480Smckusick if ((dq = hq->dq_forw) != hq) 113341480Smckusick (dq->dq_driver->d_start)(dq->dq_unit); 113441480Smckusick } 113546285Smckusick 113646285Smckusick /* 113746285Smckusick * (XXX) The following routine is needed for the SCSI tape driver 113846285Smckusick * to read odd-size records. 113946285Smckusick */ 114046285Smckusick 114146285Smckusick #include "st.h" 114246285Smckusick #if NST > 0 114346285Smckusick int 114446285Smckusick scsi_tt_oddio(ctlr, slave, unit, buf, len, b_flags, freedma) 114546285Smckusick int ctlr, slave, unit, b_flags; 114646285Smckusick u_char *buf; 114746285Smckusick u_int len; 114846285Smckusick { 114946285Smckusick register struct scsi_softc *hs = &scsi_softc[ctlr]; 115046285Smckusick struct scsi_cdb6 cdb; 115146285Smckusick u_char iphase; 115246285Smckusick int stat; 115346285Smckusick 115446285Smckusick /* 115546285Smckusick * First free any DMA channel that was allocated. 115646285Smckusick * We can't use DMA to do this transfer. 115746285Smckusick */ 115846285Smckusick if (freedma) 115946285Smckusick dmafree(hs->sc_dq); 116046285Smckusick /* 116146285Smckusick * Initialize command block 116246285Smckusick */ 116346285Smckusick bzero(&cdb, sizeof(cdb)); 116446285Smckusick cdb.lun = unit; 116546285Smckusick cdb.lbam = (len >> 16) & 0xff; 116646285Smckusick cdb.lbal = (len >> 8) & 0xff; 116746285Smckusick cdb.len = len & 0xff; 116846285Smckusick if (buf == 0) { 116946285Smckusick cdb.cmd = CMD_SPACE; 117046285Smckusick cdb.lun |= 0x00; 117146285Smckusick len = 0; 117246285Smckusick iphase = MESG_IN_PHASE; 117346285Smckusick } else if (b_flags & B_READ) { 117446285Smckusick cdb.cmd = CMD_READ; 117546285Smckusick iphase = DATA_IN_PHASE; 117646285Smckusick } else { 117746285Smckusick cdb.cmd = CMD_WRITE; 117846285Smckusick iphase = DATA_OUT_PHASE; 117946285Smckusick } 118046285Smckusick /* 118146285Smckusick * Perform command (with very long delays) 118246285Smckusick */ 118346285Smckusick scsi_delay(30000000); 118446285Smckusick stat = scsiicmd(hs, slave, &cdb, sizeof(cdb), buf, len, iphase); 118546285Smckusick scsi_delay(0); 118646285Smckusick return (stat); 118746285Smckusick } 118841480Smckusick #endif 118946285Smckusick #endif 1190