1*55138Storek /* 2*55138Storek * Copyright (c) 1992 The Regents of the University of California. 3*55138Storek * All rights reserved. 4*55138Storek * 5*55138Storek * This software was developed by the Computer Systems Engineering group 6*55138Storek * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and 7*55138Storek * contributed to Berkeley. 8*55138Storek * 9*55138Storek * %sccs.include.redist.c% 10*55138Storek * 11*55138Storek * @(#)esp.c 7.1 (Berkeley) 07/13/92 12*55138Storek * 13*55138Storek * from: $Header: esp.c,v 1.22 92/06/17 06:59:33 torek Exp $ (LBL) 14*55138Storek * 15*55138Storek * Loosely derived from Mary Baker's devSCSIC90.c from the Berkeley 16*55138Storek * Sprite project, which is: 17*55138Storek * 18*55138Storek * Copyright 1988 Regents of the University of California 19*55138Storek * Permission to use, copy, modify, and distribute this 20*55138Storek * software and its documentation for any purpose and without 21*55138Storek * fee is hereby granted, provided that the above copyright 22*55138Storek * notice appear in all copies. The University of California 23*55138Storek * makes no representations about the suitability of this 24*55138Storek * software for any purpose. It is provided "as is" without 25*55138Storek * express or implied warranty. 26*55138Storek * 27*55138Storek * from /sprite/src/kernel/dev/sun4c.md/RCS/devSCSIC90.c,v 1.4 28*55138Storek * 90/12/19 12:37:58 mgbaker Exp $ SPRITE (Berkeley) 29*55138Storek */ 30*55138Storek 31*55138Storek /* 32*55138Storek * Sbus ESP/DMA driver. A single driver must be used for both devices 33*55138Storek * as they are physically tied to each other: The DMA chip can only 34*55138Storek * be used to assist ESP SCSI transactions; the ESP interrupt enable is 35*55138Storek * in the DMA chip csr. 36*55138Storek * 37*55138Storek * Since DMA and SCSI interrupts are handled in the same routine, the 38*55138Storek * DMA device does not declare itself as an sbus device. This saves 39*55138Storek * some space. 40*55138Storek */ 41*55138Storek 42*55138Storek #include "sys/param.h" 43*55138Storek #include "sys/buf.h" 44*55138Storek #include "sys/device.h" 45*55138Storek #include "sys/malloc.h" 46*55138Storek 47*55138Storek #include "scsi/scsi.h" 48*55138Storek #include "scsi/scsivar.h" 49*55138Storek 50*55138Storek #include "machine/autoconf.h" 51*55138Storek #include "machine/cpu.h" 52*55138Storek 53*55138Storek #include "dmareg.h" 54*55138Storek #define ESP_PHASE_NAMES 55*55138Storek #include "espreg.h" 56*55138Storek #include "sbusvar.h" 57*55138Storek 58*55138Storek #ifdef DEBUG 59*55138Storek int espdebug = 1; 60*55138Storek #endif 61*55138Storek 62*55138Storek /* 63*55138Storek * This driver is organized as a collection of state machines. The 64*55138Storek * primary machine is the SCSI sequencer: 65*55138Storek * 66*55138Storek * Given some previous SCSI state (as set up or tracked by us earlier) 67*55138Storek * and the interrupt registers provided on the chips (dmacsr, espstat, 68*55138Storek * espstep, and espintr), derive an action. In many cases this is 69*55138Storek * just a matter of reading the target's phase and following its orders, 70*55138Storek * which sets a new state. 71*55138Storek * 72*55138Storek * This sequencing is done in espact(); the state is primed in espselect(). 73*55138Storek * 74*55138Storek * There will be (update this comment when there is) another state machine 75*55138Storek * used to handle transfers that fall afoul of chip limits (16 bit DMA 76*55138Storek * counter; 24 bit address counter in 32 bit address field). 77*55138Storek * 78*55138Storek * Another state bit is used to recover from bus resets: 79*55138Storek * 80*55138Storek * A single TEST UNIT READY is attempted on each target before any 81*55138Storek * real communication begins; this TEST UNIT READY is allowed to 82*55138Storek * fail in any way. This is required for the Quantum ProDrive 100 83*55138Storek * MB disks, for instance, which respond to their first selection 84*55138Storek * with status phase, and for anything that insists on implementing 85*55138Storek * the broken SCSI-2 synch transfer initial message. 86*55138Storek * 87*55138Storek * This is done in espclear() (which calls espselect(); functions that 88*55138Storek * call espselect() must check for clearing first). 89*55138Storek * 90*55138Storek * The state machines actually intermingle, as some SCSI sequences are 91*55138Storek * only allowed during clearing. 92*55138Storek */ 93*55138Storek 94*55138Storek /* per-DMA variables */ 95*55138Storek struct dma_softc { 96*55138Storek struct device sc_dev; /* base device */ 97*55138Storek volatile struct dmareg *sc_dma; /* register virtual address */ 98*55138Storek }; 99*55138Storek void dmaattach(struct device *, struct device *, void *); 100*55138Storek struct cfdriver dmacd = 101*55138Storek { NULL, "dma", matchbyname, dmaattach, DV_DULL, sizeof(struct dma_softc) }; 102*55138Storek 103*55138Storek /* per-ESP variables */ 104*55138Storek struct esp_softc { 105*55138Storek /* 106*55138Storek * External interfaces. 107*55138Storek */ 108*55138Storek struct hba_softc sc_hba; /* base device + hba, must be first */ 109*55138Storek struct sbusdev sc_sd; /* sbus device */ 110*55138Storek struct intrhand sc_ih; /* interrupt entry */ 111*55138Storek int sc_interrupts; /* total number of interrupts taken */ 112*55138Storek struct dma_softc *sc_dsc; /* pointer to corresponding dma sc */ 113*55138Storek 114*55138Storek /* 115*55138Storek * Addresses mapped to hardware registers. 116*55138Storek */ 117*55138Storek volatile struct espreg *sc_esp; 118*55138Storek volatile struct dmareg *sc_dma; 119*55138Storek 120*55138Storek /* 121*55138Storek * Copies of registers cleared/unlatched by reading. 122*55138Storek */ 123*55138Storek u_long sc_dmacsr; 124*55138Storek u_char sc_espstat; 125*55138Storek u_char sc_espstep; 126*55138Storek u_char sc_espintr; 127*55138Storek 128*55138Storek /* miscellaneous */ 129*55138Storek int sc_clockfreq; /* clock frequency */ 130*55138Storek u_char sc_sel_timeout; /* select timeout */ 131*55138Storek u_char sc_id; /* initiator ID (default = 7) */ 132*55138Storek u_char sc_needclear; /* uncleared targets (1 bit each) */ 133*55138Storek u_char sc_esptype; /* 100, 100A, 2xx (see below) */ 134*55138Storek u_char sc_ccf; /* clock conversion factor */ 135*55138Storek u_char sc_conf1; /* value for config reg 1 */ 136*55138Storek u_char sc_conf2; /* value for config reg 2 */ 137*55138Storek u_char sc_conf3; /* value for config reg 3 */ 138*55138Storek 139*55138Storek /* 140*55138Storek * Information pertaining to the current transfer, 141*55138Storek * including sequencing. 142*55138Storek * 143*55138Storek * The size of sc_msg is the size of the ESP fifo, 144*55138Storek * since we do message-in simply by allowing the fifo to fill. 145*55138Storek */ 146*55138Storek char sc_probing; /* used during autoconf; see below */ 147*55138Storek char sc_clearing; /* true => cmd is just to clear targ */ 148*55138Storek char sc_state; /* SCSI protocol state; see below */ 149*55138Storek char sc_sentcmd; /* set once we get cmd out */ 150*55138Storek char sc_dmaactive; /* true => doing dma */ 151*55138Storek #ifdef notyet 152*55138Storek u_char sc_sync; /* synchronous transfer stuff (?) */ 153*55138Storek #endif 154*55138Storek u_char sc_stat[2]; /* status from last `status' phase */ 155*55138Storek u_char sc_msg[16]; /* message from device */ 156*55138Storek u_short sc_dmactl; /* control to load into dma csr */ 157*55138Storek u_long sc_dmaaddr; /* addr to load into dma addr */ 158*55138Storek int sc_targ; /* the target involved */ 159*55138Storek int sc_resid; /* count of bytes not xferred */ 160*55138Storek struct scsi_cdb sc_cdb; /* current command (not in dvma) */ 161*55138Storek }; 162*55138Storek 163*55138Storek /* 164*55138Storek * Values for sc_esptype (used to control configuration reset). 165*55138Storek * The order is important; see espreset(). 166*55138Storek */ 167*55138Storek #define ESP100 0 168*55138Storek #define ESP100A 1 169*55138Storek #define ESP2XX 2 170*55138Storek 171*55138Storek /* 172*55138Storek * Probe state. 0 means not probing. While looking for each target 173*55138Storek * we set this to PROBE_TESTING and do a TEST UNIT READY on unit 0. 174*55138Storek * If selection fails, this is changed to PROBE_NO_TARGET; otherwise 175*55138Storek * we assume the target exists, regardless of the result of the test. 176*55138Storek */ 177*55138Storek #define PROBE_TESTING 1 178*55138Storek #define PROBE_NO_TARGET 2 179*55138Storek 180*55138Storek /* 181*55138Storek * States in sc_state. 182*55138Storek * 183*55138Storek * Note that S_CMDSVC is rare: normally we load the SCSI command into the 184*55138Storek * ESP fifo and get interrupted only when the device has gone to data 185*55138Storek * or status phase. If the device wants to play games, though, we end 186*55138Storek * up doing things differently. 187*55138Storek */ 188*55138Storek char *espstates[] = { 189*55138Storek #define S_IDLE 0 /* not doing anything */ 190*55138Storek "idle", 191*55138Storek #define S_SEL 1 /* expecting select done interrupt */ 192*55138Storek "selecting", 193*55138Storek #define S_CMDSVC 2 /* expecting service req interrupt */ 194*55138Storek "waiting for service request after command", 195*55138Storek #define S_IOSVC 3 /* expecting service req interrupt */ 196*55138Storek "waiting for service request after io", 197*55138Storek #define S_DI 4 /* expecting data-in done interrupt */ 198*55138Storek "receiving data", 199*55138Storek #define S_DO 5 /* expecting data-out done interrupt */ 200*55138Storek "sending data", 201*55138Storek #define S_STAT 6 /* expecting status done interrupt */ 202*55138Storek "receiving status", 203*55138Storek #define S_MI 7 /* expecting message-in done interrupt */ 204*55138Storek "receiving message", 205*55138Storek #define S_FI 8 /* expecting final disconnect interrupt */ 206*55138Storek "waiting for disconnect" 207*55138Storek }; 208*55138Storek 209*55138Storek /* 210*55138Storek * Return values from espact(). 211*55138Storek */ 212*55138Storek #define ACT_CONT 0 /* espact() handled everything */ 213*55138Storek #define ACT_READ 1 /* target said it is sending us data */ 214*55138Storek #define ACT_WRITE 2 /* target said it is expecting data */ 215*55138Storek #define ACT_DONE 3 /* handled everything, and op is now done */ 216*55138Storek #define ACT_ERROR 4 /* an error occurred, op has been trashed */ 217*55138Storek #define ACT_RESET 5 /* please reset ESP, then do ACT_ERROR */ 218*55138Storek #define ACT_QUICKINTR 6 /* another interrupt is expected immediately */ 219*55138Storek 220*55138Storek /* autoconfiguration driver */ 221*55138Storek void espattach(struct device *, struct device *, void *); 222*55138Storek struct cfdriver espcd = 223*55138Storek { NULL, "esp", matchbyname, espattach, DV_DULL, sizeof(struct esp_softc) }; 224*55138Storek 225*55138Storek /* Sbus driver */ 226*55138Storek void espsbreset(struct device *); 227*55138Storek 228*55138Storek /* interrupt interface */ 229*55138Storek int espintr(void *); 230*55138Storek 231*55138Storek /* SCSI HBA driver */ 232*55138Storek int espicmd(struct hba_softc *, int, struct scsi_cdb *, caddr_t, int, int); 233*55138Storek int espdump(struct hba_softc *, int, struct scsi_cdb *, caddr_t, int); 234*55138Storek void espstart(struct device *, struct sq *, struct buf *, 235*55138Storek scdgo_fn, struct device *); 236*55138Storek int espgo(struct device *, int, scintr_fn, struct device *, 237*55138Storek struct buf *, int); 238*55138Storek void esprel(struct device *); 239*55138Storek void esphbareset(struct hba_softc *, int); 240*55138Storek static struct hbadriver esphbadriver = 241*55138Storek { espicmd, espdump, espstart, espgo, esprel, esphbareset }; 242*55138Storek 243*55138Storek /* forward declarations */ 244*55138Storek static void espdoattach(int unit); 245*55138Storek static void espreset(struct esp_softc *); 246*55138Storek 247*55138Storek /* 248*55138Storek * The transfer size is limited to 16 bits since the scsi ctrl transfer 249*55138Storek * counter is only 2 bytes. A 0 value means the biggest transfer size 250*55138Storek * (2 ** 16) == 64k. 251*55138Storek */ 252*55138Storek #define MAX_TRANSFER_SIZE (64 * 1024) 253*55138Storek 254*55138Storek /* Return true if this transfer will cross a dma boundary */ 255*55138Storek #define CROSS_DMA(addr, len) \ 256*55138Storek (((int)(addr) & 0xff000000) != (((int)(addr) + (len) - 1) & 0xff000000)) 257*55138Storek 258*55138Storek /* 259*55138Storek * Attach a found DMA chip. 260*55138Storek * The second argument is really a pointer to an sbus_attach_args. 261*55138Storek */ 262*55138Storek void 263*55138Storek dmaattach(parent, dev, args) 264*55138Storek struct device *parent; 265*55138Storek struct device *dev; 266*55138Storek void *args; 267*55138Storek { 268*55138Storek register struct dma_softc *dsc = (struct dma_softc *)dev; 269*55138Storek register struct sbus_attach_args *sa = args; 270*55138Storek register volatile struct dmareg *dma; 271*55138Storek register int rev; 272*55138Storek struct esp_softc *esc; 273*55138Storek 274*55138Storek if (sa->sa_ra.ra_vaddr) 275*55138Storek dma = (volatile struct dmareg *)sa->sa_ra.ra_vaddr; 276*55138Storek else 277*55138Storek dma = (volatile struct dmareg *) 278*55138Storek mapiodev(sa->sa_ra.ra_paddr, sizeof(struct dmareg)); 279*55138Storek dsc->sc_dma = dma; 280*55138Storek 281*55138Storek switch (rev = DMA_REV(dma->dma_csr)) { 282*55138Storek case DMAREV_1: 283*55138Storek printf(": rev 1\n"); 284*55138Storek break; 285*55138Storek case DMAREV_2: 286*55138Storek printf(": rev 2\n"); 287*55138Storek break; 288*55138Storek default: 289*55138Storek printf(": unknown revision %d\n", rev); 290*55138Storek break; 291*55138Storek } 292*55138Storek espdoattach(dsc->sc_dev.dv_unit); 293*55138Storek } 294*55138Storek 295*55138Storek /* 296*55138Storek * Attach a found ESP chip. Search for targets; attach each one found. 297*55138Storek * The latter must be deferred if the corresponding dma chip has not yet 298*55138Storek * been configured. 299*55138Storek */ 300*55138Storek void 301*55138Storek espattach(parent, self, args) 302*55138Storek struct device *parent; 303*55138Storek struct device *self; 304*55138Storek void *args; 305*55138Storek { 306*55138Storek register struct esp_softc *sc = (struct esp_softc *)self; 307*55138Storek register struct sbus_attach_args *sa = args; 308*55138Storek register volatile struct espreg *esp; 309*55138Storek struct dma_softc *dsc; 310*55138Storek int node, pri, freq, t; 311*55138Storek 312*55138Storek if (sa->sa_ra.ra_nintr != 1) { 313*55138Storek printf(": expected 1 interrupt, got %d\n", sa->sa_ra.ra_nintr); 314*55138Storek return; 315*55138Storek } 316*55138Storek pri = sa->sa_ra.ra_intr[0].int_pri; 317*55138Storek printf(" pri %d", pri); 318*55138Storek if (sa->sa_ra.ra_vaddr) 319*55138Storek esp = (volatile struct espreg *)sa->sa_ra.ra_vaddr; 320*55138Storek else 321*55138Storek esp = (volatile struct espreg *) 322*55138Storek mapiodev(sa->sa_ra.ra_paddr, sizeof(struct espreg)); 323*55138Storek sc->sc_esp = esp; 324*55138Storek node = sa->sa_ra.ra_node; 325*55138Storek sc->sc_id = getpropint(node, "initiator-id", 7); 326*55138Storek freq = getpropint(node, "clock-frequency", -1); 327*55138Storek if (freq < 0) 328*55138Storek freq = ((struct sbus_softc *)sc->sc_hba.hba_dev.dv_parent)->sc_clockfreq; 329*55138Storek 330*55138Storek /* MIGHT NEED TO RESET ESP CHIP HERE ...? */ 331*55138Storek 332*55138Storek /* 333*55138Storek * Find out whether we have a -100, -100A, or -2xx, 334*55138Storek * and what speed it runs at. 335*55138Storek */ 336*55138Storek sc->sc_conf1 = sc->sc_id | ESPCONF1_PARENB; 337*55138Storek /* sc->sc_conf2 = 0; */ 338*55138Storek /* sc->sc_conf3 = 0; */ 339*55138Storek esp->esp_conf1 = sc->sc_conf1; 340*55138Storek esp->esp_conf2 = 0; 341*55138Storek esp->esp_conf2 = ESPCONF2_SCSI2 | ESPCONF2_RPE; 342*55138Storek if ((esp->esp_conf2 & ~ESPCONF2_RSVD) != 343*55138Storek (ESPCONF2_SCSI2 | ESPCONF2_RPE)) { 344*55138Storek printf(": ESP100"); 345*55138Storek sc->sc_esptype = ESP100; 346*55138Storek } else { 347*55138Storek esp->esp_conf2 = 0; 348*55138Storek esp->esp_conf3 = 0; 349*55138Storek esp->esp_conf3 = 5; 350*55138Storek if (esp->esp_conf3 != 5) { /* XXX def bits */ 351*55138Storek printf(": ESP100A"); 352*55138Storek sc->sc_esptype = ESP100A; 353*55138Storek } else { 354*55138Storek esp->esp_conf3 = 0; 355*55138Storek printf(": ESP2XX"); 356*55138Storek sc->sc_esptype = ESP2XX; 357*55138Storek } 358*55138Storek } 359*55138Storek printf(", clock = %s MHz, ID = %d\n", clockfreq(freq), sc->sc_id); 360*55138Storek 361*55138Storek /* 362*55138Storek * Set clock conversion factor and select timeout. 363*55138Storek * N.B.: clock frequency is not actually used in the rest 364*55138Storek * of the driver; I calculate it here for completeness only 365*55138Storek * (so I can see it when debugging). 366*55138Storek */ 367*55138Storek sc->sc_clockfreq = freq; 368*55138Storek freq = howmany(freq, 1000 * 1000); /* convert to MHz */ 369*55138Storek t = ESPCCF_FROMMHZ(freq); 370*55138Storek if (t < ESPCCF_MIN) 371*55138Storek t = ESPCCF_MIN; 372*55138Storek sc->sc_ccf = t; 373*55138Storek t = ESPTIMO_REGVAL(250, t, freq); /* timeout = 250 ms. */ 374*55138Storek if (t >= 256) 375*55138Storek t = 0; 376*55138Storek sc->sc_sel_timeout = t; 377*55138Storek 378*55138Storek /* 379*55138Storek * Link into sbus; set interrupt handler. 380*55138Storek */ 381*55138Storek sc->sc_sd.sd_reset = espsbreset; 382*55138Storek sbus_establish(&sc->sc_sd, &sc->sc_hba.hba_dev); 383*55138Storek sc->sc_ih.ih_fun = espintr; 384*55138Storek sc->sc_ih.ih_arg = sc; 385*55138Storek intr_establish(pri, &sc->sc_ih); 386*55138Storek espdoattach(sc->sc_hba.hba_dev.dv_unit); 387*55138Storek } 388*55138Storek 389*55138Storek /* 390*55138Storek * `Final' attach of esp occurs once esp and dma chips have been found 391*55138Storek * and assigned virtual addresses. Set up the ESP SCSI data structures 392*55138Storek * and probe the SCSI bus. 393*55138Storek */ 394*55138Storek static void 395*55138Storek espdoattach(unit) 396*55138Storek int unit; 397*55138Storek { 398*55138Storek register struct esp_softc *sc; 399*55138Storek register struct dma_softc *dsc; 400*55138Storek register int targ; 401*55138Storek 402*55138Storek /* make sure we have both */ 403*55138Storek if (espcd.cd_ndevs <= unit || 404*55138Storek dmacd.cd_ndevs <= unit || 405*55138Storek (sc = espcd.cd_devs[unit]) == NULL || 406*55138Storek (dsc = dmacd.cd_devs[unit]) == NULL) 407*55138Storek return; 408*55138Storek sc->sc_dsc = dsc; 409*55138Storek sc->sc_dma = dsc->sc_dma; 410*55138Storek sc->sc_hba.hba_driver = &esphbadriver; 411*55138Storek 412*55138Storek espreset(sc); 413*55138Storek 414*55138Storek /* MAYBE THIS SHOULD BE MOVED TO scsi_subr.c? */ 415*55138Storek for (targ = 0; targ < 8; targ++) { 416*55138Storek if (targ == sc->sc_id) 417*55138Storek continue; 418*55138Storek sc->sc_probing = PROBE_TESTING; 419*55138Storek sc->sc_clearing = 1; 420*55138Storek (void) scsi_test_unit_ready(&sc->sc_hba, targ, 0); 421*55138Storek if (sc->sc_probing != PROBE_NO_TARGET) { 422*55138Storek sc->sc_probing = 0; 423*55138Storek sc->sc_clearing = 0; 424*55138Storek SCSI_FOUNDTARGET(&sc->sc_hba, targ); 425*55138Storek } 426*55138Storek } 427*55138Storek sc->sc_probing = 0; 428*55138Storek sc->sc_clearing = 0; 429*55138Storek } 430*55138Storek 431*55138Storek /* 432*55138Storek * Internal DMA reset. 433*55138Storek */ 434*55138Storek static void 435*55138Storek dmareset(sc) 436*55138Storek struct esp_softc *sc; 437*55138Storek { 438*55138Storek register volatile struct dmareg *dma = sc->sc_dma; 439*55138Storek 440*55138Storek /* reset DMA chip */ 441*55138Storek dma->dma_csr |= DMA_RESET; 442*55138Storek DELAY(200); 443*55138Storek dma->dma_csr &= ~DMA_RESET; /* ??? */ 444*55138Storek sc->sc_state = S_IDLE; 445*55138Storek sc->sc_dmaactive = 0; 446*55138Storek dma->dma_csr |= DMA_IE; /* enable interrupts */ 447*55138Storek DELAY(200); 448*55138Storek } 449*55138Storek 450*55138Storek /* 451*55138Storek * Reset the chip. N.B.: this causes a SCSI bus reset! 452*55138Storek */ 453*55138Storek static void 454*55138Storek espreset(sc) 455*55138Storek register struct esp_softc *sc; 456*55138Storek { 457*55138Storek register volatile struct espreg *esp = sc->sc_esp; 458*55138Storek 459*55138Storek dmareset(sc); 460*55138Storek esp->esp_cmd = ESPCMD_RESET_CHIP; 461*55138Storek DELAY(200); 462*55138Storek esp->esp_cmd = ESPCMD_NOP; 463*55138Storek DELAY(200); 464*55138Storek 465*55138Storek /* 466*55138Storek * Reload configuration registers (cleared by RESET_CHIP command). 467*55138Storek * Reloading conf2 on an ESP100 goofs it up, so out of paranoia 468*55138Storek * we load only the registers that exist. 469*55138Storek */ 470*55138Storek esp->esp_conf1 = sc->sc_conf1; 471*55138Storek if (sc->sc_esptype > ESP100) { /* 100A, 2XX */ 472*55138Storek esp->esp_conf2 = sc->sc_conf2; 473*55138Storek if (sc->sc_esptype > ESP100A) /* 2XX only */ 474*55138Storek esp->esp_conf3 = sc->sc_conf3; 475*55138Storek } 476*55138Storek esp->esp_ccf = sc->sc_ccf; 477*55138Storek esp->esp_timeout = sc->sc_sel_timeout; 478*55138Storek /* We set synch offset later. */ 479*55138Storek 480*55138Storek sc->sc_needclear = 0xff; 481*55138Storek } 482*55138Storek 483*55138Storek /* 484*55138Storek * Reset the SCSI bus and, optionally, all attached targets. 485*55138Storek * The chip should retain most of its parameters (including esp_ccf) 486*55138Storek * across this kind of reset (see section 3.5 of Emulex documentation). 487*55138Storek */ 488*55138Storek void 489*55138Storek esphbareset(hba, resetunits) 490*55138Storek struct hba_softc *hba; 491*55138Storek int resetunits; 492*55138Storek { 493*55138Storek register struct esp_softc *sc = (struct esp_softc *)hba; 494*55138Storek register volatile struct espreg *esp = sc->sc_esp; 495*55138Storek 496*55138Storek dmareset(sc); 497*55138Storek 498*55138Storek /* BEGIN ??? */ 499*55138Storek /* turn off scsi bus reset interrupts and reset scsi bus */ 500*55138Storek esp->esp_conf1 = sc->sc_conf1 | ESPCONF1_REPORT; 501*55138Storek DELAY(200); 502*55138Storek esp->esp_cmd = ESPCMD_RESET_BUS; 503*55138Storek DELAY(800); 504*55138Storek esp->esp_cmd = ESPCMD_NOP; 505*55138Storek DELAY(200); 506*55138Storek esp->esp_conf1 = sc->sc_conf1; 507*55138Storek /* END ??? */ 508*55138Storek 509*55138Storek sc->sc_needclear = 0xff; 510*55138Storek 511*55138Storek if (resetunits) 512*55138Storek scsi_reset_units(&sc->sc_hba); 513*55138Storek } 514*55138Storek 515*55138Storek /* 516*55138Storek * Reset the esp, after an Sbus reset. 517*55138Storek * Also resets corresponding dma chip. 518*55138Storek * 519*55138Storek * THIS ROUTINE MIGHT GO AWAY 520*55138Storek */ 521*55138Storek void 522*55138Storek espsbreset(dev) 523*55138Storek struct device *dev; 524*55138Storek { 525*55138Storek struct esp_softc *sc = (struct esp_softc *)dev; 526*55138Storek 527*55138Storek if (sc->sc_dsc) { 528*55138Storek printf(" %s %s", sc->sc_dsc->sc_dev.dv_xname, 529*55138Storek sc->sc_hba.hba_dev.dv_xname); 530*55138Storek esphbareset(&sc->sc_hba, 1); 531*55138Storek } 532*55138Storek } 533*55138Storek 534*55138Storek static void 535*55138Storek esperror(sc, err) 536*55138Storek char *err; 537*55138Storek register struct esp_softc *sc; 538*55138Storek { 539*55138Storek 540*55138Storek printf("%s: %s (target=%d): stat=%b step=%x dmacsr=%b intr=%b\n", 541*55138Storek sc->sc_hba.hba_dev.dv_xname, err, sc->sc_targ, 542*55138Storek sc->sc_espstat, ESPSTAT_BITS, sc->sc_espstep, 543*55138Storek sc->sc_dmacsr, DMA_BITS, sc->sc_espintr, ESPINTR_BITS); 544*55138Storek } 545*55138Storek 546*55138Storek /* 547*55138Storek * An interrupt has occurred. Sequence through the SCSI state machine. 548*55138Storek * Return the action to take. 549*55138Storek * 550*55138Storek * Most of the work happens here. 551*55138Storek * 552*55138Storek * There are three interrupt sources: 553*55138Storek * -- ESP interrupt request (typically, some device wants something). 554*55138Storek * -- DMA memory error. 555*55138Storek * -- DMA byte count has reached 0 (we do not often want this one but 556*55138Storek * can only turn it off in rev 2 DMA chips, it seems). 557*55138Storek * DOES THIS OCCUR AT ALL HERE? THERE IS NOTHING TO HANDLE IT! 558*55138Storek */ 559*55138Storek static int 560*55138Storek espact(sc, esp, dma, cdb) 561*55138Storek register struct esp_softc *sc; 562*55138Storek register volatile struct espreg *esp; 563*55138Storek register volatile struct dmareg *dma; 564*55138Storek register struct scsi_cdb *cdb; 565*55138Storek { 566*55138Storek register char *xname = sc->sc_hba.hba_dev.dv_xname; 567*55138Storek register int reg, phase, i; 568*55138Storek 569*55138Storek /* check various error conditions, using as little code as possible */ 570*55138Storek if (sc->sc_dmacsr & DMA_EP) { 571*55138Storek esperror(sc, "DMA error"); 572*55138Storek dma->dma_csr |= DMA_FLUSH; 573*55138Storek return (ACT_ERROR); 574*55138Storek } 575*55138Storek reg = sc->sc_espstat; 576*55138Storek if (reg & ESPSTAT_GE) { 577*55138Storek /* 578*55138Storek * This often occurs when there is no target. 579*55138Storek * (See DSC code below.) 580*55138Storek */ 581*55138Storek if (sc->sc_espintr & ESPINTR_DSC && 582*55138Storek sc->sc_state == S_SEL && sc->sc_probing) { 583*55138Storek sc->sc_probing = PROBE_NO_TARGET; 584*55138Storek return (ACT_RESET); 585*55138Storek } 586*55138Storek esperror(sc, "DIAGNOSTIC: gross error (ignored)"); 587*55138Storek } 588*55138Storek if (reg & ESPSTAT_PE) { 589*55138Storek esperror(sc, "parity error"); 590*55138Storek return (ACT_RESET); 591*55138Storek } 592*55138Storek reg = sc->sc_espintr; 593*55138Storek #define ERR (ESPINTR_SBR|ESPINTR_ILC|ESPINTR_RSL|ESPINTR_SAT|ESPINTR_SEL) 594*55138Storek if (reg & ERR) { 595*55138Storek if (reg & ESPINTR_SBR) 596*55138Storek esperror(sc, "scsi bus reset"); 597*55138Storek else if (reg & ESPINTR_ILC) 598*55138Storek esperror(sc, "illegal command (driver bug)"); 599*55138Storek else { 600*55138Storek printf("%s: target %d", xname, sc->sc_targ); 601*55138Storek if (reg & ESPINTR_RSL) 602*55138Storek printf(" tried to reselect;"); 603*55138Storek if (reg & ESPINTR_SAT) 604*55138Storek printf(" selected with ATN;"); 605*55138Storek if (reg & ESPINTR_SEL) 606*55138Storek printf(" selected us as target;"); 607*55138Storek printf("we do not allow this yet\n"); 608*55138Storek } 609*55138Storek return (ACT_ERROR); 610*55138Storek } 611*55138Storek #undef ERR 612*55138Storek 613*55138Storek /* 614*55138Storek * Disconnect currently only allowed in `final interrupt' states. 615*55138Storek */ 616*55138Storek if (reg & ESPINTR_DSC) { 617*55138Storek if (sc->sc_state == S_FI) 618*55138Storek return (ACT_DONE); 619*55138Storek /* 620*55138Storek * If we were doing a select just to test the existence 621*55138Storek * of the target, note that it did not respond; otherwise 622*55138Storek * gripe. 623*55138Storek */ 624*55138Storek if (sc->sc_state == S_SEL) { 625*55138Storek if (sc->sc_probing) { 626*55138Storek sc->sc_probing = PROBE_NO_TARGET; 627*55138Storek return (ACT_RESET); 628*55138Storek } 629*55138Storek } 630*55138Storek /* flush fifo, in case we were selecting or sending data */ 631*55138Storek esp->esp_cmd = ESPCMD_FLUSH_FIFO; 632*55138Storek printf("%s: target %d not responding\n", 633*55138Storek xname, sc->sc_targ); 634*55138Storek return (ACT_ERROR); 635*55138Storek } 636*55138Storek 637*55138Storek /* 638*55138Storek * Okay, things are moving along. 639*55138Storek * What were we doing the last time we did something, 640*55138Storek * and did it complete normally? 641*55138Storek */ 642*55138Storek phase = sc->sc_espstat & ESPSTAT_PHASE; 643*55138Storek switch (sc->sc_state) { 644*55138Storek 645*55138Storek case S_SEL: 646*55138Storek /* 647*55138Storek * We were selecting. Arbitration and select are 648*55138Storek * complete (because ESPINTR_DSC was not set), but 649*55138Storek * there is no guarantee the command went out. 650*55138Storek */ 651*55138Storek if ((reg & (ESPINTR_SVC|ESPINTR_CMP)) != 652*55138Storek (ESPINTR_SVC|ESPINTR_CMP)) { 653*55138Storek esperror(sc, "selection failed"); 654*55138Storek return (ACT_RESET); 655*55138Storek } 656*55138Storek if (sc->sc_espstep == ESPSTEP_DONE) { 657*55138Storek sc->sc_sentcmd = 1; 658*55138Storek break; 659*55138Storek } 660*55138Storek if (sc->sc_espstep == 2) { 661*55138Storek /* 662*55138Storek * We got something other than command phase. 663*55138Storek * Just pretend things are normal; the 664*55138Storek * device will ask for the command later. 665*55138Storek */ 666*55138Storek esperror(sc, "DIAGNOSTIC: esp step 2"); 667*55138Storek } else if (sc->sc_espstep == 3) { 668*55138Storek /* 669*55138Storek * Device entered command phase and then exited it 670*55138Storek * before we finished handing out the command. 671*55138Storek * Let this happen iff we are trying to clear the 672*55138Storek * target state. 673*55138Storek */ 674*55138Storek esperror(sc, "DIAGNOSTIC: esp step 3"); 675*55138Storek if (!sc->sc_clearing) 676*55138Storek return (ACT_RESET); 677*55138Storek } else { 678*55138Storek printf("%s: mysterious esp step %d\n", 679*55138Storek xname, sc->sc_espstep); 680*55138Storek return (ACT_RESET); 681*55138Storek } 682*55138Storek /* 683*55138Storek * Part of the command may still be lodged in the FIFO. 684*55138Storek */ 685*55138Storek esp->esp_cmd = ESPCMD_FLUSH_FIFO; 686*55138Storek break; 687*55138Storek 688*55138Storek case S_CMDSVC: 689*55138Storek /* 690*55138Storek * We were waiting for phase change after stuffing the command 691*55138Storek * into the FIFO. Make sure it got out. 692*55138Storek */ 693*55138Storek reg = ESP_NFIFO(esp); 694*55138Storek if (reg) { 695*55138Storek esperror(sc, "DIAGNOSTIC: CMDSVC, fifo not empty"); 696*55138Storek printf("\tfifo count = %x\n", reg); 697*55138Storek esp->esp_cmd = ESPCMD_FLUSH_FIFO; 698*55138Storek } else 699*55138Storek sc->sc_sentcmd = 1; 700*55138Storek break; 701*55138Storek 702*55138Storek case S_IOSVC: 703*55138Storek /* 704*55138Storek * We were waiting for phase change after I/O. 705*55138Storek */ 706*55138Storek break; 707*55138Storek 708*55138Storek case S_DI: 709*55138Storek /* 710*55138Storek * We were doing DMA data in, and expecting a 711*55138Storek * transfer-count-zero interrupt or a phase change. 712*55138Storek * We got that; drain the pack register and 713*55138Storek * handle as for data out. 714*55138Storek */ 715*55138Storek dma->dma_csr |= DMA_DRAIN; 716*55138Storek reg = 0; /* FIFO auto flushed? */ 717*55138Storek goto dma_data_done; 718*55138Storek 719*55138Storek case S_DO: 720*55138Storek /* 721*55138Storek * We were doing DMA data out. If there is data in the 722*55138Storek * FIFO, it is stuff that got DMAed out but never made 723*55138Storek * it to the device, so it counts as residual. 724*55138Storek * 725*55138Storek * XXX handle DMA IO with large count or address 726*55138Storek * boundary condition by resuming here, or below? 727*55138Storek */ 728*55138Storek if ((reg = ESP_NFIFO(esp)) != 0) 729*55138Storek esp->esp_cmd = ESPCMD_FLUSH_FIFO; 730*55138Storek dma_data_done: 731*55138Storek if (sc->sc_dmaactive == 0) { 732*55138Storek printf("%s: dma done while %s, dmaactive==0\n", 733*55138Storek xname, espstates[sc->sc_state]); 734*55138Storek panic("espact"); 735*55138Storek } 736*55138Storek sc->sc_dmaactive = 0; 737*55138Storek reg += esp->esp_tcl | (esp->esp_tch << 8); 738*55138Storek if (reg == 0 && (sc->sc_espstat & ESPSTAT_TC) == 0) 739*55138Storek reg = 65536; 740*55138Storek if (reg > sc->sc_resid) { 741*55138Storek printf("%s: xfer resid (%d) > xfer req (%d)\n", 742*55138Storek xname, reg, sc->sc_resid); 743*55138Storek reg = sc->sc_resid; 744*55138Storek } 745*55138Storek /* 746*55138Storek * If data came in we must flush cache. 747*55138Storek */ 748*55138Storek if (sc->sc_state == S_DI) 749*55138Storek cache_flush(sc->sc_dmaaddr, sc->sc_resid - reg); 750*55138Storek sc->sc_resid = reg; 751*55138Storek if ((sc->sc_espintr & ESPINTR_SVC) == 0) { 752*55138Storek printf("%s: no bus service req\n", xname); 753*55138Storek return (ACT_RESET); 754*55138Storek } 755*55138Storek break; 756*55138Storek 757*55138Storek case S_STAT: 758*55138Storek /* 759*55138Storek * The last thing we did was tell it `initiator complete' 760*55138Storek * and so we expect to have gotten both the status byte 761*55138Storek * and the final message byte. It is possible that we 762*55138Storek * got something else.... 763*55138Storek * 764*55138Storek * Apparently, BUS SERVICE is set if we got just status, 765*55138Storek * while FUNCTION COMPLETE is set if we got both. 766*55138Storek */ 767*55138Storek if ((reg & (ESPINTR_SVC|ESPINTR_CMP)) != ESPINTR_CMP) { 768*55138Storek esperror(sc, "bad status interrupt state"); 769*55138Storek return (ACT_RESET); 770*55138Storek } 771*55138Storek reg = ESP_NFIFO(esp); 772*55138Storek if (reg < 2) { 773*55138Storek printf( 774*55138Storek "%s: command done but fifo count = %d; must be >= 2\n", xname, 775*55138Storek reg); 776*55138Storek return (ACT_RESET); 777*55138Storek } 778*55138Storek /* 779*55138Storek * Read the status and the first msg byte. 780*55138Storek * It should be CMD_COMPLETE. Eventually we 781*55138Storek * may handle IDENTIFY, DISCONNECT, etc., as well. 782*55138Storek */ 783*55138Storek sc->sc_stat[0] = esp->esp_fifo; 784*55138Storek sc->sc_msg[0] = reg = esp->esp_fifo; 785*55138Storek esp->esp_cmd = ESPCMD_MSG_ACCEPT; 786*55138Storek if (reg == MSG_CMD_COMPLETE) { 787*55138Storek sc->sc_state = S_FI; 788*55138Storek return (ACT_CONT); 789*55138Storek } 790*55138Storek if (SCSIMSGLEN(reg) != 1) { 791*55138Storek printf("%s: target %d is naughty\n", 792*55138Storek xname, sc->sc_targ); 793*55138Storek return (ACT_RESET); 794*55138Storek } 795*55138Storek printf("%s: warning: target %d returned msg 0x%x\n", 796*55138Storek xname, sc->sc_targ, reg); 797*55138Storek sc->sc_state = S_FI; 798*55138Storek return (ACT_CONT); 799*55138Storek 800*55138Storek case S_MI: 801*55138Storek if ((reg & ESPINTR_SVC) == 0) { 802*55138Storek esperror(sc, "missing phase after msg in"); 803*55138Storek return (ACT_RESET); 804*55138Storek } 805*55138Storek reg = ESP_NFIFO(esp); 806*55138Storek for (i = 0; i < reg; i++) 807*55138Storek sc->sc_msg[i] = esp->esp_fifo; 808*55138Storek break; 809*55138Storek 810*55138Storek case S_FI: 811*55138Storek esperror(sc, "target did not disconnect"); 812*55138Storek return (ACT_RESET); 813*55138Storek } 814*55138Storek 815*55138Storek /* 816*55138Storek * Things are still moving along. The phase tells us 817*55138Storek * what the device wants next. Do it. 818*55138Storek */ 819*55138Storek switch (phase) { 820*55138Storek 821*55138Storek case ESPPHASE_DATA_OUT: 822*55138Storek if (!sc->sc_sentcmd) esperror(sc, "DIAGNOSTIC: data out without command"); 823*55138Storek sc->sc_state = S_DO; 824*55138Storek return (ACT_WRITE); 825*55138Storek 826*55138Storek case ESPPHASE_DATA_IN: 827*55138Storek if (!sc->sc_sentcmd) esperror(sc, "DIAGNOSTIC: data in without command"); 828*55138Storek sc->sc_state = S_DI; 829*55138Storek return (ACT_READ); 830*55138Storek 831*55138Storek case ESPPHASE_CMD: 832*55138Storek /* 833*55138Storek * Silly thing wants the command again. 834*55138Storek * Load it into the FIFO and go to CMDSVC state. 835*55138Storek */ 836*55138Storek printf("%s: redoing command\n", xname); 837*55138Storek reg = SCSICMDLEN(cdb->cdb_bytes[0]); 838*55138Storek for (i = 0; i < reg; i++) 839*55138Storek esp->esp_fifo = cdb->cdb_bytes[i]; 840*55138Storek sc->sc_state = S_CMDSVC; 841*55138Storek esp->esp_cmd = ESPCMD_XFER_INFO; 842*55138Storek return (ACT_CONT); 843*55138Storek 844*55138Storek case ESPPHASE_STATUS: 845*55138Storek sc->sc_state = S_STAT; 846*55138Storek esp->esp_cmd = ESPCMD_INIT_COMP; 847*55138Storek return (ACT_CONT); 848*55138Storek 849*55138Storek case ESPPHASE_MSG_IN: 850*55138Storek printf("%s: accepting (& ignoring) msg from target %d\n", xname, sc->sc_targ); 851*55138Storek sc->sc_state = S_MI; 852*55138Storek esp->esp_cmd = ESPCMD_MSG_ACCEPT; 853*55138Storek return (ACT_CONT); 854*55138Storek 855*55138Storek default: 856*55138Storek printf("%s: target %d asked for strange phase (%s)\n", 857*55138Storek xname, sc->sc_targ, espphases[phase]); 858*55138Storek return (ACT_RESET); 859*55138Storek } 860*55138Storek /* NOTREACHED */ 861*55138Storek } 862*55138Storek 863*55138Storek /* 864*55138Storek * Issue a select, loading command into the FIFO. 865*55138Storek * Return nonzero on error, 0 if OK. 866*55138Storek * Sets state to `selecting'; espact() will sequence state FSM. 867*55138Storek */ 868*55138Storek void 869*55138Storek espselect(sc, esp, targ, cdb) 870*55138Storek register struct esp_softc *sc; 871*55138Storek register volatile struct espreg *esp; 872*55138Storek register int targ; 873*55138Storek register struct scsi_cdb *cdb; 874*55138Storek { 875*55138Storek register int i, cmdlen = SCSICMDLEN(cdb->cdb_bytes[0]); 876*55138Storek 877*55138Storek sc->sc_targ = targ; 878*55138Storek sc->sc_state = S_SEL; 879*55138Storek sc->sc_sentcmd = 0; 880*55138Storek sc->sc_stat[0] = 0xff; /* ??? */ 881*55138Storek sc->sc_msg[0] = 0xff; /* ??? */ 882*55138Storek 883*55138Storek /* 884*55138Storek * Try to talk to target. 885*55138Storek * Synch offset 0 => asynchronous transfer. 886*55138Storek */ 887*55138Storek esp->esp_id = targ; 888*55138Storek esp->esp_syncoff = 0; 889*55138Storek 890*55138Storek /* 891*55138Storek * Stuff the command bytes into the fifo. 892*55138Storek * Select without attention since we do not do disconnect yet. 893*55138Storek */ 894*55138Storek for (i = 0; i < cmdlen; i++) 895*55138Storek esp->esp_fifo = cdb->cdb_bytes[i]; 896*55138Storek esp->esp_cmd = ESPCMD_SEL_NATN; 897*55138Storek /* the rest is done elsewhere */ 898*55138Storek } 899*55138Storek 900*55138Storek /* 901*55138Storek * THIS SHOULD BE ADJUSTABLE 902*55138Storek */ 903*55138Storek /* name howlong purpose */ 904*55138Storek #define SELECT_WAIT 300000 /* wait for select to complete */ 905*55138Storek #define CMD_WAIT 1000 /* wait for next phase, generic */ 906*55138Storek #define IO_WAIT 1000000 /* time to xfer data in/out */ 907*55138Storek #define POSTDATA_WAIT 10000000 /* wait for next phase, after dataio */ 908*55138Storek 909*55138Storek /* 910*55138Storek * Transfer data out via polling. Return success (0) iff all 911*55138Storek * the bytes were sent and we got an interrupt. 912*55138Storek * 913*55138Storek * This returns -1 on timeout, resid count on early interrupt, 914*55138Storek * but no one really cares.... 915*55138Storek */ 916*55138Storek static int 917*55138Storek espixfer_out(sc, esp, dma, buf, len) 918*55138Storek register struct esp_softc *sc; 919*55138Storek register volatile struct espreg *esp; 920*55138Storek register volatile struct dmareg *dma; 921*55138Storek register caddr_t buf; 922*55138Storek register int len; 923*55138Storek { 924*55138Storek register int wait, n; 925*55138Storek 926*55138Storek if (CROSS_DMA(buf, len)) 927*55138Storek panic("espixfer_out: 16MB boundary"); 928*55138Storek 929*55138Storek /* set dma address and transfer count */ 930*55138Storek dma->dma_addr = (int)buf; 931*55138Storek esp->esp_tch = len >> 8; 932*55138Storek esp->esp_tcl = len; 933*55138Storek 934*55138Storek /* load count into counter via DMA NOP */ 935*55138Storek esp->esp_cmd = ESPCMD_DMA | ESPCMD_NOP; 936*55138Storek 937*55138Storek /* enable dma (but not interrupts) */ 938*55138Storek dma->dma_csr = DMA_ENA; 939*55138Storek 940*55138Storek /* and go */ 941*55138Storek esp->esp_cmd = ESPCMD_DMA | ESPCMD_XFER_INFO; 942*55138Storek 943*55138Storek /* wait for completion */ 944*55138Storek for (wait = IO_WAIT; wait > 0; --wait) { 945*55138Storek n = dma->dma_csr; 946*55138Storek if (DMA_INTR(n)) { 947*55138Storek sc->sc_espstat = esp->esp_stat; 948*55138Storek sc->sc_espstep = esp->esp_step & ESPSTEP_MASK; 949*55138Storek sc->sc_espintr = esp->esp_intr; 950*55138Storek sc->sc_dmacsr = n; 951*55138Storek n = esp->esp_tcl | (esp->esp_tch << 8); 952*55138Storek if (n == 0 && (sc->sc_espstat & ESPSTAT_TC) == 0) 953*55138Storek n = 65536; 954*55138Storek 955*55138Storek return (n); 956*55138Storek } 957*55138Storek DELAY(1); 958*55138Storek } 959*55138Storek return (-1); 960*55138Storek } 961*55138Storek 962*55138Storek /* 963*55138Storek * Transfer data in via polling. 964*55138Storek * Return resid count on interrupt, -1 if timed out. 965*55138Storek */ 966*55138Storek static int 967*55138Storek espixfer_in(sc, esp, dma, buf, len) 968*55138Storek register struct esp_softc *sc; 969*55138Storek register volatile struct espreg *esp; 970*55138Storek register volatile struct dmareg *dma; 971*55138Storek register caddr_t buf; 972*55138Storek register int len; 973*55138Storek { 974*55138Storek register int wait, n; 975*55138Storek 976*55138Storek if (CROSS_DMA(buf, len)) 977*55138Storek panic("espixfer_in: 16MB boundary"); 978*55138Storek 979*55138Storek /* set dma address and transfer count */ 980*55138Storek dma->dma_addr = (int)buf; 981*55138Storek esp->esp_tch = len >> 8; 982*55138Storek esp->esp_tcl = len; 983*55138Storek 984*55138Storek /* load count into counter via DMA NOP */ 985*55138Storek esp->esp_cmd = ESPCMD_DMA | ESPCMD_NOP; 986*55138Storek 987*55138Storek /* enable dma (but not interrupts) */ 988*55138Storek dma->dma_csr = DMA_ENA | DMA_READ; 989*55138Storek 990*55138Storek /* and go */ 991*55138Storek esp->esp_cmd = ESPCMD_DMA | ESPCMD_XFER_INFO; 992*55138Storek 993*55138Storek /* wait for completion */ 994*55138Storek for (wait = IO_WAIT; wait > 0; --wait) { 995*55138Storek n = dma->dma_csr; 996*55138Storek if (DMA_INTR(n)) { 997*55138Storek sc->sc_espstat = esp->esp_stat; 998*55138Storek sc->sc_espstep = esp->esp_step & ESPSTEP_MASK; 999*55138Storek sc->sc_espintr = esp->esp_intr; 1000*55138Storek dma->dma_csr |= DMA_DRAIN; 1001*55138Storek sc->sc_dmacsr = n; 1002*55138Storek n = esp->esp_tcl | (esp->esp_tch << 8); 1003*55138Storek if (n == 0 && (sc->sc_espstat & ESPSTAT_TC) == 0) 1004*55138Storek n = 65536; 1005*55138Storek 1006*55138Storek cache_flush(buf, (u_int)len - n); 1007*55138Storek return (n); 1008*55138Storek } 1009*55138Storek DELAY(1); 1010*55138Storek } 1011*55138Storek return (-1); 1012*55138Storek } 1013*55138Storek 1014*55138Storek /* 1015*55138Storek * Clear out target state by doing a special TEST UNIT READY. 1016*55138Storek * Note that this calls espicmd (possibly recursively). 1017*55138Storek */ 1018*55138Storek void 1019*55138Storek espclear(sc, targ) 1020*55138Storek register struct esp_softc *sc; 1021*55138Storek register int targ; 1022*55138Storek { 1023*55138Storek 1024*55138Storek /* turn off needclear immediately since this calls espicmd() again */ 1025*55138Storek sc->sc_needclear &= ~(1 << targ); 1026*55138Storek sc->sc_clearing = 1; 1027*55138Storek (void) scsi_test_unit_ready(&sc->sc_hba, targ, 0); 1028*55138Storek sc->sc_clearing = 0; 1029*55138Storek } 1030*55138Storek 1031*55138Storek /* 1032*55138Storek * Send an `immediate' command, i.e., poll until the whole thing is done. 1033*55138Storek * Return the status byte from the device, or -1 if we timed out. 1034*55138Storek */ 1035*55138Storek int 1036*55138Storek espicmd(hba, targ, cdb, buf, len, rw) 1037*55138Storek register struct hba_softc *hba; 1038*55138Storek int targ; 1039*55138Storek register struct scsi_cdb *cdb; 1040*55138Storek caddr_t buf; 1041*55138Storek register int len; 1042*55138Storek int rw; 1043*55138Storek { 1044*55138Storek register struct esp_softc *sc = (struct esp_softc *)hba; 1045*55138Storek register volatile struct espreg *esp = sc->sc_esp; 1046*55138Storek register volatile struct dmareg *dma = sc->sc_dma; 1047*55138Storek register int r, wait; 1048*55138Storek char *msg; 1049*55138Storek 1050*55138Storek if ((unsigned)len > MAX_TRANSFER_SIZE) { 1051*55138Storek printf("%s: bad length %d\n", sc->sc_hba.hba_dev.dv_xname, len); 1052*55138Storek panic("espicmd"); 1053*55138Storek } 1054*55138Storek 1055*55138Storek /* 1056*55138Storek * Clear the target if necessary. 1057*55138Storek */ 1058*55138Storek if (sc->sc_needclear & (1 << targ) && !sc->sc_probing) 1059*55138Storek espclear(sc, targ); 1060*55138Storek 1061*55138Storek /* 1062*55138Storek * Disable hardware interrupts, start select sequence. 1063*55138Storek * Wait for interrupt-pending bit, then call espact() to 1064*55138Storek * sequence the state machine. When it tells us to do 1065*55138Storek * data transfer, we do programmed I/O. 1066*55138Storek * In any case, we loop calling espact() until done. 1067*55138Storek */ 1068*55138Storek dma->dma_csr = 0; /* disable hardware interrupts */ 1069*55138Storek espselect(sc, esp, targ, cdb); 1070*55138Storek wait = SELECT_WAIT; 1071*55138Storek loop: 1072*55138Storek for (;;) { 1073*55138Storek r = dma->dma_csr; 1074*55138Storek if (!DMA_INTR(r)) { 1075*55138Storek if (--wait < 0) { 1076*55138Storek msg = "timeout waiting for phase change"; 1077*55138Storek goto err; 1078*55138Storek } 1079*55138Storek DELAY(1); 1080*55138Storek continue; 1081*55138Storek } 1082*55138Storek break; 1083*55138Storek } 1084*55138Storek sc->sc_espstat = esp->esp_stat; 1085*55138Storek sc->sc_espstep = esp->esp_step & ESPSTEP_MASK; 1086*55138Storek sc->sc_espintr = esp->esp_intr; 1087*55138Storek sc->sc_dmacsr = r; 1088*55138Storek /* 1089*55138Storek * The action happens `twice around' for read and write. 1090*55138Storek * All the rest `goto loop' or return or some such. 1091*55138Storek */ 1092*55138Storek wait = CMD_WAIT; 1093*55138Storek for (;;) { 1094*55138Storek switch (r = espact(sc, esp, dma, cdb)) { 1095*55138Storek 1096*55138Storek case ACT_CONT: 1097*55138Storek case ACT_QUICKINTR: 1098*55138Storek goto loop; 1099*55138Storek 1100*55138Storek case ACT_READ: 1101*55138Storek if (len == 0 || (rw & B_READ) == 0) { 1102*55138Storek msg = "wrong phase"; 1103*55138Storek goto err; 1104*55138Storek } 1105*55138Storek r = espixfer_in(sc, esp, dma, buf, len); 1106*55138Storek if (r < 0) { 1107*55138Storek msg = "timeout reading from device"; 1108*55138Storek goto err; 1109*55138Storek } 1110*55138Storek buf += len - r; 1111*55138Storek len = r; 1112*55138Storek /* we did the io, expecting `generic service' */ 1113*55138Storek sc->sc_state = S_IOSVC; 1114*55138Storek wait = POSTDATA_WAIT; 1115*55138Storek break; 1116*55138Storek 1117*55138Storek case ACT_WRITE: 1118*55138Storek if (len == 0 || rw & B_READ) { 1119*55138Storek msg = "wrong phase"; 1120*55138Storek goto err; 1121*55138Storek } 1122*55138Storek if (espixfer_out(sc, esp, dma, buf, len)) { 1123*55138Storek msg = "timeout writing to device"; 1124*55138Storek goto err; 1125*55138Storek } 1126*55138Storek sc->sc_state = S_IOSVC; 1127*55138Storek wait = POSTDATA_WAIT; 1128*55138Storek break; 1129*55138Storek 1130*55138Storek case ACT_RESET: 1131*55138Storek sc->sc_state = S_IDLE; 1132*55138Storek goto reset; 1133*55138Storek 1134*55138Storek case ACT_DONE: 1135*55138Storek sc->sc_state = S_IDLE; 1136*55138Storek return (sc->sc_stat[0]); 1137*55138Storek 1138*55138Storek case ACT_ERROR: 1139*55138Storek sc->sc_state = S_IDLE; 1140*55138Storek return (-1); 1141*55138Storek 1142*55138Storek default: 1143*55138Storek panic("espicmd action"); 1144*55138Storek } 1145*55138Storek } 1146*55138Storek err: 1147*55138Storek printf("%s: target %d: %s (phase = %s)\n", 1148*55138Storek sc->sc_hba.hba_dev.dv_xname, targ, msg, 1149*55138Storek espphases[sc->sc_espstat & ESPSTAT_PHASE]); 1150*55138Storek reset: 1151*55138Storek espreset(sc); /* ??? */ 1152*55138Storek return (-1); 1153*55138Storek } 1154*55138Storek 1155*55138Storek /* 1156*55138Storek * Dump (write memory, possibly physmem). 1157*55138Storek * SPARC higher-level dump code always provides virtual addresses, 1158*55138Storek * so we need not do any I/O mapping here. 1159*55138Storek */ 1160*55138Storek int 1161*55138Storek espdump(hba, targ, cdb, buf, len) 1162*55138Storek register struct hba_softc *hba; 1163*55138Storek int targ; 1164*55138Storek register struct scsi_cdb *cdb; 1165*55138Storek caddr_t buf; 1166*55138Storek register int len; 1167*55138Storek { 1168*55138Storek 1169*55138Storek return (espicmd(hba, targ, cdb, buf, len, B_WRITE)); 1170*55138Storek } 1171*55138Storek 1172*55138Storek /* 1173*55138Storek * Allocate resources (SCSI bus and DVMA space) for the given transfer. 1174*55138Storek * Must be called at splbio(). 1175*55138Storek * 1176*55138Storek * THIS SHOULD RETURN SUCCESS/FAIL INDICATION 1177*55138Storek */ 1178*55138Storek void 1179*55138Storek espstart(self, sq, bp, dgo, dev) 1180*55138Storek struct device *self; 1181*55138Storek register struct sq *sq; 1182*55138Storek struct buf *bp; 1183*55138Storek scdgo_fn dgo; 1184*55138Storek struct device *dev; 1185*55138Storek { 1186*55138Storek register struct esp_softc *sc = (struct esp_softc *)self; 1187*55138Storek 1188*55138Storek if (sc->sc_hba.hba_busy == 0) { 1189*55138Storek /* 1190*55138Storek * Bus not busy, nothing to do here, just tell 1191*55138Storek * this target or unit that it has the SCSI bus. 1192*55138Storek */ 1193*55138Storek sc->sc_hba.hba_busy = 1; 1194*55138Storek (*dgo)(dev, &sc->sc_cdb); 1195*55138Storek } else { 1196*55138Storek /* 1197*55138Storek * Bus is busy; just enqueue. 1198*55138Storek */ 1199*55138Storek sq->sq_dgo = dgo; 1200*55138Storek sq->sq_dev = dev; 1201*55138Storek sq->sq_forw = NULL; 1202*55138Storek if (sc->sc_hba.hba_head == NULL) 1203*55138Storek sc->sc_hba.hba_head = sq; 1204*55138Storek else 1205*55138Storek sc->sc_hba.hba_tail->sq_forw = sq; 1206*55138Storek sc->sc_hba.hba_tail = sq; 1207*55138Storek } 1208*55138Storek } 1209*55138Storek 1210*55138Storek /* 1211*55138Storek * Send a `dma' command, i.e., send the cdb and use DMA to send the data. 1212*55138Storek * Return 0 on success, 1 on failure. 1213*55138Storek */ 1214*55138Storek int 1215*55138Storek espgo(self, targ, intr, dev, bp, pad) 1216*55138Storek struct device *self; 1217*55138Storek int targ; 1218*55138Storek scintr_fn intr; 1219*55138Storek struct device *dev; 1220*55138Storek register struct buf *bp; 1221*55138Storek int pad; 1222*55138Storek { 1223*55138Storek register struct esp_softc *sc = (struct esp_softc *)self; 1224*55138Storek register int len = bp->b_bcount; 1225*55138Storek register u_long addr; 1226*55138Storek 1227*55138Storek if ((unsigned)len > MAX_TRANSFER_SIZE) { 1228*55138Storek printf("%s: %s\n", sc->sc_hba.hba_dev.dv_xname, 1229*55138Storek len < 0 ? "negative length" : "transfer too big"); 1230*55138Storek return (1); 1231*55138Storek } 1232*55138Storek 1233*55138Storek if (sc->sc_needclear & (1 << targ)) 1234*55138Storek espclear(sc, targ); 1235*55138Storek 1236*55138Storek /* 1237*55138Storek * Set dma registers later, on data transfer, 1238*55138Storek * but compute the contents now. 1239*55138Storek * COULD JUST REMEMBER bp HERE...? 1240*55138Storek * 1241*55138Storek * The DMA chip cannot cross a 16 MB address boundary. 1242*55138Storek * We should do this as multiple DMA transactions on a 1243*55138Storek * single SCSI command, but I have not written that yet. 1244*55138Storek */ 1245*55138Storek sc->sc_dmactl = bp->b_flags & B_READ ? DMA_ENA | DMA_READ | DMA_IE : 1246*55138Storek DMA_ENA | DMA_IE; 1247*55138Storek addr = (u_long)bp->b_un.b_addr; 1248*55138Storek /* dma chip cannot cross 16MB boundary XXX */ 1249*55138Storek if (CROSS_DMA(addr, len)) 1250*55138Storek panic("dma crosses 16MB boundary: fix esp.c"); 1251*55138Storek sc->sc_dmaaddr = addr; 1252*55138Storek sc->sc_resid = len; 1253*55138Storek 1254*55138Storek /* 1255*55138Storek * Enable interrupts and start selection. 1256*55138Storek * The rest is done in our interrupt handler. 1257*55138Storek */ 1258*55138Storek sc->sc_hba.hba_intr = intr; /* remember dev done function */ 1259*55138Storek sc->sc_hba.hba_intrdev = dev; /* and its first arg */ 1260*55138Storek sc->sc_dma->dma_csr = DMA_IE; 1261*55138Storek espselect(sc, sc->sc_esp, targ, &sc->sc_cdb); 1262*55138Storek return (0); 1263*55138Storek } 1264*55138Storek 1265*55138Storek /* 1266*55138Storek * Handle interrupt. Return 1 if taken. 1267*55138Storek */ 1268*55138Storek int 1269*55138Storek espintr(sc0) 1270*55138Storek void *sc0; 1271*55138Storek { 1272*55138Storek register struct esp_softc *sc = (struct esp_softc *)sc0; 1273*55138Storek register volatile struct espreg *esp = sc->sc_esp; 1274*55138Storek register volatile struct dmareg *dma = sc->sc_dma; 1275*55138Storek register int r, wait; 1276*55138Storek register struct sq *sq; 1277*55138Storek 1278*55138Storek r = dma->dma_csr; 1279*55138Storek if (!DMA_INTR(r)) 1280*55138Storek return (0); /* not ours */ 1281*55138Storek sc->sc_interrupts++; 1282*55138Storek 1283*55138Storek again: 1284*55138Storek sc->sc_espstat = esp->esp_stat; 1285*55138Storek sc->sc_espstep = esp->esp_step & ESPSTEP_MASK; 1286*55138Storek sc->sc_espintr = esp->esp_intr; 1287*55138Storek sc->sc_dmacsr = r; 1288*55138Storek 1289*55138Storek if (sc->sc_state == S_IDLE) { 1290*55138Storek printf("%s: stray interrupt\n", sc->sc_hba.hba_dev.dv_xname); 1291*55138Storek dma->dma_csr &= ~DMA_IE; /* ??? */ 1292*55138Storek return (1); 1293*55138Storek } 1294*55138Storek switch (r = espact(sc, esp, dma, &sc->sc_cdb)) { 1295*55138Storek 1296*55138Storek case ACT_CONT: /* just return */ 1297*55138Storek break; 1298*55138Storek 1299*55138Storek case ACT_READ: 1300*55138Storek case ACT_WRITE: 1301*55138Storek /* 1302*55138Storek * We have to do this ourselves since another 1303*55138Storek * user of espact() wants to do programmed I/O. 1304*55138Storek * If we already did dma, and are done, stop. 1305*55138Storek */ 1306*55138Storek if (sc->sc_resid == 0) { 1307*55138Storek printf("%s: target %d sent too much data\n", 1308*55138Storek sc->sc_hba.hba_dev.dv_xname, sc->sc_targ); 1309*55138Storek goto reset; 1310*55138Storek } 1311*55138Storek sc->sc_dmaactive = 1; 1312*55138Storek dma->dma_addr = sc->sc_dmaaddr; 1313*55138Storek esp->esp_tch = sc->sc_resid >> 8; 1314*55138Storek esp->esp_tcl = sc->sc_resid; 1315*55138Storek /* load count into counter via DMA NOP */ 1316*55138Storek esp->esp_cmd = ESPCMD_DMA | ESPCMD_NOP; 1317*55138Storek /* enable dma */ 1318*55138Storek dma->dma_csr = sc->sc_dmactl; 1319*55138Storek /* and go */ 1320*55138Storek esp->esp_cmd = ESPCMD_DMA | ESPCMD_XFER_INFO; 1321*55138Storek break; 1322*55138Storek 1323*55138Storek case ACT_RESET: /* please reset esp */ 1324*55138Storek reset: 1325*55138Storek espreset(sc); /* ??? */ 1326*55138Storek /* FALLTHROUGH */ 1327*55138Storek 1328*55138Storek case ACT_DONE: /* this one is done, successfully */ 1329*55138Storek case ACT_ERROR: /* this one is done due to `severe' error */ 1330*55138Storek sc->sc_state = S_IDLE; 1331*55138Storek if (!sc->sc_hba.hba_busy) 1332*55138Storek panic("espintr sq"); 1333*55138Storek /* 1334*55138Storek * This transaction is done. 1335*55138Storek * Call the driver's intr routine, 1336*55138Storek * then start the next guy if any. 1337*55138Storek */ 1338*55138Storek (*sc->sc_hba.hba_intr)(sc->sc_hba.hba_intrdev, 1339*55138Storek r == ACT_DONE ? sc->sc_stat[0] : -1, sc->sc_resid); 1340*55138Storek if ((sq = sc->sc_hba.hba_head) != NULL) { 1341*55138Storek sc->sc_hba.hba_head = sq->sq_forw; 1342*55138Storek (*sq->sq_dgo)(sq->sq_dev, &sc->sc_cdb); 1343*55138Storek } else 1344*55138Storek sc->sc_hba.hba_busy = 0; 1345*55138Storek break; 1346*55138Storek 1347*55138Storek case ACT_QUICKINTR: /* wait a short while for another interrupt */ 1348*55138Storek printf("%s: quickintr: ", sc->sc_hba.hba_dev.dv_xname); 1349*55138Storek wait = 100; 1350*55138Storek do { 1351*55138Storek r = dma->dma_csr; 1352*55138Storek if (DMA_INTR(r)) { 1353*55138Storek printf("got one, wait=%d\n", wait); 1354*55138Storek goto again; 1355*55138Storek } 1356*55138Storek } while (--wait > 0); 1357*55138Storek printf("did not get one\n"); 1358*55138Storek break; 1359*55138Storek 1360*55138Storek default: 1361*55138Storek panic("espintr action"); 1362*55138Storek } 1363*55138Storek return (1); 1364*55138Storek } 1365*55138Storek 1366*55138Storek /* 1367*55138Storek * Target or unit decided to let go of the bus early. 1368*55138Storek */ 1369*55138Storek void 1370*55138Storek esprel(self) 1371*55138Storek struct device *self; 1372*55138Storek { 1373*55138Storek register struct esp_softc *sc = (struct esp_softc *)self; 1374*55138Storek register struct sq *sq; 1375*55138Storek 1376*55138Storek /* if there is someone else waiting, give them a crack at it */ 1377*55138Storek if ((sq = sc->sc_hba.hba_head) != NULL) 1378*55138Storek (*sq->sq_dgo)(sq->sq_dev, &sc->sc_cdb); 1379*55138Storek else 1380*55138Storek sc->sc_hba.hba_busy = 0; 1381*55138Storek } 1382