1 /* $NecBSD: tmc18c30.c,v 1.28.12.3 2001/06/19 04:35:48 honda Exp $ */
6 #define STG_IO_CONTROL_FLAGS (STG_FIFO_INTERRUPTS | STG_WAIT_FOR_SELECT)
9 * [NetBSD for NEC PC-98 series]
10 * Copyright (c) 1996, 1997, 1998, 1999, 2000, 2001
11 * NetBSD/pc98 porting staff. All rights reserved.
12 * Copyright (c) 1996, 1997, 1998, 1999, 2000, 2001
13 * Naofumi HONDA. All rights reserved.
14 * Copyright (c) 1996, 1997, 1998, 1999
15 * Kouichi Matsuda. All rights reserved.
17 * Redistribution and use in source and binary forms, with or without
18 * modification, are permitted provided that the following conditions
20 * 1. Redistributions of source code must retain the above copyright
21 * notice, this list of conditions and the following disclaimer.
22 * 2. Redistributions in binary form must reproduce the above copyright
23 * notice, this list of conditions and the following disclaimer in the
24 * documentation and/or other materials provided with the distribution.
25 * 3. The name of the author may not be used to endorse or promote products
26 * derived from this software without specific prior written permission.
28 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
29 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
30 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
31 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
32 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
33 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
34 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
36 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
37 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38 * POSSIBILITY OF SUCH DAMAGE.
41 #include <sys/cdefs.h>
42 __FBSDID("$FreeBSD$");
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/kernel.h>
47 #if defined(__FreeBSD__) && __FreeBSD_version >= 500001
49 #endif /* __FreeBSD__ */
51 #include <sys/queue.h>
52 #include <sys/malloc.h>
53 #include <sys/errno.h>
56 #include <sys/device.h>
57 #include <machine/bus.h>
58 #include <machine/intr.h>
60 #include <dev/scsipi/scsi_all.h>
61 #include <dev/scsipi/scsipi_all.h>
62 #include <dev/scsipi/scsiconf.h>
63 #include <dev/scsipi/scsi_disk.h>
65 #include <machine/dvcfg.h>
66 #include <machine/physio_proc.h>
68 #include <i386/Cbus/dev/scsi_low.h>
69 #include <i386/Cbus/dev/tmc18c30reg.h>
70 #include <i386/Cbus/dev/tmc18c30var.h>
71 #endif /* __NetBSD__ */
74 #include <machine/cpu.h>
75 #include <machine/bus.h>
77 #include <compat/netbsd/dvcfg.h>
78 #include <compat/netbsd/physio_proc.h>
80 #include <cam/scsi/scsi_low.h>
81 #include <dev/stg/tmc18c30reg.h>
82 #include <dev/stg/tmc18c30var.h>
83 #endif /* __FreeBSD__ */
85 /***************************************************
87 ***************************************************/
88 /* DEVICE CONFIGURATION FLAGS (MINOR)
91 * 0x02 PARITY LINE OFF
92 * 0x04 IDENTIFY MSG OFF ( = single lun)
93 * 0x08 SYNC TRANSFER OFF
95 /* #define STG_SYNC_SUPPORT */ /* NOT YET but easy */
97 /* For the 512 fifo type: change below */
98 #define TMC18C30_FIFOSZ 0x800
99 #define TMC18C30_FCBSZ 0x200
100 #define TMC18C50_FIFOSZ 0x2000
101 #define TMC18C50_FCBSZ 0x400
103 #define STG_MAX_DATA_SIZE (64 * 1024)
104 #define STG_DELAY_MAX (2 * 1000 * 1000)
105 #define STG_DELAY_INTERVAL (1)
106 #define STG_DELAY_SELECT_POLLING_MAX (5 * 1000 * 1000)
108 /***************************************************
110 ***************************************************/
111 #define STG_NTARGETS 8
114 /***************************************************
116 ***************************************************/
118 static int stg_debug;
119 #endif /* STG_DEBUG */
122 static struct stg_statics {
128 #endif /* STG_STATICS */
130 /***************************************************
132 ***************************************************/
133 #define STG_FIFO_INTERRUPTS 0x0001
134 #define STG_WAIT_FOR_SELECT 0x0100
136 int stg_io_control = STG_IO_CONTROL_FLAGS;
138 /***************************************************
140 ***************************************************/
141 extern struct cfdriver stg_cd;
143 /**************************************************************
145 **************************************************************/
147 static void stg_pio_read(struct stg_softc *, struct targ_info *, u_int);
148 static void stg_pio_write(struct stg_softc *, struct targ_info *, u_int);
149 static int stg_xfer(struct stg_softc *, u_int8_t *, int, int, int);
150 static int stg_msg(struct stg_softc *, struct targ_info *, u_int);
151 static int stg_reselected(struct stg_softc *);
152 static int stg_disconnected(struct stg_softc *, struct targ_info *);
153 static __inline void stg_pdma_end(struct stg_softc *, struct targ_info *);
154 static int stghw_select_targ_wait(struct stg_softc *, int);
155 static int stghw_check(struct stg_softc *);
156 static void stghw_init(struct stg_softc *);
157 static int stg_negate_signal(struct stg_softc *, u_int8_t, u_char *);
158 static int stg_expect_signal(struct stg_softc *, u_int8_t, u_int8_t);
159 static int stg_world_start(struct stg_softc *, int);
160 static int stghw_start_selection(struct stg_softc *sc, struct slccb *);
161 static void stghw_bus_reset(struct stg_softc *);
162 static void stghw_attention(struct stg_softc *);
163 static int stg_target_nexus_establish(struct stg_softc *);
164 static int stg_lun_nexus_establish(struct stg_softc *);
165 static int stg_ccb_nexus_establish(struct stg_softc *);
166 static int stg_targ_init(struct stg_softc *, struct targ_info *, int);
167 static __inline void stghw_bcr_write_1(struct stg_softc *, u_int8_t);
168 static int stg_timeout(struct stg_softc *);
169 static void stg_selection_done_and_expect_msgout(struct stg_softc *);
171 struct scsi_low_funcs stgfuncs = {
172 SC_LOW_INIT_T stg_world_start,
173 SC_LOW_BUSRST_T stghw_bus_reset,
174 SC_LOW_TARG_INIT_T stg_targ_init,
175 SC_LOW_LUN_INIT_T NULL,
177 SC_LOW_SELECT_T stghw_start_selection,
178 SC_LOW_NEXUS_T stg_lun_nexus_establish,
179 SC_LOW_NEXUS_T stg_ccb_nexus_establish,
181 SC_LOW_ATTEN_T stghw_attention,
182 SC_LOW_MSG_T stg_msg,
184 SC_LOW_TIMEOUT_T stg_timeout,
185 SC_LOW_POLL_T stgintr,
190 /****************************************************
192 ****************************************************/
194 stghw_bcr_write_1(struct stg_softc *sc, u_int8_t bcv)
197 bus_space_write_1(sc->sc_iot, sc->sc_ioh, tmc_bctl, bcv);
203 struct stg_softc *sc;
205 struct scsi_low_softc *slp = &sc->sc_sclow;
206 bus_space_tag_t iot = sc->sc_iot;
207 bus_space_handle_t ioh = sc->sc_ioh;
211 lsb = bus_space_read_1(iot, ioh, tmc_idlsb);
212 msb = bus_space_read_1(iot, ioh, tmc_idmsb);
213 switch (msb << 8 | lsb)
216 /* TMCCHIP_1800 not supported. (it's my policy) */
217 sc->sc_chip = TMCCHIP_1800;
221 if (bus_space_read_1(iot, ioh, tmc_cfg2) & 0x02)
223 sc->sc_chip = TMCCHIP_18C30;
224 sc->sc_fsz = TMC18C30_FIFOSZ;
225 fcbsize = TMC18C30_FCBSZ;
229 sc->sc_chip = TMCCHIP_18C50;
230 sc->sc_fsz = TMC18C50_FIFOSZ;
231 fcbsize = TMC18C50_FCBSZ;
236 sc->sc_chip = TMCCHIP_UNK;
240 sc->sc_fcRinit = FCTL_INTEN;
241 sc->sc_fcWinit = FCTL_PARENB | FCTL_INTEN;
243 if (slp->sl_cfgflags & CFG_NOATTEN)
246 sc->sc_imsg = BCTL_ATN;
247 sc->sc_busc = BCTL_BUSEN;
249 sc->sc_wthold = fcbsize + 256;
250 sc->sc_rthold = fcbsize - 256;
251 sc->sc_maxwsize = sc->sc_fsz;
253 fcb = fcbsize / (sc->sc_fsz / 16);
254 sc->sc_icinit = ICTL_CD | ICTL_SEL | ICTL_ARBIT | fcb;
260 struct stg_softc *sc;
262 bus_space_tag_t iot = sc->sc_iot;
263 bus_space_handle_t ioh = sc->sc_ioh;
265 bus_space_write_1(iot, ioh, tmc_ictl, 0);
266 stghw_bcr_write_1(sc, BCTL_BUSFREE);
267 bus_space_write_1(iot, ioh, tmc_fctl,
268 sc->sc_fcRinit | FCTL_CLRFIFO | FCTL_CLRINT);
269 bus_space_write_1(iot, ioh, tmc_fctl, sc->sc_fcRinit);
270 bus_space_write_1(iot, ioh, tmc_ictl, sc->sc_icinit);
272 bus_space_write_1(iot, ioh, tmc_ssctl, 0);
276 stg_targ_init(sc, ti, action)
277 struct stg_softc *sc;
278 struct targ_info *ti;
281 struct stg_targ_info *sti = (void *) ti;
283 if (action == SCSI_LOW_INFO_ALLOC || action == SCSI_LOW_INFO_REVOKE)
285 ti->ti_width = SCSI_LOW_BUS_WIDTH_8;
286 ti->ti_maxsynch.period = 0;
287 ti->ti_maxsynch.offset = 0;
288 sti->sti_reg_synch = 0;
293 /****************************************************
295 ****************************************************/
298 struct stg_softc *sc;
301 sc->sc_busc |= BCTL_ATN;
302 sc->sc_busimg |= BCTL_ATN;
303 bus_space_write_1(sc->sc_iot, sc->sc_ioh, tmc_bctl, sc->sc_busimg);
309 struct stg_softc *sc;
311 bus_space_tag_t iot = sc->sc_iot;
312 bus_space_handle_t ioh = sc->sc_ioh;
314 bus_space_write_1(iot, ioh, tmc_ictl, 0);
315 bus_space_write_1(iot, ioh, tmc_fctl, 0);
316 stghw_bcr_write_1(sc, BCTL_RST);
317 SCSI_LOW_DELAY(100000);
318 stghw_bcr_write_1(sc, BCTL_BUSFREE);
322 stghw_start_selection(sc, cb)
323 struct stg_softc *sc;
326 bus_space_tag_t iot = sc->sc_iot;
327 bus_space_handle_t ioh = sc->sc_ioh;
328 struct targ_info *ti = cb->ti;
329 register u_int8_t stat;
332 sc->sc_tmaxcnt = cb->ccb_tcmax * 1000 * 1000;
333 sc->sc_dataout_timeout = 0;
334 sc->sc_ubf_timeout = 0;
335 stghw_bcr_write_1(sc, BCTL_BUSFREE);
336 bus_space_write_1(iot, ioh, tmc_ictl, sc->sc_icinit);
339 stat = bus_space_read_1(iot, ioh, tmc_astat);
340 if ((stat & ASTAT_INT) != 0)
343 return SCSI_LOW_START_FAIL;
346 bus_space_write_1(iot, ioh, tmc_scsiid, sc->sc_idbit);
347 bus_space_write_1(iot, ioh, tmc_fctl, sc->sc_fcRinit | FCTL_ARBIT);
350 SCSI_LOW_SETUP_PHASE(ti, PH_ARBSTART);
351 return SCSI_LOW_START_OK;
355 stg_world_start(sc, fdone)
356 struct stg_softc *sc;
359 struct scsi_low_softc *slp = &sc->sc_sclow;
362 if ((slp->sl_cfgflags & CFG_NOPARITY) == 0)
363 sc->sc_fcRinit |= FCTL_PARENB;
365 sc->sc_fcRinit &= ~FCTL_PARENB;
367 if ((error = stghw_check(sc)) != 0)
371 scsi_low_bus_reset(slp);
374 SOFT_INTR_REQUIRED(slp);
380 struct stg_softc *sc;
381 struct targ_info *ti;
384 bus_space_tag_t iot = sc->sc_iot;
385 bus_space_handle_t ioh = sc->sc_ioh;
386 struct stg_targ_info *sti = (void *) ti;
387 u_int period, offset;
389 if ((msg & SCSI_LOW_MSG_WIDE) != 0)
391 if (ti->ti_width != SCSI_LOW_BUS_WIDTH_8)
393 ti->ti_width = SCSI_LOW_BUS_WIDTH_8;
399 if ((msg & SCSI_LOW_MSG_SYNCH) == 0)
402 period = ti->ti_maxsynch.period;
403 offset = ti->ti_maxsynch.offset;
404 period = period << 2;
407 sti->sti_reg_synch = (period - 200) / 50;
409 sti->sti_reg_synch ++;
410 sti->sti_reg_synch |= SSCTL_SYNCHEN;
412 else if (period >= 100)
414 sti->sti_reg_synch = (period - 100) / 50;
416 sti->sti_reg_synch ++;
417 sti->sti_reg_synch |= SSCTL_SYNCHEN | SSCTL_FSYNCHEN;
419 bus_space_write_1(iot, ioh, tmc_ssctl, sti->sti_reg_synch);
423 /**************************************************************
424 * General probe attach
425 **************************************************************/
427 stgprobesubr(iot, ioh, dvcfg)
429 bus_space_handle_t ioh;
434 lsb = bus_space_read_1(iot, ioh, tmc_idlsb);
435 msb = bus_space_read_1(iot, ioh, tmc_idmsb);
436 switch (msb << 8 | lsb)
456 printf("%s: scsibus ", name);
462 struct stg_softc *sc;
464 struct scsi_low_softc *slp = &sc->sc_sclow;
468 sc->sc_idbit = (1 << slp->sl_hostid);
469 slp->sl_funcs = &stgfuncs;
470 sc->sc_tmaxcnt = SCSI_LOW_MIN_TOUT * 1000 * 1000; /* default */
472 slp->sl_flags |= HW_READ_PADDING;
473 slp->sl_cfgflags |= CFG_ASYNC; /* XXX */
475 (void) scsi_low_attach(slp, 0, STG_NTARGETS, STG_NLUNS,
476 sizeof(struct stg_targ_info), 0);
479 /**************************************************************
481 **************************************************************/
484 struct stg_softc *sc;
485 struct targ_info *ti;
487 struct scsi_low_softc *slp = &sc->sc_sclow;
488 bus_space_tag_t iot = sc->sc_iot;
489 bus_space_handle_t ioh = sc->sc_ioh;
490 struct slccb *cb = slp->sl_Qnexus;
493 slp->sl_flags &= ~HW_PDMASTART;
494 sc->sc_icinit &= ~ICTL_FIFO;
495 sc->sc_dataout_timeout = 0;
499 slp->sl_error |= PDMAERR;
503 if (ti->ti_phase == PH_DATA)
505 len = bus_space_read_2(iot, ioh, tmc_fdcnt);
506 if (slp->sl_scp.scp_direction == SCSI_LOW_WRITE)
510 tres = len + slp->sl_scp.scp_datalen;
511 if (tres <= (u_int) cb->ccb_scp.scp_datalen)
513 slp->sl_scp.scp_data -= len;
514 slp->sl_scp.scp_datalen = tres;
518 slp->sl_error |= PDMAERR;
519 printf("%s len %x >= datalen %x\n",
521 len, slp->sl_scp.scp_datalen);
525 else if (slp->sl_scp.scp_direction == SCSI_LOW_READ)
529 slp->sl_error |= PDMAERR;
530 printf("%s: len %x left in fifo\n",
534 scsi_low_data_finish(slp);
539 printf("%s data phase miss\n", slp->sl_xname);
540 slp->sl_error |= PDMAERR;
544 bus_space_write_1(iot, ioh, tmc_fctl, sc->sc_fcRinit);
548 stg_pio_read(sc, ti, thold)
549 struct stg_softc *sc;
550 struct targ_info *ti;
553 struct scsi_low_softc *slp = &sc->sc_sclow;
554 bus_space_tag_t iot = sc->sc_iot;
555 bus_space_handle_t ioh = sc->sc_ioh;
556 struct sc_p *sp = &slp->sl_scp;
561 if ((slp->sl_flags & HW_PDMASTART) == 0)
563 bus_space_write_1(iot, ioh, tmc_fctl,
564 sc->sc_fcRinit | FCTL_FIFOEN);
565 slp->sl_flags |= HW_PDMASTART;
568 tout = sc->sc_tmaxcnt;
574 res = bus_space_read_2(iot, ioh, tmc_fdcnt);
577 bus_space_write_1(iot, ioh, tmc_ictl,
586 stat = bus_space_read_1(iot, ioh, tmc_bstat);
587 res = bus_space_read_2(iot, ioh, tmc_fdcnt);
590 if ((stat & PHASE_MASK) != DATA_IN_PHASE)
592 if (sp->scp_datalen <= 0)
599 /* The assumtion res != 0 is valid here */
600 if (res > sp->scp_datalen)
602 if (res == (u_int) -1)
605 slp->sl_error |= PDMAERR;
606 if ((slp->sl_flags & HW_READ_PADDING) == 0)
608 printf("%s: read padding required\n",
614 if (res > STG_MAX_DATA_SIZE)
615 res = STG_MAX_DATA_SIZE;
618 (void) bus_space_read_1(iot, ioh, tmc_rfifo);
623 sp->scp_datalen -= res;
626 *sp->scp_data = bus_space_read_1(iot, ioh, tmc_rfifo);
631 bus_space_read_multi_2(iot, ioh, tmc_rfifo,
632 (u_int16_t *) sp->scp_data, res >> 1);
637 printf("%s: pio read timeout\n", slp->sl_xname);
641 stg_pio_write(sc, ti, thold)
642 struct stg_softc *sc;
643 struct targ_info *ti;
646 struct scsi_low_softc *slp = &sc->sc_sclow;
647 bus_space_tag_t iot = sc->sc_iot;
648 bus_space_handle_t ioh = sc->sc_ioh;
649 struct sc_p *sp = &slp->sl_scp;
652 register u_int8_t stat;
654 if ((slp->sl_flags & HW_PDMASTART) == 0)
656 stat = sc->sc_fcWinit | FCTL_FIFOEN | FCTL_FIFOW;
657 bus_space_write_1(iot, ioh, tmc_fctl, stat | FCTL_CLRFIFO);
658 bus_space_write_1(iot, ioh, tmc_fctl, stat);
659 slp->sl_flags |= HW_PDMASTART;
662 tout = sc->sc_tmaxcnt;
665 stat = bus_space_read_1(iot, ioh, tmc_bstat);
666 if ((stat & PHASE_MASK) != DATA_OUT_PHASE)
669 if (sp->scp_datalen <= 0)
671 if (sc->sc_dataout_timeout == 0)
672 sc->sc_dataout_timeout = SCSI_LOW_TIMEOUT_HZ;
679 res = bus_space_read_2(iot, ioh, tmc_fdcnt);
682 bus_space_write_1(iot, ioh, tmc_ictl,
691 res = bus_space_read_2(iot, ioh, tmc_fdcnt);
692 if (res > sc->sc_maxwsize / 2)
699 if (res == (u_int) -1)
701 res = sc->sc_maxwsize - res;
702 if (res > sp->scp_datalen)
703 res = sp->scp_datalen;
705 sp->scp_datalen -= res;
706 if ((res & 0x1) != 0)
708 bus_space_write_1(iot, ioh, tmc_wfifo, *sp->scp_data);
713 bus_space_write_multi_2(iot, ioh, tmc_wfifo,
714 (u_int16_t *) sp->scp_data, res >> 1);
719 printf("%s: pio write timeout\n", slp->sl_xname);
723 stg_negate_signal(struct stg_softc *sc, u_int8_t mask, u_char *s)
725 struct scsi_low_softc *slp = &sc->sc_sclow;
726 bus_space_tag_t bst = sc->sc_iot;
727 bus_space_handle_t bsh = sc->sc_ioh;
731 for (wc = 0; wc < STG_DELAY_MAX / STG_DELAY_INTERVAL; wc ++)
733 regv = bus_space_read_1(bst, bsh, tmc_bstat);
734 if (regv == (u_int8_t) -1)
736 if ((regv & mask) == 0)
739 SCSI_LOW_DELAY(STG_DELAY_INTERVAL);
742 printf("%s: %s stg_negate_signal timeout\n", slp->sl_xname, s);
747 stg_expect_signal(struct stg_softc *sc, u_int8_t phase, u_int8_t mask)
749 struct scsi_low_softc *slp = &sc->sc_sclow;
750 bus_space_tag_t bst = sc->sc_iot;
751 bus_space_handle_t bsh = sc->sc_ioh;
756 for (wc = 0; wc < STG_DELAY_MAX / STG_DELAY_INTERVAL; wc ++)
758 ph = bus_space_read_1(bst, bsh, tmc_bstat);
759 if (ph == (u_int8_t) -1)
761 if ((ph & PHASE_MASK) != phase)
763 if ((ph & mask) != 0)
766 SCSI_LOW_DELAY(STG_DELAY_INTERVAL);
769 printf("%s: stg_expect_signal timeout\n", slp->sl_xname);
774 stg_xfer(sc, buf, len, phase, clear_atn)
775 struct stg_softc *sc;
781 bus_space_tag_t iot = sc->sc_iot;
782 bus_space_handle_t ioh = sc->sc_ioh;
785 if (phase & BSTAT_IO)
786 bus_space_write_1(iot, ioh, tmc_fctl, sc->sc_fcRinit);
788 bus_space_write_1(iot, ioh, tmc_fctl, sc->sc_fcWinit);
790 for (ptr = 0; len > 0; len --)
792 rv = stg_expect_signal(sc, phase, BSTAT_REQ);
796 if (len == 1 && clear_atn != 0)
798 sc->sc_busc &= ~BCTL_ATN;
799 stghw_bcr_write_1(sc, sc->sc_busc);
800 SCSI_LOW_DEASSERT_ATN(&sc->sc_sclow);
803 if (phase & BSTAT_IO)
805 buf[ptr ++] = bus_space_read_1(iot, ioh, tmc_rdata);
809 bus_space_write_1(iot, ioh, tmc_wdata, buf[ptr ++]);
812 stg_negate_signal(sc, BSTAT_ACK, "xfer<ACK>");
816 bus_space_write_1(iot, ioh, tmc_fctl, sc->sc_fcRinit);
820 /**************************************************************
821 * disconnect & reselect (HW low)
822 **************************************************************/
825 struct stg_softc *sc;
827 struct scsi_low_softc *slp = &sc->sc_sclow;
828 bus_space_tag_t iot = sc->sc_iot;
829 bus_space_handle_t ioh = sc->sc_ioh;
834 if (slp->sl_selid != NULL)
837 * Selection vs Reselection conflicts.
839 bus_space_write_1(iot, ioh, tmc_fctl, sc->sc_fcRinit);
840 stghw_bcr_write_1(sc, BCTL_BUSFREE);
842 else if (slp->sl_Tnexus != NULL)
844 printf("%s: unexpected termination\n", slp->sl_xname);
845 stg_disconnected(sc, slp->sl_Tnexus);
849 * We should ack the reselection as soon as possible,
850 * because the target would abort the current reselection seq
851 * due to reselection timeout.
853 tout = STG_DELAY_SELECT_POLLING_MAX;
856 regv = bus_space_read_1(iot, ioh, tmc_bstat);
857 if ((regv & (BSTAT_IO | BSTAT_SEL | BSTAT_BSY)) ==
858 (BSTAT_IO | BSTAT_SEL))
861 regv = bus_space_read_1(iot, ioh, tmc_bstat);
862 if ((regv & (BSTAT_IO | BSTAT_SEL | BSTAT_BSY)) ==
863 (BSTAT_IO | BSTAT_SEL))
868 printf("%s: reselction timeout I\n", slp->sl_xname);
872 sid = (u_int) bus_space_read_1(iot, ioh, tmc_scsiid);
873 if ((sid & sc->sc_idbit) == 0)
879 bus_space_write_1(iot, ioh, tmc_fctl,
880 sc->sc_fcRinit | FCTL_CLRFIFO | FCTL_CLRINT);
881 bus_space_write_1(iot, ioh, tmc_fctl, sc->sc_fcRinit);
882 stghw_bcr_write_1(sc, sc->sc_busc | BCTL_BSY);
886 regv = bus_space_read_1(iot, ioh, tmc_bstat);
887 if ((regv & (BSTAT_SEL | BSTAT_BSY)) == BSTAT_BSY)
891 printf("%s: reselction timeout II\n", slp->sl_xname);
895 sid &= ~sc->sc_idbit;
897 if (scsi_low_reselected(slp, sid) == NULL)
901 stg_statics.reselect ++;
902 #endif /* STG_STATICS */
907 stg_disconnected(sc, ti)
908 struct stg_softc *sc;
909 struct targ_info *ti;
911 struct scsi_low_softc *slp = &sc->sc_sclow;
912 bus_space_tag_t iot = sc->sc_iot;
913 bus_space_handle_t ioh = sc->sc_ioh;
915 /* clear bus status & fifo */
916 bus_space_write_1(iot, ioh, tmc_fctl, sc->sc_fcRinit | FCTL_CLRFIFO);
917 bus_space_write_1(iot, ioh, tmc_fctl, sc->sc_fcRinit);
918 stghw_bcr_write_1(sc, BCTL_BUSFREE);
919 sc->sc_icinit &= ~ICTL_FIFO;
920 sc->sc_busc &= ~BCTL_ATN;
921 sc->sc_dataout_timeout = 0;
922 sc->sc_ubf_timeout = 0;
925 stg_statics.disconnect ++;
926 #endif /* STG_STATICS */
927 scsi_low_disconnected(slp, ti);
931 /**************************************************************
933 **************************************************************/
935 stg_target_nexus_establish(sc)
936 struct stg_softc *sc;
938 struct scsi_low_softc *slp = &sc->sc_sclow;
939 bus_space_tag_t iot = sc->sc_iot;
940 bus_space_handle_t ioh = sc->sc_ioh;
941 struct targ_info *ti = slp->sl_Tnexus;
942 struct stg_targ_info *sti = (void *) ti;
944 bus_space_write_1(iot, ioh, tmc_ssctl, sti->sti_reg_synch);
945 if ((stg_io_control & STG_FIFO_INTERRUPTS) != 0)
947 sc->sc_icinit |= ICTL_FIFO;
953 stg_lun_nexus_establish(sc)
954 struct stg_softc *sc;
961 stg_ccb_nexus_establish(sc)
962 struct stg_softc *sc;
964 struct scsi_low_softc *slp = &sc->sc_sclow;
965 struct slccb *cb = slp->sl_Qnexus;
967 sc->sc_tmaxcnt = cb->ccb_tcmax * 1000 * 1000;
971 #define STGHW_SELECT_INTERVAL 10
974 stghw_select_targ_wait(sc, mu)
975 struct stg_softc *sc;
978 bus_space_tag_t iot = sc->sc_iot;
979 bus_space_handle_t ioh = sc->sc_ioh;
981 mu = mu / STGHW_SELECT_INTERVAL;
984 if ((bus_space_read_1(iot, ioh, tmc_bstat) & BSTAT_BSY) == 0)
986 SCSI_LOW_DELAY(STGHW_SELECT_INTERVAL);
990 if ((bus_space_read_1(iot, ioh, tmc_bstat) & BSTAT_BSY) != 0)
999 stg_selection_done_and_expect_msgout(sc)
1000 struct stg_softc *sc;
1002 struct scsi_low_softc *slp = &sc->sc_sclow;
1003 bus_space_tag_t iot = sc->sc_iot;
1004 bus_space_handle_t ioh = sc->sc_ioh;
1006 bus_space_write_1(iot, ioh, tmc_fctl, sc->sc_fcRinit | FCTL_CLRFIFO);
1007 bus_space_write_1(iot, ioh, tmc_fctl, sc->sc_fcRinit);
1008 stghw_bcr_write_1(sc, sc->sc_imsg | sc->sc_busc);
1009 SCSI_LOW_ASSERT_ATN(slp);
1016 struct stg_softc *sc = arg;
1017 struct scsi_low_softc *slp = &sc->sc_sclow;
1018 bus_space_tag_t iot = sc->sc_iot;
1019 bus_space_handle_t ioh = sc->sc_ioh;
1020 struct targ_info *ti;
1021 struct physio_proc *pp;
1023 u_int derror, flags;
1025 u_int8_t status, astatus, regv;
1027 /*******************************************
1029 *******************************************/
1030 if (slp->sl_flags & HW_INACTIVE)
1033 astatus = bus_space_read_1(iot, ioh, tmc_astat);
1034 status = bus_space_read_1(iot, ioh, tmc_bstat);
1036 if ((astatus & ASTAT_STATMASK) == 0 || astatus == (u_int8_t) -1)
1039 bus_space_write_1(iot, ioh, tmc_ictl, 0);
1040 if (astatus & ASTAT_SCSIRST)
1042 bus_space_write_1(iot, ioh, tmc_fctl,
1043 sc->sc_fcRinit | FCTL_CLRFIFO);
1044 bus_space_write_1(iot, ioh, tmc_fctl, sc->sc_fcRinit);
1045 bus_space_write_1(iot, ioh, tmc_ictl, 0);
1047 scsi_low_restart(slp, SCSI_LOW_RESTART_SOFT,
1048 "bus reset (power off?)");
1052 /*******************************************
1054 *******************************************/
1058 scsi_low_print(slp, NULL);
1059 printf("%s: st %x ist %x\n\n", slp->sl_xname,
1063 SCSI_LOW_DEBUGGER("stg");
1066 #endif /* STG_DEBUG */
1068 /*******************************************
1069 * reselection & nexus
1070 *******************************************/
1071 if ((status & RESEL_PHASE_MASK)== PHASE_RESELECTED)
1073 if (stg_reselected(sc) == EJUSTRETURN)
1077 if ((ti = slp->sl_Tnexus) == NULL)
1081 if ((astatus & ASTAT_PARERR) != 0 && ti->ti_phase != PH_ARBSTART &&
1082 (sc->sc_fcRinit & FCTL_PARENB) != 0)
1084 slp->sl_error |= PARITYERR;
1085 derror = SCSI_LOW_DATA_PE;
1086 if ((status & PHASE_MASK) == MESSAGE_IN_PHASE)
1087 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_PARITY, 0);
1089 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_ERROR, 1);
1092 /*******************************************
1093 * aribitration & selection
1094 *******************************************/
1095 switch (ti->ti_phase)
1098 if ((astatus & ASTAT_ARBIT) == 0)
1101 stg_statics.arbit_fail_0 ++;
1102 #endif /* STG_STATICS */
1106 status = bus_space_read_1(iot, ioh, tmc_bstat);
1107 if ((status & BSTAT_IO) != 0)
1110 * Selection vs Reselection conflicts.
1113 stg_statics.arbit_fail_1 ++;
1114 #endif /* STG_STATICS */
1116 bus_space_write_1(iot, ioh, tmc_fctl, sc->sc_fcRinit);
1117 stghw_bcr_write_1(sc, BCTL_BUSFREE);
1118 scsi_low_arbit_fail(slp, slp->sl_Qnexus);
1123 * selection assert start.
1125 SCSI_LOW_SETUP_PHASE(ti, PH_SELSTART);
1126 scsi_low_arbit_win(slp);
1129 bus_space_write_1(iot, ioh, tmc_scsiid,
1130 sc->sc_idbit | (1 << ti->ti_id));
1131 stghw_bcr_write_1(sc, sc->sc_imsg | sc->sc_busc | BCTL_SEL);
1132 bus_space_write_1(iot, ioh, tmc_fctl, sc->sc_fcWinit);
1133 if ((stg_io_control & STG_WAIT_FOR_SELECT) != 0)
1135 /* selection abort delay 200 + 100 micro sec */
1136 if (stghw_select_targ_wait(sc, 300) == 0)
1138 SCSI_LOW_SETUP_PHASE(ti, PH_SELECTED);
1139 stg_selection_done_and_expect_msgout(sc);
1146 if ((status & BSTAT_BSY) == 0)
1148 /* selection timeout delay 250 ms */
1149 if (stghw_select_targ_wait(sc, 250 * 1000) != 0)
1151 stg_disconnected(sc, ti);
1156 SCSI_LOW_SETUP_PHASE(ti, PH_SELECTED);
1157 stg_selection_done_and_expect_msgout(sc);
1161 if ((status & BSTAT_REQ) == 0)
1163 stg_target_nexus_establish(sc);
1167 if ((status & BSTAT_REQ) == 0)
1170 /* clear a busy line */
1171 bus_space_write_1(iot, ioh, tmc_fctl, sc->sc_fcRinit);
1172 stghw_bcr_write_1(sc, sc->sc_busc);
1173 stg_target_nexus_establish(sc);
1174 if ((status & PHASE_MASK) != MESSAGE_IN_PHASE)
1176 printf("%s: unexpected phase after reselect\n",
1178 slp->sl_error |= FATALIO;
1179 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_ABORT, 1);
1185 /*******************************************
1187 *******************************************/
1188 if ((slp->sl_flags & HW_PDMASTART) && STG_IS_PHASE_DATA(status) == 0)
1190 if (slp->sl_scp.scp_direction == SCSI_LOW_READ)
1191 stg_pio_read(sc, ti, 0);
1193 stg_pdma_end(sc, ti);
1196 /*******************************************
1198 *******************************************/
1199 switch (status & PHASE_MASK)
1202 if (stg_expect_signal(sc, COMMAND_PHASE, BSTAT_REQ) <= 0)
1205 SCSI_LOW_SETUP_PHASE(ti, PH_CMD);
1206 if (scsi_low_cmd(slp, ti) != 0)
1208 scsi_low_attention(slp);
1211 if (stg_xfer(sc, slp->sl_scp.scp_cmd, slp->sl_scp.scp_cmdlen,
1212 COMMAND_PHASE, 0) != 0)
1214 printf("%s: CMDOUT short\n", slp->sl_xname);
1218 case DATA_OUT_PHASE:
1219 SCSI_LOW_SETUP_PHASE(ti, PH_DATA);
1220 if (scsi_low_data(slp, ti, &bp, SCSI_LOW_WRITE) != 0)
1222 scsi_low_attention(slp);
1225 pp = physio_proc_enter(bp);
1226 if ((sc->sc_icinit & ICTL_FIFO) != 0)
1227 stg_pio_write(sc, ti, sc->sc_wthold);
1229 stg_pio_write(sc, ti, 0);
1230 physio_proc_leave(pp);
1234 SCSI_LOW_SETUP_PHASE(ti, PH_DATA);
1235 if (scsi_low_data(slp, ti, &bp, SCSI_LOW_READ) != 0)
1237 scsi_low_attention(slp);
1240 pp = physio_proc_enter(bp);
1241 if ((sc->sc_icinit & ICTL_FIFO) != 0)
1242 stg_pio_read(sc, ti, sc->sc_rthold);
1244 stg_pio_read(sc, ti, 0);
1245 physio_proc_leave(pp);
1249 regv = stg_expect_signal(sc, STATUS_PHASE, BSTAT_REQ);
1253 SCSI_LOW_SETUP_PHASE(ti, PH_STAT);
1254 regv = bus_space_read_1(iot, ioh, tmc_sdna);
1255 if (scsi_low_statusin(slp, ti, regv | derror) != 0)
1257 scsi_low_attention(slp);
1259 if (regv != bus_space_read_1(iot, ioh, tmc_rdata))
1261 printf("%s: STATIN: data mismatch\n", slp->sl_xname);
1263 stg_negate_signal(sc, BSTAT_ACK, "statin<ACK>");
1266 case MESSAGE_OUT_PHASE:
1267 if (stg_expect_signal(sc, MESSAGE_OUT_PHASE, BSTAT_REQ) <= 0)
1270 SCSI_LOW_SETUP_PHASE(ti, PH_MSGOUT);
1271 flags = (ti->ti_ophase != ti->ti_phase) ?
1272 SCSI_LOW_MSGOUT_INIT : 0;
1273 len = scsi_low_msgout(slp, ti, flags);
1275 if (len > 1 && slp->sl_atten == 0)
1277 scsi_low_attention(slp);
1280 if (stg_xfer(sc, ti->ti_msgoutstr, len, MESSAGE_OUT_PHASE,
1281 slp->sl_clear_atten) != 0)
1283 printf("%s: MSGOUT short\n", slp->sl_xname);
1287 if (slp->sl_msgphase >= MSGPH_ABORT)
1289 stg_disconnected(sc, ti);
1294 case MESSAGE_IN_PHASE:
1295 /* confirm phase and req signal */
1296 if (stg_expect_signal(sc, MESSAGE_IN_PHASE, BSTAT_REQ) <= 0)
1299 SCSI_LOW_SETUP_PHASE(ti, PH_MSGIN);
1301 /* read data with NOACK */
1302 regv = bus_space_read_1(iot, ioh, tmc_sdna);
1304 if (scsi_low_msgin(slp, ti, derror | regv) == 0)
1306 if (scsi_low_is_msgout_continue(ti, 0) != 0)
1308 scsi_low_attention(slp);
1312 /* read data with ACK */
1313 if (regv != bus_space_read_1(iot, ioh, tmc_rdata))
1315 printf("%s: MSGIN: data mismatch\n", slp->sl_xname);
1318 /* wait for the ack negated */
1319 stg_negate_signal(sc, BSTAT_ACK, "msgin<ACK>");
1321 if (slp->sl_msgphase != 0 && slp->sl_msgphase < MSGPH_ABORT)
1323 stg_disconnected(sc, ti);
1328 printf("%s: unexpected disconnect\n", slp->sl_xname);
1329 stg_disconnected(sc, ti);
1333 slp->sl_error |= FATALIO;
1334 printf("%s: unknown phase bus %x intr %x\n",
1335 slp->sl_xname, status, astatus);
1340 bus_space_write_1(iot, ioh, tmc_ictl, sc->sc_icinit);
1346 struct stg_softc *sc;
1348 struct scsi_low_softc *slp = &sc->sc_sclow;
1349 bus_space_tag_t iot = sc->sc_iot;
1350 bus_space_handle_t ioh = sc->sc_ioh;
1354 if (slp->sl_Tnexus == NULL)
1357 status = bus_space_read_1(iot, ioh, tmc_bstat);
1358 if ((status & PHASE_MASK) == 0)
1360 if (sc->sc_ubf_timeout ++ == 0)
1363 printf("%s: unexpected bus free detected\n", slp->sl_xname);
1364 slp->sl_error |= FATALIO;
1365 scsi_low_print(slp, slp->sl_Tnexus);
1366 stg_disconnected(sc, slp->sl_Tnexus);
1370 switch (status & PHASE_MASK)
1372 case DATA_OUT_PHASE:
1373 if (sc->sc_dataout_timeout == 0)
1375 if ((status & BSTAT_REQ) == 0)
1377 if (bus_space_read_2(iot, ioh, tmc_fdcnt) != 0)
1379 if ((-- sc->sc_dataout_timeout) > 0)
1382 slp->sl_error |= PDMAERR;
1383 if ((slp->sl_flags & HW_WRITE_PADDING) == 0)
1385 printf("%s: write padding required\n",
1390 bus_space_write_1(iot, ioh, tmc_ictl, 0);
1392 tout = STG_DELAY_MAX;
1395 status = bus_space_read_1(iot, ioh, tmc_bstat);
1396 if ((status & PHASE_MASK) != DATA_OUT_PHASE)
1399 if (bus_space_read_2(iot, ioh, tmc_fdcnt) != 0)
1405 for (count = sc->sc_maxwsize; count > 0; count --)
1406 bus_space_write_1(iot, ioh, tmc_wfifo, 0);
1409 status = bus_space_read_1(iot, ioh, tmc_bstat);
1410 if ((status & PHASE_MASK) == DATA_OUT_PHASE)
1411 sc->sc_dataout_timeout = SCSI_LOW_TIMEOUT_HZ;
1413 bus_space_write_1(iot, ioh, tmc_ictl, sc->sc_icinit);