Expand PMF_FN_* macros.
[netbsd-mini2440.git] / sys / arch / acorn32 / podulebus / esc.c
blobfe591ebb986674b705d361319379c34b23eec670
1 /* $NetBSD: esc.c,v 1.22 2009/03/18 17:06:41 cegger Exp $ */
3 /*
4 * Copyright (c) 1990 The Regents of the University of California.
5 * All rights reserved.
7 * This code is derived from software contributed to Berkeley by
8 * Van Jacobson of Lawrence Berkeley Laboratory.
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.
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.
34 * @(#)scsi.c 7.5 (Berkeley) 5/4/91
38 * Copyright (c) 1995 Scott Stevens
39 * Copyright (c) 1995 Daniel Widenfalk
40 * Copyright (c) 1994 Christian E. Hopps
42 * This code is derived from software contributed to Berkeley by
43 * Van Jacobson of Lawrence Berkeley Laboratory.
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.
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.
73 * @(#)scsi.c 7.5 (Berkeley) 5/4/91
77 * AMD AM53CF94 scsi adaptor driver
79 * Functionally compatible with the FAS216
81 * Apart from a very small patch to set up control register 4
85 * Modified for NetBSD/arm32 by Scott Stevens
88 #include <sys/cdefs.h>
89 __KERNEL_RCSID(0, "$NetBSD: esc.c,v 1.22 2009/03/18 17:06:41 cegger Exp $");
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>
100 #include <uvm/uvm_extern.h>
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>
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);
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 *);
142 * Initialize these to make 'em patchable. Defaults to enable sync and discon.
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 };
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
156 * default minphys routine for esc based controllers
158 void
159 esc_minphys(struct buf *bp)
163 * No max transfer at this level.
165 minphys(bp);
169 * Initialize the nexus structs.
171 void
172 esc_init_nexus(struct esc_softc *dev, struct nexus *nexus)
174 memset(nexus, 0, sizeof(struct nexus));
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;
184 void
185 escinitialize(struct esc_softc *dev)
187 u_int *pte;
188 int i;
190 dev->sc_led_status = 0;
192 TAILQ_INIT(&dev->sc_xs_pending);
193 TAILQ_INIT(&dev->sc_xs_free);
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.
199 for(i=0; i<MAXPENDING; i++) {
200 TAILQ_INSERT_TAIL(&dev->sc_xs_free, &dev->sc_xs_store[i],
201 link);
205 * Calculate the correct clock conversion factor 2 <= factor <= 8, i.e. set
206 * the factor to clock_freq / 5 (int).
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");
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);
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;
228 escreset(dev, 1 | 2); /* Reset Chip and Bus */
230 dev->sc_units_disconnected = 0;
231 dev->sc_msg_in_len = 0;
232 dev->sc_msg_out_len = 0;
234 dev->sc_flags = 0;
236 for(i=0; i<8; i++)
237 esc_init_nexus(dev, &dev->sc_nexus[i]);
240 * Setup bump buffer.
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);
248 * Setup pages to noncachable, that way we don't have to flush the cache
249 * every time we need "bumped" transfer.
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);
257 printf(" dmabuf V0x%08x P0x%08x", (u_int)dev->sc_bump_va, (u_int)dev->sc_bump_pa);
262 * used by specific esc controller
264 void
265 esc_scsi_request(struct scsipi_channel *chan, scsipi_adapter_req_t req,
266 void *arg)
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;
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;
281 if (flags & XS_CTL_DATA_UIO)
282 panic("esc: scsi data uio requested");
284 if ((flags & XS_CTL_POLL) && (dev->sc_flags & ESC_ACTIVE))
285 panic("esc_scsicmd: busy");
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;
296 TAILQ_REMOVE(&dev->sc_xs_free, pendp, link);
297 pendp->xs = xs;
298 splx(s);
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);
310 return;
311 case ADAPTER_REQ_GROW_RESOURCES:
312 case ADAPTER_REQ_SET_XFER_MODE:
313 /* XXX Not supported. */
314 return;
320 * Actually select the unit, whereby the whole scsi-process is started.
322 void
323 esc_donextcmd(struct esc_softc *dev, struct esc_pending *pendp)
325 int s;
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.
334 if (pendp->xs->xs_control & XS_CTL_RESET) {
335 struct nexus *nexus;
337 s = splbio();
338 while(!escselect(dev, pendp, 0, 0, 0, 0, ESC_SELECT_K)) {
339 splx(s);
340 delay(10);
341 s = splbio();
344 nexus = dev->sc_cur_nexus;
345 while(nexus->flags & ESC_NF_UNIT_BUSY) {
346 esciwait(dev);
347 escintr(dev);
350 nexus->flags |= ESC_NF_UNIT_BUSY;
351 splx(s);
353 escreset(dev, 0);
355 s = splbio();
356 nexus->flags &= ~ESC_NF_UNIT_BUSY;
357 splx(s);
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.
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;
375 void
376 esc_scsidone(struct esc_softc *dev, struct scsipi_xfer *xs, int stat)
378 struct esc_pending *pendp;
379 int s;
381 xs->status = stat;
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;
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;
411 if (pendp != NULL) {
412 TAILQ_REMOVE(&dev->sc_xs_pending, pendp, link);
415 splx(s);
416 scsipi_done(xs);
418 if (pendp)
419 esc_donextcmd(dev, pendp);
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.
429 void
430 escreset(struct esc_softc *dev, int how)
432 esc_regmap_p rp;
433 int i, s;
435 rp = dev->sc_esc;
437 if (how & 1) {
438 for(i=0; i<8; i++)
439 esc_init_nexus(dev, &dev->sc_nexus[i]);
441 *rp->esc_command = ESC_CMD_RESET_CHIP;
442 delay(1);
443 *rp->esc_command = ESC_CMD_NOP;
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;
454 if (how & 2) {
455 for(i=0; i<8; i++)
456 esc_init_nexus(dev, &dev->sc_nexus[i]);
458 s = splbio();
460 *rp->esc_command = ESC_CMD_RESET_SCSI_BUS;
461 delay(100);
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;
468 delay(100);
471 dev->sc_status = *rp->esc_status;
472 dev->sc_interrupt = *rp->esc_interrupt;
474 splx(s);
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 */
484 * Save active data pointers to the nexus block currently active.
486 void
487 esc_save_pointers(struct esc_softc *dev)
489 struct nexus *nx;
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;
506 * Restore data pointers from the currently active nexus block.
508 void
509 esc_restore_pointers(struct esc_softc *dev)
511 struct nexus *nx;
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;
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.
534 void
535 esciwait(struct esc_softc *dev)
537 esc_regmap_p rp;
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.
543 if (dev->sc_flags & ESC_DONT_WAIT) {
544 dev->sc_flags &= ~ESC_DONT_WAIT;
545 return;
548 rp = dev->sc_esc;
550 /* Wait for FAS chip to signal an interrupt. */
551 while(!(*rp->esc_status & ESC_STAT_INTERRUPT_PENDING));
552 /* delay(1);*/
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;
563 #if 0
565 * Transfer info to/from device. esc_ixfer uses polled IO+esciwait so the
566 * rules that apply to esciwait also applies here.
568 void
569 esc_ixfer(struct esc_softc *dev)
571 esc_regmap_p rp;
572 u_char *buf;
573 int len, mode, phase;
575 rp = dev->sc_esc;
576 buf = dev->sc_buf;
577 len = dev->sc_len;
580 * Decode the scsi phase to determine whether we are reading or writing.
581 * mode == 1 => READ, mode == 0 => WRITE
583 phase = dev->sc_status & ESC_STAT_PHASE_MASK;
584 mode = (phase == ESC_PHASE_DATA_IN);
586 while(len && ((dev->sc_status & ESC_STAT_PHASE_MASK) == phase))
587 if (mode) {
588 *rp->esc_command = ESC_CMD_TRANSFER_INFO;
590 esciwait(dev);
592 *buf++ = *rp->esc_fifo;
593 len--;
594 } else {
595 len--;
596 *rp->esc_fifo = *buf++;
597 *rp->esc_command = ESC_CMD_TRANSFER_INFO;
599 esciwait(dev);
602 /* Update buffer pointers to reflect the sent/received data. */
603 dev->sc_buf = buf;
604 dev->sc_len = len;
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.
611 dev->sc_flags |= ESC_DONT_WAIT;
613 #else
615 * Transfer info to/from device. esc_ixfer uses polled IO+esciwait so the
616 * rules that apply to esciwait also applies here.
618 void
619 esc_ixfer(struct esc_softc *dev)
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;
629 rp = dev->sc_esc;
630 buf = dev->sc_buf;
631 len = dev->sc_len;
633 /* Use discrete variables for better optimisation */
635 esc_status = rp->esc_status;
636 esc_command = rp->esc_command;
637 esc_interrupt = rp->esc_interrupt;
638 esc_fifo = rp->esc_fifo;
641 * Decode the scsi phase to determine whether we are reading or writing.
642 * mode == 1 => READ, mode == 0 => WRITE
644 phase = dev->sc_status & ESC_STAT_PHASE_MASK;
645 mode = (phase == ESC_PHASE_DATA_IN);
647 if (mode) {
648 while(len && ((dev->sc_status & ESC_STAT_PHASE_MASK) == phase)) {
649 *esc_command = ESC_CMD_TRANSFER_INFO;
651 /* Wait for FAS chip to signal an interrupt. */
652 while(!(*esc_status & ESC_STAT_INTERRUPT_PENDING));
653 /* delay(1);*/
655 /* Grab interrupt info from chip. */
656 dev->sc_status = *esc_status;
657 dev->sc_interrupt = *esc_interrupt;
659 *buf++ = *esc_fifo;
660 len--;
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;
668 /* Wait for FAS chip to signal an interrupt. */
669 while(!(*esc_status & ESC_STAT_INTERRUPT_PENDING));
670 /* delay(1);*/
672 /* Grab interrupt info from chip. */
673 dev->sc_status = *esc_status;
674 dev->sc_interrupt = *esc_interrupt;
678 /* Update buffer pointers to reflect the sent/received data. */
679 dev->sc_buf = buf;
680 dev->sc_len = len;
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.
687 dev->sc_flags |= ESC_DONT_WAIT;
689 #endif
692 * Build a Synchronous Data Transfer Request message
694 void
695 esc_build_sdtrm(struct esc_softc *dev, int period, int offset)
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;
706 * Arbitate the scsi bus and select the unit
709 esc_select_unit(struct esc_softc *dev, short target)
711 esc_regmap_p rp;
712 struct nexus *nexus;
713 int s, retcode, i;
714 u_char cmd;
716 s = splbio(); /* Do this at splbio so that we won't be disturbed. */
718 retcode = 0;
720 nexus = &dev->sc_nexus[target];
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.
726 if (!(dev->sc_flags & ESC_ACTIVE) && !dev->sc_sel_nexus) {
727 dev->sc_flags |= ESC_ACTIVE;
729 rp = dev->sc_esc;
731 *rp->esc_syncper = nexus->syncper;
732 *rp->esc_syncoff = nexus->syncoff;
733 *rp->esc_config3 = nexus->config3;
735 *rp->esc_config1 = dev->sc_config1;
736 *rp->esc_timeout = dev->sc_timeout_val;
737 *rp->esc_dest_id = target;
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];
749 cmd = ESC_CMD_SEL_ATN;
752 dev->sc_sel_nexus = nexus;
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;
762 splx(s);
763 return(retcode);
767 * Grab the nexus if available else return 0.
769 struct nexus *
770 esc_arbitate_target(struct esc_softc *dev, int target)
772 struct nexus *nexus;
773 int s;
776 * This is realy simple. Raise interrupt level to splbio. Grab the nexus and
777 * leave.
779 nexus = &dev->sc_nexus[target];
781 s = splbio();
783 if (nexus->flags & ESC_NF_UNIT_BUSY)
784 nexus = 0;
785 else
786 nexus->flags |= ESC_NF_UNIT_BUSY;
788 splx(s);
789 return(nexus);
793 * Setup a nexus for use. Initializes command, buffer pointers and DMA chain.
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)
798 int sync, target, lun;
800 target = pendp->xs->xs_periph->periph_target;
801 lun = pendp->xs->xs_periph->periph_lun;
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.
808 if ((dev->sc_config_flags & (ESC_NO_SYNCH | ESC_NO_DMA)) ||
809 esc_inhibit_sync[(int)target])
810 mode &= ~ESC_SELECT_S;
812 if ((dev->sc_config_flags & ESC_NO_RESELECT) ||
813 esc_inhibit_disc[(int)target])
814 mode &= ~ESC_SELECT_R;
816 nexus->xs = pendp->xs;
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;
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;
837 if (mode & ESC_SELECT_I)
838 nexus->flags |= ESC_NF_IMMEDIATE;
839 if (mode & ESC_SELECT_K)
840 nexus->flags |= ESC_NF_RESET;
842 sync = ((mode & ESC_SELECT_S) ? 1 : 0);
844 /* We can't use sync during polled IO. */
845 if (sync && (mode & ESC_SELECT_I))
846 sync = 0;
848 if (!sync &&
849 ((nexus->flags & ESC_NF_SYNC_TESTED) && (nexus->offset != 0))) {
851 * If the scsi unit is set to synch transfer and we don't want
852 * that, we have to renegotiate.
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)) {
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
866 nexus->flags |= ESC_NF_DO_SDTR;
867 nexus->period = ((dev->sc_clock_freq>25) ? 100 : 200);
868 nexus->offset = 8;
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;
877 * Fake a DMA-block for polled IO. This way we can use the same code to handle
878 * reselection. Much nicer this way.
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);
890 /* Flush the caches. */
892 if (len && !(mode & ESC_SELECT_I))
893 cpu_dcache_wbinv_range((vm_offset_t)buf, len);
897 escselect(struct esc_softc *dev, struct esc_pending *pendp, unsigned char *cbuf, int clen, unsigned char *buf, int len, int mode)
899 struct nexus *nexus;
901 /* Get the nexus struct. */
902 nexus = esc_arbitate_target(dev, pendp->xs->xs_periph->periph_target);
903 if (nexus == NULL)
904 return(0);
906 /* Setup the nexus struct. */
907 esc_setup_nexus(dev, nexus, pendp, cbuf, clen, buf, len, mode);
909 /* Post it to the interrupt machine. */
910 esc_select_unit(dev, pendp->xs->xs_periph->periph_target);
912 return(1);
916 escgo(struct esc_softc *dev, struct esc_pending *pendp)
918 int s;
919 char *buf;
921 buf = pendp->xs->data;
923 if (escselect(dev, pendp, (char *)pendp->xs->cmd, pendp->xs->cmdlen,
924 buf, pendp->xs->datalen, ESC_SELECT_RS)) {
926 * We got the command going so the esc_pending struct is now
927 * free to reuse.
930 s = splbio();
931 TAILQ_INSERT_TAIL(&dev->sc_xs_free, pendp, link);
932 splx(s);
933 } else {
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.
940 s = splbio();
941 TAILQ_INSERT_HEAD(&dev->sc_xs_pending, pendp, link);
942 splx(s);
945 return(0);
949 * Part one of the interrupt machine. Error checks and reselection test.
950 * We don't know if we have an active nexus here!
953 esc_pretests(struct esc_softc *dev, esc_regmap_p rp)
955 struct nexus *nexus;
956 int i, s;
958 if (dev->sc_interrupt & ESC_INT_SCSI_RESET_DETECTED) {
960 * Cleanup and notify user. Lets hope that this is all we
961 * have to do
964 for(i=0; i<8; i++) {
965 if (dev->sc_nexus[i].xs)
966 esc_scsidone(dev, dev->sc_nexus[i].xs, -2);
968 esc_init_nexus(dev, &dev->sc_nexus[i]);
970 printf("escintr: SCSI-RESET detected!");
971 return(-1);
974 if (dev->sc_interrupt & ESC_INT_ILLEGAL_COMMAND) {
975 /* Something went terrible wrong! Dump some data and panic! */
977 printf("FIFO:");
978 while(*rp->esc_fifo_flags & ESC_FIFO_COUNT_MASK)
979 printf(" %x", *rp->esc_fifo);
980 printf("\n");
982 printf("CMD: %x\n", *rp->esc_command);
983 panic("escintr: ILLEGAL COMMAND!");
986 if (dev->sc_interrupt & ESC_INT_RESELECTED) {
987 /* We were reselected. Set the chip as busy */
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;
995 splx(s);
997 if (dev->sc_units_disconnected) {
998 /* Find out who reselected us. */
1000 dev->sc_resel[0] &= ~(1<<dev->sc_host_id);
1002 for(i=0; i<8; i++)
1003 if (dev->sc_resel[0] & (1<<i))
1004 break;
1006 if (i == 8)
1007 panic("Illegal reselection!");
1009 if (dev->sc_nexus[i].state == ESC_NS_DISCONNECTED) {
1011 * This unit had disconnected, so we reconnect
1012 * it.
1015 dev->sc_cur_nexus = &dev->sc_nexus[i];
1016 nexus = dev->sc_cur_nexus;
1018 *rp->esc_syncper = nexus->syncper;
1019 *rp->esc_syncoff = nexus->syncoff;
1020 *rp->esc_config3 = nexus->config3;
1022 *rp->esc_dest_id = i & 7;
1024 dev->sc_units_disconnected--;
1025 dev->sc_msg_in_len= 0;
1027 /* Restore active pointers. */
1028 esc_restore_pointers(dev);
1030 nexus->state = ESC_NS_RESELECTED;
1032 *rp->esc_command = ESC_CMD_MESSAGE_ACCEPTED;
1034 return(1);
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!");
1045 return(0);
1049 * Part two of the interrupt machine. Handle disconnection and post command
1050 * processing. We know that we have an active nexus here.
1053 esc_midaction(struct esc_softc *dev, esc_regmap_p rp, struct nexus *nexus)
1055 int i, left, len, s;
1056 u_char status, msg;
1058 if (dev->sc_interrupt & ESC_INT_DISCONNECT) {
1059 s = splbio();
1060 dev->sc_cur_nexus = 0;
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;
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;
1073 * We were trying to select the unit. Probably no unit
1074 * at this ID.
1076 nexus->xs->resid = dev->sc_len;
1078 nexus->status = -2;
1079 nexus->flags &= ~ESC_NF_UNIT_BUSY;
1080 nexus->state = ESC_NS_FINISHED;
1081 break;
1083 case ESC_NS_DONE:
1084 /* All done. */
1085 nexus->xs->resid = dev->sc_len;
1087 nexus->flags &= ~ESC_NF_UNIT_BUSY;
1088 nexus->state = ESC_NS_FINISHED;
1089 dev->sc_led(dev, 0);
1090 break;
1092 case ESC_NS_DISCONNECTING:
1094 * We have received a DISCONNECT message, so we are
1095 * doing a normal disconnection.
1097 nexus->state = ESC_NS_DISCONNECTED;
1099 dev->sc_units_disconnected++;
1100 break;
1102 case ESC_NS_RESET:
1104 * We were reseting this SCSI-unit. Clean up the
1105 * nexus struct.
1107 dev->sc_led(dev, 0);
1108 esc_init_nexus(dev, nexus);
1109 break;
1111 default:
1113 * Unexpected disconnection! Cleanup and exit. This
1114 * shouldn't cause any problems.
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]);
1122 nexus->xs->resid = dev->sc_len;
1124 nexus->flags &= ~ESC_NF_UNIT_BUSY;
1125 nexus->state = ESC_NS_FINISHED;
1126 nexus->status = -3;
1128 dev->sc_led(dev, 0);
1129 break;
1133 * If we have disconnected units, we MUST enable reselection
1134 * within 250ms.
1136 if (dev->sc_units_disconnected &&
1137 !(dev->sc_flags & ESC_ACTIVE))
1138 *rp->esc_command = ESC_CMD_ENABLE_RESEL;
1140 splx(s);
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;
1148 /* We are done with this nexus! */
1149 if (nexus->state == ESC_NS_FINISHED)
1150 esc_scsidone(dev, nexus->xs, nexus->status);
1152 return(1);
1155 switch(nexus->state) {
1156 case ESC_NS_SELECTED:
1157 dev->sc_cur_nexus = nexus;
1158 dev->sc_sel_nexus = 0;
1160 nexus->flags &= ~ESC_NF_SELECT_ME;
1163 * We have selected a unit. Setup chip, restore pointers and
1164 * light the led.
1166 *rp->esc_syncper = nexus->syncper;
1167 *rp->esc_syncoff = nexus->syncoff;
1168 *rp->esc_config3 = nexus->config3;
1170 esc_restore_pointers(dev);
1172 nexus->status = 0xFF;
1173 dev->sc_msg_in[0] = 0xFF;
1174 dev->sc_msg_in_len= 0;
1176 dev->sc_led(dev, 1);
1178 break;
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) {
1186 * Clean up DMA and at the same time get how
1187 * many bytes that were NOT transfered.
1189 left = dev->sc_setup_dma(dev, 0, 0, ESC_DMA_CLEAR);
1190 len = dev->sc_dma_len;
1192 if (nexus->state == ESC_NS_DATA_IN) {
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.
1199 if (dev->sc_dma_buf == dev->sc_bump_pa)
1201 while((*rp->esc_fifo_flags&ESC_FIFO_COUNT_MASK)
1202 && left)
1203 dev->sc_bump_va[len-(left--)] = *rp->esc_fifo;
1205 memcpy(dev->sc_buf, dev->sc_bump_va, len-left);
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;
1214 * Update pointers/length to reflect the transfered
1215 * data.
1217 dev->sc_len -= len-left;
1218 dev->sc_buf += len-left;
1220 dev->sc_dma_buf = (char *)dev->sc_dma_buf + len-left;
1221 dev->sc_dma_len = left;
1223 dev->sc_dma_blk_ptr = (char *)dev->sc_dma_blk_ptr +
1224 len-left;
1225 dev->sc_dma_blk_len -= len-left;
1228 * If it was the end of a DMA block, we select the
1229 * next to begin with.
1231 if (!dev->sc_dma_blk_len)
1232 dev->sc_cur_link++;
1234 break;
1236 case ESC_NS_STATUS:
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.
1242 status = *rp->esc_fifo;
1243 msg = *rp->esc_fifo;
1245 nexus->status = status;
1246 if (status != 0)
1247 nexus->status = -1;
1250 * Preload the command complete message. Handeled in
1251 * esc_postaction.
1253 dev->sc_msg_in[0] = msg;
1254 dev->sc_msg_in_len = 1;
1255 nexus->flags |= ESC_NF_HAS_MSG;
1256 break;
1258 default:
1259 break;
1262 return(0);
1266 * Part three of the interrupt machine. Handle phase changes (and repeated
1267 * phase passes). We know that we have an active nexus here.
1270 esc_postaction(struct esc_softc *dev, esc_regmap_p rp, struct nexus *nexus)
1272 int i, len;
1273 u_char cmd;
1274 short offset, period;
1276 cmd = 0;
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;
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;
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;
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);
1312 * If len != 0 we must bump the data, else we just DMA it
1313 * straight into memory.
1315 if (len) {
1316 dev->sc_dma_buf = dev->sc_bump_pa;
1317 dev->sc_dma_len = len;
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;
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));
1332 printf("Using DMA !!!!\n");
1333 cmd = ESC_CMD_TRANSFER_INFO | ESC_CMD_DMA;
1334 } else {
1336 * Hmmm, the unit wants more info than we have or has
1337 * more than we want. Let the chip handle that.
1340 *rp->esc_tc_low = 0;
1341 *rp->esc_tc_mid = 1;
1342 *rp->esc_tc_high = 0;
1343 cmd = ESC_CMD_TRANSFER_PAD;
1345 break;
1347 case ESC_PHASE_COMMAND:
1348 /* The scsi unit wants the command, send it. */
1349 nexus->state = ESC_NS_SVC;
1351 *rp->esc_command = ESC_CMD_FLUSH_FIFO;
1352 for(i=0; i<5; i++);
1354 for(i=0; i<nexus->clen; i++)
1355 *rp->esc_fifo = nexus->cbuf[i];
1356 cmd = ESC_CMD_TRANSFER_INFO;
1357 break;
1359 case ESC_PHASE_STATUS:
1361 * We've got status phase. Request status and command
1362 * complete message.
1364 nexus->state = ESC_NS_STATUS;
1365 cmd = ESC_CMD_COMMAND_COMPLETE;
1366 break;
1368 case ESC_PHASE_MESSAGE_OUT:
1370 * Either the scsi unit wants us to send a message or we have
1371 * asked for it by seting the ATN bit.
1373 nexus->state = ESC_NS_MSG_OUT;
1375 *rp->esc_command = ESC_CMD_FLUSH_FIFO;
1377 if (nexus->flags & ESC_NF_DO_SDTR) {
1378 /* Send a Synchronous Data Transfer Request. */
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. */
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. */
1393 dev->sc_msg_out[0] = 0x08;
1394 dev->sc_msg_out_len = 1;
1397 cmd = ESC_CMD_TRANSFER_INFO;
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;
1403 break;
1405 case ESC_PHASE_MESSAGE_IN:
1406 /* Receive a message from the scsi unit. */
1407 nexus->state = ESC_NS_MSG_IN;
1409 while(!(nexus->flags & ESC_NF_HAS_MSG)) {
1410 *rp->esc_command = ESC_CMD_TRANSFER_INFO;
1411 esciwait(dev);
1413 dev->sc_msg_in[dev->sc_msg_in_len++] = *rp->esc_fifo;
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;
1432 *rp->esc_command = ESC_CMD_MESSAGE_ACCEPTED;
1433 esciwait(dev);
1435 if ((dev->sc_status & ESC_STAT_PHASE_MASK) !=
1436 ESC_PHASE_MESSAGE_IN)
1437 break;
1440 cmd = ESC_CMD_MESSAGE_ACCEPTED;
1441 if (nexus->flags & ESC_NF_HAS_MSG) {
1442 /* We have a message. Decode it. */
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 */
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.
1462 if (nexus->flags & ESC_NF_SDTR_SENT) {
1463 nexus->flags &= ~ESC_NF_SDTR_SENT;
1465 nexus->config3 &= ~ESC_CFG3_FASTSCSI;
1466 nexus->syncper = 5;
1467 nexus->syncoff = 0;
1469 *rp->esc_syncper = nexus->syncper;
1470 *rp->esc_syncoff = nexus->syncoff;
1471 *rp->esc_config3 = nexus->config3;
1472 } else
1474 * Something was rejected but we don't know
1475 * what! PANIC!
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];
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.
1497 if (offset > 16)
1498 offset = 16;
1499 if ((period < 200) &&
1500 (dev->sc_clock_freq <= 25))
1501 period = 200;
1503 if (offset == 0)
1504 period = 5*dev->sc_clock_period;
1506 nexus->syncper = period/
1507 dev->sc_clock_period;
1508 nexus->syncoff = offset;
1510 if (period < 200)
1511 nexus->config3 |= ESC_CFG3_FASTSCSI;
1512 else
1513 nexus->config3 &=~ESC_CFG3_FASTSCSI;
1515 nexus->flags |= ESC_NF_SYNC_TESTED;
1517 *rp->esc_syncper = nexus->syncper;
1518 *rp->esc_syncoff = nexus->syncoff;
1519 *rp->esc_config3 = nexus->config3;
1522 * Hmmm, it seems that the scsi unit
1523 * initiated sync negotiation, so lets
1524 * reply acording to scsi-2 standard.
1526 if (!(nexus->flags& ESC_NF_SDTR_SENT))
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;
1538 nexus->offset = offset;
1539 nexus->period = period;
1540 nexus->flags |= ESC_NF_DO_SDTR;
1541 *rp->esc_command = ESC_CMD_SET_ATN;
1544 nexus->flags &= ~ESC_NF_SDTR_SENT;
1545 break;
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. */
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;
1559 break;
1561 default:
1562 /* Reject any unhandeled messages. */
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;
1570 nexus->flags &= ~ESC_NF_HAS_MSG;
1571 dev->sc_msg_in_len = 0;
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);
1580 return(-1);
1583 if (cmd)
1584 *rp->esc_command = cmd;
1586 return(0);
1590 * Stub for interrupt machine.
1592 void
1593 escintr(struct esc_softc *dev)
1595 esc_regmap_p rp;
1596 struct nexus *nexus;
1598 rp = dev->sc_esc;
1600 if (!esc_pretests(dev, rp)) {
1602 nexus = dev->sc_cur_nexus;
1603 if (nexus == NULL)
1604 nexus = dev->sc_sel_nexus;
1606 if (nexus)
1607 if (!esc_midaction(dev, rp, nexus))
1608 esc_postaction(dev, rp, nexus);
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.
1617 void
1618 escicmd(struct esc_softc *dev, struct esc_pending *pendp)
1620 esc_regmap_p rp;
1621 struct nexus *nexus;
1623 nexus = &dev->sc_nexus[pendp->xs->xs_periph->periph_target];
1624 rp = dev->sc_esc;
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");
1631 while(nexus->state != ESC_NS_FINISHED) {
1632 esciwait(dev);
1633 escintr(dev);
1636 nexus->flags &= ~ESC_NF_SYNC_TESTED;