1 /* $NetBSD: iha.c,v 1.39 2008/05/03 05:21:25 tsutsui Exp $ */
4 * Copyright (c) 2001, 2002 Izumi Tsutsui
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 * Device driver for the INI-9XXXU/UW or INIC-940/950 PCI SCSI Controller.
30 * Written for 386bsd and FreeBSD by
31 * Winston Hung <winstonh@initio.com>
33 * Copyright (c) 1997-1999 Initio Corp.
34 * Copyright (c) 2000, 2001 Ken Westerback
35 * All rights reserved.
37 * Redistribution and use in source and binary forms, with or without
38 * modification, are permitted provided that the following conditions
40 * 1. Redistributions of source code must retain the above copyright
41 * notice, this list of conditions and the following disclaimer,
42 * without modification, immediately at the beginning of the file.
43 * 2. The name of the author may not be used to endorse or promote products
44 * derived from this software without specific prior written permission.
46 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
47 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
48 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
49 * IN NO EVENT SHALL THE AUTHOR OR HIS RELATIVES BE LIABLE FOR ANY DIRECT,
50 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
51 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
52 * SERVICES; LOSS OF MIND, USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
53 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
54 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
55 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
56 * THE POSSIBILITY OF SUCH DAMAGE.
60 * Ported to NetBSD by Izumi Tsutsui <tsutsui@NetBSD.org> from OpenBSD:
61 * $OpenBSD: iha.c,v 1.3 2001/02/20 00:47:33 krw Exp $
64 #include <sys/cdefs.h>
65 __KERNEL_RCSID(0, "$NetBSD: iha.c,v 1.39 2008/05/03 05:21:25 tsutsui Exp $");
67 #include <sys/param.h>
68 #include <sys/systm.h>
69 #include <sys/kernel.h>
71 #include <sys/device.h>
72 #include <sys/malloc.h>
74 #include <uvm/uvm_extern.h>
79 #include <dev/scsipi/scsi_spc.h>
80 #include <dev/scsipi/scsi_all.h>
81 #include <dev/scsipi/scsipi_all.h>
82 #include <dev/scsipi/scsiconf.h>
83 #include <dev/scsipi/scsi_message.h>
85 #include <dev/ic/ihareg.h>
86 #include <dev/ic/ihavar.h>
89 * SCSI Rate Table, indexed by FLAG_SCSI_RATE field of
92 static const uint8_t iha_rate_tbl
[] = {
94 /* nanosecond divide by 4 */
100 43, /* 175ns, 5.7M */
104 #define IHA_MAX_PERIOD 62
107 static uint16_t eeprom_default
[EEPROM_SIZE
] = {
108 /* -- Header ------------------------------------ */
112 EEP_WORD(EEPROM_SIZE
* 2, 0x01),
113 /* -- Host Adapter Structure -------------------- */
116 /* model info, number of channel */
120 /* host adapter config */
123 /* -- eeprom_adapter[0] ------------------------------- */
124 /* ID, adapter config 1 */
125 EEP_WORD(7, CFG_DEFAULT
),
126 /* adapter config 2, number of targets */
129 EEP_WORD(FLAG_DEFAULT
, FLAG_DEFAULT
),
130 EEP_WORD(FLAG_DEFAULT
, FLAG_DEFAULT
),
131 EEP_WORD(FLAG_DEFAULT
, FLAG_DEFAULT
),
132 EEP_WORD(FLAG_DEFAULT
, FLAG_DEFAULT
),
133 EEP_WORD(FLAG_DEFAULT
, FLAG_DEFAULT
),
134 EEP_WORD(FLAG_DEFAULT
, FLAG_DEFAULT
),
135 EEP_WORD(FLAG_DEFAULT
, FLAG_DEFAULT
),
136 EEP_WORD(FLAG_DEFAULT
, FLAG_DEFAULT
),
138 /* -- eeprom_adapter[1] ------------------------------- */
139 /* ID, adapter config 1 */
140 EEP_WORD(7, CFG_DEFAULT
),
141 /* adapter config 2, number of targets */
144 EEP_WORD(FLAG_DEFAULT
, FLAG_DEFAULT
),
145 EEP_WORD(FLAG_DEFAULT
, FLAG_DEFAULT
),
146 EEP_WORD(FLAG_DEFAULT
, FLAG_DEFAULT
),
147 EEP_WORD(FLAG_DEFAULT
, FLAG_DEFAULT
),
148 EEP_WORD(FLAG_DEFAULT
, FLAG_DEFAULT
),
149 EEP_WORD(FLAG_DEFAULT
, FLAG_DEFAULT
),
150 EEP_WORD(FLAG_DEFAULT
, FLAG_DEFAULT
),
151 EEP_WORD(FLAG_DEFAULT
, FLAG_DEFAULT
),
159 static void iha_append_free_scb(struct iha_softc
*, struct iha_scb
*);
160 static void iha_append_done_scb(struct iha_softc
*, struct iha_scb
*, uint8_t);
161 static inline struct iha_scb
*iha_pop_done_scb(struct iha_softc
*);
163 static struct iha_scb
*iha_find_pend_scb(struct iha_softc
*);
164 static inline void iha_append_pend_scb(struct iha_softc
*, struct iha_scb
*);
165 static inline void iha_push_pend_scb(struct iha_softc
*, struct iha_scb
*);
166 static inline void iha_del_pend_scb(struct iha_softc
*, struct iha_scb
*);
167 static inline void iha_mark_busy_scb(struct iha_scb
*);
169 static inline void iha_set_ssig(struct iha_softc
*, uint8_t, uint8_t);
171 static int iha_alloc_sglist(struct iha_softc
*);
173 static void iha_scsipi_request(struct scsipi_channel
*, scsipi_adapter_req_t
,
175 static void iha_update_xfer_mode(struct iha_softc
*, int);
177 static void iha_reset_scsi_bus(struct iha_softc
*);
178 static void iha_reset_chip(struct iha_softc
*);
179 static void iha_reset_dma(struct iha_softc
*);
180 static void iha_reset_tcs(struct tcs
*, uint8_t);
182 static void iha_main(struct iha_softc
*);
183 static void iha_scsi(struct iha_softc
*);
184 static void iha_select(struct iha_softc
*, struct iha_scb
*, uint8_t);
185 static int iha_wait(struct iha_softc
*, uint8_t);
187 static void iha_exec_scb(struct iha_softc
*, struct iha_scb
*);
188 static void iha_done_scb(struct iha_softc
*, struct iha_scb
*);
189 static int iha_push_sense_request(struct iha_softc
*, struct iha_scb
*);
191 static void iha_timeout(void *);
192 static void iha_abort_xs(struct iha_softc
*, struct scsipi_xfer
*, uint8_t);
193 static uint8_t iha_data_over_run(struct iha_scb
*);
195 static int iha_next_state(struct iha_softc
*);
196 static int iha_state_1(struct iha_softc
*);
197 static int iha_state_2(struct iha_softc
*);
198 static int iha_state_3(struct iha_softc
*);
199 static int iha_state_4(struct iha_softc
*);
200 static int iha_state_5(struct iha_softc
*);
201 static int iha_state_6(struct iha_softc
*);
202 static int iha_state_8(struct iha_softc
*);
204 static int iha_xfer_data(struct iha_softc
*, struct iha_scb
*, int);
205 static int iha_xpad_in(struct iha_softc
*);
206 static int iha_xpad_out(struct iha_softc
*);
208 static int iha_status_msg(struct iha_softc
*);
209 static void iha_busfree(struct iha_softc
*);
210 static int iha_resel(struct iha_softc
*);
212 static int iha_msgin(struct iha_softc
*);
213 static int iha_msgin_extended(struct iha_softc
*);
214 static int iha_msgin_sdtr(struct iha_softc
*);
215 static int iha_msgin_ignore_wid_resid(struct iha_softc
*);
217 static int iha_msgout(struct iha_softc
*, uint8_t);
218 static void iha_msgout_abort(struct iha_softc
*, uint8_t);
219 static int iha_msgout_reject(struct iha_softc
*);
220 static int iha_msgout_extended(struct iha_softc
*);
221 static int iha_msgout_wdtr(struct iha_softc
*);
222 static int iha_msgout_sdtr(struct iha_softc
*);
224 static void iha_wide_done(struct iha_softc
*);
225 static void iha_sync_done(struct iha_softc
*);
227 static void iha_bad_seq(struct iha_softc
*);
229 static void iha_read_eeprom(struct iha_softc
*, struct iha_eeprom
*);
230 static int iha_se2_rd_all(struct iha_softc
*, uint16_t *);
231 static void iha_se2_instr(struct iha_softc
*, int);
232 static uint16_t iha_se2_rd(struct iha_softc
*, int);
234 static void iha_se2_update_all(struct iha_softc
*);
235 static void iha_se2_wr(struct iha_softc
*, int, uint16_t);
239 * iha_append_free_scb - append the supplied SCB to the tail of the
240 * sc_freescb queue after clearing and resetting
241 * everything possible.
244 iha_append_free_scb(struct iha_softc
*sc
, struct iha_scb
*scb
)
250 if (scb
== sc
->sc_actscb
)
251 sc
->sc_actscb
= NULL
;
253 scb
->status
= STATUS_QUEUED
;
254 scb
->ha_stat
= HOST_OK
;
255 scb
->ta_stat
= SCSI_OK
;
264 * scb_tagid, sg_addr, sglist
265 * SCB_SensePtr are set at initialization
269 TAILQ_INSERT_TAIL(&sc
->sc_freescb
, scb
, chain
);
275 iha_append_done_scb(struct iha_softc
*sc
, struct iha_scb
*scb
, uint8_t hastat
)
283 callout_stop(&scb
->xs
->xs_callout
);
285 if (scb
== sc
->sc_actscb
)
286 sc
->sc_actscb
= NULL
;
290 if (scb
->scb_tagmsg
!= 0) {
293 } else if (tcs
->ntagscb
== scb
)
296 scb
->status
= STATUS_QUEUED
;
297 scb
->ha_stat
= hastat
;
299 TAILQ_INSERT_TAIL(&sc
->sc_donescb
, scb
, chain
);
304 static inline struct iha_scb
*
305 iha_pop_done_scb(struct iha_softc
*sc
)
312 scb
= TAILQ_FIRST(&sc
->sc_donescb
);
315 scb
->status
= STATUS_RENT
;
316 TAILQ_REMOVE(&sc
->sc_donescb
, scb
, chain
);
325 * iha_find_pend_scb - scan the pending queue for a SCB that can be
326 * processed immediately. Return NULL if none found
327 * and a pointer to the SCB if one is found. If there
328 * is an active SCB, return NULL!
330 static struct iha_scb
*
331 iha_find_pend_scb(struct iha_softc
*sc
)
339 if (sc
->sc_actscb
!= NULL
)
343 TAILQ_FOREACH(scb
, &sc
->sc_pendscb
, chain
) {
344 if ((scb
->xs
->xs_control
& XS_CTL_RESET
) != 0)
345 /* ALWAYS willing to reset a device */
350 if ((scb
->scb_tagmsg
) != 0) {
352 * A Tagged I/O. OK to start If no
353 * non-tagged I/O is active on the same
356 if (tcs
->ntagscb
== NULL
)
359 } else if (scb
->cmd
[0] == SCSI_REQUEST_SENSE
) {
361 * OK to do a non-tagged request sense
362 * even if a non-tagged I/O has been
363 * started, 'cuz we don't allow any
364 * disconnect during a request sense op
368 } else if (tcs
->tagcnt
== 0) {
370 * No tagged I/O active on this target,
371 * ok to start a non-tagged one if one
372 * is not already active
374 if (tcs
->ntagscb
== NULL
)
385 iha_append_pend_scb(struct iha_softc
*sc
, struct iha_scb
*scb
)
387 /* ASSUMPTION: only called within a splbio()/splx() pair */
389 if (scb
== sc
->sc_actscb
)
390 sc
->sc_actscb
= NULL
;
392 scb
->status
= STATUS_QUEUED
;
394 TAILQ_INSERT_TAIL(&sc
->sc_pendscb
, scb
, chain
);
398 iha_push_pend_scb(struct iha_softc
*sc
, struct iha_scb
*scb
)
404 if (scb
== sc
->sc_actscb
)
405 sc
->sc_actscb
= NULL
;
407 scb
->status
= STATUS_QUEUED
;
409 TAILQ_INSERT_HEAD(&sc
->sc_pendscb
, scb
, chain
);
415 * iha_del_pend_scb - remove scb from sc_pendscb
418 iha_del_pend_scb(struct iha_softc
*sc
, struct iha_scb
*scb
)
424 TAILQ_REMOVE(&sc
->sc_pendscb
, scb
, chain
);
430 iha_mark_busy_scb(struct iha_scb
*scb
)
436 scb
->status
= STATUS_BUSY
;
438 if (scb
->scb_tagmsg
== 0)
439 scb
->tcs
->ntagscb
= scb
;
447 * iha_set_ssig - read the current scsi signal mask, then write a new
448 * one which turns off/on the specified signals.
451 iha_set_ssig(struct iha_softc
*sc
, uint8_t offsigs
, uint8_t onsigs
)
453 bus_space_tag_t iot
= sc
->sc_iot
;
454 bus_space_handle_t ioh
= sc
->sc_ioh
;
457 currsigs
= bus_space_read_1(iot
, ioh
, TUL_SSIGI
);
458 bus_space_write_1(iot
, ioh
, TUL_SSIGO
, (currsigs
& ~offsigs
) | onsigs
);
462 * iha_intr - the interrupt service routine for the iha driver
468 bus_space_handle_t ioh
;
469 struct iha_softc
*sc
;
472 sc
= (struct iha_softc
*)arg
;
476 if ((bus_space_read_1(iot
, ioh
, TUL_STAT0
) & INTPD
) == 0)
479 s
= splbio(); /* XXX - Or are interrupts off when ISR's are called? */
481 if (sc
->sc_semaph
!= SEMAPH_IN_MAIN
) {
482 /* XXX - need these inside a splbio()/splx()? */
483 bus_space_write_1(iot
, ioh
, TUL_IMSK
, MASK_ALL
);
484 sc
->sc_semaph
= SEMAPH_IN_MAIN
;
488 sc
->sc_semaph
= ~SEMAPH_IN_MAIN
;
489 bus_space_write_1(iot
, ioh
, TUL_IMSK
, (MASK_ALL
& ~MSCMP
));
498 iha_attach(struct iha_softc
*sc
)
500 bus_space_tag_t iot
= sc
->sc_iot
;
501 bus_space_handle_t ioh
= sc
->sc_ioh
;
503 struct iha_eeprom eeprom
;
504 struct eeprom_adapter
*conf
;
507 iha_read_eeprom(sc
, &eeprom
);
509 conf
= &eeprom
.adapter
[0];
512 * fill in the rest of the iha_softc fields
514 sc
->sc_id
= CFG_ID(conf
->config1
);
515 sc
->sc_semaph
= ~SEMAPH_IN_MAIN
;
517 sc
->sc_actscb
= NULL
;
519 TAILQ_INIT(&sc
->sc_freescb
);
520 TAILQ_INIT(&sc
->sc_pendscb
);
521 TAILQ_INIT(&sc
->sc_donescb
);
522 error
= iha_alloc_sglist(sc
);
524 aprint_error_dev(sc
->sc_dev
, "cannot allocate sglist\n");
528 sc
->sc_scb
= malloc(sizeof(struct iha_scb
) * IHA_MAX_SCB
,
529 M_DEVBUF
, M_NOWAIT
|M_ZERO
);
530 if (sc
->sc_scb
== NULL
) {
531 aprint_error_dev(sc
->sc_dev
, "cannot allocate SCB\n");
535 for (i
= 0, scb
= sc
->sc_scb
; i
< IHA_MAX_SCB
; i
++, scb
++) {
537 scb
->sgoffset
= IHA_SG_SIZE
* i
;
538 scb
->sglist
= sc
->sc_sglist
+ IHA_MAX_SG_ENTRIES
* i
;
540 sc
->sc_dmamap
->dm_segs
[0].ds_addr
+ scb
->sgoffset
;
542 error
= bus_dmamap_create(sc
->sc_dmat
,
543 MAXPHYS
, IHA_MAX_SG_ENTRIES
, MAXPHYS
, 0,
544 BUS_DMA_NOWAIT
, &scb
->dmap
);
547 aprint_error_dev(sc
->sc_dev
,
548 "couldn't create SCB DMA map, error = %d\n",
552 TAILQ_INSERT_TAIL(&sc
->sc_freescb
, scb
, chain
);
555 /* Mask all the interrupts */
556 bus_space_write_1(iot
, ioh
, TUL_IMSK
, MASK_ALL
);
558 /* Stop any I/O and reset the scsi module */
560 bus_space_write_1(iot
, ioh
, TUL_SCTRL0
, RSMOD
);
562 /* Program HBA's SCSI ID */
563 bus_space_write_1(iot
, ioh
, TUL_SID
, sc
->sc_id
<< 4);
566 * Configure the channel as requested by the NVRAM settings read
567 * by iha_read_eeprom() above.
570 sc
->sc_sconf1
= SCONFIG0DEFAULT
;
571 if ((conf
->config1
& CFG_EN_PAR
) != 0)
572 sc
->sc_sconf1
|= SPCHK
;
573 bus_space_write_1(iot
, ioh
, TUL_SCONFIG0
, sc
->sc_sconf1
);
575 /* set selection time out 250 ms */
576 bus_space_write_1(iot
, ioh
, TUL_STIMO
, STIMO_250MS
);
578 /* Enable desired SCSI termination configuration read from eeprom */
580 if (conf
->config1
& CFG_ACT_TERM1
)
582 if (conf
->config1
& CFG_ACT_TERM2
)
584 bus_space_write_1(iot
, ioh
, TUL_DCTRL0
, reg
);
586 reg
= bus_space_read_1(iot
, ioh
, TUL_GCTRL1
) & ~ATDEN
;
587 if (conf
->config1
& CFG_AUTO_TERM
)
589 bus_space_write_1(iot
, ioh
, TUL_GCTRL1
, reg
);
591 for (i
= 0; i
< IHA_MAX_TARGETS
/ 2; i
++) {
592 sc
->sc_tcs
[i
* 2 ].flags
= EEP_LBYTE(conf
->tflags
[i
]);
593 sc
->sc_tcs
[i
* 2 + 1].flags
= EEP_HBYTE(conf
->tflags
[i
]);
594 iha_reset_tcs(&sc
->sc_tcs
[i
* 2 ], sc
->sc_sconf1
);
595 iha_reset_tcs(&sc
->sc_tcs
[i
* 2 + 1], sc
->sc_sconf1
);
599 bus_space_write_1(iot
, ioh
, TUL_SIEN
, ALL_INTERRUPTS
);
602 * fill in the adapter.
604 sc
->sc_adapter
.adapt_dev
= sc
->sc_dev
;
605 sc
->sc_adapter
.adapt_nchannels
= 1;
606 sc
->sc_adapter
.adapt_openings
= IHA_MAX_SCB
;
607 sc
->sc_adapter
.adapt_max_periph
= IHA_MAX_SCB
;
608 sc
->sc_adapter
.adapt_ioctl
= NULL
;
609 sc
->sc_adapter
.adapt_minphys
= minphys
;
610 sc
->sc_adapter
.adapt_request
= iha_scsipi_request
;
613 * fill in the channel.
615 sc
->sc_channel
.chan_adapter
= &sc
->sc_adapter
;
616 sc
->sc_channel
.chan_bustype
= &scsi_bustype
;
617 sc
->sc_channel
.chan_channel
= 0;
618 sc
->sc_channel
.chan_ntargets
= CFG_TARGET(conf
->config2
);
619 sc
->sc_channel
.chan_nluns
= 8;
620 sc
->sc_channel
.chan_id
= sc
->sc_id
;
623 * Now try to attach all the sub devices.
625 config_found(sc
->sc_dev
, &sc
->sc_channel
, scsiprint
);
629 * iha_alloc_sglist - allocate and map sglist for SCB's
632 iha_alloc_sglist(struct iha_softc
*sc
)
634 bus_dma_segment_t seg
;
638 * Allocate DMA-safe memory for the SCB's sglist
640 if ((error
= bus_dmamem_alloc(sc
->sc_dmat
,
641 IHA_SG_SIZE
* IHA_MAX_SCB
,
642 PAGE_SIZE
, 0, &seg
, 1, &rseg
, BUS_DMA_NOWAIT
)) != 0) {
643 printf(": unable to allocate sglist, error = %d\n", error
);
646 if ((error
= bus_dmamem_map(sc
->sc_dmat
, &seg
, rseg
,
647 IHA_SG_SIZE
* IHA_MAX_SCB
, (void **)&sc
->sc_sglist
,
648 BUS_DMA_NOWAIT
| BUS_DMA_COHERENT
)) != 0) {
649 printf(": unable to map sglist, error = %d\n", error
);
654 * Create and load the DMA map used for the SCBs
656 if ((error
= bus_dmamap_create(sc
->sc_dmat
,
657 IHA_SG_SIZE
* IHA_MAX_SCB
, 1, IHA_SG_SIZE
* IHA_MAX_SCB
,
658 0, BUS_DMA_NOWAIT
, &sc
->sc_dmamap
)) != 0) {
659 printf(": unable to create control DMA map, error = %d\n",
663 if ((error
= bus_dmamap_load(sc
->sc_dmat
, sc
->sc_dmamap
,
664 sc
->sc_sglist
, IHA_SG_SIZE
* IHA_MAX_SCB
,
665 NULL
, BUS_DMA_NOWAIT
)) != 0) {
666 printf(": unable to load control DMA map, error = %d\n", error
);
670 memset(sc
->sc_sglist
, 0, IHA_SG_SIZE
* IHA_MAX_SCB
);
676 iha_scsipi_request(struct scsipi_channel
*chan
, scsipi_adapter_req_t req
,
679 struct scsipi_xfer
*xs
;
680 struct scsipi_periph
*periph
;
682 struct iha_softc
*sc
;
685 sc
= device_private(chan
->chan_adapter
->adapt_dev
);
688 case ADAPTER_REQ_RUN_XFER
:
690 periph
= xs
->xs_periph
;
692 /* XXX This size isn't actually a hardware restriction. */
693 if (xs
->cmdlen
> sizeof(scb
->cmd
) ||
694 periph
->periph_target
>= IHA_MAX_TARGETS
) {
695 xs
->error
= XS_DRIVER_STUFFUP
;
701 scb
= TAILQ_FIRST(&sc
->sc_freescb
);
703 scb
->status
= STATUS_RENT
;
704 TAILQ_REMOVE(&sc
->sc_freescb
, scb
, chain
);
707 printf("unable to allocate scb\n");
709 scsipi_printaddr(periph
);
710 panic("iha_scsipi_request");
718 scb
->target
= periph
->periph_target
;
719 scb
->lun
= periph
->periph_lun
;
720 scb
->tcs
= &sc
->sc_tcs
[scb
->target
];
721 scb
->scb_id
= MSG_IDENTIFY(periph
->periph_lun
,
722 (xs
->xs_control
& XS_CTL_REQSENSE
) == 0);
725 scb
->cmdlen
= xs
->cmdlen
;
726 memcpy(&scb
->cmd
, xs
->cmd
, xs
->cmdlen
);
727 scb
->buflen
= xs
->datalen
;
729 if (xs
->xs_control
& XS_CTL_DATA_OUT
)
730 scb
->flags
|= FLAG_DATAOUT
;
731 if (xs
->xs_control
& XS_CTL_DATA_IN
)
732 scb
->flags
|= FLAG_DATAIN
;
734 if (scb
->flags
& (FLAG_DATAIN
| FLAG_DATAOUT
)) {
735 error
= bus_dmamap_load(sc
->sc_dmat
, scb
->dmap
,
736 xs
->data
, scb
->buflen
, NULL
,
737 ((xs
->xs_control
& XS_CTL_NOSLEEP
) ?
738 BUS_DMA_NOWAIT
: BUS_DMA_WAITOK
) |
740 ((scb
->flags
& FLAG_DATAIN
) ?
741 BUS_DMA_READ
: BUS_DMA_WRITE
));
744 printf("%s: error %d loading DMA map\n",
745 device_xname(sc
->sc_dev
), error
);
746 iha_append_free_scb(sc
, scb
);
747 xs
->error
= XS_DRIVER_STUFFUP
;
751 bus_dmamap_sync(sc
->sc_dmat
, scb
->dmap
,
752 0, scb
->dmap
->dm_mapsize
,
753 (scb
->flags
& FLAG_DATAIN
) ?
754 BUS_DMASYNC_PREREAD
: BUS_DMASYNC_PREWRITE
);
757 iha_exec_scb(sc
, scb
);
760 case ADAPTER_REQ_GROW_RESOURCES
:
763 case ADAPTER_REQ_SET_XFER_MODE
:
766 struct scsipi_xfer_mode
*xm
= arg
;
768 tcs
= &sc
->sc_tcs
[xm
->xm_target
];
770 if ((xm
->xm_mode
& PERIPH_CAP_WIDE16
) != 0 &&
771 (tcs
->flags
& FLAG_NO_WIDE
) == 0)
772 tcs
->flags
&= ~(FLAG_WIDE_DONE
|FLAG_SYNC_DONE
);
774 if ((xm
->xm_mode
& PERIPH_CAP_SYNC
) != 0 &&
775 (tcs
->flags
& FLAG_NO_SYNC
) == 0)
776 tcs
->flags
&= ~FLAG_SYNC_DONE
;
779 * If we're not going to negotiate, send the
780 * notification now, since it won't happen later.
782 if ((tcs
->flags
& (FLAG_WIDE_DONE
|FLAG_SYNC_DONE
)) ==
783 (FLAG_WIDE_DONE
|FLAG_SYNC_DONE
))
784 iha_update_xfer_mode(sc
, xm
->xm_target
);
792 iha_update_xfer_mode(struct iha_softc
*sc
, int target
)
794 struct tcs
*tcs
= &sc
->sc_tcs
[target
];
795 struct scsipi_xfer_mode xm
;
797 xm
.xm_target
= target
;
802 if (tcs
->syncm
& PERIOD_WIDE_SCSI
)
803 xm
.xm_mode
|= PERIPH_CAP_WIDE16
;
806 xm
.xm_mode
|= PERIPH_CAP_SYNC
;
807 xm
.xm_period
= tcs
->period
;
808 xm
.xm_offset
= tcs
->offset
;
811 scsipi_async_event(&sc
->sc_channel
, ASYNC_EVENT_XFER_MODE
, &xm
);
815 iha_reset_scsi_bus(struct iha_softc
*sc
)
825 for (i
= 0, scb
= sc
->sc_scb
; i
< IHA_MAX_SCB
; i
++, scb
++)
826 switch (scb
->status
) {
828 iha_append_done_scb(sc
, scb
, HOST_SCSI_RST
);
832 iha_push_pend_scb(sc
, scb
);
839 for (i
= 0, tcs
= sc
->sc_tcs
; i
< IHA_MAX_TARGETS
; i
++, tcs
++)
840 iha_reset_tcs(tcs
, sc
->sc_sconf1
);
846 iha_reset_chip(struct iha_softc
*sc
)
848 bus_space_tag_t iot
= sc
->sc_iot
;
849 bus_space_handle_t ioh
= sc
->sc_ioh
;
851 /* reset tulip chip */
853 bus_space_write_1(iot
, ioh
, TUL_SCTRL0
, RSCSI
);
856 sc
->sc_sistat
= bus_space_read_1(iot
, ioh
, TUL_SISTAT
);
857 } while ((sc
->sc_sistat
& SRSTD
) == 0);
859 iha_set_ssig(sc
, 0, 0);
861 /* Clear any active interrupt*/
862 (void)bus_space_read_1(iot
, ioh
, TUL_SISTAT
);
866 * iha_reset_dma - abort any active DMA xfer, reset tulip FIFO.
869 iha_reset_dma(struct iha_softc
*sc
)
871 bus_space_tag_t iot
= sc
->sc_iot
;
872 bus_space_handle_t ioh
= sc
->sc_ioh
;
874 if ((bus_space_read_1(iot
, ioh
, TUL_ISTUS1
) & XPEND
) != 0) {
875 /* if DMA xfer is pending, abort DMA xfer */
876 bus_space_write_1(iot
, ioh
, TUL_DCMD
, ABTXFR
);
877 /* wait Abort DMA xfer done */
878 while ((bus_space_read_1(iot
, ioh
, TUL_ISTUS0
) & DABT
) == 0)
882 bus_space_write_1(iot
, ioh
, TUL_SCTRL0
, RSFIFO
);
886 * iha_reset_tcs - reset the target control structure pointed
887 * to by tcs to default values. tcs flags
888 * only has the negotiation done bits reset as
889 * the other bits are fixed at initialization.
892 iha_reset_tcs(struct tcs
*tcs
, uint8_t config0
)
895 tcs
->flags
&= ~(FLAG_SYNC_DONE
| FLAG_WIDE_DONE
);
901 tcs
->sconfig0
= config0
;
905 * iha_main - process the active SCB, taking one off pending and making it
906 * active if necessary, and any done SCB's created as
907 * a result until there are no interrupts pending and no pending
908 * SCB's that can be started.
911 iha_main(struct iha_softc
*sc
)
913 bus_space_tag_t iot
= sc
->sc_iot
;
914 bus_space_handle_t ioh
=sc
->sc_ioh
;
920 while ((scb
= iha_pop_done_scb(sc
)) != NULL
)
921 iha_done_scb(sc
, scb
);
924 * If there are no interrupts pending, or we can't start
925 * a pending sc, break out of the for(;;). Otherwise
926 * continue the good work with another call to
929 if (((bus_space_read_1(iot
, ioh
, TUL_STAT0
) & INTPD
) == 0)
930 && (iha_find_pend_scb(sc
) == NULL
))
936 * iha_scsi - service any outstanding interrupts. If there are none, try to
937 * start another SCB currently in the pending queue.
940 iha_scsi(struct iha_softc
*sc
)
942 bus_space_tag_t iot
= sc
->sc_iot
;
943 bus_space_handle_t ioh
= sc
->sc_ioh
;
948 /* service pending interrupts asap */
950 stat
= bus_space_read_1(iot
, ioh
, TUL_STAT0
);
951 if ((stat
& INTPD
) != 0) {
952 sc
->sc_status0
= stat
;
953 sc
->sc_status1
= bus_space_read_1(iot
, ioh
, TUL_STAT1
);
954 sc
->sc_sistat
= bus_space_read_1(iot
, ioh
, TUL_SISTAT
);
956 sc
->sc_phase
= sc
->sc_status0
& PH_MASK
;
958 if ((sc
->sc_sistat
& SRSTD
) != 0) {
959 iha_reset_scsi_bus(sc
);
963 if ((sc
->sc_sistat
& RSELED
) != 0) {
968 if ((sc
->sc_sistat
& (STIMEO
| DISCD
)) != 0) {
973 if ((sc
->sc_sistat
& (SCMDN
| SBSRV
)) != 0) {
978 if ((sc
->sc_sistat
& SELED
) != 0)
979 iha_set_ssig(sc
, 0, 0);
983 * There were no interrupts pending which required action elsewhere, so
984 * see if it is possible to start the selection phase on a pending SCB
986 if ((scb
= iha_find_pend_scb(sc
)) == NULL
)
991 /* program HBA's SCSI ID & target SCSI ID */
992 bus_space_write_1(iot
, ioh
, TUL_SID
, (sc
->sc_id
<< 4) | scb
->target
);
994 if ((scb
->xs
->xs_control
& XS_CTL_RESET
) == 0) {
995 bus_space_write_1(iot
, ioh
, TUL_SYNCM
, tcs
->syncm
);
997 if ((tcs
->flags
& FLAG_NO_NEG_SYNC
) == 0 ||
998 (tcs
->flags
& FLAG_NO_NEG_WIDE
) == 0)
999 iha_select(sc
, scb
, SELATNSTOP
);
1001 else if (scb
->scb_tagmsg
!= 0)
1002 iha_select(sc
, scb
, SEL_ATN3
);
1005 iha_select(sc
, scb
, SEL_ATN
);
1008 iha_select(sc
, scb
, SELATNSTOP
);
1012 if ((scb
->xs
->xs_control
& XS_CTL_POLL
) != 0) {
1014 for (timeout
= scb
->xs
->timeout
; timeout
> 0; timeout
--) {
1015 if (iha_wait(sc
, NO_OP
) == -1)
1017 if (iha_next_state(sc
) == -1)
1019 delay(1000); /* Only happens in boot, so it's ok */
1023 * Since done queue processing not done until AFTER this
1024 * function returns, scb is on the done queue, not
1025 * the free queue at this point and still has valid data
1027 * Conversely, xs->error has not been set yet
1035 iha_select(struct iha_softc
*sc
, struct iha_scb
*scb
, uint8_t select_type
)
1037 bus_space_tag_t iot
= sc
->sc_iot
;
1038 bus_space_handle_t ioh
= sc
->sc_ioh
;
1040 switch (select_type
) {
1042 bus_space_write_1(iot
, ioh
, TUL_SFIFO
, scb
->scb_id
);
1043 bus_space_write_multi_1(iot
, ioh
, TUL_SFIFO
,
1044 scb
->cmd
, scb
->cmdlen
);
1054 bus_space_write_1(iot
, ioh
, TUL_SFIFO
, scb
->scb_id
);
1055 bus_space_write_1(iot
, ioh
, TUL_SFIFO
, scb
->scb_tagmsg
);
1056 bus_space_write_1(iot
, ioh
, TUL_SFIFO
, scb
->scb_tagid
);
1058 bus_space_write_multi_1(iot
, ioh
, TUL_SFIFO
, scb
->cmd
,
1065 printf("[debug] iha_select() - unknown select type = 0x%02x\n",
1070 iha_del_pend_scb(sc
, scb
);
1071 scb
->status
= STATUS_SELECT
;
1073 sc
->sc_actscb
= scb
;
1075 bus_space_write_1(iot
, ioh
, TUL_SCMD
, select_type
);
1079 * iha_wait - wait for an interrupt to service or a SCSI bus phase change
1080 * after writing the supplied command to the tulip chip. If
1081 * the command is NO_OP, skip the command writing.
1084 iha_wait(struct iha_softc
*sc
, uint8_t cmd
)
1086 bus_space_tag_t iot
= sc
->sc_iot
;
1087 bus_space_handle_t ioh
= sc
->sc_ioh
;
1090 bus_space_write_1(iot
, ioh
, TUL_SCMD
, cmd
);
1093 * Have to do this here, in addition to in iha_isr, because
1094 * interrupts might be turned off when we get here.
1097 sc
->sc_status0
= bus_space_read_1(iot
, ioh
, TUL_STAT0
);
1098 } while ((sc
->sc_status0
& INTPD
) == 0);
1100 sc
->sc_status1
= bus_space_read_1(iot
, ioh
, TUL_STAT1
);
1101 sc
->sc_sistat
= bus_space_read_1(iot
, ioh
, TUL_SISTAT
);
1103 sc
->sc_phase
= sc
->sc_status0
& PH_MASK
;
1105 if ((sc
->sc_sistat
& SRSTD
) != 0) {
1106 /* SCSI bus reset interrupt */
1107 iha_reset_scsi_bus(sc
);
1111 if ((sc
->sc_sistat
& RSELED
) != 0)
1112 /* Reselection interrupt */
1113 return (iha_resel(sc
));
1115 if ((sc
->sc_sistat
& STIMEO
) != 0) {
1116 /* selected/reselected timeout interrupt */
1121 if ((sc
->sc_sistat
& DISCD
) != 0) {
1122 /* BUS disconnection interrupt */
1123 if ((sc
->sc_flags
& FLAG_EXPECT_DONE_DISC
) != 0) {
1124 bus_space_write_1(iot
, ioh
, TUL_SCTRL0
, RSFIFO
);
1125 bus_space_write_1(iot
, ioh
, TUL_SCONFIG0
,
1127 bus_space_write_1(iot
, ioh
, TUL_SCTRL1
, EHRSL
);
1128 iha_append_done_scb(sc
, sc
->sc_actscb
, HOST_OK
);
1129 sc
->sc_flags
&= ~FLAG_EXPECT_DONE_DISC
;
1131 } else if ((sc
->sc_flags
& FLAG_EXPECT_DISC
) != 0) {
1132 bus_space_write_1(iot
, ioh
, TUL_SCTRL0
, RSFIFO
);
1133 bus_space_write_1(iot
, ioh
, TUL_SCONFIG0
,
1135 bus_space_write_1(iot
, ioh
, TUL_SCTRL1
, EHRSL
);
1136 sc
->sc_actscb
= NULL
;
1137 sc
->sc_flags
&= ~FLAG_EXPECT_DISC
;
1145 return (sc
->sc_phase
);
1149 iha_exec_scb(struct iha_softc
*sc
, struct iha_scb
*scb
)
1151 bus_space_tag_t iot
;
1152 bus_space_handle_t ioh
;
1154 struct scsipi_xfer
*xs
= scb
->xs
;
1158 nseg
= dm
->dm_nsegs
;
1161 struct iha_sg_element
*sg
= scb
->sglist
;
1164 for (i
= 0; i
< nseg
; i
++) {
1165 sg
[i
].sg_len
= htole32(dm
->dm_segs
[i
].ds_len
);
1166 sg
[i
].sg_addr
= htole32(dm
->dm_segs
[i
].ds_addr
);
1168 bus_dmamap_sync(sc
->sc_dmat
, sc
->sc_dmamap
,
1169 scb
->sgoffset
, IHA_SG_SIZE
,
1170 BUS_DMASYNC_PREWRITE
);
1172 scb
->flags
|= FLAG_SG
;
1173 scb
->sg_size
= scb
->sg_max
= nseg
;
1176 scb
->bufaddr
= scb
->sg_addr
;
1178 scb
->bufaddr
= dm
->dm_segs
[0].ds_addr
;
1180 if ((xs
->xs_control
& XS_CTL_POLL
) == 0) {
1181 int timeout
= mstohz(xs
->timeout
);
1184 callout_reset(&xs
->xs_callout
, timeout
, iha_timeout
, scb
);
1189 if (((scb
->xs
->xs_control
& XS_RESET
) != 0) ||
1190 (scb
->cmd
[0] == SCSI_REQUEST_SENSE
))
1191 iha_push_pend_scb(sc
, scb
); /* Insert SCB at head of Pend */
1193 iha_append_pend_scb(sc
, scb
); /* Append SCB to tail of Pend */
1196 * Run through iha_main() to ensure something is active, if
1197 * only this new SCB.
1199 if (sc
->sc_semaph
!= SEMAPH_IN_MAIN
) {
1203 bus_space_write_1(iot
, ioh
, TUL_IMSK
, MASK_ALL
);
1204 sc
->sc_semaph
= SEMAPH_IN_MAIN
;
1210 sc
->sc_semaph
= ~SEMAPH_IN_MAIN
;
1211 bus_space_write_1(iot
, ioh
, TUL_IMSK
, (MASK_ALL
& ~MSCMP
));
1218 * iha_done_scb - We have a scb which has been processed by the
1219 * adaptor, now we look to see how the operation went.
1222 iha_done_scb(struct iha_softc
*sc
, struct iha_scb
*scb
)
1224 struct scsipi_xfer
*xs
= scb
->xs
;
1227 /* Cancel the timeout. */
1228 callout_stop(&xs
->xs_callout
);
1230 if (scb
->flags
& (FLAG_DATAIN
| FLAG_DATAOUT
)) {
1231 bus_dmamap_sync(sc
->sc_dmat
, scb
->dmap
,
1232 0, scb
->dmap
->dm_mapsize
,
1233 (scb
->flags
& FLAG_DATAIN
) ?
1234 BUS_DMASYNC_POSTREAD
: BUS_DMASYNC_POSTWRITE
);
1235 bus_dmamap_unload(sc
->sc_dmat
, scb
->dmap
);
1238 xs
->status
= scb
->ta_stat
;
1240 switch (scb
->ha_stat
) {
1242 switch (scb
->ta_stat
) {
1244 case SCSI_CONDITION_MET
:
1246 case SCSI_INTERM_COND_MET
:
1247 xs
->resid
= scb
->buflen
;
1248 xs
->error
= XS_NOERROR
;
1249 if ((scb
->flags
& FLAG_RSENS
) != 0)
1250 xs
->error
= XS_SENSE
;
1253 case SCSI_RESV_CONFLICT
:
1255 case SCSI_QUEUE_FULL
:
1256 xs
->error
= XS_BUSY
;
1259 case SCSI_TERMINATED
:
1260 case SCSI_ACA_ACTIVE
:
1263 ~(FLAG_SYNC_DONE
| FLAG_WIDE_DONE
);
1265 if ((scb
->flags
& FLAG_RSENS
) != 0 ||
1266 iha_push_sense_request(sc
, scb
) != 0) {
1267 scb
->flags
&= ~FLAG_RSENS
;
1268 printf("%s: request sense failed\n",
1269 device_xname(sc
->sc_dev
));
1270 xs
->error
= XS_DRIVER_STUFFUP
;
1274 xs
->error
= XS_SENSE
;
1278 xs
->error
= XS_DRIVER_STUFFUP
;
1284 xs
->error
= XS_SELTIMEOUT
;
1289 xs
->error
= XS_RESET
;
1293 printf("%s: SCSI Parity error detected\n",
1294 device_xname(sc
->sc_dev
));
1295 xs
->error
= XS_DRIVER_STUFFUP
;
1298 case HOST_TIMED_OUT
:
1299 xs
->error
= XS_TIMEOUT
;
1305 xs
->error
= XS_DRIVER_STUFFUP
;
1312 iha_append_free_scb(sc
, scb
);
1316 * iha_push_sense_request - obtain auto sense data by pushing the
1317 * SCB needing it back onto the pending
1318 * queue with a REQUEST_SENSE CDB.
1321 iha_push_sense_request(struct iha_softc
*sc
, struct iha_scb
*scb
)
1323 struct scsipi_xfer
*xs
= scb
->xs
;
1324 struct scsipi_periph
*periph
= xs
->xs_periph
;
1325 struct scsi_request_sense
*ss
= (struct scsi_request_sense
*)scb
->cmd
;
1326 int lun
= periph
->periph_lun
;
1329 memset(ss
, 0, sizeof(*ss
));
1330 ss
->opcode
= SCSI_REQUEST_SENSE
;
1331 ss
->byte2
= lun
<< SCSI_CMD_LUN_SHIFT
;
1332 ss
->length
= sizeof(struct scsi_sense_data
);
1334 scb
->flags
= FLAG_RSENS
| FLAG_DATAIN
;
1336 scb
->scb_id
&= ~MSG_IDENTIFY_DISCFLAG
;
1338 scb
->scb_tagmsg
= 0;
1339 scb
->ta_stat
= SCSI_OK
;
1341 scb
->cmdlen
= sizeof(struct scsi_request_sense
);
1342 scb
->buflen
= ss
->length
;
1344 err
= bus_dmamap_load(sc
->sc_dmat
, scb
->dmap
,
1345 &xs
->sense
.scsi_sense
, scb
->buflen
, NULL
,
1346 BUS_DMA_READ
|BUS_DMA_NOWAIT
);
1348 printf("iha_push_sense_request: cannot bus_dmamap_load()\n");
1349 xs
->error
= XS_DRIVER_STUFFUP
;
1352 bus_dmamap_sync(sc
->sc_dmat
, scb
->dmap
,
1353 0, scb
->buflen
, BUS_DMASYNC_PREREAD
);
1355 /* XXX What about queued command? */
1356 iha_exec_scb(sc
, scb
);
1362 iha_timeout(void *arg
)
1364 struct iha_scb
*scb
= (struct iha_scb
*)arg
;
1365 struct scsipi_xfer
*xs
= scb
->xs
;
1366 struct scsipi_periph
*periph
;
1367 struct iha_softc
*sc
;
1370 printf("[debug] iha_timeout called with xs == NULL\n");
1374 periph
= xs
->xs_periph
;
1376 sc
= device_private(periph
->periph_channel
->chan_adapter
->adapt_dev
);
1378 scsipi_printaddr(periph
);
1379 printf("SCSI OpCode 0x%02x timed out\n", xs
->cmd
->opcode
);
1380 iha_abort_xs(sc
, xs
, HOST_TIMED_OUT
);
1384 * iha_abort_xs - find the SCB associated with the supplied xs and
1385 * stop all processing on it, moving it to the done
1386 * queue with the supplied host status value.
1389 iha_abort_xs(struct iha_softc
*sc
, struct scsipi_xfer
*xs
, uint8_t hastat
)
1391 struct iha_scb
*scb
;
1396 /* Check the pending queue for the SCB pointing to xs */
1398 TAILQ_FOREACH(scb
, &sc
->sc_pendscb
, chain
)
1399 if (scb
->xs
== xs
) {
1400 iha_del_pend_scb(sc
, scb
);
1401 iha_append_done_scb(sc
, scb
, hastat
);
1407 * If that didn't work, check all BUSY/SELECTING SCB's for one
1411 for (i
= 0, scb
= sc
->sc_scb
; i
< IHA_MAX_SCB
; i
++, scb
++)
1412 switch (scb
->status
) {
1415 if (scb
->xs
== xs
) {
1416 iha_append_done_scb(sc
, scb
, hastat
);
1429 * iha_data_over_run - return HOST_OK for all SCSI opcodes where BufLen
1430 * is an 'Allocation Length'. All other SCSI opcodes
1431 * get HOST_DO_DU as they SHOULD have xferred all the
1434 * The list of opcodes using 'Allocation Length' was
1435 * found by scanning all the SCSI-3 T10 drafts. See
1436 * www.t10.org for the curious with a .pdf reader.
1439 iha_data_over_run(struct iha_scb
*scb
)
1441 switch (scb
->cmd
[0]) {
1442 case 0x03: /* Request Sense SPC-2 */
1443 case 0x12: /* Inquiry SPC-2 */
1444 case 0x1a: /* Mode Sense (6 byte version) SPC-2 */
1445 case 0x1c: /* Receive Diagnostic Results SPC-2 */
1446 case 0x23: /* Read Format Capacities MMC-2 */
1447 case 0x29: /* Read Generation SBC */
1448 case 0x34: /* Read Position SSC-2 */
1449 case 0x37: /* Read Defect Data SBC */
1450 case 0x3c: /* Read Buffer SPC-2 */
1451 case 0x42: /* Read Sub Channel MMC-2 */
1452 case 0x43: /* Read TOC/PMA/ATIP MMC */
1454 /* XXX - 2 with same opcode of 0x44? */
1455 case 0x44: /* Read Header/Read Density Suprt MMC/SSC*/
1457 case 0x46: /* Get Configuration MMC-2 */
1458 case 0x4a: /* Get Event/Status Notification MMC-2 */
1459 case 0x4d: /* Log Sense SPC-2 */
1460 case 0x51: /* Read Disc Information MMC */
1461 case 0x52: /* Read Track Information MMC */
1462 case 0x59: /* Read Master CUE MMC */
1463 case 0x5a: /* Mode Sense (10 byte version) SPC-2 */
1464 case 0x5c: /* Read Buffer Capacity MMC */
1465 case 0x5e: /* Persistent Reserve In SPC-2 */
1466 case 0x84: /* Receive Copy Results SPC-2 */
1467 case 0xa0: /* Report LUNs SPC-2 */
1468 case 0xa3: /* Various Report requests SBC-2/SCC-2*/
1469 case 0xa4: /* Report Key MMC-2 */
1470 case 0xad: /* Read DVD Structure MMC-2 */
1471 case 0xb4: /* Read Element Status (Attached) SMC */
1472 case 0xb5: /* Request Volume Element Address SMC */
1473 case 0xb7: /* Read Defect Data (12 byte ver.) SBC */
1474 case 0xb8: /* Read Element Status (Independ.) SMC */
1475 case 0xba: /* Report Redundancy SCC-2 */
1476 case 0xbd: /* Mechanism Status MMC */
1477 case 0xbe: /* Report Basic Redundancy SCC-2 */
1482 return (HOST_DO_DU
);
1487 * iha_next_state - process the current SCB as requested in its
1491 iha_next_state(struct iha_softc
*sc
)
1494 if (sc
->sc_actscb
== NULL
)
1497 switch (sc
->sc_actscb
->nextstat
) {
1499 if (iha_state_1(sc
) == 3)
1504 switch (iha_state_2(sc
)) {
1516 if (iha_state_3(sc
) == 4)
1522 switch (iha_state_4(sc
)) {
1533 switch (iha_state_5(sc
)) {
1553 #ifdef IHA_DEBUG_STATE
1554 printf("[debug] -unknown state: %i-\n",
1555 sc
->sc_actscb
->nextstat
);
1565 * iha_state_1 - selection is complete after a SELATNSTOP. If the target
1566 * has put the bus into MSG_OUT phase start wide/sync
1567 * negotiation. Otherwise clear the FIFO and go to state 3,
1568 * which will send the SCSI CDB to the target.
1571 iha_state_1(struct iha_softc
*sc
)
1573 bus_space_tag_t iot
= sc
->sc_iot
;
1574 bus_space_handle_t ioh
= sc
->sc_ioh
;
1575 struct iha_scb
*scb
= sc
->sc_actscb
;
1579 iha_mark_busy_scb(scb
);
1583 bus_space_write_1(iot
, ioh
, TUL_SCONFIG0
, tcs
->sconfig0
);
1586 * If we are in PHASE_MSG_OUT, send
1587 * a) IDENT message (with tags if appropriate)
1588 * b) WDTR if the target is configured to negotiate wide xfers
1590 * c) SDTR if the target is configured to negotiate sync xfers
1593 * If we are NOT, then the target is not asking for anything but
1594 * the data/command, so go straight to state 3.
1596 if (sc
->sc_phase
== PHASE_MSG_OUT
) {
1597 bus_space_write_1(iot
, ioh
, TUL_SCTRL1
, (ESBUSIN
| EHRSL
));
1598 bus_space_write_1(iot
, ioh
, TUL_SFIFO
, scb
->scb_id
);
1600 if (scb
->scb_tagmsg
!= 0) {
1601 bus_space_write_1(iot
, ioh
, TUL_SFIFO
,
1603 bus_space_write_1(iot
, ioh
, TUL_SFIFO
,
1608 if ((flags
& FLAG_NO_NEG_WIDE
) == 0) {
1609 if (iha_msgout_wdtr(sc
) == -1)
1611 } else if ((flags
& FLAG_NO_NEG_SYNC
) == 0) {
1612 if (iha_msgout_sdtr(sc
) == -1)
1617 bus_space_write_1(iot
, ioh
, TUL_SCTRL0
, RSFIFO
);
1618 iha_set_ssig(sc
, REQ
| BSY
| SEL
| ATN
, 0);
1625 * iha_state_2 - selection is complete after a SEL_ATN or SEL_ATN3. If the SCSI
1626 * CDB has already been send, go to state 4 to start the data
1627 * xfer. Otherwise reset the FIFO and go to state 3, sending
1631 iha_state_2(struct iha_softc
*sc
)
1633 bus_space_tag_t iot
= sc
->sc_iot
;
1634 bus_space_handle_t ioh
= sc
->sc_ioh
;
1635 struct iha_scb
*scb
= sc
->sc_actscb
;
1637 iha_mark_busy_scb(scb
);
1639 bus_space_write_1(iot
, ioh
, TUL_SCONFIG0
, scb
->tcs
->sconfig0
);
1641 if ((sc
->sc_status1
& CPDNE
) != 0)
1644 bus_space_write_1(iot
, ioh
, TUL_SCTRL0
, RSFIFO
);
1646 iha_set_ssig(sc
, REQ
| BSY
| SEL
| ATN
, 0);
1652 * iha_state_3 - send the SCSI CDB to the target, processing any status
1653 * or other messages received until that is done or
1657 iha_state_3(struct iha_softc
*sc
)
1659 bus_space_tag_t iot
= sc
->sc_iot
;
1660 bus_space_handle_t ioh
= sc
->sc_ioh
;
1661 struct iha_scb
*scb
= sc
->sc_actscb
;
1665 switch (sc
->sc_phase
) {
1667 bus_space_write_multi_1(iot
, ioh
, TUL_SFIFO
,
1668 scb
->cmd
, scb
->cmdlen
);
1669 if (iha_wait(sc
, XF_FIFO_OUT
) == -1)
1671 else if (sc
->sc_phase
== PHASE_CMD_OUT
) {
1679 if (iha_msgin(sc
) == -1)
1683 case PHASE_STATUS_IN
:
1684 if (iha_status_msg(sc
) == -1)
1689 flags
= scb
->tcs
->flags
;
1690 if ((flags
& FLAG_NO_NEG_SYNC
) != 0) {
1691 if (iha_msgout(sc
, MSG_NOOP
) == -1)
1693 } else if (iha_msgout_sdtr(sc
) == -1)
1698 printf("[debug] -s3- bad phase = %d\n", sc
->sc_phase
);
1706 * iha_state_4 - start a data xfer. Handle any bus state
1707 * transitions until PHASE_DATA_IN/_OUT
1708 * or the attempt is abandoned. If there is
1709 * no data to xfer, go to state 6 and finish
1710 * processing the current SCB.
1713 iha_state_4(struct iha_softc
*sc
)
1715 struct iha_scb
*scb
= sc
->sc_actscb
;
1717 if ((scb
->flags
& (FLAG_DATAIN
| FLAG_DATAOUT
)) ==
1718 (FLAG_DATAIN
| FLAG_DATAOUT
))
1719 return (6); /* Both dir flags set => NO xfer was requested */
1722 if (scb
->buflen
== 0)
1725 switch (sc
->sc_phase
) {
1726 case PHASE_STATUS_IN
:
1727 if ((scb
->flags
& (FLAG_DATAIN
| FLAG_DATAOUT
)) != 0)
1728 scb
->ha_stat
= iha_data_over_run(scb
);
1729 if ((iha_status_msg(sc
)) == -1)
1735 if (iha_msgin(sc
) == -1)
1740 if ((sc
->sc_status0
& SPERR
) != 0) {
1742 scb
->ha_stat
= HOST_SPERR
;
1743 if (iha_msgout(sc
, MSG_INITIATOR_DET_ERR
) == -1)
1748 if (iha_msgout(sc
, MSG_NOOP
) == -1)
1754 return (iha_xfer_data(sc
, scb
, FLAG_DATAIN
));
1756 case PHASE_DATA_OUT
:
1757 return (iha_xfer_data(sc
, scb
, FLAG_DATAOUT
));
1767 * iha_state_5 - handle the partial or final completion of the current
1768 * data xfer. If DMA is still active stop it. If there is
1769 * more data to xfer, go to state 4 and start the xfer.
1770 * If not go to state 6 and finish the SCB.
1773 iha_state_5(struct iha_softc
*sc
)
1775 bus_space_tag_t iot
= sc
->sc_iot
;
1776 bus_space_handle_t ioh
= sc
->sc_ioh
;
1777 struct iha_scb
*scb
= sc
->sc_actscb
;
1778 struct iha_sg_element
*sg
;
1780 uint8_t period
, stat
;
1781 long xcnt
; /* cannot use unsigned!! see code: if (xcnt < 0) */
1784 cnt
= bus_space_read_4(iot
, ioh
, TUL_STCNT0
) & TCNT
;
1787 * Stop any pending DMA activity and check for parity error.
1790 if ((bus_space_read_1(iot
, ioh
, TUL_DCMD
) & XDIR
) != 0) {
1791 /* Input Operation */
1792 if ((sc
->sc_status0
& SPERR
) != 0)
1793 scb
->ha_stat
= HOST_SPERR
;
1795 if ((bus_space_read_1(iot
, ioh
, TUL_ISTUS1
) & XPEND
) != 0) {
1796 bus_space_write_1(iot
, ioh
, TUL_DCTRL0
,
1797 bus_space_read_1(iot
, ioh
, TUL_DCTRL0
) | SXSTP
);
1798 while (bus_space_read_1(iot
, ioh
, TUL_ISTUS1
) & XPEND
)
1803 /* Output Operation */
1804 if ((sc
->sc_status1
& SXCMP
) == 0) {
1805 period
= scb
->tcs
->syncm
;
1806 if ((period
& PERIOD_WIDE_SCSI
) != 0)
1807 cnt
+= (bus_space_read_1(iot
, ioh
,
1808 TUL_SFIFOCNT
) & FIFOC
) * 2;
1810 cnt
+= bus_space_read_1(iot
, ioh
,
1811 TUL_SFIFOCNT
) & FIFOC
;
1814 if ((bus_space_read_1(iot
, ioh
, TUL_ISTUS1
) & XPEND
) != 0) {
1815 bus_space_write_1(iot
, ioh
, TUL_DCMD
, ABTXFR
);
1817 stat
= bus_space_read_1(iot
, ioh
, TUL_ISTUS0
);
1818 while ((stat
& DABT
) == 0);
1821 if ((cnt
== 1) && (sc
->sc_phase
== PHASE_DATA_OUT
)) {
1822 if (iha_wait(sc
, XF_FIFO_OUT
) == -1)
1826 } else if ((sc
->sc_status1
& SXCMP
) == 0)
1827 bus_space_write_1(iot
, ioh
, TUL_SCTRL0
, RSFIFO
);
1835 /* Update active data pointer and restart the I/O at the new point */
1837 xcnt
= scb
->buflen
- cnt
; /* xcnt == bytes xferred */
1838 scb
->buflen
= cnt
; /* cnt == bytes left */
1840 if ((scb
->flags
& FLAG_SG
) != 0) {
1841 sg
= &scb
->sglist
[scb
->sg_index
];
1842 for (i
= scb
->sg_index
; i
< scb
->sg_max
; sg
++, i
++) {
1843 xcnt
-= le32toh(sg
->sg_len
);
1845 xcnt
+= le32toh(sg
->sg_len
);
1848 htole32(le32toh(sg
->sg_addr
) + xcnt
);
1850 htole32(le32toh(sg
->sg_len
) - xcnt
);
1851 bus_dmamap_sync(sc
->sc_dmat
, sc
->sc_dmamap
,
1852 scb
->sgoffset
, IHA_SG_SIZE
,
1853 BUS_DMASYNC_PREWRITE
);
1855 scb
->bufaddr
+= (i
- scb
->sg_index
) *
1856 sizeof(struct iha_sg_element
);
1857 scb
->sg_size
= scb
->sg_max
- i
;
1866 scb
->bufaddr
+= xcnt
;
1872 * iha_state_6 - finish off the active scb (may require several
1873 * iterations if PHASE_MSG_IN) and return -1 to indicate
1877 iha_state_6(struct iha_softc
*sc
)
1881 switch (sc
->sc_phase
) {
1882 case PHASE_STATUS_IN
:
1883 if (iha_status_msg(sc
) == -1)
1888 sc
->sc_actscb
->nextstat
= 6;
1889 if ((iha_msgin(sc
)) == -1)
1894 if ((iha_msgout(sc
, MSG_NOOP
)) == -1)
1899 if (iha_xpad_in(sc
) == -1)
1903 case PHASE_DATA_OUT
:
1904 if (iha_xpad_out(sc
) == -1)
1916 * iha_state_8 - reset the active device and all busy SCBs using it
1919 iha_state_8(struct iha_softc
*sc
)
1921 bus_space_tag_t iot
= sc
->sc_iot
;
1922 bus_space_handle_t ioh
= sc
->sc_ioh
;
1923 struct iha_scb
*scb
;
1927 if (sc
->sc_phase
== PHASE_MSG_OUT
) {
1928 bus_space_write_1(iot
, ioh
, TUL_SFIFO
, MSG_BUS_DEV_RESET
);
1930 scb
= sc
->sc_actscb
;
1932 /* This SCB finished correctly -- resetting the device */
1933 iha_append_done_scb(sc
, scb
, HOST_OK
);
1935 iha_reset_tcs(scb
->tcs
, sc
->sc_sconf1
);
1938 for (i
= 0, scb
= sc
->sc_scb
; i
< IHA_MAX_SCB
; i
++, scb
++)
1939 if (scb
->target
== tar
)
1940 switch (scb
->status
) {
1942 iha_append_done_scb(sc
,
1947 iha_push_pend_scb(sc
, scb
);
1954 sc
->sc_flags
|= FLAG_EXPECT_DISC
;
1956 if (iha_wait(sc
, XF_FIFO_OUT
) == -1)
1965 * iha_xfer_data - initiate the DMA xfer of the data
1968 iha_xfer_data(struct iha_softc
*sc
, struct iha_scb
*scb
, int direction
)
1970 bus_space_tag_t iot
= sc
->sc_iot
;
1971 bus_space_handle_t ioh
= sc
->sc_ioh
;
1975 if ((scb
->flags
& (FLAG_DATAIN
| FLAG_DATAOUT
)) != direction
)
1976 return (6); /* wrong direction, abandon I/O */
1978 bus_space_write_4(iot
, ioh
, TUL_STCNT0
, scb
->buflen
);
1981 if (direction
== FLAG_DATAIN
)
1984 if (scb
->flags
& FLAG_SG
) {
1985 xferlen
= scb
->sg_size
* sizeof(struct iha_sg_element
);
1988 xferlen
= scb
->buflen
;
1990 bus_space_write_4(iot
, ioh
, TUL_DXC
, xferlen
);
1991 bus_space_write_4(iot
, ioh
, TUL_DXPA
, scb
->bufaddr
);
1992 bus_space_write_1(iot
, ioh
, TUL_DCMD
, xfercmd
);
1994 bus_space_write_1(iot
, ioh
, TUL_SCMD
,
1995 (direction
== FLAG_DATAIN
) ? XF_DMA_IN
: XF_DMA_OUT
);
2003 iha_xpad_in(struct iha_softc
*sc
)
2005 bus_space_tag_t iot
= sc
->sc_iot
;
2006 bus_space_handle_t ioh
= sc
->sc_ioh
;
2007 struct iha_scb
*scb
= sc
->sc_actscb
;
2009 if ((scb
->flags
& (FLAG_DATAIN
| FLAG_DATAOUT
)) != 0)
2010 scb
->ha_stat
= HOST_DO_DU
;
2013 if ((scb
->tcs
->syncm
& PERIOD_WIDE_SCSI
) != 0)
2014 bus_space_write_4(iot
, ioh
, TUL_STCNT0
, 2);
2016 bus_space_write_4(iot
, ioh
, TUL_STCNT0
, 1);
2018 switch (iha_wait(sc
, XF_FIFO_IN
)) {
2023 (void)bus_space_read_1(iot
, ioh
, TUL_SFIFO
);
2027 bus_space_write_1(iot
, ioh
, TUL_SCTRL0
, RSFIFO
);
2034 iha_xpad_out(struct iha_softc
*sc
)
2036 bus_space_tag_t iot
= sc
->sc_iot
;
2037 bus_space_handle_t ioh
= sc
->sc_ioh
;
2038 struct iha_scb
*scb
= sc
->sc_actscb
;
2040 if ((scb
->flags
& (FLAG_DATAIN
| FLAG_DATAOUT
)) != 0)
2041 scb
->ha_stat
= HOST_DO_DU
;
2044 if ((scb
->tcs
->syncm
& PERIOD_WIDE_SCSI
) != 0)
2045 bus_space_write_4(iot
, ioh
, TUL_STCNT0
, 2);
2047 bus_space_write_4(iot
, ioh
, TUL_STCNT0
, 1);
2049 bus_space_write_1(iot
, ioh
, TUL_SFIFO
, 0);
2051 switch (iha_wait(sc
, XF_FIFO_OUT
)) {
2055 case PHASE_DATA_OUT
:
2059 /* Disable wide CPU to allow read 16 bits */
2060 bus_space_write_1(iot
, ioh
, TUL_SCTRL1
, EHRSL
);
2061 bus_space_write_1(iot
, ioh
, TUL_SCTRL0
, RSFIFO
);
2068 iha_status_msg(struct iha_softc
*sc
)
2070 bus_space_tag_t iot
= sc
->sc_iot
;
2071 bus_space_handle_t ioh
= sc
->sc_ioh
;
2072 struct iha_scb
*scb
;
2076 if ((phase
= iha_wait(sc
, CMD_COMP
)) == -1)
2079 scb
= sc
->sc_actscb
;
2081 scb
->ta_stat
= bus_space_read_1(iot
, ioh
, TUL_SFIFO
);
2083 if (phase
== PHASE_MSG_OUT
) {
2084 if ((sc
->sc_status0
& SPERR
) == 0)
2085 bus_space_write_1(iot
, ioh
, TUL_SFIFO
, MSG_NOOP
);
2087 bus_space_write_1(iot
, ioh
, TUL_SFIFO
,
2090 return (iha_wait(sc
, XF_FIFO_OUT
));
2092 } else if (phase
== PHASE_MSG_IN
) {
2093 msg
= bus_space_read_1(iot
, ioh
, TUL_SFIFO
);
2095 if ((sc
->sc_status0
& SPERR
) != 0)
2096 switch (iha_wait(sc
, MSG_ACCEPT
)) {
2100 bus_space_write_1(iot
, ioh
, TUL_SFIFO
,
2102 return (iha_wait(sc
, XF_FIFO_OUT
));
2108 if (msg
== MSG_CMDCOMPLETE
) {
2110 (SCSI_INTERM
| SCSI_BUSY
)) == SCSI_INTERM
) {
2114 sc
->sc_flags
|= FLAG_EXPECT_DONE_DISC
;
2115 bus_space_write_1(iot
, ioh
, TUL_SCTRL0
, RSFIFO
);
2116 return (iha_wait(sc
, MSG_ACCEPT
));
2119 if ((msg
== MSG_LINK_CMD_COMPLETE
)
2120 || (msg
== MSG_LINK_CMD_COMPLETEF
)) {
2122 (SCSI_INTERM
| SCSI_BUSY
)) == SCSI_INTERM
)
2123 return (iha_wait(sc
, MSG_ACCEPT
));
2132 * iha_busfree - SCSI bus free detected as a result of a TIMEOUT or
2133 * DISCONNECT interrupt. Reset the tulip FIFO and
2134 * SCONFIG0 and enable hardware reselect. Move any active
2135 * SCB to sc_donescb list. Return an appropriate host status
2136 * if an I/O was active.
2139 iha_busfree(struct iha_softc
*sc
)
2141 bus_space_tag_t iot
= sc
->sc_iot
;
2142 bus_space_handle_t ioh
= sc
->sc_ioh
;
2143 struct iha_scb
*scb
;
2145 bus_space_write_1(iot
, ioh
, TUL_SCTRL0
, RSFIFO
);
2146 bus_space_write_1(iot
, ioh
, TUL_SCONFIG0
, SCONFIG0DEFAULT
);
2147 bus_space_write_1(iot
, ioh
, TUL_SCTRL1
, EHRSL
);
2149 scb
= sc
->sc_actscb
;
2152 if (scb
->status
== STATUS_SELECT
)
2153 /* selection timeout */
2154 iha_append_done_scb(sc
, scb
, HOST_SEL_TOUT
);
2156 /* Unexpected bus free */
2157 iha_append_done_scb(sc
, scb
, HOST_BAD_PHAS
);
2162 * iha_resel - handle a detected SCSI bus reselection request.
2165 iha_resel(struct iha_softc
*sc
)
2167 bus_space_tag_t iot
= sc
->sc_iot
;
2168 bus_space_handle_t ioh
= sc
->sc_ioh
;
2169 struct iha_scb
*scb
;
2171 uint8_t tag
, target
, lun
, msg
, abortmsg
;
2173 if (sc
->sc_actscb
!= NULL
) {
2174 if ((sc
->sc_actscb
->status
== STATUS_SELECT
))
2175 iha_push_pend_scb(sc
, sc
->sc_actscb
);
2176 sc
->sc_actscb
= NULL
;
2179 target
= bus_space_read_1(iot
, ioh
, TUL_SBID
);
2180 lun
= bus_space_read_1(iot
, ioh
, TUL_SALVC
) & IHA_MSG_IDENTIFY_LUNMASK
;
2182 tcs
= &sc
->sc_tcs
[target
];
2184 bus_space_write_1(iot
, ioh
, TUL_SCONFIG0
, tcs
->sconfig0
);
2185 bus_space_write_1(iot
, ioh
, TUL_SYNCM
, tcs
->syncm
);
2187 abortmsg
= MSG_ABORT
; /* until a valid tag has been obtained */
2189 if (tcs
->ntagscb
!= NULL
)
2190 /* There is a non-tagged I/O active on the target */
2195 * Since there is no active non-tagged operation
2196 * read the tag type, the tag itself, and find
2197 * the appropriate scb by indexing sc_scb with
2201 switch (iha_wait(sc
, MSG_ACCEPT
)) {
2205 bus_space_write_4(iot
, ioh
, TUL_STCNT0
, 1);
2206 if ((iha_wait(sc
, XF_FIFO_IN
)) == -1)
2213 msg
= bus_space_read_1(iot
, ioh
, TUL_SFIFO
); /* Read Tag Msg */
2215 if ((msg
< MSG_SIMPLE_Q_TAG
) || (msg
> MSG_ORDERED_Q_TAG
))
2218 switch (iha_wait(sc
, MSG_ACCEPT
)) {
2222 bus_space_write_4(iot
, ioh
, TUL_STCNT0
, 1);
2223 if ((iha_wait(sc
, XF_FIFO_IN
)) == -1)
2230 tag
= bus_space_read_1(iot
, ioh
, TUL_SFIFO
); /* Read Tag ID */
2231 scb
= &sc
->sc_scb
[tag
];
2233 abortmsg
= MSG_ABORT_TAG
; /* Now that we have valdid tag! */
2236 if ((scb
->target
!= target
)
2237 || (scb
->lun
!= lun
)
2238 || (scb
->status
!= STATUS_BUSY
)) {
2240 iha_msgout_abort(sc
, abortmsg
);
2244 sc
->sc_actscb
= scb
;
2246 if (iha_wait(sc
, MSG_ACCEPT
) == -1)
2249 return (iha_next_state(sc
));
2253 iha_msgin(struct iha_softc
*sc
)
2255 bus_space_tag_t iot
= sc
->sc_iot
;
2256 bus_space_handle_t ioh
= sc
->sc_ioh
;
2262 if ((bus_space_read_1(iot
, ioh
, TUL_SFIFOCNT
) & FIFOC
) > 0)
2263 bus_space_write_1(iot
, ioh
, TUL_SCTRL0
, RSFIFO
);
2265 bus_space_write_4(iot
, ioh
, TUL_STCNT0
, 1);
2267 phase
= iha_wait(sc
, XF_FIFO_IN
);
2268 msg
= bus_space_read_1(iot
, ioh
, TUL_SFIFO
);
2271 case MSG_DISCONNECT
:
2272 sc
->sc_flags
|= FLAG_EXPECT_DISC
;
2273 if (iha_wait(sc
, MSG_ACCEPT
) != -1)
2277 case MSG_SAVEDATAPOINTER
:
2278 case MSG_RESTOREPOINTERS
:
2280 phase
= iha_wait(sc
, MSG_ACCEPT
);
2282 case MSG_MESSAGE_REJECT
:
2283 /* XXX - need to clear FIFO like other 'Clear ATN'?*/
2284 iha_set_ssig(sc
, REQ
| BSY
| SEL
| ATN
, 0);
2285 flags
= sc
->sc_actscb
->tcs
->flags
;
2286 if ((flags
& FLAG_NO_NEG_SYNC
) == 0)
2287 iha_set_ssig(sc
, REQ
| BSY
| SEL
, ATN
);
2288 phase
= iha_wait(sc
, MSG_ACCEPT
);
2291 phase
= iha_msgin_extended(sc
);
2293 case MSG_IGN_WIDE_RESIDUE
:
2294 phase
= iha_msgin_ignore_wid_resid(sc
);
2296 case MSG_CMDCOMPLETE
:
2297 sc
->sc_flags
|= FLAG_EXPECT_DONE_DISC
;
2298 bus_space_write_1(iot
, ioh
, TUL_SCTRL0
, RSFIFO
);
2299 phase
= iha_wait(sc
, MSG_ACCEPT
);
2306 printf("[debug] iha_msgin: bad msg type: %d\n", msg
);
2307 phase
= iha_msgout_reject(sc
);
2311 if (phase
!= PHASE_MSG_IN
)
2318 iha_msgin_extended(struct iha_softc
*sc
)
2320 bus_space_tag_t iot
= sc
->sc_iot
;
2321 bus_space_handle_t ioh
= sc
->sc_ioh
;
2322 int flags
, i
, phase
, msglen
, msgcode
;
2325 * XXX - can we just stop reading and reject, or do we have to
2326 * read all input, discarding the excess, and then reject
2328 for (i
= 0; i
< IHA_MAX_EXTENDED_MSG
; i
++) {
2329 phase
= iha_wait(sc
, MSG_ACCEPT
);
2331 if (phase
!= PHASE_MSG_IN
)
2334 bus_space_write_4(iot
, ioh
, TUL_STCNT0
, 1);
2336 if (iha_wait(sc
, XF_FIFO_IN
) == -1)
2339 sc
->sc_msg
[i
] = bus_space_read_1(iot
, ioh
, TUL_SFIFO
);
2341 if (sc
->sc_msg
[0] == i
)
2345 msglen
= sc
->sc_msg
[0];
2346 msgcode
= sc
->sc_msg
[1];
2348 if ((msglen
== MSG_EXT_SDTR_LEN
) && (msgcode
== MSG_EXT_SDTR
)) {
2349 if (iha_msgin_sdtr(sc
) == 0) {
2351 return (iha_wait(sc
, MSG_ACCEPT
));
2354 iha_set_ssig(sc
, REQ
| BSY
| SEL
, ATN
);
2356 phase
= iha_wait(sc
, MSG_ACCEPT
);
2357 if (phase
!= PHASE_MSG_OUT
)
2360 /* Clear FIFO for important message - final SYNC offer */
2361 bus_space_write_1(iot
, ioh
, TUL_SCTRL0
, RSFIFO
);
2363 iha_sync_done(sc
); /* This is our final offer */
2365 } else if ((msglen
== MSG_EXT_WDTR_LEN
) && (msgcode
== MSG_EXT_WDTR
)) {
2367 flags
= sc
->sc_actscb
->tcs
->flags
;
2369 if ((flags
& FLAG_NO_WIDE
) != 0)
2370 /* Offer 8bit xfers only */
2371 sc
->sc_msg
[2] = MSG_EXT_WDTR_BUS_8_BIT
;
2373 else if (sc
->sc_msg
[2] > MSG_EXT_WDTR_BUS_32_BIT
)
2375 return (iha_msgout_reject(sc
));
2377 else if (sc
->sc_msg
[2] == MSG_EXT_WDTR_BUS_32_BIT
)
2378 /* Offer 16bit instead */
2379 sc
->sc_msg
[2] = MSG_EXT_WDTR_BUS_16_BIT
;
2383 if ((flags
& FLAG_NO_NEG_SYNC
) == 0)
2384 iha_set_ssig(sc
, REQ
| BSY
| SEL
, ATN
);
2385 return (iha_wait(sc
, MSG_ACCEPT
));
2388 iha_set_ssig(sc
, REQ
| BSY
| SEL
, ATN
);
2390 phase
= iha_wait(sc
, MSG_ACCEPT
);
2391 if (phase
!= PHASE_MSG_OUT
)
2394 return (iha_msgout_reject(sc
));
2396 return (iha_msgout_extended(sc
));
2400 * iha_msgin_sdtr - check SDTR msg in sc_msg. If the offer is
2401 * acceptable leave sc_msg as is and return 0.
2402 * If the negotiation must continue, modify sc_msg
2403 * as needed and return 1. Else return 0.
2406 iha_msgin_sdtr(struct iha_softc
*sc
)
2410 uint8_t default_period
;
2412 flags
= sc
->sc_actscb
->tcs
->flags
;
2414 default_period
= iha_rate_tbl
[flags
& FLAG_SCSI_RATE
];
2416 if (sc
->sc_msg
[3] == 0)
2417 /* target offered async only. Accept it. */
2422 if ((flags
& FLAG_NO_SYNC
) != 0) {
2427 if (sc
->sc_msg
[3] > IHA_MAX_OFFSET
) {
2428 sc
->sc_msg
[3] = IHA_MAX_OFFSET
;
2432 if (sc
->sc_msg
[2] < default_period
) {
2433 sc
->sc_msg
[2] = default_period
;
2437 if (sc
->sc_msg
[2] > IHA_MAX_PERIOD
) {
2447 iha_msgin_ignore_wid_resid(struct iha_softc
*sc
)
2449 bus_space_tag_t iot
= sc
->sc_iot
;
2450 bus_space_handle_t ioh
= sc
->sc_ioh
;
2453 phase
= iha_wait(sc
, MSG_ACCEPT
);
2455 if (phase
== PHASE_MSG_IN
) {
2456 phase
= iha_wait(sc
, XF_FIFO_IN
);
2459 bus_space_write_1(iot
, ioh
, TUL_SFIFO
, 0);
2460 (void)bus_space_read_1(iot
, ioh
, TUL_SFIFO
);
2461 (void)bus_space_read_1(iot
, ioh
, TUL_SFIFO
);
2463 phase
= iha_wait(sc
, MSG_ACCEPT
);
2471 iha_msgout(struct iha_softc
*sc
, uint8_t msg
)
2474 bus_space_write_1(sc
->sc_iot
, sc
->sc_ioh
, TUL_SFIFO
, msg
);
2476 return (iha_wait(sc
, XF_FIFO_OUT
));
2480 iha_msgout_abort(struct iha_softc
*sc
, uint8_t aborttype
)
2483 iha_set_ssig(sc
, REQ
| BSY
| SEL
, ATN
);
2485 switch (iha_wait(sc
, MSG_ACCEPT
)) {
2490 sc
->sc_flags
|= FLAG_EXPECT_DISC
;
2491 if (iha_msgout(sc
, aborttype
) != -1)
2502 iha_msgout_reject(struct iha_softc
*sc
)
2505 iha_set_ssig(sc
, REQ
| BSY
| SEL
, ATN
);
2507 if (iha_wait(sc
, MSG_ACCEPT
) == PHASE_MSG_OUT
)
2508 return (iha_msgout(sc
, MSG_MESSAGE_REJECT
));
2514 iha_msgout_extended(struct iha_softc
*sc
)
2516 bus_space_tag_t iot
= sc
->sc_iot
;
2517 bus_space_handle_t ioh
= sc
->sc_ioh
;
2520 bus_space_write_1(iot
, ioh
, TUL_SFIFO
, MSG_EXTENDED
);
2522 bus_space_write_multi_1(iot
, ioh
, TUL_SFIFO
,
2523 sc
->sc_msg
, sc
->sc_msg
[0] + 1);
2525 phase
= iha_wait(sc
, XF_FIFO_OUT
);
2527 bus_space_write_1(iot
, ioh
, TUL_SCTRL0
, RSFIFO
);
2528 iha_set_ssig(sc
, REQ
| BSY
| SEL
| ATN
, 0);
2534 iha_msgout_wdtr(struct iha_softc
*sc
)
2537 sc
->sc_actscb
->tcs
->flags
|= FLAG_WIDE_DONE
;
2539 sc
->sc_msg
[0] = MSG_EXT_WDTR_LEN
;
2540 sc
->sc_msg
[1] = MSG_EXT_WDTR
;
2541 sc
->sc_msg
[2] = MSG_EXT_WDTR_BUS_16_BIT
;
2543 return (iha_msgout_extended(sc
));
2547 iha_msgout_sdtr(struct iha_softc
*sc
)
2549 struct tcs
*tcs
= sc
->sc_actscb
->tcs
;
2551 tcs
->flags
|= FLAG_SYNC_DONE
;
2553 sc
->sc_msg
[0] = MSG_EXT_SDTR_LEN
;
2554 sc
->sc_msg
[1] = MSG_EXT_SDTR
;
2555 sc
->sc_msg
[2] = iha_rate_tbl
[tcs
->flags
& FLAG_SCSI_RATE
];
2556 sc
->sc_msg
[3] = IHA_MAX_OFFSET
; /* REQ/ACK */
2558 return (iha_msgout_extended(sc
));
2562 iha_wide_done(struct iha_softc
*sc
)
2564 bus_space_tag_t iot
= sc
->sc_iot
;
2565 bus_space_handle_t ioh
= sc
->sc_ioh
;
2566 struct tcs
*tcs
= sc
->sc_actscb
->tcs
;
2572 if (sc
->sc_msg
[2] != 0)
2573 tcs
->syncm
|= PERIOD_WIDE_SCSI
;
2575 tcs
->sconfig0
&= ~ALTPD
;
2576 tcs
->flags
&= ~FLAG_SYNC_DONE
;
2577 tcs
->flags
|= FLAG_WIDE_DONE
;
2579 iha_update_xfer_mode(sc
, sc
->sc_actscb
->target
);
2581 bus_space_write_1(iot
, ioh
, TUL_SCONFIG0
, tcs
->sconfig0
);
2582 bus_space_write_1(iot
, ioh
, TUL_SYNCM
, tcs
->syncm
);
2586 iha_sync_done(struct iha_softc
*sc
)
2588 bus_space_tag_t iot
= sc
->sc_iot
;
2589 bus_space_handle_t ioh
= sc
->sc_ioh
;
2590 struct tcs
*tcs
= sc
->sc_actscb
->tcs
;
2593 tcs
->period
= sc
->sc_msg
[2];
2594 tcs
->offset
= sc
->sc_msg
[3];
2595 if (tcs
->offset
!= 0) {
2596 tcs
->syncm
|= tcs
->offset
;
2598 /* pick the highest possible rate */
2599 for (i
= 0; i
< sizeof(iha_rate_tbl
); i
++)
2600 if (iha_rate_tbl
[i
] >= tcs
->period
)
2603 tcs
->syncm
|= (i
<< 4);
2604 tcs
->sconfig0
|= ALTPD
;
2607 tcs
->flags
|= FLAG_SYNC_DONE
;
2609 iha_update_xfer_mode(sc
, sc
->sc_actscb
->target
);
2611 bus_space_write_1(iot
, ioh
, TUL_SCONFIG0
, tcs
->sconfig0
);
2612 bus_space_write_1(iot
, ioh
, TUL_SYNCM
, tcs
->syncm
);
2616 * iha_bad_seq - a SCSI bus phase was encountered out of the
2617 * correct/expected sequence. Reset the SCSI bus.
2620 iha_bad_seq(struct iha_softc
*sc
)
2622 struct iha_scb
*scb
= sc
->sc_actscb
;
2625 iha_append_done_scb(sc
, scb
, HOST_BAD_PHAS
);
2627 iha_reset_scsi_bus(sc
);
2632 * iha_read_eeprom - read Serial EEPROM value & set to defaults
2633 * if required. XXX - Writing does NOT work!
2636 iha_read_eeprom(struct iha_softc
*sc
, struct iha_eeprom
*eeprom
)
2638 bus_space_tag_t iot
= sc
->sc_iot
;
2639 bus_space_handle_t ioh
= sc
->sc_ioh
;
2640 uint16_t *tbuf
= (uint16_t *)eeprom
;
2643 /* Enable EEProm programming */
2644 gctrl
= bus_space_read_1(iot
, ioh
, TUL_GCTRL0
) | EEPRG
;
2645 bus_space_write_1(iot
, ioh
, TUL_GCTRL0
, gctrl
);
2648 if (iha_se2_rd_all(sc
, tbuf
) == 0)
2649 panic("%s: cannot read EEPROM", device_xname(sc
->sc_dev
));
2651 /* Disable EEProm programming */
2652 gctrl
= bus_space_read_1(iot
, ioh
, TUL_GCTRL0
) & ~EEPRG
;
2653 bus_space_write_1(iot
, ioh
, TUL_GCTRL0
, gctrl
);
2658 * iha_se2_update_all - Update SCSI H/A configuration parameters from
2659 * serial EEPROM Setup default pattern. Only
2660 * change those values different from the values
2664 iha_se2_update_all(struct iha_softc
*sc
)
2666 bus_space_tag_t iot
= sc
->sc_iot
;
2667 bus_space_handle_t ioh
= sc
->sc_ioh
;
2672 /* Enable erase/write state of EEPROM */
2673 iha_se2_instr(sc
, ENABLE_ERASE
);
2674 bus_space_write_1(iot
, ioh
, TUL_NVRAM
, 0);
2677 np
= (uint16_t *)&eeprom_default
;
2679 for (i
= 0, chksum
= 0; i
< EEPROM_SIZE
- 1; i
++) {
2680 iha_se2_wr(sc
, i
, *np
);
2684 chksum
&= 0x0000ffff;
2685 iha_se2_wr(sc
, 31, chksum
);
2687 /* Disable erase/write state of EEPROM */
2688 iha_se2_instr(sc
, 0);
2689 bus_space_write_1(iot
, ioh
, TUL_NVRAM
, 0);
2694 * iha_se2_wr - write the given 16 bit value into the Serial EEPROM
2695 * at the specified offset
2698 iha_se2_wr(struct iha_softc
*sc
, int addr
, uint16_t writeword
)
2700 bus_space_tag_t iot
= sc
->sc_iot
;
2701 bus_space_handle_t ioh
= sc
->sc_ioh
;
2704 /* send 'WRITE' Instruction == address | WRITE bit */
2705 iha_se2_instr(sc
, addr
| WRITE
);
2707 for (i
= 16; i
> 0; i
--) {
2708 if (writeword
& (1 << (i
- 1)))
2709 bus_space_write_1(iot
, ioh
, TUL_NVRAM
, NVRCS
| NVRDO
);
2711 bus_space_write_1(iot
, ioh
, TUL_NVRAM
, NVRCS
);
2713 bus_space_write_1(iot
, ioh
, TUL_NVRAM
, NVRCS
| NVRCK
);
2717 bus_space_write_1(iot
, ioh
, TUL_NVRAM
, NVRCS
);
2719 bus_space_write_1(iot
, ioh
, TUL_NVRAM
, 0);
2721 bus_space_write_1(iot
, ioh
, TUL_NVRAM
, NVRCS
);
2725 bus_space_write_1(iot
, ioh
, TUL_NVRAM
, NVRCS
| NVRCK
);
2727 bus_space_write_1(iot
, ioh
, TUL_NVRAM
, NVRCS
);
2729 bit
= bus_space_read_1(iot
, ioh
, TUL_NVRAM
) & NVRDI
;
2732 break; /* write complete */
2735 bus_space_write_1(iot
, ioh
, TUL_NVRAM
, 0);
2740 * iha_se2_rd - read & return the 16 bit value at the specified
2741 * offset in the Serial E2PROM
2745 iha_se2_rd(struct iha_softc
*sc
, int addr
)
2747 bus_space_tag_t iot
= sc
->sc_iot
;
2748 bus_space_handle_t ioh
= sc
->sc_ioh
;
2752 /* Send 'READ' instruction == address | READ bit */
2753 iha_se2_instr(sc
, addr
| READ
);
2756 for (i
= 16; i
> 0; i
--) {
2757 bus_space_write_1(iot
, ioh
, TUL_NVRAM
, NVRCS
| NVRCK
);
2759 bus_space_write_1(iot
, ioh
, TUL_NVRAM
, NVRCS
);
2761 /* sample data after the following edge of clock */
2762 bit
= bus_space_read_1(iot
, ioh
, TUL_NVRAM
) & NVRDI
? 1 : 0;
2765 readword
|= bit
<< (i
- 1);
2768 bus_space_write_1(iot
, ioh
, TUL_NVRAM
, 0);
2774 * iha_se2_rd_all - Read SCSI H/A config parameters from serial EEPROM
2777 iha_se2_rd_all(struct iha_softc
*sc
, uint16_t *tbuf
)
2779 struct iha_eeprom
*eeprom
= (struct iha_eeprom
*)tbuf
;
2783 for (i
= 0, chksum
= 0; i
< EEPROM_SIZE
- 1; i
++) {
2784 *tbuf
= iha_se2_rd(sc
, i
);
2787 *tbuf
= iha_se2_rd(sc
, 31); /* read checksum from EEPROM */
2789 chksum
&= 0x0000ffff; /* lower 16 bits */
2791 return (eeprom
->signature
== EEP_SIGNATURE
) &&
2792 (eeprom
->checksum
== chksum
);
2796 * iha_se2_instr - write an octet to serial E2PROM one bit at a time
2799 iha_se2_instr(struct iha_softc
*sc
, int instr
)
2801 bus_space_tag_t iot
= sc
->sc_iot
;
2802 bus_space_handle_t ioh
= sc
->sc_ioh
;
2805 b
= NVRCS
| NVRDO
; /* Write the start bit (== 1) */
2807 bus_space_write_1(iot
, ioh
, TUL_NVRAM
, b
);
2809 bus_space_write_1(iot
, ioh
, TUL_NVRAM
, b
| NVRCK
);
2812 for (i
= 8; i
> 0; i
--) {
2813 if (instr
& (1 << (i
- 1)))
2814 b
= NVRCS
| NVRDO
; /* Write a 1 bit */
2816 b
= NVRCS
; /* Write a 0 bit */
2818 bus_space_write_1(iot
, ioh
, TUL_NVRAM
, b
);
2820 bus_space_write_1(iot
, ioh
, TUL_NVRAM
, b
| NVRCK
);
2824 bus_space_write_1(iot
, ioh
, TUL_NVRAM
, NVRCS
);