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>
49 #include <sys/queue.h>
50 #include <sys/malloc.h>
51 #include <sys/errno.h>
53 #include <machine/cpu.h>
54 #include <machine/bus.h>
56 #include <cam/scsi/scsi_low.h>
57 #include <dev/stg/tmc18c30reg.h>
58 #include <dev/stg/tmc18c30var.h>
60 /***************************************************
62 ***************************************************/
63 /* DEVICE CONFIGURATION FLAGS (MINOR)
66 * 0x02 PARITY LINE OFF
67 * 0x04 IDENTIFY MSG OFF ( = single lun)
68 * 0x08 SYNC TRANSFER OFF
70 /* #define STG_SYNC_SUPPORT */ /* NOT YET but easy */
72 /* For the 512 fifo type: change below */
73 #define TMC18C30_FIFOSZ 0x800
74 #define TMC18C30_FCBSZ 0x200
75 #define TMC18C50_FIFOSZ 0x2000
76 #define TMC18C50_FCBSZ 0x400
78 #define STG_MAX_DATA_SIZE (64 * 1024)
79 #define STG_DELAY_MAX (2 * 1000 * 1000)
80 #define STG_DELAY_INTERVAL (1)
81 #define STG_DELAY_SELECT_POLLING_MAX (5 * 1000 * 1000)
83 /***************************************************
85 ***************************************************/
86 #define STG_NTARGETS 8
89 /***************************************************
91 ***************************************************/
94 #endif /* STG_DEBUG */
97 static struct stg_statics {
103 #endif /* STG_STATICS */
105 /***************************************************
107 ***************************************************/
108 #define STG_FIFO_INTERRUPTS 0x0001
109 #define STG_WAIT_FOR_SELECT 0x0100
111 int stg_io_control = STG_IO_CONTROL_FLAGS;
113 /***************************************************
115 ***************************************************/
116 extern struct cfdriver stg_cd;
118 /**************************************************************
120 **************************************************************/
122 static void stg_pio_read(struct stg_softc *, struct targ_info *, u_int);
123 static void stg_pio_write(struct stg_softc *, struct targ_info *, u_int);
124 static int stg_xfer(struct stg_softc *, u_int8_t *, int, int, int);
125 static int stg_msg(struct stg_softc *, struct targ_info *, u_int);
126 static int stg_reselected(struct stg_softc *);
127 static int stg_disconnected(struct stg_softc *, struct targ_info *);
128 static __inline void stg_pdma_end(struct stg_softc *, struct targ_info *);
129 static int stghw_select_targ_wait(struct stg_softc *, int);
130 static int stghw_check(struct stg_softc *);
131 static void stghw_init(struct stg_softc *);
132 static int stg_negate_signal(struct stg_softc *, u_int8_t, u_char *);
133 static int stg_expect_signal(struct stg_softc *, u_int8_t, u_int8_t);
134 static int stg_world_start(struct stg_softc *, int);
135 static int stghw_start_selection(struct stg_softc *sc, struct slccb *);
136 static void stghw_bus_reset(struct stg_softc *);
137 static void stghw_attention(struct stg_softc *);
138 static int stg_target_nexus_establish(struct stg_softc *);
139 static int stg_lun_nexus_establish(struct stg_softc *);
140 static int stg_ccb_nexus_establish(struct stg_softc *);
141 static int stg_targ_init(struct stg_softc *, struct targ_info *, int);
142 static __inline void stghw_bcr_write_1(struct stg_softc *, u_int8_t);
143 static int stg_timeout(struct stg_softc *);
144 static void stg_selection_done_and_expect_msgout(struct stg_softc *);
146 struct scsi_low_funcs stgfuncs = {
147 SC_LOW_INIT_T stg_world_start,
148 SC_LOW_BUSRST_T stghw_bus_reset,
149 SC_LOW_TARG_INIT_T stg_targ_init,
150 SC_LOW_LUN_INIT_T NULL,
152 SC_LOW_SELECT_T stghw_start_selection,
153 SC_LOW_NEXUS_T stg_lun_nexus_establish,
154 SC_LOW_NEXUS_T stg_ccb_nexus_establish,
156 SC_LOW_ATTEN_T stghw_attention,
157 SC_LOW_MSG_T stg_msg,
159 SC_LOW_TIMEOUT_T stg_timeout,
160 SC_LOW_POLL_T stgintr,
165 /****************************************************
167 ****************************************************/
169 stghw_bcr_write_1(struct stg_softc *sc, u_int8_t bcv)
172 bus_space_write_1(sc->sc_iot, sc->sc_ioh, tmc_bctl, bcv);
178 struct stg_softc *sc;
180 struct scsi_low_softc *slp = &sc->sc_sclow;
181 bus_space_tag_t iot = sc->sc_iot;
182 bus_space_handle_t ioh = sc->sc_ioh;
186 lsb = bus_space_read_1(iot, ioh, tmc_idlsb);
187 msb = bus_space_read_1(iot, ioh, tmc_idmsb);
188 switch (msb << 8 | lsb)
191 /* TMCCHIP_1800 not supported. (it's my policy) */
192 sc->sc_chip = TMCCHIP_1800;
196 if (bus_space_read_1(iot, ioh, tmc_cfg2) & 0x02)
198 sc->sc_chip = TMCCHIP_18C30;
199 sc->sc_fsz = TMC18C30_FIFOSZ;
200 fcbsize = TMC18C30_FCBSZ;
204 sc->sc_chip = TMCCHIP_18C50;
205 sc->sc_fsz = TMC18C50_FIFOSZ;
206 fcbsize = TMC18C50_FCBSZ;
211 sc->sc_chip = TMCCHIP_UNK;
215 sc->sc_fcRinit = FCTL_INTEN;
216 sc->sc_fcWinit = FCTL_PARENB | FCTL_INTEN;
218 if (slp->sl_cfgflags & CFG_NOATTEN)
221 sc->sc_imsg = BCTL_ATN;
222 sc->sc_busc = BCTL_BUSEN;
224 sc->sc_wthold = fcbsize + 256;
225 sc->sc_rthold = fcbsize - 256;
226 sc->sc_maxwsize = sc->sc_fsz;
228 fcb = fcbsize / (sc->sc_fsz / 16);
229 sc->sc_icinit = ICTL_CD | ICTL_SEL | ICTL_ARBIT | fcb;
235 struct stg_softc *sc;
237 bus_space_tag_t iot = sc->sc_iot;
238 bus_space_handle_t ioh = sc->sc_ioh;
240 bus_space_write_1(iot, ioh, tmc_ictl, 0);
241 stghw_bcr_write_1(sc, BCTL_BUSFREE);
242 bus_space_write_1(iot, ioh, tmc_fctl,
243 sc->sc_fcRinit | FCTL_CLRFIFO | FCTL_CLRINT);
244 bus_space_write_1(iot, ioh, tmc_fctl, sc->sc_fcRinit);
245 bus_space_write_1(iot, ioh, tmc_ictl, sc->sc_icinit);
247 bus_space_write_1(iot, ioh, tmc_ssctl, 0);
251 stg_targ_init(sc, ti, action)
252 struct stg_softc *sc;
253 struct targ_info *ti;
256 struct stg_targ_info *sti = (void *) ti;
258 if (action == SCSI_LOW_INFO_ALLOC || action == SCSI_LOW_INFO_REVOKE)
260 ti->ti_width = SCSI_LOW_BUS_WIDTH_8;
261 ti->ti_maxsynch.period = 0;
262 ti->ti_maxsynch.offset = 0;
263 sti->sti_reg_synch = 0;
268 /****************************************************
270 ****************************************************/
273 struct stg_softc *sc;
276 sc->sc_busc |= BCTL_ATN;
277 sc->sc_busimg |= BCTL_ATN;
278 bus_space_write_1(sc->sc_iot, sc->sc_ioh, tmc_bctl, sc->sc_busimg);
284 struct stg_softc *sc;
286 bus_space_tag_t iot = sc->sc_iot;
287 bus_space_handle_t ioh = sc->sc_ioh;
289 bus_space_write_1(iot, ioh, tmc_ictl, 0);
290 bus_space_write_1(iot, ioh, tmc_fctl, 0);
291 stghw_bcr_write_1(sc, BCTL_RST);
293 stghw_bcr_write_1(sc, BCTL_BUSFREE);
297 stghw_start_selection(sc, cb)
298 struct stg_softc *sc;
301 bus_space_tag_t iot = sc->sc_iot;
302 bus_space_handle_t ioh = sc->sc_ioh;
303 struct targ_info *ti = cb->ti;
304 register u_int8_t stat;
307 sc->sc_tmaxcnt = cb->ccb_tcmax * 1000 * 1000;
308 sc->sc_dataout_timeout = 0;
309 sc->sc_ubf_timeout = 0;
310 stghw_bcr_write_1(sc, BCTL_BUSFREE);
311 bus_space_write_1(iot, ioh, tmc_ictl, sc->sc_icinit);
314 stat = bus_space_read_1(iot, ioh, tmc_astat);
315 if ((stat & ASTAT_INT) != 0)
318 return SCSI_LOW_START_FAIL;
321 bus_space_write_1(iot, ioh, tmc_scsiid, sc->sc_idbit);
322 bus_space_write_1(iot, ioh, tmc_fctl, sc->sc_fcRinit | FCTL_ARBIT);
325 SCSI_LOW_SETUP_PHASE(ti, PH_ARBSTART);
326 return SCSI_LOW_START_OK;
330 stg_world_start(sc, fdone)
331 struct stg_softc *sc;
334 struct scsi_low_softc *slp = &sc->sc_sclow;
337 if ((slp->sl_cfgflags & CFG_NOPARITY) == 0)
338 sc->sc_fcRinit |= FCTL_PARENB;
340 sc->sc_fcRinit &= ~FCTL_PARENB;
342 if ((error = stghw_check(sc)) != 0)
346 scsi_low_bus_reset(slp);
354 struct stg_softc *sc;
355 struct targ_info *ti;
358 bus_space_tag_t iot = sc->sc_iot;
359 bus_space_handle_t ioh = sc->sc_ioh;
360 struct stg_targ_info *sti = (void *) ti;
361 u_int period, offset;
363 if ((msg & SCSI_LOW_MSG_WIDE) != 0)
365 if (ti->ti_width != SCSI_LOW_BUS_WIDTH_8)
367 ti->ti_width = SCSI_LOW_BUS_WIDTH_8;
373 if ((msg & SCSI_LOW_MSG_SYNCH) == 0)
376 period = ti->ti_maxsynch.period;
377 offset = ti->ti_maxsynch.offset;
378 period = period << 2;
381 sti->sti_reg_synch = (period - 200) / 50;
383 sti->sti_reg_synch ++;
384 sti->sti_reg_synch |= SSCTL_SYNCHEN;
386 else if (period >= 100)
388 sti->sti_reg_synch = (period - 100) / 50;
390 sti->sti_reg_synch ++;
391 sti->sti_reg_synch |= SSCTL_SYNCHEN | SSCTL_FSYNCHEN;
393 bus_space_write_1(iot, ioh, tmc_ssctl, sti->sti_reg_synch);
397 /**************************************************************
398 * General probe attach
399 **************************************************************/
401 stgprobesubr(iot, ioh, dvcfg)
403 bus_space_handle_t ioh;
408 lsb = bus_space_read_1(iot, ioh, tmc_idlsb);
409 msb = bus_space_read_1(iot, ioh, tmc_idmsb);
410 switch (msb << 8 | lsb)
425 struct stg_softc *sc;
427 struct scsi_low_softc *slp = &sc->sc_sclow;
431 sc->sc_idbit = (1 << slp->sl_hostid);
432 slp->sl_funcs = &stgfuncs;
433 sc->sc_tmaxcnt = SCSI_LOW_MIN_TOUT * 1000 * 1000; /* default */
435 slp->sl_flags |= HW_READ_PADDING;
436 slp->sl_cfgflags |= CFG_ASYNC; /* XXX */
438 (void) scsi_low_attach(slp, 0, STG_NTARGETS, STG_NLUNS,
439 sizeof(struct stg_targ_info), 0);
442 /**************************************************************
444 **************************************************************/
447 struct stg_softc *sc;
448 struct targ_info *ti;
450 struct scsi_low_softc *slp = &sc->sc_sclow;
451 bus_space_tag_t iot = sc->sc_iot;
452 bus_space_handle_t ioh = sc->sc_ioh;
453 struct slccb *cb = slp->sl_Qnexus;
456 slp->sl_flags &= ~HW_PDMASTART;
457 sc->sc_icinit &= ~ICTL_FIFO;
458 sc->sc_dataout_timeout = 0;
462 slp->sl_error |= PDMAERR;
466 if (ti->ti_phase == PH_DATA)
468 len = bus_space_read_2(iot, ioh, tmc_fdcnt);
469 if (slp->sl_scp.scp_direction == SCSI_LOW_WRITE)
473 tres = len + slp->sl_scp.scp_datalen;
474 if (tres <= (u_int) cb->ccb_scp.scp_datalen)
476 slp->sl_scp.scp_data -= len;
477 slp->sl_scp.scp_datalen = tres;
481 slp->sl_error |= PDMAERR;
482 device_printf(slp->sl_dev,
483 "len %x >= datalen %x\n",
484 len, slp->sl_scp.scp_datalen);
488 else if (slp->sl_scp.scp_direction == SCSI_LOW_READ)
492 slp->sl_error |= PDMAERR;
493 device_printf(slp->sl_dev,
494 "len %x left in fifo\n", len);
497 scsi_low_data_finish(slp);
502 device_printf(slp->sl_dev, "data phase miss\n");
503 slp->sl_error |= PDMAERR;
507 bus_space_write_1(iot, ioh, tmc_fctl, sc->sc_fcRinit);
511 stg_pio_read(sc, ti, thold)
512 struct stg_softc *sc;
513 struct targ_info *ti;
516 struct scsi_low_softc *slp = &sc->sc_sclow;
517 bus_space_tag_t iot = sc->sc_iot;
518 bus_space_handle_t ioh = sc->sc_ioh;
519 struct sc_p *sp = &slp->sl_scp;
524 if ((slp->sl_flags & HW_PDMASTART) == 0)
526 bus_space_write_1(iot, ioh, tmc_fctl,
527 sc->sc_fcRinit | FCTL_FIFOEN);
528 slp->sl_flags |= HW_PDMASTART;
531 tout = sc->sc_tmaxcnt;
537 res = bus_space_read_2(iot, ioh, tmc_fdcnt);
540 bus_space_write_1(iot, ioh, tmc_ictl,
549 stat = bus_space_read_1(iot, ioh, tmc_bstat);
550 res = bus_space_read_2(iot, ioh, tmc_fdcnt);
553 if ((stat & PHASE_MASK) != DATA_IN_PHASE)
555 if (sp->scp_datalen <= 0)
562 /* The assumtion res != 0 is valid here */
563 if (res > sp->scp_datalen)
565 if (res == (u_int) -1)
568 slp->sl_error |= PDMAERR;
569 if ((slp->sl_flags & HW_READ_PADDING) == 0)
571 device_printf(slp->sl_dev,
572 "read padding required\n");
577 if (res > STG_MAX_DATA_SIZE)
578 res = STG_MAX_DATA_SIZE;
581 (void) bus_space_read_1(iot, ioh, tmc_rfifo);
586 sp->scp_datalen -= res;
589 *sp->scp_data = bus_space_read_1(iot, ioh, tmc_rfifo);
594 bus_space_read_multi_2(iot, ioh, tmc_rfifo,
595 (u_int16_t *) sp->scp_data, res >> 1);
600 device_printf(slp->sl_dev, "pio read timeout\n");
604 stg_pio_write(sc, ti, thold)
605 struct stg_softc *sc;
606 struct targ_info *ti;
609 struct scsi_low_softc *slp = &sc->sc_sclow;
610 bus_space_tag_t iot = sc->sc_iot;
611 bus_space_handle_t ioh = sc->sc_ioh;
612 struct sc_p *sp = &slp->sl_scp;
615 register u_int8_t stat;
617 if ((slp->sl_flags & HW_PDMASTART) == 0)
619 stat = sc->sc_fcWinit | FCTL_FIFOEN | FCTL_FIFOW;
620 bus_space_write_1(iot, ioh, tmc_fctl, stat | FCTL_CLRFIFO);
621 bus_space_write_1(iot, ioh, tmc_fctl, stat);
622 slp->sl_flags |= HW_PDMASTART;
625 tout = sc->sc_tmaxcnt;
628 stat = bus_space_read_1(iot, ioh, tmc_bstat);
629 if ((stat & PHASE_MASK) != DATA_OUT_PHASE)
632 if (sp->scp_datalen <= 0)
634 if (sc->sc_dataout_timeout == 0)
635 sc->sc_dataout_timeout = SCSI_LOW_TIMEOUT_HZ;
642 res = bus_space_read_2(iot, ioh, tmc_fdcnt);
645 bus_space_write_1(iot, ioh, tmc_ictl,
654 res = bus_space_read_2(iot, ioh, tmc_fdcnt);
655 if (res > sc->sc_maxwsize / 2)
662 if (res == (u_int) -1)
664 res = sc->sc_maxwsize - res;
665 if (res > sp->scp_datalen)
666 res = sp->scp_datalen;
668 sp->scp_datalen -= res;
669 if ((res & 0x1) != 0)
671 bus_space_write_1(iot, ioh, tmc_wfifo, *sp->scp_data);
676 bus_space_write_multi_2(iot, ioh, tmc_wfifo,
677 (u_int16_t *) sp->scp_data, res >> 1);
682 device_printf(slp->sl_dev, "pio write timeout\n");
686 stg_negate_signal(struct stg_softc *sc, u_int8_t mask, u_char *s)
688 struct scsi_low_softc *slp = &sc->sc_sclow;
689 bus_space_tag_t bst = sc->sc_iot;
690 bus_space_handle_t bsh = sc->sc_ioh;
694 for (wc = 0; wc < STG_DELAY_MAX / STG_DELAY_INTERVAL; wc ++)
696 regv = bus_space_read_1(bst, bsh, tmc_bstat);
697 if (regv == (u_int8_t) -1)
699 if ((regv & mask) == 0)
702 DELAY(STG_DELAY_INTERVAL);
705 device_printf(slp->sl_dev, "%s stg_negate_signal timeout\n", s);
710 stg_expect_signal(struct stg_softc *sc, u_int8_t phase, u_int8_t mask)
712 struct scsi_low_softc *slp = &sc->sc_sclow;
713 bus_space_tag_t bst = sc->sc_iot;
714 bus_space_handle_t bsh = sc->sc_ioh;
719 for (wc = 0; wc < STG_DELAY_MAX / STG_DELAY_INTERVAL; wc ++)
721 ph = bus_space_read_1(bst, bsh, tmc_bstat);
722 if (ph == (u_int8_t) -1)
724 if ((ph & PHASE_MASK) != phase)
726 if ((ph & mask) != 0)
729 DELAY(STG_DELAY_INTERVAL);
732 device_printf(slp->sl_dev, "stg_expect_signal timeout\n");
737 stg_xfer(sc, buf, len, phase, clear_atn)
738 struct stg_softc *sc;
744 bus_space_tag_t iot = sc->sc_iot;
745 bus_space_handle_t ioh = sc->sc_ioh;
748 if (phase & BSTAT_IO)
749 bus_space_write_1(iot, ioh, tmc_fctl, sc->sc_fcRinit);
751 bus_space_write_1(iot, ioh, tmc_fctl, sc->sc_fcWinit);
753 for (ptr = 0; len > 0; len --)
755 rv = stg_expect_signal(sc, phase, BSTAT_REQ);
759 if (len == 1 && clear_atn != 0)
761 sc->sc_busc &= ~BCTL_ATN;
762 stghw_bcr_write_1(sc, sc->sc_busc);
763 SCSI_LOW_DEASSERT_ATN(&sc->sc_sclow);
766 if (phase & BSTAT_IO)
768 buf[ptr ++] = bus_space_read_1(iot, ioh, tmc_rdata);
772 bus_space_write_1(iot, ioh, tmc_wdata, buf[ptr ++]);
775 stg_negate_signal(sc, BSTAT_ACK, "xfer<ACK>");
779 bus_space_write_1(iot, ioh, tmc_fctl, sc->sc_fcRinit);
783 /**************************************************************
784 * disconnect & reselect (HW low)
785 **************************************************************/
788 struct stg_softc *sc;
790 struct scsi_low_softc *slp = &sc->sc_sclow;
791 bus_space_tag_t iot = sc->sc_iot;
792 bus_space_handle_t ioh = sc->sc_ioh;
797 if (slp->sl_selid != NULL)
800 * Selection vs Reselection conflicts.
802 bus_space_write_1(iot, ioh, tmc_fctl, sc->sc_fcRinit);
803 stghw_bcr_write_1(sc, BCTL_BUSFREE);
805 else if (slp->sl_Tnexus != NULL)
807 device_printf(slp->sl_dev, "unexpected termination\n");
808 stg_disconnected(sc, slp->sl_Tnexus);
812 * We should ack the reselection as soon as possible,
813 * because the target would abort the current reselection seq
814 * due to reselection timeout.
816 tout = STG_DELAY_SELECT_POLLING_MAX;
819 regv = bus_space_read_1(iot, ioh, tmc_bstat);
820 if ((regv & (BSTAT_IO | BSTAT_SEL | BSTAT_BSY)) ==
821 (BSTAT_IO | BSTAT_SEL))
824 regv = bus_space_read_1(iot, ioh, tmc_bstat);
825 if ((regv & (BSTAT_IO | BSTAT_SEL | BSTAT_BSY)) ==
826 (BSTAT_IO | BSTAT_SEL))
831 device_printf(slp->sl_dev, "reselction timeout I\n");
835 sid = (u_int) bus_space_read_1(iot, ioh, tmc_scsiid);
836 if ((sid & sc->sc_idbit) == 0)
842 bus_space_write_1(iot, ioh, tmc_fctl,
843 sc->sc_fcRinit | FCTL_CLRFIFO | FCTL_CLRINT);
844 bus_space_write_1(iot, ioh, tmc_fctl, sc->sc_fcRinit);
845 stghw_bcr_write_1(sc, sc->sc_busc | BCTL_BSY);
849 regv = bus_space_read_1(iot, ioh, tmc_bstat);
850 if ((regv & (BSTAT_SEL | BSTAT_BSY)) == BSTAT_BSY)
854 device_printf(slp->sl_dev, "reselction timeout II\n");
858 sid &= ~sc->sc_idbit;
860 if (scsi_low_reselected(slp, sid) == NULL)
864 stg_statics.reselect ++;
865 #endif /* STG_STATICS */
870 stg_disconnected(sc, ti)
871 struct stg_softc *sc;
872 struct targ_info *ti;
874 struct scsi_low_softc *slp = &sc->sc_sclow;
875 bus_space_tag_t iot = sc->sc_iot;
876 bus_space_handle_t ioh = sc->sc_ioh;
878 /* clear bus status & fifo */
879 bus_space_write_1(iot, ioh, tmc_fctl, sc->sc_fcRinit | FCTL_CLRFIFO);
880 bus_space_write_1(iot, ioh, tmc_fctl, sc->sc_fcRinit);
881 stghw_bcr_write_1(sc, BCTL_BUSFREE);
882 sc->sc_icinit &= ~ICTL_FIFO;
883 sc->sc_busc &= ~BCTL_ATN;
884 sc->sc_dataout_timeout = 0;
885 sc->sc_ubf_timeout = 0;
888 stg_statics.disconnect ++;
889 #endif /* STG_STATICS */
890 scsi_low_disconnected(slp, ti);
894 /**************************************************************
896 **************************************************************/
898 stg_target_nexus_establish(sc)
899 struct stg_softc *sc;
901 struct scsi_low_softc *slp = &sc->sc_sclow;
902 bus_space_tag_t iot = sc->sc_iot;
903 bus_space_handle_t ioh = sc->sc_ioh;
904 struct targ_info *ti = slp->sl_Tnexus;
905 struct stg_targ_info *sti = (void *) ti;
907 bus_space_write_1(iot, ioh, tmc_ssctl, sti->sti_reg_synch);
908 if ((stg_io_control & STG_FIFO_INTERRUPTS) != 0)
910 sc->sc_icinit |= ICTL_FIFO;
916 stg_lun_nexus_establish(sc)
917 struct stg_softc *sc;
924 stg_ccb_nexus_establish(sc)
925 struct stg_softc *sc;
927 struct scsi_low_softc *slp = &sc->sc_sclow;
928 struct slccb *cb = slp->sl_Qnexus;
930 sc->sc_tmaxcnt = cb->ccb_tcmax * 1000 * 1000;
934 #define STGHW_SELECT_INTERVAL 10
937 stghw_select_targ_wait(sc, mu)
938 struct stg_softc *sc;
941 bus_space_tag_t iot = sc->sc_iot;
942 bus_space_handle_t ioh = sc->sc_ioh;
944 mu = mu / STGHW_SELECT_INTERVAL;
947 if ((bus_space_read_1(iot, ioh, tmc_bstat) & BSTAT_BSY) == 0)
949 DELAY(STGHW_SELECT_INTERVAL);
953 if ((bus_space_read_1(iot, ioh, tmc_bstat) & BSTAT_BSY) != 0)
962 stg_selection_done_and_expect_msgout(sc)
963 struct stg_softc *sc;
965 struct scsi_low_softc *slp = &sc->sc_sclow;
966 bus_space_tag_t iot = sc->sc_iot;
967 bus_space_handle_t ioh = sc->sc_ioh;
969 bus_space_write_1(iot, ioh, tmc_fctl, sc->sc_fcRinit | FCTL_CLRFIFO);
970 bus_space_write_1(iot, ioh, tmc_fctl, sc->sc_fcRinit);
971 stghw_bcr_write_1(sc, sc->sc_imsg | sc->sc_busc);
972 SCSI_LOW_ASSERT_ATN(slp);
979 struct stg_softc *sc = arg;
980 struct scsi_low_softc *slp = &sc->sc_sclow;
981 bus_space_tag_t iot = sc->sc_iot;
982 bus_space_handle_t ioh = sc->sc_ioh;
983 struct targ_info *ti;
987 u_int8_t status, astatus, regv;
989 /*******************************************
991 *******************************************/
992 if (slp->sl_flags & HW_INACTIVE)
995 astatus = bus_space_read_1(iot, ioh, tmc_astat);
996 status = bus_space_read_1(iot, ioh, tmc_bstat);
998 if ((astatus & ASTAT_STATMASK) == 0 || astatus == (u_int8_t) -1)
1001 bus_space_write_1(iot, ioh, tmc_ictl, 0);
1002 if (astatus & ASTAT_SCSIRST)
1004 bus_space_write_1(iot, ioh, tmc_fctl,
1005 sc->sc_fcRinit | FCTL_CLRFIFO);
1006 bus_space_write_1(iot, ioh, tmc_fctl, sc->sc_fcRinit);
1007 bus_space_write_1(iot, ioh, tmc_ictl, 0);
1009 scsi_low_restart(slp, SCSI_LOW_RESTART_SOFT,
1010 "bus reset (power off?)");
1014 /*******************************************
1016 *******************************************/
1020 scsi_low_print(slp, NULL);
1021 device_printf(slp->sl_dev, "st %x ist %x\n\n",
1025 kdb_enter(KDB_WHY_CAM, "stg");
1028 #endif /* STG_DEBUG */
1030 /*******************************************
1031 * reselection & nexus
1032 *******************************************/
1033 if ((status & RESEL_PHASE_MASK)== PHASE_RESELECTED)
1035 if (stg_reselected(sc) == EJUSTRETURN)
1039 if ((ti = slp->sl_Tnexus) == NULL)
1043 if ((astatus & ASTAT_PARERR) != 0 && ti->ti_phase != PH_ARBSTART &&
1044 (sc->sc_fcRinit & FCTL_PARENB) != 0)
1046 slp->sl_error |= PARITYERR;
1047 derror = SCSI_LOW_DATA_PE;
1048 if ((status & PHASE_MASK) == MESSAGE_IN_PHASE)
1049 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_PARITY, 0);
1051 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_ERROR, 1);
1054 /*******************************************
1055 * aribitration & selection
1056 *******************************************/
1057 switch (ti->ti_phase)
1060 if ((astatus & ASTAT_ARBIT) == 0)
1063 stg_statics.arbit_fail_0 ++;
1064 #endif /* STG_STATICS */
1068 status = bus_space_read_1(iot, ioh, tmc_bstat);
1069 if ((status & BSTAT_IO) != 0)
1072 * Selection vs Reselection conflicts.
1075 stg_statics.arbit_fail_1 ++;
1076 #endif /* STG_STATICS */
1078 bus_space_write_1(iot, ioh, tmc_fctl, sc->sc_fcRinit);
1079 stghw_bcr_write_1(sc, BCTL_BUSFREE);
1080 scsi_low_arbit_fail(slp, slp->sl_Qnexus);
1085 * selection assert start.
1087 SCSI_LOW_SETUP_PHASE(ti, PH_SELSTART);
1088 scsi_low_arbit_win(slp);
1091 bus_space_write_1(iot, ioh, tmc_scsiid,
1092 sc->sc_idbit | (1 << ti->ti_id));
1093 stghw_bcr_write_1(sc, sc->sc_imsg | sc->sc_busc | BCTL_SEL);
1094 bus_space_write_1(iot, ioh, tmc_fctl, sc->sc_fcWinit);
1095 if ((stg_io_control & STG_WAIT_FOR_SELECT) != 0)
1097 /* selection abort delay 200 + 100 micro sec */
1098 if (stghw_select_targ_wait(sc, 300) == 0)
1100 SCSI_LOW_SETUP_PHASE(ti, PH_SELECTED);
1101 stg_selection_done_and_expect_msgout(sc);
1108 if ((status & BSTAT_BSY) == 0)
1110 /* selection timeout delay 250 ms */
1111 if (stghw_select_targ_wait(sc, 250 * 1000) != 0)
1113 stg_disconnected(sc, ti);
1118 SCSI_LOW_SETUP_PHASE(ti, PH_SELECTED);
1119 stg_selection_done_and_expect_msgout(sc);
1123 if ((status & BSTAT_REQ) == 0)
1125 stg_target_nexus_establish(sc);
1129 if ((status & BSTAT_REQ) == 0)
1132 /* clear a busy line */
1133 bus_space_write_1(iot, ioh, tmc_fctl, sc->sc_fcRinit);
1134 stghw_bcr_write_1(sc, sc->sc_busc);
1135 stg_target_nexus_establish(sc);
1136 if ((status & PHASE_MASK) != MESSAGE_IN_PHASE)
1138 device_printf(slp->sl_dev,
1139 "unexpected phase after reselect\n");
1140 slp->sl_error |= FATALIO;
1141 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_ABORT, 1);
1147 /*******************************************
1149 *******************************************/
1150 if ((slp->sl_flags & HW_PDMASTART) && STG_IS_PHASE_DATA(status) == 0)
1152 if (slp->sl_scp.scp_direction == SCSI_LOW_READ)
1153 stg_pio_read(sc, ti, 0);
1155 stg_pdma_end(sc, ti);
1158 /*******************************************
1160 *******************************************/
1161 switch (status & PHASE_MASK)
1164 if (stg_expect_signal(sc, COMMAND_PHASE, BSTAT_REQ) <= 0)
1167 SCSI_LOW_SETUP_PHASE(ti, PH_CMD);
1168 if (scsi_low_cmd(slp, ti) != 0)
1170 scsi_low_attention(slp);
1173 if (stg_xfer(sc, slp->sl_scp.scp_cmd, slp->sl_scp.scp_cmdlen,
1174 COMMAND_PHASE, 0) != 0)
1176 device_printf(slp->sl_dev, "CMDOUT short\n");
1180 case DATA_OUT_PHASE:
1181 SCSI_LOW_SETUP_PHASE(ti, PH_DATA);
1182 if (scsi_low_data(slp, ti, &bp, SCSI_LOW_WRITE) != 0)
1184 scsi_low_attention(slp);
1187 if ((sc->sc_icinit & ICTL_FIFO) != 0)
1188 stg_pio_write(sc, ti, sc->sc_wthold);
1190 stg_pio_write(sc, ti, 0);
1194 SCSI_LOW_SETUP_PHASE(ti, PH_DATA);
1195 if (scsi_low_data(slp, ti, &bp, SCSI_LOW_READ) != 0)
1197 scsi_low_attention(slp);
1200 if ((sc->sc_icinit & ICTL_FIFO) != 0)
1201 stg_pio_read(sc, ti, sc->sc_rthold);
1203 stg_pio_read(sc, ti, 0);
1207 regv = stg_expect_signal(sc, STATUS_PHASE, BSTAT_REQ);
1211 SCSI_LOW_SETUP_PHASE(ti, PH_STAT);
1212 regv = bus_space_read_1(iot, ioh, tmc_sdna);
1213 if (scsi_low_statusin(slp, ti, regv | derror) != 0)
1215 scsi_low_attention(slp);
1217 if (regv != bus_space_read_1(iot, ioh, tmc_rdata))
1219 device_printf(slp->sl_dev, "STATIN: data mismatch\n");
1221 stg_negate_signal(sc, BSTAT_ACK, "statin<ACK>");
1224 case MESSAGE_OUT_PHASE:
1225 if (stg_expect_signal(sc, MESSAGE_OUT_PHASE, BSTAT_REQ) <= 0)
1228 SCSI_LOW_SETUP_PHASE(ti, PH_MSGOUT);
1229 flags = (ti->ti_ophase != ti->ti_phase) ?
1230 SCSI_LOW_MSGOUT_INIT : 0;
1231 len = scsi_low_msgout(slp, ti, flags);
1233 if (len > 1 && slp->sl_atten == 0)
1235 scsi_low_attention(slp);
1238 if (stg_xfer(sc, ti->ti_msgoutstr, len, MESSAGE_OUT_PHASE,
1239 slp->sl_clear_atten) != 0)
1241 device_printf(slp->sl_dev, "MSGOUT short\n");
1245 if (slp->sl_msgphase >= MSGPH_ABORT)
1247 stg_disconnected(sc, ti);
1252 case MESSAGE_IN_PHASE:
1253 /* confirm phase and req signal */
1254 if (stg_expect_signal(sc, MESSAGE_IN_PHASE, BSTAT_REQ) <= 0)
1257 SCSI_LOW_SETUP_PHASE(ti, PH_MSGIN);
1259 /* read data with NOACK */
1260 regv = bus_space_read_1(iot, ioh, tmc_sdna);
1262 if (scsi_low_msgin(slp, ti, derror | regv) == 0)
1264 if (scsi_low_is_msgout_continue(ti, 0) != 0)
1266 scsi_low_attention(slp);
1270 /* read data with ACK */
1271 if (regv != bus_space_read_1(iot, ioh, tmc_rdata))
1273 device_printf(slp->sl_dev, "MSGIN: data mismatch\n");
1276 /* wait for the ack negated */
1277 stg_negate_signal(sc, BSTAT_ACK, "msgin<ACK>");
1279 if (slp->sl_msgphase != 0 && slp->sl_msgphase < MSGPH_ABORT)
1281 stg_disconnected(sc, ti);
1286 device_printf(slp->sl_dev, "unexpected disconnect\n");
1287 stg_disconnected(sc, ti);
1291 slp->sl_error |= FATALIO;
1292 device_printf(slp->sl_dev, "unknown phase bus %x intr %x\n",
1298 bus_space_write_1(iot, ioh, tmc_ictl, sc->sc_icinit);
1304 struct stg_softc *sc;
1306 struct scsi_low_softc *slp = &sc->sc_sclow;
1307 bus_space_tag_t iot = sc->sc_iot;
1308 bus_space_handle_t ioh = sc->sc_ioh;
1312 if (slp->sl_Tnexus == NULL)
1315 status = bus_space_read_1(iot, ioh, tmc_bstat);
1316 if ((status & PHASE_MASK) == 0)
1318 if (sc->sc_ubf_timeout ++ == 0)
1321 device_printf(slp->sl_dev, "unexpected bus free detected\n");
1322 slp->sl_error |= FATALIO;
1323 scsi_low_print(slp, slp->sl_Tnexus);
1324 stg_disconnected(sc, slp->sl_Tnexus);
1328 switch (status & PHASE_MASK)
1330 case DATA_OUT_PHASE:
1331 if (sc->sc_dataout_timeout == 0)
1333 if ((status & BSTAT_REQ) == 0)
1335 if (bus_space_read_2(iot, ioh, tmc_fdcnt) != 0)
1337 if ((-- sc->sc_dataout_timeout) > 0)
1340 slp->sl_error |= PDMAERR;
1341 if ((slp->sl_flags & HW_WRITE_PADDING) == 0)
1343 device_printf(slp->sl_dev, "write padding required\n");
1347 bus_space_write_1(iot, ioh, tmc_ictl, 0);
1349 tout = STG_DELAY_MAX;
1352 status = bus_space_read_1(iot, ioh, tmc_bstat);
1353 if ((status & PHASE_MASK) != DATA_OUT_PHASE)
1356 if (bus_space_read_2(iot, ioh, tmc_fdcnt) != 0)
1362 for (count = sc->sc_maxwsize; count > 0; count --)
1363 bus_space_write_1(iot, ioh, tmc_wfifo, 0);
1366 status = bus_space_read_1(iot, ioh, tmc_bstat);
1367 if ((status & PHASE_MASK) == DATA_OUT_PHASE)
1368 sc->sc_dataout_timeout = SCSI_LOW_TIMEOUT_HZ;
1370 bus_space_write_1(iot, ioh, tmc_ictl, sc->sc_icinit);