1 /* $FreeBSD: src/sys/dev/stg/tmc18c30.c,v 1.1.2.5 2001/12/17 13:30:19 non Exp $ */
2 /* $DragonFly: src/sys/dev/disk/stg/tmc18c30.c,v 1.7 2004/02/12 00:00:18 dillon Exp $ */
3 /* $NecBSD: tmc18c30.c,v 1.28.12.3 2001/06/19 04:35:48 honda Exp $ */
8 #define STG_IO_CONTROL_FLAGS (STG_FIFO_INTERRUPTS | STG_WAIT_FOR_SELECT)
11 * [NetBSD for NEC PC-98 series]
12 * Copyright (c) 1996, 1997, 1998, 1999, 2000, 2001
13 * NetBSD/pc98 porting staff. All rights reserved.
14 * Copyright (c) 1996, 1997, 1998, 1999, 2000, 2001
15 * Naofumi HONDA. All rights reserved.
16 * Copyright (c) 1996, 1997, 1998, 1999
17 * Kouichi Matsuda. All rights reserved.
19 * Redistribution and use in source and binary forms, with or without
20 * modification, are permitted provided that the following conditions
22 * 1. Redistributions of source code must retain the above copyright
23 * notice, this list of conditions and the following disclaimer.
24 * 2. Redistributions in binary form must reproduce the above copyright
25 * notice, this list of conditions and the following disclaimer in the
26 * documentation and/or other materials provided with the distribution.
27 * 3. The name of the author may not be used to endorse or promote products
28 * derived from this software without specific prior written permission.
30 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
31 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
32 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
33 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
34 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
35 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
36 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
37 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
38 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
39 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
40 * POSSIBILITY OF SUCH DAMAGE.
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/kernel.h>
48 #include <sys/queue.h>
49 #include <sys/malloc.h>
50 #include <sys/errno.h>
53 #include <sys/device.h>
54 #include <machine/bus.h>
55 #include <machine/intr.h>
57 #include <dev/scsipi/scsi_all.h>
58 #include <dev/scsipi/scsipi_all.h>
59 #include <dev/scsipi/scsiconf.h>
60 #include <dev/scsipi/scsi_disk.h>
62 #include <machine/dvcfg.h>
63 #include <machine/physio_proc.h>
65 #include <i386/Cbus/dev/scsi_low.h>
66 #include <i386/Cbus/dev/tmc18c30reg.h>
67 #include <i386/Cbus/dev/tmc18c30var.h>
68 #endif /* __NetBSD__ */
71 #include <machine/clock.h>
72 #include <machine/cpu.h>
73 #include <machine/bus_pio.h>
74 #include <machine/bus.h>
76 #include <machine/dvcfg.h>
77 #include <machine/physio_proc.h>
79 #include <bus/cam/scsi/scsi_low.h>
80 #include "tmc18c30reg.h"
81 #include "tmc18c30var.h"
82 #endif /* __DragonFly__ */
84 /***************************************************
86 ***************************************************/
87 /* DEVICE CONFIGURATION FLAGS (MINOR)
90 * 0x02 PARITY LINE OFF
91 * 0x04 IDENTIFY MSG OFF ( = single lun)
92 * 0x08 SYNC TRANSFER OFF
94 /* #define STG_SYNC_SUPPORT */ /* NOT YET but easy */
96 /* For the 512 fifo type: change below */
97 #define TMC18C30_FIFOSZ 0x800
98 #define TMC18C30_FCBSZ 0x200
99 #define TMC18C50_FIFOSZ 0x2000
100 #define TMC18C50_FCBSZ 0x400
102 #define STG_MAX_DATA_SIZE (64 * 1024)
103 #define STG_DELAY_MAX (2 * 1000 * 1000)
104 #define STG_DELAY_INTERVAL (1)
105 #define STG_DELAY_SELECT_POLLING_MAX (5 * 1000 * 1000)
107 /***************************************************
109 ***************************************************/
110 #define STG_NTARGETS 8
113 /***************************************************
115 ***************************************************/
118 #endif /* STG_DEBUG */
127 #endif /* STG_STATICS */
129 /***************************************************
131 ***************************************************/
132 #define STG_FIFO_INTERRUPTS 0x0001
133 #define STG_WAIT_FOR_SELECT 0x0100
135 int stg_io_control = STG_IO_CONTROL_FLAGS;
137 /***************************************************
139 ***************************************************/
140 extern struct cfdriver stg_cd;
142 /**************************************************************
144 **************************************************************/
146 static void stg_pio_read (struct stg_softc *, struct targ_info *, u_int);
147 static void stg_pio_write (struct stg_softc *, struct targ_info *, u_int);
148 static int stg_xfer (struct stg_softc *, u_int8_t *, int, int, int);
149 static int stg_msg (struct stg_softc *, struct targ_info *, u_int);
150 static int stg_reselected (struct stg_softc *);
151 static int stg_disconnected (struct stg_softc *, struct targ_info *);
152 static __inline void stg_pdma_end (struct stg_softc *, struct targ_info *);
153 static int stghw_select_targ_wait (struct stg_softc *, int);
154 static int stghw_check (struct stg_softc *);
155 static void stghw_init (struct stg_softc *);
156 static int stg_negate_signal (struct stg_softc *, u_int8_t, u_char *);
157 static int stg_expect_signal (struct stg_softc *, u_int8_t, u_int8_t);
158 static int stg_world_start (struct stg_softc *, int);
159 static int stghw_start_selection (struct stg_softc *sc, struct slccb *);
160 static void stghw_bus_reset (struct stg_softc *);
161 static void stghw_attention (struct stg_softc *);
162 static int stg_target_nexus_establish (struct stg_softc *);
163 static int stg_lun_nexus_establish (struct stg_softc *);
164 static int stg_ccb_nexus_establish (struct stg_softc *);
165 static int stg_targ_init (struct stg_softc *, struct targ_info *, int);
166 static __inline void stghw_bcr_write_1 (struct stg_softc *, u_int8_t);
167 static int stg_timeout (struct stg_softc *);
168 static void stg_selection_done_and_expect_msgout (struct stg_softc *);
170 struct scsi_low_funcs stgfuncs = {
171 SC_LOW_INIT_T stg_world_start,
172 SC_LOW_BUSRST_T stghw_bus_reset,
173 SC_LOW_TARG_INIT_T stg_targ_init,
174 SC_LOW_LUN_INIT_T NULL,
176 SC_LOW_SELECT_T stghw_start_selection,
177 SC_LOW_NEXUS_T stg_lun_nexus_establish,
178 SC_LOW_NEXUS_T stg_ccb_nexus_establish,
180 SC_LOW_ATTEN_T stghw_attention,
181 SC_LOW_MSG_T stg_msg,
183 SC_LOW_TIMEOUT_T stg_timeout,
184 SC_LOW_POLL_T stgintr,
189 /****************************************************
191 ****************************************************/
193 stghw_bcr_write_1(sc, bcv)
194 struct stg_softc *sc;
198 bus_space_write_1(sc->sc_iot, sc->sc_ioh, tmc_bctl, bcv);
204 struct stg_softc *sc;
206 struct scsi_low_softc *slp = &sc->sc_sclow;
207 bus_space_tag_t iot = sc->sc_iot;
208 bus_space_handle_t ioh = sc->sc_ioh;
212 lsb = bus_space_read_1(iot, ioh, tmc_idlsb);
213 msb = bus_space_read_1(iot, ioh, tmc_idmsb);
214 switch (msb << 8 | lsb)
217 /* TMCCHIP_1800 not supported. (it's my policy) */
218 sc->sc_chip = TMCCHIP_1800;
222 if (bus_space_read_1(iot, ioh, tmc_cfg2) & 0x02)
224 sc->sc_chip = TMCCHIP_18C30;
225 sc->sc_fsz = TMC18C30_FIFOSZ;
226 fcbsize = TMC18C30_FCBSZ;
230 sc->sc_chip = TMCCHIP_18C50;
231 sc->sc_fsz = TMC18C50_FIFOSZ;
232 fcbsize = TMC18C50_FCBSZ;
237 sc->sc_chip = TMCCHIP_UNK;
241 sc->sc_fcRinit = FCTL_INTEN;
242 sc->sc_fcWinit = FCTL_PARENB | FCTL_INTEN;
244 if (slp->sl_cfgflags & CFG_NOATTEN)
247 sc->sc_imsg = BCTL_ATN;
248 sc->sc_busc = BCTL_BUSEN;
250 sc->sc_wthold = fcbsize + 256;
251 sc->sc_rthold = fcbsize - 256;
252 sc->sc_maxwsize = sc->sc_fsz;
254 fcb = fcbsize / (sc->sc_fsz / 16);
255 sc->sc_icinit = ICTL_CD | ICTL_SEL | ICTL_ARBIT | fcb;
261 struct stg_softc *sc;
263 bus_space_tag_t iot = sc->sc_iot;
264 bus_space_handle_t ioh = sc->sc_ioh;
266 bus_space_write_1(iot, ioh, tmc_ictl, 0);
267 stghw_bcr_write_1(sc, BCTL_BUSFREE);
268 bus_space_write_1(iot, ioh, tmc_fctl,
269 sc->sc_fcRinit | FCTL_CLRFIFO | FCTL_CLRINT);
270 bus_space_write_1(iot, ioh, tmc_fctl, sc->sc_fcRinit);
271 bus_space_write_1(iot, ioh, tmc_ictl, sc->sc_icinit);
273 bus_space_write_1(iot, ioh, tmc_ssctl, 0);
277 stg_targ_init(sc, ti, action)
278 struct stg_softc *sc;
279 struct targ_info *ti;
282 struct stg_targ_info *sti = (void *) ti;
284 if (action == SCSI_LOW_INFO_ALLOC || action == SCSI_LOW_INFO_REVOKE)
286 ti->ti_width = SCSI_LOW_BUS_WIDTH_8;
287 ti->ti_maxsynch.period = 0;
288 ti->ti_maxsynch.offset = 0;
289 sti->sti_reg_synch = 0;
294 /****************************************************
296 ****************************************************/
299 struct stg_softc *sc;
302 sc->sc_busc |= BCTL_ATN;
303 sc->sc_busimg |= BCTL_ATN;
304 bus_space_write_1(sc->sc_iot, sc->sc_ioh, tmc_bctl, sc->sc_busimg);
310 struct stg_softc *sc;
312 bus_space_tag_t iot = sc->sc_iot;
313 bus_space_handle_t ioh = sc->sc_ioh;
315 bus_space_write_1(iot, ioh, tmc_ictl, 0);
316 bus_space_write_1(iot, ioh, tmc_fctl, 0);
317 stghw_bcr_write_1(sc, BCTL_RST);
318 SCSI_LOW_DELAY(100000);
319 stghw_bcr_write_1(sc, BCTL_BUSFREE);
323 stghw_start_selection(sc, cb)
324 struct stg_softc *sc;
327 bus_space_tag_t iot = sc->sc_iot;
328 bus_space_handle_t ioh = sc->sc_ioh;
329 struct targ_info *ti = cb->ti;
333 sc->sc_tmaxcnt = cb->ccb_tcmax * 1000 * 1000;
334 sc->sc_dataout_timeout = 0;
335 sc->sc_ubf_timeout = 0;
336 stghw_bcr_write_1(sc, BCTL_BUSFREE);
337 bus_space_write_1(iot, ioh, tmc_ictl, sc->sc_icinit);
340 stat = bus_space_read_1(iot, ioh, tmc_astat);
341 if ((stat & ASTAT_INT) != 0)
344 return SCSI_LOW_START_FAIL;
347 bus_space_write_1(iot, ioh, tmc_scsiid, sc->sc_idbit);
348 bus_space_write_1(iot, ioh, tmc_fctl, sc->sc_fcRinit | FCTL_ARBIT);
351 SCSI_LOW_SETUP_PHASE(ti, PH_ARBSTART);
352 return SCSI_LOW_START_OK;
356 stg_world_start(sc, fdone)
357 struct stg_softc *sc;
360 struct scsi_low_softc *slp = &sc->sc_sclow;
363 if ((slp->sl_cfgflags & CFG_NOPARITY) == 0)
364 sc->sc_fcRinit |= FCTL_PARENB;
366 sc->sc_fcRinit &= ~FCTL_PARENB;
368 if ((error = stghw_check(sc)) != 0)
372 scsi_low_bus_reset(slp);
375 SOFT_INTR_REQUIRED(slp);
381 struct stg_softc *sc;
382 struct targ_info *ti;
385 bus_space_tag_t iot = sc->sc_iot;
386 bus_space_handle_t ioh = sc->sc_ioh;
387 struct stg_targ_info *sti = (void *) ti;
388 u_int period, offset;
390 if ((msg & SCSI_LOW_MSG_WIDE) != 0)
392 if (ti->ti_width != SCSI_LOW_BUS_WIDTH_8)
394 ti->ti_width = SCSI_LOW_BUS_WIDTH_8;
400 if ((msg & SCSI_LOW_MSG_SYNCH) == 0)
403 period = ti->ti_maxsynch.period;
404 offset = ti->ti_maxsynch.offset;
405 period = period << 2;
408 sti->sti_reg_synch = (period - 200) / 50;
410 sti->sti_reg_synch ++;
411 sti->sti_reg_synch |= SSCTL_SYNCHEN;
413 else if (period >= 100)
415 sti->sti_reg_synch = (period - 100) / 50;
417 sti->sti_reg_synch ++;
418 sti->sti_reg_synch |= SSCTL_SYNCHEN | SSCTL_FSYNCHEN;
420 bus_space_write_1(iot, ioh, tmc_ssctl, sti->sti_reg_synch);
424 /**************************************************************
425 * General probe attach
426 **************************************************************/
428 stgprobesubr(iot, ioh, dvcfg)
430 bus_space_handle_t ioh;
435 lsb = bus_space_read_1(iot, ioh, tmc_idlsb);
436 msb = bus_space_read_1(iot, ioh, tmc_idmsb);
437 switch (msb << 8 | lsb)
457 printf("%s: scsibus ", name);
463 struct stg_softc *sc;
465 struct scsi_low_softc *slp = &sc->sc_sclow;
469 sc->sc_idbit = (1 << slp->sl_hostid);
470 slp->sl_funcs = &stgfuncs;
471 sc->sc_tmaxcnt = SCSI_LOW_MIN_TOUT * 1000 * 1000; /* default */
473 slp->sl_flags |= HW_READ_PADDING;
474 slp->sl_cfgflags |= CFG_ASYNC; /* XXX */
476 (void) scsi_low_attach(slp, 0, STG_NTARGETS, STG_NLUNS,
477 sizeof(struct stg_targ_info), 0);
480 /**************************************************************
482 **************************************************************/
485 struct stg_softc *sc;
486 struct targ_info *ti;
488 struct scsi_low_softc *slp = &sc->sc_sclow;
489 bus_space_tag_t iot = sc->sc_iot;
490 bus_space_handle_t ioh = sc->sc_ioh;
491 struct slccb *cb = slp->sl_Qnexus;
494 slp->sl_flags &= ~HW_PDMASTART;
495 sc->sc_icinit &= ~ICTL_FIFO;
496 sc->sc_dataout_timeout = 0;
500 slp->sl_error |= PDMAERR;
504 if (ti->ti_phase == PH_DATA)
506 len = bus_space_read_2(iot, ioh, tmc_fdcnt);
507 if (slp->sl_scp.scp_direction == SCSI_LOW_WRITE)
511 tres = len + slp->sl_scp.scp_datalen;
512 if (tres <= (u_int) cb->ccb_scp.scp_datalen)
514 slp->sl_scp.scp_data -= len;
515 slp->sl_scp.scp_datalen = tres;
519 slp->sl_error |= PDMAERR;
520 printf("%s len %x >= datalen %x\n",
522 len, slp->sl_scp.scp_datalen);
526 else if (slp->sl_scp.scp_direction == SCSI_LOW_READ)
530 slp->sl_error |= PDMAERR;
531 printf("%s: len %x left in fifo\n",
535 scsi_low_data_finish(slp);
540 printf("%s data phase miss\n", slp->sl_xname);
541 slp->sl_error |= PDMAERR;
545 bus_space_write_1(iot, ioh, tmc_fctl, sc->sc_fcRinit);
549 stg_pio_read(sc, ti, thold)
550 struct stg_softc *sc;
551 struct targ_info *ti;
554 struct scsi_low_softc *slp = &sc->sc_sclow;
555 bus_space_tag_t iot = sc->sc_iot;
556 bus_space_handle_t ioh = sc->sc_ioh;
557 struct sc_p *sp = &slp->sl_scp;
562 if ((slp->sl_flags & HW_PDMASTART) == 0)
564 bus_space_write_1(iot, ioh, tmc_fctl,
565 sc->sc_fcRinit | FCTL_FIFOEN);
566 slp->sl_flags |= HW_PDMASTART;
569 tout = sc->sc_tmaxcnt;
575 res = bus_space_read_2(iot, ioh, tmc_fdcnt);
578 bus_space_write_1(iot, ioh, tmc_ictl,
587 stat = bus_space_read_1(iot, ioh, tmc_bstat);
588 res = bus_space_read_2(iot, ioh, tmc_fdcnt);
591 if ((stat & PHASE_MASK) != DATA_IN_PHASE)
593 if (sp->scp_datalen <= 0)
600 /* The assumtion res != 0 is valid here */
601 if (res > sp->scp_datalen)
603 if (res == (u_int) -1)
606 slp->sl_error |= PDMAERR;
607 if ((slp->sl_flags & HW_READ_PADDING) == 0)
609 printf("%s: read padding required\n",
615 if (res > STG_MAX_DATA_SIZE)
616 res = STG_MAX_DATA_SIZE;
619 (void) bus_space_read_1(iot, ioh, tmc_rfifo);
624 sp->scp_datalen -= res;
627 *sp->scp_data = bus_space_read_1(iot, ioh, tmc_rfifo);
632 bus_space_read_multi_2(iot, ioh, tmc_rfifo,
633 (u_int16_t *) sp->scp_data, res >> 1);
638 printf("%s: pio read timeout\n", slp->sl_xname);
642 stg_pio_write(sc, ti, thold)
643 struct stg_softc *sc;
644 struct targ_info *ti;
647 struct scsi_low_softc *slp = &sc->sc_sclow;
648 bus_space_tag_t iot = sc->sc_iot;
649 bus_space_handle_t ioh = sc->sc_ioh;
650 struct sc_p *sp = &slp->sl_scp;
655 if ((slp->sl_flags & HW_PDMASTART) == 0)
657 stat = sc->sc_fcWinit | FCTL_FIFOEN | FCTL_FIFOW;
658 bus_space_write_1(iot, ioh, tmc_fctl, stat | FCTL_CLRFIFO);
659 bus_space_write_1(iot, ioh, tmc_fctl, stat);
660 slp->sl_flags |= HW_PDMASTART;
663 tout = sc->sc_tmaxcnt;
666 stat = bus_space_read_1(iot, ioh, tmc_bstat);
667 if ((stat & PHASE_MASK) != DATA_OUT_PHASE)
670 if (sp->scp_datalen <= 0)
672 if (sc->sc_dataout_timeout == 0)
673 sc->sc_dataout_timeout = SCSI_LOW_TIMEOUT_HZ;
680 res = bus_space_read_2(iot, ioh, tmc_fdcnt);
683 bus_space_write_1(iot, ioh, tmc_ictl,
692 res = bus_space_read_2(iot, ioh, tmc_fdcnt);
693 if (res > sc->sc_maxwsize / 2)
700 if (res == (u_int) -1)
702 res = sc->sc_maxwsize - res;
703 if (res > sp->scp_datalen)
704 res = sp->scp_datalen;
706 sp->scp_datalen -= res;
707 if ((res & 0x1) != 0)
709 bus_space_write_1(iot, ioh, tmc_wfifo, *sp->scp_data);
714 bus_space_write_multi_2(iot, ioh, tmc_wfifo,
715 (u_int16_t *) sp->scp_data, res >> 1);
720 printf("%s: pio write timeout\n", slp->sl_xname);
724 stg_negate_signal(sc, mask, s)
725 struct stg_softc *sc;
729 struct scsi_low_softc *slp = &sc->sc_sclow;
730 bus_space_tag_t bst = sc->sc_iot;
731 bus_space_handle_t bsh = sc->sc_ioh;
735 for (wc = 0; wc < STG_DELAY_MAX / STG_DELAY_INTERVAL; wc ++)
737 regv = bus_space_read_1(bst, bsh, tmc_bstat);
738 if (regv == (u_int8_t) -1)
740 if ((regv & mask) == 0)
743 SCSI_LOW_DELAY(STG_DELAY_INTERVAL);
746 printf("%s: %s stg_negate_signal timeout\n", slp->sl_xname, s);
751 stg_expect_signal(sc, phase, mask)
752 struct stg_softc *sc;
753 u_int8_t phase, mask;
755 struct scsi_low_softc *slp = &sc->sc_sclow;
756 bus_space_tag_t bst = sc->sc_iot;
757 bus_space_handle_t bsh = sc->sc_ioh;
762 for (wc = 0; wc < STG_DELAY_MAX / STG_DELAY_INTERVAL; wc ++)
764 ph = bus_space_read_1(bst, bsh, tmc_bstat);
765 if (ph == (u_int8_t) -1)
767 if ((ph & PHASE_MASK) != phase)
769 if ((ph & mask) != 0)
772 SCSI_LOW_DELAY(STG_DELAY_INTERVAL);
775 printf("%s: stg_expect_signal timeout\n", slp->sl_xname);
780 stg_xfer(sc, buf, len, phase, clear_atn)
781 struct stg_softc *sc;
787 bus_space_tag_t iot = sc->sc_iot;
788 bus_space_handle_t ioh = sc->sc_ioh;
791 if (phase & BSTAT_IO)
792 bus_space_write_1(iot, ioh, tmc_fctl, sc->sc_fcRinit);
794 bus_space_write_1(iot, ioh, tmc_fctl, sc->sc_fcWinit);
796 for (ptr = 0; len > 0; len --)
798 rv = stg_expect_signal(sc, phase, BSTAT_REQ);
802 if (len == 1 && clear_atn != 0)
804 sc->sc_busc &= ~BCTL_ATN;
805 stghw_bcr_write_1(sc, sc->sc_busc);
806 SCSI_LOW_DEASSERT_ATN(&sc->sc_sclow);
809 if (phase & BSTAT_IO)
811 buf[ptr ++] = bus_space_read_1(iot, ioh, tmc_rdata);
815 bus_space_write_1(iot, ioh, tmc_wdata, buf[ptr ++]);
818 stg_negate_signal(sc, BSTAT_ACK, "xfer<ACK>");
822 bus_space_write_1(iot, ioh, tmc_fctl, sc->sc_fcRinit);
826 /**************************************************************
827 * disconnect & reselect (HW low)
828 **************************************************************/
831 struct stg_softc *sc;
833 struct scsi_low_softc *slp = &sc->sc_sclow;
834 bus_space_tag_t iot = sc->sc_iot;
835 bus_space_handle_t ioh = sc->sc_ioh;
840 if (slp->sl_selid != NULL)
843 * Selection vs Reselection conflicts.
845 bus_space_write_1(iot, ioh, tmc_fctl, sc->sc_fcRinit);
846 stghw_bcr_write_1(sc, BCTL_BUSFREE);
848 else if (slp->sl_Tnexus != NULL)
850 printf("%s: unexpected termination\n", slp->sl_xname);
851 stg_disconnected(sc, slp->sl_Tnexus);
855 * We should ack the reselection as soon as possible,
856 * because the target would abort the current reselection seq
857 * due to reselection timeout.
859 tout = STG_DELAY_SELECT_POLLING_MAX;
862 regv = bus_space_read_1(iot, ioh, tmc_bstat);
863 if ((regv & (BSTAT_IO | BSTAT_SEL | BSTAT_BSY)) ==
864 (BSTAT_IO | BSTAT_SEL))
867 regv = bus_space_read_1(iot, ioh, tmc_bstat);
868 if ((regv & (BSTAT_IO | BSTAT_SEL | BSTAT_BSY)) ==
869 (BSTAT_IO | BSTAT_SEL))
874 printf("%s: reselction timeout I\n", slp->sl_xname);
878 sid = (u_int) bus_space_read_1(iot, ioh, tmc_scsiid);
879 if ((sid & sc->sc_idbit) == 0)
885 bus_space_write_1(iot, ioh, tmc_fctl,
886 sc->sc_fcRinit | FCTL_CLRFIFO | FCTL_CLRINT);
887 bus_space_write_1(iot, ioh, tmc_fctl, sc->sc_fcRinit);
888 stghw_bcr_write_1(sc, sc->sc_busc | BCTL_BSY);
892 regv = bus_space_read_1(iot, ioh, tmc_bstat);
893 if ((regv & (BSTAT_SEL | BSTAT_BSY)) == BSTAT_BSY)
897 printf("%s: reselction timeout II\n", slp->sl_xname);
901 sid &= ~sc->sc_idbit;
903 if (scsi_low_reselected(slp, sid) == NULL)
907 stg_statics.reselect ++;
908 #endif /* STG_STATICS */
913 stg_disconnected(sc, ti)
914 struct stg_softc *sc;
915 struct targ_info *ti;
917 struct scsi_low_softc *slp = &sc->sc_sclow;
918 bus_space_tag_t iot = sc->sc_iot;
919 bus_space_handle_t ioh = sc->sc_ioh;
921 /* clear bus status & fifo */
922 bus_space_write_1(iot, ioh, tmc_fctl, sc->sc_fcRinit | FCTL_CLRFIFO);
923 bus_space_write_1(iot, ioh, tmc_fctl, sc->sc_fcRinit);
924 stghw_bcr_write_1(sc, BCTL_BUSFREE);
925 sc->sc_icinit &= ~ICTL_FIFO;
926 sc->sc_busc &= ~BCTL_ATN;
927 sc->sc_dataout_timeout = 0;
928 sc->sc_ubf_timeout = 0;
931 stg_statics.disconnect ++;
932 #endif /* STG_STATICS */
933 scsi_low_disconnected(slp, ti);
937 /**************************************************************
939 **************************************************************/
941 stg_target_nexus_establish(sc)
942 struct stg_softc *sc;
944 struct scsi_low_softc *slp = &sc->sc_sclow;
945 bus_space_tag_t iot = sc->sc_iot;
946 bus_space_handle_t ioh = sc->sc_ioh;
947 struct targ_info *ti = slp->sl_Tnexus;
948 struct stg_targ_info *sti = (void *) ti;
950 bus_space_write_1(iot, ioh, tmc_ssctl, sti->sti_reg_synch);
951 if ((stg_io_control & STG_FIFO_INTERRUPTS) != 0)
953 sc->sc_icinit |= ICTL_FIFO;
959 stg_lun_nexus_establish(sc)
960 struct stg_softc *sc;
967 stg_ccb_nexus_establish(sc)
968 struct stg_softc *sc;
970 struct scsi_low_softc *slp = &sc->sc_sclow;
971 struct slccb *cb = slp->sl_Qnexus;
973 sc->sc_tmaxcnt = cb->ccb_tcmax * 1000 * 1000;
977 #define STGHW_SELECT_INTERVAL 10
980 stghw_select_targ_wait(sc, mu)
981 struct stg_softc *sc;
984 bus_space_tag_t iot = sc->sc_iot;
985 bus_space_handle_t ioh = sc->sc_ioh;
987 mu = mu / STGHW_SELECT_INTERVAL;
990 if ((bus_space_read_1(iot, ioh, tmc_bstat) & BSTAT_BSY) == 0)
992 SCSI_LOW_DELAY(STGHW_SELECT_INTERVAL);
996 if ((bus_space_read_1(iot, ioh, tmc_bstat) & BSTAT_BSY) != 0)
1005 stg_selection_done_and_expect_msgout(sc)
1006 struct stg_softc *sc;
1008 struct scsi_low_softc *slp = &sc->sc_sclow;
1009 bus_space_tag_t iot = sc->sc_iot;
1010 bus_space_handle_t ioh = sc->sc_ioh;
1012 bus_space_write_1(iot, ioh, tmc_fctl, sc->sc_fcRinit | FCTL_CLRFIFO);
1013 bus_space_write_1(iot, ioh, tmc_fctl, sc->sc_fcRinit);
1014 stghw_bcr_write_1(sc, sc->sc_imsg | sc->sc_busc);
1015 SCSI_LOW_ASSERT_ATN(slp);
1022 struct stg_softc *sc = arg;
1023 struct scsi_low_softc *slp = &sc->sc_sclow;
1024 bus_space_tag_t iot = sc->sc_iot;
1025 bus_space_handle_t ioh = sc->sc_ioh;
1026 struct targ_info *ti;
1027 struct physio_proc *pp;
1029 u_int derror, flags;
1031 u_int8_t status, astatus, regv;
1033 /*******************************************
1035 *******************************************/
1036 if (slp->sl_flags & HW_INACTIVE)
1039 astatus = bus_space_read_1(iot, ioh, tmc_astat);
1040 status = bus_space_read_1(iot, ioh, tmc_bstat);
1042 if ((astatus & ASTAT_STATMASK) == 0 || astatus == (u_int8_t) -1)
1045 bus_space_write_1(iot, ioh, tmc_ictl, 0);
1046 if (astatus & ASTAT_SCSIRST)
1048 bus_space_write_1(iot, ioh, tmc_fctl,
1049 sc->sc_fcRinit | FCTL_CLRFIFO);
1050 bus_space_write_1(iot, ioh, tmc_fctl, sc->sc_fcRinit);
1051 bus_space_write_1(iot, ioh, tmc_ictl, 0);
1053 scsi_low_restart(slp, SCSI_LOW_RESTART_SOFT,
1054 "bus reset (power off?)");
1058 /*******************************************
1060 *******************************************/
1064 scsi_low_print(slp, NULL);
1065 printf("%s: st %x ist %x\n\n", slp->sl_xname,
1069 SCSI_LOW_DEBUGGER("stg");
1072 #endif /* STG_DEBUG */
1074 /*******************************************
1075 * reselection & nexus
1076 *******************************************/
1077 if ((status & RESEL_PHASE_MASK)== PHASE_RESELECTED)
1079 if (stg_reselected(sc) == EJUSTRETURN)
1083 if ((ti = slp->sl_Tnexus) == NULL)
1087 if ((astatus & ASTAT_PARERR) != 0 && ti->ti_phase != PH_ARBSTART &&
1088 (sc->sc_fcRinit & FCTL_PARENB) != 0)
1090 slp->sl_error |= PARITYERR;
1091 derror = SCSI_LOW_DATA_PE;
1092 if ((status & PHASE_MASK) == MESSAGE_IN_PHASE)
1093 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_PARITY, 0);
1095 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_ERROR, 1);
1098 /*******************************************
1099 * aribitration & selection
1100 *******************************************/
1101 switch (ti->ti_phase)
1104 if ((astatus & ASTAT_ARBIT) == 0)
1107 stg_statics.arbit_fail_0 ++;
1108 #endif /* STG_STATICS */
1112 status = bus_space_read_1(iot, ioh, tmc_bstat);
1113 if ((status & BSTAT_IO) != 0)
1116 * Selection vs Reselection conflicts.
1119 stg_statics.arbit_fail_1 ++;
1120 #endif /* STG_STATICS */
1122 bus_space_write_1(iot, ioh, tmc_fctl, sc->sc_fcRinit);
1123 stghw_bcr_write_1(sc, BCTL_BUSFREE);
1124 scsi_low_arbit_fail(slp, slp->sl_Qnexus);
1129 * selection assert start.
1131 SCSI_LOW_SETUP_PHASE(ti, PH_SELSTART);
1132 scsi_low_arbit_win(slp);
1135 bus_space_write_1(iot, ioh, tmc_scsiid,
1136 sc->sc_idbit | (1 << ti->ti_id));
1137 stghw_bcr_write_1(sc, sc->sc_imsg | sc->sc_busc | BCTL_SEL);
1138 bus_space_write_1(iot, ioh, tmc_fctl, sc->sc_fcWinit);
1139 if ((stg_io_control & STG_WAIT_FOR_SELECT) != 0)
1141 /* selection abort delay 200 + 100 micro sec */
1142 if (stghw_select_targ_wait(sc, 300) == 0)
1144 SCSI_LOW_SETUP_PHASE(ti, PH_SELECTED);
1145 stg_selection_done_and_expect_msgout(sc);
1152 if ((status & BSTAT_BSY) == 0)
1154 /* selection timeout delay 250 ms */
1155 if (stghw_select_targ_wait(sc, 250 * 1000) != 0)
1157 stg_disconnected(sc, ti);
1162 SCSI_LOW_SETUP_PHASE(ti, PH_SELECTED);
1163 stg_selection_done_and_expect_msgout(sc);
1167 if ((status & BSTAT_REQ) == 0)
1169 stg_target_nexus_establish(sc);
1173 if ((status & BSTAT_REQ) == 0)
1176 /* clear a busy line */
1177 bus_space_write_1(iot, ioh, tmc_fctl, sc->sc_fcRinit);
1178 stghw_bcr_write_1(sc, sc->sc_busc);
1179 stg_target_nexus_establish(sc);
1180 if ((status & PHASE_MASK) != MESSAGE_IN_PHASE)
1182 printf("%s: unexpected phase after reselect\n",
1184 slp->sl_error |= FATALIO;
1185 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_ABORT, 1);
1191 /*******************************************
1193 *******************************************/
1194 if ((slp->sl_flags & HW_PDMASTART) && STG_IS_PHASE_DATA(status) == 0)
1196 if (slp->sl_scp.scp_direction == SCSI_LOW_READ)
1197 stg_pio_read(sc, ti, 0);
1199 stg_pdma_end(sc, ti);
1202 /*******************************************
1204 *******************************************/
1205 switch (status & PHASE_MASK)
1208 if (stg_expect_signal(sc, COMMAND_PHASE, BSTAT_REQ) <= 0)
1211 SCSI_LOW_SETUP_PHASE(ti, PH_CMD);
1212 if (scsi_low_cmd(slp, ti) != 0)
1214 scsi_low_attention(slp);
1217 if (stg_xfer(sc, slp->sl_scp.scp_cmd, slp->sl_scp.scp_cmdlen,
1218 COMMAND_PHASE, 0) != 0)
1220 printf("%s: CMDOUT short\n", slp->sl_xname);
1224 case DATA_OUT_PHASE:
1225 SCSI_LOW_SETUP_PHASE(ti, PH_DATA);
1226 if (scsi_low_data(slp, ti, &bp, SCSI_LOW_WRITE) != 0)
1228 scsi_low_attention(slp);
1231 pp = physio_proc_enter(bp);
1232 if ((sc->sc_icinit & ICTL_FIFO) != 0)
1233 stg_pio_write(sc, ti, sc->sc_wthold);
1235 stg_pio_write(sc, ti, 0);
1236 physio_proc_leave(pp);
1240 SCSI_LOW_SETUP_PHASE(ti, PH_DATA);
1241 if (scsi_low_data(slp, ti, &bp, SCSI_LOW_READ) != 0)
1243 scsi_low_attention(slp);
1246 pp = physio_proc_enter(bp);
1247 if ((sc->sc_icinit & ICTL_FIFO) != 0)
1248 stg_pio_read(sc, ti, sc->sc_rthold);
1250 stg_pio_read(sc, ti, 0);
1251 physio_proc_leave(pp);
1255 regv = stg_expect_signal(sc, STATUS_PHASE, BSTAT_REQ);
1259 SCSI_LOW_SETUP_PHASE(ti, PH_STAT);
1260 regv = bus_space_read_1(iot, ioh, tmc_sdna);
1261 if (scsi_low_statusin(slp, ti, regv | derror) != 0)
1263 scsi_low_attention(slp);
1265 if (regv != bus_space_read_1(iot, ioh, tmc_rdata))
1267 printf("%s: STATIN: data mismatch\n", slp->sl_xname);
1269 stg_negate_signal(sc, BSTAT_ACK, "statin<ACK>");
1272 case MESSAGE_OUT_PHASE:
1273 if (stg_expect_signal(sc, MESSAGE_OUT_PHASE, BSTAT_REQ) <= 0)
1276 SCSI_LOW_SETUP_PHASE(ti, PH_MSGOUT);
1277 flags = (ti->ti_ophase != ti->ti_phase) ?
1278 SCSI_LOW_MSGOUT_INIT : 0;
1279 len = scsi_low_msgout(slp, ti, flags);
1281 if (len > 1 && slp->sl_atten == 0)
1283 scsi_low_attention(slp);
1286 if (stg_xfer(sc, ti->ti_msgoutstr, len, MESSAGE_OUT_PHASE,
1287 slp->sl_clear_atten) != 0)
1289 printf("%s: MSGOUT short\n", slp->sl_xname);
1293 if (slp->sl_msgphase >= MSGPH_ABORT)
1295 stg_disconnected(sc, ti);
1300 case MESSAGE_IN_PHASE:
1301 /* confirm phase and req signal */
1302 if (stg_expect_signal(sc, MESSAGE_IN_PHASE, BSTAT_REQ) <= 0)
1305 SCSI_LOW_SETUP_PHASE(ti, PH_MSGIN);
1307 /* read data with NOACK */
1308 regv = bus_space_read_1(iot, ioh, tmc_sdna);
1310 if (scsi_low_msgin(slp, ti, derror | regv) == 0)
1312 if (scsi_low_is_msgout_continue(ti, 0) != 0)
1314 scsi_low_attention(slp);
1318 /* read data with ACK */
1319 if (regv != bus_space_read_1(iot, ioh, tmc_rdata))
1321 printf("%s: MSGIN: data mismatch\n", slp->sl_xname);
1324 /* wait for the ack negated */
1325 stg_negate_signal(sc, BSTAT_ACK, "msgin<ACK>");
1327 if (slp->sl_msgphase != 0 && slp->sl_msgphase < MSGPH_ABORT)
1329 stg_disconnected(sc, ti);
1334 printf("%s: unexpected disconnect\n", slp->sl_xname);
1335 stg_disconnected(sc, ti);
1339 slp->sl_error |= FATALIO;
1340 printf("%s: unknown phase bus %x intr %x\n",
1341 slp->sl_xname, status, astatus);
1346 bus_space_write_1(iot, ioh, tmc_ictl, sc->sc_icinit);
1352 struct stg_softc *sc;
1354 struct scsi_low_softc *slp = &sc->sc_sclow;
1355 bus_space_tag_t iot = sc->sc_iot;
1356 bus_space_handle_t ioh = sc->sc_ioh;
1360 if (slp->sl_Tnexus == NULL)
1363 status = bus_space_read_1(iot, ioh, tmc_bstat);
1364 if ((status & PHASE_MASK) == 0)
1366 if (sc->sc_ubf_timeout ++ == 0)
1369 printf("%s: unexpected bus free detected\n", slp->sl_xname);
1370 slp->sl_error |= FATALIO;
1371 scsi_low_print(slp, slp->sl_Tnexus);
1372 stg_disconnected(sc, slp->sl_Tnexus);
1376 switch (status & PHASE_MASK)
1378 case DATA_OUT_PHASE:
1379 if (sc->sc_dataout_timeout == 0)
1381 if ((status & BSTAT_REQ) == 0)
1383 if (bus_space_read_2(iot, ioh, tmc_fdcnt) != 0)
1385 if ((-- sc->sc_dataout_timeout) > 0)
1388 slp->sl_error |= PDMAERR;
1389 if ((slp->sl_flags & HW_WRITE_PADDING) == 0)
1391 printf("%s: write padding required\n",
1396 bus_space_write_1(iot, ioh, tmc_ictl, 0);
1398 tout = STG_DELAY_MAX;
1401 status = bus_space_read_1(iot, ioh, tmc_bstat);
1402 if ((status & PHASE_MASK) != DATA_OUT_PHASE)
1405 if (bus_space_read_2(iot, ioh, tmc_fdcnt) != 0)
1411 for (count = sc->sc_maxwsize; count > 0; count --)
1412 bus_space_write_1(iot, ioh, tmc_wfifo, 0);
1415 status = bus_space_read_1(iot, ioh, tmc_bstat);
1416 if ((status & PHASE_MASK) == DATA_OUT_PHASE)
1417 sc->sc_dataout_timeout = SCSI_LOW_TIMEOUT_HZ;
1419 bus_space_write_1(iot, ioh, tmc_ictl, sc->sc_icinit);