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.14 2007/01/06 08:33:34 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>
52 #include <sys/thread2.h>
54 #include <machine/clock.h>
55 #include <machine/cpu.h>
57 #include <bus/cam/scsi/scsi_low.h>
58 #include "tmc18c30reg.h"
59 #include "tmc18c30var.h"
61 /***************************************************
63 ***************************************************/
64 /* DEVICE CONFIGURATION FLAGS (MINOR)
67 * 0x02 PARITY LINE OFF
68 * 0x04 IDENTIFY MSG OFF ( = single lun)
69 * 0x08 SYNC TRANSFER OFF
71 /* #define STG_SYNC_SUPPORT */ /* NOT YET but easy */
73 /* For the 512 fifo type: change below */
74 #define TMC18C30_FIFOSZ 0x800
75 #define TMC18C30_FCBSZ 0x200
76 #define TMC18C50_FIFOSZ 0x2000
77 #define TMC18C50_FCBSZ 0x400
79 #define STG_MAX_DATA_SIZE (64 * 1024)
80 #define STG_DELAY_MAX (2 * 1000 * 1000)
81 #define STG_DELAY_INTERVAL (1)
82 #define STG_DELAY_SELECT_POLLING_MAX (5 * 1000 * 1000)
84 /***************************************************
86 ***************************************************/
87 #define STG_NTARGETS 8
90 /***************************************************
92 ***************************************************/
95 #endif /* STG_DEBUG */
104 #endif /* STG_STATICS */
106 /***************************************************
108 ***************************************************/
109 #define STG_FIFO_INTERRUPTS 0x0001
110 #define STG_WAIT_FOR_SELECT 0x0100
112 int stg_io_control = STG_IO_CONTROL_FLAGS;
114 /***************************************************
116 ***************************************************/
117 extern struct cfdriver stg_cd;
119 /**************************************************************
121 **************************************************************/
123 static void stg_pio_read (struct stg_softc *, struct targ_info *, u_int);
124 static void stg_pio_write (struct stg_softc *, struct targ_info *, u_int);
125 static int stg_xfer (struct stg_softc *, u_int8_t *, int, int, int);
126 static int stg_msg (struct stg_softc *, struct targ_info *, u_int);
127 static int stg_reselected (struct stg_softc *);
128 static int stg_disconnected (struct stg_softc *, struct targ_info *);
129 static __inline void stg_pdma_end (struct stg_softc *, struct targ_info *);
130 static int stghw_select_targ_wait (struct stg_softc *, int);
131 static int stghw_check (struct stg_softc *);
132 static void stghw_init (struct stg_softc *);
133 static int stg_negate_signal (struct stg_softc *, u_int8_t, u_char *);
134 static int stg_expect_signal (struct stg_softc *, u_int8_t, u_int8_t);
135 static int stg_world_start (struct stg_softc *, int);
136 static int stghw_start_selection (struct stg_softc *sc, struct slccb *);
137 static void stghw_bus_reset (struct stg_softc *);
138 static void stghw_attention (struct stg_softc *);
139 static int stg_target_nexus_establish (struct stg_softc *);
140 static int stg_lun_nexus_establish (struct stg_softc *);
141 static int stg_ccb_nexus_establish (struct stg_softc *);
142 static int stg_targ_init (struct stg_softc *, struct targ_info *, int);
143 static __inline void stghw_bcr_write_1 (struct stg_softc *, u_int8_t);
144 static int stg_timeout (struct stg_softc *);
145 static void stg_selection_done_and_expect_msgout (struct stg_softc *);
147 struct scsi_low_funcs stgfuncs = {
148 SC_LOW_INIT_T stg_world_start,
149 SC_LOW_BUSRST_T stghw_bus_reset,
150 SC_LOW_TARG_INIT_T stg_targ_init,
151 SC_LOW_LUN_INIT_T NULL,
153 SC_LOW_SELECT_T stghw_start_selection,
154 SC_LOW_NEXUS_T stg_lun_nexus_establish,
155 SC_LOW_NEXUS_T stg_ccb_nexus_establish,
157 SC_LOW_ATTEN_T stghw_attention,
158 SC_LOW_MSG_T stg_msg,
160 SC_LOW_TIMEOUT_T stg_timeout,
161 SC_LOW_POLL_T stgintr,
166 /****************************************************
168 ****************************************************/
170 stghw_bcr_write_1(struct stg_softc *sc, u_int8_t bcv)
173 bus_space_write_1(sc->sc_iot, sc->sc_ioh, tmc_bctl, bcv);
178 stghw_check(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;
234 stghw_init(struct stg_softc *sc)
236 bus_space_tag_t iot = sc->sc_iot;
237 bus_space_handle_t ioh = sc->sc_ioh;
239 bus_space_write_1(iot, ioh, tmc_ictl, 0);
240 stghw_bcr_write_1(sc, BCTL_BUSFREE);
241 bus_space_write_1(iot, ioh, tmc_fctl,
242 sc->sc_fcRinit | FCTL_CLRFIFO | FCTL_CLRINT);
243 bus_space_write_1(iot, ioh, tmc_fctl, sc->sc_fcRinit);
244 bus_space_write_1(iot, ioh, tmc_ictl, sc->sc_icinit);
246 bus_space_write_1(iot, ioh, tmc_ssctl, 0);
250 stg_targ_init(struct stg_softc *sc, struct targ_info *ti, int action)
252 struct stg_targ_info *sti = (void *) ti;
254 if (action == SCSI_LOW_INFO_ALLOC || action == SCSI_LOW_INFO_REVOKE)
256 ti->ti_width = SCSI_LOW_BUS_WIDTH_8;
257 ti->ti_maxsynch.period = 0;
258 ti->ti_maxsynch.offset = 0;
259 sti->sti_reg_synch = 0;
264 /****************************************************
266 ****************************************************/
268 stghw_attention(struct stg_softc *sc)
271 sc->sc_busc |= BCTL_ATN;
272 sc->sc_busimg |= BCTL_ATN;
273 bus_space_write_1(sc->sc_iot, sc->sc_ioh, tmc_bctl, sc->sc_busimg);
278 stghw_bus_reset(struct stg_softc *sc)
280 bus_space_tag_t iot = sc->sc_iot;
281 bus_space_handle_t ioh = sc->sc_ioh;
283 bus_space_write_1(iot, ioh, tmc_ictl, 0);
284 bus_space_write_1(iot, ioh, tmc_fctl, 0);
285 stghw_bcr_write_1(sc, BCTL_RST);
286 SCSI_LOW_DELAY(100000);
287 stghw_bcr_write_1(sc, BCTL_BUSFREE);
291 stghw_start_selection(struct stg_softc *sc, struct slccb *cb)
293 bus_space_tag_t iot = sc->sc_iot;
294 bus_space_handle_t ioh = sc->sc_ioh;
295 struct targ_info *ti = cb->ti;
298 sc->sc_tmaxcnt = cb->ccb_tcmax * 1000 * 1000;
299 sc->sc_dataout_timeout = 0;
300 sc->sc_ubf_timeout = 0;
301 stghw_bcr_write_1(sc, BCTL_BUSFREE);
302 bus_space_write_1(iot, ioh, tmc_ictl, sc->sc_icinit);
305 stat = bus_space_read_1(iot, ioh, tmc_astat);
306 if ((stat & ASTAT_INT) != 0)
309 return SCSI_LOW_START_FAIL;
312 bus_space_write_1(iot, ioh, tmc_scsiid, sc->sc_idbit);
313 bus_space_write_1(iot, ioh, tmc_fctl, sc->sc_fcRinit | FCTL_ARBIT);
316 SCSI_LOW_SETUP_PHASE(ti, PH_ARBSTART);
317 return SCSI_LOW_START_OK;
321 stg_world_start(struct stg_softc *sc, int fdone)
323 struct scsi_low_softc *slp = &sc->sc_sclow;
326 if ((slp->sl_cfgflags & CFG_NOPARITY) == 0)
327 sc->sc_fcRinit |= FCTL_PARENB;
329 sc->sc_fcRinit &= ~FCTL_PARENB;
331 if ((error = stghw_check(sc)) != 0)
335 scsi_low_bus_reset(slp);
338 SOFT_INTR_REQUIRED(slp);
343 stg_msg(struct stg_softc *sc, struct targ_info *ti, u_int msg)
345 bus_space_tag_t iot = sc->sc_iot;
346 bus_space_handle_t ioh = sc->sc_ioh;
347 struct stg_targ_info *sti = (void *) ti;
348 u_int period, offset;
350 if ((msg & SCSI_LOW_MSG_WIDE) != 0)
352 if (ti->ti_width != SCSI_LOW_BUS_WIDTH_8)
354 ti->ti_width = SCSI_LOW_BUS_WIDTH_8;
360 if ((msg & SCSI_LOW_MSG_SYNCH) == 0)
363 period = ti->ti_maxsynch.period;
364 offset = ti->ti_maxsynch.offset;
365 period = period << 2;
368 sti->sti_reg_synch = (period - 200) / 50;
370 sti->sti_reg_synch ++;
371 sti->sti_reg_synch |= SSCTL_SYNCHEN;
373 else if (period >= 100)
375 sti->sti_reg_synch = (period - 100) / 50;
377 sti->sti_reg_synch ++;
378 sti->sti_reg_synch |= SSCTL_SYNCHEN | SSCTL_FSYNCHEN;
380 bus_space_write_1(iot, ioh, tmc_ssctl, sti->sti_reg_synch);
384 /**************************************************************
385 * General probe attach
386 **************************************************************/
388 stgprobesubr(bus_space_tag_t iot, bus_space_handle_t ioh, u_int dvcfg)
392 lsb = bus_space_read_1(iot, ioh, tmc_idlsb);
393 msb = bus_space_read_1(iot, ioh, tmc_idmsb);
394 switch (msb << 8 | lsb)
408 stgprint(void *aux, const char *name)
412 kprintf("%s: scsibus ", name);
417 stgattachsubr(struct stg_softc *sc)
419 struct scsi_low_softc *slp = &sc->sc_sclow;
423 sc->sc_idbit = (1 << slp->sl_hostid);
424 slp->sl_funcs = &stgfuncs;
425 sc->sc_tmaxcnt = SCSI_LOW_MIN_TOUT * 1000 * 1000; /* default */
427 slp->sl_flags |= HW_READ_PADDING;
428 slp->sl_cfgflags |= CFG_ASYNC; /* XXX */
430 (void) scsi_low_attach(slp, 0, STG_NTARGETS, STG_NLUNS,
431 sizeof(struct stg_targ_info), 0);
434 /**************************************************************
436 **************************************************************/
438 stg_pdma_end(struct stg_softc *sc, struct targ_info *ti)
440 struct scsi_low_softc *slp = &sc->sc_sclow;
441 bus_space_tag_t iot = sc->sc_iot;
442 bus_space_handle_t ioh = sc->sc_ioh;
443 struct slccb *cb = slp->sl_Qnexus;
446 slp->sl_flags &= ~HW_PDMASTART;
447 sc->sc_icinit &= ~ICTL_FIFO;
448 sc->sc_dataout_timeout = 0;
452 slp->sl_error |= PDMAERR;
456 if (ti->ti_phase == PH_DATA)
458 len = bus_space_read_2(iot, ioh, tmc_fdcnt);
459 if (slp->sl_scp.scp_direction == SCSI_LOW_WRITE)
463 tres = len + slp->sl_scp.scp_datalen;
464 if (tres <= (u_int) cb->ccb_scp.scp_datalen)
466 slp->sl_scp.scp_data -= len;
467 slp->sl_scp.scp_datalen = tres;
471 slp->sl_error |= PDMAERR;
472 kprintf("%s len %x >= datalen %x\n",
474 len, slp->sl_scp.scp_datalen);
478 else if (slp->sl_scp.scp_direction == SCSI_LOW_READ)
482 slp->sl_error |= PDMAERR;
483 kprintf("%s: len %x left in fifo\n",
487 scsi_low_data_finish(slp);
492 kprintf("%s data phase miss\n", slp->sl_xname);
493 slp->sl_error |= PDMAERR;
497 bus_space_write_1(iot, ioh, tmc_fctl, sc->sc_fcRinit);
501 stg_pio_read(struct stg_softc *sc, struct targ_info *ti, u_int thold)
503 struct scsi_low_softc *slp = &sc->sc_sclow;
504 bus_space_tag_t iot = sc->sc_iot;
505 bus_space_handle_t ioh = sc->sc_ioh;
506 struct sc_p *sp = &slp->sl_scp;
511 if ((slp->sl_flags & HW_PDMASTART) == 0)
513 bus_space_write_1(iot, ioh, tmc_fctl,
514 sc->sc_fcRinit | FCTL_FIFOEN);
515 slp->sl_flags |= HW_PDMASTART;
518 tout = sc->sc_tmaxcnt;
524 res = bus_space_read_2(iot, ioh, tmc_fdcnt);
527 bus_space_write_1(iot, ioh, tmc_ictl,
536 stat = bus_space_read_1(iot, ioh, tmc_bstat);
537 res = bus_space_read_2(iot, ioh, tmc_fdcnt);
540 if ((stat & PHASE_MASK) != DATA_IN_PHASE)
542 if (sp->scp_datalen <= 0)
549 /* The assumtion res != 0 is valid here */
550 if (res > sp->scp_datalen)
552 if (res == (u_int) -1)
555 slp->sl_error |= PDMAERR;
556 if ((slp->sl_flags & HW_READ_PADDING) == 0)
558 kprintf("%s: read padding required\n",
564 if (res > STG_MAX_DATA_SIZE)
565 res = STG_MAX_DATA_SIZE;
568 (void) bus_space_read_1(iot, ioh, tmc_rfifo);
573 sp->scp_datalen -= res;
576 *sp->scp_data = bus_space_read_1(iot, ioh, tmc_rfifo);
581 bus_space_read_multi_2(iot, ioh, tmc_rfifo,
582 (u_int16_t *) sp->scp_data, res >> 1);
587 kprintf("%s: pio read timeout\n", slp->sl_xname);
591 stg_pio_write(struct stg_softc *sc, struct targ_info *ti, u_int thold)
593 struct scsi_low_softc *slp = &sc->sc_sclow;
594 bus_space_tag_t iot = sc->sc_iot;
595 bus_space_handle_t ioh = sc->sc_ioh;
596 struct sc_p *sp = &slp->sl_scp;
601 if ((slp->sl_flags & HW_PDMASTART) == 0)
603 stat = sc->sc_fcWinit | FCTL_FIFOEN | FCTL_FIFOW;
604 bus_space_write_1(iot, ioh, tmc_fctl, stat | FCTL_CLRFIFO);
605 bus_space_write_1(iot, ioh, tmc_fctl, stat);
606 slp->sl_flags |= HW_PDMASTART;
609 tout = sc->sc_tmaxcnt;
612 stat = bus_space_read_1(iot, ioh, tmc_bstat);
613 if ((stat & PHASE_MASK) != DATA_OUT_PHASE)
616 if (sp->scp_datalen <= 0)
618 if (sc->sc_dataout_timeout == 0)
619 sc->sc_dataout_timeout = SCSI_LOW_TIMEOUT_HZ;
626 res = bus_space_read_2(iot, ioh, tmc_fdcnt);
629 bus_space_write_1(iot, ioh, tmc_ictl,
638 res = bus_space_read_2(iot, ioh, tmc_fdcnt);
639 if (res > sc->sc_maxwsize / 2)
646 if (res == (u_int) -1)
648 res = sc->sc_maxwsize - res;
649 if (res > sp->scp_datalen)
650 res = sp->scp_datalen;
652 sp->scp_datalen -= res;
653 if ((res & 0x1) != 0)
655 bus_space_write_1(iot, ioh, tmc_wfifo, *sp->scp_data);
660 bus_space_write_multi_2(iot, ioh, tmc_wfifo,
661 (u_int16_t *) sp->scp_data, res >> 1);
666 kprintf("%s: pio write timeout\n", slp->sl_xname);
670 stg_negate_signal(struct stg_softc *sc, u_int8_t mask, u_char *s)
672 struct scsi_low_softc *slp = &sc->sc_sclow;
673 bus_space_tag_t bst = sc->sc_iot;
674 bus_space_handle_t bsh = sc->sc_ioh;
678 for (wc = 0; wc < STG_DELAY_MAX / STG_DELAY_INTERVAL; wc ++)
680 regv = bus_space_read_1(bst, bsh, tmc_bstat);
681 if (regv == (u_int8_t) -1)
683 if ((regv & mask) == 0)
686 SCSI_LOW_DELAY(STG_DELAY_INTERVAL);
689 kprintf("%s: %s stg_negate_signal timeout\n", slp->sl_xname, s);
694 stg_expect_signal(struct stg_softc *sc, u_int8_t phase, u_int8_t mask)
696 struct scsi_low_softc *slp = &sc->sc_sclow;
697 bus_space_tag_t bst = sc->sc_iot;
698 bus_space_handle_t bsh = sc->sc_ioh;
703 for (wc = 0; wc < STG_DELAY_MAX / STG_DELAY_INTERVAL; wc ++)
705 ph = bus_space_read_1(bst, bsh, tmc_bstat);
706 if (ph == (u_int8_t) -1)
708 if ((ph & PHASE_MASK) != phase)
710 if ((ph & mask) != 0)
713 SCSI_LOW_DELAY(STG_DELAY_INTERVAL);
716 kprintf("%s: stg_expect_signal timeout\n", slp->sl_xname);
721 stg_xfer(struct stg_softc *sc, u_int8_t *buf, int len, int phase,
724 bus_space_tag_t iot = sc->sc_iot;
725 bus_space_handle_t ioh = sc->sc_ioh;
728 if (phase & BSTAT_IO)
729 bus_space_write_1(iot, ioh, tmc_fctl, sc->sc_fcRinit);
731 bus_space_write_1(iot, ioh, tmc_fctl, sc->sc_fcWinit);
733 for (ptr = 0; len > 0; len --)
735 rv = stg_expect_signal(sc, phase, BSTAT_REQ);
739 if (len == 1 && clear_atn != 0)
741 sc->sc_busc &= ~BCTL_ATN;
742 stghw_bcr_write_1(sc, sc->sc_busc);
743 SCSI_LOW_DEASSERT_ATN(&sc->sc_sclow);
746 if (phase & BSTAT_IO)
748 buf[ptr ++] = bus_space_read_1(iot, ioh, tmc_rdata);
752 bus_space_write_1(iot, ioh, tmc_wdata, buf[ptr ++]);
755 stg_negate_signal(sc, BSTAT_ACK, "xfer<ACK>");
759 bus_space_write_1(iot, ioh, tmc_fctl, sc->sc_fcRinit);
763 /**************************************************************
764 * disconnect & reselect (HW low)
765 **************************************************************/
767 stg_reselected(struct stg_softc *sc)
769 struct scsi_low_softc *slp = &sc->sc_sclow;
770 bus_space_tag_t iot = sc->sc_iot;
771 bus_space_handle_t ioh = sc->sc_ioh;
776 if (slp->sl_selid != NULL)
779 * Selection vs Reselection conflicts.
781 bus_space_write_1(iot, ioh, tmc_fctl, sc->sc_fcRinit);
782 stghw_bcr_write_1(sc, BCTL_BUSFREE);
784 else if (slp->sl_Tnexus != NULL)
786 kprintf("%s: unexpected termination\n", slp->sl_xname);
787 stg_disconnected(sc, slp->sl_Tnexus);
791 * We should ack the reselection as soon as possible,
792 * because the target would abort the current reselection seq
793 * due to reselection timeout.
795 tout = STG_DELAY_SELECT_POLLING_MAX;
798 regv = bus_space_read_1(iot, ioh, tmc_bstat);
799 if ((regv & (BSTAT_IO | BSTAT_SEL | BSTAT_BSY)) ==
800 (BSTAT_IO | BSTAT_SEL))
803 regv = bus_space_read_1(iot, ioh, tmc_bstat);
804 if ((regv & (BSTAT_IO | BSTAT_SEL | BSTAT_BSY)) ==
805 (BSTAT_IO | BSTAT_SEL))
810 kprintf("%s: reselction timeout I\n", slp->sl_xname);
814 sid = (u_int) bus_space_read_1(iot, ioh, tmc_scsiid);
815 if ((sid & sc->sc_idbit) == 0)
821 bus_space_write_1(iot, ioh, tmc_fctl,
822 sc->sc_fcRinit | FCTL_CLRFIFO | FCTL_CLRINT);
823 bus_space_write_1(iot, ioh, tmc_fctl, sc->sc_fcRinit);
824 stghw_bcr_write_1(sc, sc->sc_busc | BCTL_BSY);
828 regv = bus_space_read_1(iot, ioh, tmc_bstat);
829 if ((regv & (BSTAT_SEL | BSTAT_BSY)) == BSTAT_BSY)
833 kprintf("%s: reselction timeout II\n", slp->sl_xname);
837 sid &= ~sc->sc_idbit;
839 if (scsi_low_reselected(slp, sid) == NULL)
843 stg_statics.reselect ++;
844 #endif /* STG_STATICS */
849 stg_disconnected(struct stg_softc *sc, struct targ_info *ti)
851 struct scsi_low_softc *slp = &sc->sc_sclow;
852 bus_space_tag_t iot = sc->sc_iot;
853 bus_space_handle_t ioh = sc->sc_ioh;
855 /* clear bus status & fifo */
856 bus_space_write_1(iot, ioh, tmc_fctl, sc->sc_fcRinit | FCTL_CLRFIFO);
857 bus_space_write_1(iot, ioh, tmc_fctl, sc->sc_fcRinit);
858 stghw_bcr_write_1(sc, BCTL_BUSFREE);
859 sc->sc_icinit &= ~ICTL_FIFO;
860 sc->sc_busc &= ~BCTL_ATN;
861 sc->sc_dataout_timeout = 0;
862 sc->sc_ubf_timeout = 0;
865 stg_statics.disconnect ++;
866 #endif /* STG_STATICS */
867 scsi_low_disconnected(slp, ti);
871 /**************************************************************
873 **************************************************************/
875 stg_target_nexus_establish(struct stg_softc *sc)
877 struct scsi_low_softc *slp = &sc->sc_sclow;
878 bus_space_tag_t iot = sc->sc_iot;
879 bus_space_handle_t ioh = sc->sc_ioh;
880 struct targ_info *ti = slp->sl_Tnexus;
881 struct stg_targ_info *sti = (void *) ti;
883 bus_space_write_1(iot, ioh, tmc_ssctl, sti->sti_reg_synch);
884 if ((stg_io_control & STG_FIFO_INTERRUPTS) != 0)
886 sc->sc_icinit |= ICTL_FIFO;
892 stg_lun_nexus_establish(struct stg_softc *sc)
899 stg_ccb_nexus_establish(struct stg_softc *sc)
901 struct scsi_low_softc *slp = &sc->sc_sclow;
902 struct slccb *cb = slp->sl_Qnexus;
904 sc->sc_tmaxcnt = cb->ccb_tcmax * 1000 * 1000;
908 #define STGHW_SELECT_INTERVAL 10
911 stghw_select_targ_wait(struct stg_softc *sc, int mu)
913 bus_space_tag_t iot = sc->sc_iot;
914 bus_space_handle_t ioh = sc->sc_ioh;
916 mu = mu / STGHW_SELECT_INTERVAL;
919 if ((bus_space_read_1(iot, ioh, tmc_bstat) & BSTAT_BSY) == 0)
921 SCSI_LOW_DELAY(STGHW_SELECT_INTERVAL);
925 if ((bus_space_read_1(iot, ioh, tmc_bstat) & BSTAT_BSY) != 0)
934 stg_selection_done_and_expect_msgout(struct stg_softc *sc)
936 struct scsi_low_softc *slp = &sc->sc_sclow;
937 bus_space_tag_t iot = sc->sc_iot;
938 bus_space_handle_t ioh = sc->sc_ioh;
940 bus_space_write_1(iot, ioh, tmc_fctl, sc->sc_fcRinit | FCTL_CLRFIFO);
941 bus_space_write_1(iot, ioh, tmc_fctl, sc->sc_fcRinit);
942 stghw_bcr_write_1(sc, sc->sc_imsg | sc->sc_busc);
943 SCSI_LOW_ASSERT_ATN(slp);
949 struct stg_softc *sc = arg;
950 struct scsi_low_softc *slp = &sc->sc_sclow;
951 bus_space_tag_t iot = sc->sc_iot;
952 bus_space_handle_t ioh = sc->sc_ioh;
953 struct targ_info *ti;
957 u_int8_t status, astatus, regv;
959 /*******************************************
961 *******************************************/
962 if (slp->sl_flags & HW_INACTIVE)
965 astatus = bus_space_read_1(iot, ioh, tmc_astat);
966 status = bus_space_read_1(iot, ioh, tmc_bstat);
968 if ((astatus & ASTAT_STATMASK) == 0 || astatus == (u_int8_t) -1)
971 bus_space_write_1(iot, ioh, tmc_ictl, 0);
972 if (astatus & ASTAT_SCSIRST)
974 bus_space_write_1(iot, ioh, tmc_fctl,
975 sc->sc_fcRinit | FCTL_CLRFIFO);
976 bus_space_write_1(iot, ioh, tmc_fctl, sc->sc_fcRinit);
977 bus_space_write_1(iot, ioh, tmc_ictl, 0);
979 scsi_low_restart(slp, SCSI_LOW_RESTART_SOFT,
980 "bus reset (power off?)");
984 /*******************************************
986 *******************************************/
990 scsi_low_print(slp, NULL);
991 kprintf("%s: st %x ist %x\n\n", slp->sl_xname,
995 SCSI_LOW_DEBUGGER("stg");
998 #endif /* STG_DEBUG */
1000 /*******************************************
1001 * reselection & nexus
1002 *******************************************/
1003 if ((status & RESEL_PHASE_MASK)== PHASE_RESELECTED)
1005 if (stg_reselected(sc) == EJUSTRETURN)
1009 if ((ti = slp->sl_Tnexus) == NULL)
1013 if ((astatus & ASTAT_PARERR) != 0 && ti->ti_phase != PH_ARBSTART &&
1014 (sc->sc_fcRinit & FCTL_PARENB) != 0)
1016 slp->sl_error |= PARITYERR;
1017 derror = SCSI_LOW_DATA_PE;
1018 if ((status & PHASE_MASK) == MESSAGE_IN_PHASE)
1019 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_PARITY, 0);
1021 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_ERROR, 1);
1024 /*******************************************
1025 * aribitration & selection
1026 *******************************************/
1027 switch (ti->ti_phase)
1030 if ((astatus & ASTAT_ARBIT) == 0)
1033 stg_statics.arbit_fail_0 ++;
1034 #endif /* STG_STATICS */
1038 status = bus_space_read_1(iot, ioh, tmc_bstat);
1039 if ((status & BSTAT_IO) != 0)
1042 * Selection vs Reselection conflicts.
1045 stg_statics.arbit_fail_1 ++;
1046 #endif /* STG_STATICS */
1048 bus_space_write_1(iot, ioh, tmc_fctl, sc->sc_fcRinit);
1049 stghw_bcr_write_1(sc, BCTL_BUSFREE);
1050 scsi_low_arbit_fail(slp, slp->sl_Qnexus);
1055 * selection assert start.
1057 SCSI_LOW_SETUP_PHASE(ti, PH_SELSTART);
1058 scsi_low_arbit_win(slp);
1061 bus_space_write_1(iot, ioh, tmc_scsiid,
1062 sc->sc_idbit | (1 << ti->ti_id));
1063 stghw_bcr_write_1(sc, sc->sc_imsg | sc->sc_busc | BCTL_SEL);
1064 bus_space_write_1(iot, ioh, tmc_fctl, sc->sc_fcWinit);
1065 if ((stg_io_control & STG_WAIT_FOR_SELECT) != 0)
1067 /* selection abort delay 200 + 100 micro sec */
1068 if (stghw_select_targ_wait(sc, 300) == 0)
1070 SCSI_LOW_SETUP_PHASE(ti, PH_SELECTED);
1071 stg_selection_done_and_expect_msgout(sc);
1078 if ((status & BSTAT_BSY) == 0)
1080 /* selection timeout delay 250 ms */
1081 if (stghw_select_targ_wait(sc, 250 * 1000) != 0)
1083 stg_disconnected(sc, ti);
1088 SCSI_LOW_SETUP_PHASE(ti, PH_SELECTED);
1089 stg_selection_done_and_expect_msgout(sc);
1093 if ((status & BSTAT_REQ) == 0)
1095 stg_target_nexus_establish(sc);
1099 if ((status & BSTAT_REQ) == 0)
1102 /* clear a busy line */
1103 bus_space_write_1(iot, ioh, tmc_fctl, sc->sc_fcRinit);
1104 stghw_bcr_write_1(sc, sc->sc_busc);
1105 stg_target_nexus_establish(sc);
1106 if ((status & PHASE_MASK) != MESSAGE_IN_PHASE)
1108 kprintf("%s: unexpected phase after reselect\n",
1110 slp->sl_error |= FATALIO;
1111 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_ABORT, 1);
1117 /*******************************************
1119 *******************************************/
1120 if ((slp->sl_flags & HW_PDMASTART) && STG_IS_PHASE_DATA(status) == 0)
1122 if (slp->sl_scp.scp_direction == SCSI_LOW_READ)
1123 stg_pio_read(sc, ti, 0);
1125 stg_pdma_end(sc, ti);
1128 /*******************************************
1130 *******************************************/
1131 switch (status & PHASE_MASK)
1134 if (stg_expect_signal(sc, COMMAND_PHASE, BSTAT_REQ) <= 0)
1137 SCSI_LOW_SETUP_PHASE(ti, PH_CMD);
1138 if (scsi_low_cmd(slp, ti) != 0)
1140 scsi_low_attention(slp);
1143 if (stg_xfer(sc, slp->sl_scp.scp_cmd, slp->sl_scp.scp_cmdlen,
1144 COMMAND_PHASE, 0) != 0)
1146 kprintf("%s: CMDOUT short\n", slp->sl_xname);
1150 case DATA_OUT_PHASE:
1151 SCSI_LOW_SETUP_PHASE(ti, PH_DATA);
1152 if (scsi_low_data(slp, ti, &bp, SCSI_LOW_WRITE) != 0)
1154 scsi_low_attention(slp);
1157 if ((sc->sc_icinit & ICTL_FIFO) != 0)
1158 stg_pio_write(sc, ti, sc->sc_wthold);
1160 stg_pio_write(sc, ti, 0);
1164 SCSI_LOW_SETUP_PHASE(ti, PH_DATA);
1165 if (scsi_low_data(slp, ti, &bp, SCSI_LOW_READ) != 0)
1167 scsi_low_attention(slp);
1170 if ((sc->sc_icinit & ICTL_FIFO) != 0)
1171 stg_pio_read(sc, ti, sc->sc_rthold);
1173 stg_pio_read(sc, ti, 0);
1177 regv = stg_expect_signal(sc, STATUS_PHASE, BSTAT_REQ);
1181 SCSI_LOW_SETUP_PHASE(ti, PH_STAT);
1182 regv = bus_space_read_1(iot, ioh, tmc_sdna);
1183 if (scsi_low_statusin(slp, ti, regv | derror) != 0)
1185 scsi_low_attention(slp);
1187 if (regv != bus_space_read_1(iot, ioh, tmc_rdata))
1189 kprintf("%s: STATIN: data mismatch\n", slp->sl_xname);
1191 stg_negate_signal(sc, BSTAT_ACK, "statin<ACK>");
1194 case MESSAGE_OUT_PHASE:
1195 if (stg_expect_signal(sc, MESSAGE_OUT_PHASE, BSTAT_REQ) <= 0)
1198 SCSI_LOW_SETUP_PHASE(ti, PH_MSGOUT);
1199 flags = (ti->ti_ophase != ti->ti_phase) ?
1200 SCSI_LOW_MSGOUT_INIT : 0;
1201 len = scsi_low_msgout(slp, ti, flags);
1203 if (len > 1 && slp->sl_atten == 0)
1205 scsi_low_attention(slp);
1208 if (stg_xfer(sc, ti->ti_msgoutstr, len, MESSAGE_OUT_PHASE,
1209 slp->sl_clear_atten) != 0)
1211 kprintf("%s: MSGOUT short\n", slp->sl_xname);
1215 if (slp->sl_msgphase >= MSGPH_ABORT)
1217 stg_disconnected(sc, ti);
1222 case MESSAGE_IN_PHASE:
1223 /* confirm phase and req signal */
1224 if (stg_expect_signal(sc, MESSAGE_IN_PHASE, BSTAT_REQ) <= 0)
1227 SCSI_LOW_SETUP_PHASE(ti, PH_MSGIN);
1229 /* read data with NOACK */
1230 regv = bus_space_read_1(iot, ioh, tmc_sdna);
1232 if (scsi_low_msgin(slp, ti, derror | regv) == 0)
1234 if (scsi_low_is_msgout_continue(ti, 0) != 0)
1236 scsi_low_attention(slp);
1240 /* read data with ACK */
1241 if (regv != bus_space_read_1(iot, ioh, tmc_rdata))
1243 kprintf("%s: MSGIN: data mismatch\n", slp->sl_xname);
1246 /* wait for the ack negated */
1247 stg_negate_signal(sc, BSTAT_ACK, "msgin<ACK>");
1249 if (slp->sl_msgphase != 0 && slp->sl_msgphase < MSGPH_ABORT)
1251 stg_disconnected(sc, ti);
1256 kprintf("%s: unexpected disconnect\n", slp->sl_xname);
1257 stg_disconnected(sc, ti);
1261 slp->sl_error |= FATALIO;
1262 kprintf("%s: unknown phase bus %x intr %x\n",
1263 slp->sl_xname, status, astatus);
1268 bus_space_write_1(iot, ioh, tmc_ictl, sc->sc_icinit);
1273 stg_timeout(struct stg_softc *sc)
1275 struct scsi_low_softc *slp = &sc->sc_sclow;
1276 bus_space_tag_t iot = sc->sc_iot;
1277 bus_space_handle_t ioh = sc->sc_ioh;
1281 if (slp->sl_Tnexus == NULL)
1284 status = bus_space_read_1(iot, ioh, tmc_bstat);
1285 if ((status & PHASE_MASK) == 0)
1287 if (sc->sc_ubf_timeout ++ == 0)
1290 kprintf("%s: unexpected bus free detected\n", slp->sl_xname);
1291 slp->sl_error |= FATALIO;
1292 scsi_low_print(slp, slp->sl_Tnexus);
1293 stg_disconnected(sc, slp->sl_Tnexus);
1297 switch (status & PHASE_MASK)
1299 case DATA_OUT_PHASE:
1300 if (sc->sc_dataout_timeout == 0)
1302 if ((status & BSTAT_REQ) == 0)
1304 if (bus_space_read_2(iot, ioh, tmc_fdcnt) != 0)
1306 if ((-- sc->sc_dataout_timeout) > 0)
1309 slp->sl_error |= PDMAERR;
1310 if ((slp->sl_flags & HW_WRITE_PADDING) == 0)
1312 kprintf("%s: write padding required\n",
1317 bus_space_write_1(iot, ioh, tmc_ictl, 0);
1319 tout = STG_DELAY_MAX;
1322 status = bus_space_read_1(iot, ioh, tmc_bstat);
1323 if ((status & PHASE_MASK) != DATA_OUT_PHASE)
1326 if (bus_space_read_2(iot, ioh, tmc_fdcnt) != 0)
1332 for (count = sc->sc_maxwsize; count > 0; count --)
1333 bus_space_write_1(iot, ioh, tmc_wfifo, 0);
1336 status = bus_space_read_1(iot, ioh, tmc_bstat);
1337 if ((status & PHASE_MASK) == DATA_OUT_PHASE)
1338 sc->sc_dataout_timeout = SCSI_LOW_TIMEOUT_HZ;
1340 bus_space_write_1(iot, ioh, tmc_ictl, sc->sc_icinit);