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