1 /* $FreeBSD: src/sys/dev/stg/tmc18c30.c,v 1.1.2.5 2001/12/17 13:30:19 non Exp $ */
2 /* $NecBSD: tmc18c30.c,v 1.28.12.3 2001/06/19 04:35:48 honda Exp $ */
7 #define STG_IO_CONTROL_FLAGS (STG_FIFO_INTERRUPTS | STG_WAIT_FOR_SELECT)
10 * [NetBSD for NEC PC-98 series]
11 * Copyright (c) 1996, 1997, 1998, 1999, 2000, 2001
12 * NetBSD/pc98 porting staff. All rights reserved.
13 * Copyright (c) 1996, 1997, 1998, 1999, 2000, 2001
14 * Naofumi HONDA. All rights reserved.
15 * Copyright (c) 1996, 1997, 1998, 1999
16 * Kouichi Matsuda. All rights reserved.
18 * Redistribution and use in source and binary forms, with or without
19 * modification, are permitted provided that the following conditions
21 * 1. Redistributions of source code must retain the above copyright
22 * notice, this list of conditions and the following disclaimer.
23 * 2. Redistributions in binary form must reproduce the above copyright
24 * notice, this list of conditions and the following disclaimer in the
25 * documentation and/or other materials provided with the distribution.
26 * 3. The name of the author may not be used to endorse or promote products
27 * derived from this software without specific prior written permission.
29 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
30 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
31 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
32 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
33 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
34 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
35 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
37 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
38 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
39 * POSSIBILITY OF SUCH DAMAGE.
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/kernel.h>
46 #if defined(__FreeBSD__) && __FreeBSD_version >= 500001
48 #endif /* __FreeBSD__ */
50 #include <sys/queue.h>
51 #include <sys/malloc.h>
52 #include <sys/errno.h>
55 #include <sys/device.h>
56 #include <machine/bus.h>
57 #include <machine/intr.h>
59 #include <dev/scsipi/scsi_all.h>
60 #include <dev/scsipi/scsipi_all.h>
61 #include <dev/scsipi/scsiconf.h>
62 #include <dev/scsipi/scsi_disk.h>
64 #include <machine/dvcfg.h>
65 #include <machine/physio_proc.h>
67 #include <i386/Cbus/dev/scsi_low.h>
68 #include <i386/Cbus/dev/tmc18c30reg.h>
69 #include <i386/Cbus/dev/tmc18c30var.h>
70 #endif /* __NetBSD__ */
73 #include <machine/clock.h>
74 #include <machine/cpu.h>
75 #include <machine/bus_pio.h>
76 #include <machine/bus.h>
78 #include <machine/dvcfg.h>
79 #include <machine/physio_proc.h>
81 #include <cam/scsi/scsi_low.h>
82 #include <dev/stg/tmc18c30reg.h>
83 #include <dev/stg/tmc18c30var.h>
84 #endif /* __FreeBSD__ */
86 /***************************************************
88 ***************************************************/
89 /* DEVICE CONFIGURATION FLAGS (MINOR)
92 * 0x02 PARITY LINE OFF
93 * 0x04 IDENTIFY MSG OFF ( = single lun)
94 * 0x08 SYNC TRANSFER OFF
96 /* #define STG_SYNC_SUPPORT */ /* NOT YET but easy */
98 /* For the 512 fifo type: change below */
99 #define TMC18C30_FIFOSZ 0x800
100 #define TMC18C30_FCBSZ 0x200
101 #define TMC18C50_FIFOSZ 0x2000
102 #define TMC18C50_FCBSZ 0x400
104 #define STG_MAX_DATA_SIZE (64 * 1024)
105 #define STG_DELAY_MAX (2 * 1000 * 1000)
106 #define STG_DELAY_INTERVAL (1)
107 #define STG_DELAY_SELECT_POLLING_MAX (5 * 1000 * 1000)
109 /***************************************************
111 ***************************************************/
112 #define STG_NTARGETS 8
115 /***************************************************
117 ***************************************************/
120 #endif /* STG_DEBUG */
129 #endif /* STG_STATICS */
131 /***************************************************
133 ***************************************************/
134 #define STG_FIFO_INTERRUPTS 0x0001
135 #define STG_WAIT_FOR_SELECT 0x0100
137 int stg_io_control = STG_IO_CONTROL_FLAGS;
139 /***************************************************
141 ***************************************************/
142 extern struct cfdriver stg_cd;
144 /**************************************************************
146 **************************************************************/
148 static void stg_pio_read __P((struct stg_softc *, struct targ_info *, u_int));
149 static void stg_pio_write __P((struct stg_softc *, struct targ_info *, u_int));
150 static int stg_xfer __P((struct stg_softc *, u_int8_t *, int, int, int));
151 static int stg_msg __P((struct stg_softc *, struct targ_info *, u_int));
152 static int stg_reselected __P((struct stg_softc *));
153 static int stg_disconnected __P((struct stg_softc *, struct targ_info *));
154 static __inline void stg_pdma_end __P((struct stg_softc *, struct targ_info *));
155 static int stghw_select_targ_wait __P((struct stg_softc *, int));
156 static int stghw_check __P((struct stg_softc *));
157 static void stghw_init __P((struct stg_softc *));
158 static int stg_negate_signal __P((struct stg_softc *, u_int8_t, u_char *));
159 static int stg_expect_signal __P((struct stg_softc *, u_int8_t, u_int8_t));
160 static int stg_world_start __P((struct stg_softc *, int));
161 static int stghw_start_selection __P((struct stg_softc *sc, struct slccb *));
162 static void stghw_bus_reset __P((struct stg_softc *));
163 static void stghw_attention __P((struct stg_softc *));
164 static int stg_target_nexus_establish __P((struct stg_softc *));
165 static int stg_lun_nexus_establish __P((struct stg_softc *));
166 static int stg_ccb_nexus_establish __P((struct stg_softc *));
167 static int stg_targ_init __P((struct stg_softc *, struct targ_info *, int));
168 static __inline void stghw_bcr_write_1 __P((struct stg_softc *, u_int8_t));
169 static int stg_timeout __P((struct stg_softc *));
170 static void stg_selection_done_and_expect_msgout __P((struct stg_softc *));
172 struct scsi_low_funcs stgfuncs = {
173 SC_LOW_INIT_T stg_world_start,
174 SC_LOW_BUSRST_T stghw_bus_reset,
175 SC_LOW_TARG_INIT_T stg_targ_init,
176 SC_LOW_LUN_INIT_T NULL,
178 SC_LOW_SELECT_T stghw_start_selection,
179 SC_LOW_NEXUS_T stg_lun_nexus_establish,
180 SC_LOW_NEXUS_T stg_ccb_nexus_establish,
182 SC_LOW_ATTEN_T stghw_attention,
183 SC_LOW_MSG_T stg_msg,
185 SC_LOW_TIMEOUT_T stg_timeout,
186 SC_LOW_POLL_T stgintr,
191 /****************************************************
193 ****************************************************/
195 stghw_bcr_write_1(sc, bcv)
196 struct stg_softc *sc;
200 bus_space_write_1(sc->sc_iot, sc->sc_ioh, tmc_bctl, bcv);
206 struct stg_softc *sc;
208 struct scsi_low_softc *slp = &sc->sc_sclow;
209 bus_space_tag_t iot = sc->sc_iot;
210 bus_space_handle_t ioh = sc->sc_ioh;
214 lsb = bus_space_read_1(iot, ioh, tmc_idlsb);
215 msb = bus_space_read_1(iot, ioh, tmc_idmsb);
216 switch (msb << 8 | lsb)
219 /* TMCCHIP_1800 not supported. (it's my policy) */
220 sc->sc_chip = TMCCHIP_1800;
224 if (bus_space_read_1(iot, ioh, tmc_cfg2) & 0x02)
226 sc->sc_chip = TMCCHIP_18C30;
227 sc->sc_fsz = TMC18C30_FIFOSZ;
228 fcbsize = TMC18C30_FCBSZ;
232 sc->sc_chip = TMCCHIP_18C50;
233 sc->sc_fsz = TMC18C50_FIFOSZ;
234 fcbsize = TMC18C50_FCBSZ;
239 sc->sc_chip = TMCCHIP_UNK;
243 sc->sc_fcRinit = FCTL_INTEN;
244 sc->sc_fcWinit = FCTL_PARENB | FCTL_INTEN;
246 if (slp->sl_cfgflags & CFG_NOATTEN)
249 sc->sc_imsg = BCTL_ATN;
250 sc->sc_busc = BCTL_BUSEN;
252 sc->sc_wthold = fcbsize + 256;
253 sc->sc_rthold = fcbsize - 256;
254 sc->sc_maxwsize = sc->sc_fsz;
256 fcb = fcbsize / (sc->sc_fsz / 16);
257 sc->sc_icinit = ICTL_CD | ICTL_SEL | ICTL_ARBIT | fcb;
263 struct stg_softc *sc;
265 bus_space_tag_t iot = sc->sc_iot;
266 bus_space_handle_t ioh = sc->sc_ioh;
268 bus_space_write_1(iot, ioh, tmc_ictl, 0);
269 stghw_bcr_write_1(sc, BCTL_BUSFREE);
270 bus_space_write_1(iot, ioh, tmc_fctl,
271 sc->sc_fcRinit | FCTL_CLRFIFO | FCTL_CLRINT);
272 bus_space_write_1(iot, ioh, tmc_fctl, sc->sc_fcRinit);
273 bus_space_write_1(iot, ioh, tmc_ictl, sc->sc_icinit);
275 bus_space_write_1(iot, ioh, tmc_ssctl, 0);
279 stg_targ_init(sc, ti, action)
280 struct stg_softc *sc;
281 struct targ_info *ti;
284 struct stg_targ_info *sti = (void *) ti;
286 if (action == SCSI_LOW_INFO_ALLOC || action == SCSI_LOW_INFO_REVOKE)
288 ti->ti_width = SCSI_LOW_BUS_WIDTH_8;
289 ti->ti_maxsynch.period = 0;
290 ti->ti_maxsynch.offset = 0;
291 sti->sti_reg_synch = 0;
296 /****************************************************
298 ****************************************************/
301 struct stg_softc *sc;
304 sc->sc_busc |= BCTL_ATN;
305 sc->sc_busimg |= BCTL_ATN;
306 bus_space_write_1(sc->sc_iot, sc->sc_ioh, tmc_bctl, sc->sc_busimg);
312 struct stg_softc *sc;
314 bus_space_tag_t iot = sc->sc_iot;
315 bus_space_handle_t ioh = sc->sc_ioh;
317 bus_space_write_1(iot, ioh, tmc_ictl, 0);
318 bus_space_write_1(iot, ioh, tmc_fctl, 0);
319 stghw_bcr_write_1(sc, BCTL_RST);
320 SCSI_LOW_DELAY(100000);
321 stghw_bcr_write_1(sc, BCTL_BUSFREE);
325 stghw_start_selection(sc, cb)
326 struct stg_softc *sc;
329 bus_space_tag_t iot = sc->sc_iot;
330 bus_space_handle_t ioh = sc->sc_ioh;
331 struct targ_info *ti = cb->ti;
332 register u_int8_t stat;
335 sc->sc_tmaxcnt = cb->ccb_tcmax * 1000 * 1000;
336 sc->sc_dataout_timeout = 0;
337 sc->sc_ubf_timeout = 0;
338 stghw_bcr_write_1(sc, BCTL_BUSFREE);
339 bus_space_write_1(iot, ioh, tmc_ictl, sc->sc_icinit);
342 stat = bus_space_read_1(iot, ioh, tmc_astat);
343 if ((stat & ASTAT_INT) != 0)
346 return SCSI_LOW_START_FAIL;
349 bus_space_write_1(iot, ioh, tmc_scsiid, sc->sc_idbit);
350 bus_space_write_1(iot, ioh, tmc_fctl, sc->sc_fcRinit | FCTL_ARBIT);
353 SCSI_LOW_SETUP_PHASE(ti, PH_ARBSTART);
354 return SCSI_LOW_START_OK;
358 stg_world_start(sc, fdone)
359 struct stg_softc *sc;
362 struct scsi_low_softc *slp = &sc->sc_sclow;
365 if ((slp->sl_cfgflags & CFG_NOPARITY) == 0)
366 sc->sc_fcRinit |= FCTL_PARENB;
368 sc->sc_fcRinit &= ~FCTL_PARENB;
370 if ((error = stghw_check(sc)) != 0)
374 scsi_low_bus_reset(slp);
377 SOFT_INTR_REQUIRED(slp);
383 struct stg_softc *sc;
384 struct targ_info *ti;
387 bus_space_tag_t iot = sc->sc_iot;
388 bus_space_handle_t ioh = sc->sc_ioh;
389 struct stg_targ_info *sti = (void *) ti;
390 u_int period, offset;
392 if ((msg & SCSI_LOW_MSG_WIDE) != 0)
394 if (ti->ti_width != SCSI_LOW_BUS_WIDTH_8)
396 ti->ti_width = SCSI_LOW_BUS_WIDTH_8;
402 if ((msg & SCSI_LOW_MSG_SYNCH) == 0)
405 period = ti->ti_maxsynch.period;
406 offset = ti->ti_maxsynch.offset;
407 period = period << 2;
410 sti->sti_reg_synch = (period - 200) / 50;
412 sti->sti_reg_synch ++;
413 sti->sti_reg_synch |= SSCTL_SYNCHEN;
415 else if (period >= 100)
417 sti->sti_reg_synch = (period - 100) / 50;
419 sti->sti_reg_synch ++;
420 sti->sti_reg_synch |= SSCTL_SYNCHEN | SSCTL_FSYNCHEN;
422 bus_space_write_1(iot, ioh, tmc_ssctl, sti->sti_reg_synch);
426 /**************************************************************
427 * General probe attach
428 **************************************************************/
430 stgprobesubr(iot, ioh, dvcfg)
432 bus_space_handle_t ioh;
437 lsb = bus_space_read_1(iot, ioh, tmc_idlsb);
438 msb = bus_space_read_1(iot, ioh, tmc_idmsb);
439 switch (msb << 8 | lsb)
459 printf("%s: scsibus ", name);
465 struct stg_softc *sc;
467 struct scsi_low_softc *slp = &sc->sc_sclow;
471 sc->sc_idbit = (1 << slp->sl_hostid);
472 slp->sl_funcs = &stgfuncs;
473 sc->sc_tmaxcnt = SCSI_LOW_MIN_TOUT * 1000 * 1000; /* default */
475 slp->sl_flags |= HW_READ_PADDING;
476 slp->sl_cfgflags |= CFG_ASYNC; /* XXX */
478 (void) scsi_low_attach(slp, 0, STG_NTARGETS, STG_NLUNS,
479 sizeof(struct stg_targ_info), 0);
482 /**************************************************************
484 **************************************************************/
487 struct stg_softc *sc;
488 struct targ_info *ti;
490 struct scsi_low_softc *slp = &sc->sc_sclow;
491 bus_space_tag_t iot = sc->sc_iot;
492 bus_space_handle_t ioh = sc->sc_ioh;
493 struct slccb *cb = slp->sl_Qnexus;
496 slp->sl_flags &= ~HW_PDMASTART;
497 sc->sc_icinit &= ~ICTL_FIFO;
498 sc->sc_dataout_timeout = 0;
502 slp->sl_error |= PDMAERR;
506 if (ti->ti_phase == PH_DATA)
508 len = bus_space_read_2(iot, ioh, tmc_fdcnt);
509 if (slp->sl_scp.scp_direction == SCSI_LOW_WRITE)
513 tres = len + slp->sl_scp.scp_datalen;
514 if (tres <= (u_int) cb->ccb_scp.scp_datalen)
516 slp->sl_scp.scp_data -= len;
517 slp->sl_scp.scp_datalen = tres;
521 slp->sl_error |= PDMAERR;
522 printf("%s len %x >= datalen %x\n",
524 len, slp->sl_scp.scp_datalen);
528 else if (slp->sl_scp.scp_direction == SCSI_LOW_READ)
532 slp->sl_error |= PDMAERR;
533 printf("%s: len %x left in fifo\n",
537 scsi_low_data_finish(slp);
542 printf("%s data phase miss\n", slp->sl_xname);
543 slp->sl_error |= PDMAERR;
547 bus_space_write_1(iot, ioh, tmc_fctl, sc->sc_fcRinit);
551 stg_pio_read(sc, ti, thold)
552 struct stg_softc *sc;
553 struct targ_info *ti;
556 struct scsi_low_softc *slp = &sc->sc_sclow;
557 bus_space_tag_t iot = sc->sc_iot;
558 bus_space_handle_t ioh = sc->sc_ioh;
559 struct sc_p *sp = &slp->sl_scp;
564 if ((slp->sl_flags & HW_PDMASTART) == 0)
566 bus_space_write_1(iot, ioh, tmc_fctl,
567 sc->sc_fcRinit | FCTL_FIFOEN);
568 slp->sl_flags |= HW_PDMASTART;
571 tout = sc->sc_tmaxcnt;
577 res = bus_space_read_2(iot, ioh, tmc_fdcnt);
580 bus_space_write_1(iot, ioh, tmc_ictl,
589 stat = bus_space_read_1(iot, ioh, tmc_bstat);
590 res = bus_space_read_2(iot, ioh, tmc_fdcnt);
593 if ((stat & PHASE_MASK) != DATA_IN_PHASE)
595 if (sp->scp_datalen <= 0)
602 /* The assumtion res != 0 is valid here */
603 if (res > sp->scp_datalen)
605 if (res == (u_int) -1)
608 slp->sl_error |= PDMAERR;
609 if ((slp->sl_flags & HW_READ_PADDING) == 0)
611 printf("%s: read padding required\n",
617 if (res > STG_MAX_DATA_SIZE)
618 res = STG_MAX_DATA_SIZE;
621 (void) bus_space_read_1(iot, ioh, tmc_rfifo);
626 sp->scp_datalen -= res;
629 *sp->scp_data = bus_space_read_1(iot, ioh, tmc_rfifo);
634 bus_space_read_multi_2(iot, ioh, tmc_rfifo,
635 (u_int16_t *) sp->scp_data, res >> 1);
640 printf("%s: pio read timeout\n", slp->sl_xname);
644 stg_pio_write(sc, ti, thold)
645 struct stg_softc *sc;
646 struct targ_info *ti;
649 struct scsi_low_softc *slp = &sc->sc_sclow;
650 bus_space_tag_t iot = sc->sc_iot;
651 bus_space_handle_t ioh = sc->sc_ioh;
652 struct sc_p *sp = &slp->sl_scp;
655 register u_int8_t stat;
657 if ((slp->sl_flags & HW_PDMASTART) == 0)
659 stat = sc->sc_fcWinit | FCTL_FIFOEN | FCTL_FIFOW;
660 bus_space_write_1(iot, ioh, tmc_fctl, stat | FCTL_CLRFIFO);
661 bus_space_write_1(iot, ioh, tmc_fctl, stat);
662 slp->sl_flags |= HW_PDMASTART;
665 tout = sc->sc_tmaxcnt;
668 stat = bus_space_read_1(iot, ioh, tmc_bstat);
669 if ((stat & PHASE_MASK) != DATA_OUT_PHASE)
672 if (sp->scp_datalen <= 0)
674 if (sc->sc_dataout_timeout == 0)
675 sc->sc_dataout_timeout = SCSI_LOW_TIMEOUT_HZ;
682 res = bus_space_read_2(iot, ioh, tmc_fdcnt);
685 bus_space_write_1(iot, ioh, tmc_ictl,
694 res = bus_space_read_2(iot, ioh, tmc_fdcnt);
695 if (res > sc->sc_maxwsize / 2)
702 if (res == (u_int) -1)
704 res = sc->sc_maxwsize - res;
705 if (res > sp->scp_datalen)
706 res = sp->scp_datalen;
708 sp->scp_datalen -= res;
709 if ((res & 0x1) != 0)
711 bus_space_write_1(iot, ioh, tmc_wfifo, *sp->scp_data);
716 bus_space_write_multi_2(iot, ioh, tmc_wfifo,
717 (u_int16_t *) sp->scp_data, res >> 1);
722 printf("%s: pio write timeout\n", slp->sl_xname);
726 stg_negate_signal(sc, mask, s)
727 struct stg_softc *sc;
731 struct scsi_low_softc *slp = &sc->sc_sclow;
732 bus_space_tag_t bst = sc->sc_iot;
733 bus_space_handle_t bsh = sc->sc_ioh;
737 for (wc = 0; wc < STG_DELAY_MAX / STG_DELAY_INTERVAL; wc ++)
739 regv = bus_space_read_1(bst, bsh, tmc_bstat);
740 if (regv == (u_int8_t) -1)
742 if ((regv & mask) == 0)
745 SCSI_LOW_DELAY(STG_DELAY_INTERVAL);
748 printf("%s: %s stg_negate_signal timeout\n", slp->sl_xname, s);
753 stg_expect_signal(sc, phase, mask)
754 struct stg_softc *sc;
755 u_int8_t phase, mask;
757 struct scsi_low_softc *slp = &sc->sc_sclow;
758 bus_space_tag_t bst = sc->sc_iot;
759 bus_space_handle_t bsh = sc->sc_ioh;
764 for (wc = 0; wc < STG_DELAY_MAX / STG_DELAY_INTERVAL; wc ++)
766 ph = bus_space_read_1(bst, bsh, tmc_bstat);
767 if (ph == (u_int8_t) -1)
769 if ((ph & PHASE_MASK) != phase)
771 if ((ph & mask) != 0)
774 SCSI_LOW_DELAY(STG_DELAY_INTERVAL);
777 printf("%s: stg_expect_signal timeout\n", slp->sl_xname);
782 stg_xfer(sc, buf, len, phase, clear_atn)
783 struct stg_softc *sc;
789 bus_space_tag_t iot = sc->sc_iot;
790 bus_space_handle_t ioh = sc->sc_ioh;
793 if (phase & BSTAT_IO)
794 bus_space_write_1(iot, ioh, tmc_fctl, sc->sc_fcRinit);
796 bus_space_write_1(iot, ioh, tmc_fctl, sc->sc_fcWinit);
798 for (ptr = 0; len > 0; len --)
800 rv = stg_expect_signal(sc, phase, BSTAT_REQ);
804 if (len == 1 && clear_atn != 0)
806 sc->sc_busc &= ~BCTL_ATN;
807 stghw_bcr_write_1(sc, sc->sc_busc);
808 SCSI_LOW_DEASSERT_ATN(&sc->sc_sclow);
811 if (phase & BSTAT_IO)
813 buf[ptr ++] = bus_space_read_1(iot, ioh, tmc_rdata);
817 bus_space_write_1(iot, ioh, tmc_wdata, buf[ptr ++]);
820 stg_negate_signal(sc, BSTAT_ACK, "xfer<ACK>");
824 bus_space_write_1(iot, ioh, tmc_fctl, sc->sc_fcRinit);
828 /**************************************************************
829 * disconnect & reselect (HW low)
830 **************************************************************/
833 struct stg_softc *sc;
835 struct scsi_low_softc *slp = &sc->sc_sclow;
836 bus_space_tag_t iot = sc->sc_iot;
837 bus_space_handle_t ioh = sc->sc_ioh;
842 if (slp->sl_selid != NULL)
845 * Selection vs Reselection conflicts.
847 bus_space_write_1(iot, ioh, tmc_fctl, sc->sc_fcRinit);
848 stghw_bcr_write_1(sc, BCTL_BUSFREE);
850 else if (slp->sl_Tnexus != NULL)
852 printf("%s: unexpected termination\n", slp->sl_xname);
853 stg_disconnected(sc, slp->sl_Tnexus);
857 * We should ack the reselection as soon as possible,
858 * becuase the target would abort the current reselection seq
859 * due to reselection timeout.
861 tout = STG_DELAY_SELECT_POLLING_MAX;
864 regv = bus_space_read_1(iot, ioh, tmc_bstat);
865 if ((regv & (BSTAT_IO | BSTAT_SEL | BSTAT_BSY)) ==
866 (BSTAT_IO | BSTAT_SEL))
869 regv = bus_space_read_1(iot, ioh, tmc_bstat);
870 if ((regv & (BSTAT_IO | BSTAT_SEL | BSTAT_BSY)) ==
871 (BSTAT_IO | BSTAT_SEL))
876 printf("%s: reselction timeout I\n", slp->sl_xname);
880 sid = (u_int) bus_space_read_1(iot, ioh, tmc_scsiid);
881 if ((sid & sc->sc_idbit) == 0)
887 bus_space_write_1(iot, ioh, tmc_fctl,
888 sc->sc_fcRinit | FCTL_CLRFIFO | FCTL_CLRINT);
889 bus_space_write_1(iot, ioh, tmc_fctl, sc->sc_fcRinit);
890 stghw_bcr_write_1(sc, sc->sc_busc | BCTL_BSY);
894 regv = bus_space_read_1(iot, ioh, tmc_bstat);
895 if ((regv & (BSTAT_SEL | BSTAT_BSY)) == BSTAT_BSY)
899 printf("%s: reselction timeout II\n", slp->sl_xname);
903 sid &= ~sc->sc_idbit;
905 if (scsi_low_reselected(slp, sid) == NULL)
909 stg_statics.reselect ++;
910 #endif /* STG_STATICS */
915 stg_disconnected(sc, ti)
916 struct stg_softc *sc;
917 struct targ_info *ti;
919 struct scsi_low_softc *slp = &sc->sc_sclow;
920 bus_space_tag_t iot = sc->sc_iot;
921 bus_space_handle_t ioh = sc->sc_ioh;
923 /* clear bus status & fifo */
924 bus_space_write_1(iot, ioh, tmc_fctl, sc->sc_fcRinit | FCTL_CLRFIFO);
925 bus_space_write_1(iot, ioh, tmc_fctl, sc->sc_fcRinit);
926 stghw_bcr_write_1(sc, BCTL_BUSFREE);
927 sc->sc_icinit &= ~ICTL_FIFO;
928 sc->sc_busc &= ~BCTL_ATN;
929 sc->sc_dataout_timeout = 0;
930 sc->sc_ubf_timeout = 0;
933 stg_statics.disconnect ++;
934 #endif /* STG_STATICS */
935 scsi_low_disconnected(slp, ti);
939 /**************************************************************
941 **************************************************************/
943 stg_target_nexus_establish(sc)
944 struct stg_softc *sc;
946 struct scsi_low_softc *slp = &sc->sc_sclow;
947 bus_space_tag_t iot = sc->sc_iot;
948 bus_space_handle_t ioh = sc->sc_ioh;
949 struct targ_info *ti = slp->sl_Tnexus;
950 struct stg_targ_info *sti = (void *) ti;
952 bus_space_write_1(iot, ioh, tmc_ssctl, sti->sti_reg_synch);
953 if ((stg_io_control & STG_FIFO_INTERRUPTS) != 0)
955 sc->sc_icinit |= ICTL_FIFO;
961 stg_lun_nexus_establish(sc)
962 struct stg_softc *sc;
969 stg_ccb_nexus_establish(sc)
970 struct stg_softc *sc;
972 struct scsi_low_softc *slp = &sc->sc_sclow;
973 struct slccb *cb = slp->sl_Qnexus;
975 sc->sc_tmaxcnt = cb->ccb_tcmax * 1000 * 1000;
979 #define STGHW_SELECT_INTERVAL 10
982 stghw_select_targ_wait(sc, mu)
983 struct stg_softc *sc;
986 bus_space_tag_t iot = sc->sc_iot;
987 bus_space_handle_t ioh = sc->sc_ioh;
989 mu = mu / STGHW_SELECT_INTERVAL;
992 if ((bus_space_read_1(iot, ioh, tmc_bstat) & BSTAT_BSY) == 0)
994 SCSI_LOW_DELAY(STGHW_SELECT_INTERVAL);
998 if ((bus_space_read_1(iot, ioh, tmc_bstat) & BSTAT_BSY) != 0)
1007 stg_selection_done_and_expect_msgout(sc)
1008 struct stg_softc *sc;
1010 struct scsi_low_softc *slp = &sc->sc_sclow;
1011 bus_space_tag_t iot = sc->sc_iot;
1012 bus_space_handle_t ioh = sc->sc_ioh;
1014 bus_space_write_1(iot, ioh, tmc_fctl, sc->sc_fcRinit | FCTL_CLRFIFO);
1015 bus_space_write_1(iot, ioh, tmc_fctl, sc->sc_fcRinit);
1016 stghw_bcr_write_1(sc, sc->sc_imsg | sc->sc_busc);
1017 SCSI_LOW_ASSERT_ATN(slp);
1024 struct stg_softc *sc = arg;
1025 struct scsi_low_softc *slp = &sc->sc_sclow;
1026 bus_space_tag_t iot = sc->sc_iot;
1027 bus_space_handle_t ioh = sc->sc_ioh;
1028 struct targ_info *ti;
1029 struct physio_proc *pp;
1031 u_int derror, flags;
1033 u_int8_t status, astatus, regv;
1035 /*******************************************
1037 *******************************************/
1038 if (slp->sl_flags & HW_INACTIVE)
1041 astatus = bus_space_read_1(iot, ioh, tmc_astat);
1042 status = bus_space_read_1(iot, ioh, tmc_bstat);
1044 if ((astatus & ASTAT_STATMASK) == 0 || astatus == (u_int8_t) -1)
1047 bus_space_write_1(iot, ioh, tmc_ictl, 0);
1048 if (astatus & ASTAT_SCSIRST)
1050 bus_space_write_1(iot, ioh, tmc_fctl,
1051 sc->sc_fcRinit | FCTL_CLRFIFO);
1052 bus_space_write_1(iot, ioh, tmc_fctl, sc->sc_fcRinit);
1053 bus_space_write_1(iot, ioh, tmc_ictl, 0);
1055 scsi_low_restart(slp, SCSI_LOW_RESTART_SOFT,
1056 "bus reset (power off?)");
1060 /*******************************************
1062 *******************************************/
1066 scsi_low_print(slp, NULL);
1067 printf("%s: st %x ist %x\n\n", slp->sl_xname,
1071 SCSI_LOW_DEBUGGER("stg");
1074 #endif /* STG_DEBUG */
1076 /*******************************************
1077 * reselection & nexus
1078 *******************************************/
1079 if ((status & RESEL_PHASE_MASK)== PHASE_RESELECTED)
1081 if (stg_reselected(sc) == EJUSTRETURN)
1085 if ((ti = slp->sl_Tnexus) == NULL)
1089 if ((astatus & ASTAT_PARERR) != 0 && ti->ti_phase != PH_ARBSTART &&
1090 (sc->sc_fcRinit & FCTL_PARENB) != 0)
1092 slp->sl_error |= PARITYERR;
1093 derror = SCSI_LOW_DATA_PE;
1094 if ((status & PHASE_MASK) == MESSAGE_IN_PHASE)
1095 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_PARITY, 0);
1097 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_ERROR, 1);
1100 /*******************************************
1101 * aribitration & selection
1102 *******************************************/
1103 switch (ti->ti_phase)
1106 if ((astatus & ASTAT_ARBIT) == 0)
1109 stg_statics.arbit_fail_0 ++;
1110 #endif /* STG_STATICS */
1114 status = bus_space_read_1(iot, ioh, tmc_bstat);
1115 if ((status & BSTAT_IO) != 0)
1118 * Selection vs Reselection conflicts.
1121 stg_statics.arbit_fail_1 ++;
1122 #endif /* STG_STATICS */
1124 bus_space_write_1(iot, ioh, tmc_fctl, sc->sc_fcRinit);
1125 stghw_bcr_write_1(sc, BCTL_BUSFREE);
1126 scsi_low_arbit_fail(slp, slp->sl_Qnexus);
1131 * selection assert start.
1133 SCSI_LOW_SETUP_PHASE(ti, PH_SELSTART);
1134 scsi_low_arbit_win(slp);
1137 bus_space_write_1(iot, ioh, tmc_scsiid,
1138 sc->sc_idbit | (1 << ti->ti_id));
1139 stghw_bcr_write_1(sc, sc->sc_imsg | sc->sc_busc | BCTL_SEL);
1140 bus_space_write_1(iot, ioh, tmc_fctl, sc->sc_fcWinit);
1141 if ((stg_io_control & STG_WAIT_FOR_SELECT) != 0)
1143 /* selection abort delay 200 + 100 micro sec */
1144 if (stghw_select_targ_wait(sc, 300) == 0)
1146 SCSI_LOW_SETUP_PHASE(ti, PH_SELECTED);
1147 stg_selection_done_and_expect_msgout(sc);
1154 if ((status & BSTAT_BSY) == 0)
1156 /* selection timeout delay 250 ms */
1157 if (stghw_select_targ_wait(sc, 250 * 1000) != 0)
1159 stg_disconnected(sc, ti);
1164 SCSI_LOW_SETUP_PHASE(ti, PH_SELECTED);
1165 stg_selection_done_and_expect_msgout(sc);
1169 if ((status & BSTAT_REQ) == 0)
1171 stg_target_nexus_establish(sc);
1175 if ((status & BSTAT_REQ) == 0)
1178 /* clear a busy line */
1179 bus_space_write_1(iot, ioh, tmc_fctl, sc->sc_fcRinit);
1180 stghw_bcr_write_1(sc, sc->sc_busc);
1181 stg_target_nexus_establish(sc);
1182 if ((status & PHASE_MASK) != MESSAGE_IN_PHASE)
1184 printf("%s: unexpected phase after reselect\n",
1186 slp->sl_error |= FATALIO;
1187 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_ABORT, 1);
1193 /*******************************************
1195 *******************************************/
1196 if ((slp->sl_flags & HW_PDMASTART) && STG_IS_PHASE_DATA(status) == 0)
1198 if (slp->sl_scp.scp_direction == SCSI_LOW_READ)
1199 stg_pio_read(sc, ti, 0);
1201 stg_pdma_end(sc, ti);
1204 /*******************************************
1206 *******************************************/
1207 switch (status & PHASE_MASK)
1210 if (stg_expect_signal(sc, COMMAND_PHASE, BSTAT_REQ) <= 0)
1213 SCSI_LOW_SETUP_PHASE(ti, PH_CMD);
1214 if (scsi_low_cmd(slp, ti) != 0)
1216 scsi_low_attention(slp);
1219 if (stg_xfer(sc, slp->sl_scp.scp_cmd, slp->sl_scp.scp_cmdlen,
1220 COMMAND_PHASE, 0) != 0)
1222 printf("%s: CMDOUT short\n", slp->sl_xname);
1226 case DATA_OUT_PHASE:
1227 SCSI_LOW_SETUP_PHASE(ti, PH_DATA);
1228 if (scsi_low_data(slp, ti, &bp, SCSI_LOW_WRITE) != 0)
1230 scsi_low_attention(slp);
1233 pp = physio_proc_enter(bp);
1234 if ((sc->sc_icinit & ICTL_FIFO) != 0)
1235 stg_pio_write(sc, ti, sc->sc_wthold);
1237 stg_pio_write(sc, ti, 0);
1238 physio_proc_leave(pp);
1242 SCSI_LOW_SETUP_PHASE(ti, PH_DATA);
1243 if (scsi_low_data(slp, ti, &bp, SCSI_LOW_READ) != 0)
1245 scsi_low_attention(slp);
1248 pp = physio_proc_enter(bp);
1249 if ((sc->sc_icinit & ICTL_FIFO) != 0)
1250 stg_pio_read(sc, ti, sc->sc_rthold);
1252 stg_pio_read(sc, ti, 0);
1253 physio_proc_leave(pp);
1257 regv = stg_expect_signal(sc, STATUS_PHASE, BSTAT_REQ);
1261 SCSI_LOW_SETUP_PHASE(ti, PH_STAT);
1262 regv = bus_space_read_1(iot, ioh, tmc_sdna);
1263 if (scsi_low_statusin(slp, ti, regv | derror) != 0)
1265 scsi_low_attention(slp);
1267 if (regv != bus_space_read_1(iot, ioh, tmc_rdata))
1269 printf("%s: STATIN: data mismatch\n", slp->sl_xname);
1271 stg_negate_signal(sc, BSTAT_ACK, "statin<ACK>");
1274 case MESSAGE_OUT_PHASE:
1275 if (stg_expect_signal(sc, MESSAGE_OUT_PHASE, BSTAT_REQ) <= 0)
1278 SCSI_LOW_SETUP_PHASE(ti, PH_MSGOUT);
1279 flags = (ti->ti_ophase != ti->ti_phase) ?
1280 SCSI_LOW_MSGOUT_INIT : 0;
1281 len = scsi_low_msgout(slp, ti, flags);
1283 if (len > 1 && slp->sl_atten == 0)
1285 scsi_low_attention(slp);
1288 if (stg_xfer(sc, ti->ti_msgoutstr, len, MESSAGE_OUT_PHASE,
1289 slp->sl_clear_atten) != 0)
1291 printf("%s: MSGOUT short\n", slp->sl_xname);
1295 if (slp->sl_msgphase >= MSGPH_ABORT)
1297 stg_disconnected(sc, ti);
1302 case MESSAGE_IN_PHASE:
1303 /* confirm phase and req signal */
1304 if (stg_expect_signal(sc, MESSAGE_IN_PHASE, BSTAT_REQ) <= 0)
1307 SCSI_LOW_SETUP_PHASE(ti, PH_MSGIN);
1309 /* read data with NOACK */
1310 regv = bus_space_read_1(iot, ioh, tmc_sdna);
1312 if (scsi_low_msgin(slp, ti, derror | regv) == 0)
1314 if (scsi_low_is_msgout_continue(ti, 0) != 0)
1316 scsi_low_attention(slp);
1320 /* read data with ACK */
1321 if (regv != bus_space_read_1(iot, ioh, tmc_rdata))
1323 printf("%s: MSGIN: data mismatch\n", slp->sl_xname);
1326 /* wait for the ack negated */
1327 stg_negate_signal(sc, BSTAT_ACK, "msgin<ACK>");
1329 if (slp->sl_msgphase != 0 && slp->sl_msgphase < MSGPH_ABORT)
1331 stg_disconnected(sc, ti);
1336 printf("%s: unexpected disconnect\n", slp->sl_xname);
1337 stg_disconnected(sc, ti);
1341 slp->sl_error |= FATALIO;
1342 printf("%s: unknown phase bus %x intr %x\n",
1343 slp->sl_xname, status, astatus);
1348 bus_space_write_1(iot, ioh, tmc_ictl, sc->sc_icinit);
1354 struct stg_softc *sc;
1356 struct scsi_low_softc *slp = &sc->sc_sclow;
1357 bus_space_tag_t iot = sc->sc_iot;
1358 bus_space_handle_t ioh = sc->sc_ioh;
1362 if (slp->sl_Tnexus == NULL)
1365 status = bus_space_read_1(iot, ioh, tmc_bstat);
1366 if ((status & PHASE_MASK) == 0)
1368 if (sc->sc_ubf_timeout ++ == 0)
1371 printf("%s: unexpected bus free detected\n", slp->sl_xname);
1372 slp->sl_error |= FATALIO;
1373 scsi_low_print(slp, slp->sl_Tnexus);
1374 stg_disconnected(sc, slp->sl_Tnexus);
1378 switch (status & PHASE_MASK)
1380 case DATA_OUT_PHASE:
1381 if (sc->sc_dataout_timeout == 0)
1383 if ((status & BSTAT_REQ) == 0)
1385 if (bus_space_read_2(iot, ioh, tmc_fdcnt) != 0)
1387 if ((-- sc->sc_dataout_timeout) > 0)
1390 slp->sl_error |= PDMAERR;
1391 if ((slp->sl_flags & HW_WRITE_PADDING) == 0)
1393 printf("%s: write padding required\n",
1398 bus_space_write_1(iot, ioh, tmc_ictl, 0);
1400 tout = STG_DELAY_MAX;
1403 status = bus_space_read_1(iot, ioh, tmc_bstat);
1404 if ((status & PHASE_MASK) != DATA_OUT_PHASE)
1407 if (bus_space_read_2(iot, ioh, tmc_fdcnt) != 0)
1413 for (count = sc->sc_maxwsize; count > 0; count --)
1414 bus_space_write_1(iot, ioh, tmc_wfifo, 0);
1417 status = bus_space_read_1(iot, ioh, tmc_bstat);
1418 if ((status & PHASE_MASK) == DATA_OUT_PHASE)
1419 sc->sc_dataout_timeout = SCSI_LOW_TIMEOUT_HZ;
1421 bus_space_write_1(iot, ioh, tmc_ictl, sc->sc_icinit);