1 /* $NecBSD: ct.c,v 1.13.12.5 2001/06/26 07:31:53 honda Exp $ */
8 #define CT_IO_CONTROL_FLAGS (CT_USE_CCSEQ | CT_FAST_INTR)
11 * [NetBSD for NEC PC-98 series]
12 * Copyright (c) 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001
13 * NetBSD/pc98 porting staff. All rights reserved.
15 * Copyright (c) 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001
16 * Naofumi HONDA. 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.
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/kernel.h>
47 #include <sys/queue.h>
48 #include <sys/malloc.h>
49 #include <sys/errno.h>
51 #include <machine/bus.h>
53 #include <cam/scsi/scsi_low.h>
55 #include <dev/ic/wd33c93reg.h>
56 #include <dev/ct/ctvar.h>
57 #include <dev/ct/ct_machdep.h>
61 #define CT_RESET_DEFAULT 2000
62 #define CT_DELAY_MAX (2 * 1000 * 1000)
63 #define CT_DELAY_INTERVAL (1)
65 /***************************************************
67 ***************************************************/
72 /***************************************************
74 ***************************************************/
75 #define CT_USE_CCSEQ 0x0100
76 #define CT_FAST_INTR 0x0200
78 u_int ct_io_control = CT_IO_CONTROL_FLAGS;
80 /***************************************************
82 ***************************************************/
83 u_int8_t cthw_cmdlevel[256] = {
84 /* 0 1 2 3 4 5 6 7 8 9 A B C E D F */
85 /*0*/0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,1 ,0 ,1 ,0 ,0 ,0 ,0 ,0 ,
86 /*1*/0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,
87 /*2*/0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,1 ,0 ,1 ,0 ,0 ,0 ,0 ,0 ,
88 /*3*/0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,
89 /*4*/0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,
90 /*5*/0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,
91 /*6*/0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,
92 /*7*/0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,
93 /*8*/0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,
94 /*9*/0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,
95 /*A*/0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,
96 /*B*/0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,
97 /*C*/0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,
98 /*D*/0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,
99 /*E*/0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,
100 /*F*/0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,
104 /* default synch data table */
105 /* A 10 6.6 5.0 4.0 3.3 2.8 2.5 2.0 M/s */
106 /* X 100 150 200 250 300 350 400 500 ns */
107 static struct ct_synch_data ct_synch_data_FSCSI[] = {
108 {25, 0xa0}, {37, 0xb0}, {50, 0x20}, {62, 0xd0}, {75, 0x30},
109 {87, 0xf0}, {100, 0x40}, {125, 0x50}, {0, 0}
112 static struct ct_synch_data ct_synch_data_SCSI[] = {
113 {50, 0x20}, {75, 0x30}, {100, 0x40}, {125, 0x50}, {0, 0}
116 /***************************************************
118 ***************************************************/
119 extern struct cfdriver ct_cd;
121 /*****************************************************************
122 * Interface functions
123 *****************************************************************/
124 static int ct_xfer(struct ct_softc *, u_int8_t *, int, int, u_int *);
125 static void ct_io_xfer(struct ct_softc *);
126 static int ct_reselected(struct ct_softc *, u_int8_t);
127 static void ct_phase_error(struct ct_softc *, u_int8_t);
128 static int ct_start_selection(struct ct_softc *, struct slccb *);
129 static int ct_msg(struct ct_softc *, struct targ_info *, u_int);
130 static int ct_world_start(struct ct_softc *, int);
131 static __inline void cthw_phase_bypass(struct ct_softc *, u_int8_t);
132 static int cthw_chip_reset(struct ct_bus_access_handle *, int *, int, int);
133 static void cthw_bus_reset(struct ct_softc *);
134 static int ct_ccb_nexus_establish(struct ct_softc *);
135 static int ct_lun_nexus_establish(struct ct_softc *);
136 static int ct_target_nexus_establish(struct ct_softc *, int, int);
137 static void cthw_attention(struct ct_softc *);
138 static int ct_targ_init(struct ct_softc *, struct targ_info *, int);
139 static int ct_unbusy(struct ct_softc *);
140 static void ct_attention(struct ct_softc *);
141 static struct ct_synch_data *ct_make_synch_table(struct ct_softc *);
142 static int ct_catch_intr(struct ct_softc *);
144 struct scsi_low_funcs ct_funcs = {
145 SC_LOW_INIT_T ct_world_start,
146 SC_LOW_BUSRST_T cthw_bus_reset,
147 SC_LOW_TARG_INIT_T ct_targ_init,
148 SC_LOW_LUN_INIT_T NULL,
150 SC_LOW_SELECT_T ct_start_selection,
151 SC_LOW_NEXUS_T ct_lun_nexus_establish,
152 SC_LOW_NEXUS_T ct_ccb_nexus_establish,
154 SC_LOW_ATTEN_T cthw_attention,
157 SC_LOW_TIMEOUT_T NULL,
158 SC_LOW_POLL_T ctintr,
160 NULL, /* SC_LOW_POWER_T cthw_power, */
163 /**************************************************
165 **************************************************/
167 cthw_phase_bypass(ct, ph)
171 struct ct_bus_access_handle *chp = &ct->sc_ch;
173 ct_cr_write_1(chp, wd3s_cph, ph);
174 ct_cr_write_1(chp, wd3s_cmd, WD3S_SELECT_ATN_TFR);
183 * wd33c93 does not have bus reset function.
185 if (ct->ct_bus_reset != NULL)
186 ((*ct->ct_bus_reset) (ct));
190 cthw_chip_reset(chp, chiprevp, chipclk, hostid)
191 struct ct_bus_access_handle *chp;
195 #define CT_SELTIMEOUT_20MHz_REGV (0x80)
200 /* issue abort cmd */
201 ct_cr_write_1(chp, wd3s_cmd, WD3S_ABORT);
202 DELAY(1000); /* 1ms wait */
203 (void) ct_stat_read_1(chp);
204 (void) ct_cr_read_1(chp, wd3s_stat);
206 /* setup chip registers */
208 seltout = CT_SELTIMEOUT_20MHz_REGV;
213 seltout = (seltout * chipclk) / 20;
219 seltout = (seltout * chipclk) / 20;
225 seltout = (seltout * chipclk) / 20;
230 panic("ct: illegal chip clk rate");
234 regv |= IDR_EHP | hostid | IDR_RAF | IDR_EAF;
235 ct_cr_write_1(chp, wd3s_oid, regv);
237 ct_cr_write_1(chp, wd3s_cmd, WD3S_RESET);
238 for (wc = CT_RESET_DEFAULT; wc > 0; wc --)
240 aux = ct_stat_read_1(chp);
241 if (aux != 0xff && (aux & STR_INT))
243 regv = ct_cr_read_1(chp, wd3s_stat);
244 if (regv == BSR_RESET || regv == BSR_AFM_RESET)
247 ct_cr_write_1(chp, wd3s_cmd, WD3S_RESET);
254 ct_cr_write_1(chp, wd3s_tout, seltout);
255 ct_cr_write_1(chp, wd3s_sid, SIDR_RESEL);
256 ct_cr_write_1(chp, wd3s_ctrl, CR_DEFAULT);
257 ct_cr_write_1(chp, wd3s_synch, 0);
258 if (chiprevp != NULL)
260 *chiprevp = CT_WD33C93;
261 if (regv == BSR_RESET)
264 *chiprevp = CT_WD33C93_A;
265 ct_cr_write_1(chp, wd3s_qtag, 0xaa);
266 if (ct_cr_read_1(chp, wd3s_qtag) != 0xaa)
268 ct_cr_write_1(chp, wd3s_qtag, 0x0);
271 ct_cr_write_1(chp, wd3s_qtag, 0x55);
272 if (ct_cr_read_1(chp, wd3s_qtag) != 0x55)
274 ct_cr_write_1(chp, wd3s_qtag, 0x0);
277 ct_cr_write_1(chp, wd3s_qtag, 0x0);
278 *chiprevp = CT_WD33C93_B;
282 (void) ct_stat_read_1(chp);
283 (void) ct_cr_read_1(chp, wd3s_stat);
287 static struct ct_synch_data *
288 ct_make_synch_table(ct)
291 struct ct_synch_data *sdtp, *sdp;
292 u_int base, i, period;
294 sdtp = sdp = &ct->sc_default_sdt[0];
296 if ((ct->sc_chipclk % 5) == 0)
297 base = 1000 / (5 * 2); /* 5 MHz type */
299 base = 1000 / (4 * 2); /* 4 MHz type */
301 if (ct->sc_chiprev >= CT_WD33C93_B)
304 for (i = 2; i < 8; i ++, sdp ++)
306 period = (base * i) / 2;
307 if (period >= 200) /* 5 MHz */
309 sdp->cs_period = period / 4;
310 sdp->cs_syncr = (i * 0x10) | 0x80;
314 for (i = 2; i < 8; i ++, sdp ++)
317 if (period > 500) /* 2 MHz */
319 sdp->cs_period = period / 4;
320 sdp->cs_syncr = (i * 0x10);
328 /**************************************************
330 **************************************************/
332 ctprobesubr(chp, dvcfg, hsid, chipclk, chiprevp)
333 struct ct_bus_access_handle *chp;
334 u_int dvcfg, chipclk;
340 if ((ct_stat_read_1(chp) & STR_BSY) != 0)
343 if (cthw_chip_reset(chp, chiprevp, chipclk, hsid) != 0)
352 struct scsi_low_softc *slp = &ct->sc_sclow;
354 ct->sc_tmaxcnt = SCSI_LOW_MIN_TOUT * 1000 * 1000; /* default */
355 slp->sl_funcs = &ct_funcs;
356 slp->sl_flags |= HW_READ_PADDING;
357 (void) scsi_low_attach(slp, 0, CT_NTARGETS, CT_NLUNS,
358 sizeof(struct ct_targ_info), 0);
361 /**************************************************
362 * SCSI LOW interface functions
363 **************************************************/
368 struct ct_bus_access_handle *chp = &ct->sc_ch;
371 if ((ct_stat_read_1(chp) & (STR_BSY | STR_CIP)) != 0)
374 ct_cr_write_1(chp, wd3s_cmd, WD3S_ASSERT_ATN);
376 if ((ct_stat_read_1(chp) & STR_LCI) == 0)
386 struct scsi_low_softc *slp = &ct->sc_sclow;
388 if (slp->sl_atten == 0)
391 scsi_low_attention(slp);
393 else if (ct->sc_atten != 0)
401 ct_targ_init(ct, ti, action)
403 struct targ_info *ti;
406 struct ct_targ_info *cti = (void *) ti;
408 if (action == SCSI_LOW_INFO_ALLOC || action == SCSI_LOW_INFO_REVOKE)
410 if (ct->sc_sdp == NULL)
412 ct->sc_sdp = ct_make_synch_table(ct);
415 switch (ct->sc_chiprev)
418 ti->ti_maxsynch.offset = 5;
423 ti->ti_maxsynch.offset = 12;
428 ti->ti_maxsynch.offset = 12;
432 ti->ti_maxsynch.period = ct->sc_sdp[0].cs_period;
433 ti->ti_width = SCSI_LOW_BUS_WIDTH_8;
434 cti->cti_syncreg = 0;
441 ct_world_start(ct, fdone)
445 struct scsi_low_softc *slp = &ct->sc_sclow;
446 struct ct_bus_access_handle *chp = &ct->sc_ch;
448 if (ct->sc_sdp == NULL)
450 ct->sc_sdp = ct_make_synch_table(ct);
453 if (slp->sl_cfgflags & CFG_NOPARITY)
454 ct->sc_creg = CR_DEFAULT;
456 ct->sc_creg = CR_DEFAULT_HP;
458 if (ct->sc_dma & CT_DMA_DMASTART)
459 (*ct->ct_dma_xfer_stop) (ct);
460 if (ct->sc_dma & CT_DMA_PIOSTART)
461 (*ct->ct_pio_xfer_stop) (ct);
465 cthw_chip_reset(chp, NULL, ct->sc_chipclk, slp->sl_hostid);
466 scsi_low_bus_reset(slp);
467 cthw_chip_reset(chp, NULL, ct->sc_chipclk, slp->sl_hostid);
473 ct_start_selection(ct, cb)
477 struct scsi_low_softc *slp = &ct->sc_sclow;
478 struct ct_bus_access_handle *chp = &ct->sc_ch;
480 struct targ_info *ti = slp->sl_Tnexus;
481 struct lun_info *li = slp->sl_Lnexus;
485 ct->sc_tmaxcnt = cb->ccb_tcmax * 1000 * 1000;
489 if (scsi_low_is_disconnect_ok(cb) != 0)
491 if (ct->sc_chiprev >= CT_WD33C93_A)
493 else if (cthw_cmdlevel[slp->sl_scp.scp_cmd[0]] != 0)
498 scsi_low_is_msgout_continue(ti, SCSI_LOW_MSG_IDENTIFY) == 0)
500 cmd = WD3S_SELECT_ATN_TFR;
501 ct->sc_satgo = CT_SAT_GOING;
505 cmd = WD3S_SELECT_ATN;
509 if ((ct_stat_read_1(chp) & (STR_BSY | STR_INT | STR_CIP)) != 0)
510 return SCSI_LOW_START_FAIL;
512 if ((ct->sc_satgo & CT_SAT_GOING) != 0)
514 (void) scsi_low_msgout(slp, ti, SCSI_LOW_MSGOUT_INIT);
515 scsi_low_cmd(slp, ti);
516 ct_cr_write_1(chp, wd3s_oid, slp->sl_scp.scp_cmdlen);
517 ct_write_cmds(chp, slp->sl_scp.scp_cmd, slp->sl_scp.scp_cmdlen);
521 /* anyway attention assert */
522 SCSI_LOW_ASSERT_ATN(slp);
525 ct_target_nexus_establish(ct, li->li_lun, slp->sl_scp.scp_direction);
528 if ((ct_stat_read_1(chp) & (STR_BSY | STR_INT | STR_CIP)) == 0)
531 * Reload a lun again here.
533 ct_cr_write_1(chp, wd3s_lun, li->li_lun);
534 ct_cr_write_1(chp, wd3s_cmd, cmd);
535 if ((ct_stat_read_1(chp) & STR_LCI) == 0)
538 SCSI_LOW_SETUP_PHASE(ti, PH_SELSTART);
539 return SCSI_LOW_START_OK;
543 return SCSI_LOW_START_FAIL;
549 struct targ_info *ti;
552 struct ct_bus_access_handle *chp = &ct->sc_ch;
553 struct ct_targ_info *cti = (void *) ti;
554 struct ct_synch_data *csp = ct->sc_sdp;
555 u_int offset, period;
558 if ((msg & SCSI_LOW_MSG_WIDE) != 0)
560 if (ti->ti_width != SCSI_LOW_BUS_WIDTH_8)
562 ti->ti_width = SCSI_LOW_BUS_WIDTH_8;
568 if ((msg & SCSI_LOW_MSG_SYNCH) == 0)
571 offset = ti->ti_maxsynch.offset;
572 period = ti->ti_maxsynch.period;
573 for ( ; csp->cs_period != 0; csp ++)
575 if (period == csp->cs_period)
579 if (ti->ti_maxsynch.period != 0 && csp->cs_period == 0)
581 ti->ti_maxsynch.period = 0;
582 ti->ti_maxsynch.offset = 0;
583 cti->cti_syncreg = 0;
588 cti->cti_syncreg = ((offset & 0x0f) | csp->cs_syncr);
592 if (ct->ct_synch_setup != 0)
593 (*ct->ct_synch_setup) (ct, ti);
594 ct_cr_write_1(chp, wd3s_synch, cti->cti_syncreg);
598 /*************************************************
600 *************************************************/
602 ct_xfer(ct, data, len, direction, statp)
608 struct ct_bus_access_handle *chp = &ct->sc_ch;
610 register u_int8_t aux;
615 ct_cr_write_1(chp, wd3s_cmd, WD3S_SBT | WD3S_TFR_INFO);
619 cthw_set_count(chp, len);
620 ct_cr_write_1(chp, wd3s_cmd, WD3S_TFR_INFO);
623 aux = ct_stat_read_1(chp);
624 if ((aux & STR_LCI) != 0)
626 cthw_set_count(chp, 0);
630 for (wc = 0; wc < ct->sc_tmaxcnt; wc ++)
632 /* check data ready */
633 if ((aux & (STR_BSY | STR_DBR)) == (STR_BSY | STR_DBR))
635 if (direction == SCSI_LOW_READ)
637 *data = ct_cr_read_1(chp, wd3s_data);
638 if ((aux & STR_PE) != 0)
639 *statp |= SCSI_LOW_DATA_PE;
643 ct_cr_write_1(chp, wd3s_data, *data);
655 /* check phase miss */
656 aux = ct_stat_read_1(chp);
657 if ((aux & STR_INT) != 0)
663 #define CT_PADDING_BUF_SIZE 32
669 struct scsi_low_softc *slp = &ct->sc_sclow;
670 struct ct_bus_access_handle *chp = &ct->sc_ch;
671 struct sc_p *sp = &slp->sl_scp;
674 u_int8_t pbuf[CT_PADDING_BUF_SIZE];
677 ct_cr_write_1(chp, wd3s_ctrl, ct->sc_creg);
679 if (sp->scp_datalen <= 0)
681 slp->sl_error |= PDMAERR;
683 if (slp->sl_scp.scp_direction == SCSI_LOW_WRITE)
684 bzero(pbuf, CT_PADDING_BUF_SIZE);
685 ct_xfer(ct, pbuf, CT_PADDING_BUF_SIZE,
686 sp->scp_direction, &stat);
690 len = ct_xfer(ct, sp->scp_data, sp->scp_datalen,
691 sp->scp_direction, &stat);
692 sp->scp_data += (sp->scp_datalen - len);
693 sp->scp_datalen = len;
697 /**************************************************
699 **************************************************/
707 struct ct_err ct_cmderr[] = {
708 /*0*/ { "illegal cmd", FATALIO, SCSI_LOW_MSG_ABORT, 1},
709 /*1*/ { "unexpected bus free", FATALIO, 0, 1},
710 /*2*/ { NULL, SELTIMEOUTIO, 0, 1},
711 /*3*/ { "scsi bus parity error", PARITYERR, SCSI_LOW_MSG_ERROR, 0},
712 /*4*/ { "scsi bus parity error", PARITYERR, SCSI_LOW_MSG_ERROR, 0},
713 /*5*/ { "unknown" , FATALIO, SCSI_LOW_MSG_ABORT, 1},
714 /*6*/ { "miss reselection (target mode)", FATALIO, SCSI_LOW_MSG_ABORT, 0},
715 /*7*/ { "wrong status byte", PARITYERR, SCSI_LOW_MSG_ERROR, 0},
719 ct_phase_error(ct, scsi_status)
721 u_int8_t scsi_status;
723 struct scsi_low_softc *slp = &ct->sc_sclow;
724 struct targ_info *ti = slp->sl_Tnexus;
728 if ((scsi_status & BSR_CM) == BSR_CMDERR &&
729 (scsi_status & BSR_PHVALID) == 0)
731 pep = &ct_cmderr[scsi_status & BSR_PM];
732 slp->sl_error |= pep->pe_err;
733 if ((pep->pe_err & PARITYERR) != 0)
735 if (ti->ti_phase == PH_MSGIN)
736 msg = SCSI_LOW_MSG_PARITY;
738 msg = SCSI_LOW_MSG_ERROR;
741 msg = pep->pe_errmsg;
744 scsi_low_assert_msg(slp, slp->sl_Tnexus, msg, 1);
746 if (pep->pe_msg != NULL)
748 device_printf(slp->sl_dev, "phase error: %s",
750 scsi_low_print(slp, slp->sl_Tnexus);
753 if (pep->pe_done != 0)
754 scsi_low_disconnected(slp, ti);
758 slp->sl_error |= FATALIO;
759 scsi_low_restart(slp, SCSI_LOW_RESTART_HARD, "phase error");
763 /**************************************************
764 * ### SCSI PHASE SEQUENCER ###
765 **************************************************/
767 ct_reselected(ct, scsi_status)
769 u_int8_t scsi_status;
771 struct scsi_low_softc *slp = &ct->sc_sclow;
772 struct ct_bus_access_handle *chp = &ct->sc_ch;
773 struct targ_info *ti;
778 ct->sc_satgo &= ~CT_SAT_GOING;
779 regv = ct_cr_read_1(chp, wd3s_sid);
780 if ((regv & SIDR_VALID) == 0)
783 sid = regv & SIDR_IDM;
784 if ((ti = scsi_low_reselected(slp, sid)) == NULL)
787 ct_target_nexus_establish(ct, 0, SCSI_LOW_READ);
788 if (scsi_status != BSR_AFM_RESEL)
791 SCSI_LOW_SETUP_PHASE(ti, PH_MSGIN);
792 regv = ct_cr_read_1(chp, wd3s_data);
793 if (scsi_low_msgin(slp, ti, (u_int) regv) == 0)
795 if (scsi_low_is_msgout_continue(ti, 0) != 0)
797 /* XXX: scsi_low_attetion */
798 scsi_low_attention(slp);
802 if (ct->sc_atten != 0)
807 ct_cr_write_1(chp, wd3s_cmd, WD3S_NEGATE_ACK);
812 ct_target_nexus_establish(ct, lun, dir)
816 struct scsi_low_softc *slp = &ct->sc_sclow;
817 struct ct_bus_access_handle *chp = &ct->sc_ch;
818 struct targ_info *ti = slp->sl_Tnexus;
819 struct ct_targ_info *cti = (void *) ti;
821 if (dir == SCSI_LOW_WRITE)
822 ct_cr_write_1(chp, wd3s_did, ti->ti_id);
824 ct_cr_write_1(chp, wd3s_did, ti->ti_id | DIDR_DPD);
825 ct_cr_write_1(chp, wd3s_lun, lun);
826 ct_cr_write_1(chp, wd3s_ctrl, ct->sc_creg | CR_DMA);
827 ct_cr_write_1(chp, wd3s_cph, 0);
828 ct_cr_write_1(chp, wd3s_synch, cti->cti_syncreg);
829 cthw_set_count(chp, 0);
834 ct_lun_nexus_establish(ct)
837 struct scsi_low_softc *slp = &ct->sc_sclow;
838 struct ct_bus_access_handle *chp = &ct->sc_ch;
839 struct lun_info *li = slp->sl_Lnexus;
841 ct_cr_write_1(chp, wd3s_lun, li->li_lun);
846 ct_ccb_nexus_establish(ct)
849 struct scsi_low_softc *slp = &ct->sc_sclow;
850 struct ct_bus_access_handle *chp = &ct->sc_ch;
851 struct lun_info *li = slp->sl_Lnexus;
852 struct targ_info *ti = slp->sl_Tnexus;
853 struct ct_targ_info *cti = (void *) ti;
854 struct slccb *cb = slp->sl_Qnexus;
856 ct->sc_tmaxcnt = cb->ccb_tcmax * 1000 * 1000;
858 if ((ct->sc_satgo & CT_SAT_GOING) != 0)
860 ct_cr_write_1(chp, wd3s_oid, slp->sl_scp.scp_cmdlen);
861 ct_write_cmds(chp, slp->sl_scp.scp_cmd, slp->sl_scp.scp_cmdlen);
863 if (slp->sl_scp.scp_direction == SCSI_LOW_WRITE)
864 ct_cr_write_1(chp, wd3s_did, ti->ti_id);
866 ct_cr_write_1(chp, wd3s_did, ti->ti_id | DIDR_DPD);
867 ct_cr_write_1(chp, wd3s_lun, li->li_lun);
868 ct_cr_write_1(chp, wd3s_synch, cti->cti_syncreg);
876 struct scsi_low_softc *slp = &ct->sc_sclow;
877 struct ct_bus_access_handle *chp = &ct->sc_ch;
879 register u_int8_t regv;
881 for (wc = 0; wc < CT_DELAY_MAX / CT_DELAY_INTERVAL; wc ++)
883 regv = ct_stat_read_1(chp);
884 if ((regv & (STR_BSY | STR_CIP)) == 0)
886 if (regv == (u_int8_t) -1)
889 DELAY(CT_DELAY_INTERVAL);
892 device_printf(slp->sl_dev, "unbusy timeout\n");
900 struct ct_bus_access_handle *chp = &ct->sc_ch;
902 register u_int8_t regv;
904 for (wc = 0; wc < CT_DELAY_MAX / CT_DELAY_INTERVAL; wc ++)
906 regv = ct_stat_read_1(chp);
907 if ((regv & (STR_INT | STR_BSY | STR_CIP)) == STR_INT)
910 DELAY(CT_DELAY_INTERVAL);
919 struct ct_softc *ct = arg;
920 struct scsi_low_softc *slp = &ct->sc_sclow;
921 struct ct_bus_access_handle *chp = &ct->sc_ch;
922 struct targ_info *ti;
925 int len, satgo, error;
926 u_int8_t scsi_status, regv;
929 if (slp->sl_flags & HW_INACTIVE)
932 /**************************************************
933 * Get status & bus phase
934 **************************************************/
935 if ((ct_stat_read_1(chp) & STR_INT) == 0)
938 scsi_status = ct_cr_read_1(chp, wd3s_stat);
939 if (scsi_status == ((u_int8_t) -1))
942 /**************************************************
943 * Check reselection, or nexus
944 **************************************************/
945 if (scsi_status == BSR_RESEL || scsi_status == BSR_AFM_RESEL)
947 if (ct_reselected(ct, scsi_status) == EJUSTRETURN)
951 if ((ti = slp->sl_Tnexus) == NULL)
954 /**************************************************
956 **************************************************/
960 scsi_low_print(slp, NULL);
961 device_printf(slp->sl_dev, "scsi_status 0x%x\n\n",
962 (u_int) scsi_status);
965 kdb_enter(KDB_WHY_CAM, "ct");
968 #endif /* CT_DEBUG */
970 /**************************************************
971 * Internal scsi phase
972 **************************************************/
973 satgo = ct->sc_satgo;
974 ct->sc_satgo &= ~CT_SAT_GOING;
976 switch (ti->ti_phase)
979 if ((satgo & CT_SAT_GOING) == 0)
981 if (scsi_status != BSR_SELECTED)
983 ct_phase_error(ct, scsi_status);
986 scsi_low_arbit_win(slp);
987 SCSI_LOW_SETUP_PHASE(ti, PH_SELECTED);
992 scsi_low_arbit_win(slp);
993 SCSI_LOW_SETUP_PHASE(ti, PH_MSGOUT); /* XXX */
998 if ((scsi_status & BSR_PHVALID) == 0 ||
999 (scsi_status & BSR_PM) != BSR_MSGIN)
1001 scsi_low_restart(slp, SCSI_LOW_RESTART_HARD,
1002 "phase miss after reselect");
1008 if (slp->sl_flags & HW_PDMASTART)
1010 slp->sl_flags &= ~HW_PDMASTART;
1011 if (ct->sc_dma & CT_DMA_DMASTART)
1013 (*ct->ct_dma_xfer_stop) (ct);
1014 ct->sc_dma &= ~CT_DMA_DMASTART;
1016 else if (ct->sc_dma & CT_DMA_PIOSTART)
1018 (*ct->ct_pio_xfer_stop) (ct);
1019 ct->sc_dma &= ~CT_DMA_PIOSTART;
1023 scsi_low_data_finish(slp);
1029 /**************************************************
1031 **************************************************/
1032 if (scsi_status & BSR_PHVALID)
1034 /**************************************************
1035 * Normal SCSI phase.
1036 **************************************************/
1037 if ((scsi_status & BSR_CM) == BSR_CMDABT)
1039 ct_phase_error(ct, scsi_status);
1043 switch (scsi_status & BSR_PM)
1046 SCSI_LOW_SETUP_PHASE(ti, PH_DATA);
1047 if (scsi_low_data(slp, ti, &bp, SCSI_LOW_WRITE) != 0)
1051 goto common_data_phase;
1054 SCSI_LOW_SETUP_PHASE(ti, PH_DATA);
1055 if (scsi_low_data(slp, ti, &bp, SCSI_LOW_READ) != 0)
1061 if (slp->sl_scp.scp_datalen > 0)
1063 slp->sl_flags |= HW_PDMASTART;
1064 if ((ct->sc_xmode & CT_XMODE_PIO) != 0)
1066 error = (*ct->ct_pio_xfer_start) (ct);
1069 ct->sc_dma |= CT_DMA_PIOSTART;
1074 if ((ct->sc_xmode & CT_XMODE_DMA) != 0)
1076 error = (*ct->ct_dma_xfer_start) (ct);
1079 ct->sc_dma |= CT_DMA_DMASTART;
1086 if (slp->sl_scp.scp_direction == SCSI_LOW_READ)
1088 if (!(slp->sl_flags & HW_READ_PADDING))
1090 device_printf(slp->sl_dev,
1091 "read padding required\n");
1097 if (!(slp->sl_flags & HW_WRITE_PADDING))
1099 device_printf(slp->sl_dev,
1100 "write padding required\n");
1104 slp->sl_flags |= HW_PDMASTART;
1111 SCSI_LOW_SETUP_PHASE(ti, PH_CMD);
1112 if (scsi_low_cmd(slp, ti) != 0)
1117 if (ct_xfer(ct, slp->sl_scp.scp_cmd,
1118 slp->sl_scp.scp_cmdlen,
1119 SCSI_LOW_WRITE, &derror) != 0)
1121 device_printf(slp->sl_dev,
1122 "scsi cmd xfer short\n");
1127 SCSI_LOW_SETUP_PHASE(ti, PH_STAT);
1128 if ((ct_io_control & CT_USE_CCSEQ) != 0)
1130 if (scsi_low_is_msgout_continue(ti, 0) != 0 ||
1133 ct_xfer(ct, ®v, 1, SCSI_LOW_READ,
1135 scsi_low_statusin(slp, ti,
1140 ct->sc_satgo |= CT_SAT_GOING;
1141 cthw_set_count(chp, 0);
1142 cthw_phase_bypass(ct, 0x41);
1147 ct_xfer(ct, ®v, 1, SCSI_LOW_READ, &derror);
1148 scsi_low_statusin(slp, ti, regv | derror);
1154 device_printf(slp->sl_dev, "illegal bus phase (0x%x)\n",
1155 (u_int) scsi_status);
1156 scsi_low_print(slp, ti);
1160 SCSI_LOW_SETUP_PHASE(ti, PH_MSGOUT);
1161 flags = SCSI_LOW_MSGOUT_UNIFY;
1162 if (ti->ti_ophase != ti->ti_phase)
1163 flags |= SCSI_LOW_MSGOUT_INIT;
1164 len = scsi_low_msgout(slp, ti, flags);
1166 if (len > 1 && slp->sl_atten == 0)
1171 if (ct_xfer(ct, ti->ti_msgoutstr, len,
1172 SCSI_LOW_WRITE, &derror) != 0)
1174 device_printf(slp->sl_dev,
1175 "scsi msgout xfer short\n");
1177 SCSI_LOW_DEASSERT_ATN(slp);
1181 case BSR_MSGIN:/* msg in */
1182 SCSI_LOW_SETUP_PHASE(ti, PH_MSGIN);
1184 ct_xfer(ct, ®v, 1, SCSI_LOW_READ, &derror);
1185 if (scsi_low_msgin(slp, ti, regv | derror) == 0)
1187 if (scsi_low_is_msgout_continue(ti, 0) != 0)
1189 /* XXX: scsi_low_attetion */
1190 scsi_low_attention(slp);
1194 if ((ct_io_control & CT_FAST_INTR) != 0)
1196 if (ct_catch_intr(ct) == 0)
1204 /**************************************************
1205 * Special SCSI phase
1206 **************************************************/
1207 switch (scsi_status)
1209 case BSR_SATSDP: /* SAT with save data pointer */
1210 SCSI_LOW_SETUP_PHASE(ti, PH_MSGIN);
1211 ct->sc_satgo |= CT_SAT_GOING;
1212 scsi_low_msgin(slp, ti, MSG_SAVESP);
1213 cthw_phase_bypass(ct, 0x41);
1216 case BSR_SATFIN: /* SAT COMPLETE */
1218 * emulate statusin => msgin
1220 SCSI_LOW_SETUP_PHASE(ti, PH_STAT);
1221 scsi_low_statusin(slp, ti, ct_cr_read_1(chp, wd3s_lun));
1223 SCSI_LOW_SETUP_PHASE(ti, PH_MSGIN);
1224 scsi_low_msgin(slp, ti, MSG_COMP);
1226 scsi_low_disconnected(slp, ti);
1229 case BSR_ACKREQ: /* negate ACK */
1230 if (ct->sc_atten != 0)
1235 ct_cr_write_1(chp, wd3s_cmd, WD3S_NEGATE_ACK);
1236 if ((ct_io_control & CT_FAST_INTR) != 0)
1239 * Should clear a pending interrupt and
1240 * sync with a next interrupt!
1246 case BSR_DISC: /* disconnect */
1247 if (slp->sl_msgphase == MSGPH_NULL &&
1248 (satgo & CT_SAT_GOING) != 0)
1251 * emulate disconnect msg
1253 SCSI_LOW_SETUP_PHASE(ti, PH_MSGIN);
1254 scsi_low_msgin(slp, ti, MSG_DISCON);
1256 scsi_low_disconnected(slp, ti);
1264 ct_phase_error(ct, scsi_status);