1 /* $FreeBSD: src/sys/dev/stg/tmc18c30.c,v 1.1.2.5 2001/12/17 13:30:19 non Exp $ */
2 /* $NecBSD: tmc18c30.c,v 1.28.12.3 2001/06/19 04:35:48 honda Exp $ */
7 #define STG_IO_CONTROL_FLAGS (STG_FIFO_INTERRUPTS | STG_WAIT_FOR_SELECT)
10 * [NetBSD for NEC PC-98 series]
11 * Copyright (c) 1996, 1997, 1998, 1999, 2000, 2001
12 * NetBSD/pc98 porting staff. All rights reserved.
13 * Copyright (c) 1996, 1997, 1998, 1999, 2000, 2001
14 * Naofumi HONDA. All rights reserved.
15 * Copyright (c) 1996, 1997, 1998, 1999
16 * Kouichi Matsuda. All rights reserved.
18 * Redistribution and use in source and binary forms, with or without
19 * modification, are permitted provided that the following conditions
21 * 1. Redistributions of source code must retain the above copyright
22 * notice, this list of conditions and the following disclaimer.
23 * 2. Redistributions in binary form must reproduce the above copyright
24 * notice, this list of conditions and the following disclaimer in the
25 * documentation and/or other materials provided with the distribution.
26 * 3. The name of the author may not be used to endorse or promote products
27 * derived from this software without specific prior written permission.
29 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
30 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
31 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
32 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
33 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
34 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
35 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
37 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
38 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
39 * POSSIBILITY OF SUCH DAMAGE.
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/kernel.h>
47 #include <sys/queue.h>
48 #include <sys/malloc.h>
49 #include <sys/errno.h>
51 #include <sys/thread2.h>
53 #include <machine/clock.h>
54 #include <machine/cpu.h>
56 #include <bus/cam/scsi/scsi_low.h>
57 #include "tmc18c30reg.h"
58 #include "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 */
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 **************************************************************/
117 static void stg_pio_read (struct stg_softc *, struct targ_info *, u_int);
118 static void stg_pio_write (struct stg_softc *, struct targ_info *, u_int);
119 static int stg_xfer (struct stg_softc *, u_int8_t *, int, int, int);
120 static int stg_msg (struct stg_softc *, struct targ_info *, u_int);
121 static int stg_reselected (struct stg_softc *);
122 static int stg_disconnected (struct stg_softc *, struct targ_info *);
123 static __inline void stg_pdma_end (struct stg_softc *, struct targ_info *);
124 static int stghw_select_targ_wait (struct stg_softc *, int);
125 static int stghw_check (struct stg_softc *);
126 static void stghw_init (struct stg_softc *);
127 static int stg_negate_signal (struct stg_softc *, u_int8_t, u_char *);
128 static int stg_expect_signal (struct stg_softc *, u_int8_t, u_int8_t);
129 static int stg_world_start (struct stg_softc *, int);
130 static int stghw_start_selection (struct stg_softc *sc, struct slccb *);
131 static void stghw_bus_reset (struct stg_softc *);
132 static void stghw_attention (struct stg_softc *);
133 static int stg_target_nexus_establish (struct stg_softc *);
134 static int stg_lun_nexus_establish (struct stg_softc *);
135 static int stg_ccb_nexus_establish (struct stg_softc *);
136 static int stg_targ_init (struct stg_softc *, struct targ_info *, int);
137 static __inline void stghw_bcr_write_1 (struct stg_softc *, u_int8_t);
138 static int stg_timeout (struct stg_softc *);
139 static void stg_selection_done_and_expect_msgout (struct stg_softc *);
141 struct scsi_low_funcs stgfuncs = {
142 SC_LOW_INIT_T stg_world_start,
143 SC_LOW_BUSRST_T stghw_bus_reset,
144 SC_LOW_TARG_INIT_T stg_targ_init,
145 SC_LOW_LUN_INIT_T NULL,
147 SC_LOW_SELECT_T stghw_start_selection,
148 SC_LOW_NEXUS_T stg_lun_nexus_establish,
149 SC_LOW_NEXUS_T stg_ccb_nexus_establish,
151 SC_LOW_ATTEN_T stghw_attention,
152 SC_LOW_MSG_T stg_msg,
154 SC_LOW_TIMEOUT_T stg_timeout,
155 SC_LOW_POLL_T stgintr,
160 /****************************************************
162 ****************************************************/
164 stghw_bcr_write_1(struct stg_softc *sc, u_int8_t bcv)
167 bus_space_write_1(sc->sc_iot, sc->sc_ioh, tmc_bctl, bcv);
172 stghw_check(struct stg_softc *sc)
174 struct scsi_low_softc *slp = &sc->sc_sclow;
175 bus_space_tag_t iot = sc->sc_iot;
176 bus_space_handle_t ioh = sc->sc_ioh;
180 lsb = bus_space_read_1(iot, ioh, tmc_idlsb);
181 msb = bus_space_read_1(iot, ioh, tmc_idmsb);
182 switch (msb << 8 | lsb)
185 /* TMCCHIP_1800 not supported. (it's my policy) */
186 sc->sc_chip = TMCCHIP_1800;
190 if (bus_space_read_1(iot, ioh, tmc_cfg2) & 0x02)
192 sc->sc_chip = TMCCHIP_18C30;
193 sc->sc_fsz = TMC18C30_FIFOSZ;
194 fcbsize = TMC18C30_FCBSZ;
198 sc->sc_chip = TMCCHIP_18C50;
199 sc->sc_fsz = TMC18C50_FIFOSZ;
200 fcbsize = TMC18C50_FCBSZ;
205 sc->sc_chip = TMCCHIP_UNK;
209 sc->sc_fcRinit = FCTL_INTEN;
210 sc->sc_fcWinit = FCTL_PARENB | FCTL_INTEN;
212 if (slp->sl_cfgflags & CFG_NOATTEN)
215 sc->sc_imsg = BCTL_ATN;
216 sc->sc_busc = BCTL_BUSEN;
218 sc->sc_wthold = fcbsize + 256;
219 sc->sc_rthold = fcbsize - 256;
220 sc->sc_maxwsize = sc->sc_fsz;
222 fcb = fcbsize / (sc->sc_fsz / 16);
223 sc->sc_icinit = ICTL_CD | ICTL_SEL | ICTL_ARBIT | fcb;
228 stghw_init(struct stg_softc *sc)
230 bus_space_tag_t iot = sc->sc_iot;
231 bus_space_handle_t ioh = sc->sc_ioh;
233 bus_space_write_1(iot, ioh, tmc_ictl, 0);
234 stghw_bcr_write_1(sc, BCTL_BUSFREE);
235 bus_space_write_1(iot, ioh, tmc_fctl,
236 sc->sc_fcRinit | FCTL_CLRFIFO | FCTL_CLRINT);
237 bus_space_write_1(iot, ioh, tmc_fctl, sc->sc_fcRinit);
238 bus_space_write_1(iot, ioh, tmc_ictl, sc->sc_icinit);
240 bus_space_write_1(iot, ioh, tmc_ssctl, 0);
244 stg_targ_init(struct stg_softc *sc, struct targ_info *ti, int action)
246 struct stg_targ_info *sti = (void *) ti;
248 if (action == SCSI_LOW_INFO_ALLOC || action == SCSI_LOW_INFO_REVOKE)
250 ti->ti_width = SCSI_LOW_BUS_WIDTH_8;
251 ti->ti_maxsynch.period = 0;
252 ti->ti_maxsynch.offset = 0;
253 sti->sti_reg_synch = 0;
258 /****************************************************
260 ****************************************************/
262 stghw_attention(struct stg_softc *sc)
265 sc->sc_busc |= BCTL_ATN;
266 sc->sc_busimg |= BCTL_ATN;
267 bus_space_write_1(sc->sc_iot, sc->sc_ioh, tmc_bctl, sc->sc_busimg);
272 stghw_bus_reset(struct stg_softc *sc)
274 bus_space_tag_t iot = sc->sc_iot;
275 bus_space_handle_t ioh = sc->sc_ioh;
277 bus_space_write_1(iot, ioh, tmc_ictl, 0);
278 bus_space_write_1(iot, ioh, tmc_fctl, 0);
279 stghw_bcr_write_1(sc, BCTL_RST);
280 SCSI_LOW_DELAY(100000);
281 stghw_bcr_write_1(sc, BCTL_BUSFREE);
285 stghw_start_selection(struct stg_softc *sc, struct slccb *cb)
287 bus_space_tag_t iot = sc->sc_iot;
288 bus_space_handle_t ioh = sc->sc_ioh;
289 struct targ_info *ti = cb->ti;
292 sc->sc_tmaxcnt = cb->ccb_tcmax * 1000 * 1000;
293 sc->sc_dataout_timeout = 0;
294 sc->sc_ubf_timeout = 0;
295 stghw_bcr_write_1(sc, BCTL_BUSFREE);
296 bus_space_write_1(iot, ioh, tmc_ictl, sc->sc_icinit);
299 stat = bus_space_read_1(iot, ioh, tmc_astat);
300 if ((stat & ASTAT_INT) != 0)
303 return SCSI_LOW_START_FAIL;
306 bus_space_write_1(iot, ioh, tmc_scsiid, sc->sc_idbit);
307 bus_space_write_1(iot, ioh, tmc_fctl, sc->sc_fcRinit | FCTL_ARBIT);
310 SCSI_LOW_SETUP_PHASE(ti, PH_ARBSTART);
311 return SCSI_LOW_START_OK;
315 stg_world_start(struct stg_softc *sc, int fdone)
317 struct scsi_low_softc *slp = &sc->sc_sclow;
320 if ((slp->sl_cfgflags & CFG_NOPARITY) == 0)
321 sc->sc_fcRinit |= FCTL_PARENB;
323 sc->sc_fcRinit &= ~FCTL_PARENB;
325 if ((error = stghw_check(sc)) != 0)
329 scsi_low_bus_reset(slp);
332 SOFT_INTR_REQUIRED(slp);
337 stg_msg(struct stg_softc *sc, struct targ_info *ti, u_int msg)
339 bus_space_tag_t iot = sc->sc_iot;
340 bus_space_handle_t ioh = sc->sc_ioh;
341 struct stg_targ_info *sti = (void *) ti;
342 u_int period, offset;
344 if ((msg & SCSI_LOW_MSG_WIDE) != 0)
346 if (ti->ti_width != SCSI_LOW_BUS_WIDTH_8)
348 ti->ti_width = SCSI_LOW_BUS_WIDTH_8;
354 if ((msg & SCSI_LOW_MSG_SYNCH) == 0)
357 period = ti->ti_maxsynch.period;
358 offset = ti->ti_maxsynch.offset;
359 period = period << 2;
362 sti->sti_reg_synch = (period - 200) / 50;
364 sti->sti_reg_synch ++;
365 sti->sti_reg_synch |= SSCTL_SYNCHEN;
367 else if (period >= 100)
369 sti->sti_reg_synch = (period - 100) / 50;
371 sti->sti_reg_synch ++;
372 sti->sti_reg_synch |= SSCTL_SYNCHEN | SSCTL_FSYNCHEN;
374 bus_space_write_1(iot, ioh, tmc_ssctl, sti->sti_reg_synch);
378 /**************************************************************
379 * General probe attach
380 **************************************************************/
382 stgprobesubr(bus_space_tag_t iot, bus_space_handle_t ioh, u_int dvcfg)
386 lsb = bus_space_read_1(iot, ioh, tmc_idlsb);
387 msb = bus_space_read_1(iot, ioh, tmc_idmsb);
388 switch (msb << 8 | lsb)
402 stgprint(void *aux, const char *name)
406 kprintf("%s: scsibus ", name);
411 stgattachsubr(struct stg_softc *sc)
413 struct scsi_low_softc *slp = &sc->sc_sclow;
417 sc->sc_idbit = (1 << slp->sl_hostid);
418 slp->sl_funcs = &stgfuncs;
419 sc->sc_tmaxcnt = SCSI_LOW_MIN_TOUT * 1000 * 1000; /* default */
421 slp->sl_flags |= HW_READ_PADDING;
422 slp->sl_cfgflags |= CFG_ASYNC; /* XXX */
424 (void) scsi_low_attach(slp, 0, STG_NTARGETS, STG_NLUNS,
425 sizeof(struct stg_targ_info), 0);
428 /**************************************************************
430 **************************************************************/
432 stg_pdma_end(struct stg_softc *sc, struct targ_info *ti)
434 struct scsi_low_softc *slp = &sc->sc_sclow;
435 bus_space_tag_t iot = sc->sc_iot;
436 bus_space_handle_t ioh = sc->sc_ioh;
437 struct slccb *cb = slp->sl_Qnexus;
440 slp->sl_flags &= ~HW_PDMASTART;
441 sc->sc_icinit &= ~ICTL_FIFO;
442 sc->sc_dataout_timeout = 0;
446 slp->sl_error |= PDMAERR;
450 if (ti->ti_phase == PH_DATA)
452 len = bus_space_read_2(iot, ioh, tmc_fdcnt);
453 if (slp->sl_scp.scp_direction == SCSI_LOW_WRITE)
457 tres = len + slp->sl_scp.scp_datalen;
458 if (tres <= (u_int) cb->ccb_scp.scp_datalen)
460 slp->sl_scp.scp_data -= len;
461 slp->sl_scp.scp_datalen = tres;
465 slp->sl_error |= PDMAERR;
466 kprintf("%s len %x >= datalen %x\n",
468 len, slp->sl_scp.scp_datalen);
472 else if (slp->sl_scp.scp_direction == SCSI_LOW_READ)
476 slp->sl_error |= PDMAERR;
477 kprintf("%s: len %x left in fifo\n",
481 scsi_low_data_finish(slp);
486 kprintf("%s data phase miss\n", slp->sl_xname);
487 slp->sl_error |= PDMAERR;
491 bus_space_write_1(iot, ioh, tmc_fctl, sc->sc_fcRinit);
495 stg_pio_read(struct stg_softc *sc, struct targ_info *ti, u_int thold)
497 struct scsi_low_softc *slp = &sc->sc_sclow;
498 bus_space_tag_t iot = sc->sc_iot;
499 bus_space_handle_t ioh = sc->sc_ioh;
500 struct sc_p *sp = &slp->sl_scp;
505 if ((slp->sl_flags & HW_PDMASTART) == 0)
507 bus_space_write_1(iot, ioh, tmc_fctl,
508 sc->sc_fcRinit | FCTL_FIFOEN);
509 slp->sl_flags |= HW_PDMASTART;
512 tout = sc->sc_tmaxcnt;
518 res = bus_space_read_2(iot, ioh, tmc_fdcnt);
521 bus_space_write_1(iot, ioh, tmc_ictl,
530 stat = bus_space_read_1(iot, ioh, tmc_bstat);
531 res = bus_space_read_2(iot, ioh, tmc_fdcnt);
534 if ((stat & PHASE_MASK) != DATA_IN_PHASE)
536 if (sp->scp_datalen <= 0)
543 /* The assumtion res != 0 is valid here */
544 if (res > sp->scp_datalen)
546 if (res == (u_int) -1)
549 slp->sl_error |= PDMAERR;
550 if ((slp->sl_flags & HW_READ_PADDING) == 0)
552 kprintf("%s: read padding required\n",
558 if (res > STG_MAX_DATA_SIZE)
559 res = STG_MAX_DATA_SIZE;
562 (void) bus_space_read_1(iot, ioh, tmc_rfifo);
567 sp->scp_datalen -= res;
570 *sp->scp_data = bus_space_read_1(iot, ioh, tmc_rfifo);
575 bus_space_read_multi_2(iot, ioh, tmc_rfifo,
576 (u_int16_t *) sp->scp_data, res >> 1);
581 kprintf("%s: pio read timeout\n", slp->sl_xname);
585 stg_pio_write(struct stg_softc *sc, struct targ_info *ti, u_int thold)
587 struct scsi_low_softc *slp = &sc->sc_sclow;
588 bus_space_tag_t iot = sc->sc_iot;
589 bus_space_handle_t ioh = sc->sc_ioh;
590 struct sc_p *sp = &slp->sl_scp;
595 if ((slp->sl_flags & HW_PDMASTART) == 0)
597 stat = sc->sc_fcWinit | FCTL_FIFOEN | FCTL_FIFOW;
598 bus_space_write_1(iot, ioh, tmc_fctl, stat | FCTL_CLRFIFO);
599 bus_space_write_1(iot, ioh, tmc_fctl, stat);
600 slp->sl_flags |= HW_PDMASTART;
603 tout = sc->sc_tmaxcnt;
606 stat = bus_space_read_1(iot, ioh, tmc_bstat);
607 if ((stat & PHASE_MASK) != DATA_OUT_PHASE)
610 if (sp->scp_datalen <= 0)
612 if (sc->sc_dataout_timeout == 0)
613 sc->sc_dataout_timeout = SCSI_LOW_TIMEOUT_HZ;
620 res = bus_space_read_2(iot, ioh, tmc_fdcnt);
623 bus_space_write_1(iot, ioh, tmc_ictl,
632 res = bus_space_read_2(iot, ioh, tmc_fdcnt);
633 if (res > sc->sc_maxwsize / 2)
640 if (res == (u_int) -1)
642 res = sc->sc_maxwsize - res;
643 if (res > sp->scp_datalen)
644 res = sp->scp_datalen;
646 sp->scp_datalen -= res;
647 if ((res & 0x1) != 0)
649 bus_space_write_1(iot, ioh, tmc_wfifo, *sp->scp_data);
654 bus_space_write_multi_2(iot, ioh, tmc_wfifo,
655 (u_int16_t *) sp->scp_data, res >> 1);
660 kprintf("%s: pio write timeout\n", slp->sl_xname);
664 stg_negate_signal(struct stg_softc *sc, u_int8_t mask, u_char *s)
666 struct scsi_low_softc *slp = &sc->sc_sclow;
667 bus_space_tag_t bst = sc->sc_iot;
668 bus_space_handle_t bsh = sc->sc_ioh;
672 for (wc = 0; wc < STG_DELAY_MAX / STG_DELAY_INTERVAL; wc ++)
674 regv = bus_space_read_1(bst, bsh, tmc_bstat);
675 if (regv == (u_int8_t) -1)
677 if ((regv & mask) == 0)
680 SCSI_LOW_DELAY(STG_DELAY_INTERVAL);
683 kprintf("%s: %s stg_negate_signal timeout\n", slp->sl_xname, s);
688 stg_expect_signal(struct stg_softc *sc, u_int8_t phase, u_int8_t mask)
690 struct scsi_low_softc *slp = &sc->sc_sclow;
691 bus_space_tag_t bst = sc->sc_iot;
692 bus_space_handle_t bsh = sc->sc_ioh;
697 for (wc = 0; wc < STG_DELAY_MAX / STG_DELAY_INTERVAL; wc ++)
699 ph = bus_space_read_1(bst, bsh, tmc_bstat);
700 if (ph == (u_int8_t) -1)
702 if ((ph & PHASE_MASK) != phase)
704 if ((ph & mask) != 0)
707 SCSI_LOW_DELAY(STG_DELAY_INTERVAL);
710 kprintf("%s: stg_expect_signal timeout\n", slp->sl_xname);
715 stg_xfer(struct stg_softc *sc, u_int8_t *buf, int len, int phase,
718 bus_space_tag_t iot = sc->sc_iot;
719 bus_space_handle_t ioh = sc->sc_ioh;
722 if (phase & BSTAT_IO)
723 bus_space_write_1(iot, ioh, tmc_fctl, sc->sc_fcRinit);
725 bus_space_write_1(iot, ioh, tmc_fctl, sc->sc_fcWinit);
727 for (ptr = 0; len > 0; len --)
729 rv = stg_expect_signal(sc, phase, BSTAT_REQ);
733 if (len == 1 && clear_atn != 0)
735 sc->sc_busc &= ~BCTL_ATN;
736 stghw_bcr_write_1(sc, sc->sc_busc);
737 SCSI_LOW_DEASSERT_ATN(&sc->sc_sclow);
740 if (phase & BSTAT_IO)
742 buf[ptr ++] = bus_space_read_1(iot, ioh, tmc_rdata);
746 bus_space_write_1(iot, ioh, tmc_wdata, buf[ptr ++]);
749 stg_negate_signal(sc, BSTAT_ACK, "xfer<ACK>");
753 bus_space_write_1(iot, ioh, tmc_fctl, sc->sc_fcRinit);
757 /**************************************************************
758 * disconnect & reselect (HW low)
759 **************************************************************/
761 stg_reselected(struct stg_softc *sc)
763 struct scsi_low_softc *slp = &sc->sc_sclow;
764 bus_space_tag_t iot = sc->sc_iot;
765 bus_space_handle_t ioh = sc->sc_ioh;
770 if (slp->sl_selid != NULL)
773 * Selection vs Reselection conflicts.
775 bus_space_write_1(iot, ioh, tmc_fctl, sc->sc_fcRinit);
776 stghw_bcr_write_1(sc, BCTL_BUSFREE);
778 else if (slp->sl_Tnexus != NULL)
780 kprintf("%s: unexpected termination\n", slp->sl_xname);
781 stg_disconnected(sc, slp->sl_Tnexus);
785 * We should ack the reselection as soon as possible,
786 * because the target would abort the current reselection seq
787 * due to reselection timeout.
789 tout = STG_DELAY_SELECT_POLLING_MAX;
792 regv = bus_space_read_1(iot, ioh, tmc_bstat);
793 if ((regv & (BSTAT_IO | BSTAT_SEL | BSTAT_BSY)) ==
794 (BSTAT_IO | BSTAT_SEL))
797 regv = bus_space_read_1(iot, ioh, tmc_bstat);
798 if ((regv & (BSTAT_IO | BSTAT_SEL | BSTAT_BSY)) ==
799 (BSTAT_IO | BSTAT_SEL))
804 kprintf("%s: reselction timeout I\n", slp->sl_xname);
808 sid = (u_int) bus_space_read_1(iot, ioh, tmc_scsiid);
809 if ((sid & sc->sc_idbit) == 0)
815 bus_space_write_1(iot, ioh, tmc_fctl,
816 sc->sc_fcRinit | FCTL_CLRFIFO | FCTL_CLRINT);
817 bus_space_write_1(iot, ioh, tmc_fctl, sc->sc_fcRinit);
818 stghw_bcr_write_1(sc, sc->sc_busc | BCTL_BSY);
822 regv = bus_space_read_1(iot, ioh, tmc_bstat);
823 if ((regv & (BSTAT_SEL | BSTAT_BSY)) == BSTAT_BSY)
827 kprintf("%s: reselction timeout II\n", slp->sl_xname);
831 sid &= ~sc->sc_idbit;
833 if (scsi_low_reselected(slp, sid) == NULL)
837 stg_statics.reselect ++;
838 #endif /* STG_STATICS */
843 stg_disconnected(struct stg_softc *sc, struct targ_info *ti)
845 struct scsi_low_softc *slp = &sc->sc_sclow;
846 bus_space_tag_t iot = sc->sc_iot;
847 bus_space_handle_t ioh = sc->sc_ioh;
849 /* clear bus status & fifo */
850 bus_space_write_1(iot, ioh, tmc_fctl, sc->sc_fcRinit | FCTL_CLRFIFO);
851 bus_space_write_1(iot, ioh, tmc_fctl, sc->sc_fcRinit);
852 stghw_bcr_write_1(sc, BCTL_BUSFREE);
853 sc->sc_icinit &= ~ICTL_FIFO;
854 sc->sc_busc &= ~BCTL_ATN;
855 sc->sc_dataout_timeout = 0;
856 sc->sc_ubf_timeout = 0;
859 stg_statics.disconnect ++;
860 #endif /* STG_STATICS */
861 scsi_low_disconnected(slp, ti);
865 /**************************************************************
867 **************************************************************/
869 stg_target_nexus_establish(struct stg_softc *sc)
871 struct scsi_low_softc *slp = &sc->sc_sclow;
872 bus_space_tag_t iot = sc->sc_iot;
873 bus_space_handle_t ioh = sc->sc_ioh;
874 struct targ_info *ti = slp->sl_Tnexus;
875 struct stg_targ_info *sti = (void *) ti;
877 bus_space_write_1(iot, ioh, tmc_ssctl, sti->sti_reg_synch);
878 if ((stg_io_control & STG_FIFO_INTERRUPTS) != 0)
880 sc->sc_icinit |= ICTL_FIFO;
886 stg_lun_nexus_establish(struct stg_softc *sc)
893 stg_ccb_nexus_establish(struct stg_softc *sc)
895 struct scsi_low_softc *slp = &sc->sc_sclow;
896 struct slccb *cb = slp->sl_Qnexus;
898 sc->sc_tmaxcnt = cb->ccb_tcmax * 1000 * 1000;
902 #define STGHW_SELECT_INTERVAL 10
905 stghw_select_targ_wait(struct stg_softc *sc, int mu)
907 bus_space_tag_t iot = sc->sc_iot;
908 bus_space_handle_t ioh = sc->sc_ioh;
910 mu = mu / STGHW_SELECT_INTERVAL;
913 if ((bus_space_read_1(iot, ioh, tmc_bstat) & BSTAT_BSY) == 0)
915 SCSI_LOW_DELAY(STGHW_SELECT_INTERVAL);
919 if ((bus_space_read_1(iot, ioh, tmc_bstat) & BSTAT_BSY) != 0)
928 stg_selection_done_and_expect_msgout(struct stg_softc *sc)
930 struct scsi_low_softc *slp = &sc->sc_sclow;
931 bus_space_tag_t iot = sc->sc_iot;
932 bus_space_handle_t ioh = sc->sc_ioh;
934 bus_space_write_1(iot, ioh, tmc_fctl, sc->sc_fcRinit | FCTL_CLRFIFO);
935 bus_space_write_1(iot, ioh, tmc_fctl, sc->sc_fcRinit);
936 stghw_bcr_write_1(sc, sc->sc_imsg | sc->sc_busc);
937 SCSI_LOW_ASSERT_ATN(slp);
943 struct stg_softc *sc = arg;
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;
951 u_int8_t status, astatus, regv;
953 /*******************************************
955 *******************************************/
956 if (slp->sl_flags & HW_INACTIVE)
959 astatus = bus_space_read_1(iot, ioh, tmc_astat);
960 status = bus_space_read_1(iot, ioh, tmc_bstat);
962 if ((astatus & ASTAT_STATMASK) == 0 || astatus == (u_int8_t) -1)
965 bus_space_write_1(iot, ioh, tmc_ictl, 0);
966 if (astatus & ASTAT_SCSIRST)
968 bus_space_write_1(iot, ioh, tmc_fctl,
969 sc->sc_fcRinit | FCTL_CLRFIFO);
970 bus_space_write_1(iot, ioh, tmc_fctl, sc->sc_fcRinit);
971 bus_space_write_1(iot, ioh, tmc_ictl, 0);
973 scsi_low_restart(slp, SCSI_LOW_RESTART_SOFT,
974 "bus reset (power off?)");
978 /*******************************************
980 *******************************************/
984 scsi_low_print(slp, NULL);
985 kprintf("%s: st %x ist %x\n\n", slp->sl_xname,
989 SCSI_LOW_DEBUGGER("stg");
992 #endif /* STG_DEBUG */
994 /*******************************************
995 * reselection & nexus
996 *******************************************/
997 if ((status & RESEL_PHASE_MASK)== PHASE_RESELECTED)
999 if (stg_reselected(sc) == EJUSTRETURN)
1003 if ((ti = slp->sl_Tnexus) == NULL)
1007 if ((astatus & ASTAT_PARERR) != 0 && ti->ti_phase != PH_ARBSTART &&
1008 (sc->sc_fcRinit & FCTL_PARENB) != 0)
1010 slp->sl_error |= PARITYERR;
1011 derror = SCSI_LOW_DATA_PE;
1012 if ((status & PHASE_MASK) == MESSAGE_IN_PHASE)
1013 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_PARITY, 0);
1015 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_ERROR, 1);
1018 /*******************************************
1019 * aribitration & selection
1020 *******************************************/
1021 switch (ti->ti_phase)
1024 if ((astatus & ASTAT_ARBIT) == 0)
1027 stg_statics.arbit_fail_0 ++;
1028 #endif /* STG_STATICS */
1032 status = bus_space_read_1(iot, ioh, tmc_bstat);
1033 if ((status & BSTAT_IO) != 0)
1036 * Selection vs Reselection conflicts.
1039 stg_statics.arbit_fail_1 ++;
1040 #endif /* STG_STATICS */
1042 bus_space_write_1(iot, ioh, tmc_fctl, sc->sc_fcRinit);
1043 stghw_bcr_write_1(sc, BCTL_BUSFREE);
1044 scsi_low_arbit_fail(slp, slp->sl_Qnexus);
1049 * selection assert start.
1051 SCSI_LOW_SETUP_PHASE(ti, PH_SELSTART);
1052 scsi_low_arbit_win(slp);
1055 bus_space_write_1(iot, ioh, tmc_scsiid,
1056 sc->sc_idbit | (1 << ti->ti_id));
1057 stghw_bcr_write_1(sc, sc->sc_imsg | sc->sc_busc | BCTL_SEL);
1058 bus_space_write_1(iot, ioh, tmc_fctl, sc->sc_fcWinit);
1059 if ((stg_io_control & STG_WAIT_FOR_SELECT) != 0)
1061 /* selection abort delay 200 + 100 micro sec */
1062 if (stghw_select_targ_wait(sc, 300) == 0)
1064 SCSI_LOW_SETUP_PHASE(ti, PH_SELECTED);
1065 stg_selection_done_and_expect_msgout(sc);
1072 if ((status & BSTAT_BSY) == 0)
1074 /* selection timeout delay 250 ms */
1075 if (stghw_select_targ_wait(sc, 250 * 1000) != 0)
1077 stg_disconnected(sc, ti);
1082 SCSI_LOW_SETUP_PHASE(ti, PH_SELECTED);
1083 stg_selection_done_and_expect_msgout(sc);
1087 if ((status & BSTAT_REQ) == 0)
1089 stg_target_nexus_establish(sc);
1093 if ((status & BSTAT_REQ) == 0)
1096 /* clear a busy line */
1097 bus_space_write_1(iot, ioh, tmc_fctl, sc->sc_fcRinit);
1098 stghw_bcr_write_1(sc, sc->sc_busc);
1099 stg_target_nexus_establish(sc);
1100 if ((status & PHASE_MASK) != MESSAGE_IN_PHASE)
1102 kprintf("%s: unexpected phase after reselect\n",
1104 slp->sl_error |= FATALIO;
1105 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_ABORT, 1);
1111 /*******************************************
1113 *******************************************/
1114 if ((slp->sl_flags & HW_PDMASTART) && STG_IS_PHASE_DATA(status) == 0)
1116 if (slp->sl_scp.scp_direction == SCSI_LOW_READ)
1117 stg_pio_read(sc, ti, 0);
1119 stg_pdma_end(sc, ti);
1122 /*******************************************
1124 *******************************************/
1125 switch (status & PHASE_MASK)
1128 if (stg_expect_signal(sc, COMMAND_PHASE, BSTAT_REQ) <= 0)
1131 SCSI_LOW_SETUP_PHASE(ti, PH_CMD);
1132 if (scsi_low_cmd(slp, ti) != 0)
1134 scsi_low_attention(slp);
1137 if (stg_xfer(sc, slp->sl_scp.scp_cmd, slp->sl_scp.scp_cmdlen,
1138 COMMAND_PHASE, 0) != 0)
1140 kprintf("%s: CMDOUT short\n", slp->sl_xname);
1144 case DATA_OUT_PHASE:
1145 SCSI_LOW_SETUP_PHASE(ti, PH_DATA);
1146 if (scsi_low_data(slp, ti, &bp, SCSI_LOW_WRITE) != 0)
1148 scsi_low_attention(slp);
1151 if ((sc->sc_icinit & ICTL_FIFO) != 0)
1152 stg_pio_write(sc, ti, sc->sc_wthold);
1154 stg_pio_write(sc, ti, 0);
1158 SCSI_LOW_SETUP_PHASE(ti, PH_DATA);
1159 if (scsi_low_data(slp, ti, &bp, SCSI_LOW_READ) != 0)
1161 scsi_low_attention(slp);
1164 if ((sc->sc_icinit & ICTL_FIFO) != 0)
1165 stg_pio_read(sc, ti, sc->sc_rthold);
1167 stg_pio_read(sc, ti, 0);
1171 regv = stg_expect_signal(sc, STATUS_PHASE, BSTAT_REQ);
1175 SCSI_LOW_SETUP_PHASE(ti, PH_STAT);
1176 regv = bus_space_read_1(iot, ioh, tmc_sdna);
1177 if (scsi_low_statusin(slp, ti, regv | derror) != 0)
1179 scsi_low_attention(slp);
1181 if (regv != bus_space_read_1(iot, ioh, tmc_rdata))
1183 kprintf("%s: STATIN: data mismatch\n", slp->sl_xname);
1185 stg_negate_signal(sc, BSTAT_ACK, "statin<ACK>");
1188 case MESSAGE_OUT_PHASE:
1189 if (stg_expect_signal(sc, MESSAGE_OUT_PHASE, BSTAT_REQ) <= 0)
1192 SCSI_LOW_SETUP_PHASE(ti, PH_MSGOUT);
1193 flags = (ti->ti_ophase != ti->ti_phase) ?
1194 SCSI_LOW_MSGOUT_INIT : 0;
1195 len = scsi_low_msgout(slp, ti, flags);
1197 if (len > 1 && slp->sl_atten == 0)
1199 scsi_low_attention(slp);
1202 if (stg_xfer(sc, ti->ti_msgoutstr, len, MESSAGE_OUT_PHASE,
1203 slp->sl_clear_atten) != 0)
1205 kprintf("%s: MSGOUT short\n", slp->sl_xname);
1209 if (slp->sl_msgphase >= MSGPH_ABORT)
1211 stg_disconnected(sc, ti);
1216 case MESSAGE_IN_PHASE:
1217 /* confirm phase and req signal */
1218 if (stg_expect_signal(sc, MESSAGE_IN_PHASE, BSTAT_REQ) <= 0)
1221 SCSI_LOW_SETUP_PHASE(ti, PH_MSGIN);
1223 /* read data with NOACK */
1224 regv = bus_space_read_1(iot, ioh, tmc_sdna);
1226 if (scsi_low_msgin(slp, ti, derror | regv) == 0)
1228 if (scsi_low_is_msgout_continue(ti, 0) != 0)
1230 scsi_low_attention(slp);
1234 /* read data with ACK */
1235 if (regv != bus_space_read_1(iot, ioh, tmc_rdata))
1237 kprintf("%s: MSGIN: data mismatch\n", slp->sl_xname);
1240 /* wait for the ack negated */
1241 stg_negate_signal(sc, BSTAT_ACK, "msgin<ACK>");
1243 if (slp->sl_msgphase != 0 && slp->sl_msgphase < MSGPH_ABORT)
1245 stg_disconnected(sc, ti);
1250 kprintf("%s: unexpected disconnect\n", slp->sl_xname);
1251 stg_disconnected(sc, ti);
1255 slp->sl_error |= FATALIO;
1256 kprintf("%s: unknown phase bus %x intr %x\n",
1257 slp->sl_xname, status, astatus);
1262 bus_space_write_1(iot, ioh, tmc_ictl, sc->sc_icinit);
1267 stg_timeout(struct stg_softc *sc)
1269 struct scsi_low_softc *slp = &sc->sc_sclow;
1270 bus_space_tag_t iot = sc->sc_iot;
1271 bus_space_handle_t ioh = sc->sc_ioh;
1275 if (slp->sl_Tnexus == NULL)
1278 status = bus_space_read_1(iot, ioh, tmc_bstat);
1279 if ((status & PHASE_MASK) == 0)
1281 if (sc->sc_ubf_timeout ++ == 0)
1284 kprintf("%s: unexpected bus free detected\n", slp->sl_xname);
1285 slp->sl_error |= FATALIO;
1286 scsi_low_print(slp, slp->sl_Tnexus);
1287 stg_disconnected(sc, slp->sl_Tnexus);
1291 switch (status & PHASE_MASK)
1293 case DATA_OUT_PHASE:
1294 if (sc->sc_dataout_timeout == 0)
1296 if ((status & BSTAT_REQ) == 0)
1298 if (bus_space_read_2(iot, ioh, tmc_fdcnt) != 0)
1300 if ((-- sc->sc_dataout_timeout) > 0)
1303 slp->sl_error |= PDMAERR;
1304 if ((slp->sl_flags & HW_WRITE_PADDING) == 0)
1306 kprintf("%s: write padding required\n",
1311 bus_space_write_1(iot, ioh, tmc_ictl, 0);
1313 tout = STG_DELAY_MAX;
1316 status = bus_space_read_1(iot, ioh, tmc_bstat);
1317 if ((status & PHASE_MASK) != DATA_OUT_PHASE)
1320 if (bus_space_read_2(iot, ioh, tmc_fdcnt) != 0)
1326 for (count = sc->sc_maxwsize; count > 0; count --)
1327 bus_space_write_1(iot, ioh, tmc_wfifo, 0);
1330 status = bus_space_read_1(iot, ioh, tmc_bstat);
1331 if ((status & PHASE_MASK) == DATA_OUT_PHASE)
1332 sc->sc_dataout_timeout = SCSI_LOW_TIMEOUT_HZ;
1334 bus_space_write_1(iot, ioh, tmc_ictl, sc->sc_icinit);