xref: /netbsd-src/sys/arch/acorn32/podulebus/esc.c (revision 5e4c038a45edbc7d63b7c2daa76e29f88b64a4e3)
1 /*	$NetBSD: esc.c,v 1.7 2002/04/05 16:58:02 thorpej Exp $	*/
2 
3 /*
4  * Copyright (c) 1995 Scott Stevens
5  * Copyright (c) 1995 Daniel Widenfalk
6  * Copyright (c) 1994 Christian E. Hopps
7  * Copyright (c) 1990 The Regents of the University of California.
8  * All rights reserved.
9  *
10  * This code is derived from software contributed to Berkeley by
11  * Van Jacobson of Lawrence Berkeley Laboratory.
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
16  * 1. Redistributions of source code must retain the above copyright
17  *    notice, this list of conditions and the following disclaimer.
18  * 2. Redistributions in binary form must reproduce the above copyright
19  *    notice, this list of conditions and the following disclaimer in the
20  *    documentation and/or other materials provided with the distribution.
21  * 3. All advertising materials mentioning features or use of this software
22  *    must display the following acknowledgement:
23  *	This product includes software developed by the University of
24  *	California, Berkeley and its contributors.
25  * 4. Neither the name of the University nor the names of its contributors
26  *    may be used to endorse or promote products derived from this software
27  *    without specific prior written permission.
28  *
29  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
30  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
31  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
32  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
33  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
34  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
35  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
37  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
38  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39  * SUCH DAMAGE.
40  *
41  *	@(#)scsi.c	7.5 (Berkeley) 5/4/91
42  */
43 
44 /*
45  * AMD AM53CF94 scsi adaptor driver
46  *
47  * Functionally compatible with the FAS216
48  *
49  * Apart from a very small patch to set up control register 4
50  */
51 
52 /*
53  * Modified for NetBSD/arm32 by Scott Stevens
54  */
55 
56 #include <sys/param.h>
57 
58 __RCSID("$NetBSD: esc.c,v 1.7 2002/04/05 16:58:02 thorpej Exp $");
59 
60 #include <sys/systm.h>
61 #include <sys/device.h>
62 #include <sys/buf.h>
63 #include <sys/proc.h>
64 #include <dev/scsipi/scsi_all.h>
65 #include <dev/scsipi/scsipi_all.h>
66 #include <dev/scsipi/scsiconf.h>
67 
68 #include <uvm/uvm_extern.h>
69 
70 #include <machine/pmap.h>
71 #include <machine/cpu.h>
72 #include <machine/io.h>
73 #include <machine/intr.h>
74 #include <arm/arm32/katelib.h>
75 #include <acorn32/podulebus/podulebus.h>
76 #include <acorn32/podulebus/escreg.h>
77 #include <acorn32/podulebus/escvar.h>
78 
79 void escinitialize __P((struct esc_softc *));
80 void esc_minphys   __P((struct buf *bp));
81 void esc_scsi_request __P((struct scsipi_channel *,
82 				scsipi_adapter_req_t, void *));
83 void esc_donextcmd __P((struct esc_softc *dev, struct esc_pending *pendp));
84 void esc_scsidone  __P((struct esc_softc *dev, struct scsipi_xfer *xs,
85 			 int stat));
86 void escintr	    __P((struct esc_softc *dev));
87 void esciwait	    __P((struct esc_softc *dev));
88 void escreset	    __P((struct esc_softc *dev, int how));
89 int  escselect	    __P((struct esc_softc *dev, struct esc_pending *pendp,
90 			 unsigned char *cbuf, int clen,
91 			 unsigned char *buf, int len, int mode));
92 void escicmd	    __P((struct esc_softc *dev, struct esc_pending *pendp));
93 int escgo         __P((struct esc_softc *dev, struct esc_pending *pendp));
94 
95 void esc_init_nexus(struct esc_softc *, struct nexus *);
96 void esc_save_pointers(struct esc_softc *);
97 void esc_restore_pointers(struct esc_softc *);
98 void esc_ixfer(struct esc_softc *);
99 void esc_build_sdtrm(struct esc_softc *, int, int);
100 int esc_select_unit(struct esc_softc *,	short);
101 struct nexus *esc_arbitate_target(struct esc_softc *, int);
102 void esc_setup_nexus(struct esc_softc *, struct nexus *, struct esc_pending *,
103     unsigned char *, int, unsigned char *, int, int);
104 int esc_pretests(struct esc_softc *, esc_regmap_p);
105 int esc_midaction(struct esc_softc *, esc_regmap_p, struct nexus *);
106 int esc_postaction(struct esc_softc *, esc_regmap_p, struct nexus *);
107 
108 
109 /*
110  * Initialize these to make 'em patchable. Defaults to enable sync and discon.
111  */
112 u_char	esc_inhibit_sync[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
113 u_char	esc_inhibit_disc[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
114 
115 #define DEBUG
116 #ifdef DEBUG
117 #define QPRINTF(a) if (esc_debug > 1) printf a
118 int	esc_debug = 2;
119 #else
120 #define QPRINTF
121 #endif
122 
123 /*
124  * default minphys routine for esc based controllers
125  */
126 void
127 esc_minphys(bp)
128 	struct buf *bp;
129 {
130 
131 	/*
132 	 * No max transfer at this level.
133 	 */
134 	minphys(bp);
135 }
136 
137 /*
138  * Initialize the nexus structs.
139  */
140 void
141 esc_init_nexus(dev, nexus)
142 	struct esc_softc *dev;
143 	struct nexus	  *nexus;
144 {
145 	bzero(nexus, sizeof(struct nexus));
146 
147 	nexus->state	= ESC_NS_IDLE;
148 	nexus->period	= 200;
149 	nexus->offset	= 0;
150 	nexus->syncper	= 5;
151 	nexus->syncoff	= 0;
152 	nexus->config3	= dev->sc_config3 & ~ESC_CFG3_FASTSCSI;
153 }
154 
155 void
156 escinitialize(dev)
157 	struct esc_softc *dev;
158 {
159 	u_int		*pte;
160 	int		 i;
161 
162 	dev->sc_led_status = 0;
163 
164 	TAILQ_INIT(&dev->sc_xs_pending);
165 	TAILQ_INIT(&dev->sc_xs_free);
166 
167 /*
168  * Initialize the esc_pending structs and link them into the free list. We
169  * have to set vm_link_data.pages to 0 or the vm FIX won't work.
170  */
171 	for(i=0; i<MAXPENDING; i++) {
172 		TAILQ_INSERT_TAIL(&dev->sc_xs_free, &dev->sc_xs_store[i],
173 				  link);
174 	}
175 
176 /*
177  * Calculate the correct clock conversion factor 2 <= factor <= 8, i.e. set
178  * the factor to clock_freq / 5 (int).
179  */
180 	if (dev->sc_clock_freq <= 10)
181 		dev->sc_clock_conv_fact = 2;
182 	if (dev->sc_clock_freq <= 40)
183 		dev->sc_clock_conv_fact = 2+((dev->sc_clock_freq-10)/5);
184 	else
185 		panic("escinitialize: Clock frequence too high");
186 
187 /* Setup and save the basic configuration registers */
188 	dev->sc_config1 = (dev->sc_host_id & ESC_CFG1_BUS_ID_MASK);
189 	dev->sc_config2 = ESC_CFG2_FEATURES_ENABLE;
190 	dev->sc_config3 = (dev->sc_clock_freq > 25 ? ESC_CFG3_FASTCLK : 0);
191 
192 /* Precalculate timeout value and clock period. */
193 /* Ekkk ... floating point in the kernel !!!! */
194 /*	dev->sc_timeout_val  = 1+dev->sc_timeout*dev->sc_clock_freq/
195 				 (7.682*dev->sc_clock_conv_fact);*/
196 	dev->sc_timeout_val  = 1+dev->sc_timeout*dev->sc_clock_freq/
197 				 ((7682*dev->sc_clock_conv_fact)/1000);
198 	dev->sc_clock_period = 1000/dev->sc_clock_freq;
199 
200 	escreset(dev, 1 | 2);	/* Reset Chip and Bus */
201 
202 	dev->sc_units_disconnected = 0;
203 	dev->sc_msg_in_len = 0;
204 	dev->sc_msg_out_len = 0;
205 
206 	dev->sc_flags = 0;
207 
208 	for(i=0; i<8; i++)
209 		esc_init_nexus(dev, &dev->sc_nexus[i]);
210 
211 /*
212  * Setup bump buffer.
213  */
214 	dev->sc_bump_va = (u_char *)uvm_km_zalloc(kernel_map, dev->sc_bump_sz);
215 	(void) pmap_extract(pmap_kernel(), (vaddr_t)dev->sc_bump_va,
216 	    (paddr_t *)&dev->sc_bump_pa);
217 
218 /*
219  * Setup pages to noncachable, that way we don't have to flush the cache
220  * every time we need "bumped" transfer.
221  */
222 	pte = vtopte((vaddr_t) dev->sc_bump_va);
223 	*pte &= ~L2_C;
224 	cpu_tlb_flushD();
225 	cpu_dcache_wbinv_range((vm_offset_t)dev->sc_bump_va, NBPG);
226 
227 	printf(" dmabuf V0x%08x P0x%08x", (u_int)dev->sc_bump_va, (u_int)dev->sc_bump_pa);
228 }
229 
230 
231 /*
232  * used by specific esc controller
233  */
234 void
235 esc_scsi_request(struct scsipi_channel *chan, scsipi_adapter_req_t req,
236  								void *arg)
237 {
238 	struct scsipi_xfer *xs;
239 	struct esc_softc	*dev = (void *)chan->chan_adapter->adapt_dev;
240 	struct scsipi_periph	*periph;
241 	struct esc_pending	*pendp;
242 	int			 flags, s, target;
243 
244 	switch (req) {
245 	case ADAPTER_REQ_RUN_XFER:
246 		xs = arg;
247 		periph = xs->xs_periph;
248 		flags = xs->xs_control;
249 		target = periph->periph_target;
250 
251 		if (flags & XS_CTL_DATA_UIO)
252 			panic("esc: scsi data uio requested");
253 
254 		if ((flags & XS_CTL_POLL) && (dev->sc_flags & ESC_ACTIVE))
255 			panic("esc_scsicmd: busy");
256 
257 /* Get hold of a esc_pending block. */
258 		s = splbio();
259 		pendp = dev->sc_xs_free.tqh_first;
260 		if (pendp == NULL) {
261 			splx(s);
262 			xs->error = XS_RESOURCE_SHORTAGE;
263 			scsipi_done(xs);
264 			return;
265 		}
266 		TAILQ_REMOVE(&dev->sc_xs_free, pendp, link);
267 		pendp->xs = xs;
268 		splx(s);
269 
270 
271 /* If the chip if busy OR the unit is busy, we have to wait for out turn. */
272 		if ((dev->sc_flags & ESC_ACTIVE) ||
273 		    (dev->sc_nexus[target].flags & ESC_NF_UNIT_BUSY)) {
274 			s = splbio();
275 			TAILQ_INSERT_TAIL(&dev->sc_xs_pending, pendp, link);
276 			splx(s);
277 		} else
278 			esc_donextcmd(dev, pendp);
279 
280 		return;
281 	case ADAPTER_REQ_GROW_RESOURCES:
282 	case ADAPTER_REQ_SET_XFER_MODE:
283 		/* XXX Not supported. */
284 		return;
285 	}
286 
287 }
288 
289 /*
290  * Actually select the unit, whereby the whole scsi-process is started.
291  */
292 void
293 esc_donextcmd(dev, pendp)
294 	struct esc_softc	*dev;
295 	struct esc_pending	*pendp;
296 {
297 	int	s;
298 
299 /*
300  * Special case for scsi unit reset. I think this is waterproof. We first
301  * select the unit during splbio. We then cycle through the generated
302  * interrupts until the interrupt routine signals that the unit has
303  * acknowledged the reset. After that we have to wait a reset to select
304  * delay before anything else can happend.
305  */
306 	if (pendp->xs->xs_control & XS_CTL_RESET) {
307 		struct nexus	*nexus;
308 
309 		s = splbio();
310 		while(!escselect(dev, pendp, 0, 0, 0, 0, ESC_SELECT_K)) {
311 			splx(s);
312 			delay(10);
313 			s = splbio();
314 		}
315 
316 		nexus = dev->sc_cur_nexus;
317 		while(nexus->flags & ESC_NF_UNIT_BUSY) {
318 			esciwait(dev);
319 			escintr(dev);
320 		}
321 
322 		nexus->flags |= ESC_NF_UNIT_BUSY;
323 		splx(s);
324 
325 		escreset(dev, 0);
326 
327 		s = splbio();
328 		nexus->flags &= ~ESC_NF_UNIT_BUSY;
329 		splx(s);
330 	}
331 
332 /*
333  * If we are polling, go to splbio and perform the command, else we poke
334  * the scsi-bus via escgo to get the interrupt machine going.
335  */
336 	if (pendp->xs->xs_control & XS_CTL_POLL) {
337 		s = splbio();
338 		escicmd(dev, pendp);
339 		TAILQ_INSERT_TAIL(&dev->sc_xs_free, pendp, link);
340 		splx(s);
341 	} else {
342 		escgo(dev, pendp);
343 		return;
344 	}
345 }
346 
347 void
348 esc_scsidone(dev, xs, stat)
349 	struct esc_softc *dev;
350 	struct scsipi_xfer *xs;
351 	int		 stat;
352 {
353 	struct esc_pending	*pendp;
354 	int			 s;
355 
356 	xs->status = stat;
357 
358 	if (stat == 0)
359 		xs->resid = 0;
360 	else {
361 		switch(stat) {
362 		case SCSI_CHECK:
363 		case SCSI_BUSY:
364 			xs->error = XS_BUSY;
365 			break;
366 		case -1:
367 			xs->error = XS_DRIVER_STUFFUP;
368 			QPRINTF(("esc_scsicmd() bad %x\n", stat));
369 			break;
370 		default:
371 			xs->error = XS_TIMEOUT;
372 			break;
373 		}
374 	}
375 
376 /* Steal the next command from the queue so that one unit can't hog the bus. */
377 	s = splbio();
378 	pendp = dev->sc_xs_pending.tqh_first;
379 	while(pendp) {
380 		if (!(dev->sc_nexus[pendp->xs->xs_periph->periph_target].flags &
381 		      ESC_NF_UNIT_BUSY))
382 			break;
383 		pendp = pendp->link.tqe_next;
384 	}
385 
386 	if (pendp != NULL) {
387 		TAILQ_REMOVE(&dev->sc_xs_pending, pendp, link);
388 	}
389 
390 	splx(s);
391 	scsipi_done(xs);
392 
393 	if (pendp)
394 		esc_donextcmd(dev, pendp);
395 }
396 
397 /*
398  * There are two kinds of reset:
399  *  1) CHIP-bus reset. This also implies a SCSI-bus reset.
400  *  2) SCSI-bus reset.
401  * After the appropriate resets have been performed we wait a reset to select
402  * delay time.
403  */
404 void
405 escreset(dev, how)
406 	struct esc_softc *dev;
407 	int		 how;
408 {
409 	esc_regmap_p	rp;
410 	int		i, s;
411 
412 	rp = dev->sc_esc;
413 
414 	if (how & 1) {
415 		for(i=0; i<8; i++)
416 			esc_init_nexus(dev, &dev->sc_nexus[i]);
417 
418 		*rp->esc_command = ESC_CMD_RESET_CHIP;
419 		delay(1);
420 		*rp->esc_command = ESC_CMD_NOP;
421 
422 		*rp->esc_config1 = dev->sc_config1;
423 		*rp->esc_config2 = dev->sc_config2;
424 		*rp->esc_config3 = dev->sc_config3;
425 		*rp->esc_config4 = dev->sc_config4;
426 		*rp->esc_timeout = dev->sc_timeout_val;
427 		*rp->esc_clkconv = dev->sc_clock_conv_fact &
428 					ESC_CLOCK_CONVERSION_MASK;
429 	}
430 
431 	if (how & 2) {
432 		for(i=0; i<8; i++)
433 			esc_init_nexus(dev, &dev->sc_nexus[i]);
434 
435 		s = splbio();
436 
437 		*rp->esc_command = ESC_CMD_RESET_SCSI_BUS;
438 		delay(100);
439 
440 /* Skip interrupt generated by RESET_SCSI_BUS */
441 		while(*rp->esc_status & ESC_STAT_INTERRUPT_PENDING) {
442 			dev->sc_status = *rp->esc_status;
443 			dev->sc_interrupt = *rp->esc_interrupt;
444 
445 			delay(100);
446 		}
447 
448 		dev->sc_status = *rp->esc_status;
449 		dev->sc_interrupt = *rp->esc_interrupt;
450 
451 		splx(s);
452 	}
453 
454 	if (dev->sc_config_flags & ESC_SLOW_START)
455 		delay(4*250000); /* RESET to SELECT DELAY*4 for slow devices */
456 	else
457 		delay(250000);	 /* RESET to SELECT DELAY */
458 }
459 
460 /*
461  * Save active data pointers to the nexus block currently active.
462  */
463 void
464 esc_save_pointers(dev)
465 	struct esc_softc *dev;
466 {
467 	struct nexus	*nx;
468 
469 	nx = dev->sc_cur_nexus;
470 	if (nx) {
471 		nx->cur_link	= dev->sc_cur_link;
472 		nx->max_link	= dev->sc_max_link;
473 		nx->buf		= dev->sc_buf;
474 		nx->len		= dev->sc_len;
475 		nx->dma_len	= dev->sc_dma_len;
476 		nx->dma_buf	= dev->sc_dma_buf;
477 		nx->dma_blk_flg	= dev->sc_dma_blk_flg;
478 		nx->dma_blk_len	= dev->sc_dma_blk_len;
479 		nx->dma_blk_ptr	= dev->sc_dma_blk_ptr;
480 	}
481 }
482 
483 /*
484  * Restore data pointers from the currently active nexus block.
485  */
486 void
487 esc_restore_pointers(dev)
488 	struct esc_softc *dev;
489 {
490 	struct nexus	*nx;
491 
492 	nx = dev->sc_cur_nexus;
493 	if (nx) {
494 		dev->sc_cur_link    = nx->cur_link;
495 		dev->sc_max_link    = nx->max_link;
496 		dev->sc_buf	    = nx->buf;
497 		dev->sc_len	    = nx->len;
498 		dev->sc_dma_len	    = nx->dma_len;
499 		dev->sc_dma_buf	    = nx->dma_buf;
500 		dev->sc_dma_blk_flg = nx->dma_blk_flg;
501 		dev->sc_dma_blk_len = nx->dma_blk_len;
502 		dev->sc_dma_blk_ptr = nx->dma_blk_ptr;
503 		dev->sc_chain	    = nx->dma;
504 		dev->sc_unit	    = (nx->lun_unit & 0x0F);
505 		dev->sc_lun	    = (nx->lun_unit & 0xF0) >> 4;
506 	}
507 }
508 
509 /*
510  * esciwait is used during interrupt and polled IO to wait for an event from
511  * the FAS chip. This function MUST NOT BE CALLED without interrupt disabled.
512  */
513 void
514 esciwait(dev)
515 	struct esc_softc *dev;
516 {
517 	esc_regmap_p	rp;
518 
519 /*
520  * If ESC_DONT_WAIT is set, we have already grabbed the interrupt info
521  * elsewhere. So we don't have to wait for it.
522  */
523 	if (dev->sc_flags & ESC_DONT_WAIT) {
524 		dev->sc_flags &= ~ESC_DONT_WAIT;
525 		return;
526 	}
527 
528 	rp = dev->sc_esc;
529 
530 /* Wait for FAS chip to signal an interrupt. */
531 	while(!(*rp->esc_status & ESC_STAT_INTERRUPT_PENDING));
532 /*		delay(1);*/
533 
534 /* Grab interrupt info from chip. */
535 	dev->sc_status = *rp->esc_status;
536 	dev->sc_interrupt = *rp->esc_interrupt;
537 	if (dev->sc_interrupt & ESC_INT_RESELECTED) {
538 		dev->sc_resel[0] = *rp->esc_fifo;
539 		dev->sc_resel[1] = *rp->esc_fifo;
540 	}
541 }
542 
543 #if 0
544 /*
545  * Transfer info to/from device. esc_ixfer uses polled IO+esciwait so the
546  * rules that apply to esciwait also applies here.
547  */
548 void
549 esc_ixfer(dev)
550 	struct esc_softc *dev;
551 {
552 	esc_regmap_p	 rp;
553 	u_char		*buf;
554 	int		 len, mode, phase;
555 
556 	rp = dev->sc_esc;
557 	buf = dev->sc_buf;
558 	len = dev->sc_len;
559 
560 /*
561  * Decode the scsi phase to determine whether we are reading or writing.
562  * mode == 1 => READ, mode == 0 => WRITE
563  */
564 	phase = dev->sc_status & ESC_STAT_PHASE_MASK;
565 	mode = (phase == ESC_PHASE_DATA_IN);
566 
567 	while(len && ((dev->sc_status & ESC_STAT_PHASE_MASK) == phase))
568 		if (mode) {
569 			*rp->esc_command = ESC_CMD_TRANSFER_INFO;
570 
571 			esciwait(dev);
572 
573 			*buf++ = *rp->esc_fifo;
574 			len--;
575 		} else {
576 			len--;
577 			*rp->esc_fifo = *buf++;
578 			*rp->esc_command = ESC_CMD_TRANSFER_INFO;
579 
580 			esciwait(dev);
581 		}
582 
583 /* Update buffer pointers to reflect the sent/received data. */
584 	dev->sc_buf = buf;
585 	dev->sc_len = len;
586 
587 /*
588  * Since the last esciwait will be a phase-change, we can't wait for it
589  * again later, so we have to signal that.
590  */
591 
592 	dev->sc_flags |= ESC_DONT_WAIT;
593 }
594 #else
595 /*
596  * Transfer info to/from device. esc_ixfer uses polled IO+esciwait so the
597  * rules that apply to esciwait also applies here.
598  */
599 void
600 esc_ixfer(dev)
601 	struct esc_softc *dev;
602 {
603 	esc_regmap_p	 rp;
604 	vu_char		*esc_status;
605 	vu_char		*esc_command;
606 	vu_char		*esc_interrupt;
607 	vu_char		*esc_fifo;
608 	u_char		*buf;
609 	int		 len, mode, phase;
610 
611 	rp = dev->sc_esc;
612 	buf = dev->sc_buf;
613 	len = dev->sc_len;
614 
615 	/* Use discrete variables for better optimisation */
616 
617 	esc_status = rp->esc_status;
618 	esc_command = rp->esc_command;
619 	esc_interrupt = rp->esc_interrupt;
620 	esc_fifo = rp->esc_fifo;
621 
622 /*
623  * Decode the scsi phase to determine whether we are reading or writing.
624  * mode == 1 => READ, mode == 0 => WRITE
625  */
626 	phase = dev->sc_status & ESC_STAT_PHASE_MASK;
627 	mode = (phase == ESC_PHASE_DATA_IN);
628 
629 	if (mode) {
630 		while(len && ((dev->sc_status & ESC_STAT_PHASE_MASK) == phase)) {
631 			*esc_command = ESC_CMD_TRANSFER_INFO;
632 
633 			/* Wait for FAS chip to signal an interrupt. */
634 			while(!(*esc_status & ESC_STAT_INTERRUPT_PENDING));
635 /*				delay(1);*/
636 
637 			/* Grab interrupt info from chip. */
638 			dev->sc_status = *esc_status;
639 			dev->sc_interrupt = *esc_interrupt;
640 
641 			*buf++ = *esc_fifo;
642 			len--;
643 		}
644 	} else {
645 		while(len && ((dev->sc_status & ESC_STAT_PHASE_MASK) == phase)) {
646 			len--;
647 			*esc_fifo = *buf++;
648 			*esc_command = ESC_CMD_TRANSFER_INFO;
649 
650 			/* Wait for FAS chip to signal an interrupt. */
651 			while(!(*esc_status & ESC_STAT_INTERRUPT_PENDING));
652 /*				delay(1);*/
653 
654 			/* Grab interrupt info from chip. */
655 			dev->sc_status = *esc_status;
656 			dev->sc_interrupt = *esc_interrupt;
657 		}
658 	}
659 
660 /* Update buffer pointers to reflect the sent/received data. */
661 	dev->sc_buf = buf;
662 	dev->sc_len = len;
663 
664 /*
665  * Since the last esciwait will be a phase-change, we can't wait for it
666  * again later, so we have to signal that.
667  */
668 
669 	dev->sc_flags |= ESC_DONT_WAIT;
670 }
671 #endif
672 
673 /*
674  * Build a Synchronous Data Transfer Request message
675  */
676 void
677 esc_build_sdtrm(dev, period, offset)
678 	struct esc_softc *dev;
679 	int		  period;
680 	int		  offset;
681 {
682 	dev->sc_msg_out[0] = 0x01;
683 	dev->sc_msg_out[1] = 0x03;
684 	dev->sc_msg_out[2] = 0x01;
685 	dev->sc_msg_out[3] = period/4;
686 	dev->sc_msg_out[4] = offset;
687 	dev->sc_msg_out_len= 5;
688 }
689 
690 /*
691  * Arbitate the scsi bus and select the unit
692  */
693 int
694 esc_select_unit(dev, target)
695 	struct esc_softc *dev;
696 	short		  target;
697 {
698 	esc_regmap_p	 rp;
699 	struct nexus	*nexus;
700 	int		 s, retcode, i;
701 	u_char		 cmd;
702 
703 	s = splbio();	/* Do this at splbio so that we won't be disturbed. */
704 
705 	retcode = 0;
706 
707 	nexus = &dev->sc_nexus[target];
708 
709 /*
710  * Check if the chip is busy. If not the we mark it as so and hope that nobody
711  * reselects us until we have grabbed the bus.
712  */
713 	if (!(dev->sc_flags & ESC_ACTIVE) && !dev->sc_sel_nexus) {
714 		dev->sc_flags |= ESC_ACTIVE;
715 
716 		rp = dev->sc_esc;
717 
718 		*rp->esc_syncper = nexus->syncper;
719 		*rp->esc_syncoff = nexus->syncoff;
720 		*rp->esc_config3 = nexus->config3;
721 
722 		*rp->esc_config1 = dev->sc_config1;
723 		*rp->esc_timeout = dev->sc_timeout_val;
724 		*rp->esc_dest_id = target;
725 
726 /* If nobody has stolen the bus, we can send a select command to the chip. */
727 		if (!(*rp->esc_status & ESC_STAT_INTERRUPT_PENDING)) {
728 			*rp->esc_fifo = nexus->ID;
729 			if ((nexus->flags & (ESC_NF_DO_SDTR | ESC_NF_RESET))
730 			    || (dev->sc_msg_out_len != 0))
731 				cmd = ESC_CMD_SEL_ATN_STOP;
732 			else {
733 				for(i=0; i<nexus->clen; i++)
734 					*rp->esc_fifo = nexus->cbuf[i];
735 
736 				cmd = ESC_CMD_SEL_ATN;
737 			}
738 
739 			dev->sc_sel_nexus = nexus;
740 
741 			*rp->esc_command = cmd;
742 			retcode = 1;
743 			nexus->flags &= ~ESC_NF_RETRY_SELECT;
744 		} else
745 			nexus->flags |= ESC_NF_RETRY_SELECT;
746 	} else
747 		nexus->flags |= ESC_NF_RETRY_SELECT;
748 
749 	splx(s);
750 	return(retcode);
751 }
752 
753 /*
754  * Grab the nexus if available else return 0.
755  */
756 struct nexus *
757 esc_arbitate_target(dev, target)
758 	struct esc_softc *dev;
759 	int		  target;
760 {
761 	struct nexus	*nexus;
762 	int		 s;
763 
764 /*
765  * This is realy simple. Raise interrupt level to splbio. Grab the nexus and
766  * leave.
767  */
768 	nexus = &dev->sc_nexus[target];
769 
770 	s = splbio();
771 
772 	if (nexus->flags & ESC_NF_UNIT_BUSY)
773 		nexus = 0;
774 	else
775 		nexus->flags |= ESC_NF_UNIT_BUSY;
776 
777 	splx(s);
778 	return(nexus);
779 }
780 
781 /*
782  * Setup a nexus for use. Initializes command, buffer pointers and dma chain.
783  */
784 void
785 esc_setup_nexus(dev, nexus, pendp, cbuf, clen, buf, len, mode)
786 	struct esc_softc	*dev;
787 	struct nexus		*nexus;
788 	struct esc_pending	*pendp;
789 	unsigned char		*cbuf;
790 	int			 clen;
791 	unsigned char		*buf;
792 	int			 len;
793 	int			 mode;
794 {
795 	int	sync, target, lun;
796 
797 	target = pendp->xs->xs_periph->periph_target;
798 	lun    = pendp->xs->xs_periph->periph_lun;
799 
800 /*
801  * Adopt mode to reflect the config flags.
802  * If we can't use DMA we can't use synch transfer. Also check the
803  * esc_inhibit_xxx[target] flags.
804  */
805 	if ((dev->sc_config_flags & (ESC_NO_SYNCH | ESC_NO_DMA)) ||
806 	    esc_inhibit_sync[(int)target])
807 		mode &= ~ESC_SELECT_S;
808 
809 	if ((dev->sc_config_flags & ESC_NO_RESELECT) ||
810 	    esc_inhibit_disc[(int)target])
811 		mode &= ~ESC_SELECT_R;
812 
813 	nexus->xs		= pendp->xs;
814 
815 /* Setup the nexus struct. */
816 	nexus->ID	   = ((mode & ESC_SELECT_R) ? 0xC0 : 0x80) | lun;
817 	nexus->clen	   = clen;
818 	bcopy(cbuf, nexus->cbuf, nexus->clen);
819 	nexus->cbuf[1] |= lun << 5;		/* Fix the lun bits */
820 	nexus->cur_link	   = 0;
821 	nexus->dma_len	   = 0;
822 	nexus->dma_buf	   = 0;
823 	nexus->dma_blk_len = 0;
824 	nexus->dma_blk_ptr = 0;
825 	nexus->len	   = len;
826 	nexus->buf	   = buf;
827 	nexus->lun_unit	   = (lun << 4) | target;
828 	nexus->state	   = ESC_NS_SELECTED;
829 
830 /* We must keep these flags. All else must be zero. */
831 	nexus->flags	  &= ESC_NF_UNIT_BUSY
832 			   | ESC_NF_SYNC_TESTED | ESC_NF_SELECT_ME;
833 
834 	if (mode & ESC_SELECT_I)
835 		nexus->flags |= ESC_NF_IMMEDIATE;
836 	if (mode & ESC_SELECT_K)
837 		nexus->flags |= ESC_NF_RESET;
838 
839 	sync  = ((mode & ESC_SELECT_S) ? 1 : 0);
840 
841 /* We can't use sync during polled IO. */
842 	if (sync && (mode & ESC_SELECT_I))
843 		sync = 0;
844 
845 	if (!sync &&
846 	    ((nexus->flags & ESC_NF_SYNC_TESTED) && (nexus->offset != 0))) {
847 		/*
848 		 * If the scsi unit is set to synch transfer and we don't want
849 		 * that, we have to renegotiate.
850 		 */
851 
852 		nexus->flags |= ESC_NF_DO_SDTR;
853 		nexus->period = 200;
854 		nexus->offset = 0;
855 	} else if (sync && !(nexus->flags & ESC_NF_SYNC_TESTED)) {
856 		/*
857 		 * If the scsi unit is not set to synch transfer and we want
858 		 * that, we have to negotiate. This should realy base the
859 		 * period on the clock frequence rather than just check if
860 		 * >25Mhz
861 		 */
862 
863 		nexus->flags |= ESC_NF_DO_SDTR;
864 		nexus->period = ((dev->sc_clock_freq>25) ? 100 : 200);
865 		nexus->offset = 8;
866 
867 		/* If the user has a long cable, we want to limit the period */
868 		if ((nexus->period == 100) &&
869 		    (dev->sc_config_flags & ESC_SLOW_CABLE))
870 			nexus->period = 200;
871 	}
872 
873 /*
874  * Fake a dma-block for polled IO. This way we can use the same code to handle
875  * reselection. Much nicer this way.
876  */
877 	if ((mode & ESC_SELECT_I) || (dev->sc_config_flags & ESC_NO_DMA)) {
878 		nexus->dma[0].ptr = (vm_offset_t)buf;
879 		nexus->dma[0].len = len;
880 		nexus->dma[0].flg = ESC_CHAIN_PRG;
881 		nexus->max_link   = 1;
882 	} else {
883 		nexus->max_link = dev->sc_build_dma_chain(dev, nexus->dma,
884 							  buf, len);
885 	}
886 
887 /* Flush the caches. */
888 
889 	if (len && !(mode & ESC_SELECT_I))
890 		cpu_dcache_wbinv_range((vm_offset_t)buf, len);
891 }
892 
893 int
894 escselect(dev, pendp, cbuf, clen, buf, len, mode)
895 	struct esc_softc	*dev;
896 	struct esc_pending	*pendp;
897 	unsigned char		*cbuf;
898 	int			 clen;
899 	unsigned char		*buf;
900 	int			 len;
901 	int			 mode;
902 {
903 	struct nexus	*nexus;
904 
905 /* Get the nexus struct. */
906 	nexus = esc_arbitate_target(dev, pendp->xs->xs_periph->periph_target);
907 	if (nexus == NULL)
908 		return(0);
909 
910 /* Setup the nexus struct. */
911 	esc_setup_nexus(dev, nexus, pendp, cbuf, clen, buf, len, mode);
912 
913 /* Post it to the interrupt machine. */
914 	esc_select_unit(dev, pendp->xs->xs_periph->periph_target);
915 
916 	return(1);
917 }
918 
919 int
920 escgo(dev, pendp)
921 	struct esc_softc   *dev;
922 	struct esc_pending *pendp;
923 {
924 	int	 s;
925 	char	*buf;
926 
927 	buf    = pendp->xs->data;
928 
929 	if (escselect(dev, pendp, (char *)pendp->xs->cmd, pendp->xs->cmdlen,
930 		      buf, pendp->xs->datalen, ESC_SELECT_RS)) {
931 		/*
932 		 * We got the command going so the esc_pending struct is now
933 		 * free to reuse.
934 		 */
935 
936 		s = splbio();
937 		TAILQ_INSERT_TAIL(&dev->sc_xs_free, pendp, link);
938 		splx(s);
939 	} else {
940 		/*
941 		 * We couldn't make the command fly so we have to wait. The
942 		 * struct MUST be inserted at the head to keep the order of
943 		 * the commands.
944 		 */
945 
946 		s = splbio();
947 		TAILQ_INSERT_HEAD(&dev->sc_xs_pending, pendp, link);
948 		splx(s);
949 	}
950 
951 	return(0);
952 }
953 
954 /*
955  * Part one of the interrupt machine. Error checks and reselection test.
956  * We don't know if we have an active nexus here!
957  */
958 int
959 esc_pretests(dev, rp)
960 	struct esc_softc *dev;
961 	esc_regmap_p	  rp;
962 {
963 	struct nexus	*nexus;
964 	int		 i, s;
965 
966 	if (dev->sc_interrupt & ESC_INT_SCSI_RESET_DETECTED) {
967 		/*
968 		 * Cleanup and notify user. Lets hope that this is all we
969 		 * have to do
970 		 */
971 
972 		for(i=0; i<8; i++) {
973 			if (dev->sc_nexus[i].xs)
974 				esc_scsidone(dev, dev->sc_nexus[i].xs, -2);
975 
976 			esc_init_nexus(dev, &dev->sc_nexus[i]);
977 		}
978 		printf("escintr: SCSI-RESET detected!");
979 		return(-1);
980 	}
981 
982 	if (dev->sc_interrupt & ESC_INT_ILLEGAL_COMMAND) {
983 		/* Something went terrible wrong! Dump some data and panic! */
984 
985 		printf("FIFO:");
986 		while(*rp->esc_fifo_flags & ESC_FIFO_COUNT_MASK)
987 			printf(" %x", *rp->esc_fifo);
988 		printf("\n");
989 
990 		printf("CMD: %x\n", *rp->esc_command);
991 		panic("escintr: ILLEGAL COMMAND!");
992 	}
993 
994 	if (dev->sc_interrupt & ESC_INT_RESELECTED) {
995 		/* We were reselected. Set the chip as busy */
996 
997 		s = splbio();
998 		dev->sc_flags |= ESC_ACTIVE;
999 		if (dev->sc_sel_nexus) {
1000 			dev->sc_sel_nexus->flags |= ESC_NF_SELECT_ME;
1001 			dev->sc_sel_nexus = 0;
1002 		}
1003 		splx(s);
1004 
1005 		if (dev->sc_units_disconnected) {
1006 			/* Find out who reselected us. */
1007 
1008 			dev->sc_resel[0] &= ~(1<<dev->sc_host_id);
1009 
1010 			for(i=0; i<8; i++)
1011 				if (dev->sc_resel[0] & (1<<i))
1012 					break;
1013 
1014 			if (i == 8)
1015 				panic("Illegal reselection!");
1016 
1017 			if (dev->sc_nexus[i].state == ESC_NS_DISCONNECTED) {
1018 				/*
1019 				 * This unit had disconnected, so we reconnect
1020 				 * it.
1021 				 */
1022 
1023 				dev->sc_cur_nexus = &dev->sc_nexus[i];
1024 				nexus = dev->sc_cur_nexus;
1025 
1026 				*rp->esc_syncper = nexus->syncper;
1027 				*rp->esc_syncoff = nexus->syncoff;
1028 				*rp->esc_config3 = nexus->config3;
1029 
1030 				*rp->esc_dest_id = i & 7;
1031 
1032 				dev->sc_units_disconnected--;
1033 				dev->sc_msg_in_len= 0;
1034 
1035 				/* Restore active pointers. */
1036 				esc_restore_pointers(dev);
1037 
1038 				nexus->state = ESC_NS_RESELECTED;
1039 
1040 				*rp->esc_command = ESC_CMD_MESSAGE_ACCEPTED;
1041 
1042 				return(1);
1043 			}
1044 		}
1045 
1046 		/* Somehow we got an illegal reselection. Dump and panic. */
1047 		printf("escintr: resel[0] %x resel[1] %x disconnected %d\n",
1048 		       dev->sc_resel[0], dev->sc_resel[1],
1049 		       dev->sc_units_disconnected);
1050 		panic("escintr: Unexpected reselection!");
1051 	}
1052 
1053 	return(0);
1054 }
1055 
1056 /*
1057  * Part two of the interrupt machine. Handle disconnection and post command
1058  * processing. We know that we have an active nexus here.
1059  */
1060 int
1061 esc_midaction(dev, rp, nexus)
1062 	struct esc_softc *dev;
1063 	esc_regmap_p	  rp;
1064 	struct nexus	 *nexus;
1065 {
1066 	int	i, left, len, s;
1067 	u_char	status, msg;
1068 
1069 	if (dev->sc_interrupt & ESC_INT_DISCONNECT) {
1070 		s = splbio();
1071 		dev->sc_cur_nexus = 0;
1072 
1073 		/* Mark chip as busy and clean up the chip FIFO. */
1074 		dev->sc_flags &= ~ESC_ACTIVE;
1075 		*rp->esc_command = ESC_CMD_FLUSH_FIFO;
1076 
1077 		/* Let the nexus state reflect what we have to do. */
1078 		switch(nexus->state) {
1079 		case ESC_NS_SELECTED:
1080 			dev->sc_sel_nexus = 0;
1081 			nexus->flags &= ~ESC_NF_SELECT_ME;
1082 
1083 			/*
1084 			 * We were trying to select the unit. Probably no unit
1085 			 * at this ID.
1086 			 */
1087 			nexus->xs->resid = dev->sc_len;
1088 
1089 			nexus->status = -2;
1090 			nexus->flags &= ~ESC_NF_UNIT_BUSY;
1091 			nexus->state = ESC_NS_FINISHED;
1092 			break;
1093 
1094 		case ESC_NS_DONE:
1095 			/* All done. */
1096 			nexus->xs->resid = dev->sc_len;
1097 
1098 			nexus->flags &= ~ESC_NF_UNIT_BUSY;
1099 			nexus->state  = ESC_NS_FINISHED;
1100 			dev->sc_led(dev, 0);
1101 			break;
1102 
1103 		case ESC_NS_DISCONNECTING:
1104 			/*
1105 			 * We have received a DISCONNECT message, so we are
1106 			 * doing a normal disconnection.
1107 			 */
1108 			nexus->state = ESC_NS_DISCONNECTED;
1109 
1110 			dev->sc_units_disconnected++;
1111 			break;
1112 
1113 		case ESC_NS_RESET:
1114 			/*
1115 			 * We were reseting this SCSI-unit. Clean up the
1116 			 * nexus struct.
1117 			 */
1118 			dev->sc_led(dev, 0);
1119 			esc_init_nexus(dev, nexus);
1120 			break;
1121 
1122 		default:
1123 			/*
1124 			 * Unexpected disconnection! Cleanup and exit. This
1125 			 * shouldn't cause any problems.
1126 			 */
1127 			printf("escintr: Unexpected disconnection\n");
1128 			printf("escintr: u %x s %d p %d f %x c %x\n",
1129 			       nexus->lun_unit, nexus->state,
1130 			       dev->sc_status & ESC_STAT_PHASE_MASK,
1131 			       nexus->flags, nexus->cbuf[0]);
1132 
1133 			nexus->xs->resid = dev->sc_len;
1134 
1135 			nexus->flags &= ~ESC_NF_UNIT_BUSY;
1136 			nexus->state = ESC_NS_FINISHED;
1137 			nexus->status = -3;
1138 
1139 			dev->sc_led(dev, 0);
1140 			break;
1141 		}
1142 
1143 		/*
1144 		 * If we have disconnected units, we MUST enable reselection
1145 		 * within 250ms.
1146 		 */
1147 		if (dev->sc_units_disconnected &&
1148 		    !(dev->sc_flags & ESC_ACTIVE))
1149 			*rp->esc_command = ESC_CMD_ENABLE_RESEL;
1150 
1151 		splx(s);
1152 
1153 		/* Select the first pre-initialized nexus we find. */
1154 		for(i=0; i<8; i++)
1155 			if (dev->sc_nexus[i].flags & (ESC_NF_SELECT_ME | ESC_NF_RETRY_SELECT))
1156 				if (esc_select_unit(dev, i) == 2)
1157 					break;
1158 
1159 		/* We are done with this nexus! */
1160 		if (nexus->state == ESC_NS_FINISHED)
1161 			esc_scsidone(dev, nexus->xs, nexus->status);
1162 
1163 		return(1);
1164 	}
1165 
1166 	switch(nexus->state) {
1167 	case ESC_NS_SELECTED:
1168 		dev->sc_cur_nexus = nexus;
1169 		dev->sc_sel_nexus = 0;
1170 
1171 		nexus->flags &= ~ESC_NF_SELECT_ME;
1172 
1173 		/*
1174 		 * We have selected a unit. Setup chip, restore pointers and
1175 		 * light the led.
1176 		 */
1177 		*rp->esc_syncper = nexus->syncper;
1178 		*rp->esc_syncoff = nexus->syncoff;
1179 		*rp->esc_config3 = nexus->config3;
1180 
1181 		esc_restore_pointers(dev);
1182 
1183 		nexus->status	= 0xFF;
1184 		dev->sc_msg_in[0] = 0xFF;
1185 		dev->sc_msg_in_len= 0;
1186 
1187 		dev->sc_led(dev, 1);
1188 
1189 		break;
1190 
1191 	case ESC_NS_DATA_IN:
1192 	case ESC_NS_DATA_OUT:
1193 		/* We have transfered data. */
1194 		if (dev->sc_dma_len)
1195 			if (dev->sc_cur_link < dev->sc_max_link) {
1196 				/*
1197 				 * Clean up dma and at the same time get how
1198 				 * many bytes that were NOT transfered.
1199 				 */
1200 			  left = dev->sc_setup_dma(dev, 0, 0, ESC_DMA_CLEAR);
1201 			  len  = dev->sc_dma_len;
1202 
1203 			  if (nexus->state == ESC_NS_DATA_IN) {
1204 			    /*
1205 			     * If we were bumping we may have had an odd length
1206 			     * which means that there may be bytes left in the
1207 			     * fifo. We also need to move the data from the
1208 			     * bump buffer to the actual memory.
1209 			     */
1210 			    if (dev->sc_dma_buf == dev->sc_bump_pa)
1211 			    {
1212 			      while((*rp->esc_fifo_flags&ESC_FIFO_COUNT_MASK)
1213 				    && left)
1214 				dev->sc_bump_va[len-(left--)] = *rp->esc_fifo;
1215 
1216 			      bcopy(dev->sc_bump_va, dev->sc_buf, len-left);
1217 			    }
1218 			  } else {
1219 			    /* Count any unsent bytes and flush them. */
1220 			    left+= *rp->esc_fifo_flags & ESC_FIFO_COUNT_MASK;
1221 			    *rp->esc_command = ESC_CMD_FLUSH_FIFO;
1222 			  }
1223 
1224 			  /*
1225 			   * Update pointers/length to reflect the transfered
1226 			   * data.
1227 			   */
1228 			  dev->sc_len -= len-left;
1229 			  dev->sc_buf += len-left;
1230 
1231 			  dev->sc_dma_buf += len-left;
1232 			  dev->sc_dma_len  = left;
1233 
1234 			  dev->sc_dma_blk_ptr += len-left;
1235 			  dev->sc_dma_blk_len -= len-left;
1236 
1237 			  /*
1238 			   * If it was the end of a dma block, we select the
1239 			   * next to begin with.
1240 			   */
1241 			  if (!dev->sc_dma_blk_len)
1242 			    dev->sc_cur_link++;
1243 			}
1244 		break;
1245 
1246 	case ESC_NS_STATUS:
1247 		/*
1248 		 * If we were not sensing, grab the status byte. If we were
1249 		 * sensing and we got a bad status, let the user know.
1250 		 */
1251 
1252 		status = *rp->esc_fifo;
1253 		msg = *rp->esc_fifo;
1254 
1255 		nexus->status = status;
1256 		if (status != 0)
1257 			nexus->status = -1;
1258 
1259 		/*
1260 		 * Preload the command complete message. Handeled in
1261 		 * esc_postaction.
1262 		 */
1263 		dev->sc_msg_in[0] = msg;
1264 		dev->sc_msg_in_len = 1;
1265 		nexus->flags |= ESC_NF_HAS_MSG;
1266 		break;
1267 
1268 	default:
1269 		break;
1270 	}
1271 
1272 	return(0);
1273 }
1274 
1275 /*
1276  * Part three of the interrupt machine. Handle phase changes (and repeated
1277  * phase passes). We know that we have an active nexus here.
1278  */
1279 int
1280 esc_postaction(dev, rp, nexus)
1281 	struct esc_softc *dev;
1282 	esc_regmap_p	  rp;
1283 	struct nexus	 *nexus;
1284 {
1285 	int	i, len;
1286 	u_char	cmd;
1287 	short	offset, period;
1288 
1289 	cmd = 0;
1290 
1291 	switch(dev->sc_status & ESC_STAT_PHASE_MASK) {
1292 	case ESC_PHASE_DATA_OUT:
1293 	case ESC_PHASE_DATA_IN:
1294 		if ((dev->sc_status & ESC_STAT_PHASE_MASK) ==
1295 		    ESC_PHASE_DATA_OUT)
1296 			nexus->state = ESC_NS_DATA_OUT;
1297 		else
1298 			nexus->state = ESC_NS_DATA_IN;
1299 
1300 		/* Make DMA ready to accept new data. Load active pointers
1301 		 * from the DMA block. */
1302 		dev->sc_setup_dma(dev, 0, 0, ESC_DMA_CLEAR);
1303 		if (dev->sc_cur_link < dev->sc_max_link) {
1304 		  if (!dev->sc_dma_blk_len) {
1305 		    dev->sc_dma_blk_ptr = dev->sc_chain[dev->sc_cur_link].ptr;
1306 		    dev->sc_dma_blk_len = dev->sc_chain[dev->sc_cur_link].len;
1307 		    dev->sc_dma_blk_flg = dev->sc_chain[dev->sc_cur_link].flg;
1308 		  }
1309 
1310 		  /* We should use polled IO here. */
1311 		  if (dev->sc_dma_blk_flg == ESC_CHAIN_PRG) {
1312 			esc_ixfer(dev/*, nexus->xs->xs_control & XS_CTL_POLL*/);
1313 			dev->sc_cur_link++;
1314 			dev->sc_dma_len = 0;
1315 			break;
1316 		  }
1317 		  else if (dev->sc_dma_blk_flg == ESC_CHAIN_BUMP)
1318 			len = dev->sc_dma_blk_len;
1319 		  else
1320 			len = dev->sc_need_bump(dev,
1321 						(void *)dev->sc_dma_blk_ptr,
1322 						dev->sc_dma_blk_len);
1323 
1324 		  /*
1325 		   * If len != 0 we must bump the data, else we just DMA it
1326 		   * straight into memory.
1327 		   */
1328 		  if (len) {
1329 			dev->sc_dma_buf = dev->sc_bump_pa;
1330 			dev->sc_dma_len = len;
1331 
1332 			if (nexus->state == ESC_NS_DATA_OUT)
1333 			  bcopy(dev->sc_buf, dev->sc_bump_va, dev->sc_dma_len);
1334 		  } else {
1335 			dev->sc_dma_buf = dev->sc_dma_blk_ptr;
1336 			dev->sc_dma_len = dev->sc_dma_blk_len;
1337 		  }
1338 
1339 		  /* Load DMA with adress and length of transfer. */
1340 		  dev->sc_setup_dma(dev, (void *)dev->sc_dma_buf,
1341 		  		    dev->sc_dma_len,
1342 				    ((nexus->state == ESC_NS_DATA_OUT) ?
1343 				     ESC_DMA_WRITE : ESC_DMA_READ));
1344 
1345 		  printf("Using DMA !!!!\n");
1346 		  cmd = ESC_CMD_TRANSFER_INFO | ESC_CMD_DMA;
1347 		} else {
1348 			/*
1349 			 * Hmmm, the unit wants more info than we have or has
1350 			 * more than we want. Let the chip handle that.
1351 			 */
1352 
1353 			*rp->esc_tc_low = 0;
1354 			*rp->esc_tc_mid = 1;
1355 			*rp->esc_tc_high = 0;
1356 			cmd = ESC_CMD_TRANSFER_PAD;
1357 		}
1358 		break;
1359 
1360 	case ESC_PHASE_COMMAND:
1361 		/* The scsi unit wants the command, send it. */
1362 		nexus->state = ESC_NS_SVC;
1363 
1364 		*rp->esc_command = ESC_CMD_FLUSH_FIFO;
1365 		for(i=0; i<5; i++);
1366 
1367 		for(i=0; i<nexus->clen; i++)
1368 			*rp->esc_fifo = nexus->cbuf[i];
1369 		cmd = ESC_CMD_TRANSFER_INFO;
1370 		break;
1371 
1372 	case ESC_PHASE_STATUS:
1373 		/*
1374 		 * We've got status phase. Request status and command
1375 		 * complete message.
1376 		 */
1377 		nexus->state = ESC_NS_STATUS;
1378 		cmd = ESC_CMD_COMMAND_COMPLETE;
1379 		break;
1380 
1381 	case ESC_PHASE_MESSAGE_OUT:
1382 		/*
1383 		 * Either the scsi unit wants us to send a message or we have
1384 		 * asked for it by seting the ATN bit.
1385 		 */
1386 		nexus->state = ESC_NS_MSG_OUT;
1387 
1388 		*rp->esc_command = ESC_CMD_FLUSH_FIFO;
1389 
1390 		if (nexus->flags & ESC_NF_DO_SDTR) {
1391 			/* Send a Synchronous Data Transfer Request. */
1392 
1393 			esc_build_sdtrm(dev, nexus->period, nexus->offset);
1394 			nexus->flags |= ESC_NF_SDTR_SENT;
1395 			nexus->flags &= ~ESC_NF_DO_SDTR;
1396 		} else if (nexus->flags & ESC_NF_RESET) {
1397 			/* Send a reset scsi unit message. */
1398 
1399 			dev->sc_msg_out[0] = 0x0C;
1400 			dev->sc_msg_out_len = 1;
1401 			nexus->state = ESC_NS_RESET;
1402 			nexus->flags &= ~ESC_NF_RESET;
1403 		} else if (dev->sc_msg_out_len == 0) {
1404 			/* Don't know what to send so we send a NOP message. */
1405 
1406 			dev->sc_msg_out[0] = 0x08;
1407 			dev->sc_msg_out_len = 1;
1408 		}
1409 
1410 		cmd = ESC_CMD_TRANSFER_INFO;
1411 
1412 		for(i=0; i<dev->sc_msg_out_len; i++)
1413 			*rp->esc_fifo = dev->sc_msg_out[i];
1414 		dev->sc_msg_out_len = 0;
1415 
1416 		break;
1417 
1418 	case ESC_PHASE_MESSAGE_IN:
1419 		/* Receive a message from the scsi unit. */
1420 		nexus->state = ESC_NS_MSG_IN;
1421 
1422 		while(!(nexus->flags & ESC_NF_HAS_MSG)) {
1423 			*rp->esc_command = ESC_CMD_TRANSFER_INFO;
1424 			esciwait(dev);
1425 
1426 			dev->sc_msg_in[dev->sc_msg_in_len++] = *rp->esc_fifo;
1427 
1428 			/* Check if we got all the bytes in the message. */
1429 			if (dev->sc_msg_in[0] >= 0x80)       ;
1430 			else if (dev->sc_msg_in[0] >= 0x30)  ;
1431 			else if (((dev->sc_msg_in[0] >= 0x20) &&
1432 				  (dev->sc_msg_in_len == 2)) ||
1433 				 ((dev->sc_msg_in[0] != 0x01) &&
1434 				  (dev->sc_msg_in_len == 1))) {
1435 				nexus->flags |= ESC_NF_HAS_MSG;
1436 				break;
1437 			} else {
1438 			  if (dev->sc_msg_in_len >= 2)
1439 			    if ((dev->sc_msg_in[1]+2) == dev->sc_msg_in_len) {
1440 				nexus->flags |= ESC_NF_HAS_MSG;
1441 				break;
1442 			    }
1443 			}
1444 
1445 			*rp->esc_command = ESC_CMD_MESSAGE_ACCEPTED;
1446 			esciwait(dev);
1447 
1448 			if ((dev->sc_status & ESC_STAT_PHASE_MASK) !=
1449 			    ESC_PHASE_MESSAGE_IN)
1450 				break;
1451 		}
1452 
1453 		cmd = ESC_CMD_MESSAGE_ACCEPTED;
1454 		if (nexus->flags & ESC_NF_HAS_MSG) {
1455 			/* We have a message. Decode it. */
1456 
1457 			switch(dev->sc_msg_in[0]) {
1458 			case 0x00:	/* COMMAND COMPLETE */
1459 				nexus->state = ESC_NS_DONE;
1460 			case 0x04:	/* DISCONNECT */
1461 				nexus->state = ESC_NS_DISCONNECTING;
1462 				break;
1463 			case 0x02:	/* SAVE DATA POINTER */
1464 				esc_save_pointers(dev);
1465 				break;
1466 			case 0x03:	/* RESTORE DATA POINTERS */
1467 				esc_restore_pointers(dev);
1468 				break;
1469 			case 0x07:	/* MESSAGE REJECT */
1470 				/*
1471 				 * If we had sent a SDTR and we got a message
1472 				 * reject, the scsi docs say that we must go
1473 				 * to async transfer.
1474 				 */
1475 				if (nexus->flags & ESC_NF_SDTR_SENT) {
1476 					nexus->flags &= ~ESC_NF_SDTR_SENT;
1477 
1478 					nexus->config3 &= ~ESC_CFG3_FASTSCSI;
1479 					nexus->syncper = 5;
1480 					nexus->syncoff = 0;
1481 
1482 					*rp->esc_syncper = nexus->syncper;
1483 					*rp->esc_syncoff = nexus->syncoff;
1484 					*rp->esc_config3 = nexus->config3;
1485 				} else
1486 				/*
1487 				 * Something was rejected but we don't know
1488 				 * what! PANIC!
1489 				 */
1490 				  panic("escintr: Unknown message rejected!");
1491 				break;
1492 			case 0x08:	/* MO OPERATION */
1493 				break;
1494 			case 0x01:	/* EXTENDED MESSAGE */
1495 				switch(dev->sc_msg_in[2]) {
1496 				case 0x01:/* SYNC. DATA TRANSFER REQUEST */
1497 					/* Decode the SDTR message. */
1498 					period = 4*dev->sc_msg_in[3];
1499 					offset = dev->sc_msg_in[4];
1500 
1501 					/*
1502 					 * Make sure that the specs are within
1503 					 * chip limits. Note that if we
1504 					 * initiated the negotiation the specs
1505 					 * WILL be withing chip limits. If it
1506 					 * was the scsi unit that initiated
1507 					 * the negotiation, the specs may be
1508 					 * to high.
1509 					 */
1510 					if (offset > 16)
1511 						offset = 16;
1512 					if ((period < 200) &&
1513 					    (dev->sc_clock_freq <= 25))
1514 						period = 200;
1515 
1516 					if (offset == 0)
1517 					       period = 5*dev->sc_clock_period;
1518 
1519 					nexus->syncper = period/
1520 							  dev->sc_clock_period;
1521 					nexus->syncoff = offset;
1522 
1523 					if (period < 200)
1524 					  nexus->config3 |= ESC_CFG3_FASTSCSI;
1525 					else
1526 					  nexus->config3 &=~ESC_CFG3_FASTSCSI;
1527 
1528 					nexus->flags |= ESC_NF_SYNC_TESTED;
1529 
1530 					*rp->esc_syncper = nexus->syncper;
1531 					*rp->esc_syncoff = nexus->syncoff;
1532 					*rp->esc_config3 = nexus->config3;
1533 
1534 					/*
1535 					 * Hmmm, it seems that the scsi unit
1536 					 * initiated sync negotiation, so lets
1537 					 * reply acording to scsi-2 standard.
1538 					 */
1539 					if (!(nexus->flags& ESC_NF_SDTR_SENT))
1540 					{
1541 					  if ((dev->sc_config_flags &
1542 					       ESC_NO_SYNCH) ||
1543 					      (dev->sc_config_flags &
1544 					       ESC_NO_DMA) ||
1545 					      esc_inhibit_sync[
1546 							nexus->lun_unit & 7]) {
1547 					          period = 200;
1548 					          offset = 0;
1549 					  }
1550 
1551 					  nexus->offset = offset;
1552 					  nexus->period = period;
1553 					  nexus->flags |= ESC_NF_DO_SDTR;
1554 					  *rp->esc_command = ESC_CMD_SET_ATN;
1555 					}
1556 
1557 					nexus->flags &= ~ESC_NF_SDTR_SENT;
1558 					break;
1559 
1560 				case 0x00: /* MODIFY DATA POINTERS */
1561 				case 0x02: /* EXTENDED IDENTIFY (SCSI-1) */
1562 				case 0x03: /* WIDE DATA TRANSFER REQUEST */
1563 			        default:
1564 					/* Reject any unhandeled messages. */
1565 
1566 					dev->sc_msg_out[0] = 0x07;
1567 					dev->sc_msg_out_len = 1;
1568 					*rp->esc_command = ESC_CMD_SET_ATN;
1569 					cmd = ESC_CMD_MESSAGE_ACCEPTED;
1570 					break;
1571 				}
1572 				break;
1573 
1574 			default:
1575 				/* Reject any unhandeled messages. */
1576 
1577 				dev->sc_msg_out[0] = 0x07;
1578 				dev->sc_msg_out_len = 1;
1579 				*rp->esc_command = ESC_CMD_SET_ATN;
1580 				cmd = ESC_CMD_MESSAGE_ACCEPTED;
1581 				break;
1582 			}
1583 			nexus->flags &= ~ESC_NF_HAS_MSG;
1584 			dev->sc_msg_in_len = 0;
1585 		}
1586 		break;
1587 	default:
1588 		printf("ESCINTR: UNKNOWN PHASE! phase: %d\n",
1589 		       dev->sc_status & ESC_STAT_PHASE_MASK);
1590 		dev->sc_led(dev, 0);
1591 		esc_scsidone(dev, nexus->xs, -4);
1592 
1593 		return(-1);
1594 	}
1595 
1596 	if (cmd)
1597 		*rp->esc_command = cmd;
1598 
1599 	return(0);
1600 }
1601 
1602 /*
1603  * Stub for interrupt machine.
1604  */
1605 void
1606 escintr(dev)
1607 	struct esc_softc *dev;
1608 {
1609 	esc_regmap_p	 rp;
1610 	struct nexus	*nexus;
1611 
1612 	rp = dev->sc_esc;
1613 
1614 	if (!esc_pretests(dev, rp)) {
1615 
1616 		nexus = dev->sc_cur_nexus;
1617 		if (nexus == NULL)
1618 			nexus = dev->sc_sel_nexus;
1619 
1620 		if (nexus)
1621 			if (!esc_midaction(dev, rp, nexus))
1622 				esc_postaction(dev, rp, nexus);
1623 	}
1624 }
1625 
1626 /*
1627  * escicmd is used to perform IO when we can't use interrupts. escicmd
1628  * emulates the normal environment by waiting for the chip and calling
1629  * escintr.
1630  */
1631 void
1632 escicmd(dev, pendp)
1633 	struct esc_softc   *dev;
1634 	struct esc_pending *pendp;
1635 {
1636 	esc_regmap_p	 rp;
1637 	struct nexus	*nexus;
1638 
1639 	nexus = &dev->sc_nexus[pendp->xs->xs_periph->periph_target];
1640 	rp = dev->sc_esc;
1641 
1642 	if (!escselect(dev, pendp, (char *)pendp->xs->cmd, pendp->xs->cmdlen,
1643 			(char *)pendp->xs->data, pendp->xs->datalen,
1644 			ESC_SELECT_I))
1645 		panic("escicmd: Couldn't select unit");
1646 
1647 	while(nexus->state != ESC_NS_FINISHED) {
1648 		esciwait(dev);
1649 		escintr(dev);
1650 	}
1651 
1652 	nexus->flags &= ~ESC_NF_SYNC_TESTED;
1653 }
1654