1 /* $FreeBSD: src/sys/dev/stg/tmc18c30.c,v 1.1.2.5 2001/12/17 13:30:19 non Exp $ */
2 /* $NecBSD: tmc18c30.c,v 1.28.12.3 2001/06/19 04:35:48 honda Exp $ */
7 #define STG_IO_CONTROL_FLAGS (STG_FIFO_INTERRUPTS | STG_WAIT_FOR_SELECT)
10 * [NetBSD for NEC PC-98 series]
11 * Copyright (c) 1996, 1997, 1998, 1999, 2000, 2001
12 * NetBSD/pc98 porting staff. All rights reserved.
13 * Copyright (c) 1996, 1997, 1998, 1999, 2000, 2001
14 * Naofumi HONDA. All rights reserved.
15 * Copyright (c) 1996, 1997, 1998, 1999
16 * Kouichi Matsuda. All rights reserved.
18 * Redistribution and use in source and binary forms, with or without
19 * modification, are permitted provided that the following conditions
21 * 1. Redistributions of source code must retain the above copyright
22 * notice, this list of conditions and the following disclaimer.
23 * 2. Redistributions in binary form must reproduce the above copyright
24 * notice, this list of conditions and the following disclaimer in the
25 * documentation and/or other materials provided with the distribution.
26 * 3. The name of the author may not be used to endorse or promote products
27 * derived from this software without specific prior written permission.
29 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
30 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
31 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
32 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
33 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
34 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
35 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
37 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
38 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
39 * POSSIBILITY OF SUCH DAMAGE.
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/kernel.h>
47 #include <sys/queue.h>
48 #include <sys/malloc.h>
49 #include <sys/errno.h>
51 #include <sys/thread2.h>
53 #include <machine/clock.h>
54 #include <machine/cpu.h>
56 #include <bus/cam/scsi/scsi_low.h>
57 #include "tmc18c30reg.h"
58 #include "tmc18c30var.h"
60 /***************************************************
62 ***************************************************/
63 /* DEVICE CONFIGURATION FLAGS (MINOR)
66 * 0x02 PARITY LINE OFF
67 * 0x04 IDENTIFY MSG OFF ( = single lun)
68 * 0x08 SYNC TRANSFER OFF
70 /* #define STG_SYNC_SUPPORT */ /* NOT YET but easy */
72 /* For the 512 fifo type: change below */
73 #define TMC18C30_FIFOSZ 0x800
74 #define TMC18C30_FCBSZ 0x200
75 #define TMC18C50_FIFOSZ 0x2000
76 #define TMC18C50_FCBSZ 0x400
78 #define STG_MAX_DATA_SIZE (64 * 1024)
79 #define STG_DELAY_MAX (2 * 1000 * 1000)
80 #define STG_DELAY_INTERVAL (1)
81 #define STG_DELAY_SELECT_POLLING_MAX (5 * 1000 * 1000)
83 /***************************************************
85 ***************************************************/
86 #define STG_NTARGETS 8
89 /***************************************************
91 ***************************************************/
94 #endif /* STG_DEBUG */
103 #endif /* STG_STATICS */
105 /***************************************************
107 ***************************************************/
108 #define STG_FIFO_INTERRUPTS 0x0001
109 #define STG_WAIT_FOR_SELECT 0x0100
111 int stg_io_control
= STG_IO_CONTROL_FLAGS
;
113 /**************************************************************
115 **************************************************************/
117 static void stg_pio_read (struct stg_softc
*, struct targ_info
*, u_int
);
118 static void stg_pio_write (struct stg_softc
*, struct targ_info
*, u_int
);
119 static int stg_xfer (struct stg_softc
*, u_int8_t
*, int, int, int);
120 static int stg_msg (struct stg_softc
*, struct targ_info
*, u_int
);
121 static int stg_reselected (struct stg_softc
*);
122 static int stg_disconnected (struct stg_softc
*, struct targ_info
*);
123 static __inline
void stg_pdma_end (struct stg_softc
*, struct targ_info
*);
124 static int stghw_select_targ_wait (struct stg_softc
*, int);
125 static int stghw_check (struct stg_softc
*);
126 static void stghw_init (struct stg_softc
*);
127 static int stg_negate_signal (struct stg_softc
*, u_int8_t
, u_char
*);
128 static int stg_expect_signal (struct stg_softc
*, u_int8_t
, u_int8_t
);
129 static int stg_world_start (struct stg_softc
*, int);
130 static int stghw_start_selection (struct stg_softc
*sc
, struct slccb
*);
131 static void stghw_bus_reset (struct stg_softc
*);
132 static void stghw_attention (struct stg_softc
*);
133 static int stg_target_nexus_establish (struct stg_softc
*);
134 static int stg_lun_nexus_establish (struct stg_softc
*);
135 static int stg_ccb_nexus_establish (struct stg_softc
*);
136 static int stg_targ_init (struct stg_softc
*, struct targ_info
*, int);
137 static __inline
void stghw_bcr_write_1 (struct stg_softc
*, u_int8_t
);
138 static int stg_timeout (struct stg_softc
*);
139 static void stg_selection_done_and_expect_msgout (struct stg_softc
*);
141 struct scsi_low_funcs stgfuncs
= {
142 SC_LOW_INIT_T stg_world_start
,
143 SC_LOW_BUSRST_T stghw_bus_reset
,
144 SC_LOW_TARG_INIT_T stg_targ_init
,
145 SC_LOW_LUN_INIT_T NULL
,
147 SC_LOW_SELECT_T stghw_start_selection
,
148 SC_LOW_NEXUS_T stg_lun_nexus_establish
,
149 SC_LOW_NEXUS_T stg_ccb_nexus_establish
,
151 SC_LOW_ATTEN_T stghw_attention
,
152 SC_LOW_MSG_T stg_msg
,
154 SC_LOW_TIMEOUT_T stg_timeout
,
155 SC_LOW_POLL_T stgintr
,
160 /****************************************************
162 ****************************************************/
164 stghw_bcr_write_1(struct stg_softc
*sc
, u_int8_t bcv
)
167 bus_space_write_1(sc
->sc_iot
, sc
->sc_ioh
, tmc_bctl
, bcv
);
172 stghw_check(struct stg_softc
*sc
)
174 struct scsi_low_softc
*slp
= &sc
->sc_sclow
;
175 bus_space_tag_t iot
= sc
->sc_iot
;
176 bus_space_handle_t ioh
= sc
->sc_ioh
;
180 lsb
= bus_space_read_1(iot
, ioh
, tmc_idlsb
);
181 msb
= bus_space_read_1(iot
, ioh
, tmc_idmsb
);
182 switch (msb
<< 8 | lsb
)
185 /* TMCCHIP_1800 not supported. (it's my policy) */
186 sc
->sc_chip
= TMCCHIP_1800
;
190 if (bus_space_read_1(iot
, ioh
, tmc_cfg2
) & 0x02)
192 sc
->sc_chip
= TMCCHIP_18C30
;
193 sc
->sc_fsz
= TMC18C30_FIFOSZ
;
194 fcbsize
= TMC18C30_FCBSZ
;
198 sc
->sc_chip
= TMCCHIP_18C50
;
199 sc
->sc_fsz
= TMC18C50_FIFOSZ
;
200 fcbsize
= TMC18C50_FCBSZ
;
205 sc
->sc_chip
= TMCCHIP_UNK
;
209 sc
->sc_fcRinit
= FCTL_INTEN
;
210 sc
->sc_fcWinit
= FCTL_PARENB
| FCTL_INTEN
;
212 if (slp
->sl_cfgflags
& CFG_NOATTEN
)
215 sc
->sc_imsg
= BCTL_ATN
;
216 sc
->sc_busc
= BCTL_BUSEN
;
218 sc
->sc_wthold
= fcbsize
+ 256;
219 sc
->sc_rthold
= fcbsize
- 256;
220 sc
->sc_maxwsize
= sc
->sc_fsz
;
222 fcb
= fcbsize
/ (sc
->sc_fsz
/ 16);
223 sc
->sc_icinit
= ICTL_CD
| ICTL_SEL
| ICTL_ARBIT
| fcb
;
228 stghw_init(struct stg_softc
*sc
)
230 bus_space_tag_t iot
= sc
->sc_iot
;
231 bus_space_handle_t ioh
= sc
->sc_ioh
;
233 bus_space_write_1(iot
, ioh
, tmc_ictl
, 0);
234 stghw_bcr_write_1(sc
, BCTL_BUSFREE
);
235 bus_space_write_1(iot
, ioh
, tmc_fctl
,
236 sc
->sc_fcRinit
| FCTL_CLRFIFO
| FCTL_CLRINT
);
237 bus_space_write_1(iot
, ioh
, tmc_fctl
, sc
->sc_fcRinit
);
238 bus_space_write_1(iot
, ioh
, tmc_ictl
, sc
->sc_icinit
);
240 bus_space_write_1(iot
, ioh
, tmc_ssctl
, 0);
244 stg_targ_init(struct stg_softc
*sc
, struct targ_info
*ti
, int action
)
246 struct stg_targ_info
*sti
= (void *) ti
;
248 if (action
== SCSI_LOW_INFO_ALLOC
|| action
== SCSI_LOW_INFO_REVOKE
)
250 ti
->ti_width
= SCSI_LOW_BUS_WIDTH_8
;
251 ti
->ti_maxsynch
.period
= 0;
252 ti
->ti_maxsynch
.offset
= 0;
253 sti
->sti_reg_synch
= 0;
258 /****************************************************
260 ****************************************************/
262 stghw_attention(struct stg_softc
*sc
)
265 sc
->sc_busc
|= BCTL_ATN
;
266 sc
->sc_busimg
|= BCTL_ATN
;
267 bus_space_write_1(sc
->sc_iot
, sc
->sc_ioh
, tmc_bctl
, sc
->sc_busimg
);
272 stghw_bus_reset(struct stg_softc
*sc
)
274 bus_space_tag_t iot
= sc
->sc_iot
;
275 bus_space_handle_t ioh
= sc
->sc_ioh
;
277 bus_space_write_1(iot
, ioh
, tmc_ictl
, 0);
278 bus_space_write_1(iot
, ioh
, tmc_fctl
, 0);
279 stghw_bcr_write_1(sc
, BCTL_RST
);
280 SCSI_LOW_DELAY(100000);
281 stghw_bcr_write_1(sc
, BCTL_BUSFREE
);
285 stghw_start_selection(struct stg_softc
*sc
, struct slccb
*cb
)
287 bus_space_tag_t iot
= sc
->sc_iot
;
288 bus_space_handle_t ioh
= sc
->sc_ioh
;
289 struct targ_info
*ti
= cb
->ti
;
292 sc
->sc_tmaxcnt
= cb
->ccb_tcmax
* 1000 * 1000;
293 sc
->sc_dataout_timeout
= 0;
294 sc
->sc_ubf_timeout
= 0;
295 stghw_bcr_write_1(sc
, BCTL_BUSFREE
);
296 bus_space_write_1(iot
, ioh
, tmc_ictl
, sc
->sc_icinit
);
299 stat
= bus_space_read_1(iot
, ioh
, tmc_astat
);
300 if ((stat
& ASTAT_INT
) != 0)
303 return SCSI_LOW_START_FAIL
;
306 bus_space_write_1(iot
, ioh
, tmc_scsiid
, sc
->sc_idbit
);
307 bus_space_write_1(iot
, ioh
, tmc_fctl
, sc
->sc_fcRinit
| FCTL_ARBIT
);
310 SCSI_LOW_SETUP_PHASE(ti
, PH_ARBSTART
);
311 return SCSI_LOW_START_OK
;
315 stg_world_start(struct stg_softc
*sc
, int fdone
)
317 struct scsi_low_softc
*slp
= &sc
->sc_sclow
;
320 if ((slp
->sl_cfgflags
& CFG_NOPARITY
) == 0)
321 sc
->sc_fcRinit
|= FCTL_PARENB
;
323 sc
->sc_fcRinit
&= ~FCTL_PARENB
;
325 if ((error
= stghw_check(sc
)) != 0)
329 scsi_low_bus_reset(slp
);
332 SOFT_INTR_REQUIRED(slp
);
337 stg_msg(struct stg_softc
*sc
, struct targ_info
*ti
, u_int msg
)
339 bus_space_tag_t iot
= sc
->sc_iot
;
340 bus_space_handle_t ioh
= sc
->sc_ioh
;
341 struct stg_targ_info
*sti
= (void *) ti
;
344 if ((msg
& SCSI_LOW_MSG_WIDE
) != 0)
346 if (ti
->ti_width
!= SCSI_LOW_BUS_WIDTH_8
)
348 ti
->ti_width
= SCSI_LOW_BUS_WIDTH_8
;
354 if ((msg
& SCSI_LOW_MSG_SYNCH
) == 0)
357 period
= ti
->ti_maxsynch
.period
;
358 period
= period
<< 2;
361 sti
->sti_reg_synch
= (period
- 200) / 50;
363 sti
->sti_reg_synch
++;
364 sti
->sti_reg_synch
|= SSCTL_SYNCHEN
;
366 else if (period
>= 100)
368 sti
->sti_reg_synch
= (period
- 100) / 50;
370 sti
->sti_reg_synch
++;
371 sti
->sti_reg_synch
|= SSCTL_SYNCHEN
| SSCTL_FSYNCHEN
;
373 bus_space_write_1(iot
, ioh
, tmc_ssctl
, sti
->sti_reg_synch
);
377 /**************************************************************
378 * General probe attach
379 **************************************************************/
381 stgprobesubr(bus_space_tag_t iot
, bus_space_handle_t ioh
, u_int dvcfg
)
385 lsb
= bus_space_read_1(iot
, ioh
, tmc_idlsb
);
386 msb
= bus_space_read_1(iot
, ioh
, tmc_idmsb
);
387 switch (msb
<< 8 | lsb
)
401 stgprint(void *aux
, const char *name
)
405 kprintf("%s: scsibus ", name
);
410 stgattachsubr(struct stg_softc
*sc
)
412 struct scsi_low_softc
*slp
= &sc
->sc_sclow
;
416 sc
->sc_idbit
= (1 << slp
->sl_hostid
);
417 slp
->sl_funcs
= &stgfuncs
;
418 sc
->sc_tmaxcnt
= SCSI_LOW_MIN_TOUT
* 1000 * 1000; /* default */
420 slp
->sl_flags
|= HW_READ_PADDING
;
421 slp
->sl_cfgflags
|= CFG_ASYNC
; /* XXX */
423 (void) scsi_low_attach(slp
, 0, STG_NTARGETS
, STG_NLUNS
,
424 sizeof(struct stg_targ_info
), 0);
427 /**************************************************************
429 **************************************************************/
431 stg_pdma_end(struct stg_softc
*sc
, struct targ_info
*ti
)
433 struct scsi_low_softc
*slp
= &sc
->sc_sclow
;
434 bus_space_tag_t iot
= sc
->sc_iot
;
435 bus_space_handle_t ioh
= sc
->sc_ioh
;
436 struct slccb
*cb
= slp
->sl_Qnexus
;
439 slp
->sl_flags
&= ~HW_PDMASTART
;
440 sc
->sc_icinit
&= ~ICTL_FIFO
;
441 sc
->sc_dataout_timeout
= 0;
445 slp
->sl_error
|= PDMAERR
;
449 if (ti
->ti_phase
== PH_DATA
)
451 len
= bus_space_read_2(iot
, ioh
, tmc_fdcnt
);
452 if (slp
->sl_scp
.scp_direction
== SCSI_LOW_WRITE
)
456 tres
= len
+ slp
->sl_scp
.scp_datalen
;
457 if (tres
<= (u_int
) cb
->ccb_scp
.scp_datalen
)
459 slp
->sl_scp
.scp_data
-= len
;
460 slp
->sl_scp
.scp_datalen
= tres
;
464 slp
->sl_error
|= PDMAERR
;
465 kprintf("%s len %x >= datalen %x\n",
467 len
, slp
->sl_scp
.scp_datalen
);
471 else if (slp
->sl_scp
.scp_direction
== SCSI_LOW_READ
)
475 slp
->sl_error
|= PDMAERR
;
476 kprintf("%s: len %x left in fifo\n",
480 scsi_low_data_finish(slp
);
485 kprintf("%s data phase miss\n", slp
->sl_xname
);
486 slp
->sl_error
|= PDMAERR
;
490 bus_space_write_1(iot
, ioh
, tmc_fctl
, sc
->sc_fcRinit
);
494 stg_pio_read(struct stg_softc
*sc
, struct targ_info
*ti
, u_int thold
)
496 struct scsi_low_softc
*slp
= &sc
->sc_sclow
;
497 bus_space_tag_t iot
= sc
->sc_iot
;
498 bus_space_handle_t ioh
= sc
->sc_ioh
;
499 struct sc_p
*sp
= &slp
->sl_scp
;
504 if ((slp
->sl_flags
& HW_PDMASTART
) == 0)
506 bus_space_write_1(iot
, ioh
, tmc_fctl
,
507 sc
->sc_fcRinit
| FCTL_FIFOEN
);
508 slp
->sl_flags
|= HW_PDMASTART
;
511 tout
= sc
->sc_tmaxcnt
;
517 res
= bus_space_read_2(iot
, ioh
, tmc_fdcnt
);
520 bus_space_write_1(iot
, ioh
, tmc_ictl
,
529 stat
= bus_space_read_1(iot
, ioh
, tmc_bstat
);
530 res
= bus_space_read_2(iot
, ioh
, 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 kprintf("%s: read padding required\n",
557 if (res
> STG_MAX_DATA_SIZE
)
558 res
= STG_MAX_DATA_SIZE
;
561 (void) bus_space_read_1(iot
, ioh
, tmc_rfifo
);
566 sp
->scp_datalen
-= res
;
569 *sp
->scp_data
= bus_space_read_1(iot
, ioh
, tmc_rfifo
);
574 bus_space_read_multi_2(iot
, ioh
, tmc_rfifo
,
575 (u_int16_t
*) sp
->scp_data
, res
>> 1);
580 kprintf("%s: pio read timeout\n", slp
->sl_xname
);
584 stg_pio_write(struct stg_softc
*sc
, struct targ_info
*ti
, u_int thold
)
586 struct scsi_low_softc
*slp
= &sc
->sc_sclow
;
587 bus_space_tag_t iot
= sc
->sc_iot
;
588 bus_space_handle_t ioh
= sc
->sc_ioh
;
589 struct sc_p
*sp
= &slp
->sl_scp
;
594 if ((slp
->sl_flags
& HW_PDMASTART
) == 0)
596 stat
= sc
->sc_fcWinit
| FCTL_FIFOEN
| FCTL_FIFOW
;
597 bus_space_write_1(iot
, ioh
, tmc_fctl
, stat
| FCTL_CLRFIFO
);
598 bus_space_write_1(iot
, ioh
, tmc_fctl
, stat
);
599 slp
->sl_flags
|= HW_PDMASTART
;
602 tout
= sc
->sc_tmaxcnt
;
605 stat
= bus_space_read_1(iot
, ioh
, tmc_bstat
);
606 if ((stat
& PHASE_MASK
) != DATA_OUT_PHASE
)
609 if (sp
->scp_datalen
<= 0)
611 if (sc
->sc_dataout_timeout
== 0)
612 sc
->sc_dataout_timeout
= SCSI_LOW_TIMEOUT_HZ
;
619 res
= bus_space_read_2(iot
, ioh
, tmc_fdcnt
);
622 bus_space_write_1(iot
, ioh
, tmc_ictl
,
631 res
= bus_space_read_2(iot
, ioh
, tmc_fdcnt
);
632 if (res
> sc
->sc_maxwsize
/ 2)
639 if (res
== (u_int
) -1)
641 res
= sc
->sc_maxwsize
- res
;
642 if (res
> sp
->scp_datalen
)
643 res
= sp
->scp_datalen
;
645 sp
->scp_datalen
-= res
;
646 if ((res
& 0x1) != 0)
648 bus_space_write_1(iot
, ioh
, tmc_wfifo
, *sp
->scp_data
);
653 bus_space_write_multi_2(iot
, ioh
, tmc_wfifo
,
654 (u_int16_t
*) sp
->scp_data
, res
>> 1);
659 kprintf("%s: pio write timeout\n", slp
->sl_xname
);
663 stg_negate_signal(struct stg_softc
*sc
, u_int8_t mask
, u_char
*s
)
665 struct scsi_low_softc
*slp
= &sc
->sc_sclow
;
666 bus_space_tag_t bst
= sc
->sc_iot
;
667 bus_space_handle_t bsh
= sc
->sc_ioh
;
671 for (wc
= 0; wc
< STG_DELAY_MAX
/ STG_DELAY_INTERVAL
; wc
++)
673 regv
= bus_space_read_1(bst
, bsh
, tmc_bstat
);
674 if (regv
== (u_int8_t
) -1)
676 if ((regv
& mask
) == 0)
679 SCSI_LOW_DELAY(STG_DELAY_INTERVAL
);
682 kprintf("%s: %s stg_negate_signal timeout\n", slp
->sl_xname
, s
);
687 stg_expect_signal(struct stg_softc
*sc
, u_int8_t phase
, u_int8_t mask
)
689 struct scsi_low_softc
*slp
= &sc
->sc_sclow
;
690 bus_space_tag_t bst
= sc
->sc_iot
;
691 bus_space_handle_t bsh
= sc
->sc_ioh
;
696 for (wc
= 0; wc
< STG_DELAY_MAX
/ STG_DELAY_INTERVAL
; wc
++)
698 ph
= bus_space_read_1(bst
, bsh
, tmc_bstat
);
699 if (ph
== (u_int8_t
) -1)
701 if ((ph
& PHASE_MASK
) != phase
)
703 if ((ph
& mask
) != 0)
706 SCSI_LOW_DELAY(STG_DELAY_INTERVAL
);
709 kprintf("%s: stg_expect_signal timeout\n", slp
->sl_xname
);
714 stg_xfer(struct stg_softc
*sc
, u_int8_t
*buf
, int len
, int phase
,
717 bus_space_tag_t iot
= sc
->sc_iot
;
718 bus_space_handle_t ioh
= sc
->sc_ioh
;
721 if (phase
& BSTAT_IO
)
722 bus_space_write_1(iot
, ioh
, tmc_fctl
, sc
->sc_fcRinit
);
724 bus_space_write_1(iot
, ioh
, tmc_fctl
, sc
->sc_fcWinit
);
726 for (ptr
= 0; len
> 0; len
--)
728 rv
= stg_expect_signal(sc
, phase
, BSTAT_REQ
);
732 if (len
== 1 && clear_atn
!= 0)
734 sc
->sc_busc
&= ~BCTL_ATN
;
735 stghw_bcr_write_1(sc
, sc
->sc_busc
);
736 SCSI_LOW_DEASSERT_ATN(&sc
->sc_sclow
);
739 if (phase
& BSTAT_IO
)
741 buf
[ptr
++] = bus_space_read_1(iot
, ioh
, tmc_rdata
);
745 bus_space_write_1(iot
, ioh
, tmc_wdata
, buf
[ptr
++]);
748 stg_negate_signal(sc
, BSTAT_ACK
, "xfer<ACK>");
752 bus_space_write_1(iot
, ioh
, tmc_fctl
, sc
->sc_fcRinit
);
756 /**************************************************************
757 * disconnect & reselect (HW low)
758 **************************************************************/
760 stg_reselected(struct stg_softc
*sc
)
762 struct scsi_low_softc
*slp
= &sc
->sc_sclow
;
763 bus_space_tag_t iot
= sc
->sc_iot
;
764 bus_space_handle_t ioh
= sc
->sc_ioh
;
769 if (slp
->sl_selid
!= NULL
)
772 * Selection vs Reselection conflicts.
774 bus_space_write_1(iot
, ioh
, tmc_fctl
, sc
->sc_fcRinit
);
775 stghw_bcr_write_1(sc
, BCTL_BUSFREE
);
777 else if (slp
->sl_Tnexus
!= NULL
)
779 kprintf("%s: unexpected termination\n", slp
->sl_xname
);
780 stg_disconnected(sc
, slp
->sl_Tnexus
);
784 * We should ack the reselection as soon as possible,
785 * because the target would abort the current reselection seq
786 * due to reselection timeout.
788 tout
= STG_DELAY_SELECT_POLLING_MAX
;
791 regv
= bus_space_read_1(iot
, ioh
, tmc_bstat
);
792 if ((regv
& (BSTAT_IO
| BSTAT_SEL
| BSTAT_BSY
)) ==
793 (BSTAT_IO
| BSTAT_SEL
))
796 regv
= bus_space_read_1(iot
, ioh
, tmc_bstat
);
797 if ((regv
& (BSTAT_IO
| BSTAT_SEL
| BSTAT_BSY
)) ==
798 (BSTAT_IO
| BSTAT_SEL
))
803 kprintf("%s: reselction timeout I\n", slp
->sl_xname
);
807 sid
= (u_int
) bus_space_read_1(iot
, ioh
, tmc_scsiid
);
808 if ((sid
& sc
->sc_idbit
) == 0)
814 bus_space_write_1(iot
, ioh
, tmc_fctl
,
815 sc
->sc_fcRinit
| FCTL_CLRFIFO
| FCTL_CLRINT
);
816 bus_space_write_1(iot
, ioh
, tmc_fctl
, sc
->sc_fcRinit
);
817 stghw_bcr_write_1(sc
, sc
->sc_busc
| BCTL_BSY
);
821 regv
= bus_space_read_1(iot
, ioh
, tmc_bstat
);
822 if ((regv
& (BSTAT_SEL
| BSTAT_BSY
)) == BSTAT_BSY
)
826 kprintf("%s: reselction timeout II\n", slp
->sl_xname
);
830 sid
&= ~sc
->sc_idbit
;
832 if (scsi_low_reselected(slp
, sid
) == NULL
)
836 stg_statics
.reselect
++;
837 #endif /* STG_STATICS */
842 stg_disconnected(struct stg_softc
*sc
, struct targ_info
*ti
)
844 struct scsi_low_softc
*slp
= &sc
->sc_sclow
;
845 bus_space_tag_t iot
= sc
->sc_iot
;
846 bus_space_handle_t ioh
= sc
->sc_ioh
;
848 /* clear bus status & fifo */
849 bus_space_write_1(iot
, ioh
, tmc_fctl
, sc
->sc_fcRinit
| FCTL_CLRFIFO
);
850 bus_space_write_1(iot
, ioh
, tmc_fctl
, sc
->sc_fcRinit
);
851 stghw_bcr_write_1(sc
, BCTL_BUSFREE
);
852 sc
->sc_icinit
&= ~ICTL_FIFO
;
853 sc
->sc_busc
&= ~BCTL_ATN
;
854 sc
->sc_dataout_timeout
= 0;
855 sc
->sc_ubf_timeout
= 0;
858 stg_statics
.disconnect
++;
859 #endif /* STG_STATICS */
860 scsi_low_disconnected(slp
, ti
);
864 /**************************************************************
866 **************************************************************/
868 stg_target_nexus_establish(struct stg_softc
*sc
)
870 struct scsi_low_softc
*slp
= &sc
->sc_sclow
;
871 bus_space_tag_t iot
= sc
->sc_iot
;
872 bus_space_handle_t ioh
= sc
->sc_ioh
;
873 struct targ_info
*ti
= slp
->sl_Tnexus
;
874 struct stg_targ_info
*sti
= (void *) ti
;
876 bus_space_write_1(iot
, ioh
, tmc_ssctl
, sti
->sti_reg_synch
);
877 if ((stg_io_control
& STG_FIFO_INTERRUPTS
) != 0)
879 sc
->sc_icinit
|= ICTL_FIFO
;
885 stg_lun_nexus_establish(struct stg_softc
*sc
)
892 stg_ccb_nexus_establish(struct stg_softc
*sc
)
894 struct scsi_low_softc
*slp
= &sc
->sc_sclow
;
895 struct slccb
*cb
= slp
->sl_Qnexus
;
897 sc
->sc_tmaxcnt
= cb
->ccb_tcmax
* 1000 * 1000;
901 #define STGHW_SELECT_INTERVAL 10
904 stghw_select_targ_wait(struct stg_softc
*sc
, int mu
)
906 bus_space_tag_t iot
= sc
->sc_iot
;
907 bus_space_handle_t ioh
= sc
->sc_ioh
;
909 mu
= mu
/ STGHW_SELECT_INTERVAL
;
912 if ((bus_space_read_1(iot
, ioh
, tmc_bstat
) & BSTAT_BSY
) == 0)
914 SCSI_LOW_DELAY(STGHW_SELECT_INTERVAL
);
918 if ((bus_space_read_1(iot
, ioh
, tmc_bstat
) & BSTAT_BSY
) != 0)
927 stg_selection_done_and_expect_msgout(struct stg_softc
*sc
)
929 struct scsi_low_softc
*slp
= &sc
->sc_sclow
;
930 bus_space_tag_t iot
= sc
->sc_iot
;
931 bus_space_handle_t ioh
= sc
->sc_ioh
;
933 bus_space_write_1(iot
, ioh
, tmc_fctl
, sc
->sc_fcRinit
| FCTL_CLRFIFO
);
934 bus_space_write_1(iot
, ioh
, tmc_fctl
, sc
->sc_fcRinit
);
935 stghw_bcr_write_1(sc
, sc
->sc_imsg
| sc
->sc_busc
);
936 SCSI_LOW_ASSERT_ATN(slp
);
942 struct stg_softc
*sc
= arg
;
943 struct scsi_low_softc
*slp
= &sc
->sc_sclow
;
944 bus_space_tag_t iot
= sc
->sc_iot
;
945 bus_space_handle_t ioh
= sc
->sc_ioh
;
946 struct targ_info
*ti
;
950 u_int8_t status
, astatus
, regv
;
952 /*******************************************
954 *******************************************/
955 if (slp
->sl_flags
& HW_INACTIVE
)
958 astatus
= bus_space_read_1(iot
, ioh
, tmc_astat
);
959 status
= bus_space_read_1(iot
, ioh
, tmc_bstat
);
961 if ((astatus
& ASTAT_STATMASK
) == 0 || astatus
== (u_int8_t
) -1)
964 bus_space_write_1(iot
, ioh
, tmc_ictl
, 0);
965 if (astatus
& ASTAT_SCSIRST
)
967 bus_space_write_1(iot
, ioh
, tmc_fctl
,
968 sc
->sc_fcRinit
| FCTL_CLRFIFO
);
969 bus_space_write_1(iot
, ioh
, tmc_fctl
, sc
->sc_fcRinit
);
970 bus_space_write_1(iot
, ioh
, tmc_ictl
, 0);
972 scsi_low_restart(slp
, SCSI_LOW_RESTART_SOFT
,
973 "bus reset (power off?)");
977 /*******************************************
979 *******************************************/
983 scsi_low_print(slp
, NULL
);
984 kprintf("%s: st %x ist %x\n\n", slp
->sl_xname
,
988 SCSI_LOW_DEBUGGER("stg");
991 #endif /* STG_DEBUG */
993 /*******************************************
994 * reselection & nexus
995 *******************************************/
996 if ((status
& RESEL_PHASE_MASK
)== PHASE_RESELECTED
)
998 if (stg_reselected(sc
) == EJUSTRETURN
)
1002 if ((ti
= slp
->sl_Tnexus
) == NULL
)
1006 if ((astatus
& ASTAT_PARERR
) != 0 && ti
->ti_phase
!= PH_ARBSTART
&&
1007 (sc
->sc_fcRinit
& FCTL_PARENB
) != 0)
1009 slp
->sl_error
|= PARITYERR
;
1010 derror
= SCSI_LOW_DATA_PE
;
1011 if ((status
& PHASE_MASK
) == MESSAGE_IN_PHASE
)
1012 scsi_low_assert_msg(slp
, ti
, SCSI_LOW_MSG_PARITY
, 0);
1014 scsi_low_assert_msg(slp
, ti
, SCSI_LOW_MSG_ERROR
, 1);
1017 /*******************************************
1018 * aribitration & selection
1019 *******************************************/
1020 switch (ti
->ti_phase
)
1023 if ((astatus
& ASTAT_ARBIT
) == 0)
1026 stg_statics
.arbit_fail_0
++;
1027 #endif /* STG_STATICS */
1031 status
= bus_space_read_1(iot
, ioh
, tmc_bstat
);
1032 if ((status
& BSTAT_IO
) != 0)
1035 * Selection vs Reselection conflicts.
1038 stg_statics
.arbit_fail_1
++;
1039 #endif /* STG_STATICS */
1041 bus_space_write_1(iot
, ioh
, tmc_fctl
, sc
->sc_fcRinit
);
1042 stghw_bcr_write_1(sc
, BCTL_BUSFREE
);
1043 scsi_low_arbit_fail(slp
, slp
->sl_Qnexus
);
1048 * selection assert start.
1050 SCSI_LOW_SETUP_PHASE(ti
, PH_SELSTART
);
1051 scsi_low_arbit_win(slp
);
1054 bus_space_write_1(iot
, ioh
, tmc_scsiid
,
1055 sc
->sc_idbit
| (1 << ti
->ti_id
));
1056 stghw_bcr_write_1(sc
, sc
->sc_imsg
| sc
->sc_busc
| BCTL_SEL
);
1057 bus_space_write_1(iot
, ioh
, tmc_fctl
, sc
->sc_fcWinit
);
1058 if ((stg_io_control
& STG_WAIT_FOR_SELECT
) != 0)
1060 /* selection abort delay 200 + 100 micro sec */
1061 if (stghw_select_targ_wait(sc
, 300) == 0)
1063 SCSI_LOW_SETUP_PHASE(ti
, PH_SELECTED
);
1064 stg_selection_done_and_expect_msgout(sc
);
1071 if ((status
& BSTAT_BSY
) == 0)
1073 /* selection timeout delay 250 ms */
1074 if (stghw_select_targ_wait(sc
, 250 * 1000) != 0)
1076 stg_disconnected(sc
, ti
);
1081 SCSI_LOW_SETUP_PHASE(ti
, PH_SELECTED
);
1082 stg_selection_done_and_expect_msgout(sc
);
1086 if ((status
& BSTAT_REQ
) == 0)
1088 stg_target_nexus_establish(sc
);
1092 if ((status
& BSTAT_REQ
) == 0)
1095 /* clear a busy line */
1096 bus_space_write_1(iot
, ioh
, tmc_fctl
, sc
->sc_fcRinit
);
1097 stghw_bcr_write_1(sc
, sc
->sc_busc
);
1098 stg_target_nexus_establish(sc
);
1099 if ((status
& PHASE_MASK
) != MESSAGE_IN_PHASE
)
1101 kprintf("%s: unexpected phase after reselect\n",
1103 slp
->sl_error
|= FATALIO
;
1104 scsi_low_assert_msg(slp
, ti
, SCSI_LOW_MSG_ABORT
, 1);
1110 /*******************************************
1112 *******************************************/
1113 if ((slp
->sl_flags
& HW_PDMASTART
) && STG_IS_PHASE_DATA(status
) == 0)
1115 if (slp
->sl_scp
.scp_direction
== SCSI_LOW_READ
)
1116 stg_pio_read(sc
, ti
, 0);
1118 stg_pdma_end(sc
, ti
);
1121 /*******************************************
1123 *******************************************/
1124 switch (status
& PHASE_MASK
)
1127 if (stg_expect_signal(sc
, COMMAND_PHASE
, BSTAT_REQ
) <= 0)
1130 SCSI_LOW_SETUP_PHASE(ti
, PH_CMD
);
1131 if (scsi_low_cmd(slp
, ti
) != 0)
1133 scsi_low_attention(slp
);
1136 if (stg_xfer(sc
, slp
->sl_scp
.scp_cmd
, slp
->sl_scp
.scp_cmdlen
,
1137 COMMAND_PHASE
, 0) != 0)
1139 kprintf("%s: CMDOUT short\n", slp
->sl_xname
);
1143 case DATA_OUT_PHASE
:
1144 SCSI_LOW_SETUP_PHASE(ti
, PH_DATA
);
1145 if (scsi_low_data(slp
, ti
, &bp
, SCSI_LOW_WRITE
) != 0)
1147 scsi_low_attention(slp
);
1150 if ((sc
->sc_icinit
& ICTL_FIFO
) != 0)
1151 stg_pio_write(sc
, ti
, sc
->sc_wthold
);
1153 stg_pio_write(sc
, ti
, 0);
1157 SCSI_LOW_SETUP_PHASE(ti
, PH_DATA
);
1158 if (scsi_low_data(slp
, ti
, &bp
, SCSI_LOW_READ
) != 0)
1160 scsi_low_attention(slp
);
1163 if ((sc
->sc_icinit
& ICTL_FIFO
) != 0)
1164 stg_pio_read(sc
, ti
, sc
->sc_rthold
);
1166 stg_pio_read(sc
, ti
, 0);
1170 regv
= stg_expect_signal(sc
, STATUS_PHASE
, BSTAT_REQ
);
1174 SCSI_LOW_SETUP_PHASE(ti
, PH_STAT
);
1175 regv
= bus_space_read_1(iot
, ioh
, tmc_sdna
);
1176 if (scsi_low_statusin(slp
, ti
, regv
| derror
) != 0)
1178 scsi_low_attention(slp
);
1180 if (regv
!= bus_space_read_1(iot
, ioh
, tmc_rdata
))
1182 kprintf("%s: STATIN: data mismatch\n", slp
->sl_xname
);
1184 stg_negate_signal(sc
, BSTAT_ACK
, "statin<ACK>");
1187 case MESSAGE_OUT_PHASE
:
1188 if (stg_expect_signal(sc
, MESSAGE_OUT_PHASE
, BSTAT_REQ
) <= 0)
1191 SCSI_LOW_SETUP_PHASE(ti
, PH_MSGOUT
);
1192 flags
= (ti
->ti_ophase
!= ti
->ti_phase
) ?
1193 SCSI_LOW_MSGOUT_INIT
: 0;
1194 len
= scsi_low_msgout(slp
, ti
, flags
);
1196 if (len
> 1 && slp
->sl_atten
== 0)
1198 scsi_low_attention(slp
);
1201 if (stg_xfer(sc
, ti
->ti_msgoutstr
, len
, MESSAGE_OUT_PHASE
,
1202 slp
->sl_clear_atten
) != 0)
1204 kprintf("%s: MSGOUT short\n", slp
->sl_xname
);
1208 if (slp
->sl_msgphase
>= MSGPH_ABORT
)
1210 stg_disconnected(sc
, ti
);
1215 case MESSAGE_IN_PHASE
:
1216 /* confirm phase and req signal */
1217 if (stg_expect_signal(sc
, MESSAGE_IN_PHASE
, BSTAT_REQ
) <= 0)
1220 SCSI_LOW_SETUP_PHASE(ti
, PH_MSGIN
);
1222 /* read data with NOACK */
1223 regv
= bus_space_read_1(iot
, ioh
, tmc_sdna
);
1225 if (scsi_low_msgin(slp
, ti
, derror
| regv
) == 0)
1227 if (scsi_low_is_msgout_continue(ti
, 0) != 0)
1229 scsi_low_attention(slp
);
1233 /* read data with ACK */
1234 if (regv
!= bus_space_read_1(iot
, ioh
, tmc_rdata
))
1236 kprintf("%s: MSGIN: data mismatch\n", slp
->sl_xname
);
1239 /* wait for the ack negated */
1240 stg_negate_signal(sc
, BSTAT_ACK
, "msgin<ACK>");
1242 if (slp
->sl_msgphase
!= 0 && slp
->sl_msgphase
< MSGPH_ABORT
)
1244 stg_disconnected(sc
, ti
);
1249 kprintf("%s: unexpected disconnect\n", slp
->sl_xname
);
1250 stg_disconnected(sc
, ti
);
1254 slp
->sl_error
|= FATALIO
;
1255 kprintf("%s: unknown phase bus %x intr %x\n",
1256 slp
->sl_xname
, status
, astatus
);
1261 bus_space_write_1(iot
, ioh
, tmc_ictl
, sc
->sc_icinit
);
1266 stg_timeout(struct stg_softc
*sc
)
1268 struct scsi_low_softc
*slp
= &sc
->sc_sclow
;
1269 bus_space_tag_t iot
= sc
->sc_iot
;
1270 bus_space_handle_t ioh
= sc
->sc_ioh
;
1274 if (slp
->sl_Tnexus
== NULL
)
1277 status
= bus_space_read_1(iot
, ioh
, tmc_bstat
);
1278 if ((status
& PHASE_MASK
) == 0)
1280 if (sc
->sc_ubf_timeout
++ == 0)
1283 kprintf("%s: unexpected bus free detected\n", slp
->sl_xname
);
1284 slp
->sl_error
|= FATALIO
;
1285 scsi_low_print(slp
, slp
->sl_Tnexus
);
1286 stg_disconnected(sc
, slp
->sl_Tnexus
);
1290 switch (status
& PHASE_MASK
)
1292 case DATA_OUT_PHASE
:
1293 if (sc
->sc_dataout_timeout
== 0)
1295 if ((status
& BSTAT_REQ
) == 0)
1297 if (bus_space_read_2(iot
, ioh
, tmc_fdcnt
) != 0)
1299 if ((-- sc
->sc_dataout_timeout
) > 0)
1302 slp
->sl_error
|= PDMAERR
;
1303 if ((slp
->sl_flags
& HW_WRITE_PADDING
) == 0)
1305 kprintf("%s: write padding required\n",
1310 bus_space_write_1(iot
, ioh
, tmc_ictl
, 0);
1312 tout
= STG_DELAY_MAX
;
1315 status
= bus_space_read_1(iot
, ioh
, tmc_bstat
);
1316 if ((status
& PHASE_MASK
) != DATA_OUT_PHASE
)
1319 if (bus_space_read_2(iot
, ioh
, tmc_fdcnt
) != 0)
1325 for (count
= sc
->sc_maxwsize
; count
> 0; count
--)
1326 bus_space_write_1(iot
, ioh
, tmc_wfifo
, 0);
1329 status
= bus_space_read_1(iot
, ioh
, tmc_bstat
);
1330 if ((status
& PHASE_MASK
) == DATA_OUT_PHASE
)
1331 sc
->sc_dataout_timeout
= SCSI_LOW_TIMEOUT_HZ
;
1333 bus_space_write_1(iot
, ioh
, tmc_ictl
, sc
->sc_icinit
);