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.4 2003/08/07 21:16:54 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>
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/clock.h>
75 #include <machine/cpu.h>
76 #include <machine/bus_pio.h>
77 #include <machine/bus.h>
79 #include <machine/dvcfg.h>
80 #include <machine/physio_proc.h>
82 #include <bus/cam/scsi/scsi_low.h>
83 #include "tmc18c30reg.h"
84 #include "tmc18c30var.h"
85 #endif /* __FreeBSD__ */
87 /***************************************************
89 ***************************************************/
90 /* DEVICE CONFIGURATION FLAGS (MINOR)
93 * 0x02 PARITY LINE OFF
94 * 0x04 IDENTIFY MSG OFF ( = single lun)
95 * 0x08 SYNC TRANSFER OFF
97 /* #define STG_SYNC_SUPPORT */ /* NOT YET but easy */
99 /* For the 512 fifo type: change below */
100 #define TMC18C30_FIFOSZ 0x800
101 #define TMC18C30_FCBSZ 0x200
102 #define TMC18C50_FIFOSZ 0x2000
103 #define TMC18C50_FCBSZ 0x400
105 #define STG_MAX_DATA_SIZE (64 * 1024)
106 #define STG_DELAY_MAX (2 * 1000 * 1000)
107 #define STG_DELAY_INTERVAL (1)
108 #define STG_DELAY_SELECT_POLLING_MAX (5 * 1000 * 1000)
110 /***************************************************
112 ***************************************************/
113 #define STG_NTARGETS 8
116 /***************************************************
118 ***************************************************/
121 #endif /* STG_DEBUG */
130 #endif /* STG_STATICS */
132 /***************************************************
134 ***************************************************/
135 #define STG_FIFO_INTERRUPTS 0x0001
136 #define STG_WAIT_FOR_SELECT 0x0100
138 int stg_io_control = STG_IO_CONTROL_FLAGS;
140 /***************************************************
142 ***************************************************/
143 extern struct cfdriver stg_cd;
145 /**************************************************************
147 **************************************************************/
149 static void stg_pio_read __P((struct stg_softc *, struct targ_info *, u_int));
150 static void stg_pio_write __P((struct stg_softc *, struct targ_info *, u_int));
151 static int stg_xfer __P((struct stg_softc *, u_int8_t *, int, int, int));
152 static int stg_msg __P((struct stg_softc *, struct targ_info *, u_int));
153 static int stg_reselected __P((struct stg_softc *));
154 static int stg_disconnected __P((struct stg_softc *, struct targ_info *));
155 static __inline void stg_pdma_end __P((struct stg_softc *, struct targ_info *));
156 static int stghw_select_targ_wait __P((struct stg_softc *, int));
157 static int stghw_check __P((struct stg_softc *));
158 static void stghw_init __P((struct stg_softc *));
159 static int stg_negate_signal __P((struct stg_softc *, u_int8_t, u_char *));
160 static int stg_expect_signal __P((struct stg_softc *, u_int8_t, u_int8_t));
161 static int stg_world_start __P((struct stg_softc *, int));
162 static int stghw_start_selection __P((struct stg_softc *sc, struct slccb *));
163 static void stghw_bus_reset __P((struct stg_softc *));
164 static void stghw_attention __P((struct stg_softc *));
165 static int stg_target_nexus_establish __P((struct stg_softc *));
166 static int stg_lun_nexus_establish __P((struct stg_softc *));
167 static int stg_ccb_nexus_establish __P((struct stg_softc *));
168 static int stg_targ_init __P((struct stg_softc *, struct targ_info *, int));
169 static __inline void stghw_bcr_write_1 __P((struct stg_softc *, u_int8_t));
170 static int stg_timeout __P((struct stg_softc *));
171 static void stg_selection_done_and_expect_msgout __P((struct stg_softc *));
173 struct scsi_low_funcs stgfuncs = {
174 SC_LOW_INIT_T stg_world_start,
175 SC_LOW_BUSRST_T stghw_bus_reset,
176 SC_LOW_TARG_INIT_T stg_targ_init,
177 SC_LOW_LUN_INIT_T NULL,
179 SC_LOW_SELECT_T stghw_start_selection,
180 SC_LOW_NEXUS_T stg_lun_nexus_establish,
181 SC_LOW_NEXUS_T stg_ccb_nexus_establish,
183 SC_LOW_ATTEN_T stghw_attention,
184 SC_LOW_MSG_T stg_msg,
186 SC_LOW_TIMEOUT_T stg_timeout,
187 SC_LOW_POLL_T stgintr,
192 /****************************************************
194 ****************************************************/
196 stghw_bcr_write_1(sc, bcv)
197 struct stg_softc *sc;
201 bus_space_write_1(sc->sc_iot, sc->sc_ioh, tmc_bctl, bcv);
207 struct stg_softc *sc;
209 struct scsi_low_softc *slp = &sc->sc_sclow;
210 bus_space_tag_t iot = sc->sc_iot;
211 bus_space_handle_t ioh = sc->sc_ioh;
215 lsb = bus_space_read_1(iot, ioh, tmc_idlsb);
216 msb = bus_space_read_1(iot, ioh, tmc_idmsb);
217 switch (msb << 8 | lsb)
220 /* TMCCHIP_1800 not supported. (it's my policy) */
221 sc->sc_chip = TMCCHIP_1800;
225 if (bus_space_read_1(iot, ioh, tmc_cfg2) & 0x02)
227 sc->sc_chip = TMCCHIP_18C30;
228 sc->sc_fsz = TMC18C30_FIFOSZ;
229 fcbsize = TMC18C30_FCBSZ;
233 sc->sc_chip = TMCCHIP_18C50;
234 sc->sc_fsz = TMC18C50_FIFOSZ;
235 fcbsize = TMC18C50_FCBSZ;
240 sc->sc_chip = TMCCHIP_UNK;
244 sc->sc_fcRinit = FCTL_INTEN;
245 sc->sc_fcWinit = FCTL_PARENB | FCTL_INTEN;
247 if (slp->sl_cfgflags & CFG_NOATTEN)
250 sc->sc_imsg = BCTL_ATN;
251 sc->sc_busc = BCTL_BUSEN;
253 sc->sc_wthold = fcbsize + 256;
254 sc->sc_rthold = fcbsize - 256;
255 sc->sc_maxwsize = sc->sc_fsz;
257 fcb = fcbsize / (sc->sc_fsz / 16);
258 sc->sc_icinit = ICTL_CD | ICTL_SEL | ICTL_ARBIT | fcb;
264 struct stg_softc *sc;
266 bus_space_tag_t iot = sc->sc_iot;
267 bus_space_handle_t ioh = sc->sc_ioh;
269 bus_space_write_1(iot, ioh, tmc_ictl, 0);
270 stghw_bcr_write_1(sc, BCTL_BUSFREE);
271 bus_space_write_1(iot, ioh, tmc_fctl,
272 sc->sc_fcRinit | FCTL_CLRFIFO | FCTL_CLRINT);
273 bus_space_write_1(iot, ioh, tmc_fctl, sc->sc_fcRinit);
274 bus_space_write_1(iot, ioh, tmc_ictl, sc->sc_icinit);
276 bus_space_write_1(iot, ioh, tmc_ssctl, 0);
280 stg_targ_init(sc, ti, action)
281 struct stg_softc *sc;
282 struct targ_info *ti;
285 struct stg_targ_info *sti = (void *) ti;
287 if (action == SCSI_LOW_INFO_ALLOC || action == SCSI_LOW_INFO_REVOKE)
289 ti->ti_width = SCSI_LOW_BUS_WIDTH_8;
290 ti->ti_maxsynch.period = 0;
291 ti->ti_maxsynch.offset = 0;
292 sti->sti_reg_synch = 0;
297 /****************************************************
299 ****************************************************/
302 struct stg_softc *sc;
305 sc->sc_busc |= BCTL_ATN;
306 sc->sc_busimg |= BCTL_ATN;
307 bus_space_write_1(sc->sc_iot, sc->sc_ioh, tmc_bctl, sc->sc_busimg);
313 struct stg_softc *sc;
315 bus_space_tag_t iot = sc->sc_iot;
316 bus_space_handle_t ioh = sc->sc_ioh;
318 bus_space_write_1(iot, ioh, tmc_ictl, 0);
319 bus_space_write_1(iot, ioh, tmc_fctl, 0);
320 stghw_bcr_write_1(sc, BCTL_RST);
321 SCSI_LOW_DELAY(100000);
322 stghw_bcr_write_1(sc, BCTL_BUSFREE);
326 stghw_start_selection(sc, cb)
327 struct stg_softc *sc;
330 bus_space_tag_t iot = sc->sc_iot;
331 bus_space_handle_t ioh = sc->sc_ioh;
332 struct targ_info *ti = cb->ti;
336 sc->sc_tmaxcnt = cb->ccb_tcmax * 1000 * 1000;
337 sc->sc_dataout_timeout = 0;
338 sc->sc_ubf_timeout = 0;
339 stghw_bcr_write_1(sc, BCTL_BUSFREE);
340 bus_space_write_1(iot, ioh, tmc_ictl, sc->sc_icinit);
343 stat = bus_space_read_1(iot, ioh, tmc_astat);
344 if ((stat & ASTAT_INT) != 0)
347 return SCSI_LOW_START_FAIL;
350 bus_space_write_1(iot, ioh, tmc_scsiid, sc->sc_idbit);
351 bus_space_write_1(iot, ioh, tmc_fctl, sc->sc_fcRinit | FCTL_ARBIT);
354 SCSI_LOW_SETUP_PHASE(ti, PH_ARBSTART);
355 return SCSI_LOW_START_OK;
359 stg_world_start(sc, fdone)
360 struct stg_softc *sc;
363 struct scsi_low_softc *slp = &sc->sc_sclow;
366 if ((slp->sl_cfgflags & CFG_NOPARITY) == 0)
367 sc->sc_fcRinit |= FCTL_PARENB;
369 sc->sc_fcRinit &= ~FCTL_PARENB;
371 if ((error = stghw_check(sc)) != 0)
375 scsi_low_bus_reset(slp);
378 SOFT_INTR_REQUIRED(slp);
384 struct stg_softc *sc;
385 struct targ_info *ti;
388 bus_space_tag_t iot = sc->sc_iot;
389 bus_space_handle_t ioh = sc->sc_ioh;
390 struct stg_targ_info *sti = (void *) ti;
391 u_int period, offset;
393 if ((msg & SCSI_LOW_MSG_WIDE) != 0)
395 if (ti->ti_width != SCSI_LOW_BUS_WIDTH_8)
397 ti->ti_width = SCSI_LOW_BUS_WIDTH_8;
403 if ((msg & SCSI_LOW_MSG_SYNCH) == 0)
406 period = ti->ti_maxsynch.period;
407 offset = ti->ti_maxsynch.offset;
408 period = period << 2;
411 sti->sti_reg_synch = (period - 200) / 50;
413 sti->sti_reg_synch ++;
414 sti->sti_reg_synch |= SSCTL_SYNCHEN;
416 else if (period >= 100)
418 sti->sti_reg_synch = (period - 100) / 50;
420 sti->sti_reg_synch ++;
421 sti->sti_reg_synch |= SSCTL_SYNCHEN | SSCTL_FSYNCHEN;
423 bus_space_write_1(iot, ioh, tmc_ssctl, sti->sti_reg_synch);
427 /**************************************************************
428 * General probe attach
429 **************************************************************/
431 stgprobesubr(iot, ioh, dvcfg)
433 bus_space_handle_t ioh;
438 lsb = bus_space_read_1(iot, ioh, tmc_idlsb);
439 msb = bus_space_read_1(iot, ioh, tmc_idmsb);
440 switch (msb << 8 | lsb)
460 printf("%s: scsibus ", name);
466 struct stg_softc *sc;
468 struct scsi_low_softc *slp = &sc->sc_sclow;
472 sc->sc_idbit = (1 << slp->sl_hostid);
473 slp->sl_funcs = &stgfuncs;
474 sc->sc_tmaxcnt = SCSI_LOW_MIN_TOUT * 1000 * 1000; /* default */
476 slp->sl_flags |= HW_READ_PADDING;
477 slp->sl_cfgflags |= CFG_ASYNC; /* XXX */
479 (void) scsi_low_attach(slp, 0, STG_NTARGETS, STG_NLUNS,
480 sizeof(struct stg_targ_info), 0);
483 /**************************************************************
485 **************************************************************/
488 struct stg_softc *sc;
489 struct targ_info *ti;
491 struct scsi_low_softc *slp = &sc->sc_sclow;
492 bus_space_tag_t iot = sc->sc_iot;
493 bus_space_handle_t ioh = sc->sc_ioh;
494 struct slccb *cb = slp->sl_Qnexus;
497 slp->sl_flags &= ~HW_PDMASTART;
498 sc->sc_icinit &= ~ICTL_FIFO;
499 sc->sc_dataout_timeout = 0;
503 slp->sl_error |= PDMAERR;
507 if (ti->ti_phase == PH_DATA)
509 len = bus_space_read_2(iot, ioh, tmc_fdcnt);
510 if (slp->sl_scp.scp_direction == SCSI_LOW_WRITE)
514 tres = len + slp->sl_scp.scp_datalen;
515 if (tres <= (u_int) cb->ccb_scp.scp_datalen)
517 slp->sl_scp.scp_data -= len;
518 slp->sl_scp.scp_datalen = tres;
522 slp->sl_error |= PDMAERR;
523 printf("%s len %x >= datalen %x\n",
525 len, slp->sl_scp.scp_datalen);
529 else if (slp->sl_scp.scp_direction == SCSI_LOW_READ)
533 slp->sl_error |= PDMAERR;
534 printf("%s: len %x left in fifo\n",
538 scsi_low_data_finish(slp);
543 printf("%s data phase miss\n", slp->sl_xname);
544 slp->sl_error |= PDMAERR;
548 bus_space_write_1(iot, ioh, tmc_fctl, sc->sc_fcRinit);
552 stg_pio_read(sc, ti, thold)
553 struct stg_softc *sc;
554 struct targ_info *ti;
557 struct scsi_low_softc *slp = &sc->sc_sclow;
558 bus_space_tag_t iot = sc->sc_iot;
559 bus_space_handle_t ioh = sc->sc_ioh;
560 struct sc_p *sp = &slp->sl_scp;
565 if ((slp->sl_flags & HW_PDMASTART) == 0)
567 bus_space_write_1(iot, ioh, tmc_fctl,
568 sc->sc_fcRinit | FCTL_FIFOEN);
569 slp->sl_flags |= HW_PDMASTART;
572 tout = sc->sc_tmaxcnt;
578 res = bus_space_read_2(iot, ioh, tmc_fdcnt);
581 bus_space_write_1(iot, ioh, tmc_ictl,
590 stat = bus_space_read_1(iot, ioh, tmc_bstat);
591 res = bus_space_read_2(iot, ioh, tmc_fdcnt);
594 if ((stat & PHASE_MASK) != DATA_IN_PHASE)
596 if (sp->scp_datalen <= 0)
603 /* The assumtion res != 0 is valid here */
604 if (res > sp->scp_datalen)
606 if (res == (u_int) -1)
609 slp->sl_error |= PDMAERR;
610 if ((slp->sl_flags & HW_READ_PADDING) == 0)
612 printf("%s: read padding required\n",
618 if (res > STG_MAX_DATA_SIZE)
619 res = STG_MAX_DATA_SIZE;
622 (void) bus_space_read_1(iot, ioh, tmc_rfifo);
627 sp->scp_datalen -= res;
630 *sp->scp_data = bus_space_read_1(iot, ioh, tmc_rfifo);
635 bus_space_read_multi_2(iot, ioh, tmc_rfifo,
636 (u_int16_t *) sp->scp_data, res >> 1);
641 printf("%s: pio read timeout\n", slp->sl_xname);
645 stg_pio_write(sc, ti, thold)
646 struct stg_softc *sc;
647 struct targ_info *ti;
650 struct scsi_low_softc *slp = &sc->sc_sclow;
651 bus_space_tag_t iot = sc->sc_iot;
652 bus_space_handle_t ioh = sc->sc_ioh;
653 struct sc_p *sp = &slp->sl_scp;
658 if ((slp->sl_flags & HW_PDMASTART) == 0)
660 stat = sc->sc_fcWinit | FCTL_FIFOEN | FCTL_FIFOW;
661 bus_space_write_1(iot, ioh, tmc_fctl, stat | FCTL_CLRFIFO);
662 bus_space_write_1(iot, ioh, tmc_fctl, stat);
663 slp->sl_flags |= HW_PDMASTART;
666 tout = sc->sc_tmaxcnt;
669 stat = bus_space_read_1(iot, ioh, tmc_bstat);
670 if ((stat & PHASE_MASK) != DATA_OUT_PHASE)
673 if (sp->scp_datalen <= 0)
675 if (sc->sc_dataout_timeout == 0)
676 sc->sc_dataout_timeout = SCSI_LOW_TIMEOUT_HZ;
683 res = bus_space_read_2(iot, ioh, tmc_fdcnt);
686 bus_space_write_1(iot, ioh, tmc_ictl,
695 res = bus_space_read_2(iot, ioh, tmc_fdcnt);
696 if (res > sc->sc_maxwsize / 2)
703 if (res == (u_int) -1)
705 res = sc->sc_maxwsize - res;
706 if (res > sp->scp_datalen)
707 res = sp->scp_datalen;
709 sp->scp_datalen -= res;
710 if ((res & 0x1) != 0)
712 bus_space_write_1(iot, ioh, tmc_wfifo, *sp->scp_data);
717 bus_space_write_multi_2(iot, ioh, tmc_wfifo,
718 (u_int16_t *) sp->scp_data, res >> 1);
723 printf("%s: pio write timeout\n", slp->sl_xname);
727 stg_negate_signal(sc, mask, s)
728 struct stg_softc *sc;
732 struct scsi_low_softc *slp = &sc->sc_sclow;
733 bus_space_tag_t bst = sc->sc_iot;
734 bus_space_handle_t bsh = sc->sc_ioh;
738 for (wc = 0; wc < STG_DELAY_MAX / STG_DELAY_INTERVAL; wc ++)
740 regv = bus_space_read_1(bst, bsh, tmc_bstat);
741 if (regv == (u_int8_t) -1)
743 if ((regv & mask) == 0)
746 SCSI_LOW_DELAY(STG_DELAY_INTERVAL);
749 printf("%s: %s stg_negate_signal timeout\n", slp->sl_xname, s);
754 stg_expect_signal(sc, phase, mask)
755 struct stg_softc *sc;
756 u_int8_t phase, mask;
758 struct scsi_low_softc *slp = &sc->sc_sclow;
759 bus_space_tag_t bst = sc->sc_iot;
760 bus_space_handle_t bsh = sc->sc_ioh;
765 for (wc = 0; wc < STG_DELAY_MAX / STG_DELAY_INTERVAL; wc ++)
767 ph = bus_space_read_1(bst, bsh, tmc_bstat);
768 if (ph == (u_int8_t) -1)
770 if ((ph & PHASE_MASK) != phase)
772 if ((ph & mask) != 0)
775 SCSI_LOW_DELAY(STG_DELAY_INTERVAL);
778 printf("%s: stg_expect_signal timeout\n", slp->sl_xname);
783 stg_xfer(sc, buf, len, phase, clear_atn)
784 struct stg_softc *sc;
790 bus_space_tag_t iot = sc->sc_iot;
791 bus_space_handle_t ioh = sc->sc_ioh;
794 if (phase & BSTAT_IO)
795 bus_space_write_1(iot, ioh, tmc_fctl, sc->sc_fcRinit);
797 bus_space_write_1(iot, ioh, tmc_fctl, sc->sc_fcWinit);
799 for (ptr = 0; len > 0; len --)
801 rv = stg_expect_signal(sc, phase, BSTAT_REQ);
805 if (len == 1 && clear_atn != 0)
807 sc->sc_busc &= ~BCTL_ATN;
808 stghw_bcr_write_1(sc, sc->sc_busc);
809 SCSI_LOW_DEASSERT_ATN(&sc->sc_sclow);
812 if (phase & BSTAT_IO)
814 buf[ptr ++] = bus_space_read_1(iot, ioh, tmc_rdata);
818 bus_space_write_1(iot, ioh, tmc_wdata, buf[ptr ++]);
821 stg_negate_signal(sc, BSTAT_ACK, "xfer<ACK>");
825 bus_space_write_1(iot, ioh, tmc_fctl, sc->sc_fcRinit);
829 /**************************************************************
830 * disconnect & reselect (HW low)
831 **************************************************************/
834 struct stg_softc *sc;
836 struct scsi_low_softc *slp = &sc->sc_sclow;
837 bus_space_tag_t iot = sc->sc_iot;
838 bus_space_handle_t ioh = sc->sc_ioh;
843 if (slp->sl_selid != NULL)
846 * Selection vs Reselection conflicts.
848 bus_space_write_1(iot, ioh, tmc_fctl, sc->sc_fcRinit);
849 stghw_bcr_write_1(sc, BCTL_BUSFREE);
851 else if (slp->sl_Tnexus != NULL)
853 printf("%s: unexpected termination\n", slp->sl_xname);
854 stg_disconnected(sc, slp->sl_Tnexus);
858 * We should ack the reselection as soon as possible,
859 * becuase the target would abort the current reselection seq
860 * due to reselection timeout.
862 tout = STG_DELAY_SELECT_POLLING_MAX;
865 regv = bus_space_read_1(iot, ioh, tmc_bstat);
866 if ((regv & (BSTAT_IO | BSTAT_SEL | BSTAT_BSY)) ==
867 (BSTAT_IO | BSTAT_SEL))
870 regv = bus_space_read_1(iot, ioh, tmc_bstat);
871 if ((regv & (BSTAT_IO | BSTAT_SEL | BSTAT_BSY)) ==
872 (BSTAT_IO | BSTAT_SEL))
877 printf("%s: reselction timeout I\n", slp->sl_xname);
881 sid = (u_int) bus_space_read_1(iot, ioh, tmc_scsiid);
882 if ((sid & sc->sc_idbit) == 0)
888 bus_space_write_1(iot, ioh, tmc_fctl,
889 sc->sc_fcRinit | FCTL_CLRFIFO | FCTL_CLRINT);
890 bus_space_write_1(iot, ioh, tmc_fctl, sc->sc_fcRinit);
891 stghw_bcr_write_1(sc, sc->sc_busc | BCTL_BSY);
895 regv = bus_space_read_1(iot, ioh, tmc_bstat);
896 if ((regv & (BSTAT_SEL | BSTAT_BSY)) == BSTAT_BSY)
900 printf("%s: reselction timeout II\n", slp->sl_xname);
904 sid &= ~sc->sc_idbit;
906 if (scsi_low_reselected(slp, sid) == NULL)
910 stg_statics.reselect ++;
911 #endif /* STG_STATICS */
916 stg_disconnected(sc, ti)
917 struct stg_softc *sc;
918 struct targ_info *ti;
920 struct scsi_low_softc *slp = &sc->sc_sclow;
921 bus_space_tag_t iot = sc->sc_iot;
922 bus_space_handle_t ioh = sc->sc_ioh;
924 /* clear bus status & fifo */
925 bus_space_write_1(iot, ioh, tmc_fctl, sc->sc_fcRinit | FCTL_CLRFIFO);
926 bus_space_write_1(iot, ioh, tmc_fctl, sc->sc_fcRinit);
927 stghw_bcr_write_1(sc, BCTL_BUSFREE);
928 sc->sc_icinit &= ~ICTL_FIFO;
929 sc->sc_busc &= ~BCTL_ATN;
930 sc->sc_dataout_timeout = 0;
931 sc->sc_ubf_timeout = 0;
934 stg_statics.disconnect ++;
935 #endif /* STG_STATICS */
936 scsi_low_disconnected(slp, ti);
940 /**************************************************************
942 **************************************************************/
944 stg_target_nexus_establish(sc)
945 struct stg_softc *sc;
947 struct scsi_low_softc *slp = &sc->sc_sclow;
948 bus_space_tag_t iot = sc->sc_iot;
949 bus_space_handle_t ioh = sc->sc_ioh;
950 struct targ_info *ti = slp->sl_Tnexus;
951 struct stg_targ_info *sti = (void *) ti;
953 bus_space_write_1(iot, ioh, tmc_ssctl, sti->sti_reg_synch);
954 if ((stg_io_control & STG_FIFO_INTERRUPTS) != 0)
956 sc->sc_icinit |= ICTL_FIFO;
962 stg_lun_nexus_establish(sc)
963 struct stg_softc *sc;
970 stg_ccb_nexus_establish(sc)
971 struct stg_softc *sc;
973 struct scsi_low_softc *slp = &sc->sc_sclow;
974 struct slccb *cb = slp->sl_Qnexus;
976 sc->sc_tmaxcnt = cb->ccb_tcmax * 1000 * 1000;
980 #define STGHW_SELECT_INTERVAL 10
983 stghw_select_targ_wait(sc, mu)
984 struct stg_softc *sc;
987 bus_space_tag_t iot = sc->sc_iot;
988 bus_space_handle_t ioh = sc->sc_ioh;
990 mu = mu / STGHW_SELECT_INTERVAL;
993 if ((bus_space_read_1(iot, ioh, tmc_bstat) & BSTAT_BSY) == 0)
995 SCSI_LOW_DELAY(STGHW_SELECT_INTERVAL);
999 if ((bus_space_read_1(iot, ioh, tmc_bstat) & BSTAT_BSY) != 0)
1008 stg_selection_done_and_expect_msgout(sc)
1009 struct stg_softc *sc;
1011 struct scsi_low_softc *slp = &sc->sc_sclow;
1012 bus_space_tag_t iot = sc->sc_iot;
1013 bus_space_handle_t ioh = sc->sc_ioh;
1015 bus_space_write_1(iot, ioh, tmc_fctl, sc->sc_fcRinit | FCTL_CLRFIFO);
1016 bus_space_write_1(iot, ioh, tmc_fctl, sc->sc_fcRinit);
1017 stghw_bcr_write_1(sc, sc->sc_imsg | sc->sc_busc);
1018 SCSI_LOW_ASSERT_ATN(slp);
1025 struct stg_softc *sc = arg;
1026 struct scsi_low_softc *slp = &sc->sc_sclow;
1027 bus_space_tag_t iot = sc->sc_iot;
1028 bus_space_handle_t ioh = sc->sc_ioh;
1029 struct targ_info *ti;
1030 struct physio_proc *pp;
1032 u_int derror, flags;
1034 u_int8_t status, astatus, regv;
1036 /*******************************************
1038 *******************************************/
1039 if (slp->sl_flags & HW_INACTIVE)
1042 astatus = bus_space_read_1(iot, ioh, tmc_astat);
1043 status = bus_space_read_1(iot, ioh, tmc_bstat);
1045 if ((astatus & ASTAT_STATMASK) == 0 || astatus == (u_int8_t) -1)
1048 bus_space_write_1(iot, ioh, tmc_ictl, 0);
1049 if (astatus & ASTAT_SCSIRST)
1051 bus_space_write_1(iot, ioh, tmc_fctl,
1052 sc->sc_fcRinit | FCTL_CLRFIFO);
1053 bus_space_write_1(iot, ioh, tmc_fctl, sc->sc_fcRinit);
1054 bus_space_write_1(iot, ioh, tmc_ictl, 0);
1056 scsi_low_restart(slp, SCSI_LOW_RESTART_SOFT,
1057 "bus reset (power off?)");
1061 /*******************************************
1063 *******************************************/
1067 scsi_low_print(slp, NULL);
1068 printf("%s: st %x ist %x\n\n", slp->sl_xname,
1072 SCSI_LOW_DEBUGGER("stg");
1075 #endif /* STG_DEBUG */
1077 /*******************************************
1078 * reselection & nexus
1079 *******************************************/
1080 if ((status & RESEL_PHASE_MASK)== PHASE_RESELECTED)
1082 if (stg_reselected(sc) == EJUSTRETURN)
1086 if ((ti = slp->sl_Tnexus) == NULL)
1090 if ((astatus & ASTAT_PARERR) != 0 && ti->ti_phase != PH_ARBSTART &&
1091 (sc->sc_fcRinit & FCTL_PARENB) != 0)
1093 slp->sl_error |= PARITYERR;
1094 derror = SCSI_LOW_DATA_PE;
1095 if ((status & PHASE_MASK) == MESSAGE_IN_PHASE)
1096 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_PARITY, 0);
1098 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_ERROR, 1);
1101 /*******************************************
1102 * aribitration & selection
1103 *******************************************/
1104 switch (ti->ti_phase)
1107 if ((astatus & ASTAT_ARBIT) == 0)
1110 stg_statics.arbit_fail_0 ++;
1111 #endif /* STG_STATICS */
1115 status = bus_space_read_1(iot, ioh, tmc_bstat);
1116 if ((status & BSTAT_IO) != 0)
1119 * Selection vs Reselection conflicts.
1122 stg_statics.arbit_fail_1 ++;
1123 #endif /* STG_STATICS */
1125 bus_space_write_1(iot, ioh, tmc_fctl, sc->sc_fcRinit);
1126 stghw_bcr_write_1(sc, BCTL_BUSFREE);
1127 scsi_low_arbit_fail(slp, slp->sl_Qnexus);
1132 * selection assert start.
1134 SCSI_LOW_SETUP_PHASE(ti, PH_SELSTART);
1135 scsi_low_arbit_win(slp);
1138 bus_space_write_1(iot, ioh, tmc_scsiid,
1139 sc->sc_idbit | (1 << ti->ti_id));
1140 stghw_bcr_write_1(sc, sc->sc_imsg | sc->sc_busc | BCTL_SEL);
1141 bus_space_write_1(iot, ioh, tmc_fctl, sc->sc_fcWinit);
1142 if ((stg_io_control & STG_WAIT_FOR_SELECT) != 0)
1144 /* selection abort delay 200 + 100 micro sec */
1145 if (stghw_select_targ_wait(sc, 300) == 0)
1147 SCSI_LOW_SETUP_PHASE(ti, PH_SELECTED);
1148 stg_selection_done_and_expect_msgout(sc);
1155 if ((status & BSTAT_BSY) == 0)
1157 /* selection timeout delay 250 ms */
1158 if (stghw_select_targ_wait(sc, 250 * 1000) != 0)
1160 stg_disconnected(sc, ti);
1165 SCSI_LOW_SETUP_PHASE(ti, PH_SELECTED);
1166 stg_selection_done_and_expect_msgout(sc);
1170 if ((status & BSTAT_REQ) == 0)
1172 stg_target_nexus_establish(sc);
1176 if ((status & BSTAT_REQ) == 0)
1179 /* clear a busy line */
1180 bus_space_write_1(iot, ioh, tmc_fctl, sc->sc_fcRinit);
1181 stghw_bcr_write_1(sc, sc->sc_busc);
1182 stg_target_nexus_establish(sc);
1183 if ((status & PHASE_MASK) != MESSAGE_IN_PHASE)
1185 printf("%s: unexpected phase after reselect\n",
1187 slp->sl_error |= FATALIO;
1188 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_ABORT, 1);
1194 /*******************************************
1196 *******************************************/
1197 if ((slp->sl_flags & HW_PDMASTART) && STG_IS_PHASE_DATA(status) == 0)
1199 if (slp->sl_scp.scp_direction == SCSI_LOW_READ)
1200 stg_pio_read(sc, ti, 0);
1202 stg_pdma_end(sc, ti);
1205 /*******************************************
1207 *******************************************/
1208 switch (status & PHASE_MASK)
1211 if (stg_expect_signal(sc, COMMAND_PHASE, BSTAT_REQ) <= 0)
1214 SCSI_LOW_SETUP_PHASE(ti, PH_CMD);
1215 if (scsi_low_cmd(slp, ti) != 0)
1217 scsi_low_attention(slp);
1220 if (stg_xfer(sc, slp->sl_scp.scp_cmd, slp->sl_scp.scp_cmdlen,
1221 COMMAND_PHASE, 0) != 0)
1223 printf("%s: CMDOUT short\n", slp->sl_xname);
1227 case DATA_OUT_PHASE:
1228 SCSI_LOW_SETUP_PHASE(ti, PH_DATA);
1229 if (scsi_low_data(slp, ti, &bp, SCSI_LOW_WRITE) != 0)
1231 scsi_low_attention(slp);
1234 pp = physio_proc_enter(bp);
1235 if ((sc->sc_icinit & ICTL_FIFO) != 0)
1236 stg_pio_write(sc, ti, sc->sc_wthold);
1238 stg_pio_write(sc, ti, 0);
1239 physio_proc_leave(pp);
1243 SCSI_LOW_SETUP_PHASE(ti, PH_DATA);
1244 if (scsi_low_data(slp, ti, &bp, SCSI_LOW_READ) != 0)
1246 scsi_low_attention(slp);
1249 pp = physio_proc_enter(bp);
1250 if ((sc->sc_icinit & ICTL_FIFO) != 0)
1251 stg_pio_read(sc, ti, sc->sc_rthold);
1253 stg_pio_read(sc, ti, 0);
1254 physio_proc_leave(pp);
1258 regv = stg_expect_signal(sc, STATUS_PHASE, BSTAT_REQ);
1262 SCSI_LOW_SETUP_PHASE(ti, PH_STAT);
1263 regv = bus_space_read_1(iot, ioh, tmc_sdna);
1264 if (scsi_low_statusin(slp, ti, regv | derror) != 0)
1266 scsi_low_attention(slp);
1268 if (regv != bus_space_read_1(iot, ioh, tmc_rdata))
1270 printf("%s: STATIN: data mismatch\n", slp->sl_xname);
1272 stg_negate_signal(sc, BSTAT_ACK, "statin<ACK>");
1275 case MESSAGE_OUT_PHASE:
1276 if (stg_expect_signal(sc, MESSAGE_OUT_PHASE, BSTAT_REQ) <= 0)
1279 SCSI_LOW_SETUP_PHASE(ti, PH_MSGOUT);
1280 flags = (ti->ti_ophase != ti->ti_phase) ?
1281 SCSI_LOW_MSGOUT_INIT : 0;
1282 len = scsi_low_msgout(slp, ti, flags);
1284 if (len > 1 && slp->sl_atten == 0)
1286 scsi_low_attention(slp);
1289 if (stg_xfer(sc, ti->ti_msgoutstr, len, MESSAGE_OUT_PHASE,
1290 slp->sl_clear_atten) != 0)
1292 printf("%s: MSGOUT short\n", slp->sl_xname);
1296 if (slp->sl_msgphase >= MSGPH_ABORT)
1298 stg_disconnected(sc, ti);
1303 case MESSAGE_IN_PHASE:
1304 /* confirm phase and req signal */
1305 if (stg_expect_signal(sc, MESSAGE_IN_PHASE, BSTAT_REQ) <= 0)
1308 SCSI_LOW_SETUP_PHASE(ti, PH_MSGIN);
1310 /* read data with NOACK */
1311 regv = bus_space_read_1(iot, ioh, tmc_sdna);
1313 if (scsi_low_msgin(slp, ti, derror | regv) == 0)
1315 if (scsi_low_is_msgout_continue(ti, 0) != 0)
1317 scsi_low_attention(slp);
1321 /* read data with ACK */
1322 if (regv != bus_space_read_1(iot, ioh, tmc_rdata))
1324 printf("%s: MSGIN: data mismatch\n", slp->sl_xname);
1327 /* wait for the ack negated */
1328 stg_negate_signal(sc, BSTAT_ACK, "msgin<ACK>");
1330 if (slp->sl_msgphase != 0 && slp->sl_msgphase < MSGPH_ABORT)
1332 stg_disconnected(sc, ti);
1337 printf("%s: unexpected disconnect\n", slp->sl_xname);
1338 stg_disconnected(sc, ti);
1342 slp->sl_error |= FATALIO;
1343 printf("%s: unknown phase bus %x intr %x\n",
1344 slp->sl_xname, status, astatus);
1349 bus_space_write_1(iot, ioh, tmc_ictl, sc->sc_icinit);
1355 struct stg_softc *sc;
1357 struct scsi_low_softc *slp = &sc->sc_sclow;
1358 bus_space_tag_t iot = sc->sc_iot;
1359 bus_space_handle_t ioh = sc->sc_ioh;
1363 if (slp->sl_Tnexus == NULL)
1366 status = bus_space_read_1(iot, ioh, tmc_bstat);
1367 if ((status & PHASE_MASK) == 0)
1369 if (sc->sc_ubf_timeout ++ == 0)
1372 printf("%s: unexpected bus free detected\n", slp->sl_xname);
1373 slp->sl_error |= FATALIO;
1374 scsi_low_print(slp, slp->sl_Tnexus);
1375 stg_disconnected(sc, slp->sl_Tnexus);
1379 switch (status & PHASE_MASK)
1381 case DATA_OUT_PHASE:
1382 if (sc->sc_dataout_timeout == 0)
1384 if ((status & BSTAT_REQ) == 0)
1386 if (bus_space_read_2(iot, ioh, tmc_fdcnt) != 0)
1388 if ((-- sc->sc_dataout_timeout) > 0)
1391 slp->sl_error |= PDMAERR;
1392 if ((slp->sl_flags & HW_WRITE_PADDING) == 0)
1394 printf("%s: write padding required\n",
1399 bus_space_write_1(iot, ioh, tmc_ictl, 0);
1401 tout = STG_DELAY_MAX;
1404 status = bus_space_read_1(iot, ioh, tmc_bstat);
1405 if ((status & PHASE_MASK) != DATA_OUT_PHASE)
1408 if (bus_space_read_2(iot, ioh, tmc_fdcnt) != 0)
1414 for (count = sc->sc_maxwsize; count > 0; count --)
1415 bus_space_write_1(iot, ioh, tmc_wfifo, 0);
1418 status = bus_space_read_1(iot, ioh, tmc_bstat);
1419 if ((status & PHASE_MASK) == DATA_OUT_PHASE)
1420 sc->sc_dataout_timeout = SCSI_LOW_TIMEOUT_HZ;
1422 bus_space_write_1(iot, ioh, tmc_ictl, sc->sc_icinit);