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 * SPDX-License-Identifier: BSD-3-Clause
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.
43 #include <sys/cdefs.h>
44 __FBSDID("$FreeBSD$");
46 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/kernel.h>
51 #include <sys/queue.h>
52 #include <sys/malloc.h>
53 #include <sys/errno.h>
56 #include <machine/cpu.h>
57 #include <machine/bus.h>
59 #include <cam/scsi/scsi_low.h>
60 #include <dev/stg/tmc18c30reg.h>
61 #include <dev/stg/tmc18c30var.h>
63 /***************************************************
65 ***************************************************/
66 /* DEVICE CONFIGURATION FLAGS (MINOR)
69 * 0x02 PARITY LINE OFF
70 * 0x04 IDENTIFY MSG OFF ( = single lun)
71 * 0x08 SYNC TRANSFER OFF
73 /* #define STG_SYNC_SUPPORT */ /* NOT YET but easy */
75 /* For the 512 fifo type: change below */
76 #define TMC18C30_FIFOSZ 0x800
77 #define TMC18C30_FCBSZ 0x200
78 #define TMC18C50_FIFOSZ 0x2000
79 #define TMC18C50_FCBSZ 0x400
81 #define STG_MAX_DATA_SIZE (64 * 1024)
82 #define STG_DELAY_MAX (2 * 1000 * 1000)
83 #define STG_DELAY_INTERVAL (1)
84 #define STG_DELAY_SELECT_POLLING_MAX (5 * 1000 * 1000)
86 /***************************************************
88 ***************************************************/
89 #define STG_NTARGETS 8
92 /***************************************************
94 ***************************************************/
97 #endif /* STG_DEBUG */
100 static struct stg_statics {
106 #endif /* STG_STATICS */
108 /***************************************************
110 ***************************************************/
111 #define STG_FIFO_INTERRUPTS 0x0001
112 #define STG_WAIT_FOR_SELECT 0x0100
114 int stg_io_control = STG_IO_CONTROL_FLAGS;
116 /***************************************************
118 ***************************************************/
119 extern struct cfdriver stg_cd;
121 /**************************************************************
123 **************************************************************/
125 static void stg_pio_read(struct stg_softc *, struct targ_info *, u_int);
126 static void stg_pio_write(struct stg_softc *, struct targ_info *, u_int);
127 static int stg_xfer(struct stg_softc *, u_int8_t *, int, int, int);
128 static int stg_msg(struct stg_softc *, struct targ_info *, u_int);
129 static int stg_reselected(struct stg_softc *);
130 static int stg_disconnected(struct stg_softc *, struct targ_info *);
131 static __inline void stg_pdma_end(struct stg_softc *, struct targ_info *);
132 static int stghw_select_targ_wait(struct stg_softc *, int);
133 static int stghw_check(struct stg_softc *);
134 static void stghw_init(struct stg_softc *);
135 static int stg_negate_signal(struct stg_softc *, u_int8_t, u_char *);
136 static int stg_expect_signal(struct stg_softc *, u_int8_t, u_int8_t);
137 static int stg_world_start(struct stg_softc *, int);
138 static int stghw_start_selection(struct stg_softc *sc, struct slccb *);
139 static void stghw_bus_reset(struct stg_softc *);
140 static void stghw_attention(struct stg_softc *);
141 static int stg_target_nexus_establish(struct stg_softc *);
142 static int stg_lun_nexus_establish(struct stg_softc *);
143 static int stg_ccb_nexus_establish(struct stg_softc *);
144 static int stg_targ_init(struct stg_softc *, struct targ_info *, int);
145 static __inline void stghw_bcr_write_1(struct stg_softc *, u_int8_t);
146 static int stg_timeout(struct stg_softc *);
147 static void stg_selection_done_and_expect_msgout(struct stg_softc *);
149 struct scsi_low_funcs stgfuncs = {
150 SC_LOW_INIT_T stg_world_start,
151 SC_LOW_BUSRST_T stghw_bus_reset,
152 SC_LOW_TARG_INIT_T stg_targ_init,
153 SC_LOW_LUN_INIT_T NULL,
155 SC_LOW_SELECT_T stghw_start_selection,
156 SC_LOW_NEXUS_T stg_lun_nexus_establish,
157 SC_LOW_NEXUS_T stg_ccb_nexus_establish,
159 SC_LOW_ATTEN_T stghw_attention,
160 SC_LOW_MSG_T stg_msg,
162 SC_LOW_TIMEOUT_T stg_timeout,
163 SC_LOW_POLL_T stgintr,
168 /****************************************************
170 ****************************************************/
172 stghw_bcr_write_1(struct stg_softc *sc, u_int8_t bcv)
175 bus_write_1(sc->port_res, tmc_bctl, bcv);
181 struct stg_softc *sc;
183 struct scsi_low_softc *slp = &sc->sc_sclow;
187 lsb = bus_read_1(sc->port_res, tmc_idlsb);
188 msb = bus_read_1(sc->port_res, tmc_idmsb);
189 switch (msb << 8 | lsb)
192 /* TMCCHIP_1800 not supported. (it's my policy) */
193 sc->sc_chip = TMCCHIP_1800;
197 if (bus_read_1(sc->port_res, tmc_cfg2) & 0x02)
199 sc->sc_chip = TMCCHIP_18C30;
200 sc->sc_fsz = TMC18C30_FIFOSZ;
201 fcbsize = TMC18C30_FCBSZ;
205 sc->sc_chip = TMCCHIP_18C50;
206 sc->sc_fsz = TMC18C50_FIFOSZ;
207 fcbsize = TMC18C50_FCBSZ;
212 sc->sc_chip = TMCCHIP_UNK;
216 sc->sc_fcRinit = FCTL_INTEN;
217 sc->sc_fcWinit = FCTL_PARENB | FCTL_INTEN;
219 if (slp->sl_cfgflags & CFG_NOATTEN)
222 sc->sc_imsg = BCTL_ATN;
223 sc->sc_busc = BCTL_BUSEN;
225 sc->sc_wthold = fcbsize + 256;
226 sc->sc_rthold = fcbsize - 256;
227 sc->sc_maxwsize = sc->sc_fsz;
229 fcb = fcbsize / (sc->sc_fsz / 16);
230 sc->sc_icinit = ICTL_CD | ICTL_SEL | ICTL_ARBIT | fcb;
236 struct stg_softc *sc;
239 bus_write_1(sc->port_res, tmc_ictl, 0);
240 stghw_bcr_write_1(sc, BCTL_BUSFREE);
241 bus_write_1(sc->port_res, tmc_fctl,
242 sc->sc_fcRinit | FCTL_CLRFIFO | FCTL_CLRINT);
243 bus_write_1(sc->port_res, tmc_fctl, sc->sc_fcRinit);
244 bus_write_1(sc->port_res, tmc_ictl, sc->sc_icinit);
246 bus_write_1(sc->port_res, tmc_ssctl, 0);
250 stg_targ_init(sc, ti, action)
251 struct stg_softc *sc;
252 struct targ_info *ti;
255 struct stg_targ_info *sti = (void *) ti;
257 if (action == SCSI_LOW_INFO_ALLOC || action == SCSI_LOW_INFO_REVOKE)
259 ti->ti_width = SCSI_LOW_BUS_WIDTH_8;
260 ti->ti_maxsynch.period = 0;
261 ti->ti_maxsynch.offset = 0;
262 sti->sti_reg_synch = 0;
267 /****************************************************
269 ****************************************************/
272 struct stg_softc *sc;
275 sc->sc_busc |= BCTL_ATN;
276 sc->sc_busimg |= BCTL_ATN;
277 bus_write_1(sc->port_res, tmc_bctl, sc->sc_busimg);
283 struct stg_softc *sc;
286 bus_write_1(sc->port_res, tmc_ictl, 0);
287 bus_write_1(sc->port_res, tmc_fctl, 0);
288 stghw_bcr_write_1(sc, BCTL_RST);
290 stghw_bcr_write_1(sc, BCTL_BUSFREE);
294 stghw_start_selection(sc, cb)
295 struct stg_softc *sc;
298 struct targ_info *ti = cb->ti;
299 register u_int8_t stat;
301 sc->sc_tmaxcnt = cb->ccb_tcmax * 1000 * 1000;
302 sc->sc_dataout_timeout = 0;
303 sc->sc_ubf_timeout = 0;
304 stghw_bcr_write_1(sc, BCTL_BUSFREE);
305 bus_write_1(sc->port_res, tmc_ictl, sc->sc_icinit);
307 stat = bus_read_1(sc->port_res, tmc_astat);
308 if ((stat & ASTAT_INT) != 0)
310 return SCSI_LOW_START_FAIL;
313 bus_write_1(sc->port_res, tmc_scsiid, sc->sc_idbit);
314 bus_write_1(sc->port_res, 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(sc, fdone)
322 struct stg_softc *sc;
325 struct scsi_low_softc *slp = &sc->sc_sclow;
328 if ((slp->sl_cfgflags & CFG_NOPARITY) == 0)
329 sc->sc_fcRinit |= FCTL_PARENB;
331 sc->sc_fcRinit &= ~FCTL_PARENB;
333 if ((error = stghw_check(sc)) != 0)
337 scsi_low_bus_reset(slp);
345 struct stg_softc *sc;
346 struct targ_info *ti;
349 struct stg_targ_info *sti = (void *) ti;
350 u_int period, offset;
352 if ((msg & SCSI_LOW_MSG_WIDE) != 0)
354 if (ti->ti_width != SCSI_LOW_BUS_WIDTH_8)
356 ti->ti_width = SCSI_LOW_BUS_WIDTH_8;
362 if ((msg & SCSI_LOW_MSG_SYNCH) == 0)
365 period = ti->ti_maxsynch.period;
366 offset = ti->ti_maxsynch.offset;
367 period = period << 2;
370 sti->sti_reg_synch = (period - 200) / 50;
372 sti->sti_reg_synch ++;
373 sti->sti_reg_synch |= SSCTL_SYNCHEN;
375 else if (period >= 100)
377 sti->sti_reg_synch = (period - 100) / 50;
379 sti->sti_reg_synch ++;
380 sti->sti_reg_synch |= SSCTL_SYNCHEN | SSCTL_FSYNCHEN;
382 bus_write_1(sc->port_res, tmc_ssctl, sti->sti_reg_synch);
386 /**************************************************************
387 * General probe attach
388 **************************************************************/
390 stgprobesubr(struct resource *res, u_int dvcfg)
394 lsb = bus_read_1(res, tmc_idlsb);
395 msb = bus_read_1(res, tmc_idmsb);
396 switch (msb << 8 | lsb)
411 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);
426 gone_in(12, "stg(4) driver");
429 /**************************************************************
431 **************************************************************/
434 struct stg_softc *sc;
435 struct targ_info *ti;
437 struct scsi_low_softc *slp = &sc->sc_sclow;
438 struct slccb *cb = slp->sl_Qnexus;
441 slp->sl_flags &= ~HW_PDMASTART;
442 sc->sc_icinit &= ~ICTL_FIFO;
443 sc->sc_dataout_timeout = 0;
447 slp->sl_error |= PDMAERR;
451 if (ti->ti_phase == PH_DATA)
453 len = bus_read_2(sc->port_res, tmc_fdcnt);
454 if (slp->sl_scp.scp_direction == SCSI_LOW_WRITE)
458 tres = len + slp->sl_scp.scp_datalen;
459 if (tres <= (u_int) cb->ccb_scp.scp_datalen)
461 slp->sl_scp.scp_data -= len;
462 slp->sl_scp.scp_datalen = tres;
466 slp->sl_error |= PDMAERR;
467 device_printf(slp->sl_dev,
468 "len %x >= datalen %x\n",
469 len, slp->sl_scp.scp_datalen);
473 else if (slp->sl_scp.scp_direction == SCSI_LOW_READ)
477 slp->sl_error |= PDMAERR;
478 device_printf(slp->sl_dev,
479 "len %x left in fifo\n", len);
482 scsi_low_data_finish(slp);
487 device_printf(slp->sl_dev, "data phase miss\n");
488 slp->sl_error |= PDMAERR;
492 bus_write_1(sc->port_res, tmc_fctl, sc->sc_fcRinit);
496 stg_pio_read(sc, ti, thold)
497 struct stg_softc *sc;
498 struct targ_info *ti;
501 struct scsi_low_softc *slp = &sc->sc_sclow;
502 struct sc_p *sp = &slp->sl_scp;
507 if ((slp->sl_flags & HW_PDMASTART) == 0)
509 bus_write_1(sc->port_res, tmc_fctl,
510 sc->sc_fcRinit | FCTL_FIFOEN);
511 slp->sl_flags |= HW_PDMASTART;
514 tout = sc->sc_tmaxcnt;
519 res = bus_read_2(sc->port_res, tmc_fdcnt);
522 bus_write_1(sc->port_res, tmc_ictl,
529 stat = bus_read_1(sc->port_res, tmc_bstat);
530 res = bus_read_2(sc->port_res, tmc_fdcnt);
533 if ((stat & PHASE_MASK) != DATA_IN_PHASE)
535 if (sp->scp_datalen <= 0)
542 /* The assumtion res != 0 is valid here */
543 if (res > sp->scp_datalen)
545 if (res == (u_int) -1)
548 slp->sl_error |= PDMAERR;
549 if ((slp->sl_flags & HW_READ_PADDING) == 0)
551 device_printf(slp->sl_dev,
552 "read padding required\n");
557 if (res > STG_MAX_DATA_SIZE)
558 res = STG_MAX_DATA_SIZE;
561 (void) bus_read_1(sc->port_res, tmc_rfifo);
566 sp->scp_datalen -= res;
569 *sp->scp_data = bus_read_1(sc->port_res, tmc_rfifo);
574 bus_read_multi_2(sc->port_res, tmc_rfifo,
575 (u_int16_t *) sp->scp_data, res >> 1);
580 device_printf(slp->sl_dev, "pio read timeout\n");
584 stg_pio_write(sc, ti, thold)
585 struct stg_softc *sc;
586 struct targ_info *ti;
589 struct scsi_low_softc *slp = &sc->sc_sclow;
590 struct sc_p *sp = &slp->sl_scp;
593 register u_int8_t stat;
595 if ((slp->sl_flags & HW_PDMASTART) == 0)
597 stat = sc->sc_fcWinit | FCTL_FIFOEN | FCTL_FIFOW;
598 bus_write_1(sc->port_res, tmc_fctl, stat | FCTL_CLRFIFO);
599 bus_write_1(sc->port_res, tmc_fctl, stat);
600 slp->sl_flags |= HW_PDMASTART;
603 tout = sc->sc_tmaxcnt;
606 stat = bus_read_1(sc->port_res, 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;
619 res = bus_read_2(sc->port_res, tmc_fdcnt);
622 bus_write_1(sc->port_res, tmc_ictl,
629 res = bus_read_2(sc->port_res, tmc_fdcnt);
630 if (res > sc->sc_maxwsize / 2)
637 if (res == (u_int) -1)
639 res = sc->sc_maxwsize - res;
640 if (res > sp->scp_datalen)
641 res = sp->scp_datalen;
643 sp->scp_datalen -= res;
644 if ((res & 0x1) != 0)
646 bus_write_1(sc->port_res, tmc_wfifo, *sp->scp_data);
651 bus_write_multi_2(sc->port_res, tmc_wfifo,
652 (u_int16_t *) sp->scp_data, res >> 1);
657 device_printf(slp->sl_dev, "pio write timeout\n");
661 stg_negate_signal(struct stg_softc *sc, u_int8_t mask, u_char *s)
663 struct scsi_low_softc *slp = &sc->sc_sclow;
667 for (wc = 0; wc < STG_DELAY_MAX / STG_DELAY_INTERVAL; wc ++)
669 regv = bus_read_1(sc->port_res, tmc_bstat);
670 if (regv == (u_int8_t) -1)
672 if ((regv & mask) == 0)
675 DELAY(STG_DELAY_INTERVAL);
678 device_printf(slp->sl_dev, "%s stg_negate_signal timeout\n", s);
683 stg_expect_signal(struct stg_softc *sc, u_int8_t phase, u_int8_t mask)
685 struct scsi_low_softc *slp = &sc->sc_sclow;
690 for (wc = 0; wc < STG_DELAY_MAX / STG_DELAY_INTERVAL; wc ++)
692 ph = bus_read_1(sc->port_res, tmc_bstat);
693 if (ph == (u_int8_t) -1)
695 if ((ph & PHASE_MASK) != phase)
697 if ((ph & mask) != 0)
700 DELAY(STG_DELAY_INTERVAL);
703 device_printf(slp->sl_dev, "stg_expect_signal timeout\n");
708 stg_xfer(sc, buf, len, phase, clear_atn)
709 struct stg_softc *sc;
717 if (phase & BSTAT_IO)
718 bus_write_1(sc->port_res, tmc_fctl, sc->sc_fcRinit);
720 bus_write_1(sc->port_res, tmc_fctl, sc->sc_fcWinit);
722 for (ptr = 0; len > 0; len --)
724 rv = stg_expect_signal(sc, phase, BSTAT_REQ);
728 if (len == 1 && clear_atn != 0)
730 sc->sc_busc &= ~BCTL_ATN;
731 stghw_bcr_write_1(sc, sc->sc_busc);
732 SCSI_LOW_DEASSERT_ATN(&sc->sc_sclow);
735 if (phase & BSTAT_IO)
737 buf[ptr ++] = bus_read_1(sc->port_res, tmc_rdata);
741 bus_write_1(sc->port_res, tmc_wdata, buf[ptr ++]);
744 stg_negate_signal(sc, BSTAT_ACK, "xfer<ACK>");
748 bus_write_1(sc->port_res, tmc_fctl, sc->sc_fcRinit);
752 /**************************************************************
753 * disconnect & reselect (HW low)
754 **************************************************************/
757 struct stg_softc *sc;
759 struct scsi_low_softc *slp = &sc->sc_sclow;
764 if (slp->sl_selid != NULL)
767 * Selection vs Reselection conflicts.
769 bus_write_1(sc->port_res, tmc_fctl, sc->sc_fcRinit);
770 stghw_bcr_write_1(sc, BCTL_BUSFREE);
772 else if (slp->sl_Tnexus != NULL)
774 device_printf(slp->sl_dev, "unexpected termination\n");
775 stg_disconnected(sc, slp->sl_Tnexus);
779 * We should ack the reselection as soon as possible,
780 * because the target would abort the current reselection seq
781 * due to reselection timeout.
783 tout = STG_DELAY_SELECT_POLLING_MAX;
786 regv = bus_read_1(sc->port_res, tmc_bstat);
787 if ((regv & (BSTAT_IO | BSTAT_SEL | BSTAT_BSY)) ==
788 (BSTAT_IO | BSTAT_SEL))
791 regv = bus_read_1(sc->port_res, tmc_bstat);
792 if ((regv & (BSTAT_IO | BSTAT_SEL | BSTAT_BSY)) ==
793 (BSTAT_IO | BSTAT_SEL))
798 device_printf(slp->sl_dev, "reselction timeout I\n");
802 sid = (u_int) bus_read_1(sc->port_res, tmc_scsiid);
803 if ((sid & sc->sc_idbit) == 0)
809 bus_write_1(sc->port_res, tmc_fctl,
810 sc->sc_fcRinit | FCTL_CLRFIFO | FCTL_CLRINT);
811 bus_write_1(sc->port_res, tmc_fctl, sc->sc_fcRinit);
812 stghw_bcr_write_1(sc, sc->sc_busc | BCTL_BSY);
816 regv = bus_read_1(sc->port_res, tmc_bstat);
817 if ((regv & (BSTAT_SEL | BSTAT_BSY)) == BSTAT_BSY)
821 device_printf(slp->sl_dev, "reselction timeout II\n");
825 sid &= ~sc->sc_idbit;
827 if (scsi_low_reselected(slp, sid) == NULL)
831 stg_statics.reselect ++;
832 #endif /* STG_STATICS */
837 stg_disconnected(sc, ti)
838 struct stg_softc *sc;
839 struct targ_info *ti;
841 struct scsi_low_softc *slp = &sc->sc_sclow;
843 /* clear bus status & fifo */
844 bus_write_1(sc->port_res, tmc_fctl, sc->sc_fcRinit | FCTL_CLRFIFO);
845 bus_write_1(sc->port_res, tmc_fctl, sc->sc_fcRinit);
846 stghw_bcr_write_1(sc, BCTL_BUSFREE);
847 sc->sc_icinit &= ~ICTL_FIFO;
848 sc->sc_busc &= ~BCTL_ATN;
849 sc->sc_dataout_timeout = 0;
850 sc->sc_ubf_timeout = 0;
853 stg_statics.disconnect ++;
854 #endif /* STG_STATICS */
855 scsi_low_disconnected(slp, ti);
859 /**************************************************************
861 **************************************************************/
863 stg_target_nexus_establish(sc)
864 struct stg_softc *sc;
866 struct scsi_low_softc *slp = &sc->sc_sclow;
867 struct targ_info *ti = slp->sl_Tnexus;
868 struct stg_targ_info *sti = (void *) ti;
870 bus_write_1(sc->port_res, tmc_ssctl, sti->sti_reg_synch);
871 if ((stg_io_control & STG_FIFO_INTERRUPTS) != 0)
873 sc->sc_icinit |= ICTL_FIFO;
879 stg_lun_nexus_establish(sc)
880 struct stg_softc *sc;
887 stg_ccb_nexus_establish(sc)
888 struct stg_softc *sc;
890 struct scsi_low_softc *slp = &sc->sc_sclow;
891 struct slccb *cb = slp->sl_Qnexus;
893 sc->sc_tmaxcnt = cb->ccb_tcmax * 1000 * 1000;
897 #define STGHW_SELECT_INTERVAL 10
900 stghw_select_targ_wait(sc, mu)
901 struct stg_softc *sc;
905 mu = mu / STGHW_SELECT_INTERVAL;
908 if ((bus_read_1(sc->port_res, tmc_bstat) & BSTAT_BSY) == 0)
910 DELAY(STGHW_SELECT_INTERVAL);
914 if ((bus_read_1(sc->port_res, tmc_bstat) & BSTAT_BSY) != 0)
923 stg_selection_done_and_expect_msgout(sc)
924 struct stg_softc *sc;
926 struct scsi_low_softc *slp = &sc->sc_sclow;
928 bus_write_1(sc->port_res, tmc_fctl, sc->sc_fcRinit | FCTL_CLRFIFO);
929 bus_write_1(sc->port_res, tmc_fctl, sc->sc_fcRinit);
930 stghw_bcr_write_1(sc, sc->sc_imsg | sc->sc_busc);
931 SCSI_LOW_ASSERT_ATN(slp);
938 struct stg_softc *sc = arg;
939 struct scsi_low_softc *slp = &sc->sc_sclow;
940 struct targ_info *ti;
944 u_int8_t status, astatus, regv;
946 /*******************************************
948 *******************************************/
949 if (slp->sl_flags & HW_INACTIVE)
952 astatus = bus_read_1(sc->port_res, tmc_astat);
953 status = bus_read_1(sc->port_res, tmc_bstat);
955 if ((astatus & ASTAT_STATMASK) == 0 || astatus == (u_int8_t) -1)
958 bus_write_1(sc->port_res, tmc_ictl, 0);
959 if (astatus & ASTAT_SCSIRST)
961 bus_write_1(sc->port_res, tmc_fctl,
962 sc->sc_fcRinit | FCTL_CLRFIFO);
963 bus_write_1(sc->port_res, tmc_fctl, sc->sc_fcRinit);
964 bus_write_1(sc->port_res, tmc_ictl, 0);
966 scsi_low_restart(slp, SCSI_LOW_RESTART_SOFT,
967 "bus reset (power off?)");
971 /*******************************************
973 *******************************************/
977 scsi_low_print(slp, NULL);
978 device_printf(slp->sl_dev, "st %x ist %x\n\n",
982 kdb_enter(KDB_WHY_CAM, "stg");
985 #endif /* STG_DEBUG */
987 /*******************************************
988 * reselection & nexus
989 *******************************************/
990 if ((status & RESEL_PHASE_MASK)== PHASE_RESELECTED)
992 if (stg_reselected(sc) == EJUSTRETURN)
996 if ((ti = slp->sl_Tnexus) == NULL)
1000 if ((astatus & ASTAT_PARERR) != 0 && ti->ti_phase != PH_ARBSTART &&
1001 (sc->sc_fcRinit & FCTL_PARENB) != 0)
1003 slp->sl_error |= PARITYERR;
1004 derror = SCSI_LOW_DATA_PE;
1005 if ((status & PHASE_MASK) == MESSAGE_IN_PHASE)
1006 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_PARITY, 0);
1008 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_ERROR, 1);
1011 /*******************************************
1012 * aribitration & selection
1013 *******************************************/
1014 switch (ti->ti_phase)
1017 if ((astatus & ASTAT_ARBIT) == 0)
1020 stg_statics.arbit_fail_0 ++;
1021 #endif /* STG_STATICS */
1025 status = bus_read_1(sc->port_res, tmc_bstat);
1026 if ((status & BSTAT_IO) != 0)
1029 * Selection vs Reselection conflicts.
1032 stg_statics.arbit_fail_1 ++;
1033 #endif /* STG_STATICS */
1035 bus_write_1(sc->port_res, tmc_fctl, sc->sc_fcRinit);
1036 stghw_bcr_write_1(sc, BCTL_BUSFREE);
1037 scsi_low_arbit_fail(slp, slp->sl_Qnexus);
1042 * selection assert start.
1044 SCSI_LOW_SETUP_PHASE(ti, PH_SELSTART);
1045 scsi_low_arbit_win(slp);
1047 bus_write_1(sc->port_res, tmc_scsiid,
1048 sc->sc_idbit | (1 << ti->ti_id));
1049 stghw_bcr_write_1(sc, sc->sc_imsg | sc->sc_busc | BCTL_SEL);
1050 bus_write_1(sc->port_res, tmc_fctl, sc->sc_fcWinit);
1051 if ((stg_io_control & STG_WAIT_FOR_SELECT) != 0)
1053 /* selection abort delay 200 + 100 micro sec */
1054 if (stghw_select_targ_wait(sc, 300) == 0)
1056 SCSI_LOW_SETUP_PHASE(ti, PH_SELECTED);
1057 stg_selection_done_and_expect_msgout(sc);
1063 if ((status & BSTAT_BSY) == 0)
1065 /* selection timeout delay 250 ms */
1066 if (stghw_select_targ_wait(sc, 250 * 1000) != 0)
1068 stg_disconnected(sc, ti);
1073 SCSI_LOW_SETUP_PHASE(ti, PH_SELECTED);
1074 stg_selection_done_and_expect_msgout(sc);
1078 if ((status & BSTAT_REQ) == 0)
1080 stg_target_nexus_establish(sc);
1084 if ((status & BSTAT_REQ) == 0)
1087 /* clear a busy line */
1088 bus_write_1(sc->port_res, tmc_fctl, sc->sc_fcRinit);
1089 stghw_bcr_write_1(sc, sc->sc_busc);
1090 stg_target_nexus_establish(sc);
1091 if ((status & PHASE_MASK) != MESSAGE_IN_PHASE)
1093 device_printf(slp->sl_dev,
1094 "unexpected phase after reselect\n");
1095 slp->sl_error |= FATALIO;
1096 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_ABORT, 1);
1102 /*******************************************
1104 *******************************************/
1105 if ((slp->sl_flags & HW_PDMASTART) && STG_IS_PHASE_DATA(status) == 0)
1107 if (slp->sl_scp.scp_direction == SCSI_LOW_READ)
1108 stg_pio_read(sc, ti, 0);
1110 stg_pdma_end(sc, ti);
1113 /*******************************************
1115 *******************************************/
1116 switch (status & PHASE_MASK)
1119 if (stg_expect_signal(sc, COMMAND_PHASE, BSTAT_REQ) <= 0)
1122 SCSI_LOW_SETUP_PHASE(ti, PH_CMD);
1123 if (scsi_low_cmd(slp, ti) != 0)
1125 scsi_low_attention(slp);
1128 if (stg_xfer(sc, slp->sl_scp.scp_cmd, slp->sl_scp.scp_cmdlen,
1129 COMMAND_PHASE, 0) != 0)
1131 device_printf(slp->sl_dev, "CMDOUT short\n");
1135 case DATA_OUT_PHASE:
1136 SCSI_LOW_SETUP_PHASE(ti, PH_DATA);
1137 if (scsi_low_data(slp, ti, &bp, SCSI_LOW_WRITE) != 0)
1139 scsi_low_attention(slp);
1142 if ((sc->sc_icinit & ICTL_FIFO) != 0)
1143 stg_pio_write(sc, ti, sc->sc_wthold);
1145 stg_pio_write(sc, ti, 0);
1149 SCSI_LOW_SETUP_PHASE(ti, PH_DATA);
1150 if (scsi_low_data(slp, ti, &bp, SCSI_LOW_READ) != 0)
1152 scsi_low_attention(slp);
1155 if ((sc->sc_icinit & ICTL_FIFO) != 0)
1156 stg_pio_read(sc, ti, sc->sc_rthold);
1158 stg_pio_read(sc, ti, 0);
1162 regv = stg_expect_signal(sc, STATUS_PHASE, BSTAT_REQ);
1166 SCSI_LOW_SETUP_PHASE(ti, PH_STAT);
1167 regv = bus_read_1(sc->port_res, tmc_sdna);
1168 if (scsi_low_statusin(slp, ti, regv | derror) != 0)
1170 scsi_low_attention(slp);
1172 if (regv != bus_read_1(sc->port_res, tmc_rdata))
1174 device_printf(slp->sl_dev, "STATIN: data mismatch\n");
1176 stg_negate_signal(sc, BSTAT_ACK, "statin<ACK>");
1179 case MESSAGE_OUT_PHASE:
1180 if (stg_expect_signal(sc, MESSAGE_OUT_PHASE, BSTAT_REQ) <= 0)
1183 SCSI_LOW_SETUP_PHASE(ti, PH_MSGOUT);
1184 flags = (ti->ti_ophase != ti->ti_phase) ?
1185 SCSI_LOW_MSGOUT_INIT : 0;
1186 len = scsi_low_msgout(slp, ti, flags);
1188 if (len > 1 && slp->sl_atten == 0)
1190 scsi_low_attention(slp);
1193 if (stg_xfer(sc, ti->ti_msgoutstr, len, MESSAGE_OUT_PHASE,
1194 slp->sl_clear_atten) != 0)
1196 device_printf(slp->sl_dev, "MSGOUT short\n");
1200 if (slp->sl_msgphase >= MSGPH_ABORT)
1202 stg_disconnected(sc, ti);
1207 case MESSAGE_IN_PHASE:
1208 /* confirm phase and req signal */
1209 if (stg_expect_signal(sc, MESSAGE_IN_PHASE, BSTAT_REQ) <= 0)
1212 SCSI_LOW_SETUP_PHASE(ti, PH_MSGIN);
1214 /* read data with NOACK */
1215 regv = bus_read_1(sc->port_res, tmc_sdna);
1217 if (scsi_low_msgin(slp, ti, derror | regv) == 0)
1219 if (scsi_low_is_msgout_continue(ti, 0) != 0)
1221 scsi_low_attention(slp);
1225 /* read data with ACK */
1226 if (regv != bus_read_1(sc->port_res, tmc_rdata))
1228 device_printf(slp->sl_dev, "MSGIN: data mismatch\n");
1231 /* wait for the ack negated */
1232 stg_negate_signal(sc, BSTAT_ACK, "msgin<ACK>");
1234 if (slp->sl_msgphase != 0 && slp->sl_msgphase < MSGPH_ABORT)
1236 stg_disconnected(sc, ti);
1241 device_printf(slp->sl_dev, "unexpected disconnect\n");
1242 stg_disconnected(sc, ti);
1246 slp->sl_error |= FATALIO;
1247 device_printf(slp->sl_dev, "unknown phase bus %x intr %x\n",
1253 bus_write_1(sc->port_res, tmc_ictl, sc->sc_icinit);
1259 struct stg_softc *sc;
1261 struct scsi_low_softc *slp = &sc->sc_sclow;
1265 if (slp->sl_Tnexus == NULL)
1268 status = bus_read_1(sc->port_res, tmc_bstat);
1269 if ((status & PHASE_MASK) == 0)
1271 if (sc->sc_ubf_timeout ++ == 0)
1274 device_printf(slp->sl_dev, "unexpected bus free detected\n");
1275 slp->sl_error |= FATALIO;
1276 scsi_low_print(slp, slp->sl_Tnexus);
1277 stg_disconnected(sc, slp->sl_Tnexus);
1281 switch (status & PHASE_MASK)
1283 case DATA_OUT_PHASE:
1284 if (sc->sc_dataout_timeout == 0)
1286 if ((status & BSTAT_REQ) == 0)
1288 if (bus_read_2(sc->port_res, tmc_fdcnt) != 0)
1290 if ((-- sc->sc_dataout_timeout) > 0)
1293 slp->sl_error |= PDMAERR;
1294 if ((slp->sl_flags & HW_WRITE_PADDING) == 0)
1296 device_printf(slp->sl_dev, "write padding required\n");
1300 bus_write_1(sc->port_res, tmc_ictl, 0);
1302 tout = STG_DELAY_MAX;
1305 status = bus_read_1(sc->port_res, tmc_bstat);
1306 if ((status & PHASE_MASK) != DATA_OUT_PHASE)
1309 if (bus_read_2(sc->port_res, tmc_fdcnt) != 0)
1315 for (count = sc->sc_maxwsize; count > 0; count --)
1316 bus_write_1(sc->port_res, tmc_wfifo, 0);
1319 status = bus_read_1(sc->port_res, tmc_bstat);
1320 if ((status & PHASE_MASK) == DATA_OUT_PHASE)
1321 sc->sc_dataout_timeout = SCSI_LOW_TIMEOUT_HZ;
1323 bus_write_1(sc->port_res, tmc_ictl, sc->sc_icinit);