iha.c revision 1.3.2.10 1 1.3.2.10 nathanw /* $NetBSD: iha.c,v 1.3.2.10 2002/10/18 02:41:53 nathanw Exp $ */
2 1.3.2.2 nathanw /*
3 1.3.2.2 nathanw * Initio INI-9xxxU/UW SCSI Device Driver
4 1.3.2.2 nathanw *
5 1.3.2.2 nathanw * Copyright (c) 2000 Ken Westerback
6 1.3.2.2 nathanw * All rights reserved.
7 1.3.2.2 nathanw *
8 1.3.2.2 nathanw * Redistribution and use in source and binary forms, with or without
9 1.3.2.2 nathanw * modification, are permitted provided that the following conditions
10 1.3.2.2 nathanw * are met:
11 1.3.2.2 nathanw * 1. Redistributions of source code must retain the above copyright
12 1.3.2.2 nathanw * notice, this list of conditions and the following disclaimer,
13 1.3.2.2 nathanw * without modification, immediately at the beginning of the file.
14 1.3.2.2 nathanw * 2. The name of the author may not be used to endorse or promote products
15 1.3.2.2 nathanw * derived from this software without specific prior written permission.
16 1.3.2.2 nathanw *
17 1.3.2.2 nathanw * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 1.3.2.2 nathanw * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 1.3.2.2 nathanw * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 1.3.2.2 nathanw * IN NO EVENT SHALL THE AUTHOR OR HIS RELATIVES BE LIABLE FOR ANY DIRECT,
21 1.3.2.2 nathanw * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 1.3.2.2 nathanw * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
23 1.3.2.2 nathanw * SERVICES; LOSS OF MIND, USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 1.3.2.2 nathanw * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
25 1.3.2.2 nathanw * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
26 1.3.2.2 nathanw * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
27 1.3.2.2 nathanw * THE POSSIBILITY OF SUCH DAMAGE.
28 1.3.2.2 nathanw *
29 1.3.2.2 nathanw *-------------------------------------------------------------------------
30 1.3.2.2 nathanw *
31 1.3.2.2 nathanw * Ported from i91u.c, provided by Initio Corporation, which credits:
32 1.3.2.2 nathanw *
33 1.3.2.3 nathanw * Device driver for the INI-9XXXU/UW or INIC-940/950 PCI SCSI Controller.
34 1.3.2.2 nathanw *
35 1.3.2.2 nathanw * FreeBSD
36 1.3.2.2 nathanw *
37 1.3.2.2 nathanw * Written for 386bsd and FreeBSD by
38 1.3.2.2 nathanw * Winston Hung <winstonh (at) initio.com>
39 1.3.2.2 nathanw *
40 1.3.2.2 nathanw * Copyright (c) 1997-99 Initio Corp. All rights reserved.
41 1.3.2.2 nathanw *
42 1.3.2.2 nathanw *-------------------------------------------------------------------------
43 1.3.2.2 nathanw */
44 1.3.2.2 nathanw
45 1.3.2.2 nathanw /*
46 1.3.2.2 nathanw * Ported to NetBSD by Izumi Tsutsui <tsutsui (at) ceres.dti.ne.jp> from OpenBSD:
47 1.3.2.2 nathanw * $OpenBSD: iha.c,v 1.3 2001/02/20 00:47:33 krw Exp $
48 1.3.2.2 nathanw */
49 1.3.2.2 nathanw
50 1.3.2.5 nathanw #include <sys/cdefs.h>
51 1.3.2.10 nathanw __KERNEL_RCSID(0, "$NetBSD: iha.c,v 1.3.2.10 2002/10/18 02:41:53 nathanw Exp $");
52 1.3.2.5 nathanw
53 1.3.2.2 nathanw #include <sys/param.h>
54 1.3.2.2 nathanw #include <sys/systm.h>
55 1.3.2.2 nathanw #include <sys/kernel.h>
56 1.3.2.2 nathanw #include <sys/buf.h>
57 1.3.2.2 nathanw #include <sys/device.h>
58 1.3.2.2 nathanw #include <sys/malloc.h>
59 1.3.2.2 nathanw
60 1.3.2.2 nathanw #include <uvm/uvm_extern.h>
61 1.3.2.2 nathanw
62 1.3.2.2 nathanw #include <machine/bus.h>
63 1.3.2.2 nathanw #include <machine/intr.h>
64 1.3.2.2 nathanw
65 1.3.2.2 nathanw #include <dev/scsipi/scsi_all.h>
66 1.3.2.2 nathanw #include <dev/scsipi/scsipi_all.h>
67 1.3.2.2 nathanw #include <dev/scsipi/scsiconf.h>
68 1.3.2.2 nathanw #include <dev/scsipi/scsi_message.h>
69 1.3.2.2 nathanw
70 1.3.2.2 nathanw #include <dev/ic/ihareg.h>
71 1.3.2.2 nathanw #include <dev/ic/ihavar.h>
72 1.3.2.2 nathanw
73 1.3.2.2 nathanw /*
74 1.3.2.2 nathanw * SCSI Rate Table, indexed by FLAG_SCSI_RATE field of
75 1.3.2.2 nathanw * tcs flags.
76 1.3.2.2 nathanw */
77 1.3.2.6 nathanw static const u_int8_t iha_rate_tbl[] = {
78 1.3.2.2 nathanw /* fast 20 */
79 1.3.2.2 nathanw /* nanosecond divide by 4 */
80 1.3.2.2 nathanw 12, /* 50ns, 20M */
81 1.3.2.2 nathanw 18, /* 75ns, 13.3M */
82 1.3.2.2 nathanw 25, /* 100ns, 10M */
83 1.3.2.2 nathanw 31, /* 125ns, 8M */
84 1.3.2.2 nathanw 37, /* 150ns, 6.6M */
85 1.3.2.2 nathanw 43, /* 175ns, 5.7M */
86 1.3.2.2 nathanw 50, /* 200ns, 5M */
87 1.3.2.2 nathanw 62 /* 250ns, 4M */
88 1.3.2.2 nathanw };
89 1.3.2.6 nathanw #define IHA_MAX_PERIOD 62
90 1.3.2.2 nathanw
91 1.3.2.3 nathanw #ifdef notused
92 1.3.2.2 nathanw static u_int16_t eeprom_default[EEPROM_SIZE] = {
93 1.3.2.2 nathanw /* -- Header ------------------------------------ */
94 1.3.2.2 nathanw /* signature */
95 1.3.2.2 nathanw EEP_SIGNATURE,
96 1.3.2.2 nathanw /* size, revision */
97 1.3.2.2 nathanw EEP_WORD(EEPROM_SIZE * 2, 0x01),
98 1.3.2.2 nathanw /* -- Host Adapter Structure -------------------- */
99 1.3.2.2 nathanw /* model */
100 1.3.2.2 nathanw 0x0095,
101 1.3.2.2 nathanw /* model info, number of channel */
102 1.3.2.2 nathanw EEP_WORD(0x00, 1),
103 1.3.2.2 nathanw /* BIOS config */
104 1.3.2.2 nathanw EEP_BIOSCFG_DEFAULT,
105 1.3.2.2 nathanw /* host adapter config */
106 1.3.2.2 nathanw 0,
107 1.3.2.2 nathanw
108 1.3.2.2 nathanw /* -- eeprom_adapter[0] ------------------------------- */
109 1.3.2.2 nathanw /* ID, adapter config 1 */
110 1.3.2.2 nathanw EEP_WORD(7, CFG_DEFAULT),
111 1.3.2.2 nathanw /* adapter config 2, number of targets */
112 1.3.2.2 nathanw EEP_WORD(0x00, 8),
113 1.3.2.2 nathanw /* target flags */
114 1.3.2.2 nathanw EEP_WORD(FLAG_DEFAULT, FLAG_DEFAULT),
115 1.3.2.2 nathanw EEP_WORD(FLAG_DEFAULT, FLAG_DEFAULT),
116 1.3.2.2 nathanw EEP_WORD(FLAG_DEFAULT, FLAG_DEFAULT),
117 1.3.2.2 nathanw EEP_WORD(FLAG_DEFAULT, FLAG_DEFAULT),
118 1.3.2.2 nathanw EEP_WORD(FLAG_DEFAULT, FLAG_DEFAULT),
119 1.3.2.2 nathanw EEP_WORD(FLAG_DEFAULT, FLAG_DEFAULT),
120 1.3.2.2 nathanw EEP_WORD(FLAG_DEFAULT, FLAG_DEFAULT),
121 1.3.2.2 nathanw EEP_WORD(FLAG_DEFAULT, FLAG_DEFAULT),
122 1.3.2.2 nathanw
123 1.3.2.2 nathanw /* -- eeprom_adapter[1] ------------------------------- */
124 1.3.2.2 nathanw /* ID, adapter config 1 */
125 1.3.2.2 nathanw EEP_WORD(7, CFG_DEFAULT),
126 1.3.2.2 nathanw /* adapter config 2, number of targets */
127 1.3.2.2 nathanw EEP_WORD(0x00, 8),
128 1.3.2.2 nathanw /* target flags */
129 1.3.2.2 nathanw EEP_WORD(FLAG_DEFAULT, FLAG_DEFAULT),
130 1.3.2.2 nathanw EEP_WORD(FLAG_DEFAULT, FLAG_DEFAULT),
131 1.3.2.2 nathanw EEP_WORD(FLAG_DEFAULT, FLAG_DEFAULT),
132 1.3.2.2 nathanw EEP_WORD(FLAG_DEFAULT, FLAG_DEFAULT),
133 1.3.2.2 nathanw EEP_WORD(FLAG_DEFAULT, FLAG_DEFAULT),
134 1.3.2.2 nathanw EEP_WORD(FLAG_DEFAULT, FLAG_DEFAULT),
135 1.3.2.2 nathanw EEP_WORD(FLAG_DEFAULT, FLAG_DEFAULT),
136 1.3.2.2 nathanw EEP_WORD(FLAG_DEFAULT, FLAG_DEFAULT),
137 1.3.2.2 nathanw /* reserved[5] */
138 1.3.2.2 nathanw 0, 0, 0, 0, 0,
139 1.3.2.2 nathanw /* checksum */
140 1.3.2.2 nathanw 0
141 1.3.2.2 nathanw };
142 1.3.2.3 nathanw #endif
143 1.3.2.2 nathanw
144 1.3.2.7 nathanw static void iha_append_free_scb(struct iha_softc *, struct iha_scb *);
145 1.3.2.7 nathanw static void iha_append_done_scb(struct iha_softc *, struct iha_scb *, u_int8_t);
146 1.3.2.7 nathanw static __inline struct iha_scb *iha_pop_done_scb(struct iha_softc *);
147 1.3.2.7 nathanw
148 1.3.2.7 nathanw static struct iha_scb *iha_find_pend_scb(struct iha_softc *);
149 1.3.2.7 nathanw static __inline void iha_append_pend_scb(struct iha_softc *, struct iha_scb *);
150 1.3.2.7 nathanw static __inline void iha_push_pend_scb(struct iha_softc *, struct iha_scb *);
151 1.3.2.7 nathanw static __inline void iha_del_pend_scb(struct iha_softc *, struct iha_scb *);
152 1.3.2.7 nathanw static __inline void iha_mark_busy_scb(struct iha_scb *);
153 1.3.2.7 nathanw
154 1.3.2.7 nathanw static __inline void iha_set_ssig(struct iha_softc *, u_int8_t, u_int8_t);
155 1.3.2.2 nathanw
156 1.3.2.3 nathanw static int iha_alloc_sglist(struct iha_softc *);
157 1.3.2.3 nathanw
158 1.3.2.7 nathanw static void iha_scsipi_request(struct scsipi_channel *, scsipi_adapter_req_t,
159 1.3.2.8 nathanw void *);
160 1.3.2.7 nathanw static void iha_update_xfer_mode(struct iha_softc *, int);
161 1.3.2.2 nathanw
162 1.3.2.3 nathanw static void iha_reset_scsi_bus(struct iha_softc *);
163 1.3.2.3 nathanw static void iha_reset_chip(struct iha_softc *);
164 1.3.2.3 nathanw static void iha_reset_dma(struct iha_softc *);
165 1.3.2.3 nathanw static void iha_reset_tcs(struct tcs *, u_int8_t);
166 1.3.2.2 nathanw
167 1.3.2.3 nathanw static void iha_main(struct iha_softc *);
168 1.3.2.3 nathanw static void iha_scsi(struct iha_softc *);
169 1.3.2.7 nathanw static void iha_select(struct iha_softc *, struct iha_scb *, u_int8_t);
170 1.3.2.7 nathanw static int iha_wait(struct iha_softc *, u_int8_t);
171 1.3.2.2 nathanw
172 1.3.2.7 nathanw static void iha_exec_scb(struct iha_softc *, struct iha_scb *);
173 1.3.2.7 nathanw static void iha_done_scb(struct iha_softc *, struct iha_scb *);
174 1.3.2.7 nathanw static int iha_push_sense_request(struct iha_softc *, struct iha_scb *);
175 1.3.2.2 nathanw
176 1.3.2.7 nathanw static void iha_timeout(void *);
177 1.3.2.7 nathanw static void iha_abort_xs(struct iha_softc *, struct scsipi_xfer *, u_int8_t);
178 1.3.2.7 nathanw static u_int8_t iha_data_over_run(struct iha_scb *);
179 1.3.2.3 nathanw
180 1.3.2.3 nathanw static int iha_next_state(struct iha_softc *);
181 1.3.2.3 nathanw static int iha_state_1(struct iha_softc *);
182 1.3.2.3 nathanw static int iha_state_2(struct iha_softc *);
183 1.3.2.3 nathanw static int iha_state_3(struct iha_softc *);
184 1.3.2.3 nathanw static int iha_state_4(struct iha_softc *);
185 1.3.2.3 nathanw static int iha_state_5(struct iha_softc *);
186 1.3.2.3 nathanw static int iha_state_6(struct iha_softc *);
187 1.3.2.3 nathanw static int iha_state_8(struct iha_softc *);
188 1.3.2.2 nathanw
189 1.3.2.7 nathanw static int iha_xfer_data(struct iha_softc *, struct iha_scb *, int);
190 1.3.2.3 nathanw static int iha_xpad_in(struct iha_softc *);
191 1.3.2.3 nathanw static int iha_xpad_out(struct iha_softc *);
192 1.3.2.2 nathanw
193 1.3.2.3 nathanw static int iha_status_msg(struct iha_softc *);
194 1.3.2.7 nathanw static void iha_busfree(struct iha_softc *);
195 1.3.2.7 nathanw static int iha_resel(struct iha_softc *);
196 1.3.2.2 nathanw
197 1.3.2.3 nathanw static int iha_msgin(struct iha_softc *);
198 1.3.2.3 nathanw static int iha_msgin_extended(struct iha_softc *);
199 1.3.2.7 nathanw static int iha_msgin_sdtr(struct iha_softc *);
200 1.3.2.3 nathanw static int iha_msgin_ignore_wid_resid(struct iha_softc *);
201 1.3.2.3 nathanw
202 1.3.2.3 nathanw static int iha_msgout(struct iha_softc *, u_int8_t);
203 1.3.2.3 nathanw static void iha_msgout_abort(struct iha_softc *, u_int8_t);
204 1.3.2.3 nathanw static int iha_msgout_reject(struct iha_softc *);
205 1.3.2.7 nathanw static int iha_msgout_extended(struct iha_softc *);
206 1.3.2.3 nathanw static int iha_msgout_wdtr(struct iha_softc *);
207 1.3.2.7 nathanw static int iha_msgout_sdtr(struct iha_softc *);
208 1.3.2.2 nathanw
209 1.3.2.7 nathanw static void iha_wide_done(struct iha_softc *);
210 1.3.2.7 nathanw static void iha_sync_done(struct iha_softc *);
211 1.3.2.2 nathanw
212 1.3.2.7 nathanw static void iha_bad_seq(struct iha_softc *);
213 1.3.2.2 nathanw
214 1.3.2.7 nathanw static void iha_read_eeprom(struct iha_softc *, struct iha_eeprom *);
215 1.3.2.7 nathanw static int iha_se2_rd_all(struct iha_softc *, u_int16_t *);
216 1.3.2.7 nathanw static void iha_se2_instr(struct iha_softc *, int);
217 1.3.2.7 nathanw static u_int16_t iha_se2_rd(struct iha_softc *, int);
218 1.3.2.7 nathanw #ifdef notused
219 1.3.2.7 nathanw static void iha_se2_update_all(struct iha_softc *);
220 1.3.2.7 nathanw static void iha_se2_wr(struct iha_softc *, int, u_int16_t);
221 1.3.2.7 nathanw #endif
222 1.3.2.2 nathanw
223 1.3.2.2 nathanw /*
224 1.3.2.7 nathanw * iha_append_free_scb - append the supplied SCB to the tail of the
225 1.3.2.7 nathanw * sc_freescb queue after clearing and resetting
226 1.3.2.7 nathanw * everything possible.
227 1.3.2.2 nathanw */
228 1.3.2.7 nathanw static void
229 1.3.2.7 nathanw iha_append_free_scb(sc, scb)
230 1.3.2.2 nathanw struct iha_softc *sc;
231 1.3.2.7 nathanw struct iha_scb *scb;
232 1.3.2.7 nathanw {
233 1.3.2.2 nathanw int s;
234 1.3.2.2 nathanw
235 1.3.2.7 nathanw s = splbio();
236 1.3.2.2 nathanw
237 1.3.2.7 nathanw if (scb == sc->sc_actscb)
238 1.3.2.7 nathanw sc->sc_actscb = NULL;
239 1.3.2.2 nathanw
240 1.3.2.7 nathanw scb->status = STATUS_QUEUED;
241 1.3.2.7 nathanw scb->ha_stat = HOST_OK;
242 1.3.2.7 nathanw scb->ta_stat = SCSI_OK;
243 1.3.2.2 nathanw
244 1.3.2.7 nathanw scb->nextstat = 0;
245 1.3.2.7 nathanw scb->scb_tagmsg = 0;
246 1.3.2.2 nathanw
247 1.3.2.7 nathanw scb->xs = NULL;
248 1.3.2.7 nathanw scb->tcs = NULL;
249 1.3.2.2 nathanw
250 1.3.2.7 nathanw /*
251 1.3.2.7 nathanw * scb_tagid, sg_addr, sglist
252 1.3.2.7 nathanw * SCB_SensePtr are set at initialization
253 1.3.2.7 nathanw * and never change
254 1.3.2.7 nathanw */
255 1.3.2.2 nathanw
256 1.3.2.7 nathanw TAILQ_INSERT_TAIL(&sc->sc_freescb, scb, chain);
257 1.3.2.2 nathanw
258 1.3.2.7 nathanw splx(s);
259 1.3.2.2 nathanw }
260 1.3.2.2 nathanw
261 1.3.2.7 nathanw static void
262 1.3.2.7 nathanw iha_append_done_scb(sc, scb, hastat)
263 1.3.2.2 nathanw struct iha_softc *sc;
264 1.3.2.7 nathanw struct iha_scb *scb;
265 1.3.2.7 nathanw u_int8_t hastat;
266 1.3.2.7 nathanw {
267 1.3.2.7 nathanw struct tcs *tcs;
268 1.3.2.7 nathanw int s;
269 1.3.2.2 nathanw
270 1.3.2.7 nathanw s = splbio();
271 1.3.2.2 nathanw
272 1.3.2.7 nathanw if (scb->xs != NULL)
273 1.3.2.7 nathanw callout_stop(&scb->xs->xs_callout);
274 1.3.2.2 nathanw
275 1.3.2.7 nathanw if (scb == sc->sc_actscb)
276 1.3.2.7 nathanw sc->sc_actscb = NULL;
277 1.3.2.2 nathanw
278 1.3.2.7 nathanw tcs = scb->tcs;
279 1.3.2.2 nathanw
280 1.3.2.7 nathanw if (scb->scb_tagmsg != 0) {
281 1.3.2.7 nathanw if (tcs->tagcnt)
282 1.3.2.7 nathanw tcs->tagcnt--;
283 1.3.2.7 nathanw } else if (tcs->ntagscb == scb)
284 1.3.2.7 nathanw tcs->ntagscb = NULL;
285 1.3.2.2 nathanw
286 1.3.2.7 nathanw scb->status = STATUS_QUEUED;
287 1.3.2.7 nathanw scb->ha_stat = hastat;
288 1.3.2.2 nathanw
289 1.3.2.7 nathanw TAILQ_INSERT_TAIL(&sc->sc_donescb, scb, chain);
290 1.3.2.2 nathanw
291 1.3.2.7 nathanw splx(s);
292 1.3.2.7 nathanw }
293 1.3.2.2 nathanw
294 1.3.2.7 nathanw static __inline struct iha_scb *
295 1.3.2.7 nathanw iha_pop_done_scb(sc)
296 1.3.2.7 nathanw struct iha_softc *sc;
297 1.3.2.7 nathanw {
298 1.3.2.7 nathanw struct iha_scb *scb;
299 1.3.2.7 nathanw int s;
300 1.3.2.6 nathanw
301 1.3.2.7 nathanw s = splbio();
302 1.3.2.6 nathanw
303 1.3.2.7 nathanw scb = TAILQ_FIRST(&sc->sc_donescb);
304 1.3.2.6 nathanw
305 1.3.2.7 nathanw if (scb != NULL) {
306 1.3.2.7 nathanw scb->status = STATUS_RENT;
307 1.3.2.7 nathanw TAILQ_REMOVE(&sc->sc_donescb, scb, chain);
308 1.3.2.7 nathanw }
309 1.3.2.6 nathanw
310 1.3.2.7 nathanw splx(s);
311 1.3.2.6 nathanw
312 1.3.2.7 nathanw return (scb);
313 1.3.2.2 nathanw }
314 1.3.2.2 nathanw
315 1.3.2.7 nathanw /*
316 1.3.2.7 nathanw * iha_find_pend_scb - scan the pending queue for a SCB that can be
317 1.3.2.7 nathanw * processed immediately. Return NULL if none found
318 1.3.2.7 nathanw * and a pointer to the SCB if one is found. If there
319 1.3.2.7 nathanw * is an active SCB, return NULL!
320 1.3.2.7 nathanw */
321 1.3.2.7 nathanw static struct iha_scb *
322 1.3.2.7 nathanw iha_find_pend_scb(sc)
323 1.3.2.2 nathanw struct iha_softc *sc;
324 1.3.2.2 nathanw {
325 1.3.2.7 nathanw struct iha_scb *scb;
326 1.3.2.7 nathanw struct tcs *tcs;
327 1.3.2.7 nathanw int s;
328 1.3.2.2 nathanw
329 1.3.2.7 nathanw s = splbio();
330 1.3.2.2 nathanw
331 1.3.2.7 nathanw if (sc->sc_actscb != NULL)
332 1.3.2.7 nathanw scb = NULL;
333 1.3.2.2 nathanw
334 1.3.2.7 nathanw else
335 1.3.2.7 nathanw TAILQ_FOREACH(scb, &sc->sc_pendscb, chain) {
336 1.3.2.7 nathanw if ((scb->xs->xs_control & XS_CTL_RESET) != 0)
337 1.3.2.7 nathanw /* ALWAYS willing to reset a device */
338 1.3.2.7 nathanw break;
339 1.3.2.2 nathanw
340 1.3.2.7 nathanw tcs = scb->tcs;
341 1.3.2.7 nathanw
342 1.3.2.7 nathanw if ((scb->scb_tagmsg) != 0) {
343 1.3.2.7 nathanw /*
344 1.3.2.7 nathanw * A Tagged I/O. OK to start If no
345 1.3.2.7 nathanw * non-tagged I/O is active on the same
346 1.3.2.7 nathanw * target
347 1.3.2.7 nathanw */
348 1.3.2.7 nathanw if (tcs->ntagscb == NULL)
349 1.3.2.7 nathanw break;
350 1.3.2.7 nathanw
351 1.3.2.7 nathanw } else if (scb->cmd[0] == REQUEST_SENSE) {
352 1.3.2.7 nathanw /*
353 1.3.2.7 nathanw * OK to do a non-tagged request sense
354 1.3.2.7 nathanw * even if a non-tagged I/O has been
355 1.3.2.7 nathanw * started, 'cuz we don't allow any
356 1.3.2.7 nathanw * disconnect during a request sense op
357 1.3.2.7 nathanw */
358 1.3.2.7 nathanw break;
359 1.3.2.7 nathanw
360 1.3.2.7 nathanw } else if (tcs->tagcnt == 0) {
361 1.3.2.7 nathanw /*
362 1.3.2.7 nathanw * No tagged I/O active on this target,
363 1.3.2.7 nathanw * ok to start a non-tagged one if one
364 1.3.2.7 nathanw * is not already active
365 1.3.2.7 nathanw */
366 1.3.2.7 nathanw if (tcs->ntagscb == NULL)
367 1.3.2.7 nathanw break;
368 1.3.2.7 nathanw }
369 1.3.2.7 nathanw }
370 1.3.2.7 nathanw
371 1.3.2.7 nathanw splx(s);
372 1.3.2.7 nathanw
373 1.3.2.7 nathanw return (scb);
374 1.3.2.7 nathanw }
375 1.3.2.7 nathanw
376 1.3.2.7 nathanw static __inline void
377 1.3.2.7 nathanw iha_append_pend_scb(sc, scb)
378 1.3.2.7 nathanw struct iha_softc *sc;
379 1.3.2.7 nathanw struct iha_scb *scb;
380 1.3.2.7 nathanw {
381 1.3.2.7 nathanw /* ASSUMPTION: only called within a splbio()/splx() pair */
382 1.3.2.7 nathanw
383 1.3.2.7 nathanw if (scb == sc->sc_actscb)
384 1.3.2.7 nathanw sc->sc_actscb = NULL;
385 1.3.2.7 nathanw
386 1.3.2.7 nathanw scb->status = STATUS_QUEUED;
387 1.3.2.7 nathanw
388 1.3.2.7 nathanw TAILQ_INSERT_TAIL(&sc->sc_pendscb, scb, chain);
389 1.3.2.7 nathanw }
390 1.3.2.7 nathanw
391 1.3.2.7 nathanw static __inline void
392 1.3.2.7 nathanw iha_push_pend_scb(sc, scb)
393 1.3.2.7 nathanw struct iha_softc *sc;
394 1.3.2.7 nathanw struct iha_scb *scb;
395 1.3.2.7 nathanw {
396 1.3.2.7 nathanw int s;
397 1.3.2.7 nathanw
398 1.3.2.7 nathanw s = splbio();
399 1.3.2.7 nathanw
400 1.3.2.7 nathanw if (scb == sc->sc_actscb)
401 1.3.2.7 nathanw sc->sc_actscb = NULL;
402 1.3.2.7 nathanw
403 1.3.2.7 nathanw scb->status = STATUS_QUEUED;
404 1.3.2.7 nathanw
405 1.3.2.7 nathanw TAILQ_INSERT_HEAD(&sc->sc_pendscb, scb, chain);
406 1.3.2.7 nathanw
407 1.3.2.7 nathanw splx(s);
408 1.3.2.7 nathanw }
409 1.3.2.7 nathanw
410 1.3.2.7 nathanw /*
411 1.3.2.7 nathanw * iha_del_pend_scb - remove scb from sc_pendscb
412 1.3.2.7 nathanw */
413 1.3.2.7 nathanw static __inline void
414 1.3.2.7 nathanw iha_del_pend_scb(sc, scb)
415 1.3.2.7 nathanw struct iha_softc *sc;
416 1.3.2.7 nathanw struct iha_scb *scb;
417 1.3.2.7 nathanw {
418 1.3.2.7 nathanw int s;
419 1.3.2.7 nathanw
420 1.3.2.7 nathanw s = splbio();
421 1.3.2.7 nathanw
422 1.3.2.7 nathanw TAILQ_REMOVE(&sc->sc_pendscb, scb, chain);
423 1.3.2.7 nathanw
424 1.3.2.7 nathanw splx(s);
425 1.3.2.7 nathanw }
426 1.3.2.7 nathanw
427 1.3.2.7 nathanw static __inline void
428 1.3.2.7 nathanw iha_mark_busy_scb(scb)
429 1.3.2.7 nathanw struct iha_scb *scb;
430 1.3.2.7 nathanw {
431 1.3.2.7 nathanw int s;
432 1.3.2.7 nathanw
433 1.3.2.7 nathanw s = splbio();
434 1.3.2.7 nathanw
435 1.3.2.7 nathanw scb->status = STATUS_BUSY;
436 1.3.2.7 nathanw
437 1.3.2.7 nathanw if (scb->scb_tagmsg == 0)
438 1.3.2.7 nathanw scb->tcs->ntagscb = scb;
439 1.3.2.7 nathanw else
440 1.3.2.7 nathanw scb->tcs->tagcnt++;
441 1.3.2.7 nathanw
442 1.3.2.7 nathanw splx(s);
443 1.3.2.7 nathanw }
444 1.3.2.7 nathanw
445 1.3.2.7 nathanw /*
446 1.3.2.7 nathanw * iha_set_ssig - read the current scsi signal mask, then write a new
447 1.3.2.7 nathanw * one which turns off/on the specified signals.
448 1.3.2.7 nathanw */
449 1.3.2.7 nathanw static __inline void
450 1.3.2.7 nathanw iha_set_ssig(sc, offsigs, onsigs)
451 1.3.2.7 nathanw struct iha_softc *sc;
452 1.3.2.7 nathanw u_int8_t offsigs, onsigs;
453 1.3.2.7 nathanw {
454 1.3.2.7 nathanw bus_space_tag_t iot = sc->sc_iot;
455 1.3.2.7 nathanw bus_space_handle_t ioh = sc->sc_ioh;
456 1.3.2.7 nathanw u_int8_t currsigs;
457 1.3.2.7 nathanw
458 1.3.2.7 nathanw currsigs = bus_space_read_1(iot, ioh, TUL_SSIGI);
459 1.3.2.7 nathanw bus_space_write_1(iot, ioh, TUL_SSIGO, (currsigs & ~offsigs) | onsigs);
460 1.3.2.7 nathanw }
461 1.3.2.7 nathanw
462 1.3.2.7 nathanw /*
463 1.3.2.7 nathanw * iha_intr - the interrupt service routine for the iha driver
464 1.3.2.7 nathanw */
465 1.3.2.7 nathanw int
466 1.3.2.7 nathanw iha_intr(arg)
467 1.3.2.7 nathanw void *arg;
468 1.3.2.7 nathanw {
469 1.3.2.7 nathanw bus_space_tag_t iot;
470 1.3.2.7 nathanw bus_space_handle_t ioh;
471 1.3.2.7 nathanw struct iha_softc *sc;
472 1.3.2.7 nathanw int s;
473 1.3.2.7 nathanw
474 1.3.2.7 nathanw sc = (struct iha_softc *)arg;
475 1.3.2.7 nathanw iot = sc->sc_iot;
476 1.3.2.7 nathanw ioh = sc->sc_ioh;
477 1.3.2.7 nathanw
478 1.3.2.7 nathanw if ((bus_space_read_1(iot, ioh, TUL_STAT0) & INTPD) == 0)
479 1.3.2.7 nathanw return (0);
480 1.3.2.7 nathanw
481 1.3.2.7 nathanw s = splbio(); /* XXX - Or are interrupts off when ISR's are called? */
482 1.3.2.7 nathanw
483 1.3.2.7 nathanw if (sc->sc_semaph != SEMAPH_IN_MAIN) {
484 1.3.2.7 nathanw /* XXX - need these inside a splbio()/splx()? */
485 1.3.2.7 nathanw bus_space_write_1(iot, ioh, TUL_IMSK, MASK_ALL);
486 1.3.2.7 nathanw sc->sc_semaph = SEMAPH_IN_MAIN;
487 1.3.2.7 nathanw
488 1.3.2.7 nathanw iha_main(sc);
489 1.3.2.7 nathanw
490 1.3.2.7 nathanw sc->sc_semaph = ~SEMAPH_IN_MAIN;
491 1.3.2.7 nathanw bus_space_write_1(iot, ioh, TUL_IMSK, (MASK_ALL & ~MSCMP));
492 1.3.2.7 nathanw }
493 1.3.2.7 nathanw
494 1.3.2.7 nathanw splx(s);
495 1.3.2.7 nathanw
496 1.3.2.7 nathanw return (1);
497 1.3.2.7 nathanw }
498 1.3.2.7 nathanw
499 1.3.2.7 nathanw void
500 1.3.2.7 nathanw iha_attach(sc)
501 1.3.2.7 nathanw struct iha_softc *sc;
502 1.3.2.7 nathanw {
503 1.3.2.7 nathanw bus_space_tag_t iot = sc->sc_iot;
504 1.3.2.7 nathanw bus_space_handle_t ioh = sc->sc_ioh;
505 1.3.2.7 nathanw struct iha_scb *scb;
506 1.3.2.7 nathanw struct iha_eeprom eeprom;
507 1.3.2.7 nathanw struct eeprom_adapter *conf;
508 1.3.2.7 nathanw int i, error, reg;
509 1.3.2.7 nathanw
510 1.3.2.7 nathanw iha_read_eeprom(sc, &eeprom);
511 1.3.2.7 nathanw
512 1.3.2.7 nathanw conf = &eeprom.adapter[0];
513 1.3.2.7 nathanw
514 1.3.2.7 nathanw /*
515 1.3.2.7 nathanw * fill in the rest of the iha_softc fields
516 1.3.2.7 nathanw */
517 1.3.2.7 nathanw sc->sc_id = CFG_ID(conf->config1);
518 1.3.2.7 nathanw sc->sc_semaph = ~SEMAPH_IN_MAIN;
519 1.3.2.7 nathanw sc->sc_status0 = 0;
520 1.3.2.7 nathanw sc->sc_actscb = NULL;
521 1.3.2.7 nathanw
522 1.3.2.7 nathanw TAILQ_INIT(&sc->sc_freescb);
523 1.3.2.7 nathanw TAILQ_INIT(&sc->sc_pendscb);
524 1.3.2.7 nathanw TAILQ_INIT(&sc->sc_donescb);
525 1.3.2.7 nathanw error = iha_alloc_sglist(sc);
526 1.3.2.2 nathanw if (error != 0) {
527 1.3.2.2 nathanw printf(": cannot allocate sglist\n");
528 1.3.2.2 nathanw return;
529 1.3.2.2 nathanw }
530 1.3.2.2 nathanw
531 1.3.2.7 nathanw sc->sc_scb = malloc(sizeof(struct iha_scb) * IHA_MAX_SCB,
532 1.3.2.6 nathanw M_DEVBUF, M_NOWAIT|M_ZERO);
533 1.3.2.2 nathanw if (sc->sc_scb == NULL) {
534 1.3.2.2 nathanw printf(": cannot allocate SCB\n");
535 1.3.2.2 nathanw return;
536 1.3.2.2 nathanw }
537 1.3.2.2 nathanw
538 1.3.2.2 nathanw for (i = 0, scb = sc->sc_scb; i < IHA_MAX_SCB; i++, scb++) {
539 1.3.2.2 nathanw scb->scb_tagid = i;
540 1.3.2.2 nathanw scb->sgoffset = IHA_SG_SIZE * i;
541 1.3.2.4 nathanw scb->sglist = sc->sc_sglist + IHA_MAX_SG_ENTRIES * i;
542 1.3.2.2 nathanw scb->sg_addr =
543 1.3.2.2 nathanw sc->sc_dmamap->dm_segs[0].ds_addr + scb->sgoffset;
544 1.3.2.2 nathanw
545 1.3.2.2 nathanw error = bus_dmamap_create(sc->sc_dmat,
546 1.3.2.4 nathanw MAXPHYS, IHA_MAX_SG_ENTRIES, MAXPHYS, 0,
547 1.3.2.2 nathanw BUS_DMA_NOWAIT, &scb->dmap);
548 1.3.2.2 nathanw
549 1.3.2.2 nathanw if (error != 0) {
550 1.3.2.2 nathanw printf(": couldn't create SCB DMA map, error = %d\n",
551 1.3.2.2 nathanw error);
552 1.3.2.2 nathanw return;
553 1.3.2.2 nathanw }
554 1.3.2.2 nathanw TAILQ_INSERT_TAIL(&sc->sc_freescb, scb, chain);
555 1.3.2.2 nathanw }
556 1.3.2.2 nathanw
557 1.3.2.2 nathanw /* Mask all the interrupts */
558 1.3.2.2 nathanw bus_space_write_1(iot, ioh, TUL_IMSK, MASK_ALL);
559 1.3.2.2 nathanw
560 1.3.2.2 nathanw /* Stop any I/O and reset the scsi module */
561 1.3.2.3 nathanw iha_reset_dma(sc);
562 1.3.2.2 nathanw bus_space_write_1(iot, ioh, TUL_SCTRL0, RSMOD);
563 1.3.2.2 nathanw
564 1.3.2.2 nathanw /* Program HBA's SCSI ID */
565 1.3.2.2 nathanw bus_space_write_1(iot, ioh, TUL_SID, sc->sc_id << 4);
566 1.3.2.2 nathanw
567 1.3.2.2 nathanw /*
568 1.3.2.2 nathanw * Configure the channel as requested by the NVRAM settings read
569 1.3.2.3 nathanw * by iha_read_eeprom() above.
570 1.3.2.2 nathanw */
571 1.3.2.2 nathanw
572 1.3.2.2 nathanw sc->sc_sconf1 = SCONFIG0DEFAULT;
573 1.3.2.2 nathanw if ((conf->config1 & CFG_EN_PAR) != 0)
574 1.3.2.2 nathanw sc->sc_sconf1 |= SPCHK;
575 1.3.2.2 nathanw bus_space_write_1(iot, ioh, TUL_SCONFIG0, sc->sc_sconf1);
576 1.3.2.2 nathanw
577 1.3.2.2 nathanw /* set selection time out 250 ms */
578 1.3.2.2 nathanw bus_space_write_1(iot, ioh, TUL_STIMO, STIMO_250MS);
579 1.3.2.2 nathanw
580 1.3.2.2 nathanw /* Enable desired SCSI termination configuration read from eeprom */
581 1.3.2.2 nathanw reg = 0;
582 1.3.2.2 nathanw if (conf->config1 & CFG_ACT_TERM1)
583 1.3.2.2 nathanw reg |= ENTMW;
584 1.3.2.2 nathanw if (conf->config1 & CFG_ACT_TERM2)
585 1.3.2.2 nathanw reg |= ENTM;
586 1.3.2.2 nathanw bus_space_write_1(iot, ioh, TUL_DCTRL0, reg);
587 1.3.2.2 nathanw
588 1.3.2.2 nathanw reg = bus_space_read_1(iot, ioh, TUL_GCTRL1) & ~ATDEN;
589 1.3.2.2 nathanw if (conf->config1 & CFG_AUTO_TERM)
590 1.3.2.2 nathanw reg |= ATDEN;
591 1.3.2.2 nathanw bus_space_write_1(iot, ioh, TUL_GCTRL1, reg);
592 1.3.2.2 nathanw
593 1.3.2.2 nathanw for (i = 0; i < IHA_MAX_TARGETS / 2; i++) {
594 1.3.2.2 nathanw sc->sc_tcs[i * 2 ].flags = EEP_LBYTE(conf->tflags[i]);
595 1.3.2.2 nathanw sc->sc_tcs[i * 2 + 1].flags = EEP_HBYTE(conf->tflags[i]);
596 1.3.2.3 nathanw iha_reset_tcs(&sc->sc_tcs[i * 2 ], sc->sc_sconf1);
597 1.3.2.3 nathanw iha_reset_tcs(&sc->sc_tcs[i * 2 + 1], sc->sc_sconf1);
598 1.3.2.2 nathanw }
599 1.3.2.2 nathanw
600 1.3.2.3 nathanw iha_reset_chip(sc);
601 1.3.2.2 nathanw bus_space_write_1(iot, ioh, TUL_SIEN, ALL_INTERRUPTS);
602 1.3.2.2 nathanw
603 1.3.2.2 nathanw /*
604 1.3.2.2 nathanw * fill in the adapter.
605 1.3.2.2 nathanw */
606 1.3.2.2 nathanw sc->sc_adapter.adapt_dev = &sc->sc_dev;
607 1.3.2.2 nathanw sc->sc_adapter.adapt_nchannels = 1;
608 1.3.2.2 nathanw sc->sc_adapter.adapt_openings = IHA_MAX_SCB;
609 1.3.2.2 nathanw sc->sc_adapter.adapt_max_periph = IHA_MAX_SCB;
610 1.3.2.2 nathanw sc->sc_adapter.adapt_ioctl = NULL;
611 1.3.2.4 nathanw sc->sc_adapter.adapt_minphys = minphys;
612 1.3.2.2 nathanw sc->sc_adapter.adapt_request = iha_scsipi_request;
613 1.3.2.2 nathanw
614 1.3.2.2 nathanw /*
615 1.3.2.2 nathanw * fill in the channel.
616 1.3.2.2 nathanw */
617 1.3.2.2 nathanw sc->sc_channel.chan_adapter = &sc->sc_adapter;
618 1.3.2.2 nathanw sc->sc_channel.chan_bustype = &scsi_bustype;
619 1.3.2.2 nathanw sc->sc_channel.chan_channel = 0;
620 1.3.2.2 nathanw sc->sc_channel.chan_ntargets = CFG_TARGET(conf->config2);
621 1.3.2.2 nathanw sc->sc_channel.chan_nluns = 8;
622 1.3.2.2 nathanw sc->sc_channel.chan_id = sc->sc_id;
623 1.3.2.2 nathanw
624 1.3.2.2 nathanw /*
625 1.3.2.2 nathanw * Now try to attach all the sub devices.
626 1.3.2.2 nathanw */
627 1.3.2.2 nathanw config_found(&sc->sc_dev, &sc->sc_channel, scsiprint);
628 1.3.2.2 nathanw }
629 1.3.2.2 nathanw
630 1.3.2.2 nathanw /*
631 1.3.2.7 nathanw * iha_alloc_sglist - allocate and map sglist for SCB's
632 1.3.2.2 nathanw */
633 1.3.2.7 nathanw static int
634 1.3.2.7 nathanw iha_alloc_sglist(sc)
635 1.3.2.2 nathanw struct iha_softc *sc;
636 1.3.2.2 nathanw {
637 1.3.2.7 nathanw bus_dma_segment_t seg;
638 1.3.2.7 nathanw int error, rseg;
639 1.3.2.2 nathanw
640 1.3.2.7 nathanw /*
641 1.3.2.7 nathanw * Allocate dma-safe memory for the SCB's sglist
642 1.3.2.7 nathanw */
643 1.3.2.7 nathanw if ((error = bus_dmamem_alloc(sc->sc_dmat,
644 1.3.2.7 nathanw IHA_SG_SIZE * IHA_MAX_SCB,
645 1.3.2.7 nathanw PAGE_SIZE, 0, &seg, 1, &rseg, BUS_DMA_NOWAIT)) != 0) {
646 1.3.2.7 nathanw printf(": unable to allocate sglist, error = %d\n", error);
647 1.3.2.7 nathanw return (error);
648 1.3.2.7 nathanw }
649 1.3.2.7 nathanw if ((error = bus_dmamem_map(sc->sc_dmat, &seg, rseg,
650 1.3.2.7 nathanw IHA_SG_SIZE * IHA_MAX_SCB, (caddr_t *)&sc->sc_sglist,
651 1.3.2.7 nathanw BUS_DMA_NOWAIT | BUS_DMA_COHERENT)) != 0) {
652 1.3.2.7 nathanw printf(": unable to map sglist, error = %d\n", error);
653 1.3.2.7 nathanw return (error);
654 1.3.2.2 nathanw }
655 1.3.2.2 nathanw
656 1.3.2.7 nathanw /*
657 1.3.2.7 nathanw * Create and load the DMA map used for the SCBs
658 1.3.2.7 nathanw */
659 1.3.2.7 nathanw if ((error = bus_dmamap_create(sc->sc_dmat,
660 1.3.2.7 nathanw IHA_SG_SIZE * IHA_MAX_SCB, 1, IHA_SG_SIZE * IHA_MAX_SCB,
661 1.3.2.7 nathanw 0, BUS_DMA_NOWAIT, &sc->sc_dmamap)) != 0) {
662 1.3.2.7 nathanw printf(": unable to create control DMA map, error = %d\n",
663 1.3.2.7 nathanw error);
664 1.3.2.7 nathanw return (error);
665 1.3.2.7 nathanw }
666 1.3.2.7 nathanw if ((error = bus_dmamap_load(sc->sc_dmat, sc->sc_dmamap,
667 1.3.2.7 nathanw sc->sc_sglist, IHA_SG_SIZE * IHA_MAX_SCB,
668 1.3.2.7 nathanw NULL, BUS_DMA_NOWAIT)) != 0) {
669 1.3.2.7 nathanw printf(": unable to load control DMA map, error = %d\n", error);
670 1.3.2.7 nathanw return (error);
671 1.3.2.7 nathanw }
672 1.3.2.7 nathanw
673 1.3.2.7 nathanw memset(sc->sc_sglist, 0, IHA_SG_SIZE * IHA_MAX_SCB);
674 1.3.2.7 nathanw
675 1.3.2.7 nathanw return (0);
676 1.3.2.2 nathanw }
677 1.3.2.2 nathanw
678 1.3.2.7 nathanw void
679 1.3.2.7 nathanw iha_scsipi_request(chan, req, arg)
680 1.3.2.7 nathanw struct scsipi_channel *chan;
681 1.3.2.7 nathanw scsipi_adapter_req_t req;
682 1.3.2.7 nathanw void *arg;
683 1.3.2.2 nathanw {
684 1.3.2.7 nathanw struct scsipi_xfer *xs;
685 1.3.2.7 nathanw struct scsipi_periph *periph;
686 1.3.2.7 nathanw struct iha_scb *scb;
687 1.3.2.7 nathanw struct iha_softc *sc;
688 1.3.2.7 nathanw int error, s;
689 1.3.2.2 nathanw
690 1.3.2.7 nathanw sc = (struct iha_softc *)chan->chan_adapter->adapt_dev;
691 1.3.2.2 nathanw
692 1.3.2.7 nathanw switch (req) {
693 1.3.2.7 nathanw case ADAPTER_REQ_RUN_XFER:
694 1.3.2.7 nathanw xs = arg;
695 1.3.2.7 nathanw periph = xs->xs_periph;
696 1.3.2.2 nathanw
697 1.3.2.7 nathanw if (xs->cmdlen > sizeof(struct scsi_generic) ||
698 1.3.2.7 nathanw periph->periph_target >= IHA_MAX_TARGETS) {
699 1.3.2.7 nathanw xs->error = XS_DRIVER_STUFFUP;
700 1.3.2.7 nathanw return;
701 1.3.2.7 nathanw }
702 1.3.2.2 nathanw
703 1.3.2.7 nathanw s = splbio();
704 1.3.2.7 nathanw scb = TAILQ_FIRST(&sc->sc_freescb);
705 1.3.2.7 nathanw if (scb != NULL) {
706 1.3.2.7 nathanw scb->status = STATUS_RENT;
707 1.3.2.7 nathanw TAILQ_REMOVE(&sc->sc_freescb, scb, chain);
708 1.3.2.7 nathanw }
709 1.3.2.7 nathanw #ifdef DIAGNOSTIC
710 1.3.2.7 nathanw else {
711 1.3.2.7 nathanw scsipi_printaddr(periph);
712 1.3.2.7 nathanw printf("unable to allocate scb\n");
713 1.3.2.7 nathanw panic("iha_scsipi_request");
714 1.3.2.7 nathanw }
715 1.3.2.7 nathanw #endif
716 1.3.2.7 nathanw splx(s);
717 1.3.2.2 nathanw
718 1.3.2.7 nathanw scb->target = periph->periph_target;
719 1.3.2.7 nathanw scb->lun = periph->periph_lun;
720 1.3.2.7 nathanw scb->tcs = &sc->sc_tcs[scb->target];
721 1.3.2.7 nathanw scb->scb_id = MSG_IDENTIFY(periph->periph_lun,
722 1.3.2.7 nathanw (xs->xs_control & XS_CTL_REQSENSE) == 0);
723 1.3.2.2 nathanw
724 1.3.2.7 nathanw scb->xs = xs;
725 1.3.2.7 nathanw scb->cmdlen = xs->cmdlen;
726 1.3.2.7 nathanw memcpy(&scb->cmd, xs->cmd, xs->cmdlen);
727 1.3.2.7 nathanw scb->buflen = xs->datalen;
728 1.3.2.7 nathanw scb->flags = 0;
729 1.3.2.7 nathanw if (xs->xs_control & XS_CTL_DATA_OUT)
730 1.3.2.7 nathanw scb->flags |= FLAG_DATAOUT;
731 1.3.2.7 nathanw if (xs->xs_control & XS_CTL_DATA_IN)
732 1.3.2.7 nathanw scb->flags |= FLAG_DATAIN;
733 1.3.2.2 nathanw
734 1.3.2.7 nathanw if (scb->flags & (FLAG_DATAIN | FLAG_DATAOUT)) {
735 1.3.2.7 nathanw error = bus_dmamap_load(sc->sc_dmat, scb->dmap,
736 1.3.2.7 nathanw xs->data, scb->buflen, NULL,
737 1.3.2.7 nathanw ((xs->xs_control & XS_CTL_NOSLEEP) ?
738 1.3.2.7 nathanw BUS_DMA_NOWAIT : BUS_DMA_WAITOK) |
739 1.3.2.7 nathanw BUS_DMA_STREAMING |
740 1.3.2.7 nathanw ((scb->flags & FLAG_DATAIN) ?
741 1.3.2.7 nathanw BUS_DMA_READ : BUS_DMA_WRITE));
742 1.3.2.2 nathanw
743 1.3.2.7 nathanw if (error) {
744 1.3.2.7 nathanw printf("%s: error %d loading dma map\n",
745 1.3.2.7 nathanw sc->sc_dev.dv_xname, error);
746 1.3.2.7 nathanw iha_append_free_scb(sc, scb);
747 1.3.2.7 nathanw xs->error = XS_DRIVER_STUFFUP;
748 1.3.2.7 nathanw scsipi_done(xs);
749 1.3.2.7 nathanw return;
750 1.3.2.7 nathanw }
751 1.3.2.7 nathanw bus_dmamap_sync(sc->sc_dmat, scb->dmap,
752 1.3.2.7 nathanw 0, scb->dmap->dm_mapsize,
753 1.3.2.7 nathanw (scb->flags & FLAG_DATAIN) ?
754 1.3.2.7 nathanw BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE);
755 1.3.2.7 nathanw }
756 1.3.2.2 nathanw
757 1.3.2.7 nathanw iha_exec_scb(sc, scb);
758 1.3.2.7 nathanw return;
759 1.3.2.2 nathanw
760 1.3.2.7 nathanw case ADAPTER_REQ_GROW_RESOURCES:
761 1.3.2.7 nathanw return; /* XXX */
762 1.3.2.2 nathanw
763 1.3.2.7 nathanw case ADAPTER_REQ_SET_XFER_MODE:
764 1.3.2.7 nathanw {
765 1.3.2.7 nathanw struct tcs *tcs;
766 1.3.2.7 nathanw struct scsipi_xfer_mode *xm = arg;
767 1.3.2.7 nathanw
768 1.3.2.7 nathanw tcs = &sc->sc_tcs[xm->xm_target];
769 1.3.2.7 nathanw
770 1.3.2.7 nathanw if ((xm->xm_mode & PERIPH_CAP_WIDE16) != 0 &&
771 1.3.2.7 nathanw (tcs->flags & FLAG_NO_WIDE) == 0)
772 1.3.2.7 nathanw tcs->flags &= ~(FLAG_WIDE_DONE|FLAG_SYNC_DONE);
773 1.3.2.7 nathanw
774 1.3.2.7 nathanw if ((xm->xm_mode & PERIPH_CAP_SYNC) != 0 &&
775 1.3.2.7 nathanw (tcs->flags & FLAG_NO_SYNC) == 0)
776 1.3.2.7 nathanw tcs->flags &= ~FLAG_SYNC_DONE;
777 1.3.2.7 nathanw
778 1.3.2.7 nathanw /*
779 1.3.2.7 nathanw * If we're not going to negotiate, send the
780 1.3.2.7 nathanw * notification now, since it won't happen later.
781 1.3.2.7 nathanw */
782 1.3.2.7 nathanw if ((tcs->flags & (FLAG_WIDE_DONE|FLAG_SYNC_DONE)) ==
783 1.3.2.7 nathanw (FLAG_WIDE_DONE|FLAG_SYNC_DONE))
784 1.3.2.7 nathanw iha_update_xfer_mode(sc, xm->xm_target);
785 1.3.2.7 nathanw
786 1.3.2.7 nathanw return;
787 1.3.2.7 nathanw }
788 1.3.2.7 nathanw }
789 1.3.2.2 nathanw }
790 1.3.2.2 nathanw
791 1.3.2.7 nathanw void
792 1.3.2.7 nathanw iha_update_xfer_mode(sc, target)
793 1.3.2.2 nathanw struct iha_softc *sc;
794 1.3.2.7 nathanw int target;
795 1.3.2.2 nathanw {
796 1.3.2.7 nathanw struct tcs *tcs = &sc->sc_tcs[target];
797 1.3.2.7 nathanw struct scsipi_xfer_mode xm;
798 1.3.2.2 nathanw
799 1.3.2.7 nathanw xm.xm_target = target;
800 1.3.2.7 nathanw xm.xm_mode = 0;
801 1.3.2.7 nathanw xm.xm_period = 0;
802 1.3.2.7 nathanw xm.xm_offset = 0;
803 1.3.2.2 nathanw
804 1.3.2.7 nathanw if (tcs->syncm & PERIOD_WIDE_SCSI)
805 1.3.2.7 nathanw xm.xm_mode |= PERIPH_CAP_WIDE16;
806 1.3.2.2 nathanw
807 1.3.2.7 nathanw if (tcs->period) {
808 1.3.2.7 nathanw xm.xm_mode |= PERIPH_CAP_SYNC;
809 1.3.2.7 nathanw xm.xm_period = tcs->period;
810 1.3.2.7 nathanw xm.xm_offset = tcs->offset;
811 1.3.2.7 nathanw }
812 1.3.2.2 nathanw
813 1.3.2.7 nathanw scsipi_async_event(&sc->sc_channel, ASYNC_EVENT_XFER_MODE, &xm);
814 1.3.2.2 nathanw }
815 1.3.2.2 nathanw
816 1.3.2.7 nathanw static void
817 1.3.2.7 nathanw iha_reset_scsi_bus(sc)
818 1.3.2.2 nathanw struct iha_softc *sc;
819 1.3.2.2 nathanw {
820 1.3.2.7 nathanw struct iha_scb *scb;
821 1.3.2.2 nathanw struct tcs *tcs;
822 1.3.2.7 nathanw int i, s;
823 1.3.2.2 nathanw
824 1.3.2.2 nathanw s = splbio();
825 1.3.2.2 nathanw
826 1.3.2.7 nathanw iha_reset_dma(sc);
827 1.3.2.2 nathanw
828 1.3.2.7 nathanw for (i = 0, scb = sc->sc_scb; i < IHA_MAX_SCB; i++, scb++)
829 1.3.2.7 nathanw switch (scb->status) {
830 1.3.2.7 nathanw case STATUS_BUSY:
831 1.3.2.7 nathanw iha_append_done_scb(sc, scb, HOST_SCSI_RST);
832 1.3.2.7 nathanw break;
833 1.3.2.2 nathanw
834 1.3.2.7 nathanw case STATUS_SELECT:
835 1.3.2.7 nathanw iha_push_pend_scb(sc, scb);
836 1.3.2.7 nathanw break;
837 1.3.2.2 nathanw
838 1.3.2.7 nathanw default:
839 1.3.2.7 nathanw break;
840 1.3.2.2 nathanw }
841 1.3.2.2 nathanw
842 1.3.2.7 nathanw for (i = 0, tcs = sc->sc_tcs; i < IHA_MAX_TARGETS; i++, tcs++)
843 1.3.2.7 nathanw iha_reset_tcs(tcs, sc->sc_sconf1);
844 1.3.2.2 nathanw
845 1.3.2.7 nathanw splx(s);
846 1.3.2.2 nathanw }
847 1.3.2.2 nathanw
848 1.3.2.7 nathanw void
849 1.3.2.7 nathanw iha_reset_chip(sc)
850 1.3.2.2 nathanw struct iha_softc *sc;
851 1.3.2.2 nathanw {
852 1.3.2.7 nathanw bus_space_tag_t iot = sc->sc_iot;
853 1.3.2.7 nathanw bus_space_handle_t ioh = sc->sc_ioh;
854 1.3.2.2 nathanw
855 1.3.2.7 nathanw /* reset tulip chip */
856 1.3.2.2 nathanw
857 1.3.2.7 nathanw bus_space_write_1(iot, ioh, TUL_SCTRL0, RSCSI);
858 1.3.2.2 nathanw
859 1.3.2.7 nathanw do {
860 1.3.2.7 nathanw sc->sc_sistat = bus_space_read_1(iot, ioh, TUL_SISTAT);
861 1.3.2.7 nathanw } while ((sc->sc_sistat & SRSTD) == 0);
862 1.3.2.2 nathanw
863 1.3.2.7 nathanw iha_set_ssig(sc, 0, 0);
864 1.3.2.2 nathanw
865 1.3.2.7 nathanw bus_space_read_1(iot, ioh, TUL_SISTAT); /* Clear any active interrupt*/
866 1.3.2.2 nathanw }
867 1.3.2.2 nathanw
868 1.3.2.7 nathanw /*
869 1.3.2.7 nathanw * iha_reset_dma - abort any active DMA xfer, reset tulip FIFO.
870 1.3.2.7 nathanw */
871 1.3.2.2 nathanw static void
872 1.3.2.7 nathanw iha_reset_dma(sc)
873 1.3.2.2 nathanw struct iha_softc *sc;
874 1.3.2.2 nathanw {
875 1.3.2.7 nathanw bus_space_tag_t iot = sc->sc_iot;
876 1.3.2.7 nathanw bus_space_handle_t ioh = sc->sc_ioh;
877 1.3.2.2 nathanw
878 1.3.2.7 nathanw if ((bus_space_read_1(iot, ioh, TUL_ISTUS1) & XPEND) != 0) {
879 1.3.2.7 nathanw /* if DMA xfer is pending, abort DMA xfer */
880 1.3.2.7 nathanw bus_space_write_1(iot, ioh, TUL_DCMD, ABTXFR);
881 1.3.2.7 nathanw /* wait Abort DMA xfer done */
882 1.3.2.7 nathanw while ((bus_space_read_1(iot, ioh, TUL_ISTUS0) & DABT) == 0)
883 1.3.2.7 nathanw ;
884 1.3.2.7 nathanw }
885 1.3.2.2 nathanw
886 1.3.2.7 nathanw bus_space_write_1(iot, ioh, TUL_SCTRL0, RSFIFO);
887 1.3.2.7 nathanw }
888 1.3.2.2 nathanw
889 1.3.2.2 nathanw /*
890 1.3.2.7 nathanw * iha_reset_tcs - reset the target control structure pointed
891 1.3.2.7 nathanw * to by tcs to default values. tcs flags
892 1.3.2.7 nathanw * only has the negotiation done bits reset as
893 1.3.2.7 nathanw * the other bits are fixed at initialization.
894 1.3.2.2 nathanw */
895 1.3.2.2 nathanw static void
896 1.3.2.7 nathanw iha_reset_tcs(tcs, config0)
897 1.3.2.7 nathanw struct tcs *tcs;
898 1.3.2.7 nathanw u_int8_t config0;
899 1.3.2.2 nathanw {
900 1.3.2.2 nathanw
901 1.3.2.7 nathanw tcs->flags &= ~(FLAG_SYNC_DONE | FLAG_WIDE_DONE);
902 1.3.2.7 nathanw tcs->period = 0;
903 1.3.2.7 nathanw tcs->offset = 0;
904 1.3.2.7 nathanw tcs->tagcnt = 0;
905 1.3.2.7 nathanw tcs->ntagscb = NULL;
906 1.3.2.7 nathanw tcs->syncm = 0;
907 1.3.2.7 nathanw tcs->sconfig0 = config0;
908 1.3.2.2 nathanw }
909 1.3.2.2 nathanw
910 1.3.2.2 nathanw /*
911 1.3.2.3 nathanw * iha_main - process the active SCB, taking one off pending and making it
912 1.3.2.2 nathanw * active if necessary, and any done SCB's created as
913 1.3.2.2 nathanw * a result until there are no interrupts pending and no pending
914 1.3.2.2 nathanw * SCB's that can be started.
915 1.3.2.2 nathanw */
916 1.3.2.2 nathanw static void
917 1.3.2.3 nathanw iha_main(sc)
918 1.3.2.2 nathanw struct iha_softc *sc;
919 1.3.2.2 nathanw {
920 1.3.2.2 nathanw bus_space_tag_t iot = sc->sc_iot;
921 1.3.2.2 nathanw bus_space_handle_t ioh =sc->sc_ioh;
922 1.3.2.7 nathanw struct iha_scb *scb;
923 1.3.2.2 nathanw
924 1.3.2.2 nathanw for (;;) {
925 1.3.2.3 nathanw iha_scsi(sc);
926 1.3.2.2 nathanw
927 1.3.2.3 nathanw while ((scb = iha_pop_done_scb(sc)) != NULL)
928 1.3.2.3 nathanw iha_done_scb(sc, scb);
929 1.3.2.2 nathanw
930 1.3.2.2 nathanw /*
931 1.3.2.2 nathanw * If there are no interrupts pending, or we can't start
932 1.3.2.2 nathanw * a pending sc, break out of the for(;;). Otherwise
933 1.3.2.2 nathanw * continue the good work with another call to
934 1.3.2.3 nathanw * iha_scsi().
935 1.3.2.2 nathanw */
936 1.3.2.2 nathanw if (((bus_space_read_1(iot, ioh, TUL_STAT0) & INTPD) == 0)
937 1.3.2.3 nathanw && (iha_find_pend_scb(sc) == NULL))
938 1.3.2.2 nathanw break;
939 1.3.2.2 nathanw }
940 1.3.2.2 nathanw }
941 1.3.2.2 nathanw
942 1.3.2.2 nathanw /*
943 1.3.2.3 nathanw * iha_scsi - service any outstanding interrupts. If there are none, try to
944 1.3.2.2 nathanw * start another SCB currently in the pending queue.
945 1.3.2.2 nathanw */
946 1.3.2.2 nathanw static void
947 1.3.2.3 nathanw iha_scsi(sc)
948 1.3.2.2 nathanw struct iha_softc *sc;
949 1.3.2.2 nathanw {
950 1.3.2.2 nathanw bus_space_tag_t iot = sc->sc_iot;
951 1.3.2.2 nathanw bus_space_handle_t ioh = sc->sc_ioh;
952 1.3.2.7 nathanw struct iha_scb *scb;
953 1.3.2.2 nathanw struct tcs *tcs;
954 1.3.2.2 nathanw u_int8_t stat;
955 1.3.2.2 nathanw
956 1.3.2.2 nathanw /* service pending interrupts asap */
957 1.3.2.2 nathanw
958 1.3.2.2 nathanw stat = bus_space_read_1(iot, ioh, TUL_STAT0);
959 1.3.2.2 nathanw if ((stat & INTPD) != 0) {
960 1.3.2.2 nathanw sc->sc_status0 = stat;
961 1.3.2.2 nathanw sc->sc_status1 = bus_space_read_1(iot, ioh, TUL_STAT1);
962 1.3.2.2 nathanw sc->sc_sistat = bus_space_read_1(iot, ioh, TUL_SISTAT);
963 1.3.2.2 nathanw
964 1.3.2.2 nathanw sc->sc_phase = sc->sc_status0 & PH_MASK;
965 1.3.2.2 nathanw
966 1.3.2.2 nathanw if ((sc->sc_sistat & SRSTD) != 0) {
967 1.3.2.3 nathanw iha_reset_scsi_bus(sc);
968 1.3.2.2 nathanw return;
969 1.3.2.2 nathanw }
970 1.3.2.2 nathanw
971 1.3.2.2 nathanw if ((sc->sc_sistat & RSELED) != 0) {
972 1.3.2.3 nathanw iha_resel(sc);
973 1.3.2.2 nathanw return;
974 1.3.2.2 nathanw }
975 1.3.2.2 nathanw
976 1.3.2.2 nathanw if ((sc->sc_sistat & (STIMEO | DISCD)) != 0) {
977 1.3.2.3 nathanw iha_busfree(sc);
978 1.3.2.2 nathanw return;
979 1.3.2.2 nathanw }
980 1.3.2.2 nathanw
981 1.3.2.2 nathanw if ((sc->sc_sistat & (SCMDN | SBSRV)) != 0) {
982 1.3.2.3 nathanw iha_next_state(sc);
983 1.3.2.2 nathanw return;
984 1.3.2.2 nathanw }
985 1.3.2.2 nathanw
986 1.3.2.2 nathanw if ((sc->sc_sistat & SELED) != 0)
987 1.3.2.3 nathanw iha_set_ssig(sc, 0, 0);
988 1.3.2.2 nathanw }
989 1.3.2.2 nathanw
990 1.3.2.2 nathanw /*
991 1.3.2.2 nathanw * There were no interrupts pending which required action elsewhere, so
992 1.3.2.2 nathanw * see if it is possible to start the selection phase on a pending SCB
993 1.3.2.2 nathanw */
994 1.3.2.3 nathanw if ((scb = iha_find_pend_scb(sc)) == NULL)
995 1.3.2.2 nathanw return;
996 1.3.2.2 nathanw
997 1.3.2.2 nathanw tcs = scb->tcs;
998 1.3.2.2 nathanw
999 1.3.2.2 nathanw /* program HBA's SCSI ID & target SCSI ID */
1000 1.3.2.2 nathanw bus_space_write_1(iot, ioh, TUL_SID, (sc->sc_id << 4) | scb->target);
1001 1.3.2.2 nathanw
1002 1.3.2.6 nathanw if ((scb->xs->xs_control & XS_CTL_RESET) == 0) {
1003 1.3.2.2 nathanw bus_space_write_1(iot, ioh, TUL_SYNCM, tcs->syncm);
1004 1.3.2.2 nathanw
1005 1.3.2.2 nathanw if ((tcs->flags & FLAG_NO_NEG_SYNC) == 0 ||
1006 1.3.2.2 nathanw (tcs->flags & FLAG_NO_NEG_WIDE) == 0)
1007 1.3.2.3 nathanw iha_select(sc, scb, SELATNSTOP);
1008 1.3.2.2 nathanw
1009 1.3.2.2 nathanw else if (scb->scb_tagmsg != 0)
1010 1.3.2.3 nathanw iha_select(sc, scb, SEL_ATN3);
1011 1.3.2.2 nathanw
1012 1.3.2.2 nathanw else
1013 1.3.2.3 nathanw iha_select(sc, scb, SEL_ATN);
1014 1.3.2.2 nathanw
1015 1.3.2.2 nathanw } else {
1016 1.3.2.3 nathanw iha_select(sc, scb, SELATNSTOP);
1017 1.3.2.2 nathanw scb->nextstat = 8;
1018 1.3.2.2 nathanw }
1019 1.3.2.2 nathanw
1020 1.3.2.6 nathanw if ((scb->xs->xs_control & XS_CTL_POLL) != 0) {
1021 1.3.2.6 nathanw int timeout;
1022 1.3.2.6 nathanw for (timeout = scb->xs->timeout; timeout > 0; timeout--) {
1023 1.3.2.3 nathanw if (iha_wait(sc, NO_OP) == -1)
1024 1.3.2.2 nathanw break;
1025 1.3.2.3 nathanw if (iha_next_state(sc) == -1)
1026 1.3.2.2 nathanw break;
1027 1.3.2.2 nathanw delay(1000); /* Only happens in boot, so it's ok */
1028 1.3.2.2 nathanw }
1029 1.3.2.2 nathanw
1030 1.3.2.2 nathanw /*
1031 1.3.2.2 nathanw * Since done queue processing not done until AFTER this
1032 1.3.2.2 nathanw * function returns, scb is on the done queue, not
1033 1.3.2.2 nathanw * the free queue at this point and still has valid data
1034 1.3.2.2 nathanw *
1035 1.3.2.2 nathanw * Conversely, xs->error has not been set yet
1036 1.3.2.2 nathanw */
1037 1.3.2.6 nathanw if (timeout == 0)
1038 1.3.2.3 nathanw iha_timeout(scb);
1039 1.3.2.2 nathanw }
1040 1.3.2.2 nathanw }
1041 1.3.2.2 nathanw
1042 1.3.2.7 nathanw static void
1043 1.3.2.7 nathanw iha_select(sc, scb, select_type)
1044 1.3.2.7 nathanw struct iha_softc *sc;
1045 1.3.2.7 nathanw struct iha_scb *scb;
1046 1.3.2.7 nathanw u_int8_t select_type;
1047 1.3.2.2 nathanw {
1048 1.3.2.7 nathanw bus_space_tag_t iot = sc->sc_iot;
1049 1.3.2.7 nathanw bus_space_handle_t ioh = sc->sc_ioh;
1050 1.3.2.2 nathanw
1051 1.3.2.7 nathanw switch (select_type) {
1052 1.3.2.7 nathanw case SEL_ATN:
1053 1.3.2.7 nathanw bus_space_write_1(iot, ioh, TUL_SFIFO, scb->scb_id);
1054 1.3.2.7 nathanw bus_space_write_multi_1(iot, ioh, TUL_SFIFO,
1055 1.3.2.7 nathanw scb->cmd, scb->cmdlen);
1056 1.3.2.2 nathanw
1057 1.3.2.7 nathanw scb->nextstat = 2;
1058 1.3.2.7 nathanw break;
1059 1.3.2.2 nathanw
1060 1.3.2.7 nathanw case SELATNSTOP:
1061 1.3.2.7 nathanw scb->nextstat = 1;
1062 1.3.2.2 nathanw break;
1063 1.3.2.2 nathanw
1064 1.3.2.7 nathanw case SEL_ATN3:
1065 1.3.2.7 nathanw bus_space_write_1(iot, ioh, TUL_SFIFO, scb->scb_id);
1066 1.3.2.7 nathanw bus_space_write_1(iot, ioh, TUL_SFIFO, scb->scb_tagmsg);
1067 1.3.2.7 nathanw bus_space_write_1(iot, ioh, TUL_SFIFO, scb->scb_tagid);
1068 1.3.2.7 nathanw
1069 1.3.2.7 nathanw bus_space_write_multi_1(iot, ioh, TUL_SFIFO, scb->cmd,
1070 1.3.2.7 nathanw scb->cmdlen);
1071 1.3.2.7 nathanw
1072 1.3.2.7 nathanw scb->nextstat = 2;
1073 1.3.2.2 nathanw break;
1074 1.3.2.7 nathanw
1075 1.3.2.7 nathanw default:
1076 1.3.2.7 nathanw printf("[debug] iha_select() - unknown select type = 0x%02x\n",
1077 1.3.2.7 nathanw select_type);
1078 1.3.2.7 nathanw return;
1079 1.3.2.2 nathanw }
1080 1.3.2.7 nathanw
1081 1.3.2.7 nathanw iha_del_pend_scb(sc, scb);
1082 1.3.2.7 nathanw scb->status = STATUS_SELECT;
1083 1.3.2.7 nathanw
1084 1.3.2.7 nathanw sc->sc_actscb = scb;
1085 1.3.2.7 nathanw
1086 1.3.2.7 nathanw bus_space_write_1(iot, ioh, TUL_SCMD, select_type);
1087 1.3.2.2 nathanw }
1088 1.3.2.2 nathanw
1089 1.3.2.2 nathanw /*
1090 1.3.2.7 nathanw * iha_wait - wait for an interrupt to service or a SCSI bus phase change
1091 1.3.2.7 nathanw * after writing the supplied command to the tulip chip. If
1092 1.3.2.7 nathanw * the command is NO_OP, skip the command writing.
1093 1.3.2.2 nathanw */
1094 1.3.2.2 nathanw static int
1095 1.3.2.7 nathanw iha_wait(sc, cmd)
1096 1.3.2.2 nathanw struct iha_softc *sc;
1097 1.3.2.7 nathanw u_int8_t cmd;
1098 1.3.2.2 nathanw {
1099 1.3.2.7 nathanw bus_space_tag_t iot = sc->sc_iot;
1100 1.3.2.7 nathanw bus_space_handle_t ioh = sc->sc_ioh;
1101 1.3.2.2 nathanw
1102 1.3.2.7 nathanw if (cmd != NO_OP)
1103 1.3.2.7 nathanw bus_space_write_1(iot, ioh, TUL_SCMD, cmd);
1104 1.3.2.2 nathanw
1105 1.3.2.7 nathanw /*
1106 1.3.2.7 nathanw * Have to do this here, in addition to in iha_isr, because
1107 1.3.2.7 nathanw * interrupts might be turned off when we get here.
1108 1.3.2.7 nathanw */
1109 1.3.2.7 nathanw do {
1110 1.3.2.7 nathanw sc->sc_status0 = bus_space_read_1(iot, ioh, TUL_STAT0);
1111 1.3.2.7 nathanw } while ((sc->sc_status0 & INTPD) == 0);
1112 1.3.2.2 nathanw
1113 1.3.2.7 nathanw sc->sc_status1 = bus_space_read_1(iot, ioh, TUL_STAT1);
1114 1.3.2.7 nathanw sc->sc_sistat = bus_space_read_1(iot, ioh, TUL_SISTAT);
1115 1.3.2.2 nathanw
1116 1.3.2.7 nathanw sc->sc_phase = sc->sc_status0 & PH_MASK;
1117 1.3.2.2 nathanw
1118 1.3.2.7 nathanw if ((sc->sc_sistat & SRSTD) != 0) {
1119 1.3.2.7 nathanw /* SCSI bus reset interrupt */
1120 1.3.2.7 nathanw iha_reset_scsi_bus(sc);
1121 1.3.2.7 nathanw return (-1);
1122 1.3.2.7 nathanw }
1123 1.3.2.2 nathanw
1124 1.3.2.7 nathanw if ((sc->sc_sistat & RSELED) != 0)
1125 1.3.2.7 nathanw /* Reselection interrupt */
1126 1.3.2.7 nathanw return (iha_resel(sc));
1127 1.3.2.2 nathanw
1128 1.3.2.7 nathanw if ((sc->sc_sistat & STIMEO) != 0) {
1129 1.3.2.7 nathanw /* selected/reselected timeout interrupt */
1130 1.3.2.7 nathanw iha_busfree(sc);
1131 1.3.2.7 nathanw return (-1);
1132 1.3.2.7 nathanw }
1133 1.3.2.2 nathanw
1134 1.3.2.7 nathanw if ((sc->sc_sistat & DISCD) != 0) {
1135 1.3.2.7 nathanw /* BUS disconnection interrupt */
1136 1.3.2.7 nathanw if ((sc->sc_flags & FLAG_EXPECT_DONE_DISC) != 0) {
1137 1.3.2.7 nathanw bus_space_write_1(iot, ioh, TUL_SCTRL0, RSFIFO);
1138 1.3.2.7 nathanw bus_space_write_1(iot, ioh, TUL_SCONFIG0,
1139 1.3.2.7 nathanw SCONFIG0DEFAULT);
1140 1.3.2.7 nathanw bus_space_write_1(iot, ioh, TUL_SCTRL1, EHRSL);
1141 1.3.2.7 nathanw iha_append_done_scb(sc, sc->sc_actscb, HOST_OK);
1142 1.3.2.7 nathanw sc->sc_flags &= ~FLAG_EXPECT_DONE_DISC;
1143 1.3.2.2 nathanw
1144 1.3.2.7 nathanw } else if ((sc->sc_flags & FLAG_EXPECT_DISC) != 0) {
1145 1.3.2.7 nathanw bus_space_write_1(iot, ioh, TUL_SCTRL0, RSFIFO);
1146 1.3.2.7 nathanw bus_space_write_1(iot, ioh, TUL_SCONFIG0,
1147 1.3.2.7 nathanw SCONFIG0DEFAULT);
1148 1.3.2.7 nathanw bus_space_write_1(iot, ioh, TUL_SCTRL1, EHRSL);
1149 1.3.2.7 nathanw sc->sc_actscb = NULL;
1150 1.3.2.7 nathanw sc->sc_flags &= ~FLAG_EXPECT_DISC;
1151 1.3.2.7 nathanw
1152 1.3.2.7 nathanw } else
1153 1.3.2.7 nathanw iha_busfree(sc);
1154 1.3.2.7 nathanw
1155 1.3.2.7 nathanw return (-1);
1156 1.3.2.2 nathanw }
1157 1.3.2.2 nathanw
1158 1.3.2.7 nathanw return (sc->sc_phase);
1159 1.3.2.2 nathanw }
1160 1.3.2.2 nathanw
1161 1.3.2.7 nathanw static void
1162 1.3.2.7 nathanw iha_exec_scb(sc, scb)
1163 1.3.2.2 nathanw struct iha_softc *sc;
1164 1.3.2.7 nathanw struct iha_scb *scb;
1165 1.3.2.2 nathanw {
1166 1.3.2.7 nathanw bus_space_tag_t iot;
1167 1.3.2.7 nathanw bus_space_handle_t ioh;
1168 1.3.2.7 nathanw bus_dmamap_t dm;
1169 1.3.2.7 nathanw struct scsipi_xfer *xs = scb->xs;
1170 1.3.2.7 nathanw int nseg, s;
1171 1.3.2.2 nathanw
1172 1.3.2.7 nathanw dm = scb->dmap;
1173 1.3.2.7 nathanw nseg = dm->dm_nsegs;
1174 1.3.2.2 nathanw
1175 1.3.2.7 nathanw if (nseg > 1) {
1176 1.3.2.7 nathanw struct iha_sg_element *sg = scb->sglist;
1177 1.3.2.7 nathanw int i;
1178 1.3.2.2 nathanw
1179 1.3.2.7 nathanw for (i = 0; i < nseg; i++) {
1180 1.3.2.7 nathanw sg[i].sg_len = htole32(dm->dm_segs[i].ds_len);
1181 1.3.2.7 nathanw sg[i].sg_addr = htole32(dm->dm_segs[i].ds_addr);
1182 1.3.2.7 nathanw }
1183 1.3.2.7 nathanw bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap,
1184 1.3.2.7 nathanw scb->sgoffset, IHA_SG_SIZE,
1185 1.3.2.7 nathanw BUS_DMASYNC_PREWRITE);
1186 1.3.2.7 nathanw
1187 1.3.2.7 nathanw scb->flags |= FLAG_SG;
1188 1.3.2.7 nathanw scb->sg_size = scb->sg_max = nseg;
1189 1.3.2.7 nathanw scb->sg_index = 0;
1190 1.3.2.7 nathanw
1191 1.3.2.7 nathanw scb->bufaddr = scb->sg_addr;
1192 1.3.2.7 nathanw } else
1193 1.3.2.7 nathanw scb->bufaddr = dm->dm_segs[0].ds_addr;
1194 1.3.2.7 nathanw
1195 1.3.2.7 nathanw if ((xs->xs_control & XS_CTL_POLL) == 0) {
1196 1.3.2.9 nathanw int timeout = mstohz(xs->timeout);
1197 1.3.2.7 nathanw if (timeout == 0)
1198 1.3.2.7 nathanw timeout = 1;
1199 1.3.2.7 nathanw callout_reset(&xs->xs_callout, timeout, iha_timeout, scb);
1200 1.3.2.7 nathanw }
1201 1.3.2.7 nathanw
1202 1.3.2.7 nathanw s = splbio();
1203 1.3.2.7 nathanw
1204 1.3.2.7 nathanw if (((scb->xs->xs_control & XS_RESET) != 0) ||
1205 1.3.2.7 nathanw (scb->cmd[0] == REQUEST_SENSE))
1206 1.3.2.7 nathanw iha_push_pend_scb(sc, scb); /* Insert SCB at head of Pend */
1207 1.3.2.7 nathanw else
1208 1.3.2.7 nathanw iha_append_pend_scb(sc, scb); /* Append SCB to tail of Pend */
1209 1.3.2.2 nathanw
1210 1.3.2.2 nathanw /*
1211 1.3.2.7 nathanw * Run through iha_main() to ensure something is active, if
1212 1.3.2.7 nathanw * only this new SCB.
1213 1.3.2.2 nathanw */
1214 1.3.2.7 nathanw if (sc->sc_semaph != SEMAPH_IN_MAIN) {
1215 1.3.2.7 nathanw iot = sc->sc_iot;
1216 1.3.2.7 nathanw ioh = sc->sc_ioh;
1217 1.3.2.2 nathanw
1218 1.3.2.7 nathanw bus_space_write_1(iot, ioh, TUL_IMSK, MASK_ALL);
1219 1.3.2.7 nathanw sc->sc_semaph = SEMAPH_IN_MAIN;;
1220 1.3.2.2 nathanw
1221 1.3.2.7 nathanw splx(s);
1222 1.3.2.7 nathanw iha_main(sc);
1223 1.3.2.7 nathanw s = splbio();
1224 1.3.2.2 nathanw
1225 1.3.2.7 nathanw sc->sc_semaph = ~SEMAPH_IN_MAIN;;
1226 1.3.2.7 nathanw bus_space_write_1(iot, ioh, TUL_IMSK, (MASK_ALL & ~MSCMP));
1227 1.3.2.2 nathanw }
1228 1.3.2.2 nathanw
1229 1.3.2.7 nathanw splx(s);
1230 1.3.2.2 nathanw }
1231 1.3.2.2 nathanw
1232 1.3.2.2 nathanw /*
1233 1.3.2.7 nathanw * iha_done_scb - We have a scb which has been processed by the
1234 1.3.2.7 nathanw * adaptor, now we look to see how the operation went.
1235 1.3.2.2 nathanw */
1236 1.3.2.7 nathanw static void
1237 1.3.2.7 nathanw iha_done_scb(sc, scb)
1238 1.3.2.2 nathanw struct iha_softc *sc;
1239 1.3.2.7 nathanw struct iha_scb *scb;
1240 1.3.2.2 nathanw {
1241 1.3.2.7 nathanw struct scsipi_xfer *xs = scb->xs;
1242 1.3.2.2 nathanw
1243 1.3.2.7 nathanw if (xs != NULL) {
1244 1.3.2.7 nathanw /* Cancel the timeout. */
1245 1.3.2.7 nathanw callout_stop(&xs->xs_callout);
1246 1.3.2.2 nathanw
1247 1.3.2.7 nathanw if (scb->flags & (FLAG_DATAIN | FLAG_DATAOUT)) {
1248 1.3.2.7 nathanw bus_dmamap_sync(sc->sc_dmat, scb->dmap,
1249 1.3.2.7 nathanw 0, scb->dmap->dm_mapsize,
1250 1.3.2.7 nathanw (scb->flags & FLAG_DATAIN) ?
1251 1.3.2.7 nathanw BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE);
1252 1.3.2.7 nathanw bus_dmamap_unload(sc->sc_dmat, scb->dmap);
1253 1.3.2.7 nathanw }
1254 1.3.2.2 nathanw
1255 1.3.2.7 nathanw xs->status = scb->ta_stat;
1256 1.3.2.2 nathanw
1257 1.3.2.7 nathanw switch (scb->ha_stat) {
1258 1.3.2.7 nathanw case HOST_OK:
1259 1.3.2.7 nathanw switch (scb->ta_stat) {
1260 1.3.2.7 nathanw case SCSI_OK:
1261 1.3.2.7 nathanw case SCSI_CONDITION_MET:
1262 1.3.2.7 nathanw case SCSI_INTERM:
1263 1.3.2.7 nathanw case SCSI_INTERM_COND_MET:
1264 1.3.2.7 nathanw xs->resid = scb->buflen;
1265 1.3.2.7 nathanw xs->error = XS_NOERROR;
1266 1.3.2.7 nathanw if ((scb->flags & FLAG_RSENS) != 0)
1267 1.3.2.7 nathanw xs->error = XS_SENSE;
1268 1.3.2.7 nathanw break;
1269 1.3.2.2 nathanw
1270 1.3.2.7 nathanw case SCSI_RESV_CONFLICT:
1271 1.3.2.7 nathanw case SCSI_BUSY:
1272 1.3.2.7 nathanw case SCSI_QUEUE_FULL:
1273 1.3.2.7 nathanw xs->error = XS_BUSY;
1274 1.3.2.7 nathanw break;
1275 1.3.2.2 nathanw
1276 1.3.2.7 nathanw case SCSI_TERMINATED:
1277 1.3.2.7 nathanw case SCSI_ACA_ACTIVE:
1278 1.3.2.7 nathanw case SCSI_CHECK:
1279 1.3.2.7 nathanw scb->tcs->flags &=
1280 1.3.2.7 nathanw ~(FLAG_SYNC_DONE | FLAG_WIDE_DONE);
1281 1.3.2.2 nathanw
1282 1.3.2.7 nathanw if ((scb->flags & FLAG_RSENS) != 0 ||
1283 1.3.2.7 nathanw iha_push_sense_request(sc, scb) != 0) {
1284 1.3.2.7 nathanw scb->flags &= ~FLAG_RSENS;
1285 1.3.2.7 nathanw printf("%s: request sense failed\n",
1286 1.3.2.7 nathanw sc->sc_dev.dv_xname);
1287 1.3.2.7 nathanw xs->error = XS_DRIVER_STUFFUP;
1288 1.3.2.7 nathanw break;
1289 1.3.2.7 nathanw }
1290 1.3.2.7 nathanw
1291 1.3.2.7 nathanw xs->error = XS_SENSE;
1292 1.3.2.7 nathanw return;
1293 1.3.2.7 nathanw
1294 1.3.2.7 nathanw default:
1295 1.3.2.7 nathanw xs->error = XS_DRIVER_STUFFUP;
1296 1.3.2.7 nathanw break;
1297 1.3.2.7 nathanw }
1298 1.3.2.7 nathanw break;
1299 1.3.2.2 nathanw
1300 1.3.2.7 nathanw case HOST_SEL_TOUT:
1301 1.3.2.7 nathanw xs->error = XS_SELTIMEOUT;
1302 1.3.2.7 nathanw break;
1303 1.3.2.2 nathanw
1304 1.3.2.7 nathanw case HOST_SCSI_RST:
1305 1.3.2.7 nathanw case HOST_DEV_RST:
1306 1.3.2.7 nathanw xs->error = XS_RESET;
1307 1.3.2.2 nathanw break;
1308 1.3.2.2 nathanw
1309 1.3.2.7 nathanw case HOST_SPERR:
1310 1.3.2.7 nathanw printf("%s: SCSI Parity error detected\n",
1311 1.3.2.7 nathanw sc->sc_dev.dv_xname);
1312 1.3.2.7 nathanw xs->error = XS_DRIVER_STUFFUP;
1313 1.3.2.2 nathanw break;
1314 1.3.2.2 nathanw
1315 1.3.2.7 nathanw case HOST_TIMED_OUT:
1316 1.3.2.7 nathanw xs->error = XS_TIMEOUT;
1317 1.3.2.2 nathanw break;
1318 1.3.2.2 nathanw
1319 1.3.2.7 nathanw case HOST_DO_DU:
1320 1.3.2.7 nathanw case HOST_BAD_PHAS:
1321 1.3.2.2 nathanw default:
1322 1.3.2.7 nathanw xs->error = XS_DRIVER_STUFFUP;
1323 1.3.2.7 nathanw break;
1324 1.3.2.2 nathanw }
1325 1.3.2.7 nathanw
1326 1.3.2.7 nathanw scsipi_done(xs);
1327 1.3.2.2 nathanw }
1328 1.3.2.7 nathanw
1329 1.3.2.7 nathanw iha_append_free_scb(sc, scb);
1330 1.3.2.2 nathanw }
1331 1.3.2.2 nathanw
1332 1.3.2.2 nathanw /*
1333 1.3.2.7 nathanw * iha_push_sense_request - obtain auto sense data by pushing the
1334 1.3.2.7 nathanw * SCB needing it back onto the pending
1335 1.3.2.7 nathanw * queue with a REQUEST_SENSE CDB.
1336 1.3.2.2 nathanw */
1337 1.3.2.2 nathanw static int
1338 1.3.2.7 nathanw iha_push_sense_request(sc, scb)
1339 1.3.2.2 nathanw struct iha_softc *sc;
1340 1.3.2.7 nathanw struct iha_scb *scb;
1341 1.3.2.2 nathanw {
1342 1.3.2.7 nathanw struct scsipi_xfer *xs = scb->xs;
1343 1.3.2.7 nathanw struct scsipi_periph *periph = xs->xs_periph;
1344 1.3.2.7 nathanw struct scsipi_sense *ss = (struct scsipi_sense *)scb->cmd;
1345 1.3.2.7 nathanw int lun = periph->periph_lun;
1346 1.3.2.7 nathanw int err;
1347 1.3.2.2 nathanw
1348 1.3.2.7 nathanw ss->opcode = REQUEST_SENSE;
1349 1.3.2.7 nathanw ss->byte2 = lun << SCSI_CMD_LUN_SHIFT;
1350 1.3.2.7 nathanw ss->unused[0] = ss->unused[1] = 0;
1351 1.3.2.7 nathanw ss->length = sizeof(struct scsipi_sense_data);
1352 1.3.2.7 nathanw ss->control = 0;
1353 1.3.2.2 nathanw
1354 1.3.2.7 nathanw scb->flags = FLAG_RSENS | FLAG_DATAIN;
1355 1.3.2.2 nathanw
1356 1.3.2.7 nathanw scb->scb_id &= ~MSG_IDENTIFY_DISCFLAG;
1357 1.3.2.2 nathanw
1358 1.3.2.7 nathanw scb->scb_tagmsg = 0;
1359 1.3.2.7 nathanw scb->ta_stat = SCSI_OK;
1360 1.3.2.2 nathanw
1361 1.3.2.7 nathanw scb->cmdlen = sizeof(struct scsipi_sense);
1362 1.3.2.7 nathanw scb->buflen = ss->length;
1363 1.3.2.2 nathanw
1364 1.3.2.7 nathanw err = bus_dmamap_load(sc->sc_dmat, scb->dmap,
1365 1.3.2.7 nathanw &xs->sense.scsi_sense, scb->buflen, NULL,
1366 1.3.2.7 nathanw BUS_DMA_READ|BUS_DMA_NOWAIT);
1367 1.3.2.7 nathanw if (err != 0) {
1368 1.3.2.7 nathanw printf("iha_push_sense_request: cannot bus_dmamap_load()\n");
1369 1.3.2.7 nathanw xs->error = XS_DRIVER_STUFFUP;
1370 1.3.2.7 nathanw return 1;
1371 1.3.2.7 nathanw }
1372 1.3.2.7 nathanw bus_dmamap_sync(sc->sc_dmat, scb->dmap,
1373 1.3.2.7 nathanw 0, scb->buflen, BUS_DMASYNC_PREREAD);
1374 1.3.2.2 nathanw
1375 1.3.2.7 nathanw /* XXX What about queued command? */
1376 1.3.2.7 nathanw iha_exec_scb(sc, scb);
1377 1.3.2.2 nathanw
1378 1.3.2.7 nathanw return 0;
1379 1.3.2.2 nathanw }
1380 1.3.2.2 nathanw
1381 1.3.2.7 nathanw static void
1382 1.3.2.7 nathanw iha_timeout(arg)
1383 1.3.2.7 nathanw void *arg;
1384 1.3.2.2 nathanw {
1385 1.3.2.7 nathanw struct iha_scb *scb = (struct iha_scb *)arg;
1386 1.3.2.7 nathanw struct scsipi_xfer *xs = scb->xs;
1387 1.3.2.7 nathanw struct scsipi_periph *periph = xs->xs_periph;
1388 1.3.2.7 nathanw struct iha_softc *sc;
1389 1.3.2.2 nathanw
1390 1.3.2.7 nathanw sc = (void *)periph->periph_channel->chan_adapter->adapt_dev;
1391 1.3.2.2 nathanw
1392 1.3.2.7 nathanw if (xs == NULL)
1393 1.3.2.7 nathanw printf("[debug] iha_timeout called with xs == NULL\n");
1394 1.3.2.2 nathanw
1395 1.3.2.7 nathanw else {
1396 1.3.2.7 nathanw scsipi_printaddr(periph);
1397 1.3.2.7 nathanw printf("SCSI OpCode 0x%02x timed out\n", xs->cmd->opcode);
1398 1.3.2.2 nathanw
1399 1.3.2.7 nathanw iha_abort_xs(sc, xs, HOST_TIMED_OUT);
1400 1.3.2.2 nathanw }
1401 1.3.2.7 nathanw }
1402 1.3.2.2 nathanw
1403 1.3.2.7 nathanw /*
1404 1.3.2.7 nathanw * iha_abort_xs - find the SCB associated with the supplied xs and
1405 1.3.2.7 nathanw * stop all processing on it, moving it to the done
1406 1.3.2.7 nathanw * queue with the supplied host status value.
1407 1.3.2.7 nathanw */
1408 1.3.2.7 nathanw static void
1409 1.3.2.7 nathanw iha_abort_xs(sc, xs, hastat)
1410 1.3.2.7 nathanw struct iha_softc *sc;
1411 1.3.2.7 nathanw struct scsipi_xfer *xs;
1412 1.3.2.7 nathanw u_int8_t hastat;
1413 1.3.2.7 nathanw {
1414 1.3.2.7 nathanw struct iha_scb *scb;
1415 1.3.2.7 nathanw int i, s;
1416 1.3.2.2 nathanw
1417 1.3.2.7 nathanw s = splbio();
1418 1.3.2.2 nathanw
1419 1.3.2.7 nathanw /* Check the pending queue for the SCB pointing to xs */
1420 1.3.2.2 nathanw
1421 1.3.2.7 nathanw TAILQ_FOREACH(scb, &sc->sc_pendscb, chain)
1422 1.3.2.7 nathanw if (scb->xs == xs) {
1423 1.3.2.7 nathanw iha_del_pend_scb(sc, scb);
1424 1.3.2.7 nathanw iha_append_done_scb(sc, scb, hastat);
1425 1.3.2.7 nathanw splx(s);
1426 1.3.2.7 nathanw return;
1427 1.3.2.7 nathanw }
1428 1.3.2.2 nathanw
1429 1.3.2.7 nathanw /*
1430 1.3.2.7 nathanw * If that didn't work, check all BUSY/SELECTING SCB's for one
1431 1.3.2.7 nathanw * pointing to xs
1432 1.3.2.7 nathanw */
1433 1.3.2.2 nathanw
1434 1.3.2.7 nathanw for (i = 0, scb = sc->sc_scb; i < IHA_MAX_SCB; i++, scb++)
1435 1.3.2.7 nathanw switch (scb->status) {
1436 1.3.2.7 nathanw case STATUS_BUSY:
1437 1.3.2.7 nathanw case STATUS_SELECT:
1438 1.3.2.7 nathanw if (scb->xs == xs) {
1439 1.3.2.7 nathanw iha_append_done_scb(sc, scb, hastat);
1440 1.3.2.7 nathanw splx(s);
1441 1.3.2.7 nathanw return;
1442 1.3.2.2 nathanw }
1443 1.3.2.7 nathanw break;
1444 1.3.2.7 nathanw default:
1445 1.3.2.7 nathanw break;
1446 1.3.2.2 nathanw }
1447 1.3.2.2 nathanw
1448 1.3.2.7 nathanw splx(s);
1449 1.3.2.7 nathanw }
1450 1.3.2.2 nathanw
1451 1.3.2.7 nathanw /*
1452 1.3.2.7 nathanw * iha_data_over_run - return HOST_OK for all SCSI opcodes where BufLen
1453 1.3.2.7 nathanw * is an 'Allocation Length'. All other SCSI opcodes
1454 1.3.2.7 nathanw * get HOST_DO_DU as they SHOULD have xferred all the
1455 1.3.2.7 nathanw * data requested.
1456 1.3.2.7 nathanw *
1457 1.3.2.7 nathanw * The list of opcodes using 'Allocation Length' was
1458 1.3.2.7 nathanw * found by scanning all the SCSI-3 T10 drafts. See
1459 1.3.2.7 nathanw * www.t10.org for the curious with a .pdf reader.
1460 1.3.2.7 nathanw */
1461 1.3.2.7 nathanw static u_int8_t
1462 1.3.2.7 nathanw iha_data_over_run(scb)
1463 1.3.2.7 nathanw struct iha_scb *scb;
1464 1.3.2.7 nathanw {
1465 1.3.2.7 nathanw switch (scb->cmd[0]) {
1466 1.3.2.7 nathanw case 0x03: /* Request Sense SPC-2 */
1467 1.3.2.7 nathanw case 0x12: /* Inquiry SPC-2 */
1468 1.3.2.7 nathanw case 0x1a: /* Mode Sense (6 byte version) SPC-2 */
1469 1.3.2.7 nathanw case 0x1c: /* Receive Diagnostic Results SPC-2 */
1470 1.3.2.7 nathanw case 0x23: /* Read Format Capacities MMC-2 */
1471 1.3.2.7 nathanw case 0x29: /* Read Generation SBC */
1472 1.3.2.7 nathanw case 0x34: /* Read Position SSC-2 */
1473 1.3.2.7 nathanw case 0x37: /* Read Defect Data SBC */
1474 1.3.2.7 nathanw case 0x3c: /* Read Buffer SPC-2 */
1475 1.3.2.7 nathanw case 0x42: /* Read Sub Channel MMC-2 */
1476 1.3.2.7 nathanw case 0x43: /* Read TOC/PMA/ATIP MMC */
1477 1.3.2.7 nathanw
1478 1.3.2.7 nathanw /* XXX - 2 with same opcode of 0x44? */
1479 1.3.2.7 nathanw case 0x44: /* Read Header/Read Density Suprt MMC/SSC*/
1480 1.3.2.7 nathanw
1481 1.3.2.7 nathanw case 0x46: /* Get Configuration MMC-2 */
1482 1.3.2.7 nathanw case 0x4a: /* Get Event/Status Notification MMC-2 */
1483 1.3.2.7 nathanw case 0x4d: /* Log Sense SPC-2 */
1484 1.3.2.7 nathanw case 0x51: /* Read Disc Information MMC */
1485 1.3.2.7 nathanw case 0x52: /* Read Track Information MMC */
1486 1.3.2.7 nathanw case 0x59: /* Read Master CUE MMC */
1487 1.3.2.7 nathanw case 0x5a: /* Mode Sense (10 byte version) SPC-2 */
1488 1.3.2.7 nathanw case 0x5c: /* Read Buffer Capacity MMC */
1489 1.3.2.7 nathanw case 0x5e: /* Persistant Reserve In SPC-2 */
1490 1.3.2.7 nathanw case 0x84: /* Receive Copy Results SPC-2 */
1491 1.3.2.7 nathanw case 0xa0: /* Report LUNs SPC-2 */
1492 1.3.2.7 nathanw case 0xa3: /* Various Report requests SBC-2/SCC-2*/
1493 1.3.2.7 nathanw case 0xa4: /* Report Key MMC-2 */
1494 1.3.2.7 nathanw case 0xad: /* Read DVD Structure MMC-2 */
1495 1.3.2.7 nathanw case 0xb4: /* Read Element Status (Attached) SMC */
1496 1.3.2.7 nathanw case 0xb5: /* Request Volume Element Address SMC */
1497 1.3.2.7 nathanw case 0xb7: /* Read Defect Data (12 byte ver.) SBC */
1498 1.3.2.7 nathanw case 0xb8: /* Read Element Status (Independ.) SMC */
1499 1.3.2.7 nathanw case 0xba: /* Report Redundancy SCC-2 */
1500 1.3.2.7 nathanw case 0xbd: /* Mechanism Status MMC */
1501 1.3.2.7 nathanw case 0xbe: /* Report Basic Redundancy SCC-2 */
1502 1.3.2.7 nathanw
1503 1.3.2.7 nathanw return (HOST_OK);
1504 1.3.2.7 nathanw break;
1505 1.3.2.7 nathanw
1506 1.3.2.7 nathanw default:
1507 1.3.2.7 nathanw return (HOST_DO_DU);
1508 1.3.2.7 nathanw break;
1509 1.3.2.7 nathanw }
1510 1.3.2.2 nathanw }
1511 1.3.2.2 nathanw
1512 1.3.2.2 nathanw /*
1513 1.3.2.7 nathanw * iha_next_state - prcess the current SCB as requested in it's
1514 1.3.2.7 nathanw * nextstat member.
1515 1.3.2.2 nathanw */
1516 1.3.2.2 nathanw static int
1517 1.3.2.7 nathanw iha_next_state(sc)
1518 1.3.2.2 nathanw struct iha_softc *sc;
1519 1.3.2.2 nathanw {
1520 1.3.2.2 nathanw
1521 1.3.2.7 nathanw if (sc->sc_actscb == NULL)
1522 1.3.2.7 nathanw return (-1);
1523 1.3.2.2 nathanw
1524 1.3.2.7 nathanw switch (sc->sc_actscb->nextstat) {
1525 1.3.2.7 nathanw case 1:
1526 1.3.2.7 nathanw if (iha_state_1(sc) == 3)
1527 1.3.2.7 nathanw goto state_3;
1528 1.3.2.7 nathanw break;
1529 1.3.2.2 nathanw
1530 1.3.2.7 nathanw case 2:
1531 1.3.2.7 nathanw switch (iha_state_2(sc)) {
1532 1.3.2.7 nathanw case 3:
1533 1.3.2.7 nathanw goto state_3;
1534 1.3.2.7 nathanw case 4:
1535 1.3.2.7 nathanw goto state_4;
1536 1.3.2.7 nathanw default:
1537 1.3.2.2 nathanw break;
1538 1.3.2.7 nathanw }
1539 1.3.2.7 nathanw break;
1540 1.3.2.2 nathanw
1541 1.3.2.7 nathanw case 3:
1542 1.3.2.7 nathanw state_3:
1543 1.3.2.7 nathanw if (iha_state_3(sc) == 4)
1544 1.3.2.7 nathanw goto state_4;
1545 1.3.2.7 nathanw break;
1546 1.3.2.2 nathanw
1547 1.3.2.7 nathanw case 4:
1548 1.3.2.7 nathanw state_4:
1549 1.3.2.7 nathanw switch (iha_state_4(sc)) {
1550 1.3.2.7 nathanw case 0:
1551 1.3.2.7 nathanw return (0);
1552 1.3.2.7 nathanw case 6:
1553 1.3.2.7 nathanw goto state_6;
1554 1.3.2.7 nathanw default:
1555 1.3.2.2 nathanw break;
1556 1.3.2.7 nathanw }
1557 1.3.2.7 nathanw break;
1558 1.3.2.2 nathanw
1559 1.3.2.7 nathanw case 5:
1560 1.3.2.7 nathanw switch (iha_state_5(sc)) {
1561 1.3.2.7 nathanw case 4:
1562 1.3.2.7 nathanw goto state_4;
1563 1.3.2.7 nathanw case 6:
1564 1.3.2.7 nathanw goto state_6;
1565 1.3.2.2 nathanw default:
1566 1.3.2.7 nathanw break;
1567 1.3.2.2 nathanw }
1568 1.3.2.7 nathanw break;
1569 1.3.2.7 nathanw
1570 1.3.2.7 nathanw case 6:
1571 1.3.2.7 nathanw state_6:
1572 1.3.2.7 nathanw iha_state_6(sc);
1573 1.3.2.7 nathanw break;
1574 1.3.2.7 nathanw
1575 1.3.2.7 nathanw case 8:
1576 1.3.2.7 nathanw iha_state_8(sc);
1577 1.3.2.7 nathanw break;
1578 1.3.2.7 nathanw
1579 1.3.2.7 nathanw default:
1580 1.3.2.7 nathanw #ifdef IHA_DEBUG_STATE
1581 1.3.2.7 nathanw printf("[debug] -unknown state: %i-\n",
1582 1.3.2.7 nathanw sc->sc_actscb->nextstat);
1583 1.3.2.7 nathanw #endif
1584 1.3.2.7 nathanw iha_bad_seq(sc);
1585 1.3.2.7 nathanw break;
1586 1.3.2.2 nathanw }
1587 1.3.2.7 nathanw
1588 1.3.2.7 nathanw return (-1);
1589 1.3.2.2 nathanw }
1590 1.3.2.2 nathanw
1591 1.3.2.2 nathanw /*
1592 1.3.2.7 nathanw * iha_state_1 - selection is complete after a SELATNSTOP. If the target
1593 1.3.2.7 nathanw * has put the bus into MSG_OUT phase start wide/sync
1594 1.3.2.7 nathanw * negotiation. Otherwise clear the FIFO and go to state 3,
1595 1.3.2.7 nathanw * which will send the SCSI CDB to the target.
1596 1.3.2.2 nathanw */
1597 1.3.2.2 nathanw static int
1598 1.3.2.7 nathanw iha_state_1(sc)
1599 1.3.2.2 nathanw struct iha_softc *sc;
1600 1.3.2.2 nathanw {
1601 1.3.2.2 nathanw bus_space_tag_t iot = sc->sc_iot;
1602 1.3.2.2 nathanw bus_space_handle_t ioh = sc->sc_ioh;
1603 1.3.2.7 nathanw struct iha_scb *scb = sc->sc_actscb;
1604 1.3.2.7 nathanw struct tcs *tcs;
1605 1.3.2.7 nathanw int flags;
1606 1.3.2.2 nathanw
1607 1.3.2.7 nathanw iha_mark_busy_scb(scb);
1608 1.3.2.2 nathanw
1609 1.3.2.7 nathanw tcs = scb->tcs;
1610 1.3.2.2 nathanw
1611 1.3.2.7 nathanw bus_space_write_1(iot, ioh, TUL_SCONFIG0, tcs->sconfig0);
1612 1.3.2.2 nathanw
1613 1.3.2.7 nathanw /*
1614 1.3.2.7 nathanw * If we are in PHASE_MSG_OUT, send
1615 1.3.2.7 nathanw * a) IDENT message (with tags if appropriate)
1616 1.3.2.7 nathanw * b) WDTR if the target is configured to negotiate wide xfers
1617 1.3.2.7 nathanw * ** OR **
1618 1.3.2.7 nathanw * c) SDTR if the target is configured to negotiate sync xfers
1619 1.3.2.7 nathanw * but not wide ones
1620 1.3.2.7 nathanw *
1621 1.3.2.7 nathanw * If we are NOT, then the target is not asking for anything but
1622 1.3.2.7 nathanw * the data/command, so go straight to state 3.
1623 1.3.2.7 nathanw */
1624 1.3.2.7 nathanw if (sc->sc_phase == PHASE_MSG_OUT) {
1625 1.3.2.7 nathanw bus_space_write_1(iot, ioh, TUL_SCTRL1, (ESBUSIN | EHRSL));
1626 1.3.2.7 nathanw bus_space_write_1(iot, ioh, TUL_SFIFO, scb->scb_id);
1627 1.3.2.2 nathanw
1628 1.3.2.7 nathanw if (scb->scb_tagmsg != 0) {
1629 1.3.2.7 nathanw bus_space_write_1(iot, ioh, TUL_SFIFO,
1630 1.3.2.7 nathanw scb->scb_tagmsg);
1631 1.3.2.7 nathanw bus_space_write_1(iot, ioh, TUL_SFIFO,
1632 1.3.2.7 nathanw scb->scb_tagid);
1633 1.3.2.7 nathanw }
1634 1.3.2.2 nathanw
1635 1.3.2.7 nathanw flags = tcs->flags;
1636 1.3.2.7 nathanw if ((flags & FLAG_NO_NEG_WIDE) == 0) {
1637 1.3.2.7 nathanw if (iha_msgout_wdtr(sc) == -1)
1638 1.3.2.7 nathanw return (-1);
1639 1.3.2.7 nathanw } else if ((flags & FLAG_NO_NEG_SYNC) == 0) {
1640 1.3.2.7 nathanw if (iha_msgout_sdtr(sc) == -1)
1641 1.3.2.7 nathanw return (-1);
1642 1.3.2.7 nathanw }
1643 1.3.2.2 nathanw
1644 1.3.2.7 nathanw } else {
1645 1.3.2.7 nathanw bus_space_write_1(iot, ioh, TUL_SCTRL0, RSFIFO);
1646 1.3.2.7 nathanw iha_set_ssig(sc, REQ | BSY | SEL | ATN, 0);
1647 1.3.2.2 nathanw }
1648 1.3.2.2 nathanw
1649 1.3.2.7 nathanw return (3);
1650 1.3.2.2 nathanw }
1651 1.3.2.2 nathanw
1652 1.3.2.2 nathanw /*
1653 1.3.2.7 nathanw * iha_state_2 - selection is complete after a SEL_ATN or SEL_ATN3. If the SCSI
1654 1.3.2.7 nathanw * CDB has already been send, go to state 4 to start the data
1655 1.3.2.7 nathanw * xfer. Otherwise reset the FIFO and go to state 3, sending
1656 1.3.2.7 nathanw * the SCSI CDB.
1657 1.3.2.2 nathanw */
1658 1.3.2.2 nathanw static int
1659 1.3.2.7 nathanw iha_state_2(sc)
1660 1.3.2.2 nathanw struct iha_softc *sc;
1661 1.3.2.2 nathanw {
1662 1.3.2.2 nathanw bus_space_tag_t iot = sc->sc_iot;
1663 1.3.2.2 nathanw bus_space_handle_t ioh = sc->sc_ioh;
1664 1.3.2.7 nathanw struct iha_scb *scb = sc->sc_actscb;
1665 1.3.2.2 nathanw
1666 1.3.2.7 nathanw iha_mark_busy_scb(scb);
1667 1.3.2.2 nathanw
1668 1.3.2.7 nathanw bus_space_write_1(iot, ioh, TUL_SCONFIG0, scb->tcs->sconfig0);
1669 1.3.2.2 nathanw
1670 1.3.2.7 nathanw if ((sc->sc_status1 & CPDNE) != 0)
1671 1.3.2.7 nathanw return (4);
1672 1.3.2.2 nathanw
1673 1.3.2.7 nathanw bus_space_write_1(iot, ioh, TUL_SCTRL0, RSFIFO);
1674 1.3.2.2 nathanw
1675 1.3.2.7 nathanw iha_set_ssig(sc, REQ | BSY | SEL | ATN, 0);
1676 1.3.2.7 nathanw
1677 1.3.2.7 nathanw return (3);
1678 1.3.2.2 nathanw }
1679 1.3.2.2 nathanw
1680 1.3.2.7 nathanw /*
1681 1.3.2.7 nathanw * iha_state_3 - send the SCSI CDB to the target, processing any status
1682 1.3.2.7 nathanw * or other messages received until that is done or
1683 1.3.2.7 nathanw * abandoned.
1684 1.3.2.7 nathanw */
1685 1.3.2.2 nathanw static int
1686 1.3.2.7 nathanw iha_state_3(sc)
1687 1.3.2.2 nathanw struct iha_softc *sc;
1688 1.3.2.2 nathanw {
1689 1.3.2.2 nathanw bus_space_tag_t iot = sc->sc_iot;
1690 1.3.2.2 nathanw bus_space_handle_t ioh = sc->sc_ioh;
1691 1.3.2.7 nathanw struct iha_scb *scb = sc->sc_actscb;
1692 1.3.2.7 nathanw int flags;
1693 1.3.2.2 nathanw
1694 1.3.2.2 nathanw for (;;) {
1695 1.3.2.7 nathanw switch (sc->sc_phase) {
1696 1.3.2.7 nathanw case PHASE_CMD_OUT:
1697 1.3.2.7 nathanw bus_space_write_multi_1(iot, ioh, TUL_SFIFO,
1698 1.3.2.7 nathanw scb->cmd, scb->cmdlen);
1699 1.3.2.7 nathanw if (iha_wait(sc, XF_FIFO_OUT) == -1)
1700 1.3.2.7 nathanw return (-1);
1701 1.3.2.7 nathanw else if (sc->sc_phase == PHASE_CMD_OUT) {
1702 1.3.2.7 nathanw iha_bad_seq(sc);
1703 1.3.2.7 nathanw return (-1);
1704 1.3.2.7 nathanw } else
1705 1.3.2.7 nathanw return (4);
1706 1.3.2.2 nathanw
1707 1.3.2.7 nathanw case PHASE_MSG_IN:
1708 1.3.2.7 nathanw scb->nextstat = 3;
1709 1.3.2.7 nathanw if (iha_msgin(sc) == -1)
1710 1.3.2.7 nathanw return (-1);
1711 1.3.2.7 nathanw break;
1712 1.3.2.2 nathanw
1713 1.3.2.7 nathanw case PHASE_STATUS_IN:
1714 1.3.2.7 nathanw if (iha_status_msg(sc) == -1)
1715 1.3.2.7 nathanw return (-1);
1716 1.3.2.7 nathanw break;
1717 1.3.2.7 nathanw
1718 1.3.2.7 nathanw case PHASE_MSG_OUT:
1719 1.3.2.7 nathanw flags = scb->tcs->flags;
1720 1.3.2.7 nathanw if ((flags & FLAG_NO_NEG_SYNC) != 0) {
1721 1.3.2.7 nathanw if (iha_msgout(sc, MSG_NOOP) == -1)
1722 1.3.2.7 nathanw return (-1);
1723 1.3.2.7 nathanw } else if (iha_msgout_sdtr(sc) == -1)
1724 1.3.2.7 nathanw return (-1);
1725 1.3.2.2 nathanw break;
1726 1.3.2.2 nathanw
1727 1.3.2.2 nathanw default:
1728 1.3.2.7 nathanw printf("[debug] -s3- bad phase = %d\n", sc->sc_phase);
1729 1.3.2.7 nathanw iha_bad_seq(sc);
1730 1.3.2.7 nathanw return (-1);
1731 1.3.2.2 nathanw }
1732 1.3.2.2 nathanw }
1733 1.3.2.2 nathanw }
1734 1.3.2.2 nathanw
1735 1.3.2.7 nathanw /*
1736 1.3.2.7 nathanw * iha_state_4 - start a data xfer. Handle any bus state
1737 1.3.2.7 nathanw * transitions until PHASE_DATA_IN/_OUT
1738 1.3.2.7 nathanw * or the attempt is abandoned. If there is
1739 1.3.2.7 nathanw * no data to xfer, go to state 6 and finish
1740 1.3.2.7 nathanw * processing the current SCB.
1741 1.3.2.7 nathanw */
1742 1.3.2.2 nathanw static int
1743 1.3.2.7 nathanw iha_state_4(sc)
1744 1.3.2.2 nathanw struct iha_softc *sc;
1745 1.3.2.2 nathanw {
1746 1.3.2.7 nathanw struct iha_scb *scb = sc->sc_actscb;
1747 1.3.2.2 nathanw
1748 1.3.2.7 nathanw if ((scb->flags & (FLAG_DATAIN | FLAG_DATAOUT)) ==
1749 1.3.2.7 nathanw (FLAG_DATAIN | FLAG_DATAOUT))
1750 1.3.2.7 nathanw return (6); /* Both dir flags set => NO xfer was requested */
1751 1.3.2.2 nathanw
1752 1.3.2.2 nathanw for (;;) {
1753 1.3.2.7 nathanw if (scb->buflen == 0)
1754 1.3.2.7 nathanw return (6);
1755 1.3.2.2 nathanw
1756 1.3.2.7 nathanw switch (sc->sc_phase) {
1757 1.3.2.7 nathanw case PHASE_STATUS_IN:
1758 1.3.2.7 nathanw if ((scb->flags & (FLAG_DATAIN | FLAG_DATAOUT)) != 0)
1759 1.3.2.7 nathanw scb->ha_stat = iha_data_over_run(scb);
1760 1.3.2.7 nathanw if ((iha_status_msg(sc)) == -1)
1761 1.3.2.7 nathanw return (-1);
1762 1.3.2.7 nathanw break;
1763 1.3.2.2 nathanw
1764 1.3.2.7 nathanw case PHASE_MSG_IN:
1765 1.3.2.7 nathanw scb->nextstat = 4;
1766 1.3.2.7 nathanw if (iha_msgin(sc) == -1)
1767 1.3.2.7 nathanw return (-1);
1768 1.3.2.7 nathanw break;
1769 1.3.2.2 nathanw
1770 1.3.2.7 nathanw case PHASE_MSG_OUT:
1771 1.3.2.7 nathanw if ((sc->sc_status0 & SPERR) != 0) {
1772 1.3.2.7 nathanw scb->buflen = 0;
1773 1.3.2.7 nathanw scb->ha_stat = HOST_SPERR;
1774 1.3.2.7 nathanw if (iha_msgout(sc, MSG_INITIATOR_DET_ERR) == -1)
1775 1.3.2.7 nathanw return (-1);
1776 1.3.2.7 nathanw else
1777 1.3.2.7 nathanw return (6);
1778 1.3.2.7 nathanw } else {
1779 1.3.2.7 nathanw if (iha_msgout(sc, MSG_NOOP) == -1)
1780 1.3.2.7 nathanw return (-1);
1781 1.3.2.7 nathanw }
1782 1.3.2.2 nathanw break;
1783 1.3.2.2 nathanw
1784 1.3.2.7 nathanw case PHASE_DATA_IN:
1785 1.3.2.7 nathanw return (iha_xfer_data(sc, scb, FLAG_DATAIN));
1786 1.3.2.7 nathanw
1787 1.3.2.7 nathanw case PHASE_DATA_OUT:
1788 1.3.2.7 nathanw return (iha_xfer_data(sc, scb, FLAG_DATAOUT));
1789 1.3.2.7 nathanw
1790 1.3.2.2 nathanw default:
1791 1.3.2.7 nathanw iha_bad_seq(sc);
1792 1.3.2.7 nathanw return (-1);
1793 1.3.2.2 nathanw }
1794 1.3.2.2 nathanw }
1795 1.3.2.2 nathanw }
1796 1.3.2.2 nathanw
1797 1.3.2.7 nathanw /*
1798 1.3.2.7 nathanw * iha_state_5 - handle the partial or final completion of the current
1799 1.3.2.7 nathanw * data xfer. If DMA is still active stop it. If there is
1800 1.3.2.7 nathanw * more data to xfer, go to state 4 and start the xfer.
1801 1.3.2.7 nathanw * If not go to state 6 and finish the SCB.
1802 1.3.2.7 nathanw */
1803 1.3.2.2 nathanw static int
1804 1.3.2.7 nathanw iha_state_5(sc)
1805 1.3.2.2 nathanw struct iha_softc *sc;
1806 1.3.2.2 nathanw {
1807 1.3.2.2 nathanw bus_space_tag_t iot = sc->sc_iot;
1808 1.3.2.2 nathanw bus_space_handle_t ioh = sc->sc_ioh;
1809 1.3.2.7 nathanw struct iha_scb *scb = sc->sc_actscb;
1810 1.3.2.7 nathanw struct iha_sg_element *sg;
1811 1.3.2.7 nathanw u_int32_t cnt;
1812 1.3.2.7 nathanw u_int8_t period, stat;
1813 1.3.2.7 nathanw long xcnt; /* cannot use unsigned!! see code: if (xcnt < 0) */
1814 1.3.2.7 nathanw int i;
1815 1.3.2.2 nathanw
1816 1.3.2.7 nathanw cnt = bus_space_read_4(iot, ioh, TUL_STCNT0) & TCNT;
1817 1.3.2.2 nathanw
1818 1.3.2.7 nathanw /*
1819 1.3.2.7 nathanw * Stop any pending DMA activity and check for parity error.
1820 1.3.2.7 nathanw */
1821 1.3.2.2 nathanw
1822 1.3.2.7 nathanw if ((bus_space_read_1(iot, ioh, TUL_DCMD) & XDIR) != 0) {
1823 1.3.2.7 nathanw /* Input Operation */
1824 1.3.2.7 nathanw if ((sc->sc_status0 & SPERR) != 0)
1825 1.3.2.7 nathanw scb->ha_stat = HOST_SPERR;
1826 1.3.2.2 nathanw
1827 1.3.2.7 nathanw if ((bus_space_read_1(iot, ioh, TUL_ISTUS1) & XPEND) != 0) {
1828 1.3.2.7 nathanw bus_space_write_1(iot, ioh, TUL_DCTRL0,
1829 1.3.2.7 nathanw bus_space_read_1(iot, ioh, TUL_DCTRL0) | SXSTP);
1830 1.3.2.7 nathanw while (bus_space_read_1(iot, ioh, TUL_ISTUS1) & XPEND)
1831 1.3.2.7 nathanw ;
1832 1.3.2.7 nathanw }
1833 1.3.2.2 nathanw
1834 1.3.2.7 nathanw } else {
1835 1.3.2.7 nathanw /* Output Operation */
1836 1.3.2.7 nathanw if ((sc->sc_status1 & SXCMP) == 0) {
1837 1.3.2.7 nathanw period = scb->tcs->syncm;
1838 1.3.2.7 nathanw if ((period & PERIOD_WIDE_SCSI) != 0)
1839 1.3.2.7 nathanw cnt += (bus_space_read_1(iot, ioh,
1840 1.3.2.7 nathanw TUL_SFIFOCNT) & FIFOC) * 2;
1841 1.3.2.7 nathanw else
1842 1.3.2.7 nathanw cnt += bus_space_read_1(iot, ioh,
1843 1.3.2.7 nathanw TUL_SFIFOCNT) & FIFOC;
1844 1.3.2.7 nathanw }
1845 1.3.2.2 nathanw
1846 1.3.2.7 nathanw if ((bus_space_read_1(iot, ioh, TUL_ISTUS1) & XPEND) != 0) {
1847 1.3.2.7 nathanw bus_space_write_1(iot, ioh, TUL_DCMD, ABTXFR);
1848 1.3.2.7 nathanw do
1849 1.3.2.7 nathanw stat = bus_space_read_1(iot, ioh, TUL_ISTUS0);
1850 1.3.2.7 nathanw while ((stat & DABT) == 0);
1851 1.3.2.7 nathanw }
1852 1.3.2.2 nathanw
1853 1.3.2.7 nathanw if ((cnt == 1) && (sc->sc_phase == PHASE_DATA_OUT)) {
1854 1.3.2.7 nathanw if (iha_wait(sc, XF_FIFO_OUT) == -1)
1855 1.3.2.2 nathanw return (-1);
1856 1.3.2.7 nathanw cnt = 0;
1857 1.3.2.2 nathanw
1858 1.3.2.7 nathanw } else if ((sc->sc_status1 & SXCMP) == 0)
1859 1.3.2.2 nathanw bus_space_write_1(iot, ioh, TUL_SCTRL0, RSFIFO);
1860 1.3.2.7 nathanw }
1861 1.3.2.2 nathanw
1862 1.3.2.7 nathanw if (cnt == 0) {
1863 1.3.2.7 nathanw scb->buflen = 0;
1864 1.3.2.7 nathanw return (6);
1865 1.3.2.2 nathanw }
1866 1.3.2.2 nathanw
1867 1.3.2.7 nathanw /* Update active data pointer and restart the I/O at the new point */
1868 1.3.2.2 nathanw
1869 1.3.2.7 nathanw xcnt = scb->buflen - cnt; /* xcnt == bytes xferred */
1870 1.3.2.7 nathanw scb->buflen = cnt; /* cnt == bytes left */
1871 1.3.2.7 nathanw
1872 1.3.2.7 nathanw if ((scb->flags & FLAG_SG) != 0) {
1873 1.3.2.7 nathanw sg = &scb->sglist[scb->sg_index];
1874 1.3.2.7 nathanw for (i = scb->sg_index; i < scb->sg_max; sg++, i++) {
1875 1.3.2.7 nathanw xcnt -= le32toh(sg->sg_len);
1876 1.3.2.7 nathanw if (xcnt < 0) {
1877 1.3.2.7 nathanw xcnt += le32toh(sg->sg_len);
1878 1.3.2.7 nathanw
1879 1.3.2.7 nathanw sg->sg_addr =
1880 1.3.2.7 nathanw htole32(le32toh(sg->sg_addr) + xcnt);
1881 1.3.2.7 nathanw sg->sg_len =
1882 1.3.2.7 nathanw htole32(le32toh(sg->sg_len) - xcnt);
1883 1.3.2.7 nathanw bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap,
1884 1.3.2.7 nathanw scb->sgoffset, IHA_SG_SIZE,
1885 1.3.2.7 nathanw BUS_DMASYNC_PREWRITE);
1886 1.3.2.2 nathanw
1887 1.3.2.7 nathanw scb->bufaddr += (i - scb->sg_index) *
1888 1.3.2.7 nathanw sizeof(struct iha_sg_element);
1889 1.3.2.7 nathanw scb->sg_size = scb->sg_max - i;
1890 1.3.2.7 nathanw scb->sg_index = i;
1891 1.3.2.2 nathanw
1892 1.3.2.7 nathanw return (4);
1893 1.3.2.7 nathanw }
1894 1.3.2.7 nathanw }
1895 1.3.2.7 nathanw return (6);
1896 1.3.2.2 nathanw
1897 1.3.2.7 nathanw } else
1898 1.3.2.7 nathanw scb->bufaddr += xcnt;
1899 1.3.2.7 nathanw
1900 1.3.2.7 nathanw return (4);
1901 1.3.2.2 nathanw }
1902 1.3.2.2 nathanw
1903 1.3.2.7 nathanw /*
1904 1.3.2.7 nathanw * iha_state_6 - finish off the active scb (may require several
1905 1.3.2.7 nathanw * iterations if PHASE_MSG_IN) and return -1 to indicate
1906 1.3.2.7 nathanw * the bus is free.
1907 1.3.2.7 nathanw */
1908 1.3.2.7 nathanw static int
1909 1.3.2.7 nathanw iha_state_6(sc)
1910 1.3.2.2 nathanw struct iha_softc *sc;
1911 1.3.2.2 nathanw {
1912 1.3.2.2 nathanw
1913 1.3.2.7 nathanw for (;;) {
1914 1.3.2.7 nathanw switch (sc->sc_phase) {
1915 1.3.2.7 nathanw case PHASE_STATUS_IN:
1916 1.3.2.7 nathanw if (iha_status_msg(sc) == -1)
1917 1.3.2.7 nathanw return (-1);
1918 1.3.2.7 nathanw break;
1919 1.3.2.2 nathanw
1920 1.3.2.7 nathanw case PHASE_MSG_IN:
1921 1.3.2.7 nathanw sc->sc_actscb->nextstat = 6;
1922 1.3.2.7 nathanw if ((iha_msgin(sc)) == -1)
1923 1.3.2.7 nathanw return (-1);
1924 1.3.2.2 nathanw break;
1925 1.3.2.2 nathanw
1926 1.3.2.7 nathanw case PHASE_MSG_OUT:
1927 1.3.2.7 nathanw if ((iha_msgout(sc, MSG_NOOP)) == -1)
1928 1.3.2.7 nathanw return (-1);
1929 1.3.2.2 nathanw break;
1930 1.3.2.2 nathanw
1931 1.3.2.7 nathanw case PHASE_DATA_IN:
1932 1.3.2.7 nathanw if (iha_xpad_in(sc) == -1)
1933 1.3.2.7 nathanw return (-1);
1934 1.3.2.2 nathanw break;
1935 1.3.2.2 nathanw
1936 1.3.2.7 nathanw case PHASE_DATA_OUT:
1937 1.3.2.7 nathanw if (iha_xpad_out(sc) == -1)
1938 1.3.2.7 nathanw return (-1);
1939 1.3.2.7 nathanw break;
1940 1.3.2.2 nathanw
1941 1.3.2.7 nathanw default:
1942 1.3.2.7 nathanw iha_bad_seq(sc);
1943 1.3.2.7 nathanw return (-1);
1944 1.3.2.7 nathanw }
1945 1.3.2.7 nathanw }
1946 1.3.2.2 nathanw }
1947 1.3.2.2 nathanw
1948 1.3.2.2 nathanw /*
1949 1.3.2.7 nathanw * iha_state_8 - reset the active device and all busy SCBs using it
1950 1.3.2.2 nathanw */
1951 1.3.2.2 nathanw static int
1952 1.3.2.7 nathanw iha_state_8(sc)
1953 1.3.2.2 nathanw struct iha_softc *sc;
1954 1.3.2.2 nathanw {
1955 1.3.2.2 nathanw bus_space_tag_t iot = sc->sc_iot;
1956 1.3.2.2 nathanw bus_space_handle_t ioh = sc->sc_ioh;
1957 1.3.2.7 nathanw struct iha_scb *scb;
1958 1.3.2.7 nathanw int i;
1959 1.3.2.7 nathanw u_int8_t tar;
1960 1.3.2.2 nathanw
1961 1.3.2.7 nathanw if (sc->sc_phase == PHASE_MSG_OUT) {
1962 1.3.2.7 nathanw bus_space_write_1(iot, ioh, TUL_SFIFO, MSG_BUS_DEV_RESET);
1963 1.3.2.2 nathanw
1964 1.3.2.7 nathanw scb = sc->sc_actscb;
1965 1.3.2.2 nathanw
1966 1.3.2.7 nathanw /* This SCB finished correctly -- resetting the device */
1967 1.3.2.7 nathanw iha_append_done_scb(sc, scb, HOST_OK);
1968 1.3.2.2 nathanw
1969 1.3.2.7 nathanw iha_reset_tcs(scb->tcs, sc->sc_sconf1);
1970 1.3.2.2 nathanw
1971 1.3.2.7 nathanw tar = scb->target;
1972 1.3.2.7 nathanw for (i = 0, scb = sc->sc_scb; i < IHA_MAX_SCB; i++, scb++)
1973 1.3.2.7 nathanw if (scb->target == tar)
1974 1.3.2.7 nathanw switch (scb->status) {
1975 1.3.2.7 nathanw case STATUS_BUSY:
1976 1.3.2.7 nathanw iha_append_done_scb(sc,
1977 1.3.2.7 nathanw scb, HOST_DEV_RST);
1978 1.3.2.7 nathanw break;
1979 1.3.2.2 nathanw
1980 1.3.2.7 nathanw case STATUS_SELECT:
1981 1.3.2.7 nathanw iha_push_pend_scb(sc, scb);
1982 1.3.2.7 nathanw break;
1983 1.3.2.2 nathanw
1984 1.3.2.7 nathanw default:
1985 1.3.2.7 nathanw break;
1986 1.3.2.7 nathanw }
1987 1.3.2.2 nathanw
1988 1.3.2.7 nathanw sc->sc_flags |= FLAG_EXPECT_DISC;
1989 1.3.2.2 nathanw
1990 1.3.2.7 nathanw if (iha_wait(sc, XF_FIFO_OUT) == -1)
1991 1.3.2.2 nathanw return (-1);
1992 1.3.2.2 nathanw }
1993 1.3.2.2 nathanw
1994 1.3.2.7 nathanw iha_bad_seq(sc);
1995 1.3.2.7 nathanw return (-1);
1996 1.3.2.2 nathanw }
1997 1.3.2.2 nathanw
1998 1.3.2.7 nathanw /*
1999 1.3.2.7 nathanw * iha_xfer_data - initiate the DMA xfer of the data
2000 1.3.2.7 nathanw */
2001 1.3.2.2 nathanw static int
2002 1.3.2.7 nathanw iha_xfer_data(sc, scb, direction)
2003 1.3.2.2 nathanw struct iha_softc *sc;
2004 1.3.2.7 nathanw struct iha_scb *scb;
2005 1.3.2.7 nathanw int direction;
2006 1.3.2.2 nathanw {
2007 1.3.2.2 nathanw bus_space_tag_t iot = sc->sc_iot;
2008 1.3.2.2 nathanw bus_space_handle_t ioh = sc->sc_ioh;
2009 1.3.2.7 nathanw u_int32_t xferlen;
2010 1.3.2.7 nathanw u_int8_t xfercmd;
2011 1.3.2.2 nathanw
2012 1.3.2.7 nathanw if ((scb->flags & (FLAG_DATAIN | FLAG_DATAOUT)) != direction)
2013 1.3.2.7 nathanw return (6); /* wrong direction, abandon I/O */
2014 1.3.2.2 nathanw
2015 1.3.2.7 nathanw bus_space_write_4(iot, ioh, TUL_STCNT0, scb->buflen);
2016 1.3.2.2 nathanw
2017 1.3.2.7 nathanw xfercmd = STRXFR;
2018 1.3.2.7 nathanw if (direction == FLAG_DATAIN)
2019 1.3.2.7 nathanw xfercmd |= XDIR;
2020 1.3.2.2 nathanw
2021 1.3.2.7 nathanw if (scb->flags & FLAG_SG) {
2022 1.3.2.7 nathanw xferlen = scb->sg_size * sizeof(struct iha_sg_element);
2023 1.3.2.7 nathanw xfercmd |= SGXFR;
2024 1.3.2.7 nathanw } else
2025 1.3.2.7 nathanw xferlen = scb->buflen;
2026 1.3.2.2 nathanw
2027 1.3.2.7 nathanw bus_space_write_4(iot, ioh, TUL_DXC, xferlen);
2028 1.3.2.7 nathanw bus_space_write_4(iot, ioh, TUL_DXPA, scb->bufaddr);
2029 1.3.2.7 nathanw bus_space_write_1(iot, ioh, TUL_DCMD, xfercmd);
2030 1.3.2.7 nathanw
2031 1.3.2.7 nathanw bus_space_write_1(iot, ioh, TUL_SCMD,
2032 1.3.2.7 nathanw (direction == FLAG_DATAIN) ? XF_DMA_IN : XF_DMA_OUT);
2033 1.3.2.7 nathanw
2034 1.3.2.7 nathanw scb->nextstat = 5;
2035 1.3.2.7 nathanw
2036 1.3.2.7 nathanw return (0);
2037 1.3.2.2 nathanw }
2038 1.3.2.2 nathanw
2039 1.3.2.2 nathanw static int
2040 1.3.2.7 nathanw iha_xpad_in(sc)
2041 1.3.2.2 nathanw struct iha_softc *sc;
2042 1.3.2.2 nathanw {
2043 1.3.2.2 nathanw bus_space_tag_t iot = sc->sc_iot;
2044 1.3.2.2 nathanw bus_space_handle_t ioh = sc->sc_ioh;
2045 1.3.2.7 nathanw struct iha_scb *scb = sc->sc_actscb;
2046 1.3.2.2 nathanw
2047 1.3.2.7 nathanw if ((scb->flags & (FLAG_DATAIN | FLAG_DATAOUT)) != 0)
2048 1.3.2.7 nathanw scb->ha_stat = HOST_DO_DU;
2049 1.3.2.2 nathanw
2050 1.3.2.7 nathanw for (;;) {
2051 1.3.2.7 nathanw if ((scb->tcs->syncm & PERIOD_WIDE_SCSI) != 0)
2052 1.3.2.7 nathanw bus_space_write_4(iot, ioh, TUL_STCNT0, 2);
2053 1.3.2.7 nathanw else
2054 1.3.2.7 nathanw bus_space_write_4(iot, ioh, TUL_STCNT0, 1);
2055 1.3.2.2 nathanw
2056 1.3.2.7 nathanw switch (iha_wait(sc, XF_FIFO_IN)) {
2057 1.3.2.7 nathanw case -1:
2058 1.3.2.7 nathanw return (-1);
2059 1.3.2.7 nathanw
2060 1.3.2.7 nathanw case PHASE_DATA_IN:
2061 1.3.2.3 nathanw bus_space_read_1(iot, ioh, TUL_SFIFO);
2062 1.3.2.7 nathanw break;
2063 1.3.2.2 nathanw
2064 1.3.2.7 nathanw default:
2065 1.3.2.7 nathanw bus_space_write_1(iot, ioh, TUL_SCTRL0, RSFIFO);
2066 1.3.2.7 nathanw return (6);
2067 1.3.2.3 nathanw }
2068 1.3.2.2 nathanw }
2069 1.3.2.2 nathanw }
2070 1.3.2.2 nathanw
2071 1.3.2.2 nathanw static int
2072 1.3.2.7 nathanw iha_xpad_out(sc)
2073 1.3.2.2 nathanw struct iha_softc *sc;
2074 1.3.2.2 nathanw {
2075 1.3.2.2 nathanw bus_space_tag_t iot = sc->sc_iot;
2076 1.3.2.2 nathanw bus_space_handle_t ioh = sc->sc_ioh;
2077 1.3.2.7 nathanw struct iha_scb *scb = sc->sc_actscb;
2078 1.3.2.2 nathanw
2079 1.3.2.7 nathanw if ((scb->flags & (FLAG_DATAIN | FLAG_DATAOUT)) != 0)
2080 1.3.2.7 nathanw scb->ha_stat = HOST_DO_DU;
2081 1.3.2.2 nathanw
2082 1.3.2.7 nathanw for (;;) {
2083 1.3.2.7 nathanw if ((scb->tcs->syncm & PERIOD_WIDE_SCSI) != 0)
2084 1.3.2.7 nathanw bus_space_write_4(iot, ioh, TUL_STCNT0, 2);
2085 1.3.2.7 nathanw else
2086 1.3.2.7 nathanw bus_space_write_4(iot, ioh, TUL_STCNT0, 1);
2087 1.3.2.2 nathanw
2088 1.3.2.7 nathanw bus_space_write_1(iot, ioh, TUL_SFIFO, 0);
2089 1.3.2.2 nathanw
2090 1.3.2.7 nathanw switch (iha_wait(sc, XF_FIFO_OUT)) {
2091 1.3.2.7 nathanw case -1:
2092 1.3.2.2 nathanw return (-1);
2093 1.3.2.2 nathanw
2094 1.3.2.7 nathanw case PHASE_DATA_OUT:
2095 1.3.2.2 nathanw break;
2096 1.3.2.2 nathanw
2097 1.3.2.7 nathanw default:
2098 1.3.2.7 nathanw /* Disable wide CPU to allow read 16 bits */
2099 1.3.2.7 nathanw bus_space_write_1(iot, ioh, TUL_SCTRL1, EHRSL);
2100 1.3.2.7 nathanw bus_space_write_1(iot, ioh, TUL_SCTRL0, RSFIFO);
2101 1.3.2.7 nathanw return (6);
2102 1.3.2.2 nathanw }
2103 1.3.2.7 nathanw }
2104 1.3.2.2 nathanw }
2105 1.3.2.2 nathanw
2106 1.3.2.2 nathanw static int
2107 1.3.2.7 nathanw iha_status_msg(sc)
2108 1.3.2.2 nathanw struct iha_softc *sc;
2109 1.3.2.2 nathanw {
2110 1.3.2.7 nathanw bus_space_tag_t iot = sc->sc_iot;
2111 1.3.2.7 nathanw bus_space_handle_t ioh = sc->sc_ioh;
2112 1.3.2.7 nathanw struct iha_scb *scb;
2113 1.3.2.7 nathanw u_int8_t msg;
2114 1.3.2.7 nathanw int phase;
2115 1.3.2.2 nathanw
2116 1.3.2.7 nathanw if ((phase = iha_wait(sc, CMD_COMP)) == -1)
2117 1.3.2.7 nathanw return (-1);
2118 1.3.2.2 nathanw
2119 1.3.2.7 nathanw scb = sc->sc_actscb;
2120 1.3.2.2 nathanw
2121 1.3.2.7 nathanw scb->ta_stat = bus_space_read_1(iot, ioh, TUL_SFIFO);
2122 1.3.2.2 nathanw
2123 1.3.2.7 nathanw if (phase == PHASE_MSG_OUT) {
2124 1.3.2.7 nathanw if ((sc->sc_status0 & SPERR) == 0)
2125 1.3.2.7 nathanw bus_space_write_1(iot, ioh, TUL_SFIFO, MSG_NOOP);
2126 1.3.2.7 nathanw else
2127 1.3.2.7 nathanw bus_space_write_1(iot, ioh, TUL_SFIFO,
2128 1.3.2.7 nathanw MSG_PARITY_ERROR);
2129 1.3.2.2 nathanw
2130 1.3.2.7 nathanw return (iha_wait(sc, XF_FIFO_OUT));
2131 1.3.2.2 nathanw
2132 1.3.2.7 nathanw } else if (phase == PHASE_MSG_IN) {
2133 1.3.2.7 nathanw msg = bus_space_read_1(iot, ioh, TUL_SFIFO);
2134 1.3.2.2 nathanw
2135 1.3.2.7 nathanw if ((sc->sc_status0 & SPERR) != 0)
2136 1.3.2.7 nathanw switch (iha_wait(sc, MSG_ACCEPT)) {
2137 1.3.2.7 nathanw case -1:
2138 1.3.2.7 nathanw return (-1);
2139 1.3.2.7 nathanw case PHASE_MSG_OUT:
2140 1.3.2.7 nathanw bus_space_write_1(iot, ioh, TUL_SFIFO,
2141 1.3.2.7 nathanw MSG_PARITY_ERROR);
2142 1.3.2.7 nathanw return (iha_wait(sc, XF_FIFO_OUT));
2143 1.3.2.7 nathanw default:
2144 1.3.2.7 nathanw iha_bad_seq(sc);
2145 1.3.2.7 nathanw return (-1);
2146 1.3.2.7 nathanw }
2147 1.3.2.2 nathanw
2148 1.3.2.7 nathanw if (msg == MSG_CMDCOMPLETE) {
2149 1.3.2.7 nathanw if ((scb->ta_stat &
2150 1.3.2.7 nathanw (SCSI_INTERM | SCSI_BUSY)) == SCSI_INTERM) {
2151 1.3.2.7 nathanw iha_bad_seq(sc);
2152 1.3.2.7 nathanw return (-1);
2153 1.3.2.7 nathanw }
2154 1.3.2.7 nathanw sc->sc_flags |= FLAG_EXPECT_DONE_DISC;
2155 1.3.2.7 nathanw bus_space_write_1(iot, ioh, TUL_SCTRL0, RSFIFO);
2156 1.3.2.7 nathanw return (iha_wait(sc, MSG_ACCEPT));
2157 1.3.2.7 nathanw }
2158 1.3.2.2 nathanw
2159 1.3.2.7 nathanw if ((msg == MSG_LINK_CMD_COMPLETE)
2160 1.3.2.7 nathanw || (msg == MSG_LINK_CMD_COMPLETEF)) {
2161 1.3.2.7 nathanw if ((scb->ta_stat &
2162 1.3.2.7 nathanw (SCSI_INTERM | SCSI_BUSY)) == SCSI_INTERM)
2163 1.3.2.7 nathanw return (iha_wait(sc, MSG_ACCEPT));
2164 1.3.2.7 nathanw }
2165 1.3.2.7 nathanw }
2166 1.3.2.2 nathanw
2167 1.3.2.7 nathanw iha_bad_seq(sc);
2168 1.3.2.7 nathanw return (-1);
2169 1.3.2.2 nathanw }
2170 1.3.2.2 nathanw
2171 1.3.2.7 nathanw /*
2172 1.3.2.7 nathanw * iha_busfree - SCSI bus free detected as a result of a TIMEOUT or
2173 1.3.2.7 nathanw * DISCONNECT interrupt. Reset the tulip FIFO and
2174 1.3.2.7 nathanw * SCONFIG0 and enable hardware reselect. Move any active
2175 1.3.2.7 nathanw * SCB to sc_donescb list. Return an appropriate host status
2176 1.3.2.7 nathanw * if an I/O was active.
2177 1.3.2.7 nathanw */
2178 1.3.2.2 nathanw static void
2179 1.3.2.7 nathanw iha_busfree(sc)
2180 1.3.2.2 nathanw struct iha_softc *sc;
2181 1.3.2.2 nathanw {
2182 1.3.2.7 nathanw bus_space_tag_t iot = sc->sc_iot;
2183 1.3.2.7 nathanw bus_space_handle_t ioh = sc->sc_ioh;
2184 1.3.2.7 nathanw struct iha_scb *scb;
2185 1.3.2.2 nathanw
2186 1.3.2.7 nathanw bus_space_write_1(iot, ioh, TUL_SCTRL0, RSFIFO);
2187 1.3.2.7 nathanw bus_space_write_1(iot, ioh, TUL_SCONFIG0, SCONFIG0DEFAULT);
2188 1.3.2.7 nathanw bus_space_write_1(iot, ioh, TUL_SCTRL1, EHRSL);
2189 1.3.2.2 nathanw
2190 1.3.2.7 nathanw scb = sc->sc_actscb;
2191 1.3.2.2 nathanw
2192 1.3.2.7 nathanw if (scb != NULL) {
2193 1.3.2.7 nathanw if (scb->status == STATUS_SELECT)
2194 1.3.2.7 nathanw /* selection timeout */
2195 1.3.2.7 nathanw iha_append_done_scb(sc, scb, HOST_SEL_TOUT);
2196 1.3.2.7 nathanw else
2197 1.3.2.7 nathanw /* Unexpected bus free */
2198 1.3.2.7 nathanw iha_append_done_scb(sc, scb, HOST_BAD_PHAS);
2199 1.3.2.2 nathanw }
2200 1.3.2.2 nathanw }
2201 1.3.2.2 nathanw
2202 1.3.2.7 nathanw /*
2203 1.3.2.7 nathanw * iha_resel - handle a detected SCSI bus reselection request.
2204 1.3.2.7 nathanw */
2205 1.3.2.2 nathanw static int
2206 1.3.2.7 nathanw iha_resel(sc)
2207 1.3.2.2 nathanw struct iha_softc *sc;
2208 1.3.2.2 nathanw {
2209 1.3.2.7 nathanw bus_space_tag_t iot = sc->sc_iot;
2210 1.3.2.7 nathanw bus_space_handle_t ioh = sc->sc_ioh;
2211 1.3.2.7 nathanw struct iha_scb *scb;
2212 1.3.2.7 nathanw struct tcs *tcs;
2213 1.3.2.7 nathanw u_int8_t tag, target, lun, msg, abortmsg;
2214 1.3.2.2 nathanw
2215 1.3.2.7 nathanw if (sc->sc_actscb != NULL) {
2216 1.3.2.7 nathanw if ((sc->sc_actscb->status == STATUS_SELECT))
2217 1.3.2.7 nathanw iha_push_pend_scb(sc, sc->sc_actscb);
2218 1.3.2.7 nathanw sc->sc_actscb = NULL;
2219 1.3.2.7 nathanw }
2220 1.3.2.2 nathanw
2221 1.3.2.7 nathanw target = bus_space_read_1(iot, ioh, TUL_SBID);
2222 1.3.2.7 nathanw lun = bus_space_read_1(iot, ioh, TUL_SALVC) & MSG_IDENTIFY_LUNMASK;
2223 1.3.2.2 nathanw
2224 1.3.2.7 nathanw tcs = &sc->sc_tcs[target];
2225 1.3.2.2 nathanw
2226 1.3.2.7 nathanw bus_space_write_1(iot, ioh, TUL_SCONFIG0, tcs->sconfig0);
2227 1.3.2.7 nathanw bus_space_write_1(iot, ioh, TUL_SYNCM, tcs->syncm);
2228 1.3.2.2 nathanw
2229 1.3.2.7 nathanw abortmsg = MSG_ABORT; /* until a valid tag has been obtained */
2230 1.3.2.2 nathanw
2231 1.3.2.7 nathanw if (tcs->ntagscb != NULL)
2232 1.3.2.7 nathanw /* There is a non-tagged I/O active on the target */
2233 1.3.2.7 nathanw scb = tcs->ntagscb;
2234 1.3.2.3 nathanw
2235 1.3.2.7 nathanw else {
2236 1.3.2.7 nathanw /*
2237 1.3.2.7 nathanw * Since there is no active non-tagged operation
2238 1.3.2.7 nathanw * read the tag type, the tag itself, and find
2239 1.3.2.7 nathanw * the appropriate scb by indexing sc_scb with
2240 1.3.2.7 nathanw * the tag.
2241 1.3.2.7 nathanw */
2242 1.3.2.2 nathanw
2243 1.3.2.7 nathanw switch (iha_wait(sc, MSG_ACCEPT)) {
2244 1.3.2.7 nathanw case -1:
2245 1.3.2.7 nathanw return (-1);
2246 1.3.2.7 nathanw case PHASE_MSG_IN:
2247 1.3.2.7 nathanw bus_space_write_4(iot, ioh, TUL_STCNT0, 1);
2248 1.3.2.7 nathanw if ((iha_wait(sc, XF_FIFO_IN)) == -1)
2249 1.3.2.7 nathanw return (-1);
2250 1.3.2.7 nathanw break;
2251 1.3.2.7 nathanw default:
2252 1.3.2.7 nathanw goto abort;
2253 1.3.2.7 nathanw }
2254 1.3.2.2 nathanw
2255 1.3.2.7 nathanw msg = bus_space_read_1(iot, ioh, TUL_SFIFO); /* Read Tag Msg */
2256 1.3.2.2 nathanw
2257 1.3.2.7 nathanw if ((msg < MSG_SIMPLE_Q_TAG) || (msg > MSG_ORDERED_Q_TAG))
2258 1.3.2.7 nathanw goto abort;
2259 1.3.2.2 nathanw
2260 1.3.2.7 nathanw switch (iha_wait(sc, MSG_ACCEPT)) {
2261 1.3.2.7 nathanw case -1:
2262 1.3.2.7 nathanw return (-1);
2263 1.3.2.7 nathanw case PHASE_MSG_IN:
2264 1.3.2.7 nathanw bus_space_write_4(iot, ioh, TUL_STCNT0, 1);
2265 1.3.2.7 nathanw if ((iha_wait(sc, XF_FIFO_IN)) == -1)
2266 1.3.2.7 nathanw return (-1);
2267 1.3.2.7 nathanw break;
2268 1.3.2.7 nathanw default:
2269 1.3.2.7 nathanw goto abort;
2270 1.3.2.7 nathanw }
2271 1.3.2.2 nathanw
2272 1.3.2.7 nathanw tag = bus_space_read_1(iot, ioh, TUL_SFIFO); /* Read Tag ID */
2273 1.3.2.7 nathanw scb = &sc->sc_scb[tag];
2274 1.3.2.2 nathanw
2275 1.3.2.7 nathanw abortmsg = MSG_ABORT_TAG; /* Now that we have valdid tag! */
2276 1.3.2.7 nathanw }
2277 1.3.2.3 nathanw
2278 1.3.2.7 nathanw if ((scb->target != target)
2279 1.3.2.7 nathanw || (scb->lun != lun)
2280 1.3.2.7 nathanw || (scb->status != STATUS_BUSY)) {
2281 1.3.2.7 nathanw abort:
2282 1.3.2.7 nathanw iha_msgout_abort(sc, abortmsg);
2283 1.3.2.7 nathanw return (-1);
2284 1.3.2.7 nathanw }
2285 1.3.2.2 nathanw
2286 1.3.2.7 nathanw sc->sc_actscb = scb;
2287 1.3.2.2 nathanw
2288 1.3.2.7 nathanw if (iha_wait(sc, MSG_ACCEPT) == -1)
2289 1.3.2.7 nathanw return (-1);
2290 1.3.2.2 nathanw
2291 1.3.2.7 nathanw return (iha_next_state(sc));
2292 1.3.2.2 nathanw }
2293 1.3.2.2 nathanw
2294 1.3.2.7 nathanw static int
2295 1.3.2.7 nathanw iha_msgin(sc)
2296 1.3.2.2 nathanw struct iha_softc *sc;
2297 1.3.2.2 nathanw {
2298 1.3.2.2 nathanw bus_space_tag_t iot = sc->sc_iot;
2299 1.3.2.2 nathanw bus_space_handle_t ioh = sc->sc_ioh;
2300 1.3.2.7 nathanw int flags;
2301 1.3.2.7 nathanw int phase;
2302 1.3.2.7 nathanw u_int8_t msg;
2303 1.3.2.2 nathanw
2304 1.3.2.7 nathanw for (;;) {
2305 1.3.2.7 nathanw if ((bus_space_read_1(iot, ioh, TUL_SFIFOCNT) & FIFOC) > 0)
2306 1.3.2.7 nathanw bus_space_write_1(iot, ioh, TUL_SCTRL0, RSFIFO);
2307 1.3.2.2 nathanw
2308 1.3.2.7 nathanw bus_space_write_4(iot, ioh, TUL_STCNT0, 1);
2309 1.3.2.2 nathanw
2310 1.3.2.7 nathanw phase = iha_wait(sc, XF_FIFO_IN);
2311 1.3.2.7 nathanw msg = bus_space_read_1(iot, ioh, TUL_SFIFO);
2312 1.3.2.2 nathanw
2313 1.3.2.7 nathanw switch (msg) {
2314 1.3.2.7 nathanw case MSG_DISCONNECT:
2315 1.3.2.7 nathanw sc->sc_flags |= FLAG_EXPECT_DISC;
2316 1.3.2.7 nathanw if (iha_wait(sc, MSG_ACCEPT) != -1)
2317 1.3.2.7 nathanw iha_bad_seq(sc);
2318 1.3.2.7 nathanw phase = -1;
2319 1.3.2.7 nathanw break;
2320 1.3.2.7 nathanw case MSG_SAVEDATAPOINTER:
2321 1.3.2.7 nathanw case MSG_RESTOREPOINTERS:
2322 1.3.2.7 nathanw case MSG_NOOP:
2323 1.3.2.7 nathanw phase = iha_wait(sc, MSG_ACCEPT);
2324 1.3.2.7 nathanw break;
2325 1.3.2.7 nathanw case MSG_MESSAGE_REJECT:
2326 1.3.2.7 nathanw /* XXX - need to clear FIFO like other 'Clear ATN'?*/
2327 1.3.2.7 nathanw iha_set_ssig(sc, REQ | BSY | SEL | ATN, 0);
2328 1.3.2.7 nathanw flags = sc->sc_actscb->tcs->flags;
2329 1.3.2.7 nathanw if ((flags & FLAG_NO_NEG_SYNC) == 0)
2330 1.3.2.7 nathanw iha_set_ssig(sc, REQ | BSY | SEL, ATN);
2331 1.3.2.7 nathanw phase = iha_wait(sc, MSG_ACCEPT);
2332 1.3.2.7 nathanw break;
2333 1.3.2.7 nathanw case MSG_EXTENDED:
2334 1.3.2.7 nathanw phase = iha_msgin_extended(sc);
2335 1.3.2.7 nathanw break;
2336 1.3.2.7 nathanw case MSG_IGN_WIDE_RESIDUE:
2337 1.3.2.7 nathanw phase = iha_msgin_ignore_wid_resid(sc);
2338 1.3.2.7 nathanw break;
2339 1.3.2.7 nathanw case MSG_CMDCOMPLETE:
2340 1.3.2.7 nathanw sc->sc_flags |= FLAG_EXPECT_DONE_DISC;
2341 1.3.2.7 nathanw bus_space_write_1(iot, ioh, TUL_SCTRL0, RSFIFO);
2342 1.3.2.7 nathanw phase = iha_wait(sc, MSG_ACCEPT);
2343 1.3.2.7 nathanw if (phase != -1) {
2344 1.3.2.7 nathanw iha_bad_seq(sc);
2345 1.3.2.7 nathanw return (-1);
2346 1.3.2.7 nathanw }
2347 1.3.2.7 nathanw break;
2348 1.3.2.7 nathanw default:
2349 1.3.2.7 nathanw printf("[debug] iha_msgin: bad msg type: %d\n", msg);
2350 1.3.2.7 nathanw phase = iha_msgout_reject(sc);
2351 1.3.2.7 nathanw break;
2352 1.3.2.7 nathanw }
2353 1.3.2.6 nathanw
2354 1.3.2.7 nathanw if (phase != PHASE_MSG_IN)
2355 1.3.2.7 nathanw return (phase);
2356 1.3.2.7 nathanw }
2357 1.3.2.7 nathanw /* NOTREACHED */
2358 1.3.2.2 nathanw }
2359 1.3.2.2 nathanw
2360 1.3.2.7 nathanw static int
2361 1.3.2.7 nathanw iha_msgin_extended(sc)
2362 1.3.2.2 nathanw struct iha_softc *sc;
2363 1.3.2.2 nathanw {
2364 1.3.2.2 nathanw bus_space_tag_t iot = sc->sc_iot;
2365 1.3.2.2 nathanw bus_space_handle_t ioh = sc->sc_ioh;
2366 1.3.2.7 nathanw int flags, i, phase, msglen, msgcode;
2367 1.3.2.2 nathanw
2368 1.3.2.7 nathanw /*
2369 1.3.2.7 nathanw * XXX - can we just stop reading and reject, or do we have to
2370 1.3.2.7 nathanw * read all input, discarding the excess, and then reject
2371 1.3.2.7 nathanw */
2372 1.3.2.7 nathanw for (i = 0; i < IHA_MAX_EXTENDED_MSG; i++) {
2373 1.3.2.7 nathanw phase = iha_wait(sc, MSG_ACCEPT);
2374 1.3.2.2 nathanw
2375 1.3.2.7 nathanw if (phase != PHASE_MSG_IN)
2376 1.3.2.7 nathanw return (phase);
2377 1.3.2.6 nathanw
2378 1.3.2.7 nathanw bus_space_write_4(iot, ioh, TUL_STCNT0, 1);
2379 1.3.2.2 nathanw
2380 1.3.2.7 nathanw if (iha_wait(sc, XF_FIFO_IN) == -1)
2381 1.3.2.7 nathanw return (-1);
2382 1.3.2.2 nathanw
2383 1.3.2.7 nathanw sc->sc_msg[i] = bus_space_read_1(iot, ioh, TUL_SFIFO);
2384 1.3.2.2 nathanw
2385 1.3.2.7 nathanw if (sc->sc_msg[0] == i)
2386 1.3.2.7 nathanw break;
2387 1.3.2.7 nathanw }
2388 1.3.2.2 nathanw
2389 1.3.2.7 nathanw msglen = sc->sc_msg[0];
2390 1.3.2.7 nathanw msgcode = sc->sc_msg[1];
2391 1.3.2.2 nathanw
2392 1.3.2.7 nathanw if ((msglen == MSG_EXT_SDTR_LEN) && (msgcode == MSG_EXT_SDTR)) {
2393 1.3.2.7 nathanw if (iha_msgin_sdtr(sc) == 0) {
2394 1.3.2.7 nathanw iha_sync_done(sc);
2395 1.3.2.7 nathanw return (iha_wait(sc, MSG_ACCEPT));
2396 1.3.2.7 nathanw }
2397 1.3.2.2 nathanw
2398 1.3.2.7 nathanw iha_set_ssig(sc, REQ | BSY | SEL, ATN);
2399 1.3.2.2 nathanw
2400 1.3.2.7 nathanw phase = iha_wait(sc, MSG_ACCEPT);
2401 1.3.2.7 nathanw if (phase != PHASE_MSG_OUT)
2402 1.3.2.7 nathanw return (phase);
2403 1.3.2.2 nathanw
2404 1.3.2.7 nathanw /* Clear FIFO for important message - final SYNC offer */
2405 1.3.2.7 nathanw bus_space_write_1(iot, ioh, TUL_SCTRL0, RSFIFO);
2406 1.3.2.2 nathanw
2407 1.3.2.7 nathanw iha_sync_done(sc); /* This is our final offer */
2408 1.3.2.2 nathanw
2409 1.3.2.7 nathanw } else if ((msglen == MSG_EXT_WDTR_LEN) && (msgcode == MSG_EXT_WDTR)) {
2410 1.3.2.2 nathanw
2411 1.3.2.7 nathanw flags = sc->sc_actscb->tcs->flags;
2412 1.3.2.2 nathanw
2413 1.3.2.7 nathanw if ((flags & FLAG_NO_WIDE) != 0)
2414 1.3.2.7 nathanw /* Offer 8bit xfers only */
2415 1.3.2.7 nathanw sc->sc_msg[2] = MSG_EXT_WDTR_BUS_8_BIT;
2416 1.3.2.2 nathanw
2417 1.3.2.7 nathanw else if (sc->sc_msg[2] > MSG_EXT_WDTR_BUS_32_BIT)
2418 1.3.2.7 nathanw /* BAD MSG */
2419 1.3.2.7 nathanw return (iha_msgout_reject(sc));
2420 1.3.2.2 nathanw
2421 1.3.2.7 nathanw else if (sc->sc_msg[2] == MSG_EXT_WDTR_BUS_32_BIT)
2422 1.3.2.7 nathanw /* Offer 16bit instead */
2423 1.3.2.7 nathanw sc->sc_msg[2] = MSG_EXT_WDTR_BUS_16_BIT;
2424 1.3.2.2 nathanw
2425 1.3.2.7 nathanw else {
2426 1.3.2.7 nathanw iha_wide_done(sc);
2427 1.3.2.7 nathanw if ((flags & FLAG_NO_NEG_SYNC) == 0)
2428 1.3.2.7 nathanw iha_set_ssig(sc, REQ | BSY | SEL, ATN);
2429 1.3.2.7 nathanw return (iha_wait(sc, MSG_ACCEPT));
2430 1.3.2.7 nathanw }
2431 1.3.2.2 nathanw
2432 1.3.2.7 nathanw iha_set_ssig(sc, REQ | BSY | SEL, ATN);
2433 1.3.2.2 nathanw
2434 1.3.2.7 nathanw phase = iha_wait(sc, MSG_ACCEPT);
2435 1.3.2.7 nathanw if (phase != PHASE_MSG_OUT)
2436 1.3.2.7 nathanw return (phase);
2437 1.3.2.7 nathanw } else
2438 1.3.2.7 nathanw return (iha_msgout_reject(sc));
2439 1.3.2.7 nathanw
2440 1.3.2.7 nathanw return (iha_msgout_extended(sc));
2441 1.3.2.2 nathanw }
2442 1.3.2.2 nathanw
2443 1.3.2.2 nathanw /*
2444 1.3.2.7 nathanw * iha_msgin_sdtr - check SDTR msg in sc_msg. If the offer is
2445 1.3.2.7 nathanw * acceptable leave sc_msg as is and return 0.
2446 1.3.2.7 nathanw * If the negotiation must continue, modify sc_msg
2447 1.3.2.7 nathanw * as needed and return 1. Else return 0.
2448 1.3.2.2 nathanw */
2449 1.3.2.2 nathanw static int
2450 1.3.2.7 nathanw iha_msgin_sdtr(sc)
2451 1.3.2.2 nathanw struct iha_softc *sc;
2452 1.3.2.2 nathanw {
2453 1.3.2.7 nathanw int flags;
2454 1.3.2.7 nathanw int newoffer;
2455 1.3.2.7 nathanw u_int8_t default_period;
2456 1.3.2.2 nathanw
2457 1.3.2.7 nathanw flags = sc->sc_actscb->tcs->flags;
2458 1.3.2.2 nathanw
2459 1.3.2.7 nathanw default_period = iha_rate_tbl[flags & FLAG_SCSI_RATE];
2460 1.3.2.2 nathanw
2461 1.3.2.7 nathanw if (sc->sc_msg[3] == 0)
2462 1.3.2.7 nathanw /* target offered async only. Accept it. */
2463 1.3.2.7 nathanw return (0);
2464 1.3.2.2 nathanw
2465 1.3.2.7 nathanw newoffer = 0;
2466 1.3.2.2 nathanw
2467 1.3.2.7 nathanw if ((flags & FLAG_NO_SYNC) != 0) {
2468 1.3.2.7 nathanw sc->sc_msg[3] = 0;
2469 1.3.2.7 nathanw newoffer = 1;
2470 1.3.2.2 nathanw }
2471 1.3.2.2 nathanw
2472 1.3.2.7 nathanw if (sc->sc_msg[3] > IHA_MAX_OFFSET) {
2473 1.3.2.7 nathanw sc->sc_msg[3] = IHA_MAX_OFFSET;
2474 1.3.2.7 nathanw newoffer = 1;
2475 1.3.2.2 nathanw }
2476 1.3.2.2 nathanw
2477 1.3.2.7 nathanw if (sc->sc_msg[2] < default_period) {
2478 1.3.2.7 nathanw sc->sc_msg[2] = default_period;
2479 1.3.2.7 nathanw newoffer = 1;
2480 1.3.2.7 nathanw }
2481 1.3.2.2 nathanw
2482 1.3.2.7 nathanw if (sc->sc_msg[2] > IHA_MAX_PERIOD) {
2483 1.3.2.7 nathanw /* Use async */
2484 1.3.2.7 nathanw sc->sc_msg[3] = 0;
2485 1.3.2.7 nathanw newoffer = 1;
2486 1.3.2.2 nathanw }
2487 1.3.2.2 nathanw
2488 1.3.2.7 nathanw return (newoffer);
2489 1.3.2.2 nathanw }
2490 1.3.2.2 nathanw
2491 1.3.2.7 nathanw static int
2492 1.3.2.7 nathanw iha_msgin_ignore_wid_resid(sc)
2493 1.3.2.2 nathanw struct iha_softc *sc;
2494 1.3.2.2 nathanw {
2495 1.3.2.7 nathanw bus_space_tag_t iot = sc->sc_iot;
2496 1.3.2.7 nathanw bus_space_handle_t ioh = sc->sc_ioh;
2497 1.3.2.7 nathanw int phase;
2498 1.3.2.2 nathanw
2499 1.3.2.7 nathanw phase = iha_wait(sc, MSG_ACCEPT);
2500 1.3.2.2 nathanw
2501 1.3.2.7 nathanw if (phase == PHASE_MSG_IN) {
2502 1.3.2.7 nathanw phase = iha_wait(sc, XF_FIFO_IN);
2503 1.3.2.2 nathanw
2504 1.3.2.7 nathanw if (phase != -1) {
2505 1.3.2.7 nathanw bus_space_write_1(iot, ioh, TUL_SFIFO, 0);
2506 1.3.2.7 nathanw bus_space_read_1(iot, ioh, TUL_SFIFO);
2507 1.3.2.7 nathanw bus_space_read_1(iot, ioh, TUL_SFIFO);
2508 1.3.2.2 nathanw
2509 1.3.2.7 nathanw phase = iha_wait(sc, MSG_ACCEPT);
2510 1.3.2.7 nathanw }
2511 1.3.2.7 nathanw }
2512 1.3.2.2 nathanw
2513 1.3.2.7 nathanw return (phase);
2514 1.3.2.7 nathanw }
2515 1.3.2.2 nathanw
2516 1.3.2.7 nathanw static int
2517 1.3.2.7 nathanw iha_msgout(sc, msg)
2518 1.3.2.7 nathanw struct iha_softc *sc;
2519 1.3.2.7 nathanw u_int8_t msg;
2520 1.3.2.7 nathanw {
2521 1.3.2.2 nathanw
2522 1.3.2.7 nathanw bus_space_write_1(sc->sc_iot, sc->sc_ioh, TUL_SFIFO, msg);
2523 1.3.2.2 nathanw
2524 1.3.2.7 nathanw return (iha_wait(sc, XF_FIFO_OUT));
2525 1.3.2.7 nathanw }
2526 1.3.2.2 nathanw
2527 1.3.2.7 nathanw static void
2528 1.3.2.7 nathanw iha_msgout_abort(sc, aborttype)
2529 1.3.2.7 nathanw struct iha_softc *sc;
2530 1.3.2.7 nathanw u_int8_t aborttype;
2531 1.3.2.7 nathanw {
2532 1.3.2.2 nathanw
2533 1.3.2.7 nathanw iha_set_ssig(sc, REQ | BSY | SEL, ATN);
2534 1.3.2.2 nathanw
2535 1.3.2.7 nathanw switch (iha_wait(sc, MSG_ACCEPT)) {
2536 1.3.2.7 nathanw case -1:
2537 1.3.2.7 nathanw break;
2538 1.3.2.2 nathanw
2539 1.3.2.7 nathanw case PHASE_MSG_OUT:
2540 1.3.2.7 nathanw sc->sc_flags |= FLAG_EXPECT_DISC;
2541 1.3.2.7 nathanw if (iha_msgout(sc, aborttype) != -1)
2542 1.3.2.7 nathanw iha_bad_seq(sc);
2543 1.3.2.7 nathanw break;
2544 1.3.2.2 nathanw
2545 1.3.2.7 nathanw default:
2546 1.3.2.7 nathanw iha_bad_seq(sc);
2547 1.3.2.7 nathanw break;
2548 1.3.2.2 nathanw }
2549 1.3.2.2 nathanw }
2550 1.3.2.2 nathanw
2551 1.3.2.7 nathanw static int
2552 1.3.2.7 nathanw iha_msgout_reject(sc)
2553 1.3.2.7 nathanw struct iha_softc *sc;
2554 1.3.2.2 nathanw {
2555 1.3.2.7 nathanw
2556 1.3.2.7 nathanw iha_set_ssig(sc, REQ | BSY | SEL, ATN);
2557 1.3.2.7 nathanw
2558 1.3.2.7 nathanw if (iha_wait(sc, MSG_ACCEPT) == PHASE_MSG_OUT)
2559 1.3.2.7 nathanw return (iha_msgout(sc, MSG_MESSAGE_REJECT));
2560 1.3.2.7 nathanw
2561 1.3.2.7 nathanw return (-1);
2562 1.3.2.7 nathanw }
2563 1.3.2.7 nathanw
2564 1.3.2.7 nathanw static int
2565 1.3.2.7 nathanw iha_msgout_extended(sc)
2566 1.3.2.2 nathanw struct iha_softc *sc;
2567 1.3.2.7 nathanw {
2568 1.3.2.7 nathanw bus_space_tag_t iot = sc->sc_iot;
2569 1.3.2.7 nathanw bus_space_handle_t ioh = sc->sc_ioh;
2570 1.3.2.7 nathanw int phase;
2571 1.3.2.2 nathanw
2572 1.3.2.7 nathanw bus_space_write_1(iot, ioh, TUL_SFIFO, MSG_EXTENDED);
2573 1.3.2.2 nathanw
2574 1.3.2.7 nathanw bus_space_write_multi_1(iot, ioh, TUL_SFIFO,
2575 1.3.2.7 nathanw sc->sc_msg, sc->sc_msg[0] + 1);
2576 1.3.2.2 nathanw
2577 1.3.2.7 nathanw phase = iha_wait(sc, XF_FIFO_OUT);
2578 1.3.2.2 nathanw
2579 1.3.2.7 nathanw bus_space_write_1(iot, ioh, TUL_SCTRL0, RSFIFO);
2580 1.3.2.7 nathanw iha_set_ssig(sc, REQ | BSY | SEL | ATN, 0);
2581 1.3.2.7 nathanw
2582 1.3.2.7 nathanw return (phase);
2583 1.3.2.2 nathanw }
2584 1.3.2.2 nathanw
2585 1.3.2.7 nathanw static int
2586 1.3.2.7 nathanw iha_msgout_wdtr(sc)
2587 1.3.2.2 nathanw struct iha_softc *sc;
2588 1.3.2.2 nathanw {
2589 1.3.2.2 nathanw
2590 1.3.2.7 nathanw sc->sc_actscb->tcs->flags |= FLAG_WIDE_DONE;
2591 1.3.2.2 nathanw
2592 1.3.2.7 nathanw sc->sc_msg[0] = MSG_EXT_WDTR_LEN;
2593 1.3.2.7 nathanw sc->sc_msg[1] = MSG_EXT_WDTR;
2594 1.3.2.7 nathanw sc->sc_msg[2] = MSG_EXT_WDTR_BUS_16_BIT;
2595 1.3.2.2 nathanw
2596 1.3.2.7 nathanw return (iha_msgout_extended(sc));
2597 1.3.2.7 nathanw }
2598 1.3.2.2 nathanw
2599 1.3.2.7 nathanw static int
2600 1.3.2.7 nathanw iha_msgout_sdtr(sc)
2601 1.3.2.7 nathanw struct iha_softc *sc;
2602 1.3.2.7 nathanw {
2603 1.3.2.7 nathanw struct tcs *tcs = sc->sc_actscb->tcs;
2604 1.3.2.2 nathanw
2605 1.3.2.7 nathanw tcs->flags |= FLAG_SYNC_DONE;
2606 1.3.2.2 nathanw
2607 1.3.2.7 nathanw sc->sc_msg[0] = MSG_EXT_SDTR_LEN;
2608 1.3.2.7 nathanw sc->sc_msg[1] = MSG_EXT_SDTR;
2609 1.3.2.7 nathanw sc->sc_msg[2] = iha_rate_tbl[tcs->flags & FLAG_SCSI_RATE];
2610 1.3.2.7 nathanw sc->sc_msg[3] = IHA_MAX_OFFSET; /* REQ/ACK */
2611 1.3.2.2 nathanw
2612 1.3.2.7 nathanw return (iha_msgout_extended(sc));
2613 1.3.2.7 nathanw }
2614 1.3.2.2 nathanw
2615 1.3.2.7 nathanw static void
2616 1.3.2.7 nathanw iha_wide_done(sc)
2617 1.3.2.7 nathanw struct iha_softc *sc;
2618 1.3.2.7 nathanw {
2619 1.3.2.7 nathanw bus_space_tag_t iot = sc->sc_iot;
2620 1.3.2.7 nathanw bus_space_handle_t ioh = sc->sc_ioh;
2621 1.3.2.7 nathanw struct tcs *tcs = sc->sc_actscb->tcs;
2622 1.3.2.2 nathanw
2623 1.3.2.7 nathanw tcs->syncm = 0;
2624 1.3.2.7 nathanw tcs->period = 0;
2625 1.3.2.7 nathanw tcs->offset = 0;
2626 1.3.2.2 nathanw
2627 1.3.2.7 nathanw if (sc->sc_msg[2] != 0)
2628 1.3.2.7 nathanw tcs->syncm |= PERIOD_WIDE_SCSI;
2629 1.3.2.2 nathanw
2630 1.3.2.7 nathanw tcs->sconfig0 &= ~ALTPD;
2631 1.3.2.7 nathanw tcs->flags &= ~FLAG_SYNC_DONE;
2632 1.3.2.7 nathanw tcs->flags |= FLAG_WIDE_DONE;
2633 1.3.2.2 nathanw
2634 1.3.2.7 nathanw iha_update_xfer_mode(sc, sc->sc_actscb->target);
2635 1.3.2.2 nathanw
2636 1.3.2.7 nathanw bus_space_write_1(iot, ioh, TUL_SCONFIG0, tcs->sconfig0);
2637 1.3.2.7 nathanw bus_space_write_1(iot, ioh, TUL_SYNCM, tcs->syncm);
2638 1.3.2.2 nathanw }
2639 1.3.2.2 nathanw
2640 1.3.2.2 nathanw static void
2641 1.3.2.7 nathanw iha_sync_done(sc)
2642 1.3.2.2 nathanw struct iha_softc *sc;
2643 1.3.2.2 nathanw {
2644 1.3.2.2 nathanw bus_space_tag_t iot = sc->sc_iot;
2645 1.3.2.2 nathanw bus_space_handle_t ioh = sc->sc_ioh;
2646 1.3.2.7 nathanw struct tcs *tcs = sc->sc_actscb->tcs;
2647 1.3.2.7 nathanw int i;
2648 1.3.2.2 nathanw
2649 1.3.2.7 nathanw tcs->period = sc->sc_msg[2];
2650 1.3.2.7 nathanw tcs->offset = sc->sc_msg[3];
2651 1.3.2.7 nathanw if (tcs->offset != 0) {
2652 1.3.2.7 nathanw tcs->syncm |= tcs->offset;
2653 1.3.2.7 nathanw
2654 1.3.2.7 nathanw /* pick the highest possible rate */
2655 1.3.2.7 nathanw for (i = 0; i < sizeof(iha_rate_tbl); i++)
2656 1.3.2.7 nathanw if (iha_rate_tbl[i] >= tcs->period)
2657 1.3.2.7 nathanw break;
2658 1.3.2.7 nathanw
2659 1.3.2.7 nathanw tcs->syncm |= (i << 4);
2660 1.3.2.7 nathanw tcs->sconfig0 |= ALTPD;
2661 1.3.2.7 nathanw }
2662 1.3.2.7 nathanw
2663 1.3.2.7 nathanw tcs->flags |= FLAG_SYNC_DONE;
2664 1.3.2.7 nathanw
2665 1.3.2.7 nathanw iha_update_xfer_mode(sc, sc->sc_actscb->target);
2666 1.3.2.7 nathanw
2667 1.3.2.7 nathanw bus_space_write_1(iot, ioh, TUL_SCONFIG0, tcs->sconfig0);
2668 1.3.2.7 nathanw bus_space_write_1(iot, ioh, TUL_SYNCM, tcs->syncm);
2669 1.3.2.2 nathanw }
2670 1.3.2.2 nathanw
2671 1.3.2.2 nathanw /*
2672 1.3.2.7 nathanw * iha_bad_seq - a SCSI bus phase was encountered out of the
2673 1.3.2.7 nathanw * correct/expected sequence. Reset the SCSI bus.
2674 1.3.2.2 nathanw */
2675 1.3.2.7 nathanw static void
2676 1.3.2.7 nathanw iha_bad_seq(sc)
2677 1.3.2.2 nathanw struct iha_softc *sc;
2678 1.3.2.2 nathanw {
2679 1.3.2.7 nathanw struct iha_scb *scb = sc->sc_actscb;
2680 1.3.2.2 nathanw
2681 1.3.2.7 nathanw if (scb != NULL)
2682 1.3.2.7 nathanw iha_append_done_scb(sc, scb, HOST_BAD_PHAS);
2683 1.3.2.2 nathanw
2684 1.3.2.7 nathanw iha_reset_scsi_bus(sc);
2685 1.3.2.7 nathanw iha_reset_chip(sc);
2686 1.3.2.2 nathanw }
2687 1.3.2.2 nathanw
2688 1.3.2.2 nathanw /*
2689 1.3.2.3 nathanw * iha_read_eeprom - read Serial EEPROM value & set to defaults
2690 1.3.2.2 nathanw * if required. XXX - Writing does NOT work!
2691 1.3.2.2 nathanw */
2692 1.3.2.7 nathanw static void
2693 1.3.2.3 nathanw iha_read_eeprom(sc, eeprom)
2694 1.3.2.2 nathanw struct iha_softc *sc;
2695 1.3.2.2 nathanw struct iha_eeprom *eeprom;
2696 1.3.2.2 nathanw {
2697 1.3.2.2 nathanw bus_space_tag_t iot = sc->sc_iot;
2698 1.3.2.2 nathanw bus_space_handle_t ioh = sc->sc_ioh;
2699 1.3.2.2 nathanw u_int16_t *buf = (u_int16_t *)eeprom;
2700 1.3.2.2 nathanw u_int8_t gctrl;
2701 1.3.2.2 nathanw
2702 1.3.2.3 nathanw /* Enable EEProm programming */
2703 1.3.2.2 nathanw gctrl = bus_space_read_1(iot, ioh, TUL_GCTRL0) | EEPRG;
2704 1.3.2.2 nathanw bus_space_write_1(iot, ioh, TUL_GCTRL0, gctrl);
2705 1.3.2.2 nathanw
2706 1.3.2.3 nathanw /* Read EEProm */
2707 1.3.2.3 nathanw if (iha_se2_rd_all(sc, buf) == 0)
2708 1.3.2.10 nathanw panic("%s: cannot read EEPROM", sc->sc_dev.dv_xname);
2709 1.3.2.2 nathanw
2710 1.3.2.3 nathanw /* Disable EEProm programming */
2711 1.3.2.2 nathanw gctrl = bus_space_read_1(iot, ioh, TUL_GCTRL0) & ~EEPRG;
2712 1.3.2.2 nathanw bus_space_write_1(iot, ioh, TUL_GCTRL0, gctrl);
2713 1.3.2.2 nathanw }
2714 1.3.2.2 nathanw
2715 1.3.2.3 nathanw #ifdef notused
2716 1.3.2.2 nathanw /*
2717 1.3.2.3 nathanw * iha_se2_update_all - Update SCSI H/A configuration parameters from
2718 1.3.2.2 nathanw * serial EEPROM Setup default pattern. Only
2719 1.3.2.2 nathanw * change those values different from the values
2720 1.3.2.3 nathanw * in iha_eeprom.
2721 1.3.2.2 nathanw */
2722 1.3.2.7 nathanw static void
2723 1.3.2.3 nathanw iha_se2_update_all(sc)
2724 1.3.2.2 nathanw struct iha_softc *sc;
2725 1.3.2.2 nathanw {
2726 1.3.2.2 nathanw bus_space_tag_t iot = sc->sc_iot;
2727 1.3.2.2 nathanw bus_space_handle_t ioh = sc->sc_ioh;
2728 1.3.2.2 nathanw u_int16_t *np;
2729 1.3.2.2 nathanw u_int32_t chksum;
2730 1.3.2.2 nathanw int i;
2731 1.3.2.2 nathanw
2732 1.3.2.2 nathanw /* Enable erase/write state of EEPROM */
2733 1.3.2.3 nathanw iha_se2_instr(sc, ENABLE_ERASE);
2734 1.3.2.2 nathanw bus_space_write_1(iot, ioh, TUL_NVRAM, 0);
2735 1.3.2.2 nathanw EEP_WAIT();
2736 1.3.2.2 nathanw
2737 1.3.2.2 nathanw np = (u_int16_t *)&eeprom_default;
2738 1.3.2.2 nathanw
2739 1.3.2.2 nathanw for (i = 0, chksum = 0; i < EEPROM_SIZE - 1; i++) {
2740 1.3.2.3 nathanw iha_se2_wr(sc, i, *np);
2741 1.3.2.2 nathanw chksum += *np++;
2742 1.3.2.2 nathanw }
2743 1.3.2.2 nathanw
2744 1.3.2.2 nathanw chksum &= 0x0000ffff;
2745 1.3.2.3 nathanw iha_se2_wr(sc, 31, chksum);
2746 1.3.2.2 nathanw
2747 1.3.2.2 nathanw /* Disable erase/write state of EEPROM */
2748 1.3.2.3 nathanw iha_se2_instr(sc, 0);
2749 1.3.2.2 nathanw bus_space_write_1(iot, ioh, TUL_NVRAM, 0);
2750 1.3.2.2 nathanw EEP_WAIT();
2751 1.3.2.2 nathanw }
2752 1.3.2.2 nathanw
2753 1.3.2.2 nathanw /*
2754 1.3.2.3 nathanw * iha_se2_wr - write the given 16 bit value into the Serial EEPROM
2755 1.3.2.2 nathanw * at the specified offset
2756 1.3.2.2 nathanw */
2757 1.3.2.7 nathanw static void
2758 1.3.2.3 nathanw iha_se2_wr(sc, addr, writeword)
2759 1.3.2.2 nathanw struct iha_softc *sc;
2760 1.3.2.2 nathanw int addr;
2761 1.3.2.2 nathanw u_int16_t writeword;
2762 1.3.2.2 nathanw {
2763 1.3.2.2 nathanw bus_space_tag_t iot = sc->sc_iot;
2764 1.3.2.2 nathanw bus_space_handle_t ioh = sc->sc_ioh;
2765 1.3.2.2 nathanw int i, bit;
2766 1.3.2.2 nathanw
2767 1.3.2.2 nathanw /* send 'WRITE' Instruction == address | WRITE bit */
2768 1.3.2.3 nathanw iha_se2_instr(sc, addr | WRITE);
2769 1.3.2.2 nathanw
2770 1.3.2.2 nathanw for (i = 16; i > 0; i--) {
2771 1.3.2.2 nathanw if (writeword & (1 << (i - 1)))
2772 1.3.2.2 nathanw bus_space_write_1(iot, ioh, TUL_NVRAM, NVRCS | NVRDO);
2773 1.3.2.2 nathanw else
2774 1.3.2.2 nathanw bus_space_write_1(iot, ioh, TUL_NVRAM, NVRCS);
2775 1.3.2.2 nathanw EEP_WAIT();
2776 1.3.2.2 nathanw bus_space_write_1(iot, ioh, TUL_NVRAM, NVRCS | NVRCK);
2777 1.3.2.2 nathanw EEP_WAIT();
2778 1.3.2.2 nathanw }
2779 1.3.2.2 nathanw
2780 1.3.2.2 nathanw bus_space_write_1(iot, ioh, TUL_NVRAM, NVRCS);
2781 1.3.2.2 nathanw EEP_WAIT();
2782 1.3.2.2 nathanw bus_space_write_1(iot, ioh, TUL_NVRAM, 0);
2783 1.3.2.2 nathanw EEP_WAIT();
2784 1.3.2.2 nathanw bus_space_write_1(iot, ioh, TUL_NVRAM, NVRCS);
2785 1.3.2.2 nathanw EEP_WAIT();
2786 1.3.2.2 nathanw
2787 1.3.2.2 nathanw for (;;) {
2788 1.3.2.2 nathanw bus_space_write_1(iot, ioh, TUL_NVRAM, NVRCS | NVRCK);
2789 1.3.2.2 nathanw EEP_WAIT();
2790 1.3.2.2 nathanw bus_space_write_1(iot, ioh, TUL_NVRAM, NVRCS);
2791 1.3.2.2 nathanw EEP_WAIT();
2792 1.3.2.2 nathanw bit = bus_space_read_1(iot, ioh, TUL_NVRAM) & NVRDI;
2793 1.3.2.2 nathanw EEP_WAIT();
2794 1.3.2.2 nathanw if (bit != 0)
2795 1.3.2.2 nathanw break; /* write complete */
2796 1.3.2.2 nathanw }
2797 1.3.2.2 nathanw
2798 1.3.2.2 nathanw bus_space_write_1(iot, ioh, TUL_NVRAM, 0);
2799 1.3.2.2 nathanw }
2800 1.3.2.3 nathanw #endif
2801 1.3.2.2 nathanw
2802 1.3.2.2 nathanw /*
2803 1.3.2.3 nathanw * iha_se2_rd - read & return the 16 bit value at the specified
2804 1.3.2.2 nathanw * offset in the Serial E2PROM
2805 1.3.2.2 nathanw *
2806 1.3.2.2 nathanw */
2807 1.3.2.7 nathanw static u_int16_t
2808 1.3.2.3 nathanw iha_se2_rd(sc, addr)
2809 1.3.2.2 nathanw struct iha_softc *sc;
2810 1.3.2.2 nathanw int addr;
2811 1.3.2.2 nathanw {
2812 1.3.2.2 nathanw bus_space_tag_t iot = sc->sc_iot;
2813 1.3.2.2 nathanw bus_space_handle_t ioh = sc->sc_ioh;
2814 1.3.2.2 nathanw int i, bit;
2815 1.3.2.2 nathanw u_int16_t readword;
2816 1.3.2.2 nathanw
2817 1.3.2.2 nathanw /* Send 'READ' instruction == address | READ bit */
2818 1.3.2.3 nathanw iha_se2_instr(sc, addr | READ);
2819 1.3.2.2 nathanw
2820 1.3.2.2 nathanw readword = 0;
2821 1.3.2.2 nathanw for (i = 16; i > 0; i--) {
2822 1.3.2.2 nathanw bus_space_write_1(iot, ioh, TUL_NVRAM, NVRCS | NVRCK);
2823 1.3.2.2 nathanw EEP_WAIT();
2824 1.3.2.2 nathanw bus_space_write_1(iot, ioh, TUL_NVRAM, NVRCS);
2825 1.3.2.2 nathanw EEP_WAIT();
2826 1.3.2.2 nathanw /* sample data after the following edge of clock */
2827 1.3.2.2 nathanw bit = bus_space_read_1(iot, ioh, TUL_NVRAM) & NVRDI ? 1 : 0;
2828 1.3.2.2 nathanw EEP_WAIT();
2829 1.3.2.2 nathanw
2830 1.3.2.2 nathanw readword |= bit << (i - 1);
2831 1.3.2.2 nathanw }
2832 1.3.2.2 nathanw
2833 1.3.2.2 nathanw bus_space_write_1(iot, ioh, TUL_NVRAM, 0);
2834 1.3.2.2 nathanw
2835 1.3.2.2 nathanw return (readword);
2836 1.3.2.2 nathanw }
2837 1.3.2.2 nathanw
2838 1.3.2.2 nathanw /*
2839 1.3.2.3 nathanw * iha_se2_rd_all - Read SCSI H/A config parameters from serial EEPROM
2840 1.3.2.2 nathanw */
2841 1.3.2.7 nathanw static int
2842 1.3.2.3 nathanw iha_se2_rd_all(sc, buf)
2843 1.3.2.2 nathanw struct iha_softc *sc;
2844 1.3.2.2 nathanw u_int16_t *buf;
2845 1.3.2.2 nathanw {
2846 1.3.2.2 nathanw struct iha_eeprom *eeprom = (struct iha_eeprom *)buf;
2847 1.3.2.2 nathanw u_int32_t chksum;
2848 1.3.2.2 nathanw int i;
2849 1.3.2.2 nathanw
2850 1.3.2.2 nathanw for (i = 0, chksum = 0; i < EEPROM_SIZE - 1; i++) {
2851 1.3.2.3 nathanw *buf = iha_se2_rd(sc, i);
2852 1.3.2.2 nathanw chksum += *buf++;
2853 1.3.2.2 nathanw }
2854 1.3.2.3 nathanw *buf = iha_se2_rd(sc, 31); /* read checksum from EEPROM */
2855 1.3.2.2 nathanw
2856 1.3.2.3 nathanw chksum &= 0x0000ffff; /* lower 16 bits */
2857 1.3.2.2 nathanw
2858 1.3.2.2 nathanw return (eeprom->signature == EEP_SIGNATURE) &&
2859 1.3.2.2 nathanw (eeprom->checksum == chksum);
2860 1.3.2.2 nathanw }
2861 1.3.2.2 nathanw
2862 1.3.2.2 nathanw /*
2863 1.3.2.3 nathanw * iha_se2_instr - write an octet to serial E2PROM one bit at a time
2864 1.3.2.2 nathanw */
2865 1.3.2.7 nathanw static void
2866 1.3.2.3 nathanw iha_se2_instr(sc, instr)
2867 1.3.2.2 nathanw struct iha_softc *sc;
2868 1.3.2.2 nathanw int instr;
2869 1.3.2.2 nathanw {
2870 1.3.2.2 nathanw bus_space_tag_t iot = sc->sc_iot;
2871 1.3.2.2 nathanw bus_space_handle_t ioh = sc->sc_ioh;
2872 1.3.2.2 nathanw int b, i;
2873 1.3.2.2 nathanw
2874 1.3.2.2 nathanw b = NVRCS | NVRDO; /* Write the start bit (== 1) */
2875 1.3.2.2 nathanw
2876 1.3.2.2 nathanw bus_space_write_1(iot, ioh, TUL_NVRAM, b);
2877 1.3.2.2 nathanw EEP_WAIT();
2878 1.3.2.2 nathanw bus_space_write_1(iot, ioh, TUL_NVRAM, b | NVRCK);
2879 1.3.2.2 nathanw EEP_WAIT();
2880 1.3.2.2 nathanw
2881 1.3.2.2 nathanw for (i = 8; i > 0; i--) {
2882 1.3.2.2 nathanw if (instr & (1 << (i - 1)))
2883 1.3.2.2 nathanw b = NVRCS | NVRDO; /* Write a 1 bit */
2884 1.3.2.2 nathanw else
2885 1.3.2.2 nathanw b = NVRCS; /* Write a 0 bit */
2886 1.3.2.2 nathanw
2887 1.3.2.2 nathanw bus_space_write_1(iot, ioh, TUL_NVRAM, b);
2888 1.3.2.2 nathanw EEP_WAIT();
2889 1.3.2.2 nathanw bus_space_write_1(iot, ioh, TUL_NVRAM, b | NVRCK);
2890 1.3.2.2 nathanw EEP_WAIT();
2891 1.3.2.2 nathanw }
2892 1.3.2.2 nathanw
2893 1.3.2.2 nathanw bus_space_write_1(iot, ioh, TUL_NVRAM, NVRCS);
2894 1.3.2.2 nathanw }
2895