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