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.10 2005/12/11 01:54:07 swildner 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>
51 #include <sys/thread2.h>
54 #include <sys/device.h>
55 #include <machine/bus.h>
56 #include <machine/intr.h>
58 #include <dev/scsipi/scsi_all.h>
59 #include <dev/scsipi/scsipi_all.h>
60 #include <dev/scsipi/scsiconf.h>
61 #include <dev/scsipi/scsi_disk.h>
63 #include <machine/dvcfg.h>
64 #include <machine/physio_proc.h>
66 #include <i386/Cbus/dev/scsi_low.h>
67 #include <i386/Cbus/dev/tmc18c30reg.h>
68 #include <i386/Cbus/dev/tmc18c30var.h>
69 #endif /* __NetBSD__ */
72 #include <machine/clock.h>
73 #include <machine/cpu.h>
74 #include <machine/bus_pio.h>
75 #include <machine/bus.h>
77 #include <machine/dvcfg.h>
78 #include <machine/physio_proc.h>
80 #include <bus/cam/scsi/scsi_low.h>
81 #include "tmc18c30reg.h"
82 #include "tmc18c30var.h"
83 #endif /* __DragonFly__ */
85 /***************************************************
87 ***************************************************/
88 /* DEVICE CONFIGURATION FLAGS (MINOR)
91 * 0x02 PARITY LINE OFF
92 * 0x04 IDENTIFY MSG OFF ( = single lun)
93 * 0x08 SYNC TRANSFER OFF
95 /* #define STG_SYNC_SUPPORT */ /* NOT YET but easy */
97 /* For the 512 fifo type: change below */
98 #define TMC18C30_FIFOSZ 0x800
99 #define TMC18C30_FCBSZ 0x200
100 #define TMC18C50_FIFOSZ 0x2000
101 #define TMC18C50_FCBSZ 0x400
103 #define STG_MAX_DATA_SIZE (64 * 1024)
104 #define STG_DELAY_MAX (2 * 1000 * 1000)
105 #define STG_DELAY_INTERVAL (1)
106 #define STG_DELAY_SELECT_POLLING_MAX (5 * 1000 * 1000)
108 /***************************************************
110 ***************************************************/
111 #define STG_NTARGETS 8
114 /***************************************************
116 ***************************************************/
119 #endif /* STG_DEBUG */
128 #endif /* STG_STATICS */
130 /***************************************************
132 ***************************************************/
133 #define STG_FIFO_INTERRUPTS 0x0001
134 #define STG_WAIT_FOR_SELECT 0x0100
136 int stg_io_control = STG_IO_CONTROL_FLAGS;
138 /***************************************************
140 ***************************************************/
141 extern struct cfdriver stg_cd;
143 /**************************************************************
145 **************************************************************/
147 static void stg_pio_read (struct stg_softc *, struct targ_info *, u_int);
148 static void stg_pio_write (struct stg_softc *, struct targ_info *, u_int);
149 static int stg_xfer (struct stg_softc *, u_int8_t *, int, int, int);
150 static int stg_msg (struct stg_softc *, struct targ_info *, u_int);
151 static int stg_reselected (struct stg_softc *);
152 static int stg_disconnected (struct stg_softc *, struct targ_info *);
153 static __inline void stg_pdma_end (struct stg_softc *, struct targ_info *);
154 static int stghw_select_targ_wait (struct stg_softc *, int);
155 static int stghw_check (struct stg_softc *);
156 static void stghw_init (struct stg_softc *);
157 static int stg_negate_signal (struct stg_softc *, u_int8_t, u_char *);
158 static int stg_expect_signal (struct stg_softc *, u_int8_t, u_int8_t);
159 static int stg_world_start (struct stg_softc *, int);
160 static int stghw_start_selection (struct stg_softc *sc, struct slccb *);
161 static void stghw_bus_reset (struct stg_softc *);
162 static void stghw_attention (struct stg_softc *);
163 static int stg_target_nexus_establish (struct stg_softc *);
164 static int stg_lun_nexus_establish (struct stg_softc *);
165 static int stg_ccb_nexus_establish (struct stg_softc *);
166 static int stg_targ_init (struct stg_softc *, struct targ_info *, int);
167 static __inline void stghw_bcr_write_1 (struct stg_softc *, u_int8_t);
168 static int stg_timeout (struct stg_softc *);
169 static void stg_selection_done_and_expect_msgout (struct stg_softc *);
171 struct scsi_low_funcs stgfuncs = {
172 SC_LOW_INIT_T stg_world_start,
173 SC_LOW_BUSRST_T stghw_bus_reset,
174 SC_LOW_TARG_INIT_T stg_targ_init,
175 SC_LOW_LUN_INIT_T NULL,
177 SC_LOW_SELECT_T stghw_start_selection,
178 SC_LOW_NEXUS_T stg_lun_nexus_establish,
179 SC_LOW_NEXUS_T stg_ccb_nexus_establish,
181 SC_LOW_ATTEN_T stghw_attention,
182 SC_LOW_MSG_T stg_msg,
184 SC_LOW_TIMEOUT_T stg_timeout,
185 SC_LOW_POLL_T stgintr,
190 /****************************************************
192 ****************************************************/
194 stghw_bcr_write_1(struct stg_softc *sc, u_int8_t bcv)
197 bus_space_write_1(sc->sc_iot, sc->sc_ioh, tmc_bctl, bcv);
202 stghw_check(struct stg_softc *sc)
204 struct scsi_low_softc *slp = &sc->sc_sclow;
205 bus_space_tag_t iot = sc->sc_iot;
206 bus_space_handle_t ioh = sc->sc_ioh;
210 lsb = bus_space_read_1(iot, ioh, tmc_idlsb);
211 msb = bus_space_read_1(iot, ioh, tmc_idmsb);
212 switch (msb << 8 | lsb)
215 /* TMCCHIP_1800 not supported. (it's my policy) */
216 sc->sc_chip = TMCCHIP_1800;
220 if (bus_space_read_1(iot, ioh, tmc_cfg2) & 0x02)
222 sc->sc_chip = TMCCHIP_18C30;
223 sc->sc_fsz = TMC18C30_FIFOSZ;
224 fcbsize = TMC18C30_FCBSZ;
228 sc->sc_chip = TMCCHIP_18C50;
229 sc->sc_fsz = TMC18C50_FIFOSZ;
230 fcbsize = TMC18C50_FCBSZ;
235 sc->sc_chip = TMCCHIP_UNK;
239 sc->sc_fcRinit = FCTL_INTEN;
240 sc->sc_fcWinit = FCTL_PARENB | FCTL_INTEN;
242 if (slp->sl_cfgflags & CFG_NOATTEN)
245 sc->sc_imsg = BCTL_ATN;
246 sc->sc_busc = BCTL_BUSEN;
248 sc->sc_wthold = fcbsize + 256;
249 sc->sc_rthold = fcbsize - 256;
250 sc->sc_maxwsize = sc->sc_fsz;
252 fcb = fcbsize / (sc->sc_fsz / 16);
253 sc->sc_icinit = ICTL_CD | ICTL_SEL | ICTL_ARBIT | fcb;
258 stghw_init(struct stg_softc *sc)
260 bus_space_tag_t iot = sc->sc_iot;
261 bus_space_handle_t ioh = sc->sc_ioh;
263 bus_space_write_1(iot, ioh, tmc_ictl, 0);
264 stghw_bcr_write_1(sc, BCTL_BUSFREE);
265 bus_space_write_1(iot, ioh, tmc_fctl,
266 sc->sc_fcRinit | FCTL_CLRFIFO | FCTL_CLRINT);
267 bus_space_write_1(iot, ioh, tmc_fctl, sc->sc_fcRinit);
268 bus_space_write_1(iot, ioh, tmc_ictl, sc->sc_icinit);
270 bus_space_write_1(iot, ioh, tmc_ssctl, 0);
274 stg_targ_init(struct stg_softc *sc, struct targ_info *ti, int action)
276 struct stg_targ_info *sti = (void *) ti;
278 if (action == SCSI_LOW_INFO_ALLOC || action == SCSI_LOW_INFO_REVOKE)
280 ti->ti_width = SCSI_LOW_BUS_WIDTH_8;
281 ti->ti_maxsynch.period = 0;
282 ti->ti_maxsynch.offset = 0;
283 sti->sti_reg_synch = 0;
288 /****************************************************
290 ****************************************************/
292 stghw_attention(struct stg_softc *sc)
295 sc->sc_busc |= BCTL_ATN;
296 sc->sc_busimg |= BCTL_ATN;
297 bus_space_write_1(sc->sc_iot, sc->sc_ioh, tmc_bctl, sc->sc_busimg);
302 stghw_bus_reset(struct stg_softc *sc)
304 bus_space_tag_t iot = sc->sc_iot;
305 bus_space_handle_t ioh = sc->sc_ioh;
307 bus_space_write_1(iot, ioh, tmc_ictl, 0);
308 bus_space_write_1(iot, ioh, tmc_fctl, 0);
309 stghw_bcr_write_1(sc, BCTL_RST);
310 SCSI_LOW_DELAY(100000);
311 stghw_bcr_write_1(sc, BCTL_BUSFREE);
315 stghw_start_selection(struct stg_softc *sc, struct slccb *cb)
317 bus_space_tag_t iot = sc->sc_iot;
318 bus_space_handle_t ioh = sc->sc_ioh;
319 struct targ_info *ti = cb->ti;
322 sc->sc_tmaxcnt = cb->ccb_tcmax * 1000 * 1000;
323 sc->sc_dataout_timeout = 0;
324 sc->sc_ubf_timeout = 0;
325 stghw_bcr_write_1(sc, BCTL_BUSFREE);
326 bus_space_write_1(iot, ioh, tmc_ictl, sc->sc_icinit);
329 stat = bus_space_read_1(iot, ioh, tmc_astat);
330 if ((stat & ASTAT_INT) != 0)
333 return SCSI_LOW_START_FAIL;
336 bus_space_write_1(iot, ioh, tmc_scsiid, sc->sc_idbit);
337 bus_space_write_1(iot, ioh, tmc_fctl, sc->sc_fcRinit | FCTL_ARBIT);
340 SCSI_LOW_SETUP_PHASE(ti, PH_ARBSTART);
341 return SCSI_LOW_START_OK;
345 stg_world_start(struct stg_softc *sc, int fdone)
347 struct scsi_low_softc *slp = &sc->sc_sclow;
350 if ((slp->sl_cfgflags & CFG_NOPARITY) == 0)
351 sc->sc_fcRinit |= FCTL_PARENB;
353 sc->sc_fcRinit &= ~FCTL_PARENB;
355 if ((error = stghw_check(sc)) != 0)
359 scsi_low_bus_reset(slp);
362 SOFT_INTR_REQUIRED(slp);
367 stg_msg(struct stg_softc *sc, struct targ_info *ti, u_int msg)
369 bus_space_tag_t iot = sc->sc_iot;
370 bus_space_handle_t ioh = sc->sc_ioh;
371 struct stg_targ_info *sti = (void *) ti;
372 u_int period, offset;
374 if ((msg & SCSI_LOW_MSG_WIDE) != 0)
376 if (ti->ti_width != SCSI_LOW_BUS_WIDTH_8)
378 ti->ti_width = SCSI_LOW_BUS_WIDTH_8;
384 if ((msg & SCSI_LOW_MSG_SYNCH) == 0)
387 period = ti->ti_maxsynch.period;
388 offset = ti->ti_maxsynch.offset;
389 period = period << 2;
392 sti->sti_reg_synch = (period - 200) / 50;
394 sti->sti_reg_synch ++;
395 sti->sti_reg_synch |= SSCTL_SYNCHEN;
397 else if (period >= 100)
399 sti->sti_reg_synch = (period - 100) / 50;
401 sti->sti_reg_synch ++;
402 sti->sti_reg_synch |= SSCTL_SYNCHEN | SSCTL_FSYNCHEN;
404 bus_space_write_1(iot, ioh, tmc_ssctl, sti->sti_reg_synch);
408 /**************************************************************
409 * General probe attach
410 **************************************************************/
412 stgprobesubr(bus_space_tag_t iot, bus_space_handle_t ioh, u_int dvcfg)
416 lsb = bus_space_read_1(iot, ioh, tmc_idlsb);
417 msb = bus_space_read_1(iot, ioh, tmc_idmsb);
418 switch (msb << 8 | lsb)
432 stgprint(void *aux, const char *name)
436 printf("%s: scsibus ", name);
441 stgattachsubr(struct stg_softc *sc)
443 struct scsi_low_softc *slp = &sc->sc_sclow;
447 sc->sc_idbit = (1 << slp->sl_hostid);
448 slp->sl_funcs = &stgfuncs;
449 sc->sc_tmaxcnt = SCSI_LOW_MIN_TOUT * 1000 * 1000; /* default */
451 slp->sl_flags |= HW_READ_PADDING;
452 slp->sl_cfgflags |= CFG_ASYNC; /* XXX */
454 (void) scsi_low_attach(slp, 0, STG_NTARGETS, STG_NLUNS,
455 sizeof(struct stg_targ_info), 0);
458 /**************************************************************
460 **************************************************************/
462 stg_pdma_end(struct stg_softc *sc, struct targ_info *ti)
464 struct scsi_low_softc *slp = &sc->sc_sclow;
465 bus_space_tag_t iot = sc->sc_iot;
466 bus_space_handle_t ioh = sc->sc_ioh;
467 struct slccb *cb = slp->sl_Qnexus;
470 slp->sl_flags &= ~HW_PDMASTART;
471 sc->sc_icinit &= ~ICTL_FIFO;
472 sc->sc_dataout_timeout = 0;
476 slp->sl_error |= PDMAERR;
480 if (ti->ti_phase == PH_DATA)
482 len = bus_space_read_2(iot, ioh, tmc_fdcnt);
483 if (slp->sl_scp.scp_direction == SCSI_LOW_WRITE)
487 tres = len + slp->sl_scp.scp_datalen;
488 if (tres <= (u_int) cb->ccb_scp.scp_datalen)
490 slp->sl_scp.scp_data -= len;
491 slp->sl_scp.scp_datalen = tres;
495 slp->sl_error |= PDMAERR;
496 printf("%s len %x >= datalen %x\n",
498 len, slp->sl_scp.scp_datalen);
502 else if (slp->sl_scp.scp_direction == SCSI_LOW_READ)
506 slp->sl_error |= PDMAERR;
507 printf("%s: len %x left in fifo\n",
511 scsi_low_data_finish(slp);
516 printf("%s data phase miss\n", slp->sl_xname);
517 slp->sl_error |= PDMAERR;
521 bus_space_write_1(iot, ioh, tmc_fctl, sc->sc_fcRinit);
525 stg_pio_read(struct stg_softc *sc, struct targ_info *ti, u_int thold)
527 struct scsi_low_softc *slp = &sc->sc_sclow;
528 bus_space_tag_t iot = sc->sc_iot;
529 bus_space_handle_t ioh = sc->sc_ioh;
530 struct sc_p *sp = &slp->sl_scp;
535 if ((slp->sl_flags & HW_PDMASTART) == 0)
537 bus_space_write_1(iot, ioh, tmc_fctl,
538 sc->sc_fcRinit | FCTL_FIFOEN);
539 slp->sl_flags |= HW_PDMASTART;
542 tout = sc->sc_tmaxcnt;
548 res = bus_space_read_2(iot, ioh, tmc_fdcnt);
551 bus_space_write_1(iot, ioh, tmc_ictl,
560 stat = bus_space_read_1(iot, ioh, tmc_bstat);
561 res = bus_space_read_2(iot, ioh, tmc_fdcnt);
564 if ((stat & PHASE_MASK) != DATA_IN_PHASE)
566 if (sp->scp_datalen <= 0)
573 /* The assumtion res != 0 is valid here */
574 if (res > sp->scp_datalen)
576 if (res == (u_int) -1)
579 slp->sl_error |= PDMAERR;
580 if ((slp->sl_flags & HW_READ_PADDING) == 0)
582 printf("%s: read padding required\n",
588 if (res > STG_MAX_DATA_SIZE)
589 res = STG_MAX_DATA_SIZE;
592 (void) bus_space_read_1(iot, ioh, tmc_rfifo);
597 sp->scp_datalen -= res;
600 *sp->scp_data = bus_space_read_1(iot, ioh, tmc_rfifo);
605 bus_space_read_multi_2(iot, ioh, tmc_rfifo,
606 (u_int16_t *) sp->scp_data, res >> 1);
611 printf("%s: pio read timeout\n", slp->sl_xname);
615 stg_pio_write(struct stg_softc *sc, struct targ_info *ti, u_int thold)
617 struct scsi_low_softc *slp = &sc->sc_sclow;
618 bus_space_tag_t iot = sc->sc_iot;
619 bus_space_handle_t ioh = sc->sc_ioh;
620 struct sc_p *sp = &slp->sl_scp;
625 if ((slp->sl_flags & HW_PDMASTART) == 0)
627 stat = sc->sc_fcWinit | FCTL_FIFOEN | FCTL_FIFOW;
628 bus_space_write_1(iot, ioh, tmc_fctl, stat | FCTL_CLRFIFO);
629 bus_space_write_1(iot, ioh, tmc_fctl, stat);
630 slp->sl_flags |= HW_PDMASTART;
633 tout = sc->sc_tmaxcnt;
636 stat = bus_space_read_1(iot, ioh, tmc_bstat);
637 if ((stat & PHASE_MASK) != DATA_OUT_PHASE)
640 if (sp->scp_datalen <= 0)
642 if (sc->sc_dataout_timeout == 0)
643 sc->sc_dataout_timeout = SCSI_LOW_TIMEOUT_HZ;
650 res = bus_space_read_2(iot, ioh, tmc_fdcnt);
653 bus_space_write_1(iot, ioh, tmc_ictl,
662 res = bus_space_read_2(iot, ioh, tmc_fdcnt);
663 if (res > sc->sc_maxwsize / 2)
670 if (res == (u_int) -1)
672 res = sc->sc_maxwsize - res;
673 if (res > sp->scp_datalen)
674 res = sp->scp_datalen;
676 sp->scp_datalen -= res;
677 if ((res & 0x1) != 0)
679 bus_space_write_1(iot, ioh, tmc_wfifo, *sp->scp_data);
684 bus_space_write_multi_2(iot, ioh, tmc_wfifo,
685 (u_int16_t *) sp->scp_data, res >> 1);
690 printf("%s: pio write timeout\n", slp->sl_xname);
694 stg_negate_signal(struct stg_softc *sc, u_int8_t mask, u_char *s)
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;
702 for (wc = 0; wc < STG_DELAY_MAX / STG_DELAY_INTERVAL; wc ++)
704 regv = bus_space_read_1(bst, bsh, tmc_bstat);
705 if (regv == (u_int8_t) -1)
707 if ((regv & mask) == 0)
710 SCSI_LOW_DELAY(STG_DELAY_INTERVAL);
713 printf("%s: %s stg_negate_signal timeout\n", slp->sl_xname, s);
718 stg_expect_signal(struct stg_softc *sc, u_int8_t phase, u_int8_t mask)
720 struct scsi_low_softc *slp = &sc->sc_sclow;
721 bus_space_tag_t bst = sc->sc_iot;
722 bus_space_handle_t bsh = sc->sc_ioh;
727 for (wc = 0; wc < STG_DELAY_MAX / STG_DELAY_INTERVAL; wc ++)
729 ph = bus_space_read_1(bst, bsh, tmc_bstat);
730 if (ph == (u_int8_t) -1)
732 if ((ph & PHASE_MASK) != phase)
734 if ((ph & mask) != 0)
737 SCSI_LOW_DELAY(STG_DELAY_INTERVAL);
740 printf("%s: stg_expect_signal timeout\n", slp->sl_xname);
745 stg_xfer(struct stg_softc *sc, u_int8_t *buf, int len, int phase,
748 bus_space_tag_t iot = sc->sc_iot;
749 bus_space_handle_t ioh = sc->sc_ioh;
752 if (phase & BSTAT_IO)
753 bus_space_write_1(iot, ioh, tmc_fctl, sc->sc_fcRinit);
755 bus_space_write_1(iot, ioh, tmc_fctl, sc->sc_fcWinit);
757 for (ptr = 0; len > 0; len --)
759 rv = stg_expect_signal(sc, phase, BSTAT_REQ);
763 if (len == 1 && clear_atn != 0)
765 sc->sc_busc &= ~BCTL_ATN;
766 stghw_bcr_write_1(sc, sc->sc_busc);
767 SCSI_LOW_DEASSERT_ATN(&sc->sc_sclow);
770 if (phase & BSTAT_IO)
772 buf[ptr ++] = bus_space_read_1(iot, ioh, tmc_rdata);
776 bus_space_write_1(iot, ioh, tmc_wdata, buf[ptr ++]);
779 stg_negate_signal(sc, BSTAT_ACK, "xfer<ACK>");
783 bus_space_write_1(iot, ioh, tmc_fctl, sc->sc_fcRinit);
787 /**************************************************************
788 * disconnect & reselect (HW low)
789 **************************************************************/
791 stg_reselected(struct stg_softc *sc)
793 struct scsi_low_softc *slp = &sc->sc_sclow;
794 bus_space_tag_t iot = sc->sc_iot;
795 bus_space_handle_t ioh = sc->sc_ioh;
800 if (slp->sl_selid != NULL)
803 * Selection vs Reselection conflicts.
805 bus_space_write_1(iot, ioh, tmc_fctl, sc->sc_fcRinit);
806 stghw_bcr_write_1(sc, BCTL_BUSFREE);
808 else if (slp->sl_Tnexus != NULL)
810 printf("%s: unexpected termination\n", slp->sl_xname);
811 stg_disconnected(sc, slp->sl_Tnexus);
815 * We should ack the reselection as soon as possible,
816 * because the target would abort the current reselection seq
817 * due to reselection timeout.
819 tout = STG_DELAY_SELECT_POLLING_MAX;
822 regv = bus_space_read_1(iot, ioh, tmc_bstat);
823 if ((regv & (BSTAT_IO | BSTAT_SEL | BSTAT_BSY)) ==
824 (BSTAT_IO | BSTAT_SEL))
827 regv = bus_space_read_1(iot, ioh, tmc_bstat);
828 if ((regv & (BSTAT_IO | BSTAT_SEL | BSTAT_BSY)) ==
829 (BSTAT_IO | BSTAT_SEL))
834 printf("%s: reselction timeout I\n", slp->sl_xname);
838 sid = (u_int) bus_space_read_1(iot, ioh, tmc_scsiid);
839 if ((sid & sc->sc_idbit) == 0)
845 bus_space_write_1(iot, ioh, tmc_fctl,
846 sc->sc_fcRinit | FCTL_CLRFIFO | FCTL_CLRINT);
847 bus_space_write_1(iot, ioh, tmc_fctl, sc->sc_fcRinit);
848 stghw_bcr_write_1(sc, sc->sc_busc | BCTL_BSY);
852 regv = bus_space_read_1(iot, ioh, tmc_bstat);
853 if ((regv & (BSTAT_SEL | BSTAT_BSY)) == BSTAT_BSY)
857 printf("%s: reselction timeout II\n", slp->sl_xname);
861 sid &= ~sc->sc_idbit;
863 if (scsi_low_reselected(slp, sid) == NULL)
867 stg_statics.reselect ++;
868 #endif /* STG_STATICS */
873 stg_disconnected(struct stg_softc *sc, struct targ_info *ti)
875 struct scsi_low_softc *slp = &sc->sc_sclow;
876 bus_space_tag_t iot = sc->sc_iot;
877 bus_space_handle_t ioh = sc->sc_ioh;
879 /* clear bus status & fifo */
880 bus_space_write_1(iot, ioh, tmc_fctl, sc->sc_fcRinit | FCTL_CLRFIFO);
881 bus_space_write_1(iot, ioh, tmc_fctl, sc->sc_fcRinit);
882 stghw_bcr_write_1(sc, BCTL_BUSFREE);
883 sc->sc_icinit &= ~ICTL_FIFO;
884 sc->sc_busc &= ~BCTL_ATN;
885 sc->sc_dataout_timeout = 0;
886 sc->sc_ubf_timeout = 0;
889 stg_statics.disconnect ++;
890 #endif /* STG_STATICS */
891 scsi_low_disconnected(slp, ti);
895 /**************************************************************
897 **************************************************************/
899 stg_target_nexus_establish(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(struct stg_softc *sc)
923 stg_ccb_nexus_establish(struct stg_softc *sc)
925 struct scsi_low_softc *slp = &sc->sc_sclow;
926 struct slccb *cb = slp->sl_Qnexus;
928 sc->sc_tmaxcnt = cb->ccb_tcmax * 1000 * 1000;
932 #define STGHW_SELECT_INTERVAL 10
935 stghw_select_targ_wait(struct stg_softc *sc, int mu)
937 bus_space_tag_t iot = sc->sc_iot;
938 bus_space_handle_t ioh = sc->sc_ioh;
940 mu = mu / STGHW_SELECT_INTERVAL;
943 if ((bus_space_read_1(iot, ioh, tmc_bstat) & BSTAT_BSY) == 0)
945 SCSI_LOW_DELAY(STGHW_SELECT_INTERVAL);
949 if ((bus_space_read_1(iot, ioh, tmc_bstat) & BSTAT_BSY) != 0)
958 stg_selection_done_and_expect_msgout(struct stg_softc *sc)
960 struct scsi_low_softc *slp = &sc->sc_sclow;
961 bus_space_tag_t iot = sc->sc_iot;
962 bus_space_handle_t ioh = sc->sc_ioh;
964 bus_space_write_1(iot, ioh, tmc_fctl, sc->sc_fcRinit | FCTL_CLRFIFO);
965 bus_space_write_1(iot, ioh, tmc_fctl, sc->sc_fcRinit);
966 stghw_bcr_write_1(sc, sc->sc_imsg | sc->sc_busc);
967 SCSI_LOW_ASSERT_ATN(slp);
973 struct stg_softc *sc = arg;
974 struct scsi_low_softc *slp = &sc->sc_sclow;
975 bus_space_tag_t iot = sc->sc_iot;
976 bus_space_handle_t ioh = sc->sc_ioh;
977 struct targ_info *ti;
978 struct physio_proc *pp;
982 u_int8_t status, astatus, regv;
984 /*******************************************
986 *******************************************/
987 if (slp->sl_flags & HW_INACTIVE)
990 astatus = bus_space_read_1(iot, ioh, tmc_astat);
991 status = bus_space_read_1(iot, ioh, tmc_bstat);
993 if ((astatus & ASTAT_STATMASK) == 0 || astatus == (u_int8_t) -1)
996 bus_space_write_1(iot, ioh, tmc_ictl, 0);
997 if (astatus & ASTAT_SCSIRST)
999 bus_space_write_1(iot, ioh, tmc_fctl,
1000 sc->sc_fcRinit | FCTL_CLRFIFO);
1001 bus_space_write_1(iot, ioh, tmc_fctl, sc->sc_fcRinit);
1002 bus_space_write_1(iot, ioh, tmc_ictl, 0);
1004 scsi_low_restart(slp, SCSI_LOW_RESTART_SOFT,
1005 "bus reset (power off?)");
1009 /*******************************************
1011 *******************************************/
1015 scsi_low_print(slp, NULL);
1016 printf("%s: st %x ist %x\n\n", slp->sl_xname,
1020 SCSI_LOW_DEBUGGER("stg");
1023 #endif /* STG_DEBUG */
1025 /*******************************************
1026 * reselection & nexus
1027 *******************************************/
1028 if ((status & RESEL_PHASE_MASK)== PHASE_RESELECTED)
1030 if (stg_reselected(sc) == EJUSTRETURN)
1034 if ((ti = slp->sl_Tnexus) == NULL)
1038 if ((astatus & ASTAT_PARERR) != 0 && ti->ti_phase != PH_ARBSTART &&
1039 (sc->sc_fcRinit & FCTL_PARENB) != 0)
1041 slp->sl_error |= PARITYERR;
1042 derror = SCSI_LOW_DATA_PE;
1043 if ((status & PHASE_MASK) == MESSAGE_IN_PHASE)
1044 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_PARITY, 0);
1046 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_ERROR, 1);
1049 /*******************************************
1050 * aribitration & selection
1051 *******************************************/
1052 switch (ti->ti_phase)
1055 if ((astatus & ASTAT_ARBIT) == 0)
1058 stg_statics.arbit_fail_0 ++;
1059 #endif /* STG_STATICS */
1063 status = bus_space_read_1(iot, ioh, tmc_bstat);
1064 if ((status & BSTAT_IO) != 0)
1067 * Selection vs Reselection conflicts.
1070 stg_statics.arbit_fail_1 ++;
1071 #endif /* STG_STATICS */
1073 bus_space_write_1(iot, ioh, tmc_fctl, sc->sc_fcRinit);
1074 stghw_bcr_write_1(sc, BCTL_BUSFREE);
1075 scsi_low_arbit_fail(slp, slp->sl_Qnexus);
1080 * selection assert start.
1082 SCSI_LOW_SETUP_PHASE(ti, PH_SELSTART);
1083 scsi_low_arbit_win(slp);
1086 bus_space_write_1(iot, ioh, tmc_scsiid,
1087 sc->sc_idbit | (1 << ti->ti_id));
1088 stghw_bcr_write_1(sc, sc->sc_imsg | sc->sc_busc | BCTL_SEL);
1089 bus_space_write_1(iot, ioh, tmc_fctl, sc->sc_fcWinit);
1090 if ((stg_io_control & STG_WAIT_FOR_SELECT) != 0)
1092 /* selection abort delay 200 + 100 micro sec */
1093 if (stghw_select_targ_wait(sc, 300) == 0)
1095 SCSI_LOW_SETUP_PHASE(ti, PH_SELECTED);
1096 stg_selection_done_and_expect_msgout(sc);
1103 if ((status & BSTAT_BSY) == 0)
1105 /* selection timeout delay 250 ms */
1106 if (stghw_select_targ_wait(sc, 250 * 1000) != 0)
1108 stg_disconnected(sc, ti);
1113 SCSI_LOW_SETUP_PHASE(ti, PH_SELECTED);
1114 stg_selection_done_and_expect_msgout(sc);
1118 if ((status & BSTAT_REQ) == 0)
1120 stg_target_nexus_establish(sc);
1124 if ((status & BSTAT_REQ) == 0)
1127 /* clear a busy line */
1128 bus_space_write_1(iot, ioh, tmc_fctl, sc->sc_fcRinit);
1129 stghw_bcr_write_1(sc, sc->sc_busc);
1130 stg_target_nexus_establish(sc);
1131 if ((status & PHASE_MASK) != MESSAGE_IN_PHASE)
1133 printf("%s: unexpected phase after reselect\n",
1135 slp->sl_error |= FATALIO;
1136 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_ABORT, 1);
1142 /*******************************************
1144 *******************************************/
1145 if ((slp->sl_flags & HW_PDMASTART) && STG_IS_PHASE_DATA(status) == 0)
1147 if (slp->sl_scp.scp_direction == SCSI_LOW_READ)
1148 stg_pio_read(sc, ti, 0);
1150 stg_pdma_end(sc, ti);
1153 /*******************************************
1155 *******************************************/
1156 switch (status & PHASE_MASK)
1159 if (stg_expect_signal(sc, COMMAND_PHASE, BSTAT_REQ) <= 0)
1162 SCSI_LOW_SETUP_PHASE(ti, PH_CMD);
1163 if (scsi_low_cmd(slp, ti) != 0)
1165 scsi_low_attention(slp);
1168 if (stg_xfer(sc, slp->sl_scp.scp_cmd, slp->sl_scp.scp_cmdlen,
1169 COMMAND_PHASE, 0) != 0)
1171 printf("%s: CMDOUT short\n", slp->sl_xname);
1175 case DATA_OUT_PHASE:
1176 SCSI_LOW_SETUP_PHASE(ti, PH_DATA);
1177 if (scsi_low_data(slp, ti, &bp, SCSI_LOW_WRITE) != 0)
1179 scsi_low_attention(slp);
1182 pp = physio_proc_enter(bp);
1183 if ((sc->sc_icinit & ICTL_FIFO) != 0)
1184 stg_pio_write(sc, ti, sc->sc_wthold);
1186 stg_pio_write(sc, ti, 0);
1187 physio_proc_leave(pp);
1191 SCSI_LOW_SETUP_PHASE(ti, PH_DATA);
1192 if (scsi_low_data(slp, ti, &bp, SCSI_LOW_READ) != 0)
1194 scsi_low_attention(slp);
1197 pp = physio_proc_enter(bp);
1198 if ((sc->sc_icinit & ICTL_FIFO) != 0)
1199 stg_pio_read(sc, ti, sc->sc_rthold);
1201 stg_pio_read(sc, ti, 0);
1202 physio_proc_leave(pp);
1206 regv = stg_expect_signal(sc, STATUS_PHASE, BSTAT_REQ);
1210 SCSI_LOW_SETUP_PHASE(ti, PH_STAT);
1211 regv = bus_space_read_1(iot, ioh, tmc_sdna);
1212 if (scsi_low_statusin(slp, ti, regv | derror) != 0)
1214 scsi_low_attention(slp);
1216 if (regv != bus_space_read_1(iot, ioh, tmc_rdata))
1218 printf("%s: STATIN: data mismatch\n", slp->sl_xname);
1220 stg_negate_signal(sc, BSTAT_ACK, "statin<ACK>");
1223 case MESSAGE_OUT_PHASE:
1224 if (stg_expect_signal(sc, MESSAGE_OUT_PHASE, BSTAT_REQ) <= 0)
1227 SCSI_LOW_SETUP_PHASE(ti, PH_MSGOUT);
1228 flags = (ti->ti_ophase != ti->ti_phase) ?
1229 SCSI_LOW_MSGOUT_INIT : 0;
1230 len = scsi_low_msgout(slp, ti, flags);
1232 if (len > 1 && slp->sl_atten == 0)
1234 scsi_low_attention(slp);
1237 if (stg_xfer(sc, ti->ti_msgoutstr, len, MESSAGE_OUT_PHASE,
1238 slp->sl_clear_atten) != 0)
1240 printf("%s: MSGOUT short\n", slp->sl_xname);
1244 if (slp->sl_msgphase >= MSGPH_ABORT)
1246 stg_disconnected(sc, ti);
1251 case MESSAGE_IN_PHASE:
1252 /* confirm phase and req signal */
1253 if (stg_expect_signal(sc, MESSAGE_IN_PHASE, BSTAT_REQ) <= 0)
1256 SCSI_LOW_SETUP_PHASE(ti, PH_MSGIN);
1258 /* read data with NOACK */
1259 regv = bus_space_read_1(iot, ioh, tmc_sdna);
1261 if (scsi_low_msgin(slp, ti, derror | regv) == 0)
1263 if (scsi_low_is_msgout_continue(ti, 0) != 0)
1265 scsi_low_attention(slp);
1269 /* read data with ACK */
1270 if (regv != bus_space_read_1(iot, ioh, tmc_rdata))
1272 printf("%s: MSGIN: data mismatch\n", slp->sl_xname);
1275 /* wait for the ack negated */
1276 stg_negate_signal(sc, BSTAT_ACK, "msgin<ACK>");
1278 if (slp->sl_msgphase != 0 && slp->sl_msgphase < MSGPH_ABORT)
1280 stg_disconnected(sc, ti);
1285 printf("%s: unexpected disconnect\n", slp->sl_xname);
1286 stg_disconnected(sc, ti);
1290 slp->sl_error |= FATALIO;
1291 printf("%s: unknown phase bus %x intr %x\n",
1292 slp->sl_xname, status, astatus);
1297 bus_space_write_1(iot, ioh, tmc_ictl, sc->sc_icinit);
1302 stg_timeout(struct stg_softc *sc)
1304 struct scsi_low_softc *slp = &sc->sc_sclow;
1305 bus_space_tag_t iot = sc->sc_iot;
1306 bus_space_handle_t ioh = sc->sc_ioh;
1310 if (slp->sl_Tnexus == NULL)
1313 status = bus_space_read_1(iot, ioh, tmc_bstat);
1314 if ((status & PHASE_MASK) == 0)
1316 if (sc->sc_ubf_timeout ++ == 0)
1319 printf("%s: unexpected bus free detected\n", slp->sl_xname);
1320 slp->sl_error |= FATALIO;
1321 scsi_low_print(slp, slp->sl_Tnexus);
1322 stg_disconnected(sc, slp->sl_Tnexus);
1326 switch (status & PHASE_MASK)
1328 case DATA_OUT_PHASE:
1329 if (sc->sc_dataout_timeout == 0)
1331 if ((status & BSTAT_REQ) == 0)
1333 if (bus_space_read_2(iot, ioh, tmc_fdcnt) != 0)
1335 if ((-- sc->sc_dataout_timeout) > 0)
1338 slp->sl_error |= PDMAERR;
1339 if ((slp->sl_flags & HW_WRITE_PADDING) == 0)
1341 printf("%s: write padding required\n",
1346 bus_space_write_1(iot, ioh, tmc_ictl, 0);
1348 tout = STG_DELAY_MAX;
1351 status = bus_space_read_1(iot, ioh, tmc_bstat);
1352 if ((status & PHASE_MASK) != DATA_OUT_PHASE)
1355 if (bus_space_read_2(iot, ioh, tmc_fdcnt) != 0)
1361 for (count = sc->sc_maxwsize; count > 0; count --)
1362 bus_space_write_1(iot, ioh, tmc_wfifo, 0);
1365 status = bus_space_read_1(iot, ioh, tmc_bstat);
1366 if ((status & PHASE_MASK) == DATA_OUT_PHASE)
1367 sc->sc_dataout_timeout = SCSI_LOW_TIMEOUT_HZ;
1369 bus_space_write_1(iot, ioh, tmc_ictl, sc->sc_icinit);