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