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(struct ct_softc *ct, u_int8_t ph)
169 struct ct_bus_access_handle *chp = &ct->sc_ch;
171 ct_cr_write_1(chp, wd3s_cph, ph);
172 ct_cr_write_1(chp, wd3s_cmd, WD3S_SELECT_ATN_TFR);
176 cthw_bus_reset(struct ct_softc *ct)
180 * wd33c93 does not have bus reset function.
182 if (ct->ct_bus_reset != NULL)
183 ((*ct->ct_bus_reset) (ct));
187 cthw_chip_reset(struct ct_bus_access_handle *chp, int *chiprevp, int chipclk,
190 #define CT_SELTIMEOUT_20MHz_REGV (0x80)
195 /* issue abort cmd */
196 ct_cr_write_1(chp, wd3s_cmd, WD3S_ABORT);
197 DELAY(1000); /* 1ms wait */
198 (void) ct_stat_read_1(chp);
199 (void) ct_cr_read_1(chp, wd3s_stat);
201 /* setup chip registers */
203 seltout = CT_SELTIMEOUT_20MHz_REGV;
208 seltout = (seltout * chipclk) / 20;
214 seltout = (seltout * chipclk) / 20;
220 seltout = (seltout * chipclk) / 20;
225 panic("ct: illegal chip clk rate");
229 regv |= IDR_EHP | hostid | IDR_RAF | IDR_EAF;
230 ct_cr_write_1(chp, wd3s_oid, regv);
232 ct_cr_write_1(chp, wd3s_cmd, WD3S_RESET);
233 for (wc = CT_RESET_DEFAULT; wc > 0; wc --)
235 aux = ct_stat_read_1(chp);
236 if (aux != 0xff && (aux & STR_INT))
238 regv = ct_cr_read_1(chp, wd3s_stat);
239 if (regv == BSR_RESET || regv == BSR_AFM_RESET)
242 ct_cr_write_1(chp, wd3s_cmd, WD3S_RESET);
249 ct_cr_write_1(chp, wd3s_tout, seltout);
250 ct_cr_write_1(chp, wd3s_sid, SIDR_RESEL);
251 ct_cr_write_1(chp, wd3s_ctrl, CR_DEFAULT);
252 ct_cr_write_1(chp, wd3s_synch, 0);
253 if (chiprevp != NULL)
255 *chiprevp = CT_WD33C93;
256 if (regv == BSR_RESET)
259 *chiprevp = CT_WD33C93_A;
260 ct_cr_write_1(chp, wd3s_qtag, 0xaa);
261 if (ct_cr_read_1(chp, wd3s_qtag) != 0xaa)
263 ct_cr_write_1(chp, wd3s_qtag, 0x0);
266 ct_cr_write_1(chp, wd3s_qtag, 0x55);
267 if (ct_cr_read_1(chp, wd3s_qtag) != 0x55)
269 ct_cr_write_1(chp, wd3s_qtag, 0x0);
272 ct_cr_write_1(chp, wd3s_qtag, 0x0);
273 *chiprevp = CT_WD33C93_B;
277 (void) ct_stat_read_1(chp);
278 (void) ct_cr_read_1(chp, wd3s_stat);
282 static struct ct_synch_data *
283 ct_make_synch_table(struct ct_softc *ct)
285 struct ct_synch_data *sdtp, *sdp;
286 u_int base, i, period;
288 sdtp = sdp = &ct->sc_default_sdt[0];
290 if ((ct->sc_chipclk % 5) == 0)
291 base = 1000 / (5 * 2); /* 5 MHz type */
293 base = 1000 / (4 * 2); /* 4 MHz type */
295 if (ct->sc_chiprev >= CT_WD33C93_B)
298 for (i = 2; i < 8; i ++, sdp ++)
300 period = (base * i) / 2;
301 if (period >= 200) /* 5 MHz */
303 sdp->cs_period = period / 4;
304 sdp->cs_syncr = (i * 0x10) | 0x80;
308 for (i = 2; i < 8; i ++, sdp ++)
311 if (period > 500) /* 2 MHz */
313 sdp->cs_period = period / 4;
314 sdp->cs_syncr = (i * 0x10);
322 /**************************************************
324 **************************************************/
326 ctprobesubr(struct ct_bus_access_handle *chp, u_int dvcfg, int hsid,
327 u_int chipclk, int *chiprevp)
331 if ((ct_stat_read_1(chp) & STR_BSY) != 0)
334 if (cthw_chip_reset(chp, chiprevp, chipclk, hsid) != 0)
340 ctattachsubr(struct ct_softc *ct)
342 struct scsi_low_softc *slp = &ct->sc_sclow;
344 ct->sc_tmaxcnt = SCSI_LOW_MIN_TOUT * 1000 * 1000; /* default */
345 slp->sl_funcs = &ct_funcs;
346 slp->sl_flags |= HW_READ_PADDING;
347 (void) scsi_low_attach(slp, 0, CT_NTARGETS, CT_NLUNS,
348 sizeof(struct ct_targ_info), 0);
351 /**************************************************
352 * SCSI LOW interface functions
353 **************************************************/
355 cthw_attention(struct ct_softc *ct)
357 struct ct_bus_access_handle *chp = &ct->sc_ch;
360 if ((ct_stat_read_1(chp) & (STR_BSY | STR_CIP)) != 0)
363 ct_cr_write_1(chp, wd3s_cmd, WD3S_ASSERT_ATN);
365 if ((ct_stat_read_1(chp) & STR_LCI) == 0)
372 ct_attention(struct ct_softc *ct)
374 struct scsi_low_softc *slp = &ct->sc_sclow;
376 if (slp->sl_atten == 0)
379 scsi_low_attention(slp);
381 else if (ct->sc_atten != 0)
389 ct_targ_init(struct ct_softc *ct, struct targ_info *ti, int action)
391 struct ct_targ_info *cti = (void *) ti;
393 if (action == SCSI_LOW_INFO_ALLOC || action == SCSI_LOW_INFO_REVOKE)
395 if (ct->sc_sdp == NULL)
397 ct->sc_sdp = ct_make_synch_table(ct);
400 switch (ct->sc_chiprev)
403 ti->ti_maxsynch.offset = 5;
408 ti->ti_maxsynch.offset = 12;
413 ti->ti_maxsynch.offset = 12;
417 ti->ti_maxsynch.period = ct->sc_sdp[0].cs_period;
418 ti->ti_width = SCSI_LOW_BUS_WIDTH_8;
419 cti->cti_syncreg = 0;
426 ct_world_start(struct ct_softc *ct, int fdone)
428 struct scsi_low_softc *slp = &ct->sc_sclow;
429 struct ct_bus_access_handle *chp = &ct->sc_ch;
431 if (ct->sc_sdp == NULL)
433 ct->sc_sdp = ct_make_synch_table(ct);
436 if (slp->sl_cfgflags & CFG_NOPARITY)
437 ct->sc_creg = CR_DEFAULT;
439 ct->sc_creg = CR_DEFAULT_HP;
441 if (ct->sc_dma & CT_DMA_DMASTART)
442 (*ct->ct_dma_xfer_stop) (ct);
443 if (ct->sc_dma & CT_DMA_PIOSTART)
444 (*ct->ct_pio_xfer_stop) (ct);
448 cthw_chip_reset(chp, NULL, ct->sc_chipclk, slp->sl_hostid);
449 scsi_low_bus_reset(slp);
450 cthw_chip_reset(chp, NULL, ct->sc_chipclk, slp->sl_hostid);
456 ct_start_selection(struct ct_softc *ct, struct slccb *cb)
458 struct scsi_low_softc *slp = &ct->sc_sclow;
459 struct ct_bus_access_handle *chp = &ct->sc_ch;
461 struct targ_info *ti = slp->sl_Tnexus;
462 struct lun_info *li = slp->sl_Lnexus;
466 ct->sc_tmaxcnt = cb->ccb_tcmax * 1000 * 1000;
470 if (scsi_low_is_disconnect_ok(cb) != 0)
472 if (ct->sc_chiprev >= CT_WD33C93_A)
474 else if (cthw_cmdlevel[slp->sl_scp.scp_cmd[0]] != 0)
479 scsi_low_is_msgout_continue(ti, SCSI_LOW_MSG_IDENTIFY) == 0)
481 cmd = WD3S_SELECT_ATN_TFR;
482 ct->sc_satgo = CT_SAT_GOING;
486 cmd = WD3S_SELECT_ATN;
490 if ((ct_stat_read_1(chp) & (STR_BSY | STR_INT | STR_CIP)) != 0)
491 return SCSI_LOW_START_FAIL;
493 if ((ct->sc_satgo & CT_SAT_GOING) != 0)
495 (void) scsi_low_msgout(slp, ti, SCSI_LOW_MSGOUT_INIT);
496 scsi_low_cmd(slp, ti);
497 ct_cr_write_1(chp, wd3s_oid, slp->sl_scp.scp_cmdlen);
498 ct_write_cmds(chp, slp->sl_scp.scp_cmd, slp->sl_scp.scp_cmdlen);
502 /* anyway attention assert */
503 SCSI_LOW_ASSERT_ATN(slp);
506 ct_target_nexus_establish(ct, li->li_lun, slp->sl_scp.scp_direction);
509 if ((ct_stat_read_1(chp) & (STR_BSY | STR_INT | STR_CIP)) == 0)
512 * Reload a lun again here.
514 ct_cr_write_1(chp, wd3s_lun, li->li_lun);
515 ct_cr_write_1(chp, wd3s_cmd, cmd);
516 if ((ct_stat_read_1(chp) & STR_LCI) == 0)
519 SCSI_LOW_SETUP_PHASE(ti, PH_SELSTART);
520 return SCSI_LOW_START_OK;
524 return SCSI_LOW_START_FAIL;
528 ct_msg(struct ct_softc *ct, struct targ_info *ti, u_int msg)
530 struct ct_bus_access_handle *chp = &ct->sc_ch;
531 struct ct_targ_info *cti = (void *) ti;
532 struct ct_synch_data *csp = ct->sc_sdp;
533 u_int offset, period;
536 if ((msg & SCSI_LOW_MSG_WIDE) != 0)
538 if (ti->ti_width != SCSI_LOW_BUS_WIDTH_8)
540 ti->ti_width = SCSI_LOW_BUS_WIDTH_8;
546 if ((msg & SCSI_LOW_MSG_SYNCH) == 0)
549 offset = ti->ti_maxsynch.offset;
550 period = ti->ti_maxsynch.period;
551 for ( ; csp->cs_period != 0; csp ++)
553 if (period == csp->cs_period)
557 if (ti->ti_maxsynch.period != 0 && csp->cs_period == 0)
559 ti->ti_maxsynch.period = 0;
560 ti->ti_maxsynch.offset = 0;
561 cti->cti_syncreg = 0;
566 cti->cti_syncreg = ((offset & 0x0f) | csp->cs_syncr);
570 if (ct->ct_synch_setup != 0)
571 (*ct->ct_synch_setup) (ct, ti);
572 ct_cr_write_1(chp, wd3s_synch, cti->cti_syncreg);
576 /*************************************************
578 *************************************************/
580 ct_xfer(struct ct_softc *ct, u_int8_t *data, int len, int direction,
583 struct ct_bus_access_handle *chp = &ct->sc_ch;
585 register u_int8_t aux;
590 ct_cr_write_1(chp, wd3s_cmd, WD3S_SBT | WD3S_TFR_INFO);
594 cthw_set_count(chp, len);
595 ct_cr_write_1(chp, wd3s_cmd, WD3S_TFR_INFO);
598 aux = ct_stat_read_1(chp);
599 if ((aux & STR_LCI) != 0)
601 cthw_set_count(chp, 0);
605 for (wc = 0; wc < ct->sc_tmaxcnt; wc ++)
607 /* check data ready */
608 if ((aux & (STR_BSY | STR_DBR)) == (STR_BSY | STR_DBR))
610 if (direction == SCSI_LOW_READ)
612 *data = ct_cr_read_1(chp, wd3s_data);
613 if ((aux & STR_PE) != 0)
614 *statp |= SCSI_LOW_DATA_PE;
618 ct_cr_write_1(chp, wd3s_data, *data);
630 /* check phase miss */
631 aux = ct_stat_read_1(chp);
632 if ((aux & STR_INT) != 0)
638 #define CT_PADDING_BUF_SIZE 32
641 ct_io_xfer(struct ct_softc *ct)
643 struct scsi_low_softc *slp = &ct->sc_sclow;
644 struct ct_bus_access_handle *chp = &ct->sc_ch;
645 struct sc_p *sp = &slp->sl_scp;
648 u_int8_t pbuf[CT_PADDING_BUF_SIZE];
651 ct_cr_write_1(chp, wd3s_ctrl, ct->sc_creg);
653 if (sp->scp_datalen <= 0)
655 slp->sl_error |= PDMAERR;
657 if (slp->sl_scp.scp_direction == SCSI_LOW_WRITE)
658 bzero(pbuf, CT_PADDING_BUF_SIZE);
659 ct_xfer(ct, pbuf, CT_PADDING_BUF_SIZE,
660 sp->scp_direction, &stat);
664 len = ct_xfer(ct, sp->scp_data, sp->scp_datalen,
665 sp->scp_direction, &stat);
666 sp->scp_data += (sp->scp_datalen - len);
667 sp->scp_datalen = len;
671 /**************************************************
673 **************************************************/
681 struct ct_err ct_cmderr[] = {
682 /*0*/ { "illegal cmd", FATALIO, SCSI_LOW_MSG_ABORT, 1},
683 /*1*/ { "unexpected bus free", FATALIO, 0, 1},
684 /*2*/ { NULL, SELTIMEOUTIO, 0, 1},
685 /*3*/ { "scsi bus parity error", PARITYERR, SCSI_LOW_MSG_ERROR, 0},
686 /*4*/ { "scsi bus parity error", PARITYERR, SCSI_LOW_MSG_ERROR, 0},
687 /*5*/ { "unknown" , FATALIO, SCSI_LOW_MSG_ABORT, 1},
688 /*6*/ { "miss reselection (target mode)", FATALIO, SCSI_LOW_MSG_ABORT, 0},
689 /*7*/ { "wrong status byte", PARITYERR, SCSI_LOW_MSG_ERROR, 0},
693 ct_phase_error(struct ct_softc *ct, u_int8_t scsi_status)
695 struct scsi_low_softc *slp = &ct->sc_sclow;
696 struct targ_info *ti = slp->sl_Tnexus;
700 if ((scsi_status & BSR_CM) == BSR_CMDERR &&
701 (scsi_status & BSR_PHVALID) == 0)
703 pep = &ct_cmderr[scsi_status & BSR_PM];
704 slp->sl_error |= pep->pe_err;
705 if ((pep->pe_err & PARITYERR) != 0)
707 if (ti->ti_phase == PH_MSGIN)
708 msg = SCSI_LOW_MSG_PARITY;
710 msg = SCSI_LOW_MSG_ERROR;
713 msg = pep->pe_errmsg;
716 scsi_low_assert_msg(slp, slp->sl_Tnexus, msg, 1);
718 if (pep->pe_msg != NULL)
720 device_printf(slp->sl_dev, "phase error: %s",
722 scsi_low_print(slp, slp->sl_Tnexus);
725 if (pep->pe_done != 0)
726 scsi_low_disconnected(slp, ti);
730 slp->sl_error |= FATALIO;
731 scsi_low_restart(slp, SCSI_LOW_RESTART_HARD, "phase error");
735 /**************************************************
736 * ### SCSI PHASE SEQUENCER ###
737 **************************************************/
739 ct_reselected(struct ct_softc *ct, u_int8_t scsi_status)
741 struct scsi_low_softc *slp = &ct->sc_sclow;
742 struct ct_bus_access_handle *chp = &ct->sc_ch;
743 struct targ_info *ti;
748 ct->sc_satgo &= ~CT_SAT_GOING;
749 regv = ct_cr_read_1(chp, wd3s_sid);
750 if ((regv & SIDR_VALID) == 0)
753 sid = regv & SIDR_IDM;
754 if ((ti = scsi_low_reselected(slp, sid)) == NULL)
757 ct_target_nexus_establish(ct, 0, SCSI_LOW_READ);
758 if (scsi_status != BSR_AFM_RESEL)
761 SCSI_LOW_SETUP_PHASE(ti, PH_MSGIN);
762 regv = ct_cr_read_1(chp, wd3s_data);
763 if (scsi_low_msgin(slp, ti, (u_int) regv) == 0)
765 if (scsi_low_is_msgout_continue(ti, 0) != 0)
767 /* XXX: scsi_low_attetion */
768 scsi_low_attention(slp);
772 if (ct->sc_atten != 0)
777 ct_cr_write_1(chp, wd3s_cmd, WD3S_NEGATE_ACK);
782 ct_target_nexus_establish(struct ct_softc *ct, int lun, int dir)
784 struct scsi_low_softc *slp = &ct->sc_sclow;
785 struct ct_bus_access_handle *chp = &ct->sc_ch;
786 struct targ_info *ti = slp->sl_Tnexus;
787 struct ct_targ_info *cti = (void *) ti;
789 if (dir == SCSI_LOW_WRITE)
790 ct_cr_write_1(chp, wd3s_did, ti->ti_id);
792 ct_cr_write_1(chp, wd3s_did, ti->ti_id | DIDR_DPD);
793 ct_cr_write_1(chp, wd3s_lun, lun);
794 ct_cr_write_1(chp, wd3s_ctrl, ct->sc_creg | CR_DMA);
795 ct_cr_write_1(chp, wd3s_cph, 0);
796 ct_cr_write_1(chp, wd3s_synch, cti->cti_syncreg);
797 cthw_set_count(chp, 0);
802 ct_lun_nexus_establish(struct ct_softc *ct)
804 struct scsi_low_softc *slp = &ct->sc_sclow;
805 struct ct_bus_access_handle *chp = &ct->sc_ch;
806 struct lun_info *li = slp->sl_Lnexus;
808 ct_cr_write_1(chp, wd3s_lun, li->li_lun);
813 ct_ccb_nexus_establish(struct ct_softc *ct)
815 struct scsi_low_softc *slp = &ct->sc_sclow;
816 struct ct_bus_access_handle *chp = &ct->sc_ch;
817 struct lun_info *li = slp->sl_Lnexus;
818 struct targ_info *ti = slp->sl_Tnexus;
819 struct ct_targ_info *cti = (void *) ti;
820 struct slccb *cb = slp->sl_Qnexus;
822 ct->sc_tmaxcnt = cb->ccb_tcmax * 1000 * 1000;
824 if ((ct->sc_satgo & CT_SAT_GOING) != 0)
826 ct_cr_write_1(chp, wd3s_oid, slp->sl_scp.scp_cmdlen);
827 ct_write_cmds(chp, slp->sl_scp.scp_cmd, slp->sl_scp.scp_cmdlen);
829 if (slp->sl_scp.scp_direction == SCSI_LOW_WRITE)
830 ct_cr_write_1(chp, wd3s_did, ti->ti_id);
832 ct_cr_write_1(chp, wd3s_did, ti->ti_id | DIDR_DPD);
833 ct_cr_write_1(chp, wd3s_lun, li->li_lun);
834 ct_cr_write_1(chp, wd3s_synch, cti->cti_syncreg);
839 ct_unbusy(struct ct_softc *ct)
841 struct scsi_low_softc *slp = &ct->sc_sclow;
842 struct ct_bus_access_handle *chp = &ct->sc_ch;
844 register u_int8_t regv;
846 for (wc = 0; wc < CT_DELAY_MAX / CT_DELAY_INTERVAL; wc ++)
848 regv = ct_stat_read_1(chp);
849 if ((regv & (STR_BSY | STR_CIP)) == 0)
851 if (regv == (u_int8_t) -1)
854 DELAY(CT_DELAY_INTERVAL);
857 device_printf(slp->sl_dev, "unbusy timeout\n");
862 ct_catch_intr(struct ct_softc *ct)
864 struct ct_bus_access_handle *chp = &ct->sc_ch;
866 register u_int8_t regv;
868 for (wc = 0; wc < CT_DELAY_MAX / CT_DELAY_INTERVAL; wc ++)
870 regv = ct_stat_read_1(chp);
871 if ((regv & (STR_INT | STR_BSY | STR_CIP)) == STR_INT)
874 DELAY(CT_DELAY_INTERVAL);
882 struct ct_softc *ct = arg;
883 struct scsi_low_softc *slp = &ct->sc_sclow;
884 struct ct_bus_access_handle *chp = &ct->sc_ch;
885 struct targ_info *ti;
888 int len, satgo, error;
889 u_int8_t scsi_status, regv;
892 if (slp->sl_flags & HW_INACTIVE)
895 /**************************************************
896 * Get status & bus phase
897 **************************************************/
898 if ((ct_stat_read_1(chp) & STR_INT) == 0)
901 scsi_status = ct_cr_read_1(chp, wd3s_stat);
902 if (scsi_status == ((u_int8_t) -1))
905 /**************************************************
906 * Check reselection, or nexus
907 **************************************************/
908 if (scsi_status == BSR_RESEL || scsi_status == BSR_AFM_RESEL)
910 if (ct_reselected(ct, scsi_status) == EJUSTRETURN)
914 if ((ti = slp->sl_Tnexus) == NULL)
917 /**************************************************
919 **************************************************/
923 scsi_low_print(slp, NULL);
924 device_printf(slp->sl_dev, "scsi_status 0x%x\n\n",
925 (u_int) scsi_status);
928 kdb_enter(KDB_WHY_CAM, "ct");
931 #endif /* CT_DEBUG */
933 /**************************************************
934 * Internal scsi phase
935 **************************************************/
936 satgo = ct->sc_satgo;
937 ct->sc_satgo &= ~CT_SAT_GOING;
939 switch (ti->ti_phase)
942 if ((satgo & CT_SAT_GOING) == 0)
944 if (scsi_status != BSR_SELECTED)
946 ct_phase_error(ct, scsi_status);
949 scsi_low_arbit_win(slp);
950 SCSI_LOW_SETUP_PHASE(ti, PH_SELECTED);
955 scsi_low_arbit_win(slp);
956 SCSI_LOW_SETUP_PHASE(ti, PH_MSGOUT); /* XXX */
961 if ((scsi_status & BSR_PHVALID) == 0 ||
962 (scsi_status & BSR_PM) != BSR_MSGIN)
964 scsi_low_restart(slp, SCSI_LOW_RESTART_HARD,
965 "phase miss after reselect");
971 if (slp->sl_flags & HW_PDMASTART)
973 slp->sl_flags &= ~HW_PDMASTART;
974 if (ct->sc_dma & CT_DMA_DMASTART)
976 (*ct->ct_dma_xfer_stop) (ct);
977 ct->sc_dma &= ~CT_DMA_DMASTART;
979 else if (ct->sc_dma & CT_DMA_PIOSTART)
981 (*ct->ct_pio_xfer_stop) (ct);
982 ct->sc_dma &= ~CT_DMA_PIOSTART;
986 scsi_low_data_finish(slp);
992 /**************************************************
994 **************************************************/
995 if (scsi_status & BSR_PHVALID)
997 /**************************************************
999 **************************************************/
1000 if ((scsi_status & BSR_CM) == BSR_CMDABT)
1002 ct_phase_error(ct, scsi_status);
1006 switch (scsi_status & BSR_PM)
1009 SCSI_LOW_SETUP_PHASE(ti, PH_DATA);
1010 if (scsi_low_data(slp, ti, &bp, SCSI_LOW_WRITE) != 0)
1014 goto common_data_phase;
1017 SCSI_LOW_SETUP_PHASE(ti, PH_DATA);
1018 if (scsi_low_data(slp, ti, &bp, SCSI_LOW_READ) != 0)
1024 if (slp->sl_scp.scp_datalen > 0)
1026 slp->sl_flags |= HW_PDMASTART;
1027 if ((ct->sc_xmode & CT_XMODE_PIO) != 0)
1029 error = (*ct->ct_pio_xfer_start) (ct);
1032 ct->sc_dma |= CT_DMA_PIOSTART;
1037 if ((ct->sc_xmode & CT_XMODE_DMA) != 0)
1039 error = (*ct->ct_dma_xfer_start) (ct);
1042 ct->sc_dma |= CT_DMA_DMASTART;
1049 if (slp->sl_scp.scp_direction == SCSI_LOW_READ)
1051 if (!(slp->sl_flags & HW_READ_PADDING))
1053 device_printf(slp->sl_dev,
1054 "read padding required\n");
1060 if (!(slp->sl_flags & HW_WRITE_PADDING))
1062 device_printf(slp->sl_dev,
1063 "write padding required\n");
1067 slp->sl_flags |= HW_PDMASTART;
1074 SCSI_LOW_SETUP_PHASE(ti, PH_CMD);
1075 if (scsi_low_cmd(slp, ti) != 0)
1080 if (ct_xfer(ct, slp->sl_scp.scp_cmd,
1081 slp->sl_scp.scp_cmdlen,
1082 SCSI_LOW_WRITE, &derror) != 0)
1084 device_printf(slp->sl_dev,
1085 "scsi cmd xfer short\n");
1090 SCSI_LOW_SETUP_PHASE(ti, PH_STAT);
1091 if ((ct_io_control & CT_USE_CCSEQ) != 0)
1093 if (scsi_low_is_msgout_continue(ti, 0) != 0 ||
1096 ct_xfer(ct, ®v, 1, SCSI_LOW_READ,
1098 scsi_low_statusin(slp, ti,
1103 ct->sc_satgo |= CT_SAT_GOING;
1104 cthw_set_count(chp, 0);
1105 cthw_phase_bypass(ct, 0x41);
1110 ct_xfer(ct, ®v, 1, SCSI_LOW_READ, &derror);
1111 scsi_low_statusin(slp, ti, regv | derror);
1117 device_printf(slp->sl_dev, "illegal bus phase (0x%x)\n",
1118 (u_int) scsi_status);
1119 scsi_low_print(slp, ti);
1123 SCSI_LOW_SETUP_PHASE(ti, PH_MSGOUT);
1124 flags = SCSI_LOW_MSGOUT_UNIFY;
1125 if (ti->ti_ophase != ti->ti_phase)
1126 flags |= SCSI_LOW_MSGOUT_INIT;
1127 len = scsi_low_msgout(slp, ti, flags);
1129 if (len > 1 && slp->sl_atten == 0)
1134 if (ct_xfer(ct, ti->ti_msgoutstr, len,
1135 SCSI_LOW_WRITE, &derror) != 0)
1137 device_printf(slp->sl_dev,
1138 "scsi msgout xfer short\n");
1140 SCSI_LOW_DEASSERT_ATN(slp);
1144 case BSR_MSGIN:/* msg in */
1145 SCSI_LOW_SETUP_PHASE(ti, PH_MSGIN);
1147 ct_xfer(ct, ®v, 1, SCSI_LOW_READ, &derror);
1148 if (scsi_low_msgin(slp, ti, regv | derror) == 0)
1150 if (scsi_low_is_msgout_continue(ti, 0) != 0)
1152 /* XXX: scsi_low_attetion */
1153 scsi_low_attention(slp);
1157 if ((ct_io_control & CT_FAST_INTR) != 0)
1159 if (ct_catch_intr(ct) == 0)
1167 /**************************************************
1168 * Special SCSI phase
1169 **************************************************/
1170 switch (scsi_status)
1172 case BSR_SATSDP: /* SAT with save data pointer */
1173 SCSI_LOW_SETUP_PHASE(ti, PH_MSGIN);
1174 ct->sc_satgo |= CT_SAT_GOING;
1175 scsi_low_msgin(slp, ti, MSG_SAVESP);
1176 cthw_phase_bypass(ct, 0x41);
1179 case BSR_SATFIN: /* SAT COMPLETE */
1181 * emulate statusin => msgin
1183 SCSI_LOW_SETUP_PHASE(ti, PH_STAT);
1184 scsi_low_statusin(slp, ti, ct_cr_read_1(chp, wd3s_lun));
1186 SCSI_LOW_SETUP_PHASE(ti, PH_MSGIN);
1187 scsi_low_msgin(slp, ti, MSG_COMP);
1189 scsi_low_disconnected(slp, ti);
1192 case BSR_ACKREQ: /* negate ACK */
1193 if (ct->sc_atten != 0)
1198 ct_cr_write_1(chp, wd3s_cmd, WD3S_NEGATE_ACK);
1199 if ((ct_io_control & CT_FAST_INTR) != 0)
1202 * Should clear a pending interrupt and
1203 * sync with a next interrupt!
1209 case BSR_DISC: /* disconnect */
1210 if (slp->sl_msgphase == MSGPH_NULL &&
1211 (satgo & CT_SAT_GOING) != 0)
1214 * emulate disconnect msg
1216 SCSI_LOW_SETUP_PHASE(ti, PH_MSGIN);
1217 scsi_low_msgin(slp, ti, MSG_DISCON);
1219 scsi_low_disconnected(slp, ti);
1227 ct_phase_error(ct, scsi_status);